blob: 6bc02e0a97291769c9572c6ad2af3245b2fe013e [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"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000017#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000018#include "clang/AST/Expr.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/Basic/IdentifierTable.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000020
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Chris Lattnerd3b90652008-03-15 05:43:15 +000023//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000024// Decl Allocation/Deallocation Method Implementations
25//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000026
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000027TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
Steve Naroff3e970492009-01-27 21:25:57 +000028 return new (C) TranslationUnitDecl();
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000029}
30
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000031NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
32 SourceLocation L, IdentifierInfo *Id) {
Steve Naroff3e970492009-01-27 21:25:57 +000033 return new (C) NamespaceDecl(DC, L, Id);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000034}
35
Ted Kremenekd1ac17a2008-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 Kremenekebf27b12008-05-24 15:09:56 +000040 this->~NamespaceDecl();
Steve Naroff3e970492009-01-27 21:25:57 +000041 C.Deallocate((void *)this);
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000042}
43
44
Chris Lattner41110242008-06-17 18:05:57 +000045ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000046 SourceLocation L, IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +000047 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
Chris Lattner41110242008-06-17 18:05:57 +000048}
49
Chris Lattner9fdf9c62008-04-22 18:39:57 +000050ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000051 SourceLocation L, IdentifierInfo *Id,
52 QualType T, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000053 Expr *DefArg) {
Steve Naroff3e970492009-01-27 21:25:57 +000054 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg);
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000055}
56
57QualType ParmVarDecl::getOriginalType() const {
Douglas Gregor64650af2009-02-02 23:39:07 +000058 if (const OriginalParmVarDecl *PVD =
59 dyn_cast<OriginalParmVarDecl>(this))
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +000060 return PVD->OriginalType;
61 return getType();
Chris Lattner9e151e12008-03-15 21:10:16 +000062}
63
Douglas Gregor64650af2009-02-02 23:39:07 +000064OriginalParmVarDecl *OriginalParmVarDecl::Create(
Fariborz Jahanian73da9e42008-12-20 20:56:12 +000065 ASTContext &C, DeclContext *DC,
66 SourceLocation L, IdentifierInfo *Id,
67 QualType T, QualType OT, StorageClass S,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000068 Expr *DefArg) {
Douglas Gregor64650af2009-02-02 23:39:07 +000069 return new (C) OriginalParmVarDecl(DC, L, Id, T, OT, S, DefArg);
Fariborz Jahanian73da9e42008-12-20 20:56:12 +000070}
71
Chris Lattner9fdf9c62008-04-22 18:39:57 +000072FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000073 SourceLocation L,
Douglas Gregor10bd3682008-11-17 22:58:34 +000074 DeclarationName N, QualType T,
Chris Lattnera98e58d2008-03-15 21:24:04 +000075 StorageClass S, bool isInline,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000076 SourceLocation TypeSpecStartLoc) {
Steve Naroff3e970492009-01-27 21:25:57 +000077 return new (C) FunctionDecl(Function, DC, L, N, T, S, isInline,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000078 TypeSpecStartLoc);
Chris Lattnera98e58d2008-03-15 21:24:04 +000079}
80
Steve Naroff090276f2008-10-10 01:28:17 +000081BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff3e970492009-01-27 21:25:57 +000082 return new (C) BlockDecl(DC, L);
Steve Naroff56ee6892008-10-08 17:01:13 +000083}
84
Douglas Gregor44b43212008-12-11 16:49:14 +000085FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
86 IdentifierInfo *Id, QualType T, Expr *BW,
Douglas Gregor4afa39d2009-01-20 01:17:11 +000087 bool Mutable) {
Steve Naroff3e970492009-01-27 21:25:57 +000088 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable);
Chris Lattner8e25d862008-03-16 00:16:02 +000089}
90
Douglas Gregor6b3945f2009-01-07 19:46:03 +000091bool FieldDecl::isAnonymousStructOrUnion() const {
92 if (!isImplicit() || getDeclName())
93 return false;
94
95 if (const RecordType *Record = getType()->getAsRecordType())
96 return Record->getDecl()->isAnonymousStructOrUnion();
97
98 return false;
99}
Chris Lattnera98e58d2008-03-15 21:24:04 +0000100
Chris Lattner0ed844b2008-04-04 06:12:32 +0000101EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
102 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000103 IdentifierInfo *Id, QualType T,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000104 Expr *E, const llvm::APSInt &V) {
Steve Naroff3e970492009-01-27 21:25:57 +0000105 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000106}
107
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000108void EnumConstantDecl::Destroy(ASTContext& C) {
109 if (Init) Init->Destroy(C);
110 Decl::Destroy(C);
111}
112
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000113TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000114 SourceLocation L,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000115 IdentifierInfo *Id, QualType T) {
Steve Naroff3e970492009-01-27 21:25:57 +0000116 return new (C) TypedefDecl(DC, L, Id, T);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000117}
118
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000119EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000120 IdentifierInfo *Id,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000121 EnumDecl *PrevDecl) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000122 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id);
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000123 C.getTypeDeclType(Enum, PrevDecl);
124 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000125}
126
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000127void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000128 Decl::Destroy(C);
129}
130
Douglas Gregor44b43212008-12-11 16:49:14 +0000131void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
132 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000133 IntegerType = NewType;
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000134 TagDecl::completeDefinition();
Douglas Gregor44b43212008-12-11 16:49:14 +0000135}
136
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000137FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000138 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000139 StringLiteral *Str) {
Steve Naroff3e970492009-01-27 21:25:57 +0000140 return new (C) FileScopeAsmDecl(DC, L, Str);
Chris Lattner8e25d862008-03-16 00:16:02 +0000141}
142
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000143//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000144// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000145//===----------------------------------------------------------------------===//
146
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000147bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000148 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
149
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000150 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
151 // We want to keep it, unless it nominates same namespace.
152 if (getKind() == Decl::UsingDirective) {
153 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
154 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
155 }
156
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000157 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
158 // For function declarations, we keep track of redeclarations.
159 return FD->getPreviousDeclaration() == OldD;
160
161 // For non-function declarations, if the declarations are of the
162 // same kind then this must be a redeclaration, or semantic analysis
163 // would not have given us the new declaration.
164 return this->getKind() == OldD->getKind();
165}
166
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000167
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000168//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000169// VarDecl Implementation
170//===----------------------------------------------------------------------===//
171
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000172VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
173 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000174 SourceLocation TypeSpecStartLoc) {
Steve Naroff3e970492009-01-27 21:25:57 +0000175 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000176}
177
178void VarDecl::Destroy(ASTContext& C) {
179 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000180 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000181}
182
183VarDecl::~VarDecl() {
184 delete getInit();
185}
186
187//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000188// FunctionDecl Implementation
189//===----------------------------------------------------------------------===//
190
Ted Kremenek27f8a282008-05-20 00:43:19 +0000191void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-05-20 03:56:00 +0000192 if (Body)
193 Body->Destroy(C);
194
195 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
196 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000197
Steve Naroff3e970492009-01-27 21:25:57 +0000198 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000199
Ted Kremenek27f8a282008-05-20 00:43:19 +0000200 Decl::Destroy(C);
201}
202
203
Douglas Gregorf0097952008-04-21 02:02:58 +0000204Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
205 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
206 if (FD->Body) {
207 Definition = FD;
208 return FD->Body;
209 }
210 }
211
212 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000213}
214
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000215// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
216static unsigned getNumTypeParams(QualType T) {
217 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000218 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000219 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000220 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000221}
222
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000223unsigned FunctionDecl::getNumParams() const {
224 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
225 if (!ParamInfo)
226 return 0;
227
228 return getNumTypeParams(getType());
229}
230
Ted Kremenekfc767612009-01-14 00:42:25 +0000231void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
232 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000234 assert(NumParams == getNumTypeParams(getType()) &&
235 "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000236
237 // Zero params -> null pointer.
238 if (NumParams) {
Steve Naroffc0ac4922009-01-27 23:20:32 +0000239 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +0000240 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
242 }
243}
244
Chris Lattner8123a952008-04-10 02:22:51 +0000245/// getMinRequiredArguments - Returns the minimum number of arguments
246/// needed to call this function. This may be fewer than the number of
247/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000248/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000249unsigned FunctionDecl::getMinRequiredArguments() const {
250 unsigned NumRequiredArgs = getNumParams();
251 while (NumRequiredArgs > 0
252 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
253 --NumRequiredArgs;
254
255 return NumRequiredArgs;
256}
257
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000258/// getOverloadedOperator - Which C++ overloaded operator this
259/// function represents, if any.
260OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000261 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
262 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000263 else
264 return OO_None;
265}
266
Chris Lattner8a934232008-03-31 00:36:02 +0000267//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000268// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000269//===----------------------------------------------------------------------===//
270
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000271void TagDecl::startDefinition() {
272 cast<TagType>(TypeForDecl)->decl.setPointer(this);
273 cast<TagType>(TypeForDecl)->decl.setInt(1);
274}
275
276void TagDecl::completeDefinition() {
277 assert((!TypeForDecl ||
278 cast<TagType>(TypeForDecl)->decl.getPointer() == this) &&
279 "Attempt to redefine a tag definition?");
280 IsDefinition = true;
281 cast<TagType>(TypeForDecl)->decl.setPointer(this);
282 cast<TagType>(TypeForDecl)->decl.setInt(0);
283}
284
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000285TagDecl* TagDecl::getDefinition(ASTContext& C) const {
286 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
287 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
288 return D->isDefinition() ? D : 0;
289}
290
291//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000292// RecordDecl Implementation
293//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000294
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000295RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenekdf042e62008-09-05 01:34:33 +0000296 IdentifierInfo *Id)
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000297 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek63597922008-09-02 21:12:32 +0000298 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000299 AnonymousStructOrUnion = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000300 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000301}
302
303RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000304 SourceLocation L, IdentifierInfo *Id,
305 RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000306
Steve Naroff3e970492009-01-27 21:25:57 +0000307 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000308 C.getTypeDeclType(R, PrevDecl);
309 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000310}
311
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000312RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000313}
314
315void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000316 TagDecl::Destroy(C);
317}
318
Douglas Gregor44b43212008-12-11 16:49:14 +0000319/// completeDefinition - Notes that the definition of this type is now
320/// complete.
321void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000323 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000324}
325
Steve Naroff56ee6892008-10-08 17:01:13 +0000326//===----------------------------------------------------------------------===//
327// BlockDecl Implementation
328//===----------------------------------------------------------------------===//
329
330BlockDecl::~BlockDecl() {
331}
332
333void BlockDecl::Destroy(ASTContext& C) {
334 if (Body)
335 Body->Destroy(C);
336
337 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
338 (*I)->Destroy(C);
339
340 Decl::Destroy(C);
341}