Skip to content

Commit

Permalink
Merge pull request #432 from sjrd/cleanup-boilerplate
Browse files Browse the repository at this point in the history
Clean up boilerplate
  • Loading branch information
sjrd authored Dec 21, 2023
2 parents 3346833 + 0ed0945 commit 0a89f00
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 368 deletions.
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ lazy val tastyQuery =
mimaBinaryIssueFilters ++= {
import com.typesafe.tools.mima.core.*
Seq(
// private[tastyquery], not an issue
ProblemFilters.exclude[MissingClassProblem]("tastyquery.Utils"),
ProblemFilters.exclude[MissingClassProblem]("tastyquery.Utils$"),
// Everything in tastyquery.reader is private[tastyquery] at most
ProblemFilters.exclude[Problem]("tastyquery.reader.*"),
)
Expand Down
45 changes: 22 additions & 23 deletions tasty-query/shared/src/main/scala/tastyquery/Annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import tastyquery.Names.*
import tastyquery.Symbols.*
import tastyquery.Trees.*
import tastyquery.Types.*
import tastyquery.Utils.*

object Annotations:
final class Annotation(val tree: TermTree):
Expand All @@ -18,28 +19,31 @@ object Annotations:

/** The annotation class symbol. */
def symbol(using Context): ClassSymbol =
val local = mySymbol
if local != null then local
else
val computed = computeAnnotSymbol(tree)
mySymbol = computed
mySafeSymbol = Some(computed)
computed
memoized(
mySymbol,
{ computed =>
mySymbol = computed
mySafeSymbol = Some(computed)
}
) {
computeAnnotSymbol(tree)
}
end symbol

/** Tests whether the annotation has the given class symbol.
*
* If the class of this annotation cannot be successfully resolved, returns `false`.
*/
private[tastyquery] def safeHasSymbol(cls: ClassSymbol)(using Context): Boolean =
val safeSymbol =
val local = mySafeSymbol
if local != null then local
else
val local = computeSafeAnnotSymbol(tree)
local.foreach(mySymbol = _)
mySafeSymbol = local
local
val safeSymbol = memoized(
mySafeSymbol,
{ computed =>
computed.foreach(mySymbol = _)
mySafeSymbol = computed
}
) {
computeSafeAnnotSymbol(tree)
}

safeSymbol.contains(cls)
end safeHasSymbol
Expand All @@ -60,14 +64,9 @@ object Annotations:
* `NamedArg`s are not visible with this method. They are replaced by
* their right-hand-side.
*/
def arguments: List[TermTree] =
val local = myArguments
if local != null then local
else
val computed = computeAnnotArguments(tree)
myArguments = computed
computed
end arguments
def arguments: List[TermTree] = memoized(myArguments, myArguments = _) {
computeAnnotArguments(tree)
}

def argCount: Int = arguments.size

Expand Down
48 changes: 24 additions & 24 deletions tasty-query/shared/src/main/scala/tastyquery/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ final class Definitions private[tastyquery] (ctx: Context, rootPackage: PackageS

private def createSpecialClass(name: SimpleTypeName, parents: List[Type], flags: FlagSet): ClassSymbol =
val cls = ClassSymbol.create(name, scalaPackage)
cls.withTypeParams(Nil)
cls.withParentsDirect(parents)
cls.withGivenSelfType(None)
cls.withFlags(flags, None)
cls.setTypeParams(Nil)
cls.setParentsDirect(parents)
cls.setGivenSelfType(None)
cls.setFlags(flags, None)
cls.setAnnotations(Nil)
cls.checkCompleted()
cls
Expand All @@ -115,8 +115,8 @@ final class Definitions private[tastyquery] (ctx: Context, rootPackage: PackageS
): TermSymbol =
val sym = TermSymbol
.create(name, owner)
.withFlags(Method | flags, privateWithin = None)
.withDeclaredType(tpe)
.setFlags(Method | flags, privateWithin = None)
.setDeclaredType(tpe)
.setAnnotations(Nil)
.autoFillParamSymss(termParamFlags)
sym.paramSymss.foreach(_.merge.foreach(_.setAnnotations(Nil)))
Expand All @@ -139,17 +139,17 @@ final class Definitions private[tastyquery] (ctx: Context, rootPackage: PackageS
private[tastyquery] val scala2FakeOwner: TermSymbol =
TermSymbol
.createNotDeclaration(termName("<nosymbol>"), scalaPackage)
.withFlags(Synthetic, None)
.withDeclaredType(AnyType)
.setFlags(Synthetic, None)
.setDeclaredType(AnyType)
.setAnnotations(Nil)
.checkCompleted()
end scala2FakeOwner

private[tastyquery] val scala2MacroInfoFakeMethod: TermSymbol =
TermSymbol
.createNotDeclaration(nme.m_macro, scalaPackage)
.withFlags(Synthetic, None)
.withDeclaredType(NothingType)
.setFlags(Synthetic, None)
.setDeclaredType(NothingType)
.setAnnotations(Nil)
.checkCompleted()
end scala2MacroInfoFakeMethod
Expand All @@ -161,8 +161,8 @@ final class Definitions private[tastyquery] (ctx: Context, rootPackage: PackageS
alias: Type
): TypeMemberSymbol =
val sym = TypeMemberSymbol.create(name, owner)
sym.withFlags(flags, None)
sym.withDefinition(TypeMemberDefinition.TypeAlias(alias))
sym.setFlags(flags, None)
sym.setDefinition(TypeMemberDefinition.TypeAlias(alias))
sym.setAnnotations(Nil)
sym.checkCompleted()
sym
Expand All @@ -177,8 +177,8 @@ final class Definitions private[tastyquery] (ctx: Context, rootPackage: PackageS
val andOrParamNames = List(typeName("A"), typeName("B"))

val andTypeAlias = TypeMemberSymbol.create(typeName("&"), scalaPackage)
andTypeAlias.withFlags(EmptyFlagSet, None)
andTypeAlias.withDefinition(
andTypeAlias.setFlags(EmptyFlagSet, None)
andTypeAlias.setDefinition(
TypeMemberDefinition.TypeAlias(
TypeLambda(andOrParamNames)(
pt => List(NothingAnyBounds, NothingAnyBounds),
Expand All @@ -190,8 +190,8 @@ final class Definitions private[tastyquery] (ctx: Context, rootPackage: PackageS
andTypeAlias.checkCompleted()

val orTypeAlias = TypeMemberSymbol.create(typeName("|"), scalaPackage)
orTypeAlias.withFlags(EmptyFlagSet, None)
orTypeAlias.withDefinition(
orTypeAlias.setFlags(EmptyFlagSet, None)
orTypeAlias.setDefinition(
TypeMemberDefinition.TypeAlias(
TypeLambda(andOrParamNames)(
pt => List(NothingAnyBounds, NothingAnyBounds),
Expand Down Expand Up @@ -364,32 +364,32 @@ final class Definitions private[tastyquery] (ctx: Context, rootPackage: PackageS
val name = typeName("ContextFunction" + n)
val cls = ClassSymbol.create(name, scalaPackage)

cls.withFlags(Trait | NoInitsInterface, None)
cls.withParentsDirect(ObjectType :: Nil)
cls.setFlags(Trait | NoInitsInterface, None)
cls.setParentsDirect(ObjectType :: Nil)
cls.setAnnotations(Nil)
cls.withGivenSelfType(None)
cls.setGivenSelfType(None)

val inputTypeParams = List.tabulate(n) { i =>
ClassTypeParamSymbol
.create(typeName("T" + i), cls)
.withFlags(Contravariant, None)
.setFlags(Contravariant, None)
.setDeclaredBounds(NothingAnyBounds)
.setAnnotations(Nil)
}
val resultTypeParam =
ClassTypeParamSymbol
.create(typeName("R"), cls)
.withFlags(Covariant, None)
.setFlags(Covariant, None)
.setDeclaredBounds(NothingAnyBounds)
.setAnnotations(Nil)

val allTypeParams = inputTypeParams :+ resultTypeParam
allTypeParams.foreach(_.checkCompleted())
cls.withTypeParams(allTypeParams)
cls.setTypeParams(allTypeParams)

val applyMethod = TermSymbol.create(termName("apply"), cls)
applyMethod.withFlags(Method | Abstract, None)
applyMethod.withDeclaredType(
applyMethod.setFlags(Method | Abstract, None)
applyMethod.setDeclaredType(
MethodType(List.tabulate(n)(i => termName("x" + i)))(
mt => inputTypeParams.map(_.localRef),
mt => resultTypeParam.localRef
Expand Down
Loading

0 comments on commit 0a89f00

Please sign in to comment.