| 1 | // Copyright (c) 2026 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | |
| 5 | module cleanc |
| 6 | |
| 7 | import v2.ast |
| 8 | import strings |
| 9 | |
| 10 | fn (mut g Gen) gen_string_inter_literal(node ast.StringInterLiteral) { |
| 11 | if g.is_freestanding_target() { |
| 12 | g.sb.write_string(g.c_freestanding_missing_format_string_expr()) |
| 13 | return |
| 14 | } |
| 15 | // Two-pass snprintf: first into a 256-byte stack buffer to measure, |
| 16 | // then use it directly or heap-allocate only when it doesn't fit. |
| 17 | // This avoids the unconditional heap allocation of asprintf. |
| 18 | // Wrapped in GCC compound expression ({ ... }) |
| 19 | mut fmt_str := strings.new_builder(64) |
| 20 | for i, raw_val in node.values { |
| 21 | mut val := raw_val |
| 22 | // Strip only the outer string delimiters. Using trim_* here can over-strip |
| 23 | // escaped quotes in edge chunks like: 'A "${x}"', leaving a trailing `\`. |
| 24 | if i == 0 && val.len > 0 && val[0] in [`'`, `"`] { |
| 25 | val = val[1..] |
| 26 | } |
| 27 | if i == node.values.len - 1 && val.len > 0 && val[val.len - 1] in [`'`, `"`] { |
| 28 | val = val[..val.len - 1] |
| 29 | } |
| 30 | escaped := val.replace('%', '%%').replace('\t', '\\t') |
| 31 | fmt_str.write_string(escaped) |
| 32 | if i < node.inters.len { |
| 33 | inter := node.inters[i] |
| 34 | fmt_str.write_string(g.get_sprintf_format(inter)) |
| 35 | } |
| 36 | } |
| 37 | fmt_lit := c_string_literal_content_to_c(fmt_str.str()) |
| 38 | |
| 39 | // Build the argument list string once (used in both snprintf calls). |
| 40 | // write_sprintf_arg writes to g.sb, so we temporarily swap it out, |
| 41 | // capture the result, and restore it. |
| 42 | mut args_sb := strings.new_builder(64) |
| 43 | for inter in node.inters { |
| 44 | args_sb.write_string(', ') |
| 45 | saved := g.sb |
| 46 | g.sb = strings.new_builder(64) |
| 47 | g.write_sprintf_arg(inter) |
| 48 | arg_str := g.sb.str() |
| 49 | g.sb = saved |
| 50 | args_sb.write_string(arg_str) |
| 51 | } |
| 52 | args_str := args_sb.str() |
| 53 | |
| 54 | // Emit: try stack buffer first, fall back to malloc only if needed. |
| 55 | g.sb.write_string('({ char _sib[256]; int _sil = snprintf(_sib, sizeof(_sib), ${fmt_lit}${args_str}); ') |
| 56 | g.sb.write_string('char* _sip; if (_sil < (int)sizeof(_sib)) { _sip = memdup(_sib, _sil + 1); } ') |
| 57 | g.sb.write_string('else { _sip = (char*)malloc(_sil + 1); snprintf(_sip, _sil + 1, ${fmt_lit}${args_str}); } ') |
| 58 | g.sb.write_string('${c_v_string_expr_from_ptr_len('_sip', '_sil', false)}; })') |
| 59 | } |
| 60 | |
| 61 | fn string_inter_needs_selector_type_fallback(expr_type string) bool { |
| 62 | return expr_type == '' || expr_type == 'int' || expr_type == 'int_literal' |
| 63 | || expr_type == 'void' || expr_type == 'void*' || expr_type == 'voidptr' |
| 64 | } |
| 65 | |
| 66 | fn (mut g Gen) string_inter_expr_type(expr ast.Expr) string { |
| 67 | expr_type := g.get_expr_type(expr) |
| 68 | if expr is ast.SelectorExpr && string_inter_needs_selector_type_fallback(expr_type) { |
| 69 | declared_type := g.selector_declared_field_type(expr) |
| 70 | if !string_inter_needs_selector_type_fallback(declared_type) { |
| 71 | return declared_type |
| 72 | } |
| 73 | field_type := g.selector_field_type(expr) |
| 74 | if !string_inter_needs_selector_type_fallback(field_type) { |
| 75 | return field_type |
| 76 | } |
| 77 | } |
| 78 | return expr_type |
| 79 | } |
| 80 | |
| 81 | fn (mut g Gen) write_sprintf_arg(inter ast.StringInter) { |
| 82 | expr_type := g.string_inter_expr_type(inter.expr) |
| 83 | expr_src := g.expr_to_string(inter.expr) |
| 84 | fmt := g.get_sprintf_format(inter) |
| 85 | // Keep vararg C types aligned with the emitted format string. |
| 86 | // If formatter expects a non-string argument, pass expression as-is. |
| 87 | if !fmt.ends_with('s') { |
| 88 | if expr_src == '' { |
| 89 | g.sb.write_string('0') |
| 90 | } else { |
| 91 | g.sb.write_string(expr_src) |
| 92 | } |
| 93 | return |
| 94 | } |
| 95 | str_fn := g.get_str_fn_for_type(expr_type) or { '' } |
| 96 | // Float types: use V's str() for default formatting ('0.0' not '0.000000') |
| 97 | if expr_type in ['f64', 'f32', 'float_literal'] && inter.format == .unformatted { |
| 98 | str_name := if expr_type == 'f32' { 'f32__str' } else { 'f64__str' } |
| 99 | g.sb.write_string('${str_name}(') |
| 100 | if expr_src == '' { |
| 101 | g.sb.write_string('0') |
| 102 | } else { |
| 103 | g.sb.write_string(expr_src) |
| 104 | } |
| 105 | g.sb.write_string(').str') |
| 106 | return |
| 107 | } |
| 108 | if expr_type == 'string' { |
| 109 | if expr_src == '' { |
| 110 | g.sb.write_string('""') |
| 111 | return |
| 112 | } |
| 113 | g.sb.write_string(expr_src) |
| 114 | g.sb.write_string('.str') |
| 115 | } else if expr_type == 'bool' { |
| 116 | g.sb.write_string('(') |
| 117 | if expr_src == '' { |
| 118 | g.sb.write_string('false') |
| 119 | } else { |
| 120 | g.sb.write_string(expr_src) |
| 121 | } |
| 122 | g.sb.write_string(' ? "true" : "false")') |
| 123 | } else if str_fn != '' { |
| 124 | g.sb.write_string('${str_fn}(') |
| 125 | if expr_src == '' { |
| 126 | g.sb.write_string('0') |
| 127 | } else { |
| 128 | g.sb.write_string(expr_src) |
| 129 | } |
| 130 | g.sb.write_string(').str') |
| 131 | } else { |
| 132 | if expr_src == '' { |
| 133 | g.sb.write_string('0') |
| 134 | } else { |
| 135 | g.sb.write_string(expr_src) |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | fn (mut g Gen) get_sprintf_format(inter ast.StringInter) string { |
| 141 | mut fmt := '%' |
| 142 | mut width := inter.width |
| 143 | mut precision := inter.precision |
| 144 | // Extract width/precision from format_expr if not set explicitly |
| 145 | if width == 0 && precision == 0 && inter.format_expr !is ast.EmptyExpr { |
| 146 | if inter.format_expr is ast.BasicLiteral { |
| 147 | val := inter.format_expr.value |
| 148 | if val.contains('.') { |
| 149 | // ".3" or "0.3" → precision |
| 150 | parts := val.split('.') |
| 151 | if parts.len == 2 { |
| 152 | if parts[0].len > 0 && parts[0] != '0' { |
| 153 | width = parts[0].int() |
| 154 | } |
| 155 | precision = parts[1].int() |
| 156 | } |
| 157 | } else { |
| 158 | // Plain number → width (e.g., "03" for zero-padded) |
| 159 | if val.starts_with('0') && val.len > 1 { |
| 160 | fmt += '0' |
| 161 | } |
| 162 | width = val.int() |
| 163 | } |
| 164 | } else if inter.format_expr is ast.PrefixExpr { |
| 165 | // Negative alignment: -10 → left-align with width 10 |
| 166 | if inter.format_expr.op == .minus && inter.format_expr.expr is ast.BasicLiteral { |
| 167 | fmt += '-' |
| 168 | width = inter.format_expr.expr.value.int() |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | // Width |
| 173 | if width > 0 { |
| 174 | fmt += '${width}' |
| 175 | } |
| 176 | // Precision |
| 177 | if precision > 0 { |
| 178 | fmt += '.${precision}' |
| 179 | } |
| 180 | // Format specifier |
| 181 | expr_type := g.string_inter_expr_type(inter.expr) |
| 182 | if inter.format != .unformatted { |
| 183 | match inter.format { |
| 184 | .decimal { fmt += 'd' } |
| 185 | .float { fmt += 'f' } |
| 186 | .hex { fmt += 'x' } |
| 187 | .octal { fmt += 'o' } |
| 188 | .character { fmt += 'c' } |
| 189 | .exponent { fmt += 'e' } |
| 190 | .exponent_short { fmt += 'g' } |
| 191 | .binary { fmt += 'd' } // binary not supported in printf, fallback to decimal |
| 192 | .pointer_address { fmt += 'p' } |
| 193 | .string { fmt += 's' } |
| 194 | .unformatted { fmt += 'd' } |
| 195 | } |
| 196 | |
| 197 | return fmt |
| 198 | } |
| 199 | if inter.resolved_fmt != '' { |
| 200 | if expr_type in ['string', 'bool', 'f32', 'f64', 'float_literal'] { |
| 201 | return '%s' |
| 202 | } |
| 203 | if _ := g.get_str_fn_for_type(expr_type) { |
| 204 | return '%s' |
| 205 | } |
| 206 | return inter.resolved_fmt |
| 207 | } |
| 208 | // Infer from expression type |
| 209 | match expr_type { |
| 210 | 'string' { |
| 211 | return '%s' |
| 212 | } |
| 213 | 'int', 'i8', 'i16', 'i32' { |
| 214 | return '%d' |
| 215 | } |
| 216 | 'i64' { |
| 217 | return '%lld' |
| 218 | } |
| 219 | 'u8', 'u16', 'u32' { |
| 220 | return '%u' |
| 221 | } |
| 222 | 'u64' { |
| 223 | return '%llu' |
| 224 | } |
| 225 | 'f32', 'f64', 'float_literal' { |
| 226 | // Use %s with V's str() function for default formatting, |
| 227 | // since C's %f produces '0.000000' instead of V's '0.0'. |
| 228 | return '%s' |
| 229 | } |
| 230 | 'bool' { |
| 231 | return '%s' |
| 232 | } |
| 233 | 'rune' { |
| 234 | return '%c' |
| 235 | } |
| 236 | 'char' { |
| 237 | return '%c' |
| 238 | } |
| 239 | else { |
| 240 | if _ := g.get_str_fn_for_type(expr_type) { |
| 241 | return '%s' |
| 242 | } |
| 243 | return '%d' |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // get_str_fn_for_type returns the name of the str() function for a type, if one exists. |
| 249 | |