1 // expressions.h -- Go frontend expression handling. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #ifndef GO_EXPRESSIONS_H
8 #define GO_EXPRESSIONS_H
17 class Translate_context
;
19 class Statement_inserter
;
30 class Expression_list
;
32 class Enclosed_var_expression
;
33 class Temporary_reference_expression
;
34 class Set_and_use_temporary_expression
;
35 class String_expression
;
36 class Type_conversion_expression
;
37 class Unsafe_type_conversion_expression
;
38 class Unary_expression
;
39 class Binary_expression
;
40 class String_concat_expression
;
41 class Call_expression
;
42 class Builtin_call_expression
;
43 class Call_result_expression
;
44 class Func_expression
;
45 class Func_descriptor_expression
;
46 class Unknown_expression
;
47 class Index_expression
;
48 class Array_index_expression
;
49 class String_index_expression
;
50 class Map_index_expression
;
51 class Bound_method_expression
;
52 class Field_reference_expression
;
53 class Interface_field_reference_expression
;
54 class Allocation_expression
;
55 class Composite_literal_expression
;
56 class Struct_construction_expression
;
57 class Array_construction_expression
;
58 class Fixed_array_construction_expression
;
59 class Slice_construction_expression
;
60 class Map_construction_expression
;
61 class Type_guard_expression
;
62 class Heap_expression
;
63 class Receive_expression
;
64 class Slice_value_expression
;
65 class Conditional_expression
;
66 class Compound_expression
;
67 class Numeric_constant
;
69 class Export_function_body
;
70 class Import_expression
;
71 class Temporary_statement
;
73 class Ast_dump_context
;
76 // The precision to use for complex values represented as an mpc_t.
77 const int mpc_precision
= 256;
79 // The base class for all expressions.
84 // The types of expressions.
85 enum Expression_classification
91 EXPRESSION_STRING_CONCAT
,
92 EXPRESSION_CONST_REFERENCE
,
93 EXPRESSION_VAR_REFERENCE
,
94 EXPRESSION_ENCLOSED_VAR_REFERENCE
,
95 EXPRESSION_TEMPORARY_REFERENCE
,
96 EXPRESSION_SET_AND_USE_TEMPORARY
,
98 EXPRESSION_FUNC_REFERENCE
,
99 EXPRESSION_FUNC_DESCRIPTOR
,
100 EXPRESSION_FUNC_CODE_REFERENCE
,
101 EXPRESSION_UNKNOWN_REFERENCE
,
104 EXPRESSION_STRING_INFO
,
105 EXPRESSION_STRING_VALUE
,
112 EXPRESSION_CALL_RESULT
,
113 EXPRESSION_BOUND_METHOD
,
115 EXPRESSION_ARRAY_INDEX
,
116 EXPRESSION_STRING_INDEX
,
117 EXPRESSION_MAP_INDEX
,
119 EXPRESSION_FIELD_REFERENCE
,
120 EXPRESSION_INTERFACE_FIELD_REFERENCE
,
121 EXPRESSION_ALLOCATION
,
122 EXPRESSION_TYPE_GUARD
,
123 EXPRESSION_CONVERSION
,
124 EXPRESSION_UNSAFE_CONVERSION
,
125 EXPRESSION_STRUCT_CONSTRUCTION
,
126 EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
127 EXPRESSION_SLICE_CONSTRUCTION
,
128 EXPRESSION_MAP_CONSTRUCTION
,
129 EXPRESSION_COMPOSITE_LITERAL
,
130 EXPRESSION_COMPOSITE_LITERAL_KEY
,
133 EXPRESSION_TYPE_DESCRIPTOR
,
134 EXPRESSION_GC_SYMBOL
,
135 EXPRESSION_PTRMASK_SYMBOL
,
136 EXPRESSION_TYPE_INFO
,
137 EXPRESSION_SLICE_INFO
,
138 EXPRESSION_SLICE_VALUE
,
139 EXPRESSION_INTERFACE_INFO
,
140 EXPRESSION_INTERFACE_VALUE
,
141 EXPRESSION_INTERFACE_MTABLE
,
142 EXPRESSION_STRUCT_FIELD_OFFSET
,
143 EXPRESSION_LABEL_ADDR
,
144 EXPRESSION_CONDITIONAL
,
149 Expression(Expression_classification
, Location
);
151 virtual ~Expression();
153 // Make an error expression. This is used when a parse error occurs
154 // to prevent cascading errors.
156 make_error(Location
);
158 // Make an expression which is really a type. This is used during
161 make_type(Type
*, Location
);
163 // Make a unary expression.
165 make_unary(Operator
, Expression
*, Location
);
167 // Make a binary expression.
169 make_binary(Operator
, Expression
*, Expression
*, Location
);
171 // Make a string concatenation expression.
173 make_string_concat(Expression_list
*);
175 // Make a reference to a constant in an expression.
177 make_const_reference(Named_object
*, Location
);
179 // Make a reference to a variable in an expression.
181 make_var_reference(Named_object
*, Location
);
183 // Make a reference to a variable within an enclosing function.
185 make_enclosing_var_reference(Expression
*, Named_object
*, Location
);
187 // Make a reference to a temporary variable. Temporary variables
188 // are always created by a single statement, which is what we use to
190 static Temporary_reference_expression
*
191 make_temporary_reference(Temporary_statement
*, Location
);
193 // Make an expressions which sets a temporary variable and then
194 // evaluates to a reference to that temporary variable. This is
195 // used to set a temporary variable while retaining the order of
197 static Set_and_use_temporary_expression
*
198 make_set_and_use_temporary(Temporary_statement
*, Expression
*, Location
);
200 // Make a sink expression--a reference to the blank identifier _.
204 // Make a reference to a function in an expression. This returns a
205 // pointer to the struct holding the address of the function
206 // followed by any closed-over variables.
208 make_func_reference(Named_object
*, Expression
* closure
, Location
);
210 // Make a function descriptor, an immutable struct with a single
211 // field that points to the function code. This may only be used
212 // with functions that do not have closures. FN is the function for
213 // which we are making the descriptor.
214 static Func_descriptor_expression
*
215 make_func_descriptor(Named_object
* fn
);
217 // Make a reference to the code of a function. This is used to set
218 // descriptor and closure fields.
220 make_func_code_reference(Named_object
*, Location
);
222 // Make a reference to an unknown name. In a correct program this
223 // will always be lowered to a real const/var/func reference.
224 static Unknown_expression
*
225 make_unknown_reference(Named_object
*, Location
);
227 // Make a constant bool expression.
229 make_boolean(bool val
, Location
);
231 // Make a constant string expression.
233 make_string(const std::string
&, Location
);
235 // Make a constant string expression with a specific string subtype.
237 make_string_typed(const std::string
&, Type
*, Location
);
239 // Make an expression that evaluates to some characteristic of an string.
240 // For simplicity, the enum values must match the field indexes in the
241 // underlying struct. This returns an lvalue.
244 // The underlying data in the string.
246 // The length of the string.
251 make_string_info(Expression
* string
, String_info
, Location
);
253 // Make an expression for a string value.
255 make_string_value(Expression
* valptr
, Expression
* len
, Location
);
257 // Make a character constant expression. TYPE should be NULL for an
260 make_character(const mpz_t
*, Type
*, Location
);
262 // Make a constant integer expression from a multi-precision
263 // integer. TYPE should be NULL for an abstract type.
265 make_integer_z(const mpz_t
*, Type
*, Location
);
267 // Make a constant integer expression from an unsigned long. TYPE
268 // should be NULL for an abstract type.
270 make_integer_ul(unsigned long, Type
*, Location
);
272 // Make a constant integer expression from a signed long. TYPE
273 // should be NULL for an abstract type.
275 make_integer_sl(long, Type
*, Location
);
277 // Make a constant integer expression from an int64_t. TYPE should
278 // be NULL for an abstract type.
280 make_integer_int64(int64_t, Type
*, Location
);
282 // Make a constant float expression. TYPE should be NULL for an
285 make_float(const mpfr_t
*, Type
*, Location
);
287 // Make a constant complex expression. TYPE should be NULL for an
290 make_complex(const mpc_t
*, Type
*, Location
);
292 // Make a nil expression.
296 // Make an iota expression. This is used for the predeclared
301 // Make a call expression.
302 static Call_expression
*
303 make_call(Expression
* func
, Expression_list
* args
, bool is_varargs
,
306 // Make a reference to a specific result of a call expression which
309 make_call_result(Call_expression
*, unsigned int index
);
311 // Make an expression which is a method bound to its first
312 // parameter. METHOD is the method being called, FUNCTION is the
314 static Bound_method_expression
*
315 make_bound_method(Expression
* object
, const Method
* method
,
316 Named_object
* function
, Location
);
318 // Make an index or slice expression. This is a parser expression
319 // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
320 // index rather than a slice. CAP may be NULL, meaning we use the default
321 // capacity of LEFT. At parse time we may not know the type of LEFT.
322 // After parsing this is lowered to an array index, a string index,
325 make_index(Expression
* left
, Expression
* start
, Expression
* end
,
326 Expression
* cap
, Location
);
328 // Make an array index expression. END may be NULL, in which case
329 // this is an lvalue. CAP may be NULL, in which case it defaults
332 make_array_index(Expression
* array
, Expression
* start
, Expression
* end
,
333 Expression
* cap
, Location
);
335 // Make a string index expression. END may be NULL. This is never
338 make_string_index(Expression
* string
, Expression
* start
, Expression
* end
,
341 // Make a map index expression. This is an lvalue.
342 static Map_index_expression
*
343 make_map_index(Expression
* map
, Expression
* val
, Location
);
345 // Make a selector. This is a parser expression which represents
346 // LEFT.NAME. At parse time we may not know the type of the left
349 make_selector(Expression
* left
, const std::string
& name
, Location
);
351 // Make a reference to a field in a struct.
352 static Field_reference_expression
*
353 make_field_reference(Expression
*, unsigned int field_index
, Location
);
355 // Make a reference to a field of an interface, with an associated
358 make_interface_field_reference(Expression
*, const std::string
&,
361 // Make an allocation expression.
363 make_allocation(Type
*, Location
);
365 // Make a type guard expression.
367 make_type_guard(Expression
*, Type
*, Location
);
369 // Make a type cast expression.
371 make_cast(Type
*, Expression
*, Location
);
373 // Make an unsafe type cast expression. This is only used when
374 // passing parameter to builtin functions that are part of the Go
377 make_unsafe_cast(Type
*, Expression
*, Location
);
379 // Make a composite literal. The DEPTH parameter is how far down we
380 // are in a list of composite literals with omitted types. HAS_KEYS
381 // is true if the expression list has keys alternating with values.
382 // ALL_ARE_NAMES is true if all the keys could be struct field
385 make_composite_literal(Type
*, int depth
, bool has_keys
, Expression_list
*,
386 bool all_are_names
, Location
);
388 // Make a composite literal key.
390 make_composite_literal_key(const std::string
& name
, Location
);
392 // Make a struct composite literal.
394 make_struct_composite_literal(Type
*, Expression_list
*, Location
);
396 // Make an array composite literal.
398 make_array_composite_literal(Type
*, Expression_list
*, Location
);
400 // Make a slice composite literal.
401 static Slice_construction_expression
*
402 make_slice_composite_literal(Type
*, Expression_list
*, Location
);
404 // Take an expression and allocate it on the heap.
406 make_heap_expression(Expression
*, Location
);
408 // Make a receive expression. VAL is NULL for a unary receive.
409 static Receive_expression
*
410 make_receive(Expression
* channel
, Location
);
412 // Make an expression which evaluates to the address of the type
413 // descriptor for TYPE.
415 make_type_descriptor(Type
* type
, Location
);
417 // Make an expression which evaluates to the address of the gc
420 make_gc_symbol(Type
* type
);
422 // Make an expression that evaluates to the address of a ptrmask
423 // symbol for TYPE. For most types this will be the same as
424 // make_gc_symbol, but for larger types make_gc_symbol will return a
425 // gcprog while this will return a ptrmask.
427 make_ptrmask_symbol(Type
* type
);
429 // Make an expression which evaluates to some characteristic of a
430 // type. These are only used for type descriptors, so there is no
431 // location parameter.
434 // The size of a value of the type.
436 // The required alignment of a value of the type.
438 // The required alignment of a value of the type when used as a
439 // field in a struct.
440 TYPE_INFO_FIELD_ALIGNMENT
,
441 // The size of the prefix of a value of the type that contains
442 // all the pointers. This is 0 for a type that contains no
443 // pointers. It is always <= TYPE_INFO_SIZE.
444 TYPE_INFO_BACKEND_PTRDATA
,
445 // Like TYPE_INFO_BACKEND_PTRDATA, but the ptrdata value that we
446 // want to store in a type descriptor. They are the same for
447 // most types, but can differ for a type that uses a gcprog.
448 TYPE_INFO_DESCRIPTOR_PTRDATA
452 make_type_info(Type
* type
, Type_info
);
454 // Make an expression that evaluates to some characteristic of a
455 // slice. For simplicity, the enum values must match the field indexes
456 // in the underlying struct. This returns an lvalue.
459 // The underlying data of the slice.
460 SLICE_INFO_VALUE_POINTER
,
461 // The length of the slice.
463 // The capacity of the slice.
468 make_slice_info(Expression
* slice
, Slice_info
, Location
);
470 // Make an expression for a slice value.
472 make_slice_value(Type
*, Expression
* valptr
, Expression
* len
, Expression
* cap
,
475 // Make an expression that evaluates to some characteristic of an
476 // interface. For simplicity, the enum values must match the field indexes
477 // in the underlying struct. This returns an lvalue.
480 // The type descriptor of an empty interface.
481 INTERFACE_INFO_TYPE_DESCRIPTOR
= 0,
482 // The methods of an interface.
483 INTERFACE_INFO_METHODS
= 0,
484 // The first argument to pass to an interface method.
485 INTERFACE_INFO_OBJECT
489 make_interface_info(Expression
* iface
, Interface_info
, Location
);
491 // Make an expression for an interface value.
493 make_interface_value(Type
*, Expression
*, Expression
*, Location
);
495 // Make an expression that builds a reference to the interface method table
496 // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
497 // reference to the interface method table for the pointer receiver type.
499 make_interface_mtable_ref(Interface_type
* itype
, Type
* type
,
500 bool is_pointer
, Location
);
502 // Make an expression which evaluates to the offset of a field in a
503 // struct. This is only used for type descriptors, so there is no
504 // location parameter.
506 make_struct_field_offset(Struct_type
*, const Struct_field
*);
508 // Make an expression which evaluates to the address of an unnamed
511 make_label_addr(Label
*, Location
);
513 // Make a conditional expression.
515 make_conditional(Expression
*, Expression
*, Expression
*, Location
);
517 // Make a compound expression.
519 make_compound(Expression
*, Expression
*, Location
);
521 // Make a backend expression.
523 make_backend(Bexpression
*, Type
*, Location
);
525 enum Nil_check_classification
527 // Use the default policy for deciding if this deref needs a check.
529 // An explicit check is required for this dereference operation.
531 // No check needed for this dereference operation.
532 NIL_CHECK_NOT_NEEDED
,
533 // A type error or error construct was encountered when determining
534 // whether this deref needs an explicit check.
535 NIL_CHECK_ERROR_ENCOUNTERED
538 // Make a dereference expression.
540 make_dereference(Expression
*, Nil_check_classification
, Location
);
542 // Return the expression classification.
543 Expression_classification
544 classification() const
545 { return this->classification_
; }
547 // Return the location of the expression.
550 { return this->location_
; }
552 // Return whether this is a constant expression.
555 { return this->do_is_constant(); }
557 // Return whether this is the zero value of its type.
559 is_zero_value() const
560 { return this->do_is_zero_value(); }
562 // Return whether this expression can be used as a static
563 // initializer. This is true for an expression that has only
564 // numbers and pointers to global variables or composite literals
565 // that do not require runtime initialization. It is false if we
566 // must generate code to compute this expression when it is used to
567 // initialize a global variable. This is not a language-level
568 // concept, but an implementation-level one. If this expression is
569 // used to initialize a global variable, this is true if we can pass
570 // an initializer to the backend, false if we must generate code to
571 // initialize the variable. It is always safe for this method to
572 // return false, but the resulting code may be less efficient.
574 is_static_initializer() const
575 { return this->do_is_static_initializer(); }
577 // If this is not a numeric constant, return false. If it is one,
578 // return true, and set VAL to hold the value.
580 numeric_constant_value(Numeric_constant
* val
) const
581 { return this->do_numeric_constant_value(val
); }
583 // If this is not a constant expression with string type, return
584 // false. If it is one, return true, and set VAL to the value.
586 string_constant_value(std::string
* val
) const
587 { return this->do_string_constant_value(val
); }
589 // If this is not a constant expression with boolean type, return
590 // false. If it is one, return true, and set VAL to the value.
592 boolean_constant_value(bool* val
) const
593 { return this->do_boolean_constant_value(val
); }
595 // If this is a const reference expression, return the named
596 // object to which the expression refers, otherwise return NULL.
598 named_constant() const;
600 // This is called if the value of this expression is being
601 // discarded. This issues warnings about computed values being
602 // unused. This returns true if all is well, false if it issued an
606 { return this->do_discarding_value(); }
608 // Return whether this is an error expression.
610 is_error_expression() const
611 { return this->classification_
== EXPRESSION_ERROR
; }
613 // Return whether this expression really represents a type.
615 is_type_expression() const
616 { return this->classification_
== EXPRESSION_TYPE
; }
618 // If this is a variable reference, return the Var_expression
619 // structure. Otherwise, return NULL. This is a controlled dynamic
623 { return this->convert
<Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
625 const Var_expression
*
626 var_expression() const
627 { return this->convert
<const Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
629 // If this is a enclosed_variable reference, return the
630 // Enclosed_var_expression structure. Otherwise, return NULL.
631 // This is a controlled dynamic cast.
632 Enclosed_var_expression
*
633 enclosed_var_expression()
634 { return this->convert
<Enclosed_var_expression
,
635 EXPRESSION_ENCLOSED_VAR_REFERENCE
>(); }
637 const Enclosed_var_expression
*
638 enclosed_var_expression() const
639 { return this->convert
<const Enclosed_var_expression
,
640 EXPRESSION_ENCLOSED_VAR_REFERENCE
>(); }
643 // If this is a reference to a temporary variable, return the
644 // Temporary_reference_expression. Otherwise, return NULL.
645 Temporary_reference_expression
*
646 temporary_reference_expression()
648 return this->convert
<Temporary_reference_expression
,
649 EXPRESSION_TEMPORARY_REFERENCE
>();
652 // If this is a set-and-use-temporary, return the
653 // Set_and_use_temporary_expression. Otherwise, return NULL.
654 Set_and_use_temporary_expression
*
655 set_and_use_temporary_expression()
657 return this->convert
<Set_and_use_temporary_expression
,
658 EXPRESSION_SET_AND_USE_TEMPORARY
>();
661 // Return whether this is a sink expression.
663 is_sink_expression() const
664 { return this->classification_
== EXPRESSION_SINK
; }
666 // If this is a string expression, return the String_expression
667 // structure. Otherwise, return NULL.
670 { return this->convert
<String_expression
, EXPRESSION_STRING
>(); }
672 // If this is a conversion expression, return the Type_conversion_expression
673 // structure. Otherwise, return NULL.
674 Type_conversion_expression
*
675 conversion_expression()
676 { return this->convert
<Type_conversion_expression
, EXPRESSION_CONVERSION
>(); }
678 // If this is an unsafe conversion expression, return the
679 // Unsafe_type_conversion_expression structure. Otherwise, return NULL.
680 Unsafe_type_conversion_expression
*
681 unsafe_conversion_expression()
683 return this->convert
<Unsafe_type_conversion_expression
,
684 EXPRESSION_UNSAFE_CONVERSION
>();
687 // Return whether this is the expression nil.
689 is_nil_expression() const
690 { return this->classification_
== EXPRESSION_NIL
; }
692 // If this is an indirection through a pointer, return the
693 // expression being pointed through. Otherwise return this.
697 // If this is a unary expression, return the Unary_expression
698 // structure. Otherwise return NULL.
701 { return this->convert
<Unary_expression
, EXPRESSION_UNARY
>(); }
703 // If this is a binary expression, return the Binary_expression
704 // structure. Otherwise return NULL.
707 { return this->convert
<Binary_expression
, EXPRESSION_BINARY
>(); }
709 // If this is a string concatenation expression, return the
710 // String_concat_expression structure. Otherwise, return NULL.
711 String_concat_expression
*
712 string_concat_expression()
714 return this->convert
<String_concat_expression
, EXPRESSION_STRING_CONCAT
>();
717 // If this is a call expression, return the Call_expression
718 // structure. Otherwise, return NULL. This is a controlled dynamic
722 { return this->convert
<Call_expression
, EXPRESSION_CALL
>(); }
724 // If this is a call_result expression, return the Call_result_expression
725 // structure. Otherwise, return NULL. This is a controlled dynamic
727 Call_result_expression
*
728 call_result_expression()
729 { return this->convert
<Call_result_expression
, EXPRESSION_CALL_RESULT
>(); }
731 // If this is an expression which refers to a function, return the
732 // Func_expression structure. Otherwise, return NULL.
735 { return this->convert
<Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
737 const Func_expression
*
738 func_expression() const
739 { return this->convert
<const Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
741 // If this is an expression which refers to an unknown name, return
742 // the Unknown_expression structure. Otherwise, return NULL.
745 { return this->convert
<Unknown_expression
, EXPRESSION_UNKNOWN_REFERENCE
>(); }
747 const Unknown_expression
*
748 unknown_expression() const
750 return this->convert
<const Unknown_expression
,
751 EXPRESSION_UNKNOWN_REFERENCE
>();
754 // If this is an index expression, return the Index_expression
755 // structure. Otherwise, return NULL.
758 { return this->convert
<Index_expression
, EXPRESSION_INDEX
>(); }
760 // If this is an expression which refers to indexing in a array,
761 // return the Array_index_expression structure. Otherwise, return
763 Array_index_expression
*
764 array_index_expression()
765 { return this->convert
<Array_index_expression
, EXPRESSION_ARRAY_INDEX
>(); }
767 // If this is an expression which refers to indexing in a string,
768 // return the String_index_expression structure. Otherwise, return
770 String_index_expression
*
771 string_index_expression()
772 { return this->convert
<String_index_expression
, EXPRESSION_STRING_INDEX
>(); }
774 // If this is an expression which refers to indexing in a map,
775 // return the Map_index_expression structure. Otherwise, return
777 Map_index_expression
*
778 map_index_expression()
779 { return this->convert
<Map_index_expression
, EXPRESSION_MAP_INDEX
>(); }
781 // If this is a bound method expression, return the
782 // Bound_method_expression structure. Otherwise, return NULL.
783 Bound_method_expression
*
784 bound_method_expression()
785 { return this->convert
<Bound_method_expression
, EXPRESSION_BOUND_METHOD
>(); }
787 // If this is a reference to a field in a struct, return the
788 // Field_reference_expression structure. Otherwise, return NULL.
789 Field_reference_expression
*
790 field_reference_expression()
792 return this->convert
<Field_reference_expression
,
793 EXPRESSION_FIELD_REFERENCE
>();
796 // If this is a reference to a field in an interface, return the
797 // Interface_field_reference_expression structure. Otherwise,
799 Interface_field_reference_expression
*
800 interface_field_reference_expression()
802 return this->convert
<Interface_field_reference_expression
,
803 EXPRESSION_INTERFACE_FIELD_REFERENCE
>();
806 // If this is an allocation expression, return the Allocation_expression
807 // structure. Otherwise, return NULL.
808 Allocation_expression
*
809 allocation_expression()
810 { return this->convert
<Allocation_expression
, EXPRESSION_ALLOCATION
>(); }
812 // If this is a general composite literal, return the
813 // Composite_literal_expression structure. Otherwise, return NULL.
814 Composite_literal_expression
*
817 return this->convert
<Composite_literal_expression
,
818 EXPRESSION_COMPOSITE_LITERAL
>();
821 // If this is a struct composite literal, return the
822 // Struct_construction_expression structure. Otherwise, return NULL.
823 Struct_construction_expression
*
826 return this->convert
<Struct_construction_expression
,
827 EXPRESSION_STRUCT_CONSTRUCTION
>();
830 // If this is a array composite literal, return the
831 // Array_construction_expression structure. Otherwise, return NULL.
832 Fixed_array_construction_expression
*
835 return this->convert
<Fixed_array_construction_expression
,
836 EXPRESSION_FIXED_ARRAY_CONSTRUCTION
>();
839 // If this is a slice composite literal, return the
840 // Slice_construction_expression structure. Otherwise, return NULL.
841 Slice_construction_expression
*
844 return this->convert
<Slice_construction_expression
,
845 EXPRESSION_SLICE_CONSTRUCTION
>();
848 // If this is a map composite literal, return the
849 // Map_construction_expression structure. Otherwise, return NULL.
850 Map_construction_expression
*
853 return this->convert
<Map_construction_expression
,
854 EXPRESSION_MAP_CONSTRUCTION
>();
857 // If this is a type guard expression, return the
858 // Type_guard_expression structure. Otherwise, return NULL.
859 Type_guard_expression
*
860 type_guard_expression()
861 { return this->convert
<Type_guard_expression
, EXPRESSION_TYPE_GUARD
>(); }
863 // If this is a heap expression, returhn the Heap_expression structure.
864 // Otherwise, return NULL.
867 { return this->convert
<Heap_expression
, EXPRESSION_HEAP
>(); }
869 // If this is a receive expression, return the Receive_expression
870 // structure. Otherwise, return NULL.
873 { return this->convert
<Receive_expression
, EXPRESSION_RECEIVE
>(); }
875 // If this is a slice value expression, return the Slice_valiue_expression
876 // structure. Otherwise, return NULL.
877 Slice_value_expression
*
878 slice_value_expression()
879 { return this->convert
<Slice_value_expression
, EXPRESSION_SLICE_VALUE
>(); }
881 // If this is a conditional expression, return the Conditional_expression
882 // structure. Otherwise, return NULL.
883 Conditional_expression
*
884 conditional_expression()
885 { return this->convert
<Conditional_expression
, EXPRESSION_CONDITIONAL
>(); }
887 // If this is a compound expression, return the Compound_expression structure.
888 // Otherwise, return NULL.
890 compound_expression()
891 { return this->convert
<Compound_expression
, EXPRESSION_COMPOUND
>(); }
893 // Return true if this is a composite literal.
895 is_composite_literal() const;
897 // Return true if this is a composite literal which is not constant.
899 is_nonconstant_composite_literal() const;
901 // Return true if this is a variable or temporary variable.
905 // Return true if this is a reference to a local variable.
907 is_local_variable() const;
909 // Return true if two expressions refer to the same variable or
912 is_same_variable(Expression
*, Expression
*);
914 // Make the builtin function descriptor type, so that it can be
917 make_func_descriptor_type();
919 // Traverse an expression.
921 traverse(Expression
**, Traverse
*);
923 // Traverse subexpressions of this expression.
925 traverse_subexpressions(Traverse
*);
927 // Lower an expression. This is called immediately after parsing.
928 // FUNCTION is the function we are in; it will be NULL for an
929 // expression initializing a global variable. INSERTER may be used
930 // to insert statements before the statement or initializer
931 // containing this expression; it is normally used to create
932 // temporary variables. IOTA_VALUE is the value that we should give
933 // to any iota expressions. This function must resolve expressions
934 // which could not be fully parsed into their final form. It
935 // returns the same Expression or a new one.
937 lower(Gogo
* gogo
, Named_object
* function
, Statement_inserter
* inserter
,
939 { return this->do_lower(gogo
, function
, inserter
, iota_value
); }
941 // Flatten an expression. This is called after order_evaluation.
942 // FUNCTION is the function we are in; it will be NULL for an
943 // expression initializing a global variable. INSERTER may be used
944 // to insert statements before the statement or initializer
945 // containing this expression; it is normally used to create
946 // temporary variables. This function must resolve expressions
947 // which could not be fully parsed into their final form. It
948 // returns the same Expression or a new one.
950 flatten(Gogo
* gogo
, Named_object
* function
, Statement_inserter
* inserter
)
951 { return this->do_flatten(gogo
, function
, inserter
); }
953 // Make implicit type conversions explicit.
956 { this->do_add_conversions(); }
958 // Determine the real type of an expression with abstract integer,
959 // floating point, or complex type. TYPE_CONTEXT describes the
962 determine_type(const Type_context
*);
964 // Check types in an expression.
966 check_types(Gogo
* gogo
)
967 { this->do_check_types(gogo
); }
969 // Determine the type when there is no context.
971 determine_type_no_context();
973 // Return the current type of the expression. This may be changed
974 // by determine_type.
977 { return this->do_type(); }
979 // Return a copy of an expression.
982 { return this->do_copy(); }
984 // Return the cost of this statement for inlining purposes.
986 inlining_cost() const
987 { return this->do_inlining_cost(); }
989 // Return whether the expression is addressable--something which may
990 // be used as the operand of the unary & operator.
992 is_addressable() const
993 { return this->do_is_addressable(); }
995 // Note that we are taking the address of this expression. ESCAPES
996 // is true if this address escapes the current function.
998 address_taken(bool escapes
)
999 { this->do_address_taken(escapes
); }
1001 // Note that a nil check must be issued for this expression.
1004 { this->do_issue_nil_check(); }
1006 // Return whether this expression must be evaluated in order
1007 // according to the order of evaluation rules. This is basically
1008 // true of all expressions with side-effects.
1010 must_eval_in_order() const
1011 { return this->do_must_eval_in_order(); }
1013 // Return whether subexpressions of this expression must be
1014 // evaluated in order. This is true of index expressions and
1015 // pointer indirections. This sets *SKIP to the number of
1016 // subexpressions to skip during traversing, as index expressions
1017 // only requiring moving the index, not the array.
1019 must_eval_subexpressions_in_order(int* skip
) const
1022 return this->do_must_eval_subexpressions_in_order(skip
);
1025 // Return the backend representation for this expression.
1027 get_backend(Translate_context
*);
1029 // Return an expression handling any conversions which must be done during
1032 convert_for_assignment(Gogo
*, Type
* lhs_type
, Expression
* rhs
,
1035 // Return an expression converting a value of one interface type to another
1036 // interface type. If FOR_TYPE_GUARD is true this is for a type
1039 convert_interface_to_interface(Type
* lhs_type
,
1040 Expression
* rhs
, bool for_type_guard
,
1043 // Return an expression for a conversion from a non-interface type to an
1044 // interface type. If ON_STACK is true, it can allocate the storage on
1047 convert_type_to_interface(Type
* lhs_type
, Expression
* rhs
,
1048 bool on_stack
, Location
);
1050 // Return a backend expression implementing the comparison LEFT OP RIGHT.
1051 // TYPE is the type of both sides.
1053 comparison(Translate_context
*, Type
* result_type
, Operator op
,
1054 Expression
* left
, Expression
* right
, Location
);
1056 // Return the backend expression for the numeric constant VAL.
1058 backend_numeric_constant_expression(Translate_context
*,
1059 Numeric_constant
* val
);
1061 // Export the expression.
1063 export_expression(Export_function_body
* efb
) const
1064 { this->do_export(efb
); }
1066 // Import an expression. The location should be used for the
1067 // returned expression. Errors should be reported using the
1068 // Import's location method.
1070 import_expression(Import_expression
*, Location
);
1072 // Insert bounds checks for an index expression.
1074 check_bounds(Expression
* val
, Operator
, Expression
* bound
, Runtime::Function
,
1075 Runtime::Function
, Runtime::Function
, Runtime::Function
,
1076 Statement_inserter
*, Location
);
1078 // Return an expression for constructing a direct interface type from a
1081 pack_direct_iface(Type
*, Expression
*, Location
);
1083 // Return an expression of the underlying pointer for a direct interface
1084 // type (the opposite of pack_direct_iface).
1086 unpack_direct_iface(Expression
*, Location
);
1088 // Return an expression representing the type descriptor field of an
1091 get_interface_type_descriptor(Expression
*);
1093 // Look through the expression of a Slice_value_expression's valmem to
1094 // find an call to makeslice.
1095 static std::pair
<Call_expression
*, Temporary_statement
*>
1096 find_makeslice_call(Expression
*);
1098 // Dump an expression to a dump constext.
1100 dump_expression(Ast_dump_context
*) const;
1103 // May be implemented by child class: traverse the expressions.
1105 do_traverse(Traverse
*);
1107 // Return a lowered expression.
1109 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
1112 // Return a flattened expression.
1114 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*)
1117 // Make implicit type conversions explicit.
1119 do_add_conversions()
1122 // Return whether this is a constant expression.
1124 do_is_constant() const
1127 // Return whether this is the zero value of its type.
1129 do_is_zero_value() const
1132 // Return whether this expression can be used as a constant
1135 do_is_static_initializer() const
1138 // Return whether this is a constant expression of numeric type, and
1139 // set the Numeric_constant to the value.
1141 do_numeric_constant_value(Numeric_constant
*) const
1144 // Return whether this is a constant expression of string type, and
1145 // set VAL to the value.
1147 do_string_constant_value(std::string
*) const
1150 // Return whether this is a constant expression of boolean type, and
1151 // set VAL to the value.
1153 do_boolean_constant_value(bool*) const
1156 // Called by the parser if the value is being discarded.
1158 do_discarding_value();
1160 // Child class holds type.
1164 // Child class implements determining type information.
1166 do_determine_type(const Type_context
*) = 0;
1168 // Child class implements type checking if needed.
1170 do_check_types(Gogo
*)
1173 // Child class implements copying.
1177 // Child class implements determining the cost of this statement for
1178 // inlining. The default cost is high, so we only need to define
1179 // this method for expressions that can be inlined.
1181 do_inlining_cost() const
1182 { return 0x100000; }
1184 // Child class implements whether the expression is addressable.
1186 do_is_addressable() const
1189 // Child class implements taking the address of an expression.
1191 do_address_taken(bool)
1194 // Child class implements issuing a nil check if the address is taken.
1196 do_issue_nil_check()
1199 // Child class implements whether this expression must be evaluated
1202 do_must_eval_in_order() const
1205 // Child class implements whether this expressions requires that
1206 // subexpressions be evaluated in order. The child implementation
1207 // may set *SKIP if it should be non-zero.
1209 do_must_eval_subexpressions_in_order(int* /* skip */) const
1212 // Child class implements conversion to backend representation.
1213 virtual Bexpression
*
1214 do_get_backend(Translate_context
*) = 0;
1216 // Child class implements export.
1218 do_export(Export_function_body
*) const;
1220 // For children to call to give an error for an unused value.
1222 unused_value_error();
1224 // For children to call when they detect that they are in error.
1228 // For children to call to report an error conveniently.
1230 report_error(const char*);
1232 // Write a name to export data.
1234 export_name(Export_function_body
* efb
, const Named_object
*);
1236 // Child class implements dumping to a dump context.
1238 do_dump_expression(Ast_dump_context
*) const = 0;
1240 // Varargs lowering creates a slice object (unnamed compiler temp)
1241 // to contain the variable length collection of values. The enum
1242 // below tells the lowering routine whether it can mark that temp
1243 // as non-escaping or not. For general varargs calls it is not always
1244 // safe to stack-allocated the storage, but for specific cases (ex:
1245 // call to append()) it is legal.
1246 enum Slice_storage_escape_disp
1248 SLICE_STORAGE_MAY_ESCAPE
,
1249 SLICE_STORAGE_DOES_NOT_ESCAPE
1253 // Convert to the desired statement classification, or return NULL.
1254 // This is a controlled dynamic cast.
1255 template<typename Expression_class
,
1256 Expression_classification expr_classification
>
1260 return (this->classification_
== expr_classification
1261 ? static_cast<Expression_class
*>(this)
1265 template<typename Expression_class
,
1266 Expression_classification expr_classification
>
1267 const Expression_class
*
1270 return (this->classification_
== expr_classification
1271 ? static_cast<const Expression_class
*>(this)
1276 convert_interface_to_type(Gogo
*, Type
*, Expression
*, Location
);
1279 import_identifier(Import_function_body
*, Location
);
1282 import_expression_without_suffix(Import_expression
*, Location
);
1284 // The expression classification.
1285 Expression_classification classification_
;
1286 // The location in the input file.
1290 // A list of Expressions.
1292 class Expression_list
1299 // Return whether the list is empty.
1302 { return this->entries_
.empty(); }
1304 // Return the number of entries in the list.
1307 { return this->entries_
.size(); }
1309 // Add an entry to the end of the list.
1311 push_back(Expression
* expr
)
1312 { this->entries_
.push_back(expr
); }
1315 append(Expression_list
* add
)
1316 { this->entries_
.insert(this->entries_
.end(), add
->begin(), add
->end()); }
1318 // Reserve space in the list.
1320 reserve(size_t size
)
1321 { this->entries_
.reserve(size
); }
1323 // Traverse the expressions in the list.
1325 traverse(Traverse
*);
1331 // Return true if the list contains an error expression.
1333 contains_error() const;
1335 // Retrieve an element by index.
1338 { return this->entries_
.at(i
); }
1340 // Return the first and last elements.
1343 { return this->entries_
.front(); }
1347 { return this->entries_
.front(); }
1351 { return this->entries_
.back(); }
1355 { return this->entries_
.back(); }
1359 typedef std::vector
<Expression
*>::iterator iterator
;
1360 typedef std::vector
<Expression
*>::const_iterator const_iterator
;
1364 { return this->entries_
.begin(); }
1368 { return this->entries_
.begin(); }
1372 { return this->entries_
.end(); }
1376 { return this->entries_
.end(); }
1381 { this->entries_
.erase(p
); }
1384 std::vector
<Expression
*> entries_
;
1387 // An abstract base class for an expression which is only used by the
1388 // parser, and is lowered in the lowering pass.
1390 class Parser_expression
: public Expression
1393 Parser_expression(Expression_classification classification
,
1395 : Expression(classification
, location
)
1400 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int) = 0;
1406 do_determine_type(const Type_context
*)
1407 { go_unreachable(); }
1410 do_check_types(Gogo
*)
1411 { go_unreachable(); }
1414 do_get_backend(Translate_context
*)
1415 { go_unreachable(); }
1418 // An expression which is simply a variable.
1420 class Var_expression
: public Expression
1423 Var_expression(Named_object
* variable
, Location location
)
1424 : Expression(EXPRESSION_VAR_REFERENCE
, location
),
1428 // Return the variable.
1430 named_object() const
1431 { return this->variable_
; }
1435 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1441 do_determine_type(const Type_context
*);
1448 do_inlining_cost() const
1452 do_export(Export_function_body
*) const;
1455 do_is_addressable() const
1459 do_address_taken(bool);
1462 do_get_backend(Translate_context
*);
1465 do_dump_expression(Ast_dump_context
*) const;
1468 // The variable we are referencing.
1469 Named_object
* variable_
;
1472 // A reference to a variable within an enclosing function.
1474 class Enclosed_var_expression
: public Expression
1477 Enclosed_var_expression(Expression
* reference
, Named_object
* variable
,
1479 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE
, location
),
1480 reference_(reference
), variable_(variable
)
1483 // The reference to the enclosed variable. This will be an indirection of the
1484 // the field stored within closure variable.
1487 { return this->reference_
; }
1489 // The variable being enclosed and referenced.
1492 { return this->variable_
; }
1496 do_traverse(Traverse
*);
1499 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1502 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1506 { return this->reference_
->type(); }
1509 do_determine_type(const Type_context
* context
)
1510 { return this->reference_
->determine_type(context
); }
1517 do_is_addressable() const
1518 { return this->reference_
->is_addressable(); }
1521 do_address_taken(bool escapes
);
1524 do_get_backend(Translate_context
* context
)
1525 { return this->reference_
->get_backend(context
); }
1528 do_dump_expression(Ast_dump_context
*) const;
1531 // The reference to the enclosed variable.
1532 Expression
* reference_
;
1533 // The variable being enclosed.
1534 Named_object
* variable_
;
1537 // A reference to a temporary variable.
1539 class Temporary_reference_expression
: public Expression
1542 Temporary_reference_expression(Temporary_statement
* statement
,
1544 : Expression(EXPRESSION_TEMPORARY_REFERENCE
, location
),
1545 statement_(statement
), is_lvalue_(false)
1548 // The temporary that this expression refers to.
1549 Temporary_statement
*
1551 { return this->statement_
; }
1553 // Indicate that this reference appears on the left hand side of an
1554 // assignment statement.
1557 { this->is_lvalue_
= true; }
1560 do_import(Import_function_body
*, Location
);
1567 do_determine_type(const Type_context
*)
1572 { return make_temporary_reference(this->statement_
, this->location()); }
1575 do_inlining_cost() const
1579 do_export(Export_function_body
*) const;
1582 do_is_addressable() const
1586 do_address_taken(bool);
1589 do_get_backend(Translate_context
*);
1592 do_dump_expression(Ast_dump_context
*) const;
1595 // The statement where the temporary variable is defined.
1596 Temporary_statement
* statement_
;
1597 // Whether this reference appears on the left hand side of an
1598 // assignment statement.
1602 // Set and use a temporary variable.
1604 class Set_and_use_temporary_expression
: public Expression
1607 Set_and_use_temporary_expression(Temporary_statement
* statement
,
1608 Expression
* expr
, Location location
)
1609 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY
, location
),
1610 statement_(statement
), expr_(expr
)
1613 // Return the temporary.
1614 Temporary_statement
*
1616 { return this->statement_
; }
1618 // Return the expression.
1621 { return this->expr_
; }
1625 do_traverse(Traverse
* traverse
)
1626 { return Expression::traverse(&this->expr_
, traverse
); }
1632 do_determine_type(const Type_context
*);
1637 return make_set_and_use_temporary(this->statement_
, this->expr_
,
1642 do_must_eval_in_order() const
1646 do_is_addressable() const
1650 do_address_taken(bool);
1653 do_get_backend(Translate_context
*);
1656 do_dump_expression(Ast_dump_context
*) const;
1659 // The statement where the temporary variable is defined.
1660 Temporary_statement
* statement_
;
1661 // The expression to assign to the temporary.
1665 // A string expression.
1667 class String_expression
: public Expression
1670 String_expression(const std::string
& val
, Type
* type
, Location location
)
1671 : Expression(EXPRESSION_STRING
, location
),
1672 val_(val
), type_(type
)
1677 { return this->val_
; }
1680 do_import(Import_expression
*, Location
);
1684 do_traverse(Traverse
*);
1687 do_is_constant() const
1691 do_is_zero_value() const
1692 { return this->val_
== ""; }
1695 do_is_static_initializer() const
1699 do_string_constant_value(std::string
* val
) const
1709 do_determine_type(const Type_context
*);
1716 do_get_backend(Translate_context
*);
1718 // Write string literal to a string dump.
1720 export_string(String_dump
* exp
, const String_expression
* str
);
1722 // Set the inlining cost a bit high since inlining may cause
1723 // duplicated string literals.
1725 do_inlining_cost() const
1729 do_export(Export_function_body
*) const;
1732 do_dump_expression(Ast_dump_context
*) const;
1735 // The string value. This is immutable.
1736 const std::string val_
;
1737 // The type as determined by context.
1741 // A type conversion expression.
1743 class Type_conversion_expression
: public Expression
1746 Type_conversion_expression(Type
* type
, Expression
* expr
,
1748 : Expression(EXPRESSION_CONVERSION
, location
),
1749 type_(type
), expr_(expr
), may_convert_function_types_(false),
1750 no_copy_(false), no_escape_(false)
1753 // Return the type to which we are converting.
1756 { return this->type_
; }
1758 // Return the expression which we are converting.
1761 { return this->expr_
; }
1763 // Permit converting from one function type to another. This is
1764 // used internally for method expressions.
1766 set_may_convert_function_types()
1768 this->may_convert_function_types_
= true;
1771 // Mark string([]byte) conversion to reuse the backing store
1775 { this->no_copy_
= b
; };
1777 // Import a type conversion expression.
1779 do_import(Import_expression
*, Location
);
1783 do_traverse(Traverse
* traverse
);
1786 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1789 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1792 do_is_constant() const;
1795 do_is_zero_value() const;
1798 do_is_static_initializer() const;
1801 do_numeric_constant_value(Numeric_constant
*) const;
1804 do_string_constant_value(std::string
*) const;
1807 do_boolean_constant_value(bool*) const;
1811 { return this->type_
; }
1814 do_determine_type(const Type_context
*);
1817 do_check_types(Gogo
*);
1823 do_get_backend(Translate_context
* context
);
1826 do_inlining_cost() const;
1829 do_export(Export_function_body
*) const;
1832 do_dump_expression(Ast_dump_context
*) const;
1835 // The type to convert to.
1837 // The expression to convert.
1839 // True if this is permitted to convert function types. This is
1840 // used internally for method expressions.
1841 bool may_convert_function_types_
;
1842 // True if a string([]byte) conversion can reuse the backing store
1843 // without copying. Only used in string([]byte) conversion.
1845 // True if a conversion does not escape. Used in type-to-interface
1846 // conversions and slice-to/from-string conversions.
1850 // An unsafe type conversion, used to pass values to builtin functions.
1852 class Unsafe_type_conversion_expression
: public Expression
1855 Unsafe_type_conversion_expression(Type
* type
, Expression
* expr
,
1857 : Expression(EXPRESSION_UNSAFE_CONVERSION
, location
),
1858 type_(type
), expr_(expr
)
1863 { return this->expr_
; }
1867 do_traverse(Traverse
* traverse
);
1870 do_is_zero_value() const
1871 { return this->expr_
->is_zero_value(); }
1874 do_is_static_initializer() const;
1878 { return this->type_
; }
1881 do_determine_type(const Type_context
*)
1882 { this->expr_
->determine_type_no_context(); }
1888 do_get_backend(Translate_context
*);
1891 do_dump_expression(Ast_dump_context
*) const;
1894 // The type to convert to.
1896 // The expression to convert.
1900 // A Unary expression.
1902 class Unary_expression
: public Expression
1905 Unary_expression(Operator op
, Expression
* expr
, Location location
)
1906 : Expression(EXPRESSION_UNARY
, location
),
1907 op_(op
), escapes_(true), create_temp_(false), is_gc_root_(false),
1908 is_slice_init_(false), expr_(expr
),
1909 issue_nil_check_(NIL_CHECK_DEFAULT
)
1912 // Return the operator.
1915 { return this->op_
; }
1917 // Return the operand.
1920 { return this->expr_
; }
1922 // Record that an address expression does not escape.
1924 set_does_not_escape()
1926 go_assert(this->op_
== OPERATOR_AND
);
1927 this->escapes_
= false;
1930 // Record that this is an address expression which should create a
1931 // temporary variable if necessary. This is used for method calls.
1935 go_assert(this->op_
== OPERATOR_AND
);
1936 this->create_temp_
= true;
1939 // Record that this is an address expression of a GC root, which is a
1940 // mutable composite literal. This used for registering GC variables.
1944 go_assert(this->op_
== OPERATOR_AND
);
1945 this->is_gc_root_
= true;
1948 // Record that this is an address expression of a slice value initializer,
1949 // which is mutable if the values are not copied to the heap.
1953 go_assert(this->op_
== OPERATOR_AND
);
1954 this->is_slice_init_
= true;
1957 // Call the address_taken method on the operand if necessary.
1959 check_operand_address_taken(Gogo
*);
1961 // Apply unary opcode OP to UNC, setting NC. Return true if this
1962 // could be done, false if not. On overflow, issues an error and
1963 // sets *ISSUED_ERROR.
1965 eval_constant(Operator op
, const Numeric_constant
* unc
,
1966 Location
, Numeric_constant
* nc
, bool *issued_error
);
1969 do_import(Import_expression
*, Location
);
1971 // Declare that this deref does or does not require an explicit nil check.
1973 set_requires_nil_check(bool needed
)
1975 go_assert(this->op_
== OPERATOR_MULT
);
1977 this->issue_nil_check_
= NIL_CHECK_NEEDED
;
1979 this->issue_nil_check_
= NIL_CHECK_NOT_NEEDED
;
1984 do_traverse(Traverse
* traverse
)
1985 { return Expression::traverse(&this->expr_
, traverse
); }
1988 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1991 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1994 do_is_constant() const;
1997 do_is_static_initializer() const;
2000 do_numeric_constant_value(Numeric_constant
*) const;
2003 do_boolean_constant_value(bool*) const;
2009 do_determine_type(const Type_context
*);
2012 do_check_types(Gogo
*);
2017 return Expression::make_unary(this->op_
, this->expr_
->copy(),
2022 do_must_eval_subexpressions_in_order(int*) const
2023 { return this->op_
== OPERATOR_MULT
; }
2026 do_is_addressable() const
2027 { return this->op_
== OPERATOR_MULT
; }
2030 do_get_backend(Translate_context
*);
2033 do_inlining_cost() const
2037 do_export(Export_function_body
*) const;
2040 do_dump_expression(Ast_dump_context
*) const;
2043 do_issue_nil_check()
2045 if (this->op_
== OPERATOR_MULT
)
2046 this->set_requires_nil_check(true);
2051 base_is_static_initializer(Expression
*);
2053 // Return a determination as to whether this dereference expression
2054 // requires a nil check.
2055 Nil_check_classification
2056 requires_nil_check(Gogo
*);
2058 // The unary operator to apply.
2060 // Normally true. False if this is an address expression which does
2061 // not escape the current function.
2063 // True if this is an address expression which should create a
2064 // temporary variable if necessary.
2066 // True if this is an address expression for a GC root. A GC root is a
2067 // special struct composite literal that is mutable when addressed, meaning
2068 // it cannot be represented as an immutable_struct in the backend.
2070 // True if this is an address expression for a slice value with an immutable
2071 // initializer. The initializer for a slice's value pointer has an array
2072 // type, meaning it cannot be represented as an immutable_struct in the
2074 bool is_slice_init_
;
2077 // Whether or not to issue a nil check for this expression if its address
2079 Nil_check_classification issue_nil_check_
;
2082 // A binary expression.
2084 class Binary_expression
: public Expression
2087 Binary_expression(Operator op
, Expression
* left
, Expression
* right
,
2089 : Expression(EXPRESSION_BINARY
, location
),
2090 op_(op
), left_(left
), right_(right
), type_(NULL
)
2093 // Return the operator.
2096 { return this->op_
; }
2098 // Return the left hand expression.
2101 { return this->left_
; }
2103 // Return the right hand expression.
2106 { return this->right_
; }
2108 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
2109 // Return true if this could be done, false if not. Issue errors at
2110 // LOCATION as appropriate, and sets *ISSUED_ERROR if it did.
2112 eval_constant(Operator op
, Numeric_constant
* left_nc
,
2113 Numeric_constant
* right_nc
, Location location
,
2114 Numeric_constant
* nc
, bool* issued_error
);
2116 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
2117 // *RESULT. Return true if this could be done, false if not. Issue
2118 // errors at LOCATION as appropriate.
2120 compare_constant(Operator op
, Numeric_constant
* left_nc
,
2121 Numeric_constant
* right_nc
, Location location
,
2125 do_import(Import_expression
*, Location
);
2127 // Report an error if OP can not be applied to TYPE. Return whether
2128 // it can. OTYPE is the type of the other operand.
2130 check_operator_type(Operator op
, Type
* type
, Type
* otype
, Location
);
2132 // Set *RESULT_TYPE to the resulting type when OP is applied to
2133 // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
2134 // success, false on failure.
2136 operation_type(Operator op
, Type
* left_type
, Type
* right_type
,
2137 Type
** result_type
);
2141 do_traverse(Traverse
* traverse
);
2144 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2147 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2150 do_is_constant() const
2151 { return this->left_
->is_constant() && this->right_
->is_constant(); }
2154 do_is_static_initializer() const;
2157 do_numeric_constant_value(Numeric_constant
*) const;
2160 do_boolean_constant_value(bool*) const;
2163 do_discarding_value();
2169 do_determine_type(const Type_context
*);
2172 do_check_types(Gogo
*);
2177 return Expression::make_binary(this->op_
, this->left_
->copy(),
2178 this->right_
->copy(), this->location());
2182 do_get_backend(Translate_context
*);
2185 do_inlining_cost() const
2189 do_export(Export_function_body
*) const;
2192 do_dump_expression(Ast_dump_context
*) const;
2196 cmp_to_bool(Operator op
, int cmp
);
2199 eval_integer(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
2200 Location
, Numeric_constant
*);
2203 eval_float(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
2204 Location
, Numeric_constant
*);
2207 eval_complex(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
2208 Location
, Numeric_constant
*);
2211 compare_integer(const Numeric_constant
*, const Numeric_constant
*, int*);
2214 compare_float(const Numeric_constant
*, const Numeric_constant
*, int*);
2217 compare_complex(const Numeric_constant
*, const Numeric_constant
*, int*);
2220 lower_struct_comparison(Gogo
*, Statement_inserter
*);
2223 lower_array_comparison(Gogo
*, Statement_inserter
*);
2226 lower_interface_value_comparison(Gogo
*, Statement_inserter
*);
2229 lower_compare_to_memcmp(Gogo
*, Statement_inserter
*);
2232 operand_address(Statement_inserter
*, Expression
*);
2234 // The binary operator to apply.
2236 // The left hand side operand.
2238 // The right hand side operand.
2240 // The type of a comparison operation.
2244 // A string concatenation expression. This is a sequence of strings
2245 // added together. It is created when lowering Binary_expression.
2247 class String_concat_expression
: public Expression
2250 String_concat_expression(Expression_list
* exprs
)
2251 : Expression(EXPRESSION_STRING_CONCAT
, exprs
->front()->location()),
2255 // Return the list of string expressions to be concatenated.
2258 { return this->exprs_
; }
2262 do_traverse(Traverse
* traverse
)
2263 { return this->exprs_
->traverse(traverse
); }
2266 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
2270 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2273 do_is_constant() const;
2276 do_is_zero_value() const;
2279 do_is_static_initializer() const;
2285 do_determine_type(const Type_context
*);
2288 do_check_types(Gogo
*);
2292 { return Expression::make_string_concat(this->exprs_
->copy()); }
2295 do_get_backend(Translate_context
*)
2296 { go_unreachable(); }
2299 do_export(Export_function_body
*) const
2300 { go_unreachable(); }
2303 do_dump_expression(Ast_dump_context
*) const;
2306 // The string expressions to concatenate.
2307 Expression_list
* exprs_
;
2310 // A call expression. The go statement needs to dig inside this.
2312 class Call_expression
: public Expression
2315 Call_expression(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
2317 : Expression(EXPRESSION_CALL
, location
),
2318 fn_(fn
), args_(args
), type_(NULL
), call_(NULL
), call_temp_(NULL
)
2319 , expected_result_count_(0), is_varargs_(is_varargs
),
2320 varargs_are_lowered_(false), types_are_determined_(false),
2321 is_deferred_(false), is_concurrent_(false), issued_error_(false),
2322 is_multi_value_arg_(false), is_flattened_(false)
2325 // The function to call.
2328 { return this->fn_
; }
2333 { return this->args_
; }
2335 const Expression_list
*
2337 { return this->args_
; }
2339 // Get the function type.
2341 get_function_type() const;
2343 // Return the number of values this call will return.
2345 result_count() const;
2347 // Return the temporary variable that holds the results. This is
2348 // only valid after the expression has been lowered, and is only
2349 // valid for calls which return multiple results.
2350 Temporary_statement
*
2353 // Set the number of results expected from this call. This is used
2354 // when the call appears in a context that expects multiple results,
2355 // such as a, b = f().
2357 set_expected_result_count(size_t);
2359 // Return whether this is a call to the predeclared function
2362 is_recover_call() const;
2364 // Set the argument for a call to recover.
2366 set_recover_arg(Expression
*);
2368 // Whether the last argument is a varargs argument (f(a...)).
2371 { return this->is_varargs_
; }
2373 // Return whether varargs have already been lowered.
2375 varargs_are_lowered() const
2376 { return this->varargs_are_lowered_
; }
2378 // Note that varargs have already been lowered.
2380 set_varargs_are_lowered()
2381 { this->varargs_are_lowered_
= true; }
2383 // Whether this call is being deferred.
2386 { return this->is_deferred_
; }
2388 // Note that the call is being deferred.
2391 { this->is_deferred_
= true; }
2393 // Whether this call is concurrently executed.
2395 is_concurrent() const
2396 { return this->is_concurrent_
; }
2398 // Note that the call is concurrently executed.
2401 { this->is_concurrent_
= true; }
2403 // We have found an error with this call expression; return true if
2404 // we should report it.
2408 // Whether or not this call contains errors, either in the call or the
2409 // arguments to the call.
2411 is_erroneous_call();
2413 // Whether this call returns multiple results that are used as an
2414 // multi-valued argument.
2416 is_multi_value_arg() const
2417 { return this->is_multi_value_arg_
; }
2419 // Note this call is used as a multi-valued argument.
2421 set_is_multi_value_arg()
2422 { this->is_multi_value_arg_
= true; }
2424 // Whether this is a call to builtin function.
2429 // Convert to a Builtin_call_expression, or return NULL.
2430 inline Builtin_call_expression
*
2431 builtin_call_expression();
2435 do_traverse(Traverse
*);
2438 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2441 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2444 do_discarding_value()
2451 do_determine_type(const Type_context
*);
2454 do_check_types(Gogo
*);
2460 do_must_eval_in_order() const;
2462 virtual Bexpression
*
2463 do_get_backend(Translate_context
*);
2466 do_inlining_cost() const;
2469 do_export(Export_function_body
*) const;
2472 do_is_recover_call() const;
2475 do_set_recover_arg(Expression
*);
2477 // Let a builtin expression change the argument list.
2479 set_args(Expression_list
* args
)
2480 { this->args_
= args
; }
2482 // Let a builtin expression lower varargs.
2484 lower_varargs(Gogo
*, Named_object
* function
, Statement_inserter
* inserter
,
2485 Type
* varargs_type
, size_t param_count
,
2486 Slice_storage_escape_disp escape_disp
);
2488 // Let a builtin expression check whether types have been
2491 determining_types();
2494 export_arguments(Export_function_body
*) const;
2497 do_dump_expression(Ast_dump_context
*) const;
2500 do_add_conversions();
2504 check_argument_type(int, const Type
*, const Type
*, Location
, bool);
2507 intrinsify(Gogo
*, Statement_inserter
*);
2510 interface_method_function(Interface_field_reference_expression
*,
2511 Expression
**, Location
);
2514 set_results(Translate_context
*);
2516 // The function to call.
2518 // The arguments to pass. This may be NULL if there are no
2520 Expression_list
* args_
;
2521 // The type of the expression, to avoid recomputing it.
2523 // The backend expression for the call, used for a call which returns a tuple.
2525 // A temporary variable to store this call if the function returns a tuple.
2526 Temporary_statement
* call_temp_
;
2527 // If not 0, the number of results expected from this call, when
2528 // used in a context that expects multiple values.
2529 size_t expected_result_count_
;
2530 // True if the last argument is a varargs argument (f(a...)).
2532 // True if varargs have already been lowered.
2533 bool varargs_are_lowered_
;
2534 // True if types have been determined.
2535 bool types_are_determined_
;
2536 // True if the call is an argument to a defer statement.
2538 // True if the call is an argument to a go statement.
2539 bool is_concurrent_
;
2540 // True if we reported an error about a mismatch between call
2541 // results and uses. This is to avoid producing multiple errors
2542 // when there are multiple Call_result_expressions.
2544 // True if this call is used as an argument that returns multiple results.
2545 bool is_multi_value_arg_
;
2546 // True if this expression has already been flattened.
2550 // A call expression to a builtin function.
2552 class Builtin_call_expression
: public Call_expression
2555 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
2556 bool is_varargs
, Location location
);
2558 // The builtin functions.
2559 enum Builtin_function_code
2563 // Predeclared builtin functions.
2580 // Builtin functions from the unsafe package.
2586 Builtin_function_code
2588 { return this->code_
; }
2590 // This overrides Call_expression::is_builtin.
2595 // Return whether EXPR, of array type, is a constant if passed to
2598 array_len_is_constant(Expression
* expr
);
2601 flatten_append(Gogo
*, Named_object
*, Statement_inserter
*, Expression
*,
2605 // This overrides Call_expression::do_lower.
2607 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2610 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2613 do_is_constant() const;
2616 do_numeric_constant_value(Numeric_constant
*) const;
2619 do_discarding_value();
2625 do_determine_type(const Type_context
*);
2628 do_check_types(Gogo
*);
2634 do_get_backend(Translate_context
*);
2637 do_inlining_cost() const
2641 do_export(Export_function_body
*) const;
2644 do_is_recover_call() const;
2647 do_set_recover_arg(Expression
*);
2657 real_imag_type(Type
*);
2660 complex_type(Type
*);
2663 lower_make(Statement_inserter
*);
2666 check_int_value(Expression
*, bool is_length
, bool* small
);
2668 // A pointer back to the general IR structure. This avoids a global
2669 // variable, or passing it around everywhere.
2671 // The builtin function being called.
2672 Builtin_function_code code_
;
2673 // Used to stop endless loops when the length of an array uses len
2674 // or cap of the array itself.
2676 // Whether the argument is set for calls to BUILTIN_RECOVER.
2677 bool recover_arg_is_set_
;
2680 inline Builtin_call_expression
*
2681 Call_expression::builtin_call_expression()
2683 return (this->is_builtin()
2684 ? static_cast<Builtin_call_expression
*>(this)
2688 // A single result from a call which returns multiple results.
2690 class Call_result_expression
: public Expression
2693 Call_result_expression(Call_expression
* call
, unsigned int index
)
2694 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
2695 call_(call
), index_(index
)
2700 { return this->call_
; }
2704 { return this->index_
; }
2708 do_traverse(Traverse
*);
2714 do_determine_type(const Type_context
*);
2717 do_check_types(Gogo
*);
2722 return new Call_result_expression(this->call_
->call_expression(),
2727 do_must_eval_in_order() const
2731 do_get_backend(Translate_context
*);
2734 do_dump_expression(Ast_dump_context
*) const;
2737 // The underlying call expression.
2739 // Which result we want.
2740 unsigned int index_
;
2743 // An expression which represents a pointer to a function.
2745 class Func_expression
: public Expression
2748 Func_expression(Named_object
* function
, Expression
* closure
,
2750 : Expression(EXPRESSION_FUNC_REFERENCE
, location
),
2751 function_(function
), closure_(closure
),
2752 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS
)
2755 // Return the object associated with the function.
2757 named_object() const
2758 { return this->function_
; }
2760 // Return the closure for this function. This will return NULL if
2761 // the function has no closure, which is the normal case.
2764 { return this->closure_
; }
2766 // Return whether this is a reference to a runtime function.
2768 is_runtime_function() const
2769 { return this->runtime_code_
!= Runtime::NUMBER_OF_FUNCTIONS
; }
2771 // Return the runtime code for this function expression.
2772 // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
2773 // runtime function.
2775 runtime_code() const
2776 { return this->runtime_code_
; }
2778 // Set the runtime code for this function expression.
2780 set_runtime_code(Runtime::Function code
)
2781 { this->runtime_code_
= code
; }
2783 // Return a backend expression for the code of a function.
2785 get_code_pointer(Gogo
*, Named_object
* function
, Location loc
);
2789 do_traverse(Traverse
*);
2795 do_determine_type(const Type_context
*)
2797 if (this->closure_
!= NULL
)
2798 this->closure_
->determine_type_no_context();
2804 return Expression::make_func_reference(this->function_
,
2805 (this->closure_
== NULL
2807 : this->closure_
->copy()),
2812 do_get_backend(Translate_context
*);
2815 do_inlining_cost() const;
2818 do_export(Export_function_body
*) const;
2821 do_dump_expression(Ast_dump_context
*) const;
2824 // The function itself.
2825 Named_object
* function_
;
2826 // A closure. This is normally NULL. For a nested function, it may
2827 // be a struct holding pointers to all the variables referenced by
2828 // this function and defined in enclosing functions.
2829 Expression
* closure_
;
2830 // The runtime code for the referenced function.
2831 Runtime::Function runtime_code_
;
2834 // A function descriptor. A function descriptor is a struct with a
2835 // single field pointing to the function code. This is used for
2836 // functions without closures.
2838 class Func_descriptor_expression
: public Expression
2841 Func_descriptor_expression(Named_object
* fn
);
2843 // Make the function descriptor type, so that it can be converted.
2845 make_func_descriptor_type();
2849 do_traverse(Traverse
*);
2855 do_determine_type(const Type_context
*)
2860 { return Expression::make_func_descriptor(this->fn_
); }
2863 do_is_addressable() const
2867 do_get_backend(Translate_context
*);
2870 do_dump_expression(Ast_dump_context
* context
) const;
2873 // The type of all function descriptors.
2874 static Type
* descriptor_type
;
2876 // The function for which this is the descriptor.
2878 // The descriptor variable.
2882 // A reference to an unknown name.
2884 class Unknown_expression
: public Parser_expression
2887 Unknown_expression(Named_object
* named_object
, Location location
)
2888 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE
, location
),
2889 named_object_(named_object
), no_error_message_(false)
2892 // The associated named object.
2894 named_object() const
2895 { return this->named_object_
; }
2897 // The name of the identifier which was unknown.
2901 // Call this to indicate that we should not give an error if this
2902 // name is never defined. This is used to avoid knock-on errors
2903 // during an erroneous parse.
2905 set_no_error_message()
2906 { this->no_error_message_
= true; }
2910 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2914 { return new Unknown_expression(this->named_object_
, this->location()); }
2917 do_dump_expression(Ast_dump_context
*) const;
2920 // The unknown name.
2921 Named_object
* named_object_
;
2922 // True if we should not give errors if this is undefined. This is
2923 // used if there was a parse failure.
2924 bool no_error_message_
;
2927 // An index expression. This is lowered to an array index, a string
2928 // index, or a map index.
2930 class Index_expression
: public Parser_expression
2933 Index_expression(Expression
* left
, Expression
* start
, Expression
* end
,
2934 Expression
* cap
, Location location
)
2935 : Parser_expression(EXPRESSION_INDEX
, location
),
2936 left_(left
), start_(start
), end_(end
), cap_(cap
)
2939 // Dump an index expression, i.e. an expression of the form
2940 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
2942 dump_index_expression(Ast_dump_context
*, const Expression
* expr
,
2943 const Expression
* start
, const Expression
* end
,
2944 const Expression
* cap
);
2948 do_traverse(Traverse
*);
2951 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2956 return new Index_expression(this->left_
->copy(), this->start_
->copy(),
2959 : this->end_
->copy()),
2962 : this->cap_
->copy()),
2966 // This shouldn't be called--we don't know yet.
2968 do_must_eval_subexpressions_in_order(int*) const
2969 { go_unreachable(); }
2972 do_dump_expression(Ast_dump_context
*) const;
2975 do_issue_nil_check()
2976 { this->left_
->issue_nil_check(); }
2978 // The expression being indexed.
2982 // The second index. This is NULL for an index, non-NULL for a
2985 // The capacity argument. This is NULL for indices and slices that use the
2986 // default capacity, non-NULL for indices and slices that specify the
2991 // An array index. This is used for both indexing and slicing.
2993 class Array_index_expression
: public Expression
2996 Array_index_expression(Expression
* array
, Expression
* start
,
2997 Expression
* end
, Expression
* cap
, Location location
)
2998 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
2999 array_(array
), start_(start
), end_(end
), cap_(cap
), type_(NULL
),
3000 is_lvalue_(false), needs_bounds_check_(true), is_flattened_(false)
3003 // Return the array.
3006 { return this->array_
; }
3010 { return this->array_
; }
3012 // Return the index of a simple index expression, or the start index
3013 // of a slice expression.
3016 { return this->start_
; }
3020 { return this->start_
; }
3022 // Return the end index of a slice expression. This is NULL for a
3023 // simple index expression.
3026 { return this->end_
; }
3030 { return this->end_
; }
3032 // Return whether this array index expression appears in an lvalue
3033 // (left hand side of assignment) context.
3036 { return this->is_lvalue_
; }
3038 // Update this array index expression to indicate that it appears
3039 // in a left-hand-side or lvalue context.
3042 { this->is_lvalue_
= true; }
3045 set_needs_bounds_check(bool b
)
3046 { this->needs_bounds_check_
= b
; }
3050 do_traverse(Traverse
*);
3053 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3059 do_determine_type(const Type_context
*);
3062 do_check_types(Gogo
*);
3067 Expression
* ret
= Expression::make_array_index(this->array_
->copy(),
3068 this->start_
->copy(),
3071 : this->end_
->copy()),
3074 : this->cap_
->copy()),
3076 ret
->array_index_expression()->set_needs_bounds_check(this->needs_bounds_check_
);
3081 do_must_eval_subexpressions_in_order(int* skip
) const;
3084 do_is_addressable() const;
3087 do_address_taken(bool escapes
);
3090 do_issue_nil_check()
3091 { this->array_
->issue_nil_check(); }
3094 do_get_backend(Translate_context
*);
3097 do_inlining_cost() const
3098 { return this->end_
!= NULL
? 2 : 1; }
3101 do_export(Export_function_body
*) const;
3104 do_dump_expression(Ast_dump_context
*) const;
3107 // The array we are getting a value from.
3109 // The start or only index.
3111 // The end index of a slice. This may be NULL for a simple array
3112 // index, or it may be a nil expression for the length of the array.
3114 // The capacity argument of a slice. This may be NULL for an array index or
3117 // The type of the expression.
3119 // Whether expr appears in an lvalue context.
3121 // Whether bounds check is needed.
3122 bool needs_bounds_check_
;
3123 // Whether this has already been flattened.
3127 // A string index. This is used for both indexing and slicing.
3129 class String_index_expression
: public Expression
3132 String_index_expression(Expression
* string
, Expression
* start
,
3133 Expression
* end
, Location location
)
3134 : Expression(EXPRESSION_STRING_INDEX
, location
),
3135 string_(string
), start_(start
), end_(end
), is_flattened_(false)
3138 // Return the string being indexed.
3141 { return this->string_
; }
3143 // Return the index of a simple index expression, or the start index
3144 // of a slice expression.
3147 { return this->start_
; }
3149 // Return the end index of a slice expression. This is NULL for a
3150 // simple index expression.
3153 { return this->end_
; }
3157 do_traverse(Traverse
*);
3160 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3166 do_determine_type(const Type_context
*);
3169 do_check_types(Gogo
*);
3174 return Expression::make_string_index(this->string_
->copy(),
3175 this->start_
->copy(),
3178 : this->end_
->copy()),
3183 do_must_eval_subexpressions_in_order(int*) const
3187 do_get_backend(Translate_context
*);
3190 do_inlining_cost() const
3191 { return this->end_
!= NULL
? 2 : 1; }
3194 do_export(Export_function_body
*) const;
3197 do_dump_expression(Ast_dump_context
*) const;
3200 // The string we are getting a value from.
3201 Expression
* string_
;
3202 // The start or only index.
3204 // The end index of a slice. This may be NULL for a single index,
3205 // or it may be a nil expression for the length of the string.
3207 // Whether this has already been flattened.
3211 // An index into a map.
3213 class Map_index_expression
: public Expression
3216 Map_index_expression(Expression
* map
, Expression
* index
,
3218 : Expression(EXPRESSION_MAP_INDEX
, location
),
3219 map_(map
), index_(index
), value_pointer_(NULL
)
3225 { return this->map_
; }
3229 { return this->map_
; }
3231 // Return the index.
3234 { return this->index_
; }
3238 { return this->index_
; }
3240 // Get the type of the map being indexed.
3242 get_map_type() const;
3244 // Return an expression for the map index. This returns an
3245 // expression that evaluates to a pointer to a value in the map. If
3246 // the key is not present in the map, this will return a pointer to
3249 get_value_pointer(Gogo
*);
3253 do_traverse(Traverse
*);
3256 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3262 do_determine_type(const Type_context
*);
3265 do_check_types(Gogo
*);
3270 return Expression::make_map_index(this->map_
->copy(),
3271 this->index_
->copy(),
3276 do_must_eval_subexpressions_in_order(int*) const
3279 // A map index expression is an lvalue but it is not addressable.
3282 do_get_backend(Translate_context
*);
3285 do_inlining_cost() const
3289 do_export(Export_function_body
*) const;
3292 do_dump_expression(Ast_dump_context
*) const;
3295 do_add_conversions();
3298 // The map we are looking into.
3302 // A pointer to the value at this index.
3303 Expression
* value_pointer_
;
3306 // An expression which represents a method bound to its first
3309 class Bound_method_expression
: public Expression
3312 Bound_method_expression(Expression
* expr
, const Method
*method
,
3313 Named_object
* function
, Location location
)
3314 : Expression(EXPRESSION_BOUND_METHOD
, location
),
3315 expr_(expr
), expr_type_(NULL
), method_(method
), function_(function
)
3318 // Return the object which is the first argument.
3321 { return this->expr_
; }
3323 // Return the implicit type of the first argument. This will be
3324 // non-NULL when using a method from an anonymous field without
3325 // using an explicit stub.
3327 first_argument_type() const
3328 { return this->expr_type_
; }
3330 // Return the method.
3333 { return this->method_
; }
3335 // Return the function to call.
3338 { return this->function_
; }
3340 // Set the implicit type of the expression.
3342 set_first_argument_type(Type
* type
)
3343 { this->expr_type_
= type
; }
3345 // Create a thunk to call FUNCTION, for METHOD, when it is used as
3346 // part of a method value.
3347 static Named_object
*
3348 create_thunk(Gogo
*, const Method
* method
, Named_object
* function
);
3352 do_traverse(Traverse
*);
3355 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3361 do_determine_type(const Type_context
*);
3364 do_check_types(Gogo
*);
3369 return new Bound_method_expression(this->expr_
->copy(), this->method_
,
3370 this->function_
, this->location());
3374 do_get_backend(Translate_context
*)
3375 { go_unreachable(); }
3378 do_dump_expression(Ast_dump_context
*) const;
3381 // A mapping from method functions to the thunks we have created for
3383 typedef Unordered_map(Named_object
*, Named_object
*) Method_value_thunks
;
3384 static Method_value_thunks method_value_thunks
;
3386 // The object used to find the method. This is passed to the method
3387 // as the first argument.
3389 // The implicit type of the object to pass to the method. This is
3390 // NULL in the normal case, non-NULL when using a method from an
3391 // anonymous field which does not require a stub.
3394 const Method
* method_
;
3395 // The function to call. This is not the same as
3396 // method_->named_object() when the method has a stub. This will be
3397 // the real function rather than the stub.
3398 Named_object
* function_
;
3401 // A reference to a field in a struct.
3403 class Field_reference_expression
: public Expression
3406 Field_reference_expression(Expression
* expr
, unsigned int field_index
,
3408 : Expression(EXPRESSION_FIELD_REFERENCE
, location
),
3409 expr_(expr
), field_index_(field_index
), implicit_(false), called_fieldtrack_(false)
3412 // Return the struct expression.
3415 { return this->expr_
; }
3417 // Return the field index.
3420 { return this->field_index_
; }
3422 // Return whether this node was implied by an anonymous field.
3425 { return this->implicit_
; }
3428 set_implicit(bool implicit
)
3429 { this->implicit_
= implicit
; }
3431 // Set the struct expression. This is used when parsing.
3433 set_struct_expression(Expression
* expr
)
3435 go_assert(this->expr_
== NULL
);
3441 do_traverse(Traverse
* traverse
)
3442 { return Expression::traverse(&this->expr_
, traverse
); }
3445 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
3451 do_determine_type(const Type_context
*)
3452 { this->expr_
->determine_type_no_context(); }
3455 do_check_types(Gogo
*);
3460 return Expression::make_field_reference(this->expr_
->copy(),
3466 do_is_addressable() const
3467 { return this->expr_
->is_addressable(); }
3470 do_address_taken(bool escapes
)
3471 { this->expr_
->address_taken(escapes
); }
3474 do_issue_nil_check()
3475 { this->expr_
->issue_nil_check(); }
3478 do_get_backend(Translate_context
*);
3481 do_dump_expression(Ast_dump_context
*) const;
3484 // The expression we are looking into. This should have a type of
3487 // The zero-based index of the field we are retrieving.
3488 unsigned int field_index_
;
3489 // Whether this node was emitted implicitly for an embedded field,
3490 // that is, expr_ is not the expr_ of the original user node.
3492 // Whether we have already emitted a fieldtrack call.
3493 bool called_fieldtrack_
;
3496 // A reference to a field of an interface.
3498 class Interface_field_reference_expression
: public Expression
3501 Interface_field_reference_expression(Expression
* expr
,
3502 const std::string
& name
,
3504 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE
, location
),
3505 expr_(expr
), name_(name
)
3508 // Return the expression for the interface object.
3511 { return this->expr_
; }
3513 // Return the name of the method to call.
3516 { return this->name_
; }
3518 // Create a thunk to call the method NAME in TYPE when it is used as
3519 // part of a method value.
3520 static Named_object
*
3521 create_thunk(Gogo
*, Interface_type
* type
, const std::string
& name
);
3523 // Return an expression for the pointer to the function to call.
3527 // Return an expression for the first argument to pass to the interface
3528 // function. This is the real object associated with the interface object.
3530 get_underlying_object();
3534 do_traverse(Traverse
* traverse
);
3537 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3543 do_determine_type(const Type_context
*);
3546 do_check_types(Gogo
*);
3551 return Expression::make_interface_field_reference(this->expr_
->copy(),
3557 do_get_backend(Translate_context
*);
3560 do_dump_expression(Ast_dump_context
*) const;
3563 // A mapping from interface types to a list of thunks we have
3564 // created for methods.
3565 typedef std::vector
<std::pair
<std::string
, Named_object
*> > Method_thunks
;
3566 typedef Unordered_map(Interface_type
*, Method_thunks
*)
3567 Interface_method_thunks
;
3568 static Interface_method_thunks interface_method_thunks
;
3570 // The expression for the interface object. This should have a type
3571 // of interface or pointer to interface.
3573 // The field we are retrieving--the name of the method.
3577 // Implement the builtin function new.
3579 class Allocation_expression
: public Expression
3582 Allocation_expression(Type
* type
, Location location
)
3583 : Expression(EXPRESSION_ALLOCATION
, location
),
3584 type_(type
), allocate_on_stack_(false),
3589 set_allocate_on_stack()
3590 { this->allocate_on_stack_
= true; }
3592 // Mark that the allocated memory doesn't need zeroing.
3595 { this->no_zero_
= true; }
3599 do_traverse(Traverse
*);
3605 do_determine_type(const Type_context
*)
3609 do_check_types(Gogo
*);
3615 do_get_backend(Translate_context
*);
3618 do_dump_expression(Ast_dump_context
*) const;
3621 // The type we are allocating.
3623 // Whether or not this is a stack allocation.
3624 bool allocate_on_stack_
;
3625 // Whether we don't need to zero the allocated memory.
3629 // A general composite literal. This is lowered to a type specific
3632 class Composite_literal_expression
: public Parser_expression
3635 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
3636 Expression_list
* vals
, bool all_are_names
,
3638 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
3639 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
),
3640 all_are_names_(all_are_names
), key_path_(std::vector
<bool>(depth
))
3644 // Mark the DEPTH entry of KEY_PATH as containing a key.
3646 update_key_path(size_t depth
)
3648 go_assert(depth
< this->key_path_
.size());
3649 this->key_path_
[depth
] = true;
3654 do_traverse(Traverse
* traverse
);
3657 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
3663 do_dump_expression(Ast_dump_context
*) const;
3667 lower_struct(Gogo
*, Type
*);
3673 make_array(Type
*, const std::vector
<unsigned long>*, Expression_list
*);
3676 lower_map(Gogo
*, Named_object
*, Statement_inserter
*, Type
*);
3678 // The type of the composite literal.
3680 // The depth within a list of composite literals within a composite
3681 // literal, when the type is omitted.
3683 // The values to put in the composite literal.
3684 Expression_list
* vals_
;
3685 // If this is true, then VALS_ is a list of pairs: a key and a
3686 // value. In an array initializer, a missing key will be NULL.
3688 // If this is true, then HAS_KEYS_ is true, and every key is a
3689 // simple identifier.
3690 bool all_are_names_
;
3691 // A complement to DEPTH that indicates for each level starting from 0 to
3692 // DEPTH-1 whether or not this composite literal is nested inside of key or
3693 // a value. This is used to decide which type to use when given a map literal
3694 // with omitted key types.
3695 std::vector
<bool> key_path_
;
3698 // Helper/mixin class for struct and array construction expressions;
3699 // encapsulates a list of values plus an optional traversal order
3700 // recording the order in which the values should be visited.
3702 class Ordered_value_list
3705 Ordered_value_list(Expression_list
* vals
)
3706 : vals_(vals
), traverse_order_(NULL
)
3711 { return this->vals_
; }
3714 traverse_vals(Traverse
* traverse
);
3716 // Get the traversal order (may be NULL)
3717 std::vector
<unsigned long>*
3719 { return traverse_order_
; }
3721 // Set the traversal order, used to ensure that we implement the
3722 // order of evaluation rules. Takes ownership of the argument.
3724 set_traverse_order(std::vector
<unsigned long>* traverse_order
)
3725 { this->traverse_order_
= traverse_order
; }
3728 // The list of values, in order of the fields in the struct or in
3729 // order of indices in an array. A NULL value of vals_ means that
3730 // all fields/slots should be zero-initialized; a single NULL entry
3731 // in the list means that the corresponding field or array slot
3732 // should be zero-initialized.
3733 Expression_list
* vals_
;
3734 // If not NULL, the order in which to traverse vals_. This is used
3735 // so that we implement the order of evaluation rules correctly.
3736 std::vector
<unsigned long>* traverse_order_
;
3739 // Construct a struct.
3741 class Struct_construction_expression
: public Expression
,
3742 public Ordered_value_list
3745 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
3747 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
3748 Ordered_value_list(vals
),
3752 // Return whether this is a constant initializer.
3754 is_constant_struct() const;
3758 do_traverse(Traverse
* traverse
);
3761 do_is_zero_value() const;
3764 do_is_static_initializer() const;
3768 { return this->type_
; }
3771 do_determine_type(const Type_context
*);
3774 do_check_types(Gogo
*);
3780 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3783 do_get_backend(Translate_context
*);
3786 do_export(Export_function_body
*) const;
3789 do_dump_expression(Ast_dump_context
*) const;
3792 do_add_conversions();
3795 // The type of the struct to construct.
3799 // Construct an array. This class is not used directly; instead we
3800 // use the child classes, Fixed_array_construction_expression and
3801 // Slice_construction_expression.
3803 class Array_construction_expression
: public Expression
,
3804 public Ordered_value_list
3807 Array_construction_expression(Expression_classification classification
,
3809 const std::vector
<unsigned long>* indexes
,
3810 Expression_list
* vals
, Location location
)
3811 : Expression(classification
, location
),
3812 Ordered_value_list(vals
),
3813 type_(type
), indexes_(indexes
)
3814 { go_assert(indexes
== NULL
|| indexes
->size() == vals
->size()); }
3817 // Return whether this is a constant initializer.
3819 is_constant_array() const;
3821 // Return the number of elements.
3823 element_count() const
3824 { return this->vals() == NULL
? 0 : this->vals()->size(); }
3828 do_traverse(Traverse
* traverse
);
3831 do_is_zero_value() const;
3834 do_is_static_initializer() const;
3838 { return this->type_
; }
3841 do_determine_type(const Type_context
*);
3844 do_check_types(Gogo
*);
3847 do_export(Export_function_body
*) const;
3850 const std::vector
<unsigned long>*
3852 { return this->indexes_
; }
3855 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3857 // Get the backend constructor for the array values.
3859 get_constructor(Translate_context
* context
, Btype
* btype
);
3862 do_dump_expression(Ast_dump_context
*) const;
3865 dump_slice_storage_expression(Ast_dump_context
*) const { }
3868 do_add_conversions();
3871 // The type of the array to construct.
3873 // The list of indexes into the array, one for each value. This may
3874 // be NULL, in which case the indexes start at zero and increment.
3875 const std::vector
<unsigned long>* indexes_
;
3878 // Construct a fixed array.
3880 class Fixed_array_construction_expression
:
3881 public Array_construction_expression
3884 Fixed_array_construction_expression(Type
* type
,
3885 const std::vector
<unsigned long>* indexes
,
3886 Expression_list
* vals
, Location location
);
3893 do_get_backend(Translate_context
*);
3896 // Construct a slice.
3898 class Slice_construction_expression
: public Array_construction_expression
3901 Slice_construction_expression(Type
* type
,
3902 const std::vector
<unsigned long>* indexes
,
3903 Expression_list
* vals
, Location location
);
3906 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3908 // Record that the storage for this slice (e.g. vals) cannot escape,
3909 // hence it can be stack-allocated.
3911 set_storage_does_not_escape()
3913 this->storage_escapes_
= false;
3917 // Note that taking the address of a slice literal is invalid.
3920 do_traverse(Traverse
* traverse
);
3926 do_get_backend(Translate_context
*);
3929 dump_slice_storage_expression(Ast_dump_context
* ast_dump_context
) const;
3931 // Create an array value for the constructed slice. Invoked during
3932 // flattening if slice storage does not escape, otherwise invoked
3933 // later on during do_get_backend().
3938 // The type of the values in this slice.
3940 // Array value expression, optionally filled in during flattening.
3941 Expression
* array_val_
;
3942 // Slice storage expression, optionally filled in during flattening.
3943 Expression
* slice_storage_
;
3944 // Normally true. Can be set to false if we know that the resulting
3945 // storage for the slice cannot escape.
3946 bool storage_escapes_
;
3951 class Map_construction_expression
: public Expression
3954 Map_construction_expression(Type
* type
, Expression_list
* vals
,
3956 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
3957 type_(type
), vals_(vals
), element_type_(NULL
), constructor_temp_(NULL
)
3958 { go_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
3962 { return this->vals_
; }
3966 do_traverse(Traverse
* traverse
);
3969 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
3973 { return this->type_
; }
3976 do_determine_type(const Type_context
*);
3979 do_check_types(Gogo
*);
3985 do_get_backend(Translate_context
*);
3988 do_export(Export_function_body
*) const;
3991 do_dump_expression(Ast_dump_context
*) const;
3994 do_add_conversions();
3997 // The type of the map to construct.
3999 // The list of values.
4000 Expression_list
* vals_
;
4001 // The type of the key-value pair struct for each map element.
4002 Struct_type
* element_type_
;
4003 // A temporary reference to the variable storing the constructor initializer.
4004 Temporary_statement
* constructor_temp_
;
4007 // A type guard expression.
4009 class Type_guard_expression
: public Expression
4012 Type_guard_expression(Expression
* expr
, Type
* type
, Location location
)
4013 : Expression(EXPRESSION_TYPE_GUARD
, location
),
4014 expr_(expr
), type_(type
)
4017 // Return the expression to convert.
4020 { return this->expr_
; }
4022 // Return the type to which to convert.
4025 { return this->type_
; }
4029 do_traverse(Traverse
* traverse
);
4032 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
4036 { return this->type_
; }
4039 do_determine_type(const Type_context
*)
4040 { this->expr_
->determine_type_no_context(); }
4043 do_check_types(Gogo
*);
4049 do_get_backend(Translate_context
*);
4052 do_dump_expression(Ast_dump_context
*) const;
4055 // The expression to convert.
4057 // The type to which to convert.
4061 // Class Heap_expression.
4063 // When you take the address of an escaping expression, it is allocated
4064 // on the heap. This class implements that.
4066 class Heap_expression
: public Expression
4069 Heap_expression(Expression
* expr
, Location location
)
4070 : Expression(EXPRESSION_HEAP
, location
),
4071 expr_(expr
), allocate_on_stack_(false)
4076 { return this->expr_
; }
4079 set_allocate_on_stack()
4080 { this->allocate_on_stack_
= true; }
4084 do_traverse(Traverse
* traverse
)
4085 { return Expression::traverse(&this->expr_
, traverse
); }
4090 do_determine_type(const Type_context
*)
4091 { this->expr_
->determine_type_no_context(); }
4096 return Expression::make_heap_expression(this->expr_
->copy(),
4101 do_get_backend(Translate_context
*);
4103 // We only export global objects, and the parser does not generate
4104 // this in global scope.
4106 do_export(Export_function_body
*) const
4107 { go_unreachable(); }
4110 do_dump_expression(Ast_dump_context
*) const;
4113 // The expression which is being put on the heap.
4115 // Whether or not this is a stack allocation.
4116 bool allocate_on_stack_
;
4119 // A receive expression.
4121 class Receive_expression
: public Expression
4124 Receive_expression(Expression
* channel
, Location location
)
4125 : Expression(EXPRESSION_RECEIVE
, location
),
4126 channel_(channel
), temp_receiver_(NULL
)
4129 // Return the channel.
4132 { return this->channel_
; }
4135 do_import(Import_expression
*, Location
);
4139 do_traverse(Traverse
* traverse
)
4140 { return Expression::traverse(&this->channel_
, traverse
); }
4143 do_discarding_value()
4150 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
4153 do_determine_type(const Type_context
*)
4154 { this->channel_
->determine_type_no_context(); }
4157 do_check_types(Gogo
*);
4162 return Expression::make_receive(this->channel_
->copy(), this->location());
4166 do_inlining_cost() const
4170 do_must_eval_in_order() const
4174 do_get_backend(Translate_context
*);
4177 do_export(Export_function_body
*) const;
4180 do_dump_expression(Ast_dump_context
*) const;
4183 // The channel from which we are receiving.
4184 Expression
* channel_
;
4185 // A temporary reference to the variable storing the received data.
4186 Temporary_statement
* temp_receiver_
;
4189 // An expression that represents a slice value: a struct with value pointer,
4190 // length, and capacity fields.
4192 class Slice_value_expression
: public Expression
4195 Slice_value_expression(Type
* type
, Expression
* valmem
, Expression
* len
,
4196 Expression
* cap
, Location location
)
4197 : Expression(EXPRESSION_SLICE_VALUE
, location
),
4198 type_(type
), valmem_(valmem
), len_(len
), cap_(cap
)
4201 // The memory holding the values in the slice. The type should be a
4202 // pointer to the element value of the slice.
4205 { return this->valmem_
; }
4209 do_traverse(Traverse
*);
4213 { return this->type_
; }
4216 do_determine_type(const Type_context
*)
4223 do_get_backend(Translate_context
* context
);
4226 do_dump_expression(Ast_dump_context
*) const;
4229 // The type of the slice value.
4231 // The memory holding the values in the slice.
4232 Expression
* valmem_
;
4233 // The length of the slice.
4235 // The capacity of the slice.
4239 // Conditional expressions.
4241 class Conditional_expression
: public Expression
4244 Conditional_expression(Expression
* cond
, Expression
* then_expr
,
4245 Expression
* else_expr
, Location location
)
4246 : Expression(EXPRESSION_CONDITIONAL
, location
),
4247 cond_(cond
), then_(then_expr
), else_(else_expr
)
4252 { return this->cond_
; }
4256 do_traverse(Traverse
*);
4262 do_determine_type(const Type_context
*);
4267 return new Conditional_expression(this->cond_
->copy(), this->then_
->copy(),
4268 this->else_
->copy(), this->location());
4272 do_get_backend(Translate_context
* context
);
4275 do_dump_expression(Ast_dump_context
*) const;
4278 // The condition to be checked.
4280 // The expression to execute if the condition is true.
4282 // The expression to execute if the condition is false.
4286 // Compound expressions.
4288 class Compound_expression
: public Expression
4291 Compound_expression(Expression
* init
, Expression
* expr
, Location location
)
4292 : Expression(EXPRESSION_COMPOUND
, location
), init_(init
), expr_(expr
)
4297 { return this->init_
; }
4301 do_traverse(Traverse
*);
4307 do_determine_type(const Type_context
*);
4312 return new Compound_expression(this->init_
->copy(), this->expr_
->copy(),
4317 do_get_backend(Translate_context
* context
);
4320 do_dump_expression(Ast_dump_context
*) const;
4323 // The expression that is evaluated first and discarded.
4325 // The expression that is evaluated and returned.
4329 // A backend expression. This is a backend expression wrapped in an
4330 // Expression, for convenience during backend generation.
4332 class Backend_expression
: public Expression
4335 Backend_expression(Bexpression
* bexpr
, Type
* type
, Location location
)
4336 : Expression(EXPRESSION_BACKEND
, location
), bexpr_(bexpr
), type_(type
)
4341 do_traverse(Traverse
*);
4343 // For now these are always valid static initializers. If that
4344 // changes we can change this.
4346 do_is_static_initializer() const
4351 { return this->type_
; }
4354 do_determine_type(const Type_context
*)
4361 do_get_backend(Translate_context
*)
4362 { return this->bexpr_
; }
4365 do_dump_expression(Ast_dump_context
*) const;
4368 // The backend expression we are wrapping.
4369 Bexpression
* bexpr_
;
4370 // The type of the expression;
4374 // A numeric constant. This is used both for untyped constants and
4375 // for constants that have a type.
4377 class Numeric_constant
4381 : classification_(NC_INVALID
), type_(NULL
)
4384 ~Numeric_constant();
4386 Numeric_constant(const Numeric_constant
&);
4388 Numeric_constant
& operator=(const Numeric_constant
&);
4390 // Check equality with another numeric constant.
4392 equals(const Numeric_constant
&) const;
4394 // Set to an unsigned long value.
4396 set_unsigned_long(Type
*, unsigned long);
4398 // Set to an integer value.
4400 set_int(Type
*, const mpz_t
);
4402 // Set to a rune value.
4404 set_rune(Type
*, const mpz_t
);
4406 // Set to a floating point value.
4408 set_float(Type
*, const mpfr_t
);
4410 // Set to a complex value.
4412 set_complex(Type
*, const mpc_t
);
4414 // Mark numeric constant as invalid.
4417 { this->classification_
= NC_INVALID
; }
4422 { return this->classification_
== Numeric_constant::NC_INT
; }
4426 { return this->classification_
== Numeric_constant::NC_RUNE
; }
4430 { return this->classification_
== Numeric_constant::NC_FLOAT
; }
4434 { return this->classification_
== Numeric_constant::NC_COMPLEX
; }
4438 { return this->classification_
== Numeric_constant::NC_INVALID
; }
4440 // Value retrievers. These will initialize the values as well as
4441 // set them. GET_INT is only valid if IS_INT returns true, and
4442 // likewise respectively.
4444 get_int(mpz_t
*) const;
4447 get_rune(mpz_t
*) const;
4450 get_float(mpfr_t
*) const;
4453 get_complex(mpc_t
*) const;
4455 // Codes returned by to_unsigned_long.
4456 enum To_unsigned_long
4458 // Value is integer and fits in unsigned long.
4460 // Value is not integer.
4462 // Value is integer but is negative.
4464 // Value is non-negative integer but does not fit in unsigned
4469 // If the value can be expressed as an integer that fits in an
4470 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
4471 // one of the other To_unsigned_long codes.
4473 to_unsigned_long(unsigned long* val
) const;
4475 // If the value can be expressed as an integer that describes the
4476 // size of an object in memory, set *VAL and return true.
4477 // Otherwise, return false. Currently we use int64_t to represent a
4478 // memory size, as in Type::backend_type_size.
4480 to_memory_size(int64_t* val
) const;
4482 // If the value can be expressed as an int, return true and
4483 // initialize and set VAL. This will return false for a value with
4484 // an explicit float or complex type, even if the value is integral.
4486 to_int(mpz_t
* val
) const;
4488 // If the value can be expressed as a float, return true and
4489 // initialize and set VAL.
4491 to_float(mpfr_t
* val
) const;
4493 // If the value can be expressed as a complex, return true and
4494 // initialize and set VR and VI.
4496 to_complex(mpc_t
* val
) const;
4502 // If the constant can be expressed in TYPE, then set the type of
4503 // the constant to TYPE and return true. Otherwise return false,
4504 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
4505 // the location to use for the error.
4507 set_type(Type
* type
, bool issue_error
, Location location
);
4509 // Return an Expression for this value.
4511 expression(Location
) const;
4513 // Calculate a hash code with a given seed.
4515 hash(unsigned int seed
) const;
4522 mpz_to_unsigned_long(const mpz_t ival
, unsigned long *val
) const;
4525 mpfr_to_unsigned_long(const mpfr_t fval
, unsigned long *val
) const;
4528 mpz_to_memory_size(const mpz_t ival
, int64_t* val
) const;
4531 mpfr_to_memory_size(const mpfr_t fval
, int64_t* val
) const;
4534 check_int_type(Integer_type
*, bool, Location
);
4537 check_float_type(Float_type
*, bool, Location
);
4540 check_complex_type(Complex_type
*, bool, Location
);
4543 is_float_neg_zero(const mpfr_t
, int bits
);
4545 // The kinds of constants.
4555 // The kind of constant.
4556 Classification classification_
;
4560 // If NC_INT or NC_RUNE.
4567 // The type if there is one. This will be NULL for an untyped
4572 // Temporary buffer size for string conversions.
4573 // Also known to the runtime as tmpStringBufSize in runtime/string.go.
4574 static const int tmp_string_buf_size
= 32;
4576 #endif // !defined(GO_EXPRESSIONS_H)