Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f8343b8

Browse files
tamasvajkigfoo
authored andcommitted
Extract local delegated properties
1 parent 48b99cf commit f8343b8

5 files changed

Lines changed: 346 additions & 2 deletions

File tree

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,28 @@ open class KotlinFileExtractor(
891891
}
892892
}
893893
is IrLocalDelegatedProperty -> {
894-
// TODO:
895-
logger.errorElement("Unhandled IrLocalDelegatedProperty", s)
894+
val blockId = tw.getFreshIdLabel<DbBlock>()
895+
val locId = tw.getLocation(s)
896+
tw.writeStmts_block(blockId, parent, idx, callable)
897+
tw.writeHasLocation(blockId, locId)
898+
extractVariable(s.delegate, callable, blockId, 0)
899+
900+
val propType = useType(s.type)
901+
val propId = tw.getFreshIdLabel<DbKt_local_delegated_property>()
902+
tw.writeKtLocalDelegatedProperties(propId, useVariable(s.delegate), propType.javaResult.id, s.name.asString())
903+
tw.writeKtLocalDelegatedPropertiesKotlinType(propId, propType.kotlinResult.id)
904+
905+
// Getter:
906+
extractStatement(s.getter, callable, blockId, 1)
907+
val l = getLocallyVisibleFunctionLabels(s.getter).function
908+
tw.writeKtLocalDelegatedPropertyGetters(propId, l)
909+
910+
val setter = s.setter
911+
if (setter != null) {
912+
extractStatement(setter, callable, blockId, 2)
913+
val l = getLocallyVisibleFunctionLabels(setter).function
914+
tw.writeKtLocalDelegatedPropertySetters(propId, l)
915+
}
896916
}
897917
else -> {
898918
logger.errorElement("Unrecognised IrStatement: " + s.javaClass, s)

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,3 +1217,25 @@ ktLocalFunction(
12171217
ktInitializerAssignment(
12181218
unique int id: @assignexpr ref
12191219
)
1220+
1221+
ktLocalDelegatedProperties(
1222+
unique int id: @kt_local_delegated_property,
1223+
unique int variableId: @variable ref,
1224+
int typeId: @type ref,
1225+
string name: string ref
1226+
)
1227+
1228+
ktLocalDelegatedPropertiesKotlinType(
1229+
unique int id: @kt_local_delegated_property ref,
1230+
int kttypeid: @kt_type ref
1231+
);
1232+
1233+
ktLocalDelegatedPropertyGetters(
1234+
unique int id: @kt_local_delegated_property ref,
1235+
int getter: @method ref
1236+
)
1237+
1238+
ktLocalDelegatedPropertySetters(
1239+
unique int id: @kt_local_delegated_property ref,
1240+
int setter: @method ref
1241+
)

java/ql/test/kotlin/library-tests/exprs/PrintAst.expected

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,198 @@
1+
delegatedProperties.kt:
2+
# 0| [CompilationUnit] delegatedProperties
3+
# 14| 1: [Class] Resource
4+
# 14| 1: [Constructor] Resource
5+
# 14| 5: [BlockStmt] { ... }
6+
# 14| 0: [SuperConstructorInvocationStmt] super(...)
7+
# 16| 2: [Class] Owner
8+
# 16| 1: [Constructor] Owner
9+
# 16| 5: [BlockStmt] { ... }
10+
# 16| 0: [SuperConstructorInvocationStmt] super(...)
11+
# 17| 2: [Method] fn
12+
#-----| 4: (Parameters)
13+
# 17| 0: [Parameter] map
14+
# 17| 5: [BlockStmt] { ... }
15+
# 18| 0: [BlockStmt] { ... }
16+
# 18| 0: [LocalVariableDeclStmt] var ...;
17+
# 18| 1: [LocalVariableDeclExpr] varResource1$delegate
18+
# 18| 0: [ClassInstanceExpr] new ResourceDelegate(...)
19+
# 18| -3: [TypeAccess] ResourceDelegate
20+
# 18| 1: [LocalTypeDeclStmt] class ...
21+
# 18| 0: [LocalClass]
22+
# 18| 1: [Constructor]
23+
# 18| 5: [BlockStmt] { ... }
24+
# 18| 0: [SuperConstructorInvocationStmt] super(...)
25+
# 18| 1: [Method] <get-varResource1>
26+
# 18| 5: [BlockStmt] { ... }
27+
# 18| 0: [ReturnStmt] return ...
28+
# 18| 0: [MethodAccess] getValue(...)
29+
# 18| -1: [VarAccess] varResource1$delegate
30+
# 0| 0: [NullLiteral] null
31+
# 18| 2: [LocalTypeDeclStmt] class ...
32+
# 18| 0: [LocalClass]
33+
# 18| 1: [Constructor]
34+
# 18| 5: [BlockStmt] { ... }
35+
# 18| 0: [SuperConstructorInvocationStmt] super(...)
36+
# 18| 1: [Method] <set-varResource1>
37+
#-----| 4: (Parameters)
38+
# 18| 0: [Parameter] value
39+
# 18| 5: [BlockStmt] { ... }
40+
# 18| 0: [ReturnStmt] return ...
41+
# 18| 0: [MethodAccess] setValue(...)
42+
# 18| -1: [VarAccess] varResource1$delegate
43+
# 0| 0: [NullLiteral] null
44+
# 18| 2: [VarAccess] value
45+
# 19| 1: [ExprStmt] <Expr>;
46+
# 19| 0: [MethodAccess] println(...)
47+
# 19| -1: [TypeAccess] ConsoleKt
48+
# 19| 0: [MethodAccess] <get-varResource1>(...)
49+
# 19| -1: [ClassInstanceExpr] new (...)
50+
# 19| -3: [TypeAccess] Object
51+
# 20| 2: [ExprStmt] <Expr>;
52+
# 20| 0: [MethodAccess] <set-varResource1>(...)
53+
# 20| -1: [ClassInstanceExpr] new (...)
54+
# 20| -3: [TypeAccess] Object
55+
# 20| 0: [IntegerLiteral] 2
56+
# 22| 3: [BlockStmt] { ... }
57+
# 22| 0: [LocalVariableDeclStmt] var ...;
58+
# 22| 1: [LocalVariableDeclExpr] name$delegate
59+
# 22| 0: [VarAccess] map
60+
# 22| 1: [LocalTypeDeclStmt] class ...
61+
# 22| 0: [LocalClass]
62+
# 22| 1: [Constructor]
63+
# 22| 5: [BlockStmt] { ... }
64+
# 22| 0: [SuperConstructorInvocationStmt] super(...)
65+
# 22| 1: [Method] <get-name>
66+
# 22| 5: [BlockStmt] { ... }
67+
# 22| 0: [ReturnStmt] return ...
68+
# 22| 0: [MethodAccess] getValue(...)
69+
# 22| -3: [TypeAccess] String
70+
# 22| -2: [TypeAccess] Object
71+
# 22| -1: [TypeAccess] MapAccessorsKt
72+
# 22| 0: [VarAccess] name$delegate
73+
# 0| 1: [NullLiteral] null
74+
# 24| 4: [LocalTypeDeclStmt] class ...
75+
# 24| 0: [LocalClass]
76+
# 24| 1: [Constructor]
77+
# 24| 5: [BlockStmt] { ... }
78+
# 24| 0: [SuperConstructorInvocationStmt] super(...)
79+
# 24| 1: [Method] resourceDelegate
80+
# 24| 5: [BlockStmt] { ... }
81+
# 30| 0: [ReturnStmt] return ...
82+
# 24| 0: [StmtExpr] <Stmt>
83+
# 24| 0: [BlockStmt] { ... }
84+
# 24| 0: [LocalTypeDeclStmt] class ...
85+
# 24| 0: [AnonymousClass,LocalClass] new ReadWriteProperty<Object,Integer>(...) { ... }
86+
# 24| 1: [Constructor]
87+
# 24| 5: [BlockStmt] { ... }
88+
# 24| 0: [SuperConstructorInvocationStmt] super(...)
89+
# 24| 1: [BlockStmt] { ... }
90+
# 25| 0: [ExprStmt] <Expr>;
91+
# 25| 0: [KtInitializerAssignExpr] ...=...
92+
# 25| 0: [VarAccess] curValue
93+
# 25| 2: [Method] getCurValue
94+
# 25| 5: [BlockStmt] { ... }
95+
# 25| 0: [ReturnStmt] return ...
96+
# 25| 0: [VarAccess] this.curValue
97+
# 25| -1: [ThisAccess] this
98+
# 25| 2: [Method] setCurValue
99+
#-----| 4: (Parameters)
100+
# 25| 0: [Parameter] <set-?>
101+
# 25| 5: [BlockStmt] { ... }
102+
# 25| 0: [ExprStmt] <Expr>;
103+
# 25| 0: [AssignExpr] ...=...
104+
# 25| 0: [VarAccess] this.curValue
105+
# 25| -1: [ThisAccess] this
106+
# 25| 1: [VarAccess] <set-?>
107+
# 25| 2: [FieldDeclaration] int curValue;
108+
# 25| -1: [TypeAccess] int
109+
# 25| 0: [IntegerLiteral] 0
110+
# 26| 5: [Method] getValue
111+
#-----| 4: (Parameters)
112+
# 26| 0: [Parameter] thisRef
113+
# 26| 1: [Parameter] property
114+
# 26| 5: [BlockStmt] { ... }
115+
# 26| 0: [ReturnStmt] return ...
116+
# 26| 0: [MethodAccess] getCurValue(...)
117+
# 26| -1: [ThisAccess] this
118+
# 27| 6: [Method] setValue
119+
#-----| 4: (Parameters)
120+
# 27| 0: [Parameter] thisRef
121+
# 27| 1: [Parameter] property
122+
# 27| 2: [Parameter] value
123+
# 27| 5: [BlockStmt] { ... }
124+
# 28| 0: [ExprStmt] <Expr>;
125+
# 28| 0: [MethodAccess] setCurValue(...)
126+
# 28| -1: [ThisAccess] this
127+
# 28| 0: [VarAccess] value
128+
# 24| 1: [ExprStmt] <Expr>;
129+
# 24| 0: [ClassInstanceExpr] new (...)
130+
# 24| -3: [TypeAccess] ReadWriteProperty<Object,Integer>
131+
# 32| 5: [BlockStmt] { ... }
132+
# 32| 0: [LocalVariableDeclStmt] var ...;
133+
# 32| 1: [LocalVariableDeclExpr] readOnly$delegate
134+
# 32| 0: [MethodAccess] resourceDelegate(...)
135+
# 32| -1: [ClassInstanceExpr] new (...)
136+
# 32| -3: [TypeAccess] Object
137+
# 32| 1: [LocalTypeDeclStmt] class ...
138+
# 32| 0: [LocalClass]
139+
# 32| 1: [Constructor]
140+
# 32| 5: [BlockStmt] { ... }
141+
# 32| 0: [SuperConstructorInvocationStmt] super(...)
142+
# 32| 1: [Method] <get-readOnly>
143+
# 32| 5: [BlockStmt] { ... }
144+
# 32| 0: [ReturnStmt] return ...
145+
# 32| 0: [MethodAccess] getValue(...)
146+
# 32| -1: [VarAccess] readOnly$delegate
147+
# 0| 0: [NullLiteral] null
148+
# 33| 6: [BlockStmt] { ... }
149+
# 33| 0: [LocalVariableDeclStmt] var ...;
150+
# 33| 1: [LocalVariableDeclExpr] readWrite$delegate
151+
# 33| 0: [MethodAccess] resourceDelegate(...)
152+
# 33| -1: [ClassInstanceExpr] new (...)
153+
# 33| -3: [TypeAccess] Object
154+
# 33| 1: [LocalTypeDeclStmt] class ...
155+
# 33| 0: [LocalClass]
156+
# 33| 1: [Constructor]
157+
# 33| 5: [BlockStmt] { ... }
158+
# 33| 0: [SuperConstructorInvocationStmt] super(...)
159+
# 33| 1: [Method] <get-readWrite>
160+
# 33| 5: [BlockStmt] { ... }
161+
# 33| 0: [ReturnStmt] return ...
162+
# 33| 0: [MethodAccess] getValue(...)
163+
# 33| -1: [VarAccess] readWrite$delegate
164+
# 0| 0: [NullLiteral] null
165+
# 33| 2: [LocalTypeDeclStmt] class ...
166+
# 33| 0: [LocalClass]
167+
# 33| 1: [Constructor]
168+
# 33| 5: [BlockStmt] { ... }
169+
# 33| 0: [SuperConstructorInvocationStmt] super(...)
170+
# 33| 1: [Method] <set-readWrite>
171+
#-----| 4: (Parameters)
172+
# 33| 0: [Parameter] value
173+
# 33| 5: [BlockStmt] { ... }
174+
# 33| 0: [ReturnStmt] return ...
175+
# 33| 0: [MethodAccess] setValue(...)
176+
# 33| -1: [VarAccess] readWrite$delegate
177+
# 0| 0: [NullLiteral] null
178+
# 33| 2: [VarAccess] value
179+
# 37| 3: [Class] ResourceDelegate
180+
# 37| 1: [Constructor] ResourceDelegate
181+
# 37| 5: [BlockStmt] { ... }
182+
# 37| 0: [SuperConstructorInvocationStmt] super(...)
183+
# 38| 2: [Method] getValue
184+
#-----| 4: (Parameters)
185+
# 38| 0: [Parameter] thisRef
186+
# 38| 1: [Parameter] property
187+
# 38| 5: [BlockStmt] { ... }
188+
# 39| 0: [ReturnStmt] return ...
189+
# 39| 0: [IntegerLiteral] 1
190+
# 41| 3: [Method] setValue
191+
#-----| 4: (Parameters)
192+
# 41| 0: [Parameter] thisRef
193+
# 41| 1: [Parameter] property
194+
# 41| 2: [Parameter] value
195+
# 41| 5: [BlockStmt] { ... }
1196
exprs.kt:
2197
# 0| [CompilationUnit] exprs
3198
# 0| 1: [Class] ExprsKt
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import kotlin.properties.ReadWriteProperty
2+
import kotlin.reflect.KProperty
3+
/* TODO: should uncomment after https://github.com/github/codeql-kotlin/pull/294
4+
class ClassProp1 {
5+
val prop1: Int by lazy {
6+
println("init")
7+
5
8+
}
9+
fun fn() {
10+
println(prop1)
11+
}
12+
}
13+
*/
14+
class Resource
15+
16+
class Owner {
17+
fun fn(map: Map<String, Any?>) {
18+
var varResource1: Int by ResourceDelegate()
19+
println(varResource1)
20+
varResource1 = 2
21+
22+
val name: String by map
23+
24+
fun resourceDelegate(): ReadWriteProperty<Any?, Int> = object : ReadWriteProperty<Any?, Int> {
25+
var curValue = 0
26+
override fun getValue(thisRef: Any?, property: KProperty<*>): Int = curValue
27+
override fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {
28+
curValue = value
29+
}
30+
}
31+
32+
val readOnly: Int by resourceDelegate() // ReadWriteProperty as val
33+
var readWrite: Int by resourceDelegate()
34+
}
35+
}
36+
37+
class ResourceDelegate {
38+
operator fun getValue(thisRef: Owner?, property: KProperty<*>): Int {
39+
return 1
40+
}
41+
operator fun setValue(thisRef: Owner?, property: KProperty<*>, value: Int?) {
42+
}
43+
}

0 commit comments

Comments
 (0)