9942 commits 0 issues 0 pull requests 439 contributors 0 branches 0 releases
Additions: 53 Deletions: 33
vlib/v/gen/c/cgen.v
1 g.auto_fn_definitions << sb.str()
2 }
3
4+fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr bool, exp_styp string, got_is_ptr bool, got_styp string) {
5+ mut rparen_n := 1
6+ if exp_is_ptr {
7+ g.write('HEAP($exp_styp, ')
8+ rparen_n++
9+ }
10+ g.write('${fname}(')
11+ if !got_is_ptr {
12+ if !expr.is_lvalue()
13+ || (expr is ast.Ident && is_simple_define_const((expr as ast.Ident).obj)) {
14+ g.write('ADDR($got_styp, (')
15+ rparen_n += 2
16+ } else {
17+ g.write('&')
18+ }
19+ }
20+ g.expr(expr)
21+ g.write(')'.repeat(rparen_n))
22+}
23+
24 // use instead of expr() when you need to cast to a different type
25 fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_type ast.Type) {
26 got_type := g.table.mktyp(got_type_raw)
27 && !expected_type.has_flag(.optional) {
28 got_styp := g.cc_type(got_type, true)
29 exp_styp := g.cc_type(expected_type, true)
30- if expected_is_ptr {
31- g.write('HEAP($exp_styp, ')
32- }
33- g.write('I_${got_styp}_to_Interface_${exp_styp}(')
34- if !got_is_ptr {
35- g.write('&')
36- }
37- g.expr(expr)
38- g.write(')')
39- if expected_is_ptr {
40- g.write(')')
41- }
42+ fname := 'I_${got_styp}_to_Interface_$exp_styp'
43+ g.call_cfn_for_casting_expr(fname, expr, expected_is_ptr, exp_styp, got_is_ptr,
44+ got_styp)
45 return
46 }
47 // cast to sum type
48 g.expr(expr)
49 } else {
50 g.write_sumtype_casting_fn(got_type, expected_type)
51- if expected_is_ptr {
52- g.write('HEAP($exp_sym.cname, ')
53- }
54- g.write('${got_sym.cname}_to_sumtype_${exp_sym.cname}(')
55- if !got_is_ptr {
56- g.write('ADDR($got_styp, (')
57- g.expr(expr)
58- g.write(')))')
59- } else {
60- g.expr(expr)
61- g.write(')')
62- }
63- if expected_is_ptr {
64- g.write(')')
65- }
66+ fname := '${got_sym.cname}_to_sumtype_$exp_sym.cname'
67+ g.call_cfn_for_casting_expr(fname, expr, expected_is_ptr, exp_sym.cname,
68+ got_is_ptr, got_styp)
69 }
70 return
71 }
72 */
73 field_expr := field.expr
74 match field.expr {
75- ast.CharLiteral, ast.FloatLiteral, ast.IntegerLiteral {
76- // "Simple" expressions are not going to need multiple statements,
77- // only the ones which are inited later, so it's safe to use expr_string
78- g.const_decl_simple_define(name, g.expr_string(field_expr))
79- }
80 ast.ArrayInit {
81 if field.expr.is_fixed {
82 styp := g.typ(field.expr.typ)
83 }
84 }
85 else {
86- g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
87+ if is_simple_define_const(field) {
88+ // "Simple" expressions are not going to need multiple statements,
89+ // only the ones which are inited later, so it's safe to use expr_string
90+ g.const_decl_simple_define(name, g.expr_string(field_expr))
91+ } else {
92+ g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
93+ }
94 }
95 }
96 }
97 }
98
99+fn is_simple_define_const(obj ast.ScopeObject) bool {
100+ if obj is ast.ConstField {
101+ return match obj.expr {
102+ ast.CharLiteral, ast.FloatLiteral, ast.IntegerLiteral { true }
103+ else { false }
104+ }
105+ }
106+ return false
107+}
108+
109 fn (mut g Gen) const_decl_simple_define(name string, val string) {
110 // Simple expressions should use a #define
111 // so that we don't pollute the binary with unnecessary global vars
112
vlib/v/tests/interface_test.v
1 breed string
2 }
3
4+fn new_cat(breed string) Cat {
5+ return Cat{breed}
6+}
7+
8 fn (c &Cat) name() string {
9 if c.breed != '' {
10 assert c.breed == 'Persian'
11 perform_speak(Cat{
12 breed: 'Persian'
13 })
14+ perform_speak(new_cat('Persian'))
15 perform_speak_on_ptr(cat)
16 perform_speak_on_ptr(Cat{
17 breed: 'Persian'
18 })
19+ perform_speak_on_ptr(new_cat('Persian'))
20 handle_animals([dog, cat])
21 /*
22 f := Foo {
23
vlib/v/tests/sumtype_calls_test.v
1 fn test_sum_types() {
2 b := parse_bool()
3 handle_expr(b)
4+ handle_expr(parse_bool())
5+
6 de := DeclExprA{}
7 handle_expr(de)
8 handle_decl_expr(de)
9+ handle_expr(DeclExprA{})
10+ handle_decl_expr(DeclExprA{})
11 }
12
13 /*
14