Search in sources :

Example 11 with Node

use of org.eclipse.ceylon.compiler.typechecker.tree.Node in project ceylon by eclipse.

the class DeclarationVisitor method visit.

@Override
public void visit(Tree.CompilationUnit that) {
    // unit.setSupportedBackends(that.getUnit().getSupportedBackends());
    pkg.removeUnit(unit);
    pkg.addUnit(unit);
    super.visit(that);
    Node firstNonImportNode = null;
    int index = -1;
    for (Node d : that.getDeclarations()) {
        firstNonImportNode = d;
        index = d.getToken().getTokenIndex();
        break;
    }
    for (Tree.ModuleDescriptor md : that.getModuleDescriptors()) {
        if (index < 0 || md.getToken().getTokenIndex() < index) {
            firstNonImportNode = md;
            index = md.getToken().getTokenIndex();
        }
        break;
    }
    for (Tree.PackageDescriptor pd : that.getPackageDescriptors()) {
        if (index < 0 || pd.getToken().getTokenIndex() < index) {
            firstNonImportNode = pd;
            index = pd.getToken().getTokenIndex();
        }
        break;
    }
    if (firstNonImportNode != null) {
        for (Tree.Import im : that.getImportList().getImports()) {
            if (im.getEndIndex() > firstNonImportNode.getStartIndex()) {
                im.addError("import statement must occur before any declaration or descriptor");
            }
        }
    }
    boolean first = true;
    for (Tree.ModuleDescriptor md : that.getModuleDescriptors()) {
        if (!first) {
            md.addError("there may be only one module descriptor for a module");
        }
        first = false;
    }
    first = true;
    for (Tree.PackageDescriptor pd : that.getPackageDescriptors()) {
        if (!first) {
            pd.addError("there may be only one package descriptor for a module");
        }
        first = false;
    }
}
Also used : Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Example 12 with Node

use of org.eclipse.ceylon.compiler.typechecker.tree.Node in project ceylon by eclipse.

the class ExpressionTransformer method transform.

