001 /*
002 * Copyright 2010-2013 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.jet.codegen;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.asm4.MethodVisitor;
022 import org.jetbrains.asm4.Type;
023 import org.jetbrains.asm4.commons.InstructionAdapter;
024 import org.jetbrains.jet.codegen.context.CodegenContext;
025 import org.jetbrains.jet.codegen.state.GenerationState;
026 import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
027 import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor;
028 import org.jetbrains.jet.lang.psi.JetFile;
029 import org.jetbrains.jet.lang.psi.JetPsiUtil;
030 import org.jetbrains.jet.lang.resolve.DescriptorUtils;
031 import org.jetbrains.jet.lang.resolve.java.JvmClassName;
032 import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
033 import org.jetbrains.jet.lang.resolve.java.descriptor.JavaClassDescriptor;
034 import org.jetbrains.jet.lang.resolve.java.sam.SingleAbstractMethodUtils;
035 import org.jetbrains.jet.lang.resolve.name.FqName;
036 import org.jetbrains.jet.lang.resolve.name.Name;
037 import org.jetbrains.jet.lang.types.JetType;
038
039 import static org.jetbrains.asm4.Opcodes.*;
040 import static org.jetbrains.jet.codegen.AsmUtil.NO_FLAG_PACKAGE_PRIVATE;
041 import static org.jetbrains.jet.codegen.AsmUtil.writeKotlinSyntheticClassAnnotation;
042 import static org.jetbrains.jet.lang.resolve.java.AsmTypeConstants.OBJECT_TYPE;
043 import static org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass;
044
045 public class SamWrapperCodegen extends ParentCodegenAwareImpl {
046 private static final String FUNCTION_FIELD_NAME = "function";
047
048 @NotNull private final JavaClassDescriptor samInterface;
049
050 public SamWrapperCodegen(
051 @NotNull GenerationState state,
052 @NotNull JavaClassDescriptor samInterface,
053 @Nullable MemberCodegen parentCodegen
054 ) {
055 super(state, parentCodegen);
056 this.samInterface = samInterface;
057 }
058
059 public Type genWrapper(@NotNull JetFile file) {
060 // Name for generated class, in form of whatever$1
061 Type asmType = Type.getObjectType(getWrapperName(file));
062
063 // e.g. (T, T) -> Int
064 JetType functionType = samInterface.getFunctionTypeForSamInterface();
065 assert functionType != null : samInterface.toString();
066 // e.g. compare(T, T)
067 SimpleFunctionDescriptor interfaceFunction = SingleAbstractMethodUtils.getAbstractMethodOfSamInterface(samInterface);
068
069 ClassBuilder cv = state.getFactory().newVisitor(asmType, file);
070 cv.defineClass(file,
071 V1_6,
072 ACC_FINAL,
073 asmType.getInternalName(),
074 null,
075 OBJECT_TYPE.getInternalName(),
076 new String[]{ typeMapper.mapType(samInterface).getInternalName() }
077 );
078 cv.visitSource(file.getName(), null);
079
080 writeKotlinSyntheticClassAnnotation(cv, KotlinSyntheticClass.Kind.SAM_WRAPPER);
081
082 // e.g. ASM type for Function2
083 Type functionAsmType = state.getTypeMapper().mapType(functionType);
084
085 cv.newField(null,
086 ACC_SYNTHETIC | ACC_PRIVATE | ACC_FINAL,
087 FUNCTION_FIELD_NAME,
088 functionAsmType.getDescriptor(),
089 null,
090 null);
091
092 generateConstructor(asmType, functionAsmType, cv);
093 generateMethod(asmType, functionAsmType, cv, interfaceFunction, functionType);
094
095 cv.done();
096
097 return asmType;
098 }
099
100 private void generateConstructor(Type ownerType, Type functionType, ClassBuilder cv) {
101 MethodVisitor mv = cv.newMethod(null, NO_FLAG_PACKAGE_PRIVATE, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, functionType), null, null);
102
103 if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
104 mv.visitCode();
105 InstructionAdapter iv = new InstructionAdapter(mv);
106
107 // super constructor
108 iv.load(0, OBJECT_TYPE);
109 iv.invokespecial(OBJECT_TYPE.getInternalName(), "<init>", "()V");
110
111 // save parameter to field
112 iv.load(0, OBJECT_TYPE);
113 iv.load(1, functionType);
114 iv.putfield(ownerType.getInternalName(), FUNCTION_FIELD_NAME, functionType.getDescriptor());
115
116 iv.visitInsn(RETURN);
117 FunctionCodegen.endVisit(iv, "constructor of SAM wrapper", null);
118 }
119 }
120
121 private void generateMethod(
122 Type ownerType,
123 Type functionType,
124 ClassBuilder cv,
125 SimpleFunctionDescriptor interfaceFunction,
126 JetType functionJetType
127 ) {
128
129 // using static context to avoid creating ClassDescriptor and everything else
130 FunctionCodegen codegen = new FunctionCodegen(CodegenContext.STATIC, cv, state, getParentCodegen());
131
132 FunctionDescriptor invokeFunction = functionJetType.getMemberScope()
133 .getFunctions(Name.identifier("invoke")).iterator().next().getOriginal();
134 StackValue functionField = StackValue.field(functionType, ownerType, FUNCTION_FIELD_NAME, false);
135 codegen.genDelegate(interfaceFunction, invokeFunction, functionField);
136 }
137
138 private String getWrapperName(@NotNull JetFile containingFile) {
139 FqName packageClassFqName = PackageClassUtils.getPackageClassFqName(JetPsiUtil.getFQName(containingFile));
140 String packageInternalName = JvmClassName.byFqNameWithoutInnerClasses(packageClassFqName).getInternalName();
141 return packageInternalName + "$sam$" + samInterface.getName().asString() + "$" +
142 Integer.toHexString(CodegenUtil.getPathHashCode(containingFile.getVirtualFile()) * 31 + DescriptorUtils.getFqNameSafe(
143 samInterface).hashCode());
144 }
145 }