cgen: fix sum type assign/push from in match branch & type mod

pull/4792/head
joe-conigliaro 2020-05-11 16:59:55 +10:00 committed by GitHub
parent 64ba59590e
commit 1b3cd7abe0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 11 deletions

View File

@ -773,7 +773,7 @@ pub fn expr_is_call(expr Expr) bool {
} }
} }
fn (expr Expr) position() token.Position { pub fn (expr Expr) position() token.Position {
// all uncommented have to be implemented // all uncommented have to be implemented
match mut expr { match mut expr {
ArrayInit { ArrayInit {
@ -867,7 +867,7 @@ fn (expr Expr) position() token.Position {
} }
} }
fn (stmt Stmt) position() token.Position { pub fn (stmt Stmt) position() token.Position {
match mut stmt { match mut stmt {
AssertStmt { AssertStmt {
return it.pos return it.pos

View File

@ -1821,7 +1821,7 @@ pub fn (mut c Checker) ident(ident mut ast.Ident) table.Type {
} }
// Non-anon-function object (not a call), e.g. `onclick(my_click)` // Non-anon-function object (not a call), e.g. `onclick(my_click)`
if func := c.table.find_fn(name) { if func := c.table.find_fn(name) {
fn_type := table.new_type(c.table.find_or_register_fn_type(func, false, true)) fn_type := table.new_type(c.table.find_or_register_fn_type(ident.mod, func, false, true))
ident.name = name ident.name = name
ident.kind = .function ident.kind = .function
ident.info = ast.IdentFn{ ident.info = ast.IdentFn{

View File

@ -979,10 +979,10 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} }
} else { } else {
g.write(' = ') g.write(' = ')
if !is_decl { if is_decl {
g.expr_with_cast(val, assign_stmt.left_types[i], ident_var_info.typ)
} else {
g.expr(val) g.expr(val)
} else {
g.expr_with_cast(val, assign_stmt.left_types[i], ident_var_info.typ)
} }
} }
if gen_or { if gen_or {

View File

@ -307,7 +307,7 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
} }
name := 'anon_${p.tok.pos}_$func.signature()' name := 'anon_${p.tok.pos}_$func.signature()'
func.name = name func.name = name
idx := p.table.find_or_register_fn_type(func, true, false) idx := p.table.find_or_register_fn_type(p.mod, func, true, false)
typ := table.new_type(idx) typ := table.new_type(idx)
// name := p.table.get_type_name(typ) // name := p.table.get_type_name(typ)
return ast.AnonFn{ return ast.AnonFn{

View File

@ -84,7 +84,7 @@ pub fn (mut p Parser) parse_fn_type(name string) table.Type {
is_variadic: is_variadic is_variadic: is_variadic
return_type: return_type return_type: return_type
} }
idx := p.table.find_or_register_fn_type(func, false, false) idx := p.table.find_or_register_fn_type(p.mod, func, false, false)
return table.new_type(idx) return table.new_type(idx)
} }

View File

@ -1211,6 +1211,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
p.table.register_type_symbol(table.TypeSymbol{ p.table.register_type_symbol(table.TypeSymbol{
kind: .enum_ kind: .enum_
name: name name: name
mod: p.mod
info: table.Enum{ info: table.Enum{
vals: vals vals: vals
} }
@ -1264,6 +1265,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
p.table.register_type_symbol(table.TypeSymbol{ p.table.register_type_symbol(table.TypeSymbol{
kind: .sum_type kind: .sum_type
name: p.prepend_mod(name) name: p.prepend_mod(name)
mod: p.mod
info: table.SumType{ info: table.SumType{
variants: sum_variants variants: sum_variants
} }
@ -1283,6 +1285,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
kind: .alias kind: .alias
name: p.prepend_mod(name) name: p.prepend_mod(name)
parent_idx: pid parent_idx: pid
mod: p.mod
info: table.Alias{ info: table.Alias{
foo: '' foo: ''
} }

View File

@ -275,6 +275,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
t := table.TypeSymbol{ t := table.TypeSymbol{
kind: .interface_ kind: .interface_
name: interface_name name: interface_name
mod: p.mod
info: table.Interface{ info: table.Interface{
types: [] types: []
} }

View File

@ -357,94 +357,117 @@ pub fn (mut t Table) register_builtin_type_symbols() {
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .void kind: .void
name: 'void' name: 'void'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .voidptr kind: .voidptr
name: 'voidptr' name: 'voidptr'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .byteptr kind: .byteptr
name: 'byteptr' name: 'byteptr'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .charptr kind: .charptr
name: 'charptr' name: 'charptr'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .i8 kind: .i8
name: 'i8' name: 'i8'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .i16 kind: .i16
name: 'i16' name: 'i16'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .int kind: .int
name: 'int' name: 'int'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .i64 kind: .i64
name: 'i64' name: 'i64'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .byte kind: .byte
name: 'byte' name: 'byte'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .u16 kind: .u16
name: 'u16' name: 'u16'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .u32 kind: .u32
name: 'u32' name: 'u32'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .u64 kind: .u64
name: 'u64' name: 'u64'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .f32 kind: .f32
name: 'f32' name: 'f32'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .f64 kind: .f64
name: 'f64' name: 'f64'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .char kind: .char
name: 'char' name: 'char'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .bool kind: .bool
name: 'bool' name: 'bool'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .none_ kind: .none_
name: 'none' name: 'none'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .string kind: .string
name: 'string' name: 'string'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .ustring kind: .ustring
name: 'ustring' name: 'ustring'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .array kind: .array
name: 'array' name: 'array'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .map kind: .map
name: 'map' name: 'map'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .any kind: .any
name: 'any' name: 'any'
mod: 'builtin'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .size_t kind: .size_t
name: 'size_t' name: 'size_t'
mod: 'builtin'
}) })
// TODO: remove. for v1 map compatibility // TODO: remove. for v1 map compatibility
map_string_string_idx := t.find_or_register_map(string_type, string_type) map_string_string_idx := t.find_or_register_map(string_type, string_type)
@ -452,11 +475,13 @@ pub fn (mut t Table) register_builtin_type_symbols() {
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .alias kind: .alias
name: 'map_string' name: 'map_string'
mod: 'builtin'
parent_idx: map_string_string_idx parent_idx: map_string_string_idx
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
kind: .alias kind: .alias
name: 'map_int' name: 'map_int'
mod: 'builtin'
parent_idx: map_string_int_idx parent_idx: map_string_int_idx
}) })
} }

View File

@ -374,12 +374,13 @@ pub fn (mut t Table) find_or_register_multi_return(mr_typs []Type) int {
return t.register_type_symbol(mr_type) return t.register_type_symbol(mr_type)
} }
pub fn (mut t Table) find_or_register_fn_type(f Fn, is_anon, has_decl bool) int { pub fn (mut t Table) find_or_register_fn_type(mod string, f Fn, is_anon, has_decl bool) int {
name := if f.name.len == 0 { 'anon_fn_$f.signature()' } else { f.name } name := if f.name.len == 0 { 'anon_fn_$f.signature()' } else { f.name }
anon := f.name.len == 0 || is_anon anon := f.name.len == 0 || is_anon
return t.register_type_symbol(TypeSymbol{ return t.register_type_symbol(TypeSymbol{
kind: .function kind: .function
name: name name: name
mod: mod
info: FnType{ info: FnType{
is_anon: anon is_anon: anon
has_decl: has_decl has_decl: has_decl
@ -516,13 +517,15 @@ pub fn (t &Table) check(got, expected Type) bool {
// sum type // sum type
if got_type_sym.kind == .sum_type { if got_type_sym.kind == .sum_type {
sum_info := got_type_sym.info as SumType sum_info := got_type_sym.info as SumType
if expected in sum_info.variants { // TODO: handle `match SumType { &PtrVariant {} }` currently just checking base
if expected.set_nr_muls(0) in sum_info.variants {
return true return true
} }
} }
if exp_type_sym.kind == .sum_type { if exp_type_sym.kind == .sum_type {
sum_info := exp_type_sym.info as SumType sum_info := exp_type_sym.info as SumType
if got in sum_info.variants { // TODO: handle `match SumType { &PtrVariant {} }` currently just checking base
if got.set_nr_muls(0) in sum_info.variants {
return true return true
} }
} }

View File

@ -33,3 +33,21 @@ fn test_expr() {
assert handle(expr) == 'int' assert handle(expr) == 'int'
// assert expr is IntegerLiteral // TODO // assert expr is IntegerLiteral // TODO
} }
fn test_assignment_and_push() {
mut expr1 := Expr{}
mut arr1 := []Expr{}
expr := IntegerLiteral{
val: '111'
}
arr1 << expr
match arr1[0] {
IntegerLiteral {
arr1 << it
// should ref/dereference on assignent be made automatic?
// currently it is done for return stmt and fn args
expr1 = *it
}
else {}
}
}