use of com.redhat.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon-compiler by ceylon.
the class AnnotationModelVisitor method defaultedParameter.
public void defaultedParameter(Tree.SpecifierOrInitializerExpression d) {
if (annotationConstructor != null) {
AnnotationConstructorParameter annotationConstructorParameter = instantiation.getConstructorParameters().get(instantiation.getConstructorParameters().size() - 1);
Declaration t = d.getUnit().getTrueValueDeclaration();
Declaration f = d.getUnit().getFalseValueDeclaration();
Term term = d.getExpression().getTerm();
if (term instanceof Tree.InvocationExpression) {
Tree.Primary primary = ((Tree.InvocationExpression) term).getPrimary();
if (primary instanceof Tree.BaseMemberOrTypeExpression && (isAnnotationConstructor(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration()) || isAnnotationClass(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration()))) {
final AnnotationInvocation prevInstantiation = this.instantiation;
this.instantiation = new AnnotationInvocation();
if (isAnnotationConstructor(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration())) {
Function constructor = (Function) ((Tree.BaseMemberOrTypeExpression) primary).getDeclaration();
instantiation.setConstructorDeclaration(constructor);
instantiation.getConstructorParameters().addAll(((AnnotationInvocation) constructor.getAnnotationConstructor()).getConstructorParameters());
}
checkingDefaults = true;
super.visit(d);
annotationConstructorParameter.setDefaultArgument(this.term);
this.term = null;
checkingDefaults = false;
this.instantiation = prevInstantiation;
} else {
errorDefaultedParameter(d);
}
} else if (term instanceof Tree.Literal || (term instanceof Tree.NegativeOp && ((Tree.NegativeOp) term).getTerm() instanceof Tree.Literal) || (term instanceof Tree.BaseMemberExpression && (((Tree.BaseMemberExpression) term).getDeclaration().equals(t) || ((Tree.BaseMemberExpression) term).getDeclaration().equals(f) || ((Tree.BaseMemberExpression) term).getDeclaration().isParameter() || Decl.isAnonCaseOfEnumeratedType((Tree.BaseMemberExpression) term)))) {
checkingDefaults = true;
super.visit(d);
annotationConstructorParameter.setDefaultArgument(this.term);
this.term = null;
checkingDefaults = false;
} else if (term instanceof Tree.Tuple || term instanceof Tree.SequenceEnumeration) {
// TODO Tuples and SequenceEnumerations of the above cases should also be allowed
checkingDefaults = true;
super.visit(d);
annotationConstructorParameter.setDefaultArgument(this.term);
this.term = null;
checkingDefaults = false;
} else {
errorDefaultedParameter(d);
}
}
}
use of com.redhat.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon-compiler by ceylon.
the class StatementTransformer method transformCaseMatch.
private JCStatement transformCaseMatch(Naming.SyntheticName selectorAlias, Tree.SwitchClause switchClause, Tree.CaseClause caseClause, String tmpVar, Tree.Term outerExpression, Type expectedType, Tree.MatchCase matchCase, JCStatement last, Type switchType, boolean primitiveSelector) {
at(matchCase);
JCExpression tests = null;
java.util.List<Tree.Expression> expressions = matchCase.getExpressionList().getExpressions();
for (Tree.Expression expr : expressions) {
Tree.Term term = ExpressionTransformer.eliminateParens(expr.getTerm());
boolean unboxedEquality = primitiveSelector || isCeylonBasicType(typeFact().getDefiniteType(switchType));
JCExpression transformedExpression = expressionGen().transformExpression(term, unboxedEquality ? BoxingStrategy.UNBOXED : BoxingStrategy.BOXED, term.getTypeModel());
JCExpression test;
if (term instanceof Tree.Literal || term instanceof Tree.NegativeOp) {
if (unboxedEquality) {
if (term instanceof Tree.StringLiteral) {
test = make().Apply(null, makeSelect(unboxType(selectorAlias.makeIdent(), term.getTypeModel()), "equals"), List.<JCExpression>of(transformedExpression));
} else {
test = make().Binary(JCTree.EQ, primitiveSelector ? selectorAlias.makeIdent() : unboxType(selectorAlias.makeIdent(), term.getTypeModel()), transformedExpression);
}
} else {
test = make().Apply(null, makeSelect(selectorAlias.makeIdent(), "equals"), List.<JCExpression>of(transformedExpression));
}
if (isOptional(switchType)) {
test = make().Binary(JCTree.AND, make().Binary(JCTree.NE, selectorAlias.makeIdent(), makeNull()), test);
}
} else {
JCExpression selectorExpr;
if (!primitiveSelector && isCeylonBasicType(typeFact().getDefiniteType(switchType))) {
selectorExpr = unboxType(selectorAlias.makeIdent(), term.getTypeModel());
} else {
selectorExpr = selectorAlias.makeIdent();
}
test = make().Binary(JCTree.EQ, selectorExpr, transformedExpression);
}
if (tests == null)
tests = test;
else if (isNull(term.getTypeModel())) {
// ensure we do any null check as the first operation in the ||-ed expression
tests = make().Binary(JCTree.OR, test, tests);
} else {
tests = make().Binary(JCTree.OR, tests, test);
}
}
Substitution prevSubst = null;
if (switchClause.getSwitched().getVariable() != null) {
// Prepare for variable substitution in the following code block
prevSubst = naming.addVariableSubst(switchClause.getSwitched().getVariable().getDeclarationModel(), selectorAlias.toString());
}
JCBlock block = transformCaseClauseBlock(caseClause, tmpVar, outerExpression, expectedType);
if (prevSubst != null) {
// Deactivate the above variable substitution
prevSubst.close();
}
return at(caseClause).If(tests, block, last);
}
use of com.redhat.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon-compiler by ceylon.
the class ExpressionTransformer method transform.
// Prefix operator
public JCExpression transform(final Tree.PrefixOperatorExpression expr) {
final OperatorTranslation operator = Operators.getOperator(expr.getClass());
if (operator == null) {
return makeErroneous(expr, "compiler bug: " + expr.getNodeType() + " is not supported yet");
}
OptimisationStrategy optimisationStrategy = operator.getUnOpOptimisationStrategy(expr, expr.getTerm(), this);
final boolean canOptimise = optimisationStrategy.useJavaOperator();
Tree.Term term = expr.getTerm();
// only fully optimise if we don't have to access the getter/setter
if (canOptimise && CodegenUtil.isDirectAccessVariable(term)) {
JCExpression jcTerm = transformExpression(term, BoxingStrategy.UNBOXED, expr.getTypeModel(), EXPR_WIDEN_PRIM);
return at(expr).Unary(operator.javacOperator, jcTerm);
}
Interface compoundType = expr.getUnit().getOrdinalDeclaration();
Type valueType = getSupertype(term, compoundType);
final Type returnType = getMostPreciseType(term, getTypeArgument(valueType, 0));
// we work on boxed types unless we could have optimised
return transformAssignAndReturnOperation(expr, term, !canOptimise, valueType, returnType, new AssignAndReturnOperationFactory() {
@Override
public JCExpression getNewValue(JCExpression previousValue) {
// use +1/-1 if we can optimise a bit
if (canOptimise) {
JCExpression ret = make().Binary(operator == OperatorTranslation.UNARY_PREFIX_INCREMENT ? JCTree.PLUS : JCTree.MINUS, previousValue, makeInteger(1));
ret = unAutoPromote(ret, returnType);
return ret;
}
// make this call: previousValue.getSuccessor() or previousValue.getPredecessor()
return make().Apply(null, makeSelect(previousValue, operator.ceylonMethod), List.<JCExpression>nil());
}
});
}
use of com.redhat.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon-compiler by ceylon.
the class StatementTransformer method definitelySatisfiedOrNot.
private boolean definitelySatisfiedOrNot(java.util.List<Tree.Condition> conditions, boolean satisfied) {
if (conditions.size() != 1) {
return false;
}
Tree.Condition condition = conditions.get(0);
if (!(condition instanceof Tree.BooleanCondition)) {
return false;
}
Tree.Term term = ((Tree.BooleanCondition) condition).getExpression().getTerm();
if (!(term instanceof Tree.BaseMemberExpression)) {
return false;
}
Declaration declaration = ((Tree.BaseMemberExpression) term).getDeclaration();
return declaration instanceof Value && satisfied ? isBooleanTrue(declaration) : isBooleanFalse(declaration);
}
use of com.redhat.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon-compiler by ceylon.
the class ExpressionTransformer method getIntegerLiteralPower.
/**
* Returns the literal value of the power in the given power expression,
* or null if the power is not an integer literal (or negation of an
* integer literal)
* @throws ErroneousException
*/
static java.lang.Long getIntegerLiteralPower(Tree.PowerOp op) throws ErroneousException {
java.lang.Long power;
Tree.Term term = unwrapExpressionUntilTerm(op.getRightTerm());
if (term instanceof Tree.NaturalLiteral) {
power = literalValue((Tree.NaturalLiteral) term);
} else if (term instanceof Tree.NegativeOp && ((Tree.NegativeOp) term).getTerm() instanceof Tree.NaturalLiteral) {
power = literalValue((Tree.NegativeOp) term);
} else {
power = null;
}
return power;
}
Aggregations