mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: optimize const definitions
This commit is contained in:
parent
91269961d0
commit
fb785b8adf
@ -143,13 +143,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
||||
g.writeln(';')
|
||||
}
|
||||
ast.ConstDecl {
|
||||
for i, field in it.fields {
|
||||
field_type_sym := g.table.get_type_symbol(field.typ)
|
||||
name := field.name.replace('.', '__')
|
||||
g.write('$field_type_sym.name $name = ')
|
||||
g.expr(it.exprs[i])
|
||||
g.writeln(';')
|
||||
}
|
||||
g.const_decl(it)
|
||||
}
|
||||
ast.CompIf {
|
||||
// TODO
|
||||
@ -609,6 +603,30 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
|
||||
}
|
||||
}
|
||||
|
||||
fn (g mut Gen) const_decl(node ast.ConstDecl) {
|
||||
for i, field in node.fields {
|
||||
field_type_sym := g.table.get_type_symbol(field.typ)
|
||||
name := field.name.replace('.', '__')
|
||||
expr := node.exprs[i]
|
||||
match expr {
|
||||
// Simple expressions should use a #define
|
||||
// so that we don't pollute the binary with unnecessary global vars
|
||||
// Do not do this when building a module, otherwise the consts
|
||||
// will not be accessible.
|
||||
ast.CharLiteral, ast.IntegerLiteral {
|
||||
g.write('#define $name ')
|
||||
g.expr(expr)
|
||||
g.writeln('')
|
||||
}
|
||||
else {
|
||||
g.writeln('$field_type_sym.name $name; // inited later') // = ')
|
||||
// TODO
|
||||
// g.expr(node.exprs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn (g mut Gen) call_args(args []ast.Expr) {
|
||||
for i, expr in args {
|
||||
g.expr(expr)
|
||||
@ -699,7 +717,7 @@ fn (g &Gen) sort_structs(types []table.TypeSymbol) []table.TypeSymbol {
|
||||
}
|
||||
}
|
||||
else {}
|
||||
}
|
||||
}
|
||||
// add type and dependant types to graph
|
||||
dep_graph.add(t.name, field_deps)
|
||||
}
|
||||
|
@ -29,8 +29,8 @@ void matches();
|
||||
void end();
|
||||
void localmod__pub_foo();
|
||||
int localmod__get_int_10();
|
||||
int pi = 3;
|
||||
int pi2 = pi;
|
||||
#define pi 3
|
||||
int pi2; // inited later
|
||||
|
||||
typedef enum {
|
||||
Color_red, // 0
|
||||
@ -155,7 +155,7 @@ void matches() {
|
||||
;
|
||||
}
|
||||
|
||||
int path_sep = 10;
|
||||
#define path_sep 10
|
||||
|
||||
void end() {
|
||||
int i = 2;
|
||||
@ -164,7 +164,7 @@ void end() {
|
||||
int e = 2 + 3 * 4;
|
||||
}
|
||||
|
||||
int localmod__pub_int_const = 20;
|
||||
#define localmod__pub_int_const 20
|
||||
|
||||
void localmod__pub_foo() {
|
||||
int a = 10;
|
||||
|
Loading…
Reference in New Issue
Block a user