Commit Graph

596 Commits

Author SHA1 Message Date
Dmitrii Gridin 072d191306 [compiler] replace Enum values() with entries
To fix warnings. Also, use of `Enum.entries` may improve the performance

^KT-48872
2024-02-20 17:56:46 +00:00
Johan Bay d50b02adaf [K/N] only include bodies inside inlines for headers
Also renames the "header klib serializaion" option to something
that hopefully slightly better reflects its meaning
^KT-65442
2024-02-20 08:03:08 +00:00
Johan Bay ac8aae5da4 [K/N] keep all declarations inside inline functions
^KT-65442
2024-02-20 08:03:08 +00:00
Johan Bay d4abc30096 [K/N] keep coordinates for inline functions
^KT-65442
2024-02-20 08:03:07 +00:00
Johan Bay 446db595ce [K/N] keep internal members in header klibs
This is also the behavior of jvm-abi-gen and desired to support
friend modules.
^KT-65442
2024-02-20 08:03:07 +00:00
Alexander Udalov 41c6cca689 IR: minor, remove unnecessary isFromJava check
This is an addition to b72effab98. For JVM, this code is not supposed
to affect anything, and for non-JVM, declarations from Java are not
possible.
2024-02-14 11:27:09 +00:00
Alexander Udalov b72effab98 IR: remove isFromJava check from isOverridableMemberOrAccessor
This is needed because in case a static member is inherited via a Kotlin
class (class C in the newly added test), its origin becomes
FAKE_OVERRIDE which is technically not Java anymore. After this change,
we'll build fake overrides for static members from superclasses
regardless of whether they come from Java or Kotlin.

Also, move the previous logic of
isOverridableFunction/isOverridableProperty to the only call site at
IdSignatureFactory.

 #KT-65589 Fixed
2024-02-13 21:03:33 +00:00
Sergej Jaskiewicz f26e470e80 [klib] Don't show signature clash diagnostics for local declarations
For some reason type parameters end up in
`GlobdalDeclarationTable`, and thus we tracked them in
`IdSignatureClashDetector`, which wasn't right and confused the
diagnostic renderer that uses
`org.jetbrains.kotlin.resolve.MemberComparator` for sorting the
declarations to display in diagnostics. That comparator doesn't know
jow to work with type parameters.

Besides, type parameters, like many other types of declarations, are not
considered public wrt KLIB ABI, so there's no need to show
CONFLICTING_KLIB_SIGNATURES_ERROR for them.

^KT-65723 Fixed
2024-02-12 11:22:46 +00:00
Sergej Jaskiewicz 03ca64c954 [klib] Make CONFLICTING_KLIB_SIGNATURES_DATA diagnostic more precise
Show what kind of declarations exactly are clashing: functions,
properties, or fields.

This is so that diagnostics about clashing properties and fields are
distinguishable from one another, since properties and fields
are rendered the same way in those diagnostics:
2024-02-09 16:45:34 +00:00
Sergej Jaskiewicz 06684f207a [klib] Don't rely on Lazy IR for signature clash checking
We only want to report signature clashes for declarations that come
from the module currently being serialized. In GlobalDeclarationTable,
declarations from other modules could also be stored.
Checking whether a declaration is a Lazy IR declaration to
determine if it comes from an external module works okay, but it is
a hack which relies on an implementation detail of IR, which may or
may not work in the future.

Use a more robust logic here, since IrFileSerializer is always aware
which declarations are declared in the current module and which are
just referenced from it.
2024-02-05 17:30:08 +00:00
Sergej Jaskiewicz 03aa14b473 [klib] Use "IR signatures" i/o "KLIB signatures" in diagnostics
We already use the term "IR signatures" in other places.
2024-01-30 20:47:09 +00:00
Sergej Jaskiewicz 6900e20096 [klib] Fix exception for clashing signatures from different modules
If we encounter a declaration in the current module whose signature
is the same as that of a declaration in another module which we happen
to also reference from the current module, don't report any errors,
just like we don't do it in Kotlin/JVM. This leaves the user in the KLIB
hell situation, but this is intentional, because otherwise a legitimate
change like moving a declaration to another module and marking
the original one as `@Deprecated("", level = DeprecationLevel.HIDDEN)`
would lead to a error, and we don't want that.

