use of org.jetbrains.kotlin.js.backend.ast.JsExpression in project kotlin by JetBrains.
the class DestructuringDeclarationTranslator method translate.
private JsVars translate() {
if (initializer != null) {
context().getCurrentBlock().getStatements().add(JsAstUtils.newVar(multiObjectName, initializer));
}
List<JsVars.JsVar> jsVars = new ArrayList<JsVars.JsVar>();
JsNameRef multiObjNameRef = multiObjectName.makeRef();
for (KtDestructuringDeclarationEntry entry : multiDeclaration.getEntries()) {
VariableDescriptor descriptor = BindingContextUtils.getNotNull(context().bindingContext(), BindingContext.VARIABLE, entry);
// Do not call `componentX` for destructuring entry called _
if (descriptor.getName().isSpecial())
continue;
ResolvedCall<FunctionDescriptor> entryInitCall = context().bindingContext().get(BindingContext.COMPONENT_RESOLVED_CALL, entry);
assert entryInitCall != null : "Entry init call must be not null";
JsExpression entryInitializer = CallTranslator.translate(context(), entryInitCall, multiObjNameRef);
FunctionDescriptor candidateDescriptor = entryInitCall.getCandidateDescriptor();
if (CallExpressionTranslator.shouldBeInlined(candidateDescriptor, context())) {
setInlineCallMetadata(entryInitializer, entry, entryInitCall, context());
}
KotlinType returnType = candidateDescriptor.getReturnType();
if (returnType != null && KotlinBuiltIns.isCharOrNullableChar(returnType) && !KotlinBuiltIns.isCharOrNullableChar(descriptor.getType())) {
entryInitializer = JsAstUtils.charToBoxedChar(entryInitializer);
}
JsName name = context().getNameForDescriptor(descriptor);
if (isVarCapturedInClosure(context().bindingContext(), descriptor)) {
JsNameRef alias = getCapturedVarAccessor(name.makeRef());
entryInitializer = JsAstUtils.wrapValue(alias, entryInitializer);
}
jsVars.add(new JsVars.JsVar(name, entryInitializer));
}
return new JsVars(jsVars, true);
}
use of org.jetbrains.kotlin.js.backend.ast.JsExpression in project kotlin by JetBrains.
the class PatternTranslator method translateExpressionPattern.
@NotNull
public JsExpression translateExpressionPattern(@NotNull KotlinType type, @NotNull JsExpression expressionToMatch, @NotNull KtExpression patternExpression) {
JsExpression expressionToMatchAgainst = translateExpressionForExpressionPattern(patternExpression);
KotlinType patternType = BindingUtils.getTypeForExpression(bindingContext(), patternExpression);
EqualityType matchEquality = equalityType(type);
EqualityType patternEquality = equalityType(patternType);
if (matchEquality == EqualityType.PRIMITIVE && patternEquality == EqualityType.PRIMITIVE) {
return equality(expressionToMatch, expressionToMatchAgainst);
} else if (expressionToMatchAgainst == JsLiteral.NULL) {
return TranslationUtils.nullCheck(expressionToMatch, false);
} else {
return TopLevelFIF.KOTLIN_EQUALS.apply(expressionToMatch, Collections.singletonList(expressionToMatchAgainst), context());
}
}
use of org.jetbrains.kotlin.js.backend.ast.JsExpression in project kotlin by JetBrains.
the class PatternTranslator method translateIsExpression.
@NotNull
public JsExpression translateIsExpression(@NotNull KtIsExpression expression) {
KtExpression left = expression.getLeftHandSide();
JsExpression expressionToCheck = Translation.translateAsExpression(left, context());
KotlinType leftType = context().bindingContext().getType(left);
if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
expressionToCheck = JsAstUtils.charToBoxedChar(expressionToCheck);
}
KtTypeReference typeReference = expression.getTypeReference();
assert typeReference != null;
JsExpression result = translateIsCheck(expressionToCheck, typeReference);
if (result == null)
return JsLiteral.getBoolean(!expression.isNegated());
if (expression.isNegated()) {
return not(result);
}
return result;
}
use of org.jetbrains.kotlin.js.backend.ast.JsExpression in project kotlin by JetBrains.
the class PatternTranslator method doGetIsTypeCheckCallable.
@Nullable
private JsExpression doGetIsTypeCheckCallable(@NotNull KotlinType type) {
ClassifierDescriptor targetDescriptor = type.getConstructor().getDeclarationDescriptor();
if (targetDescriptor != null && AnnotationsUtils.isNativeInterface(targetDescriptor)) {
return type.isMarkedNullable() ? null : namer().isInstanceOf(JsAstUtils.pureFqn("Object", null));
}
JsExpression builtinCheck = getIsTypeCheckCallableForBuiltin(type);
if (builtinCheck != null)
return builtinCheck;
builtinCheck = getIsTypeCheckCallableForPrimitiveBuiltin(type);
if (builtinCheck != null)
return builtinCheck;
TypeParameterDescriptor typeParameterDescriptor = getTypeParameterDescriptorOrNull(type);
if (typeParameterDescriptor != null) {
if (typeParameterDescriptor.isReified()) {
return getIsTypeCheckCallableForReifiedType(typeParameterDescriptor);
}
JsExpression result = null;
for (KotlinType upperBound : typeParameterDescriptor.getUpperBounds()) {
JsExpression next = doGetIsTypeCheckCallable(upperBound);
if (next != null) {
result = result != null ? namer().andPredicate(result, next) : next;
}
}
return result;
}
ClassDescriptor referencedClass = DescriptorUtils.getClassDescriptorForType(type);
JsExpression typeName = ReferenceTranslator.translateAsTypeReference(referencedClass, context());
return namer().isInstanceOf(typeName);
}
use of org.jetbrains.kotlin.js.backend.ast.JsExpression in project kotlin by JetBrains.
the class InOperationTranslator method translate.
@NotNull
public JsExpression translate() {
ResolvedCall<? extends FunctionDescriptor> call = CallUtilKt.getFunctionResolvedCallWithAssert(operation, bindingContext());
if (INT_SPECIALIZATION_TEST.apply(call.getResultingDescriptor())) {
JsExpression candidate = translateInt();
if (candidate != null) {
return candidate;
}
}
JsExpression rightTranslated = Translation.translateAsExpression(right, context());
return translateGeneral(call, rightTranslated);
}
Aggregations