blob: afe4fcd37f13e9d7a97ec63829b8549ca3c3ffd7 [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"
Chris Lattner6d9a6852006-10-25 05:11:20 +000019using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000020
Chris Lattner88f70d62008-03-15 05:43:15 +000021//===----------------------------------------------------------------------===//
22// Statistics
23//===----------------------------------------------------------------------===//
24
Steve Narofff84d11f2007-05-23 21:48:04 +000025// temporary statistics gathering
26static unsigned nFuncs = 0;
27static unsigned nBlockVars = 0;
28static unsigned nFileVars = 0;
29static 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;
33static unsigned nTypedef = 0;
34static unsigned nFieldDecls = 0;
Steve Naroff09bf8152007-09-06 21:24:23 +000035static unsigned nInterfaceDecls = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000036static unsigned nClassDecls = 0;
37static unsigned nMethodDecls = 0;
38static unsigned nProtocolDecls = 0;
Fariborz Jahanian876e27d2007-09-21 15:40:54 +000039static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +000040static unsigned nCategoryDecls = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000041static unsigned nIvarDecls = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +000042static unsigned nObjCImplementationDecls = 0;
43static unsigned nObjCCategoryImpl = 0;
44static unsigned nObjCCompatibleAlias = 0;
45static unsigned nObjCPropertyDecl = 0;
Chris Lattner38376f12008-01-12 07:05:38 +000046static unsigned nLinkageSpecDecl = 0;
Anders Carlsson5c6c0592008-02-08 00:33:21 +000047static unsigned nFileScopeAsmDecl = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000048
Steve Narofff84d11f2007-05-23 21:48:04 +000049static bool StatSwitch = false;
50
Anders Carlssone5070062008-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 Naroff83763f22007-09-17 14:49:06 +000057const char *Decl::getDeclKindName() const {
Steve Naroff2f742082007-09-16 16:16:00 +000058 switch (DeclKind) {
59 default: assert(0 && "Unknown decl kind!");
Chris Lattner88f70d62008-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 Naroff2f742082007-09-16 16:16:00 +000075 }
76}
77
Chris Lattner88f70d62008-03-15 05:43:15 +000078bool Decl::CollectingStats(bool Enable) {
79 if (Enable)
80 StatSwitch = true;
81 return StatSwitch;
Steve Narofff84d11f2007-05-23 21:48:04 +000082}
83
84void Decl::PrintStats() {
Chris Lattnerfc234de2007-05-24 00:40:54 +000085 fprintf(stderr, "*** Decl Stats:\n");
86 fprintf(stderr, " %d decls total.\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +000087 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
88 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
89 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattnerfc234de2007-05-24 00:40:54 +000090 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +000091 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +000092 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +000093 nBlockVars, (int)sizeof(BlockVarDecl),
94 int(nBlockVars*sizeof(BlockVarDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +000095 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +000096 nFileVars, (int)sizeof(FileVarDecl),
97 int(nFileVars*sizeof(FileVarDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +000098 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +000099 nParmVars, (int)sizeof(ParmVarDecl),
100 int(nParmVars*sizeof(ParmVarDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +0000101 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000102 nFieldDecls, (int)sizeof(FieldDecl),
103 int(nFieldDecls*sizeof(FieldDecl)));
Chris Lattnera5490952007-05-24 01:09:39 +0000104 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000105 nSUC, (int)sizeof(RecordDecl),
106 int(nSUC*sizeof(RecordDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +0000107 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000108 nEnumDecls, (int)sizeof(EnumDecl),
109 int(nEnumDecls*sizeof(EnumDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +0000110 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000111 nEnumConst, (int)sizeof(EnumConstantDecl),
112 int(nEnumConst*sizeof(EnumConstantDecl)));
Chris Lattnerfc234de2007-05-24 00:40:54 +0000113 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000114 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000115 // Objective-C decls...
116 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000117 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
118 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000119 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000120 nIvarDecls, (int)sizeof(ObjCIvarDecl),
121 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000122 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000123 nClassDecls, (int)sizeof(ObjCClassDecl),
124 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000125 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000126 nMethodDecls, (int)sizeof(ObjCMethodDecl),
127 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000128 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000129 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
130 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanian876e27d2007-09-21 15:40:54 +0000131 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000132 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
133 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000134 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000135 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
136 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff73d534a2007-09-17 14:16:13 +0000137
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +0000138 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000139 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
140 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +0000141
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000142 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000143 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
144 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +0000145
Fariborz Jahanian49c64252007-10-11 23:42:27 +0000146 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000147 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
148 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian49c64252007-10-11 23:42:27 +0000149
Fariborz Jahanianf76f2b02007-11-06 22:01:00 +0000150 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattnereb85ab42008-02-25 21:04:36 +0000151 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
152 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahanianf76f2b02007-11-06 22:01:00 +0000153
Chris Lattnerfc234de2007-05-24 00:40:54 +0000154 fprintf(stderr, "Total bytes = %d\n",
Chris Lattnereb85ab42008-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 Jahanian54e423182008-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 Carlsson5c6c0592008-02-08 00:33:21 +0000171 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
172 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)));
Fariborz Jahanian54e423182008-01-23 01:34:33 +0000173
Steve Narofff84d11f2007-05-23 21:48:04 +0000174}
175
Chris Lattner88f70d62008-03-15 05:43:15 +0000176void Decl::addDeclKind(Kind k) {
Chris Lattnerfc234de2007-05-24 00:40:54 +0000177 switch (k) {
Chris Lattner88f70d62008-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 Lattner59a25942008-03-31 00:36:02 +0000196 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
Sam Bishop506215c2008-04-08 20:49:25 +0000197 case ObjCProperty: nObjCPropertyDecl++; break;
Chris Lattner88f70d62008-03-15 05:43:15 +0000198 case LinkageSpec: nLinkageSpecDecl++; break;
199 case FileScopeAsm: nFileScopeAsmDecl++; break;
Chris Lattnerfc234de2007-05-24 00:40:54 +0000200 }
Steve Narofff84d11f2007-05-23 21:48:04 +0000201}
202
Chris Lattner88f70d62008-03-15 05:43:15 +0000203//===----------------------------------------------------------------------===//
Chris Lattnera7b32872008-03-15 06:12:44 +0000204// Decl Allocation/Deallocation Method Implementations
205//===----------------------------------------------------------------------===//
206
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000207BlockVarDecl *BlockVarDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000208 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000209 IdentifierInfo *Id, QualType T,
210 StorageClass S, ScopedDecl *PrevDecl) {
Chris Lattner4b08ca82008-03-15 21:10:16 +0000211 void *Mem = C.getAllocator().Allocate<BlockVarDecl>();
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000212 return new (Mem) BlockVarDecl(CD, L, Id, T, S, PrevDecl);
Chris Lattner4b08ca82008-03-15 21:10:16 +0000213}
214
215
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000216FileVarDecl *FileVarDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000217 SourceLocation L, IdentifierInfo *Id,
218 QualType T, StorageClass S,
Chris Lattner96c460d2008-03-15 21:32:50 +0000219 ScopedDecl *PrevDecl) {
Chris Lattner4b08ca82008-03-15 21:10:16 +0000220 void *Mem = C.getAllocator().Allocate<FileVarDecl>();
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000221 return new (Mem) FileVarDecl(CD, L, Id, T, S, PrevDecl);
Chris Lattner4b08ca82008-03-15 21:10:16 +0000222}
223
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000224ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000225 SourceLocation L, IdentifierInfo *Id,
226 QualType T, StorageClass S,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000227 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner4b08ca82008-03-15 21:10:16 +0000228 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000229 return new (Mem) ParmVarDecl(CD, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner4b08ca82008-03-15 21:10:16 +0000230}
231
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000232FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000233 SourceLocation L,
Chris Lattner5072bae2008-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 Lattnerc5ffed42008-04-04 06:12:32 +0000238 return new (Mem) FunctionDecl(CD, L, Id, T, S, isInline, PrevDecl);
Chris Lattner5072bae2008-03-15 21:24:04 +0000239}
240
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000241FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattneree1284a2008-03-16 00:16:02 +0000242 IdentifierInfo *Id, QualType T, Expr *BW) {
243 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000244 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattneree1284a2008-03-16 00:16:02 +0000245}
246
Chris Lattner5072bae2008-03-15 21:24:04 +0000247
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000248EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
249 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000250 IdentifierInfo *Id, QualType T,
251 Expr *E, const llvm::APSInt &V,
252 ScopedDecl *PrevDecl){
Chris Lattnera7b32872008-03-15 06:12:44 +0000253 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000254 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000255}
256
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000257TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *CD,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000258 SourceLocation L,
Chris Lattner96c460d2008-03-15 21:32:50 +0000259 IdentifierInfo *Id, QualType T,
260 ScopedDecl *PD) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000261 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000262 return new (Mem) TypedefDecl(CD, L, Id, T, PD);
Chris Lattnera7b32872008-03-15 06:12:44 +0000263}
264
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000265EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *CD, SourceLocation L,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000266 IdentifierInfo *Id,
Chris Lattner96c460d2008-03-15 21:32:50 +0000267 ScopedDecl *PrevDecl) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000268 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000269 return new (Mem) EnumDecl(CD, L, Id, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000270}
271
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000272RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *CD,
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000273 SourceLocation L, IdentifierInfo *Id,
274 ScopedDecl *PrevDecl) {
Chris Lattnera7b32872008-03-15 06:12:44 +0000275 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000276 return new (Mem) RecordDecl(DK, CD, L, Id, PrevDecl);
Chris Lattnera7b32872008-03-15 06:12:44 +0000277}
278
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000279FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
280 SourceLocation L,
Chris Lattneree1284a2008-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 Lattnerc5ffed42008-04-04 06:12:32 +0000286LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
287 SourceLocation L,
Chris Lattneree1284a2008-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 Lattnera7b32872008-03-15 06:12:44 +0000292
293//===----------------------------------------------------------------------===//
Chris Lattner88f70d62008-03-15 05:43:15 +0000294// Decl Implementation
295//===----------------------------------------------------------------------===//
296
Chris Lattner6d9a6852006-10-25 05:11:20 +0000297// Out-of-line virtual method providing a home for Decl.
298Decl::~Decl() {
Anders Carlssonacea4152008-02-16 03:37:41 +0000299 if (!HasAttrs)
Anders Carlssone5070062008-02-15 23:30:50 +0000300 return;
301
302 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssonacea4152008-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 Carlssone5070062008-02-15 23:30:50 +0000311}
312
Chris Lattner88f70d62008-03-15 05:43:15 +0000313void Decl::addAttr(Attr *NewAttr) {
Anders Carlssone5070062008-02-15 23:30:50 +0000314 if (!DeclAttrs)
Chris Lattner89375192008-03-16 00:19:01 +0000315 DeclAttrs = new DeclAttrMapTy();
Anders Carlssone5070062008-02-15 23:30:50 +0000316
Chris Lattner88f70d62008-03-15 05:43:15 +0000317 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlssone5070062008-02-15 23:30:50 +0000318
Chris Lattner88f70d62008-03-15 05:43:15 +0000319 NewAttr->setNext(ExistingAttr);
320 ExistingAttr = NewAttr;
Anders Carlssone5070062008-02-15 23:30:50 +0000321
322 HasAttrs = true;
323}
324
Chris Lattner88f70d62008-03-15 05:43:15 +0000325const Attr *Decl::getAttrs() const {
Anders Carlssonacea4152008-02-16 03:37:41 +0000326 if (!HasAttrs)
Anders Carlssone5070062008-02-15 23:30:50 +0000327 return 0;
328
Anders Carlssonacea4152008-02-16 03:37:41 +0000329 return (*DeclAttrs)[this];
Chris Lattner6d9a6852006-10-25 05:11:20 +0000330}
Chris Lattner17ed4872006-11-20 04:58:19 +0000331
Chris Lattner59a25942008-03-31 00:36:02 +0000332//===----------------------------------------------------------------------===//
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000333// DeclContext Implementation
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000334//===----------------------------------------------------------------------===//
335
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000336DeclContext *DeclContext::getParent() const {
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000337 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000338 return SD->getDeclContext();
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000339 else
340 return NULL;
341}
342
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000343Decl *DeclContext::ToDecl (const DeclContext *D) {
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000344 return CastTo<Decl>(D);
345}
346
Chris Lattner0a5ff0d2008-04-06 04:47:34 +0000347DeclContext *DeclContext::FromDecl (const Decl *D) {
348 return CastTo<DeclContext>(D);
Chris Lattnerc5ffed42008-04-04 06:12:32 +0000349}
350
351//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000352// NamedDecl Implementation
353//===----------------------------------------------------------------------===//
354
Chris Lattnera4016552007-10-06 22:53:46 +0000355const char *NamedDecl::getName() const {
Chris Lattnerec040b12007-01-21 23:09:50 +0000356 if (const IdentifierInfo *II = getIdentifier())
357 return II->getName();
358 return "";
Chris Lattner17ed4872006-11-20 04:58:19 +0000359}
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000360
Chris Lattner59a25942008-03-31 00:36:02 +0000361//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +0000362// FunctionDecl Implementation
363//===----------------------------------------------------------------------===//
364
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000365FunctionDecl::~FunctionDecl() {
366 delete[] ParamInfo;
Sam Bishopca622662008-04-03 05:01:04 +0000367 delete Body;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000368}
369
370unsigned FunctionDecl::getNumParams() const {
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000371 const FunctionType *FT = getType()->getAsFunctionType();
372 if (isa<FunctionTypeNoProto>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +0000373 return 0;
Chris Lattnerf1e4ec22008-04-06 23:09:52 +0000374 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000375}
376
Chris Lattner53621a52007-06-13 20:44:40 +0000377void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000378 assert(ParamInfo == 0 && "Already has param info!");
379 assert(NumParams == getNumParams() && "Parameter count mismatch!");
380
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000381 // Zero params -> null pointer.
382 if (NumParams) {
Chris Lattner53621a52007-06-13 20:44:40 +0000383 ParamInfo = new ParmVarDecl*[NumParams];
384 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +0000385 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000386}
Chris Lattner41943152007-01-25 04:52:46 +0000387
Chris Lattner59a25942008-03-31 00:36:02 +0000388//===----------------------------------------------------------------------===//
389// RecordDecl Implementation
390//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +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.
Steve Naroffcc321422007-03-26 23:09:51 +0000395void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
Chris Lattner41943152007-01-25 04:52:46 +0000396 assert(!isDefinition() && "Cannot redefine record!");
397 setDefinition(true);
Chris Lattner5f521502007-01-25 06:27:24 +0000398 NumMembers = numMembers;
399 if (numMembers) {
Steve Naroffcc321422007-03-26 23:09:51 +0000400 Members = new FieldDecl*[numMembers];
Chris Lattner5f521502007-01-25 06:27:24 +0000401 memcpy(Members, members, numMembers*sizeof(Decl*));
Chris Lattner41943152007-01-25 04:52:46 +0000402 }
403}
Steve Naroffcc321422007-03-26 23:09:51 +0000404
Chris Lattner59a25942008-03-31 00:36:02 +0000405FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Steve Naroffcc321422007-03-26 23:09:51 +0000406 if (Members == 0 || NumMembers < 0)
407 return 0;
Fariborz Jahanian67341402007-10-04 00:45:27 +0000408
Chris Lattner59a25942008-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)
Steve Naroffcc321422007-03-26 23:09:51 +0000412 return Members[i];
Steve Naroffcc321422007-03-26 23:09:51 +0000413 return 0;
Chris Lattnerbd4de5df2007-07-12 15:43:07 +0000414}