Skip to content

Commit

Permalink
Add Raw Stmt/Value/Type (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lipen authored Jan 17, 2025
1 parent fe53361 commit 0d1a79f
Show file tree
Hide file tree
Showing 15 changed files with 371 additions and 579 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object Versions {
const val kotlinx_collections_immutable = "0.3.5"
const val kotlinx_coroutines = "1.6.4"
const val kotlin_metadata = kotlin
const val kotlinx_serialization = "1.4.1"
const val kotlinx_serialization = "1.8.0"
const val licenser = "0.6.1"
const val mockk = "1.13.3"
const val sarif4k = "0.5.0"
Expand Down
21 changes: 21 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ interface EtsEntity : CommonExpr {
EtsValue.Visitor<R>,
EtsExpr.Visitor<R> {

fun visit(value: EtsRawEntity): R {
if (this is Default) {
return defaultVisit(value)
}
error("Cannot handle ${value::class.java.simpleName}: $value")
}

interface Default<out R> : Visitor<R>,
EtsValue.Visitor.Default<R>,
EtsExpr.Visitor.Default<R> {
Expand All @@ -41,3 +48,17 @@ interface EtsEntity : CommonExpr {

fun <R> accept(visitor: Visitor<R>): R
}

data class EtsRawEntity(
val kind: String,
val extra: Map<String, Any> = emptyMap(),
override val type: EtsType,
) : EtsEntity {
override fun toString(): String {
return "$kind $extra: $type"
}

override fun <R> accept(visitor: EtsEntity.Visitor<R>): R {
return visitor.visit(this)
}
}
82 changes: 41 additions & 41 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsRef.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,47 +45,47 @@ data class EtsParameterRef(
}
}

data class EtsCaughtExceptionRef(
override val type: EtsType,
) : EtsValue {
override fun toString(): String {
return "catch($type)"
}

override fun <R> accept(visitor: EtsValue.Visitor<R>): R {
return visitor.visit(this)
}
}

data class EtsGlobalRef(
val name: String,
val ref: EtsValue?, // TODO: check whether it could be EtsEntity at best
) : EtsValue {
override val type: EtsType
get() = ref?.type ?: EtsUnknownType

override fun toString(): String {
return "global $name"
}

override fun <R> accept(visitor: EtsValue.Visitor<R>): R {
return visitor.visit(this)
}
}

data class EtsClosureFieldRef(
val base: EtsLocal,
val fieldName: String,
override val type: EtsType,
) : EtsValue {
override fun toString(): String {
return "$base.$fieldName"
}

override fun <R> accept(visitor: EtsValue.Visitor<R>): R {
return visitor.visit(this)
}
}
// data class EtsCaughtExceptionRef(
// override val type: EtsType,
// ) : EtsValue {
// override fun toString(): String {
// return "catch($type)"
// }
//
// override fun <R> accept(visitor: EtsValue.Visitor<R>): R {
// return visitor.visit(this)
// }
// }
//
// data class EtsGlobalRef(
// val name: String,
// val ref: EtsValue?, // TODO: check whether it could be EtsEntity at best
// ) : EtsValue {
// override val type: EtsType
// get() = ref?.type ?: EtsUnknownType
//
// override fun toString(): String {
// return "global $name"
// }
//
// override fun <R> accept(visitor: EtsValue.Visitor<R>): R {
// return visitor.visit(this)
// }
// }
//
// data class EtsClosureFieldRef(
// val base: EtsLocal,
// val fieldName: String,
// override val type: EtsType,
// ) : EtsValue {
// override fun toString(): String {
// return "$base.$fieldName"
// }
//
// override fun <R> accept(visitor: EtsValue.Visitor<R>): R {
// return visitor.visit(this)
// }
// }

data class EtsArrayAccess(
val array: EtsValue,
Expand Down
36 changes: 21 additions & 15 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsStmt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ interface EtsStmt : CommonInst {
fun visit(stmt: EtsGotoStmt): R
fun visit(stmt: EtsIfStmt): R
fun visit(stmt: EtsSwitchStmt): R
fun visit(stmt: EtsRawStmt): R

fun visit(stmt: EtsRawStmt): R {
if (this is Default) {
return defaultVisit(stmt)
}
error("Cannot handle ${stmt::class.java.simpleName}: $stmt")
}

interface Default<out R> : Visitor<R> {
override fun visit(stmt: EtsNopStmt): R = defaultVisit(stmt)
Expand All @@ -62,6 +68,20 @@ interface EtsStmt : CommonInst {
fun <R> accept(visitor: Visitor<R>): R
}

data class EtsRawStmt(
override val location: EtsInstLocation,
val kind: String,
val extra: Map<String, Any> = emptyMap(),
) : EtsStmt {
override fun toString(): String {
return "$kind $extra"
}

override fun <R> accept(visitor: EtsStmt.Visitor<R>): R {
return visitor.visit(this)
}
}

data class EtsNopStmt(
override val location: EtsInstLocation,
) : EtsStmt {
Expand Down Expand Up @@ -169,17 +189,3 @@ data class EtsSwitchStmt(
return visitor.visit(this)
}
}

data class EtsRawStmt(
override val location: EtsInstLocation,
val type: String,
val text: String,
) : EtsStmt {
override fun toString(): String {
return text
}

override fun <R> accept(visitor: EtsStmt.Visitor<R>): R {
return visitor.visit(this)
}
}
91 changes: 24 additions & 67 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.jacodb.api.common.CommonTypeName
import org.jacodb.ets.model.EtsClassSignature
import org.jacodb.ets.model.EtsLocalSignature
import org.jacodb.ets.model.EtsMethodSignature
import org.jacodb.ets.model.EtsNamespaceSignature

interface EtsType : CommonType, CommonTypeName {
override val typeName: String
Expand All @@ -45,13 +44,16 @@ interface EtsType : CommonType, CommonTypeName {
fun visit(type: EtsClassType): R
fun visit(type: EtsFunctionType): R
fun visit(type: EtsArrayType): R
fun visit(type: EtsArrayObjectType): R
fun visit(type: EtsUnclearRefType): R
fun visit(type: EtsGenericType): R
fun visit(type: EtsAliasType): R
fun visit(type: EtsAnnotationNamespaceType): R
fun visit(type: EtsAnnotationTypeQueryType): R
fun visit(type: EtsLexicalEnvType): R

fun visit(type: EtsRawType): R {
if (this is Default) {
return defaultVisit(type)
}
error("Cannot handle ${type::class.java.simpleName}: $type")
}

interface Default<R> : Visitor<R> {
override fun visit(type: EtsAnyType): R = defaultVisit(type)
Expand All @@ -69,13 +71,10 @@ interface EtsType : CommonType, CommonTypeName {
override fun visit(type: EtsClassType): R = defaultVisit(type)
override fun visit(type: EtsFunctionType): R = defaultVisit(type)
override fun visit(type: EtsArrayType): R = defaultVisit(type)
override fun visit(type: EtsArrayObjectType): R = defaultVisit(type)
override fun visit(type: EtsUnclearRefType): R = defaultVisit(type)
override fun visit(type: EtsGenericType): R = defaultVisit(type)
override fun visit(type: EtsAliasType): R = defaultVisit(type)
override fun visit(type: EtsAnnotationNamespaceType): R = defaultVisit(type)
override fun visit(type: EtsAnnotationTypeQueryType): R = defaultVisit(type)
override fun visit(type: EtsLexicalEnvType): R = defaultVisit(type)
override fun visit(type: EtsRawType): R = defaultVisit(type)

fun defaultVisit(type: EtsType): R
}
Expand All @@ -84,6 +83,22 @@ interface EtsType : CommonType, CommonTypeName {
fun <R> accept(visitor: Visitor<R>): R
}

data class EtsRawType(
val kind: String,
val extra: Map<String, Any> = emptyMap(),
) : EtsType {
override val typeName: String
get() = kind

override fun toString(): String {
return "$kind $extra"
}

override fun <R> accept(visitor: EtsType.Visitor<R>): R {
return visitor.visit(this)
}
}

object EtsAnyType : EtsType {
override val typeName: String
get() = "any"
Expand Down Expand Up @@ -278,19 +293,6 @@ data class EtsArrayType(
}
}

data class EtsArrayObjectType(
val elementType: EtsType,
) : EtsRefType {
override val typeName: String
get() = "Array<${elementType.typeName}>"

override fun toString(): String = typeName

override fun <R> accept(visitor: EtsType.Visitor<R>): R {
return visitor.visit(this)
}
}

data class EtsUnclearRefType(
val name: String,
val typeParameters: List<EtsType> = emptyList(),
Expand Down Expand Up @@ -343,48 +345,3 @@ data class EtsAliasType(
return visitor.visit(this)
}
}

data class EtsAnnotationNamespaceType(
val originType: String,
val namespaceSignature: EtsNamespaceSignature,
) : EtsType {
override val typeName: String
get() = originType

override fun toString(): String {
return originType
}

override fun <R> accept(visitor: EtsType.Visitor<R>): R {
return visitor.visit(this)
}
}

data class EtsAnnotationTypeQueryType(
val originType: String,
) : EtsType {
override val typeName: String
get() = originType

override fun toString(): String {
return originType
}

override fun <R> accept(visitor: EtsType.Visitor<R>): R {
return visitor.visit(this)
}
}

data class EtsLexicalEnvType(
val nestedMethod: EtsMethodSignature,
val closures: List<EtsLocal>,
) : EtsType {
override val typeName: String
get() = closures.joinToString(prefix = "[", postfix = "]")

override fun toString(): String = typeName

override fun <R> accept(visitor: EtsType.Visitor<R>): R {
return visitor.visit(this)
}
}
6 changes: 0 additions & 6 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ interface EtsValue : EtsEntity, CommonValue {
// Ref
fun visit(value: EtsThis): R
fun visit(value: EtsParameterRef): R
fun visit(value: EtsCaughtExceptionRef): R
fun visit(value: EtsGlobalRef): R
fun visit(value: EtsClosureFieldRef): R
fun visit(value: EtsArrayAccess): R
fun visit(value: EtsInstanceFieldRef): R
fun visit(value: EtsStaticFieldRef): R
Expand All @@ -50,9 +47,6 @@ interface EtsValue : EtsEntity, CommonValue {

override fun visit(value: EtsThis): R = defaultVisit(value)
override fun visit(value: EtsParameterRef): R = defaultVisit(value)
override fun visit(value: EtsCaughtExceptionRef): R = defaultVisit(value)
override fun visit(value: EtsGlobalRef): R = defaultVisit(value)
override fun visit(value: EtsClosureFieldRef): R = defaultVisit(value)
override fun visit(value: EtsArrayAccess): R = defaultVisit(value)
override fun visit(value: EtsInstanceFieldRef): R = defaultVisit(value)
override fun visit(value: EtsStaticFieldRef): R = defaultVisit(value)
Expand Down
Loading

0 comments on commit 0d1a79f

Please sign in to comment.