首页 > 代码库 > Javac源码简单分析之解析和填充符号表
Javac源码简单分析之解析和填充符号表
一、说明
符号表是由一组符号地址和符号信息构成的表格。符号表中所登记的信息在编译的不同阶段都要用到,在语义分析(后面的步骤)中,符号表所登记的内容将用于语义检查和产生中间代码,在目标代码生成阶段,党对符号名进行地址分配时,符号表是地址分配的依据。二、主要的类与方法
解析和填充符号表这个过程主要由com.sun.tools.javac.comp.Entry及com.sun.tools.javac.comp.MemberEnter两个类来实现的。
com.sun.tools.javac.comp.Entry 主要的方法如下:
/** * 访问类声明 */ 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.len != 0 && !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.len != 0) 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 = new ErrorType(tree.name, (TypeSymbol)owner); 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; } /** Main method: enter all classes in a list of toplevel trees. * @param trees The list of trees to be processed. */ public void main(List<JCCompilationUnit> trees) { complete(trees, null); } /** * Main method: enter one class from a list of toplevel trees and * place the rest on uncompleted for later processing. * @param trees The list of trees to be processed. * @param c The class symbol to be processed. */ public void complete(List<JCCompilationUnit> trees, ClassSymbol c) { annotate.enterStart(); ListBuffer<ClassSymbol> prevUncompleted = uncompleted; if (memberEnter.completionEnabled) uncompleted = new ListBuffer<ClassSymbol>(); try { // enter all classes, and construct uncompleted list classEnter(trees, null); // complete all uncompleted classes in memberEnter if (memberEnter.completionEnabled) { while (uncompleted.nonEmpty()) { ClassSymbol clazz = uncompleted.next(); if (c == null || c == clazz || prevUncompleted == null) clazz.complete(); else //将类符号放入prevUncompleted列表(uncompleted列表) prevUncompleted.append(clazz); } // if there remain any unimported toplevels (these must have // no classes at all), process their import statements as well. /** * uncompleted列表没有的符号(除类符号外),根据improt声明,给顶级抽象树都添加了一个MemberEnter对象 * 这些符号(包括类的参数类型符号也就是泛型、父类符号、接口类型符等) */ for (JCCompilationUnit tree : trees) { if (tree.starImportScope.elems == null) { JavaFileObject prev = log.useSource(tree.sourcefile); Env<AttrContext> env = typeEnvs.get(tree); if (env == null) env = topLevelEnv(tree); memberEnter.memberEnter(tree, env); log.useSource(prev); } } } } finally { //prevUncompleted列表赋值给uncompleted列表 uncompleted = prevUncompleted; annotate.enterDone(); } }
com.sun.tools.javac.comp.MemberEnter 主要的方法如下:
/** Complete entering a class. * 将未处理列表中的所有符号都解析到各自的类符号表中 * @param sym The symbol of the class to be completed. */ public void complete(Symbol sym) throws CompletionFailure { // Suppress some (recursive) MemberEnter invocations if (!completionEnabled) { // Re-install same completer for next time around and return. assert (sym.flags() & Flags.COMPOUND) == 0; sym.completer = this; return; } ClassSymbol c = (ClassSymbol)sym; ClassType ct = (ClassType)c.type; Env<AttrContext> env = enter.typeEnvs.get(c); JCClassDecl tree = (JCClassDecl)env.tree; boolean wasFirst = isFirst; isFirst = false; JavaFileObject prev = log.useSource(env.toplevel.sourcefile); try { // Save class environment for later member enter (2) processing. halfcompleted.append(env); // If this is a toplevel-class, make sure any preceding import // clauses have been seen. if (c.owner.kind == PCK) { memberEnter(env.toplevel, env.enclosing(JCTree.TOPLEVEL)); todo.append(env); } // Mark class as not yet attributed. c.flags_field |= UNATTRIBUTED; if (c.owner.kind == TYP) c.owner.complete(); // create an environment for evaluating the base clauses Env<AttrContext> baseEnv = baseEnv(tree, env); // Determine supertype. Type supertype = (tree.extending != null) ? attr.attribBase(tree.extending, baseEnv, true, false, true) : ((tree.mods.flags & Flags.ENUM) != 0 && !target.compilerBootstrap(c)) ? attr.attribBase(enumBase(tree.pos, c), baseEnv, true, false, false) : (c.fullname == names.java_lang_Object) ? Type.noType : syms.objectType; ct.supertype_field = supertype; // Determine interfaces. ListBuffer<Type> interfaces = new ListBuffer<Type>(); Set<Type> interfaceSet = new HashSet<Type>(); List<JCExpression> interfaceTrees = tree.implementing; if ((tree.mods.flags & Flags.ENUM) != 0 && target.compilerBootstrap(c)) { // add interface Comparable<T> interfaceTrees = interfaceTrees.prepend(make.Type(new ClassType(syms.comparableType.getEnclosingType(), List.of(c.type), syms.comparableType.tsym))); // add interface Serializable interfaceTrees = interfaceTrees.prepend(make.Type(syms.serializableType)); } for (JCExpression iface : interfaceTrees) { Type i = attr.attribBase(iface, baseEnv, false, true, true); if (i.tag == CLASS) { interfaces.append(i); chk.checkNotRepeated(iface.pos(), types.erasure(i), interfaceSet); } } if ((c.flags_field & ANNOTATION) != 0) ct.interfaces_field = List.of(syms.annotationType); else ct.interfaces_field = interfaces.toList(); if (c.fullname == names.java_lang_Object) { if (tree.extending != null) { chk.checkNonCyclic(tree.extending.pos(), supertype); ct.supertype_field = Type.noType; } else if (tree.implementing.nonEmpty()) { chk.checkNonCyclic(tree.implementing.head.pos(), ct.interfaces_field.head); ct.interfaces_field = List.nil(); } } // Annotations. // In general, we cannot fully process annotations yet, but we // can attribute the annotation types and then check to see if the // @Deprecated annotation is present. attr.attribAnnotationTypes(tree.mods.annotations, baseEnv); if (hasDeprecatedAnnotation(tree.mods.annotations)) c.flags_field |= DEPRECATED; annotateLater(tree.mods.annotations, baseEnv, c); attr.attribTypeVariables(tree.typarams, baseEnv); chk.checkNonCyclic(tree.pos(), c.type); /** * 增加一个默认的构造方法(当类没有构造方法时) */ if ((c.flags() & INTERFACE) == 0 && !TreeInfo.hasConstructors(tree.defs)) { List<Type> argtypes = List.nil(); List<Type> typarams = List.nil(); List<Type> thrown = List.nil(); long ctorFlags = 0; boolean based = false; if (c.name.len == 0) { JCNewClass nc = (JCNewClass)env.next.tree; if (nc.constructor != null) { Type superConstrType = types.memberType(c.type, nc.constructor); argtypes = superConstrType.getParameterTypes(); typarams = superConstrType.getTypeArguments(); ctorFlags = nc.constructor.flags() & VARARGS; if (nc.encl != null) { argtypes = argtypes.prepend(nc.encl.type); based = true; } thrown = superConstrType.getThrownTypes(); } } JCTree constrDef = DefaultConstructor(make.at(tree.pos), c, typarams, argtypes, thrown, ctorFlags, based); tree.defs = tree.defs.prepend(constrDef); } // If this is a class, enter symbols for this and super into // current scope. if ((c.flags_field & INTERFACE) == 0) { VarSymbol thisSym = new VarSymbol(FINAL | HASINIT, names._this, c.type, c); thisSym.pos = Position.FIRSTPOS; env.info.scope.enter(thisSym); if (ct.supertype_field.tag == CLASS) { VarSymbol superSym = new VarSymbol(FINAL | HASINIT, names._super, ct.supertype_field, c); superSym.pos = Position.FIRSTPOS; env.info.scope.enter(superSym); } } // check that no package exists with same fully qualified name, // but admit classes in the unnamed package which have the same // name as a top-level package. if (checkClash && c.owner.kind == PCK && c.owner != syms.unnamedPackage && reader.packageExists(c.fullname)) { log.error(tree.pos, "clash.with.pkg.of.same.name", c); } } catch (CompletionFailure ex) { chk.completionError(tree.pos(), ex); } finally { log.useSource(prev); } // Enter all member fields and methods of a set of half completed // classes in a second phase. if (wasFirst) { try { while (halfcompleted.nonEmpty()) { finish(halfcompleted.next()); } } finally { isFirst = true; } // commit pending annotations annotate.flush(); } }
三、过程及简单源码解析
Enter过程中,编译器会找到当前范围(enclosing scope)中发现的所有的定义(definitions),并且把这些定义注册成符号(symbols)。Enter又分为以下两个阶段:
第一个阶段:
编译器会注册所有类的符号,并且把这写符号和相应的范围(scope)联系在一起。实现方法是使用一个Visitor(访问者)类,由上而下的遍历AST(抽象语法树),访问所有的类,包括类里面的内部类。Enter给每一个类的符号都添加了一个MemberEnter对象,这个对象是由第二个阶段来调用的。
整个操作的方法调用过程如下:
上面这个过程是访问者模式的一种实现。
Enter是一个JCTree.Visitor.Enter.classEnter(l.head, env)调用JCTree.accept(Visitor v),而accept方法又是调用的Visitor类里面的visitXXX()方法,而这些方法的实现又是在Enter类中。也就是Enter.visitClassDef(JCClassDecl tree)方法,在这个方法中,会将类符号放入uncompleted列表;
visitClassDef(JCClassDecl tree)方法主要做三件事:
1、将类符号(当前类)填入类自身的符号表,添加了一个MemberEnter对象
// Enter class into `compiled' table and enclosing scope. if (chk.compiled.get(c.flatname) != null) { duplicateClass(tree.pos(), c); result = new ErrorType(tree.name, (TypeSymbol)owner); 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);
2、解析填写其它的类符号,包括当前类中使用到的内部类、枚举、变量等抽象树的类符号。
// Enter type parameters. ct.typarams_field = classEnter(tree.typarams, localEnv);
3、将类符号放入uncompleted列表
if (!c.isLocal() && uncompleted != null) uncompleted.append(c);
Enter给每一个类的符号都添加了一个MemberEnter对象,这个对象是由第二个阶段来调用的。
memberEnter.memberEnter(tree, env);
第二个阶段:
这些类被MemberEnter对象所完成(completed,即完成类的成员变量的Enter)。首先,MemberEnter决定一个类的参数,父类和接口。然后这些符号被添加进了类的范围中。不像前一个步骤,这个步骤是懒惰执行的。类的成员只有在被访问时,才加入类的定义中的。这里的实现,是通过安装一个完成对象(member object)到类的符号中。这些对象可以在需要时调用memberEnter。
整个操作的方法调用过程如下:
Enter是一个JCTree.Visitor.Enter.classEnter(l.head, env)调用JCTree.accept(Visitor v),而accept方法又是调用的Visitor类里面的visitXXX()方法,而这些方法的实现又是在Enter类中。也就是Enter.visitClassDef(JCClassDecl tree)方法,在这个方法中,会将类符号解析和填入类自身的符号表。
最后,enter把所有的顶层类(top-level classes)放到一个todo-queue中。