use of org.jetbrains.kotlin.js.backend.ast.JsNameRef 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.JsNameRef in project kotlin by JetBrains.
the class ReferenceTranslator method translateAsValueReference.
@NotNull
public static JsExpression translateAsValueReference(@NotNull DeclarationDescriptor descriptor, @NotNull TranslationContext context) {
JsExpression alias = context.getAliasForDescriptor(descriptor);
if (alias != null)
return alias;
if (shouldTranslateAsFQN(descriptor, context)) {
return context.getQualifiedReference(descriptor);
}
if (descriptor instanceof PropertyDescriptor) {
PropertyDescriptor property = (PropertyDescriptor) descriptor;
if (context.isFromCurrentModule(property)) {
return context.getInnerReference(property);
} else {
JsExpression qualifier = context.getInnerReference(property.getContainingDeclaration());
JsName name = context.getNameForDescriptor(property);
return new JsNameRef(name, qualifier);
}
}
if (DescriptorUtils.isObject(descriptor) || DescriptorUtils.isEnumEntry(descriptor)) {
if (!context.isFromCurrentModule(descriptor)) {
return getLazyReferenceToObject((ClassDescriptor) descriptor, context);
} else {
JsExpression functionRef = JsAstUtils.pureFqn(context.getNameForObjectInstance((ClassDescriptor) descriptor), null);
return new JsInvocation(functionRef);
}
}
return context.getInnerReference(descriptor);
}
use of org.jetbrains.kotlin.js.backend.ast.JsNameRef in project kotlin by JetBrains.
the class ReferenceTranslator method getLazyReferenceToObject.
@NotNull
private static JsExpression getLazyReferenceToObject(@NotNull ClassDescriptor descriptor, @NotNull TranslationContext context) {
DeclarationDescriptor container = descriptor.getContainingDeclaration();
JsExpression qualifier = context.getInnerReference(container);
return new JsNameRef(context.getNameForDescriptor(descriptor), qualifier);
}
use of org.jetbrains.kotlin.js.backend.ast.JsNameRef in project kotlin by JetBrains.
the class InitializerVisitor method visitProperty.
@Override
public final Void visitProperty(@NotNull KtProperty property, @NotNull TranslationContext context) {
PropertyDescriptor descriptor = BindingUtils.getPropertyDescriptor(context.bindingContext(), property);
JsExpression value = PropertyTranslatorKt.translateDelegateOrInitializerExpression(context, property);
JsStatement statement = null;
KtExpression initializer = property.getInitializer();
KtExpression delegate = property.getDelegateExpression();
if (initializer != null) {
assert value != null;
statement = generateInitializerForProperty(context, descriptor, value);
} else if (delegate != null) {
assert value != null;
statement = generateInitializerForDelegate(descriptor, value);
} else if (Boolean.TRUE.equals(context.bindingContext().get(BindingContext.BACKING_FIELD_REQUIRED, descriptor))) {
JsNameRef backingFieldReference = TranslationUtils.backingFieldReference(context, descriptor);
JsExpression defaultValue = generateDefaultValue(descriptor, context, backingFieldReference);
statement = TranslationUtils.assignmentToBackingField(context, descriptor, defaultValue).makeStmt();
} else if (JsDescriptorUtils.isSimpleFinalProperty(descriptor)) {
JsNameRef propRef = new JsNameRef(context.getNameForDescriptor(descriptor), JsLiteral.THIS);
JsExpression defaultValue = generateDefaultValue(descriptor, context, propRef);
statement = JsAstUtils.assignment(propRef, defaultValue).makeStmt();
}
if (statement != null && !JsAstUtils.isEmptyStatement(statement)) {
context.addStatementsToCurrentBlock(JsAstUtils.flattenStatement(statement));
}
return null;
}
use of org.jetbrains.kotlin.js.backend.ast.JsNameRef in project kotlin by JetBrains.
the class ReferenceTranslator method getPrototypeIfNecessary.
@NotNull
private static JsExpression getPrototypeIfNecessary(@NotNull ClassDescriptor descriptor, @NotNull JsExpression reference) {
if (DescriptorUtils.isObject(descriptor) || DescriptorUtils.isEnumEntry(descriptor)) {
JsNameRef getPrototypeRef = JsAstUtils.pureFqn("getPrototypeOf", JsAstUtils.pureFqn("Object", null));
JsInvocation getPrototypeInvocation = new JsInvocation(getPrototypeRef, reference);
MetadataProperties.setSideEffects(getPrototypeInvocation, SideEffectKind.PURE);
reference = JsAstUtils.pureFqn("constructor", getPrototypeInvocation);
}
return reference;
}
Aggregations