GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
symscope.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2018 John W. Eaton
4 Copyright (C) 2009 VZLU Prague, a.s.
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include <sstream>
29 
30 #include "fcn-info.h"
31 #include "interpreter-private.h"
32 #include "interpreter.h"
33 #include "ov-fcn.h"
34 #include "ov-usr-fcn.h"
35 #include "symrec.h"
36 #include "symscope.h"
37 #include "symtab.h"
38 #include "utils.h"
39 
40 namespace octave
41 {
44  const octave_value_list& args,
45  bool skip_variables, bool local_funcs)
46  {
47  // Variable.
48 
49  symbol_table& symtab
50  = __get_symbol_table__ ("symbol_scope_rep::find");
51 
52  if (! skip_variables)
53  {
54  table_iterator p = m_symbols.find (name);
55 
56  if (p != m_symbols.end ())
57  {
58  symbol_record sr = p->second;
59 
60  if (sr.is_global ())
61  return symtab.global_varval (name);
62  else
63  {
65 
66  if (val.is_defined ())
67  return val;
68  }
69  }
70  }
71 
72  if (local_funcs)
73  {
74  // Subfunction. I think it only makes sense to check for
75  // subfunctions if we are currently executing a function defined
76  // from a .m file.
77 
79 
80  if (fcn.is_defined ())
81  return fcn;
82  }
83 
84  return symtab.fcn_table_find (name, args, local_funcs);
85  }
86 
88  symbol_scope_rep::insert (const std::string& name, bool force_add)
89  {
90  table_iterator p = m_symbols.find (name);
91 
92  if (p == m_symbols.end ())
93  {
94  symbol_record ret (name);
95 
96  auto t_parent = m_parent.lock ();
97 
98  if (m_is_nested && t_parent && t_parent->look_nonlocal (name, ret))
99  return m_symbols[name] = ret;
100  else
101  {
102  if (m_is_static && ! force_add)
103  ret.mark_added_static ();
104 
105  return m_symbols[name] = ret;
106  }
107  }
108  else
109  return p->second;
110  }
111 
114  {
115  std::map<std::string, octave_value> m
116  = {{ "name", m_name },
117  { "symbols", dump_symbols_map () },
118  { "subfunctions", dump_function_map (m_subfunctions) }};
119 
120  return octave_value (m);
121  }
122 
125  {
126  std::map<std::string, octave_value> info_map;
127 
128  for (const auto& nm_sr : m_symbols)
129  {
130  std::string nm = nm_sr.first;
131  const symbol_record& sr = nm_sr.second;
132  info_map[nm] = sr.dump (m_context);
133  }
134 
135  return octave_value (info_map);
136  }
137 
140  {
142 
143  if (p != m_subfunctions.end ())
144  return p->second;
145 
146  auto t_parent = m_parent.lock ();
147 
148  if (t_parent)
149  return t_parent->find_subfunction (name);
150 
151  return octave_value ();
152  }
153 
154  void
156  {
157  for (auto& nm_sf : m_subfunctions)
158  {
159  octave_function *fcn = nm_sf.second.function_value ();
160 
161  if (fcn)
162  fcn->mark_as_private_function (class_name);
163  }
164  }
165 
166  void
167  symbol_scope_rep::set_parent (const std::shared_ptr<symbol_scope_rep>& parent)
168  {
169  m_parent = std::weak_ptr<symbol_scope_rep> (parent);
170  }
171 
172  void
174  {
175  auto t_parent = m_parent.lock ();
176 
177  if (t_parent)
178  {
179  // fix bad symbol_records
180  for (auto& nm_sr : m_symbols)
181  {
182  symbol_record& ours = nm_sr.second;
183 
184  if (! ours.is_formal ()
185  && m_is_nested && t_parent->look_nonlocal (nm_sr.first, ours))
186  {
187  if (ours.is_global () || ours.is_persistent ())
188  error ("global and persistent may only be used in the topmost level in which a nested variable is used");
189  }
190  }
191 
192  // The scopes of nested functions are static.
193  if (m_is_nested)
194  m_is_static = true;
195  }
196  else if (m_children.size ())
197  {
198  // Parents of nested functions have static scopes.
199  m_is_static = true;
200  }
201 
202  for (auto& scope_obj : m_children)
203  scope_obj.update_nest ();
204  }
205 
206  bool
209  {
210  table_iterator p = m_symbols.find (name);
211  if (p == m_symbols.end ())
212  {
213  auto t_parent = m_parent.lock ();
214 
215  if (m_is_nested && t_parent)
216  return t_parent->look_nonlocal (name, result);
217  }
218  else if (! p->second.is_automatic ())
219  {
220  result.bind_fwd_rep (shared_from_this (), p->second);
221  return true;
222  }
223 
224  return false;
225  }
226 
227  void
229  (const std::shared_ptr<symbol_scope_rep>& curr_scope)
230  {
231  for (auto& nm_sr : m_symbols)
232  nm_sr.second.bind_fwd_rep (curr_scope,
233  curr_scope->find_symbol (nm_sr.first));
234  }
235 
236  void
238  {
239  for (auto& nm_sr : m_symbols)
240  nm_sr.second.unbind_fwd_rep ();
241  }
242 }
void set_parent(const std::shared_ptr< symbol_scope_rep > &parent)
Definition: symscope.cc:167
octave_value dump(context_id context) const
Definition: symrec.h:693
octave_value dump_function_map(const std::map< std::string, octave_value > &fcn_map)
Definition: fcn-info.cc:825
void bind_script_symbols(const std::shared_ptr< symbol_scope_rep > &curr_scope)
Definition: symscope.cc:229
octave_value dump(void) const
Definition: symscope.cc:113
bool is_global(void) const
Definition: symrec.h:653
void unbind_script_symbols(void)
Definition: symscope.cc:237
symbol_record::context_id m_context
Definition: symscope.h:597
octave_value find(const std::string &name, const octave_value_list &args, bool skip_variables, bool local_funcs)
Definition: symscope.cc:43
octave_value fcn_table_find(const std::string &name, const octave_value_list &args=octave_value_list(), bool local_funcs=true)
Definition: symtab.cc:389
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
void error(const char *fmt,...)
Definition: error.cc:578
std::vector< symbol_scope > m_children
Child nested functions.
Definition: symscope.h:587
std::map< std::string, octave_value > m_subfunctions
Map from symbol names to subfunctions.
Definition: symscope.h:570
std::map< std::string, symbol_record > m_symbols
Map from symbol names to symbol info.
Definition: symscope.h:566
octave_function * fcn
Definition: ov-class.cc:1754
bool is_defined(void) const
Definition: ov-fcn.h:68
bool is_persistent(void) const
Definition: symrec.h:657
bool m_is_nested
If true, then this scope belongs to a nested function.
Definition: symscope.h:591
std::string name(void) const
Definition: symscope.h:537
bool m_is_static
If true then no variables can be added.
Definition: symscope.h:595
symbol_table & __get_symbol_table__(const std::string &who)
nd deftypefn *std::string name
Definition: sysdep.cc:647
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
virtual void mark_as_private_function(const std::string &cname="")
Definition: ov-fcn.h:129
bool is_formal(void) const
Definition: symrec.h:652
With real return the complex result
Definition: data.cc:3260
std::string m_name
Name for this scope (usually the corresponding filename of the function corresponding to the scope)...
Definition: symscope.h:562
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
symbol_record & insert(const std::string &name, bool force_add=false)
Definition: symscope.cc:88
octave_value global_varval(const std::string &name) const
Definition: symtab.h:174
std::weak_ptr< symbol_scope_rep > m_parent
Parent of nested function (may be null).
Definition: symscope.h:583
p
Definition: lu.cc:138
std::map< std::string, octave_value >::const_iterator subfunctions_const_iterator
Definition: symscope.h:63
std::map< std::string, symbol_record >::iterator table_iterator
Definition: symscope.h:60
virtual octave_function * function_value(bool silent=false)
Definition: ov-base.cc:871
octave_value varval(context_id context) const
Definition: symrec.h:624
octave_value find_subfunction(const std::string &name) const
Definition: symscope.cc:139
void mark_subfunctions_in_scope_as_private(const std::string &class_name)
Definition: symscope.cc:155
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
void mark_added_static(void)
Definition: symrec.h:666
octave_value dump_symbols_map(void) const
Definition: symscope.cc:124
bool look_nonlocal(const std::string &name, symbol_record &result)
Definition: symscope.cc:207