GNU Octave  6.2.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-2021 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 
35 class octave_function;
36 class octave_map;
37 class octave_user_code;
38 class octave_user_script;
39 class octave_value;
40 class octave_value_list;
41 
42 #include "quit.h"
43 
44 #include "stack-frame.h"
45 #include "symscope.h"
46 
47 namespace octave
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  size_t current_frame (void) const { return m_curr_frame; }
88 
89  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 
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.
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 
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 
164  void push (octave_user_script *script);
165 
166  void push (octave_function *fcn);
167 
168  void set_location (int l, int c)
169  {
170  if (! m_cs.empty ())
171  {
172  std::shared_ptr<stack_frame> elt = m_cs.back ();
173 
174  elt->line (l);
175  elt->column (c);
176  }
177  }
178 
179  void set_line (int l)
180  {
181  if (! m_cs.empty ())
182  {
183  std::shared_ptr<stack_frame> elt = m_cs.back ();
184 
185  elt->line (l);
186  }
187  }
188 
189  void set_column (int c)
190  {
191  if (! m_cs.empty ())
192  {
193  std::shared_ptr<stack_frame> elt = m_cs.back ();
194 
195  elt->column (c);
196  }
197  }
198 
199  bool goto_frame (size_t n = 0, bool verbose = false);
200 
201  void restore_frame (size_t n)
202  {
203  goto_frame (n);
204  }
205 
206  size_t find_current_user_frame (void) const;
207 
208  std::shared_ptr<stack_frame> current_user_frame (void) const;
209 
210  size_t dbupdown (size_t start, int n, bool verbose);
211  size_t dbupdown (int n = -1, bool verbose = false);
212 
213  void goto_caller_frame (void);
214 
215  void goto_base_frame (void);
216 
217  std::list<std::shared_ptr<stack_frame>>
218  backtrace_frames (octave_idx_type& curr_user_frame) const;
219 
220  // List of raw stack frames.
221 
222  std::list<std::shared_ptr<stack_frame>> backtrace_frames (void) const;
223 
224  // List of stack_info objects that can be used in liboctave and
225  // stored in the execution_exception object.
226 
227  std::list<frame_info> backtrace_info (octave_idx_type& curr_user_frame,
228  bool print_subfn = true) const;
229 
230  std::list<frame_info> backtrace_info (void) const;
231 
232  // The same as backtrace_info but in the form of a struct array
233  // object that may be used in the interpreter.
234 
235  octave_map backtrace (octave_idx_type& curr_user_frame,
236  bool print_subfn = true) const;
237 
238  octave_map backtrace (void) const;
239 
240  octave_map empty_backtrace (void) const;
241 
242  void pop (void);
243 
244  void clear (void);
245 
246  symbol_info_list all_variables (void);
247 
248  std::list<std::string> global_variable_names (void) const;
249 
250  std::list<std::string> top_level_variable_names (void) const;
251 
252  std::list<std::string> variable_names (void) const;
253 
254  void clear_global_variable (const std::string& name);
255 
256  void clear_global_variable_pattern (const std::string& pattern);
257 
258  void clear_global_variable_regexp(const std::string& pattern);
259 
260  void clear_global_variables (void);
261 
262  symbol_info_list glob_symbol_info (const std::string& pattern) const;
263 
264  symbol_info_list regexp_symbol_info (const std::string& pattern) const;
265 
266  symbol_info_list get_symbol_info (void);
267 
268  symbol_info_list top_scope_symbol_info (void) const;
269 
270  octave_value max_stack_depth (const octave_value_list& args, int nargout);
271 
272  void make_persistent (const symbol_record& sym);
273 
274  void make_global (const symbol_record& sym);
275 
276  octave_value global_varval (const std::string& name) const;
277 
278  octave_value& global_varref (const std::string& name);
279 
280  octave_value get_top_level_value (const std::string& name) const;
281 
282  void set_top_level_value (const std::string& name,
283  const octave_value& value);
284 
285  octave_value do_who (int argc, const string_vector& argv,
286  bool return_list, bool verbose = false);
287 
288  octave_value do_who_two (const string_vector& patterns, bool have_regexp,
289  bool return_list, bool verbose,
290  const std::string& msg = "");
291 
292  octave_value do_global_who_two (const string_vector& patterns,
293  bool have_regexp, bool return_list,
294  bool verbose, const std::string& msg = "");
295 
296  void display (void) const;
297 
298  void set_auto_fcn_var (stack_frame::auto_var_type avt,
299  const octave_value& val);
300 
301  octave_value get_auto_fcn_var (stack_frame::auto_var_type avt) const;
302 
303  private:
304 
305  void get_new_frame_index_and_links
306  (size_t& new_frame_idx, std::shared_ptr<stack_frame>& parent_link,
307  std::shared_ptr<stack_frame>& static_link) const;
308 
310 
311  // The list of stack frames.
313 
314  // The current frame. When a new frame is pushed, m_curr_frame
315  // moves to the end of the list of stack frames (also referred to as
316  // the top of the call stack) but may be temporarily moved to
317  // another location by evalin or debugging functions.
318 
319  // FIXME: should the current frame be managed by the evaluator
320  // instead?
321  size_t m_curr_frame;
322 
324 
325  std::map<std::string, octave_value> m_global_values;
326  };
327 }
328 
329 #endif
size_t size(void) const
Definition: call-stack.h:89
bool at_top_level(void) const
Definition: call-stack.h:108
tree_evaluator & m_evaluator
Definition: call-stack.h:309
size_t current_frame(void) const
Definition: call-stack.h:87
std::deque< std::shared_ptr< stack_frame > > stack_frames
Definition: call-stack.h:59
std::map< std::string, octave_value > m_global_values
Definition: call-stack.h:325
stack_frames::reverse_iterator reverse_iterator
Definition: call-stack.h:64
void set_line(int l)
Definition: call-stack.h:179
void restore_frame(size_t n)
Definition: call-stack.h:201
void set_location(int l, int c)
Definition: call-stack.h:168
octave_function * caller_function(void) const
Definition: call-stack.h:76
stack_frames m_cs
Definition: call-stack.h:312
stack_frames::const_reverse_iterator const_reverse_iterator
Definition: call-stack.h:65
symbol_scope top_scope(void) const
Definition: call-stack.h:96
stack_frames::iterator iterator
Definition: call-stack.h:61
octave_function * element(size_t n)
Definition: call-stack.h:115
void set_column(int c)
Definition: call-stack.h:189
std::shared_ptr< stack_frame > get_current_stack_frame(void) const
Definition: call-stack.h:91
symbol_scope current_scope(void) const
Definition: call-stack.h:101
stack_frames::const_iterator const_iterator
Definition: call-stack.h:62
std::map< std::string, octave_value > local_vars_map
Definition: stack-frame.h:112
QString name
octave_idx_type n
Definition: mx-inlines.cc:753
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
static octave::unwind_protect * curr_fcn_unwind_protect_frame(void)
Definition: variables.cc:584