run vfmt on cc, cgen, comptime, if_match

pull/3153/head
Alexander Medvednikov 2019-12-19 23:36:18 +03:00
parent ce86626ec2
commit d38687f893
6 changed files with 353 additions and 361 deletions

View File

@ -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. // 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) // []int == []int (also for: i64, f32, f64, byte, string)
/*
fn array_eq<T>(a1, a2 []T) bool { fn array_eq<T>(a1, a2 []T) bool {
if a1.len != a2.len { if a1.len != a2.len {
return false return false
} }
@ -484,3 +486,4 @@ pub fn (a []byte) eq(a2 []byte) bool {
pub fn (a []f32) eq(a2 []f32) bool { pub fn (a []f32) eq(a2 []f32) bool {
return array_eq(a, a2) return array_eq(a, a2)
} }
*/

View File

@ -1,7 +1,6 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module compiler module compiler
import ( import (
@ -11,7 +10,6 @@ import (
) )
fn todo() { fn todo() {
} }
fn (v &V) no_cc_installed() bool { 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') ret := os.system('$vexe -o $vjs_path -os js $vdir/v.v')
if ret == 0 { if ret == 0 {
println('Done.') println('Done.')
} else { }
else {
println('Failed.') println('Failed.')
exit(1) 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 // 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) exit(0)
} }
// Cross compiling for Windows // Cross compiling for Windows
@ -80,20 +80,14 @@ fn (v mut V) cc() {
} }
} }
// arguments for the C compiler // arguments for the C compiler
mut a := [v.pref.cflags, '-std=gnu11', mut a := [v.pref.cflags, '-std=gnu11', '-Wall', '-Wextra',
'-Wall',
'-Wextra',
// TODO : activate -Werror once no warnings remain // TODO : activate -Werror once no warnings remain
// '-Werror', // '-Werror',
// TODO : try and remove the below workaround options when the corresponding // TODO : try and remove the below workaround options when the corresponding
// warnings are totally fixed/removed // warnings are totally fixed/removed
'-Wno-unused-variable', '-Wno-unused-variable',
// '-Wno-unused-but-set-variable', // '-Wno-unused-but-set-variable',
'-Wno-unused-parameter', '-Wno-unused-parameter', '-Wno-unused-result', '-Wno-unused-function', '-Wno-missing-braces', '-Wno-unused-label']
'-Wno-unused-result',
'-Wno-unused-function',
'-Wno-missing-braces',
'-Wno-unused-label']
// TCC on Linux by default, unless -cc was provided // TCC on Linux by default, unless -cc was provided
// TODO if -cc = cc, TCC is still used, default compiler should be // TODO if -cc = cc, TCC is still used, default compiler should be
// used instead. // used instead.
@ -125,7 +119,6 @@ fn (v mut V) cc() {
} }
// linux_host := os.user_os() == 'linux' // linux_host := os.user_os() == 'linux'
v.log('cc() isprod=$v.pref.is_prod outname=$v.out_name') v.log('cc() isprod=$v.pref.is_prod outname=$v.out_name')
if v.pref.is_so { if v.pref.is_so {
a << '-shared -fPIC ' // -Wl,-z,defs' a << '-shared -fPIC ' // -Wl,-z,defs'
v.out_name = v.out_name + '.so' v.out_name = v.out_name + '.so'
@ -135,11 +128,7 @@ fn (v mut V) cc() {
} }
if v.pref.build_mode == .build_module { if v.pref.build_mode == .build_module {
// Create the modules & out directory if it's not there. // Create the modules & out directory if it's not there.
mut out_dir := if v.dir.starts_with('vlib') { 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' }
'$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) pdir := out_dir.all_before_last(os.path_separator)
if !os.is_dir(pdir) { if !os.is_dir(pdir) {
os.mkdir_all(pdir) os.mkdir_all(pdir)
@ -147,18 +136,15 @@ fn (v mut V) cc() {
v.out_name = '${out_dir}.o' // v.out_name v.out_name = '${out_dir}.o' // v.out_name
println('Building ${v.out_name}...') println('Building ${v.out_name}...')
} }
debug_mode := v.pref.is_debug debug_mode := v.pref.is_debug
mut debug_options := '-g' mut debug_options := '-g'
mut optimization_options := '-O2' mut optimization_options := '-O2'
mut guessed_compiler := v.pref.ccompiler mut guessed_compiler := v.pref.ccompiler
if guessed_compiler == 'cc' && v.pref.is_prod { if guessed_compiler == 'cc' && v.pref.is_prod {
// deliberately guessing only for -prod builds for performance reasons // deliberately guessing only for -prod builds for performance reasons
if ccversion:=os.exec('cc --version'){ if ccversion:=os.exec('cc --version'){
if ccversion.exit_code == 0 { if ccversion.exit_code == 0 {
if ccversion.output.contains('This is free software;') if ccversion.output.contains('This is free software;') && ccversion.output.contains('Free Software Foundation, Inc.') {
&& ccversion.output.contains('Free Software Foundation, Inc.') {
guessed_compiler = 'gcc' guessed_compiler = 'gcc'
} }
if ccversion.output.contains('clang version ') { 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 v.pref.ccompiler.contains('clang') || guessed_compiler == 'clang' {
if debug_mode { if debug_mode {
debug_options = '-g -O0 -no-pie' debug_options = '-g -O0 -no-pie'
@ -180,26 +165,21 @@ fn (v mut V) cc() {
} }
optimization_options = '-O3 -fno-strict-aliasing -flto' optimization_options = '-O3 -fno-strict-aliasing -flto'
} }
if debug_mode { if debug_mode {
a << debug_options a << debug_options
} }
if v.pref.is_prod { if v.pref.is_prod {
a << optimization_options a << optimization_options
} }
if debug_mode && os.user_os() != 'windows' { if debug_mode && os.user_os() != 'windows' {
a << ' -rdynamic ' // needed for nicer symbolic backtraces a << ' -rdynamic ' // needed for nicer symbolic backtraces
} }
if v.pref.ccompiler != 'msvc' && v.os != .freebsd { if v.pref.ccompiler != 'msvc' && v.os != .freebsd {
a << '-Werror=implicit-function-declaration' a << '-Werror=implicit-function-declaration'
} }
for f in v.generate_hotcode_reloading_compiler_flags() { for f in v.generate_hotcode_reloading_compiler_flags() {
a << f a << f
} }
mut libs := '' // builtin.o os.o http.o etc mut libs := '' // builtin.o os.o http.o etc
if v.pref.build_mode == .build_module { if v.pref.build_mode == .build_module {
a << '-c' 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 a << builtin_o_path.replace('builtin.o', 'strconv.o') // TODO hack no idea why this is needed
if os.exists(builtin_o_path) { if os.exists(builtin_o_path) {
libs = builtin_o_path libs = builtin_o_path
} else { }
else {
println('$builtin_o_path not found... building module builtin') println('$builtin_o_path not found... building module builtin')
os.system('$vexe build module vlib${os.path_separator}builtin') os.system('$vexe build module vlib${os.path_separator}builtin')
} }
for imp in v.table.imports { for imp in v.table.imports {
if imp.contains('vweb') { continue } // not working if imp.contains('vweb') {
if imp == 'webview' { continue } continue
} // not working
if imp == 'webview' {
continue
}
imp_path := imp.replace('.', os.path_separator) 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' path := '$v_modules_path${os.path_separator}cache${os.path_separator}vlib${os.path_separator}${imp_path}.o'
// println('adding ${imp_path}.o') // println('adding ${imp_path}.o')
if os.exists(path) { if os.exists(path) {
libs += ' ' + path libs += ' ' + path
} else { }
else {
println('$path not found... building module $imp') println('$path not found... building module $imp')
if path.ends_with('vlib/ui.o') { if path.ends_with('vlib/ui.o') {
println('copying ui...') println('copying ui...')
os.cp('$vdir/thirdparty/ui/ui.o', path) or { panic('error copying ui files') } os.cp('$vdir/thirdparty/ui/ui.o', path)or{
os.cp('$vdir/thirdparty/ui/ui.vh', v_modules_path + panic('error copying ui files')
'/vlib/ui.vh') or { panic('error copying ui files') } }
os.cp('$vdir/thirdparty/ui/ui.vh', v_modules_path + '/vlib/ui.vh')or{
} else { panic('error copying ui files')
}
}
else {
os.system('$vexe build module vlib${os.path_separator}$imp_path') os.system('$vexe build module vlib${os.path_separator}$imp_path')
} }
} }
@ -259,7 +247,7 @@ fn (v mut V) cc() {
// Output executable name // Output executable name
a << '-o "$v.out_name"' a << '-o "$v.out_name"'
if os.is_dir(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 // macOS code can include objective C TODO remove once objective C is replaced with C
if v.os == .mac { if v.os == .mac {
@ -275,18 +263,14 @@ fn (v mut V) cc() {
a << '-mmacosx-version-min=10.7' a << '-mmacosx-version-min=10.7'
} }
cflags := v.get_os_cflags() cflags := v.get_os_cflags()
// add .o files // add .o files
a << cflags.c_options_only_object_files() a << cflags.c_options_only_object_files()
// add all flags (-I -l -L etc) not .o files // add all flags (-I -l -L etc) not .o files
a << cflags.c_options_without_object_files() a << cflags.c_options_without_object_files()
a << libs a << libs
// Without these libs compilation will fail on Linux // Without these libs compilation will fail on Linux
// || os.user_os() == 'linux' // || os.user_os() == 'linux'
if !v.pref.is_bare && v.pref.build_mode != .build_module && v.os in [ .linux, .freebsd, if !v.pref.is_bare && v.pref.build_mode != .build_module && v.os in [.linux, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .haiku] {
.openbsd, .netbsd, .dragonfly, .solaris, .haiku ] {
a << '-lm -lpthread ' a << '-lm -lpthread '
// -ldl is a Linux only thing. BSDs have it in libc. // -ldl is a Linux only thing. BSDs have it in libc.
if v.os == .linux { if v.os == .linux {
@ -297,11 +281,9 @@ fn (v mut V) cc() {
a << ' -lexecinfo ' a << ' -lexecinfo '
} }
} }
if !v.pref.is_bare && v.os == .js && os.user_os() == 'linux' { if !v.pref.is_bare && v.os == .js && os.user_os() == 'linux' {
a << '-lm' a << '-lm'
} }
args := a.join(' ') args := a.join(' ')
start: start:
todo() todo()
@ -324,6 +306,7 @@ start:
return return
} }
*/ */
verror(err) verror(err)
return return
} }
@ -337,29 +320,22 @@ start:
goto 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 { if v.pref.is_debug {
println(res.output) println(res.output)
} else { }
else {
partial_output := res.output.limit(200).trim_right('\r\n') partial_output := res.output.limit(200).trim_right('\r\n')
print(partial_output) print(partial_output)
if res.output.len > partial_output.len { if res.output.len > partial_output.len {
println('...\n(Use `v -g` to print the entire error message)\n') println('...\n(Use `v -g` to print the entire error message)\n')
}else{ }
else {
println('') println('')
} }
} }
verror('C error. This should never happen. ' + verror('C error. This should never happen. ' + '\nPlease create a GitHub issue: https://github.com/vlang/v/issues/new/choose')
'\nPlease create a GitHub issue: https://github.com/vlang/v/issues/new/choose')
} }
diff := time.ticks() - ticks diff := time.ticks() - ticks
// Print the C command // Print the C command
@ -389,6 +365,7 @@ start:
println('linux cross compilation done. resulting binary: "$v.out_name"') println('linux cross compilation done. resulting binary: "$v.out_name"')
} }
*/ */
if !v.pref.is_keep_c && v.out_name_c != 'v.c' { if !v.pref.is_keep_c && v.out_name_c != 'v.c' {
os.rm(v.out_name_c) os.rm(v.out_name_c)
} }
@ -409,8 +386,7 @@ start:
println('install upx with `brew install upx`') println('install upx with `brew install upx`')
} }
$if linux { $if linux {
println('install upx\n' + println('install upx\n' + 'for example, on Debian/Ubuntu run `sudo apt install upx`')
'for example, on Debian/Ubuntu run `sudo apt install upx`')
} }
$if windows { $if windows {
// :) // :)
@ -419,7 +395,6 @@ start:
} }
} }
fn (c mut V) cc_windows_cross() { fn (c mut V) cc_windows_cross() {
if !c.out_name.ends_with('.exe') { if !c.out_name.ends_with('.exe') {
c.out_name = c.out_name + '.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. ' mut args := '-o $c.out_name -w -L. '
cflags := c.get_os_cflags() cflags := c.get_os_cflags()
// -I flags // -I flags
args += if c.pref.ccompiler == 'msvc' { args += if c.pref.ccompiler == 'msvc' { cflags.c_options_before_target_msvc() } else { cflags.c_options_before_target() }
cflags.c_options_before_target_msvc()
} else {
cflags.c_options_before_target()
}
mut libs := '' mut libs := ''
if c.pref.build_mode == .default_mode { if c.pref.build_mode == .default_mode {
libs = '"$v_modules_path/vlib/builtin.o"' libs = '"$v_modules_path/vlib/builtin.o"'
@ -444,11 +415,7 @@ fn (c mut V) cc_windows_cross() {
} }
} }
args += ' $c.out_name_c ' args += ' $c.out_name_c '
args += if c.pref.ccompiler == 'msvc' { args += if c.pref.ccompiler == 'msvc' { cflags.c_options_after_target_msvc() } else { cflags.c_options_after_target() }
cflags.c_options_after_target_msvc()
} else {
cflags.c_options_after_target()
}
println('Cross compiling for Windows...') println('Cross compiling for Windows...')
winroot := '$v_modules_path/winroot' winroot := '$v_modules_path/winroot'
if !os.is_dir(winroot) { if !os.is_dir(winroot) {
@ -456,8 +423,7 @@ fn (c mut V) cc_windows_cross() {
println('"$winroot" not found.') println('"$winroot" not found.')
println('Download it from $winroot_url and save it in $v_modules_path') println('Download it from $winroot_url and save it in $v_modules_path')
println('Unzip it afterwards.\n') println('Unzip it afterwards.\n')
println('winroot.zip contains all library and header files needed '+ println('winroot.zip contains all library and header files needed ' + 'to cross-compile for Windows.')
'to cross-compile for Windows.')
exit(1) exit(1)
} }
mut obj_name := c.out_name mut obj_name := c.out_name
@ -473,13 +439,10 @@ fn (c mut V) cc_windows_cross() {
exit(1) exit(1)
} }
if c.pref.build_mode != .build_module { if c.pref.build_mode != .build_module {
link_cmd := 'lld-link $obj_name $winroot/lib/libcmt.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'
'$winroot/lib/libucrt.lib $winroot/lib/kernel32.lib $winroot/lib/libvcruntime.lib ' +
'$winroot/lib/uuid.lib'
if c.pref.show_c_cmd { if c.pref.show_c_cmd {
println(link_cmd) println(link_cmd)
} }
if os.system(link_cmd) != 0 { if os.system(link_cmd) != 0 {
println('Cross compilation for Windows failed. Make sure you have lld linker installed.') println('Cross compilation for Windows failed. Make sure you have lld linker installed.')
exit(1) exit(1)
@ -515,7 +478,9 @@ fn find_c_compiler_default() string {
// return fast_clang // return fast_clang
// } // }
// TODO fix $if after 'string' // TODO fix $if after 'string'
$if windows { return 'gcc' } $if windows {
return 'gcc'
}
return 'cc' return 'cc'
} }
@ -553,3 +518,4 @@ fn missing_compiler_info() string {
} }
return '' return ''
} }

View File

@ -1,7 +1,6 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module compiler module compiler
import os import os
@ -45,12 +44,14 @@ fn new_cgen(out_name_c string) &CGen {
path := out_name_c path := out_name_c
out := os.create(path)or{ out := os.create(path)or{
println('failed to create $path') println('failed to create $path')
return &CGen{} return &CGen{
}
} }
gen := &CGen{ gen := &CGen{
out_path: path out_path: path
out: out out: out
// buf: strings.new_builder(10000) // buf: strings.new_builder(10000)
lines: make(0, 1000, sizeof(string)) lines: make(0, 1000, sizeof(string))
} }
return gen return gen
@ -118,7 +119,6 @@ fn (g mut CGen) save() {
g.out.close() g.out.close()
} }
// returns expression's type, and entire expression's string representation) // returns expression's type, and entire expression's string representation)
fn (p mut Parser) tmp_expr() (string,string) { fn (p mut Parser) tmp_expr() (string,string) {
// former start_tmp() // former start_tmp()
@ -130,12 +130,12 @@ fn (p mut Parser) tmp_expr() (string, string) {
p.cgen.is_tmp = true p.cgen.is_tmp = true
// //
typ := p.bool_expression() typ := p.bool_expression()
res := p.cgen.tmp_line res := p.cgen.tmp_line
if p.cgen.prev_tmps.len > 0 { if p.cgen.prev_tmps.len > 0 {
p.cgen.tmp_line = p.cgen.prev_tmps.last() p.cgen.tmp_line = p.cgen.prev_tmps.last()
p.cgen.prev_tmps = p.cgen.prev_tmps[0..p.cgen.prev_tmps.len - 1] p.cgen.prev_tmps = p.cgen.prev_tmps[0..p.cgen.prev_tmps.len - 1]
} else { }
else {
p.cgen.tmp_line = '' p.cgen.tmp_line = ''
p.cgen.is_tmp = false p.cgen.is_tmp = false
} }
@ -222,6 +222,7 @@ fn (v &V) prof_counters() string {
} }
} }
*/ */
return res.join(';\n') return res.join(';\n')
} }
@ -246,6 +247,7 @@ fn (p &Parser) print_prof_counters() string {
} }
} }
*/ */
return res.join(';\n') return res.join(';\n')
} }
@ -267,7 +269,6 @@ fn (g mut CGen) add_to_main(s string) {
g.fn_main = g.fn_main + s g.fn_main = g.fn_main + s
} }
fn build_thirdparty_obj_file(path string, moduleflags []CFlag) { fn build_thirdparty_obj_file(path string, moduleflags []CFlag) {
obj_path := os.realpath(path) obj_path := os.realpath(path)
if os.exists(obj_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...') println('$obj_path not found, building it...')
parent := os.dir(obj_path) parent := os.dir(obj_path)
files := os.ls(parent) or { panic(err) } files := os.ls(parent)or{
panic(err)
}
mut cfiles := '' mut cfiles := ''
for file in files { for file in files {
if file.ends_with('.c') { 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 { fn os_name_to_ifdef(name string) string {
match name { match name {
'windows' { return '_WIN32' } 'windows' {
'mac' { return '__APPLE__' } return '_WIN32'
'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"') }
} }
'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"') // verror('bad os ifdef name "$name"')
return '' return ''
} }
fn platform_postfix_to_ifdefguard(name string) string { fn platform_postfix_to_ifdefguard(name string) string {
s := match name { s := match name {
'.v' { '' }// no guard needed '.v'{
'_win.v', '_windows.v' { '#ifdef _WIN32' } ''
'_nix.v' { '#ifndef _WIN32' } } // no guard needed
'_lin.v', '_linux.v' { '#ifdef __linux__' } '_win.v','_windows.v'{
'_mac.v', '_darwin.v' { '#ifdef __APPLE__' } '#ifdef _WIN32'
'_bsd.v', '_freebsd.v '{ '#ifdef __FreeBSD__'} }
'_solaris.v' { '#ifdef __sun' } '_nix.v'{
'_haiku.v' { '#ifdef __haiku__' } '#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 { else {
// verror('bad platform_postfix "$name"') // verror('bad platform_postfix "$name"')
// TODO // TODO
'' ''}}
}
}
if s == '' { if s == '' {
verror('bad platform_postfix "$name"') verror('bad platform_postfix "$name"')
} }
@ -366,8 +409,7 @@ fn (v &V) type_definitions() string {
// sort structs // sort structs
types_sorted := sort_structs(types) types_sorted := sort_structs(types)
// Generate C code // Generate C code
res := types_to_c(builtin_types,v.table) + '\n//----\n' + res := types_to_c(builtin_types, v.table) + '\n//----\n' + types_to_c(types_sorted, v.table)
types_to_c(types_sorted, v.table)
return res return res
} }
@ -385,8 +427,7 @@ fn sort_structs(types []Type) []Type {
mut field_deps := []string mut field_deps := []string
for field in t.fields { for field in t.fields {
// Need to handle fixed size arrays as well (`[10]Point`) // Need to handle fixed size arrays as well (`[10]Point`)
ft := if field.typ.starts_with('[') { field.typ.all_after(']') } ft := if field.typ.starts_with('[') { field.typ.all_after(']') } else { field.typ }
else { field.typ }
// skip if not in types list or already in deps // skip if not in types list or already in deps
if !(ft in type_names) || ft in field_deps { if !(ft in type_names) || ft in field_deps {
continue continue
@ -399,10 +440,7 @@ fn sort_structs(types []Type) []Type {
// sort graph // sort graph
dep_graph_sorted := dep_graph.resolve() dep_graph_sorted := dep_graph.resolve()
if !dep_graph_sorted.acyclic { if !dep_graph_sorted.acyclic {
verror('cgen.sort_structs(): the following structs form a dependency cycle:\n' + 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')
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 // sort types
mut types_sorted := []Type mut types_sorted := []Type
@ -442,12 +480,10 @@ fn (v &V) interface_table() string {
if t.gen_types.len > 0 { if t.gen_types.len > 0 {
// methods = '{TCCSKIP(0)}' // methods = '{TCCSKIP(0)}'
// } // }
sb.writeln('void* (* ${t.name}_name_table[][$t.methods.len]) = ' + sb.writeln('void* (* ${t.name}_name_table[][$t.methods.len]) = ' + '{ $methods }; ')
'{ $methods }; ')
} }
continue continue
} }
return sb.str() return sb.str()
} }

View File

@ -1,7 +1,6 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module compiler module compiler
import ( import (
@ -34,10 +33,7 @@ fn (p mut Parser) comp_time() {
} }
p.check(.lcbr) p.check(.lcbr)
os := os_from_string(name) os := os_from_string(name)
if ((!not && os != p.os) || (not && os == p.os)) && if ((!not && os != p.os) || (not && os == p.os)) && !p.scanner.is_fmt && !p.pref.output_cross_c {
!p.scanner.is_fmt &&
!p.pref.output_cross_c
{
// `$if os {` for a different target, skip everything inside // `$if os {` for a different target, skip everything inside
// to avoid compilation errors (like including <windows.h> // to avoid compilation errors (like including <windows.h>
// on non-Windows systems) // on non-Windows systems)
@ -48,7 +44,8 @@ fn (p mut Parser) comp_time() {
} }
if p.tok == .lcbr { if p.tok == .lcbr {
stack++ stack++
} else if p.tok == .rcbr { }
else if p.tok == .rcbr {
stack-- stack--
} }
if p.tok == .eof { if p.tok == .eof {
@ -61,7 +58,8 @@ fn (p mut Parser) comp_time() {
} }
p.next() p.next()
} }
} else { }
else {
p.statements_no_rcbr() p.statements_no_rcbr()
} }
if !(p.tok == .dollar && p.peek() == .key_else) { if !(p.tok == .dollar && p.peek() == .key_else) {
@ -121,7 +119,8 @@ fn (p mut Parser) comp_time() {
else_returns := p.returns else_returns := p.returns
p.returns = if_returns && else_returns p.returns = if_returns && else_returns
// p.gen('/* returns $p.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') 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 // TODO remove after ui_mac.m is removed
else if hash.contains('embed') { else if hash.contains('embed') {
pos := hash.index('embed') or { return } pos := hash.index('embed') or {
return
}
file := hash[pos + 5..] file := hash[pos + 5..]
// if p.pref.build_mode != .default_mode { // if p.pref.build_mode != .default_mode {
p.genln('#include $file') p.genln('#include $file')
@ -262,7 +263,11 @@ fn (p mut Parser) chash() {
$if !js { $if !js {
if !p.can_chash { if !p.can_chash {
println('hash="$hash"') 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)') 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 mut j := 0
for method in typ.methods { for method in typ.methods {
if method.typ != 'void' { if method.typ != 'void' {
continue continue
} }
receiver := method.args[0] receiver := method.args[0]
@ -289,8 +293,7 @@ fn (p mut Parser) comptime_method_call(typ Type) {
if j > 0 { if j > 0 {
p.gen(' else ') p.gen(' else ')
} }
p.genln('if ( string_eq($var, _STR("$method.name")) ) ' + p.genln('if ( string_eq($var, _STR("$method.name")) ) ' + '${typ.name}_$method.name ($amp $p.expr_var.name);')
'${typ.name}_$method.name ($amp $p.expr_var.name);')
j++ j++
} }
p.check(.lpar) p.check(.lpar)
@ -310,7 +313,10 @@ fn (p mut Parser) gen_array_str(typ Type) {
p.add_method(typ.name, Fn{ p.add_method(typ.name, Fn{
name: 'str' name: 'str'
typ: 'string' typ: 'string'
args: [Var{typ: typ.name, is_arg:true}] args: [Var{
typ: typ.name
is_arg: true
}]
is_method: true is_method: true
is_public: true is_public: true
receiver_typ: typ.name receiver_typ: typ.name
@ -320,8 +326,8 @@ fn (p mut Parser) gen_array_str(typ Type) {
is_array := elm_type.starts_with('array_') is_array := elm_type.starts_with('array_')
if is_array { if is_array {
p.gen_array_str(elm_type2) 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.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
} }
p.v.vgen_buf.writeln(' p.v.vgen_buf.writeln('
@ -346,12 +352,14 @@ fn (p mut Parser) gen_struct_str(typ Type) {
p.add_method(typ.name, Fn{ p.add_method(typ.name, Fn{
name: 'str' name: 'str'
typ: 'string' typ: 'string'
args: [Var{typ: typ.name, is_arg:true}] args: [Var{
typ: typ.name
is_arg: true
}]
is_method: true is_method: true
is_public: true is_public: true
receiver_typ: typ.name receiver_typ: typ.name
}) })
mut sb := strings.new_builder(typ.fields.len * 20) mut sb := strings.new_builder(typ.fields.len * 20)
sb.writeln('pub fn (a $typ.name) str() string {\nreturn') sb.writeln('pub fn (a $typ.name) str() string {\nreturn')
sb.writeln("'{") sb.writeln("'{")
@ -373,7 +381,8 @@ fn (p mut Parser) gen_varg_str(typ Type) {
is_array := elm_type.starts_with('array_') is_array := elm_type.starts_with('array_')
if is_array { if is_array {
p.gen_array_str(elm_type2) 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.gen_struct_str(elm_type2)
} }
p.v.vgen_buf.writeln(' 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() tmp_elm := p.get_tmp()
a := p.expr_var.name a := p.expr_var.name
map_type,expr := p.tmp_expr() map_type,expr := p.tmp_expr()
p.cgen.set_placeholder(method_ph,'\narray $tmp = new_array(0, $a .len, ' + p.cgen.set_placeholder(method_ph, '\narray $tmp = new_array(0, $a .len, ' + 'sizeof($map_type));\n')
'sizeof($map_type));\n')
p.genln('for (int i = 0; i < ${a}.len; i++) {') p.genln('for (int i = 0; i < ${a}.len; i++) {')
p.genln('$val_type it = (($val_type*)${a}.data)[i];') p.genln('$val_type it = (($val_type*)${a}.data)[i];')
p.genln('_PUSH(&$tmp, $expr, $tmp_elm, $map_type)') 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) { fn (p mut Parser) gen_enum_flag_methods(typ mut Type) {
for method in ['set', 'clear', 'toggle', 'has'] { for method in ['set', 'clear', 'toggle', 'has'] {
typ.methods << Fn{ typ.methods << Fn{
name: method, name: method
typ: if method == 'has' { 'bool' } else { 'void' } 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_method: true
is_public: true is_public: true
receiver_typ: typ.name 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) 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) 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 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}_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}_clear($typ.name *e, $typ.name flag);'
p.cgen.fns << 'void ${typ.name}_toggle($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);' p.cgen.fns << 'bool ${typ.name}_has($typ.name *e, $typ.name flag);'
} }

View File

@ -1,14 +1,14 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module compiler module compiler
import ( import (
strings strings
) )
// Returns type if used as expression // Returns type if used as expression
fn (p mut Parser) match_statement(is_expr bool) string { fn (p mut Parser) match_statement(is_expr bool) string {
p.check(.key_match) p.check(.key_match)
p.fspace() 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 ??? // is it safe to use p.cgen.insert_before ???
tmp_var := p.get_tmp() tmp_var := p.get_tmp()
p.cgen.insert_before('$typ $tmp_var = $expr;') p.cgen.insert_before('$typ $tmp_var = $expr;')
p.fspace() p.fspace()
p.check(.lcbr) p.check(.lcbr)
mut i := 0 mut i := 0
mut all_cases_return := true mut all_cases_return := true
// stores typ of resulting variable // stores typ of resulting variable
mut res_typ := '' mut res_typ := ''
defer { defer {
@ -36,108 +34,89 @@ fn (p mut Parser) match_statement(is_expr bool) string {
if p.tok == .arrow { if p.tok == .arrow {
p.error(warn_match_arrow) p.error(warn_match_arrow)
} }
// unwrap match if there is only else // unwrap match if there is only else
if i == 0 { if i == 0 {
p.fspace() p.fspace()
if is_expr { if is_expr {
// statements are dissallowed (if match is expression) so user cant declare variables there and so on // statements are dissallowed (if match is expression) so user cant declare variables there and so on
// allow braces is else // allow braces is else
got_brace := p.tok == .lcbr got_brace := p.tok == .lcbr
if got_brace { if got_brace {
p.fspace() p.fspace()
p.check(.lcbr) p.check(.lcbr)
} }
p.gen('( ') p.gen('( ')
res_typ = p.bool_expression() res_typ = p.bool_expression()
p.gen(' )') p.gen(' )')
// allow braces in else // allow braces in else
if got_brace { if got_brace {
p.check(.rcbr) p.check(.rcbr)
} }
return res_typ return res_typ
} else { }
else {
p.returns = false p.returns = false
p.check(.lcbr) p.check(.lcbr)
p.genln('{ ') p.genln('{ ')
p.statements() p.statements()
p.returns = all_cases_return && p.returns p.returns = all_cases_return && p.returns
return '' return ''
} }
} }
if is_expr { if is_expr {
// statements are dissallowed (if match is expression) so // statements are dissallowed (if match is expression) so
// user cant declare variables there and so on // user cant declare variables there and so on
p.gen(':(') p.gen(':(')
// allow braces is else // allow braces is else
got_brace := p.tok == .lcbr got_brace := p.tok == .lcbr
if got_brace { if got_brace {
p.fspace() p.fspace()
p.check(.lcbr) p.check(.lcbr)
} }
p.check_types(p.bool_expression(), res_typ) p.check_types(p.bool_expression(), res_typ)
// allow braces in else // allow braces in else
if got_brace { if got_brace {
p.check(.rcbr) p.check(.rcbr)
} }
p.gen(strings.repeat(`)`, i + 1)) p.gen(strings.repeat(`)`, i + 1))
return res_typ return res_typ
} else { }
else {
p.returns = false p.returns = false
p.genln('else // default:') p.genln('else // default:')
p.fspace() p.fspace()
p.check(.lcbr) p.check(.lcbr)
p.genln('{ ') p.genln('{ ')
p.statements() p.statements()
p.returns = all_cases_return && p.returns p.returns = all_cases_return && p.returns
return '' return ''
} }
} }
if i > 0 { if i > 0 {
if is_expr { if is_expr {
p.gen(': (') p.gen(': (')
} else { }
else {
p.gen('else ') p.gen('else ')
} }
} else if is_expr { }
else if is_expr {
p.gen('(') p.gen('(')
} }
if is_expr { if is_expr {
p.gen('(') p.gen('(')
} else { }
else {
p.gen('if (') p.gen('if (')
} }
ph := p.cgen.add_placeholder() ph := p.cgen.add_placeholder()
// Multiple checks separated by comma // Multiple checks separated by comma
mut got_comma := false mut got_comma := false
for { for {
if got_comma { if got_comma {
p.gen(') || (') p.gen(') || (')
} }
mut got_string := false mut got_string := false
if typ == 'string' { if typ == 'string' {
got_string = true got_string = true
p.gen('string_eq($tmp_var, ') p.gen('string_eq($tmp_var, ')
@ -145,15 +124,12 @@ fn (p mut Parser) match_statement(is_expr bool) string {
else { else {
p.gen('$tmp_var == ') p.gen('$tmp_var == ')
} }
p.expected_type = typ p.expected_type = typ
p.check_types(p.bool_expression(), typ) p.check_types(p.bool_expression(), typ)
p.expected_type = '' p.expected_type = ''
if got_string { if got_string {
p.gen(')') p.gen(')')
} }
if p.tok != .comma { if p.tok != .comma {
if got_comma { if got_comma {
p.gen(') ') p.gen(') ')
@ -165,41 +141,34 @@ fn (p mut Parser) match_statement(is_expr bool) string {
got_comma = true got_comma = true
} }
p.gen(')') p.gen(')')
if p.tok == .arrow { if p.tok == .arrow {
p.error(warn_match_arrow) p.error(warn_match_arrow)
p.check(.arrow) p.check(.arrow)
} }
// statements are dissallowed (if match is expression) so user cant declare variables there and so on // statements are dissallowed (if match is expression) so user cant declare variables there and so on
if is_expr { if is_expr {
p.gen('? (') p.gen('? (')
// braces are required for now // braces are required for now
p.check(.lcbr) p.check(.lcbr)
if i == 0 { if i == 0 {
// on the first iteration we set value of res_typ // on the first iteration we set value of res_typ
res_typ = p.bool_expression() res_typ = p.bool_expression()
} else { }
else {
// later on we check that the value is of res_typ type // later on we check that the value is of res_typ type
p.check_types(p.bool_expression(), res_typ) p.check_types(p.bool_expression(), res_typ)
} }
// braces are required for now // braces are required for now
p.fgen_nl() p.fgen_nl()
p.check(.rcbr) p.check(.rcbr)
p.gen(')') p.gen(')')
} }
else { else {
p.returns = false p.returns = false
p.fspace() p.fspace()
p.check(.lcbr) p.check(.lcbr)
p.genln('{ ') p.genln('{ ')
p.statements() p.statements()
all_cases_return = all_cases_return && p.returns all_cases_return = all_cases_return && p.returns
// p.gen(')') // p.gen(')')
} }
@ -212,8 +181,7 @@ fn (p mut Parser) match_statement(is_expr bool) string {
} }
fn (p mut Parser) switch_statement() { fn (p mut Parser) switch_statement() {
p.error('`switch` statement has been removed, use `match` instead:\n' + p.error('`switch` statement has been removed, use `match` instead:\n' + 'https://vlang.io/docs#match')
'https://vlang.io/docs#match')
} }
fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string { 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 name: var_name
typ: typ typ: typ
is_mut: false // TODO is_mut: false // TODO
is_used: true // TODO is_used: true // TODO
// is_alloc: p.is_alloc || typ.starts_with('array_') // is_alloc: p.is_alloc || typ.starts_with('array_')
// line_nr: p.tokens[ var_token_idx ].line_nr // line_nr: p.tokens[ var_token_idx ].line_nr
// token_idx: var_token_idx // token_idx: var_token_idx
}) })
p.statements() p.statements()
p.close_scope() p.close_scope()
p.returns = false p.returns = false
return 'void' return 'void'
} else { }
else {
p.check_types(p.bool_expression(), 'bool') p.check_types(p.bool_expression(), 'bool')
} }
if is_expr { 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.tok == .key_else {
if p.inside_if_expr { if p.inside_if_expr {
p.fspace() p.fspace()
} else { }
else {
p.fgen_nl() p.fgen_nl()
} }
p.check(.key_else) p.check(.key_else)
@ -348,5 +320,3 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
return typ return typ
} }

View File

@ -250,7 +250,9 @@ fn (p &Parser) gen_fmt() {
if s == '' { if s == '' {
return 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 path := os.tmpdir() + '/' + p.file_name
println('generating ${path}') println('generating ${path}')
mut out := os.create(path) or { mut out := os.create(path) or {