1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1993-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING. If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#include <sstream>
#include "file-ops.h"
#include "fcn-info.h"
#include "interpreter-private.h"
#include "interpreter.h"
#include "ov-fcn.h"
#include "ov-usr-fcn.h"
#include "symrec.h"
#include "symscope.h"
#include "utils.h"
namespace octave
{
symbol_record symbol_scope_rep::insert_local (const std::string& name)
{
symbol_record sym (name);
insert_symbol_record (sym);
return sym;
}
void symbol_scope_rep::insert_symbol_record (symbol_record& sr)
{
size_t data_offset = num_symbols ();
std::string name = sr.name ();
sr.set_data_offset (data_offset);
m_symbols[name] = sr;
}
symbol_record symbol_scope_rep::insert (const std::string& name)
{
table_iterator p = m_symbols.find (name);
if (p == m_symbols.end ())
{
symbol_record ret (name);
size_t data_offset = num_symbols ();
ret.set_data_offset (data_offset);
auto t_parent = m_parent.lock ();
size_t offset = 0;
if (is_nested () && t_parent
&& t_parent->look_nonlocal (name, offset, ret))
return m_symbols[name] = ret;
else
{
if (m_is_static)
ret.mark_added_static ();
return m_symbols[name] = ret;
}
}
else
return p->second;
}
std::list<octave_value> symbol_scope_rep::localfunctions (void) const
{
std::list<octave_value> retval;
// Find the subfunctions of this function (which should be the
// primary parent function for this scope).
// 1) m_subfunction_names contains only valid subfunctions
// 2) m_subfunctions contains both nested functions and subfunctions
// loop over them.
for (const auto& nm : m_subfunction_names)
{
auto nm_fcn_iter = m_subfunctions.find (nm);
if (nm_fcn_iter != m_subfunctions.end ())
{
octave_value ov_fcn = nm_fcn_iter->second;
octave_user_code *fcn = ov_fcn.user_code_value ();
if (! fcn)
continue;
octave::symbol_scope scope = fcn->scope ();
std::list<std::string> plst = scope.parent_fcn_names ();
octave_fcn_handle *fh = new octave_fcn_handle (ov_fcn, nm, plst);
retval.push_back (octave_value (fh));
}
}
return retval;
}
octave_value
symbol_scope_rep::dump (void) const
{
std::map<std::string, octave_value> m
= {{ "name", m_name },
{ "nesting_depth", m_nesting_depth },
{ "is_static", m_is_static },
{ "symbols", dump_symbols_map () },
{ "subfunction_names", string_vector (m_subfunction_names) },
{ "subfunctions", dump_function_map (m_subfunctions) }};
return octave_value (m);
}
octave_value
symbol_scope_rep::dump_symbols_map (void) const
{
std::map<std::string, octave_value> info_map;
for (const auto& nm_sr : m_symbols)
{
std::string nm = nm_sr.first;
symbol_record sr = nm_sr.second;
info_map[nm] = sr.dump ();
}
return octave_value (info_map);
}
std::list<symbol_record> symbol_scope_rep::symbol_list (void) const
{
std::list<symbol_record> retval;
for (const auto& nm_sr : m_symbols)
retval.push_back (nm_sr.second);
return retval;
}
octave_value
symbol_scope_rep::find_subfunction (const std::string& name) const
{
subfunctions_const_iterator p = m_subfunctions.find (name);
if (p != m_subfunctions.end ())
return p->second;
auto t_parent = m_parent.lock ();
if (t_parent)
return t_parent->find_subfunction (name);
return octave_value ();
}
void
symbol_scope_rep::mark_subfunctions_in_scope_as_private (const std::string& class_name)
{
for (auto& nm_sf : m_subfunctions)
{
octave_function *fcn = nm_sf.second.function_value ();
if (fcn)
fcn->mark_as_private_function (class_name);
}
}
void
symbol_scope_rep::set_parent (const std::shared_ptr<symbol_scope_rep>& parent)
{
m_parent = std::weak_ptr<symbol_scope_rep> (parent);
}
void
symbol_scope_rep::set_primary_parent (const std::shared_ptr<symbol_scope_rep>& parent)
{
m_primary_parent = std::weak_ptr<symbol_scope_rep> (parent);
}
void
symbol_scope_rep::cache_dir_name (const std::string& name)
{
m_dir_name = octave::sys::canonicalize_file_name (name);
}
bool
symbol_scope_rep::is_relative (const std::shared_ptr<symbol_scope_rep>& scope) const
{
if (is_nested ())
{
// Since is_nested is true, the following should always return a
// valid scope.
auto t_parent = m_parent.lock ();
if (t_parent)
{
// SCOPE is the parent of this scope: this scope is a child
// of SCOPE.
if (t_parent == scope)
return true;
}
auto t_primary_parent = m_primary_parent.lock ();
if (t_primary_parent)
{
// SCOPE is the primary parent of this scope: this scope is a
// child (or grandchild) of SCOPE.
if (t_primary_parent == scope)
return true;
// SCOPE and this scope share the same primary parent: they are
// siblings (or cousins)
auto scope_primary_parent = scope->primary_parent_scope_rep ();
if (t_primary_parent == scope_primary_parent)
return true;
}
}
return false;
}
void symbol_scope_rep::update_nest (void)
{
auto t_parent = m_parent.lock ();
if (t_parent)
{
// fix bad symbol_records
for (auto& nm_sr : m_symbols)
{
symbol_record& ours = nm_sr.second;
size_t offset = 0;
if (! ours.is_formal () && is_nested ())
t_parent->look_nonlocal (nm_sr.first, offset, ours);
}
// The scopes of nested functions are static.
if (is_nested ())
m_is_static = true;
}
else if (m_children.size ())
{
// Parents of nested functions have static scopes.
m_is_static = true;
}
std::list<std::string> plst = parent_fcn_names ();
plst.push_front (m_fcn_name);
for (auto& scope_obj : m_children)
{
scope_obj.cache_parent_fcn_names (plst);
scope_obj.update_nest ();
}
}
bool symbol_scope_rep::look_nonlocal (const std::string& name,
size_t offset, symbol_record& result)
{
offset++;
table_iterator p = m_symbols.find (name);
if (p == m_symbols.end ())
{
auto t_parent = m_parent.lock ();
if (is_nested () && t_parent)
return t_parent->look_nonlocal (name, offset, result);
}
else
{
// Add scope offsets because the one we found may be used in
// this scope but initially from another parent scope beyond
// that. The parent offset will already point to the first
// occurrence because we do the overall nesting update from the
// parent function down through the lists of all children.
size_t t_frame_offset = offset + p->second.frame_offset ();
size_t t_data_offset = p->second.data_offset ();
result.set_frame_offset (t_frame_offset);
result.set_data_offset (t_data_offset);
return true;
}
return false;
}
std::list<octave_value> symbol_scope::localfunctions (void) const
{
if (! m_rep)
return std::list<octave_value> ();
if (is_primary_fcn_scope ())
return m_rep->localfunctions ();
std::shared_ptr<symbol_scope_rep> ppsr
= m_rep->primary_parent_scope_rep ();
if (! ppsr)
return std::list<octave_value> ();
return ppsr->localfunctions ();
}
}
|