diff --git a/Makefile b/Makefile index f5c3fa363a..b356619019 100644 --- a/Makefile +++ b/Makefile @@ -70,19 +70,19 @@ fresh_vc: rm -rf $(TMPVC) $(GITFASTCLONE) $(VCREPO) $(TMPVC) -#latest_tcc: $(TMPTCC)/.git/config -#ifndef ANDROID - #cd $(TMPTCC) && $(GITCLEANPULL) -#endif +latest_tcc: $(TMPTCC)/.git/config +ifndef ANDROID + cd $(TMPTCC) && $(GITCLEANPULL) +endif -#fresh_tcc: -#ifndef ANDROID - #rm -rf $(TMPTCC) - #$(GITFASTCLONE) $(TCCREPO) $(TMPTCC) -#endif +fresh_tcc: +ifndef ANDROID + rm -rf $(TMPTCC) + $(GITFASTCLONE) $(TCCREPO) $(TMPTCC) +endif -#$(TMPTCC)/.git/config: - #$(MAKE) fresh_tcc +$(TMPTCC)/.git/config: + $(MAKE) fresh_tcc $(TMPVC)/.git/config: $(MAKE) fresh_vc diff --git a/vlib/builtin/cfns.v b/vlib/builtin/cfns.v index 9a4a66a9dd..ceccfa720b 100644 --- a/vlib/builtin/cfns.v +++ b/vlib/builtin/cfns.v @@ -400,3 +400,4 @@ fn C.WaitForSingleObject(voidptr, int) int fn C.ReleaseMutex(voidptr) bool + diff --git a/vlib/compiler/cgen.v b/vlib/compiler/cgen.v index e2ad829959..e0d53579f2 100644 --- a/vlib/compiler/cgen.v +++ b/vlib/compiler/cgen.v @@ -476,16 +476,16 @@ fn (v &V) interface_table() string { } } methods += '\n},\n\n' - // Speaker_Cat_index = 0 - concrete_type_name := gen_type.replace('*', '_ptr') + // Speaker_Cat_index = 0 + concrete_type_name := gen_type.replace('*', '_ptr') sb.writeln('int _${interface_name}_${concrete_type_name}_index = $i;') } - if t.gen_types.len > 0 { // methods = '{TCCSKIP(0)}' // } sb.writeln('void* (* ${interface_name}_name_table[][$t.methods.len]) = ' + '{ \n $methods \n }; ') - }else{ + } + else { // The line below is needed so that C compilation succeeds, // even if no interface methods are called. // See https://github.com/zenith391/vgtk3/issues/7 diff --git a/vlib/compiler/expression.v b/vlib/compiler/expression.v index cafffbc26b..93149a65ff 100644 --- a/vlib/compiler/expression.v +++ b/vlib/compiler/expression.v @@ -227,7 +227,6 @@ fn (p mut Parser) name_expr() string { p.string_expr() return 'charptr' } - // known_type := p.table.known_type(name) orig_name := name is_c := name == 'C' && p.peek() == .dot @@ -722,18 +721,13 @@ fn (p mut Parser) factor() string { .key_offsetof { p.next() p.check(.lpar) - offsetof_typ := p.get_type() p.check(.comma) - member := p.check_name() p.check(.rpar) - p.gen('__offsetof($offsetof_typ, $member)') - return 'int' } - .amp, .dot, .mul { // (dot is for enum vals: `.green`) return p.name_expr() diff --git a/vlib/compiler/fn.v b/vlib/compiler/fn.v index c0761a4a1f..ed4e00c5e5 100644 --- a/vlib/compiler/fn.v +++ b/vlib/compiler/fn.v @@ -473,7 +473,7 @@ fn (p mut Parser) fn_decl() { p.table.register_fn(f) } } - p.set_current_fn( EmptyFn ) + p.set_current_fn(EmptyFn) p.skip_fn_body() return } @@ -767,26 +767,28 @@ fn (p mut Parser) fn_call(f mut Fn, method_ph int, receiver_var, receiver_type s } f.is_used = true cgen_name := p.table.fn_gen_name(f) - p.next() // fn name + p.next() // fn name mut generic_param_types := []string if p.tok == .lt { p.check(.lt) for { param_type := p.check_name() generic_param_types << param_type - if p.tok != .comma { break } + if p.tok != .comma { + break + } p.check(.comma) } p.check(.gt) // mut i := p.token_idx // for { - // if p.tokens[i].tok == .gt { - // //p.error('explicit type arguments are not allowed; remove `<...>`') - // } else if p.tokens[i].tok == .lpar { - // // probably a typo, do not concern the user with the above error message - // break - // } - // i++ + // if p.tokens[i].tok == .gt { + // //p.error('explicit type arguments are not allowed; remove `<...>`') + // } else if p.tokens[i].tok == .lpar { + // // probably a typo, do not concern the user with the above error message + // break + // } + // i++ // } } // if p.pref.is_prof { @@ -1013,7 +1015,7 @@ fn (p mut Parser) fn_call_args(f mut Fn, generic_param_types []string) { file_path := cescaped_path(p.file_path) p.cgen.resetln(p.cgen.cur_line.replace('v_panic (', 'panic_debug ($p.scanner.line_nr, tos3("$file_path"), tos3("$mod_name"), tos2((byte *)"$fn_name"), ')) } - //mut saved_args := []string + // mut saved_args := []string mut saved_args := generic_param_types for i, arg in f.args { // Receiver is the first arg @@ -1026,19 +1028,16 @@ fn (p mut Parser) fn_call_args(f mut Fn, generic_param_types []string) { // if f.args[0].typ.ends_with('*') { // p.gen('&/*119*/') // } - - //pos := p.cgen.cur_line.index('/* ? */') - //if pos > -1 { - // expr := p.cgen.cur_line[pos..] - // // TODO hack - // // If current expression is a func call, generate the array hack - // if expr.contains('(') { - // p.cgen.set_placeholder(pos, '(${arg.typ[..arg.typ.len-1]}[]){') - // p.gen('}[0] ') - // } - //} - - + // pos := p.cgen.cur_line.index('/* ? */') + // if pos > -1 { + // expr := p.cgen.cur_line[pos..] + // // TODO hack + // // If current expression is a func call, generate the array hack + // if expr.contains('(') { + // p.cgen.set_placeholder(pos, '(${arg.typ[..arg.typ.len-1]}[]){') + // p.gen('}[0] ') + // } + // } continue } // Reached the final vararg? Quit @@ -1520,7 +1519,9 @@ fn (p mut Parser) dispatch_generic_fn_instance(f mut Fn, ti &TypeInst) { } f.type_inst << *ti p.table.register_fn(f) - if f.is_method { f.name = f.receiver_typ + '_' + f.name } + if f.is_method { + f.name = f.receiver_typ + '_' + f.name + } rename_generic_fn_instance(mut f, ti) replace_generic_type_params(mut f, ti) // TODO: Handle case where type not defined yet, see above @@ -1543,7 +1544,8 @@ fn (p mut Parser) dispatch_generic_fn_instance(f mut Fn, ti &TypeInst) { gp.generic_dispatch = *ti gp.next() gp.fn_decl() - gp.generic_dispatch = TypeInst{} + gp.generic_dispatch = TypeInst{ + } p.cgen.lines_extra << p.cgen.lines p.restore_state(saved_state, false, true) p.cgen.fns << '${p.fn_signature(f)};' diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index 9e97a426cf..386707ae8d 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -156,15 +156,13 @@ pub fn (v &V) finalize_compilation() { pub fn (v mut V) add_parser(parser Parser) int { pidx := v.parsers.len v.parsers << parser - file_path := if filepath.is_abs(parser.file_path) { - parser.file_path } else { os.realpath(parser.file_path) } + file_path := if filepath.is_abs(parser.file_path) { parser.file_path } else { os.realpath(parser.file_path) } v.file_parser_idx[file_path] = pidx return pidx } pub fn (v &V) get_file_parser_index(file string) ?int { - file_path := if filepath.is_abs(file) { - file } else { os.realpath(file) } + file_path := if filepath.is_abs(file) { file } else { os.realpath(file) } if file_path in v.file_parser_idx { return v.file_parser_idx[file_path] } @@ -1058,7 +1056,7 @@ pub fn new_v(args []string) &V { is_live: '-live' in args sanitize: '-sanitize' in args // nofmt: '-nofmt' in args - + show_c_cmd: '-show_c_cmd' in args translated: 'translated' in args is_run: 'run' in args @@ -1077,7 +1075,7 @@ pub fn new_v(args []string) &V { building_v: !is_repl && (rdir_name == 'compiler' || rdir_name == 'v.v' || dir.contains('vlib')) comptime_define: comptime_define // is_fmt: comptime_define == 'vfmt' - + user_mod_path: user_mod_path vlib_path: vlib_path vpath: vpath diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index 1ab35f3f60..f4872386a6 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -980,7 +980,7 @@ fn (p mut Parser) get_type() string { // Register anon fn type fn_typ := Type{ name: f.typ_str() // 'fn (int, int) string' - + mod: p.mod func: f } @@ -1100,7 +1100,9 @@ fn (p mut Parser) get_type() string { if type_param in p.generic_dispatch.inst { typ = '${typ}_' + p.generic_dispatch.inst[type_param] } - if p.tok != .comma { break } + if p.tok != .comma { + break + } p.check(.comma) } p.check(.gt) @@ -3020,7 +3022,9 @@ fn (p mut Parser) skip_block(inside_first_lcbr bool) { } if p.tok == .rcbr { cbr_depth-- - if cbr_depth == 0 { break } + if cbr_depth == 0 { + break + } } p.next() } diff --git a/vlib/compiler/scanner.v b/vlib/compiler/scanner.v index 6162d49fbc..1bfebd1538 100644 --- a/vlib/compiler/scanner.v +++ b/vlib/compiler/scanner.v @@ -889,3 +889,4 @@ fn (s &Scanner) validate_var_name(name string) { s.error('bad variable name `$name`\n' + 'looks like you have a multi-word name without separating them with `_`' + '\nfor example, use `registration_date` instead of `registrationdate` ') } } + diff --git a/vlib/compiler/struct.v b/vlib/compiler/struct.v index 7b2509ecbc..687bc2508f 100644 --- a/vlib/compiler/struct.v +++ b/vlib/compiler/struct.v @@ -7,6 +7,8 @@ import ( strings ) // also unions and interfaces + + fn (p mut Parser) struct_decl(generic_param_types []string) { decl_tok_idx := p.cur_tok_index() is_pub := p.tok == .key_pub @@ -39,26 +41,25 @@ fn (p mut Parser) struct_decl(generic_param_types []string) { if is_interface && !name.ends_with('er') { p.error('interface names temporarily have to end with `er` (e.g. `Speaker`, `Reader`)') } - mut generic_types := map[string]string mut is_generic := false if p.tok == .lt { p.check(.lt) for i := 0; ; i++ { - if generic_param_types.len > 0 && i > generic_param_types.len-1 { + if generic_param_types.len > 0 && i > generic_param_types.len - 1 { p.error('mismatched generic type params') } type_param := p.check_name() - generic_types[type_param] = if generic_param_types.len > 0 { - generic_param_types[i] } else { '' } - if p.tok != .comma { break } + generic_types[type_param] = if generic_param_types.len > 0 { generic_param_types[i] } else { '' } + if p.tok != .comma { + break + } p.check(.comma) } p.check(.gt) is_generic = true } is_generic_instance := is_generic && generic_param_types.len > 0 - is_c := name == 'C' && p.tok == .dot if is_c { /* @@ -101,10 +102,12 @@ fn (p mut Parser) struct_decl(generic_param_types []string) { p.gen_typedef('typedef $kind $name $name;') } // TODO: handle error - parser_idx := p.v.get_file_parser_index(p.file_path) or { 0 } - //if !p.scanner.is_vh { - // parser_idx = p.v.get_file_parser_index(p.file_path) or { panic('cant find parser idx for $p.file_path') } - //} + parser_idx := p.v.get_file_parser_index(p.file_path) or { + 0 + } + // if !p.scanner.is_vh { + // parser_idx = p.v.get_file_parser_index(p.file_path) or { panic('cant find parser idx for $p.file_path') } + // } // Register the type mut is_ph := false if typ.is_placeholder { @@ -251,9 +254,14 @@ fn (p mut Parser) struct_decl(generic_param_types []string) { // `pub` access mod // access_mod := if is_pub_field { AccessMod.public } else { AccessMod.private} p.fspace() - defer { if is_generic_instance { p.generic_dispatch=TypeInst{} } } + defer { + if is_generic_instance { + p.generic_dispatch = TypeInst{ + } + } + } if is_generic_instance { - p.generic_dispatch=TypeInst{ + p.generic_dispatch = TypeInst{ inst: generic_types } } @@ -312,8 +320,7 @@ fn (p mut Parser) struct_decl(generic_param_types []string) { did_gen_something = true is_mut := access_mod in [.private_mut, .public_mut, .global] if p.first_pass() || is_generic { - p.table.add_field(typ.name, field_name, field_type, is_mut, - attr, access_mod) + p.table.add_field(typ.name, field_name, field_type, is_mut, attr, access_mod) } p.fgen_nl() // newline between struct fields } @@ -337,7 +344,6 @@ fn (p mut Parser) struct_init(typ_ string) string { if !t.is_public && t.mod != p.mod { p.warn('type `$t.name` is private') } - // generic struct init if p.peek() == .lt { p.next() @@ -349,14 +355,15 @@ fn (p mut Parser) struct_init(typ_ string) string { type_param = p.generic_dispatch.inst[type_param] } type_params << type_param - if p.tok != .comma { break } + if p.tok != .comma { + break + } p.check(.comma) } p.dispatch_generic_struct(mut t, type_params) t = p.table.find_type(t.name) typ = t.name } - if p.gen_struct_init(typ, &t) { return typ } @@ -500,7 +507,7 @@ fn (p mut Parser) dispatch_generic_struct(t mut Type, type_params []string) { mut generic_types := map[string]string if t.name in p.table.generic_struct_params { mut i := 0 - for _,v in p.table.generic_struct_params[t.name] { + for _, v in p.table.generic_struct_params[t.name] { generic_types[v] = type_params[i] i++ } @@ -510,7 +517,6 @@ fn (p mut Parser) dispatch_generic_struct(t mut Type, type_params []string) { } p.cgen.typedefs << 'typedef struct $t.name $t.name;\n' } - mut gp := p.v.parsers[t.parser_idx] gp.is_vgen = true saved_state := p.save_state() diff --git a/vlib/compiler/table.v b/vlib/compiler/table.v index 9415bb339a..612a9e0a3b 100644 --- a/vlib/compiler/table.v +++ b/vlib/compiler/table.v @@ -7,19 +7,19 @@ import strings struct Table { pub mut: - typesmap map[string]Type - consts []Var - fns map[string]Fn - obf_ids map[string]int // obf_ids['myfunction'] == 23 - modules []string // List of all modules registered by the application - imports []string // List of all imports - cflags []CFlag // ['-framework Cocoa', '-lglfw3'] - fn_cnt int // atomic - obfuscate bool - varg_access []VargAccess + typesmap map[string]Type + consts []Var + fns map[string]Fn + obf_ids map[string]int // obf_ids['myfunction'] == 23 + modules []string // List of all modules registered by the application + imports []string // List of all imports + cflags []CFlag // ['-framework Cocoa', '-lglfw3'] + fn_cnt int // atomic + obfuscate bool + varg_access []VargAccess // enum_vals map[string][]string // names []Name - max_field_len map[string]int // for vfmt: max_field_len['Parser'] == 12 + max_field_len map[string]int // for vfmt: max_field_len['Parser'] == 12 generic_struct_params map[string][]string }