public static boolean shouldBeInlined(
      @NotNull JetCallExpression expression, @NotNull TranslationContext context) {
    if (!context.getConfig().isInlineEnabled()) return false;

    CallableDescriptor descriptor = getFunctionDescriptor(expression, context);
    return shouldBeInlined(descriptor);
  }
示例#2
0
  private WhenTranslator(
      @NotNull KtWhenExpression expression, @NotNull TranslationContext context) {
    super(context);

    whenExpression = expression;

    KtExpression subject = expression.getSubjectExpression();
    if (subject != null) {
      JsExpression subjectExpression = Translation.translateAsExpression(subject, context);
      if (TranslationUtils.isCacheNeeded(subjectExpression)) {
        TemporaryVariable subjectVar = context.declareTemporary(null);
        context.addStatementToCurrentBlock(
            JsAstUtils.assignment(subjectVar.reference(), subjectExpression).makeStmt());
        subjectExpression = subjectVar.reference();
      }
      expressionToMatch = subjectExpression;
    } else {
      expressionToMatch = null;
    }
  }
  @NotNull
  public static JsNode translate(
      @NotNull JetCallExpression expression,
      @Nullable JsExpression receiver,
      @NotNull TranslationContext context) {
    ResolvedCall<? extends FunctionDescriptor> resolvedCall =
        getFunctionResolvedCallWithAssert(expression, context.bindingContext());

    if (isJsCall(resolvedCall)) {
      return (new CallExpressionTranslator(expression, receiver, context)).translateJsCode();
    }

    JsExpression callExpression =
        (new CallExpressionTranslator(expression, receiver, context)).translate();

    if (!resolvedCall.isSafeCall() && shouldBeInlined(expression, context)) {
      setInlineCallMetadata(callExpression, expression, resolvedCall, context);
    }

    return callExpression;
  }
示例#4
0
 @NotNull
 private JsExpression translateOrCondition(
     @NotNull JsExpression leftExpression,
     @NotNull KtWhenCondition condition,
     @NotNull TranslationContext context) {
   TranslationContext rightContext = context.innerBlock();
   JsExpression rightExpression = translateCondition(condition, rightContext);
   context.moveVarsFrom(rightContext);
   if (rightContext.currentBlockIsEmpty()) {
     return new JsBinaryOperation(JsBinaryOperator.OR, leftExpression, rightExpression);
   } else {
     assert rightExpression instanceof JsNameRef : "expected JsNameRef, but: " + rightExpression;
     JsNameRef result = (JsNameRef) rightExpression;
     JsIf ifStatement =
         JsAstUtils.newJsIf(
             leftExpression,
             JsAstUtils.assignment(result, JsLiteral.TRUE).makeStmt(),
             rightContext.getCurrentBlock());
     context.addStatementToCurrentBlock(ifStatement);
     return result;
   }
 }