1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: typ()

This commit is contained in:
Alexander Medvednikov 2020-03-06 22:36:51 +01:00
parent 1a8b7d0447
commit 124f754443

View File

@ -46,9 +46,22 @@ pub fn (g mut Gen) init() {
} }
// V type to C type // V type to C type
pub fn (g &Gen) typ(t string) string { pub fn (g &Gen) typ(t table.Type) string {
nr_muls := table.type_nr_muls(t)
sym := g.table.get_type_symbol(t)
mut styp := sym.name.replace_each(['.', '__'])
if nr_muls > 0 {
styp += strings.repeat(`*`, nr_muls)
}
return styp
}
/*
pub fn (g &Gen) styp(t string) string {
return t.replace_each(['.', '__']) return t.replace_each(['.', '__'])
} }
*/
pub fn (g mut Gen) write_array_types() { pub fn (g mut Gen) write_array_types() {
for typ in g.table.types { for typ in g.table.types {
@ -212,7 +225,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
g.writeln('}') g.writeln('}')
} }
ast.GlobalDecl { ast.GlobalDecl {
styp := g.typ(g.table.get_type_symbol(it.typ).name) styp := g.typ(it.typ)
g.definitions.writeln('$styp $it.name; // global') g.definitions.writeln('$styp $it.name; // global')
} }
ast.GotoLabel { ast.GotoLabel {
@ -262,8 +275,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
g.stmts(it.stmts) g.stmts(it.stmts)
} }
ast.VarDecl { ast.VarDecl {
type_sym := g.table.get_type_symbol(it.typ) styp := g.typ(it.typ)
styp := type_sym.name.replace('.', '__')
g.write('$styp $it.name = ') g.write('$styp $it.name = ')
g.expr(it.expr) g.expr(it.expr)
g.writeln(';') g.writeln(';')
@ -284,14 +296,13 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
g.write('int ${it.name}(') g.write('int ${it.name}(')
} }
else { else {
type_sym := g.table.get_type_symbol(it.typ)
mut name := it.name mut name := it.name
if it.is_method { if it.is_method {
name = g.table.get_type_symbol(it.receiver.typ).name + '_' + name name = g.table.get_type_symbol(it.receiver.typ).name + '_' + name
} }
name = name.replace('.', '__') name = name.replace('.', '__')
// type_name := g.table.type_to_str(it.typ) // type_name := g.table.type_to_str(it.typ)
type_name := type_sym.name.replace('.', '__') // g.table.type_to_str(it.typ) type_name := g.typ(it.typ)
g.write('$type_name ${name}(') g.write('$type_name ${name}(')
g.definitions.write('$type_name ${name}(') g.definitions.write('$type_name ${name}(')
} }
@ -564,8 +575,7 @@ fn (g mut Gen) expr(node ast.Expr) {
} }
// `user := User{name: 'Bob'}` // `user := User{name: 'Bob'}`
ast.StructInit { ast.StructInit {
type_sym := g.table.get_type_symbol(it.typ) styp := g.typ(it.typ)
styp := g.typ(type_sym.name)
g.writeln('($styp){') g.writeln('($styp){')
for i, field in it.fields { for i, field in it.fields {
g.write('\t.$field = ') g.write('\t.$field = ')
@ -642,7 +652,6 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
fn (g mut Gen) const_decl(node ast.ConstDecl) { fn (g mut Gen) const_decl(node ast.ConstDecl) {
for i, field in node.fields { for i, field in node.fields {
field_type_sym := g.table.get_type_symbol(field.typ)
name := field.name.replace('.', '__') name := field.name.replace('.', '__')
expr := node.exprs[i] expr := node.exprs[i]
match expr { match expr {
@ -656,7 +665,7 @@ fn (g mut Gen) const_decl(node ast.ConstDecl) {
g.writeln('') g.writeln('')
} }
else { else {
styp := g.typ(field_type_sym.name) styp := g.typ(field.typ)
g.definitions.writeln('$styp $name; // inited later') // = ') g.definitions.writeln('$styp $name; // inited later') // = ')
// TODO // TODO
// g.expr(node.exprs[i]) // g.expr(node.exprs[i])