cppSimpleType.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Filename: cppSimpleType.cxx
  2. // Created by: drose (19Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "cppSimpleType.h"
  15. #include "cppGlobals.h"
  16. ////////////////////////////////////////////////////////////////////
  17. // Function: CPPSimpleType::Constructor
  18. // Access: Public
  19. // Description:
  20. ////////////////////////////////////////////////////////////////////
  21. CPPSimpleType::
  22. CPPSimpleType(CPPSimpleType::Type type, int flags) :
  23. CPPType(CPPFile()),
  24. _type(type), _flags(flags)
  25. {
  26. }
  27. ////////////////////////////////////////////////////////////////////
  28. // Function: CPPSimpleType::is_tbd
  29. // Access: Public, Virtual
  30. // Description: Returns true if the type, or any nested type within
  31. // the type, is a CPPTBDType and thus isn't fully
  32. // determined right now. In this case, calling
  33. // resolve_type() may or may not resolve the type.
  34. ////////////////////////////////////////////////////////////////////
  35. bool CPPSimpleType::
  36. is_tbd() const {
  37. return (_type == T_unknown);
  38. }
  39. ////////////////////////////////////////////////////////////////////
  40. // Function: CPPSimpleType::is_trivial
  41. // Access: Public, Virtual
  42. // Description: Returns true if the type is considered a Plain Old
  43. // Data (POD) type.
  44. ////////////////////////////////////////////////////////////////////
  45. bool CPPSimpleType::
  46. is_trivial() const {
  47. return true;
  48. }
  49. ////////////////////////////////////////////////////////////////////
  50. // Function: CPPSimpleType::is_parameter_expr
  51. // Access: Public, Virtual
  52. // Description: Returns true if the type is a special parameter
  53. // expression type.
  54. //
  55. // This sort of type is created to handle instance
  56. // declarations that initially look like function
  57. // prototypes.
  58. ////////////////////////////////////////////////////////////////////
  59. bool CPPSimpleType::
  60. is_parameter_expr() const {
  61. return (_type == T_parameter);
  62. }
  63. ////////////////////////////////////////////////////////////////////
  64. // Function: CPPSimpleType::get_preferred_name
  65. // Access: Public, Virtual
  66. // Description:
  67. ////////////////////////////////////////////////////////////////////
  68. string CPPSimpleType::
  69. get_preferred_name() const {
  70. // Simple types always prefer to use their native types.
  71. return get_local_name();
  72. }
  73. ////////////////////////////////////////////////////////////////////
  74. // Function: CPPSimpleType::output
  75. // Access: Public, Virtual
  76. // Description:
  77. ////////////////////////////////////////////////////////////////////
  78. void CPPSimpleType::
  79. output(ostream &out, int, CPPScope *, bool) const {
  80. if (_flags & F_unsigned) {
  81. out << "unsigned ";
  82. }
  83. if (_flags & F_signed) {
  84. out << "signed ";
  85. }
  86. if ((_type == T_int && (_flags & F_longlong) != 0) &&
  87. !cpp_longlong_keyword.empty()) {
  88. // It's a long long, and we have a specific long long type name.
  89. // This is to output code for compilers that don't recognize "long
  90. // long int".
  91. out << cpp_longlong_keyword;
  92. return;
  93. }
  94. if (_flags & F_longlong) {
  95. out << "long long ";
  96. } else if (_flags & F_long) {
  97. out << "long ";
  98. } else if (_flags & F_short) {
  99. out << "short ";
  100. }
  101. switch (_type) {
  102. case T_unknown:
  103. out << "unknown";
  104. break;
  105. case T_bool:
  106. out << "bool";
  107. break;
  108. case T_char:
  109. out << "char";
  110. break;
  111. case T_wchar_t:
  112. out << "wchar_t";
  113. break;
  114. case T_char16_t:
  115. out << "char16_t";
  116. break;
  117. case T_char32_t:
  118. out << "char32_t";
  119. break;
  120. case T_int:
  121. out << "int";
  122. break;
  123. case T_float:
  124. out << "float";
  125. break;
  126. case T_double:
  127. out << "double";
  128. break;
  129. case T_void:
  130. out << "void";
  131. break;
  132. case T_nullptr:
  133. out << "decltype(nullptr)";
  134. break;
  135. case T_parameter:
  136. out << "parameter";
  137. break;
  138. default:
  139. out << "***invalid type***";
  140. }
  141. }
  142. ////////////////////////////////////////////////////////////////////
  143. // Function: CPPSimpleType::get_subtype
  144. // Access: Public, Virtual
  145. // Description:
  146. ////////////////////////////////////////////////////////////////////
  147. CPPDeclaration::SubType CPPSimpleType::
  148. get_subtype() const {
  149. return ST_simple;
  150. }
  151. ////////////////////////////////////////////////////////////////////
  152. // Function: CPPSimpleType::as_simple_type
  153. // Access: Public, Virtual
  154. // Description:
  155. ////////////////////////////////////////////////////////////////////
  156. CPPSimpleType *CPPSimpleType::
  157. as_simple_type() {
  158. return this;
  159. }
  160. ////////////////////////////////////////////////////////////////////
  161. // Function: CPPSimpleType::is_equal
  162. // Access: Protected, Virtual
  163. // Description: Called by CPPDeclaration() to determine whether this type is
  164. // equivalent to another type of the same type.
  165. ////////////////////////////////////////////////////////////////////
  166. bool CPPSimpleType::
  167. is_equal(const CPPDeclaration *other) const {
  168. const CPPSimpleType *ot = ((CPPDeclaration *)other)->as_simple_type();
  169. assert(ot != NULL);
  170. return _type == ot->_type && _flags == ot->_flags;
  171. }
  172. ////////////////////////////////////////////////////////////////////
  173. // Function: CPPSimpleType::is_less
  174. // Access: Protected, Virtual
  175. // Description: Called by CPPDeclaration() to determine whether this type
  176. // should be ordered before another type of the same
  177. // type, in an arbitrary but fixed ordering.
  178. ////////////////////////////////////////////////////////////////////
  179. bool CPPSimpleType::
  180. is_less(const CPPDeclaration *other) const {
  181. const CPPSimpleType *ot = ((CPPDeclaration *)other)->as_simple_type();
  182. assert(ot != NULL);
  183. if (_type != ot->_type) {
  184. return _type < ot->_type;
  185. }
  186. return _flags < ot->_flags;
  187. }