2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-06-29 17:47:01 +02:00
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
2021-02-02 15:41:51 +01:00
|
|
|
module c
|
2020-05-02 11:11:27 +02:00
|
|
|
|
2020-06-29 17:47:01 +02:00
|
|
|
import v.ast
|
2020-11-18 18:23:44 +01:00
|
|
|
import v.util
|
2020-06-29 17:47:01 +02:00
|
|
|
|
|
|
|
fn (mut g Gen) string_literal(node ast.StringLiteral) {
|
2022-01-11 21:36:18 +01:00
|
|
|
escaped_val := cescape_nonascii(util.smart_quote(node.val, node.is_raw))
|
2021-06-20 08:30:08 +02:00
|
|
|
if node.language == .c {
|
2020-06-29 17:47:01 +02:00
|
|
|
g.write('"$escaped_val"')
|
|
|
|
} else {
|
2020-12-03 16:02:48 +01:00
|
|
|
g.write('_SLIT("$escaped_val")')
|
2020-06-29 17:47:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 12:03:24 +02:00
|
|
|
// optimize string interpolation in string builders:
|
|
|
|
// `sb.writeln('a=$a')` =>
|
|
|
|
// `sb.writeln('a='); sb.writeln(a.str())`
|
|
|
|
fn (mut g Gen) string_inter_literal_sb_optimized(call_expr ast.CallExpr) {
|
|
|
|
node := call_expr.args[0].expr as ast.StringInterLiteral
|
|
|
|
// sb_name := g.cur_call_expr.left
|
|
|
|
// g.go_before_stmt(0)
|
|
|
|
g.writeln('// sb inter opt')
|
2020-07-13 18:59:39 +02:00
|
|
|
is_nl := call_expr.name == 'writeln'
|
|
|
|
// println('optimize sb $call_expr.name')
|
2020-07-11 12:03:24 +02:00
|
|
|
for i, val in node.vals {
|
2022-01-11 21:36:18 +01:00
|
|
|
escaped_val := cescape_nonascii(util.smart_quote(val, false))
|
2020-07-11 12:03:24 +02:00
|
|
|
// if val == '' {
|
|
|
|
// break
|
|
|
|
// continue
|
|
|
|
// }
|
2021-02-22 12:18:11 +01:00
|
|
|
g.write('strings__Builder_write_string(&')
|
2020-07-11 12:03:24 +02:00
|
|
|
g.expr(call_expr.left)
|
2020-12-03 16:02:48 +01:00
|
|
|
g.write(', _SLIT("')
|
2020-07-11 12:03:24 +02:00
|
|
|
g.write(escaped_val)
|
|
|
|
g.writeln('"));')
|
|
|
|
//
|
|
|
|
if i >= node.exprs.len {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// if node.expr_types.len <= i || node.exprs.len <= i {
|
|
|
|
// continue
|
|
|
|
// }
|
2020-07-13 18:59:39 +02:00
|
|
|
if is_nl && i == node.exprs.len - 1 {
|
|
|
|
g.write('strings__Builder_writeln(&')
|
|
|
|
} else {
|
2021-02-22 12:18:11 +01:00
|
|
|
g.write('strings__Builder_write_string(&')
|
2020-07-13 18:59:39 +02:00
|
|
|
}
|
2020-07-11 12:03:24 +02:00
|
|
|
g.expr(call_expr.left)
|
|
|
|
g.write(', ')
|
2020-07-13 18:59:39 +02:00
|
|
|
typ := node.expr_types[i]
|
2020-11-03 14:13:35 +01:00
|
|
|
g.write(g.typ(typ))
|
|
|
|
g.write('_str(')
|
2021-12-19 17:25:18 +01:00
|
|
|
sym := g.table.sym(typ)
|
2020-12-17 10:16:27 +01:00
|
|
|
if sym.kind != .function {
|
|
|
|
g.expr(node.exprs[i])
|
|
|
|
}
|
2020-07-11 12:03:24 +02:00
|
|
|
g.writeln('));')
|
|
|
|
}
|
|
|
|
g.writeln('')
|
|
|
|
// println(node.vals)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-02 00:57:09 +02:00
|
|
|
fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
|
2021-02-16 12:40:13 +01:00
|
|
|
is_shared := etype.has_flag(.shared_f)
|
2020-12-01 16:09:03 +01:00
|
|
|
mut typ := etype
|
2021-02-16 12:40:13 +01:00
|
|
|
if is_shared {
|
|
|
|
typ = typ.clear_flag(.shared_f).set_nr_muls(0)
|
|
|
|
}
|
2021-12-19 17:25:18 +01:00
|
|
|
mut sym := g.table.sym(typ)
|
2022-03-23 10:52:48 +01:00
|
|
|
// when type is alias and doesn't has `str()`, print the aliased value
|
|
|
|
if mut sym.info is ast.Alias && !sym.has_method('str') {
|
2021-12-19 17:25:18 +01:00
|
|
|
parent_sym := g.table.sym(sym.info.parent_type)
|
2020-12-01 16:09:03 +01:00
|
|
|
if parent_sym.has_method('str') {
|
|
|
|
typ = sym.info.parent_type
|
|
|
|
sym = parent_sym
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sym_has_str_method, str_method_expects_ptr, _ := sym.str_method_info()
|
|
|
|
if typ.has_flag(.variadic) {
|
2021-09-28 09:28:04 +02:00
|
|
|
str_fn_name := g.get_str_fn(typ)
|
2020-12-01 16:09:03 +01:00
|
|
|
g.write('${str_fn_name}(')
|
|
|
|
g.expr(expr)
|
|
|
|
g.write(')')
|
2021-04-02 00:57:09 +02:00
|
|
|
} else if typ == ast.string_type {
|
2022-02-06 09:52:36 +01:00
|
|
|
if etype.is_ptr() {
|
|
|
|
g.write('*')
|
|
|
|
}
|
2020-12-01 16:09:03 +01:00
|
|
|
g.expr(expr)
|
2021-04-02 00:57:09 +02:00
|
|
|
} else if typ == ast.bool_type {
|
2020-12-01 16:09:03 +01:00
|
|
|
g.expr(expr)
|
|
|
|
g.write(' ? _SLIT("true") : _SLIT("false")')
|
|
|
|
} else if sym.kind == .none_ {
|
2021-01-29 14:50:48 +01:00
|
|
|
g.write('_SLIT("<none>")')
|
2020-12-01 16:09:03 +01:00
|
|
|
} else if sym.kind == .enum_ {
|
2021-08-18 16:11:12 +02:00
|
|
|
if expr !is ast.EnumVal {
|
2021-09-28 09:28:04 +02:00
|
|
|
str_fn_name := g.get_str_fn(typ)
|
2020-12-01 16:09:03 +01:00
|
|
|
g.write('${str_fn_name}(')
|
|
|
|
g.enum_expr(expr)
|
|
|
|
g.write(')')
|
|
|
|
} else {
|
|
|
|
g.write('_SLIT("')
|
|
|
|
g.enum_expr(expr)
|
|
|
|
g.write('")')
|
|
|
|
}
|
2021-01-23 09:33:22 +01:00
|
|
|
} else if sym_has_str_method
|
|
|
|
|| sym.kind in [.array, .array_fixed, .map, .struct_, .multi_return, .sum_type, .interface_] {
|
2020-12-01 16:09:03 +01:00
|
|
|
is_ptr := typ.is_ptr()
|
2021-02-21 11:15:36 +01:00
|
|
|
is_var_mut := expr.is_auto_deref_var()
|
2021-09-28 09:28:04 +02:00
|
|
|
str_fn_name := g.get_str_fn(typ)
|
2021-01-31 02:45:45 +01:00
|
|
|
if is_ptr && !is_var_mut {
|
2021-05-28 17:44:30 +02:00
|
|
|
g.write('str_intp(1, _MOV((StrIntpData[]){{_SLIT("&"), $si_s_code ,{.d_s=')
|
2020-12-01 16:09:03 +01:00
|
|
|
}
|
|
|
|
g.write('${str_fn_name}(')
|
|
|
|
if str_method_expects_ptr && !is_ptr {
|
|
|
|
g.write('&')
|
2021-02-16 12:40:13 +01:00
|
|
|
} else if (!str_method_expects_ptr && is_ptr && !is_shared) || is_var_mut {
|
2020-12-01 16:09:03 +01:00
|
|
|
g.write('*')
|
|
|
|
}
|
|
|
|
if expr is ast.ArrayInit {
|
|
|
|
if expr.is_fixed {
|
|
|
|
s := g.typ(expr.typ)
|
2021-10-28 01:20:49 +02:00
|
|
|
if !expr.has_it {
|
|
|
|
g.write('($s)')
|
|
|
|
}
|
2020-12-01 16:09:03 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-19 14:38:48 +02:00
|
|
|
g.expr_with_cast(expr, typ, typ)
|
2021-02-16 12:40:13 +01:00
|
|
|
if is_shared {
|
|
|
|
g.write('->val')
|
|
|
|
}
|
2020-12-01 16:09:03 +01:00
|
|
|
g.write(')')
|
2021-01-31 02:45:45 +01:00
|
|
|
if is_ptr && !is_var_mut {
|
2021-05-28 17:44:30 +02:00
|
|
|
g.write('}}}))')
|
2021-05-24 04:20:45 +02:00
|
|
|
// g.write(')')
|
2020-12-01 16:09:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-28 09:28:04 +02:00
|
|
|
str_fn_name := g.get_str_fn(typ)
|
2020-12-01 16:09:03 +01:00
|
|
|
g.write('${str_fn_name}(')
|
2021-07-04 17:36:37 +02:00
|
|
|
if expr.is_auto_deref_var() {
|
|
|
|
g.write('*')
|
|
|
|
}
|
2020-12-17 10:16:27 +01:00
|
|
|
if sym.kind != .function {
|
2021-04-19 14:38:48 +02:00
|
|
|
g.expr_with_cast(expr, typ, typ)
|
2020-12-17 10:16:27 +01:00
|
|
|
}
|
2020-12-01 16:09:03 +01:00
|
|
|
g.write(')')
|
|
|
|
}
|
|
|
|
}
|