Exemplo n.º 1
0
  @Override
  public void visitTopLevel(JCCompilationUnit tree) {
    JavaFileObject prev = log.useSource(tree.sourcefile);
    boolean addEnv = false;
    boolean isPkgInfo =
        tree.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
    if (tree.pid != null) {
      tree.packge = reader.enterPackage(TreeInfo.fullName(tree.pid));
      if (tree.packageAnnotations.nonEmpty() || pkginfoOpt == PkgInfo.ALWAYS) {
        if (isPkgInfo) {
          addEnv = true;
        } else {
          log.error(tree.packageAnnotations.head.pos(), "pkg.annotations.sb.in.package-info.java");
        }
      }
    } else {
      tree.packge = syms.unnamedPackage;
    }
    tree.packge.complete(); // Find all classes in package.
    Env<AttrContext> topEnv = topLevelEnv(tree);

    // Save environment of package-info.java file.
    if (isPkgInfo) {
      Env<AttrContext> env0 = typeEnvs.get(tree.packge);
      if (env0 == null) {
        typeEnvs.put(tree.packge, topEnv);
      } else {
        JCCompilationUnit tree0 = env0.toplevel;
        if (!fileManager.isSameFile(tree.sourcefile, tree0.sourcefile)) {
          log.warning(
              tree.pid != null ? tree.pid.pos() : null, "pkg-info.already.seen", tree.packge);
          if (addEnv
              || (tree0.packageAnnotations.isEmpty()
                  && tree.docComments != null
                  && tree.docComments.get(tree) != null)) {
            typeEnvs.put(tree.packge, topEnv);
          }
        }
      }

      for (Symbol q = tree.packge; q != null && q.kind == PCK; q = q.owner) q.flags_field |= EXISTS;

      Name name = names.package_info;
      ClassSymbol c = reader.enterClass(name, tree.packge);
      c.flatname = names.fromString(tree.packge + "." + name);
      c.sourcefile = tree.sourcefile;
      c.completer = null;
      c.members_field = new Scope(c);
      tree.packge.package_info = c;
    }
    classEnter(tree.defs, topEnv);
    if (addEnv) {
      todo.append(topEnv);
    }
    log.useSource(prev);
    result = null;
  }
Exemplo n.º 2
0
  public void visitClassDef(JCClassDecl tree) {
    Symbol owner = env.info.scope.owner;
    Scope enclScope = enterScope(env);
    ClassSymbol c;
    if (owner.kind == PCK) {
      // We are seeing a toplevel class.
      PackageSymbol packge = (PackageSymbol) owner;
      for (Symbol q = packge; q != null && q.kind == PCK; q = q.owner) q.flags_field |= EXISTS;
      c = reader.enterClass(tree.name, packge);
      packge.members().enterIfAbsent(c);
      if ((tree.mods.flags & PUBLIC) != 0 && !classNameMatchesFileName(c, env)) {
        log.error(tree.pos(), "class.public.should.be.in.file", tree.name);
      }
    } else {
      if (!tree.name.isEmpty() && !chk.checkUniqueClassName(tree.pos(), tree.name, enclScope)) {
        result = null;
        return;
      }
      if (owner.kind == TYP) {
        // We are seeing a member class.
        c = reader.enterClass(tree.name, (TypeSymbol) owner);
        if ((owner.flags_field & INTERFACE) != 0) {
          tree.mods.flags |= PUBLIC | STATIC;
        }
      } else {
        // We are seeing a local class.
        c = reader.defineClass(tree.name, owner);
        c.flatname = chk.localClassName(c);
        if (!c.name.isEmpty()) chk.checkTransparentClass(tree.pos(), c, env.info.scope);
      }
    }
    tree.sym = c;

    // Enter class into `compiled' table and enclosing scope.
    if (chk.compiled.get(c.flatname) != null) {
      duplicateClass(tree.pos(), c);
      result = types.createErrorType(tree.name, (TypeSymbol) owner, Type.noType);
      tree.sym = (ClassSymbol) result.tsym;
      return;
    }
    chk.compiled.put(c.flatname, c);
    enclScope.enter(c);

    // Set up an environment for class block and store in `typeEnvs'
    // table, to be retrieved later in memberEnter and attribution.
    Env<AttrContext> localEnv = classEnv(tree, env);
    typeEnvs.put(c, localEnv);

    // Fill out class fields.
    c.completer = memberEnter;
    c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree);
    c.sourcefile = env.toplevel.sourcefile;
    c.members_field = new Scope(c);

    ClassType ct = (ClassType) c.type;
    if (owner.kind != PCK && (c.flags_field & STATIC) == 0) {
      // We are seeing a local or inner class.
      // Set outer_field of this class to closest enclosing class
      // which contains this class in a non-static context
      // (its "enclosing instance class"), provided such a class exists.
      Symbol owner1 = owner;
      while ((owner1.kind & (VAR | MTH)) != 0 && (owner1.flags_field & STATIC) == 0) {
        owner1 = owner1.owner;
      }
      if (owner1.kind == TYP) {
        ct.setEnclosingType(owner1.type);
      }
    }

    // Enter type parameters.
    ct.typarams_field = classEnter(tree.typarams, localEnv);

    // Add non-local class to uncompleted, to make sure it will be
    // completed later.
    if (!c.isLocal() && uncompleted != null) uncompleted.append(c);
    //      System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG

    // Recursively enter all member classes.
    classEnter(tree.defs, localEnv);

    result = c.type;
  }
