blob: aad8f212bfe704d220afe38f4ac350b83db28727 [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"
Chris Lattnere4650482008-03-15 06:12:44 +000016#include "clang/AST/ASTContext.h"
Anders Carlsson3f70c542008-02-15 07:04:12 +000017#include "clang/AST/Attr.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000018#include "clang/Basic/IdentifierTable.h"
Anders Carlsson484eb5f2008-02-15 23:30:50 +000019#include "llvm/ADT/DenseMap.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Chris Lattnera8344c32008-03-15 05:43:15 +000022//===----------------------------------------------------------------------===//
23// Statistics
24//===----------------------------------------------------------------------===//
25
Chris Lattner4b009652007-07-25 00:24:17 +000026// temporary statistics gathering
27static unsigned nFuncs = 0;
28static unsigned nBlockVars = 0;
29static unsigned nFileVars = 0;
30static unsigned nParmVars = 0;
31static unsigned nSUC = 0;
32static unsigned nEnumConst = 0;
33static unsigned nEnumDecls = 0;
34static unsigned nTypedef = 0;
35static unsigned nFieldDecls = 0;
Steve Naroff81f1bba2007-09-06 21:24:23 +000036static unsigned nInterfaceDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000037static unsigned nClassDecls = 0;
38static unsigned nMethodDecls = 0;
39static unsigned nProtocolDecls = 0;
Fariborz Jahanianc716c942007-09-21 15:40:54 +000040static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +000041static unsigned nCategoryDecls = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000042static unsigned nIvarDecls = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000043static unsigned nObjCImplementationDecls = 0;
44static unsigned nObjCCategoryImpl = 0;
45static unsigned nObjCCompatibleAlias = 0;
46static unsigned nObjCPropertyDecl = 0;
Chris Lattner806a5f52008-01-12 07:05:38 +000047static unsigned nLinkageSpecDecl = 0;
Anders Carlsson4f7f4412008-02-08 00:33:21 +000048static unsigned nFileScopeAsmDecl = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000049
Chris Lattner4b009652007-07-25 00:24:17 +000050static bool StatSwitch = false;
51
Anders Carlsson484eb5f2008-02-15 23:30:50 +000052// This keeps track of all decl attributes. Since so few decls have attrs, we
53// keep them in a hash map instead of wasting space in the Decl class.
54typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
55
56static DeclAttrMapTy *DeclAttrs = 0;
57
Steve Naroff590aba82007-09-17 14:49:06 +000058const char *Decl::getDeclKindName() const {
Steve Narofff0c31dd2007-09-16 16:16:00 +000059 switch (DeclKind) {
60 default: assert(0 && "Unknown decl kind!");
Chris Lattnera8344c32008-03-15 05:43:15 +000061 case Typedef: return "Typedef";
62 case Function: return "Function";
63 case BlockVar: return "BlockVar";
64 case FileVar: return "FileVar";
65 case ParmVar: return "ParmVar";
66 case EnumConstant: return "EnumConstant";
67 case ObjCInterface: return "ObjCInterface";
68 case ObjCClass: return "ObjCClass";
69 case ObjCMethod: return "ObjCMethod";
70 case ObjCProtocol: return "ObjCProtocol";
71 case ObjCForwardProtocol: return "ObjCForwardProtocol";
72 case Struct: return "Struct";
73 case Union: return "Union";
74 case Class: return "Class";
75 case Enum: return "Enum";
Steve Narofff0c31dd2007-09-16 16:16:00 +000076 }
77}
78
Chris Lattnera8344c32008-03-15 05:43:15 +000079bool Decl::CollectingStats(bool Enable) {
80 if (Enable)
81 StatSwitch = true;
82 return StatSwitch;
Chris Lattner4b009652007-07-25 00:24:17 +000083}
84
85void Decl::PrintStats() {
86 fprintf(stderr, "*** Decl Stats:\n");
87 fprintf(stderr, " %d decls total.\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000088 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
89 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
90 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Chris Lattner4b009652007-07-25 00:24:17 +000091 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000092 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000093 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000094 nBlockVars, (int)sizeof(BlockVarDecl),
95 int(nBlockVars*sizeof(BlockVarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000096 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +000097 nFileVars, (int)sizeof(FileVarDecl),
98 int(nFileVars*sizeof(FileVarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +000099 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000100 nParmVars, (int)sizeof(ParmVarDecl),
101 int(nParmVars*sizeof(ParmVarDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000102 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000103 nFieldDecls, (int)sizeof(FieldDecl),
104 int(nFieldDecls*sizeof(FieldDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000105 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000106 nSUC, (int)sizeof(RecordDecl),
107 int(nSUC*sizeof(RecordDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000108 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000109 nEnumDecls, (int)sizeof(EnumDecl),
110 int(nEnumDecls*sizeof(EnumDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000111 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000112 nEnumConst, (int)sizeof(EnumConstantDecl),
113 int(nEnumConst*sizeof(EnumConstantDecl)));
Chris Lattner4b009652007-07-25 00:24:17 +0000114 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000115 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000116 // Objective-C decls...
117 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000118 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
119 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000120 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000121 nIvarDecls, (int)sizeof(ObjCIvarDecl),
122 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000123 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000124 nClassDecls, (int)sizeof(ObjCClassDecl),
125 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000126 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000127 nMethodDecls, (int)sizeof(ObjCMethodDecl),
128 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000129 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000130 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
131 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000132 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000133 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
134 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000135 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000136 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
137 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff948fd372007-09-17 14:16:13 +0000138
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000139 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000140 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
141 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000142
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000143 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000144 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
145 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000146
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000147 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000148 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
149 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000150
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000151 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000152 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
153 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000154
Chris Lattner4b009652007-07-25 00:24:17 +0000155 fprintf(stderr, "Total bytes = %d\n",
Chris Lattner43b885f2008-02-25 21:04:36 +0000156 int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+
157 nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+
158 nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+
159 nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+
160 nTypedef*sizeof(TypedefDecl)+
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000161 nInterfaceDecls*sizeof(ObjCInterfaceDecl)+
162 nIvarDecls*sizeof(ObjCIvarDecl)+
163 nClassDecls*sizeof(ObjCClassDecl)+
164 nMethodDecls*sizeof(ObjCMethodDecl)+
165 nProtocolDecls*sizeof(ObjCProtocolDecl)+
166 nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+
167 nCategoryDecls*sizeof(ObjCCategoryDecl)+
168 nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+
169 nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+
170 nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+
171 nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000172 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
173 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)));
Fariborz Jahanian9b0994f2008-01-23 01:34:33 +0000174
Chris Lattner4b009652007-07-25 00:24:17 +0000175}
176
Chris Lattnera8344c32008-03-15 05:43:15 +0000177void Decl::addDeclKind(Kind k) {
Chris Lattner4b009652007-07-25 00:24:17 +0000178 switch (k) {
Chris Lattnera8344c32008-03-15 05:43:15 +0000179 case Typedef: nTypedef++; break;
180 case Function: nFuncs++; break;
181 case BlockVar: nBlockVars++; break;
182 case FileVar: nFileVars++; break;
183 case ParmVar: nParmVars++; break;
184 case EnumConstant: nEnumConst++; break;
185 case Field: nFieldDecls++; break;
186 case Struct: case Union: case Class: nSUC++; break;
187 case Enum: nEnumDecls++; break;
188 case ObjCInterface: nInterfaceDecls++; break;
189 case ObjCClass: nClassDecls++; break;
190 case ObjCMethod: nMethodDecls++; break;
191 case ObjCProtocol: nProtocolDecls++; break;
192 case ObjCForwardProtocol: nForwardProtocolDecls++; break;
193 case ObjCCategory: nCategoryDecls++; break;
194 case ObjCIvar: nIvarDecls++; break;
195 case ObjCImplementation: nObjCImplementationDecls++; break;
196 case ObjCCategoryImpl: nObjCCategoryImpl++; break;
197 case CompatibleAlias: nObjCCompatibleAlias++; break;
198 case PropertyDecl: nObjCPropertyDecl++; break;
199 case LinkageSpec: nLinkageSpecDecl++; break;
200 case FileScopeAsm: nFileScopeAsmDecl++; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000201 }
202}
203
Chris Lattnera8344c32008-03-15 05:43:15 +0000204//===----------------------------------------------------------------------===//
Chris Lattnere4650482008-03-15 06:12:44 +0000205// Decl Allocation/Deallocation Method Implementations
206//===----------------------------------------------------------------------===//
207
208EnumConstantDecl *EnumConstantDecl::Create(SourceLocation L, IdentifierInfo *Id,
209 QualType T, Expr *E,
210 const llvm::APSInt &V,
211 ScopedDecl *PrevDecl, ASTContext &C){
212 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
213 return new (Mem) EnumConstantDecl(L, Id, T, E, V, PrevDecl);
214}
215
216TypedefDecl *TypedefDecl::Create(SourceLocation L, IdentifierInfo *Id,
217 QualType T, ScopedDecl *PD, ASTContext &C) {
218 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
219 return new (Mem) TypedefDecl(L, Id, T, PD);
220}
221
222EnumDecl *EnumDecl::Create(SourceLocation L, IdentifierInfo *Id,
223 ScopedDecl *PrevDecl, ASTContext &C) {
224 void *Mem = C.getAllocator().Allocate<EnumDecl>();
225 return new (Mem) EnumDecl(L, Id, PrevDecl);
226}
227
228RecordDecl *RecordDecl::Create(Kind DK, SourceLocation L, IdentifierInfo *Id,
229 ScopedDecl *PrevDecl, ASTContext &C) {
230 void *Mem = C.getAllocator().Allocate<RecordDecl>();
231 return new (Mem) RecordDecl(DK, L, Id, PrevDecl);
232}
233
234
235//===----------------------------------------------------------------------===//
Chris Lattnera8344c32008-03-15 05:43:15 +0000236// Decl Implementation
237//===----------------------------------------------------------------------===//
238
Chris Lattner4b009652007-07-25 00:24:17 +0000239// Out-of-line virtual method providing a home for Decl.
240Decl::~Decl() {
Anders Carlssonb610a992008-02-16 03:37:41 +0000241 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000242 return;
243
244 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssonb610a992008-02-16 03:37:41 +0000245 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
246
247 delete it->second;
248 DeclAttrs->erase(it);
249 if (DeclAttrs->empty()) {
250 delete DeclAttrs;
251 DeclAttrs = 0;
252 }
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000253}
254
Chris Lattnera8344c32008-03-15 05:43:15 +0000255void Decl::addAttr(Attr *NewAttr) {
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000256 if (!DeclAttrs)
Chris Lattnera8344c32008-03-15 05:43:15 +0000257 DeclAttrs = new llvm::DenseMap<const Decl*, Attr*>();
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000258
Chris Lattnera8344c32008-03-15 05:43:15 +0000259 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000260
Chris Lattnera8344c32008-03-15 05:43:15 +0000261 NewAttr->setNext(ExistingAttr);
262 ExistingAttr = NewAttr;
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000263
264 HasAttrs = true;
265}
266
Chris Lattnera8344c32008-03-15 05:43:15 +0000267const Attr *Decl::getAttrs() const {
Anders Carlssonb610a992008-02-16 03:37:41 +0000268 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000269 return 0;
270
Anders Carlssonb610a992008-02-16 03:37:41 +0000271 return (*DeclAttrs)[this];
Chris Lattner4b009652007-07-25 00:24:17 +0000272}
273
Chris Lattner910435b2007-10-06 22:53:46 +0000274const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000275 if (const IdentifierInfo *II = getIdentifier())
276 return II->getName();
277 return "";
278}
279
Chris Lattner4b009652007-07-25 00:24:17 +0000280FunctionDecl::~FunctionDecl() {
281 delete[] ParamInfo;
282}
283
284unsigned FunctionDecl::getNumParams() const {
Chris Lattnera8344c32008-03-15 05:43:15 +0000285 if (isa<FunctionTypeNoProto>(getCanonicalType()))
286 return 0;
Chris Lattner23d411b2007-12-06 17:20:20 +0000287 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000288}
289
290void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
291 assert(ParamInfo == 0 && "Already has param info!");
292 assert(NumParams == getNumParams() && "Parameter count mismatch!");
293
294 // Zero params -> null pointer.
295 if (NumParams) {
296 ParamInfo = new ParmVarDecl*[NumParams];
297 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
298 }
299}
300
301
302/// defineBody - When created, RecordDecl's correspond to a forward declared
303/// record. This method is used to mark the decl as being defined, with the
304/// specified contents.
305void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
306 assert(!isDefinition() && "Cannot redefine record!");
307 setDefinition(true);
308 NumMembers = numMembers;
309 if (numMembers) {
310 Members = new FieldDecl*[numMembers];
311 memcpy(Members, members, numMembers*sizeof(Decl*));
312 }
313}
314
315FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
316 if (Members == 0 || NumMembers < 0)
317 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000318
Chris Lattner4b009652007-07-25 00:24:17 +0000319 // linear search. When C++ classes come along, will likely need to revisit.
320 for (int i = 0; i < NumMembers; ++i) {
321 if (Members[i]->getIdentifier() == name)
322 return Members[i];
323 }
324 return 0;
325}
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000326
Chris Lattnera8344c32008-03-15 05:43:15 +0000327//===----------------------------------------------------------------------===//
328// Objective-C Decl Implementation
329//===----------------------------------------------------------------------===//
330
Ted Kremenek42730c52008-01-07 19:49:32 +0000331void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanian84082af2007-10-04 17:06:28 +0000332 unsigned NumParams) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000333 assert(ParamInfo == 0 && "Already has param info!");
334
335 // Zero params -> null pointer.
336 if (NumParams) {
337 ParamInfo = new ParmVarDecl*[NumParams];
338 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
339 NumMethodParams = NumParams;
340 }
341}
342
Ted Kremenek42730c52008-01-07 19:49:32 +0000343ObjCMethodDecl::~ObjCMethodDecl() {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000344 delete[] ParamInfo;
345}
346
Ted Kremenek42730c52008-01-07 19:49:32 +0000347/// ObjCAddInstanceVariablesToClass - Inserts instance variables
348/// into ObjCInterfaceDecl's fields.
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000349///
Ted Kremenek42730c52008-01-07 19:49:32 +0000350void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000351 unsigned numIvars,
Steve Naroffef20ed32007-10-30 02:23:23 +0000352 SourceLocation RBrac) {
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000353 NumIvars = numIvars;
354 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000355 Ivars = new ObjCIvarDecl*[numIvars];
356 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000357 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000358 setLocEnd(RBrac);
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000359}
360
Ted Kremenek42730c52008-01-07 19:49:32 +0000361/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000362/// Variables (Ivars) relative to what declared in @implementation;s class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000363/// Ivars into ObjCImplementationDecl's fields.
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000364///
Ted Kremenek42730c52008-01-07 19:49:32 +0000365void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
366 ObjCIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000367 NumIvars = numIvars;
368 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000369 Ivars = new ObjCIvarDecl*[numIvars];
370 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000371 }
372}
373
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000374/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000375/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000376///
Ted Kremenek42730c52008-01-07 19:49:32 +0000377void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000378 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000379 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000380 unsigned numClsMembers,
381 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000382 NumInstanceMethods = numInsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000383 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000384 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
385 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000386 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000387 NumClassMethods = numClsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000388 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000389 ClassMethods = new ObjCMethodDecl*[numClsMembers];
390 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000391 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000392 AtEndLoc = endLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000393}
394
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000395/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000396/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000397///
Ted Kremenek42730c52008-01-07 19:49:32 +0000398void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000399 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000400 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000401 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000402 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000403 NumInstanceMethods = numInsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000404 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000405 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
406 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000407 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000408 NumClassMethods = numClsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000409 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000410 ClassMethods = new ObjCMethodDecl*[numClsMembers];
411 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000412 }
Steve Naroff667f1682007-10-30 13:30:57 +0000413 AtEndLoc = endLoc;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000414}
415
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000416/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000417/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000418///
Ted Kremenek42730c52008-01-07 19:49:32 +0000419void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000420 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000421 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000422 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000423 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000424 NumInstanceMethods = numInsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000425 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000426 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
427 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000428 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000429 NumClassMethods = numClsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000430 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000431 ClassMethods = new ObjCMethodDecl*[numClsMembers];
432 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000433 }
Steve Naroff667f1682007-10-30 13:30:57 +0000434 AtEndLoc = endLoc;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000435}
436
Ted Kremenek42730c52008-01-07 19:49:32 +0000437ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
438 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
439 ObjCInterfaceDecl* ClassDecl = this;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000440 while (ClassDecl != NULL) {
Chris Lattnerc7b06752007-12-12 07:56:42 +0000441 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
442 I != E; ++I) {
443 if ((*I)->getIdentifier() == ID) {
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000444 clsDeclared = ClassDecl;
Chris Lattnerc7b06752007-12-12 07:56:42 +0000445 return *I;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000446 }
447 }
448 ClassDecl = ClassDecl->getSuperClass();
449 }
450 return NULL;
451}
452
Chris Lattnerac9ab532007-12-12 07:30:05 +0000453/// lookupInstanceMethod - This method returns an instance method by looking in
Chris Lattner73e795c2007-12-12 08:17:45 +0000454/// the class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000455ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
456 ObjCInterfaceDecl* ClassDecl = this;
457 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000458
Steve Narofffa465d12007-10-02 20:01:56 +0000459 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000460 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000461 return MethodDecl;
462
Steve Naroff705380b2007-10-14 23:13:51 +0000463 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000464 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000465 int numProtocols = ClassDecl->getNumIntfRefProtocols();
466 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000467 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000468 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000469 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000470 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000471 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000472 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000473 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000474 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000475 CatDecl = CatDecl->getNextClassCategory();
476 }
Steve Narofffa465d12007-10-02 20:01:56 +0000477 ClassDecl = ClassDecl->getSuperClass();
478 }
479 return NULL;
480}
481
Steve Narofff66d84a2007-10-14 18:27:41 +0000482// lookupClassMethod - This method returns a class method by looking in the
Chris Lattner73e795c2007-12-12 08:17:45 +0000483// class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000484ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
485 ObjCInterfaceDecl* ClassDecl = this;
486 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000487
Steve Narofffa465d12007-10-02 20:01:56 +0000488 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000489 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000490 return MethodDecl;
491
Steve Naroff705380b2007-10-14 23:13:51 +0000492 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000493 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000494 int numProtocols = ClassDecl->getNumIntfRefProtocols();
495 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000496 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000497 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000498 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000499 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000500 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000501 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000502 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000503 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000504 CatDecl = CatDecl->getNextClassCategory();
505 }
Steve Narofffa465d12007-10-02 20:01:56 +0000506 ClassDecl = ClassDecl->getSuperClass();
507 }
508 return NULL;
509}
510
Chris Lattnerac9ab532007-12-12 07:30:05 +0000511/// lookupInstanceMethod - 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 *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerac9ab532007-12-12 07:30:05 +0000515 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
516 if ((*I)->getSelector() == Sel)
517 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000518 return NULL;
519}
520
Chris Lattnerac9ab532007-12-12 07:30:05 +0000521/// lookupClassMethod - This method returns a class method by looking in
522/// the class implementation. Unlike interfaces, we don't look outside the
523/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000524ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000525 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
526 I != E; ++I)
527 if ((*I)->getSelector() == Sel)
528 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000529 return NULL;
530}
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000531
Steve Naroff31233632007-11-12 22:05:31 +0000532// lookupInstanceMethod - This method returns an instance method by looking in
533// the class implementation. Unlike interfaces, we don't look outside the
534// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000535ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000536 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
537 if ((*I)->getSelector() == Sel)
538 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000539 return NULL;
540}
541
542// lookupClassMethod - This method returns an instance method by looking in
543// the class implementation. Unlike interfaces, we don't look outside the
544// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000545ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000546 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
547 I != E; ++I)
548 if ((*I)->getSelector() == Sel)
549 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000550 return NULL;
551}
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000552
553// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
554// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000555ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
556 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000557
Steve Naroff74273de2007-12-19 22:27:04 +0000558 if ((MethodDecl = getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000559 return MethodDecl;
560
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000561 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000562 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000563
Fariborz Jahanian87829072007-12-20 19:24:10 +0000564 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000565 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000566 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000567 }
568 }
569 return NULL;
570}
571
572// lookupInstanceMethod - Lookup a class method in the protocol and protocols
573// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000574ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
575 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000576
Steve Naroff74273de2007-12-19 22:27:04 +0000577 if ((MethodDecl = getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000578 return MethodDecl;
579
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000580 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000581 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000582
Fariborz Jahanian87829072007-12-20 19:24:10 +0000583 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000584 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000585 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000586 }
587 }
588 return NULL;
589}
Steve Naroff2ce399a2007-12-14 23:37:57 +0000590
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000591/// getSynthesizedMethodSize - Compute size of synthesized method name
592/// as done be the rewrite.
593///
594unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
Fariborz Jahanian4aaf0c92008-01-17 01:36:09 +0000595 // syntesized method name is a concatenation of -/+[class-name selector]
596 // Get length of this name.
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000597 unsigned length = 3; // _I_ or _C_
598 length += strlen(getClassInterface()->getName()) +1; // extra for _
599 NamedDecl *MethodContext = getMethodContext();
600 if (ObjCCategoryImplDecl *CID =
601 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
602 length += strlen(CID->getName()) +1;
603 length += getSelector().getName().size(); // selector name
Fariborz Jahanian4aaf0c92008-01-17 01:36:09 +0000604 return length;
605}
606
Ted Kremenek42730c52008-01-07 19:49:32 +0000607ObjCInterfaceDecl *const ObjCMethodDecl::getClassInterface() const {
608 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000609 return ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000610 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000611 return CD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000612 if (ObjCImplementationDecl *IMD =
613 dyn_cast<ObjCImplementationDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000614 return IMD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000615 if (ObjCCategoryImplDecl *CID =
616 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000617 return CID->getClassInterface();
618 assert(false && "unknown method context");
619 return 0;
620}