blob: 3c7401feab1773404b442293e967f357ca2335fa [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Anders Carlssonf78915f2008-02-15 07:04:12 +000016#include "clang/AST/Attr.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Anders Carlssonb0dd2682008-02-15 23:30:50 +000018#include "llvm/ADT/DenseMap.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Chris Lattnerd3b90652008-03-15 05:43:15 +000021//===----------------------------------------------------------------------===//
22// Statistics
23//===----------------------------------------------------------------------===//
24
Reid Spencer5f016e22007-07-11 17:01:13 +000025// temporary statistics gathering
26static unsigned nFuncs = 0;
Steve Naroff248a7532008-04-15 22:42:06 +000027static unsigned nVars = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000028static unsigned nParmVars = 0;
29static unsigned nSUC = 0;
30static unsigned nEnumConst = 0;
31static unsigned nEnumDecls = 0;
32static unsigned nTypedef = 0;
33static unsigned nFieldDecls = 0;
Steve Naroff3536b442007-09-06 21:24:23 +000034static unsigned nInterfaceDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000035static unsigned nClassDecls = 0;
36static unsigned nMethodDecls = 0;
37static unsigned nProtocolDecls = 0;
Fariborz Jahanian894c57f2007-09-21 15:40:54 +000038static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000039static unsigned nCategoryDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000040static unsigned nIvarDecls = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000041static unsigned nObjCImplementationDecls = 0;
42static unsigned nObjCCategoryImpl = 0;
43static unsigned nObjCCompatibleAlias = 0;
44static unsigned nObjCPropertyDecl = 0;
Fariborz Jahanian61d46152008-04-16 22:00:24 +000045static unsigned nObjCPropertyImplDecl = 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +000046static unsigned nLinkageSpecDecl = 0;
Anders Carlssondfab6cb2008-02-08 00:33:21 +000047static unsigned nFileScopeAsmDecl = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000048
Reid Spencer5f016e22007-07-11 17:01:13 +000049static bool StatSwitch = false;
50
Anders Carlssonb0dd2682008-02-15 23:30:50 +000051// This keeps track of all decl attributes. Since so few decls have attrs, we
52// keep them in a hash map instead of wasting space in the Decl class.
53typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
54
55static DeclAttrMapTy *DeclAttrs = 0;
56
Steve Naroffe5ea3802007-09-17 14:49:06 +000057const char *Decl::getDeclKindName() const {
Steve Naroff8c9f13e2007-09-16 16:16:00 +000058 switch (DeclKind) {
59 default: assert(0 && "Unknown decl kind!");
Chris Lattnerd3b90652008-03-15 05:43:15 +000060 case Typedef: return "Typedef";
61 case Function: return "Function";
Steve Naroff248a7532008-04-15 22:42:06 +000062 case Var: return "Var";
Chris Lattnerd3b90652008-03-15 05:43:15 +000063 case ParmVar: return "ParmVar";
64 case EnumConstant: return "EnumConstant";
65 case ObjCInterface: return "ObjCInterface";
66 case ObjCClass: return "ObjCClass";
67 case ObjCMethod: return "ObjCMethod";
68 case ObjCProtocol: return "ObjCProtocol";
69 case ObjCForwardProtocol: return "ObjCForwardProtocol";
70 case Struct: return "Struct";
71 case Union: return "Union";
72 case Class: return "Class";
73 case Enum: return "Enum";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000074 }
75}
76
Chris Lattnerd3b90652008-03-15 05:43:15 +000077bool Decl::CollectingStats(bool Enable) {
78 if (Enable)
79 StatSwitch = true;
80 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
82
83void Decl::PrintStats() {
84 fprintf(stderr, "*** Decl Stats:\n");
85 fprintf(stderr, " %d decls total.\n",
Steve Naroff248a7532008-04-15 22:42:06 +000086 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+
Chris Lattnerc81c8142008-02-25 21:04:36 +000087 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
88 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Reid Spencer5f016e22007-07-11 17:01:13 +000089 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000090 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Steve Naroff248a7532008-04-15 22:42:06 +000091 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
92 nVars, (int)sizeof(VarDecl),
93 int(nVars*sizeof(VarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +000094 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000095 nParmVars, (int)sizeof(ParmVarDecl),
96 int(nParmVars*sizeof(ParmVarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +000097 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000098 nFieldDecls, (int)sizeof(FieldDecl),
99 int(nFieldDecls*sizeof(FieldDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000101 nSUC, (int)sizeof(RecordDecl),
102 int(nSUC*sizeof(RecordDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000104 nEnumDecls, (int)sizeof(EnumDecl),
105 int(nEnumDecls*sizeof(EnumDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000107 nEnumConst, (int)sizeof(EnumConstantDecl),
108 int(nEnumConst*sizeof(EnumConstantDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000110 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000111 // Objective-C decls...
112 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000113 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
114 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000115 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000116 nIvarDecls, (int)sizeof(ObjCIvarDecl),
117 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000118 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000119 nClassDecls, (int)sizeof(ObjCClassDecl),
120 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000121 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000122 nMethodDecls, (int)sizeof(ObjCMethodDecl),
123 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000124 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000125 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
126 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000127 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000128 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
129 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000130 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000131 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
132 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000133
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000134 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000135 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
136 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000137
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000138 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000139 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
140 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000141
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000142 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000143 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
144 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000145
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000146 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000147 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
148 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000149
Fariborz Jahanian61d46152008-04-16 22:00:24 +0000150 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
151 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
152 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
153
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 fprintf(stderr, "Total bytes = %d\n",
Steve Naroff248a7532008-04-15 22:42:06 +0000155 int(nFuncs*sizeof(FunctionDecl)+
156 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
Chris Lattnerc81c8142008-02-25 21:04:36 +0000157 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
158 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
159 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian269b10d2008-01-23 01:34:33 +0000160 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
161 nIvarDecls*sizeof(ObjCIvarDecl)+
162 nClassDecls*sizeof(ObjCClassDecl)+
163 nMethodDecls*sizeof(ObjCMethodDecl)+
164 nProtocolDecls*sizeof(ObjCProtocolDecl)+
165 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
166 nCategoryDecls*sizeof(ObjCCategoryDecl)+
167 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
168 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
169 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
170 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
Fariborz Jahanian61d46152008-04-16 22:00:24 +0000171 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000172 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
173 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)));
Fariborz Jahanian269b10d2008-01-23 01:34:33 +0000174
Reid Spencer5f016e22007-07-11 17:01:13 +0000175}
176
Chris Lattnerd3b90652008-03-15 05:43:15 +0000177void Decl::addDeclKind(Kind k) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 switch (k) {
Chris Lattnerd3b90652008-03-15 05:43:15 +0000179 case Typedef: nTypedef++; break;
180 case Function: nFuncs++; break;
Steve Naroff248a7532008-04-15 22:42:06 +0000181 case Var: nVars++; break;
Chris Lattnerd3b90652008-03-15 05:43:15 +0000182 case ParmVar: nParmVars++; break;
183 case EnumConstant: nEnumConst++; break;
184 case Field: nFieldDecls++; break;
185 case Struct: case Union: case Class: nSUC++; break;
186 case Enum: nEnumDecls++; break;
187 case ObjCInterface: nInterfaceDecls++; break;
188 case ObjCClass: nClassDecls++; break;
189 case ObjCMethod: nMethodDecls++; break;
190 case ObjCProtocol: nProtocolDecls++; break;
191 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
192 case ObjCCategory: nCategoryDecls++; break;
193 case ObjCIvar: nIvarDecls++; break;
194 case ObjCImplementation: nObjCImplementationDecls++; break;
195 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
Chris Lattner8a934232008-03-31 00:36:02 +0000196 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
Sam Bishop670aa9d2008-04-08 20:49:25 +0000197 case ObjCProperty: nObjCPropertyDecl++; break;
Fariborz Jahanian61d46152008-04-16 22:00:24 +0000198 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
Chris Lattnerd3b90652008-03-15 05:43:15 +0000199 case LinkageSpec: nLinkageSpecDecl++; break;
200 case FileScopeAsm: nFileScopeAsmDecl++; break;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000201 case TranslationUnit: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 }
203}
204
Chris Lattnerd3b90652008-03-15 05:43:15 +0000205//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000206// Decl Allocation/Deallocation Method Implementations
207//===----------------------------------------------------------------------===//
208
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000209TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
210 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
211 return new (Mem) TranslationUnitDecl();
212}
213
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000214VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff248a7532008-04-15 22:42:06 +0000215 SourceLocation L,
216 IdentifierInfo *Id, QualType T,
217 StorageClass S, ScopedDecl *PrevDecl) {
218 void *Mem = C.getAllocator().Allocate<VarDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000219 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +0000220}
221
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000222ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000223 SourceLocation L, IdentifierInfo *Id,
224 QualType T, StorageClass S,
Chris Lattner04421082008-04-08 04:40:51 +0000225 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000226 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000227 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +0000228}
229
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000230FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000231 SourceLocation L,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000232 IdentifierInfo *Id, QualType T,
233 StorageClass S, bool isInline,
234 ScopedDecl *PrevDecl) {
235 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000236 return new (Mem) FunctionDecl(DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattnera98e58d2008-03-15 21:24:04 +0000237}
238
Chris Lattnerb048c982008-04-06 04:47:34 +0000239FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000240 IdentifierInfo *Id, QualType T, Expr *BW) {
241 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerb048c982008-04-06 04:47:34 +0000242 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner8e25d862008-03-16 00:16:02 +0000243}
244
Chris Lattnera98e58d2008-03-15 21:24:04 +0000245
Chris Lattner0ed844b2008-04-04 06:12:32 +0000246EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
247 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000248 IdentifierInfo *Id, QualType T,
249 Expr *E, const llvm::APSInt &V,
250 ScopedDecl *PrevDecl){
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000251 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000252 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000253}
254
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000255TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000256 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000257 IdentifierInfo *Id, QualType T,
258 ScopedDecl *PD) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000259 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000260 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000261}
262
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000263EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000264 IdentifierInfo *Id,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000265 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000266 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000267 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000268}
269
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000270RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *DC,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000271 SourceLocation L, IdentifierInfo *Id,
272 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000273 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattner9fdf9c62008-04-22 18:39:57 +0000274 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000275}
276
Chris Lattner0ed844b2008-04-04 06:12:32 +0000277FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
278 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000279 StringLiteral *Str) {
280 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
281 return new (Mem) FileScopeAsmDecl(L, Str);
282}
283
Chris Lattner0ed844b2008-04-04 06:12:32 +0000284LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
285 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000286 LanguageIDs Lang, Decl *D) {
287 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
288 return new (Mem) LinkageSpecDecl(L, Lang, D);
289}
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000290
291//===----------------------------------------------------------------------===//
Chris Lattnerd3b90652008-03-15 05:43:15 +0000292// Decl Implementation
293//===----------------------------------------------------------------------===//
294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295// Out-of-line virtual method providing a home for Decl.
296Decl::~Decl() {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000297 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000298 return;
299
300 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000301 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
302
303 delete it->second;
304 DeclAttrs->erase(it);
305 if (DeclAttrs->empty()) {
306 delete DeclAttrs;
307 DeclAttrs = 0;
308 }
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000309}
310
Chris Lattnerd3b90652008-03-15 05:43:15 +0000311void Decl::addAttr(Attr *NewAttr) {
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000312 if (!DeclAttrs)
Chris Lattner1e03a562008-03-16 00:19:01 +0000313 DeclAttrs = new DeclAttrMapTy();
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000314
Chris Lattnerd3b90652008-03-15 05:43:15 +0000315 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000316
Chris Lattnerd3b90652008-03-15 05:43:15 +0000317 NewAttr->setNext(ExistingAttr);
318 ExistingAttr = NewAttr;
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000319
320 HasAttrs = true;
321}
322
Chris Lattnerd3b90652008-03-15 05:43:15 +0000323const Attr *Decl::getAttrs() const {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000324 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000325 return 0;
326
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000327 return (*DeclAttrs)[this];
Reid Spencer5f016e22007-07-11 17:01:13 +0000328}
329
Sam Bishop1b30cb22008-04-13 04:32:18 +0000330#define CASE(KIND) \
331 case KIND: \
332 static_cast<KIND##Decl *>(const_cast<Decl *>(this))->~KIND##Decl(); \
333 break
Sam Bishop1bb19632008-04-11 18:04:39 +0000334
Sam Bishopbb45c512008-04-11 15:01:25 +0000335void Decl::Destroy(ASTContext& C) const {
Sam Bishop1bb19632008-04-11 18:04:39 +0000336 switch (getKind()) {
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000337 CASE(TranslationUnit);
Sam Bishop1bb19632008-04-11 18:04:39 +0000338 CASE(Field);
339 CASE(ObjCIvar);
340 CASE(ObjCCategory);
341 CASE(ObjCCategoryImpl);
342 CASE(ObjCImplementation);
343 CASE(ObjCProtocol);
344 CASE(ObjCProperty);
345 CASE(Typedef);
346 CASE(Enum);
347 CASE(EnumConstant);
348 CASE(Function);
Steve Naroff248a7532008-04-15 22:42:06 +0000349 CASE(Var);
Sam Bishop1bb19632008-04-11 18:04:39 +0000350 CASE(ParmVar);
351 CASE(ObjCInterface);
352 CASE(ObjCCompatibleAlias);
353 CASE(ObjCMethod);
354 CASE(ObjCClass);
355 CASE(ObjCForwardProtocol);
356 CASE(LinkageSpec);
357
358 case Struct: case Union: case Class:
Sam Bishop1b30cb22008-04-13 04:32:18 +0000359 static_cast<RecordDecl *>(const_cast<Decl *>(this))->~RecordDecl();
Sam Bishop1bb19632008-04-11 18:04:39 +0000360 break;
361
362 default: assert(0 && "Unknown decl kind!");
363 }
364
Sam Bishopbb45c512008-04-11 15:01:25 +0000365 C.getAllocator().Deallocate((void *)this);
366}
367
Sam Bishop1bb19632008-04-11 18:04:39 +0000368#undef CASE
369
Chris Lattner8a934232008-03-31 00:36:02 +0000370//===----------------------------------------------------------------------===//
Chris Lattnerb048c982008-04-06 04:47:34 +0000371// DeclContext Implementation
Chris Lattner0ed844b2008-04-04 06:12:32 +0000372//===----------------------------------------------------------------------===//
373
Chris Lattnerb048c982008-04-06 04:47:34 +0000374DeclContext *DeclContext::getParent() const {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000375 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Chris Lattnerb048c982008-04-06 04:47:34 +0000376 return SD->getDeclContext();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000377 else
378 return NULL;
379}
380
Chris Lattnerb048c982008-04-06 04:47:34 +0000381Decl *DeclContext::ToDecl (const DeclContext *D) {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000382 return CastTo<Decl>(D);
383}
384
Chris Lattnerb048c982008-04-06 04:47:34 +0000385DeclContext *DeclContext::FromDecl (const Decl *D) {
386 return CastTo<DeclContext>(D);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000387}
388
389//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000390// NamedDecl Implementation
391//===----------------------------------------------------------------------===//
392
Chris Lattnerfd5de472007-10-06 22:53:46 +0000393const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 if (const IdentifierInfo *II = getIdentifier())
395 return II->getName();
396 return "";
397}
398
Chris Lattner8a934232008-03-31 00:36:02 +0000399//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000400// FunctionDecl Implementation
401//===----------------------------------------------------------------------===//
402
Reid Spencer5f016e22007-07-11 17:01:13 +0000403FunctionDecl::~FunctionDecl() {
404 delete[] ParamInfo;
Sam Bishop8266d7f2008-04-03 05:01:04 +0000405 delete Body;
Douglas Gregorf0097952008-04-21 02:02:58 +0000406 delete PreviousDeclaration;
407}
408
409Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
410 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
411 if (FD->Body) {
412 Definition = FD;
413 return FD->Body;
414 }
415 }
416
417 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000418}
419
420unsigned FunctionDecl::getNumParams() const {
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000421 const FunctionType *FT = getType()->getAsFunctionType();
422 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000423 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000424 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000425}
426
427void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
428 assert(ParamInfo == 0 && "Already has param info!");
429 assert(NumParams == getNumParams() && "Parameter count mismatch!");
430
431 // Zero params -> null pointer.
432 if (NumParams) {
433 ParamInfo = new ParmVarDecl*[NumParams];
434 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
435 }
436}
437
Chris Lattner8123a952008-04-10 02:22:51 +0000438/// getMinRequiredArguments - Returns the minimum number of arguments
439/// needed to call this function. This may be fewer than the number of
440/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +0000441/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +0000442unsigned FunctionDecl::getMinRequiredArguments() const {
443 unsigned NumRequiredArgs = getNumParams();
444 while (NumRequiredArgs > 0
445 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
446 --NumRequiredArgs;
447
448 return NumRequiredArgs;
449}
450
Douglas Gregorf0097952008-04-21 02:02:58 +0000451/// AddRedeclaration - Specifies that this function declaration has been
452/// redeclared by the function declaration FD. FD must be a
453/// redeclaration of this based on the semantics of the language being
454/// translated ("compatible" function types in C, same signatures in
455/// C++).
456void FunctionDecl::AddRedeclaration(FunctionDecl *FD) {
457 assert(FD->PreviousDeclaration == 0 &&
458 "Redeclaration already has a previous declaration!");
459
460 // Insert FD into the list of previous declarations of this
461 // function.
462 FD->PreviousDeclaration = this->PreviousDeclaration;
463 this->PreviousDeclaration = FD;
464
465 // Swap the contents of this function declaration and FD. This
466 // effectively transforms the original declaration into the most
467 // recent declaration, so that all references to this declaration
468 // remain valid (and have information from *all* declarations),
469 // while retaining all of the information about previous
470 // declarations as well.
471
472 // Swap parameters, so that the most recent parameter names and
473 // exact types (e.g., enum vs int) show up in the original
474 // declaration.
475 ParmVarDecl **thisParamInfo = this->ParamInfo;
476 this->ParamInfo = FD->ParamInfo;
477 FD->ParamInfo = thisParamInfo;
478
479 // Swap the function body: all declarations share the same function
480 // body, but we keep track of who actually defined that function
481 // body by keeping the pointer to the body stored in that node.
482 Stmt *thisBody = this->Body;
483 this->Body = FD->Body;
484 FD->Body = thisBody;
485
486 // Swap type information: this is important because in C, later
487 // declarations can provide slightly different types (enum vs. int,
488 // for example).
489 QualType thisType = this->getType();
490 this->setType(FD->getType());
491 FD->setType(thisType);
492
493 // Swap location information: this allows us to produce diagnostics
494 // later on that reference the most recent declaration (which has
495 // the most information!) while retaining the location of previous
496 // declarations (good for "redefinition" diagnostics).
497 SourceLocation thisLocation = this->getLocation();
498 this->setLocation(FD->getLocation());
499 FD->setLocation(thisLocation);
500
501 // Swap attributes. FD will have the union of the attributes from
502 // all previous declarations.
503 if (DeclAttrs) {
504 Attr *thisAttr = (*DeclAttrs)[this];
505 (*DeclAttrs)[this] = (*DeclAttrs)[FD];
506 (*DeclAttrs)[FD] = thisAttr;
507 }
508
509 // If any declaration is inline, the function is inline.
510 this->IsInline |= FD->IsInline;
511
512 // FIXME: Is this the right way to handle storage specifiers?
513 if (FD->SClass) this->SClass = FD->SClass;
514}
515
Chris Lattner8a934232008-03-31 00:36:02 +0000516//===----------------------------------------------------------------------===//
517// RecordDecl Implementation
518//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000519
520/// defineBody - When created, RecordDecl's correspond to a forward declared
521/// record. This method is used to mark the decl as being defined, with the
522/// specified contents.
523void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
524 assert(!isDefinition() && "Cannot redefine record!");
525 setDefinition(true);
526 NumMembers = numMembers;
527 if (numMembers) {
528 Members = new FieldDecl*[numMembers];
529 memcpy(Members, members, numMembers*sizeof(Decl*));
530 }
531}
532
Chris Lattner8a934232008-03-31 00:36:02 +0000533FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 if (Members == 0 || NumMembers < 0)
535 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000536
Chris Lattner8a934232008-03-31 00:36:02 +0000537 // Linear search. When C++ classes come along, will likely need to revisit.
538 for (int i = 0; i != NumMembers; ++i)
539 if (Members[i]->getIdentifier() == II)
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 return Members[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000542}