cppFunctionType.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // Filename: cppFunctionType.cxx
  2. // Created by: drose (21Oct99)
  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 "cppFunctionType.h"
  15. #include "cppParameterList.h"
  16. #include "cppSimpleType.h"
  17. #include "cppInstance.h"
  18. ////////////////////////////////////////////////////////////////////
  19. // Function: CPPFunctionType::Constructor
  20. // Access: Public
  21. // Description:
  22. ////////////////////////////////////////////////////////////////////
  23. CPPFunctionType::
  24. CPPFunctionType(CPPType *return_type, CPPParameterList *parameters,
  25. int flags) :
  26. CPPType(CPPFile()),
  27. _return_type(return_type),
  28. _parameters(parameters),
  29. _flags(flags)
  30. {
  31. _class_owner = NULL;
  32. // If the parameter list contains just the token "void", it means no
  33. // parameters.
  34. if (_parameters->_parameters.size() == 1 &&
  35. _parameters->_parameters.front()->_type->as_simple_type() != NULL &&
  36. _parameters->_parameters.front()->_type->as_simple_type()->_type ==
  37. CPPSimpleType::T_void &&
  38. _parameters->_parameters.front()->_ident == NULL) {
  39. _parameters->_parameters.clear();
  40. }
  41. }
  42. ////////////////////////////////////////////////////////////////////
  43. // Function: CPPFunctionType::Copy Constructor
  44. // Access: Public
  45. // Description:
  46. ////////////////////////////////////////////////////////////////////
  47. CPPFunctionType::
  48. CPPFunctionType(const CPPFunctionType &copy) :
  49. CPPType(copy),
  50. _return_type(copy._return_type),
  51. _parameters(copy._parameters),
  52. _flags(copy._flags),
  53. _class_owner(copy._class_owner)
  54. {
  55. }
  56. ////////////////////////////////////////////////////////////////////
  57. // Function: CPPFunctionType::Copy Assignment Operator
  58. // Access: Public
  59. // Description:
  60. ////////////////////////////////////////////////////////////////////
  61. void CPPFunctionType::
  62. operator = (const CPPFunctionType &copy) {
  63. CPPType::operator = (copy);
  64. _return_type = copy._return_type;
  65. _parameters = copy._parameters;
  66. _flags = copy._flags;
  67. _class_owner = copy._class_owner;
  68. }
  69. ////////////////////////////////////////////////////////////////////
  70. // Function: CPPFunctionType::is_fully_specified
  71. // Access: Public, Virtual
  72. // Description: Returns true if this declaration is an actual,
  73. // factual declaration, or false if some part of the
  74. // declaration depends on a template parameter which has
  75. // not yet been instantiated.
  76. ////////////////////////////////////////////////////////////////////
  77. bool CPPFunctionType::
  78. is_fully_specified() const {
  79. return CPPType::is_fully_specified() &&
  80. _return_type->is_fully_specified() &&
  81. _parameters->is_fully_specified();
  82. }
  83. ////////////////////////////////////////////////////////////////////
  84. // Function: CPPFunctionType::substitute_decl
  85. // Access: Public, Virtual
  86. // Description:
  87. ////////////////////////////////////////////////////////////////////
  88. CPPDeclaration *CPPFunctionType::
  89. substitute_decl(CPPDeclaration::SubstDecl &subst,
  90. CPPScope *current_scope, CPPScope *global_scope) {
  91. SubstDecl::const_iterator si = subst.find(this);
  92. if (si != subst.end()) {
  93. return (*si).second;
  94. }
  95. CPPFunctionType *rep = new CPPFunctionType(*this);
  96. rep->_return_type =
  97. _return_type->substitute_decl(subst, current_scope, global_scope)
  98. ->as_type();
  99. rep->_parameters =
  100. _parameters->substitute_decl(subst, current_scope, global_scope);
  101. if (rep->_return_type == _return_type &&
  102. rep->_parameters == _parameters) {
  103. delete rep;
  104. rep = this;
  105. }
  106. rep = CPPType::new_type(rep)->as_function_type();
  107. subst.insert(SubstDecl::value_type(this, rep));
  108. return rep;
  109. }
  110. ////////////////////////////////////////////////////////////////////
  111. // Function: CPPFunctionType::resolve_type
  112. // Access: Public, Virtual
  113. // Description: If this CPPType object is a forward reference or
  114. // other nonspecified reference to a type that might now
  115. // be known a real type, returns the real type.
  116. // Otherwise returns the type itself.
  117. ////////////////////////////////////////////////////////////////////
  118. CPPType *CPPFunctionType::
  119. resolve_type(CPPScope *current_scope, CPPScope *global_scope) {
  120. CPPType *rtype = _return_type->resolve_type(current_scope, global_scope);
  121. CPPParameterList *params =
  122. _parameters->resolve_type(current_scope, global_scope);
  123. if (rtype != _return_type || params != _parameters) {
  124. CPPFunctionType *rep = new CPPFunctionType(*this);
  125. rep->_return_type = rtype;
  126. rep->_parameters = params;
  127. return CPPType::new_type(rep);
  128. }
  129. return this;
  130. }
  131. ////////////////////////////////////////////////////////////////////
  132. // Function: CPPFunctionType::is_tbd
  133. // Access: Public, Virtual
  134. // Description: Returns true if the type, or any nested type within
  135. // the type, is a CPPTBDType and thus isn't fully
  136. // determined right now. In this case, calling
  137. // resolve_type() may or may not resolve the type.
  138. ////////////////////////////////////////////////////////////////////
  139. bool CPPFunctionType::
  140. is_tbd() const {
  141. if (_return_type->is_tbd()) {
  142. return true;
  143. }
  144. return _parameters->is_tbd();
  145. }
  146. ////////////////////////////////////////////////////////////////////
  147. // Function: CPPFunctionType::output
  148. // Access: Public, Virtual
  149. // Description:
  150. ////////////////////////////////////////////////////////////////////
  151. void CPPFunctionType::
  152. output(ostream &out, int indent_level, CPPScope *scope, bool complete) const {
  153. output(out, indent_level, scope, complete, -1);
  154. }
  155. ////////////////////////////////////////////////////////////////////
  156. // Function: CPPFunctionType::output
  157. // Access: Public
  158. // Description: The additional parameter allows us to specify the
  159. // number of parameters we wish to show the default
  160. // values for. If num_default_parameters is >= 0, it
  161. // indicates the number of default parameter values to
  162. // show on output. Otherwise, all parameter values are
  163. // shown.
  164. ////////////////////////////////////////////////////////////////////
  165. void CPPFunctionType::
  166. output(ostream &out, int indent_level, CPPScope *scope, bool complete,
  167. int num_default_parameters) const {
  168. _return_type->output(out, indent_level, scope, complete);
  169. out << "(";
  170. _parameters->output(out, scope, true, num_default_parameters);
  171. out << ")";
  172. if (_flags & F_const_method) {
  173. out << " const";
  174. }
  175. }
  176. ////////////////////////////////////////////////////////////////////
  177. // Function: CPPFunctionType::output_instance
  178. // Access: Public, Virtual
  179. // Description: Formats a C++-looking line that defines an instance
  180. // of the given type, with the indicated name. In most
  181. // cases this will be "type name", but some types have
  182. // special exceptions.
  183. ////////////////////////////////////////////////////////////////////
  184. void CPPFunctionType::
  185. output_instance(ostream &out, int indent_level, CPPScope *scope,
  186. bool complete, const string &prename,
  187. const string &name) const {
  188. output_instance(out, indent_level, scope, complete, prename, name, -1);
  189. }
  190. ////////////////////////////////////////////////////////////////////
  191. // Function: CPPFunctionType::output_instance
  192. // Access: Public
  193. // Description: The additional parameter allows us to specify the
  194. // number of parameters we wish to show the default
  195. // values for. If num_default_parameters is >= 0, it
  196. // indicates the number of default parameter values to
  197. // show on output. Otherwise, all parameter values are
  198. // shown.
  199. ////////////////////////////////////////////////////////////////////
  200. void CPPFunctionType::
  201. output_instance(ostream &out, int indent_level, CPPScope *scope,
  202. bool complete, const string &prename,
  203. const string &name, int num_default_parameters) const {
  204. ostringstream parm_string;
  205. parm_string << "(";
  206. _parameters->output(parm_string, scope, true, num_default_parameters);
  207. parm_string << ")";
  208. string str = parm_string.str();
  209. if (_flags & (F_constructor | F_destructor)) {
  210. // No return type for constructors and destructors.
  211. out << prename << name << str;
  212. } else {
  213. if (prename.empty()) {
  214. _return_type->output_instance(out, indent_level, scope, complete,
  215. "", prename + name + str);
  216. } else {
  217. _return_type->output_instance(out, indent_level, scope, complete,
  218. "", "(" + prename + name + ")" + str);
  219. }
  220. }
  221. if (_flags & F_const_method) {
  222. out << " const";
  223. }
  224. }
  225. ////////////////////////////////////////////////////////////////////
  226. // Function: CPPFunctionType::get_num_default_parameters
  227. // Access: Public
  228. // Description: Returns the number of parameters in the list that may
  229. // take default values.
  230. ////////////////////////////////////////////////////////////////////
  231. int CPPFunctionType::
  232. get_num_default_parameters() const {
  233. // The trick is just to count, beginning from the end and working
  234. // towards the front, the number of parameters that have some
  235. // initializer.
  236. const CPPParameterList::Parameters &params = _parameters->_parameters;
  237. CPPParameterList::Parameters::const_reverse_iterator pi;
  238. int count = 0;
  239. for (pi = params.rbegin();
  240. pi != params.rend() && (*pi)->_initializer != (CPPExpression *)NULL;
  241. ++pi) {
  242. count++;
  243. }
  244. return count;
  245. }
  246. ////////////////////////////////////////////////////////////////////
  247. // Function: CPPFunctionType::get_subtype
  248. // Access: Public, Virtual
  249. // Description:
  250. ////////////////////////////////////////////////////////////////////
  251. CPPDeclaration::SubType CPPFunctionType::
  252. get_subtype() const {
  253. return ST_function;
  254. }
  255. ////////////////////////////////////////////////////////////////////
  256. // Function: CPPFunctionType::as_function_type
  257. // Access: Public, Virtual
  258. // Description:
  259. ////////////////////////////////////////////////////////////////////
  260. CPPFunctionType *CPPFunctionType::
  261. as_function_type() {
  262. return this;
  263. }
  264. ////////////////////////////////////////////////////////////////////
  265. // Function: CPPFunctionType::is_equivalent_function
  266. // Access: Public
  267. // Description: This is similar to is_equal(), except it is more
  268. // forgiving: it considers the functions to be
  269. // equivalent only if the return type and the types of
  270. // all parameters match.
  271. ////////////////////////////////////////////////////////////////////
  272. bool CPPFunctionType::
  273. is_equivalent_function(const CPPFunctionType &other) const {
  274. if (!_return_type->is_equivalent(*other._return_type)) {
  275. return false;
  276. }
  277. if (_flags != other._flags) {
  278. return false;
  279. }
  280. if (!_parameters->is_equivalent(*other._parameters)) {
  281. return false;
  282. }
  283. return true;
  284. }
  285. ////////////////////////////////////////////////////////////////////
  286. // Function: CPPFunctionType::is_equal
  287. // Access: Protected, Virtual
  288. // Description: Called by CPPDeclaration() to determine whether this type is
  289. // equivalent to another type of the same type.
  290. ////////////////////////////////////////////////////////////////////
  291. bool CPPFunctionType::
  292. is_equal(const CPPDeclaration *other) const {
  293. const CPPFunctionType *ot = ((CPPDeclaration *)other)->as_function_type();
  294. assert(ot != NULL);
  295. if (_return_type != ot->_return_type) {
  296. return false;
  297. }
  298. if (_flags != ot->_flags) {
  299. return false;
  300. }
  301. if (*_parameters != *ot->_parameters) {
  302. return false;
  303. }
  304. return true;
  305. }
  306. ////////////////////////////////////////////////////////////////////
  307. // Function: CPPFunctionType::is_less
  308. // Access: Protected, Virtual
  309. // Description: Called by CPPDeclaration() to determine whether this type
  310. // should be ordered before another type of the same
  311. // type, in an arbitrary but fixed ordering.
  312. ////////////////////////////////////////////////////////////////////
  313. bool CPPFunctionType::
  314. is_less(const CPPDeclaration *other) const {
  315. const CPPFunctionType *ot = ((CPPDeclaration *)other)->as_function_type();
  316. assert(ot != NULL);
  317. if (_return_type != ot->_return_type) {
  318. return _return_type < ot->_return_type;
  319. }
  320. if (_flags != ot->_flags) {
  321. return _flags < ot->_flags;
  322. }
  323. return *_parameters < *ot->_parameters;
  324. }