blob: 32ca4198f56cbf476f8b09276dba94706d6018d6 [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"
Steve Naroff3fafa102007-10-01 19:00:59 +000015#include "clang/AST/DeclObjC.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"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Chris Lattnera8344c32008-03-15 05:43:15 +000021//===----------------------------------------------------------------------===//
22// Statistics
23//===----------------------------------------------------------------------===//
24
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff81f1bba2007-09-06 21:24:23 +000035static unsigned nInterfaceDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000036static unsigned nClassDecls = 0;
37static unsigned nMethodDecls = 0;
38static unsigned nProtocolDecls = 0;
Fariborz Jahanianc716c942007-09-21 15:40:54 +000039static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000040static unsigned nCategoryDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000041static unsigned nIvarDecls = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000042static unsigned nObjCImplementationDecls = 0;
43static unsigned nObjCCategoryImpl = 0;
44static unsigned nObjCCompatibleAlias = 0;
45static unsigned nObjCPropertyDecl = 0;
Chris Lattner806a5f52008-01-12 07:05:38 +000046static unsigned nLinkageSpecDecl = 0;
Anders Carlsson4f7f4412008-02-08 00:33:21 +000047static unsigned nFileScopeAsmDecl = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000048
Chris Lattner4b009652007-07-25 00:24:17 +000049static bool StatSwitch = false;
50
Anders Carlsson484eb5f2008-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 Naroff590aba82007-09-17 14:49:06 +000057const char *Decl::getDeclKindName() const {
Steve Narofff0c31dd2007-09-16 16:16:00 +000058 switch (DeclKind) {
59 default: assert(0 && "Unknown decl kind!");
Chris Lattnera8344c32008-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 Narofff0c31dd2007-09-16 16:16:00 +000075 }
76}
77
Chris Lattnera8344c32008-03-15 05:43:15 +000078bool Decl::CollectingStats(bool Enable) {
79 if (Enable)
80 StatSwitch = true;
81 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000082}
83
84void Decl::PrintStats() {
85 fprintf(stderr, "*** Decl Stats:\n");
86 fprintf(stderr, " %d decls total.\n",
Chris Lattner43b885f2008-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 Lattner4b009652007-07-25 00:24:17 +000090 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000091 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000092 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000093 nBlockVars, (int)sizeof(BlockVarDecl),
94 int(nBlockVars*sizeof(BlockVarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000095 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000096 nFileVars, (int)sizeof(FileVarDecl),
97 int(nFileVars*sizeof(FileVarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000098 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000099 nParmVars, (int)sizeof(ParmVarDecl),
100 int(nParmVars*sizeof(ParmVarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000101 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000102 nFieldDecls, (int)sizeof(FieldDecl),
103 int(nFieldDecls*sizeof(FieldDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000104 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000105 nSUC, (int)sizeof(RecordDecl),
106 int(nSUC*sizeof(RecordDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000107 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000108 nEnumDecls, (int)sizeof(EnumDecl),
109 int(nEnumDecls*sizeof(EnumDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000110 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000111 nEnumConst, (int)sizeof(EnumConstantDecl),
112 int(nEnumConst*sizeof(EnumConstantDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000113 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000114 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000115 // Objective-C decls...
116 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000117 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
118 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000119 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000120 nIvarDecls, (int)sizeof(ObjCIvarDecl),
121 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000122 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000123 nClassDecls, (int)sizeof(ObjCClassDecl),
124 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000125 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000126 nMethodDecls, (int)sizeof(ObjCMethodDecl),
127 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000128 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000129 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
130 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000131 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000132 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
133 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000134 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000135 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
136 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000137
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000138 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000139 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
140 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000141
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000142 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000143 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
144 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000145
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000146 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000147 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
148 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000149
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000150 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000151 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
152 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000153
Chris Lattner4b009652007-07-25 00:24:17 +0000154 fprintf(stderr, "Total bytes = %d\n",
Chris Lattner43b885f2008-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 Jahanian9b0994f2008-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 Carlsson4f7f4412008-02-08 00:33:21 +0000171 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
172 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)));
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000173
Chris Lattner4b009652007-07-25 00:24:17 +0000174}
175
Chris Lattnera8344c32008-03-15 05:43:15 +0000176void Decl::addDeclKind(Kind k) {
Chris Lattner4b009652007-07-25 00:24:17 +0000177 switch (k) {
Chris Lattnera8344c32008-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;
196 case CompatibleAlias: nObjCCompatibleAlias++; break;
197 case PropertyDecl: nObjCPropertyDecl++; break;
198 case LinkageSpec: nLinkageSpecDecl++; break;
199 case FileScopeAsm: nFileScopeAsmDecl++; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000200 }
201}
202
Chris Lattnera8344c32008-03-15 05:43:15 +0000203//===----------------------------------------------------------------------===//
204// Decl Implementation
205//===----------------------------------------------------------------------===//
206
Chris Lattner4b009652007-07-25 00:24:17 +0000207// Out-of-line virtual method providing a home for Decl.
208Decl::~Decl() {
Anders Carlssonb610a992008-02-16 03:37:41 +0000209 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000210 return;
211
212 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssonb610a992008-02-16 03:37:41 +0000213 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
214
215 delete it->second;
216 DeclAttrs->erase(it);
217 if (DeclAttrs->empty()) {
218 delete DeclAttrs;
219 DeclAttrs = 0;
220 }
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000221}
222
Chris Lattnera8344c32008-03-15 05:43:15 +0000223void Decl::addAttr(Attr *NewAttr) {
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000224 if (!DeclAttrs)
Chris Lattnera8344c32008-03-15 05:43:15 +0000225 DeclAttrs = new llvm::DenseMap<const Decl*, Attr*>();
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000226
Chris Lattnera8344c32008-03-15 05:43:15 +0000227 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000228
Chris Lattnera8344c32008-03-15 05:43:15 +0000229 NewAttr->setNext(ExistingAttr);
230 ExistingAttr = NewAttr;
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000231
232 HasAttrs = true;
233}
234
Chris Lattnera8344c32008-03-15 05:43:15 +0000235const Attr *Decl::getAttrs() const {
Anders Carlssonb610a992008-02-16 03:37:41 +0000236 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000237 return 0;
238
Anders Carlssonb610a992008-02-16 03:37:41 +0000239 return (*DeclAttrs)[this];
Chris Lattner4b009652007-07-25 00:24:17 +0000240}
241
Chris Lattner910435b2007-10-06 22:53:46 +0000242const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000243 if (const IdentifierInfo *II = getIdentifier())
244 return II->getName();
245 return "";
246}
247
Chris Lattner4b009652007-07-25 00:24:17 +0000248FunctionDecl::~FunctionDecl() {
249 delete[] ParamInfo;
250}
251
252unsigned FunctionDecl::getNumParams() const {
Chris Lattnera8344c32008-03-15 05:43:15 +0000253 if (isa<FunctionTypeNoProto>(getCanonicalType()))
254 return 0;
Chris Lattner23d411b2007-12-06 17:20:20 +0000255 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000256}
257
258void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
259 assert(ParamInfo == 0 && "Already has param info!");
260 assert(NumParams == getNumParams() && "Parameter count mismatch!");
261
262 // Zero params -> null pointer.
263 if (NumParams) {
264 ParamInfo = new ParmVarDecl*[NumParams];
265 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
266 }
267}
268
269
270/// defineBody - When created, RecordDecl's correspond to a forward declared
271/// record. This method is used to mark the decl as being defined, with the
272/// specified contents.
273void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
274 assert(!isDefinition() && "Cannot redefine record!");
275 setDefinition(true);
276 NumMembers = numMembers;
277 if (numMembers) {
278 Members = new FieldDecl*[numMembers];
279 memcpy(Members, members, numMembers*sizeof(Decl*));
280 }
281}
282
283FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
284 if (Members == 0 || NumMembers < 0)
285 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000286
Chris Lattner4b009652007-07-25 00:24:17 +0000287 // linear search. When C++ classes come along, will likely need to revisit.
288 for (int i = 0; i < NumMembers; ++i) {
289 if (Members[i]->getIdentifier() == name)
290 return Members[i];
291 }
292 return 0;
293}
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000294
Chris Lattnera8344c32008-03-15 05:43:15 +0000295
296//===----------------------------------------------------------------------===//
297// Objective-C Decl Implementation
298//===----------------------------------------------------------------------===//
299
Ted Kremenek42730c52008-01-07 19:49:32 +0000300void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanian84082af2007-10-04 17:06:28 +0000301 unsigned NumParams) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000302 assert(ParamInfo == 0 && "Already has param info!");
303
304 // Zero params -> null pointer.
305 if (NumParams) {
306 ParamInfo = new ParmVarDecl*[NumParams];
307 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
308 NumMethodParams = NumParams;
309 }
310}
311
Ted Kremenek42730c52008-01-07 19:49:32 +0000312ObjCMethodDecl::~ObjCMethodDecl() {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000313 delete[] ParamInfo;
314}
315
Ted Kremenek42730c52008-01-07 19:49:32 +0000316/// ObjCAddInstanceVariablesToClass - Inserts instance variables
317/// into ObjCInterfaceDecl's fields.
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000318///
Ted Kremenek42730c52008-01-07 19:49:32 +0000319void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000320 unsigned numIvars,
Steve Naroffef20ed32007-10-30 02:23:23 +0000321 SourceLocation RBrac) {
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000322 NumIvars = numIvars;
323 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000324 Ivars = new ObjCIvarDecl*[numIvars];
325 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000326 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000327 setLocEnd(RBrac);
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000328}
329
Ted Kremenek42730c52008-01-07 19:49:32 +0000330/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000331/// Variables (Ivars) relative to what declared in @implementation;s class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000332/// Ivars into ObjCImplementationDecl's fields.
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000333///
Ted Kremenek42730c52008-01-07 19:49:32 +0000334void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
335 ObjCIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000336 NumIvars = numIvars;
337 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000338 Ivars = new ObjCIvarDecl*[numIvars];
339 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000340 }
341}
342
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000343/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000344/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000345///
Ted Kremenek42730c52008-01-07 19:49:32 +0000346void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000347 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000348 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000349 unsigned numClsMembers,
350 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000351 NumInstanceMethods = numInsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000352 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000353 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
354 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000355 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000356 NumClassMethods = numClsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000357 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000358 ClassMethods = new ObjCMethodDecl*[numClsMembers];
359 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000360 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000361 AtEndLoc = endLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000362}
363
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000364/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000365/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000366///
Ted Kremenek42730c52008-01-07 19:49:32 +0000367void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000368 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000369 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000370 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000371 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000372 NumInstanceMethods = numInsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000373 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000374 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
375 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000376 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000377 NumClassMethods = numClsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000378 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000379 ClassMethods = new ObjCMethodDecl*[numClsMembers];
380 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000381 }
Steve Naroff667f1682007-10-30 13:30:57 +0000382 AtEndLoc = endLoc;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000383}
384
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000385/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000386/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000387///
Ted Kremenek42730c52008-01-07 19:49:32 +0000388void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000389 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000390 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000391 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000392 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000393 NumInstanceMethods = numInsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000394 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000395 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
396 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000397 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000398 NumClassMethods = numClsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000399 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000400 ClassMethods = new ObjCMethodDecl*[numClsMembers];
401 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000402 }
Steve Naroff667f1682007-10-30 13:30:57 +0000403 AtEndLoc = endLoc;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000404}
405
Ted Kremenek42730c52008-01-07 19:49:32 +0000406ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
407 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
408 ObjCInterfaceDecl* ClassDecl = this;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000409 while (ClassDecl != NULL) {
Chris Lattnerc7b06752007-12-12 07:56:42 +0000410 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
411 I != E; ++I) {
412 if ((*I)->getIdentifier() == ID) {
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000413 clsDeclared = ClassDecl;
Chris Lattnerc7b06752007-12-12 07:56:42 +0000414 return *I;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000415 }
416 }
417 ClassDecl = ClassDecl->getSuperClass();
418 }
419 return NULL;
420}
421
Chris Lattnerac9ab532007-12-12 07:30:05 +0000422/// lookupInstanceMethod - This method returns an instance method by looking in
Chris Lattner73e795c2007-12-12 08:17:45 +0000423/// the class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000424ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
425 ObjCInterfaceDecl* ClassDecl = this;
426 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000427
Steve Narofffa465d12007-10-02 20:01:56 +0000428 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000429 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000430 return MethodDecl;
431
Steve Naroff705380b2007-10-14 23:13:51 +0000432 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000433 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000434 int numProtocols = ClassDecl->getNumIntfRefProtocols();
435 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000436 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000437 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000438 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000439 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000440 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000441 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000442 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000443 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000444 CatDecl = CatDecl->getNextClassCategory();
445 }
Steve Narofffa465d12007-10-02 20:01:56 +0000446 ClassDecl = ClassDecl->getSuperClass();
447 }
448 return NULL;
449}
450
Steve Narofff66d84a2007-10-14 18:27:41 +0000451// lookupClassMethod - This method returns a class method by looking in the
Chris Lattner73e795c2007-12-12 08:17:45 +0000452// class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000453ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
454 ObjCInterfaceDecl* ClassDecl = this;
455 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000456
Steve Narofffa465d12007-10-02 20:01:56 +0000457 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000458 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000459 return MethodDecl;
460
Steve Naroff705380b2007-10-14 23:13:51 +0000461 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000462 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000463 int numProtocols = ClassDecl->getNumIntfRefProtocols();
464 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000465 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000466 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000467 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000468 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000469 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000470 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000471 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000472 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000473 CatDecl = CatDecl->getNextClassCategory();
474 }
Steve Narofffa465d12007-10-02 20:01:56 +0000475 ClassDecl = ClassDecl->getSuperClass();
476 }
477 return NULL;
478}
479
Chris Lattnerac9ab532007-12-12 07:30:05 +0000480/// lookupInstanceMethod - This method returns an instance method by looking in
481/// the class implementation. Unlike interfaces, we don't look outside the
482/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000483ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerac9ab532007-12-12 07:30:05 +0000484 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
485 if ((*I)->getSelector() == Sel)
486 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000487 return NULL;
488}
489
Chris Lattnerac9ab532007-12-12 07:30:05 +0000490/// lookupClassMethod - This method returns a class method by looking in
491/// the class implementation. Unlike interfaces, we don't look outside the
492/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000493ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000494 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
495 I != E; ++I)
496 if ((*I)->getSelector() == Sel)
497 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000498 return NULL;
499}
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000500
Steve Naroff31233632007-11-12 22:05:31 +0000501// lookupInstanceMethod - This method returns an instance method by looking in
502// the class implementation. Unlike interfaces, we don't look outside the
503// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000504ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000505 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
506 if ((*I)->getSelector() == Sel)
507 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000508 return NULL;
509}
510
511// lookupClassMethod - This method returns an instance method by looking in
512// the class implementation. Unlike interfaces, we don't look outside the
513// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000514ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000515 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
516 I != E; ++I)
517 if ((*I)->getSelector() == Sel)
518 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000519 return NULL;
520}
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000521
522// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
523// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000524ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
525 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000526
Steve Naroff74273de2007-12-19 22:27:04 +0000527 if ((MethodDecl = getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000528 return MethodDecl;
529
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000530 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000531 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000532
Fariborz Jahanian87829072007-12-20 19:24:10 +0000533 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000534 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000535 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000536 }
537 }
538 return NULL;
539}
540
541// lookupInstanceMethod - Lookup a class method in the protocol and protocols
542// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000543ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
544 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000545
Steve Naroff74273de2007-12-19 22:27:04 +0000546 if ((MethodDecl = getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000547 return MethodDecl;
548
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000549 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000550 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000551
Fariborz Jahanian87829072007-12-20 19:24:10 +0000552 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000553 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000554 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000555 }
556 }
557 return NULL;
558}
Steve Naroff2ce399a2007-12-14 23:37:57 +0000559
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000560/// getSynthesizedMethodSize - Compute size of synthesized method name
561/// as done be the rewrite.
562///
563unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
Fariborz Jahanian4aaf0c92008-01-17 01:36:09 +0000564 // syntesized method name is a concatenation of -/+[class-name selector]
565 // Get length of this name.
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000566 unsigned length = 3; // _I_ or _C_
567 length += strlen(getClassInterface()->getName()) +1; // extra for _
568 NamedDecl *MethodContext = getMethodContext();
569 if (ObjCCategoryImplDecl *CID =
570 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
571 length += strlen(CID->getName()) +1;
572 length += getSelector().getName().size(); // selector name
Fariborz Jahanian4aaf0c92008-01-17 01:36:09 +0000573 return length;
574}
575
Ted Kremenek42730c52008-01-07 19:49:32 +0000576ObjCInterfaceDecl *const ObjCMethodDecl::getClassInterface() const {
577 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000578 return ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000579 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000580 return CD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000581 if (ObjCImplementationDecl *IMD =
582 dyn_cast<ObjCImplementationDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000583 return IMD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000584 if (ObjCCategoryImplDecl *CID =
585 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000586 return CID->getClassInterface();
587 assert(false && "unknown method context");
588 return 0;
589}