1 #ifndef STAN_LANG_AST_SCOPE_HPP
2 #define STAN_LANG_AST_SCOPE_HPP
3 
4 
5 #include <stan/lang/ast/origin_block.hpp>
6 #include <cstddef>
7 
8 namespace stan {
9   namespace lang {
10 
11     /**
12      * Structure which tracks enclosing program block(s) encountered by parser.
13      * Var_map records program block where variable declared.
14      * Grammar rules check allowed constructs in (enclosing) block.
15      */
16     class scope {
17       /**
18        * Outermost enclosing program block.
19        */
20       origin_block program_block_;
21 
22       /**
23        * True if in a nested (local) program block.
24        */
25       bool is_local_;
26 
27     public:
28       /**
29        * No arg constructor, defaults:
30        * - \p program_block_ : model_name_origin
31        * - \p is_local_ : \c false
32        */
33       scope();
34 
35       /**
36        * Construct an origin for variable in a specified block.
37        * Default \c is_local is \c false, i.e., not in a local block.
38        *
39        * @param program_block enclosing program block
40        */
41       scope(const origin_block& program_block);   // NOLINT(runtime/explicit)
42 
43       /**
44        * Construct scope for a variable in specified outer program block,
45        * specify whether or not variable is declared in a local block.
46        *
47        * @param program_block enclosing program block
48        * @param is_local true if declared in a local block
49        */
50       scope(const origin_block& program_block,
51                  const bool& is_local);
52 
53       /**
54        * Return value for outermost enclosing program block.
55        *
56        * @return program_block enclosing program block
57        */
58       origin_block program_block() const;
59 
60       /**
61        * Return true when declared in a nested (local) block,
62        * enclosing block can be any \c origin_block value.
63        *
64        * @return true when scope is nested (local) block.
65        */
66       bool is_local() const;
67 
68       /**
69        * Flags local scopes which permit parameter variables.
70        * Allows local blocks in functions, transfromed parameter,
71        * and model blocks; disallows local blocks in transformed data
72        * and generated quantities program blocks.
73        *
74        * @return true for local parameter origin block types
75        */
76       bool local_allows_var() const;
77 
78       /**
79        * Flags scopes where parameter variables are declared,
80        * i.e., top-level of parameter or transformed parameter block.
81        *
82        * @return true for top-level parameter origin block types
83        */
84       bool par_or_tpar() const;
85 
86       /**
87        * Return true when declared in transformed parameter block.
88        *
89        * @return true for transformed parameter origin block
90        */
91       bool tpar() const;
92 
93       /**
94        * Return true when declared as function argument.
95        *
96        * @return true for function origin block types
97        */
98       bool fun() const;
99 
100       /**
101        * Return true when declared as argument to non-void function.
102        *
103        * @return true for non void function origin block types
104        */
105       bool non_void_fun() const;
106 
107       /**
108        * Return true when declared as argument to void function.
109        *
110        * @return true for void function origin block types
111        */
112       bool void_fun() const;
113 
114       /**
115        * Return true when program block allows assignment to variables
116        * i.e., not data or parameter block
117        *
118        * @return true when program block allows access to LP
119        */
120       bool allows_assignment() const;
121 
122       /**
123        * Return true when program block allows access to LP function
124        *
125        * @return true when program block allows access to LP function
126        */
127       bool allows_lp_fun() const;
128 
129       /**
130        * Return true when program block allows access to RNG
131        * i.e., transformed data block or rng function
132        *
133        * @return true when program block allows access to RNG
134        */
135       bool allows_rng() const;
136 
137       /**
138        * Return true when program block allows access to sampling statement
139        *
140        * @return true when program block allows access to sampling statement
141        */
142       bool allows_sampling() const;
143 
144       /**
145        * Returns true for origin blocks where size-denoting expression
146        * declarations are allowed.  Origin blocks not allowed:
147        *  - parameters
148        *  - transformed parameters
149        *  - generated quantities
150        *
151        * @return true if origin block allows size-denoting variable declaration.
152        */
153       bool allows_size() const;
154     };
155 
156   }
157 }
158 #endif
159