cppType.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Filename: cppType.cxx
  2. // Created by: drose (19Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "cppType.h"
  19. #include "cppTypedef.h"
  20. CPPType::Types CPPType::_types;
  21. CPPType::PreferredNames CPPType::_preferred_names;
  22. bool CPPTypeCompare::
  23. operator () (CPPType *a, CPPType *b) const {
  24. return (*a) < (*b);
  25. }
  26. ////////////////////////////////////////////////////////////////////
  27. // Function: CPPType::Constructor
  28. // Access: Public
  29. // Description:
  30. ////////////////////////////////////////////////////////////////////
  31. CPPType::
  32. CPPType(const CPPFile &file) :
  33. CPPDeclaration(file)
  34. {
  35. _declaration = (CPPTypeDeclaration *)NULL;
  36. }
  37. ////////////////////////////////////////////////////////////////////
  38. // Function: CPPType::resolve_type
  39. // Access: Public, Virtual
  40. // Description: If this CPPType object is a forward reference or
  41. // other nonspecified reference to a type that might now
  42. // be known a real type, returns the real type.
  43. // Otherwise returns the type itself.
  44. ////////////////////////////////////////////////////////////////////
  45. CPPType *CPPType::
  46. resolve_type(CPPScope *, CPPScope *) {
  47. return this;
  48. }
  49. ////////////////////////////////////////////////////////////////////
  50. // Function: CPPType::is_tbd
  51. // Access: Public, Virtual
  52. // Description: Returns true if the type, or any nested type within
  53. // the type, is a CPPTBDType and thus isn't fully
  54. // determined right now. In this case, calling
  55. // resolve_type() may or may not resolve the type.
  56. ////////////////////////////////////////////////////////////////////
  57. bool CPPType::
  58. is_tbd() const {
  59. return false;
  60. }
  61. ////////////////////////////////////////////////////////////////////
  62. // Function: CPPType::has_typedef_name
  63. // Access: Public
  64. // Description: Returns true if the type has even been typedef'ed and
  65. // therefore has a simple name available to stand for
  66. // it. Extension types are all implicitly typedef'ed on
  67. // declaration.
  68. ////////////////////////////////////////////////////////////////////
  69. bool CPPType::
  70. has_typedef_name() const {
  71. return !_typedefs.empty();
  72. }
  73. ////////////////////////////////////////////////////////////////////
  74. // Function: CPPType::get_typedef_name
  75. // Access: Public
  76. // Description: Returns a string that can be used to name the type,
  77. // if has_typedef_name() returned true. This will be
  78. // the first typedef name applied to the type.
  79. ////////////////////////////////////////////////////////////////////
  80. string CPPType::
  81. get_typedef_name(CPPScope *scope) const {
  82. if (_typedefs.empty()) {
  83. return string();
  84. } else {
  85. return _typedefs.front()->get_local_name(scope);
  86. }
  87. }
  88. ////////////////////////////////////////////////////////////////////
  89. // Function: CPPType::get_simple_name
  90. // Access: Public, Virtual
  91. // Description: Returns a fundametal one-word name for the type.
  92. // This name will not include any scoping operators or
  93. // template parameters, so it may not be a compilable
  94. // reference to the type.
  95. ////////////////////////////////////////////////////////////////////
  96. string CPPType::
  97. get_simple_name() const {
  98. return get_local_name();
  99. }
  100. ////////////////////////////////////////////////////////////////////
  101. // Function: CPPType::get_local_name
  102. // Access: Public, Virtual
  103. // Description: Returns the compilable, correct name for this type
  104. // within the indicated scope. If the scope is NULL,
  105. // within the scope the type is declared in.
  106. ////////////////////////////////////////////////////////////////////
  107. string CPPType::
  108. get_local_name(CPPScope *scope) const {
  109. ostringstream ostrm;
  110. output(ostrm, 0, scope, false);
  111. return ostrm.str();
  112. }
  113. ////////////////////////////////////////////////////////////////////
  114. // Function: CPPType::get_fully_scoped_name
  115. // Access: Public, Virtual
  116. // Description: Returns the compilable, correct name for the type,
  117. // with completely explicit scoping.
  118. ////////////////////////////////////////////////////////////////////
  119. string CPPType::
  120. get_fully_scoped_name() const {
  121. return get_local_name();
  122. }
  123. ////////////////////////////////////////////////////////////////////
  124. // Function: CPPType::get_preferred_name
  125. // Access: Public, Virtual
  126. // Description: Returns the best name to use for the type from a
  127. // programmer's point of view. This will typically be a
  128. // typedef name if one is available, or the full C++
  129. // name if it is not. The typedef may or may not be
  130. // visible within the current scope, so this type name
  131. // may not be compilable.
  132. ////////////////////////////////////////////////////////////////////
  133. string CPPType::
  134. get_preferred_name() const {
  135. string preferred_name = get_preferred_name_for(this);
  136. if (!preferred_name.empty()) {
  137. return preferred_name;
  138. }
  139. return get_local_name();
  140. }
  141. ////////////////////////////////////////////////////////////////////
  142. // Function: CPPType::is_incomplete
  143. // Access: Public, Virtual
  144. // Description: Returns true if the type has not yet been fully
  145. // specified, false if it has.
  146. ////////////////////////////////////////////////////////////////////
  147. bool CPPType::
  148. is_incomplete() const {
  149. return false;
  150. }
  151. ////////////////////////////////////////////////////////////////////
  152. // Function: CPPType::is_equivalent
  153. // Access: Public, Virtual
  154. // Description: This is a little more forgiving than is_equal(): it
  155. // returns true if the types appear to be referring to
  156. // the same thing, even if they may have different
  157. // pointers or somewhat different definitions. It's
  158. // useful for parameter matching, etc.
  159. ////////////////////////////////////////////////////////////////////
  160. bool CPPType::
  161. is_equivalent(const CPPType &other) const {
  162. if (get_subtype() != other.get_subtype()) {
  163. return false;
  164. }
  165. return is_equal(&other);
  166. }
  167. ////////////////////////////////////////////////////////////////////
  168. // Function: CPPType::output_instance
  169. // Access: Public, Virtual
  170. // Description: Formats a C++-looking line that defines an instance
  171. // of the given type, with the indicated name. In most
  172. // cases this will be "type name", but some types have
  173. // special exceptions.
  174. ////////////////////////////////////////////////////////////////////
  175. void CPPType::
  176. output_instance(ostream &out, const string &name, CPPScope *scope) const {
  177. output_instance(out, 0, scope, false, "", name);
  178. }
  179. ////////////////////////////////////////////////////////////////////
  180. // Function: CPPType::output_instance
  181. // Access: Public, Virtual
  182. // Description: Formats a C++-looking line that defines an instance
  183. // of the given type, with the indicated name. In most
  184. // cases this will be "type name", but some types have
  185. // special exceptions.
  186. ////////////////////////////////////////////////////////////////////
  187. void CPPType::
  188. output_instance(ostream &out, int indent_level, CPPScope *scope,
  189. bool complete, const string &prename,
  190. const string &name) const {
  191. output(out, indent_level, scope, complete);
  192. out << " " << prename << name;
  193. }
  194. ////////////////////////////////////////////////////////////////////
  195. // Function: CPPType::as_type
  196. // Access: Public, Virtual
  197. // Description:
  198. ////////////////////////////////////////////////////////////////////
  199. CPPType *CPPType::
  200. as_type() {
  201. return this;
  202. }
  203. ////////////////////////////////////////////////////////////////////
  204. // Function: CPPType::new_type
  205. // Access: Public, Static
  206. // Description: This should be called whenever a new CPPType object
  207. // is created. It will uniquify the type pointers by
  208. // checking to see if some equivalent CPPType object has
  209. // previously been created; if it has, it returns the
  210. // old object and deletes the new one. Otherwise, it
  211. // stores the new one and returns it.
  212. ////////////////////////////////////////////////////////////////////
  213. CPPType *CPPType::
  214. new_type(CPPType *type) {
  215. pair<Types::iterator, bool> result = _types.insert(type);
  216. if (result.second) {
  217. // The insertion has taken place; thus, this is the first time
  218. // this type has been declared.
  219. assert(*result.first == type);
  220. return type;
  221. }
  222. // The insertion has not taken place; thus, there was previously
  223. // another equivalent type declared.
  224. if (*result.first != type) {
  225. // *** Something wrong here. Deleting this should always be safe;
  226. // however, it's not. Thus, someone failed to call new_type() on
  227. // a type pointer before saving it somewhere. Fix me soon. ****
  228. //delete type;
  229. }
  230. return *result.first;
  231. }
  232. ////////////////////////////////////////////////////////////////////
  233. // Function: CPPType::record_preferred_name_for
  234. // Access: Public, Static
  235. // Description: Records a global typedef name associated with the
  236. // indicated Type. This will be taken as the
  237. // "preferred" name for this class, should anyone ask.
  238. ////////////////////////////////////////////////////////////////////
  239. void CPPType::
  240. record_preferred_name_for(const CPPType *type, const string &name) {
  241. if (!name.empty()) {
  242. string tname = type->get_fully_scoped_name();
  243. if (!tname.empty()) {
  244. _preferred_names.insert(PreferredNames::value_type(tname, name));
  245. }
  246. }
  247. }
  248. ////////////////////////////////////////////////////////////////////
  249. // Function: CPPType::get_preferred_name_for
  250. // Access: Public, Static
  251. // Description: Returns the previously-stored "preferred" name
  252. // associated with the type, if any, or empty string if
  253. // no name is associated.
  254. ////////////////////////////////////////////////////////////////////
  255. string CPPType::
  256. get_preferred_name_for(const CPPType *type) {
  257. // We do a lookup based on the type's name, instead of its pointer,
  258. // so we can resolve different expansions of the same type.
  259. string tname = type->get_fully_scoped_name();
  260. if (!tname.empty()) {
  261. PreferredNames::const_iterator pi;
  262. pi = _preferred_names.find(tname);
  263. if (pi != _preferred_names.end()) {
  264. return (*pi).second;
  265. }
  266. }
  267. return string();
  268. }