cgen: improve generated code formatting (#5693)
parent
fc7237be7b
commit
c94038af89
|
@ -177,40 +177,53 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
|
||||||
g.finish()
|
g.finish()
|
||||||
//
|
//
|
||||||
mut b := strings.new_builder(250000)
|
mut b := strings.new_builder(250000)
|
||||||
b.writeln(g.hashes())
|
b.write(g.hashes())
|
||||||
b.writeln(g.comptime_defines.str())
|
b.write(g.comptime_defines.str())
|
||||||
b.writeln('\n// V typedefs:')
|
b.writeln('\n// V typedefs:')
|
||||||
b.writeln(g.typedefs.str())
|
b.write(g.typedefs.str())
|
||||||
b.writeln('\n// V typedefs2:')
|
b.writeln('\n// V typedefs2:')
|
||||||
b.writeln(g.typedefs2.str())
|
b.write(g.typedefs2.str())
|
||||||
b.writeln('\n// V cheaders:')
|
b.writeln('\n// V cheaders:')
|
||||||
b.writeln(g.cheaders.str())
|
b.write(g.cheaders.str())
|
||||||
b.writeln('\n// V includes:')
|
b.writeln('\n// V includes:')
|
||||||
b.writeln(g.includes.str())
|
b.write(g.includes.str())
|
||||||
b.writeln('\n// Enum definitions:')
|
b.writeln('\n// Enum definitions:')
|
||||||
b.writeln(g.enum_typedefs.str())
|
b.write(g.enum_typedefs.str())
|
||||||
b.writeln('\n// V type definitions:')
|
b.writeln('\n// V type definitions:')
|
||||||
b.writeln(g.type_definitions.str())
|
b.write(g.type_definitions.str())
|
||||||
b.writeln('\n// V Option_xxx definitions:')
|
b.writeln('\n// V Option_xxx definitions:')
|
||||||
b.writeln(g.options.str())
|
b.write(g.options.str())
|
||||||
b.writeln('\n// V json forward decls:')
|
b.writeln('\n// V json forward decls:')
|
||||||
b.writeln(g.json_forward_decls.str())
|
b.write(g.json_forward_decls.str())
|
||||||
b.writeln('\n// V definitions:')
|
b.writeln('\n// V definitions:')
|
||||||
b.writeln(g.definitions.str())
|
b.write(g.definitions.str())
|
||||||
|
if g.pcs_declarations.len > 0 {
|
||||||
b.writeln('\n// V profile counters:')
|
b.writeln('\n// V profile counters:')
|
||||||
b.writeln(g.pcs_declarations.str())
|
b.write(g.pcs_declarations.str())
|
||||||
|
}
|
||||||
|
interface_table := g.interface_table()
|
||||||
|
if interface_table.len > 0 {
|
||||||
b.writeln('\n// V interface table:')
|
b.writeln('\n// V interface table:')
|
||||||
b.writeln(g.interface_table())
|
b.write(interface_table)
|
||||||
|
}
|
||||||
|
if g.gowrappers.len > 0 {
|
||||||
b.writeln('\n// V gowrappers:')
|
b.writeln('\n// V gowrappers:')
|
||||||
b.writeln(g.gowrappers.str())
|
b.write(g.gowrappers.str())
|
||||||
|
}
|
||||||
|
if g.hotcode_definitions.len > 0 {
|
||||||
b.writeln('\n// V hotcode definitions:')
|
b.writeln('\n// V hotcode definitions:')
|
||||||
b.writeln(g.hotcode_definitions.str())
|
b.write(g.hotcode_definitions.str())
|
||||||
|
}
|
||||||
|
if g.stringliterals.len > 0 {
|
||||||
b.writeln('\n// V stringliterals:')
|
b.writeln('\n// V stringliterals:')
|
||||||
b.writeln(g.stringliterals.str())
|
b.write(g.stringliterals.str())
|
||||||
|
}
|
||||||
|
if g.auto_str_funcs.len > 0 {
|
||||||
b.writeln('\n// V auto str functions:')
|
b.writeln('\n// V auto str functions:')
|
||||||
b.writeln(g.auto_str_funcs.str())
|
b.write(g.auto_str_funcs.str())
|
||||||
|
}
|
||||||
b.writeln('\n// V out')
|
b.writeln('\n// V out')
|
||||||
b.writeln(g.out.str())
|
b.write(g.out.str())
|
||||||
b.writeln('\n// THE END.')
|
b.writeln('\n// THE END.')
|
||||||
return b.str()
|
return b.str()
|
||||||
}
|
}
|
||||||
|
@ -2985,7 +2998,7 @@ fn (mut g Gen) write_init_function() {
|
||||||
}
|
}
|
||||||
g.writeln('\tbuiltin_init();')
|
g.writeln('\tbuiltin_init();')
|
||||||
g.writeln('\tvinit_string_literals();')
|
g.writeln('\tvinit_string_literals();')
|
||||||
g.writeln(g.inits.str())
|
g.write(g.inits.str())
|
||||||
for mod_name in g.table.imports {
|
for mod_name in g.table.imports {
|
||||||
init_fn_name := '${mod_name}.init'
|
init_fn_name := '${mod_name}.init'
|
||||||
if _ := g.table.find_fn(init_fn_name) {
|
if _ := g.table.find_fn(init_fn_name) {
|
||||||
|
|
|
@ -10,14 +10,12 @@ const (
|
||||||
#ifndef V_COMMIT_HASH
|
#ifndef V_COMMIT_HASH
|
||||||
#define V_COMMIT_HASH "@@@"
|
#define V_COMMIT_HASH "@@@"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
'
|
'
|
||||||
// V_CURRENT_COMMIT_HASH is updated, when V is rebuilt inside a git repo.
|
// V_CURRENT_COMMIT_HASH is updated, when V is rebuilt inside a git repo.
|
||||||
c_current_commit_hash_default = '
|
c_current_commit_hash_default = '
|
||||||
#ifndef V_CURRENT_COMMIT_HASH
|
#ifndef V_CURRENT_COMMIT_HASH
|
||||||
#define V_CURRENT_COMMIT_HASH "@@@"
|
#define V_CURRENT_COMMIT_HASH "@@@"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
'
|
'
|
||||||
|
|
||||||
c_common_macros = '
|
c_common_macros = '
|
||||||
|
@ -66,10 +64,8 @@ const (
|
||||||
#define V64_PRINTFORMAT "0x%llx"
|
#define V64_PRINTFORMAT "0x%llx"
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
'
|
'
|
||||||
c_headers = '
|
c_headers = '
|
||||||
|
|
||||||
// c_headers
|
// c_headers
|
||||||
typedef int (*qsort_callback_func)(const void*, const void*);
|
typedef int (*qsort_callback_func)(const void*, const void*);
|
||||||
#include <stdio.h> // TODO remove all these includes, define all function signatures and types manually
|
#include <stdio.h> // TODO remove all these includes, define all function signatures and types manually
|
||||||
|
@ -128,7 +124,6 @@ extern char **environ;
|
||||||
#error Cygwin is not supported, please use MinGW or Visual Studio.
|
#error Cygwin is not supported, please use MinGW or Visual Studio.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h> // os__wait uses wait on nix
|
#include <sys/wait.h> // os__wait uses wait on nix
|
||||||
|
@ -175,8 +170,8 @@ $c_common_macros
|
||||||
#include <io.h> // _waccess
|
#include <io.h> // _waccess
|
||||||
#include <direct.h> // _wgetcwd
|
#include <direct.h> // _wgetcwd
|
||||||
//#include <WinSock2.h>
|
//#include <WinSock2.h>
|
||||||
#ifdef _MSC_VER
|
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
// On MSVC these are the same (as long as /volatile:ms is passed)
|
// On MSVC these are the same (as long as /volatile:ms is passed)
|
||||||
#define _Atomic volatile
|
#define _Atomic volatile
|
||||||
|
|
||||||
|
@ -204,7 +199,6 @@ void AcquireSRWLockExclusive(void*);
|
||||||
void ReleaseSRWLockShared(void*);
|
void ReleaseSRWLockShared(void*);
|
||||||
void ReleaseSRWLockExclusive(void*);
|
void ReleaseSRWLockExclusive(void*);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#ifndef PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
|
#ifndef PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
|
||||||
|
@ -224,7 +218,6 @@ void* g_live_info = NULL;
|
||||||
#define _IN_MAP(val, m) map_exists(m, val)
|
#define _IN_MAP(val, m) map_exists(m, val)
|
||||||
|
|
||||||
// unsigned/signed comparisons
|
// unsigned/signed comparisons
|
||||||
|
|
||||||
static inline bool _us32_gt(uint32_t a, int32_t b) { return a > INT32_MAX || (int32_t)a > b; }
|
static inline bool _us32_gt(uint32_t a, int32_t b) { return a > INT32_MAX || (int32_t)a > b; }
|
||||||
static inline bool _us32_ge(uint32_t a, int32_t b) { return a >= INT32_MAX || (int32_t)a >= b; }
|
static inline bool _us32_ge(uint32_t a, int32_t b) { return a >= INT32_MAX || (int32_t)a >= b; }
|
||||||
static inline bool _us32_eq(uint32_t a, int32_t b) { return a <= INT32_MAX && (int32_t)a == b; }
|
static inline bool _us32_eq(uint32_t a, int32_t b) { return a <= INT32_MAX && (int32_t)a == b; }
|
||||||
|
@ -279,7 +272,6 @@ void _vcleanup();
|
||||||
#define _wyp0 ((uint64_t)0xa0761d6478bd642full)
|
#define _wyp0 ((uint64_t)0xa0761d6478bd642full)
|
||||||
#define _wyp1 ((uint64_t)0xe7037ed1a0b428dbull)
|
#define _wyp1 ((uint64_t)0xe7037ed1a0b428dbull)
|
||||||
|
|
||||||
|
|
||||||
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__) || defined(__TINYC__)
|
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__) || defined(__TINYC__)
|
||||||
#define _likely_(x) __builtin_expect(x, 1)
|
#define _likely_(x) __builtin_expect(x, 1)
|
||||||
#define _unlikely_(x) __builtin_expect((x), 0)
|
#define _unlikely_(x) __builtin_expect((x), 0)
|
||||||
|
@ -373,12 +365,9 @@ static inline double wy2gau(uint64_t r) {
|
||||||
return ((r&0x1fffff)+((r>>21)&0x1fffff)+((r>>42)&0x1fffff))*_wynorm-3.0;
|
return ((r&0x1fffff)+((r>>21)&0x1fffff)+((r>>42)&0x1fffff))*_wynorm-3.0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
'
|
'
|
||||||
c_builtin_types = '
|
c_builtin_types = '
|
||||||
|
|
||||||
//================================== builtin types ================================*/
|
//================================== builtin types ================================*/
|
||||||
|
|
||||||
typedef int64_t i64;
|
typedef int64_t i64;
|
||||||
typedef int16_t i16;
|
typedef int16_t i16;
|
||||||
typedef int8_t i8;
|
typedef int8_t i8;
|
||||||
|
@ -408,6 +397,7 @@ typedef map map_int;
|
||||||
typedef map map_string;
|
typedef map map_string;
|
||||||
typedef byte array_fixed_byte_300 [300];
|
typedef byte array_fixed_byte_300 [300];
|
||||||
typedef byte array_fixed_byte_400 [400];
|
typedef byte array_fixed_byte_400 [400];
|
||||||
|
|
||||||
#ifndef __cplusplus
|
#ifndef __cplusplus
|
||||||
#ifndef bool
|
#ifndef bool
|
||||||
typedef int bool;
|
typedef int bool;
|
||||||
|
@ -415,16 +405,13 @@ typedef byte array_fixed_byte_400 [400];
|
||||||
#define false 0
|
#define false 0
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
'
|
'
|
||||||
bare_c_headers = '
|
bare_c_headers = '
|
||||||
|
|
||||||
$c_common_macros
|
$c_common_macros
|
||||||
|
|
||||||
#ifndef exit
|
#ifndef exit
|
||||||
#define exit(rc) sys_exit(rc)
|
#define exit(rc) sys_exit(rc)
|
||||||
void sys_exit (int);
|
void sys_exit (int);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
'
|
'
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue