Repository navigation
Expand file tree
/
Copy pathStructuralRuntimeHashcode.scala
More file actions
107 lines (90 loc) · 3.1 KB
/
Copy pathStructuralRuntimeHashcode.scala
File metadata and controls
107 lines (90 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package org.encalmo.utils
import scala.quoted.Quotes
import scala.quoted.Expr
import scala.quoted.Type
import org.encalmo.utils.AnnotationUtils.AnnotationInfo
/** Compute the structural runtime hashcode of a value. It will take into account structure and actual types of the
* nested values in runtime but not the values themselves.
*/
object StructuralRuntimeHashcode {
private inline def debugMode = false
inline def compute[T](value: T): Int = {
${ computeImpl[T]('{ value }) }
}
private def computeImpl[T: Type](expr: Expr[T])(using Quotes): Expr[Int] = {
import cache.quotes.reflect.*
given cache: StatementsCache = new StatementsCache
computeUsingTypeTreeIterator(using cache)(TypeRepr.of[T], expr.asTerm)
cache.asExprOf[Int]
}
private def computeUsingTypeTreeIterator(using
cache: StatementsCache
)(
tpe: cache.quotes.reflect.TypeRepr,
valueTerm: cache.quotes.reflect.Term
): Unit = {
val context = new StructuralRuntimeHashcodeMacroContext
val trace = scala.collection.mutable.Buffer.empty[String]
context.initialize(using cache)
TypeTreeIterator.visitNode(using cache, StructuralRuntimeHashcodeMacroVisitor)(
tpe = tpe,
valueTerm = valueTerm,
context = context,
isCollectionItem = false,
annotations = Set.empty,
trace = trace,
debugIndent = if debugMode then 0 else Int.MinValue
)
context.finalize(using cache)
import cache.quotes.reflect.*
if debugMode then {
report.warning(
trace.mkString("\n")
+ "\n\n--------------------------------"
+ "\ngenerated code:\n\n"
+ cache.asTerm.show(using Printer.TreeCode)
)
}
}
}
class StructuralRuntimeHashcodeMacroContext {
def initialize(using cache: StatementsCache): Unit = {
given cache.quotes.type = cache.quotes
cache.getValueRefOfExpr[Hashcode]("hashcode", '{ new Hashcode() })
}
/** Update hashcode by each value's type name encountered in the type tree. */
def updateHashcode(using cache: StatementsCache)(valueTerm: cache.quotes.reflect.Term): Unit = {
cache.put(
cache
.getValueRef("hashcode")
.methodCall(
"update",
List(
valueTerm
.maybeMethodCall("getClass", List())
.map(_.methodCall("getName", List()))
.getOrElse(valueTerm.applyToString)
)
)
)
}
def finalize(using cache: StatementsCache): Unit = {
cache.put(cache.getValueRef("hashcode").methodCall("result", List()))
}
override def toString(): String = ""
}
object StructuralRuntimeHashcodeMacroVisitor extends SimpleTypeTreeVisitor {
type Context = StructuralRuntimeHashcodeMacroContext
/** Before visiting a node in the type tree. */
inline override def beforeNode(using
cache: StatementsCache
)(
tpe: cache.quotes.reflect.TypeRepr,
valueTerm: cache.quotes.reflect.Term,
annotations: Set[AnnotationInfo],
isCollectionItem: Boolean,
context: Context
): (Context, Set[AnnotationInfo]) =
context.updateHashcode(valueTerm)
(context, annotations)
}