cppAttributeList.cxx 4.4 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 cppAttributeList.cxx
  10. * @author rdb
  11. * @date 2022-10-23
  12. */
  13. #include "cppAttributeList.h"
  14. #include "cppExpression.h"
  15. #include "cppIdentifier.h"
  16. #include "cppType.h"
  17. /**
  18. * Returns true if no attributes have been defined.
  19. */
  20. bool CPPAttributeList::
  21. is_empty() const {
  22. return _attributes.empty() && _alignas == nullptr;
  23. }
  24. /**
  25. * Returns true if the attribute list has an attribute with the given name.
  26. */
  27. bool CPPAttributeList::
  28. has_attribute(const std::string &name) const {
  29. for (const Attribute &attr : _attributes) {
  30. if (attr._ident->get_fully_scoped_name() == name) {
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. /**
  37. *
  38. */
  39. bool CPPAttributeList::
  40. operator == (const CPPAttributeList &other) const {
  41. if (_attributes.size() != other._attributes.size()) {
  42. return false;
  43. }
  44. if ((_alignas != nullptr) != (other._alignas != nullptr)) {
  45. return false;
  46. }
  47. if (_alignas != nullptr && *_alignas != *other._alignas) {
  48. return false;
  49. }
  50. for (size_t i = 0; i < _attributes.size(); ++i) {
  51. if (_attributes[i]._ident != other._attributes[i]._ident) {
  52. return false;
  53. }
  54. if (_attributes[i]._reason != other._attributes[i]._reason) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. /**
  61. *
  62. */
  63. bool CPPAttributeList::
  64. operator != (const CPPAttributeList &other) const {
  65. return !(*this == other);
  66. }
  67. /**
  68. *
  69. */
  70. bool CPPAttributeList::
  71. operator < (const CPPAttributeList &other) const {
  72. if (_attributes.size() != other._attributes.size()) {
  73. return _attributes.size() < other._attributes.size();
  74. }
  75. if ((_alignas != nullptr) != (other._alignas != nullptr)) {
  76. return _alignas == nullptr;
  77. }
  78. if (_alignas != nullptr && *_alignas != *other._alignas) {
  79. return *_alignas < *other._alignas;
  80. }
  81. for (size_t i = 0; i < _attributes.size(); ++i) {
  82. if (_attributes[i]._ident != other._attributes[i]._ident) {
  83. return _attributes[i]._ident < other._attributes[i]._ident;
  84. }
  85. if (_attributes[i]._reason != other._attributes[i]._reason) {
  86. return _attributes[i]._reason < other._attributes[i]._reason;
  87. }
  88. }
  89. return false;
  90. }
  91. /**
  92. * Adds an attribute.
  93. */
  94. void CPPAttributeList::
  95. add_attribute(CPPIdentifier *ident) {
  96. _attributes.push_back({ident});
  97. }
  98. /**
  99. * Adds an attribute.
  100. */
  101. void CPPAttributeList::
  102. add_attribute(CPPIdentifier *ident, std::string reason) {
  103. _attributes.push_back({ident, std::move(reason)});
  104. }
  105. /**
  106. * Adds an alignas specifier.
  107. */
  108. void CPPAttributeList::
  109. add_alignas(int size) {
  110. if (_alignas == nullptr || size >= _alignas->evaluate().as_integer()) {
  111. _alignas = new CPPExpression(size);
  112. }
  113. }
  114. /**
  115. * Adds an alignas specifier.
  116. */
  117. void CPPAttributeList::
  118. add_alignas(CPPType *type) {
  119. CPPExpression expr = CPPExpression::alignof_func(type);
  120. if (_alignas == nullptr || expr.evaluate().as_integer() > _alignas->evaluate().as_integer()) {
  121. _alignas = new CPPExpression(expr);
  122. }
  123. }
  124. /**
  125. * Adds an alignas specifier.
  126. */
  127. void CPPAttributeList::
  128. add_alignas(CPPExpression *expr) {
  129. if (_alignas == nullptr || expr->evaluate().as_integer() > _alignas->evaluate().as_integer()) {
  130. _alignas = expr;
  131. }
  132. }
  133. /**
  134. * Merges the other list into this one.
  135. */
  136. void CPPAttributeList::
  137. add_attributes_from(const CPPAttributeList &other) {
  138. for (const Attribute &attr : other._attributes) {
  139. _attributes.push_back(attr);
  140. }
  141. if (other._alignas != nullptr) {
  142. add_alignas(other._alignas);
  143. }
  144. }
  145. /**
  146. *
  147. */
  148. void CPPAttributeList::
  149. output(std::ostream &out, CPPScope *scope) const {
  150. Attributes::const_iterator it = _attributes.begin();
  151. if (it != _attributes.end()) {
  152. out << "[[";
  153. (*it)._ident->output(out, scope);
  154. if (!(*it)._reason.empty()) {
  155. out << "(" << (*it)._reason << ")";
  156. }
  157. for (++it; it != _attributes.end(); ++it) {
  158. out << ", ";
  159. (*it)._ident->output(out, scope);
  160. if (!(*it)._reason.empty()) {
  161. out << "(" << (*it)._reason << ")";
  162. }
  163. }
  164. out << "]]";
  165. if (_alignas != nullptr) {
  166. out << " ";
  167. }
  168. }
  169. if (_alignas != nullptr) {
  170. out << "alignas(";
  171. if (_alignas->_type == CPPExpression::T_alignof) {
  172. _alignas->_u._typecast._to->output(out, 0, scope, false);
  173. } else {
  174. _alignas->output(out, 0, scope, false);
  175. }
  176. out << ")";
  177. }
  178. }