1 // expressions.h -- Go frontend expression handling. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #ifndef GO_EXPRESSIONS_H
8 #define GO_EXPRESSIONS_H
17 class Translate_context
;
19 class Statement_inserter
;
30 class Expression_list
;
32 class Enclosed_var_expression
;
33 class Temporary_reference_expression
;
34 class Set_and_use_temporary_expression
;
35 class String_expression
;
36 class Type_conversion_expression
;
37 class Unsafe_type_conversion_expression
;
38 class Unary_expression
;
39 class Binary_expression
;
40 class String_concat_expression
;
41 class Call_expression
;
42 class Call_result_expression
;
43 class Func_expression
;
44 class Func_descriptor_expression
;
45 class Unknown_expression
;
46 class Index_expression
;
47 class Array_index_expression
;
48 class String_index_expression
;
49 class Map_index_expression
;
50 class Bound_method_expression
;
51 class Field_reference_expression
;
52 class Interface_field_reference_expression
;
53 class Allocation_expression
;
54 class Composite_literal_expression
;
55 class Struct_construction_expression
;
56 class Array_construction_expression
;
57 class Fixed_array_construction_expression
;
58 class Slice_construction_expression
;
59 class Map_construction_expression
;
60 class Type_guard_expression
;
61 class Heap_expression
;
62 class Receive_expression
;
63 class Conditional_expression
;
64 class Compound_expression
;
65 class Numeric_constant
;
69 class Temporary_statement
;
71 class Ast_dump_context
;
74 // The precision to use for complex values represented as an mpc_t.
75 const int mpc_precision
= 256;
77 // The base class for all expressions.
82 // The types of expressions.
83 enum Expression_classification
89 EXPRESSION_STRING_CONCAT
,
90 EXPRESSION_CONST_REFERENCE
,
91 EXPRESSION_VAR_REFERENCE
,
92 EXPRESSION_ENCLOSED_VAR_REFERENCE
,
93 EXPRESSION_TEMPORARY_REFERENCE
,
94 EXPRESSION_SET_AND_USE_TEMPORARY
,
96 EXPRESSION_FUNC_REFERENCE
,
97 EXPRESSION_FUNC_DESCRIPTOR
,
98 EXPRESSION_FUNC_CODE_REFERENCE
,
99 EXPRESSION_UNKNOWN_REFERENCE
,
102 EXPRESSION_STRING_INFO
,
109 EXPRESSION_CALL_RESULT
,
110 EXPRESSION_BOUND_METHOD
,
112 EXPRESSION_ARRAY_INDEX
,
113 EXPRESSION_STRING_INDEX
,
114 EXPRESSION_MAP_INDEX
,
116 EXPRESSION_FIELD_REFERENCE
,
117 EXPRESSION_INTERFACE_FIELD_REFERENCE
,
118 EXPRESSION_ALLOCATION
,
119 EXPRESSION_TYPE_GUARD
,
120 EXPRESSION_CONVERSION
,
121 EXPRESSION_UNSAFE_CONVERSION
,
122 EXPRESSION_STRUCT_CONSTRUCTION
,
123 EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
124 EXPRESSION_SLICE_CONSTRUCTION
,
125 EXPRESSION_MAP_CONSTRUCTION
,
126 EXPRESSION_COMPOSITE_LITERAL
,
129 EXPRESSION_TYPE_DESCRIPTOR
,
130 EXPRESSION_GC_SYMBOL
,
131 EXPRESSION_PTRMASK_SYMBOL
,
132 EXPRESSION_TYPE_INFO
,
133 EXPRESSION_SLICE_INFO
,
134 EXPRESSION_SLICE_VALUE
,
135 EXPRESSION_INTERFACE_INFO
,
136 EXPRESSION_INTERFACE_VALUE
,
137 EXPRESSION_INTERFACE_MTABLE
,
138 EXPRESSION_STRUCT_FIELD_OFFSET
,
139 EXPRESSION_LABEL_ADDR
,
140 EXPRESSION_CONDITIONAL
,
145 Expression(Expression_classification
, Location
);
147 virtual ~Expression();
149 // Make an error expression. This is used when a parse error occurs
150 // to prevent cascading errors.
152 make_error(Location
);
154 // Make an expression which is really a type. This is used during
157 make_type(Type
*, Location
);
159 // Make a unary expression.
161 make_unary(Operator
, Expression
*, Location
);
163 // Make a binary expression.
165 make_binary(Operator
, Expression
*, Expression
*, Location
);
167 // Make a string concatenation expression.
169 make_string_concat(Expression_list
*);
171 // Make a reference to a constant in an expression.
173 make_const_reference(Named_object
*, Location
);
175 // Make a reference to a variable in an expression.
177 make_var_reference(Named_object
*, Location
);
179 // Make a reference to a variable within an enclosing function.
181 make_enclosing_var_reference(Expression
*, Named_object
*, Location
);
183 // Make a reference to a temporary variable. Temporary variables
184 // are always created by a single statement, which is what we use to
186 static Temporary_reference_expression
*
187 make_temporary_reference(Temporary_statement
*, Location
);
189 // Make an expressions which sets a temporary variable and then
190 // evaluates to a reference to that temporary variable. This is
191 // used to set a temporary variable while retaining the order of
193 static Set_and_use_temporary_expression
*
194 make_set_and_use_temporary(Temporary_statement
*, Expression
*, Location
);
196 // Make a sink expression--a reference to the blank identifier _.
200 // Make a reference to a function in an expression. This returns a
201 // pointer to the struct holding the address of the function
202 // followed by any closed-over variables.
204 make_func_reference(Named_object
*, Expression
* closure
, Location
);
206 // Make a function descriptor, an immutable struct with a single
207 // field that points to the function code. This may only be used
208 // with functions that do not have closures. FN is the function for
209 // which we are making the descriptor.
210 static Func_descriptor_expression
*
211 make_func_descriptor(Named_object
* fn
);
213 // Make a reference to the code of a function. This is used to set
214 // descriptor and closure fields.
216 make_func_code_reference(Named_object
*, Location
);
218 // Make a reference to an unknown name. In a correct program this
219 // will always be lowered to a real const/var/func reference.
220 static Unknown_expression
*
221 make_unknown_reference(Named_object
*, Location
);
223 // Make a constant bool expression.
225 make_boolean(bool val
, Location
);
227 // Make a constant string expression.
229 make_string(const std::string
&, Location
);
231 // Make an expression that evaluates to some characteristic of an string.
232 // For simplicity, the enum values must match the field indexes in the
233 // underlying struct.
236 // The underlying data in the string.
238 // The length of the string.
243 make_string_info(Expression
* string
, String_info
, Location
);
245 // Make a character constant expression. TYPE should be NULL for an
248 make_character(const mpz_t
*, Type
*, Location
);
250 // Make a constant integer expression from a multi-precision
251 // integer. TYPE should be NULL for an abstract type.
253 make_integer_z(const mpz_t
*, Type
*, Location
);
255 // Make a constant integer expression from an unsigned long. TYPE
256 // should be NULL for an abstract type.
258 make_integer_ul(unsigned long, Type
*, Location
);
260 // Make a constant integer expression from a signed long. TYPE
261 // should be NULL for an abstract type.
263 make_integer_sl(long, Type
*, Location
);
265 // Make a constant integer expression from an int64_t. TYPE should
266 // be NULL for an abstract type.
268 make_integer_int64(int64_t, Type
*, Location
);
270 // Make a constant float expression. TYPE should be NULL for an
273 make_float(const mpfr_t
*, Type
*, Location
);
275 // Make a constant complex expression. TYPE should be NULL for an
278 make_complex(const mpc_t
*, Type
*, Location
);
280 // Make a nil expression.
284 // Make an iota expression. This is used for the predeclared
289 // Make a call expression.
290 static Call_expression
*
291 make_call(Expression
* func
, Expression_list
* args
, bool is_varargs
,
294 // Make a reference to a specific result of a call expression which
297 make_call_result(Call_expression
*, unsigned int index
);
299 // Make an expression which is a method bound to its first
300 // parameter. METHOD is the method being called, FUNCTION is the
302 static Bound_method_expression
*
303 make_bound_method(Expression
* object
, const Method
* method
,
304 Named_object
* function
, Location
);
306 // Make an index or slice expression. This is a parser expression
307 // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
308 // index rather than a slice. CAP may be NULL, meaning we use the default
309 // capacity of LEFT. At parse time we may not know the type of LEFT.
310 // After parsing this is lowered to an array index, a string index,
313 make_index(Expression
* left
, Expression
* start
, Expression
* end
,
314 Expression
* cap
, Location
);
316 // Make an array index expression. END may be NULL, in which case
317 // this is an lvalue. CAP may be NULL, in which case it defaults
320 make_array_index(Expression
* array
, Expression
* start
, Expression
* end
,
321 Expression
* cap
, Location
);
323 // Make a string index expression. END may be NULL. This is never
326 make_string_index(Expression
* string
, Expression
* start
, Expression
* end
,
329 // Make a map index expression. This is an lvalue.
330 static Map_index_expression
*
331 make_map_index(Expression
* map
, Expression
* val
, Location
);
333 // Make a selector. This is a parser expression which represents
334 // LEFT.NAME. At parse time we may not know the type of the left
337 make_selector(Expression
* left
, const std::string
& name
, Location
);
339 // Make a reference to a field in a struct.
340 static Field_reference_expression
*
341 make_field_reference(Expression
*, unsigned int field_index
, Location
);
343 // Make a reference to a field of an interface, with an associated
346 make_interface_field_reference(Expression
*, const std::string
&,
349 // Make an allocation expression.
351 make_allocation(Type
*, Location
);
353 // Make a type guard expression.
355 make_type_guard(Expression
*, Type
*, Location
);
357 // Make a type cast expression.
359 make_cast(Type
*, Expression
*, Location
);
361 // Make an unsafe type cast expression. This is only used when
362 // passing parameter to builtin functions that are part of the Go
365 make_unsafe_cast(Type
*, Expression
*, Location
);
367 // Make a composite literal. The DEPTH parameter is how far down we
368 // are in a list of composite literals with omitted types. HAS_KEYS
369 // is true if the expression list has keys alternating with values.
370 // ALL_ARE_NAMES is true if all the keys could be struct field
373 make_composite_literal(Type
*, int depth
, bool has_keys
, Expression_list
*,
374 bool all_are_names
, Location
);
376 // Make a struct composite literal.
378 make_struct_composite_literal(Type
*, Expression_list
*, Location
);
380 // Make an array composite literal.
382 make_array_composite_literal(Type
*, Expression_list
*, Location
);
384 // Make a slice composite literal.
385 static Slice_construction_expression
*
386 make_slice_composite_literal(Type
*, Expression_list
*, Location
);
388 // Take an expression and allocate it on the heap.
390 make_heap_expression(Expression
*, Location
);
392 // Make a receive expression. VAL is NULL for a unary receive.
393 static Receive_expression
*
394 make_receive(Expression
* channel
, Location
);
396 // Make an expression which evaluates to the address of the type
397 // descriptor for TYPE.
399 make_type_descriptor(Type
* type
, Location
);
401 // Make an expression which evaluates to the address of the gc
404 make_gc_symbol(Type
* type
);
406 // Make an expression that evaluates to the address of a ptrmask
407 // symbol for TYPE. For most types this will be the same as
408 // make_gc_symbol, but for larger types make_gc_symbol will return a
409 // gcprog while this will return a ptrmask.
411 make_ptrmask_symbol(Type
* type
);
413 // Make an expression which evaluates to some characteristic of a
414 // type. These are only used for type descriptors, so there is no
415 // location parameter.
418 // The size of a value of the type.
420 // The required alignment of a value of the type.
422 // The required alignment of a value of the type when used as a
423 // field in a struct.
424 TYPE_INFO_FIELD_ALIGNMENT
,
425 // The size of the prefix of a value of the type that contains
426 // all the pointers. This is 0 for a type that contains no
427 // pointers. It is always <= TYPE_INFO_SIZE.
428 TYPE_INFO_BACKEND_PTRDATA
,
429 // Like TYPE_INFO_BACKEND_PTRDATA, but the ptrdata value that we
430 // want to store in a type descriptor. They are the same for
431 // most types, but can differ for a type that uses a gcprog.
432 TYPE_INFO_DESCRIPTOR_PTRDATA
436 make_type_info(Type
* type
, Type_info
);
438 // Make an expression that evaluates to some characteristic of a
439 // slice. For simplicity, the enum values must match the field indexes
440 // in the underlying struct.
443 // The underlying data of the slice.
444 SLICE_INFO_VALUE_POINTER
,
445 // The length of the slice.
447 // The capacity of the slice.
452 make_slice_info(Expression
* slice
, Slice_info
, Location
);
454 // Make an expression for a slice value.
456 make_slice_value(Type
*, Expression
* valptr
, Expression
* len
, Expression
* cap
,
459 // Make an expression that evaluates to some characteristic of an
460 // interface. For simplicity, the enum values must match the field indexes
461 // in the underlying struct.
464 // The type descriptor of an empty interface.
465 INTERFACE_INFO_TYPE_DESCRIPTOR
= 0,
466 // The methods of an interface.
467 INTERFACE_INFO_METHODS
= 0,
468 // The first argument to pass to an interface method.
469 INTERFACE_INFO_OBJECT
473 make_interface_info(Expression
* iface
, Interface_info
, Location
);
475 // Make an expression for an interface value.
477 make_interface_value(Type
*, Expression
*, Expression
*, Location
);
479 // Make an expression that builds a reference to the interface method table
480 // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
481 // reference to the interface method table for the pointer receiver type.
483 make_interface_mtable_ref(Interface_type
* itype
, Type
* type
,
484 bool is_pointer
, Location
);
486 // Make an expression which evaluates to the offset of a field in a
487 // struct. This is only used for type descriptors, so there is no
488 // location parameter.
490 make_struct_field_offset(Struct_type
*, const Struct_field
*);
492 // Make an expression which evaluates to the address of an unnamed
495 make_label_addr(Label
*, Location
);
497 // Make a conditional expression.
499 make_conditional(Expression
*, Expression
*, Expression
*, Location
);
501 // Make a compound expression.
503 make_compound(Expression
*, Expression
*, Location
);
505 // Make a backend expression.
507 make_backend(Bexpression
*, Type
*, Location
);
509 // Return the expression classification.
510 Expression_classification
511 classification() const
512 { return this->classification_
; }
514 // Return the location of the expression.
517 { return this->location_
; }
519 // Return whether this is a constant expression.
522 { return this->do_is_constant(); }
524 // Return whether this expression can be used as a static
525 // initializer. This is true for an expression that has only
526 // numbers and pointers to global variables or composite literals
527 // that do not require runtime initialization. It is false if we
528 // must generate code to compute this expression when it is used to
529 // initialize a global variable. This is not a language-level
530 // concept, but an implementation-level one. If this expression is
531 // used to initialize a global variable, this is true if we can pass
532 // an initializer to the backend, false if we must generate code to
533 // initialize the variable. It is always safe for this method to
534 // return false, but the resulting code may be less efficient.
536 is_static_initializer() const
537 { return this->do_is_static_initializer(); }
539 // If this is not a numeric constant, return false. If it is one,
540 // return true, and set VAL to hold the value.
542 numeric_constant_value(Numeric_constant
* val
) const
543 { return this->do_numeric_constant_value(val
); }
545 // If this is not a constant expression with string type, return
546 // false. If it is one, return true, and set VAL to the value.
548 string_constant_value(std::string
* val
) const
549 { return this->do_string_constant_value(val
); }
551 // This is called if the value of this expression is being
552 // discarded. This issues warnings about computed values being
553 // unused. This returns true if all is well, false if it issued an
557 { return this->do_discarding_value(); }
559 // Return whether this is an error expression.
561 is_error_expression() const
562 { return this->classification_
== EXPRESSION_ERROR
; }
564 // Return whether this expression really represents a type.
566 is_type_expression() const
567 { return this->classification_
== EXPRESSION_TYPE
; }
569 // If this is a variable reference, return the Var_expression
570 // structure. Otherwise, return NULL. This is a controlled dynamic
574 { return this->convert
<Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
576 const Var_expression
*
577 var_expression() const
578 { return this->convert
<const Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
580 // If this is a enclosed_variable reference, return the
581 // Enclosed_var_expression structure. Otherwise, return NULL.
582 // This is a controlled dynamic cast.
583 Enclosed_var_expression
*
584 enclosed_var_expression()
585 { return this->convert
<Enclosed_var_expression
,
586 EXPRESSION_ENCLOSED_VAR_REFERENCE
>(); }
588 const Enclosed_var_expression
*
589 enclosed_var_expression() const
590 { return this->convert
<const Enclosed_var_expression
,
591 EXPRESSION_ENCLOSED_VAR_REFERENCE
>(); }
594 // If this is a reference to a temporary variable, return the
595 // Temporary_reference_expression. Otherwise, return NULL.
596 Temporary_reference_expression
*
597 temporary_reference_expression()
599 return this->convert
<Temporary_reference_expression
,
600 EXPRESSION_TEMPORARY_REFERENCE
>();
603 // If this is a set-and-use-temporary, return the
604 // Set_and_use_temporary_expression. Otherwise, return NULL.
605 Set_and_use_temporary_expression
*
606 set_and_use_temporary_expression()
608 return this->convert
<Set_and_use_temporary_expression
,
609 EXPRESSION_SET_AND_USE_TEMPORARY
>();
612 // Return whether this is a sink expression.
614 is_sink_expression() const
615 { return this->classification_
== EXPRESSION_SINK
; }
617 // If this is a string expression, return the String_expression
618 // structure. Otherwise, return NULL.
621 { return this->convert
<String_expression
, EXPRESSION_STRING
>(); }
623 // If this is a conversion expression, return the Type_conversion_expression
624 // structure. Otherwise, return NULL.
625 Type_conversion_expression
*
626 conversion_expression()
627 { return this->convert
<Type_conversion_expression
, EXPRESSION_CONVERSION
>(); }
629 // If this is an unsafe conversion expression, return the
630 // Unsafe_type_conversion_expression structure. Otherwise, return NULL.
631 Unsafe_type_conversion_expression
*
632 unsafe_conversion_expression()
634 return this->convert
<Unsafe_type_conversion_expression
,
635 EXPRESSION_UNSAFE_CONVERSION
>();
638 // Return whether this is the expression nil.
640 is_nil_expression() const
641 { return this->classification_
== EXPRESSION_NIL
; }
643 // If this is an indirection through a pointer, return the
644 // expression being pointed through. Otherwise return this.
648 // If this is a unary expression, return the Unary_expression
649 // structure. Otherwise return NULL.
652 { return this->convert
<Unary_expression
, EXPRESSION_UNARY
>(); }
654 // If this is a binary expression, return the Binary_expression
655 // structure. Otherwise return NULL.
658 { return this->convert
<Binary_expression
, EXPRESSION_BINARY
>(); }
660 // If this is a string concatenation expression, return the
661 // String_concat_expression structure. Otherwise, return NULL.
662 String_concat_expression
*
663 string_concat_expression()
665 return this->convert
<String_concat_expression
, EXPRESSION_STRING_CONCAT
>();
668 // If this is a call expression, return the Call_expression
669 // structure. Otherwise, return NULL. This is a controlled dynamic
673 { return this->convert
<Call_expression
, EXPRESSION_CALL
>(); }
675 // If this is a call_result expression, return the Call_result_expression
676 // structure. Otherwise, return NULL. This is a controlled dynamic
678 Call_result_expression
*
679 call_result_expression()
680 { return this->convert
<Call_result_expression
, EXPRESSION_CALL_RESULT
>(); }
682 // If this is an expression which refers to a function, return the
683 // Func_expression structure. Otherwise, return NULL.
686 { return this->convert
<Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
688 const Func_expression
*
689 func_expression() const
690 { return this->convert
<const Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
692 // If this is an expression which refers to an unknown name, return
693 // the Unknown_expression structure. Otherwise, return NULL.
696 { return this->convert
<Unknown_expression
, EXPRESSION_UNKNOWN_REFERENCE
>(); }
698 const Unknown_expression
*
699 unknown_expression() const
701 return this->convert
<const Unknown_expression
,
702 EXPRESSION_UNKNOWN_REFERENCE
>();
705 // If this is an index expression, return the Index_expression
706 // structure. Otherwise, return NULL.
709 { return this->convert
<Index_expression
, EXPRESSION_INDEX
>(); }
711 // If this is an expression which refers to indexing in a array,
712 // return the Array_index_expression structure. Otherwise, return
714 Array_index_expression
*
715 array_index_expression()
716 { return this->convert
<Array_index_expression
, EXPRESSION_ARRAY_INDEX
>(); }
718 // If this is an expression which refers to indexing in a string,
719 // return the String_index_expression structure. Otherwise, return
721 String_index_expression
*
722 string_index_expression()
723 { return this->convert
<String_index_expression
, EXPRESSION_STRING_INDEX
>(); }
725 // If this is an expression which refers to indexing in a map,
726 // return the Map_index_expression structure. Otherwise, return
728 Map_index_expression
*
729 map_index_expression()
730 { return this->convert
<Map_index_expression
, EXPRESSION_MAP_INDEX
>(); }
732 // If this is a bound method expression, return the
733 // Bound_method_expression structure. Otherwise, return NULL.
734 Bound_method_expression
*
735 bound_method_expression()
736 { return this->convert
<Bound_method_expression
, EXPRESSION_BOUND_METHOD
>(); }
738 // If this is a reference to a field in a struct, return the
739 // Field_reference_expression structure. Otherwise, return NULL.
740 Field_reference_expression
*
741 field_reference_expression()
743 return this->convert
<Field_reference_expression
,
744 EXPRESSION_FIELD_REFERENCE
>();
747 // If this is a reference to a field in an interface, return the
748 // Interface_field_reference_expression structure. Otherwise,
750 Interface_field_reference_expression
*
751 interface_field_reference_expression()
753 return this->convert
<Interface_field_reference_expression
,
754 EXPRESSION_INTERFACE_FIELD_REFERENCE
>();
757 // If this is an allocation expression, return the Allocation_expression
758 // structure. Otherwise, return NULL.
759 Allocation_expression
*
760 allocation_expression()
761 { return this->convert
<Allocation_expression
, EXPRESSION_ALLOCATION
>(); }
763 // If this is a general composite literal, return the
764 // Composite_literal_expression structure. Otherwise, return NULL.
765 Composite_literal_expression
*
768 return this->convert
<Composite_literal_expression
,
769 EXPRESSION_COMPOSITE_LITERAL
>();
772 // If this is a struct composite literal, return the
773 // Struct_construction_expression structure. Otherwise, return NULL.
774 Struct_construction_expression
*
777 return this->convert
<Struct_construction_expression
,
778 EXPRESSION_STRUCT_CONSTRUCTION
>();
781 // If this is a array composite literal, return the
782 // Array_construction_expression structure. Otherwise, return NULL.
783 Fixed_array_construction_expression
*
786 return this->convert
<Fixed_array_construction_expression
,
787 EXPRESSION_FIXED_ARRAY_CONSTRUCTION
>();
790 // If this is a slice composite literal, return the
791 // Slice_construction_expression structure. Otherwise, return NULL.
792 Slice_construction_expression
*
795 return this->convert
<Slice_construction_expression
,
796 EXPRESSION_SLICE_CONSTRUCTION
>();
799 // If this is a map composite literal, return the
800 // Map_construction_expression structure. Otherwise, return NULL.
801 Map_construction_expression
*
804 return this->convert
<Map_construction_expression
,
805 EXPRESSION_MAP_CONSTRUCTION
>();
808 // If this is a type guard expression, return the
809 // Type_guard_expression structure. Otherwise, return NULL.
810 Type_guard_expression
*
811 type_guard_expression()
812 { return this->convert
<Type_guard_expression
, EXPRESSION_TYPE_GUARD
>(); }
814 // If this is a heap expression, returhn the Heap_expression structure.
815 // Otherwise, return NULL.
818 { return this->convert
<Heap_expression
, EXPRESSION_HEAP
>(); }
820 // If this is a receive expression, return the Receive_expression
821 // structure. Otherwise, return NULL.
824 { return this->convert
<Receive_expression
, EXPRESSION_RECEIVE
>(); }
826 // If this is a conditional expression, return the Conditional_expression
827 // structure. Otherwise, return NULL.
828 Conditional_expression
*
829 conditional_expression()
830 { return this->convert
<Conditional_expression
, EXPRESSION_CONDITIONAL
>(); }
832 // If this is a compound expression, return the Compound_expression structure.
833 // Otherwise, return NULL.
835 compound_expression()
836 { return this->convert
<Compound_expression
, EXPRESSION_COMPOUND
>(); }
838 // Return true if this is a composite literal.
840 is_composite_literal() const;
842 // Return true if this is a composite literal which is not constant.
844 is_nonconstant_composite_literal() const;
846 // Return true if this is a variable or temporary variable.
850 // Return true if this is a reference to a local variable.
852 is_local_variable() const;
854 // Make the builtin function descriptor type, so that it can be
857 make_func_descriptor_type();
859 // Traverse an expression.
861 traverse(Expression
**, Traverse
*);
863 // Traverse subexpressions of this expression.
865 traverse_subexpressions(Traverse
*);
867 // Lower an expression. This is called immediately after parsing.
868 // FUNCTION is the function we are in; it will be NULL for an
869 // expression initializing a global variable. INSERTER may be used
870 // to insert statements before the statement or initializer
871 // containing this expression; it is normally used to create
872 // temporary variables. IOTA_VALUE is the value that we should give
873 // to any iota expressions. This function must resolve expressions
874 // which could not be fully parsed into their final form. It
875 // returns the same Expression or a new one.
877 lower(Gogo
* gogo
, Named_object
* function
, Statement_inserter
* inserter
,
879 { return this->do_lower(gogo
, function
, inserter
, iota_value
); }
881 // Flatten an expression. This is called after order_evaluation.
882 // FUNCTION is the function we are in; it will be NULL for an
883 // expression initializing a global variable. INSERTER may be used
884 // to insert statements before the statement or initializer
885 // containing this expression; it is normally used to create
886 // temporary variables. This function must resolve expressions
887 // which could not be fully parsed into their final form. It
888 // returns the same Expression or a new one.
890 flatten(Gogo
* gogo
, Named_object
* function
, Statement_inserter
* inserter
)
891 { return this->do_flatten(gogo
, function
, inserter
); }
893 // Determine the real type of an expression with abstract integer,
894 // floating point, or complex type. TYPE_CONTEXT describes the
897 determine_type(const Type_context
*);
899 // Check types in an expression.
901 check_types(Gogo
* gogo
)
902 { this->do_check_types(gogo
); }
904 // Determine the type when there is no context.
906 determine_type_no_context();
908 // Return the current type of the expression. This may be changed
909 // by determine_type.
912 { return this->do_type(); }
914 // Return a copy of an expression.
917 { return this->do_copy(); }
919 // Return whether the expression is addressable--something which may
920 // be used as the operand of the unary & operator.
922 is_addressable() const
923 { return this->do_is_addressable(); }
925 // Note that we are taking the address of this expression. ESCAPES
926 // is true if this address escapes the current function.
928 address_taken(bool escapes
)
929 { this->do_address_taken(escapes
); }
931 // Note that a nil check must be issued for this expression.
934 { this->do_issue_nil_check(); }
936 // Return whether this expression must be evaluated in order
937 // according to the order of evaluation rules. This is basically
938 // true of all expressions with side-effects.
940 must_eval_in_order() const
941 { return this->do_must_eval_in_order(); }
943 // Return whether subexpressions of this expression must be
944 // evaluated in order. This is true of index expressions and
945 // pointer indirections. This sets *SKIP to the number of
946 // subexpressions to skip during traversing, as index expressions
947 // only requiring moving the index, not the array.
949 must_eval_subexpressions_in_order(int* skip
) const
952 return this->do_must_eval_subexpressions_in_order(skip
);
955 // Return the backend representation for this expression.
957 get_backend(Translate_context
*);
959 // Return an expression handling any conversions which must be done during
962 convert_for_assignment(Gogo
*, Type
* lhs_type
, Expression
* rhs
,
965 // Return an expression converting a value of one interface type to another
966 // interface type. If FOR_TYPE_GUARD is true this is for a type
969 convert_interface_to_interface(Type
* lhs_type
,
970 Expression
* rhs
, bool for_type_guard
,
973 // Return a backend expression implementing the comparison LEFT OP RIGHT.
974 // TYPE is the type of both sides.
976 comparison(Translate_context
*, Type
* result_type
, Operator op
,
977 Expression
* left
, Expression
* right
, Location
);
979 // Return the backend expression for the numeric constant VAL.
981 backend_numeric_constant_expression(Translate_context
*,
982 Numeric_constant
* val
);
984 // Export the expression. This is only used for constants. It will
985 // be used for things like values of named constants and sizes of
988 export_expression(Export
* exp
) const
989 { this->do_export(exp
); }
991 // Import an expression.
993 import_expression(Import
*);
995 // Return an expression which checks that VAL, of arbitrary integer type,
996 // is non-negative and is not more than the maximum integer value.
998 check_bounds(Expression
* val
, Location
);
1000 // Dump an expression to a dump constext.
1002 dump_expression(Ast_dump_context
*) const;
1005 // May be implemented by child class: traverse the expressions.
1007 do_traverse(Traverse
*);
1009 // Return a lowered expression.
1011 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
1014 // Return a flattened expression.
1016 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*)
1020 // Return whether this is a constant expression.
1022 do_is_constant() const
1025 // Return whether this expression can be used as a constant
1028 do_is_static_initializer() const
1031 // Return whether this is a constant expression of numeric type, and
1032 // set the Numeric_constant to the value.
1034 do_numeric_constant_value(Numeric_constant
*) const
1037 // Return whether this is a constant expression of string type, and
1038 // set VAL to the value.
1040 do_string_constant_value(std::string
*) const
1043 // Called by the parser if the value is being discarded.
1045 do_discarding_value();
1047 // Child class holds type.
1051 // Child class implements determining type information.
1053 do_determine_type(const Type_context
*) = 0;
1055 // Child class implements type checking if needed.
1057 do_check_types(Gogo
*)
1060 // Child class implements copying.
1064 // Child class implements whether the expression is addressable.
1066 do_is_addressable() const
1069 // Child class implements taking the address of an expression.
1071 do_address_taken(bool)
1074 // Child class implements issuing a nil check if the address is taken.
1076 do_issue_nil_check()
1079 // Child class implements whether this expression must be evaluated
1082 do_must_eval_in_order() const
1085 // Child class implements whether this expressions requires that
1086 // subexpressions be evaluated in order. The child implementation
1087 // may set *SKIP if it should be non-zero.
1089 do_must_eval_subexpressions_in_order(int* /* skip */) const
1092 // Child class implements conversion to backend representation.
1093 virtual Bexpression
*
1094 do_get_backend(Translate_context
*) = 0;
1096 // Child class implements export.
1098 do_export(Export
*) const;
1100 // For children to call to give an error for an unused value.
1102 unused_value_error();
1104 // For children to call when they detect that they are in error.
1108 // For children to call to report an error conveniently.
1110 report_error(const char*);
1112 // Child class implements dumping to a dump context.
1114 do_dump_expression(Ast_dump_context
*) const = 0;
1116 // Varargs lowering creates a slice object (unnamed compiler temp)
1117 // to contain the variable length collection of values. The enum
1118 // below tells the lowering routine whether it can mark that temp
1119 // as non-escaping or not. For general varargs calls it is not always
1120 // safe to stack-allocated the storage, but for specific cases (ex:
1121 // call to append()) it is legal.
1122 enum Slice_storage_escape_disp
1124 SLICE_STORAGE_MAY_ESCAPE
,
1125 SLICE_STORAGE_DOES_NOT_ESCAPE
1129 // Convert to the desired statement classification, or return NULL.
1130 // This is a controlled dynamic cast.
1131 template<typename Expression_class
,
1132 Expression_classification expr_classification
>
1136 return (this->classification_
== expr_classification
1137 ? static_cast<Expression_class
*>(this)
1141 template<typename Expression_class
,
1142 Expression_classification expr_classification
>
1143 const Expression_class
*
1146 return (this->classification_
== expr_classification
1147 ? static_cast<const Expression_class
*>(this)
1152 convert_type_to_interface(Type
*, Expression
*, Location
);
1155 get_interface_type_descriptor(Expression
*);
1158 convert_interface_to_type(Type
*, Expression
*, Location
);
1160 // The expression classification.
1161 Expression_classification classification_
;
1162 // The location in the input file.
1166 // A list of Expressions.
1168 class Expression_list
1175 // Return whether the list is empty.
1178 { return this->entries_
.empty(); }
1180 // Return the number of entries in the list.
1183 { return this->entries_
.size(); }
1185 // Add an entry to the end of the list.
1187 push_back(Expression
* expr
)
1188 { this->entries_
.push_back(expr
); }
1191 append(Expression_list
* add
)
1192 { this->entries_
.insert(this->entries_
.end(), add
->begin(), add
->end()); }
1194 // Reserve space in the list.
1196 reserve(size_t size
)
1197 { this->entries_
.reserve(size
); }
1199 // Traverse the expressions in the list.
1201 traverse(Traverse
*);
1207 // Return true if the list contains an error expression.
1209 contains_error() const;
1211 // Retrieve an element by index.
1214 { return this->entries_
.at(i
); }
1216 // Return the first and last elements.
1219 { return this->entries_
.front(); }
1223 { return this->entries_
.front(); }
1227 { return this->entries_
.back(); }
1231 { return this->entries_
.back(); }
1235 typedef std::vector
<Expression
*>::iterator iterator
;
1236 typedef std::vector
<Expression
*>::const_iterator const_iterator
;
1240 { return this->entries_
.begin(); }
1244 { return this->entries_
.begin(); }
1248 { return this->entries_
.end(); }
1252 { return this->entries_
.end(); }
1257 { this->entries_
.erase(p
); }
1260 std::vector
<Expression
*> entries_
;
1263 // An abstract base class for an expression which is only used by the
1264 // parser, and is lowered in the lowering pass.
1266 class Parser_expression
: public Expression
1269 Parser_expression(Expression_classification classification
,
1271 : Expression(classification
, location
)
1276 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int) = 0;
1282 do_determine_type(const Type_context
*)
1283 { go_unreachable(); }
1286 do_check_types(Gogo
*)
1287 { go_unreachable(); }
1290 do_get_backend(Translate_context
*)
1291 { go_unreachable(); }
1294 // An expression which is simply a variable.
1296 class Var_expression
: public Expression
1299 Var_expression(Named_object
* variable
, Location location
)
1300 : Expression(EXPRESSION_VAR_REFERENCE
, location
),
1301 variable_(variable
), in_lvalue_pos_(VE_rvalue
)
1304 // Return the variable.
1306 named_object() const
1307 { return this->variable_
; }
1309 // Does this var expression appear in an lvalue (assigned-to) context?
1311 in_lvalue_pos() const
1312 { return this->in_lvalue_pos_
== VE_lvalue
; }
1314 // Mark a var_expression as appearing in an lvalue context.
1317 { this->in_lvalue_pos_
= VE_lvalue
; }
1321 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1327 do_determine_type(const Type_context
*);
1334 do_is_addressable() const
1338 do_address_taken(bool);
1341 do_get_backend(Translate_context
*);
1344 do_dump_expression(Ast_dump_context
*) const;
1347 // The variable we are referencing.
1348 Named_object
* variable_
;
1349 // Set to TRUE if var expression appears in lvalue context
1350 Varexpr_context in_lvalue_pos_
;
1353 // A reference to a variable within an enclosing function.
1355 class Enclosed_var_expression
: public Expression
1358 Enclosed_var_expression(Expression
* reference
, Named_object
* variable
,
1360 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE
, location
),
1361 reference_(reference
), variable_(variable
)
1364 // The reference to the enclosed variable. This will be an indirection of the
1365 // the field stored within closure variable.
1368 { return this->reference_
; }
1370 // The variable being enclosed and referenced.
1373 { return this->variable_
; }
1377 do_traverse(Traverse
*);
1380 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1383 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1387 { return this->reference_
->type(); }
1390 do_determine_type(const Type_context
* context
)
1391 { return this->reference_
->determine_type(context
); }
1398 do_is_addressable() const
1399 { return this->reference_
->is_addressable(); }
1402 do_address_taken(bool escapes
);
1405 do_get_backend(Translate_context
* context
)
1406 { return this->reference_
->get_backend(context
); }
1409 do_dump_expression(Ast_dump_context
*) const;
1412 // The reference to the enclosed variable.
1413 Expression
* reference_
;
1414 // The variable being enclosed.
1415 Named_object
* variable_
;
1418 // A reference to a temporary variable.
1420 class Temporary_reference_expression
: public Expression
1423 Temporary_reference_expression(Temporary_statement
* statement
,
1425 : Expression(EXPRESSION_TEMPORARY_REFERENCE
, location
),
1426 statement_(statement
), is_lvalue_(false)
1429 // The temporary that this expression refers to.
1430 Temporary_statement
*
1432 { return this->statement_
; }
1434 // Indicate that this reference appears on the left hand side of an
1435 // assignment statement.
1438 { this->is_lvalue_
= true; }
1445 do_determine_type(const Type_context
*)
1450 { return make_temporary_reference(this->statement_
, this->location()); }
1453 do_is_addressable() const
1457 do_address_taken(bool);
1460 do_get_backend(Translate_context
*);
1463 do_dump_expression(Ast_dump_context
*) const;
1466 // The statement where the temporary variable is defined.
1467 Temporary_statement
* statement_
;
1468 // Whether this reference appears on the left hand side of an
1469 // assignment statement.
1473 // Set and use a temporary variable.
1475 class Set_and_use_temporary_expression
: public Expression
1478 Set_and_use_temporary_expression(Temporary_statement
* statement
,
1479 Expression
* expr
, Location location
)
1480 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY
, location
),
1481 statement_(statement
), expr_(expr
)
1484 // Return the temporary.
1485 Temporary_statement
*
1487 { return this->statement_
; }
1489 // Return the expression.
1492 { return this->expr_
; }
1496 do_traverse(Traverse
* traverse
)
1497 { return Expression::traverse(&this->expr_
, traverse
); }
1503 do_determine_type(const Type_context
*);
1508 return make_set_and_use_temporary(this->statement_
, this->expr_
,
1513 do_is_addressable() const
1517 do_address_taken(bool);
1520 do_get_backend(Translate_context
*);
1523 do_dump_expression(Ast_dump_context
*) const;
1526 // The statement where the temporary variable is defined.
1527 Temporary_statement
* statement_
;
1528 // The expression to assign to the temporary.
1532 // A string expression.
1534 class String_expression
: public Expression
1537 String_expression(const std::string
& val
, Location location
)
1538 : Expression(EXPRESSION_STRING
, location
),
1539 val_(val
), type_(NULL
)
1544 { return this->val_
; }
1551 do_is_constant() const
1555 do_is_static_initializer() const
1559 do_string_constant_value(std::string
* val
) const
1569 do_determine_type(const Type_context
*);
1576 do_get_backend(Translate_context
*);
1578 // Write string literal to a string dump.
1580 export_string(String_dump
* exp
, const String_expression
* str
);
1583 do_export(Export
*) const;
1586 do_dump_expression(Ast_dump_context
*) const;
1589 // The string value. This is immutable.
1590 const std::string val_
;
1591 // The type as determined by context.
1595 // A type conversion expression.
1597 class Type_conversion_expression
: public Expression
1600 Type_conversion_expression(Type
* type
, Expression
* expr
,
1602 : Expression(EXPRESSION_CONVERSION
, location
),
1603 type_(type
), expr_(expr
), may_convert_function_types_(false)
1606 // Return the type to which we are converting.
1609 { return this->type_
; }
1611 // Return the expression which we are converting.
1614 { return this->expr_
; }
1616 // Permit converting from one function type to another. This is
1617 // used internally for method expressions.
1619 set_may_convert_function_types()
1621 this->may_convert_function_types_
= true;
1624 // Import a type conversion expression.
1630 do_traverse(Traverse
* traverse
);
1633 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1636 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1639 do_is_constant() const;
1642 do_is_static_initializer() const;
1645 do_numeric_constant_value(Numeric_constant
*) const;
1648 do_string_constant_value(std::string
*) const;
1652 { return this->type_
; }
1655 do_determine_type(const Type_context
*);
1658 do_check_types(Gogo
*);
1663 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
1668 do_get_backend(Translate_context
* context
);
1671 do_export(Export
*) const;
1674 do_dump_expression(Ast_dump_context
*) const;
1677 // The type to convert to.
1679 // The expression to convert.
1681 // True if this is permitted to convert function types. This is
1682 // used internally for method expressions.
1683 bool may_convert_function_types_
;
1686 // An unsafe type conversion, used to pass values to builtin functions.
1688 class Unsafe_type_conversion_expression
: public Expression
1691 Unsafe_type_conversion_expression(Type
* type
, Expression
* expr
,
1693 : Expression(EXPRESSION_UNSAFE_CONVERSION
, location
),
1694 type_(type
), expr_(expr
)
1699 { return this->expr_
; }
1703 do_traverse(Traverse
* traverse
);
1706 do_is_static_initializer() const;
1710 { return this->type_
; }
1713 do_determine_type(const Type_context
*)
1714 { this->expr_
->determine_type_no_context(); }
1719 return new Unsafe_type_conversion_expression(this->type_
,
1720 this->expr_
->copy(),
1725 do_get_backend(Translate_context
*);
1728 do_dump_expression(Ast_dump_context
*) const;
1731 // The type to convert to.
1733 // The expression to convert.
1737 // A Unary expression.
1739 class Unary_expression
: public Expression
1742 Unary_expression(Operator op
, Expression
* expr
, Location location
)
1743 : Expression(EXPRESSION_UNARY
, location
),
1744 op_(op
), escapes_(true), create_temp_(false), is_gc_root_(false),
1745 is_slice_init_(false), expr_(expr
), issue_nil_check_(false)
1748 // Return the operator.
1751 { return this->op_
; }
1753 // Return the operand.
1756 { return this->expr_
; }
1758 // Record that an address expression does not escape.
1760 set_does_not_escape()
1762 go_assert(this->op_
== OPERATOR_AND
);
1763 this->escapes_
= false;
1766 // Record that this is an address expression which should create a
1767 // temporary variable if necessary. This is used for method calls.
1771 go_assert(this->op_
== OPERATOR_AND
);
1772 this->create_temp_
= true;
1775 // Record that this is an address expression of a GC root, which is a
1776 // mutable composite literal. This used for registering GC variables.
1780 go_assert(this->op_
== OPERATOR_AND
);
1781 this->is_gc_root_
= true;
1784 // Record that this is an address expression of a slice value initializer,
1785 // which is mutable if the values are not copied to the heap.
1789 go_assert(this->op_
== OPERATOR_AND
);
1790 this->is_slice_init_
= true;
1793 // Call the address_taken method on the operand if necessary.
1795 check_operand_address_taken(Gogo
*);
1797 // Apply unary opcode OP to UNC, setting NC. Return true if this
1798 // could be done, false if not. On overflow, issues an error and
1799 // sets *ISSUED_ERROR.
1801 eval_constant(Operator op
, const Numeric_constant
* unc
,
1802 Location
, Numeric_constant
* nc
, bool *issued_error
);
1809 do_traverse(Traverse
* traverse
)
1810 { return Expression::traverse(&this->expr_
, traverse
); }
1813 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1816 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1819 do_is_constant() const;
1822 do_is_static_initializer() const;
1825 do_numeric_constant_value(Numeric_constant
*) const;
1831 do_determine_type(const Type_context
*);
1834 do_check_types(Gogo
*);
1839 return Expression::make_unary(this->op_
, this->expr_
->copy(),
1844 do_must_eval_subexpressions_in_order(int*) const
1845 { return this->op_
== OPERATOR_MULT
; }
1848 do_is_addressable() const
1849 { return this->op_
== OPERATOR_MULT
; }
1852 do_get_backend(Translate_context
*);
1855 do_export(Export
*) const;
1858 do_dump_expression(Ast_dump_context
*) const;
1861 do_issue_nil_check()
1862 { this->issue_nil_check_
= (this->op_
== OPERATOR_MULT
); }
1866 base_is_static_initializer(Expression
*);
1868 // The unary operator to apply.
1870 // Normally true. False if this is an address expression which does
1871 // not escape the current function.
1873 // True if this is an address expression which should create a
1874 // temporary variable if necessary.
1876 // True if this is an address expression for a GC root. A GC root is a
1877 // special struct composite literal that is mutable when addressed, meaning
1878 // it cannot be represented as an immutable_struct in the backend.
1880 // True if this is an address expression for a slice value with an immutable
1881 // initializer. The initializer for a slice's value pointer has an array
1882 // type, meaning it cannot be represented as an immutable_struct in the
1884 bool is_slice_init_
;
1887 // Whether or not to issue a nil check for this expression if its address
1889 bool issue_nil_check_
;
1892 // A binary expression.
1894 class Binary_expression
: public Expression
1897 Binary_expression(Operator op
, Expression
* left
, Expression
* right
,
1899 : Expression(EXPRESSION_BINARY
, location
),
1900 op_(op
), left_(left
), right_(right
), type_(NULL
)
1903 // Return the operator.
1906 { return this->op_
; }
1908 // Return the left hand expression.
1911 { return this->left_
; }
1913 // Return the right hand expression.
1916 { return this->right_
; }
1918 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1919 // Return true if this could be done, false if not. Issue errors at
1920 // LOCATION as appropriate, and sets *ISSUED_ERROR if it did.
1922 eval_constant(Operator op
, Numeric_constant
* left_nc
,
1923 Numeric_constant
* right_nc
, Location location
,
1924 Numeric_constant
* nc
, bool* issued_error
);
1926 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1927 // *RESULT. Return true if this could be done, false if not. Issue
1928 // errors at LOCATION as appropriate.
1930 compare_constant(Operator op
, Numeric_constant
* left_nc
,
1931 Numeric_constant
* right_nc
, Location location
,
1937 // Report an error if OP can not be applied to TYPE. Return whether
1938 // it can. OTYPE is the type of the other operand.
1940 check_operator_type(Operator op
, Type
* type
, Type
* otype
, Location
);
1942 // Set *RESULT_TYPE to the resulting type when OP is applied to
1943 // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
1944 // success, false on failure.
1946 operation_type(Operator op
, Type
* left_type
, Type
* right_type
,
1947 Type
** result_type
);
1951 do_traverse(Traverse
* traverse
);
1954 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1957 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1960 do_is_constant() const
1961 { return this->left_
->is_constant() && this->right_
->is_constant(); }
1964 do_is_static_initializer() const;
1967 do_numeric_constant_value(Numeric_constant
*) const;
1970 do_discarding_value();
1976 do_determine_type(const Type_context
*);
1979 do_check_types(Gogo
*);
1984 return Expression::make_binary(this->op_
, this->left_
->copy(),
1985 this->right_
->copy(), this->location());
1989 do_get_backend(Translate_context
*);
1992 do_export(Export
*) const;
1995 do_dump_expression(Ast_dump_context
*) const;
1999 cmp_to_bool(Operator op
, int cmp
);
2002 eval_integer(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
2003 Location
, Numeric_constant
*);
2006 eval_float(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
2007 Location
, Numeric_constant
*);
2010 eval_complex(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
2011 Location
, Numeric_constant
*);
2014 compare_integer(const Numeric_constant
*, const Numeric_constant
*, int*);
2017 compare_float(const Numeric_constant
*, const Numeric_constant
*, int*);
2020 compare_complex(const Numeric_constant
*, const Numeric_constant
*, int*);
2023 lower_struct_comparison(Gogo
*, Statement_inserter
*);
2026 lower_array_comparison(Gogo
*, Statement_inserter
*);
2029 lower_interface_value_comparison(Gogo
*, Statement_inserter
*);
2032 lower_compare_to_memcmp(Gogo
*, Statement_inserter
*);
2035 operand_address(Statement_inserter
*, Expression
*);
2037 // The binary operator to apply.
2039 // The left hand side operand.
2041 // The right hand side operand.
2043 // The type of a comparison operation.
2047 // A string concatenation expression. This is a sequence of strings
2048 // added together. It is created when lowering Binary_expression.
2050 class String_concat_expression
: public Expression
2053 String_concat_expression(Expression_list
* exprs
)
2054 : Expression(EXPRESSION_STRING_CONCAT
, exprs
->front()->location()),
2058 // Return the list of string expressions to be concatenated.
2061 { return this->exprs_
; }
2065 do_traverse(Traverse
* traverse
)
2066 { return this->exprs_
->traverse(traverse
); }
2069 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
2073 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2076 do_is_constant() const;
2079 do_is_static_initializer() const;
2085 do_determine_type(const Type_context
*);
2088 do_check_types(Gogo
*);
2092 { return Expression::make_string_concat(this->exprs_
->copy()); }
2095 do_get_backend(Translate_context
*)
2096 { go_unreachable(); }
2099 do_export(Export
*) const
2100 { go_unreachable(); }
2103 do_dump_expression(Ast_dump_context
*) const;
2106 // The string expressions to concatenate.
2107 Expression_list
* exprs_
;
2110 // A call expression. The go statement needs to dig inside this.
2112 class Call_expression
: public Expression
2115 Call_expression(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
2117 : Expression(EXPRESSION_CALL
, location
),
2118 fn_(fn
), args_(args
), type_(NULL
), call_(NULL
), call_temp_(NULL
)
2119 , expected_result_count_(0), is_varargs_(is_varargs
),
2120 varargs_are_lowered_(false), types_are_determined_(false),
2121 is_deferred_(false), is_concurrent_(false), issued_error_(false),
2122 is_multi_value_arg_(false), is_flattened_(false)
2125 // The function to call.
2128 { return this->fn_
; }
2133 { return this->args_
; }
2135 const Expression_list
*
2137 { return this->args_
; }
2139 // Get the function type.
2141 get_function_type() const;
2143 // Return the number of values this call will return.
2145 result_count() const;
2147 // Return the temporary variable that holds the results. This is
2148 // only valid after the expression has been lowered, and is only
2149 // valid for calls which return multiple results.
2150 Temporary_statement
*
2153 // Set the number of results expected from this call. This is used
2154 // when the call appears in a context that expects multiple results,
2155 // such as a, b = f().
2157 set_expected_result_count(size_t);
2159 // Return whether this is a call to the predeclared function
2162 is_recover_call() const;
2164 // Set the argument for a call to recover.
2166 set_recover_arg(Expression
*);
2168 // Whether the last argument is a varargs argument (f(a...)).
2171 { return this->is_varargs_
; }
2173 // Return whether varargs have already been lowered.
2175 varargs_are_lowered() const
2176 { return this->varargs_are_lowered_
; }
2178 // Note that varargs have already been lowered.
2180 set_varargs_are_lowered()
2181 { this->varargs_are_lowered_
= true; }
2183 // Whether this call is being deferred.
2186 { return this->is_deferred_
; }
2188 // Note that the call is being deferred.
2191 { this->is_deferred_
= true; }
2193 // Whether this call is concurrently executed.
2195 is_concurrent() const
2196 { return this->is_concurrent_
; }
2198 // Note that the call is concurrently executed.
2201 { this->is_concurrent_
= true; }
2203 // We have found an error with this call expression; return true if
2204 // we should report it.
2208 // Whether or not this call contains errors, either in the call or the
2209 // arguments to the call.
2211 is_erroneous_call();
2213 // Whether this call returns multiple results that are used as an
2214 // multi-valued argument.
2216 is_multi_value_arg() const
2217 { return this->is_multi_value_arg_
; }
2219 // Note this call is used as a multi-valued argument.
2221 set_is_multi_value_arg()
2222 { this->is_multi_value_arg_
= true; }
2226 do_traverse(Traverse
*);
2229 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2232 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2235 do_discarding_value()
2242 do_determine_type(const Type_context
*);
2245 do_check_types(Gogo
*);
2251 do_must_eval_in_order() const;
2253 virtual Bexpression
*
2254 do_get_backend(Translate_context
*);
2257 do_is_recover_call() const;
2260 do_set_recover_arg(Expression
*);
2262 // Let a builtin expression change the argument list.
2264 set_args(Expression_list
* args
)
2265 { this->args_
= args
; }
2267 // Let a builtin expression lower varargs.
2269 lower_varargs(Gogo
*, Named_object
* function
, Statement_inserter
* inserter
,
2270 Type
* varargs_type
, size_t param_count
,
2271 Slice_storage_escape_disp escape_disp
);
2273 // Let a builtin expression check whether types have been
2276 determining_types();
2279 do_dump_expression(Ast_dump_context
*) const;
2283 check_argument_type(int, const Type
*, const Type
*, Location
, bool);
2286 lower_to_builtin(Named_object
**, const char*, int);
2289 interface_method_function(Interface_field_reference_expression
*,
2290 Expression
**, Location
);
2293 set_results(Translate_context
*);
2295 // The function to call.
2297 // The arguments to pass. This may be NULL if there are no
2299 Expression_list
* args_
;
2300 // The type of the expression, to avoid recomputing it.
2302 // The backend expression for the call, used for a call which returns a tuple.
2304 // A temporary variable to store this call if the function returns a tuple.
2305 Temporary_statement
* call_temp_
;
2306 // If not 0, the number of results expected from this call, when
2307 // used in a context that expects multiple values.
2308 size_t expected_result_count_
;
2309 // True if the last argument is a varargs argument (f(a...)).
2311 // True if varargs have already been lowered.
2312 bool varargs_are_lowered_
;
2313 // True if types have been determined.
2314 bool types_are_determined_
;
2315 // True if the call is an argument to a defer statement.
2317 // True if the call is an argument to a go statement.
2318 bool is_concurrent_
;
2319 // True if we reported an error about a mismatch between call
2320 // results and uses. This is to avoid producing multiple errors
2321 // when there are multiple Call_result_expressions.
2323 // True if this call is used as an argument that returns multiple results.
2324 bool is_multi_value_arg_
;
2325 // True if this expression has already been flattened.
2329 // A single result from a call which returns multiple results.
2331 class Call_result_expression
: public Expression
2334 Call_result_expression(Call_expression
* call
, unsigned int index
)
2335 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
2336 call_(call
), index_(index
)
2341 { return this->call_
; }
2345 { return this->index_
; }
2349 do_traverse(Traverse
*);
2355 do_determine_type(const Type_context
*);
2358 do_check_types(Gogo
*);
2363 return new Call_result_expression(this->call_
->call_expression(),
2368 do_must_eval_in_order() const
2372 do_get_backend(Translate_context
*);
2375 do_dump_expression(Ast_dump_context
*) const;
2378 // The underlying call expression.
2380 // Which result we want.
2381 unsigned int index_
;
2384 // An expression which represents a pointer to a function.
2386 class Func_expression
: public Expression
2389 Func_expression(Named_object
* function
, Expression
* closure
,
2391 : Expression(EXPRESSION_FUNC_REFERENCE
, location
),
2392 function_(function
), closure_(closure
),
2393 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS
)
2396 // Return the object associated with the function.
2398 named_object() const
2399 { return this->function_
; }
2401 // Return the closure for this function. This will return NULL if
2402 // the function has no closure, which is the normal case.
2405 { return this->closure_
; }
2407 // Return whether this is a reference to a runtime function.
2409 is_runtime_function() const
2410 { return this->runtime_code_
!= Runtime::NUMBER_OF_FUNCTIONS
; }
2412 // Return the runtime code for this function expression.
2413 // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
2414 // runtime function.
2416 runtime_code() const
2417 { return this->runtime_code_
; }
2419 // Set the runtime code for this function expression.
2421 set_runtime_code(Runtime::Function code
)
2422 { this->runtime_code_
= code
; }
2424 // Return a backend expression for the code of a function.
2426 get_code_pointer(Gogo
*, Named_object
* function
, Location loc
);
2430 do_traverse(Traverse
*);
2436 do_determine_type(const Type_context
*)
2438 if (this->closure_
!= NULL
)
2439 this->closure_
->determine_type_no_context();
2445 return Expression::make_func_reference(this->function_
,
2446 (this->closure_
== NULL
2448 : this->closure_
->copy()),
2453 do_get_backend(Translate_context
*);
2456 do_dump_expression(Ast_dump_context
*) const;
2459 // The function itself.
2460 Named_object
* function_
;
2461 // A closure. This is normally NULL. For a nested function, it may
2462 // be a struct holding pointers to all the variables referenced by
2463 // this function and defined in enclosing functions.
2464 Expression
* closure_
;
2465 // The runtime code for the referenced function.
2466 Runtime::Function runtime_code_
;
2469 // A function descriptor. A function descriptor is a struct with a
2470 // single field pointing to the function code. This is used for
2471 // functions without closures.
2473 class Func_descriptor_expression
: public Expression
2476 Func_descriptor_expression(Named_object
* fn
);
2478 // Make the function descriptor type, so that it can be converted.
2480 make_func_descriptor_type();
2484 do_traverse(Traverse
*);
2490 do_determine_type(const Type_context
*)
2495 { return Expression::make_func_descriptor(this->fn_
); }
2498 do_is_addressable() const
2502 do_get_backend(Translate_context
*);
2505 do_dump_expression(Ast_dump_context
* context
) const;
2508 // The type of all function descriptors.
2509 static Type
* descriptor_type
;
2511 // The function for which this is the descriptor.
2513 // The descriptor variable.
2517 // A reference to an unknown name.
2519 class Unknown_expression
: public Parser_expression
2522 Unknown_expression(Named_object
* named_object
, Location location
)
2523 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE
, location
),
2524 named_object_(named_object
), no_error_message_(false),
2525 is_composite_literal_key_(false)
2528 // The associated named object.
2530 named_object() const
2531 { return this->named_object_
; }
2533 // The name of the identifier which was unknown.
2537 // Call this to indicate that we should not give an error if this
2538 // name is never defined. This is used to avoid knock-on errors
2539 // during an erroneous parse.
2541 set_no_error_message()
2542 { this->no_error_message_
= true; }
2544 // Note that this expression is being used as the key in a composite
2545 // literal, so it may be OK if it is not resolved.
2547 set_is_composite_literal_key()
2548 { this->is_composite_literal_key_
= true; }
2550 // Note that this expression should no longer be treated as a
2551 // composite literal key.
2553 clear_is_composite_literal_key()
2554 { this->is_composite_literal_key_
= false; }
2558 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2562 { return new Unknown_expression(this->named_object_
, this->location()); }
2565 do_dump_expression(Ast_dump_context
*) const;
2568 // The unknown name.
2569 Named_object
* named_object_
;
2570 // True if we should not give errors if this is undefined. This is
2571 // used if there was a parse failure.
2572 bool no_error_message_
;
2573 // True if this is the key in a composite literal.
2574 bool is_composite_literal_key_
;
2577 // An index expression. This is lowered to an array index, a string
2578 // index, or a map index.
2580 class Index_expression
: public Parser_expression
2583 Index_expression(Expression
* left
, Expression
* start
, Expression
* end
,
2584 Expression
* cap
, Location location
)
2585 : Parser_expression(EXPRESSION_INDEX
, location
),
2586 left_(left
), start_(start
), end_(end
), cap_(cap
)
2589 // Dump an index expression, i.e. an expression of the form
2590 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
2592 dump_index_expression(Ast_dump_context
*, const Expression
* expr
,
2593 const Expression
* start
, const Expression
* end
,
2594 const Expression
* cap
);
2598 do_traverse(Traverse
*);
2601 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2606 return new Index_expression(this->left_
->copy(), this->start_
->copy(),
2609 : this->end_
->copy()),
2612 : this->cap_
->copy()),
2617 do_must_eval_subexpressions_in_order(int* skip
) const
2624 do_dump_expression(Ast_dump_context
*) const;
2627 do_issue_nil_check()
2628 { this->left_
->issue_nil_check(); }
2630 // The expression being indexed.
2634 // The second index. This is NULL for an index, non-NULL for a
2637 // The capacity argument. This is NULL for indices and slices that use the
2638 // default capacity, non-NULL for indices and slices that specify the
2643 // An array index. This is used for both indexing and slicing.
2645 class Array_index_expression
: public Expression
2648 Array_index_expression(Expression
* array
, Expression
* start
,
2649 Expression
* end
, Expression
* cap
, Location location
)
2650 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
2651 array_(array
), start_(start
), end_(end
), cap_(cap
), type_(NULL
),
2655 // Return the array.
2658 { return this->array_
; }
2662 { return this->array_
; }
2664 // Return the index of a simple index expression, or the start index
2665 // of a slice expression.
2668 { return this->start_
; }
2672 { return this->start_
; }
2674 // Return the end index of a slice expression. This is NULL for a
2675 // simple index expression.
2678 { return this->end_
; }
2682 { return this->end_
; }
2684 // Return whether this array index expression appears in an lvalue
2685 // (left hand side of assignment) context.
2688 { return this->is_lvalue_
; }
2690 // Update this array index expression to indicate that it appears
2691 // in a left-hand-side or lvalue context.
2694 { this->is_lvalue_
= true; }
2698 do_traverse(Traverse
*);
2701 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2707 do_determine_type(const Type_context
*);
2710 do_check_types(Gogo
*);
2715 return Expression::make_array_index(this->array_
->copy(),
2716 this->start_
->copy(),
2719 : this->end_
->copy()),
2722 : this->cap_
->copy()),
2727 do_must_eval_subexpressions_in_order(int* skip
) const
2734 do_is_addressable() const;
2737 do_address_taken(bool escapes
)
2738 { this->array_
->address_taken(escapes
); }
2741 do_issue_nil_check()
2742 { this->array_
->issue_nil_check(); }
2745 do_get_backend(Translate_context
*);
2748 do_dump_expression(Ast_dump_context
*) const;
2751 // The array we are getting a value from.
2753 // The start or only index.
2755 // The end index of a slice. This may be NULL for a simple array
2756 // index, or it may be a nil expression for the length of the array.
2758 // The capacity argument of a slice. This may be NULL for an array index or
2761 // The type of the expression.
2763 // Whether expr appears in an lvalue context.
2767 // A string index. This is used for both indexing and slicing.
2769 class String_index_expression
: public Expression
2772 String_index_expression(Expression
* string
, Expression
* start
,
2773 Expression
* end
, Location location
)
2774 : Expression(EXPRESSION_STRING_INDEX
, location
),
2775 string_(string
), start_(start
), end_(end
)
2778 // Return the string being indexed.
2781 { return this->string_
; }
2785 do_traverse(Traverse
*);
2788 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2794 do_determine_type(const Type_context
*);
2797 do_check_types(Gogo
*);
2802 return Expression::make_string_index(this->string_
->copy(),
2803 this->start_
->copy(),
2806 : this->end_
->copy()),
2811 do_must_eval_subexpressions_in_order(int* skip
) const
2818 do_get_backend(Translate_context
*);
2821 do_dump_expression(Ast_dump_context
*) const;
2824 // The string we are getting a value from.
2825 Expression
* string_
;
2826 // The start or only index.
2828 // The end index of a slice. This may be NULL for a single index,
2829 // or it may be a nil expression for the length of the string.
2833 // An index into a map.
2835 class Map_index_expression
: public Expression
2838 Map_index_expression(Expression
* map
, Expression
* index
,
2840 : Expression(EXPRESSION_MAP_INDEX
, location
),
2841 map_(map
), index_(index
), value_pointer_(NULL
)
2847 { return this->map_
; }
2851 { return this->map_
; }
2853 // Return the index.
2856 { return this->index_
; }
2860 { return this->index_
; }
2862 // Get the type of the map being indexed.
2864 get_map_type() const;
2866 // Return an expression for the map index. This returns an
2867 // expression that evaluates to a pointer to a value in the map. If
2868 // the key is not present in the map, this will return a pointer to
2871 get_value_pointer(Gogo
*);
2875 do_traverse(Traverse
*);
2878 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2884 do_determine_type(const Type_context
*);
2887 do_check_types(Gogo
*);
2892 return Expression::make_map_index(this->map_
->copy(),
2893 this->index_
->copy(),
2898 do_must_eval_subexpressions_in_order(int* skip
) const
2904 // A map index expression is an lvalue but it is not addressable.
2907 do_get_backend(Translate_context
*);
2910 do_dump_expression(Ast_dump_context
*) const;
2913 // The map we are looking into.
2917 // A pointer to the value at this index.
2918 Expression
* value_pointer_
;
2921 // An expression which represents a method bound to its first
2924 class Bound_method_expression
: public Expression
2927 Bound_method_expression(Expression
* expr
, const Method
*method
,
2928 Named_object
* function
, Location location
)
2929 : Expression(EXPRESSION_BOUND_METHOD
, location
),
2930 expr_(expr
), expr_type_(NULL
), method_(method
), function_(function
)
2933 // Return the object which is the first argument.
2936 { return this->expr_
; }
2938 // Return the implicit type of the first argument. This will be
2939 // non-NULL when using a method from an anonymous field without
2940 // using an explicit stub.
2942 first_argument_type() const
2943 { return this->expr_type_
; }
2945 // Return the method.
2948 { return this->method_
; }
2950 // Return the function to call.
2953 { return this->function_
; }
2955 // Set the implicit type of the expression.
2957 set_first_argument_type(Type
* type
)
2958 { this->expr_type_
= type
; }
2960 // Create a thunk to call FUNCTION, for METHOD, when it is used as
2961 // part of a method value.
2962 static Named_object
*
2963 create_thunk(Gogo
*, const Method
* method
, Named_object
* function
);
2967 do_traverse(Traverse
*);
2970 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2976 do_determine_type(const Type_context
*);
2979 do_check_types(Gogo
*);
2984 return new Bound_method_expression(this->expr_
->copy(), this->method_
,
2985 this->function_
, this->location());
2989 do_get_backend(Translate_context
*)
2990 { go_unreachable(); }
2993 do_dump_expression(Ast_dump_context
*) const;
2996 // A mapping from method functions to the thunks we have created for
2998 typedef Unordered_map(Named_object
*, Named_object
*) Method_value_thunks
;
2999 static Method_value_thunks method_value_thunks
;
3001 // The object used to find the method. This is passed to the method
3002 // as the first argument.
3004 // The implicit type of the object to pass to the method. This is
3005 // NULL in the normal case, non-NULL when using a method from an
3006 // anonymous field which does not require a stub.
3009 const Method
* method_
;
3010 // The function to call. This is not the same as
3011 // method_->named_object() when the method has a stub. This will be
3012 // the real function rather than the stub.
3013 Named_object
* function_
;
3016 // A reference to a field in a struct.
3018 class Field_reference_expression
: public Expression
3021 Field_reference_expression(Expression
* expr
, unsigned int field_index
,
3023 : Expression(EXPRESSION_FIELD_REFERENCE
, location
),
3024 expr_(expr
), field_index_(field_index
), implicit_(false), called_fieldtrack_(false)
3027 // Return the struct expression.
3030 { return this->expr_
; }
3032 // Return the field index.
3035 { return this->field_index_
; }
3037 // Return whether this node was implied by an anonymous field.
3040 { return this->implicit_
; }
3043 set_implicit(bool implicit
)
3044 { this->implicit_
= implicit
; }
3046 // Set the struct expression. This is used when parsing.
3048 set_struct_expression(Expression
* expr
)
3050 go_assert(this->expr_
== NULL
);
3056 do_traverse(Traverse
* traverse
)
3057 { return Expression::traverse(&this->expr_
, traverse
); }
3060 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
3066 do_determine_type(const Type_context
*)
3067 { this->expr_
->determine_type_no_context(); }
3070 do_check_types(Gogo
*);
3075 return Expression::make_field_reference(this->expr_
->copy(),
3081 do_is_addressable() const
3082 { return this->expr_
->is_addressable(); }
3085 do_address_taken(bool escapes
)
3086 { this->expr_
->address_taken(escapes
); }
3089 do_issue_nil_check()
3090 { this->expr_
->issue_nil_check(); }
3093 do_get_backend(Translate_context
*);
3096 do_dump_expression(Ast_dump_context
*) const;
3099 // The expression we are looking into. This should have a type of
3102 // The zero-based index of the field we are retrieving.
3103 unsigned int field_index_
;
3104 // Whether this node was emitted implicitly for an embedded field,
3105 // that is, expr_ is not the expr_ of the original user node.
3107 // Whether we have already emitted a fieldtrack call.
3108 bool called_fieldtrack_
;
3111 // A reference to a field of an interface.
3113 class Interface_field_reference_expression
: public Expression
3116 Interface_field_reference_expression(Expression
* expr
,
3117 const std::string
& name
,
3119 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE
, location
),
3120 expr_(expr
), name_(name
)
3123 // Return the expression for the interface object.
3126 { return this->expr_
; }
3128 // Return the name of the method to call.
3131 { return this->name_
; }
3133 // Create a thunk to call the method NAME in TYPE when it is used as
3134 // part of a method value.
3135 static Named_object
*
3136 create_thunk(Gogo
*, Interface_type
* type
, const std::string
& name
);
3138 // Return an expression for the pointer to the function to call.
3142 // Return an expression for the first argument to pass to the interface
3143 // function. This is the real object associated with the interface object.
3145 get_underlying_object();
3149 do_traverse(Traverse
* traverse
);
3152 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3158 do_determine_type(const Type_context
*);
3161 do_check_types(Gogo
*);
3166 return Expression::make_interface_field_reference(this->expr_
->copy(),
3172 do_get_backend(Translate_context
*);
3175 do_dump_expression(Ast_dump_context
*) const;
3178 // A mapping from interface types to a list of thunks we have
3179 // created for methods.
3180 typedef std::vector
<std::pair
<std::string
, Named_object
*> > Method_thunks
;
3181 typedef Unordered_map(Interface_type
*, Method_thunks
*)
3182 Interface_method_thunks
;
3183 static Interface_method_thunks interface_method_thunks
;
3185 // The expression for the interface object. This should have a type
3186 // of interface or pointer to interface.
3188 // The field we are retrieving--the name of the method.
3192 // Implement the builtin function new.
3194 class Allocation_expression
: public Expression
3197 Allocation_expression(Type
* type
, Location location
)
3198 : Expression(EXPRESSION_ALLOCATION
, location
),
3199 type_(type
), allocate_on_stack_(false)
3203 set_allocate_on_stack()
3204 { this->allocate_on_stack_
= true; }
3208 do_traverse(Traverse
*);
3214 do_determine_type(const Type_context
*)
3218 do_check_types(Gogo
*);
3224 do_get_backend(Translate_context
*);
3227 do_dump_expression(Ast_dump_context
*) const;
3230 // The type we are allocating.
3232 // Whether or not this is a stack allocation.
3233 bool allocate_on_stack_
;
3236 // A general composite literal. This is lowered to a type specific
3239 class Composite_literal_expression
: public Parser_expression
3242 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
3243 Expression_list
* vals
, bool all_are_names
,
3245 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
3246 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
),
3247 all_are_names_(all_are_names
), key_path_(std::vector
<bool>(depth
))
3251 // Mark the DEPTH entry of KEY_PATH as containing a key.
3253 update_key_path(size_t depth
)
3255 go_assert(depth
< this->key_path_
.size());
3256 this->key_path_
[depth
] = true;
3261 do_traverse(Traverse
* traverse
);
3264 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
3269 Composite_literal_expression
*ret
=
3270 new Composite_literal_expression(this->type_
, this->depth_
,
3272 (this->vals_
== NULL
3274 : this->vals_
->copy()),
3275 this->all_are_names_
,
3277 ret
->key_path_
= this->key_path_
;
3282 do_dump_expression(Ast_dump_context
*) const;
3286 lower_struct(Gogo
*, Type
*);
3292 make_array(Type
*, const std::vector
<unsigned long>*, Expression_list
*);
3295 lower_map(Gogo
*, Named_object
*, Statement_inserter
*, Type
*);
3297 // The type of the composite literal.
3299 // The depth within a list of composite literals within a composite
3300 // literal, when the type is omitted.
3302 // The values to put in the composite literal.
3303 Expression_list
* vals_
;
3304 // If this is true, then VALS_ is a list of pairs: a key and a
3305 // value. In an array initializer, a missing key will be NULL.
3307 // If this is true, then HAS_KEYS_ is true, and every key is a
3308 // simple identifier.
3309 bool all_are_names_
;
3310 // A complement to DEPTH that indicates for each level starting from 0 to
3311 // DEPTH-1 whether or not this composite literal is nested inside of key or
3312 // a value. This is used to decide which type to use when given a map literal
3313 // with omitted key types.
3314 std::vector
<bool> key_path_
;
3317 // Helper/mixin class for struct and array construction expressions;
3318 // encapsulates a list of values plus an optional traversal order
3319 // recording the order in which the values should be visited.
3321 class Ordered_value_list
3324 Ordered_value_list(Expression_list
* vals
)
3325 : vals_(vals
), traverse_order_(NULL
)
3330 { return this->vals_
; }
3333 traverse_vals(Traverse
* traverse
);
3335 // Get the traversal order (may be NULL)
3336 std::vector
<unsigned long>*
3338 { return traverse_order_
; }
3340 // Set the traversal order, used to ensure that we implement the
3341 // order of evaluation rules. Takes ownership of the argument.
3343 set_traverse_order(std::vector
<unsigned long>* traverse_order
)
3344 { this->traverse_order_
= traverse_order
; }
3347 // The list of values, in order of the fields in the struct or in
3348 // order of indices in an array. A NULL value of vals_ means that
3349 // all fields/slots should be zero-initialized; a single NULL entry
3350 // in the list means that the corresponding field or array slot
3351 // should be zero-initialized.
3352 Expression_list
* vals_
;
3353 // If not NULL, the order in which to traverse vals_. This is used
3354 // so that we implement the order of evaluation rules correctly.
3355 std::vector
<unsigned long>* traverse_order_
;
3358 // Construct a struct.
3360 class Struct_construction_expression
: public Expression
,
3361 public Ordered_value_list
3364 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
3366 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
3367 Ordered_value_list(vals
),
3371 // Return whether this is a constant initializer.
3373 is_constant_struct() const;
3377 do_traverse(Traverse
* traverse
);
3380 do_is_static_initializer() const;
3384 { return this->type_
; }
3387 do_determine_type(const Type_context
*);
3390 do_check_types(Gogo
*);
3395 Struct_construction_expression
* ret
=
3396 new Struct_construction_expression(this->type_
,
3397 (this->vals() == NULL
3399 : this->vals()->copy()),
3401 if (this->traverse_order() != NULL
)
3402 ret
->set_traverse_order(this->traverse_order());
3407 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3410 do_get_backend(Translate_context
*);
3413 do_export(Export
*) const;
3416 do_dump_expression(Ast_dump_context
*) const;
3419 // The type of the struct to construct.
3423 // Construct an array. This class is not used directly; instead we
3424 // use the child classes, Fixed_array_construction_expression and
3425 // Slice_construction_expression.
3427 class Array_construction_expression
: public Expression
,
3428 public Ordered_value_list
3431 Array_construction_expression(Expression_classification classification
,
3433 const std::vector
<unsigned long>* indexes
,
3434 Expression_list
* vals
, Location location
)
3435 : Expression(classification
, location
),
3436 Ordered_value_list(vals
),
3437 type_(type
), indexes_(indexes
)
3438 { go_assert(indexes
== NULL
|| indexes
->size() == vals
->size()); }
3441 // Return whether this is a constant initializer.
3443 is_constant_array() const;
3445 // Return the number of elements.
3447 element_count() const
3448 { return this->vals() == NULL
? 0 : this->vals()->size(); }
3452 do_traverse(Traverse
* traverse
);
3455 do_is_static_initializer() const;
3459 { return this->type_
; }
3462 do_determine_type(const Type_context
*);
3465 do_check_types(Gogo
*);
3468 do_export(Export
*) const;
3471 const std::vector
<unsigned long>*
3473 { return this->indexes_
; }
3476 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3478 // Get the backend constructor for the array values.
3480 get_constructor(Translate_context
* context
, Btype
* btype
);
3483 do_dump_expression(Ast_dump_context
*) const;
3486 dump_slice_storage_expression(Ast_dump_context
*) const { }
3489 // The type of the array to construct.
3491 // The list of indexes into the array, one for each value. This may
3492 // be NULL, in which case the indexes start at zero and increment.
3493 const std::vector
<unsigned long>* indexes_
;
3496 // Construct a fixed array.
3498 class Fixed_array_construction_expression
:
3499 public Array_construction_expression
3502 Fixed_array_construction_expression(Type
* type
,
3503 const std::vector
<unsigned long>* indexes
,
3504 Expression_list
* vals
, Location location
);
3510 return new Fixed_array_construction_expression(this->type(),
3512 (this->vals() == NULL
3514 : this->vals()->copy()),
3519 do_get_backend(Translate_context
*);
3522 // Construct a slice.
3524 class Slice_construction_expression
: public Array_construction_expression
3527 Slice_construction_expression(Type
* type
,
3528 const std::vector
<unsigned long>* indexes
,
3529 Expression_list
* vals
, Location location
);
3532 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3534 // Record that the storage for this slice (e.g. vals) cannot escape,
3535 // hence it can be stack-allocated.
3537 set_storage_does_not_escape()
3539 this->storage_escapes_
= false;
3543 // Note that taking the address of a slice literal is invalid.
3546 do_traverse(Traverse
* traverse
);
3551 return new Slice_construction_expression(this->type(), this->indexes(),
3552 (this->vals() == NULL
3554 : this->vals()->copy()),
3559 do_get_backend(Translate_context
*);
3562 dump_slice_storage_expression(Ast_dump_context
* ast_dump_context
) const;
3564 // Create an array value for the constructed slice. Invoked during
3565 // flattening if slice storage does not escape, otherwise invoked
3566 // later on during do_get_backend().
3571 // The type of the values in this slice.
3573 // Array value expression, optionally filled in during flattening.
3574 Expression
* array_val_
;
3575 // Slice storage expression, optionally filled in during flattening.
3576 Expression
* slice_storage_
;
3577 // Normally true. Can be set to false if we know that the resulting
3578 // storage for the slice cannot escape.
3579 bool storage_escapes_
;
3584 class Map_construction_expression
: public Expression
3587 Map_construction_expression(Type
* type
, Expression_list
* vals
,
3589 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
3590 type_(type
), vals_(vals
), element_type_(NULL
), constructor_temp_(NULL
)
3591 { go_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
3595 { return this->vals_
; }
3599 do_traverse(Traverse
* traverse
);
3602 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3606 { return this->type_
; }
3609 do_determine_type(const Type_context
*);
3612 do_check_types(Gogo
*);
3617 return new Map_construction_expression(this->type_
,
3618 (this->vals_
== NULL
3620 : this->vals_
->copy()),
3625 do_get_backend(Translate_context
*);
3628 do_export(Export
*) const;
3631 do_dump_expression(Ast_dump_context
*) const;
3634 // The type of the map to construct.
3636 // The list of values.
3637 Expression_list
* vals_
;
3638 // The type of the key-value pair struct for each map element.
3639 Struct_type
* element_type_
;
3640 // A temporary reference to the variable storing the constructor initializer.
3641 Temporary_statement
* constructor_temp_
;
3644 // A type guard expression.
3646 class Type_guard_expression
: public Expression
3649 Type_guard_expression(Expression
* expr
, Type
* type
, Location location
)
3650 : Expression(EXPRESSION_TYPE_GUARD
, location
),
3651 expr_(expr
), type_(type
)
3654 // Return the expression to convert.
3657 { return this->expr_
; }
3659 // Return the type to which to convert.
3662 { return this->type_
; }
3666 do_traverse(Traverse
* traverse
);
3669 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3673 { return this->type_
; }
3676 do_determine_type(const Type_context
*)
3677 { this->expr_
->determine_type_no_context(); }
3680 do_check_types(Gogo
*);
3685 return new Type_guard_expression(this->expr_
->copy(), this->type_
,
3690 do_get_backend(Translate_context
*);
3693 do_dump_expression(Ast_dump_context
*) const;
3696 // The expression to convert.
3698 // The type to which to convert.
3702 // Class Heap_expression.
3704 // When you take the address of an escaping expression, it is allocated
3705 // on the heap. This class implements that.
3707 class Heap_expression
: public Expression
3710 Heap_expression(Expression
* expr
, Location location
)
3711 : Expression(EXPRESSION_HEAP
, location
),
3717 { return this->expr_
; }
3721 do_traverse(Traverse
* traverse
)
3722 { return Expression::traverse(&this->expr_
, traverse
); }
3727 do_determine_type(const Type_context
*)
3728 { this->expr_
->determine_type_no_context(); }
3733 return Expression::make_heap_expression(this->expr_
->copy(),
3738 do_get_backend(Translate_context
*);
3740 // We only export global objects, and the parser does not generate
3741 // this in global scope.
3743 do_export(Export
*) const
3744 { go_unreachable(); }
3747 do_dump_expression(Ast_dump_context
*) const;
3750 // The expression which is being put on the heap.
3754 // A receive expression.
3756 class Receive_expression
: public Expression
3759 Receive_expression(Expression
* channel
, Location location
)
3760 : Expression(EXPRESSION_RECEIVE
, location
),
3761 channel_(channel
), temp_receiver_(NULL
)
3764 // Return the channel.
3767 { return this->channel_
; }
3771 do_traverse(Traverse
* traverse
)
3772 { return Expression::traverse(&this->channel_
, traverse
); }
3775 do_discarding_value()
3782 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3785 do_determine_type(const Type_context
*)
3786 { this->channel_
->determine_type_no_context(); }
3789 do_check_types(Gogo
*);
3794 return Expression::make_receive(this->channel_
->copy(), this->location());
3798 do_must_eval_in_order() const
3802 do_get_backend(Translate_context
*);
3805 do_dump_expression(Ast_dump_context
*) const;
3808 // The channel from which we are receiving.
3809 Expression
* channel_
;
3810 // A temporary reference to the variable storing the received data.
3811 Temporary_statement
* temp_receiver_
;
3814 // Conditional expressions.
3816 class Conditional_expression
: public Expression
3819 Conditional_expression(Expression
* cond
, Expression
* then_expr
,
3820 Expression
* else_expr
, Location location
)
3821 : Expression(EXPRESSION_CONDITIONAL
, location
),
3822 cond_(cond
), then_(then_expr
), else_(else_expr
)
3827 { return this->cond_
; }
3831 do_traverse(Traverse
*);
3837 do_determine_type(const Type_context
*);
3842 return new Conditional_expression(this->cond_
->copy(), this->then_
->copy(),
3843 this->else_
->copy(), this->location());
3847 do_get_backend(Translate_context
* context
);
3850 do_dump_expression(Ast_dump_context
*) const;
3853 // The condition to be checked.
3855 // The expression to execute if the condition is true.
3857 // The expression to execute if the condition is false.
3861 // Compound expressions.
3863 class Compound_expression
: public Expression
3866 Compound_expression(Expression
* init
, Expression
* expr
, Location location
)
3867 : Expression(EXPRESSION_COMPOUND
, location
), init_(init
), expr_(expr
)
3872 { return this->init_
; }
3876 do_traverse(Traverse
*);
3882 do_determine_type(const Type_context
*);
3887 return new Compound_expression(this->init_
->copy(), this->expr_
->copy(),
3892 do_get_backend(Translate_context
* context
);
3895 do_dump_expression(Ast_dump_context
*) const;
3898 // The expression that is evaluated first and discarded.
3900 // The expression that is evaluated and returned.
3904 // A backend expression. This is a backend expression wrapped in an
3905 // Expression, for convenience during backend generation.
3907 class Backend_expression
: public Expression
3910 Backend_expression(Bexpression
* bexpr
, Type
* type
, Location location
)
3911 : Expression(EXPRESSION_BACKEND
, location
), bexpr_(bexpr
), type_(type
)
3916 do_traverse(Traverse
*);
3918 // For now these are always valid static initializers. If that
3919 // changes we can change this.
3921 do_is_static_initializer() const
3926 { return this->type_
; }
3929 do_determine_type(const Type_context
*)
3935 return new Backend_expression(this->bexpr_
, this->type_
, this->location());
3939 do_get_backend(Translate_context
*)
3940 { return this->bexpr_
; }
3943 do_dump_expression(Ast_dump_context
*) const;
3946 // The backend expression we are wrapping.
3947 Bexpression
* bexpr_
;
3948 // The type of the expression;
3952 // A numeric constant. This is used both for untyped constants and
3953 // for constants that have a type.
3955 class Numeric_constant
3959 : classification_(NC_INVALID
), type_(NULL
)
3962 ~Numeric_constant();
3964 Numeric_constant(const Numeric_constant
&);
3966 Numeric_constant
& operator=(const Numeric_constant
&);
3968 // Set to an unsigned long value.
3970 set_unsigned_long(Type
*, unsigned long);
3972 // Set to an integer value.
3974 set_int(Type
*, const mpz_t
);
3976 // Set to a rune value.
3978 set_rune(Type
*, const mpz_t
);
3980 // Set to a floating point value.
3982 set_float(Type
*, const mpfr_t
);
3984 // Set to a complex value.
3986 set_complex(Type
*, const mpc_t
);
3988 // Mark numeric constant as invalid.
3991 { this->classification_
= NC_INVALID
; }
3996 { return this->classification_
== Numeric_constant::NC_INT
; }
4000 { return this->classification_
== Numeric_constant::NC_RUNE
; }
4004 { return this->classification_
== Numeric_constant::NC_FLOAT
; }
4008 { return this->classification_
== Numeric_constant::NC_COMPLEX
; }
4012 { return this->classification_
== Numeric_constant::NC_INVALID
; }
4014 // Value retrievers. These will initialize the values as well as
4015 // set them. GET_INT is only valid if IS_INT returns true, and
4016 // likewise respectively.
4018 get_int(mpz_t
*) const;
4021 get_rune(mpz_t
*) const;
4024 get_float(mpfr_t
*) const;
4027 get_complex(mpc_t
*) const;
4029 // Codes returned by to_unsigned_long.
4030 enum To_unsigned_long
4032 // Value is integer and fits in unsigned long.
4034 // Value is not integer.
4036 // Value is integer but is negative.
4038 // Value is non-negative integer but does not fit in unsigned
4043 // If the value can be expressed as an integer that fits in an
4044 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
4045 // one of the other To_unsigned_long codes.
4047 to_unsigned_long(unsigned long* val
) const;
4049 // If the value can be expressed as an integer that describes the
4050 // size of an object in memory, set *VAL and return true.
4051 // Otherwise, return false. Currently we use int64_t to represent a
4052 // memory size, as in Type::backend_type_size.
4054 to_memory_size(int64_t* val
) const;
4056 // If the value can be expressed as an int, return true and
4057 // initialize and set VAL. This will return false for a value with
4058 // an explicit float or complex type, even if the value is integral.
4060 to_int(mpz_t
* val
) const;
4062 // If the value can be expressed as a float, return true and
4063 // initialize and set VAL.
4065 to_float(mpfr_t
* val
) const;
4067 // If the value can be expressed as a complex, return true and
4068 // initialize and set VR and VI.
4070 to_complex(mpc_t
* val
) const;
4076 // If the constant can be expressed in TYPE, then set the type of
4077 // the constant to TYPE and return true. Otherwise return false,
4078 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
4079 // the location to use for the error.
4081 set_type(Type
* type
, bool issue_error
, Location location
);
4083 // Return an Expression for this value.
4085 expression(Location
) const;
4092 mpz_to_unsigned_long(const mpz_t ival
, unsigned long *val
) const;
4095 mpfr_to_unsigned_long(const mpfr_t fval
, unsigned long *val
) const;
4098 mpz_to_memory_size(const mpz_t ival
, int64_t* val
) const;
4101 mpfr_to_memory_size(const mpfr_t fval
, int64_t* val
) const;
4104 check_int_type(Integer_type
*, bool, Location
);
4107 check_float_type(Float_type
*, bool, Location
);
4110 check_complex_type(Complex_type
*, bool, Location
);
4112 // The kinds of constants.
4122 // The kind of constant.
4123 Classification classification_
;
4127 // If NC_INT or NC_RUNE.
4134 // The type if there is one. This will be NULL for an untyped
4139 #endif // !defined(GO_EXPRESSIONS_H)