-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding
LambdaExpression
for lambda functions (#592)
This PR adds a node type type LambdaExpression. It can be used for lambda functions that are assigned to a variable or are given as an argument in a call expression. The type of the expression is a FunctionPointerType which corresponds to the signature of its function. Co-authored-by: Konrad Weiss <[email protected]>
- Loading branch information
1 parent
9352d67
commit 793b58b
Showing
8 changed files
with
181 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...ry/src/main/java/de/fraunhofer/aisec/cpg/graph/statements/expressions/LambdaExpression.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) 2021, Fraunhofer AISEC. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* $$$$$$\ $$$$$$$\ $$$$$$\ | ||
* $$ __$$\ $$ __$$\ $$ __$$\ | ||
* $$ / \__|$$ | $$ |$$ / \__| | ||
* $$ | $$$$$$$ |$$ |$$$$\ | ||
* $$ | $$ ____/ $$ |\_$$ | | ||
* $$ | $$\ $$ | $$ | $$ | | ||
* \$$$$$ |$$ | \$$$$$ | | ||
* \______/ \__| \______/ | ||
* | ||
*/ | ||
package de.fraunhofer.aisec.cpg.graph.statements.expressions | ||
|
||
import de.fraunhofer.aisec.cpg.graph.HasType | ||
import de.fraunhofer.aisec.cpg.graph.SubGraph | ||
import de.fraunhofer.aisec.cpg.graph.TypeManager | ||
import de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType | ||
import de.fraunhofer.aisec.cpg.graph.types.Type | ||
|
||
/** | ||
* This expression denotes the usage of an anonymous / lambda function. It connects the inner | ||
* anonymous function to the user of a lambda function with an expression. | ||
*/ | ||
class LambdaExpression : Expression(), HasType.TypeListener { | ||
|
||
@field:SubGraph("AST") | ||
var function: FunctionDeclaration? = null | ||
set(value) { | ||
if (value != null) { | ||
removePrevDFG(value) | ||
value.unregisterTypeListener(this) | ||
if (value is HasType.TypeListener) { | ||
unregisterTypeListener(value as HasType.TypeListener?) | ||
} | ||
} | ||
field = value | ||
if (value != null) { | ||
addPrevDFG(value) | ||
value.registerTypeListener(this) | ||
} | ||
} | ||
|
||
override fun typeChanged(src: HasType?, root: HasType?, oldType: Type?) { | ||
if (!TypeManager.isTypeSystemActive()) { | ||
return | ||
} | ||
|
||
if (!TypeManager.getInstance().isUnknown(type) && src!!.propagationType == oldType) { | ||
return | ||
} | ||
|
||
if (src !is FunctionDeclaration) { | ||
return | ||
} | ||
|
||
val previous = type | ||
|
||
val parameterTypes = src.parameters.map { it.type } | ||
val returnType = src.propagationType | ||
|
||
// the incoming "type" is associated to the function and it is only its return type (if it | ||
// is known). what we really want is to construct a function type, or rather a function | ||
// pointer type, since this is the closest to what we have | ||
val functionType = | ||
FunctionPointerType( | ||
Type.Qualifier(false, false, false, false), | ||
Type.Storage.AUTO, | ||
parameterTypes, | ||
returnType | ||
) | ||
|
||
setType(functionType, root) | ||
if (previous != type) { | ||
type.typeOrigin = Type.Origin.DATAFLOW | ||
} | ||
} | ||
|
||
override fun possibleSubTypesChanged( | ||
src: HasType?, | ||
root: HasType?, | ||
oldSubTypes: MutableSet<Type>? | ||
) { | ||
// do not take sub types from the listener | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.