Skip to content

Commit

Permalink
Finish renaming of variables still names Expr
Browse files Browse the repository at this point in the history
  • Loading branch information
konradweiss committed Sep 11, 2023
1 parent 48d394c commit 0b0eaa7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -421,46 +421,46 @@ open class CallResolver(ctx: TranslationContext) : SymbolResolverPass(ctx) {
return scopeManager.resolveFunctionStopScopeTraversalOnDefinition(call).isEmpty()
}

protected fun handleConstructExpression(constructExpr: ConstructExpression) {
if (constructExpr.instantiates != null && constructExpr.constructor != null) return
val typeName = constructExpr.type.name
protected fun handleConstructExpression(constructExpression: ConstructExpression) {
if (constructExpression.instantiates != null && constructExpression.constructor != null) return
val typeName = constructExpression.type.name
val recordDeclaration = recordMap[typeName]
constructExpr.instantiates = recordDeclaration
constructExpression.instantiates = recordDeclaration
for (template in templateList) {
if (
template is RecordTemplateDeclaration &&
recordDeclaration != null &&
recordDeclaration in template.realizations &&
(constructExpr.templateParameters.size <= template.parameters.size)
(constructExpression.templateParameters.size <= template.parameters.size)
) {
val defaultDifference =
template.parameters.size - constructExpr.templateParameters.size
template.parameters.size - constructExpression.templateParameters.size
if (defaultDifference <= template.parameterDefaults.size) {
// Check if predefined template value is used as default in next value
addRecursiveDefaultTemplateArgs(constructExpr, template)
addRecursiveDefaultTemplateArgs(constructExpression, template)

// Add missing defaults
val missingNewParams: List<Node?> =
template.parameterDefaults.subList(
constructExpr.templateParameters.size,
constructExpression.templateParameters.size,
template.parameterDefaults.size
)
for (missingParam in missingNewParams) {
if (missingParam != null) {
constructExpr.addTemplateParameter(
constructExpression.addTemplateParameter(
missingParam,
TemplateInitialization.DEFAULT
)
}
}
constructExpr.templateInstantiation = template
constructExpression.templateInstantiation = template
break
}
}
}
if (recordDeclaration != null) {
val constructor = getConstructorDeclaration(constructExpr, recordDeclaration)
constructExpr.constructor = constructor
val constructor = getConstructorDeclaration(constructExpression, recordDeclaration)
constructExpression.constructor = constructor
}
}

Expand Down Expand Up @@ -585,35 +585,35 @@ open class CallResolver(ctx: TranslationContext) : SymbolResolverPass(ctx) {
}

/**
* @param constructExpr we want to find an invocation target for
* @param constructExpression we want to find an invocation target for
* @param recordDeclaration associated with the Object the ConstructExpression constructs
* @return a ConstructDeclaration that is an invocation of the given ConstructExpression. If
* there is no valid ConstructDeclaration we will create an implicit ConstructDeclaration that
* matches the ConstructExpression.
*/
protected fun getConstructorDeclaration(
constructExpr: ConstructExpression,
constructExpression: ConstructExpression,
recordDeclaration: RecordDeclaration
): ConstructorDeclaration {
val signature = constructExpr.signature
val signature = constructExpression.signature
var constructorCandidate =
getConstructorDeclarationDirectMatch(signature, recordDeclaration)
if (constructorCandidate == null && constructExpr.language is HasDefaultArguments) {
if (constructorCandidate == null && constructExpression.language is HasDefaultArguments) {
// Check for usage of default args
constructorCandidate =
resolveConstructorWithDefaults(constructExpr, signature, recordDeclaration)
resolveConstructorWithDefaults(constructExpression, signature, recordDeclaration)
}
if (constructorCandidate == null && constructExpr.language.isCPP) { // TODO: Fix this
if (constructorCandidate == null && constructExpression.language.isCPP) { // TODO: Fix this
// If we don't find any candidate and our current language is c/c++ we check if there is
// a candidate with an implicit cast
constructorCandidate =
resolveConstructorWithImplicitCast(constructExpr, recordDeclaration)
resolveConstructorWithImplicitCast(constructExpression, recordDeclaration)
}

return constructorCandidate
?: recordDeclaration
.startInference(ctx)
.createInferredConstructor(constructExpr.signature)
.createInferredConstructor(constructExpression.signature)
}

protected fun getConstructorDeclarationForExplicitInvocation(
Expand All @@ -631,18 +631,18 @@ open class CallResolver(ctx: TranslationContext) : SymbolResolverPass(ctx) {
* Adds implicit duplicates of the TemplateParams to the implicit ConstructExpression
*
* @param templateParams of the VariableDeclaration/NewExpressionp
* @param constructExpr duplicate TemplateParameters (implicit) to preserve AST, as
* @param constructExpression duplicate TemplateParameters (implicit) to preserve AST, as
* ConstructExpression uses AST as well as the VariableDeclaration/NewExpression
*/
fun addImplicitTemplateParametersToCall(
templateParams: List<Node>,
constructExpr: ConstructExpression
constructExpression: ConstructExpression
) {
for (node in templateParams) {
if (node is TypeExpression) {
constructExpr.addTemplateParameter(node.duplicate(true))
constructExpression.addTemplateParameter(node.duplicate(true))
} else if (node is Literal<*>) {
constructExpr.addTemplateParameter(node.duplicate(true))
constructExpression.addTemplateParameter(node.duplicate(true))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,28 +104,28 @@ internal class ClassTemplateTest : BaseTest() {

private fun testClassTemplateInvocation(
pairConstructorDeclaration: ConstructorDeclaration?,
constructExpr: ConstructExpression,
constructExpression: ConstructExpression,
pair: RecordDeclaration?,
pairType: ObjectType,
template: RecordTemplateDeclaration?,
point1: VariableDeclaration
) {
assertEquals(pairConstructorDeclaration, constructExpr.constructor)
assertEquals(pairConstructorDeclaration, constructExpression.constructor)
assertNotNull(pairConstructorDeclaration)
assertTrue(constructExpr.invokes.contains(pairConstructorDeclaration))
assertEquals(pair, constructExpr.instantiates)
assertEquals(template, constructExpr.templateInstantiation)
assertLocalName("Pair", constructExpr.type)
assertEquals(constructExpr.type, point1.type)
assertNotEquals(pairType, constructExpr.type)
assertTrue(constructExpression.invokes.contains(pairConstructorDeclaration))
assertEquals(pair, constructExpression.instantiates)
assertEquals(template, constructExpression.templateInstantiation)
assertLocalName("Pair", constructExpression.type)
assertEquals(constructExpression.type, point1.type)
assertNotEquals(pairType, constructExpression.type)

val instantiatedType = constructExpr.type as? ObjectType
val instantiatedType = constructExpression.type as? ObjectType
assertNotNull(instantiatedType)
assertEquals(2, instantiatedType.generics.size)
assertLocalName("int", instantiatedType.generics[0])
assertLocalName("int", instantiatedType.generics[1])

val templateParameters = constructExpr.templateParameters
val templateParameters = constructExpression.templateParameters
assertNotNull(templateParameters)
assertEquals(2, templateParameters.size)
assertLocalName("int", (templateParameters[0] as TypeExpression).type)
Expand Down Expand Up @@ -258,25 +258,25 @@ internal class ClassTemplateTest : BaseTest() {
template: RecordTemplateDeclaration?,
pair: RecordDeclaration?,
pairConstructorDeclaration: ConstructorDeclaration?,
constructExpr: ConstructExpression,
constructExpression: ConstructExpression,
point1: VariableDeclaration
) {
assertEquals(pair, constructExpr.instantiates)
assertEquals(template, constructExpr.templateInstantiation)
assertEquals(pairConstructorDeclaration, constructExpr.constructor)
assertEquals(2, constructExpr.templateParameters.size)
assertLocalName("int", constructExpr.templateParameters[0])
assertEquals(pair, constructExpression.instantiates)
assertEquals(template, constructExpression.templateInstantiation)
assertEquals(pairConstructorDeclaration, constructExpression.constructor)
assertEquals(2, constructExpression.templateParameters.size)
assertLocalName("int", constructExpression.templateParameters[0])
assertEquals(
TemplateDeclaration.TemplateInitialization.EXPLICIT,
constructExpr.templateParameterEdges?.get(0)?.getProperty(Properties.INSTANTIATION)
constructExpression.templateParameterEdges?.get(0)?.getProperty(Properties.INSTANTIATION)
)
assertLocalName("int", constructExpr.templateParameters[1])
assertLocalName("int", constructExpression.templateParameters[1])
assertEquals(
TemplateDeclaration.TemplateInitialization.EXPLICIT,
constructExpr.templateParameterEdges?.get(1)?.getProperty(Properties.INSTANTIATION)
constructExpression.templateParameterEdges?.get(1)?.getProperty(Properties.INSTANTIATION)
)

val pairTypeInstantiated = constructExpr.type as ObjectType
val pairTypeInstantiated = constructExpression.type as ObjectType
assertEquals(pair, pairTypeInstantiated.recordDeclaration)
assertEquals(2, pairTypeInstantiated.generics.size)
assertLocalName("int", pairTypeInstantiated.generics[0])
Expand Down
12 changes: 6 additions & 6 deletions docs/docs/CPG/specs/dfg.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,18 @@ Scheme:
Interesting fields:

* `condition: Expression`: The condition which is evaluated
* `thenExpr: Expression`: The expression which is executed if the condition holds
* `elseExpr: Expression`: The expression which is executed if the condition does not hold
* `thenExpression: Expression`: The expression which is executed if the condition holds
* `elseExpression: Expression`: The expression which is executed if the condition does not hold

The `thenExpr` and the `elseExpr` flow to the `ConditionalExpression`. This means that implicit data flows are not considered.

Scheme:
```mermaid
flowchart LR
thenExpr -- DFG --> node([ConditionalExpression]);
thenExpr -.- node;
elseExpr -.- node;
elseExpr -- DFG --> node;
thenExpression -- DFG --> node([ConditionalExpression]);
thenExpression -.- node;
elseExpression -.- node;
elseExpression -- DFG --> node;
```

## Reference
Expand Down
8 changes: 4 additions & 4 deletions docs/docs/CPG/specs/eog.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,17 +604,17 @@ A conditional evaluation of two expression, realizing the branching pattern of a
Interesting fields:

* `condition:Expression`: Executed first to decide the branch of evaluation.
* `thenExpr:Expression`: Evaluated if `condition` evaluates to `true.`
* `elseExpr:Expression`: Evaluated if `condition` evaluates to `false.`
* `thenExpression:Expression`: Evaluated if `condition` evaluates to `true.`
* `elseExpression:Expression`: Evaluated if `condition` evaluates to `false.`

Scheme:
```mermaid
flowchart LR
classDef outer fill:#fff,stroke:#ddd,stroke-dasharray:5 5;
prev:::outer --EOG--> child1["condition"]
child1 --EOG--> parent(["ConditionalExpression"])
parent --EOG:true--> child2["thenExpr"]
parent --EOG:false--> child3["elseExpr"]
parent --EOG:true--> child2["thenExpression"]
parent --EOG:false--> child3["elseExpression"]
child2 --EOG--> next:::outer
child3 --EOG--> next:::outer
parent -.-> child1
Expand Down

0 comments on commit 0b0eaa7

Please sign in to comment.