2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-05-25 05:32:33 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2021-02-02 15:41:51 +01:00
|
|
|
module c
|
2020-05-25 05:32:33 +02:00
|
|
|
|
2021-01-31 18:22:42 +01:00
|
|
|
import os
|
2020-06-08 13:30:17 +02:00
|
|
|
import v.ast
|
|
|
|
import v.table
|
2020-07-01 00:53:53 +02:00
|
|
|
import v.util
|
2020-05-25 05:32:33 +02:00
|
|
|
|
2021-01-05 15:11:43 +01:00
|
|
|
fn (mut g Gen) comptime_selector(node ast.ComptimeSelector) {
|
|
|
|
g.expr(node.left)
|
|
|
|
if node.left_type.is_ptr() {
|
|
|
|
g.write('->')
|
|
|
|
} else {
|
|
|
|
g.write('.')
|
|
|
|
}
|
|
|
|
// check for field.name
|
|
|
|
if node.field_expr is ast.SelectorExpr {
|
|
|
|
if node.field_expr.expr is ast.Ident {
|
2021-01-23 09:33:22 +01:00
|
|
|
if node.field_expr.expr.name == g.comp_for_field_var
|
|
|
|
&& node.field_expr.field_name == 'name' {
|
2021-01-05 15:11:43 +01:00
|
|
|
g.write(g.comp_for_field_value.name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.expr(node.field_expr)
|
|
|
|
}
|
|
|
|
|
2020-07-23 23:16:36 +02:00
|
|
|
fn (mut g Gen) comptime_call(node ast.ComptimeCall) {
|
2021-01-14 15:20:11 +01:00
|
|
|
if node.is_embed {
|
2021-01-31 18:22:42 +01:00
|
|
|
// $embed_file('/path/to/file')
|
2021-01-14 15:20:11 +01:00
|
|
|
g.gen_embed_file_init(node)
|
|
|
|
return
|
|
|
|
}
|
2021-01-31 18:22:42 +01:00
|
|
|
if node.method_name == 'env' {
|
|
|
|
// $env('ENV_VAR_NAME')
|
|
|
|
val := util.cescaped_path(os.getenv(node.args_var))
|
|
|
|
g.write('_SLIT("$val")')
|
|
|
|
return
|
|
|
|
}
|
2020-06-08 13:30:17 +02:00
|
|
|
if node.is_vweb {
|
2020-11-26 18:40:31 +01:00
|
|
|
is_html := node.method_name == 'html'
|
2020-06-08 13:30:17 +02:00
|
|
|
for stmt in node.vweb_tmpl.stmts {
|
|
|
|
if stmt is ast.FnDecl {
|
|
|
|
// insert stmts from vweb_tmpl fn
|
2020-07-09 17:14:14 +02:00
|
|
|
if stmt.name.starts_with('main.vweb_tmpl') {
|
2020-11-26 18:40:31 +01:00
|
|
|
if is_html {
|
|
|
|
g.inside_vweb_tmpl = true
|
|
|
|
}
|
2020-07-09 17:14:14 +02:00
|
|
|
g.stmts(stmt.stmts)
|
2020-06-24 22:12:33 +02:00
|
|
|
g.inside_vweb_tmpl = false
|
2020-06-08 13:30:17 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 18:40:31 +01:00
|
|
|
if is_html {
|
|
|
|
// return vweb html template
|
2020-12-31 17:07:24 +01:00
|
|
|
g.writeln('vweb__Context_html(&app->Context, _tmpl_res_$g.fn_decl.name); strings__Builder_free(&sb); string_free(&_tmpl_res_$g.fn_decl.name);')
|
2020-11-26 18:40:31 +01:00
|
|
|
} else {
|
|
|
|
// return $tmpl string
|
|
|
|
fn_name := g.fn_decl.name.replace('.', '__')
|
|
|
|
g.writeln('return _tmpl_res_$fn_name;')
|
|
|
|
}
|
2020-06-08 13:30:17 +02:00
|
|
|
return
|
2020-05-25 05:32:33 +02:00
|
|
|
}
|
2021-01-30 15:24:16 +01:00
|
|
|
g.writeln('// \$method call. sym="$node.sym.name"')
|
2020-07-03 15:10:39 +02:00
|
|
|
if node.method_name == 'method' {
|
|
|
|
// `app.$method()`
|
2020-12-04 12:25:23 +01:00
|
|
|
m := node.sym.find_method(g.comp_for_method) or { return }
|
2020-07-03 15:10:39 +02:00
|
|
|
/*
|
|
|
|
vals := m.attrs[0].split('/')
|
|
|
|
args := vals.filter(it.starts_with(':')).map(it[1..])
|
|
|
|
println(vals)
|
|
|
|
for val in vals {
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
g.write('${util.no_dots(node.sym.name)}_${g.comp_for_method}(')
|
|
|
|
g.expr(node.left)
|
2020-09-27 03:32:56 +02:00
|
|
|
if m.params.len > 1 {
|
2020-07-03 15:10:39 +02:00
|
|
|
g.write(', ')
|
|
|
|
}
|
2020-09-27 03:32:56 +02:00
|
|
|
for i in 1 .. m.params.len {
|
2020-07-23 17:19:37 +02:00
|
|
|
if node.left is ast.Ident {
|
2020-11-21 00:05:57 +01:00
|
|
|
if m.params[i].name == node.left.name {
|
2020-07-23 17:19:37 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-09-27 03:32:56 +02:00
|
|
|
if m.params[i].typ.is_int() || m.params[i].typ.idx() == table.bool_type_idx {
|
2020-07-23 17:19:37 +02:00
|
|
|
// Gets the type name and cast the string to the type with the string_<type> function
|
2020-09-27 03:32:56 +02:00
|
|
|
type_name := g.table.types[int(m.params[i].typ)].str()
|
2020-10-15 22:12:59 +02:00
|
|
|
g.write('string_${type_name}(((string*)${node.args_var}.data) [${i - 1}])')
|
2020-07-23 17:19:37 +02:00
|
|
|
} else {
|
2020-10-15 22:12:59 +02:00
|
|
|
g.write('((string*)${node.args_var}.data) [${i - 1}] ')
|
2020-07-23 17:19:37 +02:00
|
|
|
}
|
2020-09-27 03:32:56 +02:00
|
|
|
if i < m.params.len - 1 {
|
2020-07-03 15:10:39 +02:00
|
|
|
g.write(', ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.write(' ); // vweb action call with args')
|
|
|
|
return
|
|
|
|
}
|
2021-01-30 15:24:16 +01:00
|
|
|
mut j := 0
|
2020-06-08 13:30:17 +02:00
|
|
|
for method in node.sym.methods {
|
2020-06-20 03:12:35 +02:00
|
|
|
// if method.return_type != table.void_type {
|
2021-01-30 15:24:16 +01:00
|
|
|
if method.return_type != node.result_type {
|
2020-06-08 13:30:17 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-09-27 03:32:56 +02:00
|
|
|
if method.params.len != 1 {
|
2020-07-03 15:10:39 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-06-08 13:30:17 +02:00
|
|
|
// receiver := method.args[0]
|
|
|
|
// if !p.expr_var.ptr {
|
|
|
|
// p.error('`$p.expr_var.name` needs to be a reference')
|
2020-05-25 05:32:33 +02:00
|
|
|
// }
|
2020-06-08 13:30:17 +02:00
|
|
|
amp := '' // if receiver.is_mut && !p.expr_var.ptr { '&' } else { '' }
|
2021-01-30 15:24:16 +01:00
|
|
|
if node.is_vweb {
|
|
|
|
if j > 0 {
|
|
|
|
g.write(' else ')
|
|
|
|
}
|
|
|
|
g.write('if (string_eq($node.method_name, _SLIT("$method.name"))) ')
|
2020-06-08 13:30:17 +02:00
|
|
|
}
|
2020-07-01 00:53:53 +02:00
|
|
|
g.write('${util.no_dots(node.sym.name)}_${method.name}($amp ')
|
2020-06-08 13:30:17 +02:00
|
|
|
g.expr(node.left)
|
|
|
|
g.writeln(');')
|
|
|
|
j++
|
2020-05-25 05:32:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 02:00:14 +02:00
|
|
|
fn cgen_attrs(attrs []table.Attr) []string {
|
|
|
|
mut res := []string{cap: attrs.len}
|
|
|
|
for attr in attrs {
|
|
|
|
// we currently don't quote 'arg' (otherwise we could just use `s := attr.str()`)
|
|
|
|
mut s := attr.name
|
|
|
|
if attr.arg.len > 0 {
|
|
|
|
s += ': $attr.arg'
|
|
|
|
}
|
2020-12-03 16:02:48 +01:00
|
|
|
res << '_SLIT("$s")'
|
2020-08-10 02:00:14 +02:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-11-05 09:12:32 +01:00
|
|
|
fn (mut g Gen) comp_at(node ast.AtExpr) {
|
|
|
|
if node.kind == .vmod_file {
|
2020-12-05 19:13:30 +01:00
|
|
|
val := cnewlines(node.val.replace('\r', ''))
|
2020-12-03 16:02:48 +01:00
|
|
|
g.write('_SLIT("$val")')
|
2020-11-05 09:12:32 +01:00
|
|
|
} else {
|
2020-11-27 10:16:57 +01:00
|
|
|
val := node.val.replace('\\', '\\\\')
|
2020-12-03 16:02:48 +01:00
|
|
|
g.write('_SLIT("$val")')
|
2020-11-05 09:12:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:58:54 +02:00
|
|
|
fn (mut g Gen) comp_if(node ast.IfExpr) {
|
|
|
|
line := if node.is_expr {
|
|
|
|
stmt_str := g.go_before_stmt(0)
|
2021-03-09 12:03:25 +01:00
|
|
|
g.write(util.tabs(g.indent))
|
2020-09-18 00:58:54 +02:00
|
|
|
stmt_str.trim_space()
|
2020-10-15 22:12:59 +02:00
|
|
|
} else {
|
|
|
|
''
|
|
|
|
}
|
2021-01-08 18:39:58 +01:00
|
|
|
mut comp_if_stmts_skip := false
|
2020-09-18 00:58:54 +02:00
|
|
|
for i, branch in node.branches {
|
|
|
|
start_pos := g.out.len
|
|
|
|
if i == node.branches.len - 1 && node.has_else {
|
|
|
|
g.writeln('#else')
|
2021-01-25 17:08:02 +01:00
|
|
|
comp_if_stmts_skip = false
|
2020-09-18 00:58:54 +02:00
|
|
|
} else {
|
|
|
|
if i == 0 {
|
|
|
|
g.write('#if ')
|
|
|
|
} else {
|
|
|
|
g.write('#elif ')
|
|
|
|
}
|
2021-01-25 17:08:02 +01:00
|
|
|
comp_if_stmts_skip = !g.comp_if_cond(branch.cond)
|
2020-09-18 00:58:54 +02:00
|
|
|
g.writeln('')
|
2020-07-25 00:02:44 +02:00
|
|
|
}
|
2020-09-18 00:58:54 +02:00
|
|
|
expr_str := g.out.last_n(g.out.len - start_pos).trim_space()
|
|
|
|
g.defer_ifdef = expr_str
|
|
|
|
if node.is_expr {
|
|
|
|
len := branch.stmts.len
|
|
|
|
if len > 0 {
|
|
|
|
last := branch.stmts[len - 1] as ast.ExprStmt
|
|
|
|
if len > 1 {
|
|
|
|
tmp := g.new_tmp_var()
|
|
|
|
styp := g.typ(last.typ)
|
|
|
|
g.indent++
|
|
|
|
g.writeln('$styp $tmp;')
|
|
|
|
g.writeln('{')
|
2020-10-15 22:12:59 +02:00
|
|
|
g.stmts(branch.stmts[0..len - 1])
|
2020-09-18 00:58:54 +02:00
|
|
|
g.write('\t$tmp = ')
|
|
|
|
g.stmt(last)
|
|
|
|
g.writeln('}')
|
|
|
|
g.indent--
|
|
|
|
g.writeln('$line $tmp;')
|
|
|
|
} else {
|
|
|
|
g.write('$line ')
|
|
|
|
g.stmt(last)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Only wrap the contents in {} if we're inside a function, not on the top level scope
|
|
|
|
should_create_scope := g.fn_decl != 0
|
2020-10-15 22:12:59 +02:00
|
|
|
if should_create_scope {
|
|
|
|
g.writeln('{')
|
|
|
|
}
|
2021-01-25 17:08:02 +01:00
|
|
|
if !comp_if_stmts_skip {
|
2021-01-08 18:39:58 +01:00
|
|
|
g.stmts(branch.stmts)
|
|
|
|
}
|
2020-10-15 22:12:59 +02:00
|
|
|
if should_create_scope {
|
|
|
|
g.writeln('}')
|
|
|
|
}
|
2020-07-25 00:02:44 +02:00
|
|
|
}
|
2020-06-08 13:30:17 +02:00
|
|
|
g.defer_ifdef = ''
|
|
|
|
}
|
2020-10-15 22:12:59 +02:00
|
|
|
if node.is_expr {
|
|
|
|
g.write('#endif')
|
|
|
|
} else {
|
|
|
|
g.writeln('#endif')
|
|
|
|
}
|
2020-09-18 00:58:54 +02:00
|
|
|
}
|
|
|
|
|
2021-01-25 17:08:02 +01:00
|
|
|
fn (mut g Gen) comp_if_cond(cond ast.Expr) bool {
|
2020-11-25 12:09:40 +01:00
|
|
|
match cond {
|
2020-12-11 04:46:06 +01:00
|
|
|
ast.BoolLiteral {
|
|
|
|
g.expr(cond)
|
2021-01-25 17:08:02 +01:00
|
|
|
return true
|
2020-12-11 04:46:06 +01:00
|
|
|
}
|
2020-09-18 00:58:54 +02:00
|
|
|
ast.ParExpr {
|
|
|
|
g.write('(')
|
2021-01-25 17:08:02 +01:00
|
|
|
is_cond_true := g.comp_if_cond(cond.expr)
|
2020-09-18 00:58:54 +02:00
|
|
|
g.write(')')
|
2021-01-25 17:08:02 +01:00
|
|
|
return is_cond_true
|
2020-10-15 22:12:59 +02:00
|
|
|
}
|
|
|
|
ast.PrefixExpr {
|
2020-09-18 00:58:54 +02:00
|
|
|
g.write(cond.op.str())
|
2021-01-25 17:08:02 +01:00
|
|
|
return g.comp_if_cond(cond.right)
|
2020-10-15 22:12:59 +02:00
|
|
|
}
|
|
|
|
ast.PostfixExpr {
|
2020-12-11 04:46:06 +01:00
|
|
|
ifdef := g.comp_if_to_ifdef((cond.expr as ast.Ident).name, true) or {
|
2021-02-28 21:20:21 +01:00
|
|
|
verror(err.msg)
|
2021-01-25 17:08:02 +01:00
|
|
|
return false
|
2020-12-11 04:46:06 +01:00
|
|
|
}
|
2020-09-18 00:58:54 +02:00
|
|
|
g.write('defined($ifdef)')
|
2021-01-25 17:08:02 +01:00
|
|
|
return true
|
2020-10-15 22:12:59 +02:00
|
|
|
}
|
|
|
|
ast.InfixExpr {
|
2020-09-18 00:58:54 +02:00
|
|
|
match cond.op {
|
|
|
|
.and, .logical_or {
|
2021-01-25 17:08:02 +01:00
|
|
|
l := g.comp_if_cond(cond.left)
|
2020-09-18 00:58:54 +02:00
|
|
|
g.write(' $cond.op ')
|
2021-01-25 17:08:02 +01:00
|
|
|
r := g.comp_if_cond(cond.right)
|
|
|
|
return l && r
|
2020-09-18 00:58:54 +02:00
|
|
|
}
|
|
|
|
.key_is, .not_is {
|
2021-01-06 18:58:53 +01:00
|
|
|
left := cond.left
|
|
|
|
mut name := ''
|
|
|
|
mut exp_type := table.Type(0)
|
2020-09-18 00:58:54 +02:00
|
|
|
got_type := (cond.right as ast.Type).typ
|
2021-01-06 18:58:53 +01:00
|
|
|
if left is ast.SelectorExpr {
|
|
|
|
name = '${left.expr}.$left.field_name'
|
|
|
|
exp_type = g.comptime_var_type_map[name]
|
|
|
|
} else if left is ast.Type {
|
|
|
|
name = left.str()
|
|
|
|
// this is only allowed for generics currently, otherwise blocked by checker
|
|
|
|
exp_type = g.unwrap_generic(left.typ)
|
|
|
|
}
|
2021-01-25 17:08:02 +01:00
|
|
|
|
|
|
|
if cond.op == .key_is {
|
|
|
|
g.write('$exp_type == $got_type')
|
|
|
|
return exp_type == got_type
|
|
|
|
} else {
|
|
|
|
g.write('$exp_type !=$got_type')
|
|
|
|
return exp_type != got_type
|
|
|
|
}
|
2020-10-15 22:12:59 +02:00
|
|
|
}
|
|
|
|
.eq, .ne {
|
2020-09-18 00:58:54 +02:00
|
|
|
// TODO Implement `$if method.args.len == 1`
|
2020-12-11 04:46:06 +01:00
|
|
|
g.write('1')
|
2021-01-25 17:08:02 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return true
|
2020-10-15 22:12:59 +02:00
|
|
|
}
|
2020-09-18 00:58:54 +02:00
|
|
|
}
|
2020-10-15 22:12:59 +02:00
|
|
|
}
|
|
|
|
ast.Ident {
|
2020-12-11 04:46:06 +01:00
|
|
|
ifdef := g.comp_if_to_ifdef(cond.name, false) or { 'true' } // handled in checker
|
2020-09-18 00:58:54 +02:00
|
|
|
g.write('defined($ifdef)')
|
2021-01-25 17:08:02 +01:00
|
|
|
return true
|
2020-10-15 22:12:59 +02:00
|
|
|
}
|
2020-12-11 04:46:06 +01:00
|
|
|
else {
|
|
|
|
// should be unreachable, but just in case
|
|
|
|
g.write('1')
|
2021-01-25 17:08:02 +01:00
|
|
|
return true
|
2020-12-11 04:46:06 +01:00
|
|
|
}
|
2020-09-18 00:58:54 +02:00
|
|
|
}
|
2020-05-25 05:32:33 +02:00
|
|
|
}
|
2020-07-03 15:10:39 +02:00
|
|
|
|
|
|
|
fn (mut g Gen) comp_for(node ast.CompFor) {
|
|
|
|
sym := g.table.get_type_symbol(g.unwrap_generic(node.typ))
|
2021-03-01 01:01:56 +01:00
|
|
|
g.writeln('/* \$for $node.val_var in ${sym.name}($node.kind.str()) */ {')
|
|
|
|
g.indent++
|
2020-07-25 00:02:44 +02:00
|
|
|
// vweb_result_type := table.new_type(g.table.find_type_idx('vweb.Result'))
|
2020-07-03 15:10:39 +02:00
|
|
|
mut i := 0
|
2020-12-03 16:02:48 +01:00
|
|
|
// g.writeln('string method = _SLIT("");')
|
2020-07-25 00:02:44 +02:00
|
|
|
if node.kind == .methods {
|
|
|
|
mut methods := sym.methods.filter(it.attrs.len == 0) // methods without attrs first
|
2020-07-25 18:58:23 +02:00
|
|
|
methods_with_attrs := sym.methods.filter(it.attrs.len > 0) // methods with attrs second
|
2020-07-25 00:02:44 +02:00
|
|
|
methods << methods_with_attrs
|
|
|
|
if methods.len > 0 {
|
2021-03-01 01:01:56 +01:00
|
|
|
g.writeln('FunctionData $node.val_var = {0};')
|
2020-07-03 15:10:39 +02:00
|
|
|
}
|
2020-07-25 00:02:44 +02:00
|
|
|
for method in methods { // sym.methods {
|
|
|
|
/*
|
|
|
|
if method.return_type != vweb_result_type { // table.void_type {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
g.comp_for_method = method.name
|
2021-03-01 01:01:56 +01:00
|
|
|
g.writeln('/* method $i */ {')
|
2020-12-03 16:02:48 +01:00
|
|
|
g.writeln('\t${node.val_var}.name = _SLIT("$method.name");')
|
2020-07-25 00:02:44 +02:00
|
|
|
if method.attrs.len == 0 {
|
|
|
|
g.writeln('\t${node.val_var}.attrs = __new_array_with_default(0, 0, sizeof(string), 0);')
|
|
|
|
} else {
|
2020-08-10 02:00:14 +02:00
|
|
|
attrs := cgen_attrs(method.attrs)
|
2021-01-23 09:33:22 +01:00
|
|
|
g.writeln(
|
|
|
|
'\t${node.val_var}.attrs = new_array_from_c_array($attrs.len, $attrs.len, sizeof(string), _MOV((string[$attrs.len]){' +
|
2020-07-25 00:02:44 +02:00
|
|
|
attrs.join(', ') + '}));')
|
|
|
|
}
|
2020-09-27 03:32:56 +02:00
|
|
|
if method.params.len < 2 {
|
2020-08-27 15:00:44 +02:00
|
|
|
// 0 or 1 (the receiver) args
|
|
|
|
g.writeln('\t${node.val_var}.args = __new_array_with_default(0, 0, sizeof(MethodArgs), 0);')
|
|
|
|
} else {
|
2020-09-27 03:32:56 +02:00
|
|
|
len := method.params.len - 1
|
2020-08-27 15:00:44 +02:00
|
|
|
g.write('\t${node.val_var}.args = new_array_from_c_array($len, $len, sizeof(MethodArgs), _MOV((MethodArgs[$len]){')
|
|
|
|
// Skip receiver arg
|
2020-09-27 03:32:56 +02:00
|
|
|
for j, arg in method.params[1..] {
|
2020-08-27 15:00:44 +02:00
|
|
|
typ := arg.typ.idx()
|
2021-03-07 14:28:43 +01:00
|
|
|
g.write('{$typ.str(), _SLIT("$arg.name")}')
|
2020-08-27 15:00:44 +02:00
|
|
|
if j < len - 1 {
|
|
|
|
g.write(', ')
|
|
|
|
}
|
2020-09-28 06:13:38 +02:00
|
|
|
g.comptime_var_type_map['${node.val_var}.args[$j].typ'] = typ
|
2020-08-27 15:00:44 +02:00
|
|
|
}
|
|
|
|
g.writeln('}));')
|
|
|
|
}
|
|
|
|
mut sig := 'anon_fn_'
|
|
|
|
// skip the first (receiver) arg
|
2020-09-27 03:32:56 +02:00
|
|
|
for j, arg in method.params[1..] {
|
2020-08-27 15:00:44 +02:00
|
|
|
// TODO: ignore mut/pts in sig for now
|
|
|
|
typ := arg.typ.set_nr_muls(0)
|
|
|
|
sig += '$typ'
|
2020-09-27 03:32:56 +02:00
|
|
|
if j < method.params.len - 2 {
|
2020-08-27 15:00:44 +02:00
|
|
|
sig += '_'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sig += '_$method.return_type'
|
|
|
|
styp := g.table.find_type_idx(sig)
|
|
|
|
// println(styp)
|
|
|
|
// if styp == 0 { }
|
|
|
|
// TODO: type aliases
|
|
|
|
ret_typ := method.return_type.idx()
|
2020-09-28 06:13:38 +02:00
|
|
|
g.writeln('\t${node.val_var}.typ = $styp;')
|
|
|
|
g.writeln('\t${node.val_var}.return_type = $ret_typ;')
|
2020-07-25 00:02:44 +02:00
|
|
|
//
|
2020-09-28 06:13:38 +02:00
|
|
|
g.comptime_var_type_map['${node.val_var}.return_type'] = ret_typ
|
|
|
|
g.comptime_var_type_map['${node.val_var}.typ'] = styp
|
2020-07-25 00:02:44 +02:00
|
|
|
g.stmts(node.stmts)
|
|
|
|
i++
|
2021-03-01 01:01:56 +01:00
|
|
|
g.writeln('}')
|
2020-08-27 15:00:44 +02:00
|
|
|
for key, _ in g.comptime_var_type_map {
|
|
|
|
if key.starts_with(node.val_var) {
|
|
|
|
g.comptime_var_type_map.delete(key)
|
|
|
|
}
|
|
|
|
}
|
2020-07-03 15:10:39 +02:00
|
|
|
}
|
2020-07-25 00:02:44 +02:00
|
|
|
} else if node.kind == .fields {
|
|
|
|
// TODO add fields
|
2021-01-05 15:11:43 +01:00
|
|
|
if sym.info is table.Struct {
|
|
|
|
mut fields := sym.info.fields.filter(it.attrs.len == 0)
|
|
|
|
fields_with_attrs := sym.info.fields.filter(it.attrs.len > 0)
|
2020-07-25 00:02:44 +02:00
|
|
|
fields << fields_with_attrs
|
|
|
|
if fields.len > 0 {
|
2021-01-25 17:08:48 +01:00
|
|
|
g.writeln('\tFieldData $node.val_var = {0};')
|
2020-07-25 00:02:44 +02:00
|
|
|
}
|
|
|
|
for field in fields {
|
2021-01-05 15:11:43 +01:00
|
|
|
g.comp_for_field_var = node.val_var
|
|
|
|
g.comp_for_field_value = field
|
2021-03-01 01:01:56 +01:00
|
|
|
g.writeln('/* field $i */ {')
|
2020-12-03 16:02:48 +01:00
|
|
|
g.writeln('\t${node.val_var}.name = _SLIT("$field.name");')
|
2020-07-25 00:02:44 +02:00
|
|
|
if field.attrs.len == 0 {
|
|
|
|
g.writeln('\t${node.val_var}.attrs = __new_array_with_default(0, 0, sizeof(string), 0);')
|
|
|
|
} else {
|
2020-08-10 02:00:14 +02:00
|
|
|
attrs := cgen_attrs(field.attrs)
|
2021-01-23 09:33:22 +01:00
|
|
|
g.writeln(
|
|
|
|
'\t${node.val_var}.attrs = new_array_from_c_array($attrs.len, $attrs.len, sizeof(string), _MOV((string[$attrs.len]){' +
|
2020-07-25 18:58:23 +02:00
|
|
|
attrs.join(', ') + '}));')
|
2020-07-25 00:02:44 +02:00
|
|
|
}
|
2020-08-27 15:00:44 +02:00
|
|
|
// field_sym := g.table.get_type_symbol(field.typ)
|
2020-12-03 16:02:48 +01:00
|
|
|
// g.writeln('\t${node.val_var}.typ = _SLIT("$field_sym.name");')
|
2020-08-27 15:00:44 +02:00
|
|
|
styp := field.typ
|
2020-09-28 06:13:38 +02:00
|
|
|
g.writeln('\t${node.val_var}.typ = $styp;')
|
2020-07-25 00:02:44 +02:00
|
|
|
g.writeln('\t${node.val_var}.is_pub = $field.is_pub;')
|
|
|
|
g.writeln('\t${node.val_var}.is_mut = $field.is_mut;')
|
2020-09-28 06:13:38 +02:00
|
|
|
g.comptime_var_type_map['${node.val_var}.typ'] = styp
|
2020-07-25 00:02:44 +02:00
|
|
|
g.stmts(node.stmts)
|
|
|
|
i++
|
2021-03-01 01:01:56 +01:00
|
|
|
g.writeln('}')
|
2020-07-23 17:19:37 +02:00
|
|
|
}
|
2020-07-25 18:58:23 +02:00
|
|
|
g.comptime_var_type_map.delete(node.val_var)
|
2020-07-05 16:44:23 +02:00
|
|
|
}
|
2020-07-03 15:10:39 +02:00
|
|
|
}
|
2021-03-01 01:01:56 +01:00
|
|
|
g.indent--
|
|
|
|
g.writeln('}// \$for')
|
2020-07-03 15:10:39 +02:00
|
|
|
}
|
2021-02-11 01:22:49 +01:00
|
|
|
|
|
|
|
fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) ?string {
|
|
|
|
match name {
|
|
|
|
// platforms/os-es:
|
|
|
|
'windows' {
|
|
|
|
return '_WIN32'
|
|
|
|
}
|
|
|
|
'ios' {
|
|
|
|
return '__TARGET_IOS__'
|
|
|
|
}
|
|
|
|
'macos' {
|
|
|
|
return '__APPLE__'
|
|
|
|
}
|
|
|
|
'mach' {
|
|
|
|
return '__MACH__'
|
|
|
|
}
|
|
|
|
'darwin' {
|
|
|
|
return '__DARWIN__'
|
|
|
|
}
|
|
|
|
'hpux' {
|
|
|
|
return '__HPUX__'
|
|
|
|
}
|
|
|
|
'gnu' {
|
|
|
|
return '__GNU__'
|
|
|
|
}
|
|
|
|
'qnx' {
|
|
|
|
return '__QNX__'
|
|
|
|
}
|
|
|
|
'linux' {
|
|
|
|
return '__linux__'
|
|
|
|
}
|
|
|
|
'freebsd' {
|
|
|
|
return '__FreeBSD__'
|
|
|
|
}
|
|
|
|
'openbsd' {
|
|
|
|
return '__OpenBSD__'
|
|
|
|
}
|
|
|
|
'netbsd' {
|
|
|
|
return '__NetBSD__'
|
|
|
|
}
|
|
|
|
'bsd' {
|
|
|
|
return '__BSD__'
|
|
|
|
}
|
|
|
|
'dragonfly' {
|
|
|
|
return '__DragonFly__'
|
|
|
|
}
|
|
|
|
'android' {
|
|
|
|
return '__ANDROID__'
|
|
|
|
}
|
|
|
|
'solaris' {
|
|
|
|
return '__sun'
|
|
|
|
}
|
|
|
|
'haiku' {
|
|
|
|
return '__haiku__'
|
|
|
|
}
|
|
|
|
'linux_or_macos' {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
//
|
|
|
|
'js' {
|
|
|
|
return '_VJS'
|
|
|
|
}
|
|
|
|
// compilers:
|
|
|
|
'gcc' {
|
|
|
|
return '__V_GCC__'
|
|
|
|
}
|
|
|
|
'tinyc' {
|
|
|
|
return '__TINYC__'
|
|
|
|
}
|
|
|
|
'clang' {
|
|
|
|
return '__clang__'
|
|
|
|
}
|
|
|
|
'mingw' {
|
|
|
|
return '__MINGW32__'
|
|
|
|
}
|
|
|
|
'msvc' {
|
|
|
|
return '_MSC_VER'
|
|
|
|
}
|
|
|
|
'cplusplus' {
|
|
|
|
return '__cplusplus'
|
|
|
|
}
|
|
|
|
// other:
|
|
|
|
'debug' {
|
|
|
|
return '_VDEBUG'
|
|
|
|
}
|
2021-03-10 18:26:34 +01:00
|
|
|
'prod' {
|
|
|
|
return '_VPROD'
|
|
|
|
}
|
2021-02-11 01:22:49 +01:00
|
|
|
'test' {
|
|
|
|
return '_VTEST'
|
|
|
|
}
|
|
|
|
'glibc' {
|
|
|
|
return '__GLIBC__'
|
|
|
|
}
|
|
|
|
'prealloc' {
|
|
|
|
return '_VPREALLOC'
|
|
|
|
}
|
|
|
|
'no_bounds_checking' {
|
|
|
|
return 'CUSTOM_DEFINE_no_bounds_checking'
|
|
|
|
}
|
|
|
|
// architectures:
|
|
|
|
'amd64' {
|
|
|
|
return '__V_amd64'
|
|
|
|
}
|
|
|
|
'aarch64' {
|
|
|
|
return '__V_aarch64'
|
|
|
|
}
|
|
|
|
// bitness:
|
|
|
|
'x64' {
|
|
|
|
return 'TARGET_IS_64BIT'
|
|
|
|
}
|
|
|
|
'x32' {
|
|
|
|
return 'TARGET_IS_32BIT'
|
|
|
|
}
|
|
|
|
// endianness:
|
|
|
|
'little_endian' {
|
|
|
|
return 'TARGET_ORDER_IS_LITTLE'
|
|
|
|
}
|
|
|
|
'big_endian' {
|
|
|
|
return 'TARGET_ORDER_IS_BIG'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if is_comptime_optional
|
|
|
|
|| (g.pref.compile_defines_all.len > 0 && name in g.pref.compile_defines_all) {
|
|
|
|
return 'CUSTOM_DEFINE_$name'
|
|
|
|
}
|
|
|
|
return error('bad os ifdef name "$name"') // should never happen, caught in the checker
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|