1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
|
/* valagasyncmodule.vala
*
* Copyright (C) 2008-2010 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
public class Vala.GAsyncModule : GSignalModule {
CCodeStruct generate_data_struct (Method m) {
string dataname = Symbol.lower_case_to_camel_case (get_ccode_name (m)) + "Data";
var data = new CCodeStruct ("_" + dataname);
data.add_field ("int", "_state_");
data.add_field ("GObject*", "_source_object_");
data.add_field ("GAsyncResult*", "_res_");
data.add_field ("GSimpleAsyncResult*", "_async_result");
if (m.binding == MemberBinding.INSTANCE) {
var type_sym = (TypeSymbol) m.parent_symbol;
if (type_sym is ObjectTypeSymbol) {
data.add_field (get_ccode_name (type_sym) + "*", "self");
} else {
data.add_field (get_ccode_name (type_sym), "self");
}
}
foreach (Parameter param in m.get_parameters ()) {
bool is_unowned_delegate = param.variable_type is DelegateType && !param.variable_type.value_owned;
var param_type = param.variable_type.copy ();
param_type.value_owned = true;
data.add_field (get_ccode_name (param_type), get_variable_cname (param.name));
if (param.variable_type is ArrayType) {
var array_type = (ArrayType) param.variable_type;
if (get_ccode_array_length (param)) {
for (int dim = 1; dim <= array_type.rank; dim++) {
data.add_field ("gint", get_parameter_array_length_cname (param, dim));
}
}
} else if (param.variable_type is DelegateType) {
var deleg_type = (DelegateType) param.variable_type;
if (deleg_type.delegate_symbol.has_target) {
data.add_field ("gpointer", get_delegate_target_cname (get_variable_cname (param.name)));
if (!is_unowned_delegate) {
data.add_field ("GDestroyNotify", get_delegate_target_destroy_notify_cname (get_variable_cname (param.name)));
}
}
}
}
foreach (var type_param in m.get_type_parameters ()) {
data.add_field ("GType", "%s_type".printf (type_param.name.down ()));
data.add_field ("GBoxedCopyFunc", "%s_dup_func".printf (type_param.name.down ()));
data.add_field ("GDestroyNotify", "%s_destroy_func".printf (type_param.name.down ()));
}
if (!(m.return_type is VoidType)) {
data.add_field (get_ccode_name (m.return_type), "result");
if (m.return_type is ArrayType) {
var array_type = (ArrayType) m.return_type;
if (get_ccode_array_length (m)) {
for (int dim = 1; dim <= array_type.rank; dim++) {
data.add_field ("gint", get_array_length_cname ("result", dim));
}
}
} else if (m.return_type is DelegateType) {
var deleg_type = (DelegateType) m.return_type;
if (deleg_type.delegate_symbol.has_target) {
data.add_field ("gpointer", get_delegate_target_cname ("result"));
data.add_field ("GDestroyNotify", get_delegate_target_destroy_notify_cname ("result"));
}
}
}
return data;
}
CCodeFunction generate_free_function (Method m) {
var dataname = Symbol.lower_case_to_camel_case (get_ccode_name (m)) + "Data";
var freefunc = new CCodeFunction (get_ccode_real_name (m) + "_data_free", "void");
freefunc.modifiers = CCodeModifiers.STATIC;
freefunc.add_parameter (new CCodeParameter ("_data", "gpointer"));
push_context (new EmitContext (m));
push_function (freefunc);
ccode.add_declaration (dataname + "*", new CCodeVariableDeclarator ("_data_", new CCodeIdentifier ("_data")));
foreach (Parameter param in m.get_parameters ()) {
if (param.direction != ParameterDirection.OUT) {
bool is_unowned_delegate = param.variable_type is DelegateType && !param.variable_type.value_owned;
var param_type = param.variable_type.copy ();
param_type.value_owned = true;
if (requires_destroy (param_type) && !is_unowned_delegate) {
// do not try to access closure blocks
bool old_captured = param.captured;
param.captured = false;
ccode.add_expression (destroy_parameter (param));
param.captured = old_captured;
}
}
}
if (requires_destroy (m.return_type)) {
/* this is very evil. */
var v = new LocalVariable (m.return_type, ".result");
ccode.add_expression (destroy_local (v));
}
if (m.binding == MemberBinding.INSTANCE) {
var this_type = m.this_parameter.variable_type.copy ();
this_type.value_owned = true;
if (requires_destroy (this_type)) {
ccode.add_expression (destroy_parameter (m.this_parameter));
}
}
var freecall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_free"));
freecall.add_argument (new CCodeIdentifier (dataname));
freecall.add_argument (new CCodeIdentifier ("_data_"));
ccode.add_expression (freecall);
pop_context ();
cfile.add_function_declaration (freefunc);
cfile.add_function (freefunc);
return freefunc;
}
void generate_async_function (Method m) {
push_context (new EmitContext ());
var dataname = Symbol.lower_case_to_camel_case (get_ccode_name (m)) + "Data";
var asyncfunc = new CCodeFunction (get_ccode_real_name (m), "void");
var cparam_map = new HashMap<int,CCodeParameter> (direct_hash, direct_equal);
cparam_map.set (get_param_pos (-1), new CCodeParameter ("_callback_", "GAsyncReadyCallback"));
cparam_map.set (get_param_pos (-0.9), new CCodeParameter ("_user_data_", "gpointer"));
generate_cparameters (m, cfile, cparam_map, asyncfunc, null, null, null, 1);
if (m.base_method != null || m.base_interface_method != null) {
// declare *_real_* function
asyncfunc.modifiers |= CCodeModifiers.STATIC;
cfile.add_function_declaration (asyncfunc);
} else if (m.is_private_symbol ()) {
asyncfunc.modifiers |= CCodeModifiers.STATIC;
}
push_function (asyncfunc);
// logic copied from valaccodemethodmodule
if (m.overrides || (m.base_interface_method != null && !m.is_abstract && !m.is_virtual)) {
Method base_method;
if (m.overrides) {
base_method = m.base_method;
} else {
base_method = m.base_interface_method;
}
var base_expression_type = new ObjectType ((ObjectTypeSymbol) base_method.parent_symbol);
var type_symbol = m.parent_symbol as ObjectTypeSymbol;
var self_target_type = new ObjectType (type_symbol);
var cself = get_cvalue_ (transform_value (new GLibValue (base_expression_type, new CCodeIdentifier ("base"), true), self_target_type, m));
ccode.add_declaration ("%s *".printf (get_ccode_name (type_symbol)), new CCodeVariableDeclarator ("self"));
ccode.add_assignment (new CCodeIdentifier ("self"), cself);
}
var dataalloc = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_new0"));
dataalloc.add_argument (new CCodeIdentifier (dataname));
var data_var = new CCodeIdentifier ("_data_");
ccode.add_declaration (dataname + "*", new CCodeVariableDeclarator ("_data_"));
ccode.add_assignment (data_var, dataalloc);
var create_result = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_new"));
var cl = m.parent_symbol as Class;
if (m.binding == MemberBinding.INSTANCE &&
cl != null && cl.is_subtype_of (gobject_type)) {
var gobject_cast = new CCodeFunctionCall (new CCodeIdentifier ("G_OBJECT"));
gobject_cast.add_argument (new CCodeIdentifier ("self"));
create_result.add_argument (gobject_cast);
} else {
if (context.require_glib_version (2, 20)) {
create_result.add_argument (new CCodeConstant ("NULL"));
} else {
var object_creation = new CCodeFunctionCall (new CCodeIdentifier ("g_object_newv"));
object_creation.add_argument (new CCodeConstant ("G_TYPE_OBJECT"));
object_creation.add_argument (new CCodeConstant ("0"));
object_creation.add_argument (new CCodeConstant ("NULL"));
create_result.add_argument (object_creation);
}
}
create_result.add_argument (new CCodeIdentifier ("_callback_"));
create_result.add_argument (new CCodeIdentifier ("_user_data_"));
create_result.add_argument (new CCodeIdentifier (get_ccode_real_name (m)));
ccode.add_assignment (new CCodeMemberAccess.pointer (data_var, "_async_result"), create_result);
var set_op_res_call = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_set_op_res_gpointer"));
set_op_res_call.add_argument (new CCodeMemberAccess.pointer (data_var, "_async_result"));
set_op_res_call.add_argument (data_var);
set_op_res_call.add_argument (new CCodeIdentifier (get_ccode_real_name (m) + "_data_free"));
ccode.add_expression (set_op_res_call);
if (m.binding == MemberBinding.INSTANCE) {
var this_type = m.this_parameter.variable_type.copy ();
this_type.value_owned = true;
// create copy if necessary as variables in async methods may need to be kept alive
CCodeExpression cself = new CCodeIdentifier ("self");
if (this_type.is_real_non_null_struct_type ()) {
cself = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, cself);
}
if (requires_copy (this_type)) {
cself = get_cvalue_ (copy_value (new GLibValue (m.this_parameter.variable_type, cself, true), m.this_parameter));
}
ccode.add_assignment (new CCodeMemberAccess.pointer (data_var, "self"), cself);
}
emit_context.push_symbol (m);
foreach (Parameter param in m.get_parameters ()) {
if (param.direction != ParameterDirection.OUT) {
bool is_unowned_delegate = param.variable_type is DelegateType && !param.variable_type.value_owned;
var param_type = param.variable_type.copy ();
param_type.value_owned = true;
// create copy if necessary as variables in async methods may need to be kept alive
var old_captured = param.captured;
param.captured = false;
current_method.coroutine = false;
var value = load_parameter (param);
if (requires_copy (param_type) && !param.variable_type.value_owned && !is_unowned_delegate) {
value = copy_value (value, param);
}
current_method.coroutine = true;
store_parameter (param, value);
param.captured = old_captured;
}
}
emit_context.pop_symbol ();
foreach (var type_param in m.get_type_parameters ()) {
var type = "%s_type".printf (type_param.name.down ());
var dup_func = "%s_dup_func".printf (type_param.name.down ());
var destroy_func = "%s_destroy_func".printf (type_param.name.down ());
ccode.add_assignment (new CCodeMemberAccess.pointer (data_var, type), new CCodeIdentifier (type));
ccode.add_assignment (new CCodeMemberAccess.pointer (data_var, dup_func), new CCodeIdentifier (dup_func));
ccode.add_assignment (new CCodeMemberAccess.pointer (data_var, destroy_func), new CCodeIdentifier (destroy_func));
}
var ccall = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_real_name (m) + "_co"));
ccall.add_argument (data_var);
ccode.add_expression (ccall);
cfile.add_function (asyncfunc);
pop_context ();
}
void append_struct (CCodeStruct structure) {
var typename = new CCodeVariableDeclarator (structure.name.substring (1));
var typedef = new CCodeTypeDefinition ("struct " + structure.name, typename);
cfile.add_type_declaration (typedef);
cfile.add_type_definition (structure);
}
public override void generate_method_declaration (Method m, CCodeFile decl_space) {
if (m.coroutine) {
if (add_symbol_declaration (decl_space, m, get_ccode_name (m))) {
return;
}
var asyncfunc = new CCodeFunction (get_ccode_name (m), "void");
var cparam_map = new HashMap<int,CCodeParameter> (direct_hash, direct_equal);
cparam_map.set (get_param_pos (-1), new CCodeParameter ("_callback_", "GAsyncReadyCallback"));
cparam_map.set (get_param_pos (-0.9), new CCodeParameter ("_user_data_", "gpointer"));
generate_cparameters (m, decl_space, cparam_map, asyncfunc, null, null, null, 1);
if (m.is_private_symbol ()) {
asyncfunc.modifiers |= CCodeModifiers.STATIC;
}
decl_space.add_function_declaration (asyncfunc);
var finishfunc = new CCodeFunction (get_ccode_finish_name (m));
cparam_map = new HashMap<int,CCodeParameter> (direct_hash, direct_equal);
cparam_map.set (get_param_pos (0.1), new CCodeParameter ("_res_", "GAsyncResult*"));
generate_cparameters (m, decl_space, cparam_map, finishfunc, null, null, null, 2);
if (m.is_private_symbol ()) {
finishfunc.modifiers |= CCodeModifiers.STATIC;
}
decl_space.add_function_declaration (finishfunc);
} else {
base.generate_method_declaration (m, decl_space);
}
}
public override void visit_method (Method m) {
if (m.coroutine) {
cfile.add_include ("gio/gio.h");
if (!m.is_internal_symbol ()) {
header_file.add_include ("gio/gio.h");
}
if (!m.is_abstract && m.body != null) {
var data = generate_data_struct (m);
closure_struct = data;
generate_free_function (m);
generate_async_function (m);
generate_finish_function (m);
// append the _co function
base.visit_method (m);
closure_struct = null;
// only append data struct here to make sure all struct member
// types are declared before the struct definition
append_struct (data);
} else {
generate_method_declaration (m, cfile);
if (!m.is_internal_symbol ()) {
generate_method_declaration (m, header_file);
}
if (!m.is_private_symbol ()) {
generate_method_declaration (m, internal_header_file);
}
}
if (m.is_abstract || m.is_virtual) {
// generate virtual function wrappers
var cparam_map = new HashMap<int,CCodeParameter> (direct_hash, direct_equal);
var carg_map = new HashMap<int,CCodeExpression> (direct_hash, direct_equal);
generate_vfunc (m, new VoidType (), cparam_map, carg_map, "", 1);
cparam_map = new HashMap<int,CCodeParameter> (direct_hash, direct_equal);
carg_map = new HashMap<int,CCodeExpression> (direct_hash, direct_equal);
generate_vfunc (m, m.return_type, cparam_map, carg_map, "_finish", 2);
}
} else {
base.visit_method (m);
}
}
void generate_finish_function (Method m) {
push_context (new EmitContext ());
string dataname = Symbol.lower_case_to_camel_case (get_ccode_name (m)) + "Data";
var finishfunc = new CCodeFunction (get_ccode_finish_real_name (m));
var cparam_map = new HashMap<int,CCodeParameter> (direct_hash, direct_equal);
cparam_map.set (get_param_pos (0.1), new CCodeParameter ("_res_", "GAsyncResult*"));
generate_cparameters (m, cfile, cparam_map, finishfunc, null, null, null, 2);
if (m.is_private_symbol () || m.base_method != null || m.base_interface_method != null) {
finishfunc.modifiers |= CCodeModifiers.STATIC;
}
push_function (finishfunc);
var return_type = m.return_type;
if (!(return_type is VoidType) && !return_type.is_real_non_null_struct_type ()) {
ccode.add_declaration (get_ccode_name (m.return_type), new CCodeVariableDeclarator ("result"));
}
var data_var = new CCodeIdentifier ("_data_");
ccode.add_declaration (dataname + "*", new CCodeVariableDeclarator ("_data_"));
var simple_async_result_cast = new CCodeFunctionCall (new CCodeIdentifier ("G_SIMPLE_ASYNC_RESULT"));
simple_async_result_cast.add_argument (new CCodeIdentifier ("_res_"));
if (m.get_error_types ().size > 0) {
// propagate error from async method
var propagate_error = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_propagate_error"));
propagate_error.add_argument (simple_async_result_cast);
propagate_error.add_argument (new CCodeIdentifier ("error"));
ccode.open_if (propagate_error);
return_default_value (return_type);
ccode.close ();
}
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_get_op_res_gpointer"));
ccall.add_argument (simple_async_result_cast);
ccode.add_assignment (data_var, ccall);
emit_context.push_symbol (m);
foreach (Parameter param in m.get_parameters ()) {
if (param.direction != ParameterDirection.IN) {
return_out_parameter (param);
if (!(param.variable_type is ValueType) || param.variable_type.nullable) {
ccode.add_assignment (new CCodeMemberAccess.pointer (data_var, get_variable_cname (param.name)), new CCodeConstant ("NULL"));
}
}
}
emit_context.pop_symbol ();
if (return_type.is_real_non_null_struct_type ()) {
// structs are returned via out parameter
CCodeExpression cexpr = new CCodeMemberAccess.pointer (data_var, "result");
if (requires_copy (return_type)) {
cexpr = get_cvalue_ (copy_value (new GLibValue (return_type, cexpr, true), return_type));
}
ccode.add_assignment (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("result")), cexpr);
} else if (!(return_type is VoidType)) {
ccode.add_assignment (new CCodeIdentifier ("result"), new CCodeMemberAccess.pointer (data_var, "result"));
if (return_type is ArrayType) {
var array_type = (ArrayType) return_type;
if (get_ccode_array_length (m)) {
for (int dim = 1; dim <= array_type.rank; dim++) {
ccode.add_assignment (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier (get_array_length_cname ("result", dim))), new CCodeMemberAccess.pointer (data_var, get_array_length_cname ("result", dim)));
}
}
} else if (return_type is DelegateType && ((DelegateType) return_type).delegate_symbol.has_target) {
ccode.add_assignment (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier (get_delegate_target_cname ("result"))), new CCodeMemberAccess.pointer (data_var, get_delegate_target_cname ("result")));
}
if (!(return_type is ValueType) || return_type.nullable) {
ccode.add_assignment (new CCodeMemberAccess.pointer (data_var, "result"), new CCodeConstant ("NULL"));
}
ccode.add_return (new CCodeIdentifier ("result"));
}
pop_function ();
cfile.add_function (finishfunc);
pop_context ();
}
public override string generate_ready_function (Method m) {
// generate ready callback handler
var dataname = Symbol.lower_case_to_camel_case (get_ccode_name (m)) + "Data";
var readyfunc = new CCodeFunction (get_ccode_name (m) + "_ready", "void");
if (!add_wrapper (readyfunc.name)) {
// wrapper already defined
return readyfunc.name;
}
readyfunc.add_parameter (new CCodeParameter ("source_object", "GObject*"));
readyfunc.add_parameter (new CCodeParameter ("_res_", "GAsyncResult*"));
readyfunc.add_parameter (new CCodeParameter ("_user_data_", "gpointer"));
push_function (readyfunc);
ccode.add_declaration (dataname + "*", new CCodeVariableDeclarator ("_data_"));
ccode.add_assignment (new CCodeIdentifier ("_data_"), new CCodeIdentifier ("_user_data_"));
ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), "_source_object_"), new CCodeIdentifier ("source_object"));
ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), "_res_"), new CCodeIdentifier ("_res_"));
var ccall = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_real_name (m) + "_co"));
ccall.add_argument (new CCodeIdentifier ("_data_"));
ccode.add_expression (ccall);
readyfunc.modifiers |= CCodeModifiers.STATIC;
pop_function ();
cfile.add_function_declaration (readyfunc);
cfile.add_function (readyfunc);
return readyfunc.name;
}
public override void generate_virtual_method_declaration (Method m, CCodeFile decl_space, CCodeStruct type_struct) {
if (!m.coroutine) {
base.generate_virtual_method_declaration (m, decl_space, type_struct);
return;
}
if (!m.is_abstract && !m.is_virtual) {
return;
}
var creturn_type = m.return_type;
if (m.return_type.is_real_non_null_struct_type ()) {
// structs are returned via out parameter
creturn_type = new VoidType ();
}
// add vfunc field to the type struct
var vdeclarator = new CCodeFunctionDeclarator (get_ccode_vfunc_name (m));
var cparam_map = new HashMap<int,CCodeParameter> (direct_hash, direct_equal);
generate_cparameters (m, decl_space, cparam_map, new CCodeFunction ("fake"), vdeclarator, null, null, 1);
var vdecl = new CCodeDeclaration ("void");
vdecl.add_declarator (vdeclarator);
type_struct.add_declaration (vdecl);
// add vfunc field to the type struct
vdeclarator = new CCodeFunctionDeclarator (get_ccode_finish_vfunc_name (m));
cparam_map = new HashMap<int,CCodeParameter> (direct_hash, direct_equal);
generate_cparameters (m, decl_space, cparam_map, new CCodeFunction ("fake"), vdeclarator, null, null, 2);
vdecl = new CCodeDeclaration (get_ccode_name (creturn_type));
vdecl.add_declarator (vdeclarator);
type_struct.add_declaration (vdecl);
}
public override void visit_yield_statement (YieldStatement stmt) {
if (!is_in_coroutine ()) {
return;
}
if (stmt.yield_expression == null) {
int state = next_coroutine_state++;
ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), "_state_"), new CCodeConstant (state.to_string ()));
ccode.add_return (new CCodeConstant ("FALSE"));
ccode.add_label ("_state_%d".printf (state));
ccode.add_statement (new CCodeEmptyStatement ());
return;
}
if (stmt.yield_expression.error) {
stmt.error = true;
return;
}
ccode.add_expression (get_cvalue (stmt.yield_expression));
if (stmt.tree_can_fail && stmt.yield_expression.tree_can_fail) {
// simple case, no node breakdown necessary
add_simple_check (stmt.yield_expression);
}
/* free temporary objects */
foreach (var value in temp_ref_values) {
ccode.add_expression (destroy_value (value));
}
temp_ref_values.clear ();
}
public override void return_with_exception (CCodeExpression error_expr)
{
if (!is_in_coroutine ()) {
base.return_with_exception (error_expr);
return;
}
var set_error = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_set_from_error"));
set_error.add_argument (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data_"), "_async_result"));
set_error.add_argument (error_expr);
ccode.add_expression (set_error);
var free_error = new CCodeFunctionCall (new CCodeIdentifier ("g_error_free"));
free_error.add_argument (error_expr);
ccode.add_expression (free_error);
append_local_free (current_symbol, false);
complete_async ();
}
public override void visit_return_statement (ReturnStatement stmt) {
base.visit_return_statement (stmt);
if (!is_in_coroutine ()) {
return;
}
complete_async ();
}
public override 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) {
if (m.coroutine) {
decl_space.add_include ("gio/gio.h");
if (direction == 1) {
cparam_map.set (get_param_pos (-1), new CCodeParameter ("_callback_", "GAsyncReadyCallback"));
cparam_map.set (get_param_pos (-0.9), new CCodeParameter ("_user_data_", "gpointer"));
if (carg_map != null) {
carg_map.set (get_param_pos (-1), new CCodeIdentifier ("_callback_"));
carg_map.set (get_param_pos (-0.9), new CCodeIdentifier ("_user_data_"));
}
} else if (direction == 2) {
cparam_map.set (get_param_pos (0.1), new CCodeParameter ("_res_", "GAsyncResult*"));
if (carg_map != null) {
carg_map.set (get_param_pos (0.1), new CCodeIdentifier ("_res_"));
}
}
}
base.generate_cparameters (m, decl_space, cparam_map, func, vdeclarator, carg_map, vcall, direction);
}
public string generate_async_callback_wrapper () {
string async_callback_wrapper_func = "_vala_g_async_ready_callback";
if (!add_wrapper (async_callback_wrapper_func)) {
return async_callback_wrapper_func;
}
var function = new CCodeFunction (async_callback_wrapper_func, "void");
function.modifiers = CCodeModifiers.STATIC;
function.add_parameter (new CCodeParameter ("*source_object", "GObject"));
function.add_parameter (new CCodeParameter ("*res", "GAsyncResult"));
function.add_parameter (new CCodeParameter ("*user_data", "void"));
push_function (function);
var res_ref = new CCodeFunctionCall (new CCodeIdentifier ("g_object_ref"));
res_ref.add_argument (new CCodeIdentifier ("res"));
// store reference to async result of inner async function in out async result
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_set_op_res_gpointer"));
ccall.add_argument (new CCodeIdentifier ("user_data"));
ccall.add_argument (res_ref);
ccall.add_argument (new CCodeIdentifier ("g_object_unref"));
ccode.add_expression (ccall);
// call user-provided callback
ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_complete"));
ccall.add_argument (new CCodeIdentifier ("user_data"));
ccode.add_expression (ccall);
// free async result
ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_object_unref"));
ccall.add_argument (new CCodeIdentifier ("user_data"));
ccode.add_expression (ccall);
pop_function ();
cfile.add_function_declaration (function);
cfile.add_function (function);
return async_callback_wrapper_func;
}
}
|