Skip to content

Commit

Permalink
[IR] Introduce new IdSignatures
Browse files Browse the repository at this point in the history
FileSignature, CompositeSignature, LocalSignature

They are needed to make possible reference any non-local declaration via
 signature, including private signature, type parameters and so on.

- Support those new signatures in proto and klibs
- Rename `isPublic` -> `isPubliclyVisible` due to changed semantic
- Fix FIR
- clean up code
  • Loading branch information
romanart authored and TeamCityServer committed Jul 1, 2021
1 parent 7139785 commit 6cdac22
Show file tree
Hide file tree
Showing 58 changed files with 3,217 additions and 1,276 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ open class IncrementalJsCache(
}

for ((srcFile, irData) in incrementalResults.irFileData) {
val (fileData, types, signatures, strings, declarations, bodies, fqn) = irData
irTranslationResults.put(srcFile, fileData, types, signatures, strings, declarations, bodies, fqn)
val (fileData, types, signatures, strings, declarations, bodies, fqn, debugInfos) = irData
irTranslationResults.put(srcFile, fileData, types, signatures, strings, declarations, bodies, fqn, debugInfos)
}
}

Expand Down Expand Up @@ -269,6 +269,7 @@ private object IrTranslationResultValueExternalizer : DataExternalizer<IrTransla
output.writeArray(value.declarations)
output.writeArray(value.bodies)
output.writeArray(value.fqn)
value.debugInfo?.let { output.writeArray(it) }
}

private fun DataOutput.writeArray(array: ByteArray) {
Expand All @@ -283,6 +284,17 @@ private object IrTranslationResultValueExternalizer : DataExternalizer<IrTransla
return filedata
}

private fun DataInput.readArrayOrNull(): ByteArray? {
try {
val dataSize = readInt()
val filedata = ByteArray(dataSize)
readFully(filedata)
return filedata
} catch (e: Throwable) {
return null
}
}

override fun read(input: DataInput): IrTranslationResultValue {
val fileData = input.readArray()
val types = input.readArray()
Expand All @@ -291,8 +303,9 @@ private object IrTranslationResultValueExternalizer : DataExternalizer<IrTransla
val declarations = input.readArray()
val bodies = input.readArray()
val fqn = input.readArray()
val debugInfos = input.readArrayOrNull()

return IrTranslationResultValue(fileData, types, signatures, strings, declarations, bodies, fqn)
return IrTranslationResultValue(fileData, types, signatures, strings, declarations, bodies, fqn, debugInfos)
}
}

Expand All @@ -317,10 +330,11 @@ private class IrTranslationResultMap(
newStrings: ByteArray,
newDeclarations: ByteArray,
newBodies: ByteArray,
fqn: ByteArray
fqn: ByteArray,
debugInfos: ByteArray?
) {
storage[pathConverter.toPath(sourceFile)] =
IrTranslationResultValue(newFiledata, newTypes, newSignatures, newStrings, newDeclarations, newBodies, fqn)
IrTranslationResultValue(newFiledata, newTypes, newSignatures, newStrings, newDeclarations, newBodies, fqn, debugInfos)
}

