Search in sources :

Example 21 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class InlineCodegenUtil method createSpecialEnumMethodBody.

public static MethodNode createSpecialEnumMethodBody(@NotNull ExpressionCodegen codegen, @NotNull String name, @NotNull KotlinType type, @NotNull KotlinTypeMapper typeMapper) {
    boolean isValueOf = "enumValueOf".equals(name);
    Type invokeType = typeMapper.mapType(type);
    String desc = getSpecialEnumFunDescriptor(invokeType, isValueOf);
    MethodNode node = new MethodNode(API, Opcodes.ACC_STATIC, "fake", desc, null, null);
    codegen.putReifiedOperationMarkerIfTypeIsReifiedParameter(type, ReifiedTypeInliner.OperationKind.ENUM_REIFIED, new InstructionAdapter(node));
    if (isValueOf) {
        node.visitInsn(Opcodes.ACONST_NULL);
        node.visitVarInsn(Opcodes.ALOAD, 0);
        node.visitMethodInsn(Opcodes.INVOKESTATIC, ENUM_TYPE.getInternalName(), "valueOf", Type.getMethodDescriptor(ENUM_TYPE, JAVA_CLASS_TYPE, AsmTypes.JAVA_STRING_TYPE), false);
    } else {
        node.visitInsn(Opcodes.ICONST_0);
        node.visitTypeInsn(Opcodes.ANEWARRAY, ENUM_TYPE.getInternalName());
    }
    node.visitInsn(Opcodes.ARETURN);
    node.visitMaxs(isValueOf ? 3 : 2, isValueOf ? 1 : 0);
    return node;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)

Example 22 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class CodegenAnnotatingVisitor method recordLocalVariablePropertyMetadata.

private void recordLocalVariablePropertyMetadata(LocalVariableDescriptor variableDescriptor) {
    KotlinType delegateType = JvmCodegenUtil.getPropertyDelegateType(variableDescriptor, bindingContext);
    if (delegateType == null)
        return;
    LocalVariableDescriptor delegateVariableDescriptor = new LocalVariableDescriptor(variableDescriptor.getContainingDeclaration(), Annotations.Companion.getEMPTY(), variableDescriptor.getName(), delegateType, false, false, SourceElement.NO_SOURCE);
    bindingTrace.record(LOCAL_VARIABLE_DELEGATE, variableDescriptor, delegateVariableDescriptor);
    LocalVariableDescriptor metadataVariableDescriptor = new LocalVariableDescriptor(variableDescriptor.getContainingDeclaration(), Annotations.Companion.getEMPTY(), Name.identifier(variableDescriptor.getName().asString() + "$metadata"), ReflectionTypes.Companion.createKPropertyStarType(DescriptorUtilsKt.getModule(variableDescriptor)), false, false, SourceElement.NO_SOURCE);
    bindingTrace.record(LOCAL_VARIABLE_PROPERTY_METADATA, variableDescriptor, metadataVariableDescriptor);
}
Also used : LocalVariableDescriptor(org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 23 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class CodegenAnnotatingVisitor method visitWhenExpression.

@Override
public void visitWhenExpression(@NotNull KtWhenExpression expression) {
    super.visitWhenExpression(expression);
    if (!isWhenWithEnums(expression))
        return;
    String currentClassName = getCurrentTopLevelClassOrPackagePartInternalName(expression.getContainingKtFile());
    if (bindingContext.get(MAPPINGS_FOR_WHENS_BY_ENUM_IN_CLASS_FILE, currentClassName) == null) {
        bindingTrace.record(MAPPINGS_FOR_WHENS_BY_ENUM_IN_CLASS_FILE, currentClassName, new ArrayList<WhenByEnumsMapping>(1));
    }
    List<WhenByEnumsMapping> mappings = bindingContext.get(MAPPINGS_FOR_WHENS_BY_ENUM_IN_CLASS_FILE, currentClassName);
    assert mappings != null : "guaranteed by contract";
    int fieldNumber = mappings.size();
    assert expression.getSubjectExpression() != null : "subject expression should be not null in a valid when by enums";
    KotlinType type = WhenChecker.whenSubjectType(expression, bindingContext);
    assert type != null : "should not be null in a valid when by enums";
    ClassDescriptor classDescriptor = (ClassDescriptor) type.getConstructor().getDeclarationDescriptor();
    assert classDescriptor != null : "because it's enum";
    WhenByEnumsMapping mapping = new WhenByEnumsMapping(classDescriptor, currentClassName, fieldNumber);
    for (ConstantValue<?> constant : SwitchCodegenUtil.getAllConstants(expression, bindingContext, shouldInlineConstVals)) {
        if (constant instanceof NullValue)
            continue;
        assert constant instanceof EnumValue : "expression in when should be EnumValue";
        mapping.putFirstTime((EnumValue) constant, mapping.size() + 1);
    }
    mappings.add(mapping);
    bindingTrace.record(MAPPING_FOR_WHEN_BY_ENUM, expression, mapping);
}
Also used : WhenByEnumsMapping(org.jetbrains.kotlin.codegen.when.WhenByEnumsMapping) NullValue(org.jetbrains.kotlin.resolve.constants.NullValue) EnumValue(org.jetbrains.kotlin.resolve.constants.EnumValue) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 24 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class CodegenAnnotatingVisitor method visitProperty.

@Override
public void visitProperty(@NotNull KtProperty property) {
    DeclarationDescriptor descriptor = bindingContext.get(DECLARATION_TO_DESCRIPTOR, property);
    // working around a problem with shallow analysis
    if (descriptor == null)
        return;
    if (descriptor instanceof LocalVariableDescriptor) {
        recordLocalVariablePropertyMetadata((LocalVariableDescriptor) descriptor);
    }
    String nameForClassOrPackageMember = getNameForClassOrPackageMember(descriptor);
    if (nameForClassOrPackageMember != null) {
        nameStack.push(nameForClassOrPackageMember);
    } else {
        nameStack.push(peekFromStack(nameStack) + '$' + safeIdentifier(property.getNameAsSafeName()).asString());
    }
    KtPropertyDelegate delegate = property.getDelegate();
    if (delegate != null && descriptor instanceof VariableDescriptorWithAccessors) {
        VariableDescriptorWithAccessors variableDescriptor = (VariableDescriptorWithAccessors) descriptor;
        String name = inventAnonymousClassName();
        KotlinType supertype = runtimeTypes.getSupertypeForPropertyReference(variableDescriptor, variableDescriptor.isVar(), /* bound = */
        false);
        ClassDescriptor classDescriptor = recordClassForCallable(delegate, variableDescriptor, Collections.singleton(supertype), name);
        recordClosure(classDescriptor, name);
    }
    super.visitProperty(property);
    nameStack.pop();
}
Also used : LocalVariableDescriptor(org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 25 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class MemberCodegen method shouldInitializeProperty.

protected boolean shouldInitializeProperty(@NotNull KtProperty property) {
    if (!property.hasDelegateExpressionOrInitializer())
        return false;
    PropertyDescriptor propertyDescriptor = (PropertyDescriptor) bindingContext.get(VARIABLE, property);
    assert propertyDescriptor != null;
    if (propertyDescriptor.isConst()) {
        //const initializer always inlined
        return false;
    }
    KtExpression initializer = property.getInitializer();
    ConstantValue<?> initializerValue = initializer != null ? ExpressionCodegen.getCompileTimeConstant(initializer, bindingContext, state.getShouldInlineConstVals()) : null;
    // because Java's completion for annotation arguments uses this information
    if (initializerValue == null)
        return state.getClassBuilderMode().generateBodies;
    //TODO: OPTIMIZATION: don't initialize static final fields
    KotlinType jetType = getPropertyOrDelegateType(property, propertyDescriptor);
    Type type = typeMapper.mapType(jetType);
    return !skipDefaultValue(propertyDescriptor, initializerValue.getValue(), type);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Aggregations

KotlinType (org.jetbrains.kotlin.types.KotlinType)110 NotNull (org.jetbrains.annotations.NotNull)34 IElementType (com.intellij.psi.tree.IElementType)16 Type (org.jetbrains.org.objectweb.asm.Type)16 Nullable (org.jetbrains.annotations.Nullable)10 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)7 PsiElement (com.intellij.psi.PsiElement)6 Name (org.jetbrains.kotlin.name.Name)6 ArrayList (java.util.ArrayList)4 KtExpression (org.jetbrains.kotlin.psi.KtExpression)4 Map (java.util.Map)3 BothSignatureWriter (org.jetbrains.kotlin.codegen.signature.BothSignatureWriter)3 JvmSignatureWriter (org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter)3 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)3 LocalVariableDescriptor (org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor)3 DataFlowInfo (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)3 ExpressionReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver)3 PrimitiveType (org.jetbrains.kotlin.builtins.PrimitiveType)2 CallableDescriptor (org.jetbrains.kotlin.descriptors.CallableDescriptor)2 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)2