GNU Octave  6.2.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-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 #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 
36 namespace octave
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 
tree_expression * expression(void) const
tree_parameter_list * parameter_list(void) const
void visit_parameter_list(tree_parameter_list &)
std::set< std::string > m_vars
void visit_statement(tree_statement &)
void visit_statement_list(tree_statement_list &)
tree_anon_scopes(void)=delete
void visit_identifier(tree_identifier &)
std::set< std::string > m_params
void visit_anon_fcn_handle(tree_anon_fcn_handle &)
std::list< std::string > variable_names(void) const
Definition: pt-misc.cc:55
bool takes_varargs(void) const
Definition: pt-misc.h:85
tree_expression * expression(void)
Definition: pt-stmt.h:101
void accept(tree_walker &tw)
Definition: pt-stmt.h:122
tree_command * command(void)
Definition: pt-stmt.h:99
virtual void accept(tree_walker &tw)=0
#define panic_impossible()
Definition: error.h:380