Decl.cpp 4.9 KB

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