cppEnumType.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Filename: cppEnumType.C
  2. // Created by: drose (25Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "cppEnumType.h"
  6. #include "cppTypedef.h"
  7. #include "cppExpression.h"
  8. #include "cppSimpleType.h"
  9. #include "cppScope.h"
  10. #include "cppParser.h"
  11. #include "indent.h"
  12. ////////////////////////////////////////////////////////////////////
  13. // Function: CPPEnumType::Constructor
  14. // Access: Public
  15. // Description:
  16. ////////////////////////////////////////////////////////////////////
  17. CPPEnumType::
  18. CPPEnumType(CPPIdentifier *ident, CPPScope *current_scope,
  19. const CPPFile &file) :
  20. CPPExtensionType(T_enum, ident, current_scope, file)
  21. {
  22. }
  23. ////////////////////////////////////////////////////////////////////
  24. // Function: CPPEnumType::add_element
  25. // Access: Public
  26. // Description:
  27. ////////////////////////////////////////////////////////////////////
  28. void CPPEnumType::
  29. add_element(const string &name, CPPScope *scope, CPPExpression *value) {
  30. CPPType *type =
  31. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  32. CPPSimpleType::F_unsigned));
  33. CPPIdentifier *ident = new CPPIdentifier(name);
  34. CPPInstance *inst = new CPPInstance(type, ident);
  35. inst->_initializer = value;
  36. _elements.push_back(inst);
  37. scope->add_enum_value(inst);
  38. }
  39. ////////////////////////////////////////////////////////////////////
  40. // Function: CPPEnumType::is_incomplete
  41. // Access: Public, Virtual
  42. // Description: Returns true if the type has not yet been fully
  43. // specified, false if it has.
  44. ////////////////////////////////////////////////////////////////////
  45. bool CPPEnumType::
  46. is_incomplete() const {
  47. return false;
  48. }
  49. ////////////////////////////////////////////////////////////////////
  50. // Function: CPPEnumType::substitute_decl
  51. // Access: Public, Virtual
  52. // Description:
  53. ////////////////////////////////////////////////////////////////////
  54. CPPDeclaration *CPPEnumType::
  55. substitute_decl(CPPDeclaration::SubstDecl &subst,
  56. CPPScope *current_scope, CPPScope *global_scope) {
  57. SubstDecl::const_iterator si = subst.find(this);
  58. if (si != subst.end()) {
  59. return (*si).second;
  60. }
  61. CPPEnumType *rep = new CPPEnumType(*this);
  62. if (_ident != NULL) {
  63. rep->_ident =
  64. _ident->substitute_decl(subst, current_scope, global_scope);
  65. }
  66. if (rep->_ident == _ident) {
  67. delete rep;
  68. rep = this;
  69. }
  70. rep = CPPType::new_type(rep)->as_enum_type();
  71. subst.insert(SubstDecl::value_type(this, rep));
  72. return rep;
  73. }
  74. ////////////////////////////////////////////////////////////////////
  75. // Function: CPPEnumType::output
  76. // Access: Public, Virtual
  77. // Description:
  78. ////////////////////////////////////////////////////////////////////
  79. void CPPEnumType::
  80. output(ostream &out, int indent_level, CPPScope *scope, bool complete) const {
  81. if (!complete && _ident != NULL) {
  82. // If we have a name, use it.
  83. if (cppparser_output_class_keyword) {
  84. out << _type << " ";
  85. }
  86. out << _ident->get_local_name(scope);
  87. } else if (!complete && !_typedefs.empty()) {
  88. // If we have a typedef name, use it.
  89. out << _typedefs.front()->get_local_name(scope);
  90. } else {
  91. out << _type;
  92. if (_ident != NULL) {
  93. out << " " << _ident->get_local_name(scope);
  94. }
  95. out << " {\n";
  96. Elements::const_iterator ei;
  97. for (ei = _elements.begin(); ei != _elements.end(); ++ei) {
  98. indent(out, indent_level + 2) << (*ei)->get_local_name();
  99. if ((*ei)->_initializer != NULL) {
  100. out << " = " << *(*ei)->_initializer;
  101. }
  102. out << ",\n";
  103. }
  104. indent(out, indent_level) << "}";
  105. }
  106. }
  107. ////////////////////////////////////////////////////////////////////
  108. // Function: CPPEnumType::get_subtype
  109. // Access: Public, Virtual
  110. // Description:
  111. ////////////////////////////////////////////////////////////////////
  112. CPPDeclaration::SubType CPPEnumType::
  113. get_subtype() const {
  114. return ST_enum;
  115. }
  116. ////////////////////////////////////////////////////////////////////
  117. // Function: CPPEnumType::as_enum_type
  118. // Access: Public, Virtual
  119. // Description:
  120. ////////////////////////////////////////////////////////////////////
  121. CPPEnumType *CPPEnumType::
  122. as_enum_type() {
  123. return this;
  124. }