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