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 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 339 | // FIXME: Properly release attributes. |
| 340 | // delete it->second; |
Anders Carlsson | b610a99 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 341 | DeclAttrs->erase(it); |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 342 | |
Anders Carlsson | b610a99 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 343 | if (DeclAttrs->empty()) { |
| 344 | delete DeclAttrs; |
| 345 | DeclAttrs = 0; |
| 346 | } |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 349 | void Decl::addAttr(Attr *NewAttr) { |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 350 | if (!DeclAttrs) |
Chris Lattner | 10318b8 | 2008-03-16 00:19:01 +0000 | [diff] [blame] | 351 | DeclAttrs = new DeclAttrMapTy(); |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 352 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 353 | Attr *&ExistingAttr = (*DeclAttrs)[this]; |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 354 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 355 | NewAttr->setNext(ExistingAttr); |
| 356 | ExistingAttr = NewAttr; |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 357 | |
| 358 | HasAttrs = true; |
| 359 | } |
| 360 | |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 361 | const Attr *Decl::getAttrs() const { |
Anders Carlsson | b610a99 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 362 | if (!HasAttrs) |
Anders Carlsson | 484eb5f | 2008-02-15 23:30:50 +0000 | [diff] [blame] | 363 | return 0; |
| 364 | |
Anders Carlsson | b610a99 | 2008-02-16 03:37:41 +0000 | [diff] [blame] | 365 | return (*DeclAttrs)[this]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Chris Lattner | 74bf555 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 368 | void Decl::swapAttrs(Decl *RHS) { |
| 369 | bool HasLHSAttr = this->HasAttrs; |
| 370 | bool HasRHSAttr = RHS->HasAttrs; |
| 371 | |
| 372 | // Usually, neither decl has attrs, nothing to do. |
| 373 | if (!HasLHSAttr && !HasRHSAttr) return; |
| 374 | |
| 375 | // If 'this' has no attrs, swap the other way. |
| 376 | if (!HasLHSAttr) |
| 377 | return RHS->swapAttrs(this); |
| 378 | |
| 379 | // Handle the case when both decls have attrs. |
| 380 | if (HasRHSAttr) { |
| 381 | std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | // Otherwise, LHS has an attr and RHS doesn't. |
| 386 | (*DeclAttrs)[RHS] = (*DeclAttrs)[this]; |
| 387 | (*DeclAttrs).erase(this); |
| 388 | this->HasAttrs = false; |
| 389 | RHS->HasAttrs = true; |
| 390 | } |
| 391 | |
| 392 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 393 | void Decl::Destroy(ASTContext& C) { |
Ted Kremenek | 5be4924 | 2008-05-20 04:49:55 +0000 | [diff] [blame] | 394 | |
| 395 | if (ScopedDecl* SD = dyn_cast<ScopedDecl>(this)) { |
| 396 | |
| 397 | // Observe the unrolled recursion. By setting N->NextDeclarator = 0x0 |
| 398 | // within the loop, only the Destroy method for the first ScopedDecl |
| 399 | // will deallocate all of the ScopedDecls in a chain. |
| 400 | |
| 401 | ScopedDecl* N = SD->getNextDeclarator(); |
| 402 | |
| 403 | while (N) { |
| 404 | ScopedDecl* Tmp = N->getNextDeclarator(); |
| 405 | N->NextDeclarator = 0x0; |
| 406 | N->Destroy(C); |
| 407 | N = Tmp; |
| 408 | } |
| 409 | } |
| 410 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 411 | this->~Decl(); |
Sam Bishop | 274366f | 2008-04-11 15:01:25 +0000 | [diff] [blame] | 412 | C.getAllocator().Deallocate((void *)this); |
| 413 | } |
| 414 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 415 | //===----------------------------------------------------------------------===// |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 416 | // DeclContext Implementation |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 417 | //===----------------------------------------------------------------------===// |
| 418 | |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 419 | DeclContext *DeclContext::getParent() const { |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 420 | if (ScopedDecl *SD = dyn_cast<ScopedDecl>(this)) |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 421 | return SD->getDeclContext(); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 422 | else |
| 423 | return NULL; |
| 424 | } |
| 425 | |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 426 | Decl *DeclContext::ToDecl (const DeclContext *D) { |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 427 | return CastTo<Decl>(D); |
| 428 | } |
| 429 | |
Chris Lattner | f3874bc | 2008-04-06 04:47:34 +0000 | [diff] [blame] | 430 | DeclContext *DeclContext::FromDecl (const Decl *D) { |
| 431 | return CastTo<DeclContext>(D); |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | //===----------------------------------------------------------------------===// |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 435 | // NamedDecl Implementation |
| 436 | //===----------------------------------------------------------------------===// |
| 437 | |
Chris Lattner | 910435b | 2007-10-06 22:53:46 +0000 | [diff] [blame] | 438 | const char *NamedDecl::getName() const { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 439 | if (const IdentifierInfo *II = getIdentifier()) |
| 440 | return II->getName(); |
| 441 | return ""; |
| 442 | } |
| 443 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 444 | //===----------------------------------------------------------------------===// |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 445 | // FunctionDecl Implementation |
| 446 | //===----------------------------------------------------------------------===// |
| 447 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 448 | FunctionDecl::~FunctionDecl() { |
| 449 | delete[] ParamInfo; |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 452 | void FunctionDecl::Destroy(ASTContext& C) { |
Ted Kremenek | 345b93d | 2008-05-20 03:56:00 +0000 | [diff] [blame] | 453 | if (Body) |
| 454 | Body->Destroy(C); |
| 455 | |
| 456 | for (param_iterator I=param_begin(), E=param_end(); I!=E; ++I) |
| 457 | (*I)->Destroy(C); |
| 458 | |
Ted Kremenek | afdf811 | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 459 | Decl::Destroy(C); |
| 460 | } |
| 461 | |
| 462 | |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 463 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
| 464 | for (const FunctionDecl *FD = this; FD != 0; FD = FD->PreviousDeclaration) { |
| 465 | if (FD->Body) { |
| 466 | Definition = FD; |
| 467 | return FD->Body; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | unsigned FunctionDecl::getNumParams() const { |
Chris Lattner | e873359 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 475 | const FunctionType *FT = getType()->getAsFunctionType(); |
| 476 | if (isa<FunctionTypeNoProto>(FT)) |
Chris Lattner | a8344c3 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 477 | return 0; |
Chris Lattner | e873359 | 2008-04-06 23:09:52 +0000 | [diff] [blame] | 478 | return cast<FunctionTypeProto>(FT)->getNumArgs(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 482 | assert(ParamInfo == 0 && "Already has param info!"); |
| 483 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
| 484 | |
| 485 | // Zero params -> null pointer. |
| 486 | if (NumParams) { |
| 487 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 488 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 489 | } |
| 490 | } |
| 491 | |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 492 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 493 | /// needed to call this function. This may be fewer than the number of |
| 494 | /// function parameters, if some of the parameters have default |
Chris Lattner | b1856db | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 495 | /// arguments (in C++). |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 496 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 497 | unsigned NumRequiredArgs = getNumParams(); |
| 498 | while (NumRequiredArgs > 0 |
| 499 | && getParamDecl(NumRequiredArgs-1)->getDefaultArg()) |
| 500 | --NumRequiredArgs; |
| 501 | |
| 502 | return NumRequiredArgs; |
| 503 | } |
| 504 | |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 505 | /// AddRedeclaration - Specifies that this function declaration has been |
| 506 | /// redeclared by the function declaration FD. FD must be a |
| 507 | /// redeclaration of this based on the semantics of the language being |
| 508 | /// translated ("compatible" function types in C, same signatures in |
| 509 | /// C++). |
| 510 | void FunctionDecl::AddRedeclaration(FunctionDecl *FD) { |
| 511 | assert(FD->PreviousDeclaration == 0 && |
| 512 | "Redeclaration already has a previous declaration!"); |
| 513 | |
| 514 | // Insert FD into the list of previous declarations of this |
| 515 | // function. |
| 516 | FD->PreviousDeclaration = this->PreviousDeclaration; |
| 517 | this->PreviousDeclaration = FD; |
| 518 | |
| 519 | // Swap the contents of this function declaration and FD. This |
| 520 | // effectively transforms the original declaration into the most |
| 521 | // recent declaration, so that all references to this declaration |
| 522 | // remain valid (and have information from *all* declarations), |
| 523 | // while retaining all of the information about previous |
| 524 | // declarations as well. |
| 525 | |
| 526 | // Swap parameters, so that the most recent parameter names and |
| 527 | // exact types (e.g., enum vs int) show up in the original |
| 528 | // declaration. |
Chris Lattner | 74bf555 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 529 | std::swap(this->ParamInfo, FD->ParamInfo); |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 530 | |
| 531 | // Swap the function body: all declarations share the same function |
| 532 | // body, but we keep track of who actually defined that function |
| 533 | // body by keeping the pointer to the body stored in that node. |
Chris Lattner | 74bf555 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 534 | std::swap(this->Body, FD->Body); |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 535 | |
| 536 | // Swap type information: this is important because in C, later |
| 537 | // declarations can provide slightly different types (enum vs. int, |
| 538 | // for example). |
| 539 | QualType thisType = this->getType(); |
| 540 | this->setType(FD->getType()); |
| 541 | FD->setType(thisType); |
| 542 | |
| 543 | // Swap location information: this allows us to produce diagnostics |
| 544 | // later on that reference the most recent declaration (which has |
| 545 | // the most information!) while retaining the location of previous |
| 546 | // declarations (good for "redefinition" diagnostics). |
| 547 | SourceLocation thisLocation = this->getLocation(); |
| 548 | this->setLocation(FD->getLocation()); |
| 549 | FD->setLocation(thisLocation); |
| 550 | |
| 551 | // Swap attributes. FD will have the union of the attributes from |
| 552 | // all previous declarations. |
Chris Lattner | 74bf555 | 2008-05-04 02:29:49 +0000 | [diff] [blame] | 553 | this->swapAttrs(FD); |
Douglas Gregor | 42214c5 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 554 | |
| 555 | // If any declaration is inline, the function is inline. |
| 556 | this->IsInline |= FD->IsInline; |
| 557 | |
| 558 | // FIXME: Is this the right way to handle storage specifiers? |
| 559 | if (FD->SClass) this->SClass = FD->SClass; |
| 560 | } |
| 561 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 562 | //===----------------------------------------------------------------------===// |
| 563 | // RecordDecl Implementation |
| 564 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 565 | |
| 566 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 567 | /// record. This method is used to mark the decl as being defined, with the |
| 568 | /// specified contents. |
| 569 | void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) { |
| 570 | assert(!isDefinition() && "Cannot redefine record!"); |
| 571 | setDefinition(true); |
| 572 | NumMembers = numMembers; |
| 573 | if (numMembers) { |
| 574 | Members = new FieldDecl*[numMembers]; |
| 575 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 576 | } |
| 577 | } |
| 578 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 579 | FieldDecl *RecordDecl::getMember(IdentifierInfo *II) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 580 | if (Members == 0 || NumMembers < 0) |
| 581 | return 0; |
Fariborz Jahanian | cbc36d4 | 2007-10-04 00:45:27 +0000 | [diff] [blame] | 582 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 583 | // Linear search. When C++ classes come along, will likely need to revisit. |
| 584 | for (int i = 0; i != NumMembers; ++i) |
| 585 | if (Members[i]->getIdentifier() == II) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 586 | return Members[i]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 587 | return 0; |
| 588 | } |