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