Also, don't try to show the diagnostics on a declaration that doesn't
have an IrFile.

^KT-65063 Fixed
2024-01-30 20:47:09 +00:00
Sergej Jaskiewicz f39335b3f7 [klib] Don't pass around metadata serialization closure
Pass the metadata serializer instance instead. This allows to further
reduce code duplication by introducing the common interface
`KlibSingleFileMetadataSerializer` for abstracting away K1 and K2
representation of a source file, as well as reusing
`Fir2KlibMetadataSerializer` across different backends.

KT-64392
2024-01-29 21:07:43 +00:00
Sergej Jaskiewicz 5a6f51e474 [klib] Move KlibMetadataSerializer to serialization.js
It's used only in JS compilation.
2024-01-29 21:07:43 +00:00
Sergej Jaskiewicz abc53b2ee3 [klib] Inline the makeSerializedKlibMetadata function
It's used in only one place, and it doesn't really make the code easier
to read.
2024-01-29 21:07:43 +00:00
Sergej Jaskiewicz 93a1ceae75 [klib] Deduplicate KLIB serialization logic in K/JS and K/N
Introduce the new `serializeModuleIntoKlib` function that all KLIB-based
backends can use to serialize a module to a KLIB.

^KT-64392 Fixed
2024-01-29 21:07:43 +00:00
Sergej Jaskiewicz c335cd0b1e [IR] Remove unused IrMessageLogger from serialization logic 2024-01-29 21:07:43 +00:00
Dmitry Savvinov 77ebe1085d [klib tool] Return unbound symbols for dump-ir mode
This is more convenient behaviour for debugging with klib-tool than
just failing.

Note that enabling Partial Linkage globally in klib-tool is undesirable,
as it can auto-tweak IR (e.g. when overrides do not match), thus
distorting the rendered IR

^KT-61143 Fixed
2024-01-22 17:11:56 +00:00
Sergej Jaskiewicz c8f8316e9b [klib] Return the block's return value in inFile methods 2024-01-15 13:13:55 +00:00
Sergej Jaskiewicz eda30ff704 [klib] Implement diagnostics for clashing KLIB signatures
Now, we detect clashing signatures during serialization to KLIB and
report a compiler error if two or more declarations have the same
`IdSignature`

For example, for the following code:
```kotlin
@Deprecated("", level = DeprecationLevel.HIDDEN)
fun foo(): String = ""

fun foo(): Int = 0
```

the compiler will produce this diagnostic:
```
e: main.kt:1:1 Platform declaration clash: The following declarations
       have the same KLIB signature (/foo|foo(){}[0]):
    fun foo(): String defined in root package
    fun foo(): Int defined in root package
e: main.kt:4:1 Platform declaration clash: The following declarations
       have the same KLIB signature (/foo|foo(){}[0]):
    fun foo(): String defined in root package
    fun foo(): Int defined in root package
```

Note that we report this diagnostic during serialization and not earlier
(e.g., in fir2ir) for more robustness, so ensure that we check
exactly the signatures that will be written to a KLIB.
If we later introduce some annotation for customizing a declaration's
signature (e.g., for preserving binary compatibility), this
diagnostic will continue to work as expected.

^KT-63670 Fixed
2024-01-12 15:59:28 +00:00
Vladimir Sukharev 08bc0a6bd0 [K/N] Adjust moved special backend checks tests
^KT-61564
2024-01-09 14:17:47 +00:00
Dmitriy Dolovov e92017f64e [KLIB Resolver] Deprecate Logger.fatal()
Invocation of Logger.fatal() may cause severe side effects such as
throwing an exception or even terminating the current JVM process
(check various implementations of this function for details).

The code that uses Logger.fatal() sometimes expects a particular kind
of side effect. This is totally a design flaw. And it's definitely not
a responsibility of Logger to influence the execution flow of
the program.
2024-01-08 13:26:24 +00:00
Dmitriy Dolovov 194efac68c [Manglers] Uniform handling of the error type in signature mangled names in all manglers
^KT-63723
2023-12-18 18:08:56 +00:00
Dmitriy Dolovov 46081f968d [KLIB Resolver] Report KLIB resolver issues as compiler messages
The reason of this change is to make messages (especially warnings)
that are reported by the KLIB resolver become visible to the end user.
This can be achieved to forwarding such messages to the appropriate
compiler's components such as
`org.jetbrains.kotlin.cli.common.messages.MessageCollector` and
`org.jetbrains.kotlin.ir.util.IrMessageLogger`.

