diff --git a/core/codegen/declarations.go b/core/codegen/declarations.go index 7eec291..16bdaea 100644 --- a/core/codegen/declarations.go +++ b/core/codegen/declarations.go @@ -2,6 +2,7 @@ package codegen import ( "fireball/core/ast" + "fireball/core/cst" "fireball/core/ir" "fireball/core/scanner" ) @@ -40,9 +41,9 @@ func (c *codegen) VisitFunc(decl *ast.Func) { // Add this variable if struct_ := decl.Method(); struct_ != nil { name := scanner.Token{Kind: scanner.Identifier, Lexeme: "this"} - type_ := ast.Pointer{Pointee: struct_} + node := cst.Node{Kind: cst.IdentifierNode, Token: name, Range: decl.Name.Cst().Range} - c.scopes.addVariable(&ast.Token{Token_: name}, &type_, exprValue{v: function.Typ.Params[0]}, 1) + c.scopes.addVariable(ast.NewToken(node, name), struct_, exprValue{v: function.Typ.Params[0]}, 1) } // Copy parameters diff --git a/core/codegen/expressions.go b/core/codegen/expressions.go index 5e50637..a9c89f2 100644 --- a/core/codegen/expressions.go +++ b/core/codegen/expressions.go @@ -635,7 +635,7 @@ func (c *codegen) VisitCall(expr *ast.Call) { for i, arg := range expr.Args { if hasThis { - args[i+1] = c.loadExpr(arg).v + args[i+1] = c.implicitCastLoadExpr(function.Params[i].Type, arg).v } else if i >= len(function.Params) { args[i] = c.loadExpr(arg).v } else { @@ -657,7 +657,7 @@ func (c *codegen) VisitCall(expr *ast.Call) { Args: args, }) - c.setLocationMeta(result, expr) + c.setLocationMetaCst(result, expr, scanner.LeftParen) c.exprResult = exprValue{v: result} // If the function returns a constant-sized array and the array is immediately indexed then store it in an alloca first @@ -696,7 +696,7 @@ func (c *codegen) VisitIndex(expr *ast.Index) { Inbounds: true, }) - c.setLocationMeta(result, expr) + c.setLocationMetaCst(result, expr, scanner.LeftBracket) c.exprResult = exprValue{ v: result, @@ -739,7 +739,7 @@ func (c *codegen) VisitMember(expr *ast.Member) { Inbounds: true, }) - c.setLocationMeta(result, expr) + c.setLocationMeta(result, expr.Name) c.exprResult = exprValue{ v: result, @@ -751,7 +751,7 @@ func (c *codegen) VisitMember(expr *ast.Member) { Indices: []uint32{uint32(node.Index())}, }) - c.setLocationMeta(result, expr) + c.setLocationMeta(result, expr.Name) c.exprResult = exprValue{v: result} } } diff --git a/core/codegen/instructions.go b/core/codegen/instructions.go index be85ad5..b965b53 100644 --- a/core/codegen/instructions.go +++ b/core/codegen/instructions.go @@ -3,6 +3,7 @@ package codegen import ( "fireball/core/ast" "fireball/core/ir" + "fireball/core/scanner" ) func (c *codegen) alloca(type_ ast.Type, name string, node ast.Node) ir.Value { @@ -40,3 +41,22 @@ func (c *codegen) setLocationMeta(value ir.MetaValue, node ast.Node) { value.SetMeta(c.module.Meta(meta)) } + +func (c *codegen) setLocationMetaCst(value ir.MetaValue, node ast.Node, kind scanner.TokenKind) { + if node == nil { + return + } + + meta := &ir.LocationMeta{ + Scope: c.scopes.getMeta(), + } + + if node.Cst() != nil { + token := node.Cst().Get(kind) + + meta.Line = uint32(token.Range.Start.Line) + meta.Column = uint32(token.Range.Start.Column) + } + + value.SetMeta(c.module.Meta(meta)) +} diff --git a/core/codegen/types.go b/core/codegen/types.go index cb17d9f..1c6fd4b 100644 --- a/core/codegen/types.go +++ b/core/codegen/types.go @@ -269,11 +269,13 @@ func (t *types) getMeta(type_ ast.Type) ir.MetaID { Elements: []ir.MetaID{ t.module.Meta(&ir.DerivedTypeMeta{ Tag: ir.MemberTag, + Name: "vtable", BaseType: t.getMeta(&ptr), Offset: 0, }), t.module.Meta(&ir.DerivedTypeMeta{ Tag: ir.MemberTag, + Name: "data", BaseType: t.getMeta(&ptr), Offset: ptr.Size() * 8, }), @@ -380,6 +382,7 @@ func (t *types) getMeta(type_ ast.Type) ir.MetaID { fields[i] = t.module.Meta(&ir.DerivedTypeMeta{ Tag: ir.MemberTag, + Name: field.Name.String(), BaseType: t.getMeta(field.Type), Offset: offset * 8, }) diff --git a/core/ir/metadata.go b/core/ir/metadata.go index 0c80ab9..c178afd 100644 --- a/core/ir/metadata.go +++ b/core/ir/metadata.go @@ -261,6 +261,7 @@ type DerivedTypeMeta struct { baseMeta Tag DerivedTagKind + Name string BaseType MetaID Size uint32 diff --git a/core/llvm/metadata.go b/core/llvm/metadata.go index 471dc80..7ca8d8b 100644 --- a/core/llvm/metadata.go +++ b/core/llvm/metadata.go @@ -144,6 +144,11 @@ func (w *textWriter) writeMeta(meta ir.Meta) { w.writeString("tag: ") w.writeString(meta.Tag.String()) + if meta.Name != "" { + w.writeString(", name: ") + w.writeQuotedString(meta.Name) + } + w.writeString(", baseType: ") w.writeMetaRef(meta.BaseType) @@ -340,7 +345,7 @@ func (w *textWriter) writeMeta(meta ir.Meta) { w.writeUint(uint64(meta.Line), 10) w.writeString(", column: ") - w.writeUint(uint64(meta.Column), 10) + w.writeUint(uint64(meta.Column)+1, 10) w.writeRune(')')