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