all: make __global behave consistent with const (#9711)

pull/9804/head
Lukas Neubert 2021-04-19 16:07:47 +02:00 committed by GitHub
parent 49330af715
commit 70c651ff17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 67 additions and 38 deletions

View File

@ -4,8 +4,8 @@
module builtin module builtin
__global ( __global (
g_m2_buf &byte g_m2_buf &byte
g_m2_ptr &byte g_m2_ptr &byte
) )
// isnil returns true if an object is nil (only for C objects). // isnil returns true if an object is nil (only for C objects).
@ -48,7 +48,7 @@ __global (
total_m = i64(0) total_m = i64(0)
nr_mallocs = int(0) nr_mallocs = int(0)
// will be filled in cgen // will be filled in cgen
as_cast_type_indexes []VCastTypeIndexName as_cast_type_indexes []VCastTypeIndexName
) )
fn __as_cast(obj voidptr, obj_type int, expected_type int) voidptr { fn __as_cast(obj voidptr, obj_type int, expected_type int) voidptr {

View File

@ -66,7 +66,9 @@ const (
) )
// g_original_codepage - used to restore the original windows console code page when exiting // g_original_codepage - used to restore the original windows console code page when exiting
__global ( g_original_codepage = u32(0)) __global (
g_original_codepage = u32(0)
)
// utf8 to stdout needs C.SetConsoleOutputCP(C.CP_UTF8) // utf8 to stdout needs C.SetConsoleOutputCP(C.CP_UTF8)
fn C.GetConsoleOutputCP() u32 fn C.GetConsoleOutputCP() u32

View File

@ -1285,7 +1285,9 @@ pub fn (s string) ustring() ustring {
// A hack that allows to create ustring without allocations. // A hack that allows to create ustring without allocations.
// It's called from functions like draw_text() where we know that the string is going to be freed // It's called from functions like draw_text() where we know that the string is going to be freed
// right away. Uses global buffer for storing runes []int array. // right away. Uses global buffer for storing runes []int array.
__global ( g_ustring_runes []int ) __global (
g_ustring_runes []int
)
pub fn (s string) ustring_tmp() ustring { pub fn (s string) ustring_tmp() ustring {
if g_ustring_runes.len == 0 { if g_ustring_runes.len == 0 {

View File

@ -526,7 +526,8 @@ pub mut:
pub struct GlobalDecl { pub struct GlobalDecl {
pub: pub:
pos token.Position pos token.Position
is_block bool // __global() block
pub mut: pub mut:
fields []GlobalField fields []GlobalField
end_comments []Comment end_comments []Comment

View File

@ -1,3 +1,3 @@
vlib/v/checker/tests/globals/assign_no_type.vv:1:21: error: global assign must have a type and value, use `__global ( name = type(value) )` or `__global ( name type )` vlib/v/checker/tests/globals/assign_no_type.vv:1:21: error: global assign must have a type and value, use `name = type(value)` or `name type`
1 | __global ( test = 0 ) 1 | __global ( test = 0 )
| ^ | ^

View File

@ -1,3 +1,3 @@
vlib/v/checker/tests/globals/assign_no_value.vv:1:23: error: global assign must have a type and value, use `__global ( name = type(value) )` or `__global ( name type )` vlib/v/checker/tests/globals/assign_no_value.vv:1:23: error: global assign must have a type and value, use `name = type(value)` or `name type`
1 | __global ( test = int ) 1 | __global ( test = int )
| ^ | ^

View File

@ -0,0 +1,3 @@
vlib/v/checker/tests/globals/unexpected_eof.vv:3:1: error: unexpected eof, expecting ´)´
1 | __global (
2 | x string

View File

@ -0,0 +1,2 @@
__global (
x string

View File

@ -1083,21 +1083,23 @@ pub fn (mut f Fmt) for_stmt(node ast.ForStmt) {
} }
pub fn (mut f Fmt) global_decl(node ast.GlobalDecl) { pub fn (mut f Fmt) global_decl(node ast.GlobalDecl) {
single := node.fields.len == 1 if node.fields.len == 0 && node.pos.line_nr == node.pos.last_line {
if single { f.writeln('__global ()')
f.write('__global ( ') return
} else {
f.writeln('__global (')
f.indent++
} }
f.write('__global ')
mut max := 0 mut max := 0
mut has_assign := false mut has_assign := false
for field in node.fields { if node.is_block {
if field.name.len > max { f.writeln('(')
max = field.name.len f.indent++
} for field in node.fields {
if field.has_expr { if field.name.len > max {
has_assign = true max = field.name.len
}
if field.has_expr {
has_assign = true
}
} }
} }
for field in node.fields { for field in node.fields {
@ -1111,20 +1113,19 @@ pub fn (mut f Fmt) global_decl(node ast.GlobalDecl) {
f.expr(field.expr) f.expr(field.expr)
f.write(')') f.write(')')
} else { } else {
if !single && has_assign { f.write('${f.table.type_to_str_using_aliases(field.typ, f.mod2alias)}')
f.write(' ')
}
f.write('${f.table.type_to_str_using_aliases(field.typ, f.mod2alias)} ')
} }
if !single { if node.is_block {
f.writeln('') f.writeln('')
} }
} }
if !single {
f.indent--
}
f.comments_after_last_field(node.end_comments) f.comments_after_last_field(node.end_comments)
f.writeln(')\n') if node.is_block {
f.indent--
f.writeln(')')
} else {
f.writeln('')
}
} }
pub fn (mut f Fmt) go_expr(node ast.GoExpr) { pub fn (mut f Fmt) go_expr(node ast.GoExpr) {

View File

@ -0,0 +1,10 @@
__global x = bool(true)
__global y f32
__global ()
__global (
spam string
foo = int(5)
)
__global (
a int
)

View File

@ -2818,17 +2818,19 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
return ast.GlobalDecl{} return ast.GlobalDecl{}
} }
start_pos := p.tok.position() start_pos := p.tok.position()
end_pos := p.tok.position()
p.check(.key_global) p.check(.key_global)
if p.tok.kind != .lpar { is_block := p.tok.kind == .lpar
p.error('globals must be grouped, e.g. `__global ( a = int(1) )`') if is_block {
return ast.GlobalDecl{} p.next() // (
} }
p.next() // (
mut fields := []ast.GlobalField{} mut fields := []ast.GlobalField{}
mut comments := []ast.Comment{} mut comments := []ast.Comment{}
for { for {
comments = p.eat_comments({}) comments = p.eat_comments({})
if is_block && p.tok.kind == .eof {
p.error('unexpected eof, expecting ´)´')
return ast.GlobalDecl{}
}
if p.tok.kind == .rpar { if p.tok.kind == .rpar {
break break
} }
@ -2840,13 +2842,13 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
} }
typ := p.parse_type() typ := p.parse_type()
if p.tok.kind == .assign { if p.tok.kind == .assign {
p.error('global assign must have the type around the value, use `__global ( name = type(value) )`') p.error('global assign must have the type around the value, use `name = type(value)`')
return ast.GlobalDecl{} return ast.GlobalDecl{}
} }
mut expr := ast.empty_expr() mut expr := ast.empty_expr()
if has_expr { if has_expr {
if p.tok.kind != .lpar { if p.tok.kind != .lpar {
p.error('global assign must have a type and value, use `__global ( name = type(value) )` or `__global ( name type )`') p.error('global assign must have a type and value, use `name = type(value)` or `name type`')
return ast.GlobalDecl{} return ast.GlobalDecl{}
} }
p.next() // ( p.next() // (
@ -2864,12 +2866,18 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
fields << field fields << field
p.global_scope.register(field) p.global_scope.register(field)
comments = [] comments = []
if !is_block {
break
}
}
if is_block {
p.check(.rpar)
} }
p.check(.rpar)
return ast.GlobalDecl{ return ast.GlobalDecl{
pos: start_pos.extend(end_pos) pos: start_pos.extend(p.prev_tok.position())
fields: fields fields: fields
end_comments: comments end_comments: comments
is_block: is_block
} }
} }