Skip to content

Commit

Permalink
preparing an operator call
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Jul 1, 2024
1 parent 3566bef commit a52bc60
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,29 @@ fun MetadataProvider.newCallExpression(
return node
}

/**
* Creates a new [MemberCallExpression]. The [MetadataProvider] receiver will be used to fill
* different meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin
* requires an appropriate [MetadataProvider], such as a [LanguageFrontend] as an additional
* prepended argument.
*/
@JvmOverloads
fun MetadataProvider.newOperatorCallExpression(
operatorCode: String,
callee: Expression?,
rawNode: Any? = null
): OperatorCallExpression {
val node = OperatorCallExpression()
node.applyMetadata(
this,
operatorCode,
rawNode
)

log(node)
return node
}

/**
* Creates a new [MemberCallExpression]. The [MetadataProvider] receiver will be used to fill
* different meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2024, 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.HasOperatorCode
import de.fraunhofer.aisec.cpg.graph.Name
import de.fraunhofer.aisec.cpg.graph.declarations.OperatorDeclaration

/**
* This special call expression is used when an operator (such as a [BinaryOperator]) is overloaded.
* In this case, we replace the original [BinaryOperator] with an [OperatorCallExpression], which
* points to its respective [OperatorDeclaration].
*/
class OperatorCallExpression : CallExpression(), HasOperatorCode {

override val operatorCode: String? = null

override var name: Name
get() = Name(operatorCode ?: "")
set(_) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,12 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
if (op != null) {
type = op.returnTypes.singleOrNull()?.root ?: unknownType()

// We need to insert a new call expression to our operator in between
// We need to insert a new operator call expression in between
val ref =
newMemberExpression(op.name, reference.base, operatorCode = ".")
.implicit(op.name.localName, location = reference.location)
ref.refersTo = op
var call = newMemberCallExpression(ref).codeAndLocationFrom(ref)
var call = newOperatorCallExpression(operatorCode = ".", ref).codeAndLocationFrom(ref)
call.invokes = listOf(op)

// Make the call our new base
Expand Down

0 comments on commit a52bc60

Please sign in to comment.