Commit f4f655d2 authored by Jürg Billeter's avatar Jürg Billeter

Replace CCodeDeclarationSpace by CCodeFile

parent ceb6c387
...@@ -32,6 +32,7 @@ libvalaccode_la_VALASOURCES = \ ...@@ -32,6 +32,7 @@ libvalaccode_la_VALASOURCES = \
valaccodeenumvalue.vala \ valaccodeenumvalue.vala \
valaccodeexpression.vala \ valaccodeexpression.vala \
valaccodeexpressionstatement.vala \ valaccodeexpressionstatement.vala \
valaccodefile.vala \
valaccodeformalparameter.vala \ valaccodeformalparameter.vala \
valaccodeforstatement.vala \ valaccodeforstatement.vala \
valaccodefragment.vala \ valaccodefragment.vala \
......
/* valaccodedeclarationspace.vala /* valaccodefile.vala
* *
* Copyright (C) 2009 Jürg Billeter * Copyright (C) 2009-2010 Jürg Billeter
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -21,16 +21,18 @@ ...@@ -21,16 +21,18 @@
*/ */
public class Vala.CCodeDeclarationSpace { public class Vala.CCodeFile {
public bool is_header { get; set; } public bool is_header { get; set; }
Set<string> declarations = new HashSet<string> (str_hash, str_equal); Set<string> declarations = new HashSet<string> (str_hash, str_equal);
Set<string> includes = new HashSet<string> (str_hash, str_equal); Set<string> includes = new HashSet<string> (str_hash, str_equal);
internal CCodeFragment include_directives = new CCodeFragment (); CCodeFragment comments = new CCodeFragment ();
internal CCodeFragment type_declaration = new CCodeFragment (); CCodeFragment include_directives = new CCodeFragment ();
internal CCodeFragment type_definition = new CCodeFragment (); CCodeFragment type_declaration = new CCodeFragment ();
internal CCodeFragment type_member_declaration = new CCodeFragment (); CCodeFragment type_definition = new CCodeFragment ();
internal CCodeFragment constant_declaration = new CCodeFragment (); CCodeFragment type_member_declaration = new CCodeFragment ();
CCodeFragment constant_declaration = new CCodeFragment ();
CCodeFragment type_member_definition = new CCodeFragment ();
public bool add_declaration (string name) { public bool add_declaration (string name) {
if (name in declarations) { if (name in declarations) {
...@@ -40,6 +42,10 @@ public class Vala.CCodeDeclarationSpace { ...@@ -40,6 +42,10 @@ public class Vala.CCodeDeclarationSpace {
return false; return false;
} }
public void add_comment (CCodeComment comment) {
comments.append (comment);
}
public void add_include (string filename, bool local = false) { public void add_include (string filename, bool local = false) {
if (!(filename in includes)) { if (!(filename in includes)) {
include_directives.append (new CCodeIncludeDirective (filename, local)); include_directives.append (new CCodeIncludeDirective (filename, local));
...@@ -62,5 +68,106 @@ public class Vala.CCodeDeclarationSpace { ...@@ -62,5 +68,106 @@ public class Vala.CCodeDeclarationSpace {
public void add_constant_declaration (CCodeNode node) { public void add_constant_declaration (CCodeNode node) {
constant_declaration.append (node); constant_declaration.append (node);
} }
public void add_type_member_definition (CCodeNode node) {
type_member_definition.append (node);
}
public void add_function (CCodeFunction func) {
type_member_definition.append (func);
}
public List<string> get_symbols () {
var symbols = new ArrayList<string> ();
foreach (CCodeNode node in type_member_declaration.get_children ()) {
var func = node as CCodeFunction;
if (func != null) {
symbols.add (func.name);
}
}
return symbols;
}
static string get_define_for_filename (string filename) {
var define = new StringBuilder ("__");
var i = filename;
while (i.length > 0) {
var c = i.get_char ();
if (c.isalnum () && c < 0x80) {
define.append_unichar (c.toupper ());
} else {
define.append_c ('_');
}
i = i.next_char ();
}
define.append ("__");
return define.str;
}
public bool store (string filename, string? source_filename, bool write_version, bool line_directives, string? begin_decls = null, string? end_decls = null) {
var writer = new CCodeWriter (filename, source_filename);
if (!writer.open (write_version)) {
return false;
}
if (!is_header) {
writer.line_directives = line_directives;
comments.write (writer);
writer.write_newline ();
include_directives.write (writer);
writer.write_newline ();
type_declaration.write_combined (writer);
writer.write_newline ();
type_definition.write_combined (writer);
writer.write_newline ();
type_member_declaration.write_declaration (writer);
writer.write_newline ();
type_member_declaration.write (writer);
writer.write_newline ();
constant_declaration.write_combined (writer);
writer.write_newline ();
type_member_definition.write (writer);
writer.write_newline ();
} else {
writer.write_newline ();
var once = new CCodeOnceSection (get_define_for_filename (writer.filename));
once.append (new CCodeNewline ());
once.append (include_directives);
once.append (new CCodeNewline ());
if (begin_decls != null) {
once.append (new CCodeIdentifier (begin_decls));
once.append (new CCodeNewline ());
}
once.append (new CCodeNewline ());
once.append (type_declaration);
once.append (new CCodeNewline ());
once.append (type_definition);
once.append (new CCodeNewline ());
once.append (type_member_declaration);
once.append (new CCodeNewline ());
once.append (constant_declaration);
once.append (new CCodeNewline ());
if (begin_decls != null) {
once.append (new CCodeIdentifier (end_decls));
once.append (new CCodeNewline ());
}
once.append (new CCodeNewline ());
once.write (writer);
}
writer.close ();
return true;
}
} }
...@@ -20,7 +20,6 @@ libvala_la_VALASOURCES = \ ...@@ -20,7 +20,6 @@ libvala_la_VALASOURCES = \
valaccodebasemodule.vala \ valaccodebasemodule.vala \
valaccodecompiler.vala \ valaccodecompiler.vala \
valaccodecontrolflowmodule.vala \ valaccodecontrolflowmodule.vala \
valaccodedeclarationspace.vala \
valaccodedelegatemodule.vala \ valaccodedelegatemodule.vala \
valaccodememberaccessmodule.vala \ valaccodememberaccessmodule.vala \
valaccodemethodcallmodule.vala \ valaccodemethodcallmodule.vala \
......
...@@ -61,7 +61,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -61,7 +61,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
CCodeFunctionCall gnew; CCodeFunctionCall gnew;
if (context.profile == Profile.POSIX) { if (context.profile == Profile.POSIX) {
source_declarations.add_include ("stdlib.h"); cfile.add_include ("stdlib.h");
gnew = new CCodeFunctionCall (new CCodeIdentifier ("calloc")); gnew = new CCodeFunctionCall (new CCodeIdentifier ("calloc"));
} else { } else {
gnew = new CCodeFunctionCall (new CCodeIdentifier ("g_new0")); gnew = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
...@@ -479,7 +479,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -479,7 +479,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
public override string? append_struct_array_free (Struct st) { public override string? append_struct_array_free (Struct st) {
string cname = "_vala_%s_array_free".printf (st.get_cname ());; string cname = "_vala_%s_array_free".printf (st.get_cname ());;
if (source_declarations.add_declaration (cname)) { if (cfile.add_declaration (cname)) {
return cname; return cname;
} }
...@@ -487,7 +487,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -487,7 +487,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
fun.modifiers = CCodeModifiers.STATIC; fun.modifiers = CCodeModifiers.STATIC;
fun.add_parameter (new CCodeFormalParameter ("array", "%s*".printf (st.get_cname ()))); fun.add_parameter (new CCodeFormalParameter ("array", "%s*".printf (st.get_cname ())));
fun.add_parameter (new CCodeFormalParameter ("array_length", "gint")); fun.add_parameter (new CCodeFormalParameter ("array_length", "gint"));
source_declarations.add_type_member_declaration (fun.copy ()); cfile.add_type_member_declaration (fun.copy ());
var cdofree = new CCodeBlock (); var cdofree = new CCodeBlock ();
...@@ -506,7 +506,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -506,7 +506,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
carrfree.add_argument (new CCodeIdentifier ("array")); carrfree.add_argument (new CCodeIdentifier ("array"));
fun.block.add_statement (new CCodeExpressionStatement (carrfree)); fun.block.add_statement (new CCodeExpressionStatement (carrfree));
source_type_member_definition.append (fun); cfile.add_function (fun);
return cname; return cname;
} }
...@@ -540,7 +540,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -540,7 +540,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
fun.add_parameter (new CCodeFormalParameter ("array", "gpointer")); fun.add_parameter (new CCodeFormalParameter ("array", "gpointer"));
fun.add_parameter (new CCodeFormalParameter ("array_length", "gint")); fun.add_parameter (new CCodeFormalParameter ("array_length", "gint"));
fun.add_parameter (new CCodeFormalParameter ("destroy_func", "GDestroyNotify")); fun.add_parameter (new CCodeFormalParameter ("destroy_func", "GDestroyNotify"));
source_declarations.add_type_member_declaration (fun.copy ()); cfile.add_type_member_declaration (fun.copy ());
var cdofree = new CCodeBlock (); var cdofree = new CCodeBlock ();
...@@ -556,7 +556,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -556,7 +556,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
fun.block = new CCodeBlock (); fun.block = new CCodeBlock ();
fun.block.add_statement (cif); fun.block.add_statement (cif);
source_type_member_definition.append (fun); cfile.add_function (fun);
// _vala_array_free frees elements and array // _vala_array_free frees elements and array
...@@ -565,7 +565,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -565,7 +565,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
fun.add_parameter (new CCodeFormalParameter ("array", "gpointer")); fun.add_parameter (new CCodeFormalParameter ("array", "gpointer"));
fun.add_parameter (new CCodeFormalParameter ("array_length", "gint")); fun.add_parameter (new CCodeFormalParameter ("array_length", "gint"));
fun.add_parameter (new CCodeFormalParameter ("destroy_func", "GDestroyNotify")); fun.add_parameter (new CCodeFormalParameter ("destroy_func", "GDestroyNotify"));
source_declarations.add_type_member_declaration (fun.copy ()); cfile.add_type_member_declaration (fun.copy ());
// call _vala_array_destroy to free the array elements // call _vala_array_destroy to free the array elements
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("_vala_array_destroy")); var ccall = new CCodeFunctionCall (new CCodeIdentifier ("_vala_array_destroy"));
...@@ -580,11 +580,11 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -580,11 +580,11 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
carrfree.add_argument (new CCodeIdentifier ("array")); carrfree.add_argument (new CCodeIdentifier ("array"));
fun.block.add_statement (new CCodeExpressionStatement (carrfree)); fun.block.add_statement (new CCodeExpressionStatement (carrfree));
source_type_member_definition.append (fun); cfile.add_function (fun);
} }
public override void append_vala_array_move () { public override void append_vala_array_move () {
source_declarations.add_include ("string.h"); cfile.add_include ("string.h");
// assumes that overwritten array elements are null before invocation // assumes that overwritten array elements are null before invocation
// FIXME will leak memory if that's not the case // FIXME will leak memory if that's not the case
...@@ -595,7 +595,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -595,7 +595,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
fun.add_parameter (new CCodeFormalParameter ("src", "gint")); fun.add_parameter (new CCodeFormalParameter ("src", "gint"));
fun.add_parameter (new CCodeFormalParameter ("dest", "gint")); fun.add_parameter (new CCodeFormalParameter ("dest", "gint"));
fun.add_parameter (new CCodeFormalParameter ("length", "gint")); fun.add_parameter (new CCodeFormalParameter ("length", "gint"));
source_declarations.add_type_member_declaration (fun.copy ()); cfile.add_type_member_declaration (fun.copy ());
var array = new CCodeCastExpression (new CCodeIdentifier ("array"), "char*"); var array = new CCodeCastExpression (new CCodeIdentifier ("array"), "char*");
var element_size = new CCodeIdentifier ("element_size"); var element_size = new CCodeIdentifier ("element_size");
...@@ -630,14 +630,14 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -630,14 +630,14 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
fun.block.add_statement (new CCodeIfStatement (new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, src, dest), czeroblock1, czeroblock2)); fun.block.add_statement (new CCodeIfStatement (new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, src, dest), czeroblock1, czeroblock2));
source_type_member_definition.append (fun); cfile.add_function (fun);
} }
public override void append_vala_array_length () { public override void append_vala_array_length () {
var fun = new CCodeFunction ("_vala_array_length", "gint"); var fun = new CCodeFunction ("_vala_array_length", "gint");
fun.modifiers = CCodeModifiers.STATIC; fun.modifiers = CCodeModifiers.STATIC;
fun.add_parameter (new CCodeFormalParameter ("array", "gpointer")); fun.add_parameter (new CCodeFormalParameter ("array", "gpointer"));
source_declarations.add_type_member_declaration (fun.copy ()); cfile.add_type_member_declaration (fun.copy ());
var block = new CCodeBlock (); var block = new CCodeBlock ();
...@@ -662,7 +662,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -662,7 +662,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
fun.block = block; fun.block = block;
source_type_member_definition.append (fun); cfile.add_function (fun);
} }
public override CCodeExpression? get_ref_cexpression (DataType expression_type, CCodeExpression cexpr, Expression? expr, CodeNode node) { public override CCodeExpression? get_ref_cexpression (DataType expression_type, CCodeExpression cexpr, Expression? expr, CodeNode node) {
...@@ -805,10 +805,10 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -805,10 +805,10 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
// append to file // append to file
source_declarations.add_type_member_declaration (function.copy ()); cfile.add_type_member_declaration (function.copy ());
function.block = block; function.block = block;
source_type_member_definition.append (function); cfile.add_function (function);
return dup_func; return dup_func;
} }
...@@ -854,7 +854,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -854,7 +854,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
pop_context (); pop_context ();
} else { } else {
source_declarations.add_include ("string.h"); cfile.add_include ("string.h");
var dup_call = new CCodeFunctionCall (new CCodeIdentifier ("memcpy")); var dup_call = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
dup_call.add_argument (new CCodeIdentifier ("dest")); dup_call.add_argument (new CCodeIdentifier ("dest"));
...@@ -869,10 +869,10 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -869,10 +869,10 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
// append to file // append to file
source_declarations.add_type_member_declaration (function.copy ()); cfile.add_type_member_declaration (function.copy ());
function.block = block; function.block = block;
source_type_member_definition.append (function); cfile.add_function (function);
return dup_func; return dup_func;
} }
...@@ -941,10 +941,10 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -941,10 +941,10 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
// append to file // append to file
source_declarations.add_type_member_declaration (function.copy ()); cfile.add_type_member_declaration (function.copy ());
function.block = block; function.block = block;
source_type_member_definition.append (function); cfile.add_function (function);
return add_func; return add_func;
} }
...@@ -985,7 +985,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { ...@@ -985,7 +985,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
assignment.ccodenode = ccall; assignment.ccodenode = ccall;
} }
public override void generate_parameter (FormalParameter param, CCodeDeclarationSpace decl_space, Map<int,CCodeFormalParameter> cparam_map, Map<int,CCodeExpression>? carg_map) { public override void generate_parameter (FormalParameter param, CCodeFile decl_space, Map<int,CCodeFormalParameter> cparam_map, Map<int,CCodeExpression>? carg_map) {
if (!(param.variable_type is ArrayType)) { if (!(param.variable_type is ArrayType)) {
base.generate_parameter (param, decl_space, cparam_map, carg_map); base.generate_parameter (param, decl_space, cparam_map, carg_map);
return; return;
......
...@@ -34,7 +34,7 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule { ...@@ -34,7 +34,7 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
var prop = (Property) assignment.left.symbol_reference; var prop = (Property) assignment.left.symbol_reference;
if (!(prop is DynamicProperty)) { if (!(prop is DynamicProperty)) {
generate_property_accessor_declaration (prop.set_accessor, source_declarations); generate_property_accessor_declaration (prop.set_accessor, cfile);
if (!prop.external && prop.external_package) { if (!prop.external && prop.external_package) {
// internal VAPI properties // internal VAPI properties
...@@ -212,7 +212,7 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule { ...@@ -212,7 +212,7 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
CCodeExpression rhs = (CCodeExpression) assignment.right.ccodenode; CCodeExpression rhs = (CCodeExpression) assignment.right.ccodenode;
CCodeExpression lhs = (CCodeExpression) get_ccodenode (assignment.left); CCodeExpression lhs = (CCodeExpression) get_ccodenode (assignment.left);
source_declarations.add_include ("string.h"); cfile.add_include ("string.h");
// it is necessary to use memcpy for fixed-length (stack-allocated) arrays // it is necessary to use memcpy for fixed-length (stack-allocated) arrays
// simple assignments do not work in C // simple assignments do not work in C
......
...@@ -148,19 +148,16 @@ public class Vala.CCodeBaseModule : CodeGenerator { ...@@ -148,19 +148,16 @@ public class Vala.CCodeBaseModule : CodeGenerator {
return block; return block;
} }
public CCodeDeclarationSpace header_declarations; public CCodeFile header_file;
public CCodeDeclarationSpace internal_header_declarations; public CCodeFile internal_header_file;
public CCodeDeclarationSpace source_declarations; public CCodeFile cfile;
public CCodeFragment source_signal_marshaller_declaration;
public CCodeFragment source_type_member_definition;
public CCodeFragment class_init_fragment; public CCodeFragment class_init_fragment;
public CCodeFragment base_init_fragment; public CCodeFragment base_init_fragment;
public CCodeFragment class_finalize_fragment; public CCodeFragment class_finalize_fragment;
public CCodeFragment base_finalize_fragment; public CCodeFragment base_finalize_fragment;
public CCodeFragment instance_init_fragment; public CCodeFragment instance_init_fragment;
public CCodeFragment instance_finalize_fragment; public CCodeFragment instance_finalize_fragment;
public CCodeFragment source_signal_marshaller_definition;
public CCodeStruct param_spec_struct; public CCodeStruct param_spec_struct;
public CCodeStruct closure_struct; public CCodeStruct closure_struct;
...@@ -412,10 +409,10 @@ public class Vala.CCodeBaseModule : CodeGenerator { ...@@ -412,10 +409,10 @@ public class Vala.CCodeBaseModule : CodeGenerator {
} }
} }
header_declarations = new CCodeDeclarationSpace (); header_file = new CCodeFile ();
header_declarations.is_header = true; header_file.is_header = true;
internal_header_declarations = new CCodeDeclarationSpace (); internal_header_file = new CCodeFile ();
internal_header_declarations.is_header = true; internal_header_file.is_header = true;
/* we're only interested in non-pkg source files */ /* we're only interested in non-pkg source files */
var source_files = context.get_source_files (); var source_files = context.get_source_files ();
...@@ -433,12 +430,9 @@ public class Vala.CCodeBaseModule : CodeGenerator { ...@@ -433,12 +430,9 @@ public class Vala.CCodeBaseModule : CodeGenerator {
return; return;
} }
foreach (CCodeNode node in header_declarations.type_member_declaration.get_children ()) { foreach (string symbol in header_file.get_symbols ()) {
if (node is CCodeFunction) { stream.puts (symbol);
var func = (CCodeFunction) node; stream.putc ('\n');
stream.puts (func.name);
stream.putc ('\n');
}
} }
stream = null; stream = null;
...@@ -446,80 +440,28 @@ public class Vala.CCodeBaseModule : CodeGenerator { ...@@ -446,80 +440,28 @@ public class Vala.CCodeBaseModule : CodeGenerator {
// generate C header file for public API // generate C header file for public API
if (context.header_filename != null) { if (context.header_filename != null) {
var writer = new CCodeWriter (context.header_filename); bool ret;
if (!writer.open (context.version_header)) {
Report.error (null, "unable to open `%s' for writing".printf (writer.filename));
return;
}
writer.write_newline ();
var once = new CCodeOnceSection (get_define_for_filename (writer.filename));
once.append (new CCodeNewline ());
once.append (header_declarations.include_directives);
once.append (new CCodeNewline ());
if (context.profile == Profile.GOBJECT) { if (context.profile == Profile.GOBJECT) {
once.append (new CCodeIdentifier ("G_BEGIN_DECLS")); ret = header_file.store (context.header_filename, null, context.version_header, false, "G_BEGIN_DECLS", "G_END_DECLS");
once.append (new CCodeNewline ()); } else {
ret = header_file.store (context.header_filename, null, context.version_header, false);
} }
if (!ret) {
once.append (new CCodeNewline ()); Report.error (null, "unable to open `%s' for writing".printf (context.header_filename));
once.append (header_declarations.type_declaration);
once.append (new CCodeNewline ());
once.append (header_declarations.type_definition);
once.append (new CCodeNewline ());
once.append (header_declarations.type_member_declaration);
once.append (new CCodeNewline ());
once.append (header_declarations.constant_declaration);
once.append (new CCodeNewline ());
if (context.profile == Profile.GOBJECT) {
once.append (new CCodeIdentifier ("G_END_DECLS"));
once.append (new CCodeNewline ());
} }
once.append (new CCodeNewline ());
once.write (writer);
writer.close ();
} }
// generate C header file for internal API // generate C header file for internal API
if (context.internal_header_filename != null) { if (context.internal_header_filename != null) {
var writer = new CCodeWriter (context.internal_header_filename); bool ret;
if (!writer.open (context.version_header)) {
Report.error (null, "unable to open `%s' for writing".printf (writer.filename));
return;
}
writer.write_newline ();
var once = new CCodeOnceSection (get_define_for_filename (writer.filename));
once.append (new CCodeNewline ());
once.append (internal_header_declarations.include_directives);
once.append (new CCodeNewline ());
if (context.profile == Profile.GOBJECT) { if (context.profile == Profile.GOBJECT) {
once.append (new CCodeIdentifier ("G_BEGIN_DECLS")); ret = internal_header_file.store (context.internal_header_filename, null, context.version_header, false, "G_BEGIN_DECLS", "G_END_DECLS");
once.append (new CCodeNewline ()); } else {
ret = internal_header_file.store (context.internal_header_filename, null, context.version_header, false);
} }
if (!ret) {
once.append (new CCodeNewline ()); Report.error (null, "unable to open `%s' for writing".printf (context.internal_header_filename));
once.append (internal_header_declarations.type_declaration);
once.append (new CCodeNewline ());
once.append (internal_header_declarations.type_definition);
once.append (new CCodeNewline ());
once.append (internal_header_declarations.type_member_declaration);
once.append (new CCodeNewline ());
once.append (internal_header_declarations.constant_declaration);
once.append (new CCodeNewline ());
if (context.profile == Profile.GOBJECT) {
once.append (new CCodeIdentifier ("G_END_DECLS"));
once.append (new CCodeNewline ());
} }
once.append (new CCodeNewline ());
once.write (writer);
writer.close ();
} }
} }
...@@ -540,7 +482,7 @@ public class Vala.CCodeBaseModule : CodeGenerator { ...@@ -540,7 +482,7 @@ public class Vala.CCodeBaseModule : CodeGenerator {
} }
} }
public bool add_symbol_declaration (CCodeDeclarationSpace decl_space, Symbol sym, string name) { public bool add_symbol_declaration (CCodeFile decl_space, Symbol sym, string name) {
if (decl_space.add_declaration (name)) { if (decl_space.add_declaration (name)) {
return true; return true;
} }
...@@ -603,13 +545,13 @@ public class Vala.CCodeBaseModule : CodeGenerator { ...@@ -603,13 +545,13 @@ public class Vala.CCodeBaseModule : CodeGenerator {
} }
private void append_vala_strcmp0 () { private void append_vala_strcmp0 () {
source_declarations.add_include ("string.h");; cfile.add_include ("string.h");;
var fun = new CCodeFunction ("_vala_strcmp0", "int"); var fun = new CCodeFunction ("_vala_strcmp0", "int");
fun.modifiers = CCodeModifiers.STATIC; fun.modifiers = CCodeModifiers.STATIC;
fun.add_parameter (new CCodeFormalParameter ("str1", "const char *")); fun.add_parameter (new CCodeFormalParameter ("str1", "const char *"));
fun.add_parameter (new CCodeFormalParameter ("str2", "const char *")); fun.add_parameter (new CCodeFormalParameter ("str2", "const char *"));
source_declarations.add_type_member_declaration (fun.copy ()); cfile.add_type_member_declaration (fun.copy ());
// (str1 != str2) // (str1 != str2)
var cineq = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier ("str1"), new CCodeIdentifier ("str2")); var cineq = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier ("str1"), new CCodeIdentifier ("str2"));
...@@ -637,14 +579,11 @@ public class Vala.CCodeBaseModule : CodeGenerator { ...@@ -637,14 +579,11 @@ public class Vala.CCodeBaseModule : CodeGenerator {
// return strcmp (str1, str2); // return strcmp (str1, str2);
fun.block.add_statement (new CCodeReturnStatement (ccall)); fun.block.add_statement (new CCodeReturnStatement (ccall));
source_type_member_definition.append (fun); cfile.add_function (fun);
} }
public override void visit_source_file (SourceFile source_file) { public override void visit_source_file (SourceFile source_file) {
source_declarations = new CCodeDeclarationSpace (); cfile = new CCodeFile ();
source_type_member_definition = new CCodeFragment ();
source_signal_marshaller_definition = new CCodeFragment ();
source_signal_marshaller_declaration = new CCodeFragment ();
user_marshal_set = new HashSet<string> (str_hash, str_equal); user_marshal_set = new HashSet<string> (str_hash, str_equal);
...@@ -660,10 +599,10 @@ public class Vala.CCodeBaseModule : CodeGenerator { ...@@ -660,10 +599,10 @@ public class Vala.CCodeBaseModule : CodeGenerator {
generated_external_symbols = new HashSet<Symbol> (); generated_external_symbols = new HashSet<Symbol> ();
if (context.profile == Profile.GOBJECT) { if (context.profile == Profile.GOBJECT) {
header_declarations.add_include ("glib.h");