1 #ifndef STAN_LANG_GENERATOR_WRITE_VAR_DECL_ARG_HPP
2 #define STAN_LANG_GENERATOR_WRITE_VAR_DECL_ARG_HPP
3 
4 #include <stan/lang/ast.hpp>
5 #include <stan/lang/generator/constants.hpp>
6 #include <stan/lang/generator/generate_expression.hpp>
7 #include <stan/lang/generator/get_verbose_var_type.hpp>
8 #include <ostream>
9 #include <string>
10 #include <vector>
11 
12 namespace stan {
13   namespace lang {
14     /**
15      * Write the initial value passed as an argument to the
16      * variable declaration constructor.
17      * For int type, initial value is 0.
18      * For double type, initial value is DUMMY_VAR
19      * For container types, generate proper set of nested types.
20      *
21      * Note:  this is called after array type has been unfolded,
22      * so bare_type shouldn't be bare_array_type (or ill_formed_type).
23      *
24      * @param[in] bare_type
25      * @param[in] cpp_type_str generated cpp type
26      * @param[in] ar_lens vector of sizes for each array dimension
27      * @param[in] arg1 expression for size of first dim of vec/matrix (or nil)
28      * @param[in] arg2 expression for size of second dim of matrix (or nil)
29      * @param[in,out] o stream for generating
30      */
31     void
write_var_decl_arg(const bare_expr_type & bare_type,const std::string & cpp_type_str,const std::vector<expression> & ar_lens,const expression & arg1,const expression & arg2,std::ostream & o)32     write_var_decl_arg(const bare_expr_type& bare_type,
33                        const std::string& cpp_type_str,
34                        const std::vector<expression>& ar_lens,
35                        const expression& arg1,
36                        const expression& arg2,
37                        std::ostream& o) {
38       bool ends_with_angle
39         = cpp_type_str[cpp_type_str.length()-1] == '>';
40 
41       // innermost element initialization
42       std::stringstream base_init;
43       if (bare_type.is_int_type()) {
44         base_init << "(0)";
45       } else if (bare_type.is_double_type()) {
46         base_init << "(DUMMY_VAR__)";
47       } else if (bare_type.is_vector_type()
48                  || bare_type.is_row_vector_type()) {
49         base_init << "(";
50         generate_expression(arg1, NOT_USER_FACING, base_init);
51         base_init << ")";
52       } else if (bare_type.is_matrix_type()) {
53         base_init << "(";
54         generate_expression(arg1, NOT_USER_FACING, base_init);
55         base_init << ", ";
56         generate_expression(arg2, NOT_USER_FACING, base_init);
57         base_init << ")";
58       } else {
59         // shouldn't get here
60         base_init << "()";
61       }
62 
63       // for array dimensions, init for each dimension is:
64       // <size dim-n>, (n-1) nested vectors of cpp_decl_type
65       int ct = ar_lens.size() - 1;  // tracks nesting
66       for (size_t i = 0; i < ar_lens.size(); ++i, --ct) {
67         o << "(";
68         generate_expression(ar_lens[i], NOT_USER_FACING, o);
69         o << ", ";
70         for (int j = 0; j < ct; ++j)
71           o << "std::vector<";
72         o << cpp_type_str;
73         for (int j = 0; j < ct; ++j) {
74           if (j > 0 || ends_with_angle)
75             o << " ";  // maybe not needed for c++11
76           o << ">";
77         }
78       }
79       o << base_init.str();
80       for (size_t i = 0; i < ar_lens.size(); ++i)
81         o << ")";
82     }
83   }
84 }
85 #endif
86