Also: The default `DummyLogger` should be used as minimal as possible.
Because it just forwards messages to the standard output (console)
where they can remain unattended. When the compiler is executed
from the Gradle plugin such messages appear only in DEBUG Gradle's log.

^KT-63573
2023-12-18 13:03:40 +00:00
Dmitriy Dolovov c9f4a1a841 IR: Avoid overwriting of property signature descriptions
^KT-64085
^KT-64082
2023-12-07 15:02:24 +00:00
Igor Chevdar 1805f322f8 [K/N] Do not use LazyIr for per-file caches
#KT-63789 Fixed
2023-11-28 09:22:36 +00:00
Alexander Udalov 8379e48e33 IR: remove IrSimpleTypeBuilder.variance
IrCapturedType (which is a IrSimpleType) does not have variance, so it
is confusing to have it in IrSimpleTypeBuilder, and it's clearer to
construct type projection separately anyway.
2023-11-21 20:02:12 +00:00
Alexander Udalov 898d3aeea9 IR: minor refactorings around IrType/IrCapturedType
- use identity in equals/hashCode. It was already effectively working
  like this because of `constructor === other.constructor`
- implement variance and declare it in IrSimpleType
- remove obsolete TODO
- simplify code in captureFromArguments
- use data objects
2023-11-21 20:02:12 +00:00
Iaroslav Postovalov a3b55cf758 [IR] Drastically simplify the hierarchy of IR origins
IrStatementOriginImpl and IrDeclarationOriginImpl were made final
classes to simplify the creation of them (a delegate provider was
added) and to optimize performance when comparing the origins by type
and name
2023-11-13 17:56:09 +00:00
Sergej Jaskiewicz 47f8dedfa5 [IR] Remove methods for computing full mangled names from non-IR manglers
A full mangled name of a declaration is a mangled name that includes
the mangled names of all the declaration's parents.

We don't use full mangled names for building `IdSignature`s. We use them
in two places:
- Generating stable names for JavaScript functions in Kotlin/JS
  (see `NameTables.kt`)
- Generating names for LLVM symbols from lowered IR functions in
  Kotlin/Native (see `BinaryInterface.kt`)

In both of these places we run the IR mangler, hence we don't need this
functionality in other manglers.
2023-11-10 12:31:40 +00:00
Dmitriy Dolovov f276fe6506 [KLIB] Add a reliable mechanism to check if KLIB has IR inside
^KT-62341
2023-10-27 09:27:59 +00:00
Dmitriy Novozhilov fb8bf19091 [IR] Rename IrSymbolInternals to UnsafeDuringIrConstructionAPI
The new name more precisely describes the meaning of this opt-int
2023-10-25 11:32:46 +00:00
Johan Bay fb1d2278a0 [klibs] header klibs should keep private interfaces
^KT-62213 Fixed
2023-10-23 11:48:05 +00:00
Pavel Kunyavskiy ad5831acc6 [IR] Add a method to get ModuleDescriptor from IrPackageFragment
Ideally, instead of this method, there should be a link
to IrModuleFragment. Unfortunately, it would require to big refactoring,
as some of IrPackageFragment implementations doesn't have any
IrModuleFragment inside, and are not located inside any
IrModuleFragment.

So for now, we just implement and use everywhere a single way of
getting the module descriptor, which respects a IrModuleFragment
link if it exists, and fallbacks to descriptor-based method
if it doesn't.

^KT-62623
2023-10-19 10:16:44 +00:00
Alexander Udalov c33c918bd4 IR: add IrClass.hasEnumEntries
This flag will be used on JVM to determine whether or not to generate
external enum entries mappings ("$EntriesMappings") classes. Note that
from the frontend's point of view, every enum has `entries`, so for
backend purposes we have to reach out to the underlying deserialized
data to read the flag from the metadata.
2023-10-16 20:22:20 +00:00
Ilya Kirillov 4d38cc4548 [Analysis API Standalone] fix exception on invalid KLib from symbol provider
^KT-62244 fixed
2023-10-12 15:15:05 +00:00
Nikita Bobko 91a337074e Refactoring: Improve createExpectActualTypeParameterSubstitutor API
Review: https://jetbrains.team/p/kt/reviews/12279/files

