cppType.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Filename: cppType.cxx
  2. // Created by: drose (19Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, 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://etc.cmu.edu/panda3d/docs/license/ .
  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::is_parameter_expr
  63. // Access: Public, Virtual
  64. // Description: Returns true if the type is a special parameter
  65. // expression type.
  66. //
  67. // This sort of type is created to handle instance
  68. // declarations that initially look like function
  69. // prototypes.
  70. ////////////////////////////////////////////////////////////////////
  71. bool CPPType::
  72. is_parameter_expr() const {
  73. return false;
  74. }
  75. ////////////////////////////////////////////////////////////////////
  76. // Function: CPPType::has_typedef_name
  77. // Access: Public
  78. // Description: Returns true if the type has even been typedef'ed and
  79. // therefore has a simple name available to stand for
  80. // it. Extension types are all implicitly typedef'ed on
  81. // declaration.
  82. ////////////////////////////////////////////////////////////////////
  83. bool CPPType::
  84. has_typedef_name() const {
  85. return !_typedefs.empty();
  86. }
  87. ////////////////////////////////////////////////////////////////////
  88. // Function: CPPType::get_typedef_name
  89. // Access: Public
  90. // Description: Returns a string that can be used to name the type,
  91. // if has_typedef_name() returned true. This will be
  92. // the first typedef name applied to the type.
  93. ////////////////////////////////////////////////////////////////////
  94. string CPPType::
  95. get_typedef_name(CPPScope *scope) const {
  96. if (_typedefs.empty()) {
  97. return string();
  98. } else {
  99. return _typedefs.front()->get_local_name(scope);
  100. }
  101. }
  102. ////////////////////////////////////////////////////////////////////
  103. // Function: CPPType::get_simple_name
  104. // Access: Public, Virtual
  105. // Description: Returns a fundametal one-word name for the type.
  106. // This name will not include any scoping operators or
  107. // template parameters, so it may not be a compilable
  108. // reference to the type.
  109. ////////////////////////////////////////////////////////////////////
  110. string CPPType::
  111. get_simple_name() const {
  112. return get_local_name();
  113. }
  114. ////////////////////////////////////////////////////////////////////
  115. // Function: CPPType::get_local_name
  116. // Access: Public, Virtual
  117. // Description: Returns the compilable, correct name for this type
  118. // within the indicated scope. If the scope is NULL,
  119. // within the scope the type is declared in.
  120. ////////////////////////////////////////////////////////////////////
  121. string CPPType::
  122. get_local_name(CPPScope *scope) const {
  123. ostringstream ostrm;
  124. output(ostrm, 0, scope, false);
  125. return ostrm.str();
  126. }
  127. ////////////////////////////////////////////////////////////////////
  128. // Function: CPPType::get_fully_scoped_name
  129. // Access: Public, Virtual
  130. // Description: Returns the compilable, correct name for the type,
  131. // with completely explicit scoping.
  132. ////////////////////////////////////////////////////////////////////
  133. string CPPType::
  134. get_fully_scoped_name() const {
  135. return get_local_name();
  136. }
  137. ////////////////////////////////////////////////////////////////////
  138. // Function: CPPType::get_preferred_name
  139. // Access: Public, Virtual
  140. // Description: Returns the best name to use for the type from a
  141. // programmer's point of view. This will typically be a
  142. // typedef name if one is available, or the full C++
  143. // name if it is not. The typedef may or may not be
  144. // visible within the current scope, so this type name
  145. // may not be compilable.
  146. ////////////////////////////////////////////////////////////////////
  147. string CPPType::
  148. get_preferred_name() const {
  149. string preferred_name = get_preferred_name_for(this);
  150. if (!preferred_name.empty()) {
  151. return preferred_name;
  152. }
  153. return get_local_name();
  154. }
  155. ////////////////////////////////////////////////////////////////////
  156. // Function: CPPType::is_incomplete
  157. // Access: Public, Virtual
  158. // Description: Returns true if the type has not yet been fully
  159. // specified, false if it has.
  160. ////////////////////////////////////////////////////////////////////
  161. bool CPPType::
  162. is_incomplete() const {
  163. return false;
  164. }
  165. ////////////////////////////////////////////////////////////////////
  166. // Function: CPPType::is_equivalent
  167. // Access: Public, Virtual
  168. // Description: This is a little more forgiving than is_equal(): it
  169. // returns true if the types appear to be referring to
  170. // the same thing, even if they may have different
  171. // pointers or somewhat different definitions. It's
  172. // useful for parameter matching, etc.
  173. ////////////////////////////////////////////////////////////////////
  174. bool CPPType::
  175. is_equivalent(const CPPType &other) const {
  176. if (get_subtype() != other.get_subtype()) {
  177. return false;
  178. }
  179. return is_equal(&other);
  180. }
  181. ////////////////////////////////////////////////////////////////////
  182. // Function: CPPType::output_instance
  183. // Access: Public, Virtual
  184. // Description: Formats a C++-looking line that defines an instance
  185. // of the given type, with the indicated name. In most
  186. // cases this will be "type name", but some types have
  187. // special exceptions.
  188. ////////////////////////////////////////////////////////////////////
  189. void CPPType::
  190. output_instance(ostream &out, const string &name, CPPScope *scope) const {
  191. output_instance(out, 0, scope, false, "", name);
  192. }
  193. ////////////////////////////////////////////////////////////////////
  194. // Function: CPPType::output_instance
  195. // Access: Public, Virtual
  196. // Description: Formats a C++-looking line that defines an instance
  197. // of the given type, with the indicated name. In most
  198. // cases this will be "type name", but some types have
  199. // special exceptions.
  200. ////////////////////////////////////////////////////////////////////
  201. void CPPType::
  202. output_instance(ostream &out, int indent_level, CPPScope *scope,
  203. bool complete, const string &prename,
  204. const string &name) const {
  205. output(out, indent_level, scope, complete);
  206. out << " " << prename << name;
  207. }
  208. ////////////////////////////////////////////////////////////////////
  209. // Function: CPPType::as_type
  210. // Access: Public, Virtual
  211. // Description:
  212. ////////////////////////////////////////////////////////////////////
  213. CPPType *CPPType::
  214. as_type() {
  215. return this;
  216. }
  217. ////////////////////////////////////////////////////////////////////
  218. // Function: CPPType::new_type
  219. // Access: Public, Static
  220. // Description: This should be called whenever a new CPPType object
  221. // is created. It will uniquify the type pointers by
  222. // checking to see if some equivalent CPPType object has
  223. // previously been created; if it has, it returns the
  224. // old object and deletes the new one. Otherwise, it
  225. // stores the new one and returns it.
  226. ////////////////////////////////////////////////////////////////////
  227. CPPType *CPPType::
  228. new_type(CPPType *type) {
  229. pair<Types::iterator, bool> result = _types.insert(type);
  230. if (result.second) {
  231. // The insertion has taken place; thus, this is the first time
  232. // this type has been declared.
  233. assert(*result.first == type);
  234. return type;
  235. }
  236. // The insertion has not taken place; thus, there was previously
  237. // another equivalent type declared.
  238. if (*result.first != type) {
  239. // *** Something wrong here. Deleting this should always be safe;
  240. // however, it's not. Thus, someone failed to call new_type() on
  241. // a type pointer before saving it somewhere. Fix me soon. ****
  242. //delete type;
  243. }
  244. return *result.first;
  245. }
  246. ////////////////////////////////////////////////////////////////////
  247. // Function: CPPType::record_preferred_name_for
  248. // Access: Public, Static
  249. // Description: Records a global typedef name associated with the
  250. // indicated Type. This will be taken as the
  251. // "preferred" name for this class, should anyone ask.
  252. ////////////////////////////////////////////////////////////////////
  253. void CPPType::
  254. record_preferred_name_for(const CPPType *type, const string &name) {
  255. if (!name.empty()) {
  256. string tname = type->get_fully_scoped_name();
  257. if (!tname.empty()) {
  258. _preferred_names.insert(PreferredNames::value_type(tname, name));
  259. }
  260. }
  261. }
  262. ////////////////////////////////////////////////////////////////////
  263. // Function: CPPType::get_preferred_name_for
  264. // Access: Public, Static
  265. // Description: Returns the previously-stored "preferred" name
  266. // associated with the type, if any, or empty string if
  267. // no name is associated.
  268. ////////////////////////////////////////////////////////////////////
  269. string CPPType::
  270. get_preferred_name_for(const CPPType *type) {
  271. // We do a lookup based on the type's name, instead of its pointer,
  272. // so we can resolve different expansions of the same type.
  273. string tname = type->get_fully_scoped_name();
  274. if (!tname.empty()) {
  275. PreferredNames::const_iterator pi;
  276. pi = _preferred_names.find(tname);
  277. if (pi != _preferred_names.end()) {
  278. return (*pi).second;
  279. }
  280. }
  281. return string();
  282. }