GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-anon-scopes.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29 
30 #include "error.h"
31 #include "pt-all.h"
32 #include "pt-anon-scopes.h"
33 
34 // TODO: make sure that if(f->scope()) is checked if necessary
35 
37 
39  : tree_walker (), m_params (), m_vars ()
40 {
41  visit_anon_fcn_handle (anon_fh);
42 }
43 
44 void
46 {
47  tree_parameter_list *param_list = afh.parameter_list ();
48  tree_expression *expr = afh.expression ();
49 
50  // Collect names of parameters.
51 
52  if (param_list)
53  {
54  std::list<std::string> pnames = param_list->variable_names ();
55 
56  for (const auto& nm : pnames)
57  m_params.insert (nm);
58 
59  // Hmm, should this be included in the list returned from
60  // tree_parameter_list::variable_names?
61  if (param_list->takes_varargs ())
62  m_params.insert ("varargin");
63  }
64 
65  // Further walk the tree to find free variables in this expression
66  // and any nested definitions of additional anonymous functions.
67 
68  if (expr)
69  expr->accept (*this);
70 }
71 
72 // The rest of visit_... methods is only for walking the tree. Many of
73 // them, in particular all methods for commands, are not applicable to
74 // anonymous functions. Only parts of the tree are walked which could
75 // contain further (nested) anonymous function definitions (so
76 // e.g. identifiers and left hand sides of assignments are ignored).
77 
78 void
80 {
81  std::string nm = id.name ();
82 
83  if (m_params.find (nm) == m_params.end ())
84  m_vars.insert (nm);
85 }
86 
87 void
89 {
90  // In visit_anon_fcn_handle we only accept/visit the body of
91  // anonymous function definitions, not the parameter list.
92 
94 }
95 
96 void
98 {
99  tree_command *cmd = stmt.command ();
100 
101  if (cmd)
102  panic_impossible ();
103  else
104  {
105  tree_expression *expr = stmt.expression ();
106 
107  if (expr)
108  expr->accept (*this);
109  }
110 }
111 
112 void
114 {
115  for (auto& p : lst)
116  {
117  tree_statement *elt = p;
118 
119  if (elt)
120  elt->accept (*this);
121  }
122 }
123 
124 OCTAVE_END_NAMESPACE(octave)
tree_parameter_list * parameter_list() const
tree_expression * expression() const
void visit_anon_fcn_handle(tree_anon_fcn_handle &)
void visit_statement(tree_statement &)
void visit_parameter_list(tree_parameter_list &)
void visit_identifier(tree_identifier &)
void visit_statement_list(tree_statement_list &)
bool takes_varargs() const
Definition: pt-misc.h:81
std::list< std::string > variable_names() const
Definition: pt-misc.cc:55
tree_command * command()
Definition: pt-stmt.h:95
tree_expression * expression()
Definition: pt-stmt.h:97
void accept(tree_walker &tw)
Definition: pt-stmt.h:118
virtual void accept(tree_walker &tw)=0
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define panic_impossible()
Definition: error.h:503