8000 Directly store a LocalName inside VarRef, instead of a LocalIdent. · scala-js/scala-js@5dc4127 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5dc4127

Browse files
committed
Directly store a LocalName inside VarRef, instead of a LocalIdent.
The only point of a `LocalIdent` is to keep track of a position in addition to the local name. However, in a `VarRef`, the position should always be the same as the `VarRef` itself. Storing a separate instance of `LocalIdent` is therefore wasteful.
1 parent 3d65f79 commit 5dc4127

File tree

19 files changed

+108
-97
lines changed

19 files changed

+108
-97
lines changed

compiler/src/main/scala/org/scalajs/nscplugin/GenJSCode.scala

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,19 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
179179

180180
// Per method body
181181
private val currentMethodSym = new ScopedVar[Symbol]
182-
private val thisLocalVarIdent = new ScopedVar[Option[js.LocalIdent]]
182+
private val thisLocalVarName = new ScopedVar[Option[LocalName]]
183183
private val enclosingLabelDefInfos = new ScopedVar[Map[Symbol, EnclosingLabelDefInfo]]
184184
private val isModuleInitialized = new ScopedVar[VarBox[Boolean]]
185185
private val undefinedDefaultParams = new ScopedVar[mutable.Set[Symbol]]
186186
private val mutableLocalVars = new ScopedVar[mutable.Set[Symbol]]
187187
private val mutatedLocalVars = new ScopedVar[mutable.Set[Symbol]]
188188

