all: fix a big mutability bug and update all mutable vars
parent
fb41c6659a
commit
632e27a4a9
|
@ -14,4 +14,5 @@
|
||||||
- `recover()` from panics
|
- `recover()` from panics
|
||||||
- IO streams
|
- IO streams
|
||||||
- struct and interface embedding
|
- struct and interface embedding
|
||||||
|
- vfmt: fix common errors automatically to save time (make vars mutable and vice versa, add missing imports etc)
|
||||||
|
|
||||||
|
|
|
@ -123,7 +123,7 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
|
||||||
|
|
||||||
fn break_if_debugger_attached() {
|
fn break_if_debugger_attached() {
|
||||||
unsafe {
|
unsafe {
|
||||||
ptr := &voidptr(0)
|
mut ptr := &voidptr(0)
|
||||||
*ptr = 0
|
*ptr = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -250,7 +250,7 @@ fn (mut n mapnode) remove_from_non_leaf(idx int) {
|
||||||
predecessor := current.keys[current.len - 1]
|
predecessor := current.keys[current.len - 1]
|
||||||
n.keys[idx] = predecessor
|
n.keys[idx] = predecessor
|
||||||
n.values[idx] = current.values[current.len - 1]
|
n.values[idx] = current.values[current.len - 1]
|
||||||
node := unsafe {&mapnode(n.children[idx])}
|
mut node := unsafe {&mapnode(n.children[idx])}
|
||||||
node.remove_key(predecessor)
|
node.remove_key(predecessor)
|
||||||
} else if unsafe {&mapnode(n.children[idx + 1])}.len >= degree {
|
} else if unsafe {&mapnode(n.children[idx + 1])}.len >= degree {
|
||||||
mut current := unsafe {&mapnode(n.children[idx + 1])}
|
mut current := unsafe {&mapnode(n.children[idx + 1])}
|
||||||
|
@ -260,11 +260,11 @@ fn (mut n mapnode) remove_from_non_leaf(idx int) {
|
||||||
successor := current.keys[0]
|
successor := current.keys[0]
|
||||||
n.keys[idx] = successor
|
n.keys[idx] = successor
|
||||||
n.values[idx] = current.values[0]
|
n.values[idx] = current.values[0]
|
||||||
node := unsafe {&mapnode(n.children[idx + 1])}
|
mut node := unsafe {&mapnode(n.children[idx + 1])}
|
||||||
node.remove_key(successor)
|
node.remove_key(successor)
|
||||||
} else {
|
} else {
|
||||||
n.merge(idx)
|
n.merge(idx)
|
||||||
node := unsafe {&mapnode(n.children[idx])}
|
mut node := unsafe {&mapnode(n.children[idx])}
|
||||||
node.remove_key(k)
|
node.remove_key(k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -281,7 +281,7 @@ pub fn fileno(cfile voidptr) int {
|
||||||
$if windows {
|
$if windows {
|
||||||
return C._fileno(cfile)
|
return C._fileno(cfile)
|
||||||
} $else {
|
} $else {
|
||||||
cfile_casted := &C.FILE(0) // FILE* cfile_casted = 0;
|
mut cfile_casted := &C.FILE(0) // FILE* cfile_casted = 0;
|
||||||
cfile_casted = cfile
|
cfile_casted = cfile
|
||||||
// Required on FreeBSD/OpenBSD/NetBSD as stdio.h defines fileno(..) with a macro
|
// Required on FreeBSD/OpenBSD/NetBSD as stdio.h defines fileno(..) with a macro
|
||||||
// that performs a field access on its argument without casting from void*.
|
// that performs a field access on its argument without casting from void*.
|
||||||
|
|
|
@ -33,10 +33,11 @@ pub fn new_builder(pref &pref.Preferences) Builder {
|
||||||
compiled_dir := if os.is_dir(rdir) { rdir } else { os.dir(rdir) }
|
compiled_dir := if os.is_dir(rdir) { rdir } else { os.dir(rdir) }
|
||||||
table := table.new_table()
|
table := table.new_table()
|
||||||
if pref.use_color == .always {
|
if pref.use_color == .always {
|
||||||
util.emanager.set_support_color(true)
|
// TODO
|
||||||
|
//util.emanager.set_support_color(true)
|
||||||
}
|
}
|
||||||
if pref.use_color == .never {
|
if pref.use_color == .never {
|
||||||
util.emanager.set_support_color(false)
|
//util.emanager.set_support_color(false)
|
||||||
}
|
}
|
||||||
msvc := find_msvc() or {
|
msvc := find_msvc() or {
|
||||||
if pref.ccompiler == 'msvc' {
|
if pref.ccompiler == 'msvc' {
|
||||||
|
@ -210,9 +211,9 @@ fn module_path(mod string) string {
|
||||||
return mod.replace('.', os.path_separator)
|
return mod.replace('.', os.path_separator)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (b Builder) find_module_path(mod, fpath string) ?string {
|
pub fn (b &Builder) find_module_path(mod, fpath string) ?string {
|
||||||
// support @VROOT/v.mod relative paths:
|
// support @VROOT/v.mod relative paths:
|
||||||
mcache := vmod.get_cache()
|
mut mcache := vmod.get_cache()
|
||||||
vmod_file_location := mcache.get_by_file(fpath)
|
vmod_file_location := mcache.get_by_file(fpath)
|
||||||
mod_path := module_path(mod)
|
mod_path := module_path(mod)
|
||||||
mut module_lookup_paths := []string{}
|
mut module_lookup_paths := []string{}
|
||||||
|
|
|
@ -671,7 +671,7 @@ fn (mut c Builder) cc_windows_cross() {
|
||||||
println(c.pref.out_name + ' has been successfully compiled')
|
println(c.pref.out_name + ' has been successfully compiled')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (c &Builder) build_thirdparty_obj_files() {
|
fn (mut c Builder) build_thirdparty_obj_files() {
|
||||||
for flag in c.get_os_cflags() {
|
for flag in c.get_os_cflags() {
|
||||||
if flag.value.ends_with('.o') {
|
if flag.value.ends_with('.o') {
|
||||||
rest_of_module_flags := c.get_rest_of_module_cflags(flag)
|
rest_of_module_flags := c.get_rest_of_module_cflags(flag)
|
||||||
|
|
|
@ -7,7 +7,7 @@ import v.table
|
||||||
import v.token
|
import v.token
|
||||||
import v.ast
|
import v.ast
|
||||||
|
|
||||||
pub fn (c &Checker) check_basic(got, expected table.Type) bool {
|
pub fn (mut c Checker) check_basic(got, expected table.Type) bool {
|
||||||
if got == expected {
|
if got == expected {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -112,7 +112,7 @@ pub fn (c &Checker) check_basic(got, expected table.Type) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c &Checker) check_matching_function_symbols(got_type_sym &table.TypeSymbol, exp_type_sym &table.TypeSymbol) bool {
|
pub fn (mut c Checker) check_matching_function_symbols(got_type_sym &table.TypeSymbol, exp_type_sym &table.TypeSymbol) bool {
|
||||||
got_info := got_type_sym.info as table.FnType
|
got_info := got_type_sym.info as table.FnType
|
||||||
exp_info := exp_type_sym.info as table.FnType
|
exp_info := exp_type_sym.info as table.FnType
|
||||||
got_fn := got_info.func
|
got_fn := got_info.func
|
||||||
|
@ -143,7 +143,7 @@ pub fn (c &Checker) check_matching_function_symbols(got_type_sym &table.TypeSymb
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
fn (c &Checker) check_shift(left_type, right_type table.Type, left_pos, right_pos token.Position) table.Type {
|
fn (mut c Checker) check_shift(left_type, right_type table.Type, left_pos, right_pos token.Position) table.Type {
|
||||||
if !left_type.is_int() {
|
if !left_type.is_int() {
|
||||||
c.error('cannot shift type ${c.table.get_type_symbol(right_type).name} into non-integer type ${c.table.get_type_symbol(left_type).name}',
|
c.error('cannot shift type ${c.table.get_type_symbol(right_type).name} into non-integer type ${c.table.get_type_symbol(left_type).name}',
|
||||||
left_pos)
|
left_pos)
|
||||||
|
@ -220,7 +220,7 @@ fn (c &Checker) promote_num(left_type, right_type table.Type) table.Type {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: promote(), check_types(), symmetric_check() and check() overlap - should be rearranged
|
// TODO: promote(), check_types(), symmetric_check() and check() overlap - should be rearranged
|
||||||
pub fn (c &Checker) check_types(got, expected table.Type) bool {
|
pub fn (mut c Checker) check_types(got, expected table.Type) bool {
|
||||||
if got == expected {
|
if got == expected {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -258,7 +258,7 @@ pub fn (c &Checker) check_types(got, expected table.Type) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c &Checker) symmetric_check(left, right table.Type) bool {
|
pub fn (mut c Checker) symmetric_check(left, right table.Type) bool {
|
||||||
// allow direct int-literal assignment for pointers for now
|
// allow direct int-literal assignment for pointers for now
|
||||||
// maybe in the future optionals should be used for that
|
// maybe in the future optionals should be used for that
|
||||||
if right.is_ptr() || right.is_pointer() {
|
if right.is_ptr() || right.is_pointer() {
|
||||||
|
@ -303,7 +303,7 @@ pub fn (c &Checker) get_default_fmt(ftyp, typ table.Type) byte {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c &Checker) string_inter_lit(mut node ast.StringInterLiteral) table.Type {
|
pub fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) table.Type {
|
||||||
for i, expr in node.exprs {
|
for i, expr in node.exprs {
|
||||||
ftyp := c.expr(expr)
|
ftyp := c.expr(expr)
|
||||||
node.expr_types << ftyp
|
node.expr_types << ftyp
|
||||||
|
|
|
@ -125,7 +125,7 @@ pub fn (mut c Checker) check_files(ast_files []ast.File) {
|
||||||
}
|
}
|
||||||
if has_main_mod_file && !has_main_fn && files_from_main_module.len > 0 {
|
if has_main_mod_file && !has_main_fn && files_from_main_module.len > 0 {
|
||||||
if c.pref.is_script && !c.pref.is_test {
|
if c.pref.is_script && !c.pref.is_test {
|
||||||
first_main_file := files_from_main_module[0]
|
mut first_main_file := files_from_main_module[0]
|
||||||
first_main_file.stmts << ast.FnDecl{
|
first_main_file.stmts << ast.FnDecl{
|
||||||
name: 'main.main'
|
name: 'main.main'
|
||||||
mod: 'main'
|
mod: 'main'
|
||||||
|
@ -673,14 +673,15 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
|
||||||
mut to_lock := '' // name of variable that needs lock
|
mut to_lock := '' // name of variable that needs lock
|
||||||
mut pos := token.Position{} // and its position
|
mut pos := token.Position{} // and its position
|
||||||
mut explicit_lock_needed := false
|
mut explicit_lock_needed := false
|
||||||
match expr {
|
match mut expr {
|
||||||
ast.CastExpr {
|
ast.CastExpr {
|
||||||
// TODO
|
// TODO
|
||||||
return '', pos
|
return '', pos
|
||||||
}
|
}
|
||||||
ast.Ident {
|
ast.Ident {
|
||||||
if expr.obj is ast.Var as v {
|
if expr.obj is ast.Var {
|
||||||
if !v.is_mut && !v.typ.is_ptr() {
|
mut v := expr.obj as ast.Var
|
||||||
|
if !v.is_mut {
|
||||||
c.error('`$expr.name` is immutable, declare it with `mut` to make it mutable',
|
c.error('`$expr.name` is immutable, declare it with `mut` to make it mutable',
|
||||||
expr.pos)
|
expr.pos)
|
||||||
}
|
}
|
||||||
|
@ -1368,7 +1369,7 @@ pub fn (mut c Checker) check_or_expr(mut or_expr ast.OrExpr, ret_type table.Type
|
||||||
// allow `f() or {}`
|
// allow `f() or {}`
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
last_stmt := or_expr.stmts[stmts_len - 1]
|
mut last_stmt := or_expr.stmts[stmts_len - 1]
|
||||||
if ret_type != table.void_type {
|
if ret_type != table.void_type {
|
||||||
if !(last_stmt is ast.Return || last_stmt is ast.BranchStmt || last_stmt is ast.ExprStmt) {
|
if !(last_stmt is ast.Return || last_stmt is ast.BranchStmt || last_stmt is ast.ExprStmt) {
|
||||||
expected_type_name := c.table.get_type_symbol(ret_type).name
|
expected_type_name := c.table.get_type_symbol(ret_type).name
|
||||||
|
@ -1376,7 +1377,7 @@ pub fn (mut c Checker) check_or_expr(mut or_expr ast.OrExpr, ret_type table.Type
|
||||||
or_expr.pos)
|
or_expr.pos)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
match last_stmt {
|
match mut last_stmt {
|
||||||
ast.ExprStmt {
|
ast.ExprStmt {
|
||||||
last_stmt.typ = c.expr(last_stmt.expr)
|
last_stmt.typ = c.expr(last_stmt.expr)
|
||||||
type_fits := c.check_types(last_stmt.typ, ret_type)
|
type_fits := c.check_types(last_stmt.typ, ret_type)
|
||||||
|
@ -1618,7 +1619,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
|
||||||
// left_type = c.expr(left)
|
// left_type = c.expr(left)
|
||||||
}
|
}
|
||||||
assign_stmt.left_types << left_type
|
assign_stmt.left_types << left_type
|
||||||
match left {
|
match mut left {
|
||||||
ast.Ident {
|
ast.Ident {
|
||||||
if left.kind == .blank_ident {
|
if left.kind == .blank_ident {
|
||||||
left_type = right_type
|
left_type = right_type
|
||||||
|
@ -1641,11 +1642,23 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
|
||||||
ident_var_info.typ = left_type
|
ident_var_info.typ = left_type
|
||||||
left.info = ident_var_info
|
left.info = ident_var_info
|
||||||
if left_type != 0 {
|
if left_type != 0 {
|
||||||
|
match mut left.obj as v {
|
||||||
|
ast.Var {
|
||||||
|
v.typ = left_type
|
||||||
|
}
|
||||||
|
ast.GlobalDecl {
|
||||||
|
v.typ = left_type
|
||||||
|
}
|
||||||
|
else {}
|
||||||
|
|
||||||
|
}
|
||||||
|
/*
|
||||||
if left.obj is ast.Var as v {
|
if left.obj is ast.Var as v {
|
||||||
v.typ = left_type
|
v.typ = left_type
|
||||||
} else if left.obj is ast.GlobalDecl as v {
|
} else if left.obj is ast.GlobalDecl as v {
|
||||||
v.typ = left_type
|
v.typ = left_type
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2425,7 +2438,7 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type {
|
||||||
// first use
|
// first use
|
||||||
start_scope := c.file.scope.innermost(ident.pos.pos)
|
start_scope := c.file.scope.innermost(ident.pos.pos)
|
||||||
if obj1 := start_scope.find(ident.name) {
|
if obj1 := start_scope.find(ident.name) {
|
||||||
match obj1 as obj {
|
match mut obj1 as obj {
|
||||||
ast.GlobalDecl {
|
ast.GlobalDecl {
|
||||||
ident.kind = .global
|
ident.kind = .global
|
||||||
ident.info = ast.IdentVar{
|
ident.info = ast.IdentVar{
|
||||||
|
@ -2482,7 +2495,7 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type {
|
||||||
name = '${ident.mod}.$ident.name'
|
name = '${ident.mod}.$ident.name'
|
||||||
}
|
}
|
||||||
if obj1 := c.file.global_scope.find(name) {
|
if obj1 := c.file.global_scope.find(name) {
|
||||||
match obj1 as obj {
|
match mut obj1 as obj {
|
||||||
ast.ConstField {
|
ast.ConstField {
|
||||||
mut typ := obj.typ
|
mut typ := obj.typ
|
||||||
if typ == 0 {
|
if typ == 0 {
|
||||||
|
@ -2600,7 +2613,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) table.Type {
|
||||||
c.stmts(branch.stmts)
|
c.stmts(branch.stmts)
|
||||||
// If the last statement is an expression, return its type
|
// If the last statement is an expression, return its type
|
||||||
if branch.stmts.len > 0 {
|
if branch.stmts.len > 0 {
|
||||||
match branch.stmts[branch.stmts.len - 1] as stmt {
|
match mut branch.stmts[branch.stmts.len - 1] as stmt {
|
||||||
ast.ExprStmt {
|
ast.ExprStmt {
|
||||||
ret_type = c.expr(stmt.expr)
|
ret_type = c.expr(stmt.expr)
|
||||||
stmt.typ = ret_type
|
stmt.typ = ret_type
|
||||||
|
@ -2874,7 +2887,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
|
||||||
c.stmts(branch.stmts)
|
c.stmts(branch.stmts)
|
||||||
if expr_required {
|
if expr_required {
|
||||||
if branch.stmts.len > 0 && branch.stmts[branch.stmts.len - 1] is ast.ExprStmt {
|
if branch.stmts.len > 0 && branch.stmts[branch.stmts.len - 1] is ast.ExprStmt {
|
||||||
last_expr := branch.stmts[branch.stmts.len - 1] as ast.ExprStmt
|
mut last_expr := branch.stmts[branch.stmts.len - 1] as ast.ExprStmt
|
||||||
c.expected_type = former_expected_type
|
c.expected_type = former_expected_type
|
||||||
last_expr.typ = c.expr(last_expr.expr)
|
last_expr.typ = c.expr(last_expr.expr)
|
||||||
if last_expr.typ != node.typ {
|
if last_expr.typ != node.typ {
|
||||||
|
@ -3247,7 +3260,7 @@ fn (mut c Checker) sql_stmt(mut node ast.SqlStmt) table.Type {
|
||||||
return table.void_type
|
return table.void_type
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (c &Checker) fetch_and_verify_orm_fields(info table.Struct, pos token.Position, table_name string) []table.Field {
|
fn (mut c Checker) fetch_and_verify_orm_fields(info table.Struct, pos token.Position, table_name string) []table.Field {
|
||||||
fields := info.fields.filter(it.typ in
|
fields := info.fields.filter(it.typ in
|
||||||
[table.string_type, table.int_type, table.bool_type] &&
|
[table.string_type, table.int_type, table.bool_type] &&
|
||||||
'skip' !in it.attrs)
|
'skip' !in it.attrs)
|
||||||
|
@ -3277,7 +3290,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
|
||||||
c.check_valid_snake_case(node.name, 'function name', node.pos)
|
c.check_valid_snake_case(node.name, 'function name', node.pos)
|
||||||
}
|
}
|
||||||
if node.is_method {
|
if node.is_method {
|
||||||
sym := c.table.get_type_symbol(node.receiver.typ)
|
mut sym := c.table.get_type_symbol(node.receiver.typ)
|
||||||
if sym.kind == .interface_ {
|
if sym.kind == .interface_ {
|
||||||
c.error('interfaces cannot be used as method receiver', node.receiver_pos)
|
c.error('interfaces cannot be used as method receiver', node.receiver_pos)
|
||||||
}
|
}
|
||||||
|
|
|
@ -340,7 +340,7 @@ pub fn (mut g Gen) write_typeof_functions() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// V type to C type
|
// V type to C type
|
||||||
fn (g &Gen) typ(t table.Type) string {
|
fn (mut g Gen) typ(t table.Type) string {
|
||||||
styp := g.base_type(t)
|
styp := g.base_type(t)
|
||||||
if t.has_flag(.optional) {
|
if t.has_flag(.optional) {
|
||||||
// Register an optional if it's not registered yet
|
// Register an optional if it's not registered yet
|
||||||
|
@ -354,7 +354,7 @@ fn (g &Gen) typ(t table.Type) string {
|
||||||
return styp
|
return styp
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g &Gen) base_type(t table.Type) string {
|
fn (mut g Gen) base_type(t table.Type) string {
|
||||||
share := t.share()
|
share := t.share()
|
||||||
mut styp := if share == .atomic_t { t.atomic_typename() } else { g.cc_type(t) }
|
mut styp := if share == .atomic_t { t.atomic_typename() } else { g.cc_type(t) }
|
||||||
if t.has_flag(.shared_f) {
|
if t.has_flag(.shared_f) {
|
||||||
|
@ -371,7 +371,7 @@ fn (g &Gen) base_type(t table.Type) string {
|
||||||
// but I(emily) would rather have this generation
|
// but I(emily) would rather have this generation
|
||||||
// all unified in one place so that it doesnt break
|
// all unified in one place so that it doesnt break
|
||||||
// if one location changes
|
// if one location changes
|
||||||
fn (g &Gen) optional_type_name(t table.Type) (string, string) {
|
fn (mut g Gen) optional_type_name(t table.Type) (string, string) {
|
||||||
base := g.base_type(t)
|
base := g.base_type(t)
|
||||||
mut styp := 'Option_$base'
|
mut styp := 'Option_$base'
|
||||||
if t.is_ptr() {
|
if t.is_ptr() {
|
||||||
|
@ -760,14 +760,12 @@ fn (mut g Gen) stmt(node ast.Stmt) {
|
||||||
g.has_main = true
|
g.has_main = true
|
||||||
}
|
}
|
||||||
if node.name == 'backtrace' ||
|
if node.name == 'backtrace' ||
|
||||||
node.name == 'backtrace_symbols' ||
|
node.name == 'backtrace_symbols' || node.name == 'backtrace_symbols_fd' {
|
||||||
node.name == 'backtrace_symbols_fd' {
|
|
||||||
g.write('\n#ifndef __cplusplus\n')
|
g.write('\n#ifndef __cplusplus\n')
|
||||||
}
|
}
|
||||||
g.gen_fn_decl(node, skip)
|
g.gen_fn_decl(node, skip)
|
||||||
if node.name == 'backtrace' ||
|
if node.name == 'backtrace' ||
|
||||||
node.name == 'backtrace_symbols' ||
|
node.name == 'backtrace_symbols' || node.name == 'backtrace_symbols_fd' {
|
||||||
node.name == 'backtrace_symbols_fd' {
|
|
||||||
g.write('\n#endif\n')
|
g.write('\n#endif\n')
|
||||||
}
|
}
|
||||||
g.fn_decl = keep_fn_decl
|
g.fn_decl = keep_fn_decl
|
||||||
|
@ -1043,8 +1041,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type, expected_type table.Type)
|
||||||
got_is_ptr := got_type.is_ptr()
|
got_is_ptr := got_type.is_ptr()
|
||||||
expected_is_ptr := expected_type.is_ptr()
|
expected_is_ptr := expected_type.is_ptr()
|
||||||
neither_void := table.voidptr_type !in [got_type, expected_type]
|
neither_void := table.voidptr_type !in [got_type, expected_type]
|
||||||
if got_is_ptr && !expected_is_ptr && neither_void &&
|
if got_is_ptr && !expected_is_ptr && neither_void && expected_sym.kind !in [.interface_, .placeholder] {
|
||||||
expected_sym.kind !in [.interface_, .placeholder] {
|
|
||||||
got_deref_type := got_type.deref()
|
got_deref_type := got_type.deref()
|
||||||
deref_sym := g.table.get_type_symbol(got_deref_type)
|
deref_sym := g.table.get_type_symbol(got_deref_type)
|
||||||
deref_will_match := expected_type in [got_type, got_deref_type, deref_sym.parent_idx]
|
deref_will_match := expected_type in [got_type, got_deref_type, deref_sym.parent_idx]
|
||||||
|
@ -1161,7 +1158,7 @@ fn (mut g Gen) gen_assert_single_expr(e ast.Expr, t table.Type) {
|
||||||
g.write(' /* typeof: ' + typeof(e) + ' type: ' + t.str() + ' */ ')
|
g.write(' /* typeof: ' + typeof(e) + ' type: ' + t.str() + ' */ ')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g &Gen) write_fn_ptr_decl(func &table.FnType, ptr_name string) {
|
fn (mut g Gen) write_fn_ptr_decl(func &table.FnType, ptr_name string) {
|
||||||
ret_styp := g.typ(func.func.return_type)
|
ret_styp := g.typ(func.func.return_type)
|
||||||
g.write('$ret_styp (*$ptr_name) (')
|
g.write('$ret_styp (*$ptr_name) (')
|
||||||
arg_len := func.func.args.len
|
arg_len := func.func.args.len
|
||||||
|
@ -1615,7 +1612,7 @@ fn (mut g Gen) autofree_scope_vars(pos int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g &Gen) autofree_variable(v ast.Var) {
|
fn (mut g Gen) autofree_variable(v ast.Var) {
|
||||||
sym := g.table.get_type_symbol(v.typ)
|
sym := g.table.get_type_symbol(v.typ)
|
||||||
// if v.name.contains('output2') {
|
// if v.name.contains('output2') {
|
||||||
// eprintln(' > var name: ${v.name:-20s} | is_arg: ${v.is_arg.str():6} | var type: ${int(v.typ):8} | type_name: ${sym.name:-33s}')
|
// eprintln(' > var name: ${v.name:-20s} | is_arg: ${v.is_arg.str():6} | var type: ${int(v.typ):8} | type_name: ${sym.name:-33s}')
|
||||||
|
@ -1653,7 +1650,7 @@ fn (g &Gen) autofree_variable(v ast.Var) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g &Gen) autofree_var_call(free_fn_name string, v ast.Var) {
|
fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) {
|
||||||
if v.is_arg {
|
if v.is_arg {
|
||||||
// fn args should not be autofreed
|
// fn args should not be autofreed
|
||||||
return
|
return
|
||||||
|
@ -2617,8 +2614,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
|
||||||
}
|
}
|
||||||
// `x[0] *= y`
|
// `x[0] *= y`
|
||||||
if g.assign_op != .assign &&
|
if g.assign_op != .assign &&
|
||||||
g.assign_op in token.assign_tokens &&
|
g.assign_op in token.assign_tokens && info.elem_type != table.string_type {
|
||||||
info.elem_type != table.string_type {
|
|
||||||
// TODO move this
|
// TODO move this
|
||||||
g.write('*($elem_type_str*)array_get(')
|
g.write('*($elem_type_str*)array_get(')
|
||||||
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
|
if left_is_ptr && !node.left_type.has_flag(.shared_f) {
|
||||||
|
@ -2866,8 +2862,7 @@ fn (mut g Gen) return_statement(node ast.Return) {
|
||||||
// normal return
|
// normal return
|
||||||
return_sym := g.table.get_type_symbol(node.types[0])
|
return_sym := g.table.get_type_symbol(node.types[0])
|
||||||
// `return opt_ok(expr)` for functions that expect an optional
|
// `return opt_ok(expr)` for functions that expect an optional
|
||||||
if fn_return_is_optional && !node.types[0].has_flag(.optional) &&
|
if fn_return_is_optional && !node.types[0].has_flag(.optional) && return_sym.name != 'Option' {
|
||||||
return_sym.name != 'Option' {
|
|
||||||
styp := g.base_type(g.fn_decl.return_type)
|
styp := g.base_type(g.fn_decl.return_type)
|
||||||
opt_type := g.typ(g.fn_decl.return_type)
|
opt_type := g.typ(g.fn_decl.return_type)
|
||||||
// Create a tmp for this option
|
// Create a tmp for this option
|
||||||
|
@ -3802,9 +3797,8 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table.
|
||||||
g.writeln('\tstring err = ${cvar_name}.v_error;')
|
g.writeln('\tstring err = ${cvar_name}.v_error;')
|
||||||
g.writeln('\tint errcode = ${cvar_name}.ecode;')
|
g.writeln('\tint errcode = ${cvar_name}.ecode;')
|
||||||
stmts := or_block.stmts
|
stmts := or_block.stmts
|
||||||
if stmts.len > 0 &&
|
if stmts.len > 0 && stmts[or_block.stmts.len - 1] is ast.ExprStmt && (stmts[stmts.len -
|
||||||
stmts[or_block.stmts.len - 1] is ast.ExprStmt &&
|
1] as ast.ExprStmt).typ != table.void_type {
|
||||||
(stmts[stmts.len - 1] as ast.ExprStmt).typ != table.void_type {
|
|
||||||
g.indent++
|
g.indent++
|
||||||
for i, stmt in stmts {
|
for i, stmt in stmts {
|
||||||
if i == stmts.len - 1 {
|
if i == stmts.len - 1 {
|
||||||
|
@ -4010,8 +4004,8 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string {
|
||||||
return 'TARGET_ORDER_IS_BIG'
|
return 'TARGET_ORDER_IS_BIG'
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if is_comptime_optional || (g.pref.compile_defines_all.len > 0 &&
|
if is_comptime_optional ||
|
||||||
name in g.pref.compile_defines_all) {
|
(g.pref.compile_defines_all.len > 0 && name in g.pref.compile_defines_all) {
|
||||||
return 'CUSTOM_DEFINE_$name'
|
return 'CUSTOM_DEFINE_$name'
|
||||||
}
|
}
|
||||||
verror('bad os ifdef name "$name"')
|
verror('bad os ifdef name "$name"')
|
||||||
|
@ -4030,7 +4024,7 @@ fn c_name(name_ string) string {
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g Gen) type_default(typ table.Type) string {
|
fn (mut g Gen) type_default(typ table.Type) string {
|
||||||
sym := g.table.get_type_symbol(typ)
|
sym := g.table.get_type_symbol(typ)
|
||||||
if sym.kind == .array {
|
if sym.kind == .array {
|
||||||
elem_sym := g.typ(sym.array_info().elem_type)
|
elem_sym := g.typ(sym.array_info().elem_type)
|
||||||
|
@ -4706,7 +4700,7 @@ fn (g Gen) type_to_fmt(typ table.Type) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates interface table and interface indexes
|
// Generates interface table and interface indexes
|
||||||
fn (g &Gen) interface_table() string {
|
fn (mut g Gen) interface_table() string {
|
||||||
mut sb := strings.new_builder(100)
|
mut sb := strings.new_builder(100)
|
||||||
for ityp in g.table.types {
|
for ityp in g.table.types {
|
||||||
if ityp.kind != .interface_ {
|
if ityp.kind != .interface_ {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import v.ast
|
||||||
import v.table
|
import v.table
|
||||||
import v.util
|
import v.util
|
||||||
|
|
||||||
fn (g &Gen) comptime_call(node ast.ComptimeCall) {
|
fn (mut g Gen) comptime_call(node ast.ComptimeCall) {
|
||||||
if node.is_vweb {
|
if node.is_vweb {
|
||||||
for stmt in node.vweb_tmpl.stmts {
|
for stmt in node.vweb_tmpl.stmts {
|
||||||
if stmt is ast.FnDecl {
|
if stmt is ast.FnDecl {
|
||||||
|
|
|
@ -3,7 +3,7 @@ module gen
|
||||||
import v.pref
|
import v.pref
|
||||||
import v.util
|
import v.util
|
||||||
|
|
||||||
fn (g &Gen) generate_hotcode_reloading_declarations() {
|
fn (mut g Gen) generate_hotcode_reloading_declarations() {
|
||||||
if g.pref.os == .windows {
|
if g.pref.os == .windows {
|
||||||
if g.pref.is_livemain {
|
if g.pref.is_livemain {
|
||||||
g.hotcode_definitions.writeln('HANDLE live_fn_mutex = 0;')
|
g.hotcode_definitions.writeln('HANDLE live_fn_mutex = 0;')
|
||||||
|
@ -29,7 +29,7 @@ void pthread_mutex_unlock(HANDLE *m) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (g &Gen) generate_hotcode_reloader_code() {
|
fn (mut g Gen) generate_hotcode_reloader_code() {
|
||||||
if g.pref.is_liveshared {
|
if g.pref.is_liveshared {
|
||||||
g.hotcode_definitions.writeln('')
|
g.hotcode_definitions.writeln('')
|
||||||
return
|
return
|
||||||
|
@ -66,7 +66,7 @@ void v_bind_live_symbols(void* live_lib){
|
||||||
'
|
'
|
||||||
)
|
)
|
||||||
|
|
||||||
fn (g &Gen) generate_hotcode_reloading_main_caller() {
|
fn (mut g Gen) generate_hotcode_reloading_main_caller() {
|
||||||
if !g.pref.is_livemain {
|
if !g.pref.is_livemain {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,7 +106,7 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i, lx in left {
|
for i, lx in left {
|
||||||
match lx {
|
match mut lx {
|
||||||
ast.Ident {
|
ast.Ident {
|
||||||
if op == .decl_assign {
|
if op == .decl_assign {
|
||||||
if p.scope.known_var(lx.name) {
|
if p.scope.known_var(lx.name) {
|
||||||
|
|
|
@ -18,7 +18,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
fn (mut p Parser) resolve_vroot(flag string) string {
|
fn (mut p Parser) resolve_vroot(flag string) string {
|
||||||
mcache := vmod.get_cache()
|
mut mcache := vmod.get_cache()
|
||||||
vmod_file_location := mcache.get_by_folder(p.file_name_dir)
|
vmod_file_location := mcache.get_by_folder(p.file_name_dir)
|
||||||
if vmod_file_location.vmod_file.len == 0 {
|
if vmod_file_location.vmod_file.len == 0 {
|
||||||
// There was no actual v.mod file found.
|
// There was no actual v.mod file found.
|
||||||
|
@ -133,7 +133,7 @@ fn (mut p Parser) vweb() ast.ComptimeCall {
|
||||||
for stmt in file.stmts {
|
for stmt in file.stmts {
|
||||||
if stmt is ast.FnDecl {
|
if stmt is ast.FnDecl {
|
||||||
if stmt.name == 'main.vweb_tmpl_$p.cur_fn_name' {
|
if stmt.name == 'main.vweb_tmpl_$p.cur_fn_name' {
|
||||||
tmpl_scope := file.scope.innermost(stmt.body_pos.pos)
|
mut tmpl_scope := file.scope.innermost(stmt.body_pos.pos)
|
||||||
for _, obj in p.scope.objects {
|
for _, obj in p.scope.objects {
|
||||||
if obj is ast.Var {
|
if obj is ast.Var {
|
||||||
mut v := obj
|
mut v := obj
|
||||||
|
|
|
@ -183,7 +183,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||||
// default_expr = p.tok.lit
|
// default_expr = p.tok.lit
|
||||||
// p.expr(0)
|
// p.expr(0)
|
||||||
default_expr = p.expr(0)
|
default_expr = p.expr(0)
|
||||||
match default_expr {
|
match mut default_expr {
|
||||||
ast.EnumVal { default_expr.typ = typ }
|
ast.EnumVal { default_expr.typ = typ }
|
||||||
// TODO: implement all types??
|
// TODO: implement all types??
|
||||||
else {}
|
else {}
|
||||||
|
@ -358,7 +358,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
|
||||||
name_pos)
|
name_pos)
|
||||||
}
|
}
|
||||||
typ := table.new_type(reg_idx)
|
typ := table.new_type(reg_idx)
|
||||||
ts := p.table.get_type_symbol(typ)
|
mut ts := p.table.get_type_symbol(typ)
|
||||||
// if methods were declared before, it's an error, ignore them
|
// if methods were declared before, it's an error, ignore them
|
||||||
ts.methods.clear()
|
ts.methods.clear()
|
||||||
// Parse methods
|
// Parse methods
|
||||||
|
|
|
@ -919,7 +919,7 @@ fn (mut s Scanner) text_scan() token.Token {
|
||||||
}
|
}
|
||||||
if name == 'VMOD_FILE' {
|
if name == 'VMOD_FILE' {
|
||||||
if s.vmod_file_content.len == 0 {
|
if s.vmod_file_content.len == 0 {
|
||||||
mcache := vmod.get_cache()
|
mut mcache := vmod.get_cache()
|
||||||
vmod_file_location := mcache.get_by_file(s.file_path)
|
vmod_file_location := mcache.get_by_file(s.file_path)
|
||||||
if vmod_file_location.vmod_file.len == 0 {
|
if vmod_file_location.vmod_file.len == 0 {
|
||||||
s.error('@VMOD_FILE can be used only in projects, that have v.mod file')
|
s.error('@VMOD_FILE can be used only in projects, that have v.mod file')
|
||||||
|
|
|
@ -500,7 +500,7 @@ pub fn (table &Table) qualify_module(mod, file_path string) string {
|
||||||
return mod
|
return mod
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (table &Table) register_fn_gen_type(fn_name string, typ Type) {
|
pub fn (mut table Table) register_fn_gen_type(fn_name string, typ Type) {
|
||||||
mut a := table.fn_gen_types[fn_name]
|
mut a := table.fn_gen_types[fn_name]
|
||||||
if typ in a {
|
if typ in a {
|
||||||
return
|
return
|
||||||
|
|
|
@ -39,7 +39,7 @@ pub fn new_error_manager() &EManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (e &EManager) set_support_color(b bool) {
|
pub fn (mut e EManager) set_support_color(b bool) {
|
||||||
e.support_color = b
|
e.support_color = b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue