001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.codegen;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.kotlin.descriptors.*;
021 import org.jetbrains.kotlin.descriptors.annotations.Annotations;
022 import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl;
023 import org.jetbrains.kotlin.descriptors.impl.MutablePackageFragmentDescriptor;
024 import org.jetbrains.kotlin.name.FqName;
025 import org.jetbrains.kotlin.name.Name;
026 import org.jetbrains.kotlin.resolve.FunctionTypeResolveUtilsKt;
027 import org.jetbrains.kotlin.resolve.TargetPlatformKt;
028 import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
029 import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform;
030 import org.jetbrains.kotlin.storage.LockBasedStorageManager;
031 import org.jetbrains.kotlin.types.KotlinType;
032 import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils;
033
034 import java.util.*;
035
036 public class JvmRuntimeTypes {
037 private final ClassDescriptor lambda;
038 private final ClassDescriptor functionReference;
039 private final List<ClassDescriptor> propertyReferences;
040 private final List<ClassDescriptor> mutablePropertyReferences;
041
042 public JvmRuntimeTypes() {
043 ModuleDescriptorImpl module = TargetPlatformKt.createModule(
044 JvmPlatform.INSTANCE,
045 Name.special("<jvm functions impl>"),
046 LockBasedStorageManager.NO_LOCKS
047 );
048 PackageFragmentDescriptor kotlinJvmInternal = new MutablePackageFragmentDescriptor(module, new FqName("kotlin.jvm.internal"));
049
050 this.lambda = createClass(kotlinJvmInternal, "Lambda");
051 this.functionReference = createClass(kotlinJvmInternal, "FunctionReference");
052 this.propertyReferences = new ArrayList<ClassDescriptor>(3);
053 this.mutablePropertyReferences = new ArrayList<ClassDescriptor>(3);
054
055 for (int i = 0; i <= 2; i++) {
056 propertyReferences.add(createClass(kotlinJvmInternal, "PropertyReference" + i));
057 mutablePropertyReferences.add(createClass(kotlinJvmInternal, "MutablePropertyReference" + i));
058 }
059 }
060
061 @NotNull
062 private static ClassDescriptor createClass(@NotNull PackageFragmentDescriptor packageFragment, @NotNull String name) {
063 MutableClassDescriptor descriptor = new MutableClassDescriptor(
064 packageFragment, ClassKind.CLASS, false, Name.identifier(name), SourceElement.NO_SOURCE
065 );
066
067 descriptor.setModality(Modality.FINAL);
068 descriptor.setVisibility(Visibilities.PUBLIC);
069 descriptor.setTypeParameterDescriptors(Collections.<TypeParameterDescriptor>emptyList());
070 descriptor.createTypeConstructor();
071
072 return descriptor;
073 }
074
075 @NotNull
076 public Collection<KotlinType> getSupertypesForClosure(@NotNull FunctionDescriptor descriptor) {
077 ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();
078
079 //noinspection ConstantConditions
080 KotlinType functionType = FunctionTypeResolveUtilsKt.createFunctionType(
081 DescriptorUtilsKt.getBuiltIns(descriptor),
082 Annotations.Companion.getEMPTY(),
083 receiverParameter == null ? null : receiverParameter.getType(),
084 ExpressionTypingUtils.getValueParametersTypes(descriptor.getValueParameters()),
085 descriptor.getReturnType()
086 );
087
088 return Arrays.asList(lambda.getDefaultType(), functionType);
089 }
090
091 @NotNull
092 public Collection<KotlinType> getSupertypesForFunctionReference(@NotNull FunctionDescriptor descriptor) {
093 ReceiverParameterDescriptor extensionReceiver = descriptor.getExtensionReceiverParameter();
094 ReceiverParameterDescriptor dispatchReceiver = descriptor.getDispatchReceiverParameter();
095
096 KotlinType receiverType =
097 extensionReceiver != null ? extensionReceiver.getType() : dispatchReceiver != null ? dispatchReceiver.getType() : null;
098
099 //noinspection ConstantConditions
100 KotlinType functionType = FunctionTypeResolveUtilsKt.createFunctionType(
101 DescriptorUtilsKt.getBuiltIns(descriptor),
102 Annotations.Companion.getEMPTY(),
103 receiverType,
104 ExpressionTypingUtils.getValueParametersTypes(descriptor.getValueParameters()),
105 descriptor.getReturnType()
106 );
107
108 return Arrays.asList(functionReference.getDefaultType(), functionType);
109 }
110
111 @NotNull
112 public KotlinType getSupertypeForPropertyReference(@NotNull PropertyDescriptor descriptor, boolean isMutable) {
113 int arity = (descriptor.getExtensionReceiverParameter() != null ? 1 : 0) +
114 (descriptor.getDispatchReceiverParameter() != null ? 1 : 0);
115 return (isMutable ? mutablePropertyReferences : propertyReferences).get(arity).getDefaultType();
116 }
117 }