1 #ifndef STAN_LANG_AST_VARIABLE_MAP_HPP
2 #define STAN_LANG_AST_VARIABLE_MAP_HPP
3 
4 #include <stan/lang/ast/scope.hpp>
5 #include <stan/lang/ast/type/bare_expr_type.hpp>
6 #include <stan/lang/ast/node/var_decl.hpp>
7 #include <cstddef>
8 #include <map>
9 #include <string>
10 #include <utility>
11 
12 namespace stan {
13   namespace lang {
14 
15     /**
16      * A map from variable names to their declarations and their scope.
17      */
18     struct variable_map {
19       /**
20        * A variable type and the scope of its declaration.
21        */
22       typedef std::pair<var_decl, scope> range_t;
23 
24       /**
25        * Return true if a variable has been declared with the
26        * specified name.
27        *
28        * @param name variable name
29        * @return true if the variable has been declared
30        */
31       bool exists(const std::string& name) const;
32 
33       /**
34        * Return the type for the variable with the specified name.
35        *
36        * @param name variable name
37        * @return base declaration for variable with the specified name
38        * @throw std::invalid_argument if the variable has not been
39        * declared
40        */
41       var_decl get(const std::string& name) const;
42 
43       /**
44        * Return the type declared for the variable with the specified
45        * name.
46        *
47        * @param name variable name
48        * @return bare var type
49        * @throw std::invalid_argument if the variable has not been
50        * declared
51        */
52       bare_expr_type get_bare_type(const std::string& name) const;
53 
54       /**
55        * Return the scope in which the variable is declared for the
56        * variable with the specified name.
57        *
58        * @param name variable name
59        * @return scope of the variable
60        * @throw std::invalid_argument if the variable has not been
61        * declared
62        */
63       scope get_scope(const std::string& name) const;
64 
65       /**
66        * Add the specified declaration for a variable with the
67        * specified name in the specified scope.
68        * Destructively overwrites the declaration of an existing
69        * variable if called with a new declaration and scope.
70        *
71        * @param name variable name
72        * @param var_decl variable declaration
73        * @param scope_decl declaration scope
74        */
75       void add(const std::string& name,
76                const var_decl& var_decl,
77                const scope& scope_decl);
78 
79       /**
80        * Remove the declaraiton for the variable with the specified
81        * name.  If the variable had not already been declared, it
82        * the function exits silently.
83        *
84        * @param name name of variable to remove
85        */
86       void remove(const std::string& name);
87 
88       /**
89        * The stored map from function names to their declarations and
90        * origins.
91        */
92       std::map<std::string, range_t> map_;
93 
94       size_t size() const;
95     };
96   }
97 }
98 #endif
99