cppClosureType.cxx 3.9 KB

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