v/vlib/compiler/gen_c.v

724 lines
19 KiB
V
Raw Normal View History

2019-12-11 18:43:13 +01:00
// 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
2019-09-14 22:48:30 +02:00
import strings
const (
dot_ptr = '->'
)
// returns the type of the new variable
fn (p mut Parser) gen_var_decl(name string, is_static bool) string {
2019-11-04 00:38:49 +01:00
p.is_var_decl = true
2019-09-14 22:48:30 +02:00
mut typ := p.bool_expression()
2019-12-19 21:52:45 +01:00
// mut typ, expr := p.tmp_expr()
2019-11-04 00:38:49 +01:00
p.is_var_decl = false
2019-12-19 21:52:45 +01:00
if typ.starts_with('...') {
typ = typ[3..]
}
// p.gen('/*after expr*/')
2019-09-14 22:48:30 +02:00
// Option check ? or {
or_else := p.tok == .key_orelse
if or_else {
2019-12-11 18:43:13 +01:00
return p.gen_handle_option_or_else(typ, name, 0)
2019-09-14 22:48:30 +02:00
}
gen_name := p.table.var_cgen_name(name)
mut nt_gen := p.table.cgen_name_type_pair(gen_name, typ)
// `foo := C.Foo{}` => `Foo foo;`
2019-12-19 21:52:45 +01:00
if !p.is_empty_c_struct_init && !typ.starts_with('[') {
2019-09-14 22:48:30 +02:00
nt_gen += '='
2019-12-19 21:52:45 +01:00
}
else if typ.starts_with('[') && typ[typ.len - 1] != `*` {
// a fixed_array initializer, like `v := [1.1, 2.2]!!`
// ... should translate to the following in C `f32 v[2] = {1.1, 2.2};`
2019-12-11 18:43:13 +01:00
initializer := p.cgen.cur_line
if initializer.len > 0 {
2019-12-19 21:52:45 +01:00
p.cgen.resetln(' = {' + initializer.all_after('{'))
}
else if initializer.len == 0 {
p.cgen.resetln(' = { 0 }')
}
2019-09-14 22:48:30 +02:00
}
if is_static {
nt_gen = 'static $nt_gen'
}
2019-12-11 18:43:13 +01:00
// Now that we know the type, prepend it
// `[typ] [name] = bool_expression();`
2019-12-19 21:52:45 +01:00
// p.cgen.prepend_to_statement(nt_gen)
2019-12-11 18:43:13 +01:00
p.cgen.set_placeholder(0, nt_gen)
2019-09-14 22:48:30 +02:00
return typ
}
fn (p mut Parser) gen_fn_decl(f Fn, typ, str_args string) {
2019-12-19 21:52:45 +01:00
dll_export_linkage := if p.pref.ccompiler == 'msvc' && p.attr == 'live' && p.pref.is_so { '__declspec(dllexport) ' } else if p.attr == 'inline' { 'static inline ' } else { '' }
2019-09-14 22:48:30 +02:00
fn_name_cgen := p.table.fn_gen_name(f)
2019-12-19 21:52:45 +01:00
// str_args := f.str_args(p.table)
2019-12-08 20:22:47 +01:00
p.genln('$dll_export_linkage$typ $fn_name_cgen ($str_args) {')
2019-09-14 22:48:30 +02:00
}
// blank identifer assignment `_ = 111`
fn (p mut Parser) gen_blank_identifier_assign() {
assign_error_tok_idx := p.token_idx
p.check_name()
p.check_space(.assign)
is_indexer := p.peek() == .lsbr
2019-12-19 21:52:45 +01:00
is_fn_call,next_expr := p.is_expr_fn_call(p.token_idx)
pos := p.cgen.add_placeholder()
expr_tok := p.cur_tok_index()
2019-11-04 00:38:49 +01:00
p.is_var_decl = true
typ := p.bool_expression()
if typ == 'void' {
p.error_with_token_index('${next_expr}() $err_used_as_value', expr_tok)
}
2019-11-04 00:38:49 +01:00
p.is_var_decl = false
if !is_indexer && !is_fn_call {
p.error_with_token_index('assigning `$next_expr` to `_` is redundant', assign_error_tok_idx)
}
2019-11-04 00:38:49 +01:00
// handle or
if p.tok == .key_orelse {
p.gen_handle_option_or_else(typ, '', pos)
2019-12-19 21:52:45 +01:00
}
else {
if is_fn_call {
p.gen(';')
2019-12-19 21:52:45 +01:00
}
else {
p.cgen.resetln('{$typ _ = $p.cgen.cur_line;}')
}
}
}
fn (p mut Parser) gen_handle_option_or_else(_typ, name string, fn_call_ph int) string {
mut typ := _typ
if !typ.starts_with('Option_') {
p.error('`or` block cannot be applied to non-optional type')
}
is_assign := name.len > 0
tmp := p.get_tmp()
p.cgen.set_placeholder(fn_call_ph, '$typ $tmp = ')
typ = parse_pointer(typ[7..])
p.genln(';')
2019-11-04 00:38:49 +01:00
or_tok_idx := p.token_idx
p.fspace()
p.check(.key_orelse)
p.fspace()
p.check(.lcbr)
p.fspace()
2019-12-19 21:52:45 +01:00
p.register_var(Var{
name: 'err'
typ: 'string'
is_mut: false
is_used: true
})
2019-12-19 21:52:45 +01:00
p.register_var(Var{
name: 'errcode'
typ: 'int'
is_mut: false
is_used: true
})
if is_assign && !name.contains('.') && !p.is_var_decl {
2019-12-19 21:52:45 +01:00
// don't initialize struct fields
2019-11-04 00:38:49 +01:00
p.genln('$typ $name;')
}
p.genln('if (!$tmp .ok) {')
p.genln('string err = $tmp . error;')
p.genln('int errcode = $tmp . ecode;')
2019-11-04 00:38:49 +01:00
last_ph := p.cgen.add_placeholder()
last_typ := p.statements()
if is_assign && last_typ == typ {
// workaround for -g with default optional value
// when p.cgen.line_directives is true an extra
// line is added so we need to account for that
2019-12-19 21:52:45 +01:00
expr_line := if p.cgen.line_directives { p.cgen.lines[p.cgen.lines.len - 3] } else { p.cgen.lines[p.cgen.lines.len - 2] }
2019-11-04 00:38:49 +01:00
last_expr := expr_line[last_ph..]
2019-12-19 21:52:45 +01:00
p.cgen.lines[p.cgen.lines.len - 2] = ''
// same here
if p.cgen.line_directives {
2019-12-19 21:52:45 +01:00
p.cgen.lines[p.cgen.lines.len - 3] = ''
}
2019-11-19 15:47:49 +01:00
p.genln('if ($tmp .ok) {')
p.genln('$name = *($typ*) $tmp . data;')
p.genln('} else {')
p.genln('$name = $last_expr')
2019-11-04 00:38:49 +01:00
p.genln('}')
2019-12-19 21:52:45 +01:00
}
else if is_assign {
2019-11-13 19:47:05 +01:00
p.genln('$name = *($typ*)${tmp}.data;')
}
2019-11-04 00:38:49 +01:00
if !p.returns && last_typ != typ && is_assign && p.prev_tok2 != .key_continue && p.prev_tok2 != .key_break {
p.error_with_token_index('`or` block must provide a default value or return/exit/continue/break/panic', or_tok_idx)
}
p.returns = false
return typ
}
// `files := os.ls('.')?`
fn (p mut Parser) gen_handle_question_suffix(f Fn, ph int) string {
if p.cur_fn.name != 'main__main' {
p.error('`func()?` syntax can only be used inside `fn main()` for now')
}
p.check(.question)
tmp := p.get_tmp()
p.cgen.set_placeholder(ph, '$f.typ $tmp = ')
p.genln(';')
p.genln('if (!${tmp}.ok) v_panic(${tmp}.error);')
typ := f.typ[7..] // option_xxx
p.gen('*($typ*) ${tmp}.data;')
return typ
}
2019-09-14 22:48:30 +02:00
fn types_to_c(types []Type, table &Table) string {
mut sb := strings.new_builder(10)
for t in types {
2019-12-19 21:52:45 +01:00
// if t.cat != .union_ && t.cat != .struct_ && t.cat != .objc_interface {
2019-11-08 04:03:06 +01:00
if !(t.cat in [.union_, .struct_, .objc_interface, .interface_]) {
2019-09-14 22:48:30 +02:00
continue
}
2019-12-19 21:52:45 +01:00
// if is_atomic {
// sb.write('_Atomic ')
// }
2019-11-08 04:03:06 +01:00
if t.cat == .objc_interface {
sb.writeln('@interface $t.name : $t.parent { @public')
}
else {
2019-12-19 21:52:45 +01:00
kind := if t.cat == .union_ { 'union' } else { 'struct' }
sb.writeln('$kind $t.name {')
2019-11-08 04:03:06 +01:00
if t.cat == .interface_ {
sb.writeln('\tvoid* _object;')
sb.writeln('\tint _interface_idx; // int t')
}
}
2019-09-14 22:48:30 +02:00
for field in t.fields {
sb.write('\t')
2019-12-19 21:52:45 +01:00
sb.writeln(table.cgen_name_type_pair(field.name, field.typ) + ';')
2019-09-14 22:48:30 +02:00
}
sb.writeln('};\n')
2019-12-19 21:52:45 +01:00
if t.cat == .objc_interface {
sb.writeln('@end')
}
2019-09-14 22:48:30 +02:00
}
return sb.str()
}
2019-10-26 20:58:26 +02:00
fn (p mut Parser) index_get(typ string, fn_ph int, cfg IndexConfig) {
2019-09-14 22:48:30 +02:00
// Erase var name we generated earlier: "int a = m, 0"
// "m, 0" gets killed since we need to start from scratch. It's messy.
// "m, 0" is an index expression, save it before deleting and insert later in map_get()
mut index_expr := ''
if p.cgen.is_tmp {
index_expr = p.cgen.tmp_line[fn_ph..]
p.cgen.resetln(p.cgen.tmp_line[..fn_ph])
2019-12-19 21:52:45 +01:00
}
else {
index_expr = p.cgen.cur_line[fn_ph..]
p.cgen.resetln(p.cgen.cur_line[..fn_ph])
2019-09-14 22:48:30 +02:00
}
// Can't pass integer literal, because map_get() requires a void*
tmp := p.get_tmp()
tmp_ok := p.get_tmp()
if cfg.is_map {
p.gen('$tmp')
def := type_default(typ)
2019-12-19 21:52:45 +01:00
p.cgen.insert_before('$typ $tmp = $def; ' + 'bool $tmp_ok = map_get(/*$p.file_name : $p.scanner.line_nr*/$index_expr, & $tmp);')
2019-09-14 22:48:30 +02:00
}
else if cfg.is_arr {
if p.pref.translated && !p.builtin_mod {
p.gen('$index_expr ]')
}
else {
2019-10-26 21:26:30 +02:00
ref := if cfg.is_ptr { '*' } else { '' }
2019-10-26 20:58:26 +02:00
if cfg.is_slice {
p.gen(' array_slice2($ref $index_expr) ')
2019-11-30 07:41:32 +01:00
}
2019-10-26 20:58:26 +02:00
else {
2019-10-26 21:26:30 +02:00
p.gen('( *($typ*) array_get($ref $index_expr) )')
2019-09-14 22:48:30 +02:00
}
}
}
2019-11-16 21:22:40 +01:00
else if cfg.is_str && !p.builtin_mod {
2019-12-19 21:52:45 +01:00
if p.pref.is_bare {
2019-11-16 21:22:40 +01:00
p.gen(index_expr)
2019-11-30 07:41:32 +01:00
}
2019-11-16 21:22:40 +01:00
else if cfg.is_slice {
p.gen('string_substr2($index_expr)')
2019-12-19 21:52:45 +01:00
}
else {
2019-10-26 20:58:26 +02:00
p.gen('string_at($index_expr)')
}
2019-09-14 22:48:30 +02:00
}
// Zero the string after map_get() if it's nil, numbers are automatically 0
// This is ugly, but what can I do without generics?
// TODO what about user types?
if cfg.is_map && typ == 'string' {
// p.cgen.insert_before('if (!${tmp}.str) $tmp = tos("", 0);')
p.cgen.insert_before('if (!$tmp_ok) $tmp = tos((byte *)"", 0);')
}
}
fn (table mut Table) fn_gen_name(f &Fn) string {
mut name := f.name
if f.is_method {
name = '${f.receiver_typ}_$f.name'
name = name.replace(' ', '')
if f.name.len == 1 {
match f.name[0] {
2019-12-19 21:52:45 +01:00
`+` {
name = name.replace('+', 'op_plus')
}
`-` {
name = name.replace('-', 'op_minus')
}
`*` {
name = name.replace('*', 'op_mul')
}
`/` {
name = name.replace('/', 'op_div')
}
`%` {
name = name.replace('%', 'op_mod')
}
2020-01-10 19:59:15 +01:00
else {}
}
}
2019-09-14 22:48:30 +02:00
}
2019-11-08 04:03:06 +01:00
if f.is_interface {
2019-12-19 21:52:45 +01:00
// iname := f.args[0].typ // Speaker
// var := p.expr_var.name
2019-11-08 04:03:06 +01:00
return ''
}
2019-09-14 22:48:30 +02:00
// Avoid name conflicts (with things like abs(), print() etc).
2019-09-18 14:37:34 +02:00
// Generate v_abs(), v_print()
2019-09-14 22:48:30 +02:00
// TODO duplicate functionality
2019-11-30 13:40:08 +01:00
if f.mod == 'builtin' && f.name in c_reserved {
2019-09-14 22:48:30 +02:00
return 'v_$name'
}
// Obfuscate but skip certain names
// TODO ugly, fix
// NB: the order here is from faster to potentially slower checks
2019-12-19 21:52:45 +01:00
if table.obfuscate && !f.is_c && f.name != 'main' && f.name != 'WinMain' && f.name != 'main__main' && f.name != 'gg__vec2' && f.name != 'build_token_str' && f.name != 'build_keys' && f.mod != 'builtin' && f.mod != 'darwin' && f.mod != 'os' && f.mod != 'json' && !f.name.ends_with('_init') && !f.name.contains('window_proc') && !name.ends_with('_str') && !name.contains('contains') {
2019-09-14 22:48:30 +02:00
mut idx := table.obf_ids[name]
// No such function yet, register it
if idx == 0 {
table.fn_cnt++
table.obf_ids[name] = table.fn_cnt
idx = table.fn_cnt
}
old := name
name = 'f_$idx'
println('$old ==> $name')
}
return name
}
2019-12-19 21:52:45 +01:00
fn (p mut Parser) gen_method_call(receiver &Var, receiver_type string, cgen_name string, ftyp string, method_ph int) {
// mut cgen_name := p.table.fn_gen_name(f)
2019-10-25 20:32:27 +02:00
mut method_call := cgen_name + ' ('
2019-09-14 22:48:30 +02:00
// if receiver is key_mut or a ref (&), generate & for the first arg
if receiver.ref || (receiver.is_mut && !receiver_type.contains('*')) {
method_call += '& /* ? */'
}
// generate deref (TODO copy pasta later in fn_call_args)
if !receiver.is_mut && receiver_type.contains('*') {
method_call += '*'
}
mut cast := ''
// Method returns (void*) => cast it to int, string, user etc
// number := *(int*)numbers.first()
if ftyp == 'void*' {
if receiver_type.starts_with('array_') {
// array_int => int
cast = parse_pointer(receiver_type.all_after('array_'))
cast = '*($cast*) '
2019-12-19 21:52:45 +01:00
}
else {
cast = '(voidptr) '
}
2019-09-14 22:48:30 +02:00
}
p.cgen.set_placeholder(method_ph, '$cast $method_call')
2019-09-14 22:48:30 +02:00
}
fn (p mut Parser) gen_array_at(typ_ string, is_arr0 bool, fn_ph int) {
mut typ := typ_
2019-12-19 21:52:45 +01:00
// p.fgen('[')
2019-09-14 22:48:30 +02:00
// array_int a; a[0]
// type is "array_int", need "int"
// typ = typ.replace('array_', '')
// if is_arr0 {
2019-12-19 21:52:45 +01:00
// typ = typ.right(6)
// }
2019-09-14 22:48:30 +02:00
// array a; a.first() voidptr
// type is "array", need "void*"
if typ == 'array' {
typ = 'void*'
}
// No bounds check in translated from C code
if p.pref.translated && !p.builtin_mod {
// Cast void* to typ*: add (typ*) to the beginning of the assignment :
// ((int*)a.data = ...
p.cgen.set_placeholder(fn_ph, '(($typ*)(')
p.gen('.data))[')
}
else {
p.gen(',')
}
}
2019-09-14 22:48:30 +02:00
fn (p mut Parser) gen_for_header(i, tmp, var_typ, val string) {
p.genln('for (int $i = 0; $i < ${tmp}.len; $i++) {')
2019-12-19 21:52:45 +01:00
if val == '_' {
return
}
2019-09-14 22:48:30 +02:00
p.genln('$var_typ $val = (($var_typ *) $tmp . data)[$i];')
}
2019-12-05 00:11:51 +01:00
fn (p mut Parser) gen_for_fixed_header(i, tmp, var_typ, val string) {
p.genln('for (int $i = 0; $i < sizeof(${tmp}) / sizeof($tmp [0]); $i++) {')
2019-12-19 21:52:45 +01:00
if val == '_' {
return
}
2019-12-05 00:11:51 +01:00
p.genln('$var_typ $val = $tmp[$i];')
}
2019-09-14 22:48:30 +02:00
fn (p mut Parser) gen_for_str_header(i, tmp, var_typ, val string) {
2019-11-30 07:41:32 +01:00
// TODO var_typ is always byte
2019-12-19 21:52:45 +01:00
// p.genln('array_byte bytes_$tmp = string_bytes( $tmp );')
2019-09-14 22:48:30 +02:00
p.genln(';\nfor (int $i = 0; $i < $tmp .len; $i ++) {')
2019-12-19 21:52:45 +01:00
if val == '_' {
return
}
// p.genln('$var_typ $val = (($var_typ *) bytes_$tmp . data)[$i];')
2019-11-30 07:41:32 +01:00
p.genln('$var_typ $val = ${tmp}.str[$i];')
2019-09-14 22:48:30 +02:00
}
2019-09-17 21:16:16 +02:00
fn (p mut Parser) gen_for_range_header(i, range_end, tmp, var_type, val string) {
p.genln(';\nfor (int $i = $tmp; $i < $range_end; $i++) {')
2019-12-19 21:52:45 +01:00
if val == '_' {
return
}
2019-09-17 21:16:16 +02:00
p.genln('$var_type $val = $i;')
}
2019-09-14 22:48:30 +02:00
fn (p mut Parser) gen_for_map_header(i, tmp, var_typ, val, typ string) {
def := type_default(typ)
p.genln('array_string keys_$tmp = map_keys(& $tmp ); ')
p.genln('for (int l = 0; l < keys_$tmp .len; l++) {')
p.genln('string $i = ((string*)keys_$tmp .data)[l];')
// TODO don't call map_get() for each key, fetch values while traversing
// the tree (replace `map_keys()` above with `map_key_vals()`)
2019-12-19 21:52:45 +01:00
if val == '_' {
return
}
2019-09-14 22:48:30 +02:00
p.genln('$var_typ $val = $def; map_get($tmp, $i, & $val);')
}
fn (p mut Parser) gen_for_varg_header(i, varg, var_typ, val string) {
p.genln('for (int $i = 0; $i < ${varg}->len; $i++) {')
2019-12-19 21:52:45 +01:00
if val == '_' {
return
}
p.genln('$var_typ $val = (($var_typ *) $varg->args)[$i];')
}
2019-09-14 22:48:30 +02:00
fn (p mut Parser) gen_array_init(typ string, no_alloc bool, new_arr_ph int, nr_elems int) {
mut new_arr := 'new_array_from_c_array'
if no_alloc {
new_arr += '_no_alloc'
}
if nr_elems == 0 {
p.gen(' TCCSKIP(0) })')
2019-12-19 21:52:45 +01:00
}
else {
2019-09-14 22:48:30 +02:00
p.gen(' })')
}
// Need to do this in the second pass, otherwise it goes to the very top of the out.c file
if !p.first_pass() {
2019-12-19 21:52:45 +01:00
p.cgen.set_placeholder(new_arr_ph, '${new_arr}($nr_elems, $nr_elems, sizeof($typ), EMPTY_ARRAY_OF_ELEMS( $typ, $nr_elems ) { ')
2019-09-14 22:48:30 +02:00
}
}
2019-09-14 22:48:30 +02:00
2019-12-19 21:52:45 +01:00
fn (p mut Parser) gen_array_set(typ string, is_ptr, is_map bool, fn_ph, assign_pos int, is_cao bool) {
2019-09-14 22:48:30 +02:00
// `a[0] = 7`
// curline right now: `a , 0 = 7`
mut val := p.cgen.cur_line[assign_pos..]
p.cgen.resetln(p.cgen.cur_line[..assign_pos])
2019-09-14 22:48:30 +02:00
mut cao_tmp := p.cgen.cur_line
mut func := ''
if is_map {
2019-11-12 20:35:53 +01:00
if is_ptr {
func = 'map_set('
2019-12-19 21:52:45 +01:00
}
else {
2019-11-12 20:35:53 +01:00
func = 'map_set(&'
}
2019-09-14 22:48:30 +02:00
// CAO on map is a bit more complicated as it loads
// the value inside a pointer instead of returning it.
}
else {
if is_ptr {
func = 'array_set('
if is_cao {
cao_tmp = '*($p.expected_type *) array_get(*$cao_tmp)'
2019-09-14 22:48:30 +02:00
}
}
else {
func = 'array_set(&/*q*/'
if is_cao {
cao_tmp = '*($p.expected_type *) array_get($cao_tmp)'
2019-09-14 22:48:30 +02:00
}
}
}
p.cgen.set_placeholder(fn_ph, func)
if is_cao {
2019-12-19 21:52:45 +01:00
val = cao_tmp + val.all_before('=') + val.all_after('=')
2019-09-14 22:48:30 +02:00
}
p.gen(', & ($typ []) { $val })')
}
// returns true in case of an early return
2019-12-21 01:53:58 +01:00
fn (p mut Parser) gen_struct_init(typ string, t &Type) bool {
2019-09-14 22:48:30 +02:00
// TODO hack. If it's a C type, we may need to add "struct" before declaration:
// a := &C.A{} ==> struct A* a = malloc(sizeof(struct A));
if p.is_c_struct_init {
if t.cat != .c_typedef {
p.cgen.insert_before('struct /*c struct init*/')
}
}
// TODO tm struct struct bug
if typ == 'tm' {
2019-12-19 21:52:45 +01:00
p.cgen.lines[p.cgen.lines.len - 1] = ''
2019-09-14 22:48:30 +02:00
}
p.next()
p.check(.lcbr)
ptr := typ.contains('*')
// `user := User{foo:bar}` => `User user = (User){ .foo = bar}`
if !ptr {
if p.is_c_struct_init {
// `face := C.FT_Face{}` => `FT_Face face;`
if p.tok == .rcbr {
p.is_empty_c_struct_init = true
p.check(.rcbr)
return true
}
p.gen('(struct $typ) {')
p.is_c_struct_init = false
}
else {
p.gen('($typ) {')
}
}
else {
if p.tok == .not {
2019-12-04 10:56:59 +01:00
// old &User{!} ==> 0 hack
2019-12-08 20:22:47 +01:00
p.error('use `${t.name}(0)` instead of `&$t.name{!}`')
2019-12-04 10:56:59 +01:00
/*
2019-09-14 22:48:30 +02:00
p.next()
p.gen('0')
p.check(.rcbr)
return true
2019-12-04 10:56:59 +01:00
*/
2019-12-19 21:52:45 +01:00
2019-09-14 22:48:30 +02:00
}
2019-12-04 10:56:59 +01:00
p.gen('($t.name*)memdup(&($t.name) {')
2019-09-14 22:48:30 +02:00
}
return false
}
fn (p mut Parser) gen_struct_field_init(field string) {
p.gen('.$field = ')
}
fn (p mut Parser) gen_empty_map(typ string) {
p.gen('new_map(1, sizeof($typ))')
}
fn (p mut Parser) cast(typ string) {
p.gen('(')
2019-12-19 21:52:45 +01:00
defer {
p.gen(')')
}
2019-09-14 22:48:30 +02:00
p.next()
pos := p.cgen.add_placeholder()
if p.tok == .rpar {
// skip `)` if it's `(*int)(ptr)`, not `int(a)`
p.ptr_cast = true
p.next()
}
p.check(.lpar)
p.expected_type = typ
expr_typ := p.bool_expression()
2019-12-07 13:31:56 +01:00
// Do not allow `int(my_int)`
if expr_typ == typ {
p.warn('casting `$typ` to `$expr_typ` is not needed')
}
2019-09-14 22:48:30 +02:00
// `face := FT_Face(cobj)` => `FT_Face face = *((FT_Face*)cobj);`
2019-12-19 21:52:45 +01:00
casting_voidptr_to_value := expr_typ == 'void*' && typ != 'int' && typ != 'byteptr' && !typ.ends_with('*')
2019-09-14 22:48:30 +02:00
p.expected_type = ''
// `string(buffer)` => `tos2(buffer)`
// `string(buffer, len)` => `tos(buffer, len)`
// `string(bytes_array, len)` => `tos(bytes_array.data, len)`
is_byteptr := expr_typ == 'byte*' || expr_typ == 'byteptr'
is_bytearr := expr_typ == 'array_byte'
if typ == 'string' {
if is_byteptr || is_bytearr {
if p.tok == .comma {
p.check(.comma)
p.cgen.set_placeholder(pos, 'tos((byte *)')
if is_bytearr {
p.gen('.data')
}
p.gen(', ')
p.check_types(p.expression(), 'int')
2019-12-19 21:52:45 +01:00
}
else {
2019-09-14 22:48:30 +02:00
if is_bytearr {
p.gen('.data')
}
p.cgen.set_placeholder(pos, 'tos2((byte *)')
}
}
// `string(234)` => error
else if expr_typ == 'int' {
p.error('cannot cast `$expr_typ` to `$typ`, use `str()` method instead')
}
else {
p.error('cannot cast `$expr_typ` to `$typ`')
}
}
else if typ == 'byte' && expr_typ == 'string' {
p.error('cannot cast `$expr_typ` to `$typ`, use backquotes `` to create a `$typ` or access the value of an index of `$expr_typ` using []')
}
else if casting_voidptr_to_value {
p.cgen.set_placeholder(pos, '($typ)(')
2019-09-14 22:48:30 +02:00
}
else {
2019-11-05 16:58:42 +01:00
// Nothing can be cast to bool
if typ == 'bool' {
if is_number_type(expr_typ) || is_float_type(expr_typ) {
p.error('cannot cast a number to `bool`')
2019-11-30 07:41:32 +01:00
}
2019-11-05 16:58:42 +01:00
p.error('cannot cast `$expr_typ` to `bool`')
}
2019-11-05 18:41:02 +01:00
// Strings can't be cast
if expr_typ == 'string' {
2020-01-10 19:59:15 +01:00
if is_number_type(typ) || is_float_type(typ) {
p.error('cannot cast `string` to `$typ`, use `${expr_typ}.${typ}()` instead')
}
2019-11-05 18:41:02 +01:00
p.error('cannot cast `$expr_typ` to `$typ`')
2019-11-30 07:41:32 +01:00
}
2019-11-23 18:40:54 +01:00
// Nothing can be cast to bool
if expr_typ == 'bool' {
p.error('cannot cast `bool` to `$typ`')
2019-11-30 07:41:32 +01:00
}
2019-09-14 22:48:30 +02:00
p.cgen.set_placeholder(pos, '($typ)(')
}
p.check(.rpar)
p.gen(')')
}
fn type_default(typ string) string {
if typ.starts_with('array_') {
return 'new_array(0, 1, sizeof( ${parse_pointer(typ[6..])} ))'
2019-09-14 22:48:30 +02:00
}
// Always set pointers to 0
if typ.ends_with('*') {
return '0'
}
// User struct defined in another module.
if typ.contains('__') {
return '{0}'
}
// Default values for other types are not needed because of mandatory initialization
2019-12-19 21:52:45 +01:00
match typ {
'bool' {
return '0'
}
'string' {
return 'tos3("")'
}
'i8' {
return '0'
}
'i16' {
return '0'
}
'i64' {
return '0'
}
'u16' {
return '0'
}
'u32' {
return '0'
}
'u64' {
return '0'
}
'byte' {
return '0'
}
'int' {
return '0'
}
'rune' {
return '0'
}
'f32' {
return '0.0'
}
'f64' {
return '0.0'
}
'byteptr' {
return '0'
}
'voidptr' {
return '0'
}
2020-01-10 19:59:15 +01:00
else {}
}
2019-12-19 21:52:45 +01:00
return '{0}'
2019-12-07 14:58:43 +01:00
// TODO this results in
// error: expected a field designator, such as '.field = 4'
2019-12-19 21:52:45 +01:00
// - Empty ee= (Empty) { . = {0} } ;
2019-12-07 14:58:43 +01:00
/*
return match typ {
'bool'{ '0'}
'string'{ 'tos3("")'}
'i8'{ '0'}
'i16'{ '0'}
'i64'{ '0'}
'u16'{ '0'}
'u32'{ '0'}
'u64'{ '0'}
'byte'{ '0'}
'int'{ '0'}
'rune'{ '0'}
'f32'{ '0.0'}
'f64'{ '0.0'}
'byteptr'{ '0'}
'voidptr'{ '0'}
else { '{0} '}
}
*/
2019-12-19 21:52:45 +01:00
2019-09-14 22:48:30 +02:00
}
fn (p mut Parser) gen_array_push(ph int, typ, expr_type, tmp, elm_type string) {
// Two arrays of the same type?
2019-09-14 22:48:30 +02:00
push_array := typ == expr_type
if push_array {
2019-12-19 21:52:45 +01:00
p.cgen.set_placeholder(ph, '_PUSH_MANY(&')
2019-09-14 22:48:30 +02:00
p.gen('), $tmp, $typ)')
2019-12-19 21:52:45 +01:00
}
else {
p.check_types(expr_type, elm_type)
2019-09-14 22:48:30 +02:00
// Pass tmp var info to the _PUSH macro
// Prepend tmp initialisation and push call
// Don't dereference if it's already a mutable array argument (`fn foo(mut []int)`)
2019-12-19 21:52:45 +01:00
push_call := if typ.contains('*') { '_PUSH(' } else { '_PUSH(&' }
2019-09-14 22:48:30 +02:00
p.cgen.set_placeholder(ph, push_call)
2019-12-18 01:26:04 +01:00
p.gen('), $tmp, $elm_type)')
2019-09-14 22:48:30 +02:00
}
}