Motivation: make sure that cases like KT-62027 won't happen again
Review: https://jetbrains.team/p/kt/reviews/12279/files

Now it's responsibility of the
`createExpectActualTypeParameterSubstitutor` calller to think about the
case when parameters size isn't equal. You must not be able to create a
substitutor if type parameters sizes are not equal

Improvement in `createExpectActualTypeParameterSubstitutor` API also
improves
`AbstractExpectActualCompatibilityChecker.getCallablesCompatibility`
API.

Because suppose that you accidentally created a redundant wrapping
substitutor => you need to handle the case of not equal type parameters
size on the call site => you start thinking why you should do that on
the call site? It must be a responsibility of
`getCallablesCompatibility` => you realize that you created a redundant
wrapping substitutor
2023-10-10 09:02:07 +00:00
Alexander Udalov 41f736a665 IR: remove dependency of IrTypeSubstitutor on IrBuiltIns
It was only used in one place incorrectly anyway.
2023-10-03 10:18:54 +00:00
Dmitriy Dolovov e8853fd40b [IR] Drop obsolete o.j.k.backend.common.serialization.signature.IdSignatureSerializer
It has been renamed to IdSignatureFactory. We have to keep
IdSignatureSerializer as a typealias for a while to keep the source
compatibility with the Compose compiler plugin.
2023-10-02 22:37:54 +00:00
Pavel Kunyavskiy 03914e353f [IR] Rename classes after refactoring
^KT-61934
2023-09-24 16:11:49 +00:00
Pavel Kunyavskiy 9fd057d7c3 [IR] Simplify usage of IrOverrideUtil
It had some copy-pasted code for counting current file on each use-site.
Now it's encapsulated in strategy.

KT-61934
2023-09-24 16:11:49 +00:00
Pavel Kunyavskiy 2b8b6a2b21 [IR] Split FakeOverrideBuilder to two parts
Now there is separate class encapsulating logic about how to build
fakeOverrides and one encapsulating logic of which classes
do need building fake overrides.

This also allows to untie strange inheritance dependencies.

^KT-61934
2023-09-24 16:11:49 +00:00
Pavel Kunyavskiy fcca73e900 [IR] Simplify IrOverridingUtil
Previously, it had some internal state that was saved between calls
and needed to be cleared.

This state was eliminated, which makes invariants clearer and using
easier.

^KT-61934
2023-09-24 11:58:16 +02:00
Johan Bay 64e601bea0 Extract IdSignature serializer from IrFileSerializer
^KT-61642
2023-09-22 22:30:25 +00:00
Johan Bay a041e51ff2 Rename IdSignatureSerializer to IdSignatureFactory
^KT-61642
2023-09-22 22:30:24 +00:00
Dmitriy Dolovov a2e148173d [JS] Don't produce metadata-only (IR-less) KLIBs in JS
The scenario when JS source code is shared between a few JS common
source sets is not supported in Kotlin Multiplatform.

^KT-61136
2023-09-18 17:58:43 +00:00
Alexander Udalov fda47c45ec IR: do not change overrides for lazy IR in FakeOverrideRebuilder
`FakeOverrideBuilder.provideFakeOverrides` recursively changes overrides
for all superclasses in the hierarchy, including lazy IR, which is a lot
of extra work.

Also it leads to some tests failing in the IR fake override builder mode
because it changes correct fake overrides of Java classes to incorrect
ones. Those tests are unmuted but it doesn't mean they are fixed -- most
likely we'll generate fake overrides via IR for lazy IR too, at which
point they'll start to fail again.
2023-09-15 12:55:33 +00:00
Sergej Jaskiewicz 9eba17d631 [K/N] Move Native-specific mangle constants to more suitable places
They shouldn't be mixed with target-independent constants.
2023-09-13 14:25:12 +00:00
Sergej Jaskiewicz e3b5dc509c [K/N] De-duplicate code for mangling objc interop function names
Extract the duplicated functionality to the
`ObjCFunctionNameMangleComputer` class.
2023-09-13 14:25:11 +00:00
Sergej Jaskiewicz 0fd4f10f6c [K/N] Avoid magic strings in KonanMangler 2023-09-13 14:25:10 +00:00