blob: a171589f1358ab8e39e3dd0b6110b71cb0ad1ff4 [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
Chris Lattner48d225c2008-03-15 21:10:16 +0000208BlockVarDecl *BlockVarDecl::Create(SourceLocation L, IdentifierInfo *Id,
209 QualType T, StorageClass S,
210 ScopedDecl *PrevDecl, ASTContext &C) {
211 void *Mem = C.getAllocator().Allocate<BlockVarDecl>();
212 return new (Mem) BlockVarDecl(L, Id, T, S, PrevDecl);
213}
214
215
216FileVarDecl *FileVarDecl::Create(SourceLocation L, IdentifierInfo *Id,
217 QualType T, StorageClass S,
218 ScopedDecl *PrevDecl, ASTContext &C) {
219 void *Mem = C.getAllocator().Allocate<FileVarDecl>();
220 return new (Mem) FileVarDecl(L, Id, T, S, PrevDecl);
221}
222
223ParmVarDecl *ParmVarDecl::Create(SourceLocation L, IdentifierInfo *Id,
224 QualType T, StorageClass S,
225 ScopedDecl *PrevDecl, ASTContext &C) {
226 void *Mem = C.getAllocator().Allocate<ParmVarDecl>();
227 return new (Mem) ParmVarDecl(L, Id, T, S, PrevDecl);
228}
229
Chris Lattnere4650482008-03-15 06:12:44 +0000230EnumConstantDecl *EnumConstantDecl::Create(SourceLocation L, IdentifierInfo *Id,
231 QualType T, Expr *E,
232 const llvm::APSInt &V,
233 ScopedDecl *PrevDecl, ASTContext &C){
234 void *Mem = C.getAllocator().Allocate<EnumConstantDecl>();
235 return new (Mem) EnumConstantDecl(L, Id, T, E, V, PrevDecl);
236}
237
238TypedefDecl *TypedefDecl::Create(SourceLocation L, IdentifierInfo *Id,
239 QualType T, ScopedDecl *PD, ASTContext &C) {
240 void *Mem = C.getAllocator().Allocate<TypedefDecl>();
241 return new (Mem) TypedefDecl(L, Id, T, PD);
242}
243
244EnumDecl *EnumDecl::Create(SourceLocation L, IdentifierInfo *Id,
245 ScopedDecl *PrevDecl, ASTContext &C) {
246 void *Mem = C.getAllocator().Allocate<EnumDecl>();
247 return new (Mem) EnumDecl(L, Id, PrevDecl);
248}
249
250RecordDecl *RecordDecl::Create(Kind DK, SourceLocation L, IdentifierInfo *Id,
251 ScopedDecl *PrevDecl, ASTContext &C) {
252 void *Mem = C.getAllocator().Allocate<RecordDecl>();
253 return new (Mem) RecordDecl(DK, L, Id, PrevDecl);
254}
255
256
257//===----------------------------------------------------------------------===//
Chris Lattnera8344c32008-03-15 05:43:15 +0000258// Decl Implementation
259//===----------------------------------------------------------------------===//
260
Chris Lattner4b009652007-07-25 00:24:17 +0000261// Out-of-line virtual method providing a home for Decl.
262Decl::~Decl() {
Anders Carlssonb610a992008-02-16 03:37:41 +0000263 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000264 return;
265
266 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
Anders Carlssonb610a992008-02-16 03:37:41 +0000267 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
268
269 delete it->second;
270 DeclAttrs->erase(it);
271 if (DeclAttrs->empty()) {
272 delete DeclAttrs;
273 DeclAttrs = 0;
274 }
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000275}
276
Chris Lattnera8344c32008-03-15 05:43:15 +0000277void Decl::addAttr(Attr *NewAttr) {
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000278 if (!DeclAttrs)
Chris Lattnera8344c32008-03-15 05:43:15 +0000279 DeclAttrs = new llvm::DenseMap<const Decl*, Attr*>();
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000280
Chris Lattnera8344c32008-03-15 05:43:15 +0000281 Attr *&ExistingAttr = (*DeclAttrs)[this];
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000282
Chris Lattnera8344c32008-03-15 05:43:15 +0000283 NewAttr->setNext(ExistingAttr);
284 ExistingAttr = NewAttr;
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000285
286 HasAttrs = true;
287}
288
Chris Lattnera8344c32008-03-15 05:43:15 +0000289const Attr *Decl::getAttrs() const {
Anders Carlssonb610a992008-02-16 03:37:41 +0000290 if (!HasAttrs)
Anders Carlsson484eb5f2008-02-15 23:30:50 +0000291 return 0;
292
Anders Carlssonb610a992008-02-16 03:37:41 +0000293 return (*DeclAttrs)[this];
Chris Lattner4b009652007-07-25 00:24:17 +0000294}
295
Chris Lattner910435b2007-10-06 22:53:46 +0000296const char *NamedDecl::getName() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000297 if (const IdentifierInfo *II = getIdentifier())
298 return II->getName();
299 return "";
300}
301
Chris Lattner4b009652007-07-25 00:24:17 +0000302FunctionDecl::~FunctionDecl() {
303 delete[] ParamInfo;
304}
305
306unsigned FunctionDecl::getNumParams() const {
Chris Lattnera8344c32008-03-15 05:43:15 +0000307 if (isa<FunctionTypeNoProto>(getCanonicalType()))
308 return 0;
Chris Lattner23d411b2007-12-06 17:20:20 +0000309 return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs();
Chris Lattner4b009652007-07-25 00:24:17 +0000310}
311
312void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
313 assert(ParamInfo == 0 && "Already has param info!");
314 assert(NumParams == getNumParams() && "Parameter count mismatch!");
315
316 // Zero params -> null pointer.
317 if (NumParams) {
318 ParamInfo = new ParmVarDecl*[NumParams];
319 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
320 }
321}
322
323
324/// defineBody - When created, RecordDecl's correspond to a forward declared
325/// record. This method is used to mark the decl as being defined, with the
326/// specified contents.
327void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) {
328 assert(!isDefinition() && "Cannot redefine record!");
329 setDefinition(true);
330 NumMembers = numMembers;
331 if (numMembers) {
332 Members = new FieldDecl*[numMembers];
333 memcpy(Members, members, numMembers*sizeof(Decl*));
334 }
335}
336
337FieldDecl* RecordDecl::getMember(IdentifierInfo *name) {
338 if (Members == 0 || NumMembers < 0)
339 return 0;
Fariborz Jahaniancbc36d42007-10-04 00:45:27 +0000340
Chris Lattner4b009652007-07-25 00:24:17 +0000341 // linear search. When C++ classes come along, will likely need to revisit.
342 for (int i = 0; i < NumMembers; ++i) {
343 if (Members[i]->getIdentifier() == name)
344 return Members[i];
345 }
346 return 0;
347}
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000348
Chris Lattnera8344c32008-03-15 05:43:15 +0000349//===----------------------------------------------------------------------===//
350// Objective-C Decl Implementation
351//===----------------------------------------------------------------------===//
352
Ted Kremenek42730c52008-01-07 19:49:32 +0000353void ObjCMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo,
Fariborz Jahanian84082af2007-10-04 17:06:28 +0000354 unsigned NumParams) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000355 assert(ParamInfo == 0 && "Already has param info!");
356
357 // Zero params -> null pointer.
358 if (NumParams) {
359 ParamInfo = new ParmVarDecl*[NumParams];
360 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
361 NumMethodParams = NumParams;
362 }
363}
364
Ted Kremenek42730c52008-01-07 19:49:32 +0000365ObjCMethodDecl::~ObjCMethodDecl() {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000366 delete[] ParamInfo;
367}
368
Ted Kremenek42730c52008-01-07 19:49:32 +0000369/// ObjCAddInstanceVariablesToClass - Inserts instance variables
370/// into ObjCInterfaceDecl's fields.
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000371///
Ted Kremenek42730c52008-01-07 19:49:32 +0000372void ObjCInterfaceDecl::addInstanceVariablesToClass(ObjCIvarDecl **ivars,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000373 unsigned numIvars,
Steve Naroffef20ed32007-10-30 02:23:23 +0000374 SourceLocation RBrac) {
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000375 NumIvars = numIvars;
376 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000377 Ivars = new ObjCIvarDecl*[numIvars];
378 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000379 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000380 setLocEnd(RBrac);
Fariborz Jahanianebcc9b62007-09-14 21:08:27 +0000381}
382
Ted Kremenek42730c52008-01-07 19:49:32 +0000383/// ObjCAddInstanceVariablesToClassImpl - Checks for correctness of Instance
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000384/// Variables (Ivars) relative to what declared in @implementation;s class.
Ted Kremenek42730c52008-01-07 19:49:32 +0000385/// Ivars into ObjCImplementationDecl's fields.
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000386///
Ted Kremenek42730c52008-01-07 19:49:32 +0000387void ObjCImplementationDecl::ObjCAddInstanceVariablesToClassImpl(
388 ObjCIvarDecl **ivars, unsigned numIvars) {
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000389 NumIvars = numIvars;
390 if (numIvars) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000391 Ivars = new ObjCIvarDecl*[numIvars];
392 memcpy(Ivars, ivars, numIvars*sizeof(ObjCIvarDecl*));
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000393 }
394}
395
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000396/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000397/// ObjCInterfaceDecl's InsMethods and ClsMethods fields.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000398///
Ted Kremenek42730c52008-01-07 19:49:32 +0000399void ObjCInterfaceDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000400 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000401 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000402 unsigned numClsMembers,
403 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000404 NumInstanceMethods = numInsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000405 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000406 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
407 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000408 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000409 NumClassMethods = numClsMembers;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000410 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000411 ClassMethods = new ObjCMethodDecl*[numClsMembers];
412 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000413 }
Steve Naroffef20ed32007-10-30 02:23:23 +0000414 AtEndLoc = endLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000415}
416
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000417/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000418/// ObjCProtocolDecl's ProtoInsMethods and ProtoClsMethods fields.
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000419///
Ted Kremenek42730c52008-01-07 19:49:32 +0000420void ObjCProtocolDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000421 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000422 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000423 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000424 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000425 NumInstanceMethods = numInsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000426 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000427 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
428 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000429 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000430 NumClassMethods = numClsMembers;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000431 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000432 ClassMethods = new ObjCMethodDecl*[numClsMembers];
433 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000434 }
Steve Naroff667f1682007-10-30 13:30:57 +0000435 AtEndLoc = endLoc;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000436}
437
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000438/// addMethods - Insert instance and methods declarations into
Ted Kremenek42730c52008-01-07 19:49:32 +0000439/// ObjCCategoryDecl's CatInsMethods and CatClsMethods fields.
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000440///
Ted Kremenek42730c52008-01-07 19:49:32 +0000441void ObjCCategoryDecl::addMethods(ObjCMethodDecl **insMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000442 unsigned numInsMembers,
Ted Kremenek42730c52008-01-07 19:49:32 +0000443 ObjCMethodDecl **clsMethods,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000444 unsigned numClsMembers,
Steve Naroff667f1682007-10-30 13:30:57 +0000445 SourceLocation endLoc) {
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000446 NumInstanceMethods = numInsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000447 if (numInsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000448 InstanceMethods = new ObjCMethodDecl*[numInsMembers];
449 memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000450 }
Fariborz Jahanian1c095a72007-10-02 22:05:16 +0000451 NumClassMethods = numClsMembers;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000452 if (numClsMembers) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000453 ClassMethods = new ObjCMethodDecl*[numClsMembers];
454 memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjCMethodDecl*));
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000455 }
Steve Naroff667f1682007-10-30 13:30:57 +0000456 AtEndLoc = endLoc;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000457}
458
Ted Kremenek42730c52008-01-07 19:49:32 +0000459ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
460 IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
461 ObjCInterfaceDecl* ClassDecl = this;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000462 while (ClassDecl != NULL) {
Chris Lattnerc7b06752007-12-12 07:56:42 +0000463 for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
464 I != E; ++I) {
465 if ((*I)->getIdentifier() == ID) {
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000466 clsDeclared = ClassDecl;
Chris Lattnerc7b06752007-12-12 07:56:42 +0000467 return *I;
Steve Naroffdd2e26c2007-11-12 13:56:41 +0000468 }
469 }
470 ClassDecl = ClassDecl->getSuperClass();
471 }
472 return NULL;
473}
474
Chris Lattnerac9ab532007-12-12 07:30:05 +0000475/// lookupInstanceMethod - This method returns an instance method by looking in
Chris Lattner73e795c2007-12-12 08:17:45 +0000476/// the class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000477ObjCMethodDecl *ObjCInterfaceDecl::lookupInstanceMethod(Selector Sel) {
478 ObjCInterfaceDecl* ClassDecl = this;
479 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000480
Steve Narofffa465d12007-10-02 20:01:56 +0000481 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000482 if ((MethodDecl = ClassDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000483 return MethodDecl;
484
Steve Naroff705380b2007-10-14 23:13:51 +0000485 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000486 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000487 int numProtocols = ClassDecl->getNumIntfRefProtocols();
488 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000489 if ((MethodDecl = protocols[pIdx]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000490 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000491 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000492 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000493 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000494 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000495 if ((MethodDecl = CatDecl->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000496 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000497 CatDecl = CatDecl->getNextClassCategory();
498 }
Steve Narofffa465d12007-10-02 20:01:56 +0000499 ClassDecl = ClassDecl->getSuperClass();
500 }
501 return NULL;
502}
503
Steve Narofff66d84a2007-10-14 18:27:41 +0000504// lookupClassMethod - This method returns a class method by looking in the
Chris Lattner73e795c2007-12-12 08:17:45 +0000505// class, its categories, and its super classes (using a linear search).
Ted Kremenek42730c52008-01-07 19:49:32 +0000506ObjCMethodDecl *ObjCInterfaceDecl::lookupClassMethod(Selector Sel) {
507 ObjCInterfaceDecl* ClassDecl = this;
508 ObjCMethodDecl *MethodDecl = 0;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000509
Steve Narofffa465d12007-10-02 20:01:56 +0000510 while (ClassDecl != NULL) {
Steve Naroff74273de2007-12-19 22:27:04 +0000511 if ((MethodDecl = ClassDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000512 return MethodDecl;
513
Steve Naroff705380b2007-10-14 23:13:51 +0000514 // Didn't find one yet - look through protocols.
Ted Kremenek42730c52008-01-07 19:49:32 +0000515 ObjCProtocolDecl **protocols = ClassDecl->getReferencedProtocols();
Steve Naroff705380b2007-10-14 23:13:51 +0000516 int numProtocols = ClassDecl->getNumIntfRefProtocols();
517 for (int pIdx = 0; pIdx < numProtocols; pIdx++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000518 if ((MethodDecl = protocols[pIdx]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000519 return MethodDecl;
Steve Naroff705380b2007-10-14 23:13:51 +0000520 }
Steve Narofff66d84a2007-10-14 18:27:41 +0000521 // Didn't find one yet - now look through categories.
Ted Kremenek42730c52008-01-07 19:49:32 +0000522 ObjCCategoryDecl *CatDecl = ClassDecl->getCategoryList();
Steve Narofff66d84a2007-10-14 18:27:41 +0000523 while (CatDecl) {
Steve Naroff74273de2007-12-19 22:27:04 +0000524 if ((MethodDecl = CatDecl->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000525 return MethodDecl;
Steve Narofff66d84a2007-10-14 18:27:41 +0000526 CatDecl = CatDecl->getNextClassCategory();
527 }
Steve Narofffa465d12007-10-02 20:01:56 +0000528 ClassDecl = ClassDecl->getSuperClass();
529 }
530 return NULL;
531}
532
Chris Lattnerac9ab532007-12-12 07:30:05 +0000533/// lookupInstanceMethod - This method returns an instance method by looking in
534/// the class implementation. Unlike interfaces, we don't look outside the
535/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000536ObjCMethodDecl *ObjCImplementationDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerac9ab532007-12-12 07:30:05 +0000537 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
538 if ((*I)->getSelector() == Sel)
539 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000540 return NULL;
541}
542
Chris Lattnerac9ab532007-12-12 07:30:05 +0000543/// lookupClassMethod - This method returns a class method by looking in
544/// the class implementation. Unlike interfaces, we don't look outside the
545/// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000546ObjCMethodDecl *ObjCImplementationDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000547 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
548 I != E; ++I)
549 if ((*I)->getSelector() == Sel)
550 return *I;
Steve Naroffb1c7ad92007-11-11 00:10:47 +0000551 return NULL;
552}
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000553
Steve Naroff31233632007-11-12 22:05:31 +0000554// lookupInstanceMethod - This method returns an instance method by looking in
555// the class implementation. Unlike interfaces, we don't look outside the
556// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000557ObjCMethodDecl *ObjCCategoryImplDecl::getInstanceMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000558 for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I)
559 if ((*I)->getSelector() == Sel)
560 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000561 return NULL;
562}
563
564// lookupClassMethod - This method returns an instance method by looking in
565// the class implementation. Unlike interfaces, we don't look outside the
566// implementation.
Ted Kremenek42730c52008-01-07 19:49:32 +0000567ObjCMethodDecl *ObjCCategoryImplDecl::getClassMethod(Selector Sel) {
Chris Lattnerdea5bec2007-12-12 07:46:12 +0000568 for (classmeth_iterator I = classmeth_begin(), E = classmeth_end();
569 I != E; ++I)
570 if ((*I)->getSelector() == Sel)
571 return *I;
Steve Naroff31233632007-11-12 22:05:31 +0000572 return NULL;
573}
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000574
575// lookupInstanceMethod - Lookup a instance method in the protocol and protocols
576// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000577ObjCMethodDecl *ObjCProtocolDecl::lookupInstanceMethod(Selector Sel) {
578 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000579
Steve Naroff74273de2007-12-19 22:27:04 +0000580 if ((MethodDecl = getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000581 return MethodDecl;
582
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000583 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000584 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000585
Fariborz Jahanian87829072007-12-20 19:24:10 +0000586 for (unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000587 if ((MethodDecl = RefPDecl[i]->getInstanceMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000588 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000589 }
590 }
591 return NULL;
592}
593
594// lookupInstanceMethod - Lookup a class method in the protocol and protocols
595// it inherited.
Ted Kremenek42730c52008-01-07 19:49:32 +0000596ObjCMethodDecl *ObjCProtocolDecl::lookupClassMethod(Selector Sel) {
597 ObjCMethodDecl *MethodDecl = NULL;
Steve Naroff2ce399a2007-12-14 23:37:57 +0000598
Steve Naroff74273de2007-12-19 22:27:04 +0000599 if ((MethodDecl = getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000600 return MethodDecl;
601
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000602 if (getNumReferencedProtocols() > 0) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000603 ObjCProtocolDecl **RefPDecl = getReferencedProtocols();
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000604
Fariborz Jahanian87829072007-12-20 19:24:10 +0000605 for(unsigned i = 0; i < getNumReferencedProtocols(); i++) {
Steve Naroff74273de2007-12-19 22:27:04 +0000606 if ((MethodDecl = RefPDecl[i]->getClassMethod(Sel)))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000607 return MethodDecl;
Fariborz Jahanianbe4283c2007-12-07 21:21:21 +0000608 }
609 }
610 return NULL;
611}
Steve Naroff2ce399a2007-12-14 23:37:57 +0000612
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000613/// getSynthesizedMethodSize - Compute size of synthesized method name
614/// as done be the rewrite.
615///
616unsigned ObjCMethodDecl::getSynthesizedMethodSize() const {
Fariborz Jahanian4aaf0c92008-01-17 01:36:09 +0000617 // syntesized method name is a concatenation of -/+[class-name selector]
618 // Get length of this name.
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000619 unsigned length = 3; // _I_ or _C_
620 length += strlen(getClassInterface()->getName()) +1; // extra for _
621 NamedDecl *MethodContext = getMethodContext();
622 if (ObjCCategoryImplDecl *CID =
623 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
624 length += strlen(CID->getName()) +1;
625 length += getSelector().getName().size(); // selector name
Fariborz Jahanian4aaf0c92008-01-17 01:36:09 +0000626 return length;
627}
628
Ted Kremenek42730c52008-01-07 19:49:32 +0000629ObjCInterfaceDecl *const ObjCMethodDecl::getClassInterface() const {
630 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000631 return ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000632 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000633 return CD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000634 if (ObjCImplementationDecl *IMD =
635 dyn_cast<ObjCImplementationDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000636 return IMD->getClassInterface();
Ted Kremenek42730c52008-01-07 19:49:32 +0000637 if (ObjCCategoryImplDecl *CID =
638 dyn_cast<ObjCCategoryImplDecl>(MethodContext))
Steve Naroff2ce399a2007-12-14 23:37:57 +0000639 return CID->getClassInterface();
640 assert(false && "unknown method context");
641 return 0;
642}