189189
private def withPerMethodBodyState[A](methodSym: Symbol,
190-
initThisLocalVarIdent: Option[js.LocalIdent] = None)(body: => A): A = {
190+
initThisLocalVarName: Option[LocalName] = None)(body: => A): A = {
191191

192192
withScopedVars(
193193
currentMethodSym := methodSym,
194-
thisLocalVarIdent := initThisLocalVarIdent,
194+
thisLocalVarName := initThisLocalVarName,
195195
enclosingLabelDefInfos := Map.empty,
196196
isModuleInitialized := new VarBox(false),
197197
undefinedDefaultParams := mutable.Set.empty,
@@ -241,7 +241,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
241241
generatedSAMWrapperCount := new VarBox(0),
242242
delambdafyTargetDefDefs := mutable.Map.empty,
243243
currentMethodSym := null,
244-
thisLocalVarIdent := null,
244+
thisLocalVarName := null,
245245
enclosingLabelDefInfos := null,
246246
isModuleInitialized := null,
247247
undefinedDefaultParams := null,
@@ -999,9 +999,9 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
999999
* FIXME This could clash with a local variable of the constructor or a JS
10001000
* class capture. How do we avoid this?
10011001
*/
1002-
val selfName = freshLocalIdent("this")(pos)
1002+
val selfIdent = freshLocalIdent("this")(pos)
10031003
def selfRef(implicit pos: ir.Position) =
1004-
js.VarRef(selfName)(jstpe.AnyType)
1004+
js.VarRef(selfIdent.name)(jstpe.AnyType)
10051005

10061006
def memberLambda(params: List[js.ParamDef], restParam: Option[js.ParamDef],
10071007
body: js.Tree)(implicit pos: ir.Position) = {
@@ -1101,7 +1101,8 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
11011101
js.JSNew(jsSuperClassRef, args)
11021102
}
11031103

1104-
val selfVarDef = js.VarDef(selfName, thisOriginalName, jstpe.AnyType, mutable = false, newTree)
1104+
val selfVarDef = js.VarDef(selfIdent.copy(), // copy for the correct `pos`
1105+
thisOriginalName, jstpe.AnyType, mutable = false, newTree)
11051106
selfVarDef :: memberDefinitions
11061107
}
11071108

@@ -2040,12 +2041,12 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
20402041
* Other (normal) methods are emitted with `genMethodDef()`.
20412042
*/
20422043
def genMethodWithCurrentLocalNameScope(dd: DefDef,
2043-
initThisLocalVarIdent: Option[js.LocalIdent] = None): js.MethodDef = {
2044+
initThisLocalVarName: Option[LocalName] = None): js.MethodDef = {
20442045

20452046
implicit val pos = dd.pos
20462047
val sym = dd.symbol
20472048

2048-
withPerMethodBodyState(sym, initThisLocalVarIdent) {
2049+
withPerMethodBodyState(sym, initThisLocalVarName) {
20492050
val methodName = encodeMethodSym(sym)
20502051
val originalName = originalNameOfMethod(sym)
20512052

@@ -2112,8 +2113,8 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
21122113
methodDef
21132114
} else {
21142115
val patches = (
2115-
unmutatedMutableLocalVars.map(encodeLocalSym(_).name -> false) :::
2116-
mutatedImmutableLocalVals.map(encodeLocalSym(_).name -> true)
2116+
unmutatedMutableLocalVars.map(encodeLocalSymName(_) -> false) :::
2117+
mutatedImmutableLocalVals.map(encodeLocalSymName(_) -> true)
21172118
).toMap
21182119
patchMutableFlagOfLocals(methodDef, patches)
21192120
}
@@ -2195,15 +2196,15 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
21952196
private def patchTypeOfParamDefs(methodDef: js.MethodDef,
21962197
patches: Map[LocalName, jstpe.Type]): js.MethodDef = {
21972198

2198-
def newType(name: js.LocalIdent, oldType: jstpe.Type): jstpe.Type =
2199-
patches.getOrElse(name.name, oldType)
2199+
def newType(name: LocalName, oldType: jstpe.Type): jstpe.Type =
2200+
patches.getOrElse(name, oldType)
22002201

22012202
val js.MethodDef(flags, methodName, originalName, params, resultType, body) =
22022203
methodDef
22032204
val newParams = for {
22042205
p @ js.ParamDef(name, originalName, ptpe, mutable) <- params
22052206
} yield {
2206-
js.ParamDef(name, originalName, newType(name, ptpe), mutable)(p.pos)
2207+
js.ParamDef(name, originalName, newType(name.name, ptpe), mutable)(p.pos)
22072208
}
22082209
val transformer = new ir.Transformers.Transformer {
22092210
override def transform(tree: js.Tree): js.Tree = tree match {
@@ -2292,7 +2293,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
22922293

22932294
val innerBody = {
22942295
withScopedVars(
2295-
thisLocalVarIdent := Some(thisLocalIdent)
2296+
thisLocalVarName := Some(thisLocalIdent.name)
22962297
) {
22972298
js.Block(otherStats.map(genStat) :+ (
22982299
if (bodyIsStat) genStat(rhs)
@@ -2371,7 +2372,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
23712372

23722373
val thisLocalIdent = freshLocalIdent("this")
23732374
withScopedVars(
2374-
thisLocalVarIdent := Some(thisLocalIdent)
2375+
thisLocalVarName := Some(thisLocalIdent.name)
23752376
) {
23762377
val staticNamespace =
23772378
if (namespace.isPrivate) js.MemberNamespace.PrivateStatic
@@ -2782,7 +2783,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
27822783
case _ =>
27832784
mutatedLocalVars += sym
27842785
js.Assign(
2785-
js.VarRef(encodeLocalSym(sym))(toIRType(sym.tpe)),
2786+
js.VarRef(encodeLocalSymName(sym))(toIRType(sym.tpe)),
27862787
genRhs)
27872788
}
27882789

@@ -2832,15 +2833,14 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
28322833
* is one.
28332834
*/
28342835
private def genThis()(implicit pos: Position): js.Tree = {
2835-
thisLocalVarIdent.fold[js.Tree] {
2836+
thisLocalVarName.fold[js.Tree] {
28362837
if (tryingToGenMethodAsJSFunction) {
28372838
throw new CancelGenMethodAsJSFunction(
28382839
"Trying to generate `this` inside the body")
28392840
}
28402841
js.This()(currentThisType)
2841-
} { thisLocalIdent =>
2842-
// .copy() to get the correct position
2843-
js.VarRef(thisLocalIdent.copy())(currentThisTypeNullable)
2842+
} { thisLocalName =>
2843+
js.VarRef(thisLocalName)(currentThisTypeNullable)
28442844
}
28452845
}
28462846

@@ -3093,7 +3093,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
30933093
implicit pos: Position): js.Tree = {
30943094

30953095
val exceptIdent = freshLocalIdent("e")
3096-
val origExceptVar = js.VarRef(exceptIdent)(jstpe.AnyType)
3096+
val origExceptVar = js.VarRef(exceptIdent.name)(jstpe.AnyType)
30973097

30983098
val mightCatchJavaScriptException = catches.exists { caseDef =>
30993099
caseDef.pat match {
@@ -3486,7 +3486,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
34863486
List.newBuilder[(js.VarRef, jstpe.Type, js.LocalIdent, js.Tree)]
34873487

34883488
for ((formalArgSym, arg) <- targetSyms.zip(values)) {
3489-
val formalArg = encodeLocalSym(formalArgSym)
3489+
val formalArgName = encodeLocalSymName(formalArgSym)
34903490
val actualArg = genExpr(arg)
34913491

34923492
/* #3267 The formal argument representing the special `this` of a
@@ -3511,13 +3511,13 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
35113511
else actualArg
35123512

35133513
actualArg match {
3514-
case js.VarRef(`formalArg`) =>
3514+
case js.VarRef(` F438 formalArgName`) =>
35153515
// This is trivial assignment, we don't need it
35163516

35173517
case _ =>
35183518
mutatedLocalVars += formalArgSym
3519-
quadruplets += ((js.VarRef(formalArg)(tpe), tpe,
3520-
freshLocalIdent(formalArg.name.withPrefix("temp$")),
3519+
quadruplets += ((js.VarRef(formalArgName)(tpe), tpe,
3520+
freshLocalIdent(formalArgName.withPrefix("temp$")),
35213521
fixedActualArg))
35223522
}
35233523
}
@@ -3538,7 +3538,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
35383538
yield js.VarDef(tempArg, NoOriginalName, argType, mutable = false, actualArg)
35393539
val trueAssignments =
35403540
for ((formalArg, argType, tempArg, _) <- quadruplets)
3541-
yield js.Assign(formalArg, js.VarRef(tempArg)(argType))
3541+
yield js.Assign(formalArg, js.VarRef(tempArg.name)(argType))
35423542
tempAssignments ::: trueAssignments
35433543
}
35443544
}
@@ -5002,7 +5002,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
50025002
val callTrgIdent = freshLocalIdent()
50035003
val callTrgVarDef = js.VarDef(callTrgIdent, NoOriginalName, receiverType,
50045004
mutable = false, genExpr(receiver))
5005-
val callTrg = js.VarRef(callTrgIdent)(receiverType)
5005+
val callTrg = js.VarRef(callTrgIdent.name)(receiverType)
50065006

50075007
val arguments = args zip sym.tpe.params map { case (arg, param) =>
50085008
/* No need for enteringPosterasure, because value classes are not
@@ -5453,7 +5453,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
54535453
val fVarDef = js.VarDef(freshLocalIdent("f"), NoOriginalName,
54545454
jstpe.AnyType, mutable = false, arg2)
54555455
val keyVarIdent = freshLocalIdent("key")
5456-
val keyVarRef = js.VarRef(keyVarIdent)(jstpe.AnyType)
5456+
val keyVarRef = js.VarRef(keyVarIdent.name)(jstpe.AnyType)
54575457
js.Block(
54585458
objVarDef,
54595459
fVarDef,
@@ -5488,7 +5488,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
54885488
val handlerVarDef = js.VarDef(freshLocalIdent("handler"), NoOriginalName,
54895489
jstpe.AnyType, mutable = false, arg2)
54905490
val exceptionVarIdent = freshLocalIdent("e")
5491-
val exceptionVarRef = js.VarRef(exceptionVarIdent)(jstpe.AnyType)
5491+
val exceptionVarRef = js.VarRef(exceptionVarIdent.name)(jstpe.AnyType)
54925492
js.Block(
54935493
bodyVarDef,
54945494
handlerVarDef,
@@ -6465,7 +6465,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
64656465
// Gen the inlined target method body
64666466
val genMethodDef = {
64676467
genMethodWithCurrentLocalNameScope(consumeDelambdafyTarget(target),
6468-
initThisLocalVarIdent = thisFormalCapture.map(_.name))
6468+
initThisLocalVarName = thisFormalCapture.map(_.name.name))
64696469
}
64706470
val js.MethodDef(methodFlags, _, _, methodParams, _, methodBody) = genMethodDef
64716471

@@ -6777,7 +6777,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
67776777
// Utilities ---------------------------------------------------------------
67786778

67796779
def genVarRef(sym: Symbol)(implicit pos: Position): js.VarRef =
6780-
js.VarRef(encodeLocalSym(sym))(toIRType(sym.tpe))
6780+
js.VarRef(encodeLocalSymName(sym))(toIRType(sym.tpe))
67816781

67826782
def genParamDef(sym: Symbol): js.ParamDef =
67836783
genParamDef(sym, toIRType(sym.tpe))

compiler/src/main/scala/org/scalajs/nscplugin/GenJSExports.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ trait GenJSExports[G <: Global with Singleton] extends SubComponent {
684684
val superClass = {
685685
val superClassSym = currentClassSym.superClass
686686
if (isNestedJSClass(superClassSym)) {
687-
js.VarRef(js.LocalIdent(JSSuperClassParamName))(jstpe.AnyType)
687+
js.VarRef(JSSuperClassParamName)(jstpe.AnyType)
688688
} else {
689689
js.LoadJSConstructor(encodeClassName(superClassSym))
690690
}
@@ -1053,7 +1053,7 @@ trait GenJSExports[G <: Global with Singleton] extends SubComponent {
10531053

10541054
def genArgRef(index: Int)(implicit pos: Position): js.Tree = {
10551055
if (index < minArgc)
1056-
js.VarRef(js.LocalIdent(fixedParamNames(index)))(jstpe.AnyType)
1056+
js.VarRef(fixedParamNames(index))(jstpe.AnyType)
10571057
else
10581058
js.JSSelect(genRestArgRef(), js.IntLiteral(index - minArgc))
10591059
}
@@ -1073,16 +1073,16 @@ trait GenJSExports[G <: Global with Singleton] extends SubComponent {
10731073
def genRestArgRef()(implicit pos: Position): js.Tree = {
10741074
assert(needsRestParam,
10751075
s"trying to generate a reference to non-existent rest param at $pos")
1076-
js.VarRef(js.LocalIdent(restParamName))(jstpe.AnyType)
1076+
js.VarRef(restParamName)(jstpe.AnyType)
10771077
}
10781078

10791079
def genAllArgsRefsForForwarder()(implicit pos: Position): List[js.TreeOrJSSpread] = {
10801080
val fixedArgRefs = fixedParamNames.toList.map { paramName =>
1081-
js.VarRef(js.LocalIdent(paramName))(jstpe.AnyType)
1081+
js.VarRef(paramName)(jstpe.AnyType)
10821082
}
10831083

10841084
if (needsRestParam) {
1085-
val restArgRef = js.VarRef(js.LocalIdent(restParamName))(jstpe.AnyType)
1085+
val restArgRef = js.VarRef(restParamName)(jstpe.AnyType)
10861086
fixedArgRefs :+ js.JSSpread(restArgRef)
10871087
} else {
10881088
fixedArgRefs

compiler/src/main/scala/org/scalajs/nscplugin/JSEncoding.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ trait JSEncoding[G <: Global with Singleton] extends SubComponent {
256256
}
257257
}
258258

259-
def encodeLocalSym(sym: Symbol)(implicit pos: Position): js.LocalIdent = {
259+
def encodeLocalSym(sym: Symbol)(implicit pos: Position): js.LocalIdent =
260+
js.LocalIdent(encodeLocalSymName(sym))
261+
262+
def encodeLocalSymName(sym: Symbol): LocalName = {
260263
/* The isValueParameter case is necessary to work around an internal bug
261264
* of scalac: for some @varargs methods, the owner of some parameters is
262265
* the enclosing class rather the method, so !sym.owner.isClass fails.
@@ -266,7 +269,7 @@ trait JSEncoding[G <: Global with Singleton] extends SubComponent {
266269
require(sym.isValueParameter ||
267270
(!sym.owner.isClass && sym.isTerm && !sym.isMethod && !sym.isModule),
268271
"encodeLocalSym called with non-local symbol: " + sym)
269-
js.LocalIdent(localSymbolName(sym))
272+
localSymbolName(sym)
270273
}
271274

272275
def encodeClassType(sym: Symbol): jstpe.Type = {

ir/shared/src/main/scala/org/scalajs/ir/Hashers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,9 @@ object Hashers {
524524
mixTag(TagClassOf)
525525
mixTypeRef(typeRef)
526526

527-
case VarRef(ident) =>
527+
case VarRef(name) =>
528528
mixTag(TagVarRef)
529-
mixLocalIdent(ident)
529+
mixName(name)
530530
mixType(tree.tpe)
531531

532532
case This() =>

ir/shared/src/main/scala/org/scalajs/ir/Printers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,8 @@ object Printers {
869869

870870
// Atomic expressions
871871

872-
case VarRef(ident) =>
873-
print(ident)
872+
case VarRef(name) =>
873+
print(name)
874874

875875
case This() =>
876876
print("this")

ir/shared/src/main/scala/org/scalajs/ir/Serializers.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,9 @@ object Serializers {
551551
writeTagAndPos(TagClassOf)
552552
writeTypeRef(typeRef)
553553

554-
case VarRef(ident) =>
554+
case VarRef(name) =>
555555
writeTagAndPos(TagVarRef)
556-
writeLocalIdent(ident)
556+
writeName(name)
557557
writeType(tree.tpe)
558558

559559
case This() =>
@@ -1388,7 +1388,10 @@ object Serializers {
13881388
case TagClassOf => ClassOf(readTypeRef())
13891389

13901390
case TagVarRef =>
1391-
VarRef(readLocalIdent())(readType())
1391+
val name =
1392+
if (hacks.use17) readLocalIdent().name
1393+
else readLocalName()
1394+
VarRef(name)(readType())
13921395

13931396
case TagThis =>
13941397
val tpe = readType()

ir/shared/src/main/scala/org/scalajs/ir/Trees.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ object Trees {
104104
implicit val pos: Position) extends Tree {
105105
val tpe = VoidType
106106

107-
def ref(implicit pos: Position): VarRef = VarRef(name)(vtpe)
107+
def ref(implicit pos: Position): VarRef = VarRef(name.name)(vtpe)
108108
}
109109

110110
sealed case class ParamDef(name: LocalIdent, originalName: OriginalName,
111111
ptpe: Type, mutable: Boolean)(
112112
implicit val pos: Position) extends IRNode {
113-
def ref(implicit pos: Position): VarRef = VarRef(name)(ptpe)
113+
def ref(implicit pos: Position): VarRef = VarRef(name.name)(ptpe)
114114
}
115115

116116
// Control flow constructs
@@ -1088,7 +1088,7 @@ object Trees {
10881088

10891089
// Atomic expressions
10901090

1091-
sealed case class VarRef(ident: LocalIdent)(val tpe: Type)(
1091+
sealed case class VarRef(name: LocalName)(val tpe: Type)(
10921092
implicit val pos: Position) extends AssignLhs
10931093

10941094
sealed case class This()(val tpe: Type)(implicit val pos: Position)

ir/shared/src/test/scala/org/scalajs/ir/HashersTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class HashersTest {
9191
)
9292

9393
test(
94-
"309805e5680ffa1804811ff5c9ebc77e91846957",
94+
"82df9d6beb7df0ee9f501380323bdb2038cc50cb",
9595
MethodDef(MemberFlags.empty, mIIMethodName, NON,
9696
List(ParamDef("x", NON, IntType, mutable = false)),
9797
IntType, Some(bodyWithInterestingStuff))(
@@ -106,7 +106,7 @@ class HashersTest {
106106
}
107107

108108
test(
109-
"c0f1ef1b22fd1cfdc9bba78bf3e0f433e9f82fc1",
109+
"d0fa6c753502e3d1df34e53ca6f6afb5cbdcd9d4",
110110
JSMethodDef(MemberFlags.empty, s("m"),
111111
List(ParamDef("x", NON, AnyType, mutable = false)), None,
112112
bodyWithInterestingStuff)(

0 commit comments

Comments
 (0)
0