Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Decl class and subclasses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
| 15 | #include "clang/Lex/IdentifierTable.h" |
| 16 | using namespace clang; |
| 17 | |
| 18 | // temporary statistics gathering |
| 19 | static unsigned nFuncs = 0; |
| 20 | static unsigned nBlockVars = 0; |
| 21 | static unsigned nFileVars = 0; |
| 22 | static unsigned nParmVars = 0; |
| 23 | static unsigned nSUC = 0; |
| 24 | static unsigned nEnumConst = 0; |
| 25 | static unsigned nEnumDecls = 0; |
| 26 | static unsigned nTypedef = 0; |
| 27 | static unsigned nFieldDecls = 0; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 28 | static unsigned nInterfaceDecls = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | static bool StatSwitch = false; |
| 30 | |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame^] | 31 | const char *Decl::getDeclKindName() { |
| 32 | switch (DeclKind) { |
| 33 | default: assert(0 && "Unknown decl kind!"); |
| 34 | case Typedef: |
| 35 | return "Typedef"; |
| 36 | case Function: |
| 37 | return "Function"; |
| 38 | case BlockVariable: |
| 39 | return "BlockVariable"; |
| 40 | case FileVariable: |
| 41 | return "FileVariable"; |
| 42 | case ParmVariable: |
| 43 | return "ParmVariable"; |
| 44 | case EnumConstant: |
| 45 | return "EnumConstant"; |
| 46 | case ObjcInterface: |
| 47 | return "ObjcInterface"; |
| 48 | case ObjcClass: |
| 49 | return "ObjcClass"; |
| 50 | case ObjcMethod: |
| 51 | return "ObjcMethod"; |
| 52 | case ObjcProtoMethod: |
| 53 | return "ObjcProtoMethod"; |
| 54 | case ObjcProtocol: |
| 55 | return "ObjcProtocol"; |
| 56 | case Struct: |
| 57 | return "Struct"; |
| 58 | case Union: |
| 59 | return "Union"; |
| 60 | case Class: |
| 61 | return "Class"; |
| 62 | case Enum: |
| 63 | return "Enum"; |
| 64 | } |
| 65 | } |
| 66 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | bool Decl::CollectingStats(bool enable) { |
| 68 | if (enable) StatSwitch = true; |
| 69 | return StatSwitch; |
| 70 | } |
| 71 | |
| 72 | void Decl::PrintStats() { |
| 73 | fprintf(stderr, "*** Decl Stats:\n"); |
| 74 | fprintf(stderr, " %d decls total.\n", |
| 75 | int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+ |
| 76 | nEnumDecls+nEnumConst+nTypedef)); |
| 77 | fprintf(stderr, " %d function decls, %d each (%d bytes)\n", |
| 78 | nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl))); |
| 79 | fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n", |
| 80 | nBlockVars, (int)sizeof(BlockVarDecl), |
| 81 | int(nBlockVars*sizeof(BlockVarDecl))); |
| 82 | fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n", |
| 83 | nFileVars, (int)sizeof(FileVarDecl), |
| 84 | int(nFileVars*sizeof(FileVarDecl))); |
| 85 | fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n", |
| 86 | nParmVars, (int)sizeof(ParmVarDecl), |
| 87 | int(nParmVars*sizeof(ParmVarDecl))); |
| 88 | fprintf(stderr, " %d field decls, %d each (%d bytes)\n", |
| 89 | nFieldDecls, (int)sizeof(FieldDecl), |
| 90 | int(nFieldDecls*sizeof(FieldDecl))); |
| 91 | fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n", |
| 92 | nSUC, (int)sizeof(RecordDecl), |
| 93 | int(nSUC*sizeof(RecordDecl))); |
| 94 | fprintf(stderr, " %d enum decls, %d each (%d bytes)\n", |
| 95 | nEnumDecls, (int)sizeof(EnumDecl), |
| 96 | int(nEnumDecls*sizeof(EnumDecl))); |
| 97 | fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n", |
| 98 | nEnumConst, (int)sizeof(EnumConstantDecl), |
| 99 | int(nEnumConst*sizeof(EnumConstantDecl))); |
| 100 | fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n", |
| 101 | nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl))); |
| 102 | fprintf(stderr, "Total bytes = %d\n", |
| 103 | int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+ |
| 104 | nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+ |
| 105 | nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+ |
| 106 | nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+ |
| 107 | nTypedef*sizeof(TypedefDecl))); |
| 108 | } |
| 109 | |
| 110 | void Decl::addDeclKind(const Kind k) { |
| 111 | switch (k) { |
| 112 | case Typedef: |
| 113 | nTypedef++; |
| 114 | break; |
| 115 | case Function: |
| 116 | nFuncs++; |
| 117 | break; |
| 118 | case BlockVariable: |
| 119 | nBlockVars++; |
| 120 | break; |
| 121 | case FileVariable: |
| 122 | nFileVars++; |
| 123 | break; |
| 124 | case ParmVariable: |
| 125 | nParmVars++; |
| 126 | break; |
| 127 | case EnumConstant: |
| 128 | nEnumConst++; |
| 129 | break; |
| 130 | case Field: |
| 131 | nFieldDecls++; |
| 132 | break; |
| 133 | case Struct: |
| 134 | case Union: |
| 135 | case Class: |
| 136 | nSUC++; |
| 137 | break; |
| 138 | case Enum: |
| 139 | nEnumDecls++; |
| 140 | break; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 141 | case ObjcInterface: |
| 142 | nInterfaceDecls++; |
| 143 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | // Out-of-line virtual method providing a home for Decl. |
| 148 | Decl::~Decl() { |
| 149 | } |
| 150 | |
Steve Naroff | 8e74c93 | 2007-09-13 21:41:19 +0000 | [diff] [blame] | 151 | const char *FieldDecl::getName() const { |
| 152 | if (const IdentifierInfo *II = getIdentifier()) |
| 153 | return II->getName(); |
| 154 | return ""; |
| 155 | } |
| 156 | |
| 157 | const char *ScopedDecl::getName() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | if (const IdentifierInfo *II = getIdentifier()) |
| 159 | return II->getName(); |
| 160 | return ""; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | FunctionDecl::~FunctionDecl() { |
| 165 | delete[] ParamInfo; |
| 166 | } |
| 167 | |
| 168 | unsigned FunctionDecl::getNumParams() const { |
| 169 | return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs(); |
| 170 | } |
| 171 | |
| 172 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 173 | assert(ParamInfo == 0 && "Already has param info!"); |
| 174 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
| 175 | |
| 176 | // Zero params -> null pointer. |
| 177 | if (NumParams) { |
| 178 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 179 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 185 | /// record. This method is used to mark the decl as being defined, with the |
| 186 | /// specified contents. |
| 187 | void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) { |
| 188 | assert(!isDefinition() && "Cannot redefine record!"); |
| 189 | setDefinition(true); |
| 190 | NumMembers = numMembers; |
| 191 | if (numMembers) { |
| 192 | Members = new FieldDecl*[numMembers]; |
| 193 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | FieldDecl* RecordDecl::getMember(IdentifierInfo *name) { |
| 198 | if (Members == 0 || NumMembers < 0) |
| 199 | return 0; |
| 200 | |
| 201 | // linear search. When C++ classes come along, will likely need to revisit. |
| 202 | for (int i = 0; i < NumMembers; ++i) { |
| 203 | if (Members[i]->getIdentifier() == name) |
| 204 | return Members[i]; |
| 205 | } |
| 206 | return 0; |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 207 | } |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 208 | |
Fariborz Jahanian | e55cd00 | 2007-09-12 18:23:47 +0000 | [diff] [blame] | 209 | void ObjcMethodDecl::setMethodParams(ParmVarDecl **NewParamInfo, |
| 210 | unsigned NumParams) { |
| 211 | assert(ParamInfo == 0 && "Already has param info!"); |
| 212 | |
| 213 | // Zero params -> null pointer. |
| 214 | if (NumParams) { |
| 215 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 216 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 217 | NumMethodParams = NumParams; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | ObjcMethodDecl::~ObjcMethodDecl() { |
| 222 | delete[] ParamInfo; |
| 223 | } |
| 224 | |
Fariborz Jahanian | b04a021 | 2007-09-14 21:08:27 +0000 | [diff] [blame] | 225 | /// ObjcAddInstanceVariablesToClass - Inserts instance variables |
| 226 | /// into ObjcInterfaceDecl's fields. |
| 227 | /// |
| 228 | void ObjcInterfaceDecl::ObjcAddInstanceVariablesToClass(ObjcIvarDecl **ivars, |
| 229 | unsigned numIvars) { |
| 230 | NumIvars = numIvars; |
| 231 | if (numIvars) { |
| 232 | Ivars = new ObjcIvarDecl*[numIvars]; |
| 233 | memcpy(Ivars, ivars, numIvars*sizeof(ObjcIvarDecl*)); |
| 234 | } |
| 235 | } |
| 236 | |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 237 | /// addObjcMethods - Insert instance and methods declarations into |
| 238 | /// ObjcInterfaceDecl's InsMethods and ClsMethods fields. |
| 239 | /// |
| 240 | void ObjcInterfaceDecl::ObjcAddMethods(ObjcMethodDecl **insMethods, |
| 241 | unsigned numInsMembers, |
| 242 | ObjcMethodDecl **clsMethods, |
| 243 | unsigned numClsMembers) { |
| 244 | NumInsMethods = numInsMembers; |
| 245 | if (numInsMembers) { |
| 246 | InsMethods = new ObjcMethodDecl*[numInsMembers]; |
| 247 | memcpy(InsMethods, insMethods, numInsMembers*sizeof(ObjcMethodDecl*)); |
| 248 | } |
| 249 | NumClsMethods = numClsMembers; |
| 250 | if (numClsMembers) { |
| 251 | ClsMethods = new ObjcMethodDecl*[numClsMembers]; |
| 252 | memcpy(ClsMethods, clsMethods, numClsMembers*sizeof(ObjcMethodDecl*)); |
| 253 | } |
| 254 | } |
| 255 | |