1 #ifndef STAN_LANG_GRAMMARS_SEMANTIC_ACTIONS_DEF_CPP
2 #define STAN_LANG_GRAMMARS_SEMANTIC_ACTIONS_DEF_CPP
3
4 #include <stan/io/program_reader.hpp>
5 #include <stan/lang/ast.hpp>
6 #include <stan/lang/grammars/iterator_typedefs.hpp>
7 #include <stan/lang/grammars/semantic_actions.hpp>
8
9 #include <boost/algorithm/string.hpp>
10 #include <boost/format.hpp>
11 #include <boost/spirit/include/qi.hpp>
12 #include <boost/variant/apply_visitor.hpp>
13 #include <boost/variant/recursive_variant.hpp>
14 #include <climits>
15 #include <cstddef>
16 #include <iomanip>
17 #include <iostream>
18 #include <limits>
19 #include <map>
20 #include <set>
21 #include <stdexcept>
22 #include <string>
23 #include <utility>
24 #include <vector>
25
26 #include <typeinfo>
27
28 namespace stan {
29
30 namespace lang {
31
32 /**
33 * Add namespace qualifier `stan::math::` or `std::` to function names
34 * in order to avoid ambiguities for functions in the Stan language which
35 * are also defined in c and/or other libraries that some compilers (gcc)
36 * bring into the top-level namespace.
37 *
38 * @param[in, out] f Function to qualify.
39 */
qualify_builtins(fun & f)40 void qualify_builtins(fun &f) {
41 if ((f.name_ == "max" || f.name_ == "min") && f.args_.size() == 2 &&
42 f.args_[0].bare_type().is_int_type() &&
43 f.args_[1].bare_type().is_int_type()) {
44 f.name_ = "std::" + f.name_;
45 return;
46 }
47
48 if (f.name_ == "ceil" && f.args_[0].bare_type().is_int_type()) {
49 f.name_ = "std::" + f.name_;
50 return;
51 }
52
53 if ((f.args_.size() == 0 &&
54 (f.name_ == "e" || f.name_ == "pi" || f.name_ == "log2" ||
55 f.name_ == "log10" || f.name_ == "sqrt2" || f.name_ == "not_a_number" ||
56 f.name_ == "positive_infinity" || f.name_ == "negative_infinity" ||
57 f.name_ == "machine_precision")) ||
58 (f.args_.size() == 1 &&
59 (f.name_ == "abs" || f.name_ == "acos" || f.name_ == "acosh" ||
60 f.name_ == "asin" || f.name_ == "asinh" || f.name_ == "atan" ||
61 f.name_ == "atan2" || f.name_ == "atanh" || f.name_ == "cbrt" ||
62 f.name_ == "ceil" || f.name_ == "cos" || f.name_ == "cosh" ||
63 f.name_ == "erf" || f.name_ == "erfc" || f.name_ == "exp" ||
64 f.name_ == "exp2" || f.name_ == "expm1" || f.name_ == "fabs" ||
65 f.name_ == "floor" || f.name_ == "lgamma" || f.name_ == "log" ||
66 f.name_ == "log1p" || f.name_ == "log2" || f.name_ == "log10" ||
67 f.name_ == "round" || f.name_ == "sin" || f.name_ == "sinh" ||
68 f.name_ == "sqrt" || f.name_ == "tan" || f.name_ == "tanh" ||
69 f.name_ == "tgamma" || f.name_ == "trunc")) ||
70 (f.args_.size() == 2 && (f.name_ == "fdim" || f.name_ == "fmax" ||
71 f.name_ == "fmin" || f.name_ == "hypot")) ||
72 (f.args_.size() == 3 && f.name_ == "fma"))
73 f.name_ = "stan::math::" + f.name_;
74 }
75
has_prob_suffix(const std::string & s)76 bool has_prob_suffix(const std::string &s) {
77 return ends_with("_lpdf", s) || ends_with("_lpmf", s) ||
78 ends_with("_lcdf", s) || ends_with("_lccdf", s);
79 }
80
has_rng_lp_suffix(const std::string & s)81 bool has_rng_lp_suffix(const std::string &s) {
82 return ends_with("_lp", s) || ends_with("_rng", s);
83 }
84
replace_suffix(const std::string & old_suffix,const std::string & new_suffix,fun & f)85 void replace_suffix(const std::string &old_suffix,
86 const std::string &new_suffix, fun &f) {
87 if (!ends_with(old_suffix, f.name_))
88 return;
89 f.name_ = f.name_.substr(0, f.name_.size() - old_suffix.size()) + new_suffix;
90 }
91
deprecate_fun(const std::string & old_name,const std::string & new_name,fun & f,std::ostream & msgs)92 bool deprecate_fun(const std::string &old_name, const std::string &new_name,
93 fun &f, std::ostream &msgs) {
94 if (f.name_ != old_name)
95 return false;
96 f.original_name_ = f.name_;
97 f.name_ = new_name;
98 msgs << "Info: Function name '" << old_name << "' is deprecated"
99 << " and will be removed in a later release; please replace"
100 << " with '" << new_name << "'" << std::endl;
101 return true;
102 }
103
deprecate_suffix(const std::string & deprecated_suffix,const std::string & replacement,fun & f,std::ostream & msgs)104 bool deprecate_suffix(const std::string &deprecated_suffix,
105 const std::string &replacement, fun &f,
106 std::ostream &msgs) {
107 if (!ends_with(deprecated_suffix, f.name_))
108 return false;
109 msgs << "Info: Deprecated function '" << f.name_ << "';"
110 << " please replace suffix '" << deprecated_suffix << "' with "
111 << replacement << std::endl;
112 return true;
113 }
114
set_fun_type(fun & fun,std::ostream & error_msgs)115 void set_fun_type(fun &fun, std::ostream &error_msgs) {
116 std::vector<bare_expr_type> arg_types;
117 for (size_t i = 0; i < fun.args_.size(); ++i)
118 arg_types.push_back(fun.args_[i].bare_type());
119 fun.type_ = function_signatures::instance().get_result_type(
120 fun.name_, arg_types, error_msgs);
121 }
122
num_dimss(std::vector<std::vector<stan::lang::expression>> & dimss)123 int num_dimss(std::vector<std::vector<stan::lang::expression>> &dimss) {
124 int sum = 0;
125 for (size_t i = 0; i < dimss.size(); ++i)
126 sum += dimss[i].size();
127 return sum;
128 }
129
is_double_return(const std::string & function_name,const std::vector<bare_expr_type> & arg_types,std::ostream & error_msgs)130 bool is_double_return(const std::string &function_name,
131 const std::vector<bare_expr_type> &arg_types,
132 std::ostream &error_msgs) {
133 return function_signatures::instance()
134 .get_result_type(function_name, arg_types, error_msgs, true)
135 .is_double_type();
136 }
137
is_univariate(const bare_expr_type & et)138 bool is_univariate(const bare_expr_type &et) {
139 return et.num_dims() == 0 && (et.is_int_type() || et.is_double_type());
140 }
141
can_assign_to_lhs_var(const std::string & lhs_var_name,const scope & var_scope,const variable_map & vm,std::ostream & error_msgs)142 bool can_assign_to_lhs_var(const std::string &lhs_var_name,
143 const scope &var_scope, const variable_map &vm,
144 std::ostream &error_msgs) {
145 if (lhs_var_name == std::string("lp__")) {
146 error_msgs << std::endl
147 << "Error (fatal): Use of lp__ is no longer supported."
148 << std::endl
149 << " Use target += ... statement to increment log density."
150 << std::endl;
151 return false;
152 }
153 if (!vm.exists(lhs_var_name)) {
154 error_msgs << "Unknown variable in assignment"
155 << "; lhs variable=" << lhs_var_name << std::endl;
156 return false;
157 }
158 scope lhs_origin = vm.get_scope(lhs_var_name);
159 // enforce constancy of loop variables
160 if (lhs_origin.program_block() == loop_identifier_origin) {
161 error_msgs << "Loop variable " << lhs_var_name
162 << " cannot be used on left side of assignment statement."
163 << std::endl;
164 return false;
165 }
166 // enforce constancy of function args
167 if (!lhs_origin.is_local() && lhs_origin.fun()) {
168 error_msgs << "Cannot assign to function argument variables." << std::endl
169 << "Use local variables instead." << std::endl;
170 return false;
171 }
172 if (lhs_origin.program_block() != var_scope.program_block()) {
173 error_msgs << "Cannot assign to variable outside of declaration block"
174 << "; left-hand-side variable origin=";
175 print_scope(error_msgs, lhs_origin);
176 error_msgs << std::endl;
177 return false;
178 }
179 return true;
180 }
181
infer_var_dims_type(const bare_expr_type & var_type,const variable_dims & var_dims)182 bare_expr_type infer_var_dims_type(const bare_expr_type &var_type,
183 const variable_dims &var_dims) {
184 size_t num_index_dims = var_dims.dims_.size();
185 return infer_type_indexing(var_type, num_index_dims);
186 }
187
has_same_shape(const bare_expr_type & lhs_type,const expression & rhs_expr,const std::string & name,const std::string & stmt_type,std::ostream & error_msgs)188 bool has_same_shape(const bare_expr_type &lhs_type, const expression &rhs_expr,
189 const std::string &name, const std::string &stmt_type,
190 std::ostream &error_msgs) {
191 if (lhs_type.num_dims() != rhs_expr.bare_type().num_dims() ||
192 lhs_type.array_dims() != rhs_expr.bare_type().array_dims()) {
193 error_msgs << "Dimension mismatch in " << stmt_type
194 << "; variable name = " << name << ", type = " << lhs_type
195 << "; right-hand side type = " << rhs_expr.bare_type() << "."
196 << std::endl;
197 return false;
198 }
199
200 // allow int -> double promotion, even in arrays
201 bool types_compatible =
202 (lhs_type.innermost_type() == rhs_expr.bare_type().innermost_type() ||
203 (lhs_type.innermost_type().is_double_type() &&
204 rhs_expr.bare_type().innermost_type().is_int_type()));
205 if (!types_compatible) {
206 error_msgs << "Base type mismatch in " << stmt_type
207 << "; variable name = " << name
208 << ", base type = " << lhs_type.innermost_type()
209 << "; right-hand side base type = "
210 << rhs_expr.bare_type().innermost_type() << "." << std::endl;
211 return false;
212 }
213 return true;
214 }
215
216 // //////////////////////////////////
217 // *** functors for grammar rules ***
218 // //////////////////////////////////
219
operator ()(const expression & expr,bool & pass,std::stringstream & error_msgs) const220 void validate_double_expr::operator()(const expression &expr, bool &pass,
221 std::stringstream &error_msgs) const {
222 if (!expr.bare_type().is_double_type() && !expr.bare_type().is_int_type()) {
223 error_msgs << "Expression denoting real required; found type="
224 << expr.bare_type() << "." << std::endl;
225 pass = false;
226 return;
227 }
228 pass = true;
229 }
230 boost::phoenix::function<validate_double_expr> validate_double_expr_f;
231
232 template <typename L, typename R>
operator ()(L & lhs,const R & rhs) const233 void assign_lhs::operator()(L &lhs, const R &rhs) const {
234 lhs = rhs;
235 }
236 boost::phoenix::function<assign_lhs> assign_lhs_f;
237
238 template void assign_lhs::operator()(expression &, const expression &) const;
239 template void assign_lhs::operator()(expression &,
240 const double_literal &) const;
241 template void assign_lhs::operator()(expression &, const int_literal &) const;
242 template void assign_lhs::operator()(expression &, const integrate_1d &) const;
243 template void assign_lhs::operator()(expression &, const integrate_ode &) const;
244 template void assign_lhs::operator()(expression &,
245 const integrate_ode_control &) const;
246 template void assign_lhs::operator()(expression &,
247 const algebra_solver &) const;
248 template void assign_lhs::operator()(expression &,
249 const algebra_solver_control &) const;
250 template void assign_lhs::operator()(expression &, const map_rect &) const;
251 template void assign_lhs::operator()(array_expr &, const array_expr &) const;
252 template void assign_lhs::operator()(matrix_expr &, const matrix_expr &) const;
253 template void assign_lhs::operator()(row_vector_expr &,
254 const row_vector_expr &) const;
255 template void assign_lhs::operator()(int &, const int &) const;
256 template void assign_lhs::operator()(size_t &, const size_t &) const;
257 template void assign_lhs::operator()(statement &, const statement &) const;
258 template void assign_lhs::operator()(std::vector<var_decl> &,
259 const std::vector<var_decl> &) const;
260 template void assign_lhs::operator()(std::vector<idx> &,
261 const std::vector<idx> &) const;
262 template void assign_lhs::
263 operator()(std::vector<std::vector<expression>> &,
264 const std::vector<std::vector<expression>> &) const;
265 template void assign_lhs::operator()(fun &, const fun &) const;
266 template void assign_lhs::operator()(variable &, const variable &) const;
267 template void assign_lhs::operator()(std::vector<block_var_decl> &,
268 const std::vector<block_var_decl> &) const;
269 template void assign_lhs::operator()(std::vector<local_var_decl> &,
270 const std::vector<local_var_decl> &) const;
271 template void assign_lhs::operator()(bare_expr_type &,
272 const bare_expr_type &) const;
273 template void assign_lhs::operator()(block_var_decl &,
274 const block_var_decl &) const;
275 template void assign_lhs::operator()(local_var_decl &,
276 const local_var_decl &) const;
277 template void assign_lhs::operator()(var_decl &, const var_decl &) const;
278
operator ()(const expression & expr,bool & pass,std::ostream & error_msgs) const279 void validate_expr_type3::operator()(const expression &expr, bool &pass,
280 std::ostream &error_msgs) const {
281 pass = !expr.bare_type().is_ill_formed_type();
282 if (!pass)
283 error_msgs << "Expression is ill formed." << std::endl;
284 }
285 boost::phoenix::function<validate_expr_type3> validate_expr_type3_f;
286
operator ()(const std::string & s,bool & pass) const287 void is_prob_fun::operator()(const std::string &s, bool &pass) const {
288 pass = has_prob_suffix(s);
289 }
290 boost::phoenix::function<is_prob_fun> is_prob_fun_f;
291
operator ()(expression & expr1,const expression & expr2,std::ostream & error_msgs) const292 void addition_expr3::operator()(expression &expr1, const expression &expr2,
293 std::ostream &error_msgs) const {
294 if (expr1.bare_type().is_primitive() && expr2.bare_type().is_primitive()) {
295 expr1 += expr2;
296 return;
297 }
298 std::vector<expression> args;
299 args.push_back(expr1);
300 args.push_back(expr2);
301 fun f("add", args);
302 set_fun_type(f, error_msgs);
303 expr1 = expression(f);
304 }
305 boost::phoenix::function<addition_expr3> addition3_f;
306
operator ()(expression & expr1,const expression & expr2,std::ostream & error_msgs) const307 void subtraction_expr3::operator()(expression &expr1, const expression &expr2,
308 std::ostream &error_msgs) const {
309 if (expr1.bare_type().is_primitive() && expr2.bare_type().is_primitive()) {
310 expr1 -= expr2;
311 return;
312 }
313 std::vector<expression> args;
314 args.push_back(expr1);
315 args.push_back(expr2);
316 fun f("subtract", args);
317 set_fun_type(f, error_msgs);
318 expr1 = expression(f);
319 }
320 boost::phoenix::function<subtraction_expr3> subtraction3_f;
321
operator ()(size_t & lhs) const322 void increment_size_t::operator()(size_t &lhs) const { ++lhs; }
323 boost::phoenix::function<increment_size_t> increment_size_t_f;
324
operator ()(conditional_op & conditional_op,const scope & var_scope,bool & pass,const variable_map & var_map,std::ostream & error_msgs) const325 void validate_conditional_op::operator()(conditional_op &conditional_op,
326 const scope &var_scope, bool &pass,
327 const variable_map &var_map,
328 std::ostream &error_msgs) const {
329 bare_expr_type cond_type = conditional_op.cond_.bare_type();
330 if (!cond_type.is_int_type()) {
331 error_msgs << "Condition in ternary expression must be"
332 << " primitive int;"
333 << " found type=" << cond_type << "." << std::endl;
334 pass = false;
335 return;
336 }
337
338 bare_expr_type true_val_type = conditional_op.true_val_.bare_type();
339 bare_expr_type false_val_type = conditional_op.false_val_.bare_type();
340
341 bool types_compatible =
342 (true_val_type == false_val_type) ||
343 (true_val_type.is_double_type() && false_val_type.is_int_type()) ||
344 (true_val_type.is_int_type() && false_val_type.is_double_type());
345
346 if (!types_compatible) {
347 error_msgs << "Type mismatch in ternary expression,"
348 << " expression when true is: ";
349 write_bare_expr_type(error_msgs, true_val_type);
350 error_msgs << "; expression when false is: ";
351 write_bare_expr_type(error_msgs, false_val_type);
352 error_msgs << "." << std::endl;
353 pass = false;
354 return;
355 }
356
357 if (true_val_type.is_primitive())
358 conditional_op.type_ =
359 (true_val_type == false_val_type) ? true_val_type : double_type();
360 else
361 conditional_op.type_ = true_val_type;
362
363 conditional_op.has_var_ = has_var(conditional_op, var_map);
364 conditional_op.scope_ = var_scope;
365 pass = true;
366 }
367 boost::phoenix::function<validate_conditional_op> validate_conditional_op_f;
368
operator ()(expression & expr1,const expression & expr2,const std::string & op,const std::string & fun_name,std::ostream & error_msgs) const369 void binary_op_expr::operator()(expression &expr1, const expression &expr2,
370 const std::string &op,
371 const std::string &fun_name,
372 std::ostream &error_msgs) const {
373 if (!expr1.bare_type().is_primitive() || !expr2.bare_type().is_primitive()) {
374 error_msgs << "Binary infix operator " << op
375 << " with functional interpretation " << fun_name
376 << " requires arguments of primitive type (int or real)"
377 << ", found left type=" << expr1.bare_type()
378 << ", right arg type=" << expr2.bare_type() << "." << std::endl;
379 }
380 std::vector<expression> args;
381 args.push_back(expr1);
382 args.push_back(expr2);
383 fun f(fun_name, args);
384 set_fun_type(f, error_msgs);
385 expr1 = expression(f);
386 }
387 boost::phoenix::function<binary_op_expr> binary_op_f;
388
389 void validate_non_void_arg_function::
operator ()(bare_expr_type & arg_type,const scope & var_scope,bool & pass,std::ostream & error_msgs) const390 operator()(bare_expr_type &arg_type, const scope &var_scope, bool &pass,
391 std::ostream &error_msgs) const {
392 if (var_scope.program_block() == data_origin)
393 arg_type.set_is_data();
394 pass = !arg_type.is_void_type();
395 if (!pass)
396 error_msgs << "Functions cannot contain void argument types; "
397 << "found void argument." << std::endl;
398 }
399 boost::phoenix::function<validate_non_void_arg_function>
400 validate_non_void_arg_f;
401
operator ()(const bare_expr_type & return_type,scope & var_scope,bool & pass,std::ostream & error_msgs) const402 void set_void_function::operator()(const bare_expr_type &return_type,
403 scope &var_scope, bool &pass,
404 std::ostream &error_msgs) const {
405 if (return_type.is_void_type() && return_type.num_dims() > 0) {
406 error_msgs << "Void return type may not have dimensions declared."
407 << std::endl;
408 pass = false;
409 return;
410 }
411 var_scope = return_type.is_void_type() ? scope(void_function_argument_origin)
412 : scope(function_argument_origin);
413 pass = true;
414 }
415 boost::phoenix::function<set_void_function> set_void_function_f;
416
operator ()(const std::string & identifier,scope & var_scope) const417 void set_allows_sampling_origin::operator()(const std::string &identifier,
418 scope &var_scope) const {
419 if (ends_with("_lp", identifier)) {
420 var_scope = var_scope.void_fun() ? scope(void_function_argument_origin_lp)
421 : scope(function_argument_origin_lp);
422 } else if (ends_with("_rng", identifier)) {
423 var_scope = var_scope.void_fun() ? scope(void_function_argument_origin_rng)
424 : scope(function_argument_origin_rng);
425 } else {
426 var_scope = var_scope.void_fun() ? scope(void_function_argument_origin)
427 : scope(function_argument_origin);
428 }
429 }
430 boost::phoenix::function<set_allows_sampling_origin>
431 set_allows_sampling_origin_f;
432
433 void validate_declarations::
operator ()(bool & pass,std::set<std::pair<std::string,function_signature_t>> & declared,std::set<std::pair<std::string,function_signature_t>> & defined,std::ostream & error_msgs,bool allow_undefined) const434 operator()(bool &pass,
435 std::set<std::pair<std::string, function_signature_t>> &declared,
436 std::set<std::pair<std::string, function_signature_t>> &defined,
437 std::ostream &error_msgs, bool allow_undefined) const {
438 using std::pair;
439 using std::set;
440 using std::string;
441 typedef set<pair<string, function_signature_t>>::iterator iterator_t;
442 if (!allow_undefined) {
443 for (iterator_t it = declared.begin(); it != declared.end(); ++it) {
444 if (defined.find(*it) == defined.end()) {
445 error_msgs << "Function declared, but not defined."
446 << " Function name=" << (*it).first << std::endl;
447 pass = false;
448 return;
449 }
450 }
451 }
452 pass = true;
453 }
454 boost::phoenix::function<validate_declarations> validate_declarations_f;
455
fun_exists(const std::set<std::pair<std::string,function_signature_t>> & existing,const std::pair<std::string,function_signature_t> & name_sig,bool name_only=true)456 bool fun_exists(
457 const std::set<std::pair<std::string, function_signature_t>> &existing,
458 const std::pair<std::string, function_signature_t> &name_sig,
459 bool name_only = true) {
460 for (std::set<std::pair<std::string, function_signature_t>>::const_iterator
461 it = existing.begin();
462 it != existing.end(); ++it) {
463 if (name_sig.first == (*it).first &&
464 (name_only || name_sig.second.second == (*it).second.second))
465 return true; // name and arg sequences match
466 }
467 return false;
468 }
469
operator ()(std::string & fname,bool & pass,std::ostream & error_msgs) const470 void validate_prob_fun::operator()(std::string &fname, bool &pass,
471 std::ostream &error_msgs) const {
472 if (has_prob_fun_suffix(fname)) {
473 std::string dist_name = strip_prob_fun_suffix(fname);
474 if (!fun_name_exists(fname) // catch redefines later avoid fwd
475 && (fun_name_exists(dist_name + "_lpdf") ||
476 fun_name_exists(dist_name + "_lpmf") ||
477 fun_name_exists(dist_name + "_log"))) {
478 error_msgs << "Parse Error. Probability function already defined"
479 << " for " << dist_name << std::endl;
480 pass = false;
481 return;
482 }
483 }
484 if (has_cdf_suffix(fname)) {
485 std::string dist_name = strip_cdf_suffix(fname);
486 if (fun_name_exists(dist_name + "_cdf_log") ||
487 fun_name_exists(dist_name + "_lcdf")) {
488 error_msgs << " Parse Error. CDF already defined for " << dist_name
489 << std::endl;
490 pass = false;
491 return;
492 }
493 }
494 if (has_ccdf_suffix(fname)) {
495 std::string dist_name = strip_ccdf_suffix(fname);
496 if (fun_name_exists(dist_name + "_ccdf_log") ||
497 fun_name_exists(dist_name + "_lccdf")) {
498 error_msgs << " Parse Error. CCDF already defined for " << dist_name
499 << std::endl;
500 pass = false;
501 return;
502 }
503 }
504 }
505 boost::phoenix::function<validate_prob_fun> validate_prob_fun_f;
506
operator ()(const function_decl_def & decl,bool & pass,std::set<std::pair<std::string,function_signature_t>> & functions_declared,std::set<std::pair<std::string,function_signature_t>> & functions_defined,std::ostream & error_msgs) const507 void add_function_signature::operator()(
508 const function_decl_def &decl, bool &pass,
509 std::set<std::pair<std::string, function_signature_t>> &functions_declared,
510 std::set<std::pair<std::string, function_signature_t>> &functions_defined,
511 std::ostream &error_msgs) const {
512 std::vector<bare_expr_type> arg_types;
513 for (size_t i = 0; i < decl.arg_decls_.size(); ++i) {
514 arg_types.push_back(decl.arg_decls_[i].bare_type());
515 }
516 function_signature_t sig(decl.return_type_, arg_types);
517 std::pair<std::string, function_signature_t> name_sig(decl.name_, sig);
518
519 // check that not already declared if just declaration
520 if (decl.body_.is_no_op_statement() &&
521 fun_exists(functions_declared, name_sig)) {
522 error_msgs << "Parse Error. Function already declared, name="
523 << decl.name_;
524 pass = false;
525 return;
526 }
527
528 // check not already user defined
529 if (fun_exists(functions_defined, name_sig)) {
530 error_msgs << "Parse Error. Function already defined, name=" << decl.name_;
531 pass = false;
532 return;
533 }
534
535 // check not already system defined
536 if (!fun_exists(functions_declared, name_sig) &&
537 function_signatures::instance().is_defined(decl.name_, sig)) {
538 error_msgs << "Parse Error. Function system defined, name=" << decl.name_;
539 pass = false;
540 return;
541 }
542
543 // check argument type and qualifiers
544 if (!decl.body_.is_no_op_statement()) {
545 function_signature_t decl_sig =
546 function_signatures::instance().get_definition(decl.name_, sig);
547 if (!decl_sig.first.is_ill_formed_type()) {
548 for (size_t i = 0; i < decl.arg_decls_.size(); ++i) {
549 if (decl_sig.second[i] != arg_types[i] ||
550 decl_sig.second[i].is_data() != arg_types[i].is_data()) {
551 error_msgs << "Declaration doesn't match definition "
552 << "for function: " << decl.name_ << " argument "
553 << (i + 1) << ": argument declared as " << arg_types[i]
554 << ", defined as " << decl_sig.second[i] << "."
555 << std::endl;
556 pass = false;
557 return;
558 }
559 }
560 }
561 }
562
563 if (ends_with("_lpdf", decl.name_) &&
564 arg_types[0].innermost_type().is_int_type()) {
565 error_msgs << "Parse Error. Probability density functions require"
566 << " real variates (first argument)."
567 << " Found type = " << arg_types[0] << std::endl;
568 pass = false;
569 return;
570 }
571 if (ends_with("_lpmf", decl.name_) &&
572 !arg_types[0].innermost_type().is_int_type()) {
573 error_msgs << "Parse Error. Probability mass functions require"
574 << " integer variates (first argument)."
575 << " Found type = " << arg_types[0] << std::endl;
576 pass = false;
577 return;
578 }
579
580 // add declaration in local sets and in parser function sigs
581 if (functions_declared.find(name_sig) == functions_declared.end()) {
582 functions_declared.insert(name_sig);
583 function_signatures::instance().add(decl.name_, decl.return_type_,
584 arg_types);
585 function_signatures::instance().set_user_defined(name_sig);
586 }
587
588 // add as definition if there's a body
589 if (!decl.body_.is_no_op_statement())
590 functions_defined.insert(name_sig);
591 pass = true;
592 }
593 boost::phoenix::function<add_function_signature> add_function_signature_f;
594
operator ()(function_decl_def & decl,bool & pass,std::ostream & error_msgs) const595 void validate_pmf_pdf_variate::operator()(function_decl_def &decl, bool &pass,
596 std::ostream &error_msgs) const {
597 if (!has_prob_fun_suffix(decl.name_))
598 return;
599 if (decl.arg_decls_.size() == 0) {
600 error_msgs << "Parse Error. Probability functions require"
601 << " at least one argument." << std::endl;
602 pass = false;
603 return;
604 }
605 bare_expr_type variate_type = decl.arg_decls_[0].bare_type().innermost_type();
606 if (ends_with("_lpdf", decl.name_) && variate_type.is_int_type()) {
607 error_msgs << "Parse Error. Probability density functions require"
608 << " real variates (first argument)."
609 << " Found type = " << variate_type << std::endl;
610 pass = false;
611 return;
612 }
613 if (ends_with("_lpmf", decl.name_) && !variate_type.is_int_type()) {
614 error_msgs << "Parse Error. Probability mass functions require"
615 << " integer variates (first argument)."
616 << " Found type = " << variate_type << std::endl;
617 pass = false;
618 return;
619 }
620 }
621 boost::phoenix::function<validate_pmf_pdf_variate> validate_pmf_pdf_variate_f;
622
operator ()(function_decl_def & decl,bool & pass,std::ostream & error_msgs) const623 void validate_return_type::operator()(function_decl_def &decl, bool &pass,
624 std::ostream &error_msgs) const {
625 pass = decl.body_.is_no_op_statement() ||
626 stan::lang::returns_type(decl.return_type_, decl.body_, error_msgs);
627 if (!pass) {
628 error_msgs << "Improper return in body of function." << std::endl;
629 return;
630 }
631
632 if ((ends_with("_log", decl.name_) || ends_with("_lpdf", decl.name_) ||
633 ends_with("_lpmf", decl.name_) || ends_with("_lcdf", decl.name_) ||
634 ends_with("_lccdf", decl.name_)) &&
635 !decl.return_type_.is_double_type()) {
636 pass = false;
637 error_msgs << "Real return type required for probability functions"
638 << " ending in _log, _lpdf, _lpmf, _lcdf, or _lccdf."
639 << std::endl;
640 }
641 }
642 boost::phoenix::function<validate_return_type> validate_return_type_f;
643
operator ()(scope & var_scope,variable_map & vm) const644 void set_fun_params_scope::operator()(scope &var_scope,
645 variable_map &vm) const {
646 var_scope = scope(var_scope.program_block(), true);
647 // generated log_prob code has vector called "params_r__"
648 // hidden way to get unconstrained params from model
649 vm.add("params_r__", var_decl("params_r__", vector_type()), parameter_origin);
650 }
651 boost::phoenix::function<set_fun_params_scope> set_fun_params_scope_f;
652
operator ()(function_decl_def & decl,variable_map & vm) const653 void unscope_variables::operator()(function_decl_def &decl,
654 variable_map &vm) const {
655 vm.remove("params_r__");
656 for (size_t i = 0; i < decl.arg_decls_.size(); ++i)
657 vm.remove(decl.arg_decls_[i].name());
658 }
659 boost::phoenix::function<unscope_variables> unscope_variables_f;
660
operator ()(const var_decl & decl,const scope & scope,bool & pass,variable_map & vm,std::ostream & error_msgs) const661 void add_fun_arg_var::operator()(const var_decl &decl, const scope &scope,
662 bool &pass, variable_map &vm,
663 std::ostream &error_msgs) const {
664 if (vm.exists(decl.name())) {
665 pass = false;
666 error_msgs << "Duplicate declaration of variable, name=" << decl.name()
667 << "; attempt to redeclare as function argument"
668 << "; original declaration as ";
669 print_scope(error_msgs, vm.get_scope(decl.name()));
670 error_msgs << " variable." << std::endl;
671 return;
672 }
673 pass = true;
674 stan::lang::scope arg_scope(scope.program_block() == data_origin
675 ? data_origin
676 : function_argument_origin);
677 vm.add(decl.name(), decl, arg_scope);
678 }
679 boost::phoenix::function<add_fun_arg_var> add_fun_arg_var_f;
680
681 // TODO(carpenter): seems redundant; see if it can be removed
operator ()(omni_idx & val) const682 void set_omni_idx::operator()(omni_idx &val) const { val = omni_idx(); }
683 boost::phoenix::function<set_omni_idx> set_omni_idx_f;
684
operator ()(const expression & e,bool & pass) const685 void validate_int_expr_silent::operator()(const expression &e,
686 bool &pass) const {
687 pass = e.bare_type().is_int_type();
688 }
689 boost::phoenix::function<validate_int_expr_silent> validate_int_expr_silent_f;
690
operator ()(const expression & e,bool & pass,std::ostream & error_msgs) const691 void validate_ints_expression::operator()(const expression &e, bool &pass,
692 std::ostream &error_msgs) const {
693 if (!e.bare_type().innermost_type().is_int_type()) {
694 error_msgs << "Container index must be integer; found type="
695 << e.bare_type() << std::endl;
696 pass = false;
697 return;
698 }
699 if (e.bare_type().num_dims() > 1) {
700 // tests > 1 so that message is coherent because the single
701 // integer array tests don't print
702 error_msgs << "Index must be integer or 1D integer array;"
703 << " found number of dimensions=" << e.bare_type().num_dims()
704 << std::endl;
705 pass = false;
706 return;
707 }
708 if (e.bare_type().num_dims() == 0) {
709 // not an array expression, fail and backtrack
710 pass = false;
711 return;
712 }
713 pass = true;
714 }
715 boost::phoenix::function<validate_ints_expression> validate_ints_expression_f;
716
operator ()(variable_map & vm) const717 void add_params_var::operator()(variable_map &vm) const {
718 vm.add("params_r__", var_decl("params_r__", vector_type()),
719 parameter_origin); // acts like a parameter
720 }
721 boost::phoenix::function<add_params_var> add_params_var_f;
722
operator ()(variable_map & vm) const723 void remove_params_var::operator()(variable_map &vm) const {
724 vm.remove("params_r__");
725 }
726 boost::phoenix::function<remove_params_var> remove_params_var_f;
727
dump_program_line(size_t idx_errline,int offset,const std::string & origin_file,size_t origin_line,const io::program_reader & reader,const std::vector<std::string> & program_lines,std::stringstream & error_msgs)728 void dump_program_line(size_t idx_errline, int offset,
729 const std::string &origin_file, size_t origin_line,
730 const io::program_reader &reader,
731 const std::vector<std::string> &program_lines,
732 std::stringstream &error_msgs) {
733 boost::format fmt_lineno("%6d: ");
734 if (idx_errline + offset > 0 && idx_errline + offset < program_lines.size()) {
735 io::program_reader::trace_t trace = reader.trace(idx_errline + offset);
736 if (trace[trace.size() - 1].first == origin_file) {
737 std::string lineno = str(fmt_lineno % (origin_line + offset));
738 error_msgs << lineno << program_lines[idx_errline + offset - 1]
739 << std::endl;
740 }
741 }
742 }
743
operator ()(pos_iterator_t begin,pos_iterator_t end,pos_iterator_t where,variable_map & vm,std::stringstream & error_msgs,const io::program_reader & reader) const744 void program_error::operator()(pos_iterator_t begin, pos_iterator_t end,
745 pos_iterator_t where, variable_map &vm,
746 std::stringstream &error_msgs,
747 const io::program_reader &reader) const {
748 // extract line and column of error
749 size_t idx_errline = boost::spirit::get_line(where);
750 if (idx_errline == 0) {
751 error_msgs << "Error before start of program." << std::endl;
752 return;
753 }
754 size_t idx_errcol = 0;
755 idx_errcol = get_column(begin, where) - 1;
756
757 // extract lines of included program
758 std::basic_stringstream<char> program_ss;
759 program_ss << boost::make_iterator_range(begin, end);
760 std::vector<std::string> program_lines;
761 while (!program_ss.eof()) {
762 std::string line;
763 std::getline(program_ss, line);
764 program_lines.push_back(line);
765 }
766
767 // dump include trace for error line
768 io::program_reader::trace_t trace = reader.trace(idx_errline);
769 std::string origin_file = trace[trace.size() - 1].first;
770 size_t origin_line = trace[trace.size() - 1].second;
771 error_msgs << " error in '" << trace[trace.size() - 1].first << "' at line "
772 << trace[trace.size() - 1].second << ", column " << idx_errcol
773 << std::endl;
774 for (int i = trace.size() - 1; i-- > 0;)
775 error_msgs << " included from '" << trace[i].first << "' at line "
776 << trace[i].second << std::endl;
777
778 // dump context of error
779 error_msgs << " -------------------------------------------------"
780 << std::endl;
781
782 dump_program_line(idx_errline, -2, origin_file, origin_line, reader,
783 program_lines, error_msgs);
784 dump_program_line(idx_errline, -1, origin_file, origin_line, reader,
785 program_lines, error_msgs);
786 dump_program_line(idx_errline, 0, origin_file, origin_line, reader,
787 program_lines, error_msgs);
788 error_msgs << std::setw(idx_errcol + 8) << "^" << std::endl;
789 dump_program_line(idx_errline, +1, origin_file, origin_line, reader,
790 program_lines, error_msgs);
791
792 error_msgs << " -------------------------------------------------"
793 << std::endl
794 << std::endl;
795 }
796 boost::phoenix::function<program_error> program_error_f;
797
798 void add_conditional_condition::
operator ()(conditional_statement & cs,const expression & e,bool & pass,std::stringstream & error_msgs) const799 operator()(conditional_statement &cs, const expression &e, bool &pass,
800 std::stringstream &error_msgs) const {
801 if (!e.bare_type().is_primitive()) {
802 error_msgs << "Conditions in if-else statement must be"
803 << " primitive int or real;"
804 << " found type=" << e.bare_type() << std::endl;
805 pass = false;
806 return;
807 }
808 cs.conditions_.push_back(e);
809 pass = true;
810 return;
811 }
812 boost::phoenix::function<add_conditional_condition> add_conditional_condition_f;
813
operator ()(conditional_statement & cs,const statement & s) const814 void add_conditional_body::operator()(conditional_statement &cs,
815 const statement &s) const {
816 cs.bodies_.push_back(s);
817 }
818 boost::phoenix::function<add_conditional_body> add_conditional_body_f;
819
operator ()(std::string & op,std::ostream & error_msgs) const820 void deprecate_old_assignment_op::operator()(std::string &op,
821 std::ostream &error_msgs) const {
822 error_msgs << "Info: assignment operator <- deprecated"
823 << " in the Stan language;"
824 << " use = instead." << std::endl;
825 std::string eq("=");
826 op = eq;
827 }
828 boost::phoenix::function<deprecate_old_assignment_op>
829 deprecate_old_assignment_op_f;
830
operator ()(scope var_scope,bool & pass,std::ostream & error_msgs) const831 void non_void_return_msg::operator()(scope var_scope, bool &pass,
832 std::ostream &error_msgs) const {
833 pass = false;
834 if (var_scope.non_void_fun()) {
835 error_msgs << "Non-void function must return expression"
836 << " of specified return type." << std::endl;
837 return;
838 }
839 error_msgs << "Return statement only allowed from function bodies."
840 << std::endl;
841 }
842 boost::phoenix::function<non_void_return_msg> non_void_return_msg_f;
843
operator ()(scope var_scope,bool & pass,std::ostream & error_msgs) const844 void validate_return_allowed::operator()(scope var_scope, bool &pass,
845 std::ostream &error_msgs) const {
846 if (var_scope.void_fun()) {
847 error_msgs << "Void function cannot return a value." << std::endl;
848 pass = false;
849 return;
850 }
851 if (!var_scope.non_void_fun()) {
852 error_msgs << "Returns only allowed from function bodies." << std::endl;
853 pass = false;
854 return;
855 }
856 pass = true;
857 }
858 boost::phoenix::function<validate_return_allowed> validate_return_allowed_f;
859
operator ()(scope var_scope,bool & pass,std::ostream & error_msgs) const860 void validate_void_return_allowed::operator()(scope var_scope, bool &pass,
861 std::ostream &error_msgs) const {
862 if (!var_scope.void_fun()) {
863 error_msgs << "Void returns only allowed from function"
864 << " bodies of void return type." << std::endl;
865 pass = false;
866 return;
867 }
868 pass = true;
869 }
870 boost::phoenix::function<validate_void_return_allowed>
871 validate_void_return_allowed_f;
872
operator ()(assgn & a,const scope & var_scope,bool & pass,const variable_map & vm,std::ostream & error_msgs) const873 void validate_lhs_var_assgn::operator()(assgn &a, const scope &var_scope,
874 bool &pass, const variable_map &vm,
875 std::ostream &error_msgs) const {
876 std::string name(a.lhs_var_.name_);
877 if (!can_assign_to_lhs_var(name, var_scope, vm, error_msgs)) {
878 pass = false;
879 return;
880 }
881 a.lhs_var_.set_type(vm.get_bare_type(name));
882 }
883 boost::phoenix::function<validate_lhs_var_assgn> validate_lhs_var_assgn_f;
884
operator ()(assgn & a,const std::string & name,bool & pass,const variable_map & vm) const885 void set_lhs_var_assgn::operator()(assgn &a, const std::string &name,
886 bool &pass, const variable_map &vm) const {
887 if (!vm.exists(name)) {
888 pass = false;
889 return;
890 }
891 a.lhs_var_ = variable(name);
892 a.lhs_var_.set_type(vm.get_bare_type(name));
893 pass = true;
894 }
895 boost::phoenix::function<set_lhs_var_assgn> set_lhs_var_assgn_f;
896
operator ()(assgn & a,bool & pass,const variable_map & vm,std::ostream & error_msgs) const897 void validate_assgn::operator()(assgn &a, bool &pass, const variable_map &vm,
898 std::ostream &error_msgs) const {
899 // validate lhs name + idxs
900 std::string name = a.lhs_var_.name_;
901 expression lhs_expr = expression(a.lhs_var_);
902 bare_expr_type lhs_type = indexed_type(lhs_expr, a.idxs_);
903
904 if (lhs_type.is_ill_formed_type()) {
905 error_msgs << "Left-hand side indexing incompatible with variable."
906 << std::endl;
907 pass = false;
908 return;
909 }
910 if (a.is_simple_assignment()) {
911 if (!has_same_shape(lhs_type, a.rhs_, name, "assignment", error_msgs)) {
912 pass = false;
913 return;
914 }
915 pass = true;
916 return;
917 } else {
918 // compound operator-assignment
919 std::string op_equals = a.op_;
920 a.op_ = op_equals.substr(0, op_equals.size() - 1);
921
922 if (lhs_type.array_dims() > 0) {
923 error_msgs << "Cannot apply operator '" << op_equals
924 << "' to array variable; variable name = " << name << ".";
925 error_msgs << std::endl;
926 pass = false;
927 return;
928 }
929
930 bare_expr_type rhs_type = a.rhs_.bare_type();
931 if (lhs_type.is_primitive() && boost::algorithm::starts_with(a.op_, ".")) {
932 error_msgs << "Cannot apply element-wise operation to scalar"
933 << "; compound operator is: " << op_equals << std::endl;
934 pass = false;
935 return;
936 }
937 if (lhs_type.is_primitive() && rhs_type.is_primitive() &&
938 (lhs_type.innermost_type().is_double_type() || lhs_type == rhs_type)) {
939 pass = true;
940 return;
941 }
942
943 std::string op_name;
944 if (a.op_ == "+") {
945 op_name = "add";
946 } else if (a.op_ == "-") {
947 op_name = "subtract";
948 } else if (a.op_ == "*") {
949 op_name = "multiply";
950 } else if (a.op_ == "/") {
951 op_name = "divide";
952 } else if (a.op_ == "./") {
953 op_name = "elt_divide";
954 } else if (a.op_ == ".*") {
955 op_name = "elt_multiply";
956 }
957 // check that "lhs <op> rhs" is valid stan::math function sig
958 std::vector<bare_expr_type> arg_types;
959 arg_types.push_back(bare_expr_type(lhs_type));
960 arg_types.push_back(bare_expr_type(rhs_type));
961 function_signature_t op_equals_sig(lhs_type, arg_types);
962 if (!function_signatures::instance().is_defined(op_name, op_equals_sig)) {
963 error_msgs << "Cannot apply operator '" << op_equals << "' to operands;"
964 << " left-hand side type = " << lhs_type
965 << "; right-hand side type=" << rhs_type << std::endl;
966 pass = false;
967 return;
968 }
969 a.op_name_ = op_name;
970 pass = true;
971 }
972 }
973 boost::phoenix::function<validate_assgn> validate_assgn_f;
974
operator ()(sample & s,const variable_map & var_map,bool & pass,std::ostream & error_msgs) const975 void validate_sample::operator()(sample &s, const variable_map &var_map,
976 bool &pass, std::ostream &error_msgs) const {
977 std::vector<bare_expr_type> arg_types;
978 arg_types.push_back(s.expr_.bare_type());
979 for (size_t i = 0; i < s.dist_.args_.size(); ++i) {
980 arg_types.push_back(s.dist_.args_[i].bare_type());
981 }
982
983 std::string function_name(s.dist_.family_);
984 std::string internal_function_name = get_prob_fun(function_name);
985 s.is_discrete_ = function_signatures::instance().discrete_first_arg(
986 internal_function_name);
987
988 if (internal_function_name.size() == 0) {
989 pass = false;
990 error_msgs << "Unknown distribution name: " << function_name << std::endl;
991 return;
992 }
993
994 if (!(ends_with("_lpdf", internal_function_name) ||
995 ends_with("_lpmf", internal_function_name) ||
996 ends_with("_log", internal_function_name))) {
997 pass = false;
998 error_msgs << "Probability function must end in _lpdf or _lpmf."
999 << " Found distribution family = " << function_name
1000 << " with no corresponding probability function"
1001 << " " << function_name << "_lpdf"
1002 << ", " << function_name << "_lpmf"
1003 << ", or " << function_name << "_log" << std::endl;
1004 return;
1005 }
1006
1007 if ((internal_function_name.find("multiply_log") != std::string::npos) ||
1008 (internal_function_name.find("binomial_coefficient_log") !=
1009 std::string::npos)) {
1010 error_msgs << "Only distribution names can be used with"
1011 << " sampling (~) notation; found non-distribution"
1012 << " function: " << function_name << std::endl;
1013 pass = false;
1014 return;
1015 }
1016
1017 if (internal_function_name.find("cdf_log") != std::string::npos) {
1018 error_msgs << "CDF and CCDF functions may not be used with"
1019 << " sampling notation."
1020 << " Use increment_log_prob(" << internal_function_name
1021 << "(...)) instead." << std::endl;
1022 pass = false;
1023 return;
1024 }
1025
1026 if (internal_function_name == "lkj_cov_log") {
1027 error_msgs << "Info: the lkj_cov_log() sampling distribution"
1028 << " is deprecated. It will be removed in Stan 3." << std::endl
1029 << "Code LKJ covariance in terms of an lkj_corr()"
1030 << " distribution on a correlation matrix"
1031 << " and independent lognormals on the scales." << std::endl
1032 << std::endl;
1033 }
1034
1035 if (!is_double_return(internal_function_name, arg_types, error_msgs)) {
1036 error_msgs << "Real return type required for probability function."
1037 << std::endl;
1038 pass = false;
1039 return;
1040 }
1041 // test for LHS not being purely a variable
1042 if (has_non_param_var(s.expr_, var_map)) {
1043 error_msgs << "Info:" << std::endl
1044 << "Left-hand side of sampling statement (~) may contain a"
1045 << " non-linear transform of a parameter or local variable."
1046 << std::endl
1047 << "If it does, you need to include a target += statement"
1048 << " with the log absolute determinant of the Jacobian of"
1049 << " the transform." << std::endl
1050 << "Left-hand-side of sampling statement:" << std::endl
1051 << " " << s.expr_.to_string() << " ~ " << function_name
1052 << "(...)" << std::endl;
1053 }
1054 // validate that variable and params are univariate if truncated
1055 if (s.truncation_.has_low() || s.truncation_.has_high()) {
1056 if (!is_univariate(s.expr_.bare_type())) {
1057 error_msgs << "Outcomes in truncated distributions"
1058 << " must be univariate." << std::endl
1059 << " Found outcome expression: " << s.expr_.to_string()
1060 << std::endl
1061 << " with non-univariate type: " << s.expr_.bare_type()
1062 << std::endl;
1063 pass = false;
1064 return;
1065 }
1066 for (size_t i = 0; i < s.dist_.args_.size(); ++i)
1067 if (!is_univariate(s.dist_.args_[i].bare_type())) {
1068 error_msgs << "Parameters in truncated distributions"
1069 << " must be univariate." << std::endl
1070 << " Found parameter expression: "
1071 << s.dist_.args_[i].to_string() << std::endl
1072 << " with non-univariate type: "
1073 << s.dist_.args_[i].bare_type() << std::endl;
1074 pass = false;
1075 return;
1076 }
1077 }
1078 if (s.truncation_.has_low() &&
1079 !is_univariate(s.truncation_.low_.bare_type())) {
1080 error_msgs << "Lower bounds in truncated distributions"
1081 << " must be univariate." << std::endl
1082 << " Found lower bound expression: "
1083 << s.truncation_.low_.to_string() << std::endl
1084 << " with non-univariate type: "
1085 << s.truncation_.low_.bare_type() << std::endl;
1086 pass = false;
1087 return;
1088 }
1089 if (s.truncation_.has_high() &&
1090 !is_univariate(s.truncation_.high_.bare_type())) {
1091 error_msgs << "Upper bounds in truncated distributions"
1092 << " must be univariate." << std::endl
1093 << " Found upper bound expression: "
1094 << s.truncation_.high_.to_string() << std::endl
1095 << " with non-univariate type: "
1096 << s.truncation_.high_.bare_type() << std::endl;
1097 pass = false;
1098 return;
1099 }
1100
1101 // make sure CDFs or CCDFs exist with conforming signature
1102 // T[L, ]
1103 if (s.truncation_.has_low() && !s.truncation_.has_high()) {
1104 std::vector<bare_expr_type> arg_types_trunc(arg_types);
1105 arg_types_trunc[0] = s.truncation_.low_.bare_type();
1106 std::string function_name_ccdf = get_ccdf(s.dist_.family_);
1107 if (function_name_ccdf == s.dist_.family_ ||
1108 !is_double_return(function_name_ccdf, arg_types_trunc, error_msgs)) {
1109 error_msgs << "Lower truncation not defined for specified"
1110 << " arguments to " << s.dist_.family_ << std::endl;
1111 pass = false;
1112 return;
1113 }
1114 if (!is_double_return(function_name_ccdf, arg_types, error_msgs)) {
1115 error_msgs << "Lower bound in truncation type does not match"
1116 << " sampled variate in distribution's type" << std::endl;
1117 pass = false;
1118 return;
1119 }
1120 }
1121 // T[, H]
1122 if (!s.truncation_.has_low() && s.truncation_.has_high()) {
1123 std::vector<bare_expr_type> arg_types_trunc(arg_types);
1124 arg_types_trunc[0] = s.truncation_.high_.bare_type();
1125 std::string function_name_cdf = get_cdf(s.dist_.family_);
1126 if (function_name_cdf == s.dist_.family_ ||
1127 !is_double_return(function_name_cdf, arg_types_trunc, error_msgs)) {
1128 error_msgs << "Upper truncation not defined for"
1129 << " specified arguments to " << s.dist_.family_ << std::endl;
1130
1131 pass = false;
1132 return;
1133 }
1134 if (!is_double_return(function_name_cdf, arg_types, error_msgs)) {
1135 error_msgs << "Upper bound in truncation type does not match"
1136 << " sampled variate in distribution's type" << std::endl;
1137 pass = false;
1138 return;
1139 }
1140 }
1141 // T[L, H]
1142 if (s.truncation_.has_low() && s.truncation_.has_high()) {
1143 std::vector<bare_expr_type> arg_types_trunc(arg_types);
1144 arg_types_trunc[0] = s.truncation_.low_.bare_type();
1145 std::string function_name_cdf = get_cdf(s.dist_.family_);
1146 if (function_name_cdf == s.dist_.family_ ||
1147 !is_double_return(function_name_cdf, arg_types_trunc, error_msgs)) {
1148 error_msgs << "Lower truncation not defined for specified"
1149 << " arguments to " << s.dist_.family_ << std::endl;
1150 pass = false;
1151 return;
1152 }
1153 if (!is_double_return(function_name_cdf, arg_types, error_msgs)) {
1154 error_msgs << "Lower bound in truncation type does not match"
1155 << " sampled variate in distribution's type" << std::endl;
1156 pass = false;
1157 return;
1158 }
1159 }
1160 pass = true;
1161 }
1162 boost::phoenix::function<validate_sample> validate_sample_f;
1163
operator ()(bool & pass,const stan::lang::expression & expr,std::stringstream & error_msgs) const1164 void expression_as_statement::operator()(bool &pass,
1165 const stan::lang::expression &expr,
1166 std::stringstream &error_msgs) const {
1167 if (!(expr.bare_type().is_void_type())) {
1168 error_msgs << "Illegal statement beginning with non-void"
1169 << " expression parsed as" << std::endl
1170 << " " << expr.to_string() << std::endl
1171 << "Not a legal assignment, sampling, or function"
1172 << " statement. Note that" << std::endl
1173 << " * Assignment statements only allow variables"
1174 << " (with optional indexes) on the left;" << std::endl
1175 << " * Sampling statements allow arbitrary"
1176 << " value-denoting expressions on the left." << std::endl
1177 << " * Functions used as statements must be"
1178 << " declared to have void returns" << std::endl
1179 << std::endl;
1180 pass = false;
1181 return;
1182 }
1183 pass = true;
1184 }
1185 boost::phoenix::function<expression_as_statement> expression_as_statement_f;
1186
operator ()(const std::vector<local_var_decl> & var_decls,variable_map & vm) const1187 void unscope_locals::operator()(const std::vector<local_var_decl> &var_decls,
1188 variable_map &vm) const {
1189 for (size_t i = 0; i < var_decls.size(); ++i)
1190 vm.remove(var_decls[i].name());
1191 }
1192 boost::phoenix::function<unscope_locals> unscope_locals_f;
1193
operator ()(while_statement & ws,const expression & e,bool & pass,std::stringstream & error_msgs) const1194 void add_while_condition::operator()(while_statement &ws, const expression &e,
1195 bool &pass,
1196 std::stringstream &error_msgs) const {
1197 pass = e.bare_type().is_primitive();
1198 if (!pass) {
1199 error_msgs << "Conditions in while statement must be primitive"
1200 << " int or real;"
1201 << " found type=" << e.bare_type() << std::endl;
1202 return;
1203 }
1204 ws.condition_ = e;
1205 }
1206 boost::phoenix::function<add_while_condition> add_while_condition_f;
1207
operator ()(while_statement & ws,const statement & s) const1208 void add_while_body::operator()(while_statement &ws, const statement &s) const {
1209 ws.body_ = s;
1210 }
1211 boost::phoenix::function<add_while_body> add_while_body_f;
1212
operator ()(const std::string & name,const scope & var_scope,variable_map & vm) const1213 void add_loop_identifier::operator()(const std::string &name,
1214 const scope &var_scope,
1215 variable_map &vm) const {
1216 vm.add(name, var_decl(name, int_type()), scope(loop_identifier_origin, true));
1217 }
1218 boost::phoenix::function<add_loop_identifier> add_loop_identifier_f;
1219
operator ()(const stan::lang::expression & expr,std::string & name,const scope & var_scope,bool & pass,variable_map & vm) const1220 void add_array_loop_identifier ::operator()(const stan::lang::expression &expr,
1221 std::string &name,
1222 const scope &var_scope, bool &pass,
1223 variable_map &vm) const {
1224 pass = expr.bare_type().is_array_type();
1225 if (pass)
1226 vm.add(name, var_decl(name, expr.bare_type().array_element_type()),
1227 scope(loop_identifier_origin, true));
1228 }
1229 boost::phoenix::function<add_array_loop_identifier> add_array_loop_identifier_f;
1230
1231 void add_matrix_loop_identifier ::
operator ()(const stan::lang::expression & expr,std::string & name,const scope & var_scope,bool & pass,variable_map & vm,std::stringstream & error_msgs) const1232 operator()(const stan::lang::expression &expr, std::string &name,
1233 const scope &var_scope, bool &pass, variable_map &vm,
1234 std::stringstream &error_msgs) const {
1235 pass = expr.bare_type().num_dims() > 0 && !(expr.bare_type().is_array_type());
1236 if (!pass) {
1237 error_msgs << "Loop must be over container or range." << std::endl;
1238 return;
1239 } else {
1240 vm.add(name, var_decl(name, double_type()),
1241 scope(loop_identifier_origin, true));
1242 pass = true;
1243 }
1244 }
1245 boost::phoenix::function<add_matrix_loop_identifier>
1246 add_matrix_loop_identifier_f;
1247
operator ()(const std::string & name,std::string & name_local,bool & pass,variable_map & vm,std::stringstream & error_msgs) const1248 void store_loop_identifier::operator()(const std::string &name,
1249 std::string &name_local, bool &pass,
1250 variable_map &vm,
1251 std::stringstream &error_msgs) const {
1252 pass = !(vm.exists(name));
1253 if (!pass) {
1254 // avoid repeated error message due to backtracking
1255 if (error_msgs.str().find("Loop variable already declared.") ==
1256 std::string::npos)
1257 error_msgs << "Loop variable already declared."
1258 << " variable name=\"" << name << "\"" << std::endl;
1259 } else {
1260 name_local = name;
1261 }
1262 }
1263 boost::phoenix::function<store_loop_identifier> store_loop_identifier_f;
1264
operator ()(const std::string & name,variable_map & vm) const1265 void remove_loop_identifier::operator()(const std::string &name,
1266 variable_map &vm) const {
1267 vm.remove(name);
1268 }
1269 boost::phoenix::function<remove_loop_identifier> remove_loop_identifier_f;
1270
operator ()(const expression & expr,bool & pass,std::stringstream & error_msgs) const1271 void validate_int_expr::operator()(const expression &expr, bool &pass,
1272 std::stringstream &error_msgs) const {
1273 if (!expr.bare_type().is_int_type()) {
1274 error_msgs << "Expression denoting integer required; found type="
1275 << expr.bare_type() << std::endl;
1276 pass = false;
1277 return;
1278 }
1279 pass = true;
1280 }
1281 boost::phoenix::function<validate_int_expr> validate_int_expr_f;
1282
1283 void deprecate_increment_log_prob::
operator ()(std::stringstream & error_msgs) const1284 operator()(std::stringstream &error_msgs) const {
1285 error_msgs << "Info: increment_log_prob(...);"
1286 << " is deprecated and will be removed in the future." << std::endl
1287 << " Use target += ...; instead." << std::endl;
1288 }
1289 boost::phoenix::function<deprecate_increment_log_prob>
1290 deprecate_increment_log_prob_f;
1291
operator ()(const scope & var_scope,bool & pass,std::stringstream & error_msgs) const1292 void validate_allow_sample::operator()(const scope &var_scope, bool &pass,
1293 std::stringstream &error_msgs) const {
1294 pass = var_scope.allows_sampling();
1295 if (!pass)
1296 error_msgs << "Sampling statements (~) and increment_log_prob() are"
1297 << std::endl
1298 << "only allowed in the model block or lp functions."
1299 << std::endl;
1300 }
1301 boost::phoenix::function<validate_allow_sample> validate_allow_sample_f;
1302
operator ()(const expression & e,bool & pass,std::ostream & error_msgs) const1303 void validate_non_void_expression::operator()(const expression &e, bool &pass,
1304 std::ostream &error_msgs) const {
1305 pass = !e.bare_type().is_void_type();
1306 if (!pass)
1307 error_msgs << "Attempt to increment log prob with void expression"
1308 << std::endl;
1309 }
1310 boost::phoenix::function<validate_non_void_expression>
1311 validate_non_void_expression_f;
1312
operator ()(double_literal & lit,const pos_iterator_t & begin,const pos_iterator_t & end) const1313 void add_literal_string::operator()(double_literal &lit,
1314 const pos_iterator_t &begin,
1315 const pos_iterator_t &end) const {
1316 lit.string_ = std::string(begin, end);
1317 }
1318 boost::phoenix::function<add_literal_string> add_literal_string_f;
1319
1320 template <typename T, typename I>
operator ()(T & line,const I & begin,const I & end) const1321 void add_line_number::operator()(T &line, const I &begin, const I &end) const {
1322 line.begin_line_ = get_line(begin);
1323 line.end_line_ = get_line(end);
1324 }
1325 boost::phoenix::function<add_line_number> add_line_number_f;
1326
1327 template void add_line_number::operator()(block_var_decl &,
1328 const pos_iterator_t &begin,
1329 const pos_iterator_t &end) const;
1330
1331 template void add_line_number::operator()(local_var_decl &,
1332 const pos_iterator_t &begin,
1333 const pos_iterator_t &end) const;
1334
1335 template void add_line_number::operator()(statement &,
1336 const pos_iterator_t &begin,
1337 const pos_iterator_t &end) const;
1338
operator ()(return_statement & s) const1339 void set_void_return::operator()(return_statement &s) const {
1340 s = return_statement();
1341 }
1342 boost::phoenix::function<set_void_return> set_void_return_f;
1343
operator ()(no_op_statement & s) const1344 void set_no_op::operator()(no_op_statement &s) const { s = no_op_statement(); }
1345 boost::phoenix::function<set_no_op> set_no_op_f;
1346
operator ()(std::ostream & error_msgs) const1347 void deprecated_integrate_ode::operator()(std::ostream &error_msgs) const {
1348 error_msgs << "Info: the integrate_ode() function is deprecated"
1349 << " in the Stan language; use the following functions"
1350 << " instead.\n"
1351 << " integrate_ode_rk45()"
1352 << " [explicit, order 5, for non-stiff problems]\n"
1353 << " integrate_ode_adams()"
1354 << " [implicit, up to order 12, for non-stiff problems]\n"
1355 << " integrate_ode_bdf()"
1356 << " [implicit, up to order 5, for stiff problems]." << std::endl;
1357 }
1358 boost::phoenix::function<deprecated_integrate_ode> deprecated_integrate_ode_f;
1359
1360 template <class T>
validate_integrate_ode_non_control_args(const T & ode_fun,const variable_map & var_map,bool & pass,std::ostream & error_msgs)1361 void validate_integrate_ode_non_control_args(const T &ode_fun,
1362 const variable_map &var_map,
1363 bool &pass,
1364 std::ostream &error_msgs) {
1365 pass = true;
1366
1367 // ode_integrator requires function with signature
1368 // (real, real[ ], real[ ], data real[ ], data int[ ]): real[ ]"
1369
1370 // TODO(mitzi) names indicate status, but not flagged as data_only
1371 // instantiate ode fn arg types
1372 double_type t_double;
1373 bare_expr_type t_ar_double(bare_array_type(t_double, 1));
1374 bare_expr_type t_ar_double_data(bare_array_type(t_double, 1));
1375
1376 int_type t_int_data;
1377 bare_expr_type t_ar_int_data(bare_array_type(t_int_data, 1));
1378
1379 // validate ode fn signature
1380 bare_expr_type sys_result_type(t_ar_double);
1381 std::vector<bare_expr_type> sys_arg_types;
1382 sys_arg_types.push_back(t_double);
1383 sys_arg_types.push_back(t_ar_double);
1384 sys_arg_types.push_back(t_ar_double);
1385 sys_arg_types.push_back(t_ar_double_data);
1386 sys_arg_types.push_back(t_ar_int_data);
1387 function_signature_t system_signature(sys_result_type, sys_arg_types);
1388 if (!function_signatures::instance().is_defined(ode_fun.system_function_name_,
1389 system_signature)) {
1390 error_msgs << "Wrong signature for function "
1391 << ode_fun.integration_function_name_
1392 << "; first argument must be "
1393 << "the name of a function with signature"
1394 << " (real, real[ ], real[ ], data real[ ], data int[ ]):"
1395 << " real[ ]." << std::endl;
1396 pass = false;
1397 }
1398
1399 // Stan lang integrate_ode takes 7 args:
1400 // fn_name, y0, t0, ts, theta, x_r, x_i
1401 // only y0 and theta can have params
1402 if (ode_fun.y0_.bare_type() != t_ar_double) {
1403 error_msgs << "Second argument to " << ode_fun.integration_function_name_
1404 << " must have type real[ ]"
1405 << "; found type = " << ode_fun.y0_.bare_type() << ". "
1406 << std::endl;
1407 pass = false;
1408 }
1409 if (!ode_fun.t0_.bare_type().is_primitive()) {
1410 error_msgs << "Third argument to " << ode_fun.integration_function_name_
1411 << " must have type real"
1412 << "; found type = " << ode_fun.t0_.bare_type() << ". "
1413 << std::endl;
1414 pass = false;
1415 }
1416 if (ode_fun.ts_.bare_type() != t_ar_double) {
1417 error_msgs << "Fourth argument to " << ode_fun.integration_function_name_
1418 << " must have type real[ ]"
1419 << "; found type = " << ode_fun.ts_.bare_type() << ". "
1420 << std::endl;
1421 pass = false;
1422 }
1423 if (ode_fun.theta_.bare_type() != t_ar_double) {
1424 error_msgs << "Fifth argument to " << ode_fun.integration_function_name_
1425 << " must have type real[ ]"
1426 << "; found type = " << ode_fun.theta_.bare_type() << ". "
1427 << std::endl;
1428 pass = false;
1429 }
1430 if (ode_fun.x_.bare_type() != t_ar_double_data) {
1431 error_msgs << "Sixth argument to " << ode_fun.integration_function_name_
1432 << " must have type data real[ ]"
1433 << "; found type = " << ode_fun.x_.bare_type() << ". "
1434 << std::endl;
1435 pass = false;
1436 }
1437 if (ode_fun.x_int_.bare_type() != t_ar_int_data) {
1438 error_msgs << "Seventh argument to " << ode_fun.integration_function_name_
1439 << " must have type data int[ ]"
1440 << "; found type = " << ode_fun.x_int_.bare_type() << ". "
1441 << std::endl;
1442 pass = false;
1443 }
1444
1445 // test data-only variables do not have parameters (int locals OK)
1446 if (has_var(ode_fun.t0_, var_map)) {
1447 error_msgs << "Third argument to " << ode_fun.integration_function_name_
1448 << " (initial times)"
1449 << " must be data only and not reference parameters."
1450 << std::endl;
1451 pass = false;
1452 }
1453 if (has_var(ode_fun.ts_, var_map)) {
1454 error_msgs << "Fourth argument to " << ode_fun.integration_function_name_
1455 << " (solution times)"
1456 << " must be data only and not reference parameters."
1457 << std::endl;
1458 pass = false;
1459 }
1460 if (has_var(ode_fun.x_, var_map)) {
1461 error_msgs << "Sixth argument to " << ode_fun.integration_function_name_
1462 << " (real data)"
1463 << " must be data only and not reference parameters."
1464 << std::endl;
1465 pass = false;
1466 }
1467 }
1468
operator ()(const integrate_ode & ode_fun,const variable_map & var_map,bool & pass,std::ostream & error_msgs) const1469 void validate_integrate_ode::operator()(const integrate_ode &ode_fun,
1470 const variable_map &var_map, bool &pass,
1471 std::ostream &error_msgs) const {
1472 validate_integrate_ode_non_control_args(ode_fun, var_map, pass, error_msgs);
1473 }
1474 boost::phoenix::function<validate_integrate_ode> validate_integrate_ode_f;
1475
1476 void validate_integrate_ode_control::
operator ()(const integrate_ode_control & ode_fun,const variable_map & var_map,bool & pass,std::ostream & error_msgs) const1477 operator()(const integrate_ode_control &ode_fun, const variable_map &var_map,
1478 bool &pass, std::ostream &error_msgs) const {
1479 validate_integrate_ode_non_control_args(ode_fun, var_map, pass, error_msgs);
1480 if (!ode_fun.rel_tol_.bare_type().is_primitive()) {
1481 error_msgs << "Eighth argument to " << ode_fun.integration_function_name_
1482 << " (relative tolerance) must have type real or int;"
1483 << " found type=" << ode_fun.rel_tol_.bare_type() << ". ";
1484 pass = false;
1485 }
1486 if (!ode_fun.abs_tol_.bare_type().is_primitive()) {
1487 error_msgs << "Ninth argument to " << ode_fun.integration_function_name_
1488 << " (absolute tolerance) must have type real or int;"
1489 << " found type=" << ode_fun.abs_tol_.bare_type() << ". ";
1490 pass = false;
1491 }
1492 if (!ode_fun.max_num_steps_.bare_type().is_primitive()) {
1493 error_msgs << "Tenth argument to " << ode_fun.integration_function_name_
1494 << " (max steps) must have type real or int;"
1495 << " found type=" << ode_fun.max_num_steps_.bare_type() << ". ";
1496 pass = false;
1497 }
1498
1499 // test data-only variables do not have parameters (int locals OK)
1500 if (has_var(ode_fun.rel_tol_, var_map)) {
1501 error_msgs << "Eighth argument to " << ode_fun.integration_function_name_
1502 << " (relative tolerance) must be data only"
1503 << " and not depend on parameters.";
1504 pass = false;
1505 }
1506 if (has_var(ode_fun.abs_tol_, var_map)) {
1507 error_msgs << "Ninth argument to " << ode_fun.integration_function_name_
1508 << " (absolute tolerance ) must be data only"
1509 << " and not depend parameters.";
1510 pass = false;
1511 }
1512 if (has_var(ode_fun.max_num_steps_, var_map)) {
1513 error_msgs << "Tenth argument to " << ode_fun.integration_function_name_
1514 << " (max steps) must be data only"
1515 << " and not depend on parameters.";
1516 pass = false;
1517 }
1518 }
1519 boost::phoenix::function<validate_integrate_ode_control>
1520 validate_integrate_ode_control_f;
1521
1522 template <class T>
validate_algebra_solver_non_control_args(const T & alg_fun,const variable_map & var_map,bool & pass,std::ostream & error_msgs)1523 void validate_algebra_solver_non_control_args(const T &alg_fun,
1524 const variable_map &var_map,
1525 bool &pass,
1526 std::ostream &error_msgs) {
1527 pass = true;
1528 int_type t_int;
1529 double_type t_double;
1530 vector_type t_vector;
1531 bare_expr_type t_ar_int(bare_array_type(t_int, 1));
1532 bare_expr_type t_ar_double(bare_array_type(t_double, 1));
1533
1534 bare_expr_type sys_result_type(t_vector);
1535 std::vector<bare_expr_type> sys_arg_types;
1536 sys_arg_types.push_back(t_vector); // y
1537 sys_arg_types.push_back(t_vector); // theta
1538 sys_arg_types.push_back(t_ar_double); // x_r
1539 sys_arg_types.push_back(t_ar_int); // x_i
1540 function_signature_t system_signature(sys_result_type, sys_arg_types);
1541
1542 if (!function_signatures::instance().is_defined(alg_fun.system_function_name_,
1543 system_signature)) {
1544 error_msgs << "Wrong signature for function "
1545 << alg_fun.system_function_name_
1546 << "; first argument to algebra_solver"
1547 << " must be a function with signature"
1548 << " (vector, vector, real[ ], int[ ]) : vector." << std::endl;
1549 pass = false;
1550 }
1551
1552 // check solver function arg types
1553 if (alg_fun.y_.bare_type() != t_vector) {
1554 error_msgs << "Second argument to algebra_solver must have type vector"
1555 << "; found type= " << alg_fun.y_.bare_type() << ". "
1556 << std::endl;
1557 pass = false;
1558 }
1559 if (alg_fun.theta_.bare_type() != t_vector) {
1560 error_msgs << "Third argument to algebra_solver must have type vector"
1561 << "; found type= " << alg_fun.theta_.bare_type() << ". "
1562 << std::endl;
1563 pass = false;
1564 }
1565 if (alg_fun.x_r_.bare_type() != t_ar_double) {
1566 error_msgs << "Fourth argument to algebra_solver must have type real[ ]"
1567 << "; found type= " << alg_fun.x_r_.bare_type() << ". "
1568 << std::endl;
1569 pass = false;
1570 }
1571 if (alg_fun.x_i_.bare_type() != t_ar_int) {
1572 error_msgs << "Fifth argument to algebra_solver must have type int[ ]"
1573 << "; found type= " << alg_fun.x_i_.bare_type() << ". "
1574 << std::endl;
1575 pass = false;
1576 }
1577
1578 // real data array cannot be param or x-formed param variable
1579 if (has_var(alg_fun.x_r_, var_map)) {
1580 error_msgs << "Fourth argument to algebra_solver"
1581 << " must be data only (cannot reference parameters)."
1582 << std::endl;
1583 pass = false;
1584 }
1585 }
1586
operator ()(const algebra_solver & alg_fun,const variable_map & var_map,bool & pass,std::ostream & error_msgs) const1587 void validate_algebra_solver::operator()(const algebra_solver &alg_fun,
1588 const variable_map &var_map,
1589 bool &pass,
1590 std::ostream &error_msgs) const {
1591 validate_algebra_solver_non_control_args(alg_fun, var_map, pass, error_msgs);
1592 }
1593 boost::phoenix::function<validate_algebra_solver> validate_algebra_solver_f;
1594
1595 void validate_algebra_solver_control::
operator ()(const algebra_solver_control & alg_fun,const variable_map & var_map,bool & pass,std::ostream & error_msgs) const1596 operator()(const algebra_solver_control &alg_fun, const variable_map &var_map,
1597 bool &pass, std::ostream &error_msgs) const {
1598 validate_algebra_solver_non_control_args(alg_fun, var_map, pass, error_msgs);
1599 if (!alg_fun.rel_tol_.bare_type().is_primitive()) {
1600 error_msgs << "Sixth argument to algebra_solver "
1601 << " (relative tolerance) must have type real or int;"
1602 << " found type=" << alg_fun.rel_tol_.bare_type() << ". "
1603 << std::endl;
1604 pass = false;
1605 }
1606 if (!alg_fun.fun_tol_.bare_type().is_primitive()) {
1607 error_msgs << "Seventh argument to algebra_solver "
1608 << " (function tolerance) must have type real or int;"
1609 << " found type=" << alg_fun.fun_tol_.bare_type() << ". "
1610 << std::endl;
1611 pass = false;
1612 }
1613 if (!alg_fun.max_num_steps_.bare_type().is_primitive()) {
1614 error_msgs << "Eighth argument to algebra_solver"
1615 << " (max number of steps) must have type real or int;"
1616 << " found type=" << alg_fun.max_num_steps_.bare_type() << ". "
1617 << std::endl;
1618 pass = false;
1619 }
1620
1621 // control args cannot contain param variables
1622 if (has_var(alg_fun.rel_tol_, var_map)) {
1623 error_msgs << "Sixth argument to algebra_solver"
1624 << " (relative tolerance) must be data only"
1625 << " and not depend on parameters" << std::endl;
1626 pass = false;
1627 }
1628 if (has_var(alg_fun.fun_tol_, var_map)) {
1629 error_msgs << "Seventh argument to algebra_solver"
1630 << " (function tolerance ) must be data only"
1631 << " and not depend parameters" << std::endl;
1632 pass = false;
1633 }
1634 if (has_var(alg_fun.max_num_steps_, var_map)) {
1635 error_msgs << "Eighth argument to algebra_solver"
1636 << " (max number of steps) must be data only"
1637 << " and not depend on parameters" << std::endl;
1638 pass = false;
1639 }
1640 }
1641 boost::phoenix::function<validate_algebra_solver_control>
1642 validate_algebra_solver_control_f;
1643
operator ()(integrate_1d & fx,const variable_map & var_map,bool & pass,std::ostream & error_msgs) const1644 void validate_integrate_1d::operator()(integrate_1d &fx,
1645 const variable_map &var_map, bool &pass,
1646 std::ostream &error_msgs) const {
1647 pass = true;
1648
1649 // (1) name of function to integrate
1650 if (ends_with("_rng", fx.function_name_)) {
1651 error_msgs << "integrated function may not be an _rng function,"
1652 << " found function name: " << fx.function_name_ << std::endl;
1653 pass = false;
1654 }
1655 double_type t_double;
1656 bare_expr_type sys_result_type(t_double);
1657 std::vector<bare_expr_type> sys_arg_types;
1658 sys_arg_types.push_back(bare_expr_type(t_double));
1659 sys_arg_types.push_back(bare_expr_type(t_double));
1660 sys_arg_types.push_back(bare_expr_type(bare_array_type(double_type(), 1)));
1661 sys_arg_types.push_back(bare_expr_type(bare_array_type(double_type(), 1)));
1662 sys_arg_types.push_back(bare_expr_type(bare_array_type(int_type(), 1)));
1663 function_signature_t system_signature(sys_result_type, sys_arg_types);
1664 if (!function_signatures::instance().is_defined(fx.function_name_,
1665 system_signature)) {
1666 pass = false;
1667 error_msgs << "first argument to integrate_1d"
1668 << " must be the name of a function with signature"
1669 << " (real, real, real[], real[], int[]) : real " << std::endl;
1670 }
1671
1672 // (2) lower bound of integration
1673 if (!fx.lb_.bare_type().is_primitive()) {
1674 pass = false;
1675 error_msgs << "second argument to integrate_1d, the lower bound of"
1676 << " integration, must have type int or real;"
1677 << " found type = " << fx.lb_.bare_type() << "." << std::endl;
1678 }
1679
1680 // (3) lower bound of integration
1681 if (!fx.ub_.bare_type().is_primitive()) {
1682 pass = false;
1683 error_msgs << "third argument to integrate_1d, the upper bound of"
1684 << " integration, must have type int or real;"
1685 << " found type = " << fx.ub_.bare_type() << "." << std::endl;
1686 }
1687
1688 // (4) parameters
1689 if (fx.theta_.bare_type() != bare_array_type(double_type(), 1)) {
1690 pass = false;
1691 error_msgs << "fourth argument to integrate_1d, the parameters,"
1692 << " must have type real[];"
1693 << " found type = " << fx.theta_.bare_type() << "." << std::endl;
1694 }
1695
1696 // (5) real data
1697 if (fx.x_r_.bare_type() != bare_array_type(double_type(), 1)) {
1698 pass = false;
1699 error_msgs << "fifth argument to integrate_1d, the real data,"
1700 << " must have type real[]; found type = " << fx.x_r_.bare_type()
1701 << "." << std::endl;
1702 }
1703
1704 // (6) int data
1705 if (fx.x_i_.bare_type() != bare_array_type(int_type(), 1)) {
1706 pass = false;
1707 error_msgs << "sixth argument to integrate_1d, the integer data,"
1708 << " must have type int[]; found type = " << fx.x_i_.bare_type()
1709 << "." << std::endl;
1710 }
1711
1712 // (7) relative tolerance
1713 if (!fx.rel_tol_.bare_type().is_primitive()) {
1714 pass = false;
1715 error_msgs << "seventh argument to integrate_1d, relative tolerance,"
1716 << " must be of type int or real; found type = "
1717 << fx.rel_tol_.bare_type() << "." << std::endl;
1718 }
1719
1720 // test data-only variables do not have parameters (int locals OK)
1721 if (has_var(fx.x_r_, var_map)) {
1722 pass = false;
1723 error_msgs << "fifth argument to integrate_1d, the real data,"
1724 << " must be data only and not reference parameters."
1725 << std::endl;
1726 }
1727 if (has_var(fx.rel_tol_, var_map)) {
1728 pass = false;
1729 error_msgs << "seventh argument to integrate_1d, relative tolerance,"
1730 << " must be data only and not reference parameters."
1731 << std::endl;
1732 }
1733 }
1734 boost::phoenix::function<validate_integrate_1d> validate_integrate_1d_f;
operator ()(map_rect & mr,const variable_map & var_map,bool & pass,std::ostream & error_msgs) const1735 void validate_map_rect::operator()(map_rect &mr, const variable_map &var_map,
1736 bool &pass, std::ostream &error_msgs) const {
1737 pass = true;
1738
1739 if (has_rng_lp_suffix(mr.fun_name_)) {
1740 error_msgs << "Mapped function cannot be an _rng or _lp function,"
1741 << " found function name: " << mr.fun_name_ << std::endl;
1742 pass = false;
1743 }
1744
1745 // mapped function signature
1746 // vector f(vector param_shared, vector param_local,
1747 // real[] data_r, int[] data_i)
1748 int_type t_int;
1749 double_type t_double;
1750 vector_type t_vector;
1751 bare_expr_type t_ar_int(bare_array_type(t_int, 1));
1752 bare_expr_type t_2d_ar_int(bare_array_type(t_int, 2));
1753 bare_expr_type t_ar_double(bare_array_type(t_double, 1));
1754 bare_expr_type t_2d_ar_double(bare_array_type(t_double, 2));
1755 bare_expr_type t_ar_vector(bare_array_type(t_vector, 1));
1756 bare_expr_type shared_params_type(t_vector);
1757 bare_expr_type job_params_type(t_vector);
1758 bare_expr_type job_data_r_type(t_ar_double);
1759 bare_expr_type job_data_i_type(t_ar_int);
1760 bare_expr_type result_type(t_vector);
1761 std::vector<bare_expr_type> arg_types;
1762 arg_types.push_back(shared_params_type);
1763 arg_types.push_back(job_params_type);
1764 arg_types.push_back(job_data_r_type);
1765 arg_types.push_back(job_data_i_type);
1766 function_signature_t mapped_fun_signature(result_type, arg_types);
1767
1768 // validate mapped function signature
1769 if (!function_signatures::instance().is_defined(mr.fun_name_,
1770 mapped_fun_signature)) {
1771 error_msgs << "First argument to map_rect"
1772 << " must be the name of a function with signature"
1773 << " (vector, vector, real[ ], int[ ]) : vector." << std::endl;
1774 pass = false;
1775 }
1776
1777 // shared parameters - vector
1778 if (mr.shared_params_.bare_type() != shared_params_type) {
1779 if (!pass)
1780 error_msgs << "; ";
1781 error_msgs << "Second argument to map_rect must be of type vector."
1782 << std::endl;
1783 pass = false;
1784 }
1785 // job-specific parameters - array of vectors (array elts map to arg2)
1786 if (mr.job_params_.bare_type() != t_ar_vector) {
1787 if (!pass)
1788 error_msgs << "; ";
1789 error_msgs << "Third argument to map_rect must be of type vector[ ]"
1790 << " (array of vectors)." << std::endl;
1791 pass = false;
1792 }
1793 // job-specific real data - 2-d array of double (array elts map to arg3)
1794 bare_expr_type job_data_rs_type(t_2d_ar_double);
1795 if (mr.job_data_r_.bare_type() != job_data_rs_type) {
1796 if (!pass)
1797 error_msgs << "; ";
1798 error_msgs << "Fourth argument to map_rect must be of type real[ , ]"
1799 << " (two dimensional array of reals)." << std::endl;
1800 pass = false;
1801 }
1802 // job-specific int data - 2-d array of int (array elts map to arg4)
1803 bare_expr_type job_data_is_type(t_2d_ar_int);
1804 if (mr.job_data_i_.bare_type() != job_data_is_type) {
1805 if (!pass)
1806 error_msgs << "; ";
1807 error_msgs << "Fifth argument to map_rect must be of type int[ , ]"
1808 << " (two dimensional array of integers)." << std::endl;
1809 pass = false;
1810 }
1811
1812 // test data is data only
1813 if (has_var(mr.job_data_r_, var_map)) {
1814 if (!pass)
1815 error_msgs << "; ";
1816 error_msgs << "Fourth argment to map_rect must be data only." << std::endl;
1817 pass = false;
1818 }
1819 if (has_var(mr.job_data_i_, var_map)) {
1820 if (!pass)
1821 error_msgs << "; ";
1822 error_msgs << "Fifth argument to map_rect must be data only." << std::endl;
1823 pass = false;
1824 }
1825
1826 if (pass)
1827 mr.register_id();
1828 }
1829 boost::phoenix::function<validate_map_rect> validate_map_rect_f;
1830
operator ()(expression & fun_result,fun & fun,const scope & var_scope,bool & pass,const variable_map & var_map,std::ostream & error_msgs) const1831 void set_fun_type_named::operator()(expression &fun_result, fun &fun,
1832 const scope &var_scope, bool &pass,
1833 const variable_map &var_map,
1834 std::ostream &error_msgs) const {
1835 if (fun.name_ == "get_lp")
1836 error_msgs << "Info: get_lp() function deprecated." << std::endl
1837 << " It will be removed in a future release." << std::endl
1838 << " Use target() instead." << std::endl;
1839 if (fun.name_ == "target")
1840 fun.name_ = "get_lp"; // for code gen and context validation
1841
1842 std::vector<bare_expr_type> arg_types;
1843 for (size_t i = 0; i < fun.args_.size(); ++i)
1844 arg_types.push_back(fun.args_[i].bare_type());
1845
1846 fun.type_ = function_signatures::instance().get_result_type(
1847 fun.name_, arg_types, error_msgs);
1848 if (fun.type_.is_ill_formed_type()) {
1849 pass = false;
1850 return;
1851 }
1852
1853 // get function definition for this functiion
1854 std::vector<bare_expr_type> fun_arg_types;
1855 for (size_t i = 0; i < fun.args_.size(); ++i)
1856 fun_arg_types.push_back(arg_types[i]);
1857 function_signature_t sig(fun.type_, fun_arg_types);
1858 function_signature_t decl_sig =
1859 function_signatures::instance().get_definition(fun.name_, sig);
1860 if (!decl_sig.first.is_ill_formed_type()) {
1861 for (size_t i = 0; i < fun_arg_types.size(); ++i) {
1862 if (decl_sig.second[i].is_data() && has_var(fun.args_[i], var_map)) {
1863 error_msgs << "Function argument error, function: " << fun.name_
1864 << ", argument: " << (i + 1) << " must be data only, "
1865 << "found expression containing a parameter varaible."
1866 << std::endl;
1867 pass = false;
1868 return;
1869 }
1870 }
1871 }
1872
1873 // disjunction so only first match triggered
1874 deprecate_fun("binomial_coefficient_log", "lchoose", fun, error_msgs) ||
1875 deprecate_fun("multiply_log", "lmultiply", fun, error_msgs) ||
1876 deprecate_suffix("_cdf_log", "'_lcdf'", fun, error_msgs) ||
1877 deprecate_suffix("_ccdf_log", "'_lccdf'", fun, error_msgs) ||
1878 deprecate_suffix(
1879 "_log", "'_lpdf' for density functions or '_lpmf' for mass functions",
1880 fun, error_msgs);
1881
1882 // use old function names for built-in prob funs
1883 if (!function_signatures::instance().has_user_defined_key(fun.name_)) {
1884 replace_suffix("_lpdf", "_log", fun);
1885 replace_suffix("_lpmf", "_log", fun);
1886 replace_suffix("_lcdf", "_cdf_log", fun);
1887 replace_suffix("_lccdf", "_ccdf_log", fun);
1888 }
1889 // know these are not user-defined`x
1890 replace_suffix("lmultiply", "multiply_log", fun);
1891 replace_suffix("lchoose", "binomial_coefficient_log", fun);
1892
1893 if (has_rng_suffix(fun.name_)) {
1894 if (!(var_scope.allows_rng())) {
1895 error_msgs << "Random number generators only allowed in"
1896 << " transformed data block, generated quantities block"
1897 << " or user-defined functions with names ending in _rng"
1898 << "; found function=" << fun.name_ << " in block=";
1899 print_scope(error_msgs, var_scope);
1900 error_msgs << std::endl;
1901 pass = false;
1902 return;
1903 }
1904 }
1905
1906 if (has_lp_suffix(fun.name_) || fun.name_ == "target") {
1907 if (!(var_scope.allows_lp_fun())) {
1908 error_msgs << "Function target() or functions suffixed with _lp only"
1909 << " allowed in transformed parameter block, model block"
1910 << std::endl
1911 << "or the body of a function with suffix _lp." << std::endl
1912 << "Found function = "
1913 << (fun.name_ == "get_lp" ? "target or get_lp" : fun.name_)
1914 << " in block = ";
1915 print_scope(error_msgs, var_scope);
1916 error_msgs << std::endl;
1917 pass = false;
1918 return;
1919 }
1920 }
1921
1922 if (fun.name_ == "abs" && fun.args_.size() > 0 &&
1923 fun.args_[0].bare_type().is_double_type()) {
1924 error_msgs << "Info: Function abs(real) is deprecated"
1925 << " in the Stan language." << std::endl
1926 << " It will be removed in a future release."
1927 << std::endl
1928 << " Use fabs(real) instead." << std::endl
1929 << std::endl;
1930 }
1931
1932 if (fun.name_ == "lkj_cov_log") {
1933 error_msgs << "Info: the lkj_cov_log() function"
1934 << " is deprecated. It will be removed in Stan 3." << std::endl
1935 << "Code LKJ covariance in terms of an lkj_corr()"
1936 << " distribution on a correlation matrix"
1937 << " and independent lognormals on the scales." << std::endl
1938 << std::endl;
1939 }
1940
1941 if (fun.name_ == "if_else") {
1942 error_msgs << "Info: the if_else() function"
1943 << " is deprecated. "
1944 << "Use the conditional operator '?:' instead." << std::endl;
1945 }
1946
1947 // add namespace qualifier to avoid ambiguities w/ c math fns
1948 qualify_builtins(fun);
1949
1950 fun_result = fun;
1951 pass = true;
1952 }
1953 boost::phoenix::function<set_fun_type_named> set_fun_type_named_f;
1954
operator ()(expression & e,array_expr & array_expr,const scope & var_scope,bool & pass,const variable_map & var_map,std::ostream & error_msgs) const1955 void infer_array_expr_type::operator()(expression &e, array_expr &array_expr,
1956 const scope &var_scope, bool &pass,
1957 const variable_map &var_map,
1958 std::ostream &error_msgs) const {
1959 if (array_expr.args_.size() == 0) {
1960 // shouldn't occur, because of % operator used to construct it
1961 error_msgs << "Array expression found size 0, must be > 0";
1962 array_expr.type_ = ill_formed_type();
1963 pass = false;
1964 return;
1965 }
1966 bare_expr_type e_first(array_expr.args_[0].bare_type());
1967 for (size_t i = 1; i < array_expr.args_.size(); ++i) {
1968 bare_expr_type e_next(array_expr.args_[i].bare_type());
1969 if (e_first != e_next) {
1970 if (e_first.is_primitive() && e_next.is_primitive()) {
1971 e_first = double_type();
1972 } else {
1973 error_msgs << "Expressions for elements of array must have"
1974 << " the same or promotable types; found"
1975 << " first element type=" << e_first << "; type at position "
1976 << i + 1 << "=" << e_next;
1977 array_expr.type_ = ill_formed_type();
1978 pass = false;
1979 return;
1980 }
1981 }
1982 }
1983 array_expr.type_ = bare_array_type(e_first);
1984 array_expr.array_expr_scope_ = var_scope;
1985 array_expr.has_var_ = has_var(array_expr, var_map);
1986 e = array_expr;
1987 pass = true;
1988 }
1989 boost::phoenix::function<infer_array_expr_type> infer_array_expr_type_f;
1990
1991 void infer_vec_or_matrix_expr_type::
operator ()(expression & e,row_vector_expr & vec_expr,const scope & var_scope,bool & pass,const variable_map & var_map,std::ostream & error_msgs) const1992 operator()(expression &e, row_vector_expr &vec_expr, const scope &var_scope,
1993 bool &pass, const variable_map &var_map,
1994 std::ostream &error_msgs) const {
1995 if (vec_expr.args_.size() == 0) {
1996 // shouldn't occur, because of % operator used to construct it
1997 error_msgs << "Vector or matrix expression found size 0, must be > 0";
1998 pass = false;
1999 return;
2000 }
2001 bare_expr_type e_first = vec_expr.args_[0].bare_type();
2002 if (!(e_first.is_primitive() || e_first.is_row_vector_type())) {
2003 error_msgs << "Matrix expression elements must be type row_vector "
2004 << "and row vector expression elements must be int "
2005 << "or real, but found element of type " << e_first << std::endl;
2006 pass = false;
2007 return;
2008 }
2009 bool is_matrix_el = e_first.is_row_vector_type();
2010 for (size_t i = 1; i < vec_expr.args_.size(); ++i) {
2011 if (is_matrix_el && !vec_expr.args_[i].bare_type().is_row_vector_type()) {
2012 error_msgs << "Matrix expression elements must be type row_vector, "
2013 << "but found element of type "
2014 << vec_expr.args_[i].bare_type() << std::endl;
2015 pass = false;
2016 return;
2017 } else if (!is_matrix_el &&
2018 !(vec_expr.args_[i].bare_type().is_primitive())) {
2019 error_msgs << "Row vector expression elements must be int or real, "
2020 << "but found element of type "
2021 << vec_expr.args_[i].bare_type() << std::endl;
2022 pass = false;
2023 return;
2024 }
2025 }
2026 if (is_matrix_el) {
2027 // create matrix expr object
2028 matrix_expr me = matrix_expr(vec_expr.args_);
2029 me.matrix_expr_scope_ = var_scope;
2030 me.has_var_ = has_var(me, var_map);
2031 e = me;
2032 } else {
2033 vec_expr.row_vector_expr_scope_ = var_scope;
2034 vec_expr.has_var_ = has_var(vec_expr, var_map);
2035 e = vec_expr;
2036 }
2037 pass = true;
2038 }
2039 boost::phoenix::function<infer_vec_or_matrix_expr_type>
2040 infer_vec_or_matrix_expr_type_f;
2041
operator ()(expression & expr1,const expression & expr2,const scope & var_scope,bool & pass,std::ostream & error_msgs) const2042 void exponentiation_expr::operator()(expression &expr1, const expression &expr2,
2043 const scope &var_scope, bool &pass,
2044 std::ostream &error_msgs) const {
2045 if (!expr1.bare_type().is_primitive() || !expr2.bare_type().is_primitive()) {
2046 error_msgs << "Arguments to ^ must be primitive (real or int)"
2047 << "; cannot exponentiate " << expr1.bare_type() << " by "
2048 << expr2.bare_type() << " in block=";
2049 print_scope(error_msgs, var_scope);
2050 error_msgs << std::endl;
2051 pass = false;
2052 return;
2053 }
2054 std::vector<expression> args;
2055 args.push_back(expr1);
2056 args.push_back(expr2);
2057 fun f("pow", args);
2058 set_fun_type(f, error_msgs);
2059 expr1 = expression(f);
2060 }
2061 boost::phoenix::function<exponentiation_expr> exponentiation_f;
2062
operator ()(expression & expr1,const expression & expr2,std::ostream & error_msgs) const2063 void multiplication_expr::operator()(expression &expr1, const expression &expr2,
2064 std::ostream &error_msgs) const {
2065 if (expr1.bare_type().is_primitive() && expr2.bare_type().is_primitive()) {
2066 expr1 *= expr2;
2067 return;
2068 }
2069 std::vector<expression> args;
2070 args.push_back(expr1);
2071 args.push_back(expr2);
2072 fun f("multiply", args);
2073 set_fun_type(f, error_msgs);
2074 expr1 = expression(f);
2075 }
2076 boost::phoenix::function<multiplication_expr> multiplication_f;
2077
operator ()(expression & expr1,const expression & expr2,std::ostream & error_msgs) const2078 void division_expr::operator()(expression &expr1, const expression &expr2,
2079 std::ostream &error_msgs) const {
2080 if (expr1.bare_type().is_primitive() && expr2.bare_type().is_primitive() &&
2081 (expr1.bare_type().is_double_type() ||
2082 expr2.bare_type().is_double_type())) {
2083 expr1 /= expr2;
2084 return;
2085 }
2086 std::vector<expression> args;
2087 args.push_back(expr1);
2088 args.push_back(expr2);
2089
2090 if (expr1.bare_type().is_int_type() && expr2.bare_type().is_int_type()) {
2091 // result might be assigned to real - generate warning
2092 error_msgs << "Info: integer division"
2093 << " implicitly rounds to integer."
2094 << " Found int division: " << expr1.to_string() << " / "
2095 << expr2.to_string() << std::endl
2096 << " Positive values rounded down,"
2097 << " negative values rounded up or down"
2098 << " in platform-dependent way." << std::endl;
2099
2100 fun f("divide", args);
2101 set_fun_type(f, error_msgs);
2102 expr1 = expression(f);
2103 return;
2104 }
2105 if ((expr1.bare_type().is_matrix_type() ||
2106 expr1.bare_type().is_row_vector_type()) &&
2107 expr2.bare_type().is_matrix_type()) {
2108 fun f("mdivide_right", args);
2109 set_fun_type(f, error_msgs);
2110 expr1 = expression(f);
2111 return;
2112 }
2113 fun f("divide", args);
2114 set_fun_type(f, error_msgs);
2115 expr1 = expression(f);
2116 return;
2117 }
2118 boost::phoenix::function<division_expr> division_f;
2119
operator ()(expression & expr1,const expression & expr2,bool & pass,std::ostream & error_msgs) const2120 void modulus_expr::operator()(expression &expr1, const expression &expr2,
2121 bool &pass, std::ostream &error_msgs) const {
2122 if (!expr1.bare_type().is_int_type() && !expr2.bare_type().is_int_type()) {
2123 error_msgs << "Both operands of % must be int"
2124 << "; cannot modulo " << expr1.bare_type() << " by "
2125 << expr2.bare_type();
2126 error_msgs << std::endl;
2127 pass = false;
2128 return;
2129 }
2130 std::vector<expression> args;
2131 args.push_back(expr1);
2132 args.push_back(expr2);
2133 fun f("modulus", args);
2134 set_fun_type(f, error_msgs);
2135 expr1 = expression(f);
2136 }
2137 boost::phoenix::function<modulus_expr> modulus_f;
2138
operator ()(expression & expr1,bool & pass,const expression & expr2,std::ostream & error_msgs) const2139 void left_division_expr::operator()(expression &expr1, bool &pass,
2140 const expression &expr2,
2141 std::ostream &error_msgs) const {
2142 std::vector<expression> args;
2143 args.push_back(expr1);
2144 args.push_back(expr2);
2145 if (expr1.bare_type().is_matrix_type() &&
2146 (expr2.bare_type().is_vector_type() ||
2147 expr2.bare_type().is_matrix_type())) {
2148 fun f("mdivide_left", args);
2149 set_fun_type(f, error_msgs);
2150 expr1 = expression(f);
2151 pass = true;
2152 return;
2153 }
2154 fun f("mdivide_left", args); // set for alt args err msg
2155 set_fun_type(f, error_msgs);
2156 expr1 = expression(f);
2157 pass = false;
2158 }
2159 boost::phoenix::function<left_division_expr> left_division_f;
2160
operator ()(expression & expr1,const expression & expr2,std::ostream & error_msgs) const2161 void elt_multiplication_expr::operator()(expression &expr1,
2162 const expression &expr2,
2163 std::ostream &error_msgs) const {
2164 if (expr1.bare_type().is_primitive() && expr2.bare_type().is_primitive()) {
2165 expr1 *= expr2;
2166 return;
2167 }
2168 std::vector<expression> args;
2169 args.push_back(expr1);
2170 args.push_back(expr2);
2171 fun f("elt_multiply", args);
2172 set_fun_type(f, error_msgs);
2173 expr1 = expression(f);
2174 }
2175 boost::phoenix::function<elt_multiplication_expr> elt_multiplication_f;
2176
operator ()(expression & expr1,const expression & expr2,std::ostream & error_msgs) const2177 void elt_division_expr::operator()(expression &expr1, const expression &expr2,
2178 std::ostream &error_msgs) const {
2179 if (expr1.bare_type().is_primitive() && expr2.bare_type().is_primitive()) {
2180 expr1 /= expr2;
2181 return;
2182 }
2183 std::vector<expression> args;
2184 args.push_back(expr1);
2185 args.push_back(expr2);
2186 fun f("elt_divide", args);
2187 set_fun_type(f, error_msgs);
2188 expr1 = expression(f);
2189 }
2190 boost::phoenix::function<elt_division_expr> elt_division_f;
2191
operator ()(expression & expr_result,const expression & expr,bool & pass,std::ostream & error_msgs) const2192 void negate_expr::operator()(expression &expr_result, const expression &expr,
2193 bool &pass, std::ostream &error_msgs) const {
2194 if (expr.bare_type().is_primitive()) {
2195 expr_result = expression(unary_op('-', expr));
2196 return;
2197 }
2198 std::vector<expression> args;
2199 args.push_back(expr);
2200 fun f("minus", args);
2201 set_fun_type(f, error_msgs);
2202 expr_result = expression(f);
2203 }
2204 boost::phoenix::function<negate_expr> negate_expr_f;
2205
operator ()(expression & expr_result,const expression & expr,std::ostream & error_msgs) const2206 void logical_negate_expr::operator()(expression &expr_result,
2207 const expression &expr,
2208 std::ostream &error_msgs) const {
2209 if (!expr.bare_type().is_primitive()) {
2210 error_msgs << "Logical negation operator !"
2211 << " only applies to int or real types; ";
2212 expr_result = expression();
2213 }
2214 std::vector<expression> args;
2215 args.push_back(expr);
2216 fun f("logical_negation", args);
2217 set_fun_type(f, error_msgs);
2218 expr_result = expression(f);
2219 }
2220 boost::phoenix::function<logical_negate_expr> logical_negate_expr_f;
2221
operator ()(expression & expr,bool & pass,std::ostream & error_msgs) const2222 void transpose_expr::operator()(expression &expr, bool &pass,
2223 std::ostream &error_msgs) const {
2224 if (expr.bare_type().is_primitive())
2225 return;
2226 std::vector<expression> args;
2227 args.push_back(expr);
2228 fun f("transpose", args);
2229 set_fun_type(f, error_msgs);
2230 expr = expression(f);
2231 pass = !expr.bare_type().is_ill_formed_type();
2232 }
2233 boost::phoenix::function<transpose_expr> transpose_f;
2234
operator ()(expression & e,std::vector<idx> & idxs,bool & pass,std::ostream & error_msgs) const2235 void add_idxs::operator()(expression &e, std::vector<idx> &idxs, bool &pass,
2236 std::ostream &error_msgs) const {
2237 e = index_op_sliced(e, idxs);
2238 pass = !e.bare_type().is_ill_formed_type();
2239 if (!pass)
2240 error_msgs << "Indexed expression must have at least as many"
2241 << " dimensions as number of indexes supplied:" << std::endl
2242 << " indexed expression dims=" << e.total_dims()
2243 << "; num indexes=" << idxs.size() << std::endl;
2244 }
2245 boost::phoenix::function<add_idxs> add_idxs_f;
2246
2247 void add_expression_dimss::
operator ()(expression & expression,std::vector<std::vector<stan::lang::expression>> & dimss,bool & pass,std::ostream & error_msgs) const2248 operator()(expression &expression,
2249 std::vector<std::vector<stan::lang::expression>> &dimss, bool &pass,
2250 std::ostream &error_msgs) const {
2251 int expr_dims = expression.total_dims();
2252 int index_dims = num_dimss(dimss);
2253 if (expr_dims < index_dims) {
2254 error_msgs << "Too many indexes, expression dimensions=" << expr_dims
2255 << ", indexes found=" << index_dims << std::endl;
2256 pass = false;
2257 return;
2258 }
2259 index_op iop(expression, dimss);
2260 iop.infer_type();
2261 if (iop.type_.is_ill_formed_type()) {
2262 error_msgs << "Indexed expression must have at least as many"
2263 << " dimensions as number of indexes supplied." << std::endl;
2264 pass = false;
2265 return;
2266 }
2267 pass = true;
2268 expression = iop;
2269 }
2270 boost::phoenix::function<add_expression_dimss> add_expression_dimss_f;
2271
operator ()(variable & var_expr,expression & val,variable_map & vm,std::ostream & error_msgs,bool & pass) const2272 void set_var_type::operator()(variable &var_expr, expression &val,
2273 variable_map &vm, std::ostream &error_msgs,
2274 bool &pass) const {
2275 std::string name = var_expr.name_;
2276 if (name == std::string("lp__")) {
2277 error_msgs << std::endl
2278 << "Error (fatal): Use of lp__ is no longer supported."
2279 << std::endl
2280 << " Use target += ... statement to increment log density."
2281 << std::endl
2282 << " Use target() function to get log density." << std::endl;
2283 pass = false;
2284 return;
2285 } else if (name == std::string("params_r__")) {
2286 error_msgs << std::endl
2287 << "Info:" << std::endl
2288 << " Direct access to params_r__ yields an inconsistent"
2289 << " statistical model in isolation and no guarantee is"
2290 << " made that this model will yield valid inferences."
2291 << std::endl
2292 << " Moreover, access to params_r__ is unsupported"
2293 << " and the variable may be removed without notice."
2294 << std::endl;
2295 } else if (name == std::string("data") || name == std::string("generated") ||
2296 name == std::string("model") ||
2297 name == std::string("parameters") ||
2298 name == std::string("transformed")) {
2299 error_msgs << std::endl
2300 << "Unexpected open block, missing close block \"}\""
2301 << " before keyword \"" << name << "\"." << std::endl;
2302 pass = false;
2303 return;
2304 }
2305 pass = vm.exists(name);
2306 if (pass) {
2307 var_expr.set_type(vm.get_bare_type(name));
2308 } else {
2309 error_msgs << "Variable \"" << name << '"' << " does not exist."
2310 << std::endl;
2311 return;
2312 }
2313 val = expression(var_expr);
2314 }
2315 boost::phoenix::function<set_var_type> set_var_type_f;
2316
operator ()(bool & pass,std::ostream & error_msgs) const2317 void require_vbar::operator()(bool &pass, std::ostream &error_msgs) const {
2318 pass = false;
2319 error_msgs << "Probabilty functions with suffixes _lpdf, _lpmf,"
2320 << " _lcdf, and _lccdf," << std::endl
2321 << "require a vertical bar (|) between the first two"
2322 << " arguments." << std::endl;
2323 }
2324 boost::phoenix::function<require_vbar> require_vbar_f;
2325
data_only_expression(std::stringstream & error_msgs,variable_map & var_map)2326 data_only_expression::data_only_expression(std::stringstream &error_msgs,
2327 variable_map &var_map)
2328 : error_msgs_(error_msgs), var_map_(var_map) {}
operator ()(const nil &) const2329 bool data_only_expression::operator()(const nil & /*e*/) const { return true; }
operator ()(const int_literal &) const2330 bool data_only_expression::operator()(const int_literal & /*x*/) const {
2331 return true;
2332 }
operator ()(const double_literal &) const2333 bool data_only_expression::operator()(const double_literal & /*x*/) const {
2334 return true;
2335 }
operator ()(const array_expr & x) const2336 bool data_only_expression::operator()(const array_expr &x) const {
2337 for (size_t i = 0; i < x.args_.size(); ++i)
2338 if (!boost::apply_visitor(*this, x.args_[i].expr_))
2339 return false;
2340 return true;
2341 }
operator ()(const matrix_expr & x) const2342 bool data_only_expression::operator()(const matrix_expr &x) const {
2343 for (size_t i = 0; i < x.args_.size(); ++i)
2344 if (!boost::apply_visitor(*this, x.args_[i].expr_))
2345 return false;
2346 return true;
2347 }
operator ()(const row_vector_expr & x) const2348 bool data_only_expression::operator()(const row_vector_expr &x) const {
2349 for (size_t i = 0; i < x.args_.size(); ++i)
2350 if (!boost::apply_visitor(*this, x.args_[i].expr_))
2351 return false;
2352 return true;
2353 }
operator ()(const variable & x) const2354 bool data_only_expression::operator()(const variable &x) const {
2355 scope var_scope = var_map_.get_scope(x.name_);
2356 bool is_data = var_scope.allows_size();
2357 if (!is_data) {
2358 error_msgs_ << "Non-data variables are not allowed"
2359 << " in dimension declarations;"
2360 << " found variable=" << x.name_ << "; declared in block=";
2361 print_scope(error_msgs_, var_scope);
2362 error_msgs_ << "." << std::endl;
2363 }
2364 return is_data;
2365 }
operator ()(const integrate_1d & x) const2366 bool data_only_expression::operator()(const integrate_1d &x) const {
2367 return boost::apply_visitor(*this, x.lb_.expr_) &&
2368 boost::apply_visitor(*this, x.ub_.expr_) &&
2369 boost::apply_visitor(*this, x.theta_.expr_);
2370 }
operator ()(const integrate_ode & x) const2371 bool data_only_expression::operator()(const integrate_ode &x) const {
2372 return boost::apply_visitor(*this, x.y0_.expr_) &&
2373 boost::apply_visitor(*this, x.theta_.expr_);
2374 }
operator ()(const integrate_ode_control & x) const2375 bool data_only_expression::operator()(const integrate_ode_control &x) const {
2376 return boost::apply_visitor(*this, x.y0_.expr_) &&
2377 boost::apply_visitor(*this, x.theta_.expr_);
2378 }
operator ()(const algebra_solver & x) const2379 bool data_only_expression::operator()(const algebra_solver &x) const {
2380 return boost::apply_visitor(*this, x.theta_.expr_);
2381 }
operator ()(const algebra_solver_control & x) const2382 bool data_only_expression::operator()(const algebra_solver_control &x) const {
2383 return boost::apply_visitor(*this, x.theta_.expr_);
2384 }
operator ()(const map_rect & x) const2385 bool data_only_expression::operator()(const map_rect &x) const {
2386 return boost::apply_visitor(*this, x.shared_params_.expr_) &&
2387 boost::apply_visitor(*this, x.job_params_.expr_);
2388 }
operator ()(const fun & x) const2389 bool data_only_expression::operator()(const fun &x) const {
2390 for (size_t i = 0; i < x.args_.size(); ++i)
2391 if (!boost::apply_visitor(*this, x.args_[i].expr_))
2392 return false;
2393 return true;
2394 }
operator ()(const index_op & x) const2395 bool data_only_expression::operator()(const index_op &x) const {
2396 if (!boost::apply_visitor(*this, x.expr_.expr_))
2397 return false;
2398 for (size_t i = 0; i < x.dimss_.size(); ++i)
2399 for (size_t j = 0; j < x.dimss_[i].size(); ++j)
2400 if (!boost::apply_visitor(*this, x.dimss_[i][j].expr_))
2401 return false;
2402 return true;
2403 }
operator ()(const index_op_sliced & x) const2404 bool data_only_expression::operator()(const index_op_sliced &x) const {
2405 return boost::apply_visitor(*this, x.expr_.expr_);
2406 }
operator ()(const conditional_op & x) const2407 bool data_only_expression::operator()(const conditional_op &x) const {
2408 return boost::apply_visitor(*this, x.cond_.expr_) &&
2409 boost::apply_visitor(*this, x.true_val_.expr_) &&
2410 boost::apply_visitor(*this, x.false_val_.expr_);
2411 }
operator ()(const binary_op & x) const2412 bool data_only_expression::operator()(const binary_op &x) const {
2413 return boost::apply_visitor(*this, x.left.expr_) &&
2414 boost::apply_visitor(*this, x.right.expr_);
2415 }
operator ()(const unary_op & x) const2416 bool data_only_expression::operator()(const unary_op &x) const {
2417 return boost::apply_visitor(*this, x.subject.expr_);
2418 }
2419
2420 template <typename T>
operator ()(const scope & var_scope,const T & var_decl,bool & pass,std::stringstream & error_msgs) const2421 void validate_definition::operator()(const scope &var_scope, const T &var_decl,
2422 bool &pass,
2423 std::stringstream &error_msgs) const {
2424 if (is_nil(var_decl.def()))
2425 return;
2426
2427 // validate that assigment is allowed in this block
2428 if (!var_scope.allows_assignment()) {
2429 error_msgs << "Variable definition not possible in this block."
2430 << std::endl;
2431 pass = false;
2432 }
2433
2434 // validate type
2435 bare_expr_type decl_type(var_decl.bare_type());
2436 bare_expr_type def_type = var_decl.def().bare_type();
2437
2438 bool types_compatible =
2439 (decl_type == def_type) ||
2440 (decl_type.is_primitive() && def_type.is_primitive() &&
2441 decl_type.is_double_type() && def_type.is_int_type());
2442
2443 if (!types_compatible) {
2444 error_msgs << "Variable definition base type mismatch,"
2445 << " variable declared as base type ";
2446 write_bare_expr_type(error_msgs, decl_type);
2447 error_msgs << " variable definition has base type ";
2448 write_bare_expr_type(error_msgs, def_type);
2449 pass = false;
2450 }
2451 // validate dims
2452 if (decl_type.num_dims() != def_type.num_dims()) {
2453 error_msgs << "Variable definition dimensions mismatch,"
2454 << " definition specifies " << decl_type.num_dims()
2455 << ", declaration specifies " << def_type.num_dims();
2456 pass = false;
2457 }
2458 return;
2459 }
2460 boost::phoenix::function<validate_definition> validate_definition_f;
2461
2462 template void validate_definition::
2463 operator()(const scope &var_scope, const block_var_decl &var_decl, bool &pass,
2464 std::stringstream &error_msgs) const;
2465
2466 template void validate_definition::
2467 operator()(const scope &var_scope, const local_var_decl &var_decl, bool &pass,
2468 std::stringstream &error_msgs) const;
2469
2470 template void validate_definition::
2471 operator()(const scope &var_scope, const var_decl &var_decl, bool &pass,
2472 std::stringstream &error_msgs) const;
2473
reserve(const std::string & w)2474 void validate_identifier::reserve(const std::string &w) {
2475 reserved_word_set_.insert(w);
2476 }
contains(const std::set<std::string> & s,const std::string & x) const2477 bool validate_identifier::contains(const std::set<std::string> &s,
2478 const std::string &x) const {
2479 return s.find(x) != s.end();
2480 }
identifier_exists(const std::string & identifier) const2481 bool validate_identifier::identifier_exists(
2482 const std::string &identifier) const {
2483 return contains(reserved_word_set_, identifier) ||
2484 (contains(function_signatures::instance().key_set(), identifier) &&
2485 !contains(const_fun_name_set_, identifier));
2486 }
2487
validate_identifier()2488 validate_identifier::validate_identifier() {
2489 // constant functions which may be used as identifiers
2490 const_fun_name_set_.insert("pi");
2491 const_fun_name_set_.insert("e");
2492 const_fun_name_set_.insert("sqrt2");
2493 const_fun_name_set_.insert("log2");
2494 const_fun_name_set_.insert("log10");
2495 const_fun_name_set_.insert("not_a_number");
2496 const_fun_name_set_.insert("positive_infinity");
2497 const_fun_name_set_.insert("negative_infinity");
2498 const_fun_name_set_.insert("epsilon");
2499 const_fun_name_set_.insert("negative_epsilon");
2500 const_fun_name_set_.insert("machine_precision");
2501
2502 // illegal identifiers
2503 reserve("for");
2504 reserve("in");
2505 reserve("while");
2506 reserve("repeat");
2507 reserve("until");
2508 reserve("if");
2509 reserve("then");
2510 reserve("else");
2511 reserve("true");
2512 reserve("false");
2513
2514 reserve("int");
2515 reserve("real");
2516 reserve("vector");
2517 reserve("unit_vector");
2518 reserve("simplex");
2519 reserve("ordered");
2520 reserve("positive_ordered");
2521 reserve("row_vector");
2522 reserve("matrix");
2523 reserve("cholesky_factor_cov");
2524 reserve("cholesky_factor_corr");
2525 reserve("cov_matrix");
2526 reserve("corr_matrix");
2527
2528 reserve("target");
2529
2530 reserve("model");
2531 reserve("data");
2532 reserve("parameters");
2533 reserve("quantities");
2534 reserve("transformed");
2535 reserve("generated");
2536
2537 reserve("var");
2538 reserve("fvar");
2539 reserve("STAN_MAJOR");
2540 reserve("STAN_MINOR");
2541 reserve("STAN_PATCH");
2542 reserve("STAN_MATH_MAJOR");
2543 reserve("STAN_MATH_MINOR");
2544 reserve("STAN_MATH_PATCH");
2545
2546 reserve("alignas");
2547 reserve("alignof");
2548 reserve("and");
2549 reserve("and_eq");
2550 reserve("asm");
2551 reserve("auto");
2552 reserve("bitand");
2553 reserve("bitor");
2554 reserve("bool");
2555 reserve("break");
2556 reserve("case");
2557 reserve("catch");
2558 reserve("char");
2559 reserve("char16_t");
2560 reserve("char32_t");
2561 reserve("class");
2562 reserve("compl");
2563 reserve("const");
2564 reserve("constexpr");
2565 reserve("const_cast");
2566 reserve("continue");
2567 reserve("decltype");
2568 reserve("default");
2569 reserve("delete");
2570 reserve("do");
2571 reserve("double");
2572 reserve("dynamic_cast");
2573 reserve("else");
2574 reserve("enum");
2575 reserve("explicit");
2576 reserve("export");
2577 reserve("extern");
2578 reserve("false");
2579 reserve("float");
2580 reserve("for");
2581 reserve("friend");
2582 reserve("goto");
2583 reserve("if");
2584 reserve("inline");
2585 reserve("int");
2586 reserve("long");
2587 reserve("mutable");
2588 reserve("namespace");
2589 reserve("new");
2590 reserve("noexcept");
2591 reserve("not");
2592 reserve("not_eq");
2593 reserve("nullptr");
2594 reserve("operator");
2595 reserve("or");
2596 reserve("or_eq");
2597 reserve("private");
2598 reserve("protected");
2599 reserve("public");
2600 reserve("register");
2601 reserve("reinterpret_cast");
2602 reserve("return");
2603 reserve("short");
2604 reserve("signed");
2605 reserve("sizeof");
2606 reserve("static");
2607 reserve("static_assert");
2608 reserve("static_cast");
2609 reserve("struct");
2610 reserve("switch");
2611 reserve("template");
2612 reserve("this");
2613 reserve("thread_local");
2614 reserve("throw");
2615 reserve("true");
2616 reserve("try");
2617 reserve("typedef");
2618 reserve("typeid");
2619 reserve("typename");
2620 reserve("union");
2621 reserve("unsigned");
2622 reserve("using");
2623 reserve("virtual");
2624 reserve("void");
2625 reserve("volatile");
2626 reserve("wchar_t");
2627 reserve("while");
2628 reserve("xor");
2629 reserve("xor_eq");
2630
2631 // function names declared in signatures
2632 using stan::lang::function_signatures;
2633 using std::set;
2634 using std::string;
2635 const function_signatures &sigs = function_signatures::instance();
2636
2637 set<string> fun_names = sigs.key_set();
2638 for (set<string>::iterator it = fun_names.begin(); it != fun_names.end();
2639 ++it)
2640 if (!contains(const_fun_name_set_, *it))
2641 reserve(*it);
2642 }
2643
2644 // validates identifier shape
operator ()(const std::string & identifier,bool & pass,std::stringstream & error_msgs) const2645 void validate_identifier::operator()(const std::string &identifier, bool &pass,
2646 std::stringstream &error_msgs) const {
2647 int len = identifier.size();
2648 if (len >= 2 && identifier[len - 1] == '_' && identifier[len - 2] == '_') {
2649 error_msgs << "Variable identifier (name) may"
2650 << " not end in double underscore (__)" << std::endl
2651 << " found identifer=" << identifier << std::endl;
2652 pass = false;
2653 return;
2654 }
2655 size_t period_position = identifier.find('.');
2656 if (period_position != std::string::npos) {
2657 error_msgs << "Variable identifier may not contain a period (.)"
2658 << std::endl
2659 << " found period at position (indexed from 0)="
2660 << period_position << std::endl
2661 << " found identifier=" << identifier << std::endl;
2662 pass = false;
2663 return;
2664 }
2665 if (identifier_exists(identifier)) {
2666 error_msgs << "Variable identifier (name) may not be reserved word"
2667 << std::endl
2668 << " found identifier=" << identifier << std::endl;
2669 pass = false;
2670 return;
2671 }
2672 pass = true;
2673 }
2674 boost::phoenix::function<validate_identifier> validate_identifier_f;
2675
2676 // copies single dimension from M to N if only M declared
2677 void copy_square_cholesky_dimension_if_necessary::
operator ()(cholesky_factor_cov_block_type & block_type) const2678 operator()(cholesky_factor_cov_block_type &block_type) const {
2679 if (is_nil(block_type.N_))
2680 block_type.N_ = block_type.M_;
2681 }
2682 boost::phoenix::function<copy_square_cholesky_dimension_if_necessary>
2683 copy_square_cholesky_dimension_if_necessary_f;
2684
operator ()(range & r,std::stringstream &) const2685 void empty_range::operator()(range &r,
2686 std::stringstream & /*error_msgs*/) const {
2687 r = range();
2688 }
2689 boost::phoenix::function<empty_range> empty_range_f;
2690
2691 void empty_offset_multiplier::
operator ()(offset_multiplier & r,std::stringstream &) const2692 operator()(offset_multiplier &r, std::stringstream & /*error_msgs*/) const {
2693 r = offset_multiplier();
2694 }
2695 boost::phoenix::function<empty_offset_multiplier> empty_offset_multiplier_f;
2696
operator ()(range & range,const expression & expr,bool & pass,std::stringstream & error_msgs) const2697 void set_int_range_lower::operator()(range &range, const expression &expr,
2698 bool &pass,
2699 std::stringstream &error_msgs) const {
2700 range.low_ = expr;
2701 validate_int_expr validator;
2702 validator(expr, pass, error_msgs);
2703 }
2704 boost::phoenix::function<set_int_range_lower> set_int_range_lower_f;
2705
operator ()(range & range,const expression & expr,bool & pass,std::stringstream & error_msgs) const2706 void set_int_range_upper::operator()(range &range, const expression &expr,
2707 bool &pass,
2708 std::stringstream &error_msgs) const {
2709 range.high_ = expr;
2710 validate_int_expr validator;
2711 validator(expr, pass, error_msgs);
2712 }
2713 boost::phoenix::function<set_int_range_upper> set_int_range_upper_f;
2714
2715 void validate_int_data_only_expr::
operator ()(const expression & expr,bool & pass,variable_map & var_map,std::stringstream & error_msgs) const2716 operator()(const expression &expr, bool &pass, variable_map &var_map,
2717 std::stringstream &error_msgs) const {
2718 if (!expr.bare_type().is_int_type()) {
2719 error_msgs << "Dimension declaration requires expression"
2720 << " denoting integer; found type=" << expr.bare_type()
2721 << std::endl;
2722 pass = false;
2723 return;
2724 }
2725 data_only_expression vis(error_msgs, var_map);
2726 bool only_data_dimensions = boost::apply_visitor(vis, expr.expr_);
2727 pass = only_data_dimensions;
2728 return;
2729 }
2730 boost::phoenix::function<validate_int_data_only_expr>
2731 validate_int_data_only_expr_f;
2732
operator ()(range & range,const expression & expr,bool & pass,std::stringstream & error_msgs) const2733 void set_double_range_lower::operator()(range &range, const expression &expr,
2734 bool &pass,
2735 std::stringstream &error_msgs) const {
2736 range.low_ = expr;
2737 validate_double_expr validator;
2738 validator(expr, pass, error_msgs);
2739 }
2740 boost::phoenix::function<set_double_range_lower> set_double_range_lower_f;
2741
operator ()(range & range,const expression & expr,bool & pass,std::stringstream & error_msgs) const2742 void set_double_range_upper::operator()(range &range, const expression &expr,
2743 bool &pass,
2744 std::stringstream &error_msgs) const {
2745 range.high_ = expr;
2746 validate_double_expr validator;
2747 validator(expr, pass, error_msgs);
2748 }
2749 boost::phoenix::function<set_double_range_upper> set_double_range_upper_f;
2750
2751 void set_double_offset_multiplier_loc::
operator ()(offset_multiplier & offset_multiplier,const expression & expr,bool & pass,std::stringstream & error_msgs) const2752 operator()(offset_multiplier &offset_multiplier, const expression &expr,
2753 bool &pass, std::stringstream &error_msgs) const {
2754 offset_multiplier.offset_ = expr;
2755 validate_double_expr validator;
2756 validator(expr, pass, error_msgs);
2757 }
2758 boost::phoenix::function<set_double_offset_multiplier_loc>
2759 set_double_offset_multiplier_offset_f;
2760
2761 void set_double_offset_multiplier_multiplier::
operator ()(offset_multiplier & offset_multiplier,const expression & expr,bool & pass,std::stringstream & error_msgs) const2762 operator()(offset_multiplier &offset_multiplier, const expression &expr,
2763 bool &pass, std::stringstream &error_msgs) const {
2764 offset_multiplier.multiplier_ = expr;
2765 validate_double_expr validator;
2766 validator(expr, pass, error_msgs);
2767 }
2768 boost::phoenix::function<set_double_offset_multiplier_multiplier>
2769 set_double_offset_multiplier_multiplier_f;
2770
2771
2772 void validate_array_block_var_decl::
operator ()(block_var_decl & var_decl_result,const block_var_type & el_type,const std::string & name,const std::vector<expression> & dims,const expression & def,bool & pass,std::ostream & error_msgs) const2773 operator()(block_var_decl &var_decl_result, const block_var_type &el_type,
2774 const std::string &name, const std::vector<expression> &dims,
2775 const expression &def, bool &pass, std::ostream &error_msgs) const {
2776 if (dims.size() == 0) {
2777 error_msgs << "Array type requires at least 1 dimension,"
2778 << " none found" << std::endl;
2779 pass = false;
2780 return;
2781 }
2782 if (el_type.bare_type().is_ill_formed_type()) {
2783 error_msgs << "Array variable declaration is ill formed,"
2784 << " variable name=" << name << std::endl;
2785 pass = false;
2786 return;
2787 }
2788 stan::lang::block_array_type bat(el_type, dims);
2789 block_var_decl result(name, bat, def);
2790 var_decl_result = result;
2791 }
2792 boost::phoenix::function<validate_array_block_var_decl>
2793 validate_array_block_var_decl_f;
2794
2795 void validate_single_block_var_decl::
operator ()(const block_var_decl & var_decl,bool & pass,std::ostream & error_msgs) const2796 operator()(const block_var_decl &var_decl, bool &pass,
2797 std::ostream &error_msgs) const {
2798 if (var_decl.bare_type().is_ill_formed_type()) {
2799 error_msgs << "Variable declaration is ill formed,"
2800 << " variable name=" << var_decl.name() << std::endl;
2801 pass = false;
2802 return;
2803 }
2804 }
2805 boost::phoenix::function<validate_single_block_var_decl>
2806 validate_single_block_var_decl_f;
2807
2808 void validate_single_local_var_decl::
operator ()(const local_var_decl & var_decl,bool & pass,std::ostream & error_msgs) const2809 operator()(const local_var_decl &var_decl, bool &pass,
2810 std::ostream &error_msgs) const {
2811 if (var_decl.bare_type().is_ill_formed_type()) {
2812 error_msgs << "Variable declaration is ill formed,"
2813 << " variable name=" << var_decl.name() << std::endl;
2814 pass = false;
2815 return;
2816 }
2817 }
2818 boost::phoenix::function<validate_single_local_var_decl>
2819 validate_single_local_var_decl_f;
2820
2821 void validate_array_local_var_decl::
operator ()(local_var_decl & var_decl_result,const local_var_type & el_type,const std::string & name,const std::vector<expression> & dims,const expression & def,bool & pass,std::ostream & error_msgs) const2822 operator()(local_var_decl &var_decl_result, const local_var_type &el_type,
2823 const std::string &name, const std::vector<expression> &dims,
2824 const expression &def, bool &pass, std::ostream &error_msgs) const {
2825 if (dims.size() == 0) {
2826 error_msgs << "Array type requires at least 1 dimension,"
2827 << " none found" << std::endl;
2828 pass = false;
2829 return;
2830 }
2831 if (el_type.bare_type().is_ill_formed_type()) {
2832 error_msgs << "Array variable declaration is ill formed,"
2833 << " variable name=" << name << std::endl;
2834 pass = false;
2835 return;
2836 }
2837 stan::lang::local_array_type bat(el_type, dims);
2838 local_var_decl result(name, bat, def);
2839 var_decl_result = result;
2840 }
2841 boost::phoenix::function<validate_array_local_var_decl>
2842 validate_array_local_var_decl_f;
2843
operator ()(var_decl & var_decl_result,const bare_expr_type & bare_type,const std::string & name,bool & pass,std::ostream & error_msgs) const2844 void validate_fun_arg_var::operator()(var_decl &var_decl_result,
2845 const bare_expr_type &bare_type,
2846 const std::string &name, bool &pass,
2847 std::ostream &error_msgs) const {
2848 if (bare_type.is_ill_formed_type()) {
2849 error_msgs << "Function argument is ill formed,"
2850 << " name=" << name << std::endl;
2851 pass = false;
2852 return;
2853 }
2854 stan::lang::var_decl vd(name, bare_type);
2855 var_decl_result = vd;
2856 }
2857 boost::phoenix::function<validate_fun_arg_var> validate_fun_arg_var_f;
2858
operator ()(bare_expr_type & bare_type_result,const bare_expr_type & el_type,const size_t & num_dims,bool & pass,std::ostream & error_msgs) const2859 void validate_bare_type::operator()(bare_expr_type &bare_type_result,
2860 const bare_expr_type &el_type,
2861 const size_t &num_dims, bool &pass,
2862 std::ostream &error_msgs) const {
2863 if (el_type.is_ill_formed_type()) {
2864 error_msgs << "Ill-formed bare type" << std::endl;
2865 pass = false;
2866 return;
2867 }
2868 pass = true;
2869 if (num_dims == 0) {
2870 bare_type_result = el_type;
2871 return;
2872 }
2873 stan::lang::bare_array_type bat(el_type);
2874 for (size_t i = 0; i < num_dims - 1; ++i) {
2875 stan::lang::bare_expr_type cur_type(bat);
2876 bat = bare_array_type(cur_type);
2877 }
2878 bare_type_result = bat;
2879 }
2880 boost::phoenix::function<validate_bare_type> validate_bare_type_f;
2881
2882 template <typename T>
operator ()(const T & decl,variable_map & vm,bool & pass,const scope & var_scope,std::ostream & error_msgs) const2883 void add_to_var_map::operator()(const T &decl, variable_map &vm, bool &pass,
2884 const scope &var_scope,
2885 std::ostream &error_msgs) const {
2886 pass = false;
2887 if (vm.exists(decl.name())) {
2888 var_decl prev_decl = vm.get(decl.name());
2889 error_msgs << "Duplicate declaration of variable, name=" << decl.name();
2890
2891 error_msgs << "; attempt to redeclare as " << decl.bare_type() << " in ";
2892 print_scope(error_msgs, var_scope);
2893
2894 error_msgs << "; previously declared as " << prev_decl.bare_type()
2895 << " in ";
2896 print_scope(error_msgs, vm.get_scope(decl.name()));
2897
2898 error_msgs << std::endl;
2899 pass = false;
2900 return;
2901 }
2902 if (var_scope.par_or_tpar() &&
2903 decl.bare_type().innermost_type().is_int_type()) {
2904 error_msgs << "Parameters or transformed parameters"
2905 << " cannot be integer or integer array; "
2906 << " found int variable declaration, name=" << decl.name()
2907 << std::endl;
2908 pass = false;
2909 return;
2910 }
2911 var_decl bare_decl(decl.name(), decl.type().bare_type(), decl.def());
2912
2913 vm.add(decl.name(), bare_decl, var_scope);
2914 pass = true;
2915 }
2916 boost::phoenix::function<add_to_var_map> add_to_var_map_f;
2917
2918 template void add_to_var_map::operator()(const block_var_decl &decl,
2919 variable_map &vm, bool &pass,
2920 const scope &var_scope,
2921 std::ostream &error_msgs) const;
2922
2923 template void add_to_var_map::operator()(const local_var_decl &decl,
2924 variable_map &vm, bool &pass,
2925 const scope &var_scope,
2926 std::ostream &error_msgs) const;
2927
operator ()(bool in_loop,bool & pass,std::ostream & error_msgs) const2928 void validate_in_loop::operator()(bool in_loop, bool &pass,
2929 std::ostream &error_msgs) const {
2930 pass = in_loop;
2931 if (!pass)
2932 error_msgs << "Break and continue statements are only allowed"
2933 << " in the body of a for-loop or while-loop." << std::endl;
2934 }
2935 boost::phoenix::function<validate_in_loop> validate_in_loop_f;
2936
operator ()(const expression & e,bool & pass,std::ostream & error_msgs) const2937 void non_void_expression::operator()(const expression &e, bool &pass,
2938 std::ostream &error_msgs) const {
2939 // ill-formed shouldn't be possible, but just in case
2940 pass = !(e.bare_type().is_void_type() || e.bare_type().is_ill_formed_type());
2941 if (!pass)
2942 error_msgs << "Error: expected printable (non-void) expression."
2943 << std::endl;
2944 }
2945 boost::phoenix::function<non_void_expression> non_void_expression_f;
2946
operator ()(scope & var_scope,const origin_block & program_block) const2947 void set_var_scope::operator()(scope &var_scope,
2948 const origin_block &program_block) const {
2949 var_scope = scope(program_block);
2950 }
2951 boost::phoenix::function<set_var_scope> set_var_scope_f;
2952
operator ()(scope & var_scope) const2953 void set_data_origin::operator()(scope &var_scope) const {
2954 var_scope = scope(data_origin);
2955 }
2956 boost::phoenix::function<set_data_origin> set_data_origin_f;
2957
operator ()(scope & var_scope,const origin_block & program_block) const2958 void set_var_scope_local::operator()(scope &var_scope,
2959 const origin_block &program_block) const {
2960 var_scope = scope(program_block, true);
2961 }
2962 boost::phoenix::function<set_var_scope_local> set_var_scope_local_f;
2963
operator ()(scope & var_scope,const scope & scope_enclosing) const2964 void reset_var_scope::operator()(scope &var_scope,
2965 const scope &scope_enclosing) const {
2966 origin_block enclosing_block = scope_enclosing.program_block();
2967 var_scope = scope(enclosing_block, true);
2968 }
2969 boost::phoenix::function<reset_var_scope> reset_var_scope_f;
2970
2971 // only used to debug grammars
operator ()(const std::string & msg) const2972 void trace::operator()(const std::string &msg) const {
2973 // std::cout << msg << std::endl;
2974 }
2975 boost::phoenix::function<trace> trace_f;
2976
2977 // only used to debug grammars
operator ()(const std::string & msg,const bool & pass) const2978 void trace_pass::operator()(const std::string &msg, const bool &pass) const {
2979 // std::cout << msg << " pass? " << pass << std::endl;
2980 }
2981 boost::phoenix::function<trace_pass> trace_pass_f;
2982
operator ()(std::ostream & error_msgs) const2983 void deprecate_pound_comment::operator()(std::ostream &error_msgs) const {
2984 error_msgs << "Info: Comments beginning with #"
2985 << " are deprecated. Please use // in place of #"
2986 << " for line comments." << std::endl;
2987 }
2988 boost::phoenix::function<deprecate_pound_comment> deprecate_pound_comment_f;
2989
2990 } // namespace lang
2991 } // namespace stan
2992 #endif
2993