File: jvm-native-flag-support.md

package info (click to toggle)
kotlin 1.3.31%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 109,908 kB
  • sloc: java: 454,756; xml: 18,599; javascript: 10,452; sh: 513; python: 97; makefile: 69; ansic: 4
file content (67 lines) | stat: -rw-r--r-- 2,398 bytes parent folder | download | duplicates (2)
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
# Support for JVM's ACC_NATIVE flag

Goal: enable JNI interop for Kotlin, so that anything that works through JNI in Java could be reproduced 1-to-1 in Kotlin

## Syntax

The following definition is present in the library:

``` kotlin
package kotlin.jvm

Retention(RetentionPolicy.SOURCE)
public annotation class native
```

This annotation is applicable to
 - functions
 - property accessors

A declaration marked with this annotation is referred to as a *native declaration*.

Checks to perform:
 - a native declaration can not be abstract
 - a native declaration can not have a body
 - a native declaration can not be marked `inline`
 - a native declaration can not have `reified` type parameters
   NOTE: this is achieved through prohibiting `inline`, as `reified` is only allowed on inline-functions now
 - members of traits can not be native

NOTE: native members can override open (or abstract) members of supertypes

## Semantics on the JVM

Intuition: a JVM method whose source declaration is native is marked with the `ACC_NATIVE` flag, and has no `CODE` attribute.

### Interaction with \[platformStatic\]

A native member of an object marked `native` and `platformStatic` is translated in a straightforward way:
there is only one JVM method corresponding to it, and it is marked as `ACC_NATIVE`.

A member of a companion object of class `C` marked `native` and `platformStatic` yields two JVM methods:
 - static member of `C` that is marked `ACC_NATIVE`;
 - instance member of `C$object` that is not marked `ACC_NATIVE` and its body delegates to the native static method.

### Top-level declarations

A native member of package `p` yields one JVM method:
 - member of a package-facade class `PPackage` which is marked with `ACC_NATIVE` flag.

### Native Property Accessors

A property can not be marked `native`.
A *property accessor* can be marked `native` if it has no body (i.e. it is a *default accessor*).
In this case the generated code is the same as for a native function defined in the same context as the property.

Example:
``` kotlin
val foo: Int
  [native] get
```

## Not implemented (yet)

- native property accessors
 - frontend: when accessors are default and native, don't require an initializer, don't allow a backing field
 - backend: when accessors are default and native, don't generate a backing field
- applicability checks: only functions and property accessors