cppExtensionType.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Filename: cppExtensionType.cxx
  2. // Created by: drose (21Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "cppExtensionType.h"
  19. #include "cppTypedef.h"
  20. #include "cppIdentifier.h"
  21. #include "cppParser.h"
  22. #include "indent.h"
  23. ////////////////////////////////////////////////////////////////////
  24. // Function: CPPExtensionType::Conextensionor
  25. // Access: Public
  26. // Description:
  27. ////////////////////////////////////////////////////////////////////
  28. CPPExtensionType::
  29. CPPExtensionType(CPPExtensionType::Type type,
  30. CPPIdentifier *ident, CPPScope *current_scope,
  31. const CPPFile &file) :
  32. CPPType(file),
  33. _type(type), _ident(ident)
  34. {
  35. if (_ident != NULL) {
  36. _ident->_native_scope = current_scope;
  37. }
  38. }
  39. ////////////////////////////////////////////////////////////////////
  40. // Function: CPPExtensionType::get_simple_name
  41. // Access: Public, Virtual
  42. // Description:
  43. ////////////////////////////////////////////////////////////////////
  44. string CPPExtensionType::
  45. get_simple_name() const {
  46. if (_ident == NULL) {
  47. return "";
  48. }
  49. return _ident->get_simple_name();
  50. }
  51. ////////////////////////////////////////////////////////////////////
  52. // Function: CPPExtensionType::get_local_name
  53. // Access: Public, Virtual
  54. // Description:
  55. ////////////////////////////////////////////////////////////////////
  56. string CPPExtensionType::
  57. get_local_name(CPPScope *scope) const {
  58. if (_ident == NULL) {
  59. return "";
  60. }
  61. return _ident->get_local_name(scope);
  62. }
  63. ////////////////////////////////////////////////////////////////////
  64. // Function: CPPExtensionType::get_fully_scoped_name
  65. // Access: Public, Virtual
  66. // Description:
  67. ////////////////////////////////////////////////////////////////////
  68. string CPPExtensionType::
  69. get_fully_scoped_name() const {
  70. if (_ident == NULL) {
  71. return "";
  72. }
  73. return _ident->get_fully_scoped_name();
  74. }
  75. ////////////////////////////////////////////////////////////////////
  76. // Function: CPPExtensionType::is_incomplete
  77. // Access: Public, Virtual
  78. // Description: Returns true if the type has not yet been fully
  79. // specified, false if it has.
  80. ////////////////////////////////////////////////////////////////////
  81. bool CPPExtensionType::
  82. is_incomplete() const {
  83. return true;
  84. }
  85. ////////////////////////////////////////////////////////////////////
  86. // Function: CPPExtensionType::is_tbd
  87. // Access: Public, Virtual
  88. // Description: Returns true if the type, or any nested type within
  89. // the type, is a CPPTBDType and thus isn't fully
  90. // determined right now. In this case, calling
  91. // resolve_type() may or may not resolve the type.
  92. ////////////////////////////////////////////////////////////////////
  93. bool CPPExtensionType::
  94. is_tbd() const {
  95. if (_ident != (CPPIdentifier *)NULL) {
  96. return _ident->is_tbd();
  97. }
  98. return false;
  99. }
  100. ////////////////////////////////////////////////////////////////////
  101. // Function: CPPExtensionType::substitute_decl
  102. // Access: Public, Virtual
  103. // Description:
  104. ////////////////////////////////////////////////////////////////////
  105. CPPDeclaration *CPPExtensionType::
  106. substitute_decl(CPPDeclaration::SubstDecl &subst,
  107. CPPScope *current_scope, CPPScope *global_scope) {
  108. SubstDecl::const_iterator si = subst.find(this);
  109. if (si != subst.end()) {
  110. return (*si).second;
  111. }
  112. CPPExtensionType *rep = new CPPExtensionType(*this);
  113. if (_ident != NULL) {
  114. rep->_ident =
  115. _ident->substitute_decl(subst, current_scope, global_scope);
  116. }
  117. if (rep->_ident == _ident) {
  118. delete rep;
  119. rep = this;
  120. }
  121. rep = CPPType::new_type(rep)->as_extension_type();
  122. subst.insert(SubstDecl::value_type(this, rep));
  123. return rep;
  124. }
  125. ////////////////////////////////////////////////////////////////////
  126. // Function: CPPExtensionType::is_equivalent_type
  127. // Access: Public, Virtual
  128. // Description: This is a little more forgiving than is_equal(): it
  129. // returns true if the types appear to be referring to
  130. // the same thing, even if they may have different
  131. // pointers or somewhat different definitions. It's
  132. // useful for parameter matching, etc.
  133. ////////////////////////////////////////////////////////////////////
  134. bool CPPExtensionType::
  135. is_equivalent(const CPPType &other) const {
  136. const CPPExtensionType *ot = ((CPPType *)&other)->as_extension_type();
  137. if (ot == (CPPExtensionType *)NULL) {
  138. return CPPType::is_equivalent(other);
  139. }
  140. // We consider two different extension types to be equivalent if
  141. // they have the same name.
  142. return *_ident == *ot->_ident;
  143. }
  144. ////////////////////////////////////////////////////////////////////
  145. // Function: CPPExtensionType::output
  146. // Access: Public, Virtual
  147. // Description:
  148. ////////////////////////////////////////////////////////////////////
  149. void CPPExtensionType::
  150. output(ostream &out, int, CPPScope *scope, bool) const {
  151. if (_ident != NULL) {
  152. // If we have a name, use it.
  153. if (cppparser_output_class_keyword) {
  154. out << _type << " ";
  155. }
  156. out << _ident->get_local_name(scope);
  157. } else if (!_typedefs.empty()) {
  158. // If we have a typedef name, use it.
  159. out << _typedefs.front()->get_local_name(scope);
  160. } else {
  161. out << "(**unknown forward-reference type**)";
  162. }
  163. }
  164. ////////////////////////////////////////////////////////////////////
  165. // Function: CPPExtensionType::get_subtype
  166. // Access: Public, Virtual
  167. // Description:
  168. ////////////////////////////////////////////////////////////////////
  169. CPPDeclaration::SubType CPPExtensionType::
  170. get_subtype() const {
  171. return ST_extension;
  172. }
  173. ////////////////////////////////////////////////////////////////////
  174. // Function: CPPExtensionType::as_extension_type
  175. // Access: Public, Virtual
  176. // Description:
  177. ////////////////////////////////////////////////////////////////////
  178. CPPExtensionType *CPPExtensionType::
  179. as_extension_type() {
  180. return this;
  181. }
  182. ostream &
  183. operator << (ostream &out, CPPExtensionType::Type type) {
  184. switch (type) {
  185. case CPPExtensionType::T_enum:
  186. return out << "enum";
  187. case CPPExtensionType::T_class:
  188. return out << "class";
  189. case CPPExtensionType::T_struct:
  190. return out << "struct";
  191. case CPPExtensionType::T_union:
  192. return out << "union";
  193. default:
  194. return out << "***invalid extension type***";
  195. }
  196. }