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 field_value
= get_field_cvalue (f
, load_this_parameter ((TypeSymbol
) f
.parent_symbol
));
973 List
<Expression
> sizes
= ((ArrayCreationExpression
) f
.initializer
).get_sizes ();
974 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
975 var array_len_lhs
= get_array_length_cvalue (field_value
, dim
);
976 var size
= sizes
[dim
- 1];
977 ccode
.add_assignment (array_len_lhs
, get_cvalue (size
));
980 if (array_type
.rank
== 1 && f
.is_internal_symbol ()) {
981 var lhs_array_size
= get_array_size_cvalue (field_value
);
982 var rhs_array_len
= get_array_length_cvalue (field_value
, 1);
983 ccode
.add_assignment (lhs_array_size
, rhs_array_len
);
987 foreach (LocalVariable local
in temp_ref_vars
) {
988 ccode
.add_expression (destroy_local (local
));
991 temp_ref_vars
.clear ();
996 if (requires_destroy (f
.variable_type
) && instance_finalize_context
!= null) {
997 push_context (instance_finalize_context
);
998 ccode
.add_expression (destroy_field (f
, load_this_parameter ((TypeSymbol
) f
.parent_symbol
)));
1001 } else if (f
.binding
== MemberBinding
.CLASS
) {
1002 if (!is_gtypeinstance
) {
1003 Report
.error (f
.source_reference
, "class fields are not supported in compact classes");
1008 if (f
.access
== SymbolAccessibility
.PRIVATE
) {
1009 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf (cl
.get_upper_case_cname ())));
1010 ccall
.add_argument (new
CCodeIdentifier ("klass"));
1011 lhs
= new
CCodeMemberAccess (ccall
, f
.get_cname (), true);
1013 lhs
= new
CCodeMemberAccess (new
CCodeIdentifier ("klass"), f
.get_cname (), true);
1016 if (f
.initializer
!= null) {
1017 push_context (class_init_context
);
1019 f
.initializer
.emit (this
);
1021 var rhs
= get_cvalue (f
.initializer
);
1023 ccode
.add_assignment (lhs
, rhs
);
1025 foreach (LocalVariable local
in temp_ref_vars
) {
1026 ccode
.add_expression (destroy_local (local
));
1029 temp_ref_vars
.clear ();
1034 generate_field_declaration (f
, cfile
);
1036 if (!f
.is_internal_symbol ()) {
1037 generate_field_declaration (f
, header_file
);
1039 if (!f
.is_private_symbol ()) {
1040 generate_field_declaration (f
, internal_header_file
);
1043 lhs
= new
CCodeIdentifier (f
.get_cname ());
1045 var var_decl
= new
CCodeVariableDeclarator (f
.get_cname (), null, f
.variable_type
.get_cdeclarator_suffix ());
1046 var_decl
.initializer
= default_value_for_type (f
.variable_type
, true);
1048 if (class_init_context
!= null) {
1049 push_context (class_init_context
);
1051 push_context (new
EmitContext ());
1054 if (f
.initializer
!= null) {
1055 f
.initializer
.emit (this
);
1057 var init
= get_cvalue (f
.initializer
);
1058 if (is_constant_ccode_expression (init
)) {
1059 var_decl
.initializer
= init
;
1063 var var_def
= new
CCodeDeclaration (field_ctype
);
1064 var_def
.add_declarator (var_decl
);
1065 if (!f
.is_private_symbol ()) {
1066 var_def
.modifiers
= CCodeModifiers
.EXTERN
;
1068 var_def
.modifiers
= CCodeModifiers
.STATIC
;
1070 cfile
.add_type_member_declaration (var_def
);
1072 /* add array length fields where necessary */
1073 if (f
.variable_type is ArrayType
&& !f
.no_array_length
) {
1074 var array_type
= (ArrayType
) f
.variable_type
;
1076 if (!array_type
.fixed_length
) {
1077 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1078 var len_type
= int_type
.copy ();
1080 var len_def
= new
CCodeDeclaration (len_type
.get_cname ());
1081 len_def
.add_declarator (new
CCodeVariableDeclarator (get_array_length_cname (f
.get_cname (), dim
), new
CCodeConstant ("0")));
1082 if (!f
.is_private_symbol ()) {
1083 len_def
.modifiers
= CCodeModifiers
.EXTERN
;
1085 len_def
.modifiers
= CCodeModifiers
.STATIC
;
1087 cfile
.add_type_member_declaration (len_def
);
1090 if (array_type
.rank
== 1 && f
.is_internal_symbol ()) {
1091 var len_type
= int_type
.copy ();
1093 var cdecl
= new
CCodeDeclaration (len_type
.get_cname ());
1094 cdecl
.add_declarator (new
CCodeVariableDeclarator (get_array_size_cname (f
.get_cname ()), new
CCodeConstant ("0")));
1095 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
1096 cfile
.add_type_member_declaration (cdecl
);
1099 } else if (f
.variable_type is DelegateType
) {
1100 var delegate_type
= (DelegateType
) f
.variable_type
;
1101 if (delegate_type
.delegate_symbol
.has_target
) {
1102 // create field to store delegate target
1104 var target_def
= new
CCodeDeclaration ("gpointer");
1105 target_def
.add_declarator (new
CCodeVariableDeclarator (get_delegate_target_cname (f
.get_cname ()), new
CCodeConstant ("NULL")));
1106 if (!f
.is_private_symbol ()) {
1107 target_def
.modifiers
= CCodeModifiers
.EXTERN
;
1109 target_def
.modifiers
= CCodeModifiers
.STATIC
;
1111 cfile
.add_type_member_declaration (target_def
);
1113 if (delegate_type
.value_owned
) {
1114 var target_destroy_notify_def
= new
CCodeDeclaration ("GDestroyNotify");
1115 target_destroy_notify_def
.add_declarator (new
CCodeVariableDeclarator (get_delegate_target_destroy_notify_cname (f
.get_cname ()), new
CCodeConstant ("NULL")));
1116 if (!f
.is_private_symbol ()) {
1117 target_destroy_notify_def
.modifiers
= CCodeModifiers
.EXTERN
;
1119 target_destroy_notify_def
.modifiers
= CCodeModifiers
.STATIC
;
1121 cfile
.add_type_member_declaration (target_destroy_notify_def
);
1127 if (f
.initializer
!= null) {
1128 var rhs
= get_cvalue (f
.initializer
);
1129 if (!is_constant_ccode_expression (rhs
)) {
1130 if (f
.parent_symbol is Class
) {
1131 if (f
.initializer is InitializerList
) {
1132 ccode
.open_block ();
1134 var temp_decl
= get_temp_variable (f
.variable_type
);
1135 var vardecl
= new CCodeVariableDeclarator
.zero (temp_decl
.name
, rhs
);
1136 ccode
.add_declaration (temp_decl
.variable_type
.get_cname (), vardecl
);
1138 var tmp
= get_variable_cexpression (get_variable_cname (temp_decl
.name
));
1139 ccode
.add_assignment (lhs
, tmp
);
1143 ccode
.add_assignment (lhs
, rhs
);
1146 if (f
.variable_type is ArrayType
&& !f
.no_array_length
&&
1147 f
.initializer is ArrayCreationExpression
) {
1148 var array_type
= (ArrayType
) f
.variable_type
;
1149 var ma
= new MemberAccess
.simple (f
.name
);
1150 ma
.symbol_reference
= f
;
1151 ma
.value_type
= f
.variable_type
.copy ();
1152 visit_member_access (ma
);
1154 List
<Expression
> sizes
= ((ArrayCreationExpression
) f
.initializer
).get_sizes ();
1155 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1156 var array_len_lhs
= get_array_length_cexpression (ma
, dim
);
1157 var size
= sizes
[dim
- 1];
1158 ccode
.add_assignment (array_len_lhs
, get_cvalue (size
));
1163 Report
.error (f
.source_reference
, "Non-constant field initializers not supported in this context");
1173 public bool is_constant_ccode_expression (CCodeExpression cexpr
) {
1174 if (cexpr is CCodeConstant
) {
1176 } else if (cexpr is CCodeCastExpression
) {
1177 var ccast
= (CCodeCastExpression
) cexpr
;
1178 return is_constant_ccode_expression (ccast
.inner
);
1179 } else if (cexpr is CCodeBinaryExpression
) {
1180 var cbinary
= (CCodeBinaryExpression
) cexpr
;
1181 return is_constant_ccode_expression (cbinary
.left
) && is_constant_ccode_expression (cbinary
.right
);
1184 var cparenthesized
= (cexpr as CCodeParenthesizedExpression
);
1185 return (null != cparenthesized
&& is_constant_ccode_expression (cparenthesized
.inner
));
1189 * Returns whether the passed cexpr is a pure expression, i.e. an
1190 * expression without side-effects.
1192 public bool is_pure_ccode_expression (CCodeExpression cexpr
) {
1193 if (cexpr is CCodeConstant
|| cexpr is CCodeIdentifier
) {
1195 } else if (cexpr is CCodeBinaryExpression
) {
1196 var cbinary
= (CCodeBinaryExpression
) cexpr
;
1197 return is_pure_ccode_expression (cbinary
.left
) && is_constant_ccode_expression (cbinary
.right
);
1198 } else if (cexpr is CCodeUnaryExpression
) {
1199 var cunary
= (CCodeUnaryExpression
) cexpr
;
1200 switch (cunary
.operator
) {
1201 case CCodeUnaryOperator
.PREFIX_INCREMENT
:
1202 case CCodeUnaryOperator
.PREFIX_DECREMENT
:
1203 case CCodeUnaryOperator
.POSTFIX_INCREMENT
:
1204 case CCodeUnaryOperator
.POSTFIX_DECREMENT
:
1207 return is_pure_ccode_expression (cunary
.inner
);
1209 } else if (cexpr is CCodeMemberAccess
) {
1210 var cma
= (CCodeMemberAccess
) cexpr
;
1211 return is_pure_ccode_expression (cma
.inner
);
1212 } else if (cexpr is CCodeElementAccess
) {
1213 var cea
= (CCodeElementAccess
) cexpr
;
1214 return is_pure_ccode_expression (cea
.container
) && is_pure_ccode_expression (cea
.index
);
1215 } else if (cexpr is CCodeCastExpression
) {
1216 var ccast
= (CCodeCastExpression
) cexpr
;
1217 return is_pure_ccode_expression (ccast
.inner
);
1218 } else if (cexpr is CCodeParenthesizedExpression
) {
1219 var cparenthesized
= (CCodeParenthesizedExpression
) cexpr
;
1220 return is_pure_ccode_expression (cparenthesized
.inner
);
1226 public override void visit_formal_parameter (Parameter p
) {
1228 check_type (p
.variable_type
);
1232 public override void visit_property (Property prop
) {
1233 visit_member (prop
);
1235 check_type (prop
.property_type
);
1237 if (prop
.get_accessor
!= null) {
1238 prop
.get_accessor
.accept (this
);
1240 if (prop
.set_accessor
!= null) {
1241 prop
.set_accessor
.accept (this
);
1245 public void generate_type_declaration (DataType type
, CCodeFile decl_space
) {
1246 if (type is ObjectType
) {
1247 var object_type
= (ObjectType
) type
;
1248 if (object_type
.type_symbol is Class
) {
1249 generate_class_declaration ((Class
) object_type
.type_symbol
, decl_space
);
1250 } else if (object_type
.type_symbol is Interface
) {
1251 generate_interface_declaration ((Interface
) object_type
.type_symbol
, decl_space
);
1253 } else if (type is DelegateType
) {
1254 var deleg_type
= (DelegateType
) type
;
1255 var d
= deleg_type
.delegate_symbol
;
1256 generate_delegate_declaration (d
, decl_space
);
1257 } else if (type
.data_type is Enum
) {
1258 var en
= (Enum
) type
.data_type
;
1259 generate_enum_declaration (en
, decl_space
);
1260 } else if (type is ValueType
) {
1261 var value_type
= (ValueType
) type
;
1262 generate_struct_declaration ((Struct
) value_type
.type_symbol
, decl_space
);
1263 } else if (type is ArrayType
) {
1264 var array_type
= (ArrayType
) type
;
1265 generate_type_declaration (array_type
.element_type
, decl_space
);
1266 } else if (type is ErrorType
) {
1267 var error_type
= (ErrorType
) type
;
1268 if (error_type
.error_domain
!= null) {
1269 generate_error_domain_declaration (error_type
.error_domain
, decl_space
);
1271 } else if (type is PointerType
) {
1272 var pointer_type
= (PointerType
) type
;
1273 generate_type_declaration (pointer_type
.base_type
, decl_space
);
1276 foreach (DataType type_arg
in type
.get_type_arguments ()) {
1277 generate_type_declaration (type_arg
, decl_space
);
1281 public virtual void generate_class_struct_declaration (Class cl
, CCodeFile decl_space
) {
1284 public virtual void generate_struct_declaration (Struct st
, CCodeFile decl_space
) {
1287 public virtual void generate_delegate_declaration (Delegate d
, CCodeFile decl_space
) {
1290 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) {
1293 public void generate_property_accessor_declaration (PropertyAccessor acc
, CCodeFile decl_space
) {
1294 if (add_symbol_declaration (decl_space
, acc
, acc
.get_cname ())) {
1298 var prop
= (Property
) acc
.prop
;
1300 bool returns_real_struct
= acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ();
1303 CCodeParameter cvalueparam
;
1304 if (returns_real_struct
) {
1305 cvalueparam
= new
CCodeParameter ("result", acc
.value_type
.get_cname () + "*");
1306 } else if (!acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ()) {
1307 cvalueparam
= new
CCodeParameter ("value", acc
.value_type
.get_cname () + "*");
1309 cvalueparam
= new
CCodeParameter ("value", acc
.value_type
.get_cname ());
1311 generate_type_declaration (acc
.value_type
, decl_space
);
1313 CCodeFunction function
;
1314 if (acc
.readable
&& !returns_real_struct
) {
1315 function
= new
CCodeFunction (acc
.get_cname (), acc
.value_type
.get_cname ());
1317 function
= new
CCodeFunction (acc
.get_cname (), "void");
1320 if (prop
.binding
== MemberBinding
.INSTANCE
) {
1321 var t
= (TypeSymbol
) prop
.parent_symbol
;
1322 var this_type
= get_data_type_for_symbol (t
);
1323 generate_type_declaration (this_type
, decl_space
);
1324 var cselfparam
= new
CCodeParameter ("self", this_type
.get_cname ());
1326 cselfparam
.type_name
+= "*";
1329 function
.add_parameter (cselfparam
);
1332 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1333 function
.add_parameter (cvalueparam
);
1336 if (acc
.value_type is ArrayType
) {
1337 var array_type
= (ArrayType
) acc
.value_type
;
1339 var length_ctype
= "int";
1341 length_ctype
= "int*";
1344 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1345 function
.add_parameter (new
CCodeParameter (get_array_length_cname (acc
.readable ?
"result" : "value", dim
), length_ctype
));
1347 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1348 function
.add_parameter (new
CCodeParameter (get_delegate_target_cname (acc
.readable ?
"result" : "value"), acc
.readable ?
"gpointer*" : "gpointer"));
1351 if (prop
.is_private_symbol () || (!acc
.readable
&& !acc
.writable
) || acc
.access
== SymbolAccessibility
.PRIVATE
) {
1352 function
.modifiers
|= CCodeModifiers
.STATIC
;
1354 decl_space
.add_function_declaration (function
);
1357 public override void visit_property_accessor (PropertyAccessor acc
) {
1358 push_context (new
EmitContext (acc
));
1360 var prop
= (Property
) acc
.prop
;
1362 if (acc
.comment
!= null) {
1363 cfile
.add_type_member_definition (new
CCodeComment (acc
.comment
.content
));
1366 bool returns_real_struct
= acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ();
1368 if (acc
.result_var
!= null) {
1369 acc
.result_var
.accept (this
);
1372 var t
= (TypeSymbol
) prop
.parent_symbol
;
1374 if (acc
.construction
&& !t
.is_subtype_of (gobject_type
)) {
1375 Report
.error (acc
.source_reference
, "construct properties require GLib.Object");
1378 } else if (acc
.construction
&& !is_gobject_property (prop
)) {
1379 Report
.error (acc
.source_reference
, "construct properties not supported for specified property type");
1384 // do not declare overriding properties and interface implementations
1385 if (prop
.is_abstract
|| prop
.is_virtual
1386 || (prop
.base_property
== null && prop
.base_interface_property
== null)) {
1387 generate_property_accessor_declaration (acc
, cfile
);
1389 // do not declare construct-only properties in header files
1390 if (acc
.readable
|| acc
.writable
) {
1391 if (!prop
.is_internal_symbol ()
1392 && (acc
.access
== SymbolAccessibility
.PUBLIC
1393 || acc
.access
== SymbolAccessibility
.PROTECTED
)) {
1394 generate_property_accessor_declaration (acc
, header_file
);
1396 if (!prop
.is_private_symbol () && acc
.access
!= SymbolAccessibility
.PRIVATE
) {
1397 generate_property_accessor_declaration (acc
, internal_header_file
);
1402 if (acc
.source_type
== SourceFileType
.FAST
) {
1406 var this_type
= get_data_type_for_symbol (t
);
1407 var cselfparam
= new
CCodeParameter ("self", this_type
.get_cname ());
1409 cselfparam
.type_name
+= "*";
1411 CCodeParameter cvalueparam
;
1412 if (returns_real_struct
) {
1413 cvalueparam
= new
CCodeParameter ("result", acc
.value_type
.get_cname () + "*");
1414 } else if (!acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ()) {
1415 cvalueparam
= new
CCodeParameter ("value", acc
.value_type
.get_cname () + "*");
1417 cvalueparam
= new
CCodeParameter ("value", acc
.value_type
.get_cname ());
1420 if (prop
.is_abstract
|| prop
.is_virtual
) {
1421 CCodeFunction function
;
1422 if (acc
.readable
&& !returns_real_struct
) {
1423 function
= new
CCodeFunction (acc
.get_cname (), current_return_type
.get_cname ());
1425 function
= new
CCodeFunction (acc
.get_cname (), "void");
1427 function
.add_parameter (cselfparam
);
1428 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1429 function
.add_parameter (cvalueparam
);
1432 if (acc
.value_type is ArrayType
) {
1433 var array_type
= (ArrayType
) acc
.value_type
;
1435 var length_ctype
= "int";
1437 length_ctype
= "int*";
1440 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1441 function
.add_parameter (new
CCodeParameter (get_array_length_cname (acc
.readable ?
"result" : "value", dim
), length_ctype
));
1443 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1444 function
.add_parameter (new
CCodeParameter (get_delegate_target_cname (acc
.readable ?
"result" : "value"), acc
.readable ?
"gpointer*" : "gpointer"));
1447 if (prop
.is_private_symbol () || !(acc
.readable
|| acc
.writable
) || acc
.access
== SymbolAccessibility
.PRIVATE
) {
1448 // accessor function should be private if the property is an internal symbol or it's a construct-only setter
1449 function
.modifiers
|= CCodeModifiers
.STATIC
;
1452 push_function (function
);
1454 CCodeFunctionCall vcast
= null;
1455 if (prop
.parent_symbol is Interface
) {
1456 var iface
= (Interface
) prop
.parent_symbol
;
1458 vcast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_INTERFACE".printf (iface
.get_upper_case_cname (null))));
1460 var cl
= (Class
) prop
.parent_symbol
;
1462 vcast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS".printf (cl
.get_upper_case_cname (null))));
1464 vcast
.add_argument (new
CCodeIdentifier ("self"));
1467 var vcall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (vcast
, "get_%s".printf (prop
.name
)));
1468 vcall
.add_argument (new
CCodeIdentifier ("self"));
1469 if (returns_real_struct
) {
1470 vcall
.add_argument (new
CCodeIdentifier ("result"));
1471 ccode
.add_expression (vcall
);
1473 if (acc
.value_type is ArrayType
) {
1474 var array_type
= (ArrayType
) acc
.value_type
;
1476 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1477 var len_expr
= new
CCodeIdentifier (get_array_length_cname ("result", dim
));
1478 vcall
.add_argument (len_expr
);
1480 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1481 vcall
.add_argument (new
CCodeIdentifier (get_delegate_target_cname ("result")));
1484 ccode
.add_return (vcall
);
1487 var vcall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (vcast
, "set_%s".printf (prop
.name
)));
1488 vcall
.add_argument (new
CCodeIdentifier ("self"));
1489 vcall
.add_argument (new
CCodeIdentifier ("value"));
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 ("value", 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 ("value")));
1502 ccode
.add_expression (vcall
);
1507 cfile
.add_function (function
);
1510 if (!prop
.is_abstract
) {
1511 bool is_virtual
= prop
.base_property
!= null || prop
.base_interface_property
!= null;
1516 cname
= "%s_real_get_%s".printf (t
.get_lower_case_cname (null), prop
.name
);
1518 cname
= "%s_real_set_%s".printf (t
.get_lower_case_cname (null), prop
.name
);
1521 cname
= acc
.get_cname ();
1524 CCodeFunction function
;
1525 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1526 function
= new
CCodeFunction (cname
, "void");
1528 function
= new
CCodeFunction (cname
, acc
.value_type
.get_cname ());
1531 ObjectType base_type
= null;
1532 if (prop
.binding
== MemberBinding
.INSTANCE
) {
1534 if (prop
.base_property
!= null) {
1535 base_type
= new
ObjectType ((ObjectTypeSymbol
) prop
.base_property
.parent_symbol
);
1536 } else if (prop
.base_interface_property
!= null) {
1537 base_type
= new
ObjectType ((ObjectTypeSymbol
) prop
.base_interface_property
.parent_symbol
);
1539 function
.modifiers
|= CCodeModifiers
.STATIC
;
1540 function
.add_parameter (new
CCodeParameter ("base", base_type
.get_cname ()));
1542 function
.add_parameter (cselfparam
);
1545 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1546 function
.add_parameter (cvalueparam
);
1549 if (acc
.value_type is ArrayType
) {
1550 var array_type
= (ArrayType
) acc
.value_type
;
1552 var length_ctype
= "int";
1554 length_ctype
= "int*";
1557 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1558 function
.add_parameter (new
CCodeParameter (get_array_length_cname (acc
.readable ?
"result" : "value", dim
), length_ctype
));
1560 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1561 function
.add_parameter (new
CCodeParameter (get_delegate_target_cname (acc
.readable ?
"result" : "value"), acc
.readable ?
"gpointer*" : "gpointer"));
1565 if (prop
.is_private_symbol () || !(acc
.readable
|| acc
.writable
) || acc
.access
== SymbolAccessibility
.PRIVATE
) {
1566 // accessor function should be private if the property is an internal symbol or it's a construct-only setter
1567 function
.modifiers
|= CCodeModifiers
.STATIC
;
1571 push_function (function
);
1573 if (prop
.binding
== MemberBinding
.INSTANCE
&& !is_virtual
) {
1574 if (!acc
.readable
|| returns_real_struct
) {
1575 create_property_type_check_statement (prop
, false, t
, true, "self");
1577 create_property_type_check_statement (prop
, true, t
, true, "self");
1581 if (acc
.readable
&& !returns_real_struct
) {
1582 // do not declare result variable if exit block is known to be unreachable
1583 if (acc
.return_block
== null || acc
.return_block
.get_predecessors ().size
> 0) {
1584 ccode
.add_declaration (acc
.value_type
.get_cname (), new
CCodeVariableDeclarator ("result"));
1589 ccode
.add_declaration (this_type
.get_cname (), new
CCodeVariableDeclarator ("self"));
1590 ccode
.add_assignment (new
CCodeIdentifier ("self"), transform_expression (new
CCodeIdentifier ("base"), base_type
, this_type
));
1593 acc
.body
.emit (this
);
1595 if (current_method_inner_error
) {
1596 ccode
.add_declaration ("GError *", new CCodeVariableDeclarator
.zero ("_inner_error_", new
CCodeConstant ("NULL")));
1599 // notify on property changes
1600 if (is_gobject_property (prop
) &&
1602 (acc
.writable
|| acc
.construction
)) {
1603 var notify_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_object_notify"));
1604 notify_call
.add_argument (new
CCodeCastExpression (new
CCodeIdentifier ("self"), "GObject *"));
1605 notify_call
.add_argument (prop
.get_canonical_cconstant ());
1606 ccode
.add_expression (notify_call
);
1609 cfile
.add_function (function
);
1615 public override void visit_destructor (Destructor d
) {
1616 if (d
.binding
== MemberBinding
.STATIC
&& !in_plugin
) {
1617 Report
.error (d
.source_reference
, "static destructors are only supported for dynamic types");
1623 public int get_block_id (Block b
) {
1624 int result
= block_map
[b
];
1626 result
= ++next_block_id
;
1627 block_map
[b
] = result
;
1632 void capture_parameter (Parameter param
, CCodeStruct data
, int block_id
) {
1633 generate_type_declaration (param
.variable_type
, cfile
);
1635 var param_type
= param
.variable_type
.copy ();
1636 param_type
.value_owned
= true;
1637 data
.add_field (param_type
.get_cname (), get_variable_cname (param
.name
));
1639 bool is_unowned_delegate
= param
.variable_type is DelegateType
&& !param
.variable_type
.value_owned
;
1641 // create copy if necessary as captured variables may need to be kept alive
1642 CCodeExpression cparam
= get_variable_cexpression (param
.name
);
1643 if (param
.variable_type
.is_real_non_null_struct_type ()) {
1644 cparam
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, cparam
);
1646 if (requires_copy (param_type
) && !param
.variable_type
.value_owned
&& !is_unowned_delegate
) {
1647 var ma
= new MemberAccess
.simple (param
.name
);
1648 ma
.symbol_reference
= param
;
1649 ma
.value_type
= param
.variable_type
.copy ();
1650 // directly access parameters in ref expressions
1651 param
.captured
= false;
1652 visit_member_access (ma
);
1653 cparam
= get_ref_cexpression (param
.variable_type
, cparam
, ma
, param
);
1654 param
.captured
= true;
1657 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), get_variable_cname (param
.name
)), cparam
);
1659 if (param
.variable_type is ArrayType
) {
1660 var array_type
= (ArrayType
) param
.variable_type
;
1661 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1662 data
.add_field ("gint", get_parameter_array_length_cname (param
, dim
));
1663 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
)));
1665 } else if (param
.variable_type is DelegateType
) {
1666 CCodeExpression target_expr
;
1667 CCodeExpression delegate_target_destroy_notify
;
1668 if (is_in_coroutine ()) {
1669 target_expr
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("data"), get_delegate_target_cname (get_variable_cname (param
.name
)));
1670 delegate_target_destroy_notify
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("data"), get_delegate_target_destroy_notify_cname (get_variable_cname (param
.name
)));
1672 target_expr
= new
CCodeIdentifier (get_delegate_target_cname (get_variable_cname (param
.name
)));
1673 delegate_target_destroy_notify
= new
CCodeIdentifier (get_delegate_target_destroy_notify_cname (get_variable_cname (param
.name
)));
1676 data
.add_field ("gpointer", get_delegate_target_cname (get_variable_cname (param
.name
)));
1677 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
);
1678 if (param
.variable_type
.value_owned
) {
1679 data
.add_field ("GDestroyNotify", get_delegate_target_destroy_notify_cname (get_variable_cname (param
.name
)));
1680 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
);
1685 public override void visit_block (Block b
) {
1686 emit_context
.push_symbol (b
);
1688 var local_vars
= b
.get_local_variables ();
1690 if (b
.parent_node is Block
|| b
.parent_node is SwitchStatement
) {
1691 ccode
.open_block ();
1695 var parent_block
= next_closure_block (b
.parent_symbol
);
1697 int block_id
= get_block_id (b
);
1698 string struct_name
= "Block%dData".printf (block_id
);
1700 var data
= new
CCodeStruct ("_" + struct_name
);
1701 data
.add_field ("int", "_ref_count_");
1702 if (parent_block
!= null) {
1703 int parent_block_id
= get_block_id (parent_block
);
1705 data
.add_field ("Block%dData *".printf (parent_block_id
), "_data%d_".printf (parent_block_id
));
1707 if (in_constructor
|| (current_method
!= null && current_method
.binding
== MemberBinding
.INSTANCE
) ||
1708 (current_property_accessor
!= null && current_property_accessor
.prop
.binding
== MemberBinding
.INSTANCE
)) {
1709 data
.add_field ("%s *".printf (current_class
.get_cname ()), "self");
1712 if (current_method
!= null) {
1713 // allow capturing generic type parameters
1714 foreach (var type_param
in current_method
.get_type_parameters ()) {
1717 func_name
= "%s_type".printf (type_param
.name
.down ());
1718 data
.add_field ("GType", func_name
);
1720 func_name
= "%s_dup_func".printf (type_param
.name
.down ());
1721 data
.add_field ("GBoxedCopyFunc", func_name
);
1723 func_name
= "%s_destroy_func".printf (type_param
.name
.down ());
1724 data
.add_field ("GDestroyNotify", func_name
);
1728 foreach (var local
in local_vars
) {
1729 if (local
.captured
) {
1730 generate_type_declaration (local
.variable_type
, cfile
);
1732 data
.add_field (local
.variable_type
.get_cname (), get_variable_cname (local
.name
) + local
.variable_type
.get_cdeclarator_suffix ());
1734 if (local
.variable_type is ArrayType
) {
1735 var array_type
= (ArrayType
) local
.variable_type
;
1736 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1737 data
.add_field ("gint", get_array_length_cname (get_variable_cname (local
.name
), dim
));
1739 data
.add_field ("gint", get_array_size_cname (get_variable_cname (local
.name
)));
1740 } else if (local
.variable_type is DelegateType
) {
1741 data
.add_field ("gpointer", get_delegate_target_cname (get_variable_cname (local
.name
)));
1742 if (local
.variable_type
.value_owned
) {
1743 data
.add_field ("GDestroyNotify", get_delegate_target_destroy_notify_cname (get_variable_cname (local
.name
)));
1749 var data_alloc
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_slice_new0"));
1750 data_alloc
.add_argument (new
CCodeIdentifier (struct_name
));
1752 if (is_in_coroutine ()) {
1753 closure_struct
.add_field (struct_name
+ "*", "_data%d_".printf (block_id
));
1755 ccode
.add_declaration (struct_name
+ "*", new
CCodeVariableDeclarator ("_data%d_".printf (block_id
)));
1757 ccode
.add_assignment (get_variable_cexpression ("_data%d_".printf (block_id
)), data_alloc
);
1759 // initialize ref_count
1760 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "_ref_count_"), new
CCodeIdentifier ("1"));
1762 if (parent_block
!= null) {
1763 int parent_block_id
= get_block_id (parent_block
);
1765 var ref_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_ref".printf (parent_block_id
)));
1766 ref_call
.add_argument (get_variable_cexpression ("_data%d_".printf (parent_block_id
)));
1768 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "_data%d_".printf (parent_block_id
)), ref_call
);
1770 if (in_constructor
|| (current_method
!= null && current_method
.binding
== MemberBinding
.INSTANCE
&&
1771 (!(current_method is CreationMethod
) || current_method
.body
!= b
)) ||
1772 (current_property_accessor
!= null && current_property_accessor
.prop
.binding
== MemberBinding
.INSTANCE
)) {
1773 var ref_call
= new
CCodeFunctionCall (get_dup_func_expression (new
ObjectType (current_class
), b
.source_reference
));
1774 ref_call
.add_argument (get_result_cexpression ("self"));
1776 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "self"), ref_call
);
1779 if (current_method
!= null) {
1780 // allow capturing generic type parameters
1781 foreach (var type_param
in current_method
.get_type_parameters ()) {
1784 func_name
= "%s_type".printf (type_param
.name
.down ());
1785 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), func_name
), new
CCodeIdentifier (func_name
));
1787 func_name
= "%s_dup_func".printf (type_param
.name
.down ());
1788 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), func_name
), new
CCodeIdentifier (func_name
));
1790 func_name
= "%s_destroy_func".printf (type_param
.name
.down ());
1791 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), func_name
), new
CCodeIdentifier (func_name
));
1796 if (b
.parent_symbol is Method
) {
1797 var m
= (Method
) b
.parent_symbol
;
1799 // parameters are captured with the top-level block of the method
1800 foreach (var param
in m
.get_parameters ()) {
1801 if (param
.captured
) {
1802 capture_parameter (param
, data
, block_id
);
1807 // capture async data to allow invoking callback from inside closure
1808 data
.add_field ("gpointer", "_async_data_");
1810 // async method is suspended while waiting for callback,
1811 // so we never need to care about memory management of async data
1812 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "_async_data_"), new
CCodeIdentifier ("data"));
1814 } else if (b
.parent_symbol is PropertyAccessor
) {
1815 var acc
= (PropertyAccessor
) b
.parent_symbol
;
1817 if (!acc
.readable
&& acc
.value_parameter
.captured
) {
1818 capture_parameter (acc
.value_parameter
, data
, block_id
);
1822 var typedef
= new
CCodeTypeDefinition ("struct _" + struct_name
, new
CCodeVariableDeclarator (struct_name
));
1823 cfile
.add_type_declaration (typedef
);
1824 cfile
.add_type_definition (data
);
1826 // create ref/unref functions
1827 var ref_fun
= new
CCodeFunction ("block%d_data_ref".printf (block_id
), struct_name
+ "*");
1828 ref_fun
.add_parameter (new
CCodeParameter ("_data%d_".printf (block_id
), struct_name
+ "*"));
1829 ref_fun
.modifiers
= CCodeModifiers
.STATIC
;
1830 cfile
.add_function_declaration (ref_fun
);
1831 ref_fun
.block
= new
CCodeBlock ();
1833 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_atomic_int_inc"));
1834 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_ref_count_")));
1835 ref_fun
.block
.add_statement (new
CCodeExpressionStatement (ccall
));
1836 ref_fun
.block
.add_statement (new
CCodeReturnStatement (new
CCodeIdentifier ("_data%d_".printf (block_id
))));
1837 cfile
.add_function (ref_fun
);
1839 var unref_fun
= new
CCodeFunction ("block%d_data_unref".printf (block_id
), "void");
1840 unref_fun
.add_parameter (new
CCodeParameter ("_data%d_".printf (block_id
), struct_name
+ "*"));
1841 unref_fun
.modifiers
= CCodeModifiers
.STATIC
;
1842 cfile
.add_function_declaration (unref_fun
);
1844 push_function (unref_fun
);
1846 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_atomic_int_dec_and_test"));
1847 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_ref_count_")));
1848 ccode
.open_if (ccall
);
1850 if (parent_block
!= null) {
1851 int parent_block_id
= get_block_id (parent_block
);
1853 var unref_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_unref".printf (parent_block_id
)));
1854 unref_call
.add_argument (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_data%d_".printf (parent_block_id
)));
1855 ccode
.add_expression (unref_call
);
1856 ccode
.add_assignment (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_data%d_".printf (parent_block_id
)), new
CCodeConstant ("NULL"));
1858 if (in_constructor
|| (current_method
!= null && current_method
.binding
== MemberBinding
.INSTANCE
) ||
1859 (current_property_accessor
!= null && current_property_accessor
.prop
.binding
== MemberBinding
.INSTANCE
)) {
1860 var ma
= new MemberAccess
.simple ("this");
1861 ma
.symbol_reference
= current_class
;
1862 ccode
.add_expression (get_unref_expression (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "self"), new
ObjectType (current_class
), ma
));
1866 // free in reverse order
1867 for (int i
= local_vars
.size
- 1; i
>= 0; i
--) {
1868 var local
= local_vars
[i
];
1869 if (local
.captured
) {
1870 if (requires_destroy (local
.variable_type
)) {
1871 bool old_coroutine
= false;
1872 if (current_method
!= null) {
1873 old_coroutine
= current_method
.coroutine
;
1874 current_method
.coroutine
= false;
1877 ccode
.add_expression (destroy_local (local
));
1879 if (old_coroutine
) {
1880 current_method
.coroutine
= true;
1886 if (b
.parent_symbol is Method
) {
1887 var m
= (Method
) b
.parent_symbol
;
1889 // parameters are captured with the top-level block of the method
1890 foreach (var param
in m
.get_parameters ()) {
1891 if (param
.captured
) {
1892 var param_type
= param
.variable_type
.copy ();
1893 param_type
.value_owned
= true;
1895 bool is_unowned_delegate
= param
.variable_type is DelegateType
&& !param
.variable_type
.value_owned
;
1897 if (requires_destroy (param_type
) && !is_unowned_delegate
) {
1898 bool old_coroutine
= false;
1900 old_coroutine
= m
.coroutine
;
1901 m
.coroutine
= false;
1904 ccode
.add_expression (destroy_parameter (param
));
1906 if (old_coroutine
) {
1912 } else if (b
.parent_symbol is PropertyAccessor
) {
1913 var acc
= (PropertyAccessor
) b
.parent_symbol
;
1915 if (!acc
.readable
&& acc
.value_parameter
.captured
) {
1916 var param_type
= acc
.value_parameter
.variable_type
.copy ();
1917 param_type
.value_owned
= true;
1919 bool is_unowned_delegate
= acc
.value_parameter
.variable_type is DelegateType
&& !acc
.value_parameter
.variable_type
.value_owned
;
1921 if (requires_destroy (param_type
) && !is_unowned_delegate
) {
1922 ccode
.add_expression (destroy_parameter (acc
.value_parameter
));
1927 var data_free
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_slice_free"));
1928 data_free
.add_argument (new
CCodeIdentifier (struct_name
));
1929 data_free
.add_argument (new
CCodeIdentifier ("_data%d_".printf (block_id
)));
1930 ccode
.add_expression (data_free
);
1936 cfile
.add_function (unref_fun
);
1939 foreach (Statement stmt
in b
.get_statements ()) {
1943 // free in reverse order
1944 for (int i
= local_vars
.size
- 1; i
>= 0; i
--) {
1945 var local
= local_vars
[i
];
1946 local
.active
= false;
1947 if (!local
.unreachable
&& !local
.floating
&& !local
.captured
&& requires_destroy (local
.variable_type
)) {
1948 ccode
.add_expression (destroy_local (local
));
1952 if (b
.parent_symbol is Method
) {
1953 var m
= (Method
) b
.parent_symbol
;
1954 foreach (Parameter param
in m
.get_parameters ()) {
1955 if (!param
.captured
&& !param
.ellipsis
&& requires_destroy (param
.variable_type
) && param
.direction
== ParameterDirection
.IN
) {
1956 ccode
.add_expression (destroy_parameter (param
));
1957 } else if (param
.direction
== ParameterDirection
.OUT
&& !m
.coroutine
) {
1958 return_out_parameter (param
);
1964 int block_id
= get_block_id (b
);
1966 var data_unref
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_unref".printf (block_id
)));
1967 data_unref
.add_argument (get_variable_cexpression ("_data%d_".printf (block_id
)));
1968 ccode
.add_expression (data_unref
);
1969 ccode
.add_assignment (get_variable_cexpression ("_data%d_".printf (block_id
)), new
CCodeConstant ("NULL"));
1972 if (b
.parent_node is Block
|| b
.parent_node is SwitchStatement
) {
1976 emit_context
.pop_symbol ();
1979 public override void visit_declaration_statement (DeclarationStatement stmt
) {
1980 stmt
.declaration
.accept (this
);
1983 public CCodeExpression
get_variable_cexpression (string name
) {
1984 if (is_in_coroutine ()) {
1985 return new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("data"), get_variable_cname (name
));
1987 return new
CCodeIdentifier (get_variable_cname (name
));
1991 public string get_variable_cname (string name
) {
1992 if (name
[0] == '.') {
1993 if (name
== ".result") {
1996 // compiler-internal variable
1997 if (!variable_name_map
.contains (name
)) {
1998 variable_name_map
.set (name
, "_tmp%d_".printf (next_temp_var_id
));
2001 return variable_name_map
.get (name
);
2002 } else if (reserved_identifiers
.contains (name
)) {
2003 return "_%s_".printf (name
);
2009 public CCodeExpression
get_result_cexpression (string cname
= "result") {
2010 if (is_in_coroutine ()) {
2011 return new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("data"), cname
);
2013 return new
CCodeIdentifier (cname
);
2017 bool has_simple_struct_initializer (LocalVariable local
) {
2018 var st
= local
.variable_type
.data_type as Struct
;
2019 var initializer
= local
.initializer as ObjectCreationExpression
;
2020 if (st
!= null && (!st
.is_simple_type () || st
.get_cname () == "va_list") && !local
.variable_type
.nullable
&&
2021 initializer
!= null && initializer
.get_object_initializer ().size
== 0) {
2028 public override void visit_local_variable (LocalVariable local
) {
2029 check_type (local
.variable_type
);
2031 if (local
.initializer
!= null) {
2032 local
.initializer
.emit (this
);
2034 visit_end_full_expression (local
.initializer
);
2037 generate_type_declaration (local
.variable_type
, cfile
);
2039 CCodeExpression rhs
= null;
2040 if (local
.initializer
!= null && get_cvalue (local
.initializer
) != null) {
2041 rhs
= get_cvalue (local
.initializer
);
2044 if (!local
.captured
) {
2045 if (current_method
!= null && current_method
.coroutine
) {
2046 closure_struct
.add_field (local
.variable_type
.get_cname (), get_variable_cname (local
.name
) + local
.variable_type
.get_cdeclarator_suffix ());
2048 var cvar
= new
CCodeVariableDeclarator (get_variable_cname (local
.name
), null, local
.variable_type
.get_cdeclarator_suffix ());
2050 // try to initialize uninitialized variables
2051 // initialization not necessary for variables stored in closure
2052 if (rhs
== null || has_simple_struct_initializer (local
)) {
2053 cvar
.initializer
= default_value_for_type (local
.variable_type
, true);
2057 ccode
.add_declaration (local
.variable_type
.get_cname (), cvar
);
2060 if (local
.variable_type is ArrayType
) {
2061 // create variables to store array dimensions
2062 var array_type
= (ArrayType
) local
.variable_type
;
2064 if (!array_type
.fixed_length
) {
2065 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
2066 var len_var
= new
LocalVariable (int_type
.copy (), get_array_length_cname (get_variable_cname (local
.name
), dim
));
2067 emit_temp_var (len_var
, local
.initializer
== null);
2070 if (array_type
.rank
== 1) {
2071 var size_var
= new
LocalVariable (int_type
.copy (), get_array_size_cname (get_variable_cname (local
.name
)));
2072 emit_temp_var (size_var
, local
.initializer
== null);
2075 } else if (local
.variable_type is DelegateType
) {
2076 var deleg_type
= (DelegateType
) local
.variable_type
;
2077 var d
= deleg_type
.delegate_symbol
;
2079 // create variable to store delegate target
2080 var target_var
= new
LocalVariable (new
PointerType (new
VoidType ()), get_delegate_target_cname (get_variable_cname (local
.name
)));
2081 emit_temp_var (target_var
, local
.initializer
== null);
2082 if (deleg_type
.value_owned
) {
2083 var target_destroy_notify_var
= new
LocalVariable (gdestroynotify_type
, get_delegate_target_destroy_notify_cname (get_variable_cname (local
.name
)));
2084 emit_temp_var (target_destroy_notify_var
, local
.initializer
== null);
2091 if (!has_simple_struct_initializer (local
)) {
2092 store_local (local
, local
.initializer
.target_value
, true);
2096 if (local
.initializer
!= null && local
.initializer
.tree_can_fail
) {
2097 add_simple_check (local
.initializer
);
2100 local
.active
= true;
2103 public override void visit_initializer_list (InitializerList list
) {
2104 if (list
.target_type
.data_type is Struct
) {
2105 /* initializer is used as struct initializer */
2106 var st
= (Struct
) list
.target_type
.data_type
;
2108 if (list
.parent_node is Constant
|| list
.parent_node is Field
|| list
.parent_node is InitializerList
) {
2109 var clist
= new
CCodeInitializerList ();
2111 var field_it
= st
.get_fields ().iterator ();
2112 foreach (Expression expr
in list
.get_initializers ()) {
2114 while (field
== null) {
2116 field
= field_it
.get ();
2117 if (field
.binding
!= MemberBinding
.INSTANCE
) {
2118 // we only initialize instance fields
2123 var cexpr
= get_cvalue (expr
);
2125 string ctype
= field
.get_ctype ();
2126 if (ctype
!= null) {
2127 cexpr
= new
CCodeCastExpression (cexpr
, ctype
);
2130 clist
.append (cexpr
);
2133 set_cvalue (list
, clist
);
2135 // used as expression
2136 var temp_decl
= get_temp_variable (list
.target_type
, false, list
);
2137 emit_temp_var (temp_decl
);
2139 var instance
= get_variable_cexpression (get_variable_cname (temp_decl
.name
));
2141 var field_it
= st
.get_fields ().iterator ();
2142 foreach (Expression expr
in list
.get_initializers ()) {
2144 while (field
== null) {
2146 field
= field_it
.get ();
2147 if (field
.binding
!= MemberBinding
.INSTANCE
) {
2148 // we only initialize instance fields
2153 var cexpr
= get_cvalue (expr
);
2155 string ctype
= field
.get_ctype ();
2156 if (ctype
!= null) {
2157 cexpr
= new
CCodeCastExpression (cexpr
, ctype
);
2160 var lhs
= new
CCodeMemberAccess (instance
, field
.get_cname ());;
2161 ccode
.add_assignment (lhs
, cexpr
);
2164 set_cvalue (list
, instance
);
2167 var clist
= new
CCodeInitializerList ();
2168 foreach (Expression expr
in list
.get_initializers ()) {
2169 clist
.append (get_cvalue (expr
));
2171 set_cvalue (list
, clist
);
2175 public override LocalVariable
create_local (DataType type
) {
2176 var result
= get_temp_variable (type
, type
.value_owned
);
2177 emit_temp_var (result
);
2181 public LocalVariable
get_temp_variable (DataType type
, bool value_owned
= true, CodeNode? node_reference
= null, bool init
= true) {
2182 var var_type
= type
.copy ();
2183 var_type
.value_owned
= value_owned
;
2184 var local
= new
LocalVariable (var_type
, "_tmp%d_".printf (next_temp_var_id
));
2185 local
.no_init
= !init
;
2187 if (node_reference
!= null) {
2188 local
.source_reference
= node_reference
.source_reference
;
2196 bool is_in_generic_type (DataType type
) {
2197 if (current_symbol
!= null && type
.type_parameter
.parent_symbol is TypeSymbol
2198 && (current_method
== null || current_method
.binding
== MemberBinding
.INSTANCE
)) {
2205 public CCodeExpression
get_type_id_expression (DataType type
, bool is_chainup
= false) {
2206 if (type is GenericType
) {
2207 string var_name
= "%s_type".printf (type
.type_parameter
.name
.down ());
2208 if (is_in_generic_type (type
) && !is_chainup
&& !in_creation_method
) {
2209 return new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (get_result_cexpression ("self"), "priv"), var_name
);
2211 return new
CCodeIdentifier (var_name
);
2214 string type_id
= type
.get_type_id ();
2215 if (type_id
== null) {
2216 type_id
= "G_TYPE_INVALID";
2218 generate_type_declaration (type
, cfile
);
2220 return new
CCodeIdentifier (type_id
);
2224 public virtual CCodeExpression?
get_dup_func_expression (DataType type
, SourceReference? source_reference
, bool is_chainup
= false) {
2225 if (type is ErrorType
) {
2226 return new
CCodeIdentifier ("g_error_copy");
2227 } else if (type
.data_type
!= null) {
2228 string dup_function
;
2229 var cl
= type
.data_type as Class
;
2230 if (type
.data_type
.is_reference_counting ()) {
2231 dup_function
= type
.data_type
.get_ref_function ();
2232 if (type
.data_type is Interface
&& dup_function
== null) {
2233 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 ()));
2236 } else if (cl
!= null && cl
.is_immutable
) {
2237 // allow duplicates of immutable instances as for example strings
2238 dup_function
= type
.data_type
.get_dup_function ();
2239 if (dup_function
== null) {
2242 } else if (cl
!= null && cl
.is_gboxed
) {
2243 // allow duplicates of gboxed instances
2244 dup_function
= generate_dup_func_wrapper (type
);
2245 if (dup_function
== null) {
2248 } else if (type is ValueType
) {
2249 dup_function
= type
.data_type
.get_dup_function ();
2250 if (dup_function
== null && type
.nullable
) {
2251 dup_function
= generate_struct_dup_wrapper ((ValueType
) type
);
2252 } else if (dup_function
== null) {
2256 // duplicating non-reference counted objects may cause side-effects (and performance issues)
2257 Report
.error (source_reference
, "duplicating %s instance, use unowned variable or explicitly invoke copy method".printf (type
.data_type
.name
));
2261 return new
CCodeIdentifier (dup_function
);
2262 } else if (type
.type_parameter
!= null) {
2263 string func_name
= "%s_dup_func".printf (type
.type_parameter
.name
.down ());
2264 if (is_in_generic_type (type
) && !is_chainup
&& !in_creation_method
) {
2265 return new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (get_result_cexpression ("self"), "priv"), func_name
);
2267 return new
CCodeIdentifier (func_name
);
2269 } else if (type is PointerType
) {
2270 var pointer_type
= (PointerType
) type
;
2271 return get_dup_func_expression (pointer_type
.base_type
, source_reference
);
2273 return new
CCodeConstant ("NULL");
2277 void make_comparable_cexpression (ref DataType left_type
, ref CCodeExpression cleft
, ref DataType right_type
, ref CCodeExpression cright
) {
2278 var left_type_as_struct
= left_type
.data_type as Struct
;
2279 var right_type_as_struct
= right_type
.data_type as Struct
;
2282 var valuecast
= try_cast_value_to_type (cleft
, left_type
, right_type
);
2283 if (valuecast
!= null) {
2285 left_type
= right_type
;
2286 make_comparable_cexpression (ref left_type
, ref cleft
, ref right_type
, ref cright
);
2290 valuecast
= try_cast_value_to_type (cright
, right_type
, left_type
);
2291 if (valuecast
!= null) {
2293 right_type
= left_type
;
2294 make_comparable_cexpression (ref left_type
, ref cleft
, ref right_type
, ref cright
);
2298 if (left_type
.data_type is Class
&& !((Class
) left_type
.data_type
).is_compact
&&
2299 right_type
.data_type is Class
&& !((Class
) right_type
.data_type
).is_compact
) {
2300 var left_cl
= (Class
) left_type
.data_type
;
2301 var right_cl
= (Class
) right_type
.data_type
;
2303 if (left_cl
!= right_cl
) {
2304 if (left_cl
.is_subtype_of (right_cl
)) {
2305 cleft
= generate_instance_cast (cleft
, right_cl
);
2306 } else if (right_cl
.is_subtype_of (left_cl
)) {
2307 cright
= generate_instance_cast (cright
, left_cl
);
2310 } else if (left_type_as_struct
!= null && right_type_as_struct
!= null) {
2311 if (left_type is StructValueType
) {
2312 // real structs (uses compare/equal function)
2313 if (!left_type
.nullable
) {
2314 cleft
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cleft
);
2316 if (!right_type
.nullable
) {
2317 cright
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cright
);
2320 // integer or floating or boolean type
2321 if (left_type
.nullable
&& right_type
.nullable
) {
2322 // FIXME also compare contents, not just address
2323 } else if (left_type
.nullable
) {
2324 // FIXME check left value is not null
2325 cleft
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, cleft
);
2326 } else if (right_type
.nullable
) {
2327 // FIXME check right value is not null
2328 cright
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, cright
);
2334 private string generate_struct_equal_function (Struct st
) {
2335 string equal_func
= "_%sequal".printf (st
.get_lower_case_cprefix ());
2337 if (!add_wrapper (equal_func
)) {
2338 // wrapper already defined
2342 var function
= new
CCodeFunction (equal_func
, "gboolean");
2343 function
.modifiers
= CCodeModifiers
.STATIC
;
2345 function
.add_parameter (new
CCodeParameter ("s1", "const " + st
.get_cname () + "*"));
2346 function
.add_parameter (new
CCodeParameter ("s2", "const " + st
.get_cname () + "*"));
2348 push_function (function
);
2350 // if (s1 == s2) return TRUE;
2352 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeIdentifier ("s2"));
2353 ccode
.open_if (cexp
);
2354 ccode
.add_return (new
CCodeConstant ("TRUE"));
2357 // if (s1 == NULL || s2 == NULL) return FALSE;
2359 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeConstant ("NULL"));
2360 ccode
.open_if (cexp
);
2361 ccode
.add_return (new
CCodeConstant ("FALSE"));
2364 cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s2"), new
CCodeConstant ("NULL"));
2365 ccode
.open_if (cexp
);
2366 ccode
.add_return (new
CCodeConstant ("FALSE"));
2370 foreach (Field f
in st
.get_fields ()) {
2371 if (f
.binding
!= MemberBinding
.INSTANCE
) {
2372 // we only compare instance fields
2376 CCodeExpression cexp
; // if (cexp) return FALSE;
2377 var s1
= (CCodeExpression
) new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("s1"), f
.name
); // s1->f
2378 var s2
= (CCodeExpression
) new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("s2"), f
.name
); // s2->f
2380 var variable_type
= f
.variable_type
.copy ();
2381 make_comparable_cexpression (ref variable_type
, ref s1
, ref variable_type
, ref s2
);
2383 if (!(f
.variable_type is NullType
) && f
.variable_type
.compatible (string_type
)) {
2384 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strcmp0"));
2385 ccall
.add_argument (s1
);
2386 ccall
.add_argument (s2
);
2388 } else if (f
.variable_type is StructValueType
) {
2389 var equalfunc
= generate_struct_equal_function (f
.variable_type
.data_type as Struct
);
2390 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
2391 ccall
.add_argument (s1
);
2392 ccall
.add_argument (s2
);
2393 cexp
= new
CCodeUnaryExpression (CCodeUnaryOperator
.LOGICAL_NEGATION
, ccall
);
2395 cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, s1
, s2
);
2398 ccode
.open_if (cexp
);
2399 ccode
.add_return (new
CCodeConstant ("FALSE"));
2403 if (st
.get_fields().size
== 0) {
2404 // either opaque structure or simple type
2405 if (st
.is_simple_type ()) {
2406 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s1")), new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s2")));
2407 ccode
.add_return (cexp
);
2409 ccode
.add_return (new
CCodeConstant ("FALSE"));
2412 ccode
.add_return (new
CCodeConstant ("TRUE"));
2417 cfile
.add_function_declaration (function
);
2418 cfile
.add_function (function
);
2423 private string generate_numeric_equal_function (Struct st
) {
2424 string equal_func
= "_%sequal".printf (st
.get_lower_case_cprefix ());
2426 if (!add_wrapper (equal_func
)) {
2427 // wrapper already defined
2431 var function
= new
CCodeFunction (equal_func
, "gboolean");
2432 function
.modifiers
= CCodeModifiers
.STATIC
;
2434 function
.add_parameter (new
CCodeParameter ("s1", "const " + st
.get_cname () + "*"));
2435 function
.add_parameter (new
CCodeParameter ("s2", "const " + st
.get_cname () + "*"));
2437 push_function (function
);
2439 // if (s1 == s2) return TRUE;
2441 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeIdentifier ("s2"));
2442 ccode
.open_if (cexp
);
2443 ccode
.add_return (new
CCodeConstant ("TRUE"));
2446 // if (s1 == NULL || s2 == NULL) return FALSE;
2448 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeConstant ("NULL"));
2449 ccode
.open_if (cexp
);
2450 ccode
.add_return (new
CCodeConstant ("FALSE"));
2453 cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s2"), new
CCodeConstant ("NULL"));
2454 ccode
.open_if (cexp
);
2455 ccode
.add_return (new
CCodeConstant ("FALSE"));
2458 // return (*s1 == *s2);
2460 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s1")), new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s2")));
2461 ccode
.add_return (cexp
);
2466 cfile
.add_function_declaration (function
);
2467 cfile
.add_function (function
);
2472 private string generate_struct_dup_wrapper (ValueType value_type
) {
2473 string dup_func
= "_%sdup".printf (value_type
.type_symbol
.get_lower_case_cprefix ());
2475 if (!add_wrapper (dup_func
)) {
2476 // wrapper already defined
2480 var function
= new
CCodeFunction (dup_func
, value_type
.get_cname ());
2481 function
.modifiers
= CCodeModifiers
.STATIC
;
2483 function
.add_parameter (new
CCodeParameter ("self", value_type
.get_cname ()));
2485 push_function (function
);
2487 if (value_type
.type_symbol
== gvalue_type
) {
2488 var dup_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_boxed_copy"));
2489 dup_call
.add_argument (new
CCodeIdentifier ("G_TYPE_VALUE"));
2490 dup_call
.add_argument (new
CCodeIdentifier ("self"));
2492 ccode
.add_return (dup_call
);
2494 ccode
.add_declaration (value_type
.get_cname (), new
CCodeVariableDeclarator ("dup"));
2496 var creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_new0"));
2497 creation_call
.add_argument (new
CCodeConstant (value_type
.data_type
.get_cname ()));
2498 creation_call
.add_argument (new
CCodeConstant ("1"));
2499 ccode
.add_assignment (new
CCodeIdentifier ("dup"), creation_call
);
2501 var st
= value_type
.data_type as Struct
;
2502 if (st
!= null && st
.is_disposable ()) {
2503 if (!st
.has_copy_function
) {
2504 generate_struct_copy_function (st
);
2507 var copy_call
= new
CCodeFunctionCall (new
CCodeIdentifier (st
.get_copy_function ()));
2508 copy_call
.add_argument (new
CCodeIdentifier ("self"));
2509 copy_call
.add_argument (new
CCodeIdentifier ("dup"));
2510 ccode
.add_expression (copy_call
);
2512 cfile
.add_include ("string.h");
2514 var sizeof_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
2515 sizeof_call
.add_argument (new
CCodeConstant (value_type
.data_type
.get_cname ()));
2517 var copy_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memcpy"));
2518 copy_call
.add_argument (new
CCodeIdentifier ("dup"));
2519 copy_call
.add_argument (new
CCodeIdentifier ("self"));
2520 copy_call
.add_argument (sizeof_call
);
2521 ccode
.add_expression (copy_call
);
2524 ccode
.add_return (new
CCodeIdentifier ("dup"));
2529 cfile
.add_function_declaration (function
);
2530 cfile
.add_function (function
);
2535 protected string generate_dup_func_wrapper (DataType type
) {
2536 string destroy_func
= "_vala_%s_copy".printf (type
.data_type
.get_cname ());
2538 if (!add_wrapper (destroy_func
)) {
2539 // wrapper already defined
2540 return destroy_func
;
2543 var function
= new
CCodeFunction (destroy_func
, type
.get_cname ());
2544 function
.modifiers
= CCodeModifiers
.STATIC
;
2545 function
.add_parameter (new
CCodeParameter ("self", type
.get_cname ()));
2547 push_function (function
);
2549 var cl
= type
.data_type as Class
;
2550 assert (cl
!= null && cl
.is_gboxed
);
2552 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_boxed_copy"));
2553 free_call
.add_argument (new
CCodeIdentifier (cl
.get_type_id ()));
2554 free_call
.add_argument (new
CCodeIdentifier ("self"));
2556 ccode
.add_return (free_call
);
2560 cfile
.add_function_declaration (function
);
2561 cfile
.add_function (function
);
2563 return destroy_func
;
2566 protected string generate_free_func_wrapper (DataType type
) {
2567 string destroy_func
= "_vala_%s_free".printf (type
.data_type
.get_cname ());
2569 if (!add_wrapper (destroy_func
)) {
2570 // wrapper already defined
2571 return destroy_func
;
2574 var function
= new
CCodeFunction (destroy_func
, "void");
2575 function
.modifiers
= CCodeModifiers
.STATIC
;
2576 function
.add_parameter (new
CCodeParameter ("self", type
.get_cname ()));
2578 push_function (function
);
2580 var cl
= type
.data_type as Class
;
2581 if (cl
!= null && cl
.is_gboxed
) {
2582 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_boxed_free"));
2583 free_call
.add_argument (new
CCodeIdentifier (cl
.get_type_id ()));
2584 free_call
.add_argument (new
CCodeIdentifier ("self"));
2586 ccode
.add_expression (free_call
);
2587 } else if (cl
!= null) {
2588 assert (cl
.free_function_address_of
);
2590 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier (type
.data_type
.get_free_function ()));
2591 free_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new
CCodeIdentifier ("self")));
2593 ccode
.add_expression (free_call
);
2595 var st
= type
.data_type as Struct
;
2596 if (st
!= null && st
.is_disposable ()) {
2597 if (!st
.has_destroy_function
) {
2598 generate_struct_destroy_function (st
);
2601 var destroy_call
= new
CCodeFunctionCall (new
CCodeIdentifier (st
.get_destroy_function ()));
2602 destroy_call
.add_argument (new
CCodeIdentifier ("self"));
2603 ccode
.add_expression (destroy_call
);
2606 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_free"));
2607 free_call
.add_argument (new
CCodeIdentifier ("self"));
2609 ccode
.add_expression (free_call
);
2614 cfile
.add_function_declaration (function
);
2615 cfile
.add_function (function
);
2617 return destroy_func
;
2620 public CCodeExpression?
get_destroy0_func_expression (DataType type
, bool is_chainup
= false) {
2621 var element_destroy_func_expression
= get_destroy_func_expression (type
, is_chainup
);
2623 if (element_destroy_func_expression is CCodeIdentifier
) {
2624 var freeid
= (CCodeIdentifier
) element_destroy_func_expression
;
2625 string free0_func
= "_%s0_".printf (freeid
.name
);
2627 if (add_wrapper (free0_func
)) {
2628 var function
= new
CCodeFunction (free0_func
, "void");
2629 function
.modifiers
= CCodeModifiers
.STATIC
;
2631 function
.add_parameter (new
CCodeParameter ("var", "gpointer"));
2633 push_function (function
);
2635 ccode
.add_expression (get_unref_expression (new
CCodeIdentifier ("var"), type
, null, true));
2639 cfile
.add_function_declaration (function
);
2640 cfile
.add_function (function
);
2643 element_destroy_func_expression
= new
CCodeIdentifier (free0_func
);
2646 return element_destroy_func_expression
;
2649 public CCodeExpression?
get_destroy_func_expression (DataType type
, bool is_chainup
= false) {
2650 if (context
.profile
== Profile
.GOBJECT
&& (type
.data_type
== glist_type
|| type
.data_type
== gslist_type
|| type
.data_type
== gnode_type
)) {
2651 // create wrapper function to free list elements if necessary
2653 bool elements_require_free
= false;
2654 CCodeExpression element_destroy_func_expression
= null;
2656 foreach (DataType type_arg
in type
.get_type_arguments ()) {
2657 elements_require_free
= requires_destroy (type_arg
);
2658 if (elements_require_free
) {
2659 element_destroy_func_expression
= get_destroy0_func_expression (type_arg
);
2663 if (elements_require_free
&& element_destroy_func_expression is CCodeIdentifier
) {
2664 return new
CCodeIdentifier (generate_collection_free_wrapper (type
, (CCodeIdentifier
) element_destroy_func_expression
));
2666 return new
CCodeIdentifier (type
.data_type
.get_free_function ());
2668 } else if (type is ErrorType
) {
2669 return new
CCodeIdentifier ("g_error_free");
2670 } else if (type
.data_type
!= null) {
2671 string unref_function
;
2672 if (type is ReferenceType
) {
2673 if (type
.data_type
.is_reference_counting ()) {
2674 unref_function
= type
.data_type
.get_unref_function ();
2675 if (type
.data_type is Interface
&& unref_function
== null) {
2676 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 ()));
2680 var cl
= type
.data_type as Class
;
2681 if (cl
!= null && (cl
.free_function_address_of
|| cl
.is_gboxed
)) {
2682 unref_function
= generate_free_func_wrapper (type
);
2684 unref_function
= type
.data_type
.get_free_function ();
2688 if (type
.nullable
) {
2689 unref_function
= type
.data_type
.get_free_function ();
2690 if (unref_function
== null) {
2691 if (type
.data_type is Struct
&& ((Struct
) type
.data_type
).is_disposable ()) {
2692 unref_function
= generate_free_func_wrapper (type
);
2694 unref_function
= "g_free";
2698 var st
= (Struct
) type
.data_type
;
2699 if (!st
.has_destroy_function
) {
2700 generate_struct_destroy_function (st
);
2702 unref_function
= st
.get_destroy_function ();
2705 if (unref_function
== null) {
2706 return new
CCodeConstant ("NULL");
2708 return new
CCodeIdentifier (unref_function
);
2709 } else if (type
.type_parameter
!= null && current_type_symbol is Class
) {
2710 string func_name
= "%s_destroy_func".printf (type
.type_parameter
.name
.down ());
2711 if (is_in_generic_type (type
) && !is_chainup
&& !in_creation_method
) {
2712 return new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (get_result_cexpression ("self"), "priv"), func_name
);
2714 return new
CCodeIdentifier (func_name
);
2716 } else if (type is ArrayType
) {
2717 if (context
.profile
== Profile
.POSIX
) {
2718 return new
CCodeIdentifier ("free");
2720 return new
CCodeIdentifier ("g_free");
2722 } else if (type is PointerType
) {
2723 if (context
.profile
== Profile
.POSIX
) {
2724 return new
CCodeIdentifier ("free");
2726 return new
CCodeIdentifier ("g_free");
2729 return new
CCodeConstant ("NULL");
2733 private string generate_collection_free_wrapper (DataType collection_type
, CCodeIdentifier element_destroy_func_expression
) {
2734 string destroy_func
= "_%s_%s".printf (collection_type
.data_type
.get_free_function (), element_destroy_func_expression
.name
);
2736 if (!add_wrapper (destroy_func
)) {
2737 // wrapper already defined
2738 return destroy_func
;
2741 var function
= new
CCodeFunction (destroy_func
, "void");
2742 function
.modifiers
= CCodeModifiers
.STATIC
;
2744 function
.add_parameter (new
CCodeParameter ("self", collection_type
.get_cname ()));
2746 push_function (function
);
2748 CCodeFunctionCall element_free_call
;
2749 if (collection_type
.data_type
== gnode_type
) {
2750 /* A wrapper which converts GNodeTraverseFunc into GDestroyNotify */
2751 string destroy_node_func
= "%s_node".printf (destroy_func
);
2752 var wrapper
= new
CCodeFunction (destroy_node_func
, "gboolean");
2753 wrapper
.modifiers
= CCodeModifiers
.STATIC
;
2754 wrapper
.add_parameter (new
CCodeParameter ("node", collection_type
.get_cname ()));
2755 wrapper
.add_parameter (new
CCodeParameter ("unused", "gpointer"));
2756 var wrapper_block
= new
CCodeBlock ();
2757 var free_call
= new
CCodeFunctionCall (element_destroy_func_expression
);
2758 free_call
.add_argument (new CCodeMemberAccess
.pointer(new
CCodeIdentifier("node"), "data"));
2759 wrapper_block
.add_statement (new
CCodeExpressionStatement (free_call
));
2760 wrapper_block
.add_statement (new
CCodeReturnStatement (new
CCodeConstant ("FALSE")));
2761 cfile
.add_function_declaration (function
);
2762 wrapper
.block
= wrapper_block
;
2763 cfile
.add_function (wrapper
);
2765 /* Now the code to call g_traverse with the above */
2766 element_free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_node_traverse"));
2767 element_free_call
.add_argument (new
CCodeIdentifier("self"));
2768 element_free_call
.add_argument (new
CCodeConstant ("G_POST_ORDER"));
2769 element_free_call
.add_argument (new
CCodeConstant ("G_TRAVERSE_ALL"));
2770 element_free_call
.add_argument (new
CCodeConstant ("-1"));
2771 element_free_call
.add_argument (new
CCodeIdentifier (destroy_node_func
));
2772 element_free_call
.add_argument (new
CCodeConstant ("NULL"));
2774 if (collection_type
.data_type
== glist_type
) {
2775 element_free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_list_foreach"));
2777 element_free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_slist_foreach"));
2780 element_free_call
.add_argument (new
CCodeIdentifier ("self"));
2781 element_free_call
.add_argument (new
CCodeCastExpression (element_destroy_func_expression
, "GFunc"));
2782 element_free_call
.add_argument (new
CCodeConstant ("NULL"));
2785 ccode
.add_expression (element_free_call
);
2787 var cfreecall
= new
CCodeFunctionCall (new
CCodeIdentifier (collection_type
.data_type
.get_free_function ()));
2788 cfreecall
.add_argument (new
CCodeIdentifier ("self"));
2789 ccode
.add_expression (cfreecall
);
2793 cfile
.add_function_declaration (function
);
2794 cfile
.add_function (function
);
2796 return destroy_func
;
2799 public virtual string?
append_struct_array_free (Struct st
) {
2803 // logic in this method is temporarily duplicated in destroy_value
2804 // apply changes to both methods
2805 public virtual CCodeExpression
destroy_variable (Variable variable
, TargetValue target_lvalue
) {
2806 var type
= variable
.variable_type
;
2807 var cvar
= get_cvalue_ (target_lvalue
);
2809 if (type is DelegateType
) {
2810 var delegate_target
= get_delegate_target_cvalue (target_lvalue
);
2811 var delegate_target_destroy_notify
= get_delegate_target_destroy_notify_cvalue (target_lvalue
);
2813 var ccall
= new
CCodeFunctionCall (delegate_target_destroy_notify
);
2814 ccall
.add_argument (delegate_target
);
2816 var destroy_call
= new
CCodeCommaExpression ();
2817 destroy_call
.append_expression (ccall
);
2818 destroy_call
.append_expression (new
CCodeConstant ("NULL"));
2820 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, delegate_target_destroy_notify
, new
CCodeConstant ("NULL"));
2822 var ccomma
= new
CCodeCommaExpression ();
2823 ccomma
.append_expression (new
CCodeConditionalExpression (cisnull
, new
CCodeConstant ("NULL"), destroy_call
));
2824 ccomma
.append_expression (new
CCodeAssignment (cvar
, new
CCodeConstant ("NULL")));
2825 ccomma
.append_expression (new
CCodeAssignment (delegate_target
, new
CCodeConstant ("NULL")));
2826 ccomma
.append_expression (new
CCodeAssignment (delegate_target_destroy_notify
, new
CCodeConstant ("NULL")));
2831 var ccall
= new
CCodeFunctionCall (get_destroy_func_expression (type
));
2833 if (type is ValueType
&& !type
.nullable
) {
2834 // normal value type, no null check
2835 var st
= type
.data_type as Struct
;
2836 if (st
!= null && st
.is_simple_type ()) {
2838 ccall
.add_argument (cvar
);
2840 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cvar
));
2843 if (gvalue_type
!= null && type
.data_type
== gvalue_type
) {
2844 // g_value_unset must not be called for already unset values
2845 var cisvalid
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_IS_VALUE"));
2846 cisvalid
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cvar
));
2848 var ccomma
= new
CCodeCommaExpression ();
2849 ccomma
.append_expression (ccall
);
2850 ccomma
.append_expression (new
CCodeConstant ("NULL"));
2852 return new
CCodeConditionalExpression (cisvalid
, ccomma
, new
CCodeConstant ("NULL"));
2858 if (ccall
.call is CCodeIdentifier
&& !(type is ArrayType
)) {
2859 // generate and use NULL-aware free macro to simplify code
2861 var freeid
= (CCodeIdentifier
) ccall
.call
;
2862 string free0_func
= "_%s0".printf (freeid
.name
);
2864 if (add_wrapper (free0_func
)) {
2865 var macro
= destroy_value (new
GLibValue (type
, new
CCodeIdentifier ("var")), true);
2866 cfile
.add_type_declaration (new CCodeMacroReplacement
.with_expression ("%s(var)".printf (free0_func
), macro
));
2869 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (free0_func
));
2870 ccall
.add_argument (cvar
);
2874 /* (foo == NULL ? NULL : foo = (unref (foo), NULL)) */
2876 /* can be simplified to
2877 * foo = (unref (foo), NULL)
2878 * if foo is of static type non-null
2881 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, cvar
, new
CCodeConstant ("NULL"));
2882 if (type
.type_parameter
!= null) {
2883 if (!(current_type_symbol is Class
) || current_class
.is_compact
) {
2884 return new
CCodeConstant ("NULL");
2887 // unref functions are optional for type parameters
2888 var cunrefisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, get_destroy_func_expression (type
), new
CCodeConstant ("NULL"));
2889 cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.OR
, cisnull
, cunrefisnull
);
2892 ccall
.add_argument (cvar
);
2894 /* set freed references to NULL to prevent further use */
2895 var ccomma
= new
CCodeCommaExpression ();
2897 if (context
.profile
== Profile
.GOBJECT
) {
2898 if (type
.data_type
!= null && !type
.data_type
.is_reference_counting () &&
2899 (type
.data_type
== gstringbuilder_type
2900 || type
.data_type
== garray_type
2901 || type
.data_type
== gbytearray_type
2902 || type
.data_type
== gptrarray_type
)) {
2903 ccall
.add_argument (new
CCodeConstant ("TRUE"));
2904 } else if (type
.data_type
== gthreadpool_type
) {
2905 ccall
.add_argument (new
CCodeConstant ("FALSE"));
2906 ccall
.add_argument (new
CCodeConstant ("TRUE"));
2907 } else if (type is ArrayType
) {
2908 var array_type
= (ArrayType
) type
;
2909 if (requires_destroy (array_type
.element_type
)) {
2910 CCodeExpression csizeexpr
= null;
2911 if (variable
.array_null_terminated
) {
2912 var len_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("_vala_array_length"));
2913 len_call
.add_argument (cvar
);
2914 csizeexpr
= len_call
;
2915 } else if (variable
.has_array_length_cexpr
) {
2916 csizeexpr
= new
CCodeConstant (variable
.get_array_length_cexpr ());
2917 } else if (!variable
.no_array_length
) {
2919 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
2921 csizeexpr
= get_array_length_cvalue (target_lvalue
, dim
);
2924 csizeexpr
= new
CCodeBinaryExpression (CCodeBinaryOperator
.MUL
, csizeexpr
, get_array_length_cvalue (target_lvalue
, dim
));
2929 if (csizeexpr
!= null) {
2930 var st
= array_type
.element_type
.data_type as Struct
;
2931 if (st
!= null && !array_type
.element_type
.nullable
) {
2932 ccall
.call
= new
CCodeIdentifier (append_struct_array_free (st
));
2933 ccall
.add_argument (csizeexpr
);
2935 requires_array_free
= true;
2936 ccall
.call
= new
CCodeIdentifier ("_vala_array_free");
2937 ccall
.add_argument (csizeexpr
);
2938 ccall
.add_argument (new
CCodeCastExpression (get_destroy_func_expression (array_type
.element_type
), "GDestroyNotify"));
2945 ccomma
.append_expression (ccall
);
2946 ccomma
.append_expression (new
CCodeConstant ("NULL"));
2948 var cassign
= new
CCodeAssignment (cvar
, ccomma
);
2950 // g_free (NULL) is allowed
2951 bool uses_gfree
= (type
.data_type
!= null && !type
.data_type
.is_reference_counting () && type
.data_type
.get_free_function () == "g_free");
2952 uses_gfree
= uses_gfree
|| type is ArrayType
;
2957 return new
CCodeConditionalExpression (cisnull
, new
CCodeConstant ("NULL"), cassign
);
2960 public CCodeExpression
destroy_local (LocalVariable local
) {
2961 return destroy_variable (local
, get_local_cvalue (local
));
2964 public CCodeExpression
destroy_parameter (Parameter param
) {
2965 return destroy_variable (param
, get_parameter_cvalue (param
));
2968 public CCodeExpression
destroy_field (Field field
, TargetValue? instance
) {
2969 return destroy_variable (field
, get_field_cvalue (field
, instance
));
2972 public CCodeExpression
get_unref_expression (CCodeExpression cvar
, DataType type
, Expression? expr
, bool is_macro_definition
= false) {
2974 if (expr
.symbol_reference is LocalVariable
) {
2975 return destroy_local ((LocalVariable
) expr
.symbol_reference
);
2976 } else if (expr
.symbol_reference is Parameter
) {
2977 return destroy_parameter ((Parameter
) expr
.symbol_reference
);
2980 var value
= new
GLibValue (type
, cvar
);
2981 if (expr
!= null && expr
.target_value
!= null) {
2982 value
.array_length_cvalues
= ((GLibValue
) expr
.target_value
).array_length_cvalues
;
2983 value
.delegate_target_cvalue
= get_delegate_target_cvalue (expr
.target_value
);
2984 value
.delegate_target_destroy_notify_cvalue
= get_delegate_target_destroy_notify_cvalue (expr
.target_value
);
2986 return destroy_value (value
, is_macro_definition
);
2989 // logic in this method is temporarily duplicated in destroy_variable
2990 // apply changes to both methods
2991 public virtual CCodeExpression
destroy_value (TargetValue value
, bool is_macro_definition
= false) {
2992 var type
= value
.value_type
;
2993 var cvar
= get_cvalue_ (value
);
2995 if (type is DelegateType
) {
2996 var delegate_target
= get_delegate_target_cvalue (value
);
2997 var delegate_target_destroy_notify
= get_delegate_target_destroy_notify_cvalue (value
);
2999 var ccall
= new
CCodeFunctionCall (delegate_target_destroy_notify
);
3000 ccall
.add_argument (delegate_target
);
3002 var destroy_call
= new
CCodeCommaExpression ();
3003 destroy_call
.append_expression (ccall
);
3004 destroy_call
.append_expression (new
CCodeConstant ("NULL"));
3006 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, delegate_target_destroy_notify
, new
CCodeConstant ("NULL"));
3008 var ccomma
= new
CCodeCommaExpression ();
3009 ccomma
.append_expression (new
CCodeConditionalExpression (cisnull
, new
CCodeConstant ("NULL"), destroy_call
));
3010 ccomma
.append_expression (new
CCodeAssignment (cvar
, new
CCodeConstant ("NULL")));
3011 ccomma
.append_expression (new
CCodeAssignment (delegate_target
, new
CCodeConstant ("NULL")));
3012 ccomma
.append_expression (new
CCodeAssignment (delegate_target_destroy_notify
, new
CCodeConstant ("NULL")));
3017 var ccall
= new
CCodeFunctionCall (get_destroy_func_expression (type
));
3019 if (type is ValueType
&& !type
.nullable
) {
3020 // normal value type, no null check
3021 var st
= type
.data_type as Struct
;
3022 if (st
!= null && st
.is_simple_type ()) {
3024 ccall
.add_argument (cvar
);
3026 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cvar
));
3029 if (gvalue_type
!= null && type
.data_type
== gvalue_type
) {
3030 // g_value_unset must not be called for already unset values
3031 var cisvalid
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_IS_VALUE"));
3032 cisvalid
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cvar
));
3034 var ccomma
= new
CCodeCommaExpression ();
3035 ccomma
.append_expression (ccall
);
3036 ccomma
.append_expression (new
CCodeConstant ("NULL"));
3038 return new
CCodeConditionalExpression (cisvalid
, ccomma
, new
CCodeConstant ("NULL"));
3044 if (ccall
.call is CCodeIdentifier
&& !(type is ArrayType
) && !is_macro_definition
) {
3045 // generate and use NULL-aware free macro to simplify code
3047 var freeid
= (CCodeIdentifier
) ccall
.call
;
3048 string free0_func
= "_%s0".printf (freeid
.name
);
3050 if (add_wrapper (free0_func
)) {
3051 var macro
= destroy_value (new
GLibValue (type
, new
CCodeIdentifier ("var")), true);
3052 cfile
.add_type_declaration (new CCodeMacroReplacement
.with_expression ("%s(var)".printf (free0_func
), macro
));
3055 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (free0_func
));
3056 ccall
.add_argument (cvar
);
3060 /* (foo == NULL ? NULL : foo = (unref (foo), NULL)) */
3062 /* can be simplified to
3063 * foo = (unref (foo), NULL)
3064 * if foo is of static type non-null
3067 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, cvar
, new
CCodeConstant ("NULL"));
3068 if (type
.type_parameter
!= null) {
3069 if (!(current_type_symbol is Class
) || current_class
.is_compact
) {
3070 return new
CCodeConstant ("NULL");
3073 // unref functions are optional for type parameters
3074 var cunrefisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, get_destroy_func_expression (type
), new
CCodeConstant ("NULL"));
3075 cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.OR
, cisnull
, cunrefisnull
);
3078 ccall
.add_argument (cvar
);
3080 /* set freed references to NULL to prevent further use */
3081 var ccomma
= new
CCodeCommaExpression ();
3083 if (context
.profile
== Profile
.GOBJECT
) {
3084 if (type
.data_type
!= null && !type
.data_type
.is_reference_counting () &&
3085 (type
.data_type
== gstringbuilder_type
3086 || type
.data_type
== garray_type
3087 || type
.data_type
== gbytearray_type
3088 || type
.data_type
== gptrarray_type
)) {
3089 ccall
.add_argument (new
CCodeConstant ("TRUE"));
3090 } else if (type
.data_type
== gthreadpool_type
) {
3091 ccall
.add_argument (new
CCodeConstant ("FALSE"));
3092 ccall
.add_argument (new
CCodeConstant ("TRUE"));
3093 } else if (type is ArrayType
) {
3094 var array_type
= (ArrayType
) type
;
3095 if (requires_destroy (array_type
.element_type
)) {
3096 CCodeExpression csizeexpr
= null;
3098 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
3100 csizeexpr
= get_array_length_cvalue (value
, dim
);
3103 csizeexpr
= new
CCodeBinaryExpression (CCodeBinaryOperator
.MUL
, csizeexpr
, get_array_length_cvalue (value
, dim
));
3107 var st
= array_type
.element_type
.data_type as Struct
;
3108 if (st
!= null && !array_type
.element_type
.nullable
) {
3109 ccall
.call
= new
CCodeIdentifier (append_struct_array_free (st
));
3110 ccall
.add_argument (csizeexpr
);
3112 requires_array_free
= true;
3113 ccall
.call
= new
CCodeIdentifier ("_vala_array_free");
3114 ccall
.add_argument (csizeexpr
);
3115 ccall
.add_argument (new
CCodeCastExpression (get_destroy_func_expression (array_type
.element_type
), "GDestroyNotify"));
3121 ccomma
.append_expression (ccall
);
3122 ccomma
.append_expression (new
CCodeConstant ("NULL"));
3124 var cassign
= new
CCodeAssignment (cvar
, ccomma
);
3126 // g_free (NULL) is allowed
3127 bool uses_gfree
= (type
.data_type
!= null && !type
.data_type
.is_reference_counting () && type
.data_type
.get_free_function () == "g_free");
3128 uses_gfree
= uses_gfree
|| type is ArrayType
;
3133 return new
CCodeConditionalExpression (cisnull
, new
CCodeConstant ("NULL"), cassign
);
3136 public override void visit_end_full_expression (Expression expr
) {
3137 /* expr is a full expression, i.e. an initializer, the
3138 * expression in an expression statement, the controlling
3139 * expression in if, while, for, or foreach statements
3141 * we unref temporary variables at the end of a full
3144 if (temp_ref_vars
.size
== 0) {
3145 /* nothing to do without temporary variables */
3149 LocalVariable full_expr_var
= null;
3151 var local_decl
= expr
.parent_node as LocalVariable
;
3152 if (!(local_decl
!= null && has_simple_struct_initializer (local_decl
))) {
3153 var expr_type
= expr
.value_type
;
3154 if (expr
.target_type
!= null) {
3155 expr_type
= expr
.target_type
;
3158 full_expr_var
= get_temp_variable (expr_type
, true, expr
, false);
3159 emit_temp_var (full_expr_var
);
3161 ccode
.add_assignment (get_variable_cexpression (full_expr_var
.name
), get_cvalue (expr
));
3164 foreach (LocalVariable local
in temp_ref_vars
) {
3165 ccode
.add_expression (destroy_local (local
));
3168 if (full_expr_var
!= null) {
3169 set_cvalue (expr
, get_variable_cexpression (full_expr_var
.name
));
3172 temp_ref_vars
.clear ();
3175 public void emit_temp_var (LocalVariable local
, bool always_init
= false) {
3176 var vardecl
= new
CCodeVariableDeclarator (local
.name
, null, local
.variable_type
.get_cdeclarator_suffix ());
3178 var st
= local
.variable_type
.data_type as Struct
;
3179 var array_type
= local
.variable_type as ArrayType
;
3181 if (local
.name
.has_prefix ("*")) {
3182 // do not dereference unintialized variable
3183 // initialization is not needed for these special
3184 // pointer temp variables
3185 // used to avoid side-effects in assignments
3186 } else if (local
.no_init
) {
3187 // no initialization necessary for this temp var
3188 } else if (!local
.variable_type
.nullable
&&
3189 (st
!= null && !st
.is_simple_type ()) ||
3190 (array_type
!= null && array_type
.fixed_length
)) {
3191 // 0-initialize struct with struct initializer { 0 }
3192 // necessary as they will be passed by reference
3193 var clist
= new
CCodeInitializerList ();
3194 clist
.append (new
CCodeConstant ("0"));
3196 vardecl
.initializer
= clist
;
3197 vardecl
.init0
= true;
3198 } else if (local
.variable_type
.is_reference_type_or_type_parameter () ||
3199 local
.variable_type
.nullable
||
3200 local
.variable_type is DelegateType
) {
3201 vardecl
.initializer
= new
CCodeConstant ("NULL");
3202 vardecl
.init0
= true;
3203 } else if (always_init
) {
3204 vardecl
.initializer
= default_value_for_type (local
.variable_type
, true);
3205 vardecl
.init0
= true;
3208 if (is_in_coroutine ()) {
3209 closure_struct
.add_field (local
.variable_type
.get_cname (), local
.name
);
3211 // even though closure struct is zerod, we need to initialize temporary variables
3212 // as they might be used multiple times when declared in a loop
3214 if (vardecl
.initializer is CCodeInitializerList
) {
3215 // C does not support initializer lists in assignments, use memset instead
3216 cfile
.add_include ("string.h");
3217 var memset_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memset"));
3218 memset_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (local
.name
)));
3219 memset_call
.add_argument (new
CCodeConstant ("0"));
3220 memset_call
.add_argument (new
CCodeIdentifier ("sizeof (%s)".printf (local
.variable_type
.get_cname ())));
3221 ccode
.add_expression (memset_call
);
3222 } else if (vardecl
.initializer
!= null) {
3223 ccode
.add_assignment (get_variable_cexpression (local
.name
), vardecl
.initializer
);
3226 ccode
.add_declaration (local
.variable_type
.get_cname (), vardecl
);
3230 public override void visit_expression_statement (ExpressionStatement stmt
) {
3231 if (stmt
.expression
.error
) {
3236 /* free temporary objects and handle errors */
3238 foreach (LocalVariable local
in temp_ref_vars
) {
3239 ccode
.add_expression (destroy_local (local
));
3242 if (stmt
.tree_can_fail
&& stmt
.expression
.tree_can_fail
) {
3243 // simple case, no node breakdown necessary
3244 add_simple_check (stmt
.expression
);
3247 temp_ref_vars
.clear ();
3250 public virtual void append_local_free (Symbol sym
, bool stop_at_loop
= false, CodeNode? stop_at
= null) {
3251 var b
= (Block
) sym
;
3253 var local_vars
= b
.get_local_variables ();
3254 // free in reverse order
3255 for (int i
= local_vars
.size
- 1; i
>= 0; i
--) {
3256 var local
= local_vars
[i
];
3257 if (!local
.unreachable
&& local
.active
&& !local
.floating
&& !local
.captured
&& requires_destroy (local
.variable_type
)) {
3258 ccode
.add_expression (destroy_local (local
));
3263 int block_id
= get_block_id (b
);
3265 var data_unref
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_unref".printf (block_id
)));
3266 data_unref
.add_argument (get_variable_cexpression ("_data%d_".printf (block_id
)));
3267 ccode
.add_expression (data_unref
);
3268 ccode
.add_assignment (get_variable_cexpression ("_data%d_".printf (block_id
)), new
CCodeConstant ("NULL"));
3272 if (b
.parent_node is Loop
||
3273 b
.parent_node is ForeachStatement
||
3274 b
.parent_node is SwitchStatement
) {
3279 if (stop_at
!= null && b
.parent_node
== stop_at
) {
3283 if (sym
.parent_symbol is Block
) {
3284 append_local_free (sym
.parent_symbol
, stop_at_loop
, stop_at
);
3285 } else if (sym
.parent_symbol is Method
) {
3286 append_param_free ((Method
) sym
.parent_symbol
);
3290 private void append_param_free (Method m
) {
3291 foreach (Parameter param
in m
.get_parameters ()) {
3292 if (!param
.ellipsis
&& requires_destroy (param
.variable_type
) && param
.direction
== ParameterDirection
.IN
) {
3293 ccode
.add_expression (destroy_parameter (param
));
3298 public bool variable_accessible_in_finally (LocalVariable local
) {
3299 if (current_try
== null) {
3303 var sym
= current_symbol
;
3305 while (!(sym is Method
|| sym is PropertyAccessor
) && sym
.scope
.lookup (local
.name
) == null) {
3306 if ((sym
.parent_node is TryStatement
&& ((TryStatement
) sym
.parent_node
).finally_body
!= null) ||
3307 (sym
.parent_node is CatchClause
&& ((TryStatement
) sym
.parent_node
.parent_node
).finally_body
!= null)) {
3312 sym
= sym
.parent_symbol
;
3318 void return_out_parameter (Parameter param
) {
3319 var delegate_type
= param
.variable_type as DelegateType
;
3321 ccode
.open_if (get_variable_cexpression (param
.name
));
3322 ccode
.add_assignment (new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_variable_cexpression (param
.name
)), get_variable_cexpression ("_" + param
.name
));
3324 if (delegate_type
!= null && delegate_type
.delegate_symbol
.has_target
) {
3325 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
))));
3326 if (delegate_type
.value_owned
) {
3327 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
))));
3331 if (param
.variable_type
.is_disposable ()){
3333 ccode
.add_expression (destroy_parameter (param
));
3337 var array_type
= param
.variable_type as ArrayType
;
3338 if (array_type
!= null && !array_type
.fixed_length
&& !param
.no_array_length
) {
3339 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
3340 ccode
.open_if (get_variable_cexpression (get_parameter_array_length_cname (param
, dim
)));
3341 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
)));
3347 public override void visit_return_statement (ReturnStatement stmt
) {
3348 Symbol return_expression_symbol
= null;
3350 if (stmt
.return_expression
!= null) {
3351 // avoid unnecessary ref/unref pair
3352 var local
= stmt
.return_expression
.symbol_reference as LocalVariable
;
3353 if (current_return_type
.value_owned
3354 && local
!= null && local
.variable_type
.value_owned
3356 && !variable_accessible_in_finally (local
)) {
3357 /* return expression is local variable taking ownership and
3358 * current method is transferring ownership */
3360 return_expression_symbol
= local
;
3364 // return array length if appropriate
3365 if (((current_method
!= null && !current_method
.no_array_length
) || current_property_accessor
!= null) && current_return_type is ArrayType
) {
3366 var return_expr_decl
= get_temp_variable (stmt
.return_expression
.value_type
, true, stmt
, false);
3368 ccode
.add_assignment (get_variable_cexpression (return_expr_decl
.name
), get_cvalue (stmt
.return_expression
));
3370 var array_type
= (ArrayType
) current_return_type
;
3372 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
3373 var len_l
= get_result_cexpression (get_array_length_cname ("result", dim
));
3374 if (!is_in_coroutine ()) {
3375 len_l
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, len_l
);
3377 var len_r
= get_array_length_cexpression (stmt
.return_expression
, dim
);
3378 ccode
.add_assignment (len_l
, len_r
);
3381 set_cvalue (stmt
.return_expression
, get_variable_cexpression (return_expr_decl
.name
));
3383 emit_temp_var (return_expr_decl
);
3384 } else if ((current_method
!= null || current_property_accessor
!= null) && current_return_type is DelegateType
) {
3385 var delegate_type
= (DelegateType
) current_return_type
;
3386 if (delegate_type
.delegate_symbol
.has_target
) {
3387 var return_expr_decl
= get_temp_variable (stmt
.return_expression
.value_type
, true, stmt
, false);
3389 ccode
.add_assignment (get_variable_cexpression (return_expr_decl
.name
), get_cvalue (stmt
.return_expression
));
3391 var target_l
= get_result_cexpression (get_delegate_target_cname ("result"));
3392 if (!is_in_coroutine ()) {
3393 target_l
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, target_l
);
3395 CCodeExpression target_r_destroy_notify
;
3396 var target_r
= get_delegate_target_cexpression (stmt
.return_expression
, out target_r_destroy_notify
);
3397 ccode
.add_assignment (target_l
, target_r
);
3398 if (delegate_type
.value_owned
) {
3399 var target_l_destroy_notify
= get_result_cexpression (get_delegate_target_destroy_notify_cname ("result"));
3400 if (!is_in_coroutine ()) {
3401 target_l_destroy_notify
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, target_l_destroy_notify
);
3403 ccode
.add_assignment (target_l_destroy_notify
, target_r_destroy_notify
);
3406 set_cvalue (stmt
.return_expression
, get_variable_cexpression (return_expr_decl
.name
));
3408 emit_temp_var (return_expr_decl
);
3412 if (stmt
.return_expression
!= null) {
3413 // assign method result to `result'
3414 CCodeExpression result_lhs
= get_result_cexpression ();
3415 if (current_return_type
.is_real_non_null_struct_type () && !is_in_coroutine ()) {
3416 result_lhs
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, result_lhs
);
3418 ccode
.add_assignment (result_lhs
, get_cvalue (stmt
.return_expression
));
3421 // free local variables
3422 append_local_free (current_symbol
);
3424 if (current_method
!= null) {
3425 // check postconditions
3426 foreach (Expression postcondition
in current_method
.get_postconditions ()) {
3427 create_postcondition_statement (postcondition
);
3431 if (current_method
!= null && !current_method
.coroutine
) {
3432 // assign values to output parameters if they are not NULL
3433 // otherwise, free the value if necessary
3434 foreach (var param
in current_method
.get_parameters ()) {
3435 if (param
.direction
!= ParameterDirection
.OUT
) {
3439 return_out_parameter (param
);
3443 if (is_in_constructor ()) {
3444 ccode
.add_return (new
CCodeIdentifier ("obj"));
3445 } else if (is_in_destructor ()) {
3446 // do not call return as member cleanup and chain up to base finalizer
3447 // stil need to be executed
3448 ccode
.add_goto ("_return");
3449 } else if (current_method is CreationMethod
) {
3450 ccode
.add_return (new
CCodeIdentifier ("self"));
3451 } else if (is_in_coroutine ()) {
3452 } else if (current_return_type is VoidType
|| current_return_type
.is_real_non_null_struct_type ()) {
3453 // structs are returned via out parameter
3454 ccode
.add_return ();
3456 ccode
.add_return (new
CCodeIdentifier ("result"));
3459 if (return_expression_symbol
!= null) {
3460 return_expression_symbol
.active
= true;
3463 // required for destructors
3464 current_method_return
= true;
3467 public string get_symbol_lock_name (string symname
) {
3468 return "__lock_%s".printf (symname
);
3471 private CCodeExpression
get_lock_expression (Statement stmt
, Expression resource
) {
3472 CCodeExpression l
= null;
3473 var inner_node
= ((MemberAccess
)resource
).inner
;
3474 var member
= resource
.symbol_reference
;
3475 var parent
= (TypeSymbol
)resource
.symbol_reference
.parent_symbol
;
3477 if (member
.is_instance_member ()) {
3478 if (inner_node
== null) {
3479 l
= new
CCodeIdentifier ("self");
3480 } else if (resource
.symbol_reference
.parent_symbol
!= current_type_symbol
) {
3481 l
= generate_instance_cast (get_cvalue (inner_node
), parent
);
3483 l
= get_cvalue (inner_node
);
3486 l
= new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (l
, "priv"), get_symbol_lock_name (resource
.symbol_reference
.name
));
3487 } else if (member
.is_class_member ()) {
3488 CCodeExpression klass
;
3490 if (current_method
!= null && current_method
.binding
== MemberBinding
.INSTANCE
||
3491 current_property_accessor
!= null && current_property_accessor
.prop
.binding
== MemberBinding
.INSTANCE
||
3492 (in_constructor
&& !in_static_or_class_context
)) {
3493 var k
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_OBJECT_GET_CLASS"));
3494 k
.add_argument (new
CCodeIdentifier ("self"));
3497 klass
= new
CCodeIdentifier ("klass");
3500 var get_class_private_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf(parent
.get_upper_case_cname ())));
3501 get_class_private_call
.add_argument (klass
);
3502 l
= new CCodeMemberAccess
.pointer (get_class_private_call
, get_symbol_lock_name (resource
.symbol_reference
.name
));
3504 string lock_name
= "%s_%s".printf(parent
.get_lower_case_cname (), resource
.symbol_reference
.name
);
3505 l
= new
CCodeIdentifier (get_symbol_lock_name (lock_name
));
3510 public override void visit_lock_statement (LockStatement stmt
) {
3511 var l
= get_lock_expression (stmt
, stmt
.resource
);
3513 var fc
= new
CCodeFunctionCall (new
CCodeIdentifier (((Method
) mutex_type
.scope
.lookup ("lock")).get_cname ()));
3514 fc
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, l
));
3516 ccode
.add_expression (fc
);
3519 public override void visit_unlock_statement (UnlockStatement stmt
) {
3520 var l
= get_lock_expression (stmt
, stmt
.resource
);
3522 var fc
= new
CCodeFunctionCall (new
CCodeIdentifier (((Method
) mutex_type
.scope
.lookup ("unlock")).get_cname ()));
3523 fc
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, l
));
3525 ccode
.add_expression (fc
);
3528 public override void visit_delete_statement (DeleteStatement stmt
) {
3529 var pointer_type
= (PointerType
) stmt
.expression
.value_type
;
3530 DataType type
= pointer_type
;
3531 if (pointer_type
.base_type
.data_type
!= null && pointer_type
.base_type
.data_type
.is_reference_type ()) {
3532 type
= pointer_type
.base_type
;
3535 var ccall
= new
CCodeFunctionCall (get_destroy_func_expression (type
));
3536 ccall
.add_argument (get_cvalue (stmt
.expression
));
3537 ccode
.add_expression (ccall
);
3540 public override void visit_expression (Expression expr
) {
3541 if (get_cvalue (expr
) != null && !expr
.lvalue
) {
3542 if (expr
.formal_value_type is GenericType
&& !(expr
.value_type is GenericType
)) {
3543 var st
= expr
.formal_value_type
.type_parameter
.parent_symbol
.parent_symbol as Struct
;
3544 if (expr
.formal_value_type
.type_parameter
.parent_symbol
!= garray_type
&&
3545 (st
== null || st
.get_cname () != "va_list")) {
3546 // GArray and va_list don't use pointer-based generics
3547 set_cvalue (expr
, convert_from_generic_pointer (get_cvalue (expr
), expr
.value_type
));
3551 // memory management, implicit casts, and boxing/unboxing
3552 set_cvalue (expr
, transform_expression (get_cvalue (expr
), expr
.value_type
, expr
.target_type
, expr
));
3554 if (expr
.formal_target_type is GenericType
&& !(expr
.target_type is GenericType
)) {
3555 if (expr
.formal_target_type
.type_parameter
.parent_symbol
!= garray_type
) {
3556 // GArray doesn't use pointer-based generics
3557 set_cvalue (expr
, convert_to_generic_pointer (get_cvalue (expr
), expr
.target_type
));
3563 public override void visit_boolean_literal (BooleanLiteral expr
) {
3564 if (context
.profile
== Profile
.GOBJECT
) {
3565 set_cvalue (expr
, new
CCodeConstant (expr
.value ?
"TRUE" : "FALSE"));
3567 cfile
.add_include ("stdbool.h");
3568 set_cvalue (expr
, new
CCodeConstant (expr
.value ?
"true" : "false"));
3572 public override void visit_character_literal (CharacterLiteral expr
) {
3573 if (expr
.get_char () >= 0x20 && expr
.get_char () < 0x80) {
3574 set_cvalue (expr
, new
CCodeConstant (expr
.value
));
3576 set_cvalue (expr
, new
CCodeConstant ("%uU".printf (expr
.get_char ())));
3580 public override void visit_integer_literal (IntegerLiteral expr
) {
3581 set_cvalue (expr
, new
CCodeConstant (expr
.value
+ expr
.type_suffix
));
3584 public override void visit_real_literal (RealLiteral expr
) {
3585 string c_literal
= expr
.value
;
3586 if (c_literal
.has_suffix ("d") || c_literal
.has_suffix ("D")) {
3587 // there is no suffix for double in C
3588 c_literal
= c_literal
.substring (0, c_literal
.length
- 1);
3590 if (!("." in c_literal
|| "e" in c_literal
|| "E" in c_literal
)) {
3591 // C requires period or exponent part for floating constants
3592 if ("f" in c_literal
|| "F" in c_literal
) {
3593 c_literal
= c_literal
.substring (0, c_literal
.length
- 1) + ".f";
3598 set_cvalue (expr
, new
CCodeConstant (c_literal
));
3601 public override void visit_string_literal (StringLiteral expr
) {
3602 set_cvalue (expr
, new CCodeConstant
.string (expr
.value
.replace ("\n", "\\n")));
3604 if (expr
.translate
) {
3605 // translated string constant
3607 var m
= (Method
) root_symbol
.scope
.lookup ("GLib").scope
.lookup ("_");
3608 add_symbol_declaration (cfile
, m
, m
.get_cname ());
3610 var translate
= new
CCodeFunctionCall (new
CCodeIdentifier ("_"));
3611 translate
.add_argument (get_cvalue (expr
));
3612 set_cvalue (expr
, translate
);
3616 public override void visit_regex_literal (RegexLiteral expr
) {
3617 string[] parts
= expr
.value
.split ("/", 3);
3618 string re
= parts
[2].escape ("");
3621 if (parts
[1].contains ("i")) {
3622 flags
+= " | G_REGEX_CASELESS";
3624 if (parts
[1].contains ("m")) {
3625 flags
+= " | G_REGEX_MULTILINE";
3627 if (parts
[1].contains ("s")) {
3628 flags
+= " | G_REGEX_DOTALL";
3630 if (parts
[1].contains ("x")) {
3631 flags
+= " | G_REGEX_EXTENDED";
3634 var regex_var
= get_temp_variable (regex_type
, true, expr
, false);
3635 emit_temp_var (regex_var
);
3637 var cdecl
= new
CCodeDeclaration ("GRegex*");
3639 var cname
= regex_var
.name
+ "regex_" + next_regex_id
.to_string ();
3640 if (this
.next_regex_id
== 0) {
3641 var fun
= new
CCodeFunction ("_thread_safe_regex_init", "GRegex*");
3642 fun
.modifiers
= CCodeModifiers
.STATIC
| CCodeModifiers
.INLINE
;
3643 fun
.add_parameter (new
CCodeParameter ("re", "GRegex**"));
3644 fun
.add_parameter (new
CCodeParameter ("pattern", "const gchar *"));
3645 fun
.add_parameter (new
CCodeParameter ("match_options", "GRegexMatchFlags"));
3647 push_function (fun
);
3649 var once_enter_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_once_init_enter"));
3650 once_enter_call
.add_argument (new
CCodeConstant ("(volatile gsize*) re"));
3651 ccode
.open_if (once_enter_call
);
3653 var regex_new_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_regex_new"));
3654 regex_new_call
.add_argument (new
CCodeConstant ("pattern"));
3655 regex_new_call
.add_argument (new
CCodeConstant ("match_options"));
3656 regex_new_call
.add_argument (new
CCodeConstant ("0"));
3657 regex_new_call
.add_argument (new
CCodeConstant ("NULL"));
3658 ccode
.add_assignment (new
CCodeIdentifier ("GRegex* val"), regex_new_call
);
3660 var once_leave_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_once_init_leave"));
3661 once_leave_call
.add_argument (new
CCodeConstant ("(volatile gsize*) re"));
3662 once_leave_call
.add_argument (new
CCodeConstant ("(gsize) val"));
3663 ccode
.add_expression (once_leave_call
);
3667 ccode
.add_return (new
CCodeIdentifier ("*re"));
3671 cfile
.add_function (fun
);
3673 this
.next_regex_id
++;
3675 cdecl
.add_declarator (new
CCodeVariableDeclarator (cname
+ " = NULL"));
3676 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
3678 var regex_const
= new
CCodeConstant ("_thread_safe_regex_init (&%s, \"%s\", %s)".printf (cname
, re
, flags
));
3680 cfile
.add_constant_declaration (cdecl
);
3681 set_cvalue (expr
, regex_const
);
3684 public override void visit_null_literal (NullLiteral expr
) {
3685 if (context
.profile
!= Profile
.GOBJECT
) {
3686 cfile
.add_include ("stddef.h");
3688 set_cvalue (expr
, new
CCodeConstant ("NULL"));
3690 var array_type
= expr
.target_type as ArrayType
;
3691 var delegate_type
= expr
.target_type as DelegateType
;
3692 if (array_type
!= null) {
3693 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
3694 append_array_length (expr
, new
CCodeConstant ("0"));
3696 } else if (delegate_type
!= null && delegate_type
.delegate_symbol
.has_target
) {
3697 set_delegate_target (expr
, new
CCodeConstant ("NULL"));
3698 set_delegate_target_destroy_notify (expr
, new
CCodeConstant ("NULL"));
3702 public abstract TargetValue
get_local_cvalue (LocalVariable local
);
3704 public abstract TargetValue
get_parameter_cvalue (Parameter param
);
3706 public abstract TargetValue
get_field_cvalue (Field field
, TargetValue? instance
);
3708 public abstract TargetValue
load_this_parameter (TypeSymbol sym
);
3710 public virtual string get_delegate_target_cname (string delegate_cname
) {
3711 assert_not_reached ();
3714 public virtual CCodeExpression
get_delegate_target_cexpression (Expression delegate_expr
, out CCodeExpression delegate_target_destroy_notify
) {
3715 assert_not_reached ();
3718 public virtual CCodeExpression
get_delegate_target_cvalue (TargetValue value
) {
3719 return new
CCodeInvalidExpression ();
3722 public virtual CCodeExpression
get_delegate_target_destroy_notify_cvalue (TargetValue value
) {
3723 return new
CCodeInvalidExpression ();
3726 public virtual string get_delegate_target_destroy_notify_cname (string delegate_cname
) {
3727 assert_not_reached ();
3730 public override void visit_base_access (BaseAccess expr
) {
3731 CCodeExpression this_access
;
3732 if (is_in_coroutine ()) {
3734 this_access
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("data"), "self");
3736 this_access
= new
CCodeIdentifier ("self");
3739 set_cvalue (expr
, generate_instance_cast (this_access
, expr
.value_type
.data_type
));
3742 public override void visit_postfix_expression (PostfixExpression expr
) {
3743 MemberAccess ma
= find_property_access (expr
.inner
);
3745 // property postfix expression
3746 var prop
= (Property
) ma
.symbol_reference
;
3748 // assign current value to temp variable
3749 var temp_decl
= get_temp_variable (prop
.property_type
, true, expr
, false);
3750 emit_temp_var (temp_decl
);
3751 ccode
.add_assignment (get_variable_cexpression (temp_decl
.name
), get_cvalue (expr
.inner
));
3753 // increment/decrement property
3754 var op
= expr
.increment ? CCodeBinaryOperator
.PLUS
: CCodeBinaryOperator
.MINUS
;
3755 var cexpr
= new
CCodeBinaryExpression (op
, get_variable_cexpression (temp_decl
.name
), new
CCodeConstant ("1"));
3756 store_property (prop
, ma
.inner
, new
GLibValue (expr
.value_type
, cexpr
));
3758 // return previous value
3759 set_cvalue (expr
, get_variable_cexpression (temp_decl
.name
));
3763 if (expr
.parent_node is ExpressionStatement
) {
3764 var op
= expr
.increment ? CCodeUnaryOperator
.POSTFIX_INCREMENT
: CCodeUnaryOperator
.POSTFIX_DECREMENT
;
3766 ccode
.add_expression (new
CCodeUnaryExpression (op
, get_cvalue (expr
.inner
)));
3768 // assign current value to temp variable
3769 var temp_decl
= get_temp_variable (expr
.inner
.value_type
, true, expr
, false);
3770 emit_temp_var (temp_decl
);
3771 ccode
.add_assignment (get_variable_cexpression (temp_decl
.name
), get_cvalue (expr
.inner
));
3773 // increment/decrement variable
3774 var op
= expr
.increment ? CCodeBinaryOperator
.PLUS
: CCodeBinaryOperator
.MINUS
;
3775 var cexpr
= new
CCodeBinaryExpression (op
, get_variable_cexpression (temp_decl
.name
), new
CCodeConstant ("1"));
3776 ccode
.add_assignment (get_cvalue (expr
.inner
), cexpr
);
3778 // return previous value
3779 set_cvalue (expr
, get_variable_cexpression (temp_decl
.name
));
3783 private MemberAccess?
find_property_access (Expression expr
) {
3784 if (!(expr is MemberAccess
)) {
3788 var ma
= (MemberAccess
) expr
;
3789 if (ma
.symbol_reference is Property
) {
3796 bool is_limited_generic_type (DataType type
) {
3797 var cl
= type
.type_parameter
.parent_symbol as Class
;
3798 var st
= type
.type_parameter
.parent_symbol as Struct
;
3799 if ((cl
!= null && cl
.is_compact
) || st
!= null) {
3800 // compact classes and structs only
3801 // have very limited generics support
3807 public bool requires_copy (DataType type
) {
3808 if (!type
.is_disposable ()) {
3812 var cl
= type
.data_type as Class
;
3813 if (cl
!= null && cl
.is_reference_counting ()
3814 && cl
.get_ref_function () == "") {
3815 // empty ref_function => no ref necessary
3819 if (type
.type_parameter
!= null) {
3820 if (is_limited_generic_type (type
)) {
3828 public bool requires_destroy (DataType type
) {
3829 if (!type
.is_disposable ()) {
3833 var array_type
= type as ArrayType
;
3834 if (array_type
!= null && array_type
.fixed_length
) {
3835 return requires_destroy (array_type
.element_type
);
3838 var cl
= type
.data_type as Class
;
3839 if (cl
!= null && cl
.is_reference_counting ()
3840 && cl
.get_unref_function () == "") {
3841 // empty unref_function => no unref necessary
3845 if (type
.type_parameter
!= null) {
3846 if (is_limited_generic_type (type
)) {
3854 bool is_ref_function_void (DataType type
) {
3855 var cl
= type
.data_type as Class
;
3856 if (cl
!= null && cl
.ref_function_void
) {
3863 public virtual CCodeExpression?
get_ref_cexpression (DataType expression_type
, CCodeExpression cexpr
, Expression? expr
, CodeNode node
) {
3864 if (expression_type is DelegateType
) {
3868 if (expression_type is ValueType
&& !expression_type
.nullable
) {
3869 // normal value type, no null check
3871 var decl
= get_temp_variable (expression_type
, false, node
);
3872 emit_temp_var (decl
);
3873 var ctemp
= get_variable_cexpression (decl
.name
);
3875 var vt
= (ValueType
) expression_type
;
3876 var st
= (Struct
) vt
.type_symbol
;
3877 var copy_call
= new
CCodeFunctionCall (new
CCodeIdentifier (st
.get_copy_function ()));
3878 copy_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
));
3879 copy_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, ctemp
));
3881 if (!st
.has_copy_function
) {
3882 generate_struct_copy_function (st
);
3885 if (gvalue_type
!= null && expression_type
.data_type
== gvalue_type
) {
3886 var cisvalid
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_IS_VALUE"));
3887 cisvalid
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
));
3889 ccode
.open_if (cisvalid
);
3891 // GValue requires g_value_init in addition to g_value_copy
3892 var value_type_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_VALUE_TYPE"));
3893 value_type_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
));
3895 var init_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_value_init"));
3896 init_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, ctemp
));
3897 init_call
.add_argument (value_type_call
);
3898 ccode
.add_expression (init_call
);
3899 ccode
.add_expression (copy_call
);
3903 // g_value_init/copy must not be called for uninitialized values
3904 ccode
.add_assignment (ctemp
, cexpr
);
3907 ccode
.add_expression (copy_call
);
3913 /* (temp = expr, temp == NULL ? NULL : ref (temp))
3915 * can be simplified to
3917 * if static type of expr is non-null
3920 var dupexpr
= get_dup_func_expression (expression_type
, node
.source_reference
);
3922 if (dupexpr
== null) {
3927 if (dupexpr is CCodeIdentifier
&& !(expression_type is ArrayType
) && !(expression_type is GenericType
) && !is_ref_function_void (expression_type
)) {
3928 // generate and call NULL-aware ref function to reduce number
3929 // of temporary variables and simplify code
3931 var dupid
= (CCodeIdentifier
) dupexpr
;
3932 string dup0_func
= "_%s0".printf (dupid
.name
);
3934 // g_strdup is already NULL-safe
3935 if (dupid
.name
== "g_strdup") {
3936 dup0_func
= dupid
.name
;
3937 } else if (add_wrapper (dup0_func
)) {
3938 string pointer_cname
= "gpointer";
3939 if (context
.profile
== Profile
.POSIX
) {
3940 pointer_cname
= "void*";
3942 var dup0_fun
= new
CCodeFunction (dup0_func
, pointer_cname
);
3943 dup0_fun
.add_parameter (new
CCodeParameter ("self", pointer_cname
));
3944 dup0_fun
.modifiers
= CCodeModifiers
.STATIC
;
3946 push_function (dup0_fun
);
3948 var dup_call
= new
CCodeFunctionCall (dupexpr
);
3949 dup_call
.add_argument (new
CCodeIdentifier ("self"));
3951 ccode
.add_return (new
CCodeConditionalExpression (new
CCodeIdentifier ("self"), dup_call
, new
CCodeConstant ("NULL")));
3955 cfile
.add_function (dup0_fun
);
3958 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (dup0_func
));
3959 ccall
.add_argument (cexpr
);
3963 var ccall
= new
CCodeFunctionCall (dupexpr
);
3965 if (!(expression_type is ArrayType
) && expr
!= null && expr
.is_non_null ()
3966 && !is_ref_function_void (expression_type
)) {
3967 // expression is non-null
3968 ccall
.add_argument (get_cvalue (expr
));
3972 var decl
= get_temp_variable (expression_type
, false, node
, false);
3973 emit_temp_var (decl
);
3975 var ctemp
= get_variable_cexpression (decl
.name
);
3977 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, ctemp
, new
CCodeConstant ("NULL"));
3978 if (expression_type
.type_parameter
!= null) {
3979 // dup functions are optional for type parameters
3980 var cdupisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, get_dup_func_expression (expression_type
, node
.source_reference
), new
CCodeConstant ("NULL"));
3981 cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.OR
, cisnull
, cdupisnull
);
3984 if (expression_type
.type_parameter
!= null) {
3985 // cast from gconstpointer to gpointer as GBoxedCopyFunc expects gpointer
3986 ccall
.add_argument (new
CCodeCastExpression (ctemp
, "gpointer"));
3988 ccall
.add_argument (ctemp
);
3991 if (expression_type is ArrayType
) {
3992 var array_type
= (ArrayType
) expression_type
;
3994 CCodeExpression csizeexpr
= null;
3995 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
3997 csizeexpr
= get_array_length_cexpression (expr
, dim
);
4000 csizeexpr
= new
CCodeBinaryExpression (CCodeBinaryOperator
.MUL
, csizeexpr
, get_array_length_cexpression (expr
, dim
));
4004 ccall
.add_argument (csizeexpr
);
4006 if (array_type
.element_type is GenericType
) {
4007 var elem_dupexpr
= get_dup_func_expression (array_type
.element_type
, node
.source_reference
);
4008 if (elem_dupexpr
== null) {
4009 elem_dupexpr
= new
CCodeConstant ("NULL");
4011 ccall
.add_argument (elem_dupexpr
);
4015 var ccomma
= new
CCodeCommaExpression ();
4016 ccomma
.append_expression (new
CCodeAssignment (ctemp
, cexpr
));
4018 CCodeExpression cifnull
;
4019 if (expression_type
.data_type
!= null) {
4020 cifnull
= new
CCodeConstant ("NULL");
4022 // the value might be non-null even when the dup function is null,
4023 // so we may not just use NULL for type parameters
4025 // cast from gconstpointer to gpointer as methods in
4026 // generic classes may not return gconstpointer
4027 cifnull
= new
CCodeCastExpression (ctemp
, "gpointer");
4029 ccomma
.append_expression (new
CCodeConditionalExpression (cisnull
, cifnull
, ccall
));
4031 // repeat temp variable at the end of the comma expression
4032 // if the ref function returns void
4033 if (is_ref_function_void (expression_type
)) {
4034 ccomma
.append_expression (ctemp
);
4041 bool is_reference_type_argument (DataType type_arg
) {
4042 if (type_arg is ErrorType
|| (type_arg
.data_type
!= null && type_arg
.data_type
.is_reference_type ())) {
4049 bool is_nullable_value_type_argument (DataType type_arg
) {
4050 if (type_arg is ValueType
&& type_arg
.nullable
) {
4057 bool is_signed_integer_type_argument (DataType type_arg
) {
4058 var st
= type_arg
.data_type as Struct
;
4059 if (type_arg
.nullable
) {
4061 } else if (st
== bool_type
.data_type
) {
4063 } else if (st
== char_type
.data_type
) {
4065 } else if (unichar_type
!= null && st
== unichar_type
.data_type
) {
4067 } else if (st
== short_type
.data_type
) {
4069 } else if (st
== int_type
.data_type
) {
4071 } else if (st
== long_type
.data_type
) {
4073 } else if (st
== int8_type
.data_type
) {
4075 } else if (st
== int16_type
.data_type
) {
4077 } else if (st
== int32_type
.data_type
) {
4079 } else if (st
== gtype_type
) {
4081 } else if (type_arg is EnumValueType
) {
4088 bool is_unsigned_integer_type_argument (DataType type_arg
) {
4089 var st
= type_arg
.data_type as Struct
;
4090 if (type_arg
.nullable
) {
4092 } else if (st
== uchar_type
.data_type
) {
4094 } else if (st
== ushort_type
.data_type
) {
4096 } else if (st
== uint_type
.data_type
) {
4098 } else if (st
== ulong_type
.data_type
) {
4100 } else if (st
== uint8_type
.data_type
) {
4102 } else if (st
== uint16_type
.data_type
) {
4104 } else if (st
== uint32_type
.data_type
) {
4111 public void check_type (DataType type
) {
4112 var array_type
= type as ArrayType
;
4113 if (array_type
!= null) {
4114 check_type (array_type
.element_type
);
4115 if (array_type
.element_type is ArrayType
) {
4116 Report
.error (type
.source_reference
, "Stacked arrays are not supported");
4117 } else if (array_type
.element_type is DelegateType
) {
4118 var delegate_type
= (DelegateType
) array_type
.element_type
;
4119 if (delegate_type
.delegate_symbol
.has_target
) {
4120 Report
.error (type
.source_reference
, "Delegates with target are not supported as array element type");
4124 foreach (var type_arg
in type
.get_type_arguments ()) {
4125 check_type (type_arg
);
4126 check_type_argument (type_arg
);
4130 void check_type_argument (DataType type_arg
) {
4131 if (type_arg is GenericType
4132 || type_arg is PointerType
4133 || is_reference_type_argument (type_arg
)
4134 || is_nullable_value_type_argument (type_arg
)
4135 || is_signed_integer_type_argument (type_arg
)
4136 || is_unsigned_integer_type_argument (type_arg
)) {
4138 } else if (type_arg is DelegateType
) {
4139 var delegate_type
= (DelegateType
) type_arg
;
4140 if (delegate_type
.delegate_symbol
.has_target
) {
4141 Report
.error (type_arg
.source_reference
, "Delegates with target are not supported as generic type arguments");
4144 Report
.error (type_arg
.source_reference
, "`%s' is not a supported generic type argument, use `?' to box value types".printf (type_arg
.to_string ()));
4148 public virtual void generate_class_declaration (Class cl
, CCodeFile decl_space
) {
4149 if (add_symbol_declaration (decl_space
, cl
, cl
.get_cname ())) {
4154 public virtual void generate_interface_declaration (Interface iface
, CCodeFile decl_space
) {
4157 public virtual void generate_method_declaration (Method m
, CCodeFile decl_space
) {
4160 public virtual void generate_error_domain_declaration (ErrorDomain edomain
, CCodeFile decl_space
) {
4163 public void add_generic_type_arguments (Map
<int,CCodeExpression
> arg_map
, List
<DataType
> type_args
, CodeNode expr
, bool is_chainup
= false) {
4164 int type_param_index
= 0;
4165 foreach (var type_arg
in type_args
) {
4166 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.01), get_type_id_expression (type_arg
, is_chainup
));
4167 if (requires_copy (type_arg
)) {
4168 var dup_func
= get_dup_func_expression (type_arg
, type_arg
.source_reference
, is_chainup
);
4169 if (dup_func
== null) {
4170 // type doesn't contain a copy function
4174 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.02), new
CCodeCastExpression (dup_func
, "GBoxedCopyFunc"));
4175 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.03), get_destroy_func_expression (type_arg
, is_chainup
));
4177 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.02), new
CCodeConstant ("NULL"));
4178 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.03), new
CCodeConstant ("NULL"));
4184 public override void visit_object_creation_expression (ObjectCreationExpression expr
) {
4185 CCodeExpression instance
= null;
4186 CCodeExpression creation_expr
= null;
4188 check_type (expr
.type_reference
);
4190 var st
= expr
.type_reference
.data_type as Struct
;
4191 if ((st
!= null && (!st
.is_simple_type () || st
.get_cname () == "va_list")) || expr
.get_object_initializer ().size
> 0) {
4192 // value-type initialization or object creation expression with object initializer
4194 var local
= expr
.parent_node as LocalVariable
;
4195 if (local
!= null && has_simple_struct_initializer (local
)) {
4196 if (local
.captured
) {
4197 var block
= (Block
) local
.parent_symbol
;
4198 instance
= new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block
))), get_variable_cname (local
.name
));
4200 instance
= get_variable_cexpression (get_variable_cname (local
.name
));
4203 var temp_decl
= get_temp_variable (expr
.type_reference
, false, expr
);
4204 emit_temp_var (temp_decl
);
4206 instance
= get_variable_cexpression (get_variable_cname (temp_decl
.name
));
4210 if (expr
.symbol_reference
== null) {
4211 // no creation method
4212 if (expr
.type_reference
.data_type is Struct
) {
4213 // memset needs string.h
4214 cfile
.add_include ("string.h");
4215 var creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memset"));
4216 creation_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, instance
));
4217 creation_call
.add_argument (new
CCodeConstant ("0"));
4218 creation_call
.add_argument (new
CCodeIdentifier ("sizeof (%s)".printf (expr
.type_reference
.get_cname ())));
4220 creation_expr
= creation_call
;
4222 } else if (expr
.type_reference
.data_type
== glist_type
||
4223 expr
.type_reference
.data_type
== gslist_type
) {
4224 // NULL is an empty list
4225 set_cvalue (expr
, new
CCodeConstant ("NULL"));
4226 } else if (expr
.symbol_reference is Method
) {
4227 // use creation method
4228 var m
= (Method
) expr
.symbol_reference
;
4229 var params
= m
.get_parameters ();
4230 CCodeFunctionCall creation_call
;
4232 generate_method_declaration (m
, cfile
);
4234 var cl
= expr
.type_reference
.data_type as Class
;
4236 if (!m
.has_new_function
) {
4237 // use construct function directly
4238 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier (m
.get_real_cname ()));
4239 creation_call
.add_argument (new
CCodeIdentifier (cl
.get_type_id ()));
4241 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier (m
.get_cname ()));
4244 if ((st
!= null && !st
.is_simple_type ()) && !(m
.cinstance_parameter_position
< 0)) {
4245 creation_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, instance
));
4246 } else if (st
!= null && st
.get_cname () == "va_list") {
4247 creation_call
.add_argument (instance
);
4248 if (m
.get_cname () == "va_start") {
4249 Parameter last_param
= null;
4250 foreach (var param
in current_method
.get_parameters ()) {
4251 if (param
.ellipsis
) {
4256 creation_call
.add_argument (new
CCodeIdentifier (get_variable_cname (last_param
.name
)));
4260 generate_type_declaration (expr
.type_reference
, cfile
);
4262 var carg_map
= new HashMap
<int,CCodeExpression
> (direct_hash
, direct_equal
);
4264 if (cl
!= null && !cl
.is_compact
) {
4265 add_generic_type_arguments (carg_map
, expr
.type_reference
.get_type_arguments (), expr
);
4266 } else if (cl
!= null && m
.simple_generics
) {
4267 int type_param_index
= 0;
4268 foreach (var type_arg
in expr
.type_reference
.get_type_arguments ()) {
4269 if (requires_copy (type_arg
)) {
4270 carg_map
.set (get_param_pos (-1 + 0.1 * type_param_index
+ 0.03), get_destroy0_func_expression (type_arg
));
4272 carg_map
.set (get_param_pos (-1 + 0.1 * type_param_index
+ 0.03), new
CCodeConstant ("NULL"));
4278 bool ellipsis
= false;
4282 Iterator
<Parameter
> params_it
= params
.iterator ();
4283 foreach (Expression arg
in expr
.get_argument_list ()) {
4284 CCodeExpression cexpr
= get_cvalue (arg
);
4285 Parameter param
= null;
4286 if (params_it
.next ()) {
4287 param
= params_it
.get ();
4288 ellipsis
= param
.ellipsis
;
4290 if (!param
.no_array_length
&& param
.variable_type is ArrayType
) {
4291 var array_type
= (ArrayType
) param
.variable_type
;
4292 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4293 carg_map
.set (get_param_pos (param
.carray_length_parameter_position
+ 0.01 * dim
), get_array_length_cexpression (arg
, dim
));
4295 } else if (param
.variable_type is DelegateType
) {
4296 var deleg_type
= (DelegateType
) param
.variable_type
;
4297 var d
= deleg_type
.delegate_symbol
;
4299 CCodeExpression delegate_target_destroy_notify
;
4300 var delegate_target
= get_delegate_target_cexpression (arg
, out delegate_target_destroy_notify
);
4301 carg_map
.set (get_param_pos (param
.cdelegate_target_parameter_position
), delegate_target
);
4302 if (deleg_type
.value_owned
) {
4303 carg_map
.set (get_param_pos (param
.cdelegate_target_parameter_position
+ 0.01), delegate_target_destroy_notify
);
4308 cexpr
= handle_struct_argument (param
, arg
, cexpr
);
4310 if (param
.ctype
!= null) {
4311 cexpr
= new
CCodeCastExpression (cexpr
, param
.ctype
);
4314 cexpr
= handle_struct_argument (null, arg
, cexpr
);
4317 arg_pos
= get_param_pos (param
.cparameter_position
, ellipsis
);
4319 // default argument position
4320 cexpr
= handle_struct_argument (null, arg
, cexpr
);
4321 arg_pos
= get_param_pos (i
, ellipsis
);
4324 carg_map
.set (arg_pos
, cexpr
);
4328 while (params_it
.next ()) {
4329 var param
= params_it
.get ();
4331 if (param
.ellipsis
) {
4336 if (param
.initializer
== null) {
4337 Report
.error (expr
.source_reference
, "no default expression for argument %d".printf (i
));
4341 /* evaluate default expression here as the code
4342 * generator might not have visited the formal
4344 param
.initializer
.emit (this
);
4346 carg_map
.set (get_param_pos (param
.cparameter_position
), get_cvalue (param
.initializer
));
4350 // append C arguments in the right order
4355 foreach (int pos
in carg_map
.get_keys ()) {
4356 if (pos
> last_pos
&& (min_pos
== -1 || pos
< min_pos
)) {
4360 if (min_pos
== -1) {
4363 creation_call
.add_argument (carg_map
.get (min_pos
));
4367 if ((st
!= null && !st
.is_simple_type ()) && m
.cinstance_parameter_position
< 0) {
4368 // instance parameter is at the end in a struct creation method
4369 creation_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, instance
));
4372 if (expr
.tree_can_fail
) {
4374 current_method_inner_error
= true;
4375 creation_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression ("_inner_error_")));
4379 /* ensure variable argument list ends with NULL
4380 * except when using printf-style arguments */
4381 if (!m
.printf_format
&& !m
.scanf_format
&& m
.sentinel
!= "") {
4382 creation_call
.add_argument (new
CCodeConstant (m
.sentinel
));
4386 creation_expr
= creation_call
;
4388 // cast the return value of the creation method back to the intended type if
4389 // it requested a special C return type
4390 if (get_custom_creturn_type (m
) != null) {
4391 creation_expr
= new
CCodeCastExpression (creation_expr
, expr
.type_reference
.get_cname ());
4393 } else if (expr
.symbol_reference is ErrorCode
) {
4394 var ecode
= (ErrorCode
) expr
.symbol_reference
;
4395 var edomain
= (ErrorDomain
) ecode
.parent_symbol
;
4396 CCodeFunctionCall creation_call
;
4398 generate_error_domain_declaration (edomain
, cfile
);
4400 if (expr
.get_argument_list ().size
== 1) {
4401 // must not be a format argument
4402 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_error_new_literal"));
4404 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_error_new"));
4406 creation_call
.add_argument (new
CCodeIdentifier (edomain
.get_upper_case_cname ()));
4407 creation_call
.add_argument (new
CCodeIdentifier (ecode
.get_cname ()));
4409 foreach (Expression arg
in expr
.get_argument_list ()) {
4410 creation_call
.add_argument (get_cvalue (arg
));
4413 creation_expr
= creation_call
;
4418 var local
= expr
.parent_node as LocalVariable
;
4419 if (local
!= null && has_simple_struct_initializer (local
)) {
4420 // no temporary variable necessary
4421 ccode
.add_expression (creation_expr
);
4422 set_cvalue (expr
, instance
);
4424 } else if (instance
!= null) {
4425 if (expr
.type_reference
.data_type is Struct
) {
4426 ccode
.add_expression (creation_expr
);
4428 ccode
.add_assignment (instance
, creation_expr
);
4431 foreach (MemberInitializer init
in expr
.get_object_initializer ()) {
4432 if (init
.symbol_reference is Field
) {
4433 var f
= (Field
) init
.symbol_reference
;
4434 var instance_target_type
= get_data_type_for_symbol ((TypeSymbol
) f
.parent_symbol
);
4435 var typed_inst
= transform_expression (instance
, expr
.type_reference
, instance_target_type
);
4436 CCodeExpression lhs
;
4437 if (expr
.type_reference
.data_type is Struct
) {
4438 lhs
= new
CCodeMemberAccess (typed_inst
, f
.get_cname ());
4440 lhs
= new CCodeMemberAccess
.pointer (typed_inst
, f
.get_cname ());
4442 ccode
.add_assignment (lhs
, get_cvalue (init
.initializer
));
4444 if (f
.variable_type is ArrayType
&& !f
.no_array_length
) {
4445 var array_type
= (ArrayType
) f
.variable_type
;
4446 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4447 if (expr
.type_reference
.data_type is Struct
) {
4448 lhs
= new
CCodeMemberAccess (typed_inst
, get_array_length_cname (f
.get_cname (), dim
));
4450 lhs
= new CCodeMemberAccess
.pointer (typed_inst
, get_array_length_cname (f
.get_cname (), dim
));
4452 var rhs_array_len
= get_array_length_cexpression (init
.initializer
, dim
);
4453 ccode
.add_assignment (lhs
, rhs_array_len
);
4455 } else if (f
.variable_type is DelegateType
&& (f
.variable_type as DelegateType
).delegate_symbol
.has_target
&& !f
.no_delegate_target
) {
4456 if (expr
.type_reference
.data_type is Struct
) {
4457 lhs
= new
CCodeMemberAccess (typed_inst
, get_delegate_target_cname (f
.get_cname ()));
4459 lhs
= new CCodeMemberAccess
.pointer (typed_inst
, get_delegate_target_cname (f
.get_cname ()));
4461 CCodeExpression rhs_delegate_target_destroy_notify
;
4462 var rhs_delegate_target
= get_delegate_target_cexpression (init
.initializer
, out rhs_delegate_target_destroy_notify
);
4463 ccode
.add_assignment (lhs
, rhs_delegate_target
);
4466 var cl
= f
.parent_symbol as Class
;
4468 generate_class_struct_declaration (cl
, cfile
);
4470 } else if (init
.symbol_reference is Property
) {
4471 var inst_ma
= new MemberAccess
.simple ("new");
4472 inst_ma
.value_type
= expr
.type_reference
;
4473 set_cvalue (inst_ma
, instance
);
4474 store_property ((Property
) init
.symbol_reference
, inst_ma
, init
.initializer
.target_value
);
4478 creation_expr
= instance
;
4481 if (creation_expr
!= null) {
4482 var temp_var
= get_temp_variable (expr
.value_type
);
4483 var temp_ref
= get_variable_cexpression (temp_var
.name
);
4485 emit_temp_var (temp_var
);
4487 ccode
.add_assignment (temp_ref
, creation_expr
);
4488 set_cvalue (expr
, temp_ref
);
4492 public CCodeExpression?
handle_struct_argument (Parameter? param
, Expression arg
, CCodeExpression? cexpr
) {
4494 if (param
!= null) {
4495 type
= param
.variable_type
;
4498 type
= arg
.value_type
;
4501 // pass non-simple struct instances always by reference
4502 if (!(arg
.value_type is NullType
) && type
.is_real_struct_type ()) {
4503 // we already use a reference for arguments of ref, out, and nullable parameters
4504 if ((param
== null || param
.direction
== ParameterDirection
.IN
) && !type
.nullable
) {
4505 var unary
= cexpr as CCodeUnaryExpression
;
4506 if (unary
!= null && unary
.operator
== CCodeUnaryOperator
.POINTER_INDIRECTION
) {
4509 } else if (cexpr is CCodeIdentifier
|| cexpr is CCodeMemberAccess
) {
4510 return new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
);
4512 // if cexpr is e.g. a function call, we can't take the address of the expression
4513 var temp_var
= get_temp_variable (type
, true, null, false);
4514 emit_temp_var (temp_var
);
4516 ccode
.add_assignment (get_variable_cexpression (temp_var
.name
), cexpr
);
4517 return new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (temp_var
.name
));
4525 public override void visit_sizeof_expression (SizeofExpression expr
) {
4526 generate_type_declaration (expr
.type_reference
, cfile
);
4528 var csizeof
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
4529 csizeof
.add_argument (new
CCodeIdentifier (expr
.type_reference
.get_cname ()));
4530 set_cvalue (expr
, csizeof
);
4533 public override void visit_typeof_expression (TypeofExpression expr
) {
4534 set_cvalue (expr
, get_type_id_expression (expr
.type_reference
));
4537 public override void visit_unary_expression (UnaryExpression expr
) {
4538 if (expr
.operator
== UnaryOperator
.REF
|| expr
.operator
== UnaryOperator
.OUT
) {
4539 var glib_value
= (GLibValue
) expr
.inner
.target_value
;
4541 var ref_value
= new
GLibValue (glib_value
.value_type
);
4542 ref_value
.cvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.cvalue
);
4544 if (glib_value
.array_length_cvalues
!= null) {
4545 for (int i
= 0; i
< glib_value
.array_length_cvalues
.size
; i
++) {
4546 ref_value
.append_array_length_cvalue (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.array_length_cvalues
[i
]));
4550 if (glib_value
.delegate_target_cvalue
!= null) {
4551 ref_value
.delegate_target_cvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.delegate_target_cvalue
);
4553 if (glib_value
.delegate_target_destroy_notify_cvalue
!= null) {
4554 ref_value
.delegate_target_destroy_notify_cvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.delegate_target_destroy_notify_cvalue
);
4557 expr
.target_value
= ref_value
;
4561 CCodeUnaryOperator op
;
4562 if (expr
.operator
== UnaryOperator
.PLUS
) {
4563 op
= CCodeUnaryOperator
.PLUS
;
4564 } else if (expr
.operator
== UnaryOperator
.MINUS
) {
4565 op
= CCodeUnaryOperator
.MINUS
;
4566 } else if (expr
.operator
== UnaryOperator
.LOGICAL_NEGATION
) {
4567 op
= CCodeUnaryOperator
.LOGICAL_NEGATION
;
4568 } else if (expr
.operator
== UnaryOperator
.BITWISE_COMPLEMENT
) {
4569 op
= CCodeUnaryOperator
.BITWISE_COMPLEMENT
;
4570 } else if (expr
.operator
== UnaryOperator
.INCREMENT
) {
4571 op
= CCodeUnaryOperator
.PREFIX_INCREMENT
;
4572 } else if (expr
.operator
== UnaryOperator
.DECREMENT
) {
4573 op
= CCodeUnaryOperator
.PREFIX_DECREMENT
;
4575 assert_not_reached ();
4577 set_cvalue (expr
, new
CCodeUnaryExpression (op
, get_cvalue (expr
.inner
)));
4580 public CCodeExpression?
try_cast_value_to_type (CCodeExpression ccodeexpr
, DataType from
, DataType to
, Expression? expr
= null) {
4581 if (from
== null || gvalue_type
== null || from
.data_type
!= gvalue_type
|| to
.get_type_id () == null) {
4585 // explicit conversion from GValue
4586 var ccall
= new
CCodeFunctionCall (get_value_getter_function (to
));
4587 CCodeExpression gvalue
;
4588 if (from
.nullable
) {
4591 gvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, ccodeexpr
);
4593 ccall
.add_argument (gvalue
);
4595 CCodeExpression rv
= ccall
;
4597 if (expr
!= null && to is ArrayType
) {
4598 // null-terminated string array
4599 var len_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strv_length"));
4600 len_call
.add_argument (rv
);
4601 append_array_length (expr
, len_call
);
4602 } else if (to is StructValueType
) {
4603 var temp_decl
= get_temp_variable (to
, true, null, true);
4604 emit_temp_var (temp_decl
);
4605 var ctemp
= get_variable_cexpression (temp_decl
.name
);
4607 rv
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeCastExpression (rv
, (new
PointerType(to
)).get_cname ()));
4608 var holds
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_VALUE_HOLDS"));
4609 holds
.add_argument (gvalue
);
4610 holds
.add_argument (new
CCodeIdentifier (to
.get_type_id ()));
4611 var cond
= new
CCodeBinaryExpression (CCodeBinaryOperator
.AND
, holds
, ccall
);
4612 var warn
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_warning"));
4613 warn
.add_argument (new
CCodeConstant ("\"Invalid GValue unboxing (wrong type or NULL)\""));
4614 var fail
= new
CCodeCommaExpression ();
4615 fail
.append_expression (warn
);
4616 fail
.append_expression (ctemp
);
4617 rv
= new
CCodeConditionalExpression (cond
, rv
, fail
);
4623 int next_variant_function_id
= 0;
4625 public CCodeExpression?
try_cast_variant_to_type (CCodeExpression ccodeexpr
, DataType from
, DataType to
, Expression? expr
= null) {
4626 if (from
== null || gvariant_type
== null || from
.data_type
!= gvariant_type
) {
4630 string variant_func
= "_variant_get%d".printf (++next_variant_function_id
);
4632 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (variant_func
));
4633 ccall
.add_argument (ccodeexpr
);
4635 var cfunc
= new
CCodeFunction (variant_func
);
4636 cfunc
.modifiers
= CCodeModifiers
.STATIC
;
4637 cfunc
.add_parameter (new
CCodeParameter ("value", "GVariant*"));
4639 if (!to
.is_real_non_null_struct_type ()) {
4640 cfunc
.return_type
= to
.get_cname ();
4643 if (to
.is_real_non_null_struct_type ()) {
4644 // structs are returned via out parameter
4645 cfunc
.add_parameter (new
CCodeParameter ("result", to
.get_cname () + "*"));
4646 } else if (to is ArrayType
) {
4647 // return array length if appropriate
4648 var array_type
= (ArrayType
) to
;
4650 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4651 var temp_decl
= get_temp_variable (int_type
, false, expr
);
4652 emit_temp_var (temp_decl
);
4654 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (temp_decl
.name
)));
4655 cfunc
.add_parameter (new
CCodeParameter (get_array_length_cname ("result", dim
), "int*"));
4656 append_array_length (expr
, get_variable_cexpression (temp_decl
.name
));
4660 push_function (cfunc
);
4662 var result
= deserialize_expression (to
, new
CCodeIdentifier ("value"), new
CCodeIdentifier ("*result"));
4663 ccode
.add_return (result
);
4667 cfile
.add_function_declaration (cfunc
);
4668 cfile
.add_function (cfunc
);
4673 public virtual CCodeExpression?
deserialize_expression (DataType type
, CCodeExpression variant_expr
, CCodeExpression? expr
, CCodeExpression? error_expr
= null, out bool may_fail
= null) {
4677 public virtual CCodeExpression?
serialize_expression (DataType type
, CCodeExpression expr
) {
4681 public override void visit_cast_expression (CastExpression expr
) {
4682 var valuecast
= try_cast_value_to_type (get_cvalue (expr
.inner
), expr
.inner
.value_type
, expr
.type_reference
, expr
);
4683 if (valuecast
!= null) {
4684 set_cvalue (expr
, valuecast
);
4688 var variantcast
= try_cast_variant_to_type (get_cvalue (expr
.inner
), expr
.inner
.value_type
, expr
.type_reference
, expr
);
4689 if (variantcast
!= null) {
4690 set_cvalue (expr
, variantcast
);
4694 generate_type_declaration (expr
.type_reference
, cfile
);
4696 var cl
= expr
.type_reference
.data_type as Class
;
4697 var iface
= expr
.type_reference
.data_type as Interface
;
4698 if (context
.profile
== Profile
.GOBJECT
&& (iface
!= null || (cl
!= null && !cl
.is_compact
))) {
4699 // checked cast for strict subtypes of GTypeInstance
4700 if (expr
.is_silent_cast
) {
4701 var temp_decl
= get_temp_variable (expr
.inner
.value_type
, expr
.inner
.value_type
.value_owned
, expr
, false);
4702 emit_temp_var (temp_decl
);
4703 var ctemp
= get_variable_cexpression (temp_decl
.name
);
4705 ccode
.add_assignment (ctemp
, get_cvalue (expr
.inner
));
4706 var ccheck
= create_type_check (ctemp
, expr
.type_reference
);
4707 var ccast
= new
CCodeCastExpression (ctemp
, expr
.type_reference
.get_cname ());
4708 var cnull
= new
CCodeConstant ("NULL");
4710 set_cvalue (expr
, new
CCodeConditionalExpression (ccheck
, ccast
, cnull
));
4712 set_cvalue (expr
, generate_instance_cast (get_cvalue (expr
.inner
), expr
.type_reference
.data_type
));
4715 if (expr
.is_silent_cast
) {
4717 Report
.error (expr
.source_reference
, "Operation not supported for this type");
4721 // retain array length
4722 var array_type
= expr
.type_reference as ArrayType
;
4723 if (array_type
!= null && expr
.inner
.value_type is ArrayType
) {
4724 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4725 append_array_length (expr
, get_array_length_cexpression (expr
.inner
, dim
));
4727 } else if (array_type
!= null) {
4728 // cast from non-array to array, set invalid length
4729 // required by string.data, e.g.
4730 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4731 append_array_length (expr
, new
CCodeConstant ("-1"));
4735 var innercexpr
= get_cvalue (expr
.inner
);
4736 if (expr
.type_reference
.data_type is Struct
&& !expr
.type_reference
.nullable
&&
4737 expr
.inner
.value_type
.data_type is Struct
&& expr
.inner
.value_type
.nullable
) {
4738 // nullable integer or float or boolean or struct cast to non-nullable
4739 innercexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, innercexpr
);
4741 set_cvalue (expr
, new
CCodeCastExpression (innercexpr
, expr
.type_reference
.get_cname ()));
4743 if (expr
.type_reference is DelegateType
) {
4744 if (get_delegate_target (expr
.inner
) != null) {
4745 set_delegate_target (expr
, get_delegate_target (expr
.inner
));
4747 set_delegate_target (expr
, new
CCodeConstant ("NULL"));
4749 if (get_delegate_target_destroy_notify (expr
.inner
) != null) {
4750 set_delegate_target_destroy_notify (expr
, get_delegate_target_destroy_notify (expr
.inner
));
4752 set_delegate_target_destroy_notify (expr
, new
CCodeConstant ("NULL"));
4758 public override void visit_named_argument (NamedArgument expr
) {
4759 set_cvalue (expr
, get_cvalue (expr
.inner
));
4762 public override void visit_pointer_indirection (PointerIndirection expr
) {
4763 set_cvalue (expr
, new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_cvalue (expr
.inner
)));
4766 public override void visit_addressof_expression (AddressofExpression expr
) {
4767 set_cvalue (expr
, new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_cvalue (expr
.inner
)));
4770 public override void visit_reference_transfer_expression (ReferenceTransferExpression expr
) {
4771 /* (tmp = var, var = null, tmp) */
4772 var temp_decl
= get_temp_variable (expr
.value_type
, true, expr
, false);
4773 emit_temp_var (temp_decl
);
4774 var cvar
= get_variable_cexpression (temp_decl
.name
);
4776 ccode
.add_assignment (cvar
, get_cvalue (expr
.inner
));
4777 if (!(expr
.value_type is DelegateType
)) {
4778 ccode
.add_assignment (get_cvalue (expr
.inner
), new
CCodeConstant ("NULL"));
4781 set_cvalue (expr
, cvar
);
4783 var array_type
= expr
.value_type as ArrayType
;
4784 if (array_type
!= null) {
4785 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4786 append_array_length (expr
, get_array_length_cexpression (expr
.inner
, dim
));
4790 var delegate_type
= expr
.value_type as DelegateType
;
4791 if (delegate_type
!= null && delegate_type
.delegate_symbol
.has_target
) {
4792 var temp_target_decl
= get_temp_variable (new
PointerType (new
VoidType ()), true, expr
, false);
4793 emit_temp_var (temp_target_decl
);
4794 var target_cvar
= get_variable_cexpression (temp_target_decl
.name
);
4795 CCodeExpression target_destroy_notify
;
4796 var target
= get_delegate_target_cexpression (expr
.inner
, out target_destroy_notify
);
4797 ccode
.add_assignment (target_cvar
, target
);
4798 set_delegate_target (expr
, target_cvar
);
4799 if (target_destroy_notify
!= null) {
4800 var temp_target_destroy_notify_decl
= get_temp_variable (gdestroynotify_type
, true, expr
, false);
4801 emit_temp_var (temp_target_destroy_notify_decl
);
4802 var target_destroy_notify_cvar
= get_variable_cexpression (temp_target_destroy_notify_decl
.name
);
4803 ccode
.add_assignment (target_destroy_notify_cvar
, target_destroy_notify
);
4804 ccode
.add_assignment (target_destroy_notify
, new
CCodeConstant ("NULL"));
4805 set_delegate_target_destroy_notify (expr
, target_destroy_notify_cvar
);
4810 public override void visit_binary_expression (BinaryExpression expr
) {
4811 var cleft
= get_cvalue (expr
.left
);
4812 var cright
= get_cvalue (expr
.right
);
4814 CCodeExpression? left_chain
= null;
4816 var lbe
= (BinaryExpression
) expr
.left
;
4818 var temp_decl
= get_temp_variable (lbe
.right
.value_type
, true, null, false);
4819 emit_temp_var (temp_decl
);
4820 var cvar
= get_variable_cexpression (temp_decl
.name
);
4821 var clbe
= (CCodeBinaryExpression
) get_cvalue (lbe
);
4823 clbe
= (CCodeBinaryExpression
) clbe
.right
;
4825 ccode
.add_assignment (cvar
, get_cvalue (lbe
.right
));
4826 clbe
.right
= get_variable_cexpression (temp_decl
.name
);
4831 CCodeBinaryOperator op
;
4832 if (expr
.operator
== BinaryOperator
.PLUS
) {
4833 op
= CCodeBinaryOperator
.PLUS
;
4834 } else if (expr
.operator
== BinaryOperator
.MINUS
) {
4835 op
= CCodeBinaryOperator
.MINUS
;
4836 } else if (expr
.operator
== BinaryOperator
.MUL
) {
4837 op
= CCodeBinaryOperator
.MUL
;
4838 } else if (expr
.operator
== BinaryOperator
.DIV
) {
4839 op
= CCodeBinaryOperator
.DIV
;
4840 } else if (expr
.operator
== BinaryOperator
.MOD
) {
4841 if (expr
.value_type
.equals (double_type
)) {
4842 cfile
.add_include ("math.h");
4843 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("fmod"));
4844 ccall
.add_argument (cleft
);
4845 ccall
.add_argument (cright
);
4846 set_cvalue (expr
, ccall
);
4848 } else if (expr
.value_type
.equals (float_type
)) {
4849 cfile
.add_include ("math.h");
4850 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("fmodf"));
4851 ccall
.add_argument (cleft
);
4852 ccall
.add_argument (cright
);
4853 set_cvalue (expr
, ccall
);
4856 op
= CCodeBinaryOperator
.MOD
;
4858 } else if (expr
.operator
== BinaryOperator
.SHIFT_LEFT
) {
4859 op
= CCodeBinaryOperator
.SHIFT_LEFT
;
4860 } else if (expr
.operator
== BinaryOperator
.SHIFT_RIGHT
) {
4861 op
= CCodeBinaryOperator
.SHIFT_RIGHT
;
4862 } else if (expr
.operator
== BinaryOperator
.LESS_THAN
) {
4863 op
= CCodeBinaryOperator
.LESS_THAN
;
4864 } else if (expr
.operator
== BinaryOperator
.GREATER_THAN
) {
4865 op
= CCodeBinaryOperator
.GREATER_THAN
;
4866 } else if (expr
.operator
== BinaryOperator
.LESS_THAN_OR_EQUAL
) {
4867 op
= CCodeBinaryOperator
.LESS_THAN_OR_EQUAL
;
4868 } else if (expr
.operator
== BinaryOperator
.GREATER_THAN_OR_EQUAL
) {
4869 op
= CCodeBinaryOperator
.GREATER_THAN_OR_EQUAL
;
4870 } else if (expr
.operator
== BinaryOperator
.EQUALITY
) {
4871 op
= CCodeBinaryOperator
.EQUALITY
;
4872 } else if (expr
.operator
== BinaryOperator
.INEQUALITY
) {
4873 op
= CCodeBinaryOperator
.INEQUALITY
;
4874 } else if (expr
.operator
== BinaryOperator
.BITWISE_AND
) {
4875 op
= CCodeBinaryOperator
.BITWISE_AND
;
4876 } else if (expr
.operator
== BinaryOperator
.BITWISE_OR
) {
4877 op
= CCodeBinaryOperator
.BITWISE_OR
;
4878 } else if (expr
.operator
== BinaryOperator
.BITWISE_XOR
) {
4879 op
= CCodeBinaryOperator
.BITWISE_XOR
;
4880 } else if (expr
.operator
== BinaryOperator
.AND
) {
4881 op
= CCodeBinaryOperator
.AND
;
4882 } else if (expr
.operator
== BinaryOperator
.OR
) {
4883 op
= CCodeBinaryOperator
.OR
;
4884 } else if (expr
.operator
== BinaryOperator
.IN
) {
4885 if (expr
.right
.value_type is ArrayType
) {
4886 var array_type
= (ArrayType
) expr
.right
.value_type
;
4887 var node
= new
CCodeFunctionCall (new
CCodeIdentifier (generate_array_contains_wrapper (array_type
)));
4888 node
.add_argument (cright
);
4889 node
.add_argument (get_array_length_cexpression (expr
.right
));
4890 if (array_type
.element_type is StructValueType
) {
4891 node
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cleft
));
4893 node
.add_argument (cleft
);
4895 set_cvalue (expr
, node
);
4897 set_cvalue (expr
, new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeBinaryExpression (CCodeBinaryOperator
.BITWISE_AND
, cright
, cleft
), cleft
));
4901 assert_not_reached ();
4904 if (expr
.operator
== BinaryOperator
.EQUALITY
||
4905 expr
.operator
== BinaryOperator
.INEQUALITY
) {
4906 var left_type
= expr
.left
.target_type
;
4907 var right_type
= expr
.right
.target_type
;
4908 make_comparable_cexpression (ref left_type
, ref cleft
, ref right_type
, ref cright
);
4910 if (left_type is StructValueType
&& right_type is StructValueType
) {
4911 var equalfunc
= generate_struct_equal_function ((Struct
) left_type
.data_type as Struct
);
4912 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
4913 ccall
.add_argument (cleft
);
4914 ccall
.add_argument (cright
);
4916 cright
= new
CCodeConstant ("TRUE");
4917 } else if ((left_type is IntegerType
|| left_type is FloatingType
|| left_type is BooleanType
) && left_type
.nullable
&&
4918 (right_type is IntegerType
|| right_type is FloatingType
|| right_type is BooleanType
) && right_type
.nullable
) {
4919 var equalfunc
= generate_numeric_equal_function ((Struct
) left_type
.data_type as Struct
);
4920 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
4921 ccall
.add_argument (cleft
);
4922 ccall
.add_argument (cright
);
4924 cright
= new
CCodeConstant ("TRUE");
4928 if (!(expr
.left
.value_type is NullType
)
4929 && expr
.left
.value_type
.compatible (string_type
)
4930 && !(expr
.right
.value_type is NullType
)
4931 && expr
.right
.value_type
.compatible (string_type
)) {
4932 if (expr
.operator
== BinaryOperator
.PLUS
) {
4933 // string concatenation
4934 if (expr
.left
.is_constant () && expr
.right
.is_constant ()) {
4937 if (cleft is CCodeIdentifier
) {
4938 left
= ((CCodeIdentifier
) cleft
).name
;
4939 } else if (cleft is CCodeConstant
) {
4940 left
= ((CCodeConstant
) cleft
).name
;
4942 assert_not_reached ();
4944 if (cright is CCodeIdentifier
) {
4945 right
= ((CCodeIdentifier
) cright
).name
;
4946 } else if (cright is CCodeConstant
) {
4947 right
= ((CCodeConstant
) cright
).name
;
4949 assert_not_reached ();
4952 set_cvalue (expr
, new
CCodeConstant ("%s %s".printf (left
, right
)));
4955 if (context
.profile
== Profile
.POSIX
) {
4956 // convert to strcat(strcpy(malloc(1+strlen(a)+strlen(b)),a),b)
4957 var strcat
= new
CCodeFunctionCall (new
CCodeIdentifier ("strcat"));
4958 var strcpy
= new
CCodeFunctionCall (new
CCodeIdentifier ("strcpy"));
4959 var malloc
= new
CCodeFunctionCall (new
CCodeIdentifier ("malloc"));
4961 var strlen_a
= new
CCodeFunctionCall (new
CCodeIdentifier ("strlen"));
4962 strlen_a
.add_argument(cleft
);
4963 var strlen_b
= new
CCodeFunctionCall (new
CCodeIdentifier ("strlen"));
4964 strlen_b
.add_argument(cright
);
4965 var newlength
= new
CCodeBinaryExpression (CCodeBinaryOperator
.PLUS
, new
CCodeIdentifier("1"),
4966 new
CCodeBinaryExpression (CCodeBinaryOperator
.PLUS
, strlen_a
, strlen_b
));
4967 malloc
.add_argument(newlength
);
4969 strcpy
.add_argument(malloc
);
4970 strcpy
.add_argument(cleft
);
4972 strcat
.add_argument(strcpy
);
4973 strcat
.add_argument(cright
);
4974 set_cvalue (expr
, strcat
);
4976 // convert to g_strconcat (a, b, NULL)
4977 var temp_var
= get_temp_variable (expr
.value_type
, true, null, false);
4978 var temp_ref
= get_variable_cexpression (temp_var
.name
);
4979 emit_temp_var (temp_var
);
4981 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strconcat"));
4982 ccall
.add_argument (cleft
);
4983 ccall
.add_argument (cright
);
4984 ccall
.add_argument (new
CCodeConstant("NULL"));
4986 ccode
.add_assignment (temp_ref
, ccall
);
4987 set_cvalue (expr
, temp_ref
);
4991 } else if (expr
.operator
== BinaryOperator
.EQUALITY
4992 || expr
.operator
== BinaryOperator
.INEQUALITY
4993 || expr
.operator
== BinaryOperator
.LESS_THAN
4994 || expr
.operator
== BinaryOperator
.GREATER_THAN
4995 || expr
.operator
== BinaryOperator
.LESS_THAN_OR_EQUAL
4996 || expr
.operator
== BinaryOperator
.GREATER_THAN_OR_EQUAL
) {
4997 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strcmp0"));
4998 ccall
.add_argument (cleft
);
4999 ccall
.add_argument (cright
);
5001 cright
= new
CCodeConstant ("0");
5005 set_cvalue (expr
, new
CCodeBinaryExpression (op
, cleft
, cright
));
5006 if (left_chain
!= null) {
5007 set_cvalue (expr
, new
CCodeBinaryExpression (CCodeBinaryOperator
.AND
, left_chain
, get_cvalue (expr
)));
5011 public string?
get_type_check_function (TypeSymbol type
) {
5012 var cl
= type as Class
;
5013 if (cl
!= null && cl
.type_check_function
!= null) {
5014 return cl
.type_check_function
;
5015 } else if ((cl
!= null && cl
.is_compact
) || type is Struct
|| type is Enum
|| type is Delegate
) {
5018 return type
.get_upper_case_cname ("IS_");
5022 CCodeExpression?
create_type_check (CCodeNode ccodenode
, DataType type
) {
5023 var et
= type as ErrorType
;
5024 if (et
!= null && et
.error_code
!= null) {
5025 var matches_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_error_matches"));
5026 matches_call
.add_argument ((CCodeExpression
) ccodenode
);
5027 matches_call
.add_argument (new
CCodeIdentifier (et
.error_domain
.get_upper_case_cname ()));
5028 matches_call
.add_argument (new
CCodeIdentifier (et
.error_code
.get_cname ()));
5029 return matches_call
;
5030 } else if (et
!= null && et
.error_domain
!= null) {
5031 var instance_domain
= new CCodeMemberAccess
.pointer ((CCodeExpression
) ccodenode
, "domain");
5032 var type_domain
= new
CCodeIdentifier (et
.error_domain
.get_upper_case_cname ());
5033 return new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, instance_domain
, type_domain
);
5035 string type_check_func
= get_type_check_function (type
.data_type
);
5036 if (type_check_func
== null) {
5037 return new
CCodeInvalidExpression ();
5039 var ccheck
= new
CCodeFunctionCall (new
CCodeIdentifier (type_check_func
));
5040 ccheck
.add_argument ((CCodeExpression
) ccodenode
);
5045 string generate_array_contains_wrapper (ArrayType array_type
) {
5046 string array_contains_func
= "_vala_%s_array_contains".printf (array_type
.element_type
.get_lower_case_cname ());
5048 if (!add_wrapper (array_contains_func
)) {
5049 return array_contains_func
;
5052 var function
= new
CCodeFunction (array_contains_func
, "gboolean");
5053 function
.modifiers
= CCodeModifiers
.STATIC
;
5055 function
.add_parameter (new
CCodeParameter ("stack", array_type
.get_cname ()));
5056 function
.add_parameter (new
CCodeParameter ("stack_length", "int"));
5057 if (array_type
.element_type is StructValueType
) {
5058 function
.add_parameter (new
CCodeParameter ("needle", array_type
.element_type
.get_cname () + "*"));
5060 function
.add_parameter (new
CCodeParameter ("needle", array_type
.element_type
.get_cname ()));
5063 push_function (function
);
5065 ccode
.add_declaration ("int", new
CCodeVariableDeclarator ("i"));
5067 var cloop_initializer
= new
CCodeAssignment (new
CCodeIdentifier ("i"), new
CCodeConstant ("0"));
5068 var cloop_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.LESS_THAN
, new
CCodeIdentifier ("i"), new
CCodeIdentifier ("stack_length"));
5069 var cloop_iterator
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POSTFIX_INCREMENT
, new
CCodeIdentifier ("i"));
5070 ccode
.open_for (cloop_initializer
, cloop_condition
, cloop_iterator
);
5072 var celement
= new
CCodeElementAccess (new
CCodeIdentifier ("stack"), new
CCodeIdentifier ("i"));
5073 var cneedle
= new
CCodeIdentifier ("needle");
5074 CCodeBinaryExpression cif_condition
;
5075 if (array_type
.element_type
.compatible (string_type
)) {
5076 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strcmp0"));
5077 ccall
.add_argument (celement
);
5078 ccall
.add_argument (cneedle
);
5079 cif_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, ccall
, new
CCodeConstant ("0"));
5080 } else if (array_type
.element_type is StructValueType
) {
5081 var equalfunc
= generate_struct_equal_function ((Struct
) array_type
.element_type
.data_type as Struct
);
5082 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
5083 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, celement
));
5084 ccall
.add_argument (cneedle
);
5085 cif_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, ccall
, new
CCodeConstant ("TRUE"));
5087 cif_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, cneedle
, celement
);
5090 ccode
.open_if (cif_condition
);
5091 ccode
.add_return (new
CCodeConstant ("TRUE"));
5096 ccode
.add_return (new
CCodeConstant ("FALSE"));
5100 cfile
.add_function_declaration (function
);
5101 cfile
.add_function (function
);
5103 return array_contains_func
;
5106 public override void visit_type_check (TypeCheck expr
) {
5107 generate_type_declaration (expr
.type_reference
, cfile
);
5109 set_cvalue (expr
, create_type_check (get_cvalue (expr
.expression
), expr
.type_reference
));
5110 if (get_cvalue (expr
) is CCodeInvalidExpression
) {
5111 Report
.error (expr
.source_reference
, "type check expressions not supported for compact classes, structs, and enums");
5115 public override void visit_lambda_expression (LambdaExpression lambda
) {
5116 // use instance position from delegate
5117 var dt
= (DelegateType
) lambda
.target_type
;
5118 lambda
.method
.cinstance_parameter_position
= dt
.delegate_symbol
.cinstance_parameter_position
;
5120 lambda
.accept_children (this
);
5122 bool expr_owned
= lambda
.value_type
.value_owned
;
5124 set_cvalue (lambda
, new
CCodeIdentifier (lambda
.method
.get_cname ()));
5126 var delegate_type
= (DelegateType
) lambda
.target_type
;
5127 if (lambda
.method
.closure
) {
5128 int block_id
= get_block_id (current_closure_block
);
5129 var delegate_target
= get_variable_cexpression ("_data%d_".printf (block_id
));
5130 if (expr_owned
|| delegate_type
.is_called_once
) {
5131 var ref_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_ref".printf (block_id
)));
5132 ref_call
.add_argument (delegate_target
);
5133 delegate_target
= ref_call
;
5134 set_delegate_target_destroy_notify (lambda
, new
CCodeIdentifier ("block%d_data_unref".printf (block_id
)));
5136 set_delegate_target_destroy_notify (lambda
, new
CCodeConstant ("NULL"));
5138 set_delegate_target (lambda
, delegate_target
);
5139 } else if (get_this_type () != null || in_constructor
) {
5140 CCodeExpression delegate_target
= get_result_cexpression ("self");
5141 if (expr_owned
|| delegate_type
.is_called_once
) {
5142 if (get_this_type () != null) {
5143 var ref_call
= new
CCodeFunctionCall (get_dup_func_expression (get_this_type (), lambda
.source_reference
));
5144 ref_call
.add_argument (delegate_target
);
5145 delegate_target
= ref_call
;
5146 set_delegate_target_destroy_notify (lambda
, get_destroy_func_expression (get_this_type ()));
5149 var ref_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_object_ref"));
5150 ref_call
.add_argument (delegate_target
);
5151 delegate_target
= ref_call
;
5152 set_delegate_target_destroy_notify (lambda
, new
CCodeIdentifier ("g_object_unref"));
5155 set_delegate_target_destroy_notify (lambda
, new
CCodeConstant ("NULL"));
5157 set_delegate_target (lambda
, delegate_target
);
5159 set_delegate_target (lambda
, new
CCodeConstant ("NULL"));
5160 set_delegate_target_destroy_notify (lambda
, new
CCodeConstant ("NULL"));
5164 public CCodeExpression
convert_from_generic_pointer (CCodeExpression cexpr
, DataType actual_type
) {
5166 if (is_reference_type_argument (actual_type
) || is_nullable_value_type_argument (actual_type
)) {
5167 result
= new
CCodeCastExpression (cexpr
, actual_type
.get_cname ());
5168 } else if (is_signed_integer_type_argument (actual_type
)) {
5169 var cconv
= new
CCodeFunctionCall (new
CCodeIdentifier ("GPOINTER_TO_INT"));
5170 cconv
.add_argument (cexpr
);
5172 } else if (is_unsigned_integer_type_argument (actual_type
)) {
5173 var cconv
= new
CCodeFunctionCall (new
CCodeIdentifier ("GPOINTER_TO_UINT"));
5174 cconv
.add_argument (cexpr
);
5180 public CCodeExpression
convert_to_generic_pointer (CCodeExpression cexpr
, DataType actual_type
) {
5182 if (is_signed_integer_type_argument (actual_type
)) {
5183 var cconv
= new
CCodeFunctionCall (new
CCodeIdentifier ("GINT_TO_POINTER"));
5184 cconv
.add_argument (cexpr
);
5186 } else if (is_unsigned_integer_type_argument (actual_type
)) {
5187 var cconv
= new
CCodeFunctionCall (new
CCodeIdentifier ("GUINT_TO_POINTER"));
5188 cconv
.add_argument (cexpr
);
5194 // manage memory and implicit casts
5195 public CCodeExpression
transform_expression (CCodeExpression source_cexpr
, DataType? expression_type
, DataType? target_type
, Expression? expr
= null) {
5196 var cexpr
= source_cexpr
;
5197 if (expression_type
== null) {
5202 if (expression_type
.value_owned
5203 && expression_type
.floating_reference
) {
5204 /* floating reference, sink it.
5206 var cl
= expression_type
.data_type as ObjectTypeSymbol
;
5207 var sink_func
= (cl
!= null) ? cl
.get_ref_sink_function () : null;
5209 if (sink_func
!= null) {
5210 var csink
= new
CCodeFunctionCall (new
CCodeIdentifier (sink_func
));
5211 csink
.add_argument (cexpr
);
5215 Report
.error (null, "type `%s' does not support floating references".printf (expression_type
.data_type
.name
));
5219 bool boxing
= (expression_type is ValueType
&& !expression_type
.nullable
5220 && target_type is ValueType
&& target_type
.nullable
);
5221 bool unboxing
= (expression_type is ValueType
&& expression_type
.nullable
5222 && target_type is ValueType
&& !target_type
.nullable
);
5224 bool gvalue_boxing
= (context
.profile
== Profile
.GOBJECT
5225 && target_type
!= null
5226 && target_type
.data_type
== gvalue_type
5227 && !(expression_type is NullType
)
5228 && expression_type
.get_type_id () != "G_TYPE_VALUE");
5229 bool gvariant_boxing
= (context
.profile
== Profile
.GOBJECT
5230 && target_type
!= null
5231 && target_type
.data_type
== gvariant_type
5232 && !(expression_type is NullType
)
5233 && expression_type
.data_type
!= gvariant_type
);
5235 if (expression_type
.value_owned
5236 && (target_type
== null || !target_type
.value_owned
|| boxing
|| unboxing
)
5237 && !gvalue_boxing
/* gvalue can assume ownership of value, no need to free it */) {
5238 // value leaked, destroy it
5239 var pointer_type
= target_type as PointerType
;
5240 if (pointer_type
!= null && !(pointer_type
.base_type is VoidType
)) {
5241 // manual memory management for non-void pointers
5242 // treat void* special to not leak memory with void* method parameters
5243 } else if (requires_destroy (expression_type
)) {
5244 var decl
= get_temp_variable (expression_type
, true, expression_type
, false);
5245 emit_temp_var (decl
);
5246 temp_ref_vars
.insert (0, decl
);
5247 ccode
.add_assignment (get_variable_cexpression (decl
.name
), cexpr
);
5248 cexpr
= get_variable_cexpression (decl
.name
);
5250 if (expression_type is ArrayType
&& expr
!= null) {
5251 var array_type
= (ArrayType
) expression_type
;
5252 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5253 var len_decl
= new
LocalVariable (int_type
.copy (), get_array_length_cname (decl
.name
, dim
));
5254 emit_temp_var (len_decl
);
5255 ccode
.add_assignment (get_variable_cexpression (len_decl
.name
), get_array_length_cexpression (expr
, dim
));
5257 } else if (expression_type is DelegateType
&& expr
!= null) {
5258 var target_decl
= new
LocalVariable (new
PointerType (new
VoidType ()), get_delegate_target_cname (decl
.name
));
5259 emit_temp_var (target_decl
);
5260 var target_destroy_notify_decl
= new
LocalVariable (gdestroynotify_type
, get_delegate_target_destroy_notify_cname (decl
.name
));
5261 emit_temp_var (target_destroy_notify_decl
);
5262 CCodeExpression target_destroy_notify
;
5263 ccode
.add_assignment (get_variable_cexpression (target_decl
.name
), get_delegate_target_cexpression (expr
, out target_destroy_notify
));
5264 ccode
.add_assignment (get_variable_cexpression (target_destroy_notify_decl
.name
), target_destroy_notify
);
5270 if (target_type
== null) {
5271 // value will be destroyed, no need for implicit casts
5275 if (gvalue_boxing
) {
5276 // implicit conversion to GValue
5277 var decl
= get_temp_variable (target_type
, true, target_type
);
5278 emit_temp_var (decl
);
5280 if (!target_type
.value_owned
) {
5281 // boxed GValue leaked, destroy it
5282 temp_ref_vars
.insert (0, decl
);
5285 if (target_type
.nullable
) {
5286 var newcall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_new0"));
5287 newcall
.add_argument (new
CCodeConstant ("GValue"));
5288 newcall
.add_argument (new
CCodeConstant ("1"));
5289 var newassignment
= new
CCodeAssignment (get_variable_cexpression (decl
.name
), newcall
);
5290 ccode
.add_expression (newassignment
);
5293 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_value_init"));
5294 if (target_type
.nullable
) {
5295 ccall
.add_argument (get_variable_cexpression (decl
.name
));
5297 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (decl
.name
)));
5299 ccall
.add_argument (new
CCodeIdentifier (expression_type
.get_type_id ()));
5300 ccode
.add_expression (ccall
);
5302 if (requires_destroy (expression_type
)) {
5303 ccall
= new
CCodeFunctionCall (get_value_taker_function (expression_type
));
5305 ccall
= new
CCodeFunctionCall (get_value_setter_function (expression_type
));
5307 if (target_type
.nullable
) {
5308 ccall
.add_argument (get_variable_cexpression (decl
.name
));
5310 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (decl
.name
)));
5312 if (expression_type
.is_real_non_null_struct_type ()) {
5313 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
));
5315 ccall
.add_argument (cexpr
);
5318 ccode
.add_expression (ccall
);
5320 cexpr
= get_variable_cexpression (decl
.name
);
5323 } else if (gvariant_boxing
) {
5324 // implicit conversion to GVariant
5325 string variant_func
= "_variant_new%d".printf (++next_variant_function_id
);
5327 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (variant_func
));
5328 ccall
.add_argument (cexpr
);
5330 var cfunc
= new
CCodeFunction (variant_func
, "GVariant*");
5331 cfunc
.modifiers
= CCodeModifiers
.STATIC
;
5332 cfunc
.add_parameter (new
CCodeParameter ("value", expression_type
.get_cname ()));
5334 if (expression_type is ArrayType
) {
5335 // return array length if appropriate
5336 var array_type
= (ArrayType
) expression_type
;
5338 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5339 ccall
.add_argument (get_array_length_cexpression (expr
, dim
));
5340 cfunc
.add_parameter (new
CCodeParameter (get_array_length_cname ("value", dim
), "gint"));
5344 push_function (cfunc
);
5346 var result
= serialize_expression (expression_type
, new
CCodeIdentifier ("value"));
5348 // sink floating reference
5349 var sink
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_variant_ref_sink"));
5350 sink
.add_argument (result
);
5351 ccode
.add_return (sink
);
5355 cfile
.add_function_declaration (cfunc
);
5356 cfile
.add_function (cfunc
);
5359 } else if (boxing
) {
5360 // value needs to be boxed
5362 var unary
= cexpr as CCodeUnaryExpression
;
5363 if (unary
!= null && unary
.operator
== CCodeUnaryOperator
.POINTER_INDIRECTION
) {
5365 cexpr
= unary
.inner
;
5366 } else if (cexpr is CCodeIdentifier
|| cexpr is CCodeMemberAccess
) {
5367 cexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
);
5369 var decl
= get_temp_variable (expression_type
, expression_type
.value_owned
, expression_type
, false);
5370 emit_temp_var (decl
);
5372 ccode
.add_assignment (get_variable_cexpression (decl
.name
), cexpr
);
5373 cexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (decl
.name
));
5375 } else if (unboxing
) {
5378 cexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, cexpr
);
5380 cexpr
= get_implicit_cast_expression (cexpr
, expression_type
, target_type
, expr
);
5383 if (target_type
.value_owned
&& (!expression_type
.value_owned
|| boxing
|| unboxing
)) {
5384 // need to copy value
5385 if (requires_copy (target_type
) && !(expression_type is NullType
)) {
5386 CodeNode node
= expr
;
5388 node
= expression_type
;
5391 var decl
= get_temp_variable (target_type
, true, node
, false);
5392 emit_temp_var (decl
);
5393 ccode
.add_assignment (get_variable_cexpression (decl
.name
), get_ref_cexpression (target_type
, cexpr
, expr
, node
));
5394 cexpr
= get_variable_cexpression (decl
.name
);
5401 public virtual CCodeExpression
get_implicit_cast_expression (CCodeExpression source_cexpr
, DataType? expression_type
, DataType? target_type
, Expression? expr
= null) {
5402 var cexpr
= source_cexpr
;
5404 if (expression_type
.data_type
!= null && expression_type
.data_type
== target_type
.data_type
) {
5405 // same type, no cast required
5409 if (expression_type is NullType
) {
5410 // null literal, no cast required when not converting to generic type pointer
5414 generate_type_declaration (target_type
, cfile
);
5416 var cl
= target_type
.data_type as Class
;
5417 var iface
= target_type
.data_type as Interface
;
5418 if (context
.checking
&& (iface
!= null || (cl
!= null && !cl
.is_compact
))) {
5419 // checked cast for strict subtypes of GTypeInstance
5420 return generate_instance_cast (cexpr
, target_type
.data_type
);
5421 } else if (target_type
.data_type
!= null && expression_type
.get_cname () != target_type
.get_cname ()) {
5422 var st
= target_type
.data_type as Struct
;
5423 if (target_type
.data_type
.is_reference_type () || (st
!= null && st
.is_simple_type ())) {
5424 // don't cast non-simple structs
5425 return new
CCodeCastExpression (cexpr
, target_type
.get_cname ());
5434 public void store_property (Property prop
, Expression? instance
, TargetValue value
) {
5435 if (instance is BaseAccess
) {
5436 if (prop
.base_property
!= null) {
5437 var base_class
= (Class
) prop
.base_property
.parent_symbol
;
5438 var vcast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_CLASS".printf (base_class
.get_upper_case_cname (null))));
5439 vcast
.add_argument (new
CCodeIdentifier ("%s_parent_class".printf (current_class
.get_lower_case_cname (null))));
5441 var ccall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (vcast
, "set_%s".printf (prop
.name
)));
5442 ccall
.add_argument ((CCodeExpression
) get_ccodenode (instance
));
5443 ccall
.add_argument (get_cvalue_ (value
));
5445 ccode
.add_expression (ccall
);
5446 } else if (prop
.base_interface_property
!= null) {
5447 var base_iface
= (Interface
) prop
.base_interface_property
.parent_symbol
;
5448 string parent_iface_var
= "%s_%s_parent_iface".printf (current_class
.get_lower_case_cname (null), base_iface
.get_lower_case_cname (null));
5450 var ccall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (new
CCodeIdentifier (parent_iface_var
), "set_%s".printf (prop
.name
)));
5451 ccall
.add_argument ((CCodeExpression
) get_ccodenode (instance
));
5452 ccall
.add_argument (get_cvalue_ (value
));
5454 ccode
.add_expression (ccall
);
5459 var set_func
= "g_object_set";
5461 var base_property
= prop
;
5462 if (!prop
.no_accessor_method
) {
5463 if (prop
.base_property
!= null) {
5464 base_property
= prop
.base_property
;
5465 } else if (prop
.base_interface_property
!= null) {
5466 base_property
= prop
.base_interface_property
;
5469 if (prop is DynamicProperty
) {
5470 set_func
= get_dynamic_property_setter_cname ((DynamicProperty
) prop
);
5472 generate_property_accessor_declaration (base_property
.set_accessor
, cfile
);
5473 set_func
= base_property
.set_accessor
.get_cname ();
5475 if (!prop
.external
&& prop
.external_package
) {
5476 // internal VAPI properties
5477 // only add them once per source file
5478 if (add_generated_external_symbol (prop
)) {
5479 visit_property (prop
);
5485 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (set_func
));
5487 if (prop
.binding
== MemberBinding
.INSTANCE
) {
5488 /* target instance is first argument */
5489 var cinstance
= (CCodeExpression
) get_ccodenode (instance
);
5491 if (prop
.parent_symbol is Struct
) {
5492 // we need to pass struct instance by reference
5493 var unary
= cinstance as CCodeUnaryExpression
;
5494 if (unary
!= null && unary
.operator
== CCodeUnaryOperator
.POINTER_INDIRECTION
) {
5496 cinstance
= unary
.inner
;
5497 } else if (cinstance is CCodeIdentifier
|| cinstance is CCodeMemberAccess
) {
5498 cinstance
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cinstance
);
5500 // if instance is e.g. a function call, we can't take the address of the expression
5501 // (tmp = expr, &tmp)
5503 var temp_var
= get_temp_variable (instance
.target_type
, true, null, false);
5504 emit_temp_var (temp_var
);
5505 ccode
.add_assignment (get_variable_cexpression (temp_var
.name
), cinstance
);
5507 cinstance
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (temp_var
.name
));
5511 ccall
.add_argument (cinstance
);
5514 if (prop
.no_accessor_method
) {
5515 /* property name is second argument of g_object_set */
5516 ccall
.add_argument (prop
.get_canonical_cconstant ());
5519 var cexpr
= get_cvalue_ (value
);
5521 if (prop
.property_type
.is_real_non_null_struct_type ()) {
5522 cexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
);
5525 var array_type
= prop
.property_type as ArrayType
;
5527 if (array_type
!= null && !prop
.no_array_length
) {
5528 var temp_var
= get_temp_variable (prop
.property_type
, true, null, false);
5529 emit_temp_var (temp_var
);
5530 ccode
.add_assignment (get_variable_cexpression (temp_var
.name
), cexpr
);
5531 ccall
.add_argument (get_variable_cexpression (temp_var
.name
));
5533 ccall
.add_argument (cexpr
);
5536 if (array_type
!= null && !prop
.no_array_length
) {
5537 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5538 ccall
.add_argument (get_array_length_cvalue (value
, dim
));
5540 } else if (prop
.property_type is DelegateType
) {
5541 var delegate_type
= (DelegateType
) prop
.property_type
;
5542 if (delegate_type
.delegate_symbol
.has_target
) {
5543 ccall
.add_argument (get_delegate_target_cvalue (value
));
5547 if (prop
.no_accessor_method
) {
5548 ccall
.add_argument (new
CCodeConstant ("NULL"));
5551 ccode
.add_expression (ccall
);
5554 public bool add_wrapper (string wrapper_name
) {
5555 return wrappers
.add (wrapper_name
);
5558 public bool add_generated_external_symbol (Symbol external_symbol
) {
5559 return generated_external_symbols
.add (external_symbol
);
5562 public static DataType
get_data_type_for_symbol (TypeSymbol sym
) {
5563 DataType type
= null;
5566 type
= new
ObjectType ((Class
) sym
);
5567 } else if (sym is Interface
) {
5568 type
= new
ObjectType ((Interface
) sym
);
5569 } else if (sym is Struct
) {
5570 var st
= (Struct
) sym
;
5571 if (st
.is_boolean_type ()) {
5572 type
= new
BooleanType (st
);
5573 } else if (st
.is_integer_type ()) {
5574 type
= new
IntegerType (st
);
5575 } else if (st
.is_floating_type ()) {
5576 type
= new
FloatingType (st
);
5578 type
= new
StructValueType (st
);
5580 } else if (sym is Enum
) {
5581 type
= new
EnumValueType ((Enum
) sym
);
5582 } else if (sym is ErrorDomain
) {
5583 type
= new
ErrorType ((ErrorDomain
) sym
, null);
5584 } else if (sym is ErrorCode
) {
5585 type
= new
ErrorType ((ErrorDomain
) sym
.parent_symbol
, (ErrorCode
) sym
);
5587 Report
.error (null, "internal error: `%s' is not a supported type".printf (sym
.get_full_name ()));
5588 return new
InvalidType ();
5594 public CCodeExpression?
default_value_for_type (DataType type
, bool initializer_expression
) {
5595 var st
= type
.data_type as Struct
;
5596 var array_type
= type as ArrayType
;
5597 if (initializer_expression
&& !type
.nullable
&&
5598 ((st
!= null && !st
.is_simple_type ()) ||
5599 (array_type
!= null && array_type
.fixed_length
))) {
5600 // 0-initialize struct with struct initializer { 0 }
5601 // only allowed as initializer expression in C
5602 var clist
= new
CCodeInitializerList ();
5603 clist
.append (new
CCodeConstant ("0"));
5605 } else if ((type
.data_type
!= null && type
.data_type
.is_reference_type ())
5607 || type is PointerType
|| type is DelegateType
5608 || (array_type
!= null && !array_type
.fixed_length
)) {
5609 return new
CCodeConstant ("NULL");
5610 } else if (type
.data_type
!= null && type
.data_type
.get_default_value () != null) {
5611 return new
CCodeConstant (type
.data_type
.get_default_value ());
5612 } else if (type
.type_parameter
!= null) {
5613 return new
CCodeConstant ("NULL");
5614 } else if (type is ErrorType
) {
5615 return new
CCodeConstant ("NULL");
5620 private void create_property_type_check_statement (Property prop
, bool check_return_type
, TypeSymbol t
, bool non_null
, string var_name
) {
5621 if (check_return_type
) {
5622 create_type_check_statement (prop
, prop
.property_type
, t
, non_null
, var_name
);
5624 create_type_check_statement (prop
, new
VoidType (), t
, non_null
, var_name
);
5628 public void create_type_check_statement (CodeNode method_node
, DataType ret_type
, TypeSymbol t
, bool non_null
, string var_name
) {
5629 var ccheck
= new
CCodeFunctionCall ();
5631 if (!context
.assert
) {
5633 } else if (context
.checking
&& ((t is Class
&& !((Class
) t
).is_compact
) || t is Interface
)) {
5634 var ctype_check
= new
CCodeFunctionCall (new
CCodeIdentifier (get_type_check_function (t
)));
5635 ctype_check
.add_argument (new
CCodeIdentifier (var_name
));
5637 CCodeExpression cexpr
= ctype_check
;
5639 var cnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier (var_name
), new
CCodeConstant ("NULL"));
5641 cexpr
= new
CCodeBinaryExpression (CCodeBinaryOperator
.OR
, cnull
, ctype_check
);
5643 ccheck
.add_argument (cexpr
);
5644 } else if (!non_null
) {
5646 } else if (t
== glist_type
|| t
== gslist_type
) {
5647 // NULL is empty list
5650 var cnonnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, new
CCodeIdentifier (var_name
), new
CCodeConstant ("NULL"));
5651 ccheck
.add_argument (cnonnull
);
5654 var cm
= method_node as CreationMethod
;
5655 if (cm
!= null && cm
.parent_symbol is ObjectTypeSymbol
) {
5656 ccheck
.call
= new
CCodeIdentifier ("g_return_val_if_fail");
5657 ccheck
.add_argument (new
CCodeConstant ("NULL"));
5658 } else if (ret_type is VoidType
) {
5660 ccheck
.call
= new
CCodeIdentifier ("g_return_if_fail");
5662 ccheck
.call
= new
CCodeIdentifier ("g_return_val_if_fail");
5664 var cdefault
= default_value_for_type (ret_type
, false);
5665 if (cdefault
!= null) {
5666 ccheck
.add_argument (cdefault
);
5672 ccode
.add_expression (ccheck
);
5675 public int get_param_pos (double param_pos
, bool ellipsis
= false) {
5677 if (param_pos
>= 0) {
5678 return (int) (param_pos
* 1000);
5680 return (int) ((100 + param_pos
) * 1000);
5683 if (param_pos
>= 0) {
5684 return (int) ((100 + param_pos
) * 1000);
5686 return (int) ((200 + param_pos
) * 1000);
5691 public CCodeExpression?
get_ccodenode (Expression node
) {
5692 if (get_cvalue (node
) == null) {
5695 return get_cvalue (node
);
5698 public override void visit_class (Class cl
) {
5701 public void create_postcondition_statement (Expression postcondition
) {
5702 var cassert
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_warn_if_fail"));
5704 postcondition
.emit (this
);
5706 cassert
.add_argument (get_cvalue (postcondition
));
5708 ccode
.add_expression (cassert
);
5711 public virtual bool is_gobject_property (Property prop
) {
5715 public DataType?
get_this_type () {
5716 if (current_method
!= null && current_method
.binding
== MemberBinding
.INSTANCE
) {
5717 return current_method
.this_parameter
.variable_type
;
5718 } else if (current_property_accessor
!= null && current_property_accessor
.prop
.binding
== MemberBinding
.INSTANCE
) {
5719 return current_property_accessor
.prop
.this_parameter
.variable_type
;
5724 public CCodeFunctionCall
generate_instance_cast (CCodeExpression expr
, TypeSymbol type
) {
5725 var result
= new
CCodeFunctionCall (new
CCodeIdentifier (type
.get_upper_case_cname (null)));
5726 result
.add_argument (expr
);
5730 void generate_struct_destroy_function (Struct st
) {
5731 if (cfile
.add_declaration (st
.get_destroy_function ())) {
5732 // only generate function once per source file
5736 var function
= new
CCodeFunction (st
.get_destroy_function (), "void");
5737 function
.modifiers
= CCodeModifiers
.STATIC
;
5738 function
.add_parameter (new
CCodeParameter ("self", st
.get_cname () + "*"));
5740 push_function (function
);
5742 foreach (Field f
in st
.get_fields ()) {
5743 if (f
.binding
== MemberBinding
.INSTANCE
) {
5744 if (requires_destroy (f
.variable_type
)) {
5745 var this_access
= new MemberAccess
.simple ("this");
5746 this_access
.value_type
= get_data_type_for_symbol ((TypeSymbol
) f
.parent_symbol
);
5747 set_cvalue (this_access
, new
CCodeIdentifier ("(*self)"));
5749 ccode
.add_expression (destroy_field (f
, this_access
.target_value
));
5756 cfile
.add_function_declaration (function
);
5757 cfile
.add_function (function
);
5760 void generate_struct_copy_function (Struct st
) {
5761 if (cfile
.add_declaration (st
.get_copy_function ())) {
5762 // only generate function once per source file
5766 var function
= new
CCodeFunction (st
.get_copy_function (), "void");
5767 function
.modifiers
= CCodeModifiers
.STATIC
;
5768 function
.add_parameter (new
CCodeParameter ("self", "const " + st
.get_cname () + "*"));
5769 function
.add_parameter (new
CCodeParameter ("dest", st
.get_cname () + "*"));
5771 push_context (new
EmitContext ());
5772 push_function (function
);
5774 foreach (Field f
in st
.get_fields ()) {
5775 if (f
.binding
== MemberBinding
.INSTANCE
) {
5776 CCodeExpression copy
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("self"), f
.name
);
5777 if (requires_copy (f
.variable_type
)) {
5778 var this_access
= new MemberAccess
.simple ("this");
5779 this_access
.value_type
= get_data_type_for_symbol ((TypeSymbol
) f
.parent_symbol
);
5780 set_cvalue (this_access
, new
CCodeIdentifier ("(*self)"));
5781 var ma
= new
MemberAccess (this_access
, f
.name
);
5782 ma
.symbol_reference
= f
;
5783 ma
.value_type
= f
.variable_type
.copy ();
5784 visit_member_access (ma
);
5785 copy
= get_ref_cexpression (f
.variable_type
, copy
, ma
, f
);
5787 var dest
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("dest"), f
.name
);
5789 var array_type
= f
.variable_type as ArrayType
;
5790 if (array_type
!= null && array_type
.fixed_length
) {
5791 // fixed-length (stack-allocated) arrays
5792 cfile
.add_include ("string.h");
5794 var sizeof_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
5795 sizeof_call
.add_argument (new
CCodeIdentifier (array_type
.element_type
.get_cname ()));
5796 var size
= new
CCodeBinaryExpression (CCodeBinaryOperator
.MUL
, new
CCodeConstant ("%d".printf (array_type
.length
)), sizeof_call
);
5798 var array_copy_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memcpy"));
5799 array_copy_call
.add_argument (dest
);
5800 array_copy_call
.add_argument (copy
);
5801 array_copy_call
.add_argument (size
);
5802 ccode
.add_expression (array_copy_call
);
5804 ccode
.add_assignment (dest
, copy
);
5806 if (array_type
!= null && !f
.no_array_length
) {
5807 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5808 var len_src
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("self"), get_array_length_cname (f
.name
, dim
));
5809 var len_dest
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("dest"), get_array_length_cname (f
.name
, dim
));
5810 ccode
.add_assignment (len_dest
, len_src
);
5820 cfile
.add_function_declaration (function
);
5821 cfile
.add_function (function
);
5824 public void return_default_value (DataType return_type
) {
5825 ccode
.add_return (default_value_for_type (return_type
, false));
5828 public virtual string?
get_custom_creturn_type (Method m
) {
5832 public virtual void generate_dynamic_method_wrapper (DynamicMethod method
) {
5835 public virtual bool method_has_wrapper (Method method
) {
5839 public virtual CCodeFunctionCall
get_param_spec (Property prop
) {
5840 return new
CCodeFunctionCall (new
CCodeIdentifier (""));
5843 public virtual CCodeFunctionCall
get_signal_creation (Signal sig
, TypeSymbol type
) {
5844 return new
CCodeFunctionCall (new
CCodeIdentifier (""));
5847 public virtual void register_dbus_info (CCodeBlock block
, ObjectTypeSymbol bindable
) {
5850 public virtual string get_dynamic_property_getter_cname (DynamicProperty node
) {
5851 Report
.error (node
.source_reference
, "dynamic properties are not supported for %s".printf (node
.dynamic_type
.to_string ()));
5855 public virtual string get_dynamic_property_setter_cname (DynamicProperty node
) {
5856 Report
.error (node
.source_reference
, "dynamic properties are not supported for %s".printf (node
.dynamic_type
.to_string ()));
5860 public virtual string get_dynamic_signal_cname (DynamicSignal node
) {
5864 public virtual string get_dynamic_signal_connect_wrapper_name (DynamicSignal node
) {
5868 public virtual string get_dynamic_signal_connect_after_wrapper_name (DynamicSignal node
) {
5872 public virtual string get_dynamic_signal_disconnect_wrapper_name (DynamicSignal node
) {
5876 public virtual void generate_marshaller (List
<Parameter
> params
, DataType return_type
, bool dbus
= false) {
5879 public virtual string get_marshaller_function (List
<Parameter
> params
, DataType return_type
, string? prefix
= null, bool dbus
= false) {
5883 public virtual string get_array_length_cname (string array_cname
, int dim
) {
5887 public virtual string get_parameter_array_length_cname (Parameter param
, int dim
) {
5891 public virtual CCodeExpression
get_array_length_cexpression (Expression array_expr
, int dim
= -1) {
5892 return new
CCodeConstant ("");
5895 public virtual CCodeExpression
get_array_length_cvalue (TargetValue value
, int dim
= -1) {
5896 return new
CCodeInvalidExpression ();
5899 public virtual string get_array_size_cname (string array_cname
) {
5903 public virtual void add_simple_check (CodeNode node
, bool always_fails
= false) {
5906 public virtual string generate_ready_function (Method m
) {
5910 public CCodeExpression?
get_cvalue (Expression expr
) {
5911 if (expr
.target_value
== null) {
5914 var glib_value
= (GLibValue
) expr
.target_value
;
5915 return glib_value
.cvalue
;
5918 public CCodeExpression?
get_cvalue_ (TargetValue value
) {
5919 var glib_value
= (GLibValue
) value
;
5920 return glib_value
.cvalue
;
5923 public void set_cvalue (Expression expr
, CCodeExpression? cvalue
) {
5924 var glib_value
= (GLibValue
) expr
.target_value
;
5925 if (glib_value
== null) {
5926 glib_value
= new
GLibValue (expr
.value_type
);
5927 expr
.target_value
= glib_value
;
5929 glib_value
.cvalue
= cvalue
;
5932 public CCodeExpression?
get_array_size_cvalue (TargetValue value
) {
5933 var glib_value
= (GLibValue
) value
;
5934 return glib_value
.array_size_cvalue
;
5937 public void set_array_size_cvalue (TargetValue value
, CCodeExpression? cvalue
) {
5938 var glib_value
= (GLibValue
) value
;
5939 glib_value
.array_size_cvalue
= cvalue
;
5942 public CCodeExpression?
get_delegate_target (Expression expr
) {
5943 if (expr
.target_value
== null) {
5946 var glib_value
= (GLibValue
) expr
.target_value
;
5947 return glib_value
.delegate_target_cvalue
;
5950 public void set_delegate_target (Expression expr
, CCodeExpression? delegate_target
) {
5951 var glib_value
= (GLibValue
) expr
.target_value
;
5952 if (glib_value
== null) {
5953 glib_value
= new
GLibValue (expr
.value_type
);
5954 expr
.target_value
= glib_value
;
5956 glib_value
.delegate_target_cvalue
= delegate_target
;
5959 public CCodeExpression?
get_delegate_target_destroy_notify (Expression expr
) {
5960 if (expr
.target_value
== null) {
5963 var glib_value
= (GLibValue
) expr
.target_value
;
5964 return glib_value
.delegate_target_destroy_notify_cvalue
;
5967 public void set_delegate_target_destroy_notify (Expression expr
, CCodeExpression? destroy_notify
) {
5968 var glib_value
= (GLibValue
) expr
.target_value
;
5969 if (glib_value
== null) {
5970 glib_value
= new
GLibValue (expr
.value_type
);
5971 expr
.target_value
= glib_value
;
5973 glib_value
.delegate_target_destroy_notify_cvalue
= destroy_notify
;
5976 public void append_array_length (Expression expr
, CCodeExpression size
) {
5977 var glib_value
= (GLibValue
) expr
.target_value
;
5978 if (glib_value
== null) {
5979 glib_value
= new
GLibValue (expr
.value_type
);
5980 expr
.target_value
= glib_value
;
5982 glib_value
.append_array_length_cvalue (size
);
5985 public List
<CCodeExpression
>?
get_array_lengths (Expression expr
) {
5986 var glib_value
= (GLibValue
) expr
.target_value
;
5987 if (glib_value
== null) {
5988 glib_value
= new
GLibValue (expr
.value_type
);
5989 expr
.target_value
= glib_value
;
5991 return glib_value
.array_length_cvalues
;
5995 public class Vala
.GLibValue
: TargetValue
{
5996 public CCodeExpression cvalue
;
5998 public List
<CCodeExpression
> array_length_cvalues
;
5999 public CCodeExpression? array_size_cvalue
;
6001 public CCodeExpression? delegate_target_cvalue
;
6002 public CCodeExpression? delegate_target_destroy_notify_cvalue
;
6004 public GLibValue (DataType? value_type
= null, CCodeExpression? cvalue
= null) {
6006 this
.cvalue
= cvalue
;
6009 public void append_array_length_cvalue (CCodeExpression length_cvalue
) {
6010 if (array_length_cvalues
== null) {
6011 array_length_cvalues
= new ArrayList
<CCodeExpression
> ();
6013 array_length_cvalues
.add (length_cvalue
);