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 com.intellij.openapi.vfs.VirtualFile;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.asm4.MethodVisitor;
023 import org.jetbrains.asm4.Type;
024 import org.jetbrains.asm4.commons.InstructionAdapter;
025 import org.jetbrains.jet.codegen.context.CodegenContext;
026 import org.jetbrains.jet.codegen.state.GenerationState;
027 import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
028 import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
029 import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor;
030 import org.jetbrains.jet.lang.psi.JetFile;
031 import org.jetbrains.jet.lang.resolve.BindingContext;
032 import org.jetbrains.jet.lang.resolve.DescriptorUtils;
033 import org.jetbrains.jet.lang.resolve.java.JvmClassName;
034 import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
035 import org.jetbrains.jet.lang.resolve.java.descriptor.ClassDescriptorFromJvmBytecode;
036 import org.jetbrains.jet.lang.resolve.java.sam.SingleAbstractMethodUtils;
037 import org.jetbrains.jet.lang.resolve.name.FqName;
038 import org.jetbrains.jet.lang.resolve.name.Name;
039 import org.jetbrains.jet.lang.types.JetType;
040
041 import static org.jetbrains.asm4.Opcodes.*;
042 import static org.jetbrains.jet.codegen.AsmUtil.NO_FLAG_PACKAGE_PRIVATE;
043 import static org.jetbrains.jet.lang.resolve.java.AsmTypeConstants.OBJECT_TYPE;
044
045 public class SamWrapperCodegen extends ParentCodegenAwareImpl {
046 private static final String FUNCTION_FIELD_NAME = "function";
047
048 @NotNull private final ClassDescriptorFromJvmBytecode samInterface;
049
050 public SamWrapperCodegen(
051 @NotNull GenerationState state,
052 @NotNull ClassDescriptorFromJvmBytecode 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 // e.g. ASM type for Function2
081 Type functionAsmType = state.getTypeMapper().mapType(functionType);
082
083 cv.newField(null,
084 ACC_SYNTHETIC | ACC_PRIVATE | ACC_FINAL,
085 FUNCTION_FIELD_NAME,
086 functionAsmType.getDescriptor(),
087 null,
088 null);
089
090 generateConstructor(asmType, functionAsmType, cv);
091 generateMethod(asmType, functionAsmType, cv, interfaceFunction, functionType);
092
093 cv.done();
094
095 return asmType;
096 }
097
098 private void generateConstructor(Type ownerType, Type functionType, ClassBuilder cv) {
099 MethodVisitor mv = cv.newMethod(null, NO_FLAG_PACKAGE_PRIVATE, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, functionType), null, null);
100
101 if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
102 mv.visitCode();
103 InstructionAdapter iv = new InstructionAdapter(mv);
104
105 // super constructor
106 iv.load(0, OBJECT_TYPE);
107 iv.invokespecial(OBJECT_TYPE.getInternalName(), "<init>", "()V");
108
109 // save parameter to field
110 iv.load(0, OBJECT_TYPE);
111 iv.load(1, functionType);
112 iv.putfield(ownerType.getInternalName(), FUNCTION_FIELD_NAME, functionType.getDescriptor());
113
114 iv.visitInsn(RETURN);
115 FunctionCodegen.endVisit(iv, "constructor of SAM wrapper", null);
116 }
117 }
118
119 private void generateMethod(
120 Type ownerType,
121 Type functionType,
122 ClassBuilder cv,
123 SimpleFunctionDescriptor interfaceFunction,
124 JetType functionJetType
125 ) {
126
127 // using static context to avoid creating ClassDescriptor and everything else
128 FunctionCodegen codegen = new FunctionCodegen(CodegenContext.STATIC, cv, state, getParentCodegen());
129
130 FunctionDescriptor invokeFunction = functionJetType.getMemberScope()
131 .getFunctions(Name.identifier("invoke")).iterator().next().getOriginal();
132 StackValue functionField = StackValue.field(functionType, ownerType, FUNCTION_FIELD_NAME, false);
133 codegen.genDelegate(interfaceFunction, invokeFunction, functionField);
134 }
135
136 private String getWrapperName(@NotNull JetFile containingFile) {
137 NamespaceDescriptor namespace = state.getBindingContext().get(BindingContext.FILE_TO_NAMESPACE, containingFile);
138 VirtualFile virtualFile = containingFile.getVirtualFile();
139 assert namespace != null : "couldn't find namespace for file: " + virtualFile;
140 FqName fqName = DescriptorUtils.getFQName(namespace).toSafe();
141 String packageInternalName = JvmClassName.byFqNameWithoutInnerClasses(
142 PackageClassUtils.getPackageClassFqName(fqName)).getInternalName();
143 return packageInternalName + "$sam$" + samInterface.getName().asString() + "$" +
144 Integer.toHexString(CodegenUtil.getPathHashCode(virtualFile) * 31 + DescriptorUtils.getFQName(samInterface).hashCode());
145 }
146 }