Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func UnMask(mask Flags) common.Set[model.Flag] {

type BNodeWithSymbol interface {
model.NodeWithSymbol
BLangNode
SetSymbol(symbolRef model.SymbolRef)
}

Expand Down
8 changes: 8 additions & 0 deletions ast/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@
return model.NodeKind_BREAK
}

func (this *BLangCompoundAssignment) IsDeclaredWithVar() bool {
return false

Check warning on line 206 in ast/statements.go

View check run for this annotation

Codecov / codecov/patch

ast/statements.go#L205-L206

Added lines #L205 - L206 were not covered by tests
}

func (this *BLangCompoundAssignment) SetDeclaredWithVar(_ bool) {
panic("compound assignemnt can't be declared with var")

Check warning on line 210 in ast/statements.go

View check run for this annotation

Codecov / codecov/patch

ast/statements.go#L209-L210

Added lines #L209 - L210 were not covered by tests
}

func (this *BLangCompoundAssignment) GetOperatorKind() model.OperatorKind {
// migrated from BLangCompoundAssignment.java:59:5
return this.OpKind
Expand Down
2 changes: 1 addition & 1 deletion bir/bir_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ func binaryExpression(ctx *stmtContext, curBB *BIRBasicBlock, expr *ast.BLangBin

func simpleVariableReference(ctx *stmtContext, curBB *BIRBasicBlock, expr *ast.BLangSimpleVarRef) expressionEffect {
varName := expr.VariableName.GetValue()
symRef := expr.Symbol()
symRef := ctx.birCx.CompilerContext.UnnarrowedSymbol(expr.Symbol())

// Try local variable lookup first
if operand, ok := ctx.varMap[symRef]; ok {
Expand Down
5 changes: 4 additions & 1 deletion bir/corpus_bir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ func testBIRGeneration(t *testing.T, testPair test_util.TestCase) {
// Step 6: Generate control flow graph
cfg := semantics.CreateControlFlowGraph(cx, pkg)

// Step 7: Run semantic analysis
// Step 7: Run type narrowing
semantics.NarrowTypes(cx, pkg)

// Step 8: Run semantic analysis
semanticAnalyzer := semantics.NewSemanticAnalyzer(cx)
semanticAnalyzer.Analyze(pkg)

Expand Down
33 changes: 27 additions & 6 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import (
"ballerina-lang-go/tools/diagnostics"
"fmt"
"strconv"
"sync"
)

type CompilerContext struct {
anonTypeCount map[*model.PackageID]int
packageInterner *model.PackageIDInterner
symbolSpaces []*model.SymbolSpace
typeEnv semtypes.Env
anonTypeCount map[*model.PackageID]int
packageInterner *model.PackageIDInterner
symbolSpaces []*model.SymbolSpace
typeEnv semtypes.Env
underlyingSymbol sync.Map
}

func (this *CompilerContext) NewSymbolSpace(packageId model.PackageID) *model.SymbolSpace {
Expand Down Expand Up @@ -56,8 +58,27 @@ func (this *CompilerContext) NewBlockScope(parent model.Scope, pkg model.Package
}

func (this *CompilerContext) GetSymbol(symbol model.SymbolRef) model.Symbol {
symbolSpace := this.symbolSpaces[symbol.SpaceIndex]
return symbolSpace.Symbols[symbol.Index]
return this.symbolSpaces[symbol.SpaceIndex].SymbolAt(symbol.Index)
}

func (this *CompilerContext) CreateNarrowedSymbol(baseRef model.SymbolRef) model.SymbolRef {
symbolSpace := this.symbolSpaces[baseRef.SpaceIndex]
underlyingSymbolCopy := *this.GetSymbol(baseRef).(*model.ValueSymbol)
symbolIndex := symbolSpace.AppendSymbol(&underlyingSymbolCopy)
narrowedSymbol := model.SymbolRef{
Package: baseRef.Package,
SpaceIndex: baseRef.SpaceIndex,
Index: symbolIndex,
}
this.underlyingSymbol.Store(narrowedSymbol, baseRef)
return narrowedSymbol
}

func (this *CompilerContext) UnnarrowedSymbol(symbol model.SymbolRef) model.SymbolRef {
if underlying, ok := this.underlyingSymbol.Load(symbol); ok {
return underlying.(model.SymbolRef)
}
return symbol
}

func (this *CompilerContext) SymbolName(symbol model.SymbolRef) string {
Expand Down
52 changes: 52 additions & 0 deletions corpus/ast/subset4/04-narrowing/1-v.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions corpus/ast/subset4/04-narrowing/3-v.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions corpus/bal/subset4/04-narrowing/1-v.bal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions corpus/bal/subset4/04-narrowing/2-e.bal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions corpus/bal/subset4/04-narrowing/3-v.bal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions corpus/bal/subset4/04-narrowing/4-e.bal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions corpus/bir/subset4/04-narrowing/1-v.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions corpus/bir/subset4/04-narrowing/3-v.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading