blob: 8d9913b1aae9dc82b94f8aa75d8bca963d59df7d [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//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattnere4650482008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Anders Carlsson3f70c542008-02-15 07:04:12 +000016#include "clang/AST/Attr.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Anders Carlsson484eb5f2008-02-15 23:30:50 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenekafdf8112008-05-20 00:43:19 +000019
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Chris Lattnera8344c32008-03-15 05:43:15 +000022//===----------------------------------------------------------------------===//
23// Statistics
24//===----------------------------------------------------------------------===//
25
Chris Lattner4b009652007-07-25 00:24:17 +000026// temporary statistics gathering
27static unsigned nFuncs = 0;
Steve Naroff72a6ebc2008-04-15 22:42:06 +000028static unsigned nVars = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000029static unsigned nParmVars = 0;
30static unsigned nSUC = 0;
31static unsigned nEnumConst = 0;
32static unsigned nEnumDecls = 0;
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000033static unsigned nNamespaces = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000034static unsigned nTypedef = 0;
35static unsigned nFieldDecls = 0;
Steve Naroff81f1bba2007-09-06 21:24:23 +000036static unsigned nInterfaceDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000037static unsigned nClassDecls = 0;
38static unsigned nMethodDecls = 0;
39static unsigned nProtocolDecls = 0;
Fariborz Jahanianc716c942007-09-21 15:40:54 +000040static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000041static unsigned nCategoryDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000042static unsigned nIvarDecls = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000043static unsigned nObjCImplementationDecls = 0;
44static unsigned nObjCCategoryImpl = 0;
45static unsigned nObjCCompatibleAlias = 0;
46static unsigned nObjCPropertyDecl = 0;
Fariborz Jahanian65a81732008-04-16 22:00:24 +000047static unsigned nObjCPropertyImplDecl = 0;
Chris Lattner806a5f52008-01-12 07:05:38 +000048static unsigned nLinkageSpecDecl = 0;
Anders Carlsson4f7f4412008-02-08 00:33:21 +000049static unsigned nFileScopeAsmDecl = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000050
Chris Lattner4b009652007-07-25 00:24:17 +000051static bool StatSwitch = false;
52
Anders Carlsson484eb5f2008-02-15 23:30:50 +000053// This keeps track of all decl attributes. Since so few decls have attrs, we
54// keep them in a hash map instead of wasting space in the Decl class.
55typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
56
57static DeclAttrMapTy *DeclAttrs = 0;
58
Steve Naroff590aba82007-09-17 14:49:06 +000059const char *Decl::getDeclKindName() const {
Steve Narofff0c31dd2007-09-16 16:16:00 +000060 switch (DeclKind) {
61 default: assert(0 && "Unknown decl kind!");
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000062 case Namespace: return "Namespace";
Chris Lattnera8344c32008-03-15 05:43:15 +000063 case Typedef: return "Typedef";
64 case Function: return "Function";
Steve Naroff72a6ebc2008-04-15 22:42:06 +000065 case Var: return "Var";
Chris Lattnera8344c32008-03-15 05:43:15 +000066 case ParmVar: return "ParmVar";
67 case EnumConstant: return "EnumConstant";
Steve Naroffc38f4352008-05-23 00:59:14 +000068 case ObjCIvar: return "ObjCIvar";
Chris Lattnera8344c32008-03-15 05:43:15 +000069 case ObjCInterface: return "ObjCInterface";
70 case ObjCClass: return "ObjCClass";
71 case ObjCMethod: return "ObjCMethod";
72 case ObjCProtocol: return "ObjCProtocol";
73 case ObjCForwardProtocol: return "ObjCForwardProtocol";
74 case Struct: return "Struct";
75 case Union: return "Union";
76 case Class: return "Class";
77 case Enum: return "Enum";
Steve Narofff0c31dd2007-09-16 16:16:00 +000078 }
79}
80
Chris Lattnera8344c32008-03-15 05:43:15 +000081bool Decl::CollectingStats(bool Enable) {
82 if (Enable)
83 StatSwitch = true;
84 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
87void Decl::PrintStats() {
88 fprintf(stderr, "*** Decl Stats:\n");
89 fprintf(stderr, " %d decls total.\n",
Steve Naroff72a6ebc2008-04-15 22:42:06 +000090 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+
Chris Lattner43b885f2008-02-25 21:04:36 +000091 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000092 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
93 nNamespaces));
94 fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n",
95 nNamespaces, (int)sizeof(NamespaceDecl),
96 int(nNamespaces*sizeof(NamespaceDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000097 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000098 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Steve Naroff72a6ebc2008-04-15 22:42:06 +000099 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
100 nVars, (int)sizeof(VarDecl),
101 int(nVars*sizeof(VarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000102 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000103 nParmVars, (int)sizeof(ParmVarDecl),
104 int(nParmVars*sizeof(ParmVarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000105 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000106 nFieldDecls, (int)sizeof(FieldDecl),
107 int(nFieldDecls*sizeof(FieldDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000108 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000109 nSUC, (int)sizeof(RecordDecl),
110 int(nSUC*sizeof(RecordDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000111 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000112 nEnumDecls, (int)sizeof(EnumDecl),
113 int(nEnumDecls*sizeof(EnumDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000114 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000115 nEnumConst, (int)sizeof(EnumConstantDecl),
116 int(nEnumConst*sizeof(EnumConstantDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000117 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000118 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000119 // Objective-C decls...
120 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000121 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
122 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000123 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000124 nIvarDecls, (int)sizeof(ObjCIvarDecl),
125 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000126 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000127 nClassDecls, (int)sizeof(ObjCClassDecl),
128 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000129 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000130 nMethodDecls, (int)sizeof(ObjCMethodDecl),
131 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000132 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000133 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
134 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000135 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000136 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
137 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000138 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000139 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
140 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000141
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000142 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000143 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
144 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000145
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000146 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000147 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
148 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000149
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000150 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000151 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
152 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000153
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000154 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000155 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
156 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000157
Fariborz Jahanian65a81732008-04-16 22:00:24 +0000158 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
159 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
160 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
161
Chris Lattner4b009652007-07-25 00:24:17 +0000162 fprintf(stderr, "Total bytes = %d\n",
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000163 int(nFuncs*sizeof(FunctionDecl)+
164 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
Chris Lattner43b885f2008-02-25 21:04:36 +0000165 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
166 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
167 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000168 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
169 nIvarDecls*sizeof(ObjCIvarDecl)+
170 nClassDecls*sizeof(ObjCClassDecl)+
171 nMethodDecls*sizeof(ObjCMethodDecl)+
172 nProtocolDecls*sizeof(ObjCProtocolDecl)+
173 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
174 nCategoryDecls*sizeof(ObjCCategoryDecl)+
175 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
176 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
177 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
178 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
Fariborz Jahanian65a81732008-04-16 22:00:24 +0000179 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000180 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000181 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
182 nNamespaces*sizeof(NamespaceDecl)));
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000183
Chris Lattner4b009652007-07-25 00:24:17 +0000184}
185
Chris Lattnera8344c32008-03-15 05:43:15 +0000186void Decl::addDeclKind(Kind k) {
Chris Lattner4b009652007-07-25 00:24:17 +0000187 switch (k) {
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000188 case Namespace: nNamespaces++; break;
Chris Lattnera8344c32008-03-15 05:43:15 +0000189 case Typedef: nTypedef++; break;
190 case Function: nFuncs++; break;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000191 case Var: nVars++; break;
Chris Lattnera8344c32008-03-15 05:43:15 +0000192 case ParmVar: nParmVars++; break;
193 case EnumConstant: nEnumConst++; break;
194 case Field: nFieldDecls++; break;
195 case Struct: case Union: case Class: nSUC++; break;
196 case Enum: nEnumDecls++; break;
197 case ObjCInterface: nInterfaceDecls++; break;
198 case ObjCClass: nClassDecls++; break;
199 case ObjCMethod: nMethodDecls++; break;
200 case ObjCProtocol: nProtocolDecls++; break;
201 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
202 case ObjCCategory: nCategoryDecls++; break;
203 case ObjCIvar: nIvarDecls++; break;
204 case ObjCImplementation: nObjCImplementationDecls++; break;
205 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000206 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
Sam Bishopbe9c27a2008-04-08 20:49:25 +0000207 case ObjCProperty: nObjCPropertyDecl++; break;
Fariborz Jahanian65a81732008-04-16 22:00:24 +0000208 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
Chris Lattnera8344c32008-03-15 05:43:15 +0000209 case LinkageSpec: nLinkageSpecDecl++; break;
210 case FileScopeAsm: nFileScopeAsmDecl++; break;
Argiris Kirtzidisd3586002008-04-17 14:40:12 +0000211 case TranslationUnit: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000212 }
213}
214
Chris Lattnera8344c32008-03-15 05:43:15 +0000215//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +0000216// Decl Allocation/Deallocation Method Implementations
217//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000218
Argiris Kirtzidisd3586002008-04-17 14:40:12 +0000219TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
220 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
221 return new (Mem) TranslationUnitDecl();
222}
223
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000224NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
225 SourceLocation L, IdentifierInfo *Id) {
226 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
227 return new (Mem) NamespaceDecl(DC, L, Id);
228}
229
Ted Kremenek5be49242008-05-20 04:49:55 +0000230void NamespaceDecl::Destroy(ASTContext& C) {
231 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
232 // together. They are all top-level Decls.
233
Ted Kremenek0f433842008-05-24 15:09:56 +0000234 this->~NamespaceDecl();
Ted Kremenek5be49242008-05-20 04:49:55 +0000235 C.getAllocator().Deallocate((void *)this);
236}
237
238
Chris Lattneref87a202008-04-22 18:39:57 +0000239VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000240 SourceLocation L,
241 IdentifierInfo *Id, QualType T,
242 StorageClass S, ScopedDecl *PrevDecl) {
243 void *Mem = C.getAllocator().Allocate<VarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000244 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +0000245}
246
Chris Lattneref87a202008-04-22 18:39:57 +0000247ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000248 SourceLocation L, IdentifierInfo *Id,
249 QualType T, StorageClass S,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000250 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner48d225c2008-03-15 21:10:16 +0000251 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000252 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +0000253}
254
Chris Lattneref87a202008-04-22 18:39:57 +0000255FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000256 SourceLocation L,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000257 IdentifierInfo *Id, QualType T,
258 StorageClass S, bool isInline,
259 ScopedDecl *PrevDecl) {
260 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000261 return new (Mem) FunctionDecl(DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattner4c7802b2008-03-15 21:24:04 +0000262}
263
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000264FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000265 IdentifierInfo *Id, QualType T, Expr *BW) {
266 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000267 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner81db64a2008-03-16 00:16:02 +0000268}
269
Chris Lattner4c7802b2008-03-15 21:24:04 +0000270
Chris Lattnereee57c02008-04-04 06:12:32 +0000271EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
272 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000273 IdentifierInfo *Id, QualType T,
274 Expr *E, const llvm::APSInt &V,
275 ScopedDecl *PrevDecl){
Chris Lattnere4650482008-03-15 06:12:44 +0000276 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +0000277 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000278}
279
Ted Kremenek5be49242008-05-20 04:49:55 +0000280void EnumConstantDecl::Destroy(ASTContext& C) {
281 if (Init) Init->Destroy(C);
282 Decl::Destroy(C);
283}
284
Chris Lattneref87a202008-04-22 18:39:57 +0000285TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000286 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000287 IdentifierInfo *Id, QualType T,
288 ScopedDecl *PD) {
Chris Lattnere4650482008-03-15 06:12:44 +0000289 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000290 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnere4650482008-03-15 06:12:44 +0000291}
292
Chris Lattneref87a202008-04-22 18:39:57 +0000293EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000294 IdentifierInfo *Id,
Chris Lattner58114f02008-03-15 21:32:50 +0000295 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000296 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000297 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000298}
299
Chris Lattneref87a202008-04-22 18:39:57 +0000300RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000301 SourceLocation L, IdentifierInfo *Id,
302 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000303 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000304 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000305}
306
Ted Kremenek5be49242008-05-20 04:49:55 +0000307void EnumDecl::Destroy(ASTContext& C) {
308 if (ElementList) ElementList->Destroy(C);
309 Decl::Destroy(C);
310}
311
312
Chris Lattnereee57c02008-04-04 06:12:32 +0000313FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
314 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000315 StringLiteral *Str) {
316 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
317 return new (Mem) FileScopeAsmDecl(L, Str);
318}
319
Chris Lattnereee57c02008-04-04 06:12:32 +0000320LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
321 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000322 LanguageIDs Lang, Decl *D) {
323 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
324 return new (Mem) LinkageSpecDecl(L, Lang, D);
325}
Chris Lattnere4650482008-03-15 06:12:44 +0000326
327//===----------------------------------------------------------------------===//
Chris Lattnera8344c32008-03-15 05:43:15 +0000328// Decl Implementation
329//===----------------------------------------------------------------------===//
330
Chris Lattner4b009652007-07-25 00:24:17 +0000331// Out-of-line virtual method providing a home for Decl.
332Decl::~Decl() {
Anders Carlssonb610a992008-02-16 03:37:41 +0000333 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000334 return;
335
336 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssonb610a992008-02-16 03:37:41 +0000337 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
338
Nuno Lopes77654342008-06-01 22:53:53 +0000339 // release attributes.
340 delete it->second;
341 invalidateAttrs();
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000342}
343
Chris Lattnera8344c32008-03-15 05:43:15 +0000344void Decl::addAttr(Attr *NewAttr) {
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000345 if (!DeclAttrs)
Chris Lattner10318b82008-03-16 00:19:01 +0000346 DeclAttrs = new DeclAttrMapTy();
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000347
Chris Lattnera8344c32008-03-15 05:43:15 +0000348 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000349
Chris Lattnera8344c32008-03-15 05:43:15 +0000350 NewAttr->setNext(ExistingAttr);
351 ExistingAttr = NewAttr;
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000352
353 HasAttrs = true;
354}
355
Nuno Lopes77654342008-06-01 22:53:53 +0000356void Decl::invalidateAttrs() {
357 if (!HasAttrs) return;
358
359 HasAttrs = false;
360 (*DeclAttrs)[this] = 0;
361 DeclAttrs->erase(this);
362
363 if (DeclAttrs->empty()) {
364 delete DeclAttrs;
365 DeclAttrs = 0;
366 }
367}
368
Chris Lattnera8344c32008-03-15 05:43:15 +0000369const Attr *Decl::getAttrs() const {
Anders Carlssonb610a992008-02-16 03:37:41 +0000370 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000371 return 0;
372
Anders Carlssonb610a992008-02-16 03:37:41 +0000373 return (*DeclAttrs)[this];
Chris Lattner4b009652007-07-25 00:24:17 +0000374}
375
Chris Lattner74bf5552008-05-04 02:29:49 +0000376void Decl::swapAttrs(Decl *RHS) {
377 bool HasLHSAttr = this->HasAttrs;
378 bool HasRHSAttr = RHS->HasAttrs;
379
380 // Usually, neither decl has attrs, nothing to do.
381 if (!HasLHSAttr && !HasRHSAttr) return;
382
383 // If 'this' has no attrs, swap the other way.
384 if (!HasLHSAttr)
385 return RHS->swapAttrs(this);
386
387 // Handle the case when both decls have attrs.
388 if (HasRHSAttr) {
389 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
390 return;
391 }
392
393 // Otherwise, LHS has an attr and RHS doesn't.
394 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
395 (*DeclAttrs).erase(this);
396 this->HasAttrs = false;
397 RHS->HasAttrs = true;
398}
399
400
Ted Kremenekafdf8112008-05-20 00:43:19 +0000401void Decl::Destroy(ASTContext& C) {
Ted Kremenek5be49242008-05-20 04:49:55 +0000402
403 if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {
404
405 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
406 // within the loop, only the Destroy method for the first ScopedDecl
407 // will deallocate all of the ScopedDecls in a chain.
408
409 ScopedDecl* N = SD->getNextDeclarator();
410
411 while (N) {
412 ScopedDecl* Tmp = N->getNextDeclarator();
413 N->NextDeclarator = 0x0;
414 N->Destroy(C);
415 N = Tmp;
416 }
417 }
418
Ted Kremenekafdf8112008-05-20 00:43:19 +0000419 this->~Decl();
Sam Bishop274366f2008-04-11 15:01:25 +0000420 C.getAllocator().Deallocate((void *)this);
421}
422
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000423//===----------------------------------------------------------------------===//
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000424// DeclContext Implementation
Chris Lattnereee57c02008-04-04 06:12:32 +0000425//===----------------------------------------------------------------------===//
426
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000427DeclContext *DeclContext::getParent() const {
Chris Lattnereee57c02008-04-04 06:12:32 +0000428 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000429 return SD->getDeclContext();
Chris Lattnereee57c02008-04-04 06:12:32 +0000430 else
431 return NULL;
432}
433
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000434Decl *DeclContext::ToDecl (const DeclContext *D) {
Chris Lattnereee57c02008-04-04 06:12:32 +0000435 return CastTo<Decl>(D);
436}
437
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000438DeclContext *DeclContext::FromDecl (const Decl *D) {
439 return CastTo<DeclContext>(D);
Chris Lattnereee57c02008-04-04 06:12:32 +0000440}
441
442//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000443// NamedDecl Implementation
444//===----------------------------------------------------------------------===//
445
Chris Lattner910435b2007-10-06 22:53:46 +0000446const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000447 if (const IdentifierInfo *II = getIdentifier())
448 return II->getName();
449 return "";
450}
451
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000452//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000453// FunctionDecl Implementation
454//===----------------------------------------------------------------------===//
455
Chris Lattner4b009652007-07-25 00:24:17 +0000456FunctionDecl::~FunctionDecl() {
457 delete[] ParamInfo;
Douglas Gregor42214c52008-04-21 02:02:58 +0000458}
459
Ted Kremenekafdf8112008-05-20 00:43:19 +0000460void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000461 if (Body)
462 Body->Destroy(C);
463
464 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
465 (*I)->Destroy(C);
466
Ted Kremenekafdf8112008-05-20 00:43:19 +0000467 Decl::Destroy(C);
468}
469
470
Douglas Gregor42214c52008-04-21 02:02:58 +0000471Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
472 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
473 if (FD->Body) {
474 Definition = FD;
475 return FD->Body;
476 }
477 }
478
479 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000480}
481
482unsigned FunctionDecl::getNumParams() const {
Chris Lattnere8733592008-04-06 23:09:52 +0000483 const FunctionType *FT = getType()->getAsFunctionType();
484 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000485 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000486 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000487}
488
489void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
490 assert(ParamInfo == 0 && "Already has param info!");
491 assert(NumParams == getNumParams() && "Parameter count mismatch!");
492
493 // Zero params -> null pointer.
494 if (NumParams) {
495 ParamInfo = new ParmVarDecl*[NumParams];
496 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
497 }
498}
499
Chris Lattner97316c02008-04-10 02:22:51 +0000500/// getMinRequiredArguments - Returns the minimum number of arguments
501/// needed to call this function. This may be fewer than the number of
502/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000503/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000504unsigned FunctionDecl::getMinRequiredArguments() const {
505 unsigned NumRequiredArgs = getNumParams();
506 while (NumRequiredArgs > 0
507 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
508 --NumRequiredArgs;
509
510 return NumRequiredArgs;
511}
512
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000513//===----------------------------------------------------------------------===//
514// RecordDecl Implementation
515//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000516
517/// defineBody - When created, RecordDecl's correspond to a forward declared
518/// record. This method is used to mark the decl as being defined, with the
519/// specified contents.
520void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
521 assert(!isDefinition() && "Cannot redefine record!");
522 setDefinition(true);
523 NumMembers = numMembers;
524 if (numMembers) {
525 Members = new FieldDecl*[numMembers];
526 memcpy(Members, members, numMembers*sizeof(Decl*));
527 }
528}
529
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000530FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Chris Lattner4b009652007-07-25 00:24:17 +0000531 if (Members == 0 || NumMembers < 0)
532 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000533
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000534 // Linear search. When C++ classes come along, will likely need to revisit.
535 for (int i = 0; i != NumMembers; ++i)
536 if (Members[i]->getIdentifier() == II)
Chris Lattner4b009652007-07-25 00:24:17 +0000537 return Members[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000538 return 0;
539}