1 /* valaccodebasemodule.vala
3 * Copyright (C) 2006-2012 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
<TargetValue
> temp_ref_values
= new ArrayList
<TargetValue
> ();
38 public int next_temp_var_id
;
39 public bool current_method_inner_error
;
40 public bool current_method_return
;
41 public int next_coroutine_state
= 1;
42 public Map
<string,string> variable_name_map
= new HashMap
<string,string> (str_hash
, str_equal
);
43 public Map
<string,int> closure_variable_count_map
= new HashMap
<string,int> (str_hash
, str_equal
);
44 public Map
<LocalVariable
,int> closure_variable_clash_map
= new HashMap
<LocalVariable
,int> ();
46 public EmitContext (Symbol? symbol
= null) {
47 current_symbol
= symbol
;
50 public void push_symbol (Symbol symbol
) {
51 symbol_stack
.add (current_symbol
);
52 current_symbol
= symbol
;
55 public void pop_symbol () {
56 current_symbol
= symbol_stack
.remove_at (symbol_stack
.size
- 1);
60 public CodeContext context
{ get; set; }
62 public Symbol root_symbol
;
64 public EmitContext emit_context
= new
EmitContext ();
66 List
<EmitContext
> emit_context_stack
= new ArrayList
<EmitContext
> ();
68 public CCodeLineDirective? current_line
= null;
70 List
<CCodeLineDirective
> line_directive_stack
= new ArrayList
<CCodeLineDirective
> ();
72 public Symbol current_symbol
{ get { return emit_context
.current_symbol
; } }
74 public TryStatement current_try
{
75 get { return emit_context
.current_try
; }
76 set { emit_context
.current_try
= value
; }
79 public CatchClause current_catch
{
80 get { return emit_context
.current_catch
; }
81 set { emit_context
.current_catch
= value
; }
84 public TypeSymbol? current_type_symbol
{
86 var sym
= current_symbol
;
88 if (sym is TypeSymbol
) {
89 return (TypeSymbol
) sym
;
91 sym
= sym
.parent_symbol
;
97 public Class? current_class
{
98 get { return current_type_symbol as Class
; }
101 public Method? current_method
{
103 var sym
= current_symbol
;
104 while (sym is Block
) {
105 sym
= sym
.parent_symbol
;
107 return sym as Method
;
111 public PropertyAccessor? current_property_accessor
{
113 var sym
= current_symbol
;
114 while (sym is Block
) {
115 sym
= sym
.parent_symbol
;
117 return sym as PropertyAccessor
;
121 public Constructor? current_constructor
{
123 var sym
= current_symbol
;
124 while (sym is Block
) {
125 sym
= sym
.parent_symbol
;
127 return sym as Constructor
;
131 public Destructor? current_destructor
{
133 var sym
= current_symbol
;
134 while (sym is Block
) {
135 sym
= sym
.parent_symbol
;
137 return sym as Destructor
;
141 public DataType? current_return_type
{
143 var m
= current_method
;
145 return m
.return_type
;
148 var acc
= current_property_accessor
;
151 return acc
.value_type
;
157 if (is_in_constructor () || is_in_destructor ()) {
165 public bool is_in_coroutine () {
166 return current_method
!= null && current_method
.coroutine
;
169 public bool is_in_constructor () {
170 if (current_method
!= null) {
171 // make sure to not return true in lambda expression inside constructor
174 var sym
= current_symbol
;
175 while (sym
!= null) {
176 if (sym is Constructor
) {
179 sym
= sym
.parent_symbol
;
184 public bool is_in_destructor () {
185 if (current_method
!= null) {
186 // make sure to not return true in lambda expression inside constructor
189 var sym
= current_symbol
;
190 while (sym
!= null) {
191 if (sym is Destructor
) {
194 sym
= sym
.parent_symbol
;
199 public Block? current_closure_block
{
201 return next_closure_block (current_symbol
);
205 public unowned Block?
next_closure_block (Symbol sym
) {
207 unowned Method method
= sym as Method
;
208 if (method
!= null && !method
.closure
) {
209 // parent blocks are not captured by this method
213 unowned Block block
= sym as Block
;
214 if (method
== null && block
== null) {
219 if (block
!= null && block
.captured
) {
220 // closure block found
223 sym
= sym
.parent_symbol
;
228 public CCodeFile header_file
;
229 public CCodeFile internal_header_file
;
230 public CCodeFile cfile
;
232 public EmitContext class_init_context
;
233 public EmitContext base_init_context
;
234 public EmitContext class_finalize_context
;
235 public EmitContext base_finalize_context
;
236 public EmitContext instance_init_context
;
237 public EmitContext instance_finalize_context
;
239 public CCodeStruct param_spec_struct
;
240 public CCodeStruct closure_struct
;
241 public CCodeEnum prop_enum
;
242 public CCodeEnum signal_enum
;
244 public CCodeFunction ccode
{ get { return emit_context
.ccode
; } }
246 /* temporary variables that own their content */
247 public ArrayList
<TargetValue
> temp_ref_values
{ get { return emit_context
.temp_ref_values
; } }
248 /* cache to check whether a certain marshaller has been created yet */
249 public Set
<string> user_marshal_set
;
250 /* (constant) hash table with all predefined marshallers */
251 public Set
<string> predefined_marshal_set
;
252 /* (constant) hash table with all reserved identifiers in the generated code */
253 Set
<string> reserved_identifiers
;
255 public int next_temp_var_id
{
256 get { return emit_context
.next_temp_var_id
; }
257 set { emit_context
.next_temp_var_id
= value
; }
260 public int next_regex_id
= 0;
261 public bool in_creation_method
{ get { return current_method is CreationMethod
; } }
263 public bool current_method_inner_error
{
264 get { return emit_context
.current_method_inner_error
; }
265 set { emit_context
.current_method_inner_error
= value
; }
268 public bool current_method_return
{
269 get { return emit_context
.current_method_return
; }
270 set { emit_context
.current_method_return
= value
; }
273 int next_block_id
= 0;
274 Map
<Block
,int> block_map
= new HashMap
<Block
,int> ();
276 public DataType void_type
= new
VoidType ();
277 public DataType bool_type
;
278 public DataType char_type
;
279 public DataType uchar_type
;
280 public DataType? unichar_type
;
281 public DataType short_type
;
282 public DataType ushort_type
;
283 public DataType int_type
;
284 public DataType uint_type
;
285 public DataType long_type
;
286 public DataType ulong_type
;
287 public DataType int8_type
;
288 public DataType uint8_type
;
289 public DataType int16_type
;
290 public DataType uint16_type
;
291 public DataType int32_type
;
292 public DataType uint32_type
;
293 public DataType int64_type
;
294 public DataType uint64_type
;
295 public DataType string_type
;
296 public DataType regex_type
;
297 public DataType float_type
;
298 public DataType double_type
;
299 public TypeSymbol gtype_type
;
300 public TypeSymbol gobject_type
;
301 public ErrorType gerror_type
;
302 public Class glist_type
;
303 public Class gslist_type
;
304 public Class gnode_type
;
305 public Class gqueue_type
;
306 public Class gvaluearray_type
;
307 public TypeSymbol gstringbuilder_type
;
308 public TypeSymbol garray_type
;
309 public TypeSymbol gbytearray_type
;
310 public TypeSymbol gptrarray_type
;
311 public TypeSymbol gthreadpool_type
;
312 public DataType gdestroynotify_type
;
313 public DataType gquark_type
;
314 public Struct gvalue_type
;
315 public Class gvariant_type
;
316 public Struct mutex_type
;
317 public Struct gmutex_type
;
318 public Struct grecmutex_type
;
319 public Struct grwlock_type
;
320 public Struct gcond_type
;
321 public Class gsource_type
;
322 public TypeSymbol type_module_type
;
323 public TypeSymbol dbus_proxy_type
;
324 public Class gtk_widget_type
;
326 public bool in_plugin
= false;
327 public string module_init_param_name
;
329 public bool gvaluecollector_h_needed
;
330 public bool requires_assert
;
331 public bool requires_array_free
;
332 public bool requires_array_move
;
333 public bool requires_array_length
;
334 public bool requires_clear_mutex
;
336 public Set
<string> wrappers
;
337 Set
<Symbol
> generated_external_symbols
;
339 public Map
<string,string> variable_name_map
{ get { return emit_context
.variable_name_map
; } }
341 public static int ccode_attribute_cache_index
= CodeNode
.get_attribute_cache_index ();
343 public CCodeBaseModule () {
344 predefined_marshal_set
= new HashSet
<string> (str_hash
, str_equal
);
345 predefined_marshal_set
.add ("VOID:VOID");
346 predefined_marshal_set
.add ("VOID:BOOLEAN");
347 predefined_marshal_set
.add ("VOID:CHAR");
348 predefined_marshal_set
.add ("VOID:UCHAR");
349 predefined_marshal_set
.add ("VOID:INT");
350 predefined_marshal_set
.add ("VOID:UINT");
351 predefined_marshal_set
.add ("VOID:LONG");
352 predefined_marshal_set
.add ("VOID:ULONG");
353 predefined_marshal_set
.add ("VOID:ENUM");
354 predefined_marshal_set
.add ("VOID:FLAGS");
355 predefined_marshal_set
.add ("VOID:FLOAT");
356 predefined_marshal_set
.add ("VOID:DOUBLE");
357 predefined_marshal_set
.add ("VOID:STRING");
358 predefined_marshal_set
.add ("VOID:POINTER");
359 predefined_marshal_set
.add ("VOID:OBJECT");
360 predefined_marshal_set
.add ("STRING:OBJECT,POINTER");
361 predefined_marshal_set
.add ("VOID:UINT,POINTER");
362 predefined_marshal_set
.add ("BOOLEAN:FLAGS");
363 predefined_marshal_set
.add ("VOID:BOXED");
364 predefined_marshal_set
.add ("VOID:VARIANT");
365 predefined_marshal_set
.add ("BOOLEAN:BOXED,BOXED");
367 reserved_identifiers
= new HashSet
<string> (str_hash
, str_equal
);
370 reserved_identifiers
.add ("_Bool");
371 reserved_identifiers
.add ("_Complex");
372 reserved_identifiers
.add ("_Imaginary");
373 reserved_identifiers
.add ("asm");
374 reserved_identifiers
.add ("auto");
375 reserved_identifiers
.add ("break");
376 reserved_identifiers
.add ("case");
377 reserved_identifiers
.add ("char");
378 reserved_identifiers
.add ("const");
379 reserved_identifiers
.add ("continue");
380 reserved_identifiers
.add ("default");
381 reserved_identifiers
.add ("do");
382 reserved_identifiers
.add ("double");
383 reserved_identifiers
.add ("else");
384 reserved_identifiers
.add ("enum");
385 reserved_identifiers
.add ("extern");
386 reserved_identifiers
.add ("float");
387 reserved_identifiers
.add ("for");
388 reserved_identifiers
.add ("goto");
389 reserved_identifiers
.add ("if");
390 reserved_identifiers
.add ("inline");
391 reserved_identifiers
.add ("int");
392 reserved_identifiers
.add ("long");
393 reserved_identifiers
.add ("register");
394 reserved_identifiers
.add ("restrict");
395 reserved_identifiers
.add ("return");
396 reserved_identifiers
.add ("short");
397 reserved_identifiers
.add ("signed");
398 reserved_identifiers
.add ("sizeof");
399 reserved_identifiers
.add ("static");
400 reserved_identifiers
.add ("struct");
401 reserved_identifiers
.add ("switch");
402 reserved_identifiers
.add ("typedef");
403 reserved_identifiers
.add ("union");
404 reserved_identifiers
.add ("unsigned");
405 reserved_identifiers
.add ("void");
406 reserved_identifiers
.add ("volatile");
407 reserved_identifiers
.add ("while");
410 reserved_identifiers
.add ("_Alignas");
411 reserved_identifiers
.add ("_Alignof");
412 reserved_identifiers
.add ("_Atomic");
413 reserved_identifiers
.add ("_Generic");
414 reserved_identifiers
.add ("_Noreturn");
415 reserved_identifiers
.add ("_Static_assert");
416 reserved_identifiers
.add ("_Thread_local");
419 reserved_identifiers
.add ("cdecl");
421 // reserved for Vala/GObject naming conventions
422 reserved_identifiers
.add ("error");
423 reserved_identifiers
.add ("result");
424 reserved_identifiers
.add ("self");
427 public override void emit (CodeContext context
) {
428 this
.context
= context
;
430 root_symbol
= context
.root
;
432 bool_type
= new
BooleanType ((Struct
) root_symbol
.scope
.lookup ("bool"));
433 char_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("char"));
434 uchar_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uchar"));
435 short_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("short"));
436 ushort_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("ushort"));
437 int_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int"));
438 uint_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uint"));
439 long_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("long"));
440 ulong_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("ulong"));
441 int8_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int8"));
442 uint8_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uint8"));
443 int16_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int16"));
444 uint16_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uint16"));
445 int32_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int32"));
446 uint32_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uint32"));
447 int64_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("int64"));
448 uint64_type
= new
IntegerType ((Struct
) root_symbol
.scope
.lookup ("uint64"));
449 float_type
= new
FloatingType ((Struct
) root_symbol
.scope
.lookup ("float"));
450 double_type
= new
FloatingType ((Struct
) root_symbol
.scope
.lookup ("double"));
451 string_type
= new
ObjectType ((Class
) root_symbol
.scope
.lookup ("string"));
452 var unichar_struct
= (Struct
) root_symbol
.scope
.lookup ("unichar");
453 if (unichar_struct
!= null) {
454 unichar_type
= new
IntegerType (unichar_struct
);
457 var glib_ns
= root_symbol
.scope
.lookup ("GLib");
459 gtype_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("Type");
460 gobject_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("Object");
461 gerror_type
= new
ErrorType (null, null);
462 glist_type
= (Class
) glib_ns
.scope
.lookup ("List");
463 gslist_type
= (Class
) glib_ns
.scope
.lookup ("SList");
464 gnode_type
= (Class
) glib_ns
.scope
.lookup ("Node");
465 gqueue_type
= (Class
) glib_ns
.scope
.lookup ("Queue");
466 gvaluearray_type
= (Class
) glib_ns
.scope
.lookup ("ValueArray");
467 gstringbuilder_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("StringBuilder");
468 garray_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("Array");
469 gbytearray_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("ByteArray");
470 gptrarray_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("PtrArray");
471 gthreadpool_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("ThreadPool");
472 gdestroynotify_type
= new
DelegateType ((Delegate
) glib_ns
.scope
.lookup ("DestroyNotify"));
474 gquark_type
= new
IntegerType ((Struct
) glib_ns
.scope
.lookup ("Quark"));
475 gvalue_type
= (Struct
) glib_ns
.scope
.lookup ("Value");
476 gvariant_type
= (Class
) glib_ns
.scope
.lookup ("Variant");
477 gsource_type
= (Class
) glib_ns
.scope
.lookup ("Source");
479 gmutex_type
= (Struct
) glib_ns
.scope
.lookup ("Mutex");
480 grecmutex_type
= (Struct
) glib_ns
.scope
.lookup ("RecMutex");
481 grwlock_type
= (Struct
) glib_ns
.scope
.lookup ("RWLock");
482 gcond_type
= (Struct
) glib_ns
.scope
.lookup ("Cond");
484 mutex_type
= grecmutex_type
;
486 type_module_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("TypeModule");
488 regex_type
= new
ObjectType ((Class
) root_symbol
.scope
.lookup ("GLib").scope
.lookup ("Regex"));
490 if (context
.module_init_method
!= null) {
491 foreach (Parameter parameter
in context
.module_init_method
.get_parameters ()) {
492 if (parameter
.variable_type
.data_type
== type_module_type
) {
494 module_init_param_name
= parameter
.name
;
499 Report
.error (context
.module_init_method
.source_reference
, "[ModuleInit] requires a parameter of type `GLib.TypeModule'");
503 dbus_proxy_type
= (TypeSymbol
) glib_ns
.scope
.lookup ("DBusProxy");
505 var gtk_ns
= root_symbol
.scope
.lookup ("Gtk");
506 if (gtk_ns
!= null) {
507 gtk_widget_type
= (Class
) gtk_ns
.scope
.lookup ("Widget");
510 header_file
= new
CCodeFile ();
511 header_file
.is_header
= true;
512 internal_header_file
= new
CCodeFile ();
513 internal_header_file
.is_header
= true;
515 /* we're only interested in non-pkg source files */
516 var source_files
= context
.get_source_files ();
517 foreach (SourceFile file
in source_files
) {
518 if (file
.file_type
== SourceFileType
.SOURCE
||
519 (context
.header_filename
!= null && file
.file_type
== SourceFileType
.FAST
)) {
524 // generate symbols file for public API
525 if (context
.symbols_filename
!= null) {
526 var stream
= FileStream
.open (context
.symbols_filename
, "w");
527 if (stream
== null) {
528 Report
.error (null, "unable to open `%s' for writing".printf (context
.symbols_filename
));
533 foreach (string symbol
in header_file
.get_symbols ()) {
534 stream
.puts (symbol
);
541 // generate C header file for public API
542 if (context
.header_filename
!= null) {
543 bool ret
= header_file
.store (context
.header_filename
, null, context
.version_header
, false, "G_BEGIN_DECLS", "G_END_DECLS");
545 Report
.error (null, "unable to open `%s' for writing".printf (context
.header_filename
));
549 // generate C header file for internal API
550 if (context
.internal_header_filename
!= null) {
551 bool ret
= internal_header_file
.store (context
.internal_header_filename
, null, context
.version_header
, false, "G_BEGIN_DECLS", "G_END_DECLS");
553 Report
.error (null, "unable to open `%s' for writing".printf (context
.internal_header_filename
));
560 public void push_context (EmitContext emit_context
) {
561 if (this
.emit_context
!= null) {
562 emit_context_stack
.add (this
.emit_context
);
565 this
.emit_context
= emit_context
;
567 ccode
.current_line
= current_line
;
571 public void pop_context () {
572 if (emit_context_stack
.size
> 0) {
573 this
.emit_context
= emit_context_stack
.remove_at (emit_context_stack
.size
- 1);
575 ccode
.current_line
= current_line
;
578 this
.emit_context
= null;
582 public void push_line (SourceReference? source_reference
) {
583 line_directive_stack
.add (current_line
);
584 if (source_reference
!= null) {
585 current_line
= new
CCodeLineDirective (source_reference
.file
.filename
, source_reference
.begin
.line
);
587 ccode
.current_line
= current_line
;
592 public void pop_line () {
593 current_line
= line_directive_stack
.remove_at (line_directive_stack
.size
- 1);
595 ccode
.current_line
= current_line
;
599 public void push_function (CCodeFunction func
) {
600 emit_context
.ccode_stack
.add (ccode
);
601 emit_context
.ccode
= func
;
602 ccode
.current_line
= current_line
;
605 public void pop_function () {
606 emit_context
.ccode
= emit_context
.ccode_stack
.remove_at (emit_context
.ccode_stack
.size
- 1);
608 ccode
.current_line
= current_line
;
612 public bool add_symbol_declaration (CCodeFile decl_space
, Symbol sym
, string name
) {
613 if (decl_space
.add_declaration (name
)) {
616 if (sym
.source_reference
!= null) {
617 sym
.source_reference
.file
.used
= true;
619 if (sym
.external_package
|| (!decl_space
.is_header
&& CodeContext
.get ().use_header
&& !sym
.is_internal_symbol ())) {
620 // add appropriate include file
621 foreach (unowned
string header_filename
in get_ccode_header_filenames (sym
).split (",")) {
622 decl_space
.add_include (header_filename
, !sym
.external_package
||
623 (sym
.external_package
&&
624 sym
.from_commandline
));
626 // declaration complete
629 // require declaration
634 public CCodeIdentifier
get_value_setter_function (DataType type_reference
) {
635 var array_type
= type_reference as ArrayType
;
636 if (type_reference
.data_type
!= null) {
637 return new
CCodeIdentifier (get_ccode_set_value_function (type_reference
.data_type
));
638 } else if (array_type
!= null && array_type
.element_type
.data_type
== string_type
.data_type
) {
640 return new
CCodeIdentifier ("g_value_set_boxed");
642 return new
CCodeIdentifier ("g_value_set_pointer");
646 public CCodeIdentifier
get_value_taker_function (DataType type_reference
) {
647 var array_type
= type_reference as ArrayType
;
648 if (type_reference
.data_type
!= null) {
649 return new
CCodeIdentifier (get_ccode_take_value_function (type_reference
.data_type
));
650 } else if (array_type
!= null && array_type
.element_type
.data_type
== string_type
.data_type
) {
652 return new
CCodeIdentifier ("g_value_take_boxed");
654 return new
CCodeIdentifier ("g_value_set_pointer");
658 CCodeIdentifier
get_value_getter_function (DataType type_reference
) {
659 var array_type
= type_reference as ArrayType
;
660 if (type_reference
.data_type
!= null) {
661 return new
CCodeIdentifier (get_ccode_get_value_function (type_reference
.data_type
));
662 } else if (array_type
!= null && array_type
.element_type
.data_type
== string_type
.data_type
) {
664 return new
CCodeIdentifier ("g_value_get_boxed");
666 return new
CCodeIdentifier ("g_value_get_pointer");
670 public virtual void append_vala_array_free () {
673 public virtual void append_vala_array_move () {
676 public virtual void append_vala_array_length () {
679 public void append_vala_clear_mutex (string typename
, string funcprefix
) {
681 cfile
.add_include ("string.h");
683 var fun
= new
CCodeFunction ("_vala_clear_" + typename
);
684 fun
.modifiers
= CCodeModifiers
.STATIC
;
685 fun
.add_parameter (new
CCodeParameter ("mutex", typename
+ " *"));
689 ccode
.add_declaration (typename
, new CCodeVariableDeclarator
.zero ("zero_mutex", new
CCodeConstant ("{ 0 }")));
691 var cmp
= new
CCodeFunctionCall (new
CCodeIdentifier ("memcmp"));
692 cmp
.add_argument (new
CCodeIdentifier ("mutex"));
693 cmp
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new
CCodeIdentifier ("zero_mutex")));
694 cmp
.add_argument (new
CCodeIdentifier ("sizeof (" + typename
+ ")"));
697 var mutex_clear
= new
CCodeFunctionCall (new
CCodeIdentifier (funcprefix
+ "_clear"));
698 mutex_clear
.add_argument (new
CCodeIdentifier ("mutex"));
699 ccode
.add_expression (mutex_clear
);
701 var mset
= new
CCodeFunctionCall (new
CCodeIdentifier ("memset"));
702 mset
.add_argument (new
CCodeIdentifier ("mutex"));
703 mset
.add_argument (new
CCodeConstant ("0"));
704 mset
.add_argument (new
CCodeIdentifier ("sizeof (" + typename
+ ")"));
705 ccode
.add_expression (mset
);
711 cfile
.add_function_declaration (fun
);
712 cfile
.add_function (fun
);
715 public override void visit_source_file (SourceFile source_file
) {
716 cfile
= new
CCodeFile ();
718 user_marshal_set
= new HashSet
<string> (str_hash
, str_equal
);
722 gvaluecollector_h_needed
= false;
723 requires_assert
= false;
724 requires_array_free
= false;
725 requires_array_move
= false;
726 requires_array_length
= false;
727 requires_clear_mutex
= false;
729 wrappers
= new HashSet
<string> (str_hash
, str_equal
);
730 generated_external_symbols
= new HashSet
<Symbol
> ();
732 header_file
.add_include ("glib.h");
733 internal_header_file
.add_include ("glib.h");
734 cfile
.add_include ("glib.h");
735 cfile
.add_include ("glib-object.h");
737 source_file
.accept_children (this
);
739 if (context
.report
.get_errors () > 0) {
743 /* For fast-vapi, we only wanted the header declarations
744 * to be emitted, so bail out here without writing the
747 if (source_file
.file_type
== SourceFileType
.FAST
) {
751 if (requires_assert
) {
752 cfile
.add_type_declaration (new CCodeMacroReplacement
.with_expression ("_vala_assert(expr, msg)", new
CCodeConstant ("if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);")));
753 cfile
.add_type_declaration (new CCodeMacroReplacement
.with_expression ("_vala_return_if_fail(expr, msg)", new
CCodeConstant ("if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }")));
754 cfile
.add_type_declaration (new CCodeMacroReplacement
.with_expression ("_vala_return_val_if_fail(expr, msg, val)", new
CCodeConstant ("if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }")));
755 cfile
.add_type_declaration (new CCodeMacroReplacement
.with_expression ("_vala_warn_if_fail(expr, msg)", new
CCodeConstant ("if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);")));
757 if (requires_array_free
) {
758 append_vala_array_free ();
760 if (requires_array_move
) {
761 append_vala_array_move ();
763 if (requires_array_length
) {
764 append_vala_array_length ();
766 if (requires_clear_mutex
) {
767 append_vala_clear_mutex ("GMutex", "g_mutex");
768 append_vala_clear_mutex ("GRecMutex", "g_rec_mutex");
769 append_vala_clear_mutex ("GRWLock", "g_rw_lock");
770 append_vala_clear_mutex ("GCond", "g_cond");
773 if (gvaluecollector_h_needed
) {
774 cfile
.add_include ("gobject/gvaluecollector.h");
777 var comments
= source_file
.get_comments();
778 if (comments
!= null) {
779 foreach (Comment comment
in comments
) {
780 var ccomment
= new
CCodeComment (comment
.content
);
781 cfile
.add_comment (ccomment
);
785 if (!cfile
.store (source_file
.get_csource_filename (), source_file
.filename
, context
.version_header
, context
.debug
)) {
786 Report
.error (null, "unable to open `%s' for writing".printf (source_file
.get_csource_filename ()));
792 public virtual bool generate_enum_declaration (Enum en
, CCodeFile decl_space
) {
793 if (add_symbol_declaration (decl_space
, en
, get_ccode_name (en
))) {
797 var cenum
= new
CCodeEnum (get_ccode_name (en
));
799 cenum
.modifiers
|= (en
.version
.deprecated ? CCodeModifiers
.DEPRECATED
: 0);
802 foreach (EnumValue ev
in en
.get_values ()) {
804 if (ev
.value
== null) {
805 c_ev
= new
CCodeEnumValue (get_ccode_name (ev
));
807 c_ev
.value
= new
CCodeConstant ("1 << %d".printf (flag_shift
));
811 ev
.value
.emit (this
);
812 c_ev
= new
CCodeEnumValue (get_ccode_name (ev
), get_cvalue (ev
.value
));
814 c_ev
.modifiers
|= (ev
.version
.deprecated ? CCodeModifiers
.DEPRECATED
: 0);
815 cenum
.add_value (c_ev
);
818 decl_space
.add_type_definition (cenum
);
819 decl_space
.add_type_definition (new
CCodeNewline ());
821 if (!get_ccode_has_type_id (en
)) {
825 decl_space
.add_include ("glib-object.h");
826 decl_space
.add_type_declaration (new
CCodeNewline ());
828 var macro
= "(%s_get_type ())".printf (get_ccode_lower_case_name (en
, null));
829 decl_space
.add_type_declaration (new
CCodeMacroReplacement (get_ccode_type_id (en
), macro
));
831 var fun_name
= "%s_get_type".printf (get_ccode_lower_case_name (en
, null));
832 var regfun
= new
CCodeFunction (fun_name
, "GType");
833 regfun
.modifiers
= CCodeModifiers
.CONST
;
835 if (en
.is_private_symbol ()) {
836 // avoid C warning as this function is not always used
837 regfun
.modifiers
|= CCodeModifiers
.STATIC
| CCodeModifiers
.UNUSED
;
838 } else if (context
.hide_internal
&& en
.is_internal_symbol ()) {
839 regfun
.modifiers
|= CCodeModifiers
.INTERNAL
;
842 decl_space
.add_function_declaration (regfun
);
847 public override void visit_enum (Enum en
) {
848 push_line (en
.source_reference
);
850 en
.accept_children (this
);
852 if (en
.comment
!= null) {
853 cfile
.add_type_member_definition (new
CCodeComment (en
.comment
.content
));
856 generate_enum_declaration (en
, cfile
);
858 if (!en
.is_internal_symbol ()) {
859 generate_enum_declaration (en
, header_file
);
861 if (!en
.is_private_symbol ()) {
862 generate_enum_declaration (en
, internal_header_file
);
868 public void visit_member (Symbol m
) {
869 /* stuff meant for all lockable members */
870 if (m is Lockable
&& ((Lockable
) m
).get_lock_used ()) {
871 CCodeExpression l
= new
CCodeIdentifier ("self");
872 var init_context
= class_init_context
;
873 var finalize_context
= class_finalize_context
;
875 if (m
.is_instance_member ()) {
876 l
= new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (l
, "priv"), get_symbol_lock_name (m
.name
));
877 init_context
= instance_init_context
;
878 finalize_context
= instance_finalize_context
;
879 } else if (m
.is_class_member ()) {
880 TypeSymbol parent
= (TypeSymbol
)m
.parent_symbol
;
882 var get_class_private_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf(get_ccode_upper_case_name (parent
))));
883 get_class_private_call
.add_argument (new
CCodeIdentifier ("klass"));
884 l
= new CCodeMemberAccess
.pointer (get_class_private_call
, get_symbol_lock_name (m
.name
));
886 l
= new
CCodeIdentifier (get_symbol_lock_name ("%s_%s".printf(get_ccode_lower_case_name (m
.parent_symbol
), m
.name
)));
889 push_context (init_context
);
890 var initf
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_name (mutex_type
.default_construction_method
)));
891 initf
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, l
));
892 ccode
.add_expression (initf
);
895 if (finalize_context
!= null) {
896 push_context (finalize_context
);
897 var fc
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_rec_mutex_clear"));
898 fc
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, l
));
899 ccode
.add_expression (fc
);
905 public void generate_constant_declaration (Constant c
, CCodeFile decl_space
, bool definition
= false) {
906 if (c
.parent_symbol is Block
) {
911 if (add_symbol_declaration (decl_space
, c
, get_ccode_name (c
))) {
916 generate_type_declaration (c
.type_reference
, decl_space
);
920 var initializer_list
= c
.value as InitializerList
;
921 if (initializer_list
!= null) {
922 var cdecl
= new
CCodeDeclaration (get_ccode_const_name (c
.type_reference
));
924 if (c
.type_reference is ArrayType
) {
925 arr
= "[%d]".printf (initializer_list
.size
);
928 var cinitializer
= get_cvalue (c
.value
);
930 // never output value in header
931 // special case needed as this method combines declaration and definition
935 cdecl
.add_declarator (new
CCodeVariableDeclarator ("%s%s".printf (get_ccode_name (c
), arr
), cinitializer
));
936 if (c
.is_private_symbol ()) {
937 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
939 cdecl
.modifiers
= CCodeModifiers
.EXTERN
;
942 decl_space
.add_constant_declaration (cdecl
);
944 var cdefine
= new CCodeMacroReplacement
.with_expression (get_ccode_name (c
), get_cvalue (c
.value
));
945 decl_space
.add_type_member_declaration (cdefine
);
950 public override void visit_constant (Constant c
) {
951 push_line (c
.source_reference
);
953 if (c
.parent_symbol is Block
) {
956 generate_type_declaration (c
.type_reference
, cfile
);
960 string type_name
= get_ccode_const_name (c
.type_reference
);
962 if (c
.type_reference is ArrayType
) {
966 if (c
.type_reference
.compatible (string_type
)) {
967 type_name
= "const char";
971 var cinitializer
= get_cvalue (c
.value
);
973 ccode
.add_declaration (type_name
, new
CCodeVariableDeclarator ("%s%s".printf (get_ccode_name (c
), arr
), cinitializer
), CCodeModifiers
.STATIC
);
975 generate_constant_declaration (c
, cfile
, true);
977 if (!c
.is_internal_symbol ()) {
978 generate_constant_declaration (c
, header_file
);
980 if (!c
.is_private_symbol ()) {
981 generate_constant_declaration (c
, internal_header_file
);
988 public void generate_field_declaration (Field f
, CCodeFile decl_space
) {
989 if (add_symbol_declaration (decl_space
, f
, get_ccode_name (f
))) {
993 generate_type_declaration (f
.variable_type
, decl_space
);
995 var cdecl
= new
CCodeDeclaration (get_ccode_name (f
.variable_type
));
996 cdecl
.add_declarator (new
CCodeVariableDeclarator (get_ccode_name (f
), null, get_ccode_declarator_suffix (f
.variable_type
)));
997 if (f
.is_private_symbol ()) {
998 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
1000 cdecl
.modifiers
= CCodeModifiers
.EXTERN
;
1002 if (f
.version
.deprecated
) {
1003 cdecl
.modifiers
|= CCodeModifiers
.DEPRECATED
;
1005 if (f
.is_volatile
) {
1006 cdecl
.modifiers
|= CCodeModifiers
.VOLATILE
;
1008 decl_space
.add_type_member_declaration (cdecl
);
1010 if (f
.get_lock_used ()) {
1011 // Declare mutex for static member
1012 var flock
= new
CCodeDeclaration (get_ccode_name (mutex_type
));
1013 var flock_decl
= new
CCodeVariableDeclarator (get_symbol_lock_name (get_ccode_name (f
)), new
CCodeConstant ("{0}"));
1014 flock
.add_declarator (flock_decl
);
1016 if (f
.is_private_symbol ()) {
1017 flock
.modifiers
= CCodeModifiers
.STATIC
;
1018 } else if (context
.hide_internal
&& f
.is_internal_symbol ()) {
1019 flock
.modifiers
= CCodeModifiers
.INTERNAL
;
1021 flock
.modifiers
= CCodeModifiers
.EXTERN
;
1023 decl_space
.add_type_member_declaration (flock
);
1026 if (f
.variable_type is ArrayType
&& get_ccode_array_length (f
)) {
1027 var array_type
= (ArrayType
) f
.variable_type
;
1029 if (!array_type
.fixed_length
) {
1030 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1031 var len_type
= int_type
.copy ();
1033 cdecl
= new
CCodeDeclaration (get_ccode_name (len_type
));
1034 cdecl
.add_declarator (new
CCodeVariableDeclarator (get_array_length_cname (get_ccode_name (f
), dim
)));
1035 if (f
.is_private_symbol ()) {
1036 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
1037 } else if (context
.hide_internal
&& f
.is_internal_symbol ()) {
1038 cdecl
.modifiers
= CCodeModifiers
.INTERNAL
;
1040 cdecl
.modifiers
= CCodeModifiers
.EXTERN
;
1042 decl_space
.add_type_member_declaration (cdecl
);
1045 } else if (f
.variable_type is DelegateType
) {
1046 var delegate_type
= (DelegateType
) f
.variable_type
;
1047 if (delegate_type
.delegate_symbol
.has_target
) {
1048 // create field to store delegate target
1050 cdecl
= new
CCodeDeclaration ("gpointer");
1051 cdecl
.add_declarator (new
CCodeVariableDeclarator (get_ccode_delegate_target_name (f
)));
1052 if (f
.is_private_symbol ()) {
1053 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
1054 } else if (context
.hide_internal
&& f
.is_internal_symbol ()) {
1055 cdecl
.modifiers
= CCodeModifiers
.INTERNAL
;
1057 cdecl
.modifiers
= CCodeModifiers
.EXTERN
;
1059 decl_space
.add_type_member_declaration (cdecl
);
1061 if (delegate_type
.value_owned
&& !delegate_type
.is_called_once
) {
1062 cdecl
= new
CCodeDeclaration ("GDestroyNotify");
1063 cdecl
.add_declarator (new
CCodeVariableDeclarator (get_delegate_target_destroy_notify_cname (get_ccode_name (f
))));
1064 if (f
.is_private_symbol ()) {
1065 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
1066 } else if (context
.hide_internal
&& f
.is_internal_symbol ()) {
1067 cdecl
.modifiers
= CCodeModifiers
.INTERNAL
;
1069 cdecl
.modifiers
= CCodeModifiers
.EXTERN
;
1071 decl_space
.add_type_member_declaration (cdecl
);
1077 public override void visit_field (Field f
) {
1078 push_line (f
.source_reference
);
1081 check_type (f
.variable_type
);
1083 var cl
= f
.parent_symbol as Class
;
1084 bool is_gtypeinstance
= (cl
!= null && !cl
.is_compact
);
1086 CCodeExpression lhs
= null;
1088 if (f
.binding
== MemberBinding
.INSTANCE
) {
1089 if (is_gtypeinstance
&& f
.access
== SymbolAccessibility
.PRIVATE
) {
1090 lhs
= new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("self"), "priv"), get_ccode_name (f
));
1092 lhs
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("self"), get_ccode_name (f
));
1095 if (f
.initializer
!= null) {
1096 push_context (instance_init_context
);
1098 f
.initializer
.emit (this
);
1100 var rhs
= get_cvalue (f
.initializer
);
1101 if (!is_simple_struct_creation (f
, f
.initializer
)) {
1102 // otherwise handled in visit_object_creation_expression
1104 ccode
.add_assignment (lhs
, rhs
);
1106 if (f
.variable_type is ArrayType
&& get_ccode_array_length (f
)) {
1107 var array_type
= (ArrayType
) f
.variable_type
;
1108 var field_value
= get_field_cvalue (f
, load_this_parameter ((TypeSymbol
) f
.parent_symbol
));
1110 var glib_value
= (GLibValue
) f
.initializer
.target_value
;
1111 if (glib_value
.array_length_cvalues
!= null) {
1112 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1113 var array_len_lhs
= get_array_length_cvalue (field_value
, dim
);
1114 ccode
.add_assignment (array_len_lhs
, get_array_length_cvalue (glib_value
, dim
));
1116 } else if (glib_value
.array_null_terminated
) {
1117 requires_array_length
= true;
1118 var len_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("_vala_array_length"));
1119 len_call
.add_argument (get_cvalue_ (glib_value
));
1121 ccode
.add_assignment (get_array_length_cvalue (field_value
, 1), len_call
);
1123 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1124 ccode
.add_assignment (get_array_length_cvalue (field_value
, dim
), new
CCodeConstant ("-1"));
1128 if (array_type
.rank
== 1 && f
.is_internal_symbol ()) {
1129 var lhs_array_size
= get_array_size_cvalue (field_value
);
1130 var rhs_array_len
= get_array_length_cvalue (field_value
, 1);
1131 ccode
.add_assignment (lhs_array_size
, rhs_array_len
);
1133 } else if (f
.variable_type is DelegateType
) {
1134 var delegate_type
= (DelegateType
) f
.variable_type
;
1135 if (delegate_type
.delegate_symbol
.has_target
) {
1136 var field_value
= get_field_cvalue (f
, load_this_parameter ((TypeSymbol
) f
.parent_symbol
));
1138 ccode
.add_assignment (get_delegate_target_cvalue (field_value
), new
CCodeIdentifier ("self"));
1139 if (delegate_type
.is_disposable ()) {
1140 ccode
.add_assignment (get_delegate_target_destroy_notify_cvalue (field_value
), new
CCodeIdentifier ("NULL"));
1146 foreach (var value
in temp_ref_values
) {
1147 ccode
.add_expression (destroy_value (value
));
1150 temp_ref_values
.clear ();
1155 if (requires_destroy (f
.variable_type
) && instance_finalize_context
!= null) {
1156 push_context (instance_finalize_context
);
1157 ccode
.add_expression (destroy_field (f
, load_this_parameter ((TypeSymbol
) f
.parent_symbol
)));
1160 } else if (f
.binding
== MemberBinding
.CLASS
) {
1161 if (!is_gtypeinstance
) {
1162 Report
.error (f
.source_reference
, "class fields are not supported in compact classes");
1167 if (f
.access
== SymbolAccessibility
.PRIVATE
) {
1168 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf (get_ccode_upper_case_name (cl
))));
1169 ccall
.add_argument (new
CCodeIdentifier ("klass"));
1170 lhs
= new
CCodeMemberAccess (ccall
, get_ccode_name (f
), true);
1172 lhs
= new
CCodeMemberAccess (new
CCodeIdentifier ("klass"), get_ccode_name (f
), true);
1175 if (f
.initializer
!= null) {
1176 push_context (class_init_context
);
1178 f
.initializer
.emit (this
);
1180 var rhs
= get_cvalue (f
.initializer
);
1182 ccode
.add_assignment (lhs
, rhs
);
1184 foreach (var value
in temp_ref_values
) {
1185 ccode
.add_expression (destroy_value (value
));
1188 temp_ref_values
.clear ();
1193 generate_field_declaration (f
, cfile
);
1195 if (!f
.is_internal_symbol ()) {
1196 generate_field_declaration (f
, header_file
);
1198 if (!f
.is_private_symbol ()) {
1199 generate_field_declaration (f
, internal_header_file
);
1203 lhs
= new
CCodeIdentifier (get_ccode_name (f
));
1205 var var_decl
= new
CCodeVariableDeclarator (get_ccode_name (f
), null, get_ccode_declarator_suffix (f
.variable_type
));
1206 var_decl
.initializer
= default_value_for_type (f
.variable_type
, true);
1208 if (class_init_context
!= null) {
1209 push_context (class_init_context
);
1211 push_context (new
EmitContext ());
1214 if (f
.initializer
!= null) {
1215 f
.initializer
.emit (this
);
1217 var init
= get_cvalue (f
.initializer
);
1218 if (is_constant_ccode_expression (init
)) {
1219 var_decl
.initializer
= init
;
1223 var var_def
= new
CCodeDeclaration (get_ccode_name (f
.variable_type
));
1224 var_def
.add_declarator (var_decl
);
1225 if (!f
.is_private_symbol ()) {
1226 var_def
.modifiers
= CCodeModifiers
.EXTERN
;
1228 var_def
.modifiers
= CCodeModifiers
.STATIC
;
1230 if (f
.version
.deprecated
) {
1231 var_def
.modifiers
|= CCodeModifiers
.DEPRECATED
;
1233 if (f
.is_volatile
) {
1234 var_def
.modifiers
|= CCodeModifiers
.VOLATILE
;
1236 cfile
.add_type_member_declaration (var_def
);
1238 /* add array length fields where necessary */
1239 if (f
.variable_type is ArrayType
&& get_ccode_array_length (f
)) {
1240 var array_type
= (ArrayType
) f
.variable_type
;
1242 if (!array_type
.fixed_length
) {
1243 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1244 var len_type
= int_type
.copy ();
1246 var len_def
= new
CCodeDeclaration (get_ccode_name (len_type
));
1247 len_def
.add_declarator (new
CCodeVariableDeclarator (get_array_length_cname (get_ccode_name (f
), dim
), new
CCodeConstant ("0")));
1248 if (!f
.is_private_symbol ()) {
1249 len_def
.modifiers
= CCodeModifiers
.EXTERN
;
1251 len_def
.modifiers
= CCodeModifiers
.STATIC
;
1253 cfile
.add_type_member_declaration (len_def
);
1256 if (array_type
.rank
== 1 && f
.is_internal_symbol ()) {
1257 var len_type
= int_type
.copy ();
1259 var cdecl
= new
CCodeDeclaration (get_ccode_name (len_type
));
1260 cdecl
.add_declarator (new
CCodeVariableDeclarator (get_array_size_cname (get_ccode_name (f
)), new
CCodeConstant ("0")));
1261 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
1262 cfile
.add_type_member_declaration (cdecl
);
1265 } else if (f
.variable_type is DelegateType
) {
1266 var delegate_type
= (DelegateType
) f
.variable_type
;
1267 if (delegate_type
.delegate_symbol
.has_target
) {
1268 // create field to store delegate target
1270 var target_def
= new
CCodeDeclaration ("gpointer");
1271 target_def
.add_declarator (new
CCodeVariableDeclarator (get_ccode_delegate_target_name (f
), new
CCodeConstant ("NULL")));
1272 if (!f
.is_private_symbol ()) {
1273 target_def
.modifiers
= CCodeModifiers
.EXTERN
;
1275 target_def
.modifiers
= CCodeModifiers
.STATIC
;
1277 cfile
.add_type_member_declaration (target_def
);
1279 if (delegate_type
.is_disposable ()) {
1280 var target_destroy_notify_def
= new
CCodeDeclaration ("GDestroyNotify");
1281 target_destroy_notify_def
.add_declarator (new
CCodeVariableDeclarator (get_delegate_target_destroy_notify_cname (get_ccode_name (f
)), new
CCodeConstant ("NULL")));
1282 if (!f
.is_private_symbol ()) {
1283 target_destroy_notify_def
.modifiers
= CCodeModifiers
.EXTERN
;
1285 target_destroy_notify_def
.modifiers
= CCodeModifiers
.STATIC
;
1287 cfile
.add_type_member_declaration (target_destroy_notify_def
);
1293 if (f
.initializer
!= null) {
1294 var rhs
= get_cvalue (f
.initializer
);
1295 if (!is_constant_ccode_expression (rhs
)) {
1296 if (is_gtypeinstance
) {
1297 if (f
.initializer is InitializerList
) {
1298 ccode
.open_block ();
1300 var temp_decl
= get_temp_variable (f
.variable_type
);
1301 var vardecl
= new CCodeVariableDeclarator
.zero (temp_decl
.name
, rhs
);
1302 ccode
.add_declaration (get_ccode_name (temp_decl
.variable_type
), vardecl
);
1304 var tmp
= get_variable_cexpression (get_variable_cname (temp_decl
.name
));
1305 ccode
.add_assignment (lhs
, tmp
);
1309 ccode
.add_assignment (lhs
, rhs
);
1312 if (f
.variable_type is ArrayType
&& get_ccode_array_length (f
)) {
1313 var array_type
= (ArrayType
) f
.variable_type
;
1314 var field_value
= get_field_cvalue (f
, null);
1316 var glib_value
= (GLibValue
) f
.initializer
.target_value
;
1317 if (glib_value
.array_length_cvalues
!= null) {
1318 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1319 var array_len_lhs
= get_array_length_cvalue (field_value
, dim
);
1320 ccode
.add_assignment (array_len_lhs
, get_array_length_cvalue (glib_value
, dim
));
1322 } else if (glib_value
.array_null_terminated
) {
1323 requires_array_length
= true;
1324 var len_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("_vala_array_length"));
1325 len_call
.add_argument (get_cvalue_ (glib_value
));
1327 ccode
.add_assignment (get_array_length_cvalue (field_value
, 1), len_call
);
1329 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1330 ccode
.add_assignment (get_array_length_cvalue (field_value
, dim
), new
CCodeConstant ("-1"));
1336 Report
.error (f
.source_reference
, "Non-constant field initializers not supported in this context");
1349 public bool is_constant_ccode_expression (CCodeExpression cexpr
) {
1350 if (cexpr is CCodeConstant
) {
1352 } else if (cexpr is CCodeCastExpression
) {
1353 var ccast
= (CCodeCastExpression
) cexpr
;
1354 return is_constant_ccode_expression (ccast
.inner
);
1355 } else if (cexpr is CCodeUnaryExpression
) {
1356 var cunary
= (CCodeUnaryExpression
) cexpr
;
1357 switch (cunary
.operator
) {
1358 case CCodeUnaryOperator
.PREFIX_INCREMENT
:
1359 case CCodeUnaryOperator
.PREFIX_DECREMENT
:
1360 case CCodeUnaryOperator
.POSTFIX_INCREMENT
:
1361 case CCodeUnaryOperator
.POSTFIX_DECREMENT
:
1364 return is_constant_ccode_expression (cunary
.inner
);
1365 } else if (cexpr is CCodeBinaryExpression
) {
1366 var cbinary
= (CCodeBinaryExpression
) cexpr
;
1367 return is_constant_ccode_expression (cbinary
.left
) && is_constant_ccode_expression (cbinary
.right
);
1370 var cparenthesized
= (cexpr as CCodeParenthesizedExpression
);
1371 return (null != cparenthesized
&& is_constant_ccode_expression (cparenthesized
.inner
));
1375 * Returns whether the passed cexpr is a pure expression, i.e. an
1376 * expression without side-effects.
1378 public bool is_pure_ccode_expression (CCodeExpression cexpr
) {
1379 if (cexpr is CCodeConstant
|| cexpr is CCodeIdentifier
) {
1381 } else if (cexpr is CCodeBinaryExpression
) {
1382 var cbinary
= (CCodeBinaryExpression
) cexpr
;
1383 return is_pure_ccode_expression (cbinary
.left
) && is_constant_ccode_expression (cbinary
.right
);
1384 } else if (cexpr is CCodeUnaryExpression
) {
1385 var cunary
= (CCodeUnaryExpression
) cexpr
;
1386 switch (cunary
.operator
) {
1387 case CCodeUnaryOperator
.PREFIX_INCREMENT
:
1388 case CCodeUnaryOperator
.PREFIX_DECREMENT
:
1389 case CCodeUnaryOperator
.POSTFIX_INCREMENT
:
1390 case CCodeUnaryOperator
.POSTFIX_DECREMENT
:
1393 return is_pure_ccode_expression (cunary
.inner
);
1395 } else if (cexpr is CCodeMemberAccess
) {
1396 var cma
= (CCodeMemberAccess
) cexpr
;
1397 return is_pure_ccode_expression (cma
.inner
);
1398 } else if (cexpr is CCodeElementAccess
) {
1399 var cea
= (CCodeElementAccess
) cexpr
;
1400 return is_pure_ccode_expression (cea
.container
) && is_pure_ccode_expression (cea
.index
);
1401 } else if (cexpr is CCodeCastExpression
) {
1402 var ccast
= (CCodeCastExpression
) cexpr
;
1403 return is_pure_ccode_expression (ccast
.inner
);
1404 } else if (cexpr is CCodeParenthesizedExpression
) {
1405 var cparenthesized
= (CCodeParenthesizedExpression
) cexpr
;
1406 return is_pure_ccode_expression (cparenthesized
.inner
);
1412 public override void visit_formal_parameter (Parameter p
) {
1414 check_type (p
.variable_type
);
1418 public override void visit_property (Property prop
) {
1419 visit_member (prop
);
1421 check_type (prop
.property_type
);
1423 if (prop
.get_accessor
!= null) {
1424 prop
.get_accessor
.accept (this
);
1426 if (prop
.set_accessor
!= null) {
1427 prop
.set_accessor
.accept (this
);
1431 public void generate_type_declaration (DataType type
, CCodeFile decl_space
) {
1432 if (type is ObjectType
) {
1433 var object_type
= (ObjectType
) type
;
1434 if (object_type
.type_symbol is Class
) {
1435 generate_class_declaration ((Class
) object_type
.type_symbol
, decl_space
);
1436 } else if (object_type
.type_symbol is Interface
) {
1437 generate_interface_declaration ((Interface
) object_type
.type_symbol
, decl_space
);
1439 } else if (type is DelegateType
) {
1440 var deleg_type
= (DelegateType
) type
;
1441 var d
= deleg_type
.delegate_symbol
;
1442 generate_delegate_declaration (d
, decl_space
);
1443 } else if (type
.data_type is Enum
) {
1444 var en
= (Enum
) type
.data_type
;
1445 generate_enum_declaration (en
, decl_space
);
1446 } else if (type is ValueType
) {
1447 var value_type
= (ValueType
) type
;
1448 generate_struct_declaration ((Struct
) value_type
.type_symbol
, decl_space
);
1449 } else if (type is ArrayType
) {
1450 var array_type
= (ArrayType
) type
;
1451 generate_type_declaration (array_type
.element_type
, decl_space
);
1452 } else if (type is ErrorType
) {
1453 var error_type
= (ErrorType
) type
;
1454 if (error_type
.error_domain
!= null) {
1455 generate_error_domain_declaration (error_type
.error_domain
, decl_space
);
1457 } else if (type is PointerType
) {
1458 var pointer_type
= (PointerType
) type
;
1459 generate_type_declaration (pointer_type
.base_type
, decl_space
);
1462 foreach (DataType type_arg
in type
.get_type_arguments ()) {
1463 generate_type_declaration (type_arg
, decl_space
);
1467 public virtual void generate_class_struct_declaration (Class cl
, CCodeFile decl_space
) {
1470 public virtual void generate_struct_declaration (Struct st
, CCodeFile decl_space
) {
1473 public virtual void generate_delegate_declaration (Delegate d
, CCodeFile decl_space
) {
1476 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) {
1479 public void generate_property_accessor_declaration (PropertyAccessor acc
, CCodeFile decl_space
) {
1480 if (add_symbol_declaration (decl_space
, acc
, get_ccode_name (acc
))) {
1484 var prop
= (Property
) acc
.prop
;
1486 bool returns_real_struct
= acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ();
1489 CCodeParameter cvalueparam
;
1490 if (returns_real_struct
) {
1491 cvalueparam
= new
CCodeParameter ("result", "%s *".printf (get_ccode_name (acc
.value_type
)));
1492 } else if (!acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ()) {
1493 cvalueparam
= new
CCodeParameter ("value", "%s *".printf (get_ccode_name (acc
.value_type
)));
1495 cvalueparam
= new
CCodeParameter ("value", get_ccode_name (acc
.value_type
));
1497 generate_type_declaration (acc
.value_type
, decl_space
);
1499 CCodeFunction function
;
1500 if (acc
.readable
&& !returns_real_struct
) {
1501 function
= new
CCodeFunction (get_ccode_name (acc
), get_ccode_name (acc
.value_type
));
1503 function
= new
CCodeFunction (get_ccode_name (acc
), "void");
1506 if (prop
.binding
== MemberBinding
.INSTANCE
) {
1507 var t
= (TypeSymbol
) prop
.parent_symbol
;
1508 var this_type
= get_data_type_for_symbol (t
);
1509 generate_type_declaration (this_type
, decl_space
);
1510 var cselfparam
= new
CCodeParameter ("self", get_ccode_name (this_type
));
1511 if (t is Struct
&& !((Struct
) t
).is_simple_type ()) {
1512 cselfparam
.type_name
+= "*";
1515 function
.add_parameter (cselfparam
);
1518 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1519 function
.add_parameter (cvalueparam
);
1522 if (acc
.value_type is ArrayType
) {
1523 var array_type
= (ArrayType
) acc
.value_type
;
1524 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1525 function
.add_parameter (new
CCodeParameter (get_array_length_cname (acc
.readable ?
"result" : "value", dim
), acc
.readable ?
"int*" : "int"));
1527 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1528 function
.add_parameter (new
CCodeParameter (get_delegate_target_cname (acc
.readable ?
"result" : "value"), acc
.readable ?
"gpointer*" : "gpointer"));
1529 if (!acc
.readable
&& acc
.value_type
.value_owned
) {
1530 function
.add_parameter (new
CCodeParameter (get_delegate_target_destroy_notify_cname ("value"), "GDestroyNotify"));
1534 if (prop
.version
.deprecated
) {
1535 function
.modifiers
|= CCodeModifiers
.DEPRECATED
;
1538 if (!prop
.is_abstract
1539 && (prop
.is_private_symbol () || (!acc
.readable
&& !acc
.writable
) || acc
.access
== SymbolAccessibility
.PRIVATE
)) {
1540 function
.modifiers
|= CCodeModifiers
.STATIC
;
1541 } else if (context
.hide_internal
&& (prop
.is_internal_symbol () || acc
.access
== SymbolAccessibility
.INTERNAL
)) {
1542 function
.modifiers
|= CCodeModifiers
.INTERNAL
;
1544 decl_space
.add_function_declaration (function
);
1547 public override void visit_property_accessor (PropertyAccessor acc
) {
1548 push_context (new
EmitContext (acc
));
1549 push_line (acc
.source_reference
);
1551 var prop
= (Property
) acc
.prop
;
1553 if (acc
.comment
!= null) {
1554 cfile
.add_type_member_definition (new
CCodeComment (acc
.comment
.content
));
1557 bool returns_real_struct
= acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ();
1559 if (acc
.result_var
!= null) {
1560 acc
.result_var
.accept (this
);
1563 var t
= (TypeSymbol
) prop
.parent_symbol
;
1565 if (acc
.construction
&& !t
.is_subtype_of (gobject_type
)) {
1566 Report
.error (acc
.source_reference
, "construct properties require GLib.Object");
1569 } else if (acc
.construction
&& !is_gobject_property (prop
)) {
1570 Report
.error (acc
.source_reference
, "construct properties not supported for specified property type");
1575 // do not declare overriding properties and interface implementations
1576 if (prop
.is_abstract
|| prop
.is_virtual
1577 || (prop
.base_property
== null && prop
.base_interface_property
== null)) {
1578 generate_property_accessor_declaration (acc
, cfile
);
1580 // do not declare construct-only properties in header files
1581 if (acc
.readable
|| acc
.writable
) {
1582 if (!prop
.is_internal_symbol ()
1583 && (acc
.access
== SymbolAccessibility
.PUBLIC
1584 || acc
.access
== SymbolAccessibility
.PROTECTED
)) {
1585 generate_property_accessor_declaration (acc
, header_file
);
1587 if (!prop
.is_private_symbol () && acc
.access
!= SymbolAccessibility
.PRIVATE
) {
1588 generate_property_accessor_declaration (acc
, internal_header_file
);
1593 if (acc
.source_type
== SourceFileType
.FAST
) {
1598 var this_type
= get_data_type_for_symbol (t
);
1599 var cselfparam
= new
CCodeParameter ("self", get_ccode_name (this_type
));
1600 if (t is Struct
&& !((Struct
) t
).is_simple_type ()) {
1601 cselfparam
.type_name
+= "*";
1603 CCodeParameter cvalueparam
;
1604 if (returns_real_struct
) {
1605 cvalueparam
= new
CCodeParameter ("result", "%s *".printf (get_ccode_name (acc
.value_type
)));
1606 } else if (!acc
.readable
&& prop
.property_type
.is_real_non_null_struct_type ()) {
1607 cvalueparam
= new
CCodeParameter ("value", "%s *".printf (get_ccode_name (acc
.value_type
)));
1609 cvalueparam
= new
CCodeParameter ("value", get_ccode_name (acc
.value_type
));
1612 if (prop
.is_abstract
|| prop
.is_virtual
) {
1613 CCodeFunction function
;
1614 if (acc
.readable
&& !returns_real_struct
) {
1615 function
= new
CCodeFunction (get_ccode_name (acc
), get_ccode_name (current_return_type
));
1617 function
= new
CCodeFunction (get_ccode_name (acc
), "void");
1619 function
.add_parameter (cselfparam
);
1620 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1621 function
.add_parameter (cvalueparam
);
1624 if (acc
.value_type is ArrayType
) {
1625 var array_type
= (ArrayType
) acc
.value_type
;
1626 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1627 function
.add_parameter (new
CCodeParameter (get_array_length_cname (acc
.readable ?
"result" : "value", dim
), acc
.readable ?
"int*" : "int"));
1629 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1630 function
.add_parameter (new
CCodeParameter (get_delegate_target_cname (acc
.readable ?
"result" : "value"), acc
.readable ?
"gpointer*" : "gpointer"));
1631 if (!acc
.readable
&& acc
.value_type
.value_owned
) {
1632 function
.add_parameter (new
CCodeParameter (get_delegate_target_destroy_notify_cname ("value"), "GDestroyNotify"));
1636 if (!prop
.is_abstract
1637 && (prop
.is_private_symbol () || !(acc
.readable
|| acc
.writable
) || acc
.access
== SymbolAccessibility
.PRIVATE
)) {
1638 // accessor function should be private if the property is an internal symbol or it's a construct-only setter
1639 function
.modifiers
|= CCodeModifiers
.STATIC
;
1640 } else if (context
.hide_internal
&& (prop
.is_internal_symbol () || acc
.access
== SymbolAccessibility
.INTERNAL
)) {
1641 function
.modifiers
|= CCodeModifiers
.INTERNAL
;
1644 push_function (function
);
1646 if (prop
.binding
== MemberBinding
.INSTANCE
) {
1647 if (!acc
.readable
|| returns_real_struct
) {
1648 create_property_type_check_statement (prop
, false, t
, true, "self");
1650 create_property_type_check_statement (prop
, true, t
, true, "self");
1654 CCodeExpression vcast
;
1655 if (prop
.parent_symbol is Interface
) {
1656 var iface
= (Interface
) prop
.parent_symbol
;
1658 vcast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_INTERFACE".printf (get_ccode_upper_case_name (iface
, null))));
1659 ((CCodeFunctionCall
) vcast
).add_argument (new
CCodeIdentifier ("self"));
1661 var cl
= (Class
) prop
.parent_symbol
;
1662 if (!cl
.is_compact
) {
1663 vcast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS".printf (get_ccode_upper_case_name (cl
, null))));
1664 ((CCodeFunctionCall
) vcast
).add_argument (new
CCodeIdentifier ("self"));
1666 vcast
= new
CCodeIdentifier ("self");
1671 var vcall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (vcast
, "get_%s".printf (prop
.name
)));
1672 vcall
.add_argument (new
CCodeIdentifier ("self"));
1673 if (returns_real_struct
) {
1674 vcall
.add_argument (new
CCodeIdentifier ("result"));
1675 ccode
.add_expression (vcall
);
1677 if (acc
.value_type is ArrayType
) {
1678 var array_type
= (ArrayType
) acc
.value_type
;
1680 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1681 var len_expr
= new
CCodeIdentifier (get_array_length_cname ("result", dim
));
1682 vcall
.add_argument (len_expr
);
1684 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1685 vcall
.add_argument (new
CCodeIdentifier (get_delegate_target_cname ("result")));
1688 ccode
.add_return (vcall
);
1691 var vcall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (vcast
, "set_%s".printf (prop
.name
)));
1692 vcall
.add_argument (new
CCodeIdentifier ("self"));
1693 vcall
.add_argument (new
CCodeIdentifier ("value"));
1695 if (acc
.value_type is ArrayType
) {
1696 var array_type
= (ArrayType
) acc
.value_type
;
1698 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1699 var len_expr
= new
CCodeIdentifier (get_array_length_cname ("value", dim
));
1700 vcall
.add_argument (len_expr
);
1702 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1703 vcall
.add_argument (new
CCodeIdentifier (get_delegate_target_cname ("value")));
1704 if (!acc
.readable
&& acc
.value_type
.value_owned
) {
1705 vcall
.add_argument (new
CCodeIdentifier (get_delegate_target_destroy_notify_cname ("value")));
1709 ccode
.add_expression (vcall
);
1714 cfile
.add_function (function
);
1717 if (!prop
.is_abstract
&& acc
.body
!= null) {
1718 bool is_virtual
= prop
.base_property
!= null || prop
.base_interface_property
!= null;
1720 string cname
= get_ccode_real_name (acc
);
1722 CCodeFunction function
;
1723 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1724 function
= new
CCodeFunction (cname
, "void");
1726 function
= new
CCodeFunction (cname
, get_ccode_name (acc
.value_type
));
1729 ObjectType base_type
= null;
1730 if (prop
.binding
== MemberBinding
.INSTANCE
) {
1732 if (prop
.base_property
!= null) {
1733 base_type
= new
ObjectType ((ObjectTypeSymbol
) prop
.base_property
.parent_symbol
);
1734 } else if (prop
.base_interface_property
!= null) {
1735 base_type
= new
ObjectType ((ObjectTypeSymbol
) prop
.base_interface_property
.parent_symbol
);
1737 function
.modifiers
|= CCodeModifiers
.STATIC
;
1738 function
.add_parameter (new
CCodeParameter ("base", get_ccode_name (base_type
)));
1740 function
.add_parameter (cselfparam
);
1743 if (acc
.writable
|| acc
.construction
|| returns_real_struct
) {
1744 function
.add_parameter (cvalueparam
);
1747 if (acc
.value_type is ArrayType
) {
1748 var array_type
= (ArrayType
) acc
.value_type
;
1749 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1750 function
.add_parameter (new
CCodeParameter (get_array_length_cname (acc
.readable ?
"result" : "value", dim
), acc
.readable ?
"int*" : "int"));
1752 } else if ((acc
.value_type is DelegateType
) && ((DelegateType
) acc
.value_type
).delegate_symbol
.has_target
) {
1753 function
.add_parameter (new
CCodeParameter (get_delegate_target_cname (acc
.readable ?
"result" : "value"), acc
.readable ?
"gpointer*" : "gpointer"));
1754 if (!acc
.readable
&& acc
.value_type
.value_owned
) {
1755 function
.add_parameter (new
CCodeParameter (get_delegate_target_destroy_notify_cname ("value"), "GDestroyNotify"));
1760 if (prop
.is_private_symbol () || !(acc
.readable
|| acc
.writable
) || acc
.access
== SymbolAccessibility
.PRIVATE
) {
1761 // accessor function should be private if the property is an internal symbol or it's a construct-only setter
1762 function
.modifiers
|= CCodeModifiers
.STATIC
;
1763 } else if (context
.hide_internal
&& (prop
.is_internal_symbol () || acc
.access
== SymbolAccessibility
.INTERNAL
)) {
1764 function
.modifiers
|= CCodeModifiers
.INTERNAL
;
1768 push_function (function
);
1770 if (prop
.binding
== MemberBinding
.INSTANCE
&& !is_virtual
) {
1771 if (!acc
.readable
|| returns_real_struct
) {
1772 create_property_type_check_statement (prop
, false, t
, true, "self");
1774 create_property_type_check_statement (prop
, true, t
, true, "self");
1778 if (acc
.readable
&& !returns_real_struct
) {
1779 // do not declare result variable if exit block is known to be unreachable
1780 if (acc
.return_block
== null || acc
.return_block
.get_predecessors ().size
> 0) {
1781 ccode
.add_declaration (get_ccode_name (acc
.value_type
), new
CCodeVariableDeclarator ("result"));
1786 ccode
.add_declaration (get_ccode_name (this_type
), new
CCodeVariableDeclarator ("self"));
1787 ccode
.add_assignment (new
CCodeIdentifier ("self"), get_cvalue_ (transform_value (new
GLibValue (base_type
, new
CCodeIdentifier ("base"), true), this_type
, acc
)));
1790 // notify on property changes
1791 if (is_gobject_property (prop
) &&
1793 (acc
.writable
|| acc
.construction
)) {
1794 var notify_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_object_notify_by_pspec"));
1795 notify_call
.add_argument (new
CCodeCastExpression (new
CCodeIdentifier ("self"), "GObject *"));
1796 notify_call
.add_argument (get_param_spec_cexpression (prop
));
1798 var get_accessor
= prop
.get_accessor
;
1799 if (get_accessor
!= null && get_accessor
.automatic_body
) {
1800 var property_type
= prop
.property_type
;
1801 var get_call
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_real_name (get_accessor
)));
1802 get_call
.add_argument (new
CCodeIdentifier (is_virtual ?
"base" : "self"));
1804 if (property_type is ArrayType
) {
1805 ccode
.add_declaration ("int", new
CCodeVariableDeclarator ("old_value_length"));
1806 get_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new
CCodeIdentifier ("old_value_length")));
1807 ccode
.open_if (new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, get_call
, new
CCodeIdentifier ("value")));
1808 } else if (property_type
.compatible (string_type
)) {
1809 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strcmp0"));
1810 ccall
.add_argument (new
CCodeIdentifier ("value"));
1811 ccall
.add_argument (get_call
);
1812 ccode
.open_if (new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, ccall
, new
CCodeConstant ("0")));
1813 } else if (property_type is StructValueType
) {
1814 ccode
.add_declaration (get_ccode_name (property_type
), new
CCodeVariableDeclarator ("old_value"));
1815 get_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new
CCodeIdentifier ("old_value")));
1817 var get_expr
= new
CCodeCommaExpression ();
1818 get_expr
.append_expression (get_call
);
1819 get_expr
.append_expression (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new
CCodeIdentifier ("old_value")));
1821 var equalfunc
= generate_struct_equal_function ((Struct
) property_type
.data_type
);
1822 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
1823 ccall
.add_argument (new
CCodeIdentifier ("value"));
1824 ccall
.add_argument (get_expr
);
1825 ccode
.open_if (new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, ccall
, new
CCodeConstant ("TRUE")));
1827 ccode
.open_if (new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, get_call
, new
CCodeIdentifier ("value")));
1830 acc
.body
.emit (this
);
1831 ccode
.add_expression (notify_call
);
1834 acc
.body
.emit (this
);
1835 ccode
.add_expression (notify_call
);
1838 acc
.body
.emit (this
);
1841 if (current_method_inner_error
) {
1842 ccode
.add_declaration ("GError *", new CCodeVariableDeclarator
.zero ("_inner_error_", new
CCodeConstant ("NULL")));
1845 cfile
.add_function (function
);
1852 public override void visit_destructor (Destructor d
) {
1853 if (d
.binding
== MemberBinding
.STATIC
&& !in_plugin
) {
1854 Report
.error (d
.source_reference
, "static destructors are only supported for dynamic types");
1860 public int get_block_id (Block b
) {
1861 int result
= block_map
[b
];
1863 result
= ++next_block_id
;
1864 block_map
[b
] = result
;
1869 public bool no_implicit_copy (DataType type
) {
1870 // note: implicit copy of array is planned to be forbidden
1871 var cl
= type
.data_type as Class
;
1872 return (type is DelegateType
||
1874 (cl
!= null && !cl
.is_immutable
&& !is_reference_counting (cl
) && !get_ccode_is_gboxed (cl
)));
1877 void capture_parameter (Parameter param
, CCodeStruct data
, int block_id
) {
1878 generate_type_declaration (param
.variable_type
, cfile
);
1880 var param_type
= param
.variable_type
.copy ();
1881 if (!param
.variable_type
.value_owned
) {
1882 param_type
.value_owned
= !no_implicit_copy (param
.variable_type
);
1884 data
.add_field (get_ccode_name (param_type
), get_variable_cname (param
.name
));
1886 // create copy if necessary as captured variables may need to be kept alive
1887 param
.captured
= false;
1888 var value
= load_parameter (param
);
1890 var array_type
= param
.variable_type as ArrayType
;
1891 var deleg_type
= param
.variable_type as DelegateType
;
1893 if (array_type
!= null && get_ccode_array_length (param
)) {
1894 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1895 data
.add_field ("gint", get_parameter_array_length_cname (param
, dim
));
1897 } else if (deleg_type
!= null && deleg_type
.delegate_symbol
.has_target
) {
1898 data
.add_field ("gpointer", get_ccode_delegate_target_name (param
));
1899 if (param
.variable_type
.is_disposable ()) {
1900 data
.add_field ("GDestroyNotify", get_delegate_target_destroy_notify_cname (get_variable_cname (param
.name
)));
1901 // reference transfer for delegates
1902 var lvalue
= get_parameter_cvalue (param
);
1903 ((GLibValue
) value
).delegate_target_destroy_notify_cvalue
= get_delegate_target_destroy_notify_cvalue (lvalue
);
1906 param
.captured
= true;
1908 store_parameter (param
, value
, true);
1911 public override void visit_block (Block b
) {
1912 emit_context
.push_symbol (b
);
1914 var local_vars
= b
.get_local_variables ();
1916 if (b
.parent_node is Block
|| b
.parent_node is SwitchStatement
|| b
.parent_node is TryStatement
) {
1917 ccode
.open_block ();
1921 var parent_block
= next_closure_block (b
.parent_symbol
);
1923 int block_id
= get_block_id (b
);
1924 string struct_name
= "Block%dData".printf (block_id
);
1926 var data
= new
CCodeStruct ("_" + struct_name
);
1927 data
.add_field ("int", "_ref_count_");
1928 if (parent_block
!= null) {
1929 int parent_block_id
= get_block_id (parent_block
);
1931 data
.add_field ("Block%dData *".printf (parent_block_id
), "_data%d_".printf (parent_block_id
));
1933 if (get_this_type () != null) {
1934 data
.add_field (get_ccode_name (get_data_type_for_symbol (current_type_symbol
)), "self");
1937 if (current_method
!= null) {
1938 // allow capturing generic type parameters
1939 foreach (var type_param
in current_method
.get_type_parameters ()) {
1942 func_name
= "%s_type".printf (type_param
.name
.down ());
1943 data
.add_field ("GType", func_name
);
1945 func_name
= "%s_dup_func".printf (type_param
.name
.down ());
1946 data
.add_field ("GBoxedCopyFunc", func_name
);
1948 func_name
= "%s_destroy_func".printf (type_param
.name
.down ());
1949 data
.add_field ("GDestroyNotify", func_name
);
1953 foreach (var local
in local_vars
) {
1954 if (local
.captured
) {
1955 generate_type_declaration (local
.variable_type
, cfile
);
1957 data
.add_field (get_ccode_name (local
.variable_type
), get_local_cname (local
), 0, get_ccode_declarator_suffix (local
.variable_type
));
1959 if (local
.variable_type is ArrayType
) {
1960 var array_type
= (ArrayType
) local
.variable_type
;
1961 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
1962 data
.add_field ("gint", get_array_length_cname (get_local_cname (local
), dim
));
1964 data
.add_field ("gint", get_array_size_cname (get_local_cname (local
)));
1965 } else if (local
.variable_type is DelegateType
) {
1966 data
.add_field ("gpointer", get_delegate_target_cname (get_local_cname (local
)));
1967 if (local
.variable_type
.value_owned
) {
1968 data
.add_field ("GDestroyNotify", get_delegate_target_destroy_notify_cname (get_local_cname (local
)));
1974 var data_alloc
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_slice_new0"));
1975 data_alloc
.add_argument (new
CCodeIdentifier (struct_name
));
1977 if (is_in_coroutine ()) {
1978 closure_struct
.add_field (struct_name
+ "*", "_data%d_".printf (block_id
));
1980 ccode
.add_declaration (struct_name
+ "*", new
CCodeVariableDeclarator ("_data%d_".printf (block_id
)));
1982 ccode
.add_assignment (get_variable_cexpression ("_data%d_".printf (block_id
)), data_alloc
);
1984 // initialize ref_count
1985 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "_ref_count_"), new
CCodeIdentifier ("1"));
1987 if (parent_block
!= null) {
1988 int parent_block_id
= get_block_id (parent_block
);
1990 var ref_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_ref".printf (parent_block_id
)));
1991 ref_call
.add_argument (get_variable_cexpression ("_data%d_".printf (parent_block_id
)));
1993 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "_data%d_".printf (parent_block_id
)), ref_call
);
1995 // skip self assignment in toplevel block of creation methods with chainup as self is not set at the beginning of the method
1996 // the chainup statement takes care of assigning self in the closure struct
1997 bool in_creation_method_with_chainup
= (current_method is CreationMethod
&& current_class
!= null && current_class
.base_class
!= null);
1999 if (get_this_type () != null && (!in_creation_method_with_chainup
|| current_method
.body
!= b
)) {
2000 var ref_call
= new
CCodeFunctionCall (get_dup_func_expression (get_data_type_for_symbol (current_type_symbol
), b
.source_reference
));
2001 ref_call
.add_argument (get_result_cexpression ("self"));
2003 // never increase reference count for self in finalizers to avoid infinite recursion on following unref
2004 var instance
= (is_in_destructor () ?
(CCodeExpression
) new
CCodeIdentifier ("self") : (CCodeExpression
) ref_call
);
2006 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "self"), instance
);
2009 if (current_method
!= null) {
2010 // allow capturing generic type parameters
2011 var suffices
= new
string[] {"type", "dup_func", "destroy_func"};
2012 foreach (var type_param
in current_method
.get_type_parameters ()) {
2013 foreach (string suffix
in suffices
) {
2014 string func_name
= "%s_%s".printf (type_param
.name
.down (), suffix
);
2015 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), func_name
), get_variable_cexpression (func_name
));
2021 if (b
.parent_symbol is Method
) {
2022 var m
= (Method
) b
.parent_symbol
;
2024 // parameters are captured with the top-level block of the method
2025 foreach (var param
in m
.get_parameters ()) {
2026 if (param
.captured
) {
2027 capture_parameter (param
, data
, block_id
);
2032 // capture async data to allow invoking callback from inside closure
2033 data
.add_field ("gpointer", "_async_data_");
2035 // async method is suspended while waiting for callback,
2036 // so we never need to care about memory management of async data
2037 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), "_async_data_"), new
CCodeIdentifier ("_data_"));
2039 } else if (b
.parent_symbol is PropertyAccessor
) {
2040 var acc
= (PropertyAccessor
) b
.parent_symbol
;
2042 if (!acc
.readable
&& acc
.value_parameter
.captured
) {
2043 capture_parameter (acc
.value_parameter
, data
, block_id
);
2045 } else if (b
.parent_symbol is ForeachStatement
) {
2046 var stmt
= (ForeachStatement
) b
.parent_symbol
;
2047 if (!stmt
.use_iterator
&& stmt
.element_variable
.captured
) {
2048 ccode
.add_assignment (new CCodeMemberAccess
.pointer (get_variable_cexpression ("_data%d_".printf (block_id
)), get_local_cname (stmt
.element_variable
)), get_variable_cexpression (get_local_cname (stmt
.element_variable
)));
2052 var typedef
= new
CCodeTypeDefinition ("struct _" + struct_name
, new
CCodeVariableDeclarator (struct_name
));
2053 cfile
.add_type_declaration (typedef
);
2054 cfile
.add_type_definition (data
);
2056 // create ref/unref functions
2057 var ref_fun
= new
CCodeFunction ("block%d_data_ref".printf (block_id
), struct_name
+ "*");
2058 ref_fun
.add_parameter (new
CCodeParameter ("_data%d_".printf (block_id
), struct_name
+ "*"));
2059 ref_fun
.modifiers
= CCodeModifiers
.STATIC
;
2061 push_function (ref_fun
);
2063 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_atomic_int_inc"));
2064 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_ref_count_")));
2065 ccode
.add_expression (ccall
);
2066 ccode
.add_return (new
CCodeIdentifier ("_data%d_".printf (block_id
)));
2070 cfile
.add_function_declaration (ref_fun
);
2071 cfile
.add_function (ref_fun
);
2073 var unref_fun
= new
CCodeFunction ("block%d_data_unref".printf (block_id
), "void");
2074 unref_fun
.add_parameter (new
CCodeParameter ("_userdata_", "void *"));
2075 unref_fun
.modifiers
= CCodeModifiers
.STATIC
;
2077 push_function (unref_fun
);
2079 ccode
.add_declaration (struct_name
+ "*", new
CCodeVariableDeclarator ("_data%d_".printf (block_id
), new
CCodeCastExpression (new
CCodeIdentifier ("_userdata_"), struct_name
+ "*")));
2080 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_atomic_int_dec_and_test"));
2081 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_ref_count_")));
2082 ccode
.open_if (ccall
);
2084 CCodeExpression outer_block
= new
CCodeIdentifier ("_data%d_".printf (block_id
));
2085 unowned Block parent_closure_block
= b
;
2087 parent_closure_block
= next_closure_block (parent_closure_block
.parent_symbol
);
2088 if (parent_closure_block
== null) {
2091 int parent_block_id
= get_block_id (parent_closure_block
);
2092 outer_block
= new CCodeMemberAccess
.pointer (outer_block
, "_data%d_".printf (parent_block_id
));
2095 if (get_this_type () != null) {
2096 // assign "self" for type parameters
2097 ccode
.add_declaration(get_ccode_name (get_data_type_for_symbol (current_type_symbol
)), new
CCodeVariableDeclarator ("self"));
2098 ccode
.add_assignment (new
CCodeIdentifier ("self"), new CCodeMemberAccess
.pointer (outer_block
, "self"));
2101 if (current_method
!= null) {
2102 // assign captured generic type parameters
2103 foreach (var type_param
in current_method
.get_type_parameters ()) {
2106 func_name
= "%s_type".printf (type_param
.name
.down ());
2107 ccode
.add_declaration ("GType", new
CCodeVariableDeclarator (func_name
));
2108 ccode
.add_assignment (new
CCodeIdentifier (func_name
), new CCodeMemberAccess
.pointer (outer_block
, func_name
));
2110 func_name
= "%s_dup_func".printf (type_param
.name
.down ());
2111 ccode
.add_declaration ("GBoxedCopyFunc", new
CCodeVariableDeclarator (func_name
));
2112 ccode
.add_assignment (new
CCodeIdentifier (func_name
), new CCodeMemberAccess
.pointer (outer_block
, func_name
));
2114 func_name
= "%s_destroy_func".printf (type_param
.name
.down ());
2115 ccode
.add_declaration ("GDestroyNotify", new
CCodeVariableDeclarator (func_name
));
2116 ccode
.add_assignment (new
CCodeIdentifier (func_name
), new CCodeMemberAccess
.pointer (outer_block
, func_name
));
2120 // free in reverse order
2121 for (int i
= local_vars
.size
- 1; i
>= 0; i
--) {
2122 var local
= local_vars
[i
];
2123 if (local
.captured
) {
2124 if (requires_destroy (local
.variable_type
)) {
2125 bool old_coroutine
= false;
2126 if (current_method
!= null) {
2127 old_coroutine
= current_method
.coroutine
;
2128 current_method
.coroutine
= false;
2131 ccode
.add_expression (destroy_local (local
));
2133 if (old_coroutine
) {
2134 current_method
.coroutine
= true;
2140 if (b
.parent_symbol is Method
) {
2141 var m
= (Method
) b
.parent_symbol
;
2143 // parameters are captured with the top-level block of the method
2144 foreach (var param
in m
.get_parameters ()) {
2145 if (param
.captured
) {
2146 var param_type
= param
.variable_type
.copy ();
2147 if (!param_type
.value_owned
) {
2148 param_type
.value_owned
= !no_implicit_copy (param_type
);
2151 if (requires_destroy (param_type
)) {
2152 bool old_coroutine
= false;
2154 old_coroutine
= m
.coroutine
;
2155 m
.coroutine
= false;
2158 ccode
.add_expression (destroy_parameter (param
));
2160 if (old_coroutine
) {
2166 } else if (b
.parent_symbol is PropertyAccessor
) {
2167 var acc
= (PropertyAccessor
) b
.parent_symbol
;
2169 if (!acc
.readable
&& acc
.value_parameter
.captured
) {
2170 var param_type
= acc
.value_parameter
.variable_type
.copy ();
2171 if (!param_type
.value_owned
) {
2172 param_type
.value_owned
= !no_implicit_copy (param_type
);
2175 if (requires_destroy (param_type
)) {
2176 ccode
.add_expression (destroy_parameter (acc
.value_parameter
));
2181 // free parent block and "self" after captured variables
2182 // because they may require type parameters
2183 if (parent_block
!= null) {
2184 int parent_block_id
= get_block_id (parent_block
);
2186 var unref_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_unref".printf (parent_block_id
)));
2187 unref_call
.add_argument (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_data%d_".printf (parent_block_id
)));
2188 ccode
.add_expression (unref_call
);
2189 ccode
.add_assignment (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data%d_".printf (block_id
)), "_data%d_".printf (parent_block_id
)), new
CCodeConstant ("NULL"));
2191 var this_type
= get_this_type ();
2192 if (this_type
!= null) {
2193 this_type
= this_type
.copy ();
2194 this_type
.value_owned
= true;
2195 if (this_type
.is_disposable () && !is_in_destructor ()) {
2196 // reference count for self is not increased in finalizers
2197 var this_value
= new
GLibValue (get_data_type_for_symbol (current_type_symbol
), new
CCodeIdentifier ("self"), true);
2198 ccode
.add_expression (destroy_value (this_value
));
2203 var data_free
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_slice_free"));
2204 data_free
.add_argument (new
CCodeIdentifier (struct_name
));
2205 data_free
.add_argument (new
CCodeIdentifier ("_data%d_".printf (block_id
)));
2206 ccode
.add_expression (data_free
);
2212 cfile
.add_function_declaration (unref_fun
);
2213 cfile
.add_function (unref_fun
);
2216 foreach (Statement stmt
in b
.get_statements ()) {
2217 push_line (stmt
.source_reference
);
2222 // free in reverse order
2223 for (int i
= local_vars
.size
- 1; i
>= 0; i
--) {
2224 var local
= local_vars
[i
];
2225 local
.active
= false;
2226 if (!local
.unreachable
&& !local
.captured
&& requires_destroy (local
.variable_type
)) {
2227 ccode
.add_expression (destroy_local (local
));
2231 if (b
.parent_symbol is Method
) {
2232 var m
= (Method
) b
.parent_symbol
;
2233 foreach (Parameter param
in m
.get_parameters ()) {
2234 if (!param
.captured
&& !param
.ellipsis
&& requires_destroy (param
.variable_type
) && param
.direction
== ParameterDirection
.IN
) {
2235 ccode
.add_expression (destroy_parameter (param
));
2236 } else if (param
.direction
== ParameterDirection
.OUT
&& !m
.coroutine
) {
2237 return_out_parameter (param
);
2240 // check postconditions
2241 foreach (var postcondition
in m
.get_postconditions ()) {
2242 create_postcondition_statement (postcondition
);
2244 } else if (b
.parent_symbol is PropertyAccessor
) {
2245 var acc
= (PropertyAccessor
) b
.parent_symbol
;
2246 if (acc
.value_parameter
!= null && !acc
.value_parameter
.captured
&& requires_destroy (acc
.value_parameter
.variable_type
)) {
2247 ccode
.add_expression (destroy_parameter (acc
.value_parameter
));
2252 int block_id
= get_block_id (b
);
2254 var data_unref
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_unref".printf (block_id
)));
2255 data_unref
.add_argument (get_variable_cexpression ("_data%d_".printf (block_id
)));
2256 ccode
.add_expression (data_unref
);
2257 ccode
.add_assignment (get_variable_cexpression ("_data%d_".printf (block_id
)), new
CCodeConstant ("NULL"));
2260 if (b
.parent_node is Block
|| b
.parent_node is SwitchStatement
|| b
.parent_node is TryStatement
) {
2264 emit_context
.pop_symbol ();
2267 public override void visit_declaration_statement (DeclarationStatement stmt
) {
2268 stmt
.declaration
.accept (this
);
2271 public CCodeExpression
get_local_cexpression (LocalVariable local
) {
2272 if (is_in_coroutine ()) {
2273 return new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data_"), get_local_cname (local
));
2275 return new
CCodeIdentifier (get_local_cname (local
));
2279 public CCodeExpression
get_variable_cexpression (string name
) {
2280 if (is_in_coroutine ()) {
2281 return new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data_"), get_variable_cname (name
));
2283 return new
CCodeIdentifier (get_variable_cname (name
));
2287 public CCodeExpression
get_this_cexpression () {
2288 if (is_in_coroutine ()) {
2289 return new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data_"), "self");
2291 return new
CCodeIdentifier ("self");
2295 public string get_local_cname (LocalVariable local
) {
2296 var cname
= get_variable_cname (local
.name
);
2297 if (cname
[0].isdigit ()) {
2298 cname
= "_%s_".printf (cname
);
2300 if (is_in_coroutine ()) {
2301 var clash_index
= emit_context
.closure_variable_clash_map
.get (local
);
2302 if (clash_index
> 0) {
2303 cname
= "_vala%d_%s".printf (clash_index
, cname
);
2309 public string get_variable_cname (string name
) {
2310 if (name
[0] == '.') {
2311 if (name
== ".result") {
2314 // compiler-internal variable
2315 if (!variable_name_map
.contains (name
)) {
2316 variable_name_map
.set (name
, "_tmp%d_".printf (next_temp_var_id
));
2319 return variable_name_map
.get (name
);
2320 } else if (reserved_identifiers
.contains (name
)) {
2321 return "_%s_".printf (name
);
2327 public CCodeExpression
get_result_cexpression (string cname
= "result") {
2328 if (is_in_coroutine ()) {
2329 return new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data_"), cname
);
2331 return new
CCodeIdentifier (cname
);
2335 public bool is_simple_struct_creation (Variable variable
, Expression expr
) {
2336 var st
= variable
.variable_type
.data_type as Struct
;
2337 var creation
= expr as ObjectCreationExpression
;
2338 if (creation
!= null && st
!= null && (!st
.is_simple_type () || get_ccode_name (st
) == "va_list") && !variable
.variable_type
.nullable
&&
2339 variable
.variable_type
.data_type
!= gvalue_type
&& creation
.get_object_initializer ().size
== 0) {
2346 bool is_foreach_element_variable (LocalVariable local
) {
2347 var block
= local
.parent_symbol
;
2348 if (block
!= null) {
2349 var stmt
= block
.parent_symbol as ForeachStatement
;
2350 if (stmt
!= null && !stmt
.use_iterator
&& stmt
.element_variable
== local
) {
2357 public override void visit_local_variable (LocalVariable local
) {
2358 check_type (local
.variable_type
);
2362 generate_type_declaration (local
.variable_type
, cfile
);
2364 // captured element variables of foreach statements (without iterator) require local declaration
2365 var declared
= !local
.captured
|| is_foreach_element_variable (local
);
2367 if (is_in_coroutine ()) {
2368 var count
= emit_context
.closure_variable_count_map
.get (local
.name
);
2370 emit_context
.closure_variable_clash_map
.set (local
, count
);
2372 emit_context
.closure_variable_count_map
.set (local
.name
, count
+ 1);
2374 closure_struct
.add_field (get_ccode_name (local
.variable_type
), get_local_cname (local
), 0, get_ccode_declarator_suffix (local
.variable_type
));
2376 var cvar
= new
CCodeVariableDeclarator (get_local_cname (local
), null, get_ccode_declarator_suffix (local
.variable_type
));
2378 // try to initialize uninitialized variables
2379 // initialization not necessary for variables stored in closure
2380 cvar
.initializer
= default_value_for_type (local
.variable_type
, true);
2383 ccode
.add_declaration (get_ccode_name (local
.variable_type
), cvar
);
2387 /* Emit initializer */
2388 if (local
.initializer
!= null) {
2389 local
.initializer
.emit (this
);
2391 visit_end_full_expression (local
.initializer
);
2395 CCodeExpression rhs
= null;
2396 if (local
.initializer
!= null && get_cvalue (local
.initializer
) != null) {
2397 rhs
= get_cvalue (local
.initializer
);
2400 /* Additional temp variables */
2403 if (local
.variable_type is ArrayType
) {
2404 // create variables to store array dimensions
2405 var array_type
= (ArrayType
) local
.variable_type
;
2407 if (!array_type
.fixed_length
) {
2408 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
2409 var len_var
= new
LocalVariable (int_type
.copy (), get_array_length_cname (get_local_cname (local
), dim
));
2410 len_var
.init
= local
.initializer
== null;
2411 emit_temp_var (len_var
);
2414 if (array_type
.rank
== 1) {
2415 var size_var
= new
LocalVariable (int_type
.copy (), get_array_size_cname (get_local_cname (local
)));
2416 size_var
.init
= local
.initializer
== null;
2417 emit_temp_var (size_var
);
2420 } else if (local
.variable_type is DelegateType
) {
2421 var deleg_type
= (DelegateType
) local
.variable_type
;
2422 var d
= deleg_type
.delegate_symbol
;
2424 // create variable to store delegate target
2425 var target_var
= new
LocalVariable (new
PointerType (new
VoidType ()), get_delegate_target_cname (get_local_cname (local
)));
2426 target_var
.init
= local
.initializer
== null;
2427 emit_temp_var (target_var
);
2428 if (deleg_type
.value_owned
) {
2429 var target_destroy_notify_var
= new
LocalVariable (gdestroynotify_type
, get_delegate_target_destroy_notify_cname (get_local_cname (local
)));
2430 target_destroy_notify_var
.init
= local
.initializer
== null;
2431 emit_temp_var (target_destroy_notify_var
);
2437 /* Store the initializer */
2440 if (!is_simple_struct_creation (local
, local
.initializer
)) {
2441 store_local (local
, local
.initializer
.target_value
, true);
2445 if (local
.initializer
!= null && local
.initializer
.tree_can_fail
) {
2446 add_simple_check (local
.initializer
);
2449 local
.active
= true;
2453 * Create a temporary variable and return lvalue access to it
2455 public TargetValue
create_temp_value (DataType type
, bool init
, CodeNode node_reference
, bool? value_owned
= null) {
2456 var local
= new
LocalVariable (type
.copy (), "_tmp%d_".printf (next_temp_var_id
++), null, node_reference
.source_reference
);
2458 if (value_owned
!= null) {
2459 local
.variable_type
.value_owned
= value_owned
;
2462 var array_type
= local
.variable_type as ArrayType
;
2463 var deleg_type
= local
.variable_type as DelegateType
;
2465 emit_temp_var (local
);
2466 if (array_type
!= null) {
2467 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
2468 var len_var
= new
LocalVariable (int_type
.copy (), get_array_length_cname (local
.name
, dim
), null, node_reference
.source_reference
);
2469 len_var
.init
= init
;
2470 emit_temp_var (len_var
);
2472 } else if (deleg_type
!= null && deleg_type
.delegate_symbol
.has_target
) {
2473 var target_var
= new
LocalVariable (new
PointerType (new
VoidType ()), get_delegate_target_cname (local
.name
), null, node_reference
.source_reference
);
2474 target_var
.init
= init
;
2475 emit_temp_var (target_var
);
2476 if (deleg_type
.value_owned
) {
2477 var target_destroy_notify_var
= new
LocalVariable (gdestroynotify_type
.copy (), get_delegate_target_destroy_notify_cname (local
.name
), null, node_reference
.source_reference
);
2478 target_destroy_notify_var
.init
= init
;
2479 emit_temp_var (target_destroy_notify_var
);
2483 var value
= get_local_cvalue (local
);
2484 set_array_size_cvalue (value
, null);
2489 * Load a temporary variable returning unowned or owned rvalue access to it, depending on the ownership of the value type.
2491 public TargetValue
load_temp_value (TargetValue lvalue
) {
2492 var value
= ((GLibValue
) lvalue
).copy ();
2493 var deleg_type
= value
.value_type as DelegateType
;
2494 if (deleg_type
!= null) {
2495 if (!deleg_type
.delegate_symbol
.has_target
) {
2496 value
.delegate_target_cvalue
= new
CCodeConstant ("NULL");
2497 ((GLibValue
) value
).lvalue
= false;
2498 } else if (!deleg_type
.is_disposable ()) {
2499 value
.delegate_target_destroy_notify_cvalue
= new
CCodeConstant ("NULL");
2500 ((GLibValue
) value
).lvalue
= false;
2507 * Store a value in a temporary variable and return unowned or owned rvalue access to it, depending on the ownership of the given type.
2509 public TargetValue
store_temp_value (TargetValue initializer
, CodeNode node_reference
, bool? value_owned
= null) {
2510 var lvalue
= create_temp_value (initializer
.value_type
, false, node_reference
, value_owned
);
2511 store_value (lvalue
, initializer
, node_reference
.source_reference
);
2512 return load_temp_value (lvalue
);
2515 public override void visit_initializer_list (InitializerList list
) {
2516 if (list
.target_type
.data_type is Struct
) {
2517 /* initializer is used as struct initializer */
2518 var st
= (Struct
) list
.target_type
.data_type
;
2519 while (st
.base_struct
!= null) {
2520 st
= st
.base_struct
;
2523 if (list
.parent_node is Constant
|| list
.parent_node is Field
|| list
.parent_node is InitializerList
) {
2524 var clist
= new
CCodeInitializerList ();
2526 var field_it
= st
.get_fields ().iterator ();
2527 foreach (Expression expr
in list
.get_initializers ()) {
2529 while (field
== null) {
2531 field
= field_it
.get ();
2532 if (field
.binding
!= MemberBinding
.INSTANCE
) {
2533 // we only initialize instance fields
2538 var cexpr
= get_cvalue (expr
);
2540 string ctype
= get_ccode_type (field
);
2541 if (ctype
!= null) {
2542 cexpr
= new
CCodeCastExpression (cexpr
, ctype
);
2545 clist
.append (cexpr
);
2547 var array_type
= field
.variable_type as ArrayType
;
2548 if (array_type
!= null && get_ccode_array_length (field
) && !get_ccode_array_null_terminated (field
)) {
2549 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
2550 clist
.append (get_array_length_cvalue (expr
.target_value
, dim
));
2555 set_cvalue (list
, clist
);
2557 // used as expression
2558 var instance
= create_temp_value (list
.value_type
, true, list
);
2560 var field_it
= st
.get_fields ().iterator ();
2561 foreach (Expression expr
in list
.get_initializers ()) {
2563 while (field
== null) {
2565 field
= field_it
.get ();
2566 if (field
.binding
!= MemberBinding
.INSTANCE
) {
2567 // we only initialize instance fields
2572 store_field (field
, instance
, expr
.target_value
);
2575 list
.target_value
= instance
;
2578 var clist
= new
CCodeInitializerList ();
2579 foreach (Expression expr
in list
.get_initializers ()) {
2580 clist
.append (get_cvalue (expr
));
2582 set_cvalue (list
, clist
);
2586 public LocalVariable
get_temp_variable (DataType type
, bool value_owned
= true, CodeNode? node_reference
= null, bool init
= false) {
2587 var var_type
= type
.copy ();
2588 var_type
.value_owned
= value_owned
;
2589 var local
= new
LocalVariable (var_type
, "_tmp%d_".printf (next_temp_var_id
));
2592 if (node_reference
!= null) {
2593 local
.source_reference
= node_reference
.source_reference
;
2601 bool is_in_generic_type (GenericType type
) {
2602 if (current_symbol
!= null && type
.type_parameter
.parent_symbol is TypeSymbol
2603 && (current_method
== null || current_method
.binding
== MemberBinding
.INSTANCE
)) {
2610 void require_generic_accessors (Interface iface
) {
2611 if (iface
.get_attribute ("GenericAccessors") == null) {
2612 Report
.error (iface
.source_reference
,
2613 "missing generic type for interface `%s', add GenericAccessors attribute to interface declaration"
2614 .printf (iface
.get_full_name ()));
2618 public CCodeExpression
get_type_id_expression (DataType type
, bool is_chainup
= false) {
2619 if (type is GenericType
) {
2620 var type_parameter
= ((GenericType
) type
).type_parameter
;
2621 string var_name
= "%s_type".printf (type_parameter
.name
.down ());
2623 if (type_parameter
.parent_symbol is Interface
) {
2624 var iface
= (Interface
) type_parameter
.parent_symbol
;
2625 require_generic_accessors (iface
);
2627 string method_name
= "get_%s_type".printf (type_parameter
.name
.down ());
2628 var cast_self
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_INTERFACE".printf (get_ccode_upper_case_name (iface
))));
2629 cast_self
.add_argument (new
CCodeIdentifier ("self"));
2630 var function_call
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (cast_self
, method_name
));
2631 function_call
.add_argument (new
CCodeIdentifier ("self"));
2632 return function_call
;
2635 if (is_in_generic_type ((GenericType
) type
) && !is_chainup
&& !in_creation_method
) {
2636 return new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (get_result_cexpression ("self"), "priv"), var_name
);
2638 return get_variable_cexpression (var_name
);
2641 string type_id
= get_ccode_type_id (type
);
2642 if (type_id
== "") {
2643 type_id
= "G_TYPE_INVALID";
2645 generate_type_declaration (type
, cfile
);
2647 return new
CCodeIdentifier (type_id
);
2651 public virtual CCodeExpression?
get_dup_func_expression (DataType type
, SourceReference? source_reference
, bool is_chainup
= false) {
2652 if (type is ErrorType
) {
2653 return new
CCodeIdentifier ("g_error_copy");
2654 } else if (type
.data_type
!= null) {
2655 string dup_function
;
2656 var cl
= type
.data_type as Class
;
2657 if (is_reference_counting (type
.data_type
)) {
2658 dup_function
= get_ccode_ref_function ((ObjectTypeSymbol
) type
.data_type
);
2659 if (type
.data_type is Interface
&& dup_function
== null) {
2660 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 ()));
2661 return new
CCodeInvalidExpression();
2663 } else if (cl
!= null && cl
.is_immutable
) {
2664 // allow duplicates of immutable instances as for example strings
2665 dup_function
= get_ccode_dup_function (type
.data_type
);
2666 if (dup_function
== null) {
2669 } else if (cl
!= null && get_ccode_is_gboxed (cl
)) {
2670 // allow duplicates of gboxed instances
2671 dup_function
= generate_dup_func_wrapper (type
);
2672 if (dup_function
== null) {
2675 } else if (type is ValueType
) {
2676 dup_function
= get_ccode_dup_function (type
.data_type
);
2677 if (dup_function
== null && type
.nullable
) {
2678 dup_function
= generate_struct_dup_wrapper ((ValueType
) type
);
2679 } else if (dup_function
== null) {
2683 // duplicating non-reference counted objects may cause side-effects (and performance issues)
2684 Report
.error (source_reference
, "duplicating %s instance, use unowned variable or explicitly invoke copy method".printf (type
.data_type
.name
));
2685 return new
CCodeInvalidExpression();
2688 return new
CCodeIdentifier (dup_function
);
2689 } else if (type is GenericType
) {
2690 var type_parameter
= ((GenericType
) type
).type_parameter
;
2691 string func_name
= "%s_dup_func".printf (type_parameter
.name
.down ());
2693 if (type_parameter
.parent_symbol is Interface
) {
2694 var iface
= (Interface
) type_parameter
.parent_symbol
;
2695 require_generic_accessors (iface
);
2697 string method_name
= "get_%s_dup_func".printf (type_parameter
.name
.down ());
2698 var cast_self
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_INTERFACE".printf (get_ccode_upper_case_name (iface
))));
2699 cast_self
.add_argument (new
CCodeIdentifier ("self"));
2700 var function_call
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (cast_self
, method_name
));
2701 function_call
.add_argument (new
CCodeIdentifier ("self"));
2702 return function_call
;
2705 if (is_in_generic_type ((GenericType
) type
) && !is_chainup
&& !in_creation_method
) {
2706 return new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (get_result_cexpression ("self"), "priv"), func_name
);
2708 return get_variable_cexpression (func_name
);
2710 } else if (type is PointerType
) {
2711 var pointer_type
= (PointerType
) type
;
2712 return get_dup_func_expression (pointer_type
.base_type
, source_reference
);
2714 return new
CCodeConstant ("NULL");
2718 void make_comparable_cexpression (ref DataType left_type
, ref CCodeExpression cleft
, ref DataType right_type
, ref CCodeExpression cright
) {
2719 var left_type_as_struct
= left_type
.data_type as Struct
;
2720 var right_type_as_struct
= right_type
.data_type as Struct
;
2723 var valuecast
= try_cast_value_to_type (cleft
, left_type
, right_type
);
2724 if (valuecast
!= null) {
2726 left_type
= right_type
;
2727 make_comparable_cexpression (ref left_type
, ref cleft
, ref right_type
, ref cright
);
2731 valuecast
= try_cast_value_to_type (cright
, right_type
, left_type
);
2732 if (valuecast
!= null) {
2734 right_type
= left_type
;
2735 make_comparable_cexpression (ref left_type
, ref cleft
, ref right_type
, ref cright
);
2739 if (left_type
.data_type is Class
&& !((Class
) left_type
.data_type
).is_compact
&&
2740 right_type
.data_type is Class
&& !((Class
) right_type
.data_type
).is_compact
) {
2741 var left_cl
= (Class
) left_type
.data_type
;
2742 var right_cl
= (Class
) right_type
.data_type
;
2744 if (left_cl
!= right_cl
) {
2745 if (left_cl
.is_subtype_of (right_cl
)) {
2746 cleft
= generate_instance_cast (cleft
, right_cl
);
2747 } else if (right_cl
.is_subtype_of (left_cl
)) {
2748 cright
= generate_instance_cast (cright
, left_cl
);
2751 } else if (left_type_as_struct
!= null && right_type_as_struct
!= null) {
2752 if (left_type is StructValueType
) {
2753 // real structs (uses compare/equal function)
2754 if (!left_type
.nullable
) {
2755 cleft
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cleft
);
2757 if (!right_type
.nullable
) {
2758 cright
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cright
);
2761 // integer or floating or boolean type
2762 if (left_type
.nullable
&& right_type
.nullable
) {
2763 // FIXME also compare contents, not just address
2764 } else if (left_type
.nullable
) {
2765 // FIXME check left value is not null
2766 cleft
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, cleft
);
2767 } else if (right_type
.nullable
) {
2768 // FIXME check right value is not null
2769 cright
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, cright
);
2775 private string generate_struct_equal_function (Struct st
) {
2776 if (st
.base_struct
!= null) {
2777 return generate_struct_equal_function (st
.base_struct
);
2780 string equal_func
= "_%sequal".printf (get_ccode_lower_case_prefix (st
));
2782 if (!add_wrapper (equal_func
)) {
2783 // wrapper already defined
2787 var function
= new
CCodeFunction (equal_func
, "gboolean");
2788 function
.modifiers
= CCodeModifiers
.STATIC
;
2790 function
.add_parameter (new
CCodeParameter ("s1", "const %s *".printf (get_ccode_name (st
))));
2791 function
.add_parameter (new
CCodeParameter ("s2", "const %s *".printf (get_ccode_name (st
))));
2793 push_function (function
);
2795 // if (s1 == s2) return TRUE;
2797 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeIdentifier ("s2"));
2798 ccode
.open_if (cexp
);
2799 ccode
.add_return (new
CCodeConstant ("TRUE"));
2802 // if (s1 == NULL || s2 == NULL) return FALSE;
2804 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeConstant ("NULL"));
2805 ccode
.open_if (cexp
);
2806 ccode
.add_return (new
CCodeConstant ("FALSE"));
2809 cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s2"), new
CCodeConstant ("NULL"));
2810 ccode
.open_if (cexp
);
2811 ccode
.add_return (new
CCodeConstant ("FALSE"));
2815 bool has_instance_fields
= false;
2816 foreach (Field f
in st
.get_fields ()) {
2817 if (f
.binding
!= MemberBinding
.INSTANCE
) {
2818 // we only compare instance fields
2822 has_instance_fields
= true;
2824 CCodeExpression cexp
; // if (cexp) return FALSE;
2825 var s1
= (CCodeExpression
) new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("s1"), get_ccode_name (f
)); // s1->f
2826 var s2
= (CCodeExpression
) new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("s2"), get_ccode_name (f
)); // s2->f
2828 var variable_type
= f
.variable_type
.copy ();
2829 make_comparable_cexpression (ref variable_type
, ref s1
, ref variable_type
, ref s2
);
2831 if (!(f
.variable_type is NullType
) && f
.variable_type
.compatible (string_type
)) {
2832 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strcmp0"));
2833 ccall
.add_argument (s1
);
2834 ccall
.add_argument (s2
);
2836 } else if (f
.variable_type is StructValueType
) {
2837 var equalfunc
= generate_struct_equal_function (f
.variable_type
.data_type as Struct
);
2838 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
2839 ccall
.add_argument (s1
);
2840 ccall
.add_argument (s2
);
2841 cexp
= new
CCodeUnaryExpression (CCodeUnaryOperator
.LOGICAL_NEGATION
, ccall
);
2843 cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, s1
, s2
);
2846 ccode
.open_if (cexp
);
2847 ccode
.add_return (new
CCodeConstant ("FALSE"));
2851 if (!has_instance_fields
) {
2852 // either opaque structure or simple type
2853 if (st
.is_simple_type ()) {
2854 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s1")), new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s2")));
2855 ccode
.add_return (cexp
);
2857 ccode
.add_return (new
CCodeConstant ("FALSE"));
2860 ccode
.add_return (new
CCodeConstant ("TRUE"));
2865 cfile
.add_function_declaration (function
);
2866 cfile
.add_function (function
);
2871 private string generate_numeric_equal_function (TypeSymbol sym
) {
2872 string equal_func
= "_%sequal".printf (get_ccode_lower_case_prefix (sym
));
2874 if (!add_wrapper (equal_func
)) {
2875 // wrapper already defined
2879 var function
= new
CCodeFunction (equal_func
, "gboolean");
2880 function
.modifiers
= CCodeModifiers
.STATIC
;
2882 function
.add_parameter (new
CCodeParameter ("s1", "const %s *".printf (get_ccode_name (sym
))));
2883 function
.add_parameter (new
CCodeParameter ("s2", "const %s *".printf (get_ccode_name (sym
))));
2885 push_function (function
);
2887 // if (s1 == s2) return TRUE;
2889 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeIdentifier ("s2"));
2890 ccode
.open_if (cexp
);
2891 ccode
.add_return (new
CCodeConstant ("TRUE"));
2894 // if (s1 == NULL || s2 == NULL) return FALSE;
2896 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s1"), new
CCodeConstant ("NULL"));
2897 ccode
.open_if (cexp
);
2898 ccode
.add_return (new
CCodeConstant ("FALSE"));
2901 cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier ("s2"), new
CCodeConstant ("NULL"));
2902 ccode
.open_if (cexp
);
2903 ccode
.add_return (new
CCodeConstant ("FALSE"));
2906 // return (*s1 == *s2);
2908 var cexp
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s1")), new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeIdentifier ("s2")));
2909 ccode
.add_return (cexp
);
2914 cfile
.add_function_declaration (function
);
2915 cfile
.add_function (function
);
2920 private string generate_struct_dup_wrapper (ValueType value_type
) {
2921 string dup_func
= "_%sdup".printf (get_ccode_lower_case_prefix (value_type
.type_symbol
));
2923 if (!add_wrapper (dup_func
)) {
2924 // wrapper already defined
2928 var function
= new
CCodeFunction (dup_func
, get_ccode_name (value_type
));
2929 function
.modifiers
= CCodeModifiers
.STATIC
;
2931 function
.add_parameter (new
CCodeParameter ("self", get_ccode_name (value_type
)));
2933 push_function (function
);
2935 if (value_type
.type_symbol
== gvalue_type
) {
2936 var dup_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_boxed_copy"));
2937 dup_call
.add_argument (new
CCodeIdentifier ("G_TYPE_VALUE"));
2938 dup_call
.add_argument (new
CCodeIdentifier ("self"));
2940 ccode
.add_return (dup_call
);
2942 ccode
.add_declaration (get_ccode_name (value_type
), new
CCodeVariableDeclarator ("dup"));
2944 var creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_new0"));
2945 creation_call
.add_argument (new
CCodeConstant (get_ccode_name (value_type
.data_type
)));
2946 creation_call
.add_argument (new
CCodeConstant ("1"));
2947 ccode
.add_assignment (new
CCodeIdentifier ("dup"), creation_call
);
2949 var st
= value_type
.data_type as Struct
;
2950 if (st
!= null && st
.is_disposable ()) {
2951 if (!get_ccode_has_copy_function (st
)) {
2952 generate_struct_copy_function (st
);
2955 var copy_call
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_copy_function (st
)));
2956 copy_call
.add_argument (new
CCodeIdentifier ("self"));
2957 copy_call
.add_argument (new
CCodeIdentifier ("dup"));
2958 ccode
.add_expression (copy_call
);
2960 cfile
.add_include ("string.h");
2962 var sizeof_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
2963 sizeof_call
.add_argument (new
CCodeConstant (get_ccode_name (value_type
.data_type
)));
2965 var copy_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memcpy"));
2966 copy_call
.add_argument (new
CCodeIdentifier ("dup"));
2967 copy_call
.add_argument (new
CCodeIdentifier ("self"));
2968 copy_call
.add_argument (sizeof_call
);
2969 ccode
.add_expression (copy_call
);
2972 ccode
.add_return (new
CCodeIdentifier ("dup"));
2977 cfile
.add_function_declaration (function
);
2978 cfile
.add_function (function
);
2983 protected string generate_dup_func_wrapper (DataType type
) {
2984 string destroy_func
= "_vala_%s_copy".printf (get_ccode_name (type
.data_type
));
2986 if (!add_wrapper (destroy_func
)) {
2987 // wrapper already defined
2988 return destroy_func
;
2991 var function
= new
CCodeFunction (destroy_func
, get_ccode_name (type
));
2992 function
.modifiers
= CCodeModifiers
.STATIC
;
2993 function
.add_parameter (new
CCodeParameter ("self", get_ccode_name (type
)));
2995 push_function (function
);
2997 var cl
= type
.data_type as Class
;
2998 assert (cl
!= null && get_ccode_is_gboxed (cl
));
3000 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_boxed_copy"));
3001 free_call
.add_argument (new
CCodeIdentifier (get_ccode_type_id (cl
)));
3002 free_call
.add_argument (new
CCodeIdentifier ("self"));
3004 ccode
.add_return (free_call
);
3008 cfile
.add_function_declaration (function
);
3009 cfile
.add_function (function
);
3011 return destroy_func
;
3014 protected string generate_free_function_address_of_wrapper (DataType type
) {
3015 string destroy_func
= "_vala_%s_free_function_address_of".printf (get_ccode_name (type
.data_type
));
3017 if (!add_wrapper (destroy_func
)) {
3018 // wrapper already defined
3019 return destroy_func
;
3022 var function
= new
CCodeFunction (destroy_func
, "void");
3023 function
.modifiers
= CCodeModifiers
.STATIC
;
3024 function
.add_parameter (new
CCodeParameter ("self", get_ccode_name (type
)));
3026 push_function (function
);
3028 var cl
= type
.data_type as Class
;
3029 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_free_function (cl
)));
3030 free_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, new
CCodeIdentifier ("self")));
3032 ccode
.add_expression (free_call
);
3036 cfile
.add_function_declaration (function
);
3037 cfile
.add_function (function
);
3039 return destroy_func
;
3042 protected string generate_free_func_wrapper (DataType type
) {
3043 string destroy_func
= "_vala_%s_free".printf (get_ccode_name (type
.data_type
));
3045 if (!add_wrapper (destroy_func
)) {
3046 // wrapper already defined
3047 return destroy_func
;
3050 var function
= new
CCodeFunction (destroy_func
, "void");
3051 function
.modifiers
= CCodeModifiers
.STATIC
;
3052 function
.add_parameter (new
CCodeParameter ("self", get_ccode_name (type
)));
3054 push_function (function
);
3056 var cl
= type
.data_type as Class
;
3057 if (cl
!= null && get_ccode_is_gboxed (cl
)) {
3058 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_boxed_free"));
3059 free_call
.add_argument (new
CCodeIdentifier (get_ccode_type_id (cl
)));
3060 free_call
.add_argument (new
CCodeIdentifier ("self"));
3062 ccode
.add_expression (free_call
);
3064 var st
= type
.data_type as Struct
;
3065 if (st
!= null && st
.is_disposable ()) {
3066 if (!get_ccode_has_destroy_function (st
)) {
3067 generate_struct_destroy_function (st
);
3070 var destroy_call
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_destroy_function (st
)));
3071 destroy_call
.add_argument (new
CCodeIdentifier ("self"));
3072 ccode
.add_expression (destroy_call
);
3075 var free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_free"));
3076 free_call
.add_argument (new
CCodeIdentifier ("self"));
3078 ccode
.add_expression (free_call
);
3083 cfile
.add_function_declaration (function
);
3084 cfile
.add_function (function
);
3086 return destroy_func
;
3089 public CCodeExpression?
get_destroy0_func_expression (DataType type
, bool is_chainup
= false) {
3090 var element_destroy_func_expression
= get_destroy_func_expression (type
, is_chainup
);
3092 if (!(type is GenericType
) && element_destroy_func_expression is CCodeIdentifier
) {
3093 var freeid
= (CCodeIdentifier
) element_destroy_func_expression
;
3094 string free0_func
= "_%s0_".printf (freeid
.name
);
3096 if (add_wrapper (free0_func
)) {
3097 var function
= new
CCodeFunction (free0_func
, "void");
3098 function
.modifiers
= CCodeModifiers
.STATIC
;
3100 function
.add_parameter (new
CCodeParameter ("var", "gpointer"));
3102 push_function (function
);
3104 ccode
.add_expression (destroy_value (new
GLibValue (type
, new
CCodeIdentifier ("var"), true), true));
3108 cfile
.add_function_declaration (function
);
3109 cfile
.add_function (function
);
3112 element_destroy_func_expression
= new
CCodeIdentifier (free0_func
);
3115 return element_destroy_func_expression
;
3118 public CCodeExpression?
get_destroy_func_expression (DataType type
, bool is_chainup
= false) {
3119 if (type
.data_type
== glist_type
|| type
.data_type
== gslist_type
|| type
.data_type
== gnode_type
|| type
.data_type
== gqueue_type
) {
3120 // create wrapper function to free list elements if necessary
3122 bool elements_require_free
= false;
3123 CCodeExpression element_destroy_func_expression
= null;
3125 foreach (DataType type_arg
in type
.get_type_arguments ()) {
3126 elements_require_free
= requires_destroy (type_arg
);
3127 if (elements_require_free
) {
3128 element_destroy_func_expression
= get_destroy0_func_expression (type_arg
);
3132 if (elements_require_free
&& element_destroy_func_expression is CCodeIdentifier
) {
3133 return new
CCodeIdentifier (generate_collection_free_wrapper (type
, (CCodeIdentifier
) element_destroy_func_expression
));
3135 return new
CCodeIdentifier (get_ccode_free_function (type
.data_type
));
3137 } else if (type is ErrorType
) {
3138 return new
CCodeIdentifier ("g_error_free");
3139 } else if (type
.data_type
!= null) {
3140 string unref_function
;
3141 if (type is ReferenceType
) {
3142 if (is_reference_counting (type
.data_type
)) {
3143 unref_function
= get_ccode_unref_function ((ObjectTypeSymbol
) type
.data_type
);
3144 if (type
.data_type is Interface
&& unref_function
== null) {
3145 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 ()));
3149 var cl
= type
.data_type as Class
;
3150 if (cl
!= null && get_ccode_is_gboxed (cl
)) {
3151 unref_function
= generate_free_func_wrapper (type
);
3153 if (is_free_function_address_of (type
)) {
3154 unref_function
= generate_free_function_address_of_wrapper (type
);
3156 unref_function
= get_ccode_free_function (type
.data_type
);
3161 if (type
.nullable
) {
3162 unref_function
= get_ccode_free_function (type
.data_type
);
3163 if (unref_function
== null) {
3164 if (type
.data_type is Struct
&& ((Struct
) type
.data_type
).is_disposable ()) {
3165 unref_function
= generate_free_func_wrapper (type
);
3167 unref_function
= "g_free";
3170 } else if (type is EnumValueType
) {
3171 unref_function
= null;
3173 var st
= (Struct
) type
.data_type
;
3174 if (st
.is_disposable ()) {
3175 if (!get_ccode_has_destroy_function (st
)) {
3176 generate_struct_destroy_function (st
);
3178 unref_function
= get_ccode_destroy_function (st
);
3180 unref_function
= null;
3184 if (unref_function
== null) {
3185 return new
CCodeConstant ("NULL");
3187 return new
CCodeIdentifier (unref_function
);
3188 } else if (type is GenericType
) {
3189 var type_parameter
= ((GenericType
) type
).type_parameter
;
3190 string func_name
= "%s_destroy_func".printf (type_parameter
.name
.down ());
3192 if (type_parameter
.parent_symbol is Interface
) {
3193 var iface
= (Interface
) type_parameter
.parent_symbol
;
3194 require_generic_accessors (iface
);
3196 string method_name
= "get_%s_destroy_func".printf (type_parameter
.name
.down ());
3197 var cast_self
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_INTERFACE".printf (get_ccode_upper_case_name (iface
))));
3198 cast_self
.add_argument (new
CCodeIdentifier ("self"));
3199 var function_call
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (cast_self
, method_name
));
3200 function_call
.add_argument (new
CCodeIdentifier ("self"));
3201 return function_call
;
3204 if (is_in_generic_type ((GenericType
) type
) && !is_chainup
&& !in_creation_method
) {
3205 return new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (get_result_cexpression ("self"), "priv"), func_name
);
3207 return get_variable_cexpression (func_name
);
3209 } else if (type is ArrayType
) {
3210 return new
CCodeIdentifier ("g_free");
3211 } else if (type is PointerType
) {
3212 return new
CCodeIdentifier ("g_free");
3214 return new
CCodeConstant ("NULL");
3218 private string generate_collection_free_wrapper (DataType collection_type
, CCodeIdentifier element_destroy_func_expression
) {
3219 string destroy_func
= "_%s_%s".printf (get_ccode_free_function (collection_type
.data_type
), element_destroy_func_expression
.name
);
3221 if (!add_wrapper (destroy_func
)) {
3222 // wrapper already defined
3223 return destroy_func
;
3226 var function
= new
CCodeFunction (destroy_func
, "void");
3227 function
.add_parameter (new
CCodeParameter ("self", get_ccode_name (collection_type
)));
3229 push_function (function
);
3231 if (collection_type
.data_type
== gnode_type
) {
3232 CCodeFunctionCall element_free_call
;
3233 /* A wrapper which converts GNodeTraverseFunc into GDestroyNotify */
3234 string destroy_node_func
= "%s_node".printf (destroy_func
);
3235 var wrapper
= new
CCodeFunction (destroy_node_func
, "gboolean");
3236 wrapper
.modifiers
= CCodeModifiers
.STATIC
;
3237 wrapper
.add_parameter (new
CCodeParameter ("node", get_ccode_name (collection_type
)));
3238 wrapper
.add_parameter (new
CCodeParameter ("unused", "gpointer"));
3239 push_function (wrapper
);
3241 var free_call
= new
CCodeFunctionCall (element_destroy_func_expression
);
3242 free_call
.add_argument (new CCodeMemberAccess
.pointer(new
CCodeIdentifier("node"), "data"));
3243 ccode
.add_expression (free_call
);
3244 ccode
.add_return (new
CCodeConstant ("FALSE"));
3247 cfile
.add_function_declaration (wrapper
);
3248 cfile
.add_function (wrapper
);
3250 /* Now the code to call g_traverse with the above */
3251 element_free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_node_traverse"));
3252 element_free_call
.add_argument (new
CCodeIdentifier("self"));
3253 element_free_call
.add_argument (new
CCodeConstant ("G_POST_ORDER"));
3254 element_free_call
.add_argument (new
CCodeConstant ("G_TRAVERSE_ALL"));
3255 element_free_call
.add_argument (new
CCodeConstant ("-1"));
3256 element_free_call
.add_argument (new
CCodeIdentifier (destroy_node_func
));
3257 element_free_call
.add_argument (new
CCodeConstant ("NULL"));
3258 ccode
.add_expression (element_free_call
);
3260 var cfreecall
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_free_function (gnode_type
)));
3261 cfreecall
.add_argument (new
CCodeIdentifier ("self"));
3262 ccode
.add_expression (cfreecall
);
3264 function
.modifiers
= CCodeModifiers
.STATIC
;
3266 CCodeFunctionCall collection_free_call
;
3267 if (collection_type
.data_type
== glist_type
) {
3268 collection_free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_list_free_full"));
3269 } else if (collection_type
.data_type
== gslist_type
) {
3270 collection_free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_slist_free_full"));
3272 collection_free_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_queue_free_full"));
3275 collection_free_call
.add_argument (new
CCodeIdentifier ("self"));
3276 collection_free_call
.add_argument (new
CCodeCastExpression (element_destroy_func_expression
, "GDestroyNotify"));
3277 ccode
.add_expression (collection_free_call
);
3279 function
.modifiers
= CCodeModifiers
.STATIC
| CCodeModifiers
.INLINE
;
3284 cfile
.add_function_declaration (function
);
3285 cfile
.add_function (function
);
3287 return destroy_func
;
3290 public virtual string?
append_struct_array_free (Struct st
) {
3294 public CCodeExpression
destroy_local (LocalVariable local
) {
3295 return destroy_value (get_local_cvalue (local
));
3298 public CCodeExpression
destroy_parameter (Parameter param
) {
3299 return destroy_value (get_parameter_cvalue (param
));
3302 public CCodeExpression
destroy_field (Field field
, TargetValue? instance
) {
3303 return destroy_value (get_field_cvalue (field
, instance
));
3306 public virtual CCodeExpression
destroy_value (TargetValue value
, bool is_macro_definition
= false) {
3307 var type
= value
.value_type
;
3308 if (value
.actual_value_type
!= null) {
3309 type
= value
.actual_value_type
;
3311 var cvar
= get_cvalue_ (value
);
3313 if (type is DelegateType
) {
3314 var delegate_target
= get_delegate_target_cvalue (value
);
3315 var delegate_target_destroy_notify
= get_delegate_target_destroy_notify_cvalue (value
);
3317 var ccall
= new
CCodeFunctionCall (delegate_target_destroy_notify
);
3318 ccall
.add_argument (delegate_target
);
3320 var destroy_call
= new
CCodeCommaExpression ();
3321 destroy_call
.append_expression (ccall
);
3322 destroy_call
.append_expression (new
CCodeConstant ("NULL"));
3324 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, delegate_target_destroy_notify
, new
CCodeConstant ("NULL"));
3326 var ccomma
= new
CCodeCommaExpression ();
3327 ccomma
.append_expression (new
CCodeConditionalExpression (cisnull
, new
CCodeConstant ("NULL"), destroy_call
));
3328 ccomma
.append_expression (new
CCodeAssignment (cvar
, new
CCodeConstant ("NULL")));
3329 ccomma
.append_expression (new
CCodeAssignment (delegate_target
, new
CCodeConstant ("NULL")));
3330 ccomma
.append_expression (new
CCodeAssignment (delegate_target_destroy_notify
, new
CCodeConstant ("NULL")));
3335 var ccall
= new
CCodeFunctionCall (get_destroy_func_expression (type
));
3337 if (type is ValueType
&& !type
.nullable
) {
3338 // normal value type, no null check
3339 var st
= type
.data_type as Struct
;
3340 if (st
!= null && st
.is_simple_type ()) {
3342 ccall
.add_argument (cvar
);
3344 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cvar
));
3347 if (gvalue_type
!= null && type
.data_type
== gvalue_type
) {
3348 // g_value_unset must not be called for already unset values
3349 var cisvalid
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_IS_VALUE"));
3350 cisvalid
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cvar
));
3352 var ccomma
= new
CCodeCommaExpression ();
3353 ccomma
.append_expression (ccall
);
3354 ccomma
.append_expression (new
CCodeConstant ("NULL"));
3356 return new
CCodeConditionalExpression (cisvalid
, ccomma
, new
CCodeConstant ("NULL"));
3357 } else if ((type
.data_type
== gmutex_type
||
3358 type
.data_type
== grecmutex_type
||
3359 type
.data_type
== grwlock_type
||
3360 type
.data_type
== gcond_type
)) {
3361 // g_mutex_clear must not be called for uninitialized mutex
3362 // also, g_mutex_clear does not clear the struct
3363 requires_clear_mutex
= true;
3364 ccall
.call
= new
CCodeIdentifier ("_vala_clear_" + get_ccode_name (type
.data_type
));
3371 if (ccall
.call is CCodeIdentifier
&& !(type is ArrayType
) && !is_macro_definition
) {
3372 // generate and use NULL-aware free macro to simplify code
3374 var freeid
= (CCodeIdentifier
) ccall
.call
;
3375 string free0_func
= "_%s0".printf (freeid
.name
);
3377 if (add_wrapper (free0_func
)) {
3378 var macro
= destroy_value (new
GLibValue (type
, new
CCodeIdentifier ("var"), true), true);
3379 cfile
.add_type_declaration (new CCodeMacroReplacement
.with_expression ("%s(var)".printf (free0_func
), macro
));
3382 ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (free0_func
));
3383 ccall
.add_argument (cvar
);
3387 /* (foo == NULL ? NULL : foo = (unref (foo), NULL)) */
3389 /* can be simplified to
3390 * foo = (unref (foo), NULL)
3391 * if foo is of static type non-null
3394 var cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, cvar
, new
CCodeConstant ("NULL"));
3395 if (type is GenericType
) {
3396 var parent
= ((GenericType
) type
).type_parameter
.parent_symbol
;
3397 var cl
= parent as Class
;
3398 if ((!(parent is Method
) && !(parent is ObjectTypeSymbol
)) || (cl
!= null && cl
.is_compact
)) {
3399 return new
CCodeConstant ("NULL");
3402 // unref functions are optional for type parameters
3403 var cunrefisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, get_destroy_func_expression (type
), new
CCodeConstant ("NULL"));
3404 cisnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.OR
, cisnull
, cunrefisnull
);
3407 ccall
.add_argument (cvar
);
3409 /* set freed references to NULL to prevent further use */
3410 var ccomma
= new
CCodeCommaExpression ();
3412 if (type
.data_type
!= null && !is_reference_counting (type
.data_type
) &&
3413 (type
.data_type
.is_subtype_of (gstringbuilder_type
)
3414 || type
.data_type
.is_subtype_of (garray_type
)
3415 || type
.data_type
.is_subtype_of (gbytearray_type
)
3416 || type
.data_type
.is_subtype_of (gptrarray_type
))) {
3417 ccall
.add_argument (new
CCodeConstant ("TRUE"));
3418 } else if (type
.data_type
== gthreadpool_type
) {
3419 ccall
.add_argument (new
CCodeConstant ("FALSE"));
3420 ccall
.add_argument (new
CCodeConstant ("TRUE"));
3421 } else if (type is ArrayType
) {
3422 var array_type
= (ArrayType
) type
;
3423 if (requires_destroy (array_type
.element_type
)) {
3424 CCodeExpression csizeexpr
= null;
3425 if (((GLibValue
) value
).array_length_cvalues
!= null) {
3426 csizeexpr
= get_array_length_cvalue (value
);
3427 } else if (get_array_null_terminated (value
)) {
3428 requires_array_length
= true;
3429 var len_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("_vala_array_length"));
3430 len_call
.add_argument (cvar
);
3431 csizeexpr
= len_call
;
3433 csizeexpr
= get_array_length_cexpr (value
);
3436 if (csizeexpr
!= null) {
3437 var st
= array_type
.element_type
.data_type as Struct
;
3438 if (st
!= null && !array_type
.element_type
.nullable
) {
3439 ccall
.call
= new
CCodeIdentifier (append_struct_array_free (st
));
3440 ccall
.add_argument (csizeexpr
);
3442 requires_array_free
= true;
3443 ccall
.call
= new
CCodeIdentifier ("_vala_array_free");
3444 ccall
.add_argument (csizeexpr
);
3445 ccall
.add_argument (new
CCodeCastExpression (get_destroy_func_expression (array_type
.element_type
), "GDestroyNotify"));
3451 ccomma
.append_expression (ccall
);
3452 ccomma
.append_expression (new
CCodeConstant ("NULL"));
3454 var cassign
= new
CCodeAssignment (cvar
, ccomma
);
3456 // g_free (NULL) is allowed
3457 bool uses_gfree
= (type
.data_type
!= null && !is_reference_counting (type
.data_type
) && get_ccode_free_function (type
.data_type
) == "g_free");
3458 uses_gfree
= uses_gfree
|| type is ArrayType
;
3463 return new
CCodeConditionalExpression (cisnull
, new
CCodeConstant ("NULL"), cassign
);
3466 public override void visit_end_full_expression (Expression expr
) {
3467 /* expr is a full expression, i.e. an initializer, the
3468 * expression in an expression statement, the controlling
3469 * expression in if, while, for, or foreach statements
3471 * we unref temporary variables at the end of a full
3474 if (temp_ref_values
.size
== 0) {
3475 /* nothing to do without temporary variables */
3479 var local_decl
= expr
.parent_node as LocalVariable
;
3480 if (!(local_decl
!= null && is_simple_struct_creation (local_decl
, local_decl
.initializer
))) {
3481 expr
.target_value
= store_temp_value (expr
.target_value
, expr
);
3484 foreach (var value
in temp_ref_values
) {
3485 ccode
.add_expression (destroy_value (value
));
3488 temp_ref_values
.clear ();
3491 public void emit_temp_var (LocalVariable local
) {
3492 var init
= (!local
.name
.has_prefix ("*") && local
.init
);
3493 if (is_in_coroutine ()) {
3494 closure_struct
.add_field (get_ccode_name (local
.variable_type
), local
.name
);
3496 // even though closure struct is zerod, we need to initialize temporary variables
3497 // as they might be used multiple times when declared in a loop
3500 var initializer
= default_value_for_type (local
.variable_type
, false);
3501 if (initializer
== null) {
3502 cfile
.add_include ("string.h");
3503 var memset_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memset"));
3504 memset_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression (local
.name
)));
3505 memset_call
.add_argument (new
CCodeConstant ("0"));
3506 memset_call
.add_argument (new
CCodeIdentifier ("sizeof (%s)".printf (get_ccode_name (local
.variable_type
))));
3507 ccode
.add_expression (memset_call
);
3509 ccode
.add_assignment (get_variable_cexpression (local
.name
), initializer
);
3513 var cvar
= new
CCodeVariableDeclarator (local
.name
, null, get_ccode_declarator_suffix (local
.variable_type
));
3515 cvar
.initializer
= default_value_for_type (local
.variable_type
, true);
3518 ccode
.add_declaration (get_ccode_name (local
.variable_type
), cvar
);
3522 public override void visit_expression_statement (ExpressionStatement stmt
) {
3523 if (stmt
.expression
.error
) {
3528 /* free temporary objects and handle errors */
3530 foreach (var value
in temp_ref_values
) {
3531 ccode
.add_expression (destroy_value (value
));
3534 if (stmt
.tree_can_fail
&& stmt
.expression
.tree_can_fail
) {
3535 // simple case, no node breakdown necessary
3536 add_simple_check (stmt
.expression
);
3539 temp_ref_values
.clear ();
3542 protected virtual void append_scope_free (Symbol sym
, CodeNode? stop_at
= null) {
3543 var b
= (Block
) sym
;
3545 var local_vars
= b
.get_local_variables ();
3546 // free in reverse order
3547 for (int i
= local_vars
.size
- 1; i
>= 0; i
--) {
3548 var local
= local_vars
[i
];
3549 if (!local
.unreachable
&& local
.active
&& !local
.captured
&& requires_destroy (local
.variable_type
)) {
3550 ccode
.add_expression (destroy_local (local
));
3555 int block_id
= get_block_id (b
);
3557 var data_unref
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_unref".printf (block_id
)));
3558 data_unref
.add_argument (get_variable_cexpression ("_data%d_".printf (block_id
)));
3559 ccode
.add_expression (data_unref
);
3560 ccode
.add_assignment (get_variable_cexpression ("_data%d_".printf (block_id
)), new
CCodeConstant ("NULL"));
3564 public void append_local_free (Symbol sym
, bool stop_at_loop
= false, CodeNode? stop_at
= null) {
3565 var b
= (Block
) sym
;
3567 append_scope_free (sym
, stop_at
);
3570 if (b
.parent_node is Loop
||
3571 b
.parent_node is ForeachStatement
||
3572 b
.parent_node is SwitchStatement
) {
3577 if (stop_at
!= null && b
.parent_node
== stop_at
) {
3581 if (sym
.parent_symbol is Block
) {
3582 append_local_free (sym
.parent_symbol
, stop_at_loop
, stop_at
);
3583 } else if (sym
.parent_symbol is Method
) {
3584 append_param_free ((Method
) sym
.parent_symbol
);
3585 } else if (sym
.parent_symbol is PropertyAccessor
) {
3586 var acc
= (PropertyAccessor
) sym
.parent_symbol
;
3587 if (acc
.value_parameter
!= null && requires_destroy (acc
.value_parameter
.variable_type
)) {
3588 ccode
.add_expression (destroy_parameter (acc
.value_parameter
));
3593 private void append_param_free (Method m
) {
3594 foreach (Parameter param
in m
.get_parameters ()) {
3595 if (!param
.captured
&& !param
.ellipsis
&& requires_destroy (param
.variable_type
) && param
.direction
== ParameterDirection
.IN
) {
3596 ccode
.add_expression (destroy_parameter (param
));
3601 public bool variable_accessible_in_finally (LocalVariable local
) {
3602 if (current_try
== null) {
3606 var sym
= current_symbol
;
3608 while (!(sym is Method
|| sym is PropertyAccessor
) && sym
.scope
.lookup (local
.name
) == null) {
3609 if ((sym
.parent_node is TryStatement
&& ((TryStatement
) sym
.parent_node
).finally_body
!= null) ||
3610 (sym
.parent_node is CatchClause
&& ((TryStatement
) sym
.parent_node
.parent_node
).finally_body
!= null)) {
3615 sym
= sym
.parent_symbol
;
3621 public void return_out_parameter (Parameter param
) {
3622 var delegate_type
= param
.variable_type as DelegateType
;
3624 var value
= get_parameter_cvalue (param
);
3626 var old_coroutine
= is_in_coroutine ();
3627 current_method
.coroutine
= false;
3629 ccode
.open_if (get_variable_cexpression (param
.name
));
3630 ccode
.add_assignment (new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_variable_cexpression (param
.name
)), get_cvalue_ (value
));
3632 if (delegate_type
!= null && delegate_type
.delegate_symbol
.has_target
) {
3633 ccode
.add_assignment (new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_variable_cexpression (get_ccode_delegate_target_name (param
))), get_delegate_target_cvalue (value
));
3634 if (delegate_type
.is_disposable ()) {
3635 ccode
.add_assignment (new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_variable_cexpression (get_delegate_target_destroy_notify_cname (param
.name
))), get_delegate_target_destroy_notify_cvalue (get_parameter_cvalue (param
)));
3639 if (param
.variable_type
.is_disposable ()){
3641 current_method
.coroutine
= old_coroutine
;
3642 ccode
.add_expression (destroy_parameter (param
));
3643 current_method
.coroutine
= false;
3647 var array_type
= param
.variable_type as ArrayType
;
3648 if (array_type
!= null && !array_type
.fixed_length
&& get_ccode_array_length (param
)) {
3649 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
3650 ccode
.open_if (get_variable_cexpression (get_parameter_array_length_cname (param
, dim
)));
3651 ccode
.add_assignment (new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_variable_cexpression (get_parameter_array_length_cname (param
, dim
))), get_array_length_cvalue (value
, dim
));
3656 current_method
.coroutine
= old_coroutine
;
3659 public override void visit_return_statement (ReturnStatement stmt
) {
3660 Symbol return_expression_symbol
= null;
3662 if (stmt
.return_expression
!= null) {
3663 // avoid unnecessary ref/unref pair
3664 var local
= stmt
.return_expression
.symbol_reference as LocalVariable
;
3665 if (local
!= null && !local
.active
) {
3666 /* return expression is local variable taking ownership and
3667 * current method is transferring ownership */
3669 return_expression_symbol
= local
;
3673 // return array length if appropriate
3674 if (((current_method
!= null && get_ccode_array_length (current_method
)) || current_property_accessor
!= null) && current_return_type is ArrayType
) {
3675 var temp_value
= store_temp_value (stmt
.return_expression
.target_value
, stmt
);
3677 var array_type
= (ArrayType
) current_return_type
;
3678 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
3679 var len_l
= get_result_cexpression (get_array_length_cname ("result", dim
));
3680 var len_r
= get_array_length_cvalue (temp_value
, dim
);
3681 if (!is_in_coroutine ()) {
3682 ccode
.open_if (len_l
);
3683 len_l
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, len_l
);
3684 ccode
.add_assignment (len_l
, len_r
);
3687 ccode
.add_assignment (len_l
, len_r
);
3691 stmt
.return_expression
.target_value
= temp_value
;
3692 } else if ((current_method
!= null || current_property_accessor
!= null) && current_return_type is DelegateType
) {
3693 var delegate_type
= (DelegateType
) current_return_type
;
3694 if (delegate_type
.delegate_symbol
.has_target
) {
3695 var temp_value
= store_temp_value (stmt
.return_expression
.target_value
, stmt
);
3697 var target_l
= get_result_cexpression (get_delegate_target_cname ("result"));
3698 if (!is_in_coroutine ()) {
3699 target_l
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, target_l
);
3701 var target_r
= get_delegate_target_cvalue (temp_value
);
3702 ccode
.add_assignment (target_l
, target_r
);
3703 if (delegate_type
.is_disposable ()) {
3704 var target_l_destroy_notify
= get_result_cexpression (get_delegate_target_destroy_notify_cname ("result"));
3705 if (!is_in_coroutine ()) {
3706 target_l_destroy_notify
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, target_l_destroy_notify
);
3708 var target_r_destroy_notify
= get_delegate_target_destroy_notify_cvalue (temp_value
);
3709 ccode
.add_assignment (target_l_destroy_notify
, target_r_destroy_notify
);
3712 stmt
.return_expression
.target_value
= temp_value
;
3716 if (stmt
.return_expression
!= null) {
3717 // assign method result to `result'
3718 CCodeExpression result_lhs
= get_result_cexpression ();
3719 if (current_return_type
.is_real_non_null_struct_type () && !is_in_coroutine ()) {
3720 result_lhs
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, result_lhs
);
3722 ccode
.add_assignment (result_lhs
, get_cvalue (stmt
.return_expression
));
3725 // free local variables
3726 append_local_free (current_symbol
);
3728 if (current_method
!= null) {
3729 // check postconditions
3730 foreach (Expression postcondition
in current_method
.get_postconditions ()) {
3731 create_postcondition_statement (postcondition
);
3735 if (current_method
!= null && !current_method
.coroutine
) {
3736 // assign values to output parameters if they are not NULL
3737 // otherwise, free the value if necessary
3738 foreach (var param
in current_method
.get_parameters ()) {
3739 if (param
.direction
!= ParameterDirection
.OUT
) {
3743 return_out_parameter (param
);
3747 // TODO: don't duplicate the code in CCodeMethodModule, we do this right now because it needs to be before return
3748 if (current_method
!= null && current_method
.get_attribute ("Profile") != null) {
3749 string prefix
= "_vala_prof_%s".printf (get_ccode_real_name (current_method
));
3751 var level
= new
CCodeIdentifier (prefix
+ "_level");
3752 ccode
.open_if (new
CCodeUnaryExpression (CCodeUnaryOperator
.LOGICAL_NEGATION
, new
CCodeUnaryExpression (CCodeUnaryOperator
.PREFIX_DECREMENT
, level
)));
3754 var timer
= new
CCodeIdentifier (prefix
+ "_timer");
3756 var stop_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_timer_stop"));
3757 stop_call
.add_argument (timer
);
3758 ccode
.add_expression (stop_call
);
3763 if (is_in_constructor ()) {
3764 ccode
.add_return (new
CCodeIdentifier ("obj"));
3765 } else if (is_in_destructor ()) {
3766 // do not call return as member cleanup and chain up to base finalizer
3767 // stil need to be executed
3768 ccode
.add_goto ("_return");
3769 } else if (is_in_coroutine ()) {
3770 } else if (current_method is CreationMethod
) {
3771 ccode
.add_return (new
CCodeIdentifier ("self"));
3772 } else if (current_return_type is VoidType
|| current_return_type
.is_real_non_null_struct_type ()) {
3773 // structs are returned via out parameter
3774 ccode
.add_return ();
3776 ccode
.add_return (new
CCodeIdentifier ("result"));
3779 if (return_expression_symbol
!= null) {
3780 return_expression_symbol
.active
= true;
3783 // required for destructors
3784 current_method_return
= true;
3787 public string get_symbol_lock_name (string symname
) {
3788 return "__lock_%s".printf (symname
);
3791 private CCodeExpression
get_lock_expression (Statement stmt
, Expression resource
) {
3792 CCodeExpression l
= null;
3793 var inner_node
= ((MemberAccess
)resource
).inner
;
3794 var member
= resource
.symbol_reference
;
3795 var parent
= (TypeSymbol
)resource
.symbol_reference
.parent_symbol
;
3797 if (member
.is_instance_member ()) {
3798 if (inner_node
== null) {
3799 l
= new
CCodeIdentifier ("self");
3800 } else if (resource
.symbol_reference
.parent_symbol
!= current_type_symbol
) {
3801 l
= generate_instance_cast (get_cvalue (inner_node
), parent
);
3803 l
= get_cvalue (inner_node
);
3806 l
= new CCodeMemberAccess
.pointer (new CCodeMemberAccess
.pointer (l
, "priv"), get_symbol_lock_name (resource
.symbol_reference
.name
));
3807 } else if (member
.is_class_member ()) {
3808 CCodeExpression klass
;
3810 if (get_this_type () != null) {
3811 var k
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_OBJECT_GET_CLASS"));
3812 k
.add_argument (new
CCodeIdentifier ("self"));
3815 klass
= new
CCodeIdentifier ("klass");
3818 var get_class_private_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf(get_ccode_upper_case_name (parent
))));
3819 get_class_private_call
.add_argument (klass
);
3820 l
= new CCodeMemberAccess
.pointer (get_class_private_call
, get_symbol_lock_name (resource
.symbol_reference
.name
));
3822 string lock_name
= "%s_%s".printf(get_ccode_lower_case_name (parent
), resource
.symbol_reference
.name
);
3823 l
= new
CCodeIdentifier (get_symbol_lock_name (lock_name
));
3828 public override void visit_lock_statement (LockStatement stmt
) {
3829 var l
= get_lock_expression (stmt
, stmt
.resource
);
3831 var fc
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_name (mutex_type
.scope
.lookup ("lock"))));
3832 fc
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, l
));
3834 ccode
.add_expression (fc
);
3837 public override void visit_unlock_statement (UnlockStatement stmt
) {
3838 var l
= get_lock_expression (stmt
, stmt
.resource
);
3840 var fc
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_name (mutex_type
.scope
.lookup ("unlock"))));
3841 fc
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, l
));
3843 ccode
.add_expression (fc
);
3846 public override void visit_delete_statement (DeleteStatement stmt
) {
3847 var pointer_type
= (PointerType
) stmt
.expression
.value_type
;
3848 DataType type
= pointer_type
;
3849 if (pointer_type
.base_type
.data_type
!= null && pointer_type
.base_type
.data_type
.is_reference_type ()) {
3850 type
= pointer_type
.base_type
;
3853 var ccall
= new
CCodeFunctionCall (get_destroy_func_expression (type
));
3854 ccall
.add_argument (get_cvalue (stmt
.expression
));
3855 ccode
.add_expression (ccall
);
3858 public override void visit_expression (Expression expr
) {
3859 if (get_cvalue (expr
) != null && !expr
.lvalue
) {
3860 if (expr
.formal_value_type is GenericType
&& !(expr
.value_type is GenericType
)) {
3861 var type_parameter
= ((GenericType
) expr
.formal_value_type
).type_parameter
;
3862 var st
= type_parameter
.parent_symbol
.parent_symbol as Struct
;
3863 if (type_parameter
.parent_symbol
!= garray_type
&&
3864 (st
== null || get_ccode_name (st
) != "va_list")) {
3865 // GArray and va_list don't use pointer-based generics
3866 set_cvalue (expr
, convert_from_generic_pointer (get_cvalue (expr
), expr
.value_type
));
3867 ((GLibValue
) expr
.target_value
).lvalue
= false;
3871 // memory management, implicit casts, and boxing/unboxing
3872 if (expr
.value_type
!= null) {
3873 // FIXME: temporary workaround until the refactoring is complete, not all target_value have a value_type
3874 expr
.target_value
.value_type
= expr
.value_type
;
3875 expr
.target_value
= transform_value (expr
.target_value
, expr
.target_type
, expr
);
3878 if (expr
.target_value
== null) {
3882 if (expr
.formal_target_type is GenericType
&& !(expr
.target_type is GenericType
)) {
3883 if (((GenericType
) expr
.formal_target_type
).type_parameter
.parent_symbol
!= garray_type
) {
3884 // GArray doesn't use pointer-based generics
3885 set_cvalue (expr
, convert_to_generic_pointer (get_cvalue (expr
), expr
.target_type
));
3886 ((GLibValue
) expr
.target_value
).lvalue
= false;
3890 if (!(expr
.value_type is ValueType
&& !expr
.value_type
.nullable
)) {
3891 ((GLibValue
) expr
.target_value
).non_null
= expr
.is_non_null ();
3896 public override void visit_boolean_literal (BooleanLiteral expr
) {
3897 set_cvalue (expr
, new
CCodeConstant (expr
.value ?
"TRUE" : "FALSE"));
3900 public override void visit_character_literal (CharacterLiteral expr
) {
3901 if (expr
.get_char () >= 0x20 && expr
.get_char () < 0x80) {
3902 set_cvalue (expr
, new
CCodeConstant (expr
.value
));
3904 set_cvalue (expr
, new
CCodeConstant ("%uU".printf (expr
.get_char ())));
3908 public override void visit_integer_literal (IntegerLiteral expr
) {
3909 set_cvalue (expr
, new
CCodeConstant (expr
.value
+ expr
.type_suffix
));
3912 public override void visit_real_literal (RealLiteral expr
) {
3913 string c_literal
= expr
.value
;
3914 if (c_literal
.has_suffix ("d") || c_literal
.has_suffix ("D")) {
3915 // there is no suffix for double in C
3916 c_literal
= c_literal
.substring (0, c_literal
.length
- 1);
3918 if (!("." in c_literal
|| "e" in c_literal
|| "E" in c_literal
)) {
3919 // C requires period or exponent part for floating constants
3920 if ("f" in c_literal
|| "F" in c_literal
) {
3921 c_literal
= c_literal
.substring (0, c_literal
.length
- 1) + ".f";
3926 set_cvalue (expr
, new
CCodeConstant (c_literal
));
3929 public override void visit_string_literal (StringLiteral expr
) {
3930 set_cvalue (expr
, new CCodeConstant
.string (expr
.value
.replace ("\n", "\\n")));
3932 if (expr
.translate
) {
3933 // translated string constant
3935 var m
= (Method
) root_symbol
.scope
.lookup ("GLib").scope
.lookup ("_");
3936 add_symbol_declaration (cfile
, m
, get_ccode_name (m
));
3938 var translate
= new
CCodeFunctionCall (new
CCodeIdentifier ("_"));
3939 translate
.add_argument (get_cvalue (expr
));
3940 set_cvalue (expr
, translate
);
3944 public override void visit_regex_literal (RegexLiteral expr
) {
3945 string[] parts
= expr
.value
.split ("/", 3);
3946 string re
= parts
[2].escape ("");
3949 if (parts
[1].contains ("i")) {
3950 flags
+= " | G_REGEX_CASELESS";
3952 if (parts
[1].contains ("m")) {
3953 flags
+= " | G_REGEX_MULTILINE";
3955 if (parts
[1].contains ("s")) {
3956 flags
+= " | G_REGEX_DOTALL";
3958 if (parts
[1].contains ("x")) {
3959 flags
+= " | G_REGEX_EXTENDED";
3962 var cdecl
= new
CCodeDeclaration ("GRegex*");
3964 var cname
= "_tmp_regex_%d".printf (next_regex_id
);
3965 if (this
.next_regex_id
== 0) {
3966 var fun
= new
CCodeFunction ("_thread_safe_regex_init", "GRegex*");
3967 fun
.modifiers
= CCodeModifiers
.STATIC
| CCodeModifiers
.INLINE
;
3968 fun
.add_parameter (new
CCodeParameter ("re", "GRegex**"));
3969 fun
.add_parameter (new
CCodeParameter ("pattern", "const gchar *"));
3970 fun
.add_parameter (new
CCodeParameter ("match_options", "GRegexMatchFlags"));
3972 push_function (fun
);
3974 var once_enter_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_once_init_enter"));
3975 once_enter_call
.add_argument (new
CCodeConstant ("(volatile gsize*) re"));
3976 ccode
.open_if (once_enter_call
);
3978 var regex_new_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_regex_new"));
3979 regex_new_call
.add_argument (new
CCodeConstant ("pattern"));
3980 regex_new_call
.add_argument (new
CCodeConstant ("match_options"));
3981 regex_new_call
.add_argument (new
CCodeConstant ("0"));
3982 regex_new_call
.add_argument (new
CCodeConstant ("NULL"));
3983 ccode
.add_assignment (new
CCodeIdentifier ("GRegex* val"), regex_new_call
);
3985 var once_leave_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_once_init_leave"));
3986 once_leave_call
.add_argument (new
CCodeConstant ("(volatile gsize*) re"));
3987 once_leave_call
.add_argument (new
CCodeConstant ("(gsize) val"));
3988 ccode
.add_expression (once_leave_call
);
3992 ccode
.add_return (new
CCodeIdentifier ("*re"));
3996 cfile
.add_function (fun
);
3998 this
.next_regex_id
++;
4000 cdecl
.add_declarator (new
CCodeVariableDeclarator (cname
+ " = NULL"));
4001 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
4003 var regex_const
= new
CCodeConstant ("_thread_safe_regex_init (&%s, \"%s\", %s)".printf (cname
, re
, flags
));
4005 cfile
.add_constant_declaration (cdecl
);
4006 set_cvalue (expr
, regex_const
);
4009 public override void visit_null_literal (NullLiteral expr
) {
4010 set_cvalue (expr
, new
CCodeConstant ("NULL"));
4012 var array_type
= expr
.target_type as ArrayType
;
4013 var delegate_type
= expr
.target_type as DelegateType
;
4014 if (array_type
!= null) {
4015 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4016 append_array_length (expr
, new
CCodeConstant ("0"));
4018 } else if (delegate_type
!= null && delegate_type
.delegate_symbol
.has_target
) {
4019 set_delegate_target (expr
, new
CCodeConstant ("NULL"));
4020 set_delegate_target_destroy_notify (expr
, new
CCodeConstant ("NULL"));
4024 public abstract TargetValue
get_local_cvalue (LocalVariable local
);
4026 public abstract TargetValue
get_parameter_cvalue (Parameter param
);
4028 public abstract TargetValue
get_field_cvalue (Field field
, TargetValue? instance
);
4030 public abstract TargetValue
load_variable (Variable variable
, TargetValue value
);
4032 public abstract TargetValue
load_this_parameter (TypeSymbol sym
);
4034 public abstract void store_value (TargetValue lvalue
, TargetValue value
, SourceReference? source_reference
= null);
4036 public virtual string get_delegate_target_cname (string delegate_cname
) {
4037 assert_not_reached ();
4040 public virtual CCodeExpression
get_delegate_target_cexpression (Expression delegate_expr
, out CCodeExpression delegate_target_destroy_notify
) {
4041 assert_not_reached ();
4044 public virtual CCodeExpression
get_delegate_target_cvalue (TargetValue value
) {
4045 return new
CCodeInvalidExpression ();
4048 public virtual CCodeExpression
get_delegate_target_destroy_notify_cvalue (TargetValue value
) {
4049 return new
CCodeInvalidExpression ();
4052 public virtual string get_delegate_target_destroy_notify_cname (string delegate_cname
) {
4053 assert_not_reached ();
4056 public override void visit_base_access (BaseAccess expr
) {
4057 CCodeExpression this_access
;
4058 if (is_in_coroutine ()) {
4060 this_access
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data_"), "self");
4062 this_access
= new
CCodeIdentifier ("self");
4065 set_cvalue (expr
, generate_instance_cast (this_access
, expr
.value_type
.data_type
));
4068 public override void visit_postfix_expression (PostfixExpression expr
) {
4069 MemberAccess ma
= find_property_access (expr
.inner
);
4071 // property postfix expression
4072 var prop
= (Property
) ma
.symbol_reference
;
4074 // increment/decrement property
4075 var op
= expr
.increment ? CCodeBinaryOperator
.PLUS
: CCodeBinaryOperator
.MINUS
;
4076 var cexpr
= new
CCodeBinaryExpression (op
, get_cvalue (expr
.inner
), new
CCodeConstant ("1"));
4077 store_property (prop
, ma
.inner
, new
GLibValue (expr
.value_type
, cexpr
));
4079 // return previous value
4080 expr
.target_value
= expr
.inner
.target_value
;
4084 // assign current value to temp variable
4085 var temp_value
= store_temp_value (expr
.inner
.target_value
, expr
);
4087 // increment/decrement variable
4088 var op
= expr
.increment ? CCodeBinaryOperator
.PLUS
: CCodeBinaryOperator
.MINUS
;
4089 var cexpr
= new
CCodeBinaryExpression (op
, get_cvalue_ (temp_value
), new
CCodeConstant ("1"));
4090 ccode
.add_assignment (get_cvalue (expr
.inner
), cexpr
);
4092 // return previous value
4093 expr
.target_value
= temp_value
;
4096 private MemberAccess?
find_property_access (Expression expr
) {
4097 if (!(expr is MemberAccess
)) {
4101 var ma
= (MemberAccess
) expr
;
4102 if (ma
.symbol_reference is Property
) {
4109 bool is_limited_generic_type (GenericType type
) {
4110 var cl
= type
.type_parameter
.parent_symbol as Class
;
4111 var st
= type
.type_parameter
.parent_symbol as Struct
;
4112 if ((cl
!= null && cl
.is_compact
) || st
!= null) {
4113 // compact classes and structs only
4114 // have very limited generics support
4120 public bool requires_copy (DataType type
) {
4121 if (!type
.is_disposable ()) {
4125 var cl
= type
.data_type as Class
;
4126 if (cl
!= null && is_reference_counting (cl
)
4127 && get_ccode_ref_function (cl
) == "") {
4128 // empty ref_function => no ref necessary
4132 if (type is GenericType
) {
4133 if (is_limited_generic_type ((GenericType
) type
)) {
4141 public bool requires_destroy (DataType type
) {
4142 if (!type
.is_disposable ()) {
4146 var array_type
= type as ArrayType
;
4147 if (array_type
!= null && array_type
.fixed_length
) {
4148 return requires_destroy (array_type
.element_type
);
4151 var cl
= type
.data_type as Class
;
4152 if (cl
!= null && is_reference_counting (cl
)
4153 && get_ccode_unref_function (cl
) == "") {
4154 // empty unref_function => no unref necessary
4158 if (type is GenericType
) {
4159 if (is_limited_generic_type ((GenericType
) type
)) {
4167 bool is_ref_function_void (DataType type
) {
4168 var cl
= type
.data_type as Class
;
4170 return get_ccode_ref_function_void (cl
);
4176 bool is_free_function_address_of (DataType type
) {
4177 var cl
= type
.data_type as Class
;
4179 return get_ccode_free_function_address_of (cl
);
4185 public virtual TargetValue?
copy_value (TargetValue value
, CodeNode node
) {
4186 var type
= value
.value_type
;
4187 var cexpr
= get_cvalue_ (value
);
4188 var result
= ((GLibValue
) value
).copy ();
4190 if (type is DelegateType
) {
4191 var delegate_type
= (DelegateType
) type
;
4192 if (delegate_type
.delegate_symbol
.has_target
&& !context
.deprecated
) {
4193 Report
.deprecated (node
.source_reference
, "copying delegates is not supported");
4195 result
.delegate_target_destroy_notify_cvalue
= new
CCodeConstant ("NULL");
4199 if (type is ValueType
&& !type
.nullable
) {
4200 // normal value type, no null check
4202 var temp_value
= create_temp_value (type
, true, node
, true);
4203 var ctemp
= get_cvalue_ (temp_value
);
4205 var vt
= (ValueType
) type
;
4206 var st
= (Struct
) vt
.type_symbol
;
4207 var copy_call
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_copy_function (st
)));
4208 copy_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
));
4209 copy_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, ctemp
));
4211 if (!get_ccode_has_copy_function (st
)) {
4212 generate_struct_copy_function (st
);
4215 if (gvalue_type
!= null && type
.data_type
== gvalue_type
) {
4216 var cisvalid
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_IS_VALUE"));
4217 cisvalid
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
));
4219 ccode
.open_if (cisvalid
);
4221 // GValue requires g_value_init in addition to g_value_copy
4222 var value_type_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_VALUE_TYPE"));
4223 value_type_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
));
4225 var init_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_value_init"));
4226 init_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, ctemp
));
4227 init_call
.add_argument (value_type_call
);
4228 ccode
.add_expression (init_call
);
4229 ccode
.add_expression (copy_call
);
4233 // g_value_init/copy must not be called for uninitialized values
4234 store_value (temp_value
, value
, node
.source_reference
);
4237 ccode
.add_expression (copy_call
);
4243 /* (temp = expr, temp == NULL ? NULL : ref (temp))
4245 * can be simplified to
4247 * if static type of expr is non-null
4250 var dupexpr
= get_dup_func_expression (type
, node
.source_reference
);
4252 if (dupexpr
== null) {
4257 if (dupexpr is CCodeIdentifier
&& !(type is ArrayType
) && !(type is GenericType
) && !is_ref_function_void (type
)) {
4258 // generate and call NULL-aware ref function to reduce number
4259 // of temporary variables and simplify code
4261 var dupid
= (CCodeIdentifier
) dupexpr
;
4262 string dup0_func
= "_%s0".printf (dupid
.name
);
4264 // g_strdup is already NULL-safe
4265 if (dupid
.name
== "g_strdup") {
4266 dup0_func
= dupid
.name
;
4267 } else if (add_wrapper (dup0_func
)) {
4268 string pointer_cname
= "gpointer";
4269 var dup0_fun
= new
CCodeFunction (dup0_func
, pointer_cname
);
4270 dup0_fun
.add_parameter (new
CCodeParameter ("self", pointer_cname
));
4271 dup0_fun
.modifiers
= CCodeModifiers
.STATIC
;
4273 push_function (dup0_fun
);
4275 var dup_call
= new
CCodeFunctionCall (dupexpr
);
4276 dup_call
.add_argument (new
CCodeIdentifier ("self"));
4278 ccode
.add_return (new
CCodeConditionalExpression (new
CCodeIdentifier ("self"), dup_call
, new
CCodeConstant ("NULL")));
4282 cfile
.add_function (dup0_fun
);
4285 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (dup0_func
));
4286 ccall
.add_argument (cexpr
);
4287 result
.cvalue
= ccall
;
4288 result
.value_type
.value_owned
= true;
4289 return store_temp_value (result
, node
);
4292 var ccall
= new
CCodeFunctionCall (dupexpr
);
4294 if (!(type is ArrayType
) && get_non_null (value
) && !is_ref_function_void (type
)) {
4295 // expression is non-null
4296 ccall
.add_argument (cexpr
);
4298 return store_temp_value (new
GLibValue (type
, ccall
), node
);
4300 var cnotnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, cexpr
, new
CCodeConstant ("NULL"));
4301 if (type is GenericType
) {
4302 // dup functions are optional for type parameters
4303 var cdupnotnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, get_dup_func_expression (type
, node
.source_reference
), new
CCodeConstant ("NULL"));
4304 cnotnull
= new
CCodeBinaryExpression (CCodeBinaryOperator
.AND
, cnotnull
, cdupnotnull
);
4307 if (type is GenericType
) {
4308 // cast from gconstpointer to gpointer as GBoxedCopyFunc expects gpointer
4309 ccall
.add_argument (new
CCodeCastExpression (cexpr
, "gpointer"));
4311 ccall
.add_argument (cexpr
);
4314 if (type is ArrayType
) {
4315 var array_type
= (ArrayType
) type
;
4316 ccall
.add_argument (get_array_length_cvalue (value
));
4318 if (array_type
.element_type is GenericType
) {
4319 var elem_dupexpr
= get_dup_func_expression (array_type
.element_type
, node
.source_reference
);
4320 if (elem_dupexpr
== null) {
4321 elem_dupexpr
= new
CCodeConstant ("NULL");
4323 ccall
.add_argument (elem_dupexpr
);
4327 CCodeExpression cifnull
;
4328 if (type
.data_type
!= null) {
4329 cifnull
= new
CCodeConstant ("NULL");
4331 // the value might be non-null even when the dup function is null,
4332 // so we may not just use NULL for type parameters
4334 // cast from gconstpointer to gpointer as methods in
4335 // generic classes may not return gconstpointer
4336 cifnull
= new
CCodeCastExpression (cexpr
, "gpointer");
4339 if (is_ref_function_void (type
)) {
4340 ccode
.open_if (cnotnull
);
4341 ccode
.add_expression (ccall
);
4344 var ccond
= new
CCodeConditionalExpression (cnotnull
, ccall
, cifnull
);
4345 result
.cvalue
= ccond
;
4346 result
= (GLibValue
) store_temp_value (result
, node
, true);
4352 bool is_reference_type_argument (DataType type_arg
) {
4353 if (type_arg is ErrorType
|| (type_arg
.data_type
!= null && type_arg
.data_type
.is_reference_type ())) {
4360 bool is_nullable_value_type_argument (DataType type_arg
) {
4361 if (type_arg is ValueType
&& type_arg
.nullable
) {
4368 bool is_signed_integer_type_argument (DataType type_arg
) {
4369 var st
= type_arg
.data_type as Struct
;
4370 if (type_arg is EnumValueType
) {
4372 } else if (type_arg
.nullable
) {
4374 } else if (st
== null) {
4376 } else if (st
.is_subtype_of (bool_type
.data_type
)) {
4378 } else if (st
.is_subtype_of (char_type
.data_type
)) {
4380 } else if (unichar_type
!= null && st
.is_subtype_of (unichar_type
.data_type
)) {
4382 } else if (st
.is_subtype_of (short_type
.data_type
)) {
4384 } else if (st
.is_subtype_of (int_type
.data_type
)) {
4386 } else if (st
.is_subtype_of (long_type
.data_type
)) {
4388 } else if (st
.is_subtype_of (int8_type
.data_type
)) {
4390 } else if (st
.is_subtype_of (int16_type
.data_type
)) {
4392 } else if (st
.is_subtype_of (int32_type
.data_type
)) {
4394 } else if (st
.is_subtype_of (gtype_type
)) {
4401 bool is_unsigned_integer_type_argument (DataType type_arg
) {
4402 var st
= type_arg
.data_type as Struct
;
4405 } else if (type_arg
.nullable
) {
4407 } else if (st
.is_subtype_of (uchar_type
.data_type
)) {
4409 } else if (st
.is_subtype_of (ushort_type
.data_type
)) {
4411 } else if (st
.is_subtype_of (uint_type
.data_type
)) {
4413 } else if (st
.is_subtype_of (ulong_type
.data_type
)) {
4415 } else if (st
.is_subtype_of (uint8_type
.data_type
)) {
4417 } else if (st
.is_subtype_of (uint16_type
.data_type
)) {
4419 } else if (st
.is_subtype_of (uint32_type
.data_type
)) {
4426 public void check_type (DataType type
) {
4427 var array_type
= type as ArrayType
;
4428 if (array_type
!= null) {
4429 check_type (array_type
.element_type
);
4430 if (array_type
.element_type is ArrayType
) {
4431 Report
.error (type
.source_reference
, "Stacked arrays are not supported");
4432 } else if (array_type
.element_type is DelegateType
) {
4433 var delegate_type
= (DelegateType
) array_type
.element_type
;
4434 if (delegate_type
.delegate_symbol
.has_target
) {
4435 Report
.error (type
.source_reference
, "Delegates with target are not supported as array element type");
4439 foreach (var type_arg
in type
.get_type_arguments ()) {
4440 check_type (type_arg
);
4441 check_type_argument (type_arg
);
4445 public void check_type_arguments (MemberAccess access
) {
4446 foreach (var type_arg
in access
.get_type_arguments ()) {
4447 check_type (type_arg
);
4448 check_type_argument (type_arg
);
4452 void check_type_argument (DataType type_arg
) {
4453 if (type_arg is GenericType
4454 || type_arg is PointerType
4455 || is_reference_type_argument (type_arg
)
4456 || is_nullable_value_type_argument (type_arg
)
4457 || is_signed_integer_type_argument (type_arg
)
4458 || is_unsigned_integer_type_argument (type_arg
)) {
4460 } else if (type_arg is DelegateType
) {
4461 var delegate_type
= (DelegateType
) type_arg
;
4462 if (delegate_type
.delegate_symbol
.has_target
) {
4463 Report
.error (type_arg
.source_reference
, "Delegates with target are not supported as generic type arguments");
4466 Report
.error (type_arg
.source_reference
, "`%s' is not a supported generic type argument, use `?' to box value types".printf (type_arg
.to_string ()));
4470 public virtual void generate_class_declaration (Class cl
, CCodeFile decl_space
) {
4471 if (add_symbol_declaration (decl_space
, cl
, get_ccode_name (cl
))) {
4476 public virtual void generate_interface_declaration (Interface iface
, CCodeFile decl_space
) {
4479 public virtual void generate_method_declaration (Method m
, CCodeFile decl_space
) {
4482 public virtual void generate_error_domain_declaration (ErrorDomain edomain
, CCodeFile decl_space
) {
4485 public void add_generic_type_arguments (Map
<int,CCodeExpression
> arg_map
, List
<DataType
> type_args
, CodeNode expr
, bool is_chainup
= false, List
<TypeParameter
>? type_parameters
= null) {
4486 int type_param_index
= 0;
4487 foreach (var type_arg
in type_args
) {
4488 if (type_parameters
!= null) {
4489 var type_param_name
= type_parameters
.get (type_param_index
).name
.down ();
4490 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.01), new
CCodeConstant ("\"%s_type\"".printf (type_param_name
)));
4491 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.03), new
CCodeConstant ("\"%s_dup_func\"".printf (type_param_name
)));
4492 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.05), new
CCodeConstant ("\"%s_destroy_func\"".printf (type_param_name
)));
4495 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.02), get_type_id_expression (type_arg
, is_chainup
));
4496 if (requires_copy (type_arg
)) {
4497 var dup_func
= get_dup_func_expression (type_arg
, type_arg
.source_reference
, is_chainup
);
4498 if (dup_func
== null) {
4499 // type doesn't contain a copy function
4503 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.04), new
CCodeCastExpression (dup_func
, "GBoxedCopyFunc"));
4504 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.06), new
CCodeCastExpression (get_destroy_func_expression (type_arg
, is_chainup
), "GDestroyNotify"));
4506 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.04), new
CCodeConstant ("NULL"));
4507 arg_map
.set (get_param_pos (0.1 * type_param_index
+ 0.06), new
CCodeConstant ("NULL"));
4513 public override void visit_object_creation_expression (ObjectCreationExpression expr
) {
4514 CCodeExpression instance
= null;
4515 CCodeExpression creation_expr
= null;
4517 check_type (expr
.type_reference
);
4519 var st
= expr
.type_reference
.data_type as Struct
;
4520 if ((st
!= null && (!st
.is_simple_type () || get_ccode_name (st
) == "va_list")) || expr
.get_object_initializer ().size
> 0) {
4521 // value-type initialization or object creation expression with object initializer
4523 var local
= expr
.parent_node as LocalVariable
;
4524 var field
= expr
.parent_node as Field
;
4525 var a
= expr
.parent_node as Assignment
;
4526 if (local
!= null && is_simple_struct_creation (local
, local
.initializer
)) {
4527 instance
= get_cvalue_ (get_local_cvalue (local
));
4528 } else if (field
!= null && is_simple_struct_creation (field
, field
.initializer
)) {
4529 // field initialization
4530 var thisparam
= load_this_parameter ((TypeSymbol
) field
.parent_symbol
);
4531 instance
= get_cvalue_ (get_field_cvalue (field
, thisparam
));
4532 } else if (a
!= null && a
.left
.symbol_reference is Variable
&& is_simple_struct_creation ((Variable
) a
.left
.symbol_reference
, a
.right
)) {
4533 if (requires_destroy (a
.left
.value_type
)) {
4534 /* unref old value */
4535 ccode
.add_expression (destroy_value (a
.left
.target_value
));
4538 local
= a
.left
.symbol_reference as LocalVariable
;
4539 field
= a
.left
.symbol_reference as Field
;
4540 var param
= a
.left
.symbol_reference as Parameter
;
4541 if (local
!= null) {
4542 instance
= get_cvalue_ (get_local_cvalue (local
));
4543 } else if (field
!= null) {
4544 var inner
= ((MemberAccess
) a
.left
).inner
;
4545 instance
= get_cvalue_ (get_field_cvalue (field
, inner
!= null ? inner
.target_value
: null));
4546 } else if (param
!= null) {
4547 instance
= get_cvalue_ (get_parameter_cvalue (param
));
4550 var temp_value
= create_temp_value (expr
.type_reference
, true, expr
);
4551 instance
= get_cvalue_ (temp_value
);
4555 if (expr
.symbol_reference
== null) {
4556 // no creation method
4557 if (expr
.type_reference
.data_type is Struct
) {
4558 // memset needs string.h
4559 cfile
.add_include ("string.h");
4560 var creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memset"));
4561 creation_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, instance
));
4562 creation_call
.add_argument (new
CCodeConstant ("0"));
4563 creation_call
.add_argument (new
CCodeIdentifier ("sizeof (%s)".printf (get_ccode_name (expr
.type_reference
))));
4565 creation_expr
= creation_call
;
4567 } else if (expr
.type_reference
.data_type
== glist_type
||
4568 expr
.type_reference
.data_type
== gslist_type
) {
4569 // NULL is an empty list
4570 set_cvalue (expr
, new
CCodeConstant ("NULL"));
4571 } else if (expr
.symbol_reference is Method
) {
4572 // use creation method
4573 var m
= (Method
) expr
.symbol_reference
;
4574 var params
= m
.get_parameters ();
4575 CCodeFunctionCall creation_call
;
4577 CCodeFunctionCall async_call
= null;
4578 CCodeFunctionCall finish_call
= null;
4580 generate_method_declaration (m
, cfile
);
4582 var cl
= expr
.type_reference
.data_type as Class
;
4584 if (!get_ccode_has_new_function (m
)) {
4585 // use construct function directly
4586 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_real_name (m
)));
4587 creation_call
.add_argument (new
CCodeIdentifier (get_ccode_type_id (cl
)));
4589 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_name (m
)));
4592 if ((st
!= null && !st
.is_simple_type ()) && !(get_ccode_instance_pos (m
) < 0)) {
4593 creation_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, instance
));
4594 } else if (st
!= null && get_ccode_name (st
) == "va_list") {
4595 creation_call
.add_argument (instance
);
4596 if (get_ccode_name (m
) == "va_start") {
4597 if (in_creation_method
) {
4598 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("va_copy"));
4599 creation_call
.add_argument (instance
);
4600 creation_call
.add_argument (new
CCodeIdentifier ("_vala_va_list"));
4602 Parameter last_param
= null;
4603 // FIXME: this doesn't take into account exception handling parameters
4604 foreach (var param
in current_method
.get_parameters ()) {
4605 if (param
.ellipsis
) {
4610 int nParams
= ccode
.get_parameter_count ();
4611 if (nParams
== 0 || !ccode
.get_parameter (nParams
- 1).ellipsis
) {
4612 Report
.error (expr
.source_reference
, "`va_list' used in method with fixed args");
4613 } else if (nParams
== 1) {
4614 Report
.error (expr
.source_reference
, "`va_list' used in method without parameter");
4616 creation_call
.add_argument (new
CCodeIdentifier (ccode
.get_parameter (nParams
- 2).name
));
4622 generate_type_declaration (expr
.type_reference
, cfile
);
4624 var in_arg_map
= new HashMap
<int,CCodeExpression
> (direct_hash
, direct_equal
);
4625 var out_arg_map
= in_arg_map
;
4627 if (m
!= null && m
.coroutine
) {
4630 async_call
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_name (m
)));
4631 finish_call
= new
CCodeFunctionCall (new
CCodeIdentifier (get_ccode_finish_name (m
)));
4633 creation_call
= finish_call
;
4635 // output arguments used separately
4636 out_arg_map
= new HashMap
<int,CCodeExpression
> (direct_hash
, direct_equal
);
4637 // pass GAsyncResult stored in closure to finish function
4638 out_arg_map
.set (get_param_pos (0.1), new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data_"), "_res_"));
4641 if (cl
!= null && !cl
.is_compact
) {
4642 add_generic_type_arguments (in_arg_map
, expr
.type_reference
.get_type_arguments (), expr
);
4643 } else if (cl
!= null && get_ccode_simple_generics (m
)) {
4644 int type_param_index
= 0;
4645 foreach (var type_arg
in expr
.type_reference
.get_type_arguments ()) {
4646 if (requires_copy (type_arg
)) {
4647 in_arg_map
.set (get_param_pos (-1 + 0.1 * type_param_index
+ 0.03), get_destroy0_func_expression (type_arg
));
4649 in_arg_map
.set (get_param_pos (-1 + 0.1 * type_param_index
+ 0.03), new
CCodeConstant ("NULL"));
4655 bool ellipsis
= false;
4659 Iterator
<Parameter
> params_it
= params
.iterator ();
4660 foreach (Expression arg
in expr
.get_argument_list ()) {
4661 CCodeExpression cexpr
= get_cvalue (arg
);
4663 var carg_map
= in_arg_map
;
4665 Parameter param
= null;
4666 if (params_it
.next ()) {
4667 param
= params_it
.get ();
4668 ellipsis
= param
.ellipsis
;
4670 if (param
.direction
== ParameterDirection
.OUT
) {
4671 carg_map
= out_arg_map
;
4674 // g_array_new: element size
4675 if (cl
== garray_type
&& param
.name
== "element_size") {
4676 var csizeof
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
4677 csizeof
.add_argument (new
CCodeIdentifier (get_ccode_name (expr
.type_reference
.get_type_arguments ().get (0))));
4681 if (get_ccode_array_length (param
) && param
.variable_type is ArrayType
) {
4682 var array_type
= (ArrayType
) param
.variable_type
;
4683 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
4684 carg_map
.set (get_param_pos (get_ccode_array_length_pos (param
) + 0.01 * dim
), get_array_length_cexpression (arg
, dim
));
4686 } else if (param
.variable_type is DelegateType
) {
4687 var deleg_type
= (DelegateType
) param
.variable_type
;
4688 var d
= deleg_type
.delegate_symbol
;
4690 CCodeExpression delegate_target_destroy_notify
;
4691 var delegate_target
= get_delegate_target_cexpression (arg
, out delegate_target_destroy_notify
);
4692 carg_map
.set (get_param_pos (get_ccode_delegate_target_pos (param
)), delegate_target
);
4693 if (deleg_type
.is_disposable ()) {
4694 carg_map
.set (get_param_pos (get_ccode_delegate_target_pos (param
) + 0.01), delegate_target_destroy_notify
);
4699 cexpr
= handle_struct_argument (param
, arg
, cexpr
);
4701 if (get_ccode_type (param
) != null) {
4702 cexpr
= new
CCodeCastExpression (cexpr
, get_ccode_type (param
));
4705 cexpr
= handle_struct_argument (null, arg
, cexpr
);
4708 arg_pos
= get_param_pos (get_ccode_pos (param
), ellipsis
);
4710 // default argument position
4711 cexpr
= handle_struct_argument (null, arg
, cexpr
);
4712 arg_pos
= get_param_pos (i
, ellipsis
);
4715 carg_map
.set (arg_pos
, cexpr
);
4719 if (params_it
.next ()) {
4720 var param
= params_it
.get ();
4722 /* if there are more parameters than arguments,
4723 * the additional parameter is an ellipsis parameter
4724 * otherwise there is a bug in the semantic analyzer
4726 assert (param
.params_array
|| param
.ellipsis
);
4730 if (expr
.tree_can_fail
) {
4732 current_method_inner_error
= true;
4733 // add &inner_error before the ellipsis arguments
4734 out_arg_map
.set (get_param_pos (-1), new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_variable_cexpression ("_inner_error_")));
4738 /* ensure variable argument list ends with NULL
4739 * except when using printf-style arguments */
4741 in_arg_map
.set (get_param_pos (-1, true), new
CCodeConstant ("NULL"));
4742 } else if (!m
.printf_format
&& !m
.scanf_format
&& get_ccode_sentinel (m
) != "") {
4743 in_arg_map
.set (get_param_pos (-1, true), new
CCodeConstant (get_ccode_sentinel (m
)));
4747 if ((st
!= null && !st
.is_simple_type ()) && get_ccode_instance_pos (m
) < 0) {
4748 // instance parameter is at the end in a struct creation method
4749 out_arg_map
.set (get_param_pos (-3), new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, instance
));
4752 if (m
!= null && m
.coroutine
) {
4753 if (expr
.is_yield_expression
) {
4754 // asynchronous call
4755 in_arg_map
.set (get_param_pos (-1), new
CCodeIdentifier (generate_ready_function (current_method
)));
4756 in_arg_map
.set (get_param_pos (-0.9), new
CCodeIdentifier ("_data_"));
4760 if (m
!= null && m
.parent_symbol is Class
) {
4761 if (get_ccode_finish_instance (m
)) {
4762 var tmp
= new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data_"), "_source_object_");
4763 out_arg_map
.set (get_param_pos (get_ccode_instance_pos (m
)), tmp
);
4767 // append C arguments in the right order
4772 if (async_call
!= creation_call
) {
4773 // don't append out arguments for .begin() calls
4777 foreach (int pos
in out_arg_map
.get_keys ()) {
4778 if (pos
> last_pos
&& (min_pos
== -1 || pos
< min_pos
)) {
4782 if (min_pos
== -1) {
4785 creation_call
.add_argument (out_arg_map
.get (min_pos
));
4790 if (async_call
!= null) {
4794 foreach (int pos
in in_arg_map
.get_keys ()) {
4795 if (pos
> last_pos
&& (min_pos
== -1 || pos
< min_pos
)) {
4799 if (min_pos
== -1) {
4802 async_call
.add_argument (in_arg_map
.get (min_pos
));
4807 if (expr
.is_yield_expression
) {
4808 // set state before calling async function to support immediate callbacks
4809 int state
= emit_context
.next_coroutine_state
++;
4811 ccode
.add_assignment (new CCodeMemberAccess
.pointer (new
CCodeIdentifier ("_data_"), "_state_"), new
CCodeConstant (state
.to_string ()));
4812 ccode
.add_expression (async_call
);
4813 ccode
.add_return (new
CCodeConstant ("FALSE"));
4814 ccode
.add_label ("_state_%d".printf (state
));
4817 creation_expr
= creation_call
;
4819 // cast the return value of the creation method back to the intended type if
4820 // it requested a special C return type
4821 if (get_ccode_type (m
) != null) {
4822 creation_expr
= new
CCodeCastExpression (creation_expr
, get_ccode_name (expr
.type_reference
));
4824 } else if (expr
.symbol_reference is ErrorCode
) {
4825 var ecode
= (ErrorCode
) expr
.symbol_reference
;
4826 var edomain
= (ErrorDomain
) ecode
.parent_symbol
;
4827 CCodeFunctionCall creation_call
;
4829 generate_error_domain_declaration (edomain
, cfile
);
4831 if (expr
.get_argument_list ().size
== 1) {
4832 // must not be a format argument
4833 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_error_new_literal"));
4835 creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_error_new"));
4837 creation_call
.add_argument (new
CCodeIdentifier (get_ccode_upper_case_name (edomain
)));
4838 creation_call
.add_argument (new
CCodeIdentifier (get_ccode_name (ecode
)));
4840 foreach (Expression arg
in expr
.get_argument_list ()) {
4841 creation_call
.add_argument (get_cvalue (arg
));
4844 creation_expr
= creation_call
;
4849 var local
= expr
.parent_node as LocalVariable
;
4850 if (local
!= null && is_simple_struct_creation (local
, local
.initializer
)) {
4851 // no temporary variable necessary
4852 ccode
.add_expression (creation_expr
);
4853 set_cvalue (expr
, instance
);
4854 } else if (instance
!= null) {
4855 if (expr
.type_reference
.data_type is Struct
) {
4856 ccode
.add_expression (creation_expr
);
4858 ccode
.add_assignment (instance
, creation_expr
);
4861 foreach (MemberInitializer init
in expr
.get_object_initializer ()) {
4862 if (init
.symbol_reference is Field
) {
4863 var f
= (Field
) init
.symbol_reference
;
4864 var instance_target_type
= get_data_type_for_symbol ((TypeSymbol
) f
.parent_symbol
);
4865 var typed_inst
= transform_value (new
GLibValue (expr
.type_reference
, instance
, true), instance_target_type
, init
);
4866 store_field (f
, typed_inst
, init
.initializer
.target_value
);
4868 var cl
= f
.parent_symbol as Class
;
4870 generate_class_struct_declaration (cl
, cfile
);
4872 } else if (init
.symbol_reference is Property
) {
4873 var inst_ma
= new MemberAccess
.simple ("new");
4874 inst_ma
.value_type
= expr
.type_reference
;
4875 set_cvalue (inst_ma
, instance
);
4876 store_property ((Property
) init
.symbol_reference
, inst_ma
, init
.initializer
.target_value
);
4877 // FIXME Do not ref/copy in the first place
4878 if (requires_destroy (init
.initializer
.target_value
.value_type
)) {
4879 ccode
.add_expression (destroy_value (init
.initializer
.target_value
));
4884 set_cvalue (expr
, instance
);
4885 } else if (creation_expr
!= null) {
4886 var temp_value
= create_temp_value (expr
.value_type
, false, expr
);
4887 ccode
.add_assignment (get_cvalue_ (temp_value
), creation_expr
);
4888 expr
.target_value
= temp_value
;
4890 if (context
.gobject_tracing
) {
4891 // GObject creation tracing enabled
4893 var cl
= expr
.type_reference
.data_type as Class
;
4894 if (cl
!= null && cl
.is_subtype_of (gobject_type
)) {
4897 // instance can be NULL in error cases
4898 ccode
.open_if (get_cvalue_ (expr
.target_value
));
4900 var set_data_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_object_set_data"));
4901 set_data_call
.add_argument (new
CCodeCastExpression (get_cvalue_ (expr
.target_value
), "GObject *"));
4902 set_data_call
.add_argument (new
CCodeConstant ("\"vala-creation-function\""));
4904 string func_name
= "";
4905 if (current_method
!= null) {
4906 func_name
= current_method
.get_full_name ();
4907 } else if (current_property_accessor
!= null) {
4908 func_name
= current_property_accessor
.get_full_name ();
4911 set_data_call
.add_argument (new
CCodeConstant ("\"%s\"".printf (func_name
)));
4913 ccode
.add_expression (set_data_call
);
4920 ((GLibValue
) expr
.target_value
).lvalue
= true;
4923 public CCodeExpression?
handle_struct_argument (Parameter? param
, Expression arg
, CCodeExpression? cexpr
) {
4925 if (param
!= null) {
4926 type
= param
.variable_type
;
4929 type
= arg
.value_type
;
4932 var unary
= arg as UnaryExpression
;
4933 // pass non-simple struct instances always by reference
4934 if (!(arg
.value_type is NullType
) && type
.is_real_struct_type ()) {
4935 // we already use a reference for arguments of ref, out, and nullable parameters
4936 if (!(unary
!= null && (unary
.operator
== UnaryOperator
.OUT
|| unary
.operator
== UnaryOperator
.REF
)) && !type
.nullable
) {
4937 if (cexpr is CCodeIdentifier
|| cexpr is CCodeMemberAccess
) {
4938 return new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
);
4940 // if cexpr is e.g. a function call, we can't take the address of the expression
4941 var temp_value
= create_temp_value (type
, false, arg
);
4942 ccode
.add_assignment (get_cvalue_ (temp_value
), cexpr
);
4943 return new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_cvalue_ (temp_value
));
4951 public override void visit_sizeof_expression (SizeofExpression expr
) {
4952 generate_type_declaration (expr
.type_reference
, cfile
);
4954 var csizeof
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
4955 csizeof
.add_argument (new
CCodeIdentifier (get_ccode_name (expr
.type_reference
)));
4956 set_cvalue (expr
, csizeof
);
4959 public override void visit_typeof_expression (TypeofExpression expr
) {
4960 set_cvalue (expr
, get_type_id_expression (expr
.type_reference
));
4963 public override void visit_unary_expression (UnaryExpression expr
) {
4964 if (expr
.operator
== UnaryOperator
.REF
|| expr
.operator
== UnaryOperator
.OUT
) {
4965 var glib_value
= (GLibValue
) expr
.inner
.target_value
;
4967 var ref_value
= new
GLibValue (glib_value
.value_type
);
4968 if (expr
.target_type
!= null && glib_value
.value_type
.is_real_struct_type () && glib_value
.value_type
.nullable
!= expr
.target_type
.nullable
) {
4969 // the only possibility is that value_type is nullable and target_type is non-nullable
4970 ref_value
.cvalue
= glib_value
.cvalue
;
4972 ref_value
.cvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.cvalue
);
4975 if (glib_value
.array_length_cvalues
!= null) {
4976 for (int i
= 0; i
< glib_value
.array_length_cvalues
.size
; i
++) {
4977 ref_value
.append_array_length_cvalue (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.array_length_cvalues
[i
]));
4981 if (glib_value
.delegate_target_cvalue
!= null) {
4982 ref_value
.delegate_target_cvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.delegate_target_cvalue
);
4984 if (glib_value
.delegate_target_destroy_notify_cvalue
!= null) {
4985 ref_value
.delegate_target_destroy_notify_cvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, glib_value
.delegate_target_destroy_notify_cvalue
);
4988 expr
.target_value
= ref_value
;
4992 CCodeUnaryOperator op
;
4993 if (expr
.operator
== UnaryOperator
.PLUS
) {
4994 op
= CCodeUnaryOperator
.PLUS
;
4995 } else if (expr
.operator
== UnaryOperator
.MINUS
) {
4996 op
= CCodeUnaryOperator
.MINUS
;
4997 } else if (expr
.operator
== UnaryOperator
.LOGICAL_NEGATION
) {
4998 op
= CCodeUnaryOperator
.LOGICAL_NEGATION
;
4999 } else if (expr
.operator
== UnaryOperator
.BITWISE_COMPLEMENT
) {
5000 op
= CCodeUnaryOperator
.BITWISE_COMPLEMENT
;
5001 } else if (expr
.operator
== UnaryOperator
.INCREMENT
) {
5002 op
= CCodeUnaryOperator
.PREFIX_INCREMENT
;
5003 } else if (expr
.operator
== UnaryOperator
.DECREMENT
) {
5004 op
= CCodeUnaryOperator
.PREFIX_DECREMENT
;
5006 assert_not_reached ();
5008 set_cvalue (expr
, new
CCodeUnaryExpression (op
, get_cvalue (expr
.inner
)));
5011 public CCodeExpression?
try_cast_value_to_type (CCodeExpression ccodeexpr
, DataType from
, DataType to
, Expression? expr
= null) {
5012 if (from
== null || gvalue_type
== null || from
.data_type
!= gvalue_type
|| to
.data_type
== gvalue_type
|| get_ccode_type_id (to
) == "") {
5016 // explicit conversion from GValue
5017 var ccall
= new
CCodeFunctionCall (get_value_getter_function (to
));
5018 CCodeExpression gvalue
;
5019 if (from
.nullable
) {
5022 gvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, ccodeexpr
);
5024 ccall
.add_argument (gvalue
);
5026 CCodeExpression rv
= ccall
;
5028 if (expr
!= null && to is ArrayType
) {
5029 // null-terminated string array
5030 var len_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strv_length"));
5031 len_call
.add_argument (rv
);
5032 append_array_length (expr
, len_call
);
5033 } else if (to is StructValueType
) {
5034 CodeNode node
= expr
!= null ?
(CodeNode
) expr
: to
;
5035 var temp_value
= create_temp_value (to
, true, node
, true);
5036 var ctemp
= get_cvalue_ (temp_value
);
5038 rv
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, new
CCodeCastExpression (rv
, get_ccode_name (new
PointerType (to
))));
5039 var holds
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_VALUE_HOLDS"));
5040 holds
.add_argument (gvalue
);
5041 holds
.add_argument (new
CCodeIdentifier (get_ccode_type_id (to
)));
5042 var cond
= new
CCodeBinaryExpression (CCodeBinaryOperator
.AND
, holds
, ccall
);
5043 var warn
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_warning"));
5044 warn
.add_argument (new
CCodeConstant ("\"Invalid GValue unboxing (wrong type or NULL)\""));
5045 var fail
= new
CCodeCommaExpression ();
5046 fail
.append_expression (warn
);
5047 fail
.append_expression (ctemp
);
5048 rv
= new
CCodeConditionalExpression (cond
, rv
, fail
);
5054 int next_variant_function_id
= 0;
5056 public TargetValue?
try_cast_variant_to_type (TargetValue value
, DataType to
, CodeNode? node
= null) {
5057 if (value
.value_type
== null || gvariant_type
== null || value
.value_type
.data_type
!= gvariant_type
) {
5061 string variant_func
= "_variant_get%d".printf (++next_variant_function_id
);
5063 var variant
= value
;
5064 if (value
.value_type
.value_owned
) {
5065 // value leaked, destroy it
5066 var temp_value
= store_temp_value (value
, node
);
5067 temp_ref_values
.insert (0, ((GLibValue
) temp_value
).copy ());
5068 variant
= temp_value
;
5071 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (variant_func
));
5072 ccall
.add_argument (get_cvalue_ (variant
));
5074 var result
= create_temp_value (to
, false, node
);
5076 var cfunc
= new
CCodeFunction (variant_func
);
5077 cfunc
.modifiers
= CCodeModifiers
.STATIC
;
5078 cfunc
.add_parameter (new
CCodeParameter ("value", "GVariant*"));
5080 if (!to
.is_real_non_null_struct_type ()) {
5081 cfunc
.return_type
= get_ccode_name (to
);
5084 if (to
.is_real_non_null_struct_type ()) {
5085 // structs are returned via out parameter
5086 cfunc
.add_parameter (new
CCodeParameter ("result", "%s *".printf (get_ccode_name (to
))));
5087 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_cvalue_ (result
)));
5088 } else if (to is ArrayType
) {
5089 // return array length if appropriate
5090 // tmp = _variant_get (variant, &tmp_length);
5091 var array_type
= (ArrayType
) to
;
5093 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5094 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_array_length_cvalue (result
, dim
)));
5095 cfunc
.add_parameter (new
CCodeParameter (get_array_length_cname ("result", dim
), "int*"));
5099 if (!to
.is_real_non_null_struct_type ()) {
5100 ccode
.add_assignment (get_cvalue_ (result
), ccall
);
5102 ccode
.add_expression (ccall
);
5105 push_function (cfunc
);
5107 CCodeExpression func_result
= deserialize_expression (to
, new
CCodeIdentifier ("value"), new
CCodeIdentifier ("*result"));
5108 if (to
.is_real_non_null_struct_type ()) {
5109 ccode
.add_assignment (new
CCodeIdentifier ("*result"), func_result
);
5111 ccode
.add_return (func_result
);
5116 cfile
.add_function_declaration (cfunc
);
5117 cfile
.add_function (cfunc
);
5119 return load_temp_value (result
);
5122 public virtual CCodeExpression?
deserialize_expression (DataType type
, CCodeExpression variant_expr
, CCodeExpression? expr
, CCodeExpression? error_expr
= null, out bool may_fail
= null) {
5123 assert_not_reached ();
5126 public virtual CCodeExpression?
serialize_expression (DataType type
, CCodeExpression expr
) {
5127 assert_not_reached ();
5130 public override void visit_cast_expression (CastExpression expr
) {
5131 generate_type_declaration (expr
.type_reference
, cfile
);
5133 if (!expr
.is_non_null_cast
) {
5134 var valuecast
= try_cast_value_to_type (get_cvalue (expr
.inner
), expr
.inner
.value_type
, expr
.type_reference
, expr
);
5135 if (valuecast
!= null) {
5136 set_cvalue (expr
, valuecast
);
5140 var variantcast
= try_cast_variant_to_type (expr
.inner
.target_value
, expr
.type_reference
, expr
);
5141 if (variantcast
!= null) {
5142 expr
.target_value
= variantcast
;
5147 var cl
= expr
.type_reference
.data_type as Class
;
5148 var iface
= expr
.type_reference
.data_type as Interface
;
5149 if (iface
!= null || (cl
!= null && !cl
.is_compact
)) {
5150 // checked cast for strict subtypes of GTypeInstance
5151 if (expr
.is_silent_cast
) {
5152 TargetValue to_cast
= expr
.inner
.target_value
;
5153 CCodeExpression cexpr
;
5154 if (!get_lvalue (to_cast
)) {
5155 to_cast
= store_temp_value (to_cast
, expr
);
5157 cexpr
= get_cvalue_ (to_cast
);
5158 var ccheck
= create_type_check (cexpr
, expr
.type_reference
);
5159 var ccast
= new
CCodeCastExpression (cexpr
, get_ccode_name (expr
.type_reference
));
5160 var cnull
= new
CCodeConstant ("NULL");
5161 var cast_value
= new
GLibValue (expr
.value_type
, new
CCodeConditionalExpression (ccheck
, ccast
, cnull
));
5162 if (requires_destroy (expr
.inner
.value_type
)) {
5163 var casted
= store_temp_value (cast_value
, expr
);
5164 ccode
.open_if (new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, get_cvalue_ (casted
), new
CCodeConstant ("NULL")));
5165 ccode
.add_expression (destroy_value (to_cast
));
5167 expr
.target_value
= ((GLibValue
) casted
).copy ();
5169 expr
.target_value
= cast_value
;
5172 set_cvalue (expr
, generate_instance_cast (get_cvalue (expr
.inner
), expr
.type_reference
.data_type
));
5175 if (expr
.is_silent_cast
) {
5176 set_cvalue (expr
, new
CCodeInvalidExpression ());
5178 Report
.error (expr
.source_reference
, "Operation not supported for this type");
5182 // recompute array length when casting to other array type
5183 var array_type
= expr
.type_reference as ArrayType
;
5184 if (array_type
!= null && expr
.inner
.value_type is ArrayType
) {
5185 if (array_type
.element_type is GenericType
|| ((ArrayType
) expr
.inner
.value_type
).element_type is GenericType
) {
5186 // element size unknown for generic arrays, retain array length as is
5187 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5188 append_array_length (expr
, get_array_length_cexpression (expr
.inner
, dim
));
5191 var sizeof_to
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
5192 sizeof_to
.add_argument (new
CCodeConstant (get_ccode_name (array_type
.element_type
)));
5194 var sizeof_from
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
5195 sizeof_from
.add_argument (new
CCodeConstant (get_ccode_name (((ArrayType
) expr
.inner
.value_type
).element_type
)));
5197 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5198 append_array_length (expr
, new
CCodeBinaryExpression (CCodeBinaryOperator
.DIV
, new
CCodeBinaryExpression (CCodeBinaryOperator
.MUL
, get_array_length_cexpression (expr
.inner
, dim
), sizeof_from
), sizeof_to
));
5201 } else if (array_type
!= null) {
5202 CCodeExpression array_length_expr
;
5204 var sizeof_to
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
5205 sizeof_to
.add_argument (new
CCodeConstant (get_ccode_name (array_type
.element_type
)));
5206 var sizeof_from
= new
CCodeFunctionCall (new
CCodeIdentifier ("sizeof"));
5208 var value_type
= expr
.inner
.value_type
;
5209 if (value_type is ValueType
) {
5210 var data_type
= ((ValueType
) value_type
).data_type
;
5211 sizeof_from
.add_argument (new
CCodeConstant (get_ccode_name (data_type
)));
5212 array_length_expr
= new
CCodeBinaryExpression (CCodeBinaryOperator
.DIV
, sizeof_from
, sizeof_to
);
5213 } else if (value_type is PointerType
&& ((PointerType
) value_type
).base_type is ValueType
) {
5214 var data_type
= ((ValueType
) (((PointerType
) value_type
).base_type
)).data_type
;
5215 sizeof_from
.add_argument (new
CCodeConstant (get_ccode_name (data_type
)));
5216 array_length_expr
= new
CCodeBinaryExpression (CCodeBinaryOperator
.DIV
, sizeof_from
, sizeof_to
);
5218 // cast from unsupported non-array to array, set invalid length
5219 // required by string.data, e.g.
5220 array_length_expr
= new
CCodeConstant ("-1");
5223 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5224 append_array_length (expr
, array_length_expr
);
5228 var innercexpr
= get_cvalue (expr
.inner
);
5229 if (expr
.type_reference is ValueType
&& !expr
.type_reference
.nullable
&&
5230 expr
.inner
.value_type is ValueType
&& expr
.inner
.value_type
.nullable
) {
5231 // nullable integer or float or boolean or struct or enum cast to non-nullable
5232 innercexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, innercexpr
);
5233 } else if (expr
.type_reference is ArrayType
5234 && expr
.inner
.value_type is ValueType
&& !expr
.inner
.value_type
.nullable
) {
5235 // integer or float or boolean or struct or enum to array cast
5236 innercexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, innercexpr
);
5238 set_cvalue (expr
, new
CCodeCastExpression (innercexpr
, get_ccode_name (expr
.type_reference
)));
5240 if (expr
.type_reference is DelegateType
) {
5241 if (get_delegate_target (expr
.inner
) != null) {
5242 set_delegate_target (expr
, get_delegate_target (expr
.inner
));
5244 set_delegate_target (expr
, new
CCodeConstant ("NULL"));
5246 if (get_delegate_target_destroy_notify (expr
.inner
) != null) {
5247 set_delegate_target_destroy_notify (expr
, get_delegate_target_destroy_notify (expr
.inner
));
5249 set_delegate_target_destroy_notify (expr
, new
CCodeConstant ("NULL"));
5255 public override void visit_named_argument (NamedArgument expr
) {
5256 set_cvalue (expr
, get_cvalue (expr
.inner
));
5259 public override void visit_pointer_indirection (PointerIndirection expr
) {
5260 set_cvalue (expr
, new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, get_cvalue (expr
.inner
)));
5261 ((GLibValue
) expr
.target_value
).lvalue
= get_lvalue (expr
.inner
.target_value
);
5264 public override void visit_addressof_expression (AddressofExpression expr
) {
5265 set_cvalue (expr
, new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_cvalue (expr
.inner
)));
5268 public override void visit_reference_transfer_expression (ReferenceTransferExpression expr
) {
5269 /* tmp = expr.inner; expr.inner = NULL; expr = tmp; */
5270 expr
.target_value
= store_temp_value (expr
.inner
.target_value
, expr
);
5272 if (expr
.inner
.value_type is StructValueType
&& !expr
.inner
.value_type
.nullable
) {
5273 // memset needs string.h
5274 cfile
.add_include ("string.h");
5275 var creation_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("memset"));
5276 creation_call
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_cvalue (expr
.inner
)));
5277 creation_call
.add_argument (new
CCodeConstant ("0"));
5278 creation_call
.add_argument (new
CCodeIdentifier ("sizeof (%s)".printf (get_ccode_name (expr
.inner
.value_type
))));
5279 ccode
.add_expression (creation_call
);
5280 } else if (expr
.value_type is DelegateType
) {
5281 ccode
.add_assignment (get_cvalue (expr
.inner
), new
CCodeConstant ("NULL"));
5282 var target
= get_delegate_target_cvalue (expr
.inner
.target_value
);
5283 if (target
!= null) {
5284 ccode
.add_assignment (target
, new
CCodeConstant ("NULL"));
5286 var target_destroy_notify
= get_delegate_target_destroy_notify_cvalue (expr
.inner
.target_value
);
5287 if (target_destroy_notify
!= null) {
5288 ccode
.add_assignment (target_destroy_notify
, new
CCodeConstant ("NULL"));
5290 } else if (expr
.inner
.value_type is ArrayType
) {
5291 var array_type
= (ArrayType
) expr
.inner
.value_type
;
5292 var glib_value
= (GLibValue
) expr
.inner
.target_value
;
5294 ccode
.add_assignment (get_cvalue (expr
.inner
), new
CCodeConstant ("NULL"));
5295 if (glib_value
.array_length_cvalues
!= null) {
5296 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5297 ccode
.add_assignment (get_array_length_cvalue (glib_value
, dim
), new
CCodeConstant ("0"));
5301 ccode
.add_assignment (get_cvalue (expr
.inner
), new
CCodeConstant ("NULL"));
5305 public override void visit_binary_expression (BinaryExpression expr
) {
5306 var cleft
= get_cvalue (expr
.left
);
5307 var cright
= get_cvalue (expr
.right
);
5309 CCodeExpression? left_chain
= null;
5310 if (expr
.is_chained
) {
5311 var lbe
= (BinaryExpression
) expr
.left
;
5313 var temp_decl
= get_temp_variable (lbe
.right
.target_type
, true, null, false);
5314 emit_temp_var (temp_decl
);
5315 var cvar
= get_variable_cexpression (temp_decl
.name
);
5316 var clbe
= (CCodeBinaryExpression
) get_cvalue (lbe
);
5317 if (lbe
.is_chained
) {
5318 clbe
= (CCodeBinaryExpression
) clbe
.right
;
5320 ccode
.add_assignment (cvar
, get_cvalue (lbe
.right
));
5321 clbe
.right
= get_variable_cexpression (temp_decl
.name
);
5326 CCodeBinaryOperator op
;
5327 if (expr
.operator
== BinaryOperator
.PLUS
) {
5328 op
= CCodeBinaryOperator
.PLUS
;
5329 } else if (expr
.operator
== BinaryOperator
.MINUS
) {
5330 op
= CCodeBinaryOperator
.MINUS
;
5331 } else if (expr
.operator
== BinaryOperator
.MUL
) {
5332 op
= CCodeBinaryOperator
.MUL
;
5333 } else if (expr
.operator
== BinaryOperator
.DIV
) {
5334 op
= CCodeBinaryOperator
.DIV
;
5335 } else if (expr
.operator
== BinaryOperator
.MOD
) {
5336 if (expr
.value_type
.equals (double_type
)) {
5337 cfile
.add_include ("math.h");
5338 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("fmod"));
5339 ccall
.add_argument (cleft
);
5340 ccall
.add_argument (cright
);
5341 set_cvalue (expr
, ccall
);
5343 } else if (expr
.value_type
.equals (float_type
)) {
5344 cfile
.add_include ("math.h");
5345 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("fmodf"));
5346 ccall
.add_argument (cleft
);
5347 ccall
.add_argument (cright
);
5348 set_cvalue (expr
, ccall
);
5351 op
= CCodeBinaryOperator
.MOD
;
5353 } else if (expr
.operator
== BinaryOperator
.SHIFT_LEFT
) {
5354 op
= CCodeBinaryOperator
.SHIFT_LEFT
;
5355 } else if (expr
.operator
== BinaryOperator
.SHIFT_RIGHT
) {
5356 op
= CCodeBinaryOperator
.SHIFT_RIGHT
;
5357 } else if (expr
.operator
== BinaryOperator
.LESS_THAN
) {
5358 op
= CCodeBinaryOperator
.LESS_THAN
;
5359 } else if (expr
.operator
== BinaryOperator
.GREATER_THAN
) {
5360 op
= CCodeBinaryOperator
.GREATER_THAN
;
5361 } else if (expr
.operator
== BinaryOperator
.LESS_THAN_OR_EQUAL
) {
5362 op
= CCodeBinaryOperator
.LESS_THAN_OR_EQUAL
;
5363 } else if (expr
.operator
== BinaryOperator
.GREATER_THAN_OR_EQUAL
) {
5364 op
= CCodeBinaryOperator
.GREATER_THAN_OR_EQUAL
;
5365 } else if (expr
.operator
== BinaryOperator
.EQUALITY
) {
5366 op
= CCodeBinaryOperator
.EQUALITY
;
5367 } else if (expr
.operator
== BinaryOperator
.INEQUALITY
) {
5368 op
= CCodeBinaryOperator
.INEQUALITY
;
5369 } else if (expr
.operator
== BinaryOperator
.BITWISE_AND
) {
5370 op
= CCodeBinaryOperator
.BITWISE_AND
;
5371 } else if (expr
.operator
== BinaryOperator
.BITWISE_OR
) {
5372 op
= CCodeBinaryOperator
.BITWISE_OR
;
5373 } else if (expr
.operator
== BinaryOperator
.BITWISE_XOR
) {
5374 op
= CCodeBinaryOperator
.BITWISE_XOR
;
5375 } else if (expr
.operator
== BinaryOperator
.AND
) {
5376 op
= CCodeBinaryOperator
.AND
;
5377 } else if (expr
.operator
== BinaryOperator
.OR
) {
5378 op
= CCodeBinaryOperator
.OR
;
5379 } else if (expr
.operator
== BinaryOperator
.IN
) {
5380 if (expr
.right
.value_type is ArrayType
) {
5381 var array_type
= (ArrayType
) expr
.right
.value_type
;
5382 var node
= new
CCodeFunctionCall (new
CCodeIdentifier (generate_array_contains_wrapper (array_type
)));
5383 node
.add_argument (cright
);
5384 node
.add_argument (get_array_length_cexpression (expr
.right
));
5385 if (array_type
.element_type is StructValueType
) {
5386 node
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cleft
));
5388 node
.add_argument (cleft
);
5390 set_cvalue (expr
, node
);
5392 set_cvalue (expr
, new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeBinaryExpression (CCodeBinaryOperator
.BITWISE_AND
, cright
, cleft
), cleft
));
5396 assert_not_reached ();
5399 if (expr
.operator
== BinaryOperator
.EQUALITY
||
5400 expr
.operator
== BinaryOperator
.INEQUALITY
) {
5401 var left_type
= expr
.left
.target_type
;
5402 var right_type
= expr
.right
.target_type
;
5403 make_comparable_cexpression (ref left_type
, ref cleft
, ref right_type
, ref cright
);
5405 if (left_type is StructValueType
&& right_type is StructValueType
) {
5406 var equalfunc
= generate_struct_equal_function ((Struct
) left_type
.data_type
);
5407 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
5408 ccall
.add_argument (cleft
);
5409 ccall
.add_argument (cright
);
5411 cright
= new
CCodeConstant ("TRUE");
5412 } else if ((left_type is IntegerType
|| left_type is FloatingType
|| left_type is BooleanType
|| left_type is EnumValueType
) && left_type
.nullable
&&
5413 (right_type is IntegerType
|| right_type is FloatingType
|| right_type is BooleanType
|| right_type is EnumValueType
) && right_type
.nullable
) {
5414 var equalfunc
= generate_numeric_equal_function ((TypeSymbol
) left_type
.data_type
);
5415 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
5416 ccall
.add_argument (cleft
);
5417 ccall
.add_argument (cright
);
5419 cright
= new
CCodeConstant ("TRUE");
5423 if (!(expr
.left
.value_type is NullType
)
5424 && expr
.left
.value_type
.compatible (string_type
)
5425 && !(expr
.right
.value_type is NullType
)
5426 && expr
.right
.value_type
.compatible (string_type
)) {
5427 if (expr
.operator
== BinaryOperator
.PLUS
) {
5428 // string concatenation
5429 if (expr
.left
.is_constant () && expr
.right
.is_constant ()) {
5432 if (cleft is CCodeIdentifier
) {
5433 left
= ((CCodeIdentifier
) cleft
).name
;
5434 } else if (cleft is CCodeConstant
) {
5435 left
= ((CCodeConstant
) cleft
).name
;
5437 assert_not_reached ();
5439 if (cright is CCodeIdentifier
) {
5440 right
= ((CCodeIdentifier
) cright
).name
;
5441 } else if (cright is CCodeConstant
) {
5442 right
= ((CCodeConstant
) cright
).name
;
5444 assert_not_reached ();
5447 set_cvalue (expr
, new
CCodeConstant ("%s %s".printf (left
, right
)));
5450 // convert to g_strconcat (a, b, NULL)
5451 var temp_value
= create_temp_value (expr
.value_type
, false, expr
);
5453 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strconcat"));
5454 ccall
.add_argument (cleft
);
5455 ccall
.add_argument (cright
);
5456 ccall
.add_argument (new
CCodeConstant("NULL"));
5458 ccode
.add_assignment (get_cvalue_ (temp_value
), ccall
);
5459 expr
.target_value
= temp_value
;
5462 } else if (expr
.operator
== BinaryOperator
.EQUALITY
5463 || expr
.operator
== BinaryOperator
.INEQUALITY
5464 || expr
.operator
== BinaryOperator
.LESS_THAN
5465 || expr
.operator
== BinaryOperator
.GREATER_THAN
5466 || expr
.operator
== BinaryOperator
.LESS_THAN_OR_EQUAL
5467 || expr
.operator
== BinaryOperator
.GREATER_THAN_OR_EQUAL
) {
5468 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strcmp0"));
5469 ccall
.add_argument (cleft
);
5470 ccall
.add_argument (cright
);
5472 cright
= new
CCodeConstant ("0");
5476 set_cvalue (expr
, new
CCodeBinaryExpression (op
, cleft
, cright
));
5477 if (left_chain
!= null) {
5478 set_cvalue (expr
, new
CCodeBinaryExpression (CCodeBinaryOperator
.AND
, left_chain
, get_cvalue (expr
)));
5482 CCodeExpression?
create_type_check (CCodeNode ccodenode
, DataType type
) {
5483 var et
= type as ErrorType
;
5484 if (et
!= null && et
.error_code
!= null) {
5485 var matches_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_error_matches"));
5486 matches_call
.add_argument ((CCodeExpression
) ccodenode
);
5487 matches_call
.add_argument (new
CCodeIdentifier (get_ccode_upper_case_name (et
.error_domain
)));
5488 matches_call
.add_argument (new
CCodeIdentifier (get_ccode_name (et
.error_code
)));
5489 return matches_call
;
5490 } else if (et
!= null && et
.error_domain
!= null) {
5491 var instance_domain
= new CCodeMemberAccess
.pointer ((CCodeExpression
) ccodenode
, "domain");
5492 var type_domain
= new
CCodeIdentifier (get_ccode_upper_case_name (et
.error_domain
));
5493 return new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, instance_domain
, type_domain
);
5495 var type_id
= get_type_id_expression (type
);
5496 if (type_id
== null) {
5497 return new
CCodeInvalidExpression ();
5499 var ccheck
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_TYPE_CHECK_INSTANCE_TYPE"));
5500 ccheck
.add_argument ((CCodeExpression
) ccodenode
);
5501 ccheck
.add_argument (type_id
);
5506 string generate_array_contains_wrapper (ArrayType array_type
) {
5507 string array_contains_func
= "_vala_%s_array_contains".printf (get_ccode_lower_case_name (array_type
.element_type
));
5509 if (!add_wrapper (array_contains_func
)) {
5510 return array_contains_func
;
5513 var function
= new
CCodeFunction (array_contains_func
, "gboolean");
5514 function
.modifiers
= CCodeModifiers
.STATIC
;
5516 function
.add_parameter (new
CCodeParameter ("stack", "%s *".printf (get_ccode_name (array_type
.element_type
))));
5517 function
.add_parameter (new
CCodeParameter ("stack_length", "int"));
5518 if (array_type
.element_type is StructValueType
) {
5519 function
.add_parameter (new
CCodeParameter ("needle", "%s *".printf (get_ccode_name (array_type
.element_type
))));
5521 function
.add_parameter (new
CCodeParameter ("needle", get_ccode_name (array_type
.element_type
)));
5524 push_function (function
);
5526 ccode
.add_declaration ("int", new
CCodeVariableDeclarator ("i"));
5528 var cloop_initializer
= new
CCodeAssignment (new
CCodeIdentifier ("i"), new
CCodeConstant ("0"));
5529 var cloop_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.LESS_THAN
, new
CCodeIdentifier ("i"), new
CCodeIdentifier ("stack_length"));
5530 var cloop_iterator
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POSTFIX_INCREMENT
, new
CCodeIdentifier ("i"));
5531 ccode
.open_for (cloop_initializer
, cloop_condition
, cloop_iterator
);
5533 var celement
= new
CCodeElementAccess (new
CCodeIdentifier ("stack"), new
CCodeIdentifier ("i"));
5534 var cneedle
= new
CCodeIdentifier ("needle");
5535 CCodeBinaryExpression cif_condition
;
5536 if (array_type
.element_type
.compatible (string_type
)) {
5537 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_strcmp0"));
5538 ccall
.add_argument (celement
);
5539 ccall
.add_argument (cneedle
);
5540 cif_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, ccall
, new
CCodeConstant ("0"));
5541 } else if (array_type
.element_type is StructValueType
) {
5542 var equalfunc
= generate_struct_equal_function ((Struct
) array_type
.element_type
.data_type
);
5543 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (equalfunc
));
5544 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, celement
));
5545 ccall
.add_argument (cneedle
);
5546 cif_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, ccall
, new
CCodeConstant ("TRUE"));
5548 cif_condition
= new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, cneedle
, celement
);
5551 ccode
.open_if (cif_condition
);
5552 ccode
.add_return (new
CCodeConstant ("TRUE"));
5557 ccode
.add_return (new
CCodeConstant ("FALSE"));
5561 cfile
.add_function_declaration (function
);
5562 cfile
.add_function (function
);
5564 return array_contains_func
;
5567 public override void visit_type_check (TypeCheck expr
) {
5568 generate_type_declaration (expr
.type_reference
, cfile
);
5570 var type
= expr
.expression
.value_type
;
5571 var pointer_type
= type as PointerType
;
5572 if (pointer_type
!= null) {
5573 type
= pointer_type
.base_type
;
5575 var cl
= type
.data_type as Class
;
5576 var iface
= type
.data_type as Interface
;
5577 if ((cl
!= null && !cl
.is_compact
) || iface
!= null || type is GenericType
|| type is ErrorType
) {
5578 set_cvalue (expr
, create_type_check (get_cvalue (expr
.expression
), expr
.type_reference
));
5580 set_cvalue (expr
, new
CCodeInvalidExpression ());
5583 if (get_cvalue (expr
) is CCodeInvalidExpression
) {
5584 Report
.error (expr
.source_reference
, "type check expressions not supported for compact classes, structs, and enums");
5588 public override void visit_lambda_expression (LambdaExpression lambda
) {
5589 var delegate_type
= (DelegateType
) lambda
.target_type
;
5590 var d
= delegate_type
.delegate_symbol
;
5592 lambda
.method
.set_attribute_bool ("CCode", "array_length", get_ccode_array_length (d
));
5593 lambda
.method
.set_attribute_bool ("CCode", "array_null_terminated", get_ccode_array_null_terminated (d
));
5594 lambda
.method
.set_attribute_string ("CCode", "array_length_type", get_ccode_array_length_type (d
));
5596 lambda
.accept_children (this
);
5598 bool expr_owned
= lambda
.value_type
.value_owned
;
5600 set_cvalue (lambda
, new
CCodeIdentifier (get_ccode_name (lambda
.method
)));
5602 if (lambda
.method
.closure
) {
5603 int block_id
= get_block_id (current_closure_block
);
5604 var delegate_target
= get_variable_cexpression ("_data%d_".printf (block_id
));
5605 if (expr_owned
|| delegate_type
.is_called_once
) {
5606 var ref_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("block%d_data_ref".printf (block_id
)));
5607 ref_call
.add_argument (delegate_target
);
5608 delegate_target
= ref_call
;
5609 set_delegate_target_destroy_notify (lambda
, new
CCodeIdentifier ("block%d_data_unref".printf (block_id
)));
5611 set_delegate_target_destroy_notify (lambda
, new
CCodeConstant ("NULL"));
5613 set_delegate_target (lambda
, delegate_target
);
5614 } else if (get_this_type () != null) {
5615 CCodeExpression delegate_target
= get_result_cexpression ("self");
5616 delegate_target
= convert_to_generic_pointer (delegate_target
, get_this_type ());
5617 if (expr_owned
|| delegate_type
.is_called_once
) {
5618 var ref_call
= new
CCodeFunctionCall (get_dup_func_expression (get_this_type (), lambda
.source_reference
));
5619 ref_call
.add_argument (delegate_target
);
5620 delegate_target
= ref_call
;
5621 set_delegate_target_destroy_notify (lambda
, get_destroy_func_expression (get_this_type ()));
5623 set_delegate_target_destroy_notify (lambda
, new
CCodeConstant ("NULL"));
5625 set_delegate_target (lambda
, delegate_target
);
5627 set_delegate_target (lambda
, new
CCodeConstant ("NULL"));
5628 set_delegate_target_destroy_notify (lambda
, new
CCodeConstant ("NULL"));
5632 public CCodeExpression
convert_from_generic_pointer (CCodeExpression cexpr
, DataType actual_type
) {
5634 if (is_reference_type_argument (actual_type
) || is_nullable_value_type_argument (actual_type
)) {
5635 result
= new
CCodeCastExpression (cexpr
, get_ccode_name (actual_type
));
5636 } else if (is_signed_integer_type_argument (actual_type
)) {
5637 result
= new
CCodeCastExpression (new
CCodeCastExpression (cexpr
, "gintptr"), get_ccode_name (actual_type
));
5638 } else if (is_unsigned_integer_type_argument (actual_type
)) {
5639 result
= new
CCodeCastExpression (new
CCodeCastExpression (cexpr
, "guintptr"), get_ccode_name (actual_type
));
5644 public CCodeExpression
convert_to_generic_pointer (CCodeExpression cexpr
, DataType actual_type
) {
5646 if (is_signed_integer_type_argument (actual_type
)) {
5647 result
= new
CCodeCastExpression (new
CCodeCastExpression (cexpr
, "gintptr"), "gpointer");
5648 } else if (is_unsigned_integer_type_argument (actual_type
)) {
5649 result
= new
CCodeCastExpression (new
CCodeCastExpression (cexpr
, "guintptr"), "gpointer");
5654 public TargetValue
transform_value (TargetValue value
, DataType? target_type
, CodeNode node
) {
5655 var type
= value
.value_type
;
5656 var result
= ((GLibValue
) value
).copy ();
5658 if (type
.value_owned
5659 && type
.floating_reference
) {
5660 /* floating reference, sink it.
5662 var cl
= type
.data_type as ObjectTypeSymbol
;
5663 var sink_func
= (cl
!= null) ?
get_ccode_ref_sink_function (cl
) : "";
5665 if (sink_func
!= "") {
5666 if (type
.nullable
) {
5667 var is_not_null
= new
CCodeBinaryExpression (CCodeBinaryOperator
.INEQUALITY
, result
.cvalue
, new
CCodeIdentifier ("NULL"));
5668 ccode
.open_if (is_not_null
);
5671 var csink
= new
CCodeFunctionCall (new
CCodeIdentifier (sink_func
));
5672 csink
.add_argument (result
.cvalue
);
5673 ccode
.add_expression (csink
);
5675 if (type
.nullable
) {
5679 Report
.error (null, "type `%s' does not support floating references".printf (type
.data_type
.name
));
5683 bool boxing
= (type is ValueType
&& !type
.nullable
5684 && target_type is ValueType
&& target_type
.nullable
);
5685 bool unboxing
= (type is ValueType
&& type
.nullable
5686 && target_type is ValueType
&& !target_type
.nullable
);
5688 bool gvalue_boxing
= (target_type
!= null
5689 && target_type
.data_type
== gvalue_type
5690 && !(type is NullType
)
5691 && get_ccode_type_id (type
) != "G_TYPE_VALUE");
5692 bool gvariant_boxing
= (target_type
!= null
5693 && target_type
.data_type
== gvariant_type
5694 && !(type is NullType
)
5695 && type
.data_type
!= gvariant_type
);
5697 if (type
.value_owned
5698 && (target_type
== null || !target_type
.value_owned
|| boxing
|| unboxing
|| gvariant_boxing
)
5699 && !gvalue_boxing
/* gvalue can assume ownership of value, no need to free it */) {
5700 // value leaked, destroy it
5701 if (target_type is PointerType
) {
5702 // manual memory management for pointers
5703 } else if (requires_destroy (type
)) {
5704 if (!is_lvalue_access_allowed (type
)) {
5705 // cannot assign to a temporary variable
5706 temp_ref_values
.insert (0, result
.copy ());
5708 var temp_value
= create_temp_value (type
, false, node
);
5709 temp_ref_values
.insert (0, ((GLibValue
) temp_value
).copy ());
5710 store_value (temp_value
, result
, node
.source_reference
);
5711 result
.cvalue
= get_cvalue_ (temp_value
);
5716 if (target_type
== null) {
5717 // value will be destroyed, no need for implicit casts
5721 result
.value_type
= target_type
.copy ();
5723 if (gvalue_boxing
) {
5724 // implicit conversion to GValue
5725 var temp_value
= create_temp_value (target_type
, true, node
, true);
5727 if (!target_type
.value_owned
) {
5728 // boxed GValue leaked, destroy it
5729 temp_ref_values
.insert (0, ((GLibValue
) temp_value
).copy ());
5732 if (target_type
.nullable
) {
5733 var newcall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_new0"));
5734 newcall
.add_argument (new
CCodeConstant ("GValue"));
5735 newcall
.add_argument (new
CCodeConstant ("1"));
5736 var newassignment
= new
CCodeAssignment (get_cvalue_ (temp_value
), newcall
);
5737 ccode
.add_expression (newassignment
);
5740 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_value_init"));
5741 if (target_type
.nullable
) {
5742 ccall
.add_argument (get_cvalue_ (temp_value
));
5744 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_cvalue_ (temp_value
)));
5746 var type_id
= get_ccode_type_id (type
);
5747 if (type_id
== "") {
5748 Report
.error (node
.source_reference
, "GValue boxing of type `%s' is not supported".printf (type
.to_string ()));
5750 ccall
.add_argument (new
CCodeIdentifier (type_id
));
5751 ccode
.add_expression (ccall
);
5753 if (requires_destroy (type
)) {
5754 ccall
= new
CCodeFunctionCall (get_value_taker_function (type
));
5756 ccall
= new
CCodeFunctionCall (get_value_setter_function (type
));
5758 if (target_type
.nullable
) {
5759 ccall
.add_argument (get_cvalue_ (temp_value
));
5761 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_cvalue_ (temp_value
)));
5763 if (type
.is_real_non_null_struct_type ()) {
5764 ccall
.add_argument (new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, result
.cvalue
));
5766 ccall
.add_argument (result
.cvalue
);
5769 ccode
.add_expression (ccall
);
5771 result
= (GLibValue
) temp_value
;
5772 } else if (gvariant_boxing
) {
5773 // implicit conversion to GVariant
5774 string variant_func
= "_variant_new%d".printf (++next_variant_function_id
);
5776 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (variant_func
));
5777 ccall
.add_argument (result
.cvalue
);
5779 var cfunc
= new
CCodeFunction (variant_func
, "GVariant*");
5780 cfunc
.modifiers
= CCodeModifiers
.STATIC
;
5781 cfunc
.add_parameter (new
CCodeParameter ("value", get_ccode_name (type
)));
5783 if (type is ArrayType
) {
5784 // return array length if appropriate
5785 var array_type
= (ArrayType
) type
;
5787 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5788 ccall
.add_argument (get_array_length_cvalue (value
, dim
));
5789 cfunc
.add_parameter (new
CCodeParameter (get_array_length_cname ("value", dim
), "gint"));
5793 push_function (cfunc
);
5795 // sink floating reference
5796 var sink
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_variant_ref_sink"));
5797 sink
.add_argument (serialize_expression (type
, new
CCodeIdentifier ("value")));
5798 ccode
.add_return (sink
);
5802 cfile
.add_function_declaration (cfunc
);
5803 cfile
.add_function (cfunc
);
5805 result
.cvalue
= ccall
;
5806 result
.value_type
.value_owned
= true;
5808 result
= (GLibValue
) store_temp_value (result
, node
);
5809 if (!target_type
.value_owned
) {
5811 temp_ref_values
.insert (0, ((GLibValue
) result
).copy ());
5813 } else if (boxing
) {
5814 // value needs to be boxed
5816 result
.value_type
.nullable
= false;
5817 if (!result
.lvalue
|| !result
.value_type
.equals (value
.value_type
)) {
5818 result
.cvalue
= get_implicit_cast_expression (result
.cvalue
, value
.value_type
, result
.value_type
, node
);
5819 result
= (GLibValue
) store_temp_value (result
, node
);
5821 result
.cvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, result
.cvalue
);
5822 result
.lvalue
= false;
5823 result
.value_type
.nullable
= true;
5824 } else if (unboxing
) {
5827 result
.cvalue
= new
CCodeUnaryExpression (CCodeUnaryOperator
.POINTER_INDIRECTION
, result
.cvalue
);
5829 // TODO: rewrite get_implicit_cast_expression to return a GLibValue
5830 var old_cexpr
= result
.cvalue
;
5831 result
.cvalue
= get_implicit_cast_expression (result
.cvalue
, type
, target_type
, node
);
5832 result
.lvalue
= result
.lvalue
&& result
.cvalue
== old_cexpr
;
5835 bool array_needs_copy
= false;
5836 if (type is ArrayType
&& target_type is ArrayType
) {
5837 var array
= (ArrayType
) type
;
5838 var target_array
= (ArrayType
) target_type
;
5839 if (target_array
.element_type
.value_owned
&& !array
.element_type
.value_owned
) {
5840 array_needs_copy
= requires_copy (target_array
.element_type
);
5844 if (!gvalue_boxing
&& !gvariant_boxing
&& target_type
.value_owned
&& (!type
.value_owned
|| boxing
|| unboxing
|| array_needs_copy
) && requires_copy (target_type
) && !(type is NullType
)) {
5845 // need to copy value
5846 var copy
= (GLibValue
) copy_value (result
, node
);
5847 if (target_type
.data_type is Interface
&& copy
== null) {
5848 Report
.error (node
.source_reference
, "missing class prerequisite for interface `%s', add GLib.Object to interface declaration if unsure".printf (target_type
.data_type
.get_full_name ()));
5857 public virtual CCodeExpression
get_implicit_cast_expression (CCodeExpression source_cexpr
, DataType? expression_type
, DataType? target_type
, CodeNode? node
) {
5858 var cexpr
= source_cexpr
;
5860 if (expression_type
.data_type
!= null && expression_type
.data_type
== target_type
.data_type
) {
5861 // same type, no cast required
5865 if (expression_type is NullType
) {
5866 // null literal, no cast required when not converting to generic type pointer
5870 generate_type_declaration (target_type
, cfile
);
5872 var cl
= target_type
.data_type as Class
;
5873 var iface
= target_type
.data_type as Interface
;
5874 if (context
.checking
&& (iface
!= null || (cl
!= null && !cl
.is_compact
))) {
5875 // checked cast for strict subtypes of GTypeInstance
5876 return generate_instance_cast (cexpr
, target_type
.data_type
);
5877 } else if (target_type
.data_type
!= null && get_ccode_name (expression_type
) != get_ccode_name (target_type
)) {
5878 var st
= target_type
.data_type as Struct
;
5879 if (target_type
.data_type
.is_reference_type () || (st
!= null && st
.is_simple_type ())) {
5880 // don't cast non-simple structs
5881 return new
CCodeCastExpression (cexpr
, get_ccode_name (target_type
));
5890 public void store_property (Property prop
, Expression? instance
, TargetValue value
) {
5891 if (instance is BaseAccess
) {
5892 if (prop
.base_property
!= null) {
5893 var base_class
= (Class
) prop
.base_property
.parent_symbol
;
5894 var vcast
= new
CCodeFunctionCall (new
CCodeIdentifier ("%s_CLASS".printf (get_ccode_upper_case_name (base_class
, null))));
5895 vcast
.add_argument (new
CCodeIdentifier ("%s_parent_class".printf (get_ccode_lower_case_name (current_class
, null))));
5897 var ccall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (vcast
, "set_%s".printf (prop
.name
)));
5898 ccall
.add_argument ((CCodeExpression
) get_ccodenode (instance
));
5899 var cexpr
= get_cvalue_ (value
);
5900 if (prop
.property_type
.is_real_non_null_struct_type ()) {
5901 cexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
);
5903 ccall
.add_argument (cexpr
);
5905 ccode
.add_expression (ccall
);
5906 } else if (prop
.base_interface_property
!= null) {
5907 var base_iface
= (Interface
) prop
.base_interface_property
.parent_symbol
;
5908 string parent_iface_var
= "%s_%s_parent_iface".printf (get_ccode_lower_case_name (current_class
), get_ccode_lower_case_name (base_iface
));
5910 var ccall
= new
CCodeFunctionCall (new CCodeMemberAccess
.pointer (new
CCodeIdentifier (parent_iface_var
), "set_%s".printf (prop
.name
)));
5911 ccall
.add_argument ((CCodeExpression
) get_ccodenode (instance
));
5912 var cexpr
= get_cvalue_ (value
);
5913 if (prop
.property_type
.is_real_non_null_struct_type ()) {
5914 cexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
);
5916 ccall
.add_argument (cexpr
);
5918 ccode
.add_expression (ccall
);
5923 var set_func
= "g_object_set";
5925 var base_property
= prop
;
5926 if (!get_ccode_no_accessor_method (prop
)) {
5927 if (prop
.base_property
!= null) {
5928 base_property
= prop
.base_property
;
5929 } else if (prop
.base_interface_property
!= null) {
5930 base_property
= prop
.base_interface_property
;
5933 if (prop is DynamicProperty
) {
5934 set_func
= get_dynamic_property_setter_cname ((DynamicProperty
) prop
);
5936 generate_property_accessor_declaration (base_property
.set_accessor
, cfile
);
5937 set_func
= get_ccode_name (base_property
.set_accessor
);
5939 if (!prop
.external
&& prop
.external_package
) {
5940 // internal VAPI properties
5941 // only add them once per source file
5942 if (add_generated_external_symbol (prop
)) {
5943 visit_property (prop
);
5949 var ccall
= new
CCodeFunctionCall (new
CCodeIdentifier (set_func
));
5951 if (prop
.binding
== MemberBinding
.INSTANCE
) {
5952 /* target instance is first argument */
5953 var cinstance
= (CCodeExpression
) get_ccodenode (instance
);
5955 if (prop
.parent_symbol is Struct
&& !((Struct
) prop
.parent_symbol
).is_simple_type ()) {
5956 // we need to pass struct instance by reference if it isn't a simple-type
5957 var instance_value
= instance
.target_value
;
5958 if (!get_lvalue (instance_value
)) {
5959 instance_value
= store_temp_value (instance_value
, instance
);
5961 cinstance
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, get_cvalue_ (instance_value
));
5964 ccall
.add_argument (cinstance
);
5967 if (get_ccode_no_accessor_method (prop
)) {
5968 /* property name is second argument of g_object_set */
5969 ccall
.add_argument (get_property_canonical_cconstant (prop
));
5972 var cexpr
= get_cvalue_ (value
);
5974 if (prop
.property_type
.is_real_non_null_struct_type ()) {
5975 cexpr
= new
CCodeUnaryExpression (CCodeUnaryOperator
.ADDRESS_OF
, cexpr
);
5978 var array_type
= prop
.property_type as ArrayType
;
5980 ccall
.add_argument (cexpr
);
5982 if (array_type
!= null && get_ccode_array_length (prop
)) {
5983 for (int dim
= 1; dim
<= array_type
.rank
; dim
++) {
5984 ccall
.add_argument (get_array_length_cvalue (value
, dim
));
5986 } else if (prop
.property_type is DelegateType
) {
5987 var delegate_type
= (DelegateType
) prop
.property_type
;
5988 if (delegate_type
.delegate_symbol
.has_target
) {
5989 ccall
.add_argument (get_delegate_target_cvalue (value
));
5990 if (base_property
.set_accessor
.value_type
.value_owned
) {
5991 ccall
.add_argument (get_delegate_target_destroy_notify_cvalue (value
));
5996 if (get_ccode_no_accessor_method (prop
)) {
5997 ccall
.add_argument (new
CCodeConstant ("NULL"));
6000 ccode
.add_expression (ccall
);
6003 public bool add_wrapper (string wrapper_name
) {
6004 return wrappers
.add (wrapper_name
);
6007 public bool add_generated_external_symbol (Symbol external_symbol
) {
6008 return generated_external_symbols
.add (external_symbol
);
6011 public static DataType
get_data_type_for_symbol (TypeSymbol sym
) {
6012 DataType type
= null;
6015 type
= new
ObjectType ((Class
) sym
);
6016 } else if (sym is Interface
) {
6017 type
= new
ObjectType ((Interface
) sym
);
6018 } else if (sym is Struct
) {
6019 var st
= (Struct
) sym
;
6020 if (st
.is_boolean_type ()) {
6021 type
= new
BooleanType (st
);
6022 } else if (st
.is_integer_type ()) {
6023 type
= new
IntegerType (st
);
6024 } else if (st
.is_floating_type ()) {
6025 type
= new
FloatingType (st
);
6027 type
= new
StructValueType (st
);
6029 } else if (sym is Enum
) {
6030 type
= new
EnumValueType ((Enum
) sym
);
6031 } else if (sym is ErrorDomain
) {
6032 type
= new
ErrorType ((ErrorDomain
) sym
, null);
6033 } else if (sym is ErrorCode
) {
6034 type
= new
ErrorType ((ErrorDomain
) sym
.parent_symbol
, (ErrorCode
) sym
);
6036 Report
.error (null, "internal error: `%s' is not a supported type".printf (sym
.get_full_name ()));
6037 return new
InvalidType ();
6043 public CCodeExpression?
default_value_for_type (DataType type
, bool initializer_expression
) {
6044 var st
= type
.data_type as Struct
;
6045 var array_type
= type as ArrayType
;
6046 if (type
.data_type
!= null && !type
.nullable
&& get_ccode_default_value (type
.data_type
) != "") {
6047 return new
CCodeConstant (get_ccode_default_value (type
.data_type
));
6048 } else if (initializer_expression
&& !type
.nullable
&&
6049 (st
!= null || (array_type
!= null && array_type
.fixed_length
))) {
6050 // 0-initialize struct with struct initializer { 0 }
6051 // only allowed as initializer expression in C
6052 var clist
= new
CCodeInitializerList ();
6053 clist
.append (new
CCodeConstant ("0"));
6055 } else if ((type
.data_type
!= null && type
.data_type
.is_reference_type ())
6057 || type is PointerType
|| type is DelegateType
6058 || (array_type
!= null && !array_type
.fixed_length
)) {
6059 return new
CCodeConstant ("NULL");
6060 } else if (type is GenericType
) {
6061 return new
CCodeConstant ("NULL");
6062 } else if (type is ErrorType
) {
6063 return new
CCodeConstant ("NULL");
6068 private void create_property_type_check_statement (Property prop
, bool check_return_type
, TypeSymbol t
, bool non_null
, string var_name
) {
6069 if (check_return_type
) {
6070 create_type_check_statement (prop
, prop
.property_type
, t
, non_null
, var_name
);
6072 create_type_check_statement (prop
, new
VoidType (), t
, non_null
, var_name
);
6076 public virtual void create_type_check_statement (CodeNode method_node
, DataType ret_type
, TypeSymbol t
, bool non_null
, string var_name
) {
6079 public int get_param_pos (double param_pos
, bool ellipsis
= false) {
6081 if (param_pos
>= 0) {
6082 return (int) (param_pos
* 1000);
6084 return (int) ((100 + param_pos
) * 1000);
6087 if (param_pos
>= 0) {
6088 return (int) ((100 + param_pos
) * 1000);
6090 return (int) ((200 + param_pos
) * 1000);
6095 public CCodeExpression?
get_ccodenode (Expression node
) {
6096 if (get_cvalue (node
) == null) {
6099 return get_cvalue (node
);
6102 public bool is_lvalue_access_allowed (DataType type
) {
6103 var array_type
= type as ArrayType
;
6104 if (array_type
!= null && array_type
.inline_allocated
) {
6107 if (type
.data_type
!= null) {
6108 return type
.data_type
.get_attribute_bool ("CCode", "lvalue_access", true);
6113 public CCodeDeclaratorSuffix?
get_ccode_declarator_suffix (DataType type
) {
6114 var array_type
= type as ArrayType
;
6115 if (array_type
!= null) {
6116 if (array_type
.fixed_length
) {
6117 return new CCodeDeclaratorSuffix
.with_array (get_ccodenode (array_type
.length
));
6118 } else if (array_type
.inline_allocated
) {
6119 return new CCodeDeclaratorSuffix
.with_array ();
6125 public CCodeConstant
get_signal_canonical_constant (Signal sig
, string? detail
= null) {
6126 return new
CCodeConstant ("\"%s%s\"".printf (get_ccode_name (sig
), (detail
!= null ?
"::%s".printf (detail
) : "")));
6129 public static CCodeConstant
get_enum_value_canonical_cconstant (EnumValue ev
) {
6130 return new
CCodeConstant ("\"%s\"".printf (ev
.name
.down ().replace ("_", "-")));
6133 public bool get_signal_has_emitter (Signal sig
) {
6134 return sig
.get_attribute ("HasEmitter") != null;
6137 public CCodeConstant
get_property_canonical_cconstant (Property prop
) {
6138 return new
CCodeConstant ("\"%s\"".printf (prop
.name
.replace ("_", "-")));
6141 public override void visit_class (Class cl
) {
6144 public void create_postcondition_statement (Expression postcondition
) {
6145 var cassert
= new
CCodeFunctionCall (new
CCodeIdentifier ("_vala_warn_if_fail"));
6147 postcondition
.emit (this
);
6149 string message
= ((string) postcondition
.source_reference
.begin
.pos
).substring (0, (int) (postcondition
.source_reference
.end
.pos
- postcondition
.source_reference
.begin
.pos
));
6150 cassert
.add_argument (get_cvalue (postcondition
));
6151 cassert
.add_argument (new
CCodeConstant ("\"%s\"".printf (message
.replace ("\n", " ").escape (""))));
6152 requires_assert
= true;
6154 ccode
.add_expression (cassert
);
6157 public virtual bool is_gobject_property (Property prop
) {
6161 public DataType?
get_this_type () {
6162 if (current_method
!= null && current_method
.binding
== MemberBinding
.INSTANCE
) {
6163 return current_method
.this_parameter
.variable_type
;
6164 } else if (current_property_accessor
!= null && current_property_accessor
.prop
.binding
== MemberBinding
.INSTANCE
) {
6165 return current_property_accessor
.prop
.this_parameter
.variable_type
;
6166 } else if (current_constructor
!= null && current_constructor
.binding
== MemberBinding
.INSTANCE
) {
6167 return current_constructor
.this_parameter
.variable_type
;
6168 } else if (current_destructor
!= null && current_destructor
.binding
== MemberBinding
.INSTANCE
) {
6169 return current_destructor
.this_parameter
.variable_type
;
6174 public CCodeFunctionCall
generate_instance_cast (CCodeExpression expr
, TypeSymbol type
) {
6175 var result
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_TYPE_CHECK_INSTANCE_CAST"));
6176 result
.add_argument (expr
);
6177 result
.add_argument (new
CCodeIdentifier (get_ccode_type_id (type
)));
6178 result
.add_argument (new
CCodeIdentifier (get_ccode_name (type
)));
6182 void generate_struct_destroy_function (Struct st
) {
6183 if (cfile
.add_declaration (get_ccode_destroy_function (st
))) {
6184 // only generate function once per source file
6188 var function
= new
CCodeFunction (get_ccode_destroy_function (st
), "void");
6189 function
.modifiers
= CCodeModifiers
.STATIC
;
6190 function
.add_parameter (new
CCodeParameter ("self", "%s *".printf (get_ccode_name (st
))));
6192 push_context (new
EmitContext ());
6193 push_function (function
);
6195 var this_value
= load_this_parameter (st
);
6196 foreach (Field f
in st
.get_fields ()) {
6197 if (f
.binding
== MemberBinding
.INSTANCE
) {
6198 if (requires_destroy (f
.variable_type
)) {
6199 ccode
.add_expression (destroy_field (f
, this_value
));
6207 cfile
.add_function_declaration (function
);
6208 cfile
.add_function (function
);
6211 void generate_struct_copy_function (Struct st
) {
6212 if (cfile
.add_declaration (get_ccode_copy_function (st
))) {
6213 // only generate function once per source file
6217 var function
= new
CCodeFunction (get_ccode_copy_function (st
), "void");
6218 function
.modifiers
= CCodeModifiers
.STATIC
;
6219 function
.add_parameter (new
CCodeParameter ("self", "const %s *".printf (get_ccode_name (st
))));
6220 function
.add_parameter (new
CCodeParameter ("dest", "%s *".printf (get_ccode_name (st
))));
6222 push_context (new
EmitContext ());
6223 push_function (function
);
6225 var dest_struct
= new
GLibValue (get_data_type_for_symbol (st
), new
CCodeIdentifier ("(*dest)"), true);
6226 foreach (Field f
in st
.get_fields ()) {
6227 if (f
.binding
== MemberBinding
.INSTANCE
) {
6228 var value
= load_field (f
, load_this_parameter ((TypeSymbol
) st
));
6229 if (requires_copy (f
.variable_type
)) {
6230 value
= copy_value (value
, f
);
6231 if (value
== null) {
6232 // error case, continue to avoid critical
6236 store_field (f
, dest_struct
, value
);
6243 cfile
.add_function_declaration (function
);
6244 cfile
.add_function (function
);
6247 public void return_default_value (DataType return_type
) {
6248 var st
= return_type
.data_type as Struct
;
6249 if (st
!= null && st
.is_simple_type () && !return_type
.nullable
) {
6250 // 0-initialize struct with struct initializer { 0 }
6251 // only allowed as initializer expression in C
6252 var ret_temp_var
= get_temp_variable (return_type
, true, null, true);
6253 emit_temp_var (ret_temp_var
);
6254 ccode
.add_return (new
CCodeIdentifier (ret_temp_var
.name
));
6256 ccode
.add_return (default_value_for_type (return_type
, false));
6260 public virtual void generate_dynamic_method_wrapper (DynamicMethod method
) {
6263 public virtual bool method_has_wrapper (Method method
) {
6267 public virtual CCodeExpression
get_param_spec_cexpression (Property prop
) {
6268 return new
CCodeFunctionCall (new
CCodeIdentifier (""));
6271 public virtual CCodeExpression
get_param_spec (Property prop
) {
6272 return new
CCodeFunctionCall (new
CCodeIdentifier (""));
6275 public virtual CCodeExpression
get_signal_creation (Signal sig
, TypeSymbol type
) {
6276 return new
CCodeFunctionCall (new
CCodeIdentifier (""));
6279 public virtual void register_dbus_info (CCodeBlock block
, ObjectTypeSymbol bindable
) {
6282 public virtual string get_dynamic_property_getter_cname (DynamicProperty node
) {
6283 Report
.error (node
.source_reference
, "dynamic properties are not supported for %s".printf (node
.dynamic_type
.to_string ()));
6287 public virtual string get_dynamic_property_setter_cname (DynamicProperty node
) {
6288 Report
.error (node
.source_reference
, "dynamic properties are not supported for %s".printf (node
.dynamic_type
.to_string ()));
6292 public virtual string get_dynamic_signal_cname (DynamicSignal node
) {
6296 public virtual string get_dynamic_signal_connect_wrapper_name (DynamicSignal node
) {
6300 public virtual string get_dynamic_signal_connect_after_wrapper_name (DynamicSignal node
) {
6304 public virtual string get_dynamic_signal_disconnect_wrapper_name (DynamicSignal node
) {
6308 public virtual string get_array_length_cname (string array_cname
, int dim
) {
6312 public virtual string get_parameter_array_length_cname (Parameter param
, int dim
) {
6316 public virtual CCodeExpression
get_array_length_cexpression (Expression array_expr
, int dim
= -1) {
6317 return new
CCodeConstant ("");
6320 public virtual CCodeExpression
get_array_length_cvalue (TargetValue value
, int dim
= -1) {
6321 return new
CCodeInvalidExpression ();
6324 public virtual string get_array_size_cname (string array_cname
) {
6328 public virtual void add_simple_check (CodeNode node
, bool always_fails
= false) {
6331 public virtual string generate_ready_function (Method m
) {
6335 public CCodeExpression?
get_cvalue (Expression expr
) {
6336 if (expr
.target_value
== null) {
6339 var glib_value
= (GLibValue
) expr
.target_value
;
6340 return glib_value
.cvalue
;
6343 public CCodeExpression?
get_cvalue_ (TargetValue value
) {
6344 var glib_value
= (GLibValue
) value
;
6345 return glib_value
.cvalue
;
6348 public void set_cvalue (Expression expr
, CCodeExpression? cvalue
) {
6349 var glib_value
= (GLibValue
) expr
.target_value
;
6350 if (glib_value
== null) {
6351 glib_value
= new
GLibValue (expr
.value_type
);
6352 expr
.target_value
= glib_value
;
6354 glib_value
.cvalue
= cvalue
;
6357 public CCodeExpression?
get_array_size_cvalue (TargetValue value
) {
6358 var glib_value
= (GLibValue
) value
;
6359 return glib_value
.array_size_cvalue
;
6362 public void set_array_size_cvalue (TargetValue value
, CCodeExpression? cvalue
) {
6363 var glib_value
= (GLibValue
) value
;
6364 glib_value
.array_size_cvalue
= cvalue
;
6367 public CCodeExpression?
get_delegate_target (Expression expr
) {
6368 if (expr
.target_value
== null) {
6371 var glib_value
= (GLibValue
) expr
.target_value
;
6372 return glib_value
.delegate_target_cvalue
;
6375 public void set_delegate_target (Expression expr
, CCodeExpression? delegate_target
) {
6376 var glib_value
= (GLibValue
) expr
.target_value
;
6377 if (glib_value
== null) {
6378 glib_value
= new
GLibValue (expr
.value_type
);
6379 expr
.target_value
= glib_value
;
6381 glib_value
.delegate_target_cvalue
= delegate_target
;
6384 public CCodeExpression?
get_delegate_target_destroy_notify (Expression expr
) {
6385 if (expr
.target_value
== null) {
6388 var glib_value
= (GLibValue
) expr
.target_value
;
6389 return glib_value
.delegate_target_destroy_notify_cvalue
;
6392 public void set_delegate_target_destroy_notify (Expression expr
, CCodeExpression? destroy_notify
) {
6393 var glib_value
= (GLibValue
) expr
.target_value
;
6394 if (glib_value
== null) {
6395 glib_value
= new
GLibValue (expr
.value_type
);
6396 expr
.target_value
= glib_value
;
6398 glib_value
.delegate_target_destroy_notify_cvalue
= destroy_notify
;
6401 public void append_array_length (Expression expr
, CCodeExpression size
) {
6402 var glib_value
= (GLibValue
) expr
.target_value
;
6403 if (glib_value
== null) {
6404 glib_value
= new
GLibValue (expr
.value_type
);
6405 expr
.target_value
= glib_value
;
6407 glib_value
.append_array_length_cvalue (size
);
6410 public List
<CCodeExpression
>?
get_array_lengths (Expression expr
) {
6411 var glib_value
= (GLibValue
) expr
.target_value
;
6412 if (glib_value
== null) {
6413 glib_value
= new
GLibValue (expr
.value_type
);
6414 expr
.target_value
= glib_value
;
6416 return glib_value
.array_length_cvalues
;
6419 public bool get_lvalue (TargetValue value
) {
6420 var glib_value
= (GLibValue
) value
;
6421 return glib_value
.lvalue
;
6424 public bool get_non_null (TargetValue value
) {
6425 var glib_value
= (GLibValue
) value
;
6426 return glib_value
.non_null
;
6429 public string?
get_ctype (TargetValue value
) {
6430 var glib_value
= (GLibValue
) value
;
6431 return glib_value
.ctype
;
6434 public bool get_array_null_terminated (TargetValue value
) {
6435 var glib_value
= (GLibValue
) value
;
6436 return glib_value
.array_null_terminated
;
6439 public CCodeExpression
get_array_length_cexpr (TargetValue value
) {
6440 var glib_value
= (GLibValue
) value
;
6441 return glib_value
.array_length_cexpr
;
6445 internal class Vala
.GLibValue
: TargetValue
{
6446 public CCodeExpression cvalue
;
6448 public bool non_null
;
6449 public string? ctype
;
6451 public List
<CCodeExpression
> array_length_cvalues
;
6452 public CCodeExpression? array_size_cvalue
;
6453 public bool array_null_terminated
;
6454 public CCodeExpression? array_length_cexpr
;
6456 public CCodeExpression? delegate_target_cvalue
;
6457 public CCodeExpression? delegate_target_destroy_notify_cvalue
;
6459 public GLibValue (DataType? value_type
= null, CCodeExpression? cvalue
= null, bool lvalue
= false) {
6461 this
.cvalue
= cvalue
;
6462 this
.lvalue
= lvalue
;
6465 public void append_array_length_cvalue (CCodeExpression length_cvalue
) {
6466 if (array_length_cvalues
== null) {
6467 array_length_cvalues
= new ArrayList
<CCodeExpression
> ();
6469 array_length_cvalues
.add (length_cvalue
);
6472 public GLibValue
copy () {
6473 var result
= new
GLibValue (value_type
.copy (), cvalue
, lvalue
);
6474 result
.actual_value_type
= actual_value_type
;
6475 result
.non_null
= non_null
;
6476 result
.ctype
= ctype
;
6478 if (array_length_cvalues
!= null) {
6479 foreach (var cexpr
in array_length_cvalues
) {
6480 result
.append_array_length_cvalue (cexpr
);
6483 result
.array_size_cvalue
= array_size_cvalue
;
6484 result
.array_null_terminated
= array_null_terminated
;
6485 result
.array_length_cexpr
= array_length_cexpr
;
6487 result
.delegate_target_cvalue
= delegate_target_cvalue
;
6488 result
.delegate_target_destroy_notify_cvalue
= delegate_target_destroy_notify_cvalue
;