Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ class IntegrationTest {
@Test
fun testProject() {
val project =
Project.Companion.buildProject(
"src/test/resources/golang/integration",
"darwin",
"arm64",
)
Project.buildProject("src/test/resources/golang/integration", "darwin", "arm64")

val app = project.components[TranslationResult.Companion.DEFAULT_APPLICATION_NAME]
val app = project.components[TranslationResult.DEFAULT_APPLICATION_NAME]
assertNotNull(app)
assertNotNull(app.firstOrNull { it.endsWith("main.go") })
assertNotNull(app.firstOrNull { it.endsWith("func_darwin.go") })
Expand All @@ -58,7 +54,7 @@ class IntegrationTest {

val tus =
analyzeWithBuilder(
TranslationConfiguration.Companion.builder()
TranslationConfiguration.builder()
.softwareComponents(project.components)
.symbols(project.symbols)
.includePath(project.includePaths.first().path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DeclarationHandler(frontend: GoLanguageFrontend) :
is GoStandardLibrary.Ast.FuncDecl -> handleFuncDecl(node)
is GoStandardLibrary.Ast.GenDecl -> handleGenDecl(node)
else -> {
return handleNotSupported(node, node.goType)
handleNotSupported(node, node.goType)
}
}
}
Expand Down Expand Up @@ -201,7 +201,7 @@ class DeclarationHandler(frontend: GoLanguageFrontend) :
// Create one param variable per name
for (name in names) {
// Check for varargs. In this case we want to parse the element type
// (and make it an array afterwards)
// (and make it an array afterward)
val (type, variadic) = frontend.fieldTypeOf(param.type)

val p = newParameterDeclaration(name, type, variadic, rawNode = param)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ class ExpressionHandler(frontend: GoLanguageFrontend) :
is GoStandardLibrary.Ast.CallExpr -> handleCallExpr(node)
is GoStandardLibrary.Ast.KeyValueExpr -> handleKeyValueExpr(node)
is GoStandardLibrary.Ast.ParenExpr -> {
return handle(node.x)
handle(node.x)
}
is GoStandardLibrary.Ast.SelectorExpr -> handleSelectorExpr(node)
is GoStandardLibrary.Ast.SliceExpr -> handleSliceExpr(node)
is GoStandardLibrary.Ast.StarExpr -> handleStarExpr(node)
is GoStandardLibrary.Ast.TypeAssertExpr -> handleTypeAssertExpr(node)
is GoStandardLibrary.Ast.UnaryExpr -> handleUnaryExpr(node)
else -> {
return handleNotSupported(node, node.goType)
handleNotSupported(node, node.goType)
}
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ class ExpressionHandler(frontend: GoLanguageFrontend) :
when {
name in builtins -> name
isPackageName(name) -> name
language?.namespaceDelimiter.toString() in name -> name
language.namespaceDelimiter in name -> name
else -> parseName((scope as? NameScope)?.name?.fqn(ident.name) ?: ident.name)
}

Expand All @@ -173,7 +173,7 @@ class ExpressionHandler(frontend: GoLanguageFrontend) :
}

private fun handleCallExpr(callExpr: GoStandardLibrary.Ast.CallExpr): Expression {
// In Go, regular cast expressions (not type asserts are modelled as calls).
// In Go, regular cast expressions (not type asserts are modeled as calls).
// In this case, the Fun contains a type expression.
when (val unwrapped = unwrap(callExpr.`fun`)) {
is GoStandardLibrary.Ast.ArrayType,
Expand Down Expand Up @@ -363,8 +363,8 @@ class ExpressionHandler(frontend: GoLanguageFrontend) :
}

/**
* This function handles a ast.SliceExpr, which is an extended version of ast.IndexExpr. We are
* modelling this as a combination of a [SubscriptExpression] that contains a [RangeExpression]
* This function handles an ast.SliceExpr, which is an extended version of ast.IndexExpr. We are
* modeling this as a combination of a [SubscriptExpression] that contains a [RangeExpression]
* as its subscriptExpression to share some code between this and an index expression.
*/
private fun handleSliceExpr(sliceExpr: GoStandardLibrary.Ast.SliceExpr): SubscriptExpression {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class GoLanguage :
}
}

// We additionally want to emulate the behaviour of Go's interface system here
// We additionally want to emulate the behavior of Go's interface system here
if (targetType.isInterface) {
var b: CastResult = DirectMatch
val target = (type.root as? ObjectType)?.recordDeclaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class GoLanguageFrontend(ctx: TranslationContext, language: Language<GoLanguageF

// Make sure, that our top level is set either way
val topLevel =
// If this file is part of an include, we set the top level to the root of the include
// If this file is part of an include, we set the top level to the root of the include.
when {
dependency != null -> {
isDependency = true
Expand Down Expand Up @@ -321,9 +321,9 @@ class GoLanguageFrontend(ctx: TranslationContext, language: Language<GoLanguageF
// Create an anonymous struct, this will add it to the scope manager. This is
// somewhat duplicate, but the easiest for now. We need to create it in the
// global scope to avoid namespace issues
var record =
val record =
scopeManager.withScope(scopeManager.globalScope) {
var record = specificationHandler.buildRecordDeclaration(type, name)
val record = specificationHandler.buildRecordDeclaration(type, name)
scopeManager.addDeclaration(record)
currentTU?.declarations += record
record
Expand Down Expand Up @@ -395,6 +395,7 @@ class GoLanguageFrontend(ctx: TranslationContext, language: Language<GoLanguageF
return Pair(type, variadic)
}

// TODO function is never used -> missing implementation?
private fun isBuiltinType(name: String): Boolean {
return language.primitiveTypeNames.contains(name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface GoStandardLibrary : Library {
* This class represents the Go `go/token` package and contains classes representing structs in
* this package.
*/
class Token {}
class Token

/**
* This class represents the Go `go/parser` package and contains classes representing structs in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SpecificationHandler(frontend: GoLanguageFrontend) :
is GoStandardLibrary.Ast.TypeSpec -> handleTypeSpec(node)
is GoStandardLibrary.Ast.ValueSpec -> handleValueSpec(node)
else -> {
return handleNotSupported(node, node.goType)
handleNotSupported(node, node.goType)
}
}
}
Expand Down Expand Up @@ -205,7 +205,8 @@ class SpecificationHandler(frontend: GoLanguageFrontend) :

for (ident in valueSpec.names) {
// We want to make sure that top-level declarations, i.e, the ones that are directly
// in a namespace are FQNs. Otherwise we cannot resolve them properly when we access
// in a namespace are FQNs. Otherwise, we cannot resolve them properly when we
// access
// them outside of the package.
val fqn =
if (frontend.scopeManager.currentScope is NameScope) {
Expand Down Expand Up @@ -237,7 +238,8 @@ class SpecificationHandler(frontend: GoLanguageFrontend) :

for ((nameIdx, ident) in valueSpec.names.withIndex()) {
// We want to make sure that top-level declarations, i.e, the ones that are directly
// in a namespace are FQNs. Otherwise we cannot resolve them properly when we access
// in a namespace are FQNs. Otherwise, we cannot resolve them properly when we
// access
// them outside of the package.
val fqn =
if (frontend.scopeManager.currentScope is NameScope) {
Expand Down Expand Up @@ -299,6 +301,7 @@ class SpecificationHandler(frontend: GoLanguageFrontend) :
}
}

// TODO Never used -> missing implementation?
private fun handleFuncTypeSpec(
spec: GoStandardLibrary.Ast.TypeSpec,
type: GoStandardLibrary.Ast.FuncType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class StatementHandler(frontend: GoLanguageFrontend) :
is GoStandardLibrary.Ast.DeclStmt -> handleDeclStmt(node)
is GoStandardLibrary.Ast.DeferStmt -> handleDeferStmt(node)
is GoStandardLibrary.Ast.ExprStmt -> {
return frontend.expressionHandler.handle(node.x)
frontend.expressionHandler.handle(node.x)
}
is GoStandardLibrary.Ast.ForStmt -> handleForStmt(node)
is GoStandardLibrary.Ast.GoStmt -> handleGoStmt(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class GoEvaluationOrderGraphPass(ctx: TranslationContext) : EvaluationOrderGraph
attachToEOG(node)

// Evaluate the callee
input.callee?.let { handleEOG(it) }
handleEOG(input.callee)

// Then the arguments
for (arg in input.arguments) {
Expand Down Expand Up @@ -114,7 +114,7 @@ class GoEvaluationOrderGraphPass(ctx: TranslationContext) : EvaluationOrderGraph
// It is a bit philosophical whether the deferred call happens before or after the
// return statement in the EOG. For now, it is easier to have it as the last node
// AFTER the return statement
val eogEdge = addEOGEdge(path.nodes.last(), defer.input)
addEOGEdge(path.nodes.last(), defer.input)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class GoExtraPass(ctx: TranslationContext) : ComponentPass(ctx) {

private lateinit var walker: SubgraphWalker.ScopedWalker<AstNode>

// Note: Code analysis suggests that this property is non-nullable.
override val scope: Scope?
get() = scopeManager.currentScope

Expand Down Expand Up @@ -243,7 +244,7 @@ class GoExtraPass(ctx: TranslationContext) : ComponentPass(ctx) {
type
}

// The type of a "inner" composite literal can be omitted if the outer one is creating
// The type of an "inner" composite literal can be omitted if the outer one is creating
// an array type. In this case, we need to set the type manually because the type for
// the "inner" one is empty.
// Example code:
Expand All @@ -258,28 +259,34 @@ class GoExtraPass(ctx: TranslationContext) : ComponentPass(ctx) {
// }
if (type is PointerType && type.isArray) {
for (init in node.initializers) {
if (init is InitializerListExpression) {
init.type = type.elementType
} else if (init is KeyValueExpression && init.value is InitializerListExpression) {
init.value?.type = type.elementType
} else if (init is KeyValueExpression && init.key is InitializerListExpression) {
init.key?.type = type.elementType
when (init) {
is InitializerListExpression -> {
init.type = type.elementType
}

is KeyValueExpression if init.value is InitializerListExpression -> {
init.value.type = type.elementType
}

is KeyValueExpression if init.key is InitializerListExpression -> {
init.key.type = type.elementType
}
}
}
} else if (type?.isMap == true) {
for (init in node.initializers) {
if (init is KeyValueExpression) {
if (init.key is InitializerListExpression) {
init.key?.type = (type as ObjectType).generics.getOrNull(0) ?: unknownType()
init.key.type = (type as ObjectType).generics.getOrNull(0) ?: unknownType()
} else if (init.value is InitializerListExpression) {
init.value?.type =
init.value.type =
(type as ObjectType).generics.getOrNull(1) ?: unknownType()
}
}
}
}

// Afterwards, we are not interested in arrays and maps, but only the "inner" single-object
// Afterward, we are not interested in arrays and maps, but only the "inner" single-object
// expressions
if (
type is UnknownType ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class GoLanguageFrontendTest : BaseTest() {

val n = p.variables["n"]
assertNotNull(n)
with(tu) { assertEquals(tu.primitiveType("int").pointer(), n.type) }
assertEquals(tu.primitiveType("int").pointer(), n.type)

val nil = n.initializer as? Literal<*>
assertNotNull(nil)
Expand Down

This file was deleted.

Loading