cppFunctionType.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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::is_trivial
  148. // Access: Public, Virtual
  149. // Description: Returns true if the type is considered a Plain Old
  150. // Data (POD) type.
  151. ////////////////////////////////////////////////////////////////////
  152. bool CPPFunctionType::
  153. is_trivial() const {
  154. return false;
  155. }
  156. ////////////////////////////////////////////////////////////////////
  157. // Function: CPPFunctionType::output
  158. // Access: Public, Virtual
  159. // Description:
  160. ////////////////////////////////////////////////////////////////////
  161. void CPPFunctionType::
  162. output(ostream &out, int indent_level, CPPScope *scope, bool complete) const {
  163. output(out, indent_level, scope, complete, -1);
  164. }
  165. ////////////////////////////////////////////////////////////////////
  166. // Function: CPPFunctionType::output
  167. // Access: Public
  168. // Description: The additional parameter allows us to specify the
  169. // number of parameters we wish to show the default
  170. // values for. If num_default_parameters is >= 0, it
  171. // indicates the number of default parameter values to
  172. // show on output. Otherwise, all parameter values are
  173. // shown.
  174. ////////////////////////////////////////////////////////////////////
  175. void CPPFunctionType::
  176. output(ostream &out, int indent_level, CPPScope *scope, bool complete,
  177. int num_default_parameters) const {
  178. if (_flags & F_trailing_return_type) {
  179. // It was declared using trailing return type, so let's format it that way.
  180. out << "auto(";
  181. _parameters->output(out, scope, true, num_default_parameters);
  182. out << ")";
  183. if (_flags & F_const_method) {
  184. out << " const";
  185. }
  186. if (_flags & F_noexcept) {
  187. out << " noexcept";
  188. }
  189. if (_flags & F_final) {
  190. out << " final";
  191. }
  192. if (_flags & F_override) {
  193. out << " override";
  194. }
  195. out << " -> ";
  196. _return_type->output(out, indent_level, scope, false);
  197. } else {
  198. _return_type->output(out, indent_level, scope, complete);
  199. out << "(";
  200. _parameters->output(out, scope, true, num_default_parameters);
  201. out << ")";
  202. if (_flags & F_const_method) {
  203. out << " const";
  204. }
  205. if (_flags & F_noexcept) {
  206. out << " noexcept";
  207. }
  208. if (_flags & F_final) {
  209. out << " final";
  210. }
  211. if (_flags & F_override) {
  212. out << " override";
  213. }
  214. }
  215. }
  216. ////////////////////////////////////////////////////////////////////
  217. // Function: CPPFunctionType::output_instance
  218. // Access: Public, Virtual
  219. // Description: Formats a C++-looking line that defines an instance
  220. // of the given type, with the indicated name. In most
  221. // cases this will be "type name", but some types have
  222. // special exceptions.
  223. ////////////////////////////////////////////////////////////////////
  224. void CPPFunctionType::
  225. output_instance(ostream &out, int indent_level, CPPScope *scope,
  226. bool complete, const string &prename,
  227. const string &name) const {
  228. output_instance(out, indent_level, scope, complete, prename, name, -1);
  229. }
  230. ////////////////////////////////////////////////////////////////////
  231. // Function: CPPFunctionType::output_instance
  232. // Access: Public
  233. // Description: The additional parameter allows us to specify the
  234. // number of parameters we wish to show the default
  235. // values for. If num_default_parameters is >= 0, it
  236. // indicates the number of default parameter values to
  237. // show on output. Otherwise, all parameter values are
  238. // shown.
  239. ////////////////////////////////////////////////////////////////////
  240. void CPPFunctionType::
  241. output_instance(ostream &out, int indent_level, CPPScope *scope,
  242. bool complete, const string &prename,
  243. const string &name, int num_default_parameters) const {
  244. ostringstream parm_string;
  245. parm_string << "(";
  246. _parameters->output(parm_string, scope, true, num_default_parameters);
  247. parm_string << ")";
  248. string str = parm_string.str();
  249. if (_flags & (F_constructor | F_destructor)) {
  250. // No return type for constructors and destructors.
  251. out << prename << name << str;
  252. } else if (_flags & F_trailing_return_type) {
  253. // It was declared using trailing return type, so let's format it that way.
  254. out << "auto ";
  255. if (prename.empty()) {
  256. out << name;
  257. } else {
  258. out << "(" << prename << name << ")";
  259. }
  260. out << str;
  261. } else {
  262. if (prename.empty()) {
  263. _return_type->output_instance(out, indent_level, scope, complete,
  264. "", prename + name + str);
  265. } else {
  266. _return_type->output_instance(out, indent_level, scope, complete,
  267. "", "(" + prename + name + ")" + str);
  268. }
  269. }
  270. if (_flags & F_const_method) {
  271. out << " const";
  272. }
  273. if (_flags & F_noexcept) {
  274. out << " noexcept";
  275. }
  276. if (_flags & F_final) {
  277. out << " final";
  278. }
  279. if (_flags & F_override) {
  280. out << " override";
  281. }
  282. if (_flags & F_trailing_return_type) {
  283. out << " -> ";
  284. _return_type->output(out, indent_level, scope, false);
  285. }
  286. }
  287. ////////////////////////////////////////////////////////////////////
  288. // Function: CPPFunctionType::get_num_default_parameters
  289. // Access: Public
  290. // Description: Returns the number of parameters in the list that may
  291. // take default values.
  292. ////////////////////////////////////////////////////////////////////
  293. int CPPFunctionType::
  294. get_num_default_parameters() const {
  295. // The trick is just to count, beginning from the end and working
  296. // towards the front, the number of parameters that have some
  297. // initializer.
  298. const CPPParameterList::Parameters &params = _parameters->_parameters;
  299. CPPParameterList::Parameters::const_reverse_iterator pi;
  300. int count = 0;
  301. for (pi = params.rbegin();
  302. pi != params.rend() && (*pi)->_initializer != (CPPExpression *)NULL;
  303. ++pi) {
  304. count++;
  305. }
  306. return count;
  307. }
  308. ////////////////////////////////////////////////////////////////////
  309. // Function: CPPFunctionType::get_subtype
  310. // Access: Public, Virtual
  311. // Description:
  312. ////////////////////////////////////////////////////////////////////
  313. CPPDeclaration::SubType CPPFunctionType::
  314. get_subtype() const {
  315. return ST_function;
  316. }
  317. ////////////////////////////////////////////////////////////////////
  318. // Function: CPPFunctionType::as_function_type
  319. // Access: Public, Virtual
  320. // Description:
  321. ////////////////////////////////////////////////////////////////////
  322. CPPFunctionType *CPPFunctionType::
  323. as_function_type() {
  324. return this;
  325. }
  326. ////////////////////////////////////////////////////////////////////
  327. // Function: CPPFunctionType::is_equivalent_function
  328. // Access: Public
  329. // Description: This is similar to is_equal(), except it is more
  330. // forgiving: it considers the functions to be
  331. // equivalent only if the return type and the types of
  332. // all parameters match.
  333. ////////////////////////////////////////////////////////////////////
  334. bool CPPFunctionType::
  335. is_equivalent_function(const CPPFunctionType &other) const {
  336. if (!_return_type->is_equivalent(*other._return_type)) {
  337. return false;
  338. }
  339. if (_flags != other._flags) {
  340. return false;
  341. }
  342. if (!_parameters->is_equivalent(*other._parameters)) {
  343. return false;
  344. }
  345. return true;
  346. }
  347. ////////////////////////////////////////////////////////////////////
  348. // Function: CPPFunctionType::is_equal
  349. // Access: Protected, Virtual
  350. // Description: Called by CPPDeclaration() to determine whether this type is
  351. // equivalent to another type of the same type.
  352. ////////////////////////////////////////////////////////////////////
  353. bool CPPFunctionType::
  354. is_equal(const CPPDeclaration *other) const {
  355. const CPPFunctionType *ot = ((CPPDeclaration *)other)->as_function_type();
  356. assert(ot != NULL);
  357. if (_return_type != ot->_return_type) {
  358. return false;
  359. }
  360. if (_flags != ot->_flags) {
  361. return false;
  362. }
  363. if (*_parameters != *ot->_parameters) {
  364. return false;
  365. }
  366. return true;
  367. }
  368. ////////////////////////////////////////////////////////////////////
  369. // Function: CPPFunctionType::is_less
  370. // Access: Protected, Virtual
  371. // Description: Called by CPPDeclaration() to determine whether this type
  372. // should be ordered before another type of the same
  373. // type, in an arbitrary but fixed ordering.
  374. ////////////////////////////////////////////////////////////////////
  375. bool CPPFunctionType::
  376. is_less(const CPPDeclaration *other) const {
  377. const CPPFunctionType *ot = ((CPPDeclaration *)other)->as_function_type();
  378. assert(ot != NULL);
  379. if (_return_type != ot->_return_type) {
  380. return _return_type < ot->_return_type;
  381. }
  382. if (_flags != ot->_flags) {
  383. return _flags < ot->_flags;
  384. }
  385. return *_parameters < *ot->_parameters;
  386. }