public JCTree transform(Tree.TypeLiteral expr) {
    at(expr);
    if (!expr.getWantsDeclaration()) {
        if (expr.getDeclaration() instanceof Constructor) {
            JCExpression classLiteral = makeTypeLiteralCall(expr.getType().getTypeModel().getQualifyingType(), false, expr.getTypeModel());
            TypeDeclaration classModelDeclaration = (TypeDeclaration) typeFact().getLanguageModuleModelDeclaration(expr.getType().getTypeModel().getQualifyingType().getDeclaration().isMember() ? "MemberClass" : "Class");
            JCTypeCast typeCast = make().TypeCast(makeJavaType(classModelDeclaration.appliedType(null, List.of(expr.getType().getTypeModel().getQualifyingType(), typeFact().getNothingType()))), classLiteral);
            Type callableType = expr.getTypeModel().getFullType();
            JCExpression reifiedArgumentsExpr = makeReifiedTypeArgument(typeFact().getCallableTuple(callableType));
            return make().Apply(null, naming.makeQualIdent(typeCast, "getConstructor"), List.<JCExpression>of(reifiedArgumentsExpr, make().Literal(expr.getDeclaration().getName())));
        } else {
            if (coerced) {
                Type t = expr.getType().getTypeModel();
                if (!typeFact().isJavaObjectArrayType(t) || t.getTypeArgumentList().get(0).isClassOrInterface()) {
                    return makeSelect(makeJavaType(t, JT_NO_PRIMITIVES | JT_RAW), "class");
                }
            }
            return makeTypeLiteralCall(expr.getType().getTypeModel(), true, expr.getTypeModel());
        }
    } else if (expr.getDeclaration() instanceof TypeParameter) {
        // we must get it from its container
        TypeParameter declaration = (TypeParameter) expr.getDeclaration();
        Node node = expr;
        return makeTypeParameterDeclaration(node, declaration);
    } else if (expr.getDeclaration() instanceof Constructor || expr instanceof Tree.NewLiteral) {
        Constructor ctor;
        if (expr.getDeclaration() instanceof Constructor) {
            ctor = (Constructor) expr.getDeclaration();
        } else {
            ctor = ((Class) expr.getDeclaration()).getDefaultConstructor();
        }
        JCExpression metamodelCall = makeTypeDeclarationLiteral(ModelUtil.getConstructedClass(ctor));
        metamodelCall = make().TypeCast(makeJavaType(typeFact().getClassDeclarationType(), JT_RAW), metamodelCall);
        metamodelCall = make().Apply(null, naming.makeQualIdent(metamodelCall, "getConstructorDeclaration"), List.<JCExpression>of(make().Literal(ctor.getName() == null ? "" : ctor.getName())));
        if (ModelUtil.isEnumeratedConstructor(ctor)) {
            metamodelCall = make().TypeCast(makeJavaType(typeFact().getValueConstructorDeclarationType(), JT_RAW), metamodelCall);
        } else /*else if (Decl.isDefaultConstructor(ctor)){
                metamodelCall = make().TypeCast(
                        makeJavaType(typeFact().getDefaultConstructorDeclarationType(), JT_RAW), metamodelCall);
            } */
        {
            metamodelCall = make().TypeCast(makeJavaType(typeFact().getCallableConstructorDeclarationType(), JT_RAW), metamodelCall);
        }
        return metamodelCall;
    } else if (expr.getDeclaration() instanceof ClassOrInterface || expr.getDeclaration() instanceof TypeAlias) {
        // use the generated class to get to the declaration literal
        JCExpression metamodelCall = makeTypeDeclarationLiteral((TypeDeclaration) expr.getDeclaration());
        Type exprType = expr.getTypeModel().resolveAliases();
        // now cast if required
        if (!exprType.isExactly(((TypeDeclaration) typeFact().getLanguageModuleDeclarationDeclaration("NestableDeclaration")).getType())) {
            JCExpression type = makeJavaType(exprType, JT_NO_PRIMITIVES);
            return make().TypeCast(type, metamodelCall);
        }
        return metamodelCall;
    } else {
        return makeErroneous(expr, "compiler bug: " + expr.getDeclaration() + " is an unsupported declaration type");
    }
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCTypeCast(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeCast) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeAlias(org.eclipse.ceylon.model.typechecker.model.TypeAlias) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 13 with Node

use of org.eclipse.ceylon.compiler.typechecker.tree.Node in project ceylon by eclipse.

the class JavaPositionsRetriever method formatCeylonPosition.

private String formatCeylonPosition(int position) {
    String result = ceylonLineMap.getLineNumber(position) + " : " + ceylonLineMap.getColumnNumber(position);
    if (ceylonNodesAtPosition != null) {
        if (ceylonNodesAtPosition.containsKey(position)) {
            List<String> nodeLabels = new LinkedList<String>();
            for (Node node : ceylonNodesAtPosition.get(position)) {
                nodeLabels.add(node.getNodeType() + " - " + node.getToken().getText());
            }
            result += "(" + nodeLabels + ")";
        }
    }
    return result;
}
Also used : Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) LinkedList(java.util.LinkedList)

Example 14 with Node

use of org.eclipse.ceylon.compiler.typechecker.tree.Node in project ceylon by eclipse.

the class RefinementVisitor method checkParameterTypes.

