blob: a831c4ff9a8a9e8bc0be996015edda16f9f0691f [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Chris Lattnere4650482008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Anders Carlsson3f70c542008-02-15 07:04:12 +000016#include "clang/AST/Attr.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Anders Carlsson484eb5f2008-02-15 23:30:50 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenekafdf8112008-05-20 00:43:19 +000019
20#include <stdio.h>
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
Chris Lattnera8344c32008-03-15 05:43:15 +000023//===----------------------------------------------------------------------===//
24// Statistics
25//===----------------------------------------------------------------------===//
26
Chris Lattner4b009652007-07-25 00:24:17 +000027// temporary statistics gathering
28static unsigned nFuncs = 0;
Steve Naroff72a6ebc2008-04-15 22:42:06 +000029static unsigned nVars = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000030static unsigned nParmVars = 0;
31static unsigned nSUC = 0;
32static unsigned nEnumConst = 0;
33static unsigned nEnumDecls = 0;
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000034static unsigned nNamespaces = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000035static unsigned nTypedef = 0;
36static unsigned nFieldDecls = 0;
Steve Naroff81f1bba2007-09-06 21:24:23 +000037static unsigned nInterfaceDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000038static unsigned nClassDecls = 0;
39static unsigned nMethodDecls = 0;
40static unsigned nProtocolDecls = 0;
Fariborz Jahanianc716c942007-09-21 15:40:54 +000041static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000042static unsigned nCategoryDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000043static unsigned nIvarDecls = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000044static unsigned nObjCImplementationDecls = 0;
45static unsigned nObjCCategoryImpl = 0;
46static unsigned nObjCCompatibleAlias = 0;
47static unsigned nObjCPropertyDecl = 0;
Fariborz Jahanian65a81732008-04-16 22:00:24 +000048static unsigned nObjCPropertyImplDecl = 0;
Chris Lattner806a5f52008-01-12 07:05:38 +000049static unsigned nLinkageSpecDecl = 0;
Anders Carlsson4f7f4412008-02-08 00:33:21 +000050static unsigned nFileScopeAsmDecl = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000051
Chris Lattner4b009652007-07-25 00:24:17 +000052static bool StatSwitch = false;
53
Anders Carlsson484eb5f2008-02-15 23:30:50 +000054// This keeps track of all decl attributes. Since so few decls have attrs, we
55// keep them in a hash map instead of wasting space in the Decl class.
56typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
57
58static DeclAttrMapTy *DeclAttrs = 0;
59
Steve Naroff590aba82007-09-17 14:49:06 +000060const char *Decl::getDeclKindName() const {
Steve Narofff0c31dd2007-09-16 16:16:00 +000061 switch (DeclKind) {
62 default: assert(0 && "Unknown decl kind!");
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000063 case Namespace: return "Namespace";
Chris Lattnera8344c32008-03-15 05:43:15 +000064 case Typedef: return "Typedef";
65 case Function: return "Function";
Steve Naroff72a6ebc2008-04-15 22:42:06 +000066 case Var: return "Var";
Chris Lattnera8344c32008-03-15 05:43:15 +000067 case ParmVar: return "ParmVar";
68 case EnumConstant: return "EnumConstant";
69 case ObjCInterface: return "ObjCInterface";
70 case ObjCClass: return "ObjCClass";
71 case ObjCMethod: return "ObjCMethod";
72 case ObjCProtocol: return "ObjCProtocol";
73 case ObjCForwardProtocol: return "ObjCForwardProtocol";
74 case Struct: return "Struct";
75 case Union: return "Union";
76 case Class: return "Class";
77 case Enum: return "Enum";
Steve Narofff0c31dd2007-09-16 16:16:00 +000078 }
79}
80
Chris Lattnera8344c32008-03-15 05:43:15 +000081bool Decl::CollectingStats(bool Enable) {
82 if (Enable)
83 StatSwitch = true;
84 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
87void Decl::PrintStats() {
88 fprintf(stderr, "*** Decl Stats:\n");
89 fprintf(stderr, " %d decls total.\n",
Steve Naroff72a6ebc2008-04-15 22:42:06 +000090 int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+
Chris Lattner43b885f2008-02-25 21:04:36 +000091 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000092 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
93 nNamespaces));
94 fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n",
95 nNamespaces, (int)sizeof(NamespaceDecl),
96 int(nNamespaces*sizeof(NamespaceDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000097 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000098 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Steve Naroff72a6ebc2008-04-15 22:42:06 +000099 fprintf(stderr, " %d variable decls, %d each (%d bytes)\n",
100 nVars, (int)sizeof(VarDecl),
101 int(nVars*sizeof(VarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000102 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000103 nParmVars, (int)sizeof(ParmVarDecl),
104 int(nParmVars*sizeof(ParmVarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000105 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000106 nFieldDecls, (int)sizeof(FieldDecl),
107 int(nFieldDecls*sizeof(FieldDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000108 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000109 nSUC, (int)sizeof(RecordDecl),
110 int(nSUC*sizeof(RecordDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000111 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000112 nEnumDecls, (int)sizeof(EnumDecl),
113 int(nEnumDecls*sizeof(EnumDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000114 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000115 nEnumConst, (int)sizeof(EnumConstantDecl),
116 int(nEnumConst*sizeof(EnumConstantDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000117 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000118 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000119 // Objective-C decls...
120 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000121 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
122 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000123 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000124 nIvarDecls, (int)sizeof(ObjCIvarDecl),
125 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000126 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000127 nClassDecls, (int)sizeof(ObjCClassDecl),
128 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000129 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000130 nMethodDecls, (int)sizeof(ObjCMethodDecl),
131 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000132 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000133 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
134 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000135 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000136 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
137 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000138 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000139 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
140 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000141
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000142 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000143 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
144 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000145
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000146 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000147 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
148 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000149
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000150 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000151 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
152 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000153
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000154 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000155 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
156 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000157
Fariborz Jahanian65a81732008-04-16 22:00:24 +0000158 fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n",
159 nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl),
160 int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)));
161
Chris Lattner4b009652007-07-25 00:24:17 +0000162 fprintf(stderr, "Total bytes = %d\n",
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000163 int(nFuncs*sizeof(FunctionDecl)+
164 nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+
Chris Lattner43b885f2008-02-25 21:04:36 +0000165 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
166 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
167 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000168 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
169 nIvarDecls*sizeof(ObjCIvarDecl)+
170 nClassDecls*sizeof(ObjCClassDecl)+
171 nMethodDecls*sizeof(ObjCMethodDecl)+
172 nProtocolDecls*sizeof(ObjCProtocolDecl)+
173 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
174 nCategoryDecls*sizeof(ObjCCategoryDecl)+
175 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
176 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
177 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
178 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
Fariborz Jahanian65a81732008-04-16 22:00:24 +0000179 nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000180 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000181 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
182 nNamespaces*sizeof(NamespaceDecl)));
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000183
Chris Lattner4b009652007-07-25 00:24:17 +0000184}
185
Chris Lattnera8344c32008-03-15 05:43:15 +0000186void Decl::addDeclKind(Kind k) {
Chris Lattner4b009652007-07-25 00:24:17 +0000187 switch (k) {
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000188 case Namespace: nNamespaces++; break;
Chris Lattnera8344c32008-03-15 05:43:15 +0000189 case Typedef: nTypedef++; break;
190 case Function: nFuncs++; break;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000191 case Var: nVars++; break;
Chris Lattnera8344c32008-03-15 05:43:15 +0000192 case ParmVar: nParmVars++; break;
193 case EnumConstant: nEnumConst++; break;
194 case Field: nFieldDecls++; break;
195 case Struct: case Union: case Class: nSUC++; break;
196 case Enum: nEnumDecls++; break;
197 case ObjCInterface: nInterfaceDecls++; break;
198 case ObjCClass: nClassDecls++; break;
199 case ObjCMethod: nMethodDecls++; break;
200 case ObjCProtocol: nProtocolDecls++; break;
201 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
202 case ObjCCategory: nCategoryDecls++; break;
203 case ObjCIvar: nIvarDecls++; break;
204 case ObjCImplementation: nObjCImplementationDecls++; break;
205 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000206 case ObjCCompatibleAlias: nObjCCompatibleAlias++; break;
Sam Bishopbe9c27a2008-04-08 20:49:25 +0000207 case ObjCProperty: nObjCPropertyDecl++; break;
Fariborz Jahanian65a81732008-04-16 22:00:24 +0000208 case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
Chris Lattnera8344c32008-03-15 05:43:15 +0000209 case LinkageSpec: nLinkageSpecDecl++; break;
210 case FileScopeAsm: nFileScopeAsmDecl++; break;
Argiris Kirtzidisd3586002008-04-17 14:40:12 +0000211 case TranslationUnit: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000212 }
213}
214
Chris Lattnera8344c32008-03-15 05:43:15 +0000215//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +0000216// Decl Allocation/Deallocation Method Implementations
217//===----------------------------------------------------------------------===//
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000218
Argiris Kirtzidisd3586002008-04-17 14:40:12 +0000219TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
220 void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
221 return new (Mem) TranslationUnitDecl();
222}
223
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000224NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
225 SourceLocation L, IdentifierInfo *Id) {
226 void *Mem = C.getAllocator().Allocate<NamespaceDecl>();
227 return new (Mem) NamespaceDecl(DC, L, Id);
228}
229
Chris Lattneref87a202008-04-22 18:39:57 +0000230VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000231 SourceLocation L,
232 IdentifierInfo *Id, QualType T,
233 StorageClass S, ScopedDecl *PrevDecl) {
234 void *Mem = C.getAllocator().Allocate<VarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000235 return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +0000236}
237
Chris Lattneref87a202008-04-22 18:39:57 +0000238ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000239 SourceLocation L, IdentifierInfo *Id,
240 QualType T, StorageClass S,
Chris Lattner3e254fb2008-04-08 04:40:51 +0000241 Expr *DefArg, ScopedDecl *PrevDecl) {
Chris Lattner48d225c2008-03-15 21:10:16 +0000242 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000243 return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl);
Chris Lattner48d225c2008-03-15 21:10:16 +0000244}
245
Chris Lattneref87a202008-04-22 18:39:57 +0000246FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000247 SourceLocation L,
Chris Lattner4c7802b2008-03-15 21:24:04 +0000248 IdentifierInfo *Id, QualType T,
249 StorageClass S, bool isInline,
250 ScopedDecl *PrevDecl) {
251 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000252 return new (Mem) FunctionDecl(DC, L, Id, T, S, isInline, PrevDecl);
Chris Lattner4c7802b2008-03-15 21:24:04 +0000253}
254
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000255FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000256 IdentifierInfo *Id, QualType T, Expr *BW) {
257 void *Mem = C.getAllocator().Allocate<FieldDecl>();
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000258 return new (Mem) FieldDecl(L, Id, T, BW);
Chris Lattner81db64a2008-03-16 00:16:02 +0000259}
260
Chris Lattner4c7802b2008-03-15 21:24:04 +0000261
Chris Lattnereee57c02008-04-04 06:12:32 +0000262EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
263 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000264 IdentifierInfo *Id, QualType T,
265 Expr *E, const llvm::APSInt &V,
266 ScopedDecl *PrevDecl){
Chris Lattnere4650482008-03-15 06:12:44 +0000267 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
Chris Lattnereee57c02008-04-04 06:12:32 +0000268 return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000269}
270
Chris Lattneref87a202008-04-22 18:39:57 +0000271TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000272 SourceLocation L,
Chris Lattner58114f02008-03-15 21:32:50 +0000273 IdentifierInfo *Id, QualType T,
274 ScopedDecl *PD) {
Chris Lattnere4650482008-03-15 06:12:44 +0000275 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000276 return new (Mem) TypedefDecl(DC, L, Id, T, PD);
Chris Lattnere4650482008-03-15 06:12:44 +0000277}
278
Chris Lattneref87a202008-04-22 18:39:57 +0000279EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
Chris Lattnereee57c02008-04-04 06:12:32 +0000280 IdentifierInfo *Id,
Chris Lattner58114f02008-03-15 21:32:50 +0000281 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000282 void *Mem = C.getAllocator().Allocate<EnumDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000283 return new (Mem) EnumDecl(DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000284}
285
Chris Lattneref87a202008-04-22 18:39:57 +0000286RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *DC,
Chris Lattnereee57c02008-04-04 06:12:32 +0000287 SourceLocation L, IdentifierInfo *Id,
288 ScopedDecl *PrevDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +0000289 void *Mem = C.getAllocator().Allocate<RecordDecl>();
Chris Lattneref87a202008-04-22 18:39:57 +0000290 return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl);
Chris Lattnere4650482008-03-15 06:12:44 +0000291}
292
Chris Lattnereee57c02008-04-04 06:12:32 +0000293FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C,
294 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000295 StringLiteral *Str) {
296 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
297 return new (Mem) FileScopeAsmDecl(L, Str);
298}
299
Chris Lattnereee57c02008-04-04 06:12:32 +0000300LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
301 SourceLocation L,
Chris Lattner81db64a2008-03-16 00:16:02 +0000302 LanguageIDs Lang, Decl *D) {
303 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
304 return new (Mem) LinkageSpecDecl(L, Lang, D);
305}
Chris Lattnere4650482008-03-15 06:12:44 +0000306
307//===----------------------------------------------------------------------===//
Chris Lattnera8344c32008-03-15 05:43:15 +0000308// Decl Implementation
309//===----------------------------------------------------------------------===//
310
Chris Lattner4b009652007-07-25 00:24:17 +0000311// Out-of-line virtual method providing a home for Decl.
312Decl::~Decl() {
Anders Carlssonb610a992008-02-16 03:37:41 +0000313 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000314 return;
315
316 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssonb610a992008-02-16 03:37:41 +0000317 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
318
Ted Kremenekafdf8112008-05-20 00:43:19 +0000319 // FIXME: Properly release attributes.
320 // delete it->second;
Anders Carlssonb610a992008-02-16 03:37:41 +0000321 DeclAttrs->erase(it);
Ted Kremenekafdf8112008-05-20 00:43:19 +0000322
Anders Carlssonb610a992008-02-16 03:37:41 +0000323 if (DeclAttrs->empty()) {
324 delete DeclAttrs;
325 DeclAttrs = 0;
326 }
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000327}
328
Chris Lattnera8344c32008-03-15 05:43:15 +0000329void Decl::addAttr(Attr *NewAttr) {
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000330 if (!DeclAttrs)
Chris Lattner10318b82008-03-16 00:19:01 +0000331 DeclAttrs = new DeclAttrMapTy();
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000332
Chris Lattnera8344c32008-03-15 05:43:15 +0000333 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000334
Chris Lattnera8344c32008-03-15 05:43:15 +0000335 NewAttr->setNext(ExistingAttr);
336 ExistingAttr = NewAttr;
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000337
338 HasAttrs = true;
339}
340
Chris Lattnera8344c32008-03-15 05:43:15 +0000341const Attr *Decl::getAttrs() const {
Anders Carlssonb610a992008-02-16 03:37:41 +0000342 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000343 return 0;
344
Anders Carlssonb610a992008-02-16 03:37:41 +0000345 return (*DeclAttrs)[this];
Chris Lattner4b009652007-07-25 00:24:17 +0000346}
347
Chris Lattner74bf5552008-05-04 02:29:49 +0000348void Decl::swapAttrs(Decl *RHS) {
349 bool HasLHSAttr = this->HasAttrs;
350 bool HasRHSAttr = RHS->HasAttrs;
351
352 // Usually, neither decl has attrs, nothing to do.
353 if (!HasLHSAttr && !HasRHSAttr) return;
354
355 // If 'this' has no attrs, swap the other way.
356 if (!HasLHSAttr)
357 return RHS->swapAttrs(this);
358
359 // Handle the case when both decls have attrs.
360 if (HasRHSAttr) {
361 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
362 return;
363 }
364
365 // Otherwise, LHS has an attr and RHS doesn't.
366 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
367 (*DeclAttrs).erase(this);
368 this->HasAttrs = false;
369 RHS->HasAttrs = true;
370}
371
372
Ted Kremenekafdf8112008-05-20 00:43:19 +0000373void Decl::Destroy(ASTContext& C) {
374 this->~Decl();
Sam Bishop274366f2008-04-11 15:01:25 +0000375 C.getAllocator().Deallocate((void *)this);
376}
377
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000378//===----------------------------------------------------------------------===//
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000379// DeclContext Implementation
Chris Lattnereee57c02008-04-04 06:12:32 +0000380//===----------------------------------------------------------------------===//
381
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000382DeclContext *DeclContext::getParent() const {
Chris Lattnereee57c02008-04-04 06:12:32 +0000383 if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this))
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000384 return SD->getDeclContext();
Chris Lattnereee57c02008-04-04 06:12:32 +0000385 else
386 return NULL;
387}
388
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000389Decl *DeclContext::ToDecl (const DeclContext *D) {
Chris Lattnereee57c02008-04-04 06:12:32 +0000390 return CastTo<Decl>(D);
391}
392
Chris Lattnerf3874bc2008-04-06 04:47:34 +0000393DeclContext *DeclContext::FromDecl (const Decl *D) {
394 return CastTo<DeclContext>(D);
Chris Lattnereee57c02008-04-04 06:12:32 +0000395}
396
397//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000398// NamedDecl Implementation
399//===----------------------------------------------------------------------===//
400
Chris Lattner910435b2007-10-06 22:53:46 +0000401const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000402 if (const IdentifierInfo *II = getIdentifier())
403 return II->getName();
404 return "";
405}
406
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000407//===----------------------------------------------------------------------===//
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000408// FunctionDecl Implementation
409//===----------------------------------------------------------------------===//
410
Chris Lattner4b009652007-07-25 00:24:17 +0000411FunctionDecl::~FunctionDecl() {
412 delete[] ParamInfo;
Douglas Gregor42214c52008-04-21 02:02:58 +0000413}
414
Ted Kremenekafdf8112008-05-20 00:43:19 +0000415void FunctionDecl::Destroy(ASTContext& C) {
416 if (Body) Body->Destroy(C);
417 Decl::Destroy(C);
418}
419
420
Douglas Gregor42214c52008-04-21 02:02:58 +0000421Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
422 for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) {
423 if (FD->Body) {
424 Definition = FD;
425 return FD->Body;
426 }
427 }
428
429 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000430}
431
432unsigned FunctionDecl::getNumParams() const {
Chris Lattnere8733592008-04-06 23:09:52 +0000433 const FunctionType *FT = getType()->getAsFunctionType();
434 if (isa<FunctionTypeNoProto>(FT))
Chris Lattnera8344c32008-03-15 05:43:15 +0000435 return 0;
Chris Lattnere8733592008-04-06 23:09:52 +0000436 return cast<FunctionTypeProto>(FT)->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000437}
438
439void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
440 assert(ParamInfo == 0 && "Already has param info!");
441 assert(NumParams == getNumParams() && "Parameter count mismatch!");
442
443 // Zero params -> null pointer.
444 if (NumParams) {
445 ParamInfo = new ParmVarDecl*[NumParams];
446 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
447 }
448}
449
Chris Lattner97316c02008-04-10 02:22:51 +0000450/// getMinRequiredArguments - Returns the minimum number of arguments
451/// needed to call this function. This may be fewer than the number of
452/// function parameters, if some of the parameters have default
Chris Lattnerb1856db2008-04-12 23:52:44 +0000453/// arguments (in C++).
Chris Lattner97316c02008-04-10 02:22:51 +0000454unsigned FunctionDecl::getMinRequiredArguments() const {
455 unsigned NumRequiredArgs = getNumParams();
456 while (NumRequiredArgs > 0
457 && getParamDecl(NumRequiredArgs-1)->getDefaultArg())
458 --NumRequiredArgs;
459
460 return NumRequiredArgs;
461}
462
Douglas Gregor42214c52008-04-21 02:02:58 +0000463/// AddRedeclaration - Specifies that this function declaration has been
464/// redeclared by the function declaration FD. FD must be a
465/// redeclaration of this based on the semantics of the language being
466/// translated ("compatible" function types in C, same signatures in
467/// C++).
468void FunctionDecl::AddRedeclaration(FunctionDecl *FD) {
469 assert(FD->PreviousDeclaration == 0 &&
470 "Redeclaration already has a previous declaration!");
471
472 // Insert FD into the list of previous declarations of this
473 // function.
474 FD->PreviousDeclaration = this->PreviousDeclaration;
475 this->PreviousDeclaration = FD;
476
477 // Swap the contents of this function declaration and FD. This
478 // effectively transforms the original declaration into the most
479 // recent declaration, so that all references to this declaration
480 // remain valid (and have information from *all* declarations),
481 // while retaining all of the information about previous
482 // declarations as well.
483
484 // Swap parameters, so that the most recent parameter names and
485 // exact types (e.g., enum vs int) show up in the original
486 // declaration.
Chris Lattner74bf5552008-05-04 02:29:49 +0000487 std::swap(this->ParamInfo, FD->ParamInfo);
Douglas Gregor42214c52008-04-21 02:02:58 +0000488
489 // Swap the function body: all declarations share the same function
490 // body, but we keep track of who actually defined that function
491 // body by keeping the pointer to the body stored in that node.
Chris Lattner74bf5552008-05-04 02:29:49 +0000492 std::swap(this->Body, FD->Body);
Douglas Gregor42214c52008-04-21 02:02:58 +0000493
494 // Swap type information: this is important because in C, later
495 // declarations can provide slightly different types (enum vs. int,
496 // for example).
497 QualType thisType = this->getType();
498 this->setType(FD->getType());
499 FD->setType(thisType);
500
501 // Swap location information: this allows us to produce diagnostics
502 // later on that reference the most recent declaration (which has
503 // the most information!) while retaining the location of previous
504 // declarations (good for "redefinition" diagnostics).
505 SourceLocation thisLocation = this->getLocation();
506 this->setLocation(FD->getLocation());
507 FD->setLocation(thisLocation);
508
509 // Swap attributes. FD will have the union of the attributes from
510 // all previous declarations.
Chris Lattner74bf5552008-05-04 02:29:49 +0000511 this->swapAttrs(FD);
Douglas Gregor42214c52008-04-21 02:02:58 +0000512
513 // If any declaration is inline, the function is inline.
514 this->IsInline |= FD->IsInline;
515
516 // FIXME: Is this the right way to handle storage specifiers?
517 if (FD->SClass) this->SClass = FD->SClass;
518}
519
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000520//===----------------------------------------------------------------------===//
521// RecordDecl Implementation
522//===----------------------------------------------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +0000523
524/// defineBody - When created, RecordDecl's correspond to a forward declared
525/// record. This method is used to mark the decl as being defined, with the
526/// specified contents.
527void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
528 assert(!isDefinition() && "Cannot redefine record!");
529 setDefinition(true);
530 NumMembers = numMembers;
531 if (numMembers) {
532 Members = new FieldDecl*[numMembers];
533 memcpy(Members, members, numMembers*sizeof(Decl*));
534 }
535}
536
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000537FieldDecl *RecordDecl::getMember(IdentifierInfo *II) {
Chris Lattner4b009652007-07-25 00:24:17 +0000538 if (Members == 0 || NumMembers < 0)
539 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000540
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000541 // Linear search. When C++ classes come along, will likely need to revisit.
542 for (int i = 0; i != NumMembers; ++i)
543 if (Members[i]->getIdentifier() == II)
Chris Lattner4b009652007-07-25 00:24:17 +0000544 return Members[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000545 return 0;
546}