raulraja
06/13/2019, 11:50 PMSyntheticResolverExtension
I’m overriding generateSyntheticClasses(PackageFragmentDescriptor, ...)
and addSyntheticSupertypes
. I’m attempting to add a super type to a class that I generate myself in that pass.
For example:
package x
interface Foo<A>
class Bar
I want to expand to:
package x
interface Foo<A>
class GeneratedClass
class Bar : Foo<GeneratedClass>
The issue I’m having is that addSyntheticSupertypes
happens before GeneratedClass
has a valid ClasDescriptor
so when I add the supertype I get:
Bar : Foo<[ERROR : Unsubstituted type for <ERROR CLASS>]>
e: java.lang.IllegalStateException: Backend Internal error: Exception during code generation
Cause: Error type encountered: [ERROR : Unsubstituted type for <ERROR CLASS>] (ErrorType).
How can I generate a valid descriptor in that phase for GeneratedClass
or how can I add a supertype to a class given the class the type refers to may not have been generated yet?
Is this the wrong phase to do this kind of work?dsavvinov
06/14/2019, 9:53 AMGeneratedClass
?raulraja
06/14/2019, 9:59 AMgenerateSyntheticClasses
overload that takes first the PackageFragmentDescriptor
with something like:
fun PackageFragmentDescriptor.kindMarker(ctx: LazyClassContext, target: ClassDescriptor): ClassDescriptor =
ClassDescriptorImpl(
containingDeclaration,
target.markerName.shortName(),
Modality.FINAL,
ClassKind.CLASS,
emptyList<KotlinType>(),
SourceElement.NO_SOURCE,
false,
ctx.storageManager
)
raulraja
06/14/2019, 10:00 AMresult: MutableSet<ClassDescriptor>
which is passed to me in generateSyntheticClasses
dsavvinov
06/14/2019, 10:06 AMaddSyntheticSupertypes
? Is it possible to see the actual code?raulraja
06/14/2019, 1:36 PMsomeOtherClassDescriptor.module.resolveClassByFqName(GeneratedClassName, NoLookupLocation.FROM_BACKEND)
raulraja
06/14/2019, 2:09 PMdsavvinov
06/14/2019, 2:53 PMpackageFragmentDescriptor
of the module which you later query in addSyntheticSupertypes
(but most probably you’ve checked that already)raulraja
06/17/2019, 8:32 AMaddSynthetiClasses
which is where one can access the packageFragmentDescriptor
is invoked after addSyntheticSupertypes
. I could create a valid ClassDescriptor
inside addSyntheticSupertypes
if I get access to a StorageManager
. For what I can see the storage manager which is a requirement in ClassDescriptorImpl
is not available because is private in the KotlinBuiltIns and other objects that reference it. Can I access the StorageManager as global service and request it wherever needed?raulraja
06/17/2019, 1:24 PMClassDescriptorImpl(
descriptor.parents.first(),
Name("GeneratedClass"),
Modality.FINAL,
ClassKind.CLASS,
emptyList<KotlinType>(),
SourceElement.NO_SOURCE,
false,
LockBasedStorageManager.NO_LOCKS
) {
override fun getUnsubstitutedMemberScope(): MemberScope = MemberScope.Empty
override fun getConstructors(): Collection<ClassConstructorDescriptor> = emptyList()
}
I had to override those two methods so it would not blow up. The issue I see now is that GeneratedClass
is in the metadata properly in the bytecode but no actual class is generated since Class.forName("sample.GeneratedClass")
blows up.
I suppose I’m missing the IR generation step.
Is there an example somewhere that shows how to generate a marker simple class with no methods in the IR backend?raulraja
06/17/2019, 8:33 PMgenerateSyntheticClasses(PackageFramentProvider, ....)
, addSyntheticSupertypes
and doing all the IR generation for the class with transformDeclarationsFlat. Do I also need to add the class to the package declaration somehow?kralli
06/18/2019, 6:30 AMGeneratedClass
in the PackageFragmentProvider
of the sample package via the PackageFragmentProviderExtension
? Thats what I am doing and it worked out quite well for me. Though I'm not sure whether it's available in IR.raulraja
06/18/2019, 6:43 AMraulraja
06/18/2019, 6:43 AM