Exemplo n.º 3
0
  /** Constructor; enters all predefined identifiers and operators into symbol table. */
  private Symtab(Context context) throws CompletionFailure {
    super();
    context.put(symtabKey, this);
    names = Name.Table.instance(context);
    byteType = new Type(TypeTags.BYTE, null);
    charType = new Type(TypeTags.CHAR, null);
    shortType = new Type(TypeTags.SHORT, null);
    intType = new Type(TypeTags.INT, null);
    longType = new Type(TypeTags.LONG, null);
    floatType = new Type(TypeTags.FLOAT, null);
    doubleType = new Type(TypeTags.DOUBLE, null);
    booleanType = new Type(TypeTags.BOOLEAN, null);
    voidType = new Type(TypeTags.VOID, null);
    botType = new Type(TypeTags.BOT, null);
    unknownType =
        new Type(TypeTags.UNKNOWN, null) {

          public boolean isSameType(Type that) {
            return true;
          }

          public boolean isSubType(Type that) {
            return false;
          }

          public boolean isSuperType(Type that) {
            return true;
          }
        };
    rootPackage = new PackageSymbol(names.empty, null);
    emptyPackage = new PackageSymbol(names.emptyPackage, rootPackage);
    noSymbol = new TypeSymbol(0, names.empty, Type.noType, rootPackage);
    noSymbol.kind = Kinds.NIL;
    errSymbol = new ClassSymbol(PUBLIC | STATIC, names.any, null, rootPackage);
    errType = new ErrorType(errSymbol);
    initType(byteType, "byte", "Byte");
    initType(shortType, "short", "Short");
    initType(charType, "char", "Character");
    initType(intType, "int", "Integer");
    initType(longType, "long", "Long");
    initType(floatType, "float", "Float");
    initType(doubleType, "double", "Double");
    initType(booleanType, "boolean", "Boolean");
    initType(voidType, "void", "Void");
    initType(botType, "<nulltype>");
    initType(errType, errSymbol);
    initType(unknownType, "<any?>");
    arrayClass = new ClassSymbol(PUBLIC, names.Array, noSymbol);
    methodClass = new ClassSymbol(PUBLIC, names.Method, noSymbol);
    predefClass = new ClassSymbol(PUBLIC, names.empty, rootPackage);
    Scope scope = new Scope(predefClass);
    predefClass.members_field = scope;
    scope.enter(byteType.tsym);
    scope.enter(shortType.tsym);
    scope.enter(charType.tsym);
    scope.enter(intType.tsym);
    scope.enter(longType.tsym);
    scope.enter(floatType.tsym);
    scope.enter(doubleType.tsym);
    scope.enter(booleanType.tsym);
    scope.enter(errType.tsym);
    classes.put(predefClass.fullname, predefClass);
    reader = ClassReader.instance(context);
    reader.init(this);
    objectType = enterClass("java.lang.Object");
    classType = enterClass("java.lang.Class");
    stringType = enterClass("java.lang.String");
    stringBufferType = enterClass("java.lang.StringBuffer");
    cloneableType = enterClass("java.lang.Cloneable");
    throwableType = enterClass("java.lang.Throwable");
    serializableType = enterClass("java.io.Serializable");
    errorType = enterClass("java.lang.Error");
    exceptionType = enterClass("java.lang.Exception");
    runtimeExceptionType = enterClass("java.lang.RuntimeException");
    classNotFoundExceptionType = enterClass("java.lang.ClassNotFoundException");
    noClassDefFoundErrorType = enterClass("java.lang.NoClassDefFoundError");
    assertionErrorType = enterClass("java.lang.AssertionError");
    classLoaderType = enterClass("java.lang.ClassLoader");
    ClassType arrayClassType = (ClassType) arrayClass.type;
    arrayClassType.supertype_field = objectType;
    arrayClassType.interfaces_field = List.make(cloneableType, serializableType);
    arrayClass.members_field = new Scope(arrayClass);
    lengthVar = new VarSymbol(PUBLIC | FINAL, names.length, intType, arrayClass);
    arrayClass.members().enter(lengthVar);
    Symbol cloneMethod =
        new MethodSymbol(
            PUBLIC,
            names.clone,
            new MethodType(Type.emptyList, objectType, Type.emptyList, methodClass),
            arrayClass);
    arrayClass.members().enter(cloneMethod);
    nullConst = enterConstant("null", botType);
    trueConst = enterConstant("true", booleanType.constType(new Integer(1)));
    falseConst = enterConstant("false", booleanType.constType(new Integer(0)));
    enterUnop("+", intType, intType, nop);
    enterUnop("+", longType, longType, nop);
    enterUnop("+", floatType, floatType, nop);
    enterUnop("+", doubleType, doubleType, nop);
    enterUnop("-", intType, intType, ineg);
    enterUnop("-", longType, longType, lneg);
    enterUnop("-", floatType, floatType, fneg);
    enterUnop("-", doubleType, doubleType, dneg);
    enterUnop("~", intType, intType, ixor);
    enterUnop("~", longType, longType, lxor);
    enterUnop("++", byteType, byteType, iadd);
    enterUnop("++", shortType, shortType, iadd);
    enterUnop("++", charType, charType, iadd);
    enterUnop("++", intType, intType, iadd);
    enterUnop("++", longType, longType, ladd);
    enterUnop("++", floatType, floatType, fadd);
    enterUnop("++", doubleType, doubleType, dadd);
    enterUnop("--", byteType, byteType, isub);
    enterUnop("--", shortType, shortType, isub);
    enterUnop("--", charType, charType, isub);
    enterUnop("--", intType, intType, isub);
    enterUnop("--", longType, longType, lsub);
    enterUnop("--", floatType, floatType, fsub);
    enterUnop("--", doubleType, doubleType, dsub);
    enterUnop("!", booleanType, booleanType, bool_not);
    nullcheck = enterUnop("<*nullchk*>", objectType, objectType, nullchk);
    enterBinop("+", stringType, stringType, stringType, string_add);
    enterBinop("+", stringType, intType, stringType, string_add);
    enterBinop("+", stringType, longType, stringType, string_add);
    enterBinop("+", stringType, floatType, stringType, string_add);
    enterBinop("+", stringType, doubleType, stringType, string_add);
    enterBinop("+", stringType, booleanType, stringType, string_add);
    enterBinop("+", stringType, objectType, stringType, string_add);
    enterBinop("+", stringType, botType, stringType, string_add);
    enterBinop("+", intType, stringType, stringType, string_add);
    enterBinop("+", longType, stringType, stringType, string_add);
    enterBinop("+", floatType, stringType, stringType, string_add);
    enterBinop("+", doubleType, stringType, stringType, string_add);
    enterBinop("+", booleanType, stringType, stringType, string_add);
    enterBinop("+", objectType, stringType, stringType, string_add);
    enterBinop("+", botType, stringType, stringType, string_add);
    enterBinop("+", intType, intType, intType, iadd);
    enterBinop("+", longType, longType, longType, ladd);
    enterBinop("+", floatType, floatType, floatType, fadd);
    enterBinop("+", doubleType, doubleType, doubleType, dadd);
    enterBinop("+", botType, botType, botType, error);
    enterBinop("+", botType, intType, botType, error);
    enterBinop("+", botType, longType, botType, error);
    enterBinop("+", botType, floatType, botType, error);
    enterBinop("+", botType, doubleType, botType, error);
    enterBinop("+", botType, booleanType, botType, error);
    enterBinop("+", botType, objectType, botType, error);
    enterBinop("+", intType, botType, botType, error);
    enterBinop("+", longType, botType, botType, error);
    enterBinop("+", floatType, botType, botType, error);
    enterBinop("+", doubleType, botType, botType, error);
    enterBinop("+", booleanType, botType, botType, error);
    enterBinop("+", objectType, botType, botType, error);
    enterBinop("-", intType, intType, intType, isub);
    enterBinop("-", longType, longType, longType, lsub);
    enterBinop("-", floatType, floatType, floatType, fsub);
    enterBinop("-", doubleType, doubleType, doubleType, dsub);
    enterBinop("*", intType, intType, intType, imul);
    enterBinop("*", longType, longType, longType, lmul);
    enterBinop("*", floatType, floatType, floatType, fmul);
    enterBinop("*", doubleType, doubleType, doubleType, dmul);
    enterBinop("/", intType, intType, intType, idiv);
    enterBinop("/", longType, longType, longType, ldiv);
    enterBinop("/", floatType, floatType, floatType, fdiv);
    enterBinop("/", doubleType, doubleType, doubleType, ddiv);
    enterBinop("%", intType, intType, intType, imod);
    enterBinop("%", longType, longType, longType, lmod);
    enterBinop("%", floatType, floatType, floatType, fmod);
    enterBinop("%", doubleType, doubleType, doubleType, dmod);
    enterBinop("&", booleanType, booleanType, booleanType, iand);
    enterBinop("&", intType, intType, intType, iand);
    enterBinop("&", longType, longType, longType, land);
    enterBinop("|", booleanType, booleanType, booleanType, ior);
    enterBinop("|", intType, intType, intType, ior);
    enterBinop("|", longType, longType, longType, lor);
    enterBinop("^", booleanType, booleanType, booleanType, ixor);
    enterBinop("^", intType, intType, intType, ixor);
    enterBinop("^", longType, longType, longType, lxor);
    enterBinop("<<", intType, intType, intType, ishl);
    enterBinop("<<", longType, intType, longType, lshl);
    enterBinop("<<", intType, longType, intType, ishll);
    enterBinop("<<", longType, longType, longType, lshll);
    enterBinop(">>", intType, intType, intType, ishr);
    enterBinop(">>", longType, intType, longType, lshr);
    enterBinop(">>", intType, longType, intType, ishrl);
    enterBinop(">>", longType, longType, longType, lshrl);
    enterBinop(">>>", intType, intType, intType, iushr);
    enterBinop(">>>", longType, intType, longType, lushr);
    enterBinop(">>>", intType, longType, intType, iushrl);
    enterBinop(">>>", longType, longType, longType, lushrl);
    enterBinop("<", intType, intType, booleanType, if_icmplt);
    enterBinop("<", longType, longType, booleanType, lcmp, iflt);
    enterBinop("<", floatType, floatType, booleanType, fcmpg, iflt);
    enterBinop("<", doubleType, doubleType, booleanType, dcmpg, iflt);
    enterBinop(">", intType, intType, booleanType, if_icmpgt);
    enterBinop(">", longType, longType, booleanType, lcmp, ifgt);
    enterBinop(">", floatType, floatType, booleanType, fcmpl, ifgt);
    enterBinop(">", doubleType, doubleType, booleanType, dcmpl, ifgt);
    enterBinop("<=", intType, intType, booleanType, if_icmple);
    enterBinop("<=", longType, longType, booleanType, lcmp, ifle);
    enterBinop("<=", floatType, floatType, booleanType, fcmpg, ifle);
    enterBinop("<=", doubleType, doubleType, booleanType, dcmpg, ifle);
    enterBinop(">=", intType, intType, booleanType, if_icmpge);
    enterBinop(">=", longType, longType, booleanType, lcmp, ifge);
    enterBinop(">=", floatType, floatType, booleanType, fcmpl, ifge);
    enterBinop(">=", doubleType, doubleType, booleanType, dcmpl, ifge);
    enterBinop("==", intType, intType, booleanType, if_icmpeq);
    enterBinop("==", longType, longType, booleanType, lcmp, ifeq);
    enterBinop("==", floatType, floatType, booleanType, fcmpl, ifeq);
    enterBinop("==", doubleType, doubleType, booleanType, dcmpl, ifeq);
    enterBinop("==", booleanType, booleanType, booleanType, if_icmpeq);
    enterBinop("==", objectType, objectType, booleanType, if_acmpeq);
    enterBinop("!=", intType, intType, booleanType, if_icmpne);
    enterBinop("!=", longType, longType, booleanType, lcmp, ifne);
    enterBinop("!=", floatType, floatType, booleanType, fcmpl, ifne);
    enterBinop("!=", doubleType, doubleType, booleanType, dcmpl, ifne);
    enterBinop("!=", booleanType, booleanType, booleanType, if_icmpne);
    enterBinop("!=", objectType, objectType, booleanType, if_acmpne);
    enterBinop("&&", booleanType, booleanType, booleanType, bool_and);
    enterBinop("||", booleanType, booleanType, booleanType, bool_or);
  }