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; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 32 | static unsigned nNamespaces = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | static unsigned nTypedef = 0; |
| 34 | static unsigned nFieldDecls = 0; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 35 | static unsigned nInterfaceDecls = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 36 | static unsigned nClassDecls = 0; |
| 37 | static unsigned nMethodDecls = 0; |
| 38 | static unsigned nProtocolDecls = 0; |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 39 | static unsigned nForwardProtocolDecls = 0; |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 40 | static unsigned nCategoryDecls = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 41 | static unsigned nIvarDecls = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 42 | static unsigned nObjCImplementationDecls = 0; |
| 43 | static unsigned nObjCCategoryImpl = 0; |
| 44 | static unsigned nObjCCompatibleAlias = 0; |
| 45 | static unsigned nObjCPropertyDecl = 0; |
Fariborz Jahanian | 61d4615 | 2008-04-16 22:00:24 +0000 | [diff] [blame] | 46 | static unsigned nObjCPropertyImplDecl = 0; |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 47 | static unsigned nLinkageSpecDecl = 0; |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 48 | static unsigned nFileScopeAsmDecl = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 49 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | static bool StatSwitch = false; |
| 51 | |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 52 | // This keeps track of all decl attributes. Since so few decls have attrs, we |
| 53 | // keep them in a hash map instead of wasting space in the Decl class. |
| 54 | typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy; |
| 55 | |
| 56 | static DeclAttrMapTy *DeclAttrs = 0; |
| 57 | |
Steve Naroff | e5ea380 | 2007-09-17 14:49:06 +0000 | [diff] [blame] | 58 | const char *Decl::getDeclKindName() const { |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 59 | switch (DeclKind) { |
| 60 | default: assert(0 && "Unknown decl kind!"); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 61 | case Namespace: return "Namespace"; |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 62 | case Typedef: return "Typedef"; |
| 63 | case Function: return "Function"; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 64 | case Var: return "Var"; |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 65 | case ParmVar: return "ParmVar"; |
| 66 | case EnumConstant: return "EnumConstant"; |
| 67 | case ObjCInterface: return "ObjCInterface"; |
| 68 | case ObjCClass: return "ObjCClass"; |
| 69 | case ObjCMethod: return "ObjCMethod"; |
| 70 | case ObjCProtocol: return "ObjCProtocol"; |
| 71 | case ObjCForwardProtocol: return "ObjCForwardProtocol"; |
| 72 | case Struct: return "Struct"; |
| 73 | case Union: return "Union"; |
| 74 | case Class: return "Class"; |
| 75 | case Enum: return "Enum"; |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 79 | bool Decl::CollectingStats(bool Enable) { |
| 80 | if (Enable) |
| 81 | StatSwitch = true; |
| 82 | return StatSwitch; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void Decl::PrintStats() { |
| 86 | fprintf(stderr, "*** Decl Stats:\n"); |
| 87 | fprintf(stderr, " %d decls total.\n", |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 88 | int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+ |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 89 | nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+ |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 90 | nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+ |
| 91 | nNamespaces)); |
| 92 | fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n", |
| 93 | nNamespaces, (int)sizeof(NamespaceDecl), |
| 94 | int(nNamespaces*sizeof(NamespaceDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | fprintf(stderr, " %d function decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 96 | nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl))); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 97 | fprintf(stderr, " %d variable decls, %d each (%d bytes)\n", |
| 98 | nVars, (int)sizeof(VarDecl), |
| 99 | int(nVars*sizeof(VarDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 101 | nParmVars, (int)sizeof(ParmVarDecl), |
| 102 | int(nParmVars*sizeof(ParmVarDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | fprintf(stderr, " %d field decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 104 | nFieldDecls, (int)sizeof(FieldDecl), |
| 105 | int(nFieldDecls*sizeof(FieldDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 107 | nSUC, (int)sizeof(RecordDecl), |
| 108 | int(nSUC*sizeof(RecordDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | fprintf(stderr, " %d enum decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 110 | nEnumDecls, (int)sizeof(EnumDecl), |
| 111 | int(nEnumDecls*sizeof(EnumDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 113 | nEnumConst, (int)sizeof(EnumConstantDecl), |
| 114 | int(nEnumConst*sizeof(EnumConstantDecl))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 116 | nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 117 | // Objective-C decls... |
| 118 | fprintf(stderr, " %d interface decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 119 | nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl), |
| 120 | int(nInterfaceDecls*sizeof(ObjCInterfaceDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 121 | fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 122 | nIvarDecls, (int)sizeof(ObjCIvarDecl), |
| 123 | int(nIvarDecls*sizeof(ObjCIvarDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 124 | fprintf(stderr, " %d class decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 125 | nClassDecls, (int)sizeof(ObjCClassDecl), |
| 126 | int(nClassDecls*sizeof(ObjCClassDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 127 | fprintf(stderr, " %d method decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 128 | nMethodDecls, (int)sizeof(ObjCMethodDecl), |
| 129 | int(nMethodDecls*sizeof(ObjCMethodDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 130 | fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 131 | nProtocolDecls, (int)sizeof(ObjCProtocolDecl), |
| 132 | int(nProtocolDecls*sizeof(ObjCProtocolDecl))); |
Fariborz Jahanian | 894c57f | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 133 | fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 134 | nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl), |
| 135 | int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl))); |
Fariborz Jahanian | fd225cc | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 136 | fprintf(stderr, " %d category decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 137 | nCategoryDecls, (int)sizeof(ObjCCategoryDecl), |
| 138 | int(nCategoryDecls*sizeof(ObjCCategoryDecl))); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 139 | |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 140 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 141 | nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl), |
| 142 | int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl))); |
Fariborz Jahanian | ccb4f31 | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 143 | |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 144 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 145 | nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl), |
| 146 | int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl))); |
Fariborz Jahanian | 8f3fde0 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 147 | |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 148 | fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 149 | nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl), |
| 150 | int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl))); |
Fariborz Jahanian | 243b64b | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 151 | |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 152 | fprintf(stderr, " %d property decls, %d each (%d bytes)\n", |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 153 | nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl), |
| 154 | int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl))); |
Fariborz Jahanian | 82a5fe3 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 155 | |
Fariborz Jahanian | 61d4615 | 2008-04-16 22:00:24 +0000 | [diff] [blame] | 156 | fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n", |
| 157 | nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl), |
| 158 | int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl))); |
| 159 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | fprintf(stderr, "Total bytes = %d\n", |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 161 | int(nFuncs*sizeof(FunctionDecl)+ |
| 162 | nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+ |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 163 | nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+ |
| 164 | nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+ |
| 165 | nTypedef*sizeof(TypedefDecl)+ |
Fariborz Jahanian | 269b10d | 2008-01-23 01:34:33 +0000 | [diff] [blame] | 166 | nInterfaceDecls*sizeof(ObjCInterfaceDecl)+ |
| 167 | nIvarDecls*sizeof(ObjCIvarDecl)+ |
| 168 | nClassDecls*sizeof(ObjCClassDecl)+ |
| 169 | nMethodDecls*sizeof(ObjCMethodDecl)+ |
| 170 | nProtocolDecls*sizeof(ObjCProtocolDecl)+ |
| 171 | nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+ |
| 172 | nCategoryDecls*sizeof(ObjCCategoryDecl)+ |
| 173 | nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+ |
| 174 | nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+ |
| 175 | nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+ |
| 176 | nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+ |
Fariborz Jahanian | 61d4615 | 2008-04-16 22:00:24 +0000 | [diff] [blame] | 177 | nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+ |
Anders Carlsson | dfab6cb | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 178 | nLinkageSpecDecl*sizeof(LinkageSpecDecl)+ |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 179 | nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+ |
| 180 | nNamespaces*sizeof(NamespaceDecl))); |
Fariborz Jahanian | 269b10d | 2008-01-23 01:34:33 +0000 | [diff] [blame] | 181 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 184 | void Decl::addDeclKind(Kind k) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | switch (k) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 186 | case Namespace: nNamespaces++; break; |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 187 | case Typedef: nTypedef++; break; |
| 188 | case Function: nFuncs++; break; |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 189 | case Var: nVars++; break; |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 190 | case ParmVar: nParmVars++; break; |
| 191 | case EnumConstant: nEnumConst++; break; |
| 192 | case Field: nFieldDecls++; break; |
| 193 | case Struct: case Union: case Class: nSUC++; break; |
| 194 | case Enum: nEnumDecls++; break; |
| 195 | case ObjCInterface: nInterfaceDecls++; break; |
| 196 | case ObjCClass: nClassDecls++; break; |
| 197 | case ObjCMethod: nMethodDecls++; break; |
| 198 | case ObjCProtocol: nProtocolDecls++; break; |
| 199 | case ObjCForwardProtocol: nForwardProtocolDecls++; break; |
| 200 | case ObjCCategory: nCategoryDecls++; break; |
| 201 | case ObjCIvar: nIvarDecls++; break; |
| 202 | case ObjCImplementation: nObjCImplementationDecls++; break; |
| 203 | case ObjCCategoryImpl: nObjCCategoryImpl++; break; |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 204 | case ObjCCompatibleAlias: nObjCCompatibleAlias++; break; |
Sam Bishop | 670aa9d | 2008-04-08 20:49:25 +0000 | [diff] [blame] | 205 | case ObjCProperty: nObjCPropertyDecl++; break; |
Fariborz Jahanian | 61d4615 | 2008-04-16 22:00:24 +0000 | [diff] [blame] | 206 | case ObjCPropertyImpl: nObjCPropertyImplDecl++; break; |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 207 | case LinkageSpec: nLinkageSpecDecl++; break; |
| 208 | case FileScopeAsm: nFileScopeAsmDecl++; break; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 209 | case TranslationUnit: break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 213 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 214 | // Decl Allocation/Deallocation Method Implementations |
| 215 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 216 | |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 217 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 218 | void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>(); |
| 219 | return new (Mem) TranslationUnitDecl(); |
| 220 | } |
| 221 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 222 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 223 | SourceLocation L, IdentifierInfo *Id) { |
| 224 | void *Mem = C.getAllocator().Allocate<NamespaceDecl>(); |
| 225 | return new (Mem) NamespaceDecl(DC, L, Id); |
| 226 | } |
| 227 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 228 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 229 | SourceLocation L, |
| 230 | IdentifierInfo *Id, QualType T, |
| 231 | StorageClass S, ScopedDecl *PrevDecl) { |
| 232 | void *Mem = C.getAllocator().Allocate<VarDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 233 | return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 236 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 237 | SourceLocation L, IdentifierInfo *Id, |
| 238 | QualType T, StorageClass S, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 239 | Expr *DefArg, ScopedDecl *PrevDecl) { |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 240 | void *Mem = C.getAllocator().Allocate<ParmVarDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 241 | return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl); |
Chris Lattner | 9e151e1 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 244 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 245 | SourceLocation L, |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 246 | IdentifierInfo *Id, QualType T, |
| 247 | StorageClass S, bool isInline, |
| 248 | ScopedDecl *PrevDecl) { |
| 249 | void *Mem = C.getAllocator().Allocate<FunctionDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 250 | return new (Mem) FunctionDecl(DC, L, Id, T, S, isInline, PrevDecl); |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 253 | FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 254 | IdentifierInfo *Id, QualType T, Expr *BW) { |
| 255 | void *Mem = C.getAllocator().Allocate<FieldDecl>(); |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 256 | return new (Mem) FieldDecl(L, Id, T, BW); |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Chris Lattner | a98e58d | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 260 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 261 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 262 | IdentifierInfo *Id, QualType T, |
| 263 | Expr *E, const llvm::APSInt &V, |
| 264 | ScopedDecl *PrevDecl){ |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 265 | void *Mem = C.getAllocator().Allocate<EnumConstantDecl>(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 266 | return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 269 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 270 | SourceLocation L, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 271 | IdentifierInfo *Id, QualType T, |
| 272 | ScopedDecl *PD) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 273 | void *Mem = C.getAllocator().Allocate<TypedefDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 274 | return new (Mem) TypedefDecl(DC, L, Id, T, PD); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 277 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 278 | IdentifierInfo *Id, |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 279 | ScopedDecl *PrevDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 280 | void *Mem = C.getAllocator().Allocate<EnumDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 281 | return new (Mem) EnumDecl(DC, L, Id, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 284 | RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *DC, |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 285 | SourceLocation L, IdentifierInfo *Id, |
| 286 | ScopedDecl *PrevDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 287 | void *Mem = C.getAllocator().Allocate<RecordDecl>(); |
Chris Lattner | 9fdf9c6 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 288 | return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl); |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 291 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, |
| 292 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 293 | StringLiteral *Str) { |
| 294 | void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>(); |
| 295 | return new (Mem) FileScopeAsmDecl(L, Str); |
| 296 | } |
| 297 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 298 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
| 299 | SourceLocation L, |
Chris Lattner | 8e25d86 | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 300 | LanguageIDs Lang, Decl *D) { |
| 301 | void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>(); |
| 302 | return new (Mem) LinkageSpecDecl(L, Lang, D); |
| 303 | } |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 304 | |
| 305 | //===----------------------------------------------------------------------===// |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 306 | // Decl Implementation |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | // Out-of-line virtual method providing a home for Decl. |
| 310 | Decl::~Decl() { |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 311 | if (!HasAttrs) |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 312 | return; |
| 313 | |
| 314 | DeclAttrMapTy::iterator it = DeclAttrs->find(this); |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 315 | assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!"); |
| 316 | |
| 317 | delete it->second; |
| 318 | DeclAttrs->erase(it); |
| 319 | if (DeclAttrs->empty()) { |
| 320 | delete DeclAttrs; |
| 321 | DeclAttrs = 0; |
| 322 | } |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 325 | void Decl::addAttr(Attr *NewAttr) { |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 326 | if (!DeclAttrs) |
Chris Lattner | 1e03a56 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 327 | DeclAttrs = new DeclAttrMapTy(); |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 328 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 329 | Attr *&ExistingAttr = (*DeclAttrs)[this]; |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 330 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 331 | NewAttr->setNext(ExistingAttr); |
| 332 | ExistingAttr = NewAttr; |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 333 | |
| 334 | HasAttrs = true; |
| 335 | } |
| 336 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 337 | const Attr *Decl::getAttrs() const { |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 338 | if (!HasAttrs) |
Anders Carlsson | b0dd268 | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 339 | return 0; |
| 340 | |
Anders Carlsson | eb7adf3 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 341 | return (*DeclAttrs)[this]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Chris Lattner | a212c56 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 344 | void Decl::swapAttrs(Decl *RHS) { |
| 345 | bool HasLHSAttr = this->HasAttrs; |
| 346 | bool HasRHSAttr = RHS->HasAttrs; |
| 347 | |
| 348 | // Usually, neither decl has attrs, nothing to do. |
| 349 | if (!HasLHSAttr && !HasRHSAttr) return; |
| 350 | |
| 351 | // If 'this' has no attrs, swap the other way. |
| 352 | if (!HasLHSAttr) |
| 353 | return RHS->swapAttrs(this); |
| 354 | |
| 355 | // Handle the case when both decls have attrs. |
| 356 | if (HasRHSAttr) { |
| 357 | std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | // Otherwise, LHS has an attr and RHS doesn't. |
| 362 | (*DeclAttrs)[RHS] = (*DeclAttrs)[this]; |
| 363 | (*DeclAttrs).erase(this); |
| 364 | this->HasAttrs = false; |
| 365 | RHS->HasAttrs = true; |
| 366 | } |
| 367 | |
| 368 | |
Sam Bishop | 1b30cb2 | 2008-04-13 04:32:18 +0000 | [diff] [blame] | 369 | #define CASE(KIND) \ |
| 370 | case KIND: \ |
| 371 | static_cast<KIND##Decl *>(const_cast<Decl *>(this))->~KIND##Decl(); \ |
| 372 | break |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 373 | |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 374 | void Decl::Destroy(ASTContext& C) const { |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 375 | switch (getKind()) { |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 376 | CASE(TranslationUnit); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 377 | CASE(Field); |
| 378 | CASE(ObjCIvar); |
| 379 | CASE(ObjCCategory); |
| 380 | CASE(ObjCCategoryImpl); |
| 381 | CASE(ObjCImplementation); |
| 382 | CASE(ObjCProtocol); |
| 383 | CASE(ObjCProperty); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 384 | CASE(Namespace); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 385 | CASE(Typedef); |
| 386 | CASE(Enum); |
| 387 | CASE(EnumConstant); |
| 388 | CASE(Function); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 389 | CASE(Var); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 390 | CASE(ParmVar); |
| 391 | CASE(ObjCInterface); |
| 392 | CASE(ObjCCompatibleAlias); |
| 393 | CASE(ObjCMethod); |
| 394 | CASE(ObjCClass); |
| 395 | CASE(ObjCForwardProtocol); |
| 396 | CASE(LinkageSpec); |
| 397 | |
| 398 | case Struct: case Union: case Class: |
Sam Bishop | 1b30cb2 | 2008-04-13 04:32:18 +0000 | [diff] [blame] | 399 | static_cast<RecordDecl *>(const_cast<Decl *>(this))->~RecordDecl(); |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 400 | break; |
| 401 | |
| 402 | default: assert(0 && "Unknown decl kind!"); |
| 403 | } |
| 404 | |
Sam Bishop | bb45c51 | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 405 | C.getAllocator().Deallocate((void *)this); |
| 406 | } |
| 407 | |
Sam Bishop | 1bb1963 | 2008-04-11 18:04:39 +0000 | [diff] [blame] | 408 | #undef CASE |
| 409 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 410 | //===----------------------------------------------------------------------===// |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 411 | // DeclContext Implementation |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 412 | //===----------------------------------------------------------------------===// |
| 413 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 414 | DeclContext *DeclContext::getParent() const { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 415 | if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this)) |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 416 | return SD->getDeclContext(); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 417 | else |
| 418 | return NULL; |
| 419 | } |
| 420 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 421 | Decl *DeclContext::ToDecl (const DeclContext *D) { |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 422 | return CastTo<Decl>(D); |
| 423 | } |
| 424 | |
Chris Lattner | b048c98 | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 425 | DeclContext *DeclContext::FromDecl (const Decl *D) { |
| 426 | return CastTo<DeclContext>(D); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 430 | // NamedDecl Implementation |
| 431 | //===----------------------------------------------------------------------===// |
| 432 | |
Chris Lattner | fd5de47 | 2007-10-06 22:53:46 +0000 | [diff] [blame] | 433 | const char *NamedDecl::getName() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 434 | if (const IdentifierInfo *II = getIdentifier()) |
| 435 | return II->getName(); |
| 436 | return ""; |
| 437 | } |
| 438 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 439 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 440 | // FunctionDecl Implementation |
| 441 | //===----------------------------------------------------------------------===// |
| 442 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 443 | FunctionDecl::~FunctionDecl() { |
| 444 | delete[] ParamInfo; |
Sam Bishop | 8266d7f | 2008-04-03 05:01:04 +0000 | [diff] [blame] | 445 | delete Body; |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 446 | delete PreviousDeclaration; |
| 447 | } |
| 448 | |
| 449 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
| 450 | for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) { |
| 451 | if (FD->Body) { |
| 452 | Definition = FD; |
| 453 | return FD->Body; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | unsigned FunctionDecl::getNumParams() const { |
Chris Lattner | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 461 | const FunctionType *FT = getType()->getAsFunctionType(); |
| 462 | if (isa<FunctionTypeNoProto>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 463 | return 0; |
Chris Lattner | d8bdba5 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 464 | return cast<FunctionTypeProto>(FT)->getNumArgs(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 468 | assert(ParamInfo == 0 && "Already has param info!"); |
| 469 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
| 470 | |
| 471 | // Zero params -> null pointer. |
| 472 | if (NumParams) { |
| 473 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 474 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 475 | } |
| 476 | } |
| 477 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 478 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 479 | /// needed to call this function. This may be fewer than the number of |
| 480 | /// function parameters, if some of the parameters have default |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 481 | /// arguments (in C++). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 482 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 483 | unsigned NumRequiredArgs = getNumParams(); |
| 484 | while (NumRequiredArgs > 0 |
| 485 | && getParamDecl(NumRequiredArgs-1)->getDefaultArg()) |
| 486 | --NumRequiredArgs; |
| 487 | |
| 488 | return NumRequiredArgs; |
| 489 | } |
| 490 | |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 491 | /// AddRedeclaration - Specifies that this function declaration has been |
| 492 | /// redeclared by the function declaration FD. FD must be a |
| 493 | /// redeclaration of this based on the semantics of the language being |
| 494 | /// translated ("compatible" function types in C, same signatures in |
| 495 | /// C++). |
| 496 | void FunctionDecl::AddRedeclaration(FunctionDecl *FD) { |
| 497 | assert(FD->PreviousDeclaration == 0 && |
| 498 | "Redeclaration already has a previous declaration!"); |
| 499 | |
| 500 | // Insert FD into the list of previous declarations of this |
| 501 | // function. |
| 502 | FD->PreviousDeclaration = this->PreviousDeclaration; |
| 503 | this->PreviousDeclaration = FD; |
| 504 | |
| 505 | // Swap the contents of this function declaration and FD. This |
| 506 | // effectively transforms the original declaration into the most |
| 507 | // recent declaration, so that all references to this declaration |
| 508 | // remain valid (and have information from *all* declarations), |
| 509 | // while retaining all of the information about previous |
| 510 | // declarations as well. |
| 511 | |
| 512 | // Swap parameters, so that the most recent parameter names and |
| 513 | // exact types (e.g., enum vs int) show up in the original |
| 514 | // declaration. |
Chris Lattner | a212c56 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 515 | std::swap(this->ParamInfo, FD->ParamInfo); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 516 | |
| 517 | // Swap the function body: all declarations share the same function |
| 518 | // body, but we keep track of who actually defined that function |
| 519 | // body by keeping the pointer to the body stored in that node. |
Chris Lattner | a212c56 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 520 | std::swap(this->Body, FD->Body); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 521 | |
| 522 | // Swap type information: this is important because in C, later |
| 523 | // declarations can provide slightly different types (enum vs. int, |
| 524 | // for example). |
| 525 | QualType thisType = this->getType(); |
| 526 | this->setType(FD->getType()); |
| 527 | FD->setType(thisType); |
| 528 | |
| 529 | // Swap location information: this allows us to produce diagnostics |
| 530 | // later on that reference the most recent declaration (which has |
| 531 | // the most information!) while retaining the location of previous |
| 532 | // declarations (good for "redefinition" diagnostics). |
| 533 | SourceLocation thisLocation = this->getLocation(); |
| 534 | this->setLocation(FD->getLocation()); |
| 535 | FD->setLocation(thisLocation); |
| 536 | |
| 537 | // Swap attributes. FD will have the union of the attributes from |
| 538 | // all previous declarations. |
Chris Lattner | a212c56 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 539 | this->swapAttrs(FD); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 540 | |
| 541 | // If any declaration is inline, the function is inline. |
| 542 | this->IsInline |= FD->IsInline; |
| 543 | |
| 544 | // FIXME: Is this the right way to handle storage specifiers? |
| 545 | if (FD->SClass) this->SClass = FD->SClass; |
| 546 | } |
| 547 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 548 | //===----------------------------------------------------------------------===// |
| 549 | // RecordDecl Implementation |
| 550 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 551 | |
| 552 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 553 | /// record. This method is used to mark the decl as being defined, with the |
| 554 | /// specified contents. |
| 555 | void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) { |
| 556 | assert(!isDefinition() && "Cannot redefine record!"); |
| 557 | setDefinition(true); |
| 558 | NumMembers = numMembers; |
| 559 | if (numMembers) { |
| 560 | Members = new FieldDecl*[numMembers]; |
| 561 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 562 | } |
| 563 | } |
| 564 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 565 | FieldDecl *RecordDecl::getMember(IdentifierInfo *II) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 566 | if (Members == 0 || NumMembers < 0) |
| 567 | return 0; |
Fariborz Jahanian | 3f5faf7 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 568 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 569 | // Linear search. When C++ classes come along, will likely need to revisit. |
| 570 | for (int i = 0; i != NumMembers; ++i) |
| 571 | if (Members[i]->getIdentifier() == II) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 572 | return Members[i]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 573 | return 0; |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 574 | } |