blob: c75fb8b674dc1aa944113454559e3cd9ab79083b [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Anders Carlsson1a841062008-02-15 07:04:12 +000016#include "clang/AST/Attr.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Anders Carlssone5070062008-02-15 23:30:50 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000019
Chris Lattner6d9a6852006-10-25 05:11:20 +000020using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000021
Chris Lattner88f70d62008-03-15 05:43:15 +000022//===----------------------------------------------------------------------===//
23// Statistics
24//===----------------------------------------------------------------------===//
25
Steve Narofff84d11f2007-05-23 21:48:04 +000026// temporary statistics gathering
27static unsigned nFuncs = 0;
Steve Naroff08899ff2008-04-15 22:42:06 +000028static unsigned nVars = 0;
Steve Narofff84d11f2007-05-23 21:48:04 +000029static unsigned nParmVars = 0;
Chris Lattnera5490952007-05-24 01:09:39 +000030static unsigned nSUC = 0;
Steve Narofff84d11f2007-05-23 21:48:04 +000031static unsigned nEnumConst = 0;
32static unsigned nEnumDecls = 0;
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000033static unsigned nNamespaces = 0;
Steve Narofff84d11f2007-05-23 21:48:04 +000034static unsigned nTypedef = 0;
35static unsigned nFieldDecls = 0;
Steve Naroff09bf8152007-09-06 21:24:23 +000036static unsigned nInterfaceDecls = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000037static unsigned nClassDecls = 0;
38static unsigned nMethodDecls = 0;
39static unsigned nProtocolDecls = 0;
Fariborz Jahanian876e27d2007-09-21 15:40:54 +000040static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +000041static unsigned nCategoryDecls = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000042static unsigned nIvarDecls = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +000043static unsigned nObjCImplementationDecls = 0;
44static unsigned nObjCCategoryImpl = 0;
45static unsigned nObjCCompatibleAlias = 0;
46static unsigned nObjCPropertyDecl = 0;
Fariborz Jahanian549e83c2008-04-16 22:00:24 +000047static unsigned nObjCPropertyImplDecl = 0;
Chris Lattner38376f12008-01-12 07:05:38 +000048static unsigned nLinkageSpecDecl = 0;
Anders Carlsson5c6c0592008-02-08 00:33:21 +000049static unsigned nFileScopeAsmDecl = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000050
Steve Narofff84d11f2007-05-23 21:48:04 +000051static bool StatSwitch = false;
52
Anders Carlssone5070062008-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 Naroff83763f22007-09-17 14:49:06 +000059const char *Decl::getDeclKindName() const {
Steve Naroff2f742082007-09-16 16:16:00 +000060 switch (DeclKind) {
61 default: assert(0 && "Unknown decl kind!");
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +000062 case Namespace: return "Namespace";
Chris Lattner88f70d62008-03-15 05:43:15 +000063 case Typedef: return "Typedef";
64 case Function: return "Function";
Steve Naroff08899ff2008-04-15 22:42:06 +000065 case Var: return "Var";
Chris Lattner88f70d62008-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 Naroff2f742082007-09-16 16:16:00 +000077 }
78}
79
Chris Lattner88f70d62008-03-15 05:43:15 +000080bool Decl::CollectingStats(bool Enable) {
81 if (Enable)
82 StatSwitch = true;
83 return StatSwitch;
Steve Narofff84d11f2007-05-23 21:48:04 +000084}
85
86void Decl::PrintStats() {
Chris Lattnerfc234de2007-05-24 00:40:54 +000087 fprintf(stderr, "*** Decl Stats:\n");
88 fprintf(stderr, " %d decls total.\n",
Steve Naroff08899ff2008-04-15 22:42:06 +000089 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+
Chris Lattnereb85ab42008-02-25 21:04:36 +000090 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Argyrios Kyrtzidis08114892008-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 Lattnerfc234de2007-05-24 00:40:54 +000096 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +000097 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Steve Naroff08899ff2008-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 Lattnerfc234de2007-05-24 00:40:54 +0000101 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000102 nParmVars, (int)sizeof(ParmVarDecl),
103 int(nParmVars*sizeof(ParmVarDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +0000104 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000105 nFieldDecls, (int)sizeof(FieldDecl),
106 int(nFieldDecls*sizeof(FieldDecl)));
Chris Lattnera5490952007-05-24 01:09:39 +0000107 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000108 nSUC, (int)sizeof(RecordDecl),
109 int(nSUC*sizeof(RecordDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +0000110 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000111 nEnumDecls, (int)sizeof(EnumDecl),
112 int(nEnumDecls*sizeof(EnumDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +0000113 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000114 nEnumConst, (int)sizeof(EnumConstantDecl),
115 int(nEnumConst*sizeof(EnumConstantDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +0000116 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000117 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000118 // Objective-C decls...
119 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000120 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
121 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000122 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000123 nIvarDecls, (int)sizeof(ObjCIvarDecl),
124 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000125 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000126 nClassDecls, (int)sizeof(ObjCClassDecl),
127 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000128 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000129 nMethodDecls, (int)sizeof(ObjCMethodDecl),
130 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000131 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000132 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
133 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanian876e27d2007-09-21 15:40:54 +0000134 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000135 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
136 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000137 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000138 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
139 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000140
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +0000141 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000142 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
143 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +0000144
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000145 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000146 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
147 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000148
Fariborz Jahanian49c64252007-10-11 23:42:27 +0000149 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000150 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
151 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian49c64252007-10-11 23:42:27 +0000152
Fariborz Jahanianf76f2b02007-11-06 22:01:00 +0000153 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000154 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
155 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahanianf76f2b02007-11-06 22:01:00 +0000156
Fariborz Jahanian549e83c2008-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 Lattnerfc234de2007-05-24 00:40:54 +0000161 fprintf(stderr, "Total bytes = %d\n",
Steve Naroff08899ff2008-04-15 22:42:06 +0000162 int(nFuncs*sizeof(FunctionDecl)+
163 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
Chris Lattnereb85ab42008-02-25 21:04:36 +0000164 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
165 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
166 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian54e423182008-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 Jahanian549e83c2008-04-16 22:00:24 +0000178 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
Anders Carlsson5c6c0592008-02-08 00:33:21 +0000179 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000180 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
181 nNamespaces*sizeof(NamespaceDecl)));
Fariborz Jahanian54e423182008-01-23 01:34:33 +0000182
Steve Narofff84d11f2007-05-23 21:48:04 +0000183}
184
Chris Lattner88f70d62008-03-15 05:43:15 +0000185void Decl::addDeclKind(Kind k) {
Chris Lattnerfc234de2007-05-24 00:40:54 +0000186 switch (k) {
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000187 case Namespace: nNamespaces++; break;
Chris Lattner88f70d62008-03-15 05:43:15 +0000188 case Typedef: nTypedef++; break;
189 case Function: nFuncs++; break;
Steve Naroff08899ff2008-04-15 22:42:06 +0000190 case Var: nVars++; break;
Chris Lattner88f70d62008-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 Lattner59a25942008-03-31 00:36:02 +0000205 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
Sam Bishop506215c2008-04-08 20:49:25 +0000206 case ObjCProperty: nObjCPropertyDecl++; break;
Fariborz Jahanian549e83c2008-04-16 22:00:24 +0000207 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
Chris Lattner88f70d62008-03-15 05:43:15 +0000208 case LinkageSpec: nLinkageSpecDecl++; break;
209 case FileScopeAsm: nFileScopeAsmDecl++; break;
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +0000210 case TranslationUnit: break;
Chris Lattnerfc234de2007-05-24 00:40:54 +0000211 }
Steve Narofff84d11f2007-05-23 21:48:04 +0000212}
213
Chris Lattner88f70d62008-03-15 05:43:15 +0000214//===----------------------------------------------------------------------===//
Chris Lattnera7b32872008-03-15 06:12:44 +0000215// Decl Allocation/Deallocation Method Implementations
216//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000217
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +0000218TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
219 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
220 return new (Mem) TranslationUnitDecl();
221}
222
Argyrios Kyrtzidis08114892008-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 Lattnerbec41342008-04-22 18:39:57 +0000229VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff08899ff2008-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 Lattnerbec41342008-04-22 18:39:57 +0000234 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner4b08ca82008-03-15 21:10:16 +0000235}
236
Chris Lattnerbec41342008-04-22 18:39:57 +0000237ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000238 SourceLocation L, IdentifierInfo *Id,
239 QualType T, StorageClass S,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000240 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner4b08ca82008-03-15 21:10:16 +0000241 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +0000242 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner4b08ca82008-03-15 21:10:16 +0000243}
244
Chris Lattnerbec41342008-04-22 18:39:57 +0000245FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000246 SourceLocation L,
Chris Lattner5072bae2008-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 Lattnerbec41342008-04-22 18:39:57 +0000251 return new (Mem) FunctionDecl(DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattner5072bae2008-03-15 21:24:04 +0000252}
253
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000254FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000255 IdentifierInfo *Id, QualType T, Expr *BW) {
256 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000257 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattneree1284a2008-03-16 00:16:02 +0000258}
259
Chris Lattner5072bae2008-03-15 21:24:04 +0000260
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000261EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
262 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000263 IdentifierInfo *Id, QualType T,
264 Expr *E, const llvm::APSInt &V,
265 ScopedDecl *PrevDecl){
Chris Lattnera7b32872008-03-15 06:12:44 +0000266 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000267 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000268}
269
Chris Lattnerbec41342008-04-22 18:39:57 +0000270TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000271 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000272 IdentifierInfo *Id, QualType T,
273 ScopedDecl *PD) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000274 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +0000275 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnera7b32872008-03-15 06:12:44 +0000276}
277
Chris Lattnerbec41342008-04-22 18:39:57 +0000278EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000279 IdentifierInfo *Id,
Chris Lattner96c460d2008-03-15 21:32:50 +0000280 ScopedDecl *PrevDecl) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000281 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +0000282 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000283}
284
Chris Lattnerbec41342008-04-22 18:39:57 +0000285RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000286 SourceLocation L, IdentifierInfo *Id,
287 ScopedDecl *PrevDecl) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000288 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattnerbec41342008-04-22 18:39:57 +0000289 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000290}
291
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000292FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
293 SourceLocation L,
Chris Lattneree1284a2008-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 Lattnerc5ffed42008-04-04 06:12:32 +0000299LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
300 SourceLocation L,
Chris Lattneree1284a2008-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 Lattnera7b32872008-03-15 06:12:44 +0000305
306//===----------------------------------------------------------------------===//
Chris Lattner88f70d62008-03-15 05:43:15 +0000307// Decl Implementation
308//===----------------------------------------------------------------------===//
309
Chris Lattner6d9a6852006-10-25 05:11:20 +0000310// Out-of-line virtual method providing a home for Decl.
311Decl::~Decl() {
Anders Carlssonacea4152008-02-16 03:37:41 +0000312 if (!HasAttrs)
Anders Carlssone5070062008-02-15 23:30:50 +0000313 return;
314
315 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssonacea4152008-02-16 03:37:41 +0000316 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
317
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000318 // FIXME: Properly release attributes.
319 // delete it->second;
Anders Carlssonacea4152008-02-16 03:37:41 +0000320 DeclAttrs->erase(it);
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000321
Anders Carlssonacea4152008-02-16 03:37:41 +0000322 if (DeclAttrs->empty()) {
323 delete DeclAttrs;
324 DeclAttrs = 0;
325 }
Anders Carlssone5070062008-02-15 23:30:50 +0000326}
327
Chris Lattner88f70d62008-03-15 05:43:15 +0000328void Decl::addAttr(Attr *NewAttr) {
Anders Carlssone5070062008-02-15 23:30:50 +0000329 if (!DeclAttrs)
Chris Lattner89375192008-03-16 00:19:01 +0000330 DeclAttrs = new DeclAttrMapTy();
Anders Carlssone5070062008-02-15 23:30:50 +0000331
Chris Lattner88f70d62008-03-15 05:43:15 +0000332 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlssone5070062008-02-15 23:30:50 +0000333
Chris Lattner88f70d62008-03-15 05:43:15 +0000334 NewAttr->setNext(ExistingAttr);
335 ExistingAttr = NewAttr;
Anders Carlssone5070062008-02-15 23:30:50 +0000336
337 HasAttrs = true;
338}
339
Chris Lattner88f70d62008-03-15 05:43:15 +0000340const Attr *Decl::getAttrs() const {
Anders Carlssonacea4152008-02-16 03:37:41 +0000341 if (!HasAttrs)
Anders Carlssone5070062008-02-15 23:30:50 +0000342 return 0;
343
Anders Carlssonacea4152008-02-16 03:37:41 +0000344 return (*DeclAttrs)[this];
Chris Lattner6d9a6852006-10-25 05:11:20 +0000345}
Chris Lattner17ed4872006-11-20 04:58:19 +0000346
Chris Lattner7b9293b2008-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 Kremenekce20e8f2008-05-20 00:43:19 +0000372void Decl::Destroy(ASTContext& C) {
373 this->~Decl();
Sam Bishop703ae962008-04-11 15:01:25 +0000374 C.getAllocator().Deallocate((void *)this);
375}
376
Chris Lattner59a25942008-03-31 00:36:02 +0000377//===----------------------------------------------------------------------===//
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000378// DeclContext Implementation
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000379//===----------------------------------------------------------------------===//
380
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000381DeclContext *DeclContext::getParent() const {
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000382 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000383 return SD->getDeclContext();
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000384 else
385 return NULL;
386}
387
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000388Decl *DeclContext::ToDecl (const DeclContext *D) {
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000389 return CastTo<Decl>(D);
390}
391
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000392DeclContext *DeclContext::FromDecl (const Decl *D) {
393 return CastTo<DeclContext>(D);
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000394}
395
396//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000397// NamedDecl Implementation
398//===----------------------------------------------------------------------===//
399
Chris Lattnera4016552007-10-06 22:53:46 +0000400const char *NamedDecl::getName() const {
Chris Lattnerec040b12007-01-21 23:09:50 +0000401 if (const IdentifierInfo *II = getIdentifier())
402 return II->getName();
403 return "";
Chris Lattner17ed4872006-11-20 04:58:19 +0000404}
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000405
Chris Lattner59a25942008-03-31 00:36:02 +0000406//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000407// FunctionDecl Implementation
408//===----------------------------------------------------------------------===//
409
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000410FunctionDecl::~FunctionDecl() {
411 delete[] ParamInfo;
Douglas Gregor89f238c2008-04-21 02:02:58 +0000412}
413
Ted Kremenekce20e8f2008-05-20 00:43:19 +0000414void FunctionDecl::Destroy(ASTContext& C) {
Ted Kremenekfa8a09e2008-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 Kremenekce20e8f2008-05-20 00:43:19 +0000421 Decl::Destroy(C);
422}
423
424
Douglas Gregor89f238c2008-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 Lattnerc5cdf4d2007-01-21 07:42:07 +0000434}
435
436unsigned FunctionDecl::getNumParams() const {
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000437 const FunctionType *FT = getType()->getAsFunctionType();
438 if (isa<FunctionTypeNoProto>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000439 return 0;
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000440 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000441}
442
Chris Lattner53621a52007-06-13 20:44:40 +0000443void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000444 assert(ParamInfo == 0 && "Already has param info!");
445 assert(NumParams == getNumParams() && "Parameter count mismatch!");
446
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000447 // Zero params -> null pointer.
448 if (NumParams) {
Chris Lattner53621a52007-06-13 20:44:40 +0000449 ParamInfo = new ParmVarDecl*[NumParams];
450 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000451 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000452}
Chris Lattner41943152007-01-25 04:52:46 +0000453
Chris Lattner58258242008-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 Lattnerb0d38442008-04-12 23:52:44 +0000457/// arguments (in C++).
Chris Lattner58258242008-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 Gregor89f238c2008-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 Lattner7b9293b2008-05-04 02:29:49 +0000491 std::swap(this->ParamInfo, FD->ParamInfo);
Douglas Gregor89f238c2008-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 Lattner7b9293b2008-05-04 02:29:49 +0000496 std::swap(this->Body, FD->Body);
Douglas Gregor89f238c2008-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 Lattner7b9293b2008-05-04 02:29:49 +0000515 this->swapAttrs(FD);
Douglas Gregor89f238c2008-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 Lattner59a25942008-03-31 00:36:02 +0000524//===----------------------------------------------------------------------===//
525// RecordDecl Implementation
526//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +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.
Steve Naroffcc321422007-03-26 23:09:51 +0000531void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
Chris Lattner41943152007-01-25 04:52:46 +0000532 assert(!isDefinition() && "Cannot redefine record!");
533 setDefinition(true);
Chris Lattner5f521502007-01-25 06:27:24 +0000534 NumMembers = numMembers;
535 if (numMembers) {
Steve Naroffcc321422007-03-26 23:09:51 +0000536 Members = new FieldDecl*[numMembers];
Chris Lattner5f521502007-01-25 06:27:24 +0000537 memcpy(Members, members, numMembers*sizeof(Decl*));
Chris Lattner41943152007-01-25 04:52:46 +0000538 }
539}
Steve Naroffcc321422007-03-26 23:09:51 +0000540
Chris Lattner59a25942008-03-31 00:36:02 +0000541FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Steve Naroffcc321422007-03-26 23:09:51 +0000542 if (Members == 0 || NumMembers < 0)
543 return 0;
Fariborz Jahanian67341402007-10-04 00:45:27 +0000544
Chris Lattner59a25942008-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)
Steve Naroffcc321422007-03-26 23:09:51 +0000548 return Members[i];
Steve Naroffcc321422007-03-26 23:09:51 +0000549 return 0;
Chris Lattnerbd4de5df2007-07-12 15:43:07 +0000550}