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 { tree_anon_scopes(tree_anon_fcn_handle & anon_fh)38 tree_anon_scopes::tree_anon_scopes (tree_anon_fcn_handle& anon_fh) 39 : tree_walker (), m_params (), m_vars () 40 { 41 visit_anon_fcn_handle (anon_fh); 42 } 43 44 void visit_anon_fcn_handle(tree_anon_fcn_handle & afh)45 tree_anon_scopes::visit_anon_fcn_handle (tree_anon_fcn_handle& afh) 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 visit_identifier(tree_identifier & id)79 tree_anon_scopes::visit_identifier (tree_identifier& id) 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 visit_parameter_list(tree_parameter_list &)88 tree_anon_scopes::visit_parameter_list (tree_parameter_list&) 89 { 90 // In visit_anon_fcn_handle we only accept/visit the body of 91 // anonymous function definitions, not the parameter list. 92 93 panic_impossible (); 94 } 95 96 void visit_statement(tree_statement & stmt)97 tree_anon_scopes::visit_statement (tree_statement& stmt) 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 visit_statement_list(tree_statement_list & lst)113 tree_anon_scopes::visit_statement_list (tree_statement_list& lst) 114 { 115 for (auto& p : lst) 116 { 117 tree_statement *elt = p; 118 119 if (elt) 120 elt->accept (*this); 121 } 122 } 123 } 124 125