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 | |
| 31 | bool Decl::CollectingStats(bool enable) { |
| 32 | if (enable) StatSwitch = true; |
| 33 | return StatSwitch; |
| 34 | } |
| 35 | |
| 36 | void Decl::PrintStats() { |
| 37 | fprintf(stderr, "*** Decl Stats:\n"); |
| 38 | fprintf(stderr, " %d decls total.\n", |
| 39 | int(nFuncs+nBlockVars+nFileVars+nParmVars+nFieldDecls+nSUC+ |
| 40 | nEnumDecls+nEnumConst+nTypedef)); |
| 41 | fprintf(stderr, " %d function decls, %d each (%d bytes)\n", |
| 42 | nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl))); |
| 43 | fprintf(stderr, " %d block variable decls, %d each (%d bytes)\n", |
| 44 | nBlockVars, (int)sizeof(BlockVarDecl), |
| 45 | int(nBlockVars*sizeof(BlockVarDecl))); |
| 46 | fprintf(stderr, " %d file variable decls, %d each (%d bytes)\n", |
| 47 | nFileVars, (int)sizeof(FileVarDecl), |
| 48 | int(nFileVars*sizeof(FileVarDecl))); |
| 49 | fprintf(stderr, " %d parameter variable decls, %d each (%d bytes)\n", |
| 50 | nParmVars, (int)sizeof(ParmVarDecl), |
| 51 | int(nParmVars*sizeof(ParmVarDecl))); |
| 52 | fprintf(stderr, " %d field decls, %d each (%d bytes)\n", |
| 53 | nFieldDecls, (int)sizeof(FieldDecl), |
| 54 | int(nFieldDecls*sizeof(FieldDecl))); |
| 55 | fprintf(stderr, " %d struct/union/class decls, %d each (%d bytes)\n", |
| 56 | nSUC, (int)sizeof(RecordDecl), |
| 57 | int(nSUC*sizeof(RecordDecl))); |
| 58 | fprintf(stderr, " %d enum decls, %d each (%d bytes)\n", |
| 59 | nEnumDecls, (int)sizeof(EnumDecl), |
| 60 | int(nEnumDecls*sizeof(EnumDecl))); |
| 61 | fprintf(stderr, " %d enum constant decls, %d each (%d bytes)\n", |
| 62 | nEnumConst, (int)sizeof(EnumConstantDecl), |
| 63 | int(nEnumConst*sizeof(EnumConstantDecl))); |
| 64 | fprintf(stderr, " %d typedef decls, %d each (%d bytes)\n", |
| 65 | nTypedef, (int)sizeof(TypedefDecl),int(nTypedef*sizeof(TypedefDecl))); |
| 66 | fprintf(stderr, "Total bytes = %d\n", |
| 67 | int(nFuncs*sizeof(FunctionDecl)+nBlockVars*sizeof(BlockVarDecl)+ |
| 68 | nFileVars*sizeof(FileVarDecl)+nParmVars*sizeof(ParmVarDecl)+ |
| 69 | nFieldDecls*sizeof(FieldDecl)+nSUC*sizeof(RecordDecl)+ |
| 70 | nEnumDecls*sizeof(EnumDecl)+nEnumConst*sizeof(EnumConstantDecl)+ |
| 71 | nTypedef*sizeof(TypedefDecl))); |
| 72 | } |
| 73 | |
| 74 | void Decl::addDeclKind(const Kind k) { |
| 75 | switch (k) { |
| 76 | case Typedef: |
| 77 | nTypedef++; |
| 78 | break; |
| 79 | case Function: |
| 80 | nFuncs++; |
| 81 | break; |
| 82 | case BlockVariable: |
| 83 | nBlockVars++; |
| 84 | break; |
| 85 | case FileVariable: |
| 86 | nFileVars++; |
| 87 | break; |
| 88 | case ParmVariable: |
| 89 | nParmVars++; |
| 90 | break; |
| 91 | case EnumConstant: |
| 92 | nEnumConst++; |
| 93 | break; |
| 94 | case Field: |
| 95 | nFieldDecls++; |
| 96 | break; |
| 97 | case Struct: |
| 98 | case Union: |
| 99 | case Class: |
| 100 | nSUC++; |
| 101 | break; |
| 102 | case Enum: |
| 103 | nEnumDecls++; |
| 104 | break; |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame^] | 105 | case ObjcInterface: |
| 106 | nInterfaceDecls++; |
| 107 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | // Out-of-line virtual method providing a home for Decl. |
| 112 | Decl::~Decl() { |
| 113 | } |
| 114 | |
| 115 | const char *Decl::getName() const { |
| 116 | if (const IdentifierInfo *II = getIdentifier()) |
| 117 | return II->getName(); |
| 118 | return ""; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | FunctionDecl::~FunctionDecl() { |
| 123 | delete[] ParamInfo; |
| 124 | } |
| 125 | |
| 126 | unsigned FunctionDecl::getNumParams() const { |
| 127 | return cast<FunctionTypeProto>(getType().getTypePtr())->getNumArgs(); |
| 128 | } |
| 129 | |
| 130 | void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) { |
| 131 | assert(ParamInfo == 0 && "Already has param info!"); |
| 132 | assert(NumParams == getNumParams() && "Parameter count mismatch!"); |
| 133 | |
| 134 | // Zero params -> null pointer. |
| 135 | if (NumParams) { |
| 136 | ParamInfo = new ParmVarDecl*[NumParams]; |
| 137 | memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /// defineBody - When created, RecordDecl's correspond to a forward declared |
| 143 | /// record. This method is used to mark the decl as being defined, with the |
| 144 | /// specified contents. |
| 145 | void RecordDecl::defineBody(FieldDecl **members, unsigned numMembers) { |
| 146 | assert(!isDefinition() && "Cannot redefine record!"); |
| 147 | setDefinition(true); |
| 148 | NumMembers = numMembers; |
| 149 | if (numMembers) { |
| 150 | Members = new FieldDecl*[numMembers]; |
| 151 | memcpy(Members, members, numMembers*sizeof(Decl*)); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | FieldDecl* RecordDecl::getMember(IdentifierInfo *name) { |
| 156 | if (Members == 0 || NumMembers < 0) |
| 157 | return 0; |
| 158 | |
| 159 | // linear search. When C++ classes come along, will likely need to revisit. |
| 160 | for (int i = 0; i < NumMembers; ++i) { |
| 161 | if (Members[i]->getIdentifier() == name) |
| 162 | return Members[i]; |
| 163 | } |
| 164 | return 0; |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 165 | } |