JS backend: removed adding '$' to label suffix

This commit is contained in:
Alexey Tsvetkov
2014-10-23 20:07:42 +04:00
parent 78e12f7cfa
commit fb8918bbe6
11 changed files with 38 additions and 22 deletions
@@ -49,14 +49,30 @@ public class JsFunctionScope(parent: JsScope, description: String) : JsScope(par
topLabelScope?.findName(label) topLabelScope?.findName(label)
private inner class LabelScope(parent: LabelScope?, val ident: String) : JsScope(parent, "Label scope for $ident", null) { private inner class LabelScope(parent: LabelScope?, val ident: String) : JsScope(parent, "Label scope for $ident", null) {
val labelName = JsName(this@JsFunctionScope, parent?.getFreshIdent(ident) ?: ident) val labelName: JsName
{
val freshIdent = when {
ident in RESERVED_WORDS -> getFreshIdent(ident)
parent != null -> parent.getFreshIdent(ident)
else -> ident
}
labelName = JsName(this@JsFunctionScope, freshIdent)
}
override fun findOwnName(name: String): JsName? = override fun findOwnName(name: String): JsName? =
if (name == ident) labelName else null if (name == ident) labelName else null
/**
* Safe call is necessary, because hasOwnName can be called
* in constructor before labelName is initialized (see KT-4394)
*/
[suppress("UNNECESSARY_SAFE_CALL")]
override fun hasOwnName(name: String): Boolean = override fun hasOwnName(name: String): Boolean =
name == ident name in RESERVED_WORDS
|| name == labelName.getIdent() || name == ident
|| name == labelName?.getIdent()
|| getParent()?.hasOwnName(name) ?: false || getParent()?.hasOwnName(name) ?: false
} }
@@ -364,7 +364,7 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
private static String getReferencedName(JetSimpleNameExpression expression) { private static String getReferencedName(JetSimpleNameExpression expression) {
return expression.getReferencedName() return expression.getReferencedName()
.replaceAll("^@", "") .replaceAll("^@", "")
.replaceAll("(?:^`(.*)`$)", "$1") + "$"; .replaceAll("(?:^`(.*)`$)", "$1");
} }
private static JsNameRef getTargetLabel(JetExpressionWithLabel expression, TranslationContext context) { private static JsNameRef getTargetLabel(JetExpressionWithLabel expression, TranslationContext context) {
@@ -1,7 +1,7 @@
package foo package foo
// CHECK_LABELS_COUNT: function=test1 name=loop$ count=1 // CHECK_LABELS_COUNT: function=test1 name=loop count=1
// CHECK_LABELS_COUNT: function=test2 name=loop$ count=1 // CHECK_LABELS_COUNT: function=test2 name=loop count=1
fun test1() { fun test1() {
var `loop$` = 0 var `loop$` = 0
@@ -1,7 +1,7 @@
package foo package foo
// CHECK_LABELS_COUNT: function=test name=loop$ count=1 // CHECK_LABELS_COUNT: function=test name=loop count=1
// CHECK_LABELS_COUNT: function=test name=loop$_0 count=1 // CHECK_LABELS_COUNT: function=test name=loop_0 count=1
fun test() { fun test() {
var i = 0 var i = 0
@@ -1,9 +1,9 @@
package foo package foo
// CHECK_CONTAINS_NO_CALLS: test // CHECK_CONTAINS_NO_CALLS: test
// CHECK_LABELS_COUNT: function=test name=loop$ count=1 // CHECK_LABELS_COUNT: function=test name=loop count=1
// CHECK_LABELS_COUNT: function=test name=loop$_0 count=1 // CHECK_LABELS_COUNT: function=test name=loop_0 count=1
// CHECK_LABELS_COUNT: function=test name=loop$_1 count=1 // CHECK_LABELS_COUNT: function=test name=loop_1 count=1
class State() { class State() {
public var value: Int = 0 public var value: Int = 0
@@ -1,8 +1,8 @@
package foo package foo
// CHECK_LABELS_COUNT: function=test name=loop$ count=1 // CHECK_LABELS_COUNT: function=test name=loop count=1
// CHECK_LABELS_COUNT: function=test name=loop$_0 count=1 // CHECK_LABELS_COUNT: function=test name=loop_0 count=1
// CHECK_LABELS_COUNT: function=test name=loop$_1 count=1 // CHECK_LABELS_COUNT: function=test name=loop_1 count=1
class State() { class State() {
public var value: Int = 0 public var value: Int = 0
@@ -1,6 +1,6 @@
package foo package foo
// CHECK_LABELS_COUNT: function=test name=loop$ count=2 // CHECK_LABELS_COUNT: function=test name=loop count=2
fun test() { fun test() {
var i = 0 var i = 0
@@ -1,7 +1,7 @@
package foo package foo
// CHECK_NOT_CALLED: testInline // CHECK_NOT_CALLED: testInline
// CHECK_LABELS_COUNT: function=testNoinline name=loop$ count=2 // CHECK_LABELS_COUNT: function=testNoinline name=loop count=2
inline fun testInline(): Int { inline fun testInline(): Int {
var c = 0 var c = 0
@@ -1,8 +1,8 @@
package foo package foo
// CHECK_NOT_CALLED: testLabelInline // CHECK_NOT_CALLED: testLabelInline
// CHECK_LABELS_COUNT: function=testLabel name=loop$ count=1 // CHECK_LABELS_COUNT: function=testLabel name=loop count=1
// CHECK_LABELS_COUNT: function=testLabel name=loop$_0 count=2 // CHECK_LABELS_COUNT: function=testLabel name=loop_0 count=2
inline fun testLabelInline(): Int { inline fun testLabelInline(): Int {
var a = 0 var a = 0
@@ -1,7 +1,7 @@
package foo package foo
// CHECK_LABELS_COUNT: function=testBreak name=loop$ count=1 // CHECK_LABELS_COUNT: function=testBreak name=loop count=1
// CHECK_LABELS_COUNT: function=testContinue name=loop$ count=1 // CHECK_LABELS_COUNT: function=testContinue name=loop count=1
fun testBreak() { fun testBreak() {
var i = 0 var i = 0
@@ -2,8 +2,8 @@ package foo
// CHECK_NOT_CALLED: testBreak // CHECK_NOT_CALLED: testBreak
// CHECK_NOT_CALLED: testContinue // CHECK_NOT_CALLED: testContinue
// CHECK_LABELS_COUNT: function=testBreakNoinline name=loop$ count=1 // CHECK_LABELS_COUNT: function=testBreakNoinline name=loop count=1
// CHECK_LABELS_COUNT: function=testContinueNoinline name=loop$ count=1 // CHECK_LABELS_COUNT: function=testContinueNoinline name=loop count=1
inline fun testBreak(): Int { inline fun testBreak(): Int {
var i = 0 var i = 0