blob: 8d68a58f8c13253d23b67f361c6bc7abee43f688 [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"
Ted Kremenekafdf8112008-05-20 00:43:19 +000016
Chris Lattner4b009652007-07-25 00:24:17 +000017using namespace clang;
18
Chris Lattnera8344c32008-03-15 05:43:15 +000019//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +000020// Decl Allocation/Deallocation Method Implementations
21//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000022
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000023TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
24 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
25 return new (Mem) TranslationUnitDecl();
26}
27
Argiris Kirtzidis03e6aaf2008-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 Kremenek5be49242008-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 Kremenek0f433842008-05-24 15:09:56 +000038 this->~NamespaceDecl();
Ted Kremenek5be49242008-05-20 04:49:55 +000039 C.getAllocator().Deallocate((void *)this);
40}
41
42
Chris Lattneref87a202008-04-22 18:39:57 +000043VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff72a6ebc2008-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 Lattneref87a202008-04-22 18:39:57 +000048 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +000049}
50
Chris Lattneref87a202008-04-22 18:39:57 +000051ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000052 SourceLocation L, IdentifierInfo *Id,
53 QualType T, StorageClass S,
Chris Lattner3e254fb2008-04-08 04:40:51 +000054 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner48d225c2008-03-15 21:10:16 +000055 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +000056 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +000057}
58
Chris Lattneref87a202008-04-22 18:39:57 +000059FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000060 SourceLocation L,
Chris Lattner4c7802b2008-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>();
Chris Lattneref87a202008-04-22 18:39:57 +000065 return new (Mem) FunctionDecl(DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattner4c7802b2008-03-15 21:24:04 +000066}
67
Chris Lattnerf3874bc2008-04-06 04:47:34 +000068FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +000069 IdentifierInfo *Id, QualType T, Expr *BW) {
70 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +000071 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner81db64a2008-03-16 00:16:02 +000072}
73
Chris Lattner4c7802b2008-03-15 21:24:04 +000074
Chris Lattnereee57c02008-04-04 06:12:32 +000075EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
76 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +000077 IdentifierInfo *Id, QualType T,
78 Expr *E, const llvm::APSInt &V,
79 ScopedDecl *PrevDecl){
Chris Lattnere4650482008-03-15 06:12:44 +000080 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000081 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +000082}
83
Ted Kremenek5be49242008-05-20 04:49:55 +000084void EnumConstantDecl::Destroy(ASTContext& C) {
85 if (Init) Init->Destroy(C);
86 Decl::Destroy(C);
87}
88
Chris Lattneref87a202008-04-22 18:39:57 +000089TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000090 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +000091 IdentifierInfo *Id, QualType T,
92 ScopedDecl *PD) {
Chris Lattnere4650482008-03-15 06:12:44 +000093 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +000094 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnere4650482008-03-15 06:12:44 +000095}
96
Chris Lattneref87a202008-04-22 18:39:57 +000097EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +000098 IdentifierInfo *Id,
Chris Lattner58114f02008-03-15 21:32:50 +000099 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000100 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000101 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000102}
103
Chris Lattneref87a202008-04-22 18:39:57 +0000104RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000105 SourceLocation L, IdentifierInfo *Id,
106 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000107 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000108 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000109}
110
Ted Kremenek5be49242008-05-20 04:49:55 +0000111void EnumDecl::Destroy(ASTContext& C) {
112 if (ElementList) ElementList->Destroy(C);
113 Decl::Destroy(C);
114}
115
116
Chris Lattnereee57c02008-04-04 06:12:32 +0000117FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
118 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000119 StringLiteral *Str) {
120 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
121 return new (Mem) FileScopeAsmDecl(L, Str);
122}
123
Chris Lattnereee57c02008-04-04 06:12:32 +0000124LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
125 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000126 LanguageIDs Lang, Decl *D) {
127 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
128 return new (Mem) LinkageSpecDecl(L, Lang, D);
129}
Chris Lattnere4650482008-03-15 06:12:44 +0000130
131//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000132// NamedDecl Implementation
133//===----------------------------------------------------------------------===//
134
Chris Lattner910435b2007-10-06 22:53:46 +0000135const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000136 if (const IdentifierInfo *II = getIdentifier())
137 return II->getName();
138 return "";
139}
140
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000141//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000142// FunctionDecl Implementation
143//===----------------------------------------------------------------------===//
144
Chris Lattner4b009652007-07-25 00:24:17 +0000145FunctionDecl::~FunctionDecl() {
146 delete[] ParamInfo;
Douglas Gregor42214c52008-04-21 02:02:58 +0000147}
148
Ted Kremenekafdf8112008-05-20 00:43:19 +0000149void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000150 if (Body)
151 Body->Destroy(C);
152
153 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
154 (*I)->Destroy(C);
155
Ted Kremenekafdf8112008-05-20 00:43:19 +0000156 Decl::Destroy(C);
157}
158
159
Douglas Gregor42214c52008-04-21 02:02:58 +0000160Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
161 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
162 if (FD->Body) {
163 Definition = FD;
164 return FD->Body;
165 }
166 }
167
168 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000169}
170
171unsigned FunctionDecl::getNumParams() const {
Chris Lattnere8733592008-04-06 23:09:52 +0000172 const FunctionType *FT = getType()->getAsFunctionType();
173 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000174 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000175 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000176}
177
178void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
179 assert(ParamInfo == 0 && "Already has param info!");
180 assert(NumParams == getNumParams() && "Parameter count mismatch!");
181
182 // Zero params -> null pointer.
183 if (NumParams) {
184 ParamInfo = new ParmVarDecl*[NumParams];
185 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
186 }
187}
188
Chris Lattner97316c02008-04-10 02:22:51 +0000189/// getMinRequiredArguments - Returns the minimum number of arguments
190/// needed to call this function. This may be fewer than the number of
191/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000192/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000193unsigned FunctionDecl::getMinRequiredArguments() const {
194 unsigned NumRequiredArgs = getNumParams();
195 while (NumRequiredArgs > 0
196 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
197 --NumRequiredArgs;
198
199 return NumRequiredArgs;
200}
201
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000202//===----------------------------------------------------------------------===//
203// RecordDecl Implementation
204//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000205
206/// defineBody - When created, RecordDecl's correspond to a forward declared
207/// record. This method is used to mark the decl as being defined, with the
208/// specified contents.
209void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
210 assert(!isDefinition() && "Cannot redefine record!");
211 setDefinition(true);
212 NumMembers = numMembers;
213 if (numMembers) {
214 Members = new FieldDecl*[numMembers];
215 memcpy(Members, members, numMembers*sizeof(Decl*));
216 }
217}
218
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000219FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Chris Lattner4b009652007-07-25 00:24:17 +0000220 if (Members == 0 || NumMembers < 0)
221 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000222
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000223 // Linear search. When C++ classes come along, will likely need to revisit.
224 for (int i = 0; i != NumMembers; ++i)
225 if (Members[i]->getIdentifier() == II)
Chris Lattner4b009652007-07-25 00:24:17 +0000226 return Members[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000227 return 0;
228}