blob: dc4d6027ee8b60ea4b67bee7f9095ee84bf1c239 [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"
17#include "clang/Basic/IdentifierTable.h"
Ted Kremenekafdf8112008-05-20 00:43:19 +000018
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Chris Lattnera8344c32008-03-15 05:43:15 +000021//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +000022// Decl Allocation/Deallocation Method Implementations
23//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000024
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000025TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
26 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
27 return new (Mem) TranslationUnitDecl();
28}
29
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000030NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
31 SourceLocation L, IdentifierInfo *Id) {
32 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
33 return new (Mem) NamespaceDecl(DC, L, Id);
34}
35
Ted Kremenek5be49242008-05-20 04:49:55 +000036void NamespaceDecl::Destroy(ASTContext& C) {
37 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
38 // together. They are all top-level Decls.
39
Ted Kremenek0f433842008-05-24 15:09:56 +000040 this->~NamespaceDecl();
Ted Kremenek5be49242008-05-20 04:49:55 +000041 C.getAllocator().Deallocate((void *)this);
42}
43
44
Chris Lattner8c7c6a12008-06-17 18:05:57 +000045ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
46 SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) {
47 void *Mem = C.getAllocator().Allocate<ImplicitParamDecl>();
48 return new (Mem) ImplicitParamDecl(ImplicitParam, DC, L, Id, T, PrevDecl);
49}
50
Chris Lattneref87a202008-04-22 18:39:57 +000051VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff72a6ebc2008-04-15 22:42:06 +000052 SourceLocation L,
53 IdentifierInfo *Id, QualType T,
Steve Naroff71cd7762008-10-03 00:02:03 +000054 StorageClass S, ScopedDecl *PrevDecl,
55 SourceLocation TypeSpecStartLoc) {
Steve Naroff72a6ebc2008-04-15 22:42:06 +000056 void *Mem = C.getAllocator().Allocate<VarDecl>();
Steve Naroff71cd7762008-10-03 00:02:03 +000057 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl, TypeSpecStartLoc);
Chris Lattner48d225c2008-03-15 21:10:16 +000058}
59
Chris Lattneref87a202008-04-22 18:39:57 +000060ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000061 SourceLocation L, IdentifierInfo *Id,
62 QualType T, StorageClass S,
Chris Lattner3e254fb2008-04-08 04:40:51 +000063 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner48d225c2008-03-15 21:10:16 +000064 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +000065 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +000066}
67
Chris Lattneref87a202008-04-22 18:39:57 +000068FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +000069 SourceLocation L,
Chris Lattner4c7802b2008-03-15 21:24:04 +000070 IdentifierInfo *Id, QualType T,
71 StorageClass S, bool isInline,
Steve Naroff71cd7762008-10-03 00:02:03 +000072 ScopedDecl *PrevDecl,
73 SourceLocation TypeSpecStartLoc) {
Chris Lattner4c7802b2008-03-15 21:24:04 +000074 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Steve Naroff71cd7762008-10-03 00:02:03 +000075 return new (Mem) FunctionDecl(Function, DC, L, Id, T, S, isInline, PrevDecl,
76 TypeSpecStartLoc);
Chris Lattner4c7802b2008-03-15 21:24:04 +000077}
78
Steve Naroff52059382008-10-10 01:28:17 +000079BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
Steve Naroff9ac456d2008-10-08 17:01:13 +000080 void *Mem = C.getAllocator().Allocate<BlockDecl>();
Steve Naroff52059382008-10-10 01:28:17 +000081 return new (Mem) BlockDecl(DC, L);
Steve Naroff9ac456d2008-10-08 17:01:13 +000082}
83
Chris Lattnerf3874bc2008-04-06 04:47:34 +000084FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +000085 IdentifierInfo *Id, QualType T, Expr *BW) {
86 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +000087 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner81db64a2008-03-16 00:16:02 +000088}
89
Chris Lattner4c7802b2008-03-15 21:24:04 +000090
Chris Lattnereee57c02008-04-04 06:12:32 +000091EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
92 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +000093 IdentifierInfo *Id, QualType T,
94 Expr *E, const llvm::APSInt &V,
95 ScopedDecl *PrevDecl){
Chris Lattnere4650482008-03-15 06:12:44 +000096 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +000097 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +000098}
99
Ted Kremenek5be49242008-05-20 04:49:55 +0000100void EnumConstantDecl::Destroy(ASTContext& C) {
101 if (Init) Init->Destroy(C);
102 Decl::Destroy(C);
103}
104
Chris Lattneref87a202008-04-22 18:39:57 +0000105TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000106 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000107 IdentifierInfo *Id, QualType T,
108 ScopedDecl *PD) {
Chris Lattnere4650482008-03-15 06:12:44 +0000109 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000110 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnere4650482008-03-15 06:12:44 +0000111}
112
Chris Lattneref87a202008-04-22 18:39:57 +0000113EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000114 IdentifierInfo *Id,
Chris Lattner58114f02008-03-15 21:32:50 +0000115 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000116 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000117 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000118}
119
Ted Kremenek25d8be12008-09-02 20:13:32 +0000120void EnumDecl::Destroy(ASTContext& C) {
121 if (getEnumConstantList()) getEnumConstantList()->Destroy(C);
122 Decl::Destroy(C);
123}
124
Chris Lattnereee57c02008-04-04 06:12:32 +0000125FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
126 SourceLocation L,
Chris Lattner81db64a2008-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 Lattnere4650482008-03-15 06:12:44 +0000132//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000133// NamedDecl Implementation
134//===----------------------------------------------------------------------===//
135
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000136std::string NamedDecl::getName() const {
137 switch (Name.getNameKind()) {
138 case DeclarationName::Identifier:
139 if (const IdentifierInfo *II = Name.getAsIdentifierInfo())
140 return II->getName();
141 return "";
142
143 case DeclarationName::ObjCZeroArgSelector:
144 case DeclarationName::ObjCOneArgSelector:
145 case DeclarationName::ObjCMultiArgSelector:
146 return Name.getObjCSelector().getName();
147
148 case DeclarationName::CXXConstructorName: {
149 QualType ClassType = Name.getCXXNameType();
150 if (const RecordType *ClassRec = ClassType->getAsRecordType())
151 return ClassRec->getDecl()->getName();
152 return ClassType.getAsString();
153 }
154
155 case DeclarationName::CXXDestructorName: {
156 std::string Result = "~";
157 QualType Type = Name.getCXXNameType();
158 if (const RecordType *Rec = Type->getAsRecordType())
159 Result += Rec->getDecl()->getName();
160 else
161 Result += Type.getAsString();
162 return Result;
163 }
164
165 case DeclarationName::CXXConversionFunctionName: {
166 std::string Result = "operator ";
167 QualType Type = Name.getCXXNameType();
168 if (const RecordType *Rec = Type->getAsRecordType())
169 Result += Rec->getDecl()->getName();
170 else
171 Result += Type.getAsString();
172 return Result;
173 }
174 }
175
176 assert(false && "Unexpected declaration name kind");
Chris Lattner4b009652007-07-25 00:24:17 +0000177 return "";
178}
179
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000180//===----------------------------------------------------------------------===//
Argiris Kirtzidis881964b2008-11-09 23:41:00 +0000181// ScopedDecl Implementation
182//===----------------------------------------------------------------------===//
183
184void ScopedDecl::setLexicalDeclContext(DeclContext *DC) {
185 if (DC == getLexicalDeclContext())
186 return;
187
188 if (isInSemaDC()) {
189 MultipleDC *MDC = new MultipleDC();
190 MDC->SemanticDC = getDeclContext();
191 MDC->LexicalDC = DC;
192 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
193 } else {
194 getMultipleDC()->LexicalDC = DC;
195 }
196}
197
198ScopedDecl::~ScopedDecl() {
199 if (isOutOfSemaDC())
200 delete getMultipleDC();
201}
202
203//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000204// FunctionDecl Implementation
205//===----------------------------------------------------------------------===//
206
Chris Lattner4b009652007-07-25 00:24:17 +0000207FunctionDecl::~FunctionDecl() {
208 delete[] ParamInfo;
Douglas Gregor42214c52008-04-21 02:02:58 +0000209}
210
Ted Kremenekafdf8112008-05-20 00:43:19 +0000211void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000212 if (Body)
213 Body->Destroy(C);
214
215 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
216 (*I)->Destroy(C);
217
Ted Kremenekafdf8112008-05-20 00:43:19 +0000218 Decl::Destroy(C);
219}
220
221
Douglas Gregor42214c52008-04-21 02:02:58 +0000222Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
223 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
224 if (FD->Body) {
225 Definition = FD;
226 return FD->Body;
227 }
228 }
229
230 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000231}
232
Ted Kremeneka6ded352008-10-29 18:41:34 +0000233// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
234static unsigned getNumTypeParams(QualType T) {
235 const FunctionType *FT = T->getAsFunctionType();
Chris Lattnere8733592008-04-06 23:09:52 +0000236 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000237 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000238 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000239}
240
Ted Kremeneka6ded352008-10-29 18:41:34 +0000241unsigned FunctionDecl::getNumParams() const {
242 // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
243 if (!ParamInfo)
244 return 0;
245
246 return getNumTypeParams(getType());
247}
248
Chris Lattner4b009652007-07-25 00:24:17 +0000249void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
250 assert(ParamInfo == 0 && "Already has param info!");
Ted Kremeneka6ded352008-10-29 18:41:34 +0000251 assert(NumParams == getNumTypeParams(getType()) &&
252 "Parameter count mismatch!");
Chris Lattner4b009652007-07-25 00:24:17 +0000253
254 // Zero params -> null pointer.
255 if (NumParams) {
256 ParamInfo = new ParmVarDecl*[NumParams];
257 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
258 }
259}
260
Chris Lattner97316c02008-04-10 02:22:51 +0000261/// getMinRequiredArguments - Returns the minimum number of arguments
262/// needed to call this function. This may be fewer than the number of
263/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000264/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000265unsigned FunctionDecl::getMinRequiredArguments() const {
266 unsigned NumRequiredArgs = getNumParams();
267 while (NumRequiredArgs > 0
268 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
269 --NumRequiredArgs;
270
271 return NumRequiredArgs;
272}
273
Douglas Gregore60e5d32008-11-06 22:13:31 +0000274/// getOverloadedOperator - Which C++ overloaded operator this
275/// function represents, if any.
276OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
277 if (getIdentifier())
278 return getIdentifier()->getOverloadedOperatorID();
279 else
280 return OO_None;
281}
282
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000283//===----------------------------------------------------------------------===//
Ted Kremenek46a837c2008-09-05 17:16:31 +0000284// TagdDecl Implementation
285//===----------------------------------------------------------------------===//
286
287TagDecl* TagDecl::getDefinition(ASTContext& C) const {
288 QualType T = C.getTypeDeclType(const_cast<TagDecl*>(this));
289 TagDecl* D = cast<TagDecl>(cast<TagType>(T)->getDecl());
290 return D->isDefinition() ? D : 0;
291}
292
293//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000294// RecordDecl Implementation
295//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000296
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000297RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Ted Kremenek2c984042008-09-05 01:34:33 +0000298 IdentifierInfo *Id)
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000299: TagDecl(DK, TK, DC, L, Id, 0) {
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000300
301 HasFlexibleArrayMember = false;
302 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
303 Members = 0;
Ted Kremenek2c984042008-09-05 01:34:33 +0000304 NumMembers = -1;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000305}
306
307RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek46a837c2008-09-05 17:16:31 +0000308 SourceLocation L, IdentifierInfo *Id,
309 RecordDecl* PrevDecl) {
Ted Kremenek2c984042008-09-05 01:34:33 +0000310
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000311 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000312 RecordDecl* R = new (Mem) RecordDecl(Record, TK, DC, L, Id);
Ted Kremenek46a837c2008-09-05 17:16:31 +0000313 C.getTypeDeclType(R, PrevDecl);
314 return R;
Ted Kremenek6f0a2412008-09-02 21:12:32 +0000315}
316
Argiris Kirtzidisd64c1112008-08-08 14:08:55 +0000317RecordDecl::~RecordDecl() {
318 delete[] Members;
319}
320
321void RecordDecl::Destroy(ASTContext& C) {
322 if (isDefinition())
323 for (field_iterator I=field_begin(), E=field_end(); I!=E; ++I)
324 (*I)->Destroy(C);
325
326 TagDecl::Destroy(C);
327}
328
Chris Lattner4b009652007-07-25 00:24:17 +0000329/// defineBody - When created, RecordDecl's correspond to a forward declared
330/// record. This method is used to mark the decl as being defined, with the
331/// specified contents.
Ted Kremenek46a837c2008-09-05 17:16:31 +0000332void RecordDecl::defineBody(ASTContext& C, FieldDecl **members,
333 unsigned numMembers) {
Chris Lattner4b009652007-07-25 00:24:17 +0000334 assert(!isDefinition() && "Cannot redefine record!");
335 setDefinition(true);
336 NumMembers = numMembers;
337 if (numMembers) {
338 Members = new FieldDecl*[numMembers];
339 memcpy(Members, members, numMembers*sizeof(Decl*));
340 }
Ted Kremenek46a837c2008-09-05 17:16:31 +0000341
342 // Let ASTContext know that this is the defining RecordDecl this type.
343 C.setTagDefinition(this);
Chris Lattner4b009652007-07-25 00:24:17 +0000344}
345
Ted Kremenek46a837c2008-09-05 17:16:31 +0000346
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000347FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Chris Lattner4b009652007-07-25 00:24:17 +0000348 if (Members == 0 || NumMembers < 0)
349 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000350
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000351 // Linear search. When C++ classes come along, will likely need to revisit.
352 for (int i = 0; i != NumMembers; ++i)
353 if (Members[i]->getIdentifier() == II)
Chris Lattner4b009652007-07-25 00:24:17 +0000354 return Members[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000355 return 0;
356}
Steve Naroff9ac456d2008-10-08 17:01:13 +0000357
358//===----------------------------------------------------------------------===//
359// BlockDecl Implementation
360//===----------------------------------------------------------------------===//
361
362BlockDecl::~BlockDecl() {
363}
364
365void BlockDecl::Destroy(ASTContext& C) {
366 if (Body)
367 Body->Destroy(C);
368
369 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
370 (*I)->Destroy(C);
371
372 Decl::Destroy(C);
373}