GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
stack-frame.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_stack_frame_h)
27#define octave_stack_frame_h 1
28
29#include "octave-config.h"
30
31#include <deque>
32#include <iosfwd>
33#include <list>
34#include <map>
35#include <memory>
36#include <string>
37
38class octave_value;
40
41#include "error.h"
42#include "ov-fcn.h"
43#include "ov-usr-fcn.h"
44#include "syminfo.h"
45#include "symscope.h"
46
47// Variable values are stored in the stack_frame objects that make up
48// the call_stack. There are four separate stack_frame objects
49// corresponding to the following language elements:
50//
51// * user-defined functions
52//
53// These are .m files. They have local variables.
54//
55// * scripts
56//
57// These are .m files, but not functions. They access variables,
58// but do not store any values directly. All values are stored in
59// the stack frame corresponding to the scope in which they are
60// executed.
61//
62// * scopes that do not correspond to functions
63//
64// This is primarily used by the top-level scope but the
65// interpreter may also create temporary scopes in which to
66// evaluate functions or scripts.
67//
68// * compiled functions
69//
70// These are built-in functions and dynamically-loaded compiled
71// functions (.mex and .oct files) and do not contain variable
72// values of their own. They are skipped when Octave displays a
73// stack trace.
74//
75// All stack frames also contain the following data:
76//
77// * a reference to the evaluator that contains the frame
78//
79// Global variables are now stored in the evaluator and this link
80// gives us immediate access to them.
81//
82// * line and column in the source file where the stack frame was created
83//
84// These values are used to print stack traces.
85//
86// * A pointer to the nearest parent frame that contains variable
87// info (the "static" link)
88//
89// A frame that contains variable info may be a user-defined
90// function, script, or scope frame. This pointer should never
91// point to a compiled function stack frame.
92//
93// * A pointer to the nearest lexical parent frame (the "access" link)
94//
95// Used to access non-local variables for nested and anonymous
96// functions or as a link to the parent frame in which a script is
97// executed. This pointer should only point to a parent function
98// stack frame.
99
101
102class tree_evaluator;
103class symbol_info_list;
104class unwind_protect;
105
106class stack_frame_walker;
107
109{
110public:
111
112 typedef std::map<std::string, octave_value> local_vars_map;
113
114 // Markers indicating the type of a variable. Values for local
115 // variables are stored in the stack frame. Values for
116 // global variables are stored in the tree_evaluator object that
117 // contains the stack frame. Values for persistent variables are
118 // stored in the function scope corresponding to the stack frame.
119
126
127 // Index into the list of automatic variables for user-defined
128 // function stack frames.
129
139
140 stack_frame () = delete;
141
143 const std::shared_ptr<stack_frame>& parent_link,
144 const std::shared_ptr<stack_frame>& static_link,
145 const std::shared_ptr<stack_frame>& access_link)
146 : m_evaluator (tw), m_is_closure_context (false),
147 m_line (-1), m_column (-1), m_index (index),
150 { }
151
152 // Compiled function.
153 static stack_frame *
154 create (tree_evaluator& tw, octave_function *fcn, std::size_t index,
155 const std::shared_ptr<stack_frame>& parent_link,
156 const std::shared_ptr<stack_frame>& static_link);
157
158 // Script.
159 static stack_frame *
160 create (tree_evaluator& tw, octave_user_script *script, std::size_t index,
161 const std::shared_ptr<stack_frame>& parent_link,
162 const std::shared_ptr<stack_frame>& static_link);
163
164 // User-defined function.
165 static stack_frame *
166 create (tree_evaluator& tw, octave_user_function *fcn, std::size_t index,
167 const std::shared_ptr<stack_frame>& parent_link,
168 const std::shared_ptr<stack_frame>& static_link,
169 const std::shared_ptr<stack_frame>& access_link = std::shared_ptr<stack_frame> ());
170
171 // Anonymous user-defined function with init vars.
172 static stack_frame *
173 create (tree_evaluator& tw, octave_user_function *fcn, std::size_t index,
174 const std::shared_ptr<stack_frame>& parent_link,
175 const std::shared_ptr<stack_frame>& static_link,
176 const local_vars_map& local_vars,
177 const std::shared_ptr<stack_frame>& access_link = std::shared_ptr<stack_frame> ());
178
179 // Scope.
180 static stack_frame *
181 create (tree_evaluator& tw, const symbol_scope& scope, std::size_t index,
182 const std::shared_ptr<stack_frame>& parent_link,
183 const std::shared_ptr<stack_frame>& static_link);
184
185 stack_frame (const stack_frame& elt) = default;
186
187 stack_frame& operator = (const stack_frame& elt) = delete;
188
189 virtual ~stack_frame () = default;
190
191 // FIXME: It would be nice to eliminate these but there are a few
192 // places where we still need to know the specific type of the
193 // stack frame that we are handling.
194
195 virtual bool is_compiled_fcn_frame () const { return false; }
196 virtual bool is_user_script_frame () const { return false; }
197 virtual bool is_user_fcn_frame () const { return false; }
198 virtual bool is_scope_frame () const { return false; }
199
200 virtual void clear_values ();
201
202 std::size_t index () const { return m_index; }
203
204 void line (int l) { m_line = l; }
205 virtual int line () const { return m_line; }
206
207 void column (int c) { m_column = c; }
208 virtual int column () const { return m_column; }
209
210 std::string fcn_file_name () const
211 {
212 octave_function *fcn = function ();
213
214 return fcn ? fcn->fcn_file_name () : "";
215 }
216
217 std::string fcn_name (bool print_subfn = true) const
218 {
219 std::string retval;
220
221 octave_function *fcn = function ();
222
223 if (fcn)
224 {
225 std::string parent_fcn_name = fcn->parent_fcn_name ();
226
227 if (print_subfn && ! parent_fcn_name.empty ())
228 retval = parent_fcn_name + '>';
229
230 if (fcn->is_anonymous_function ())
231 retval += "@<anonymous>";
232 else
233 retval += fcn->name ();
234 }
235 else
236 retval = "<unknown>";
237
238 return retval;
239 }
240
241 virtual symbol_scope get_scope () const = 0;
242
243 virtual octave_function * function () const { return nullptr; }
244
245 virtual unwind_protect * unwind_protect_frame () { return nullptr; }
246
248 make_symbol_info_list (const std::list<symbol_record>& symrec_list) const;
249
250 octave_value who (const string_vector& patterns, bool have_regexp,
251 bool return_list, bool verbose,
252 const std::string& whos_line_fmt,
253 const std::string& msg);
254
256
258
259 std::list<std::string> variable_names () const;
260
261 // Look for named symbol visible from current scope. Don't
262 // attempt to insert if missing.
263 virtual symbol_record lookup_symbol (const std::string&) const = 0;
264
265 // Look for named symbol visible from current scope. Attempt to
266 // insert if missing.
267 virtual symbol_record insert_symbol (const std::string&) = 0;
268
269 symbol_info_list glob_symbol_info (const std::string& pattern);
270
271 symbol_info_list regexp_symbol_info (const std::string& pattern);
272
274 {
275 return all_variables ();
276 }
277
279 {
280 if (sym.is_formal ())
281 {
282 std::string nm = sym.name ();
283 error ("can't make function parameter %s persistent", nm.c_str ());
284 }
285
286 if (is_global (sym))
287 {
288 std::string nm = sym.name ();
289 error ("can't make global variable '%s' persistent", nm.c_str ());
290 }
291
292 install_variable (sym, octave_value (), false);
293
294 mark_persistent (sym);
295 }
296
297 void make_global (const symbol_record& sym)
298 {
299 if (is_persistent (sym))
300 {
301 std::string nm = sym.name ();
302 error ("can't make persistent variable '%s' global", nm.c_str ());
303 }
304
305 install_variable (sym, octave_value (), true);
306
307 mark_global (sym);
308 }
309
311 {
312 m_parent_link = nullptr;
313 m_static_link = nullptr;
314 }
315
316 std::size_t
317 parent_frame_index () const { return m_parent_link->index (); }
318
319 std::shared_ptr<stack_frame>
320 parent_link () const {return m_parent_link; }
321
322 std::shared_ptr<stack_frame>
323 static_link () const {return m_static_link; }
324
325 std::shared_ptr<stack_frame>
326 access_link () const {return m_access_link; }
327
328 virtual std::size_t size () const;
329
330 virtual void resize (std::size_t);
331
332 void mark_global (const symbol_record& sym)
333 {
334 mark_scope (sym, GLOBAL);
335 }
336
337 void unmark_global (const symbol_record& sym)
338 {
339 mark_scope (sym, LOCAL);
340 }
341
343 {
344 mark_scope (sym, PERSISTENT);
345 }
346
348 {
349 mark_scope (sym, LOCAL);
350 }
351
352 bool is_defined (const symbol_record& sym) const
353 {
354 octave_value val = varval (sym);
355
356 return val.is_defined ();
357 }
358
359 bool is_variable (const symbol_record& sym) const
360 {
361 octave_value val = varval (sym);
362
363 return val.is_defined ();
364 }
365
366 bool is_variable (const std::string& name) const
367 {
368 symbol_record sym = lookup_symbol (name);
369
370 return sym ? is_variable (sym) : false;
371 }
372
373 bool is_local_variable (const std::string& name) const
374 {
375 symbol_record sym = lookup_symbol (name);
376
377 return sym ? (is_variable (sym) && ! is_global (sym)) : false;
378 }
379
380 bool is_object (const symbol_record& sym) const
381 {
382 octave_value val = varval (sym);
383
384 return val.isobject ();
385 }
386
387 bool is_object (const std::string& name) const
388 {
389 symbol_record sym = lookup_symbol (name);
390
391 return sym ? is_object (sym) : false;
392 }
393
394 virtual scope_flags scope_flag (const symbol_record&) const = 0;
395
396 virtual scope_flags get_scope_flag (std::size_t) const;
397
398 virtual void set_scope_flag (std::size_t, scope_flags);
399
400 bool is_global (const symbol_record& sym) const
401 {
402 return scope_flag (sym) == GLOBAL;
403 }
404
405 bool is_global (const std::string& name) const
406 {
407 symbol_record sym = lookup_symbol (name);
408
409 return sym ? is_global (sym) : false;
410 }
411
412 bool is_persistent (const symbol_record& sym) const
413 {
414 return scope_flag (sym) == PERSISTENT;
415 }
416
417 bool is_persistent (const std::string& name) const
418 {
419 symbol_record sym = lookup_symbol (name);
420
421 return sym ? is_persistent (sym) : false;
422 }
423
424 void install_variable (const symbol_record& sym,
425 const octave_value& value, bool global);
426
427 void install_variable (const std::string& name,
428 const octave_value& value, bool global)
429 {
430 symbol_record sym = insert_symbol (name);
431
432 install_variable (sym, value, global);
433 }
434
436
437 virtual void set_auto_fcn_var (auto_var_type, const octave_value&) = 0;
438
439 virtual void set_nargin (int nargin) { set_auto_fcn_var (NARGIN, nargin); }
440 virtual void set_nargout (int nargout) { set_auto_fcn_var (NARGOUT, nargout); }
441
442 virtual octave_value varval (const symbol_record& sym) const = 0;
443
444 virtual octave_value varval (std::size_t data_offset) const;
445
446 octave_value varval (const std::string& name) const
447 {
448 symbol_record sym = lookup_symbol (name);
449
450 return sym ? varval (sym) : octave_value ();
451 }
452
453
454 virtual octave_value& varref (const symbol_record& sym) = 0;
455
456 virtual octave_value& varref (std::size_t data_offset);
457
458 virtual std::string inputname (int n, bool ids_only) const;
459
460 void assign (const symbol_record& sym, const octave_value& val)
461 {
462 octave_value& lhs = varref (sym);
463
464 if (lhs.get_count () == 1)
466
467 // Regularize a null matrix if stored into a variable.
468 lhs = val.storable_value ();
469 }
470
471 void assign (const std::string& name, const octave_value& val)
472 {
473 symbol_record sym = insert_symbol (name);
474
475 assign (sym, val);
476 }
477
479 const std::string& type,
480 const std::list<octave_value_list>& idx,
481 const octave_value& rhs)
482 {
483 if (idx.empty ())
484 {
485 if (op == octave_value::op_asn_eq)
486 assign (sym, rhs);
487 else
488 varref (sym).assign (op, rhs);
489 }
490 else
491 varref (sym).assign (op, type, idx, rhs);
492 }
493
495 const symbol_record& sym,
496 const std::string& type,
497 const std::list<octave_value_list>& idx)
498 {
499 if (idx.empty ())
500 varref (sym).non_const_unary_op (op);
501 else
502 varref (sym).non_const_unary_op (op, type, idx);
503 }
504
505 octave_value value (const symbol_record& sym, const std::string& type,
506 const std::list<octave_value_list>& idx) const
507 {
508 octave_value retval = varval (sym);
509
510 if (! idx.empty ())
511 {
512 if (retval.is_constant ())
513 retval = retval.subsref (type, idx);
514 else
515 {
516 octave_value_list t = retval.subsref (type, idx, 1);
517
518 retval = t.length () > 0 ? t(0) : octave_value ();
519 }
520 }
521
522 return retval;
523 }
524
525 octave_value find_subfunction (const std::string& name) const
526 {
527 symbol_scope scope = get_scope ();
528
529 return scope.find_subfunction (name);
530 }
531
532 void clear (const symbol_record& sym)
533 {
534 if (is_global (sym))
535 unmark_global (sym);
536
537 assign (sym, octave_value ());
538
539 if (is_persistent (sym))
540 unmark_persistent (sym);
541 }
542
543 void clear_objects ();
544
545 void clear_variable (const std::string& name);
546
547 void clear_variable_pattern (const std::string& pattern);
548 void clear_variable_pattern (const string_vector& patterns);
549
550 void clear_variable_regexp (const std::string& pattern);
551 void clear_variable_regexp (const string_vector& patterns);
552
553 void clear_variables ();
554
555 std::string get_dispatch_class () const { return m_dispatch_class; }
556
557 void set_dispatch_class (const std::string& class_name)
558 {
559 m_dispatch_class = class_name;
560 }
561
562 void display_stopped_in_message (std::ostream& os) const;
563
564 void debug_where (std::ostream& os) const
565 {
567 }
568
569 void debug_list (std::ostream& os, int num_lines) const;
570
571 void debug_type (std::ostream& os, int start_line, int end_line) const;
572
573 virtual void mark_scope (const symbol_record&, scope_flags) = 0;
574
575 virtual void display (bool follow = true) const;
576
577 virtual void accept (stack_frame_walker& sfw) = 0;
578
579 virtual void break_closure_cycles (const std::shared_ptr<stack_frame>&) { }
580
582 {
584
585 // Mark any access linked frames as closure contexts too,
586 // so that they'll make any function handle on its stack frame
587 // weak when the frame itself is being popped.
588 auto nxt = access_link ();
589 while (nxt)
590 {
591 nxt->m_is_closure_context = true;
592 nxt = nxt->access_link ();
593 }
594 }
595
596 bool is_closure_context () const { return m_is_closure_context; }
597
598protected:
599
600 // Reference to the call stack that contains this frame. Global
601 // variables are stored in the call stack. This link gives us
602 // immediate access to them.
604
605 // TRUE if this stack frame is saved with a handle to a nested
606 // function (closure).
608
609 // The line and column of the source file where this stack frame
610 // was created. Used to print stack traces.
613
614 // Index in call stack.
615 std::size_t m_index;
616
617 // Pointer to the nearest parent frame. May include compiled
618 // functions.
619 std::shared_ptr<stack_frame> m_parent_link;
620
621 // Pointer to the nearest parent frame that contains variable
622 // information (script, function, or scope). This link skips over
623 // compiled function parent frames.
624 std::shared_ptr<stack_frame> m_static_link;
625
626 // Pointer to the nearest lexical parent frame. Used to access
627 // non-local variables for nested and anonymous functions or as a
628 // link to the parent frame in which a script is executed.
629 std::shared_ptr<stack_frame> m_access_link;
630
631 // Allow function handles to temporarily store their dispatch class
632 // in the call stack.
633 std::string m_dispatch_class;
634};
635
636OCTAVE_END_NAMESPACE(octave)
637
638#endif
virtual bool is_anonymous_function() const
Definition ov-base.h:539
virtual std::string parent_fcn_name() const
Definition ov-fcn.h:84
virtual std::string fcn_file_name() const
Definition ov-fcn.h:74
std::string name() const
Definition ov-fcn.h:208
octave_idx_type length() const
Definition ovl.h:111
void call_object_destructor()
Definition ov.h:1452
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition ov.h:476
bool is_constant() const
Definition ov.h:765
bool is_defined() const
Definition ov.h:592
@ op_asn_eq
Definition ov.h:135
octave_value storable_value() const
octave_value & non_const_unary_op(unary_op op)
octave_value & assign(assign_op op, const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
octave_idx_type get_count() const
Definition ov.h:421
bool isobject() const
Definition ov.h:664
virtual scope_flags get_scope_flag(std::size_t) const
virtual bool is_scope_frame() const
octave_value workspace()
symbol_info_list glob_symbol_info(const std::string &pattern)
std::string fcn_file_name() const
bool is_persistent(const std::string &name) const
virtual bool is_user_script_frame() const
virtual octave_function * function() const
std::list< std::string > variable_names() const
virtual void set_nargout(int nargout)
void unmark_persistent(const symbol_record &sym)
void clear_variables()
void line(int l)
void set_dispatch_class(const std::string &class_name)
bool is_object(const std::string &name) const
void make_global(const symbol_record &sym)
virtual octave_value varval(const symbol_record &sym) const =0
void clear(const symbol_record &sym)
void clear_objects()
static stack_frame * create(tree_evaluator &tw, octave_function *fcn, std::size_t index, const std::shared_ptr< stack_frame > &parent_link, const std::shared_ptr< stack_frame > &static_link)
std::shared_ptr< stack_frame > static_link() const
octave_value value(const symbol_record &sym, const std::string &type, const std::list< octave_value_list > &idx) const
stack_frame(const stack_frame &elt)=default
bool is_global(const symbol_record &sym) const
std::string m_dispatch_class
void clear_variable_regexp(const std::string &pattern)
void mark_global(const symbol_record &sym)
std::size_t m_index
virtual int line() const
virtual octave_value & varref(const symbol_record &sym)=0
void clear_variable_pattern(const std::string &pattern)
virtual symbol_record lookup_symbol(const std::string &) const =0
virtual int column() const
void debug_type(std::ostream &os, int start_line, int end_line) const
std::size_t index() const
bool is_global(const std::string &name) const
octave_value find_subfunction(const std::string &name) const
void install_variable(const std::string &name, const octave_value &value, bool global)
symbol_info_list regexp_symbol_info(const std::string &pattern)
virtual void display(bool follow=true) const
stack_frame()=delete
virtual void set_scope_flag(std::size_t, scope_flags)
bool is_defined(const symbol_record &sym) const
std::shared_ptr< stack_frame > access_link() const
virtual void accept(stack_frame_walker &sfw)=0
void display_stopped_in_message(std::ostream &os) const
void debug_where(std::ostream &os) const
octave_value varval(const std::string &name) const
std::shared_ptr< stack_frame > m_parent_link
bool is_closure_context() const
void clear_parent_static_link()
std::shared_ptr< stack_frame > parent_link() const
virtual void set_auto_fcn_var(auto_var_type, const octave_value &)=0
void assign(octave_value::assign_op op, const symbol_record &sym, const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
bool is_persistent(const symbol_record &sym) const
virtual unwind_protect * unwind_protect_frame()
std::shared_ptr< stack_frame > m_access_link
virtual bool is_user_fcn_frame() const
void non_const_unary_op(octave_value::unary_op op, const symbol_record &sym, const std::string &type, const std::list< octave_value_list > &idx)
virtual ~stack_frame()=default
std::string fcn_name(bool print_subfn=true) const
void column(int c)
void assign(const symbol_record &sym, const octave_value &val)
virtual octave_value get_auto_fcn_var(auto_var_type) const =0
std::string get_dispatch_class() const
bool is_object(const symbol_record &sym) const
virtual bool is_compiled_fcn_frame() const
void install_variable(const symbol_record &sym, const octave_value &value, bool global)
void clear_variable(const std::string &name)
std::shared_ptr< stack_frame > m_static_link
std::map< std::string, octave_value > local_vars_map
virtual void mark_scope(const symbol_record &, scope_flags)=0
virtual void set_nargin(int nargin)
void assign(const std::string &name, const octave_value &val)
octave_value who(const string_vector &patterns, bool have_regexp, bool return_list, bool verbose, const std::string &whos_line_fmt, const std::string &msg)
void unmark_global(const symbol_record &sym)
stack_frame & operator=(const stack_frame &elt)=delete
virtual scope_flags scope_flag(const symbol_record &) const =0
virtual std::string inputname(int n, bool ids_only) const
bool is_local_variable(const std::string &name) const
tree_evaluator & m_evaluator
virtual void break_closure_cycles(const std::shared_ptr< stack_frame > &)
symbol_info_list get_symbol_info()
void mark_persistent(const symbol_record &sym)
virtual symbol_record insert_symbol(const std::string &)=0
void mark_closure_context()
stack_frame(tree_evaluator &tw, std::size_t index, const std::shared_ptr< stack_frame > &parent_link, const std::shared_ptr< stack_frame > &static_link, const std::shared_ptr< stack_frame > &access_link)
bool m_is_closure_context
virtual std::size_t size() const
symbol_info_list all_variables()
virtual void clear_values()
std::size_t parent_frame_index() const
virtual symbol_scope get_scope() const =0
bool is_variable(const std::string &name) const
virtual void resize(std::size_t)
void make_persistent(const symbol_record &sym)
bool is_variable(const symbol_record &sym) const
void debug_list(std::ostream &os, int num_lines) const
symbol_info_list make_symbol_info_list(const std::list< symbol_record > &symrec_list) const
std::string name() const
Definition symrec.h:218
bool is_formal() const
Definition symrec.h:223
octave_value find_subfunction(const std::string &name) const
Definition symscope.h:526
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition error.cc:1003