Commit Graph

2719 Commits

Author SHA1 Message Date
Dmitry Petrov 5b8c0d4c3e Optimize for-in-string loops
For-in-string loop can be generated using specialized 'length' and
'charAt' method calls, and with cached string length.
Note that update of the string variable in loop body doesn't affect
loop execution semantics.

 #KT-21322 Fixed Target versions 1.2.20
2017-11-29 10:15:38 +03:00
Dmitry Petrov e2fa613b70 Cache array length in for-in-array loop if possible
If the range expression is not a local variable (which can be updated in
the loop body affecting loop behavior, see KT-21354), we can cache the
array length, thus turning a for-in-array loop into a simple optimizable
counter loop.

 #KT-21321 In Progress
2017-11-29 10:15:32 +03:00
Alexander Udalov 386a3fb5ce Fix tests after f4f5359725 2017-11-27 16:03:38 +01:00
Dmitry Petrov f586bd4a34 Generate proper visibility for companion object instance field in LV1.3+ 2017-11-27 17:15:16 +03:00
Dmitry Petrov 70d3e6592d Unwrap object member imported by name before determining receivers
Existing code for receiver generation accidentally worked in most cases
for object members imported by name. However, it generated strange
bytecode (such as
    GETFIELD AnObject.INSTANCE
    GETFIELD AnObject.INSTANCE
    POP
), and worked incorrectly for augmented assignments.

 #KT-21343 Fixed Target versions 1.2.20
2017-11-27 17:15:16 +03:00
Alexey Andreev 71b1591044 JS: replace suspend inline metadata after inlining
This fixes some issues on coroutine inlining, see tests
2017-11-27 17:01:18 +03:00
Alexey Andreev f8e7861ce6 JS: add partial tail-call optimization for suspend functions 2017-11-27 17:01:17 +03:00
Alexey Andreev 26843509c7 JS: disable suspend function tail-call optimization based on FE data
Partially fixes KT-21026
2017-11-27 17:01:16 +03:00
Alexey Andreev 6ab87ad66a Fix failing JS tests 2017-11-27 17:01:15 +03:00
Alexander Udalov 938fd1a57e Use ResolvedCall for callable reference in KCallableNameProperty intrinsic
Instead of manually inspecting the DOUBLE_COLON_LHS slice, which is a
bit more error-prone. Note that new tests were passing before this
change
2017-11-27 12:46:56 +01:00
Alexander Udalov f4f5359725 Fix exception on inlining callable reference with implicit this in LHS
Use ResolvedCall to determine the receiver type in the JVM codegen,
instead of manually inspecting the PSI

 #KT-20821 Fixed
2017-11-27 12:46:55 +01:00
Alexey Andreev ec8adfe7c4 JS: fix referencing outer class from secondary constructor
See KT-21041
2017-11-21 12:22:10 +03:00
Alexey Andreev 32a0221474 JS: refactor code that copies default methods in interfaces to classes
Als fixes KT-21245
2017-11-21 12:18:16 +03:00
Alexey Andreev a9548b1224 JS: fix translation of safe calls to suspend unit functions
See KT-21317
2017-11-20 19:19:24 +03:00
Ilmir Usmanov 0fbbe10143 Support suspendCoroutineUninterceptedOrReturn intrinsic
KT-17336: Fixed
2017-11-20 18:12:41 +03:00
Mikhael Bogdanov a547019ed0 Switch DEFAULT_METHOD_CALL_FROM_JAVA6_TARGET according to LL 2017-11-17 13:48:44 +01:00
Alexey Andreev 89db4dfe79 JS: translate when against enum to JsSwitch when possible 2017-11-17 11:07:41 +03:00
Alexey Andreev 678b4c67c1 JS: translate when to JsSwitch when possible
See KT-21160
2017-11-17 11:07:38 +03:00
Alexey Andreev 21de76f88d JS: fix identifier generation in delegate constructor calls
See KT-21093
2017-11-17 11:07:34 +03:00
Dmitry Petrov 4193fae9fa Fix floating point comparison generation for range literals 2017-11-16 10:54:25 +03:00
Dmitry Petrov 354d54aef6 Don't generate DUPX instructions for in-range-literal expressions
Store argument into a local variable instead.
2017-11-16 10:54:25 +03:00
Ilmir Usmanov f4e180556c Fix assignment codegen for suspend operators plus and plusAssign
KT-16079: Fixed
2017-11-14 14:49:26 +03:00
Dmitry Petrov 15ac471626 Treat accessors to JvmStatic methods as having no dispatch receiver
#KT-21246 Fixed
2017-11-14 09:59:16 +03:00
Dmitry Petrov 4365084645 Lookup for local variables taking into account uninitialized this
Consider a context with uninitialized this, e.g.:

  fun foo() {
    val x = "..."
    class Local(y: String) : Base(L@{ x + y })
  }

