Skip to content

Commit caad067

Browse files
authored
refactor(cfg): unify semantic visitor api for parameters (#1710)
1 parent 9724969 commit caad067

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/control-flow/semantic-cfg-guided-visitor.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import type { DataflowInformation } from '../dataflow/info';
55

66
import type { DataflowCfgGuidedVisitorConfiguration } from './dfg-cfg-guided-visitor';
77
import { DataflowAwareCfgGuidedVisitor } from './dfg-cfg-guided-visitor';
8-
import type {
9-
NormalizedAst,
10-
ParentInformation
11-
} from '../r-bridge/lang-4.x/ast/model/processing/decorate';
8+
import type { NormalizedAst, ParentInformation } from '../r-bridge/lang-4.x/ast/model/processing/decorate';
129
import type { SyntaxCfgGuidedVisitorConfiguration } from './syntax-cfg-guided-visitor';
1310
import type { NodeId } from '../r-bridge/lang-4.x/ast/model/processing/node-id';
1411
import type { Origin } from '../dataflow/origin/dfg-get-origin';
@@ -136,7 +133,12 @@ export class SemanticCfgGuidedVisitor<
136133
*/
137134
protected override visitFunctionDefinition(vertex: DataflowGraphVertexFunctionDefinition): void {
138135
super.visitFunctionDefinition(vertex);
139-
this.onFunctionDefinition({ vertex });
136+
const ast = this.getNormalizedAst(vertex.id);
137+
if(ast?.type === RType.FunctionDefinition) {
138+
this.onFunctionDefinition({ vertex, parameters: ast.parameters.map(p => p.info.id) });
139+
} else {
140+
this.onFunctionDefinition({ vertex });
141+
}
140142
}
141143

142144
/**
@@ -239,9 +241,15 @@ export class SemanticCfgGuidedVisitor<
239241
return this.onAssignmentCall({ call, target: undefined, source: undefined });
240242
}
241243
case 'builtin:special-bin-op':
242-
return this.onSpecialBinaryOpCall({ call });
244+
if(call.args.length !== 2) {
245+
return this.onSpecialBinaryOpCall({ call });
246+
}
247+
return this.onSpecialBinaryOpCall({ call, lhs: call.args[0], rhs: call.args[1] });
243248
case 'builtin:pipe':
244-
return this.onPipeCall({ call });
249+
if(call.args.length !== 2) {
250+
return this.onPipeCall({ call });
251+
}
252+
return this.onPipeCall({ call, lhs: call.args[0], rhs: call.args[1] });
245253
case 'builtin:quote':
246254
return this.onQuoteCall({ call });
247255
case 'builtin:for-loop':
@@ -274,6 +282,11 @@ export class SemanticCfgGuidedVisitor<
274282
}
275283
}
276284

285+
/**
286+
* This event is called for the root program node, i.e., the program that is being analyzed.
287+
*
288+
* @protected
289+
*/
277290
protected onProgram(_data: RExpressionList<OtherInfo>) {
278291
}
279292

@@ -331,7 +344,7 @@ export class SemanticCfgGuidedVisitor<
331344
*
332345
* For example, `function(x) { x + 1 }` in `lapply(1:10, function(x) { x + 1 })`.
333346
*/
334-
protected onFunctionDefinition(_data: { vertex: DataflowGraphVertexFunctionDefinition }) {}
347+
protected onFunctionDefinition(_data: { vertex: DataflowGraphVertexFunctionDefinition, parameters?: readonly NodeId[] }) {}
335348

336349
/**
337350
* This event triggers for every anonymous call within the program.
@@ -362,7 +375,7 @@ export class SemanticCfgGuidedVisitor<
362375
*
363376
* This explicitly will not trigger for scenarios in which the function has no name (i.e., if it is anonymous).
364377
* For such cases, you may rely on the {@link SemanticCfgGuidedVisitor#onUnnamedCall|`onUnnamedCall`} event.
365-
* The main reason for this separation is part of flowR's handling of these functions, as anonmyous calls cannot be resolved using the active environment.
378+
* The main reason for this separation is part of flowR's handling of these functions, as anonymous calls cannot be resolved using the active environment.
366379
*
367380
* @protected
368381
*/
@@ -482,14 +495,14 @@ export class SemanticCfgGuidedVisitor<
482495
*
483496
* @protected
484497
*/
485-
protected onSpecialBinaryOpCall(_data: { call: DataflowGraphVertexFunctionCall }) {}
498+
protected onSpecialBinaryOpCall(_data: { call: DataflowGraphVertexFunctionCall, lhs?: FunctionArgument, rhs?: FunctionArgument }) {}
486499

487500
/**
488501
* This event triggers for every call to R's pipe operator, i.e., for every call to `|>`.
489502
*
490503
* @protected
491504
*/
492-
protected onPipeCall(_data: { call: DataflowGraphVertexFunctionCall }) {}
505+
protected onPipeCall(_data: { call: DataflowGraphVertexFunctionCall, lhs?: FunctionArgument, rhs?: FunctionArgument }) {}
493506

494507

495508
/**

0 commit comments

Comments
 (0)