compiler: permit inlining receive expressions
[gcc.git] / gcc / go / gofrontend / expressions.h
1 // expressions.h -- Go frontend expression handling. -*- C++ -*-
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 #ifndef GO_EXPRESSIONS_H
8 #define GO_EXPRESSIONS_H
9
10 #include <mpfr.h>
11 #include <mpc.h>
12
13 #include "operator.h"
14 #include "runtime.h"
15
16 class Gogo;
17 class Translate_context;
18 class Traverse;
19 class Statement_inserter;
20 class Type;
21 class Method;
22 struct Type_context;
23 class Integer_type;
24 class Float_type;
25 class Complex_type;
26 class Function_type;
27 class Map_type;
28 class Struct_type;
29 class Struct_field;
30 class Expression_list;
31 class Var_expression;
32 class Enclosed_var_expression;
33 class Temporary_reference_expression;
34 class Set_and_use_temporary_expression;
35 class String_expression;
36 class Type_conversion_expression;
37 class Unsafe_type_conversion_expression;
38 class Unary_expression;
39 class Binary_expression;
40 class String_concat_expression;
41 class Call_expression;
42 class Builtin_call_expression;
43 class Call_result_expression;
44 class Func_expression;
45 class Func_descriptor_expression;
46 class Unknown_expression;
47 class Index_expression;
48 class Array_index_expression;
49 class String_index_expression;
50 class Map_index_expression;
51 class Bound_method_expression;
52 class Field_reference_expression;
53 class Interface_field_reference_expression;
54 class Allocation_expression;
55 class Composite_literal_expression;
56 class Struct_construction_expression;
57 class Array_construction_expression;
58 class Fixed_array_construction_expression;
59 class Slice_construction_expression;
60 class Map_construction_expression;
61 class Type_guard_expression;
62 class Heap_expression;
63 class Receive_expression;
64 class Slice_value_expression;
65 class Conditional_expression;
66 class Compound_expression;
67 class Numeric_constant;
68 class Named_object;
69 class Export_function_body;
70 class Import_expression;
71 class Temporary_statement;
72 class Label;
73 class Ast_dump_context;
74 class String_dump;
75
76 // The precision to use for complex values represented as an mpc_t.
77 const int mpc_precision = 256;
78
79 // The base class for all expressions.
80
81 class Expression
82 {
83 public:
84 // The types of expressions.
85 enum Expression_classification
86 {
87 EXPRESSION_ERROR,
88 EXPRESSION_TYPE,
89 EXPRESSION_UNARY,
90 EXPRESSION_BINARY,
91 EXPRESSION_STRING_CONCAT,
92 EXPRESSION_CONST_REFERENCE,
93 EXPRESSION_VAR_REFERENCE,
94 EXPRESSION_ENCLOSED_VAR_REFERENCE,
95 EXPRESSION_TEMPORARY_REFERENCE,
96 EXPRESSION_SET_AND_USE_TEMPORARY,
97 EXPRESSION_SINK,
98 EXPRESSION_FUNC_REFERENCE,
99 EXPRESSION_FUNC_DESCRIPTOR,
100 EXPRESSION_FUNC_CODE_REFERENCE,
101 EXPRESSION_UNKNOWN_REFERENCE,
102 EXPRESSION_BOOLEAN,
103 EXPRESSION_STRING,
104 EXPRESSION_STRING_INFO,
105 EXPRESSION_STRING_VALUE,
106 EXPRESSION_INTEGER,
107 EXPRESSION_FLOAT,
108 EXPRESSION_COMPLEX,
109 EXPRESSION_NIL,
110 EXPRESSION_IOTA,
111 EXPRESSION_CALL,
112 EXPRESSION_CALL_RESULT,
113 EXPRESSION_BOUND_METHOD,
114 EXPRESSION_INDEX,
115 EXPRESSION_ARRAY_INDEX,
116 EXPRESSION_STRING_INDEX,
117 EXPRESSION_MAP_INDEX,
118 EXPRESSION_SELECTOR,
119 EXPRESSION_FIELD_REFERENCE,
120 EXPRESSION_INTERFACE_FIELD_REFERENCE,
121 EXPRESSION_ALLOCATION,
122 EXPRESSION_TYPE_GUARD,
123 EXPRESSION_CONVERSION,
124 EXPRESSION_UNSAFE_CONVERSION,
125 EXPRESSION_STRUCT_CONSTRUCTION,
126 EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
127 EXPRESSION_SLICE_CONSTRUCTION,
128 EXPRESSION_MAP_CONSTRUCTION,
129 EXPRESSION_COMPOSITE_LITERAL,
130 EXPRESSION_HEAP,
131 EXPRESSION_RECEIVE,
132 EXPRESSION_TYPE_DESCRIPTOR,
133 EXPRESSION_GC_SYMBOL,
134 EXPRESSION_PTRMASK_SYMBOL,
135 EXPRESSION_TYPE_INFO,
136 EXPRESSION_SLICE_INFO,
137 EXPRESSION_SLICE_VALUE,
138 EXPRESSION_INTERFACE_INFO,
139 EXPRESSION_INTERFACE_VALUE,
140 EXPRESSION_INTERFACE_MTABLE,
141 EXPRESSION_STRUCT_FIELD_OFFSET,
142 EXPRESSION_LABEL_ADDR,
143 EXPRESSION_CONDITIONAL,
144 EXPRESSION_COMPOUND,
145 EXPRESSION_BACKEND
146 };
147
148 Expression(Expression_classification, Location);
149
150 virtual ~Expression();
151
152 // Make an error expression. This is used when a parse error occurs
153 // to prevent cascading errors.
154 static Expression*
155 make_error(Location);
156
157 // Make an expression which is really a type. This is used during
158 // parsing.
159 static Expression*
160 make_type(Type*, Location);
161
162 // Make a unary expression.
163 static Expression*
164 make_unary(Operator, Expression*, Location);
165
166 // Make a binary expression.
167 static Expression*
168 make_binary(Operator, Expression*, Expression*, Location);
169
170 // Make a string concatenation expression.
171 static Expression*
172 make_string_concat(Expression_list*);
173
174 // Make a reference to a constant in an expression.
175 static Expression*
176 make_const_reference(Named_object*, Location);
177
178 // Make a reference to a variable in an expression.
179 static Expression*
180 make_var_reference(Named_object*, Location);
181
182 // Make a reference to a variable within an enclosing function.
183 static Expression*
184 make_enclosing_var_reference(Expression*, Named_object*, Location);
185
186 // Make a reference to a temporary variable. Temporary variables
187 // are always created by a single statement, which is what we use to
188 // refer to them.
189 static Temporary_reference_expression*
190 make_temporary_reference(Temporary_statement*, Location);
191
192 // Make an expressions which sets a temporary variable and then
193 // evaluates to a reference to that temporary variable. This is
194 // used to set a temporary variable while retaining the order of
195 // evaluation.
196 static Set_and_use_temporary_expression*
197 make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
198
199 // Make a sink expression--a reference to the blank identifier _.
200 static Expression*
201 make_sink(Location);
202
203 // Make a reference to a function in an expression. This returns a
204 // pointer to the struct holding the address of the function
205 // followed by any closed-over variables.
206 static Expression*
207 make_func_reference(Named_object*, Expression* closure, Location);
208
209 // Make a function descriptor, an immutable struct with a single
210 // field that points to the function code. This may only be used
211 // with functions that do not have closures. FN is the function for
212 // which we are making the descriptor.
213 static Func_descriptor_expression*
214 make_func_descriptor(Named_object* fn);
215
216 // Make a reference to the code of a function. This is used to set
217 // descriptor and closure fields.
218 static Expression*
219 make_func_code_reference(Named_object*, Location);
220
221 // Make a reference to an unknown name. In a correct program this
222 // will always be lowered to a real const/var/func reference.
223 static Unknown_expression*
224 make_unknown_reference(Named_object*, Location);
225
226 // Make a constant bool expression.
227 static Expression*
228 make_boolean(bool val, Location);
229
230 // Make a constant string expression.
231 static Expression*
232 make_string(const std::string&, Location);
233
234 // Make a constant string expression with a specific string subtype.
235 static Expression*
236 make_string_typed(const std::string&, Type*, Location);
237
238 // Make an expression that evaluates to some characteristic of an string.
239 // For simplicity, the enum values must match the field indexes in the
240 // underlying struct.
241 enum String_info
242 {
243 // The underlying data in the string.
244 STRING_INFO_DATA,
245 // The length of the string.
246 STRING_INFO_LENGTH
247 };
248
249 static Expression*
250 make_string_info(Expression* string, String_info, Location);
251
252 // Make an expression for a string value.
253 static Expression*
254 make_string_value(Expression* valptr, Expression* len, Location);
255
256 // Make a character constant expression. TYPE should be NULL for an
257 // abstract type.
258 static Expression*
259 make_character(const mpz_t*, Type*, Location);
260
261 // Make a constant integer expression from a multi-precision
262 // integer. TYPE should be NULL for an abstract type.
263 static Expression*
264 make_integer_z(const mpz_t*, Type*, Location);
265
266 // Make a constant integer expression from an unsigned long. TYPE
267 // should be NULL for an abstract type.
268 static Expression*
269 make_integer_ul(unsigned long, Type*, Location);
270
271 // Make a constant integer expression from a signed long. TYPE
272 // should be NULL for an abstract type.
273 static Expression*
274 make_integer_sl(long, Type*, Location);
275
276 // Make a constant integer expression from an int64_t. TYPE should
277 // be NULL for an abstract type.
278 static Expression*
279 make_integer_int64(int64_t, Type*, Location);
280
281 // Make a constant float expression. TYPE should be NULL for an
282 // abstract type.
283 static Expression*
284 make_float(const mpfr_t*, Type*, Location);
285
286 // Make a constant complex expression. TYPE should be NULL for an
287 // abstract type.
288 static Expression*
289 make_complex(const mpc_t*, Type*, Location);
290
291 // Make a nil expression.
292 static Expression*
293 make_nil(Location);
294
295 // Make an iota expression. This is used for the predeclared
296 // constant iota.
297 static Expression*
298 make_iota();
299
300 // Make a call expression.
301 static Call_expression*
302 make_call(Expression* func, Expression_list* args, bool is_varargs,
303 Location);
304
305 // Make a reference to a specific result of a call expression which
306 // returns a tuple.
307 static Expression*
308 make_call_result(Call_expression*, unsigned int index);
309
310 // Make an expression which is a method bound to its first
311 // parameter. METHOD is the method being called, FUNCTION is the
312 // function to call.
313 static Bound_method_expression*
314 make_bound_method(Expression* object, const Method* method,
315 Named_object* function, Location);
316
317 // Make an index or slice expression. This is a parser expression
318 // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
319 // index rather than a slice. CAP may be NULL, meaning we use the default
320 // capacity of LEFT. At parse time we may not know the type of LEFT.
321 // After parsing this is lowered to an array index, a string index,
322 // or a map index.
323 static Expression*
324 make_index(Expression* left, Expression* start, Expression* end,
325 Expression* cap, Location);
326
327 // Make an array index expression. END may be NULL, in which case
328 // this is an lvalue. CAP may be NULL, in which case it defaults
329 // to cap(ARRAY).
330 static Expression*
331 make_array_index(Expression* array, Expression* start, Expression* end,
332 Expression* cap, Location);
333
334 // Make a string index expression. END may be NULL. This is never
335 // an lvalue.
336 static Expression*
337 make_string_index(Expression* string, Expression* start, Expression* end,
338 Location);
339
340 // Make a map index expression. This is an lvalue.
341 static Map_index_expression*
342 make_map_index(Expression* map, Expression* val, Location);
343
344 // Make a selector. This is a parser expression which represents
345 // LEFT.NAME. At parse time we may not know the type of the left
346 // hand side.
347 static Expression*
348 make_selector(Expression* left, const std::string& name, Location);
349
350 // Make a reference to a field in a struct.
351 static Field_reference_expression*
352 make_field_reference(Expression*, unsigned int field_index, Location);
353
354 // Make a reference to a field of an interface, with an associated
355 // object.
356 static Expression*
357 make_interface_field_reference(Expression*, const std::string&,
358 Location);
359
360 // Make an allocation expression.
361 static Expression*
362 make_allocation(Type*, Location);
363
364 // Make a type guard expression.
365 static Expression*
366 make_type_guard(Expression*, Type*, Location);
367
368 // Make a type cast expression.
369 static Expression*
370 make_cast(Type*, Expression*, Location);
371
372 // Make an unsafe type cast expression. This is only used when
373 // passing parameter to builtin functions that are part of the Go
374 // runtime.
375 static Expression*
376 make_unsafe_cast(Type*, Expression*, Location);
377
378 // Make a composite literal. The DEPTH parameter is how far down we
379 // are in a list of composite literals with omitted types. HAS_KEYS
380 // is true if the expression list has keys alternating with values.
381 // ALL_ARE_NAMES is true if all the keys could be struct field
382 // names.
383 static Expression*
384 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
385 bool all_are_names, Location);
386
387 // Make a struct composite literal.
388 static Expression*
389 make_struct_composite_literal(Type*, Expression_list*, Location);
390
391 // Make an array composite literal.
392 static Expression*
393 make_array_composite_literal(Type*, Expression_list*, Location);
394
395 // Make a slice composite literal.
396 static Slice_construction_expression*
397 make_slice_composite_literal(Type*, Expression_list*, Location);
398
399 // Take an expression and allocate it on the heap.
400 static Expression*
401 make_heap_expression(Expression*, Location);
402
403 // Make a receive expression. VAL is NULL for a unary receive.
404 static Receive_expression*
405 make_receive(Expression* channel, Location);
406
407 // Make an expression which evaluates to the address of the type
408 // descriptor for TYPE.
409 static Expression*
410 make_type_descriptor(Type* type, Location);
411
412 // Make an expression which evaluates to the address of the gc
413 // symbol for TYPE.
414 static Expression*
415 make_gc_symbol(Type* type);
416
417 // Make an expression that evaluates to the address of a ptrmask
418 // symbol for TYPE. For most types this will be the same as
419 // make_gc_symbol, but for larger types make_gc_symbol will return a
420 // gcprog while this will return a ptrmask.
421 static Expression*
422 make_ptrmask_symbol(Type* type);
423
424 // Make an expression which evaluates to some characteristic of a
425 // type. These are only used for type descriptors, so there is no
426 // location parameter.
427 enum Type_info
428 {
429 // The size of a value of the type.
430 TYPE_INFO_SIZE,
431 // The required alignment of a value of the type.
432 TYPE_INFO_ALIGNMENT,
433 // The required alignment of a value of the type when used as a
434 // field in a struct.
435 TYPE_INFO_FIELD_ALIGNMENT,
436 // The size of the prefix of a value of the type that contains
437 // all the pointers. This is 0 for a type that contains no
438 // pointers. It is always <= TYPE_INFO_SIZE.
439 TYPE_INFO_BACKEND_PTRDATA,
440 // Like TYPE_INFO_BACKEND_PTRDATA, but the ptrdata value that we
441 // want to store in a type descriptor. They are the same for
442 // most types, but can differ for a type that uses a gcprog.
443 TYPE_INFO_DESCRIPTOR_PTRDATA
444 };
445
446 static Expression*
447 make_type_info(Type* type, Type_info);
448
449 // Make an expression that evaluates to some characteristic of a
450 // slice. For simplicity, the enum values must match the field indexes
451 // in the underlying struct.
452 enum Slice_info
453 {
454 // The underlying data of the slice.
455 SLICE_INFO_VALUE_POINTER,
456 // The length of the slice.
457 SLICE_INFO_LENGTH,
458 // The capacity of the slice.
459 SLICE_INFO_CAPACITY
460 };
461
462 static Expression*
463 make_slice_info(Expression* slice, Slice_info, Location);
464
465 // Make an expression for a slice value.
466 static Expression*
467 make_slice_value(Type*, Expression* valptr, Expression* len, Expression* cap,
468 Location);
469
470 // Make an expression that evaluates to some characteristic of an
471 // interface. For simplicity, the enum values must match the field indexes
472 // in the underlying struct.
473 enum Interface_info
474 {
475 // The type descriptor of an empty interface.
476 INTERFACE_INFO_TYPE_DESCRIPTOR = 0,
477 // The methods of an interface.
478 INTERFACE_INFO_METHODS = 0,
479 // The first argument to pass to an interface method.
480 INTERFACE_INFO_OBJECT
481 };
482
483 static Expression*
484 make_interface_info(Expression* iface, Interface_info, Location);
485
486 // Make an expression for an interface value.
487 static Expression*
488 make_interface_value(Type*, Expression*, Expression*, Location);
489
490 // Make an expression that builds a reference to the interface method table
491 // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
492 // reference to the interface method table for the pointer receiver type.
493 static Expression*
494 make_interface_mtable_ref(Interface_type* itype, Type* type,
495 bool is_pointer, Location);
496
497 // Make an expression which evaluates to the offset of a field in a
498 // struct. This is only used for type descriptors, so there is no
499 // location parameter.
500 static Expression*
501 make_struct_field_offset(Struct_type*, const Struct_field*);
502
503 // Make an expression which evaluates to the address of an unnamed
504 // label.
505 static Expression*
506 make_label_addr(Label*, Location);
507
508 // Make a conditional expression.
509 static Expression*
510 make_conditional(Expression*, Expression*, Expression*, Location);
511
512 // Make a compound expression.
513 static Expression*
514 make_compound(Expression*, Expression*, Location);
515
516 // Make a backend expression.
517 static Expression*
518 make_backend(Bexpression*, Type*, Location);
519
520 enum Nil_check_classification
521 {
522 // Use the default policy for deciding if this deref needs a check.
523 NIL_CHECK_DEFAULT,
524 // An explicit check is required for this dereference operation.
525 NIL_CHECK_NEEDED,
526 // No check needed for this dereference operation.
527 NIL_CHECK_NOT_NEEDED,
528 // A type error or error construct was encountered when determining
529 // whether this deref needs an explicit check.
530 NIL_CHECK_ERROR_ENCOUNTERED
531 };
532
533 // Make a dereference expression.
534 static Expression*
535 make_dereference(Expression*, Nil_check_classification, Location);
536
537 // Return the expression classification.
538 Expression_classification
539 classification() const
540 { return this->classification_; }
541
542 // Return the location of the expression.
543 Location
544 location() const
545 { return this->location_; }
546
547 // Return whether this is a constant expression.
548 bool
549 is_constant() const
550 { return this->do_is_constant(); }
551
552 // Return whether this is the zero value of its type.
553 bool
554 is_zero_value() const
555 { return this->do_is_zero_value(); }
556
557 // Return whether this expression can be used as a static
558 // initializer. This is true for an expression that has only
559 // numbers and pointers to global variables or composite literals
560 // that do not require runtime initialization. It is false if we
561 // must generate code to compute this expression when it is used to
562 // initialize a global variable. This is not a language-level
563 // concept, but an implementation-level one. If this expression is
564 // used to initialize a global variable, this is true if we can pass
565 // an initializer to the backend, false if we must generate code to
566 // initialize the variable. It is always safe for this method to
567 // return false, but the resulting code may be less efficient.
568 bool
569 is_static_initializer() const
570 { return this->do_is_static_initializer(); }
571
572 // If this is not a numeric constant, return false. If it is one,
573 // return true, and set VAL to hold the value.
574 bool
575 numeric_constant_value(Numeric_constant* val) const
576 { return this->do_numeric_constant_value(val); }
577
578 // If this is not a constant expression with string type, return
579 // false. If it is one, return true, and set VAL to the value.
580 bool
581 string_constant_value(std::string* val) const
582 { return this->do_string_constant_value(val); }
583
584 // This is called if the value of this expression is being
585 // discarded. This issues warnings about computed values being
586 // unused. This returns true if all is well, false if it issued an
587 // error message.
588 bool
589 discarding_value()
590 { return this->do_discarding_value(); }
591
592 // Return whether this is an error expression.
593 bool
594 is_error_expression() const
595 { return this->classification_ == EXPRESSION_ERROR; }
596
597 // Return whether this expression really represents a type.
598 bool
599 is_type_expression() const
600 { return this->classification_ == EXPRESSION_TYPE; }
601
602 // If this is a variable reference, return the Var_expression
603 // structure. Otherwise, return NULL. This is a controlled dynamic
604 // cast.
605 Var_expression*
606 var_expression()
607 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
608
609 const Var_expression*
610 var_expression() const
611 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
612
613 // If this is a enclosed_variable reference, return the
614 // Enclosed_var_expression structure. Otherwise, return NULL.
615 // This is a controlled dynamic cast.
616 Enclosed_var_expression*
617 enclosed_var_expression()
618 { return this->convert<Enclosed_var_expression,
619 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
620
621 const Enclosed_var_expression*
622 enclosed_var_expression() const
623 { return this->convert<const Enclosed_var_expression,
624 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
625
626
627 // If this is a reference to a temporary variable, return the
628 // Temporary_reference_expression. Otherwise, return NULL.
629 Temporary_reference_expression*
630 temporary_reference_expression()
631 {
632 return this->convert<Temporary_reference_expression,
633 EXPRESSION_TEMPORARY_REFERENCE>();
634 }
635
636 // If this is a set-and-use-temporary, return the
637 // Set_and_use_temporary_expression. Otherwise, return NULL.
638 Set_and_use_temporary_expression*
639 set_and_use_temporary_expression()
640 {
641 return this->convert<Set_and_use_temporary_expression,
642 EXPRESSION_SET_AND_USE_TEMPORARY>();
643 }
644
645 // Return whether this is a sink expression.
646 bool
647 is_sink_expression() const
648 { return this->classification_ == EXPRESSION_SINK; }
649
650 // If this is a string expression, return the String_expression
651 // structure. Otherwise, return NULL.
652 String_expression*
653 string_expression()
654 { return this->convert<String_expression, EXPRESSION_STRING>(); }
655
656 // If this is a conversion expression, return the Type_conversion_expression
657 // structure. Otherwise, return NULL.
658 Type_conversion_expression*
659 conversion_expression()
660 { return this->convert<Type_conversion_expression, EXPRESSION_CONVERSION>(); }
661
662 // If this is an unsafe conversion expression, return the
663 // Unsafe_type_conversion_expression structure. Otherwise, return NULL.
664 Unsafe_type_conversion_expression*
665 unsafe_conversion_expression()
666 {
667 return this->convert<Unsafe_type_conversion_expression,
668 EXPRESSION_UNSAFE_CONVERSION>();
669 }
670
671 // Return whether this is the expression nil.
672 bool
673 is_nil_expression() const
674 { return this->classification_ == EXPRESSION_NIL; }
675
676 // If this is an indirection through a pointer, return the
677 // expression being pointed through. Otherwise return this.
678 Expression*
679 deref();
680
681 // If this is a unary expression, return the Unary_expression
682 // structure. Otherwise return NULL.
683 Unary_expression*
684 unary_expression()
685 { return this->convert<Unary_expression, EXPRESSION_UNARY>(); }
686
687 // If this is a binary expression, return the Binary_expression
688 // structure. Otherwise return NULL.
689 Binary_expression*
690 binary_expression()
691 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
692
693 // If this is a string concatenation expression, return the
694 // String_concat_expression structure. Otherwise, return NULL.
695 String_concat_expression*
696 string_concat_expression()
697 {
698 return this->convert<String_concat_expression, EXPRESSION_STRING_CONCAT>();
699 }
700
701 // If this is a call expression, return the Call_expression
702 // structure. Otherwise, return NULL. This is a controlled dynamic
703 // cast.
704 Call_expression*
705 call_expression()
706 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
707
708 // If this is a call_result expression, return the Call_result_expression
709 // structure. Otherwise, return NULL. This is a controlled dynamic
710 // cast.
711 Call_result_expression*
712 call_result_expression()
713 { return this->convert<Call_result_expression, EXPRESSION_CALL_RESULT>(); }
714
715 // If this is an expression which refers to a function, return the
716 // Func_expression structure. Otherwise, return NULL.
717 Func_expression*
718 func_expression()
719 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
720
721 const Func_expression*
722 func_expression() const
723 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
724
725 // If this is an expression which refers to an unknown name, return
726 // the Unknown_expression structure. Otherwise, return NULL.
727 Unknown_expression*
728 unknown_expression()
729 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
730
731 const Unknown_expression*
732 unknown_expression() const
733 {
734 return this->convert<const Unknown_expression,
735 EXPRESSION_UNKNOWN_REFERENCE>();
736 }
737
738 // If this is an index expression, return the Index_expression
739 // structure. Otherwise, return NULL.
740 Index_expression*
741 index_expression()
742 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
743
744 // If this is an expression which refers to indexing in a array,
745 // return the Array_index_expression structure. Otherwise, return
746 // NULL.
747 Array_index_expression*
748 array_index_expression()
749 { return this->convert<Array_index_expression, EXPRESSION_ARRAY_INDEX>(); }
750
751 // If this is an expression which refers to indexing in a string,
752 // return the String_index_expression structure. Otherwise, return
753 // NULL.
754 String_index_expression*
755 string_index_expression()
756 { return this->convert<String_index_expression, EXPRESSION_STRING_INDEX>(); }
757
758 // If this is an expression which refers to indexing in a map,
759 // return the Map_index_expression structure. Otherwise, return
760 // NULL.
761 Map_index_expression*
762 map_index_expression()
763 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
764
765 // If this is a bound method expression, return the
766 // Bound_method_expression structure. Otherwise, return NULL.
767 Bound_method_expression*
768 bound_method_expression()
769 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
770
771 // If this is a reference to a field in a struct, return the
772 // Field_reference_expression structure. Otherwise, return NULL.
773 Field_reference_expression*
774 field_reference_expression()
775 {
776 return this->convert<Field_reference_expression,
777 EXPRESSION_FIELD_REFERENCE>();
778 }
779
780 // If this is a reference to a field in an interface, return the
781 // Interface_field_reference_expression structure. Otherwise,
782 // return NULL.
783 Interface_field_reference_expression*
784 interface_field_reference_expression()
785 {
786 return this->convert<Interface_field_reference_expression,
787 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
788 }
789
790 // If this is an allocation expression, return the Allocation_expression
791 // structure. Otherwise, return NULL.
792 Allocation_expression*
793 allocation_expression()
794 { return this->convert<Allocation_expression, EXPRESSION_ALLOCATION>(); }
795
796 // If this is a general composite literal, return the
797 // Composite_literal_expression structure. Otherwise, return NULL.
798 Composite_literal_expression*
799 complit()
800 {
801 return this->convert<Composite_literal_expression,
802 EXPRESSION_COMPOSITE_LITERAL>();
803 }
804
805 // If this is a struct composite literal, return the
806 // Struct_construction_expression structure. Otherwise, return NULL.
807 Struct_construction_expression*
808 struct_literal()
809 {
810 return this->convert<Struct_construction_expression,
811 EXPRESSION_STRUCT_CONSTRUCTION>();
812 }
813
814 // If this is a array composite literal, return the
815 // Array_construction_expression structure. Otherwise, return NULL.
816 Fixed_array_construction_expression*
817 array_literal()
818 {
819 return this->convert<Fixed_array_construction_expression,
820 EXPRESSION_FIXED_ARRAY_CONSTRUCTION>();
821 }
822
823 // If this is a slice composite literal, return the
824 // Slice_construction_expression structure. Otherwise, return NULL.
825 Slice_construction_expression*
826 slice_literal()
827 {
828 return this->convert<Slice_construction_expression,
829 EXPRESSION_SLICE_CONSTRUCTION>();
830 }
831
832 // If this is a map composite literal, return the
833 // Map_construction_expression structure. Otherwise, return NULL.
834 Map_construction_expression*
835 map_literal()
836 {
837 return this->convert<Map_construction_expression,
838 EXPRESSION_MAP_CONSTRUCTION>();
839 }
840
841 // If this is a type guard expression, return the
842 // Type_guard_expression structure. Otherwise, return NULL.
843 Type_guard_expression*
844 type_guard_expression()
845 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
846
847 // If this is a heap expression, returhn the Heap_expression structure.
848 // Otherwise, return NULL.
849 Heap_expression*
850 heap_expression()
851 { return this->convert<Heap_expression, EXPRESSION_HEAP>(); }
852
853 // If this is a receive expression, return the Receive_expression
854 // structure. Otherwise, return NULL.
855 Receive_expression*
856 receive_expression()
857 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
858
859 // If this is a slice value expression, return the Slice_valiue_expression
860 // structure. Otherwise, return NULL.
861 Slice_value_expression*
862 slice_value_expression()
863 { return this->convert<Slice_value_expression, EXPRESSION_SLICE_VALUE>(); }
864
865 // If this is a conditional expression, return the Conditional_expression
866 // structure. Otherwise, return NULL.
867 Conditional_expression*
868 conditional_expression()
869 { return this->convert<Conditional_expression, EXPRESSION_CONDITIONAL>(); }
870
871 // If this is a compound expression, return the Compound_expression structure.
872 // Otherwise, return NULL.
873 Compound_expression*
874 compound_expression()
875 { return this->convert<Compound_expression, EXPRESSION_COMPOUND>(); }
876
877 // Return true if this is a composite literal.
878 bool
879 is_composite_literal() const;
880
881 // Return true if this is a composite literal which is not constant.
882 bool
883 is_nonconstant_composite_literal() const;
884
885 // Return true if this is a variable or temporary variable.
886 bool
887 is_variable() const;
888
889 // Return true if this is a reference to a local variable.
890 bool
891 is_local_variable() const;
892
893 // Return true if two expressions refer to the same variable or
894 // struct field.
895 static bool
896 is_same_variable(Expression*, Expression*);
897
898 // Make the builtin function descriptor type, so that it can be
899 // converted.
900 static void
901 make_func_descriptor_type();
902
903 // Traverse an expression.
904 static int
905 traverse(Expression**, Traverse*);
906
907 // Traverse subexpressions of this expression.
908 int
909 traverse_subexpressions(Traverse*);
910
911 // Lower an expression. This is called immediately after parsing.
912 // FUNCTION is the function we are in; it will be NULL for an
913 // expression initializing a global variable. INSERTER may be used
914 // to insert statements before the statement or initializer
915 // containing this expression; it is normally used to create
916 // temporary variables. IOTA_VALUE is the value that we should give
917 // to any iota expressions. This function must resolve expressions
918 // which could not be fully parsed into their final form. It
919 // returns the same Expression or a new one.
920 Expression*
921 lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
922 int iota_value)
923 { return this->do_lower(gogo, function, inserter, iota_value); }
924
925 // Flatten an expression. This is called after order_evaluation.
926 // FUNCTION is the function we are in; it will be NULL for an
927 // expression initializing a global variable. INSERTER may be used
928 // to insert statements before the statement or initializer
929 // containing this expression; it is normally used to create
930 // temporary variables. This function must resolve expressions
931 // which could not be fully parsed into their final form. It
932 // returns the same Expression or a new one.
933 Expression*
934 flatten(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
935 { return this->do_flatten(gogo, function, inserter); }
936
937 // Determine the real type of an expression with abstract integer,
938 // floating point, or complex type. TYPE_CONTEXT describes the
939 // expected type.
940 void
941 determine_type(const Type_context*);
942
943 // Check types in an expression.
944 void
945 check_types(Gogo* gogo)
946 { this->do_check_types(gogo); }
947
948 // Determine the type when there is no context.
949 void
950 determine_type_no_context();
951
952 // Return the current type of the expression. This may be changed
953 // by determine_type.
954 Type*
955 type()
956 { return this->do_type(); }
957
958 // Return a copy of an expression.
959 Expression*
960 copy()
961 { return this->do_copy(); }
962
963 // Return the cost of this statement for inlining purposes.
964 int
965 inlining_cost() const
966 { return this->do_inlining_cost(); }
967
968 // Return whether the expression is addressable--something which may
969 // be used as the operand of the unary & operator.
970 bool
971 is_addressable() const
972 { return this->do_is_addressable(); }
973
974 // Note that we are taking the address of this expression. ESCAPES
975 // is true if this address escapes the current function.
976 void
977 address_taken(bool escapes)
978 { this->do_address_taken(escapes); }
979
980 // Note that a nil check must be issued for this expression.
981 void
982 issue_nil_check()
983 { this->do_issue_nil_check(); }
984
985 // Return whether this expression must be evaluated in order
986 // according to the order of evaluation rules. This is basically
987 // true of all expressions with side-effects.
988 bool
989 must_eval_in_order() const
990 { return this->do_must_eval_in_order(); }
991
992 // Return whether subexpressions of this expression must be
993 // evaluated in order. This is true of index expressions and
994 // pointer indirections. This sets *SKIP to the number of
995 // subexpressions to skip during traversing, as index expressions
996 // only requiring moving the index, not the array.
997 bool
998 must_eval_subexpressions_in_order(int* skip) const
999 {
1000 *skip = 0;
1001 return this->do_must_eval_subexpressions_in_order(skip);
1002 }
1003
1004 // Return the backend representation for this expression.
1005 Bexpression*
1006 get_backend(Translate_context*);
1007
1008 // Return an expression handling any conversions which must be done during
1009 // assignment.
1010 static Expression*
1011 convert_for_assignment(Gogo*, Type* lhs_type, Expression* rhs,
1012 Location location);
1013
1014 // Return an expression converting a value of one interface type to another
1015 // interface type. If FOR_TYPE_GUARD is true this is for a type
1016 // assertion.
1017 static Expression*
1018 convert_interface_to_interface(Type* lhs_type,
1019 Expression* rhs, bool for_type_guard,
1020 Location);
1021
1022 // Return a backend expression implementing the comparison LEFT OP RIGHT.
1023 // TYPE is the type of both sides.
1024 static Bexpression*
1025 comparison(Translate_context*, Type* result_type, Operator op,
1026 Expression* left, Expression* right, Location);
1027
1028 // Return the backend expression for the numeric constant VAL.
1029 static Bexpression*
1030 backend_numeric_constant_expression(Translate_context*,
1031 Numeric_constant* val);
1032
1033 // Export the expression.
1034 void
1035 export_expression(Export_function_body* efb) const
1036 { this->do_export(efb); }
1037
1038 // Import an expression. The location should be used for the
1039 // returned expression. Errors should be reported using the
1040 // Import's location method.
1041 static Expression*
1042 import_expression(Import_expression*, Location);
1043
1044 // Return an expression which checks that VAL, of arbitrary integer type,
1045 // is non-negative and is not more than the maximum integer value.
1046 static Expression*
1047 check_bounds(Expression* val, Location);
1048
1049 // Return an expression for constructing a direct interface type from a
1050 // pointer.
1051 static Expression*
1052 pack_direct_iface(Type*, Expression*, Location);
1053
1054 // Dump an expression to a dump constext.
1055 void
1056 dump_expression(Ast_dump_context*) const;
1057
1058 protected:
1059 // May be implemented by child class: traverse the expressions.
1060 virtual int
1061 do_traverse(Traverse*);
1062
1063 // Return a lowered expression.
1064 virtual Expression*
1065 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1066 { return this; }
1067
1068 // Return a flattened expression.
1069 virtual Expression*
1070 do_flatten(Gogo*, Named_object*, Statement_inserter*)
1071 { return this; }
1072
1073
1074 // Return whether this is a constant expression.
1075 virtual bool
1076 do_is_constant() const
1077 { return false; }
1078
1079 // Return whether this is the zero value of its type.
1080 virtual bool
1081 do_is_zero_value() const
1082 { return false; }
1083
1084 // Return whether this expression can be used as a constant
1085 // initializer.
1086 virtual bool
1087 do_is_static_initializer() const
1088 { return false; }
1089
1090 // Return whether this is a constant expression of numeric type, and
1091 // set the Numeric_constant to the value.
1092 virtual bool
1093 do_numeric_constant_value(Numeric_constant*) const
1094 { return false; }
1095
1096 // Return whether this is a constant expression of string type, and
1097 // set VAL to the value.
1098 virtual bool
1099 do_string_constant_value(std::string*) const
1100 { return false; }
1101
1102 // Called by the parser if the value is being discarded.
1103 virtual bool
1104 do_discarding_value();
1105
1106 // Child class holds type.
1107 virtual Type*
1108 do_type() = 0;
1109
1110 // Child class implements determining type information.
1111 virtual void
1112 do_determine_type(const Type_context*) = 0;
1113
1114 // Child class implements type checking if needed.
1115 virtual void
1116 do_check_types(Gogo*)
1117 { }
1118
1119 // Child class implements copying.
1120 virtual Expression*
1121 do_copy() = 0;
1122
1123 // Child class implements determining the cost of this statement for
1124 // inlining. The default cost is high, so we only need to define
1125 // this method for expressions that can be inlined.
1126 virtual int
1127 do_inlining_cost() const
1128 { return 0x100000; }
1129
1130 // Child class implements whether the expression is addressable.
1131 virtual bool
1132 do_is_addressable() const
1133 { return false; }
1134
1135 // Child class implements taking the address of an expression.
1136 virtual void
1137 do_address_taken(bool)
1138 { }
1139
1140 // Child class implements issuing a nil check if the address is taken.
1141 virtual void
1142 do_issue_nil_check()
1143 { }
1144
1145 // Child class implements whether this expression must be evaluated
1146 // in order.
1147 virtual bool
1148 do_must_eval_in_order() const
1149 { return false; }
1150
1151 // Child class implements whether this expressions requires that
1152 // subexpressions be evaluated in order. The child implementation
1153 // may set *SKIP if it should be non-zero.
1154 virtual bool
1155 do_must_eval_subexpressions_in_order(int* /* skip */) const
1156 { return false; }
1157
1158 // Child class implements conversion to backend representation.
1159 virtual Bexpression*
1160 do_get_backend(Translate_context*) = 0;
1161
1162 // Child class implements export.
1163 virtual void
1164 do_export(Export_function_body*) const;
1165
1166 // For children to call to give an error for an unused value.
1167 void
1168 unused_value_error();
1169
1170 // For children to call when they detect that they are in error.
1171 void
1172 set_is_error();
1173
1174 // For children to call to report an error conveniently.
1175 void
1176 report_error(const char*);
1177
1178 // Child class implements dumping to a dump context.
1179 virtual void
1180 do_dump_expression(Ast_dump_context*) const = 0;
1181
1182 // Varargs lowering creates a slice object (unnamed compiler temp)
1183 // to contain the variable length collection of values. The enum
1184 // below tells the lowering routine whether it can mark that temp
1185 // as non-escaping or not. For general varargs calls it is not always
1186 // safe to stack-allocated the storage, but for specific cases (ex:
1187 // call to append()) it is legal.
1188 enum Slice_storage_escape_disp
1189 {
1190 SLICE_STORAGE_MAY_ESCAPE,
1191 SLICE_STORAGE_DOES_NOT_ESCAPE
1192 };
1193
1194 private:
1195 // Convert to the desired statement classification, or return NULL.
1196 // This is a controlled dynamic cast.
1197 template<typename Expression_class,
1198 Expression_classification expr_classification>
1199 Expression_class*
1200 convert()
1201 {
1202 return (this->classification_ == expr_classification
1203 ? static_cast<Expression_class*>(this)
1204 : NULL);
1205 }
1206
1207 template<typename Expression_class,
1208 Expression_classification expr_classification>
1209 const Expression_class*
1210 convert() const
1211 {
1212 return (this->classification_ == expr_classification
1213 ? static_cast<const Expression_class*>(this)
1214 : NULL);
1215 }
1216
1217 static Expression*
1218 convert_type_to_interface(Type*, Expression*, Location);
1219
1220 static Expression*
1221 unpack_direct_iface(Expression*, Location);
1222
1223 static Expression*
1224 get_interface_type_descriptor(Expression*);
1225
1226 static Expression*
1227 convert_interface_to_type(Type*, Expression*, Location);
1228
1229 // The expression classification.
1230 Expression_classification classification_;
1231 // The location in the input file.
1232 Location location_;
1233 };
1234
1235 // A list of Expressions.
1236
1237 class Expression_list
1238 {
1239 public:
1240 Expression_list()
1241 : entries_()
1242 { }
1243
1244 // Return whether the list is empty.
1245 bool
1246 empty() const
1247 { return this->entries_.empty(); }
1248
1249 // Return the number of entries in the list.
1250 size_t
1251 size() const
1252 { return this->entries_.size(); }
1253
1254 // Add an entry to the end of the list.
1255 void
1256 push_back(Expression* expr)
1257 { this->entries_.push_back(expr); }
1258
1259 void
1260 append(Expression_list* add)
1261 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
1262
1263 // Reserve space in the list.
1264 void
1265 reserve(size_t size)
1266 { this->entries_.reserve(size); }
1267
1268 // Traverse the expressions in the list.
1269 int
1270 traverse(Traverse*);
1271
1272 // Copy the list.
1273 Expression_list*
1274 copy();
1275
1276 // Return true if the list contains an error expression.
1277 bool
1278 contains_error() const;
1279
1280 // Retrieve an element by index.
1281 Expression*&
1282 at(size_t i)
1283 { return this->entries_.at(i); }
1284
1285 // Return the first and last elements.
1286 Expression*&
1287 front()
1288 { return this->entries_.front(); }
1289
1290 Expression*
1291 front() const
1292 { return this->entries_.front(); }
1293
1294 Expression*&
1295 back()
1296 { return this->entries_.back(); }
1297
1298 Expression*
1299 back() const
1300 { return this->entries_.back(); }
1301
1302 // Iterators.
1303
1304 typedef std::vector<Expression*>::iterator iterator;
1305 typedef std::vector<Expression*>::const_iterator const_iterator;
1306
1307 iterator
1308 begin()
1309 { return this->entries_.begin(); }
1310
1311 const_iterator
1312 begin() const
1313 { return this->entries_.begin(); }
1314
1315 iterator
1316 end()
1317 { return this->entries_.end(); }
1318
1319 const_iterator
1320 end() const
1321 { return this->entries_.end(); }
1322
1323 // Erase an entry.
1324 void
1325 erase(iterator p)
1326 { this->entries_.erase(p); }
1327
1328 private:
1329 std::vector<Expression*> entries_;
1330 };
1331
1332 // An abstract base class for an expression which is only used by the
1333 // parser, and is lowered in the lowering pass.
1334
1335 class Parser_expression : public Expression
1336 {
1337 public:
1338 Parser_expression(Expression_classification classification,
1339 Location location)
1340 : Expression(classification, location)
1341 { }
1342
1343 protected:
1344 virtual Expression*
1345 do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
1346
1347 Type*
1348 do_type();
1349
1350 void
1351 do_determine_type(const Type_context*)
1352 { go_unreachable(); }
1353
1354 void
1355 do_check_types(Gogo*)
1356 { go_unreachable(); }
1357
1358 Bexpression*
1359 do_get_backend(Translate_context*)
1360 { go_unreachable(); }
1361 };
1362
1363 // An expression which is simply a variable.
1364
1365 class Var_expression : public Expression
1366 {
1367 public:
1368 Var_expression(Named_object* variable, Location location)
1369 : Expression(EXPRESSION_VAR_REFERENCE, location),
1370 variable_(variable)
1371 { }
1372
1373 // Return the variable.
1374 Named_object*
1375 named_object() const
1376 { return this->variable_; }
1377
1378 protected:
1379 Expression*
1380 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1381
1382 Type*
1383 do_type();
1384
1385 void
1386 do_determine_type(const Type_context*);
1387
1388 Expression*
1389 do_copy()
1390 { return this; }
1391
1392 int
1393 do_inlining_cost() const;
1394
1395 void
1396 do_export(Export_function_body*) const;
1397
1398 bool
1399 do_is_addressable() const
1400 { return true; }
1401
1402 void
1403 do_address_taken(bool);
1404
1405 Bexpression*
1406 do_get_backend(Translate_context*);
1407
1408 void
1409 do_dump_expression(Ast_dump_context*) const;
1410
1411 private:
1412 // The variable we are referencing.
1413 Named_object* variable_;
1414 };
1415
1416 // A reference to a variable within an enclosing function.
1417
1418 class Enclosed_var_expression : public Expression
1419 {
1420 public:
1421 Enclosed_var_expression(Expression* reference, Named_object* variable,
1422 Location location)
1423 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE, location),
1424 reference_(reference), variable_(variable)
1425 { }
1426
1427 // The reference to the enclosed variable. This will be an indirection of the
1428 // the field stored within closure variable.
1429 Expression*
1430 reference() const
1431 { return this->reference_; }
1432
1433 // The variable being enclosed and referenced.
1434 Named_object*
1435 variable() const
1436 { return this->variable_; }
1437
1438 protected:
1439 int
1440 do_traverse(Traverse*);
1441
1442 Expression*
1443 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1444
1445 Expression*
1446 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1447
1448 Type*
1449 do_type()
1450 { return this->reference_->type(); }
1451
1452 void
1453 do_determine_type(const Type_context* context)
1454 { return this->reference_->determine_type(context); }
1455
1456 Expression*
1457 do_copy()
1458 { return this; }
1459
1460 bool
1461 do_is_addressable() const
1462 { return this->reference_->is_addressable(); }
1463
1464 void
1465 do_address_taken(bool escapes);
1466
1467 Bexpression*
1468 do_get_backend(Translate_context* context)
1469 { return this->reference_->get_backend(context); }
1470
1471 void
1472 do_dump_expression(Ast_dump_context*) const;
1473
1474 private:
1475 // The reference to the enclosed variable.
1476 Expression* reference_;
1477 // The variable being enclosed.
1478 Named_object* variable_;
1479 };
1480
1481 // A reference to a temporary variable.
1482
1483 class Temporary_reference_expression : public Expression
1484 {
1485 public:
1486 Temporary_reference_expression(Temporary_statement* statement,
1487 Location location)
1488 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1489 statement_(statement), is_lvalue_(false)
1490 { }
1491
1492 // The temporary that this expression refers to.
1493 Temporary_statement*
1494 statement() const
1495 { return this->statement_; }
1496
1497 // Indicate that this reference appears on the left hand side of an
1498 // assignment statement.
1499 void
1500 set_is_lvalue()
1501 { this->is_lvalue_ = true; }
1502
1503 protected:
1504 Type*
1505 do_type();
1506
1507 void
1508 do_determine_type(const Type_context*)
1509 { }
1510
1511 Expression*
1512 do_copy()
1513 { return make_temporary_reference(this->statement_, this->location()); }
1514
1515 bool
1516 do_is_addressable() const
1517 { return true; }
1518
1519 void
1520 do_address_taken(bool);
1521
1522 Bexpression*
1523 do_get_backend(Translate_context*);
1524
1525 void
1526 do_dump_expression(Ast_dump_context*) const;
1527
1528 private:
1529 // The statement where the temporary variable is defined.
1530 Temporary_statement* statement_;
1531 // Whether this reference appears on the left hand side of an
1532 // assignment statement.
1533 bool is_lvalue_;
1534 };
1535
1536 // Set and use a temporary variable.
1537
1538 class Set_and_use_temporary_expression : public Expression
1539 {
1540 public:
1541 Set_and_use_temporary_expression(Temporary_statement* statement,
1542 Expression* expr, Location location)
1543 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1544 statement_(statement), expr_(expr)
1545 { }
1546
1547 // Return the temporary.
1548 Temporary_statement*
1549 temporary() const
1550 { return this->statement_; }
1551
1552 // Return the expression.
1553 Expression*
1554 expression() const
1555 { return this->expr_; }
1556
1557 protected:
1558 int
1559 do_traverse(Traverse* traverse)
1560 { return Expression::traverse(&this->expr_, traverse); }
1561
1562 Type*
1563 do_type();
1564
1565 void
1566 do_determine_type(const Type_context*);
1567
1568 Expression*
1569 do_copy()
1570 {
1571 return make_set_and_use_temporary(this->statement_, this->expr_,
1572 this->location());
1573 }
1574
1575 bool
1576 do_is_addressable() const
1577 { return true; }
1578
1579 void
1580 do_address_taken(bool);
1581
1582 Bexpression*
1583 do_get_backend(Translate_context*);
1584
1585 void
1586 do_dump_expression(Ast_dump_context*) const;
1587
1588 private:
1589 // The statement where the temporary variable is defined.
1590 Temporary_statement* statement_;
1591 // The expression to assign to the temporary.
1592 Expression* expr_;
1593 };
1594
1595 // A string expression.
1596
1597 class String_expression : public Expression
1598 {
1599 public:
1600 String_expression(const std::string& val, Type* type, Location location)
1601 : Expression(EXPRESSION_STRING, location),
1602 val_(val), type_(type)
1603 { }
1604
1605 const std::string&
1606 val() const
1607 { return this->val_; }
1608
1609 static Expression*
1610 do_import(Import_expression*, Location);
1611
1612 protected:
1613 bool
1614 do_is_constant() const
1615 { return true; }
1616
1617 bool
1618 do_is_zero_value() const
1619 { return this->val_ == ""; }
1620
1621 bool
1622 do_is_static_initializer() const
1623 { return true; }
1624
1625 bool
1626 do_string_constant_value(std::string* val) const
1627 {
1628 *val = this->val_;
1629 return true;
1630 }
1631
1632 Type*
1633 do_type();
1634
1635 void
1636 do_determine_type(const Type_context*);
1637
1638 Expression*
1639 do_copy()
1640 { return this; }
1641
1642 Bexpression*
1643 do_get_backend(Translate_context*);
1644
1645 // Write string literal to a string dump.
1646 static void
1647 export_string(String_dump* exp, const String_expression* str);
1648
1649 // Set the inlining cost a bit high since inlining may cause
1650 // duplicated string literals.
1651 int
1652 do_inlining_cost() const
1653 { return 5; }
1654
1655 void
1656 do_export(Export_function_body*) const;
1657
1658 void
1659 do_dump_expression(Ast_dump_context*) const;
1660
1661 private:
1662 // The string value. This is immutable.
1663 const std::string val_;
1664 // The type as determined by context.
1665 Type* type_;
1666 };
1667
1668 // A type conversion expression.
1669
1670 class Type_conversion_expression : public Expression
1671 {
1672 public:
1673 Type_conversion_expression(Type* type, Expression* expr,
1674 Location location)
1675 : Expression(EXPRESSION_CONVERSION, location),
1676 type_(type), expr_(expr), may_convert_function_types_(false),
1677 no_copy_(false)
1678 { }
1679
1680 // Return the type to which we are converting.
1681 Type*
1682 type() const
1683 { return this->type_; }
1684
1685 // Return the expression which we are converting.
1686 Expression*
1687 expr() const
1688 { return this->expr_; }
1689
1690 // Permit converting from one function type to another. This is
1691 // used internally for method expressions.
1692 void
1693 set_may_convert_function_types()
1694 {
1695 this->may_convert_function_types_ = true;
1696 }
1697
1698 // Mark string([]byte) conversion to reuse the backing store
1699 // without copying.
1700 void
1701 set_no_copy(bool b)
1702 { this->no_copy_ = b; };
1703
1704 // Import a type conversion expression.
1705 static Expression*
1706 do_import(Import_expression*, Location);
1707
1708 protected:
1709 int
1710 do_traverse(Traverse* traverse);
1711
1712 Expression*
1713 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1714
1715 Expression*
1716 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1717
1718 bool
1719 do_is_constant() const;
1720
1721 bool
1722 do_is_zero_value() const;
1723
1724 bool
1725 do_is_static_initializer() const;
1726
1727 bool
1728 do_numeric_constant_value(Numeric_constant*) const;
1729
1730 bool
1731 do_string_constant_value(std::string*) const;
1732
1733 Type*
1734 do_type()
1735 { return this->type_; }
1736
1737 void
1738 do_determine_type(const Type_context*);
1739
1740 void
1741 do_check_types(Gogo*);
1742
1743 Expression*
1744 do_copy();
1745
1746 Bexpression*
1747 do_get_backend(Translate_context* context);
1748
1749 int
1750 do_inlining_cost() const;
1751
1752 void
1753 do_export(Export_function_body*) const;
1754
1755 void
1756 do_dump_expression(Ast_dump_context*) const;
1757
1758 private:
1759 // The type to convert to.
1760 Type* type_;
1761 // The expression to convert.
1762 Expression* expr_;
1763 // True if this is permitted to convert function types. This is
1764 // used internally for method expressions.
1765 bool may_convert_function_types_;
1766 // True if a string([]byte) conversion can reuse the backing store
1767 // without copying. Only used in string([]byte) conversion.
1768 bool no_copy_;
1769 };
1770
1771 // An unsafe type conversion, used to pass values to builtin functions.
1772
1773 class Unsafe_type_conversion_expression : public Expression
1774 {
1775 public:
1776 Unsafe_type_conversion_expression(Type* type, Expression* expr,
1777 Location location)
1778 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
1779 type_(type), expr_(expr)
1780 { }
1781
1782 Expression*
1783 expr() const
1784 { return this->expr_; }
1785
1786 protected:
1787 int
1788 do_traverse(Traverse* traverse);
1789
1790 bool
1791 do_is_zero_value() const
1792 { return this->expr_->is_zero_value(); }
1793
1794 bool
1795 do_is_static_initializer() const;
1796
1797 Type*
1798 do_type()
1799 { return this->type_; }
1800
1801 void
1802 do_determine_type(const Type_context*)
1803 { this->expr_->determine_type_no_context(); }
1804
1805 Expression*
1806 do_copy();
1807
1808 Bexpression*
1809 do_get_backend(Translate_context*);
1810
1811 void
1812 do_dump_expression(Ast_dump_context*) const;
1813
1814 private:
1815 // The type to convert to.
1816 Type* type_;
1817 // The expression to convert.
1818 Expression* expr_;
1819 };
1820
1821 // A Unary expression.
1822
1823 class Unary_expression : public Expression
1824 {
1825 public:
1826 Unary_expression(Operator op, Expression* expr, Location location)
1827 : Expression(EXPRESSION_UNARY, location),
1828 op_(op), escapes_(true), create_temp_(false), is_gc_root_(false),
1829 is_slice_init_(false), expr_(expr),
1830 issue_nil_check_(NIL_CHECK_DEFAULT)
1831 { }
1832
1833 // Return the operator.
1834 Operator
1835 op() const
1836 { return this->op_; }
1837
1838 // Return the operand.
1839 Expression*
1840 operand() const
1841 { return this->expr_; }
1842
1843 // Record that an address expression does not escape.
1844 void
1845 set_does_not_escape()
1846 {
1847 go_assert(this->op_ == OPERATOR_AND);
1848 this->escapes_ = false;
1849 }
1850
1851 // Record that this is an address expression which should create a
1852 // temporary variable if necessary. This is used for method calls.
1853 void
1854 set_create_temp()
1855 {
1856 go_assert(this->op_ == OPERATOR_AND);
1857 this->create_temp_ = true;
1858 }
1859
1860 // Record that this is an address expression of a GC root, which is a
1861 // mutable composite literal. This used for registering GC variables.
1862 void
1863 set_is_gc_root()
1864 {
1865 go_assert(this->op_ == OPERATOR_AND);
1866 this->is_gc_root_ = true;
1867 }
1868
1869 // Record that this is an address expression of a slice value initializer,
1870 // which is mutable if the values are not copied to the heap.
1871 void
1872 set_is_slice_init()
1873 {
1874 go_assert(this->op_ == OPERATOR_AND);
1875 this->is_slice_init_ = true;
1876 }
1877
1878 // Call the address_taken method on the operand if necessary.
1879 void
1880 check_operand_address_taken(Gogo*);
1881
1882 // Apply unary opcode OP to UNC, setting NC. Return true if this
1883 // could be done, false if not. On overflow, issues an error and
1884 // sets *ISSUED_ERROR.
1885 static bool
1886 eval_constant(Operator op, const Numeric_constant* unc,
1887 Location, Numeric_constant* nc, bool *issued_error);
1888
1889 static Expression*
1890 do_import(Import_expression*, Location);
1891
1892 // Declare that this deref does or does not require an explicit nil check.
1893 void
1894 set_requires_nil_check(bool needed)
1895 {
1896 go_assert(this->op_ == OPERATOR_MULT);
1897 if (needed)
1898 this->issue_nil_check_ = NIL_CHECK_NEEDED;
1899 else
1900 this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED;
1901 }
1902
1903 protected:
1904 int
1905 do_traverse(Traverse* traverse)
1906 { return Expression::traverse(&this->expr_, traverse); }
1907
1908 Expression*
1909 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1910
1911 Expression*
1912 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1913
1914 bool
1915 do_is_constant() const;
1916
1917 bool
1918 do_is_static_initializer() const;
1919
1920 bool
1921 do_numeric_constant_value(Numeric_constant*) const;
1922
1923 Type*
1924 do_type();
1925
1926 void
1927 do_determine_type(const Type_context*);
1928
1929 void
1930 do_check_types(Gogo*);
1931
1932 Expression*
1933 do_copy()
1934 {
1935 return Expression::make_unary(this->op_, this->expr_->copy(),
1936 this->location());
1937 }
1938
1939 bool
1940 do_must_eval_subexpressions_in_order(int*) const
1941 { return this->op_ == OPERATOR_MULT; }
1942
1943 bool
1944 do_is_addressable() const
1945 { return this->op_ == OPERATOR_MULT; }
1946
1947 Bexpression*
1948 do_get_backend(Translate_context*);
1949
1950 int
1951 do_inlining_cost() const
1952 { return 1; }
1953
1954 void
1955 do_export(Export_function_body*) const;
1956
1957 void
1958 do_dump_expression(Ast_dump_context*) const;
1959
1960 void
1961 do_issue_nil_check()
1962 {
1963 if (this->op_ == OPERATOR_MULT)
1964 this->set_requires_nil_check(true);
1965 }
1966
1967 private:
1968 static bool
1969 base_is_static_initializer(Expression*);
1970
1971 // Return a determination as to whether this dereference expression
1972 // requires a nil check.
1973 Nil_check_classification
1974 requires_nil_check(Gogo*);
1975
1976 // The unary operator to apply.
1977 Operator op_;
1978 // Normally true. False if this is an address expression which does
1979 // not escape the current function.
1980 bool escapes_;
1981 // True if this is an address expression which should create a
1982 // temporary variable if necessary.
1983 bool create_temp_;
1984 // True if this is an address expression for a GC root. A GC root is a
1985 // special struct composite literal that is mutable when addressed, meaning
1986 // it cannot be represented as an immutable_struct in the backend.
1987 bool is_gc_root_;
1988 // True if this is an address expression for a slice value with an immutable
1989 // initializer. The initializer for a slice's value pointer has an array
1990 // type, meaning it cannot be represented as an immutable_struct in the
1991 // backend.
1992 bool is_slice_init_;
1993 // The operand.
1994 Expression* expr_;
1995 // Whether or not to issue a nil check for this expression if its address
1996 // is being taken.
1997 Nil_check_classification issue_nil_check_;
1998 };
1999
2000 // A binary expression.
2001
2002 class Binary_expression : public Expression
2003 {
2004 public:
2005 Binary_expression(Operator op, Expression* left, Expression* right,
2006 Location location)
2007 : Expression(EXPRESSION_BINARY, location),
2008 op_(op), left_(left), right_(right), type_(NULL)
2009 { }
2010
2011 // Return the operator.
2012 Operator
2013 op()
2014 { return this->op_; }
2015
2016 // Return the left hand expression.
2017 Expression*
2018 left()
2019 { return this->left_; }
2020
2021 // Return the right hand expression.
2022 Expression*
2023 right()
2024 { return this->right_; }
2025
2026 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
2027 // Return true if this could be done, false if not. Issue errors at
2028 // LOCATION as appropriate, and sets *ISSUED_ERROR if it did.
2029 static bool
2030 eval_constant(Operator op, Numeric_constant* left_nc,
2031 Numeric_constant* right_nc, Location location,
2032 Numeric_constant* nc, bool* issued_error);
2033
2034 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
2035 // *RESULT. Return true if this could be done, false if not. Issue
2036 // errors at LOCATION as appropriate.
2037 static bool
2038 compare_constant(Operator op, Numeric_constant* left_nc,
2039 Numeric_constant* right_nc, Location location,
2040 bool* result);
2041
2042 static Expression*
2043 do_import(Import_expression*, Location);
2044
2045 // Report an error if OP can not be applied to TYPE. Return whether
2046 // it can. OTYPE is the type of the other operand.
2047 static bool
2048 check_operator_type(Operator op, Type* type, Type* otype, Location);
2049
2050 // Set *RESULT_TYPE to the resulting type when OP is applied to
2051 // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
2052 // success, false on failure.
2053 static bool
2054 operation_type(Operator op, Type* left_type, Type* right_type,
2055 Type** result_type);
2056
2057 protected:
2058 int
2059 do_traverse(Traverse* traverse);
2060
2061 Expression*
2062 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2063
2064 Expression*
2065 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2066
2067 bool
2068 do_is_constant() const
2069 { return this->left_->is_constant() && this->right_->is_constant(); }
2070
2071 bool
2072 do_is_static_initializer() const;
2073
2074 bool
2075 do_numeric_constant_value(Numeric_constant*) const;
2076
2077 bool
2078 do_discarding_value();
2079
2080 Type*
2081 do_type();
2082
2083 void
2084 do_determine_type(const Type_context*);
2085
2086 void
2087 do_check_types(Gogo*);
2088
2089 Expression*
2090 do_copy()
2091 {
2092 return Expression::make_binary(this->op_, this->left_->copy(),
2093 this->right_->copy(), this->location());
2094 }
2095
2096 Bexpression*
2097 do_get_backend(Translate_context*);
2098
2099 int
2100 do_inlining_cost() const
2101 { return 1; }
2102
2103 void
2104 do_export(Export_function_body*) const;
2105
2106 void
2107 do_dump_expression(Ast_dump_context*) const;
2108
2109 private:
2110 static bool
2111 cmp_to_bool(Operator op, int cmp);
2112
2113 static bool
2114 eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
2115 Location, Numeric_constant*);
2116
2117 static bool
2118 eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
2119 Location, Numeric_constant*);
2120
2121 static bool
2122 eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
2123 Location, Numeric_constant*);
2124
2125 static bool
2126 compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
2127
2128 static bool
2129 compare_float(const Numeric_constant*, const Numeric_constant *, int*);
2130
2131 static bool
2132 compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
2133
2134 Expression*
2135 lower_struct_comparison(Gogo*, Statement_inserter*);
2136
2137 Expression*
2138 lower_array_comparison(Gogo*, Statement_inserter*);
2139
2140 Expression*
2141 lower_interface_value_comparison(Gogo*, Statement_inserter*);
2142
2143 Expression*
2144 lower_compare_to_memcmp(Gogo*, Statement_inserter*);
2145
2146 Expression*
2147 operand_address(Statement_inserter*, Expression*);
2148
2149 // The binary operator to apply.
2150 Operator op_;
2151 // The left hand side operand.
2152 Expression* left_;
2153 // The right hand side operand.
2154 Expression* right_;
2155 // The type of a comparison operation.
2156 Type* type_;
2157 };
2158
2159 // A string concatenation expression. This is a sequence of strings
2160 // added together. It is created when lowering Binary_expression.
2161
2162 class String_concat_expression : public Expression
2163 {
2164 public:
2165 String_concat_expression(Expression_list* exprs)
2166 : Expression(EXPRESSION_STRING_CONCAT, exprs->front()->location()),
2167 exprs_(exprs)
2168 { }
2169
2170 // Return the list of string expressions to be concatenated.
2171 Expression_list*
2172 exprs()
2173 { return this->exprs_; }
2174
2175 protected:
2176 int
2177 do_traverse(Traverse* traverse)
2178 { return this->exprs_->traverse(traverse); }
2179
2180 Expression*
2181 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2182 { return this; }
2183
2184 Expression*
2185 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2186
2187 bool
2188 do_is_constant() const;
2189
2190 bool
2191 do_is_zero_value() const;
2192
2193 bool
2194 do_is_static_initializer() const;
2195
2196 Type*
2197 do_type();
2198
2199 void
2200 do_determine_type(const Type_context*);
2201
2202 void
2203 do_check_types(Gogo*);
2204
2205 Expression*
2206 do_copy()
2207 { return Expression::make_string_concat(this->exprs_->copy()); }
2208
2209 Bexpression*
2210 do_get_backend(Translate_context*)
2211 { go_unreachable(); }
2212
2213 void
2214 do_export(Export_function_body*) const
2215 { go_unreachable(); }
2216
2217 void
2218 do_dump_expression(Ast_dump_context*) const;
2219
2220 private:
2221 // The string expressions to concatenate.
2222 Expression_list* exprs_;
2223 };
2224
2225 // A call expression. The go statement needs to dig inside this.
2226
2227 class Call_expression : public Expression
2228 {
2229 public:
2230 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
2231 Location location)
2232 : Expression(EXPRESSION_CALL, location),
2233 fn_(fn), args_(args), type_(NULL), call_(NULL), call_temp_(NULL)
2234 , expected_result_count_(0), is_varargs_(is_varargs),
2235 varargs_are_lowered_(false), types_are_determined_(false),
2236 is_deferred_(false), is_concurrent_(false), issued_error_(false),
2237 is_multi_value_arg_(false), is_flattened_(false)
2238 { }
2239
2240 // The function to call.
2241 Expression*
2242 fn() const
2243 { return this->fn_; }
2244
2245 // The arguments.
2246 Expression_list*
2247 args()
2248 { return this->args_; }
2249
2250 const Expression_list*
2251 args() const
2252 { return this->args_; }
2253
2254 // Get the function type.
2255 Function_type*
2256 get_function_type() const;
2257
2258 // Return the number of values this call will return.
2259 size_t
2260 result_count() const;
2261
2262 // Return the temporary variable that holds the results. This is
2263 // only valid after the expression has been lowered, and is only
2264 // valid for calls which return multiple results.
2265 Temporary_statement*
2266 results() const;
2267
2268 // Set the number of results expected from this call. This is used
2269 // when the call appears in a context that expects multiple results,
2270 // such as a, b = f().
2271 void
2272 set_expected_result_count(size_t);
2273
2274 // Return whether this is a call to the predeclared function
2275 // recover.
2276 bool
2277 is_recover_call() const;
2278
2279 // Set the argument for a call to recover.
2280 void
2281 set_recover_arg(Expression*);
2282
2283 // Whether the last argument is a varargs argument (f(a...)).
2284 bool
2285 is_varargs() const
2286 { return this->is_varargs_; }
2287
2288 // Return whether varargs have already been lowered.
2289 bool
2290 varargs_are_lowered() const
2291 { return this->varargs_are_lowered_; }
2292
2293 // Note that varargs have already been lowered.
2294 void
2295 set_varargs_are_lowered()
2296 { this->varargs_are_lowered_ = true; }
2297
2298 // Whether this call is being deferred.
2299 bool
2300 is_deferred() const
2301 { return this->is_deferred_; }
2302
2303 // Note that the call is being deferred.
2304 void
2305 set_is_deferred()
2306 { this->is_deferred_ = true; }
2307
2308 // Whether this call is concurrently executed.
2309 bool
2310 is_concurrent() const
2311 { return this->is_concurrent_; }
2312
2313 // Note that the call is concurrently executed.
2314 void
2315 set_is_concurrent()
2316 { this->is_concurrent_ = true; }
2317
2318 // We have found an error with this call expression; return true if
2319 // we should report it.
2320 bool
2321 issue_error();
2322
2323 // Whether or not this call contains errors, either in the call or the
2324 // arguments to the call.
2325 bool
2326 is_erroneous_call();
2327
2328 // Whether this call returns multiple results that are used as an
2329 // multi-valued argument.
2330 bool
2331 is_multi_value_arg() const
2332 { return this->is_multi_value_arg_; }
2333
2334 // Note this call is used as a multi-valued argument.
2335 void
2336 set_is_multi_value_arg()
2337 { this->is_multi_value_arg_ = true; }
2338
2339 // Whether this is a call to builtin function.
2340 virtual bool
2341 is_builtin()
2342 { return false; }
2343
2344 // Convert to a Builtin_call_expression, or return NULL.
2345 inline Builtin_call_expression*
2346 builtin_call_expression();
2347
2348 protected:
2349 int
2350 do_traverse(Traverse*);
2351
2352 virtual Expression*
2353 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2354
2355 virtual Expression*
2356 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2357
2358 bool
2359 do_discarding_value()
2360 { return true; }
2361
2362 virtual Type*
2363 do_type();
2364
2365 virtual void
2366 do_determine_type(const Type_context*);
2367
2368 virtual void
2369 do_check_types(Gogo*);
2370
2371 Expression*
2372 do_copy();
2373
2374 bool
2375 do_must_eval_in_order() const;
2376
2377 virtual Bexpression*
2378 do_get_backend(Translate_context*);
2379
2380 virtual bool
2381 do_is_recover_call() const;
2382
2383 virtual void
2384 do_set_recover_arg(Expression*);
2385
2386 // Let a builtin expression change the argument list.
2387 void
2388 set_args(Expression_list* args)
2389 { this->args_ = args; }
2390
2391 // Let a builtin expression lower varargs.
2392 void
2393 lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
2394 Type* varargs_type, size_t param_count,
2395 Slice_storage_escape_disp escape_disp);
2396
2397 // Let a builtin expression check whether types have been
2398 // determined.
2399 bool
2400 determining_types();
2401
2402 void
2403 do_dump_expression(Ast_dump_context*) const;
2404
2405 private:
2406 bool
2407 check_argument_type(int, const Type*, const Type*, Location, bool);
2408
2409 Expression*
2410 lower_to_builtin(Named_object**, const char*, int*);
2411
2412 Expression*
2413 interface_method_function(Interface_field_reference_expression*,
2414 Expression**, Location);
2415
2416 Bexpression*
2417 set_results(Translate_context*);
2418
2419 // The function to call.
2420 Expression* fn_;
2421 // The arguments to pass. This may be NULL if there are no
2422 // arguments.
2423 Expression_list* args_;
2424 // The type of the expression, to avoid recomputing it.
2425 Type* type_;
2426 // The backend expression for the call, used for a call which returns a tuple.
2427 Bexpression* call_;
2428 // A temporary variable to store this call if the function returns a tuple.
2429 Temporary_statement* call_temp_;
2430 // If not 0, the number of results expected from this call, when
2431 // used in a context that expects multiple values.
2432 size_t expected_result_count_;
2433 // True if the last argument is a varargs argument (f(a...)).
2434 bool is_varargs_;
2435 // True if varargs have already been lowered.
2436 bool varargs_are_lowered_;
2437 // True if types have been determined.
2438 bool types_are_determined_;
2439 // True if the call is an argument to a defer statement.
2440 bool is_deferred_;
2441 // True if the call is an argument to a go statement.
2442 bool is_concurrent_;
2443 // True if we reported an error about a mismatch between call
2444 // results and uses. This is to avoid producing multiple errors
2445 // when there are multiple Call_result_expressions.
2446 bool issued_error_;
2447 // True if this call is used as an argument that returns multiple results.
2448 bool is_multi_value_arg_;
2449 // True if this expression has already been flattened.
2450 bool is_flattened_;
2451 };
2452
2453 // A call expression to a builtin function.
2454
2455 class Builtin_call_expression : public Call_expression
2456 {
2457 public:
2458 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
2459 bool is_varargs, Location location);
2460
2461 // The builtin functions.
2462 enum Builtin_function_code
2463 {
2464 BUILTIN_INVALID,
2465
2466 // Predeclared builtin functions.
2467 BUILTIN_APPEND,
2468 BUILTIN_CAP,
2469 BUILTIN_CLOSE,
2470 BUILTIN_COMPLEX,
2471 BUILTIN_COPY,
2472 BUILTIN_DELETE,
2473 BUILTIN_IMAG,
2474 BUILTIN_LEN,
2475 BUILTIN_MAKE,
2476 BUILTIN_NEW,
2477 BUILTIN_PANIC,
2478 BUILTIN_PRINT,
2479 BUILTIN_PRINTLN,
2480 BUILTIN_REAL,
2481 BUILTIN_RECOVER,
2482
2483 // Builtin functions from the unsafe package.
2484 BUILTIN_ALIGNOF,
2485 BUILTIN_OFFSETOF,
2486 BUILTIN_SIZEOF
2487 };
2488
2489 Builtin_function_code
2490 code()
2491 { return this->code_; }
2492
2493 // This overrides Call_expression::is_builtin.
2494 bool
2495 is_builtin()
2496 { return true; }
2497
2498 // Return whether EXPR, of array type, is a constant if passed to
2499 // len or cap.
2500 static bool
2501 array_len_is_constant(Expression* expr);
2502
2503 Expression*
2504 flatten_append(Gogo*, Named_object*, Statement_inserter*, Expression*,
2505 Block*);
2506
2507 protected:
2508 // This overrides Call_expression::do_lower.
2509 Expression*
2510 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2511
2512 Expression*
2513 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2514
2515 bool
2516 do_is_constant() const;
2517
2518 bool
2519 do_numeric_constant_value(Numeric_constant*) const;
2520
2521 bool
2522 do_discarding_value();
2523
2524 Type*
2525 do_type();
2526
2527 void
2528 do_determine_type(const Type_context*);
2529
2530 void
2531 do_check_types(Gogo*);
2532
2533 Expression*
2534 do_copy();
2535
2536 Bexpression*
2537 do_get_backend(Translate_context*);
2538
2539 void
2540 do_export(Export_function_body*) const;
2541
2542 virtual bool
2543 do_is_recover_call() const;
2544
2545 virtual void
2546 do_set_recover_arg(Expression*);
2547
2548 private:
2549 Expression*
2550 one_arg() const;
2551
2552 bool
2553 check_one_arg();
2554
2555 static Type*
2556 real_imag_type(Type*);
2557
2558 static Type*
2559 complex_type(Type*);
2560
2561 Expression*
2562 lower_make(Statement_inserter*);
2563
2564 bool
2565 check_int_value(Expression*, bool is_length, bool* small);
2566
2567 // A pointer back to the general IR structure. This avoids a global
2568 // variable, or passing it around everywhere.
2569 Gogo* gogo_;
2570 // The builtin function being called.
2571 Builtin_function_code code_;
2572 // Used to stop endless loops when the length of an array uses len
2573 // or cap of the array itself.
2574 mutable bool seen_;
2575 // Whether the argument is set for calls to BUILTIN_RECOVER.
2576 bool recover_arg_is_set_;
2577 };
2578
2579 inline Builtin_call_expression*
2580 Call_expression::builtin_call_expression()
2581 {
2582 return (this->is_builtin()
2583 ? static_cast<Builtin_call_expression*>(this)
2584 : NULL);
2585 }
2586
2587 // A single result from a call which returns multiple results.
2588
2589 class Call_result_expression : public Expression
2590 {
2591 public:
2592 Call_result_expression(Call_expression* call, unsigned int index)
2593 : Expression(EXPRESSION_CALL_RESULT, call->location()),
2594 call_(call), index_(index)
2595 { }
2596
2597 Expression*
2598 call() const
2599 { return this->call_; }
2600
2601 unsigned int
2602 index() const
2603 { return this->index_; }
2604
2605 protected:
2606 int
2607 do_traverse(Traverse*);
2608
2609 Type*
2610 do_type();
2611
2612 void
2613 do_determine_type(const Type_context*);
2614
2615 void
2616 do_check_types(Gogo*);
2617
2618 Expression*
2619 do_copy()
2620 {
2621 return new Call_result_expression(this->call_->call_expression(),
2622 this->index_);
2623 }
2624
2625 bool
2626 do_must_eval_in_order() const
2627 { return true; }
2628
2629 Bexpression*
2630 do_get_backend(Translate_context*);
2631
2632 void
2633 do_dump_expression(Ast_dump_context*) const;
2634
2635 private:
2636 // The underlying call expression.
2637 Expression* call_;
2638 // Which result we want.
2639 unsigned int index_;
2640 };
2641
2642 // An expression which represents a pointer to a function.
2643
2644 class Func_expression : public Expression
2645 {
2646 public:
2647 Func_expression(Named_object* function, Expression* closure,
2648 Location location)
2649 : Expression(EXPRESSION_FUNC_REFERENCE, location),
2650 function_(function), closure_(closure),
2651 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS)
2652 { }
2653
2654 // Return the object associated with the function.
2655 Named_object*
2656 named_object() const
2657 { return this->function_; }
2658
2659 // Return the closure for this function. This will return NULL if
2660 // the function has no closure, which is the normal case.
2661 Expression*
2662 closure()
2663 { return this->closure_; }
2664
2665 // Return whether this is a reference to a runtime function.
2666 bool
2667 is_runtime_function() const
2668 { return this->runtime_code_ != Runtime::NUMBER_OF_FUNCTIONS; }
2669
2670 // Return the runtime code for this function expression.
2671 // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
2672 // runtime function.
2673 Runtime::Function
2674 runtime_code() const
2675 { return this->runtime_code_; }
2676
2677 // Set the runtime code for this function expression.
2678 void
2679 set_runtime_code(Runtime::Function code)
2680 { this->runtime_code_ = code; }
2681
2682 // Return a backend expression for the code of a function.
2683 static Bexpression*
2684 get_code_pointer(Gogo*, Named_object* function, Location loc);
2685
2686 protected:
2687 int
2688 do_traverse(Traverse*);
2689
2690 Type*
2691 do_type();
2692
2693 void
2694 do_determine_type(const Type_context*)
2695 {
2696 if (this->closure_ != NULL)
2697 this->closure_->determine_type_no_context();
2698 }
2699
2700 Expression*
2701 do_copy()
2702 {
2703 return Expression::make_func_reference(this->function_,
2704 (this->closure_ == NULL
2705 ? NULL
2706 : this->closure_->copy()),
2707 this->location());
2708 }
2709
2710 Bexpression*
2711 do_get_backend(Translate_context*);
2712
2713 void
2714 do_dump_expression(Ast_dump_context*) const;
2715
2716 private:
2717 // The function itself.
2718 Named_object* function_;
2719 // A closure. This is normally NULL. For a nested function, it may
2720 // be a struct holding pointers to all the variables referenced by
2721 // this function and defined in enclosing functions.
2722 Expression* closure_;
2723 // The runtime code for the referenced function.
2724 Runtime::Function runtime_code_;
2725 };
2726
2727 // A function descriptor. A function descriptor is a struct with a
2728 // single field pointing to the function code. This is used for
2729 // functions without closures.
2730
2731 class Func_descriptor_expression : public Expression
2732 {
2733 public:
2734 Func_descriptor_expression(Named_object* fn);
2735
2736 // Make the function descriptor type, so that it can be converted.
2737 static void
2738 make_func_descriptor_type();
2739
2740 protected:
2741 int
2742 do_traverse(Traverse*);
2743
2744 Type*
2745 do_type();
2746
2747 void
2748 do_determine_type(const Type_context*)
2749 { }
2750
2751 Expression*
2752 do_copy()
2753 { return Expression::make_func_descriptor(this->fn_); }
2754
2755 bool
2756 do_is_addressable() const
2757 { return true; }
2758
2759 Bexpression*
2760 do_get_backend(Translate_context*);
2761
2762 void
2763 do_dump_expression(Ast_dump_context* context) const;
2764
2765 private:
2766 // The type of all function descriptors.
2767 static Type* descriptor_type;
2768
2769 // The function for which this is the descriptor.
2770 Named_object* fn_;
2771 // The descriptor variable.
2772 Bvariable* dvar_;
2773 };
2774
2775 // A reference to an unknown name.
2776
2777 class Unknown_expression : public Parser_expression
2778 {
2779 public:
2780 Unknown_expression(Named_object* named_object, Location location)
2781 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
2782 named_object_(named_object), no_error_message_(false),
2783 is_composite_literal_key_(false)
2784 { }
2785
2786 // The associated named object.
2787 Named_object*
2788 named_object() const
2789 { return this->named_object_; }
2790
2791 // The name of the identifier which was unknown.
2792 const std::string&
2793 name() const;
2794
2795 // Call this to indicate that we should not give an error if this
2796 // name is never defined. This is used to avoid knock-on errors
2797 // during an erroneous parse.
2798 void
2799 set_no_error_message()
2800 { this->no_error_message_ = true; }
2801
2802 // Note that this expression is being used as the key in a composite
2803 // literal, so it may be OK if it is not resolved.
2804 void
2805 set_is_composite_literal_key()
2806 { this->is_composite_literal_key_ = true; }
2807
2808 // Note that this expression should no longer be treated as a
2809 // composite literal key.
2810 void
2811 clear_is_composite_literal_key()
2812 { this->is_composite_literal_key_ = false; }
2813
2814 protected:
2815 Expression*
2816 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2817
2818 Expression*
2819 do_copy()
2820 { return new Unknown_expression(this->named_object_, this->location()); }
2821
2822 void
2823 do_dump_expression(Ast_dump_context*) const;
2824
2825 private:
2826 // The unknown name.
2827 Named_object* named_object_;
2828 // True if we should not give errors if this is undefined. This is
2829 // used if there was a parse failure.
2830 bool no_error_message_;
2831 // True if this is the key in a composite literal.
2832 bool is_composite_literal_key_;
2833 };
2834
2835 // An index expression. This is lowered to an array index, a string
2836 // index, or a map index.
2837
2838 class Index_expression : public Parser_expression
2839 {
2840 public:
2841 Index_expression(Expression* left, Expression* start, Expression* end,
2842 Expression* cap, Location location)
2843 : Parser_expression(EXPRESSION_INDEX, location),
2844 left_(left), start_(start), end_(end), cap_(cap)
2845 { }
2846
2847 // Dump an index expression, i.e. an expression of the form
2848 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
2849 static void
2850 dump_index_expression(Ast_dump_context*, const Expression* expr,
2851 const Expression* start, const Expression* end,
2852 const Expression* cap);
2853
2854 protected:
2855 int
2856 do_traverse(Traverse*);
2857
2858 Expression*
2859 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2860
2861 Expression*
2862 do_copy()
2863 {
2864 return new Index_expression(this->left_->copy(), this->start_->copy(),
2865 (this->end_ == NULL
2866 ? NULL
2867 : this->end_->copy()),
2868 (this->cap_ == NULL
2869 ? NULL
2870 : this->cap_->copy()),
2871 this->location());
2872 }
2873
2874 // This shouldn't be called--we don't know yet.
2875 bool
2876 do_must_eval_subexpressions_in_order(int*) const
2877 { go_unreachable(); }
2878
2879 void
2880 do_dump_expression(Ast_dump_context*) const;
2881
2882 void
2883 do_issue_nil_check()
2884 { this->left_->issue_nil_check(); }
2885 private:
2886 // The expression being indexed.
2887 Expression* left_;
2888 // The first index.
2889 Expression* start_;
2890 // The second index. This is NULL for an index, non-NULL for a
2891 // slice.
2892 Expression* end_;
2893 // The capacity argument. This is NULL for indices and slices that use the
2894 // default capacity, non-NULL for indices and slices that specify the
2895 // capacity.
2896 Expression* cap_;
2897 };
2898
2899 // An array index. This is used for both indexing and slicing.
2900
2901 class Array_index_expression : public Expression
2902 {
2903 public:
2904 Array_index_expression(Expression* array, Expression* start,
2905 Expression* end, Expression* cap, Location location)
2906 : Expression(EXPRESSION_ARRAY_INDEX, location),
2907 array_(array), start_(start), end_(end), cap_(cap), type_(NULL),
2908 is_lvalue_(false), needs_bounds_check_(true)
2909 { }
2910
2911 // Return the array.
2912 Expression*
2913 array()
2914 { return this->array_; }
2915
2916 const Expression*
2917 array() const
2918 { return this->array_; }
2919
2920 // Return the index of a simple index expression, or the start index
2921 // of a slice expression.
2922 Expression*
2923 start()
2924 { return this->start_; }
2925
2926 const Expression*
2927 start() const
2928 { return this->start_; }
2929
2930 // Return the end index of a slice expression. This is NULL for a
2931 // simple index expression.
2932 Expression*
2933 end()
2934 { return this->end_; }
2935
2936 const Expression*
2937 end() const
2938 { return this->end_; }
2939
2940 // Return whether this array index expression appears in an lvalue
2941 // (left hand side of assignment) context.
2942 bool
2943 is_lvalue() const
2944 { return this->is_lvalue_; }
2945
2946 // Update this array index expression to indicate that it appears
2947 // in a left-hand-side or lvalue context.
2948 void
2949 set_is_lvalue()
2950 { this->is_lvalue_ = true; }
2951
2952 void
2953 set_needs_bounds_check(bool b)
2954 { this->needs_bounds_check_ = b; }
2955
2956 protected:
2957 int
2958 do_traverse(Traverse*);
2959
2960 Expression*
2961 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2962
2963 Type*
2964 do_type();
2965
2966 void
2967 do_determine_type(const Type_context*);
2968
2969 void
2970 do_check_types(Gogo*);
2971
2972 Expression*
2973 do_copy()
2974 {
2975 Expression* ret = Expression::make_array_index(this->array_->copy(),
2976 this->start_->copy(),
2977 (this->end_ == NULL
2978 ? NULL
2979 : this->end_->copy()),
2980 (this->cap_ == NULL
2981 ? NULL
2982 : this->cap_->copy()),
2983 this->location());
2984 ret->array_index_expression()->set_needs_bounds_check(this->needs_bounds_check_);
2985 return ret;
2986 }
2987
2988 bool
2989 do_must_eval_subexpressions_in_order(int* skip) const;
2990
2991 bool
2992 do_is_addressable() const;
2993
2994 void
2995 do_address_taken(bool escapes);
2996
2997 void
2998 do_issue_nil_check()
2999 { this->array_->issue_nil_check(); }
3000
3001 Bexpression*
3002 do_get_backend(Translate_context*);
3003
3004 void
3005 do_dump_expression(Ast_dump_context*) const;
3006
3007 private:
3008 // The array we are getting a value from.
3009 Expression* array_;
3010 // The start or only index.
3011 Expression* start_;
3012 // The end index of a slice. This may be NULL for a simple array
3013 // index, or it may be a nil expression for the length of the array.
3014 Expression* end_;
3015 // The capacity argument of a slice. This may be NULL for an array index or
3016 // slice.
3017 Expression* cap_;
3018 // The type of the expression.
3019 Type* type_;
3020 // Whether expr appears in an lvalue context.
3021 bool is_lvalue_;
3022 // Whether bounds check is needed.
3023 bool needs_bounds_check_;
3024 };
3025
3026 // A string index. This is used for both indexing and slicing.
3027
3028 class String_index_expression : public Expression
3029 {
3030 public:
3031 String_index_expression(Expression* string, Expression* start,
3032 Expression* end, Location location)
3033 : Expression(EXPRESSION_STRING_INDEX, location),
3034 string_(string), start_(start), end_(end)
3035 { }
3036
3037 // Return the string being indexed.
3038 Expression*
3039 string() const
3040 { return this->string_; }
3041
3042 protected:
3043 int
3044 do_traverse(Traverse*);
3045
3046 Expression*
3047 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3048
3049 Type*
3050 do_type();
3051
3052 void
3053 do_determine_type(const Type_context*);
3054
3055 void
3056 do_check_types(Gogo*);
3057
3058 Expression*
3059 do_copy()
3060 {
3061 return Expression::make_string_index(this->string_->copy(),
3062 this->start_->copy(),
3063 (this->end_ == NULL
3064 ? NULL
3065 : this->end_->copy()),
3066 this->location());
3067 }
3068
3069 bool
3070 do_must_eval_subexpressions_in_order(int*) const
3071 { return true; }
3072
3073 Bexpression*
3074 do_get_backend(Translate_context*);
3075
3076 void
3077 do_dump_expression(Ast_dump_context*) const;
3078
3079 private:
3080 // The string we are getting a value from.
3081 Expression* string_;
3082 // The start or only index.
3083 Expression* start_;
3084 // The end index of a slice. This may be NULL for a single index,
3085 // or it may be a nil expression for the length of the string.
3086 Expression* end_;
3087 };
3088
3089 // An index into a map.
3090
3091 class Map_index_expression : public Expression
3092 {
3093 public:
3094 Map_index_expression(Expression* map, Expression* index,
3095 Location location)
3096 : Expression(EXPRESSION_MAP_INDEX, location),
3097 map_(map), index_(index), value_pointer_(NULL)
3098 { }
3099
3100 // Return the map.
3101 Expression*
3102 map()
3103 { return this->map_; }
3104
3105 const Expression*
3106 map() const
3107 { return this->map_; }
3108
3109 // Return the index.
3110 Expression*
3111 index()
3112 { return this->index_; }
3113
3114 const Expression*
3115 index() const
3116 { return this->index_; }
3117
3118 // Get the type of the map being indexed.
3119 Map_type*
3120 get_map_type() const;
3121
3122 // Return an expression for the map index. This returns an
3123 // expression that evaluates to a pointer to a value in the map. If
3124 // the key is not present in the map, this will return a pointer to
3125 // the zero value.
3126 Expression*
3127 get_value_pointer(Gogo*);
3128
3129 protected:
3130 int
3131 do_traverse(Traverse*);
3132
3133 Expression*
3134 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3135
3136 Type*
3137 do_type();
3138
3139 void
3140 do_determine_type(const Type_context*);
3141
3142 void
3143 do_check_types(Gogo*);
3144
3145 Expression*
3146 do_copy()
3147 {
3148 return Expression::make_map_index(this->map_->copy(),
3149 this->index_->copy(),
3150 this->location());
3151 }
3152
3153 bool
3154 do_must_eval_subexpressions_in_order(int*) const
3155 { return true; }
3156
3157 // A map index expression is an lvalue but it is not addressable.
3158
3159 Bexpression*
3160 do_get_backend(Translate_context*);
3161
3162 void
3163 do_dump_expression(Ast_dump_context*) const;
3164
3165 private:
3166 // The map we are looking into.
3167 Expression* map_;
3168 // The index.
3169 Expression* index_;
3170 // A pointer to the value at this index.
3171 Expression* value_pointer_;
3172 };
3173
3174 // An expression which represents a method bound to its first
3175 // argument.
3176
3177 class Bound_method_expression : public Expression
3178 {
3179 public:
3180 Bound_method_expression(Expression* expr, const Method *method,
3181 Named_object* function, Location location)
3182 : Expression(EXPRESSION_BOUND_METHOD, location),
3183 expr_(expr), expr_type_(NULL), method_(method), function_(function)
3184 { }
3185
3186 // Return the object which is the first argument.
3187 Expression*
3188 first_argument()
3189 { return this->expr_; }
3190
3191 // Return the implicit type of the first argument. This will be
3192 // non-NULL when using a method from an anonymous field without
3193 // using an explicit stub.
3194 Type*
3195 first_argument_type() const
3196 { return this->expr_type_; }
3197
3198 // Return the method.
3199 const Method*
3200 method() const
3201 { return this->method_; }
3202
3203 // Return the function to call.
3204 Named_object*
3205 function() const
3206 { return this->function_; }
3207
3208 // Set the implicit type of the expression.
3209 void
3210 set_first_argument_type(Type* type)
3211 { this->expr_type_ = type; }
3212
3213 // Create a thunk to call FUNCTION, for METHOD, when it is used as
3214 // part of a method value.
3215 static Named_object*
3216 create_thunk(Gogo*, const Method* method, Named_object* function);
3217
3218 protected:
3219 int
3220 do_traverse(Traverse*);
3221
3222 Expression*
3223 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3224
3225 Type*
3226 do_type();
3227
3228 void
3229 do_determine_type(const Type_context*);
3230
3231 void
3232 do_check_types(Gogo*);
3233
3234 Expression*
3235 do_copy()
3236 {
3237 return new Bound_method_expression(this->expr_->copy(), this->method_,
3238 this->function_, this->location());
3239 }
3240
3241 Bexpression*
3242 do_get_backend(Translate_context*)
3243 { go_unreachable(); }
3244
3245 void
3246 do_dump_expression(Ast_dump_context*) const;
3247
3248 private:
3249 // A mapping from method functions to the thunks we have created for
3250 // them.
3251 typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
3252 static Method_value_thunks method_value_thunks;
3253
3254 // The object used to find the method. This is passed to the method
3255 // as the first argument.
3256 Expression* expr_;
3257 // The implicit type of the object to pass to the method. This is
3258 // NULL in the normal case, non-NULL when using a method from an
3259 // anonymous field which does not require a stub.
3260 Type* expr_type_;
3261 // The method.
3262 const Method* method_;
3263 // The function to call. This is not the same as
3264 // method_->named_object() when the method has a stub. This will be
3265 // the real function rather than the stub.
3266 Named_object* function_;
3267 };
3268
3269 // A reference to a field in a struct.
3270
3271 class Field_reference_expression : public Expression
3272 {
3273 public:
3274 Field_reference_expression(Expression* expr, unsigned int field_index,
3275 Location location)
3276 : Expression(EXPRESSION_FIELD_REFERENCE, location),
3277 expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
3278 { }
3279
3280 // Return the struct expression.
3281 Expression*
3282 expr() const
3283 { return this->expr_; }
3284
3285 // Return the field index.
3286 unsigned int
3287 field_index() const
3288 { return this->field_index_; }
3289
3290 // Return whether this node was implied by an anonymous field.
3291 bool
3292 implicit() const
3293 { return this->implicit_; }
3294
3295 void
3296 set_implicit(bool implicit)
3297 { this->implicit_ = implicit; }
3298
3299 // Set the struct expression. This is used when parsing.
3300 void
3301 set_struct_expression(Expression* expr)
3302 {
3303 go_assert(this->expr_ == NULL);
3304 this->expr_ = expr;
3305 }
3306
3307 protected:
3308 int
3309 do_traverse(Traverse* traverse)
3310 { return Expression::traverse(&this->expr_, traverse); }
3311
3312 Expression*
3313 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3314
3315 Type*
3316 do_type();
3317
3318 void
3319 do_determine_type(const Type_context*)
3320 { this->expr_->determine_type_no_context(); }
3321
3322 void
3323 do_check_types(Gogo*);
3324
3325 Expression*
3326 do_copy()
3327 {
3328 return Expression::make_field_reference(this->expr_->copy(),
3329 this->field_index_,
3330 this->location());
3331 }
3332
3333 bool
3334 do_is_addressable() const
3335 { return this->expr_->is_addressable(); }
3336
3337 void
3338 do_address_taken(bool escapes)
3339 { this->expr_->address_taken(escapes); }
3340
3341 void
3342 do_issue_nil_check()
3343 { this->expr_->issue_nil_check(); }
3344
3345 Bexpression*
3346 do_get_backend(Translate_context*);
3347
3348 void
3349 do_dump_expression(Ast_dump_context*) const;
3350
3351 private:
3352 // The expression we are looking into. This should have a type of
3353 // struct.
3354 Expression* expr_;
3355 // The zero-based index of the field we are retrieving.
3356 unsigned int field_index_;
3357 // Whether this node was emitted implicitly for an embedded field,
3358 // that is, expr_ is not the expr_ of the original user node.
3359 bool implicit_;
3360 // Whether we have already emitted a fieldtrack call.
3361 bool called_fieldtrack_;
3362 };
3363
3364 // A reference to a field of an interface.
3365
3366 class Interface_field_reference_expression : public Expression
3367 {
3368 public:
3369 Interface_field_reference_expression(Expression* expr,
3370 const std::string& name,
3371 Location location)
3372 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
3373 expr_(expr), name_(name)
3374 { }
3375
3376 // Return the expression for the interface object.
3377 Expression*
3378 expr()
3379 { return this->expr_; }
3380
3381 // Return the name of the method to call.
3382 const std::string&
3383 name() const
3384 { return this->name_; }
3385
3386 // Create a thunk to call the method NAME in TYPE when it is used as
3387 // part of a method value.
3388 static Named_object*
3389 create_thunk(Gogo*, Interface_type* type, const std::string& name);
3390
3391 // Return an expression for the pointer to the function to call.
3392 Expression*
3393 get_function();
3394
3395 // Return an expression for the first argument to pass to the interface
3396 // function. This is the real object associated with the interface object.
3397 Expression*
3398 get_underlying_object();
3399
3400 protected:
3401 int
3402 do_traverse(Traverse* traverse);
3403
3404 Expression*
3405 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3406
3407 Type*
3408 do_type();
3409
3410 void
3411 do_determine_type(const Type_context*);
3412
3413 void
3414 do_check_types(Gogo*);
3415
3416 Expression*
3417 do_copy()
3418 {
3419 return Expression::make_interface_field_reference(this->expr_->copy(),
3420 this->name_,
3421 this->location());
3422 }
3423
3424 Bexpression*
3425 do_get_backend(Translate_context*);
3426
3427 void
3428 do_dump_expression(Ast_dump_context*) const;
3429
3430 private:
3431 // A mapping from interface types to a list of thunks we have
3432 // created for methods.
3433 typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
3434 typedef Unordered_map(Interface_type*, Method_thunks*)
3435 Interface_method_thunks;
3436 static Interface_method_thunks interface_method_thunks;
3437
3438 // The expression for the interface object. This should have a type
3439 // of interface or pointer to interface.
3440 Expression* expr_;
3441 // The field we are retrieving--the name of the method.
3442 std::string name_;
3443 };
3444
3445 // Implement the builtin function new.
3446
3447 class Allocation_expression : public Expression
3448 {
3449 public:
3450 Allocation_expression(Type* type, Location location)
3451 : Expression(EXPRESSION_ALLOCATION, location),
3452 type_(type), allocate_on_stack_(false)
3453 { }
3454
3455 void
3456 set_allocate_on_stack()
3457 { this->allocate_on_stack_ = true; }
3458
3459 protected:
3460 int
3461 do_traverse(Traverse*);
3462
3463 Type*
3464 do_type();
3465
3466 void
3467 do_determine_type(const Type_context*)
3468 { }
3469
3470 void
3471 do_check_types(Gogo*);
3472
3473 Expression*
3474 do_copy();
3475
3476 Bexpression*
3477 do_get_backend(Translate_context*);
3478
3479 void
3480 do_dump_expression(Ast_dump_context*) const;
3481
3482 private:
3483 // The type we are allocating.
3484 Type* type_;
3485 // Whether or not this is a stack allocation.
3486 bool allocate_on_stack_;
3487 };
3488
3489 // A general composite literal. This is lowered to a type specific
3490 // version.
3491
3492 class Composite_literal_expression : public Parser_expression
3493 {
3494 public:
3495 Composite_literal_expression(Type* type, int depth, bool has_keys,
3496 Expression_list* vals, bool all_are_names,
3497 Location location)
3498 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
3499 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys),
3500 all_are_names_(all_are_names), key_path_(std::vector<bool>(depth))
3501 {}
3502
3503
3504 // Mark the DEPTH entry of KEY_PATH as containing a key.
3505 void
3506 update_key_path(size_t depth)
3507 {
3508 go_assert(depth < this->key_path_.size());
3509 this->key_path_[depth] = true;
3510 }
3511
3512 protected:
3513 int
3514 do_traverse(Traverse* traverse);
3515
3516 Expression*
3517 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3518
3519 Expression*
3520 do_copy();
3521
3522 void
3523 do_dump_expression(Ast_dump_context*) const;
3524
3525 private:
3526 Expression*
3527 lower_struct(Gogo*, Type*);
3528
3529 Expression*
3530 lower_array(Type*);
3531
3532 Expression*
3533 make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
3534
3535 Expression*
3536 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
3537
3538 // The type of the composite literal.
3539 Type* type_;
3540 // The depth within a list of composite literals within a composite
3541 // literal, when the type is omitted.
3542 int depth_;
3543 // The values to put in the composite literal.
3544 Expression_list* vals_;
3545 // If this is true, then VALS_ is a list of pairs: a key and a
3546 // value. In an array initializer, a missing key will be NULL.
3547 bool has_keys_;
3548 // If this is true, then HAS_KEYS_ is true, and every key is a
3549 // simple identifier.
3550 bool all_are_names_;
3551 // A complement to DEPTH that indicates for each level starting from 0 to
3552 // DEPTH-1 whether or not this composite literal is nested inside of key or
3553 // a value. This is used to decide which type to use when given a map literal
3554 // with omitted key types.
3555 std::vector<bool> key_path_;
3556 };
3557
3558 // Helper/mixin class for struct and array construction expressions;
3559 // encapsulates a list of values plus an optional traversal order
3560 // recording the order in which the values should be visited.
3561
3562 class Ordered_value_list
3563 {
3564 public:
3565 Ordered_value_list(Expression_list* vals)
3566 : vals_(vals), traverse_order_(NULL)
3567 { }
3568
3569 Expression_list*
3570 vals() const
3571 { return this->vals_; }
3572
3573 int
3574 traverse_vals(Traverse* traverse);
3575
3576 // Get the traversal order (may be NULL)
3577 std::vector<unsigned long>*
3578 traverse_order()
3579 { return traverse_order_; }
3580
3581 // Set the traversal order, used to ensure that we implement the
3582 // order of evaluation rules. Takes ownership of the argument.
3583 void
3584 set_traverse_order(std::vector<unsigned long>* traverse_order)
3585 { this->traverse_order_ = traverse_order; }
3586
3587 private:
3588 // The list of values, in order of the fields in the struct or in
3589 // order of indices in an array. A NULL value of vals_ means that
3590 // all fields/slots should be zero-initialized; a single NULL entry
3591 // in the list means that the corresponding field or array slot
3592 // should be zero-initialized.
3593 Expression_list* vals_;
3594 // If not NULL, the order in which to traverse vals_. This is used
3595 // so that we implement the order of evaluation rules correctly.
3596 std::vector<unsigned long>* traverse_order_;
3597 };
3598
3599 // Construct a struct.
3600
3601 class Struct_construction_expression : public Expression,
3602 public Ordered_value_list
3603 {
3604 public:
3605 Struct_construction_expression(Type* type, Expression_list* vals,
3606 Location location)
3607 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
3608 Ordered_value_list(vals),
3609 type_(type)
3610 { }
3611
3612 // Return whether this is a constant initializer.
3613 bool
3614 is_constant_struct() const;
3615
3616 protected:
3617 int
3618 do_traverse(Traverse* traverse);
3619
3620 bool
3621 do_is_zero_value() const;
3622
3623 bool
3624 do_is_static_initializer() const;
3625
3626 Type*
3627 do_type()
3628 { return this->type_; }
3629
3630 void
3631 do_determine_type(const Type_context*);
3632
3633 void
3634 do_check_types(Gogo*);
3635
3636 Expression*
3637 do_copy();
3638
3639 Expression*
3640 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3641
3642 Bexpression*
3643 do_get_backend(Translate_context*);
3644
3645 void
3646 do_export(Export_function_body*) const;
3647
3648 void
3649 do_dump_expression(Ast_dump_context*) const;
3650
3651 private:
3652 // The type of the struct to construct.
3653 Type* type_;
3654 };
3655
3656 // Construct an array. This class is not used directly; instead we
3657 // use the child classes, Fixed_array_construction_expression and
3658 // Slice_construction_expression.
3659
3660 class Array_construction_expression : public Expression,
3661 public Ordered_value_list
3662 {
3663 protected:
3664 Array_construction_expression(Expression_classification classification,
3665 Type* type,
3666 const std::vector<unsigned long>* indexes,
3667 Expression_list* vals, Location location)
3668 : Expression(classification, location),
3669 Ordered_value_list(vals),
3670 type_(type), indexes_(indexes)
3671 { go_assert(indexes == NULL || indexes->size() == vals->size()); }
3672
3673 public:
3674 // Return whether this is a constant initializer.
3675 bool
3676 is_constant_array() const;
3677
3678 // Return the number of elements.
3679 size_t
3680 element_count() const
3681 { return this->vals() == NULL ? 0 : this->vals()->size(); }
3682
3683 protected:
3684 virtual int
3685 do_traverse(Traverse* traverse);
3686
3687 bool
3688 do_is_zero_value() const;
3689
3690 bool
3691 do_is_static_initializer() const;
3692
3693 Type*
3694 do_type()
3695 { return this->type_; }
3696
3697 void
3698 do_determine_type(const Type_context*);
3699
3700 void
3701 do_check_types(Gogo*);
3702
3703 void
3704 do_export(Export_function_body*) const;
3705
3706 // The indexes.
3707 const std::vector<unsigned long>*
3708 indexes()
3709 { return this->indexes_; }
3710
3711 Expression*
3712 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3713
3714 // Get the backend constructor for the array values.
3715 Bexpression*
3716 get_constructor(Translate_context* context, Btype* btype);
3717
3718 void
3719 do_dump_expression(Ast_dump_context*) const;
3720
3721 virtual void
3722 dump_slice_storage_expression(Ast_dump_context*) const { }
3723
3724 private:
3725 // The type of the array to construct.
3726 Type* type_;
3727 // The list of indexes into the array, one for each value. This may
3728 // be NULL, in which case the indexes start at zero and increment.
3729 const std::vector<unsigned long>* indexes_;
3730 };
3731
3732 // Construct a fixed array.
3733
3734 class Fixed_array_construction_expression :
3735 public Array_construction_expression
3736 {
3737 public:
3738 Fixed_array_construction_expression(Type* type,
3739 const std::vector<unsigned long>* indexes,
3740 Expression_list* vals, Location location);
3741
3742 protected:
3743 Expression*
3744 do_copy();
3745
3746 Bexpression*
3747 do_get_backend(Translate_context*);
3748 };
3749
3750 // Construct a slice.
3751
3752 class Slice_construction_expression : public Array_construction_expression
3753 {
3754 public:
3755 Slice_construction_expression(Type* type,
3756 const std::vector<unsigned long>* indexes,
3757 Expression_list* vals, Location location);
3758
3759 Expression*
3760 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3761
3762 // Record that the storage for this slice (e.g. vals) cannot escape,
3763 // hence it can be stack-allocated.
3764 void
3765 set_storage_does_not_escape()
3766 {
3767 this->storage_escapes_ = false;
3768 }
3769
3770 protected:
3771 // Note that taking the address of a slice literal is invalid.
3772
3773 int
3774 do_traverse(Traverse* traverse);
3775
3776 Expression*
3777 do_copy();
3778
3779 Bexpression*
3780 do_get_backend(Translate_context*);
3781
3782 void
3783 dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const;
3784
3785 // Create an array value for the constructed slice. Invoked during
3786 // flattening if slice storage does not escape, otherwise invoked
3787 // later on during do_get_backend().
3788 Expression*
3789 create_array_val();
3790
3791 private:
3792 // The type of the values in this slice.
3793 Type* valtype_;
3794 // Array value expression, optionally filled in during flattening.
3795 Expression* array_val_;
3796 // Slice storage expression, optionally filled in during flattening.
3797 Expression* slice_storage_;
3798 // Normally true. Can be set to false if we know that the resulting
3799 // storage for the slice cannot escape.
3800 bool storage_escapes_;
3801 };
3802
3803 // Construct a map.
3804
3805 class Map_construction_expression : public Expression
3806 {
3807 public:
3808 Map_construction_expression(Type* type, Expression_list* vals,
3809 Location location)
3810 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
3811 type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL)
3812 { go_assert(vals == NULL || vals->size() % 2 == 0); }
3813
3814 Expression_list*
3815 vals() const
3816 { return this->vals_; }
3817
3818 protected:
3819 int
3820 do_traverse(Traverse* traverse);
3821
3822 Expression*
3823 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3824
3825 Type*
3826 do_type()
3827 { return this->type_; }
3828
3829 void
3830 do_determine_type(const Type_context*);
3831
3832 void
3833 do_check_types(Gogo*);
3834
3835 Expression*
3836 do_copy();
3837
3838 Bexpression*
3839 do_get_backend(Translate_context*);
3840
3841 void
3842 do_export(Export_function_body*) const;
3843
3844 void
3845 do_dump_expression(Ast_dump_context*) const;
3846
3847 private:
3848 // The type of the map to construct.
3849 Type* type_;
3850 // The list of values.
3851 Expression_list* vals_;
3852 // The type of the key-value pair struct for each map element.
3853 Struct_type* element_type_;
3854 // A temporary reference to the variable storing the constructor initializer.
3855 Temporary_statement* constructor_temp_;
3856 };
3857
3858 // A type guard expression.
3859
3860 class Type_guard_expression : public Expression
3861 {
3862 public:
3863 Type_guard_expression(Expression* expr, Type* type, Location location)
3864 : Expression(EXPRESSION_TYPE_GUARD, location),
3865 expr_(expr), type_(type)
3866 { }
3867
3868 // Return the expression to convert.
3869 Expression*
3870 expr()
3871 { return this->expr_; }
3872
3873 // Return the type to which to convert.
3874 Type*
3875 type()
3876 { return this->type_; }
3877
3878 protected:
3879 int
3880 do_traverse(Traverse* traverse);
3881
3882 Expression*
3883 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3884
3885 Type*
3886 do_type()
3887 { return this->type_; }
3888
3889 void
3890 do_determine_type(const Type_context*)
3891 { this->expr_->determine_type_no_context(); }
3892
3893 void
3894 do_check_types(Gogo*);
3895
3896 Expression*
3897 do_copy();
3898
3899 Bexpression*
3900 do_get_backend(Translate_context*);
3901
3902 void
3903 do_dump_expression(Ast_dump_context*) const;
3904
3905 private:
3906 // The expression to convert.
3907 Expression* expr_;
3908 // The type to which to convert.
3909 Type* type_;
3910 };
3911
3912 // Class Heap_expression.
3913
3914 // When you take the address of an escaping expression, it is allocated
3915 // on the heap. This class implements that.
3916
3917 class Heap_expression : public Expression
3918 {
3919 public:
3920 Heap_expression(Expression* expr, Location location)
3921 : Expression(EXPRESSION_HEAP, location),
3922 expr_(expr), allocate_on_stack_(false)
3923 { }
3924
3925 Expression*
3926 expr() const
3927 { return this->expr_; }
3928
3929 void
3930 set_allocate_on_stack()
3931 { this->allocate_on_stack_ = true; }
3932
3933 protected:
3934 int
3935 do_traverse(Traverse* traverse)
3936 { return Expression::traverse(&this->expr_, traverse); }
3937
3938 Type*
3939 do_type();
3940 void
3941 do_determine_type(const Type_context*)
3942 { this->expr_->determine_type_no_context(); }
3943
3944 Expression*
3945 do_copy()
3946 {
3947 return Expression::make_heap_expression(this->expr_->copy(),
3948 this->location());
3949 }
3950
3951 Bexpression*
3952 do_get_backend(Translate_context*);
3953
3954 // We only export global objects, and the parser does not generate
3955 // this in global scope.
3956 void
3957 do_export(Export_function_body*) const
3958 { go_unreachable(); }
3959
3960 void
3961 do_dump_expression(Ast_dump_context*) const;
3962
3963 private:
3964 // The expression which is being put on the heap.
3965 Expression* expr_;
3966 // Whether or not this is a stack allocation.
3967 bool allocate_on_stack_;
3968 };
3969
3970 // A receive expression.
3971
3972 class Receive_expression : public Expression
3973 {
3974 public:
3975 Receive_expression(Expression* channel, Location location)
3976 : Expression(EXPRESSION_RECEIVE, location),
3977 channel_(channel), temp_receiver_(NULL)
3978 { }
3979
3980 // Return the channel.
3981 Expression*
3982 channel()
3983 { return this->channel_; }
3984
3985 static Expression*
3986 do_import(Import_expression*, Location);
3987
3988 protected:
3989 int
3990 do_traverse(Traverse* traverse)
3991 { return Expression::traverse(&this->channel_, traverse); }
3992
3993 bool
3994 do_discarding_value()
3995 { return true; }
3996
3997 Type*
3998 do_type();
3999
4000 Expression*
4001 do_flatten(Gogo*, Named_object*, Statement_inserter*);
4002
4003 void
4004 do_determine_type(const Type_context*)
4005 { this->channel_->determine_type_no_context(); }
4006
4007 void
4008 do_check_types(Gogo*);
4009
4010 Expression*
4011 do_copy()
4012 {
4013 return Expression::make_receive(this->channel_->copy(), this->location());
4014 }
4015
4016 int
4017 do_inlining_cost() const
4018 { return 1; }
4019
4020 bool
4021 do_must_eval_in_order() const
4022 { return true; }
4023
4024 Bexpression*
4025 do_get_backend(Translate_context*);
4026
4027 void
4028 do_export(Export_function_body*) const;
4029
4030 void
4031 do_dump_expression(Ast_dump_context*) const;
4032
4033 private:
4034 // The channel from which we are receiving.
4035 Expression* channel_;
4036 // A temporary reference to the variable storing the received data.
4037 Temporary_statement* temp_receiver_;
4038 };
4039
4040 // An expression that represents a slice value: a struct with value pointer,
4041 // length, and capacity fields.
4042
4043 class Slice_value_expression : public Expression
4044 {
4045 public:
4046 Slice_value_expression(Type* type, Expression* valmem, Expression* len,
4047 Expression* cap, Location location)
4048 : Expression(EXPRESSION_SLICE_VALUE, location),
4049 type_(type), valmem_(valmem), len_(len), cap_(cap)
4050 { }
4051
4052 // The memory holding the values in the slice. The type should be a
4053 // pointer to the element value of the slice.
4054 Expression*
4055 valmem() const
4056 { return this->valmem_; }
4057
4058 protected:
4059 int
4060 do_traverse(Traverse*);
4061
4062 Type*
4063 do_type()
4064 { return this->type_; }
4065
4066 void
4067 do_determine_type(const Type_context*)
4068 { }
4069
4070 Expression*
4071 do_copy();
4072
4073 Bexpression*
4074 do_get_backend(Translate_context* context);
4075
4076 void
4077 do_dump_expression(Ast_dump_context*) const;
4078
4079 private:
4080 // The type of the slice value.
4081 Type* type_;
4082 // The memory holding the values in the slice.
4083 Expression* valmem_;
4084 // The length of the slice.
4085 Expression* len_;
4086 // The capacity of the slice.
4087 Expression* cap_;
4088 };
4089
4090 // Conditional expressions.
4091
4092 class Conditional_expression : public Expression
4093 {
4094 public:
4095 Conditional_expression(Expression* cond, Expression* then_expr,
4096 Expression* else_expr, Location location)
4097 : Expression(EXPRESSION_CONDITIONAL, location),
4098 cond_(cond), then_(then_expr), else_(else_expr)
4099 {}
4100
4101 Expression*
4102 condition() const
4103 { return this->cond_; }
4104
4105 protected:
4106 int
4107 do_traverse(Traverse*);
4108
4109 Type*
4110 do_type();
4111
4112 void
4113 do_determine_type(const Type_context*);
4114
4115 Expression*
4116 do_copy()
4117 {
4118 return new Conditional_expression(this->cond_->copy(), this->then_->copy(),
4119 this->else_->copy(), this->location());
4120 }
4121
4122 Bexpression*
4123 do_get_backend(Translate_context* context);
4124
4125 void
4126 do_dump_expression(Ast_dump_context*) const;
4127
4128 private:
4129 // The condition to be checked.
4130 Expression* cond_;
4131 // The expression to execute if the condition is true.
4132 Expression* then_;
4133 // The expression to execute if the condition is false.
4134 Expression* else_;
4135 };
4136
4137 // Compound expressions.
4138
4139 class Compound_expression : public Expression
4140 {
4141 public:
4142 Compound_expression(Expression* init, Expression* expr, Location location)
4143 : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr)
4144 {}
4145
4146 Expression*
4147 init() const
4148 { return this->init_; }
4149
4150 protected:
4151 int
4152 do_traverse(Traverse*);
4153
4154 Type*
4155 do_type();
4156
4157 void
4158 do_determine_type(const Type_context*);
4159
4160 Expression*
4161 do_copy()
4162 {
4163 return new Compound_expression(this->init_->copy(), this->expr_->copy(),
4164 this->location());
4165 }
4166
4167 Bexpression*
4168 do_get_backend(Translate_context* context);
4169
4170 void
4171 do_dump_expression(Ast_dump_context*) const;
4172
4173 private:
4174 // The expression that is evaluated first and discarded.
4175 Expression* init_;
4176 // The expression that is evaluated and returned.
4177 Expression* expr_;
4178 };
4179
4180 // A backend expression. This is a backend expression wrapped in an
4181 // Expression, for convenience during backend generation.
4182
4183 class Backend_expression : public Expression
4184 {
4185 public:
4186 Backend_expression(Bexpression* bexpr, Type* type, Location location)
4187 : Expression(EXPRESSION_BACKEND, location), bexpr_(bexpr), type_(type)
4188 {}
4189
4190 protected:
4191 int
4192 do_traverse(Traverse*);
4193
4194 // For now these are always valid static initializers. If that
4195 // changes we can change this.
4196 bool
4197 do_is_static_initializer() const
4198 { return true; }
4199
4200 Type*
4201 do_type()
4202 { return this->type_; }
4203
4204 void
4205 do_determine_type(const Type_context*)
4206 { }
4207
4208 Expression*
4209 do_copy();
4210
4211 Bexpression*
4212 do_get_backend(Translate_context*)
4213 { return this->bexpr_; }
4214
4215 void
4216 do_dump_expression(Ast_dump_context*) const;
4217
4218 private:
4219 // The backend expression we are wrapping.
4220 Bexpression* bexpr_;
4221 // The type of the expression;
4222 Type* type_;
4223 };
4224
4225 // A numeric constant. This is used both for untyped constants and
4226 // for constants that have a type.
4227
4228 class Numeric_constant
4229 {
4230 public:
4231 Numeric_constant()
4232 : classification_(NC_INVALID), type_(NULL)
4233 { }
4234
4235 ~Numeric_constant();
4236
4237 Numeric_constant(const Numeric_constant&);
4238
4239 Numeric_constant& operator=(const Numeric_constant&);
4240
4241 // Check equality with another numeric constant.
4242 bool
4243 equals(const Numeric_constant&) const;
4244
4245 // Set to an unsigned long value.
4246 void
4247 set_unsigned_long(Type*, unsigned long);
4248
4249 // Set to an integer value.
4250 void
4251 set_int(Type*, const mpz_t);
4252
4253 // Set to a rune value.
4254 void
4255 set_rune(Type*, const mpz_t);
4256
4257 // Set to a floating point value.
4258 void
4259 set_float(Type*, const mpfr_t);
4260
4261 // Set to a complex value.
4262 void
4263 set_complex(Type*, const mpc_t);
4264
4265 // Mark numeric constant as invalid.
4266 void
4267 set_invalid()
4268 { this->classification_ = NC_INVALID; }
4269
4270 // Classifiers.
4271 bool
4272 is_int() const
4273 { return this->classification_ == Numeric_constant::NC_INT; }
4274
4275 bool
4276 is_rune() const
4277 { return this->classification_ == Numeric_constant::NC_RUNE; }
4278
4279 bool
4280 is_float() const
4281 { return this->classification_ == Numeric_constant::NC_FLOAT; }
4282
4283 bool
4284 is_complex() const
4285 { return this->classification_ == Numeric_constant::NC_COMPLEX; }
4286
4287 bool
4288 is_invalid() const
4289 { return this->classification_ == Numeric_constant::NC_INVALID; }
4290
4291 // Value retrievers. These will initialize the values as well as
4292 // set them. GET_INT is only valid if IS_INT returns true, and
4293 // likewise respectively.
4294 void
4295 get_int(mpz_t*) const;
4296
4297 void
4298 get_rune(mpz_t*) const;
4299
4300 void
4301 get_float(mpfr_t*) const;
4302
4303 void
4304 get_complex(mpc_t*) const;
4305
4306 // Codes returned by to_unsigned_long.
4307 enum To_unsigned_long
4308 {
4309 // Value is integer and fits in unsigned long.
4310 NC_UL_VALID,
4311 // Value is not integer.
4312 NC_UL_NOTINT,
4313 // Value is integer but is negative.
4314 NC_UL_NEGATIVE,
4315 // Value is non-negative integer but does not fit in unsigned
4316 // long.
4317 NC_UL_BIG
4318 };
4319
4320 // If the value can be expressed as an integer that fits in an
4321 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
4322 // one of the other To_unsigned_long codes.
4323 To_unsigned_long
4324 to_unsigned_long(unsigned long* val) const;
4325
4326 // If the value can be expressed as an integer that describes the
4327 // size of an object in memory, set *VAL and return true.
4328 // Otherwise, return false. Currently we use int64_t to represent a
4329 // memory size, as in Type::backend_type_size.
4330 bool
4331 to_memory_size(int64_t* val) const;
4332
4333 // If the value can be expressed as an int, return true and
4334 // initialize and set VAL. This will return false for a value with
4335 // an explicit float or complex type, even if the value is integral.
4336 bool
4337 to_int(mpz_t* val) const;
4338
4339 // If the value can be expressed as a float, return true and
4340 // initialize and set VAL.
4341 bool
4342 to_float(mpfr_t* val) const;
4343
4344 // If the value can be expressed as a complex, return true and
4345 // initialize and set VR and VI.
4346 bool
4347 to_complex(mpc_t* val) const;
4348
4349 // Get the type.
4350 Type*
4351 type() const;
4352
4353 // If the constant can be expressed in TYPE, then set the type of
4354 // the constant to TYPE and return true. Otherwise return false,
4355 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
4356 // the location to use for the error.
4357 bool
4358 set_type(Type* type, bool issue_error, Location location);
4359
4360 // Return an Expression for this value.
4361 Expression*
4362 expression(Location) const;
4363
4364 // Calculate a hash code with a given seed.
4365 unsigned int
4366 hash(unsigned int seed) const;
4367
4368 private:
4369 void
4370 clear();
4371
4372 To_unsigned_long
4373 mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
4374
4375 To_unsigned_long
4376 mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
4377
4378 bool
4379 mpz_to_memory_size(const mpz_t ival, int64_t* val) const;
4380
4381 bool
4382 mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const;
4383
4384 bool
4385 check_int_type(Integer_type*, bool, Location);
4386
4387 bool
4388 check_float_type(Float_type*, bool, Location);
4389
4390 bool
4391 check_complex_type(Complex_type*, bool, Location);
4392
4393 static bool
4394 is_float_neg_zero(const mpfr_t, int bits);
4395
4396 // The kinds of constants.
4397 enum Classification
4398 {
4399 NC_INVALID,
4400 NC_RUNE,
4401 NC_INT,
4402 NC_FLOAT,
4403 NC_COMPLEX
4404 };
4405
4406 // The kind of constant.
4407 Classification classification_;
4408 // The value.
4409 union
4410 {
4411 // If NC_INT or NC_RUNE.
4412 mpz_t int_val;
4413 // If NC_FLOAT.
4414 mpfr_t float_val;
4415 // If NC_COMPLEX.
4416 mpc_t complex_val;
4417 } u_;
4418 // The type if there is one. This will be NULL for an untyped
4419 // constant.
4420 Type* type_;
4421 };
4422
4423 #endif // !defined(GO_EXPRESSIONS_H)