Search in sources :

Example 1 with ConditionScope

use of org.eclipse.ceylon.model.typechecker.model.ConditionScope in project ceylon by eclipse.

the class DeclarationVisitor method visit.

@Override
public void visit(Tree.Resource that) {
    ConditionScope cb = new ConditionScope();
    cb.setId(id++);
    that.setScope(cb);
    visitElement(that, cb);
    enterScope(cb);
    super.visit(that);
}
Also used : ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope)

Example 2 with ConditionScope

use of org.eclipse.ceylon.model.typechecker.model.ConditionScope in project ceylon by eclipse.

the class StatementTransformer method transform.

public JCTree transform(CustomTree.GuardedVariable that) {
    BoxingStrategy boxingStrategy = CodegenUtil.getBoxingStrategy(that.getDeclarationModel());
    Tree.Expression expr = that.getSpecifierExpression().getExpression();
    Type fromType = expr.getTypeModel();
    Value newValue = that.getDeclarationModel();
    Type toType = newValue.getType();
    Tree.ConditionList conditionList = that.getConditionList();
    Tree.Condition condition = conditionList.getConditions().get(0);
    JCExpression val = expressionGen().transformExpression(expr, newValue.hasUncheckedNullType() ? ExpressionTransformer.EXPR_TARGET_ACCEPTS_NULL : 0);
    at(that);
    if (condition instanceof Tree.IsCondition) {
        if (!willEraseToObject(toType)) {
            // Want raw type for instanceof since it can't be used with generic types
            JCExpression rawToTypeExpr = makeJavaType(toType, JT_NO_PRIMITIVES | JT_RAW);
            // Substitute variable with the correct type to use in the rest of the code block
            val = make().TypeCast(rawToTypeExpr, val);
            if (CodegenUtil.isUnBoxed(newValue) && canUnbox(toType)) {
                val = unboxType(val, toType);
            }
        }
    } else if (condition instanceof Tree.ExistsCondition) {
        Type exprType = fromType;
        if (isOptional(exprType)) {
            exprType = typeFact().getDefiniteType(exprType);
        }
        val = expressionGen().applyErasureAndBoxing(val, exprType, CodegenUtil.hasTypeErased(expr), true, CodegenUtil.hasUntrustedType(expr), boxingStrategy, toType, 0);
    } else if (condition instanceof Tree.NonemptyCondition) {
        Type exprType = fromType;
        if (isOptional(exprType)) {
            exprType = typeFact().getDefiniteType(exprType);
        }
        val = expressionGen().applyErasureAndBoxing(val, exprType, false, true, BoxingStrategy.BOXED, toType, ExpressionTransformer.EXPR_DOWN_CAST);
    }
    SyntheticName alias = naming.alias(that.getIdentifier().getText());
    Substitution subst = naming.addVariableSubst(newValue, alias.getName());
    // FIXME: this is rubbish, but the same rubbish from assert. it's most likely wrong there too
    Scope scope = that.getScope().getScope();
    while (scope instanceof ConditionScope) {
        scope = scope.getScope();
    }
    subst.scopeClose(scope);
    JCExpression varType = makeJavaType(toType);
    return make().VarDef(make().Modifiers(FINAL), alias.asName(), varType, val);
}
Also used : SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) Substitution(org.eclipse.ceylon.compiler.java.codegen.Naming.Substitution) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) Condition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition) Expression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression) Value(org.eclipse.ceylon.model.typechecker.model.Value) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Example 3 with ConditionScope

use of org.eclipse.ceylon.model.typechecker.model.ConditionScope in project ceylon by eclipse.

the class DeclarationVisitor method visit.

@Override
public void visit(Tree.Condition that) {
    ConditionScope cb = new ConditionScope();
    cb.setId(id++);
    that.setScope(cb);
    visitElement(that, cb);
    enterScope(cb);
    super.visit(that);
}
Also used : ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope)

Example 4 with ConditionScope

use of org.eclipse.ceylon.model.typechecker.model.ConditionScope in project ceylon by eclipse.

the class DeclarationVisitor method visit.

