annotate libinterp/parse-tree/pt-anon-scopes.cc @ 27725:6388a240de87

move some common actions into tree_walker base class Instead of having most tree_walker class methods be pure virtual functions, define them to be methods that walk the tree but do nothing else. This change allows us to eliminate some duplication in derived classes, and makes it easier to define derived classes that need to walk the parse tree but onny perform actions for a few parse tree elements. * pt-walk.h, pt-walk.cc (class tree_walker): Define most methods to walk the tree. * cdef-class.cc (class ctor_analyzer): Delete methods in base class that are now handled by the base tree_walker class. * pt-anon-scopes.h, pt-anon-scopes.cc (class tree_anon_scopes): Likewise. * pt-bp.h, pt-bp.cc (tree_breakpoint): Likewise. * pt-check.h, pt-check.cc (tree_checker): Likewise. * pt-eval.h, pt-eval.cc (tree_evaluator): Likewise. * pt-jit.cc (jit_convert): Likewise. * pt-pr-code.h, pt-pr-code.cc (tree_print_code): Likewise.
author John W. Eaton <jwe@octave.org>
date Wed, 20 Nov 2019 00:01:18 -0600
parents 581d01526b34
children b442ec6dda5c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
1 /*
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
2
26376
00f796120a6d maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 26177
diff changeset
3 Copyright (C) 1996-2019 John W. Eaton
00f796120a6d maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 26177
diff changeset
4 Copyright (C) 2015-2019 Olaf Till
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
5
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
6 This file is part of Octave.
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
7
26177
f2d795f07c84 maint: Use standard copyright phrasing in all files.
Rik <rik@octave.org>
parents: 25824
diff changeset
8 Octave is free software: you can redistribute it and/or modify it
f2d795f07c84 maint: Use standard copyright phrasing in all files.
Rik <rik@octave.org>
parents: 25824
diff changeset
9 under the terms of the GNU General Public License as published by
f2d795f07c84 maint: Use standard copyright phrasing in all files.
Rik <rik@octave.org>
parents: 25824
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
f2d795f07c84 maint: Use standard copyright phrasing in all files.
Rik <rik@octave.org>
parents: 25824
diff changeset
11 (at your option) any later version.
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
12
26177
f2d795f07c84 maint: Use standard copyright phrasing in all files.
Rik <rik@octave.org>
parents: 25824
diff changeset
13 Octave is distributed in the hope that it will be useful, but
f2d795f07c84 maint: Use standard copyright phrasing in all files.
Rik <rik@octave.org>
parents: 25824
diff changeset
14 WITHOUT ANY WARRANTY; without even the implied warranty of
f2d795f07c84 maint: Use standard copyright phrasing in all files.
Rik <rik@octave.org>
parents: 25824
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f2d795f07c84 maint: Use standard copyright phrasing in all files.
Rik <rik@octave.org>
parents: 25824
diff changeset
16 GNU General Public License for more details.
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
17
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
19 along with Octave; see the file COPYING. If not, see
26177
f2d795f07c84 maint: Use standard copyright phrasing in all files.
Rik <rik@octave.org>
parents: 25824
diff changeset
20 <https://www.gnu.org/licenses/>.
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
21
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
22 */
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
23
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
25 #include <config.h>
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
26 #endif
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
27
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
28 #include "error.h"
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
29 #include "pt-all.h"
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
30 #include "pt-anon-scopes.h"
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
31
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
32 // TODO: make sure that if(f->scope()) is checked if necessary
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
33
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
34 namespace octave
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
35 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
36 tree_anon_scopes::tree_anon_scopes (tree_anon_fcn_handle& anon_fh)
27725
6388a240de87 move some common actions into tree_walker base class
John W. Eaton <jwe@octave.org>
parents: 26686
diff changeset
37 : tree_walker (), m_params (), m_vars ()
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
38 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
39 visit_anon_fcn_handle (anon_fh);
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
40 }
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
41
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
42 void
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
43 tree_anon_scopes::visit_anon_fcn_handle (tree_anon_fcn_handle& afh)
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
44 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
45 tree_parameter_list *param_list = afh.parameter_list ();
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
46 tree_expression *expr = afh.expression ();
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
47
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
48 // Collect names of parameters.
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
49
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
50 if (param_list)
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
51 {
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
52 std::list<std::string> pnames = param_list->variable_names ();
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
53
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
54 for (const auto& nm : pnames)
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
55 m_params.insert (nm);
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
56
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
57 // Hmm, should this be included in the list returned from
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
58 // tree_parameter_list::variable_names?
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
59 if (param_list->takes_varargs ())
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
60 m_params.insert ("varargin");
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
61 }
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
62
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
63 // Further walk the tree to find free variables in this expression
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
64 // and any nested definitions of additional anonymous functions.
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
65
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
66 if (expr)
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
67 expr->accept (*this);
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
68 }
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
69
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
70 // The rest of visit_... methods is only for walking the tree. Many of
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
71 // them, in particular all methods for commands, are not applicable to
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
72 // anonymous functions. Only parts of the tree are walked which could
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
73 // contain further (nested) anonymous function definitions (so
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
74 // e.g. identifiers and left hand sides of assignments are ignored).
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
75
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
76 void
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
77 tree_anon_scopes::visit_identifier (tree_identifier& id)
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
78 {
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
79 std::string nm = id.name ();
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
80
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
81 if (m_params.find (nm) == m_params.end ())
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
82 m_vars.insert (nm);
25824
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
83 }
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
84
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
85 void
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
86 tree_anon_scopes::visit_parameter_list (tree_parameter_list&)
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
87 {
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
88 // In visit_anon_fcn_handle we only accept/visit the body of
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
89 // anonymous function definitions, not the parameter list.
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
90
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
91 panic_impossible ();
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
92 }
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
93
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
94 void
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
95 tree_anon_scopes::visit_statement (tree_statement& stmt)
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
96 {
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
97 tree_command *cmd = stmt.command ();
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
98
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
99 if (cmd)
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
100 panic_impossible ();
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
101 else
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
102 {
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
103 tree_expression *expr = stmt.expression ();
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
104
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
105 if (expr)
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
106 expr->accept (*this);
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
107 }
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
108 }
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
109
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
110 void
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
111 tree_anon_scopes::visit_statement_list (tree_statement_list& lst)
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
112 {
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
113 for (auto& p : lst)
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
114 {
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
115 tree_statement *elt = p;
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
116
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
117 if (elt)
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
118 elt->accept (*this);
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
119 }
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
120 }
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
121 }
91e1ca0e3a9d Save all scopes of nested anonymous functions (bug #45969).
Olaf Till <i7tiol@t-online.de>
parents:
diff changeset
122