Skip to content
This repository
Browse code

removes duplication in FreeDef extractors

  • Loading branch information...
commit 99bdebb36d90cb0c9632fb637e3262c8c1b174f3 1 parent b7b4f87
Eugene Burmako xeno-by authored

Showing 1 changed file with 22 additions and 43 deletions. Show diff stats Hide diff stats

  1. +22 43 src/compiler/scala/reflect/reify/utils/Extractors.scala
65 src/compiler/scala/reflect/reify/utils/Extractors.scala
@@ -164,51 +164,30 @@ trait Extractors {
164 164 }
165 165 }
166 166
167   - object FreeDef {
168   - def unapply(tree: Tree): Option[(Tree, TermName, Tree, Long, String)] = tree match {
169   - case FreeTermDef(uref, name, binding, flags, origin) =>
170   - Some((uref, name, binding, flags, origin))
171   - case FreeTypeDef(uref, name, binding, flags, origin) =>
172   - Some((uref, name, binding, flags, origin))
173   - case _ =>
174   - None
175   - }
176   - }
177   -
178   - object FreeTermDef {
179   - def unapply(tree: Tree): Option[(Tree, TermName, Tree, Long, String)] = tree match {
180   - case
181   - ValDef(_, name, _, Apply(
182   - Select(Select(uref1 @ Ident(_), build1), newFreeTerm),
183   - List(
184   - _,
185   - _,
186   - Apply(Select(Select(uref2 @ Ident(_), build2), flagsFromBits), List(Literal(Constant(flags: Long)))),
187   - Literal(Constant(origin: String)))))
188   - if uref1.name == nme.UNIVERSE_SHORT && build1 == nme.build && newFreeTerm == nme.newFreeTerm &&
189   - uref2.name == nme.UNIVERSE_SHORT && build2 == nme.build && flagsFromBits == nme.flagsFromBits =>
190   - Some(uref1, name, reifyBinding(tree), flags, origin)
191   - case _ =>
192   - None
193   - }
194   - }
195   -
196   - object FreeTypeDef {
197   - def unapply(tree: Tree): Option[(Tree, TermName, Tree, Long, String)] = tree match {
198   - case
199   - ValDef(_, name, _, Apply(
200   - Select(Select(uref1 @ Ident(_), build1), newFreeType),
201   - List(
202   - _,
203   - Apply(Select(Select(uref2 @ Ident(_), build2), flagsFromBits), List(Literal(Constant(flags: Long)))),
204   - Literal(Constant(origin: String)))))
205   - if uref1.name == nme.UNIVERSE_SHORT && build1 == nme.build && newFreeType == nme.newFreeType &&
206   - uref2.name == nme.UNIVERSE_SHORT && build2 == nme.build && flagsFromBits == nme.flagsFromBits =>
207   - Some(uref1, name, reifyBinding(tree), flags, origin)
208   - case _ =>
209   - None
  167 + sealed abstract class FreeDefExtractor(acceptTerms: Boolean, acceptTypes: Boolean) {
  168 + def unapply(tree: Tree): Option[(Tree, TermName, Tree, Long, String)] = {
  169 + def acceptFreeTermFactory(name: Name) = {
  170 + (acceptTerms && name == nme.newFreeTerm) ||
  171 + (acceptTypes && name == nme.newFreeType)
  172 + }
  173 + tree match {
  174 + case
  175 + ValDef(_, name, _, Apply(
  176 + Select(Select(uref1 @ Ident(_), build1), freeTermFactory),
  177 + _ :+
  178 + Apply(Select(Select(uref2 @ Ident(_), build2), flagsFromBits), List(Literal(Constant(flags: Long)))) :+
  179 + Literal(Constant(origin: String))))
  180 + if uref1.name == nme.UNIVERSE_SHORT && build1 == nme.build && acceptFreeTermFactory(freeTermFactory) &&
  181 + uref2.name == nme.UNIVERSE_SHORT && build2 == nme.build && flagsFromBits == nme.flagsFromBits =>
  182 + Some(uref1, name, reifyBinding(tree), flags, origin)
  183 + case _ =>
  184 + None
  185 + }
210 186 }
211 187 }
  188 + object FreeDef extends FreeDefExtractor(acceptTerms = true, acceptTypes = true)
  189 + object FreeTermDef extends FreeDefExtractor(acceptTerms = true, acceptTypes = false)
  190 + object FreeTypeDef extends FreeDefExtractor(acceptTerms = false, acceptTypes = true)
212 191
213 192 object FreeRef {
214 193 def unapply(tree: Tree): Option[(Tree, TermName)] = tree match {

0 comments on commit 99bdebb

Please sign in to comment.
Something went wrong with that request. Please try again.