@@ -6,9 +6,12 @@ import scala.annotation.{Annotation, compileTimeOnly}
6
6
7
7
import dotty .tools .dotc
8
8
import dotty .tools .dotc .ast .tpd
9
- import dotty .tools .dotc .core .Contexts ._
9
+ import dotty .tools .dotc .core .Contexts .*
10
+ import dotty .tools .dotc .core .Flags .*
10
11
import dotty .tools .dotc .core .Names .*
12
+ import dotty .tools .dotc .core .Types .*
11
13
import dotty .tools .dotc .core .StdNames .nme
14
+ import dotty .tools .dotc .core .Symbols .*
12
15
13
16
/** Matches a quoted tree against a quoted pattern tree.
14
17
* A quoted pattern tree may have type and term holes in addition to normal terms.
@@ -119,15 +122,15 @@ object Matcher {
119
122
* ```
120
123
* when matching `a * a` with `x * x` the environment will contain `Map(a -> x)`.
121
124
*/
122
- private type Env = Map [dotc.core. Symbols . Symbol , dotc.core. Symbols . Symbol ]
125
+ private type Env = Map [Symbol , Symbol ]
123
126
124
127
inline private def withEnv [T ](env : Env )(inline body : Env ?=> T ): T = body(using env)
125
128
126
- def termMatch (scrutineeTerm : tpd. Tree , patternTerm : tpd. Tree ): Option [Tuple ] =
129
+ def termMatch (scrutineeTerm : Tree , patternTerm : Tree ): Option [Tuple ] =
127
130
given Env = Map .empty
128
131
scrutineeTerm =?= patternTerm
129
132
130
- def typeTreeMatch (scrutineeTypeTree : tpd. Tree , patternTypeTree : tpd. Tree ): Option [Tuple ] =
133
+ def typeTreeMatch (scrutineeTypeTree : Tree , patternTypeTree : Tree ): Option [Tuple ] =
131
134
given Env = Map .empty
132
135
scrutineeTypeTree =?= patternTypeTree
133
136
@@ -138,11 +141,11 @@ object Matcher {
138
141
case _ => notMatched
139
142
}
140
143
141
- extension (scrutinees : List [tpd. Tree ])
142
- private def =?= (patterns : List [tpd. Tree ])(using Env )(using DummyImplicit ): Matching =
144
+ extension (scrutinees : List [Tree ])
145
+ private def =?= (patterns : List [Tree ])(using Env )(using DummyImplicit ): Matching =
143
146
matchLists(scrutinees, patterns)(_ =?= _)
144
147
145
- extension (scrutinee0 : tpd. Tree )
148
+ extension (scrutinee0 : Tree )
146
149
147
150
/** Check that the trees match and return the contents from the pattern holes.
148
151
* Return None if the trees do not match otherwise return Some of a tuple containing all the contents in the holes.
@@ -152,11 +155,7 @@ object Matcher {
152
155
* @param `summon[Env]` Set of tuples containing pairs of symbols (s, p) where s defines a symbol in `scrutinee` which corresponds to symbol p in `pattern`.
153
156
* @return `None` if it did not match or `Some(tup: Tuple)` if it matched where `tup` contains the contents of the holes.
154
157
*/
155
- private def =?= (pattern0 : tpd.Tree )(using Env ): Matching =
156
- import tpd .* // TODO remove
157
- import dotc .core .Flags .* // TODO remove
158
- import dotc .core .Types .* // TODO remove
159
- import dotc .core .Symbols .* // TODO remove
158
+ private def =?= (pattern0 : Tree )(using Env ): Matching =
160
159
161
160
/* Match block flattening */ // TODO move to cases
162
161
/** Normalize the tree */
@@ -183,38 +182,38 @@ object Matcher {
183
182
// TODO remove
184
183
object TypeTreeTypeTest :
185
184
def unapply (x : Tree ): Option [Tree & x.type ] = x match
186
- case x : (tpd. TypeBoundsTree & x.type ) => None
187
- case x : (tpd. Tree & x.type ) if x.isType => Some (x)
185
+ case x : (TypeBoundsTree & x.type ) => None
186
+ case x : (Tree & x.type ) if x.isType => Some (x)
188
187
case _ => None
189
188
end TypeTreeTypeTest
190
189
191
190
object Lambda :
192
191
def apply (owner : Symbol , tpe : MethodType , rhsFn : (Symbol , List [Tree ]) => Tree ): Block =
193
- val meth = dotc.core. Symbols . newSymbol(owner, nme.ANON_FUN , Synthetic | Method , tpe)
194
- tpd. Closure (meth, tss => rhsFn(meth, tss.head))
192
+ val meth = newSymbol(owner, nme.ANON_FUN , Synthetic | Method , tpe)
193
+ Closure (meth, tss => rhsFn(meth, tss.head))
195
194
end Lambda
196
195
197
196
(scrutinee, pattern) match
198
197
199
198
/* Term hole */
200
199
// Match a scala.internal.Quoted.patternHole typed as a repeated argument and return the scrutinee tree
201
200
case (scrutinee @ Typed (s, tpt1), Typed (TypeApply (patternHole, tpt :: Nil ), tpt2))
202
- if patternHole.symbol.eq(dotc.core. Symbols . defn.QuotedRuntimePatterns_patternHole ) &&
201
+ if patternHole.symbol.eq(defn.QuotedRuntimePatterns_patternHole ) &&
203
202
s.tpe <:< tpt.tpe &&
204
203
tpt2.tpe.derivesFrom(defn.RepeatedParamClass ) =>
205
204
matched(quotes.reflect.TreeMethods .asExpr(scrutinee.asInstanceOf [quotes.reflect.Tree ]))
206
205
207
206
/* Term hole */
208
207
// Match a scala.internal.Quoted.patternHole and return the scrutinee tree
209
208
case (ClosedPatternTerm (scrutinee), TypeApply (patternHole, tpt :: Nil ))
210
- if patternHole.symbol.eq(dotc.core. Symbols . defn.QuotedRuntimePatterns_patternHole ) &&
209
+ if patternHole.symbol.eq(defn.QuotedRuntimePatterns_patternHole ) &&
211
210
scrutinee.tpe <:< tpt.tpe =>
212
211
matched(quotes.reflect.TreeMethods .asExpr(scrutinee.asInstanceOf [quotes.reflect.Tree ]))
213
212
214
213
/* Higher order term hole */
215
214
// Matches an open term and wraps it into a lambda that provides the free variables
216
215
case (scrutinee, pattern @ Apply (TypeApply (Ident (_), List (TypeTree ())), SeqLiteral (args, _) :: Nil ))
217
- if pattern.symbol.eq(dotc.core. Symbols . defn.QuotedRuntimePatterns_higherOrderHole ) =>
216
+ if pattern.symbol.eq(defn.QuotedRuntimePatterns_higherOrderHole ) =>
218
217
219
218
def bodyFn (lambdaArgs : List [Tree ]): Tree = {
220
219
val argsMap = args.map(_.symbol).zip(lambdaArgs.asInstanceOf [List [Tree ]]).toMap
@@ -236,7 +235,7 @@ object Matcher {
236
235
ctx.owner,
237
236
MethodType (names)(
238
237
_ => argTypes, _ => resType),
239
- (meth, x) => tpd. TreeOps (bodyFn(x)).changeNonLocalOwners(meth.asInstanceOf ))
238
+ (meth, x) => TreeOps (bodyFn(x)).changeNonLocalOwners(meth.asInstanceOf ))
240
239
matched(quotes.reflect.TreeMethods .asExpr(res.asInstanceOf [quotes.reflect.Tree ]))
241
240
242
241
//
@@ -323,20 +322,20 @@ object Matcher {
323
322
324
323
/* Match val */
325
324
case (scrutinee @ ValDef (_, tpt1, _), pattern @ ValDef (_, tpt2, _)) if checkValFlags() =>
326
- def rhsEnv = summon[Env ] + (scrutinee.symbol.asInstanceOf [dotc.core. Symbols . Symbol ] -> pattern.symbol.asInstanceOf [dotc.core. Symbols . Symbol ])
325
+ def rhsEnv = summon[Env ] + (scrutinee.symbol.asInstanceOf [Symbol ] -> pattern.symbol.asInstanceOf [Symbol ])
327
326
tpt1 =?= tpt2 &&& withEnv(rhsEnv)(scrutinee.rhs =?= pattern.rhs)
328
327
329
328
/* Match def */
330
329
case (scrutinee @ DefDef (_, paramss1, tpt1, _), pattern @ DefDef (_, paramss2, tpt2, _)) =>
331
330
def rhsEnv : Env =
332
- val paramSyms : List [(dotc.core. Symbols . Symbol , dotc.core. Symbols . Symbol )] =
331
+ val paramSyms : List [(Symbol , Symbol )] =
333
332
for
334
333
(clause1, clause2) <- paramss1.zip(paramss2)
335
334
(param1, param2) <- clause1.zip(clause2)
336
335
yield
337
- param1.symbol.asInstanceOf [dotc.core. Symbols . Symbol ] -> param2.symbol.asInstanceOf [dotc.core. Symbols . Symbol ]
336
+ param1.symbol.asInstanceOf [Symbol ] -> param2.symbol.asInstanceOf [Symbol ]
338
337
val oldEnv : Env = summon[Env ]
339
- val newEnv : List [(dotc.core. Symbols . Symbol , dotc.core. Symbols . Symbol )] = (scrutinee.symbol.asInstanceOf [dotc.core. Symbols . Symbol ] -> pattern.symbol.asInstanceOf [dotc.core. Symbols . Symbol ]) :: paramSyms
338
+ val newEnv : List [(Symbol , Symbol )] = (scrutinee.symbol.asInstanceOf [Symbol ] -> pattern.symbol.asInstanceOf [Symbol ]) :: paramSyms
340
339
oldEnv ++ newEnv
341
340
342
341
matchLists(paramss1, paramss2)(_ =?= _)
@@ -373,18 +372,17 @@ object Matcher {
373
372
374
373
end extension
375
374
376
- /** Does the scrutenne symbol match the pattern symbol? It matches if:
375
+ /** Does the scrutinee symbol match the pattern symbol? It matches if:
377
376
* - They are the same symbol
378
377
* - The scrutinee has is in the environment and they are equivalent
379
378
* - The scrutinee overrides the symbol of the pattern
380
379
*/
381
- private def symbolMatch (scrutineeTree : tpd.Tree , patternTree : tpd.Tree )(using Env ): Boolean =
382
- import tpd .* // TODO remove
380
+ private def symbolMatch (scrutineeTree : Tree , patternTree : Tree )(using Env ): Boolean =
383
381
val scrutinee = scrutineeTree.symbol
384
382
385
- def overridingSymbol (ofclazz : dotc.core. Symbols . Symbol ): dotc.core. Symbols . Symbol =
383
+ def overridingSymbol (ofclazz : Symbol ): Symbol =
386
384
if ofclazz.isClass then scrutinee.denot.overridingSymbol(ofclazz.asClass)
387
- else dotc.core. Symbols . NoSymbol
385
+ else NoSymbol
388
386
389
387
val devirtualizedScrutinee = scrutineeTree match
390
388
case Select (qual, _) =>
@@ -401,13 +399,11 @@ object Matcher {
401
399
402
400
private object ClosedPatternTerm {
403
401
/** Matches a term that does not contain free variables defined in the pattern (i.e. not defined in `Env`) */
404
- def unapply (term : tpd. Tree )(using Env ): Option [term.type ] =
402
+ def unapply (term : Tree )(using Env ): Option [term.type ] =
405
403
if freePatternVars(term).isEmpty then Some (term) else None
406
404
407
405
/** Return all free variables of the term defined in the pattern (i.e. defined in `Env`) */
408
- def freePatternVars (term : dotc.ast.tpd.Tree )(using env : Env ): Set [dotc.core.Symbols .Symbol ] =
409
- import dotc .ast .tpd .* // TODO remove
410
- import dotc .core .Symbols .* // TODO remove
406
+ def freePatternVars (term : Tree )(using env : Env ): Set [Symbol ] =
411
407
val accumulator = new TreeAccumulator [Set [Symbol ]] {
412
408
def apply (x : Set [Symbol ], tree : Tree )(using Context ): Set [Symbol ] =
413
409
tree match
0 commit comments