blob: bfe8a184e8d51c190964f29be234912973e2755b [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) {
27 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
28 return new (Mem) TranslationUnitDecl();
29}
30
Argiris Kirtzidis03e6aaf2008-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 Kremenek5be49242008-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 Kremenek0f433842008-05-24 15:09:56 +000041 this->~NamespaceDecl();
Ted Kremenek5be49242008-05-20 04:49:55 +000042 C.getAllocator().Deallocate((void *)this);
43}
44
45
Chris Lattner8c7c6a12008-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 Lattneref87a202008-04-22 18:39:57 +000052ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000053 SourceLocation L, IdentifierInfo *Id,
54 QualType T, StorageClass S,
Chris Lattner3e254fb2008-04-08 04:40:51 +000055 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner48d225c2008-03-15 21:10:16 +000056 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Fariborz Jahaniane26cb432008-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 Lattner48d225c2008-03-15 21:10:16 +000065}
66
Fariborz Jahanian160e8812008-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 Lattneref87a202008-04-22 18:39:57 +000077FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000078 SourceLocation L,
Douglas Gregor6704b312008-11-17 22:58:34 +000079 DeclarationName N, QualType T,
Chris Lattner4c7802b2008-03-15 21:24:04 +000080 StorageClass S, bool isInline,
Steve Naroff71cd7762008-10-03 00:02:03 +000081 ScopedDecl *PrevDecl,
82 SourceLocation TypeSpecStartLoc) {
Chris Lattner4c7802b2008-03-15 21:24:04 +000083 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Douglas Gregor6704b312008-11-17 22:58:34 +000084 return new (Mem) FunctionDecl(Function, DC, L, N, T, S, isInline, PrevDecl,
Steve Naroff71cd7762008-10-03 00:02:03 +000085 TypeSpecStartLoc);
Chris Lattner4c7802b2008-03-15 21:24:04 +000086}
87
Steve Naroff52059382008-10-10 01:28:17 +000088BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff9ac456d2008-10-08 17:01:13 +000089 void *Mem = C.getAllocator().Allocate<BlockDecl>();
Steve Naroff52059382008-10-10 01:28:17 +000090 return new (Mem) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +000091}
92
Douglas Gregor8acb7272008-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 Lattner81db64a2008-03-16 00:16:02 +000096 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Douglas Gregor8acb7272008-12-11 16:49:14 +000097 return new (Mem) FieldDecl(Decl::Field, DC, L, Id, T, BW, Mutable, PrevDecl);
Chris Lattner81db64a2008-03-16 00:16:02 +000098}
99
Chris Lattner4c7802b2008-03-15 21:24:04 +0000100
Chris Lattnereee57c02008-04-04 06:12:32 +0000101EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
102 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000103 IdentifierInfo *Id, QualType T,
104 Expr *E, const llvm::APSInt &V,
105 ScopedDecl *PrevDecl){
Chris Lattnere4650482008-03-15 06:12:44 +0000106 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +0000107 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000108}
109
Ted Kremenek5be49242008-05-20 04:49:55 +0000110void EnumConstantDecl::Destroy(ASTContext& C) {
111 if (Init) Init->Destroy(C);
112 Decl::Destroy(C);
113}
114
Chris Lattneref87a202008-04-22 18:39:57 +0000115TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000116 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000117 IdentifierInfo *Id, QualType T,
118 ScopedDecl *PD) {
Chris Lattnere4650482008-03-15 06:12:44 +0000119 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000120 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnere4650482008-03-15 06:12:44 +0000121}
122
Chris Lattneref87a202008-04-22 18:39:57 +0000123EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000124 IdentifierInfo *Id,
Douglas Gregorae644892008-12-15 16:32:14 +0000125 EnumDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000126 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Douglas Gregorae644892008-12-15 16:32:14 +0000127 EnumDecl *Enum = new (Mem) EnumDecl(DC, L, Id, 0);
128 C.getTypeDeclType(Enum, PrevDecl);
129 return Enum;
Chris Lattnere4650482008-03-15 06:12:44 +0000130}
131
Ted Kremenek25d8be12008-09-02 20:13:32 +0000132void EnumDecl::Destroy(ASTContext& C) {
Ted Kremenek25d8be12008-09-02 20:13:32 +0000133 Decl::Destroy(C);
134}
135
Douglas Gregor8acb7272008-12-11 16:49:14 +0000136void EnumDecl::completeDefinition(ASTContext &C, QualType NewType) {
137 assert(!isDefinition() && "Cannot redefine enums!");
138 setDefinition(true);
139
140 IntegerType = NewType;
141
142 // Let ASTContext know that this is the defining EnumDecl for this
143 // type.
144 C.setTagDefinition(this);
145}
146
Chris Lattnereee57c02008-04-04 06:12:32 +0000147FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
148 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000149 StringLiteral *Str) {
150 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
151 return new (Mem) FileScopeAsmDecl(L, Str);
152}
153
Chris Lattnere4650482008-03-15 06:12:44 +0000154//===----------------------------------------------------------------------===//
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000155// ScopedDecl Implementation
156//===----------------------------------------------------------------------===//
157
158void ScopedDecl::setLexicalDeclContext(DeclContext *DC) {
159 if (DC == getLexicalDeclContext())
160 return;
161
162 if (isInSemaDC()) {
163 MultipleDC *MDC = new MultipleDC();
164 MDC->SemanticDC = getDeclContext();
165 MDC->LexicalDC = DC;
166 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
167 } else {
168 getMultipleDC()->LexicalDC = DC;
169 }
170}
171
172ScopedDecl::~ScopedDecl() {
173 if (isOutOfSemaDC())
174 delete getMultipleDC();
175}
176
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000177bool ScopedDecl::declarationReplaces(NamedDecl *OldD) const {
178 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
179
180 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
181 // For function declarations, we keep track of redeclarations.
182 return FD->getPreviousDeclaration() == OldD;
183
184 // For non-function declarations, if the declarations are of the
185 // same kind then this must be a redeclaration, or semantic analysis
186 // would not have given us the new declaration.
187 return this->getKind() == OldD->getKind();
188}
189
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000190//===----------------------------------------------------------------------===//
Nuno Lopesc98406e2008-12-17 23:39:55 +0000191// VarDecl Implementation
192//===----------------------------------------------------------------------===//
193
194VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
195 SourceLocation L,
196 IdentifierInfo *Id, QualType T,
197 StorageClass S, ScopedDecl *PrevDecl,
198 SourceLocation TypeSpecStartLoc) {
199 void *Mem = C.getAllocator().Allocate<VarDecl>();
200 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl, TypeSpecStartLoc);
201}
202
203void VarDecl::Destroy(ASTContext& C) {
204 this->~VarDecl();
205 C.getAllocator().Deallocate((void *)this);
206}
207
208VarDecl::~VarDecl() {
209 delete getInit();
210}
211
212//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000213// FunctionDecl Implementation
214//===----------------------------------------------------------------------===//
215
Chris Lattner4b009652007-07-25 00:24:17 +0000216FunctionDecl::~FunctionDecl() {
217 delete[] ParamInfo;
Douglas Gregor42214c52008-04-21 02:02:58 +0000218}
219
Ted Kremenekafdf8112008-05-20 00:43:19 +0000220void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000221 if (Body)
222 Body->Destroy(C);
223
224 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
225 (*I)->Destroy(C);
226
Ted Kremenekafdf8112008-05-20 00:43:19 +0000227 Decl::Destroy(C);
228}
229
230
Douglas Gregor42214c52008-04-21 02:02:58 +0000231Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
232 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
233 if (FD->Body) {
234 Definition = FD;
235 return FD->Body;
236 }
237 }
238
239 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000240}
241
Ted Kremeneka6ded352008-10-29 18:41:34 +0000242// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
243static unsigned getNumTypeParams(QualType T) {
244 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnere8733592008-04-06 23:09:52 +0000245 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000246 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000247 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000248}
249
Ted Kremeneka6ded352008-10-29 18:41:34 +0000250unsigned FunctionDecl::getNumParams() const {
251 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
252 if (!ParamInfo)
253 return 0;
254
255 return getNumTypeParams(getType());
256}
257
Chris Lattner4b009652007-07-25 00:24:17 +0000258void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
259 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremeneka6ded352008-10-29 18:41:34 +0000260 assert(NumParams == getNumTypeParams(getType()) &&
261 "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000262
263 // Zero params -> null pointer.
264 if (NumParams) {
265 ParamInfo = new ParmVarDecl*[NumParams];
266 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
267 }
268}
269
Chris Lattner97316c02008-04-10 02:22:51 +0000270/// getMinRequiredArguments - Returns the minimum number of arguments
271/// needed to call this function. This may be fewer than the number of
272/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000273/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000274unsigned FunctionDecl::getMinRequiredArguments() const {
275 unsigned NumRequiredArgs = getNumParams();
276 while (NumRequiredArgs > 0
277 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
278 --NumRequiredArgs;
279
280 return NumRequiredArgs;
281}
282
Douglas Gregore60e5d32008-11-06 22:13:31 +0000283/// getOverloadedOperator - Which C++ overloaded operator this
284/// function represents, if any.
285OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor96a32dd2008-11-18 14:39:36 +0000286 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
287 return getDeclName().getCXXOverloadedOperator();
Douglas Gregore60e5d32008-11-06 22:13:31 +0000288 else
289 return OO_None;
290}
291
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000292//===----------------------------------------------------------------------===//
Douglas Gregor723d3332009-01-07 00:43:41 +0000293// TagDecl Implementation
Ted Kremenek46a837c2008-09-05 17:16:31 +0000294//===----------------------------------------------------------------------===//
295
296TagDecl* TagDecl::getDefinition(ASTContext& C) const {
297 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
298 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
299 return D->isDefinition() ? D : 0;
300}
301
302//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000303// RecordDecl Implementation
304//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000305
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000306RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000307 IdentifierInfo *Id)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000308 : TagDecl(DK, TK, DC, L, Id, 0), DeclContext(DK) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000309
310 HasFlexibleArrayMember = false;
Douglas Gregor723d3332009-01-07 00:43:41 +0000311 AnonymousStructOrUnion = false;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000312 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000313}
314
315RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000316 SourceLocation L, IdentifierInfo *Id,
317 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000318
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000319 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000320 RecordDecl* R = new (Mem) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000321 C.getTypeDeclType(R, PrevDecl);
322 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000323}
324
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000325RecordDecl::~RecordDecl() {
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000326}
327
328void RecordDecl::Destroy(ASTContext& C) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000329 DeclContext::DestroyDecls(C);
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000330 TagDecl::Destroy(C);
331}
332
Douglas Gregor8acb7272008-12-11 16:49:14 +0000333/// completeDefinition - Notes that the definition of this type is now
334/// complete.
335void RecordDecl::completeDefinition(ASTContext& C) {
Chris Lattner4b009652007-07-25 00:24:17 +0000336 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor8acb7272008-12-11 16:49:14 +0000337
Chris Lattner4b009652007-07-25 00:24:17 +0000338 setDefinition(true);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000339
Douglas Gregor8acb7272008-12-11 16:49:14 +0000340 // Let ASTContext know that this is the defining RecordDecl for this
341 // type.
Ted Kremenek46a837c2008-09-05 17:16:31 +0000342 C.setTagDefinition(this);
Chris Lattner4b009652007-07-25 00:24:17 +0000343}
344
Steve Naroff9ac456d2008-10-08 17:01:13 +0000345//===----------------------------------------------------------------------===//
346// BlockDecl Implementation
347//===----------------------------------------------------------------------===//
348
349BlockDecl::~BlockDecl() {
350}
351
352void BlockDecl::Destroy(ASTContext& C) {
353 if (Body)
354 Body->Destroy(C);
355
356 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
357 (*I)->Destroy(C);
358
359 Decl::Destroy(C);
360}