1 #ifndef STAN_LANG_AST_BARE_EXPR_TYPE_DEF_HPP
2 #define STAN_LANG_AST_BARE_EXPR_TYPE_DEF_HPP
3 
4 #include <stan/lang/ast/type/bare_array_type.hpp>
5 #include <stan/lang/ast/type/bare_expr_type.hpp>
6 #include <stan/lang/ast/type/double_type.hpp>
7 #include <stan/lang/ast/type/ill_formed_type.hpp>
8 #include <stan/lang/ast/type/int_type.hpp>
9 #include <stan/lang/ast/type/matrix_type.hpp>
10 #include <stan/lang/ast/type/row_vector_type.hpp>
11 #include <stan/lang/ast/type/vector_type.hpp>
12 #include <stan/lang/ast/type/void_type.hpp>
13 
14 #include <stan/lang/ast/fun/bare_type_is_data_vis.hpp>
15 #include <stan/lang/ast/fun/bare_type_order_id_vis.hpp>
16 #include <stan/lang/ast/fun/bare_type_set_is_data_vis.hpp>
17 #include <stan/lang/ast/fun/bare_type_total_dims_vis.hpp>
18 #include <stan/lang/ast/fun/bare_type_vis.hpp>
19 #include <stan/lang/ast/fun/write_bare_expr_type.hpp>
20 
21 #include <boost/variant/apply_visitor.hpp>
22 #include <boost/variant/get.hpp>
23 
24 #include <ostream>
25 #include <string>
26 
27 namespace stan {
28 namespace lang {
29 
bare_expr_type()30 bare_expr_type::bare_expr_type() : bare_type_(ill_formed_type()) {}
31 
bare_expr_type(const bare_expr_type & x)32 bare_expr_type::bare_expr_type(const bare_expr_type& x)
33     : bare_type_(x.bare_type_) {}
34 
bare_expr_type(const bare_t & x)35 bare_expr_type::bare_expr_type(const bare_t& x) : bare_type_(x) {}
36 
bare_expr_type(const ill_formed_type & x)37 bare_expr_type::bare_expr_type(const ill_formed_type& x)
38     : bare_type_(ill_formed_type()) {}
39 
bare_expr_type(const void_type & x)40 bare_expr_type::bare_expr_type(const void_type& x) : bare_type_(void_type()) {}
41 
bare_expr_type(const int_type & x)42 bare_expr_type::bare_expr_type(const int_type& x)
43     : bare_type_(int_type(x.is_data_)) {}
44 
bare_expr_type(const double_type & x)45 bare_expr_type::bare_expr_type(const double_type& x)
46     : bare_type_(double_type(x.is_data_)) {}
47 
bare_expr_type(const vector_type & x)48 bare_expr_type::bare_expr_type(const vector_type& x)
49     : bare_type_(vector_type(x.is_data_)) {}
50 
bare_expr_type(const row_vector_type & x)51 bare_expr_type::bare_expr_type(const row_vector_type& x)
52     : bare_type_(row_vector_type(x.is_data_)) {}
53 
bare_expr_type(const matrix_type & x)54 bare_expr_type::bare_expr_type(const matrix_type& x)
55     : bare_type_(matrix_type(x.is_data_)) {}
56 
bare_expr_type(const bare_array_type & x)57 bare_expr_type::bare_expr_type(const bare_array_type& x)
58     : bare_type_(bare_array_type(x.element_type_)) {}
59 
array_element_type() const60 bare_expr_type bare_expr_type::array_element_type() const {
61   if (boost::get<stan::lang::bare_array_type>(&bare_type_)) {
62     bare_array_type bat = boost::get<stan::lang::bare_array_type>(bare_type_);
63     return bat.element_type_;
64   }
65   return ill_formed_type();
66 }
67 
array_contains() const68 bare_expr_type bare_expr_type::array_contains() const {
69   if (boost::get<stan::lang::bare_array_type>(&bare_type_)) {
70     bare_array_type bat = boost::get<stan::lang::bare_array_type>(bare_type_);
71     return bat.contains();
72   }
73   return ill_formed_type();
74 }
75 
array_dims() const76 int bare_expr_type::array_dims() const {
77   if (boost::get<stan::lang::bare_array_type>(&bare_type_)) {
78     bare_array_type bat = boost::get<stan::lang::bare_array_type>(bare_type_);
79     return bat.dims();
80   }
81   return 0;
82 }
83 
innermost_type() const84 bare_expr_type bare_expr_type::innermost_type() const {
85   if (boost::get<stan::lang::bare_array_type>(&bare_type_)) {
86     bare_array_type bat = boost::get<stan::lang::bare_array_type>(bare_type_);
87     return bat.contains();
88   }
89   return bare_type_;
90 }
91 
is_array_type() const92 bool bare_expr_type::is_array_type() const {
93   if (boost::get<stan::lang::bare_array_type>(&bare_type_))
94     return true;
95   return false;
96 }
97 
is_data() const98 bool bare_expr_type::is_data() const {
99   bare_type_is_data_vis vis;
100   return boost::apply_visitor(vis, bare_type_);
101 }
102 
is_double_type() const103 bool bare_expr_type::is_double_type() const {
104   return order_id() == double_type().oid();
105 }
106 
is_ill_formed_type() const107 bool bare_expr_type::is_ill_formed_type() const {
108   return order_id() == ill_formed_type().oid();
109 }
110 
is_int_type() const111 bool bare_expr_type::is_int_type() const {
112   return order_id() == int_type().oid();
113 }
114 
is_matrix_type() const115 bool bare_expr_type::is_matrix_type() const {
116   return order_id() == matrix_type().oid();
117 }
118 
is_primitive() const119 bool bare_expr_type::is_primitive() const {
120   return order_id() == int_type().oid() || order_id() == double_type().oid();
121 }
122 
is_row_vector_type() const123 bool bare_expr_type::is_row_vector_type() const {
124   return order_id() == row_vector_type().oid();
125 }
126 
is_vector_type() const127 bool bare_expr_type::is_vector_type() const {
128   return order_id() == vector_type().oid();
129 }
130 
is_void_type() const131 bool bare_expr_type::is_void_type() const {
132   return order_id() == void_type().oid();
133 }
134 
num_dims() const135 int bare_expr_type::num_dims() const {
136   bare_type_total_dims_vis vis;
137   return boost::apply_visitor(vis, bare_type_);
138 }
139 
order_id() const140 std::string bare_expr_type::order_id() const {
141   bare_type_order_id_vis vis;
142   return boost::apply_visitor(vis, bare_type_);
143 }
144 
set_is_data()145 void bare_expr_type::set_is_data() {
146   bare_type_set_is_data_vis vis;
147   return boost::apply_visitor(vis, bare_type_);
148 }
149 
operator ==(const bare_expr_type & bare_type) const150 bool bare_expr_type::operator==(const bare_expr_type& bare_type) const {
151   return order_id() == bare_type.order_id();
152 }
153 
operator !=(const bare_expr_type & bare_type) const154 bool bare_expr_type::operator!=(const bare_expr_type& bare_type) const {
155   return order_id() != bare_type.order_id();
156 }
157 
operator <(const bare_expr_type & bare_type) const158 bool bare_expr_type::operator<(const bare_expr_type& bare_type) const {
159   if (is_data() == bare_type.is_data())
160     return order_id() < bare_type.order_id();
161   return is_data() < bare_type.is_data();
162 }
163 
operator >(const bare_expr_type & bare_type) const164 bool bare_expr_type::operator>(const bare_expr_type& bare_type) const {
165   if (is_data() == bare_type.is_data())
166     return order_id() > bare_type.order_id();
167   return is_data() > bare_type.is_data();
168 }
169 
operator <=(const bare_expr_type & bare_type) const170 bool bare_expr_type::operator<=(const bare_expr_type& bare_type) const {
171   if (is_data() == bare_type.is_data())
172     return order_id() <= bare_type.order_id();
173   return is_data() <= bare_type.is_data();
174 }
175 
operator >=(const bare_expr_type & bare_type) const176 bool bare_expr_type::operator>=(const bare_expr_type& bare_type) const {
177   if (is_data() == bare_type.is_data())
178     return order_id() >= bare_type.order_id();
179   return is_data() >= bare_type.is_data();
180 }
181 
operator <<(std::ostream & o,const bare_expr_type & bare_type)182 std::ostream& operator<<(std::ostream& o, const bare_expr_type& bare_type) {
183   write_bare_expr_type(o, bare_type);
184   return o;
185 }
186 }  // namespace lang
187 }  // namespace stan
188 #endif
189