Search in sources :

Example 21 with Scope

use of com.redhat.ceylon.model.typechecker.model.Scope 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 22 with Scope

use of com.redhat.ceylon.model.typechecker.model.Scope in project ceylon-compiler by ceylon.

the class LinkRenderer method isInCurrentModule.

private boolean isInCurrentModule(Object obj) {
    Module objModule = null;
    if (obj instanceof Module) {
        objModule = (Module) obj;
    } else if (obj instanceof Scope) {
        objModule = getPackage((Scope) obj).getModule();
    } else if (obj instanceof Element) {
        objModule = getPackage(((Element) obj).getScope()).getModule();
    }
    Module currentModule = ceylonDocTool.getCurrentModule();
    if (currentModule != null && objModule != null) {
        return currentModule.equals(objModule);
    }
    return false;
}
Also used : Scope(com.redhat.ceylon.model.typechecker.model.Scope) Element(com.redhat.ceylon.model.typechecker.model.Element) Module(com.redhat.ceylon.model.typechecker.model.Module)

Example 23 with Scope

use of com.redhat.ceylon.model.typechecker.model.Scope in project ceylon-compiler by ceylon.

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);
    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(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) ConditionList(com.redhat.ceylon.compiler.typechecker.tree.Tree.ConditionList) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Substitution(com.redhat.ceylon.compiler.java.codegen.Naming.Substitution) ConditionScope(com.redhat.ceylon.model.typechecker.model.ConditionScope) Scope(com.redhat.ceylon.model.typechecker.model.Scope) ConditionScope(com.redhat.ceylon.model.typechecker.model.ConditionScope) Condition(com.redhat.ceylon.compiler.typechecker.tree.Tree.Condition) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) Value(com.redhat.ceylon.model.typechecker.model.Value) CustomTree(com.redhat.ceylon.compiler.typechecker.tree.CustomTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) BoxingStrategy(com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy)

Example 24 with Scope

use of com.redhat.ceylon.model.typechecker.model.Scope in project ceylon-compiler by ceylon.

the class Naming method makeTypeDeclaration.

private <R> R makeTypeDeclaration(TypeDeclarationBuilder<R> declarationBuilder, final TypeDeclaration decl, DeclNameFlag... options) {
    EnumSet<DeclNameFlag> flags = EnumSet.noneOf(DeclNameFlag.class);
    flags.addAll(Arrays.asList(options));
    if (flags.contains(DeclNameFlag.ANNOTATION) && !Decl.isAnnotationClass(decl)) {
        throw new BugException(decl.getName());
    }
    if (flags.contains(DeclNameFlag.ANNOTATIONS) && !(Decl.isAnnotationClass(decl) || decl instanceof Class || gen().isSequencedAnnotation((Class) decl))) {
        throw new BugException(decl.getName());
    }
    java.util.List<Scope> l = new java.util.ArrayList<Scope>();
    Scope s = decl;
    do {
        l.add(s);
        s = s.getContainer();
    } while (!(s instanceof Package));
    Collections.reverse(l);
    if (flags.contains(DeclNameFlag.QUALIFIED) && (!decl.isAnonymous() || decl.isNamed())) {
        final List<String> packageName;
        if (!AbstractTransformer.isJavaArray(decl))
            packageName = ((Package) s).getName();
        else
            packageName = COM_REDHAT_CEYLON_LANGUAGE_PACKAGE;
        if (packageName.isEmpty() || !packageName.get(0).isEmpty()) {
            declarationBuilder.select("");
        }
        for (int ii = 0; ii < packageName.size(); ii++) {
            declarationBuilder.select(quoteIfJavaKeyword(packageName.get(ii)));
        }
    }
    for (int ii = 0; ii < l.size(); ii++) {
        Scope scope = l.get(ii);
        final boolean last = ii == l.size() - 1;
        appendTypeDeclaration(decl, flags, declarationBuilder, scope, last);
    }
    return declarationBuilder.result();
}
Also used : ArrayList(java.util.ArrayList) Scope(com.redhat.ceylon.model.typechecker.model.Scope) Class(com.redhat.ceylon.model.typechecker.model.Class) LazyClass(com.redhat.ceylon.model.loader.model.LazyClass) Package(com.redhat.ceylon.model.typechecker.model.Package)

Example 25 with Scope

use of com.redhat.ceylon.model.typechecker.model.Scope in project ceylon-compiler by ceylon.

the class Naming method getTypeArgumentDescriptorName.

public String getTypeArgumentDescriptorName(TypeParameter tp) {
    String name;
    if (tp.isCaptured()) {
        // must build unique name
        StringBuilder sb = new StringBuilder();
        LinkedList<Declaration> decls = new LinkedList<Declaration>();
        Scope scope = tp;
        while (scope != null && scope instanceof Package == false) {
            if (scope instanceof Declaration)
                decls.add((Declaration) scope);
            scope = scope.getContainer();
        }
        Iterator<Declaration> iterator = decls.descendingIterator();
        while (iterator.hasNext()) {
            sb.append(iterator.next().getName());
            if (iterator.hasNext())
                sb.append("$");
        }
        name = sb.toString();
    } else {
        name = tp.getName();
    }
    return prefixName(Prefix.$reified$, name);
}
Also used : Scope(com.redhat.ceylon.model.typechecker.model.Scope) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Package(com.redhat.ceylon.model.typechecker.model.Package) LinkedList(java.util.LinkedList)

Aggregations

Scope (com.redhat.ceylon.model.typechecker.model.Scope)38 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)26 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)22 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)20 Package (com.redhat.ceylon.model.typechecker.model.Package)16 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)14 Interface (com.redhat.ceylon.model.typechecker.model.Interface)10 Class (com.redhat.ceylon.model.typechecker.model.Class)8 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)8 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)7 Type (com.redhat.ceylon.model.typechecker.model.Type)7 ArrayList (java.util.ArrayList)7 Function (com.redhat.ceylon.model.typechecker.model.Function)6 Constructor (com.redhat.ceylon.model.typechecker.model.Constructor)5 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)5 Value (com.redhat.ceylon.model.typechecker.model.Value)5 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)5 JCTree (com.sun.tools.javac.tree.JCTree)4 Generic (com.redhat.ceylon.model.typechecker.model.Generic)3 ParameterList (com.redhat.ceylon.model.typechecker.model.ParameterList)3