Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/tools/vast/vast.v
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ fn (t Tree) fn_decl(node ast.FnDecl) &Node {
obj.add_terse('is_variadic', t.bool_node(node.is_variadic))
obj.add('is_anon', t.bool_node(node.is_anon))
obj.add_terse('is_noreturn', t.bool_node(node.is_noreturn))
obj.add_terse('is_weak', t.bool_node(node.is_weak))
obj.add_terse('is_manualfree', t.bool_node(node.is_manualfree))
obj.add('is_main', t.bool_node(node.is_main))
obj.add('is_test', t.bool_node(node.is_test))
Expand Down Expand Up @@ -840,6 +841,8 @@ fn (t Tree) global_field(node ast.GlobalField) &Node {
obj.add_terse('typ', t.type_node(node.typ))
obj.add_terse('has_expr', t.bool_node(node.has_expr))
obj.add_terse('is_markused', t.bool_node(node.is_markused))
obj.add_terse('is_weak', t.bool_node(node.is_weak))
obj.add_terse('is_hidden', t.bool_node(node.is_hidden))
obj.add('comments', t.array_node_comment(node.comments))
obj.add('pos', t.pos(node.pos))
obj.add('typ_pos', t.pos(node.typ_pos))
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ pub:
module_pos int = -1 // module:
is_union bool
is_option bool
is_aligned bool
attrs []Attr
pre_comments []Comment
end_comments []Comment
Expand Down Expand Up @@ -591,6 +592,7 @@ pub:
is_c_extern bool
is_variadic bool
is_anon bool
is_weak bool
is_noreturn bool // true, when @[noreturn] is used on a fn
is_manualfree bool // true, when @[manualfree] is used on a fn
is_main bool // true for `fn main()`
Expand Down Expand Up @@ -965,6 +967,8 @@ pub:
is_markused bool // an explicit `@[markused]` tag; the global will NOT be removed by `-skip-unused`
is_volatile bool
is_exported bool // an explicit `@[export]` tag; the global will NOT be removed by `-skip-unused`
is_weak bool
is_hidden bool
pub mut:
expr Expr
typ Type
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/ast/table.v
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ pub mut:
// json bool // json is imported
comptime_calls map[string]bool // resolved name to call on comptime
comptime_syms map[Type]bool // resolved syms (generic)
//
uses_attr_noreturn bool // @[noreturn]
uses_attr_hidden bool // @[hidden]
uses_attr_weak bool // @[weak]
}

@[unsafe]
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,16 @@ pub fn (mut g Gen) init() {
} else {
g.cheaders.writeln(c_headers)
}
if !g.pref.skip_unused || g.table.used_features.uses_attr_weak {
g.cheaders.writeln(c_common_weak_attr)
}
if !g.pref.skip_unused || g.table.used_features.uses_attr_hidden {
g.cheaders.writeln(c_common_hidden_attr)
}
if !g.pref.skip_unused || g.table.used_features.uses_attr_noreturn {
g.cheaders.writeln(c_common_noreturn_attr)
g.cheaders.writeln(c_common_unreachable_attr)
}
if !g.pref.skip_unused || g.table.used_features.used_maps > 0 {
g.cheaders.writeln(c_wyhash_headers)
}
Expand Down
34 changes: 16 additions & 18 deletions vlib/v/gen/c/cheaders.v
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ const c_common_macros = '
#endif
#endif

#define OPTION_CAST(x) (x)

#if defined(_WIN32) || defined(__CYGWIN__)
#define VV_EXP extern __declspec(dllexport)
#define VV_LOC static
Expand Down Expand Up @@ -220,7 +218,17 @@ const c_common_macros = '
#undef __has_include
#endif

//likely and unlikely macros
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
#define _likely_(x) __builtin_expect(x,1)
#define _unlikely_(x) __builtin_expect(x,0)
#else
#define _likely_(x) (x)
#define _unlikely_(x) (x)
#endif
'

const c_common_weak_attr = '
#if !defined(VWEAK)
#define VWEAK __attribute__((weak))
#ifdef _MSC_VER
Expand All @@ -232,7 +240,9 @@ const c_common_macros = '
#define VWEAK
#endif
#endif
'

const c_common_hidden_attr = '
#if !defined(VHIDDEN)
#define VHIDDEN __attribute__((visibility("hidden")))
#ifdef _MSC_VER
Expand All @@ -244,7 +254,9 @@ const c_common_macros = '
#define VHIDDEN
#endif
#endif
'

const c_common_noreturn_attr = '
#if !defined(VNORETURN)
#if defined(__TINYC__)
#include <stdnoreturn.h>
Expand All @@ -259,7 +271,9 @@ const c_common_macros = '
#define VNORETURN
#endif
#endif
'

const c_common_unreachable_attr = '
#if !defined(VUNREACHABLE)
#if defined(__GNUC__) && !defined(__clang__)
#define V_GCC_VERSION (__GNUC__ * 10000L + __GNUC_MINOR__ * 100L + __GNUC_PATCHLEVEL__)
Expand All @@ -276,16 +290,6 @@ const c_common_macros = '
#define VUNREACHABLE() do { } while (0)
#endif
#endif

//likely and unlikely macros
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
#define _likely_(x) __builtin_expect(x,1)
#define _unlikely_(x) __builtin_expect(x,0)
#else
#define _likely_(x) (x)
#define _unlikely_(x) (x)
#endif

'

const c_unsigned_comparison_functions = '
Expand Down Expand Up @@ -433,8 +437,6 @@ void v_free(voidptr ptr);
#define _Atomic volatile

// MSVC cannot parse some things properly
#undef OPTION_CAST
#define OPTION_CAST(x)
#undef __NOINLINE
#undef __IRQHANDLER
#define __NOINLINE __declspec(noinline)
Expand All @@ -451,9 +453,6 @@ void v_free(voidptr ptr);
#endif
#endif

// g_live_info is used by live.info()
static void* g_live_info = NULL;

#if defined(__MINGW32__) || defined(__MINGW64__) || (defined(_WIN32) && defined(__TINYC__))
#undef PRId64
#undef PRIi64
Expand Down Expand Up @@ -711,5 +710,4 @@ static inline uint64_t wy2u0k(uint64_t r, uint64_t k){ _wymum(&r,&k); return k;
#endif

#define _IN_MAP(val, m) map_exists(m, val)

'
7 changes: 4 additions & 3 deletions vlib/v/gen/c/consts_and_globals.v
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,14 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) {
should_init := (!g.pref.use_cache && g.pref.build_mode != .build_module)
|| (g.pref.build_mode == .build_module && g.module_built == node.mod)
mut attributes := ''
if node.attrs.contains('weak') {
first_field := node.fields[0]
if first_field.is_weak {
attributes += 'VWEAK '
}
if node.attrs.contains('hidden') {
if first_field.is_hidden {
attributes += 'VHIDDEN '
}
if node.attrs.contains('export') {
if first_field.is_exported {
attributes += 'VV_EXP '
}
if attr := node.attrs.find_first('_linker_section') {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,9 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
if g.pref.printfn_list.len > 0 && g.last_fn_c_name in g.pref.printfn_list {
println(g.out.after(fn_start_pos))
}
weak := if node.is_weak { 'VWEAK ' } else { '' }
for attr in node.attrs {
if attr.name == 'export' {
weak := if node.attrs.any(it.name == 'weak') { 'VWEAK ' } else { '' }
g.writeln('// export alias: ${attr.arg} -> ${name}')
g.export_funcs << attr.arg
export_alias := '${weak}${type_name} ${fn_attrs}${attr.arg}(${arg_str})'
Expand Down
14 changes: 8 additions & 6 deletions vlib/v/markused/walker.v
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ pub fn (mut w Walker) mark_global_as_used(ckey string) {
}
w.used_globals[ckey] = true
gfield := w.all_globals[ckey] or { return }
w.table.used_features.uses_attr_weak = w.table.used_features.uses_attr_weak || gfield.is_weak
w.table.used_features.uses_attr_hidden = w.table.used_features.uses_attr_hidden
|| gfield.is_hidden
w.expr(gfield.expr)
if !gfield.has_expr {
w.mark_by_type(gfield.typ)
Expand Down Expand Up @@ -355,9 +358,7 @@ pub fn (mut w Walker) stmt(node_ ast.Stmt) {
w.mark_by_type(typ.typ)
}
w.struct_fields(node.fields)
if !w.uses_mem_align {
w.uses_mem_align = node.attrs.contains('aligned')
}
w.uses_mem_align = w.uses_mem_align || node.is_aligned
}
ast.DeferStmt {
w.stmts(node.stmts)
Expand Down Expand Up @@ -894,6 +895,9 @@ pub fn (mut w Walker) fn_decl(mut node ast.FnDecl) {
w.is_builtin_mod = last_is_builtin_mod
}
}
w.table.used_features.uses_attr_weak = w.table.used_features.uses_attr_weak || node.is_weak
w.table.used_features.uses_attr_noreturn = w.table.used_features.uses_attr_noreturn
|| node.is_noreturn
if node.language == .c {
w.mark_fn_as_used(node.fkey())
w.mark_fn_ret_and_params(node.return_type, node.params)
Expand Down Expand Up @@ -1153,9 +1157,7 @@ pub fn (mut w Walker) mark_by_sym(isym ast.TypeSymbol) {
}
if decl := w.all_structs[isym.name] {
w.struct_fields(decl.fields)
if !w.uses_mem_align {
w.uses_mem_align = decl.attrs.contains('aligned')
}
w.uses_mem_align = w.uses_mem_align || decl.is_aligned
for iface_typ in decl.implements_types {
w.mark_by_type(iface_typ.typ)
}
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/parser/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
mut is_c2v_variadic := false
mut is_c_extern := false
mut is_markused := false
mut is_weak := false
mut is_expand_simple_interpolation := false
mut comments := []ast.Comment{}
fn_attrs := p.attrs
Expand Down Expand Up @@ -254,6 +255,9 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
'c2v_variadic' {
is_c2v_variadic = true
}
'weak' {
is_weak = true
}
'use_new' {
is_ctor_new = true
}
Expand Down Expand Up @@ -703,6 +707,7 @@ run them via `v file.v` instead',
is_unsafe: is_unsafe
is_must_use: is_must_use
is_markused: is_markused
is_weak: is_weak
is_file_translated: p.is_translated
//
attrs: fn_attrs
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -2550,10 +2550,14 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {

mut is_markused := false
mut is_exported := false
mut is_weak := false
mut is_hidden := false
for ga in attrs {
match ga.name {
'export' { is_exported = true }
'markused' { is_markused = true }
'weak' { is_weak = true }
'hidden' { is_hidden = true }
else {}
}
}
Expand Down Expand Up @@ -2635,6 +2639,8 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
is_markused: is_markused
is_volatile: is_volatile
is_exported: is_exported
is_weak: is_weak
is_hidden: is_hidden
}
fields << field
if name !in ast.global_reserved_type_names {
Expand Down
1 change: 1 addition & 0 deletions vlib/v/parser/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
language: language
is_union: is_union
is_option: is_option
is_aligned: attrs.contains('aligned')
attrs: if is_anon { []ast.Attr{} } else { attrs } // anon structs can't have attributes
pre_comments: pre_comments
end_comments: end_comments
Expand Down
Loading