operator fun get(sourceFile: File): IrTranslationResultValue? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,17 +669,14 @@ class Fir2IrDeclarationStorage(
internal fun IrProperty.createBackingField(
property: FirProperty,
origin: IrDeclarationOrigin,
descriptor: PropertyDescriptor,
visibility: DescriptorVisibility,
name: Name,
isFinal: Boolean,
firInitializerExpression: FirExpression?,
type: IrType? = null
): IrField = convertCatching(property) {
val inferredType = type ?: firInitializerExpression!!.typeRef.toIrType()
return symbolTable.declareField(
startOffset, endOffset, origin, descriptor, inferredType
) { symbol ->
return declareIrField(null) { symbol ->
irFactory.createField(
startOffset, endOffset, origin, symbol,
name, inferredType,
Expand Down Expand Up @@ -713,6 +710,12 @@ class Fir2IrDeclarationStorage(
else
symbolTable.declareProperty(signature, { Fir2IrPropertySymbol(signature, containerSource) }, factory)

private fun declareIrField(signature: IdSignature?, factory: (IrFieldSymbol) -> IrField): IrField =
if (signature == null)
factory(IrFieldSymbolImpl())
else
symbolTable.declareField(signature, { IrFieldPublicSymbolImpl(signature) }, factory)

fun getOrCreateIrProperty(
property: FirProperty,
irParent: IrDeclarationParent?,
Expand Down Expand Up @@ -797,13 +800,13 @@ class Fir2IrDeclarationStorage(
if (delegate != null || property.hasBackingField) {
backingField = if (delegate != null) {
createBackingField(
property, IrDeclarationOrigin.PROPERTY_DELEGATE, descriptor,
property, IrDeclarationOrigin.PROPERTY_DELEGATE,
components.visibilityConverter.convertToDescriptorVisibility(property.fieldVisibility),
Name.identifier("${property.name}\$delegate"), true, delegate
)
} else {
createBackingField(
property, IrDeclarationOrigin.PROPERTY_BACKING_FIELD, descriptor,
property, IrDeclarationOrigin.PROPERTY_BACKING_FIELD,
components.visibilityConverter.convertToDescriptorVisibility(property.fieldVisibility),
property.name, property.isVal, initializer, type
).also { field ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@

package org.jetbrains.kotlin.fir.lazy

import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.fir.backend.ConversionTypeContext
import org.jetbrains.kotlin.fir.backend.ConversionTypeOrigin
import org.jetbrains.kotlin.fir.backend.Fir2IrComponents
import org.jetbrains.kotlin.fir.backend.toIrConst
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyGetter
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertySetter
import org.jetbrains.kotlin.fir.expressions.FirConstExpression
import org.jetbrains.kotlin.fir.symbols.Fir2IrPropertySymbol
import org.jetbrains.kotlin.fir.symbols.Fir2IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.lazy.lazyVar
Expand All @@ -21,11 +27,8 @@ import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.isInterface
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.fir.backend.*
import org.jetbrains.kotlin.fir.backend.ConversionTypeOrigin
import org.jetbrains.kotlin.fir.declarations.utils.*
import org.jetbrains.kotlin.fir.symbols.Fir2IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol

class Fir2IrLazyProperty(
Expand Down Expand Up @@ -96,7 +99,7 @@ class Fir2IrLazyProperty(
fir.initializer != null || fir.getter is FirDefaultPropertyGetter || fir.isVar && fir.setter is FirDefaultPropertySetter -> {
with(declarationStorage) {
createBackingField(
fir, IrDeclarationOrigin.PROPERTY_BACKING_FIELD, descriptor,
fir, IrDeclarationOrigin.PROPERTY_BACKING_FIELD,
components.visibilityConverter.convertToDescriptorVisibility(fir.visibility), fir.name, fir.isVal, fir.initializer,
type
).also { field ->
Expand All @@ -112,7 +115,7 @@ class Fir2IrLazyProperty(
fir.delegate != null -> {
with(declarationStorage) {
createBackingField(
fir, IrDeclarationOrigin.PROPERTY_DELEGATE, descriptor,
fir, IrDeclarationOrigin.PROPERTY_DELEGATE,
components.visibilityConverter.convertToDescriptorVisibility(fir.visibility),
Name.identifier("${fir.name}\$delegate"), true, fir.delegate
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class JvmBackendContext(
val state: GenerationState,
override val irBuiltIns: IrBuiltIns,
irModuleFragment: IrModuleFragment,
private val symbolTable: SymbolTable,
val symbolTable: SymbolTable,
val phaseConfig: PhaseConfig,
val generatorExtensions: JvmGeneratorExtensions,
val backendExtension: JvmBackendExtension,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@ private fun codegenPhase(generateMultifileFacade: Boolean): NamedCompilerPhase<J
object : FileLoweringPass {
override fun lower(irFile: IrFile) {
val isMultifileFacade = irFile.fileEntry is MultifileFacadeFileEntry
if (isMultifileFacade != generateMultifileFacade) return

for (loweredClass in irFile.declarations) {
if (loweredClass !is IrClass) {
throw AssertionError("File-level declaration should be IrClass after JvmLower, got: " + loweredClass.render())
if (isMultifileFacade == generateMultifileFacade) {
for (loweredClass in irFile.declarations) {
if (loweredClass !is IrClass) {
throw AssertionError("File-level declaration should be IrClass after JvmLower, got: " + loweredClass.render())
}
ClassCodegen.getOrCreate(loweredClass, context).generate()
}
ClassCodegen.getOrCreate(loweredClass, context).generate()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class GeneratorContext(
val languageVersionSettings: LanguageVersionSettings,
val symbolTable: SymbolTable,
val extensions: GeneratorExtensions,
val typeTranslator: TypeTranslator,
val typeTranslator: TypeTranslatorImpl,
val constantValueGenerator: ConstantValueGenerator,
override val irBuiltIns: IrBuiltIns
) : IrGeneratorContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ import org.jetbrains.kotlin.ir.declarations.DescriptorMetadataSource
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl
import org.jetbrains.kotlin.ir.linkage.IrDeserializer
import org.jetbrains.kotlin.ir.linkage.IrProvider
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.ir.util.StubGeneratorExtensions
import org.jetbrains.kotlin.ir.util.patchDeclarationParents
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.transformations.insertImplicitCasts
Expand All @@ -47,26 +44,14 @@ class ModuleGenerator(
IrModuleFragmentImpl(context.moduleDescriptor, context.irBuiltIns).also { irModule ->
val irDeclarationGenerator = DeclarationGenerator(context)
ktFiles.toSet().mapTo(irModule.files) { ktFile ->
generateSingleFile(irDeclarationGenerator, ktFile, irModule)
context.typeTranslator.inFile(ktFile) {
generateSingleFile(irDeclarationGenerator, ktFile, irModule)
}
}
}

fun generateUnboundSymbolsAsDependencies(
irModule: IrModuleFragment,
deserializer: IrDeserializer? = null,
extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY
) {
val fullIrProvidersList = generateTypicalIrProviderList(
irModule.descriptor, context.irBuiltIns, context.symbolTable, deserializer,
extensions
)
ExternalDependenciesGenerator(context.symbolTable, fullIrProvidersList)
.generateUnboundSymbolsAsDependencies()
}

fun generateUnboundSymbolsAsDependencies(irProviders: List<IrProvider>) {
ExternalDependenciesGenerator(context.symbolTable, irProviders)
.generateUnboundSymbolsAsDependencies()
ExternalDependenciesGenerator(context.symbolTable, irProviders).generateUnboundSymbolsAsDependencies()
}

private fun generateSingleFile(irDeclarationGenerator: DeclarationGenerator, ktFile: KtFile, module: IrModuleFragment): IrFileImpl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
package org.jetbrains.kotlin.psi2ir.generators

import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.types.*

class TypeTranslatorImpl(
Expand Down Expand Up @@ -38,4 +42,23 @@ class TypeTranslatorImpl(

override fun commonSupertype(types: Collection<KotlinType>): KotlinType =
CommonSupertypes.commonSupertype(types)

override fun isTypeAliasAccessibleHere(typeAliasDescriptor: TypeAliasDescriptor): Boolean {
if (typeAliasDescriptor.visibility != DescriptorVisibilities.PRIVATE) return true

val psiFile = typeAliasDescriptor.source.getPsi()?.containingFile ?: return false

return psiFile == currentFile
}

private var currentFile: KtFile? = null

fun <R> inFile(ktFile: KtFile?, block: () -> R): R {
try {
currentFile = ktFile
return block()
} finally {
currentFile = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOriginImpl
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl
Expand Down Expand Up @@ -48,8 +47,7 @@ class IrBuiltIns(
private val builtInsModule = builtIns.builtInsModule

private val packageFragmentDescriptor = IrBuiltinsPackageFragmentDescriptorImpl(builtInsModule, KOTLIN_INTERNAL_IR_FQN)
val packageFragment =
IrExternalPackageFragmentImpl(symbolTable.referenceExternalPackageFragment(packageFragmentDescriptor), KOTLIN_INTERNAL_IR_FQN)
val packageFragment = symbolTable.declareExternalPackageFragmentIfNotExists(packageFragmentDescriptor)

private fun ClassDescriptor.toIrSymbol() = symbolTable.referenceClass(this)
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ abstract class IrDelegateDescriptorBase(
override fun isVar(): Boolean = false

override fun <R, D> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D): R =
visitor.visitVariableDescriptor(this, data)
visitor.visitPropertyDescriptor(this, data)
}

class IrPropertyDelegateDescriptorImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,18 @@ class IrFunctionFactory(private val irBuiltIns: IrBuiltIns, private val symbolTa

private val kotlinPackageFragment: IrPackageFragment by lazy {
irBuiltIns.builtIns.getFunction(0).let {
symbolTable.declareExternalPackageFragment(it.containingDeclaration as PackageFragmentDescriptor)
symbolTable.declareExternalPackageFragmentIfNotExists(it.containingDeclaration as PackageFragmentDescriptor)
}
}
private val kotlinCoroutinesPackageFragment: IrPackageFragment by lazy {
irBuiltIns.builtIns.getSuspendFunction(0).let {
symbolTable.declareExternalPackageFragment(it.containingDeclaration as PackageFragmentDescriptor)
symbolTable.declareExternalPackageFragmentIfNotExists(it.containingDeclaration as PackageFragmentDescriptor)
}
}

private val kotlinReflectPackageFragment: IrPackageFragment by lazy {
irBuiltIns.kPropertyClass.descriptor.let {
symbolTable.declareExternalPackageFragment(it.containingDeclaration as PackageFragmentDescriptor)
symbolTable.declareExternalPackageFragmentIfNotExists(it.containingDeclaration as PackageFragmentDescriptor)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class IrBindablePublicSymbolBase<out D : DeclarationDescriptor, B : IrS
assert(descriptor == null || isOriginalDescriptor(descriptor)) {
"Substituted descriptor $descriptor for ${descriptor!!.original}"
}
assert(sig.isPublic)
assert(sig.isPubliclyVisible)
}

private fun isOriginalDescriptor(descriptor: DeclarationDescriptor): Boolean =
Expand Down Expand Up @@ -91,6 +91,6 @@ class IrFieldPublicSymbolImpl(sig: IdSignature, descriptor: PropertyDescriptor?
IrBindablePublicSymbolBase<PropertyDescriptor, IrField>(sig, descriptor),
IrFieldSymbol

class IrTypeParameterPublicSymbolImpl(sig: IdSignature, descriptor: TypeParameterDescriptor? = null):
class IrTypeParameterPublicSymbolImpl(sig: IdSignature, descriptor: TypeParameterDescriptor? = null) :
IrBindablePublicSymbolBase<TypeParameterDescriptor, IrTypeParameter>(sig, descriptor),
IrTypeParameterSymbol
IrTypeParameterSymbol
Loading

0 comments on commit 6cdac22

Please sign in to comment.