Lambda 'L' is an argument of a super class constructor call.
Here 'this@Local' is not initialized yet. Thus local variables captured
in 'Local' can't be used. Instead, they should be captured by lambda 'L'
itself.

Note that lambda 'L' sees both 'x' and 'y' as local variables that
should be captured.

When in context with uninitialized this (generating arguments for super
type constructor or delegating constructor call), and a variable in
question is not found in the current context, use enclosing local lookup
to determine whether a local variable should be captured by a closure.
2017-11-14 09:33:28 +03:00
Dmitry Petrov bbb0389c51 Fix determining enclosing class for closure
Enclosing class for closure is a class whose instance is captured by
closure as an outer 'this', and stored in a field 'this$0'.
Usually enclosing class for closure is an immediate outer class,
including classes for nested closures. For example:

  class C {
    fun foo() {}
    val example1 = L1@ { foo() }
    // Enclosing class for lambda 'L1' is 'C'
    val example2 = L2a@ { L2b@ { foo() } }
    // Enclosing class for nested lambda 'L2b'
    // is a closure class for outer lambda 'L2a'
  }

However, if the closure is created in a super type constructor call for
the outer class, corresponding instance is considered "uninitialized",
and can't be used as a proper class instance, and can't be referenced:
corresponding code is rejected by front-end.

  class Outer {
    fun foo() {}
    inner class Inner : Base(L3@ { foo() })
    // Enclosing class for lambda 'L3' is 'Outer',
    // because 'Inner' is uninitialized in super type constructor call.
  }

In CodegenAnnotatingVisitor, we maintain a stack of currently
uninitialized classes, and chose enclosing class for closure
as an inner-most surrounding class with initialized instance.

When generating code for this or outer class instance, we skip
contexts corresponding to classes with uninitialized instances.

This fixes a number of bytecode verification errors caused by incorrect
enclosing class for closure.

 #KT-4174 Fixed Target versions 1.2.20
 #KT-13454 Fixed Target versions 1.2.20
 #KT-14148 Fixed Target versions 1.2.20
2017-11-14 09:33:28 +03:00
Mikhael Bogdanov dff6e943bb Always do stack spilling during inline cause of dex problem
Dex issue: https://issuetracker.google.com/issues/68796377

  #KT-20844 Fixed
2017-11-13 16:50:24 +01:00
Ilmir Usmanov 0f9a21d429 Add regression test for KT-17640 2017-11-10 19:16:06 +03:00
Mikhael Bogdanov 416392bb74 Don't propagate reified markers for special enum functions
#KT-18254 Fixed
2017-11-08 15:35:44 +01:00
Mikhael Bogdanov 800cc63347 Remove external finally block interval on splitting during inline
#KT-20433 Fixed
2017-11-08 15:35:43 +01:00
Alexey Andreev 779af375a7 JS: fix inlining of array constructor expressed via typealias
See KT-20978
2017-11-08 16:12:16 +03:00
Alexey Andreev bd7129d0d6 JS: fix non-abstract extension properties in interfaces
KT-20994
2017-11-03 17:29:44 +03:00
Ilmir Usmanov 817f79520a Implement coroutineContext intrinsic
This intrinsic allows coroutine to access its context without suspention
and, furthermore, disabling tail-call optimization.
KT-17609: Fixed
2017-11-02 12:45:24 +03:00
Dmitry Petrov cbef372e69 Recapture shared variables when calling local class constructor
When a local function or class A creates an instance of a local class B
capturing an outer variable 'x', it should use ref for 'x', but not the
value of 'x'.
2017-10-30 15:19:13 +03:00
Dmitry Petrov 7a156e4407 Give unique names to fields for captured local functions
When a local function is captured, corresponding field accesses are
later transformed by the inliner. It doesn't have enough information to
restore the original semantics completely, so it has to rely on field
names. Local functions can be overloaded or can have names matching
local variable names, in both cases we generated fields with the same
name for captured values.

