cppReferenceType.cxx 7.5 KB

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