blob: c75fb8b674dc1aa944113454559e3cd9ab79083b [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
Chris Lattneref87a202008-04-22 18:39:57 +0000229VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000230 SourceLocation L,
231 IdentifierInfo *Id, QualType T,
232 StorageClass S, ScopedDecl *PrevDecl) {
233 void *Mem = C.getAllocator().Allocate<VarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000234 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +0000235}
236
Chris Lattneref87a202008-04-22 18:39:57 +0000237ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000238 SourceLocation L, IdentifierInfo *Id,
239 QualType T, StorageClass S,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000240 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner48d225c2008-03-15 21:10:16 +0000241 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000242 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +0000243}
244
Chris Lattneref87a202008-04-22 18:39:57 +0000245FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000246 SourceLocation L,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000247 IdentifierInfo *Id, QualType T,
248 StorageClass S, bool isInline,
249 ScopedDecl *PrevDecl) {
250 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000251 return new (Mem) FunctionDecl(DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattner4c7802b2008-03-15 21:24:04 +0000252}
253
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000254FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000255 IdentifierInfo *Id, QualType T, Expr *BW) {
256 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000257 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner81db64a2008-03-16 00:16:02 +0000258}
259
Chris Lattner4c7802b2008-03-15 21:24:04 +0000260
Chris Lattnereee57c02008-04-04 06:12:32 +0000261EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
262 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000263 IdentifierInfo *Id, QualType T,
264 Expr *E, const llvm::APSInt &V,
265 ScopedDecl *PrevDecl){
Chris Lattnere4650482008-03-15 06:12:44 +0000266 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +0000267 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000268}
269
Chris Lattneref87a202008-04-22 18:39:57 +0000270TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000271 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000272 IdentifierInfo *Id, QualType T,
273 ScopedDecl *PD) {
Chris Lattnere4650482008-03-15 06:12:44 +0000274 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000275 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnere4650482008-03-15 06:12:44 +0000276}
277
Chris Lattneref87a202008-04-22 18:39:57 +0000278EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000279 IdentifierInfo *Id,
Chris Lattner58114f02008-03-15 21:32:50 +0000280 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000281 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000282 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000283}
284
Chris Lattneref87a202008-04-22 18:39:57 +0000285RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000286 SourceLocation L, IdentifierInfo *Id,
287 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000288 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000289 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000290}
291
Chris Lattnereee57c02008-04-04 06:12:32 +0000292FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
293 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000294 StringLiteral *Str) {
295 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
296 return new (Mem) FileScopeAsmDecl(L, Str);
297}
298
Chris Lattnereee57c02008-04-04 06:12:32 +0000299LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
300 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000301 LanguageIDs Lang, Decl *D) {
302 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
303 return new (Mem) LinkageSpecDecl(L, Lang, D);
304}
Chris Lattnere4650482008-03-15 06:12:44 +0000305
306//===----------------------------------------------------------------------===//
Chris Lattnera8344c32008-03-15 05:43:15 +0000307// Decl Implementation
308//===----------------------------------------------------------------------===//
309
Chris Lattner4b009652007-07-25 00:24:17 +0000310// Out-of-line virtual method providing a home for Decl.
311Decl::~Decl() {
Anders Carlssonb610a992008-02-16 03:37:41 +0000312 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000313 return;
314
315 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssonb610a992008-02-16 03:37:41 +0000316 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
317
Ted Kremenekafdf8112008-05-20 00:43:19 +0000318 // FIXME: Properly release attributes.
319 // delete it->second;
Anders Carlssonb610a992008-02-16 03:37:41 +0000320 DeclAttrs->erase(it);
Ted Kremenekafdf8112008-05-20 00:43:19 +0000321
Anders Carlssonb610a992008-02-16 03:37:41 +0000322 if (DeclAttrs->empty()) {
323 delete DeclAttrs;
324 DeclAttrs = 0;
325 }
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000326}
327
Chris Lattnera8344c32008-03-15 05:43:15 +0000328void Decl::addAttr(Attr *NewAttr) {
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000329 if (!DeclAttrs)
Chris Lattner10318b82008-03-16 00:19:01 +0000330 DeclAttrs = new DeclAttrMapTy();
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000331
Chris Lattnera8344c32008-03-15 05:43:15 +0000332 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000333
Chris Lattnera8344c32008-03-15 05:43:15 +0000334 NewAttr->setNext(ExistingAttr);
335 ExistingAttr = NewAttr;
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000336
337 HasAttrs = true;
338}
339
Chris Lattnera8344c32008-03-15 05:43:15 +0000340const Attr *Decl::getAttrs() const {
Anders Carlssonb610a992008-02-16 03:37:41 +0000341 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000342 return 0;
343
Anders Carlssonb610a992008-02-16 03:37:41 +0000344 return (*DeclAttrs)[this];
Chris Lattner4b009652007-07-25 00:24:17 +0000345}
346
Chris Lattner74bf5552008-05-04 02:29:49 +0000347void Decl::swapAttrs(Decl *RHS) {
348 bool HasLHSAttr = this->HasAttrs;
349 bool HasRHSAttr = RHS->HasAttrs;
350
351 // Usually, neither decl has attrs, nothing to do.
352 if (!HasLHSAttr && !HasRHSAttr) return;
353
354 // If 'this' has no attrs, swap the other way.
355 if (!HasLHSAttr)
356 return RHS->swapAttrs(this);
357
358 // Handle the case when both decls have attrs.
359 if (HasRHSAttr) {
360 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
361 return;
362 }
363
364 // Otherwise, LHS has an attr and RHS doesn't.
365 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
366 (*DeclAttrs).erase(this);
367 this->HasAttrs = false;
368 RHS->HasAttrs = true;
369}
370
371
Ted Kremenekafdf8112008-05-20 00:43:19 +0000372void Decl::Destroy(ASTContext& C) {
373 this->~Decl();
Sam Bishop274366f2008-04-11 15:01:25 +0000374 C.getAllocator().Deallocate((void *)this);
375}
376
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000377//===----------------------------------------------------------------------===//
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000378// DeclContext Implementation
Chris Lattnereee57c02008-04-04 06:12:32 +0000379//===----------------------------------------------------------------------===//
380
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000381DeclContext *DeclContext::getParent() const {
Chris Lattnereee57c02008-04-04 06:12:32 +0000382 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000383 return SD->getDeclContext();
Chris Lattnereee57c02008-04-04 06:12:32 +0000384 else
385 return NULL;
386}
387
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000388Decl *DeclContext::ToDecl (const DeclContext *D) {
Chris Lattnereee57c02008-04-04 06:12:32 +0000389 return CastTo<Decl>(D);
390}
391
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000392DeclContext *DeclContext::FromDecl (const Decl *D) {
393 return CastTo<DeclContext>(D);
Chris Lattnereee57c02008-04-04 06:12:32 +0000394}
395
396//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000397// NamedDecl Implementation
398//===----------------------------------------------------------------------===//
399
Chris Lattner910435b2007-10-06 22:53:46 +0000400const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000401 if (const IdentifierInfo *II = getIdentifier())
402 return II->getName();
403 return "";
404}
405
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000406//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000407// FunctionDecl Implementation
408//===----------------------------------------------------------------------===//
409
Chris Lattner4b009652007-07-25 00:24:17 +0000410FunctionDecl::~FunctionDecl() {
411 delete[] ParamInfo;
Douglas Gregor42214c52008-04-21 02:02:58 +0000412}
413
Ted Kremenekafdf8112008-05-20 00:43:19 +0000414void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenek345b93d2008-05-20 03:56:00 +0000415 if (Body)
416 Body->Destroy(C);
417
418 for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I)
419 (*I)->Destroy(C);
420
Ted Kremenekafdf8112008-05-20 00:43:19 +0000421 Decl::Destroy(C);
422}
423
424
Douglas Gregor42214c52008-04-21 02:02:58 +0000425Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
426 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
427 if (FD->Body) {
428 Definition = FD;
429 return FD->Body;
430 }
431 }
432
433 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000434}
435
436unsigned FunctionDecl::getNumParams() const {
Chris Lattnere8733592008-04-06 23:09:52 +0000437 const FunctionType *FT = getType()->getAsFunctionType();
438 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000439 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000440 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000441}
442
443void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
444 assert(ParamInfo == 0 && "Already has param info!");
445 assert(NumParams == getNumParams() && "Parameter count mismatch!");
446
447 // Zero params -> null pointer.
448 if (NumParams) {
449 ParamInfo = new ParmVarDecl*[NumParams];
450 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
451 }
452}
453
Chris Lattner97316c02008-04-10 02:22:51 +0000454/// getMinRequiredArguments - Returns the minimum number of arguments
455/// needed to call this function. This may be fewer than the number of
456/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000457/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000458unsigned FunctionDecl::getMinRequiredArguments() const {
459 unsigned NumRequiredArgs = getNumParams();
460 while (NumRequiredArgs > 0
461 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
462 --NumRequiredArgs;
463
464 return NumRequiredArgs;
465}
466
Douglas Gregor42214c52008-04-21 02:02:58 +0000467/// AddRedeclaration - Specifies that this function declaration has been
468/// redeclared by the function declaration FD. FD must be a
469/// redeclaration of this based on the semantics of the language being
470/// translated ("compatible" function types in C, same signatures in
471/// C++).
472void FunctionDecl::AddRedeclaration(FunctionDecl *FD) {
473 assert(FD->PreviousDeclaration == 0 &&
474 "Redeclaration already has a previous declaration!");
475
476 // Insert FD into the list of previous declarations of this
477 // function.
478 FD->PreviousDeclaration = this->PreviousDeclaration;
479 this->PreviousDeclaration = FD;
480
481 // Swap the contents of this function declaration and FD. This
482 // effectively transforms the original declaration into the most
483 // recent declaration, so that all references to this declaration
484 // remain valid (and have information from *all* declarations),
485 // while retaining all of the information about previous
486 // declarations as well.
487
488 // Swap parameters, so that the most recent parameter names and
489 // exact types (e.g., enum vs int) show up in the original
490 // declaration.
Chris Lattner74bf5552008-05-04 02:29:49 +0000491 std::swap(this->ParamInfo, FD->ParamInfo);
Douglas Gregor42214c52008-04-21 02:02:58 +0000492
493 // Swap the function body: all declarations share the same function
494 // body, but we keep track of who actually defined that function
495 // body by keeping the pointer to the body stored in that node.
Chris Lattner74bf5552008-05-04 02:29:49 +0000496 std::swap(this->Body, FD->Body);
Douglas Gregor42214c52008-04-21 02:02:58 +0000497
498 // Swap type information: this is important because in C, later
499 // declarations can provide slightly different types (enum vs. int,
500 // for example).
501 QualType thisType = this->getType();
502 this->setType(FD->getType());
503 FD->setType(thisType);
504
505 // Swap location information: this allows us to produce diagnostics
506 // later on that reference the most recent declaration (which has
507 // the most information!) while retaining the location of previous
508 // declarations (good for "redefinition" diagnostics).
509 SourceLocation thisLocation = this->getLocation();
510 this->setLocation(FD->getLocation());
511 FD->setLocation(thisLocation);
512
513 // Swap attributes. FD will have the union of the attributes from
514 // all previous declarations.
Chris Lattner74bf5552008-05-04 02:29:49 +0000515 this->swapAttrs(FD);
Douglas Gregor42214c52008-04-21 02:02:58 +0000516
517 // If any declaration is inline, the function is inline.
518 this->IsInline |= FD->IsInline;
519
520 // FIXME: Is this the right way to handle storage specifiers?
521 if (FD->SClass) this->SClass = FD->SClass;
522}
523
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000524//===----------------------------------------------------------------------===//
525// RecordDecl Implementation
526//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000527
528/// defineBody - When created, RecordDecl's correspond to a forward declared
529/// record. This method is used to mark the decl as being defined, with the
530/// specified contents.
531void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
532 assert(!isDefinition() && "Cannot redefine record!");
533 setDefinition(true);
534 NumMembers = numMembers;
535 if (numMembers) {
536 Members = new FieldDecl*[numMembers];
537 memcpy(Members, members, numMembers*sizeof(Decl*));
538 }
539}
540
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000541FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Chris Lattner4b009652007-07-25 00:24:17 +0000542 if (Members == 0 || NumMembers < 0)
543 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000544
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000545 // Linear search. When C++ classes come along, will likely need to revisit.
546 for (int i = 0; i != NumMembers; ++i)
547 if (Members[i]->getIdentifier() == II)
Chris Lattner4b009652007-07-25 00:24:17 +0000548 return Members[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000549 return 0;
550}