blob: 4628761de59587b1cdad960f6555e37aae353402 [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"
17#include "clang/Basic/IdentifierTable.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000018
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Chris Lattnerd3b90652008-03-15 05:43:15 +000021//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000022// Decl Allocation/Deallocation Method Implementations
23//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000024
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000025TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
26 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
27 return new (Mem) TranslationUnitDecl();
28}
29
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000030NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
31 SourceLocation L, IdentifierInfo *Id) {
32 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
33 return new (Mem) NamespaceDecl(DC, L, Id);
34}
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();
Ted Kremenekd1ac17a2008-05-20 04:49:55 +000041 C.getAllocator().Deallocate((void *)this);
42}
43
44
Chris Lattner41110242008-06-17 18:05:57 +000045ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
46 SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) {
47 void *Mem = C.getAllocator().Allocate<ImplicitParamDecl>();
48 return new (Mem) ImplicitParamDecl(ImplicitParam, DC, L, Id, T, PrevDecl);
49}
50
Chris Lattner9fdf9c62008-04-22 18:39:57 +000051VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff248a7532008-04-15 22:42:06 +000052 SourceLocation L,
53 IdentifierInfo *Id, QualType T,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000054 StorageClass S, ScopedDecl *PrevDecl,
55 SourceLocation TypeSpecStartLoc) {
Steve Naroff248a7532008-04-15 22:42:06 +000056 void *Mem = C.getAllocator().Allocate<VarDecl>();
Steve Naroff0eb07bf2008-10-03 00:02:03 +000057 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl, TypeSpecStartLoc);
Chris Lattner9e151e12008-03-15 21:10:16 +000058}
59
Chris Lattner9fdf9c62008-04-22 18:39:57 +000060ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000061 SourceLocation L, IdentifierInfo *Id,
62 QualType T, StorageClass S,
Chris Lattner04421082008-04-08 04:40:51 +000063 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +000064 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +000065 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +000066}
67
Chris Lattner9fdf9c62008-04-22 18:39:57 +000068FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +000069 SourceLocation L,
Douglas Gregor10bd3682008-11-17 22:58:34 +000070 DeclarationName N, QualType T,
Chris Lattnera98e58d2008-03-15 21:24:04 +000071 StorageClass S, bool isInline,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000072 ScopedDecl *PrevDecl,
73 SourceLocation TypeSpecStartLoc) {
Chris Lattnera98e58d2008-03-15 21:24:04 +000074 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Douglas Gregor10bd3682008-11-17 22:58:34 +000075 return new (Mem) FunctionDecl(Function, DC, L, N, T, S, isInline, PrevDecl,
Steve Naroff0eb07bf2008-10-03 00:02:03 +000076 TypeSpecStartLoc);
Chris Lattnera98e58d2008-03-15 21:24:04 +000077}
78
Steve Naroff090276f2008-10-10 01:28:17 +000079BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff56ee6892008-10-08 17:01:13 +000080 void *Mem = C.getAllocator().Allocate<BlockDecl>();
Steve Naroff090276f2008-10-10 01:28:17 +000081 return new (Mem) 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,
86 bool Mutable, ScopedDecl *PrevDecl) {
Chris Lattner8e25d862008-03-16 00:16:02 +000087 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Douglas Gregor44b43212008-12-11 16:49:14 +000088 return new (Mem) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable, PrevDecl);
Chris Lattner8e25d862008-03-16 00:16:02 +000089}
90
Chris Lattnera98e58d2008-03-15 21:24:04 +000091
Chris Lattner0ed844b2008-04-04 06:12:32 +000092EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
93 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +000094 IdentifierInfo *Id, QualType T,
95 Expr *E, const llvm::APSInt &V,
96 ScopedDecl *PrevDecl){
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000097 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +000098 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000099}
100
Ted Kremenekd1ac17a2008-05-20 04:49:55 +0000101void EnumConstantDecl::Destroy(ASTContext& C) {
102 if (Init) Init->Destroy(C);
103 Decl::Destroy(C);
104}
105
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000106TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000107 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000108 IdentifierInfo *Id, QualType T,
109 ScopedDecl *PD) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000110 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000111 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000112}
113
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000114EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000115 IdentifierInfo *Id,
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000116 EnumDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000117 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Douglas Gregor7df7b6b2008-12-15 16:32:14 +0000118 EnumDecl *Enum = new (Mem) EnumDecl(DC, L, Id, 0);
119 C.getTypeDeclType(Enum, PrevDecl);
120 return Enum;
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000121}
122
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000123void EnumDecl::Destroy(ASTContext& C) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000124 DeclContext::DestroyDecls(C);
Ted Kremenekdf91eca2008-09-02 20:13:32 +0000125 Decl::Destroy(C);
126}
127
Douglas Gregor44b43212008-12-11 16:49:14 +0000128void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
129 assert(!isDefinition() && "Cannot redefine enums!");
130 setDefinition(true);
131
132 IntegerType = NewType;
133
134 // Let ASTContext know that this is the defining EnumDecl for this
135 // type.
136 C.setTagDefinition(this);
137}
138
Chris Lattner0ed844b2008-04-04 06:12:32 +0000139FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
140 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000141 StringLiteral *Str) {
142 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
143 return new (Mem) FileScopeAsmDecl(L, Str);
144}
145
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000146//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000147// ScopedDecl Implementation
148//===----------------------------------------------------------------------===//
149
150void ScopedDecl::setLexicalDeclContext(DeclContext *DC) {
151 if (DC == getLexicalDeclContext())
152 return;
153
154 if (isInSemaDC()) {
155 MultipleDC *MDC = new MultipleDC();
156 MDC->SemanticDC = getDeclContext();
157 MDC->LexicalDC = DC;
158 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
159 } else {
160 getMultipleDC()->LexicalDC = DC;
161 }
162}
163
164ScopedDecl::~ScopedDecl() {
165 if (isOutOfSemaDC())
166 delete getMultipleDC();
167}
168
169//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000170// FunctionDecl Implementation
171//===----------------------------------------------------------------------===//
172
Reid Spencer5f016e22007-07-11 17:01:13 +0000173FunctionDecl::~FunctionDecl() {
174 delete[] ParamInfo;
Douglas Gregorf0097952008-04-21 02:02:58 +0000175}
176
Ted Kremenek27f8a282008-05-20 00:43:19 +0000177void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-05-20 03:56:00 +0000178 if (Body)
179 Body->Destroy(C);
180
181 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
182 (*I)->Destroy(C);
183
Ted Kremenek27f8a282008-05-20 00:43:19 +0000184 Decl::Destroy(C);
185}
186
187
Douglas Gregorf0097952008-04-21 02:02:58 +0000188Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
189 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
190 if (FD->Body) {
191 Definition = FD;
192 return FD->Body;
193 }
194 }
195
196 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000197}
198
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000199// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
200static unsigned getNumTypeParams(QualType T) {
201 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000202 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000203 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000204 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000205}
206
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000207unsigned FunctionDecl::getNumParams() const {
208 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
209 if (!ParamInfo)
210 return 0;
211
212 return getNumTypeParams(getType());
213}
214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
216 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000217 assert(NumParams == getNumTypeParams(getType()) &&
218 "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000219
220 // Zero params -> null pointer.
221 if (NumParams) {
222 ParamInfo = new ParmVarDecl*[NumParams];
223 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
224 }
225}
226
Chris Lattner8123a952008-04-10 02:22:51 +0000227/// getMinRequiredArguments - Returns the minimum number of arguments
228/// needed to call this function. This may be fewer than the number of
229/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000230/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000231unsigned FunctionDecl::getMinRequiredArguments() const {
232 unsigned NumRequiredArgs = getNumParams();
233 while (NumRequiredArgs > 0
234 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
235 --NumRequiredArgs;
236
237 return NumRequiredArgs;
238}
239
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000240/// getOverloadedOperator - Which C++ overloaded operator this
241/// function represents, if any.
242OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000243 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
244 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000245 else
246 return OO_None;
247}
248
Chris Lattner8a934232008-03-31 00:36:02 +0000249//===----------------------------------------------------------------------===//
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000250// TagdDecl Implementation
251//===----------------------------------------------------------------------===//
252
253TagDecl* TagDecl::getDefinition(ASTContext& C) const {
254 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
255 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
256 return D->isDefinition() ? D : 0;
257}
258
259//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000260// RecordDecl Implementation
261//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000262
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000263RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenekdf042e62008-09-05 01:34:33 +0000264 IdentifierInfo *Id)
Douglas Gregor44b43212008-12-11 16:49:14 +0000265 : TagDecl(DK, TK, DC, L, Id, 0), DeclContext(DK) {
Ted Kremenek63597922008-09-02 21:12:32 +0000266
267 HasFlexibleArrayMember = false;
268 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000269}
270
271RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000272 SourceLocation L, IdentifierInfo *Id,
273 RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000274
Ted Kremenek63597922008-09-02 21:12:32 +0000275 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000276 RecordDecl* R = new (Mem) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000277 C.getTypeDeclType(R, PrevDecl);
278 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000279}
280
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000281RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000282}
283
284void RecordDecl::Destroy(ASTContext& C) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000285 DeclContext::DestroyDecls(C);
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000286 TagDecl::Destroy(C);
287}
288
Douglas Gregor44b43212008-12-11 16:49:14 +0000289/// completeDefinition - Notes that the definition of this type is now
290/// complete.
291void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor44b43212008-12-11 16:49:14 +0000293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 setDefinition(true);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000295
Douglas Gregor44b43212008-12-11 16:49:14 +0000296 // Let ASTContext know that this is the defining RecordDecl for this
297 // type.
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000298 C.setTagDefinition(this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000299}
300
Steve Naroff56ee6892008-10-08 17:01:13 +0000301//===----------------------------------------------------------------------===//
302// BlockDecl Implementation
303//===----------------------------------------------------------------------===//
304
305BlockDecl::~BlockDecl() {
306}
307
308void BlockDecl::Destroy(ASTContext& C) {
309 if (Body)
310 Body->Destroy(C);
311
312 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
313 (*I)->Destroy(C);
314
315 Decl::Destroy(C);
316}