Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Decl class and subclasses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 3f70c54 | 2008-02-15 07:04:12 +0000 | [diff] [blame] | 16 | #include "clang/AST/Attr.h" |
Chris Lattner | 2fd1c65 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 17 | #include "clang/Basic/IdentifierTable.h" |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
| 23 | // Statistics |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 26 | // temporary statistics gathering |
| 27 | static unsigned nFuncs = 0; |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 28 | static unsigned nVars = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 29 | static unsigned nParmVars = 0; |
| 30 | static unsigned nSUC = 0; |
| 31 | static unsigned nEnumConst = 0; |
| 32 | static unsigned nEnumDecls = 0; |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 33 | static unsigned nNamespaces = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 34 | static unsigned nTypedef = 0; |
| 35 | static unsigned nFieldDecls = 0; |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 36 | static unsigned nInterfaceDecls = 0; |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 37 | static unsigned nClassDecls = 0; |
| 38 | static unsigned nMethodDecls = 0; |
| 39 | static unsigned nProtocolDecls = 0; |
Fariborz Jahanian | c716c94 | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 40 | static unsigned nForwardProtocolDecls = 0; |
Fariborz Jahanian | f25220e | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 41 | static unsigned nCategoryDecls = 0; |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 42 | static unsigned nIvarDecls = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 43 | static unsigned nObjCImplementationDecls = 0; |
| 44 | static unsigned nObjCCategoryImpl = 0; |
| 45 | static unsigned nObjCCompatibleAlias = 0; |
| 46 | static unsigned nObjCPropertyDecl = 0; |
Fariborz Jahanian | 65a8173 | 2008-04-16 22:00:24 +0000 | [diff] [blame] | 47 | static unsigned nObjCPropertyImplDecl = 0; |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 48 | static unsigned nLinkageSpecDecl = 0; |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 49 | static unsigned nFileScopeAsmDecl = 0; |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 51 | static bool StatSwitch = false; |
| 52 | |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 53 | // This keeps track of all decl attributes. Since so few decls have attrs, we |
| 54 | // keep them in a hash map instead of wasting space in the Decl class. |
| 55 | typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy; |
| 56 | |
| 57 | static DeclAttrMapTy *DeclAttrs = 0; |
| 58 | |
Steve Naroff | 590aba8 | 2007-09-17 14:49:06 +0000 | [diff] [blame] | 59 | const char *Decl::getDeclKindName() const { |
Steve Naroff | f0c31dd | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 60 | switch (DeclKind) { |
| 61 | default: assert(0 && "Unknown decl kind!"); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 62 | case Namespace: return "Namespace"; |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 63 | case Typedef: return "Typedef"; |
| 64 | case Function: return "Function"; |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 65 | case Var: return "Var"; |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 66 | case ParmVar: return "ParmVar"; |
| 67 | case EnumConstant: return "EnumConstant"; |
Steve Naroff | c38f435 | 2008-05-23 00:59:14 +0000 | [diff] [blame] | 68 | case ObjCIvar: return "ObjCIvar"; |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 69 | case ObjCInterface: return "ObjCInterface"; |
| 70 | case ObjCClass: return "ObjCClass"; |
| 71 | case ObjCMethod: return "ObjCMethod"; |
| 72 | case ObjCProtocol: return "ObjCProtocol"; |
| 73 | case ObjCForwardProtocol: return "ObjCForwardProtocol"; |
| 74 | case Struct: return "Struct"; |
| 75 | case Union: return "Union"; |
| 76 | case Class: return "Class"; |
| 77 | case Enum: return "Enum"; |
Steve Naroff | f0c31dd | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 81 | bool Decl::CollectingStats(bool Enable) { |
| 82 | if (Enable) |
| 83 | StatSwitch = true; |
| 84 | return StatSwitch; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void Decl::PrintStats() { |
| 88 | fprintf(stderr, "*** Decl Stats:\n"); |
| 89 | fprintf(stderr, " %d decls total.\n", |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 90 | int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+ |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 91 | nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+ |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 92 | nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+ |
| 93 | nNamespaces)); |
| 94 | fprintf(stderr, " %d namespace decls, %d each (%d bytes)\n", |
| 95 | nNamespaces, (int)sizeof(NamespaceDecl), |
| 96 | int(nNamespaces*sizeof(NamespaceDecl))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 97 | fprintf(stderr, " %d function decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 98 | nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl))); |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 99 | fprintf(stderr, " %d variable decls, %d each (%d bytes)\n", |
| 100 | nVars, (int)sizeof(VarDecl), |
| 101 | int(nVars*sizeof(VarDecl))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 102 | fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 103 | nParmVars, (int)sizeof(ParmVarDecl), |
| 104 | int(nParmVars*sizeof(ParmVarDecl))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 105 | fprintf(stderr, " %d field decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 106 | nFieldDecls, (int)sizeof(FieldDecl), |
| 107 | int(nFieldDecls*sizeof(FieldDecl))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 108 | fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 109 | nSUC, (int)sizeof(RecordDecl), |
| 110 | int(nSUC*sizeof(RecordDecl))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 111 | fprintf(stderr, " %d enum decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 112 | nEnumDecls, (int)sizeof(EnumDecl), |
| 113 | int(nEnumDecls*sizeof(EnumDecl))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 114 | fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 115 | nEnumConst, (int)sizeof(EnumConstantDecl), |
| 116 | int(nEnumConst*sizeof(EnumConstantDecl))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 117 | fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 118 | nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl))); |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 119 | // Objective-C decls... |
| 120 | fprintf(stderr, " %d interface decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 121 | nInterfaceDecls, (int)sizeof(ObjCInterfaceDecl), |
| 122 | int(nInterfaceDecls*sizeof(ObjCInterfaceDecl))); |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 123 | fprintf(stderr, " %d instance variable decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 124 | nIvarDecls, (int)sizeof(ObjCIvarDecl), |
| 125 | int(nIvarDecls*sizeof(ObjCIvarDecl))); |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 126 | fprintf(stderr, " %d class decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 127 | nClassDecls, (int)sizeof(ObjCClassDecl), |
| 128 | int(nClassDecls*sizeof(ObjCClassDecl))); |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 129 | fprintf(stderr, " %d method decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 130 | nMethodDecls, (int)sizeof(ObjCMethodDecl), |
| 131 | int(nMethodDecls*sizeof(ObjCMethodDecl))); |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 132 | fprintf(stderr, " %d protocol decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 133 | nProtocolDecls, (int)sizeof(ObjCProtocolDecl), |
| 134 | int(nProtocolDecls*sizeof(ObjCProtocolDecl))); |
Fariborz Jahanian | c716c94 | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 135 | fprintf(stderr, " %d forward protocol decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 136 | nForwardProtocolDecls, (int)sizeof(ObjCForwardProtocolDecl), |
| 137 | int(nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl))); |
Fariborz Jahanian | f25220e | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 138 | fprintf(stderr, " %d category decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 139 | nCategoryDecls, (int)sizeof(ObjCCategoryDecl), |
| 140 | int(nCategoryDecls*sizeof(ObjCCategoryDecl))); |
Steve Naroff | 948fd37 | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 141 | |
Fariborz Jahanian | c091b5d | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 142 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 143 | nObjCImplementationDecls, (int)sizeof(ObjCImplementationDecl), |
| 144 | int(nObjCImplementationDecls*sizeof(ObjCImplementationDecl))); |
Fariborz Jahanian | c091b5d | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 145 | |
Fariborz Jahanian | a91aa32 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 146 | fprintf(stderr, " %d class implementation decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 147 | nObjCCategoryImpl, (int)sizeof(ObjCCategoryImplDecl), |
| 148 | int(nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl))); |
Fariborz Jahanian | a91aa32 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 149 | |
Fariborz Jahanian | 05d212a | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 150 | fprintf(stderr, " %d compatibility alias decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 151 | nObjCCompatibleAlias, (int)sizeof(ObjCCompatibleAliasDecl), |
| 152 | int(nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl))); |
Fariborz Jahanian | 05d212a | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 153 | |
Fariborz Jahanian | d8df6d8 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 154 | fprintf(stderr, " %d property decls, %d each (%d bytes)\n", |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 155 | nObjCPropertyDecl, (int)sizeof(ObjCPropertyDecl), |
| 156 | int(nObjCPropertyDecl*sizeof(ObjCPropertyDecl))); |
Fariborz Jahanian | d8df6d8 | 2007-11-06 22:01:00 +0000 | [diff] [blame] | 157 | |
Fariborz Jahanian | 65a8173 | 2008-04-16 22:00:24 +0000 | [diff] [blame] | 158 | fprintf(stderr, " %d property implementation decls, %d each (%d bytes)\n", |
| 159 | nObjCPropertyImplDecl, (int)sizeof(ObjCPropertyImplDecl), |
| 160 | int(nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl))); |
| 161 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 162 | fprintf(stderr, "Total bytes = %d\n", |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 163 | int(nFuncs*sizeof(FunctionDecl)+ |
| 164 | nVars*sizeof(VarDecl)+nParmVars*sizeof(ParmVarDecl)+ |
Chris Lattner | 43b885f | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 165 | nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+ |
| 166 | nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+ |
| 167 | nTypedef*sizeof(TypedefDecl)+ |
Fariborz Jahanian | 9b0994f | 2008-01-23 01:34:33 +0000 | [diff] [blame] | 168 | nInterfaceDecls*sizeof(ObjCInterfaceDecl)+ |
| 169 | nIvarDecls*sizeof(ObjCIvarDecl)+ |
| 170 | nClassDecls*sizeof(ObjCClassDecl)+ |
| 171 | nMethodDecls*sizeof(ObjCMethodDecl)+ |
| 172 | nProtocolDecls*sizeof(ObjCProtocolDecl)+ |
| 173 | nForwardProtocolDecls*sizeof(ObjCForwardProtocolDecl)+ |
| 174 | nCategoryDecls*sizeof(ObjCCategoryDecl)+ |
| 175 | nObjCImplementationDecls*sizeof(ObjCImplementationDecl)+ |
| 176 | nObjCCategoryImpl*sizeof(ObjCCategoryImplDecl)+ |
| 177 | nObjCCompatibleAlias*sizeof(ObjCCompatibleAliasDecl)+ |
| 178 | nObjCPropertyDecl*sizeof(ObjCPropertyDecl)+ |
Fariborz Jahanian | 65a8173 | 2008-04-16 22:00:24 +0000 | [diff] [blame] | 179 | nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+ |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 180 | nLinkageSpecDecl*sizeof(LinkageSpecDecl)+ |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 181 | nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+ |
| 182 | nNamespaces*sizeof(NamespaceDecl))); |
Fariborz Jahanian | 9b0994f | 2008-01-23 01:34:33 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 186 | void Decl::addDeclKind(Kind k) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 187 | switch (k) { |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 188 | case Namespace: nNamespaces++; break; |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 189 | case Typedef: nTypedef++; break; |
| 190 | case Function: nFuncs++; break; |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 191 | case Var: nVars++; break; |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 192 | case ParmVar: nParmVars++; break; |
| 193 | case EnumConstant: nEnumConst++; break; |
| 194 | case Field: nFieldDecls++; break; |
| 195 | case Struct: case Union: case Class: nSUC++; break; |
| 196 | case Enum: nEnumDecls++; break; |
| 197 | case ObjCInterface: nInterfaceDecls++; break; |
| 198 | case ObjCClass: nClassDecls++; break; |
| 199 | case ObjCMethod: nMethodDecls++; break; |
| 200 | case ObjCProtocol: nProtocolDecls++; break; |
| 201 | case ObjCForwardProtocol: nForwardProtocolDecls++; break; |
| 202 | case ObjCCategory: nCategoryDecls++; break; |
| 203 | case ObjCIvar: nIvarDecls++; break; |
| 204 | case ObjCImplementation: nObjCImplementationDecls++; break; |
| 205 | case ObjCCategoryImpl: nObjCCategoryImpl++; break; |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 206 | case ObjCCompatibleAlias: nObjCCompatibleAlias++; break; |
Sam Bishop | be9c27a | 2008-04-08 20:49:25 +0000 | [diff] [blame] | 207 | case ObjCProperty: nObjCPropertyDecl++; break; |
Fariborz Jahanian | 65a8173 | 2008-04-16 22:00:24 +0000 | [diff] [blame] | 208 | case ObjCPropertyImpl: nObjCPropertyImplDecl++; break; |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 209 | case LinkageSpec: nLinkageSpecDecl++; break; |
| 210 | case FileScopeAsm: nFileScopeAsmDecl++; break; |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 211 | case TranslationUnit: break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 216 | // Decl Allocation/Deallocation Method Implementations |
| 217 | //===----------------------------------------------------------------------===// |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 218 | |
Argiris Kirtzidis | d358600 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 219 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 220 | void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>(); |
| 221 | return new (Mem) TranslationUnitDecl(); |
| 222 | } |
| 223 | |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 224 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| 225 | SourceLocation L, IdentifierInfo *Id) { |
| 226 | void *Mem = C.getAllocator().Allocate<NamespaceDecl>(); |
| 227 | return new (Mem) NamespaceDecl(DC, L, Id); |
| 228 | } |
| 229 | |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 230 | void NamespaceDecl::Destroy(ASTContext& C) { |
| 231 | // NamespaceDecl uses "NextDeclarator" to chain namespace declarations |
| 232 | // together. They are all top-level Decls. |
| 233 | |
Ted Kremenek | 0f43384 | 2008-05-24 15:09:56 +0000 | [diff] [blame] | 234 | this->~NamespaceDecl(); |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 235 | C.getAllocator().Deallocate((void *)this); |
| 236 | } |
| 237 | |
| 238 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 239 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 240 | SourceLocation L, |
| 241 | IdentifierInfo *Id, QualType T, |
| 242 | StorageClass S, ScopedDecl *PrevDecl) { |
| 243 | void *Mem = C.getAllocator().Allocate<VarDecl>(); |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 244 | return new (Mem) VarDecl(Var, DC, L, Id, T, S, PrevDecl); |
Chris Lattner | 48d225c | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 247 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 248 | SourceLocation L, IdentifierInfo *Id, |
| 249 | QualType T, StorageClass S, |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 250 | Expr *DefArg, ScopedDecl *PrevDecl) { |
Chris Lattner | 48d225c | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 251 | void *Mem = C.getAllocator().Allocate<ParmVarDecl>(); |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 252 | return new (Mem) ParmVarDecl(DC, L, Id, T, S, DefArg, PrevDecl); |
Chris Lattner | 48d225c | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 255 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 256 | SourceLocation L, |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 257 | IdentifierInfo *Id, QualType T, |
| 258 | StorageClass S, bool isInline, |
| 259 | ScopedDecl *PrevDecl) { |
| 260 | void *Mem = C.getAllocator().Allocate<FunctionDecl>(); |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 261 | return new (Mem) FunctionDecl(DC, L, Id, T, S, isInline, PrevDecl); |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 264 | FieldDecl *FieldDecl::Create(ASTContext &C, SourceLocation L, |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 265 | IdentifierInfo *Id, QualType T, Expr *BW) { |
| 266 | void *Mem = C.getAllocator().Allocate<FieldDecl>(); |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 267 | return new (Mem) FieldDecl(L, Id, T, BW); |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Chris Lattner | 4c7802b | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 270 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 271 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 272 | SourceLocation L, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 273 | IdentifierInfo *Id, QualType T, |
| 274 | Expr *E, const llvm::APSInt &V, |
| 275 | ScopedDecl *PrevDecl){ |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 276 | void *Mem = C.getAllocator().Allocate<EnumConstantDecl>(); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 277 | return new (Mem) EnumConstantDecl(CD, L, Id, T, E, V, PrevDecl); |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 280 | void EnumConstantDecl::Destroy(ASTContext& C) { |
| 281 | if (Init) Init->Destroy(C); |
| 282 | Decl::Destroy(C); |
| 283 | } |
| 284 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 285 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 286 | SourceLocation L, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 287 | IdentifierInfo *Id, QualType T, |
| 288 | ScopedDecl *PD) { |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 289 | void *Mem = C.getAllocator().Allocate<TypedefDecl>(); |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 290 | return new (Mem) TypedefDecl(DC, L, Id, T, PD); |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 293 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 294 | IdentifierInfo *Id, |
Chris Lattner | 58114f0 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 295 | ScopedDecl *PrevDecl) { |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 296 | void *Mem = C.getAllocator().Allocate<EnumDecl>(); |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 297 | return new (Mem) EnumDecl(DC, L, Id, PrevDecl); |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 300 | RecordDecl *RecordDecl::Create(ASTContext &C, Kind DK, DeclContext *DC, |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 301 | SourceLocation L, IdentifierInfo *Id, |
| 302 | ScopedDecl *PrevDecl) { |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 303 | void *Mem = C.getAllocator().Allocate<RecordDecl>(); |
Chris Lattner | ef87a20 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 304 | return new (Mem) RecordDecl(DK, DC, L, Id, PrevDecl); |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 307 | void EnumDecl::Destroy(ASTContext& C) { |
| 308 | if (ElementList) ElementList->Destroy(C); |
| 309 | Decl::Destroy(C); |
| 310 | } |
| 311 | |
| 312 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 313 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, |
| 314 | SourceLocation L, |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 315 | StringLiteral *Str) { |
| 316 | void *Mem = C.getAllocator().Allocate<FileScopeAsmDecl>(); |
| 317 | return new (Mem) FileScopeAsmDecl(L, Str); |
| 318 | } |
| 319 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 320 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
| 321 | SourceLocation L, |
Chris Lattner | 81db64a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 322 | LanguageIDs Lang, Decl *D) { |
| 323 | void *Mem = C.getAllocator().Allocate<LinkageSpecDecl>(); |
| 324 | return new (Mem) LinkageSpecDecl(L, Lang, D); |
| 325 | } |
Chris Lattner | e465048 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 326 | |
| 327 | //===----------------------------------------------------------------------===// |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 328 | // Decl Implementation |
| 329 | //===----------------------------------------------------------------------===// |
| 330 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 331 | // Out-of-line virtual method providing a home for Decl. |
| 332 | Decl::~Decl() { |
Anders Carlsson | b610a99 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 333 | if (!HasAttrs) |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 334 | return; |
| 335 | |
| 336 | DeclAttrMapTy::iterator it = DeclAttrs->find(this); |
Anders Carlsson | b610a99 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 337 | assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!"); |
| 338 | |
Nuno Lopes | 7765434 | 2008-06-01 22:53:53 +0000 | [diff] [blame^] | 339 | // release attributes. |
| 340 | delete it->second; |
| 341 | invalidateAttrs(); |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 344 | void Decl::addAttr(Attr *NewAttr) { |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 345 | if (!DeclAttrs) |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 346 | DeclAttrs = new DeclAttrMapTy(); |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 347 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 348 | Attr *&ExistingAttr = (*DeclAttrs)[this]; |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 349 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 350 | NewAttr->setNext(ExistingAttr); |
| 351 | ExistingAttr = NewAttr; |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 352 | |
| 353 | HasAttrs = true; |
| 354 | } |
| 355 | |
Nuno Lopes | 7765434 | 2008-06-01 22:53:53 +0000 | [diff] [blame^] | 356 | void Decl::invalidateAttrs() { |
| 357 | if (!HasAttrs) return; |
| 358 | |
| 359 | HasAttrs = false; |
| 360 | (*DeclAttrs)[this] = 0; |
| 361 | DeclAttrs->erase(this); |
| 362 | |
| 363 | if (DeclAttrs->empty()) { |
| 364 | delete DeclAttrs; |
| 365 | DeclAttrs = 0; |
| 366 | } |
| 367 | } |
| 368 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 369 | const Attr *Decl::getAttrs() const { |
Anders Carlsson | b610a99 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 370 | if (!HasAttrs) |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 371 | return 0; |
| 372 | |
Anders Carlsson | b610a99 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 373 | return (*DeclAttrs)[this]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Chris Lattner | 74bf555 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 376 | void Decl::swapAttrs(Decl *RHS) { |
| 377 | bool HasLHSAttr = this->HasAttrs; |
| 378 | bool HasRHSAttr = RHS->HasAttrs; |
| 379 | |
| 380 | // Usually, neither decl has attrs, nothing to do. |
| 381 | if (!HasLHSAttr && !HasRHSAttr) return; |
| 382 | |
| 383 | // If 'this' has no attrs, swap the other way. |
| 384 | if (!HasLHSAttr) |
| 385 | return RHS->swapAttrs(this); |
| 386 | |
| 387 | // Handle the case when both decls have attrs. |
| 388 | if (HasRHSAttr) { |
| 389 | std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | // Otherwise, LHS has an attr and RHS doesn't. |
| 394 | (*DeclAttrs)[RHS] = (*DeclAttrs)[this]; |
| 395 | (*DeclAttrs).erase(this); |
| 396 | this->HasAttrs = false; |
| 397 | RHS->HasAttrs = true; |
| 398 | } |
| 399 | |
| 400 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 401 | void Decl::Destroy(ASTContext& C) { |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 402 | |
| 403 | if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) { |
| 404 | |
| 405 | // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0 |
| 406 | // within the loop, only the Destroy method for the first ScopedDecl |
| 407 | // will deallocate all of the ScopedDecls in a chain. |
| 408 | |
| 409 | ScopedDecl* N = SD->getNextDeclarator(); |
| 410 | |
| 411 | while (N) { |
| 412 | ScopedDecl* Tmp = N->getNextDeclarator(); |
| 413 | N->NextDeclarator = 0x0; |
| 414 | N->Destroy(C); |
| 415 | N = Tmp; |
| 416 | } |
| 417 | } |
| 418 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 419 | this->~Decl(); |
Sam Bishop | 274366f | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 420 | C.getAllocator().Deallocate((void *)this); |
| 421 | } |
| 422 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 423 | //===----------------------------------------------------------------------===// |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 424 | // DeclContext Implementation |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 425 | //===----------------------------------------------------------------------===// |
| 426 | |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 427 | DeclContext *DeclContext::getParent() const { |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 428 | if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this)) |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 429 | return SD->getDeclContext(); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 430 | else |
| 431 | return NULL; |
| 432 | } |
| 433 | |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 434 | Decl *DeclContext::ToDecl (const DeclContext *D) { |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 435 | return CastTo<Decl>(D); |
| 436 | } |
| 437 | |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 438 | DeclContext *DeclContext::FromDecl (const Decl *D) { |
| 439 | return CastTo<DeclContext>(D); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | //===----------------------------------------------------------------------===// |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 443 | // NamedDecl Implementation |
| 444 | //===----------------------------------------------------------------------===// |
| 445 | |
Chris Lattner | 910435b | 2007-10-06 22:53:46 +0000 | [diff] [blame] | 446 | const char *NamedDecl::getName() const { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 447 | if (const IdentifierInfo *II = getIdentifier()) |
| 448 | return II->getName(); |
| 449 | return ""; |
| 450 | } |
| 451 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 452 | //===----------------------------------------------------------------------===// |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 453 | // FunctionDecl Implementation |
| 454 | //===----------------------------------------------------------------------===// |
| 455 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 456 | FunctionDecl::~FunctionDecl() { |
| 457 | delete[] ParamInfo; |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 460 | void FunctionDecl::Destroy(ASTContext& C) { |
Ted Kremenek | 345b93d | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 461 | if (Body) |
| 462 | Body->Destroy(C); |
| 463 | |
| 464 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 465 | (*I)->Destroy(C); |
| 466 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 467 | Decl::Destroy(C); |
| 468 | } |
| 469 | |
| 470 | |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 471 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
| 472 | for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) { |
| 473 | if (FD->Body) { |
| 474 | Definition = FD; |
| 475 | return FD->Body; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | unsigned FunctionDecl::getNumParams() const { |
Chris Lattner | e873359 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 483 | const FunctionType *FT = getType()->getAsFunctionType(); |
| 484 | if (isa<FunctionTypeNoProto>(FT)) |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 485 | return 0; |
Chris Lattner | e873359 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 486 | return cast<FunctionTypeProto>(FT)->getNumArgs(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 490 | assert(ParamInfo == 0 && "Already has param info!"); |
| 491 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
| 492 | |
| 493 | // Zero params -> null pointer. |
| 494 | if (NumParams) { |
| 495 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 496 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 497 | } |
| 498 | } |
| 499 | |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 500 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 501 | /// needed to call this function. This may be fewer than the number of |
| 502 | /// function parameters, if some of the parameters have default |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 503 | /// arguments (in C++). |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 504 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 505 | unsigned NumRequiredArgs = getNumParams(); |
| 506 | while (NumRequiredArgs > 0 |
| 507 | && getParamDecl(NumRequiredArgs-1)->getDefaultArg()) |
| 508 | --NumRequiredArgs; |
| 509 | |
| 510 | return NumRequiredArgs; |
| 511 | } |
| 512 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 513 | //===----------------------------------------------------------------------===// |
| 514 | // RecordDecl Implementation |
| 515 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 516 | |
| 517 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 518 | /// record. This method is used to mark the decl as being defined, with the |
| 519 | /// specified contents. |
| 520 | void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) { |
| 521 | assert(!isDefinition() && "Cannot redefine record!"); |
| 522 | setDefinition(true); |
| 523 | NumMembers = numMembers; |
| 524 | if (numMembers) { |
| 525 | Members = new FieldDecl*[numMembers]; |
| 526 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 527 | } |
| 528 | } |
| 529 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 530 | FieldDecl *RecordDecl::getMember(IdentifierInfo *II) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 531 | if (Members == 0 || NumMembers < 0) |
| 532 | return 0; |
Fariborz Jahanian | cbc36d4 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 533 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 534 | // Linear search. When C++ classes come along, will likely need to revisit. |
| 535 | for (int i = 0; i != NumMembers; ++i) |
| 536 | if (Members[i]->getIdentifier() == II) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 537 | return Members[i]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 538 | return 0; |
| 539 | } |