comparison libinterp/corefcn/stack-frame.h @ 31607:aac27ad79be6 stable

maint: Re-indent code after switch to using namespace macros. * build-env.h, build-env.in.cc, Cell.h, __betainc__.cc, __eigs__.cc, __ftp__.cc, __ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __magick_read__.cc, __pchip_deriv__.cc, amd.cc, base-text-renderer.cc, base-text-renderer.h, besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.h, call-stack.cc, call-stack.h, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc, dasrt.cc, data.cc, debug.cc, defaults.cc, defaults.h, det.cc, display.cc, display.h, dlmread.cc, dynamic-ld.cc, dynamic-ld.h, ellipj.cc, environment.cc, environment.h, error.cc, error.h, errwarn.h, event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc, fcn-info.h, fft.cc, fft2.cc, file-io.cc, filter.cc, find.cc, ft-text-renderer.cc, ft-text-renderer.h, gcd.cc, gl-render.cc, gl-render.h, gl2ps-print.cc, gl2ps-print.h, graphics-toolkit.cc, graphics-toolkit.h, graphics.cc, gsvd.cc, gtk-manager.cc, gtk-manager.h, help.cc, help.h, hook-fcn.cc, hook-fcn.h, input.cc, input.h, interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h, inv.cc, jsondecode.cc, jsonencode.cc, latex-text-renderer.cc, latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h, lookup.cc, ls-hdf5.cc, ls-mat4.cc, ls-mat5.cc, lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex.cc, mexproto.h, mxarray.h, mxtypes.in.h, oct-errno.in.cc, oct-hdf5-types.cc, oct-hist.cc, oct-hist.h, oct-map.cc, oct-map.h, oct-opengl.h, oct-prcstrm.h, oct-process.cc, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h, oct-strstrm.h, octave-default-image.h, ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc, pow2.cc, pr-output.cc, psi.cc, qr.cc, quadcc.cc, rand.cc, regexp.cc, settings.cc, settings.h, sighandlers.cc, sighandlers.h, sparse-xpow.cc, sqrtm.cc, stack-frame.cc, stack-frame.h, stream-euler.cc, strfns.cc, svd.cc, syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h, symscope.cc, symscope.h, symtab.cc, symtab.h, sysdep.cc, sysdep.h, text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, time.cc, toplev.cc, typecast.cc, url-handle-manager.cc, url-handle-manager.h, urlwrite.cc, utils.cc, utils.h, variables.cc, variables.h, xdiv.cc, __delaunayn__.cc, __init_fltk__.cc, __init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audioread.cc, convhulln.cc, gzip.cc, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc, cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h, cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h, cdef-utils.cc, cdef-utils.h, ov-base-diag.cc, ov-base-int.cc, ov-base-mat.cc, ov-base-mat.h, ov-base-scalar.cc, ov-base.cc, ov-base.h, ov-bool-mat.cc, ov-bool-mat.h, ov-bool-sparse.cc, ov-bool.cc, ov-builtin.h, ov-cell.cc, ov-ch-mat.cc, ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h, ov-complex.cc, ov-cx-diag.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-dld-fcn.cc, ov-dld-fcn.h, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h, ov-float.cc, ov-flt-complex.cc, ov-flt-cx-diag.cc, ov-flt-cx-mat.cc, ov-flt-re-diag.cc, ov-flt-re-mat.cc, ov-flt-re-mat.h, ov-intx.h, ov-java.cc, ov-lazy-idx.cc, ov-legacy-range.cc, ov-magic-int.cc, ov-mex-fcn.cc, ov-mex-fcn.h, ov-null-mat.cc, ov-perm.cc, ov-range.cc, ov-re-diag.cc, ov-re-mat.cc, ov-re-mat.h, ov-re-sparse.cc, ov-scalar.cc, ov-str-mat.cc, ov-struct.cc, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, ovl.h, octave.cc, octave.h, op-b-sbm.cc, op-bm-sbm.cc, op-cs-scm.cc, op-fm-fcm.cc, op-fs-fcm.cc, op-s-scm.cc, op-scm-cs.cc, op-scm-s.cc, op-sm-cs.cc, ops.h, anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h, comment-list.cc, comment-list.h, filepos.h, lex.h, oct-lvalue.cc, oct-lvalue.h, parse.h, profiler.cc, profiler.h, pt-anon-scopes.cc, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h, pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.cc, pt-bp.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-check.cc, pt-check.h, pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc, pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc, pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc, pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc, pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-walk.cc, pt-walk.h, pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h, idx-vector.cc, idx-vector.h, range-fwd.h, CollocWt.cc, CollocWt.h, aepbalance.cc, aepbalance.h, chol.cc, chol.h, gepbalance.cc, gepbalance.h, gsvd.cc, gsvd.h, hess.cc, hess.h, lo-mappers.cc, lo-mappers.h, lo-specfun.cc, lo-specfun.h, lu.cc, lu.h, oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-norm.h, oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc, qrp.h, randgamma.cc, randgamma.h, randmtzig.cc, randmtzig.h, randpoisson.cc, randpoisson.h, schur.cc, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc, sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc, child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc, file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h, mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h, oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc, oct-time.h, oct-uname.cc, oct-uname.h, action-container.cc, action-container.h, base-list.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, f77-fcn.h, file-info.cc, file-info.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc, lo-hash.h, lo-ieee.h, lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h, oct-base64.cc, oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h, oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h, oct-sparse.cc, oct-sparse.h, oct-string.h, octave-preserve-stream-state.h, pathsearch.cc, pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h, url-transfer.cc, url-transfer.h: Re-indent code after switch to using namespace macros.
author Rik <rik@octave.org>
date Thu, 01 Dec 2022 18:02:15 -0800
parents e88a07dec498
children 597f3ee61a48
comparison
equal deleted inserted replaced
31605:e88a07dec498 31607:aac27ad79be6
97 // executed. This pointer should only point to a parent function 97 // executed. This pointer should only point to a parent function
98 // stack frame. 98 // stack frame.
99 99
100 OCTAVE_BEGIN_NAMESPACE(octave) 100 OCTAVE_BEGIN_NAMESPACE(octave)
101 101
102 class tree_evaluator; 102 class tree_evaluator;
103 class symbol_info_list; 103 class symbol_info_list;
104 class unwind_protect; 104 class unwind_protect;
105 105
106 class stack_frame_walker; 106 class stack_frame_walker;
107 107
108 class stack_frame 108 class stack_frame
109 { 109 {
110 public: 110 public:
111 111
112 typedef std::map<std::string, octave_value> local_vars_map; 112 typedef std::map<std::string, octave_value> local_vars_map;
113 113
114 // Markers indicating the type of a variable. Values for local 114 // Markers indicating the type of a variable. Values for local
115 // variables are stored in the stack frame. Values for 115 // variables are stored in the stack frame. Values for
116 // global variables are stored in the tree_evaluator object that 116 // global variables are stored in the tree_evaluator object that
117 // contains the stack frame. Values for persistent variables are 117 // contains the stack frame. Values for persistent variables are
118 // stored in the function scope corresponding to the stack frame. 118 // stored in the function scope corresponding to the stack frame.
119 119
120 enum scope_flags 120 enum scope_flags
121 { 121 {
122 LOCAL, 122 LOCAL,
123 GLOBAL, 123 GLOBAL,
124 PERSISTENT 124 PERSISTENT
125 };
126
127 // Index into the list of automatic variables for user-defined
128 // function stack frames.
129
130 enum auto_var_type
131 {
132 ARG_NAMES,
133 IGNORED,
134 NARGIN,
135 NARGOUT,
136 SAVED_WARNING_STATES,
137 NUM_AUTO_VARS
138 };
139
140 stack_frame (void) = delete;
141
142 stack_frame (tree_evaluator& tw, std::size_t index,
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),
148 m_parent_link (parent_link), m_static_link (static_link),
149 m_access_link (access_link), m_dispatch_class ()
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 (void) = 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 (void) const { return false; }
196 virtual bool is_user_script_frame (void) const { return false; }
197 virtual bool is_user_fcn_frame (void) const { return false; }
198 virtual bool is_scope_frame (void) const { return false; }
199
200 virtual void clear_values (void);
201
202 std::size_t index (void) const { return m_index; }
203
204 void line (int l) { m_line = l; }
205 int line (void) const { return m_line; }
206
207 void column (int c) { m_column = c; }
208 int column (void) const { return m_column; }
209
210 std::string fcn_file_name (void) 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 (void) const = 0;
242
243 virtual octave_function * function (void) const { return nullptr; }
244
245 virtual unwind_protect * unwind_protect_frame (void) { return nullptr; }
246
247 symbol_info_list
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
255 symbol_info_list all_variables (void);
256
257 octave_value workspace (void);
258
259 std::list<std::string> variable_names (void) 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
273 symbol_info_list get_symbol_info (void)
274 {
275 return all_variables ();
276 }
277
278 void make_persistent (const symbol_record& sym)
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
310 std::shared_ptr<stack_frame>
311 parent_link (void) const {return m_parent_link; }
312
313 std::shared_ptr<stack_frame>
314 static_link (void) const {return m_static_link; }
315
316 std::shared_ptr<stack_frame>
317 access_link (void) const {return m_access_link; }
318
319 virtual std::size_t size (void) const;
320
321 virtual void resize (std::size_t);
322
323 void mark_global (const symbol_record& sym)
324 {
325 mark_scope (sym, GLOBAL);
326 }
327
328 void unmark_global (const symbol_record& sym)
329 {
330 mark_scope (sym, LOCAL);
331 }
332
333 void mark_persistent (const symbol_record& sym)
334 {
335 mark_scope (sym, PERSISTENT);
336 }
337
338 void unmark_persistent (const symbol_record& sym)
339 {
340 mark_scope (sym, LOCAL);
341 }
342
343 bool is_defined (const symbol_record& sym) const
344 {
345 octave_value val = varval (sym);
346
347 return val.is_defined ();
348 }
349
350 bool is_variable (const symbol_record& sym) const
351 {
352 octave_value val = varval (sym);
353
354 return val.is_defined ();
355 }
356
357 bool is_variable (const std::string& name) const
358 {
359 symbol_record sym = lookup_symbol (name);
360
361 return sym ? is_variable (sym) : false;
362 }
363
364 bool is_local_variable (const std::string& name) const
365 {
366 symbol_record sym = lookup_symbol (name);
367
368 return sym ? (is_variable (sym) && ! is_global (sym)) : false;
369 }
370
371 bool is_object (const symbol_record& sym) const
372 {
373 octave_value val = varval (sym);
374
375 return val.isobject ();
376 }
377
378 bool is_object (const std::string& name) const
379 {
380 symbol_record sym = lookup_symbol (name);
381
382 return sym ? is_object (sym) : false;
383 }
384
385 virtual scope_flags scope_flag (const symbol_record&) const = 0;
386
387 virtual scope_flags get_scope_flag (std::size_t) const;
388
389 virtual void set_scope_flag (std::size_t, scope_flags);
390
391 bool is_global (const symbol_record& sym) const
392 {
393 return scope_flag (sym) == GLOBAL;
394 }
395
396 bool is_global (const std::string& name) const
397 {
398 symbol_record sym = lookup_symbol (name);
399
400 return sym ? is_global (sym) : false;
401 }
402
403 bool is_persistent (const symbol_record& sym) const
404 {
405 return scope_flag (sym) == PERSISTENT;
406 }
407
408 bool is_persistent (const std::string& name) const
409 {
410 symbol_record sym = lookup_symbol (name);
411
412 return sym ? is_persistent (sym) : false;
413 }
414
415 void install_variable (const symbol_record& sym,
416 const octave_value& value, bool global);
417
418 void install_variable (const std::string& name,
419 const octave_value& value, bool global)
420 {
421 symbol_record sym = insert_symbol (name);
422
423 install_variable (sym, value, global);
424 }
425
426 virtual octave_value get_auto_fcn_var (auto_var_type) const = 0;
427
428 virtual void set_auto_fcn_var (auto_var_type, const octave_value&) = 0;
429
430 virtual octave_value varval (const symbol_record& sym) const = 0;;
431
432 virtual octave_value varval (std::size_t data_offset) const;
433
434 octave_value varval (const std::string& name) const
435 {
436 symbol_record sym = lookup_symbol (name);
437
438 return sym ? varval (sym) : octave_value ();
439 }
440
441 virtual octave_value& varref (const symbol_record& sym) = 0;
442
443 virtual octave_value& varref (std::size_t data_offset);
444
445 void assign (const symbol_record& sym, const octave_value& val)
446 {
447 octave_value& lhs = varref (sym);
448
449 if (lhs.get_count () == 1)
450 lhs.call_object_destructor ();
451
452 // Regularize a null matrix if stored into a variable.
453 lhs = val.storable_value ();
454 }
455
456 void assign (const std::string& name, const octave_value& val)
457 {
458 symbol_record sym = insert_symbol (name);
459
460 assign (sym, val);
461 }
462
463 void assign (octave_value::assign_op op, const symbol_record& sym,
464 const std::string& type,
465 const std::list<octave_value_list>& idx,
466 const octave_value& rhs)
467 {
468 if (idx.empty ())
469 {
470 if (op == octave_value::op_asn_eq)
471 assign (sym, rhs);
472 else
473 varref (sym).assign (op, rhs);
474 }
475 else
476 varref (sym).assign (op, type, idx, rhs);
477 }
478
479 void non_const_unary_op (octave_value::unary_op op,
480 const symbol_record& sym,
481 const std::string& type,
482 const std::list<octave_value_list>& idx)
483 {
484 if (idx.empty ())
485 varref (sym).non_const_unary_op (op);
486 else
487 varref (sym).non_const_unary_op (op, type, idx);
488 }
489
490 octave_value value (const symbol_record& sym, const std::string& type,
491 const std::list<octave_value_list>& idx) const
492 {
493 octave_value retval = varval (sym);
494
495 if (! idx.empty ())
496 {
497 if (retval.is_constant ())
498 retval = retval.subsref (type, idx);
499 else
500 {
501 octave_value_list t = retval.subsref (type, idx, 1);
502
503 retval = t.length () > 0 ? t(0) : octave_value ();
504 }
505 }
506
507 return retval;
508 }
509
510 octave_value find_subfunction (const std::string& name) const
511 {
512 symbol_scope scope = get_scope ();
513
514 return scope.find_subfunction (name);
515 }
516
517 void clear (const symbol_record& sym)
518 {
519 if (is_global (sym))
520 unmark_global (sym);
521
522 assign (sym, octave_value ());
523
524 if (is_persistent (sym))
525 unmark_persistent (sym);
526 }
527
528 void clear_objects (void);
529
530 void clear_variable (const std::string& name);
531
532 void clear_variable_pattern (const std::string& pattern);
533 void clear_variable_pattern (const string_vector& patterns);
534
535 void clear_variable_regexp (const std::string& pattern);
536 void clear_variable_regexp (const string_vector& patterns);
537
538 void clear_variables (void);
539
540 std::string get_dispatch_class (void) const { return m_dispatch_class; }
541
542 void set_dispatch_class (const std::string& class_name)
543 {
544 m_dispatch_class = class_name;
545 }
546
547 void display_stopped_in_message (std::ostream& os) const;
548
549 virtual void mark_scope (const symbol_record&, scope_flags) = 0;
550
551 virtual void display (bool follow = true) const;
552
553 virtual void accept (stack_frame_walker& sfw) = 0;
554
555 virtual void break_closure_cycles (const std::shared_ptr<stack_frame>&) { }
556
557 void mark_closure_context (void) { m_is_closure_context = true; }
558 bool is_closure_context (void) const { return m_is_closure_context; }
559
560 protected:
561
562 // Reference to the call stack that contains this frame. Global
563 // variables are stored in the call stack. This link gives us
564 // immediate access to them.
565 tree_evaluator& m_evaluator;
566
567 // TRUE if this stack frame is saved with a handle to a nested
568 // function (closure).
569 bool m_is_closure_context;
570
571 // The line and column of the source file where this stack frame
572 // was created. Used to print stack traces.
573 int m_line;
574 int m_column;
575
576 // Index in call stack.
577 std::size_t m_index;
578
579 // Pointer to the nearest parent frame. May include compiled
580 // functions.
581 std::shared_ptr<stack_frame> m_parent_link;
582
583 // Pointer to the nearest parent frame that contains variable
584 // information (script, function, or scope). This link skips over
585 // compiled function parent frames.
586 std::shared_ptr<stack_frame> m_static_link;
587
588 // Pointer to the nearest lexical parent frame. Used to access
589 // non-local variables for nested and anonymous functions or as a
590 // link to the parent frame in which a script is executed.
591 std::shared_ptr<stack_frame> m_access_link;
592
593 // Allow function handles to temporarily store their dispatch class
594 // in the call stack.
595 std::string m_dispatch_class;
596 }; 125 };
597 126
127 // Index into the list of automatic variables for user-defined
128 // function stack frames.
129
130 enum auto_var_type
131 {
132 ARG_NAMES,
133 IGNORED,
134 NARGIN,
135 NARGOUT,
136 SAVED_WARNING_STATES,
137 NUM_AUTO_VARS
138 };
139
140 stack_frame (void) = delete;
141
142 stack_frame (tree_evaluator& tw, std::size_t index,
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),
148 m_parent_link (parent_link), m_static_link (static_link),
149 m_access_link (access_link), m_dispatch_class ()
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 (void) = 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 (void) const { return false; }
196 virtual bool is_user_script_frame (void) const { return false; }
197 virtual bool is_user_fcn_frame (void) const { return false; }
198 virtual bool is_scope_frame (void) const { return false; }
199
200 virtual void clear_values (void);
201
202 std::size_t index (void) const { return m_index; }
203
204 void line (int l) { m_line = l; }
205 int line (void) const { return m_line; }
206
207 void column (int c) { m_column = c; }
208 int column (void) const { return m_column; }
209
210 std::string fcn_file_name (void) 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 (void) const = 0;
242
243 virtual octave_function * function (void) const { return nullptr; }
244
245 virtual unwind_protect * unwind_protect_frame (void) { return nullptr; }
246
247 symbol_info_list
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
255 symbol_info_list all_variables (void);
256
257 octave_value workspace (void);
258
259 std::list<std::string> variable_names (void) 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
273 symbol_info_list get_symbol_info (void)
274 {
275 return all_variables ();
276 }
277
278 void make_persistent (const symbol_record& sym)
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
310 std::shared_ptr<stack_frame>
311 parent_link (void) const {return m_parent_link; }
312
313 std::shared_ptr<stack_frame>
314 static_link (void) const {return m_static_link; }
315
316 std::shared_ptr<stack_frame>
317 access_link (void) const {return m_access_link; }
318
319 virtual std::size_t size (void) const;
320
321 virtual void resize (std::size_t);
322
323 void mark_global (const symbol_record& sym)
324 {
325 mark_scope (sym, GLOBAL);
326 }
327
328 void unmark_global (const symbol_record& sym)
329 {
330 mark_scope (sym, LOCAL);
331 }
332
333 void mark_persistent (const symbol_record& sym)
334 {
335 mark_scope (sym, PERSISTENT);
336 }
337
338 void unmark_persistent (const symbol_record& sym)
339 {
340 mark_scope (sym, LOCAL);
341 }
342
343 bool is_defined (const symbol_record& sym) const
344 {
345 octave_value val = varval (sym);
346
347 return val.is_defined ();
348 }
349
350 bool is_variable (const symbol_record& sym) const
351 {
352 octave_value val = varval (sym);
353
354 return val.is_defined ();
355 }
356
357 bool is_variable (const std::string& name) const
358 {
359 symbol_record sym = lookup_symbol (name);
360
361 return sym ? is_variable (sym) : false;
362 }
363
364 bool is_local_variable (const std::string& name) const
365 {
366 symbol_record sym = lookup_symbol (name);
367
368 return sym ? (is_variable (sym) && ! is_global (sym)) : false;
369 }
370
371 bool is_object (const symbol_record& sym) const
372 {
373 octave_value val = varval (sym);
374
375 return val.isobject ();
376 }
377
378 bool is_object (const std::string& name) const
379 {
380 symbol_record sym = lookup_symbol (name);
381
382 return sym ? is_object (sym) : false;
383 }
384
385 virtual scope_flags scope_flag (const symbol_record&) const = 0;
386
387 virtual scope_flags get_scope_flag (std::size_t) const;
388
389 virtual void set_scope_flag (std::size_t, scope_flags);
390
391 bool is_global (const symbol_record& sym) const
392 {
393 return scope_flag (sym) == GLOBAL;
394 }
395
396 bool is_global (const std::string& name) const
397 {
398 symbol_record sym = lookup_symbol (name);
399
400 return sym ? is_global (sym) : false;
401 }
402
403 bool is_persistent (const symbol_record& sym) const
404 {
405 return scope_flag (sym) == PERSISTENT;
406 }
407
408 bool is_persistent (const std::string& name) const
409 {
410 symbol_record sym = lookup_symbol (name);
411
412 return sym ? is_persistent (sym) : false;
413 }
414
415 void install_variable (const symbol_record& sym,
416 const octave_value& value, bool global);
417
418 void install_variable (const std::string& name,
419 const octave_value& value, bool global)
420 {
421 symbol_record sym = insert_symbol (name);
422
423 install_variable (sym, value, global);
424 }
425
426 virtual octave_value get_auto_fcn_var (auto_var_type) const = 0;
427
428 virtual void set_auto_fcn_var (auto_var_type, const octave_value&) = 0;
429
430 virtual octave_value varval (const symbol_record& sym) const = 0;;
431
432 virtual octave_value varval (std::size_t data_offset) const;
433
434 octave_value varval (const std::string& name) const
435 {
436 symbol_record sym = lookup_symbol (name);
437
438 return sym ? varval (sym) : octave_value ();
439 }
440
441 virtual octave_value& varref (const symbol_record& sym) = 0;
442
443 virtual octave_value& varref (std::size_t data_offset);
444
445 void assign (const symbol_record& sym, const octave_value& val)
446 {
447 octave_value& lhs = varref (sym);
448
449 if (lhs.get_count () == 1)
450 lhs.call_object_destructor ();
451
452 // Regularize a null matrix if stored into a variable.
453 lhs = val.storable_value ();
454 }
455
456 void assign (const std::string& name, const octave_value& val)
457 {
458 symbol_record sym = insert_symbol (name);
459
460 assign (sym, val);
461 }
462
463 void assign (octave_value::assign_op op, const symbol_record& sym,
464 const std::string& type,
465 const std::list<octave_value_list>& idx,
466 const octave_value& rhs)
467 {
468 if (idx.empty ())
469 {
470 if (op == octave_value::op_asn_eq)
471 assign (sym, rhs);
472 else
473 varref (sym).assign (op, rhs);
474 }
475 else
476 varref (sym).assign (op, type, idx, rhs);
477 }
478
479 void non_const_unary_op (octave_value::unary_op op,
480 const symbol_record& sym,
481 const std::string& type,
482 const std::list<octave_value_list>& idx)
483 {
484 if (idx.empty ())
485 varref (sym).non_const_unary_op (op);
486 else
487 varref (sym).non_const_unary_op (op, type, idx);
488 }
489
490 octave_value value (const symbol_record& sym, const std::string& type,
491 const std::list<octave_value_list>& idx) const
492 {
493 octave_value retval = varval (sym);
494
495 if (! idx.empty ())
496 {
497 if (retval.is_constant ())
498 retval = retval.subsref (type, idx);
499 else
500 {
501 octave_value_list t = retval.subsref (type, idx, 1);
502
503 retval = t.length () > 0 ? t(0) : octave_value ();
504 }
505 }
506
507 return retval;
508 }
509
510 octave_value find_subfunction (const std::string& name) const
511 {
512 symbol_scope scope = get_scope ();
513
514 return scope.find_subfunction (name);
515 }
516
517 void clear (const symbol_record& sym)
518 {
519 if (is_global (sym))
520 unmark_global (sym);
521
522 assign (sym, octave_value ());
523
524 if (is_persistent (sym))
525 unmark_persistent (sym);
526 }
527
528 void clear_objects (void);
529
530 void clear_variable (const std::string& name);
531
532 void clear_variable_pattern (const std::string& pattern);
533 void clear_variable_pattern (const string_vector& patterns);
534
535 void clear_variable_regexp (const std::string& pattern);
536 void clear_variable_regexp (const string_vector& patterns);
537
538 void clear_variables (void);
539
540 std::string get_dispatch_class (void) const { return m_dispatch_class; }
541
542 void set_dispatch_class (const std::string& class_name)
543 {
544 m_dispatch_class = class_name;
545 }
546
547 void display_stopped_in_message (std::ostream& os) const;
548
549 virtual void mark_scope (const symbol_record&, scope_flags) = 0;
550
551 virtual void display (bool follow = true) const;
552
553 virtual void accept (stack_frame_walker& sfw) = 0;
554
555 virtual void break_closure_cycles (const std::shared_ptr<stack_frame>&) { }
556
557 void mark_closure_context (void) { m_is_closure_context = true; }
558 bool is_closure_context (void) const { return m_is_closure_context; }
559
560 protected:
561
562 // Reference to the call stack that contains this frame. Global
563 // variables are stored in the call stack. This link gives us
564 // immediate access to them.
565 tree_evaluator& m_evaluator;
566
567 // TRUE if this stack frame is saved with a handle to a nested
568 // function (closure).
569 bool m_is_closure_context;
570
571 // The line and column of the source file where this stack frame
572 // was created. Used to print stack traces.
573 int m_line;
574 int m_column;
575
576 // Index in call stack.
577 std::size_t m_index;
578
579 // Pointer to the nearest parent frame. May include compiled
580 // functions.
581 std::shared_ptr<stack_frame> m_parent_link;
582
583 // Pointer to the nearest parent frame that contains variable
584 // information (script, function, or scope). This link skips over
585 // compiled function parent frames.
586 std::shared_ptr<stack_frame> m_static_link;
587
588 // Pointer to the nearest lexical parent frame. Used to access
589 // non-local variables for nested and anonymous functions or as a
590 // link to the parent frame in which a script is executed.
591 std::shared_ptr<stack_frame> m_access_link;
592
593 // Allow function handles to temporarily store their dispatch class
594 // in the call stack.
595 std::string m_dispatch_class;
596 };
597
598 OCTAVE_END_NAMESPACE(octave) 598 OCTAVE_END_NAMESPACE(octave)
599 599
600 #endif 600 #endif