/
PositionMacro.scala
87 lines (74 loc) · 3.55 KB
/
PositionMacro.scala
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
/*
* Copyright 2001-2019 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalactic.source
import org.scalactic.MacroContext.Context
import org.scalactic.Resources
/**
* Helper class for Position macro. (Will be removed from the public API if possible in a subsequent 3.0.0-RCx release.)
*/
object PositionMacro {
private object sourceCompatHack {
object internal { object decorators {} }
}
import sourceCompatHack._
private[scalactic] lazy val showScalacticFillFilePathnames: Boolean = {
val value = System.getenv("SCALACTIC_FILL_FILE_PATHNAMES")
value != null && value == "yes"
}
private class PositionMacroImpl(val universe: scala.reflect.api.Universe) {
private val PositionModule = universe.rootMirror.staticModule("org.scalactic.source.Position")
private val PositionClass = universe.rootMirror.staticClass("org.scalactic.source.Position")
private val Position_apply = PositionModule.typeSignature.declaration(universe.newTermName("apply"))
def apply(context: Context): context.Tree = {
import context.universe._
// On 2.10, this imports the empty object defined above
// On 2.11+, this imports `Tree.setType` extension method
import internal.decorators._
// Our types don't capture the fact that context.universe eq this.universe, so we have to cast.
implicit def castSymbol(s: universe.Symbol): context.universe.Symbol = s.asInstanceOf[context.universe.Symbol]
implicit def castType(t: universe.Type): context.universe.Type = t.asInstanceOf[context.universe.Type]
import treeBuild.{mkAttributedIdent, mkAttributedSelect}
// Because this macro is called so frequently, we assign types as we build them, rather
// than returning an untyped tree and letting the typechecker fill in the types.
// (`setType` and `mkAttributedXxx` assign Tree.tpe.)
def strLit(s: String) = {
Literal(Constant(s)).setType(definitions.StringClass.toTypeConstructor)
}
def intLit(i: Int) = {
Literal(Constant(i)).setType(definitions.IntTpe)
}
val args = List(
strLit(context.enclosingPosition.source.file.name),
if (showScalacticFillFilePathnames) strLit(context.enclosingPosition.source.path) else strLit(Resources.pleaseDefineScalacticFillFilePathnameEnvVar),
intLit(context.enclosingPosition.line)
)
Apply(mkAttributedSelect(mkAttributedIdent(PositionModule), Position_apply), args).setType(PositionClass.toTypeConstructor)
}
}
// Cache to avoid needing to lookup of PositionModule/PositionClass/Position_apply each macro expansion.
private var cache = new java.lang.ref.WeakReference[PositionMacroImpl](null)
/**
* Helper method for Position macro.
*/
def genPosition(context: Context): context.Expr[Position] = {
var impl = cache.get()
if (impl == null || impl.universe != context.universe) {
impl = new PositionMacroImpl(context.universe)
cache = new java.lang.ref.WeakReference[PositionMacroImpl](impl)
}
context.Expr[Position](impl.apply(context))
}
}