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.google.common.collect.Lists; 020 import com.intellij.psi.PsiElement; 021 import com.intellij.util.ArrayUtil; 022 import org.jetbrains.annotations.NotNull; 023 import org.jetbrains.annotations.Nullable; 024 import org.jetbrains.jet.codegen.binding.CalculatedClosure; 025 import org.jetbrains.jet.codegen.context.CodegenContext; 026 import org.jetbrains.jet.codegen.context.LocalLookup; 027 import org.jetbrains.jet.codegen.signature.BothSignatureWriter; 028 import org.jetbrains.jet.lang.resolve.java.jvmSignature.JvmMethodSignature; 029 import org.jetbrains.jet.codegen.state.GenerationState; 030 import org.jetbrains.jet.codegen.state.JetTypeMapper; 031 import org.jetbrains.jet.lang.descriptors.*; 032 import org.jetbrains.jet.lang.resolve.BindingContext; 033 import org.jetbrains.jet.lang.resolve.DescriptorUtils; 034 import org.jetbrains.jet.lang.resolve.java.JvmAbi; 035 import org.jetbrains.jet.lang.resolve.name.Name; 036 import org.jetbrains.jet.lang.types.JetType; 037 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; 038 import org.jetbrains.org.objectweb.asm.MethodVisitor; 039 import org.jetbrains.org.objectweb.asm.Type; 040 import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter; 041 import org.jetbrains.org.objectweb.asm.commons.Method; 042 043 import java.util.ArrayList; 044 import java.util.Collections; 045 import java.util.List; 046 047 import static org.jetbrains.jet.codegen.AsmUtil.*; 048 import static org.jetbrains.jet.codegen.JvmCodegenUtil.isConst; 049 import static org.jetbrains.jet.codegen.binding.CodegenBinding.*; 050 import static org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass; 051 import static org.jetbrains.org.objectweb.asm.Opcodes.*; 052 053 public class ClosureCodegen extends ParentCodegenAwareImpl { 054 private final PsiElement fun; 055 private final FunctionDescriptor funDescriptor; 056 private final SamType samType; 057 private final JetType superClassType; 058 private final List<JetType> superInterfaceTypes; 059 private final CodegenContext context; 060 private final FunctionGenerationStrategy strategy; 061 private final CalculatedClosure closure; 062 private final Type asmType; 063 private final int visibilityFlag; 064 private final KotlinSyntheticClass.Kind syntheticClassKind; 065 066 private Method constructor; 067 068 public ClosureCodegen( 069 @NotNull GenerationState state, 070 @NotNull PsiElement fun, 071 @NotNull FunctionDescriptor funDescriptor, 072 @Nullable SamType samType, 073 @NotNull CodegenContext parentContext, 074 @NotNull KotlinSyntheticClass.Kind syntheticClassKind, 075 @NotNull LocalLookup localLookup, 076 @NotNull FunctionGenerationStrategy strategy, 077 @Nullable MemberCodegen<?> parentCodegen 078 ) { 079 super(state, parentCodegen); 080 081 this.fun = fun; 082 this.funDescriptor = funDescriptor; 083 this.samType = samType; 084 this.context = parentContext.intoClosure(funDescriptor, localLookup, typeMapper); 085 this.syntheticClassKind = syntheticClassKind; 086 this.strategy = strategy; 087 088 ClassDescriptor classDescriptor = anonymousClassForFunction(bindingContext, funDescriptor); 089 090 if (samType == null) { 091 this.superInterfaceTypes = new ArrayList<JetType>(); 092 093 JetType superClassType = null; 094 for (JetType supertype : classDescriptor.getTypeConstructor().getSupertypes()) { 095 ClassifierDescriptor classifier = supertype.getConstructor().getDeclarationDescriptor(); 096 if (DescriptorUtils.isTrait(classifier)) { 097 superInterfaceTypes.add(supertype); 098 } 099 else { 100 assert superClassType == null : "Closure class can't have more than one superclass: " + funDescriptor; 101 superClassType = supertype; 102 } 103 } 104 assert superClassType != null : "Closure class should have a superclass: " + funDescriptor; 105 106 this.superClassType = superClassType; 107 } 108 else { 109 this.superInterfaceTypes = Collections.singletonList(samType.getType()); 110 this.superClassType = KotlinBuiltIns.getInstance().getAnyType(); 111 } 112 113 this.closure = bindingContext.get(CLOSURE, classDescriptor); 114 assert closure != null : "Closure must be calculated for class: " + classDescriptor; 115 116 this.asmType = asmTypeForAnonymousClass(bindingContext, funDescriptor); 117 118 visibilityFlag = AsmUtil.getVisibilityAccessFlagForAnonymous(classDescriptor); 119 } 120 121 public void gen() { 122 ClassBuilder cv = state.getFactory().newVisitor(asmType, fun.getContainingFile()); 123 124 FunctionDescriptor erasedInterfaceFunction; 125 if (samType == null) { 126 erasedInterfaceFunction = getErasedInvokeFunction(funDescriptor); 127 } 128 else { 129 erasedInterfaceFunction = samType.getAbstractMethod().getOriginal(); 130 } 131 132 BothSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.CLASS); 133 if (samType != null) { 134 typeMapper.writeFormalTypeParameters(samType.getType().getConstructor().getParameters(), sw); 135 } 136 sw.writeSuperclass(); 137 Type superClassAsmType = typeMapper.mapSupertype(superClassType, sw); 138 sw.writeSuperclassEnd(); 139 String[] superInterfaceAsmTypes = new String[superInterfaceTypes.size()]; 140 for (int i = 0; i < superInterfaceTypes.size(); i++) { 141 JetType superInterfaceType = superInterfaceTypes.get(i); 142 sw.writeInterface(); 143 superInterfaceAsmTypes[i] = typeMapper.mapSupertype(superInterfaceType, sw).getInternalName(); 144 sw.writeInterfaceEnd(); 145 } 146 147 cv.defineClass(fun, 148 V1_6, 149 ACC_FINAL | ACC_SUPER | visibilityFlag, 150 asmType.getInternalName(), 151 sw.makeJavaGenericSignature(), 152 superClassAsmType.getInternalName(), 153 superInterfaceAsmTypes 154 ); 155 cv.visitSource(fun.getContainingFile().getName(), null); 156 157 writeKotlinSyntheticClassAnnotation(cv, syntheticClassKind); 158 159 JvmMethodSignature jvmMethodSignature = 160 typeMapper.mapSignature(funDescriptor).replaceName(erasedInterfaceFunction.getName().toString()); 161 generateBridge(cv, typeMapper.mapSignature(erasedInterfaceFunction).getAsmMethod(), jvmMethodSignature.getAsmMethod()); 162 163 FunctionCodegen fc = new FunctionCodegen(context, cv, state, getParentCodegen()); 164 fc.generateMethod(fun, jvmMethodSignature, funDescriptor, strategy); 165 166 this.constructor = generateConstructor(cv, superClassAsmType); 167 168 if (isConst(closure)) { 169 generateConstInstance(cv); 170 } 171 172 genClosureFields(closure, cv, typeMapper); 173 174 fc.generateDefaultIfNeeded(context.intoFunction(funDescriptor), 175 typeMapper.mapSignature(funDescriptor), 176 funDescriptor, 177 context.getContextKind(), 178 DefaultParameterValueLoader.DEFAULT, 179 null); 180 181 182 cv.done(); 183 } 184 185 @NotNull 186 public StackValue putInstanceOnStack(@NotNull InstructionAdapter v, @NotNull ExpressionCodegen codegen) { 187 if (isConst(closure)) { 188 v.getstatic(asmType.getInternalName(), JvmAbi.INSTANCE_FIELD, asmType.getDescriptor()); 189 } 190 else { 191 v.anew(asmType); 192 v.dup(); 193 194 codegen.pushClosureOnStack(closure, false, codegen.defaultCallGenerator); 195 v.invokespecial(asmType.getInternalName(), "<init>", constructor.getDescriptor()); 196 } 197 return StackValue.onStack(asmType); 198 } 199 200 201 private void generateConstInstance(@NotNull ClassBuilder cv) { 202 MethodVisitor mv = cv.newMethod(fun, ACC_STATIC | ACC_SYNTHETIC, "<clinit>", "()V", null, ArrayUtil.EMPTY_STRING_ARRAY); 203 InstructionAdapter iv = new InstructionAdapter(mv); 204 205 cv.newField(fun, ACC_STATIC | ACC_FINAL, JvmAbi.INSTANCE_FIELD, asmType.getDescriptor(), null, null); 206 207 if (state.getClassBuilderMode() == ClassBuilderMode.FULL) { 208 mv.visitCode(); 209 iv.anew(asmType); 210 iv.dup(); 211 iv.invokespecial(asmType.getInternalName(), "<init>", "()V"); 212 iv.putstatic(asmType.getInternalName(), JvmAbi.INSTANCE_FIELD, asmType.getDescriptor()); 213 mv.visitInsn(RETURN); 214 FunctionCodegen.endVisit(mv, "<clinit>", fun); 215 } 216 } 217 218 private void generateBridge(@NotNull ClassBuilder cv, @NotNull Method bridge, @NotNull Method delegate) { 219 if (bridge.equals(delegate)) return; 220 221 MethodVisitor mv = 222 cv.newMethod(fun, ACC_PUBLIC | ACC_BRIDGE, bridge.getName(), bridge.getDescriptor(), null, ArrayUtil.EMPTY_STRING_ARRAY); 223 224 if (state.getClassBuilderMode() != ClassBuilderMode.FULL) return; 225 226 mv.visitCode(); 227 228 InstructionAdapter iv = new InstructionAdapter(mv); 229 iv.load(0, asmType); 230 231 ReceiverParameterDescriptor receiver = funDescriptor.getReceiverParameter(); 232 int count = 1; 233 if (receiver != null) { 234 StackValue.local(count, bridge.getArgumentTypes()[count - 1]).put(typeMapper.mapType(receiver.getType()), iv); 235 count++; 236 } 237 238 List<ValueParameterDescriptor> params = funDescriptor.getValueParameters(); 239 for (ValueParameterDescriptor param : params) { 240 StackValue.local(count, bridge.getArgumentTypes()[count - 1]).put(typeMapper.mapType(param.getType()), iv); 241 count++; 242 } 243 244 iv.invokevirtual(asmType.getInternalName(), delegate.getName(), delegate.getDescriptor()); 245 StackValue.onStack(delegate.getReturnType()).put(bridge.getReturnType(), iv); 246 247 iv.areturn(bridge.getReturnType()); 248 249 FunctionCodegen.endVisit(mv, "bridge", fun); 250 } 251 252 @NotNull 253 private Method generateConstructor(@NotNull ClassBuilder cv, @NotNull Type superClassAsmType) { 254 List<FieldInfo> args = calculateConstructorParameters(typeMapper, closure, asmType); 255 256 return generateConstructor(cv, args, fun, superClassAsmType, state, visibilityFlag); 257 } 258 259 public static Method generateConstructor( 260 @NotNull ClassBuilder cv, 261 @NotNull List<FieldInfo> args, 262 @Nullable PsiElement fun, 263 @NotNull Type superClass, 264 @NotNull GenerationState state, 265 int flags 266 ) { 267 Type[] argTypes = fieldListToTypeArray(args); 268 269 Method constructor = new Method("<init>", Type.VOID_TYPE, argTypes); 270 MethodVisitor mv = cv.newMethod(fun, flags, "<init>", constructor.getDescriptor(), null, 271 ArrayUtil.EMPTY_STRING_ARRAY); 272 if (state.getClassBuilderMode() == ClassBuilderMode.FULL) { 273 mv.visitCode(); 274 InstructionAdapter iv = new InstructionAdapter(mv); 275 276 int k = 1; 277 for (FieldInfo fieldInfo : args) { 278 k = AsmUtil.genAssignInstanceFieldFromParam(fieldInfo, k, iv); 279 } 280 281 iv.load(0, superClass); 282 iv.invokespecial(superClass.getInternalName(), "<init>", "()V"); 283 284 iv.visitInsn(RETURN); 285 286 FunctionCodegen.endVisit(iv, "constructor", fun); 287 } 288 return constructor; 289 } 290 291 @NotNull 292 public static List<FieldInfo> calculateConstructorParameters( 293 @NotNull JetTypeMapper typeMapper, 294 @NotNull CalculatedClosure closure, 295 @NotNull Type ownerType 296 ) { 297 BindingContext bindingContext = typeMapper.getBindingContext(); 298 List<FieldInfo> args = Lists.newArrayList(); 299 ClassDescriptor captureThis = closure.getCaptureThis(); 300 if (captureThis != null) { 301 Type type = typeMapper.mapType(captureThis); 302 args.add(FieldInfo.createForHiddenField(ownerType, type, CAPTURED_THIS_FIELD)); 303 } 304 JetType captureReceiverType = closure.getCaptureReceiverType(); 305 if (captureReceiverType != null) { 306 args.add(FieldInfo.createForHiddenField(ownerType, typeMapper.mapType(captureReceiverType), CAPTURED_RECEIVER_FIELD)); 307 } 308 309 for (DeclarationDescriptor descriptor : closure.getCaptureVariables().keySet()) { 310 if (descriptor instanceof VariableDescriptor && !(descriptor instanceof PropertyDescriptor)) { 311 Type sharedVarType = typeMapper.getSharedVarType(descriptor); 312 313 Type type = sharedVarType != null 314 ? sharedVarType 315 : typeMapper.mapType((VariableDescriptor) descriptor); 316 args.add(FieldInfo.createForHiddenField(ownerType, type, "$" + descriptor.getName().asString())); 317 } 318 else if (isLocalNamedFun(descriptor)) { 319 Type classType = asmTypeForAnonymousClass(bindingContext, (FunctionDescriptor) descriptor); 320 args.add(FieldInfo.createForHiddenField(ownerType, classType, "$" + descriptor.getName().asString())); 321 } 322 else if (descriptor instanceof FunctionDescriptor) { 323 assert captureReceiverType != null; 324 } 325 } 326 return args; 327 } 328 329 private static Type[] fieldListToTypeArray(List<FieldInfo> args) { 330 Type[] argTypes = new Type[args.size()]; 331 for (int i = 0; i != argTypes.length; ++i) { 332 argTypes[i] = args.get(i).getFieldType(); 333 } 334 return argTypes; 335 } 336 337 @NotNull 338 public static FunctionDescriptor getErasedInvokeFunction(@NotNull FunctionDescriptor funDescriptor) { 339 int arity = funDescriptor.getValueParameters().size(); 340 ClassDescriptor funClass = funDescriptor.getReceiverParameter() == null 341 ? KotlinBuiltIns.getInstance().getFunction(arity) 342 : KotlinBuiltIns.getInstance().getExtensionFunction(arity); 343 return funClass.getDefaultType().getMemberScope().getFunctions(Name.identifier("invoke")).iterator().next(); 344 } 345 }