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