The following changes should fix real and potential problems
regarding how variables are set from function return values.
In assign from call expressions, Values in caller frame are now
directly assigned from function calls (call(), binCall() or builtins).
The assignement is performed with reflect Set methods or variants,
instead of "=" operator, to enforce runtime type checks.
The assignX() and assignX2() builtins are now removed in favor of
above method.
The representation of nil for pointer on struct has been fixed.
The identification of channel is fixed in for-range channel expression.
* feat: add support for named output variables
Function output parameters are located at the start of the function
frame, uninitialized, as they could be only set through return
statements.
Supporting named output variables requires to:
- identify and allocate symbols corresponding to output names:
done at ident parsing, with the funcRet() helper,
- compute the location of output name in the frame:
done with retRank() helper,
- initialize the frame entry with the zero value of symbol type,
as the symbol can be accessed and written at multiple times,
and return not setting the variable (opposite to unnamed).
Done with frameType() helper, which now takes into account
output parameters.
* refactor: simplify memory management
Perform function frame analysis at pre-order, instead of post-order, to
initialize memory frame, and track value types. Remove all operation
involving unitialized types. Frame memory layout is now build
incrementally, instead of having to perform dedicated tree walks on AST.
Define a new Method action to detect method calls on interface types.
In this case the receiver is resolved from a dynamic type rather than a static one.
Handle method calls where receiver is a pointer.
In typeAssert, always return an interface value with the concrete type, to fix method lookup on interface objects.
Add relevant tests.
A Wrap represents the wrapper which allows objects created by the
interpreter to satify Go interfaces (despite limitations in reflect
which forbid dynamic method creation).
All the struct fields are functions, where the fied name corresponds
to the method name prefixed by "Do". The function signature must
be the same as the interface one.
A corresponding Wrap method Xyz which satisfies the interface must
exist and must invoke the DoXyz function.
To be usable, the interpreter should return a Wrap instance with
the relevant function fields filled. The application can then invoke
methods on it. The method calls will be forwarded to the interpreter.
Only the Wrap type definition needs to be exported to the interpreter
(not the interfaces and methods definitions)
A complete working example test is provided, and necessary corrections
in binary struct fields as functions.
Wrap functions in struct fields in `reflect.Value` to have a consistent
representation of function inside and outside the interpreter.
Teach assign to wrap a function node if destination is reflect.Value
The same needs to be done for composite literal, in a next commit.
The field index computed from the key value expression in sparse
composite literal was stored in `node.findex`, corrupting the memory
allocation. It is now stored in a local variable for immediate use.
Type category `UnsetT` is renamed in `NilT`.
Catch invalid use of untyped nil in `:=` expression.
Convert nil to output parameter type when used in `return` expression.
Improve and add relevant unit tests.
Call genNodeWrapper on interpreted function returned by a closure,
itself being wrapped, to ensure that the function can be called from
binary, no matter the level of nesting closures.
At CFG, in pre-order processing, determine the correct type of CompositeLitExpr from its first child (if it's a type) or from the ancestor node.
Make sure The type is propagated to children so the algorithm works recursively.
Fix also the isType() method to handle case of imported types,
either from source or binary packages.
Do not ignore type node when used in a variable declaration with assign.
The source node in a assign node can be the 2nd or the 3rd (the 2nd being the type).
The code for comparison operators is now generated by genop.
The support for multiple type has been added.
Missing operators <= and >= are now implemented.
Add `cmd/genop` program to generate operator functions in `interp/op.go`.
For each operator, determine its type, handle argument types conversion if necessary.
Arithmetic operators are added for now. Assign and comparison operators coming in a next commit.