Now, we use the same '$<local-class-number>' suffix for field names for
local functions as it is present in the corresponding local class name.
This allows to distinguish captured local functions from captured local
variables and between different overloads of a function with the same
name.

 #KT-19827 Fixed
 #KT-18639 Fixed
2017-10-30 15:19:13 +03:00
Dmitry Petrov da99a100cc Additional constructor call normalization tests 2017-10-30 15:19:13 +03:00
Alexey Andreev 1f643ddce8 JS: add test to prove that KT-11910 is not reproducible anymore 2017-10-30 13:12:12 +03:00
Dmitry Petrov 28535a57d8 Fix issues with enum entry self-reference
Given a singleton class 'S' with possibly uninitialized static instance
(enum entry, interface companion object).
Such singleton can be referenced by name, or as an explicit or implicit
'this'.
For a given singleton class 'S' we
either use 'this@S' from context (local or captured),
or 'S' as a static instance.

Local or captured 'this@S' should be used if:
  - we are in the constructor for 'S',
    and corresponding instance is initialized
        by super or delegating constructor call;
  - we are in any other member of 'S' or any of its inner classes.

Otherwise, a static instance should be used.
2017-10-26 16:11:58 +03:00
Alexander Udalov e73760d4ff Fix test data for empty LHS vs isInitialized
Ignore the "property imported from object" part on JS
2017-10-25 12:10:10 +02:00
Dmitry Petrov 42945d33ee Use !LANGUAGE directive in tests for enhanced nullability 2017-10-25 12:30:32 +03:00
Dmitry Petrov 706a3698ec Add box tests for new nullability assertions 2017-10-25 12:30:32 +03:00
Dmitry Petrov 2a7d555be4 Minor: move nullability assertion tests javaInterop/notNullAssertions 2017-10-25 12:30:32 +03:00
Dmitry Petrov f23dfdc0ac Mark operands of POP2 as don't touch in unused expression elimination
Fixes KT-20879.
2017-10-24 15:38:15 +03:00
Alexander Udalov bafc75ade4 Fix isInitialized receiver generation for empty LHS
#KT-20774 Fixed
2017-10-24 11:26:01 +02:00
Mikhail Zarechenskiy 73b3efd628 Remove LANGUAGE_VERSION directive from blackBox codegen tests 2017-10-24 11:44:02 +03:00
Zalim Bashorov f9809d5a73 Minor: replace IGNORE_BACKEND with TARGET_BACKEND for the test what should be run only on JVM
The test was written to check the case for Platform Types which now exists only on JVM.
2017-10-20 21:02:10 +03:00
Anton Bannykh b5d32f420d JS: correct Double and Float conversions to Int (KT-8374 fixed) 2017-10-13 20:29:28 +03:00
Alexander Udalov 5f775497e2 Add Kotlin package FQ name to kotlin.Metadata
It might differ from the JVM package FQ name if the JvmPackageName
annotation is used. This will be useful for faster indexing in the IDE
and for reflection
2017-10-13 16:23:07 +02:00
Dmitry Petrov 16b7bece46 Fix constant expression inlining logic
Constant expressions are inlined if they do not depend on non-inlineable
vals.
Java constants are always inlined.
Kotlin constants are inlined in LV 1.1+.
2017-10-13 17:01:42 +03:00
Stanislav Erokhin 4932fa1ddd Support expected type from explicit cast
This commit support the following case.
Suppose we have such declaration:
  fun <T> foo(): T { ... }

Then in code we want to use it like this: `foo() as String`.
But in LV <= 1.1 we have type inference error: "Not enough
information for type parameter `T`". This error happened because we
do not use type from cast as expected type for call.

In this commit we fix this problem and use this type as expected type
in following cases:
 - our function has only one type parameter (this can be relaxed later)
 - function parameter types and extension receiver type not contains `T`

Also this fix problem with `findViewById`.
Already signature was: `fun findViewById(...): View`
and was used like: `findViewById() as MyView`.
New signature is `fun <T : View> findViewById(...): T`
and old usage was broken because of problem described above
2017-10-11 19:23:52 +03:00
Anton Bannykh ac508a510e JS: Support isInitialized intrisic for lateinit properties 2017-10-11 19:23:49 +03:00