Commit 489895fb authored by Rico Tzschichholz's avatar Rico Tzschichholz

Reintroduce POSIX profile

This reverts commit ca020bf0.
parent f7e70338
......@@ -57,8 +57,14 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
return;
}
var gnew = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
gnew.add_argument (new CCodeIdentifier (get_ccode_name (expr.element_type)));
CCodeFunctionCall gnew;
if (context.profile == Profile.POSIX) {
cfile.add_include ("stdlib.h");
gnew = new CCodeFunctionCall (new CCodeIdentifier ("calloc"));
} else {
gnew = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
gnew.add_argument (new CCodeIdentifier (get_ccode_name (expr.element_type)));
}
bool first = true;
CCodeExpression cexpr = null;
......@@ -83,6 +89,12 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
gnew.add_argument (cexpr);
if (context.profile == Profile.POSIX) {
var csizeof = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
csizeof.add_argument (new CCodeIdentifier (get_ccode_name (expr.element_type)));
gnew.add_argument (csizeof);
}
var temp_var = get_temp_variable (expr.value_type, true, expr);
var name_cnode = get_variable_cexpression (temp_var.name);
int i = 0;
......
......@@ -713,14 +713,30 @@ public class Vala.CCodeAttribute : AttributeCache {
} else if (node is GenericType) {
var type = (GenericType) node;
if (type.value_owned) {
return "gpointer";
if (CodeContext.get ().profile == Profile.GOBJECT) {
return "gpointer";
} else {
return "void *";
}
} else {
return "gconstpointer";
if (CodeContext.get ().profile == Profile.GOBJECT) {
return "gconstpointer";
} else {
return "const void *";
}
}
} else if (node is MethodType) {
return "gpointer";
if (CodeContext.get ().profile == Profile.GOBJECT) {
return "gpointer";
} else {
return "void *";
}
} else if (node is NullType) {
return "gpointer";
if (CodeContext.get ().profile == Profile.GOBJECT) {
return "gpointer";
} else {
return "void *";
}
} else if (node is PointerType) {
var type = (PointerType) node;
if (type.base_type.data_type != null && type.base_type.data_type.is_reference_type ()) {
......
......@@ -454,53 +454,53 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
unichar_type = new IntegerType (unichar_struct);
}
var glib_ns = root_symbol.scope.lookup ("GLib");
gtype_type = (TypeSymbol) glib_ns.scope.lookup ("Type");
gobject_type = (TypeSymbol) glib_ns.scope.lookup ("Object");
gerror_type = new ErrorType (null, null);
glist_type = (Class) glib_ns.scope.lookup ("List");
gslist_type = (Class) glib_ns.scope.lookup ("SList");
gnode_type = (Class) glib_ns.scope.lookup ("Node");
gqueue_type = (Class) glib_ns.scope.lookup ("Queue");
gvaluearray_type = (Class) glib_ns.scope.lookup ("ValueArray");
gstringbuilder_type = (TypeSymbol) glib_ns.scope.lookup ("StringBuilder");
garray_type = (TypeSymbol) glib_ns.scope.lookup ("Array");
gbytearray_type = (TypeSymbol) glib_ns.scope.lookup ("ByteArray");
gptrarray_type = (TypeSymbol) glib_ns.scope.lookup ("PtrArray");
gthreadpool_type = (TypeSymbol) glib_ns.scope.lookup ("ThreadPool");
gdestroynotify_type = new DelegateType ((Delegate) glib_ns.scope.lookup ("DestroyNotify"));
gquark_type = new IntegerType ((Struct) glib_ns.scope.lookup ("Quark"));
gvalue_type = (Struct) glib_ns.scope.lookup ("Value");
gvariant_type = (Class) glib_ns.scope.lookup ("Variant");
gsource_type = (Class) glib_ns.scope.lookup ("Source");
gmutex_type = (Struct) glib_ns.scope.lookup ("Mutex");
grecmutex_type = (Struct) glib_ns.scope.lookup ("RecMutex");
grwlock_type = (Struct) glib_ns.scope.lookup ("RWLock");
gcond_type = (Struct) glib_ns.scope.lookup ("Cond");
mutex_type = grecmutex_type;
type_module_type = (TypeSymbol) glib_ns.scope.lookup ("TypeModule");
regex_type = new ObjectType ((Class) root_symbol.scope.lookup ("GLib").scope.lookup ("Regex"));
if (context.module_init_method != null) {
foreach (Parameter parameter in context.module_init_method.get_parameters ()) {
if (parameter.variable_type.data_type == type_module_type) {
in_plugin = true;
module_init_param_name = parameter.name;
break;
if (context.profile == Profile.GOBJECT) {
var glib_ns = root_symbol.scope.lookup ("GLib");
gtype_type = (TypeSymbol) glib_ns.scope.lookup ("Type");
gobject_type = (TypeSymbol) glib_ns.scope.lookup ("Object");
gerror_type = new ErrorType (null, null);
glist_type = (Class) glib_ns.scope.lookup ("List");
gslist_type = (Class) glib_ns.scope.lookup ("SList");
gnode_type = (Class) glib_ns.scope.lookup ("Node");
gqueue_type = (Class) glib_ns.scope.lookup ("Queue");
gvaluearray_type = (Class) glib_ns.scope.lookup ("ValueArray");
gstringbuilder_type = (TypeSymbol) glib_ns.scope.lookup ("StringBuilder");
garray_type = (TypeSymbol) glib_ns.scope.lookup ("Array");
gbytearray_type = (TypeSymbol) glib_ns.scope.lookup ("ByteArray");
gptrarray_type = (TypeSymbol) glib_ns.scope.lookup ("PtrArray");
gthreadpool_type = (TypeSymbol) glib_ns.scope.lookup ("ThreadPool");
gdestroynotify_type = new DelegateType ((Delegate) glib_ns.scope.lookup ("DestroyNotify"));
gquark_type = new IntegerType ((Struct) glib_ns.scope.lookup ("Quark"));
gvalue_type = (Struct) glib_ns.scope.lookup ("Value");
gvariant_type = (Class) glib_ns.scope.lookup ("Variant");
gsource_type = (Class) glib_ns.scope.lookup ("Source");
gmutex_type = (Struct) glib_ns.scope.lookup ("Mutex");
grecmutex_type = (Struct) glib_ns.scope.lookup ("RecMutex");
grwlock_type = (Struct) glib_ns.scope.lookup ("RWLock");
gcond_type = (Struct) glib_ns.scope.lookup ("Cond");
mutex_type = grecmutex_type;
type_module_type = (TypeSymbol) glib_ns.scope.lookup ("TypeModule");
regex_type = new ObjectType ((Class) root_symbol.scope.lookup ("GLib").scope.lookup ("Regex"));
if (context.module_init_method != null) {
foreach (Parameter parameter in context.module_init_method.get_parameters ()) {
if (parameter.variable_type.data_type == type_module_type) {
in_plugin = true;
module_init_param_name = parameter.name;
break;
}
}
}
if (!in_plugin) {
Report.error (context.module_init_method.source_reference, "[ModuleInit] requires a parameter of type `GLib.TypeModule'");
}
}
dbus_proxy_type = (TypeSymbol) glib_ns.scope.lookup ("DBusProxy");
dbus_proxy_type = (TypeSymbol) glib_ns.scope.lookup ("DBusProxy");
}
var gtk_ns = root_symbol.scope.lookup ("Gtk");
if (gtk_ns != null) {
......@@ -540,7 +540,12 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
// generate C header file for public API
if (context.header_filename != null) {
bool ret = header_file.store (context.header_filename, null, context.version_header, false, "G_BEGIN_DECLS", "G_END_DECLS");
bool ret;
if (context.profile == Profile.GOBJECT) {
ret = header_file.store (context.header_filename, null, context.version_header, false, "G_BEGIN_DECLS", "G_END_DECLS");
} else {
ret = header_file.store (context.header_filename, null, context.version_header, false);
}
if (!ret) {
Report.error (null, "unable to open `%s' for writing".printf (context.header_filename));
}
......@@ -548,7 +553,12 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
// generate C header file for internal API
if (context.internal_header_filename != null) {
bool ret = internal_header_file.store (context.internal_header_filename, null, context.version_header, false, "G_BEGIN_DECLS", "G_END_DECLS");
bool ret;
if (context.profile == Profile.GOBJECT) {
ret = internal_header_file.store (context.internal_header_filename, null, context.version_header, false, "G_BEGIN_DECLS", "G_END_DECLS");
} else {
ret = internal_header_file.store (context.internal_header_filename, null, context.version_header, false);
}
if (!ret) {
Report.error (null, "unable to open `%s' for writing".printf (context.internal_header_filename));
}
......@@ -736,10 +746,12 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
wrappers = new HashSet<string> (str_hash, str_equal);
generated_external_symbols = new HashSet<Symbol> ();
header_file.add_include ("glib.h");
internal_header_file.add_include ("glib.h");
cfile.add_include ("glib.h");
cfile.add_include ("glib-object.h");
if (context.profile == Profile.GOBJECT) {
header_file.add_include ("glib.h");
internal_header_file.add_include ("glib.h");
cfile.add_include ("glib.h");
cfile.add_include ("glib-object.h");
}
source_file.accept_children (this);
......@@ -3129,7 +3141,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
public CCodeExpression? get_destroy_func_expression (DataType type, bool is_chainup = false) {
if (type.data_type == glist_type || type.data_type == gslist_type || type.data_type == gnode_type || type.data_type == gqueue_type) {
if (context.profile == Profile.GOBJECT && (type.data_type == glist_type || type.data_type == gslist_type || type.data_type == gnode_type || type.data_type == gqueue_type)) {
// create wrapper function to free list elements if necessary
bool elements_require_free = false;
......@@ -3233,9 +3245,17 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
return get_variable_cexpression (func_name);
}
} else if (type is ArrayType) {
return new CCodeIdentifier ("g_free");
if (context.profile == Profile.POSIX) {
return new CCodeIdentifier ("free");
} else {
return new CCodeIdentifier ("g_free");
}
} else if (type is PointerType) {
return new CCodeIdentifier ("g_free");
if (context.profile == Profile.POSIX) {
return new CCodeIdentifier ("free");
} else {
return new CCodeIdentifier ("g_free");
}
} else {
return new CCodeConstant ("NULL");
}
......@@ -3476,40 +3496,42 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
/* set freed references to NULL to prevent further use */
var ccomma = new CCodeCommaExpression ();
if (type.data_type != null && !is_reference_counting (type.data_type) &&
(type.data_type.is_subtype_of (gstringbuilder_type)
|| type.data_type.is_subtype_of (garray_type)
|| type.data_type.is_subtype_of (gbytearray_type)
|| type.data_type.is_subtype_of (gptrarray_type))) {
ccall.add_argument (new CCodeConstant ("TRUE"));
} else if (type.data_type == gthreadpool_type) {
ccall.add_argument (new CCodeConstant ("FALSE"));
ccall.add_argument (new CCodeConstant ("TRUE"));
} else if (type is ArrayType) {
var array_type = (ArrayType) type;
if (requires_destroy (array_type.element_type)) {
CCodeExpression csizeexpr = null;
if (((GLibValue) value).array_length_cvalues != null) {
csizeexpr = get_array_length_cvalue (value);
} else if (get_array_null_terminated (value)) {
requires_array_length = true;
var len_call = new CCodeFunctionCall (new CCodeIdentifier ("_vala_array_length"));
len_call.add_argument (cvar);
csizeexpr = len_call;
} else {
csizeexpr = get_array_length_cexpr (value);
}
if (csizeexpr != null) {
var st = array_type.element_type.data_type as Struct;
if (st != null && !array_type.element_type.nullable) {
ccall.call = new CCodeIdentifier (append_struct_array_free (st));
ccall.add_argument (csizeexpr);
if (context.profile == Profile.GOBJECT) {
if (type.data_type != null && !is_reference_counting (type.data_type) &&
(type.data_type.is_subtype_of (gstringbuilder_type)
|| type.data_type.is_subtype_of (garray_type)
|| type.data_type.is_subtype_of (gbytearray_type)
|| type.data_type.is_subtype_of (gptrarray_type))) {
ccall.add_argument (new CCodeConstant ("TRUE"));
} else if (type.data_type == gthreadpool_type) {
ccall.add_argument (new CCodeConstant ("FALSE"));
ccall.add_argument (new CCodeConstant ("TRUE"));
} else if (type is ArrayType) {
var array_type = (ArrayType) type;
if (requires_destroy (array_type.element_type)) {
CCodeExpression csizeexpr = null;
if (((GLibValue) value).array_length_cvalues != null) {
csizeexpr = get_array_length_cvalue (value);
} else if (get_array_null_terminated (value)) {
requires_array_length = true;
var len_call = new CCodeFunctionCall (new CCodeIdentifier ("_vala_array_length"));
len_call.add_argument (cvar);
csizeexpr = len_call;
} else {
requires_array_free = true;
ccall.call = new CCodeIdentifier ("_vala_array_free");
ccall.add_argument (csizeexpr);
ccall.add_argument (new CCodeCastExpression (get_destroy_func_expression (array_type.element_type), "GDestroyNotify"));
csizeexpr = get_array_length_cexpr (value);
}
if (csizeexpr != null) {
var st = array_type.element_type.data_type as Struct;
if (st != null && !array_type.element_type.nullable) {
ccall.call = new CCodeIdentifier (append_struct_array_free (st));
ccall.add_argument (csizeexpr);
} else {
requires_array_free = true;
ccall.call = new CCodeIdentifier ("_vala_array_free");
ccall.add_argument (csizeexpr);
ccall.add_argument (new CCodeCastExpression (get_destroy_func_expression (array_type.element_type), "GDestroyNotify"));
}
}
}
}
......@@ -3961,7 +3983,12 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
public override void visit_boolean_literal (BooleanLiteral expr) {
set_cvalue (expr, new CCodeConstant (expr.value ? "TRUE" : "FALSE"));
if (context.profile == Profile.GOBJECT) {
set_cvalue (expr, new CCodeConstant (expr.value ? "TRUE" : "FALSE"));
} else {
cfile.add_include ("stdbool.h");
set_cvalue (expr, new CCodeConstant (expr.value ? "true" : "false"));
}
}
public override void visit_character_literal (CharacterLiteral expr) {
......@@ -4074,6 +4101,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
public override void visit_null_literal (NullLiteral expr) {
if (context.profile != Profile.GOBJECT) {
cfile.add_include ("stddef.h");
}
set_cvalue (expr, new CCodeConstant ("NULL"));
var array_type = expr.target_type as ArrayType;
......@@ -4333,6 +4363,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
dup0_func = dupid.name;
} else if (add_wrapper (dup0_func)) {
string pointer_cname = "gpointer";
if (context.profile == Profile.POSIX) {
pointer_cname = "void *";
}
var dup0_fun = new CCodeFunction (dup0_func, pointer_cname);
dup0_fun.add_parameter (new CCodeParameter ("self", pointer_cname));
dup0_fun.modifiers = CCodeModifiers.STATIC;
......@@ -5213,7 +5246,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
var cl = expr.type_reference.data_type as Class;
var iface = expr.type_reference.data_type as Interface;
if (iface != null || (cl != null && !cl.is_compact)) {
if (context.profile == Profile.GOBJECT && (iface != null || (cl != null && !cl.is_compact))) {
// checked cast for strict subtypes of GTypeInstance
if (expr.is_silent_cast) {
TargetValue to_cast = expr.inner.target_value;
......@@ -5514,13 +5547,35 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
set_cvalue (expr, new CCodeConstant ("%s %s".printf (left, right)));
return;
} else {
// convert to g_strconcat (a, b, NULL)
var temp_value = create_temp_value (expr.value_type, false, expr);
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_strconcat"));
ccall.add_argument (cleft);
ccall.add_argument (cright);
ccall.add_argument (new CCodeConstant("NULL"));
CCodeFunctionCall ccall;
if (context.profile == Profile.POSIX) {
// convert to strcat (strcpy (malloc (1 + strlen (a) + strlen (b)), a), b)
ccall = new CCodeFunctionCall (new CCodeIdentifier ("strcat"));
var strcpy = new CCodeFunctionCall (new CCodeIdentifier ("strcpy"));
var malloc = new CCodeFunctionCall (new CCodeIdentifier ("malloc"));
var strlen_a = new CCodeFunctionCall (new CCodeIdentifier ("strlen"));
strlen_a.add_argument (cleft);
var strlen_b = new CCodeFunctionCall (new CCodeIdentifier ("strlen"));
strlen_b.add_argument (cright);
var newlength = new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, new CCodeIdentifier ("1"),
new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, strlen_a, strlen_b));
malloc.add_argument (newlength);
strcpy.add_argument (malloc);
strcpy.add_argument (cleft);
ccall.add_argument (strcpy);
ccall.add_argument (cright);
} else {
// convert to g_strconcat (a, b, NULL)
ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_strconcat"));
ccall.add_argument (cleft);
ccall.add_argument (cright);
ccall.add_argument (new CCodeConstant("NULL"));
}
ccode.add_assignment (get_cvalue_ (temp_value), ccall);
expr.target_value = temp_value;
......@@ -5753,11 +5808,13 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
bool unboxing = (type is ValueType && type.nullable
&& target_type is ValueType && !target_type.nullable);
bool gvalue_boxing = (target_type != null
bool gvalue_boxing = (context.profile == Profile.GOBJECT
&& target_type != null
&& target_type.data_type == gvalue_type
&& !(type is NullType)
&& get_ccode_type_id (type) != "G_TYPE_VALUE");
bool gvariant_boxing = (target_type != null
bool gvariant_boxing = (context.profile == Profile.GOBJECT
&& target_type != null
&& target_type.data_type == gvariant_type
&& !(type is NullType)
&& type.data_type != gvariant_type);
......
......@@ -36,15 +36,23 @@ public class Vala.CCodeCompiler {
* @param context a code context
*/
public void compile (CodeContext context, string? cc_command, string[] cc_options) {
string pc = " gobject-2.0";
string pc = "";
if (context.profile == Profile.GOBJECT) {
pc += " gobject-2.0";
}
foreach (string pkg in context.get_packages ()) {
if (context.pkg_config_exists (pkg)) {
pc += " " + pkg;
}
}
string? pkgflags = context.pkg_config_compile_flags (pc);
if (pkgflags == null) {
return;
string? pkgflags;
if (pc.length > 0) {
pkgflags = context.pkg_config_compile_flags (pc);
if (pkgflags == null) {
return;
}
} else {
pkgflags = "";
}
// TODO compile the C code files in parallel
......
......@@ -206,7 +206,12 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
}
public override void visit_loop (Loop stmt) {
ccode.open_while (new CCodeConstant ("TRUE"));
if (context.profile == Profile.GOBJECT) {
ccode.open_while (new CCodeConstant ("TRUE"));
} else {
cfile.add_include ("stdbool.h");
ccode.open_while (new CCodeConstant ("true"));
}
stmt.body.emit (this);
......
......@@ -53,6 +53,8 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
// Enum.VALUE.to_string()
var en = (Enum) ma.inner.value_type.data_type;
ccall.call = new CCodeIdentifier (generate_enum_tostring_function (en));
} else if (context.profile == Profile.POSIX && ma.inner != null && ma.inner.value_type != null && ma.inner.value_type.data_type == string_type.data_type && ma.member_name == "printf") {
ccall.call = new CCodeIdentifier (generate_string_printf_function ());
} else if (expr.is_constructv_chainup) {
ccall.call = new CCodeIdentifier (get_ccode_constructv_name ((CreationMethod) m));
}
......@@ -132,8 +134,12 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
}
if (m is CreationMethod && m.parent_symbol is Class) {
if (!((Class) m.parent_symbol).is_compact) {
ccall.add_argument (get_variable_cexpression ("object_type"));
if (context.profile == Profile.GOBJECT) {
if (!((Class) m.parent_symbol).is_compact) {
ccall.add_argument (get_variable_cexpression ("object_type"));
}
} else {
ccall.add_argument (get_this_cexpression ());
}
if (!current_class.is_compact) {
......@@ -311,7 +317,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
param.accept (this);
}
generate_dynamic_method_wrapper ((DynamicMethod) m);
} else if (m is CreationMethod && m.parent_symbol is Class) {
} else if (m is CreationMethod && context.profile == Profile.GOBJECT && m.parent_symbol is Class) {
ccode.add_assignment (get_this_cexpression (), new CCodeCastExpression (ccall, get_ccode_name (current_class) + "*"));
if (current_method.body.captured) {
......@@ -920,5 +926,80 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
}
return false;
}
string generate_string_printf_function () {
if (!add_wrapper ("string_printf")) {
// wrapper already defined
return "string_printf";
}
// declaration
var function = new CCodeFunction ("string_printf", "char*");
function.add_parameter (new CCodeParameter ("format", "const char*"));
function.add_parameter (new CCodeParameter.with_ellipsis ());
function.modifiers = CCodeModifiers.STATIC;
// definition
push_context (new EmitContext ());
push_function (function);
ccode.add_declaration ("int", new CCodeVariableDeclarator ("length"));
ccode.add_declaration ("va_list", new CCodeVariableDeclarator ("ap"));
ccode.add_declaration ("char*", new CCodeVariableDeclarator ("result"));
var va_start = new CCodeFunctionCall (new CCodeIdentifier ("va_start"));
va_start.add_argument (new CCodeIdentifier ("ap"));
va_start.add_argument (new CCodeIdentifier ("format"));
ccode.add_expression (va_start);
var vsnprintf = new CCodeFunctionCall (new CCodeIdentifier ("vsnprintf"));
vsnprintf.add_argument (new CCodeConstant ("NULL"));
vsnprintf.add_argument (new CCodeConstant ("0"));
vsnprintf.add_argument (new CCodeIdentifier ("format"));
vsnprintf.add_argument (new CCodeIdentifier ("ap"));
ccode.add_assignment (new CCodeIdentifier ("length"), new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, vsnprintf, new CCodeConstant ("1")));
var va_end = new CCodeFunctionCall (new CCodeIdentifier ("va_end"));
va_end.add_argument (new CCodeIdentifier ("ap"));
ccode.add_expression (va_end);
var malloc = new CCodeFunctionCall (new CCodeIdentifier ("malloc"));
malloc.add_argument (new CCodeIdentifier ("length"));
ccode.add_assignment (new CCodeIdentifier ("result"), malloc);
va_start = new CCodeFunctionCall (new CCodeIdentifier ("va_start"));
va_start.add_argument (new CCodeIdentifier ("ap"));
va_start.add_argument (new CCodeIdentifier ("format"));
ccode.add_expression (va_start);
vsnprintf = new CCodeFunctionCall (new CCodeIdentifier ("vsnprintf"));
vsnprintf.add_argument (new CCodeIdentifier ("result"));
vsnprintf.add_argument (new CCodeIdentifier ("length"));
vsnprintf.add_argument (new CCodeIdentifier ("format"));
vsnprintf.add_argument (new CCodeIdentifier ("ap"));
ccode.add_expression (vsnprintf);
va_end = new CCodeFunctionCall (new CCodeIdentifier ("va_end"));
va_end.add_argument (new CCodeIdentifier ("ap"));
ccode.add_expression (va_end);
ccode.add_return (new CCodeIdentifier ("result"));
// append to file
cfile.add_include ("stdarg.h");
cfile.add_function_declaration (function);
cfile.add_function (function);
pop_context ();
return "string_printf";
}
}
......@@ -858,11 +858,13 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
cmain.add_parameter (new CCodeParameter ("argv", "char **"));
push_function (cmain);
if (context.mem_profiler) {
var mem_profiler_init_call = new CCodeFunctionCall (new CCodeIdentifier ("g_mem_set_vtable"));
mem_profiler_init_call.line = cmain.line;
mem_profiler_init_call.add_argument (new CCodeConstant ("glib_mem_profiler_table"));
ccode.add_expression (mem_profiler_init_call);
if (context.profile == Profile.GOBJECT) {
if (context.mem_profiler) {
var mem_profiler_init_call = new CCodeFunctionCall (new CCodeIdentifier ("g_mem_set_vtable"));
mem_profiler_init_call.line = cmain.line;
mem_profiler_init_call.add_argument (new CCodeConstant ("glib_mem_profiler_table"));
ccode.add_expression (mem_profiler_init_call);
}
}
var main_call = new CCodeFunctionCall (new CCodeIdentifier (function.name));
......
......@@ -53,15 +53,17 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
return;
}
if (get_ccode_has_type_id (st)) {
decl_space.add_include ("glib-object.h");
decl_space.add_type_declaration (new CCodeNewline ());
var macro = "(%s_get_type ())".printf (get_ccode_lower_case_name (st, null));
decl_space.add_type_declaration (new CCodeMacroReplacement (get_ccode_type_id (st), macro));
var type_fun = new StructRegisterFunction (st);
type_fun.init_from_type (context, false, true);
decl_space.add_type_member_declaration (type_fun.get_declaration ());
if (context.profile == Profile.GOBJECT) {
if (get_ccode_has_type_id (st)) {
decl_space.add_include ("glib-object.h");
decl_space.add_type_declaration (new CCodeNewline ());
var macro = "(%s_get_type ())".printf (get_ccode_lower_case_name (st, null));
decl_space.add_type_declaration (new CCodeMacroReplacement (get_ccode_type_id (st), macro));
var type_fun = new StructRegisterFunction (st);
type_fun.init_from_type (context, false, true);
decl_space.add_type_member_declaration (type_fun.get_declaration ());
}
}
var instance_struct = new CCodeStruct ("_%s".printf (get_ccode_name (st)));
......@@ -212,10 +214,17 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
ccode.add_declaration (get_ccode_name (st) + "*", new CCodeVariableDeclarator ("dup"));
var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
creation_call.add_argument (new CCodeConstant (get_ccode_name (st)));
creation_call.add_argument (new CCodeConstant ("1"));
ccode.add_assignment (new CCodeIdentifier ("dup"), creation_call);
if (context.profile == Profile.GOBJECT) {
var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
creation_call.add_argument (new CCodeConstant (get_ccode_name (st)));
creation_call.add_argument (new CCodeConstant ("1"));
ccode.add_assignment (new CCodeIdentifier ("dup"), creation_call);
} else if (context.profile == Profile.POSIX) {
var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("calloc"));
creation_call.add_argument (new CCodeConstant ("1"));
creation_call.add_argument (new CCodeIdentifier ("sizeof (%s*)".printf (get_ccode_name (st))));
ccode.add_assignment (new CCodeIdentifier ("dup"), creation_call);
}
if (st.is_disposable ()) {
var copy_call = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_copy_function (st)));
......@@ -260,9 +269,15 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
ccode.add_expression (destroy_call);
}
var free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_free"));
free_call.add_argument (new CCodeIdentifier ("self"));
ccode.add_expression (free_call);
if (context.profile == Profile.GOBJECT) {
var free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_free"));
free_call.add_argument (new CCodeIdentifier ("self"));
ccode.add_expression (free_call);
} else if (context.profile == Profile.POSIX) {
var free_call = new CCodeFunctionCall (new CCodeIdentifier ("free"));
free_call.add_argument (new CCodeIdentifier ("self"));
ccode.add_expression (free_call);
}
pop_function ();
......
......@@ -272,7 +272,10 @@ class Vala.Compiler {
if (ccode_only && save_temps) {
Report.warning (null, "--save-temps has no effect when -C or --ccode is set");
}
if (profile == "gobject-2.0" || profile == "gobject" || profile == null) {
if (profile == "posix") {
context.profile = Profile.POSIX;
context.add_define ("POSIX");
} else if (profile == "gobject-2.0" || profile == "gobject" || profile == null) {
// default profile
context.profile = Profile.GOBJECT;
context.add_define ("GOBJECT");
......@@ -301,26 +304,33 @@ class Vala.Compiler {
context.add_define ("VALA_0_%d".printf (i));
}
int glib_major = 2;
int glib_minor = 40;
if (target_glib != null && target_glib.scanf ("%d.%d", out glib_major, out glib_minor) != 2) {
Report.error (null, "Invalid format for --target-glib");
}
if (context.profile == Profile.POSIX) {
if (!nostdpkg) {
/* default package */
context.add_external_package ("posix");
}
} else if (context.profile == Profile.GOBJECT) {
int glib_major