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.codegen.state.GenerationState;
021    import org.jetbrains.kotlin.codegen.state.JetTypeMapper;
022    import org.jetbrains.kotlin.descriptors.*;
023    import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl;
024    import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl;
025    import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil;
026    import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
027    import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor;
028    import org.jetbrains.kotlin.load.kotlin.PackagePartClassUtils;
029    import org.jetbrains.kotlin.name.FqName;
030    import org.jetbrains.kotlin.name.Name;
031    import org.jetbrains.kotlin.psi.KtFile;
032    import org.jetbrains.kotlin.resolve.DescriptorUtils;
033    import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
034    import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOriginKt;
035    import org.jetbrains.kotlin.types.KotlinType;
036    import org.jetbrains.kotlin.util.OperatorNameConventions;
037    import org.jetbrains.org.objectweb.asm.MethodVisitor;
038    import org.jetbrains.org.objectweb.asm.Type;
039    import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter;
040    
041    import java.util.Collections;
042    
043    import static org.jetbrains.kotlin.codegen.AsmUtil.NO_FLAG_PACKAGE_PRIVATE;
044    import static org.jetbrains.kotlin.codegen.AsmUtil.asmTypeByFqNameWithoutInnerClasses;
045    import static org.jetbrains.kotlin.resolve.jvm.AsmTypes.OBJECT_TYPE;
046    import static org.jetbrains.org.objectweb.asm.Opcodes.*;
047    
048    public class SamWrapperCodegen {
049        private static final String FUNCTION_FIELD_NAME = "function";
050    
051        private final GenerationState state;
052        private final JetTypeMapper typeMapper;
053        private final SamType samType;
054        private final MemberCodegen<?> parentCodegen;
055    
056        public SamWrapperCodegen(@NotNull GenerationState state, @NotNull SamType samType, @NotNull MemberCodegen<?> parentCodegen) {
057            this.state = state;
058            this.typeMapper = state.getTypeMapper();
059            this.samType = samType;
060            this.parentCodegen = parentCodegen;
061        }
062    
063        public Type genWrapper(@NotNull KtFile file) {
064            // Name for generated class, in form of whatever$1
065            FqName fqName = getWrapperName(file);
066            Type asmType = asmTypeByFqNameWithoutInnerClasses(fqName);
067    
068            // e.g. (T, T) -> Int
069            KotlinType functionType = samType.getKotlinFunctionType();
070    
071            ClassDescriptor classDescriptor = new ClassDescriptorImpl(
072                    samType.getJavaClassDescriptor().getContainingDeclaration(),
073                    fqName.shortName(),
074                    Modality.FINAL,
075                    ClassKind.CLASS,
076                    Collections.singleton(samType.getType()),
077                    SourceElement.NO_SOURCE
078            );
079            // e.g. compare(T, T)
080            SimpleFunctionDescriptor erasedInterfaceFunction = samType.getAbstractMethod().getOriginal().copy(
081                    classDescriptor,
082                    Modality.FINAL,
083                    Visibilities.PUBLIC,
084                    CallableMemberDescriptor.Kind.SYNTHESIZED,
085                    /*copyOverrides=*/ false
086            );
087    
088            ClassBuilder cv = state.getFactory().newVisitor(JvmDeclarationOriginKt.OtherOrigin(erasedInterfaceFunction), asmType, file);
089            cv.defineClass(file,
090                           V1_6,
091                           ACC_FINAL | ACC_SUPER,
092                           asmType.getInternalName(),
093                           null,
094                           OBJECT_TYPE.getInternalName(),
095                           new String[]{ typeMapper.mapType(samType.getType()).getInternalName() }
096            );
097            cv.visitSource(file.getName(), null);
098    
099            WriteAnnotationUtilKt.writeSyntheticClassMetadata(cv);
100    
101            // e.g. ASM type for Function2
102            Type functionAsmType = typeMapper.mapType(functionType);
103    
104            cv.newField(JvmDeclarationOriginKt.OtherOrigin(erasedInterfaceFunction),
105                        ACC_SYNTHETIC | ACC_PRIVATE | ACC_FINAL,
106                        FUNCTION_FIELD_NAME,
107                        functionAsmType.getDescriptor(),
108                        null,
109                        null);
110    
111            generateConstructor(asmType, functionAsmType, cv);
112            generateMethod(asmType, functionAsmType, cv, erasedInterfaceFunction, functionType);
113    
114            cv.done();
115    
116            return asmType;
117        }
118    
119        private void generateConstructor(Type ownerType, Type functionType, ClassBuilder cv) {
120            MethodVisitor mv = cv.newMethod(JvmDeclarationOriginKt.OtherOrigin(samType.getJavaClassDescriptor()),
121                                            NO_FLAG_PACKAGE_PRIVATE, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, functionType), null, null);
122    
123            if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
124                mv.visitCode();
125                InstructionAdapter iv = new InstructionAdapter(mv);
126    
127                // super constructor
128                iv.load(0, OBJECT_TYPE);
129                iv.invokespecial(OBJECT_TYPE.getInternalName(), "<init>", "()V", false);
130    
131                // save parameter to field
132                iv.load(0, OBJECT_TYPE);
133                iv.load(1, functionType);
134                iv.putfield(ownerType.getInternalName(), FUNCTION_FIELD_NAME, functionType.getDescriptor());
135    
136                iv.visitInsn(RETURN);
137                FunctionCodegen.endVisit(iv, "constructor of SAM wrapper", null);
138            }
139        }
140    
141        private void generateMethod(
142                Type ownerType,
143                Type functionType,
144                ClassBuilder cv,
145                SimpleFunctionDescriptor erasedInterfaceFunction,
146                KotlinType functionJetType
147        ) {
148            // using root context to avoid creating ClassDescriptor and everything else
149            FunctionCodegen codegen = new FunctionCodegen(state.getRootContext().intoClass(
150                    (ClassDescriptor) erasedInterfaceFunction.getContainingDeclaration(), OwnerKind.IMPLEMENTATION, state), cv, state, parentCodegen);
151    
152            FunctionDescriptor invokeFunction =
153                    functionJetType.getMemberScope().getContributedFunctions(OperatorNameConventions.INVOKE, NoLookupLocation.FROM_BACKEND).iterator().next().getOriginal();
154            StackValue functionField = StackValue.field(functionType, ownerType, FUNCTION_FIELD_NAME, false, StackValue.none());
155            codegen.genDelegate(erasedInterfaceFunction, invokeFunction, functionField);
156    
157            // generate sam bridges
158            // TODO: erasedInterfaceFunction is actually not an interface function, but function in generated class
159            SimpleFunctionDescriptor originalInterfaceErased = samType.getAbstractMethod().getOriginal();
160            SimpleFunctionDescriptorImpl descriptorForBridges = SimpleFunctionDescriptorImpl
161                    .create(erasedInterfaceFunction.getContainingDeclaration(), erasedInterfaceFunction.getAnnotations(), originalInterfaceErased.getName(),
162                            CallableMemberDescriptor.Kind.DECLARATION, erasedInterfaceFunction.getSource());
163    
164            descriptorForBridges
165                    .initialize(null, originalInterfaceErased.getDispatchReceiverParameter(), originalInterfaceErased.getTypeParameters(),
166                                originalInterfaceErased.getValueParameters(), originalInterfaceErased.getReturnType(),
167                                Modality.OPEN, originalInterfaceErased.getVisibility());
168    
169            DescriptorUtilsKt.setSingleOverridden(descriptorForBridges, originalInterfaceErased);
170            codegen.generateBridges(descriptorForBridges);
171        }
172    
173        @NotNull
174        private FqName getWrapperName(@NotNull KtFile containingFile) {
175            FqName fileClassFqName = JvmFileClassUtil.getFileClassInfoNoResolve(containingFile).getFileClassFqName();
176            JavaClassDescriptor descriptor = samType.getJavaClassDescriptor();
177            int hash = PackagePartClassUtils.getPathHashCode(containingFile.getVirtualFile()) * 31 +
178                    DescriptorUtils.getFqNameSafe(descriptor).hashCode();
179            String shortName = String.format(
180                    "%s$sam$%s$%08x",
181                    fileClassFqName.shortName().asString(),
182                    descriptor.getName().asString(),
183                    hash
184            );
185            return fileClassFqName.parent().child(Name.identifier(shortName));
186        }
187    }