// Source code is taken from org.jetbrains.kotlin.idea.projectView.JetDeclarationTreeNode,
  // updateImple()
  private String getPresentableElement(JetElement declaration) {
    String text = "";
    if (declaration != null) {
      text = declaration.getName();
      if (text == null) return "";
      if (declaration instanceof JetClassInitializer) {
        text = CLASS_INITIALIZER;
      } else if (declaration instanceof JetProperty) {
        JetProperty property = (JetProperty) declaration;
        JetTypeReference ref = property.getTypeReference();
        if (ref != null) {
          text += " ";
          text += ":";
          text += " ";
          text += ref.getText();
        }
      } else if (declaration instanceof JetFunction) {
        JetFunction function = (JetFunction) declaration;
        JetTypeReference receiverTypeRef = function.getReceiverTypeReference();
        if (receiverTypeRef != null) {
          text = receiverTypeRef.getText() + "." + text;
        }
        text += "(";
        List<JetParameter> parameters = function.getValueParameters();
        for (JetParameter parameter : parameters) {
          if (parameter.getName() != null) {
            text += parameter.getName();
            text += " ";
            text += ":";
            text += " ";
          }
          JetTypeReference typeReference = parameter.getTypeReference();
          if (typeReference != null) {
            text += typeReference.getText();
          }
          text += ", ";
        }
        if (parameters.size() > 0) text = text.substring(0, text.length() - 2);
        text += ")";
        JetTypeReference typeReference = function.getTypeReference();
        if (typeReference != null) {
          text += " ";
          text += ":";
          text += " ";
          text += typeReference.getText();
        }
      }
    }

    return text;
  }
Example #2
0
  private InlineResult inlineCall(SMAPAndMethodNode nodeAndSmap) {
    MethodNode node = nodeAndSmap.getNode();
    ReifiedTypeParametersUsages reificationResult =
        reifiedTypeInliner.reifyInstructions(node.instructions);
    generateClosuresBodies();

    // through generation captured parameters will be added to invocationParamBuilder
    putClosureParametersOnStack();

    addInlineMarker(codegen.v, true);

    Parameters parameters = invocationParamBuilder.buildParameters();

    InliningContext info =
        new RootInliningContext(
            expressionMap,
            state,
            codegen.getInlineNameGenerator().subGenerator(functionDescriptor.getName().asString()),
            codegen.getContext(),
            callElement,
            codegen.getParentCodegen().getClassName(),
            reifiedTypeInliner);

    MethodInliner inliner =
        new MethodInliner(
            node,
            parameters,
            info,
            new FieldRemapper(null, null, parameters),
            isSameModule,
            "Method inlining " + callElement.getText(),
            createNestedSourceMapper(nodeAndSmap)); // with captured

    LocalVarRemapper remapper = new LocalVarRemapper(parameters, initialFrameSize);

    MethodNode adapter = InlineCodegenUtil.createEmptyMethodNode();
    // hack to keep linenumber info, otherwise jdi will skip begin of linenumber chain
    adapter.visitInsn(Opcodes.NOP);

    InlineResult result = inliner.doInline(adapter, remapper, true, LabelOwner.SKIP_ALL);
    result.getReifiedTypeParametersUsages().mergeAll(reificationResult);

    CallableMemberDescriptor descriptor = codegen.getContext().getContextDescriptor();
    final Set<String> labels =
        getDeclarationLabels(
            DescriptorToSourceUtils.descriptorToDeclaration(descriptor), descriptor);
    LabelOwner labelOwner =
        new LabelOwner() {
          @Override
          public boolean isMyLabel(@NotNull String name) {
            return labels.contains(name);
          }
        };

    List<MethodInliner.PointForExternalFinallyBlocks> infos =
        MethodInliner.processReturns(adapter, labelOwner, true, null);
    generateAndInsertFinallyBlocks(
        adapter,
        infos,
        ((StackValue.Local) remapper.remap(parameters.totalSize() + 1).value).index);
    removeFinallyMarkers(adapter);

    adapter.accept(new InliningInstructionAdapter(codegen.v));

    addInlineMarker(codegen.v, false);

    return result;
  }