blob: dbe476db1f278f5265ddc0d8e1f968a207034089 [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"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000016
Chris Lattner6d9a6852006-10-25 05:11:20 +000017using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000018
Chris Lattner88f70d62008-03-15 05:43:15 +000019//===----------------------------------------------------------------------===//
Chris Lattnera7b32872008-03-15 06:12:44 +000020// Decl Allocation/Deallocation Method Implementations
21//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000022
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000023TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
24 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
25 return new (Mem) TranslationUnitDecl();
26}
27
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000028NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
29 SourceLocation L, IdentifierInfo *Id) {
30 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
31 return new (Mem) NamespaceDecl(DC, L, Id);
32}
33
Ted Kremenek78aa98f2008-05-20 04:49:55 +000034void NamespaceDecl::Destroy(ASTContext& C) {
35 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
36 // together. They are all top-level Decls.
37
Ted Kremeneka08154d2008-05-24 15:09:56 +000038 this->~NamespaceDecl();
Ted Kremenek78aa98f2008-05-20 04:49:55 +000039 C.getAllocator().Deallocate((void *)this);
40}
41
42
Chris Lattnerbec41342008-04-22 18:39:57 +000043VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff08899ff2008-04-15 22:42:06 +000044 SourceLocation L,
45 IdentifierInfo *Id, QualType T,
46 StorageClass S, ScopedDecl *PrevDecl) {
47 void *Mem = C.getAllocator().Allocate<VarDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +000048 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner4b08ca82008-03-15 21:10:16 +000049}
50
Chris Lattnerbec41342008-04-22 18:39:57 +000051ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000052 SourceLocation L, IdentifierInfo *Id,
53 QualType T, StorageClass S,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000054 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner4b08ca82008-03-15 21:10:16 +000055 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +000056 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner4b08ca82008-03-15 21:10:16 +000057}
58
Chris Lattnerbec41342008-04-22 18:39:57 +000059FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000060 SourceLocation L,
Chris Lattner5072bae2008-03-15 21:24:04 +000061 IdentifierInfo *Id, QualType T,
62 StorageClass S, bool isInline,
63 ScopedDecl *PrevDecl) {
64 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +000065 return new (Mem) FunctionDecl(Function, DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattner5072bae2008-03-15 21:24:04 +000066}
67
Chris Lattner0a5ff0d2008-04-06 04:47:34 +000068FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +000069 IdentifierInfo *Id, QualType T, Expr *BW) {
70 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattner0a5ff0d2008-04-06 04:47:34 +000071 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattneree1284a2008-03-16 00:16:02 +000072}
73
Chris Lattner5072bae2008-03-15 21:24:04 +000074
Chris Lattnerc5ffed42008-04-04 06:12:32 +000075EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
76 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +000077 IdentifierInfo *Id, QualType T,
78 Expr *E, const llvm::APSInt &V,
79 ScopedDecl *PrevDecl){
Chris Lattnera7b32872008-03-15 06:12:44 +000080 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnerc5ffed42008-04-04 06:12:32 +000081 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +000082}
83
Ted Kremenek78aa98f2008-05-20 04:49:55 +000084void EnumConstantDecl::Destroy(ASTContext& C) {
85 if (Init) Init->Destroy(C);
86 Decl::Destroy(C);
87}
88
Chris Lattnerbec41342008-04-22 18:39:57 +000089TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000090 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +000091 IdentifierInfo *Id, QualType T,
92 ScopedDecl *PD) {
Chris Lattnera7b32872008-03-15 06:12:44 +000093 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +000094 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnera7b32872008-03-15 06:12:44 +000095}
96
Chris Lattnerbec41342008-04-22 18:39:57 +000097EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnerc5ffed42008-04-04 06:12:32 +000098 IdentifierInfo *Id,
Chris Lattner96c460d2008-03-15 21:32:50 +000099 ScopedDecl *PrevDecl) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000100 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +0000101 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000102}
103
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +0000104RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000105 SourceLocation L, IdentifierInfo *Id,
106 ScopedDecl *PrevDecl) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000107 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +0000108 Kind DK;
109 switch (TK) {
Ted Kremenekbba87242008-06-16 23:45:12 +0000110 default: assert(0 && "Invalid TagKind!");
111 case TK_enum: assert(0 && "Enum TagKind passed for Record!");
112 case TK_struct: DK = Struct; break;
113 case TK_union: DK = Union; break;
114 case TK_class: DK = Class; break;
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +0000115 }
Chris Lattnerbec41342008-04-22 18:39:57 +0000116 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000117}
118
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000119void EnumDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis406fb232008-06-10 01:32:09 +0000120 if (getEnumConstantList()) getEnumConstantList()->Destroy(C);
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000121 Decl::Destroy(C);
122}
123
124
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000125FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
126 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000127 StringLiteral *Str) {
128 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
129 return new (Mem) FileScopeAsmDecl(L, Str);
130}
131
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000132LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
133 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000134 LanguageIDs Lang, Decl *D) {
135 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
136 return new (Mem) LinkageSpecDecl(L, Lang, D);
137}
Chris Lattnera7b32872008-03-15 06:12:44 +0000138
139//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000140// NamedDecl Implementation
141//===----------------------------------------------------------------------===//
142
Chris Lattnera4016552007-10-06 22:53:46 +0000143const char *NamedDecl::getName() const {
Chris Lattnerec040b12007-01-21 23:09:50 +0000144 if (const IdentifierInfo *II = getIdentifier())
145 return II->getName();
146 return "";
Chris Lattner17ed4872006-11-20 04:58:19 +0000147}
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000148
Chris Lattner59a25942008-03-31 00:36:02 +0000149//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000150// FunctionDecl Implementation
151//===----------------------------------------------------------------------===//
152
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000153FunctionDecl::~FunctionDecl() {
154 delete[] ParamInfo;
Douglas Gregor89f238c2008-04-21 02:02:58 +0000155}
156
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000157void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000158 if (Body)
159 Body->Destroy(C);
160
161 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
162 (*I)->Destroy(C);
163
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000164 Decl::Destroy(C);
165}
166
167
Douglas Gregor89f238c2008-04-21 02:02:58 +0000168Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
169 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
170 if (FD->Body) {
171 Definition = FD;
172 return FD->Body;
173 }
174 }
175
176 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000177}
178
179unsigned FunctionDecl::getNumParams() const {
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000180 const FunctionType *FT = getType()->getAsFunctionType();
181 if (isa<FunctionTypeNoProto>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000182 return 0;
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000183 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000184}
185
Chris Lattner53621a52007-06-13 20:44:40 +0000186void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000187 assert(ParamInfo == 0 && "Already has param info!");
188 assert(NumParams == getNumParams() && "Parameter count mismatch!");
189
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000190 // Zero params -> null pointer.
191 if (NumParams) {
Chris Lattner53621a52007-06-13 20:44:40 +0000192 ParamInfo = new ParmVarDecl*[NumParams];
193 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000194 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000195}
Chris Lattner41943152007-01-25 04:52:46 +0000196
Chris Lattner58258242008-04-10 02:22:51 +0000197/// getMinRequiredArguments - Returns the minimum number of arguments
198/// needed to call this function. This may be fewer than the number of
199/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000200/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000201unsigned FunctionDecl::getMinRequiredArguments() const {
202 unsigned NumRequiredArgs = getNumParams();
203 while (NumRequiredArgs > 0
204 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
205 --NumRequiredArgs;
206
207 return NumRequiredArgs;
208}
209
Chris Lattner59a25942008-03-31 00:36:02 +0000210//===----------------------------------------------------------------------===//
211// RecordDecl Implementation
212//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000213
214/// defineBody - When created, RecordDecl's correspond to a forward declared
215/// record. This method is used to mark the decl as being defined, with the
216/// specified contents.
Steve Naroffcc321422007-03-26 23:09:51 +0000217void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
Chris Lattner41943152007-01-25 04:52:46 +0000218 assert(!isDefinition() && "Cannot redefine record!");
219 setDefinition(true);
Chris Lattner5f521502007-01-25 06:27:24 +0000220 NumMembers = numMembers;
221 if (numMembers) {
Steve Naroffcc321422007-03-26 23:09:51 +0000222 Members = new FieldDecl*[numMembers];
Chris Lattner5f521502007-01-25 06:27:24 +0000223 memcpy(Members, members, numMembers*sizeof(Decl*));
Chris Lattner41943152007-01-25 04:52:46 +0000224 }
225}
Steve Naroffcc321422007-03-26 23:09:51 +0000226
Chris Lattner59a25942008-03-31 00:36:02 +0000227FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Steve Naroffcc321422007-03-26 23:09:51 +0000228 if (Members == 0 || NumMembers < 0)
229 return 0;
Fariborz Jahanian67341402007-10-04 00:45:27 +0000230
Chris Lattner59a25942008-03-31 00:36:02 +0000231 // Linear search. When C++ classes come along, will likely need to revisit.
232 for (int i = 0; i != NumMembers; ++i)
233 if (Members[i]->getIdentifier() == II)
Steve Naroffcc321422007-03-26 23:09:51 +0000234 return Members[i];
Steve Naroffcc321422007-03-26 23:09:51 +0000235 return 0;
Chris Lattnerbd4de5df2007-07-12 15:43:07 +0000236}