blob: 949d10072ec114ae5faf851884056850395ca090 [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) {
110 case TK_enum: assert(0 && "Enum TagKind passed for Record!");
111 case TK_struct: DK = Struct; break;
112 case TK_union: DK = Union; break;
113 case TK_class: DK = Class; break;
114 }
Chris Lattnerbec41342008-04-22 18:39:57 +0000115 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000116}
117
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000118void EnumDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis406fb232008-06-10 01:32:09 +0000119 if (getEnumConstantList()) getEnumConstantList()->Destroy(C);
Ted Kremenek78aa98f2008-05-20 04:49:55 +0000120 Decl::Destroy(C);
121}
122
123
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000124FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
125 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000126 StringLiteral *Str) {
127 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
128 return new (Mem) FileScopeAsmDecl(L, Str);
129}
130
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000131LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
132 SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000133 LanguageIDs Lang, Decl *D) {
134 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
135 return new (Mem) LinkageSpecDecl(L, Lang, D);
136}
Chris Lattnera7b32872008-03-15 06:12:44 +0000137
138//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000139// NamedDecl Implementation
140//===----------------------------------------------------------------------===//
141
Chris Lattnera4016552007-10-06 22:53:46 +0000142const char *NamedDecl::getName() const {
Chris Lattnerec040b12007-01-21 23:09:50 +0000143 if (const IdentifierInfo *II = getIdentifier())
144 return II->getName();
145 return "";
Chris Lattner17ed4872006-11-20 04:58:19 +0000146}
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000147
Chris Lattner59a25942008-03-31 00:36:02 +0000148//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000149// FunctionDecl Implementation
150//===----------------------------------------------------------------------===//
151
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000152FunctionDecl::~FunctionDecl() {
153 delete[] ParamInfo;
Douglas Gregor89f238c2008-04-21 02:02:58 +0000154}
155
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000156void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekfa8a09e2008-05-20 03:56:00 +0000157 if (Body)
158 Body->Destroy(C);
159
160 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
161 (*I)->Destroy(C);
162
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000163 Decl::Destroy(C);
164}
165
166
Douglas Gregor89f238c2008-04-21 02:02:58 +0000167Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
168 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
169 if (FD->Body) {
170 Definition = FD;
171 return FD->Body;
172 }
173 }
174
175 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000176}
177
178unsigned FunctionDecl::getNumParams() const {
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000179 const FunctionType *FT = getType()->getAsFunctionType();
180 if (isa<FunctionTypeNoProto>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000181 return 0;
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000182 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000183}
184
Chris Lattner53621a52007-06-13 20:44:40 +0000185void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000186 assert(ParamInfo == 0 && "Already has param info!");
187 assert(NumParams == getNumParams() && "Parameter count mismatch!");
188
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000189 // Zero params -> null pointer.
190 if (NumParams) {
Chris Lattner53621a52007-06-13 20:44:40 +0000191 ParamInfo = new ParmVarDecl*[NumParams];
192 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000193 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000194}
Chris Lattner41943152007-01-25 04:52:46 +0000195
Chris Lattner58258242008-04-10 02:22:51 +0000196/// getMinRequiredArguments - Returns the minimum number of arguments
197/// needed to call this function. This may be fewer than the number of
198/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +0000199/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +0000200unsigned FunctionDecl::getMinRequiredArguments() const {
201 unsigned NumRequiredArgs = getNumParams();
202 while (NumRequiredArgs > 0
203 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
204 --NumRequiredArgs;
205
206 return NumRequiredArgs;
207}
208
Chris Lattner59a25942008-03-31 00:36:02 +0000209//===----------------------------------------------------------------------===//
210// RecordDecl Implementation
211//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +0000212
213/// defineBody - When created, RecordDecl's correspond to a forward declared
214/// record. This method is used to mark the decl as being defined, with the
215/// specified contents.
Steve Naroffcc321422007-03-26 23:09:51 +0000216void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
Chris Lattner41943152007-01-25 04:52:46 +0000217 assert(!isDefinition() && "Cannot redefine record!");
218 setDefinition(true);
Chris Lattner5f521502007-01-25 06:27:24 +0000219 NumMembers = numMembers;
220 if (numMembers) {
Steve Naroffcc321422007-03-26 23:09:51 +0000221 Members = new FieldDecl*[numMembers];
Chris Lattner5f521502007-01-25 06:27:24 +0000222 memcpy(Members, members, numMembers*sizeof(Decl*));
Chris Lattner41943152007-01-25 04:52:46 +0000223 }
224}
Steve Naroffcc321422007-03-26 23:09:51 +0000225
Chris Lattner59a25942008-03-31 00:36:02 +0000226FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Steve Naroffcc321422007-03-26 23:09:51 +0000227 if (Members == 0 || NumMembers < 0)
228 return 0;
Fariborz Jahanian67341402007-10-04 00:45:27 +0000229
Chris Lattner59a25942008-03-31 00:36:02 +0000230 // Linear search. When C++ classes come along, will likely need to revisit.
231 for (int i = 0; i != NumMembers; ++i)
232 if (Members[i]->getIdentifier() == II)
Steve Naroffcc321422007-03-26 23:09:51 +0000233 return Members[i];
Steve Naroffcc321422007-03-26 23:09:51 +0000234 return 0;
Chris Lattnerbd4de5df2007-07-12 15:43:07 +0000235}