Commit Graph

25 Commits

Author SHA1 Message Date
Brian Norman 17a1871b83 [FIR] Make sure the primary constructor is first in class CFG
The primary constructor of a class needs to be the first subgraph of the
class control-flow graph. Based on the Kotlin specification, class
initialization order goes first primary constructor, in-place
declarations (properties and init blocks), and then secondary
constructors. If the class doesn't have a primary constructor, then it
is just skipped in the order.

Unfortunately, the class control-flow graph had in-place declarations
first and then all constructors. Instead, we should treat the primary
constructor as the first in-place declaration, and then continue with
the existing processing as secondary constructors. This will guarantee
that super constructor calls have the correct property initialization
information.

^KT-65093 Fixed
2024-01-23 23:16:00 +00:00
Brian Norman 0881910a1b [FIR] Rewind DFA after call arguments for correct receiver smartcasting
^KT-63709 Fixed
2023-12-08 14:32:22 +00:00
Brian Norman b55fda0c55 [FIR] Create CFG for files to track top-level property initialization
In order to properly analyze top-level property initialization, a
control-flow graph must be created for FirFiles. This change adds the
foundation for the file CFG and updates body resolve to create the CFG.
Checking the CFG for proper initialization is separated into a following
change to ease code review.

KT-56683
2023-08-31 12:50:52 +00:00
pyos 9f17b5de97 FIR CFA: add edges according to constructor delegation 2023-01-26 13:12:11 +00:00
pyos 17ee8f3a7b FIR CFA: put primary constructor before other class members 2023-01-10 15:40:50 +02:00
pyos c6e9afb788 FIR CFA: remove class initializer part nodes
Instead, attach subgraphs directly to the class enter node.
2023-01-10 15:40:50 +02:00
pyos 54f32a6fba Test: FIR CFA: index nodes in rendering order 2023-01-10 15:40:49 +02:00
pyos 74758278d7 FIR CFA: attach method graphs to class exit node
Even for non-local classes. This ensures that the CFG edges in this case
will only go to a subgraph, not to an unrelated graph.
2023-01-10 15:40:48 +02:00
pyos aadea0e26f FIR CFA: properly visit subgraphs in checkers
Interpretation: a graph A is a subgraph of B if information available at
nodes of A depends on the paths taken in B. For example, local classes
are subgraphs of a graph in which they are declared, and members of
those classes are subgraphs of the local class itself - because these
members can reference captured values.

Consequences:

 * if graph G is a subgraph of node N, then G is a subgraph of N's
   owner;
 * `ControlFlowAnalysisDiagnosticComponent` will only visit root graphs;
 * `graph.traverse` will ignore subgraph boundaries, as if all subgraphs
   are inlined into one huge root graph;
 * if a control flow checker needs information from a declaration to
   which a graph is attached, it must look at subgraphs explicitly.

For example, consider the `callsInPlace` checker. When a function
has a `callsInPlace` contract and a local declaration, the checker must
visit that local declaration to ensure it does not capture the allegedly
called-in-place argument - hence `graph.traverse` will look at the
nodes. However, the local declaration can also be a function with its
own `callsInPlace` contracts, so the checker should also run for it in
isolation. If that sounds quadratic, that's because unfortunately it is.
2023-01-10 15:40:48 +02:00
pyos faf0129a5d Test: FIR CFA: sort edges by style & target node id 2023-01-10 15:40:46 +02:00
pyos a9be27e330 FIR CFG: add union nodes
Quick quiz:

 Q: In a CFG, what does `a -> b -> c -> d` mean?
 A: `a`, then `b`, then `c`, then `d`.

 Q: In a CFG, what does `a -> b -> d; a -> c -> d` mean?
 A: `a`, then `b` or `c`, then `d`.

 Q: So how do you encode "a, then (b, then c) or (c, then b), then d`?
 A: You can't.

Problem is, you need to, because that's what `a; run2({ b }, { c }); d`
does when `run2` has a contract that it calls both its lambda arguments
in-place: `shuffle(listOf(block1, block2)).forEach { it() }` is a
perfectly valid implementation for it, as little sense as that makes.

So that's what union nodes solve. When a node implements
`UnionNodeMarker`, its inputs are interpreted as "all visited in some
order" instead of the normal "one of the inputs is visited".

Currently this is used for data flow. It *should* also be used for
control flow, but it isn't. But it should be. But that's not so easy.

BTW, `try` exit is NOT a union node; although lambdas in one branch can
be completed according to types' of lambdas in another, data does not
flow between the branches anyway (since we don't know how much of the
`try` executed before jumping into `catch`, and `catch`es are mutually
exclusive) so a `try` expression is more like `when` than a function
call with called-in-place-exactly-once arguments. The fact that
`exitTryExpression` used `processUnionOfArguments` in a weird way
should've hinted at that, but now we know for certain.
2022-12-08 10:19:29 +00:00
Simon Ogorodnik 513af2dfbc FIR. Refactor smart-cast representation in FIR tree
Make smart-casts non-transparent expression without delegation
to underlying FirQualifiedAccessExpression, as children delegation in
fir tree has unclear semantics
Remove two different kinds of tree nodes for smart-casts
2022-08-15 21:46:11 +00:00
Dmitriy Novozhilov 4225813d79 [FIR] Update CFG dumps according to changed order of visiting class children 2021-06-29 21:03:29 +03:00
Ilya Kirillov 0cf00d0f72 FIR: fix FirDefaultPropertyAccessor phase to BODY_RESOLVE 2021-04-15 15:23:56 +03:00
Dmitriy Novozhilov f794ced888 [FIR] Fix incorrect cluster creating in CFG dumps 2020-10-12 11:55:05 +03:00
Oleg Ivanov cc9c5b9e3c [FIR] Add CFG nodes, add multiple subGraphs for CFGOwner 2020-08-11 16:17:01 +03:00
Ivan Kylchik 3c01a39846 [FIR] Update test data after introducing new fir nodes 2020-07-21 13:54:17 +03:00
Dmitriy Novozhilov 26458875d5 [FIR] Add checker for uninitialized properties 2020-06-19 15:53:09 +03:00
Dmitriy Novozhilov 12ed8c3bb4 [FIR-TEST] Update CFG dumps according to new nodes order 2020-06-19 15:53:04 +03:00
Dmitriy Novozhilov 87859b0faa [FIR] Introduce new algorithm for building CFG for declarations 2020-06-19 15:53:00 +03:00
Denis Zharkov 723b275d99 FIR: Fix rendered fir and DFA graph test data after safe-calls refactoring
^KT-38444 In Progress
2020-06-03 10:43:37 +03:00
Dmitriy Novozhilov 1d39270b3e [FIR] Don't pass data flow to property accessors of non local classes 2020-04-03 10:08:24 +03:00
Dmitriy Novozhilov 1d1b8d3290 [FIR-TEST] Update cfg dumps according to previous commits 2020-04-03 10:08:23 +03:00
Dmitriy Novozhilov 8cb6e8f8af [FIR] Add control flow graph for class initialization 2020-04-03 10:08:17 +03:00
Dmitriy Novozhilov cc07ae96b3 [FIR-TEST] Move analysis tests to separate module 2020-03-19 09:51:01 +03:00