blob: 8228eceebf6dc401ba6cdf9b73d519ae79cb5ede [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;
27static unsigned nBlockVars = 0;
28static unsigned nFileVars = 0;
29static unsigned nParmVars = 0;
30static unsigned nSUC = 0;
31static unsigned nEnumConst = 0;
32static unsigned nEnumDecls = 0;
33static 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;
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";
62 case BlockVar: return "BlockVar";
63 case FileVar: return "FileVar";
64 case ParmVar: return "ParmVar";
65 case EnumConstant: return "EnumConstant";
66 case ObjCInterface: return "ObjCInterface";
67 case ObjCClass: return "ObjCClass";
68 case ObjCMethod: return "ObjCMethod";
69 case ObjCProtocol: return "ObjCProtocol";
70 case ObjCForwardProtocol: return "ObjCForwardProtocol";
71 case Struct: return "Struct";
72 case Union: return "Union";
73 case Class: return "Class";
74 case Enum: return "Enum";
Steve Naroff8c9f13e2007-09-16 16:16:00 +000075 }
76}
77
Chris Lattnerd3b90652008-03-15 05:43:15 +000078bool Decl::CollectingStats(bool Enable) {
79 if (Enable)
80 StatSwitch = true;
81 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000082}
83
84void Decl::PrintStats() {
85 fprintf(stderr, "*** Decl Stats:\n");
86 fprintf(stderr, " %d decls total.\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000087 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
88 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
89 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Reid Spencer5f016e22007-07-11 17:01:13 +000090 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000091 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +000092 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000093 nBlockVars, (int)sizeof(BlockVarDecl),
94 int(nBlockVars*sizeof(BlockVarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +000095 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000096 nFileVars, (int)sizeof(FileVarDecl),
97 int(nFileVars*sizeof(FileVarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +000098 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000099 nParmVars, (int)sizeof(ParmVarDecl),
100 int(nParmVars*sizeof(ParmVarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000102 nFieldDecls, (int)sizeof(FieldDecl),
103 int(nFieldDecls*sizeof(FieldDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000105 nSUC, (int)sizeof(RecordDecl),
106 int(nSUC*sizeof(RecordDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000108 nEnumDecls, (int)sizeof(EnumDecl),
109 int(nEnumDecls*sizeof(EnumDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000111 nEnumConst, (int)sizeof(EnumConstantDecl),
112 int(nEnumConst*sizeof(EnumConstantDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000114 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000115 // Objective-C decls...
116 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000117 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
118 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000119 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000120 nIvarDecls, (int)sizeof(ObjCIvarDecl),
121 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000122 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000123 nClassDecls, (int)sizeof(ObjCClassDecl),
124 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000125 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000126 nMethodDecls, (int)sizeof(ObjCMethodDecl),
127 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000128 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000129 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
130 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000131 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000132 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
133 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000134 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000135 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
136 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000137
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000138 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000139 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
140 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000141
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000142 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000143 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
144 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000145
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000146 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000147 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
148 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000149
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000150 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000151 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
152 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000153
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 fprintf(stderr, "Total bytes = %d\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000155 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
156 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
157 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)+
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000171 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
172 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)));
Fariborz Jahanian269b10d2008-01-23 01:34:33 +0000173
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Chris Lattnerd3b90652008-03-15 05:43:15 +0000176void Decl::addDeclKind(Kind k) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 switch (k) {
Chris Lattnerd3b90652008-03-15 05:43:15 +0000178 case Typedef: nTypedef++; break;
179 case Function: nFuncs++; break;
180 case BlockVar: nBlockVars++; break;
181 case FileVar: nFileVars++; break;
182 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;
Chris Lattnerd3b90652008-03-15 05:43:15 +0000197 case PropertyDecl: nObjCPropertyDecl++; break;
198 case LinkageSpec: nLinkageSpecDecl++; break;
199 case FileScopeAsm: nFileScopeAsmDecl++; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 }
201}
202
Chris Lattnerd3b90652008-03-15 05:43:15 +0000203//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000204// Decl Allocation/Deallocation Method Implementations
205//===----------------------------------------------------------------------===//
206
Chris Lattnerb048c982008-04-06 04:47:34 +0000207BlockVarDecl *BlockVarDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000208 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000209 IdentifierInfo *Id, QualType T,
210 StorageClass S, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000211 void *Mem = C.getAllocator().Allocate<BlockVarDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000212 return new (Mem) BlockVarDecl(CD, L, Id, T, S, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +0000213}
214
215
Chris Lattnerb048c982008-04-06 04:47:34 +0000216FileVarDecl *FileVarDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000217 SourceLocation L, IdentifierInfo *Id,
218 QualType T, StorageClass S,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000219 ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000220 void *Mem = C.getAllocator().Allocate<FileVarDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000221 return new (Mem) FileVarDecl(CD, L, Id, T, S, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +0000222}
223
Chris Lattnerb048c982008-04-06 04:47:34 +0000224ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000225 SourceLocation L, IdentifierInfo *Id,
226 QualType T, StorageClass S,
Chris Lattner04421082008-04-08 04:40:51 +0000227 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000228 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattner04421082008-04-08 04:40:51 +0000229 return new (Mem) ParmVarDecl(CD, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner9e151e12008-03-15 21:10:16 +0000230}
231
Chris Lattnerb048c982008-04-06 04:47:34 +0000232FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000233 SourceLocation L,
Chris Lattnera98e58d2008-03-15 21:24:04 +0000234 IdentifierInfo *Id, QualType T,
235 StorageClass S, bool isInline,
236 ScopedDecl *PrevDecl) {
237 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000238 return new (Mem) FunctionDecl(CD, L, Id, T, S, isInline, PrevDecl);
Chris Lattnera98e58d2008-03-15 21:24:04 +0000239}
240
Chris Lattnerb048c982008-04-06 04:47:34 +0000241FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000242 IdentifierInfo *Id, QualType T, Expr *BW) {
243 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerb048c982008-04-06 04:47:34 +0000244 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner8e25d862008-03-16 00:16:02 +0000245}
246
Chris Lattnera98e58d2008-03-15 21:24:04 +0000247
Chris Lattner0ed844b2008-04-04 06:12:32 +0000248EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
249 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000250 IdentifierInfo *Id, QualType T,
251 Expr *E, const llvm::APSInt &V,
252 ScopedDecl *PrevDecl){
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000253 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000254 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000255}
256
Chris Lattnerb048c982008-04-06 04:47:34 +0000257TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000258 SourceLocation L,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000259 IdentifierInfo *Id, QualType T,
260 ScopedDecl *PD) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000261 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000262 return new (Mem) TypedefDecl(CD, L, Id, T, PD);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000263}
264
Chris Lattnerb048c982008-04-06 04:47:34 +0000265EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *CD, SourceLocation L,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000266 IdentifierInfo *Id,
Chris Lattnerc63e6602008-03-15 21:32:50 +0000267 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000268 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000269 return new (Mem) EnumDecl(CD, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000270}
271
Chris Lattnerb048c982008-04-06 04:47:34 +0000272RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *CD,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000273 SourceLocation L, IdentifierInfo *Id,
274 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000275 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000276 return new (Mem) RecordDecl(DK, CD, L, Id, PrevDecl);
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000277}
278
Chris Lattner0ed844b2008-04-04 06:12:32 +0000279FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
280 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000281 StringLiteral *Str) {
282 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
283 return new (Mem) FileScopeAsmDecl(L, Str);
284}
285
Chris Lattner0ed844b2008-04-04 06:12:32 +0000286LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
287 SourceLocation L,
Chris Lattner8e25d862008-03-16 00:16:02 +0000288 LanguageIDs Lang, Decl *D) {
289 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
290 return new (Mem) LinkageSpecDecl(L, Lang, D);
291}
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000292
293//===----------------------------------------------------------------------===//
Chris Lattnerd3b90652008-03-15 05:43:15 +0000294// Decl Implementation
295//===----------------------------------------------------------------------===//
296
Reid Spencer5f016e22007-07-11 17:01:13 +0000297// Out-of-line virtual method providing a home for Decl.
298Decl::~Decl() {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000299 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000300 return;
301
302 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000303 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
304
305 delete it->second;
306 DeclAttrs->erase(it);
307 if (DeclAttrs->empty()) {
308 delete DeclAttrs;
309 DeclAttrs = 0;
310 }
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000311}
312
Chris Lattnerd3b90652008-03-15 05:43:15 +0000313void Decl::addAttr(Attr *NewAttr) {
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000314 if (!DeclAttrs)
Chris Lattner1e03a562008-03-16 00:19:01 +0000315 DeclAttrs = new DeclAttrMapTy();
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000316
Chris Lattnerd3b90652008-03-15 05:43:15 +0000317 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000318
Chris Lattnerd3b90652008-03-15 05:43:15 +0000319 NewAttr->setNext(ExistingAttr);
320 ExistingAttr = NewAttr;
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000321
322 HasAttrs = true;
323}
324
Chris Lattnerd3b90652008-03-15 05:43:15 +0000325const Attr *Decl::getAttrs() const {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000326 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000327 return 0;
328
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000329 return (*DeclAttrs)[this];
Reid Spencer5f016e22007-07-11 17:01:13 +0000330}
331
Chris Lattner8a934232008-03-31 00:36:02 +0000332//===----------------------------------------------------------------------===//
Chris Lattnerb048c982008-04-06 04:47:34 +0000333// DeclContext Implementation
Chris Lattner0ed844b2008-04-04 06:12:32 +0000334//===----------------------------------------------------------------------===//
335
Chris Lattnerb048c982008-04-06 04:47:34 +0000336DeclContext *DeclContext::getParent() const {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000337 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Chris Lattnerb048c982008-04-06 04:47:34 +0000338 return SD->getDeclContext();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000339 else
340 return NULL;
341}
342
Chris Lattnerb048c982008-04-06 04:47:34 +0000343Decl *DeclContext::ToDecl (const DeclContext *D) {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000344 return CastTo<Decl>(D);
345}
346
Chris Lattnerb048c982008-04-06 04:47:34 +0000347DeclContext *DeclContext::FromDecl (const Decl *D) {
348 return CastTo<DeclContext>(D);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000349}
350
351//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000352// NamedDecl Implementation
353//===----------------------------------------------------------------------===//
354
Chris Lattnerfd5de472007-10-06 22:53:46 +0000355const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 if (const IdentifierInfo *II = getIdentifier())
357 return II->getName();
358 return "";
359}
360
Chris Lattner8a934232008-03-31 00:36:02 +0000361//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +0000362// FunctionDecl Implementation
363//===----------------------------------------------------------------------===//
364
Reid Spencer5f016e22007-07-11 17:01:13 +0000365FunctionDecl::~FunctionDecl() {
366 delete[] ParamInfo;
Sam Bishop8266d7f2008-04-03 05:01:04 +0000367 delete Body;
Reid Spencer5f016e22007-07-11 17:01:13 +0000368}
369
370unsigned FunctionDecl::getNumParams() const {
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000371 const FunctionType *FT = getType()->getAsFunctionType();
372 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +0000373 return 0;
Chris Lattnerd8bdba52008-04-06 23:09:52 +0000374 return cast<FunctionTypeProto>(FT)->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000375}
376
377void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
378 assert(ParamInfo == 0 && "Already has param info!");
379 assert(NumParams == getNumParams() && "Parameter count mismatch!");
380
381 // Zero params -> null pointer.
382 if (NumParams) {
383 ParamInfo = new ParmVarDecl*[NumParams];
384 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
385 }
386}
387
Chris Lattner8a934232008-03-31 00:36:02 +0000388//===----------------------------------------------------------------------===//
389// RecordDecl Implementation
390//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000391
392/// defineBody - When created, RecordDecl's correspond to a forward declared
393/// record. This method is used to mark the decl as being defined, with the
394/// specified contents.
395void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
396 assert(!isDefinition() && "Cannot redefine record!");
397 setDefinition(true);
398 NumMembers = numMembers;
399 if (numMembers) {
400 Members = new FieldDecl*[numMembers];
401 memcpy(Members, members, numMembers*sizeof(Decl*));
402 }
403}
404
Chris Lattner8a934232008-03-31 00:36:02 +0000405FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 if (Members == 0 || NumMembers < 0)
407 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000408
Chris Lattner8a934232008-03-31 00:36:02 +0000409 // Linear search. When C++ classes come along, will likely need to revisit.
410 for (int i = 0; i != NumMembers; ++i)
411 if (Members[i]->getIdentifier() == II)
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 return Members[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000414}