Spicy
unit-item.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <utility>
6 #include <vector>
7 
8 #include <hilti/ast/attribute.h>
9 #include <hilti/ast/expression.h>
10 #include <hilti/ast/id.h>
11 #include <hilti/ast/type.h>
12 
13 #include <spicy/ast/hook.h>
14 
15 namespace spicy {
16 
17 namespace trait {
19 class isUnitItem : public hilti::trait::isNode {};
20 } // namespace trait
21 
22 namespace type {
23 namespace unit {
24 namespace detail {
25 
26 #include <spicy/autogen/__unit-item.h>
27 
29 inline Node to_node(Item i) { return Node(std::move(i)); }
30 
32 inline std::ostream& operator<<(std::ostream& out, Item d) { return out << to_node(std::move(d)); }
33 
34 } // namespace detail
35 
36 using Item = detail::Item;
37 using detail::to_node;
38 
39 namespace item {
41 template<typename T, typename std::enable_if_t<std::is_base_of<trait::isUnitItem, T>::value>* = nullptr>
42 inline Node to_node(T t) {
43  return Node(Item(std::move(t)));
44 }
45 
46 } // namespace item
47 } // namespace unit
48 } // namespace type
49 } // namespace spicy
50 
51 inline bool operator==(const spicy::type::unit::Item& x, const spicy::type::unit::Item& y) {
52  if ( &x == &y )
53  return true;
54 
55  assert(x.isEqual(y) == y.isEqual(x)); // Expected to be symmetric.
56  return x.isEqual(y);
57 }
58 
59 // TODO(robin): Not clear why we need this. Without it, vector comparisions dont'
60 // seem to find the eleement comparision operator.
61 inline bool operator==(const std::vector<spicy::type::unit::Item>& t1, const std::vector<spicy::type::unit::Item>& t2) {
62  if ( &t1 == &t2 )
63  return true;
64 
65  if ( t1.size() != t2.size() )
66  return false;
67 
68  for ( auto i = std::make_pair(t1.cbegin(), t2.cbegin()); i.first != t1.end() && i.second != t2.end();
69  ++i.first, ++i.second )
70  if ( ! (*i.first == *i.second) )
71  return false;
72 
73  return true;
74 }
75 
76 inline bool operator!=(const spicy::type::unit::Item& d1, const spicy::type::unit::Item& d2) { return ! (d1 == d2); }
77 
78 inline bool operator!=(const std::vector<spicy::type::unit::Item>& t1, const std::vector<spicy::type::unit::Item>& t2) {
79  return ! (t1 == t2);
80 }
Definition: node.h:97
Definition: unit-item.h:19
Definition: node.h:19