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