cppSimpleType.cxx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file cppSimpleType.cxx
  10. * @author drose
  11. * @date 1999-10-19
  12. */
  13. #include "cppSimpleType.h"
  14. #include "cppGlobals.h"
  15. /**
  16. *
  17. */
  18. CPPSimpleType::
  19. CPPSimpleType(CPPSimpleType::Type type, int flags) :
  20. CPPType(CPPFile()),
  21. _type(type), _flags(flags)
  22. {
  23. }
  24. /**
  25. * Returns true if the type, or any nested type within the type, is a
  26. * CPPTBDType and thus isn't fully determined right now. In this case,
  27. * calling resolve_type() may or may not resolve the type.
  28. */
  29. bool CPPSimpleType::
  30. is_tbd() const {
  31. return (_type == T_unknown);
  32. }
  33. /**
  34. * Returns true if the type is a boolean, floating point or integral type.
  35. */
  36. bool CPPSimpleType::
  37. is_arithmetic() const {
  38. return (_type > T_unknown && _type < T_void);
  39. }
  40. /**
  41. * Returns true if the type is considered a fundamental type.
  42. */
  43. bool CPPSimpleType::
  44. is_fundamental() const {
  45. return (_type != T_unknown && _type != T_parameter && _type != T_auto && _type != T_va_list);
  46. }
  47. /**
  48. * Returns true if the type is considered a standard layout type.
  49. */
  50. bool CPPSimpleType::
  51. is_standard_layout() const {
  52. return (_type != T_unknown && _type != T_parameter && _type != T_auto && _type != T_va_list);
  53. }
  54. /**
  55. * Returns true if the type is considered a Plain Old Data (POD) type.
  56. */
  57. bool CPPSimpleType::
  58. is_trivial() const {
  59. return (_type != T_unknown && _type != T_parameter && _type != T_auto && _type != T_va_list);
  60. }
  61. /**
  62. * Returns true if the type can be safely copied by memcpy or memmove.
  63. */
  64. bool CPPSimpleType::
  65. is_trivially_copyable() const {
  66. return (_type != T_unknown && _type != T_parameter && _type != T_auto && _type != T_va_list);
  67. }
  68. /**
  69. * Returns true if the type can be constructed using the given argument.
  70. */
  71. bool CPPSimpleType::
  72. is_constructible(const CPPType *given_type) const {
  73. given_type = ((CPPType *)given_type)->remove_reference()->remove_cv();
  74. const CPPSimpleType *simple_type = given_type->as_simple_type();
  75. if (simple_type == nullptr) {
  76. return given_type->is_enum() && is_arithmetic();
  77. } else if (_type == T_nullptr) {
  78. return simple_type->_type == T_nullptr;
  79. } else if (_type == T_bool) {
  80. return simple_type->is_arithmetic() || simple_type->_type == T_nullptr;
  81. } else if (is_arithmetic()) {
  82. return simple_type->is_arithmetic();
  83. } else {
  84. return false;
  85. }
  86. }
  87. /**
  88. * Returns true if the type is default-constructible.
  89. */
  90. bool CPPSimpleType::
  91. is_default_constructible() const {
  92. return (_type != T_void);
  93. }
  94. /**
  95. * Returns true if the type is copy-constructible.
  96. */
  97. bool CPPSimpleType::
  98. is_copy_constructible() const {
  99. return (_type != T_void);
  100. }
  101. /**
  102. * Returns true if the type is copy-assignable.
  103. */
  104. bool CPPSimpleType::
  105. is_copy_assignable() const {
  106. return (_type != T_void);
  107. }
  108. /**
  109. * Returns true if the type is destructible.
  110. */
  111. bool CPPSimpleType::
  112. is_destructible() const {
  113. return (_type != T_void);
  114. }
  115. /**
  116. * Returns true if the type is a special parameter expression type.
  117. *
  118. * This sort of type is created to handle instance declarations that initially
  119. * look like function prototypes.
  120. */
  121. bool CPPSimpleType::
  122. is_parameter_expr() const {
  123. return (_type == T_parameter);
  124. }
  125. /**
  126. *
  127. */
  128. std::string CPPSimpleType::
  129. get_preferred_name() const {
  130. // Simple types always prefer to use their native types.
  131. return get_local_name();
  132. }
  133. /**
  134. *
  135. */
  136. void CPPSimpleType::
  137. output(std::ostream &out, int, CPPScope *, bool) const {
  138. if (_flags & F_unsigned) {
  139. out << "unsigned ";
  140. }
  141. if (_flags & F_signed) {
  142. out << "signed ";
  143. }
  144. if ((_type == T_int && (_flags & F_longlong) != 0) &&
  145. !cpp_longlong_keyword.empty()) {
  146. // It's a long long, and we have a specific long long type name. This is
  147. // to output code for compilers that don't recognize "long long int".
  148. out << cpp_longlong_keyword;
  149. return;
  150. }
  151. if (_flags & F_longlong) {
  152. out << "long long ";
  153. } else if (_flags & F_long) {
  154. out << "long ";
  155. } else if (_flags & F_short) {
  156. out << "short ";
  157. }
  158. switch (_type) {
  159. case T_unknown:
  160. out << "unknown";
  161. break;
  162. case T_bool:
  163. out << "bool";
  164. break;
  165. case T_char:
  166. out << "char";
  167. break;
  168. case T_wchar_t:
  169. out << "wchar_t";
  170. break;
  171. case T_char8_t:
  172. out << "char8_t";
  173. break;
  174. case T_char16_t:
  175. out << "char16_t";
  176. break;
  177. case T_char32_t:
  178. out << "char32_t";
  179. break;
  180. case T_int:
  181. out << "int";
  182. break;
  183. case T_float:
  184. out << "float";
  185. break;
  186. case T_double:
  187. out << "double";
  188. break;
  189. case T_void:
  190. out << "void";
  191. break;
  192. case T_nullptr:
  193. out << "decltype(nullptr)";
  194. break;
  195. case T_parameter:
  196. out << "parameter";
  197. break;
  198. case T_auto:
  199. out << "auto";
  200. break;
  201. case T_va_list:
  202. out << "__builtin_va_list";
  203. break;
  204. default:
  205. out << "***invalid type***";
  206. }
  207. }
  208. /**
  209. *
  210. */
  211. CPPDeclaration::SubType CPPSimpleType::
  212. get_subtype() const {
  213. return ST_simple;
  214. }
  215. /**
  216. *
  217. */
  218. CPPSimpleType *CPPSimpleType::
  219. as_simple_type() {
  220. return this;
  221. }
  222. /**
  223. * Called by CPPDeclaration() to determine whether this type is equivalent to
  224. * another type of the same type.
  225. */
  226. bool CPPSimpleType::
  227. is_equal(const CPPDeclaration *other) const {
  228. const CPPSimpleType *ot = ((CPPDeclaration *)other)->as_simple_type();
  229. assert(ot != nullptr);
  230. return _type == ot->_type && _flags == ot->_flags;
  231. }
  232. /**
  233. * Called by CPPDeclaration() to determine whether this type should be ordered
  234. * before another type of the same type, in an arbitrary but fixed ordering.
  235. */
  236. bool CPPSimpleType::
  237. is_less(const CPPDeclaration *other) const {
  238. const CPPSimpleType *ot = ((CPPDeclaration *)other)->as_simple_type();
  239. assert(ot != nullptr);
  240. if (_type != ot->_type) {
  241. return _type < ot->_type;
  242. }
  243. return _flags < ot->_flags;
  244. }