cppReferenceType.cxx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Filename: cppReferenceType.cxx
  2. // Created by: drose (19Oct99)
  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 "cppReferenceType.h"
  19. ////////////////////////////////////////////////////////////////////
  20. // Function: CPPReferenceType::Constructor
  21. // Access: Public
  22. // Description:
  23. ////////////////////////////////////////////////////////////////////
  24. CPPReferenceType::
  25. CPPReferenceType(CPPType *pointing_at) :
  26. CPPType(CPPFile()),
  27. _pointing_at(pointing_at)
  28. {
  29. }
  30. ////////////////////////////////////////////////////////////////////
  31. // Function: CPPReferenceType::is_fully_specified
  32. // Access: Public, Virtual
  33. // Description: Returns true if this declaration is an actual,
  34. // factual declaration, or false if some part of the
  35. // declaration depends on a template parameter which has
  36. // not yet been instantiated.
  37. ////////////////////////////////////////////////////////////////////
  38. bool CPPReferenceType::
  39. is_fully_specified() const {
  40. return CPPType::is_fully_specified() &&
  41. _pointing_at->is_fully_specified();
  42. }
  43. ////////////////////////////////////////////////////////////////////
  44. // Function: CPPReferenceType::substitute_decl
  45. // Access: Public, Virtual
  46. // Description:
  47. ////////////////////////////////////////////////////////////////////
  48. CPPDeclaration *CPPReferenceType::
  49. substitute_decl(CPPDeclaration::SubstDecl &subst,
  50. CPPScope *current_scope, CPPScope *global_scope) {
  51. SubstDecl::const_iterator si = subst.find(this);
  52. if (si != subst.end()) {
  53. return (*si).second;
  54. }
  55. CPPReferenceType *rep = new CPPReferenceType(*this);
  56. rep->_pointing_at =
  57. _pointing_at->substitute_decl(subst, current_scope, global_scope)
  58. ->as_type();
  59. if (rep->_pointing_at == _pointing_at) {
  60. delete rep;
  61. rep = this;
  62. }
  63. rep = CPPType::new_type(rep)->as_reference_type();
  64. subst.insert(SubstDecl::value_type(this, rep));
  65. return rep;
  66. }
  67. ////////////////////////////////////////////////////////////////////
  68. // Function: CPPReferenceType::resolve_type
  69. // Access: Public, Virtual
  70. // Description: If this CPPType object is a forward reference or
  71. // other nonspecified reference to a type that might now
  72. // be known a real type, returns the real type.
  73. // Otherwise returns the type itself.
  74. ////////////////////////////////////////////////////////////////////
  75. CPPType *CPPReferenceType::
  76. resolve_type(CPPScope *current_scope, CPPScope *global_scope) {
  77. CPPType *ptype = _pointing_at->resolve_type(current_scope, global_scope);
  78. if (ptype != _pointing_at) {
  79. CPPReferenceType *rep = new CPPReferenceType(*this);
  80. rep->_pointing_at = ptype;
  81. return CPPType::new_type(rep);
  82. }
  83. return this;
  84. }
  85. ////////////////////////////////////////////////////////////////////
  86. // Function: CPPReferenceType::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 CPPReferenceType::
  94. is_tbd() const {
  95. return _pointing_at->is_tbd();
  96. }
  97. ////////////////////////////////////////////////////////////////////
  98. // Function: CPPReferenceType::is_equivalent
  99. // Access: Public, Virtual
  100. // Description: This is a little more forgiving than is_equal(): it
  101. // returns true if the types appear to be referring to
  102. // the same thing, even if they may have different
  103. // pointers or somewhat different definitions. It's
  104. // useful for parameter matching, etc.
  105. ////////////////////////////////////////////////////////////////////
  106. bool CPPReferenceType::
  107. is_equivalent(const CPPType &other) const {
  108. const CPPReferenceType *ot = ((CPPType *)&other)->as_reference_type();
  109. if (ot == (CPPReferenceType *)NULL) {
  110. return CPPType::is_equivalent(other);
  111. }
  112. return _pointing_at->is_equivalent(*ot->_pointing_at);
  113. }
  114. ////////////////////////////////////////////////////////////////////
  115. // Function: CPPReferenceType::output
  116. // Access: Public, Virtual
  117. // Description:
  118. ////////////////////////////////////////////////////////////////////
  119. void CPPReferenceType::
  120. output(ostream &out, int indent_level, CPPScope *scope, bool complete) const {
  121. /*
  122. _pointing_at->output(out, indent_level, scope, complete);
  123. out << " &";
  124. */
  125. output_instance(out, indent_level, scope, complete, "", "");
  126. }
  127. ////////////////////////////////////////////////////////////////////
  128. // Function: CPPReferenceType::output_instance
  129. // Access: Public, Virtual
  130. // Description: Formats a C++-looking line that defines an instance
  131. // of the given type, with the indicated name. In most
  132. // cases this will be "type name", but some types have
  133. // special exceptions.
  134. ////////////////////////////////////////////////////////////////////
  135. void CPPReferenceType::
  136. output_instance(ostream &out, int indent_level, CPPScope *scope,
  137. bool complete, const string &prename,
  138. const string &name) const {
  139. _pointing_at->output_instance(out, indent_level, scope, complete,
  140. "&" + prename, name);
  141. }
  142. ////////////////////////////////////////////////////////////////////
  143. // Function: CPPReferenceType::get_subtype
  144. // Access: Public, Virtual
  145. // Description:
  146. ////////////////////////////////////////////////////////////////////
  147. CPPDeclaration::SubType CPPReferenceType::
  148. get_subtype() const {
  149. return ST_reference;
  150. }
  151. ////////////////////////////////////////////////////////////////////
  152. // Function: CPPReferenceType::as_reference_type
  153. // Access: Public, Virtual
  154. // Description:
  155. ////////////////////////////////////////////////////////////////////
  156. CPPReferenceType *CPPReferenceType::
  157. as_reference_type() {
  158. return this;
  159. }
  160. ////////////////////////////////////////////////////////////////////
  161. // Function: CPPReferenceType::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 CPPReferenceType::
  167. is_equal(const CPPDeclaration *other) const {
  168. const CPPReferenceType *ot = ((CPPDeclaration *)other)->as_reference_type();
  169. assert(ot != NULL);
  170. return _pointing_at == ot->_pointing_at;
  171. }
  172. ////////////////////////////////////////////////////////////////////
  173. // Function: CPPReferenceType::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 CPPReferenceType::
  180. is_less(const CPPDeclaration *other) const {
  181. const CPPReferenceType *ot = ((CPPDeclaration *)other)->as_reference_type();
  182. assert(ot != NULL);
  183. return _pointing_at < ot->_pointing_at;
  184. }