From b7cfbdf831f8e48c9295a7d95c38198185434802 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Thu, 3 Oct 2024 22:10:49 +0200 Subject: [PATCH] Added language trait `HasImplicitReceiver` (#1778) Added language trait `HasImplicit Receiver` This is needed in preparation for #1777 to better handle access to fields/members of a class when an implicit receiver is used. --- .../de/fraunhofer/aisec/cpg/frontends/LanguageTraits.kt | 9 +++++++++ .../de/fraunhofer/aisec/cpg/frontends/cxx/CPPLanguage.kt | 6 +++++- .../fraunhofer/aisec/cpg/frontends/java/JavaLanguage.kt | 5 ++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/LanguageTraits.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/LanguageTraits.kt index beb36b3661..dee23cc697 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/LanguageTraits.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/LanguageTraits.kt @@ -123,6 +123,15 @@ interface HasSuperClasses : LanguageTrait { ): Boolean } +/** + * A language trait, that specifies that this language has support for implicit receiver, e.g., that + * one can omit references to a base such as `this`. + */ +interface HasImplicitReceiver : LanguageTrait { + + val receiverName: String +} + /** * A language trait, that specifies that this language has certain qualifiers. If so, we should * consider them when parsing the types. diff --git a/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/CPPLanguage.kt b/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/CPPLanguage.kt index 23d7b942fd..c5fc9815fd 100644 --- a/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/CPPLanguage.kt +++ b/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/CPPLanguage.kt @@ -56,7 +56,8 @@ open class CPPLanguage : HasUnknownType, HasFunctionStyleCasts, HasFunctionOverloading, - HasOperatorOverloading { + HasOperatorOverloading, + HasImplicitReceiver { override val fileExtensions = listOf("cpp", "cc", "cxx", "c++", "hpp", "hh") override val elaboratedTypeSpecifier = listOf("class", "struct", "union", "enum") override val unknownTypeString = listOf("auto") @@ -317,4 +318,7 @@ open class CPPLanguage : return Pair(false, listOf()) } + + override val receiverName: String + get() = "this" } diff --git a/cpg-language-java/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/java/JavaLanguage.kt b/cpg-language-java/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/java/JavaLanguage.kt index 4a3ed16ee2..f0f6497559 100644 --- a/cpg-language-java/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/java/JavaLanguage.kt +++ b/cpg-language-java/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/java/JavaLanguage.kt @@ -45,7 +45,8 @@ open class JavaLanguage : HasQualifier, HasUnknownType, HasShortCircuitOperators, - HasFunctionOverloading { + HasFunctionOverloading, + HasImplicitReceiver { override val fileExtensions = listOf("java") override val namespaceDelimiter = "." @Transient override val frontend: KClass = JavaLanguageFrontend::class @@ -116,4 +117,6 @@ open class JavaLanguage : override val startCharacter = '<' override val endCharacter = '>' + override val receiverName: String + get() = "this" }