cppFunctionType.cxx 11 KB

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