Search in sources :

Example 1 with Lint

use of com.sun.tools.javac.code.Lint in project ceylon-compiler by ceylon.

the class Attr method visitLetExpr.

/* Added for Ceylon, required by the ExpressionTransformer */
@Override
public void visitLetExpr(LetExpr tree) {
    Scope scope;
    if (env.info.scope.owner.kind == Kinds.VAR) {
        // for field initialisers we must access the parent scope, otherwise we get
        // a DelegateScope which doesn't allow us to declare new variables
        scope = env.info.scope.next;
    } else
        scope = env.info.scope;
    if (scope instanceof DelegatedScope) {
        throw new AssertionError("Broken use of LetExpr");
    }
    // make a new environment which captures the current one
    Env<AttrContext> localEnv = env.dup(tree, env.info.dup(scope.dup()));
    // Field initialisers have not been entered yet, so we need to do it now:
    if (localEnv.info.scope.owner.kind == Kinds.TYP) {
        memberEnter.memberEnter(tree, localEnv);
        annotate.flush();
    }
    // find the lint
    Env<AttrContext> lintEnv = env;
    while (lintEnv.info.lint == null) lintEnv = lintEnv.next;
    // Having found the enclosing lint value, we can initialize the lint value for this let
    localEnv.info.lint = lintEnv.info.lint;
    Lint prevLint = chk.setLint(env.info.lint);
    try {
        // do statements if we have any
        if (tree.stats != null) {
            if (sourceLanguage.isCeylon())
                completeLocalTypes(tree.stats, localEnv);
            attribStats(tree.stats, localEnv);
        }
        // now attribute the expression with the LET expected type (pt)
        attribExpr(tree.expr, localEnv, pt);
        // the type of the LET is the type of the expression
        tree.type = tree.expr.type;
        // same code in TransTypes.visit(LetExpr)
        if (tree.type.constValue() != null && tree.stats != null && !tree.stats.isEmpty())
            tree.type = tree.type.baseType();
    } finally {
        // exit the LET scope
        localEnv.info.scope.leave();
        // restore the previous lint
        chk.setLint(prevLint);
    }
}
Also used : DelegatedScope(com.sun.tools.javac.code.Scope.DelegatedScope) Scope(com.sun.tools.javac.code.Scope) DelegatedScope(com.sun.tools.javac.code.Scope.DelegatedScope) Lint(com.sun.tools.javac.code.Lint)

Example 2 with Lint

use of com.sun.tools.javac.code.Lint in project ceylon-compiler by ceylon.

the class Attr method attribLazyConstantValue.

/**
 * Attribute a "lazy constant value".
 *  @param env         The env for the const value
 *  @param initializer The initializer for the const value
 *  @param type        The expected type, or null
 *  @see VarSymbol#setlazyConstValue
 */
public Object attribLazyConstantValue(Env<AttrContext> env, JCTree.JCExpression initializer, Type type) {
    // in case no lint value has been set up for this env, scan up
    // env stack looking for smallest enclosing env for which it is set.
    Env<AttrContext> lintEnv = env;
    while (lintEnv.info.lint == null) lintEnv = lintEnv.next;
    // Having found the enclosing lint value, we can initialize the lint value for this class
    env.info.lint = lintEnv.info.lint.augment(env.info.enclVar.attributes_field, env.info.enclVar.flags());
    Lint prevLint = chk.setLint(env.info.lint);
    JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile);
    try {
        Type itype = attribExpr(initializer, env, type);
        if (itype.constValue() != null)
            return coerce(itype, type).constValue();
        else
            return null;
    } finally {
        env.info.lint = prevLint;
        log.useSource(prevSource);
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) Lint(com.sun.tools.javac.code.Lint)

Example 3 with Lint

use of com.sun.tools.javac.code.Lint in project ceylon-compiler by ceylon.

the class Attr method visitVarDef.

