Skip to content

Commit

Permalink
Converted Go frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Mar 28, 2024
1 parent 7800b56 commit 6eee05b
Show file tree
Hide file tree
Showing 15 changed files with 526 additions and 595 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement
import de.fraunhofer.aisec.cpg.graph.statements.ForStatement
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.passes.EdgeCachePass
import de.fraunhofer.aisec.cpg.passes.astParent
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Reference
import de.fraunhofer.aisec.cpg.passes.astParent
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package de.fraunhofer.aisec.cpg.frontends

import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression
import de.fraunhofer.aisec.cpg.helpers.Util.errorWithFileLocation
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,11 @@ fun MetadataProvider.newUnaryOperator(
@JvmOverloads
fun MetadataProvider.newAssignExpression(
operatorCode: String = "=",
lhs: List<Expression> = listOf(),
rhs: List<Expression> = listOf(),
rawNode: Any? = null
): AssignExpression {
val node = AssignExpression()
node.applyMetadata(this, operatorCode, rawNode, true)
node.operatorCode = operatorCode
node.lhs = lhs
node.rhs = rhs

log(node)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Block
import de.fraunhofer.aisec.cpg.helpers.SubgraphWalker
import de.fraunhofer.aisec.cpg.passes.EvaluationOrderGraphPass
import de.fraunhofer.aisec.cpg.passes.astParent

/**
* Flattens the AST beginning with this node and returns all nodes of type [T]. For convenience, an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import de.fraunhofer.aisec.cpg.frontends.*
import de.fraunhofer.aisec.cpg.graph.Node.Companion.EMPTY_NAME
import de.fraunhofer.aisec.cpg.graph.NodeBuilder.LOGGER
import de.fraunhofer.aisec.cpg.graph.NodeBuilder.log
import de.fraunhofer.aisec.cpg.graph.declarations.Declaration
import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration
import de.fraunhofer.aisec.cpg.graph.scopes.Scope
import de.fraunhofer.aisec.cpg.graph.statements.expressions.*
Expand Down Expand Up @@ -421,3 +422,14 @@ fun <T : Node> T.withChildren(

return this
}

context(ContextProvider)
fun <T : Declaration> T.declare(): T {
val scopeManager =
this@ContextProvider.ctx?.scopeManager
?: throw TranslationException(
"Trying to create node children without a ContextProvider. This will fail."
)
scopeManager.addDeclaration(this)
return this
}
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,11 @@ operator fun Expression.plus(rhs: Expression): BinaryOperator {
*/
context(LanguageFrontend<*, *>, StatementHolder)
operator fun Expression.plusAssign(rhs: Expression) {
val node = (this@LanguageFrontend).newAssignExpression("+=", listOf(this), listOf(rhs))
val node =
(this@LanguageFrontend).newAssignExpression("+=").withChildren {
it.rhs = listOf(rhs)
it.lhs = listOf(this)
}

(this@StatementHolder) += node
}
Expand Down Expand Up @@ -1328,7 +1332,11 @@ infix fun Expression.assign(init: AssignExpression.() -> Expression): AssignExpr
*/
context(LanguageFrontend<*, *>, Holder<out Node>)
infix fun Expression.assign(rhs: Expression): AssignExpression {
val node = (this@LanguageFrontend).newAssignExpression("=", listOf(this), listOf(rhs))
val node =
(this@LanguageFrontend).newAssignExpression("=").withChildren {
it.rhs = listOf(rhs)
it.lhs = listOf(this)
}

if (this@Holder is StatementHolder) {
this@Holder += node
Expand All @@ -1343,7 +1351,11 @@ infix fun Expression.assign(rhs: Expression): AssignExpression {
*/
context(LanguageFrontend<*, *>, Holder<out Node>)
infix fun Expression.assignPlus(rhs: Expression): AssignExpression {
val node = (this@LanguageFrontend).newAssignExpression("+=", listOf(this), listOf(rhs))
val node =
(this@LanguageFrontend).newAssignExpression("+=").withChildren {
it.rhs = listOf(rhs)
it.lhs = listOf(this)
}

if (this@Holder is StatementHolder) {
this@Holder += node
Expand All @@ -1358,7 +1370,11 @@ infix fun Expression.assignPlus(rhs: Expression): AssignExpression {
*/
context(LanguageFrontend<*, *>, Holder<out Node>)
infix fun Expression.assignAsExpr(rhs: Expression): AssignExpression {
val node = (this@LanguageFrontend).newAssignExpression("=", listOf(this), listOf(rhs))
val node =
(this@LanguageFrontend).newAssignExpression("=").withChildren {
it.rhs = listOf(rhs)
it.lhs = listOf(this)
}

node.usedAsExpression = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ open class DeclarationStatement : Statement() {
}

override fun hashCode() = Objects.hash(super.hashCode(), declarations)

operator fun plusAssign(declaration: Declaration) {
addToPropertyEdgeDeclaration(declaration)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,3 @@ class EdgeCachePass(ctx: TranslationContext) : ComponentPass(ctx) {
// nothing to do
}
}

val Node.astParent: Node?
get() {
return Edges.to(this, EdgeType.AST).firstOrNull()?.source
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ class DeclarationHandler(frontend: GoLanguageFrontend) :
// because the syntax is only there to ensure that this method is part
// of the struct, but it is not modifying the receiver.
if (recvField?.names?.isNotEmpty() == true) {
method.receiver =
newVariableDeclaration(
recvField.names[0].name,
recordType,
rawNode = recvField
)
method.withChildren {
it.receiver =
newVariableDeclaration(
recvField.names[0].name,
recordType,
rawNode = recvField
)
}
}

if (recordType !is UnknownType) {
Expand Down Expand Up @@ -103,60 +105,58 @@ class DeclarationHandler(frontend: GoLanguageFrontend) :
newFunctionDeclaration(funcDecl.name.name, localNameOnly, rawNode = funcDecl)
}

frontend.scopeManager.enterScope(func)

if (func is MethodDeclaration && func.receiver != null) {
// Add the receiver do the scope manager, so we can resolve the receiver value
frontend.scopeManager.addDeclaration(func.receiver)
}
func.withChildren(hasScope = true) {
if (func is MethodDeclaration && func.receiver != null) {
// Add the receiver do the scope manager, so we can resolve the receiver value
func.receiver?.declare()
}

val returnTypes = mutableListOf<Type>()

// Build return types (and variables)
val results = funcDecl.type.results
if (results != null) {
for (returnVar in results.list) {
returnTypes += frontend.typeOf(returnVar.type)

// If the function has named return variables, be sure to declare them as well
if (returnVar.names.isNotEmpty()) {
val param =
newVariableDeclaration(
returnVar.names[0].name,
frontend.typeOf(returnVar.type),
rawNode = returnVar
)

// Add parameter to scope
frontend.scopeManager.addDeclaration(param)
val returnTypes = mutableListOf<Type>()

// Build return types (and variables)
val results = funcDecl.type.results
if (results != null) {
for (returnVar in results.list) {
returnTypes += frontend.typeOf(returnVar.type)

// If the function has named return variables, be sure to declare them as well
if (returnVar.names.isNotEmpty()) {
val param =
newVariableDeclaration(
returnVar.names[0].name,
frontend.typeOf(returnVar.type),
rawNode = returnVar
)

// Add parameter to scope
param.declare()
}
}
}
}

func.type = frontend.typeOf(funcDecl.type)
func.returnTypes = returnTypes

// Parse parameters
handleFuncParams(funcDecl.type.params)

// Only parse function body in non-dependencies
if (!frontend.isDependency) {
// Check, if the last statement is a return statement, otherwise we insert an implicit
// one
val body = funcDecl.body?.let { frontend.statementHandler.handle(it) }
if (body is Block) {
val last = body.statements.lastOrNull()
if (last !is ReturnStatement) {
val ret = newReturnStatement()
ret.isImplicit = true
body += ret
}
func.type = frontend.typeOf(funcDecl.type)
func.returnTypes = returnTypes

// Parse parameters
handleFuncParams(funcDecl.type.params)

// Only parse function body in non-dependencies
if (!frontend.isDependency) {
// Check, if the last statement is a return statement, otherwise we insert an
// implicit one
func.body =
(funcDecl.body?.let { frontend.statementHandler.handle(it) } as? Block)
?.withChildren { body ->
val last = body.statements.lastOrNull()
if (last !is ReturnStatement) {
val ret = newReturnStatement()
ret.isImplicit = true
body += ret
}
}
}
func.body = body
}

frontend.scopeManager.leaveScope(func)

// Leave scope of record, if applicable
(func as? MethodDeclaration)?.recordDeclaration?.let {
frontend.scopeManager.leaveScope(it)
Expand Down
Loading

0 comments on commit 6eee05b

Please sign in to comment.