blob: 900096f1eccc18adcff0e0fd98d26c148636efce [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//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Anders Carlssonf78915f2008-02-15 07:04:12 +000016#include "clang/AST/Attr.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Anders Carlssonb0dd2682008-02-15 23:30:50 +000018#include "llvm/ADT/DenseMap.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Chris Lattnerd3b90652008-03-15 05:43:15 +000021//===----------------------------------------------------------------------===//
22// Statistics
23//===----------------------------------------------------------------------===//
24
Reid Spencer5f016e22007-07-11 17:01:13 +000025// temporary statistics gathering
26static unsigned nFuncs = 0;
Steve Naroff248a7532008-04-15 22:42:06 +000027static unsigned nVars = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000028static unsigned nParmVars = 0;
29static unsigned nSUC = 0;
30static unsigned nEnumConst = 0;
31static unsigned nEnumDecls = 0;
32static unsigned nTypedef = 0;
33static unsigned nFieldDecls = 0;
Steve Naroff3536b442007-09-06 21:24:23 +000034static unsigned nInterfaceDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000035static unsigned nClassDecls = 0;
36static unsigned nMethodDecls = 0;
37static unsigned nProtocolDecls = 0;
Fariborz Jahanian894c57f2007-09-21 15:40:54 +000038static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000039static unsigned nCategoryDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000040static unsigned nIvarDecls = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000041static unsigned nObjCImplementationDecls = 0;
42static unsigned nObjCCategoryImpl = 0;
43static unsigned nObjCCompatibleAlias = 0;
44static unsigned nObjCPropertyDecl = 0;
Fariborz Jahanian61d46152008-04-16 22:00:24 +000045static unsigned nObjCPropertyImplDecl = 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +000046static unsigned nLinkageSpecDecl = 0;
Anders Carlssondfab6cb2008-02-08 00:33:21 +000047static unsigned nFileScopeAsmDecl = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000048
Reid Spencer5f016e22007-07-11 17:01:13 +000049static bool StatSwitch = false;
50
Anders Carlssonb0dd2682008-02-15 23:30:50 +000051// This keeps track of all decl attributes. Since so few decls have attrs, we
52// keep them in a hash map instead of wasting space in the Decl class.
53typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
54
55static DeclAttrMapTy *DeclAttrs = 0;
56
Steve Naroffe5ea3802007-09-17 14:49:06 +000057const char *Decl::getDeclKindName() const {
Steve Naroff8c9f13e2007-09-16 16:16:00 +000058 switch (DeclKind) {
59 default: assert(0 && "Unknown decl kind!");
Chris Lattnerd3b90652008-03-15 05:43:15 +000060 case Typedef: return "Typedef";
61 case Function: return "Function";
Steve Naroff248a7532008-04-15 22:42:06 +000062 case Var: return "Var";
Chris Lattnerd3b90652008-03-15 05:43:15 +000063 case ParmVar: return "ParmVar";
64 case EnumConstant: return "EnumConstant";
65 case ObjCInterface: return "ObjCInterface";
66 case ObjCClass: return "ObjCClass";
67 case ObjCMethod: return "ObjCMethod";
68 case ObjCProtocol: return "ObjCProtocol";
69 case ObjCForwardProtocol: return "ObjCForwardProtocol";
70 case Struct: return "Struct";
71 case Union: return "Union";
72 case Class: return "Class";
73 case Enum: return "Enum";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000074 }
75}
76
Chris Lattnerd3b90652008-03-15 05:43:15 +000077bool Decl::CollectingStats(bool Enable) {
78 if (Enable)
79 StatSwitch = true;
80 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
82
83void Decl::PrintStats() {
84 fprintf(stderr, "*** Decl Stats:\n");
85 fprintf(stderr, " %d decls total.\n",
Steve Naroff248a7532008-04-15 22:42:06 +000086 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+
Chris Lattnerc81c8142008-02-25 21:04:36 +000087 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
88 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Reid Spencer5f016e22007-07-11 17:01:13 +000089 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000090 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Steve Naroff248a7532008-04-15 22:42:06 +000091 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
92 nVars, (int)sizeof(VarDecl),
93 int(nVars*sizeof(VarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +000094 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000095 nParmVars, (int)sizeof(ParmVarDecl),
96 int(nParmVars*sizeof(ParmVarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +000097 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000098 nFieldDecls, (int)sizeof(FieldDecl),
99 int(nFieldDecls*sizeof(FieldDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000101 nSUC, (int)sizeof(RecordDecl),
102 int(nSUC*sizeof(RecordDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000104 nEnumDecls, (int)sizeof(EnumDecl),
105 int(nEnumDecls*sizeof(EnumDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000107 nEnumConst, (int)sizeof(EnumConstantDecl),
108 int(nEnumConst*sizeof(EnumConstantDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000110 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000111 // Objective-C decls...
112 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000113 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
114 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000115 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000116 nIvarDecls, (int)sizeof(ObjCIvarDecl),
117 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000118 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000119 nClassDecls, (int)sizeof(ObjCClassDecl),
120 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000121 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000122 nMethodDecls, (int)sizeof(ObjCMethodDecl),
123 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000124 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000125 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
126 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000127 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000128 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
129 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000130 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000131 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
132 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000133
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000134 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000135 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
136 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000137
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000138 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000139 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
140 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000141
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000142 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000143 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
144 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000145
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000146 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000147 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
148 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000149
Fariborz Jahanian61d46152008-04-16 22:00:24 +0000150 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
151 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
152 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
153
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 fprintf(stderr, "Total bytes = %d\n",
Steve Naroff248a7532008-04-15 22:42:06 +0000155 int(nFuncs*sizeof(FunctionDecl)+
156 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
Chris Lattnerc81c8142008-02-25 21:04:36 +0000157 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
158 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
159 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian269b10d2008-01-23 01:34:33 +0000160 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
161 nIvarDecls*sizeof(ObjCIvarDecl)+
162 nClassDecls*sizeof(ObjCClassDecl)+
163 nMethodDecls*sizeof(ObjCMethodDecl)+
164 nProtocolDecls*sizeof(ObjCProtocolDecl)+
165 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
166 nCategoryDecls*sizeof(ObjCCategoryDecl)+
167 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
168 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
169 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
170 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
Fariborz Jahanian61d46152008-04-16 22:00:24 +0000171 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000172 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
173 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)));
Fariborz Jahanian269b10d2008-01-23 01:34:33 +0000174
Reid Spencer5f016e22007-07-11 17:01:13 +0000175}
176
Chris Lattnerd3b90652008-03-15 05:43:15 +0000177void Decl::addDeclKind(Kind k) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 switch (k) {
Chris Lattnerd3b90652008-03-15 05:43:15 +0000179 case Typedef: nTypedef++; break;
180 case Function: nFuncs++; break;
Steve Naroff248a7532008-04-15 22:42:06 +0000181 case Var: nVars++; break;
Chris Lattnerd3b90652008-03-15 05:43:15 +0000182 case ParmVar: nParmVars++; break;
183 case EnumConstant: nEnumConst++; break;
184 case Field: nFieldDecls++; break;
185 case Struct: case Union: case Class: nSUC++; break;
186 case Enum: nEnumDecls++; break;
187 case ObjCInterface: nInterfaceDecls++; break;
188 case ObjCClass: nClassDecls++; break;
189 case ObjCMethod: nMethodDecls++; break;
190 case ObjCProtocol: nProtocolDecls++; break;
191 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
192 case ObjCCategory: nCategoryDecls++; break;
193 case ObjCIvar: nIvarDecls++; break;
194 case ObjCImplementation: nObjCImplementationDecls++; break;
195 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
Chris Lattner8a934232008-03-31 00:36:02 +0000196 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
Sam Bishop670aa9d2008-04-08 20:49:25 +0000197 case ObjCProperty: nObjCPropertyDecl++; break;
Fariborz Jahanian61d46152008-04-16 22:00:24 +0000198 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
Chris Lattnerd3b90652008-03-15 05:43:15 +0000199 case LinkageSpec: nLinkageSpecDecl++; break;
200 case FileScopeAsm: nFileScopeAsmDecl++; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 }
202}
203
Chris Lattnerd3b90652008-03-15 05:43:15 +0000204//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000205// Decl Allocation/Deallocation Method Implementations
206//===----------------------------------------------------------------------===//
207
Steve Naroff248a7532008-04-15 22:42:06 +0000208VarDecl *VarDecl::Create(ASTContext &C, DeclContext *CD,
209 SourceLocation L,
210 IdentifierInfo *Id, QualType T,
211 StorageClass S, ScopedDecl *PrevDecl) {
212 void *Mem = C.getAllocator().Allocate<VarDecl>();
213 return new (Mem) VarDecl(Var, CD, L, Id, T, S, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +0000214}
215
216
Chris Lattnerb048c982008-04-06 04:47:34 +0000217ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000218 SourceLocation L, IdentifierInfo *Id,
219 QualType T, StorageClass S,
Chris Lattner04421082008-04-08 04:40:51 +0000220 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000221 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattner04421082008-04-08 04:40:51 +0000222 return new (Mem) ParmVarDecl(CD, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +0000223}
224
Chris Lattnerb048c982008-04-06 04:47:34 +0000225FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000226 SourceLocation L,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000227 IdentifierInfo *Id, QualType T,
228 StorageClass S, bool isInline,
229 ScopedDecl *PrevDecl) {
230 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000231 return new (Mem) FunctionDecl(CD, L, Id, T, S, isInline, PrevDecl);
Chris Lattnera98e58d2008-03-15 21:24:04 +0000232}
233
Chris Lattnerb048c982008-04-06 04:47:34 +0000234FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000235 IdentifierInfo *Id, QualType T, Expr *BW) {
236 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerb048c982008-04-06 04:47:34 +0000237 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner8e25d862008-03-16 00:16:02 +0000238}
239
Chris Lattnera98e58d2008-03-15 21:24:04 +0000240
Chris Lattner0ed844b2008-04-04 06:12:32 +0000241EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
242 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000243 IdentifierInfo *Id, QualType T,
244 Expr *E, const llvm::APSInt &V,
245 ScopedDecl *PrevDecl){
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000246 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000247 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000248}
249
Chris Lattnerb048c982008-04-06 04:47:34 +0000250TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000251 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000252 IdentifierInfo *Id, QualType T,
253 ScopedDecl *PD) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000254 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000255 return new (Mem) TypedefDecl(CD, L, Id, T, PD);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000256}
257
Chris Lattnerb048c982008-04-06 04:47:34 +0000258EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *CD, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000259 IdentifierInfo *Id,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000260 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000261 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000262 return new (Mem) EnumDecl(CD, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000263}
264
Chris Lattnerb048c982008-04-06 04:47:34 +0000265RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *CD,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000266 SourceLocation L, IdentifierInfo *Id,
267 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000268 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000269 return new (Mem) RecordDecl(DK, CD, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000270}
271
Chris Lattner0ed844b2008-04-04 06:12:32 +0000272FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
273 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000274 StringLiteral *Str) {
275 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
276 return new (Mem) FileScopeAsmDecl(L, Str);
277}
278
Chris Lattner0ed844b2008-04-04 06:12:32 +0000279LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
280 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000281 LanguageIDs Lang, Decl *D) {
282 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
283 return new (Mem) LinkageSpecDecl(L, Lang, D);
284}
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000285
286//===----------------------------------------------------------------------===//
Chris Lattnerd3b90652008-03-15 05:43:15 +0000287// Decl Implementation
288//===----------------------------------------------------------------------===//
289
Reid Spencer5f016e22007-07-11 17:01:13 +0000290// Out-of-line virtual method providing a home for Decl.
291Decl::~Decl() {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000292 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000293 return;
294
295 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000296 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
297
298 delete it->second;
299 DeclAttrs->erase(it);
300 if (DeclAttrs->empty()) {
301 delete DeclAttrs;
302 DeclAttrs = 0;
303 }
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000304}
305
Chris Lattnerd3b90652008-03-15 05:43:15 +0000306void Decl::addAttr(Attr *NewAttr) {
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000307 if (!DeclAttrs)
Chris Lattner1e03a562008-03-16 00:19:01 +0000308 DeclAttrs = new DeclAttrMapTy();
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000309
Chris Lattnerd3b90652008-03-15 05:43:15 +0000310 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000311
Chris Lattnerd3b90652008-03-15 05:43:15 +0000312 NewAttr->setNext(ExistingAttr);
313 ExistingAttr = NewAttr;
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000314
315 HasAttrs = true;
316}
317
Chris Lattnerd3b90652008-03-15 05:43:15 +0000318const Attr *Decl::getAttrs() const {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000319 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000320 return 0;
321
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000322 return (*DeclAttrs)[this];
Reid Spencer5f016e22007-07-11 17:01:13 +0000323}
324
Sam Bishop1b30cb22008-04-13 04:32:18 +0000325#define CASE(KIND) \
326 case KIND: \
327 static_cast<KIND##Decl *>(const_cast<Decl *>(this))->~KIND##Decl(); \
328 break
Sam Bishop1bb19632008-04-11 18:04:39 +0000329
Sam Bishopbb45c512008-04-11 15:01:25 +0000330void Decl::Destroy(ASTContext& C) const {
Sam Bishop1bb19632008-04-11 18:04:39 +0000331 switch (getKind()) {
332 CASE(Field);
333 CASE(ObjCIvar);
334 CASE(ObjCCategory);
335 CASE(ObjCCategoryImpl);
336 CASE(ObjCImplementation);
337 CASE(ObjCProtocol);
338 CASE(ObjCProperty);
339 CASE(Typedef);
340 CASE(Enum);
341 CASE(EnumConstant);
342 CASE(Function);
Steve Naroff248a7532008-04-15 22:42:06 +0000343 CASE(Var);
Sam Bishop1bb19632008-04-11 18:04:39 +0000344 CASE(ParmVar);
345 CASE(ObjCInterface);
346 CASE(ObjCCompatibleAlias);
347 CASE(ObjCMethod);
348 CASE(ObjCClass);
349 CASE(ObjCForwardProtocol);
350 CASE(LinkageSpec);
351
352 case Struct: case Union: case Class:
Sam Bishop1b30cb22008-04-13 04:32:18 +0000353 static_cast<RecordDecl *>(const_cast<Decl *>(this))->~RecordDecl();
Sam Bishop1bb19632008-04-11 18:04:39 +0000354 break;
355
356 default: assert(0 && "Unknown decl kind!");
357 }
358
Sam Bishopbb45c512008-04-11 15:01:25 +0000359 C.getAllocator().Deallocate((void *)this);
360}
361
Sam Bishop1bb19632008-04-11 18:04:39 +0000362#undef CASE
363
Chris Lattner8a934232008-03-31 00:36:02 +0000364//===----------------------------------------------------------------------===//
Chris Lattnerb048c982008-04-06 04:47:34 +0000365// DeclContext Implementation
Chris Lattner0ed844b2008-04-04 06:12:32 +0000366//===----------------------------------------------------------------------===//
367
Chris Lattnerb048c982008-04-06 04:47:34 +0000368DeclContext *DeclContext::getParent() const {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000369 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Chris Lattnerb048c982008-04-06 04:47:34 +0000370 return SD->getDeclContext();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000371 else
372 return NULL;
373}
374
Chris Lattnerb048c982008-04-06 04:47:34 +0000375Decl *DeclContext::ToDecl (const DeclContext *D) {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000376 return CastTo<Decl>(D);
377}
378
Chris Lattnerb048c982008-04-06 04:47:34 +0000379DeclContext *DeclContext::FromDecl (const Decl *D) {
380 return CastTo<DeclContext>(D);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000381}
382
383//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000384// NamedDecl Implementation
385//===----------------------------------------------------------------------===//
386
Chris Lattnerfd5de472007-10-06 22:53:46 +0000387const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 if (const IdentifierInfo *II = getIdentifier())
389 return II->getName();
390 return "";
391}
392
Chris Lattner8a934232008-03-31 00:36:02 +0000393//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000394// FunctionDecl Implementation
395//===----------------------------------------------------------------------===//
396
Reid Spencer5f016e22007-07-11 17:01:13 +0000397FunctionDecl::~FunctionDecl() {
398 delete[] ParamInfo;
Sam Bishop8266d7f2008-04-03 05:01:04 +0000399 delete Body;
Reid Spencer5f016e22007-07-11 17:01:13 +0000400}
401
402unsigned FunctionDecl::getNumParams() const {
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000403 const FunctionType *FT = getType()->getAsFunctionType();
404 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000405 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000406 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000407}
408
409void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
410 assert(ParamInfo == 0 && "Already has param info!");
411 assert(NumParams == getNumParams() && "Parameter count mismatch!");
412
413 // Zero params -> null pointer.
414 if (NumParams) {
415 ParamInfo = new ParmVarDecl*[NumParams];
416 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
417 }
418}
419
Chris Lattner8123a952008-04-10 02:22:51 +0000420/// getMinRequiredArguments - Returns the minimum number of arguments
421/// needed to call this function. This may be fewer than the number of
422/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000423/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000424unsigned FunctionDecl::getMinRequiredArguments() const {
425 unsigned NumRequiredArgs = getNumParams();
426 while (NumRequiredArgs > 0
427 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
428 --NumRequiredArgs;
429
430 return NumRequiredArgs;
431}
432
Chris Lattner8a934232008-03-31 00:36:02 +0000433//===----------------------------------------------------------------------===//
434// RecordDecl Implementation
435//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000436
437/// defineBody - When created, RecordDecl's correspond to a forward declared
438/// record. This method is used to mark the decl as being defined, with the
439/// specified contents.
440void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
441 assert(!isDefinition() && "Cannot redefine record!");
442 setDefinition(true);
443 NumMembers = numMembers;
444 if (numMembers) {
445 Members = new FieldDecl*[numMembers];
446 memcpy(Members, members, numMembers*sizeof(Decl*));
447 }
448}
449
Chris Lattner8a934232008-03-31 00:36:02 +0000450FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 if (Members == 0 || NumMembers < 0)
452 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000453
Chris Lattner8a934232008-03-31 00:36:02 +0000454 // Linear search. When C++ classes come along, will likely need to revisit.
455 for (int i = 0; i != NumMembers; ++i)
456 if (Members[i]->getIdentifier() == II)
Reid Spencer5f016e22007-07-11 17:01:13 +0000457 return Members[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000459}