blob: c8b7fc2dcf982f696a13174f3a784a6f66957dc8 [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) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000121 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000122 EnumDecl *Enum = new (Mem) 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
150 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
151 // For function declarations, we keep track of redeclarations.
152 return FD->getPreviousDeclaration() == OldD;
153
154 // For non-function declarations, if the declarations are of the
155 // same kind then this must be a redeclaration, or semantic analysis
156 // would not have given us the new declaration.
157 return this->getKind() == OldD->getKind();
158}
159
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000160
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000161//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000162// VarDecl Implementation
163//===----------------------------------------------------------------------===//
164
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000165VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
166 IdentifierInfo *Id, QualType T, StorageClass S,
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000167 SourceLocation TypeSpecStartLoc) {
Steve Naroff3e970492009-01-27 21:25:57 +0000168 return new (C) VarDecl(Var, DC, L, Id, T, S, TypeSpecStartLoc);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000169}
170
171void VarDecl::Destroy(ASTContext& C) {
172 this->~VarDecl();
Steve Naroff3e970492009-01-27 21:25:57 +0000173 C.Deallocate((void *)this);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000174}
175
176VarDecl::~VarDecl() {
177 delete getInit();
178}
179
180//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000181// FunctionDecl Implementation
182//===----------------------------------------------------------------------===//
183
Ted Kremenek27f8a282008-05-20 00:43:19 +0000184void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekb65cf412008-05-20 03:56:00 +0000185 if (Body)
186 Body->Destroy(C);
187
188 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
189 (*I)->Destroy(C);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000190
Steve Naroff3e970492009-01-27 21:25:57 +0000191 C.Deallocate(ParamInfo);
Nuno Lopes460b0ac2009-01-18 19:57:27 +0000192
Ted Kremenek27f8a282008-05-20 00:43:19 +0000193 Decl::Destroy(C);
194}
195
196
Douglas Gregorf0097952008-04-21 02:02:58 +0000197Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
198 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
199 if (FD->Body) {
200 Definition = FD;
201 return FD->Body;
202 }
203 }
204
205 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000208// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
209static unsigned getNumTypeParams(QualType T) {
210 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000211 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000212 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000213 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000214}
215
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000216unsigned FunctionDecl::getNumParams() const {
217 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
218 if (!ParamInfo)
219 return 0;
220
221 return getNumTypeParams(getType());
222}
223
Ted Kremenekfc767612009-01-14 00:42:25 +0000224void FunctionDecl::setParams(ASTContext& C, ParmVarDecl **NewParamInfo,
225 unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremenek4f03fd62008-10-29 18:41:34 +0000227 assert(NumParams == getNumTypeParams(getType()) &&
228 "Parameter count mismatch!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000229
230 // Zero params -> null pointer.
231 if (NumParams) {
Ted Kremenekfc767612009-01-14 00:42:25 +0000232 void *Mem = C.getAllocator().Allocate<ParmVarDecl*>(NumParams);
233 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
235 }
236}
237
Chris Lattner8123a952008-04-10 02:22:51 +0000238/// getMinRequiredArguments - Returns the minimum number of arguments
239/// needed to call this function. This may be fewer than the number of
240/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000241/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000242unsigned FunctionDecl::getMinRequiredArguments() const {
243 unsigned NumRequiredArgs = getNumParams();
244 while (NumRequiredArgs > 0
245 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
246 --NumRequiredArgs;
247
248 return NumRequiredArgs;
249}
250
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000251/// getOverloadedOperator - Which C++ overloaded operator this
252/// function represents, if any.
253OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +0000254 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
255 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000256 else
257 return OO_None;
258}
259
Chris Lattner8a934232008-03-31 00:36:02 +0000260//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000261// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000262//===----------------------------------------------------------------------===//
263
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000264void TagDecl::startDefinition() {
265 cast<TagType>(TypeForDecl)->decl.setPointer(this);
266 cast<TagType>(TypeForDecl)->decl.setInt(1);
267}
268
269void TagDecl::completeDefinition() {
270 assert((!TypeForDecl ||
271 cast<TagType>(TypeForDecl)->decl.getPointer() == this) &&
272 "Attempt to redefine a tag definition?");
273 IsDefinition = true;
274 cast<TagType>(TypeForDecl)->decl.setPointer(this);
275 cast<TagType>(TypeForDecl)->decl.setInt(0);
276}
277
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000278TagDecl* TagDecl::getDefinition(ASTContext& C) const {
279 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
280 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
281 return D->isDefinition() ? D : 0;
282}
283
284//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000285// RecordDecl Implementation
286//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000287
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000288RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenekdf042e62008-09-05 01:34:33 +0000289 IdentifierInfo *Id)
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000290 : TagDecl(DK, TK, DC, L, Id) {
Ted Kremenek63597922008-09-02 21:12:32 +0000291 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000292 AnonymousStructOrUnion = false;
Ted Kremenek63597922008-09-02 21:12:32 +0000293 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +0000294}
295
296RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000297 SourceLocation L, IdentifierInfo *Id,
298 RecordDecl* PrevDecl) {
Ted Kremenekdf042e62008-09-05 01:34:33 +0000299
Steve Naroff3e970492009-01-27 21:25:57 +0000300 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000301 C.getTypeDeclType(R, PrevDecl);
302 return R;
Ted Kremenek63597922008-09-02 21:12:32 +0000303}
304
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000305RecordDecl::~RecordDecl() {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000306}
307
308void RecordDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis997b6c62008-08-08 14:08:55 +0000309 TagDecl::Destroy(C);
310}
311
Douglas Gregor44b43212008-12-11 16:49:14 +0000312/// completeDefinition - Notes that the definition of this type is now
313/// complete.
314void RecordDecl::completeDefinition(ASTContext& C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000316 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +0000317}
318
Steve Naroff56ee6892008-10-08 17:01:13 +0000319//===----------------------------------------------------------------------===//
320// BlockDecl Implementation
321//===----------------------------------------------------------------------===//
322
323BlockDecl::~BlockDecl() {
324}
325
326void BlockDecl::Destroy(ASTContext& C) {
327 if (Body)
328 Body->Destroy(C);
329
330 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
331 (*I)->Destroy(C);
332
333 Decl::Destroy(C);
334}