cppPointerType.cxx 8.4 KB

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