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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
/* valamethodcall.vala
*
* Copyright (C) 2006-2012 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;
/**
* Represents an invocation expression in the source code.
*/
public class Vala.MethodCall : Expression {
/**
* The method to call.
*/
public Expression call {
get { return _call; }
set {
_call = value;
_call.parent_node = this;
}
}
public bool is_yield_expression { get; set; }
public bool is_assert { get; private set; }
/**
* Whether this chain up uses the constructv function with va_list.
*/
public bool is_constructv_chainup { get; private set; }
public bool is_chainup { get; private set; }
private Expression _call;
private List<Expression> argument_list = new ArrayList<Expression> ();
/**
* Creates a new invocation expression.
*
* @param call method to call
* @param source_reference reference to source code
* @return newly created invocation expression
*/
public MethodCall (Expression call, SourceReference? source_reference = null) {
this.source_reference = source_reference;
this.call = call;
}
/**
* Appends the specified expression to the list of arguments.
*
* @param arg an argument
*/
public void add_argument (Expression arg) {
argument_list.add (arg);
arg.parent_node = this;
}
/**
* Returns a copy of the argument list.
*
* @return argument list
*/
public List<Expression> get_argument_list () {
return argument_list;
}
public override void accept (CodeVisitor visitor) {
visitor.visit_method_call (this);
visitor.visit_expression (this);
}
public override void accept_children (CodeVisitor visitor) {
call.accept (visitor);
foreach (Expression expr in argument_list) {
expr.accept (visitor);
}
}
public override void replace_expression (Expression old_node, Expression new_node) {
if (call == old_node) {
call = new_node;
}
int index = argument_list.index_of (old_node);
if (index >= 0 && new_node.parent_node == null) {
argument_list[index] = new_node;
new_node.parent_node = this;
}
}
public override bool is_constant () {
var method_type = call.value_type as MethodType;
if (method_type != null) {
// N_ and NC_ do not have any effect on the C code,
// they are only interpreted by xgettext
// this means that it is ok to use them in constant initializers
if (method_type.method_symbol.get_full_name () == "GLib.N_") {
// first argument is string
return argument_list[0].is_constant ();
} else if (method_type.method_symbol.get_full_name () == "GLib.NC_") {
// second argument is string
return argument_list[1].is_constant ();
}
}
return false;
}
public override bool is_pure () {
return false;
}
public override bool is_accessible (Symbol sym) {
foreach (var arg in argument_list) {
if (!arg.is_accessible (sym)) {
return false;
}
}
return call.is_accessible (sym);
}
public override bool check (CodeContext context) {
if (checked) {
return !error;
}
checked = true;
if (!call.check (context)) {
/* if method resolving didn't succeed, skip this check */
error = true;
return false;
}
// type of target object
DataType target_object_type = null;
List<DataType> method_type_args = null;
if (call.value_type is DelegateType) {
// delegate invocation, resolve generic types relative to delegate
target_object_type = call.value_type;
} else if (call is MemberAccess) {
var ma = (MemberAccess) call;
if (ma.prototype_access) {
error = true;
Report.error (source_reference, "Access to instance member `%s' denied".printf (call.symbol_reference.get_full_name ()));
return false;
}
method_type_args = ma.get_type_arguments ();
if (ma.inner != null) {
target_object_type = ma.inner.value_type;
// foo is relevant instance in foo.bar.connect (on_bar)
if (ma.inner.symbol_reference is Signal) {
var sig = ma.inner as MemberAccess;
if (sig != null) {
target_object_type = sig.inner.value_type;
}
}
// foo is relevant instance in foo.bar.begin (bar_ready) and foo.bar.end (result)
var m = ma.symbol_reference as Method;
if (m != null && m.coroutine) {
// begin or end call of async method
if (ma.member_name == "begin" || ma.member_name == "end") {
var method_access = ma.inner as MemberAccess;
if (method_access != null && method_access.inner != null) {
target_object_type = method_access.inner.value_type;
} else {
// static method
target_object_type = null;
}
}
}
}
if (ma.symbol_reference != null && ma.symbol_reference.get_attribute ("Assert") != null) {
this.is_assert = true;
var args = get_argument_list ();
if (args.size == 1) {
this.source_reference = args[0].source_reference;
}
}
}
var mtype = call.value_type;
var gobject_chainup = (context.profile == Profile.GOBJECT && call.symbol_reference == context.analyzer.object_type);
is_chainup = gobject_chainup;
if (!gobject_chainup) {
var expr = call;
var ma = expr as MemberAccess;
if (ma != null && ma.symbol_reference is CreationMethod) {
expr = ma.inner;
ma = expr as MemberAccess;
}
if (ma != null && ma.member_name == "this") {
// this[.with_foo] ()
is_chainup = true;
} else if (expr is BaseAccess) {
// base[.with_foo] ()
is_chainup = true;
}
}
CreationMethod base_cm = null;
if (is_chainup) {
var cm = context.analyzer.find_current_method () as CreationMethod;
if (cm == null) {
error = true;
Report.error (source_reference, "invocation not supported in this context");
return false;
} else if (cm.chain_up) {
error = true;
Report.error (source_reference, "Multiple constructor calls in the same constructor are not permitted");
return false;
}
cm.chain_up = true;
if (mtype is ObjectType) {
var otype = (ObjectType) mtype;
var cl = (Class) otype.type_symbol;
base_cm = cl.default_construction_method;
if (base_cm == null) {
error = true;
Report.error (source_reference, "chain up to `%s' not supported".printf (cl.get_full_name ()));
return false;
} else if (!base_cm.has_construct_function) {
error = true;
Report.error (source_reference, "chain up to `%s' not supported".printf (base_cm.get_full_name ()));
return false;
}
} else if (call.symbol_reference is CreationMethod && call.symbol_reference.parent_symbol is Class) {
base_cm = (CreationMethod) call.symbol_reference;
if (!base_cm.has_construct_function) {
error = true;
Report.error (source_reference, "chain up to `%s' not supported".printf (base_cm.get_full_name ()));
return false;
}
} else if (gobject_chainup) {
var cl = cm.parent_symbol as Class;
if (cl == null || !cl.is_subtype_of (context.analyzer.object_type)) {
error = true;
Report.error (source_reference, "chain up to `GLib.Object' not supported");
return false;
}
call.value_type = new ObjectType (context.analyzer.object_type);
mtype = call.value_type;
}
}
// check for struct construction
if (call is MemberAccess &&
((call.symbol_reference is CreationMethod
&& call.symbol_reference.parent_symbol is Struct)
|| call.symbol_reference is Struct)) {
var st = call.symbol_reference as Struct;
if (st != null && st.default_construction_method == null && (st.is_boolean_type () || st.is_integer_type () || st.is_floating_type ())) {
error = true;
Report.error (source_reference, "invocation not supported in this context");
return false;
}
var struct_creation_expression = new ObjectCreationExpression ((MemberAccess) call, source_reference);
struct_creation_expression.struct_creation = true;
foreach (Expression arg in get_argument_list ()) {
struct_creation_expression.add_argument (arg);
}
struct_creation_expression.target_type = target_type;
context.analyzer.replaced_nodes.add (this);
parent_node.replace_expression (this, struct_creation_expression);
struct_creation_expression.check (context);
return true;
} else if (!is_chainup && call is MemberAccess && call.symbol_reference is CreationMethod) {
error = true;
Report.error (source_reference, "use `new' operator to create new objects");
return false;
}
if (!is_chainup && mtype is ObjectType) {
// prevent funny stuff like (new Object ()) ()
error = true;
Report.error (source_reference, "invocation not supported in this context");
return false;
} else if (mtype != null && mtype.is_invokable ()) {
// call ok, expression is invokable
} else if (call.symbol_reference is Class) {
error = true;
Report.error (source_reference, "use `new' operator to create new objects");
return false;
} else {
error = true;
Report.error (source_reference, "invocation not supported in this context");
return false;
}
var ret_type = mtype.get_return_type ();
var params = mtype.get_parameters ();
if (mtype is MethodType) {
var m = ((MethodType) mtype).method_symbol;
if (m != null && m.coroutine) {
var ma = (MemberAccess) call;
if (!is_yield_expression) {
// begin or end call of async method
if (ma.member_name != "end") {
// begin (possibly implicit)
if (ma.member_name != "begin") {
Report.deprecated (ma.source_reference, "implicit .begin is deprecated");
}
params = m.get_async_begin_parameters ();
ret_type = new VoidType ();
} else {
// end
params = m.get_async_end_parameters ();
}
} else if (ma.member_name == "begin" || ma.member_name == "end") {
error = true;
Report.error (ma.source_reference, "use of `%s' not allowed in yield statement".printf (ma.member_name));
}
}
if (m != null) {
var ma = (MemberAccess) call;
int n_type_params = m.get_type_parameters ().size;
int n_type_args = ma.get_type_arguments ().size;
if (n_type_args > 0 && n_type_args < n_type_params) {
error = true;
Report.error (ma.source_reference, "too few type arguments");
return false;
} else if (n_type_args > 0 && n_type_args > n_type_params) {
error = true;
Report.error (ma.source_reference, "too many type arguments");
return false;
}
}
}
// FIXME partial code duplication in ObjectCreationExpression.check
Expression last_arg = null;
var args = get_argument_list ();
Iterator<Expression> arg_it = args.iterator ();
foreach (Parameter param in params) {
if (!param.check (context)) {
error = true;
}
if (param.ellipsis) {
break;
}
if (param.params_array) {
var array_type = (ArrayType) param.variable_type;
while (arg_it.next ()) {
Expression arg = arg_it.get ();
/* store expected type for callback parameters */
arg.target_type = array_type.element_type;
arg.target_type.value_owned = array_type.value_owned;
}
break;
}
if (arg_it.next ()) {
Expression arg = arg_it.get ();
/* store expected type for callback parameters */
arg.formal_target_type = param.variable_type;
arg.target_type = arg.formal_target_type.get_actual_type (target_object_type, method_type_args, this);
last_arg = arg;
}
}
// concatenate stringified arguments for methods with attribute [Print]
if (mtype is MethodType && ((MethodType) mtype).method_symbol.get_attribute ("Print") != null) {
var template = new Template (source_reference);
foreach (Expression arg in argument_list) {
arg.parent_node = null;
template.add_expression (arg);
}
argument_list.clear ();
add_argument (template);
}
// printf arguments
if (mtype is MethodType && ((MethodType) mtype).method_symbol.printf_format) {
StringLiteral format_literal = null;
if (last_arg is NullLiteral) {
// do not replace explicit null
} else if (last_arg != null) {
// use last argument as format string
format_literal = StringLiteral.get_format_literal (last_arg);
if (format_literal == null && args.size == params.size - 1) {
// insert "%s" to avoid issues with embedded %
format_literal = new StringLiteral ("\"%s\"");
format_literal.target_type = context.analyzer.string_type.copy ();
argument_list.insert (args.size - 1, format_literal);
// recreate iterator and skip to right position
arg_it = argument_list.iterator ();
foreach (Parameter param in params) {
if (param.ellipsis) {
break;
}
arg_it.next ();
}
}
} else {
// use instance as format string for string.printf (...)
var ma = call as MemberAccess;
if (ma != null) {
format_literal = StringLiteral.get_format_literal (ma.inner);
}
}
if (format_literal != null) {
string format = format_literal.eval ();
if (!context.analyzer.check_print_format (format, arg_it, source_reference)) {
return false;
}
}
}
foreach (Expression arg in get_argument_list ()) {
arg.check (context);
}
if (ret_type is VoidType) {
// void return type
if (!(parent_node is ExpressionStatement)
&& !(parent_node is ForStatement)
&& !(parent_node is YieldStatement)) {
// A void method invocation can be in the initializer or
// iterator of a for statement
error = true;
Report.error (source_reference, "invocation of void method not allowed as expression");
return false;
}
}
formal_value_type = ret_type.copy ();
value_type = formal_value_type.get_actual_type (target_object_type, method_type_args, this);
bool may_throw = false;
if (mtype is MethodType) {
var m = ((MethodType) mtype).method_symbol;
if (is_yield_expression) {
if (!m.coroutine) {
error = true;
Report.error (source_reference, "yield expression requires async method");
}
if (context.analyzer.current_method == null || !context.analyzer.current_method.coroutine) {
error = true;
Report.error (source_reference, "yield expression not available outside async method");
}
}
if (m != null && m.coroutine && !is_yield_expression && ((MemberAccess) call).member_name != "end") {
// .begin call of async method, no error can happen here
} else {
foreach (DataType error_type in m.get_error_types ()) {
may_throw = true;
// ensure we can trace back which expression may throw errors of this type
var call_error_type = error_type.copy ();
call_error_type.source_reference = source_reference;
add_error_type (call_error_type);
}
}
if (m.returns_floating_reference) {
value_type.floating_reference = true;
}
if (m.returns_modified_pointer) {
((MemberAccess) call).inner.lvalue = true;
}
// avoid passing possible null to ref_sink_function without checking
if (may_throw && !value_type.nullable && value_type.floating_reference && ret_type is ObjectType) {
value_type.nullable = true;
}
var dynamic_sig = m.parent_symbol as DynamicSignal;
if (dynamic_sig != null && dynamic_sig.handler != null) {
dynamic_sig.return_type = dynamic_sig.handler.value_type.get_return_type ().copy ();
bool first = true;
foreach (Parameter param in dynamic_sig.handler.value_type.get_parameters ()) {
if (first) {
// skip sender parameter
first = false;
} else {
dynamic_sig.add_parameter (param.copy ());
}
}
dynamic_sig.handler.target_type = new DelegateType (dynamic_sig.get_delegate (new ObjectType ((ObjectTypeSymbol) dynamic_sig.parent_symbol), this));
}
if (m != null && m.get_type_parameters ().size > 0) {
var ma = (MemberAccess) call;
if (ma.get_type_arguments ().size == 0) {
// infer type arguments
foreach (var type_param in m.get_type_parameters ()) {
DataType type_arg = null;
// infer type arguments from arguments
arg_it = args.iterator ();
foreach (Parameter param in params) {
if (param.ellipsis || param.params_array) {
break;
}
if (arg_it.next ()) {
Expression arg = arg_it.get ();
type_arg = param.variable_type.infer_type_argument (type_param, arg.value_type);
if (type_arg != null) {
break;
}
arg.target_type = arg.formal_target_type.get_actual_type (target_object_type, method_type_args, this);
}
}
// infer type arguments from expected return type
if (type_arg == null && target_type != null) {
type_arg = m.return_type.infer_type_argument (type_param, target_type);
}
if (type_arg == null) {
error = true;
Report.error (ma.source_reference, "cannot infer generic type argument for type parameter `%s'".printf (type_param.get_full_name ()));
return false;
}
ma.add_type_argument (type_arg);
}
// recalculate argument target types with new information
arg_it = args.iterator ();
foreach (Parameter param in params) {
if (param.ellipsis || param.params_array) {
break;
}
if (arg_it.next ()) {
Expression arg = arg_it.get ();
arg.target_type = arg.formal_target_type.get_actual_type (target_object_type, method_type_args, this);
}
}
// recalculate return value type with new information
value_type = formal_value_type.get_actual_type (target_object_type, method_type_args, this);
}
}
// replace method-type if needed for proper argument-check in semantic-analyser
if (m != null && m.coroutine) {
var ma = (MemberAccess) call;
if (ma.member_name == "end") {
mtype = new MethodType (m.get_end_method ());
}
}
} else if (mtype is ObjectType) {
// constructor
var cl = (Class) ((ObjectType) mtype).type_symbol;
var m = cl.default_construction_method;
foreach (DataType error_type in m.get_error_types ()) {
may_throw = true;
// ensure we can trace back which expression may throw errors of this type
var call_error_type = error_type.copy ();
call_error_type.source_reference = source_reference;
add_error_type (call_error_type);
}
} else if (mtype is DelegateType) {
var d = ((DelegateType) mtype).delegate_symbol;
foreach (DataType error_type in d.get_error_types ()) {
may_throw = true;
// ensure we can trace back which expression may throw errors of this type
var call_error_type = error_type.copy ();
call_error_type.source_reference = source_reference;
add_error_type (call_error_type);
}
}
if (!context.analyzer.check_arguments (this, mtype, params, get_argument_list ())) {
error = true;
return false;
}
/* Check for constructv chain up */
if (base_cm != null && base_cm.is_variadic () && args.size == base_cm.get_parameters ().size) {
var this_last_arg = args[args.size-1];
if (this_last_arg.value_type is StructValueType && this_last_arg.value_type.data_type == context.analyzer.va_list_type.data_type) {
is_constructv_chainup = true;
}
}
if (may_throw) {
if (parent_node is LocalVariable || parent_node is ExpressionStatement) {
// simple statements, no side effects after method call
} else if (!(context.analyzer.current_symbol is Block)) {
// can't handle errors in field initializers
Report.error (source_reference, "Field initializers must not throw errors");
} else {
// store parent_node as we need to replace the expression in the old parent node later on
var old_parent_node = parent_node;
var local = new LocalVariable (value_type.copy (), get_temp_name (), null, source_reference);
var decl = new DeclarationStatement (local, source_reference);
insert_statement (context.analyzer.insert_block, decl);
var temp_access = SemanticAnalyzer.create_temp_access (local, target_type);
// don't set initializer earlier as this changes parent_node and parent_statement
local.initializer = this;
decl.check (context);
// move temp variable to insert block to ensure the
// variable is in the same block as the declaration
// otherwise there will be scoping issues in the generated code
var block = (Block) context.analyzer.current_symbol;
block.remove_local_variable (local);
context.analyzer.insert_block.add_local_variable (local);
old_parent_node.replace_expression (this, temp_access);
temp_access.check (context);
}
}
return !error;
}
public override void emit (CodeGenerator codegen) {
var method_type = call.value_type as MethodType;
if (method_type != null && method_type.method_symbol.parent_symbol is Signal) {
var signal_access = ((MemberAccess) call).inner;
signal_access.emit (codegen);
} else {
call.emit (codegen);
}
foreach (Expression expr in argument_list) {
expr.emit (codegen);
}
codegen.visit_method_call (this);
codegen.visit_expression (this);
}
public override void get_defined_variables (Collection<Variable> collection) {
call.get_defined_variables (collection);
foreach (Expression arg in argument_list) {
arg.get_defined_variables (collection);
}
}
public override void get_used_variables (Collection<Variable> collection) {
call.get_used_variables (collection);
foreach (Expression arg in argument_list) {
arg.get_used_variables (collection);
}
}
public StringLiteral? get_format_literal () {
var mtype = this.call.value_type as MethodType;
if (mtype != null) {
int format_arg = mtype.method_symbol.get_format_arg_index ();
if (format_arg >= 0 && format_arg < argument_list.size) {
return StringLiteral.get_format_literal (argument_list[format_arg]);
}
}
return null;
}
}
|