Stan  2.14.0
probability, sampling & optimization
returns_type_vis_def.hpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_AST_FUN_RETURNS_TYPE_VIS_DEF_HPP
2 #define STAN_LANG_AST_FUN_RETURNS_TYPE_VIS_DEF_HPP
3 
15 #include <ostream>
16 
17 namespace stan {
18  namespace lang {
19 
20 
22  std::ostream& error_msgs)
23  : return_type_(return_type), error_msgs_(error_msgs) { }
24 
25  bool returns_type_vis::operator()(const nil& st) const {
26  error_msgs_ << "Expecting return, found nil statement."
27  << std::endl;
28  return false;
29  }
30 
31  bool returns_type_vis::operator()(const assignment& st) const {
32  error_msgs_ << "Expecting return, found assignment statement."
33  << std::endl;
34  return false;
35  }
36 
37  bool returns_type_vis::operator()(const assgn& st) const {
38  error_msgs_ << "Expecting return, found assignment statement."
39  << std::endl;
40  return false;
41  }
42 
43  bool returns_type_vis::operator()(const sample& st) const {
44  error_msgs_ << "Expecting return, found sampling statement."
45  << std::endl;
46  return false;
47  }
48 
51  error_msgs_ << "Expecting return, found increment_log_prob statement."
52  << std::endl;
53  return false;
54  }
55 
56  bool returns_type_vis::operator()(const expression& st) const {
57  error_msgs_ << "Expecting return, found increment_log_prob statement."
58  << std::endl;
59  return false;
60  }
61 
63  error_msgs_ << "Expecting return, found print statement."
64  << std::endl;
65  return false;
66  }
67 
69  error_msgs_ << "Expecting return, found reject statement."
70  << std::endl;
71  return false;
72  }
73 
75  error_msgs_ << "Expecting return, found no_op statement."
76  << std::endl;
77  return false;
78  }
79 
80  bool returns_type_vis::operator()(const statements& st) const {
81  // last statement in sequence must return type
82  if (st.statements_.size() == 0) {
83  error_msgs_ << ("Expecting return, found"
84  " statement sequence with empty body.")
85  << std::endl;
86  return false;
87  }
89  }
90 
92  // body must end in appropriate return
94  }
95 
97  // body must end in appropriate return
99  }
100 
102  const {
103  // break/continue OK only as end of nested loop in void return
104  bool pass = (return_type_ == VOID_T);
105  if (!pass)
106  error_msgs_ << "statement " << st.generate_
107  << " does not match return type";
108  return pass;
109  }
110 
112  // all condition bodies must end in appropriate return
113  if (st.bodies_.size() != (st.conditions_.size() + 1)) {
114  error_msgs_ << ("Expecting return, found conditional"
115  " without final else.")
116  << std::endl;
117  return false;
118  }
119  for (size_t i = 0; i < st.bodies_.size(); ++i)
121  return false;
122  return true;
123  }
124 
126  // return checked for type
127  return return_type_ == VOID_T
129  "Returned expression does not match return type",
130  error_msgs_);
131  }
132 
133  }
134 }
135 #endif
AST node for print statements.
std::string generate_
Text to generate, "break" or "continue".
Holder for local variable declarations and a sequence of statements.
Definition: statements.hpp:16
AST node for assignment to variable with multi-indexing.
Definition: assgn.hpp:15
std::vector< statement > statements_
Sequence of statements.
Definition: statements.hpp:25
bool operator()(const nil &st) const
Return true if the specified statement is a return statement with an expression of the type specified...
AST node for the increment log prob (deprecated) and target increment statements. ...
Probability, optimization and sampling library.
AST node for the reject statement.
AST structure for break and continue statements.
std::vector< statement > bodies_
The sequence of bodies to execute.
bool is_assignable(const expr_type &l_type, const expr_type &r_type, const std::string &failure_message, std::ostream &error_msgs)
Return true if an expression of the right-hand side type is assignable to a variable of the left-hand...
Structure of the type of an expression, which consists of a base type and a number of dimensions...
Definition: expr_type.hpp:14
AST node for sampling statements.
Definition: sample.hpp:17
expr_type expression_type() const
bool returns_type(const expr_type &return_type, const statement &statement, std::ostream &error_msgs)
Return true if the specified statement is a return statement returning an expression of the specified...
const int VOID_T
Void type.
expression return_value_
The value returned.
expr_type return_type_
The type of the returned expression expected.
std::vector< expression > conditions_
The sequence of conditions (parallel with bodies).
statement statement_
The body of the for loop.
AST node for representing while statements.
AST node for the no-operation statement.
AST node for the return statement.
returns_type_vis(const expr_type &return_type, std::ostream &error_msgs)
Construct visitor expecting a return statement with an expression of the specified type...
statement body_
The loop body.
The nil structure used as a placeholder for undefined or empty values in several structures.
Definition: nil.hpp:11
AST node for representing a for statement.
AST node for conditional statements.
std::ostream & error_msgs_
Stream to which error messages are written.
AST node for assignment statements.
Definition: assignment.hpp:14

     [ Stan Home Page ] © 2011–2016, Stan Development Team.