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