Search in sources :

Example 6 with Node

use of com.redhat.ceylon.compiler.typechecker.tree.Node in project ceylon-compiler by ceylon.

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 {
            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 = Decl.getDefaultConstructor((Class) expr.getDeclaration());
        }
        JCExpression metamodelCall = makeTypeDeclarationLiteral(Decl.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 (Decl.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(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) Type(com.redhat.ceylon.model.typechecker.model.Type) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTypeCast(com.sun.tools.javac.tree.JCTree.JCTypeCast) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) Node(com.redhat.ceylon.compiler.typechecker.tree.Node) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 7 with Node

use of com.redhat.ceylon.compiler.typechecker.tree.Node in project ceylon-compiler by ceylon.

the class CeylonDocTool method warningMissingThrows.

protected void warningMissingThrows(Declaration d) {
    if (ignoreMissingThrows) {
        return;
    }
    final Scope scope = d.getScope();
    final PhasedUnit unit = getUnit(d);
    final Node node = getNode(d);
    if (scope == null || unit == null || unit.getUnit() == null || node == null || !(d instanceof FunctionOrValue)) {
        return;
    }
    List<Type> documentedExceptions = new ArrayList<Type>();
    for (Annotation annotation : d.getAnnotations()) {
        if (annotation.getName().equals("throws")) {
            String exceptionName = annotation.getPositionalArguments().get(0);
            Declaration exceptionDecl = scope.getMemberOrParameter(unit.getUnit(), exceptionName, null, false);
            if (exceptionDecl instanceof TypeDeclaration) {
                documentedExceptions.add(((TypeDeclaration) exceptionDecl).getType());
            }
        }
    }
    final List<Type> thrownExceptions = new ArrayList<Type>();
    node.visitChildren(new Visitor() {

        @Override
        public void visit(Tree.Throw that) {
            Expression expression = that.getExpression();
            if (expression != null) {
                thrownExceptions.add(expression.getTypeModel());
            } else {
                thrownExceptions.add(unit.getUnit().getExceptionType());
            }
        }

        @Override
        public void visit(Tree.Declaration that) {
        // the end of searching
        }
    });
    for (Type thrownException : thrownExceptions) {
        boolean isDocumented = false;
        for (Type documentedException : documentedExceptions) {
            if (thrownException.isSubtypeOf(documentedException)) {
                isDocumented = true;
                break;
            }
        }
        if (!isDocumented) {
            log.warning(CeylondMessages.msg("warn.missingThrows", thrownException.asString(), getWhere(d), getPosition(getNode(d))));
        }
    }
}
Also used : Visitor(com.redhat.ceylon.compiler.typechecker.tree.Visitor) SourceDeclarationVisitor(com.redhat.ceylon.compiler.java.loader.SourceDeclarationVisitor) Node(com.redhat.ceylon.compiler.typechecker.tree.Node) ArrayList(java.util.ArrayList) Annotation(com.redhat.ceylon.model.typechecker.model.Annotation) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) NothingType(com.redhat.ceylon.model.typechecker.model.NothingType) Type(com.redhat.ceylon.model.typechecker.model.Type) Scope(com.redhat.ceylon.model.typechecker.model.Scope) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue)

Example 8 with Node

use of com.redhat.ceylon.compiler.typechecker.tree.Node in project ceylon-compiler by ceylon.

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(com.redhat.ceylon.compiler.typechecker.tree.Node) LinkedList(java.util.LinkedList)

Example 9 with Node

use of com.redhat.ceylon.compiler.typechecker.tree.Node in project ceylon-compiler by ceylon.

the class BugException method addError.

/**
     * Add an error to the a node. A {@link CodeGenError} 
     * is created with the exception's message and the exception as cause, 
     * and is added to the assoicated node (if there is one), and otherwise 
     * the given node.
     * @param fallbackNode
     */
public void addError(Node fallbackNode) {
    if (!this.attached) {
        Node bestNode = bestNode(fallbackNode);
        bestNode.addError(new CodeGenError(bestNode, getMessage(), Backend.Java, this));
        this.attached = true;
    }
}
Also used : Node(com.redhat.ceylon.compiler.typechecker.tree.Node)

Aggregations

Node (com.redhat.ceylon.compiler.typechecker.tree.Node)9 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)7 PhasedUnit (com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)4 Visitor (com.redhat.ceylon.compiler.typechecker.tree.Visitor)3 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)3 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)3 Scope (com.redhat.ceylon.model.typechecker.model.Scope)2 Type (com.redhat.ceylon.model.typechecker.model.Type)2 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)2 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)2 ArrayList (java.util.ArrayList)2 Util.findBottomMostRefinedDeclaration (com.redhat.ceylon.ceylondoc.Util.findBottomMostRefinedDeclaration)1 SourceDeclarationVisitor (com.redhat.ceylon.compiler.java.loader.SourceDeclarationVisitor)1 Expression (com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression)1 Annotation (com.redhat.ceylon.model.typechecker.model.Annotation)1 Class (com.redhat.ceylon.model.typechecker.model.Class)1 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)1 Constructor (com.redhat.ceylon.model.typechecker.model.Constructor)1 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)1 Functional (com.redhat.ceylon.model.typechecker.model.Functional)1