forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getter generation for $stableprop #1376
Open
shishkin-pavel
wants to merge
1
commit into
shish/compiler/1.5.13
Choose a base branch
from
shish/compiler/dev/1.5.13
base: shish/compiler/1.5.13
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,22 +32,24 @@ import androidx.compose.compiler.plugins.kotlin.analysis.knownUnstable | |
import androidx.compose.compiler.plugins.kotlin.irTrace | ||
import com.intellij.openapi.progress.ProcessCanceledException | ||
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext | ||
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder | ||
import org.jetbrains.kotlin.backend.jvm.ir.isInlineClassType | ||
import org.jetbrains.kotlin.builtins.PrimitiveType | ||
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities | ||
import org.jetbrains.kotlin.ir.IrStatement | ||
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI | ||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET | ||
import org.jetbrains.kotlin.ir.builders.declarations.addGetter | ||
import org.jetbrains.kotlin.ir.builders.declarations.addTypeParameter | ||
import org.jetbrains.kotlin.ir.builders.declarations.addValueParameter | ||
import org.jetbrains.kotlin.ir.builders.declarations.buildField | ||
import org.jetbrains.kotlin.ir.builders.declarations.buildFun | ||
import org.jetbrains.kotlin.ir.builders.declarations.buildProperty | ||
import org.jetbrains.kotlin.ir.builders.irBlockBody | ||
import org.jetbrains.kotlin.ir.builders.irReturn | ||
import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer | ||
import org.jetbrains.kotlin.ir.declarations.IrAttributeContainer | ||
import org.jetbrains.kotlin.ir.declarations.IrClass | ||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationContainer | ||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin | ||
import org.jetbrains.kotlin.ir.declarations.IrField | ||
import org.jetbrains.kotlin.ir.declarations.IrFile | ||
|
@@ -377,15 +379,7 @@ abstract class AbstractComposeLowering( | |
null | ||
|
||
is Stability.Parameter -> resolve(parameter) | ||
is Stability.Runtime -> { | ||
val stableField = declaration.makeStabilityField() | ||
IrGetFieldImpl( | ||
UNDEFINED_OFFSET, | ||
UNDEFINED_OFFSET, | ||
stableField.symbol, | ||
stableField.type | ||
) | ||
} | ||
is Stability.Runtime -> declaration.runtimeStabilityValue() | ||
|
||
is Stability.Unknown -> null | ||
} | ||
|
@@ -904,63 +898,86 @@ abstract class AbstractComposeLowering( | |
} | ||
} | ||
|
||
private fun IrClass.makeStabilityFieldJvm(): IrField { | ||
private fun IrField.stabilityFieldValue(): IrGetField = | ||
IrGetFieldImpl( | ||
UNDEFINED_OFFSET, | ||
UNDEFINED_OFFSET, | ||
this.symbol, | ||
this.type | ||
) | ||
|
||
private fun IrClass.runtimeStabilityValue(): IrExpression { | ||
return if (context.platform.isJvm()) { | ||
val stableField = this.makeStabilityFieldJvm() | ||
stableField.stabilityFieldValue() | ||
} else { | ||
val stableProp = this.makeStabilityProp() | ||
irCall(stableProp.getter!!, SYNTHETIC_OFFSET, SYNTHETIC_OFFSET) | ||
} | ||
} | ||
|
||
private fun makeStabilityField(fieldName: Name): IrField { | ||
return context.irFactory.buildField { | ||
startOffset = SYNTHETIC_OFFSET | ||
endOffset = SYNTHETIC_OFFSET | ||
name = KtxNameConventions.STABILITY_FLAG | ||
name = fieldName | ||
isStatic = true | ||
isFinal = true | ||
type = context.irBuiltIns.intType | ||
visibility = DescriptorVisibilities.PUBLIC | ||
}.also { stabilityField -> | ||
} | ||
} | ||
|
||
private fun IrClass.makeStabilityFieldJvm(): IrField { | ||
return makeStabilityField(KtxNameConventions.STABILITY_FLAG).also { stabilityField -> | ||
stabilityField.parent = this@makeStabilityFieldJvm | ||
} | ||
} | ||
|
||
private fun IrClass.makeStabilityFieldNonJvm(): IrField { | ||
val stabilityFieldName = this.uniqueStabilityFieldName() | ||
private fun IrClass.makeStabilityProp(): IrProperty { | ||
val fieldParent = this.getPackageFragment() | ||
|
||
return context.irFactory.buildField { | ||
startOffset = SYNTHETIC_OFFSET | ||
endOffset = SYNTHETIC_OFFSET | ||
name = stabilityFieldName | ||
isStatic = true | ||
isFinal = true | ||
type = context.irBuiltIns.intType | ||
visibility = DescriptorVisibilities.PUBLIC | ||
}.let { stabilityField -> | ||
stabilityField.parent = fieldParent | ||
makeStabilityProp(stabilityField, fieldParent) | ||
}.backingField!! | ||
} | ||
|
||
private fun IrClass.makeStabilityProp( | ||
stabilityField: IrField, | ||
fieldParent: IrDeclarationContainer | ||
): IrProperty { | ||
val propName = [email protected]() | ||
val existingProp = fieldParent.declarations.firstOrNull { | ||
it is IrProperty && it.name == propName | ||
} as? IrProperty | ||
if (existingProp != null) { | ||
return existingProp | ||
} | ||
|
||
val stabilityField = makeStabilityField(this.uniqueStabilityFieldName()).also { | ||
it.parent = fieldParent | ||
} | ||
return context.irFactory.buildProperty { | ||
startOffset = SYNTHETIC_OFFSET | ||
endOffset = SYNTHETIC_OFFSET | ||
name = propName | ||
visibility = DescriptorVisibilities.PUBLIC | ||
isConst = true | ||
}.also { property -> | ||
property.parent = fieldParent | ||
stabilityField.correspondingPropertySymbol = property.symbol | ||
property.backingField = stabilityField | ||
fieldParent.addChild(property) | ||
|
||
property.addGetter { | ||
startOffset = SYNTHETIC_OFFSET | ||
endOffset = SYNTHETIC_OFFSET | ||
returnType = stabilityField.type | ||
visibility = DescriptorVisibilities.PUBLIC | ||
origin = IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR | ||
}.also { fn -> | ||
fn.parent = fieldParent | ||
fn.body = DeclarationIrBuilder(context, fn.symbol).irBlockBody { | ||
+irReturn(stabilityField.stabilityFieldValue()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun IrClass.makeStabilityFieldNonJvm(): IrField { | ||
return makeStabilityProp().backingField!! | ||
} | ||
|
||
fun IrExpression.isStatic(): Boolean { | ||
return when (this) { | ||
// A constant by definition is static | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wdyt, if we keep this line only for 1.9.2x versions, but get rid of it for 2.0.x?
I guess it might help preserve the compatibility for more cases.