blob: 1a7eecac32ad2a0be7024049529988ffeb969e0e [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;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000032static unsigned nNamespaces = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000033static unsigned nTypedef = 0;
34static unsigned nFieldDecls = 0;
Steve Naroff3536b442007-09-06 21:24:23 +000035static unsigned nInterfaceDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000036static unsigned nClassDecls = 0;
37static unsigned nMethodDecls = 0;
38static unsigned nProtocolDecls = 0;
Fariborz Jahanian894c57f2007-09-21 15:40:54 +000039static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000040static unsigned nCategoryDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000041static unsigned nIvarDecls = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000042static unsigned nObjCImplementationDecls = 0;
43static unsigned nObjCCategoryImpl = 0;
44static unsigned nObjCCompatibleAlias = 0;
45static unsigned nObjCPropertyDecl = 0;
Fariborz Jahanian61d46152008-04-16 22:00:24 +000046static unsigned nObjCPropertyImplDecl = 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +000047static unsigned nLinkageSpecDecl = 0;
Anders Carlssondfab6cb2008-02-08 00:33:21 +000048static unsigned nFileScopeAsmDecl = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050static bool StatSwitch = false;
51
Anders Carlssonb0dd2682008-02-15 23:30:50 +000052// This keeps track of all decl attributes. Since so few decls have attrs, we
53// keep them in a hash map instead of wasting space in the Decl class.
54typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
55
56static DeclAttrMapTy *DeclAttrs = 0;
57
Steve Naroffe5ea3802007-09-17 14:49:06 +000058const char *Decl::getDeclKindName() const {
Steve Naroff8c9f13e2007-09-16 16:16:00 +000059 switch (DeclKind) {
60 default: assert(0 && "Unknown decl kind!");
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000061 case Namespace: return "Namespace";
Chris Lattnerd3b90652008-03-15 05:43:15 +000062 case Typedef: return "Typedef";
63 case Function: return "Function";
Steve Naroff248a7532008-04-15 22:42:06 +000064 case Var: return "Var";
Chris Lattnerd3b90652008-03-15 05:43:15 +000065 case ParmVar: return "ParmVar";
66 case EnumConstant: return "EnumConstant";
67 case ObjCInterface: return "ObjCInterface";
68 case ObjCClass: return "ObjCClass";
69 case ObjCMethod: return "ObjCMethod";
70 case ObjCProtocol: return "ObjCProtocol";
71 case ObjCForwardProtocol: return "ObjCForwardProtocol";
72 case Struct: return "Struct";
73 case Union: return "Union";
74 case Class: return "Class";
75 case Enum: return "Enum";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000076 }
77}
78
Chris Lattnerd3b90652008-03-15 05:43:15 +000079bool Decl::CollectingStats(bool Enable) {
80 if (Enable)
81 StatSwitch = true;
82 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000083}
84
85void Decl::PrintStats() {
86 fprintf(stderr, "*** Decl Stats:\n");
87 fprintf(stderr, " %d decls total.\n",
Steve Naroff248a7532008-04-15 22:42:06 +000088 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+
Chris Lattnerc81c8142008-02-25 21:04:36 +000089 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000090 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
91 nNamespaces));
92 fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n",
93 nNamespaces, (int)sizeof(NamespaceDecl),
94 int(nNamespaces*sizeof(NamespaceDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +000095 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000096 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Steve Naroff248a7532008-04-15 22:42:06 +000097 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
98 nVars, (int)sizeof(VarDecl),
99 int(nVars*sizeof(VarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000101 nParmVars, (int)sizeof(ParmVarDecl),
102 int(nParmVars*sizeof(ParmVarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000104 nFieldDecls, (int)sizeof(FieldDecl),
105 int(nFieldDecls*sizeof(FieldDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000107 nSUC, (int)sizeof(RecordDecl),
108 int(nSUC*sizeof(RecordDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000110 nEnumDecls, (int)sizeof(EnumDecl),
111 int(nEnumDecls*sizeof(EnumDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000113 nEnumConst, (int)sizeof(EnumConstantDecl),
114 int(nEnumConst*sizeof(EnumConstantDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000116 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000117 // Objective-C decls...
118 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000119 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
120 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000121 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000122 nIvarDecls, (int)sizeof(ObjCIvarDecl),
123 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000124 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000125 nClassDecls, (int)sizeof(ObjCClassDecl),
126 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000127 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000128 nMethodDecls, (int)sizeof(ObjCMethodDecl),
129 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000130 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000131 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
132 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000133 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000134 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
135 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000136 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000137 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
138 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000139
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000140 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000141 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
142 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000143
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000144 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000145 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
146 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000147
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000148 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000149 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
150 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000151
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000152 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000153 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
154 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000155
Fariborz Jahanian61d46152008-04-16 22:00:24 +0000156 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
157 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
158 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
159
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 fprintf(stderr, "Total bytes = %d\n",
Steve Naroff248a7532008-04-15 22:42:06 +0000161 int(nFuncs*sizeof(FunctionDecl)+
162 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
Chris Lattnerc81c8142008-02-25 21:04:36 +0000163 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
164 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
165 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian269b10d2008-01-23 01:34:33 +0000166 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
167 nIvarDecls*sizeof(ObjCIvarDecl)+
168 nClassDecls*sizeof(ObjCClassDecl)+
169 nMethodDecls*sizeof(ObjCMethodDecl)+
170 nProtocolDecls*sizeof(ObjCProtocolDecl)+
171 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
172 nCategoryDecls*sizeof(ObjCCategoryDecl)+
173 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
174 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
175 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
176 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
Fariborz Jahanian61d46152008-04-16 22:00:24 +0000177 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000178 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000179 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
180 nNamespaces*sizeof(NamespaceDecl)));
Fariborz Jahanian269b10d2008-01-23 01:34:33 +0000181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182}
183
Chris Lattnerd3b90652008-03-15 05:43:15 +0000184void Decl::addDeclKind(Kind k) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 switch (k) {
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000186 case Namespace: nNamespaces++; break;
Chris Lattnerd3b90652008-03-15 05:43:15 +0000187 case Typedef: nTypedef++; break;
188 case Function: nFuncs++; break;
Steve Naroff248a7532008-04-15 22:42:06 +0000189 case Var: nVars++; break;
Chris Lattnerd3b90652008-03-15 05:43:15 +0000190 case ParmVar: nParmVars++; break;
191 case EnumConstant: nEnumConst++; break;
192 case Field: nFieldDecls++; break;
193 case Struct: case Union: case Class: nSUC++; break;
194 case Enum: nEnumDecls++; break;
195 case ObjCInterface: nInterfaceDecls++; break;
196 case ObjCClass: nClassDecls++; break;
197 case ObjCMethod: nMethodDecls++; break;
198 case ObjCProtocol: nProtocolDecls++; break;
199 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
200 case ObjCCategory: nCategoryDecls++; break;
201 case ObjCIvar: nIvarDecls++; break;
202 case ObjCImplementation: nObjCImplementationDecls++; break;
203 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
Chris Lattner8a934232008-03-31 00:36:02 +0000204 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
Sam Bishop670aa9d2008-04-08 20:49:25 +0000205 case ObjCProperty: nObjCPropertyDecl++; break;
Fariborz Jahanian61d46152008-04-16 22:00:24 +0000206 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
Chris Lattnerd3b90652008-03-15 05:43:15 +0000207 case LinkageSpec: nLinkageSpecDecl++; break;
208 case FileScopeAsm: nFileScopeAsmDecl++; break;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000209 case TranslationUnit: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 }
211}
212
Chris Lattnerd3b90652008-03-15 05:43:15 +0000213//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000214// Decl Allocation/Deallocation Method Implementations
215//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000216
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000217TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
218 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
219 return new (Mem) TranslationUnitDecl();
220}
221
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000222NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
223 SourceLocation L, IdentifierInfo *Id) {
224 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
225 return new (Mem) NamespaceDecl(DC, L, Id);
226}
227
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000228VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff248a7532008-04-15 22:42:06 +0000229 SourceLocation L,
230 IdentifierInfo *Id, QualType T,
231 StorageClass S, ScopedDecl *PrevDecl) {
232 void *Mem = C.getAllocator().Allocate<VarDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000233 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +0000234}
235
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000236ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000237 SourceLocation L, IdentifierInfo *Id,
238 QualType T, StorageClass S,
Chris Lattner04421082008-04-08 04:40:51 +0000239 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000240 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000241 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +0000242}
243
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000244FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000245 SourceLocation L,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000246 IdentifierInfo *Id, QualType T,
247 StorageClass S, bool isInline,
248 ScopedDecl *PrevDecl) {
249 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000250 return new (Mem) FunctionDecl(DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattnera98e58d2008-03-15 21:24:04 +0000251}
252
Chris Lattnerb048c982008-04-06 04:47:34 +0000253FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000254 IdentifierInfo *Id, QualType T, Expr *BW) {
255 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerb048c982008-04-06 04:47:34 +0000256 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner8e25d862008-03-16 00:16:02 +0000257}
258
Chris Lattnera98e58d2008-03-15 21:24:04 +0000259
Chris Lattner0ed844b2008-04-04 06:12:32 +0000260EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
261 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000262 IdentifierInfo *Id, QualType T,
263 Expr *E, const llvm::APSInt &V,
264 ScopedDecl *PrevDecl){
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000265 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000266 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000267}
268
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000269TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000270 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000271 IdentifierInfo *Id, QualType T,
272 ScopedDecl *PD) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000273 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000274 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000275}
276
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000277EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000278 IdentifierInfo *Id,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000279 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000280 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000281 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000282}
283
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000284RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000285 SourceLocation L, IdentifierInfo *Id,
286 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000287 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000288 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000289}
290
Chris Lattner0ed844b2008-04-04 06:12:32 +0000291FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
292 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000293 StringLiteral *Str) {
294 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
295 return new (Mem) FileScopeAsmDecl(L, Str);
296}
297
Chris Lattner0ed844b2008-04-04 06:12:32 +0000298LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
299 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000300 LanguageIDs Lang, Decl *D) {
301 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
302 return new (Mem) LinkageSpecDecl(L, Lang, D);
303}
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000304
305//===----------------------------------------------------------------------===//
Chris Lattnerd3b90652008-03-15 05:43:15 +0000306// Decl Implementation
307//===----------------------------------------------------------------------===//
308
Reid Spencer5f016e22007-07-11 17:01:13 +0000309// Out-of-line virtual method providing a home for Decl.
310Decl::~Decl() {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000311 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000312 return;
313
314 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000315 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
316
317 delete it->second;
318 DeclAttrs->erase(it);
319 if (DeclAttrs->empty()) {
320 delete DeclAttrs;
321 DeclAttrs = 0;
322 }
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000323}
324
Chris Lattnerd3b90652008-03-15 05:43:15 +0000325void Decl::addAttr(Attr *NewAttr) {
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000326 if (!DeclAttrs)
Chris Lattner1e03a562008-03-16 00:19:01 +0000327 DeclAttrs = new DeclAttrMapTy();
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000328
Chris Lattnerd3b90652008-03-15 05:43:15 +0000329 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000330
Chris Lattnerd3b90652008-03-15 05:43:15 +0000331 NewAttr->setNext(ExistingAttr);
332 ExistingAttr = NewAttr;
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000333
334 HasAttrs = true;
335}
336
Chris Lattnerd3b90652008-03-15 05:43:15 +0000337const Attr *Decl::getAttrs() const {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000338 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000339 return 0;
340
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000341 return (*DeclAttrs)[this];
Reid Spencer5f016e22007-07-11 17:01:13 +0000342}
343
Chris Lattnera212c562008-05-04 02:29:49 +0000344void Decl::swapAttrs(Decl *RHS) {
345 bool HasLHSAttr = this->HasAttrs;
346 bool HasRHSAttr = RHS->HasAttrs;
347
348 // Usually, neither decl has attrs, nothing to do.
349 if (!HasLHSAttr && !HasRHSAttr) return;
350
351 // If 'this' has no attrs, swap the other way.
352 if (!HasLHSAttr)
353 return RHS->swapAttrs(this);
354
355 // Handle the case when both decls have attrs.
356 if (HasRHSAttr) {
357 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
358 return;
359 }
360
361 // Otherwise, LHS has an attr and RHS doesn't.
362 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
363 (*DeclAttrs).erase(this);
364 this->HasAttrs = false;
365 RHS->HasAttrs = true;
366}
367
368
Sam Bishop1b30cb22008-04-13 04:32:18 +0000369#define CASE(KIND) \
370 case KIND: \
371 static_cast<KIND##Decl *>(const_cast<Decl *>(this))->~KIND##Decl(); \
372 break
Sam Bishop1bb19632008-04-11 18:04:39 +0000373
Sam Bishopbb45c512008-04-11 15:01:25 +0000374void Decl::Destroy(ASTContext& C) const {
Sam Bishop1bb19632008-04-11 18:04:39 +0000375 switch (getKind()) {
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000376 CASE(TranslationUnit);
Sam Bishop1bb19632008-04-11 18:04:39 +0000377 CASE(Field);
378 CASE(ObjCIvar);
379 CASE(ObjCCategory);
380 CASE(ObjCCategoryImpl);
381 CASE(ObjCImplementation);
382 CASE(ObjCProtocol);
383 CASE(ObjCProperty);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000384 CASE(Namespace);
Sam Bishop1bb19632008-04-11 18:04:39 +0000385 CASE(Typedef);
386 CASE(Enum);
387 CASE(EnumConstant);
388 CASE(Function);
Steve Naroff248a7532008-04-15 22:42:06 +0000389 CASE(Var);
Sam Bishop1bb19632008-04-11 18:04:39 +0000390 CASE(ParmVar);
391 CASE(ObjCInterface);
392 CASE(ObjCCompatibleAlias);
393 CASE(ObjCMethod);
394 CASE(ObjCClass);
395 CASE(ObjCForwardProtocol);
396 CASE(LinkageSpec);
397
398 case Struct: case Union: case Class:
Sam Bishop1b30cb22008-04-13 04:32:18 +0000399 static_cast<RecordDecl *>(const_cast<Decl *>(this))->~RecordDecl();
Sam Bishop1bb19632008-04-11 18:04:39 +0000400 break;
401
402 default: assert(0 && "Unknown decl kind!");
403 }
404
Sam Bishopbb45c512008-04-11 15:01:25 +0000405 C.getAllocator().Deallocate((void *)this);
406}
407
Sam Bishop1bb19632008-04-11 18:04:39 +0000408#undef CASE
409
Chris Lattner8a934232008-03-31 00:36:02 +0000410//===----------------------------------------------------------------------===//
Chris Lattnerb048c982008-04-06 04:47:34 +0000411// DeclContext Implementation
Chris Lattner0ed844b2008-04-04 06:12:32 +0000412//===----------------------------------------------------------------------===//
413
Chris Lattnerb048c982008-04-06 04:47:34 +0000414DeclContext *DeclContext::getParent() const {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000415 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Chris Lattnerb048c982008-04-06 04:47:34 +0000416 return SD->getDeclContext();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000417 else
418 return NULL;
419}
420
Chris Lattnerb048c982008-04-06 04:47:34 +0000421Decl *DeclContext::ToDecl (const DeclContext *D) {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000422 return CastTo<Decl>(D);
423}
424
Chris Lattnerb048c982008-04-06 04:47:34 +0000425DeclContext *DeclContext::FromDecl (const Decl *D) {
426 return CastTo<DeclContext>(D);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000427}
428
429//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000430// NamedDecl Implementation
431//===----------------------------------------------------------------------===//
432
Chris Lattnerfd5de472007-10-06 22:53:46 +0000433const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 if (const IdentifierInfo *II = getIdentifier())
435 return II->getName();
436 return "";
437}
438
Chris Lattner8a934232008-03-31 00:36:02 +0000439//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000440// FunctionDecl Implementation
441//===----------------------------------------------------------------------===//
442
Reid Spencer5f016e22007-07-11 17:01:13 +0000443FunctionDecl::~FunctionDecl() {
444 delete[] ParamInfo;
Sam Bishop8266d7f2008-04-03 05:01:04 +0000445 delete Body;
Douglas Gregorf0097952008-04-21 02:02:58 +0000446 delete PreviousDeclaration;
447}
448
449Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
450 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
451 if (FD->Body) {
452 Definition = FD;
453 return FD->Body;
454 }
455 }
456
457 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000458}
459
460unsigned FunctionDecl::getNumParams() const {
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000461 const FunctionType *FT = getType()->getAsFunctionType();
462 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000463 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000464 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000465}
466
467void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
468 assert(ParamInfo == 0 && "Already has param info!");
469 assert(NumParams == getNumParams() && "Parameter count mismatch!");
470
471 // Zero params -> null pointer.
472 if (NumParams) {
473 ParamInfo = new ParmVarDecl*[NumParams];
474 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
475 }
476}
477
Chris Lattner8123a952008-04-10 02:22:51 +0000478/// getMinRequiredArguments - Returns the minimum number of arguments
479/// needed to call this function. This may be fewer than the number of
480/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000481/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000482unsigned FunctionDecl::getMinRequiredArguments() const {
483 unsigned NumRequiredArgs = getNumParams();
484 while (NumRequiredArgs > 0
485 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
486 --NumRequiredArgs;
487
488 return NumRequiredArgs;
489}
490
Douglas Gregorf0097952008-04-21 02:02:58 +0000491/// AddRedeclaration - Specifies that this function declaration has been
492/// redeclared by the function declaration FD. FD must be a
493/// redeclaration of this based on the semantics of the language being
494/// translated ("compatible" function types in C, same signatures in
495/// C++).
496void FunctionDecl::AddRedeclaration(FunctionDecl *FD) {
497 assert(FD->PreviousDeclaration == 0 &&
498 "Redeclaration already has a previous declaration!");
499
500 // Insert FD into the list of previous declarations of this
501 // function.
502 FD->PreviousDeclaration = this->PreviousDeclaration;
503 this->PreviousDeclaration = FD;
504
505 // Swap the contents of this function declaration and FD. This
506 // effectively transforms the original declaration into the most
507 // recent declaration, so that all references to this declaration
508 // remain valid (and have information from *all* declarations),
509 // while retaining all of the information about previous
510 // declarations as well.
511
512 // Swap parameters, so that the most recent parameter names and
513 // exact types (e.g., enum vs int) show up in the original
514 // declaration.
Chris Lattnera212c562008-05-04 02:29:49 +0000515 std::swap(this->ParamInfo, FD->ParamInfo);
Douglas Gregorf0097952008-04-21 02:02:58 +0000516
517 // Swap the function body: all declarations share the same function
518 // body, but we keep track of who actually defined that function
519 // body by keeping the pointer to the body stored in that node.
Chris Lattnera212c562008-05-04 02:29:49 +0000520 std::swap(this->Body, FD->Body);
Douglas Gregorf0097952008-04-21 02:02:58 +0000521
522 // Swap type information: this is important because in C, later
523 // declarations can provide slightly different types (enum vs. int,
524 // for example).
525 QualType thisType = this->getType();
526 this->setType(FD->getType());
527 FD->setType(thisType);
528
529 // Swap location information: this allows us to produce diagnostics
530 // later on that reference the most recent declaration (which has
531 // the most information!) while retaining the location of previous
532 // declarations (good for "redefinition" diagnostics).
533 SourceLocation thisLocation = this->getLocation();
534 this->setLocation(FD->getLocation());
535 FD->setLocation(thisLocation);
536
537 // Swap attributes. FD will have the union of the attributes from
538 // all previous declarations.
Chris Lattnera212c562008-05-04 02:29:49 +0000539 this->swapAttrs(FD);
Douglas Gregorf0097952008-04-21 02:02:58 +0000540
541 // If any declaration is inline, the function is inline.
542 this->IsInline |= FD->IsInline;
543
544 // FIXME: Is this the right way to handle storage specifiers?
545 if (FD->SClass) this->SClass = FD->SClass;
546}
547
Chris Lattner8a934232008-03-31 00:36:02 +0000548//===----------------------------------------------------------------------===//
549// RecordDecl Implementation
550//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000551
552/// defineBody - When created, RecordDecl's correspond to a forward declared
553/// record. This method is used to mark the decl as being defined, with the
554/// specified contents.
555void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
556 assert(!isDefinition() && "Cannot redefine record!");
557 setDefinition(true);
558 NumMembers = numMembers;
559 if (numMembers) {
560 Members = new FieldDecl*[numMembers];
561 memcpy(Members, members, numMembers*sizeof(Decl*));
562 }
563}
564
Chris Lattner8a934232008-03-31 00:36:02 +0000565FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 if (Members == 0 || NumMembers < 0)
567 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000568
Chris Lattner8a934232008-03-31 00:36:02 +0000569 // Linear search. When C++ classes come along, will likely need to revisit.
570 for (int i = 0; i != NumMembers; ++i)
571 if (Members[i]->getIdentifier() == II)
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 return Members[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000574}