@@ -2,7 +2,6 @@ package sourcecode
2
2
3
3
import scala .language .implicitConversions
4
4
import scala .quoted ._
5
- import scala .tasty .Reflection
6
5
7
6
trait NameMacros {
8
7
inline implicit def generate : Name =
@@ -65,28 +64,26 @@ trait ArgsMacros {
65
64
}
66
65
67
66
object Util {
68
- def isSynthetic (c : Reflection )(s : c. Symbol ) = isSyntheticName(getName(c) (s))
67
+ def isSynthetic (using Quotes )(s : quotes.reflect. Symbol ) = isSyntheticName(getName(s))
69
68
def isSyntheticName (name : String ) = {
70
69
name == " <init>" || (name.startsWith(" <local " ) && name.endsWith(" >" )) || name == " $anonfun" || name == " macro"
71
70
}
72
- def getName (c : Reflection )(s : c.Symbol ) = {
73
- import c .{given _ }
71
+ def getName (using Quotes )(s : quotes.reflect.Symbol ) = {
74
72
s.name.trim
75
73
.stripSuffix(" $" ) // meh
76
74
}
77
75
}
78
76
79
77
object Macros {
80
78
81
- def findOwner (c : Reflection )(owner : c.Symbol , skipIf : (c : Reflection ) => (c.Symbol ) => Boolean ): c.Symbol = {
82
- import c .{given _ }
79
+ def findOwner (using Quotes )(owner : quotes.reflect.Symbol , skipIf : quotes.reflect.Symbol => Boolean ): quotes.reflect.Symbol = {
83
80
var owner0 = owner
84
- while (skipIf(c)( owner0)) owner0 = owner0.owner
81
+ while (skipIf(owner0)) owner0 = owner0.owner
85
82
owner0
86
83
}
87
84
88
- def actualOwner (c : Reflection )(owner : c. Symbol ): c .Symbol =
89
- findOwner(c)( owner, c => owner0 => Util .isSynthetic(c)( owner0) || Util .getName(c) (owner0) == " ev" )
85
+ def actualOwner (using Quotes )(owner : quotes.reflect. Symbol ): quotes.reflect .Symbol =
86
+ findOwner(owner, owner0 => Util .isSynthetic(owner0) || Util .getName(owner0) == " ev" )
90
87
91
88
/**
92
89
* In Scala 3, macro `mcr()` is expanded to:
@@ -97,13 +94,13 @@ object Macros {
97
94
* Where n is an ordinal. This method returns the first owner that is not
98
95
* such a synthetic variable.
99
96
*/
100
- def nonMacroOwner (c : Reflection )(owner : c. Symbol ): c .Symbol =
101
- findOwner(c)( owner, c => owner0 => {import c .{ given _ }; owner0.flags.is(c. Flags .Macro ) && Util .getName(c) (owner0) == " macro" })
97
+ def nonMacroOwner (using Quotes )(owner : quotes.reflect. Symbol ): quotes.reflect .Symbol =
98
+ findOwner(owner, owner0 => { owner0.flags.is(quotes.reflect. Flags .Macro ) && Util .getName(owner0) == " macro" })
102
99
103
- def nameImpl (using ctx : QuoteContext ): Expr [Name ] = {
104
- import ctx . tasty ._
105
- val owner = actualOwner(ctx.tasty)( Symbol .currentOwner )
106
- val simpleName = Util .getName(ctx.tasty)( owner)
100
+ def nameImpl (using Quotes ): Expr [Name ] = {
101
+ import quotes . reflect ._
102
+ val owner = actualOwner(Symbol .spliceOwner )
103
+ val simpleName = Util .getName(owner)
107
104
' {Name ($ {Expr (simpleName)})}
108
105
}
109
106
@@ -114,20 +111,20 @@ object Macros {
114
111
else
115
112
s
116
113
117
- def nameMachineImpl (using ctx : QuoteContext ): Expr [Name .Machine ] = {
118
- import ctx . tasty ._
119
- val owner = nonMacroOwner(ctx.tasty)( Symbol .currentOwner )
120
- val simpleName = adjustName(Util .getName(ctx.tasty)( owner))
114
+ def nameMachineImpl (using Quotes ): Expr [Name .Machine ] = {
115
+ import quotes . reflect ._
116
+ val owner = nonMacroOwner(Symbol .spliceOwner )
117
+ val simpleName = adjustName(Util .getName(owner))
121
118
' {Name .Machine ($ {Expr (simpleName)})}
122
119
}
123
120
124
- def fullNameImpl (using ctx : QuoteContext ): Expr [FullName ] = {
125
- import ctx . tasty ._
121
+ def fullNameImpl (using Quotes ): Expr [FullName ] = {
122
+ import quotes . reflect ._
126
123
@ annotation.tailrec def cleanChunk (chunk : String ): String =
127
124
val refined = chunk.stripPrefix(" _$" ).stripSuffix(" $" )
128
125
if chunk != refined then cleanChunk(refined) else refined
129
126
130
- val owner = actualOwner(ctx.tasty)( Symbol .currentOwner )
127
+ val owner = actualOwner(Symbol .spliceOwner )
131
128
val fullName =
132
129
owner.fullName.trim
133
130
.split(" \\ ." , - 1 )
@@ -137,9 +134,9 @@ object Macros {
137
134
' {FullName ($ {Expr (fullName)})}
138
135
}
139
136
140
- def fullNameMachineImpl (using ctx : QuoteContext ): Expr [FullName .Machine ] = {
141
- import ctx . tasty ._
142
- val owner = nonMacroOwner(ctx.tasty)( Symbol .currentOwner )
137
+ def fullNameMachineImpl (using Quotes ): Expr [FullName .Machine ] = {
138
+ import quotes . reflect ._
139
+ val owner = nonMacroOwner(Symbol .spliceOwner )
143
140
val fullName = owner.fullName.trim
144
141
.split(" \\ ." , - 1 )
145
142
.map(_.stripPrefix(" _$" ).stripSuffix(" $" )) // meh
@@ -148,52 +145,47 @@ object Macros {
148
145
' {FullName .Machine ($ {Expr (fullName)})}
149
146
}
150
147
151
- def fileImpl (using ctx : QuoteContext ): Expr [sourcecode.File ] = {
152
- import ctx . tasty ._
153
- val file = ctx.tasty.rootPosition .sourceFile.jpath.toAbsolutePath.toString
148
+ def fileImpl (using Quotes ): Expr [sourcecode.File ] = {
149
+ import quotes . reflect ._
150
+ val file = quotes.reflect. Position .ofMacroExpansion .sourceFile.jpath.toAbsolutePath.toString
154
151
' {sourcecode.File ($ {Expr (file)})}
155
152
}
156
153
157
- def fileNameImpl (using ctx : QuoteContext ): Expr [sourcecode.FileName ] = {
158
- import ctx .tasty ._
159
- val name = ctx.tasty.rootPosition.sourceFile.jpath.getFileName.toString
154
+ def fileNameImpl (using Quotes ): Expr [sourcecode.FileName ] = {
155
+ val name = quotes.reflect.Position .ofMacroExpansion.sourceFile.jpath.getFileName.toString
160
156
' {sourcecode.FileName ($ {Expr (name)})}
161
157
}
162
158
163
- def lineImpl (using ctx : QuoteContext ): Expr [sourcecode.Line ] = {
164
- import ctx .tasty ._
165
- val line = ctx.tasty.rootPosition.startLine + 1
159
+ def lineImpl (using Quotes ): Expr [sourcecode.Line ] = {
160
+ val line = quotes.reflect.Position .ofMacroExpansion.startLine + 1
166
161
' {sourcecode.Line ($ {Expr (line)})}
167
162
}
168
163
169
- def enclosingImpl (using ctx : QuoteContext ): Expr [Enclosing ] = {
170
- val path = enclosing(ctx.tasty)(
171
- ! Util .isSynthetic(ctx.tasty)(_)
172
- )
173
-
164
+ def enclosingImpl (using Quotes ): Expr [Enclosing ] = {
165
+ import quotes .reflect ._
166
+ val path = enclosing(machine = false )(! Util .isSynthetic(_))
174
167
' {Enclosing ($ {Expr (path)})}
175
168
}
176
169
177
- def enclosingMachineImpl (using ctx : QuoteContext ): Expr [Enclosing .Machine ] = {
178
- val path = enclosing(ctx.tasty, machine = true )(_ => true )
170
+ def enclosingMachineImpl (using Quotes ): Expr [Enclosing .Machine ] = {
171
+ val path = enclosing(machine = true )(_ => true )
179
172
' {Enclosing .Machine ($ {Expr (path)})}
180
173
}
181
174
182
- def pkgImpl (using ctx : QuoteContext ): Expr [Pkg ] = {
183
- import ctx .tasty ._
184
- val path = enclosing(ctx.tasty) {
175
+ def pkgImpl (using Quotes ): Expr [Pkg ] = {
176
+ val path = enclosing(machine = false ) {
185
177
case s if s.isPackageDef => true
186
178
case _ => false
187
179
}
188
180
189
181
' {Pkg ($ {Expr (path)})}
190
182
}
191
183
192
- def argsImpl (using ctx : QuoteContext ): Expr [Args ] = {
193
- import ctx . tasty ._
184
+ def argsImpl (using qctx : Quotes ): Expr [Args ] = {
185
+ import quotes . reflect ._
194
186
195
- val param : List [List [ctx.tasty. ValDef ]] = {
196
- def nearestEnclosingMethod (owner : ctx.tasty. Symbol ): List [List [ctx.tasty. ValDef ]] =
187
+ val param : List [List [ValDef ]] = {
188
+ def nearestEnclosingMethod (owner : Symbol ): List [List [ValDef ]] =
197
189
owner match {
198
190
case defSym if defSym.isDefDef =>
199
191
defSym.tree.asInstanceOf [DefDef ].paramss
@@ -203,12 +195,12 @@ object Macros {
203
195
nearestEnclosingMethod(owner.owner)
204
196
}
205
197
206
- nearestEnclosingMethod(Symbol .currentOwner )
198
+ nearestEnclosingMethod(Symbol .spliceOwner )
207
199
}
208
200
209
201
val texts0 = param.map(_.foldRight(' {List .empty[Text [_]]}) {
210
202
case (vd @ ValDef (nme, _, optV), l) =>
211
- ' {Text ($ {optV.fold(' None )(_.seal )}, $ {Expr (nme)}) :: $l}
203
+ ' {Text ($ {optV.fold(' None )(_.asExpr )}, $ {Expr (nme)}) :: $l}
212
204
})
213
205
val texts = texts0.foldRight(' {List .empty[List [Text [_]]]}) {
214
206
case (l, acc) =>
@@ -219,9 +211,9 @@ object Macros {
219
211
}
220
212
221
213
222
- def text [T : Type ](v : Expr [T ])(using ctx : QuoteContext ): Expr [sourcecode.Text [T ]] = {
223
- import ctx . tasty ._
224
- val txt = v.unseal .pos.sourceCode
214
+ def text [T : Type ](v : Expr [T ])(using Quotes ): Expr [sourcecode.Text [T ]] = {
215
+ import quotes . reflect ._
216
+ val txt = Term .of(v) .pos.sourceCode
225
217
' {sourcecode.Text [T ]($v, $ {Expr (txt)})}
226
218
}
227
219
@@ -233,14 +225,14 @@ object Macros {
233
225
234
226
}
235
227
236
- def enclosing (c : Reflection , machine : Boolean = false )(filter : c .Symbol => Boolean ): String = {
237
- import c ._
228
+ def enclosing (using Quotes )( machine : Boolean )(filter : quotes.reflect .Symbol => Boolean ): String = {
229
+ import quotes . reflect ._
238
230
239
- var current = Symbol .currentOwner
231
+ var current = Symbol .spliceOwner
240
232
if (! machine)
241
- current = actualOwner(c)( current)
233
+ current = actualOwner(current)
242
234
else
243
- current = nonMacroOwner(c)( current)
235
+ current = nonMacroOwner(current)
244
236
var path = List .empty[Chunk ]
245
237
while (current != Symbol .noSymbol && current != defn.RootPackage && current != defn.RootClass ){
246
238
if (filter(current)) {
@@ -255,7 +247,7 @@ object Macros {
255
247
case _ => Chunk .PkgObj
256
248
}
257
249
258
- path = chunk(Util .getName(c)( current).stripSuffix(" $" )) :: path
250
+ path = chunk(Util .getName(current).stripSuffix(" $" )) :: path
259
251
}
260
252
current = current.owner
261
253
}
0 commit comments