Commit Graph

519 Commits

Author SHA1 Message Date
pyos 159d292ea7 JVM_IR: make continuation detection more consistent
* unify various checks for whether a suspend function needs a
    continuation;

  * mark the continuation parameter with an origin (this also allows
    correctly computing the offset of the local in codegen -- see the
    new test);

  * when wrapping `suspend fun main`, use a suspend reference instead of
    a synthetic non-suspend lambda (required to correctly implement the
    previous point, as previously the continuation parameter was passed
    to main() itself correctly only because of very lenient checks in
    AddContinuationLowering).
2020-03-03 15:12:13 +01:00
Ilmir Usmanov 22de20e7e5 JVM_IR: Generate PUBLIC constructor for suspend lambda
if the lambda is inside inline function.
2020-03-02 14:03:35 +01:00
pyos c5ffbfd33c JVM_IR: mark origins of capture fields in suspend lambdas 2020-02-21 12:11:19 +01:00
pyos 6b98ea2378 JVM_IR: do not place suspend markers around crossinline lambda calls 2020-02-21 12:11:19 +01:00
pyos 39ebc8cfa5 Add a suspend test that fails on JVM_IR 2020-02-21 12:11:19 +01:00
pyos eff02b6e72 JVM_IR: improve suspend tail call detection.
* TailCallOptimizationLowering should go into local classes in order to
   transform their suspend methods;
 * the check for invokes of noinline lambda arguments in codegen was
   incorrect, as it also returned true for calls of lambdas stored in
   local variables;
 * IrInlineCodegen should mark non-inlinable arguments used as inline
   suspend parameters;
 * detection of suspend/inline call sites was incorrect (or maybe it's
   the `compilationContextDescriptor` that was incorrect?..)
2020-02-20 11:10:26 +01:00
Steven Schäfer ba90e87756 JVM, JVM IR: Fix assertion status for regenerated anonymous objects
We always set the $assertionsDisabled field based on the top-level
enclosing class. This means that for anonymous objects we have to
rewrite the call to Class.desiredAssertionStatus.
2020-02-19 11:23:24 +01:00
Steven Schäfer 5760c0be9c JVM IR: Avoid redundant coercions in ExpressionCodegen 2020-02-15 22:32:23 +03:00
Mikhail Zarechenskiy 7526162f6f Mute one inline codegen test for NI
#KT-36448
2020-02-13 11:54:08 +03:00
Mikhail Zarechenskiy abc5eb4740 [NI-MIGRATE-BAD] Update problematic/questionable tests
These tests are going to be reviewed in more detail before 1.4
2020-02-13 11:15:59 +03:00
pyos 5acb3e14fb IR: mark parameters for captures of crossinline parameters 2020-02-07 12:16:26 +01:00
pyos 2bf50cc91a JVM_IR: correctly name $$forInline versions of @JvmName suspend funs
Using a hack similar to $default stubs.
2020-02-07 12:16:26 +01:00
pyos 08074bb60e JVM_IR: wrap inline suspend references in suspend lambdas 2020-02-07 12:16:26 +01:00
Ilmir Usmanov 7dfd7b6081 Spill stack before analyzing it when looking for non-inline suspend lambda
parameters of inline function. Otherwise, it leads to AnalyzerException,
when inlined lambda contains try-catch block. The reason is simple:
in try block, we leave some variables on stack, while on catch block the
stack is empty. Spilling the variables before try block does the trick.
 #KT-34708 Fixed
