Skip to content
Merged
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
29 changes: 29 additions & 0 deletions ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@
// TODO: Add support for NamedArgs
}

BLangTypeTestExpr struct {
BLangExpressionBase
Expr BLangExpression
Type model.TypeData
isNegation bool
}

BLangMappingKey struct {
BLangNodeBase
Expr BLangExpression
Expand Down Expand Up @@ -335,6 +342,8 @@
_ BLangExpression = &BLangTypeConversionExpr{}
_ BLangExpression = &BLangErrorConstructorExpr{}
_ BLangNode = &BLangErrorConstructorExpr{}
_ BLangExpression = &BLangTypeTestExpr{}
_ model.TypeTestExpressionNode = &BLangTypeTestExpr{}
_ model.MappingConstructor = &BLangMappingConstructorExpr{}
_ model.MappingKeyValueFieldNode = &BLangMappingKeyValueField{}
_ BLangExpression = &BLangMappingConstructorExpr{}
Expand Down Expand Up @@ -982,6 +991,26 @@
panic("not implemented")
}

func (this *BLangTypeTestExpr) GetKind() model.NodeKind {
return model.NodeKind_TYPE_TEST_EXPR

Check warning on line 995 in ast/expressions.go

View check run for this annotation

Codecov / codecov/patch

ast/expressions.go#L994-L995

Added lines #L994 - L995 were not covered by tests
}

func (this *BLangTypeTestExpr) IsNegation() bool {
return this.isNegation
}

func (this *BLangTypeTestExpr) GetExpression() model.ExpressionNode {
return this.Expr

Check warning on line 1003 in ast/expressions.go

View check run for this annotation

Codecov / codecov/patch

ast/expressions.go#L1002-L1003

Added lines #L1002 - L1003 were not covered by tests
}

func (this *BLangTypeTestExpr) GetType() model.TypeData {
return this.Type

Check warning on line 1007 in ast/expressions.go

View check run for this annotation

Codecov / codecov/patch

ast/expressions.go#L1006-L1007

Added lines #L1006 - L1007 were not covered by tests
}

func (this *BLangTypeTestExpr) SetTypeCheckedType(ty BType) {
panic("not implemented")

Check warning on line 1011 in ast/expressions.go

View check run for this annotation

Codecov / codecov/patch

ast/expressions.go#L1010-L1011

Added lines #L1010 - L1011 were not covered by tests
}

func (this *BLangMappingKey) GetKind() model.NodeKind {
panic("BLangMappingKey has no NodeKind")
}
Expand Down
9 changes: 8 additions & 1 deletion ast/node_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2135,7 +2135,14 @@
}

func (n *NodeBuilder) TransformTypeTestExpression(typeTestExpressionNode *tree.TypeTestExpressionNode) BLangNode {
panic("TransformTypeTestExpression unimplemented")
typeTestExpr := &BLangTypeTestExpr{}
if typeTestExpressionNode.IsKeyword().Kind() == common.NOT_IS_KEYWORD {
typeTestExpr.isNegation = true

Check warning on line 2140 in ast/node_builder.go

View check run for this annotation

Codecov / codecov/patch

ast/node_builder.go#L2140

Added line #L2140 was not covered by tests
}
typeTestExpr.Expr = n.createExpression(typeTestExpressionNode.Expression())
typeTestExpr.Type = model.TypeData{TypeDescriptor: n.createTypeNode(typeTestExpressionNode.TypeDescriptor())}
typeTestExpr.SetPosition(getPosition(typeTestExpressionNode))
return typeTestExpr
}

func (n *NodeBuilder) TransformRemoteMethodCallAction(remoteMethodCallActionNode *tree.RemoteMethodCallActionNode) BLangNode {
Expand Down
18 changes: 18 additions & 0 deletions ast/pretty_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
p.printMappingConstructor(t)
case *BLangTypeConversionExpr:
p.printTypeConversionExpr(t)
case *BLangTypeTestExpr:
p.printTypeTestExpr(t)
default:
fmt.Println(p.buffer.String())
panic("Unsupported node type: " + reflect.TypeOf(t).String())
Expand Down Expand Up @@ -529,6 +531,22 @@
p.endNode()
}

func (p *PrettyPrinter) printTypeTestExpr(node *BLangTypeTestExpr) {
p.startNode()
if node.isNegation {
p.printString("type-test-expr !is")

Check warning on line 537 in ast/pretty_printer.go

View check run for this annotation

Codecov / codecov/patch

ast/pretty_printer.go#L537

Added line #L537 was not covered by tests
} else {
p.printString("type-test-expr is")
}
p.indentLevel++
p.PrintInner(node.Expr.(BLangNode))
if node.Type.TypeDescriptor != nil {
p.PrintInner(node.Type.TypeDescriptor.(BLangNode))
}
p.indentLevel--
p.endNode()
}

