cppFunctionType.cxx 10 KB

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