attr.java

来自「是一款用JAVA 编写的编译器 具有很强的编译功能」· Java 代码 · 共 1,568 行 · 第 1/5 页

JAVA
1,568 行
字号
                    boolean selectSuperPrev = localEnv.info.selectSuper;                    localEnv.info.selectSuper = true;                    localEnv.info.varArgs = false;                    Symbol sym = rs.resolveConstructor(                        tree.meth.pos(), localEnv, site, argtypes, typeargtypes);                    localEnv.info.selectSuper = selectSuperPrev;                    // Set method symbol to resolved constructor...                    TreeInfo.setSymbol(tree.meth, sym);                    // ...and check that it is legal in the current context.                    // (this will also set the tree's type)                    Type mpt = newMethTemplate(argtypes, typeargtypes);                    checkId(tree.meth, site, sym, localEnv, MTH,                            mpt, tree.varargsElement != null);                }                // Otherwise, `site' is an error type and we do nothing            }            result = tree.type = syms.voidType;        } else {            // Otherwise, we are seeing a regular method call.            // Attribute the arguments, yielding list of argument types, ...            argtypes = attribArgs(tree.args, localEnv);            typeargtypes = attribTypes(tree.typeargs, localEnv);            // ... and attribute the method using as a prototype a methodtype            // whose formal argument types is exactly the list of actual            // arguments (this will also set the method symbol).            Type mpt = newMethTemplate(argtypes, typeargtypes);            localEnv.info.varArgs = false;            Type mtype = attribExpr(tree.meth, localEnv, mpt);            if (localEnv.info.varArgs)                assert mtype.isErroneous() || tree.varargsElement != null;            // Compute the result type.            Type restype = mtype.getReturnType();            assert restype.tag != WILDCARD : mtype;            // as a special case, array.clone() has a result that is            // the same as static type of the array being cloned            if (tree.meth.getTag() == JCTree.SELECT &&                allowCovariantReturns &&                methName == names.clone &&                types.isArray(((JCFieldAccess) tree.meth).selected.type))                restype = ((JCFieldAccess) tree.meth).selected.type;            // as a special case, x.getClass() has type Class<? extends |X|>            if (allowGenerics &&                methName == names.getClass && tree.args.isEmpty()) {                Type qualifier = (tree.meth.getTag() == JCTree.SELECT)                    ? ((JCFieldAccess) tree.meth).selected.type                    : env.enclClass.sym.type;                restype = new                    ClassType(restype.getEnclosingType(),                              List.<Type>of(new WildcardType(types.erasure(qualifier),                                                               BoundKind.EXTENDS,                                                               syms.boundClass)),                              restype.tsym);            }            // Check that value of resulting type is admissible in the            // current context.  Also, capture the return type            result = check(tree, capture(restype), VAL, pkind, pt);        }        chk.validate(tree.typeargs);    }    //where        /** Check that given application node appears as first statement         *  in a constructor call.         *  @param tree   The application node         *  @param env    The environment current at the application.         */        boolean checkFirstConstructorStat(JCMethodInvocation tree, Env<AttrContext> env) {            JCMethodDecl enclMethod = env.enclMethod;            if (enclMethod != null && enclMethod.name == names.init) {                JCBlock body = enclMethod.body;                if (body.stats.head.getTag() == JCTree.EXEC &&                    ((JCExpressionStatement) body.stats.head).expr == tree)                    return true;            }            log.error(tree.pos(),"call.must.be.first.stmt.in.ctor",                      TreeInfo.name(tree.meth));            return false;        }        /** Obtain a method type with given argument types.         */        Type newMethTemplate(List<Type> argtypes, List<Type> typeargtypes) {            MethodType mt = new MethodType(argtypes, null, null, syms.methodClass);            return (typeargtypes == null) ? mt : (Type)new ForAll(typeargtypes, mt);        }    public void visitNewClass(JCNewClass tree) {        Type owntype = syms.errType;        // The local environment of a class creation is        // a new environment nested in the current one.        Env<AttrContext> localEnv = env.dup(tree, env.info.dup());        // The anonymous inner class definition of the new expression,        // if one is defined by it.        JCClassDecl cdef = tree.def;        // If enclosing class is given, attribute it, and        // complete class name to be fully qualified        JCExpression clazz = tree.clazz; // Class field following new        JCExpression clazzid =          // Identifier in class field            (clazz.getTag() == JCTree.TYPEAPPLY)            ? ((JCTypeApply) clazz).clazz            : clazz;        JCExpression clazzid1 = clazzid; // The same in fully qualified form        if (tree.encl != null) {            // We are seeing a qualified new, of the form            //    <expr>.new C <...> (...) ...            // In this case, we let clazz stand for the name of the            // allocated class C prefixed with the type of the qualifier            // expression, so that we can            // resolve it with standard techniques later. I.e., if            // <expr> has type T, then <expr>.new C <...> (...)            // yields a clazz T.C.            Type encltype = chk.checkRefType(tree.encl.pos(),                                             attribExpr(tree.encl, env));            clazzid1 = make.at(clazz.pos).Select(make.Type(encltype),                                                 ((JCIdent) clazzid).name);            if (clazz.getTag() == JCTree.TYPEAPPLY)                clazz = make.at(tree.pos).                    TypeApply(clazzid1,                              ((JCTypeApply) clazz).arguments);            else                clazz = clazzid1;//          System.out.println(clazz + " generated.");//DEBUG        }        // Attribute clazz expression and store        // symbol + type back into the attributed tree.        Type clazztype = chk.checkClassType(            tree.clazz.pos(), attribType(clazz, env), true);        chk.validate(clazz);        if (tree.encl != null) {            // We have to work in this case to store            // symbol + type back into the attributed tree.            tree.clazz.type = clazztype;            TreeInfo.setSymbol(clazzid, TreeInfo.symbol(clazzid1));            clazzid.type = ((JCIdent) clazzid).sym.type;            if (!clazztype.isErroneous()) {                if (cdef != null && clazztype.tsym.isInterface()) {                    log.error(tree.encl.pos(), "anon.class.impl.intf.no.qual.for.new");                } else if (clazztype.tsym.isStatic()) {                    log.error(tree.encl.pos(), "qualified.new.of.static.class", clazztype.tsym);                }            }        } else if (!clazztype.tsym.isInterface() &&                   clazztype.getEnclosingType().tag == CLASS) {            // Check for the existence of an apropos outer instance            rs.resolveImplicitThis(tree.pos(), env, clazztype);        }        // Attribute constructor arguments.        List<Type> argtypes = attribArgs(tree.args, localEnv);        List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv);        // If we have made no mistakes in the class type...        if (clazztype.tag == CLASS) {            // Enums may not be instantiated except implicitly            if (allowEnums &&                (clazztype.tsym.flags_field&Flags.ENUM) != 0 &&                (env.tree.getTag() != JCTree.VARDEF ||                 (((JCVariableDecl) env.tree).mods.flags&Flags.ENUM) == 0 ||                 ((JCVariableDecl) env.tree).init != tree))                log.error(tree.pos(), "enum.cant.be.instantiated");            // Check that class is not abstract            if (cdef == null &&                (clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {                log.error(tree.pos(), "abstract.cant.be.instantiated",                          clazztype.tsym);            } else if (cdef != null && clazztype.tsym.isInterface()) {                // Check that no constructor arguments are given to                // anonymous classes implementing an interface                if (!argtypes.isEmpty())                    log.error(tree.args.head.pos(), "anon.class.impl.intf.no.args");                if (!typeargtypes.isEmpty())                    log.error(tree.typeargs.head.pos(), "anon.class.impl.intf.no.typeargs");                // Error recovery: pretend no arguments were supplied.                argtypes = List.nil();                typeargtypes = List.nil();            }            // Resolve the called constructor under the assumption            // that we are referring to a superclass instance of the            // current instance (JLS ???).            else {                localEnv.info.selectSuper = cdef != null;                localEnv.info.varArgs = false;                tree.constructor = rs.resolveConstructor(                    tree.pos(), localEnv, clazztype, argtypes, typeargtypes);                Type ctorType = checkMethod(clazztype,                                            tree.constructor,                                            localEnv,                                            tree.args,                                            argtypes,                                            typeargtypes,                                            localEnv.info.varArgs);                if (localEnv.info.varArgs)                    assert ctorType.isErroneous() || tree.varargsElement != null;            }            if (cdef != null) {                // We are seeing an anonymous class instance creation.                // In this case, the class instance creation                // expression                //                //    E.new <typeargs1>C<typargs2>(args) { ... }                //                // is represented internally as                //                //    E . new <typeargs1>C<typargs2>(args) ( class <empty-name> { ... } )  .                //                // This expression is then *transformed* as follows:                //                // (1) add a STATIC flag to the class definition                //     if the current environment is static                // (2) add an extends or implements clause                // (3) add a constructor.                //                // For instance, if C is a class, and ET is the type of E,                // the expression                //                //    E.new <typeargs1>C<typargs2>(args) { ... }                //                // is translated to (where X is a fresh name and typarams is the                // parameter list of the super constructor):                //                //   new <typeargs1>X(<*nullchk*>E, args) where                //     X extends C<typargs2> {                //       <typarams> X(ET e, args) {                //         e.<typeargs1>super(args)                //       }                //       ...                //     }                if (Resolve.isStatic(env)) cdef.mods.flags |= STATIC;                if (clazztype.tsym.isInterface()) {                    cdef.implementing = List.of(clazz);                } else {                    cdef.extending = clazz;                }                attribStat(cdef, localEnv);                // If an outer instance is given,                // prefix it to the constructor arguments                // and delete it from the new expression                if (tree.encl != null && !clazztype.tsym.isInterface()) {                    tree.args = tree.args.prepend(makeNullCheck(tree.encl));                    argtypes = argtypes.prepend(tree.encl.type);                    tree.encl = null;                }                // Reassign clazztype and recompute constructor.                clazztype = cdef.sym.type;                Symbol sym = rs.resolveConstructor(                    tree.pos(), localEnv, clazztype, argtypes,                    typeargtypes, true, tree.varargsElement != null);                assert sym.kind < AMBIGUOUS || tree.constructor.type.isErroneous();                tree.constructor = sym;            }            if (tree.constructor != null && tree.constructor.kind == MTH)                owntype = clazztype;        }        result = check(tree, owntype, VAL, pkind, pt);        chk.validate(tree.typeargs);    }    /** Make an attributed null check tree.     */    public JCExpression makeNullCheck(JCExpression arg) {        // optimization: X.this is never null; skip null check        Name name = TreeInfo.name(arg);        if (name == names._this || name == names._super) return arg;        int optag = JCTree.NULLCHK;        JCUnary tree = make.at(arg.pos).Unary(optag, arg);        tree.operator = syms.nullcheck;        tree.type = arg.type;        return tree;    }    public void visitNewArray(JCNewArray tree) {        Type owntype = syms.errType;        Type elemtype;        if (tree.elemtype != null) {            elemtype = attribType(tree.elemtype, env);            chk.validate(tree.elemtype);            owntype = elemtype;            for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) {                attribExpr(l.head, env, syms.intType);                owntype = new ArrayType(owntype, syms.arrayClass);            }        } else {            // we are seeing an untyped aggregate { ... }            // this is allowed only if the prototype is an array            if (pt.tag == ARRAY) {                elemtype = types.elemtype(pt);            } else {                if (pt.tag != ERROR) {                    log.error(tree.pos(), "illegal.initializer.for.type",                              p

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?