-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathIrElementTransformerVoidWithContext.kt
143 lines (119 loc) · 5.56 KB
/
IrElementTransformerVoidWithContext.kt
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* 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.jetbrains.kotlin.backend.common
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.builders.Scope
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol
import org.jetbrains.kotlin.ir.symbols.IrScriptSymbol
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
open class ScopeWithIr(val scope: Scope, val irElement: IrElement)
abstract class IrElementTransformerVoidWithContext : IrElementTransformerVoid() {
private val scopeStack = mutableListOf<ScopeWithIr>()
protected open fun createScope(declaration: IrSymbolOwner): ScopeWithIr =
ScopeWithIr(Scope(declaration.symbol), declaration)
final override fun visitFile(declaration: IrFile): IrFile {
scopeStack.push(createScope(declaration))
val result = visitFileNew(declaration)
scopeStack.pop()
return result
}
final override fun visitClass(declaration: IrClass): IrStatement {
scopeStack.push(createScope(declaration))
val result = visitClassNew(declaration)
scopeStack.pop()
return result
}
final override fun visitProperty(declaration: IrProperty): IrStatement {
scopeStack.push(createScope(declaration))
val result = visitPropertyNew(declaration)
scopeStack.pop()
return result
}
final override fun visitField(declaration: IrField): IrStatement {
scopeStack.push(createScope(declaration))
val result = visitFieldNew(declaration)
scopeStack.pop()
return result
}
final override fun visitFunction(declaration: IrFunction): IrStatement {
scopeStack.push(createScope(declaration))
val result = visitFunctionNew(declaration)
scopeStack.pop()
return result
}
final override fun visitAnonymousInitializer(declaration: IrAnonymousInitializer): IrStatement {
scopeStack.push(createScope(declaration))
val result = visitAnonymousInitializerNew(declaration)
scopeStack.pop()
return result
}
final override fun visitValueParameter(declaration: IrValueParameter): IrStatement {
scopeStack.push(createScope(declaration))
val result = visitValueParameterNew(declaration)
scopeStack.pop()
return result
}
final override fun visitScript(declaration: IrScript): IrStatement {
scopeStack.push(createScope(declaration))
val result = visitScriptNew(declaration)
scopeStack.pop()
return result
}
protected val currentFile get() = scopeStack.lastOrNull { it.irElement is IrFile }!!.irElement as IrFile
protected val currentScript get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrScriptSymbol }
protected val currentClass get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrClassSymbol }
protected val currentFunction get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrFunctionSymbol }
protected val currentProperty get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrPropertySymbol }
protected val currentAnonymousInitializer get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrAnonymousInitializer }
protected val currentValueParameter get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrValueParameter }
protected val currentScope get() = scopeStack.peek()
protected val parentScope get() = if (scopeStack.size < 2) null else scopeStack[scopeStack.size - 2]
protected val allScopes get() = scopeStack
protected val currentDeclarationParent get() = scopeStack.lastOrNull { it.irElement is IrDeclarationParent }?.irElement as? IrDeclarationParent
@ObsoleteDescriptorBasedAPI
fun printScopeStack() {
scopeStack.forEach { println(it.scope.scopeOwner) }
}
open fun visitFileNew(declaration: IrFile): IrFile {
return super.visitFile(declaration)
}
open fun visitClassNew(declaration: IrClass): IrStatement {
return super.visitClass(declaration)
}
open fun visitFunctionNew(declaration: IrFunction): IrStatement {
return super.visitFunction(declaration)
}
open fun visitPropertyNew(declaration: IrProperty): IrStatement {
return super.visitProperty(declaration)
}
open fun visitFieldNew(declaration: IrField): IrStatement {
return super.visitField(declaration)
}
open fun visitAnonymousInitializerNew(declaration: IrAnonymousInitializer): IrStatement {
return super.visitAnonymousInitializer(declaration)
}
open fun visitValueParameterNew(declaration: IrValueParameter): IrStatement {
return super.visitValueParameter(declaration)
}
open fun visitScriptNew(declaration: IrScript): IrStatement {
return super.visitScript(declaration)
}
}