-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4167e54
commit e488e10
Showing
12 changed files
with
156 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
macro generated main | ||
executed in: Test_2$package$Bar$macro$1$ | ||
macro generated main | ||
executed in: Test_2$package$Bar$macro$2$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import scala.annotation.{experimental, MacroAnnotation} | ||
import scala.quoted._ | ||
import scala.collection.mutable | ||
|
||
@experimental | ||
class addClass extends MacroAnnotation: | ||
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = | ||
import quotes.reflect._ | ||
tree match | ||
case DefDef(name, List(TermParamClause(Nil)), tpt, Some(rhs)) => | ||
val parents = List(TypeTree.of[Object]) | ||
def decls(cls: Symbol): List[Symbol] = | ||
List(Symbol.newMethod(cls, "run", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit]), Flags.EmptyFlags, Symbol.noSymbol)) | ||
|
||
val mod = Symbol.newModule(Symbol.spliceOwner, Symbol.freshName("Bar"), Flags.EmptyFlags, Flags.EmptyFlags, parents.map(_.tpe), decls, Symbol.noSymbol) | ||
val cls = mod.moduleClass | ||
|
||
val runSym = cls.declaredMethod("run").head | ||
|
||
val runDef = DefDef(runSym, _ => Some(rhs)) | ||
|
||
val clsDef = ClassDef(cls, parents, body = List(runDef)) | ||
|
||
val newCls = Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil) | ||
val modVal = ValDef(mod, Some(newCls)) | ||
|
||
val newDef = DefDef.copy(tree)(name, List(TermParamClause(Nil)), tpt, Some(Apply(Select(Ref(mod), runSym), Nil))) | ||
List(modVal, clsDef, newDef) | ||
case _ => | ||
report.error("Annotation only supports `def` with one argument") | ||
List(tree) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@addClass def foo(): Unit = | ||
println("macro generated main") | ||
println("executed in: " + (new Throwable().getStackTrace().head.getClassName)) | ||
//> object Baz$macro$1 { | ||
//> def run() = | ||
//> println("macro generated main") | ||
//> println("executed in: " + getClass.getName) | ||
//> } | ||
//> def foo(): Unit = | ||
//> Baz$macro$1.run | ||
|
||
@addClass def bar(): Unit = | ||
println("macro generated main") | ||
println("executed in: " + (new Throwable().getStackTrace().head.getClassName)) | ||
//> object Baz$macro$2 { | ||
//> def run() = | ||
//> println("macro generated main") | ||
//> println("executed in: " + getClass.getName) | ||
//> } | ||
//> def foo(): Unit = | ||
//> Baz$macro$2.run | ||
|
||
object Bar: | ||
def run() = () | ||
|
||
@main def Test(): Unit = | ||
foo() | ||
bar() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
macro generated main | ||
executed in: Test_2$package$Baz$2 | ||
executed in: Test_2$package$Baz$2$ | ||
macro generated main | ||
executed in: Test_2$package$Baz$4 | ||
executed in: Test_2$package$Baz$4$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
macro generated main | ||
executed in: Foo$Bar$macro$1$ | ||
macro generated main | ||
executed in: Foo$Bar$macro$2$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import scala.annotation.{experimental, MacroAnnotation} | ||
import scala.quoted._ | ||
import scala.collection.mutable | ||
|
||
@experimental | ||
class addClass extends MacroAnnotation: | ||
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = | ||
import quotes.reflect._ | ||
tree match | ||
case DefDef(name, List(TermParamClause(Nil)), tpt, Some(rhs)) => | ||
val parents = List(TypeTree.of[Object]) | ||
def decls(cls: Symbol): List[Symbol] = | ||
List(Symbol.newMethod(cls, "run", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit]), Flags.EmptyFlags, Symbol.noSymbol)) | ||
|
||
val mod = Symbol.newModule(Symbol.spliceOwner, Symbol.freshName("Bar"), Flags.EmptyFlags, Flags.EmptyFlags, parents.map(_.tpe), decls, Symbol.noSymbol) | ||
val cls = mod.moduleClass | ||
|
||
val runSym = cls.declaredMethod("run").head | ||
|
||
val runDef = DefDef(runSym, _ => Some(rhs)) | ||
|
||
val clsDef = ClassDef(cls, parents, body = List(runDef)) | ||
|
||
val newCls = Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil) | ||
val modVal = ValDef(mod, Some(newCls)) | ||
|
||
val newDef = DefDef.copy(tree)(name, List(TermParamClause(Nil)), tpt, Some(Apply(Select(Ref(mod), runSym), Nil))) | ||
List(modVal, clsDef, newDef) | ||
case _ => | ||
report.error("Annotation only supports `def` with one argument") | ||
List(tree) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Foo(): | ||
@addClass def foo(): Unit = | ||
println("macro generated main") | ||
println("executed in: " + (new Throwable().getStackTrace().head.getClassName)) | ||
//> object Baz$macro$1 { | ||
//> def run() = | ||
//> println("macro generated main") | ||
//> println("executed in: " + getClass.getName) | ||
//> } | ||
//> def foo(): Unit = | ||
//> Baz$macro$1.run | ||
|
||
@addClass def bar(): Unit = | ||
println("macro generated main") | ||
println("executed in: " + (new Throwable().getStackTrace().head.getClassName)) | ||
//> object Baz$macro$2 { | ||
//> def run() = | ||
//> println("macro generated main") | ||
//> println("executed in: " + getClass.getName) | ||
//> } | ||
//> def foo(): Unit = | ||
//> Baz$macro$2.run | ||
|
||
@main def Test(): Unit = | ||
new Foo().foo() | ||
new Foo().bar() |