Skip to content

Commit

Permalink
Fixed scopes of record declaration and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Aug 11, 2023
1 parent 112ef5f commit 32fd851
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ class DeclarationHandler(frontend: GoLanguageFrontend) :
val recv = funcDecl.recv
val func =
if (recv != null) {
val method = newMethodDeclaration(funcDecl.name.name, rawNode = funcDecl)
val recvField = recv.list.firstOrNull()
val recordType = recvField?.type?.let { frontend.typeOf(it) } ?: unknownType()

val method =
newMethodDeclaration(
Name(funcDecl.name.name, recordType.name),
rawNode = funcDecl
)

// The name of the Go receiver is optional. In fact, if the name is not
// specified we probably do not need any receiver variable at all,
// because the syntax is only there to ensure that this method is part
Expand Down Expand Up @@ -95,7 +100,13 @@ class DeclarationHandler(frontend: GoLanguageFrontend) :
// associated members of the class and the pure AST nodes declared in the
// struct
// itself
record?.addMethod(method)
if (record != null) {
method.recordDeclaration = record
record.addMethod(method)

// Enter scope of record
frontend.scopeManager.enterScope(record)
}
}
method
} else {
Expand Down Expand Up @@ -186,6 +197,11 @@ class DeclarationHandler(frontend: GoLanguageFrontend) :

frontend.scopeManager.leaveScope(func)

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

return func
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,12 @@ class DeclarationTest {
) {
it.registerLanguage<GoLanguage>()
}

assertNotNull(tu)

val p = tu.getDeclarationsByName("p", NamespaceDeclaration::class.java).iterator().next()

val myStruct =
p.getDeclarationsByName("p.MyStruct", RecordDeclaration::class.java).iterator().next()
val p = tu.namespaces["p"]
assertNotNull(p)

val myStruct = p.records["MyStruct"]
assertNotNull(myStruct)
assertEquals("struct", myStruct.kind)

Expand All @@ -126,8 +124,7 @@ class DeclarationTest {

var myFunc = methods.firstOrNull()
assertNotNull(myFunc)

assertLocalName("MyFunc", myFunc)
assertFullName("p.MyStruct.MyFunc", myFunc)

val myField = fields.firstOrNull()
assertNotNull(myField)
Expand Down

0 comments on commit 32fd851

Please sign in to comment.