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 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
;
69 class Export_function_body
;
70 class Import_expression
;
71 class Temporary_statement
;
73 class Ast_dump_context
;
76 // The precision to use for complex values represented as an mpc_t.
77 const int mpc_precision
= 256;
79 // The base class for all expressions.
84 // The types of expressions.
85 enum Expression_classification
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
,
98 EXPRESSION_FUNC_REFERENCE
,
99 EXPRESSION_FUNC_DESCRIPTOR
,
100 EXPRESSION_FUNC_CODE_REFERENCE
,
101 EXPRESSION_UNKNOWN_REFERENCE
,
104 EXPRESSION_STRING_INFO
,
105 EXPRESSION_STRING_VALUE
,
112 EXPRESSION_CALL_RESULT
,
113 EXPRESSION_BOUND_METHOD
,
115 EXPRESSION_ARRAY_INDEX
,
116 EXPRESSION_STRING_INDEX
,
117 EXPRESSION_MAP_INDEX
,
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
,
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
,
148 Expression(Expression_classification
, Location
);
150 virtual ~Expression();
152 // Make an error expression. This is used when a parse error occurs
153 // to prevent cascading errors.
155 make_error(Location
);
157 // Make an expression which is really a type. This is used during
160 make_type(Type
*, Location
);
162 // Make a unary expression.
164 make_unary(Operator
, Expression
*, Location
);
166 // Make a binary expression.
168 make_binary(Operator
, Expression
*, Expression
*, Location
);
170 // Make a string concatenation expression.
172 make_string_concat(Expression_list
*);
174 // Make a reference to a constant in an expression.
176 make_const_reference(Named_object
*, Location
);
178 // Make a reference to a variable in an expression.
180 make_var_reference(Named_object
*, Location
);
182 // Make a reference to a variable within an enclosing function.
184 make_enclosing_var_reference(Expression
*, Named_object
*, Location
);
186 // Make a reference to a temporary variable. Temporary variables
187 // are always created by a single statement, which is what we use to
189 static Temporary_reference_expression
*
190 make_temporary_reference(Temporary_statement
*, Location
);
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
196 static Set_and_use_temporary_expression
*
197 make_set_and_use_temporary(Temporary_statement
*, Expression
*, Location
);
199 // Make a sink expression--a reference to the blank identifier _.
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.
207 make_func_reference(Named_object
*, Expression
* closure
, Location
);
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
);
216 // Make a reference to the code of a function. This is used to set
217 // descriptor and closure fields.
219 make_func_code_reference(Named_object
*, Location
);
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
);
226 // Make a constant bool expression.
228 make_boolean(bool val
, Location
);
230 // Make a constant string expression.
232 make_string(const std::string
&, Location
);
234 // Make a constant string expression with a specific string subtype.
236 make_string_typed(const std::string
&, Type
*, Location
);
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.
243 // The underlying data in the string.
245 // The length of the string.
250 make_string_info(Expression
* string
, String_info
, Location
);
252 // Make an expression for a string value.
254 make_string_value(Expression
* valptr
, Expression
* len
, Location
);
256 // Make a character constant expression. TYPE should be NULL for an
259 make_character(const mpz_t
*, Type
*, Location
);
261 // Make a constant integer expression from a multi-precision
262 // integer. TYPE should be NULL for an abstract type.
264 make_integer_z(const mpz_t
*, Type
*, Location
);
266 // Make a constant integer expression from an unsigned long. TYPE
267 // should be NULL for an abstract type.
269 make_integer_ul(unsigned long, Type
*, Location
);
271 // Make a constant integer expression from a signed long. TYPE
272 // should be NULL for an abstract type.
274 make_integer_sl(long, Type
*, Location
);
276 // Make a constant integer expression from an int64_t. TYPE should
277 // be NULL for an abstract type.
279 make_integer_int64(int64_t, Type
*, Location
);
281 // Make a constant float expression. TYPE should be NULL for an
284 make_float(const mpfr_t
*, Type
*, Location
);
286 // Make a constant complex expression. TYPE should be NULL for an
289 make_complex(const mpc_t
*, Type
*, Location
);
291 // Make a nil expression.
295 // Make an iota expression. This is used for the predeclared
300 // Make a call expression.
301 static Call_expression
*
302 make_call(Expression
* func
, Expression_list
* args
, bool is_varargs
,
305 // Make a reference to a specific result of a call expression which
308 make_call_result(Call_expression
*, unsigned int index
);
310 // Make an expression which is a method bound to its first
311 // parameter. METHOD is the method being called, FUNCTION is the
313 static Bound_method_expression
*
314 make_bound_method(Expression
* object
, const Method
* method
,
315 Named_object
* function
, Location
);
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,
324 make_index(Expression
* left
, Expression
* start
, Expression
* end
,
325 Expression
* cap
, Location
);
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
331 make_array_index(Expression
* array
, Expression
* start
, Expression
* end
,
332 Expression
* cap
, Location
);
334 // Make a string index expression. END may be NULL. This is never
337 make_string_index(Expression
* string
, Expression
* start
, Expression
* end
,
340 // Make a map index expression. This is an lvalue.
341 static Map_index_expression
*
342 make_map_index(Expression
* map
, Expression
* val
, Location
);
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
348 make_selector(Expression
* left
, const std::string
& name
, Location
);
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
);
354 // Make a reference to a field of an interface, with an associated
357 make_interface_field_reference(Expression
*, const std::string
&,
360 // Make an allocation expression.
362 make_allocation(Type
*, Location
);
364 // Make a type guard expression.
366 make_type_guard(Expression
*, Type
*, Location
);
368 // Make a type cast expression.
370 make_cast(Type
*, Expression
*, Location
);
372 // Make an unsafe type cast expression. This is only used when
373 // passing parameter to builtin functions that are part of the Go
376 make_unsafe_cast(Type
*, Expression
*, Location
);
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
384 make_composite_literal(Type
*, int depth
, bool has_keys
, Expression_list
*,
385 bool all_are_names
, Location
);
387 // Make a struct composite literal.
389 make_struct_composite_literal(Type
*, Expression_list
*, Location
);
391 // Make an array composite literal.
393 make_array_composite_literal(Type
*, Expression_list
*, Location
);
395 // Make a slice composite literal.
396 static Slice_construction_expression
*
397 make_slice_composite_literal(Type
*, Expression_list
*, Location
);
399 // Take an expression and allocate it on the heap.
401 make_heap_expression(Expression
*, Location
);
403 // Make a receive expression. VAL is NULL for a unary receive.
404 static Receive_expression
*
405 make_receive(Expression
* channel
, Location
);
407 // Make an expression which evaluates to the address of the type
408 // descriptor for TYPE.
410 make_type_descriptor(Type
* type
, Location
);
412 // Make an expression which evaluates to the address of the gc
415 make_gc_symbol(Type
* type
);
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.
422 make_ptrmask_symbol(Type
* type
);
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.
429 // The size of a value of the type.
431 // The required alignment of a value of the type.
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
447 make_type_info(Type
* type
, Type_info
);
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.
454 // The underlying data of the slice.
455 SLICE_INFO_VALUE_POINTER
,
456 // The length of the slice.
458 // The capacity of the slice.
463 make_slice_info(Expression
* slice
, Slice_info
, Location
);
465 // Make an expression for a slice value.
467 make_slice_value(Type
*, Expression
* valptr
, Expression
* len
, Expression
* cap
,
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.
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
484 make_interface_info(Expression
* iface
, Interface_info
, Location
);
486 // Make an expression for an interface value.
488 make_interface_value(Type
*, Expression
*, Expression
*, Location
);
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.
494 make_interface_mtable_ref(Interface_type
* itype
, Type
* type
,
495 bool is_pointer
, Location
);
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.
501 make_struct_field_offset(Struct_type
*, const Struct_field
*);
503 // Make an expression which evaluates to the address of an unnamed
506 make_label_addr(Label
*, Location
);
508 // Make a conditional expression.
510 make_conditional(Expression
*, Expression
*, Expression
*, Location
);
512 // Make a compound expression.
514 make_compound(Expression
*, Expression
*, Location
);
516 // Make a backend expression.
518 make_backend(Bexpression
*, Type
*, Location
);
520 enum Nil_check_classification
522 // Use the default policy for deciding if this deref needs a check.
524 // An explicit check is required for this dereference operation.
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
533 // Make a dereference expression.
535 make_dereference(Expression
*, Nil_check_classification
, Location
);
537 // Return the expression classification.
538 Expression_classification
539 classification() const
540 { return this->classification_
; }
542 // Return the location of the expression.
545 { return this->location_
; }
547 // Return whether this is a constant expression.
550 { return this->do_is_constant(); }
552 // Return whether this is the zero value of its type.
554 is_zero_value() const
555 { return this->do_is_zero_value(); }
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.
569 is_static_initializer() const
570 { return this->do_is_static_initializer(); }
572 // If this is not a numeric constant, return false. If it is one,
573 // return true, and set VAL to hold the value.
575 numeric_constant_value(Numeric_constant
* val
) const
576 { return this->do_numeric_constant_value(val
); }
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.
581 string_constant_value(std::string
* val
) const
582 { return this->do_string_constant_value(val
); }
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
590 { return this->do_discarding_value(); }
592 // Return whether this is an error expression.
594 is_error_expression() const
595 { return this->classification_
== EXPRESSION_ERROR
; }
597 // Return whether this expression really represents a type.
599 is_type_expression() const
600 { return this->classification_
== EXPRESSION_TYPE
; }
602 // If this is a variable reference, return the Var_expression
603 // structure. Otherwise, return NULL. This is a controlled dynamic
607 { return this->convert
<Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
609 const Var_expression
*
610 var_expression() const
611 { return this->convert
<const Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
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
>(); }
621 const Enclosed_var_expression
*
622 enclosed_var_expression() const
623 { return this->convert
<const Enclosed_var_expression
,
624 EXPRESSION_ENCLOSED_VAR_REFERENCE
>(); }
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()
632 return this->convert
<Temporary_reference_expression
,
633 EXPRESSION_TEMPORARY_REFERENCE
>();
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()
641 return this->convert
<Set_and_use_temporary_expression
,
642 EXPRESSION_SET_AND_USE_TEMPORARY
>();
645 // Return whether this is a sink expression.
647 is_sink_expression() const
648 { return this->classification_
== EXPRESSION_SINK
; }
650 // If this is a string expression, return the String_expression
651 // structure. Otherwise, return NULL.
654 { return this->convert
<String_expression
, EXPRESSION_STRING
>(); }
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
>(); }
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()
667 return this->convert
<Unsafe_type_conversion_expression
,
668 EXPRESSION_UNSAFE_CONVERSION
>();
671 // Return whether this is the expression nil.
673 is_nil_expression() const
674 { return this->classification_
== EXPRESSION_NIL
; }
676 // If this is an indirection through a pointer, return the
677 // expression being pointed through. Otherwise return this.
681 // If this is a unary expression, return the Unary_expression
682 // structure. Otherwise return NULL.
685 { return this->convert
<Unary_expression
, EXPRESSION_UNARY
>(); }
687 // If this is a binary expression, return the Binary_expression
688 // structure. Otherwise return NULL.
691 { return this->convert
<Binary_expression
, EXPRESSION_BINARY
>(); }
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()
698 return this->convert
<String_concat_expression
, EXPRESSION_STRING_CONCAT
>();
701 // If this is a call expression, return the Call_expression
702 // structure. Otherwise, return NULL. This is a controlled dynamic
706 { return this->convert
<Call_expression
, EXPRESSION_CALL
>(); }
708 // If this is a call_result expression, return the Call_result_expression
709 // structure. Otherwise, return NULL. This is a controlled dynamic
711 Call_result_expression
*
712 call_result_expression()
713 { return this->convert
<Call_result_expression
, EXPRESSION_CALL_RESULT
>(); }
715 // If this is an expression which refers to a function, return the
716 // Func_expression structure. Otherwise, return NULL.
719 { return this->convert
<Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
721 const Func_expression
*
722 func_expression() const
723 { return this->convert
<const Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
725 // If this is an expression which refers to an unknown name, return
726 // the Unknown_expression structure. Otherwise, return NULL.
729 { return this->convert
<Unknown_expression
, EXPRESSION_UNKNOWN_REFERENCE
>(); }
731 const Unknown_expression
*
732 unknown_expression() const
734 return this->convert
<const Unknown_expression
,
735 EXPRESSION_UNKNOWN_REFERENCE
>();
738 // If this is an index expression, return the Index_expression
739 // structure. Otherwise, return NULL.
742 { return this->convert
<Index_expression
, EXPRESSION_INDEX
>(); }
744 // If this is an expression which refers to indexing in a array,
745 // return the Array_index_expression structure. Otherwise, return
747 Array_index_expression
*
748 array_index_expression()
749 { return this->convert
<Array_index_expression
, EXPRESSION_ARRAY_INDEX
>(); }
751 // If this is an expression which refers to indexing in a string,
752 // return the String_index_expression structure. Otherwise, return
754 String_index_expression
*
755 string_index_expression()
756 { return this->convert
<String_index_expression
, EXPRESSION_STRING_INDEX
>(); }
758 // If this is an expression which refers to indexing in a map,
759 // return the Map_index_expression structure. Otherwise, return
761 Map_index_expression
*
762 map_index_expression()
763 { return this->convert
<Map_index_expression
, EXPRESSION_MAP_INDEX
>(); }
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
>(); }
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()
776 return this->convert
<Field_reference_expression
,
777 EXPRESSION_FIELD_REFERENCE
>();
780 // If this is a reference to a field in an interface, return the
781 // Interface_field_reference_expression structure. Otherwise,
783 Interface_field_reference_expression
*
784 interface_field_reference_expression()
786 return this->convert
<Interface_field_reference_expression
,
787 EXPRESSION_INTERFACE_FIELD_REFERENCE
>();
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
>(); }
796 // If this is a general composite literal, return the
797 // Composite_literal_expression structure. Otherwise, return NULL.
798 Composite_literal_expression
*
801 return this->convert
<Composite_literal_expression
,
802 EXPRESSION_COMPOSITE_LITERAL
>();
805 // If this is a struct composite literal, return the
806 // Struct_construction_expression structure. Otherwise, return NULL.
807 Struct_construction_expression
*
810 return this->convert
<Struct_construction_expression
,
811 EXPRESSION_STRUCT_CONSTRUCTION
>();
814 // If this is a array composite literal, return the
815 // Array_construction_expression structure. Otherwise, return NULL.
816 Fixed_array_construction_expression
*
819 return this->convert
<Fixed_array_construction_expression
,
820 EXPRESSION_FIXED_ARRAY_CONSTRUCTION
>();
823 // If this is a slice composite literal, return the
824 // Slice_construction_expression structure. Otherwise, return NULL.
825 Slice_construction_expression
*
828 return this->convert
<Slice_construction_expression
,
829 EXPRESSION_SLICE_CONSTRUCTION
>();
832 // If this is a map composite literal, return the
833 // Map_construction_expression structure. Otherwise, return NULL.
834 Map_construction_expression
*
837 return this->convert
<Map_construction_expression
,
838 EXPRESSION_MAP_CONSTRUCTION
>();
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
>(); }
847 // If this is a heap expression, returhn the Heap_expression structure.
848 // Otherwise, return NULL.
851 { return this->convert
<Heap_expression
, EXPRESSION_HEAP
>(); }
853 // If this is a receive expression, return the Receive_expression
854 // structure. Otherwise, return NULL.
857 { return this->convert
<Receive_expression
, EXPRESSION_RECEIVE
>(); }
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
>(); }
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
>(); }
871 // If this is a compound expression, return the Compound_expression structure.
872 // Otherwise, return NULL.
874 compound_expression()
875 { return this->convert
<Compound_expression
, EXPRESSION_COMPOUND
>(); }
877 // Return true if this is a composite literal.
879 is_composite_literal() const;
881 // Return true if this is a composite literal which is not constant.
883 is_nonconstant_composite_literal() const;
885 // Return true if this is a variable or temporary variable.
889 // Return true if this is a reference to a local variable.
891 is_local_variable() const;
893 // Return true if two expressions refer to the same variable or
896 is_same_variable(Expression
*, Expression
*);
898 // Make the builtin function descriptor type, so that it can be
901 make_func_descriptor_type();
903 // Traverse an expression.
905 traverse(Expression
**, Traverse
*);
907 // Traverse subexpressions of this expression.
909 traverse_subexpressions(Traverse
*);
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.
921 lower(Gogo
* gogo
, Named_object
* function
, Statement_inserter
* inserter
,
923 { return this->do_lower(gogo
, function
, inserter
, iota_value
); }
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.
934 flatten(Gogo
* gogo
, Named_object
* function
, Statement_inserter
* inserter
)
935 { return this->do_flatten(gogo
, function
, inserter
); }
937 // Determine the real type of an expression with abstract integer,
938 // floating point, or complex type. TYPE_CONTEXT describes the
941 determine_type(const Type_context
*);
943 // Check types in an expression.
945 check_types(Gogo
* gogo
)
946 { this->do_check_types(gogo
); }
948 // Determine the type when there is no context.
950 determine_type_no_context();
952 // Return the current type of the expression. This may be changed
953 // by determine_type.
956 { return this->do_type(); }
958 // Return a copy of an expression.
961 { return this->do_copy(); }
963 // Return the cost of this statement for inlining purposes.
965 inlining_cost() const
966 { return this->do_inlining_cost(); }
968 // Return whether the expression is addressable--something which may
969 // be used as the operand of the unary & operator.
971 is_addressable() const
972 { return this->do_is_addressable(); }
974 // Note that we are taking the address of this expression. ESCAPES
975 // is true if this address escapes the current function.
977 address_taken(bool escapes
)
978 { this->do_address_taken(escapes
); }
980 // Note that a nil check must be issued for this expression.
983 { this->do_issue_nil_check(); }
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.
989 must_eval_in_order() const
990 { return this->do_must_eval_in_order(); }
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.
998 must_eval_subexpressions_in_order(int* skip
) const
1001 return this->do_must_eval_subexpressions_in_order(skip
);
1004 // Return the backend representation for this expression.
1006 get_backend(Translate_context
*);
1008 // Return an expression handling any conversions which must be done during
1011 convert_for_assignment(Gogo
*, Type
* lhs_type
, Expression
* rhs
,
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
1018 convert_interface_to_interface(Type
* lhs_type
,
1019 Expression
* rhs
, bool for_type_guard
,
1022 // Return a backend expression implementing the comparison LEFT OP RIGHT.
1023 // TYPE is the type of both sides.
1025 comparison(Translate_context
*, Type
* result_type
, Operator op
,
1026 Expression
* left
, Expression
* right
, Location
);
1028 // Return the backend expression for the numeric constant VAL.
1030 backend_numeric_constant_expression(Translate_context
*,
1031 Numeric_constant
* val
);
1033 // Export the expression.
1035 export_expression(Export_function_body
* efb
) const
1036 { this->do_export(efb
); }
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.
1042 import_expression(Import_expression
*, Location
);
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.
1047 check_bounds(Expression
* val
, Location
);
1049 // Return an expression for constructing a direct interface type from a
1052 pack_direct_iface(Type
*, Expression
*, Location
);
1054 // Dump an expression to a dump constext.
1056 dump_expression(Ast_dump_context
*) const;
1059 // May be implemented by child class: traverse the expressions.
1061 do_traverse(Traverse
*);
1063 // Return a lowered expression.
1065 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
1068 // Return a flattened expression.
1070 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*)
1074 // Return whether this is a constant expression.
1076 do_is_constant() const
1079 // Return whether this is the zero value of its type.
1081 do_is_zero_value() const
1084 // Return whether this expression can be used as a constant
1087 do_is_static_initializer() const
1090 // Return whether this is a constant expression of numeric type, and
1091 // set the Numeric_constant to the value.
1093 do_numeric_constant_value(Numeric_constant
*) const
1096 // Return whether this is a constant expression of string type, and
1097 // set VAL to the value.
1099 do_string_constant_value(std::string
*) const
1102 // Called by the parser if the value is being discarded.
1104 do_discarding_value();
1106 // Child class holds type.
1110 // Child class implements determining type information.
1112 do_determine_type(const Type_context
*) = 0;
1114 // Child class implements type checking if needed.
1116 do_check_types(Gogo
*)
1119 // Child class implements copying.
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.
1127 do_inlining_cost() const
1128 { return 0x100000; }
1130 // Child class implements whether the expression is addressable.
1132 do_is_addressable() const
1135 // Child class implements taking the address of an expression.
1137 do_address_taken(bool)
1140 // Child class implements issuing a nil check if the address is taken.
1142 do_issue_nil_check()
1145 // Child class implements whether this expression must be evaluated
1148 do_must_eval_in_order() const
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.
1155 do_must_eval_subexpressions_in_order(int* /* skip */) const
1158 // Child class implements conversion to backend representation.
1159 virtual Bexpression
*
1160 do_get_backend(Translate_context
*) = 0;
1162 // Child class implements export.
1164 do_export(Export_function_body
*) const;
1166 // For children to call to give an error for an unused value.
1168 unused_value_error();
1170 // For children to call when they detect that they are in error.
1174 // For children to call to report an error conveniently.
1176 report_error(const char*);
1178 // Child class implements dumping to a dump context.
1180 do_dump_expression(Ast_dump_context
*) const = 0;
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
1190 SLICE_STORAGE_MAY_ESCAPE
,
1191 SLICE_STORAGE_DOES_NOT_ESCAPE
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
>
1202 return (this->classification_
== expr_classification
1203 ? static_cast<Expression_class
*>(this)
1207 template<typename Expression_class
,
1208 Expression_classification expr_classification
>
1209 const Expression_class
*
1212 return (this->classification_
== expr_classification
1213 ? static_cast<const Expression_class
*>(this)
1218 convert_type_to_interface(Type
*, Expression
*, Location
);
1221 unpack_direct_iface(Expression
*, Location
);
1224 get_interface_type_descriptor(Expression
*);
1227 convert_interface_to_type(Type
*, Expression
*, Location
);
1229 // The expression classification.
1230 Expression_classification classification_
;
1231 // The location in the input file.
1235 // A list of Expressions.
1237 class Expression_list
1244 // Return whether the list is empty.
1247 { return this->entries_
.empty(); }
1249 // Return the number of entries in the list.
1252 { return this->entries_
.size(); }
1254 // Add an entry to the end of the list.
1256 push_back(Expression
* expr
)
1257 { this->entries_
.push_back(expr
); }
1260 append(Expression_list
* add
)
1261 { this->entries_
.insert(this->entries_
.end(), add
->begin(), add
->end()); }
1263 // Reserve space in the list.
1265 reserve(size_t size
)
1266 { this->entries_
.reserve(size
); }
1268 // Traverse the expressions in the list.
1270 traverse(Traverse
*);
1276 // Return true if the list contains an error expression.
1278 contains_error() const;
1280 // Retrieve an element by index.
1283 { return this->entries_
.at(i
); }
1285 // Return the first and last elements.
1288 { return this->entries_
.front(); }
1292 { return this->entries_
.front(); }
1296 { return this->entries_
.back(); }
1300 { return this->entries_
.back(); }
1304 typedef std::vector
<Expression
*>::iterator iterator
;
1305 typedef std::vector
<Expression
*>::const_iterator const_iterator
;
1309 { return this->entries_
.begin(); }
1313 { return this->entries_
.begin(); }
1317 { return this->entries_
.end(); }
1321 { return this->entries_
.end(); }
1326 { this->entries_
.erase(p
); }
1329 std::vector
<Expression
*> entries_
;
1332 // An abstract base class for an expression which is only used by the
1333 // parser, and is lowered in the lowering pass.
1335 class Parser_expression
: public Expression
1338 Parser_expression(Expression_classification classification
,
1340 : Expression(classification
, location
)
1345 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int) = 0;
1351 do_determine_type(const Type_context
*)
1352 { go_unreachable(); }
1355 do_check_types(Gogo
*)
1356 { go_unreachable(); }
1359 do_get_backend(Translate_context
*)
1360 { go_unreachable(); }
1363 // An expression which is simply a variable.
1365 class Var_expression
: public Expression
1368 Var_expression(Named_object
* variable
, Location location
)
1369 : Expression(EXPRESSION_VAR_REFERENCE
, location
),
1373 // Return the variable.
1375 named_object() const
1376 { return this->variable_
; }
1380 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1386 do_determine_type(const Type_context
*);
1393 do_inlining_cost() const;
1396 do_export(Export_function_body
*) const;
1399 do_is_addressable() const
1403 do_address_taken(bool);
1406 do_get_backend(Translate_context
*);
1409 do_dump_expression(Ast_dump_context
*) const;
1412 // The variable we are referencing.
1413 Named_object
* variable_
;
1416 // A reference to a variable within an enclosing function.
1418 class Enclosed_var_expression
: public Expression
1421 Enclosed_var_expression(Expression
* reference
, Named_object
* variable
,
1423 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE
, location
),
1424 reference_(reference
), variable_(variable
)
1427 // The reference to the enclosed variable. This will be an indirection of the
1428 // the field stored within closure variable.
1431 { return this->reference_
; }
1433 // The variable being enclosed and referenced.
1436 { return this->variable_
; }
1440 do_traverse(Traverse
*);
1443 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1446 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1450 { return this->reference_
->type(); }
1453 do_determine_type(const Type_context
* context
)
1454 { return this->reference_
->determine_type(context
); }
1461 do_is_addressable() const
1462 { return this->reference_
->is_addressable(); }
1465 do_address_taken(bool escapes
);
1468 do_get_backend(Translate_context
* context
)
1469 { return this->reference_
->get_backend(context
); }
1472 do_dump_expression(Ast_dump_context
*) const;
1475 // The reference to the enclosed variable.
1476 Expression
* reference_
;
1477 // The variable being enclosed.
1478 Named_object
* variable_
;
1481 // A reference to a temporary variable.
1483 class Temporary_reference_expression
: public Expression
1486 Temporary_reference_expression(Temporary_statement
* statement
,
1488 : Expression(EXPRESSION_TEMPORARY_REFERENCE
, location
),
1489 statement_(statement
), is_lvalue_(false)
1492 // The temporary that this expression refers to.
1493 Temporary_statement
*
1495 { return this->statement_
; }
1497 // Indicate that this reference appears on the left hand side of an
1498 // assignment statement.
1501 { this->is_lvalue_
= true; }
1508 do_determine_type(const Type_context
*)
1513 { return make_temporary_reference(this->statement_
, this->location()); }
1516 do_is_addressable() const
1520 do_address_taken(bool);
1523 do_get_backend(Translate_context
*);
1526 do_dump_expression(Ast_dump_context
*) const;
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.
1536 // Set and use a temporary variable.
1538 class Set_and_use_temporary_expression
: public Expression
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
)
1547 // Return the temporary.
1548 Temporary_statement
*
1550 { return this->statement_
; }
1552 // Return the expression.
1555 { return this->expr_
; }
1559 do_traverse(Traverse
* traverse
)
1560 { return Expression::traverse(&this->expr_
, traverse
); }
1566 do_determine_type(const Type_context
*);
1571 return make_set_and_use_temporary(this->statement_
, this->expr_
,
1576 do_is_addressable() const
1580 do_address_taken(bool);
1583 do_get_backend(Translate_context
*);
1586 do_dump_expression(Ast_dump_context
*) const;
1589 // The statement where the temporary variable is defined.
1590 Temporary_statement
* statement_
;
1591 // The expression to assign to the temporary.
1595 // A string expression.
1597 class String_expression
: public Expression
1600 String_expression(const std::string
& val
, Type
* type
, Location location
)
1601 : Expression(EXPRESSION_STRING
, location
),
1602 val_(val
), type_(type
)
1607 { return this->val_
; }
1610 do_import(Import_expression
*, Location
);
1614 do_is_constant() const
1618 do_is_zero_value() const
1619 { return this->val_
== ""; }
1622 do_is_static_initializer() const
1626 do_string_constant_value(std::string
* val
) const
1636 do_determine_type(const Type_context
*);
1643 do_get_backend(Translate_context
*);
1645 // Write string literal to a string dump.
1647 export_string(String_dump
* exp
, const String_expression
* str
);
1649 // Set the inlining cost a bit high since inlining may cause
1650 // duplicated string literals.
1652 do_inlining_cost() const
1656 do_export(Export_function_body
*) const;
1659 do_dump_expression(Ast_dump_context
*) const;
1662 // The string value. This is immutable.
1663 const std::string val_
;
1664 // The type as determined by context.
1668 // A type conversion expression.
1670 class Type_conversion_expression
: public Expression
1673 Type_conversion_expression(Type
* type
, Expression
* expr
,
1675 : Expression(EXPRESSION_CONVERSION
, location
),
1676 type_(type
), expr_(expr
), may_convert_function_types_(false),
1680 // Return the type to which we are converting.
1683 { return this->type_
; }
1685 // Return the expression which we are converting.
1688 { return this->expr_
; }
1690 // Permit converting from one function type to another. This is
1691 // used internally for method expressions.
1693 set_may_convert_function_types()
1695 this->may_convert_function_types_
= true;
1698 // Mark string([]byte) conversion to reuse the backing store
1702 { this->no_copy_
= b
; };
1704 // Import a type conversion expression.
1706 do_import(Import_expression
*, Location
);
1710 do_traverse(Traverse
* traverse
);
1713 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1716 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1719 do_is_constant() const;
1722 do_is_zero_value() const;
1725 do_is_static_initializer() const;
1728 do_numeric_constant_value(Numeric_constant
*) const;
1731 do_string_constant_value(std::string
*) const;
1735 { return this->type_
; }
1738 do_determine_type(const Type_context
*);
1741 do_check_types(Gogo
*);
1747 do_get_backend(Translate_context
* context
);
1750 do_inlining_cost() const;
1753 do_export(Export_function_body
*) const;
1756 do_dump_expression(Ast_dump_context
*) const;
1759 // The type to convert to.
1761 // The expression to convert.
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.
1771 // An unsafe type conversion, used to pass values to builtin functions.
1773 class Unsafe_type_conversion_expression
: public Expression
1776 Unsafe_type_conversion_expression(Type
* type
, Expression
* expr
,
1778 : Expression(EXPRESSION_UNSAFE_CONVERSION
, location
),
1779 type_(type
), expr_(expr
)
1784 { return this->expr_
; }
1788 do_traverse(Traverse
* traverse
);
1791 do_is_zero_value() const
1792 { return this->expr_
->is_zero_value(); }
1795 do_is_static_initializer() const;
1799 { return this->type_
; }
1802 do_determine_type(const Type_context
*)
1803 { this->expr_
->determine_type_no_context(); }
1809 do_get_backend(Translate_context
*);
1812 do_dump_expression(Ast_dump_context
*) const;
1815 // The type to convert to.
1817 // The expression to convert.
1821 // A Unary expression.
1823 class Unary_expression
: public Expression
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
)
1833 // Return the operator.
1836 { return this->op_
; }
1838 // Return the operand.
1841 { return this->expr_
; }
1843 // Record that an address expression does not escape.
1845 set_does_not_escape()
1847 go_assert(this->op_
== OPERATOR_AND
);
1848 this->escapes_
= false;
1851 // Record that this is an address expression which should create a
1852 // temporary variable if necessary. This is used for method calls.
1856 go_assert(this->op_
== OPERATOR_AND
);
1857 this->create_temp_
= true;
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.
1865 go_assert(this->op_
== OPERATOR_AND
);
1866 this->is_gc_root_
= true;
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.
1874 go_assert(this->op_
== OPERATOR_AND
);
1875 this->is_slice_init_
= true;
1878 // Call the address_taken method on the operand if necessary.
1880 check_operand_address_taken(Gogo
*);
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.
1886 eval_constant(Operator op
, const Numeric_constant
* unc
,
1887 Location
, Numeric_constant
* nc
, bool *issued_error
);
1890 do_import(Import_expression
*, Location
);
1892 // Declare that this deref does or does not require an explicit nil check.
1894 set_requires_nil_check(bool needed
)
1896 go_assert(this->op_
== OPERATOR_MULT
);
1898 this->issue_nil_check_
= NIL_CHECK_NEEDED
;
1900 this->issue_nil_check_
= NIL_CHECK_NOT_NEEDED
;
1905 do_traverse(Traverse
* traverse
)
1906 { return Expression::traverse(&this->expr_
, traverse
); }
1909 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1912 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1915 do_is_constant() const;
1918 do_is_static_initializer() const;
1921 do_numeric_constant_value(Numeric_constant
*) const;
1927 do_determine_type(const Type_context
*);
1930 do_check_types(Gogo
*);
1935 return Expression::make_unary(this->op_
, this->expr_
->copy(),
1940 do_must_eval_subexpressions_in_order(int*) const
1941 { return this->op_
== OPERATOR_MULT
; }
1944 do_is_addressable() const
1945 { return this->op_
== OPERATOR_MULT
; }
1948 do_get_backend(Translate_context
*);
1951 do_inlining_cost() const
1955 do_export(Export_function_body
*) const;
1958 do_dump_expression(Ast_dump_context
*) const;
1961 do_issue_nil_check()
1963 if (this->op_
== OPERATOR_MULT
)
1964 this->set_requires_nil_check(true);
1969 base_is_static_initializer(Expression
*);
1971 // Return a determination as to whether this dereference expression
1972 // requires a nil check.
1973 Nil_check_classification
1974 requires_nil_check(Gogo
*);
1976 // The unary operator to apply.
1978 // Normally true. False if this is an address expression which does
1979 // not escape the current function.
1981 // True if this is an address expression which should create a
1982 // temporary variable if necessary.
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.
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
1992 bool is_slice_init_
;
1995 // Whether or not to issue a nil check for this expression if its address
1997 Nil_check_classification issue_nil_check_
;
2000 // A binary expression.
2002 class Binary_expression
: public Expression
2005 Binary_expression(Operator op
, Expression
* left
, Expression
* right
,
2007 : Expression(EXPRESSION_BINARY
, location
),
2008 op_(op
), left_(left
), right_(right
), type_(NULL
)
2011 // Return the operator.
2014 { return this->op_
; }
2016 // Return the left hand expression.
2019 { return this->left_
; }
2021 // Return the right hand expression.
2024 { return this->right_
; }
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.
2030 eval_constant(Operator op
, Numeric_constant
* left_nc
,
2031 Numeric_constant
* right_nc
, Location location
,
2032 Numeric_constant
* nc
, bool* issued_error
);
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.
2038 compare_constant(Operator op
, Numeric_constant
* left_nc
,
2039 Numeric_constant
* right_nc
, Location location
,
2043 do_import(Import_expression
*, Location
);
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.
2048 check_operator_type(Operator op
, Type
* type
, Type
* otype
, Location
);
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.
2054 operation_type(Operator op
, Type
* left_type
, Type
* right_type
,
2055 Type
** result_type
);
2059 do_traverse(Traverse
* traverse
);
2062 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2065 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2068 do_is_constant() const
2069 { return this->left_
->is_constant() && this->right_
->is_constant(); }
2072 do_is_static_initializer() const;
2075 do_numeric_constant_value(Numeric_constant
*) const;
2078 do_discarding_value();
2084 do_determine_type(const Type_context
*);
2087 do_check_types(Gogo
*);
2092 return Expression::make_binary(this->op_
, this->left_
->copy(),
2093 this->right_
->copy(), this->location());
2097 do_get_backend(Translate_context
*);
2100 do_inlining_cost() const
2104 do_export(Export_function_body
*) const;
2107 do_dump_expression(Ast_dump_context
*) const;
2111 cmp_to_bool(Operator op
, int cmp
);
2114 eval_integer(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
2115 Location
, Numeric_constant
*);
2118 eval_float(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
2119 Location
, Numeric_constant
*);
2122 eval_complex(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
2123 Location
, Numeric_constant
*);
2126 compare_integer(const Numeric_constant
*, const Numeric_constant
*, int*);
2129 compare_float(const Numeric_constant
*, const Numeric_constant
*, int*);
2132 compare_complex(const Numeric_constant
*, const Numeric_constant
*, int*);
2135 lower_struct_comparison(Gogo
*, Statement_inserter
*);
2138 lower_array_comparison(Gogo
*, Statement_inserter
*);
2141 lower_interface_value_comparison(Gogo
*, Statement_inserter
*);
2144 lower_compare_to_memcmp(Gogo
*, Statement_inserter
*);
2147 operand_address(Statement_inserter
*, Expression
*);
2149 // The binary operator to apply.
2151 // The left hand side operand.
2153 // The right hand side operand.
2155 // The type of a comparison operation.
2159 // A string concatenation expression. This is a sequence of strings
2160 // added together. It is created when lowering Binary_expression.
2162 class String_concat_expression
: public Expression
2165 String_concat_expression(Expression_list
* exprs
)
2166 : Expression(EXPRESSION_STRING_CONCAT
, exprs
->front()->location()),
2170 // Return the list of string expressions to be concatenated.
2173 { return this->exprs_
; }
2177 do_traverse(Traverse
* traverse
)
2178 { return this->exprs_
->traverse(traverse
); }
2181 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
2185 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2188 do_is_constant() const;
2191 do_is_zero_value() const;
2194 do_is_static_initializer() const;
2200 do_determine_type(const Type_context
*);
2203 do_check_types(Gogo
*);
2207 { return Expression::make_string_concat(this->exprs_
->copy()); }
2210 do_get_backend(Translate_context
*)
2211 { go_unreachable(); }
2214 do_export(Export_function_body
*) const
2215 { go_unreachable(); }
2218 do_dump_expression(Ast_dump_context
*) const;
2221 // The string expressions to concatenate.
2222 Expression_list
* exprs_
;
2225 // A call expression. The go statement needs to dig inside this.
2227 class Call_expression
: public Expression
2230 Call_expression(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
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)
2240 // The function to call.
2243 { return this->fn_
; }
2248 { return this->args_
; }
2250 const Expression_list
*
2252 { return this->args_
; }
2254 // Get the function type.
2256 get_function_type() const;
2258 // Return the number of values this call will return.
2260 result_count() const;
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
*
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().
2272 set_expected_result_count(size_t);
2274 // Return whether this is a call to the predeclared function
2277 is_recover_call() const;
2279 // Set the argument for a call to recover.
2281 set_recover_arg(Expression
*);
2283 // Whether the last argument is a varargs argument (f(a...)).
2286 { return this->is_varargs_
; }
2288 // Return whether varargs have already been lowered.
2290 varargs_are_lowered() const
2291 { return this->varargs_are_lowered_
; }
2293 // Note that varargs have already been lowered.
2295 set_varargs_are_lowered()
2296 { this->varargs_are_lowered_
= true; }
2298 // Whether this call is being deferred.
2301 { return this->is_deferred_
; }
2303 // Note that the call is being deferred.
2306 { this->is_deferred_
= true; }
2308 // Whether this call is concurrently executed.
2310 is_concurrent() const
2311 { return this->is_concurrent_
; }
2313 // Note that the call is concurrently executed.
2316 { this->is_concurrent_
= true; }
2318 // We have found an error with this call expression; return true if
2319 // we should report it.
2323 // Whether or not this call contains errors, either in the call or the
2324 // arguments to the call.
2326 is_erroneous_call();
2328 // Whether this call returns multiple results that are used as an
2329 // multi-valued argument.
2331 is_multi_value_arg() const
2332 { return this->is_multi_value_arg_
; }
2334 // Note this call is used as a multi-valued argument.
2336 set_is_multi_value_arg()
2337 { this->is_multi_value_arg_
= true; }
2339 // Whether this is a call to builtin function.
2344 // Convert to a Builtin_call_expression, or return NULL.
2345 inline Builtin_call_expression
*
2346 builtin_call_expression();
2350 do_traverse(Traverse
*);
2353 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2356 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2359 do_discarding_value()
2366 do_determine_type(const Type_context
*);
2369 do_check_types(Gogo
*);
2375 do_must_eval_in_order() const;
2377 virtual Bexpression
*
2378 do_get_backend(Translate_context
*);
2381 do_is_recover_call() const;
2384 do_set_recover_arg(Expression
*);
2386 // Let a builtin expression change the argument list.
2388 set_args(Expression_list
* args
)
2389 { this->args_
= args
; }
2391 // Let a builtin expression lower varargs.
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
);
2397 // Let a builtin expression check whether types have been
2400 determining_types();
2403 do_dump_expression(Ast_dump_context
*) const;
2407 check_argument_type(int, const Type
*, const Type
*, Location
, bool);
2410 lower_to_builtin(Named_object
**, const char*, int*);
2413 interface_method_function(Interface_field_reference_expression
*,
2414 Expression
**, Location
);
2417 set_results(Translate_context
*);
2419 // The function to call.
2421 // The arguments to pass. This may be NULL if there are no
2423 Expression_list
* args_
;
2424 // The type of the expression, to avoid recomputing it.
2426 // The backend expression for the call, used for a call which returns a tuple.
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...)).
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.
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.
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.
2453 // A call expression to a builtin function.
2455 class Builtin_call_expression
: public Call_expression
2458 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
2459 bool is_varargs
, Location location
);
2461 // The builtin functions.
2462 enum Builtin_function_code
2466 // Predeclared builtin functions.
2483 // Builtin functions from the unsafe package.
2489 Builtin_function_code
2491 { return this->code_
; }
2493 // This overrides Call_expression::is_builtin.
2498 // Return whether EXPR, of array type, is a constant if passed to
2501 array_len_is_constant(Expression
* expr
);
2504 flatten_append(Gogo
*, Named_object
*, Statement_inserter
*, Expression
*,
2508 // This overrides Call_expression::do_lower.
2510 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2513 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2516 do_is_constant() const;
2519 do_numeric_constant_value(Numeric_constant
*) const;
2522 do_discarding_value();
2528 do_determine_type(const Type_context
*);
2531 do_check_types(Gogo
*);
2537 do_get_backend(Translate_context
*);
2540 do_export(Export_function_body
*) const;
2543 do_is_recover_call() const;
2546 do_set_recover_arg(Expression
*);
2556 real_imag_type(Type
*);
2559 complex_type(Type
*);
2562 lower_make(Statement_inserter
*);
2565 check_int_value(Expression
*, bool is_length
, bool* small
);
2567 // A pointer back to the general IR structure. This avoids a global
2568 // variable, or passing it around everywhere.
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.
2575 // Whether the argument is set for calls to BUILTIN_RECOVER.
2576 bool recover_arg_is_set_
;
2579 inline Builtin_call_expression
*
2580 Call_expression::builtin_call_expression()
2582 return (this->is_builtin()
2583 ? static_cast<Builtin_call_expression
*>(this)
2587 // A single result from a call which returns multiple results.
2589 class Call_result_expression
: public Expression
2592 Call_result_expression(Call_expression
* call
, unsigned int index
)
2593 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
2594 call_(call
), index_(index
)
2599 { return this->call_
; }
2603 { return this->index_
; }
2607 do_traverse(Traverse
*);
2613 do_determine_type(const Type_context
*);
2616 do_check_types(Gogo
*);
2621 return new Call_result_expression(this->call_
->call_expression(),
2626 do_must_eval_in_order() const
2630 do_get_backend(Translate_context
*);
2633 do_dump_expression(Ast_dump_context
*) const;
2636 // The underlying call expression.
2638 // Which result we want.
2639 unsigned int index_
;
2642 // An expression which represents a pointer to a function.
2644 class Func_expression
: public Expression
2647 Func_expression(Named_object
* function
, Expression
* closure
,
2649 : Expression(EXPRESSION_FUNC_REFERENCE
, location
),
2650 function_(function
), closure_(closure
),
2651 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS
)
2654 // Return the object associated with the function.
2656 named_object() const
2657 { return this->function_
; }
2659 // Return the closure for this function. This will return NULL if
2660 // the function has no closure, which is the normal case.
2663 { return this->closure_
; }
2665 // Return whether this is a reference to a runtime function.
2667 is_runtime_function() const
2668 { return this->runtime_code_
!= Runtime::NUMBER_OF_FUNCTIONS
; }
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.
2674 runtime_code() const
2675 { return this->runtime_code_
; }
2677 // Set the runtime code for this function expression.
2679 set_runtime_code(Runtime::Function code
)
2680 { this->runtime_code_
= code
; }
2682 // Return a backend expression for the code of a function.
2684 get_code_pointer(Gogo
*, Named_object
* function
, Location loc
);
2688 do_traverse(Traverse
*);
2694 do_determine_type(const Type_context
*)
2696 if (this->closure_
!= NULL
)
2697 this->closure_
->determine_type_no_context();
2703 return Expression::make_func_reference(this->function_
,
2704 (this->closure_
== NULL
2706 : this->closure_
->copy()),
2711 do_get_backend(Translate_context
*);
2714 do_dump_expression(Ast_dump_context
*) const;
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_
;
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.
2731 class Func_descriptor_expression
: public Expression
2734 Func_descriptor_expression(Named_object
* fn
);
2736 // Make the function descriptor type, so that it can be converted.
2738 make_func_descriptor_type();
2742 do_traverse(Traverse
*);
2748 do_determine_type(const Type_context
*)
2753 { return Expression::make_func_descriptor(this->fn_
); }
2756 do_is_addressable() const
2760 do_get_backend(Translate_context
*);
2763 do_dump_expression(Ast_dump_context
* context
) const;
2766 // The type of all function descriptors.
2767 static Type
* descriptor_type
;
2769 // The function for which this is the descriptor.
2771 // The descriptor variable.
2775 // A reference to an unknown name.
2777 class Unknown_expression
: public Parser_expression
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)
2786 // The associated named object.
2788 named_object() const
2789 { return this->named_object_
; }
2791 // The name of the identifier which was unknown.
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.
2799 set_no_error_message()
2800 { this->no_error_message_
= true; }
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.
2805 set_is_composite_literal_key()
2806 { this->is_composite_literal_key_
= true; }
2808 // Note that this expression should no longer be treated as a
2809 // composite literal key.
2811 clear_is_composite_literal_key()
2812 { this->is_composite_literal_key_
= false; }
2816 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2820 { return new Unknown_expression(this->named_object_
, this->location()); }
2823 do_dump_expression(Ast_dump_context
*) const;
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_
;
2835 // An index expression. This is lowered to an array index, a string
2836 // index, or a map index.
2838 class Index_expression
: public Parser_expression
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
)
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.
2850 dump_index_expression(Ast_dump_context
*, const Expression
* expr
,
2851 const Expression
* start
, const Expression
* end
,
2852 const Expression
* cap
);
2856 do_traverse(Traverse
*);
2859 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2864 return new Index_expression(this->left_
->copy(), this->start_
->copy(),
2867 : this->end_
->copy()),
2870 : this->cap_
->copy()),
2874 // This shouldn't be called--we don't know yet.
2876 do_must_eval_subexpressions_in_order(int*) const
2877 { go_unreachable(); }
2880 do_dump_expression(Ast_dump_context
*) const;
2883 do_issue_nil_check()
2884 { this->left_
->issue_nil_check(); }
2886 // The expression being indexed.
2890 // The second index. This is NULL for an index, non-NULL for a
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
2899 // An array index. This is used for both indexing and slicing.
2901 class Array_index_expression
: public Expression
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)
2911 // Return the array.
2914 { return this->array_
; }
2918 { return this->array_
; }
2920 // Return the index of a simple index expression, or the start index
2921 // of a slice expression.
2924 { return this->start_
; }
2928 { return this->start_
; }
2930 // Return the end index of a slice expression. This is NULL for a
2931 // simple index expression.
2934 { return this->end_
; }
2938 { return this->end_
; }
2940 // Return whether this array index expression appears in an lvalue
2941 // (left hand side of assignment) context.
2944 { return this->is_lvalue_
; }
2946 // Update this array index expression to indicate that it appears
2947 // in a left-hand-side or lvalue context.
2950 { this->is_lvalue_
= true; }
2953 set_needs_bounds_check(bool b
)
2954 { this->needs_bounds_check_
= b
; }
2958 do_traverse(Traverse
*);
2961 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2967 do_determine_type(const Type_context
*);
2970 do_check_types(Gogo
*);
2975 Expression
* ret
= Expression::make_array_index(this->array_
->copy(),
2976 this->start_
->copy(),
2979 : this->end_
->copy()),
2982 : this->cap_
->copy()),
2984 ret
->array_index_expression()->set_needs_bounds_check(this->needs_bounds_check_
);
2989 do_must_eval_subexpressions_in_order(int* skip
) const;
2992 do_is_addressable() const;
2995 do_address_taken(bool escapes
);
2998 do_issue_nil_check()
2999 { this->array_
->issue_nil_check(); }
3002 do_get_backend(Translate_context
*);
3005 do_dump_expression(Ast_dump_context
*) const;
3008 // The array we are getting a value from.
3010 // The start or only index.
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.
3015 // The capacity argument of a slice. This may be NULL for an array index or
3018 // The type of the expression.
3020 // Whether expr appears in an lvalue context.
3022 // Whether bounds check is needed.
3023 bool needs_bounds_check_
;
3026 // A string index. This is used for both indexing and slicing.
3028 class String_index_expression
: public Expression
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
)
3037 // Return the string being indexed.
3040 { return this->string_
; }
3044 do_traverse(Traverse
*);
3047 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3053 do_determine_type(const Type_context
*);
3056 do_check_types(Gogo
*);
3061 return Expression::make_string_index(this->string_
->copy(),
3062 this->start_
->copy(),
3065 : this->end_
->copy()),
3070 do_must_eval_subexpressions_in_order(int*) const
3074 do_get_backend(Translate_context
*);
3077 do_dump_expression(Ast_dump_context
*) const;
3080 // The string we are getting a value from.
3081 Expression
* string_
;
3082 // The start or only index.
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.
3089 // An index into a map.
3091 class Map_index_expression
: public Expression
3094 Map_index_expression(Expression
* map
, Expression
* index
,
3096 : Expression(EXPRESSION_MAP_INDEX
, location
),
3097 map_(map
), index_(index
), value_pointer_(NULL
)
3103 { return this->map_
; }
3107 { return this->map_
; }
3109 // Return the index.
3112 { return this->index_
; }
3116 { return this->index_
; }
3118 // Get the type of the map being indexed.
3120 get_map_type() const;
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
3127 get_value_pointer(Gogo
*);
3131 do_traverse(Traverse
*);
3134 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3140 do_determine_type(const Type_context
*);
3143 do_check_types(Gogo
*);
3148 return Expression::make_map_index(this->map_
->copy(),
3149 this->index_
->copy(),
3154 do_must_eval_subexpressions_in_order(int*) const
3157 // A map index expression is an lvalue but it is not addressable.
3160 do_get_backend(Translate_context
*);
3163 do_dump_expression(Ast_dump_context
*) const;
3166 // The map we are looking into.
3170 // A pointer to the value at this index.
3171 Expression
* value_pointer_
;
3174 // An expression which represents a method bound to its first
3177 class Bound_method_expression
: public Expression
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
)
3186 // Return the object which is the first argument.
3189 { return this->expr_
; }
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.
3195 first_argument_type() const
3196 { return this->expr_type_
; }
3198 // Return the method.
3201 { return this->method_
; }
3203 // Return the function to call.
3206 { return this->function_
; }
3208 // Set the implicit type of the expression.
3210 set_first_argument_type(Type
* type
)
3211 { this->expr_type_
= type
; }
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
);
3220 do_traverse(Traverse
*);
3223 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3229 do_determine_type(const Type_context
*);
3232 do_check_types(Gogo
*);
3237 return new Bound_method_expression(this->expr_
->copy(), this->method_
,
3238 this->function_
, this->location());
3242 do_get_backend(Translate_context
*)
3243 { go_unreachable(); }
3246 do_dump_expression(Ast_dump_context
*) const;
3249 // A mapping from method functions to the thunks we have created for
3251 typedef Unordered_map(Named_object
*, Named_object
*) Method_value_thunks
;
3252 static Method_value_thunks method_value_thunks
;
3254 // The object used to find the method. This is passed to the method
3255 // as the first argument.
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.
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_
;
3269 // A reference to a field in a struct.
3271 class Field_reference_expression
: public Expression
3274 Field_reference_expression(Expression
* expr
, unsigned int field_index
,
3276 : Expression(EXPRESSION_FIELD_REFERENCE
, location
),
3277 expr_(expr
), field_index_(field_index
), implicit_(false), called_fieldtrack_(false)
3280 // Return the struct expression.
3283 { return this->expr_
; }
3285 // Return the field index.
3288 { return this->field_index_
; }
3290 // Return whether this node was implied by an anonymous field.
3293 { return this->implicit_
; }
3296 set_implicit(bool implicit
)
3297 { this->implicit_
= implicit
; }
3299 // Set the struct expression. This is used when parsing.
3301 set_struct_expression(Expression
* expr
)
3303 go_assert(this->expr_
== NULL
);
3309 do_traverse(Traverse
* traverse
)
3310 { return Expression::traverse(&this->expr_
, traverse
); }
3313 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
3319 do_determine_type(const Type_context
*)
3320 { this->expr_
->determine_type_no_context(); }
3323 do_check_types(Gogo
*);
3328 return Expression::make_field_reference(this->expr_
->copy(),
3334 do_is_addressable() const
3335 { return this->expr_
->is_addressable(); }
3338 do_address_taken(bool escapes
)
3339 { this->expr_
->address_taken(escapes
); }
3342 do_issue_nil_check()
3343 { this->expr_
->issue_nil_check(); }
3346 do_get_backend(Translate_context
*);
3349 do_dump_expression(Ast_dump_context
*) const;
3352 // The expression we are looking into. This should have a type of
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.
3360 // Whether we have already emitted a fieldtrack call.
3361 bool called_fieldtrack_
;
3364 // A reference to a field of an interface.
3366 class Interface_field_reference_expression
: public Expression
3369 Interface_field_reference_expression(Expression
* expr
,
3370 const std::string
& name
,
3372 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE
, location
),
3373 expr_(expr
), name_(name
)
3376 // Return the expression for the interface object.
3379 { return this->expr_
; }
3381 // Return the name of the method to call.
3384 { return this->name_
; }
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
);
3391 // Return an expression for the pointer to the function to call.
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.
3398 get_underlying_object();
3402 do_traverse(Traverse
* traverse
);
3405 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3411 do_determine_type(const Type_context
*);
3414 do_check_types(Gogo
*);
3419 return Expression::make_interface_field_reference(this->expr_
->copy(),
3425 do_get_backend(Translate_context
*);
3428 do_dump_expression(Ast_dump_context
*) const;
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
;
3438 // The expression for the interface object. This should have a type
3439 // of interface or pointer to interface.
3441 // The field we are retrieving--the name of the method.
3445 // Implement the builtin function new.
3447 class Allocation_expression
: public Expression
3450 Allocation_expression(Type
* type
, Location location
)
3451 : Expression(EXPRESSION_ALLOCATION
, location
),
3452 type_(type
), allocate_on_stack_(false)
3456 set_allocate_on_stack()
3457 { this->allocate_on_stack_
= true; }
3461 do_traverse(Traverse
*);
3467 do_determine_type(const Type_context
*)
3471 do_check_types(Gogo
*);
3477 do_get_backend(Translate_context
*);
3480 do_dump_expression(Ast_dump_context
*) const;
3483 // The type we are allocating.
3485 // Whether or not this is a stack allocation.
3486 bool allocate_on_stack_
;
3489 // A general composite literal. This is lowered to a type specific
3492 class Composite_literal_expression
: public Parser_expression
3495 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
3496 Expression_list
* vals
, bool all_are_names
,
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
))
3504 // Mark the DEPTH entry of KEY_PATH as containing a key.
3506 update_key_path(size_t depth
)
3508 go_assert(depth
< this->key_path_
.size());
3509 this->key_path_
[depth
] = true;
3514 do_traverse(Traverse
* traverse
);
3517 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
3523 do_dump_expression(Ast_dump_context
*) const;
3527 lower_struct(Gogo
*, Type
*);
3533 make_array(Type
*, const std::vector
<unsigned long>*, Expression_list
*);
3536 lower_map(Gogo
*, Named_object
*, Statement_inserter
*, Type
*);
3538 // The type of the composite literal.
3540 // The depth within a list of composite literals within a composite
3541 // literal, when the type is omitted.
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.
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_
;
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.
3562 class Ordered_value_list
3565 Ordered_value_list(Expression_list
* vals
)
3566 : vals_(vals
), traverse_order_(NULL
)
3571 { return this->vals_
; }
3574 traverse_vals(Traverse
* traverse
);
3576 // Get the traversal order (may be NULL)
3577 std::vector
<unsigned long>*
3579 { return traverse_order_
; }
3581 // Set the traversal order, used to ensure that we implement the
3582 // order of evaluation rules. Takes ownership of the argument.
3584 set_traverse_order(std::vector
<unsigned long>* traverse_order
)
3585 { this->traverse_order_
= traverse_order
; }
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_
;
3599 // Construct a struct.
3601 class Struct_construction_expression
: public Expression
,
3602 public Ordered_value_list
3605 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
3607 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
3608 Ordered_value_list(vals
),
3612 // Return whether this is a constant initializer.
3614 is_constant_struct() const;
3618 do_traverse(Traverse
* traverse
);
3621 do_is_zero_value() const;
3624 do_is_static_initializer() const;
3628 { return this->type_
; }
3631 do_determine_type(const Type_context
*);
3634 do_check_types(Gogo
*);
3640 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3643 do_get_backend(Translate_context
*);
3646 do_export(Export_function_body
*) const;
3649 do_dump_expression(Ast_dump_context
*) const;
3652 // The type of the struct to construct.
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.
3660 class Array_construction_expression
: public Expression
,
3661 public Ordered_value_list
3664 Array_construction_expression(Expression_classification classification
,
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()); }
3674 // Return whether this is a constant initializer.
3676 is_constant_array() const;
3678 // Return the number of elements.
3680 element_count() const
3681 { return this->vals() == NULL
? 0 : this->vals()->size(); }
3685 do_traverse(Traverse
* traverse
);
3688 do_is_zero_value() const;
3691 do_is_static_initializer() const;
3695 { return this->type_
; }
3698 do_determine_type(const Type_context
*);
3701 do_check_types(Gogo
*);
3704 do_export(Export_function_body
*) const;
3707 const std::vector
<unsigned long>*
3709 { return this->indexes_
; }
3712 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3714 // Get the backend constructor for the array values.
3716 get_constructor(Translate_context
* context
, Btype
* btype
);
3719 do_dump_expression(Ast_dump_context
*) const;
3722 dump_slice_storage_expression(Ast_dump_context
*) const { }
3725 // The type of the array to construct.
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_
;
3732 // Construct a fixed array.
3734 class Fixed_array_construction_expression
:
3735 public Array_construction_expression
3738 Fixed_array_construction_expression(Type
* type
,
3739 const std::vector
<unsigned long>* indexes
,
3740 Expression_list
* vals
, Location location
);
3747 do_get_backend(Translate_context
*);
3750 // Construct a slice.
3752 class Slice_construction_expression
: public Array_construction_expression
3755 Slice_construction_expression(Type
* type
,
3756 const std::vector
<unsigned long>* indexes
,
3757 Expression_list
* vals
, Location location
);
3760 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3762 // Record that the storage for this slice (e.g. vals) cannot escape,
3763 // hence it can be stack-allocated.
3765 set_storage_does_not_escape()
3767 this->storage_escapes_
= false;
3771 // Note that taking the address of a slice literal is invalid.
3774 do_traverse(Traverse
* traverse
);
3780 do_get_backend(Translate_context
*);
3783 dump_slice_storage_expression(Ast_dump_context
* ast_dump_context
) const;
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().
3792 // The type of the values in this slice.
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_
;
3805 class Map_construction_expression
: public Expression
3808 Map_construction_expression(Type
* type
, Expression_list
* vals
,
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); }
3816 { return this->vals_
; }
3820 do_traverse(Traverse
* traverse
);
3823 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3827 { return this->type_
; }
3830 do_determine_type(const Type_context
*);
3833 do_check_types(Gogo
*);
3839 do_get_backend(Translate_context
*);
3842 do_export(Export_function_body
*) const;
3845 do_dump_expression(Ast_dump_context
*) const;
3848 // The type of the map to construct.
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_
;
3858 // A type guard expression.
3860 class Type_guard_expression
: public Expression
3863 Type_guard_expression(Expression
* expr
, Type
* type
, Location location
)
3864 : Expression(EXPRESSION_TYPE_GUARD
, location
),
3865 expr_(expr
), type_(type
)
3868 // Return the expression to convert.
3871 { return this->expr_
; }
3873 // Return the type to which to convert.
3876 { return this->type_
; }
3880 do_traverse(Traverse
* traverse
);
3883 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3887 { return this->type_
; }
3890 do_determine_type(const Type_context
*)
3891 { this->expr_
->determine_type_no_context(); }
3894 do_check_types(Gogo
*);
3900 do_get_backend(Translate_context
*);
3903 do_dump_expression(Ast_dump_context
*) const;
3906 // The expression to convert.
3908 // The type to which to convert.
3912 // Class Heap_expression.
3914 // When you take the address of an escaping expression, it is allocated
3915 // on the heap. This class implements that.
3917 class Heap_expression
: public Expression
3920 Heap_expression(Expression
* expr
, Location location
)
3921 : Expression(EXPRESSION_HEAP
, location
),
3922 expr_(expr
), allocate_on_stack_(false)
3927 { return this->expr_
; }
3930 set_allocate_on_stack()
3931 { this->allocate_on_stack_
= true; }
3935 do_traverse(Traverse
* traverse
)
3936 { return Expression::traverse(&this->expr_
, traverse
); }
3941 do_determine_type(const Type_context
*)
3942 { this->expr_
->determine_type_no_context(); }
3947 return Expression::make_heap_expression(this->expr_
->copy(),
3952 do_get_backend(Translate_context
*);
3954 // We only export global objects, and the parser does not generate
3955 // this in global scope.
3957 do_export(Export_function_body
*) const
3958 { go_unreachable(); }
3961 do_dump_expression(Ast_dump_context
*) const;
3964 // The expression which is being put on the heap.
3966 // Whether or not this is a stack allocation.
3967 bool allocate_on_stack_
;
3970 // A receive expression.
3972 class Receive_expression
: public Expression
3975 Receive_expression(Expression
* channel
, Location location
)
3976 : Expression(EXPRESSION_RECEIVE
, location
),
3977 channel_(channel
), temp_receiver_(NULL
)
3980 // Return the channel.
3983 { return this->channel_
; }
3986 do_import(Import_expression
*, Location
);
3990 do_traverse(Traverse
* traverse
)
3991 { return Expression::traverse(&this->channel_
, traverse
); }
3994 do_discarding_value()
4001 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
4004 do_determine_type(const Type_context
*)
4005 { this->channel_
->determine_type_no_context(); }
4008 do_check_types(Gogo
*);
4013 return Expression::make_receive(this->channel_
->copy(), this->location());
4017 do_inlining_cost() const
4021 do_must_eval_in_order() const
4025 do_get_backend(Translate_context
*);
4028 do_export(Export_function_body
*) const;
4031 do_dump_expression(Ast_dump_context
*) const;
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_
;
4040 // An expression that represents a slice value: a struct with value pointer,
4041 // length, and capacity fields.
4043 class Slice_value_expression
: public Expression
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
)
4052 // The memory holding the values in the slice. The type should be a
4053 // pointer to the element value of the slice.
4056 { return this->valmem_
; }
4060 do_traverse(Traverse
*);
4064 { return this->type_
; }
4067 do_determine_type(const Type_context
*)
4074 do_get_backend(Translate_context
* context
);
4077 do_dump_expression(Ast_dump_context
*) const;
4080 // The type of the slice value.
4082 // The memory holding the values in the slice.
4083 Expression
* valmem_
;
4084 // The length of the slice.
4086 // The capacity of the slice.
4090 // Conditional expressions.
4092 class Conditional_expression
: public Expression
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
)
4103 { return this->cond_
; }
4107 do_traverse(Traverse
*);
4113 do_determine_type(const Type_context
*);
4118 return new Conditional_expression(this->cond_
->copy(), this->then_
->copy(),
4119 this->else_
->copy(), this->location());
4123 do_get_backend(Translate_context
* context
);
4126 do_dump_expression(Ast_dump_context
*) const;
4129 // The condition to be checked.
4131 // The expression to execute if the condition is true.
4133 // The expression to execute if the condition is false.
4137 // Compound expressions.
4139 class Compound_expression
: public Expression
4142 Compound_expression(Expression
* init
, Expression
* expr
, Location location
)
4143 : Expression(EXPRESSION_COMPOUND
, location
), init_(init
), expr_(expr
)
4148 { return this->init_
; }
4152 do_traverse(Traverse
*);
4158 do_determine_type(const Type_context
*);
4163 return new Compound_expression(this->init_
->copy(), this->expr_
->copy(),
4168 do_get_backend(Translate_context
* context
);
4171 do_dump_expression(Ast_dump_context
*) const;
4174 // The expression that is evaluated first and discarded.
4176 // The expression that is evaluated and returned.
4180 // A backend expression. This is a backend expression wrapped in an
4181 // Expression, for convenience during backend generation.
4183 class Backend_expression
: public Expression
4186 Backend_expression(Bexpression
* bexpr
, Type
* type
, Location location
)
4187 : Expression(EXPRESSION_BACKEND
, location
), bexpr_(bexpr
), type_(type
)
4192 do_traverse(Traverse
*);
4194 // For now these are always valid static initializers. If that
4195 // changes we can change this.
4197 do_is_static_initializer() const
4202 { return this->type_
; }
4205 do_determine_type(const Type_context
*)
4212 do_get_backend(Translate_context
*)
4213 { return this->bexpr_
; }
4216 do_dump_expression(Ast_dump_context
*) const;
4219 // The backend expression we are wrapping.
4220 Bexpression
* bexpr_
;
4221 // The type of the expression;
4225 // A numeric constant. This is used both for untyped constants and
4226 // for constants that have a type.
4228 class Numeric_constant
4232 : classification_(NC_INVALID
), type_(NULL
)
4235 ~Numeric_constant();
4237 Numeric_constant(const Numeric_constant
&);
4239 Numeric_constant
& operator=(const Numeric_constant
&);
4241 // Check equality with another numeric constant.
4243 equals(const Numeric_constant
&) const;
4245 // Set to an unsigned long value.
4247 set_unsigned_long(Type
*, unsigned long);
4249 // Set to an integer value.
4251 set_int(Type
*, const mpz_t
);
4253 // Set to a rune value.
4255 set_rune(Type
*, const mpz_t
);
4257 // Set to a floating point value.
4259 set_float(Type
*, const mpfr_t
);
4261 // Set to a complex value.
4263 set_complex(Type
*, const mpc_t
);
4265 // Mark numeric constant as invalid.
4268 { this->classification_
= NC_INVALID
; }
4273 { return this->classification_
== Numeric_constant::NC_INT
; }
4277 { return this->classification_
== Numeric_constant::NC_RUNE
; }
4281 { return this->classification_
== Numeric_constant::NC_FLOAT
; }
4285 { return this->classification_
== Numeric_constant::NC_COMPLEX
; }
4289 { return this->classification_
== Numeric_constant::NC_INVALID
; }
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.
4295 get_int(mpz_t
*) const;
4298 get_rune(mpz_t
*) const;
4301 get_float(mpfr_t
*) const;
4304 get_complex(mpc_t
*) const;
4306 // Codes returned by to_unsigned_long.
4307 enum To_unsigned_long
4309 // Value is integer and fits in unsigned long.
4311 // Value is not integer.
4313 // Value is integer but is negative.
4315 // Value is non-negative integer but does not fit in unsigned
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.
4324 to_unsigned_long(unsigned long* val
) const;
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.
4331 to_memory_size(int64_t* val
) const;
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.
4337 to_int(mpz_t
* val
) const;
4339 // If the value can be expressed as a float, return true and
4340 // initialize and set VAL.
4342 to_float(mpfr_t
* val
) const;
4344 // If the value can be expressed as a complex, return true and
4345 // initialize and set VR and VI.
4347 to_complex(mpc_t
* val
) const;
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.
4358 set_type(Type
* type
, bool issue_error
, Location location
);
4360 // Return an Expression for this value.
4362 expression(Location
) const;
4364 // Calculate a hash code with a given seed.
4366 hash(unsigned int seed
) const;
4373 mpz_to_unsigned_long(const mpz_t ival
, unsigned long *val
) const;
4376 mpfr_to_unsigned_long(const mpfr_t fval
, unsigned long *val
) const;
4379 mpz_to_memory_size(const mpz_t ival
, int64_t* val
) const;
4382 mpfr_to_memory_size(const mpfr_t fval
, int64_t* val
) const;
4385 check_int_type(Integer_type
*, bool, Location
);
4388 check_float_type(Float_type
*, bool, Location
);
4391 check_complex_type(Complex_type
*, bool, Location
);
4394 is_float_neg_zero(const mpfr_t
, int bits
);
4396 // The kinds of constants.
4406 // The kind of constant.
4407 Classification classification_
;
4411 // If NC_INT or NC_RUNE.
4418 // The type if there is one. This will be NULL for an untyped
4423 #endif // !defined(GO_EXPRESSIONS_H)