run vfmt on cc, cgen, comptime, if_match
parent
ce86626ec2
commit
d38687f893
|
@ -453,7 +453,9 @@ pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int {
|
|||
|
||||
// array_eq<T> checks if two arrays contain all the same elements in the same order.
|
||||
// []int == []int (also for: i64, f32, f64, byte, string)
|
||||
/*
|
||||
fn array_eq<T>(a1, a2 []T) bool {
|
||||
|
||||
if a1.len != a2.len {
|
||||
return false
|
||||
}
|
||||
|
@ -484,3 +486,4 @@ pub fn (a []byte) eq(a2 []byte) bool {
|
|||
pub fn (a []f32) eq(a2 []f32) bool {
|
||||
return array_eq(a, a2)
|
||||
}
|
||||
*/
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module compiler
|
||||
|
||||
import (
|
||||
|
@ -11,7 +10,6 @@ import (
|
|||
)
|
||||
|
||||
fn todo() {
|
||||
|
||||
}
|
||||
|
||||
fn (v &V) no_cc_installed() bool {
|
||||
|
@ -49,7 +47,8 @@ fn (v mut V) cc() {
|
|||
ret := os.system('$vexe -o $vjs_path -os js $vdir/v.v')
|
||||
if ret == 0 {
|
||||
println('Done.')
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
println('Failed.')
|
||||
exit(1)
|
||||
}
|
||||
|
@ -61,9 +60,10 @@ fn (v mut V) cc() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// v.out_name_c may be on a different partition than v.out_name
|
||||
os.mv_by_cp(v.out_name_c, v.out_name) or { panic(err) }
|
||||
os.mv_by_cp(v.out_name_c, v.out_name)or{
|
||||
panic(err)
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
// Cross compiling for Windows
|
||||
|
@ -80,20 +80,14 @@ fn (v mut V) cc() {
|
|||
}
|
||||
}
|
||||
// arguments for the C compiler
|
||||
mut a := [v.pref.cflags, '-std=gnu11',
|
||||
'-Wall',
|
||||
'-Wextra',
|
||||
mut a := [v.pref.cflags, '-std=gnu11', '-Wall', '-Wextra',
|
||||
// TODO : activate -Werror once no warnings remain
|
||||
// '-Werror',
|
||||
// TODO : try and remove the below workaround options when the corresponding
|
||||
// warnings are totally fixed/removed
|
||||
'-Wno-unused-variable',
|
||||
// '-Wno-unused-but-set-variable',
|
||||
'-Wno-unused-parameter',
|
||||
'-Wno-unused-result',
|
||||
'-Wno-unused-function',
|
||||
'-Wno-missing-braces',
|
||||
'-Wno-unused-label']
|
||||
'-Wno-unused-parameter', '-Wno-unused-result', '-Wno-unused-function', '-Wno-missing-braces', '-Wno-unused-label']
|
||||
// TCC on Linux by default, unless -cc was provided
|
||||
// TODO if -cc = cc, TCC is still used, default compiler should be
|
||||
// used instead.
|
||||
|
@ -125,7 +119,6 @@ fn (v mut V) cc() {
|
|||
}
|
||||
// linux_host := os.user_os() == 'linux'
|
||||
v.log('cc() isprod=$v.pref.is_prod outname=$v.out_name')
|
||||
|
||||
if v.pref.is_so {
|
||||
a << '-shared -fPIC ' // -Wl,-z,defs'
|
||||
v.out_name = v.out_name + '.so'
|
||||
|
@ -135,11 +128,7 @@ fn (v mut V) cc() {
|
|||
}
|
||||
if v.pref.build_mode == .build_module {
|
||||
// Create the modules & out directory if it's not there.
|
||||
mut out_dir := if v.dir.starts_with('vlib') {
|
||||
'$v_modules_path${os.path_separator}cache${os.path_separator}$v.dir'
|
||||
} else {
|
||||
'$v_modules_path${os.path_separator}$v.dir'
|
||||
}
|
||||
mut out_dir := if v.dir.starts_with('vlib') { '$v_modules_path${os.path_separator}cache${os.path_separator}$v.dir' } else { '$v_modules_path${os.path_separator}$v.dir' }
|
||||
pdir := out_dir.all_before_last(os.path_separator)
|
||||
if !os.is_dir(pdir) {
|
||||
os.mkdir_all(pdir)
|
||||
|
@ -147,18 +136,15 @@ fn (v mut V) cc() {
|
|||
v.out_name = '${out_dir}.o' // v.out_name
|
||||
println('Building ${v.out_name}...')
|
||||
}
|
||||
|
||||
debug_mode := v.pref.is_debug
|
||||
mut debug_options := '-g'
|
||||
mut optimization_options := '-O2'
|
||||
|
||||
mut guessed_compiler := v.pref.ccompiler
|
||||
if guessed_compiler == 'cc' && v.pref.is_prod {
|
||||
// deliberately guessing only for -prod builds for performance reasons
|
||||
if ccversion:=os.exec('cc --version'){
|
||||
if ccversion.exit_code == 0 {
|
||||
if ccversion.output.contains('This is free software;')
|
||||
&& ccversion.output.contains('Free Software Foundation, Inc.') {
|
||||
if ccversion.output.contains('This is free software;') && ccversion.output.contains('Free Software Foundation, Inc.') {
|
||||
guessed_compiler = 'gcc'
|
||||
}
|
||||
if ccversion.output.contains('clang version ') {
|
||||
|
@ -167,7 +153,6 @@ fn (v mut V) cc() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if v.pref.ccompiler.contains('clang') || guessed_compiler == 'clang' {
|
||||
if debug_mode {
|
||||
debug_options = '-g -O0 -no-pie'
|
||||
|
@ -180,26 +165,21 @@ fn (v mut V) cc() {
|
|||
}
|
||||
optimization_options = '-O3 -fno-strict-aliasing -flto'
|
||||
}
|
||||
|
||||
if debug_mode {
|
||||
a << debug_options
|
||||
}
|
||||
if v.pref.is_prod {
|
||||
a << optimization_options
|
||||
}
|
||||
|
||||
if debug_mode && os.user_os() != 'windows' {
|
||||
a << ' -rdynamic ' // needed for nicer symbolic backtraces
|
||||
}
|
||||
|
||||
if v.pref.ccompiler != 'msvc' && v.os != .freebsd {
|
||||
a << '-Werror=implicit-function-declaration'
|
||||
}
|
||||
|
||||
for f in v.generate_hotcode_reloading_compiler_flags() {
|
||||
a << f
|
||||
}
|
||||
|
||||
mut libs := '' // builtin.o os.o http.o etc
|
||||
if v.pref.build_mode == .build_module {
|
||||
a << '-c'
|
||||
|
@ -209,28 +189,36 @@ fn (v mut V) cc() {
|
|||
a << builtin_o_path.replace('builtin.o', 'strconv.o') // TODO hack no idea why this is needed
|
||||
if os.exists(builtin_o_path) {
|
||||
libs = builtin_o_path
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
println('$builtin_o_path not found... building module builtin')
|
||||
os.system('$vexe build module vlib${os.path_separator}builtin')
|
||||
}
|
||||
for imp in v.table.imports {
|
||||
if imp.contains('vweb') { continue } // not working
|
||||
if imp == 'webview' { continue }
|
||||
|
||||
if imp.contains('vweb') {
|
||||
continue
|
||||
} // not working
|
||||
if imp == 'webview' {
|
||||
continue
|
||||
}
|
||||
imp_path := imp.replace('.', os.path_separator)
|
||||
path := '$v_modules_path${os.path_separator}cache${os.path_separator}vlib${os.path_separator}${imp_path}.o'
|
||||
// println('adding ${imp_path}.o')
|
||||
if os.exists(path) {
|
||||
libs += ' ' + path
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
println('$path not found... building module $imp')
|
||||
if path.ends_with('vlib/ui.o') {
|
||||
println('copying ui...')
|
||||
os.cp('$vdir/thirdparty/ui/ui.o', path) or { panic('error copying ui files') }
|
||||
os.cp('$vdir/thirdparty/ui/ui.vh', v_modules_path +
|
||||
'/vlib/ui.vh') or { panic('error copying ui files') }
|
||||
|
||||
} else {
|
||||
os.cp('$vdir/thirdparty/ui/ui.o', path)or{
|
||||
panic('error copying ui files')
|
||||
}
|
||||
os.cp('$vdir/thirdparty/ui/ui.vh', v_modules_path + '/vlib/ui.vh')or{
|
||||
panic('error copying ui files')
|
||||
}
|
||||
}
|
||||
else {
|
||||
os.system('$vexe build module vlib${os.path_separator}$imp_path')
|
||||
}
|
||||
}
|
||||
|
@ -259,7 +247,7 @@ fn (v mut V) cc() {
|
|||
// Output executable name
|
||||
a << '-o "$v.out_name"'
|
||||
if os.is_dir(v.out_name) {
|
||||
verror('\'$v.out_name\' is a directory')
|
||||
verror("\'$v.out_name\' is a directory")
|
||||
}
|
||||
// macOS code can include objective C TODO remove once objective C is replaced with C
|
||||
if v.os == .mac {
|
||||
|
@ -275,18 +263,14 @@ fn (v mut V) cc() {
|
|||
a << '-mmacosx-version-min=10.7'
|
||||
}
|
||||
cflags := v.get_os_cflags()
|
||||
|
||||
// add .o files
|
||||
a << cflags.c_options_only_object_files()
|
||||
|
||||
// add all flags (-I -l -L etc) not .o files
|
||||
a << cflags.c_options_without_object_files()
|
||||
|
||||
a << libs
|
||||
// Without these libs compilation will fail on Linux
|
||||
// || os.user_os() == 'linux'
|
||||
if !v.pref.is_bare && v.pref.build_mode != .build_module && v.os in [ .linux, .freebsd,
|
||||
.openbsd, .netbsd, .dragonfly, .solaris, .haiku ] {
|
||||
if !v.pref.is_bare && v.pref.build_mode != .build_module && v.os in [.linux, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .haiku] {
|
||||
a << '-lm -lpthread '
|
||||
// -ldl is a Linux only thing. BSDs have it in libc.
|
||||
if v.os == .linux {
|
||||
|
@ -297,11 +281,9 @@ fn (v mut V) cc() {
|
|||
a << ' -lexecinfo '
|
||||
}
|
||||
}
|
||||
|
||||
if !v.pref.is_bare && v.os == .js && os.user_os() == 'linux' {
|
||||
a << '-lm'
|
||||
}
|
||||
|
||||
args := a.join(' ')
|
||||
start:
|
||||
todo()
|
||||
|
@ -324,6 +306,7 @@ start:
|
|||
return
|
||||
}
|
||||
*/
|
||||
|
||||
verror(err)
|
||||
return
|
||||
}
|
||||
|
@ -337,29 +320,22 @@ start:
|
|||
goto start
|
||||
}
|
||||
}
|
||||
|
||||
verror('C compiler error, while attempting to run: \n' +
|
||||
'-----------------------------------------------------------\n' +
|
||||
'$cmd\n' +
|
||||
'-----------------------------------------------------------\n' +
|
||||
'Probably your C compiler is missing. \n' +
|
||||
'Please reinstall it, or make it available in your PATH.\n\n' +
|
||||
missing_compiler_info())
|
||||
verror('C compiler error, while attempting to run: \n' + '-----------------------------------------------------------\n' + '$cmd\n' + '-----------------------------------------------------------\n' + 'Probably your C compiler is missing. \n' + 'Please reinstall it, or make it available in your PATH.\n\n' + missing_compiler_info())
|
||||
}
|
||||
|
||||
if v.pref.is_debug {
|
||||
println(res.output)
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
partial_output := res.output.limit(200).trim_right('\r\n')
|
||||
print(partial_output)
|
||||
if res.output.len > partial_output.len {
|
||||
println('...\n(Use `v -g` to print the entire error message)\n')
|
||||
}else{
|
||||
}
|
||||
else {
|
||||
println('')
|
||||
}
|
||||
}
|
||||
verror('C error. This should never happen. ' +
|
||||
'\nPlease create a GitHub issue: https://github.com/vlang/v/issues/new/choose')
|
||||
verror('C error. This should never happen. ' + '\nPlease create a GitHub issue: https://github.com/vlang/v/issues/new/choose')
|
||||
}
|
||||
diff := time.ticks() - ticks
|
||||
// Print the C command
|
||||
|
@ -389,6 +365,7 @@ start:
|
|||
println('linux cross compilation done. resulting binary: "$v.out_name"')
|
||||
}
|
||||
*/
|
||||
|
||||
if !v.pref.is_keep_c && v.out_name_c != 'v.c' {
|
||||
os.rm(v.out_name_c)
|
||||
}
|
||||
|
@ -409,8 +386,7 @@ start:
|
|||
println('install upx with `brew install upx`')
|
||||
}
|
||||
$if linux {
|
||||
println('install upx\n' +
|
||||
'for example, on Debian/Ubuntu run `sudo apt install upx`')
|
||||
println('install upx\n' + 'for example, on Debian/Ubuntu run `sudo apt install upx`')
|
||||
}
|
||||
$if windows {
|
||||
// :)
|
||||
|
@ -419,7 +395,6 @@ start:
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
fn (c mut V) cc_windows_cross() {
|
||||
if !c.out_name.ends_with('.exe') {
|
||||
c.out_name = c.out_name + '.exe'
|
||||
|
@ -427,11 +402,7 @@ fn (c mut V) cc_windows_cross() {
|
|||
mut args := '-o $c.out_name -w -L. '
|
||||
cflags := c.get_os_cflags()
|
||||
// -I flags
|
||||
args += if c.pref.ccompiler == 'msvc' {
|
||||
cflags.c_options_before_target_msvc()
|
||||
} else {
|
||||
cflags.c_options_before_target()
|
||||
}
|
||||
args += if c.pref.ccompiler == 'msvc' { cflags.c_options_before_target_msvc() } else { cflags.c_options_before_target() }
|
||||
mut libs := ''
|
||||
if c.pref.build_mode == .default_mode {
|
||||
libs = '"$v_modules_path/vlib/builtin.o"'
|
||||
|
@ -444,11 +415,7 @@ fn (c mut V) cc_windows_cross() {
|
|||
}
|
||||
}
|
||||
args += ' $c.out_name_c '
|
||||
args += if c.pref.ccompiler == 'msvc' {
|
||||
cflags.c_options_after_target_msvc()
|
||||
} else {
|
||||
cflags.c_options_after_target()
|
||||
}
|
||||
args += if c.pref.ccompiler == 'msvc' { cflags.c_options_after_target_msvc() } else { cflags.c_options_after_target() }
|
||||
println('Cross compiling for Windows...')
|
||||
winroot := '$v_modules_path/winroot'
|
||||
if !os.is_dir(winroot) {
|
||||
|
@ -456,8 +423,7 @@ fn (c mut V) cc_windows_cross() {
|
|||
println('"$winroot" not found.')
|
||||
println('Download it from $winroot_url and save it in $v_modules_path')
|
||||
println('Unzip it afterwards.\n')
|
||||
println('winroot.zip contains all library and header files needed '+
|
||||
'to cross-compile for Windows.')
|
||||
println('winroot.zip contains all library and header files needed ' + 'to cross-compile for Windows.')
|
||||
exit(1)
|
||||
}
|
||||
mut obj_name := c.out_name
|
||||
|
@ -473,13 +439,10 @@ fn (c mut V) cc_windows_cross() {
|
|||
exit(1)
|
||||
}
|
||||
if c.pref.build_mode != .build_module {
|
||||
link_cmd := 'lld-link $obj_name $winroot/lib/libcmt.lib ' +
|
||||
'$winroot/lib/libucrt.lib $winroot/lib/kernel32.lib $winroot/lib/libvcruntime.lib ' +
|
||||
'$winroot/lib/uuid.lib'
|
||||
link_cmd := 'lld-link $obj_name $winroot/lib/libcmt.lib ' + '$winroot/lib/libucrt.lib $winroot/lib/kernel32.lib $winroot/lib/libvcruntime.lib ' + '$winroot/lib/uuid.lib'
|
||||
if c.pref.show_c_cmd {
|
||||
println(link_cmd)
|
||||
}
|
||||
|
||||
if os.system(link_cmd) != 0 {
|
||||
println('Cross compilation for Windows failed. Make sure you have lld linker installed.')
|
||||
exit(1)
|
||||
|
@ -515,7 +478,9 @@ fn find_c_compiler_default() string {
|
|||
// return fast_clang
|
||||
// }
|
||||
// TODO fix $if after 'string'
|
||||
$if windows { return 'gcc' }
|
||||
$if windows {
|
||||
return 'gcc'
|
||||
}
|
||||
return 'cc'
|
||||
}
|
||||
|
||||
|
@ -553,3 +518,4 @@ fn missing_compiler_info() string {
|
|||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module compiler
|
||||
|
||||
import os
|
||||
|
@ -45,12 +44,14 @@ fn new_cgen(out_name_c string) &CGen {
|
|||
path := out_name_c
|
||||
out := os.create(path)or{
|
||||
println('failed to create $path')
|
||||
return &CGen{}
|
||||
return &CGen{
|
||||
}
|
||||
}
|
||||
gen := &CGen{
|
||||
out_path: path
|
||||
out: out
|
||||
// buf: strings.new_builder(10000)
|
||||
|
||||
lines: make(0, 1000, sizeof(string))
|
||||
}
|
||||
return gen
|
||||
|
@ -118,7 +119,6 @@ fn (g mut CGen) save() {
|
|||
g.out.close()
|
||||
}
|
||||
|
||||
|
||||
// returns expression's type, and entire expression's string representation)
|
||||
fn (p mut Parser) tmp_expr() (string,string) {
|
||||
// former start_tmp()
|
||||
|
@ -130,12 +130,12 @@ fn (p mut Parser) tmp_expr() (string, string) {
|
|||
p.cgen.is_tmp = true
|
||||
//
|
||||
typ := p.bool_expression()
|
||||
|
||||
res := p.cgen.tmp_line
|
||||
if p.cgen.prev_tmps.len > 0 {
|
||||
p.cgen.tmp_line = p.cgen.prev_tmps.last()
|
||||
p.cgen.prev_tmps = p.cgen.prev_tmps[0..p.cgen.prev_tmps.len - 1]
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p.cgen.tmp_line = ''
|
||||
p.cgen.is_tmp = false
|
||||
}
|
||||
|
@ -222,6 +222,7 @@ fn (v &V) prof_counters() string {
|
|||
}
|
||||
}
|
||||
*/
|
||||
|
||||
return res.join(';\n')
|
||||
}
|
||||
|
||||
|
@ -246,6 +247,7 @@ fn (p &Parser) print_prof_counters() string {
|
|||
}
|
||||
}
|
||||
*/
|
||||
|
||||
return res.join(';\n')
|
||||
}
|
||||
|
||||
|
@ -267,7 +269,6 @@ fn (g mut CGen) add_to_main(s string) {
|
|||
g.fn_main = g.fn_main + s
|
||||
}
|
||||
|
||||
|
||||
fn build_thirdparty_obj_file(path string, moduleflags []CFlag) {
|
||||
obj_path := os.realpath(path)
|
||||
if os.exists(obj_path) {
|
||||
|
@ -275,7 +276,9 @@ fn build_thirdparty_obj_file(path string, moduleflags []CFlag) {
|
|||
}
|
||||
println('$obj_path not found, building it...')
|
||||
parent := os.dir(obj_path)
|
||||
files := os.ls(parent) or { panic(err) }
|
||||
files := os.ls(parent)or{
|
||||
panic(err)
|
||||
}
|
||||
mut cfiles := ''
|
||||
for file in files {
|
||||
if file.ends_with('.c') {
|
||||
|
@ -302,42 +305,82 @@ fn build_thirdparty_obj_file(path string, moduleflags []CFlag) {
|
|||
|
||||
fn os_name_to_ifdef(name string) string {
|
||||
match name {
|
||||
'windows' { return '_WIN32' }
|
||||
'mac' { return '__APPLE__' }
|
||||
'macos' { return '__APPLE__' }
|
||||
'linux' { return '__linux__' }
|
||||
'freebsd' { return '__FreeBSD__' }
|
||||
'openbsd'{ return '__OpenBSD__' }
|
||||
'netbsd'{ return '__NetBSD__' }
|
||||
'dragonfly'{ return '__DragonFly__' }
|
||||
'msvc'{ return '_MSC_VER' }
|
||||
'android'{ return '__BIONIC__' }
|
||||
'js' {return '_VJS' }
|
||||
'solaris'{ return '__sun' }
|
||||
'haiku' { return '__haiku__' }
|
||||
else { verror('bad os ifdef name "$name"') }
|
||||
'windows' {
|
||||
return '_WIN32'
|
||||
}
|
||||
'mac' {
|
||||
return '__APPLE__'
|
||||
}
|
||||
'macos' {
|
||||
return '__APPLE__'
|
||||
}
|
||||
'linux' {
|
||||
return '__linux__'
|
||||
}
|
||||
'freebsd' {
|
||||
return '__FreeBSD__'
|
||||
}
|
||||
'openbsd' {
|
||||
return '__OpenBSD__'
|
||||
}
|
||||
'netbsd' {
|
||||
return '__NetBSD__'
|
||||
}
|
||||
'dragonfly' {
|
||||
return '__DragonFly__'
|
||||
}
|
||||
'msvc' {
|
||||
return '_MSC_VER'
|
||||
}
|
||||
'android' {
|
||||
return '__BIONIC__'
|
||||
}
|
||||
'js' {
|
||||
return '_VJS'
|
||||
}
|
||||
'solaris' {
|
||||
return '__sun'
|
||||
}
|
||||
'haiku' {
|
||||
return '__haiku__'
|
||||
}
|
||||
else {
|
||||
verror('bad os ifdef name "$name"')
|
||||
}}
|
||||
// verror('bad os ifdef name "$name"')
|
||||
return ''
|
||||
}
|
||||
|
||||
fn platform_postfix_to_ifdefguard(name string) string {
|
||||
s := match name {
|
||||
'.v' { '' }// no guard needed
|
||||
'_win.v', '_windows.v' { '#ifdef _WIN32' }
|
||||
'_nix.v' { '#ifndef _WIN32' }
|
||||
'_lin.v', '_linux.v' { '#ifdef __linux__' }
|
||||
'_mac.v', '_darwin.v' { '#ifdef __APPLE__' }
|
||||
'_bsd.v', '_freebsd.v '{ '#ifdef __FreeBSD__'}
|
||||
'_solaris.v' { '#ifdef __sun' }
|
||||
'_haiku.v' { '#ifdef __haiku__' }
|
||||
'.v'{
|
||||
''
|
||||
} // no guard needed
|
||||
'_win.v','_windows.v'{
|
||||
'#ifdef _WIN32'
|
||||
}
|
||||
'_nix.v'{
|
||||
'#ifndef _WIN32'
|
||||
}
|
||||
'_lin.v','_linux.v'{
|
||||
'#ifdef __linux__'
|
||||
}
|
||||
'_mac.v','_darwin.v'{
|
||||
'#ifdef __APPLE__'
|
||||
}
|
||||
'_bsd.v','_freebsd.v '{
|
||||
'#ifdef __FreeBSD__'
|
||||
}
|
||||
'_solaris.v'{
|
||||
'#ifdef __sun'
|
||||
}
|
||||
'_haiku.v'{
|
||||
'#ifdef __haiku__'
|
||||
}
|
||||
else {
|
||||
|
||||
// verror('bad platform_postfix "$name"')
|
||||
// TODO
|
||||
''
|
||||
}
|
||||
}
|
||||
''}}
|
||||
if s == '' {
|
||||
verror('bad platform_postfix "$name"')
|
||||
}
|
||||
|
@ -366,8 +409,7 @@ fn (v &V) type_definitions() string {
|
|||
// sort structs
|
||||
types_sorted := sort_structs(types)
|
||||
// Generate C code
|
||||
res := types_to_c(builtin_types,v.table) + '\n//----\n' +
|
||||
types_to_c(types_sorted, v.table)
|
||||
res := types_to_c(builtin_types, v.table) + '\n//----\n' + types_to_c(types_sorted, v.table)
|
||||
return res
|
||||
}
|
||||
|
||||
|
@ -385,8 +427,7 @@ fn sort_structs(types []Type) []Type {
|
|||
mut field_deps := []string
|
||||
for field in t.fields {
|
||||
// Need to handle fixed size arrays as well (`[10]Point`)
|
||||
ft := if field.typ.starts_with('[') { field.typ.all_after(']') }
|
||||
else { field.typ }
|
||||
ft := if field.typ.starts_with('[') { field.typ.all_after(']') } else { field.typ }
|
||||
// skip if not in types list or already in deps
|
||||
if !(ft in type_names) || ft in field_deps {
|
||||
continue
|
||||
|
@ -399,10 +440,7 @@ fn sort_structs(types []Type) []Type {
|
|||
// sort graph
|
||||
dep_graph_sorted := dep_graph.resolve()
|
||||
if !dep_graph_sorted.acyclic {
|
||||
verror('cgen.sort_structs(): the following structs form a dependency cycle:\n' +
|
||||
dep_graph_sorted.display_cycles() +
|
||||
'\nyou can solve this by making one or both of the dependant struct fields references, eg: field &MyStruct' +
|
||||
'\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
|
||||
verror('cgen.sort_structs(): the following structs form a dependency cycle:\n' + dep_graph_sorted.display_cycles() + '\nyou can solve this by making one or both of the dependant struct fields references, eg: field &MyStruct' + '\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
|
||||
}
|
||||
// sort types
|
||||
mut types_sorted := []Type
|
||||
|
@ -442,12 +480,10 @@ fn (v &V) interface_table() string {
|
|||
if t.gen_types.len > 0 {
|
||||
// methods = '{TCCSKIP(0)}'
|
||||
// }
|
||||
sb.writeln('void* (* ${t.name}_name_table[][$t.methods.len]) = ' +
|
||||
'{ $methods }; ')
|
||||
sb.writeln('void* (* ${t.name}_name_table[][$t.methods.len]) = ' + '{ $methods }; ')
|
||||
}
|
||||
continue
|
||||
}
|
||||
return sb.str()
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module compiler
|
||||
|
||||
import (
|
||||
|
@ -34,10 +33,7 @@ fn (p mut Parser) comp_time() {
|
|||
}
|
||||
p.check(.lcbr)
|
||||
os := os_from_string(name)
|
||||
if ((!not && os != p.os) || (not && os == p.os)) &&
|
||||
!p.scanner.is_fmt &&
|
||||
!p.pref.output_cross_c
|
||||
{
|
||||
if ((!not && os != p.os) || (not && os == p.os)) && !p.scanner.is_fmt && !p.pref.output_cross_c {
|
||||
// `$if os {` for a different target, skip everything inside
|
||||
// to avoid compilation errors (like including <windows.h>
|
||||
// on non-Windows systems)
|
||||
|
@ -48,7 +44,8 @@ fn (p mut Parser) comp_time() {
|
|||
}
|
||||
if p.tok == .lcbr {
|
||||
stack++
|
||||
} else if p.tok == .rcbr {
|
||||
}
|
||||
else if p.tok == .rcbr {
|
||||
stack--
|
||||
}
|
||||
if p.tok == .eof {
|
||||
|
@ -61,7 +58,8 @@ fn (p mut Parser) comp_time() {
|
|||
}
|
||||
p.next()
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p.statements_no_rcbr()
|
||||
}
|
||||
if !(p.tok == .dollar && p.peek() == .key_else) {
|
||||
|
@ -121,7 +119,8 @@ fn (p mut Parser) comp_time() {
|
|||
else_returns := p.returns
|
||||
p.returns = if_returns && else_returns
|
||||
// p.gen('/* returns $p.returns */')
|
||||
} else if p.tok == .key_else {
|
||||
}
|
||||
else if p.tok == .key_else {
|
||||
p.error('use `$' + 'else` instead of `else` in comptime if statements')
|
||||
}
|
||||
}
|
||||
|
@ -236,7 +235,9 @@ fn (p mut Parser) chash() {
|
|||
}
|
||||
// TODO remove after ui_mac.m is removed
|
||||
else if hash.contains('embed') {
|
||||
pos := hash.index('embed') or { return }
|
||||
pos := hash.index('embed') or {
|
||||
return
|
||||
}
|
||||
file := hash[pos + 5..]
|
||||
// if p.pref.build_mode != .default_mode {
|
||||
p.genln('#include $file')
|
||||
|
@ -262,7 +263,11 @@ fn (p mut Parser) chash() {
|
|||
$if !js {
|
||||
if !p.can_chash {
|
||||
println('hash="$hash"')
|
||||
if hash.starts_with('include') { println("include") } else {}
|
||||
if hash.starts_with('include') {
|
||||
println('include')
|
||||
}
|
||||
else {
|
||||
}
|
||||
p.error('bad token `#` (embedding C code is no longer supported)')
|
||||
}
|
||||
}
|
||||
|
@ -278,7 +283,6 @@ fn (p mut Parser) comptime_method_call(typ Type) {
|
|||
mut j := 0
|
||||
for method in typ.methods {
|
||||
if method.typ != 'void' {
|
||||
|
||||
continue
|
||||
}
|
||||
receiver := method.args[0]
|
||||
|
@ -289,8 +293,7 @@ fn (p mut Parser) comptime_method_call(typ Type) {
|
|||
if j > 0 {
|
||||
p.gen(' else ')
|
||||
}
|
||||
p.genln('if ( string_eq($var, _STR("$method.name")) ) ' +
|
||||
'${typ.name}_$method.name ($amp $p.expr_var.name);')
|
||||
p.genln('if ( string_eq($var, _STR("$method.name")) ) ' + '${typ.name}_$method.name ($amp $p.expr_var.name);')
|
||||
j++
|
||||
}
|
||||
p.check(.lpar)
|
||||
|
@ -310,7 +313,10 @@ fn (p mut Parser) gen_array_str(typ Type) {
|
|||
p.add_method(typ.name, Fn{
|
||||
name: 'str'
|
||||
typ: 'string'
|
||||
args: [Var{typ: typ.name, is_arg:true}]
|
||||
args: [Var{
|
||||
typ: typ.name
|
||||
is_arg: true
|
||||
}]
|
||||
is_method: true
|
||||
is_public: true
|
||||
receiver_typ: typ.name
|
||||
|
@ -320,8 +326,8 @@ fn (p mut Parser) gen_array_str(typ Type) {
|
|||
is_array := elm_type.starts_with('array_')
|
||||
if is_array {
|
||||
p.gen_array_str(elm_type2)
|
||||
} else if p.typ_to_fmt(elm_type, 0) == '' &&
|
||||
!p.table.type_has_method(elm_type2, 'str') {
|
||||
}
|
||||
else if p.typ_to_fmt(elm_type, 0) == '' && !p.table.type_has_method(elm_type2, 'str') {
|
||||
p.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
|
||||
}
|
||||
p.v.vgen_buf.writeln('
|
||||
|
@ -346,12 +352,14 @@ fn (p mut Parser) gen_struct_str(typ Type) {
|
|||
p.add_method(typ.name, Fn{
|
||||
name: 'str'
|
||||
typ: 'string'
|
||||
args: [Var{typ: typ.name, is_arg:true}]
|
||||
args: [Var{
|
||||
typ: typ.name
|
||||
is_arg: true
|
||||
}]
|
||||
is_method: true
|
||||
is_public: true
|
||||
receiver_typ: typ.name
|
||||
})
|
||||
|
||||
mut sb := strings.new_builder(typ.fields.len * 20)
|
||||
sb.writeln('pub fn (a $typ.name) str() string {\nreturn')
|
||||
sb.writeln("'{")
|
||||
|
@ -373,7 +381,8 @@ fn (p mut Parser) gen_varg_str(typ Type) {
|
|||
is_array := elm_type.starts_with('array_')
|
||||
if is_array {
|
||||
p.gen_array_str(elm_type2)
|
||||
} else if elm_type2.cat == .struct_ {
|
||||
}
|
||||
else if elm_type2.cat == .struct_ {
|
||||
p.gen_struct_str(elm_type2)
|
||||
}
|
||||
p.v.vgen_buf.writeln('
|
||||
|
@ -460,8 +469,7 @@ fn (p mut Parser) gen_array_map(str_typ string, method_ph int) string {
|
|||
tmp_elm := p.get_tmp()
|
||||
a := p.expr_var.name
|
||||
map_type,expr := p.tmp_expr()
|
||||
p.cgen.set_placeholder(method_ph,'\narray $tmp = new_array(0, $a .len, ' +
|
||||
'sizeof($map_type));\n')
|
||||
p.cgen.set_placeholder(method_ph, '\narray $tmp = new_array(0, $a .len, ' + 'sizeof($map_type));\n')
|
||||
p.genln('for (int i = 0; i < ${a}.len; i++) {')
|
||||
p.genln('$val_type it = (($val_type*)${a}.data)[i];')
|
||||
p.genln('_PUSH(&$tmp, $expr, $tmp_elm, $map_type)')
|
||||
|
@ -484,9 +492,16 @@ fn (p mut Parser) comptime_if_block(name string) {
|
|||
fn (p mut Parser) gen_enum_flag_methods(typ mut Type) {
|
||||
for method in ['set', 'clear', 'toggle', 'has'] {
|
||||
typ.methods << Fn{
|
||||
name: method,
|
||||
name: method
|
||||
typ: if method == 'has' { 'bool' } else { 'void' }
|
||||
args: [Var{typ: typ.name, is_mut: true, is_arg:true}, Var{typ: typ.name, is_arg: true}]
|
||||
args: [Var{
|
||||
typ: typ.name
|
||||
is_mut: true
|
||||
is_arg: true
|
||||
}, Var{
|
||||
typ: typ.name
|
||||
is_arg: true
|
||||
}]
|
||||
is_method: true
|
||||
is_public: true
|
||||
receiver_typ: typ.name
|
||||
|
@ -496,10 +511,10 @@ fn (p mut Parser) gen_enum_flag_methods(typ mut Type) {
|
|||
pub fn (e mut $typ.name) set(flag $typ.name) { *e = int(*e) | (1 << int(flag)) }
|
||||
pub fn (e mut $typ.name) clear(flag $typ.name) { *e = int(*e) &~ (1 << int(flag)) }
|
||||
pub fn (e mut $typ.name) toggle(flag $typ.name) { *e = int(*e) ^ (1 << int(flag)) }
|
||||
pub fn (e &$typ.name) has(flag $typ.name) bool { return int(*e)&(1 << int(flag)) != 0 }'
|
||||
)
|
||||
pub fn (e &$typ.name) has(flag $typ.name) bool { return int(*e)&(1 << int(flag)) != 0 }')
|
||||
p.cgen.fns << 'void ${typ.name}_set($typ.name *e, $typ.name flag);'
|
||||
p.cgen.fns << 'void ${typ.name}_clear($typ.name *e, $typ.name flag);'
|
||||
p.cgen.fns << 'void ${typ.name}_toggle($typ.name *e, $typ.name flag);'
|
||||
p.cgen.fns << 'bool ${typ.name}_has($typ.name *e, $typ.name flag);'
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
module compiler
|
||||
|
||||
import (
|
||||
strings
|
||||
)
|
||||
|
||||
// Returns type if used as expression
|
||||
|
||||
|
||||
fn (p mut Parser) match_statement(is_expr bool) string {
|
||||
p.check(.key_match)
|
||||
p.fspace()
|
||||
|
@ -19,12 +19,10 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
|||
// is it safe to use p.cgen.insert_before ???
|
||||
tmp_var := p.get_tmp()
|
||||
p.cgen.insert_before('$typ $tmp_var = $expr;')
|
||||
|
||||
p.fspace()
|
||||
p.check(.lcbr)
|
||||
mut i := 0
|
||||
mut all_cases_return := true
|
||||
|
||||
// stores typ of resulting variable
|
||||
mut res_typ := ''
|
||||
defer {
|
||||
|
@ -36,108 +34,89 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
|||
if p.tok == .arrow {
|
||||
p.error(warn_match_arrow)
|
||||
}
|
||||
|
||||
// unwrap match if there is only else
|
||||
if i == 0 {
|
||||
p.fspace()
|
||||
if is_expr {
|
||||
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
||||
|
||||
// allow braces is else
|
||||
got_brace := p.tok == .lcbr
|
||||
if got_brace {
|
||||
p.fspace()
|
||||
p.check(.lcbr)
|
||||
}
|
||||
|
||||
p.gen('( ')
|
||||
|
||||
res_typ = p.bool_expression()
|
||||
|
||||
p.gen(' )')
|
||||
|
||||
// allow braces in else
|
||||
if got_brace {
|
||||
p.check(.rcbr)
|
||||
}
|
||||
|
||||
return res_typ
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p.returns = false
|
||||
p.check(.lcbr)
|
||||
|
||||
p.genln('{ ')
|
||||
p.statements()
|
||||
p.returns = all_cases_return && p.returns
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
if is_expr {
|
||||
// statements are dissallowed (if match is expression) so
|
||||
// user cant declare variables there and so on
|
||||
p.gen(':(')
|
||||
|
||||
// allow braces is else
|
||||
got_brace := p.tok == .lcbr
|
||||
if got_brace {
|
||||
p.fspace()
|
||||
p.check(.lcbr)
|
||||
}
|
||||
|
||||
p.check_types(p.bool_expression(), res_typ)
|
||||
|
||||
// allow braces in else
|
||||
if got_brace {
|
||||
p.check(.rcbr)
|
||||
}
|
||||
|
||||
p.gen(strings.repeat(`)`, i + 1))
|
||||
|
||||
return res_typ
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p.returns = false
|
||||
p.genln('else // default:')
|
||||
|
||||
p.fspace()
|
||||
p.check(.lcbr)
|
||||
|
||||
p.genln('{ ')
|
||||
p.statements()
|
||||
|
||||
p.returns = all_cases_return && p.returns
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
if i > 0 {
|
||||
if is_expr {
|
||||
p.gen(': (')
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p.gen('else ')
|
||||
}
|
||||
} else if is_expr {
|
||||
}
|
||||
else if is_expr {
|
||||
p.gen('(')
|
||||
}
|
||||
|
||||
if is_expr {
|
||||
p.gen('(')
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p.gen('if (')
|
||||
}
|
||||
|
||||
ph := p.cgen.add_placeholder()
|
||||
|
||||
// Multiple checks separated by comma
|
||||
mut got_comma := false
|
||||
|
||||
for {
|
||||
if got_comma {
|
||||
p.gen(') || (')
|
||||
}
|
||||
|
||||
mut got_string := false
|
||||
|
||||
if typ == 'string' {
|
||||
got_string = true
|
||||
p.gen('string_eq($tmp_var, ')
|
||||
|
@ -145,15 +124,12 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
|||
else {
|
||||
p.gen('$tmp_var == ')
|
||||
}
|
||||
|
||||
p.expected_type = typ
|
||||
p.check_types(p.bool_expression(), typ)
|
||||
p.expected_type = ''
|
||||
|
||||
if got_string {
|
||||
p.gen(')')
|
||||
}
|
||||
|
||||
if p.tok != .comma {
|
||||
if got_comma {
|
||||
p.gen(') ')
|
||||
|
@ -165,41 +141,34 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
|||
got_comma = true
|
||||
}
|
||||
p.gen(')')
|
||||
|
||||
if p.tok == .arrow {
|
||||
p.error(warn_match_arrow)
|
||||
p.check(.arrow)
|
||||
}
|
||||
|
||||
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
||||
if is_expr {
|
||||
p.gen('? (')
|
||||
|
||||
// braces are required for now
|
||||
p.check(.lcbr)
|
||||
|
||||
if i == 0 {
|
||||
// on the first iteration we set value of res_typ
|
||||
res_typ = p.bool_expression()
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
// later on we check that the value is of res_typ type
|
||||
p.check_types(p.bool_expression(), res_typ)
|
||||
}
|
||||
|
||||
// braces are required for now
|
||||
p.fgen_nl()
|
||||
p.check(.rcbr)
|
||||
|
||||
p.gen(')')
|
||||
}
|
||||
else {
|
||||
p.returns = false
|
||||
p.fspace()
|
||||
p.check(.lcbr)
|
||||
|
||||
p.genln('{ ')
|
||||
p.statements()
|
||||
|
||||
all_cases_return = all_cases_return && p.returns
|
||||
// p.gen(')')
|
||||
}
|
||||
|
@ -212,8 +181,7 @@ fn (p mut Parser) match_statement(is_expr bool) string {
|
|||
}
|
||||
|
||||
fn (p mut Parser) switch_statement() {
|
||||
p.error('`switch` statement has been removed, use `match` instead:\n' +
|
||||
'https://vlang.io/docs#match')
|
||||
p.error('`switch` statement has been removed, use `match` instead:\n' + 'https://vlang.io/docs#match')
|
||||
}
|
||||
|
||||
fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
|
||||
|
@ -260,16 +228,19 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
|
|||
name: var_name
|
||||
typ: typ
|
||||
is_mut: false // TODO
|
||||
|
||||
is_used: true // TODO
|
||||
// is_alloc: p.is_alloc || typ.starts_with('array_')
|
||||
// line_nr: p.tokens[ var_token_idx ].line_nr
|
||||
// token_idx: var_token_idx
|
||||
|
||||
})
|
||||
p.statements()
|
||||
p.close_scope()
|
||||
p.returns = false
|
||||
return 'void'
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p.check_types(p.bool_expression(), 'bool')
|
||||
}
|
||||
if is_expr {
|
||||
|
@ -297,7 +268,8 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
|
|||
if p.tok == .key_else {
|
||||
if p.inside_if_expr {
|
||||
p.fspace()
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
p.fgen_nl()
|
||||
}
|
||||
p.check(.key_else)
|
||||
|
@ -348,5 +320,3 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
|
|||
return typ
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -250,7 +250,9 @@ fn (p &Parser) gen_fmt() {
|
|||
if s == '' {
|
||||
return
|
||||
}
|
||||
if !p.file_path.contains('table.v') {return}
|
||||
files := ['cgen.v', 'comptime.v', 'cc.v', 'if_match.v']
|
||||
//if !p.file_path.contains('table.v') {return}
|
||||
if !(p.file_name in files) { return }
|
||||
path := os.tmpdir() + '/' + p.file_name
|
||||
println('generating ${path}')
|
||||
mut out := os.create(path) or {
|
||||
|
|
Loading…
Reference in New Issue