GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
call-stack.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1993-2025 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
48
49class tree_evaluator;
51class unwind_protect;
52
53class OCTINTERP_API call_stack
54{
55public:
56
57 typedef std::deque<std::shared_ptr<stack_frame>> stack_frames;
58
59 typedef stack_frames::iterator iterator;
60 typedef stack_frames::const_iterator const_iterator;
61
62 typedef stack_frames::reverse_iterator reverse_iterator;
63 typedef stack_frames::const_reverse_iterator const_reverse_iterator;
64
65 call_stack (tree_evaluator& evaluator);
66
67 // Lock current function. Look for the first stack frame that is
68 // a function. If SKIP_FIST is true, then skip the first frame.
69 // That allows functions like Fmlock to find and lock the calling
70 // function instead of locking Fmlock itself.
71
72 octave_function * current_function (bool skip_first = false) const;
73
75 {
76 return current_function (true);
77 }
78
79 // Current line in current function.
80 int current_line () const;
81
82 // Current column in current function.
83 int current_column () const;
84
85 std::size_t current_frame () const { return m_curr_frame; }
86
87 std::size_t size () const { return m_cs.size (); }
88
89 std::shared_ptr<stack_frame> get_current_stack_frame () const
90 {
91 return m_cs[m_curr_frame];
92 }
93
95 {
96 return m_cs[0]->get_scope ();
97 }
98
100 {
101 // FIXME: Can m_curr_frame ever be invalid?
102 return (m_curr_frame < m_cs.size ()
103 ? m_cs[m_curr_frame]->get_scope () : symbol_scope::invalid ());
104 }
105
106 bool at_top_level () const
107 {
108 return current_scope () == top_scope ();
109 }
110
111 // Function at location N on the call stack (N == 0 is current), may
112 // be built-in.
113 octave_function * element (std::size_t n)
114 {
115 octave_function *retval = nullptr;
116
117 if (m_cs.size () > n)
118 retval = m_cs[n]->function ();
119
120 return retval;
121 }
122
123 // User code caller.
124 octave_user_code * current_user_code () const;
125
126 unwind_protect * curr_fcn_unwind_protect_frame ();
127
128 // Line in user code caller.
129 int current_user_code_line () const;
130
131 // Column in user code caller.
132 int current_user_code_column () const;
133
134 // Current function that we are debugging.
135 octave_user_code * debug_user_code () const;
136
137 // Line number in current function that we are debugging.
138 int debug_user_code_line () const;
139
140 // Column number in current function that we are debugging.
141 int debug_user_code_column () const;
142
143 std::string get_dispatch_class () const;
144
145 void set_dispatch_class (const std::string& class_name);
146
147 bool is_class_method_executing (std::string& dispatch_class) const;
148
149 bool is_class_constructor_executing (std::string& dispatch_class) const;
150
151 // Return TRUE if all elements on the call stack are scripts.
152 bool all_scripts () const;
153
154 void push (const symbol_scope& scope);
155
156 void push (octave_user_function *fcn,
157 const std::shared_ptr<stack_frame>& closure_frames = std::shared_ptr<stack_frame> ());
158
159 void push (octave_user_function *fcn,
160 const stack_frame::local_vars_map& local_vars,
161 const std::shared_ptr<stack_frame>& closure_frames = std::shared_ptr<stack_frame> ());
162
163 void push (octave_user_script *script);
164
165 void push (octave_function *fcn);
166
167 void set_location (int l, int c)
168 {
169 if (! m_cs.empty ())
170 {
171 std::shared_ptr<stack_frame> elt = m_cs.back ();
172
173 elt->line (l);
174 elt->column (c);
175 }
176 }
177
178 void set_line (int l)
179 {
180 if (! m_cs.empty ())
181 {
182 std::shared_ptr<stack_frame> elt = m_cs.back ();
183
184 elt->line (l);
185 }
186 }
187
188 void set_column (int c)
189 {
190 if (! m_cs.empty ())
191 {
192 std::shared_ptr<stack_frame> elt = m_cs.back ();
193
194 elt->column (c);
195 }
196 }
197
198 bool goto_frame (std::size_t n = 0, bool verbose = false);
199
200 void restore_frame (std::size_t n)
201 {
202 goto_frame (n);
203 }
204
205 std::size_t find_current_user_frame () const;
206
207 std::shared_ptr<stack_frame> current_user_frame () const;
208
209 std::size_t dbupdown (std::size_t start, int n, bool verbose);
210 std::size_t dbupdown (int n = -1, bool verbose = false);
211
212 void goto_caller_frame ();
213
214 void goto_base_frame ();
215
216 std::list<std::shared_ptr<stack_frame>>
217 backtrace_frames (octave_idx_type& curr_user_frame) const;
218
219 // List of raw stack frames.
220
221 std::list<std::shared_ptr<stack_frame>> backtrace_frames () const;
222
223 // List of stack_info objects that can be used in liboctave and
224 // stored in the execution_exception object.
225
226 std::list<frame_info> backtrace_info (octave_idx_type& curr_user_frame,
227 bool print_subfn = true) const;
228
229 std::list<frame_info> backtrace_info () const;
230
231 // The same as backtrace_info but in the form of a struct array
232 // object that may be used in the interpreter.
233
234 octave_map backtrace (octave_idx_type& curr_user_frame,
235 bool print_subfn = true) const;
236
237 octave_map backtrace () const;
238
239 octave_map empty_backtrace () const;
240
241 void pop ();
242
243 std::shared_ptr<stack_frame> pop_return ();
244
245 void clear ();
246
247 symbol_info_list all_variables ();
248
249 std::list<std::string> global_variable_names () const;
250
251 std::list<std::string> top_level_variable_names () const;
252
253 std::list<std::string> variable_names () 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 ();
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 ();
268
269 symbol_info_list top_scope_symbol_info () 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 () const;
298
299 void set_auto_fcn_var (stack_frame::auto_var_type avt,
300 const octave_value& val);
301
302 void set_nargin (int nargin);
303 void set_nargout (int nargout);
304
305 octave_value get_auto_fcn_var (stack_frame::auto_var_type avt) const;
306
307private:
308
309 void get_new_frame_index_and_links
310 (std::size_t& new_frame_idx, std::shared_ptr<stack_frame>& parent_link,
311 std::shared_ptr<stack_frame>& static_link) const;
312
313 tree_evaluator& m_evaluator;
314
315 // The list of stack frames.
316 stack_frames m_cs;
317
318 // The current frame. When a new frame is pushed, m_curr_frame
319 // moves to the end of the list of stack frames (also referred to as
320 // the top of the call stack) but may be temporarily moved to
321 // another location by evalin or debugging functions.
322
323 // FIXME: should the current frame be managed by the evaluator
324 // instead?
325 std::size_t m_curr_frame;
326
327 int m_max_stack_depth;
328
329 std::map<std::string, octave_value> m_global_values;
330};
331
332OCTAVE_END_NAMESPACE(octave)
333
334#endif
std::shared_ptr< stack_frame > get_current_stack_frame() const
Definition call-stack.h:89
stack_frames::const_reverse_iterator const_reverse_iterator
Definition call-stack.h:63
void set_column(int c)
Definition call-stack.h:188
octave_function * element(std::size_t n)
Definition call-stack.h:113
stack_frames::iterator iterator
Definition call-stack.h:59
std::size_t size() const
Definition call-stack.h:87
void restore_frame(std::size_t n)
Definition call-stack.h:200
bool at_top_level() const
Definition call-stack.h:106
std::size_t current_frame() const
Definition call-stack.h:85
octave_function * caller_function() const
Definition call-stack.h:74
void set_location(int l, int c)
Definition call-stack.h:167
symbol_scope current_scope() const
Definition call-stack.h:99
void set_line(int l)
Definition call-stack.h:178
std::deque< std::shared_ptr< stack_frame > > stack_frames
Definition call-stack.h:57
stack_frames::const_iterator const_iterator
Definition call-stack.h:60
symbol_scope top_scope() const
Definition call-stack.h:94
stack_frames::reverse_iterator reverse_iterator
Definition call-stack.h:62
std::map< std::string, octave_value > local_vars_map
static symbol_scope invalid()
Definition symscope.h:400
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn