trees.scala
来自「JAVA 语言的函数式编程扩展」· SCALA 代码 · 共 1,693 行 · 第 1/5 页
SCALA
1,693 行
(superRef /: argss) (Apply) } /** Type annotation, eliminated by explicit outer */ case class Typed(expr: Tree, tpt: Tree) extends TermTree abstract class GenericApply extends TermTree { val fun: Tree val args: List[Tree] } /** Type application */ case class TypeApply(fun: Tree, args: List[Tree]) extends GenericApply { override def symbol: Symbol = fun.symbol override def symbol_=(sym: Symbol) { fun.symbol = sym } } /** Value application */ case class Apply(fun: Tree, args: List[Tree]) extends GenericApply { override def symbol: Symbol = fun.symbol override def symbol_=(sym: Symbol) { fun.symbol = sym } } /** Dynamic value application. * In a dynamic application q.f(as) * - q is stored in qual * - as is stored in args * - f is stored as the node's symbol field. */ case class ApplyDynamic(qual: Tree, args: List[Tree]) extends TermTree with SymTree /** Super reference */ case class Super(qual: Name, mix: Name) extends TermTree with SymTree def Super(sym: Symbol, mix: Name): Tree = Super(sym.name, mix) setSymbol sym /** Self reference */ case class This(qual: Name) extends TermTree with SymTree def This(sym: Symbol): Tree = This(sym.name) setSymbol sym /** Designator */ case class Select(qualifier: Tree, selector: Name) extends SymTree { override def isTerm = selector.isTermName override def isType = selector.isTypeName } def Select(qualifier: Tree, sym: Symbol): Select = Select(qualifier, sym.name) setSymbol sym /** Identifier */ case class Ident(name: Name) extends SymTree { override def isTerm = name.isTermName override def isType = name.isTypeName } class BackQuotedIdent(name: Name) extends Ident(name) def Ident(sym: Symbol): Ident = Ident(sym.name) setSymbol sym /** Literal */ case class Literal(value: Constant) extends TermTree { assert(value ne null) } def Literal(value: Any): Literal = Literal(Constant(value)) /** A synthetic term holding an arbitrary type. Not to be confused with * with TypTree, the trait for trees that are only used for type trees. * TypeTree's are inserted in several places, but most notably in * <code>RefCheck</code>, where the arbitrary type trees are all replaced by * TypeTree's. */ case class TypeTree() extends TypTree { var original: Tree = _ override def symbol = if (tpe == null) null else tpe.typeSymbol def setOriginal(tree: Tree): this.type = { original = tree setPos(tree.pos) } override def isEmpty = (tpe eq null) || tpe == NoType } def TypeTree(tp: Type): TypeTree = TypeTree() setType tp // def TypeTree(tp: Type, tree : Tree): TypeTree = TypeTree(tree) setType tp /** A tree that has an attribute attached to it */ case class Annotated(annot: Annotation, arg: Tree) extends Tree { override def isType = arg.isType override def isTerm = arg.isTerm } /** Singleton type, eliminated by RefCheck */ case class SingletonTypeTree(ref: Tree) extends TypTree /** Type selection, eliminated by RefCheck */ case class SelectFromTypeTree(qualifier: Tree, selector: Name) extends TypTree with SymTree /** Intersection type, eliminated by RefCheck */ case class CompoundTypeTree(templ: Template) extends TypTree /** Applied type, eliminated by RefCheck */ case class AppliedTypeTree(tpt: Tree, args: List[Tree]) extends TypTree { override def symbol: Symbol = tpt.symbol override def symbol_=(sym: Symbol) { tpt.symbol = sym } } case class TypeBoundsTree(lo: Tree, hi: Tree) extends TypTree case class ExistentialTypeTree(tpt: Tree, whereClauses: List[Tree]) extends TypTree trait StubTree extends Tree { override def equalsStructure0(that: Tree)(f : (Tree,Tree) => Boolean): Boolean = this eq that }/* A standard pattern match case EmptyTree => case PackageDef(name, stats) => // package name { stats } case ClassDef(mods, name, tparams, impl) => // mods class name[tparams] impl case ModuleDef(mods, name, impl) => (eliminated by refcheck) // mods object name impl where impl = extends parents { defs } case ValDef(mods, name, tpt, rhs) => // mods val name: tpt = rhs case DefDef(mods, name, tparams, vparamss, tpt, rhs) => // mods def name[tparams](vparams): tpt = rhs case TypeDef(mods, name, tparams, rhs) => (eliminated by erasure) // mods type name[tparams] = rhs case LabelDef(name, params, rhs) => // used for tailcalls and like case Import(expr, selectors) => (eliminated by typecheck) // import expr.{selectors} case Annotation(constr, elements) => // @constr(elements) where constr = tp(args), elements = { val x1 = c1, ..., val xn = cn } case DocDef(comment, definition) => (eliminated by typecheck) // /** comment */ definition case Template(parents, self, body) => // extends parents { self => body } case Block(stats, expr) => // { stats; expr } case CaseDef(pat, guard, body) => (eliminated by transmatch/explicitouter) // case pat if guard => body case Sequence(trees) => (eliminated by transmatch/explicitouter) // pat1, ..., pat_n case Alternative(trees) => (eliminated by transmatch/explicitouter) // pat1 | ... | patn case Star(elem) => (eliminated by transmatch/explicitouter) // pat* case Bind(name, body) => (eliminated by transmatch/explicitouter) // name @ pat case UnApply(fun: Tree, args) (introduced by typer, eliminated by transmatch/explicitouter) // used for unapply's case ArrayValue(elemtpt, trees) => (introduced by uncurry) // used to pass arguments to vararg arguments case Function(vparams, body) => (eliminated by lambdaLift) // vparams => body where vparams:List[ValDef] case Assign(lhs, rhs) => // lhs = rhs case If(cond, thenp, elsep) => // if (cond) thenp else elsep case Match(selector, cases) => // selector match { cases } case Return(expr) => // return expr case Try(block, catches, finalizer) => // try block catch { catches } finally finalizer where catches: List[CaseDef] case Throw(expr) => // throw expr case New(tpt) => // new tpt always in the context: new tpt.<init>[targs](args) case Typed(expr, tpt) => (eliminated by erasure) // expr: tpt case TypeApply(fun, args) => // fun[args] case Apply(fun, args) => // fun(args) case ApplyDynamic(qual, args) (introduced by erasure, eliminated by cleanup) // fun(args) case Super(qual, mix) => // qual.super[mix] case This(qual) => // qual.this case Select(qualifier, selector) => // qualifier.selector case Ident(name) => // name case Literal(value) => // value case TypeTree() => (introduced by refcheck) // a type that's not written out, but given in the attribute case Annotated(annot, arg) => (eliminated by typer) // arg @annot for types, arg: @annot for exprs case SingletonTypeTree(ref) => (eliminated by uncurry) // ref.type case SelectFromTypeTree(qualifier, selector) => (eliminated by uncurry) // qualifier # selector, a path-dependent type p.T is expressed as p.type # T case CompoundTypeTree(templ: Template) => (eliminated by uncurry) // parent1 with ... with parentN { refinement } case AppliedTypeTree(tpt, args) => (eliminated by uncurry) // tpt[args] case TypeBoundsTree(lo, hi) => (eliminated by uncurry) // >: lo <: hi case ExistentialTypeTree(tpt, whereClauses) =>*/ abstract class TreeCopier { def ClassDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[TypeDef], impl: Template): ClassDef def PackageDef(tree: Tree, name: Name, stats: List[Tree]): PackageDef def ModuleDef(tree: Tree, mods: Modifiers, name: Name, impl: Template): ModuleDef def ValDef(tree: Tree, mods: Modifiers, name: Name, tpt: Tree, rhs: Tree): ValDef def DefDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[TypeDef], vparamss: List[List[ValDef]], tpt: Tree, rhs: Tree): DefDef def TypeDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[TypeDef], rhs: Tree): TypeDef def LabelDef(tree: Tree, name: Name, params: List[Ident], rhs: Tree): LabelDef def Import(tree: Tree, expr: Tree, selectors: List[(Name, Name)]): Import def Annotation(tree: Tree, constr: Tree, elements: List[Tree]): Annotation def DocDef(tree: Tree, comment: String, definition: Tree): DocDef def Template(tree: Tree, parents: List[Tree], self: ValDef, body: List[Tree]): Template def Block(tree: Tree, stats: List[Tree], expr: Tree): Block def CaseDef(tree: Tree, pat: Tree, guard: Tree, body: Tree): CaseDef def Sequence(tree: Tree, trees: List[Tree]): Sequence def Alternative(tree: Tree, trees: List[Tree]): Alternative def Star(tree: Tree, elem: Tree): Star def Bind(tree: Tree, name: Name, body: Tree): Bind def UnApply(tree: Tree, fun: Tree, args: List[Tree]): UnApply def ArrayValue(tree: Tree, elemtpt: Tree, trees: List[Tree]): ArrayValue def Function(tree: Tree, vparams: List[ValDef], body: Tree): Function def Assign(tree: Tree, lhs: Tree, rhs: Tree): Assign def If(tree: Tree, cond: Tree, thenp: Tree, elsep: Tree): If def Match(tree: Tree, selector: Tree, cases: List[CaseDef]): Match def Return(tree: Tree, expr: Tree): Return def Try(tree: Tree, block: Tree, catches: List[CaseDef], finalizer: Tree): Try def Throw(tree: Tree, expr: Tree): Throw def New(tree: Tree, tpt: Tree): New def Typed(tree: Tree, expr: Tree, tpt: Tree): Typed def TypeApply(tree: Tree, fun: Tree, args: List[Tree]): TypeApply def Apply(tree: Tree, fun: Tree, args: List[Tree]): Apply def ApplyDynamic(tree: Tree, qual: Tree, args: List[Tree]): ApplyDynamic def Super(tree: Tree, qual: Name, mix: Name): Super def This(tree: Tree, qual: Name): This def Select(tree: Tree, qualifier: Tree, selector: Name): Select def Ident(tree: Tree, name: Name): Ident def Literal(tree: Tree, value: Constant): Literal def TypeTree(tree: Tree): TypeTree def Annotated(tree: Tree, annot: Annotation, arg: Tree): Annotated def SingletonTypeTree(tree: Tree, ref: Tree): SingletonTypeTree def SelectFromTypeTree(tree: Tree, qualifier: Tree, selector: Name): SelectFromTypeTree def CompoundTypeTree(tree: Tree, templ: Template): CompoundTypeTree def AppliedTypeTree(tree: Tree, tpt: Tree, args: List[Tree]): AppliedTypeTree def TypeBoundsTree(tree: Tree, lo: Tree, hi: Tree): TypeBoundsTree def ExistentialTypeTree(tree: Tree, tpt: Tree, whereClauses: List[Tree]): ExistentialTypeTree } class StrictTreeCopier extends TreeCopier { def ClassDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[TypeDef], impl: Template) = new ClassDef(mods, name, tparams, impl).copyAttrs(tree); def PackageDef(tree: Tree, name: Name, stats: List[Tree]) = new PackageDef(name, stats).copyAttrs(tree) def ModuleDef(tree: Tree, mods: Modifiers, name: Name, impl: Template) = new ModuleDef(mods, name, impl).copyAttrs(tree) def ValDef(tree: Tree, mods: Modifiers, name: Name, tpt: Tree, rhs: Tree) = new ValDef(mods, name, tpt, rhs).copyAttrs(tree) def DefDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[TypeDef], vparamss: List[List[ValDef]], tpt: Tree, rhs: Tree) = new DefDef(mods, name, tparams, vparamss, tpt, rhs).copyAttrs(tree) def TypeDef(tree: Tree, mods: Modifiers, name: Name, tparams: List[TypeDef], rhs: Tree) = new TypeDef(mods, name, tparams, rhs).copyAttrs(tree) def LabelDef(tree: Tree, name: Name, params: List[Ident], rhs: Tree) = new LabelDef(name, params, rhs).copyAttrs(tree) def Import(tree: Tree, expr: Tree, selectors: List[(Name, Name)]) = new Import(expr, selectors).copyAttrs(tree) def Annotation(tree: Tree, constr: Tree, elements: List[Tree]) = new Annotation(constr, elements).copyAttrs(tree) def DocDef(tree: Tree, comment: String, definition: Tree) = new DocDef(comment, definition).copyAttrs(tree) def Template(tree: Tree, parents: List[Tree], self: ValDef, body: List[Tree]) = new Template(parents, self, body).copyAttrs(tree) def Block(tree: Tree, stats: List[Tree], expr: Tree) = new Block(stats, expr).copyAttrs(tree) def CaseDef(tree: Tree, pat: Tree, guard: Tree, body: Tree) = new CaseDef(pat, guard, body).copyAttrs(tree) def Sequence(tree: Tree, trees: List[Tree]) = new Sequence(trees).copyAttrs(tree) def Alternative(tree: Tree, trees: List[Tree]) = new Alternative(trees).copyAttrs(tree) def Star(tree: Tree, elem: Tree) = new Star(elem).copyAttrs(tree) def Bind(tree: Tree, name: Name, body: Tree) = new Bind(name, body).copyAttrs(tree) def UnApply(tree: Tree, fun: Tree, args: List[Tree]) = new UnApply(fun, args).copyAttrs(tree) def ArrayValue(tree: Tree, elemtpt: Tree, trees: List[Tree]) = new ArrayValue(elemtpt, trees).copyAttrs(tree) def Function(tree: Tree, vparams: List[ValDef], body: Tree) = new Function(vparams, body).copyAttrs(tree) def Assign(tree: Tree, lhs: Tree, rhs: Tree) = new Assign(lhs, rhs).copyAttrs(tree) def If(tree: Tree, cond: Tree, thenp: Tree, elsep: Tree) = new If(cond, thenp, elsep).copyAttrs(tree) def Match(tree: Tree, selector: Tree, cases: List[CaseDef]) = new Match(selector, cases).copyAttrs(tree) def Return(tree: Tree, expr: Tree) = new Return(expr).copyAttrs(tree) def Try(tree: Tree, block: Tree, catches: List[CaseDef], finalizer: Tree) = new Try(block, catches, finalizer).copyAttrs(tree) def Throw(tree: Tree, expr: Tree) = new Throw(expr).copyAttrs(tree) def New(tree: Tree, tpt: Tree) = new New(tpt).copyAttrs(tree) def Typed(tree: Tree, expr: Tree, tpt: Tree) = new Typed(expr, tpt).copyAttrs(tree) def TypeApply(tree: Tree, fun: Tree, args: List[Tree]) = new TypeApply(fun, args).copyAttrs(tree) def Apply(tree: Tree, fun: Tree, args: List[Tree]) = new Apply(fun, args).copyAttrs(tree) def ApplyDynamic(tree: Tree, qual: Tree, args: List[Tree]) = new ApplyDynamic(qual, args).copyAttrs(tree) def Super(tree: Tree, qual: Name, mix: Name) = new Super(qual, mix).copyAttrs(tree) def This(tree: Tree, qual: Name) = new This(qual).copyAttrs(tree)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?