cppExtensionType.cxx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 cppExtensionType.cxx
  10. * @author drose
  11. * @date 1999-10-21
  12. */
  13. #include "cppExtensionType.h"
  14. #include "cppTypedefType.h"
  15. #include "cppIdentifier.h"
  16. #include "cppParser.h"
  17. #include <map>
  18. /**
  19. *
  20. */
  21. CPPExtensionType::
  22. CPPExtensionType(CPPExtensionType::Type type,
  23. CPPIdentifier *ident, CPPScope *current_scope,
  24. const CPPFile &file) :
  25. CPPType(file),
  26. _type(type), _ident(ident),
  27. _alignment(nullptr)
  28. {
  29. if (_ident != nullptr) {
  30. _ident->_native_scope = current_scope;
  31. }
  32. }
  33. /**
  34. *
  35. */
  36. std::string CPPExtensionType::
  37. get_simple_name() const {
  38. if (_ident == nullptr) {
  39. return "";
  40. }
  41. return _ident->get_simple_name();
  42. }
  43. /**
  44. *
  45. */
  46. std::string CPPExtensionType::
  47. get_local_name(CPPScope *scope) const {
  48. if (_ident == nullptr) {
  49. return "";
  50. }
  51. return _ident->get_local_name(scope);
  52. }
  53. /**
  54. *
  55. */
  56. std::string CPPExtensionType::
  57. get_fully_scoped_name() const {
  58. if (_ident == nullptr) {
  59. return "";
  60. }
  61. return _ident->get_fully_scoped_name();
  62. }
  63. /**
  64. * Returns true if the type has not yet been fully specified, false if it has.
  65. */
  66. bool CPPExtensionType::
  67. is_incomplete() const {
  68. return true;
  69. }
  70. /**
  71. * Returns true if the type, or any nested type within the type, is a
  72. * CPPTBDType and thus isn't fully determined right now. In this case,
  73. * calling resolve_type() may or may not resolve the type.
  74. */
  75. bool CPPExtensionType::
  76. is_tbd() const {
  77. if (_ident != nullptr) {
  78. return _ident->is_tbd();
  79. }
  80. return false;
  81. }
  82. /**
  83. * Returns true if the type is considered a standard layout type.
  84. */
  85. bool CPPExtensionType::
  86. is_standard_layout() const {
  87. return (_type == T_enum || _type == T_enum_class || _type == T_enum_struct);
  88. }
  89. /**
  90. * Returns true if the type is considered a Plain Old Data (POD) type.
  91. */
  92. bool CPPExtensionType::
  93. is_trivial() const {
  94. return (_type == T_enum || _type == T_enum_class || _type == T_enum_struct);
  95. }
  96. /**
  97. * Returns true if the type can be constructed using the given argument.
  98. */
  99. bool CPPExtensionType::
  100. is_constructible(const CPPType *given_type) const {
  101. if (_type == T_enum || _type == T_enum_class || _type == T_enum_struct) {
  102. const CPPExtensionType *other = ((CPPType *)given_type)->remove_reference()->remove_const()->as_extension_type();
  103. return other != nullptr && is_equal(other);
  104. }
  105. return false;
  106. }
  107. /**
  108. * Returns true if the type is default-constructible.
  109. */
  110. bool CPPExtensionType::
  111. is_default_constructible() const {
  112. return (_type == T_enum || _type == T_enum_class || _type == T_enum_struct);
  113. }
  114. /**
  115. * Returns true if the type is copy-constructible.
  116. */
  117. bool CPPExtensionType::
  118. is_copy_constructible() const {
  119. return (_type == T_enum || _type == T_enum_class || _type == T_enum_struct);
  120. }
  121. /**
  122. * Returns true if the type is copy-assignable.
  123. */
  124. bool CPPExtensionType::
  125. is_copy_assignable() const {
  126. return (_type == T_enum || _type == T_enum_class || _type == T_enum_struct);
  127. }
  128. /**
  129. *
  130. */
  131. CPPDeclaration *CPPExtensionType::
  132. substitute_decl(CPPDeclaration::SubstDecl &subst,
  133. CPPScope *current_scope, CPPScope *global_scope) {
  134. SubstDecl::const_iterator si = subst.find(this);
  135. if (si != subst.end()) {
  136. return (*si).second;
  137. }
  138. CPPExtensionType *rep = new CPPExtensionType(*this);
  139. if (_ident != nullptr) {
  140. rep->_ident =
  141. _ident->substitute_decl(subst, current_scope, global_scope);
  142. }
  143. if (rep->_ident == _ident) {
  144. delete rep;
  145. rep = this;
  146. }
  147. rep = CPPType::new_type(rep)->as_extension_type();
  148. subst.insert(SubstDecl::value_type(this, rep));
  149. return rep;
  150. }
  151. /**
  152. * If this CPPType object is a forward reference or other nonspecified
  153. * reference to a type that might now be known a real type, returns the real
  154. * type. Otherwise returns the type itself.
  155. */
  156. CPPType *CPPExtensionType::
  157. resolve_type(CPPScope *current_scope, CPPScope *global_scope) {
  158. if (_ident == nullptr) {
  159. // We can't resolve anonymous types. But that's OK, since they can't be
  160. // forward declared anyway.
  161. return this;
  162. }
  163. // Maybe it has been defined by now.
  164. CPPType *type = _ident->find_type(current_scope, global_scope);
  165. if (type != nullptr) {
  166. return type;
  167. }
  168. return this;
  169. }
  170. /**
  171. * This is a little more forgiving than is_equal(): it returns true if the
  172. * types appear to be referring to the same thing, even if they may have
  173. * different pointers or somewhat different definitions. It's useful for
  174. * parameter matching, etc.
  175. */
  176. bool CPPExtensionType::
  177. is_equivalent(const CPPType &other) const {
  178. const CPPExtensionType *ot = ((CPPType *)&other)->as_extension_type();
  179. if (ot == nullptr) {
  180. return CPPType::is_equivalent(other);
  181. }
  182. // We consider two different extension types to be equivalent if they have
  183. // the same name.
  184. return *_ident == *ot->_ident;
  185. }
  186. /**
  187. *
  188. */
  189. void CPPExtensionType::
  190. output(std::ostream &out, int, CPPScope *scope, bool complete) const {
  191. if (_ident != nullptr) {
  192. // If we have a name, use it.
  193. if (complete || cppparser_output_class_keyword) {
  194. out << _type << " ";
  195. }
  196. out << _ident->get_local_name(scope);
  197. } else if (!_typedefs.empty()) {
  198. // If we have a typedef name, use it.
  199. out << _typedefs.front()->get_local_name(scope);
  200. } else {
  201. out << "(**unknown forward-reference type**)";
  202. }
  203. }
  204. /**
  205. *
  206. */
  207. CPPDeclaration::SubType CPPExtensionType::
  208. get_subtype() const {
  209. return ST_extension;
  210. }
  211. /**
  212. *
  213. */
  214. CPPExtensionType *CPPExtensionType::
  215. as_extension_type() {
  216. return this;
  217. }
  218. std::ostream &
  219. operator << (std::ostream &out, CPPExtensionType::Type type) {
  220. switch (type) {
  221. case CPPExtensionType::T_enum:
  222. return out << "enum";
  223. case CPPExtensionType::T_class:
  224. return out << "class";
  225. case CPPExtensionType::T_struct:
  226. return out << "struct";
  227. case CPPExtensionType::T_union:
  228. return out << "union";
  229. case CPPExtensionType::T_enum_class:
  230. return out << "enum class";
  231. case CPPExtensionType::T_enum_struct:
  232. return out << "enum struct";
  233. default:
  234. return out << "***invalid extension type***";
  235. }
  236. }