GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
call-stack.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1993-2022 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if ! defined (octave_call_stack_h)
27#define octave_call_stack_h 1
28
29#include "octave-config.h"
30
31#include <deque>
32#include <memory>
33#include <string>
34
35class octave_function;
36class octave_map;
39class octave_value;
41
42#include "quit.h"
43
44#include "stack-frame.h"
45#include "symscope.h"
46
47OCTAVE_NAMESPACE_BEGIN
48
49 class tree_evaluator;
50 class symbol_info_list;
51 class unwind_protect;
52
53 class
54 OCTINTERP_API
56 {
57 public:
58
59 typedef std::deque<std::shared_ptr<stack_frame>> stack_frames;
60
61 typedef stack_frames::iterator iterator;
62 typedef stack_frames::const_iterator const_iterator;
63
64 typedef stack_frames::reverse_iterator reverse_iterator;
65 typedef stack_frames::const_reverse_iterator const_reverse_iterator;
66
67 call_stack (tree_evaluator& evaluator);
68
69 // Lock current function. Look for the first stack frame that is
70 // a function. If SKIP_FIST is true, then skip the first frame.
71 // That allows functions like Fmlock to find and lock the calling
72 // function instead of locking Fmlock itself.
73
74 octave_function * current_function (bool skip_first = false) const;
75
77 {
78 return current_function (true);
79 }
80
81 // Current line in current function.
82 int current_line (void) const;
83
84 // Current column in current function.
85 int current_column (void) const;
86
87 std::size_t current_frame (void) const { return m_curr_frame; }
88
89 std::size_t size (void) const { return m_cs.size (); }
90
91 std::shared_ptr<stack_frame> get_current_stack_frame (void) const
92 {
93 return m_cs[m_curr_frame];
94 }
95
96 symbol_scope top_scope (void) const
97 {
98 return m_cs[0]->get_scope ();
99 }
100
101 symbol_scope current_scope (void) const
102 {
103 // FIXME: Can m_curr_frame ever be invalid?
104 return (m_curr_frame < m_cs.size ()
105 ? m_cs[m_curr_frame]->get_scope () : symbol_scope ());
106 }
107
108 bool at_top_level (void) const
109 {
110 return current_scope () == top_scope ();
111 }
112
113 // Function at location N on the call stack (N == 0 is current), may
114 // be built-in.
115 octave_function * element (std::size_t n)
116 {
117 octave_function *retval = nullptr;
118
119 if (m_cs.size () > n)
120 retval = m_cs[n]->function ();
121
122 return retval;
123 }
124
125 // User code caller.
126 octave_user_code * current_user_code (void) const;
127
128 unwind_protect * curr_fcn_unwind_protect_frame (void);
129
130 // Line in user code caller.
131 int current_user_code_line (void) const;
132
133 // Column in user code caller.
134 int current_user_code_column (void) const;
135
136 // Current function that we are debugging.
137 octave_user_code * debug_user_code (void) const;
138
139 // Line number in current function that we are debugging.
140 int debug_user_code_line (void) const;
141
142 // Column number in current function that we are debugging.
143 int debug_user_code_column (void) const;
144
145 std::string get_dispatch_class (void) const;
146
147 void set_dispatch_class (const std::string& class_name);
148
149 bool is_class_method_executing (std::string& dispatch_class) const;
150
151 bool is_class_constructor_executing (std::string& dispatch_class) const;
152
153 // Return TRUE if all elements on the call stack are scripts.
154 bool all_scripts (void) const;
155
156 void push (const symbol_scope& scope);
157
158 void push (octave_user_function *fcn,
159 const std::shared_ptr<stack_frame>& closure_frames = std::shared_ptr<stack_frame> ());
160
161 void push (octave_user_function *fcn,
162 const stack_frame::local_vars_map& local_vars,
163 const std::shared_ptr<stack_frame>& closure_frames = std::shared_ptr<stack_frame> ());
164
165 void push (octave_user_script *script);
166
167 void push (octave_function *fcn);
168
169 void set_location (int l, int c)
170 {
171 if (! m_cs.empty ())
172 {
173 std::shared_ptr<stack_frame> elt = m_cs.back ();
174
175 elt->line (l);
176 elt->column (c);
177 }
178 }
179
180 void set_line (int l)
181 {
182 if (! m_cs.empty ())
183 {
184 std::shared_ptr<stack_frame> elt = m_cs.back ();
185
186 elt->line (l);
187 }
188 }
189
190 void set_column (int c)
191 {
192 if (! m_cs.empty ())
193 {
194 std::shared_ptr<stack_frame> elt = m_cs.back ();
195
196 elt->column (c);
197 }
198 }
199
200 bool goto_frame (std::size_t n = 0, bool verbose = false);
201
202 void restore_frame (std::size_t n)
203 {
204 goto_frame (n);
205 }
206
207 std::size_t find_current_user_frame (void) const;
208
209 std::shared_ptr<stack_frame> current_user_frame (void) const;
210
211 std::size_t dbupdown (std::size_t start, int n, bool verbose);
212 std::size_t dbupdown (int n = -1, bool verbose = false);
213
214 void goto_caller_frame (void);
215
216 void goto_base_frame (void);
217
218 std::list<std::shared_ptr<stack_frame>>
219 backtrace_frames (octave_idx_type& curr_user_frame) const;
220
221 // List of raw stack frames.
222
223 std::list<std::shared_ptr<stack_frame>> backtrace_frames (void) const;
224
225 // List of stack_info objects that can be used in liboctave and
226 // stored in the execution_exception object.
227
228 std::list<frame_info> backtrace_info (octave_idx_type& curr_user_frame,
229 bool print_subfn = true) const;
230
231 std::list<frame_info> backtrace_info (void) const;
232
233 // The same as backtrace_info but in the form of a struct array
234 // object that may be used in the interpreter.
235
236 octave_map backtrace (octave_idx_type& curr_user_frame,
237 bool print_subfn = true) const;
238
239 octave_map backtrace (void) const;
240
241 octave_map empty_backtrace (void) const;
242
243 void pop (void);
244
245 void clear (void);
246
247 symbol_info_list all_variables (void);
248
249 std::list<std::string> global_variable_names (void) const;
250
251 std::list<std::string> top_level_variable_names (void) const;
252
253 std::list<std::string> variable_names (void) const;
254
255 void clear_global_variable (const std::string& name);
256
257 void clear_global_variable_pattern (const std::string& pattern);
258
259 void clear_global_variable_regexp(const std::string& pattern);
260
261 void clear_global_variables (void);
262
263 symbol_info_list glob_symbol_info (const std::string& pattern) const;
264
265 symbol_info_list regexp_symbol_info (const std::string& pattern) const;
266
267 symbol_info_list get_symbol_info (void);
268
269 symbol_info_list top_scope_symbol_info (void) const;
270
271 octave_value max_stack_depth (const octave_value_list& args, int nargout);
272
273 void make_persistent (const symbol_record& sym);
274
275 void make_global (const symbol_record& sym);
276
277 octave_value global_varval (const std::string& name) const;
278
279 octave_value& global_varref (const std::string& name);
280
281 octave_value get_top_level_value (const std::string& name) const;
282
283 void set_top_level_value (const std::string& name,
284 const octave_value& value);
285
286 octave_value do_who (int argc, const string_vector& argv,
287 bool return_list, bool verbose = false);
288
289 octave_value do_who_two (const string_vector& patterns, bool have_regexp,
290 bool return_list, bool verbose,
291 const std::string& msg = "");
292
293 octave_value do_global_who_two (const string_vector& patterns,
294 bool have_regexp, bool return_list,
295 bool verbose, const std::string& msg = "");
296
297 void display (void) const;
298
299 void set_auto_fcn_var (stack_frame::auto_var_type avt,
300 const octave_value& val);
301
302 octave_value get_auto_fcn_var (stack_frame::auto_var_type avt) const;
303
304 private:
305
306 void get_new_frame_index_and_links
307 (std::size_t& new_frame_idx, std::shared_ptr<stack_frame>& parent_link,
308 std::shared_ptr<stack_frame>& static_link) const;
309
311
312 // The list of stack frames.
314
315 // The current frame. When a new frame is pushed, m_curr_frame
316 // moves to the end of the list of stack frames (also referred to as
317 // the top of the call stack) but may be temporarily moved to
318 // another location by evalin or debugging functions.
319
320 // FIXME: should the current frame be managed by the evaluator
321 // instead?
322 std::size_t m_curr_frame;
323
325
326 std::map<std::string, octave_value> m_global_values;
327 };
328
329OCTAVE_NAMESPACE_END
330
331#endif
std::map< std::string, octave_value > m_global_values
Definition: call-stack.h:326
stack_frames::const_reverse_iterator const_reverse_iterator
Definition: call-stack.h:65
void set_column(int c)
Definition: call-stack.h:190
symbol_scope current_scope(void) const
Definition: call-stack.h:101
std::size_t m_curr_frame
Definition: call-stack.h:322
octave_function * element(std::size_t n)
Definition: call-stack.h:115
stack_frames::iterator iterator
Definition: call-stack.h:61
void restore_frame(std::size_t n)
Definition: call-stack.h:202
std::size_t current_frame(void) const
Definition: call-stack.h:87
void set_location(int l, int c)
Definition: call-stack.h:169
tree_evaluator & m_evaluator
Definition: call-stack.h:310
std::shared_ptr< stack_frame > get_current_stack_frame(void) const
Definition: call-stack.h:91
octave_function * caller_function(void) const
Definition: call-stack.h:76
void set_line(int l)
Definition: call-stack.h:180
std::deque< std::shared_ptr< stack_frame > > stack_frames
Definition: call-stack.h:59
stack_frames::const_iterator const_iterator
Definition: call-stack.h:62
int m_max_stack_depth
Definition: call-stack.h:324
stack_frames m_cs
Definition: call-stack.h:313
stack_frames::reverse_iterator reverse_iterator
Definition: call-stack.h:64
bool at_top_level(void) const
Definition: call-stack.h:108
std::size_t size(void) const
Definition: call-stack.h:89
symbol_scope top_scope(void) const
Definition: call-stack.h:96
QString name
static octave::unwind_protect * curr_fcn_unwind_protect_frame(void)
Definition: variables.cc:563