Mercurial > octave
diff libinterp/parse-tree/pt-anon-scopes.cc @ 26661:cf9e10ce3351
move variable values from symbol_record objects to stack_frame objects
Apologies for the massive commit. I see no way to untangle these
changes into a set of smaller incremental changes in a way that would
be more useful.
Previously, handling data for recursive function calls was managed by
a stack of values in the symbol_record class and an auxiliary integer
variable was used for managing the recursion depth (context_id). Now,
values for local variables are in the stack_frame class and recursion
is handled naturally by the call_stack as a new stack frame is added
to the call_stack object for any call to a function or a script.
Values for internal function call information (nargin, nargout, etc.)
are now stored specially in the stack_frame object. Values for global
variables are now stored in a map in the call_stack object. Values
for persistent variables are stored in the corresponding
octave_user_function object. Access to non-local variables inside
nested functions is managed through pointers to stack_frame objects
for the parent function scopes. The new implementation more closely
resembles the techniques described in standard compiler literature.
These changes should make it easier to create proper closures and
finally solve bug #39257 (handles to nested functions are not yet
supported). They may also make it easier to implement JIT compiler,
though that is probably still a long way off.
The new stack-frame.h file has some details about the new
implementation of stack frames that should help in understanding how
things work now.
Describing each change to each file and function will probably not
provide much greater understanding of the changes and would be quite
tedious to write so I am omitting them.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 28 Jan 2019 18:01:46 +0000 |
parents | 00f796120a6d |
children | 581d01526b34 |
line wrap: on
line diff
--- a/libinterp/parse-tree/pt-anon-scopes.cc Sat Jan 26 15:53:29 2019 +0000 +++ b/libinterp/parse-tree/pt-anon-scopes.cc Mon Jan 28 18:01:46 2019 +0000 @@ -33,46 +33,38 @@ namespace octave { - tree_anon_scopes::tree_anon_scopes (octave_user_function *f) - : scopes (), merged_tables () + tree_anon_scopes::tree_anon_scopes (tree_anon_fcn_handle& anon_fh) + : m_params (), m_vars () { - if (f) - { - if (! f->is_anonymous_function ()) - panic_impossible (); - - // Collect the scope of the outer anonymous function. - - stash_scope_if_valid (f->scope ()); - - // Further walk the tree to find nested definitions of further - // anonymous functions. - - tree_statement_list *cmd_list = f->body (); - - if (cmd_list) - cmd_list->accept (*this); - - // Collect symbol records of all collected scopes. - - merge_tables (); - } + visit_anon_fcn_handle (anon_fh); } void tree_anon_scopes::visit_anon_fcn_handle (tree_anon_fcn_handle& afh) { - // Collect the scope of this anonymous function. + tree_parameter_list *param_list = afh.parameter_list (); + tree_expression *expr = afh.expression (); + + // Collect names of parameters. - stash_scope_if_valid (afh.scope ()); + if (param_list) + { + std::list<std::string> pnames = param_list->variable_names (); + + for (const auto& nm : pnames) + m_params.insert (nm); - // Further walk the tree to find nested definitions of further - // anonymous functions. + // Hmm, should this be included in the list returned from + // tree_parameter_list::variable_names? + if (param_list->takes_varargs ()) + m_params.insert ("varargin"); + } - tree_expression *e = afh.expression (); + // Further walk the tree to find free variables in this expression + // and any nested definitions of additional anonymous functions. - if (e) - e->accept (*this); + if (expr) + expr->accept (*this); } // The rest of visit_... methods is only for walking the tree. Many of @@ -191,8 +183,12 @@ } void - tree_anon_scopes::visit_identifier (tree_identifier& /* id */) + tree_anon_scopes::visit_identifier (tree_identifier& id) { + std::string nm = id.name (); + + if (m_params.find (nm) == m_params.end ()) + m_vars.insert (nm); } void @@ -417,17 +413,5 @@ { panic_impossible (); } - - void - tree_anon_scopes::merge_tables (void) - { - for (const auto& sc : scopes) - { - symrec_list vars = sc.all_variables (); - - for (const auto& symrec : vars) - merged_tables[symrec.name ()] = symrec; - } - } }