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.inline; 018 019 import com.intellij.openapi.util.Pair; 020 import com.intellij.openapi.vfs.VirtualFile; 021 import com.intellij.util.ArrayUtil; 022 import org.jetbrains.annotations.NotNull; 023 import org.jetbrains.jet.OutputFile; 024 import org.jetbrains.jet.codegen.*; 025 import org.jetbrains.jet.codegen.state.GenerationState; 026 import org.jetbrains.jet.codegen.state.JetTypeMapper; 027 import org.jetbrains.org.objectweb.asm.*; 028 import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter; 029 import org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode; 030 import org.jetbrains.org.objectweb.asm.tree.FieldInsnNode; 031 import org.jetbrains.org.objectweb.asm.tree.MethodNode; 032 import org.jetbrains.org.objectweb.asm.tree.VarInsnNode; 033 034 import java.io.IOException; 035 import java.util.*; 036 037 import static org.jetbrains.jet.lang.resolve.java.diagnostics.JvmDeclarationOrigin.NO_ORIGIN; 038 039 public class AnonymousObjectTransformer { 040 041 protected final GenerationState state; 042 043 protected final JetTypeMapper typeMapper; 044 045 private MethodNode constructor; 046 047 private final InliningContext inliningContext; 048 049 private final Type oldObjectType; 050 051 private final Type newLambdaType; 052 053 private final ClassReader reader; 054 055 private final boolean isSameModule; 056 057 private final Map<String, List<String>> fieldNames = new HashMap<String, List<String>>(); 058 059 public AnonymousObjectTransformer( 060 @NotNull String objectInternalName, 061 @NotNull InliningContext inliningContext, 062 boolean isSameModule, 063 @NotNull Type newLambdaType 064 ) { 065 this.isSameModule = isSameModule; 066 this.state = inliningContext.state; 067 this.typeMapper = state.getTypeMapper(); 068 this.inliningContext = inliningContext; 069 this.oldObjectType = Type.getObjectType(objectInternalName); 070 this.newLambdaType = newLambdaType; 071 072 //try to find just compiled classes then in dependencies 073 try { 074 OutputFile outputFile = state.getFactory().get(objectInternalName + ".class"); 075 if (outputFile != null) { 076 reader = new ClassReader(outputFile.asByteArray()); 077 } else { 078 VirtualFile file = InlineCodegenUtil.findVirtualFile(state.getProject(), objectInternalName); 079 if (file == null) { 080 throw new RuntimeException("Couldn't find virtual file for " + objectInternalName); 081 } 082 reader = new ClassReader(file.getInputStream()); 083 } 084 } 085 catch (IOException e) { 086 throw new RuntimeException(e); 087 } 088 } 089 090 private void buildInvokeParamsFor(@NotNull ParametersBuilder builder, @NotNull MethodNode node) { 091 builder.addThis(oldObjectType, false); 092 093 Type[] types = Type.getArgumentTypes(node.desc); 094 for (Type type : types) { 095 builder.addNextParameter(type, false, null); 096 } 097 } 098 099 @NotNull 100 public InlineResult doTransform(@NotNull ConstructorInvocation invocation, @NotNull FieldRemapper parentRemapper) { 101 ClassBuilder classBuilder = createClassBuilder(); 102 final List<MethodNode> methodsToTransform = new ArrayList<MethodNode>(); 103 reader.accept(new ClassVisitor(InlineCodegenUtil.API, classBuilder.getVisitor()) { 104 105 @Override 106 public void visitOuterClass(@NotNull String owner, String name, String desc) { 107 InliningContext parent = inliningContext.getParent(); 108 assert parent != null : "Context for transformer should have parent one: " + inliningContext; 109 110 //we don't write owner info for lamdbas and SAMs just only for objects 111 if (parent.isRoot() || parent.isInliningLambdaRootContext()) { 112 //TODO: think about writing method info - there is some problem with new constructor desc calculation 113 super.visitOuterClass(inliningContext.getParent().getClassNameToInline(), null, null); 114 return; 115 } 116 117 super.visitOuterClass(owner, name, desc); 118 } 119 120 @Override 121 public MethodVisitor visitMethod( 122 int access, @NotNull String name, @NotNull String desc, String signature, String[] exceptions 123 ) { 124 MethodNode node = new MethodNode(access, name, desc, signature, exceptions); 125 if (name.equals("<init>")){ 126 if (constructor != null) 127 throw new RuntimeException("Lambda, SAM or anonymous object should have only one constructor"); 128 129 constructor = node; 130 } else { 131 methodsToTransform.add(node); 132 } 133 return node; 134 } 135 136 @Override 137 public FieldVisitor visitField( 138 int access, @NotNull String name, @NotNull String desc, String signature, Object value 139 ) { 140 addUniqueField(name); 141 if (InlineCodegenUtil.isCapturedFieldName(name)) { 142 return null; 143 } else { 144 return super.visitField(access, name, desc, signature, value); 145 } 146 } 147 }, ClassReader.SKIP_FRAMES); 148 149 ParametersBuilder allCapturedParamBuilder = ParametersBuilder.newBuilder(); 150 ParametersBuilder constructorParamBuilder = ParametersBuilder.newBuilder(); 151 List<CapturedParamInfo> additionalFakeParams = 152 extractParametersMappingAndPatchConstructor(constructor, allCapturedParamBuilder, constructorParamBuilder, invocation); 153 154 InlineResult result = InlineResult.create(); 155 for (MethodNode next : methodsToTransform) { 156 MethodVisitor visitor = newMethod(classBuilder, next); 157 InlineResult funResult = inlineMethod(invocation, parentRemapper, visitor, next, allCapturedParamBuilder); 158 result.addAllClassesToRemove(funResult); 159 } 160 161 InlineResult constructorResult = 162 generateConstructorAndFields(classBuilder, allCapturedParamBuilder, constructorParamBuilder, invocation, parentRemapper, additionalFakeParams); 163 164 result.addAllClassesToRemove(constructorResult); 165 166 classBuilder.done(); 167 168 invocation.setNewLambdaType(newLambdaType); 169 return result; 170 } 171 172 @NotNull 173 private InlineResult inlineMethod( 174 @NotNull ConstructorInvocation invocation, 175 @NotNull FieldRemapper parentRemapper, 176 @NotNull MethodVisitor resultVisitor, 177 @NotNull MethodNode sourceNode, 178 @NotNull ParametersBuilder capturedBuilder 179 ) { 180 181 Parameters parameters = getMethodParametersWithCaptured(capturedBuilder, sourceNode); 182 183 RegeneratedLambdaFieldRemapper remapper = 184 new RegeneratedLambdaFieldRemapper(oldObjectType.getInternalName(), newLambdaType.getInternalName(), 185 parameters, invocation.getCapturedLambdasToInline(), 186 parentRemapper); 187 188 MethodInliner inliner = new MethodInliner(sourceNode, parameters, inliningContext.subInline(inliningContext.nameGenerator.subGenerator("lambda")), 189 remapper, isSameModule, "Transformer for " + invocation.getOwnerInternalName()); 190 InlineResult result = inliner.doInline(resultVisitor, new LocalVarRemapper(parameters, 0), false); 191 resultVisitor.visitMaxs(-1, -1); 192 return result; 193 } 194 195 private InlineResult generateConstructorAndFields( 196 @NotNull ClassBuilder classBuilder, 197 @NotNull ParametersBuilder allCapturedBuilder, 198 @NotNull ParametersBuilder constructorInlineBuilder, 199 @NotNull ConstructorInvocation invocation, 200 @NotNull FieldRemapper parentRemapper, 201 @NotNull List<CapturedParamInfo> constructorAdditionalFakeParams 202 ) { 203 List<Type> descTypes = new ArrayList<Type>(); 204 205 Parameters constructorParams = constructorInlineBuilder.buildParameters(); 206 int [] capturedIndexes = new int [constructorParams.totalSize()]; 207 int index = 0; 208 int size = 0; 209 210 //complex processing cause it could have super constructor call params 211 for (ParameterInfo info : constructorParams) { 212 if (!info.isSkipped()) { //not inlined 213 if (info.isCaptured() || info instanceof CapturedParamInfo) { 214 capturedIndexes[index] = size; 215 index++; 216 } 217 218 if (size != 0) { //skip this 219 descTypes.add(info.getType()); 220 } 221 size += info.getType().getSize(); 222 } 223 } 224 225 List<Pair<String, Type>> capturedFieldsToGenerate = new ArrayList<Pair<String, Type>>(); 226 for (CapturedParamInfo capturedParamInfo : allCapturedBuilder.listCaptured()) { 227 if (capturedParamInfo.getLambda() == null) { //not inlined 228 capturedFieldsToGenerate.add(new Pair<String, Type>(capturedParamInfo.getNewFieldName(), capturedParamInfo.getType())); 229 } 230 } 231 232 String constructorDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, descTypes.toArray(new Type[descTypes.size()])); 233 234 MethodVisitor constructorVisitor = classBuilder.newMethod(NO_ORIGIN, 235 AsmUtil.NO_FLAG_PACKAGE_PRIVATE, 236 "<init>", constructorDescriptor, 237 null, ArrayUtil.EMPTY_STRING_ARRAY); 238 239 //initialize captured fields 240 List<FieldInfo> fields = AsmUtil.transformCapturedParams(capturedFieldsToGenerate, newLambdaType); 241 int paramIndex = 0; 242 InstructionAdapter capturedFieldInitializer = new InstructionAdapter(constructorVisitor); 243 for (FieldInfo fieldInfo : fields) { 244 AsmUtil.genAssignInstanceFieldFromParam(fieldInfo, capturedIndexes[paramIndex], capturedFieldInitializer); 245 paramIndex++; 246 } 247 248 //then transform constructor 249 //HACK: in inlinining into constructor we access original captured fields with field access not local var 250 //but this fields added to general params (this assumes local var access) not captured one, 251 //so we need to add them to captured params 252 for (CapturedParamInfo info : constructorAdditionalFakeParams) { 253 CapturedParamInfo fake = constructorInlineBuilder.addCapturedParamCopy(info); 254 255 if (fake.getLambda() != null) { 256 //set remap value to skip this fake (captured with lambda already skipped) 257 StackValue composed = StackValue.composed(StackValue.local(0, oldObjectType), 258 StackValue.field(fake.getType(), 259 oldObjectType, 260 fake.getNewFieldName(), false) 261 ); 262 fake.setRemapValue(composed); 263 } 264 } 265 266 Parameters constructorParameters = constructorInlineBuilder.buildParameters(); 267 268 RegeneratedLambdaFieldRemapper remapper = 269 new RegeneratedLambdaFieldRemapper(oldObjectType.getInternalName(), newLambdaType.getInternalName(), 270 constructorParameters, invocation.getCapturedLambdasToInline(), 271 parentRemapper); 272 273 MethodInliner inliner = new MethodInliner(constructor, constructorParameters, inliningContext.subInline(inliningContext.nameGenerator.subGenerator("lambda")), 274 remapper, isSameModule, "Transformer for constructor of " + invocation.getOwnerInternalName()); 275 InlineResult result = inliner.doInline(capturedFieldInitializer, new LocalVarRemapper(constructorParameters, 0), false); 276 constructorVisitor.visitMaxs(-1, -1); 277 278 AsmUtil.genClosureFields(capturedFieldsToGenerate, classBuilder); 279 //TODO for inline method make public class 280 invocation.setNewConstructorDescriptor(constructorDescriptor); 281 return result; 282 } 283 284 @NotNull 285 private Parameters getMethodParametersWithCaptured( 286 @NotNull ParametersBuilder capturedBuilder, 287 @NotNull MethodNode sourceNode 288 ) { 289 ParametersBuilder builder = ParametersBuilder.newBuilder(); 290 buildInvokeParamsFor(builder, sourceNode); 291 for (CapturedParamInfo param : capturedBuilder.listCaptured()) { 292 builder.addCapturedParamCopy(param); 293 } 294 return builder.buildParameters(); 295 } 296 297 @NotNull 298 private ClassBuilder createClassBuilder() { 299 ClassBuilder classBuilder = state.getFactory().newVisitor(NO_ORIGIN, newLambdaType, inliningContext.getRoot().callElement.getContainingFile()); 300 return new RemappingClassBuilder(classBuilder, new TypeRemapper(inliningContext.typeMapping)); 301 } 302 303 @NotNull 304 private static MethodVisitor newMethod(@NotNull ClassBuilder builder, @NotNull MethodNode original) { 305 return builder.newMethod( 306 NO_ORIGIN, 307 original.access, 308 original.name, 309 original.desc, 310 original.signature, 311 original.exceptions.toArray(new String [original.exceptions.size()]) 312 ); 313 } 314 315 private List<CapturedParamInfo> extractParametersMappingAndPatchConstructor( 316 @NotNull MethodNode constructor, 317 @NotNull ParametersBuilder capturedParamBuilder, 318 @NotNull ParametersBuilder constructorParamBuilder, 319 @NotNull final ConstructorInvocation invocation 320 ) { 321 322 CapturedParamOwner owner = new CapturedParamOwner() { 323 @Override 324 public Type getType() { 325 return Type.getObjectType(invocation.getOwnerInternalName()); 326 } 327 }; 328 329 List<LambdaInfo> capturedLambdas = new ArrayList<LambdaInfo>(); //captured var of inlined parameter 330 List<CapturedParamInfo> constructorAdditionalFakeParams = new ArrayList<CapturedParamInfo>(); 331 Map<Integer, LambdaInfo> indexToLambda = invocation.getLambdasToInline(); 332 Set<Integer> capturedParams = new HashSet<Integer>(); 333 334 //load captured parameters and patch instruction list (NB: there is also could be object fields) 335 AbstractInsnNode cur = constructor.instructions.getFirst(); 336 while (cur != null) { 337 if (cur instanceof FieldInsnNode) { 338 FieldInsnNode fieldNode = (FieldInsnNode) cur; 339 if (fieldNode.getOpcode() == Opcodes.PUTFIELD && InlineCodegenUtil.isCapturedFieldName(fieldNode.name)) { 340 341 boolean isPrevVarNode = fieldNode.getPrevious() instanceof VarInsnNode; 342 boolean isPrevPrevVarNode = isPrevVarNode && fieldNode.getPrevious().getPrevious() instanceof VarInsnNode; 343 344 if (isPrevPrevVarNode) { 345 VarInsnNode node = (VarInsnNode) fieldNode.getPrevious().getPrevious(); 346 if (node.var == 0) { 347 VarInsnNode previous = (VarInsnNode) fieldNode.getPrevious(); 348 int varIndex = previous.var; 349 LambdaInfo lambdaInfo = indexToLambda.get(varIndex); 350 CapturedParamInfo info = capturedParamBuilder.addCapturedParam(owner, fieldNode.name, Type.getType(fieldNode.desc), lambdaInfo != null, null); 351 if (lambdaInfo != null) { 352 info.setLambda(lambdaInfo); 353 capturedLambdas.add(lambdaInfo); 354 } 355 constructorAdditionalFakeParams.add(info); 356 capturedParams.add(varIndex); 357 358 constructor.instructions.remove(previous.getPrevious()); 359 constructor.instructions.remove(previous); 360 AbstractInsnNode temp = cur; 361 cur = cur.getNext(); 362 constructor.instructions.remove(temp); 363 continue; 364 } 365 } 366 } 367 } 368 cur = cur.getNext(); 369 } 370 371 constructorParamBuilder.addThis(oldObjectType, false); 372 Type [] types = Type.getArgumentTypes(invocation.getDesc()); 373 for (Type type : types) { 374 LambdaInfo info = indexToLambda.get(constructorParamBuilder.getNextValueParameterIndex()); 375 ParameterInfo parameterInfo = constructorParamBuilder.addNextParameter(type, info != null, null); 376 parameterInfo.setLambda(info); 377 if (capturedParams.contains(parameterInfo.getIndex())) { 378 parameterInfo.setCaptured(true); 379 } else { 380 //otherwise it's super constructor parameter 381 } 382 } 383 384 //For all inlined lambdas add their captured parameters 385 //TODO: some of such parameters could be skipped - we should perform additional analysis 386 Map<String, LambdaInfo> capturedLambdasToInline = new HashMap<String, LambdaInfo>(); //captured var of inlined parameter 387 List<CapturedParamInfo> allRecapturedParameters = new ArrayList<CapturedParamInfo>(); 388 for (LambdaInfo info : capturedLambdas) { 389 for (CapturedParamInfo var : info.getCapturedVars()) { 390 CapturedParamInfo recapturedParamInfo = capturedParamBuilder.addCapturedParam(var, 391 getNewFieldName(var.getOriginalFieldName())); 392 StackValue composed = StackValue.composed(StackValue.local(0, oldObjectType), 393 StackValue.field(var.getType(), 394 oldObjectType, /*TODO owner type*/ 395 recapturedParamInfo.getNewFieldName(), false) 396 ); 397 recapturedParamInfo.setRemapValue(composed); 398 allRecapturedParameters.add(var); 399 400 constructorParamBuilder.addCapturedParam(var, recapturedParamInfo.getNewFieldName()).setRemapValue(composed); 401 } 402 capturedLambdasToInline.put(info.getLambdaClassType().getInternalName(), info); 403 } 404 405 406 407 invocation.setAllRecapturedParameters(allRecapturedParameters); 408 invocation.setCapturedLambdasToInline(capturedLambdasToInline); 409 410 return constructorAdditionalFakeParams; 411 } 412 413 @NotNull 414 public String getNewFieldName(@NotNull String oldName) { 415 if (InlineCodegenUtil.THIS$0.equals(oldName)) { 416 //"this$0" couldn't clash and we should keep this name invariant for further transformations 417 return oldName; 418 } 419 return addUniqueField(oldName + "$inlined"); 420 } 421 422 @NotNull 423 private String addUniqueField(@NotNull String name) { 424 List<String> existNames = fieldNames.get(name); 425 if (existNames == null) { 426 existNames = new LinkedList<String>(); 427 fieldNames.put(name, existNames); 428 } 429 String suffix = existNames.isEmpty() ? "" : "$" + existNames.size(); 430 String newName = name + suffix; 431 existNames.add(newName); 432 return newName; 433 } 434 }