1 /* valaccodebasemodule.vala
3 * Copyright (C) 2006-2011 Jürg Billeter
4 * Copyright (C) 2006-2008 Raffaele Sandrini
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Jürg Billeter <j@bitron.ch>
22 * Raffaele Sandrini <raffaele@sandrini.ch>
27 * Code visitor generating C Code.
29 public abstract class Vala
.CCodeBaseModule
: CodeGenerator
{
30 public class EmitContext
{
31 public Symbol? current_symbol
;
32 public ArrayList
<Symbol
> symbol_stack
= new ArrayList
<Symbol
> ();
33 public TryStatement current_try
;
34 public CatchClause current_catch
;
35 public CCodeFunction ccode
;
36 public ArrayList
<CCodeFunction
> ccode_stack
= new ArrayList
<CCodeFunction
> ();
37 public ArrayList
<LocalVariable
> temp_ref_vars
= new ArrayList
<LocalVariable
> ();
38 public int next_temp_var_id
;
39 public bool current_method_inner_error
;
40 public bool current_method_return
;
41 public Map
<string,string> variable_name_map
= new HashMap
<string,string> (str_hash
, str_equal
);
43 public EmitContext (Symbol? symbol
= null) {
44 current_symbol
= symbol
;
47 public void push_symbol (Symbol symbol
) {
48 symbol_stack
.add (current_symbol
);
49 current_symbol
= symbol
;
52 public void pop_symbol () {
53 current_symbol
= symbol_stack
[symbol_stack
.size
- 1];
54 symbol_stack
.remove_at (symbol_stack
.size
- 1);
58 public CodeContext context
{ get; set; }
60 public Symbol root_symbol
;
62 public EmitContext emit_context
= new
EmitContext ();
64 List
<EmitContext
> emit_context_stack
= new ArrayList
<EmitContext
> ();
66 public Symbol current_symbol
{ get { return emit_context
.current_symbol
; } }
68 public TryStatement current_try
{
69 get { return emit_context
.current_try
; }
70 set { emit_context
.current_try
= value
; }
73 public CatchClause current_catch
{
74 get { return emit_context
.current_catch
; }
75 set { emit_context
.current_catch
= value
; }
78 public TypeSymbol? current_type_symbol
{
80 var sym
= current_symbol
;
82 if (sym is TypeSymbol
) {
83 return (TypeSymbol
) sym
;
85 sym
= sym
.parent_symbol
;
91 public Class? current_class
{
92 get { return current_type_symbol as Class
; }
95 public Method? current_method
{
97 var sym
= current_symbol
;
98 while (sym is Block
) {
99 sym
= sym
.parent_symbol
;
101 return sym as Method
;
105 public PropertyAccessor? current_property_accessor
{
107 var sym
= current_symbol
;
108 while (sym is Block
) {
109 sym
= sym
.parent_symbol
;
111 return sym as PropertyAccessor
;
115 public DataType? current_return_type
{
117 var m
= current_method
;
119 return m
.return_type
;
122 var acc
= current_property_accessor
;
125 return acc
.value_type
;
131 if (is_in_constructor () || is_in_destructor ()) {
139 public bool is_in_coroutine () {
140 return current_method
!= null && current_method
.coroutine
;
143 public bool is_in_constructor () {
144 if (current_method
!= null) {
145 // make sure to not return true in lambda expression inside constructor
148 var sym
= current_symbol
;
149 while (sym
!= null) {
150 if (sym is Constructor
) {
153 sym
= sym
.parent_symbol
;
158 public bool is_in_destructor () {
159 if (current_method
!= null) {
160 // make sure to not return true in lambda expression inside constructor
163 var sym
= current_symbol
;
164 while (sym
!= null) {
165 if (sym is Destructor
) {
168 sym
= sym
.parent_symbol
;
173 public Block? current_closure_block
{
175 return next_closure_block (current_symbol
);
179 public unowned Block?
next_closure_block (Symbol sym
) {
180 unowned Block block
= null;
182 block
= sym as Block
;
183 if (!(sym is Block
|| sym is Method
)) {
187 if (block
!= null && block
.captured
) {
188 // closure block found
191 sym
= sym
.parent_symbol
;
196 public CCodeFile header_file
;
197 public CCodeFile internal_header_file
;
198 public CCodeFile cfile
;
200 public EmitContext class_init_context
;
201 public EmitContext base_init_context
;
202 public EmitContext class_finalize_context
;
203 public EmitContext base_finalize_context
;
204 public EmitContext instance_init_context
;
205 public EmitContext instance_finalize_context
;
207 public CCodeStruct param_spec_struct
;
208 public CCodeStruct closure_struct
;
209 public CCodeEnum prop_enum
;
211 public CCodeFunction ccode
{ get { return emit_context
.ccode
; } }
213 /* temporary variables that own their content */
214 public ArrayList
<LocalVariable
> temp_ref_vars
{ get { return emit_context
.temp_ref_vars
; } }
215 /* cache to check whether a certain marshaller has been created yet */
216 public Set
<string> user_marshal_set
;
217 /* (constant) hash table with all predefined marshallers */
218 public Set
<string> predefined_marshal_set
;
219 /* (constant) hash table with all reserved identifiers in the generated code */
220 Set
<string> reserved_identifiers
;
222 public int next_temp_var_id
{
223 get { return emit_context
.next_temp_var_id
; }
224 set { emit_context
.next_temp_var_id
= value
; }
227 public int next_regex_id
= 0;
228 public bool in_creation_method
{ get { return current_method is CreationMethod
; } }
229 public bool in_constructor
= false;
230 public bool in_static_or_class_context
= false;
232 public bool current_method_inner_error
{
233 get { return emit_context
.current_method_inner_error
; }
234 set { emit_context
.current_method_inner_error
= value
; }
237 public bool current_method_return
{
238 get { return emit_context
.current_method_return
; }
239 set { emit_context
.current_method_return
= value
; }
242 public int next_coroutine_state
= 1;
243 int next_block_id
= 0;
244 Map
<Block
,int> block_map
= new HashMap
<Block
,int> ();
246 public DataType void_type
= new
VoidType ();
247 public DataType bool_type
;
248 public DataType char_type
;
249 public DataType uchar_type
;
250 public DataType? unichar_type
;
251 public DataType short_type
;
252 public DataType ushort_type
;
253 public DataType int_type
;
254 public DataType uint_type
;
255 public DataType long_type
;
256 public DataType ulong_type
;
257 public DataType int8_type
;
258 public DataType uint8_type
;
259 public DataType int16_type
;
260 public DataType uint16_type
;
261 public DataType int32_type
;
262 public DataType uint32_type
;
263 public DataType int64_type
;
264 public DataType uint64_type
;
265 public DataType string_type
;
266 public DataType regex_type
;
267 public DataType float_type
;
268 public DataType double_type
;
269 public TypeSymbol gtype_type
;
270 public TypeSymbol gobject_type
;
271 public ErrorType gerror_type
;
272 public Class glist_type
;
273 public Class gslist_type
;
274 public Class gnode_type
;
275 public Class gvaluearray_type
;
276 public TypeSymbol gstringbuilder_type
;
277 public TypeSymbol garray_type
;
278 public TypeSymbol gbytearray_type
;
279 public TypeSymbol gptrarray_type
;
280 public TypeSymbol gthreadpool_type
;
281 public DataType gdestroynotify_type
;
282 public DataType gquark_type
;
283 public Struct gvalue_type
;
284 public Class gvariant_type
;
285 public Struct mutex_type
;
286 public TypeSymbol type_module_type
;
287 public TypeSymbol dbus_proxy_type
;
288 public TypeSymbol dbus_object_type
;
290 public bool in_plugin
= false;
291 public string module_init_param_name
;
293 public bool gvaluecollector_h_needed
;
294 public bool requires_array_free
;
295 public bool requires_array_move
;
296 public bool requires_array_length
;
298 public Set
<string> wrappers
;
299 Set
<Symbol
> generated_external_symbols
;
301 public Map
<string,string> variable_name_map
{ get { return emit_context
.variable_name_map
; } }
303 public CCodeBaseModule () {
304 predefined_marshal_set
= new HashSet
<string> (str_hash
, str_equal
);
305 predefined_marshal_set
.add ("VOID:VOID");
306 predefined_marshal_set
.add ("VOID:BOOLEAN");
307 predefined_marshal_set
.add ("VOID:CHAR");
308 predefined_marshal_set
.add ("VOID:UCHAR");
309 predefined_marshal_set
.add ("VOID:INT");
310 predefined_marshal_set
.add ("VOID:UINT");
311 predefined_marshal_set
.add ("VOID:LONG");
312 predefined_marshal_set
.add ("VOID:ULONG");
313 predefined_marshal_set
.add ("VOID:ENUM");
314 predefined_marshal_set
.add ("VOID:FLAGS");
315 predefined_marshal_set
.add ("VOID:FLOAT");
316 predefined_marshal_set
.add ("VOID:DOUBLE");
317 predefined_marshal_set
.add ("VOID:STRING");
318 predefined_marshal_set
.add ("VOID:POINTER");
319 predefined_marshal_set
.add ("VOID:OBJECT");
320 predefined_marshal_set
.add ("STRING:OBJECT,POINTER");
321 predefined_marshal_set
.add ("VOID:UINT,POINTER");
322 predefined_marshal_set
.add ("BOOLEAN:FLAGS");
324 reserved_identifiers
= new HashSet
<string> (str_hash
, str_equal
);
327 reserved_identifiers
.add ("_Bool");
328 reserved_identifiers
.add ("_Complex");
329 reserved_identifiers
.add ("_Imaginary");
330 reserved_identifiers
.add ("asm");
331 reserved_identifiers
.add ("auto");
332 reserved_identifiers
.add ("break");
333 reserved_identifiers
.add ("case");
334 reserved_identifiers
.add ("char");
335 reserved_identifiers
.add ("const");
336 reserved_identifiers
.add ("continue");
337 reserved_identifiers
.add ("default");
338 reserved_identifiers
.add ("do");
339 reserved_identifiers
.add ("double");
340 reserved_identifiers
.add ("else");
341 reserved_identifiers
.add ("enum");
342 reserved_identifiers
.add ("extern");
343 reserved_identifiers
.add ("float");
344 reserved_identifiers
.add ("for");
345 reserved_identifiers
.add ("goto");
346 reserved_identifiers
.add ("if");
347 reserved_identifiers
.add ("inline");
348 reserved_identifiers
.add ("int");
349 reserved_identifiers
.add ("long");
350 reserved_identifiers
.add ("register");
351 reserved_identifiers
.add ("restrict");
352 reserved_identifiers
.add ("return");
353 reserved_identifiers
.add ("short");
354 reserved_identifiers
.add ("signed");
355 reserved_identifiers
.add ("sizeof");
356 reserved_identifiers
.add ("static");
357 reserved_identifiers
.add ("struct");
358 reserved_identifiers
.add ("switch");
359 reserved_identifiers
.add ("typedef");
360 reserved_identifiers
.add ("union");
361 reserved_identifiers
.add ("unsigned");
362 reserved_identifiers
.add ("void");
363 reserved_identifiers
.add ("volatile");
364 reserved_identifiers
.add ("while");
367 reserved_identifiers
.add ("cdecl");
369 // reserved for Vala/GObject naming conventions
370 reserved_identifiers
.add ("error");
371 reserved_identifiers
.add ("result");
372 reserved_identifiers
.add ("self");
375 public override void emit (CodeContext context
) {
376 this
.context
= context
;
378 root_symbol
= context
.root
;
380 bool_type
= new
BooleanType ((Struct
) root_symbol
.scope
.lookup ("bool"));
381 char_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("char"));
382 uchar_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uchar"));
383 short_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("short"));
384 ushort_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("ushort"));
385 int_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int"));
386 uint_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uint"));
387 long_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("long"));
388 ulong_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("ulong"));
389 int8_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int8"));
390 uint8_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uint8"));
391 int16_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int16"));
392 uint16_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uint16"));
393 int32_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int32"));
394 uint32_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uint32"));
395 int64_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int64"));
396 uint64_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uint64"));
397 float_type
= new
FloatingType ((Struct
) root_symbol
.scope
.lookup ("float"));
398 double_type
= new
FloatingType ((Struct
) root_symbol
.scope
.lookup ("double"));
399 string_type
= new
ObjectType ((Class
) root_symbol
.scope
.lookup ("string"));
400 var unichar_struct
= (Struct
) root_symbol
.scope
.lookup ("unichar");
401 if (unichar_struct
!= null) {
402 unichar_type
= new
IntegerType (unichar_struct
);
405 if (context
.profile
== Profile
.GOBJECT
) {
406 var glib_ns
= root_symbol
.scope
.lookup ("GLib");
408 gtype_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("Type");
409 gobject_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("Object");
410 gerror_type
= new
ErrorType (null, null);
411 glist_type
= (Class
) glib_ns
.scope
.lookup ("List");
412 gslist_type
= (Class
) glib_ns
.scope
.lookup ("SList");
413 gnode_type
= (Class
) glib_ns
.scope
.lookup ("Node");
414 gvaluearray_type
= (Class
) glib_ns
.scope
.lookup ("ValueArray");
415 gstringbuilder_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("StringBuilder");
416 garray_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("Array");
417 gbytearray_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("ByteArray");
418 gptrarray_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("PtrArray");
419 gthreadpool_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("ThreadPool");
420 gdestroynotify_type
= new
DelegateType ((Delegate
) glib_ns
.scope
.lookup ("DestroyNotify"));
422 gquark_type
= new
IntegerType ((Struct
) glib_ns
.scope
.lookup ("Quark"));
423 gvalue_type
= (Struct
) glib_ns
.scope
.lookup ("Value");
424 gvariant_type
= (Class
) glib_ns
.scope
.lookup ("Variant");
425 mutex_type
= (Struct
) glib_ns
.scope
.lookup ("StaticRecMutex");
427 type_module_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("TypeModule");
429 regex_type
= new
ObjectType ((Class
) root_symbol
.scope
.lookup ("GLib").scope
.lookup ("Regex"));
431 if (context
.module_init_method
!= null) {
432 foreach (Parameter parameter
in context
.module_init_method
.get_parameters ()) {
433 if (parameter
.variable_type
.data_type
== type_module_type
) {
435 module_init_param_name
= parameter
.name
;
440 Report
.error (context
.module_init_method
.source_reference
, "[ModuleInit] requires a parameter of type `GLib.TypeModule'");
444 dbus_proxy_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("DBusProxy");
446 var dbus_ns
= root_symbol
.scope
.lookup ("DBus");
447 if (dbus_ns
!= null) {
448 dbus_object_type
= (TypeSymbol
) dbus_ns
.scope
.lookup ("Object");
452 header_file
= new
CCodeFile ();
453 header_file
.is_header
= true;
454 internal_header_file
= new
CCodeFile ();
455 internal_header_file
.is_header
= true;
457 /* we're only interested in non-pkg source files */
458 var source_files
= context
.get_source_files ();
459 foreach (SourceFile file
in source_files
) {
460 if (file
.file_type
== SourceFileType
.SOURCE
||
461 (context
.header_filename
!= null && file
.file_type
== SourceFileType
.FAST
)) {
466 // generate symbols file for public API
467 if (context
.symbols_filename
!= null) {
468 var stream
= FileStream
.open (context
.symbols_filename
, "w");
469 if (stream
== null) {
470 Report
.error (null, "unable to open `%s' for writing".printf (context
.symbols_filename
));
474 foreach (string symbol
in header_file
.get_symbols ()) {
475 stream
.puts (symbol
);
482 // generate C header file for public API
483 if (context
.header_filename
!= null) {
485 if (context
.profile
== Profile
.GOBJECT
) {
486 ret
= header_file
.store (context
.header_filename
, null, context
.version_header
, false, "G_BEGIN_DECLS", "G_END_DECLS");
488 ret
= header_file
.store (context
.header_filename
, null, context
.version_header
, false);
491 Report
.error (null, "unable to open `%s' for writing".printf (context
.header_filename
));
495 // generate C header file for internal API
496 if (context
.internal_header_filename
!= null) {
498 if (context
.profile
== Profile
.GOBJECT
) {
499 ret
= internal_header_file
.store (context
.internal_header_filename
, null, context
.version_header
, false, "G_BEGIN_DECLS", "G_END_DECLS");
501 ret
= internal_header_file
.store (context
.internal_header_filename
, null, context
.version_header
, false);
504 Report
.error (null, "unable to open `%s' for writing".printf (context
.internal_header_filename
));
509 public void push_context (EmitContext emit_context
) {
510 if (this
.emit_context
!= null) {
511 emit_context_stack
.add (this
.emit_context
);
514 this
.emit_context
= emit_context
;
517 public void pop_context () {
518 if (emit_context_stack
.size
> 0) {
519 this
.emit_context
= emit_context_stack
[emit_context_stack
.size
- 1];
520 emit_context_stack
.remove_at (emit_context_stack
.size
- 1);
522 this
.emit_context
= null;
526 public void push_function (CCodeFunction func
) {
527 emit_context
.ccode_stack
.add (ccode
);
528 emit_context
.ccode
= func
;
531 public void pop_function () {
532 emit_context
.ccode
= emit_context
.ccode_stack
[emit_context
.ccode_stack
.size
- 1];
533 emit_context
.ccode_stack
.remove_at (emit_context
.ccode_stack
.size
- 1);
536 public bool add_symbol_declaration (CCodeFile decl_space
, Symbol sym
, string name
) {
537 if (decl_space
.add_declaration (name
)) {
540 if (sym
.source_reference
!= null) {
541 sym
.source_reference
.file
.used
= true;
543 if (sym
.external_package
|| (!decl_space
.is_header
&& CodeContext
.get ().use_header
&& !sym
.is_internal_symbol ())) {
544 // add appropriate include file
545 foreach (string header_filename
in sym
.get_cheader_filenames ()) {
546 decl_space
.add_include (header_filename
, !sym
.external_package
);
548 // declaration complete
551 // require declaration
556 public CCodeIdentifier
get_value_setter_function (DataType type_reference
) {
557 var array_type
= type_reference as ArrayType
;
558 if (type_reference
.data_type
!= null) {
559 return new
CCodeIdentifier (type_reference
.data_type
.get_set_value_function ());
560 } else if (array_type
!= null && array_type
.element_type
.data_type
== string_type
.data_type
) {
562 return new
CCodeIdentifier ("g_value_set_boxed");
564 return new
CCodeIdentifier ("g_value_set_pointer");
568 public CCodeIdentifier
get_value_taker_function (DataType type_reference
) {
569 var array_type
= type_reference as ArrayType
;
570 if (type_reference
.data_type
!= null) {
571 return new
CCodeIdentifier (type_reference
.data_type
.get_take_value_function ());
572 } else if (array_type
!= null && array_type
.element_type
.data_type
== string_type
.data_type
) {
574 return new
CCodeIdentifier ("g_value_take_boxed");
576 return new
CCodeIdentifier ("g_value_set_pointer");
580 CCodeIdentifier
get_value_getter_function (DataType type_reference
) {
581 var array_type
= type_reference as ArrayType
;
582 if (type_reference
.data_type
!= null) {
583 return new
CCodeIdentifier (type_reference
.data_type
.get_get_value_function ());
584 } else if (array_type
!= null && array_type
.element_type
.data_type
== string_type
.data_type
) {
586 return new
CCodeIdentifier ("g_value_get_boxed");
588 return new
CCodeIdentifier ("g_value_get_pointer");
592 public virtual void append_vala_array_free () {
595 public virtual void append_vala_array_move () {
598 public virtual void append_vala_array_length () {
601 public override void visit_source_file (SourceFile source_file
) {
602 cfile
= new
CCodeFile ();
604 user_marshal_set
= new HashSet
<string> (str_hash
, str_equal
);
608 gvaluecollector_h_needed
= false;
609 requires_array_free
= false;
610 requires_array_move
= false;
611 requires_array_length
= false;
613 wrappers
= new HashSet
<string> (str_hash
, str_equal
);
614 generated_external_symbols
= new HashSet
<Symbol
> ();
616 if (context
.profile
== Profile
.GOBJECT
) {
617 header_file
.add_include ("glib.h");
618 internal_header_file
.add_include ("glib.h");
619 cfile
.add_include ("glib.h");
620 cfile
.add_include ("glib-object.h");
623 source_file
.accept_children (this
);
625 if (context
.report
.get_errors () > 0) {
629 /* For fast-vapi, we only wanted the header declarations
630 * to be emitted, so bail out here without writing the
633 if (source_file
.file_type
== SourceFileType
.FAST
) {
637 if (requires_array_free
) {
638 append_vala_array_free ();
640 if (requires_array_move
) {
641 append_vala_array_move ();
643 if (requires_array_length
) {
644 append_vala_array_length ();
647 if (gvaluecollector_h_needed
) {
648 cfile
.add_include ("gobject/gvaluecollector.h");
651 var comments
= source_file
.get_comments();
652 if (comments
!= null) {
653 foreach (Comment comment
in comments
) {
654 var ccomment
= new
CCodeComment (comment
.content
);
655 cfile
.add_comment (ccomment
);
659 if (!cfile
.store (source_file
.get_csource_filename (), source_file
.filename
, context
.version_header
, context
.debug
)) {
660 Report
.error (null, "unable to open `%s' for writing".printf (source_file
.get_csource_filename ()));
666 public virtual bool generate_enum_declaration (Enum en
, CCodeFile decl_space
) {
667 if (add_symbol_declaration (decl_space
, en
, en
.get_cname ())) {
671 var cenum
= new
CCodeEnum (en
.get_cname ());
673 cenum
.deprecated
= en
.deprecated
;
676 foreach (EnumValue ev
in en
.get_values ()) {
678 if (ev
.value
== null) {
679 c_ev
= new
CCodeEnumValue (ev
.get_cname ());
681 c_ev
.value
= new
CCodeConstant ("1 << %d".printf (flag_shift
));
685 ev
.value
.emit (this
);
686 c_ev
= new
CCodeEnumValue (ev
.get_cname (), get_cvalue (ev
.value
));
688 c_ev
.deprecated
= ev
.deprecated
;
689 cenum
.add_value (c_ev
);
692 decl_space
.add_type_definition (cenum
);
693 decl_space
.add_type_definition (new
CCodeNewline ());
695 if (!en
.has_type_id
) {
699 decl_space
.add_type_declaration (new
CCodeNewline ());
701 var macro
= "(%s_get_type ())".printf (en
.get_lower_case_cname (null));
702 decl_space
.add_type_declaration (new
CCodeMacroReplacement (en
.get_type_id (), macro
));
704 var fun_name
= "%s_get_type".printf (en
.get_lower_case_cname (null));
705 var regfun
= new
CCodeFunction (fun_name
, "GType");
706 regfun
.attributes
= "G_GNUC_CONST";
708 if (en
.access
== SymbolAccessibility
.PRIVATE
) {
709 regfun
.modifiers
= CCodeModifiers
.STATIC
;
710 // avoid C warning as this function is not always used
711 regfun
.attributes
= "G_GNUC_UNUSED";
714 decl_space
.add_function_declaration (regfun
);
719 public override void visit_enum (Enum en
) {
720 en
.accept_children (this
);
722 if (en
.comment
!= null) {
723 cfile
.add_type_member_definition (new
CCodeComment (en
.comment
.content
));
726 generate_enum_declaration (en
, cfile
);
728 if (!en
.is_internal_symbol ()) {
729 generate_enum_declaration (en
, header_file
);
731 if (!en
.is_private_symbol ()) {
732 generate_enum_declaration (en
, internal_header_file
);
736 public void visit_member (Symbol m
) {
737 /* stuff meant for all lockable members */
738 if (m is Lockable
&& ((Lockable
) m
).get_lock_used ()) {
739 CCodeExpression l
= new
CCodeIdentifier ("self");
740 var init_context
= class_init_context
;
741 var finalize_context
= class_finalize_context
;
743 if (m
.is_instance_member ()) {
744 l
= new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (l
, "priv"), get_symbol_lock_name (m
.name
));
745 init_context
= instance_init_context
;
746 finalize_context
= instance_finalize_context
;
747 } else if (m
.is_class_member ()) {
748 TypeSymbol parent
= (TypeSymbol
)m
.parent_symbol
;
750 var get_class_private_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf(parent
.get_upper_case_cname ())));
751 get_class_private_call
.add_argument (new
CCodeIdentifier ("klass"));
752 l
= new CCodeMemberAccess
.pointer (get_class_private_call
, get_symbol_lock_name (m
.name
));
754 l
= new
CCodeIdentifier (get_symbol_lock_name ("%s_%s".printf(m
.parent_symbol
.get_lower_case_cname (), m
.name
)));
757 push_context (init_context
);
758 var initf
= new
CCodeFunctionCall (new
CCodeIdentifier (mutex_type
.default_construction_method
.get_cname ()));
759 initf
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, l
));
760 ccode
.add_expression (initf
);
763 if (finalize_context
!= null) {
764 push_context (finalize_context
);
765 var fc
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_static_rec_mutex_free"));
766 fc
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, l
));
767 ccode
.add_expression (fc
);
773 public void generate_constant_declaration (Constant c
, CCodeFile decl_space
, bool definition
= false) {
774 if (c
.parent_symbol is Block
) {
779 if (add_symbol_declaration (decl_space
, c
, c
.get_cname ())) {
784 generate_type_declaration (c
.type_reference
, decl_space
);
788 var initializer_list
= c
.value as InitializerList
;
789 if (initializer_list
!= null) {
790 var cdecl
= new
CCodeDeclaration (c
.type_reference
.get_const_cname ());
792 if (c
.type_reference is ArrayType
) {
793 arr
= "[%d]".printf (initializer_list
.size
);
796 var cinitializer
= get_cvalue (c
.value
);
798 // never output value in header
799 // special case needed as this method combines declaration and definition
803 cdecl
.add_declarator (new
CCodeVariableDeclarator ("%s%s".printf (c
.get_cname (), arr
), cinitializer
));
804 if (c
.is_private_symbol ()) {
805 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
807 cdecl
.modifiers
= CCodeModifiers
.EXTERN
;
810 decl_space
.add_constant_declaration (cdecl
);
812 var cdefine
= new CCodeMacroReplacement
.with_expression (c
.get_cname (), get_cvalue (c
.value
));
813 decl_space
.add_type_member_declaration (cdefine
);
818 public override void visit_constant (Constant c
) {
819 if (c
.parent_symbol is Block
) {
822 generate_type_declaration (c
.type_reference
, cfile
);
826 string type_name
= c
.type_reference
.get_const_cname ();
828 if (c
.type_reference is ArrayType
) {
832 if (c
.type_reference
.compatible (string_type
)) {
833 type_name
= "const char";
837 var cinitializer
= get_cvalue (c
.value
);
839 ccode
.add_declaration (type_name
, new
CCodeVariableDeclarator ("%s%s".printf (c
.get_cname (), arr
), cinitializer
), CCodeModifiers
.STATIC
);
844 generate_constant_declaration (c
, cfile
, true);
846 if (!c
.is_internal_symbol ()) {
847 generate_constant_declaration (c
, header_file
);
849 if (!c
.is_private_symbol ()) {
850 generate_constant_declaration (c
, internal_header_file
);
854 public void generate_field_declaration (Field f
, CCodeFile decl_space
) {
855 if (add_symbol_declaration (decl_space
, f
, f
.get_cname ())) {
859 generate_type_declaration (f
.variable_type
, decl_space
);
861 string field_ctype
= f
.variable_type
.get_cname ();
863 field_ctype
= "volatile " + field_ctype
;
866 var cdecl
= new
CCodeDeclaration (field_ctype
);
867 cdecl
.add_declarator (new
CCodeVariableDeclarator (f
.get_cname (), null, f
.variable_type
.get_cdeclarator_suffix ()));
868 if (f
.is_private_symbol ()) {
869 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
871 cdecl
.modifiers
= CCodeModifiers
.EXTERN
;
874 cdecl
.modifiers
|= CCodeModifiers
.DEPRECATED
;
876 decl_space
.add_type_member_declaration (cdecl
);
878 if (f
.get_lock_used ()) {
879 // Declare mutex for static member
880 var flock
= new
CCodeDeclaration (mutex_type
.get_cname ());
881 var flock_decl
= new
CCodeVariableDeclarator (get_symbol_lock_name (f
.get_cname ()), new
CCodeConstant ("{0}"));
882 flock
.add_declarator (flock_decl
);
884 if (f
.is_private_symbol ()) {
885 flock
.modifiers
= CCodeModifiers
.STATIC
;
887 flock
.modifiers
= CCodeModifiers
.EXTERN
;
889 decl_space
.add_type_member_declaration (flock
);
892 if (f
.variable_type is ArrayType
&& !f
.no_array_length
) {
893 var array_type
= (ArrayType
) f
.variable_type
;
895 if (!array_type
.fixed_length
) {
896 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
897 var len_type
= int_type
.copy ();
899 cdecl
= new
CCodeDeclaration (len_type
.get_cname ());
900 cdecl
.add_declarator (new
CCodeVariableDeclarator (get_array_length_cname (f
.get_cname (), dim
)));
901 if (f
.is_private_symbol ()) {
902 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
904 cdecl
.modifiers
= CCodeModifiers
.EXTERN
;
906 decl_space
.add_type_member_declaration (cdecl
);
909 } else if (f
.variable_type is DelegateType
) {
910 var delegate_type
= (DelegateType
) f
.variable_type
;
911 if (delegate_type
.delegate_symbol
.has_target
) {
912 // create field to store delegate target
914 cdecl
= new
CCodeDeclaration ("gpointer");
915 cdecl
.add_declarator (new
CCodeVariableDeclarator (get_delegate_target_cname (f
.get_cname ())));
916 if (f
.is_private_symbol ()) {
917 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
919 cdecl
.modifiers
= CCodeModifiers
.EXTERN
;
921 decl_space
.add_type_member_declaration (cdecl
);
923 if (delegate_type
.value_owned
) {
924 cdecl
= new
CCodeDeclaration ("GDestroyNotify");
925 cdecl
.add_declarator (new
CCodeVariableDeclarator (get_delegate_target_destroy_notify_cname (f
.get_cname ())));
926 if (f
.is_private_symbol ()) {
927 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
929 cdecl
.modifiers
= CCodeModifiers
.EXTERN
;
931 decl_space
.add_type_member_declaration (cdecl
);
937 public override void visit_field (Field f
) {
940 check_type (f
.variable_type
);
942 var cl
= f
.parent_symbol as Class
;
943 bool is_gtypeinstance
= (cl
!= null && !cl
.is_compact
);
945 CCodeExpression lhs
= null;
947 string field_ctype
= f
.variable_type
.get_cname ();
949 field_ctype
= "volatile " + field_ctype
;
952 if (f
.binding
== MemberBinding
.INSTANCE
) {
953 if (is_gtypeinstance
&& f
.access
== SymbolAccessibility
.PRIVATE
) {
954 lhs
= new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("self"), "priv"), f
.get_cname ());
956 lhs
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("self"), f
.get_cname ());
959 if (f
.initializer
!= null) {
960 push_context (instance_init_context
);
962 f
.initializer
.emit (this
);
964 var rhs
= get_cvalue (f
.initializer
);
966 ccode
.add_assignment (lhs
, rhs
);
968 if (f
.variable_type is ArrayType
&& !f
.no_array_length
&&
969 f
.initializer is ArrayCreationExpression
) {
970 var array_type
= (ArrayType
) f
.variable_type
;
971 var this_access
= new MemberAccess
.simple ("this");
972 this_access
.value_type
= get_data_type_for_symbol ((TypeSymbol
) f
.parent_symbol
);
973 set_cvalue (this_access
, new
CCodeIdentifier ("self"));
974 var ma
= new
MemberAccess (this_access
, f
.name
);
975 ma
.symbol_reference
= f
;
976 ma
.value_type
= f
.variable_type
.copy ();
977 visit_member_access (ma
);
979 List
<Expression
> sizes
= ((ArrayCreationExpression
) f
.initializer
).get_sizes ();
980 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
981 var array_len_lhs
= get_array_length_cexpression (ma
, dim
);
982 var size
= sizes
[dim
- 1];
983 ccode
.add_assignment (array_len_lhs
, get_cvalue (size
));
986 if (array_type
.rank
== 1 && f
.is_internal_symbol ()) {
987 var lhs_array_size
= get_array_size_cvalue (ma
.target_value
);
988 var rhs_array_len
= get_array_length_cexpression (ma
, 1);
989 ccode
.add_assignment (lhs_array_size
, rhs_array_len
);
993 foreach (LocalVariable local
in temp_ref_vars
) {
994 ccode
.add_expression (destroy_local (local
));
997 temp_ref_vars
.clear ();
1002 if (requires_destroy (f
.variable_type
) && instance_finalize_context
!= null) {
1003 push_context (instance_finalize_context
);
1005 var this_access
= new MemberAccess
.simple ("this");
1006 this_access
.value_type
= get_data_type_for_symbol ((TypeSymbol
) f
.parent_symbol
);
1008 var field_st
= f
.parent_symbol as Struct
;
1009 if (field_st
!= null && !field_st
.is_simple_type ()) {
1010 set_cvalue (this_access
, new
CCodeIdentifier ("(*self)"));
1012 set_cvalue (this_access
, new
CCodeIdentifier ("self"));
1015 ccode
.add_expression (destroy_field (f
, this_access
));
1019 } else if (f
.binding
== MemberBinding
.CLASS
) {
1020 if (!is_gtypeinstance
) {
1021 Report
.error (f
.source_reference
, "class fields are not supported in compact classes");
1026 if (f
.access
== SymbolAccessibility
.PRIVATE
) {
1027 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf (cl
.get_upper_case_cname ())));
1028 ccall
.add_argument (new
CCodeIdentifier ("klass"));
1029 lhs
= new
CCodeMemberAccess (ccall
, f
.get_cname (), true);
1031 lhs
= new
CCodeMemberAccess (new
CCodeIdentifier ("klass"), f
.get_cname (), true);
1034 if (f
.initializer
!= null) {
1035 push_context (class_init_context
);
1037 f
.initializer
.emit (this
);
1039 var rhs
= get_cvalue (f
.initializer
);
1041 ccode
.add_assignment (lhs
, rhs
);
1043 foreach (LocalVariable local
in temp_ref_vars
) {
1044 ccode
.add_expression (destroy_local (local
));
1047 temp_ref_vars
.clear ();
1052 generate_field_declaration (f
, cfile
);
1054 if (!f
.is_internal_symbol ()) {
1055 generate_field_declaration (f
, header_file
);
1057 if (!f
.is_private_symbol ()) {
1058 generate_field_declaration (f
, internal_header_file
);
1061 lhs
= new
CCodeIdentifier (f
.get_cname ());
1063 var var_decl
= new
CCodeVariableDeclarator (f
.get_cname (), null, f
.variable_type
.get_cdeclarator_suffix ());
1064 var_decl
.initializer
= default_value_for_type (f
.variable_type
, true);
1066 if (class_init_context
!= null) {
1067 push_context (class_init_context
);
1069 push_context (new
EmitContext ());
1072 if (f
.initializer
!= null) {
1073 f
.initializer
.emit (this
);
1075 var init
= get_cvalue (f
.initializer
);
1076 if (is_constant_ccode_expression (init
)) {
1077 var_decl
.initializer
= init
;
1081 var var_def
= new
CCodeDeclaration (field_ctype
);
1082 var_def
.add_declarator (var_decl
);
1083 if (!f
.is_private_symbol ()) {
1084 var_def
.modifiers
= CCodeModifiers
.EXTERN
;
1086 var_def
.modifiers
= CCodeModifiers
.STATIC
;
1088 cfile
.add_type_member_declaration (var_def
);
1090 /* add array length fields where necessary */
1091 if (f
.variable_type is ArrayType
&& !f
.no_array_length
) {
1092 var array_type
= (ArrayType
) f
.variable_type
;
1094 if (!array_type
.fixed_length
) {
1095 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1096 var len_type
= int_type
.copy ();
1098 var len_def
= new
CCodeDeclaration (len_type
.get_cname ());
1099 len_def
.add_declarator (new
CCodeVariableDeclarator (get_array_length_cname (f
.get_cname (), dim
), new
CCodeConstant ("0")));
1100 if (!f
.is_private_symbol ()) {
1101 len_def
.modifiers
= CCodeModifiers
.EXTERN
;
1103 len_def
.modifiers
= CCodeModifiers
.STATIC
;
1105 cfile
.add_type_member_declaration (len_def
);
1108 if (array_type
.rank
== 1 && f
.is_internal_symbol ()) {
1109 var len_type
= int_type
.copy ();
1111 var cdecl
= new
CCodeDeclaration (len_type
.get_cname ());
1112 cdecl
.add_declarator (new
CCodeVariableDeclarator (get_array_size_cname (f
.get_cname ()), new
CCodeConstant ("0")));
1113 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
1114 cfile
.add_type_member_declaration (cdecl
);
1117 } else if (f
.variable_type is DelegateType
) {
1118 var delegate_type
= (DelegateType
) f
.variable_type
;
1119 if (delegate_type
.delegate_symbol
.has_target
) {
1120 // create field to store delegate target
1122 var target_def
= new
CCodeDeclaration ("gpointer");
1123 target_def
.add_declarator (new
CCodeVariableDeclarator (get_delegate_target_cname (f
.get_cname ()), new
CCodeConstant ("NULL")));
1124 if (!f
.is_private_symbol ()) {
1125 target_def
.modifiers
= CCodeModifiers
.EXTERN
;
1127 target_def
.modifiers
= CCodeModifiers
.STATIC
;
1129 cfile
.add_type_member_declaration (target_def
);
1131 if (delegate_type
.value_owned
) {
1132 var target_destroy_notify_def
= new
CCodeDeclaration ("GDestroyNotify");
1133 target_destroy_notify_def
.add_declarator (new
CCodeVariableDeclarator (get_delegate_target_destroy_notify_cname (f
.get_cname ()), new
CCodeConstant ("NULL")));
1134 if (!f
.is_private_symbol ()) {
1135 target_destroy_notify_def
.modifiers
= CCodeModifiers
.EXTERN
;
1137 target_destroy_notify_def
.modifiers
= CCodeModifiers
.STATIC
;
1139 cfile
.add_type_member_declaration (target_destroy_notify_def
);
1145 if (f
.initializer
!= null) {
1146 var rhs
= get_cvalue (f
.initializer
);
1147 if (!is_constant_ccode_expression (rhs
)) {
1148 if (f
.parent_symbol is Class
) {
1149 if (f
.initializer is InitializerList
) {
1150 ccode
.open_block ();
1152 var temp_decl
= get_temp_variable (f
.variable_type
);
1153 var vardecl
= new CCodeVariableDeclarator
.zero (temp_decl
.name
, rhs
);
1154 ccode
.add_declaration (temp_decl
.variable_type
.get_cname (), vardecl
);
1156 var tmp
= get_variable_cexpression (get_variable_cname (temp_decl
.name
));
1157 ccode
.add_assignment (lhs
, tmp
);
1161 ccode
.add_assignment (lhs
, rhs
);
1164 if (f
.variable_type is ArrayType
&& !f
.no_array_length
&&
1165 f
.initializer is ArrayCreationExpression
) {
1166 var array_type
= (ArrayType
) f
.variable_type
;
1167 var ma
= new MemberAccess
.simple (f
.name
);
1168 ma
.symbol_reference
= f
;
1169 ma
.value_type
= f
.variable_type
.copy ();
1170 visit_member_access (ma
);
1172 List
<Expression
> sizes
= ((ArrayCreationExpression
) f
.initializer
).get_sizes ();
1173 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1174 var array_len_lhs
= get_array_length_cexpression (ma
, dim
);
1175 var size
= sizes
[dim
- 1];
1176 ccode
.add_assignment (array_len_lhs
, get_cvalue (size
));
1181 Report
.error (f
.source_reference
, "Non-constant field initializers not supported in this context");
1191 public bool is_constant_ccode_expression (CCodeExpression cexpr
) {
1192 if (cexpr is CCodeConstant
) {
1194 } else if (cexpr is CCodeCastExpression
) {
1195 var ccast
= (CCodeCastExpression
) cexpr
;
1196 return is_constant_ccode_expression (ccast
.inner
);
1197 } else if (cexpr is CCodeBinaryExpression
) {
1198 var cbinary
= (CCodeBinaryExpression
) cexpr
;
1199 return is_constant_ccode_expression (cbinary
.left
) && is_constant_ccode_expression (cbinary
.right
);
1202 var cparenthesized
= (cexpr as CCodeParenthesizedExpression
);
1203 return (null != cparenthesized
&& is_constant_ccode_expression (cparenthesized
.inner
));
1207 * Returns whether the passed cexpr is a pure expression, i.e. an
1208 * expression without side-effects.
1210 public bool is_pure_ccode_expression (CCodeExpression cexpr
) {
1211 if (cexpr is CCodeConstant
|| cexpr is CCodeIdentifier
) {
1213 } else if (cexpr is CCodeBinaryExpression
) {
1214 var cbinary
= (CCodeBinaryExpression
) cexpr
;
1215 return is_pure_ccode_expression (cbinary
.left
) && is_constant_ccode_expression (cbinary
.right
);
1216 } else if (cexpr is CCodeUnaryExpression
) {
1217 var cunary
= (CCodeUnaryExpression
) cexpr
;
1218 switch (cunary
.operator
) {
1219 case CCodeUnaryOperator
.PREFIX_INCREMENT
:
1220 case CCodeUnaryOperator
.PREFIX_DECREMENT
:
1221 case CCodeUnaryOperator
.POSTFIX_INCREMENT
:
1222 case CCodeUnaryOperator
.POSTFIX_DECREMENT
:
1225 return is_pure_ccode_expression (cunary
.inner
);
1227 } else if (cexpr is CCodeMemberAccess
) {
1228 var cma
= (CCodeMemberAccess
) cexpr
;
1229 return is_pure_ccode_expression (cma
.inner
);
1230 } else if (cexpr is CCodeElementAccess
) {
1231 var cea
= (CCodeElementAccess
) cexpr
;
1232 return is_pure_ccode_expression (cea
.container
) && is_pure_ccode_expression (cea
.index
);
1233 } else if (cexpr is CCodeCastExpression
) {
1234 var ccast
= (CCodeCastExpression
) cexpr
;
1235 return is_pure_ccode_expression (ccast
.inner
);
1236 } else if (cexpr is CCodeParenthesizedExpression
) {
1237 var cparenthesized
= (CCodeParenthesizedExpression
) cexpr
;
1238 return is_pure_ccode_expression (cparenthesized
.inner
);
1244 public override void visit_formal_parameter (Parameter p
) {
1246 check_type (p
.variable_type
);
1250 public override void visit_property (Property prop
) {
1251 visit_member (prop
);
1253 check_type (prop
.property_type
);
1255 if (prop
.get_accessor
!= null) {
1256 prop
.get_accessor
.accept (this
);
1258 if (prop
.set_accessor
!= null) {
1259 prop
.set_accessor
.accept (this
);
1263 public void generate_type_declaration (DataType type
, CCodeFile decl_space
) {
1264 if (type is ObjectType
) {
1265 var object_type
= (ObjectType
) type
;
1266 if (object_type
.type_symbol is Class
) {
1267 generate_class_declaration ((Class
) object_type
.type_symbol
, decl_space
);
1268 } else if (object_type
.type_symbol is Interface
) {
1269 generate_interface_declaration ((Interface
) object_type
.type_symbol
, decl_space
);
1271 } else if (type is DelegateType
) {
1272 var deleg_type
= (DelegateType
) type
;
1273 var d
= deleg_type
.delegate_symbol
;
1274 generate_delegate_declaration (d
, decl_space
);
1275 } else if (type
.data_type is Enum
) {
1276 var en
= (Enum
) type
.data_type
;
1277 generate_enum_declaration (en
, decl_space
);
1278 } else if (type is ValueType
) {
1279 var value_type
= (ValueType
) type
;
1280 generate_struct_declaration ((Struct
) value_type
.type_symbol
, decl_space
);
1281 } else if (type is ArrayType
) {
1282 var array_type
= (ArrayType
) type
;
1283 generate_type_declaration (array_type
.element_type
, decl_space
);
1284 } else if (type is ErrorType
) {
1285 var error_type
= (ErrorType
) type
;
1286 if (error_type
.error_domain
!= null) {
1287 generate_error_domain_declaration (error_type
.error_domain
, decl_space
);
1289 } else if (type is PointerType
) {
1290 var pointer_type
= (PointerType
) type
;
1291 generate_type_declaration (pointer_type
.base_type
, decl_space
);
1294 foreach (DataType type_arg
in type
.get_type_arguments ()) {
1295 generate_type_declaration (type_arg
, decl_space
);
1299 public virtual void generate_class_struct_declaration (Class cl
, CCodeFile decl_space
) {
1302 public virtual void generate_struct_declaration (Struct st
, CCodeFile decl_space
) {
1305 public virtual void generate_delegate_declaration (Delegate d
, CCodeFile decl_space
) {
1308 public virtual void generate_cparameters (Method m
, CCodeFile decl_space
, Map
<int,CCodeParameter
> cparam_map
, CCodeFunction func
, CCodeFunctionDeclarator? vdeclarator
= null, Map
<int,CCodeExpression
>? carg_map
= null, CCodeFunctionCall? vcall
= null, int direction
= 3) {
1311 public void generate_property_accessor_declaration (PropertyAccessor acc
, CCodeFile decl_space
) {
1312 if (add_symbol_declaration (decl_space
, acc
, acc
.get_cname ())) {
1316 var prop
= (Property
) acc
.prop
;
1318 bool returns_real_struct
= acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ();
1321 CCodeParameter cvalueparam
;
1322 if (returns_real_struct
) {
1323 cvalueparam
= new
CCodeParameter ("result", acc
.value_type
.get_cname () + "*");
1324 } else if (!acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ()) {
1325 cvalueparam
= new
CCodeParameter ("value", acc
.value_type
.get_cname () + "*");
1327 cvalueparam
= new
CCodeParameter ("value", acc
.value_type
.get_cname ());
1329 generate_type_declaration (acc
.value_type
, decl_space
);
1331 CCodeFunction function
;
1332 if (acc
.readable
&& !returns_real_struct
) {
1333 function
= new
CCodeFunction (acc
.get_cname (), acc
.value_type
.get_cname ());
1335 function
= new
CCodeFunction (acc
.get_cname (), "void");
1338 if (prop
.binding
== MemberBinding
.INSTANCE
) {
1339 var t
= (TypeSymbol
) prop
.parent_symbol
;
1340 var this_type
= get_data_type_for_symbol (t
);
1341 generate_type_declaration (this_type
, decl_space
);
1342 var cselfparam
= new
CCodeParameter ("self", this_type
.get_cname ());
1344 cselfparam
.type_name
+= "*";
1347 function
.add_parameter (cselfparam
);
1350 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1351 function
.add_parameter (cvalueparam
);
1354 if (acc
.value_type is ArrayType
) {
1355 var array_type
= (ArrayType
) acc
.value_type
;
1357 var length_ctype
= "int";
1359 length_ctype
= "int*";
1362 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1363 function
.add_parameter (new
CCodeParameter (get_array_length_cname (acc
.readable ?
"result" : "value", dim
), length_ctype
));
1365 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1366 function
.add_parameter (new
CCodeParameter (get_delegate_target_cname (acc
.readable ?
"result" : "value"), acc
.readable ?
"gpointer*" : "gpointer"));
1369 if (prop
.is_private_symbol () || (!acc
.readable
&& !acc
.writable
) || acc
.access
== SymbolAccessibility
.PRIVATE
) {
1370 function
.modifiers
|= CCodeModifiers
.STATIC
;
1372 decl_space
.add_function_declaration (function
);
1375 public override void visit_property_accessor (PropertyAccessor acc
) {
1376 push_context (new
EmitContext (acc
));
1378 var prop
= (Property
) acc
.prop
;
1380 if (acc
.comment
!= null) {
1381 cfile
.add_type_member_definition (new
CCodeComment (acc
.comment
.content
));
1384 bool returns_real_struct
= acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ();
1386 if (acc
.result_var
!= null) {
1387 acc
.result_var
.accept (this
);
1390 var t
= (TypeSymbol
) prop
.parent_symbol
;
1392 if (acc
.construction
&& !t
.is_subtype_of (gobject_type
)) {
1393 Report
.error (acc
.source_reference
, "construct properties require GLib.Object");
1396 } else if (acc
.construction
&& !is_gobject_property (prop
)) {
1397 Report
.error (acc
.source_reference
, "construct properties not supported for specified property type");
1402 // do not declare overriding properties and interface implementations
1403 if (prop
.is_abstract
|| prop
.is_virtual
1404 || (prop
.base_property
== null && prop
.base_interface_property
== null)) {
1405 generate_property_accessor_declaration (acc
, cfile
);
1407 // do not declare construct-only properties in header files
1408 if (acc
.readable
|| acc
.writable
) {
1409 if (!prop
.is_internal_symbol ()
1410 && (acc
.access
== SymbolAccessibility
.PUBLIC
1411 || acc
.access
== SymbolAccessibility
.PROTECTED
)) {
1412 generate_property_accessor_declaration (acc
, header_file
);
1414 if (!prop
.is_private_symbol () && acc
.access
!= SymbolAccessibility
.PRIVATE
) {
1415 generate_property_accessor_declaration (acc
, internal_header_file
);
1420 if (acc
.source_type
== SourceFileType
.FAST
) {
1424 var this_type
= get_data_type_for_symbol (t
);
1425 var cselfparam
= new
CCodeParameter ("self", this_type
.get_cname ());
1427 cselfparam
.type_name
+= "*";
1429 CCodeParameter cvalueparam
;
1430 if (returns_real_struct
) {
1431 cvalueparam
= new
CCodeParameter ("result", acc
.value_type
.get_cname () + "*");
1432 } else if (!acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ()) {
1433 cvalueparam
= new
CCodeParameter ("value", acc
.value_type
.get_cname () + "*");
1435 cvalueparam
= new
CCodeParameter ("value", acc
.value_type
.get_cname ());
1438 if (prop
.is_abstract
|| prop
.is_virtual
) {
1439 CCodeFunction function
;
1440 if (acc
.readable
&& !returns_real_struct
) {
1441 function
= new
CCodeFunction (acc
.get_cname (), current_return_type
.get_cname ());
1443 function
= new
CCodeFunction (acc
.get_cname (), "void");
1445 function
.add_parameter (cselfparam
);
1446 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1447 function
.add_parameter (cvalueparam
);
1450 if (acc
.value_type is ArrayType
) {
1451 var array_type
= (ArrayType
) acc
.value_type
;
1453 var length_ctype
= "int";
1455 length_ctype
= "int*";
1458 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1459 function
.add_parameter (new
CCodeParameter (get_array_length_cname (acc
.readable ?
"result" : "value", dim
), length_ctype
));
1461 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1462 function
.add_parameter (new
CCodeParameter (get_delegate_target_cname (acc
.readable ?
"result" : "value"), acc
.readable ?
"gpointer*" : "gpointer"));
1465 if (prop
.is_private_symbol () || !(acc
.readable
|| acc
.writable
) || acc
.access
== SymbolAccessibility
.PRIVATE
) {
1466 // accessor function should be private if the property is an internal symbol or it's a construct-only setter
1467 function
.modifiers
|= CCodeModifiers
.STATIC
;
1470 push_function (function
);
1472 CCodeFunctionCall vcast
= null;
1473 if (prop
.parent_symbol is Interface
) {
1474 var iface
= (Interface
) prop
.parent_symbol
;
1476 vcast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_INTERFACE".printf (iface
.get_upper_case_cname (null))));
1478 var cl
= (Class
) prop
.parent_symbol
;
1480 vcast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS".printf (cl
.get_upper_case_cname (null))));
1482 vcast
.add_argument (new
CCodeIdentifier ("self"));
1485 var vcall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (vcast
, "get_%s".printf (prop
.name
)));
1486 vcall
.add_argument (new
CCodeIdentifier ("self"));
1487 if (returns_real_struct
) {
1488 vcall
.add_argument (new
CCodeIdentifier ("result"));
1489 ccode
.add_expression (vcall
);
1491 if (acc
.value_type is ArrayType
) {
1492 var array_type
= (ArrayType
) acc
.value_type
;
1494 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1495 var len_expr
= new
CCodeIdentifier (get_array_length_cname ("result", dim
));
1496 vcall
.add_argument (len_expr
);
1498 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1499 vcall
.add_argument (new
CCodeIdentifier (get_delegate_target_cname ("result")));
1502 ccode
.add_return (vcall
);
1505 var vcall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (vcast
, "set_%s".printf (prop
.name
)));
1506 vcall
.add_argument (new
CCodeIdentifier ("self"));
1507 vcall
.add_argument (new
CCodeIdentifier ("value"));
1509 if (acc
.value_type is ArrayType
) {
1510 var array_type
= (ArrayType
) acc
.value_type
;
1512 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1513 var len_expr
= new
CCodeIdentifier (get_array_length_cname ("value", dim
));
1514 vcall
.add_argument (len_expr
);
1516 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1517 vcall
.add_argument (new
CCodeIdentifier (get_delegate_target_cname ("value")));
1520 ccode
.add_expression (vcall
);
1525 cfile
.add_function (function
);
1528 if (!prop
.is_abstract
) {
1529 bool is_virtual
= prop
.base_property
!= null || prop
.base_interface_property
!= null;
1534 cname
= "%s_real_get_%s".printf (t
.get_lower_case_cname (null), prop
.name
);
1536 cname
= "%s_real_set_%s".printf (t
.get_lower_case_cname (null), prop
.name
);
1539 cname
= acc
.get_cname ();
1542 CCodeFunction function
;
1543 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1544 function
= new
CCodeFunction (cname
, "void");
1546 function
= new
CCodeFunction (cname
, acc
.value_type
.get_cname ());
1549 ObjectType base_type
= null;
1550 if (prop
.binding
== MemberBinding
.INSTANCE
) {
1552 if (prop
.base_property
!= null) {
1553 base_type
= new
ObjectType ((ObjectTypeSymbol
) prop
.base_property
.parent_symbol
);
1554 } else if (prop
.base_interface_property
!= null) {
1555 base_type
= new
ObjectType ((ObjectTypeSymbol
) prop
.base_interface_property
.parent_symbol
);
1557 function
.modifiers
|= CCodeModifiers
.STATIC
;
1558 function
.add_parameter (new
CCodeParameter ("base", base_type
.get_cname ()));
1560 function
.add_parameter (cselfparam
);
1563 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1564 function
.add_parameter (cvalueparam
);
1567 if (acc
.value_type is ArrayType
) {
1568 var array_type
= (ArrayType
) acc
.value_type
;
1570 var length_ctype
= "int";
1572 length_ctype
= "int*";
1575 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1576 function
.add_parameter (new
CCodeParameter (get_array_length_cname (acc
.readable ?
"result" : "value", dim
), length_ctype
));
1578 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1579 function
.add_parameter (new
CCodeParameter (get_delegate_target_cname (acc
.readable ?
"result" : "value"), acc
.readable ?
"gpointer*" : "gpointer"));
1583 if (prop
.is_private_symbol () || !(acc
.readable
|| acc
.writable
) || acc
.access
== SymbolAccessibility
.PRIVATE
) {
1584 // accessor function should be private if the property is an internal symbol or it's a construct-only setter
1585 function
.modifiers
|= CCodeModifiers
.STATIC
;
1589 push_function (function
);
1591 if (prop
.binding
== MemberBinding
.INSTANCE
&& !is_virtual
) {
1592 if (!acc
.readable
|| returns_real_struct
) {
1593 create_property_type_check_statement (prop
, false, t
, true, "self");
1595 create_property_type_check_statement (prop
, true, t
, true, "self");
1599 if (acc
.readable
&& !returns_real_struct
) {
1600 // do not declare result variable if exit block is known to be unreachable
1601 if (acc
.return_block
== null || acc
.return_block
.get_predecessors ().size
> 0) {
1602 ccode
.add_declaration (acc
.value_type
.get_cname (), new
CCodeVariableDeclarator ("result"));
1607 ccode
.add_declaration (this_type
.get_cname (), new
CCodeVariableDeclarator ("self"));
1608 ccode
.add_assignment (new
CCodeIdentifier ("self"), transform_expression (new
CCodeIdentifier ("base"), base_type
, this_type
));
1611 acc
.body
.emit (this
);
1613 if (current_method_inner_error
) {
1614 ccode
.add_declaration ("GError *", new CCodeVariableDeclarator
.zero ("_inner_error_", new
CCodeConstant ("NULL")));
1617 // notify on property changes
1618 if (is_gobject_property (prop
) &&
1620 (acc
.writable
|| acc
.construction
)) {
1621 var notify_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_object_notify"));
1622 notify_call
.add_argument (new
CCodeCastExpression (new
CCodeIdentifier ("self"), "GObject *"));
1623 notify_call
.add_argument (prop
.get_canonical_cconstant ());
1624 ccode
.add_expression (notify_call
);
1627 cfile
.add_function (function
);
1633 public override void visit_destructor (Destructor d
) {
1634 if (d
.binding
== MemberBinding
.STATIC
&& !in_plugin
) {
1635 Report
.error (d
.source_reference
, "static destructors are only supported for dynamic types");
1641 public int get_block_id (Block b
) {
1642 int result
= block_map
[b
];
1644 result
= ++next_block_id
;
1645 block_map
[b
] = result
;
1650 void capture_parameter (Parameter param
, CCodeStruct data
, int block_id
) {
1651 generate_type_declaration (param
.variable_type
, cfile
);
1653 var param_type
= param
.variable_type
.copy ();
1654 param_type
.value_owned
= true;
1655 data
.add_field (param_type
.get_cname (), get_variable_cname (param
.name
));
1657 bool is_unowned_delegate
= param
.variable_type is DelegateType
&& !param
.variable_type
.value_owned
;
1659 // create copy if necessary as captured variables may need to be kept alive
1660 CCodeExpression cparam
= get_variable_cexpression (param
.name
);
1661 if (param
.variable_type
.is_real_non_null_struct_type ()) {
1662 cparam
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, cparam
);
1664 if (requires_copy (param_type
) && !param
.variable_type
.value_owned
&& !is_unowned_delegate
) {
1665 var ma
= new MemberAccess
.simple (param
.name
);
1666 ma
.symbol_reference
= param
;
1667 ma
.value_type
= param
.variable_type
.copy ();
1668 // directly access parameters in ref expressions
1669 param
.captured
= false;
1670 visit_member_access (ma
);
1671 cparam
= get_ref_cexpression (param
.variable_type
, cparam
, ma
, param
);
1672 param
.captured
= true;
1675 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), get_variable_cname (param
.name
)), cparam
);
1677 if (param
.variable_type is ArrayType
) {
1678 var array_type
= (ArrayType
) param
.variable_type
;
1679 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1680 data
.add_field ("gint", get_parameter_array_length_cname (param
, dim
));
1681 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), get_array_length_cname (get_variable_cname (param
.name
), dim
)), new
CCodeIdentifier (get_array_length_cname (get_variable_cname (param
.name
), dim
)));
1683 } else if (param
.variable_type is DelegateType
) {
1684 CCodeExpression target_expr
;
1685 CCodeExpression delegate_target_destroy_notify
;
1686 if (is_in_coroutine ()) {
1687 target_expr
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("data"), get_delegate_target_cname (get_variable_cname (param
.name
)));
1688 delegate_target_destroy_notify
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("data"), get_delegate_target_destroy_notify_cname (get_variable_cname (param
.name
)));
1690 target_expr
= new
CCodeIdentifier (get_delegate_target_cname (get_variable_cname (param
.name
)));
1691 delegate_target_destroy_notify
= new
CCodeIdentifier (get_delegate_target_destroy_notify_cname (get_variable_cname (param
.name
)));
1694 data
.add_field ("gpointer", get_delegate_target_cname (get_variable_cname (param
.name
)));
1695 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), get_delegate_target_cname (get_variable_cname (param
.name
))), target_expr
);
1696 if (param
.variable_type
.value_owned
) {
1697 data
.add_field ("GDestroyNotify", get_delegate_target_destroy_notify_cname (get_variable_cname (param
.name
)));
1698 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), get_delegate_target_destroy_notify_cname (get_variable_cname (param
.name
))), delegate_target_destroy_notify
);
1703 public override void visit_block (Block b
) {
1704 emit_context
.push_symbol (b
);
1706 var local_vars
= b
.get_local_variables ();
1708 if (b
.parent_node is Block
|| b
.parent_node is SwitchStatement
) {
1709 ccode
.open_block ();
1713 var parent_block
= next_closure_block (b
.parent_symbol
);
1715 int block_id
= get_block_id (b
);
1716 string struct_name
= "Block%dData".printf (block_id
);
1718 var data
= new
CCodeStruct ("_" + struct_name
);
1719 data
.add_field ("int", "_ref_count_");
1720 if (parent_block
!= null) {
1721 int parent_block_id
= get_block_id (parent_block
);
1723 data
.add_field ("Block%dData *".printf (parent_block_id
), "_data%d_".printf (parent_block_id
));
1725 if (in_constructor
|| (current_method
!= null && current_method
.binding
== MemberBinding
.INSTANCE
) ||
1726 (current_property_accessor
!= null && current_property_accessor
.prop
.binding
== MemberBinding
.INSTANCE
)) {
1727 data
.add_field ("%s *".printf (current_class
.get_cname ()), "self");
1730 if (current_method
!= null) {
1731 // allow capturing generic type parameters
1732 foreach (var type_param
in current_method
.get_type_parameters ()) {
1735 func_name
= "%s_type".printf (type_param
.name
.down ());
1736 data
.add_field ("GType", func_name
);
1738 func_name
= "%s_dup_func".printf (type_param
.name
.down ());
1739 data
.add_field ("GBoxedCopyFunc", func_name
);
1741 func_name
= "%s_destroy_func".printf (type_param
.name
.down ());
1742 data
.add_field ("GDestroyNotify", func_name
);
1746 foreach (var local
in local_vars
) {
1747 if (local
.captured
) {
1748 generate_type_declaration (local
.variable_type
, cfile
);
1750 data
.add_field (local
.variable_type
.get_cname (), get_variable_cname (local
.name
) + local
.variable_type
.get_cdeclarator_suffix ());
1752 if (local
.variable_type is ArrayType
) {
1753 var array_type
= (ArrayType
) local
.variable_type
;
1754 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1755 data
.add_field ("gint", get_array_length_cname (get_variable_cname (local
.name
), dim
));
1757 data
.add_field ("gint", get_array_size_cname (get_variable_cname (local
.name
)));
1758 } else if (local
.variable_type is DelegateType
) {
1759 data
.add_field ("gpointer", get_delegate_target_cname (get_variable_cname (local
.name
)));
1760 if (local
.variable_type
.value_owned
) {
1761 data
.add_field ("GDestroyNotify", get_delegate_target_destroy_notify_cname (get_variable_cname (local
.name
)));
1767 var data_alloc
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_slice_new0"));
1768 data_alloc
.add_argument (new
CCodeIdentifier (struct_name
));
1770 if (is_in_coroutine ()) {
1771 closure_struct
.add_field (struct_name
+ "*", "_data%d_".printf (block_id
));
1773 ccode
.add_declaration (struct_name
+ "*", new
CCodeVariableDeclarator ("_data%d_".printf (block_id
)));
1775 ccode
.add_assignment (get_variable_cexpression ("_data%d_".printf (block_id
)), data_alloc
);
1777 // initialize ref_count
1778 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "_ref_count_"), new
CCodeIdentifier ("1"));
1780 if (parent_block
!= null) {
1781 int parent_block_id
= get_block_id (parent_block
);
1783 var ref_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_ref".printf (parent_block_id
)));
1784 ref_call
.add_argument (get_variable_cexpression ("_data%d_".printf (parent_block_id
)));
1786 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "_data%d_".printf (parent_block_id
)), ref_call
);
1788 if (in_constructor
|| (current_method
!= null && current_method
.binding
== MemberBinding
.INSTANCE
&&
1789 (!(current_method is CreationMethod
) || current_method
.body
!= b
)) ||
1790 (current_property_accessor
!= null && current_property_accessor
.prop
.binding
== MemberBinding
.INSTANCE
)) {
1791 var ref_call
= new
CCodeFunctionCall (get_dup_func_expression (new
ObjectType (current_class
), b
.source_reference
));
1792 ref_call
.add_argument (get_result_cexpression ("self"));
1794 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "self"), ref_call
);
1797 if (current_method
!= null) {
1798 // allow capturing generic type parameters
1799 foreach (var type_param
in current_method
.get_type_parameters ()) {
1802 func_name
= "%s_type".printf (type_param
.name
.down ());
1803 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), func_name
), new
CCodeIdentifier (func_name
));
1805 func_name
= "%s_dup_func".printf (type_param
.name
.down ());
1806 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), func_name
), new
CCodeIdentifier (func_name
));
1808 func_name
= "%s_destroy_func".printf (type_param
.name
.down ());
1809 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), func_name
), new
CCodeIdentifier (func_name
));
1814 if (b
.parent_symbol is Method
) {
1815 var m
= (Method
) b
.parent_symbol
;
1817 // parameters are captured with the top-level block of the method
1818 foreach (var param
in m
.get_parameters ()) {
1819 if (param
.captured
) {
1820 capture_parameter (param
, data
, block_id
);
1825 // capture async data to allow invoking callback from inside closure
1826 data
.add_field ("gpointer", "_async_data_");
1828 // async method is suspended while waiting for callback,
1829 // so we never need to care about memory management of async data
1830 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "_async_data_"), new
CCodeIdentifier ("data"));
1832 } else if (b
.parent_symbol is PropertyAccessor
) {
1833 var acc
= (PropertyAccessor
) b
.parent_symbol
;
1835 if (!acc
.readable
&& acc
.value_parameter
.captured
) {
1836 capture_parameter (acc
.value_parameter
, data
, block_id
);
1840 var typedef
= new
CCodeTypeDefinition ("struct _" + struct_name
, new
CCodeVariableDeclarator (struct_name
));
1841 cfile
.add_type_declaration (typedef
);
1842 cfile
.add_type_definition (data
);
1844 // create ref/unref functions
1845 var ref_fun
= new
CCodeFunction ("block%d_data_ref".printf (block_id
), struct_name
+ "*");
1846 ref_fun
.add_parameter (new
CCodeParameter ("_data%d_".printf (block_id
), struct_name
+ "*"));
1847 ref_fun
.modifiers
= CCodeModifiers
.STATIC
;
1848 cfile
.add_function_declaration (ref_fun
);
1849 ref_fun
.block
= new
CCodeBlock ();
1851 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_atomic_int_inc"));
1852 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_ref_count_")));
1853 ref_fun
.block
.add_statement (new
CCodeExpressionStatement (ccall
));
1854 ref_fun
.block
.add_statement (new
CCodeReturnStatement (new
CCodeIdentifier ("_data%d_".printf (block_id
))));
1855 cfile
.add_function (ref_fun
);
1857 var unref_fun
= new
CCodeFunction ("block%d_data_unref".printf (block_id
), "void");
1858 unref_fun
.add_parameter (new
CCodeParameter ("_data%d_".printf (block_id
), struct_name
+ "*"));
1859 unref_fun
.modifiers
= CCodeModifiers
.STATIC
;
1860 cfile
.add_function_declaration (unref_fun
);
1862 push_function (unref_fun
);
1864 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_atomic_int_dec_and_test"));
1865 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_ref_count_")));
1866 ccode
.open_if (ccall
);
1868 if (parent_block
!= null) {
1869 int parent_block_id
= get_block_id (parent_block
);
1871 var unref_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_unref".printf (parent_block_id
)));
1872 unref_call
.add_argument (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_data%d_".printf (parent_block_id
)));
1873 ccode
.add_expression (unref_call
);
1874 ccode
.add_assignment (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_data%d_".printf (parent_block_id
)), new
CCodeConstant ("NULL"));
1876 if (in_constructor
|| (current_method
!= null && current_method
.binding
== MemberBinding
.INSTANCE
) ||
1877 (current_property_accessor
!= null && current_property_accessor
.prop
.binding
== MemberBinding
.INSTANCE
)) {
1878 var ma
= new MemberAccess
.simple ("this");
1879 ma
.symbol_reference
= current_class
;
1880 ccode
.add_expression (get_unref_expression (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "self"), new
ObjectType (current_class
), ma
));
1884 // free in reverse order
1885 for (int i
= local_vars
.size
- 1; i
>= 0; i
--) {
1886 var local
= local_vars
[i
];
1887 if (local
.captured
) {
1888 if (requires_destroy (local
.variable_type
)) {
1889 bool old_coroutine
= false;
1890 if (current_method
!= null) {
1891 old_coroutine
= current_method
.coroutine
;
1892 current_method
.coroutine
= false;
1895 ccode
.add_expression (destroy_local (local
));
1897 if (old_coroutine
) {
1898 current_method
.coroutine
= true;
1904 if (b
.parent_symbol is Method
) {
1905 var m
= (Method
) b
.parent_symbol
;
1907 // parameters are captured with the top-level block of the method
1908 foreach (var param
in m
.get_parameters ()) {
1909 if (param
.captured
) {
1910 var param_type
= param
.variable_type
.copy ();
1911 param_type
.value_owned
= true;
1913 bool is_unowned_delegate
= param
.variable_type is DelegateType
&& !param
.variable_type
.value_owned
;
1915 if (requires_destroy (param_type
) && !is_unowned_delegate
) {
1916 bool old_coroutine
= false;
1918 old_coroutine
= m
.coroutine
;
1919 m
.coroutine
= false;
1922 ccode
.add_expression (destroy_parameter (param
));
1924 if (old_coroutine
) {
1930 } else if (b
.parent_symbol is PropertyAccessor
) {
1931 var acc
= (PropertyAccessor
) b
.parent_symbol
;
1933 if (!acc
.readable
&& acc
.value_parameter
.captured
) {
1934 var param_type
= acc
.value_parameter
.variable_type
.copy ();
1935 param_type
.value_owned
= true;
1937 bool is_unowned_delegate
= acc
.value_parameter
.variable_type is DelegateType
&& !acc
.value_parameter
.variable_type
.value_owned
;
1939 if (requires_destroy (param_type
) && !is_unowned_delegate
) {
1940 ccode
.add_expression (destroy_parameter (acc
.value_parameter
));
1945 var data_free
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_slice_free"));
1946 data_free
.add_argument (new
CCodeIdentifier (struct_name
));
1947 data_free
.add_argument (new
CCodeIdentifier ("_data%d_".printf (block_id
)));
1948 ccode
.add_expression (data_free
);
1954 cfile
.add_function (unref_fun
);
1957 foreach (Statement stmt
in b
.get_statements ()) {
1961 // free in reverse order
1962 for (int i
= local_vars
.size
- 1; i
>= 0; i
--) {
1963 var local
= local_vars
[i
];
1964 local
.active
= false;
1965 if (!local
.unreachable
&& !local
.floating
&& !local
.captured
&& requires_destroy (local
.variable_type
)) {
1966 ccode
.add_expression (destroy_local (local
));
1970 if (b
.parent_symbol is Method
) {
1971 var m
= (Method
) b
.parent_symbol
;
1972 foreach (Parameter param
in m
.get_parameters ()) {
1973 if (!param
.captured
&& !param
.ellipsis
&& requires_destroy (param
.variable_type
) && param
.direction
== ParameterDirection
.IN
) {
1974 ccode
.add_expression (destroy_parameter (param
));
1975 } else if (param
.direction
== ParameterDirection
.OUT
&& !m
.coroutine
) {
1976 return_out_parameter (param
);
1982 int block_id
= get_block_id (b
);
1984 var data_unref
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_unref".printf (block_id
)));
1985 data_unref
.add_argument (get_variable_cexpression ("_data%d_".printf (block_id
)));
1986 ccode
.add_expression (data_unref
);
1987 ccode
.add_assignment (get_variable_cexpression ("_data%d_".printf (block_id
)), new
CCodeConstant ("NULL"));
1990 if (b
.parent_node is Block
|| b
.parent_node is SwitchStatement
) {
1994 emit_context
.pop_symbol ();
1997 public override void visit_declaration_statement (DeclarationStatement stmt
) {
1998 stmt
.declaration
.accept (this
);
2001 public CCodeExpression
get_variable_cexpression (string name
) {
2002 if (is_in_coroutine ()) {
2003 return new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("data"), get_variable_cname (name
));
2005 return new
CCodeIdentifier (get_variable_cname (name
));
2009 public string get_variable_cname (string name
) {
2010 if (name
[0] == '.') {
2011 if (name
== ".result") {
2014 // compiler-internal variable
2015 if (!variable_name_map
.contains (name
)) {
2016 variable_name_map
.set (name
, "_tmp%d_".printf (next_temp_var_id
));
2019 return variable_name_map
.get (name
);
2020 } else if (reserved_identifiers
.contains (name
)) {
2021 return "_%s_".printf (name
);
2027 public CCodeExpression
get_result_cexpression (string cname
= "result") {
2028 if (is_in_coroutine ()) {
2029 return new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("data"), cname
);
2031 return new
CCodeIdentifier (cname
);
2035 bool has_simple_struct_initializer (LocalVariable local
) {
2036 var st
= local
.variable_type
.data_type as Struct
;
2037 var initializer
= local
.initializer as ObjectCreationExpression
;
2038 if (st
!= null && (!st
.is_simple_type () || st
.get_cname () == "va_list") && !local
.variable_type
.nullable
&&
2039 initializer
!= null && initializer
.get_object_initializer ().size
== 0) {
2046 public override void visit_local_variable (LocalVariable local
) {
2047 check_type (local
.variable_type
);
2049 if (local
.initializer
!= null) {
2050 local
.initializer
.emit (this
);
2052 visit_end_full_expression (local
.initializer
);
2055 generate_type_declaration (local
.variable_type
, cfile
);
2057 CCodeExpression rhs
= null;
2058 if (local
.initializer
!= null && get_cvalue (local
.initializer
) != null) {
2059 rhs
= get_cvalue (local
.initializer
);
2062 if (!local
.captured
) {
2063 if (current_method
!= null && current_method
.coroutine
) {
2064 closure_struct
.add_field (local
.variable_type
.get_cname (), get_variable_cname (local
.name
) + local
.variable_type
.get_cdeclarator_suffix ());
2066 var cvar
= new
CCodeVariableDeclarator (get_variable_cname (local
.name
), null, local
.variable_type
.get_cdeclarator_suffix ());
2068 // try to initialize uninitialized variables
2069 // initialization not necessary for variables stored in closure
2070 if (rhs
== null || has_simple_struct_initializer (local
)) {
2071 cvar
.initializer
= default_value_for_type (local
.variable_type
, true);
2075 ccode
.add_declaration (local
.variable_type
.get_cname (), cvar
);
2078 if (local
.variable_type is ArrayType
) {
2079 // create variables to store array dimensions
2080 var array_type
= (ArrayType
) local
.variable_type
;
2082 if (!array_type
.fixed_length
) {
2083 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
2084 var len_var
= new
LocalVariable (int_type
.copy (), get_array_length_cname (get_variable_cname (local
.name
), dim
));
2085 emit_temp_var (len_var
, local
.initializer
== null);
2088 if (array_type
.rank
== 1) {
2089 var size_var
= new
LocalVariable (int_type
.copy (), get_array_size_cname (get_variable_cname (local
.name
)));
2090 emit_temp_var (size_var
, local
.initializer
== null);
2093 } else if (local
.variable_type is DelegateType
) {
2094 var deleg_type
= (DelegateType
) local
.variable_type
;
2095 var d
= deleg_type
.delegate_symbol
;
2097 // create variable to store delegate target
2098 var target_var
= new
LocalVariable (new
PointerType (new
VoidType ()), get_delegate_target_cname (get_variable_cname (local
.name
)));
2099 emit_temp_var (target_var
, local
.initializer
== null);
2100 if (deleg_type
.value_owned
) {
2101 var target_destroy_notify_var
= new
LocalVariable (gdestroynotify_type
, get_delegate_target_destroy_notify_cname (get_variable_cname (local
.name
)));
2102 emit_temp_var (target_destroy_notify_var
, local
.initializer
== null);
2109 if (!has_simple_struct_initializer (local
)) {
2110 store_local (local
, local
.initializer
.target_value
, true);
2114 if (local
.initializer
!= null && local
.initializer
.tree_can_fail
) {
2115 add_simple_check (local
.initializer
);
2118 local
.active
= true;
2121 public override void visit_initializer_list (InitializerList list
) {
2122 if (list
.target_type
.data_type is Struct
) {
2123 /* initializer is used as struct initializer */
2124 var st
= (Struct
) list
.target_type
.data_type
;
2126 if (list
.parent_node is Constant
|| list
.parent_node is Field
|| list
.parent_node is InitializerList
) {
2127 var clist
= new
CCodeInitializerList ();
2129 var field_it
= st
.get_fields ().iterator ();
2130 foreach (Expression expr
in list
.get_initializers ()) {
2132 while (field
== null) {
2134 field
= field_it
.get ();
2135 if (field
.binding
!= MemberBinding
.INSTANCE
) {
2136 // we only initialize instance fields
2141 var cexpr
= get_cvalue (expr
);
2143 string ctype
= field
.get_ctype ();
2144 if (ctype
!= null) {
2145 cexpr
= new
CCodeCastExpression (cexpr
, ctype
);
2148 clist
.append (cexpr
);
2151 set_cvalue (list
, clist
);
2153 // used as expression
2154 var temp_decl
= get_temp_variable (list
.target_type
, false, list
);
2155 emit_temp_var (temp_decl
);
2157 var instance
= get_variable_cexpression (get_variable_cname (temp_decl
.name
));
2159 var field_it
= st
.get_fields ().iterator ();
2160 foreach (Expression expr
in list
.get_initializers ()) {
2162 while (field
== null) {
2164 field
= field_it
.get ();
2165 if (field
.binding
!= MemberBinding
.INSTANCE
) {
2166 // we only initialize instance fields
2171 var cexpr
= get_cvalue (expr
);
2173 string ctype
= field
.get_ctype ();
2174 if (ctype
!= null) {
2175 cexpr
= new
CCodeCastExpression (cexpr
, ctype
);
2178 var lhs
= new
CCodeMemberAccess (instance
, field
.get_cname ());;
2179 ccode
.add_assignment (lhs
, cexpr
);
2182 set_cvalue (list
, instance
);
2185 var clist
= new
CCodeInitializerList ();
2186 foreach (Expression expr
in list
.get_initializers ()) {
2187 clist
.append (get_cvalue (expr
));
2189 set_cvalue (list
, clist
);
2193 public override LocalVariable
create_local (DataType type
) {
2194 var result
= get_temp_variable (type
, type
.value_owned
);
2195 emit_temp_var (result
);
2199 public LocalVariable
get_temp_variable (DataType type
, bool value_owned
= true, CodeNode? node_reference
= null, bool init
= true) {
2200 var var_type
= type
.copy ();
2201 var_type
.value_owned
= value_owned
;
2202 var local
= new
LocalVariable (var_type
, "_tmp%d_".printf (next_temp_var_id
));
2203 local
.no_init
= !init
;
2205 if (node_reference
!= null) {
2206 local
.source_reference
= node_reference
.source_reference
;
2214 bool is_in_generic_type (DataType type
) {
2215 if (current_symbol
!= null && type
.type_parameter
.parent_symbol is TypeSymbol
2216 && (current_method
== null || current_method
.binding
== MemberBinding
.INSTANCE
)) {
2223 public CCodeExpression
get_type_id_expression (DataType type
, bool is_chainup
= false) {
2224 if (type is GenericType
) {
2225 string var_name
= "%s_type".printf (type
.type_parameter
.name
.down ());
2226 if (is_in_generic_type (type
) && !is_chainup
&& !in_creation_method
) {
2227 return new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (get_result_cexpression ("self"), "priv"), var_name
);
2229 return new
CCodeIdentifier (var_name
);
2232 string type_id
= type
.get_type_id ();
2233 if (type_id
== null) {
2234 type_id
= "G_TYPE_INVALID";
2236 generate_type_declaration (type
, cfile
);
2238 return new
CCodeIdentifier (type_id
);
2242 public virtual CCodeExpression?
get_dup_func_expression (DataType type
, SourceReference? source_reference
, bool is_chainup
= false) {
2243 if (type is ErrorType
) {
2244 return new
CCodeIdentifier ("g_error_copy");
2245 } else if (type
.data_type
!= null) {
2246 string dup_function
;
2247 var cl
= type
.data_type as Class
;
2248 if (type
.data_type
.is_reference_counting ()) {
2249 dup_function
= type
.data_type
.get_ref_function ();
2250 if (type
.data_type is Interface
&& dup_function
== null) {
2251 Report
.error (source_reference
, "missing class prerequisite for interface `%s', add GLib.Object to interface declaration if unsure".printf (type
.data_type
.get_full_name ()));
2254 } else if (cl
!= null && cl
.is_immutable
) {
2255 // allow duplicates of immutable instances as for example strings
2256 dup_function
= type
.data_type
.get_dup_function ();
2257 if (dup_function
== null) {
2260 } else if (cl
!= null && cl
.is_gboxed
) {
2261 // allow duplicates of gboxed instances
2262 dup_function
= generate_dup_func_wrapper (type
);
2263 if (dup_function
== null) {
2266 } else if (type is ValueType
) {
2267 dup_function
= type
.data_type
.get_dup_function ();
2268 if (dup_function
== null && type
.nullable
) {
2269 dup_function
= generate_struct_dup_wrapper ((ValueType
) type
);
2270 } else if (dup_function
== null) {
2274 // duplicating non-reference counted objects may cause side-effects (and performance issues)
2275 Report
.error (source_reference
, "duplicating %s instance, use unowned variable or explicitly invoke copy method".printf (type
.data_type
.name
));
2279 return new
CCodeIdentifier (dup_function
);
2280 } else if (type
.type_parameter
!= null) {
2281 string func_name
= "%s_dup_func".printf (type
.type_parameter
.name
.down ());
2282 if (is_in_generic_type (type
) && !is_chainup
&& !in_creation_method
) {
2283 return new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (get_result_cexpression ("self"), "priv"), func_name
);
2285 return new
CCodeIdentifier (func_name
);
2287 } else if (type is PointerType
) {
2288 var pointer_type
= (PointerType
) type
;
2289 return get_dup_func_expression (pointer_type
.base_type
, source_reference
);
2291 return new
CCodeConstant ("NULL");
2295 void make_comparable_cexpression (ref DataType left_type
, ref CCodeExpression cleft
, ref DataType right_type
, ref CCodeExpression cright
) {
2296 var left_type_as_struct
= left_type
.data_type as Struct
;
2297 var right_type_as_struct
= right_type
.data_type as Struct
;
2300 var valuecast
= try_cast_value_to_type (cleft
, left_type
, right_type
);
2301 if (valuecast
!= null) {
2303 left_type
= right_type
;
2304 make_comparable_cexpression (ref left_type
, ref cleft
, ref right_type
, ref cright
);
2308 valuecast
= try_cast_value_to_type (cright
, right_type
, left_type
);
2309 if (valuecast
!= null) {
2311 right_type
= left_type
;
2312 make_comparable_cexpression (ref left_type
, ref cleft
, ref right_type
, ref cright
);
2316 if (left_type
.data_type is Class
&& !((Class
) left_type
.data_type
).is_compact
&&
2317 right_type
.data_type is Class
&& !((Class
) right_type
.data_type
).is_compact
) {
2318 var left_cl
= (Class
) left_type
.data_type
;
2319 var right_cl
= (Class
) right_type
.data_type
;
2321 if (left_cl
!= right_cl
) {
2322 if (left_cl
.is_subtype_of (right_cl
)) {
2323 cleft
= generate_instance_cast (cleft
, right_cl
);
2324 } else if (right_cl
.is_subtype_of (left_cl
)) {
2325 cright
= generate_instance_cast (cright
, left_cl
);
2328 } else if (left_type_as_struct
!= null && right_type_as_struct
!= null) {
2329 if (left_type is StructValueType
) {
2330 // real structs (uses compare/equal function)
2331 if (!left_type
.nullable
) {
2332 cleft
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cleft
);
2334 if (!right_type
.nullable
) {
2335 cright
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cright
);
2338 // integer or floating or boolean type
2339 if (left_type
.nullable
&& right_type
.nullable
) {
2340 // FIXME also compare contents, not just address
2341 } else if (left_type
.nullable
) {
2342 // FIXME check left value is not null
2343 cleft
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, cleft
);
2344 } else if (right_type
.nullable
) {
2345 // FIXME check right value is not null
2346 cright
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, cright
);
2352 private string generate_struct_equal_function (Struct st
) {
2353 string equal_func
= "_%sequal".printf (st
.get_lower_case_cprefix ());
2355 if (!add_wrapper (equal_func
)) {
2356 // wrapper already defined
2360 var function
= new
CCodeFunction (equal_func
, "gboolean");
2361 function
.modifiers
= CCodeModifiers
.STATIC
;
2363 function
.add_parameter (new
CCodeParameter ("s1", "const " + st
.get_cname () + "*"));
2364 function
.add_parameter (new
CCodeParameter ("s2", "const " + st
.get_cname () + "*"));
2366 push_function (function
);
2368 // if (s1 == s2) return TRUE;
2370 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeIdentifier ("s2"));
2371 ccode
.open_if (cexp
);
2372 ccode
.add_return (new
CCodeConstant ("TRUE"));
2375 // if (s1 == NULL || s2 == NULL) return FALSE;
2377 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeConstant ("NULL"));
2378 ccode
.open_if (cexp
);
2379 ccode
.add_return (new
CCodeConstant ("FALSE"));
2382 cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s2"), new
CCodeConstant ("NULL"));
2383 ccode
.open_if (cexp
);
2384 ccode
.add_return (new
CCodeConstant ("FALSE"));
2388 foreach (Field f
in st
.get_fields ()) {
2389 if (f
.binding
!= MemberBinding
.INSTANCE
) {
2390 // we only compare instance fields
2394 CCodeExpression cexp
; // if (cexp) return FALSE;
2395 var s1
= (CCodeExpression
) new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("s1"), f
.name
); // s1->f
2396 var s2
= (CCodeExpression
) new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("s2"), f
.name
); // s2->f
2398 var variable_type
= f
.variable_type
.copy ();
2399 make_comparable_cexpression (ref variable_type
, ref s1
, ref variable_type
, ref s2
);
2401 if (!(f
.variable_type is NullType
) && f
.variable_type
.compatible (string_type
)) {
2402 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strcmp0"));
2403 ccall
.add_argument (s1
);
2404 ccall
.add_argument (s2
);
2406 } else if (f
.variable_type is StructValueType
) {
2407 var equalfunc
= generate_struct_equal_function (f
.variable_type
.data_type as Struct
);
2408 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
2409 ccall
.add_argument (s1
);
2410 ccall
.add_argument (s2
);
2411 cexp
= new
CCodeUnaryExpression (CCodeUnaryOperator
.LOGICAL_NEGATION
, ccall
);
2413 cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, s1
, s2
);
2416 ccode
.open_if (cexp
);
2417 ccode
.add_return (new
CCodeConstant ("FALSE"));
2421 if (st
.get_fields().size
== 0) {
2422 // either opaque structure or simple type
2423 if (st
.is_simple_type ()) {
2424 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s1")), new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s2")));
2425 ccode
.add_return (cexp
);
2427 ccode
.add_return (new
CCodeConstant ("FALSE"));
2430 ccode
.add_return (new
CCodeConstant ("TRUE"));
2435 cfile
.add_function_declaration (function
);
2436 cfile
.add_function (function
);
2441 private string generate_numeric_equal_function (Struct st
) {
2442 string equal_func
= "_%sequal".printf (st
.get_lower_case_cprefix ());
2444 if (!add_wrapper (equal_func
)) {
2445 // wrapper already defined
2449 var function
= new
CCodeFunction (equal_func
, "gboolean");
2450 function
.modifiers
= CCodeModifiers
.STATIC
;
2452 function
.add_parameter (new
CCodeParameter ("s1", "const " + st
.get_cname () + "*"));
2453 function
.add_parameter (new
CCodeParameter ("s2", "const " + st
.get_cname () + "*"));
2455 push_function (function
);
2457 // if (s1 == s2) return TRUE;
2459 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeIdentifier ("s2"));
2460 ccode
.open_if (cexp
);
2461 ccode
.add_return (new
CCodeConstant ("TRUE"));
2464 // if (s1 == NULL || s2 == NULL) return FALSE;
2466 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeConstant ("NULL"));
2467 ccode
.open_if (cexp
);
2468 ccode
.add_return (new
CCodeConstant ("FALSE"));
2471 cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s2"), new
CCodeConstant ("NULL"));
2472 ccode
.open_if (cexp
);
2473 ccode
.add_return (new
CCodeConstant ("FALSE"));
2476 // return (*s1 == *s2);
2478 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s1")), new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s2")));
2479 ccode
.add_return (cexp
);
2484 cfile
.add_function_declaration (function
);
2485 cfile
.add_function (function
);
2490 private string generate_struct_dup_wrapper (ValueType value_type
) {
2491 string dup_func
= "_%sdup".printf (value_type
.type_symbol
.get_lower_case_cprefix ());
2493 if (!add_wrapper (dup_func
)) {
2494 // wrapper already defined
2498 var function
= new
CCodeFunction (dup_func
, value_type
.get_cname ());
2499 function
.modifiers
= CCodeModifiers
.STATIC
;
2501 function
.add_parameter (new
CCodeParameter ("self", value_type
.get_cname ()));
2503 push_function (function
);
2505 if (value_type
.type_symbol
== gvalue_type
) {
2506 var dup_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_boxed_copy"));
2507 dup_call
.add_argument (new
CCodeIdentifier ("G_TYPE_VALUE"));
2508 dup_call
.add_argument (new
CCodeIdentifier ("self"));
2510 ccode
.add_return (dup_call
);
2512 ccode
.add_declaration (value_type
.get_cname (), new
CCodeVariableDeclarator ("dup"));
2514 var creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_new0"));
2515 creation_call
.add_argument (new
CCodeConstant (value_type
.data_type
.get_cname ()));
2516 creation_call
.add_argument (new
CCodeConstant ("1"));
2517 ccode
.add_assignment (new
CCodeIdentifier ("dup"), creation_call
);
2519 var st
= value_type
.data_type as Struct
;
2520 if (st
!= null && st
.is_disposable ()) {
2521 if (!st
.has_copy_function
) {
2522 generate_struct_copy_function (st
);
2525 var copy_call
= new
CCodeFunctionCall (new
CCodeIdentifier (st
.get_copy_function ()));
2526 copy_call
.add_argument (new
CCodeIdentifier ("self"));
2527 copy_call
.add_argument (new
CCodeIdentifier ("dup"));
2528 ccode
.add_expression (copy_call
);
2530 cfile
.add_include ("string.h");
2532 var sizeof_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
2533 sizeof_call
.add_argument (new
CCodeConstant (value_type
.data_type
.get_cname ()));
2535 var copy_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memcpy"));
2536 copy_call
.add_argument (new
CCodeIdentifier ("dup"));
2537 copy_call
.add_argument (new
CCodeIdentifier ("self"));
2538 copy_call
.add_argument (sizeof_call
);
2539 ccode
.add_expression (copy_call
);
2542 ccode
.add_return (new
CCodeIdentifier ("dup"));
2547 cfile
.add_function_declaration (function
);
2548 cfile
.add_function (function
);
2553 protected string generate_dup_func_wrapper (DataType type
) {
2554 string destroy_func
= "_vala_%s_copy".printf (type
.data_type
.get_cname ());
2556 if (!add_wrapper (destroy_func
)) {
2557 // wrapper already defined
2558 return destroy_func
;
2561 var function
= new
CCodeFunction (destroy_func
, type
.get_cname ());
2562 function
.modifiers
= CCodeModifiers
.STATIC
;
2563 function
.add_parameter (new
CCodeParameter ("self", type
.get_cname ()));
2565 push_function (function
);
2567 var cl
= type
.data_type as Class
;
2568 assert (cl
!= null && cl
.is_gboxed
);
2570 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_boxed_copy"));
2571 free_call
.add_argument (new
CCodeIdentifier (cl
.get_type_id ()));
2572 free_call
.add_argument (new
CCodeIdentifier ("self"));
2574 ccode
.add_return (free_call
);
2578 cfile
.add_function_declaration (function
);
2579 cfile
.add_function (function
);
2581 return destroy_func
;
2584 protected string generate_free_func_wrapper (DataType type
) {
2585 string destroy_func
= "_vala_%s_free".printf (type
.data_type
.get_cname ());
2587 if (!add_wrapper (destroy_func
)) {
2588 // wrapper already defined
2589 return destroy_func
;
2592 var function
= new
CCodeFunction (destroy_func
, "void");
2593 function
.modifiers
= CCodeModifiers
.STATIC
;
2594 function
.add_parameter (new
CCodeParameter ("self", type
.get_cname ()));
2596 push_function (function
);
2598 var cl
= type
.data_type as Class
;
2599 if (cl
!= null && cl
.is_gboxed
) {
2600 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_boxed_free"));
2601 free_call
.add_argument (new
CCodeIdentifier (cl
.get_type_id ()));
2602 free_call
.add_argument (new
CCodeIdentifier ("self"));
2604 ccode
.add_expression (free_call
);
2605 } else if (cl
!= null) {
2606 assert (cl
.free_function_address_of
);
2608 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier (type
.data_type
.get_free_function ()));
2609 free_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new
CCodeIdentifier ("self")));
2611 ccode
.add_expression (free_call
);
2613 var st
= type
.data_type as Struct
;
2614 if (st
!= null && st
.is_disposable ()) {
2615 if (!st
.has_destroy_function
) {
2616 generate_struct_destroy_function (st
);
2619 var destroy_call
= new
CCodeFunctionCall (new
CCodeIdentifier (st
.get_destroy_function ()));
2620 destroy_call
.add_argument (new
CCodeIdentifier ("self"));
2621 ccode
.add_expression (destroy_call
);
2624 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_free"));
2625 free_call
.add_argument (new
CCodeIdentifier ("self"));
2627 ccode
.add_expression (free_call
);
2632 cfile
.add_function_declaration (function
);
2633 cfile
.add_function (function
);
2635 return destroy_func
;
2638 public CCodeExpression?
get_destroy0_func_expression (DataType type
, bool is_chainup
= false) {
2639 var element_destroy_func_expression
= get_destroy_func_expression (type
, is_chainup
);
2641 if (element_destroy_func_expression is CCodeIdentifier
) {
2642 var freeid
= (CCodeIdentifier
) element_destroy_func_expression
;
2643 string free0_func
= "_%s0_".printf (freeid
.name
);
2645 if (add_wrapper (free0_func
)) {
2646 var function
= new
CCodeFunction (free0_func
, "void");
2647 function
.modifiers
= CCodeModifiers
.STATIC
;
2649 function
.add_parameter (new
CCodeParameter ("var", "gpointer"));
2651 push_function (function
);
2653 ccode
.add_expression (get_unref_expression (new
CCodeIdentifier ("var"), type
, null, true));
2657 cfile
.add_function_declaration (function
);
2658 cfile
.add_function (function
);
2661 element_destroy_func_expression
= new
CCodeIdentifier (free0_func
);
2664 return element_destroy_func_expression
;
2667 public CCodeExpression?
get_destroy_func_expression (DataType type
, bool is_chainup
= false) {
2668 if (context
.profile
== Profile
.GOBJECT
&& (type
.data_type
== glist_type
|| type
.data_type
== gslist_type
|| type
.data_type
== gnode_type
)) {
2669 // create wrapper function to free list elements if necessary
2671 bool elements_require_free
= false;
2672 CCodeExpression element_destroy_func_expression
= null;
2674 foreach (DataType type_arg
in type
.get_type_arguments ()) {
2675 elements_require_free
= requires_destroy (type_arg
);
2676 if (elements_require_free
) {
2677 element_destroy_func_expression
= get_destroy0_func_expression (type_arg
);
2681 if (elements_require_free
&& element_destroy_func_expression is CCodeIdentifier
) {
2682 return new
CCodeIdentifier (generate_collection_free_wrapper (type
, (CCodeIdentifier
) element_destroy_func_expression
));
2684 return new
CCodeIdentifier (type
.data_type
.get_free_function ());
2686 } else if (type is ErrorType
) {
2687 return new
CCodeIdentifier ("g_error_free");
2688 } else if (type
.data_type
!= null) {
2689 string unref_function
;
2690 if (type is ReferenceType
) {
2691 if (type
.data_type
.is_reference_counting ()) {
2692 unref_function
= type
.data_type
.get_unref_function ();
2693 if (type
.data_type is Interface
&& unref_function
== null) {
2694 Report
.error (type
.source_reference
, "missing class prerequisite for interface `%s', add GLib.Object to interface declaration if unsure".printf (type
.data_type
.get_full_name ()));
2698 var cl
= type
.data_type as Class
;
2699 if (cl
!= null && (cl
.free_function_address_of
|| cl
.is_gboxed
)) {
2700 unref_function
= generate_free_func_wrapper (type
);
2702 unref_function
= type
.data_type
.get_free_function ();
2706 if (type
.nullable
) {
2707 unref_function
= type
.data_type
.get_free_function ();
2708 if (unref_function
== null) {
2709 if (type
.data_type is Struct
&& ((Struct
) type
.data_type
).is_disposable ()) {
2710 unref_function
= generate_free_func_wrapper (type
);
2712 unref_function
= "g_free";
2716 var st
= (Struct
) type
.data_type
;
2717 if (!st
.has_destroy_function
) {
2718 generate_struct_destroy_function (st
);
2720 unref_function
= st
.get_destroy_function ();
2723 if (unref_function
== null) {
2724 return new
CCodeConstant ("NULL");
2726 return new
CCodeIdentifier (unref_function
);
2727 } else if (type
.type_parameter
!= null && current_type_symbol is Class
) {
2728 string func_name
= "%s_destroy_func".printf (type
.type_parameter
.name
.down ());
2729 if (is_in_generic_type (type
) && !is_chainup
&& !in_creation_method
) {
2730 return new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (get_result_cexpression ("self"), "priv"), func_name
);
2732 return new
CCodeIdentifier (func_name
);
2734 } else if (type is ArrayType
) {
2735 if (context
.profile
== Profile
.POSIX
) {
2736 return new
CCodeIdentifier ("free");
2738 return new
CCodeIdentifier ("g_free");
2740 } else if (type is PointerType
) {
2741 if (context
.profile
== Profile
.POSIX
) {
2742 return new
CCodeIdentifier ("free");
2744 return new
CCodeIdentifier ("g_free");
2747 return new
CCodeConstant ("NULL");
2751 private string generate_collection_free_wrapper (DataType collection_type
, CCodeIdentifier element_destroy_func_expression
) {
2752 string destroy_func
= "_%s_%s".printf (collection_type
.data_type
.get_free_function (), element_destroy_func_expression
.name
);
2754 if (!add_wrapper (destroy_func
)) {
2755 // wrapper already defined
2756 return destroy_func
;
2759 var function
= new
CCodeFunction (destroy_func
, "void");
2760 function
.modifiers
= CCodeModifiers
.STATIC
;
2762 function
.add_parameter (new
CCodeParameter ("self", collection_type
.get_cname ()));
2764 push_function (function
);
2766 CCodeFunctionCall element_free_call
;
2767 if (collection_type
.data_type
== gnode_type
) {
2768 /* A wrapper which converts GNodeTraverseFunc into GDestroyNotify */
2769 string destroy_node_func
= "%s_node".printf (destroy_func
);
2770 var wrapper
= new
CCodeFunction (destroy_node_func
, "gboolean");
2771 wrapper
.modifiers
= CCodeModifiers
.STATIC
;
2772 wrapper
.add_parameter (new
CCodeParameter ("node", collection_type
.get_cname ()));
2773 wrapper
.add_parameter (new
CCodeParameter ("unused", "gpointer"));
2774 var wrapper_block
= new
CCodeBlock ();
2775 var free_call
= new
CCodeFunctionCall (element_destroy_func_expression
);
2776 free_call
.add_argument (new CCodeMemberAccess
.pointer(new
CCodeIdentifier("node"), "data"));
2777 wrapper_block
.add_statement (new
CCodeExpressionStatement (free_call
));
2778 wrapper_block
.add_statement (new
CCodeReturnStatement (new
CCodeConstant ("FALSE")));
2779 cfile
.add_function_declaration (function
);
2780 wrapper
.block
= wrapper_block
;
2781 cfile
.add_function (wrapper
);
2783 /* Now the code to call g_traverse with the above */
2784 element_free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_node_traverse"));
2785 element_free_call
.add_argument (new
CCodeIdentifier("self"));
2786 element_free_call
.add_argument (new
CCodeConstant ("G_POST_ORDER"));
2787 element_free_call
.add_argument (new
CCodeConstant ("G_TRAVERSE_ALL"));
2788 element_free_call
.add_argument (new
CCodeConstant ("-1"));
2789 element_free_call
.add_argument (new
CCodeIdentifier (destroy_node_func
));
2790 element_free_call
.add_argument (new
CCodeConstant ("NULL"));
2792 if (collection_type
.data_type
== glist_type
) {
2793 element_free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_list_foreach"));
2795 element_free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_slist_foreach"));
2798 element_free_call
.add_argument (new
CCodeIdentifier ("self"));
2799 element_free_call
.add_argument (new
CCodeCastExpression (element_destroy_func_expression
, "GFunc"));
2800 element_free_call
.add_argument (new
CCodeConstant ("NULL"));
2803 ccode
.add_expression (element_free_call
);
2805 var cfreecall
= new
CCodeFunctionCall (new
CCodeIdentifier (collection_type
.data_type
.get_free_function ()));
2806 cfreecall
.add_argument (new
CCodeIdentifier ("self"));
2807 ccode
.add_expression (cfreecall
);
2811 cfile
.add_function_declaration (function
);
2812 cfile
.add_function (function
);
2814 return destroy_func
;
2817 public virtual string?
append_struct_array_free (Struct st
) {
2821 // logic in this method is temporarily duplicated in destroy_value
2822 // apply changes to both methods
2823 public virtual CCodeExpression
destroy_variable (Variable variable
, TargetValue target_lvalue
) {
2824 var type
= variable
.variable_type
;
2825 var cvar
= get_cvalue_ (target_lvalue
);
2827 if (type is DelegateType
) {
2828 var delegate_target
= get_delegate_target_cvalue (target_lvalue
);
2829 var delegate_target_destroy_notify
= get_delegate_target_destroy_notify_cvalue (target_lvalue
);
2831 var ccall
= new
CCodeFunctionCall (delegate_target_destroy_notify
);
2832 ccall
.add_argument (delegate_target
);
2834 var destroy_call
= new
CCodeCommaExpression ();
2835 destroy_call
.append_expression (ccall
);
2836 destroy_call
.append_expression (new
CCodeConstant ("NULL"));
2838 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, delegate_target_destroy_notify
, new
CCodeConstant ("NULL"));
2840 var ccomma
= new
CCodeCommaExpression ();
2841 ccomma
.append_expression (new
CCodeConditionalExpression (cisnull
, new
CCodeConstant ("NULL"), destroy_call
));
2842 ccomma
.append_expression (new
CCodeAssignment (cvar
, new
CCodeConstant ("NULL")));
2843 ccomma
.append_expression (new
CCodeAssignment (delegate_target
, new
CCodeConstant ("NULL")));
2844 ccomma
.append_expression (new
CCodeAssignment (delegate_target_destroy_notify
, new
CCodeConstant ("NULL")));
2849 var ccall
= new
CCodeFunctionCall (get_destroy_func_expression (type
));
2851 if (type is ValueType
&& !type
.nullable
) {
2852 // normal value type, no null check
2853 var st
= type
.data_type as Struct
;
2854 if (st
!= null && st
.is_simple_type ()) {
2856 ccall
.add_argument (cvar
);
2858 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cvar
));
2861 if (gvalue_type
!= null && type
.data_type
== gvalue_type
) {
2862 // g_value_unset must not be called for already unset values
2863 var cisvalid
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_IS_VALUE"));
2864 cisvalid
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cvar
));
2866 var ccomma
= new
CCodeCommaExpression ();
2867 ccomma
.append_expression (ccall
);
2868 ccomma
.append_expression (new
CCodeConstant ("NULL"));
2870 return new
CCodeConditionalExpression (cisvalid
, ccomma
, new
CCodeConstant ("NULL"));
2876 if (ccall
.call is CCodeIdentifier
&& !(type is ArrayType
)) {
2877 // generate and use NULL-aware free macro to simplify code
2879 var freeid
= (CCodeIdentifier
) ccall
.call
;
2880 string free0_func
= "_%s0".printf (freeid
.name
);
2882 if (add_wrapper (free0_func
)) {
2883 var macro
= destroy_value (new
GLibValue (type
, new
CCodeIdentifier ("var")), true);
2884 cfile
.add_type_declaration (new CCodeMacroReplacement
.with_expression ("%s(var)".printf (free0_func
), macro
));
2887 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (free0_func
));
2888 ccall
.add_argument (cvar
);
2892 /* (foo == NULL ? NULL : foo = (unref (foo), NULL)) */
2894 /* can be simplified to
2895 * foo = (unref (foo), NULL)
2896 * if foo is of static type non-null
2899 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, cvar
, new
CCodeConstant ("NULL"));
2900 if (type
.type_parameter
!= null) {
2901 if (!(current_type_symbol is Class
) || current_class
.is_compact
) {
2902 return new
CCodeConstant ("NULL");
2905 // unref functions are optional for type parameters
2906 var cunrefisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, get_destroy_func_expression (type
), new
CCodeConstant ("NULL"));
2907 cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.OR
, cisnull
, cunrefisnull
);
2910 ccall
.add_argument (cvar
);
2912 /* set freed references to NULL to prevent further use */
2913 var ccomma
= new
CCodeCommaExpression ();
2915 if (context
.profile
== Profile
.GOBJECT
) {
2916 if (type
.data_type
!= null && !type
.data_type
.is_reference_counting () &&
2917 (type
.data_type
== gstringbuilder_type
2918 || type
.data_type
== garray_type
2919 || type
.data_type
== gbytearray_type
2920 || type
.data_type
== gptrarray_type
)) {
2921 ccall
.add_argument (new
CCodeConstant ("TRUE"));
2922 } else if (type
.data_type
== gthreadpool_type
) {
2923 ccall
.add_argument (new
CCodeConstant ("FALSE"));
2924 ccall
.add_argument (new
CCodeConstant ("TRUE"));
2925 } else if (type is ArrayType
) {
2926 var array_type
= (ArrayType
) type
;
2927 if (requires_destroy (array_type
.element_type
) && !variable
.no_array_length
) {
2928 CCodeExpression csizeexpr
= null;
2929 if (variable
.array_null_terminated
) {
2930 var len_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("_vala_array_length"));
2931 len_call
.add_argument (cvar
);
2932 csizeexpr
= len_call
;
2933 } else if (variable
.has_array_length_cexpr
) {
2934 csizeexpr
= new
CCodeConstant (variable
.get_array_length_cexpr ());
2937 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
2939 csizeexpr
= get_array_length_cvalue (target_lvalue
, dim
);
2942 csizeexpr
= new
CCodeBinaryExpression (CCodeBinaryOperator
.MUL
, csizeexpr
, get_array_length_cvalue (target_lvalue
, dim
));
2947 var st
= array_type
.element_type
.data_type as Struct
;
2948 if (st
!= null && !array_type
.element_type
.nullable
) {
2949 ccall
.call
= new
CCodeIdentifier (append_struct_array_free (st
));
2950 ccall
.add_argument (csizeexpr
);
2952 requires_array_free
= true;
2953 ccall
.call
= new
CCodeIdentifier ("_vala_array_free");
2954 ccall
.add_argument (csizeexpr
);
2955 ccall
.add_argument (new
CCodeCastExpression (get_destroy_func_expression (array_type
.element_type
), "GDestroyNotify"));
2961 ccomma
.append_expression (ccall
);
2962 ccomma
.append_expression (new
CCodeConstant ("NULL"));
2964 var cassign
= new
CCodeAssignment (cvar
, ccomma
);
2966 // g_free (NULL) is allowed
2967 bool uses_gfree
= (type
.data_type
!= null && !type
.data_type
.is_reference_counting () && type
.data_type
.get_free_function () == "g_free");
2968 uses_gfree
= uses_gfree
|| type is ArrayType
;
2973 return new
CCodeConditionalExpression (cisnull
, new
CCodeConstant ("NULL"), cassign
);
2976 public CCodeExpression
destroy_local (LocalVariable local
) {
2977 return destroy_variable (local
, get_local_cvalue (local
));
2980 public CCodeExpression
destroy_parameter (Parameter param
) {
2981 return destroy_variable (param
, get_parameter_cvalue (param
));
2984 public CCodeExpression
destroy_field (Field field
, Expression? instance
) {
2985 return destroy_variable (field
, get_field_cvalue (field
, instance
));
2988 public CCodeExpression
get_unref_expression (CCodeExpression cvar
, DataType type
, Expression? expr
, bool is_macro_definition
= false) {
2990 if (expr
.symbol_reference is LocalVariable
) {
2991 return destroy_local ((LocalVariable
) expr
.symbol_reference
);
2992 } else if (expr
.symbol_reference is Parameter
) {
2993 return destroy_parameter ((Parameter
) expr
.symbol_reference
);
2996 var value
= new
GLibValue (type
, cvar
);
2997 if (expr
!= null && expr
.target_value
!= null) {
2998 value
.array_length_cvalues
= ((GLibValue
) expr
.target_value
).array_length_cvalues
;
2999 value
.delegate_target_cvalue
= get_delegate_target_cvalue (expr
.target_value
);
3000 value
.delegate_target_destroy_notify_cvalue
= get_delegate_target_destroy_notify_cvalue (expr
.target_value
);
3002 return destroy_value (value
, is_macro_definition
);
3005 // logic in this method is temporarily duplicated in destroy_variable
3006 // apply changes to both methods
3007 public virtual CCodeExpression
destroy_value (TargetValue value
, bool is_macro_definition
= false) {
3008 var type
= value
.value_type
;
3009 var cvar
= get_cvalue_ (value
);
3011 if (type is DelegateType
) {
3012 var delegate_target
= get_delegate_target_cvalue (value
);
3013 var delegate_target_destroy_notify
= get_delegate_target_destroy_notify_cvalue (value
);
3015 var ccall
= new
CCodeFunctionCall (delegate_target_destroy_notify
);
3016 ccall
.add_argument (delegate_target
);
3018 var destroy_call
= new
CCodeCommaExpression ();
3019 destroy_call
.append_expression (ccall
);
3020 destroy_call
.append_expression (new
CCodeConstant ("NULL"));
3022 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, delegate_target_destroy_notify
, new
CCodeConstant ("NULL"));
3024 var ccomma
= new
CCodeCommaExpression ();
3025 ccomma
.append_expression (new
CCodeConditionalExpression (cisnull
, new
CCodeConstant ("NULL"), destroy_call
));
3026 ccomma
.append_expression (new
CCodeAssignment (cvar
, new
CCodeConstant ("NULL")));
3027 ccomma
.append_expression (new
CCodeAssignment (delegate_target
, new
CCodeConstant ("NULL")));
3028 ccomma
.append_expression (new
CCodeAssignment (delegate_target_destroy_notify
, new
CCodeConstant ("NULL")));
3033 var ccall
= new
CCodeFunctionCall (get_destroy_func_expression (type
));
3035 if (type is ValueType
&& !type
.nullable
) {
3036 // normal value type, no null check
3037 var st
= type
.data_type as Struct
;
3038 if (st
!= null && st
.is_simple_type ()) {
3040 ccall
.add_argument (cvar
);
3042 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cvar
));
3045 if (gvalue_type
!= null && type
.data_type
== gvalue_type
) {
3046 // g_value_unset must not be called for already unset values
3047 var cisvalid
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_IS_VALUE"));
3048 cisvalid
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cvar
));
3050 var ccomma
= new
CCodeCommaExpression ();
3051 ccomma
.append_expression (ccall
);
3052 ccomma
.append_expression (new
CCodeConstant ("NULL"));
3054 return new
CCodeConditionalExpression (cisvalid
, ccomma
, new
CCodeConstant ("NULL"));
3060 if (ccall
.call is CCodeIdentifier
&& !(type is ArrayType
) && !is_macro_definition
) {
3061 // generate and use NULL-aware free macro to simplify code
3063 var freeid
= (CCodeIdentifier
) ccall
.call
;
3064 string free0_func
= "_%s0".printf (freeid
.name
);
3066 if (add_wrapper (free0_func
)) {
3067 var macro
= destroy_value (new
GLibValue (type
, new
CCodeIdentifier ("var")), true);
3068 cfile
.add_type_declaration (new CCodeMacroReplacement
.with_expression ("%s(var)".printf (free0_func
), macro
));
3071 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (free0_func
));
3072 ccall
.add_argument (cvar
);
3076 /* (foo == NULL ? NULL : foo = (unref (foo), NULL)) */
3078 /* can be simplified to
3079 * foo = (unref (foo), NULL)
3080 * if foo is of static type non-null
3083 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, cvar
, new
CCodeConstant ("NULL"));
3084 if (type
.type_parameter
!= null) {
3085 if (!(current_type_symbol is Class
) || current_class
.is_compact
) {
3086 return new
CCodeConstant ("NULL");
3089 // unref functions are optional for type parameters
3090 var cunrefisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, get_destroy_func_expression (type
), new
CCodeConstant ("NULL"));
3091 cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.OR
, cisnull
, cunrefisnull
);
3094 ccall
.add_argument (cvar
);
3096 /* set freed references to NULL to prevent further use */
3097 var ccomma
= new
CCodeCommaExpression ();
3099 if (context
.profile
== Profile
.GOBJECT
) {
3100 if (type
.data_type
!= null && !type
.data_type
.is_reference_counting () &&
3101 (type
.data_type
== gstringbuilder_type
3102 || type
.data_type
== garray_type
3103 || type
.data_type
== gbytearray_type
3104 || type
.data_type
== gptrarray_type
)) {
3105 ccall
.add_argument (new
CCodeConstant ("TRUE"));
3106 } else if (type
.data_type
== gthreadpool_type
) {
3107 ccall
.add_argument (new
CCodeConstant ("FALSE"));
3108 ccall
.add_argument (new
CCodeConstant ("TRUE"));
3109 } else if (type is ArrayType
) {
3110 var array_type
= (ArrayType
) type
;
3111 if (requires_destroy (array_type
.element_type
)) {
3112 CCodeExpression csizeexpr
= null;
3114 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
3116 csizeexpr
= get_array_length_cvalue (value
, dim
);
3119 csizeexpr
= new
CCodeBinaryExpression (CCodeBinaryOperator
.MUL
, csizeexpr
, get_array_length_cvalue (value
, dim
));
3123 var st
= array_type
.element_type
.data_type as Struct
;
3124 if (st
!= null && !array_type
.element_type
.nullable
) {
3125 ccall
.call
= new
CCodeIdentifier (append_struct_array_free (st
));
3126 ccall
.add_argument (csizeexpr
);
3128 requires_array_free
= true;
3129 ccall
.call
= new
CCodeIdentifier ("_vala_array_free");
3130 ccall
.add_argument (csizeexpr
);
3131 ccall
.add_argument (new
CCodeCastExpression (get_destroy_func_expression (array_type
.element_type
), "GDestroyNotify"));
3137 ccomma
.append_expression (ccall
);
3138 ccomma
.append_expression (new
CCodeConstant ("NULL"));
3140 var cassign
= new
CCodeAssignment (cvar
, ccomma
);
3142 // g_free (NULL) is allowed
3143 bool uses_gfree
= (type
.data_type
!= null && !type
.data_type
.is_reference_counting () && type
.data_type
.get_free_function () == "g_free");
3144 uses_gfree
= uses_gfree
|| type is ArrayType
;
3149 return new
CCodeConditionalExpression (cisnull
, new
CCodeConstant ("NULL"), cassign
);
3152 public override void visit_end_full_expression (Expression expr
) {
3153 /* expr is a full expression, i.e. an initializer, the
3154 * expression in an expression statement, the controlling
3155 * expression in if, while, for, or foreach statements
3157 * we unref temporary variables at the end of a full
3160 if (temp_ref_vars
.size
== 0) {
3161 /* nothing to do without temporary variables */
3165 LocalVariable full_expr_var
= null;
3167 var local_decl
= expr
.parent_node as LocalVariable
;
3168 if (!(local_decl
!= null && has_simple_struct_initializer (local_decl
))) {
3169 var expr_type
= expr
.value_type
;
3170 if (expr
.target_type
!= null) {
3171 expr_type
= expr
.target_type
;
3174 full_expr_var
= get_temp_variable (expr_type
, true, expr
, false);
3175 emit_temp_var (full_expr_var
);
3177 ccode
.add_assignment (get_variable_cexpression (full_expr_var
.name
), get_cvalue (expr
));
3180 foreach (LocalVariable local
in temp_ref_vars
) {
3181 ccode
.add_expression (destroy_local (local
));
3184 if (full_expr_var
!= null) {
3185 set_cvalue (expr
, get_variable_cexpression (full_expr_var
.name
));
3188 temp_ref_vars
.clear ();
3191 public void emit_temp_var (LocalVariable local
, bool always_init
= false) {
3192 var vardecl
= new
CCodeVariableDeclarator (local
.name
, null, local
.variable_type
.get_cdeclarator_suffix ());
3194 var st
= local
.variable_type
.data_type as Struct
;
3195 var array_type
= local
.variable_type as ArrayType
;
3197 if (local
.name
.has_prefix ("*")) {
3198 // do not dereference unintialized variable
3199 // initialization is not needed for these special
3200 // pointer temp variables
3201 // used to avoid side-effects in assignments
3202 } else if (local
.no_init
) {
3203 // no initialization necessary for this temp var
3204 } else if (!local
.variable_type
.nullable
&&
3205 (st
!= null && !st
.is_simple_type ()) ||
3206 (array_type
!= null && array_type
.fixed_length
)) {
3207 // 0-initialize struct with struct initializer { 0 }
3208 // necessary as they will be passed by reference
3209 var clist
= new
CCodeInitializerList ();
3210 clist
.append (new
CCodeConstant ("0"));
3212 vardecl
.initializer
= clist
;
3213 vardecl
.init0
= true;
3214 } else if (local
.variable_type
.is_reference_type_or_type_parameter () ||
3215 local
.variable_type
.nullable
||
3216 local
.variable_type is DelegateType
) {
3217 vardecl
.initializer
= new
CCodeConstant ("NULL");
3218 vardecl
.init0
= true;
3219 } else if (always_init
) {
3220 vardecl
.initializer
= default_value_for_type (local
.variable_type
, true);
3221 vardecl
.init0
= true;
3224 if (is_in_coroutine ()) {
3225 closure_struct
.add_field (local
.variable_type
.get_cname (), local
.name
);
3227 // even though closure struct is zerod, we need to initialize temporary variables
3228 // as they might be used multiple times when declared in a loop
3230 if (vardecl
.initializer is CCodeInitializerList
) {
3231 // C does not support initializer lists in assignments, use memset instead
3232 cfile
.add_include ("string.h");
3233 var memset_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memset"));
3234 memset_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (local
.name
)));
3235 memset_call
.add_argument (new
CCodeConstant ("0"));
3236 memset_call
.add_argument (new
CCodeIdentifier ("sizeof (%s)".printf (local
.variable_type
.get_cname ())));
3237 ccode
.add_expression (memset_call
);
3238 } else if (vardecl
.initializer
!= null) {
3239 ccode
.add_assignment (get_variable_cexpression (local
.name
), vardecl
.initializer
);
3242 ccode
.add_declaration (local
.variable_type
.get_cname (), vardecl
);
3246 public override void visit_expression_statement (ExpressionStatement stmt
) {
3247 if (stmt
.expression
.error
) {
3252 /* free temporary objects and handle errors */
3254 foreach (LocalVariable local
in temp_ref_vars
) {
3255 ccode
.add_expression (destroy_local (local
));
3258 if (stmt
.tree_can_fail
&& stmt
.expression
.tree_can_fail
) {
3259 // simple case, no node breakdown necessary
3260 add_simple_check (stmt
.expression
);
3263 temp_ref_vars
.clear ();
3266 public virtual void append_local_free (Symbol sym
, bool stop_at_loop
= false, CodeNode? stop_at
= null) {
3267 var b
= (Block
) sym
;
3269 var local_vars
= b
.get_local_variables ();
3270 // free in reverse order
3271 for (int i
= local_vars
.size
- 1; i
>= 0; i
--) {
3272 var local
= local_vars
[i
];
3273 if (!local
.unreachable
&& local
.active
&& !local
.floating
&& !local
.captured
&& requires_destroy (local
.variable_type
)) {
3274 ccode
.add_expression (destroy_local (local
));
3279 int block_id
= get_block_id (b
);
3281 var data_unref
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_unref".printf (block_id
)));
3282 data_unref
.add_argument (get_variable_cexpression ("_data%d_".printf (block_id
)));
3283 ccode
.add_expression (data_unref
);
3284 ccode
.add_assignment (get_variable_cexpression ("_data%d_".printf (block_id
)), new
CCodeConstant ("NULL"));
3288 if (b
.parent_node is Loop
||
3289 b
.parent_node is ForeachStatement
||
3290 b
.parent_node is SwitchStatement
) {
3295 if (b
.parent_node
== stop_at
) {
3299 if (sym
.parent_symbol is Block
) {
3300 append_local_free (sym
.parent_symbol
, stop_at_loop
, stop_at
);
3301 } else if (sym
.parent_symbol is Method
) {
3302 append_param_free ((Method
) sym
.parent_symbol
);
3306 private void append_param_free (Method m
) {
3307 foreach (Parameter param
in m
.get_parameters ()) {
3308 if (!param
.ellipsis
&& requires_destroy (param
.variable_type
) && param
.direction
== ParameterDirection
.IN
) {
3309 ccode
.add_expression (destroy_parameter (param
));
3314 public bool variable_accessible_in_finally (LocalVariable local
) {
3315 if (current_try
== null) {
3319 var sym
= current_symbol
;
3321 while (!(sym is Method
|| sym is PropertyAccessor
) && sym
.scope
.lookup (local
.name
) == null) {
3322 if ((sym
.parent_node is TryStatement
&& ((TryStatement
) sym
.parent_node
).finally_body
!= null) ||
3323 (sym
.parent_node is CatchClause
&& ((TryStatement
) sym
.parent_node
.parent_node
).finally_body
!= null)) {
3328 sym
= sym
.parent_symbol
;
3334 void return_out_parameter (Parameter param
) {
3335 var delegate_type
= param
.variable_type as DelegateType
;
3337 ccode
.open_if (get_variable_cexpression (param
.name
));
3338 ccode
.add_assignment (new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_variable_cexpression (param
.name
)), get_variable_cexpression ("_" + param
.name
));
3340 if (delegate_type
!= null && delegate_type
.delegate_symbol
.has_target
) {
3341 ccode
.add_assignment (new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_variable_cexpression (get_delegate_target_cname (param
.name
))), new
CCodeIdentifier (get_delegate_target_cname (get_variable_cname ("_" + param
.name
))));
3342 if (delegate_type
.value_owned
) {
3343 ccode
.add_assignment (new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_variable_cexpression (get_delegate_target_destroy_notify_cname (param
.name
))), new
CCodeIdentifier (get_delegate_target_destroy_notify_cname (get_variable_cname ("_" + param
.name
))));
3347 if (param
.variable_type
.is_disposable ()){
3349 ccode
.add_expression (destroy_parameter (param
));
3353 var array_type
= param
.variable_type as ArrayType
;
3354 if (array_type
!= null && !array_type
.fixed_length
&& !param
.no_array_length
) {
3355 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
3356 ccode
.open_if (get_variable_cexpression (get_parameter_array_length_cname (param
, dim
)));
3357 ccode
.add_assignment (new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_variable_cexpression (get_parameter_array_length_cname (param
, dim
))), new
CCodeIdentifier (get_array_length_cname (get_variable_cname ("_" + param
.name
), dim
)));
3363 public override void visit_return_statement (ReturnStatement stmt
) {
3364 Symbol return_expression_symbol
= null;
3366 if (stmt
.return_expression
!= null) {
3367 // avoid unnecessary ref/unref pair
3368 var local
= stmt
.return_expression
.symbol_reference as LocalVariable
;
3369 if (current_return_type
.value_owned
3370 && local
!= null && local
.variable_type
.value_owned
3372 && !variable_accessible_in_finally (local
)) {
3373 /* return expression is local variable taking ownership and
3374 * current method is transferring ownership */
3376 return_expression_symbol
= local
;
3380 // return array length if appropriate
3381 if (((current_method
!= null && !current_method
.no_array_length
) || current_property_accessor
!= null) && current_return_type is ArrayType
) {
3382 var return_expr_decl
= get_temp_variable (stmt
.return_expression
.value_type
, true, stmt
, false);
3384 ccode
.add_assignment (get_variable_cexpression (return_expr_decl
.name
), get_cvalue (stmt
.return_expression
));
3386 var array_type
= (ArrayType
) current_return_type
;
3388 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
3389 var len_l
= get_result_cexpression (get_array_length_cname ("result", dim
));
3390 if (!is_in_coroutine ()) {
3391 len_l
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, len_l
);
3393 var len_r
= get_array_length_cexpression (stmt
.return_expression
, dim
);
3394 ccode
.add_assignment (len_l
, len_r
);
3397 set_cvalue (stmt
.return_expression
, get_variable_cexpression (return_expr_decl
.name
));
3399 emit_temp_var (return_expr_decl
);
3400 } else if ((current_method
!= null || current_property_accessor
!= null) && current_return_type is DelegateType
) {
3401 var delegate_type
= (DelegateType
) current_return_type
;
3402 if (delegate_type
.delegate_symbol
.has_target
) {
3403 var return_expr_decl
= get_temp_variable (stmt
.return_expression
.value_type
, true, stmt
, false);
3405 ccode
.add_assignment (get_variable_cexpression (return_expr_decl
.name
), get_cvalue (stmt
.return_expression
));
3407 var target_l
= get_result_cexpression (get_delegate_target_cname ("result"));
3408 if (!is_in_coroutine ()) {
3409 target_l
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, target_l
);
3411 CCodeExpression target_r_destroy_notify
;
3412 var target_r
= get_delegate_target_cexpression (stmt
.return_expression
, out target_r_destroy_notify
);
3413 ccode
.add_assignment (target_l
, target_r
);
3414 if (delegate_type
.value_owned
) {
3415 var target_l_destroy_notify
= get_result_cexpression (get_delegate_target_destroy_notify_cname ("result"));
3416 if (!is_in_coroutine ()) {
3417 target_l_destroy_notify
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, target_l_destroy_notify
);
3419 ccode
.add_assignment (target_l_destroy_notify
, target_r_destroy_notify
);
3422 set_cvalue (stmt
.return_expression
, get_variable_cexpression (return_expr_decl
.name
));
3424 emit_temp_var (return_expr_decl
);
3428 if (stmt
.return_expression
!= null) {
3429 // assign method result to `result'
3430 CCodeExpression result_lhs
= get_result_cexpression ();
3431 if (current_return_type
.is_real_non_null_struct_type () && !is_in_coroutine ()) {
3432 result_lhs
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, result_lhs
);
3434 ccode
.add_assignment (result_lhs
, get_cvalue (stmt
.return_expression
));
3437 // free local variables
3438 append_local_free (current_symbol
);
3440 if (current_method
!= null) {
3441 // check postconditions
3442 foreach (Expression postcondition
in current_method
.get_postconditions ()) {
3443 create_postcondition_statement (postcondition
);
3447 if (current_method
!= null && !current_method
.coroutine
) {
3448 // assign values to output parameters if they are not NULL
3449 // otherwise, free the value if necessary
3450 foreach (var param
in current_method
.get_parameters ()) {
3451 if (param
.direction
!= ParameterDirection
.OUT
) {
3455 return_out_parameter (param
);
3459 if (is_in_constructor ()) {
3460 ccode
.add_return (new
CCodeIdentifier ("obj"));
3461 } else if (is_in_destructor ()) {
3462 // do not call return as member cleanup and chain up to base finalizer
3463 // stil need to be executed
3464 ccode
.add_goto ("_return");
3465 } else if (current_method is CreationMethod
) {
3466 ccode
.add_return (new
CCodeIdentifier ("self"));
3467 } else if (is_in_coroutine ()) {
3468 } else if (current_return_type is VoidType
|| current_return_type
.is_real_non_null_struct_type ()) {
3469 // structs are returned via out parameter
3470 ccode
.add_return ();
3472 ccode
.add_return (new
CCodeIdentifier ("result"));
3475 if (return_expression_symbol
!= null) {
3476 return_expression_symbol
.active
= true;
3479 // required for destructors
3480 current_method_return
= true;
3483 public string get_symbol_lock_name (string symname
) {
3484 return "__lock_%s".printf (symname
);
3487 private CCodeExpression
get_lock_expression (Statement stmt
, Expression resource
) {
3488 CCodeExpression l
= null;
3489 var inner_node
= ((MemberAccess
)resource
).inner
;
3490 var member
= resource
.symbol_reference
;
3491 var parent
= (TypeSymbol
)resource
.symbol_reference
.parent_symbol
;
3493 if (member
.is_instance_member ()) {
3494 if (inner_node
== null) {
3495 l
= new
CCodeIdentifier ("self");
3496 } else if (resource
.symbol_reference
.parent_symbol
!= current_type_symbol
) {
3497 l
= generate_instance_cast (get_cvalue (inner_node
), parent
);
3499 l
= get_cvalue (inner_node
);
3502 l
= new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (l
, "priv"), get_symbol_lock_name (resource
.symbol_reference
.name
));
3503 } else if (member
.is_class_member ()) {
3504 CCodeExpression klass
;
3506 if (current_method
!= null && current_method
.binding
== MemberBinding
.INSTANCE
||
3507 current_property_accessor
!= null && current_property_accessor
.prop
.binding
== MemberBinding
.INSTANCE
||
3508 (in_constructor
&& !in_static_or_class_context
)) {
3509 var k
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_OBJECT_GET_CLASS"));
3510 k
.add_argument (new
CCodeIdentifier ("self"));
3513 klass
= new
CCodeIdentifier ("klass");
3516 var get_class_private_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf(parent
.get_upper_case_cname ())));
3517 get_class_private_call
.add_argument (klass
);
3518 l
= new CCodeMemberAccess
.pointer (get_class_private_call
, get_symbol_lock_name (resource
.symbol_reference
.name
));
3520 string lock_name
= "%s_%s".printf(parent
.get_lower_case_cname (), resource
.symbol_reference
.name
);
3521 l
= new
CCodeIdentifier (get_symbol_lock_name (lock_name
));
3526 public override void visit_lock_statement (LockStatement stmt
) {
3527 var l
= get_lock_expression (stmt
, stmt
.resource
);
3529 var fc
= new
CCodeFunctionCall (new
CCodeIdentifier (((Method
) mutex_type
.scope
.lookup ("lock")).get_cname ()));
3530 fc
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, l
));
3532 ccode
.add_expression (fc
);
3535 public override void visit_unlock_statement (UnlockStatement stmt
) {
3536 var l
= get_lock_expression (stmt
, stmt
.resource
);
3538 var fc
= new
CCodeFunctionCall (new
CCodeIdentifier (((Method
) mutex_type
.scope
.lookup ("unlock")).get_cname ()));
3539 fc
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, l
));
3541 ccode
.add_expression (fc
);
3544 public override void visit_delete_statement (DeleteStatement stmt
) {
3545 var pointer_type
= (PointerType
) stmt
.expression
.value_type
;
3546 DataType type
= pointer_type
;
3547 if (pointer_type
.base_type
.data_type
!= null && pointer_type
.base_type
.data_type
.is_reference_type ()) {
3548 type
= pointer_type
.base_type
;
3551 var ccall
= new
CCodeFunctionCall (get_destroy_func_expression (type
));
3552 ccall
.add_argument (get_cvalue (stmt
.expression
));
3553 ccode
.add_expression (ccall
);
3556 public override void visit_expression (Expression expr
) {
3557 if (get_cvalue (expr
) != null && !expr
.lvalue
) {
3558 if (expr
.formal_value_type is GenericType
&& !(expr
.value_type is GenericType
)) {
3559 var st
= expr
.formal_value_type
.type_parameter
.parent_symbol
.parent_symbol as Struct
;
3560 if (expr
.formal_value_type
.type_parameter
.parent_symbol
!= garray_type
&&
3561 (st
== null || st
.get_cname () != "va_list")) {
3562 // GArray and va_list don't use pointer-based generics
3563 set_cvalue (expr
, convert_from_generic_pointer (get_cvalue (expr
), expr
.value_type
));
3567 // memory management, implicit casts, and boxing/unboxing
3568 set_cvalue (expr
, transform_expression (get_cvalue (expr
), expr
.value_type
, expr
.target_type
, expr
));
3570 if (expr
.formal_target_type is GenericType
&& !(expr
.target_type is GenericType
)) {
3571 if (expr
.formal_target_type
.type_parameter
.parent_symbol
!= garray_type
) {
3572 // GArray doesn't use pointer-based generics
3573 set_cvalue (expr
, convert_to_generic_pointer (get_cvalue (expr
), expr
.target_type
));
3579 public override void visit_boolean_literal (BooleanLiteral expr
) {
3580 if (context
.profile
== Profile
.GOBJECT
) {
3581 set_cvalue (expr
, new
CCodeConstant (expr
.value ?
"TRUE" : "FALSE"));
3583 cfile
.add_include ("stdbool.h");
3584 set_cvalue (expr
, new
CCodeConstant (expr
.value ?
"true" : "false"));
3588 public override void visit_character_literal (CharacterLiteral expr
) {
3589 if (expr
.get_char () >= 0x20 && expr
.get_char () < 0x80) {
3590 set_cvalue (expr
, new
CCodeConstant (expr
.value
));
3592 set_cvalue (expr
, new
CCodeConstant ("%uU".printf (expr
.get_char ())));
3596 public override void visit_integer_literal (IntegerLiteral expr
) {
3597 set_cvalue (expr
, new
CCodeConstant (expr
.value
+ expr
.type_suffix
));
3600 public override void visit_real_literal (RealLiteral expr
) {
3601 string c_literal
= expr
.value
;
3602 if (c_literal
.has_suffix ("d") || c_literal
.has_suffix ("D")) {
3603 // there is no suffix for double in C
3604 c_literal
= c_literal
.substring (0, c_literal
.length
- 1);
3606 if (!("." in c_literal
|| "e" in c_literal
|| "E" in c_literal
)) {
3607 // C requires period or exponent part for floating constants
3608 if ("f" in c_literal
|| "F" in c_literal
) {
3609 c_literal
= c_literal
.substring (0, c_literal
.length
- 1) + ".f";
3614 set_cvalue (expr
, new
CCodeConstant (c_literal
));
3617 public override void visit_string_literal (StringLiteral expr
) {
3618 set_cvalue (expr
, new CCodeConstant
.string (expr
.value
.replace ("\n", "\\n")));
3620 if (expr
.translate
) {
3621 // translated string constant
3623 var m
= (Method
) root_symbol
.scope
.lookup ("GLib").scope
.lookup ("_");
3624 add_symbol_declaration (cfile
, m
, m
.get_cname ());
3626 var translate
= new
CCodeFunctionCall (new
CCodeIdentifier ("_"));
3627 translate
.add_argument (get_cvalue (expr
));
3628 set_cvalue (expr
, translate
);
3632 public override void visit_regex_literal (RegexLiteral expr
) {
3633 string[] parts
= expr
.value
.split ("/", 3);
3634 string re
= parts
[2].escape ("");
3637 if (parts
[1].contains ("i")) {
3638 flags
+= " | G_REGEX_CASELESS";
3640 if (parts
[1].contains ("m")) {
3641 flags
+= " | G_REGEX_MULTILINE";
3643 if (parts
[1].contains ("s")) {
3644 flags
+= " | G_REGEX_DOTALL";
3646 if (parts
[1].contains ("x")) {
3647 flags
+= " | G_REGEX_EXTENDED";
3650 var regex_var
= get_temp_variable (regex_type
, true, expr
, false);
3651 emit_temp_var (regex_var
);
3653 var cdecl
= new
CCodeDeclaration ("GRegex*");
3655 var cname
= regex_var
.name
+ "regex_" + next_regex_id
.to_string ();
3656 if (this
.next_regex_id
== 0) {
3657 var fun
= new
CCodeFunction ("_thread_safe_regex_init", "GRegex*");
3658 fun
.modifiers
= CCodeModifiers
.STATIC
| CCodeModifiers
.INLINE
;
3659 fun
.add_parameter (new
CCodeParameter ("re", "GRegex**"));
3660 fun
.add_parameter (new
CCodeParameter ("pattern", "const gchar *"));
3661 fun
.add_parameter (new
CCodeParameter ("match_options", "GRegexMatchFlags"));
3663 push_function (fun
);
3665 var once_enter_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_once_init_enter"));
3666 once_enter_call
.add_argument (new
CCodeConstant ("(volatile gsize*) re"));
3667 ccode
.open_if (once_enter_call
);
3669 var regex_new_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_regex_new"));
3670 regex_new_call
.add_argument (new
CCodeConstant ("pattern"));
3671 regex_new_call
.add_argument (new
CCodeConstant ("match_options"));
3672 regex_new_call
.add_argument (new
CCodeConstant ("0"));
3673 regex_new_call
.add_argument (new
CCodeConstant ("NULL"));
3674 ccode
.add_assignment (new
CCodeIdentifier ("GRegex* val"), regex_new_call
);
3676 var once_leave_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_once_init_leave"));
3677 once_leave_call
.add_argument (new
CCodeConstant ("(volatile gsize*) re"));
3678 once_leave_call
.add_argument (new
CCodeConstant ("(gsize) val"));
3679 ccode
.add_expression (once_leave_call
);
3683 ccode
.add_return (new
CCodeIdentifier ("*re"));
3687 cfile
.add_function (fun
);
3689 this
.next_regex_id
++;
3691 cdecl
.add_declarator (new
CCodeVariableDeclarator (cname
+ " = NULL"));
3692 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
3694 var regex_const
= new
CCodeConstant ("_thread_safe_regex_init (&%s, \"%s\", %s)".printf (cname
, re
, flags
));
3696 cfile
.add_constant_declaration (cdecl
);
3697 set_cvalue (expr
, regex_const
);
3700 public override void visit_null_literal (NullLiteral expr
) {
3701 if (context
.profile
!= Profile
.GOBJECT
) {
3702 cfile
.add_include ("stddef.h");
3704 set_cvalue (expr
, new
CCodeConstant ("NULL"));
3706 var array_type
= expr
.target_type as ArrayType
;
3707 var delegate_type
= expr
.target_type as DelegateType
;
3708 if (array_type
!= null) {
3709 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
3710 append_array_length (expr
, new
CCodeConstant ("0"));
3712 } else if (delegate_type
!= null && delegate_type
.delegate_symbol
.has_target
) {
3713 set_delegate_target (expr
, new
CCodeConstant ("NULL"));
3714 set_delegate_target_destroy_notify (expr
, new
CCodeConstant ("NULL"));
3718 public abstract TargetValue
get_local_cvalue (LocalVariable local
);
3720 public abstract TargetValue
get_parameter_cvalue (Parameter param
);
3722 public abstract TargetValue
get_field_cvalue (Field field
, Expression? instance
);
3724 public virtual string get_delegate_target_cname (string delegate_cname
) {
3725 assert_not_reached ();
3728 public virtual CCodeExpression
get_delegate_target_cexpression (Expression delegate_expr
, out CCodeExpression delegate_target_destroy_notify
) {
3729 assert_not_reached ();
3732 public virtual CCodeExpression
get_delegate_target_cvalue (TargetValue value
) {
3733 return new
CCodeInvalidExpression ();
3736 public virtual CCodeExpression
get_delegate_target_destroy_notify_cvalue (TargetValue value
) {
3737 return new
CCodeInvalidExpression ();
3740 public virtual string get_delegate_target_destroy_notify_cname (string delegate_cname
) {
3741 assert_not_reached ();
3744 public override void visit_base_access (BaseAccess expr
) {
3745 CCodeExpression this_access
;
3746 if (is_in_coroutine ()) {
3748 this_access
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("data"), "self");
3750 this_access
= new
CCodeIdentifier ("self");
3753 set_cvalue (expr
, generate_instance_cast (this_access
, expr
.value_type
.data_type
));
3756 public override void visit_postfix_expression (PostfixExpression expr
) {
3757 MemberAccess ma
= find_property_access (expr
.inner
);
3759 // property postfix expression
3760 var prop
= (Property
) ma
.symbol_reference
;
3762 // assign current value to temp variable
3763 var temp_decl
= get_temp_variable (prop
.property_type
, true, expr
, false);
3764 emit_temp_var (temp_decl
);
3765 ccode
.add_assignment (get_variable_cexpression (temp_decl
.name
), get_cvalue (expr
.inner
));
3767 // increment/decrement property
3768 var op
= expr
.increment ? CCodeBinaryOperator
.PLUS
: CCodeBinaryOperator
.MINUS
;
3769 var cexpr
= new
CCodeBinaryExpression (op
, get_variable_cexpression (temp_decl
.name
), new
CCodeConstant ("1"));
3770 store_property (prop
, ma
.inner
, new
GLibValue (expr
.value_type
, cexpr
));
3772 // return previous value
3773 set_cvalue (expr
, get_variable_cexpression (temp_decl
.name
));
3777 if (expr
.parent_node is ExpressionStatement
) {
3778 var op
= expr
.increment ? CCodeUnaryOperator
.POSTFIX_INCREMENT
: CCodeUnaryOperator
.POSTFIX_DECREMENT
;
3780 ccode
.add_expression (new
CCodeUnaryExpression (op
, get_cvalue (expr
.inner
)));
3782 // assign current value to temp variable
3783 var temp_decl
= get_temp_variable (expr
.inner
.value_type
, true, expr
, false);
3784 emit_temp_var (temp_decl
);
3785 ccode
.add_assignment (get_variable_cexpression (temp_decl
.name
), get_cvalue (expr
.inner
));
3787 // increment/decrement variable
3788 var op
= expr
.increment ? CCodeBinaryOperator
.PLUS
: CCodeBinaryOperator
.MINUS
;
3789 var cexpr
= new
CCodeBinaryExpression (op
, get_variable_cexpression (temp_decl
.name
), new
CCodeConstant ("1"));
3790 ccode
.add_assignment (get_cvalue (expr
.inner
), cexpr
);
3792 // return previous value
3793 set_cvalue (expr
, get_variable_cexpression (temp_decl
.name
));
3797 private MemberAccess?
find_property_access (Expression expr
) {
3798 if (!(expr is MemberAccess
)) {
3802 var ma
= (MemberAccess
) expr
;
3803 if (ma
.symbol_reference is Property
) {
3810 bool is_limited_generic_type (DataType type
) {
3811 var cl
= type
.type_parameter
.parent_symbol as Class
;
3812 var st
= type
.type_parameter
.parent_symbol as Struct
;
3813 if ((cl
!= null && cl
.is_compact
) || st
!= null) {
3814 // compact classes and structs only
3815 // have very limited generics support
3821 public bool requires_copy (DataType type
) {
3822 if (!type
.is_disposable ()) {
3826 var cl
= type
.data_type as Class
;
3827 if (cl
!= null && cl
.is_reference_counting ()
3828 && cl
.get_ref_function () == "") {
3829 // empty ref_function => no ref necessary
3833 if (type
.type_parameter
!= null) {
3834 if (is_limited_generic_type (type
)) {
3842 public bool requires_destroy (DataType type
) {
3843 if (!type
.is_disposable ()) {
3847 var array_type
= type as ArrayType
;
3848 if (array_type
!= null && array_type
.fixed_length
) {
3849 return requires_destroy (array_type
.element_type
);
3852 var cl
= type
.data_type as Class
;
3853 if (cl
!= null && cl
.is_reference_counting ()
3854 && cl
.get_unref_function () == "") {
3855 // empty unref_function => no unref necessary
3859 if (type
.type_parameter
!= null) {
3860 if (is_limited_generic_type (type
)) {
3868 bool is_ref_function_void (DataType type
) {
3869 var cl
= type
.data_type as Class
;
3870 if (cl
!= null && cl
.ref_function_void
) {
3877 public virtual CCodeExpression?
get_ref_cexpression (DataType expression_type
, CCodeExpression cexpr
, Expression? expr
, CodeNode node
) {
3878 if (expression_type is DelegateType
) {
3882 if (expression_type is ValueType
&& !expression_type
.nullable
) {
3883 // normal value type, no null check
3884 // (copy (&expr, &temp), temp)
3886 var decl
= get_temp_variable (expression_type
, false, node
);
3887 emit_temp_var (decl
);
3889 var ctemp
= get_variable_cexpression (decl
.name
);
3891 var vt
= (ValueType
) expression_type
;
3892 var st
= (Struct
) vt
.type_symbol
;
3893 var copy_call
= new
CCodeFunctionCall (new
CCodeIdentifier (st
.get_copy_function ()));
3894 copy_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
));
3895 copy_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, ctemp
));
3897 if (!st
.has_copy_function
) {
3898 generate_struct_copy_function (st
);
3901 var ccomma
= new
CCodeCommaExpression ();
3903 if (st
.get_copy_function () == "g_value_copy") {
3904 // GValue requires g_value_init in addition to g_value_copy
3906 var value_type_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_VALUE_TYPE"));
3907 value_type_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
));
3909 var init_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_value_init"));
3910 init_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, ctemp
));
3911 init_call
.add_argument (value_type_call
);
3913 ccomma
.append_expression (init_call
);
3916 ccomma
.append_expression (copy_call
);
3917 ccomma
.append_expression (ctemp
);
3919 if (gvalue_type
!= null && expression_type
.data_type
== gvalue_type
) {
3920 // g_value_init/copy must not be called for uninitialized values
3921 var cisvalid
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_IS_VALUE"));
3922 cisvalid
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
));
3924 return new
CCodeConditionalExpression (cisvalid
, ccomma
, cexpr
);
3930 /* (temp = expr, temp == NULL ? NULL : ref (temp))
3932 * can be simplified to
3934 * if static type of expr is non-null
3937 var dupexpr
= get_dup_func_expression (expression_type
, node
.source_reference
);
3939 if (dupexpr
== null) {
3944 if (dupexpr is CCodeIdentifier
&& !(expression_type is ArrayType
) && !(expression_type is GenericType
) && !is_ref_function_void (expression_type
)) {
3945 // generate and call NULL-aware ref function to reduce number
3946 // of temporary variables and simplify code
3948 var dupid
= (CCodeIdentifier
) dupexpr
;
3949 string dup0_func
= "_%s0".printf (dupid
.name
);
3951 // g_strdup is already NULL-safe
3952 if (dupid
.name
== "g_strdup") {
3953 dup0_func
= dupid
.name
;
3954 } else if (add_wrapper (dup0_func
)) {
3955 string pointer_cname
= "gpointer";
3956 if (context
.profile
== Profile
.POSIX
) {
3957 pointer_cname
= "void*";
3959 var dup0_fun
= new
CCodeFunction (dup0_func
, pointer_cname
);
3960 dup0_fun
.add_parameter (new
CCodeParameter ("self", pointer_cname
));
3961 dup0_fun
.modifiers
= CCodeModifiers
.STATIC
;
3963 push_function (dup0_fun
);
3965 var dup_call
= new
CCodeFunctionCall (dupexpr
);
3966 dup_call
.add_argument (new
CCodeIdentifier ("self"));
3968 ccode
.add_return (new
CCodeConditionalExpression (new
CCodeIdentifier ("self"), dup_call
, new
CCodeConstant ("NULL")));
3972 cfile
.add_function (dup0_fun
);
3975 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (dup0_func
));
3976 ccall
.add_argument (cexpr
);
3980 var ccall
= new
CCodeFunctionCall (dupexpr
);
3982 if (!(expression_type is ArrayType
) && expr
!= null && expr
.is_non_null ()
3983 && !is_ref_function_void (expression_type
)) {
3984 // expression is non-null
3985 ccall
.add_argument (get_cvalue (expr
));
3989 var decl
= get_temp_variable (expression_type
, false, node
, false);
3990 emit_temp_var (decl
);
3992 var ctemp
= get_variable_cexpression (decl
.name
);
3994 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, ctemp
, new
CCodeConstant ("NULL"));
3995 if (expression_type
.type_parameter
!= null) {
3996 // dup functions are optional for type parameters
3997 var cdupisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, get_dup_func_expression (expression_type
, node
.source_reference
), new
CCodeConstant ("NULL"));
3998 cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.OR
, cisnull
, cdupisnull
);
4001 if (expression_type
.type_parameter
!= null) {
4002 // cast from gconstpointer to gpointer as GBoxedCopyFunc expects gpointer
4003 ccall
.add_argument (new
CCodeCastExpression (ctemp
, "gpointer"));
4005 ccall
.add_argument (ctemp
);
4008 if (expression_type is ArrayType
) {
4009 var array_type
= (ArrayType
) expression_type
;
4011 CCodeExpression csizeexpr
= null;
4012 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4014 csizeexpr
= get_array_length_cexpression (expr
, dim
);
4017 csizeexpr
= new
CCodeBinaryExpression (CCodeBinaryOperator
.MUL
, csizeexpr
, get_array_length_cexpression (expr
, dim
));
4021 ccall
.add_argument (csizeexpr
);
4023 if (array_type
.element_type is GenericType
) {
4024 var elem_dupexpr
= get_dup_func_expression (array_type
.element_type
, node
.source_reference
);
4025 if (elem_dupexpr
== null) {
4026 elem_dupexpr
= new
CCodeConstant ("NULL");
4028 ccall
.add_argument (elem_dupexpr
);
4032 var ccomma
= new
CCodeCommaExpression ();
4033 ccomma
.append_expression (new
CCodeAssignment (ctemp
, cexpr
));
4035 CCodeExpression cifnull
;
4036 if (expression_type
.data_type
!= null) {
4037 cifnull
= new
CCodeConstant ("NULL");
4039 // the value might be non-null even when the dup function is null,
4040 // so we may not just use NULL for type parameters
4042 // cast from gconstpointer to gpointer as methods in
4043 // generic classes may not return gconstpointer
4044 cifnull
= new
CCodeCastExpression (ctemp
, "gpointer");
4046 ccomma
.append_expression (new
CCodeConditionalExpression (cisnull
, cifnull
, ccall
));
4048 // repeat temp variable at the end of the comma expression
4049 // if the ref function returns void
4050 if (is_ref_function_void (expression_type
)) {
4051 ccomma
.append_expression (ctemp
);
4058 bool is_reference_type_argument (DataType type_arg
) {
4059 if (type_arg is ErrorType
|| (type_arg
.data_type
!= null && type_arg
.data_type
.is_reference_type ())) {
4066 bool is_nullable_value_type_argument (DataType type_arg
) {
4067 if (type_arg is ValueType
&& type_arg
.nullable
) {
4074 bool is_signed_integer_type_argument (DataType type_arg
) {
4075 var st
= type_arg
.data_type as Struct
;
4076 if (type_arg
.nullable
) {
4078 } else if (st
== bool_type
.data_type
) {
4080 } else if (st
== char_type
.data_type
) {
4082 } else if (unichar_type
!= null && st
== unichar_type
.data_type
) {
4084 } else if (st
== short_type
.data_type
) {
4086 } else if (st
== int_type
.data_type
) {
4088 } else if (st
== long_type
.data_type
) {
4090 } else if (st
== int8_type
.data_type
) {
4092 } else if (st
== int16_type
.data_type
) {
4094 } else if (st
== int32_type
.data_type
) {
4096 } else if (st
== gtype_type
) {
4098 } else if (type_arg is EnumValueType
) {
4105 bool is_unsigned_integer_type_argument (DataType type_arg
) {
4106 var st
= type_arg
.data_type as Struct
;
4107 if (type_arg
.nullable
) {
4109 } else if (st
== uchar_type
.data_type
) {
4111 } else if (st
== ushort_type
.data_type
) {
4113 } else if (st
== uint_type
.data_type
) {
4115 } else if (st
== ulong_type
.data_type
) {
4117 } else if (st
== uint8_type
.data_type
) {
4119 } else if (st
== uint16_type
.data_type
) {
4121 } else if (st
== uint32_type
.data_type
) {
4128 public void check_type (DataType type
) {
4129 var array_type
= type as ArrayType
;
4130 if (array_type
!= null) {
4131 check_type (array_type
.element_type
);
4132 if (array_type
.element_type is ArrayType
) {
4133 Report
.error (type
.source_reference
, "Stacked arrays are not supported");
4134 } else if (array_type
.element_type is DelegateType
) {
4135 var delegate_type
= (DelegateType
) array_type
.element_type
;
4136 if (delegate_type
.delegate_symbol
.has_target
) {
4137 Report
.error (type
.source_reference
, "Delegates with target are not supported as array element type");
4141 foreach (var type_arg
in type
.get_type_arguments ()) {
4142 check_type (type_arg
);
4143 check_type_argument (type_arg
);
4147 void check_type_argument (DataType type_arg
) {
4148 if (type_arg is GenericType
4149 || type_arg is PointerType
4150 || is_reference_type_argument (type_arg
)
4151 || is_nullable_value_type_argument (type_arg
)
4152 || is_signed_integer_type_argument (type_arg
)
4153 || is_unsigned_integer_type_argument (type_arg
)) {
4155 } else if (type_arg is DelegateType
) {
4156 var delegate_type
= (DelegateType
) type_arg
;
4157 if (delegate_type
.delegate_symbol
.has_target
) {
4158 Report
.error (type_arg
.source_reference
, "Delegates with target are not supported as generic type arguments");
4161 Report
.error (type_arg
.source_reference
, "`%s' is not a supported generic type argument, use `?' to box value types".printf (type_arg
.to_string ()));
4165 public virtual void generate_class_declaration (Class cl
, CCodeFile decl_space
) {
4166 if (add_symbol_declaration (decl_space
, cl
, cl
.get_cname ())) {
4171 public virtual void generate_interface_declaration (Interface iface
, CCodeFile decl_space
) {
4174 public virtual void generate_method_declaration (Method m
, CCodeFile decl_space
) {
4177 public virtual void generate_error_domain_declaration (ErrorDomain edomain
, CCodeFile decl_space
) {
4180 public void add_generic_type_arguments (Map
<int,CCodeExpression
> arg_map
, List
<DataType
> type_args
, CodeNode expr
, bool is_chainup
= false) {
4181 int type_param_index
= 0;
4182 foreach (var type_arg
in type_args
) {
4183 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.01), get_type_id_expression (type_arg
, is_chainup
));
4184 if (requires_copy (type_arg
)) {
4185 var dup_func
= get_dup_func_expression (type_arg
, type_arg
.source_reference
, is_chainup
);
4186 if (dup_func
== null) {
4187 // type doesn't contain a copy function
4191 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.02), new
CCodeCastExpression (dup_func
, "GBoxedCopyFunc"));
4192 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.03), get_destroy_func_expression (type_arg
, is_chainup
));
4194 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.02), new
CCodeConstant ("NULL"));
4195 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.03), new
CCodeConstant ("NULL"));
4201 public override void visit_object_creation_expression (ObjectCreationExpression expr
) {
4202 CCodeExpression instance
= null;
4203 CCodeExpression creation_expr
= null;
4205 check_type (expr
.type_reference
);
4207 var st
= expr
.type_reference
.data_type as Struct
;
4208 if ((st
!= null && (!st
.is_simple_type () || st
.get_cname () == "va_list")) || expr
.get_object_initializer ().size
> 0) {
4209 // value-type initialization or object creation expression with object initializer
4211 var local
= expr
.parent_node as LocalVariable
;
4212 if (local
!= null && has_simple_struct_initializer (local
)) {
4213 if (local
.captured
) {
4214 var block
= (Block
) local
.parent_symbol
;
4215 instance
= new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block
))), get_variable_cname (local
.name
));
4217 instance
= get_variable_cexpression (get_variable_cname (local
.name
));
4220 var temp_decl
= get_temp_variable (expr
.type_reference
, false, expr
);
4221 emit_temp_var (temp_decl
);
4223 instance
= get_variable_cexpression (get_variable_cname (temp_decl
.name
));
4227 if (expr
.symbol_reference
== null) {
4228 // no creation method
4229 if (expr
.type_reference
.data_type is Struct
) {
4230 // memset needs string.h
4231 cfile
.add_include ("string.h");
4232 var creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memset"));
4233 creation_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, instance
));
4234 creation_call
.add_argument (new
CCodeConstant ("0"));
4235 creation_call
.add_argument (new
CCodeIdentifier ("sizeof (%s)".printf (expr
.type_reference
.get_cname ())));
4237 creation_expr
= creation_call
;
4239 } else if (expr
.type_reference
.data_type
== glist_type
||
4240 expr
.type_reference
.data_type
== gslist_type
) {
4241 // NULL is an empty list
4242 set_cvalue (expr
, new
CCodeConstant ("NULL"));
4243 } else if (expr
.symbol_reference is Method
) {
4244 // use creation method
4245 var m
= (Method
) expr
.symbol_reference
;
4246 var params
= m
.get_parameters ();
4247 CCodeFunctionCall creation_call
;
4249 generate_method_declaration (m
, cfile
);
4251 var cl
= expr
.type_reference
.data_type as Class
;
4253 if (!m
.has_new_function
) {
4254 // use construct function directly
4255 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier (m
.get_real_cname ()));
4256 creation_call
.add_argument (new
CCodeIdentifier (cl
.get_type_id ()));
4258 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier (m
.get_cname ()));
4261 if ((st
!= null && !st
.is_simple_type ()) && !(m
.cinstance_parameter_position
< 0)) {
4262 creation_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, instance
));
4263 } else if (st
!= null && st
.get_cname () == "va_list") {
4264 creation_call
.add_argument (instance
);
4265 if (m
.get_cname () == "va_start") {
4266 Parameter last_param
= null;
4267 foreach (var param
in current_method
.get_parameters ()) {
4268 if (param
.ellipsis
) {
4273 creation_call
.add_argument (new
CCodeIdentifier (get_variable_cname (last_param
.name
)));
4277 generate_type_declaration (expr
.type_reference
, cfile
);
4279 var carg_map
= new HashMap
<int,CCodeExpression
> (direct_hash
, direct_equal
);
4281 if (cl
!= null && !cl
.is_compact
) {
4282 add_generic_type_arguments (carg_map
, expr
.type_reference
.get_type_arguments (), expr
);
4283 } else if (cl
!= null && m
.simple_generics
) {
4284 int type_param_index
= 0;
4285 foreach (var type_arg
in expr
.type_reference
.get_type_arguments ()) {
4286 if (requires_copy (type_arg
)) {
4287 carg_map
.set (get_param_pos (-1 + 0.1 * type_param_index
+ 0.03), get_destroy0_func_expression (type_arg
));
4289 carg_map
.set (get_param_pos (-1 + 0.1 * type_param_index
+ 0.03), new
CCodeConstant ("NULL"));
4295 bool ellipsis
= false;
4299 Iterator
<Parameter
> params_it
= params
.iterator ();
4300 foreach (Expression arg
in expr
.get_argument_list ()) {
4301 CCodeExpression cexpr
= get_cvalue (arg
);
4302 Parameter param
= null;
4303 if (params_it
.next ()) {
4304 param
= params_it
.get ();
4305 ellipsis
= param
.ellipsis
;
4307 if (!param
.no_array_length
&& param
.variable_type is ArrayType
) {
4308 var array_type
= (ArrayType
) param
.variable_type
;
4309 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4310 carg_map
.set (get_param_pos (param
.carray_length_parameter_position
+ 0.01 * dim
), get_array_length_cexpression (arg
, dim
));
4312 } else if (param
.variable_type is DelegateType
) {
4313 var deleg_type
= (DelegateType
) param
.variable_type
;
4314 var d
= deleg_type
.delegate_symbol
;
4316 CCodeExpression delegate_target_destroy_notify
;
4317 var delegate_target
= get_delegate_target_cexpression (arg
, out delegate_target_destroy_notify
);
4318 carg_map
.set (get_param_pos (param
.cdelegate_target_parameter_position
), delegate_target
);
4319 if (deleg_type
.value_owned
) {
4320 carg_map
.set (get_param_pos (param
.cdelegate_target_parameter_position
+ 0.01), delegate_target_destroy_notify
);
4325 cexpr
= handle_struct_argument (param
, arg
, cexpr
);
4327 if (param
.ctype
!= null) {
4328 cexpr
= new
CCodeCastExpression (cexpr
, param
.ctype
);
4331 cexpr
= handle_struct_argument (null, arg
, cexpr
);
4334 arg_pos
= get_param_pos (param
.cparameter_position
, ellipsis
);
4336 // default argument position
4337 cexpr
= handle_struct_argument (null, arg
, cexpr
);
4338 arg_pos
= get_param_pos (i
, ellipsis
);
4341 carg_map
.set (arg_pos
, cexpr
);
4345 while (params_it
.next ()) {
4346 var param
= params_it
.get ();
4348 if (param
.ellipsis
) {
4353 if (param
.initializer
== null) {
4354 Report
.error (expr
.source_reference
, "no default expression for argument %d".printf (i
));
4358 /* evaluate default expression here as the code
4359 * generator might not have visited the formal
4361 param
.initializer
.emit (this
);
4363 carg_map
.set (get_param_pos (param
.cparameter_position
), get_cvalue (param
.initializer
));
4367 // append C arguments in the right order
4372 foreach (int pos
in carg_map
.get_keys ()) {
4373 if (pos
> last_pos
&& (min_pos
== -1 || pos
< min_pos
)) {
4377 if (min_pos
== -1) {
4380 creation_call
.add_argument (carg_map
.get (min_pos
));
4384 if ((st
!= null && !st
.is_simple_type ()) && m
.cinstance_parameter_position
< 0) {
4385 // instance parameter is at the end in a struct creation method
4386 creation_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, instance
));
4389 if (expr
.tree_can_fail
) {
4391 current_method_inner_error
= true;
4392 creation_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression ("_inner_error_")));
4396 /* ensure variable argument list ends with NULL
4397 * except when using printf-style arguments */
4398 if (!m
.printf_format
&& !m
.scanf_format
&& m
.sentinel
!= "") {
4399 creation_call
.add_argument (new
CCodeConstant (m
.sentinel
));
4403 creation_expr
= creation_call
;
4405 // cast the return value of the creation method back to the intended type if
4406 // it requested a special C return type
4407 if (get_custom_creturn_type (m
) != null) {
4408 creation_expr
= new
CCodeCastExpression (creation_expr
, expr
.type_reference
.get_cname ());
4410 } else if (expr
.symbol_reference is ErrorCode
) {
4411 var ecode
= (ErrorCode
) expr
.symbol_reference
;
4412 var edomain
= (ErrorDomain
) ecode
.parent_symbol
;
4413 CCodeFunctionCall creation_call
;
4415 generate_error_domain_declaration (edomain
, cfile
);
4417 if (expr
.get_argument_list ().size
== 1) {
4418 // must not be a format argument
4419 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_error_new_literal"));
4421 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_error_new"));
4423 creation_call
.add_argument (new
CCodeIdentifier (edomain
.get_upper_case_cname ()));
4424 creation_call
.add_argument (new
CCodeIdentifier (ecode
.get_cname ()));
4426 foreach (Expression arg
in expr
.get_argument_list ()) {
4427 creation_call
.add_argument (get_cvalue (arg
));
4430 creation_expr
= creation_call
;
4435 var local
= expr
.parent_node as LocalVariable
;
4436 if (local
!= null && has_simple_struct_initializer (local
)) {
4437 // no temporary variable necessary
4438 ccode
.add_expression (creation_expr
);
4439 set_cvalue (expr
, instance
);
4441 } else if (instance
!= null) {
4442 if (expr
.type_reference
.data_type is Struct
) {
4443 ccode
.add_expression (creation_expr
);
4445 ccode
.add_assignment (instance
, creation_expr
);
4448 foreach (MemberInitializer init
in expr
.get_object_initializer ()) {
4449 if (init
.symbol_reference is Field
) {
4450 var f
= (Field
) init
.symbol_reference
;
4451 var instance_target_type
= get_data_type_for_symbol ((TypeSymbol
) f
.parent_symbol
);
4452 var typed_inst
= transform_expression (instance
, expr
.type_reference
, instance_target_type
);
4453 CCodeExpression lhs
;
4454 if (expr
.type_reference
.data_type is Struct
) {
4455 lhs
= new
CCodeMemberAccess (typed_inst
, f
.get_cname ());
4457 lhs
= new CCodeMemberAccess
.pointer (typed_inst
, f
.get_cname ());
4459 ccode
.add_assignment (lhs
, get_cvalue (init
.initializer
));
4461 if (f
.variable_type is ArrayType
&& !f
.no_array_length
) {
4462 var array_type
= (ArrayType
) f
.variable_type
;
4463 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4464 if (expr
.type_reference
.data_type is Struct
) {
4465 lhs
= new
CCodeMemberAccess (typed_inst
, get_array_length_cname (f
.get_cname (), dim
));
4467 lhs
= new CCodeMemberAccess
.pointer (typed_inst
, get_array_length_cname (f
.get_cname (), dim
));
4469 var rhs_array_len
= get_array_length_cexpression (init
.initializer
, dim
);
4470 ccode
.add_assignment (lhs
, rhs_array_len
);
4472 } else if (f
.variable_type is DelegateType
&& (f
.variable_type as DelegateType
).delegate_symbol
.has_target
&& !f
.no_delegate_target
) {
4473 if (expr
.type_reference
.data_type is Struct
) {
4474 lhs
= new
CCodeMemberAccess (typed_inst
, get_delegate_target_cname (f
.get_cname ()));
4476 lhs
= new CCodeMemberAccess
.pointer (typed_inst
, get_delegate_target_cname (f
.get_cname ()));
4478 CCodeExpression rhs_delegate_target_destroy_notify
;
4479 var rhs_delegate_target
= get_delegate_target_cexpression (init
.initializer
, out rhs_delegate_target_destroy_notify
);
4480 ccode
.add_assignment (lhs
, rhs_delegate_target
);
4483 var cl
= f
.parent_symbol as Class
;
4485 generate_class_struct_declaration (cl
, cfile
);
4487 } else if (init
.symbol_reference is Property
) {
4488 var inst_ma
= new MemberAccess
.simple ("new");
4489 inst_ma
.value_type
= expr
.type_reference
;
4490 set_cvalue (inst_ma
, instance
);
4491 store_property ((Property
) init
.symbol_reference
, inst_ma
, init
.initializer
.target_value
);
4495 creation_expr
= instance
;
4498 if (creation_expr
!= null) {
4499 var temp_var
= get_temp_variable (expr
.value_type
);
4500 var temp_ref
= get_variable_cexpression (temp_var
.name
);
4502 emit_temp_var (temp_var
);
4504 ccode
.add_assignment (temp_ref
, creation_expr
);
4505 set_cvalue (expr
, temp_ref
);
4509 public CCodeExpression?
handle_struct_argument (Parameter? param
, Expression arg
, CCodeExpression? cexpr
) {
4511 if (param
!= null) {
4512 type
= param
.variable_type
;
4515 type
= arg
.value_type
;
4518 // pass non-simple struct instances always by reference
4519 if (!(arg
.value_type is NullType
) && type
.is_real_struct_type ()) {
4520 // we already use a reference for arguments of ref, out, and nullable parameters
4521 if ((param
== null || param
.direction
== ParameterDirection
.IN
) && !type
.nullable
) {
4522 var unary
= cexpr as CCodeUnaryExpression
;
4523 if (unary
!= null && unary
.operator
== CCodeUnaryOperator
.POINTER_INDIRECTION
) {
4526 } else if (cexpr is CCodeIdentifier
|| cexpr is CCodeMemberAccess
) {
4527 return new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
);
4529 // if cexpr is e.g. a function call, we can't take the address of the expression
4530 // (tmp = expr, &tmp)
4531 var ccomma
= new
CCodeCommaExpression ();
4533 var temp_var
= get_temp_variable (type
, true, null, false);
4534 emit_temp_var (temp_var
);
4535 ccomma
.append_expression (new
CCodeAssignment (get_variable_cexpression (temp_var
.name
), cexpr
));
4536 ccomma
.append_expression (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (temp_var
.name
)));
4546 public override void visit_sizeof_expression (SizeofExpression expr
) {
4547 generate_type_declaration (expr
.type_reference
, cfile
);
4549 var csizeof
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
4550 csizeof
.add_argument (new
CCodeIdentifier (expr
.type_reference
.get_cname ()));
4551 set_cvalue (expr
, csizeof
);
4554 public override void visit_typeof_expression (TypeofExpression expr
) {
4555 set_cvalue (expr
, get_type_id_expression (expr
.type_reference
));
4558 public override void visit_unary_expression (UnaryExpression expr
) {
4559 if (expr
.operator
== UnaryOperator
.REF
|| expr
.operator
== UnaryOperator
.OUT
) {
4560 var glib_value
= (GLibValue
) expr
.inner
.target_value
;
4562 var ref_value
= new
GLibValue (glib_value
.value_type
);
4563 ref_value
.cvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.cvalue
);
4565 if (glib_value
.array_length_cvalues
!= null) {
4566 for (int i
= 0; i
< glib_value
.array_length_cvalues
.size
; i
++) {
4567 ref_value
.append_array_length_cvalue (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.array_length_cvalues
[i
]));
4571 if (glib_value
.delegate_target_cvalue
!= null) {
4572 ref_value
.delegate_target_cvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.delegate_target_cvalue
);
4574 if (glib_value
.delegate_target_destroy_notify_cvalue
!= null) {
4575 ref_value
.delegate_target_destroy_notify_cvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.delegate_target_destroy_notify_cvalue
);
4578 expr
.target_value
= ref_value
;
4582 CCodeUnaryOperator op
;
4583 if (expr
.operator
== UnaryOperator
.PLUS
) {
4584 op
= CCodeUnaryOperator
.PLUS
;
4585 } else if (expr
.operator
== UnaryOperator
.MINUS
) {
4586 op
= CCodeUnaryOperator
.MINUS
;
4587 } else if (expr
.operator
== UnaryOperator
.LOGICAL_NEGATION
) {
4588 op
= CCodeUnaryOperator
.LOGICAL_NEGATION
;
4589 } else if (expr
.operator
== UnaryOperator
.BITWISE_COMPLEMENT
) {
4590 op
= CCodeUnaryOperator
.BITWISE_COMPLEMENT
;
4591 } else if (expr
.operator
== UnaryOperator
.INCREMENT
) {
4592 op
= CCodeUnaryOperator
.PREFIX_INCREMENT
;
4593 } else if (expr
.operator
== UnaryOperator
.DECREMENT
) {
4594 op
= CCodeUnaryOperator
.PREFIX_DECREMENT
;
4596 assert_not_reached ();
4598 set_cvalue (expr
, new
CCodeUnaryExpression (op
, get_cvalue (expr
.inner
)));
4601 public CCodeExpression?
try_cast_value_to_type (CCodeExpression ccodeexpr
, DataType from
, DataType to
, Expression? expr
= null) {
4602 if (from
== null || gvalue_type
== null || from
.data_type
!= gvalue_type
|| to
.get_type_id () == null) {
4606 // explicit conversion from GValue
4607 var ccall
= new
CCodeFunctionCall (get_value_getter_function (to
));
4608 CCodeExpression gvalue
;
4609 if (from
.nullable
) {
4612 gvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, ccodeexpr
);
4614 ccall
.add_argument (gvalue
);
4616 CCodeExpression rv
= ccall
;
4618 if (expr
!= null && to is ArrayType
) {
4619 // null-terminated string array
4620 var len_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strv_length"));
4621 len_call
.add_argument (rv
);
4622 append_array_length (expr
, len_call
);
4623 } else if (to is StructValueType
) {
4624 var temp_decl
= get_temp_variable (to
, true, null, true);
4625 emit_temp_var (temp_decl
);
4626 var ctemp
= get_variable_cexpression (temp_decl
.name
);
4628 rv
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeCastExpression (rv
, (new
PointerType(to
)).get_cname ()));
4629 var holds
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_VALUE_HOLDS"));
4630 holds
.add_argument (gvalue
);
4631 holds
.add_argument (new
CCodeIdentifier (to
.get_type_id ()));
4632 var cond
= new
CCodeBinaryExpression (CCodeBinaryOperator
.AND
, holds
, ccall
);
4633 var warn
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_warning"));
4634 warn
.add_argument (new
CCodeConstant ("\"Invalid GValue unboxing (wrong type or NULL)\""));
4635 var fail
= new
CCodeCommaExpression ();
4636 fail
.append_expression (warn
);
4637 fail
.append_expression (ctemp
);
4638 rv
= new
CCodeConditionalExpression (cond
, rv
, fail
);
4644 int next_variant_function_id
= 0;
4646 public CCodeExpression?
try_cast_variant_to_type (CCodeExpression ccodeexpr
, DataType from
, DataType to
, Expression? expr
= null) {
4647 if (from
== null || gvariant_type
== null || from
.data_type
!= gvariant_type
) {
4651 string variant_func
= "_variant_get%d".printf (++next_variant_function_id
);
4653 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (variant_func
));
4654 ccall
.add_argument (ccodeexpr
);
4656 var cfunc
= new
CCodeFunction (variant_func
);
4657 cfunc
.modifiers
= CCodeModifiers
.STATIC
;
4658 cfunc
.add_parameter (new
CCodeParameter ("value", "GVariant*"));
4660 if (!to
.is_real_non_null_struct_type ()) {
4661 cfunc
.return_type
= to
.get_cname ();
4664 if (to
.is_real_non_null_struct_type ()) {
4665 // structs are returned via out parameter
4666 cfunc
.add_parameter (new
CCodeParameter ("result", to
.get_cname () + "*"));
4667 } else if (to is ArrayType
) {
4668 // return array length if appropriate
4669 var array_type
= (ArrayType
) to
;
4671 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4672 var temp_decl
= get_temp_variable (int_type
, false, expr
);
4673 emit_temp_var (temp_decl
);
4675 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (temp_decl
.name
)));
4676 cfunc
.add_parameter (new
CCodeParameter (get_array_length_cname ("result", dim
), "int*"));
4677 append_array_length (expr
, get_variable_cexpression (temp_decl
.name
));
4681 push_function (cfunc
);
4683 var result
= deserialize_expression (to
, new
CCodeIdentifier ("value"), new
CCodeIdentifier ("*result"));
4684 ccode
.add_return (result
);
4688 cfile
.add_function_declaration (cfunc
);
4689 cfile
.add_function (cfunc
);
4694 public virtual CCodeExpression?
deserialize_expression (DataType type
, CCodeExpression variant_expr
, CCodeExpression? expr
, CCodeExpression? error_expr
= null, out bool may_fail
= null) {
4698 public virtual CCodeExpression?
serialize_expression (DataType type
, CCodeExpression expr
) {
4702 public override void visit_cast_expression (CastExpression expr
) {
4703 var valuecast
= try_cast_value_to_type (get_cvalue (expr
.inner
), expr
.inner
.value_type
, expr
.type_reference
, expr
);
4704 if (valuecast
!= null) {
4705 set_cvalue (expr
, valuecast
);
4709 var variantcast
= try_cast_variant_to_type (get_cvalue (expr
.inner
), expr
.inner
.value_type
, expr
.type_reference
, expr
);
4710 if (variantcast
!= null) {
4711 set_cvalue (expr
, variantcast
);
4715 generate_type_declaration (expr
.type_reference
, cfile
);
4717 var cl
= expr
.type_reference
.data_type as Class
;
4718 var iface
= expr
.type_reference
.data_type as Interface
;
4719 if (context
.profile
== Profile
.GOBJECT
&& (iface
!= null || (cl
!= null && !cl
.is_compact
))) {
4720 // checked cast for strict subtypes of GTypeInstance
4721 if (expr
.is_silent_cast
) {
4722 var temp_decl
= get_temp_variable (expr
.inner
.value_type
, expr
.inner
.value_type
.value_owned
, expr
, false);
4723 emit_temp_var (temp_decl
);
4724 var ctemp
= get_variable_cexpression (temp_decl
.name
);
4726 ccode
.add_assignment (ctemp
, get_cvalue (expr
.inner
));
4727 var ccheck
= create_type_check (ctemp
, expr
.type_reference
);
4728 var ccast
= new
CCodeCastExpression (ctemp
, expr
.type_reference
.get_cname ());
4729 var cnull
= new
CCodeConstant ("NULL");
4731 set_cvalue (expr
, new
CCodeConditionalExpression (ccheck
, ccast
, cnull
));
4733 set_cvalue (expr
, generate_instance_cast (get_cvalue (expr
.inner
), expr
.type_reference
.data_type
));
4736 if (expr
.is_silent_cast
) {
4738 Report
.error (expr
.source_reference
, "Operation not supported for this type");
4742 // retain array length
4743 var array_type
= expr
.type_reference as ArrayType
;
4744 if (array_type
!= null && expr
.inner
.value_type is ArrayType
) {
4745 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4746 append_array_length (expr
, get_array_length_cexpression (expr
.inner
, dim
));
4748 } else if (array_type
!= null) {
4749 // cast from non-array to array, set invalid length
4750 // required by string.data, e.g.
4751 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4752 append_array_length (expr
, new
CCodeConstant ("-1"));
4756 var innercexpr
= get_cvalue (expr
.inner
);
4757 if (expr
.type_reference
.data_type is Struct
&& !expr
.type_reference
.nullable
&&
4758 expr
.inner
.value_type
.data_type is Struct
&& expr
.inner
.value_type
.nullable
) {
4759 // nullable integer or float or boolean or struct cast to non-nullable
4760 innercexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, innercexpr
);
4762 set_cvalue (expr
, new
CCodeCastExpression (innercexpr
, expr
.type_reference
.get_cname ()));
4764 if (expr
.type_reference is DelegateType
) {
4765 if (get_delegate_target (expr
.inner
) != null) {
4766 set_delegate_target (expr
, get_delegate_target (expr
.inner
));
4768 set_delegate_target (expr
, new
CCodeConstant ("NULL"));
4770 if (get_delegate_target_destroy_notify (expr
.inner
) != null) {
4771 set_delegate_target_destroy_notify (expr
, get_delegate_target_destroy_notify (expr
.inner
));
4773 set_delegate_target_destroy_notify (expr
, new
CCodeConstant ("NULL"));
4779 public override void visit_named_argument (NamedArgument expr
) {
4780 set_cvalue (expr
, get_cvalue (expr
.inner
));
4783 public override void visit_pointer_indirection (PointerIndirection expr
) {
4784 set_cvalue (expr
, new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_cvalue (expr
.inner
)));
4787 public override void visit_addressof_expression (AddressofExpression expr
) {
4788 if (get_cvalue (expr
.inner
) is CCodeCommaExpression
) {
4789 var ccomma
= get_cvalue (expr
.inner
) as CCodeCommaExpression
;
4790 var inner
= ccomma
.get_inner ();
4791 var last
= inner
.get (inner
.size
- 1);
4792 ccomma
.set_expression (inner
.size
- 1, new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, (CCodeExpression
) last
));
4793 set_cvalue (expr
, ccomma
);
4795 set_cvalue (expr
, new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_cvalue (expr
.inner
)));
4799 public override void visit_reference_transfer_expression (ReferenceTransferExpression expr
) {
4800 /* (tmp = var, var = null, tmp) */
4801 var temp_decl
= get_temp_variable (expr
.value_type
, true, expr
, false);
4802 emit_temp_var (temp_decl
);
4803 var cvar
= get_variable_cexpression (temp_decl
.name
);
4805 ccode
.add_assignment (cvar
, get_cvalue (expr
.inner
));
4806 if (!(expr
.value_type is DelegateType
)) {
4807 ccode
.add_assignment (get_cvalue (expr
.inner
), new
CCodeConstant ("NULL"));
4810 set_cvalue (expr
, cvar
);
4812 var array_type
= expr
.value_type as ArrayType
;
4813 if (array_type
!= null) {
4814 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4815 append_array_length (expr
, get_array_length_cexpression (expr
.inner
, dim
));
4819 var delegate_type
= expr
.value_type as DelegateType
;
4820 if (delegate_type
!= null && delegate_type
.delegate_symbol
.has_target
) {
4821 var temp_target_decl
= get_temp_variable (new
PointerType (new
VoidType ()), true, expr
, false);
4822 emit_temp_var (temp_target_decl
);
4823 var target_cvar
= get_variable_cexpression (temp_target_decl
.name
);
4824 CCodeExpression target_destroy_notify
;
4825 var target
= get_delegate_target_cexpression (expr
.inner
, out target_destroy_notify
);
4826 ccode
.add_assignment (target_cvar
, target
);
4827 set_delegate_target (expr
, target_cvar
);
4828 if (target_destroy_notify
!= null) {
4829 var temp_target_destroy_notify_decl
= get_temp_variable (gdestroynotify_type
, true, expr
, false);
4830 emit_temp_var (temp_target_destroy_notify_decl
);
4831 var target_destroy_notify_cvar
= get_variable_cexpression (temp_target_destroy_notify_decl
.name
);
4832 ccode
.add_assignment (target_destroy_notify_cvar
, target_destroy_notify
);
4833 ccode
.add_assignment (target_destroy_notify
, new
CCodeConstant ("NULL"));
4834 set_delegate_target_destroy_notify (expr
, target_destroy_notify_cvar
);
4839 public override void visit_binary_expression (BinaryExpression expr
) {
4840 var cleft
= get_cvalue (expr
.left
);
4841 var cright
= get_cvalue (expr
.right
);
4843 CCodeExpression? left_chain
= null;
4845 var lbe
= (BinaryExpression
) expr
.left
;
4847 var temp_decl
= get_temp_variable (lbe
.right
.value_type
, true, null, false);
4848 emit_temp_var (temp_decl
);
4849 var cvar
= get_variable_cexpression (temp_decl
.name
);
4850 var ccomma
= new
CCodeCommaExpression ();
4851 var clbe
= (CCodeBinaryExpression
) get_cvalue (lbe
);
4853 clbe
= (CCodeBinaryExpression
) clbe
.right
;
4855 ccomma
.append_expression (new
CCodeAssignment (cvar
, get_cvalue (lbe
.right
)));
4856 clbe
.right
= get_variable_cexpression (temp_decl
.name
);
4857 ccomma
.append_expression (cleft
);
4859 left_chain
= ccomma
;
4862 CCodeBinaryOperator op
;
4863 if (expr
.operator
== BinaryOperator
.PLUS
) {
4864 op
= CCodeBinaryOperator
.PLUS
;
4865 } else if (expr
.operator
== BinaryOperator
.MINUS
) {
4866 op
= CCodeBinaryOperator
.MINUS
;
4867 } else if (expr
.operator
== BinaryOperator
.MUL
) {
4868 op
= CCodeBinaryOperator
.MUL
;
4869 } else if (expr
.operator
== BinaryOperator
.DIV
) {
4870 op
= CCodeBinaryOperator
.DIV
;
4871 } else if (expr
.operator
== BinaryOperator
.MOD
) {
4872 if (expr
.value_type
.equals (double_type
)) {
4873 cfile
.add_include ("math.h");
4874 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("fmod"));
4875 ccall
.add_argument (cleft
);
4876 ccall
.add_argument (cright
);
4877 set_cvalue (expr
, ccall
);
4879 } else if (expr
.value_type
.equals (float_type
)) {
4880 cfile
.add_include ("math.h");
4881 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("fmodf"));
4882 ccall
.add_argument (cleft
);
4883 ccall
.add_argument (cright
);
4884 set_cvalue (expr
, ccall
);
4887 op
= CCodeBinaryOperator
.MOD
;
4889 } else if (expr
.operator
== BinaryOperator
.SHIFT_LEFT
) {
4890 op
= CCodeBinaryOperator
.SHIFT_LEFT
;
4891 } else if (expr
.operator
== BinaryOperator
.SHIFT_RIGHT
) {
4892 op
= CCodeBinaryOperator
.SHIFT_RIGHT
;
4893 } else if (expr
.operator
== BinaryOperator
.LESS_THAN
) {
4894 op
= CCodeBinaryOperator
.LESS_THAN
;
4895 } else if (expr
.operator
== BinaryOperator
.GREATER_THAN
) {
4896 op
= CCodeBinaryOperator
.GREATER_THAN
;
4897 } else if (expr
.operator
== BinaryOperator
.LESS_THAN_OR_EQUAL
) {
4898 op
= CCodeBinaryOperator
.LESS_THAN_OR_EQUAL
;
4899 } else if (expr
.operator
== BinaryOperator
.GREATER_THAN_OR_EQUAL
) {
4900 op
= CCodeBinaryOperator
.GREATER_THAN_OR_EQUAL
;
4901 } else if (expr
.operator
== BinaryOperator
.EQUALITY
) {
4902 op
= CCodeBinaryOperator
.EQUALITY
;
4903 } else if (expr
.operator
== BinaryOperator
.INEQUALITY
) {
4904 op
= CCodeBinaryOperator
.INEQUALITY
;
4905 } else if (expr
.operator
== BinaryOperator
.BITWISE_AND
) {
4906 op
= CCodeBinaryOperator
.BITWISE_AND
;
4907 } else if (expr
.operator
== BinaryOperator
.BITWISE_OR
) {
4908 op
= CCodeBinaryOperator
.BITWISE_OR
;
4909 } else if (expr
.operator
== BinaryOperator
.BITWISE_XOR
) {
4910 op
= CCodeBinaryOperator
.BITWISE_XOR
;
4911 } else if (expr
.operator
== BinaryOperator
.AND
) {
4912 op
= CCodeBinaryOperator
.AND
;
4913 } else if (expr
.operator
== BinaryOperator
.OR
) {
4914 op
= CCodeBinaryOperator
.OR
;
4915 } else if (expr
.operator
== BinaryOperator
.IN
) {
4916 if (expr
.right
.value_type is ArrayType
) {
4917 var array_type
= (ArrayType
) expr
.right
.value_type
;
4918 var node
= new
CCodeFunctionCall (new
CCodeIdentifier (generate_array_contains_wrapper (array_type
)));
4919 node
.add_argument (cright
);
4920 node
.add_argument (get_array_length_cexpression (expr
.right
));
4921 if (array_type
.element_type is StructValueType
) {
4922 node
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cleft
));
4924 node
.add_argument (cleft
);
4926 set_cvalue (expr
, node
);
4928 set_cvalue (expr
, new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeBinaryExpression (CCodeBinaryOperator
.BITWISE_AND
, cright
, cleft
), cleft
));
4932 assert_not_reached ();
4935 if (expr
.operator
== BinaryOperator
.EQUALITY
||
4936 expr
.operator
== BinaryOperator
.INEQUALITY
) {
4937 var left_type
= expr
.left
.target_type
;
4938 var right_type
= expr
.right
.target_type
;
4939 make_comparable_cexpression (ref left_type
, ref cleft
, ref right_type
, ref cright
);
4941 if (left_type is StructValueType
&& right_type is StructValueType
) {
4942 var equalfunc
= generate_struct_equal_function ((Struct
) left_type
.data_type as Struct
);
4943 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
4944 ccall
.add_argument (cleft
);
4945 ccall
.add_argument (cright
);
4947 cright
= new
CCodeConstant ("TRUE");
4948 } else if ((left_type is IntegerType
|| left_type is FloatingType
|| left_type is BooleanType
) && left_type
.nullable
&&
4949 (right_type is IntegerType
|| right_type is FloatingType
|| right_type is BooleanType
) && right_type
.nullable
) {
4950 var equalfunc
= generate_numeric_equal_function ((Struct
) left_type
.data_type as Struct
);
4951 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
4952 ccall
.add_argument (cleft
);
4953 ccall
.add_argument (cright
);
4955 cright
= new
CCodeConstant ("TRUE");
4959 if (!(expr
.left
.value_type is NullType
)
4960 && expr
.left
.value_type
.compatible (string_type
)
4961 && !(expr
.right
.value_type is NullType
)
4962 && expr
.right
.value_type
.compatible (string_type
)) {
4963 if (expr
.operator
== BinaryOperator
.PLUS
) {
4964 // string concatenation
4965 if (expr
.left
.is_constant () && expr
.right
.is_constant ()) {
4968 if (cleft is CCodeIdentifier
) {
4969 left
= ((CCodeIdentifier
) cleft
).name
;
4970 } else if (cleft is CCodeConstant
) {
4971 left
= ((CCodeConstant
) cleft
).name
;
4973 assert_not_reached ();
4975 if (cright is CCodeIdentifier
) {
4976 right
= ((CCodeIdentifier
) cright
).name
;
4977 } else if (cright is CCodeConstant
) {
4978 right
= ((CCodeConstant
) cright
).name
;
4980 assert_not_reached ();
4983 set_cvalue (expr
, new
CCodeConstant ("%s %s".printf (left
, right
)));
4986 if (context
.profile
== Profile
.POSIX
) {
4987 // convert to strcat(strcpy(malloc(1+strlen(a)+strlen(b)),a),b)
4988 var strcat
= new
CCodeFunctionCall (new
CCodeIdentifier ("strcat"));
4989 var strcpy
= new
CCodeFunctionCall (new
CCodeIdentifier ("strcpy"));
4990 var malloc
= new
CCodeFunctionCall (new
CCodeIdentifier ("malloc"));
4992 var strlen_a
= new
CCodeFunctionCall (new
CCodeIdentifier ("strlen"));
4993 strlen_a
.add_argument(cleft
);
4994 var strlen_b
= new
CCodeFunctionCall (new
CCodeIdentifier ("strlen"));
4995 strlen_b
.add_argument(cright
);
4996 var newlength
= new
CCodeBinaryExpression (CCodeBinaryOperator
.PLUS
, new
CCodeIdentifier("1"),
4997 new
CCodeBinaryExpression (CCodeBinaryOperator
.PLUS
, strlen_a
, strlen_b
));
4998 malloc
.add_argument(newlength
);
5000 strcpy
.add_argument(malloc
);
5001 strcpy
.add_argument(cleft
);
5003 strcat
.add_argument(strcpy
);
5004 strcat
.add_argument(cright
);
5005 set_cvalue (expr
, strcat
);
5007 // convert to g_strconcat (a, b, NULL)
5008 var temp_var
= get_temp_variable (expr
.value_type
, true, null, false);
5009 var temp_ref
= get_variable_cexpression (temp_var
.name
);
5010 emit_temp_var (temp_var
);
5012 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strconcat"));
5013 ccall
.add_argument (cleft
);
5014 ccall
.add_argument (cright
);
5015 ccall
.add_argument (new
CCodeConstant("NULL"));
5017 ccode
.add_assignment (temp_ref
, ccall
);
5018 set_cvalue (expr
, temp_ref
);
5022 } else if (expr
.operator
== BinaryOperator
.EQUALITY
5023 || expr
.operator
== BinaryOperator
.INEQUALITY
5024 || expr
.operator
== BinaryOperator
.LESS_THAN
5025 || expr
.operator
== BinaryOperator
.GREATER_THAN
5026 || expr
.operator
== BinaryOperator
.LESS_THAN_OR_EQUAL
5027 || expr
.operator
== BinaryOperator
.GREATER_THAN_OR_EQUAL
) {
5028 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strcmp0"));
5029 ccall
.add_argument (cleft
);
5030 ccall
.add_argument (cright
);
5032 cright
= new
CCodeConstant ("0");
5036 set_cvalue (expr
, new
CCodeBinaryExpression (op
, cleft
, cright
));
5037 if (left_chain
!= null) {
5038 set_cvalue (expr
, new
CCodeBinaryExpression (CCodeBinaryOperator
.AND
, left_chain
, get_cvalue (expr
)));
5042 public string?
get_type_check_function (TypeSymbol type
) {
5043 var cl
= type as Class
;
5044 if (cl
!= null && cl
.type_check_function
!= null) {
5045 return cl
.type_check_function
;
5046 } else if ((cl
!= null && cl
.is_compact
) || type is Struct
|| type is Enum
|| type is Delegate
) {
5049 return type
.get_upper_case_cname ("IS_");
5053 CCodeExpression?
create_type_check (CCodeNode ccodenode
, DataType type
) {
5054 var et
= type as ErrorType
;
5055 if (et
!= null && et
.error_code
!= null) {
5056 var matches_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_error_matches"));
5057 matches_call
.add_argument ((CCodeExpression
) ccodenode
);
5058 matches_call
.add_argument (new
CCodeIdentifier (et
.error_domain
.get_upper_case_cname ()));
5059 matches_call
.add_argument (new
CCodeIdentifier (et
.error_code
.get_cname ()));
5060 return matches_call
;
5061 } else if (et
!= null && et
.error_domain
!= null) {
5062 var instance_domain
= new CCodeMemberAccess
.pointer ((CCodeExpression
) ccodenode
, "domain");
5063 var type_domain
= new
CCodeIdentifier (et
.error_domain
.get_upper_case_cname ());
5064 return new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, instance_domain
, type_domain
);
5066 string type_check_func
= get_type_check_function (type
.data_type
);
5067 if (type_check_func
== null) {
5068 return new
CCodeInvalidExpression ();
5070 var ccheck
= new
CCodeFunctionCall (new
CCodeIdentifier (type_check_func
));
5071 ccheck
.add_argument ((CCodeExpression
) ccodenode
);
5076 string generate_array_contains_wrapper (ArrayType array_type
) {
5077 string array_contains_func
= "_vala_%s_array_contains".printf (array_type
.element_type
.get_lower_case_cname ());
5079 if (!add_wrapper (array_contains_func
)) {
5080 return array_contains_func
;
5083 var function
= new
CCodeFunction (array_contains_func
, "gboolean");
5084 function
.modifiers
= CCodeModifiers
.STATIC
;
5086 function
.add_parameter (new
CCodeParameter ("stack", array_type
.get_cname ()));
5087 function
.add_parameter (new
CCodeParameter ("stack_length", "int"));
5088 if (array_type
.element_type is StructValueType
) {
5089 function
.add_parameter (new
CCodeParameter ("needle", array_type
.element_type
.get_cname () + "*"));
5091 function
.add_parameter (new
CCodeParameter ("needle", array_type
.element_type
.get_cname ()));
5094 push_function (function
);
5096 ccode
.add_declaration ("int", new
CCodeVariableDeclarator ("i"));
5098 var cloop_initializer
= new
CCodeAssignment (new
CCodeIdentifier ("i"), new
CCodeConstant ("0"));
5099 var cloop_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.LESS_THAN
, new
CCodeIdentifier ("i"), new
CCodeIdentifier ("stack_length"));
5100 var cloop_iterator
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POSTFIX_INCREMENT
, new
CCodeIdentifier ("i"));
5101 ccode
.open_for (cloop_initializer
, cloop_condition
, cloop_iterator
);
5103 var celement
= new
CCodeElementAccess (new
CCodeIdentifier ("stack"), new
CCodeIdentifier ("i"));
5104 var cneedle
= new
CCodeIdentifier ("needle");
5105 CCodeBinaryExpression cif_condition
;
5106 if (array_type
.element_type
.compatible (string_type
)) {
5107 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strcmp0"));
5108 ccall
.add_argument (celement
);
5109 ccall
.add_argument (cneedle
);
5110 cif_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, ccall
, new
CCodeConstant ("0"));
5111 } else if (array_type
.element_type is StructValueType
) {
5112 var equalfunc
= generate_struct_equal_function ((Struct
) array_type
.element_type
.data_type as Struct
);
5113 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
5114 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, celement
));
5115 ccall
.add_argument (cneedle
);
5116 cif_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, ccall
, new
CCodeConstant ("TRUE"));
5118 cif_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, cneedle
, celement
);
5121 ccode
.open_if (cif_condition
);
5122 ccode
.add_return (new
CCodeConstant ("TRUE"));
5127 ccode
.add_return (new
CCodeConstant ("FALSE"));
5131 cfile
.add_function_declaration (function
);
5132 cfile
.add_function (function
);
5134 return array_contains_func
;
5137 public override void visit_type_check (TypeCheck expr
) {
5138 generate_type_declaration (expr
.type_reference
, cfile
);
5140 set_cvalue (expr
, create_type_check (get_cvalue (expr
.expression
), expr
.type_reference
));
5141 if (get_cvalue (expr
) is CCodeInvalidExpression
) {
5142 Report
.error (expr
.source_reference
, "type check expressions not supported for compact classes, structs, and enums");
5146 public override void visit_lambda_expression (LambdaExpression lambda
) {
5147 // use instance position from delegate
5148 var dt
= (DelegateType
) lambda
.target_type
;
5149 lambda
.method
.cinstance_parameter_position
= dt
.delegate_symbol
.cinstance_parameter_position
;
5151 lambda
.accept_children (this
);
5153 bool expr_owned
= lambda
.value_type
.value_owned
;
5155 set_cvalue (lambda
, new
CCodeIdentifier (lambda
.method
.get_cname ()));
5157 var delegate_type
= (DelegateType
) lambda
.target_type
;
5158 if (lambda
.method
.closure
) {
5159 int block_id
= get_block_id (current_closure_block
);
5160 var delegate_target
= get_variable_cexpression ("_data%d_".printf (block_id
));
5161 if (expr_owned
|| delegate_type
.is_called_once
) {
5162 var ref_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_ref".printf (block_id
)));
5163 ref_call
.add_argument (delegate_target
);
5164 delegate_target
= ref_call
;
5165 set_delegate_target_destroy_notify (lambda
, new
CCodeIdentifier ("block%d_data_unref".printf (block_id
)));
5167 set_delegate_target_destroy_notify (lambda
, new
CCodeConstant ("NULL"));
5169 set_delegate_target (lambda
, delegate_target
);
5170 } else if (get_this_type () != null || in_constructor
) {
5171 CCodeExpression delegate_target
= get_result_cexpression ("self");
5172 if (expr_owned
|| delegate_type
.is_called_once
) {
5173 if (get_this_type () != null) {
5174 var ref_call
= new
CCodeFunctionCall (get_dup_func_expression (get_this_type (), lambda
.source_reference
));
5175 ref_call
.add_argument (delegate_target
);
5176 delegate_target
= ref_call
;
5177 set_delegate_target_destroy_notify (lambda
, get_destroy_func_expression (get_this_type ()));
5180 var ref_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_object_ref"));
5181 ref_call
.add_argument (delegate_target
);
5182 delegate_target
= ref_call
;
5183 set_delegate_target_destroy_notify (lambda
, new
CCodeIdentifier ("g_object_unref"));
5186 set_delegate_target_destroy_notify (lambda
, new
CCodeConstant ("NULL"));
5188 set_delegate_target (lambda
, delegate_target
);
5190 set_delegate_target (lambda
, new
CCodeConstant ("NULL"));
5191 set_delegate_target_destroy_notify (lambda
, new
CCodeConstant ("NULL"));
5195 public CCodeExpression
convert_from_generic_pointer (CCodeExpression cexpr
, DataType actual_type
) {
5197 if (is_reference_type_argument (actual_type
) || is_nullable_value_type_argument (actual_type
)) {
5198 result
= new
CCodeCastExpression (cexpr
, actual_type
.get_cname ());
5199 } else if (is_signed_integer_type_argument (actual_type
)) {
5200 var cconv
= new
CCodeFunctionCall (new
CCodeIdentifier ("GPOINTER_TO_INT"));
5201 cconv
.add_argument (cexpr
);
5203 } else if (is_unsigned_integer_type_argument (actual_type
)) {
5204 var cconv
= new
CCodeFunctionCall (new
CCodeIdentifier ("GPOINTER_TO_UINT"));
5205 cconv
.add_argument (cexpr
);
5211 public CCodeExpression
convert_to_generic_pointer (CCodeExpression cexpr
, DataType actual_type
) {
5213 if (is_signed_integer_type_argument (actual_type
)) {
5214 var cconv
= new
CCodeFunctionCall (new
CCodeIdentifier ("GINT_TO_POINTER"));
5215 cconv
.add_argument (cexpr
);
5217 } else if (is_unsigned_integer_type_argument (actual_type
)) {
5218 var cconv
= new
CCodeFunctionCall (new
CCodeIdentifier ("GUINT_TO_POINTER"));
5219 cconv
.add_argument (cexpr
);
5225 // manage memory and implicit casts
5226 public CCodeExpression
transform_expression (CCodeExpression source_cexpr
, DataType? expression_type
, DataType? target_type
, Expression? expr
= null) {
5227 var cexpr
= source_cexpr
;
5228 if (expression_type
== null) {
5233 if (expression_type
.value_owned
5234 && expression_type
.floating_reference
) {
5235 /* floating reference, sink it.
5237 var cl
= expression_type
.data_type as ObjectTypeSymbol
;
5238 var sink_func
= (cl
!= null) ? cl
.get_ref_sink_function () : null;
5240 if (sink_func
!= null) {
5241 var csink
= new
CCodeFunctionCall (new
CCodeIdentifier (sink_func
));
5242 csink
.add_argument (cexpr
);
5246 Report
.error (null, "type `%s' does not support floating references".printf (expression_type
.data_type
.name
));
5250 bool boxing
= (expression_type is ValueType
&& !expression_type
.nullable
5251 && target_type is ValueType
&& target_type
.nullable
);
5252 bool unboxing
= (expression_type is ValueType
&& expression_type
.nullable
5253 && target_type is ValueType
&& !target_type
.nullable
);
5255 bool gvalue_boxing
= (context
.profile
== Profile
.GOBJECT
5256 && target_type
!= null
5257 && target_type
.data_type
== gvalue_type
5258 && !(expression_type is NullType
)
5259 && expression_type
.get_type_id () != "G_TYPE_VALUE");
5260 bool gvariant_boxing
= (context
.profile
== Profile
.GOBJECT
5261 && target_type
!= null
5262 && target_type
.data_type
== gvariant_type
5263 && !(expression_type is NullType
)
5264 && expression_type
.data_type
!= gvariant_type
);
5266 if (expression_type
.value_owned
5267 && (target_type
== null || !target_type
.value_owned
|| boxing
|| unboxing
)
5268 && !gvalue_boxing
/* gvalue can assume ownership of value, no need to free it */) {
5269 // value leaked, destroy it
5270 var pointer_type
= target_type as PointerType
;
5271 if (pointer_type
!= null && !(pointer_type
.base_type is VoidType
)) {
5272 // manual memory management for non-void pointers
5273 // treat void* special to not leak memory with void* method parameters
5274 } else if (requires_destroy (expression_type
)) {
5275 var decl
= get_temp_variable (expression_type
, true, expression_type
, false);
5276 emit_temp_var (decl
);
5277 temp_ref_vars
.insert (0, decl
);
5278 ccode
.add_assignment (get_variable_cexpression (decl
.name
), cexpr
);
5279 cexpr
= get_variable_cexpression (decl
.name
);
5281 if (expression_type is ArrayType
&& expr
!= null) {
5282 var array_type
= (ArrayType
) expression_type
;
5283 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5284 var len_decl
= new
LocalVariable (int_type
.copy (), get_array_length_cname (decl
.name
, dim
));
5285 emit_temp_var (len_decl
);
5286 ccode
.add_assignment (get_variable_cexpression (len_decl
.name
), get_array_length_cexpression (expr
, dim
));
5288 } else if (expression_type is DelegateType
&& expr
!= null) {
5289 var target_decl
= new
LocalVariable (new
PointerType (new
VoidType ()), get_delegate_target_cname (decl
.name
));
5290 emit_temp_var (target_decl
);
5291 var target_destroy_notify_decl
= new
LocalVariable (gdestroynotify_type
, get_delegate_target_destroy_notify_cname (decl
.name
));
5292 emit_temp_var (target_destroy_notify_decl
);
5293 CCodeExpression target_destroy_notify
;
5294 ccode
.add_assignment (get_variable_cexpression (target_decl
.name
), get_delegate_target_cexpression (expr
, out target_destroy_notify
));
5295 ccode
.add_assignment (get_variable_cexpression (target_destroy_notify_decl
.name
), target_destroy_notify
);
5301 if (target_type
== null) {
5302 // value will be destroyed, no need for implicit casts
5306 if (gvalue_boxing
) {
5307 // implicit conversion to GValue
5308 var decl
= get_temp_variable (target_type
, true, target_type
);
5309 emit_temp_var (decl
);
5311 if (!target_type
.value_owned
) {
5312 // boxed GValue leaked, destroy it
5313 temp_ref_vars
.insert (0, decl
);
5316 if (target_type
.nullable
) {
5317 var newcall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_new0"));
5318 newcall
.add_argument (new
CCodeConstant ("GValue"));
5319 newcall
.add_argument (new
CCodeConstant ("1"));
5320 var newassignment
= new
CCodeAssignment (get_variable_cexpression (decl
.name
), newcall
);
5321 ccode
.add_expression (newassignment
);
5324 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_value_init"));
5325 if (target_type
.nullable
) {
5326 ccall
.add_argument (get_variable_cexpression (decl
.name
));
5328 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (decl
.name
)));
5330 ccall
.add_argument (new
CCodeIdentifier (expression_type
.get_type_id ()));
5331 ccode
.add_expression (ccall
);
5333 if (requires_destroy (expression_type
)) {
5334 ccall
= new
CCodeFunctionCall (get_value_taker_function (expression_type
));
5336 ccall
= new
CCodeFunctionCall (get_value_setter_function (expression_type
));
5338 if (target_type
.nullable
) {
5339 ccall
.add_argument (get_variable_cexpression (decl
.name
));
5341 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (decl
.name
)));
5343 if (expression_type
.is_real_non_null_struct_type ()) {
5344 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
));
5346 ccall
.add_argument (cexpr
);
5349 ccode
.add_expression (ccall
);
5351 cexpr
= get_variable_cexpression (decl
.name
);
5354 } else if (gvariant_boxing
) {
5355 // implicit conversion to GVariant
5356 string variant_func
= "_variant_new%d".printf (++next_variant_function_id
);
5358 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (variant_func
));
5359 ccall
.add_argument (cexpr
);
5361 var cfunc
= new
CCodeFunction (variant_func
, "GVariant*");
5362 cfunc
.modifiers
= CCodeModifiers
.STATIC
;
5363 cfunc
.add_parameter (new
CCodeParameter ("value", expression_type
.get_cname ()));
5365 if (expression_type is ArrayType
) {
5366 // return array length if appropriate
5367 var array_type
= (ArrayType
) expression_type
;
5369 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5370 ccall
.add_argument (get_array_length_cexpression (expr
, dim
));
5371 cfunc
.add_parameter (new
CCodeParameter (get_array_length_cname ("value", dim
), "gint"));
5375 push_function (cfunc
);
5377 var result
= serialize_expression (expression_type
, new
CCodeIdentifier ("value"));
5379 // sink floating reference
5380 var sink
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_variant_ref_sink"));
5381 sink
.add_argument (result
);
5382 ccode
.add_return (sink
);
5386 cfile
.add_function_declaration (cfunc
);
5387 cfile
.add_function (cfunc
);
5390 } else if (boxing
) {
5391 // value needs to be boxed
5393 var unary
= cexpr as CCodeUnaryExpression
;
5394 if (unary
!= null && unary
.operator
== CCodeUnaryOperator
.POINTER_INDIRECTION
) {
5396 cexpr
= unary
.inner
;
5397 } else if (cexpr is CCodeIdentifier
|| cexpr is CCodeMemberAccess
) {
5398 cexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
);
5400 var decl
= get_temp_variable (expression_type
, expression_type
.value_owned
, expression_type
, false);
5401 emit_temp_var (decl
);
5403 ccode
.add_assignment (get_variable_cexpression (decl
.name
), cexpr
);
5404 cexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (decl
.name
));
5406 } else if (unboxing
) {
5409 cexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, cexpr
);
5411 cexpr
= get_implicit_cast_expression (cexpr
, expression_type
, target_type
, expr
);
5414 if (target_type
.value_owned
&& (!expression_type
.value_owned
|| boxing
|| unboxing
)) {
5415 // need to copy value
5416 if (requires_copy (target_type
) && !(expression_type is NullType
)) {
5417 CodeNode node
= expr
;
5419 node
= expression_type
;
5422 var decl
= get_temp_variable (target_type
, true, node
, false);
5423 emit_temp_var (decl
);
5424 ccode
.add_assignment (get_variable_cexpression (decl
.name
), get_ref_cexpression (target_type
, cexpr
, expr
, node
));
5425 cexpr
= get_variable_cexpression (decl
.name
);
5432 public virtual CCodeExpression
get_implicit_cast_expression (CCodeExpression source_cexpr
, DataType? expression_type
, DataType? target_type
, Expression? expr
= null) {
5433 var cexpr
= source_cexpr
;
5435 if (expression_type
.data_type
!= null && expression_type
.data_type
== target_type
.data_type
) {
5436 // same type, no cast required
5440 if (expression_type is NullType
) {
5441 // null literal, no cast required when not converting to generic type pointer
5445 generate_type_declaration (target_type
, cfile
);
5447 var cl
= target_type
.data_type as Class
;
5448 var iface
= target_type
.data_type as Interface
;
5449 if (context
.checking
&& (iface
!= null || (cl
!= null && !cl
.is_compact
))) {
5450 // checked cast for strict subtypes of GTypeInstance
5451 return generate_instance_cast (cexpr
, target_type
.data_type
);
5452 } else if (target_type
.data_type
!= null && expression_type
.get_cname () != target_type
.get_cname ()) {
5453 var st
= target_type
.data_type as Struct
;
5454 if (target_type
.data_type
.is_reference_type () || (st
!= null && st
.is_simple_type ())) {
5455 // don't cast non-simple structs
5456 return new
CCodeCastExpression (cexpr
, target_type
.get_cname ());
5465 public void store_property (Property prop
, Expression? instance
, TargetValue value
) {
5466 if (instance is BaseAccess
) {
5467 if (prop
.base_property
!= null) {
5468 var base_class
= (Class
) prop
.base_property
.parent_symbol
;
5469 var vcast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_CLASS".printf (base_class
.get_upper_case_cname (null))));
5470 vcast
.add_argument (new
CCodeIdentifier ("%s_parent_class".printf (current_class
.get_lower_case_cname (null))));
5472 var ccall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (vcast
, "set_%s".printf (prop
.name
)));
5473 ccall
.add_argument ((CCodeExpression
) get_ccodenode (instance
));
5474 ccall
.add_argument (get_cvalue_ (value
));
5476 ccode
.add_expression (ccall
);
5477 } else if (prop
.base_interface_property
!= null) {
5478 var base_iface
= (Interface
) prop
.base_interface_property
.parent_symbol
;
5479 string parent_iface_var
= "%s_%s_parent_iface".printf (current_class
.get_lower_case_cname (null), base_iface
.get_lower_case_cname (null));
5481 var ccall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (new
CCodeIdentifier (parent_iface_var
), "set_%s".printf (prop
.name
)));
5482 ccall
.add_argument ((CCodeExpression
) get_ccodenode (instance
));
5483 ccall
.add_argument (get_cvalue_ (value
));
5485 ccode
.add_expression (ccall
);
5490 var set_func
= "g_object_set";
5492 var base_property
= prop
;
5493 if (!prop
.no_accessor_method
) {
5494 if (prop
.base_property
!= null) {
5495 base_property
= prop
.base_property
;
5496 } else if (prop
.base_interface_property
!= null) {
5497 base_property
= prop
.base_interface_property
;
5500 if (prop is DynamicProperty
) {
5501 set_func
= get_dynamic_property_setter_cname ((DynamicProperty
) prop
);
5503 generate_property_accessor_declaration (base_property
.set_accessor
, cfile
);
5504 set_func
= base_property
.set_accessor
.get_cname ();
5506 if (!prop
.external
&& prop
.external_package
) {
5507 // internal VAPI properties
5508 // only add them once per source file
5509 if (add_generated_external_symbol (prop
)) {
5510 visit_property (prop
);
5516 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (set_func
));
5518 if (prop
.binding
== MemberBinding
.INSTANCE
) {
5519 /* target instance is first argument */
5520 var cinstance
= (CCodeExpression
) get_ccodenode (instance
);
5522 if (prop
.parent_symbol is Struct
) {
5523 // we need to pass struct instance by reference
5524 var unary
= cinstance as CCodeUnaryExpression
;
5525 if (unary
!= null && unary
.operator
== CCodeUnaryOperator
.POINTER_INDIRECTION
) {
5527 cinstance
= unary
.inner
;
5528 } else if (cinstance is CCodeIdentifier
|| cinstance is CCodeMemberAccess
) {
5529 cinstance
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cinstance
);
5531 // if instance is e.g. a function call, we can't take the address of the expression
5532 // (tmp = expr, &tmp)
5534 var temp_var
= get_temp_variable (instance
.target_type
, true, null, false);
5535 emit_temp_var (temp_var
);
5536 ccode
.add_assignment (get_variable_cexpression (temp_var
.name
), cinstance
);
5538 cinstance
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (temp_var
.name
));
5542 ccall
.add_argument (cinstance
);
5545 if (prop
.no_accessor_method
) {
5546 /* property name is second argument of g_object_set */
5547 ccall
.add_argument (prop
.get_canonical_cconstant ());
5550 var cexpr
= get_cvalue_ (value
);
5552 if (prop
.property_type
.is_real_non_null_struct_type ()) {
5553 cexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
);
5556 var array_type
= prop
.property_type as ArrayType
;
5558 if (array_type
!= null && !prop
.no_array_length
) {
5559 var temp_var
= get_temp_variable (prop
.property_type
, true, null, false);
5560 emit_temp_var (temp_var
);
5561 ccode
.add_assignment (get_variable_cexpression (temp_var
.name
), cexpr
);
5562 ccall
.add_argument (get_variable_cexpression (temp_var
.name
));
5564 ccall
.add_argument (cexpr
);
5567 if (array_type
!= null && !prop
.no_array_length
) {
5568 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5569 ccall
.add_argument (get_array_length_cvalue (value
, dim
));
5571 } else if (prop
.property_type is DelegateType
) {
5572 var delegate_type
= (DelegateType
) prop
.property_type
;
5573 if (delegate_type
.delegate_symbol
.has_target
) {
5574 ccall
.add_argument (get_delegate_target_cvalue (value
));
5578 if (prop
.no_accessor_method
) {
5579 ccall
.add_argument (new
CCodeConstant ("NULL"));
5582 ccode
.add_expression (ccall
);
5585 public bool add_wrapper (string wrapper_name
) {
5586 return wrappers
.add (wrapper_name
);
5589 public bool add_generated_external_symbol (Symbol external_symbol
) {
5590 return generated_external_symbols
.add (external_symbol
);
5593 public static DataType
get_data_type_for_symbol (TypeSymbol sym
) {
5594 DataType type
= null;
5597 type
= new
ObjectType ((Class
) sym
);
5598 } else if (sym is Interface
) {
5599 type
= new
ObjectType ((Interface
) sym
);
5600 } else if (sym is Struct
) {
5601 var st
= (Struct
) sym
;
5602 if (st
.is_boolean_type ()) {
5603 type
= new
BooleanType (st
);
5604 } else if (st
.is_integer_type ()) {
5605 type
= new
IntegerType (st
);
5606 } else if (st
.is_floating_type ()) {
5607 type
= new
FloatingType (st
);
5609 type
= new
StructValueType (st
);
5611 } else if (sym is Enum
) {
5612 type
= new
EnumValueType ((Enum
) sym
);
5613 } else if (sym is ErrorDomain
) {
5614 type
= new
ErrorType ((ErrorDomain
) sym
, null);
5615 } else if (sym is ErrorCode
) {
5616 type
= new
ErrorType ((ErrorDomain
) sym
.parent_symbol
, (ErrorCode
) sym
);
5618 Report
.error (null, "internal error: `%s' is not a supported type".printf (sym
.get_full_name ()));
5619 return new
InvalidType ();
5625 public CCodeExpression?
default_value_for_type (DataType type
, bool initializer_expression
) {
5626 var st
= type
.data_type as Struct
;
5627 var array_type
= type as ArrayType
;
5628 if (initializer_expression
&& !type
.nullable
&&
5629 ((st
!= null && !st
.is_simple_type ()) ||
5630 (array_type
!= null && array_type
.fixed_length
))) {
5631 // 0-initialize struct with struct initializer { 0 }
5632 // only allowed as initializer expression in C
5633 var clist
= new
CCodeInitializerList ();
5634 clist
.append (new
CCodeConstant ("0"));
5636 } else if ((type
.data_type
!= null && type
.data_type
.is_reference_type ())
5638 || type is PointerType
|| type is DelegateType
5639 || (array_type
!= null && !array_type
.fixed_length
)) {
5640 return new
CCodeConstant ("NULL");
5641 } else if (type
.data_type
!= null && type
.data_type
.get_default_value () != null) {
5642 return new
CCodeConstant (type
.data_type
.get_default_value ());
5643 } else if (type
.type_parameter
!= null) {
5644 return new
CCodeConstant ("NULL");
5645 } else if (type is ErrorType
) {
5646 return new
CCodeConstant ("NULL");
5651 private void create_property_type_check_statement (Property prop
, bool check_return_type
, TypeSymbol t
, bool non_null
, string var_name
) {
5652 if (check_return_type
) {
5653 create_type_check_statement (prop
, prop
.property_type
, t
, non_null
, var_name
);
5655 create_type_check_statement (prop
, new
VoidType (), t
, non_null
, var_name
);
5659 public void create_type_check_statement (CodeNode method_node
, DataType ret_type
, TypeSymbol t
, bool non_null
, string var_name
) {
5660 var ccheck
= new
CCodeFunctionCall ();
5662 if (!context
.assert
) {
5664 } else if (context
.checking
&& ((t is Class
&& !((Class
) t
).is_compact
) || t is Interface
)) {
5665 var ctype_check
= new
CCodeFunctionCall (new
CCodeIdentifier (get_type_check_function (t
)));
5666 ctype_check
.add_argument (new
CCodeIdentifier (var_name
));
5668 CCodeExpression cexpr
= ctype_check
;
5670 var cnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier (var_name
), new
CCodeConstant ("NULL"));
5672 cexpr
= new
CCodeBinaryExpression (CCodeBinaryOperator
.OR
, cnull
, ctype_check
);
5674 ccheck
.add_argument (cexpr
);
5675 } else if (!non_null
) {
5677 } else if (t
== glist_type
|| t
== gslist_type
) {
5678 // NULL is empty list
5681 var cnonnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, new
CCodeIdentifier (var_name
), new
CCodeConstant ("NULL"));
5682 ccheck
.add_argument (cnonnull
);
5685 var cm
= method_node as CreationMethod
;
5686 if (cm
!= null && cm
.parent_symbol is ObjectTypeSymbol
) {
5687 ccheck
.call
= new
CCodeIdentifier ("g_return_val_if_fail");
5688 ccheck
.add_argument (new
CCodeConstant ("NULL"));
5689 } else if (ret_type is VoidType
) {
5691 ccheck
.call
= new
CCodeIdentifier ("g_return_if_fail");
5693 ccheck
.call
= new
CCodeIdentifier ("g_return_val_if_fail");
5695 var cdefault
= default_value_for_type (ret_type
, false);
5696 if (cdefault
!= null) {
5697 ccheck
.add_argument (cdefault
);
5703 ccode
.add_expression (ccheck
);
5706 public int get_param_pos (double param_pos
, bool ellipsis
= false) {
5708 if (param_pos
>= 0) {
5709 return (int) (param_pos
* 1000);
5711 return (int) ((100 + param_pos
) * 1000);
5714 if (param_pos
>= 0) {
5715 return (int) ((100 + param_pos
) * 1000);
5717 return (int) ((200 + param_pos
) * 1000);
5722 public CCodeExpression?
get_ccodenode (Expression node
) {
5723 if (get_cvalue (node
) == null) {
5726 return get_cvalue (node
);
5729 public override void visit_class (Class cl
) {
5732 public void create_postcondition_statement (Expression postcondition
) {
5733 var cassert
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_warn_if_fail"));
5735 postcondition
.emit (this
);
5737 cassert
.add_argument (get_cvalue (postcondition
));
5739 ccode
.add_expression (cassert
);
5742 public virtual bool is_gobject_property (Property prop
) {
5746 public DataType?
get_this_type () {
5747 if (current_method
!= null && current_method
.binding
== MemberBinding
.INSTANCE
) {
5748 return current_method
.this_parameter
.variable_type
;
5749 } else if (current_property_accessor
!= null && current_property_accessor
.prop
.binding
== MemberBinding
.INSTANCE
) {
5750 return current_property_accessor
.prop
.this_parameter
.variable_type
;
5755 public CCodeFunctionCall
generate_instance_cast (CCodeExpression expr
, TypeSymbol type
) {
5756 var result
= new
CCodeFunctionCall (new
CCodeIdentifier (type
.get_upper_case_cname (null)));
5757 result
.add_argument (expr
);
5761 void generate_struct_destroy_function (Struct st
) {
5762 if (cfile
.add_declaration (st
.get_destroy_function ())) {
5763 // only generate function once per source file
5767 var function
= new
CCodeFunction (st
.get_destroy_function (), "void");
5768 function
.modifiers
= CCodeModifiers
.STATIC
;
5769 function
.add_parameter (new
CCodeParameter ("self", st
.get_cname () + "*"));
5771 push_function (function
);
5773 foreach (Field f
in st
.get_fields ()) {
5774 if (f
.binding
== MemberBinding
.INSTANCE
) {
5775 if (requires_destroy (f
.variable_type
)) {
5776 var this_access
= new MemberAccess
.simple ("this");
5777 this_access
.value_type
= get_data_type_for_symbol ((TypeSymbol
) f
.parent_symbol
);
5778 set_cvalue (this_access
, new
CCodeIdentifier ("(*self)"));
5780 ccode
.add_expression (destroy_field (f
, this_access
));
5787 cfile
.add_function_declaration (function
);
5788 cfile
.add_function (function
);
5791 void generate_struct_copy_function (Struct st
) {
5792 if (cfile
.add_declaration (st
.get_copy_function ())) {
5793 // only generate function once per source file
5797 var function
= new
CCodeFunction (st
.get_copy_function (), "void");
5798 function
.modifiers
= CCodeModifiers
.STATIC
;
5799 function
.add_parameter (new
CCodeParameter ("self", "const " + st
.get_cname () + "*"));
5800 function
.add_parameter (new
CCodeParameter ("dest", st
.get_cname () + "*"));
5802 push_context (new
EmitContext ());
5803 push_function (function
);
5805 foreach (Field f
in st
.get_fields ()) {
5806 if (f
.binding
== MemberBinding
.INSTANCE
) {
5807 CCodeExpression copy
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("self"), f
.name
);
5808 if (requires_copy (f
.variable_type
)) {
5809 var this_access
= new MemberAccess
.simple ("this");
5810 this_access
.value_type
= get_data_type_for_symbol ((TypeSymbol
) f
.parent_symbol
);
5811 set_cvalue (this_access
, new
CCodeIdentifier ("(*self)"));
5812 var ma
= new
MemberAccess (this_access
, f
.name
);
5813 ma
.symbol_reference
= f
;
5814 ma
.value_type
= f
.variable_type
.copy ();
5815 visit_member_access (ma
);
5816 copy
= get_ref_cexpression (f
.variable_type
, copy
, ma
, f
);
5818 var dest
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("dest"), f
.name
);
5820 var array_type
= f
.variable_type as ArrayType
;
5821 if (array_type
!= null && array_type
.fixed_length
) {
5822 // fixed-length (stack-allocated) arrays
5823 cfile
.add_include ("string.h");
5825 var sizeof_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
5826 sizeof_call
.add_argument (new
CCodeIdentifier (array_type
.element_type
.get_cname ()));
5827 var size
= new
CCodeBinaryExpression (CCodeBinaryOperator
.MUL
, new
CCodeConstant ("%d".printf (array_type
.length
)), sizeof_call
);
5829 var array_copy_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memcpy"));
5830 array_copy_call
.add_argument (dest
);
5831 array_copy_call
.add_argument (copy
);
5832 array_copy_call
.add_argument (size
);
5833 ccode
.add_expression (array_copy_call
);
5835 ccode
.add_assignment (dest
, copy
);
5837 if (array_type
!= null && !f
.no_array_length
) {
5838 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5839 var len_src
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("self"), get_array_length_cname (f
.name
, dim
));
5840 var len_dest
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("dest"), get_array_length_cname (f
.name
, dim
));
5841 ccode
.add_assignment (len_dest
, len_src
);
5851 cfile
.add_function_declaration (function
);
5852 cfile
.add_function (function
);
5855 public void return_default_value (DataType return_type
) {
5856 ccode
.add_return (default_value_for_type (return_type
, false));
5859 public virtual string?
get_custom_creturn_type (Method m
) {
5863 public virtual void generate_dynamic_method_wrapper (DynamicMethod method
) {
5866 public virtual bool method_has_wrapper (Method method
) {
5870 public virtual CCodeFunctionCall
get_param_spec (Property prop
) {
5871 return new
CCodeFunctionCall (new
CCodeIdentifier (""));
5874 public virtual CCodeFunctionCall
get_signal_creation (Signal sig
, TypeSymbol type
) {
5875 return new
CCodeFunctionCall (new
CCodeIdentifier (""));
5878 public virtual void register_dbus_info (CCodeBlock block
, ObjectTypeSymbol bindable
) {
5881 public virtual string get_dynamic_property_getter_cname (DynamicProperty node
) {
5882 Report
.error (node
.source_reference
, "dynamic properties are not supported for %s".printf (node
.dynamic_type
.to_string ()));
5886 public virtual string get_dynamic_property_setter_cname (DynamicProperty node
) {
5887 Report
.error (node
.source_reference
, "dynamic properties are not supported for %s".printf (node
.dynamic_type
.to_string ()));
5891 public virtual string get_dynamic_signal_cname (DynamicSignal node
) {
5895 public virtual string get_dynamic_signal_connect_wrapper_name (DynamicSignal node
) {
5899 public virtual string get_dynamic_signal_connect_after_wrapper_name (DynamicSignal node
) {
5903 public virtual string get_dynamic_signal_disconnect_wrapper_name (DynamicSignal node
) {
5907 public virtual void generate_marshaller (List
<Parameter
> params
, DataType return_type
, bool dbus
= false) {
5910 public virtual string get_marshaller_function (List
<Parameter
> params
, DataType return_type
, string? prefix
= null, bool dbus
= false) {
5914 public virtual string get_array_length_cname (string array_cname
, int dim
) {
5918 public virtual string get_parameter_array_length_cname (Parameter param
, int dim
) {
5922 public virtual CCodeExpression
get_array_length_cexpression (Expression array_expr
, int dim
= -1) {
5923 return new
CCodeConstant ("");
5926 public virtual CCodeExpression
get_array_length_cvalue (TargetValue value
, int dim
= -1) {
5927 return new
CCodeInvalidExpression ();
5930 public virtual string get_array_size_cname (string array_cname
) {
5934 public virtual void add_simple_check (CodeNode node
, bool always_fails
= false) {
5937 public virtual string generate_ready_function (Method m
) {
5941 public CCodeExpression?
get_cvalue (Expression expr
) {
5942 if (expr
.target_value
== null) {
5945 var glib_value
= (GLibValue
) expr
.target_value
;
5946 return glib_value
.cvalue
;
5949 public CCodeExpression?
get_cvalue_ (TargetValue value
) {
5950 var glib_value
= (GLibValue
) value
;
5951 return glib_value
.cvalue
;
5954 public void set_cvalue (Expression expr
, CCodeExpression? cvalue
) {
5955 var glib_value
= (GLibValue
) expr
.target_value
;
5956 if (glib_value
== null) {
5957 glib_value
= new
GLibValue (expr
.value_type
);
5958 expr
.target_value
= glib_value
;
5960 glib_value
.cvalue
= cvalue
;
5963 public CCodeExpression?
get_array_size_cvalue (TargetValue value
) {
5964 var glib_value
= (GLibValue
) value
;
5965 return glib_value
.array_size_cvalue
;
5968 public void set_array_size_cvalue (TargetValue value
, CCodeExpression? cvalue
) {
5969 var glib_value
= (GLibValue
) value
;
5970 glib_value
.array_size_cvalue
= cvalue
;
5973 public CCodeExpression?
get_delegate_target (Expression expr
) {
5974 if (expr
.target_value
== null) {
5977 var glib_value
= (GLibValue
) expr
.target_value
;
5978 return glib_value
.delegate_target_cvalue
;
5981 public void set_delegate_target (Expression expr
, CCodeExpression? delegate_target
) {
5982 var glib_value
= (GLibValue
) expr
.target_value
;
5983 if (glib_value
== null) {
5984 glib_value
= new
GLibValue (expr
.value_type
);
5985 expr
.target_value
= glib_value
;
5987 glib_value
.delegate_target_cvalue
= delegate_target
;
5990 public CCodeExpression?
get_delegate_target_destroy_notify (Expression expr
) {
5991 if (expr
.target_value
== null) {
5994 var glib_value
= (GLibValue
) expr
.target_value
;
5995 return glib_value
.delegate_target_destroy_notify_cvalue
;
5998 public void set_delegate_target_destroy_notify (Expression expr
, CCodeExpression? destroy_notify
) {
5999 var glib_value
= (GLibValue
) expr
.target_value
;
6000 if (glib_value
== null) {
6001 glib_value
= new
GLibValue (expr
.value_type
);
6002 expr
.target_value
= glib_value
;
6004 glib_value
.delegate_target_destroy_notify_cvalue
= destroy_notify
;
6007 public void append_array_length (Expression expr
, CCodeExpression size
) {
6008 var glib_value
= (GLibValue
) expr
.target_value
;
6009 if (glib_value
== null) {
6010 glib_value
= new
GLibValue (expr
.value_type
);
6011 expr
.target_value
= glib_value
;
6013 glib_value
.append_array_length_cvalue (size
);
6016 public List
<CCodeExpression
>?
get_array_lengths (Expression expr
) {
6017 var glib_value
= (GLibValue
) expr
.target_value
;
6018 if (glib_value
== null) {
6019 glib_value
= new
GLibValue (expr
.value_type
);
6020 expr
.target_value
= glib_value
;
6022 return glib_value
.array_length_cvalues
;
6026 public class Vala
.GLibValue
: TargetValue
{
6027 public CCodeExpression cvalue
;
6029 public List
<CCodeExpression
> array_length_cvalues
;
6030 public CCodeExpression? array_size_cvalue
;
6032 public CCodeExpression? delegate_target_cvalue
;
6033 public CCodeExpression? delegate_target_destroy_notify_cvalue
;
6035 public GLibValue (DataType? value_type
= null, CCodeExpression? cvalue
= null) {
6037 this
.cvalue
= cvalue
;
6040 public void append_array_length_cvalue (CCodeExpression length_cvalue
) {
6041 if (array_length_cvalues
== null) {
6042 array_length_cvalues
= new ArrayList
<CCodeExpression
> ();
6044 array_length_cvalues
.add (length_cvalue
);