关于语法节点Tree、类型Type和符号Symbol
?
每個語法節點Tree都有Type屬性,部分的語法節點有Symbol屬性,如下:
?
與Symbol類型與Type類型之間的關系如下:
?
?
下面是Symbol與Type之間的關系:
(1)MethodSymbol("finalize").type = MethodType("()void").tsym=
ClassSymbol("Method").type=ClassType("Method").tsym=ClassSymbol("Method")
(2)TypeVar("M").tsym=TypeSymbol("M").type=TypeVar("M").tsym
(3)PackageSymbol("java").type = PackageType("java").tsym=PackageSymbol("java")
(4)VarSymbol("length").type=Type("int").tsym=ClassSymbol("int").type=Type("int").tsym=ClassSymbol("int")
?
下面是Node與Type之間的關系:
?
class Outer{class Inner{} }public class Test01 extends Outer.Inner{public Test01(Outer o){o.super();} }?
Outer.Inner是一個樹節點,如果這個節點中的type有值,則直接返回即可,不用再進行標注。
?
?
?
關于Node、Symbol與Type舉一個例子,如下:
import java.io.FileInputStream; import java.io.InputStream;public class TestScope<T1 extends InputStream,T2>{ public void test(){TestScope<FileInputStream,?> x = null;} }截圖如下:?
JCTypeApply的Node結點中沒有Symbol屬性,但是每個Node中都有Type屬性,其值如上圖藍色部分。
ClassSymbol的Symbol結點中,由于每個Symbol中都有TypeSymbol類型的屬性,這個屬性值為com.test18.TestScope<T1,T2>
ClassType的Type結點中,由于每個Type中都有Symbol屬性,這個屬性的值為com.test18.TestScope?
1、Symbol
對于Symbol來說:
Symbol中既有Symbol類型屬性也有Type類型的屬性,如下:
/** The type of this symbol.*/public Type type;/** The owner of this symbol.*/public Symbol owner;/** The completer of this symbol.*/public Completer completer;/** A cache for the type erasure of this symbol.*/public Type erasure_field;所以每個Symbol類型都有type屬性。
而標注Symbol類型的是Kinds枚舉類型,代碼如下:
Symbol可以通過訪問模式來訪問各個結點,定義如下:
(1)Symbol.Visitor<R, P>
符號類中定義的訪問者模式接口如下:
/*** A visitor for symbols. A visitor is used to implement operations* (or relations) on symbols. Most common operations on types are* binary relations and this interface is designed for binary* relations, that is, operations on the form* Symbol × P → R.* <!-- In plain text: Type x P -> R -->** @param <R> the return type of the operation implemented by this* visitor; use Void if no return type is needed.* @param <P> the type of the second argument (the first being the* symbol itself) of the operation implemented by this visitor; use* Void if a second argument is not needed.*/public interface Visitor<R,P> {R visitClassSymbol(ClassSymbol s, P arg);R visitMethodSymbol(MethodSymbol s, P arg);R visitPackageSymbol(PackageSymbol s, P arg);R visitOperatorSymbol(OperatorSymbol s, P arg);R visitVarSymbol(VarSymbol s, P arg);R visitTypeSymbol(TypeSymbol s, P arg);R visitSymbol(Symbol s, P arg);}?
(2)Types中的DefaultSymbolVisitor<R,S>
?
/*** A default visitor for symbols. All visitor methods except* visitSymbol are implemented by delegating to visitSymbol. Concrete* subclasses must provide an implementation of visitSymbol and can* override other methods as needed.** @param <R> the return type of the operation implemented by this* visitor; use Void if no return type is needed.* @param <S> the type of the second argument (the first being the* symbol itself) of the operation implemented by this visitor; use* Void if a second argument is not needed.*/public static abstract class DefaultSymbolVisitor<R,S> implements Symbol.Visitor<R,S> {final public R visit(Symbol s, S arg) { return s.accept(this, arg); }public R visitClassSymbol(ClassSymbol s, S arg) { return visitSymbol(s, arg); }public R visitMethodSymbol(MethodSymbol s, S arg) { return visitSymbol(s, arg); }public R visitOperatorSymbol(OperatorSymbol s, S arg) { return visitSymbol(s, arg); }public R visitPackageSymbol(PackageSymbol s, S arg) { return visitSymbol(s, arg); }public R visitTypeSymbol(TypeSymbol s, S arg) { return visitSymbol(s, arg); }public R visitVarSymbol(VarSymbol s, S arg) { return visitSymbol(s, arg); }}?
?
?
2、Type
對于每個Type類型來說,只限定有TypeSymbol類型的屬性,也就是包、類和類型變量對應的符號。
// The defining class / interface / package / type variable public TypeSymbol typeSymbol; // 只有ClassSymbol與PackageSymbol繼承了TypeSymbol標注每個Type類型的為TypeTags,代碼如下:
/** An interface for type tag values, which distinguish between different sorts of types.*/ public class TypeTags {private TypeTags() {} // uninstantiable/** The tag of the basic type `byte'.*/public static final int BYTE = 1;/** The tag of the basic type `char'.*/public static final int CHAR = BYTE+1;/** The tag of the basic type `short'.*/public static final int SHORT = CHAR+1;/** The tag of the basic type `int'.*/public static final int INT = SHORT+1;/** The tag of the basic type `long'.*/public static final int LONG = INT+1;/** The tag of the basic type `float'.*/public static final int FLOAT = LONG+1;/** The tag of the basic type `double'.*/public static final int DOUBLE = FLOAT+1;/** The tag of the basic type `boolean'.*/public static final int BOOLEAN = DOUBLE+1;/** The tag of the type `void'.*/public static final int VOID = BOOLEAN+1;/** The tag of all class and interface types.*/public static final int CLASS = VOID+1;/** The tag of all array types.*/public static final int ARRAY = CLASS+1;/** The tag of all (monomorphic 單一同態的) method types.*/public static final int METHOD = ARRAY+1;/** The tag of all package "types".*/public static final int PACKAGE = METHOD+1;/** The tag of all (source-level) type variables.*/public static final int TYPEVAR = PACKAGE+1;/** The tag of all type arguments.*/public static final int WILDCARD = TYPEVAR+1;/** The tag of all polymorphic (method-) types.*/public static final int FORALL = WILDCARD+1;/** The tag of the bottom type <null>.*/public static final int BOT = FORALL+1;/** The tag of a missing type.*/public static final int NONE = BOT+1;/** The tag of the error type.*/public static final int ERROR = NONE+1;/** The tag of an unknown type*/public static final int UNKNOWN = ERROR+1;/** The tag of all instantiatable type variables.*/public static final int UNDETVAR = UNKNOWN+1;/** The number of type tags.*/public static final int TypeTagCount = UNDETVAR+1;/** The maximum tag of a basic type.*/public static final int lastBaseTag = BOOLEAN;/** The minimum tag of a partial type*/public static final int firstPartialTag = ERROR; }Javac為Type結果定義了訪問者接口,如下:
(1)Type.Visitor<R, S>
類型Type中定義的訪問者模式:
/*** A visitor for types. A visitor is used to implement operations* (or relations) on types. Most common operations on types are* binary relations and this interface is designed for binary* relations, that is, operations on the form* Type × S → R.* <!-- In plain text: Type x S -> R -->** @param <R> the return type of the operation implemented by this* visitor; use Void if no return type is needed.* @param <S> the type of the second argument (the first being the* type itself) of the operation implemented by this visitor; use* Void if a second argument is not needed.*/public interface Visitor<R,S> {R visitClassType(ClassType t, S s);R visitWildcardType(WildcardType t, S s);R visitArrayType(ArrayType t, S s);R visitMethodType(MethodType t, S s);R visitPackageType(PackageType t, S s);R visitTypeVar(TypeVar t, S s);R visitCapturedType(CapturedType t, S s);R visitForAll(ForAll t, S s);R visitUndeterminedVar(UndeterminedVar t, S s);R visitErrorType(ErrorType t, S s);R visitType(Type t, S s);}?
(2)Types中的DefaultTypeVisitor<R,S>,SimpleTypeVisitor<R,S>
?
/*** A default visitor for types. All visitor methods except* visitType are implemented by delegating to visitType. Concrete* subclasses must provide an implementation of visitType and can* override other methods as needed.** @param <R> the return type of the operation implemented by this* visitor; use Void if no return type is needed.* @param <S> the type of the second argument (the first being the* type itself) of the operation implemented by this visitor; use* Void if a second argument is not needed.*/public static abstract class DefaultTypeVisitor<R,S> implements Type.Visitor<R,S> {final public R visit(Type t, S s) { return t.accept(this, s); }public R visitClassType(ClassType t, S s) { return visitType(t, s); }public R visitWildcardType(WildcardType t, S s) { return visitType(t, s); }public R visitArrayType(ArrayType t, S s) { return visitType(t, s); }public R visitMethodType(MethodType t, S s) { return visitType(t, s); }public R visitPackageType(PackageType t, S s) { return visitType(t, s); }public R visitTypeVar(TypeVar t, S s) { return visitType(t, s); }public R visitCapturedType(CapturedType t, S s) { return visitType(t, s); }public R visitForAll(ForAll t, S s) { return visitType(t, s); }public R visitUndeterminedVar(UndeterminedVar t, S s) { return visitType(t, s); }public R visitErrorType(ErrorType t, S s) { return visitType(t, s); }}?
/*** A <em>simple</em> visitor for types. This visitor is simple as* captured wildcards, for-all types (generic methods), and* undetermined(未確定的) type variables (part of inference) are hidden.* Captured wildcards are hidden by treating them as type* variables and the rest are hidden by visiting their qtypes.** @param <R> the return type of the operation implemented by this* visitor; use Void if no return type is needed.* @param <S> the type of the second argument (the first being the* type itself) of the operation implemented by this visitor; use* Void if a second argument is not needed.*/public static abstract class SimpleTypeVisitor<R,S> extends DefaultTypeVisitor<R,S> {@Overridepublic R visitCapturedType(CapturedType t, S s) {return visitTypeVar(t, s);}@Overridepublic R visitForAll(ForAll t, S s) {return visit(t.qtype, s);}@Overridepublic R visitUndeterminedVar(UndeterminedVar t, S s) {return visit(t.qtype, s);}}?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/extjs4/p/6897234.html
總結
以上是生活随笔為你收集整理的关于语法节点Tree、类型Type和符号Symbol的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT 甲级 1048 Find Coi
- 下一篇: android像素密度转厘米,Andro