public void visitVarDef(JCVariableDecl tree) {
    // Local variables have not been entered yet, so we need to do it now:
    if (env.info.scope.owner.kind == MTH) {
        if (tree.sym != null) {
            // parameters have already been entered
            env.info.scope.enter(tree.sym);
        } else {
            memberEnter.memberEnter(tree, env);
            annotate.flush();
        }
        tree.sym.flags_field |= EFFECTIVELY_FINAL;
    }
    VarSymbol v = tree.sym;
    Lint lint = env.info.lint.augment(v.attributes_field, v.flags());
    Lint prevLint = chk.setLint(lint);
    // Check that the variable's declared type is well-formed.
    chk.validate(tree.vartype, env);
    deferredLintHandler.flush(tree.pos());
    try {
        chk.checkDeprecatedAnnotation(tree.pos(), v);
        if (tree.init != null) {
            if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS) {
                // In this case, `v' is final.  Ensure that it's initializer is
                // evaluated.
                // ensure initializer is evaluated
                v.getConstValue();
            } else {
                // Attribute initializer in a new environment
                // with the declared variable as owner.
                // Check that initializer conforms to variable's declared type.
                Env<AttrContext> initEnv = memberEnter.initEnv(tree, env);
                initEnv.info.lint = lint;
                // In order to catch self-references, we set the variable's
                // declaration position to maximal possible value, effectively
                // marking the variable as undefined.
                initEnv.info.enclVar = v;
                attribExpr(tree.init, initEnv, v.type);
            }
        }
        result = tree.type = v.type;
        chk.validateAnnotations(tree.mods.annotations, v);
    } finally {
        chk.setLint(prevLint);
    }
}
Also used : Lint(com.sun.tools.javac.code.Lint) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 4 with Lint

use of com.sun.tools.javac.code.Lint in project ceylon-compiler by ceylon.

the class Attr method visitMethodDef.

public void visitMethodDef(JCMethodDecl tree) {
    MethodSymbol m = tree.sym;
    Lint lint = env.info.lint.augment(m.attributes_field, m.flags());
    Lint prevLint = chk.setLint(lint);
    MethodSymbol prevMethod = chk.setMethod(m);
    try {
        deferredLintHandler.flush(tree.pos());
        chk.checkDeprecatedAnnotation(tree.pos(), m);
        attribBounds(tree.typarams);
        // JLS ???
        if (m.isStatic()) {
            chk.checkHideClashes(tree.pos(), env.enclClass.type, m);
        } else {
            chk.checkOverrideClashes(tree.pos(), env.enclClass.type, m);
        }
        chk.checkOverride(tree, m);
        // Create a new environment with local scope
        // for attributing the method.
        Env<AttrContext> localEnv = memberEnter.methodEnv(tree, env);
        localEnv.info.lint = lint;
        // Enter all type parameters into the local method scope.
        for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) localEnv.info.scope.enterIfAbsent(l.head.type.tsym);
        ClassSymbol owner = env.enclClass.sym;
        if ((owner.flags() & ANNOTATION) != 0 && tree.params.nonEmpty())
            log.error(tree.params.head.pos(), "intf.annotation.members.cant.have.params");
        // Attribute all value parameters.
        for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
            attribStat(l.head, localEnv);
        }
        chk.checkVarargsMethodDecl(localEnv, tree);
        // Check that type parameters are well-formed.
        chk.validate(tree.typarams, localEnv);
        // Check that result type is well-formed.
        chk.validate(tree.restype, localEnv);
        // annotation method checks
        if ((owner.flags() & ANNOTATION) != 0) {
            // annotation method cannot have throws clause
            if (tree.thrown.nonEmpty()) {
                log.error(tree.thrown.head.pos(), "throws.not.allowed.in.intf.annotation");
            }
            // annotation method cannot declare type-parameters
            if (tree.typarams.nonEmpty()) {
                log.error(tree.typarams.head.pos(), "intf.annotation.members.cant.have.type.params");
            }
            // validate annotation method's return type (could be an annotation type)
            chk.validateAnnotationType(tree.restype);
            // ensure that annotation method does not clash with members of Object/Annotation
            chk.validateAnnotationMethod(tree.pos(), m);
            if (tree.defaultValue != null) {
                // if default value is an annotation, check it is a well-formed
                // annotation value (e.g. no duplicate values, no missing values, etc.)
                chk.validateAnnotationTree(tree.defaultValue);
            }
        }
        for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail) chk.checkType(l.head.pos(), l.head.type, syms.throwableType);
        if (tree.body == null) {
            // in a retrofit signature class.
            if ((owner.flags() & INTERFACE) == 0 && (tree.mods.flags & (ABSTRACT | NATIVE)) == 0 && !relax)
                log.error(tree.pos(), "missing.meth.body.or.decl.abstract");
            if (tree.defaultValue != null) {
                if ((owner.flags() & ANNOTATION) == 0)
                    log.error(tree.pos(), "default.allowed.in.intf.annotation.member");
            }
        } else if ((owner.flags() & INTERFACE) != 0) {
            log.error(tree.body.pos(), "intf.meth.cant.have.body");
        } else if ((tree.mods.flags & ABSTRACT) != 0) {
            log.error(tree.pos(), "abstract.meth.cant.have.body");
        } else if ((tree.mods.flags & NATIVE) != 0) {
            log.error(tree.pos(), "native.meth.cant.have.body");
        } else {
            // or we are compiling class java.lang.Object.
            if (tree.name == names.init && owner.type != syms.objectType) {
                JCBlock body = tree.body;
                if (body.stats.isEmpty() || !TreeInfo.isSelfCall(names, body.stats.head)) {
                    body.stats = body.stats.prepend(memberEnter.SuperCall(make.at(body.pos), List.<Type>nil(), List.<JCVariableDecl>nil(), false));
                } else if ((env.enclClass.sym.flags() & ENUM) != 0 && (tree.mods.flags & GENERATEDCONSTR) == 0 && TreeInfo.isSuperCall(names, body.stats.head)) {
                    // enum constructors are not allowed to call super
                    // directly, so make sure there aren't any super calls
                    // in enum constructors, except in the compiler
                    // generated one.
                    log.error(tree.body.stats.head.pos(), "call.to.super.not.allowed.in.enum.ctor", env.enclClass.sym);
                }
            }
            // Attribute method body.
            attribStat(tree.body, localEnv);
        }
        localEnv.info.scope.leave();
        result = tree.type = m.type;
        chk.validateAnnotations(tree.mods.annotations, m);
    } finally {
        chk.setLint(prevLint);
        chk.setMethod(prevMethod);
    }
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Lint(com.sun.tools.javac.code.Lint) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) DynamicMethodSymbol(com.sun.tools.javac.code.Symbol.DynamicMethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol)

