Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Decl class and subclasses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 16 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
| 19 | // temporary statistics gathering |
| 20 | static unsigned nFuncs = 0; |
| 21 | static unsigned nBlockVars = 0; |
| 22 | static unsigned nFileVars = 0; |
| 23 | static unsigned nParmVars = 0; |
| 24 | static unsigned nSUC = 0; |
| 25 | static unsigned nEnumConst = 0; |
| 26 | static unsigned nEnumDecls = 0; |
| 27 | static unsigned nTypedef = 0; |
| 28 | static unsigned nFieldDecls = 0; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 29 | static unsigned nInterfaceDecls = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 30 | static unsigned nClassDecls = 0; |
| 31 | static unsigned nMethodDecls = 0; |
| 32 | static unsigned nProtocolDecls = 0; |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 33 | static unsigned nForwardProtocolDecls = 0; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 34 | static unsigned nCategoryDecls = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 35 | static unsigned nIvarDecls = 0; |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 36 | static unsigned nObjcImplementationDecls = 0; |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 37 | static unsigned nObjcCategoryImpl = 0; |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 38 | static unsigned nObjcCompatibleAlias = 0; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 39 | static unsigned nObjcPropertyDecl = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 40 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | static bool StatSwitch = false; |
| 42 | |
Steve Naroff | e5ea380 | 2007-09-17 14:49:06 +0000 | [diff] [blame] | 43 | const char *Decl::getDeclKindName() const { |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 44 | switch (DeclKind) { |
| 45 | default: assert(0 && "Unknown decl kind!"); |
| 46 | case Typedef: |
| 47 | return "Typedef"; |
| 48 | case Function: |
| 49 | return "Function"; |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 50 | case BlockVar: |
| 51 | return "BlockVar"; |
| 52 | case FileVar: |
| 53 | return "FileVar"; |
| 54 | case ParmVar: |
| 55 | return "ParmVar"; |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 56 | case EnumConstant: |
| 57 | return "EnumConstant"; |
| 58 | case ObjcInterface: |
| 59 | return "ObjcInterface"; |
| 60 | case ObjcClass: |
| 61 | return "ObjcClass"; |
| 62 | case ObjcMethod: |
| 63 | return "ObjcMethod"; |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 64 | case ObjcProtocol: |
| 65 | return "ObjcProtocol"; |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 66 | case ObjcForwardProtocol: |
| 67 | return "ObjcForwardProtocol"; |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 68 | case Struct: |
| 69 | return "Struct"; |
| 70 | case Union: |
| 71 | return "Union"; |
| 72 | case Class: |
| 73 | return "Class"; |
| 74 | case Enum: |
| 75 | return "Enum"; |
| 76 | } |
| 77 | } |
| 78 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | bool Decl::CollectingStats(bool enable) { |
| 80 | if (enable) StatSwitch = true; |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 81 | return StatSwitch; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void Decl::PrintStats() { |
| 85 | fprintf(stderr, "*** Decl Stats:\n"); |
| 86 | fprintf(stderr, " %d decls total.\n", |
| 87 | int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+ |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 88 | nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+ |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 89 | nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | fprintf(stderr, " %d function decls, %d each (%d bytes)\n", |
| 91 | nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl))); |
| 92 | fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n", |
| 93 | nBlockVars, (int)sizeof(BlockVarDecl), |
| 94 | int(nBlockVars*sizeof(BlockVarDecl))); |
| 95 | fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n", |
| 96 | nFileVars, (int)sizeof(FileVarDecl), |
| 97 | int(nFileVars*sizeof(FileVarDecl))); |
| 98 | fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n", |
| 99 | nParmVars, (int)sizeof(ParmVarDecl), |
| 100 | int(nParmVars*sizeof(ParmVarDecl))); |
| 101 | fprintf(stderr, " %d field decls, %d each (%d bytes)\n", |
| 102 | nFieldDecls, (int)sizeof(FieldDecl), |
| 103 | int(nFieldDecls*sizeof(FieldDecl))); |
| 104 | fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n", |
| 105 | nSUC, (int)sizeof(RecordDecl), |
| 106 | int(nSUC*sizeof(RecordDecl))); |
| 107 | fprintf(stderr, " %d enum decls, %d each (%d bytes)\n", |
| 108 | nEnumDecls, (int)sizeof(EnumDecl), |
| 109 | int(nEnumDecls*sizeof(EnumDecl))); |
| 110 | fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n", |
| 111 | nEnumConst, (int)sizeof(EnumConstantDecl), |
| 112 | int(nEnumConst*sizeof(EnumConstantDecl))); |
| 113 | fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n", |
| 114 | nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 115 | // Objective-C decls... |
| 116 | fprintf(stderr, " %d interface decls, %d each (%d bytes)\n", |
| 117 | nInterfaceDecls, (int)sizeof(ObjcInterfaceDecl), |
| 118 | int(nInterfaceDecls*sizeof(ObjcInterfaceDecl))); |
| 119 | fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n", |
| 120 | nIvarDecls, (int)sizeof(ObjcIvarDecl), |
| 121 | int(nIvarDecls*sizeof(ObjcIvarDecl))); |
| 122 | fprintf(stderr, " %d class decls, %d each (%d bytes)\n", |
| 123 | nClassDecls, (int)sizeof(ObjcClassDecl), |
| 124 | int(nClassDecls*sizeof(ObjcClassDecl))); |
| 125 | fprintf(stderr, " %d method decls, %d each (%d bytes)\n", |
| 126 | nMethodDecls, (int)sizeof(ObjcMethodDecl), |
| 127 | int(nMethodDecls*sizeof(ObjcMethodDecl))); |
| 128 | fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n", |
| 129 | nProtocolDecls, (int)sizeof(ObjcProtocolDecl), |
| 130 | int(nProtocolDecls*sizeof(ObjcProtocolDecl))); |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 131 | fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n", |
| 132 | nForwardProtocolDecls, (int)sizeof(ObjcForwardProtocolDecl), |
| 133 | int(nForwardProtocolDecls*sizeof(ObjcForwardProtocolDecl))); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 134 | fprintf(stderr, " %d category decls, %d each (%d bytes)\n", |
| 135 | nCategoryDecls, (int)sizeof(ObjcCategoryDecl), |
| 136 | int(nCategoryDecls*sizeof(ObjcCategoryDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 137 | |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 138 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
| 139 | nObjcImplementationDecls, (int)sizeof(ObjcImplementationDecl), |
| 140 | int(nObjcImplementationDecls*sizeof(ObjcImplementationDecl))); |
| 141 | |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 142 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
| 143 | nObjcCategoryImpl, (int)sizeof(ObjcCategoryImplDecl), |
| 144 | int(nObjcCategoryImpl*sizeof(ObjcCategoryImplDecl))); |
| 145 | |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 146 | fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n", |
| 147 | nObjcCompatibleAlias, (int)sizeof(ObjcCompatibleAliasDecl), |
| 148 | int(nObjcCompatibleAlias*sizeof(ObjcCompatibleAliasDecl))); |
| 149 | |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 150 | fprintf(stderr, " %d property decls, %d each (%d bytes)\n", |
| 151 | nObjcPropertyDecl, (int)sizeof(ObjcPropertyDecl), |
| 152 | int(nObjcPropertyDecl*sizeof(ObjcPropertyDecl))); |
| 153 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 154 | fprintf(stderr, "Total bytes = %d\n", |
| 155 | int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+ |
| 156 | nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+ |
| 157 | nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+ |
| 158 | nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+ |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 159 | nTypedef*sizeof(TypedefDecl)) /* FIXME: add Objc decls */); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void Decl::addDeclKind(const Kind k) { |
| 163 | switch (k) { |
| 164 | case Typedef: |
| 165 | nTypedef++; |
| 166 | break; |
| 167 | case Function: |
| 168 | nFuncs++; |
| 169 | break; |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 170 | case BlockVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | nBlockVars++; |
| 172 | break; |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 173 | case FileVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | nFileVars++; |
| 175 | break; |
Chris Lattner | aa9fc46 | 2007-10-08 21:37:32 +0000 | [diff] [blame] | 176 | case ParmVar: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | nParmVars++; |
| 178 | break; |
| 179 | case EnumConstant: |
| 180 | nEnumConst++; |
| 181 | break; |
| 182 | case Field: |
| 183 | nFieldDecls++; |
| 184 | break; |
| 185 | case Struct: |
| 186 | case Union: |
| 187 | case Class: |
| 188 | nSUC++; |
| 189 | break; |
| 190 | case Enum: |
| 191 | nEnumDecls++; |
| 192 | break; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 193 | case ObjcInterface: |
| 194 | nInterfaceDecls++; |
| 195 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 196 | case ObjcClass: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 197 | nClassDecls++; |
| 198 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 199 | case ObjcMethod: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 200 | nMethodDecls++; |
| 201 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 202 | case ObjcProtocol: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 203 | nProtocolDecls++; |
| 204 | break; |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 205 | case ObjcForwardProtocol: |
| 206 | nForwardProtocolDecls++; |
| 207 | break; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 208 | case ObjcCategory: |
| 209 | nCategoryDecls++; |
| 210 | break; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 211 | case ObjcIvar: |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 212 | nIvarDecls++; |
Chris Lattner | 7341c33 | 2007-09-16 19:23:04 +0000 | [diff] [blame] | 213 | break; |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 214 | case ObjcImplementation: |
| 215 | nObjcImplementationDecls++; |
| 216 | break; |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 217 | case ObjcCategoryImpl: |
| 218 | nObjcCategoryImpl++; |
| 219 | break; |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 220 | case CompatibleAlias: |
| 221 | nObjcCompatibleAlias++; |
| 222 | break; |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 223 | case PropertyDecl: |
| 224 | nObjcPropertyDecl++; |
| 225 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | // Out-of-line virtual method providing a home for Decl. |
| 230 | Decl::~Decl() { |
| 231 | } |
| 232 | |
Chris Lattner | fd5de47 | 2007-10-06 22:53:46 +0000 | [diff] [blame] | 233 | const char *NamedDecl::getName() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | if (const IdentifierInfo *II = getIdentifier()) |
| 235 | return II->getName(); |
| 236 | return ""; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | FunctionDecl::~FunctionDecl() { |
| 241 | delete[] ParamInfo; |
| 242 | } |
| 243 | |
| 244 | unsigned FunctionDecl::getNumParams() const { |
Chris Lattner | ec584d6 | 2007-12-06 17:20:20 +0000 | [diff] [blame] | 245 | if (isa<FunctionTypeNoProto>(getCanonicalType())) return 0; |
| 246 | return cast<FunctionTypeProto>(getCanonicalType())->getNumArgs(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 250 | assert(ParamInfo == 0 && "Already has param info!"); |
| 251 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
| 252 | |
| 253 | // Zero params -> null pointer. |
| 254 | if (NumParams) { |
| 255 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 256 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | |
| 261 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 262 | /// record. This method is used to mark the decl as being defined, with the |
| 263 | /// specified contents. |
| 264 | void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) { |
| 265 | assert(!isDefinition() && "Cannot redefine record!"); |
| 266 | setDefinition(true); |
| 267 | NumMembers = numMembers; |
| 268 | if (numMembers) { |
| 269 | Members = new FieldDecl*[numMembers]; |
| 270 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | FieldDecl* RecordDecl::getMember(IdentifierInfo *name) { |
| 275 | if (Members == 0 || NumMembers < 0) |
| 276 | return 0; |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 277 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 278 | // linear search. When C++ classes come along, will likely need to revisit. |
| 279 | for (int i = 0; i < NumMembers; ++i) { |
| 280 | if (Members[i]->getIdentifier() == name) |
| 281 | return Members[i]; |
| 282 | } |
| 283 | return 0; |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 284 | } |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 285 | |
Fariborz Jahanian | 5951965 | 2007-10-04 17:06:28 +0000 | [diff] [blame] | 286 | void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo, |
| 287 | unsigned NumParams) { |
Fariborz Jahanian | e55cd00 | 2007-09-12 18:23:47 +0000 | [diff] [blame] | 288 | assert(ParamInfo == 0 && "Already has param info!"); |
| 289 | |
| 290 | // Zero params -> null pointer. |
| 291 | if (NumParams) { |
| 292 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 293 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 294 | NumMethodParams = NumParams; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | ObjcMethodDecl::~ObjcMethodDecl() { |
| 299 | delete[] ParamInfo; |
| 300 | } |
| 301 | |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 302 | /// ObjcAddInstanceVariablesToClass - Inserts instance variables |
| 303 | /// into ObjcInterfaceDecl's fields. |
| 304 | /// |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 305 | void ObjcInterfaceDecl::addInstanceVariablesToClass(ObjcIvarDecl **ivars, |
| 306 | unsigned numIvars, |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 307 | SourceLocation RBrac) { |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 308 | NumIvars = numIvars; |
| 309 | if (numIvars) { |
| 310 | Ivars = new ObjcIvarDecl*[numIvars]; |
| 311 | memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*)); |
| 312 | } |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 313 | setLocEnd(RBrac); |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 316 | /// ObjcAddInstanceVariablesToClassImpl - Checks for correctness of Instance |
| 317 | /// Variables (Ivars) relative to what declared in @implementation;s class. |
| 318 | /// Ivars into ObjcImplementationDecl's fields. |
| 319 | /// |
| 320 | void ObjcImplementationDecl::ObjcAddInstanceVariablesToClassImpl( |
Fariborz Jahanian | 5951965 | 2007-10-04 17:06:28 +0000 | [diff] [blame] | 321 | ObjcIvarDecl **ivars, unsigned numIvars) { |
Fariborz Jahanian | d0b90bf | 2007-09-26 18:27:25 +0000 | [diff] [blame] | 322 | NumIvars = numIvars; |
| 323 | if (numIvars) { |
| 324 | Ivars = new ObjcIvarDecl*[numIvars]; |
| 325 | memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*)); |
| 326 | } |
| 327 | } |
| 328 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 329 | /// addMethods - Insert instance and methods declarations into |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 330 | /// ObjcInterfaceDecl's InsMethods and ClsMethods fields. |
| 331 | /// |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 332 | void ObjcInterfaceDecl::addMethods(ObjcMethodDecl **insMethods, |
| 333 | unsigned numInsMembers, |
| 334 | ObjcMethodDecl **clsMethods, |
| 335 | unsigned numClsMembers, |
| 336 | SourceLocation endLoc) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 337 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 338 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 339 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 340 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 341 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 342 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 343 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 344 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 345 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 346 | } |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 347 | AtEndLoc = endLoc; |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 350 | /// addMethods - Insert instance and methods declarations into |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 351 | /// ObjcProtocolDecl's ProtoInsMethods and ProtoClsMethods fields. |
| 352 | /// |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 353 | void ObjcProtocolDecl::addMethods(ObjcMethodDecl **insMethods, |
| 354 | unsigned numInsMembers, |
| 355 | ObjcMethodDecl **clsMethods, |
| 356 | unsigned numClsMembers, |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 357 | SourceLocation endLoc) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 358 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 359 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 360 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 361 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 362 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 363 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 364 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 365 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 366 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 367 | } |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 368 | AtEndLoc = endLoc; |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 371 | /// addMethods - Insert instance and methods declarations into |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 372 | /// ObjcCategoryDecl's CatInsMethods and CatClsMethods fields. |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 373 | /// |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 374 | void ObjcCategoryDecl::addMethods(ObjcMethodDecl **insMethods, |
| 375 | unsigned numInsMembers, |
| 376 | ObjcMethodDecl **clsMethods, |
| 377 | unsigned numClsMembers, |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 378 | SourceLocation endLoc) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 379 | NumInstanceMethods = numInsMembers; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 380 | if (numInsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 381 | InstanceMethods = new ObjcMethodDecl*[numInsMembers]; |
| 382 | memcpy(InstanceMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 383 | } |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 384 | NumClassMethods = numClsMembers; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 385 | if (numClsMembers) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 386 | ClassMethods = new ObjcMethodDecl*[numClsMembers]; |
| 387 | memcpy(ClassMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 388 | } |
Steve Naroff | 423cb56 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 389 | AtEndLoc = endLoc; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Steve Naroff | 0330071 | 2007-11-12 13:56:41 +0000 | [diff] [blame] | 392 | ObjcIvarDecl *ObjcInterfaceDecl::lookupInstanceVariable( |
| 393 | IdentifierInfo *ID, ObjcInterfaceDecl *&clsDeclared) { |
| 394 | ObjcInterfaceDecl* ClassDecl = this; |
| 395 | while (ClassDecl != NULL) { |
| 396 | ObjcIvarDecl **ivars = ClassDecl->getInstanceVariables(); |
| 397 | int ivarCount = ClassDecl->getNumInstanceVariables(); |
| 398 | for (int i = 0; i < ivarCount; ++i) { |
| 399 | if (ivars[i]->getIdentifier() == ID) { |
| 400 | clsDeclared = ClassDecl; |
| 401 | return ivars[i]; |
| 402 | } |
| 403 | } |
| 404 | ClassDecl = ClassDecl->getSuperClass(); |
| 405 | } |
| 406 | return NULL; |
| 407 | } |
| 408 | |
Chris Lattner | 0157c51 | 2007-12-12 07:30:05 +0000 | [diff] [blame^] | 409 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 410 | /// the class, it's categories, and it's super classes (using a linear search). |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 411 | ObjcMethodDecl *ObjcInterfaceDecl::lookupInstanceMethod(Selector &Sel) { |
| 412 | ObjcInterfaceDecl* ClassDecl = this; |
| 413 | while (ClassDecl != NULL) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 414 | ObjcMethodDecl **methods = ClassDecl->getInstanceMethods(); |
| 415 | int methodCount = ClassDecl->getNumInstanceMethods(); |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 416 | for (int i = 0; i < methodCount; ++i) { |
| 417 | if (methods[i]->getSelector() == Sel) { |
| 418 | return methods[i]; |
| 419 | } |
| 420 | } |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame] | 421 | // Didn't find one yet - look through protocols. |
| 422 | ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 423 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 424 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 425 | ObjcMethodDecl **methods = protocols[pIdx]->getInstanceMethods(); |
| 426 | int methodCount = protocols[pIdx]->getNumInstanceMethods(); |
| 427 | for (int i = 0; i < methodCount; ++i) { |
| 428 | if (methods[i]->getSelector() == Sel) { |
| 429 | return methods[i]; |
| 430 | } |
| 431 | } |
| 432 | } |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 433 | // Didn't find one yet - now look through categories. |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame] | 434 | ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 435 | while (CatDecl) { |
| 436 | ObjcMethodDecl **methods = CatDecl->getInstanceMethods(); |
| 437 | int methodCount = CatDecl->getNumInstanceMethods(); |
| 438 | for (int i = 0; i < methodCount; ++i) { |
| 439 | if (methods[i]->getSelector() == Sel) { |
| 440 | return methods[i]; |
| 441 | } |
| 442 | } |
| 443 | CatDecl = CatDecl->getNextClassCategory(); |
| 444 | } |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 445 | ClassDecl = ClassDecl->getSuperClass(); |
| 446 | } |
| 447 | return NULL; |
| 448 | } |
| 449 | |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 450 | // lookupClassMethod - This method returns a class method by looking in the |
| 451 | // class, it's categories, and it's super classes (using a linear search). |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 452 | ObjcMethodDecl *ObjcInterfaceDecl::lookupClassMethod(Selector &Sel) { |
| 453 | ObjcInterfaceDecl* ClassDecl = this; |
| 454 | while (ClassDecl != NULL) { |
Fariborz Jahanian | 7ed9e0f | 2007-10-02 22:05:16 +0000 | [diff] [blame] | 455 | ObjcMethodDecl **methods = ClassDecl->getClassMethods(); |
| 456 | int methodCount = ClassDecl->getNumClassMethods(); |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 457 | for (int i = 0; i < methodCount; ++i) { |
| 458 | if (methods[i]->getSelector() == Sel) { |
| 459 | return methods[i]; |
| 460 | } |
| 461 | } |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame] | 462 | // Didn't find one yet - look through protocols. |
| 463 | ObjcProtocolDecl **protocols = ClassDecl->getReferencedProtocols(); |
| 464 | int numProtocols = ClassDecl->getNumIntfRefProtocols(); |
| 465 | for (int pIdx = 0; pIdx < numProtocols; pIdx++) { |
| 466 | ObjcMethodDecl **methods = protocols[pIdx]->getClassMethods(); |
| 467 | int methodCount = protocols[pIdx]->getNumClassMethods(); |
| 468 | for (int i = 0; i < methodCount; ++i) { |
| 469 | if (methods[i]->getSelector() == Sel) { |
| 470 | return methods[i]; |
| 471 | } |
| 472 | } |
| 473 | } |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 474 | // Didn't find one yet - now look through categories. |
Steve Naroff | ff1afdb | 2007-10-14 23:13:51 +0000 | [diff] [blame] | 475 | ObjcCategoryDecl *CatDecl = ClassDecl->getCategoryList(); |
Steve Naroff | 3d58138 | 2007-10-14 18:27:41 +0000 | [diff] [blame] | 476 | while (CatDecl) { |
| 477 | ObjcMethodDecl **methods = CatDecl->getClassMethods(); |
| 478 | int methodCount = CatDecl->getNumClassMethods(); |
| 479 | for (int i = 0; i < methodCount; ++i) { |
| 480 | if (methods[i]->getSelector() == Sel) { |
| 481 | return methods[i]; |
| 482 | } |
| 483 | } |
| 484 | CatDecl = CatDecl->getNextClassCategory(); |
| 485 | } |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 486 | ClassDecl = ClassDecl->getSuperClass(); |
| 487 | } |
| 488 | return NULL; |
| 489 | } |
| 490 | |
Chris Lattner | 0157c51 | 2007-12-12 07:30:05 +0000 | [diff] [blame^] | 491 | /// lookupInstanceMethod - This method returns an instance method by looking in |
| 492 | /// the class implementation. Unlike interfaces, we don't look outside the |
| 493 | /// implementation. |
| 494 | ObjcMethodDecl *ObjcImplementationDecl::lookupInstanceMethod(Selector Sel) { |
| 495 | for (instmeth_iterator I = instmeth_begin(), E = instmeth_end(); I != E; ++I) |
| 496 | if ((*I)->getSelector() == Sel) |
| 497 | return *I; |
Steve Naroff | c43d868 | 2007-11-11 00:10:47 +0000 | [diff] [blame] | 498 | return NULL; |
| 499 | } |
| 500 | |
Chris Lattner | 0157c51 | 2007-12-12 07:30:05 +0000 | [diff] [blame^] | 501 | /// lookupClassMethod - This method returns a class method by looking in |
| 502 | /// the class implementation. Unlike interfaces, we don't look outside the |
| 503 | /// implementation. |
| 504 | ObjcMethodDecl *ObjcImplementationDecl::lookupClassMethod(Selector Sel) { |
Steve Naroff | 0416fb9 | 2007-11-11 17:19:15 +0000 | [diff] [blame] | 505 | ObjcMethodDecl *const*methods = getClassMethods(); |
Steve Naroff | c43d868 | 2007-11-11 00:10:47 +0000 | [diff] [blame] | 506 | int methodCount = getNumClassMethods(); |
| 507 | for (int i = 0; i < methodCount; ++i) { |
| 508 | if (methods[i]->getSelector() == Sel) { |
| 509 | return methods[i]; |
| 510 | } |
| 511 | } |
| 512 | return NULL; |
| 513 | } |
Fariborz Jahanian | 25e077d | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 514 | |
Steve Naroff | e1e6c0d | 2007-11-12 22:05:31 +0000 | [diff] [blame] | 515 | // lookupInstanceMethod - This method returns an instance method by looking in |
| 516 | // the class implementation. Unlike interfaces, we don't look outside the |
| 517 | // implementation. |
| 518 | ObjcMethodDecl *ObjcCategoryImplDecl::lookupInstanceMethod(Selector &Sel) { |
| 519 | ObjcMethodDecl *const*methods = getInstanceMethods(); |
| 520 | int methodCount = getNumInstanceMethods(); |
| 521 | for (int i = 0; i < methodCount; ++i) { |
| 522 | if (methods[i]->getSelector() == Sel) { |
| 523 | return methods[i]; |
| 524 | } |
| 525 | } |
| 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | // lookupClassMethod - This method returns an instance method by looking in |
| 530 | // the class implementation. Unlike interfaces, we don't look outside the |
| 531 | // implementation. |
| 532 | ObjcMethodDecl *ObjcCategoryImplDecl::lookupClassMethod(Selector &Sel) { |
| 533 | ObjcMethodDecl *const*methods = getClassMethods(); |
| 534 | int methodCount = getNumClassMethods(); |
| 535 | for (int i = 0; i < methodCount; ++i) { |
| 536 | if (methods[i]->getSelector() == Sel) { |
| 537 | return methods[i]; |
| 538 | } |
| 539 | } |
| 540 | return NULL; |
| 541 | } |
Fariborz Jahanian | 7dd8283 | 2007-12-07 21:21:21 +0000 | [diff] [blame] | 542 | |
| 543 | // lookupInstanceMethod - Lookup a instance method in the protocol and protocols |
| 544 | // it inherited. |
| 545 | ObjcMethodDecl *ObjcProtocolDecl::lookupInstanceMethod(Selector &Sel) { |
| 546 | ObjcMethodDecl *const*methods = getInstanceMethods(); |
| 547 | int methodCount = getNumInstanceMethods(); |
| 548 | for (int i = 0; i < methodCount; ++i) { |
| 549 | if (methods[i]->getSelector() == Sel) { |
| 550 | return methods[i]; |
| 551 | } |
| 552 | } |
| 553 | if (getNumReferencedProtocols() > 0) { |
| 554 | ObjcProtocolDecl **RefPDecl = getReferencedProtocols(); |
| 555 | |
| 556 | for (int i = 0; i < getNumReferencedProtocols(); i++) { |
| 557 | if (ObjcMethodDecl *Method = RefPDecl[i]->lookupInstanceMethod(Sel)) |
| 558 | return Method; |
| 559 | } |
| 560 | } |
| 561 | return NULL; |
| 562 | } |
| 563 | |
| 564 | // lookupInstanceMethod - Lookup a class method in the protocol and protocols |
| 565 | // it inherited. |
| 566 | ObjcMethodDecl *ObjcProtocolDecl::lookupClassMethod(Selector &Sel) { |
| 567 | ObjcMethodDecl *const*methods = getClassMethods(); |
| 568 | int methodCount = getNumClassMethods(); |
| 569 | for (int i = 0; i < methodCount; ++i) { |
| 570 | if (methods[i]->getSelector() == Sel) { |
| 571 | return methods[i]; |
| 572 | } |
| 573 | } |
| 574 | if (getNumReferencedProtocols() > 0) { |
| 575 | ObjcProtocolDecl **RefPDecl = getReferencedProtocols(); |
| 576 | |
| 577 | for (int i = 0; i < getNumReferencedProtocols(); i++) { |
| 578 | if (ObjcMethodDecl *Method = RefPDecl[i]->lookupClassMethod(Sel)) |
| 579 | return Method; |
| 580 | } |
| 581 | } |
| 582 | return NULL; |
| 583 | } |