cgen: fix `v -usecache examples/news_fetcher.v` on macos (.sort() compare_ functions duplication)

pull/13000/head
Delyan Angelov 2021-12-29 20:15:51 +02:00
parent 537760edcd
commit ca5154747e
2 changed files with 5 additions and 3 deletions

View File

@ -382,7 +382,7 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
// `users.sort(a.age > b.age)` // `users.sort(a.age > b.age)`
// Generate a comparison function for a custom type // Generate a comparison function for a custom type
elem_stype := g.typ(info.elem_type) elem_stype := g.typ(info.elem_type)
mut compare_fn := 'compare_${elem_stype.replace('*', '_ptr')}' mut compare_fn := 'compare_${g.unique_file_path_hash}_${elem_stype.replace('*', '_ptr')}'
mut comparison_type := g.unwrap(ast.void_type) mut comparison_type := g.unwrap(ast.void_type)
mut left_expr, mut right_expr := '', '' mut left_expr, mut right_expr := '', ''
// the only argument can only be an infix expression like `a < b` or `b.field > a.field` // the only argument can only be an infix expression like `a < b` or `b.field > a.field`
@ -443,7 +443,7 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
} }
stype_arg := g.typ(info.elem_type) stype_arg := g.typ(info.elem_type)
g.definitions.writeln('int ${compare_fn}($stype_arg* a, $stype_arg* b) {') g.definitions.writeln('VV_LOCAL_SYMBOL int ${compare_fn}($stype_arg* a, $stype_arg* b) {')
c_condition := if comparison_type.sym.has_method('<') { c_condition := if comparison_type.sym.has_method('<') {
'${g.typ(comparison_type.typ)}__lt($left_expr, $right_expr)' '${g.typ(comparison_type.typ)}__lt($left_expr, $right_expr)'
} else if comparison_type.unaliased_sym.has_method('<') { } else if comparison_type.unaliased_sym.has_method('<') {

View File

@ -5,6 +5,7 @@ module c
import os import os
import strings import strings
import hash.fnv1a
import v.ast import v.ast
import v.pref import v.pref
import v.token import v.token
@ -72,6 +73,7 @@ mut:
json_forward_decls strings.Builder // json type forward decls json_forward_decls strings.Builder // json type forward decls
sql_buf strings.Builder // for writing exprs to args via `sqlite3_bind_int()` etc sql_buf strings.Builder // for writing exprs to args via `sqlite3_bind_int()` etc
file &ast.File file &ast.File
unique_file_path_hash u64 // a hash of file.path, used for making auxilary fn generation unique (like `compare_xyz`)
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0 fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
last_fn_c_name string last_fn_c_name string
tmp_count int // counter for unique tmp vars (_tmp1, _tmp2 etc); resets at the start of each fn. tmp_count int // counter for unique tmp vars (_tmp1, _tmp2 etc); resets at the start of each fn.
@ -579,7 +581,7 @@ pub fn (mut g Gen) free_builders() {
pub fn (mut g Gen) gen_file() { pub fn (mut g Gen) gen_file() {
g.timers.start('cgen_file $g.file.path') g.timers.start('cgen_file $g.file.path')
g.unique_file_path_hash = fnv1a.sum64_string(g.file.path)
if g.pref.is_vlines { if g.pref.is_vlines {
g.vlines_path = util.vlines_escape_path(g.file.path, g.pref.ccompiler) g.vlines_path = util.vlines_escape_path(g.file.path, g.pref.ccompiler)
g.is_vlines_enabled = true g.is_vlines_enabled = true