IR: synchronize on SymbolTable operations

This commit is contained in:
Georgy Bronnikov
2021-01-15 14:17:41 +03:00
parent c9d0448fd1
commit ec2dc9c0fa
@@ -76,6 +76,7 @@ class SymbolTable(
abstract fun get(sig: IdSignature): S? abstract fun get(sig: IdSignature): S?
inline fun declare(d: D, createSymbol: () -> S, createOwner: (S) -> B): B { inline fun declare(d: D, createSymbol: () -> S, createOwner: (S) -> B): B {
synchronized(this) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val d0 = d.original as D val d0 = d.original as D
assert(d0 === d) { assert(d0 === d) {
@@ -92,9 +93,11 @@ class SymbolTable(
} }
return createOwner(symbol) return createOwner(symbol)
} }
}
@OptIn(ObsoleteDescriptorBasedAPI::class) @OptIn(ObsoleteDescriptorBasedAPI::class)
inline fun declare(sig: IdSignature, createSymbol: () -> S, createOwner: (S) -> B): B { inline fun declare(sig: IdSignature, createSymbol: () -> S, createOwner: (S) -> B): B {
synchronized(this) {
val existing = get(sig) val existing = get(sig)
val symbol = if (existing == null) { val symbol = if (existing == null) {
createSymbol() createSymbol()
@@ -107,8 +110,10 @@ class SymbolTable(
set(symbol) set(symbol)
return result return result
} }
}
inline fun declareIfNotExists(d: D, createSymbol: () -> S, createOwner: (S) -> B): B { inline fun declareIfNotExists(d: D, createSymbol: () -> S, createOwner: (S) -> B): B {
synchronized(this) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val d0 = d.original as D val d0 = d.original as D
assert(d0 === d) { assert(d0 === d) {
@@ -125,8 +130,10 @@ class SymbolTable(
} }
return if (symbol.isBound) symbol.owner else createOwner(symbol) return if (symbol.isBound) symbol.owner else createOwner(symbol)
} }
}
inline fun declare(sig: IdSignature, d: D?, createSymbol: () -> S, createOwner: (S) -> B): B { inline fun declare(sig: IdSignature, d: D?, createSymbol: () -> S, createOwner: (S) -> B): B {
synchronized(this) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val d0 = d?.original as D val d0 = d?.original as D
assert(d0 === d) { assert(d0 === d) {
@@ -143,8 +150,10 @@ class SymbolTable(
} }
return createOwner(symbol) return createOwner(symbol)
} }
}
inline fun referenced(d: D, orElse: () -> S): S { inline fun referenced(d: D, orElse: () -> S): S {
synchronized(this) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val d0 = d.original as D val d0 = d.original as D
assert(d0 === d) { assert(d0 === d) {
@@ -161,9 +170,11 @@ class SymbolTable(
} }
return s return s
} }
}
@OptIn(ObsoleteDescriptorBasedAPI::class) @OptIn(ObsoleteDescriptorBasedAPI::class)
inline fun referenced(sig: IdSignature, orElse: () -> S): S { inline fun referenced(sig: IdSignature, orElse: () -> S): S {
synchronized(this) {
return get(sig) ?: run { return get(sig) ?: run {
val new = orElse() val new = orElse()
assert(unboundSymbols.add(new)) { assert(unboundSymbols.add(new)) {
@@ -174,6 +185,7 @@ class SymbolTable(
} }
} }
} }
}
private open inner class FlatSymbolTable<D : DeclarationDescriptor, B : IrSymbolOwner, S : IrBindableSymbol<D, B>> : private open inner class FlatSymbolTable<D : DeclarationDescriptor, B : IrSymbolOwner, S : IrBindableSymbol<D, B>> :
SymbolTableBase<D, B, S>() { SymbolTableBase<D, B, S>() {