Skip to content

Basic Design of Macro Annotations #15626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

Ang9876
Copy link
Contributor

@Ang9876 Ang9876 commented Jul 8, 2022

No description provided.

ProblemFilters.exclude[MissingClassProblem]("scala.annotation.since"),

// Macro annotations
ProblemFilters.exclude[MissingClassProblem]("scala.quoted.annotation.MacroAnnotation"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this?

@prolativ prolativ requested a review from nicolasstucki July 18, 2022 08:55
@Ang9876 Ang9876 marked this pull request as ready for review August 6, 2022 08:39
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 22, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 22, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 22, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 23, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 23, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 24, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 24, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 24, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 24, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 24, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 25, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 28, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 28, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.TastyAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 30, 2022
Add basic support for TASTy annotations.

* Introduce experimental `scala.annotations.TastyAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Nov 30, 2022
Add basic support for TASTy annotations.

* Introduce experimental `scala.annotations.TastyAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Dec 1, 2022
Add basic support for TASTy annotations.

* Introduce experimental `scala.annotations.TastyAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Dec 1, 2022
Add basic support for TASTy annotations.

* Introduce experimental `scala.annotations.TastyAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Dec 5, 2022
Add basic support for TASTy annotations.

* Introduce experimental `scala.annotations.TastyAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Dec 5, 2022
Add basic support for TASTy annotations.

* Introduce experimental `scala.annotations.TastyAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Dec 5, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Dec 7, 2022
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
odersky added a commit that referenced this pull request Dec 12, 2022
#### Add basic support for macro annotations

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at `Inlining` phase 
* Can use macro annotations in staged expressions (expanded when at
stage 0)
    * Can use staged expression to implement macro annotations 
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened in following PRs)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

#### Example
```scala
class memoize extends MacroAnnotation:
  def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
    import quotes.reflect._
    tree match
      case DefDef(name, TermParamClause(param :: Nil) :: Nil, tpt, Some(rhsTree)) =>
        (Ref(param.symbol).asExpr, rhsTree.asExpr) match
          case ('{ $paramRefExpr: t }, '{ $rhsExpr: u }) =>
            val cacheTpe = TypeRepr.of[Map[t, u]]
            val cacheSymbol = 
              Symbol.newVal(tree.symbol.owner, name + "Cache", cacheTpe, Flags.Private, Symbol.noSymbol)
            val cacheRhs = '{ Map.empty[t, u] }.asTerm
            val cacheVal = ValDef(cacheSymbol, Some(cacheRhs))
            val cacheRefExpr = Ref(cacheSymbol).asExprOf[Map[t, u]]
            val newRhs = '{ $cacheRefExpr.getOrElseUpdate($paramRefExpr, $rhsExpr) }.asTerm
            val newTree = DefDef.copy(tree)(name, TermParamClause(param :: Nil) :: Nil, tpt, Some(newRhs))
            List(cacheVal, newTree)
      case _ =>
        report.error("Annotation only supported on `def` with a single argument are supported")
        List(tree)
```

with this macro annotation a user can write
```scala
@memoize
def fib(n: Int): Int =
  println(s"compute fib of $n")
  if n <= 1 then n else fib(n - 1) + fib(n - 2)
```
and the macro will modify the definition to create
```scala
val fibCache = mutable.Map.empty[Int, Int]
def fib(n: Int): Int =
  fibCache.getOrElseUpdate(
    n,
    {
      println(s"compute fib of $n")
      if n <= 1 then n else fib(n - 1) + fib(n - 2)
    }
  )
```

#### Based on
* #15626
* https://infoscience.epfl.ch/record/294615?ln=en

#### Followed by
* #16454
little-inferno pushed a commit to little-inferno/dotty that referenced this pull request Jan 25, 2023
Add basic support for macro annotations.

* Introduce experimental `scala.annotations.MacroAnnotation`
* Macro annotations can analyze or modify definitions
* Macro annotation can add definition around the annotated definition
  * Added members are not visible while typing
  * Added members are not visible to other macro annotations
  * Added definition must have the same owner
* Implement macro annotation expansion
  * Implemented at Inlining phase
    * Can use macro annotations in staged expressions (expanded when at stage 0)
    * Can use staged expression to implement macro annotations
    * Can insert calls to inline methods in macro annotations
  * Current limitations (to be loosened)
    * Can only be used on `def`, `val`, `lazy val` and `var`
    * Can only add `def`, `val`, `lazy val` and `var` definitions

Based on:
  * scala#15626
  * https://infoscience.epfl.ch/record/294615?ln=en
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants