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