diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/ListType.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/ListType.kt new file mode 100644 index 00000000000..67f22d31394 --- /dev/null +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/ListType.kt @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023, 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.types + +import de.fraunhofer.aisec.cpg.frontends.Language + +/** Represents a [List] type that contains multiple elements. */ +class ListType(typeName: CharSequence, override var elementType: Type, language: Language<*>) : + ObjectType(typeName, listOf(elementType), false, language), SecondOrderType diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/MapType.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/MapType.kt new file mode 100644 index 00000000000..114fe0b972d --- /dev/null +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/MapType.kt @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023, 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.types + +import de.fraunhofer.aisec.cpg.frontends.Language + +/** Represents a [Map] type with key-value pairs. */ +class MapType(typeName: CharSequence, override var elementType: Type, language: Language<*>) : + ObjectType(typeName, listOf(elementType), false, language), SecondOrderType diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/SecondOrderType.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/SecondOrderType.kt index 12dc438e761..f3fe3b3f238 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/SecondOrderType.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/SecondOrderType.kt @@ -25,6 +25,7 @@ */ package de.fraunhofer.aisec.cpg.graph.types +/** Second-order types are generic container types (e.g., List, Set, Map) or pointer types. */ interface SecondOrderType { var elementType: Type diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/SetType.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/SetType.kt new file mode 100644 index 00000000000..2abf3ccbb54 --- /dev/null +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/types/SetType.kt @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023, 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.types + +import de.fraunhofer.aisec.cpg.frontends.Language + +/** Represents a [Set] type that contains unique elements. */ +class SetType(typeName: CharSequence, override var elementType: Type, language: Language<*>) : + ObjectType(typeName, listOf(elementType), false, language), SecondOrderType diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguage.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguage.kt index a0526f0255c..9537028190a 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguage.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguage.kt @@ -140,7 +140,31 @@ class PythonLanguage : this, NumericType.Modifier.NOT_APPLICABLE ), // It's two floats - "str" to StringType("str", this, listOf()) + "str" to StringType("str", this, listOf()), + "list" to + ListType( + typeName = "list", + elementType = ObjectType("object", listOf(), false, this), + language = this, + ), + "tuple" to + ListType( + typeName = "tuple", + elementType = ObjectType("object", listOf(), false, this), + language = this + ), + "dict" to + MapType( + typeName = "dict", + elementType = ObjectType("object", listOf(), false, this), + language = this + ), + "set" to + SetType( + typeName = "set", + elementType = ObjectType("object", listOf(), false, this), + language = this + ) ) override fun propagateTypeOfBinaryOperation(operation: BinaryOperator): Type { diff --git a/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt b/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt index 6775df2aafc..4f8f9082274 100644 --- a/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt +++ b/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt @@ -32,7 +32,10 @@ import de.fraunhofer.aisec.cpg.graph.Annotation import de.fraunhofer.aisec.cpg.graph.declarations.* import de.fraunhofer.aisec.cpg.graph.statements.* import de.fraunhofer.aisec.cpg.graph.statements.expressions.* +import de.fraunhofer.aisec.cpg.graph.types.ListType +import de.fraunhofer.aisec.cpg.graph.types.MapType import de.fraunhofer.aisec.cpg.graph.types.ObjectType +import de.fraunhofer.aisec.cpg.graph.types.SetType import de.fraunhofer.aisec.cpg.helpers.SubgraphWalker import de.fraunhofer.aisec.cpg.passes.ControlDependenceGraphPass import de.fraunhofer.aisec.cpg.sarif.Region @@ -1312,25 +1315,25 @@ class PythonFrontendTest : BaseTest() { assertIs(aStmt) val aStmtRhs = aStmt.rhs.singleOrNull() assertIs(aStmtRhs) - assertEquals("list", aStmtRhs.type.name.localName) + assertIs(aStmtRhs.type) val bStmt = namespace.statements[1] assertIs(bStmt) val bStmtRhs = bStmt.rhs.singleOrNull() assertIs(bStmtRhs) - assertEquals("set", bStmtRhs.type.name.localName) + assertIs(bStmtRhs.type) val cStmt = namespace.statements[2] assertIs(cStmt) val cStmtRhs = cStmt.rhs.singleOrNull() assertIs(cStmtRhs) - assertEquals("tuple", cStmtRhs.type.name.localName) + assertIs(cStmtRhs.type) val dStmt = namespace.statements[3] assertIs(dStmt) val dStmtRhs = dStmt.rhs.singleOrNull() assertIs(dStmtRhs) - assertEquals("dict", dStmtRhs.type.name.localName) + assertIs(dStmtRhs.type) val fourthStmt = namespace.statements[4] assertIs(fourthStmt)