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" |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | f78915f | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 16 | #include "clang/AST/Attr.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 17 | #include "clang/Basic/IdentifierTable.h" |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
| 22 | // Statistics |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | // temporary statistics gathering |
| 26 | static unsigned nFuncs = 0; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 27 | static unsigned nVars = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | static unsigned nParmVars = 0; |
| 29 | static unsigned nSUC = 0; |
| 30 | static unsigned nEnumConst = 0; |
| 31 | static unsigned nEnumDecls = 0; |
| 32 | static unsigned nTypedef = 0; |
| 33 | static unsigned nFieldDecls = 0; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 34 | static unsigned nInterfaceDecls = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 35 | static unsigned nClassDecls = 0; |
| 36 | static unsigned nMethodDecls = 0; |
| 37 | static unsigned nProtocolDecls = 0; |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 38 | static unsigned nForwardProtocolDecls = 0; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 39 | static unsigned nCategoryDecls = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 40 | static unsigned nIvarDecls = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 41 | static unsigned nObjCImplementationDecls = 0; |
| 42 | static unsigned nObjCCategoryImpl = 0; |
| 43 | static unsigned nObjCCompatibleAlias = 0; |
| 44 | static unsigned nObjCPropertyDecl = 0; |
Fariborz Jahanian | 61d4615 | 2008-04-16 22:00:24 +0000 | [diff] [blame^] | 45 | static unsigned nObjCPropertyImplDecl = 0; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 46 | static unsigned nLinkageSpecDecl = 0; |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 47 | static unsigned nFileScopeAsmDecl = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 48 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | static bool StatSwitch = false; |
| 50 | |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 51 | // This keeps track of all decl attributes. Since so few decls have attrs, we |
| 52 | // keep them in a hash map instead of wasting space in the Decl class. |
| 53 | typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy; |
| 54 | |
| 55 | static DeclAttrMapTy *DeclAttrs = 0; |
| 56 | |
Steve Naroff | e5ea380 | 2007-09-17 14:49:06 +0000 | [diff] [blame] | 57 | const char *Decl::getDeclKindName() const { |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 58 | switch (DeclKind) { |
| 59 | default: assert(0 && "Unknown decl kind!"); |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 60 | case Typedef: return "Typedef"; |
| 61 | case Function: return "Function"; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 62 | case Var: return "Var"; |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 63 | case ParmVar: return "ParmVar"; |
| 64 | case EnumConstant: return "EnumConstant"; |
| 65 | case ObjCInterface: return "ObjCInterface"; |
| 66 | case ObjCClass: return "ObjCClass"; |
| 67 | case ObjCMethod: return "ObjCMethod"; |
| 68 | case ObjCProtocol: return "ObjCProtocol"; |
| 69 | case ObjCForwardProtocol: return "ObjCForwardProtocol"; |
| 70 | case Struct: return "Struct"; |
| 71 | case Union: return "Union"; |
| 72 | case Class: return "Class"; |
| 73 | case Enum: return "Enum"; |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 77 | bool Decl::CollectingStats(bool Enable) { |
| 78 | if (Enable) |
| 79 | StatSwitch = true; |
| 80 | return StatSwitch; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void Decl::PrintStats() { |
| 84 | fprintf(stderr, "*** Decl Stats:\n"); |
| 85 | fprintf(stderr, " %d decls total.\n", |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 86 | int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+ |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 87 | nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+ |
| 88 | nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | fprintf(stderr, " %d function decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 90 | nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl))); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 91 | fprintf(stderr, " %d variable decls, %d each (%d bytes)\n", |
| 92 | nVars, (int)sizeof(VarDecl), |
| 93 | int(nVars*sizeof(VarDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 95 | nParmVars, (int)sizeof(ParmVarDecl), |
| 96 | int(nParmVars*sizeof(ParmVarDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | fprintf(stderr, " %d field decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 98 | nFieldDecls, (int)sizeof(FieldDecl), |
| 99 | int(nFieldDecls*sizeof(FieldDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 101 | nSUC, (int)sizeof(RecordDecl), |
| 102 | int(nSUC*sizeof(RecordDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | fprintf(stderr, " %d enum decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 104 | nEnumDecls, (int)sizeof(EnumDecl), |
| 105 | int(nEnumDecls*sizeof(EnumDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 107 | nEnumConst, (int)sizeof(EnumConstantDecl), |
| 108 | int(nEnumConst*sizeof(EnumConstantDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 110 | nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 111 | // Objective-C decls... |
| 112 | fprintf(stderr, " %d interface decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 113 | nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl), |
| 114 | int(nInterfaceDecls*sizeof(ObjCInterfaceDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 115 | fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 116 | nIvarDecls, (int)sizeof(ObjCIvarDecl), |
| 117 | int(nIvarDecls*sizeof(ObjCIvarDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 118 | fprintf(stderr, " %d class decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 119 | nClassDecls, (int)sizeof(ObjCClassDecl), |
| 120 | int(nClassDecls*sizeof(ObjCClassDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 121 | fprintf(stderr, " %d method decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 122 | nMethodDecls, (int)sizeof(ObjCMethodDecl), |
| 123 | int(nMethodDecls*sizeof(ObjCMethodDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 124 | fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 125 | nProtocolDecls, (int)sizeof(ObjCProtocolDecl), |
| 126 | int(nProtocolDecls*sizeof(ObjCProtocolDecl))); |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 127 | fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 128 | nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl), |
| 129 | int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl))); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 130 | fprintf(stderr, " %d category decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 131 | nCategoryDecls, (int)sizeof(ObjCCategoryDecl), |
| 132 | int(nCategoryDecls*sizeof(ObjCCategoryDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 133 | |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 134 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 135 | nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl), |
| 136 | int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl))); |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 137 | |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 138 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 139 | nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl), |
| 140 | int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl))); |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 141 | |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 142 | fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 143 | nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl), |
| 144 | int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl))); |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 145 | |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 146 | fprintf(stderr, " %d property decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 147 | nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl), |
| 148 | int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl))); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 149 | |
Fariborz Jahanian | 61d4615 | 2008-04-16 22:00:24 +0000 | [diff] [blame^] | 150 | fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n", |
| 151 | nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl), |
| 152 | int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl))); |
| 153 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 154 | fprintf(stderr, "Total bytes = %d\n", |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 155 | int(nFuncs*sizeof(FunctionDecl)+ |
| 156 | nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+ |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 157 | nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+ |
| 158 | nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+ |
| 159 | nTypedef*sizeof(TypedefDecl)+ |
Fariborz Jahanian | 269b10d | 2008-01-23 01:34:33 +0000 | [diff] [blame] | 160 | nInterfaceDecls*sizeof(ObjCInterfaceDecl)+ |
| 161 | nIvarDecls*sizeof(ObjCIvarDecl)+ |
| 162 | nClassDecls*sizeof(ObjCClassDecl)+ |
| 163 | nMethodDecls*sizeof(ObjCMethodDecl)+ |
| 164 | nProtocolDecls*sizeof(ObjCProtocolDecl)+ |
| 165 | nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+ |
| 166 | nCategoryDecls*sizeof(ObjCCategoryDecl)+ |
| 167 | nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+ |
| 168 | nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+ |
| 169 | nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+ |
| 170 | nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+ |
Fariborz Jahanian | 61d4615 | 2008-04-16 22:00:24 +0000 | [diff] [blame^] | 171 | nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+ |
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; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 181 | case Var: nVars++; break; |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 182 | case ParmVar: nParmVars++; break; |
| 183 | case EnumConstant: nEnumConst++; break; |
| 184 | case Field: nFieldDecls++; break; |
| 185 | case Struct: case Union: case Class: nSUC++; break; |
| 186 | case Enum: nEnumDecls++; break; |
| 187 | case ObjCInterface: nInterfaceDecls++; break; |
| 188 | case ObjCClass: nClassDecls++; break; |
| 189 | case ObjCMethod: nMethodDecls++; break; |
| 190 | case ObjCProtocol: nProtocolDecls++; break; |
| 191 | case ObjCForwardProtocol: nForwardProtocolDecls++; break; |
| 192 | case ObjCCategory: nCategoryDecls++; break; |
| 193 | case ObjCIvar: nIvarDecls++; break; |
| 194 | case ObjCImplementation: nObjCImplementationDecls++; break; |
| 195 | case ObjCCategoryImpl: nObjCCategoryImpl++; break; |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 196 | case ObjCCompatibleAlias: nObjCCompatibleAlias++; break; |
Sam Bishop | 670aa9d | 2008-04-08 20:49:25 +0000 | [diff] [blame] | 197 | case ObjCProperty: nObjCPropertyDecl++; break; |
Fariborz Jahanian | 61d4615 | 2008-04-16 22:00:24 +0000 | [diff] [blame^] | 198 | case ObjCPropertyImpl: nObjCPropertyImplDecl++; break; |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 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 | |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 208 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *CD, |
| 209 | SourceLocation L, |
| 210 | IdentifierInfo *Id, QualType T, |
| 211 | StorageClass S, ScopedDecl *PrevDecl) { |
| 212 | void *Mem = C.getAllocator().Allocate<VarDecl>(); |
| 213 | return new (Mem) VarDecl(Var, CD, L, Id, T, S, PrevDecl); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 217 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *CD, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 218 | SourceLocation L, IdentifierInfo *Id, |
| 219 | QualType T, StorageClass S, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 220 | Expr *DefArg, ScopedDecl *PrevDecl) { |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 221 | void *Mem = C.getAllocator().Allocate<ParmVarDecl>(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 222 | return new (Mem) ParmVarDecl(CD, L, Id, T, S, DefArg, PrevDecl); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 225 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *CD, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 226 | SourceLocation L, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 227 | IdentifierInfo *Id, QualType T, |
| 228 | StorageClass S, bool isInline, |
| 229 | ScopedDecl *PrevDecl) { |
| 230 | void *Mem = C.getAllocator().Allocate<FunctionDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 231 | return new (Mem) FunctionDecl(CD, L, Id, T, S, isInline, PrevDecl); |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 234 | FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 235 | IdentifierInfo *Id, QualType T, Expr *BW) { |
| 236 | void *Mem = C.getAllocator().Allocate<FieldDecl>(); |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 237 | return new (Mem) FieldDecl(L, Id, T, BW); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 241 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 242 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 243 | IdentifierInfo *Id, QualType T, |
| 244 | Expr *E, const llvm::APSInt &V, |
| 245 | ScopedDecl *PrevDecl){ |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 246 | void *Mem = C.getAllocator().Allocate<EnumConstantDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 247 | return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 250 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *CD, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 251 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 252 | IdentifierInfo *Id, QualType T, |
| 253 | ScopedDecl *PD) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 254 | void *Mem = C.getAllocator().Allocate<TypedefDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 255 | return new (Mem) TypedefDecl(CD, L, Id, T, PD); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 258 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *CD, SourceLocation L, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 259 | IdentifierInfo *Id, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 260 | ScopedDecl *PrevDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 261 | void *Mem = C.getAllocator().Allocate<EnumDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 262 | return new (Mem) EnumDecl(CD, L, Id, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 265 | RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *CD, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 266 | SourceLocation L, IdentifierInfo *Id, |
| 267 | ScopedDecl *PrevDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 268 | void *Mem = C.getAllocator().Allocate<RecordDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 269 | return new (Mem) RecordDecl(DK, CD, L, Id, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 272 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, |
| 273 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 274 | StringLiteral *Str) { |
| 275 | void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>(); |
| 276 | return new (Mem) FileScopeAsmDecl(L, Str); |
| 277 | } |
| 278 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 279 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
| 280 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 281 | LanguageIDs Lang, Decl *D) { |
| 282 | void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>(); |
| 283 | return new (Mem) LinkageSpecDecl(L, Lang, D); |
| 284 | } |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 285 | |
| 286 | //===----------------------------------------------------------------------===// |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 287 | // Decl Implementation |
| 288 | //===----------------------------------------------------------------------===// |
| 289 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 290 | // Out-of-line virtual method providing a home for Decl. |
| 291 | Decl::~Decl() { |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 292 | if (!HasAttrs) |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 293 | return; |
| 294 | |
| 295 | DeclAttrMapTy::iterator it = DeclAttrs->find(this); |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 296 | assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!"); |
| 297 | |
| 298 | delete it->second; |
| 299 | DeclAttrs->erase(it); |
| 300 | if (DeclAttrs->empty()) { |
| 301 | delete DeclAttrs; |
| 302 | DeclAttrs = 0; |
| 303 | } |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 306 | void Decl::addAttr(Attr *NewAttr) { |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 307 | if (!DeclAttrs) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 308 | DeclAttrs = new DeclAttrMapTy(); |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 309 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 310 | Attr *&ExistingAttr = (*DeclAttrs)[this]; |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 311 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 312 | NewAttr->setNext(ExistingAttr); |
| 313 | ExistingAttr = NewAttr; |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 314 | |
| 315 | HasAttrs = true; |
| 316 | } |
| 317 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 318 | const Attr *Decl::getAttrs() const { |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 319 | if (!HasAttrs) |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 320 | return 0; |
| 321 | |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 322 | return (*DeclAttrs)[this]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Sam Bishop | 1b30cb2 | 2008-04-13 04:32:18 +0000 | [diff] [blame] | 325 | #define CASE(KIND) \ |
| 326 | case KIND: \ |
| 327 | static_cast<KIND##Decl *>(const_cast<Decl *>(this))->~KIND##Decl(); \ |
| 328 | break |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 329 | |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 330 | void Decl::Destroy(ASTContext& C) const { |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 331 | switch (getKind()) { |
| 332 | CASE(Field); |
| 333 | CASE(ObjCIvar); |
| 334 | CASE(ObjCCategory); |
| 335 | CASE(ObjCCategoryImpl); |
| 336 | CASE(ObjCImplementation); |
| 337 | CASE(ObjCProtocol); |
| 338 | CASE(ObjCProperty); |
| 339 | CASE(Typedef); |
| 340 | CASE(Enum); |
| 341 | CASE(EnumConstant); |
| 342 | CASE(Function); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 343 | CASE(Var); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 344 | CASE(ParmVar); |
| 345 | CASE(ObjCInterface); |
| 346 | CASE(ObjCCompatibleAlias); |
| 347 | CASE(ObjCMethod); |
| 348 | CASE(ObjCClass); |
| 349 | CASE(ObjCForwardProtocol); |
| 350 | CASE(LinkageSpec); |
| 351 | |
| 352 | case Struct: case Union: case Class: |
Sam Bishop | 1b30cb2 | 2008-04-13 04:32:18 +0000 | [diff] [blame] | 353 | static_cast<RecordDecl *>(const_cast<Decl *>(this))->~RecordDecl(); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 354 | break; |
| 355 | |
| 356 | default: assert(0 && "Unknown decl kind!"); |
| 357 | } |
| 358 | |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 359 | C.getAllocator().Deallocate((void *)this); |
| 360 | } |
| 361 | |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 362 | #undef CASE |
| 363 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 364 | //===----------------------------------------------------------------------===// |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 365 | // DeclContext Implementation |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 366 | //===----------------------------------------------------------------------===// |
| 367 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 368 | DeclContext *DeclContext::getParent() const { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 369 | if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this)) |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 370 | return SD->getDeclContext(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 371 | else |
| 372 | return NULL; |
| 373 | } |
| 374 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 375 | Decl *DeclContext::ToDecl (const DeclContext *D) { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 376 | return CastTo<Decl>(D); |
| 377 | } |
| 378 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 379 | DeclContext *DeclContext::FromDecl (const Decl *D) { |
| 380 | return CastTo<DeclContext>(D); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 384 | // NamedDecl Implementation |
| 385 | //===----------------------------------------------------------------------===// |
| 386 | |
Chris Lattner | fd5de47 | 2007-10-06 22:53:46 +0000 | [diff] [blame] | 387 | const char *NamedDecl::getName() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | if (const IdentifierInfo *II = getIdentifier()) |
| 389 | return II->getName(); |
| 390 | return ""; |
| 391 | } |
| 392 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 393 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 394 | // FunctionDecl Implementation |
| 395 | //===----------------------------------------------------------------------===// |
| 396 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 397 | FunctionDecl::~FunctionDecl() { |
| 398 | delete[] ParamInfo; |
Sam Bishop | 8266d7f | 2008-04-03 05:01:04 +0000 | [diff] [blame] | 399 | delete Body; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | unsigned FunctionDecl::getNumParams() const { |
Chris Lattner | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 403 | const FunctionType *FT = getType()->getAsFunctionType(); |
| 404 | if (isa<FunctionTypeNoProto>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 405 | return 0; |
Chris Lattner | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 406 | return cast<FunctionTypeProto>(FT)->getNumArgs(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 410 | assert(ParamInfo == 0 && "Already has param info!"); |
| 411 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
| 412 | |
| 413 | // Zero params -> null pointer. |
| 414 | if (NumParams) { |
| 415 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 416 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 417 | } |
| 418 | } |
| 419 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 420 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 421 | /// needed to call this function. This may be fewer than the number of |
| 422 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 423 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 424 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 425 | unsigned NumRequiredArgs = getNumParams(); |
| 426 | while (NumRequiredArgs > 0 |
| 427 | && getParamDecl(NumRequiredArgs-1)->getDefaultArg()) |
| 428 | --NumRequiredArgs; |
| 429 | |
| 430 | return NumRequiredArgs; |
| 431 | } |
| 432 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 433 | //===----------------------------------------------------------------------===// |
| 434 | // RecordDecl Implementation |
| 435 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 436 | |
| 437 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 438 | /// record. This method is used to mark the decl as being defined, with the |
| 439 | /// specified contents. |
| 440 | void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) { |
| 441 | assert(!isDefinition() && "Cannot redefine record!"); |
| 442 | setDefinition(true); |
| 443 | NumMembers = numMembers; |
| 444 | if (numMembers) { |
| 445 | Members = new FieldDecl*[numMembers]; |
| 446 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 447 | } |
| 448 | } |
| 449 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 450 | FieldDecl *RecordDecl::getMember(IdentifierInfo *II) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 451 | if (Members == 0 || NumMembers < 0) |
| 452 | return 0; |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 454 | // Linear search. When C++ classes come along, will likely need to revisit. |
| 455 | for (int i = 0; i != NumMembers; ++i) |
| 456 | if (Members[i]->getIdentifier() == II) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 457 | return Members[i]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 458 | return 0; |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 459 | } |