blob: bad65cf84c62ea18c0f17a42fac4d3e9ce9fb037 [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 Lattnerc63e6602008-03-15 21:32:50 +0000207BlockVarDecl *BlockVarDecl::Create(ASTContext &C, SourceLocation L,
208 IdentifierInfo *Id, QualType T,
209 StorageClass S, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000210 void *Mem = C.getAllocator().Allocate<BlockVarDecl>();
211 return new (Mem) BlockVarDecl(L, Id, T, S, PrevDecl);
212}
213
214
Chris Lattnerc63e6602008-03-15 21:32:50 +0000215FileVarDecl *FileVarDecl::Create(ASTContext &C, SourceLocation L,
216 IdentifierInfo *Id, QualType T, StorageClass S,
217 ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000218 void *Mem = C.getAllocator().Allocate<FileVarDecl>();
219 return new (Mem) FileVarDecl(L, Id, T, S, PrevDecl);
220}
221
Chris Lattnerc63e6602008-03-15 21:32:50 +0000222ParmVarDecl *ParmVarDecl::Create(ASTContext &C, SourceLocation L,
223 IdentifierInfo *Id, QualType T, StorageClass S,
224 ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000225 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
226 return new (Mem) ParmVarDecl(L, Id, T, S, PrevDecl);
227}
228
Chris Lattnera98e58d2008-03-15 21:24:04 +0000229FunctionDecl *FunctionDecl::Create(ASTContext &C, SourceLocation L,
230 IdentifierInfo *Id, QualType T,
231 StorageClass S, bool isInline,
232 ScopedDecl *PrevDecl) {
233 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
234 return new (Mem) FunctionDecl(L, Id, T, S, isInline, PrevDecl);
235}
236
Chris Lattner8e25d862008-03-16 00:16:02 +0000237FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
238 IdentifierInfo *Id, QualType T, Expr *BW) {
239 void *Mem = C.getAllocator().Allocate<FieldDecl>();
240 return new (Mem) FieldDecl(L, Id, T, BW);
241}
242
Chris Lattnera98e58d2008-03-15 21:24:04 +0000243
Chris Lattnerc63e6602008-03-15 21:32:50 +0000244EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, SourceLocation L,
245 IdentifierInfo *Id, QualType T,
246 Expr *E, const llvm::APSInt &V,
247 ScopedDecl *PrevDecl){
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000248 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
249 return new (Mem) EnumConstantDecl(L, Id, T, E, V, PrevDecl);
250}
251
Chris Lattnerc63e6602008-03-15 21:32:50 +0000252TypedefDecl *TypedefDecl::Create(ASTContext &C, SourceLocation L,
253 IdentifierInfo *Id, QualType T,
254 ScopedDecl *PD) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000255 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
256 return new (Mem) TypedefDecl(L, Id, T, PD);
257}
258
Chris Lattnerc63e6602008-03-15 21:32:50 +0000259EnumDecl *EnumDecl::Create(ASTContext &C, SourceLocation L, IdentifierInfo *Id,
260 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000261 void *Mem = C.getAllocator().Allocate<EnumDecl>();
262 return new (Mem) EnumDecl(L, Id, PrevDecl);
263}
264
Chris Lattnerc63e6602008-03-15 21:32:50 +0000265RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, SourceLocation L,
266 IdentifierInfo *Id, ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000267 void *Mem = C.getAllocator().Allocate<RecordDecl>();
268 return new (Mem) RecordDecl(DK, L, Id, PrevDecl);
269}
270
Chris Lattner8e25d862008-03-16 00:16:02 +0000271FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, SourceLocation L,
272 StringLiteral *Str) {
273 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
274 return new (Mem) FileScopeAsmDecl(L, Str);
275}
276
277LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, SourceLocation L,
278 LanguageIDs Lang, Decl *D) {
279 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
280 return new (Mem) LinkageSpecDecl(L, Lang, D);
281}
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000282
283//===----------------------------------------------------------------------===//
Chris Lattnerd3b90652008-03-15 05:43:15 +0000284// Decl Implementation
285//===----------------------------------------------------------------------===//
286
Reid Spencer5f016e22007-07-11 17:01:13 +0000287// Out-of-line virtual method providing a home for Decl.
288Decl::~Decl() {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000289 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000290 return;
291
292 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000293 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
294
295 delete it->second;
296 DeclAttrs->erase(it);
297 if (DeclAttrs->empty()) {
298 delete DeclAttrs;
299 DeclAttrs = 0;
300 }
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000301}
302
Chris Lattnerd3b90652008-03-15 05:43:15 +0000303void Decl::addAttr(Attr *NewAttr) {
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000304 if (!DeclAttrs)
Chris Lattner1e03a562008-03-16 00:19:01 +0000305 DeclAttrs = new DeclAttrMapTy();
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000306
Chris Lattnerd3b90652008-03-15 05:43:15 +0000307 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000308
Chris Lattnerd3b90652008-03-15 05:43:15 +0000309 NewAttr->setNext(ExistingAttr);
310 ExistingAttr = NewAttr;
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000311
312 HasAttrs = true;
313}
314
Chris Lattnerd3b90652008-03-15 05:43:15 +0000315const Attr *Decl::getAttrs() const {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000316 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000317 return 0;
318
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000319 return (*DeclAttrs)[this];
Reid Spencer5f016e22007-07-11 17:01:13 +0000320}
321
Chris Lattner8a934232008-03-31 00:36:02 +0000322//===----------------------------------------------------------------------===//
323// NamedDecl Implementation
324//===----------------------------------------------------------------------===//
325
Chris Lattnerfd5de472007-10-06 22:53:46 +0000326const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 if (const IdentifierInfo *II = getIdentifier())
328 return II->getName();
329 return "";
330}
331
Chris Lattner8a934232008-03-31 00:36:02 +0000332//===----------------------------------------------------------------------===//
333// ScopedDecl Implementation
334//===----------------------------------------------------------------------===//
335
336// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
337// scoped decl is defined outside the current function or method. This is
338// roughly global variables and functions, but also handles enums (which could
339// be defined inside or outside a function etc).
340bool ScopedDecl::isDefinedOutsideFunctionOrMethod() const {
341 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
342 return VD->hasGlobalStorage();
343 if (isa<FunctionDecl>(this))
344 return true;
345
346 // FIXME: Why is ObjCCompatibleAlias a scopedecl?
347 if (isa<ObjCCompatibleAliasDecl>(this))
348 return true;
349
350 // FIXME: This needs to check the context the decl was defined in!
351 if (isa<TypeDecl>(this) || isa<EnumConstantDecl>(this))
352 return true;
353
354 assert(0 && "Unknown ScopedDecl!");
355 return false;
356}
357
358//===----------------------------------------------------------------------===//
359// FunctionDecl Implementation
360//===----------------------------------------------------------------------===//
361
Reid Spencer5f016e22007-07-11 17:01:13 +0000362FunctionDecl::~FunctionDecl() {
363 delete[] ParamInfo;
364}
365
366unsigned FunctionDecl::getNumParams() const {
Chris Lattnerd3b90652008-03-15 05:43:15 +0000367 if (isa<FunctionTypeNoProto>(getCanonicalType()))
368 return 0;
Chris Lattnerec584d62007-12-06 17:20:20 +0000369 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000370}
371
372void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
373 assert(ParamInfo == 0 && "Already has param info!");
374 assert(NumParams == getNumParams() && "Parameter count mismatch!");
375
376 // Zero params -> null pointer.
377 if (NumParams) {
378 ParamInfo = new ParmVarDecl*[NumParams];
379 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
380 }
381}
382
Chris Lattner8a934232008-03-31 00:36:02 +0000383//===----------------------------------------------------------------------===//
384// RecordDecl Implementation
385//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000386
387/// defineBody - When created, RecordDecl's correspond to a forward declared
388/// record. This method is used to mark the decl as being defined, with the
389/// specified contents.
390void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
391 assert(!isDefinition() && "Cannot redefine record!");
392 setDefinition(true);
393 NumMembers = numMembers;
394 if (numMembers) {
395 Members = new FieldDecl*[numMembers];
396 memcpy(Members, members, numMembers*sizeof(Decl*));
397 }
398}
399
Chris Lattner8a934232008-03-31 00:36:02 +0000400FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000401 if (Members == 0 || NumMembers < 0)
402 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000403
Chris Lattner8a934232008-03-31 00:36:02 +0000404 // Linear search. When C++ classes come along, will likely need to revisit.
405 for (int i = 0; i != NumMembers; ++i)
406 if (Members[i]->getIdentifier() == II)
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 return Members[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000409}