annotate libinterp/parse-tree/pt-id.h @ 27371:fcaecdbc8d8a

don't use visitor pattern for expression evaluation (bug #56752) Although it is desirable to have all parse tree evaluation functions grouped together in a single file, using the visitor pattern can be inefficient, especially when the visitor function is small and the extra levels of indirection and virtual function resolution can take more time than the evaluation function itself (evaluation of constants, for example). For all classes derived from tree_expression, introduce new evaluate and evaluate_n methods. Use those instead of visit_CLASS functions to perform expression evaluation. Results are now returned directly from the evaluation functions instead of storing them in the tree_evaluator object. Files affected: cdef-class.cc, oct-parse.yy, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-classdef.cc, pt-classdef.h, pt-colon.cc, pt-colon.h, pt-const.h, pt-eval.cc, pt-eval.h, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-loop.cc, pt-mat.cc, pt-mat.h, pt-select.cc, pt-tm-const.cc, pt-unop.cc, and pt-unop.h.
author John W. Eaton <jwe@octave.org>
date Fri, 30 Aug 2019 15:02:14 -0400
parents cf9e10ce3351
children b442ec6dda5c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
1 /*
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
2
26376
00f796120a6d maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 25383
diff changeset
3 Copyright (C) 1996-2019 John W. Eaton
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
4
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
5 This file is part of Octave.
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
6
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24361
diff changeset
7 Octave is free software: you can redistribute it and/or modify it
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
8 under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24361
diff changeset
9 the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
10 (at your option) any later version.
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
11
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
12 Octave is distributed in the hope that it will be useful, but
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
13 WITHOUT ANY WARRANTY; without even the implied warranty of
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
15 GNU General Public License for more details.
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
16
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5861
diff changeset
18 along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24361
diff changeset
19 <https://www.gnu.org/licenses/>.
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
20
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
21 */
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
22
20791
f7084eae3318 maint: Use Octave coding conventions for #if statements.
Rik <rik@octave.org>
parents: 20542
diff changeset
23 #if ! defined (octave_pt_id_h)
17822
ebb3ef964372 maint: Use common #define syntax "octave_filename_h" in h_files.
Rik <rik@octave.org>
parents: 17787
diff changeset
24 #define octave_pt_id_h 1
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
25
21244
1473547f50f5 include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents: 20791
diff changeset
26 #include "octave-config.h"
1473547f50f5 include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents: 20791
diff changeset
27
8950
d865363208d6 include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents: 8920
diff changeset
28 #include <iosfwd>
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
29 #include <string>
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
30
2943
e6dfc3ea6299 [project @ 1997-05-08 02:31:59 by jwe]
jwe
parents: 2887
diff changeset
31 class octave_value;
e6dfc3ea6299 [project @ 1997-05-08 02:31:59 by jwe]
jwe
parents: 2887
diff changeset
32 class octave_value_list;
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
33 class octave_function;
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
34
15037
56b8eb7c9c04 improvements in parsing classdef
John W. Eaton <jwe@octave.org>
parents: 14982
diff changeset
35 #include "oct-lvalue.h"
7677
db02cc0ba8f2 handle breakpoints in tree_identifier::do_lookup
John W. Eaton <jwe@octave.org>
parents: 7336
diff changeset
36 #include "pt-bp.h"
2980
cd5ad3fd8049 [project @ 1997-05-16 01:12:13 by jwe]
jwe
parents: 2979
diff changeset
37 #include "pt-exp.h"
23435
c452180ab672 begin refactoring parse tree evaluator
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
38 #include "pt-walk.h"
24352
bff8e3884a88 restructure global variable handling in symbol table
John W. Eaton <jwe@octave.org>
parents: 24270
diff changeset
39 #include "symscope.h"
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
40
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
41 namespace octave
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
42 {
23435
c452180ab672 begin refactoring parse tree evaluator
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
43 class tree_evaluator;
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
44
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
45 // Symbols from the symbol table.
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
46
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
47 class tree_identifier : public tree_expression
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
48 {
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
49 friend class tree_index_expression;
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
50
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
51 public:
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
52
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
53 tree_identifier (int l = -1, int c = -1)
24846
7620f1f5290d use m_ prefix for member variables in parse tree classes
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
54 : tree_expression (l, c), m_sym () { }
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
55
24270
bc3819b7cca1 don't use symbol_table:: nesting for symbol_record, symbol_scope, or fcn_info
John W. Eaton <jwe@octave.org>
parents: 24037
diff changeset
56 tree_identifier (const symbol_record& s,
23599
5cb3a2bb5e1e don't use singleton for symbol_table
John W. Eaton <jwe@octave.org>
parents: 23470
diff changeset
57 int l = -1, int c = -1)
24846
7620f1f5290d use m_ prefix for member variables in parse tree classes
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
58 : tree_expression (l, c), m_sym (s) { }
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
59
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
60 // No copying!
4267
7cd865a8c815 [project @ 2003-01-03 15:27:05 by jwe]
jwe
parents: 4192
diff changeset
61
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
62 tree_identifier (const tree_identifier&) = delete;
22869
f75d289645ec make deleted functions public
John W. Eaton <jwe@octave.org>
parents: 22868
diff changeset
63
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
64 tree_identifier& operator = (const tree_identifier&) = delete;
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
65
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
66 ~tree_identifier (void) = default;
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
67
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
68 bool has_magic_end (void) const { return (name () == "end"); }
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
69
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
70 bool is_identifier (void) const { return true; }
10206
37a08e0ce2dc support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents: 10160
diff changeset
71
24846
7620f1f5290d use m_ prefix for member variables in parse tree classes
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
72 std::string name (void) const { return m_sym.name (); }
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
73
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26377
diff changeset
74 virtual bool is_black_hole (void) const { return false; }
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
75
24846
7620f1f5290d use m_ prefix for member variables in parse tree classes
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
76 void mark_as_formal_parameter (void) { m_sym.mark_formal (); }
3010
1aeb8869e464 [project @ 1997-06-01 19:24:02 by jwe]
jwe
parents: 2988
diff changeset
77
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26377
diff changeset
78 // We really need to know whether this symbol refers to a variable
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
79 // or a function, but we may not know that yet.
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
80
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
81 bool lvalue_ok (void) const { return true; }
3010
1aeb8869e464 [project @ 1997-06-01 19:24:02 by jwe]
jwe
parents: 2988
diff changeset
82
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26377
diff changeset
83 octave_lvalue lvalue (tree_evaluator& tw);
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
84
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
85 void eval_undefined_error (void);
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
86
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
87 void static_workspace_error (void)
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
88 {
23803
90689bdbe048 Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents: 23615
diff changeset
89 error (R"(can not add variable "%s" to a static workspace)",
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
90 name ().c_str ());
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
91 }
15236
44d6ffdf9479 Disallow new variables in nested functions (bug #36271)
Max Brister <max@2bass.com>
parents: 15195
diff changeset
92
24270
bc3819b7cca1 don't use symbol_table:: nesting for symbol_record, symbol_scope, or fcn_info
John W. Eaton <jwe@octave.org>
parents: 24037
diff changeset
93 tree_identifier * dup (symbol_scope& scope) const;
5861
2a6cb4ed8f1e [project @ 2006-06-16 05:09:41 by jwe]
jwe
parents: 5307
diff changeset
94
27371
fcaecdbc8d8a don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents: 26661
diff changeset
95 octave_value evaluate (tree_evaluator& tw, int nargout = 1)
fcaecdbc8d8a don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents: 26661
diff changeset
96 {
fcaecdbc8d8a don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents: 26661
diff changeset
97 octave_value_list retval = evaluate_n (tw, nargout);
fcaecdbc8d8a don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents: 26661
diff changeset
98
fcaecdbc8d8a don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents: 26661
diff changeset
99 return retval.length () > 0 ? retval(0) : octave_value ();
fcaecdbc8d8a don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents: 26661
diff changeset
100 }
fcaecdbc8d8a don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents: 26661
diff changeset
101
fcaecdbc8d8a don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents: 26661
diff changeset
102 octave_value_list evaluate_n (tree_evaluator& tw, int nargout = 1);
fcaecdbc8d8a don't use visitor pattern for expression evaluation (bug #56752)
John W. Eaton <jwe@octave.org>
parents: 26661
diff changeset
103
23435
c452180ab672 begin refactoring parse tree evaluator
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
104 void accept (tree_walker& tw)
c452180ab672 begin refactoring parse tree evaluator
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
105 {
c452180ab672 begin refactoring parse tree evaluator
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
106 tw.visit_identifier (*this);
c452180ab672 begin refactoring parse tree evaluator
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
107 }
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
108
24846
7620f1f5290d use m_ prefix for member variables in parse tree classes
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
109 symbol_record symbol (void) const { return m_sym; }
24037
21915520ac7b use more direct method for non-local symbol access (bug #38236)
John W. Eaton <jwe@octave.org>
parents: 23803
diff changeset
110
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26377
diff changeset
111 protected:
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
112
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
113 // The symbol record that this identifier references.
24846
7620f1f5290d use m_ prefix for member variables in parse tree classes
John W. Eaton <jwe@octave.org>
parents: 24534
diff changeset
114 symbol_record m_sym;
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
115 };
7752
40c428ea3408 initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents: 7677
diff changeset
116
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
117 class tree_black_hole : public tree_identifier
14913
c7071907a641 Use symbol_record_ref instead of names in JIT
Max Brister <max@2bass.com>
parents: 14912
diff changeset
118 {
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
119 public:
2988
daa1ed1f5462 [project @ 1997-05-16 07:23:15 by jwe]
jwe
parents: 2980
diff changeset
120
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
121 tree_black_hole (int l = -1, int c = -1)
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
122 : tree_identifier (l, c) { }
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
123
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
124 std::string name (void) const { return "~"; }
10206
37a08e0ce2dc support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents: 10160
diff changeset
125
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26377
diff changeset
126 bool is_black_hole (void) const { return true; }
10206
37a08e0ce2dc support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents: 10160
diff changeset
127
24270
bc3819b7cca1 don't use symbol_table:: nesting for symbol_record, symbol_scope, or fcn_info
John W. Eaton <jwe@octave.org>
parents: 24037
diff changeset
128 tree_black_hole * dup (symbol_scope&) const
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
129 {
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
130 return new tree_black_hole;
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
131 }
10206
37a08e0ce2dc support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents: 10160
diff changeset
132
26661
cf9e10ce3351 move variable values from symbol_record objects to stack_frame objects
John W. Eaton <jwe@octave.org>
parents: 26377
diff changeset
133 octave_lvalue lvalue (tree_evaluator& tw);
23075
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
134 };
4e3d47dc7e25 move parse tree classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 22869
diff changeset
135 }
10206
37a08e0ce2dc support Matlab-style empty output/input arguments
Jaroslav Hajek <highegg@gmail.com>
parents: 10160
diff changeset
136
2887
4b71bb90c388 [project @ 1997-04-28 01:20:55 by jwe]
jwe
parents:
diff changeset
137 #endif