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
15 class Translate_context
;
17 class Statement_inserter
;
28 class Expression_list
;
30 class Temporary_reference_expression
;
31 class Set_and_use_temporary_expression
;
32 class String_expression
;
33 class Unary_expression
;
34 class Binary_expression
;
35 class Call_expression
;
36 class Func_expression
;
37 class Func_descriptor_expression
;
38 class Unknown_expression
;
39 class Index_expression
;
40 class Map_index_expression
;
41 class Bound_method_expression
;
42 class Field_reference_expression
;
43 class Interface_field_reference_expression
;
44 class Type_guard_expression
;
45 class Receive_expression
;
46 class Numeric_constant
;
50 class Temporary_statement
;
52 class Ast_dump_context
;
55 // The base class for all expressions.
60 // The types of expressions.
61 enum Expression_classification
67 EXPRESSION_CONST_REFERENCE
,
68 EXPRESSION_VAR_REFERENCE
,
69 EXPRESSION_TEMPORARY_REFERENCE
,
70 EXPRESSION_SET_AND_USE_TEMPORARY
,
72 EXPRESSION_FUNC_REFERENCE
,
73 EXPRESSION_FUNC_DESCRIPTOR
,
74 EXPRESSION_FUNC_CODE_REFERENCE
,
75 EXPRESSION_UNKNOWN_REFERENCE
,
78 EXPRESSION_STRING_INFO
,
85 EXPRESSION_CALL_RESULT
,
86 EXPRESSION_BOUND_METHOD
,
88 EXPRESSION_ARRAY_INDEX
,
89 EXPRESSION_STRING_INDEX
,
92 EXPRESSION_FIELD_REFERENCE
,
93 EXPRESSION_INTERFACE_FIELD_REFERENCE
,
94 EXPRESSION_ALLOCATION
,
95 EXPRESSION_TYPE_GUARD
,
96 EXPRESSION_CONVERSION
,
97 EXPRESSION_UNSAFE_CONVERSION
,
98 EXPRESSION_STRUCT_CONSTRUCTION
,
99 EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
100 EXPRESSION_SLICE_CONSTRUCTION
,
101 EXPRESSION_MAP_CONSTRUCTION
,
102 EXPRESSION_COMPOSITE_LITERAL
,
105 EXPRESSION_TYPE_DESCRIPTOR
,
106 EXPRESSION_TYPE_INFO
,
107 EXPRESSION_SLICE_INFO
,
108 EXPRESSION_SLICE_VALUE
,
109 EXPRESSION_INTERFACE_INFO
,
110 EXPRESSION_INTERFACE_VALUE
,
111 EXPRESSION_INTERFACE_MTABLE
,
112 EXPRESSION_STRUCT_FIELD_OFFSET
,
113 EXPRESSION_MAP_DESCRIPTOR
,
114 EXPRESSION_LABEL_ADDR
,
115 EXPRESSION_CONDITIONAL
,
119 Expression(Expression_classification
, Location
);
121 virtual ~Expression();
123 // Make an error expression. This is used when a parse error occurs
124 // to prevent cascading errors.
126 make_error(Location
);
128 // Make an expression which is really a type. This is used during
131 make_type(Type
*, Location
);
133 // Make a unary expression.
135 make_unary(Operator
, Expression
*, Location
);
137 // Make a binary expression.
139 make_binary(Operator
, Expression
*, Expression
*, Location
);
141 // Make a reference to a constant in an expression.
143 make_const_reference(Named_object
*, Location
);
145 // Make a reference to a variable in an expression.
147 make_var_reference(Named_object
*, Location
);
149 // Make a reference to a temporary variable. Temporary variables
150 // are always created by a single statement, which is what we use to
152 static Temporary_reference_expression
*
153 make_temporary_reference(Temporary_statement
*, Location
);
155 // Make an expressions which sets a temporary variable and then
156 // evaluates to a reference to that temporary variable. This is
157 // used to set a temporary variable while retaining the order of
159 static Set_and_use_temporary_expression
*
160 make_set_and_use_temporary(Temporary_statement
*, Expression
*, Location
);
162 // Make a sink expression--a reference to the blank identifier _.
166 // Make a reference to a function in an expression. This returns a
167 // pointer to the struct holding the address of the function
168 // followed by any closed-over variables.
170 make_func_reference(Named_object
*, Expression
* closure
, Location
);
172 // Make a function descriptor, an immutable struct with a single
173 // field that points to the function code. This may only be used
174 // with functions that do not have closures. FN is the function for
175 // which we are making the descriptor.
176 static Func_descriptor_expression
*
177 make_func_descriptor(Named_object
* fn
);
179 // Make a reference to the code of a function. This is used to set
180 // descriptor and closure fields.
182 make_func_code_reference(Named_object
*, Location
);
184 // Make a reference to an unknown name. In a correct program this
185 // will always be lowered to a real const/var/func reference.
186 static Unknown_expression
*
187 make_unknown_reference(Named_object
*, Location
);
189 // Make a constant bool expression.
191 make_boolean(bool val
, Location
);
193 // Make a constant string expression.
195 make_string(const std::string
&, Location
);
197 // Make an expression that evaluates to some characteristic of an string.
198 // For simplicity, the enum values must match the field indexes in the
199 // underlying struct.
202 // The underlying data in the string.
204 // The length of the string.
209 make_string_info(Expression
* string
, String_info
, Location
);
211 // Make a character constant expression. TYPE should be NULL for an
214 make_character(const mpz_t
*, Type
*, Location
);
216 // Make a constant integer expression. TYPE should be NULL for an
219 make_integer(const mpz_t
*, Type
*, Location
);
221 // Make a constant float expression. TYPE should be NULL for an
224 make_float(const mpfr_t
*, Type
*, Location
);
226 // Make a constant complex expression. TYPE should be NULL for an
229 make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
*, Location
);
231 // Make a nil expression.
235 // Make an iota expression. This is used for the predeclared
240 // Make a call expression.
241 static Call_expression
*
242 make_call(Expression
* func
, Expression_list
* args
, bool is_varargs
,
245 // Make a reference to a specific result of a call expression which
248 make_call_result(Call_expression
*, unsigned int index
);
250 // Make an expression which is a method bound to its first
251 // parameter. METHOD is the method being called, FUNCTION is the
253 static Bound_method_expression
*
254 make_bound_method(Expression
* object
, const Method
* method
,
255 Named_object
* function
, Location
);
257 // Make an index or slice expression. This is a parser expression
258 // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
259 // index rather than a slice. CAP may be NULL, meaning we use the default
260 // capacity of LEFT. At parse time we may not know the type of LEFT.
261 // After parsing this is lowered to an array index, a string index,
264 make_index(Expression
* left
, Expression
* start
, Expression
* end
,
265 Expression
* cap
, Location
);
267 // Make an array index expression. END may be NULL, in which case
268 // this is an lvalue. CAP may be NULL, in which case it defaults
271 make_array_index(Expression
* array
, Expression
* start
, Expression
* end
,
272 Expression
* cap
, Location
);
274 // Make a string index expression. END may be NULL. This is never
277 make_string_index(Expression
* string
, Expression
* start
, Expression
* end
,
280 // Make a map index expression. This is an lvalue.
281 static Map_index_expression
*
282 make_map_index(Expression
* map
, Expression
* val
, Location
);
284 // Make a selector. This is a parser expression which represents
285 // LEFT.NAME. At parse time we may not know the type of the left
288 make_selector(Expression
* left
, const std::string
& name
, Location
);
290 // Make a reference to a field in a struct.
291 static Field_reference_expression
*
292 make_field_reference(Expression
*, unsigned int field_index
, Location
);
294 // Make a reference to a field of an interface, with an associated
297 make_interface_field_reference(Expression
*, const std::string
&,
300 // Make an allocation expression.
302 make_allocation(Type
*, Location
);
304 // Make a type guard expression.
306 make_type_guard(Expression
*, Type
*, Location
);
308 // Make a type cast expression.
310 make_cast(Type
*, Expression
*, Location
);
312 // Make an unsafe type cast expression. This is only used when
313 // passing parameter to builtin functions that are part of the Go
316 make_unsafe_cast(Type
*, Expression
*, Location
);
318 // Make a composite literal. The DEPTH parameter is how far down we
319 // are in a list of composite literals with omitted types. HAS_KEYS
320 // is true if the expression list has keys alternating with values.
321 // ALL_ARE_NAMES is true if all the keys could be struct field
324 make_composite_literal(Type
*, int depth
, bool has_keys
, Expression_list
*,
325 bool all_are_names
, Location
);
327 // Make a struct composite literal.
329 make_struct_composite_literal(Type
*, Expression_list
*, Location
);
331 // Make an array composite literal.
333 make_array_composite_literal(Type
*, Expression_list
*, Location
);
335 // Make a slice composite literal.
337 make_slice_composite_literal(Type
*, Expression_list
*, Location
);
339 // Take an expression and allocate it on the heap.
341 make_heap_expression(Expression
*, Location
);
343 // Make a receive expression. VAL is NULL for a unary receive.
344 static Receive_expression
*
345 make_receive(Expression
* channel
, Location
);
347 // Make an expression which evaluates to the address of the type
348 // descriptor for TYPE.
350 make_type_descriptor(Type
* type
, Location
);
352 // Make an expression which evaluates to some characteristic of a
353 // type. These are only used for type descriptors, so there is no
354 // location parameter.
357 // The size of a value of the type.
359 // The required alignment of a value of the type.
361 // The required alignment of a value of the type when used as a
362 // field in a struct.
363 TYPE_INFO_FIELD_ALIGNMENT
367 make_type_info(Type
* type
, Type_info
);
369 // Make an expression that evaluates to some characteristic of a
370 // slice. For simplicity, the enum values must match the field indexes
371 // in the underlying struct.
374 // The underlying data of the slice.
375 SLICE_INFO_VALUE_POINTER
,
376 // The length of the slice.
378 // The capacity of the slice.
383 make_slice_info(Expression
* slice
, Slice_info
, Location
);
385 // Make an expression for a slice value.
387 make_slice_value(Type
*, Expression
* valptr
, Expression
* len
, Expression
* cap
,
390 // Make an expression that evaluates to some characteristic of an
391 // interface. For simplicity, the enum values must match the field indexes
392 // in the underlying struct.
395 // The type descriptor of an empty interface.
396 INTERFACE_INFO_TYPE_DESCRIPTOR
= 0,
397 // The methods of an interface.
398 INTERFACE_INFO_METHODS
= 0,
399 // The first argument to pass to an interface method.
400 INTERFACE_INFO_OBJECT
404 make_interface_info(Expression
* iface
, Interface_info
, Location
);
406 // Make an expression for an interface value.
408 make_interface_value(Type
*, Expression
*, Expression
*, Location
);
410 // Make an expression that builds a reference to the interface method table
411 // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
412 // reference to the interface method table for the pointer receiver type.
414 make_interface_mtable_ref(Interface_type
* itype
, Type
* type
,
415 bool is_pointer
, Location
);
417 // Make an expression which evaluates to the offset of a field in a
418 // struct. This is only used for type descriptors, so there is no
419 // location parameter.
421 make_struct_field_offset(Struct_type
*, const Struct_field
*);
423 // Make an expression which evaluates to the address of the map
424 // descriptor for TYPE.
426 make_map_descriptor(Map_type
* type
, Location
);
428 // Make an expression which evaluates to the address of an unnamed
431 make_label_addr(Label
*, Location
);
433 // Make a conditional expression.
435 make_conditional(Expression
*, Expression
*, Expression
*, Location
);
437 // Make a compound expression.
439 make_compound(Expression
*, Expression
*, Location
);
441 // Return the expression classification.
442 Expression_classification
443 classification() const
444 { return this->classification_
; }
446 // Return the location of the expression.
449 { return this->location_
; }
451 // Return whether this is a constant expression.
454 { return this->do_is_constant(); }
456 // Return whether this is an immutable expression.
459 { return this->do_is_immutable(); }
461 // If this is not a numeric constant, return false. If it is one,
462 // return true, and set VAL to hold the value.
464 numeric_constant_value(Numeric_constant
* val
) const
465 { return this->do_numeric_constant_value(val
); }
467 // If this is not a constant expression with string type, return
468 // false. If it is one, return true, and set VAL to the value.
470 string_constant_value(std::string
* val
) const
471 { return this->do_string_constant_value(val
); }
473 // This is called if the value of this expression is being
474 // discarded. This issues warnings about computed values being
475 // unused. This returns true if all is well, false if it issued an
479 { return this->do_discarding_value(); }
481 // Return whether this is an error expression.
483 is_error_expression() const
484 { return this->classification_
== EXPRESSION_ERROR
; }
486 // Return whether this expression really represents a type.
488 is_type_expression() const
489 { return this->classification_
== EXPRESSION_TYPE
; }
491 // If this is a variable reference, return the Var_expression
492 // structure. Otherwise, return NULL. This is a controlled dynamic
496 { return this->convert
<Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
498 const Var_expression
*
499 var_expression() const
500 { return this->convert
<const Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
502 // If this is a reference to a temporary variable, return the
503 // Temporary_reference_expression. Otherwise, return NULL.
504 Temporary_reference_expression
*
505 temporary_reference_expression()
507 return this->convert
<Temporary_reference_expression
,
508 EXPRESSION_TEMPORARY_REFERENCE
>();
511 // If this is a set-and-use-temporary, return the
512 // Set_and_use_temporary_expression. Otherwise, return NULL.
513 Set_and_use_temporary_expression
*
514 set_and_use_temporary_expression()
516 return this->convert
<Set_and_use_temporary_expression
,
517 EXPRESSION_SET_AND_USE_TEMPORARY
>();
520 // Return whether this is a sink expression.
522 is_sink_expression() const
523 { return this->classification_
== EXPRESSION_SINK
; }
525 // If this is a string expression, return the String_expression
526 // structure. Otherwise, return NULL.
529 { return this->convert
<String_expression
, EXPRESSION_STRING
>(); }
531 // Return whether this is the expression nil.
533 is_nil_expression() const
534 { return this->classification_
== EXPRESSION_NIL
; }
536 // If this is an indirection through a pointer, return the
537 // expression being pointed through. Otherwise return this.
541 // If this is a unary expression, return the Unary_expression
542 // structure. Otherwise return NULL.
545 { return this->convert
<Unary_expression
, EXPRESSION_UNARY
>(); }
547 // If this is a binary expression, return the Binary_expression
548 // structure. Otherwise return NULL.
551 { return this->convert
<Binary_expression
, EXPRESSION_BINARY
>(); }
553 // If this is a call expression, return the Call_expression
554 // structure. Otherwise, return NULL. This is a controlled dynamic
558 { return this->convert
<Call_expression
, EXPRESSION_CALL
>(); }
560 // If this is an expression which refers to a function, return the
561 // Func_expression structure. Otherwise, return NULL.
564 { return this->convert
<Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
566 const Func_expression
*
567 func_expression() const
568 { return this->convert
<const Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
570 // If this is an expression which refers to an unknown name, return
571 // the Unknown_expression structure. Otherwise, return NULL.
574 { return this->convert
<Unknown_expression
, EXPRESSION_UNKNOWN_REFERENCE
>(); }
576 const Unknown_expression
*
577 unknown_expression() const
579 return this->convert
<const Unknown_expression
,
580 EXPRESSION_UNKNOWN_REFERENCE
>();
583 // If this is an index expression, return the Index_expression
584 // structure. Otherwise, return NULL.
587 { return this->convert
<Index_expression
, EXPRESSION_INDEX
>(); }
589 // If this is an expression which refers to indexing in a map,
590 // return the Map_index_expression structure. Otherwise, return
592 Map_index_expression
*
593 map_index_expression()
594 { return this->convert
<Map_index_expression
, EXPRESSION_MAP_INDEX
>(); }
596 // If this is a bound method expression, return the
597 // Bound_method_expression structure. Otherwise, return NULL.
598 Bound_method_expression
*
599 bound_method_expression()
600 { return this->convert
<Bound_method_expression
, EXPRESSION_BOUND_METHOD
>(); }
602 // If this is a reference to a field in a struct, return the
603 // Field_reference_expression structure. Otherwise, return NULL.
604 Field_reference_expression
*
605 field_reference_expression()
607 return this->convert
<Field_reference_expression
,
608 EXPRESSION_FIELD_REFERENCE
>();
611 // If this is a reference to a field in an interface, return the
612 // Interface_field_reference_expression structure. Otherwise,
614 Interface_field_reference_expression
*
615 interface_field_reference_expression()
617 return this->convert
<Interface_field_reference_expression
,
618 EXPRESSION_INTERFACE_FIELD_REFERENCE
>();
621 // If this is a type guard expression, return the
622 // Type_guard_expression structure. Otherwise, return NULL.
623 Type_guard_expression
*
624 type_guard_expression()
625 { return this->convert
<Type_guard_expression
, EXPRESSION_TYPE_GUARD
>(); }
627 // If this is a receive expression, return the Receive_expression
628 // structure. Otherwise, return NULL.
631 { return this->convert
<Receive_expression
, EXPRESSION_RECEIVE
>(); }
633 // Return true if this is a composite literal.
635 is_composite_literal() const;
637 // Return true if this is a composite literal which is not constant.
639 is_nonconstant_composite_literal() const;
641 // Return true if this is a variable or temporary variable.
645 // Return true if this is a reference to a local variable.
647 is_local_variable() const;
649 // Make the builtin function descriptor type, so that it can be
652 make_func_descriptor_type();
654 // Traverse an expression.
656 traverse(Expression
**, Traverse
*);
658 // Traverse subexpressions of this expression.
660 traverse_subexpressions(Traverse
*);
662 // Lower an expression. This is called immediately after parsing.
663 // FUNCTION is the function we are in; it will be NULL for an
664 // expression initializing a global variable. INSERTER may be used
665 // to insert statements before the statement or initializer
666 // containing this expression; it is normally used to create
667 // temporary variables. IOTA_VALUE is the value that we should give
668 // to any iota expressions. This function must resolve expressions
669 // which could not be fully parsed into their final form. It
670 // returns the same Expression or a new one.
672 lower(Gogo
* gogo
, Named_object
* function
, Statement_inserter
* inserter
,
674 { return this->do_lower(gogo
, function
, inserter
, iota_value
); }
676 // Flatten an expression. This is called after order_evaluation.
677 // FUNCTION is the function we are in; it will be NULL for an
678 // expression initializing a global variable. INSERTER may be used
679 // to insert statements before the statement or initializer
680 // containing this expression; it is normally used to create
681 // temporary variables. This function must resolve expressions
682 // which could not be fully parsed into their final form. It
683 // returns the same Expression or a new one.
685 flatten(Gogo
* gogo
, Named_object
* function
, Statement_inserter
* inserter
)
686 { return this->do_flatten(gogo
, function
, inserter
); }
688 // Determine the real type of an expression with abstract integer,
689 // floating point, or complex type. TYPE_CONTEXT describes the
692 determine_type(const Type_context
*);
694 // Check types in an expression.
696 check_types(Gogo
* gogo
)
697 { this->do_check_types(gogo
); }
699 // Determine the type when there is no context.
701 determine_type_no_context();
703 // Return the current type of the expression. This may be changed
704 // by determine_type.
707 { return this->do_type(); }
709 // Return a copy of an expression.
712 { return this->do_copy(); }
714 // Return whether the expression is addressable--something which may
715 // be used as the operand of the unary & operator.
717 is_addressable() const
718 { return this->do_is_addressable(); }
720 // Note that we are taking the address of this expression. ESCAPES
721 // is true if this address escapes the current function.
723 address_taken(bool escapes
)
724 { this->do_address_taken(escapes
); }
726 // Note that a nil check must be issued for this expression.
729 { this->do_issue_nil_check(); }
731 // Return whether this expression must be evaluated in order
732 // according to the order of evaluation rules. This is basically
733 // true of all expressions with side-effects.
735 must_eval_in_order() const
736 { return this->do_must_eval_in_order(); }
738 // Return whether subexpressions of this expression must be
739 // evaluated in order. This is true of index expressions and
740 // pointer indirections. This sets *SKIP to the number of
741 // subexpressions to skip during traversing, as index expressions
742 // only requiring moving the index, not the array.
744 must_eval_subexpressions_in_order(int* skip
) const
747 return this->do_must_eval_subexpressions_in_order(skip
);
750 // Return the backend representation for this expression.
752 get_backend(Translate_context
*);
754 // Return an expression handling any conversions which must be done during
757 convert_for_assignment(Gogo
*, Type
* lhs_type
, Expression
* rhs
,
760 // Return an expression converting a value of one interface type to another
761 // interface type. If FOR_TYPE_GUARD is true this is for a type
764 convert_interface_to_interface(Type
* lhs_type
,
765 Expression
* rhs
, bool for_type_guard
,
768 // Return a backend expression implementing the comparison LEFT OP RIGHT.
769 // TYPE is the type of both sides.
771 comparison(Translate_context
*, Type
* result_type
, Operator op
,
772 Expression
* left
, Expression
* right
, Location
);
774 // Return the backend expression for the numeric constant VAL.
776 backend_numeric_constant_expression(Translate_context
*,
777 Numeric_constant
* val
);
779 // Export the expression. This is only used for constants. It will
780 // be used for things like values of named constants and sizes of
783 export_expression(Export
* exp
) const
784 { this->do_export(exp
); }
786 // Import an expression.
788 import_expression(Import
*);
790 // Return an expression which checks that VAL, of arbitrary integer type,
791 // is non-negative and is not more than the maximum integer value.
793 check_bounds(Expression
* val
, Location
);
795 // Dump an expression to a dump constext.
797 dump_expression(Ast_dump_context
*) const;
800 // May be implemented by child class: traverse the expressions.
802 do_traverse(Traverse
*);
804 // Return a lowered expression.
806 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
809 // Return a flattened expression.
811 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*)
815 // Return whether this is a constant expression.
817 do_is_constant() const
820 // Return whether this is an immutable expression.
822 do_is_immutable() const
825 // Return whether this is a constant expression of numeric type, and
826 // set the Numeric_constant to the value.
828 do_numeric_constant_value(Numeric_constant
*) const
831 // Return whether this is a constant expression of string type, and
832 // set VAL to the value.
834 do_string_constant_value(std::string
*) const
837 // Called by the parser if the value is being discarded.
839 do_discarding_value();
841 // Child class holds type.
845 // Child class implements determining type information.
847 do_determine_type(const Type_context
*) = 0;
849 // Child class implements type checking if needed.
851 do_check_types(Gogo
*)
854 // Child class implements copying.
858 // Child class implements whether the expression is addressable.
860 do_is_addressable() const
863 // Child class implements taking the address of an expression.
865 do_address_taken(bool)
868 // Child class implements issuing a nil check if the address is taken.
873 // Child class implements whether this expression must be evaluated
876 do_must_eval_in_order() const
879 // Child class implements whether this expressions requires that
880 // subexpressions be evaluated in order. The child implementation
881 // may set *SKIP if it should be non-zero.
883 do_must_eval_subexpressions_in_order(int* /* skip */) const
886 // Child class implements conversion to backend representation.
888 do_get_backend(Translate_context
*) = 0;
890 // Child class implements export.
892 do_export(Export
*) const;
894 // For children to call to give an error for an unused value.
896 unused_value_error();
898 // For children to call when they detect that they are in error.
902 // For children to call to report an error conveniently.
904 report_error(const char*);
906 // Child class implements dumping to a dump context.
908 do_dump_expression(Ast_dump_context
*) const = 0;
911 // Convert to the desired statement classification, or return NULL.
912 // This is a controlled dynamic cast.
913 template<typename Expression_class
,
914 Expression_classification expr_classification
>
918 return (this->classification_
== expr_classification
919 ? static_cast<Expression_class
*>(this)
923 template<typename Expression_class
,
924 Expression_classification expr_classification
>
925 const Expression_class
*
928 return (this->classification_
== expr_classification
929 ? static_cast<const Expression_class
*>(this)
934 convert_type_to_interface(Type
*, Expression
*, Location
);
937 get_interface_type_descriptor(Expression
*);
940 convert_interface_to_type(Type
*, Expression
*, Location
);
942 // The expression classification.
943 Expression_classification classification_
;
944 // The location in the input file.
948 // A list of Expressions.
950 class Expression_list
957 // Return whether the list is empty.
960 { return this->entries_
.empty(); }
962 // Return the number of entries in the list.
965 { return this->entries_
.size(); }
967 // Add an entry to the end of the list.
969 push_back(Expression
* expr
)
970 { this->entries_
.push_back(expr
); }
973 append(Expression_list
* add
)
974 { this->entries_
.insert(this->entries_
.end(), add
->begin(), add
->end()); }
976 // Reserve space in the list.
979 { this->entries_
.reserve(size
); }
981 // Traverse the expressions in the list.
989 // Return true if the list contains an error expression.
991 contains_error() const;
993 // Retrieve an element by index.
996 { return this->entries_
.at(i
); }
998 // Return the first and last elements.
1001 { return this->entries_
.front(); }
1005 { return this->entries_
.front(); }
1009 { return this->entries_
.back(); }
1013 { return this->entries_
.back(); }
1017 typedef std::vector
<Expression
*>::iterator iterator
;
1018 typedef std::vector
<Expression
*>::const_iterator const_iterator
;
1022 { return this->entries_
.begin(); }
1026 { return this->entries_
.begin(); }
1030 { return this->entries_
.end(); }
1034 { return this->entries_
.end(); }
1039 { this->entries_
.erase(p
); }
1042 std::vector
<Expression
*> entries_
;
1045 // An abstract base class for an expression which is only used by the
1046 // parser, and is lowered in the lowering pass.
1048 class Parser_expression
: public Expression
1051 Parser_expression(Expression_classification classification
,
1053 : Expression(classification
, location
)
1058 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int) = 0;
1064 do_determine_type(const Type_context
*)
1065 { go_unreachable(); }
1068 do_check_types(Gogo
*)
1069 { go_unreachable(); }
1072 do_get_backend(Translate_context
*)
1073 { go_unreachable(); }
1076 // An expression which is simply a variable.
1078 class Var_expression
: public Expression
1081 Var_expression(Named_object
* variable
, Location location
)
1082 : Expression(EXPRESSION_VAR_REFERENCE
, location
),
1086 // Return the variable.
1088 named_object() const
1089 { return this->variable_
; }
1093 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1099 do_determine_type(const Type_context
*);
1106 do_is_addressable() const
1110 do_address_taken(bool);
1113 do_get_backend(Translate_context
*);
1116 do_dump_expression(Ast_dump_context
*) const;
1119 // The variable we are referencing.
1120 Named_object
* variable_
;
1123 // A reference to a temporary variable.
1125 class Temporary_reference_expression
: public Expression
1128 Temporary_reference_expression(Temporary_statement
* statement
,
1130 : Expression(EXPRESSION_TEMPORARY_REFERENCE
, location
),
1131 statement_(statement
), is_lvalue_(false)
1134 // The temporary that this expression refers to.
1135 Temporary_statement
*
1137 { return this->statement_
; }
1139 // Indicate that this reference appears on the left hand side of an
1140 // assignment statement.
1143 { this->is_lvalue_
= true; }
1150 do_determine_type(const Type_context
*)
1155 { return make_temporary_reference(this->statement_
, this->location()); }
1158 do_is_addressable() const
1162 do_address_taken(bool);
1165 do_get_backend(Translate_context
*);
1168 do_dump_expression(Ast_dump_context
*) const;
1171 // The statement where the temporary variable is defined.
1172 Temporary_statement
* statement_
;
1173 // Whether this reference appears on the left hand side of an
1174 // assignment statement.
1178 // Set and use a temporary variable.
1180 class Set_and_use_temporary_expression
: public Expression
1183 Set_and_use_temporary_expression(Temporary_statement
* statement
,
1184 Expression
* expr
, Location location
)
1185 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY
, location
),
1186 statement_(statement
), expr_(expr
)
1189 // Return the temporary.
1190 Temporary_statement
*
1192 { return this->statement_
; }
1194 // Return the expression.
1197 { return this->expr_
; }
1201 do_traverse(Traverse
* traverse
)
1202 { return Expression::traverse(&this->expr_
, traverse
); }
1208 do_determine_type(const Type_context
*);
1213 return make_set_and_use_temporary(this->statement_
, this->expr_
,
1218 do_is_addressable() const
1222 do_address_taken(bool);
1225 do_get_backend(Translate_context
*);
1228 do_dump_expression(Ast_dump_context
*) const;
1231 // The statement where the temporary variable is defined.
1232 Temporary_statement
* statement_
;
1233 // The expression to assign to the temporary.
1237 // A string expression.
1239 class String_expression
: public Expression
1242 String_expression(const std::string
& val
, Location location
)
1243 : Expression(EXPRESSION_STRING
, location
),
1244 val_(val
), type_(NULL
)
1249 { return this->val_
; }
1256 do_is_constant() const
1260 do_is_immutable() const
1264 do_string_constant_value(std::string
* val
) const
1274 do_determine_type(const Type_context
*);
1281 do_get_backend(Translate_context
*);
1283 // Write string literal to a string dump.
1285 export_string(String_dump
* exp
, const String_expression
* str
);
1288 do_export(Export
*) const;
1291 do_dump_expression(Ast_dump_context
*) const;
1294 // The string value. This is immutable.
1295 const std::string val_
;
1296 // The type as determined by context.
1300 // A Unary expression.
1302 class Unary_expression
: public Expression
1305 Unary_expression(Operator op
, Expression
* expr
, Location location
)
1306 : Expression(EXPRESSION_UNARY
, location
),
1307 op_(op
), escapes_(true), create_temp_(false), is_gc_root_(false),
1308 is_slice_init_(false), expr_(expr
), issue_nil_check_(false)
1311 // Return the operator.
1314 { return this->op_
; }
1316 // Return the operand.
1319 { return this->expr_
; }
1321 // Record that an address expression does not escape.
1323 set_does_not_escape()
1325 go_assert(this->op_
== OPERATOR_AND
);
1326 this->escapes_
= false;
1329 // Record that this is an address expression which should create a
1330 // temporary variable if necessary. This is used for method calls.
1334 go_assert(this->op_
== OPERATOR_AND
);
1335 this->create_temp_
= true;
1338 // Record that this is an address expression of a GC root, which is a
1339 // mutable composite literal. This used for registering GC variables.
1343 go_assert(this->op_
== OPERATOR_AND
);
1344 this->is_gc_root_
= true;
1347 // Record that this is an address expression of a slice value initializer,
1348 // which is mutable if the values are not copied to the heap.
1352 go_assert(this->op_
== OPERATOR_AND
);
1353 this->is_slice_init_
= true;
1356 // Apply unary opcode OP to UNC, setting NC. Return true if this
1357 // could be done, false if not. Issue errors for overflow.
1359 eval_constant(Operator op
, const Numeric_constant
* unc
,
1360 Location
, Numeric_constant
* nc
);
1367 do_traverse(Traverse
* traverse
)
1368 { return Expression::traverse(&this->expr_
, traverse
); }
1371 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1374 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1377 do_is_constant() const;
1380 do_is_immutable() const
1382 return (this->expr_
->is_immutable()
1383 || (this->op_
== OPERATOR_AND
&& this->expr_
->is_variable()));
1387 do_numeric_constant_value(Numeric_constant
*) const;
1393 do_determine_type(const Type_context
*);
1396 do_check_types(Gogo
*);
1401 return Expression::make_unary(this->op_
, this->expr_
->copy(),
1406 do_must_eval_subexpressions_in_order(int*) const
1407 { return this->op_
== OPERATOR_MULT
; }
1410 do_is_addressable() const
1411 { return this->op_
== OPERATOR_MULT
; }
1414 do_get_backend(Translate_context
*);
1417 do_export(Export
*) const;
1420 do_dump_expression(Ast_dump_context
*) const;
1423 do_issue_nil_check()
1424 { this->issue_nil_check_
= (this->op_
== OPERATOR_MULT
); }
1427 // The unary operator to apply.
1429 // Normally true. False if this is an address expression which does
1430 // not escape the current function.
1432 // True if this is an address expression which should create a
1433 // temporary variable if necessary.
1435 // True if this is an address expression for a GC root. A GC root is a
1436 // special struct composite literal that is mutable when addressed, meaning
1437 // it cannot be represented as an immutable_struct in the backend.
1439 // True if this is an address expression for a slice value with an immutable
1440 // initializer. The initializer for a slice's value pointer has an array
1441 // type, meaning it cannot be represented as an immutable_struct in the
1443 bool is_slice_init_
;
1446 // Whether or not to issue a nil check for this expression if its address
1448 bool issue_nil_check_
;
1451 // A binary expression.
1453 class Binary_expression
: public Expression
1456 Binary_expression(Operator op
, Expression
* left
, Expression
* right
,
1458 : Expression(EXPRESSION_BINARY
, location
),
1459 op_(op
), left_(left
), right_(right
), type_(NULL
)
1462 // Return the operator.
1465 { return this->op_
; }
1467 // Return the left hand expression.
1470 { return this->left_
; }
1472 // Return the right hand expression.
1475 { return this->right_
; }
1477 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1478 // Return true if this could be done, false if not. Issue errors at
1479 // LOCATION as appropriate.
1481 eval_constant(Operator op
, Numeric_constant
* left_nc
,
1482 Numeric_constant
* right_nc
, Location location
,
1483 Numeric_constant
* nc
);
1485 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1486 // *RESULT. Return true if this could be done, false if not. Issue
1487 // errors at LOCATION as appropriate.
1489 compare_constant(Operator op
, Numeric_constant
* left_nc
,
1490 Numeric_constant
* right_nc
, Location location
,
1496 // Report an error if OP can not be applied to TYPE. Return whether
1497 // it can. OTYPE is the type of the other operand.
1499 check_operator_type(Operator op
, Type
* type
, Type
* otype
, Location
);
1503 do_traverse(Traverse
* traverse
);
1506 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1509 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1512 do_is_constant() const
1513 { return this->left_
->is_constant() && this->right_
->is_constant(); }
1516 do_numeric_constant_value(Numeric_constant
*) const;
1519 do_discarding_value();
1525 do_determine_type(const Type_context
*);
1528 do_check_types(Gogo
*);
1533 return Expression::make_binary(this->op_
, this->left_
->copy(),
1534 this->right_
->copy(), this->location());
1538 do_get_backend(Translate_context
*);
1541 do_export(Export
*) const;
1544 do_dump_expression(Ast_dump_context
*) const;
1548 operation_type(Operator op
, Type
* left_type
, Type
* right_type
,
1549 Type
** result_type
);
1552 cmp_to_bool(Operator op
, int cmp
);
1555 eval_integer(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
1556 Location
, Numeric_constant
*);
1559 eval_float(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
1560 Location
, Numeric_constant
*);
1563 eval_complex(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
1564 Location
, Numeric_constant
*);
1567 compare_integer(const Numeric_constant
*, const Numeric_constant
*, int*);
1570 compare_float(const Numeric_constant
*, const Numeric_constant
*, int*);
1573 compare_complex(const Numeric_constant
*, const Numeric_constant
*, int*);
1576 lower_struct_comparison(Gogo
*, Statement_inserter
*);
1579 lower_array_comparison(Gogo
*, Statement_inserter
*);
1582 lower_interface_value_comparison(Gogo
*, Statement_inserter
*);
1585 lower_compare_to_memcmp(Gogo
*, Statement_inserter
*);
1588 operand_address(Statement_inserter
*, Expression
*);
1590 // The binary operator to apply.
1592 // The left hand side operand.
1594 // The right hand side operand.
1596 // The type of a comparison operation.
1600 // A call expression. The go statement needs to dig inside this.
1602 class Call_expression
: public Expression
1605 Call_expression(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
1607 : Expression(EXPRESSION_CALL
, location
),
1608 fn_(fn
), args_(args
), type_(NULL
), results_(NULL
), call_(NULL
),
1609 call_temp_(NULL
), expected_result_count_(0), is_varargs_(is_varargs
),
1610 are_hidden_fields_ok_(false), varargs_are_lowered_(false),
1611 types_are_determined_(false), is_deferred_(false), issued_error_(false)
1614 // The function to call.
1617 { return this->fn_
; }
1622 { return this->args_
; }
1624 const Expression_list
*
1626 { return this->args_
; }
1628 // Get the function type.
1630 get_function_type() const;
1632 // Return the number of values this call will return.
1634 result_count() const;
1636 // Return the temporary variable which holds result I. This is only
1637 // valid after the expression has been lowered, and is only valid
1638 // for calls which return multiple results.
1639 Temporary_statement
*
1640 result(size_t i
) const;
1642 // Set the number of results expected from this call. This is used
1643 // when the call appears in a context that expects multiple results,
1644 // such as a, b = f().
1646 set_expected_result_count(size_t);
1648 // Return whether this is a call to the predeclared function
1651 is_recover_call() const;
1653 // Set the argument for a call to recover.
1655 set_recover_arg(Expression
*);
1657 // Whether the last argument is a varargs argument (f(a...)).
1660 { return this->is_varargs_
; }
1662 // Note that varargs have already been lowered.
1664 set_varargs_are_lowered()
1665 { this->varargs_are_lowered_
= true; }
1667 // Note that it is OK for this call to set hidden fields when
1668 // passing arguments.
1670 set_hidden_fields_are_ok()
1671 { this->are_hidden_fields_ok_
= true; }
1673 // Whether this call is being deferred.
1676 { return this->is_deferred_
; }
1678 // Note that the call is being deferred.
1681 { this->is_deferred_
= true; }
1683 // We have found an error with this call expression; return true if
1684 // we should report it.
1690 do_traverse(Traverse
*);
1693 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1696 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
1699 do_discarding_value()
1706 do_determine_type(const Type_context
*);
1709 do_check_types(Gogo
*);
1714 return Expression::make_call(this->fn_
->copy(),
1715 (this->args_
== NULL
1717 : this->args_
->copy()),
1718 this->is_varargs_
, this->location());
1722 do_must_eval_in_order() const;
1724 virtual Bexpression
*
1725 do_get_backend(Translate_context
*);
1728 do_is_recover_call() const;
1731 do_set_recover_arg(Expression
*);
1733 // Let a builtin expression change the argument list.
1735 set_args(Expression_list
* args
)
1736 { this->args_
= args
; }
1738 // Let a builtin expression lower varargs.
1740 lower_varargs(Gogo
*, Named_object
* function
, Statement_inserter
* inserter
,
1741 Type
* varargs_type
, size_t param_count
);
1743 // Let a builtin expression check whether types have been
1746 determining_types();
1749 do_dump_expression(Ast_dump_context
*) const;
1753 check_argument_type(int, const Type
*, const Type
*, Location
, bool);
1756 interface_method_function(Interface_field_reference_expression
*,
1760 set_results(Translate_context
*, Bexpression
*);
1762 // The function to call.
1764 // The arguments to pass. This may be NULL if there are no
1766 Expression_list
* args_
;
1767 // The type of the expression, to avoid recomputing it.
1769 // The list of temporaries which will hold the results if the
1770 // function returns a tuple.
1771 std::vector
<Temporary_statement
*>* results_
;
1772 // The backend expression for the call, used for a call which returns a tuple.
1774 // A temporary variable to store this call if the function returns a tuple.
1775 Temporary_statement
* call_temp_
;
1776 // If not 0, the number of results expected from this call, when
1777 // used in a context that expects multiple values.
1778 size_t expected_result_count_
;
1779 // True if the last argument is a varargs argument (f(a...)).
1781 // True if this statement may pass hidden fields in the arguments.
1782 // This is used for generated method stubs.
1783 bool are_hidden_fields_ok_
;
1784 // True if varargs have already been lowered.
1785 bool varargs_are_lowered_
;
1786 // True if types have been determined.
1787 bool types_are_determined_
;
1788 // True if the call is an argument to a defer statement.
1790 // True if we reported an error about a mismatch between call
1791 // results and uses. This is to avoid producing multiple errors
1792 // when there are multiple Call_result_expressions.
1796 // An expression which represents a pointer to a function.
1798 class Func_expression
: public Expression
1801 Func_expression(Named_object
* function
, Expression
* closure
,
1803 : Expression(EXPRESSION_FUNC_REFERENCE
, location
),
1804 function_(function
), closure_(closure
)
1807 // Return the object associated with the function.
1809 named_object() const
1810 { return this->function_
; }
1812 // Return the closure for this function. This will return NULL if
1813 // the function has no closure, which is the normal case.
1816 { return this->closure_
; }
1818 // Return a backend expression for the code of a function.
1820 get_code_pointer(Gogo
*, Named_object
* function
, Location loc
);
1824 do_traverse(Traverse
*);
1830 do_determine_type(const Type_context
*)
1832 if (this->closure_
!= NULL
)
1833 this->closure_
->determine_type_no_context();
1839 return Expression::make_func_reference(this->function_
,
1840 (this->closure_
== NULL
1842 : this->closure_
->copy()),
1847 do_get_backend(Translate_context
*);
1850 do_dump_expression(Ast_dump_context
*) const;
1853 // The function itself.
1854 Named_object
* function_
;
1855 // A closure. This is normally NULL. For a nested function, it may
1856 // be a struct holding pointers to all the variables referenced by
1857 // this function and defined in enclosing functions.
1858 Expression
* closure_
;
1861 // A function descriptor. A function descriptor is a struct with a
1862 // single field pointing to the function code. This is used for
1863 // functions without closures.
1865 class Func_descriptor_expression
: public Expression
1868 Func_descriptor_expression(Named_object
* fn
);
1870 // Make the function descriptor type, so that it can be converted.
1872 make_func_descriptor_type();
1876 do_traverse(Traverse
*);
1882 do_determine_type(const Type_context
*)
1887 { return Expression::make_func_descriptor(this->fn_
); }
1890 do_is_addressable() const
1894 do_get_backend(Translate_context
*);
1897 do_dump_expression(Ast_dump_context
* context
) const;
1900 // The type of all function descriptors.
1901 static Type
* descriptor_type
;
1903 // The function for which this is the descriptor.
1905 // The descriptor variable.
1909 // A reference to an unknown name.
1911 class Unknown_expression
: public Parser_expression
1914 Unknown_expression(Named_object
* named_object
, Location location
)
1915 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE
, location
),
1916 named_object_(named_object
), no_error_message_(false),
1917 is_composite_literal_key_(false)
1920 // The associated named object.
1922 named_object() const
1923 { return this->named_object_
; }
1925 // The name of the identifier which was unknown.
1929 // Call this to indicate that we should not give an error if this
1930 // name is never defined. This is used to avoid knock-on errors
1931 // during an erroneous parse.
1933 set_no_error_message()
1934 { this->no_error_message_
= true; }
1936 // Note that this expression is being used as the key in a composite
1937 // literal, so it may be OK if it is not resolved.
1939 set_is_composite_literal_key()
1940 { this->is_composite_literal_key_
= true; }
1942 // Note that this expression should no longer be treated as a
1943 // composite literal key.
1945 clear_is_composite_literal_key()
1946 { this->is_composite_literal_key_
= false; }
1950 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1954 { return new Unknown_expression(this->named_object_
, this->location()); }
1957 do_dump_expression(Ast_dump_context
*) const;
1960 // The unknown name.
1961 Named_object
* named_object_
;
1962 // True if we should not give errors if this is undefined. This is
1963 // used if there was a parse failure.
1964 bool no_error_message_
;
1965 // True if this is the key in a composite literal.
1966 bool is_composite_literal_key_
;
1969 // An index expression. This is lowered to an array index, a string
1970 // index, or a map index.
1972 class Index_expression
: public Parser_expression
1975 Index_expression(Expression
* left
, Expression
* start
, Expression
* end
,
1976 Expression
* cap
, Location location
)
1977 : Parser_expression(EXPRESSION_INDEX
, location
),
1978 left_(left
), start_(start
), end_(end
), cap_(cap
), is_lvalue_(false)
1981 // Record that this expression is an lvalue.
1984 { this->is_lvalue_
= true; }
1986 // Dump an index expression, i.e. an expression of the form
1987 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
1989 dump_index_expression(Ast_dump_context
*, const Expression
* expr
,
1990 const Expression
* start
, const Expression
* end
,
1991 const Expression
* cap
);
1995 do_traverse(Traverse
*);
1998 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2003 return new Index_expression(this->left_
->copy(), this->start_
->copy(),
2006 : this->end_
->copy()),
2009 : this->cap_
->copy()),
2014 do_must_eval_subexpressions_in_order(int* skip
) const
2021 do_dump_expression(Ast_dump_context
*) const;
2024 do_issue_nil_check()
2025 { this->left_
->issue_nil_check(); }
2027 // The expression being indexed.
2031 // The second index. This is NULL for an index, non-NULL for a
2034 // The capacity argument. This is NULL for indices and slices that use the
2035 // default capacity, non-NULL for indices and slices that specify the
2038 // Whether this is being used as an l-value. We set this during the
2039 // parse because map index expressions need to know.
2043 // An index into a map.
2045 class Map_index_expression
: public Expression
2048 Map_index_expression(Expression
* map
, Expression
* index
,
2050 : Expression(EXPRESSION_MAP_INDEX
, location
),
2051 map_(map
), index_(index
), is_lvalue_(false),
2052 is_in_tuple_assignment_(false), value_pointer_(NULL
)
2058 { return this->map_
; }
2062 { return this->map_
; }
2064 // Return the index.
2067 { return this->index_
; }
2071 { return this->index_
; }
2073 // Get the type of the map being indexed.
2075 get_map_type() const;
2077 // Record that this map expression is an lvalue. The difference is
2078 // that an lvalue always inserts the key.
2081 { this->is_lvalue_
= true; }
2083 // Return whether this map expression occurs in an assignment to a
2086 is_in_tuple_assignment() const
2087 { return this->is_in_tuple_assignment_
; }
2089 // Record that this map expression occurs in an assignment to a pair
2092 set_is_in_tuple_assignment()
2093 { this->is_in_tuple_assignment_
= true; }
2095 // Return an expression for the map index. This returns an expression which
2096 // evaluates to a pointer to a value in the map. If INSERT is true,
2097 // the key will be inserted if not present, and the value pointer
2098 // will be zero initialized. If INSERT is false, and the key is not
2099 // present in the map, the pointer will be NULL.
2101 get_value_pointer(bool insert
);
2105 do_traverse(Traverse
*);
2108 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2114 do_determine_type(const Type_context
*);
2117 do_check_types(Gogo
*);
2122 return Expression::make_map_index(this->map_
->copy(),
2123 this->index_
->copy(),
2128 do_must_eval_subexpressions_in_order(int* skip
) const
2134 // A map index expression is an lvalue but it is not addressable.
2137 do_get_backend(Translate_context
*);
2140 do_dump_expression(Ast_dump_context
*) const;
2143 // The map we are looking into.
2147 // Whether this is an lvalue.
2149 // Whether this is in a tuple assignment to a pair of values.
2150 bool is_in_tuple_assignment_
;
2151 // A pointer to the value at this index.
2152 Expression
* value_pointer_
;
2155 // An expression which represents a method bound to its first
2158 class Bound_method_expression
: public Expression
2161 Bound_method_expression(Expression
* expr
, const Method
*method
,
2162 Named_object
* function
, Location location
)
2163 : Expression(EXPRESSION_BOUND_METHOD
, location
),
2164 expr_(expr
), expr_type_(NULL
), method_(method
), function_(function
)
2167 // Return the object which is the first argument.
2170 { return this->expr_
; }
2172 // Return the implicit type of the first argument. This will be
2173 // non-NULL when using a method from an anonymous field without
2174 // using an explicit stub.
2176 first_argument_type() const
2177 { return this->expr_type_
; }
2179 // Return the method.
2182 { return this->method_
; }
2184 // Return the function to call.
2187 { return this->function_
; }
2189 // Set the implicit type of the expression.
2191 set_first_argument_type(Type
* type
)
2192 { this->expr_type_
= type
; }
2194 // Create a thunk to call FUNCTION, for METHOD, when it is used as
2195 // part of a method value.
2196 static Named_object
*
2197 create_thunk(Gogo
*, const Method
* method
, Named_object
* function
);
2201 do_traverse(Traverse
*);
2204 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2210 do_determine_type(const Type_context
*);
2213 do_check_types(Gogo
*);
2218 return new Bound_method_expression(this->expr_
->copy(), this->method_
,
2219 this->function_
, this->location());
2223 do_get_backend(Translate_context
*);
2226 do_dump_expression(Ast_dump_context
*) const;
2229 // A mapping from method functions to the thunks we have created for
2231 typedef Unordered_map(Named_object
*, Named_object
*) Method_value_thunks
;
2232 static Method_value_thunks method_value_thunks
;
2234 // The object used to find the method. This is passed to the method
2235 // as the first argument.
2237 // The implicit type of the object to pass to the method. This is
2238 // NULL in the normal case, non-NULL when using a method from an
2239 // anonymous field which does not require a stub.
2242 const Method
* method_
;
2243 // The function to call. This is not the same as
2244 // method_->named_object() when the method has a stub. This will be
2245 // the real function rather than the stub.
2246 Named_object
* function_
;
2249 // A reference to a field in a struct.
2251 class Field_reference_expression
: public Expression
2254 Field_reference_expression(Expression
* expr
, unsigned int field_index
,
2256 : Expression(EXPRESSION_FIELD_REFERENCE
, location
),
2257 expr_(expr
), field_index_(field_index
), implicit_(false), called_fieldtrack_(false)
2260 // Return the struct expression.
2263 { return this->expr_
; }
2265 // Return the field index.
2268 { return this->field_index_
; }
2270 // Return whether this node was implied by an anonymous field.
2273 { return this->implicit_
; }
2276 set_implicit(bool implicit
)
2277 { this->implicit_
= implicit
; }
2279 // Set the struct expression. This is used when parsing.
2281 set_struct_expression(Expression
* expr
)
2283 go_assert(this->expr_
== NULL
);
2289 do_traverse(Traverse
* traverse
)
2290 { return Expression::traverse(&this->expr_
, traverse
); }
2293 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2299 do_determine_type(const Type_context
*)
2300 { this->expr_
->determine_type_no_context(); }
2303 do_check_types(Gogo
*);
2308 return Expression::make_field_reference(this->expr_
->copy(),
2314 do_is_addressable() const
2315 { return this->expr_
->is_addressable(); }
2318 do_address_taken(bool escapes
)
2319 { this->expr_
->address_taken(escapes
); }
2322 do_issue_nil_check()
2323 { this->expr_
->issue_nil_check(); }
2326 do_get_backend(Translate_context
*);
2329 do_dump_expression(Ast_dump_context
*) const;
2332 // The expression we are looking into. This should have a type of
2335 // The zero-based index of the field we are retrieving.
2336 unsigned int field_index_
;
2337 // Whether this node was emitted implicitly for an embedded field,
2338 // that is, expr_ is not the expr_ of the original user node.
2340 // Whether we have already emitted a fieldtrack call.
2341 bool called_fieldtrack_
;
2344 // A reference to a field of an interface.
2346 class Interface_field_reference_expression
: public Expression
2349 Interface_field_reference_expression(Expression
* expr
,
2350 const std::string
& name
,
2352 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE
, location
),
2353 expr_(expr
), name_(name
)
2356 // Return the expression for the interface object.
2359 { return this->expr_
; }
2361 // Return the name of the method to call.
2364 { return this->name_
; }
2366 // Create a thunk to call the method NAME in TYPE when it is used as
2367 // part of a method value.
2368 static Named_object
*
2369 create_thunk(Gogo
*, Interface_type
* type
, const std::string
& name
);
2371 // Return an expression for the pointer to the function to call.
2375 // Return an expression for the first argument to pass to the interface
2376 // function. This is the real object associated with the interface object.
2378 get_underlying_object();
2382 do_traverse(Traverse
* traverse
);
2385 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2391 do_determine_type(const Type_context
*);
2394 do_check_types(Gogo
*);
2399 return Expression::make_interface_field_reference(this->expr_
->copy(),
2405 do_get_backend(Translate_context
*);
2408 do_dump_expression(Ast_dump_context
*) const;
2411 // A mapping from interface types to a list of thunks we have
2412 // created for methods.
2413 typedef std::vector
<std::pair
<std::string
, Named_object
*> > Method_thunks
;
2414 typedef Unordered_map(Interface_type
*, Method_thunks
*)
2415 Interface_method_thunks
;
2416 static Interface_method_thunks interface_method_thunks
;
2418 // The expression for the interface object. This should have a type
2419 // of interface or pointer to interface.
2421 // The field we are retrieving--the name of the method.
2425 // A type guard expression.
2427 class Type_guard_expression
: public Expression
2430 Type_guard_expression(Expression
* expr
, Type
* type
, Location location
)
2431 : Expression(EXPRESSION_TYPE_GUARD
, location
),
2432 expr_(expr
), type_(type
)
2435 // Return the expression to convert.
2438 { return this->expr_
; }
2440 // Return the type to which to convert.
2443 { return this->type_
; }
2447 do_traverse(Traverse
* traverse
);
2450 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2454 { return this->type_
; }
2457 do_determine_type(const Type_context
*)
2458 { this->expr_
->determine_type_no_context(); }
2461 do_check_types(Gogo
*);
2466 return new Type_guard_expression(this->expr_
->copy(), this->type_
,
2471 do_get_backend(Translate_context
*);
2474 do_dump_expression(Ast_dump_context
*) const;
2477 // The expression to convert.
2479 // The type to which to convert.
2483 // A receive expression.
2485 class Receive_expression
: public Expression
2488 Receive_expression(Expression
* channel
, Location location
)
2489 : Expression(EXPRESSION_RECEIVE
, location
),
2490 channel_(channel
), temp_receiver_(NULL
)
2493 // Return the channel.
2496 { return this->channel_
; }
2500 do_traverse(Traverse
* traverse
)
2501 { return Expression::traverse(&this->channel_
, traverse
); }
2504 do_discarding_value()
2511 do_flatten(Gogo
*, Named_object
*, Statement_inserter
*);
2514 do_determine_type(const Type_context
*)
2515 { this->channel_
->determine_type_no_context(); }
2518 do_check_types(Gogo
*);
2523 return Expression::make_receive(this->channel_
->copy(), this->location());
2527 do_must_eval_in_order() const
2531 do_get_backend(Translate_context
*);
2534 do_dump_expression(Ast_dump_context
*) const;
2537 // The channel from which we are receiving.
2538 Expression
* channel_
;
2539 // A temporary reference to the variable storing the received data.
2540 Temporary_statement
* temp_receiver_
;
2543 // A numeric constant. This is used both for untyped constants and
2544 // for constants that have a type.
2546 class Numeric_constant
2550 : classification_(NC_INVALID
), type_(NULL
)
2553 ~Numeric_constant();
2555 Numeric_constant(const Numeric_constant
&);
2557 Numeric_constant
& operator=(const Numeric_constant
&);
2559 // Set to an unsigned long value.
2561 set_unsigned_long(Type
*, unsigned long);
2563 // Set to an integer value.
2565 set_int(Type
*, const mpz_t
);
2567 // Set to a rune value.
2569 set_rune(Type
*, const mpz_t
);
2571 // Set to a floating point value.
2573 set_float(Type
*, const mpfr_t
);
2575 // Set to a complex value.
2577 set_complex(Type
*, const mpfr_t
, const mpfr_t
);
2582 { return this->classification_
== Numeric_constant::NC_INT
; }
2586 { return this->classification_
== Numeric_constant::NC_RUNE
; }
2590 { return this->classification_
== Numeric_constant::NC_FLOAT
; }
2594 { return this->classification_
== Numeric_constant::NC_COMPLEX
; }
2596 // Value retrievers. These will initialize the values as well as
2597 // set them. GET_INT is only valid if IS_INT returns true, and
2598 // likewise respectively.
2600 get_int(mpz_t
*) const;
2603 get_rune(mpz_t
*) const;
2606 get_float(mpfr_t
*) const;
2609 get_complex(mpfr_t
*, mpfr_t
*) const;
2611 // Codes returned by to_unsigned_long.
2612 enum To_unsigned_long
2614 // Value is integer and fits in unsigned long.
2616 // Value is not integer.
2618 // Value is integer but is negative.
2620 // Value is non-negative integer but does not fit in unsigned
2625 // If the value can be expressed as an integer that fits in an
2626 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
2627 // one of the other To_unsigned_long codes.
2629 to_unsigned_long(unsigned long* val
) const;
2631 // If the value can be expressed as an int, return true and
2632 // initialize and set VAL. This will return false for a value with
2633 // an explicit float or complex type, even if the value is integral.
2635 to_int(mpz_t
* val
) const;
2637 // If the value can be expressed as a float, return true and
2638 // initialize and set VAL.
2640 to_float(mpfr_t
* val
) const;
2642 // If the value can be expressed as a complex, return true and
2643 // initialize and set VR and VI.
2645 to_complex(mpfr_t
* vr
, mpfr_t
* vi
) const;
2651 // If the constant can be expressed in TYPE, then set the type of
2652 // the constant to TYPE and return true. Otherwise return false,
2653 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
2654 // the location to use for the error.
2656 set_type(Type
* type
, bool issue_error
, Location location
);
2658 // Return an Expression for this value.
2660 expression(Location
) const;
2667 mpz_to_unsigned_long(const mpz_t ival
, unsigned long *val
) const;
2670 mpfr_to_unsigned_long(const mpfr_t fval
, unsigned long *val
) const;
2673 check_int_type(Integer_type
*, bool, Location
) const;
2676 check_float_type(Float_type
*, bool, Location
);
2679 check_complex_type(Complex_type
*, bool, Location
);
2681 // The kinds of constants.
2691 // The kind of constant.
2692 Classification classification_
;
2696 // If NC_INT or NC_RUNE.
2707 // The type if there is one. This will be NULL for an untyped
2712 #endif // !defined(GO_EXPRESSIONS_H)