Make notNullCheck for safe calls more readable

Cherry-picked from:
https://github.com/develar/kotlin/commit/5f4d9dd0a1d720d36e3387574223b80388ecdca7
This commit is contained in:
develar
2012-08-21 15:38:49 +04:00
committed by Pavel V. Talanov
parent 2f8af7bcec
commit f728d1cb25
@@ -1,3 +1,4 @@
/*
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
@@ -16,10 +17,9 @@
package org.jetbrains.k2js.translate.reference;
import com.google.dart.compiler.backend.js.ast.JsBinaryOperation;
import com.google.dart.compiler.backend.js.ast.JsConditional;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsNullLiteral;
import com.google.dart.compiler.backend.js.ast.JsLiteral;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression;
@@ -27,9 +27,8 @@ import org.jetbrains.jet.lang.psi.JetQualifiedExpression;
import org.jetbrains.jet.lang.psi.JetSafeQualifiedExpression;
import org.jetbrains.k2js.translate.context.TemporaryVariable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.utils.TranslationUtils;
import static com.google.dart.compiler.util.AstUtil.newSequence;
import static org.jetbrains.k2js.translate.utils.TranslationUtils.notNullConditionalTestExpression;
/**
* @author Pavel Talanov
@@ -41,11 +40,8 @@ public enum CallType {
JsExpression constructCall(@Nullable JsExpression receiver, @NotNull CallConstructor constructor,
@NotNull TranslationContext context) {
assert receiver != null;
TemporaryVariable temporaryVariable = context.declareTemporary(receiver);
JsBinaryOperation notNullCheck = TranslationUtils.notNullConditionalTestExpression(temporaryVariable);
JsExpression methodCall = constructor.construct(temporaryVariable.reference());
JsConditional callMethodIfNotNullElseNull = new JsConditional(notNullCheck, methodCall, JsNullLiteral.NULL);
return newSequence(temporaryVariable.assignmentExpression(), callMethodIfNotNullElseNull);
TemporaryVariable cachedValue = context.declareTemporary(receiver);
return new JsConditional(notNullConditionalTestExpression(cachedValue), constructor.construct(cachedValue.reference()), JsLiteral.NULL);
}
},
//TODO: bang qualifier is not implemented in frontend for now
@@ -77,4 +73,4 @@ public enum CallType {
JsExpression construct(@Nullable JsExpression receiver);
}
}
}