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