cppFunctionType.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. if (_flags & F_noexcept) {
  225. out << " noexcept";
  226. }
  227. }
  228. ////////////////////////////////////////////////////////////////////
  229. // Function: CPPFunctionType::get_num_default_parameters
  230. // Access: Public
  231. // Description: Returns the number of parameters in the list that may
  232. // take default values.
  233. ////////////////////////////////////////////////////////////////////
  234. int CPPFunctionType::
  235. get_num_default_parameters() const {
  236. // The trick is just to count, beginning from the end and working
  237. // towards the front, the number of parameters that have some
  238. // initializer.
  239. const CPPParameterList::Parameters &params = _parameters->_parameters;
  240. CPPParameterList::Parameters::const_reverse_iterator pi;
  241. int count = 0;
  242. for (pi = params.rbegin();
  243. pi != params.rend() && (*pi)->_initializer != (CPPExpression *)NULL;
  244. ++pi) {
  245. count++;
  246. }
  247. return count;
  248. }
  249. ////////////////////////////////////////////////////////////////////
  250. // Function: CPPFunctionType::get_subtype
  251. // Access: Public, Virtual
  252. // Description:
  253. ////////////////////////////////////////////////////////////////////
  254. CPPDeclaration::SubType CPPFunctionType::
  255. get_subtype() const {
  256. return ST_function;
  257. }
  258. ////////////////////////////////////////////////////////////////////
  259. // Function: CPPFunctionType::as_function_type
  260. // Access: Public, Virtual
  261. // Description:
  262. ////////////////////////////////////////////////////////////////////
  263. CPPFunctionType *CPPFunctionType::
  264. as_function_type() {
  265. return this;
  266. }
  267. ////////////////////////////////////////////////////////////////////
  268. // Function: CPPFunctionType::is_equivalent_function
  269. // Access: Public
  270. // Description: This is similar to is_equal(), except it is more
  271. // forgiving: it considers the functions to be
  272. // equivalent only if the return type and the types of
  273. // all parameters match.
  274. ////////////////////////////////////////////////////////////////////
  275. bool CPPFunctionType::
  276. is_equivalent_function(const CPPFunctionType &other) const {
  277. if (!_return_type->is_equivalent(*other._return_type)) {
  278. return false;
  279. }
  280. if (_flags != other._flags) {
  281. return false;
  282. }
  283. if (!_parameters->is_equivalent(*other._parameters)) {
  284. return false;
  285. }
  286. return true;
  287. }
  288. ////////////////////////////////////////////////////////////////////
  289. // Function: CPPFunctionType::is_equal
  290. // Access: Protected, Virtual
  291. // Description: Called by CPPDeclaration() to determine whether this type is
  292. // equivalent to another type of the same type.
  293. ////////////////////////////////////////////////////////////////////
  294. bool CPPFunctionType::
  295. is_equal(const CPPDeclaration *other) const {
  296. const CPPFunctionType *ot = ((CPPDeclaration *)other)->as_function_type();
  297. assert(ot != NULL);
  298. if (_return_type != ot->_return_type) {
  299. return false;
  300. }
  301. if (_flags != ot->_flags) {
  302. return false;
  303. }
  304. if (*_parameters != *ot->_parameters) {
  305. return false;
  306. }
  307. return true;
  308. }
  309. ////////////////////////////////////////////////////////////////////
  310. // Function: CPPFunctionType::is_less
  311. // Access: Protected, Virtual
  312. // Description: Called by CPPDeclaration() to determine whether this type
  313. // should be ordered before another type of the same
  314. // type, in an arbitrary but fixed ordering.
  315. ////////////////////////////////////////////////////////////////////
  316. bool CPPFunctionType::
  317. is_less(const CPPDeclaration *other) const {
  318. const CPPFunctionType *ot = ((CPPDeclaration *)other)->as_function_type();
  319. assert(ot != NULL);
  320. if (_return_type != ot->_return_type) {
  321. return _return_type < ot->_return_type;
  322. }
  323. if (_flags != ot->_flags) {
  324. return _flags < ot->_flags;
  325. }
  326. return *_parameters < *ot->_parameters;
  327. }