Skip to content

Commit 6a52249

Browse files
committed
Add API section to doc
1 parent a26ac68 commit 6a52249

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

‎resources/doc/analysis.md‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ A general note: When we import an entity, we cannot know if it is a variable, a
2121
- [FAST Python visitor](#fastpython-visitor)
2222
- [Control Flow Graph (CFG)](#control-flow-graph-cfg)
2323
- [FAST utilities](#fast-utilities)
24+
- [API](#api)
25+
- [Variables analysis](#variables-analysis)
26+
- [Knowing what is a variable](#knowing-what-is-a-variable)
27+
- [Accessing the SSA versions of a variable](#accessing-the-ssa-versions-of-a-variable)
28+
- [Getting the accesses of a variable](#getting-the-accesses-of-a-variable)
29+
- [Knowing what uses a variable](#knowing-what-uses-a-variable)
30+
- [Getting the internal accesses of a variable](#getting-the-internal-accesses-of-a-variable)
31+
- [Getting the calls on a variable](#getting-the-calls-on-a-variable)
32+
- [Getting the assigned expressions](#getting-the-assigned-expressions)
2433
- [Examples of analysis](#examples-of-analysis)
2534

2635
## Local resolution
@@ -407,6 +416,77 @@ A node can also be accessed internally, via an attribute access such as `x.y` or
407416

408417
TODO: Document more
409418

419+
## API
420+
421+
This section references the API available to analyse a FASTPython model. For now, FAST-Python was mostly used to analyse variables, so the API is centered on variables analysis.
422+
423+
The requirement column indicates what needs to be done on the model before using the selector:
424+
- Vanilla: nothing, it works on the freshly imported model
425+
- LR: the local resolution needs to be done
426+
- SSA: the SSA resolution needs to be done (which includes the local resolution)
427+
428+
### Variables analysis
429+
430+
#### Knowing what is a variable
431+
432+
| Selector | Receiver | Requirement | Description |
433+
|---|---|---|---|
434+
| `isResolvedVariable` | `FASTPyEntity` | LR | Returns `true` if the node resolves to a local variable declaration (not an unresolved name, function, method or import) |
435+
| `usedVariables` | `FASTPyEntity` | LR | Returns all the entities of the node and its subtree resolving to local variable declarations (includes the node itself if it is one) |
436+
| `allResolvedVariables` | `FASTPyModel` | LR | Returns every entity of the model resolving to a variable declaration |
437+
438+
#### Accessing the SSA versions of a variable
439+
440+
| Selector | Receiver | Requirement | Description |
441+
|---|---|---|---|
442+
| `ssaVersion` | `FASTPyEntity` representing a variable | SSA | Returns the current SSA version of the variable. It is a `FASTVariableVersionSSA` or a `FASTVariablePhiVersionSSA` if the value can come from multiple assignments |
443+
| `allSSAVersions` | `FASTPyEntity` representing a variable | SSA | Returns all the SSA versions the variable can get, including the Phi versions |
444+
| `allSSABasicVersions` | `FASTPyEntity` representing a variable | SSA | Same as `#allSSAVersions` but without the Phi versions |
445+
446+
#### Getting the accesses of a variable
447+
448+
| Selector | Receiver | Requirement | Description |
449+
|---|---|---|---|
450+
| `allAccesses` | Any access of a variable | LR | Returns all the read and write accesses to the variable |
451+
| `allReadAccesses` | Any access of a variable | LR | Returns all the read accesses to the variable |
452+
| `allWriteAccesses` | Any access of a variable | LR | Returns all the write accesses to the variable |
453+
| `variableDeclarator` | A write access of a variable | LR | Returns the node assigning the variable (assignment, augmented assignment, for loop or for in clause) |
454+
| `versionAccesses` | Any access of a variable | SSA | Returns the read and write accesses for the current SSA version of the receiver |
455+
| `versionReadAccesses` | Any access of a variable | SSA | Returns the read accesses for the current SSA version of the receiver |
456+
| `versionWriteAccesses` | Any access of a variable | SSA | Returns the write accesses for the current SSA version of the receiver |
457+
458+
#### Knowing what uses a variable
459+
460+
| Selector | Receiver | Requirement | Description |
461+
|---|---|---|---|
462+
| `allNodesUsingMe` | Any access of a variable | LR | Returns all the nodes using the variable: the ancestors of each of its accesses, up to the statement containing it, without going further than the statement blocks |
463+
| `statementsUsingMe` | Any access of a variable | LR | Same as `#allNodesUsingMe` but only the statements, without the intermediate nodes |
464+
| `allNodesUsingMyVersion` | Any access of a variable | SSA | Same as `#allNodesUsingMe` but only with the accesses reachable from the current SSA version |
465+
| `statementsUsingMyVersion` | Any access of a variable | SSA | Same as `#statementsUsingMe` but only with the accesses reachable from the current SSA version |
466+
467+
#### Getting the internal accesses of a variable
468+
469+
| Selector | Receiver | Requirement | Description |
470+
|---|---|---|---|
471+
| `internalAccess` | `FASTPyEntity` | Vanilla | Returns the attribute access or subscript directly accessing the node, or `nil` |
472+
| `internalAccesses` | Any access of a variable | LR | Returns all the internal accesses done on the variable, on all its accesses |
473+
474+
#### Getting the calls on a variable
475+
476+
| Selector | Receiver | Requirement | Description |
477+
|---|---|---|---|
478+
| `callsOnVariable` | Any access of a variable | LR | Returns the `FASTPyCall` nodes having the variable as receiver: the callee is the read of the variable itself (invocation or instantiation `x()`) or an internal access on it (`x.append(1)`, `x[3]()`) |
479+
| `callsOnVariableVersion` | Any access of a variable | SSA | Same as `#callsOnVariable` but only with the reads reachable from the current SSA version |
480+
481+
#### Getting the assigned expressions
482+
483+
| Selector | Receiver | Requirement | Description |
484+
|---|---|---|---|
485+
| `assignedExpressions` | An assignment node (the `variableDeclarator` of a write access) | Vanilla | Returns the expressions used by this assignment. Returns an empty collection on any other node |
486+
| `assignedExpressionsMap` | Any access of a variable | SSA | Returns a map with, as key, each write access impacting the value of the variable, and as value, the expressions used by that assignment |
487+
| `transitiveAssignedExpressions` | An assignment node (the `variableDeclarator` of a write access) | SSA | Same as `#assignedExpressions` but the variables used in the assigned expressions are followed: the expressions assigned to them are added, recursively |
488+
| `transitiveAssignedExpressionsMap` | Any access of a variable | SSA | Same as `#assignedExpressionsMap` but each value is the transitive assigned expressions of the corresponding assignment |
489+
410490
## Examples of analysis
411491

412492
In this section I'll show how to answer some questions I was asked to answer with FAST-Python.

0 commit comments

Comments
 (0)