2020-02-06 13:10:21 +01:00
Ilmir Usmanov fc70455877 JVM_IR: Move suspend function views creation to lowering
Now AddContinuationLowering is responsible for both adding continuation
classes to suspend functions and adding continuation parameters to
them.
Because we cannot create a view if inline suspend function is defined
in another file, we generate a stub without body when we encounter call
to it. And then, when we lower the file containing the function we add
the body. This way we have no unlowered views after the lowering.
Thus, after the lowering there should be no suspend function, which
are not views, therefore, remove VIEW origins.
Because transformations of suspend functions can copy them into another
object, use attribute as a key inside function to view map.
2020-02-05 15:07:37 +01:00
pyos bda5b0d5a9 JVM_IR: further refine synthetic accessor generation
References to protected members from crossinline lambdas in the same
package do not need accessors.
2020-01-31 13:20:30 +01:00
Alexander Udalov 2fca7f1a54 Add codegen tests for old behavior of null checks
#KT-22275
2020-01-24 11:56:28 +01:00
pyos 2f0f4e570f JVM_IR: do not remap locals in contexts nested inside lambdas
The only case where this code is reachable at all is objects in lambdas
that use reified type parameters of the outer inline function. Since the
type parameters are declared outside the inlining root, regenerating the
object is actually pointless, and remapping its captures even more so
(not to mention that the code under the condition uses the captures of
the lambda, not of the method it's currently transforming).
2020-01-22 15:36:09 +01:00
Alexander Udalov 99294cfdae Minor, fix test data
TypeCastException was unresolved after an accidental change in
f48bdc1fcb.
2020-01-21 11:26:21 +01:00
Alexander Udalov f48bdc1fcb Fix codegen box tests for language version 1.4
Since API version 1.4, NullPointerException is thrown for casts of null
to any type instead of TypeCastException.
2020-01-20 19:12:59 +01:00
Ilmir Usmanov 6ede10c1ca JVM_IR: Minor. Add test for SMAP of inner object/lambda of inline suspend function 2020-01-20 16:00:36 +01:00
Ilmir Usmanov 877509306d JVM_IR: Minor. Unmute tests. 2020-01-20 16:00:36 +01:00
Ilmir Usmanov 891a55d79a JVM_IR: Fix visibility of inner suspend lambda inside inline function
It should be public, otherwise, after inlining, we will get IllegalAccessError.
2020-01-20 16:00:36 +01:00
Ilmir Usmanov 9f5b51ed43 JVM_IR: Do not generate fake continuation markers inside inline suspend lambdas
Since the markers replace ALOAD 0 as continuations, passed to suspend calls, in
JVM_IR we do not need this, since in JVM_IR all inline lambdas are static
functions.
2020-01-20 16:00:35 +01:00
Ilmir Usmanov 2507e2b526 JVM_IR: Fix parents of $$forInline companions 2020-01-20 16:00:33 +01:00
Ilmir Usmanov 7167d5f75c JVM_IR: Generate $$forInline companion for transformed crossroutines 2020-01-20 16:00:33 +01:00
pyos 99eab5a058 IR: unify 3 copies of function body remapping
Also,

  1. remove some redundant copies;

  2. fix remapping of non-local returns in lambdas if the body is moved
     after LocalDeclarationsLowering (the lambda is no longer inside the
     body, but must still be visited)
2020-01-14 18:48:27 +03:00
pyos ef5fe0675a JVM_IR: refactor suspendFunctionView
fixing the check for DescriptorWithContainerSource in the process
(containerSource *may* be null).
2020-01-08 19:52:20 +01:00
Mikhael Bogdanov 4b6202c902 JVM_IR. Support inlining of bound CR 2019-12-30 08:35:46 +01:00
Georgy Bronnikov d4b0151f51 JVM_IR: fix name of received field for suspend lambdas
$ at the start caused AnonymousObjectTransformer to skip the field.
2019-12-23 18:03:46 +01:00
Ilmir Usmanov 9292022f88 JVM_IR: Support inner lambdas which capture crossinline 2019-12-23 18:03:42 +01:00
Ilmir Usmanov daa76cbf1e JVM_IR: Support inner objects with multiple suspend functions
Do not clean `functionsToAdd`
2019-12-23 18:03:41 +01:00
Ilmir Usmanov a1448ebb37 JVM_IR: Support crossinline suspend lambdas
The main idea is the following: since we need to generate
(fake)continuations before inlining, we move IrClasses of suspend
lambdas and continuation classes of named functions into the functions.
Thus, it allows the codegen to generate them prior to inlining and
the inliner will happily transform them for us.
Because of that, lowerings which transform call-site function are likely
to change reference to lowered suspend lambdas or functions.
Hence, do not rely on references to lowered suspend lambdas or
functions, instead, rely on attributes.

Do not generate continuation for inline suspend lambdas.
Previously, inline suspend lambdas were treated like suspend functions,
thus we generated continuations for them. Now we just do not treat them
as suspend functions or lambdas during AddContinuationLowering.
We should add continuation parameter to them, however.

Do not generate secondary constructor for suspend lambdas, otherwise,
the inliner is unable to transform them (it requires only one
constructor to be present).

Generate continuation classes for suspend functions as first statement
inside the function.
This enables suspend functions in local object inside inline functions.
Since we already have attributes inside suspend named functions, we
just reuse them to generate continuation class names. This allows us
to close the gap between code generated by old back-end and the new
one.

If a suspend named function captures crossinline lambda, we should
generate a template for inliner: a copy of the function without
state-machine and a continuation constructor call. The call is needed
so the inliner transforms the continuation as well.

Refactor CoroutineTransformerMethodVisitor, so it no longer depends on
PSI.
2019-12-23 18:03:40 +01:00
pyos a4b005fd5d PSI2IR: generate field writes for all val property assignments
Assuming the frontend is correct, and it hopefully is, all of them are
initializations even if not done in the owner class directly.
2019-12-20 13:03:39 +03:00
Mikhael Bogdanov 650cfeaaed Update inline tests to void PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR 2019-12-19 12:46:36 +01:00
Igor Chevdar cca3f13e48 Added multi-module test on inline functions 2019-12-18 12:45:41 +03:00
Mikhael Bogdanov 03c2350e79 Keep original casts during reification to avoid VerifyError
#KT-26435 Fixed
2019-12-17 12:41:22 +01:00
Mikhael Bogdanov 26032e4297 Split exception table on finally insertion before non-local return
in nested try blocks without finally

 #KT-31653 Fixed
2019-12-12 13:33:40 +01:00
Ilmir Usmanov df0a86ea57 Add NOP as first instruction in coroutine's try blocks
#KT-35035 Fixed
2019-12-11 15:01:41 +01:00
Mikhael Bogdanov b9dee4e93a Remove assertion on sorted ranges: it could be empty if lambda doesn't contain any linenumber
#KT-35101 Fixed
2019-12-02 12:08:36 +01:00
Mikhael Bogdanov cf6f823d29 Use descriptor from resolved call to inline accessors
In case of inline it should be same descriptor (except of fake override), In general case getter could be synthetic accessor and in such case it's not inline
2019-11-29 13:15:42 +01:00
Alexander Udalov a485a5ffd6 JVM IR: load fields for JvmField properties from dependencies
This is needed to properly lower JvmField property calls to field
accesses.
2019-11-20 15:35:12 +01:00
Mikhael Bogdanov d28ec1d449 Add test for default lambda inlining in suspend inline 2019-11-20 12:57:41 +01:00
Mikhael Bogdanov 19ce055322 JVM_IR. Pass stub function reference in 'getSignature' 2019-11-20 12:57:38 +01:00
Mikhael Bogdanov ac31e0e8c7 Support default lambda inlining in IR 2019-11-20 12:57:36 +01:00
pyos ce0fb662c0 JVM_IR: fold inline lambdas when computing OUTERCLASS
so that the enclosing method of objects defined inside lambdas is the
one they are declared in.

Note that this does not fix *all* enclosingInfo tests because JVM_IR
currently follows the KT-28064 proposal, i.e. does not regenerate
objects defined inside lambdas under any circumstances. For example,
this causes test boxInline/enclosingInfo/inlineChain2.kt to fail because
the enclosing method of objects is _2Kt.box instead of (non-existent in
source code) `_2Kt$box$inlined$call$1.invoke` or whatever. What's more
important is that OUTERCLASS no longer points to a non-existent
`box$lambda-N` and therefore `.enclosingMethod` no longer throws.
2019-11-12 12:44:46 +01:00
pyos f906524d76 Mark a SAM conversion test as JVM-only 2019-11-12 12:24:55 +01:00
pyos 82fb5c4d19 JVM_IR: move lambda captures to end of signature when inlining
For example, a lambda `{ param -> captured }` of type `E.(T) -> U` will
be transformed by LocalDeclarationsLowering into a private static method

    fun f$lambda-0($this: E, $captured: U, param: T) = $captured

The reason for such an ordering is that a lambda looks the same as a
local function, and local function can have default arguments, and those
arguments can reference captured variables; thus, captured variables
must come before actual declared arguments.

However, this is not the order that the inliner wants. Moreover, since
it was written to handle lambdas represented as `invoke` methods of
anonymous objects, it does not expect the actual callable method to have
any parameters corresponding to captured variables at all. This results
in it attempting to generate a temporary node with descriptor

    (LE;LU;LT;LU;)LU;

while still using locals 1 and 2 as `param` and `$captured` respectively.
In the example above, this is not critical, as they both have reference
type and the lambda will eventually be pasted into a different node
anyway; however, if it happens that one of them is a primitive, or both
are primitives of different types, the bytecode will use incorrect
instructions, causing verification errors. The correct descriptor is

    (LE;LT;LU;)LU;
2019-11-11 13:46:42 +01:00
pyos 433e0e4740 JVM_IR: remember facade fqnames of imported classes
Necessary to support importing file classes annotated @JvmPackageName,
since the actual package fragment they are a part of has the name from
the `package` declaration.
2019-11-11 13:31:57 +01:00
pyos fb9f43c119 JVM_IR: fix load check in inliner (should accept primitives too) 2019-11-07 15:58:44 +01:00