cppPointerType.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_trivial
  97. // Access: Public, Virtual
  98. // Description: Returns true if the type is considered a Plain Old
  99. // Data (POD) type.
  100. ////////////////////////////////////////////////////////////////////
  101. bool CPPPointerType::
  102. is_trivial() const {
  103. return true;
  104. }
  105. ////////////////////////////////////////////////////////////////////
  106. // Function: CPPPointerType::is_equivalent
  107. // Access: Public, Virtual
  108. // Description: This is a little more forgiving than is_equal(): it
  109. // returns true if the types appear to be referring to
  110. // the same thing, even if they may have different
  111. // pointers or somewhat different definitions. It's
  112. // useful for parameter matching, etc.
  113. ////////////////////////////////////////////////////////////////////
  114. bool CPPPointerType::
  115. is_equivalent(const CPPType &other) const {
  116. const CPPPointerType *ot = ((CPPType *)&other)->as_pointer_type();
  117. if (ot == (CPPPointerType *)NULL) {
  118. return CPPType::is_equivalent(other);
  119. }
  120. return _pointing_at->is_equivalent(*ot->_pointing_at);
  121. }
  122. ////////////////////////////////////////////////////////////////////
  123. // Function: CPPPointerType::output
  124. // Access: Public, Virtual
  125. // Description:
  126. ////////////////////////////////////////////////////////////////////
  127. void CPPPointerType::
  128. output(ostream &out, int indent_level, CPPScope *scope, bool complete) const {
  129. /*
  130. CPPFunctionType *ftype = _pointing_at->as_function_type();
  131. if (ftype != (CPPFunctionType *)NULL) {
  132. // Pointers to functions are a bit of a special case; we have to
  133. // be a little more careful about where the '*' goes.
  134. string star = "*";
  135. if ((ftype->_flags & CPPFunctionType::F_method_pointer) != 0) {
  136. // We have to output pointers-to-method with a scoping before the
  137. // '*'.
  138. star = ftype->_class_owner->get_fully_scoped_name() + "::*";
  139. }
  140. _pointing_at->output_instance(out, indent_level, scope, complete,
  141. star, "");
  142. } else {
  143. _pointing_at->output(out, indent_level, scope, complete);
  144. out << " *";
  145. }
  146. */
  147. output_instance(out, indent_level, scope, complete, "", "");
  148. }
  149. ////////////////////////////////////////////////////////////////////
  150. // Function: CPPPointerType::output_instance
  151. // Access: Public, Virtual
  152. // Description: Formats a C++-looking line that defines an instance
  153. // of the given type, with the indicated name. In most
  154. // cases this will be "type name", but some types have
  155. // special exceptions.
  156. ////////////////////////////////////////////////////////////////////
  157. void CPPPointerType::
  158. output_instance(ostream &out, int indent_level, CPPScope *scope,
  159. bool complete, const string &prename,
  160. const string &name) const {
  161. string star = "*";
  162. CPPFunctionType *ftype = _pointing_at->as_function_type();
  163. if (ftype != NULL &&
  164. ((ftype->_flags & CPPFunctionType::F_method_pointer) != 0)) {
  165. // We have to output pointers-to-method with a scoping before the
  166. // '*'.
  167. star = ftype->_class_owner->get_fully_scoped_name() + "::*";
  168. }
  169. _pointing_at->output_instance(out, indent_level, scope, complete,
  170. star + prename, name);
  171. }
  172. ////////////////////////////////////////////////////////////////////
  173. // Function: CPPPointerType::get_subtype
  174. // Access: Public, Virtual
  175. // Description:
  176. ////////////////////////////////////////////////////////////////////
  177. CPPDeclaration::SubType CPPPointerType::
  178. get_subtype() const {
  179. return ST_pointer;
  180. }
  181. ////////////////////////////////////////////////////////////////////
  182. // Function: CPPPointerType::as_pointer_type
  183. // Access: Public, Virtual
  184. // Description:
  185. ////////////////////////////////////////////////////////////////////
  186. CPPPointerType *CPPPointerType::
  187. as_pointer_type() {
  188. return this;
  189. }
  190. ////////////////////////////////////////////////////////////////////
  191. // Function: CPPPointerType::is_equal
  192. // Access: Protected, Virtual
  193. // Description: Called by CPPDeclaration() to determine whether this type is
  194. // equivalent to another type of the same type.
  195. ////////////////////////////////////////////////////////////////////
  196. bool CPPPointerType::
  197. is_equal(const CPPDeclaration *other) const {
  198. const CPPPointerType *ot = ((CPPDeclaration *)other)->as_pointer_type();
  199. assert(ot != NULL);
  200. return _pointing_at == ot->_pointing_at;
  201. }
  202. ////////////////////////////////////////////////////////////////////
  203. // Function: CPPPointerType::is_less
  204. // Access: Protected, Virtual
  205. // Description: Called by CPPDeclaration() to determine whether this type
  206. // should be ordered before another type of the same
  207. // type, in an arbitrary but fixed ordering.
  208. ////////////////////////////////////////////////////////////////////
  209. bool CPPPointerType::
  210. is_less(const CPPDeclaration *other) const {
  211. const CPPPointerType *ot = ((CPPDeclaration *)other)->as_pointer_type();
  212. assert(ot != NULL);
  213. return _pointing_at < ot->_pointing_at;
  214. }