1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2003-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_pt_fcn_handle_h)
27 #define octave_pt_fcn_handle_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iosfwd>
32 #include <string>
33 
34 #include "pt-bp.h"
35 #include "pt-exp.h"
36 #include "pt-misc.h"
37 #include "pt-stmt.h"
38 #include "pt-walk.h"
39 #include "symscope.h"
40 
41 class octave_value_list;
42 
43 #include "ov.h"
44 #include "ov-usr-fcn.h"
45 
46 namespace octave
47 {
48   class tree_fcn_handle : public tree_expression
49   {
50   public:
51 
52     tree_fcn_handle (int l = -1, int c = -1)
tree_expression(l,c)53       : tree_expression (l, c), m_name () { }
54 
55     tree_fcn_handle (const std::string& n, int l = -1, int c = -1)
tree_expression(l,c)56       : tree_expression (l, c), m_name (n) { }
57 
58     // No copying!
59 
60     tree_fcn_handle (const tree_fcn_handle&) = delete;
61 
62     tree_fcn_handle& operator = (const tree_fcn_handle&) = delete;
63 
64     ~tree_fcn_handle (void) = default;
65 
66     void print (std::ostream& os, bool pr_as_read_syntax = false,
67                 bool pr_orig_txt = true);
68 
69     void print_raw (std::ostream& os, bool pr_as_read_syntax = false,
70                     bool pr_orig_txt = true);
71 
name(void)72     std::string name (void) const { return m_name; }
73 
rvalue_ok(void)74     bool rvalue_ok (void) const { return true; }
75 
76     tree_expression * dup (symbol_scope& scope) const;
77 
78     octave_value evaluate (tree_evaluator& tw, int nargout = 1);
79 
80     octave_value_list evaluate_n (tree_evaluator& tw, int nargout = 1)
81     {
82       return ovl (evaluate (tw, nargout));
83     }
84 
accept(tree_walker & tw)85     void accept (tree_walker& tw)
86     {
87       tw.visit_fcn_handle (*this);
88     }
89 
90   private:
91 
92     // The name of this function handle.
93     std::string m_name;
94   };
95 
96   class tree_anon_fcn_handle : public tree_expression
97   {
98   public:
99 
100     tree_anon_fcn_handle (int l = -1, int c = -1)
tree_expression(l,c)101       : tree_expression (l, c), m_parameter_list (nullptr),
102         m_expression (nullptr), m_scope (), m_parent_scope (),
103         m_file_name ()
104     { }
105 
106     tree_anon_fcn_handle (tree_parameter_list *pl, tree_expression *ex,
107                           const symbol_scope& scope,
108                           const symbol_scope& parent_scope,
109                           int l = -1, int c = -1)
tree_expression(l,c)110       : tree_expression (l, c), m_parameter_list (pl), m_expression (ex),
111         m_scope (scope), m_parent_scope (parent_scope), m_file_name ()
112     { }
113 
114     // No copying!
115 
116     tree_anon_fcn_handle (const tree_anon_fcn_handle&) = delete;
117 
118     tree_anon_fcn_handle& operator = (const tree_anon_fcn_handle&) = delete;
119 
120     ~tree_anon_fcn_handle (void);
121 
rvalue_ok(void)122     bool rvalue_ok (void) const { return true; }
123 
parameter_list(void)124     tree_parameter_list * parameter_list (void) const
125     {
126       return m_parameter_list;
127     }
128 
expression(void)129     tree_expression * expression (void) const { return m_expression; }
130 
scope(void)131     symbol_scope scope (void) const { return m_scope; }
132 
parent_scope(void)133     symbol_scope parent_scope (void) const { return m_parent_scope; }
134 
has_parent_scope(void)135     bool has_parent_scope (void) const { return m_parent_scope.is_valid (); }
136 
137     tree_expression * dup (symbol_scope& scope) const;
138 
139     octave_value evaluate (tree_evaluator& tw, int nargout = 1);
140 
141     octave_value_list evaluate_n (tree_evaluator& tw, int nargout = 1)
142     {
143       return ovl (evaluate (tw, nargout));
144     }
145 
accept(tree_walker & tw)146     void accept (tree_walker& tw) { tw.visit_anon_fcn_handle (*this); }
147 
stash_file_name(const std::string & file)148     void stash_file_name (const std::string& file) { m_file_name = file; }
149 
file_name(void)150     std::string file_name (void) const { return m_file_name; }
151 
152   private:
153 
154     // Inputs parameters.
155     tree_parameter_list *m_parameter_list;
156 
157     // Function body, limited to a single expression.
158     tree_expression *m_expression;
159 
160     // Function scope.
161     symbol_scope m_scope;
162 
163     // Parent scope, or an invalid scope if none.
164     symbol_scope m_parent_scope;
165 
166     // Filename where the handle was defined.
167     std::string m_file_name;
168   };
169 }
170 
171 #endif
172