cppReferenceType.cxx 7.0 KB

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