cppClosureType.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 cppClosureType.cxx
  10. * @author rdb
  11. * @date 2017-01-14
  12. */
  13. #include "cppClosureType.h"
  14. #include "cppType.h"
  15. #include "cppParameterList.h"
  16. #include "cppExpression.h"
  17. /**
  18. *
  19. */
  20. CPPClosureType::
  21. CPPClosureType(CaptureType default_capture) :
  22. CPPFunctionType(nullptr, nullptr, 0),
  23. _default_capture(default_capture) {
  24. }
  25. /**
  26. *
  27. */
  28. CPPClosureType::
  29. CPPClosureType(const CPPClosureType &copy) :
  30. CPPFunctionType(copy),
  31. _captures(copy._captures),
  32. _default_capture(copy._default_capture)
  33. {
  34. }
  35. /**
  36. *
  37. */
  38. void CPPClosureType::
  39. operator = (const CPPClosureType &copy) {
  40. CPPFunctionType::operator = (copy);
  41. _captures = copy._captures;
  42. _default_capture = copy._default_capture;
  43. }
  44. /**
  45. * Adds a new capture to the beginning of the capture list.
  46. */
  47. void CPPClosureType::
  48. add_capture(std::string name, CaptureType type, CPPExpression *initializer) {
  49. if (type == CT_none) {
  50. if (name == "this") {
  51. type = CT_by_reference;
  52. } else {
  53. type = CT_by_value;
  54. }
  55. }
  56. Capture capture = {std::move(name), type, initializer};
  57. _captures.insert(_captures.begin(), std::move(capture));
  58. }
  59. /**
  60. * Returns true if this declaration is an actual, factual declaration, or
  61. * false if some part of the declaration depends on a template parameter which
  62. * has not yet been instantiated.
  63. */
  64. bool CPPClosureType::
  65. is_fully_specified() const {
  66. return CPPFunctionType::is_fully_specified();
  67. }
  68. /**
  69. * Returns true if the type is default-constructible.
  70. */
  71. bool CPPClosureType::
  72. is_default_constructible() const {
  73. return false;
  74. }
  75. /**
  76. * Returns true if the type is copy-constructible.
  77. */
  78. bool CPPClosureType::
  79. is_copy_constructible() const {
  80. return true;
  81. }
  82. /**
  83. * Returns true if the type is destructible.
  84. */
  85. bool CPPClosureType::
  86. is_destructible() const {
  87. return true;
  88. }
  89. /**
  90. *
  91. */
  92. void CPPClosureType::
  93. output(std::ostream &out, int indent_level, CPPScope *scope, bool complete) const {
  94. out.put('[');
  95. bool have_capture = false;
  96. switch (_default_capture) {
  97. case CT_none:
  98. break;
  99. case CT_by_reference:
  100. out.put('&');
  101. have_capture = true;
  102. break;
  103. case CT_by_value:
  104. out.put('=');
  105. have_capture = true;
  106. break;
  107. }
  108. Captures::const_iterator it;
  109. for (it = _captures.begin(); it != _captures.end(); ++it) {
  110. const Capture &capture = *it;
  111. if (have_capture) {
  112. out << ", ";
  113. }
  114. if (capture._name == "this") {
  115. if (capture._type == CT_by_value) {
  116. out.put('*');
  117. }
  118. } else {
  119. if (capture._type == CT_by_reference) {
  120. out.put('&');
  121. }
  122. }
  123. out << capture._name;
  124. if (capture._initializer != nullptr) {
  125. out << " = " << *capture._initializer;
  126. }
  127. have_capture = true;
  128. }
  129. out.put(']');
  130. if (_parameters != nullptr) {
  131. out.put('(');
  132. _parameters->output(out, scope, true, -1);
  133. out.put(')');
  134. }
  135. if (_flags & F_noexcept) {
  136. out << " noexcept";
  137. }
  138. if (_return_type != nullptr) {
  139. out << " -> ";
  140. _return_type->output(out, indent_level, scope, false);
  141. }
  142. out << " {}";
  143. }
  144. /**
  145. *
  146. */
  147. CPPDeclaration::SubType CPPClosureType::
  148. get_subtype() const {
  149. return ST_closure;
  150. }
  151. /**
  152. *
  153. */
  154. CPPClosureType *CPPClosureType::
  155. as_closure_type() {
  156. return this;
  157. }
  158. /**
  159. * Called by CPPDeclaration() to determine whether this type is equivalent to
  160. * another type of the same type.
  161. */
  162. bool CPPClosureType::
  163. is_equal(const CPPDeclaration *other) const {
  164. return (this == other);
  165. }
  166. /**
  167. * Called by CPPDeclaration() to determine whether this type should be ordered
  168. * before another type of the same type, in an arbitrary but fixed ordering.
  169. */
  170. bool CPPClosureType::
  171. is_less(const CPPDeclaration *other) const {
  172. return (this < other);
  173. }