Commit e632b60f authored by Luca Bruno's avatar Luca Bruno

codegen: Add push_line and pop_line for writing line directives

Fixes bug 635095.
parent 0d03519f
......@@ -63,6 +63,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
List<EmitContext> emit_context_stack = new ArrayList<EmitContext> ();
public CCodeLineDirective? current_line = null;
List<CCodeLineDirective> line_directive_stack = new ArrayList<CCodeLineDirective> ();
public Symbol current_symbol { get { return emit_context.current_symbol; } }
public TryStatement current_try {
......@@ -512,25 +516,53 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
this.emit_context = emit_context;
if (ccode != null) {
ccode.current_line = current_line;
}
}
public void pop_context () {
if (emit_context_stack.size > 0) {
this.emit_context = emit_context_stack[emit_context_stack.size - 1];
emit_context_stack.remove_at (emit_context_stack.size - 1);
if (ccode != null) {
ccode.current_line = current_line;
}
} else {
this.emit_context = null;
}
}
public void push_line (SourceReference? source_reference) {
line_directive_stack.add (current_line);
if (source_reference != null) {
current_line = new CCodeLineDirective (source_reference.file.filename, source_reference.first_line);
if (ccode != null) {
ccode.current_line = current_line;
}
}
}
public void pop_line () {
current_line = line_directive_stack[line_directive_stack.size - 1];
line_directive_stack.remove_at (line_directive_stack.size - 1);
if (ccode != null) {
ccode.current_line = current_line;
}
}
public void push_function (CCodeFunction func) {
emit_context.ccode_stack.add (ccode);
emit_context.ccode = func;
ccode.current_line = current_line;
}
public void pop_function () {
emit_context.ccode = emit_context.ccode_stack[emit_context.ccode_stack.size - 1];
emit_context.ccode_stack.remove_at (emit_context.ccode_stack.size - 1);
if (ccode != null) {
ccode.current_line = current_line;
}
}
public bool add_symbol_declaration (CCodeFile decl_space, Symbol sym, string name) {
......@@ -717,6 +749,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
public override void visit_enum (Enum en) {
push_line (en.source_reference);
en.accept_children (this);
if (en.comment != null) {
......@@ -731,6 +765,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
if (!en.is_private_symbol ()) {
generate_enum_declaration (en, internal_header_file);
}
pop_line ();
}
public void visit_member (Symbol m) {
......@@ -816,6 +852,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
public override void visit_constant (Constant c) {
push_line (c.source_reference);
if (c.parent_symbol is Block) {
// local constant
......@@ -837,18 +875,18 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
var cinitializer = get_cvalue (c.value);
ccode.add_declaration (type_name, new CCodeVariableDeclarator ("%s%s".printf (c.get_cname (), arr), cinitializer), CCodeModifiers.STATIC);
} else {
generate_constant_declaration (c, cfile, true);
return;
if (!c.is_internal_symbol ()) {
generate_constant_declaration (c, header_file);
}
if (!c.is_private_symbol ()) {
generate_constant_declaration (c, internal_header_file);
}
}
generate_constant_declaration (c, cfile, true);
if (!c.is_internal_symbol ()) {
generate_constant_declaration (c, header_file);
}
if (!c.is_private_symbol ()) {
generate_constant_declaration (c, internal_header_file);
}
pop_line ();
}
public void generate_field_declaration (Field f, CCodeFile decl_space) {
......@@ -935,6 +973,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
public override void visit_field (Field f) {
push_line (f.source_reference);
visit_member (f);
check_type (f.variable_type);
......@@ -1165,6 +1204,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
pop_context ();
}
pop_line ();
}
public bool is_constant_ccode_expression (CCodeExpression cexpr) {
......@@ -1356,6 +1397,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
public override void visit_property_accessor (PropertyAccessor acc) {
push_context (new EmitContext (acc));
push_line (acc.source_reference);
var prop = (Property) acc.prop;
......@@ -1400,6 +1442,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
if (acc.source_type == SourceFileType.FAST) {
pop_line ();
return;
}
......@@ -1618,6 +1661,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
cfile.add_function (function);
}
pop_line ();
pop_context ();
}
......@@ -1938,7 +1982,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
foreach (Statement stmt in b.get_statements ()) {
push_line (stmt.source_reference);
stmt.emit (this);
pop_line ();
}
// free in reverse order
......
......@@ -255,6 +255,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
public override void visit_method (Method m) {
push_context (new EmitContext (m));
push_line (m.source_reference);
bool in_gobject_creation_method = false;
bool in_fundamental_creation_method = false;
......@@ -720,6 +721,8 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
pop_function ();
cfile.add_function (cmain);
}
pop_line ();
}
public virtual CCodeParameter generate_parameter (Parameter param, CCodeFile decl_space, Map<int,CCodeParameter> cparam_map, Map<int,CCodeExpression>? carg_map) {
......@@ -998,6 +1001,8 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
}
public override void visit_creation_method (CreationMethod m) {
push_line (m.source_reference);
bool visible = !m.is_private_symbol ();
visit_method (m);
......@@ -1029,6 +1034,8 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
cfile.add_function (vfunc);
}
pop_line ();
}
}
......
......@@ -147,6 +147,7 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
public override void visit_struct (Struct st) {
push_context (new EmitContext (st));
push_line (st.source_reference);
var old_instance_finalize_context = instance_finalize_context;
instance_finalize_context = new EmitContext ();
......@@ -180,6 +181,7 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
instance_finalize_context = old_instance_finalize_context;
pop_line ();
pop_context ();
}
......
......@@ -34,12 +34,14 @@ public class Vala.GObjectModule : GTypeModule {
return;
}
push_line (cl.source_reference);
if (class_has_readable_properties (cl) || cl.get_type_parameters ().size > 0) {
add_get_property_function (cl);
}
if (class_has_writable_properties (cl) || cl.get_type_parameters ().size > 0) {
add_set_property_function (cl);
}
pop_line ();
}
public override void generate_class_init (Class cl) {
......@@ -398,6 +400,8 @@ public class Vala.GObjectModule : GTypeModule {
}
public override void visit_constructor (Constructor c) {
push_line (c.source_reference);
if (c.binding == MemberBinding.CLASS || c.binding == MemberBinding.STATIC) {
in_static_or_class_context = true;
} else {
......@@ -510,6 +514,8 @@ public class Vala.GObjectModule : GTypeModule {
in_static_or_class_context = false;
in_constructor = false;
pop_line ();
}
public override string get_dynamic_property_getter_cname (DynamicProperty prop) {
......@@ -716,6 +722,8 @@ public class Vala.GObjectModule : GTypeModule {
public override void visit_method_call (MethodCall expr) {
if (expr.call is MemberAccess) {
push_line (expr.source_reference);
var ma = expr.call as MemberAccess;
if (ma.inner != null && ma.inner.symbol_reference == gobject_type &&
(ma.member_name == "new" || ma.member_name == "newv")) {
......@@ -758,6 +766,8 @@ public class Vala.GObjectModule : GTypeModule {
}
}
}
pop_line ();
}
base.visit_method_call (expr);
......
......@@ -525,6 +525,7 @@ public class Vala.GTypeModule : GErrorModule {
public override void visit_class (Class cl) {
push_context (new EmitContext (cl));
push_line (cl.source_reference);
var old_param_spec_struct = param_spec_struct;
var old_prop_enum = prop_enum;
......@@ -604,7 +605,9 @@ public class Vala.GTypeModule : GErrorModule {
add_g_value_take_function (cl);
var ref_count = new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "ref_count"), new CCodeConstant ("1"));
instance_init_context.ccode.add_expression (ref_count);
push_context (instance_init_context);
ccode.add_expression (ref_count);
pop_context ();
}
......@@ -711,6 +714,7 @@ public class Vala.GTypeModule : GErrorModule {
instance_init_context = old_instance_init_context;
instance_finalize_context = old_instance_finalize_context;
pop_line ();
pop_context ();
}
......@@ -1645,7 +1649,9 @@ public class Vala.GTypeModule : GErrorModule {
ccast.add_argument (new CCodeIdentifier ("%s_parent_class".printf (cl.get_lower_case_cname (null))));
var ccall = new CCodeFunctionCall (new CCodeMemberAccess.pointer (ccast, "finalize"));
ccall.add_argument (new CCodeIdentifier ("obj"));
instance_finalize_context.ccode.add_expression (ccall);
push_context (instance_finalize_context);
ccode.add_expression (ccall);
pop_context ();
}
cfile.add_function_declaration (instance_finalize_context.ccode);
......@@ -1653,7 +1659,9 @@ public class Vala.GTypeModule : GErrorModule {
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_free"));
ccall.add_argument (new CCodeIdentifier (cl.get_cname ()));
ccall.add_argument (new CCodeIdentifier ("self"));
instance_finalize_context.ccode.add_expression (ccall);
push_context (instance_finalize_context);
ccode.add_expression (ccall);
pop_context ();
}
cfile.add_function (instance_finalize_context.ccode);
......@@ -1954,6 +1962,7 @@ public class Vala.GTypeModule : GErrorModule {
public override void visit_interface (Interface iface) {
push_context (new EmitContext (iface));
push_line (iface.source_reference);
if (iface.get_cname().length < 3) {
iface.error = true;
......@@ -1982,6 +1991,7 @@ public class Vala.GTypeModule : GErrorModule {
cfile.add_type_member_declaration (type_fun.get_source_declaration ());
cfile.add_type_member_definition (type_fun.get_definition ());
pop_line ();
pop_context ();
}
......@@ -2063,9 +2073,11 @@ public class Vala.GTypeModule : GErrorModule {
base.visit_struct (st);
if (st.has_type_id) {
push_line (st.source_reference);
var type_fun = new StructRegisterFunction (st, context);
type_fun.init_from_type (false, false);
cfile.add_type_member_definition (type_fun.get_definition ());
pop_line ();
}
}
......@@ -2073,9 +2085,11 @@ public class Vala.GTypeModule : GErrorModule {
base.visit_enum (en);
if (en.has_type_id) {
push_line (en.source_reference);
var type_fun = new EnumRegisterFunction (en, context);
type_fun.init_from_type (false, false);
cfile.add_type_member_definition (type_fun.get_definition ());
pop_line ();
}
}
......@@ -2090,6 +2104,7 @@ public class Vala.GTypeModule : GErrorModule {
}
// to_string() on a gtype enum
push_line (expr.source_reference);
var temp_var = get_temp_variable (new CType ("GEnumValue*"), false, expr, false);
emit_temp_var (temp_var);
......@@ -2102,6 +2117,7 @@ public class Vala.GTypeModule : GErrorModule {
ccode.add_assignment (get_variable_cexpression (temp_var.name), get_value);
var is_null_value = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, get_variable_cexpression (temp_var.name), new CCodeIdentifier ("NULL"));
set_cvalue (expr, new CCodeConditionalExpression (is_null_value, new CCodeMemberAccess.pointer (get_variable_cexpression (temp_var.name), "value_name"), new CCodeIdentifier ("NULL")));
pop_line ();
}
public override void visit_property (Property prop) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment