cppPointerType.cxx 7.6 KB

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