Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Fix for #72: safe access operator for sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Nov 7, 2011
1 parent 3111002 commit a493b2f
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,19 @@
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCBinary;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCConditional;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCLiteral;
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
import com.sun.tools.javac.tree.JCTree.JCNewClass;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCUnary;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;

/**
* This transformer deals with expressions only
Expand Down Expand Up @@ -901,16 +904,35 @@ public JCTree transform(Outer expr) {
}

public JCTree transform(IndexExpression access) {
boolean safe = access.getIndexOperator() instanceof Tree.SafeIndexOp;

// look at the lhs
JCExpression lhs = transformExpression(access.getPrimary());

// do the index
ElementOrRange elementOrRange = access.getElementOrRange();
if(elementOrRange instanceof Tree.Element){
Tree.Element element = (Element) elementOrRange;
JCExpression index = transformExpression(element.getExpression(), BoxingStrategy.BOXED);
// make a "lhs.item(index)" call
return at(access).Apply(List.<JCTree.JCExpression>nil(),
make().Select(lhs, names().fromString("item")), List.of(index));
if(!safe)
// make a "lhs.item(index)" call
return at(access).Apply(List.<JCTree.JCExpression>nil(),
make().Select(lhs, names().fromString("item")), List.of(index));
// make a (let ArrayElem tmp = lhs in (tmp != null ? tmp.item(index) : null)) call
JCExpression arrayType = makeJavaType(access.getPrimary().getTypeModel());
Name varName = names().fromString(tempName("safeaccess"));
// tmpVar.item(index)
JCExpression safeAccess = make().Apply(List.<JCTree.JCExpression>nil(),
make().Select(make().Ident(varName), names().fromString("item")), List.of(index));

at(access.getPrimary());
// (tmpVar != null ? safeAccess : null)
JCConditional conditional = make().Conditional(make().Binary(JCTree.NE, make().Ident(varName), makeNull()),
safeAccess, makeNull());
// ArrayElem tmp = lhs
JCVariableDecl tmpVar = make().VarDef(make().Modifiers(0), varName, arrayType, lhs);
// (let tmpVar in conditional)
return make().LetExpr(tmpVar, conditional);
}else{
throw new RuntimeException("Not supported yet");
}
Expand Down

0 comments on commit a493b2f

Please sign in to comment.