blob: 4ce6fa8f3c4f03429b5f3b330a6c78b4a2427979 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Steve Naroff980e5082007-10-01 19:00:59 +000015#include "clang/AST/DeclObjC.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000016#include "clang/AST/ASTContext.h"
Anders Carlssonf78915f2008-02-15 07:04:12 +000017#include "clang/AST/Attr.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000018#include "clang/Basic/IdentifierTable.h"
Anders Carlssonb0dd2682008-02-15 23:30:50 +000019#include "llvm/ADT/DenseMap.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Chris Lattnerd3b90652008-03-15 05:43:15 +000022//===----------------------------------------------------------------------===//
23// Statistics
24//===----------------------------------------------------------------------===//
25
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff3536b442007-09-06 21:24:23 +000036static unsigned nInterfaceDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000037static unsigned nClassDecls = 0;
38static unsigned nMethodDecls = 0;
39static unsigned nProtocolDecls = 0;
Fariborz Jahanian894c57f2007-09-21 15:40:54 +000040static unsigned nForwardProtocolDecls = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +000041static unsigned nCategoryDecls = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000042static unsigned nIvarDecls = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000043static unsigned nObjCImplementationDecls = 0;
44static unsigned nObjCCategoryImpl = 0;
45static unsigned nObjCCompatibleAlias = 0;
46static unsigned nObjCPropertyDecl = 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +000047static unsigned nLinkageSpecDecl = 0;
Anders Carlssondfab6cb2008-02-08 00:33:21 +000048static unsigned nFileScopeAsmDecl = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050static bool StatSwitch = false;
51
Anders Carlssonb0dd2682008-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 Naroffe5ea3802007-09-17 14:49:06 +000058const char *Decl::getDeclKindName() const {
Steve Naroff8c9f13e2007-09-16 16:16:00 +000059 switch (DeclKind) {
60 default: assert(0 && "Unknown decl kind!");
Chris Lattnerd3b90652008-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 Naroff8c9f13e2007-09-16 16:16:00 +000076 }
77}
78
Chris Lattnerd3b90652008-03-15 05:43:15 +000079bool Decl::CollectingStats(bool Enable) {
80 if (Enable)
81 StatSwitch = true;
82 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000083}
84
85void Decl::PrintStats() {
86 fprintf(stderr, "*** Decl Stats:\n");
87 fprintf(stderr, " %d decls total.\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000088 int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+
89 nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
90 nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls));
Reid Spencer5f016e22007-07-11 17:01:13 +000091 fprintf(stderr, " %d function decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000092 nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +000093 fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000094 nBlockVars, (int)sizeof(BlockVarDecl),
95 int(nBlockVars*sizeof(BlockVarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +000096 fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +000097 nFileVars, (int)sizeof(FileVarDecl),
98 int(nFileVars*sizeof(FileVarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +000099 fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000100 nParmVars, (int)sizeof(ParmVarDecl),
101 int(nParmVars*sizeof(ParmVarDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 fprintf(stderr, " %d field decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000103 nFieldDecls, (int)sizeof(FieldDecl),
104 int(nFieldDecls*sizeof(FieldDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000106 nSUC, (int)sizeof(RecordDecl),
107 int(nSUC*sizeof(RecordDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 fprintf(stderr, " %d enum decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000109 nEnumDecls, (int)sizeof(EnumDecl),
110 int(nEnumDecls*sizeof(EnumDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000112 nEnumConst, (int)sizeof(EnumConstantDecl),
113 int(nEnumConst*sizeof(EnumConstantDecl)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000115 nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000116 // Objective-C decls...
117 fprintf(stderr, " %d interface decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000118 nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl),
119 int(nInterfaceDecls*sizeof(ObjCInterfaceDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000120 fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000121 nIvarDecls, (int)sizeof(ObjCIvarDecl),
122 int(nIvarDecls*sizeof(ObjCIvarDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000123 fprintf(stderr, " %d class decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000124 nClassDecls, (int)sizeof(ObjCClassDecl),
125 int(nClassDecls*sizeof(ObjCClassDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000126 fprintf(stderr, " %d method decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000127 nMethodDecls, (int)sizeof(ObjCMethodDecl),
128 int(nMethodDecls*sizeof(ObjCMethodDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000129 fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000130 nProtocolDecls, (int)sizeof(ObjCProtocolDecl),
131 int(nProtocolDecls*sizeof(ObjCProtocolDecl)));
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000132 fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000133 nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl),
134 int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000135 fprintf(stderr, " %d category decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000136 nCategoryDecls, (int)sizeof(ObjCCategoryDecl),
137 int(nCategoryDecls*sizeof(ObjCCategoryDecl)));
Steve Naroff3f128ad2007-09-17 14:16:13 +0000138
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000139 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000140 nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl),
141 int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl)));
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000142
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000143 fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000144 nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl),
145 int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)));
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000146
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000147 fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000148 nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl),
149 int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)));
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000150
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000151 fprintf(stderr, " %d property decls, %d each (%d bytes)\n",
Chris Lattnerc81c8142008-02-25 21:04:36 +0000152 nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl),
153 int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl)));
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 fprintf(stderr, "Total bytes = %d\n",
Chris Lattnerc81c8142008-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 Jahanian269b10d2008-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 Carlssondfab6cb2008-02-08 00:33:21 +0000172 nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
173 nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)));
Fariborz Jahanian269b10d2008-01-23 01:34:33 +0000174
Reid Spencer5f016e22007-07-11 17:01:13 +0000175}
176
Chris Lattnerd3b90652008-03-15 05:43:15 +0000177void Decl::addDeclKind(Kind k) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 switch (k) {
Chris Lattnerd3b90652008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 }
202}
203
Chris Lattnerd3b90652008-03-15 05:43:15 +0000204//===----------------------------------------------------------------------===//
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000205// Decl Allocation/Deallocation Method Implementations
206//===----------------------------------------------------------------------===//
207
Chris Lattnerc63e6602008-03-15 21:32:50 +0000208BlockVarDecl *BlockVarDecl::Create(ASTContext &C, SourceLocation L,
209 IdentifierInfo *Id, QualType T,
210 StorageClass S, ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000211 void *Mem = C.getAllocator().Allocate<BlockVarDecl>();
212 return new (Mem) BlockVarDecl(L, Id, T, S, PrevDecl);
213}
214
215
Chris Lattnerc63e6602008-03-15 21:32:50 +0000216FileVarDecl *FileVarDecl::Create(ASTContext &C, SourceLocation L,
217 IdentifierInfo *Id, QualType T, StorageClass S,
218 ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000219 void *Mem = C.getAllocator().Allocate<FileVarDecl>();
220 return new (Mem) FileVarDecl(L, Id, T, S, PrevDecl);
221}
222
Chris Lattnerc63e6602008-03-15 21:32:50 +0000223ParmVarDecl *ParmVarDecl::Create(ASTContext &C, SourceLocation L,
224 IdentifierInfo *Id, QualType T, StorageClass S,
225 ScopedDecl *PrevDecl) {
Chris Lattner9e151e12008-03-15 21:10:16 +0000226 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
227 return new (Mem) ParmVarDecl(L, Id, T, S, PrevDecl);
228}
229
Chris Lattnera98e58d2008-03-15 21:24:04 +0000230FunctionDecl *FunctionDecl::Create(ASTContext &C, SourceLocation L,
231 IdentifierInfo *Id, QualType T,
232 StorageClass S, bool isInline,
233 ScopedDecl *PrevDecl) {
234 void *Mem = C.getAllocator().Allocate<FunctionDecl>();
235 return new (Mem) FunctionDecl(L, Id, T, S, isInline, PrevDecl);
236}
237
Chris Lattner8e25d862008-03-16 00:16:02 +0000238FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L,
239 IdentifierInfo *Id, QualType T, Expr *BW) {
240 void *Mem = C.getAllocator().Allocate<FieldDecl>();
241 return new (Mem) FieldDecl(L, Id, T, BW);
242}
243
Chris Lattnera98e58d2008-03-15 21:24:04 +0000244
Chris Lattnerc63e6602008-03-15 21:32:50 +0000245EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, SourceLocation L,
246 IdentifierInfo *Id, QualType T,
247 Expr *E, const llvm::APSInt &V,
248 ScopedDecl *PrevDecl){
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000249 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
250 return new (Mem) EnumConstantDecl(L, Id, T, E, V, PrevDecl);
251}
252
Chris Lattnerc63e6602008-03-15 21:32:50 +0000253TypedefDecl *TypedefDecl::Create(ASTContext &C, SourceLocation L,
254 IdentifierInfo *Id, QualType T,
255 ScopedDecl *PD) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000256 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
257 return new (Mem) TypedefDecl(L, Id, T, PD);
258}
259
Chris Lattnerc63e6602008-03-15 21:32:50 +0000260EnumDecl *EnumDecl::Create(ASTContext &C, SourceLocation L, IdentifierInfo *Id,
261 ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000262 void *Mem = C.getAllocator().Allocate<EnumDecl>();
263 return new (Mem) EnumDecl(L, Id, PrevDecl);
264}
265
Chris Lattnerc63e6602008-03-15 21:32:50 +0000266RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, SourceLocation L,
267 IdentifierInfo *Id, ScopedDecl *PrevDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000268 void *Mem = C.getAllocator().Allocate<RecordDecl>();
269 return new (Mem) RecordDecl(DK, L, Id, PrevDecl);
270}
271
Chris Lattner8e25d862008-03-16 00:16:02 +0000272FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, SourceLocation L,
273 StringLiteral *Str) {
274 void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>();
275 return new (Mem) FileScopeAsmDecl(L, Str);
276}
277
278LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, SourceLocation L,
279 LanguageIDs Lang, Decl *D) {
280 void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>();
281 return new (Mem) LinkageSpecDecl(L, Lang, D);
282}
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000283
284//===----------------------------------------------------------------------===//
Chris Lattnerd3b90652008-03-15 05:43:15 +0000285// Decl Implementation
286//===----------------------------------------------------------------------===//
287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288// Out-of-line virtual method providing a home for Decl.
289Decl::~Decl() {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000290 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000291 return;
292
293 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000294 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
295
296 delete it->second;
297 DeclAttrs->erase(it);
298 if (DeclAttrs->empty()) {
299 delete DeclAttrs;
300 DeclAttrs = 0;
301 }
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000302}
303
Chris Lattnerd3b90652008-03-15 05:43:15 +0000304void Decl::addAttr(Attr *NewAttr) {
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000305 if (!DeclAttrs)
Chris Lattnerd3b90652008-03-15 05:43:15 +0000306 DeclAttrs = new llvm::DenseMap<const Decl*, Attr*>();
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000307
Chris Lattnerd3b90652008-03-15 05:43:15 +0000308 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000309
Chris Lattnerd3b90652008-03-15 05:43:15 +0000310 NewAttr->setNext(ExistingAttr);
311 ExistingAttr = NewAttr;
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000312
313 HasAttrs = true;
314}
315
Chris Lattnerd3b90652008-03-15 05:43:15 +0000316const Attr *Decl::getAttrs() const {
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000317 if (!HasAttrs)
Anders Carlssonb0dd2682008-02-15 23:30:50 +0000318 return 0;
319
Anders Carlssoneb7adf32008-02-16 03:37:41 +0000320 return (*DeclAttrs)[this];
Reid Spencer5f016e22007-07-11 17:01:13 +0000321}
322
Chris Lattnerfd5de472007-10-06 22:53:46 +0000323const char *NamedDecl::getName() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 if (const IdentifierInfo *II = getIdentifier())
325 return II->getName();
326 return "";
327}
328
Reid Spencer5f016e22007-07-11 17:01:13 +0000329FunctionDecl::~FunctionDecl() {
330 delete[] ParamInfo;
331}
332
333unsigned FunctionDecl::getNumParams() const {
Chris Lattnerd3b90652008-03-15 05:43:15 +0000334 if (isa<FunctionTypeNoProto>(getCanonicalType()))
335 return 0;
Chris Lattnerec584d62007-12-06 17:20:20 +0000336 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Reid Spencer5f016e22007-07-11 17:01:13 +0000337}
338
339void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
340 assert(ParamInfo == 0 && "Already has param info!");
341 assert(NumParams == getNumParams() && "Parameter count mismatch!");
342
343 // Zero params -> null pointer.
344 if (NumParams) {
345 ParamInfo = new ParmVarDecl*[NumParams];
346 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
347 }
348}
349
350
351/// defineBody - When created, RecordDecl's correspond to a forward declared
352/// record. This method is used to mark the decl as being defined, with the
353/// specified contents.
354void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
355 assert(!isDefinition() && "Cannot redefine record!");
356 setDefinition(true);
357 NumMembers = numMembers;
358 if (numMembers) {
359 Members = new FieldDecl*[numMembers];
360 memcpy(Members, members, numMembers*sizeof(Decl*));
361 }
362}
363
364FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
365 if (Members == 0 || NumMembers < 0)
366 return 0;
Fariborz Jahanian3f5faf72007-10-04 00:45:27 +0000367
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 // linear search. When C++ classes come along, will likely need to revisit.
369 for (int i = 0; i < NumMembers; ++i) {
370 if (Members[i]->getIdentifier() == name)
371 return Members[i];
372 }
373 return 0;
Chris Lattner6fa5f092007-07-12 15:43:07 +0000374}
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000375
Chris Lattnerd3b90652008-03-15 05:43:15 +0000376//===----------------------------------------------------------------------===//
377// Objective-C Decl Implementation
378//===----------------------------------------------------------------------===//
379
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000380void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanian59519652007-10-04 17:06:28 +0000381 unsigned NumParams) {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000382 assert(ParamInfo == 0 && "Already has param info!");
383
384 // Zero params -> null pointer.
385 if (NumParams) {
386 ParamInfo = new ParmVarDecl*[NumParams];
387 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
388 NumMethodParams = NumParams;
389 }
390}
391
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000392ObjCMethodDecl::~ObjCMethodDecl() {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000393 delete[] ParamInfo;
394}
395
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000396/// ObjCAddInstanceVariablesToClass - Inserts instance variables
397/// into ObjCInterfaceDecl's fields.
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000398///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000399void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
Steve Naroff60fccee2007-10-29 21:38:07 +0000400 unsigned numIvars,
Steve Narofff908a872007-10-30 02:23:23 +0000401 SourceLocation RBrac) {
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000402 NumIvars = numIvars;
403 if (numIvars) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000404 Ivars = new ObjCIvarDecl*[numIvars];
405 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000406 }
Steve Narofff908a872007-10-30 02:23:23 +0000407 setLocEnd(RBrac);
Fariborz Jahanianb04a0212007-09-14 21:08:27 +0000408}
409
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000410/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000411/// Variables (Ivars) relative to what declared in @implementation;s class.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000412/// Ivars into ObjCImplementationDecl's fields.
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000413///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000414void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
415 ObjCIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000416 NumIvars = numIvars;
417 if (numIvars) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000418 Ivars = new ObjCIvarDecl*[numIvars];
419 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahaniand0b90bf2007-09-26 18:27:25 +0000420 }
421}
422
Steve Naroff60fccee2007-10-29 21:38:07 +0000423/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000424/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000425///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000426void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000427 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000428 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000429 unsigned numClsMembers,
430 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000431 NumInstanceMethods = numInsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000432 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000433 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
434 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000435 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000436 NumClassMethods = numClsMembers;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000437 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000438 ClassMethods = new ObjCMethodDecl*[numClsMembers];
439 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000440 }
Steve Narofff908a872007-10-30 02:23:23 +0000441 AtEndLoc = endLoc;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000442}
443
Steve Naroff60fccee2007-10-29 21:38:07 +0000444/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000445/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000446///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000447void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000448 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000449 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000450 unsigned numClsMembers,
Steve Naroff423cb562007-10-30 13:30:57 +0000451 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000452 NumInstanceMethods = numInsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000453 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000454 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
455 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000456 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000457 NumClassMethods = numClsMembers;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000458 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000459 ClassMethods = new ObjCMethodDecl*[numClsMembers];
460 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000461 }
Steve Naroff423cb562007-10-30 13:30:57 +0000462 AtEndLoc = endLoc;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000463}
464
Steve Naroff60fccee2007-10-29 21:38:07 +0000465/// addMethods - Insert instance and methods declarations into
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000466/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000467///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000468void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000469 unsigned numInsMembers,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000470 ObjCMethodDecl **clsMethods,
Steve Naroff60fccee2007-10-29 21:38:07 +0000471 unsigned numClsMembers,
Steve Naroff423cb562007-10-30 13:30:57 +0000472 SourceLocation endLoc) {
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000473 NumInstanceMethods = numInsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000474 if (numInsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000475 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
476 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000477 }
Fariborz Jahanian7ed9e0f2007-10-02 22:05:16 +0000478 NumClassMethods = numClsMembers;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000479 if (numClsMembers) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000480 ClassMethods = new ObjCMethodDecl*[numClsMembers];
481 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000482 }
Steve Naroff423cb562007-10-30 13:30:57 +0000483 AtEndLoc = endLoc;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000484}
485
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000486ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
487 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
488 ObjCInterfaceDecl* ClassDecl = this;
Steve Naroff03300712007-11-12 13:56:41 +0000489 while (ClassDecl != NULL) {
Chris Lattnerbe6df082007-12-12 07:56:42 +0000490 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
491 I != E; ++I) {
492 if ((*I)->getIdentifier() == ID) {
Steve Naroff03300712007-11-12 13:56:41 +0000493 clsDeclared = ClassDecl;
Chris Lattnerbe6df082007-12-12 07:56:42 +0000494 return *I;
Steve Naroff03300712007-11-12 13:56:41 +0000495 }
496 }
497 ClassDecl = ClassDecl->getSuperClass();
498 }
499 return NULL;
500}
501
Chris Lattner0157c512007-12-12 07:30:05 +0000502/// lookupInstanceMethod - This method returns an instance method by looking in
Chris Lattner33ef2592007-12-12 08:17:45 +0000503/// the class, its categories, and its super classes (using a linear search).
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000504ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
505 ObjCInterfaceDecl* ClassDecl = this;
506 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000507
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000508 while (ClassDecl != NULL) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000509 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000510 return MethodDecl;
511
Steve Naroffff1afdb2007-10-14 23:13:51 +0000512 // Didn't find one yet - look through protocols.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000513 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroffff1afdb2007-10-14 23:13:51 +0000514 int numProtocols = ClassDecl->getNumIntfRefProtocols();
515 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000516 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000517 return MethodDecl;
Steve Naroffff1afdb2007-10-14 23:13:51 +0000518 }
Steve Naroff3d581382007-10-14 18:27:41 +0000519 // Didn't find one yet - now look through categories.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000520 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroff3d581382007-10-14 18:27:41 +0000521 while (CatDecl) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000522 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000523 return MethodDecl;
Steve Naroff3d581382007-10-14 18:27:41 +0000524 CatDecl = CatDecl->getNextClassCategory();
525 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000526 ClassDecl = ClassDecl->getSuperClass();
527 }
528 return NULL;
529}
530
Steve Naroff3d581382007-10-14 18:27:41 +0000531// lookupClassMethod - This method returns a class method by looking in the
Chris Lattner33ef2592007-12-12 08:17:45 +0000532// class, its categories, and its super classes (using a linear search).
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000533ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
534 ObjCInterfaceDecl* ClassDecl = this;
535 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000536
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000537 while (ClassDecl != NULL) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000538 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000539 return MethodDecl;
540
Steve Naroffff1afdb2007-10-14 23:13:51 +0000541 // Didn't find one yet - look through protocols.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000542 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroffff1afdb2007-10-14 23:13:51 +0000543 int numProtocols = ClassDecl->getNumIntfRefProtocols();
544 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000545 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000546 return MethodDecl;
Steve Naroffff1afdb2007-10-14 23:13:51 +0000547 }
Steve Naroff3d581382007-10-14 18:27:41 +0000548 // Didn't find one yet - now look through categories.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000549 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Naroff3d581382007-10-14 18:27:41 +0000550 while (CatDecl) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000551 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000552 return MethodDecl;
Steve Naroff3d581382007-10-14 18:27:41 +0000553 CatDecl = CatDecl->getNextClassCategory();
554 }
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000555 ClassDecl = ClassDecl->getSuperClass();
556 }
557 return NULL;
558}
559
Chris Lattner0157c512007-12-12 07:30:05 +0000560/// lookupInstanceMethod - This method returns an instance method by looking in
561/// the class implementation. Unlike interfaces, we don't look outside the
562/// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000563ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
Chris Lattner0157c512007-12-12 07:30:05 +0000564 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
565 if ((*I)->getSelector() == Sel)
566 return *I;
Steve Naroffc43d8682007-11-11 00:10:47 +0000567 return NULL;
568}
569
Chris Lattner0157c512007-12-12 07:30:05 +0000570/// lookupClassMethod - This method returns a class method by looking in
571/// the class implementation. Unlike interfaces, we don't look outside the
572/// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000573ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000574 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
575 I != E; ++I)
576 if ((*I)->getSelector() == Sel)
577 return *I;
Steve Naroffc43d8682007-11-11 00:10:47 +0000578 return NULL;
579}
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000580
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000581// lookupInstanceMethod - This method returns an instance method by looking in
582// the class implementation. Unlike interfaces, we don't look outside the
583// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000584ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000585 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
586 if ((*I)->getSelector() == Sel)
587 return *I;
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000588 return NULL;
589}
590
591// lookupClassMethod - This method returns an instance method by looking in
592// the class implementation. Unlike interfaces, we don't look outside the
593// implementation.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000594ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
Chris Lattnerab4c4d52007-12-12 07:46:12 +0000595 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
596 I != E; ++I)
597 if ((*I)->getSelector() == Sel)
598 return *I;
Steve Naroffe1e6c0d2007-11-12 22:05:31 +0000599 return NULL;
600}
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000601
602// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
603// it inherited.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000604ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
605 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000606
Steve Naroff94a5c332007-12-19 22:27:04 +0000607 if ((MethodDecl = getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000608 return MethodDecl;
609
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000610 if (getNumReferencedProtocols() > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000611 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000612
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000613 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000614 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000615 return MethodDecl;
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000616 }
617 }
618 return NULL;
619}
620
621// lookupInstanceMethod - Lookup a class method in the protocol and protocols
622// it inherited.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000623ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
624 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000625
Steve Naroff94a5c332007-12-19 22:27:04 +0000626 if ((MethodDecl = getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000627 return MethodDecl;
628
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000629 if (getNumReferencedProtocols() > 0) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000630 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000631
Fariborz Jahanianc395bda2007-12-20 19:24:10 +0000632 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff94a5c332007-12-19 22:27:04 +0000633 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000634 return MethodDecl;
Fariborz Jahanian7dd82832007-12-07 21:21:21 +0000635 }
636 }
637 return NULL;
638}
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000639
Fariborz Jahanianfaf5e772008-01-17 17:37:26 +0000640/// getSynthesizedMethodSize - Compute size of synthesized method name
641/// as done be the rewrite.
642///
643unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
Fariborz Jahanian847794a2008-01-17 01:36:09 +0000644 // syntesized method name is a concatenation of -/+[class-name selector]
645 // Get length of this name.
Fariborz Jahanianfaf5e772008-01-17 17:37:26 +0000646 unsigned length = 3; // _I_ or _C_
647 length += strlen(getClassInterface()->getName()) +1; // extra for _
648 NamedDecl *MethodContext = getMethodContext();
649 if (ObjCCategoryImplDecl *CID =
650 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
651 length += strlen(CID->getName()) +1;
652 length += getSelector().getName().size(); // selector name
Fariborz Jahanian847794a2008-01-17 01:36:09 +0000653 return length;
654}
655
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000656ObjCInterfaceDecl *const ObjCMethodDecl::getClassInterface() const {
657 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000658 return ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000659 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000660 return CD->getClassInterface();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000661 if (ObjCImplementationDecl *IMD =
662 dyn_cast<ObjCImplementationDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000663 return IMD->getClassInterface();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000664 if (ObjCCategoryImplDecl *CID =
665 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000666 return CID->getClassInterface();
667 assert(false && "unknown method context");
668 return 0;
669}