blob: 02d86a32fccd358b94342c09f07a76f0d025d797 [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";
68 case ObjCInterface: return "ObjCInterface";
69 case ObjCClass: return "ObjCClass";
70 case ObjCMethod: return "ObjCMethod";
71 case ObjCProtocol: return "ObjCProtocol";
72 case ObjCForwardProtocol: return "ObjCForwardProtocol";
73 case Struct: return "Struct";
74 case Union: return "Union";
75 case Class: return "Class";
76 case Enum: return "Enum";
Steve Narofff0c31dd2007-09-16 16:16:00 +000077 }
78}
79
Chris Lattnera8344c32008-03-15 05:43:15 +000080bool Decl::CollectingStats(bool Enable) {
81 if (Enable)
82 StatSwitch = true;
83 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000084}
85
86void Decl::PrintStats() {
87 fprintf(stderr, "*** Decl Stats:\n");
88 fprintf(stderr, " %d decls total.\n",
Steve Naroff72a6ebc2008-04-15 22:42:06 +000089 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+
Chris Lattner43b885f2008-02-25 21:04:36 +000090 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000091 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
92 nNamespaces));
93 fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n",
94 nNamespaces, (int)sizeof(NamespaceDecl),
95 int(nNamespaces*sizeof(NamespaceDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000096 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000097 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Steve Naroff72a6ebc2008-04-15 22:42:06 +000098 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
99 nVars, (int)sizeof(VarDecl),
100 int(nVars*sizeof(VarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000101 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000102 nParmVars, (int)sizeof(ParmVarDecl),
103 int(nParmVars*sizeof(ParmVarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000104 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000105 nFieldDecls, (int)sizeof(FieldDecl),
106 int(nFieldDecls*sizeof(FieldDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000107 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000108 nSUC, (int)sizeof(RecordDecl),
109 int(nSUC*sizeof(RecordDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000110 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000111 nEnumDecls, (int)sizeof(EnumDecl),
112 int(nEnumDecls*sizeof(EnumDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000113 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000114 nEnumConst, (int)sizeof(EnumConstantDecl),
115 int(nEnumConst*sizeof(EnumConstantDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000116 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000117 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000118 // Objective-C decls...
119 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000120 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
121 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000122 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000123 nIvarDecls, (int)sizeof(ObjCIvarDecl),
124 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000125 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000126 nClassDecls, (int)sizeof(ObjCClassDecl),
127 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000128 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000129 nMethodDecls, (int)sizeof(ObjCMethodDecl),
130 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000131 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000132 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
133 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000134 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000135 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
136 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000137 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000138 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
139 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000140
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000141 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000142 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
143 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000144
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000145 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000146 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
147 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000148
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000149 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000150 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
151 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000152
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000153 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000154 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
155 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000156
Fariborz Jahanian65a81732008-04-16 22:00:24 +0000157 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
158 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
159 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
160
Chris Lattner4b009652007-07-25 00:24:17 +0000161 fprintf(stderr, "Total bytes = %d\n",
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000162 int(nFuncs*sizeof(FunctionDecl)+
163 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
Chris Lattner43b885f2008-02-25 21:04:36 +0000164 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
165 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
166 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000167 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
168 nIvarDecls*sizeof(ObjCIvarDecl)+
169 nClassDecls*sizeof(ObjCClassDecl)+
170 nMethodDecls*sizeof(ObjCMethodDecl)+
171 nProtocolDecls*sizeof(ObjCProtocolDecl)+
172 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
173 nCategoryDecls*sizeof(ObjCCategoryDecl)+
174 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
175 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
176 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
177 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
Fariborz Jahanian65a81732008-04-16 22:00:24 +0000178 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000179 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000180 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
181 nNamespaces*sizeof(NamespaceDecl)));
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000182
Chris Lattner4b009652007-07-25 00:24:17 +0000183}
184
Chris Lattnera8344c32008-03-15 05:43:15 +0000185void Decl::addDeclKind(Kind k) {
Chris Lattner4b009652007-07-25 00:24:17 +0000186 switch (k) {
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000187 case Namespace: nNamespaces++; break;
Chris Lattnera8344c32008-03-15 05:43:15 +0000188 case Typedef: nTypedef++; break;
189 case Function: nFuncs++; break;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000190 case Var: nVars++; break;
Chris Lattnera8344c32008-03-15 05:43:15 +0000191 case ParmVar: nParmVars++; break;
192 case EnumConstant: nEnumConst++; break;
193 case Field: nFieldDecls++; break;
194 case Struct: case Union: case Class: nSUC++; break;
195 case Enum: nEnumDecls++; break;
196 case ObjCInterface: nInterfaceDecls++; break;
197 case ObjCClass: nClassDecls++; break;
198 case ObjCMethod: nMethodDecls++; break;
199 case ObjCProtocol: nProtocolDecls++; break;
200 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
201 case ObjCCategory: nCategoryDecls++; break;
202 case ObjCIvar: nIvarDecls++; break;
203 case ObjCImplementation: nObjCImplementationDecls++; break;
204 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000205 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
Sam Bishopbe9c27a2008-04-08 20:49:25 +0000206 case ObjCProperty: nObjCPropertyDecl++; break;
Fariborz Jahanian65a81732008-04-16 22:00:24 +0000207 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
Chris Lattnera8344c32008-03-15 05:43:15 +0000208 case LinkageSpec: nLinkageSpecDecl++; break;
209 case FileScopeAsm: nFileScopeAsmDecl++; break;
Argiris Kirtzidisd3586002008-04-17 14:40:12 +0000210 case TranslationUnit: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000211 }
212}
213
Chris Lattnera8344c32008-03-15 05:43:15 +0000214//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +0000215// Decl Allocation/Deallocation Method Implementations
216//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000217
Argiris Kirtzidisd3586002008-04-17 14:40:12 +0000218TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
219 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
220 return new (Mem) TranslationUnitDecl();
221}
222
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000223NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
224 SourceLocation L, IdentifierInfo *Id) {
225 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
226 return new (Mem) NamespaceDecl(DC, L, Id);
227}
228
Ted Kremenek5be49242008-05-20 04:49:55 +0000229void NamespaceDecl::Destroy(ASTContext& C) {
230 // NamespaceDecl uses "NextDeclarator" to chain namespace declarations
231 // together. They are all top-level Decls.
232
233 this->~Decl();
234 C.getAllocator().Deallocate((void *)this);
235}
236
237
Chris Lattneref87a202008-04-22 18:39:57 +0000238VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000239 SourceLocation L,
240 IdentifierInfo *Id, QualType T,
241 StorageClass S, ScopedDecl *PrevDecl) {
242 void *Mem = C.getAllocator().Allocate<VarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000243 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +0000244}
245
Chris Lattneref87a202008-04-22 18:39:57 +0000246ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000247 SourceLocation L, IdentifierInfo *Id,
248 QualType T, StorageClass S,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000249 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner48d225c2008-03-15 21:10:16 +0000250 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000251 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +0000252}
253
Chris Lattneref87a202008-04-22 18:39:57 +0000254FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000255 SourceLocation L,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000256 IdentifierInfo *Id, QualType T,
257 StorageClass S, bool isInline,
258 ScopedDecl *PrevDecl) {
259 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000260 return new (Mem) FunctionDecl(DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattner4c7802b2008-03-15 21:24:04 +0000261}
262
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000263FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000264 IdentifierInfo *Id, QualType T, Expr *BW) {
265 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000266 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner81db64a2008-03-16 00:16:02 +0000267}
268
Chris Lattner4c7802b2008-03-15 21:24:04 +0000269
Chris Lattnereee57c02008-04-04 06:12:32 +0000270EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
271 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000272 IdentifierInfo *Id, QualType T,
273 Expr *E, const llvm::APSInt &V,
274 ScopedDecl *PrevDecl){
Chris Lattnere4650482008-03-15 06:12:44 +0000275 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +0000276 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000277}
278
Ted Kremenek5be49242008-05-20 04:49:55 +0000279void EnumConstantDecl::Destroy(ASTContext& C) {
280 if (Init) Init->Destroy(C);
281 Decl::Destroy(C);
282}
283
Chris Lattneref87a202008-04-22 18:39:57 +0000284TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000285 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000286 IdentifierInfo *Id, QualType T,
287 ScopedDecl *PD) {
Chris Lattnere4650482008-03-15 06:12:44 +0000288 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000289 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnere4650482008-03-15 06:12:44 +0000290}
291
Chris Lattneref87a202008-04-22 18:39:57 +0000292EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000293 IdentifierInfo *Id,
Chris Lattner58114f02008-03-15 21:32:50 +0000294 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000295 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000296 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000297}
298
Chris Lattneref87a202008-04-22 18:39:57 +0000299RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000300 SourceLocation L, IdentifierInfo *Id,
301 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000302 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000303 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000304}
305
Ted Kremenek5be49242008-05-20 04:49:55 +0000306void EnumDecl::Destroy(ASTContext& C) {
307 if (ElementList) ElementList->Destroy(C);
308 Decl::Destroy(C);
309}
310
311
Chris Lattnereee57c02008-04-04 06:12:32 +0000312FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
313 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000314 StringLiteral *Str) {
315 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
316 return new (Mem) FileScopeAsmDecl(L, Str);
317}
318
Chris Lattnereee57c02008-04-04 06:12:32 +0000319LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
320 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000321 LanguageIDs Lang, Decl *D) {
322 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
323 return new (Mem) LinkageSpecDecl(L, Lang, D);
324}
Chris Lattnere4650482008-03-15 06:12:44 +0000325
326//===----------------------------------------------------------------------===//
Chris Lattnera8344c32008-03-15 05:43:15 +0000327// Decl Implementation
328//===----------------------------------------------------------------------===//
329
Chris Lattner4b009652007-07-25 00:24:17 +0000330// Out-of-line virtual method providing a home for Decl.
331Decl::~Decl() {
Anders Carlssonb610a992008-02-16 03:37:41 +0000332 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000333 return;
334
335 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssonb610a992008-02-16 03:37:41 +0000336 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
337
Ted Kremenekafdf8112008-05-20 00:43:19 +0000338 // FIXME: Properly release attributes.
339 // delete it->second;
Anders Carlssonb610a992008-02-16 03:37:41 +0000340 DeclAttrs->erase(it);
Ted Kremenekafdf8112008-05-20 00:43:19 +0000341
Anders Carlssonb610a992008-02-16 03:37:41 +0000342 if (DeclAttrs->empty()) {
343 delete DeclAttrs;
344 DeclAttrs = 0;
345 }
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000346}
347
Chris Lattnera8344c32008-03-15 05:43:15 +0000348void Decl::addAttr(Attr *NewAttr) {
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000349 if (!DeclAttrs)
Chris Lattner10318b82008-03-16 00:19:01 +0000350 DeclAttrs = new DeclAttrMapTy();
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000351
Chris Lattnera8344c32008-03-15 05:43:15 +0000352 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000353
Chris Lattnera8344c32008-03-15 05:43:15 +0000354 NewAttr->setNext(ExistingAttr);
355 ExistingAttr = NewAttr;
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000356
357 HasAttrs = true;
358}
359
Chris Lattnera8344c32008-03-15 05:43:15 +0000360const Attr *Decl::getAttrs() const {
Anders Carlssonb610a992008-02-16 03:37:41 +0000361 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000362 return 0;
363
Anders Carlssonb610a992008-02-16 03:37:41 +0000364 return (*DeclAttrs)[this];
Chris Lattner4b009652007-07-25 00:24:17 +0000365}
366
Chris Lattner74bf5552008-05-04 02:29:49 +0000367void Decl::swapAttrs(Decl *RHS) {
368 bool HasLHSAttr = this->HasAttrs;
369 bool HasRHSAttr = RHS->HasAttrs;
370
371 // Usually, neither decl has attrs, nothing to do.
372 if (!HasLHSAttr && !HasRHSAttr) return;
373
374 // If 'this' has no attrs, swap the other way.
375 if (!HasLHSAttr)
376 return RHS->swapAttrs(this);
377
378 // Handle the case when both decls have attrs.
379 if (HasRHSAttr) {
380 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
381 return;
382 }
383
384 // Otherwise, LHS has an attr and RHS doesn't.
385 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
386 (*DeclAttrs).erase(this);
387 this->HasAttrs = false;
388 RHS->HasAttrs = true;
389}
390
391
Ted Kremenekafdf8112008-05-20 00:43:19 +0000392void Decl::Destroy(ASTContext& C) {
Ted Kremenek5be49242008-05-20 04:49:55 +0000393
394 if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) {
395
396 // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0
397 // within the loop, only the Destroy method for the first ScopedDecl
398 // will deallocate all of the ScopedDecls in a chain.
399
400 ScopedDecl* N = SD->getNextDeclarator();
401
402 while (N) {
403 ScopedDecl* Tmp = N->getNextDeclarator();
404 N->NextDeclarator = 0x0;
405 N->Destroy(C);
406 N = Tmp;
407 }
408 }
409
Ted Kremenekafdf8112008-05-20 00:43:19 +0000410 this->~Decl();
Sam Bishop274366f2008-04-11 15:01:25 +0000411 C.getAllocator().Deallocate((void *)this);
412}
413
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000414//===----------------------------------------------------------------------===//
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000415// DeclContext Implementation
Chris Lattnereee57c02008-04-04 06:12:32 +0000416//===----------------------------------------------------------------------===//
417
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000418DeclContext *DeclContext::getParent() const {
Chris Lattnereee57c02008-04-04 06:12:32 +0000419 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000420 return SD->getDeclContext();
Chris Lattnereee57c02008-04-04 06:12:32 +0000421 else
422 return NULL;
423}
424
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000425Decl *DeclContext::ToDecl (const DeclContext *D) {
Chris Lattnereee57c02008-04-04 06:12:32 +0000426 return CastTo<Decl>(D);
427}
428
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000429DeclContext *DeclContext::FromDecl (const Decl *D) {
430 return CastTo<DeclContext>(D);
Chris Lattnereee57c02008-04-04 06:12:32 +0000431}
432
433//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000434// NamedDecl Implementation
435//===----------------------------------------------------------------------===//
436
Chris Lattner910435b2007-10-06 22:53:46 +0000437const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000438 if (const IdentifierInfo *II = getIdentifier())
439 return II->getName();
440 return "";
441}
442
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000443//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000444// FunctionDecl Implementation
445//===----------------------------------------------------------------------===//
446
Chris Lattner4b009652007-07-25 00:24:17 +0000447FunctionDecl::~FunctionDecl() {
448 delete[] ParamInfo;
Douglas Gregor42214c52008-04-21 02:02:58 +0000449}
450
Ted Kremenekafdf8112008-05-20 00:43:19 +0000451void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000452 if (Body)
453 Body->Destroy(C);
454
455 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
456 (*I)->Destroy(C);
457
Ted Kremenekafdf8112008-05-20 00:43:19 +0000458 Decl::Destroy(C);
459}
460
461
Douglas Gregor42214c52008-04-21 02:02:58 +0000462Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
463 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
464 if (FD->Body) {
465 Definition = FD;
466 return FD->Body;
467 }
468 }
469
470 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000471}
472
473unsigned FunctionDecl::getNumParams() const {
Chris Lattnere8733592008-04-06 23:09:52 +0000474 const FunctionType *FT = getType()->getAsFunctionType();
475 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000476 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000477 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000478}
479
480void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
481 assert(ParamInfo == 0 && "Already has param info!");
482 assert(NumParams == getNumParams() && "Parameter count mismatch!");
483
484 // Zero params -> null pointer.
485 if (NumParams) {
486 ParamInfo = new ParmVarDecl*[NumParams];
487 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
488 }
489}
490
Chris Lattner97316c02008-04-10 02:22:51 +0000491/// getMinRequiredArguments - Returns the minimum number of arguments
492/// needed to call this function. This may be fewer than the number of
493/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000494/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000495unsigned FunctionDecl::getMinRequiredArguments() const {
496 unsigned NumRequiredArgs = getNumParams();
497 while (NumRequiredArgs > 0
498 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
499 --NumRequiredArgs;
500
501 return NumRequiredArgs;
502}
503
Douglas Gregor42214c52008-04-21 02:02:58 +0000504/// AddRedeclaration - Specifies that this function declaration has been
505/// redeclared by the function declaration FD. FD must be a
506/// redeclaration of this based on the semantics of the language being
507/// translated ("compatible" function types in C, same signatures in
508/// C++).
509void FunctionDecl::AddRedeclaration(FunctionDecl *FD) {
510 assert(FD->PreviousDeclaration == 0 &&
511 "Redeclaration already has a previous declaration!");
512
513 // Insert FD into the list of previous declarations of this
514 // function.
515 FD->PreviousDeclaration = this->PreviousDeclaration;
516 this->PreviousDeclaration = FD;
517
518 // Swap the contents of this function declaration and FD. This
519 // effectively transforms the original declaration into the most
520 // recent declaration, so that all references to this declaration
521 // remain valid (and have information from *all* declarations),
522 // while retaining all of the information about previous
523 // declarations as well.
524
525 // Swap parameters, so that the most recent parameter names and
526 // exact types (e.g., enum vs int) show up in the original
527 // declaration.
Chris Lattner74bf5552008-05-04 02:29:49 +0000528 std::swap(this->ParamInfo, FD->ParamInfo);
Douglas Gregor42214c52008-04-21 02:02:58 +0000529
530 // Swap the function body: all declarations share the same function
531 // body, but we keep track of who actually defined that function
532 // body by keeping the pointer to the body stored in that node.
Chris Lattner74bf5552008-05-04 02:29:49 +0000533 std::swap(this->Body, FD->Body);
Douglas Gregor42214c52008-04-21 02:02:58 +0000534
535 // Swap type information: this is important because in C, later
536 // declarations can provide slightly different types (enum vs. int,
537 // for example).
538 QualType thisType = this->getType();
539 this->setType(FD->getType());
540 FD->setType(thisType);
541
542 // Swap location information: this allows us to produce diagnostics
543 // later on that reference the most recent declaration (which has
544 // the most information!) while retaining the location of previous
545 // declarations (good for "redefinition" diagnostics).
546 SourceLocation thisLocation = this->getLocation();
547 this->setLocation(FD->getLocation());
548 FD->setLocation(thisLocation);
549
550 // Swap attributes. FD will have the union of the attributes from
551 // all previous declarations.
Chris Lattner74bf5552008-05-04 02:29:49 +0000552 this->swapAttrs(FD);
Douglas Gregor42214c52008-04-21 02:02:58 +0000553
554 // If any declaration is inline, the function is inline.
555 this->IsInline |= FD->IsInline;
556
557 // FIXME: Is this the right way to handle storage specifiers?
558 if (FD->SClass) this->SClass = FD->SClass;
559}
560
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000561//===----------------------------------------------------------------------===//
562// RecordDecl Implementation
563//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000564
565/// defineBody - When created, RecordDecl's correspond to a forward declared
566/// record. This method is used to mark the decl as being defined, with the
567/// specified contents.
568void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
569 assert(!isDefinition() && "Cannot redefine record!");
570 setDefinition(true);
571 NumMembers = numMembers;
572 if (numMembers) {
573 Members = new FieldDecl*[numMembers];
574 memcpy(Members, members, numMembers*sizeof(Decl*));
575 }
576}
577
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000578FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Chris Lattner4b009652007-07-25 00:24:17 +0000579 if (Members == 0 || NumMembers < 0)
580 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000581
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000582 // Linear search. When C++ classes come along, will likely need to revisit.
583 for (int i = 0; i != NumMembers; ++i)
584 if (Members[i]->getIdentifier() == II)
Chris Lattner4b009652007-07-25 00:24:17 +0000585 return Members[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000586 return 0;
587}