use of com.sun.tools.javac.tree.JCTree.JCExpression in project ceylon-compiler by ceylon.
the class ExpressionTransformer method optimiseAssignmentOperator.
private JCExpression optimiseAssignmentOperator(final Tree.AssignmentOp op, final AssignmentOperatorTranslation operator) {
// we don't care about their types since they're unboxed and we know it
JCExpression left = transformExpression(op.getLeftTerm(), BoxingStrategy.UNBOXED, null);
JCExpression right = transformExpression(op.getRightTerm(), BoxingStrategy.UNBOXED, null);
return at(op).Assignop(operator.javacOperator, left, right);
}
use of com.sun.tools.javac.tree.JCTree.JCExpression in project ceylon-compiler by ceylon.
the class ExpressionTransformer method makeTuple.
private JCExpression makeTuple(Type tupleType, java.util.List<Tree.PositionalArgument> expressions) {
if (typeFact().isEmptyType(tupleType)) {
// A tuple terminated by empty
return makeEmpty();
}
JCExpression tail = null;
List<JCExpression> elems = List.<JCExpression>nil();
for (int i = 0; i < expressions.size(); i++) {
Tree.PositionalArgument expr = expressions.get(i);
if (expr instanceof Tree.ListedArgument) {
JCExpression elem = transformExpression(((Tree.ListedArgument) expr).getExpression());
elems = elems.append(elem);
} else if (expr instanceof Tree.SpreadArgument) {
Tree.SpreadArgument spreadExpr = (Tree.SpreadArgument) expr;
// make sure we get a spread part of the right type
Type spreadType = spreadExpr.getExpression().getTypeModel();
Type sequentialSpreadType = null;
// try to get a Sequence
if (typeFact().isNonemptyIterableType(spreadType))
sequentialSpreadType = spreadType.getSupertype(typeFact().getSequenceDeclaration());
// failing that, try Sequential
if (sequentialSpreadType == null)
sequentialSpreadType = spreadType.getSupertype(typeFact().getSequentialDeclaration());
if (sequentialSpreadType != null) {
tail = transformExpression(spreadExpr.getExpression(), BoxingStrategy.BOXED, sequentialSpreadType);
} else {
// must at least be an Iterable then
Type iterableSpreadType = spreadType.getSupertype(typeFact().getIterableDeclaration());
tail = transformExpression(spreadExpr.getExpression(), BoxingStrategy.BOXED, iterableSpreadType);
tail = utilInvocation().sequentialOf(makeReifiedTypeArgument(typeFact().getIteratedType(iterableSpreadType)), tail);
Type elementType = typeFact().getIteratedType(spreadExpr.getTypeModel());
Type sequentialType = typeFact().getSequentialType(elementType);
Type expectedType = spreadExpr.getTypeModel();
if (typeFact().isNonemptyIterableType(spreadExpr.getTypeModel())) {
expectedType = typeFact().getSequenceType(elementType);
} else if (typeFact().isIterableType(spreadExpr.getTypeModel())) {
expectedType = typeFact().getSequentialType(elementType);
}
tail = sequentialEmptiness(tail, expectedType, sequentialType);
}
} else if (expr instanceof Tree.Comprehension) {
Tree.Comprehension comp = (Tree.Comprehension) expr;
Type elementType = expr.getTypeModel();
Type expectedType = comp.getInitialComprehensionClause().getPossiblyEmpty() ? typeFact().getSequentialType(elementType) : typeFact().getSequenceType(elementType);
tail = comprehensionAsSequential(comp, expectedType);
} else {
return makeErroneous(expr, "compiler bug: " + expr.getNodeType() + " is not a supported tuple argument");
}
}
if (!elems.isEmpty()) {
JCExpression reifiedTypeArg = makeReifiedTypeArgument(tupleType.getTypeArgumentList().get(0));
List<JCExpression> args = List.<JCExpression>of(reifiedTypeArg);
args = args.append(make().NewArray(make().Type(syms().objectType), List.<JCExpression>nil(), elems));
if (tail != null) {
args = args.append(tail);
}
JCExpression typeExpr = makeJavaType(tupleType, JT_TYPE_ARGUMENT);
/* Tuple.instance(reifiedElement, new Object[]{elem, elem, elem}, tail) */
return make().TypeCast(typeExpr, make().Apply(List.<JCExpression>nil(), naming.makeQualIdent(make().QualIdent(syms().ceylonTupleType.tsym), "instance"), args));
} else {
return tail;
}
}
use of com.sun.tools.javac.tree.JCTree.JCExpression in project ceylon-compiler by ceylon.
the class ExpressionTransformer method transform.
public JCTree transform(Tree.InOp op) {
if (isCeylonInteger(op.getLeftTerm().getTypeModel())) {
if (op.getRightTerm() instanceof Tree.RangeOp && isCeylonInteger(((Tree.RangeOp) op.getRightTerm()).getLeftTerm().getTypeModel()) && isCeylonInteger(((Tree.RangeOp) op.getRightTerm()).getRightTerm().getTypeModel())) {
return makeOptimizedInIntegerRange(op, syms().longType);
} else if (op.getRightTerm() instanceof Tree.SegmentOp && isCeylonInteger(((Tree.SegmentOp) op.getRightTerm()).getLeftTerm().getTypeModel()) && isCeylonInteger(((Tree.SegmentOp) op.getRightTerm()).getRightTerm().getTypeModel())) {
// x in y:z with x, y, z all Integer
return makeOptimizedInIntegerOrCharacterMeasure(op, syms().ceylonIntegerType, syms().longType);
}
} else if (isCeylonCharacter(op.getLeftTerm().getTypeModel())) {
if (op.getRightTerm() instanceof Tree.RangeOp && isCeylonCharacter(((Tree.RangeOp) op.getRightTerm()).getLeftTerm().getTypeModel()) && isCeylonCharacter(((Tree.RangeOp) op.getRightTerm()).getRightTerm().getTypeModel())) {
// x in y..z with x, y, z all Character
return makeOptimizedInCharacterRange(op);
} else if (op.getRightTerm() instanceof Tree.SegmentOp && isCeylonCharacter(((Tree.SegmentOp) op.getRightTerm()).getLeftTerm().getTypeModel()) && isCeylonInteger(((Tree.SegmentOp) op.getRightTerm()).getRightTerm().getTypeModel())) {
// x in y:z with x, y both Character, z all Integer
return makeOptimizedInIntegerOrCharacterMeasure(op, syms().ceylonCharacterType, syms().intType);
}
}
JCExpression left = transformExpression(op.getLeftTerm(), BoxingStrategy.BOXED, typeFact().getObjectType());
JCExpression right = transformExpression(op.getRightTerm(), BoxingStrategy.BOXED, op.getRightTerm().getTypeModel().getSupertype(typeFact().getCategoryDeclaration()));
Naming.SyntheticName varName = naming.temp();
JCExpression varIdent = varName.makeIdent();
JCExpression contains = at(op).Apply(null, makeSelect(right, "contains"), List.<JCExpression>of(varIdent));
JCExpression typeExpr = makeJavaType(op.getLeftTerm().getTypeModel(), JT_NO_PRIMITIVES);
return makeLetExpr(varName, null, typeExpr, left, contains);
}
use of com.sun.tools.javac.tree.JCTree.JCExpression in project ceylon-compiler by ceylon.
the class ExpressionTransformer method transformArgumentsForCallableSpecifier.
private List<ExpressionAndType> transformArgumentsForCallableSpecifier(CallableSpecifierInvocation invocation) {
List<ExpressionAndType> result = List.<ExpressionAndType>nil();
int argIndex = 0;
for (Parameter parameter : invocation.getMethod().getFirstParameterList().getParameters()) {
Type exprType = expressionGen().getTypeForParameter(parameter, null, this.TP_TO_BOUND);
Parameter declaredParameter = invocation.getMethod().getFirstParameterList().getParameters().get(argIndex);
JCExpression arg = naming.makeName(parameter.getModel(), Naming.NA_IDENT);
arg = expressionGen().applyErasureAndBoxing(arg, exprType, !parameter.getModel().getUnboxed(), // Callables always have boxed params
BoxingStrategy.BOXED, declaredParameter.getType());
result = result.append(new ExpressionAndType(arg, makeJavaType(declaredParameter.getType())));
argIndex++;
}
return result;
}
use of com.sun.tools.javac.tree.JCTree.JCExpression in project ceylon-compiler by ceylon.
the class ExpressionTransformer method makeModuleLiteralCall.
private JCExpression makeModuleLiteralCall(Module module) {
JCExpression modulesGetIdent = naming.makeFQIdent("ceylon", "language", "meta", "modules_", "get_");
JCExpression modulesGet = make().Apply(null, modulesGetIdent, List.<JCExpression>nil());
JCExpression call;
if (module.isDefault()) {
call = make().Apply(null, makeSelect(modulesGet, "getDefault"), List.<JCExpression>nil());
} else {
call = make().Apply(null, makeSelect(modulesGet, "find"), List.<JCExpression>of(ceylonLiteral(module.getNameAsString()), ceylonLiteral(module.getVersion())));
}
// make sure we handle missing modules gracefully
String version = module.getVersion();
return makeMetamodelInvocation("checkModule", List.of(call, ceylonLiteral(module.getNameAsString()), version == null ? makeNull() : ceylonLiteral(version)), null);
}
Aggregations