org.jacodb.api.cfg.JcGraphs.kt Maven / Gradle / Ivy
Show all versions of jacodb-api Show documentation
/*
* Copyright 2022 UnitTestBot contributors (utbot.org)
*
* 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.
*/
@file:JvmName("JcGraphs")
package org.jacodb.api.cfg
abstract class TypedExprResolver : AbstractFullExprSetCollector() {
val result = hashSetOf()
}
class LocalResolver : TypedExprResolver() {
override fun ifMatches(expr: JcExpr) {
if (expr is JcLocal) {
result.add(expr)
}
}
}
class ValueResolver : TypedExprResolver() {
override fun ifMatches(expr: JcExpr) {
if (expr is JcValue) {
result.add(expr)
}
}
}
fun JcGraph.apply(visitor: JcInstVisitor): JcGraph {
instructions.forEach { it.accept(visitor) }
return this
}
fun > JcGraph.applyAndGet(visitor: T, getter: (T) -> R): R {
instructions.forEach { it.accept(visitor) }
return getter(visitor)
}
fun JcGraph.collect(visitor: JcInstVisitor): Collection {
return instructions.map { it.accept(visitor) }
}
fun > JcInst.applyAndGet(visitor: T, getter: (T) -> R): R {
this.accept(visitor)
return getter(visitor)
}
fun > JcExpr.applyAndGet(visitor: T, getter: (T) -> R): R {
this.accept(visitor)
return getter(visitor)
}
val JcGraph.locals: Set
get() {
val resolver = LocalResolver().also {
collect(it)
}
return resolver.result
}
val JcInst.locals: Set
get() {
val resolver = LocalResolver().also {
accept(it)
}
return resolver.result
}
val JcExpr.values: Set
get() {
val resolver = ValueResolver().also {
accept(it)
}
return resolver.result
}
val JcInst.values: Set
get() {
val resolver = ValueResolver().also {
accept(it)
}
return resolver.result
}
val JcGraph.values: Set
get() {
val resolver = ValueResolver().also {
collect(it)
}
return resolver.result
}