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