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; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame^] | 201 | case TranslationUnit: break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 205 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 206 | // Decl Allocation/Deallocation Method Implementations |
| 207 | //===----------------------------------------------------------------------===// |
| 208 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame^] | 209 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 210 | void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>(); |
| 211 | return new (Mem) TranslationUnitDecl(); |
| 212 | } |
| 213 | |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 214 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *CD, |
| 215 | SourceLocation L, |
| 216 | IdentifierInfo *Id, QualType T, |
| 217 | StorageClass S, ScopedDecl *PrevDecl) { |
| 218 | void *Mem = C.getAllocator().Allocate<VarDecl>(); |
| 219 | return new (Mem) VarDecl(Var, CD, L, Id, T, S, PrevDecl); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 222 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *CD, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 223 | SourceLocation L, IdentifierInfo *Id, |
| 224 | QualType T, StorageClass S, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 225 | Expr *DefArg, ScopedDecl *PrevDecl) { |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 226 | void *Mem = C.getAllocator().Allocate<ParmVarDecl>(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 227 | return new (Mem) ParmVarDecl(CD, L, Id, T, S, DefArg, PrevDecl); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 230 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *CD, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 231 | SourceLocation L, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 232 | IdentifierInfo *Id, QualType T, |
| 233 | StorageClass S, bool isInline, |
| 234 | ScopedDecl *PrevDecl) { |
| 235 | void *Mem = C.getAllocator().Allocate<FunctionDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 236 | return new (Mem) FunctionDecl(CD, L, Id, T, S, isInline, PrevDecl); |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 239 | FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 240 | IdentifierInfo *Id, QualType T, Expr *BW) { |
| 241 | void *Mem = C.getAllocator().Allocate<FieldDecl>(); |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 242 | return new (Mem) FieldDecl(L, Id, T, BW); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 246 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 247 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 248 | IdentifierInfo *Id, QualType T, |
| 249 | Expr *E, const llvm::APSInt &V, |
| 250 | ScopedDecl *PrevDecl){ |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 251 | void *Mem = C.getAllocator().Allocate<EnumConstantDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 252 | return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 255 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *CD, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 256 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 257 | IdentifierInfo *Id, QualType T, |
| 258 | ScopedDecl *PD) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 259 | void *Mem = C.getAllocator().Allocate<TypedefDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 260 | return new (Mem) TypedefDecl(CD, L, Id, T, PD); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 263 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *CD, SourceLocation L, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 264 | IdentifierInfo *Id, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 265 | ScopedDecl *PrevDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 266 | void *Mem = C.getAllocator().Allocate<EnumDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 267 | return new (Mem) EnumDecl(CD, L, Id, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 270 | RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *CD, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 271 | SourceLocation L, IdentifierInfo *Id, |
| 272 | ScopedDecl *PrevDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 273 | void *Mem = C.getAllocator().Allocate<RecordDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 274 | return new (Mem) RecordDecl(DK, CD, L, Id, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 277 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, |
| 278 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 279 | StringLiteral *Str) { |
| 280 | void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>(); |
| 281 | return new (Mem) FileScopeAsmDecl(L, Str); |
| 282 | } |
| 283 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 284 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
| 285 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 286 | LanguageIDs Lang, Decl *D) { |
| 287 | void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>(); |
| 288 | return new (Mem) LinkageSpecDecl(L, Lang, D); |
| 289 | } |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 290 | |
| 291 | //===----------------------------------------------------------------------===// |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 292 | // Decl Implementation |
| 293 | //===----------------------------------------------------------------------===// |
| 294 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | // Out-of-line virtual method providing a home for Decl. |
| 296 | Decl::~Decl() { |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 297 | if (!HasAttrs) |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 298 | return; |
| 299 | |
| 300 | DeclAttrMapTy::iterator it = DeclAttrs->find(this); |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 301 | assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!"); |
| 302 | |
| 303 | delete it->second; |
| 304 | DeclAttrs->erase(it); |
| 305 | if (DeclAttrs->empty()) { |
| 306 | delete DeclAttrs; |
| 307 | DeclAttrs = 0; |
| 308 | } |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 311 | void Decl::addAttr(Attr *NewAttr) { |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 312 | if (!DeclAttrs) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 313 | DeclAttrs = new DeclAttrMapTy(); |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 314 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 315 | Attr *&ExistingAttr = (*DeclAttrs)[this]; |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 316 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 317 | NewAttr->setNext(ExistingAttr); |
| 318 | ExistingAttr = NewAttr; |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 319 | |
| 320 | HasAttrs = true; |
| 321 | } |
| 322 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 323 | const Attr *Decl::getAttrs() const { |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 324 | if (!HasAttrs) |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 325 | return 0; |
| 326 | |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 327 | return (*DeclAttrs)[this]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Sam Bishop | 1b30cb2 | 2008-04-13 04:32:18 +0000 | [diff] [blame] | 330 | #define CASE(KIND) \ |
| 331 | case KIND: \ |
| 332 | static_cast<KIND##Decl *>(const_cast<Decl *>(this))->~KIND##Decl(); \ |
| 333 | break |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 334 | |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 335 | void Decl::Destroy(ASTContext& C) const { |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 336 | switch (getKind()) { |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame^] | 337 | CASE(TranslationUnit); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 338 | CASE(Field); |
| 339 | CASE(ObjCIvar); |
| 340 | CASE(ObjCCategory); |
| 341 | CASE(ObjCCategoryImpl); |
| 342 | CASE(ObjCImplementation); |
| 343 | CASE(ObjCProtocol); |
| 344 | CASE(ObjCProperty); |
| 345 | CASE(Typedef); |
| 346 | CASE(Enum); |
| 347 | CASE(EnumConstant); |
| 348 | CASE(Function); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 349 | CASE(Var); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 350 | CASE(ParmVar); |
| 351 | CASE(ObjCInterface); |
| 352 | CASE(ObjCCompatibleAlias); |
| 353 | CASE(ObjCMethod); |
| 354 | CASE(ObjCClass); |
| 355 | CASE(ObjCForwardProtocol); |
| 356 | CASE(LinkageSpec); |
| 357 | |
| 358 | case Struct: case Union: case Class: |
Sam Bishop | 1b30cb2 | 2008-04-13 04:32:18 +0000 | [diff] [blame] | 359 | static_cast<RecordDecl *>(const_cast<Decl *>(this))->~RecordDecl(); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 360 | break; |
| 361 | |
| 362 | default: assert(0 && "Unknown decl kind!"); |
| 363 | } |
| 364 | |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 365 | C.getAllocator().Deallocate((void *)this); |
| 366 | } |
| 367 | |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 368 | #undef CASE |
| 369 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 370 | //===----------------------------------------------------------------------===// |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 371 | // DeclContext Implementation |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 372 | //===----------------------------------------------------------------------===// |
| 373 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 374 | DeclContext *DeclContext::getParent() const { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 375 | if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this)) |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 376 | return SD->getDeclContext(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 377 | else |
| 378 | return NULL; |
| 379 | } |
| 380 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 381 | Decl *DeclContext::ToDecl (const DeclContext *D) { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 382 | return CastTo<Decl>(D); |
| 383 | } |
| 384 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 385 | DeclContext *DeclContext::FromDecl (const Decl *D) { |
| 386 | return CastTo<DeclContext>(D); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 390 | // NamedDecl Implementation |
| 391 | //===----------------------------------------------------------------------===// |
| 392 | |
Chris Lattner | fd5de47 | 2007-10-06 22:53:46 +0000 | [diff] [blame] | 393 | const char *NamedDecl::getName() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | if (const IdentifierInfo *II = getIdentifier()) |
| 395 | return II->getName(); |
| 396 | return ""; |
| 397 | } |
| 398 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 399 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 400 | // FunctionDecl Implementation |
| 401 | //===----------------------------------------------------------------------===// |
| 402 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 403 | FunctionDecl::~FunctionDecl() { |
| 404 | delete[] ParamInfo; |
Sam Bishop | 8266d7f | 2008-04-03 05:01:04 +0000 | [diff] [blame] | 405 | delete Body; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | unsigned FunctionDecl::getNumParams() const { |
Chris Lattner | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 409 | const FunctionType *FT = getType()->getAsFunctionType(); |
| 410 | if (isa<FunctionTypeNoProto>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 411 | return 0; |
Chris Lattner | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 412 | return cast<FunctionTypeProto>(FT)->getNumArgs(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 416 | assert(ParamInfo == 0 && "Already has param info!"); |
| 417 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
| 418 | |
| 419 | // Zero params -> null pointer. |
| 420 | if (NumParams) { |
| 421 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 422 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 423 | } |
| 424 | } |
| 425 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 426 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 427 | /// needed to call this function. This may be fewer than the number of |
| 428 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 429 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 430 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 431 | unsigned NumRequiredArgs = getNumParams(); |
| 432 | while (NumRequiredArgs > 0 |
| 433 | && getParamDecl(NumRequiredArgs-1)->getDefaultArg()) |
| 434 | --NumRequiredArgs; |
| 435 | |
| 436 | return NumRequiredArgs; |
| 437 | } |
| 438 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 439 | //===----------------------------------------------------------------------===// |
| 440 | // RecordDecl Implementation |
| 441 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 442 | |
| 443 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 444 | /// record. This method is used to mark the decl as being defined, with the |
| 445 | /// specified contents. |
| 446 | void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) { |
| 447 | assert(!isDefinition() && "Cannot redefine record!"); |
| 448 | setDefinition(true); |
| 449 | NumMembers = numMembers; |
| 450 | if (numMembers) { |
| 451 | Members = new FieldDecl*[numMembers]; |
| 452 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 453 | } |
| 454 | } |
| 455 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 456 | FieldDecl *RecordDecl::getMember(IdentifierInfo *II) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 457 | if (Members == 0 || NumMembers < 0) |
| 458 | return 0; |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 460 | // Linear search. When C++ classes come along, will likely need to revisit. |
| 461 | for (int i = 0; i != NumMembers; ++i) |
| 462 | if (Members[i]->getIdentifier() == II) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 463 | return Members[i]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 464 | return 0; |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 465 | } |