CFG.LexicalScope minor refactoring

This commit is contained in:
Mikhail Glukhikh
2016-04-19 18:32:37 +03:00
parent 5f7f46ff6c
commit cda4d2dcce
4 changed files with 17 additions and 20 deletions
@@ -35,9 +35,9 @@ interface ControlFlowBuilder {
val returnSubroutine: KtElement val returnSubroutine: KtElement
// Lexical scopes // Lexical scopes
fun enterLexicalScope(element: KtElement) fun enterLexicalScope(block: KtElement)
fun exitLexicalScope(element: KtElement) fun exitLexicalScope(block: KtElement)
fun getExitPoint(labelElement: KtElement): Label? fun getExitPoint(labelElement: KtElement): Label?
fun getConditionEntryPoint(labelElement: KtElement): Label fun getConditionEntryPoint(labelElement: KtElement): Label
@@ -217,11 +217,11 @@ abstract class ControlFlowBuilderAdapter : ControlFlowBuilder {
return delegateBuilder.newValue(element) return delegateBuilder.newValue(element)
} }
override fun enterLexicalScope(element: KtElement) { override fun enterLexicalScope(block: KtElement) {
delegateBuilder.enterLexicalScope(element) delegateBuilder.enterLexicalScope(block)
} }
override fun exitLexicalScope(element: KtElement) { override fun exitLexicalScope(block: KtElement) {
delegateBuilder.exitLexicalScope(element) delegateBuilder.exitLexicalScope(block)
} }
} }
@@ -179,18 +179,18 @@ class ControlFlowInstructionsGenerator : ControlFlowBuilderAdapter() {
private val currentScope: LexicalScope private val currentScope: LexicalScope
get() = lexicalScopes.peek() get() = lexicalScopes.peek()
override fun enterLexicalScope(element: KtElement) { override fun enterLexicalScope(block: KtElement) {
val current = if (lexicalScopes.isEmpty()) null else currentScope val current = if (lexicalScopes.isEmpty()) null else currentScope
val scope = LexicalScope(current, element) val scope = LexicalScope(current, block)
lexicalScopes.push(scope) lexicalScopes.push(scope)
} }
override fun exitLexicalScope(element: KtElement) { override fun exitLexicalScope(block: KtElement) {
val currentScope = currentScope val currentScope = currentScope
assert(currentScope.element === element) { assert(currentScope.block === block) {
"Exit from not the current lexical scope.\n" + "Exit from not the current lexical scope.\n" +
"Current scope is for: " + currentScope.element + ".\n" + "Current scope is for a block: " + currentScope.block.text + ".\n" +
"Exit from the scope for: " + element.text "Exit from the scope for: " + block.text
} }
lexicalScopes.pop() lexicalScopes.pop()
} }
@@ -19,20 +19,17 @@ package org.jetbrains.kotlin.cfg.pseudocode.instructions
import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.KtElement
class LexicalScope(val parentScope: LexicalScope?, val element: KtElement) { class LexicalScope(private val parentScope: LexicalScope?, val block: KtElement) {
//todo remove after KT-4126 val depth: Int = (parentScope?.depth ?: 0) + 1
private val d = (parentScope?.depth ?: 0) + 1
val depth: Int get() = d
val lexicalScopeForContainingDeclaration: LexicalScope? by lazy { val lexicalScopeForContainingDeclaration: LexicalScope? by lazy {
var scope: LexicalScope? = this var scope: LexicalScope? = this
while (scope != null) { while (scope != null) {
if (scope.element is KtDeclaration) { if (scope.block is KtDeclaration) {
return@lazy scope break
} }
scope = scope.parentScope scope = scope.parentScope
} }
scope
return@lazy null
} }
} }