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.MutableClassDescriptor;
024    import org.jetbrains.kotlin.descriptors.impl.MutablePackageFragmentDescriptor;
025    import org.jetbrains.kotlin.name.FqName;
026    import org.jetbrains.kotlin.name.Name;
027    import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM;
028    import org.jetbrains.kotlin.storage.LockBasedStorageManager;
029    import org.jetbrains.kotlin.types.JetType;
030    import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils;
031    
032    import java.util.Arrays;
033    import java.util.Collection;
034    import java.util.Collections;
035    
036    import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage.getBuiltIns;
037    
038    public class JvmRuntimeTypes {
039        private final ClassDescriptor lambda;
040        private final ClassDescriptor functionReference;
041    
042        public JvmRuntimeTypes() {
043            ModuleDescriptorImpl module = new ModuleDescriptorImpl(
044                    Name.special("<jvm functions impl>"),
045                    LockBasedStorageManager.NO_LOCKS,
046                    TopDownAnalyzerFacadeForJVM.JVM_MODULE_PARAMETERS
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        }
053    
054        @NotNull
055        private static ClassDescriptor createClass(@NotNull PackageFragmentDescriptor packageFragment, @NotNull String name) {
056            MutableClassDescriptor descriptor = new MutableClassDescriptor(
057                    packageFragment, packageFragment.getMemberScope(), ClassKind.CLASS, false, Name.identifier(name), SourceElement.NO_SOURCE
058            );
059    
060            descriptor.setModality(Modality.FINAL);
061            descriptor.setVisibility(Visibilities.PUBLIC);
062            descriptor.setTypeParameterDescriptors(Collections.<TypeParameterDescriptor>emptyList());
063            descriptor.createTypeConstructor();
064    
065            return descriptor;
066        }
067    
068        @NotNull
069        public Collection<JetType> getSupertypesForClosure(@NotNull FunctionDescriptor descriptor) {
070            ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();
071    
072            //noinspection ConstantConditions
073            JetType functionType = getBuiltIns(descriptor).getFunctionType(
074                    Annotations.EMPTY,
075                    receiverParameter == null ? null : receiverParameter.getType(),
076                    ExpressionTypingUtils.getValueParametersTypes(descriptor.getValueParameters()),
077                    descriptor.getReturnType()
078            );
079    
080            return Arrays.asList(lambda.getDefaultType(), functionType);
081        }
082    
083        @NotNull
084        public Collection<JetType> getSupertypesForFunctionReference(@NotNull FunctionDescriptor descriptor) {
085            ReceiverParameterDescriptor extensionReceiver = descriptor.getExtensionReceiverParameter();
086            ReceiverParameterDescriptor dispatchReceiver = descriptor.getDispatchReceiverParameter();
087    
088            JetType receiverType =
089                    extensionReceiver != null ? extensionReceiver.getType() : dispatchReceiver != null ? dispatchReceiver.getType() : null;
090    
091            //noinspection ConstantConditions
092            JetType functionType = getBuiltIns(descriptor).getFunctionType(
093                    Annotations.EMPTY,
094                    receiverType,
095                    ExpressionTypingUtils.getValueParametersTypes(descriptor.getValueParameters()),
096                    descriptor.getReturnType()
097            );
098    
099            return Arrays.asList(functionReference.getDefaultType(), functionType);
100        }
101    }