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
16 class Translate_context;
24 class Expression_list;
26 class Temporary_reference_expression;
27 class String_expression;
28 class Binary_expression;
29 class Call_expression;
30 class Func_expression;
31 class Unknown_expression;
32 class Index_expression;
33 class Map_index_expression;
34 class Bound_method_expression;
35 class Field_reference_expression;
36 class Interface_field_reference_expression;
37 class Type_guard_expression;
38 class Receive_expression;
39 class Send_expression;
43 class Temporary_statement;
46 // The base class for all expressions.
51 // The types of expressions.
52 enum Expression_classification
58 EXPRESSION_CONST_REFERENCE,
59 EXPRESSION_VAR_REFERENCE,
60 EXPRESSION_TEMPORARY_REFERENCE,
62 EXPRESSION_FUNC_REFERENCE,
63 EXPRESSION_UNKNOWN_REFERENCE,
72 EXPRESSION_CALL_RESULT,
73 EXPRESSION_BOUND_METHOD,
75 EXPRESSION_ARRAY_INDEX,
76 EXPRESSION_STRING_INDEX,
79 EXPRESSION_FIELD_REFERENCE,
80 EXPRESSION_INTERFACE_FIELD_REFERENCE,
81 EXPRESSION_ALLOCATION,
83 EXPRESSION_TYPE_GUARD,
84 EXPRESSION_CONVERSION,
85 EXPRESSION_STRUCT_CONSTRUCTION,
86 EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
87 EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
88 EXPRESSION_MAP_CONSTRUCTION,
89 EXPRESSION_COMPOSITE_LITERAL,
90 EXPRESSION_HEAP_COMPOSITE,
93 EXPRESSION_TYPE_DESCRIPTOR,
95 EXPRESSION_STRUCT_FIELD_OFFSET,
99 Expression(Expression_classification, source_location);
101 virtual ~Expression();
103 // Make an error expression. This is used when a parse error occurs
104 // to prevent cascading errors.
106 make_error(source_location);
108 // Make an expression which is really a type. This is used during
111 make_type(Type*, source_location);
113 // Make a unary expression.
115 make_unary(Operator, Expression*, source_location);
117 // Make a binary expression.
119 make_binary(Operator, Expression*, Expression*, source_location);
121 // Make a reference to a constant in an expression.
123 make_const_reference(Named_object*, source_location);
125 // Make a reference to a variable in an expression.
127 make_var_reference(Named_object*, source_location);
129 // Make a reference to a temporary variable. Temporary variables
130 // are always created by a single statement, which is what we use to
133 make_temporary_reference(Temporary_statement*, source_location);
135 // Make a sink expression--a reference to the blank identifier _.
137 make_sink(source_location);
139 // Make a reference to a function in an expression.
141 make_func_reference(Named_object*, Expression* closure, source_location);
143 // Make a reference to an unknown name. In a correct program this
144 // will always be lowered to a real const/var/func reference.
146 make_unknown_reference(Named_object*, source_location);
148 // Make a constant bool expression.
150 make_boolean(bool val, source_location);
152 // Make a constant string expression.
154 make_string(const std::string&, source_location);
156 // Make a constant integer expression. TYPE should be NULL for an
159 make_integer(const mpz_t*, Type*, source_location);
161 // Make a constant float expression. TYPE should be NULL for an
164 make_float(const mpfr_t*, Type*, source_location);
166 // Make a constant complex expression. TYPE should be NULL for an
169 make_complex(const mpfr_t* real, const mpfr_t* imag, Type*, source_location);
171 // Make a nil expression.
173 make_nil(source_location);
175 // Make an iota expression. This is used for the predeclared
180 // Make a call expression.
181 static Call_expression*
182 make_call(Expression* func, Expression_list* args, bool is_varargs,
185 // Make a reference to a specific result of a call expression which
188 make_call_result(Call_expression*, unsigned int index);
190 // Make an expression which is a method bound to its first
192 static Bound_method_expression*
193 make_bound_method(Expression* object, Expression* method, source_location);
195 // Make an index or slice expression. This is a parser expression
196 // which represents LEFT[START:END]. END may be NULL, meaning an
197 // index rather than a slice. At parse time we may not know the
198 // type of LEFT. After parsing this is lowered to an array index, a
199 // string index, or a map index.
201 make_index(Expression* left, Expression* start, Expression* end,
204 // Make an array index expression. END may be NULL, in which case
205 // this is an lvalue.
207 make_array_index(Expression* array, Expression* start, Expression* end,
210 // Make a string index expression. END may be NULL. This is never
213 make_string_index(Expression* string, Expression* start, Expression* end,
216 // Make a map index expression. This is an lvalue.
217 static Map_index_expression*
218 make_map_index(Expression* map, Expression* val, source_location);
220 // Make a selector. This is a parser expression which represents
221 // LEFT.NAME. At parse time we may not know the type of the left
224 make_selector(Expression* left, const std::string& name, source_location);
226 // Make a reference to a field in a struct.
227 static Field_reference_expression*
228 make_field_reference(Expression*, unsigned int field_index, source_location);
230 // Make a reference to a field of an interface, with an associated
233 make_interface_field_reference(Expression*, const std::string&,
236 // Make an allocation expression.
238 make_allocation(Type*, source_location);
240 // Make a call to the builtin function make.
242 make_make(Type*, Expression_list*, source_location);
244 // Make a type guard expression.
246 make_type_guard(Expression*, Type*, source_location);
248 // Make a type cast expression.
250 make_cast(Type*, Expression*, source_location);
252 // Make a composite literal. The DEPTH parameter is how far down we
253 // are in a list of composite literals with omitted types.
255 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
258 // Make a struct composite literal.
260 make_struct_composite_literal(Type*, Expression_list*, source_location);
262 // Make a slice composite literal.
264 make_slice_composite_literal(Type*, Expression_list*, source_location);
266 // Take a composite literal and allocate it on the heap.
268 make_heap_composite(Expression*, source_location);
270 // Make a receive expression. VAL is NULL for a unary receive.
271 static Receive_expression*
272 make_receive(Expression* channel, source_location);
274 // Make a send expression.
275 static Send_expression*
276 make_send(Expression* channel, Expression* val, source_location);
278 // Make an expression which evaluates to the type descriptor of a
281 make_type_descriptor(Type* type, source_location);
283 // Make an expression which evaluates to some characteristic of a
284 // type. These are only used for type descriptors, so there is no
285 // location parameter.
288 // The size of a value of the type.
290 // The required alignment of a value of the type.
292 // The required alignment of a value of the type when used as a
293 // field in a struct.
294 TYPE_INFO_FIELD_ALIGNMENT
298 make_type_info(Type* type, Type_info);
300 // Make an expression which evaluates to the offset of a field in a
301 // struct. This is only used for type descriptors, so there is no
302 // location parameter.
304 make_struct_field_offset(Struct_type*, const Struct_field*);
306 // Make an expression which evaluates to the address of an unnamed
309 make_label_addr(Label*, source_location);
311 // Return the expression classification.
312 Expression_classification
313 classification() const
314 { return this->classification_; }
316 // Return the location of the expression.
319 { return this->location_; }
321 // Return whether this is a constant expression.
324 { return this->do_is_constant(); }
326 // If this is not a constant expression with integral type, return
327 // false. If it is one, return true, and set VAL to the value. VAL
328 // should already be initialized. If this returns true, it sets
329 // *PTYPE to the type of the value, or NULL for an abstract type.
330 // If IOTA_IS_CONSTANT is true, then an iota expression is assumed
331 // to have its final value.
333 integer_constant_value(bool iota_is_constant, mpz_t val, Type** ptype) const;
335 // If this is not a constant expression with floating point type,
336 // return false. If it is one, return true, and set VAL to the
337 // value. VAL should already be initialized. If this returns true,
338 // it sets *PTYPE to the type of the value, or NULL for an abstract
341 float_constant_value(mpfr_t val, Type** ptype) const;
343 // If this is not a constant expression with complex type, return
344 // false. If it is one, return true, and set REAL and IMAG to the
345 // value. REAL and IMAG should already be initialized. If this
346 // return strue, it sets *PTYPE to the type of the value, or NULL
347 // for an abstract type.
349 complex_constant_value(mpfr_t real, mpfr_t imag, Type** ptype) const;
351 // If this is not a constant expression with string type, return
352 // false. If it is one, return true, and set VAL to the value.
354 string_constant_value(std::string* val) const
355 { return this->do_string_constant_value(val); }
357 // This is called by the parser if the value of this expression is
358 // being discarded. This issues warnings about computed values
359 // being unused, and handles send expressions which act differently
360 // depending upon whether the value is used.
363 { this->do_discarding_value(); }
365 // Return whether this is an error expression.
367 is_error_expression() const
368 { return this->classification_ == EXPRESSION_ERROR; }
370 // Return whether this expression really represents a type.
372 is_type_expression() const
373 { return this->classification_ == EXPRESSION_TYPE; }
375 // If this is a variable reference, return the Var_expression
376 // structure. Otherwise, return NULL. This is a controlled dynamic
380 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
382 const Var_expression*
383 var_expression() const
384 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
386 // If this is a reference to a temporary variable, return the
387 // Temporary_reference_expression. Otherwise, return NULL.
388 Temporary_reference_expression*
389 temporary_reference_expression()
391 return this->convert<Temporary_reference_expression,
392 EXPRESSION_TEMPORARY_REFERENCE>();
395 // Return whether this is a sink expression.
397 is_sink_expression() const
398 { return this->classification_ == EXPRESSION_SINK; }
400 // If this is a string expression, return the String_expression
401 // structure. Otherwise, return NULL.
404 { return this->convert<String_expression, EXPRESSION_STRING>(); }
406 // Return whether this is the expression nil.
408 is_nil_expression() const
409 { return this->classification_ == EXPRESSION_NIL; }
411 // If this is an indirection through a pointer, return the
412 // expression being pointed through. Otherwise return this.
416 // If this is a binary expression, return the Binary_expression
417 // structure. Otherwise return NULL.
420 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
422 // If this is a call expression, return the Call_expression
423 // structure. Otherwise, return NULL. This is a controlled dynamic
427 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
429 // If this is an expression which refers to a function, return the
430 // Func_expression structure. Otherwise, return NULL.
433 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
435 const Func_expression*
436 func_expression() const
437 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
439 // If this is an expression which refers to an unknown name, return
440 // the Unknown_expression structure. Otherwise, return NULL.
443 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
445 const Unknown_expression*
446 unknown_expression() const
448 return this->convert<const Unknown_expression,
449 EXPRESSION_UNKNOWN_REFERENCE>();
452 // If this is an index expression, return the Index_expression
453 // structure. Otherwise, return NULL.
456 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
458 // If this is an expression which refers to indexing in a map,
459 // return the Map_index_expression structure. Otherwise, return
461 Map_index_expression*
462 map_index_expression()
463 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
465 // If this is a bound method expression, return the
466 // Bound_method_expression structure. Otherwise, return NULL.
467 Bound_method_expression*
468 bound_method_expression()
469 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
471 // If this is a reference to a field in a struct, return the
472 // Field_reference_expression structure. Otherwise, return NULL.
473 Field_reference_expression*
474 field_reference_expression()
476 return this->convert<Field_reference_expression,
477 EXPRESSION_FIELD_REFERENCE>();
480 // If this is a reference to a field in an interface, return the
481 // Interface_field_reference_expression structure. Otherwise,
483 Interface_field_reference_expression*
484 interface_field_reference_expression()
486 return this->convert<Interface_field_reference_expression,
487 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
490 // If this is a type guard expression, return the
491 // Type_guard_expression structure. Otherwise, return NULL.
492 Type_guard_expression*
493 type_guard_expression()
494 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
496 // If this is a receive expression, return the Receive_expression
497 // structure. Otherwise, return NULL.
500 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
502 // Return true if this is a composite literal.
504 is_composite_literal() const;
506 // Return true if this is a composite literal which is not constant.
508 is_nonconstant_composite_literal() const;
510 // Return true if this is a reference to a local variable.
512 is_local_variable() const;
514 // Traverse an expression.
516 traverse(Expression**, Traverse*);
518 // Traverse subexpressions of this expression.
520 traverse_subexpressions(Traverse*);
522 // Lower an expression. This is called immediately after parsing.
523 // IOTA_VALUE is the value that we should give to any iota
524 // expressions. This function must resolve expressions which could
525 // not be fully parsed into their final form. It returns the same
526 // Expression or a new one.
528 lower(Gogo* gogo, Named_object* function, int iota_value)
529 { return this->do_lower(gogo, function, iota_value); }
531 // Determine the real type of an expression with abstract integer,
532 // floating point, or complex type. TYPE_CONTEXT describes the
535 determine_type(const Type_context*);
537 // Check types in an expression.
539 check_types(Gogo* gogo)
540 { this->do_check_types(gogo); }
542 // Determine the type when there is no context.
544 determine_type_no_context();
546 // Return the current type of the expression. This may be changed
547 // by determine_type.
550 { return this->do_type(); }
552 // Return a copy of an expression.
555 { return this->do_copy(); }
557 // Return whether the expression is addressable--something which may
558 // be used as the operand of the unary & operator.
560 is_addressable() const
561 { return this->do_is_addressable(); }
563 // Note that we are taking the address of this expression. ESCAPES
564 // is true if this address escapes the current function.
566 address_taken(bool escapes)
567 { this->do_address_taken(escapes); }
569 // Return whether this expression must be evaluated in order
570 // according to the order of evaluation rules. This is basically
571 // true of all expressions with side-effects.
573 must_eval_in_order() const
574 { return this->do_must_eval_in_order(); }
576 // Return the tree for this expression.
578 get_tree(Translate_context*);
580 // Return a tree handling any conversions which must be done during
583 convert_for_assignment(Translate_context*, Type* lhs_type, Type* rhs_type,
584 tree rhs_tree, source_location location);
586 // Return a tree converting a value of one interface type to another
587 // interface type. If FOR_TYPE_GUARD is true this is for a type
590 convert_interface_to_interface(Translate_context*, Type* lhs_type,
591 Type* rhs_type, tree rhs_tree,
592 bool for_type_guard, source_location);
594 // Return a tree implementing the comparison LHS_TREE OP RHS_TREE.
595 // TYPE is the type of both sides.
597 comparison_tree(Translate_context*, Operator op, Type* left_type,
598 tree left_tree, Type* right_type, tree right_tree,
601 // Return a tree for the multi-precision integer VAL in TYPE.
603 integer_constant_tree(mpz_t val, tree type);
605 // Return a tree for the floating point value VAL in TYPE.
607 float_constant_tree(mpfr_t val, tree type);
609 // Return a tree for the complex value REAL/IMAG in TYPE.
611 complex_constant_tree(mpfr_t real, mpfr_t imag, tree type);
613 // Export the expression. This is only used for constants. It will
614 // be used for things like values of named constants and sizes of
617 export_expression(Export* exp) const
618 { this->do_export(exp); }
620 // Import an expression.
622 import_expression(Import*);
624 // Return a tree which checks that VAL, of arbitrary integer type,
625 // is non-negative and is not more than the maximum value of
626 // BOUND_TYPE. If SOFAR is not NULL, it is or'red into the result.
627 // The return value may be NULL if SOFAR is NULL.
629 check_bounds(tree val, tree bound_type, tree sofar, source_location);
632 // May be implemented by child class: traverse the expressions.
634 do_traverse(Traverse*);
636 // Return a lowered expression.
638 do_lower(Gogo*, Named_object*, int)
641 // Return whether this is a constant expression.
643 do_is_constant() const
646 // Return whether this is a constant expression of integral type,
647 // and set VAL to the value.
649 do_integer_constant_value(bool, mpz_t, Type**) const
652 // Return whether this is a constant expression of floating point
653 // type, and set VAL to the value.
655 do_float_constant_value(mpfr_t, Type**) const
658 // Return whether this is a constant expression of complex type, and
659 // set REAL and IMAGE to the value.
661 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const
664 // Return whether this is a constant expression of string type, and
665 // set VAL to the value.
667 do_string_constant_value(std::string*) const
670 // Called by the parser if the value is being discarded.
672 do_discarding_value();
674 // Child class holds type.
678 // Child class implements determining type information.
680 do_determine_type(const Type_context*) = 0;
682 // Child class implements type checking if needed.
684 do_check_types(Gogo*)
687 // Child class implements copying.
691 // Child class implements whether the expression is addressable.
693 do_is_addressable() const
696 // Child class implements taking the address of an expression.
698 do_address_taken(bool)
701 // Child class implements whether this expression must be evaluated
704 do_must_eval_in_order() const
707 // Child class implements conversion to tree.
709 do_get_tree(Translate_context*) = 0;
711 // Child class implements export.
713 do_export(Export*) const;
715 // For children to call to warn about an unused value.
717 warn_about_unused_value();
719 // For children to call when they detect that they are in error.
723 // For children to call to report an error conveniently.
725 report_error(const char*);
728 // Convert to the desired statement classification, or return NULL.
729 // This is a controlled dynamic cast.
730 template<typename Expression_class,
731 Expression_classification expr_classification>
735 return (this->classification_ == expr_classification
736 ? static_cast<Expression_class*>(this)
740 template<typename Expression_class,
741 Expression_classification expr_classification>
742 const Expression_class*
745 return (this->classification_ == expr_classification
746 ? static_cast<const Expression_class*>(this)
751 convert_type_to_interface(Translate_context*, Type*, Type*, tree,
755 get_interface_type_descriptor(Translate_context*, Type*, tree,
759 convert_interface_to_type(Translate_context*, Type*, Type*, tree,
762 // The expression classification.
763 Expression_classification classification_;
764 // The location in the input file.
765 source_location location_;
768 // A list of Expressions.
770 class Expression_list
777 // Return whether the list is empty.
780 { return this->entries_.empty(); }
782 // Return the number of entries in the list.
785 { return this->entries_.size(); }
787 // Add an entry to the end of the list.
789 push_back(Expression* expr)
790 { this->entries_.push_back(expr); }
793 append(Expression_list* add)
794 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
796 // Reserve space in the list.
799 { this->entries_.reserve(size); }
801 // Traverse the expressions in the list.
809 // Return true if the list contains an error expression.
811 contains_error() const;
813 // Return the first and last elements.
816 { return this->entries_.front(); }
820 { return this->entries_.front(); }
824 { return this->entries_.back(); }
828 { return this->entries_.back(); }
832 typedef std::vector<Expression*>::iterator iterator;
833 typedef std::vector<Expression*>::const_iterator const_iterator;
837 { return this->entries_.begin(); }
841 { return this->entries_.begin(); }
845 { return this->entries_.end(); }
849 { return this->entries_.end(); }
854 { this->entries_.erase(p); }
857 std::vector<Expression*> entries_;
860 // An abstract base class for an expression which is only used by the
861 // parser, and is lowered in the lowering pass.
863 class Parser_expression : public Expression
866 Parser_expression(Expression_classification classification,
867 source_location location)
868 : Expression(classification, location)
873 do_lower(Gogo*, Named_object*, int) = 0;
879 do_determine_type(const Type_context*)
880 { gcc_unreachable(); }
883 do_check_types(Gogo*)
884 { gcc_unreachable(); }
887 do_get_tree(Translate_context*)
888 { gcc_unreachable(); }
891 // An expression which is simply a variable.
893 class Var_expression : public Expression
896 Var_expression(Named_object* variable, source_location location)
897 : Expression(EXPRESSION_VAR_REFERENCE, location),
901 // Return the variable.
904 { return this->variable_; }
906 // Return the name of the variable.
912 do_lower(Gogo*, Named_object*, int);
918 do_determine_type(const Type_context*);
925 do_is_addressable() const
929 do_address_taken(bool);
932 do_get_tree(Translate_context*);
935 // The variable we are referencing.
936 Named_object* variable_;
939 // A reference to a temporary variable.
941 class Temporary_reference_expression : public Expression
944 Temporary_reference_expression(Temporary_statement* statement,
945 source_location location)
946 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
947 statement_(statement)
955 do_determine_type(const Type_context*)
960 { return make_temporary_reference(this->statement_, this->location()); }
963 do_is_addressable() const
967 do_address_taken(bool);
970 do_get_tree(Translate_context*);
973 // The statement where the temporary variable is defined.
974 Temporary_statement* statement_;
977 // A string expression.
979 class String_expression : public Expression
982 String_expression(const std::string& val, source_location location)
983 : Expression(EXPRESSION_STRING, location),
984 val_(val), type_(NULL)
989 { return this->val_; }
996 do_is_constant() const
1000 do_string_constant_value(std::string* val) const
1010 do_determine_type(const Type_context*);
1017 do_get_tree(Translate_context*);
1020 do_export(Export*) const;
1023 // The string value. This is immutable.
1024 const std::string val_;
1025 // The type as determined by context.
1029 // A binary expression.
1031 class Binary_expression : public Expression
1034 Binary_expression(Operator op, Expression* left, Expression* right,
1035 source_location location)
1036 : Expression(EXPRESSION_BINARY, location),
1037 op_(op), left_(left), right_(right)
1040 // Return the operator.
1043 { return this->op_; }
1045 // Return the left hand expression.
1048 { return this->left_; }
1050 // Return the right hand expression.
1053 { return this->right_; }
1055 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1056 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
1057 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true
1058 // if this could be done, false if not.
1060 eval_integer(Operator op, Type* left_type, mpz_t left_val,
1061 Type* right_type, mpz_t right_val, source_location,
1064 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1065 // Return true if this could be done, false if not.
1067 eval_float(Operator op, Type* left_type, mpfr_t left_val,
1068 Type* right_type, mpfr_t right_val, mpfr_t val,
1071 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
1072 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
1073 // could be done, false if not.
1075 eval_complex(Operator op, Type* left_type, mpfr_t left_real,
1076 mpfr_t left_imag, Type* right_type, mpfr_t right_real,
1077 mpfr_t right_imag, mpfr_t real, mpfr_t imag, source_location);
1079 // Compare integer constants according to OP.
1081 compare_integer(Operator op, mpz_t left_val, mpz_t right_val);
1083 // Compare floating point constants according to OP.
1085 compare_float(Operator op, Type* type, mpfr_t left_val, mpfr_t right_val);
1087 // Compare complex constants according to OP.
1089 compare_complex(Operator op, Type* type, mpfr_t left_real, mpfr_t left_imag,
1090 mpfr_t right_val, mpfr_t right_imag);
1095 // Report an error if OP can not be applied to TYPE. Return whether
1098 check_operator_type(Operator op, Type* type, source_location);
1102 do_traverse(Traverse* traverse);
1105 do_lower(Gogo*, Named_object*, int);
1108 do_is_constant() const
1109 { return this->left_->is_constant() && this->right_->is_constant(); }
1112 do_integer_constant_value(bool, mpz_t val, Type**) const;
1115 do_float_constant_value(mpfr_t val, Type**) const;
1118 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
1121 do_discarding_value();
1127 do_determine_type(const Type_context*);
1130 do_check_types(Gogo*);
1135 return Expression::make_binary(this->op_, this->left_->copy(),
1136 this->right_->copy(), this->location());
1140 do_get_tree(Translate_context*);
1143 do_export(Export*) const;
1146 // The binary operator to apply.
1148 // The left hand side operand.
1150 // The right hand side operand.
1154 // A call expression. The go statement needs to dig inside this.
1156 class Call_expression : public Expression
1159 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
1160 source_location location)
1161 : Expression(EXPRESSION_CALL, location),
1162 fn_(fn), args_(args), type_(NULL), tree_(NULL), is_varargs_(is_varargs),
1163 varargs_are_lowered_(false), types_are_determined_(false),
1167 // The function to call.
1170 { return this->fn_; }
1175 { return this->args_; }
1177 const Expression_list*
1179 { return this->args_; }
1181 // Get the function type.
1183 get_function_type() const;
1185 // Return the number of values this call will return.
1187 result_count() const;
1189 // Return whether this is a call to the predeclared function
1192 is_recover_call() const;
1194 // Set the argument for a call to recover.
1196 set_recover_arg(Expression*);
1198 // Whether the last argument is a varargs argument (f(a...)).
1201 { return this->is_varargs_; }
1203 // Whether this call is being deferred.
1206 { return this->is_deferred_; }
1208 // Note that the call is being deferred.
1211 { this->is_deferred_ = true; }
1215 do_traverse(Traverse*);
1218 do_lower(Gogo*, Named_object*, int);
1221 do_discarding_value()
1228 do_determine_type(const Type_context*);
1231 do_check_types(Gogo*);
1236 return Expression::make_call(this->fn_->copy(),
1237 (this->args_ == NULL
1239 : this->args_->copy()),
1240 this->is_varargs_, this->location());
1244 do_must_eval_in_order() const;
1247 do_get_tree(Translate_context*);
1250 do_is_recover_call() const;
1253 do_set_recover_arg(Expression*);
1255 // Let a builtin expression change the argument list.
1257 set_args(Expression_list* args)
1258 { this->args_ = args; }
1260 // Let a builtin expression lower varargs.
1262 lower_varargs(Gogo*, Named_object* function, Type* varargs_type,
1263 size_t param_count);
1265 // Let a builtin expression check whether types have been
1268 determining_types();
1272 check_argument_type(int, const Type*, const Type*, source_location, bool);
1275 bound_method_function(Translate_context*, Bound_method_expression*, tree*);
1278 interface_method_function(Translate_context*,
1279 Interface_field_reference_expression*,
1282 // The function to call.
1284 // The arguments to pass. This may be NULL if there are no
1286 Expression_list* args_;
1287 // The type of the expression, to avoid recomputing it.
1289 // The tree for the call, used for a call which returns a tuple.
1291 // True if the last argument is a varargs argument (f(a...)).
1293 // True if varargs have already been lowered.
1294 bool varargs_are_lowered_;
1295 // True if types have been determined.
1296 bool types_are_determined_;
1297 // True if the call is an argument to a defer statement.
1301 // An expression which represents a pointer to a function.
1303 class Func_expression : public Expression
1306 Func_expression(Named_object* function, Expression* closure,
1307 source_location location)
1308 : Expression(EXPRESSION_FUNC_REFERENCE, location),
1309 function_(function), closure_(closure)
1312 // Return the object associated with the function.
1314 named_object() const
1315 { return this->function_; }
1317 // Return the name of the function.
1321 // Return the closure for this function. This will return NULL if
1322 // the function has no closure, which is the normal case.
1325 { return this->closure_; }
1327 // Return a tree for this function without evaluating the closure.
1329 get_tree_without_closure(Gogo*);
1333 do_traverse(Traverse*);
1339 do_determine_type(const Type_context*)
1341 if (this->closure_ != NULL)
1342 this->closure_->determine_type_no_context();
1348 return Expression::make_func_reference(this->function_,
1349 (this->closure_ == NULL
1351 : this->closure_->copy()),
1356 do_get_tree(Translate_context*);
1359 // The function itself.
1360 Named_object* function_;
1361 // A closure. This is normally NULL. For a nested function, it may
1362 // be a heap-allocated struct holding pointers to all the variables
1363 // referenced by this function and defined in enclosing functions.
1364 Expression* closure_;
1367 // A reference to an unknown name.
1369 class Unknown_expression : public Parser_expression
1372 Unknown_expression(Named_object* named_object, source_location location)
1373 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
1374 named_object_(named_object), is_composite_literal_key_(false)
1377 // The associated named object.
1379 named_object() const
1380 { return this->named_object_; }
1382 // The name of the identifier which was unknown.
1386 // Note that this expression is being used as the key in a composite
1387 // literal, so it may be OK if it is not resolved.
1389 set_is_composite_literal_key()
1390 { this->is_composite_literal_key_ = true; }
1392 // Note that this expression should no longer be treated as a
1393 // composite literal key.
1395 clear_is_composite_literal_key()
1396 { this->is_composite_literal_key_ = false; }
1400 do_lower(Gogo*, Named_object*, int);
1404 { return new Unknown_expression(this->named_object_, this->location()); }
1407 // The unknown name.
1408 Named_object* named_object_;
1409 // True if this is the key in a composite literal.
1410 bool is_composite_literal_key_;
1413 // An index expression. This is lowered to an array index, a string
1414 // index, or a map index.
1416 class Index_expression : public Parser_expression
1419 Index_expression(Expression* left, Expression* start, Expression* end,
1420 source_location location)
1421 : Parser_expression(EXPRESSION_INDEX, location),
1422 left_(left), start_(start), end_(end), is_lvalue_(false)
1425 // Record that this expression is an lvalue.
1428 { this->is_lvalue_ = true; }
1432 do_traverse(Traverse*);
1435 do_lower(Gogo*, Named_object*, int);
1440 return new Index_expression(this->left_->copy(), this->start_->copy(),
1443 : this->end_->copy()),
1448 // The expression being indexed.
1452 // The second index. This is NULL for an index, non-NULL for a
1455 // Whether this is being used as an l-value. We set this during the
1456 // parse because map index expressions need to know.
1460 // An index into a map.
1462 class Map_index_expression : public Expression
1465 Map_index_expression(Expression* map, Expression* index,
1466 source_location location)
1467 : Expression(EXPRESSION_MAP_INDEX, location),
1468 map_(map), index_(index), is_lvalue_(false),
1469 is_in_tuple_assignment_(false)
1475 { return this->map_; }
1479 { return this->map_; }
1481 // Return the index.
1484 { return this->index_; }
1488 { return this->index_; }
1490 // Get the type of the map being indexed.
1492 get_map_type() const;
1494 // Record that this map expression is an lvalue. The difference is
1495 // that an lvalue always inserts the key.
1498 { this->is_lvalue_ = true; }
1500 // Return whether this map expression occurs in an assignment to a
1503 is_in_tuple_assignment() const
1504 { return this->is_in_tuple_assignment_; }
1506 // Record that this map expression occurs in an assignment to a pair
1509 set_is_in_tuple_assignment()
1510 { this->is_in_tuple_assignment_ = true; }
1512 // Return a tree for the map index. This returns a tree which
1513 // evaluates to a pointer to a value in the map. If INSERT is true,
1514 // the key will be inserted if not present, and the value pointer
1515 // will be zero initialized. If INSERT is false, and the key is not
1516 // present in the map, the pointer will be NULL.
1518 get_value_pointer(Translate_context*, bool insert);
1522 do_traverse(Traverse*);
1528 do_determine_type(const Type_context*);
1531 do_check_types(Gogo*);
1536 return Expression::make_map_index(this->map_->copy(),
1537 this->index_->copy(),
1541 // A map index expression is an lvalue but it is not addressable.
1544 do_get_tree(Translate_context*);
1547 // The map we are looking into.
1551 // Whether this is an lvalue.
1553 // Whether this is in a tuple assignment to a pair of values.
1554 bool is_in_tuple_assignment_;
1557 // An expression which represents a method bound to its first
1560 class Bound_method_expression : public Expression
1563 Bound_method_expression(Expression* expr, Expression* method,
1564 source_location location)
1565 : Expression(EXPRESSION_BOUND_METHOD, location),
1566 expr_(expr), expr_type_(NULL), method_(method)
1569 // Return the object which is the first argument.
1572 { return this->expr_; }
1574 // Return the implicit type of the first argument. This will be
1575 // non-NULL when using a method from an anonymous field without
1576 // using an explicit stub.
1578 first_argument_type() const
1579 { return this->expr_type_; }
1581 // Return the reference to the method function.
1584 { return this->method_; }
1586 // Set the implicit type of the expression.
1588 set_first_argument_type(Type* type)
1589 { this->expr_type_ = type; }
1593 do_traverse(Traverse*);
1599 do_determine_type(const Type_context*);
1602 do_check_types(Gogo*);
1607 return new Bound_method_expression(this->expr_->copy(),
1608 this->method_->copy(),
1613 do_get_tree(Translate_context*);
1616 // The object used to find the method. This is passed to the method
1617 // as the first argument.
1619 // The implicit type of the object to pass to the method. This is
1620 // NULL in the normal case, non-NULL when using a method from an
1621 // anonymous field which does not require a stub.
1623 // The method itself. This is a Func_expression.
1624 Expression* method_;
1627 // A reference to a field in a struct.
1629 class Field_reference_expression : public Expression
1632 Field_reference_expression(Expression* expr, unsigned int field_index,
1633 source_location location)
1634 : Expression(EXPRESSION_FIELD_REFERENCE, location),
1635 expr_(expr), field_index_(field_index)
1638 // Return the struct expression.
1641 { return this->expr_; }
1643 // Return the field index.
1646 { return this->field_index_; }
1648 // Set the struct expression. This is used when parsing.
1650 set_struct_expression(Expression* expr)
1652 gcc_assert(this->expr_ == NULL);
1658 do_traverse(Traverse* traverse)
1659 { return Expression::traverse(&this->expr_, traverse); }
1665 do_determine_type(const Type_context*)
1666 { this->expr_->determine_type_no_context(); }
1669 do_check_types(Gogo*);
1674 return Expression::make_field_reference(this->expr_->copy(),
1680 do_is_addressable() const
1681 { return this->expr_->is_addressable(); }
1684 do_get_tree(Translate_context*);
1687 // The expression we are looking into. This should have a type of
1690 // The zero-based index of the field we are retrieving.
1691 unsigned int field_index_;
1694 // A reference to a field of an interface.
1696 class Interface_field_reference_expression : public Expression
1699 Interface_field_reference_expression(Expression* expr,
1700 const std::string& name,
1701 source_location location)
1702 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
1703 expr_(expr), name_(name)
1706 // Return the expression for the interface object.
1709 { return this->expr_; }
1711 // Return the name of the method to call.
1714 { return this->name_; }
1716 // Return a tree for the pointer to the function to call, given a
1717 // tree for the expression.
1719 get_function_tree(Translate_context*, tree);
1721 // Return a tree for the first argument to pass to the interface
1722 // function, given a tree for the expression. This is the real
1723 // object associated with the interface object.
1725 get_underlying_object_tree(Translate_context*, tree);
1729 do_traverse(Traverse* traverse);
1735 do_determine_type(const Type_context*);
1738 do_check_types(Gogo*);
1743 return Expression::make_interface_field_reference(this->expr_->copy(),
1749 do_get_tree(Translate_context*);
1752 // The expression for the interface object. This should have a type
1753 // of interface or pointer to interface.
1755 // The field we are retrieving--the name of the method.
1759 // A type guard expression.
1761 class Type_guard_expression : public Expression
1764 Type_guard_expression(Expression* expr, Type* type, source_location location)
1765 : Expression(EXPRESSION_TYPE_GUARD, location),
1766 expr_(expr), type_(type)
1769 // Return the expression to convert.
1772 { return this->expr_; }
1774 // Return the type to which to convert.
1777 { return this->type_; }
1781 do_traverse(Traverse* traverse);
1785 { return this->type_; }
1788 do_determine_type(const Type_context*)
1789 { this->expr_->determine_type_no_context(); }
1792 do_check_types(Gogo*);
1797 return new Type_guard_expression(this->expr_->copy(), this->type_,
1802 do_get_tree(Translate_context*);
1805 // The expression to convert.
1807 // The type to which to convert.
1811 // A receive expression.
1813 class Receive_expression : public Expression
1816 Receive_expression(Expression* channel, source_location location)
1817 : Expression(EXPRESSION_RECEIVE, location),
1818 channel_(channel), is_value_discarded_(false), for_select_(false)
1821 // Return the channel.
1824 { return this->channel_; }
1826 // Note that this is for a select statement.
1829 { this->for_select_ = true; }
1833 do_traverse(Traverse* traverse)
1834 { return Expression::traverse(&this->channel_, traverse); }
1837 do_discarding_value()
1838 { this->is_value_discarded_ = true; }
1844 do_determine_type(const Type_context*)
1845 { this->channel_->determine_type_no_context(); }
1848 do_check_types(Gogo*);
1853 return Expression::make_receive(this->channel_->copy(), this->location());
1857 do_must_eval_in_order() const
1861 do_get_tree(Translate_context*);
1864 // The channel from which we are receiving.
1865 Expression* channel_;
1866 // Whether the value is being discarded.
1867 bool is_value_discarded_;
1868 // Whether this is for a select statement.
1872 // A send expression.
1874 class Send_expression : public Expression
1877 Send_expression(Expression* channel, Expression* val,
1878 source_location location)
1879 : Expression(EXPRESSION_SEND, location),
1880 channel_(channel), val_(val), is_value_discarded_(false),
1884 // Note that this is for a select statement.
1887 { this->for_select_ = true; }
1891 do_traverse(Traverse* traverse);
1894 do_discarding_value()
1895 { this->is_value_discarded_ = true; }
1901 do_determine_type(const Type_context*);
1904 do_check_types(Gogo*);
1909 return Expression::make_send(this->channel_->copy(), this->val_->copy(),
1914 do_must_eval_in_order() const
1918 do_get_tree(Translate_context*);
1921 // The channel on which to send the value.
1922 Expression* channel_;
1923 // The value to send.
1925 // Whether the value is being discarded.
1926 bool is_value_discarded_;
1927 // Whether this is for a select statement.
1931 #endif // !defined(GO_EXPRESSIONS_H)