// While loop printer
func (p *PrettyPrinter) printWhile(node *BLangWhile) {
p.startNode()
Expand Down
6 changes: 6 additions & 0 deletions ast/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ func Walk(v Visitor, node BLangNode) {
Walk(v, node.TypeDescriptor.(BLangNode))
}

case *BLangTypeTestExpr:
if node.Expr != nil {
Walk(v, node.Expr.(BLangNode))
}
WalkTypeData(v, &node.Type)

case *BLangCommitExpr:
panic("unimplemented")

Expand Down
18 changes: 18 additions & 0 deletions bir/bir_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ func handleExpression(ctx *stmtContext, curBB *BIRBasicBlock, expr ast.BLangExpr
return listConstructorExpression(ctx, curBB, expr)
case *ast.BLangTypeConversionExpr:
return typeConversionExpression(ctx, curBB, expr)
case *ast.BLangTypeTestExpr:
return typeTestExpression(ctx, curBB, expr)
case *ast.BLangMappingConstructorExpr:
return mappingConstructorExpression(ctx, curBB, expr)
default:
Expand Down Expand Up @@ -575,6 +577,22 @@ func typeConversionExpression(ctx *stmtContext, curBB *BIRBasicBlock, expr *ast.
}
}

func typeTestExpression(ctx *stmtContext, curBB *BIRBasicBlock, expr *ast.BLangTypeTestExpr) expressionEffect {
exprEffect := handleExpression(ctx, curBB, expr.Expr)
curBB = exprEffect.block
resultOperand := ctx.addTempVar(expr.GetDeterminedType())
typeTest := &TypeTest{}
typeTest.LhsOp = resultOperand
typeTest.RhsOp = exprEffect.result
typeTest.Type = expr.Type.Type
typeTest.IsNegation = expr.IsNegation()
curBB.Instructions = append(curBB.Instructions, typeTest)
return expressionEffect{
result: resultOperand,
block: curBB,
}
}

func listConstructorExpression(ctx *stmtContext, bb *BIRBasicBlock, expr *ast.BLangListConstructorExpr) expressionEffect {
initValues := make([]*BIROperand, len(expr.Exprs))
for i, expr := range expr.Exprs {
Expand Down
16 changes: 16 additions & 0 deletions bir/non_terminator.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@
// numeric conversions, which can be done with pure types
Type semtypes.SemType
}

TypeTest struct {
BIRInstructionBase
RhsOp *BIROperand
Type semtypes.SemType
IsNegation bool
}
)

type (
Expand All @@ -106,6 +113,7 @@
_ BIRInstruction = &FieldAccess{}
_ BIRInstruction = &NewArray{}
_ BIRInstruction = &TypeCast{}
_ BIRAssignInstruction = &TypeTest{}
_ BIRInstruction = &NewMap{}
_ MappingConstructorEntry = &MappingConstructorKeyValueEntry{}
)
Expand Down Expand Up @@ -206,6 +214,14 @@
return INSTRUCTION_KIND_TYPE_CAST
}

func (t *TypeTest) GetLhsOperand() *BIROperand {
return t.LhsOp

Check warning on line 218 in bir/non_terminator.go

View check run for this annotation

Codecov / codecov/patch

bir/non_terminator.go#L217-L218

Added lines #L217 - L218 were not covered by tests
}

func (t *TypeTest) GetKind() InstructionKind {
return INSTRUCTION_KIND_TYPE_TEST

Check warning on line 222 in bir/non_terminator.go

View check run for this annotation

Codecov / codecov/patch

bir/non_terminator.go#L221-L222

Added lines #L221 - L222 were not covered by tests
}

func (n *NewMap) GetKind() InstructionKind {
return INSTRUCTION_KIND_NEW_STRUCTURE
}
Expand Down
10 changes: 10 additions & 0 deletions bir/pretty_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@
return p.PrintNewMap(instruction.(*NewMap))
case *TypeCast:
return p.PrintTypeCast(instruction.(*TypeCast))
case *TypeTest:
return p.PrintTypeTest(instruction.(*TypeTest))
default:
panic(fmt.Sprintf("unknown instruction type: %T", instruction))
}
Expand All @@ -150,6 +152,14 @@
return fmt.Sprintf("%s = <%s>(%s)", p.PrintOperand(*cast.LhsOp), cast.Type.String(), p.PrintOperand(*cast.RhsOp))
}

func (p *PrettyPrinter) PrintTypeTest(test *TypeTest) string {
op := "is"
if test.IsNegation {
op = "!is"

Check warning on line 158 in bir/pretty_print.go

View check run for this annotation

Codecov / codecov/patch

bir/pretty_print.go#L158

Added line #L158 was not covered by tests
}
return fmt.Sprintf("%s = %s %s %s", p.PrintOperand(*test.LhsOp), p.PrintOperand(*test.RhsOp), op, test.Type.String())
}

func (p *PrettyPrinter) PrintNewArray(array *NewArray) string {
values := strings.Builder{}
for i, v := range array.Values {
Expand Down
28 changes: 28 additions & 0 deletions corpus/ast/subset4/04-typetest/1-v.txt

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

21 changes: 21 additions & 0 deletions corpus/ast/subset4/04-typetest/2-v.txt

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

27 changes: 27 additions & 0 deletions corpus/bal/subset4/04-typetest/1-v.bal

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

21 changes: 21 additions & 0 deletions corpus/bal/subset4/04-typetest/2-v.bal

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

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

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

15 changes: 15 additions & 0 deletions corpus/bir/subset4/04-typetest/2-v.txt

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

27 changes: 27 additions & 0 deletions corpus/cfg/subset4/04-typetest/1-v.txt

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

Loading