blob: 0bc0043ccb8f0ea7103a2458b71899ceb137e011 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000016#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000017#include "clang/AST/Expr.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000018#include "clang/Basic/IdentifierTable.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000019
Chris Lattner6d9a6852006-10-25 05:11:20 +000020using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000021
Chris Lattner88f70d62008-03-15 05:43:15 +000022//===----------------------------------------------------------------------===//
Chris Lattnera7b32872008-03-15 06:12:44 +000023// Decl Allocation/Deallocation Method Implementations
24//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000025
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000026TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
27 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
28 return new (Mem) TranslationUnitDecl();
29}
30
Argyrios Kyrtzidis08114892008-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 Kremenek78aa98f2008-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 Kremeneka08154d2008-05-24 15:09:56 +000041 this->~NamespaceDecl();
Ted Kremenek78aa98f2008-05-20 04:49:55 +000042 C.getAllocator().Deallocate((void *)this);
43}
44
45
Chris Lattner5696e7b2008-06-17 18:05:57 +000046ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
47 SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) {
48 void *Mem = C.getAllocator().Allocate<ImplicitParamDecl>();
49 return new (Mem) ImplicitParamDecl(ImplicitParam, DC, L, Id, T, PrevDecl);
50}
51
Chris Lattnerbec41342008-04-22 18:39:57 +000052ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000053 SourceLocation L, IdentifierInfo *Id,
54 QualType T, StorageClass S,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000055 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner4b08ca82008-03-15 21:10:16 +000056 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +000057 return new (Mem) ParmVarDecl(ParmVar, DC, L, Id, T, S, DefArg, PrevDecl);
58}
59
60QualType ParmVarDecl::getOriginalType() const {
61 if (const ParmVarWithOriginalTypeDecl *PVD =
62 dyn_cast<ParmVarWithOriginalTypeDecl>(this))
63 return PVD->OriginalType;
64 return getType();
Chris Lattner4b08ca82008-03-15 21:10:16 +000065}
66
Fariborz Jahanian7664ffb2008-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,
71 Expr *DefArg, ScopedDecl *PrevDecl) {
72 void *Mem = C.getAllocator().Allocate<ParmVarWithOriginalTypeDecl>();
73 return new (Mem) ParmVarWithOriginalTypeDecl(DC, L, Id, T, OT, S,
74 DefArg, PrevDecl);
75}
76
Chris Lattnerbec41342008-04-22 18:39:57 +000077FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000078 SourceLocation L,
Douglas Gregor92751d42008-11-17 22:58:34 +000079 DeclarationName N, QualType T,
Chris Lattner5072bae2008-03-15 21:24:04 +000080 StorageClass S, bool isInline,
Steve Naroff22315692008-10-03 00:02:03 +000081 ScopedDecl *PrevDecl,
82 SourceLocation TypeSpecStartLoc) {
Chris Lattner5072bae2008-03-15 21:24:04 +000083 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Douglas Gregor92751d42008-11-17 22:58:34 +000084 return new (Mem) FunctionDecl(Function, DC, L, N, T, S, isInline, PrevDecl,
Steve Naroff22315692008-10-03 00:02:03 +000085 TypeSpecStartLoc);
Chris Lattner5072bae2008-03-15 21:24:04 +000086}
87
Steve Naroff1d95e5a2008-10-10 01:28:17 +000088BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff415d3d52008-10-08 17:01:13 +000089 void *Mem = C.getAllocator().Allocate<BlockDecl>();
Steve Naroff1d95e5a2008-10-10 01:28:17 +000090 return new (Mem) BlockDecl(DC, L);
Steve Naroff415d3d52008-10-08 17:01:13 +000091}
92
Douglas Gregor91f84212008-12-11 16:49:14 +000093FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
94 IdentifierInfo *Id, QualType T, Expr *BW,
95 bool Mutable, ScopedDecl *PrevDecl) {
Chris Lattneree1284a2008-03-16 00:16:02 +000096 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Douglas Gregor91f84212008-12-11 16:49:14 +000097 return new (Mem) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable, PrevDecl);
Chris Lattneree1284a2008-03-16 00:16:02 +000098}
99
Douglas Gregorf4d33272009-01-07 19:46:03 +0000100bool FieldDecl::isAnonymousStructOrUnion() const {
101 if (!isImplicit() || getDeclName())
102 return false;
103
104 if (const RecordType *Record = getType()->getAsRecordType())
105 return Record->getDecl()->isAnonymousStructOrUnion();
106
107 return false;
108}
Chris Lattner5072bae2008-03-15 21:24:04 +0000109
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000110EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
111 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000112 IdentifierInfo *Id, QualType T,
113 Expr *E, const llvm::APSInt &V,
114 ScopedDecl *PrevDecl){
Chris Lattnera7b32872008-03-15 06:12:44 +0000115 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000116 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000117}
118
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000119void EnumConstantDecl::Destroy(ASTContext& C) {
120 if (Init) Init->Destroy(C);
121 Decl::Destroy(C);
122}
123
Chris Lattnerbec41342008-04-22 18:39:57 +0000124TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000125 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000126 IdentifierInfo *Id, QualType T,
127 ScopedDecl *PD) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000128 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +0000129 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnera7b32872008-03-15 06:12:44 +0000130}
131
Chris Lattnerbec41342008-04-22 18:39:57 +0000132EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000133 IdentifierInfo *Id,
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000134 EnumDecl *PrevDecl) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000135 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Douglas Gregorc811d8f2008-12-15 16:32:14 +0000136 EnumDecl *Enum = new (Mem) EnumDecl(DC, L, Id, 0);
137 C.getTypeDeclType(Enum, PrevDecl);
138 return Enum;
Chris Lattnera7b32872008-03-15 06:12:44 +0000139}
140
Ted Kremenek123f0252008-09-02 20:13:32 +0000141void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek123f0252008-09-02 20:13:32 +0000142 Decl::Destroy(C);
143}
144
Douglas Gregor91f84212008-12-11 16:49:14 +0000145void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
146 assert(!isDefinition() && "Cannot redefine enums!");
147 setDefinition(true);
148
149 IntegerType = NewType;
150
151 // Let ASTContext know that this is the defining EnumDecl for this
152 // type.
153 C.setTagDefinition(this);
154}
155
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000156FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
157 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000158 StringLiteral *Str) {
159 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
160 return new (Mem) FileScopeAsmDecl(L, Str);
161}
162
Chris Lattnera7b32872008-03-15 06:12:44 +0000163//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000164// ScopedDecl Implementation
165//===----------------------------------------------------------------------===//
166
Douglas Gregorc72e6452009-01-09 18:51:29 +0000167void ScopedDecl::setDeclContext(DeclContext *DC) {
168 if (isOutOfSemaDC())
169 delete getMultipleDC();
170
171 DeclCtx = reinterpret_cast<uintptr_t>(DC);
172}
173
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000174void ScopedDecl::setLexicalDeclContext(DeclContext *DC) {
175 if (DC == getLexicalDeclContext())
176 return;
177
178 if (isInSemaDC()) {
179 MultipleDC *MDC = new MultipleDC();
180 MDC->SemanticDC = getDeclContext();
181 MDC->LexicalDC = DC;
182 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
183 } else {
184 getMultipleDC()->LexicalDC = DC;
185 }
186}
187
188ScopedDecl::~ScopedDecl() {
189 if (isOutOfSemaDC())
190 delete getMultipleDC();
191}
192
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000193bool ScopedDecl::declarationReplaces(NamedDecl *OldD) const {
194 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
195
196 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
197 // For function declarations, we keep track of redeclarations.
198 return FD->getPreviousDeclaration() == OldD;
199
200 // For non-function declarations, if the declarations are of the
201 // same kind then this must be a redeclaration, or semantic analysis
202 // would not have given us the new declaration.
203 return this->getKind() == OldD->getKind();
204}
205
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000206//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000207// VarDecl Implementation
208//===----------------------------------------------------------------------===//
209
210VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
211 SourceLocation L,
212 IdentifierInfo *Id, QualType T,
213 StorageClass S, ScopedDecl *PrevDecl,
214 SourceLocation TypeSpecStartLoc) {
215 void *Mem = C.getAllocator().Allocate<VarDecl>();
216 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl, TypeSpecStartLoc);
217}
218
219void VarDecl::Destroy(ASTContext& C) {
220 this->~VarDecl();
221 C.getAllocator().Deallocate((void *)this);
222}
223
224VarDecl::~VarDecl() {
225 delete getInit();
226}
227
228//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000229// FunctionDecl Implementation
230//===----------------------------------------------------------------------===//
231
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000232FunctionDecl::~FunctionDecl() {
233 delete[] ParamInfo;
Douglas Gregor89f238c2008-04-21 02:02:58 +0000234}
235
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000236void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000237 if (Body)
238 Body->Destroy(C);
239
240 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
241 (*I)->Destroy(C);
242
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000243 Decl::Destroy(C);
244}
245
246
Douglas Gregor89f238c2008-04-21 02:02:58 +0000247Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
248 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
249 if (FD->Body) {
250 Definition = FD;
251 return FD->Body;
252 }
253 }
254
255 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000256}
257
Ted Kremenek82c1cc32008-10-29 18:41:34 +0000258// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
259static unsigned getNumTypeParams(QualType T) {
260 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000261 if (isa<FunctionTypeNoProto>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000262 return 0;
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000263 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000264}
265
Ted Kremenek82c1cc32008-10-29 18:41:34 +0000266unsigned FunctionDecl::getNumParams() const {
267 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
268 if (!ParamInfo)
269 return 0;
270
271 return getNumTypeParams(getType());
272}
273
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000274void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
275 unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000276 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek82c1cc32008-10-29 18:41:34 +0000277 assert(NumParams == getNumTypeParams(getType()) &&
278 "Parameter count mismatch!");
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000279
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000280 // Zero params -> null pointer.
281 if (NumParams) {
Ted Kremenek4ba36fc2009-01-14 00:42:25 +0000282 void *Mem = C.getAllocator().Allocate<ParmVarDecl*>(NumParams);
283 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +0000284 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000285 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000286}
Chris Lattner41943152007-01-25 04:52:46 +0000287
Chris Lattner58258242008-04-10 02:22:51 +0000288/// getMinRequiredArguments - Returns the minimum number of arguments
289/// needed to call this function. This may be fewer than the number of
290/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000291/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000292unsigned FunctionDecl::getMinRequiredArguments() const {
293 unsigned NumRequiredArgs = getNumParams();
294 while (NumRequiredArgs > 0
295 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
296 --NumRequiredArgs;
297
298 return NumRequiredArgs;
299}
300
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000301/// getOverloadedOperator - Which C++ overloaded operator this
302/// function represents, if any.
303OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +0000304 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
305 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000306 else
307 return OO_None;
308}
309
Chris Lattner59a25942008-03-31 00:36:02 +0000310//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000311// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +0000312//===----------------------------------------------------------------------===//
313
314TagDecl* TagDecl::getDefinition(ASTContext& C) const {
315 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
316 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
317 return D->isDefinition() ? D : 0;
318}
319
320//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000321// RecordDecl Implementation
322//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000323
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000324RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek47923c72008-09-05 01:34:33 +0000325 IdentifierInfo *Id)
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000326 : TagDecl(DK, TK, DC, L, Id, 0) {
Ted Kremenek52baf502008-09-02 21:12:32 +0000327 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +0000328 AnonymousStructOrUnion = false;
Ted Kremenek52baf502008-09-02 21:12:32 +0000329 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +0000330}
331
332RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +0000333 SourceLocation L, IdentifierInfo *Id,
334 RecordDecl* PrevDecl) {
Ted Kremenek47923c72008-09-05 01:34:33 +0000335
Ted Kremenek52baf502008-09-02 21:12:32 +0000336 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +0000337 RecordDecl* R = new (Mem) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek21475702008-09-05 17:16:31 +0000338 C.getTypeDeclType(R, PrevDecl);
339 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +0000340}
341
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000342RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000343}
344
345void RecordDecl::Destroy(ASTContext& C) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000346 DeclContext::DestroyDecls(C);
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +0000347 TagDecl::Destroy(C);
348}
349
Douglas Gregor91f84212008-12-11 16:49:14 +0000350/// completeDefinition - Notes that the definition of this type is now
351/// complete.
352void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner41943152007-01-25 04:52:46 +0000353 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor91f84212008-12-11 16:49:14 +0000354
Chris Lattner41943152007-01-25 04:52:46 +0000355 setDefinition(true);
Ted Kremenek21475702008-09-05 17:16:31 +0000356
Douglas Gregor91f84212008-12-11 16:49:14 +0000357 // Let ASTContext know that this is the defining RecordDecl for this
358 // type.
Ted Kremenek21475702008-09-05 17:16:31 +0000359 C.setTagDefinition(this);
Chris Lattner41943152007-01-25 04:52:46 +0000360}
Steve Naroffcc321422007-03-26 23:09:51 +0000361
Steve Naroff415d3d52008-10-08 17:01:13 +0000362//===----------------------------------------------------------------------===//
363// BlockDecl Implementation
364//===----------------------------------------------------------------------===//
365
366BlockDecl::~BlockDecl() {
367}
368
369void BlockDecl::Destroy(ASTContext& C) {
370 if (Body)
371 Body->Destroy(C);
372
373 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
374 (*I)->Destroy(C);
375
376 Decl::Destroy(C);
377}