1 // expressions.h -- Go frontend expression handling. -*- C++ -*-
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.
7 #ifndef GO_EXPRESSIONS_H
8 #define GO_EXPRESSIONS_H
17 class Translate_context
;
19 class Statement_inserter
;
30 class Expression_list
;
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 Call_result_expression
;
43 class Func_expression
;
44 class Func_descriptor_expression
;
45 class Unknown_expression
;
46 class Index_expression
;
47 class Array_index_expression
;
48 class String_index_expression
;
49 class Map_index_expression
;
50 class Bound_method_expression
;
51 class Field_reference_expression
;
52 class Interface_field_reference_expression
;
53 class Allocation_expression
;
54 class Composite_literal_expression
;
55 class Struct_construction_expression
;
56 class Array_construction_expression
;
57 class Fixed_array_construction_expression
;
58 class Slice_construction_expression
;
59 class Map_construction_expression
;
60 class Type_guard_expression
;
61 class Heap_expression
;
62 class Receive_expression
;
63 class Conditional_expression
;
64 class Compound_expression
;
65 class Numeric_constant
;
69 class Temporary_statement
;
71 class Ast_dump_context
;
74 // The precision to use for complex values represented as an mpc_t.
75 const int mpc_precision
= 256;
77 // The base class for all expressions.
82 // The types of expressions.
83 enum Expression_classification
89 EXPRESSION_STRING_CONCAT
,
90 EXPRESSION_CONST_REFERENCE
,
91 EXPRESSION_VAR_REFERENCE
,
92 EXPRESSION_ENCLOSED_VAR_REFERENCE
,
93 EXPRESSION_TEMPORARY_REFERENCE
,
94 EXPRESSION_SET_AND_USE_TEMPORARY
,
96 EXPRESSION_FUNC_REFERENCE
,
97 EXPRESSION_FUNC_DESCRIPTOR
,
98 EXPRESSION_FUNC_CODE_REFERENCE
,
99 EXPRESSION_UNKNOWN_REFERENCE
,
102 EXPRESSION_STRING_INFO
,
109 EXPRESSION_CALL_RESULT
,
110 EXPRESSION_BOUND_METHOD
,
112 EXPRESSION_ARRAY_INDEX
,
113 EXPRESSION_STRING_INDEX
,
114 EXPRESSION_MAP_INDEX
,
116 EXPRESSION_FIELD_REFERENCE
,
117 EXPRESSION_INTERFACE_FIELD_REFERENCE
,
118 EXPRESSION_ALLOCATION
,
119 EXPRESSION_TYPE_GUARD
,
120 EXPRESSION_CONVERSION
,
121 EXPRESSION_UNSAFE_CONVERSION
,
122 EXPRESSION_STRUCT_CONSTRUCTION
,
123 EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
124 EXPRESSION_SLICE_CONSTRUCTION
,
125 EXPRESSION_MAP_CONSTRUCTION
,
126 EXPRESSION_COMPOSITE_LITERAL
,
129 EXPRESSION_TYPE_DESCRIPTOR
,
130 EXPRESSION_GC_SYMBOL
,
131 EXPRESSION_TYPE_INFO
,
132 EXPRESSION_SLICE_INFO
,
133 EXPRESSION_SLICE_VALUE
,
134 EXPRESSION_INTERFACE_INFO
,
135 EXPRESSION_INTERFACE_VALUE
,
136 EXPRESSION_INTERFACE_MTABLE
,
137 EXPRESSION_STRUCT_FIELD_OFFSET
,
138 EXPRESSION_LABEL_ADDR
,
139 EXPRESSION_CONDITIONAL
,
143 Expression(Expression_classification
, Location
);
145 virtual ~Expression();
147 // Make an error expression. This is used when a parse error occurs
148 // to prevent cascading errors.
150 make_error(Location
);
152 // Make an expression which is really a type. This is used during
155 make_type(Type
*, Location
);
157 // Make a unary expression.
159 make_unary(Operator
, Expression
*, Location
);
161 // Make a binary expression.
163 make_binary(Operator
, Expression
*, Expression
*, Location
);
165 // Make a string concatenation expression.
167 make_string_concat(Expression_list
*);
169 // Make a reference to a constant in an expression.
171 make_const_reference(Named_object
*, Location
);
173 // Make a reference to a variable in an expression.
175 make_var_reference(Named_object
*, Location
);
177 // Make a reference to a variable within an enclosing function.
179 make_enclosing_var_reference(Expression
*, Named_object
*, Location
);
181 // Make a reference to a temporary variable. Temporary variables
182 // are always created by a single statement, which is what we use to
184 static Temporary_reference_expression
*
185 make_temporary_reference(Temporary_statement
*, Location
);
187 // Make an expressions which sets a temporary variable and then
188 // evaluates to a reference to that temporary variable. This is
189 // used to set a temporary variable while retaining the order of
191 static Set_and_use_temporary_expression
*
192 make_set_and_use_temporary(Temporary_statement
*, Expression
*, Location
);
194 // Make a sink expression--a reference to the blank identifier _.
198 // Make a reference to a function in an expression. This returns a
199 // pointer to the struct holding the address of the function
200 // followed by any closed-over variables.
202 make_func_reference(Named_object
*, Expression
* closure
, Location
);
204 // Make a function descriptor, an immutable struct with a single
205 // field that points to the function code. This may only be used
206 // with functions that do not have closures. FN is the function for
207 // which we are making the descriptor.
208 static Func_descriptor_expression
*
209 make_func_descriptor(Named_object
* fn
);
211 // Make a reference to the code of a function. This is used to set
212 // descriptor and closure fields.
214 make_func_code_reference(Named_object
*, Location
);
216 // Make a reference to an unknown name. In a correct program this
217 // will always be lowered to a real const/var/func reference.
218 static Unknown_expression
*
219 make_unknown_reference(Named_object
*, Location
);
221 // Make a constant bool expression.
223 make_boolean(bool val
, Location
);
225 // Make a constant string expression.
227 make_string(const std::string
&, Location
);
229 // Make an expression that evaluates to some characteristic of an string.
230 // For simplicity, the enum values must match the field indexes in the
231 // underlying struct.
234 // The underlying data in the string.
236 // The length of the string.
241 make_string_info(Expression
* string
, String_info
, Location
);
243 // Make a character constant expression. TYPE should be NULL for an
246 make_character(const mpz_t
*, Type
*, Location
);
248 // Make a constant integer expression from a multi-precision
249 // integer. TYPE should be NULL for an abstract type.
251 make_integer_z(const mpz_t
*, Type
*, Location
);
253 // Make a constant integer expression from an unsigned long. TYPE
254 // should be NULL for an abstract type.
256 make_integer_ul(unsigned long, Type
*, Location
);
258 // Make a constant integer expression from a signed long. TYPE
259 // should be NULL for an abstract type.
261 make_integer_sl(long, Type
*, Location
);
263 // Make a constant integer expression from an int64_t. TYPE should
264 // be NULL for an abstract type.
266 make_integer_int64(int64_t, Type
*, Location
);
268 // Make a constant float expression. TYPE should be NULL for an
271 make_float(const mpfr_t
*, Type
*, Location
);
273 // Make a constant complex expression. TYPE should be NULL for an
276 make_complex(const mpc_t
*, Type
*, Location
);
278 // Make a nil expression.
282 // Make an iota expression. This is used for the predeclared
287 // Make a call expression.
288 static Call_expression
*
289 make_call(Expression
* func
, Expression_list
* args
, bool is_varargs
,
292 // Make a reference to a specific result of a call expression which
295 make_call_result(Call_expression
*, unsigned int index
);
297 // Make an expression which is a method bound to its first
298 // parameter. METHOD is the method being called, FUNCTION is the
300 static Bound_method_expression
*
301 make_bound_method(Expression
* object
, const Method
* method
,
302 Named_object
* function
, Location
);
304 // Make an index or slice expression. This is a parser expression
305 // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
306 // index rather than a slice. CAP may be NULL, meaning we use the default
307 // capacity of LEFT. At parse time we may not know the type of LEFT.
308 // After parsing this is lowered to an array index, a string index,
311 make_index(Expression
* left
, Expression
* start
, Expression
* end
,
312 Expression
* cap
, Location
);
314 // Make an array index expression. END may be NULL, in which case
315 // this is an lvalue. CAP may be NULL, in which case it defaults
318 make_array_index(Expression
* array
, Expression
* start
, Expression
* end
,
319 Expression
* cap
, Location
);
321 // Make a string index expression. END may be NULL. This is never
324 make_string_index(Expression
* string
, Expression
* start
, Expression
* end
,
327 // Make a map index expression. This is an lvalue.
328 static Map_index_expression
*
329 make_map_index(Expression
* map
, Expression
* val
, Location
);
331 // Make a selector. This is a parser expression which represents
332 // LEFT.NAME. At parse time we may not know the type of the left
335 make_selector(Expression
* left
, const std::string
& name
, Location
);
337 // Make a reference to a field in a struct.
338 static Field_reference_expression
*
339 make_field_reference(Expression
*, unsigned int field_index
, Location
);
341 // Make a reference to a field of an interface, with an associated
344 make_interface_field_reference(Expression
*, const std::string
&,
347 // Make an allocation expression.
349 make_allocation(Type
*, Location
);
351 // Make a type guard expression.
353 make_type_guard(Expression
*, Type
*, Location
);
355 // Make a type cast expression.
357 make_cast(Type
*, Expression
*, Location
);
359 // Make an unsafe type cast expression. This is only used when
360 // passing parameter to builtin functions that are part of the Go
363 make_unsafe_cast(Type
*, Expression
*, Location
);
365 // Make a composite literal. The DEPTH parameter is how far down we
366 // are in a list of composite literals with omitted types. HAS_KEYS
367 // is true if the expression list has keys alternating with values.
368 // ALL_ARE_NAMES is true if all the keys could be struct field
371 make_composite_literal(Type
*, int depth
, bool has_keys
, Expression_list
*,
372 bool all_are_names
, Location
);
374 // Make a struct composite literal.
376 make_struct_composite_literal(Type
*, Expression_list
*, Location
);
378 // Make an array composite literal.
380 make_array_composite_literal(Type
*, Expression_list
*, Location
);
382 // Make a slice composite literal.
383 static Slice_construction_expression
*
384 make_slice_composite_literal(Type
*, Expression_list
*, Location
);
386 // Take an expression and allocate it on the heap.
388 make_heap_expression(Expression
*, Location
);
390 // Make a receive expression. VAL is NULL for a unary receive.
391 static Receive_expression
*
392 make_receive(Expression
* channel
, Location
);
394 // Make an expression which evaluates to the address of the type
395 // descriptor for TYPE.
397 make_type_descriptor(Type
* type
, Location
);
399 // Make an expression which evaluates to the address of the gc
402 make_gc_symbol(Type
* type
);
404 // Make an expression which evaluates to some characteristic of a
405 // type. These are only used for type descriptors, so there is no
406 // location parameter.
409 // The size of a value of the type.
411 // The required alignment of a value of the type.
413 // The required alignment of a value of the type when used as a
414 // field in a struct.
415 TYPE_INFO_FIELD_ALIGNMENT
419 make_type_info(Type
* type
, Type_info
);
421 // Make an expression that evaluates to some characteristic of a
422 // slice. For simplicity, the enum values must match the field indexes
423 // in the underlying struct.
426 // The underlying data of the slice.
427 SLICE_INFO_VALUE_POINTER
,
428 // The length of the slice.
430 // The capacity of the slice.
435 make_slice_info(Expression
* slice
, Slice_info
, Location
);
437 // Make an expression for a slice value.
439 make_slice_value(Type
*, Expression
* valptr
, Expression
* len
, Expression
* cap
,
442 // Make an expression that evaluates to some characteristic of an
443 // interface. For simplicity, the enum values must match the field indexes
444 // in the underlying struct.
447 // The type descriptor of an empty interface.
448 INTERFACE_INFO_TYPE_DESCRIPTOR
= 0,
449 // The methods of an interface.
450 INTERFACE_INFO_METHODS
= 0,
451 // The first argument to pass to an interface method.
452 INTERFACE_INFO_OBJECT
456 make_interface_info(Expression
* iface
, Interface_info
, Location
);
458 // Make an expression for an interface value.
460 make_interface_value(Type
*, Expression
*, Expression
*, Location
);
462 // Make an expression that builds a reference to the interface method table
463 // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
464 // reference to the interface method table for the pointer receiver type.
466 make_interface_mtable_ref(Interface_type
* itype
, Type
* type
,
467 bool is_pointer
, Location
);
469 // Make an expression which evaluates to the offset of a field in a
470 // struct. This is only used for type descriptors, so there is no
471 // location parameter.
473 make_struct_field_offset(Struct_type
*, const Struct_field
*);
475 // Make an expression which evaluates to the address of an unnamed
478 make_label_addr(Label
*, Location
);
480 // Make a conditional expression.
482 make_conditional(Expression
*, Expression
*, Expression
*, Location
);
484 // Make a compound expression.
486 make_compound(Expression
*, Expression
*, Location
);
488 // Return the expression classification.
489 Expression_classification
490 classification() const
491 { return this->classification_
; }
493 // Return the location of the expression.
496 { return this->location_
; }
498 // Return whether this is a constant expression.
501 { return this->do_is_constant(); }
503 // Return whether this is an immutable expression.
506 { return this->do_is_immutable(); }
508 // If this is not a numeric constant, return false. If it is one,
509 // return true, and set VAL to hold the value.
511 numeric_constant_value(Numeric_constant
* val
) const
512 { return this->do_numeric_constant_value(val
); }
514 // If this is not a constant expression with string type, return
515 // false. If it is one, return true, and set VAL to the value.
517 string_constant_value(std::string
* val
) const
518 { return this->do_string_constant_value(val
); }
520 // This is called if the value of this expression is being
521 // discarded. This issues warnings about computed values being
522 // unused. This returns true if all is well, false if it issued an
526 { return this->do_discarding_value(); }
528 // Return whether this is an error expression.
530 is_error_expression() const
531 { return this->classification_
== EXPRESSION_ERROR
; }
533 // Return whether this expression really represents a type.
535 is_type_expression() const
536 { return this->classification_
== EXPRESSION_TYPE
; }
538 // If this is a variable reference, return the Var_expression
539 // structure. Otherwise, return NULL. This is a controlled dynamic
543 { return this->convert
<Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
545 const Var_expression
*
546 var_expression() const
547 { return this->convert
<const Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
549 // If this is a enclosed_variable reference, return the
550 // Enclosed_var_expression structure. Otherwise, return NULL.
551 // This is a controlled dynamic cast.
552 Enclosed_var_expression
*
553 enclosed_var_expression()
554 { return this->convert
<Enclosed_var_expression
,
555 EXPRESSION_ENCLOSED_VAR_REFERENCE
>(); }
557 const Enclosed_var_expression
*
558 enclosed_var_expression() const
559 { return this->convert
<const Enclosed_var_expression
,
560 EXPRESSION_ENCLOSED_VAR_REFERENCE
>(); }
563 // If this is a reference to a temporary variable, return the
564 // Temporary_reference_expression. Otherwise, return NULL.
565 Temporary_reference_expression
*
566 temporary_reference_expression()
568 return this->convert
<Temporary_reference_expression
,
569 EXPRESSION_TEMPORARY_REFERENCE
>();
572 // If this is a set-and-use-temporary, return the
573 // Set_and_use_temporary_expression. Otherwise, return NULL.
574 Set_and_use_temporary_expression
*
575 set_and_use_temporary_expression()
577 return this->convert
<Set_and_use_temporary_expression
,
578 EXPRESSION_SET_AND_USE_TEMPORARY
>();
581 // Return whether this is a sink expression.
583 is_sink_expression() const
584 { return this->classification_
== EXPRESSION_SINK
; }
586 // If this is a string expression, return the String_expression
587 // structure. Otherwise, return NULL.
590 { return this->convert
<String_expression
, EXPRESSION_STRING
>(); }
592 // If this is a conversion expression, return the Type_conversion_expression
593 // structure. Otherwise, return NULL.
594 Type_conversion_expression
*
595 conversion_expression()
596 { return this->convert
<Type_conversion_expression
, EXPRESSION_CONVERSION
>(); }
598 // If this is an unsafe conversion expression, return the
599 // Unsafe_type_conversion_expression structure. Otherwise, return NULL.
600 Unsafe_type_conversion_expression
*
601 unsafe_conversion_expression()
603 return this->convert
<Unsafe_type_conversion_expression
,
604 EXPRESSION_UNSAFE_CONVERSION
>();
607 // Return whether this is the expression nil.
609 is_nil_expression() const
610 { return this->classification_
== EXPRESSION_NIL
; }
612 // If this is an indirection through a pointer, return the
613 // expression being pointed through. Otherwise return this.
617 // If this is a unary expression, return the Unary_expression
618 // structure. Otherwise return NULL.
621 { return this->convert
<Unary_expression
, EXPRESSION_UNARY
>(); }
623 // If this is a binary expression, return the Binary_expression
624 // structure. Otherwise return NULL.
627 { return this->convert
<Binary_expression
, EXPRESSION_BINARY
>(); }
629 // If this is a string concatenation expression, return the
630 // String_concat_expression structure. Otherwise, return NULL.
631 String_concat_expression
*
632 string_concat_expression()
634 return this->convert
<String_concat_expression
, EXPRESSION_STRING_CONCAT
>();
637 // If this is a call expression, return the Call_expression
638 // structure. Otherwise, return NULL. This is a controlled dynamic
642 { return this->convert
<Call_expression
, EXPRESSION_CALL
>(); }
644 // If this is a call_result expression, return the Call_result_expression
645 // structure. Otherwise, return NULL. This is a controlled dynamic
647 Call_result_expression
*
648 call_result_expression()
649 { return this->convert
<Call_result_expression
, EXPRESSION_CALL_RESULT
>(); }
651 // If this is an expression which refers to a function, return the
652 // Func_expression structure. Otherwise, return NULL.
655 { return this->convert
<Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
657 const Func_expression
*
658 func_expression() const
659 { return this->convert
<const Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
661 // If this is an expression which refers to an unknown name, return
662 // the Unknown_expression structure. Otherwise, return NULL.
665 { return this->convert
<Unknown_expression
, EXPRESSION_UNKNOWN_REFERENCE
>(); }
667 const Unknown_expression
*
668 unknown_expression() const
670 return this->convert
<const Unknown_expression
,
671 EXPRESSION_UNKNOWN_REFERENCE
>();
674 // If this is an index expression, return the Index_expression
675 // structure. Otherwise, return NULL.
678 { return this->convert
<Index_expression
, EXPRESSION_INDEX
>(); }
680 // If this is an expression which refers to indexing in a array,
681 // return the Array_index_expression structure. Otherwise, return
683 Array_index_expression
*
684 array_index_expression()
685 { return this->convert
<Array_index_expression
, EXPRESSION_ARRAY_INDEX
>(); }
687 // If this is an expression which refers to indexing in a string,
688 // return the String_index_expression structure. Otherwise, return
690 String_index_expression
*
691 string_index_expression()
692 { return this->convert
<String_index_expression
, EXPRESSION_STRING_INDEX
>(); }
694 // If this is an expression which refers to indexing in a map,
695 // return the Map_index_expression structure. Otherwise, return
697 Map_index_expression
*
698 map_index_expression()
699 { return this->convert
<Map_index_expression
, EXPRESSION_MAP_INDEX
>(); }
701 // If this is a bound method expression, return the
702 // Bound_method_expression structure. Otherwise, return NULL.
703 Bound_method_expression
*
704 bound_method_expression()
705 { return this->convert
<Bound_method_expression
, EXPRESSION_BOUND_METHOD
>(); }
707 // If this is a reference to a field in a struct, return the
708 // Field_reference_expression structure. Otherwise, return NULL.
709 Field_reference_expression
*
710 field_reference_expression()
712 return this->convert
<Field_reference_expression
,
713 EXPRESSION_FIELD_REFERENCE
>();
716 // If this is a reference to a field in an interface, return the
717 // Interface_field_reference_expression structure. Otherwise,
719 Interface_field_reference_expression
*
720 interface_field_reference_expression()
722 return this->convert
<Interface_field_reference_expression
,
723 EXPRESSION_INTERFACE_FIELD_REFERENCE
>();
726 // If this is an allocation expression, return the Allocation_expression
727 // structure. Otherwise, return NULL.
728 Allocation_expression
*
729 allocation_expression()
730 { return this->convert
<Allocation_expression
, EXPRESSION_ALLOCATION
>(); }
732 // If this is a general composite literal, return the
733 // Composite_literal_expression structure. Otherwise, return NULL.
734 Composite_literal_expression
*
737 return this->convert
<Composite_literal_expression
,
738 EXPRESSION_COMPOSITE_LITERAL
>();
741 // If this is a struct composite literal, return the
742 // Struct_construction_expression structure. Otherwise, return NULL.
743 Struct_construction_expression
*
746 return this->convert
<Struct_construction_expression
,
747 EXPRESSION_STRUCT_CONSTRUCTION
>();
750 // If this is a array composite literal, return the
751 // Array_construction_expression structure. Otherwise, return NULL.
752 Fixed_array_construction_expression
*
755 return this->convert
<Fixed_array_construction_expression
,
756 EXPRESSION_FIXED_ARRAY_CONSTRUCTION
>();
759 // If this is a slice composite literal, return the
760 // Slice_construction_expression structure. Otherwise, return NULL.
761 Slice_construction_expression
*
764 return this->convert
<Slice_construction_expression
,
765 EXPRESSION_SLICE_CONSTRUCTION
>();
768 // If this is a map composite literal, return the
769 // Map_construction_expression structure. Otherwise, return NULL.
770 Map_construction_expression
*
773 return this->convert
<Map_construction_expression
,
774 EXPRESSION_MAP_CONSTRUCTION
>();
777 // If this is a type guard expression, return the
778 // Type_guard_expression structure. Otherwise, return NULL.
779 Type_guard_expression
*
780 type_guard_expression()
781 { return this->convert
<Type_guard_expression
, EXPRESSION_TYPE_GUARD
>(); }
783 // If this is a heap expression, returhn the Heap_expression structure.
784 // Otherwise, return NULL.
787 { return this->convert
<Heap_expression
, EXPRESSION_HEAP
>(); }
789 // If this is a receive expression, return the Receive_expression
790 // structure. Otherwise, return NULL.
793 { return this->convert
<Receive_expression
, EXPRESSION_RECEIVE
>(); }
795 // If this is a conditional expression, return the Conditional_expression
796 // structure. Otherwise, return NULL.
797 Conditional_expression
*
798 conditional_expression()
799 { return this->convert
<Conditional_expression
, EXPRESSION_CONDITIONAL
>(); }
801 // If this is a compound expression, return the Compound_expression structure.
802 // Otherwise, return NULL.
804 compound_expression()
805 { return this->convert
<Compound_expression
, EXPRESSION_COMPOUND
>(); }
807 // Return true if this is a composite literal.
809 is_composite_literal() const;
811 // Return true if this is a composite literal which is not constant.
813 is_nonconstant_composite_literal() const;
815 // Return true if this is a variable or temporary variable.
819 // Return true if this is a reference to a local variable.
821 is_local_variable() const;
823 // Make the builtin function descriptor type, so that it can be
826 make_func_descriptor_type();
828 // Traverse an expression.
830 traverse(Expression
**, Traverse
*);
832 // Traverse subexpressions of this expression.
834 traverse_subexpressions(Traverse
*);
836 // Lower an expression. This is called immediately after parsing.
837 // FUNCTION is the function we are in; it will be NULL for an
838 // expression initializing a global variable. INSERTER may be used
839 // to insert statements before the statement or initializer
840 // containing this expression; it is normally used to create
841 // temporary variables. IOTA_VALUE is the value that we should give
842 // to any iota expressions. This function must resolve expressions
843 // which could not be fully parsed into their final form. It
844 // returns the same Expression or a new one.
846 lower(Gogo
* gogo
, Named_object
* function
, Statement_inserter
* inserter
,
848 { return this->do_lower(gogo
, function
, inserter
, iota_value
); }
850 // Flatten an expression. This is called after order_evaluation.
851 // FUNCTION is the function we are in; it will be NULL for an
852 // expression initializing a global variable. INSERTER may be used
853 // to insert statements before the statement or initializer
854 // containing this expression; it is normally used to create
855 // temporary variables. This function must resolve expressions
856 // which could not be fully parsed into their final form. It
857 // returns the same Expression or a new one.
859 flatten(Gogo
* gogo
, Named_object
* function
, Statement_inserter
* inserter
)
860 { return this->do_flatten(gogo
, function
, inserter
); }
862 // Determine the real type of an expression with abstract integer,
863 // floating point, or complex type. TYPE_CONTEXT describes the
866 determine_type(const Type_context
*);
868 // Check types in an expression.
870 check_types(Gogo
* gogo
)
871 { this->do_check_types(gogo
); }
873 // Determine the type when there is no context.
875 determine_type_no_context();
877 // Return the current type of the expression. This may be changed
878 // by determine_type.
881 { return this->do_type(); }
883 // Return a copy of an expression.
886 { return this->do_copy(); }
888 // Return whether the expression is addressable--something which may
889 // be used as the operand of the unary & operator.
891 is_addressable() const
892 { return this->do_is_addressable(); }
894 // Note that we are taking the address of this expression. ESCAPES
895 // is true if this address escapes the current function.
897 address_taken(bool escapes
)
898 { this->do_address_taken(escapes
); }
900 // Note that a nil check must be issued for this expression.
903 { this->do_issue_nil_check(); }
905 // Return whether this expression must be evaluated in order
906 // according to the order of evaluation rules. This is basically
907 // true of all expressions with side-effects.
909 must_eval_in_order() const
910 { return this->do_must_eval_in_order(); }
912 // Return whether subexpressions of this expression must be
913 // evaluated in order. This is true of index expressions and
914 // pointer indirections. This sets *SKIP to the number of
915 // subexpressions to skip during traversing, as index expressions
916 // only requiring moving the index, not the array.
918 must_eval_subexpressions_in_order(int* skip
) const
921 return this->do_must_eval_subexpressions_in_order(skip
);
924 // Return the backend representation for this expression.
926 get_backend(Translate_context
*);
928 // Return an expression handling any conversions which must be done during
931 convert_for_assignment(Gogo
*, Type
* lhs_type
, Expression
* rhs
,
934 // Return an expression converting a value of one interface type to another
935 // interface type. If FOR_TYPE_GUARD is true this is for a type
938 convert_interface_to_interface(Type
* lhs_type
,
939 Expression
* rhs
, bool for_type_guard
,
942 // Return a backend expression implementing the comparison LEFT OP RIGHT.
943 // TYPE is the type of both sides.
945 comparison(Translate_context
*, Type
* result_type
, Operator op
,
946 Expression
* left
, Expression
* right
, Location
);
948 // Return the backend expression for the numeric constant VAL.
950 backend_numeric_constant_expression(Translate_context
*,
951 Numeric_constant
* val
);
953 // Export the expression. This is only used for constants. It will
954 // be used for things like values of named constants and sizes of
957 export_expression(Export
* exp
) const
958 { this->do_export(exp
); }
960 // Import an expression.
962 import_expression(Import
*);
964 // Return an expression which checks that VAL, of arbitrary integer type,
965 // is non-negative and is not more than the maximum integer value.
967 check_bounds(Expression
* val
, Location
);
969 // Dump an expression to a dump constext.
971 dump_expression(Ast_dump_context
*) const;
974 // May be implemented by child class: traverse the expressions.
976 do_traverse(Traverse
*);
978 // Return a lowered expression.
980 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
983 // Return a flattened expression.
985 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*)
989 // Return whether this is a constant expression.
991 do_is_constant() const
994 // Return whether this is an immutable expression.
996 do_is_immutable() const
999 // Return whether this is a constant expression of numeric type, and
1000 // set the Numeric_constant to the value.
1002 do_numeric_constant_value(Numeric_constant
*) const
1005 // Return whether this is a constant expression of string type, and
1006 // set VAL to the value.
1008 do_string_constant_value(std::string
*) const
1011 // Called by the parser if the value is being discarded.
1013 do_discarding_value();
1015 // Child class holds type.
1019 // Child class implements determining type information.
1021 do_determine_type(const Type_context
*) = 0;
1023 // Child class implements type checking if needed.
1025 do_check_types(Gogo
*)
1028 // Child class implements copying.
1032 // Child class implements whether the expression is addressable.
1034 do_is_addressable() const
1037 // Child class implements taking the address of an expression.
1039 do_address_taken(bool)
1042 // Child class implements issuing a nil check if the address is taken.
1044 do_issue_nil_check()
1047 // Child class implements whether this expression must be evaluated
1050 do_must_eval_in_order() const
1053 // Child class implements whether this expressions requires that
1054 // subexpressions be evaluated in order. The child implementation
1055 // may set *SKIP if it should be non-zero.
1057 do_must_eval_subexpressions_in_order(int* /* skip */) const
1060 // Child class implements conversion to backend representation.
1061 virtual Bexpression
*
1062 do_get_backend(Translate_context
*) = 0;
1064 // Child class implements export.
1066 do_export(Export
*) const;
1068 // For children to call to give an error for an unused value.
1070 unused_value_error();
1072 // For children to call when they detect that they are in error.
1076 // For children to call to report an error conveniently.
1078 report_error(const char*);
1080 // Child class implements dumping to a dump context.
1082 do_dump_expression(Ast_dump_context
*) const = 0;
1084 // Varargs lowering creates a slice object (unnamed compiler temp)
1085 // to contain the variable length collection of values. The enum
1086 // below tells the lowering routine whether it can mark that temp
1087 // as non-escaping or not. For general varargs calls it is not always
1088 // safe to stack-allocated the storage, but for specific cases (ex:
1089 // call to append()) it is legal.
1090 enum Slice_storage_escape_disp
1092 SLICE_STORAGE_MAY_ESCAPE
,
1093 SLICE_STORAGE_DOES_NOT_ESCAPE
1097 // Convert to the desired statement classification, or return NULL.
1098 // This is a controlled dynamic cast.
1099 template<typename Expression_class
,
1100 Expression_classification expr_classification
>
1104 return (this->classification_
== expr_classification
1105 ? static_cast<Expression_class
*>(this)
1109 template<typename Expression_class
,
1110 Expression_classification expr_classification
>
1111 const Expression_class
*
1114 return (this->classification_
== expr_classification
1115 ? static_cast<const Expression_class
*>(this)
1120 convert_type_to_interface(Type
*, Expression
*, Location
);
1123 get_interface_type_descriptor(Expression
*);
1126 convert_interface_to_type(Type
*, Expression
*, Location
);
1128 // The expression classification.
1129 Expression_classification classification_
;
1130 // The location in the input file.
1134 // A list of Expressions.
1136 class Expression_list
1143 // Return whether the list is empty.
1146 { return this->entries_
.empty(); }
1148 // Return the number of entries in the list.
1151 { return this->entries_
.size(); }
1153 // Add an entry to the end of the list.
1155 push_back(Expression
* expr
)
1156 { this->entries_
.push_back(expr
); }
1159 append(Expression_list
* add
)
1160 { this->entries_
.insert(this->entries_
.end(), add
->begin(), add
->end()); }
1162 // Reserve space in the list.
1164 reserve(size_t size
)
1165 { this->entries_
.reserve(size
); }
1167 // Traverse the expressions in the list.
1169 traverse(Traverse
*);
1175 // Return true if the list contains an error expression.
1177 contains_error() const;
1179 // Retrieve an element by index.
1182 { return this->entries_
.at(i
); }
1184 // Return the first and last elements.
1187 { return this->entries_
.front(); }
1191 { return this->entries_
.front(); }
1195 { return this->entries_
.back(); }
1199 { return this->entries_
.back(); }
1203 typedef std::vector
<Expression
*>::iterator iterator
;
1204 typedef std::vector
<Expression
*>::const_iterator const_iterator
;
1208 { return this->entries_
.begin(); }
1212 { return this->entries_
.begin(); }
1216 { return this->entries_
.end(); }
1220 { return this->entries_
.end(); }
1225 { this->entries_
.erase(p
); }
1228 std::vector
<Expression
*> entries_
;
1231 // An abstract base class for an expression which is only used by the
1232 // parser, and is lowered in the lowering pass.
1234 class Parser_expression
: public Expression
1237 Parser_expression(Expression_classification classification
,
1239 : Expression(classification
, location
)
1244 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int) = 0;
1250 do_determine_type(const Type_context
*)
1251 { go_unreachable(); }
1254 do_check_types(Gogo
*)
1255 { go_unreachable(); }
1258 do_get_backend(Translate_context
*)
1259 { go_unreachable(); }
1262 // An expression which is simply a variable.
1264 class Var_expression
: public Expression
1267 Var_expression(Named_object
* variable
, Location location
)
1268 : Expression(EXPRESSION_VAR_REFERENCE
, location
),
1272 // Return the variable.
1274 named_object() const
1275 { return this->variable_
; }
1279 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1285 do_determine_type(const Type_context
*);
1292 do_is_addressable() const
1296 do_address_taken(bool);
1299 do_get_backend(Translate_context
*);
1302 do_dump_expression(Ast_dump_context
*) const;
1305 // The variable we are referencing.
1306 Named_object
* variable_
;
1309 // A reference to a variable within an enclosing function.
1311 class Enclosed_var_expression
: public Expression
1314 Enclosed_var_expression(Expression
* reference
, Named_object
* variable
,
1316 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE
, location
),
1317 reference_(reference
), variable_(variable
)
1320 // The reference to the enclosed variable. This will be an indirection of the
1321 // the field stored within closure variable.
1324 { return this->reference_
; }
1326 // The variable being enclosed and referenced.
1329 { return this->variable_
; }
1333 do_traverse(Traverse
*);
1336 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1339 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1343 { return this->reference_
->type(); }
1346 do_determine_type(const Type_context
* context
)
1347 { return this->reference_
->determine_type(context
); }
1354 do_is_addressable() const
1355 { return this->reference_
->is_addressable(); }
1358 do_address_taken(bool escapes
);
1361 do_get_backend(Translate_context
* context
)
1362 { return this->reference_
->get_backend(context
); }
1365 do_dump_expression(Ast_dump_context
*) const;
1368 // The reference to the enclosed variable.
1369 Expression
* reference_
;
1370 // The variable being enclosed.
1371 Named_object
* variable_
;
1374 // A reference to a temporary variable.
1376 class Temporary_reference_expression
: public Expression
1379 Temporary_reference_expression(Temporary_statement
* statement
,
1381 : Expression(EXPRESSION_TEMPORARY_REFERENCE
, location
),
1382 statement_(statement
), is_lvalue_(false)
1385 // The temporary that this expression refers to.
1386 Temporary_statement
*
1388 { return this->statement_
; }
1390 // Indicate that this reference appears on the left hand side of an
1391 // assignment statement.
1394 { this->is_lvalue_
= true; }
1401 do_determine_type(const Type_context
*)
1406 { return make_temporary_reference(this->statement_
, this->location()); }
1409 do_is_addressable() const
1413 do_address_taken(bool);
1416 do_get_backend(Translate_context
*);
1419 do_dump_expression(Ast_dump_context
*) const;
1422 // The statement where the temporary variable is defined.
1423 Temporary_statement
* statement_
;
1424 // Whether this reference appears on the left hand side of an
1425 // assignment statement.
1429 // Set and use a temporary variable.
1431 class Set_and_use_temporary_expression
: public Expression
1434 Set_and_use_temporary_expression(Temporary_statement
* statement
,
1435 Expression
* expr
, Location location
)
1436 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY
, location
),
1437 statement_(statement
), expr_(expr
)
1440 // Return the temporary.
1441 Temporary_statement
*
1443 { return this->statement_
; }
1445 // Return the expression.
1448 { return this->expr_
; }
1452 do_traverse(Traverse
* traverse
)
1453 { return Expression::traverse(&this->expr_
, traverse
); }
1459 do_determine_type(const Type_context
*);
1464 return make_set_and_use_temporary(this->statement_
, this->expr_
,
1469 do_is_addressable() const
1473 do_address_taken(bool);
1476 do_get_backend(Translate_context
*);
1479 do_dump_expression(Ast_dump_context
*) const;
1482 // The statement where the temporary variable is defined.
1483 Temporary_statement
* statement_
;
1484 // The expression to assign to the temporary.
1488 // A string expression.
1490 class String_expression
: public Expression
1493 String_expression(const std::string
& val
, Location location
)
1494 : Expression(EXPRESSION_STRING
, location
),
1495 val_(val
), type_(NULL
)
1500 { return this->val_
; }
1507 do_is_constant() const
1511 do_is_immutable() const
1515 do_string_constant_value(std::string
* val
) const
1525 do_determine_type(const Type_context
*);
1532 do_get_backend(Translate_context
*);
1534 // Write string literal to a string dump.
1536 export_string(String_dump
* exp
, const String_expression
* str
);
1539 do_export(Export
*) const;
1542 do_dump_expression(Ast_dump_context
*) const;
1545 // The string value. This is immutable.
1546 const std::string val_
;
1547 // The type as determined by context.
1551 // A type conversion expression.
1553 class Type_conversion_expression
: public Expression
1556 Type_conversion_expression(Type
* type
, Expression
* expr
,
1558 : Expression(EXPRESSION_CONVERSION
, location
),
1559 type_(type
), expr_(expr
), may_convert_function_types_(false)
1562 // Return the type to which we are converting.
1565 { return this->type_
; }
1567 // Return the expression which we are converting.
1570 { return this->expr_
; }
1572 // Permit converting from one function type to another. This is
1573 // used internally for method expressions.
1575 set_may_convert_function_types()
1577 this->may_convert_function_types_
= true;
1580 // Import a type conversion expression.
1586 do_traverse(Traverse
* traverse
);
1589 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1592 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1595 do_is_constant() const;
1598 do_is_immutable() const;
1601 do_numeric_constant_value(Numeric_constant
*) const;
1604 do_string_constant_value(std::string
*) const;
1608 { return this->type_
; }
1611 do_determine_type(const Type_context
*);
1614 do_check_types(Gogo
*);
1619 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
1624 do_get_backend(Translate_context
* context
);
1627 do_export(Export
*) const;
1630 do_dump_expression(Ast_dump_context
*) const;
1633 // The type to convert to.
1635 // The expression to convert.
1637 // True if this is permitted to convert function types. This is
1638 // used internally for method expressions.
1639 bool may_convert_function_types_
;
1642 // An unsafe type conversion, used to pass values to builtin functions.
1644 class Unsafe_type_conversion_expression
: public Expression
1647 Unsafe_type_conversion_expression(Type
* type
, Expression
* expr
,
1649 : Expression(EXPRESSION_UNSAFE_CONVERSION
, location
),
1650 type_(type
), expr_(expr
)
1655 { return this->expr_
; }
1659 do_traverse(Traverse
* traverse
);
1662 do_is_immutable() const;
1666 { return this->type_
; }
1669 do_determine_type(const Type_context
*)
1670 { this->expr_
->determine_type_no_context(); }
1675 return new Unsafe_type_conversion_expression(this->type_
,
1676 this->expr_
->copy(),
1681 do_get_backend(Translate_context
*);
1684 do_dump_expression(Ast_dump_context
*) const;
1687 // The type to convert to.
1689 // The expression to convert.
1693 // A Unary expression.
1695 class Unary_expression
: public Expression
1698 Unary_expression(Operator op
, Expression
* expr
, Location location
)
1699 : Expression(EXPRESSION_UNARY
, location
),
1700 op_(op
), escapes_(true), create_temp_(false), is_gc_root_(false),
1701 is_slice_init_(false), expr_(expr
), issue_nil_check_(false)
1704 // Return the operator.
1707 { return this->op_
; }
1709 // Return the operand.
1712 { return this->expr_
; }
1714 // Record that an address expression does not escape.
1716 set_does_not_escape()
1718 go_assert(this->op_
== OPERATOR_AND
);
1719 this->escapes_
= false;
1722 // Record that this is an address expression which should create a
1723 // temporary variable if necessary. This is used for method calls.
1727 go_assert(this->op_
== OPERATOR_AND
);
1728 this->create_temp_
= true;
1731 // Record that this is an address expression of a GC root, which is a
1732 // mutable composite literal. This used for registering GC variables.
1736 go_assert(this->op_
== OPERATOR_AND
);
1737 this->is_gc_root_
= true;
1740 // Record that this is an address expression of a slice value initializer,
1741 // which is mutable if the values are not copied to the heap.
1745 go_assert(this->op_
== OPERATOR_AND
);
1746 this->is_slice_init_
= true;
1749 // Apply unary opcode OP to UNC, setting NC. Return true if this
1750 // could be done, false if not. Issue errors for overflow.
1752 eval_constant(Operator op
, const Numeric_constant
* unc
,
1753 Location
, Numeric_constant
* nc
);
1760 do_traverse(Traverse
* traverse
)
1761 { return Expression::traverse(&this->expr_
, traverse
); }
1764 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1767 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1770 do_is_constant() const;
1773 do_is_immutable() const
1775 return (this->expr_
->is_immutable()
1776 || (this->op_
== OPERATOR_AND
&& this->expr_
->is_variable()));
1780 do_numeric_constant_value(Numeric_constant
*) const;
1786 do_determine_type(const Type_context
*);
1789 do_check_types(Gogo
*);
1794 return Expression::make_unary(this->op_
, this->expr_
->copy(),
1799 do_must_eval_subexpressions_in_order(int*) const
1800 { return this->op_
== OPERATOR_MULT
; }
1803 do_is_addressable() const
1804 { return this->op_
== OPERATOR_MULT
; }
1807 do_get_backend(Translate_context
*);
1810 do_export(Export
*) const;
1813 do_dump_expression(Ast_dump_context
*) const;
1816 do_issue_nil_check()
1817 { this->issue_nil_check_
= (this->op_
== OPERATOR_MULT
); }
1820 // The unary operator to apply.
1822 // Normally true. False if this is an address expression which does
1823 // not escape the current function.
1825 // True if this is an address expression which should create a
1826 // temporary variable if necessary.
1828 // True if this is an address expression for a GC root. A GC root is a
1829 // special struct composite literal that is mutable when addressed, meaning
1830 // it cannot be represented as an immutable_struct in the backend.
1832 // True if this is an address expression for a slice value with an immutable
1833 // initializer. The initializer for a slice's value pointer has an array
1834 // type, meaning it cannot be represented as an immutable_struct in the
1836 bool is_slice_init_
;
1839 // Whether or not to issue a nil check for this expression if its address
1841 bool issue_nil_check_
;
1844 // A binary expression.
1846 class Binary_expression
: public Expression
1849 Binary_expression(Operator op
, Expression
* left
, Expression
* right
,
1851 : Expression(EXPRESSION_BINARY
, location
),
1852 op_(op
), left_(left
), right_(right
), type_(NULL
)
1855 // Return the operator.
1858 { return this->op_
; }
1860 // Return the left hand expression.
1863 { return this->left_
; }
1865 // Return the right hand expression.
1868 { return this->right_
; }
1870 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1871 // Return true if this could be done, false if not. Issue errors at
1872 // LOCATION as appropriate.
1874 eval_constant(Operator op
, Numeric_constant
* left_nc
,
1875 Numeric_constant
* right_nc
, Location location
,
1876 Numeric_constant
* nc
);
1878 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1879 // *RESULT. Return true if this could be done, false if not. Issue
1880 // errors at LOCATION as appropriate.
1882 compare_constant(Operator op
, Numeric_constant
* left_nc
,
1883 Numeric_constant
* right_nc
, Location location
,
1889 // Report an error if OP can not be applied to TYPE. Return whether
1890 // it can. OTYPE is the type of the other operand.
1892 check_operator_type(Operator op
, Type
* type
, Type
* otype
, Location
);
1894 // Set *RESULT_TYPE to the resulting type when OP is applied to
1895 // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
1896 // success, false on failure.
1898 operation_type(Operator op
, Type
* left_type
, Type
* right_type
,
1899 Type
** result_type
);
1903 do_traverse(Traverse
* traverse
);
1906 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1909 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1912 do_is_constant() const
1913 { return this->left_
->is_constant() && this->right_
->is_constant(); }
1916 do_is_immutable() const
1917 { return this->left_
->is_immutable() && this->right_
->is_immutable(); }
1920 do_numeric_constant_value(Numeric_constant
*) const;
1923 do_discarding_value();
1929 do_determine_type(const Type_context
*);
1932 do_check_types(Gogo
*);
1937 return Expression::make_binary(this->op_
, this->left_
->copy(),
1938 this->right_
->copy(), this->location());
1942 do_get_backend(Translate_context
*);
1945 do_export(Export
*) const;
1948 do_dump_expression(Ast_dump_context
*) const;
1952 cmp_to_bool(Operator op
, int cmp
);
1955 eval_integer(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
1956 Location
, Numeric_constant
*);
1959 eval_float(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
1960 Location
, Numeric_constant
*);
1963 eval_complex(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
1964 Location
, Numeric_constant
*);
1967 compare_integer(const Numeric_constant
*, const Numeric_constant
*, int*);
1970 compare_float(const Numeric_constant
*, const Numeric_constant
*, int*);
1973 compare_complex(const Numeric_constant
*, const Numeric_constant
*, int*);
1976 lower_struct_comparison(Gogo
*, Statement_inserter
*);
1979 lower_array_comparison(Gogo
*, Statement_inserter
*);
1982 lower_interface_value_comparison(Gogo
*, Statement_inserter
*);
1985 lower_compare_to_memcmp(Gogo
*, Statement_inserter
*);
1988 operand_address(Statement_inserter
*, Expression
*);
1990 // The binary operator to apply.
1992 // The left hand side operand.
1994 // The right hand side operand.
1996 // The type of a comparison operation.
2000 // A string concatenation expression. This is a sequence of strings
2001 // added together. It is created when lowering Binary_expression.
2003 class String_concat_expression
: public Expression
2006 String_concat_expression(Expression_list
* exprs
)
2007 : Expression(EXPRESSION_STRING_CONCAT
, exprs
->front()->location()),
2011 // Return the list of string expressions to be concatenated.
2014 { return this->exprs_
; }
2018 do_traverse(Traverse
* traverse
)
2019 { return this->exprs_
->traverse(traverse
); }
2022 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
2026 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2029 do_is_constant() const;
2032 do_is_immutable() const;
2038 do_determine_type(const Type_context
*);
2041 do_check_types(Gogo
*);
2045 { return Expression::make_string_concat(this->exprs_
->copy()); }
2048 do_get_backend(Translate_context
*)
2049 { go_unreachable(); }
2052 do_export(Export
*) const
2053 { go_unreachable(); }
2056 do_dump_expression(Ast_dump_context
*) const;
2059 // The string expressions to concatenate.
2060 Expression_list
* exprs_
;
2063 // A call expression. The go statement needs to dig inside this.
2065 class Call_expression
: public Expression
2068 Call_expression(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
2070 : Expression(EXPRESSION_CALL
, location
),
2071 fn_(fn
), args_(args
), type_(NULL
), results_(NULL
), call_(NULL
),
2072 call_temp_(NULL
), expected_result_count_(0), is_varargs_(is_varargs
),
2073 varargs_are_lowered_(false), types_are_determined_(false),
2074 is_deferred_(false), is_concurrent_(false), issued_error_(false),
2075 is_multi_value_arg_(false), is_flattened_(false)
2078 // The function to call.
2081 { return this->fn_
; }
2086 { return this->args_
; }
2088 const Expression_list
*
2090 { return this->args_
; }
2092 // Get the function type.
2094 get_function_type() const;
2096 // Return the number of values this call will return.
2098 result_count() const;
2100 // Return the temporary variable which holds result I. This is only
2101 // valid after the expression has been lowered, and is only valid
2102 // for calls which return multiple results.
2103 Temporary_statement
*
2104 result(size_t i
) const;
2106 // Set the number of results expected from this call. This is used
2107 // when the call appears in a context that expects multiple results,
2108 // such as a, b = f().
2110 set_expected_result_count(size_t);
2112 // Return whether this is a call to the predeclared function
2115 is_recover_call() const;
2117 // Set the argument for a call to recover.
2119 set_recover_arg(Expression
*);
2121 // Whether the last argument is a varargs argument (f(a...)).
2124 { return this->is_varargs_
; }
2126 // Return whether varargs have already been lowered.
2128 varargs_are_lowered() const
2129 { return this->varargs_are_lowered_
; }
2131 // Note that varargs have already been lowered.
2133 set_varargs_are_lowered()
2134 { this->varargs_are_lowered_
= true; }
2136 // Whether this call is being deferred.
2139 { return this->is_deferred_
; }
2141 // Note that the call is being deferred.
2144 { this->is_deferred_
= true; }
2146 // Whether this call is concurrently executed.
2148 is_concurrent() const
2149 { return this->is_concurrent_
; }
2151 // Note that the call is concurrently executed.
2154 { this->is_concurrent_
= true; }
2156 // We have found an error with this call expression; return true if
2157 // we should report it.
2161 // Whether or not this call contains errors, either in the call or the
2162 // arguments to the call.
2164 is_erroneous_call();
2166 // Whether this call returns multiple results that are used as an
2167 // multi-valued argument.
2169 is_multi_value_arg() const
2170 { return this->is_multi_value_arg_
; }
2172 // Note this call is used as a multi-valued argument.
2174 set_is_multi_value_arg()
2175 { this->is_multi_value_arg_
= true; }
2179 do_traverse(Traverse
*);
2182 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2185 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2188 do_discarding_value()
2195 do_determine_type(const Type_context
*);
2198 do_check_types(Gogo
*);
2204 do_must_eval_in_order() const;
2206 virtual Bexpression
*
2207 do_get_backend(Translate_context
*);
2210 do_is_recover_call() const;
2213 do_set_recover_arg(Expression
*);
2215 // Let a builtin expression change the argument list.
2217 set_args(Expression_list
* args
)
2218 { this->args_
= args
; }
2220 // Let a builtin expression lower varargs.
2222 lower_varargs(Gogo
*, Named_object
* function
, Statement_inserter
* inserter
,
2223 Type
* varargs_type
, size_t param_count
,
2224 Slice_storage_escape_disp escape_disp
);
2226 // Let a builtin expression check whether types have been
2229 determining_types();
2232 do_dump_expression(Ast_dump_context
*) const;
2236 check_argument_type(int, const Type
*, const Type
*, Location
, bool);
2239 lower_to_builtin(Named_object
**, const char*, int);
2242 interface_method_function(Interface_field_reference_expression
*,
2246 set_results(Translate_context
*, Bexpression
*);
2248 // The function to call.
2250 // The arguments to pass. This may be NULL if there are no
2252 Expression_list
* args_
;
2253 // The type of the expression, to avoid recomputing it.
2255 // The list of temporaries which will hold the results if the
2256 // function returns a tuple.
2257 std::vector
<Temporary_statement
*>* results_
;
2258 // The backend expression for the call, used for a call which returns a tuple.
2260 // A temporary variable to store this call if the function returns a tuple.
2261 Temporary_statement
* call_temp_
;
2262 // If not 0, the number of results expected from this call, when
2263 // used in a context that expects multiple values.
2264 size_t expected_result_count_
;
2265 // True if the last argument is a varargs argument (f(a...)).
2267 // True if varargs have already been lowered.
2268 bool varargs_are_lowered_
;
2269 // True if types have been determined.
2270 bool types_are_determined_
;
2271 // True if the call is an argument to a defer statement.
2273 // True if the call is an argument to a go statement.
2274 bool is_concurrent_
;
2275 // True if we reported an error about a mismatch between call
2276 // results and uses. This is to avoid producing multiple errors
2277 // when there are multiple Call_result_expressions.
2279 // True if this call is used as an argument that returns multiple results.
2280 bool is_multi_value_arg_
;
2281 // True if this expression has already been flattened.
2285 // A single result from a call which returns multiple results.
2287 class Call_result_expression
: public Expression
2290 Call_result_expression(Call_expression
* call
, unsigned int index
)
2291 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
2292 call_(call
), index_(index
)
2297 { return this->call_
; }
2301 { return this->index_
; }
2305 do_traverse(Traverse
*);
2311 do_determine_type(const Type_context
*);
2314 do_check_types(Gogo
*);
2319 return new Call_result_expression(this->call_
->call_expression(),
2324 do_must_eval_in_order() const
2328 do_get_backend(Translate_context
*);
2331 do_dump_expression(Ast_dump_context
*) const;
2334 // The underlying call expression.
2336 // Which result we want.
2337 unsigned int index_
;
2340 // An expression which represents a pointer to a function.
2342 class Func_expression
: public Expression
2345 Func_expression(Named_object
* function
, Expression
* closure
,
2347 : Expression(EXPRESSION_FUNC_REFERENCE
, location
),
2348 function_(function
), closure_(closure
),
2349 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS
)
2352 // Return the object associated with the function.
2354 named_object() const
2355 { return this->function_
; }
2357 // Return the closure for this function. This will return NULL if
2358 // the function has no closure, which is the normal case.
2361 { return this->closure_
; }
2363 // Return whether this is a reference to a runtime function.
2365 is_runtime_function() const
2366 { return this->runtime_code_
!= Runtime::NUMBER_OF_FUNCTIONS
; }
2368 // Return the runtime code for this function expression.
2369 // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
2370 // runtime function.
2372 runtime_code() const
2373 { return this->runtime_code_
; }
2375 // Set the runtime code for this function expression.
2377 set_runtime_code(Runtime::Function code
)
2378 { this->runtime_code_
= code
; }
2380 // Return a backend expression for the code of a function.
2382 get_code_pointer(Gogo
*, Named_object
* function
, Location loc
);
2386 do_traverse(Traverse
*);
2392 do_determine_type(const Type_context
*)
2394 if (this->closure_
!= NULL
)
2395 this->closure_
->determine_type_no_context();
2401 return Expression::make_func_reference(this->function_
,
2402 (this->closure_
== NULL
2404 : this->closure_
->copy()),
2409 do_get_backend(Translate_context
*);
2412 do_dump_expression(Ast_dump_context
*) const;
2415 // The function itself.
2416 Named_object
* function_
;
2417 // A closure. This is normally NULL. For a nested function, it may
2418 // be a struct holding pointers to all the variables referenced by
2419 // this function and defined in enclosing functions.
2420 Expression
* closure_
;
2421 // The runtime code for the referenced function.
2422 Runtime::Function runtime_code_
;
2425 // A function descriptor. A function descriptor is a struct with a
2426 // single field pointing to the function code. This is used for
2427 // functions without closures.
2429 class Func_descriptor_expression
: public Expression
2432 Func_descriptor_expression(Named_object
* fn
);
2434 // Make the function descriptor type, so that it can be converted.
2436 make_func_descriptor_type();
2440 do_traverse(Traverse
*);
2446 do_determine_type(const Type_context
*)
2451 { return Expression::make_func_descriptor(this->fn_
); }
2454 do_is_addressable() const
2458 do_get_backend(Translate_context
*);
2461 do_dump_expression(Ast_dump_context
* context
) const;
2464 // The type of all function descriptors.
2465 static Type
* descriptor_type
;
2467 // The function for which this is the descriptor.
2469 // The descriptor variable.
2473 // A reference to an unknown name.
2475 class Unknown_expression
: public Parser_expression
2478 Unknown_expression(Named_object
* named_object
, Location location
)
2479 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE
, location
),
2480 named_object_(named_object
), no_error_message_(false),
2481 is_composite_literal_key_(false)
2484 // The associated named object.
2486 named_object() const
2487 { return this->named_object_
; }
2489 // The name of the identifier which was unknown.
2493 // Call this to indicate that we should not give an error if this
2494 // name is never defined. This is used to avoid knock-on errors
2495 // during an erroneous parse.
2497 set_no_error_message()
2498 { this->no_error_message_
= true; }
2500 // Note that this expression is being used as the key in a composite
2501 // literal, so it may be OK if it is not resolved.
2503 set_is_composite_literal_key()
2504 { this->is_composite_literal_key_
= true; }
2506 // Note that this expression should no longer be treated as a
2507 // composite literal key.
2509 clear_is_composite_literal_key()
2510 { this->is_composite_literal_key_
= false; }
2514 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2518 { return new Unknown_expression(this->named_object_
, this->location()); }
2521 do_dump_expression(Ast_dump_context
*) const;
2524 // The unknown name.
2525 Named_object
* named_object_
;
2526 // True if we should not give errors if this is undefined. This is
2527 // used if there was a parse failure.
2528 bool no_error_message_
;
2529 // True if this is the key in a composite literal.
2530 bool is_composite_literal_key_
;
2533 // An index expression. This is lowered to an array index, a string
2534 // index, or a map index.
2536 class Index_expression
: public Parser_expression
2539 Index_expression(Expression
* left
, Expression
* start
, Expression
* end
,
2540 Expression
* cap
, Location location
)
2541 : Parser_expression(EXPRESSION_INDEX
, location
),
2542 left_(left
), start_(start
), end_(end
), cap_(cap
)
2545 // Dump an index expression, i.e. an expression of the form
2546 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
2548 dump_index_expression(Ast_dump_context
*, const Expression
* expr
,
2549 const Expression
* start
, const Expression
* end
,
2550 const Expression
* cap
);
2554 do_traverse(Traverse
*);
2557 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2562 return new Index_expression(this->left_
->copy(), this->start_
->copy(),
2565 : this->end_
->copy()),
2568 : this->cap_
->copy()),
2573 do_must_eval_subexpressions_in_order(int* skip
) const
2580 do_dump_expression(Ast_dump_context
*) const;
2583 do_issue_nil_check()
2584 { this->left_
->issue_nil_check(); }
2586 // The expression being indexed.
2590 // The second index. This is NULL for an index, non-NULL for a
2593 // The capacity argument. This is NULL for indices and slices that use the
2594 // default capacity, non-NULL for indices and slices that specify the
2599 // An array index. This is used for both indexing and slicing.
2601 class Array_index_expression
: public Expression
2604 Array_index_expression(Expression
* array
, Expression
* start
,
2605 Expression
* end
, Expression
* cap
, Location location
)
2606 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
2607 array_(array
), start_(start
), end_(end
), cap_(cap
), type_(NULL
)
2610 // Return the array.
2613 { return this->array_
; }
2617 { return this->array_
; }
2621 do_traverse(Traverse
*);
2624 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2630 do_determine_type(const Type_context
*);
2633 do_check_types(Gogo
*);
2638 return Expression::make_array_index(this->array_
->copy(),
2639 this->start_
->copy(),
2642 : this->end_
->copy()),
2645 : this->cap_
->copy()),
2650 do_must_eval_subexpressions_in_order(int* skip
) const
2657 do_is_addressable() const;
2660 do_address_taken(bool escapes
)
2661 { this->array_
->address_taken(escapes
); }
2664 do_issue_nil_check()
2665 { this->array_
->issue_nil_check(); }
2668 do_get_backend(Translate_context
*);
2671 do_dump_expression(Ast_dump_context
*) const;
2674 // The array we are getting a value from.
2676 // The start or only index.
2678 // The end index of a slice. This may be NULL for a simple array
2679 // index, or it may be a nil expression for the length of the array.
2681 // The capacity argument of a slice. This may be NULL for an array index or
2684 // The type of the expression.
2688 // A string index. This is used for both indexing and slicing.
2690 class String_index_expression
: public Expression
2693 String_index_expression(Expression
* string
, Expression
* start
,
2694 Expression
* end
, Location location
)
2695 : Expression(EXPRESSION_STRING_INDEX
, location
),
2696 string_(string
), start_(start
), end_(end
)
2699 // Return the string being indexed.
2702 { return this->string_
; }
2706 do_traverse(Traverse
*);
2709 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2715 do_determine_type(const Type_context
*);
2718 do_check_types(Gogo
*);
2723 return Expression::make_string_index(this->string_
->copy(),
2724 this->start_
->copy(),
2727 : this->end_
->copy()),
2732 do_must_eval_subexpressions_in_order(int* skip
) const
2739 do_get_backend(Translate_context
*);
2742 do_dump_expression(Ast_dump_context
*) const;
2745 // The string we are getting a value from.
2746 Expression
* string_
;
2747 // The start or only index.
2749 // The end index of a slice. This may be NULL for a single index,
2750 // or it may be a nil expression for the length of the string.
2754 // An index into a map.
2756 class Map_index_expression
: public Expression
2759 Map_index_expression(Expression
* map
, Expression
* index
,
2761 : Expression(EXPRESSION_MAP_INDEX
, location
),
2762 map_(map
), index_(index
), value_pointer_(NULL
)
2768 { return this->map_
; }
2772 { return this->map_
; }
2774 // Return the index.
2777 { return this->index_
; }
2781 { return this->index_
; }
2783 // Get the type of the map being indexed.
2785 get_map_type() const;
2787 // Return an expression for the map index. This returns an
2788 // expression that evaluates to a pointer to a value in the map. If
2789 // the key is not present in the map, this will return a pointer to
2792 get_value_pointer(Gogo
*);
2796 do_traverse(Traverse
*);
2799 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2805 do_determine_type(const Type_context
*);
2808 do_check_types(Gogo
*);
2813 return Expression::make_map_index(this->map_
->copy(),
2814 this->index_
->copy(),
2819 do_must_eval_subexpressions_in_order(int* skip
) const
2825 // A map index expression is an lvalue but it is not addressable.
2828 do_get_backend(Translate_context
*);
2831 do_dump_expression(Ast_dump_context
*) const;
2834 // The map we are looking into.
2838 // A pointer to the value at this index.
2839 Expression
* value_pointer_
;
2842 // An expression which represents a method bound to its first
2845 class Bound_method_expression
: public Expression
2848 Bound_method_expression(Expression
* expr
, const Method
*method
,
2849 Named_object
* function
, Location location
)
2850 : Expression(EXPRESSION_BOUND_METHOD
, location
),
2851 expr_(expr
), expr_type_(NULL
), method_(method
), function_(function
)
2854 // Return the object which is the first argument.
2857 { return this->expr_
; }
2859 // Return the implicit type of the first argument. This will be
2860 // non-NULL when using a method from an anonymous field without
2861 // using an explicit stub.
2863 first_argument_type() const
2864 { return this->expr_type_
; }
2866 // Return the method.
2869 { return this->method_
; }
2871 // Return the function to call.
2874 { return this->function_
; }
2876 // Set the implicit type of the expression.
2878 set_first_argument_type(Type
* type
)
2879 { this->expr_type_
= type
; }
2881 // Create a thunk to call FUNCTION, for METHOD, when it is used as
2882 // part of a method value.
2883 static Named_object
*
2884 create_thunk(Gogo
*, const Method
* method
, Named_object
* function
);
2888 do_traverse(Traverse
*);
2891 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2897 do_determine_type(const Type_context
*);
2900 do_check_types(Gogo
*);
2905 return new Bound_method_expression(this->expr_
->copy(), this->method_
,
2906 this->function_
, this->location());
2910 do_get_backend(Translate_context
*)
2911 { go_unreachable(); }
2914 do_dump_expression(Ast_dump_context
*) const;
2917 // A mapping from method functions to the thunks we have created for
2919 typedef Unordered_map(Named_object
*, Named_object
*) Method_value_thunks
;
2920 static Method_value_thunks method_value_thunks
;
2922 // The object used to find the method. This is passed to the method
2923 // as the first argument.
2925 // The implicit type of the object to pass to the method. This is
2926 // NULL in the normal case, non-NULL when using a method from an
2927 // anonymous field which does not require a stub.
2930 const Method
* method_
;
2931 // The function to call. This is not the same as
2932 // method_->named_object() when the method has a stub. This will be
2933 // the real function rather than the stub.
2934 Named_object
* function_
;
2937 // A reference to a field in a struct.
2939 class Field_reference_expression
: public Expression
2942 Field_reference_expression(Expression
* expr
, unsigned int field_index
,
2944 : Expression(EXPRESSION_FIELD_REFERENCE
, location
),
2945 expr_(expr
), field_index_(field_index
), implicit_(false), called_fieldtrack_(false)
2948 // Return the struct expression.
2951 { return this->expr_
; }
2953 // Return the field index.
2956 { return this->field_index_
; }
2958 // Return whether this node was implied by an anonymous field.
2961 { return this->implicit_
; }
2964 set_implicit(bool implicit
)
2965 { this->implicit_
= implicit
; }
2967 // Set the struct expression. This is used when parsing.
2969 set_struct_expression(Expression
* expr
)
2971 go_assert(this->expr_
== NULL
);
2977 do_traverse(Traverse
* traverse
)
2978 { return Expression::traverse(&this->expr_
, traverse
); }
2981 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2987 do_determine_type(const Type_context
*)
2988 { this->expr_
->determine_type_no_context(); }
2991 do_check_types(Gogo
*);
2996 return Expression::make_field_reference(this->expr_
->copy(),
3002 do_is_addressable() const
3003 { return this->expr_
->is_addressable(); }
3006 do_address_taken(bool escapes
)
3007 { this->expr_
->address_taken(escapes
); }
3010 do_issue_nil_check()
3011 { this->expr_
->issue_nil_check(); }
3014 do_get_backend(Translate_context
*);
3017 do_dump_expression(Ast_dump_context
*) const;
3020 // The expression we are looking into. This should have a type of
3023 // The zero-based index of the field we are retrieving.
3024 unsigned int field_index_
;
3025 // Whether this node was emitted implicitly for an embedded field,
3026 // that is, expr_ is not the expr_ of the original user node.
3028 // Whether we have already emitted a fieldtrack call.
3029 bool called_fieldtrack_
;
3032 // A reference to a field of an interface.
3034 class Interface_field_reference_expression
: public Expression
3037 Interface_field_reference_expression(Expression
* expr
,
3038 const std::string
& name
,
3040 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE
, location
),
3041 expr_(expr
), name_(name
)
3044 // Return the expression for the interface object.
3047 { return this->expr_
; }
3049 // Return the name of the method to call.
3052 { return this->name_
; }
3054 // Create a thunk to call the method NAME in TYPE when it is used as
3055 // part of a method value.
3056 static Named_object
*
3057 create_thunk(Gogo
*, Interface_type
* type
, const std::string
& name
);
3059 // Return an expression for the pointer to the function to call.
3063 // Return an expression for the first argument to pass to the interface
3064 // function. This is the real object associated with the interface object.
3066 get_underlying_object();
3070 do_traverse(Traverse
* traverse
);
3073 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3079 do_determine_type(const Type_context
*);
3082 do_check_types(Gogo
*);
3087 return Expression::make_interface_field_reference(this->expr_
->copy(),
3093 do_get_backend(Translate_context
*);
3096 do_dump_expression(Ast_dump_context
*) const;
3099 // A mapping from interface types to a list of thunks we have
3100 // created for methods.
3101 typedef std::vector
<std::pair
<std::string
, Named_object
*> > Method_thunks
;
3102 typedef Unordered_map(Interface_type
*, Method_thunks
*)
3103 Interface_method_thunks
;
3104 static Interface_method_thunks interface_method_thunks
;
3106 // The expression for the interface object. This should have a type
3107 // of interface or pointer to interface.
3109 // The field we are retrieving--the name of the method.
3113 // Implement the builtin function new.
3115 class Allocation_expression
: public Expression
3118 Allocation_expression(Type
* type
, Location location
)
3119 : Expression(EXPRESSION_ALLOCATION
, location
),
3120 type_(type
), allocate_on_stack_(false)
3124 set_allocate_on_stack()
3125 { this->allocate_on_stack_
= true; }
3129 do_traverse(Traverse
*);
3135 do_determine_type(const Type_context
*)
3142 do_get_backend(Translate_context
*);
3145 do_dump_expression(Ast_dump_context
*) const;
3148 // The type we are allocating.
3150 // Whether or not this is a stack allocation.
3151 bool allocate_on_stack_
;
3154 // A general composite literal. This is lowered to a type specific
3157 class Composite_literal_expression
: public Parser_expression
3160 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
3161 Expression_list
* vals
, bool all_are_names
,
3163 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
3164 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
),
3165 all_are_names_(all_are_names
), key_path_(std::vector
<bool>(depth
))
3169 // Mark the DEPTH entry of KEY_PATH as containing a key.
3171 update_key_path(size_t depth
)
3173 go_assert(depth
< this->key_path_
.size());
3174 this->key_path_
[depth
] = true;
3179 do_traverse(Traverse
* traverse
);
3182 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
3187 Composite_literal_expression
*ret
=
3188 new Composite_literal_expression(this->type_
, this->depth_
,
3190 (this->vals_
== NULL
3192 : this->vals_
->copy()),
3193 this->all_are_names_
,
3195 ret
->key_path_
= this->key_path_
;
3200 do_dump_expression(Ast_dump_context
*) const;
3204 lower_struct(Gogo
*, Type
*);
3210 make_array(Type
*, const std::vector
<unsigned long>*, Expression_list
*);
3213 lower_map(Gogo
*, Named_object
*, Statement_inserter
*, Type
*);
3215 // The type of the composite literal.
3217 // The depth within a list of composite literals within a composite
3218 // literal, when the type is omitted.
3220 // The values to put in the composite literal.
3221 Expression_list
* vals_
;
3222 // If this is true, then VALS_ is a list of pairs: a key and a
3223 // value. In an array initializer, a missing key will be NULL.
3225 // If this is true, then HAS_KEYS_ is true, and every key is a
3226 // simple identifier.
3227 bool all_are_names_
;
3228 // A complement to DEPTH that indicates for each level starting from 0 to
3229 // DEPTH-1 whether or not this composite literal is nested inside of key or
3230 // a value. This is used to decide which type to use when given a map literal
3231 // with omitted key types.
3232 std::vector
<bool> key_path_
;
3235 // Construct a struct.
3237 class Struct_construction_expression
: public Expression
3240 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
3242 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
3243 type_(type
), vals_(vals
), traverse_order_(NULL
)
3246 // Set the traversal order, used to ensure that we implement the
3247 // order of evaluation rules. Takes ownership of the argument.
3249 set_traverse_order(std::vector
<int>* traverse_order
)
3250 { this->traverse_order_
= traverse_order
; }
3252 // Return whether this is a constant initializer.
3254 is_constant_struct() const;
3258 { return this->vals_
; }
3262 do_traverse(Traverse
* traverse
);
3265 do_is_immutable() const;
3269 { return this->type_
; }
3272 do_determine_type(const Type_context
*);
3275 do_check_types(Gogo
*);
3280 Struct_construction_expression
* ret
=
3281 new Struct_construction_expression(this->type_
,
3282 (this->vals_
== NULL
3284 : this->vals_
->copy()),
3286 if (this->traverse_order_
!= NULL
)
3287 ret
->set_traverse_order(this->traverse_order_
);
3292 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3295 do_get_backend(Translate_context
*);
3298 do_export(Export
*) const;
3301 do_dump_expression(Ast_dump_context
*) const;
3304 // The type of the struct to construct.
3306 // The list of values, in order of the fields in the struct. A NULL
3307 // entry means that the field should be zero-initialized.
3308 Expression_list
* vals_
;
3309 // If not NULL, the order in which to traverse vals_. This is used
3310 // so that we implement the order of evaluation rules correctly.
3311 std::vector
<int>* traverse_order_
;
3314 // Construct an array. This class is not used directly; instead we
3315 // use the child classes, Fixed_array_construction_expression and
3316 // Slice_construction_expression.
3318 class Array_construction_expression
: public Expression
3321 Array_construction_expression(Expression_classification classification
,
3323 const std::vector
<unsigned long>* indexes
,
3324 Expression_list
* vals
, Location location
)
3325 : Expression(classification
, location
),
3326 type_(type
), indexes_(indexes
), vals_(vals
)
3327 { go_assert(indexes
== NULL
|| indexes
->size() == vals
->size()); }
3330 // Return whether this is a constant initializer.
3332 is_constant_array() const;
3334 // Return the number of elements.
3336 element_count() const
3337 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
3339 // The list of values.
3342 { return this->vals_
; }
3346 do_traverse(Traverse
* traverse
);
3349 do_is_immutable() const;
3353 { return this->type_
; }
3356 do_determine_type(const Type_context
*);
3359 do_check_types(Gogo
*);
3362 do_export(Export
*) const;
3365 const std::vector
<unsigned long>*
3367 { return this->indexes_
; }
3370 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3372 // Get the backend constructor for the array values.
3374 get_constructor(Translate_context
* context
, Btype
* btype
);
3377 do_dump_expression(Ast_dump_context
*) const;
3380 dump_slice_storage_expression(Ast_dump_context
*) const { }
3383 // The type of the array to construct.
3385 // The list of indexes into the array, one for each value. This may
3386 // be NULL, in which case the indexes start at zero and increment.
3387 const std::vector
<unsigned long>* indexes_
;
3388 // The list of values. This may be NULL if there are no values.
3389 Expression_list
* vals_
;
3392 // Construct a fixed array.
3394 class Fixed_array_construction_expression
:
3395 public Array_construction_expression
3398 Fixed_array_construction_expression(Type
* type
,
3399 const std::vector
<unsigned long>* indexes
,
3400 Expression_list
* vals
, Location location
);
3406 return new Fixed_array_construction_expression(this->type(),
3408 (this->vals() == NULL
3410 : this->vals()->copy()),
3415 do_get_backend(Translate_context
*);
3418 // Construct a slice.
3420 class Slice_construction_expression
: public Array_construction_expression
3423 Slice_construction_expression(Type
* type
,
3424 const std::vector
<unsigned long>* indexes
,
3425 Expression_list
* vals
, Location location
);
3428 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3430 // Record that the storage for this slice (e.g. vals) cannot escape,
3431 // hence it can be stack-allocated.
3433 set_storage_does_not_escape()
3435 this->storage_escapes_
= false;
3439 // Note that taking the address of a slice literal is invalid.
3442 do_traverse(Traverse
* traverse
);
3447 return new Slice_construction_expression(this->type(), this->indexes(),
3448 (this->vals() == NULL
3450 : this->vals()->copy()),
3455 do_get_backend(Translate_context
*);
3458 dump_slice_storage_expression(Ast_dump_context
* ast_dump_context
) const;
3460 // Create an array value for the constructed slice. Invoked during
3461 // flattening if slice storage does not escape, otherwise invoked
3462 // later on during do_get_backend().
3467 // The type of the values in this slice.
3469 // Array value expression, optionally filled in during flattening.
3470 Expression
* array_val_
;
3471 // Slice storage expression, optionally filled in during flattening.
3472 Expression
* slice_storage_
;
3473 // Normally true. Can be set to false if we know that the resulting
3474 // storage for the slice cannot escape.
3475 bool storage_escapes_
;
3480 class Map_construction_expression
: public Expression
3483 Map_construction_expression(Type
* type
, Expression_list
* vals
,
3485 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
3486 type_(type
), vals_(vals
), element_type_(NULL
), constructor_temp_(NULL
)
3487 { go_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
3491 { return this->vals_
; }
3495 do_traverse(Traverse
* traverse
);
3498 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3502 { return this->type_
; }
3505 do_determine_type(const Type_context
*);
3508 do_check_types(Gogo
*);
3513 return new Map_construction_expression(this->type_
,
3514 (this->vals_
== NULL
3516 : this->vals_
->copy()),
3521 do_get_backend(Translate_context
*);
3524 do_export(Export
*) const;
3527 do_dump_expression(Ast_dump_context
*) const;
3530 // The type of the map to construct.
3532 // The list of values.
3533 Expression_list
* vals_
;
3534 // The type of the key-value pair struct for each map element.
3535 Struct_type
* element_type_
;
3536 // A temporary reference to the variable storing the constructor initializer.
3537 Temporary_statement
* constructor_temp_
;
3540 // A type guard expression.
3542 class Type_guard_expression
: public Expression
3545 Type_guard_expression(Expression
* expr
, Type
* type
, Location location
)
3546 : Expression(EXPRESSION_TYPE_GUARD
, location
),
3547 expr_(expr
), type_(type
)
3550 // Return the expression to convert.
3553 { return this->expr_
; }
3555 // Return the type to which to convert.
3558 { return this->type_
; }
3562 do_traverse(Traverse
* traverse
);
3565 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3569 { return this->type_
; }
3572 do_determine_type(const Type_context
*)
3573 { this->expr_
->determine_type_no_context(); }
3576 do_check_types(Gogo
*);
3581 return new Type_guard_expression(this->expr_
->copy(), this->type_
,
3586 do_get_backend(Translate_context
*);
3589 do_dump_expression(Ast_dump_context
*) const;
3592 // The expression to convert.
3594 // The type to which to convert.
3598 // Class Heap_expression.
3600 // When you take the address of an escaping expression, it is allocated
3601 // on the heap. This class implements that.
3603 class Heap_expression
: public Expression
3606 Heap_expression(Expression
* expr
, Location location
)
3607 : Expression(EXPRESSION_HEAP
, location
),
3613 { return this->expr_
; }
3617 do_traverse(Traverse
* traverse
)
3618 { return Expression::traverse(&this->expr_
, traverse
); }
3623 do_determine_type(const Type_context
*)
3624 { this->expr_
->determine_type_no_context(); }
3629 return Expression::make_heap_expression(this->expr_
->copy(),
3634 do_get_backend(Translate_context
*);
3636 // We only export global objects, and the parser does not generate
3637 // this in global scope.
3639 do_export(Export
*) const
3640 { go_unreachable(); }
3643 do_dump_expression(Ast_dump_context
*) const;
3646 // The expression which is being put on the heap.
3650 // A receive expression.
3652 class Receive_expression
: public Expression
3655 Receive_expression(Expression
* channel
, Location location
)
3656 : Expression(EXPRESSION_RECEIVE
, location
),
3657 channel_(channel
), temp_receiver_(NULL
)
3660 // Return the channel.
3663 { return this->channel_
; }
3667 do_traverse(Traverse
* traverse
)
3668 { return Expression::traverse(&this->channel_
, traverse
); }
3671 do_discarding_value()
3678 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3681 do_determine_type(const Type_context
*)
3682 { this->channel_
->determine_type_no_context(); }
3685 do_check_types(Gogo
*);
3690 return Expression::make_receive(this->channel_
->copy(), this->location());
3694 do_must_eval_in_order() const
3698 do_get_backend(Translate_context
*);
3701 do_dump_expression(Ast_dump_context
*) const;
3704 // The channel from which we are receiving.
3705 Expression
* channel_
;
3706 // A temporary reference to the variable storing the received data.
3707 Temporary_statement
* temp_receiver_
;
3710 // Conditional expressions.
3712 class Conditional_expression
: public Expression
3715 Conditional_expression(Expression
* cond
, Expression
* then_expr
,
3716 Expression
* else_expr
, Location location
)
3717 : Expression(EXPRESSION_CONDITIONAL
, location
),
3718 cond_(cond
), then_(then_expr
), else_(else_expr
)
3723 { return this->cond_
; }
3727 do_traverse(Traverse
*);
3733 do_determine_type(const Type_context
*);
3738 return new Conditional_expression(this->cond_
->copy(), this->then_
->copy(),
3739 this->else_
->copy(), this->location());
3743 do_get_backend(Translate_context
* context
);
3746 do_dump_expression(Ast_dump_context
*) const;
3749 // The condition to be checked.
3751 // The expression to execute if the condition is true.
3753 // The expression to execute if the condition is false.
3757 // Compound expressions.
3759 class Compound_expression
: public Expression
3762 Compound_expression(Expression
* init
, Expression
* expr
, Location location
)
3763 : Expression(EXPRESSION_COMPOUND
, location
), init_(init
), expr_(expr
)
3768 { return this->init_
; }
3772 do_traverse(Traverse
*);
3778 do_determine_type(const Type_context
*);
3783 return new Compound_expression(this->init_
->copy(), this->expr_
->copy(),
3788 do_get_backend(Translate_context
* context
);
3791 do_dump_expression(Ast_dump_context
*) const;
3794 // The expression that is evaluated first and discarded.
3796 // The expression that is evaluated and returned.
3800 // A numeric constant. This is used both for untyped constants and
3801 // for constants that have a type.
3803 class Numeric_constant
3807 : classification_(NC_INVALID
), type_(NULL
)
3810 ~Numeric_constant();
3812 Numeric_constant(const Numeric_constant
&);
3814 Numeric_constant
& operator=(const Numeric_constant
&);
3816 // Set to an unsigned long value.
3818 set_unsigned_long(Type
*, unsigned long);
3820 // Set to an integer value.
3822 set_int(Type
*, const mpz_t
);
3824 // Set to a rune value.
3826 set_rune(Type
*, const mpz_t
);
3828 // Set to a floating point value.
3830 set_float(Type
*, const mpfr_t
);
3832 // Set to a complex value.
3834 set_complex(Type
*, const mpc_t
);
3836 // Mark numeric constant as invalid.
3839 { this->classification_
= NC_INVALID
; }
3844 { return this->classification_
== Numeric_constant::NC_INT
; }
3848 { return this->classification_
== Numeric_constant::NC_RUNE
; }
3852 { return this->classification_
== Numeric_constant::NC_FLOAT
; }
3856 { return this->classification_
== Numeric_constant::NC_COMPLEX
; }
3860 { return this->classification_
== Numeric_constant::NC_INVALID
; }
3862 // Value retrievers. These will initialize the values as well as
3863 // set them. GET_INT is only valid if IS_INT returns true, and
3864 // likewise respectively.
3866 get_int(mpz_t
*) const;
3869 get_rune(mpz_t
*) const;
3872 get_float(mpfr_t
*) const;
3875 get_complex(mpc_t
*) const;
3877 // Codes returned by to_unsigned_long.
3878 enum To_unsigned_long
3880 // Value is integer and fits in unsigned long.
3882 // Value is not integer.
3884 // Value is integer but is negative.
3886 // Value is non-negative integer but does not fit in unsigned
3891 // If the value can be expressed as an integer that fits in an
3892 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
3893 // one of the other To_unsigned_long codes.
3895 to_unsigned_long(unsigned long* val
) const;
3897 // If the value can be expressed as an int, return true and
3898 // initialize and set VAL. This will return false for a value with
3899 // an explicit float or complex type, even if the value is integral.
3901 to_int(mpz_t
* val
) const;
3903 // If the value can be expressed as a float, return true and
3904 // initialize and set VAL.
3906 to_float(mpfr_t
* val
) const;
3908 // If the value can be expressed as a complex, return true and
3909 // initialize and set VR and VI.
3911 to_complex(mpc_t
* val
) const;
3917 // If the constant can be expressed in TYPE, then set the type of
3918 // the constant to TYPE and return true. Otherwise return false,
3919 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
3920 // the location to use for the error.
3922 set_type(Type
* type
, bool issue_error
, Location location
);
3924 // Return an Expression for this value.
3926 expression(Location
) const;
3933 mpz_to_unsigned_long(const mpz_t ival
, unsigned long *val
) const;
3936 mpfr_to_unsigned_long(const mpfr_t fval
, unsigned long *val
) const;
3939 check_int_type(Integer_type
*, bool, Location
);
3942 check_float_type(Float_type
*, bool, Location
);
3945 check_complex_type(Complex_type
*, bool, Location
);
3947 // The kinds of constants.
3957 // The kind of constant.
3958 Classification classification_
;
3962 // If NC_INT or NC_RUNE.
3969 // The type if there is one. This will be NULL for an untyped
3974 #endif // !defined(GO_EXPRESSIONS_H)