private void checkParameterTypes(Tree.Declaration that, Tree.ParameterList pl, Reference member, Reference refinedMember, ParameterList params, ParameterList refinedParams, boolean forNative) {
    List<Parameter> paramsList = params.getParameters();
    List<Parameter> refinedParamsList = refinedParams.getParameters();
    if (paramsList.size() != refinedParamsList.size()) {
        handleWrongParameterListLength(that, member, refinedMember, forNative, pl);
    } else {
        for (int i = 0; i < paramsList.size(); i++) {
            Parameter rparam = refinedParamsList.get(i);
            Parameter param = paramsList.get(i);
            Tree.Parameter parameter = pl.getParameters().get(i);
            if (forNative && !param.getName().equals(rparam.getName())) {
                parameter.addError("parameter does not have the same name as its header: '" + param.getName() + "' is not '" + rparam.getName() + "' for " + message(refinedMember.getDeclaration()));
            }
            if (rparam.isSequenced() && !param.isSequenced()) {
                parameter.addError("parameter must be variadic: parameter '" + rparam.getName() + "' of " + (forNative ? "native header " : "refined member ") + message(refinedMember.getDeclaration()) + " is variadic");
            }
            if (!rparam.isSequenced() && param.isSequenced()) {
                parameter.addError("parameter may not be variadic: parameter '" + rparam.getName() + "' of " + (forNative ? "native header " : "refined member ") + message(refinedMember.getDeclaration()) + " is not variadic");
            }
            Type refinedParameterType = refinedMember.getTypedParameter(rparam).getFullType();
            Type parameterType = member.getTypedParameter(param).getFullType();
            Node typeNode = parameter;
            if (parameter instanceof Tree.ParameterDeclaration) {
                Tree.ParameterDeclaration pd = (Tree.ParameterDeclaration) parameter;
                Tree.Type type = pd.getTypedDeclaration().getType();
                if (type != null) {
                    typeNode = type;
                }
            }
            if (parameter != null) {
                if (rparam.getModel().isDynamicallyTyped()) {
                    checkRefiningParameterDynamicallyTyped(member, refinedMember, param, typeNode);
                } else if (param.getModel() != null && param.getModel().isDynamicallyTyped()) {
                    checkRefinedParameterDynamicallyTyped(member, refinedMember, rparam, param, typeNode);
                } else if (refinedParameterType == null || parameterType == null) {
                    handleUnknownParameterType(member, refinedMember, param, typeNode, forNative);
                } else {
                    checkRefiningParameterType(member, refinedMember, refinedParams, rparam, refinedParameterType, param, parameterType, typeNode, forNative);
                    checkRefiningParameterSmall(typeNode, member.getDeclaration(), param, refinedMember.getDeclaration(), rparam);
                }
            }
        }
    }
}
Also used : IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.erasedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.erasedType) AnalyzerUtil.getTypeErrorNode(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeErrorNode) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Example 15 with Node

use of org.eclipse.ceylon.compiler.typechecker.tree.Node in project ceylon by eclipse.

the class ExpressionVisitor method visit.

@Override
public void visit(Tree.IfExpression that) {
    Node ose = switchStatementOrExpression;
    Node oie = ifStatementOrExpression;
    switchStatementOrExpression = null;
    ifStatementOrExpression = that;
    super.visit(that);
    List<Type> list = new ArrayList<Type>();
    Tree.IfClause ifClause = that.getIfClause();
    if (ifClause != null && ifClause.getExpression() != null) {
        Type t = ifClause.getExpression().getTypeModel();
        if (t != null) {
            addToUnion(list, t);
        }
    } else {
        that.addError("missing then expression");
    }
    Tree.ElseClause elseClause = that.getElseClause();
    if (elseClause != null && elseClause.getExpression() != null) {
        Type t = elseClause.getExpression().getTypeModel();
        if (t != null) {
            addToUnion(list, t);
        }
    } else {
        that.addError("missing else expression");
    }
    that.setTypeModel(union(list, unit));
    switchStatementOrExpression = ose;
    ifStatementOrExpression = oie;
}
Also used : ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) ModelUtil.unionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType) AnalyzerUtil.spreadType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType) AnalyzerUtil.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) ModelUtil.genericFunctionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType) Node(org.eclipse.ceylon.compiler.typechecker.tree.Node) ArrayList(java.util.ArrayList) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Aggregations

Node (org.eclipse.ceylon.compiler.typechecker.tree.Node)33 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)26 Type (org.eclipse.ceylon.model.typechecker.model.Type)16 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)14 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)12 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)11 AnalyzerUtil.getTupleType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType)10 AnalyzerUtil.spreadType (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType)10 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)10 ModelUtil.genericFunctionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType)10 ModelUtil.unionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType)10 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)10 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)10 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)9 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)9 AnalyzerUtil.getTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration)7 AnalyzerUtil.getPackageTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration)6 AnalyzerUtil.getPackageTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypedDeclaration)6 AnalyzerUtil.getTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration)6 AnalyzerUtil.checkCasesDisjoint (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.checkCasesDisjoint)5