Example 5 with Lint

use of com.sun.tools.javac.code.Lint in project ceylon-compiler by ceylon.

the class Attr method attribClass.

/**
 * Attribute class definition associated with given class symbol.
 *  @param c   The class symbol whose definition will be attributed.
 */
void attribClass(ClassSymbol c) throws CompletionFailure {
    if (c.type.tag == ERROR)
        return;
    // Check for cycles in the inheritance graph, which can arise from
    // ill-formed class files.
    chk.checkNonCyclic(null, c.type);
    Type st = types.supertype(c.type);
    if ((c.flags_field & Flags.COMPOUND) == 0) {
        // First, attribute superclass.
        if (st.tag == CLASS)
            attribClass((ClassSymbol) st.tsym);
        // Next attribute owner, if it is a class.
        if (c.owner.kind == TYP && c.owner.type.tag == CLASS)
            attribClass((ClassSymbol) c.owner);
    }
    // UNATTRIBUTED.
    if ((c.flags_field & UNATTRIBUTED) != 0) {
        c.flags_field &= ~UNATTRIBUTED;
        // Get environment current at the point of class definition.
        Env<AttrContext> env = enter.typeEnvs.get(c);
        // The info.lint field in the envs stored in enter.typeEnvs is deliberately uninitialized,
        // because the annotations were not available at the time the env was created. Therefore,
        // we look up the environment chain for the first enclosing environment for which the
        // lint value is set. Typically, this is the parent env, but might be further if there
        // are any envs created as a result of TypeParameter nodes.
        Env<AttrContext> lintEnv = env;
        while (lintEnv.info.lint == null) lintEnv = lintEnv.next;
        // Having found the enclosing lint value, we can initialize the lint value for this class
        env.info.lint = lintEnv.info.lint.augment(c.attributes_field, c.flags());
        Lint prevLint = chk.setLint(env.info.lint);
        JavaFileObject prev = log.useSource(c.sourcefile);
        try {
            // java.lang.Enum may not be subclassed by a non-enum
            if (st.tsym == syms.enumSym && ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0))
                log.error(env.tree.pos(), "enum.no.subclassing");
            // Enums may not be extended by source-level classes
            if (st.tsym != null && ((st.tsym.flags_field & Flags.ENUM) != 0) && ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0) && !target.compilerBootstrap(c)) {
                log.error(env.tree.pos(), "enum.types.not.extensible");
            }
            attribClassBody(env, c);
            chk.checkDeprecatedAnnotation(env.tree.pos(), c);
        } finally {
            log.useSource(prev);
            chk.setLint(prevLint);
        }
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Lint(com.sun.tools.javac.code.Lint)

Aggregations

Lint (com.sun.tools.javac.code.Lint)7 Type (com.sun.tools.javac.code.Type)3 ArrayType (com.sun.tools.javac.code.Type.ArrayType)3 ClassType (com.sun.tools.javac.code.Type.ClassType)3 MethodType (com.sun.tools.javac.code.Type.MethodType)3 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)3 WildcardType (com.sun.tools.javac.code.Type.WildcardType)3 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)2 JavaFileObject (javax.tools.JavaFileObject)2 Scope (com.sun.tools.javac.code.Scope)1 DelegatedScope (com.sun.tools.javac.code.Scope.DelegatedScope)1 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)1 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)1 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)1 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)1 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)1 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)1 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)1 Location (javax.tools.JavaFileManager.Location)1