@Override
public void visit(Tree.Variable that) {
    if (that instanceof CustomTree.GuardedVariable) {
        ConditionScope cb = new ConditionScope();
        cb.setId(id++);
        that.setScope(cb);
        visitElement(that, cb);
        enterScope(cb);
    }
    Tree.SpecifierExpression se = that.getSpecifierExpression();
    if (se != null) {
        Scope s = scope;
        if (scope instanceof ControlBlock) {
            ControlBlock block = (ControlBlock) scope;
            if (!block.isLet()) {
                scope = scope.getContainer();
            }
        }
        se.visit(this);
        scope = s;
    }
    Tree.Type type = that.getType();
    Value v = new Value();
    that.setDeclarationModel(v);
    visitDeclaration(that, v, !(type instanceof Tree.SyntheticVariable));
    ModelUtil.setVisibleScope(v);
    if (type != null) {
        type.visit(this);
    }
    Tree.Identifier identifier = that.getIdentifier();
    if (identifier != null) {
        identifier.visit(this);
    }
    // TODO: scope should be the variable, not the
    // containing control structure:
    Tree.AnnotationList al = that.getAnnotationList();
    if (al != null) {
        al.visit(this);
    }
    List<Tree.ParameterList> pls = that.getParameterLists();
    for (Tree.ParameterList pl : pls) {
        pl.visit(this);
    }
    if (pls.isEmpty()) {
        if (type instanceof Tree.FunctionModifier) {
            type.addError("variables with no parameters may not be declared using the keyword 'function'");
        }
        if (type instanceof Tree.VoidModifier) {
            type.addError("variables with no parameters may not be declared using the keyword 'void'");
        }
    } else {
        Tree.ParameterList pl = pls.get(0);
        pl.addUnsupportedError("variables with parameter lists are not yet supported");
        if (type instanceof Tree.ValueModifier) {
            type.addError("variables with parameters may not be declared using the keyword 'value'");
        }
    }
    that.setScope(scope);
    that.setUnit(unit);
}
Also used : ControlBlock(org.eclipse.ceylon.model.typechecker.model.ControlBlock) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) ModelUtil.getRealScope(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList)

Example 5 with ConditionScope

use of org.eclipse.ceylon.model.typechecker.model.ConditionScope in project ceylon by eclipse.

the class StatementTransformer method transformBlock.

public List<JCStatement> transformBlock(Tree.Block block, boolean revertRet) {
    if (block == null) {
        return List.<JCStatement>nil();
    }
    at(block);
    CeylonVisitor v = gen().visitor;
    final ListBuffer<JCTree> prevDefs = v.defs;
    final boolean prevInInitializer = v.inInitializer;
    final ClassDefinitionBuilder prevClassBuilder = v.classBuilder;
    Tree.Block oldBlock = block;
    currentBlock = block;
    List<JCStatement> result;
    try {
        v.defs = new ListBuffer<JCTree>();
        v.inInitializer = false;
        v.classBuilder = current();
        pushBlockImports(block);
        java.util.Iterator<Statement> statements = block.getStatements().iterator();
        while (statements.hasNext()) {
            Tree.Statement stmt = statements.next();
            Transformer<JCStatement, Return> returnTransformer;
            if (revertRet && stmt instanceof Tree.Declaration) {
                returnTransformer = returnTransformer(defaultReturnTransformer);
            } else {
                returnTransformer = this.returnTransformer;
            }
            try {
                HasErrorException error = errors().getFirstErrorBlock(stmt);
                if (error == null) {
                    stmt.visit(v);
                } else {
                    v.append(this.makeThrowUnresolvedCompilationError(error));
                    break;
                }
            } finally {
                returnTransformer(returnTransformer);
            }
        }
        popBlockImports(block);
        result = (List<JCStatement>) v.getResult().toList();
        Runnable r = onEndBlock.get(block);
        if (r != null) {
            r.run();
        }
    } finally {
        v.classBuilder = prevClassBuilder;
        v.inInitializer = prevInInitializer;
        v.defs = prevDefs;
        // Close Substitutions which were scoped to this block
        Scope scope = block.getScope();
        while (scope instanceof ConditionScope) {
            scope = scope.getScope();
        }
        naming.closeScopedSubstitutions(scope);
        currentBlock = oldBlock;
    }
    return result;
}
Also used : Return(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Return) JCExpressionStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpressionStatement) Statement(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Statement) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) ForStatement(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ForStatement) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) Statement(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Statement) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) HasErrorException(org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Aggregations

ConditionScope (org.eclipse.ceylon.model.typechecker.model.ConditionScope)6 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)4 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)3 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)3 Value (org.eclipse.ceylon.model.typechecker.model.Value)3 Condition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition)2 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)2 ArrayList (java.util.ArrayList)1 Substitution (org.eclipse.ceylon.compiler.java.codegen.Naming.Substitution)1 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)1 HasErrorException (org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException)1 ExistsCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsCondition)1 ExistsOrNonemptyCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsOrNonemptyCondition)1 Expression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression)1 ForStatement (org.eclipse.ceylon.compiler.typechecker.tree.Tree.ForStatement)1 IsCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.IsCondition)1 NonemptyCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.NonemptyCondition)1 Return (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Return)1 Statement (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Statement)1 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)1