use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class VarargValueArgument method toString.
@Override
public String toString() {
StringBuilder builder = new StringBuilder("vararg:{");
for (Iterator<ValueArgument> iterator = arguments.iterator(); iterator.hasNext(); ) {
ValueArgument valueArgument = iterator.next();
KtExpression expression = valueArgument.getArgumentExpression();
builder.append(expression == null ? "no expression" : expression.getText());
if (iterator.hasNext()) {
builder.append(", ");
}
}
return builder.append("}").toString();
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class ArrayAccessTranslator method contextWithValueParameterAliasInArrayGetAccess.
// this is hack for a[b]++ -> a.set(b, a.get(b) + 1). Frontend generate fake expression for a.get(b) + 1.
@NotNull
private TranslationContext contextWithValueParameterAliasInArrayGetAccess(@NotNull JsExpression toSetTo) {
ResolvedCall<FunctionDescriptor> resolvedCall = BindingUtils.getResolvedCallForArrayAccess(bindingContext(), expression, /*isGetter = */
false);
List<ResolvedValueArgument> arguments = resolvedCall.getValueArgumentsByIndex();
if (arguments == null) {
throw new IllegalStateException("Failed to arrange value arguments by index: " + resolvedCall.getResultingDescriptor());
}
ResolvedValueArgument lastArgument = arguments.get(arguments.size() - 1);
assert lastArgument instanceof ExpressionValueArgument : "Last argument of array-like setter must be ExpressionValueArgument: " + lastArgument;
ValueArgument valueArgument = ((ExpressionValueArgument) lastArgument).getValueArgument();
assert valueArgument != null;
KtExpression element = valueArgument.getArgumentExpression();
return context().innerContextWithAliasesForExpressions(Collections.singletonMap(element, toSetTo));
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class CallExpressionTranslator method translateJsCode.
@NotNull
private JsNode translateJsCode() {
List<? extends ValueArgument> arguments = expression.getValueArguments();
KtExpression argumentExpression = arguments.get(0).getArgumentExpression();
assert argumentExpression != null;
List<JsStatement> statements = parseJsCode(argumentExpression);
int size = statements.size();
JsNode node;
if (size == 0) {
node = JsLiteral.NULL;
} else if (size > 1) {
node = new JsBlock(statements);
} else {
JsStatement resultStatement = statements.get(0);
if (resultStatement instanceof JsExpressionStatement) {
node = ((JsExpressionStatement) resultStatement).getExpression();
} else {
node = resultStatement;
}
}
LexicalScope lexicalScope = context().bindingContext().get(BindingContextSlicesJsKt.LEXICAL_SCOPE_FOR_JS, resolvedCall);
Map<JsName, JsExpression> replacements = new HashMap<JsName, JsExpression>();
if (lexicalScope != null) {
Set<JsName> references = CollectUtilsKt.collectUsedNames(node);
references.removeAll(CollectUtilsKt.collectDefinedNames(node));
for (JsName name : references) {
VariableDescriptor variable = getVariableByName(lexicalScope, Name.identifier(name.getIdent()));
if (variable != null) {
replacements.put(name, ReferenceTranslator.translateAsValueReference(variable, context()));
}
}
if (!replacements.isEmpty()) {
node = RewriteUtilsKt.replaceNames(node, replacements);
}
}
return node;
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class PatternTranslator method translateCastExpression.
@NotNull
public JsExpression translateCastExpression(@NotNull KtBinaryExpressionWithTypeRHS expression) {
assert isCastExpression(expression) : "Expected cast expression, got " + expression;
KtExpression left = expression.getLeft();
JsExpression expressionToCast = Translation.translateAsExpression(left, context());
KtTypeReference typeReference = expression.getRight();
assert typeReference != null : "Cast expression must have type reference";
KotlinType leftType = context().bindingContext().getType(left);
if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
expressionToCast = JsAstUtils.charToBoxedChar(expressionToCast);
}
TemporaryVariable temporary = context().declareTemporary(expressionToCast);
JsExpression isCheck = translateIsCheck(temporary.assignmentExpression(), typeReference);
if (isCheck == null)
return expressionToCast;
JsExpression onFail;
if (isSafeCast(expression)) {
onFail = JsLiteral.NULL;
} else {
JsExpression throwCCEFunRef = Namer.throwClassCastExceptionFunRef();
onFail = new JsInvocation(throwCCEFunRef);
}
JsExpression result = new JsConditional(isCheck, temporary.reference(), onFail);
KotlinType expressionType = context().bindingContext().getType(expression);
if (expressionType != null && KotlinBuiltIns.isCharOrNullableChar(expressionType)) {
result = JsAstUtils.boxedCharToChar(result);
}
return result;
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class OverloadedAssignmentTranslator method reassignment.
@NotNull
private JsExpression reassignment() {
KtExpression left = expression.getLeft();
assert left != null;
AccessTranslator accessTranslator = AccessTranslationUtils.getAccessTranslator(left, context()).getCached();
JsExpression newValue = overloadedMethodInvocation(accessTranslator);
return accessTranslator.translateAsSet(newValue);
}
Aggregations