nullable safe calls supported (only for fields)
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
package org.jetbrains.jet.j2k.ast;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ignatov
|
||||||
|
*/
|
||||||
|
public class CallChain extends Expression {
|
||||||
|
private Expression myExpression;
|
||||||
|
private IdentifierImpl myIdentifier;
|
||||||
|
|
||||||
|
public CallChain(Expression expression, IdentifierImpl identifier) {
|
||||||
|
myExpression = expression;
|
||||||
|
myIdentifier = identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isNullable() {
|
||||||
|
return myIdentifier.isNullable();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public String toKotlin() {
|
||||||
|
if (!myExpression.isEmpty())
|
||||||
|
if (myExpression.isNullable())
|
||||||
|
return myExpression.toKotlin() + QUESTDOT + myIdentifier.toKotlin();
|
||||||
|
else
|
||||||
|
return myExpression.toKotlin() + DOT + myIdentifier.toKotlin();
|
||||||
|
return myIdentifier.toKotlin();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,5 +17,18 @@ public abstract class Expression extends Statement {
|
|||||||
public String toKotlin() {
|
public String toKotlin() {
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isNullable() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,17 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
*/
|
*/
|
||||||
public class IdentifierImpl extends Expression implements Identifier {
|
public class IdentifierImpl extends Expression implements Identifier {
|
||||||
private final String myName;
|
private final String myName;
|
||||||
|
private boolean myNullable = true;
|
||||||
|
|
||||||
public IdentifierImpl(String name) {
|
public IdentifierImpl(String name) {
|
||||||
myName = name;
|
myName = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IdentifierImpl(String name, boolean nullable) {
|
||||||
|
myName = name;
|
||||||
|
myNullable = nullable;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return myName;
|
return myName;
|
||||||
@@ -26,6 +32,11 @@ public class IdentifierImpl extends Expression implements Identifier {
|
|||||||
return BACKTICK + str + BACKTICK;
|
return BACKTICK + str + BACKTICK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isNullable() {
|
||||||
|
return myNullable;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String toKotlin() {
|
public String toKotlin() {
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ public abstract class Node implements INode {
|
|||||||
static final String EQUAL = "=";
|
static final String EQUAL = "=";
|
||||||
static final String EMPTY = "";
|
static final String EMPTY = "";
|
||||||
static final String DOT = ".";
|
static final String DOT = ".";
|
||||||
|
static final String QUESTDOT = "?.";
|
||||||
static final String COLON = ":";
|
static final String COLON = ":";
|
||||||
static final String IN = "in";
|
static final String IN = "in";
|
||||||
static final String AT = "@";
|
static final String AT = "@";
|
||||||
|
|||||||
@@ -225,7 +225,11 @@ public class ExpressionVisitor extends StatementVisitor implements Visitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visitReferenceExpression(PsiReferenceExpression expression) {
|
public void visitReferenceExpression(PsiReferenceExpression expression) {
|
||||||
super.visitReferenceExpression(expression);
|
super.visitReferenceExpression(expression);
|
||||||
myResult = new IdentifierImpl(expression.getQualifiedName()); // TODO
|
final IdentifierImpl identifier = expression.getType() != null ? // TODO: if type exists so id is nullable
|
||||||
|
new IdentifierImpl(expression.getReferenceName()) :
|
||||||
|
new IdentifierImpl(expression.getReferenceName(), false);
|
||||||
|
|
||||||
|
myResult = new CallChain(expressionToExpression(expression.getQualifierExpression()), identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
class Library {
|
||||||
|
final static java.io.PrintStream ourOut;
|
||||||
|
}
|
||||||
|
|
||||||
|
class User {
|
||||||
|
void main() {
|
||||||
|
Library.ourOut.print();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace {
|
||||||
|
open class Library {
|
||||||
|
class object {
|
||||||
|
val ourOut : PrintStream?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
open class User {
|
||||||
|
fun main() : Unit {
|
||||||
|
Library.ourOut?.print()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
System.out.println("Hello, world");
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
System.`out`?.println("Hello, world")
|
||||||
@@ -5,6 +5,6 @@ RED
|
|||||||
YELLOW
|
YELLOW
|
||||||
BLUE
|
BLUE
|
||||||
override public fun run() : Unit {
|
override public fun run() : Unit {
|
||||||
System.out.println(("name()=" + name() + ", toString()=" + toString()))
|
System.`out`?.println(("name()=" + name() + ", toString()=" + toString()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,7 @@ break@test
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println((if (foundIt)
|
System.`out`?.println((if (foundIt)
|
||||||
"Found it"
|
"Found it"
|
||||||
else
|
else
|
||||||
"Didn't find it"))
|
"Didn't find it"))
|
||||||
@@ -1 +1 @@
|
|||||||
(o.toString() + "abc")
|
(str.toString() + "abc")
|
||||||
@@ -1 +1 @@
|
|||||||
((o.toString() + "abc"))
|
((str.toString() + "abc"))
|
||||||
Reference in New Issue
Block a user