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;
42 class Temporary_statement;
45 // The base class for all expressions.
50 // The types of expressions.
51 enum Expression_classification
57 EXPRESSION_CONST_REFERENCE,
58 EXPRESSION_VAR_REFERENCE,
59 EXPRESSION_TEMPORARY_REFERENCE,
61 EXPRESSION_FUNC_REFERENCE,
62 EXPRESSION_UNKNOWN_REFERENCE,
71 EXPRESSION_CALL_RESULT,
72 EXPRESSION_BOUND_METHOD,
74 EXPRESSION_ARRAY_INDEX,
75 EXPRESSION_STRING_INDEX,
78 EXPRESSION_FIELD_REFERENCE,
79 EXPRESSION_INTERFACE_FIELD_REFERENCE,
80 EXPRESSION_ALLOCATION,
82 EXPRESSION_TYPE_GUARD,
83 EXPRESSION_CONVERSION,
84 EXPRESSION_STRUCT_CONSTRUCTION,
85 EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
86 EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
87 EXPRESSION_MAP_CONSTRUCTION,
88 EXPRESSION_COMPOSITE_LITERAL,
89 EXPRESSION_HEAP_COMPOSITE,
91 EXPRESSION_TYPE_DESCRIPTOR,
93 EXPRESSION_STRUCT_FIELD_OFFSET,
97 Expression(Expression_classification, source_location);
99 virtual ~Expression();
101 // Make an error expression. This is used when a parse error occurs
102 // to prevent cascading errors.
104 make_error(source_location);
106 // Make an expression which is really a type. This is used during
109 make_type(Type*, source_location);
111 // Make a unary expression.
113 make_unary(Operator, Expression*, source_location);
115 // Make a binary expression.
117 make_binary(Operator, Expression*, Expression*, source_location);
119 // Make a reference to a constant in an expression.
121 make_const_reference(Named_object*, source_location);
123 // Make a reference to a variable in an expression.
125 make_var_reference(Named_object*, source_location);
127 // Make a reference to a temporary variable. Temporary variables
128 // are always created by a single statement, which is what we use to
131 make_temporary_reference(Temporary_statement*, source_location);
133 // Make a sink expression--a reference to the blank identifier _.
135 make_sink(source_location);
137 // Make a reference to a function in an expression.
139 make_func_reference(Named_object*, Expression* closure, source_location);
141 // Make a reference to an unknown name. In a correct program this
142 // will always be lowered to a real const/var/func reference.
144 make_unknown_reference(Named_object*, source_location);
146 // Make a constant bool expression.
148 make_boolean(bool val, source_location);
150 // Make a constant string expression.
152 make_string(const std::string&, source_location);
154 // Make a constant integer expression. TYPE should be NULL for an
157 make_integer(const mpz_t*, Type*, source_location);
159 // Make a constant float expression. TYPE should be NULL for an
162 make_float(const mpfr_t*, Type*, source_location);
164 // Make a constant complex expression. TYPE should be NULL for an
167 make_complex(const mpfr_t* real, const mpfr_t* imag, Type*, source_location);
169 // Make a nil expression.
171 make_nil(source_location);
173 // Make an iota expression. This is used for the predeclared
178 // Make a call expression.
179 static Call_expression*
180 make_call(Expression* func, Expression_list* args, bool is_varargs,
183 // Make a reference to a specific result of a call expression which
186 make_call_result(Call_expression*, unsigned int index);
188 // Make an expression which is a method bound to its first
190 static Bound_method_expression*
191 make_bound_method(Expression* object, Expression* method, source_location);
193 // Make an index or slice expression. This is a parser expression
194 // which represents LEFT[START:END]. END may be NULL, meaning an
195 // index rather than a slice. At parse time we may not know the
196 // type of LEFT. After parsing this is lowered to an array index, a
197 // string index, or a map index.
199 make_index(Expression* left, Expression* start, Expression* end,
202 // Make an array index expression. END may be NULL, in which case
203 // this is an lvalue.
205 make_array_index(Expression* array, Expression* start, Expression* end,
208 // Make a string index expression. END may be NULL. This is never
211 make_string_index(Expression* string, Expression* start, Expression* end,
214 // Make a map index expression. This is an lvalue.
215 static Map_index_expression*
216 make_map_index(Expression* map, Expression* val, source_location);
218 // Make a selector. This is a parser expression which represents
219 // LEFT.NAME. At parse time we may not know the type of the left
222 make_selector(Expression* left, const std::string& name, source_location);
224 // Make a reference to a field in a struct.
225 static Field_reference_expression*
226 make_field_reference(Expression*, unsigned int field_index, source_location);
228 // Make a reference to a field of an interface, with an associated
231 make_interface_field_reference(Expression*, const std::string&,
234 // Make an allocation expression.
236 make_allocation(Type*, source_location);
238 // Make a call to the builtin function make.
240 make_make(Type*, Expression_list*, source_location);
242 // Make a type guard expression.
244 make_type_guard(Expression*, Type*, source_location);
246 // Make a type cast expression.
248 make_cast(Type*, Expression*, source_location);
250 // Make a composite literal. The DEPTH parameter is how far down we
251 // are in a list of composite literals with omitted types.
253 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
256 // Make a struct composite literal.
258 make_struct_composite_literal(Type*, Expression_list*, source_location);
260 // Make a slice composite literal.
262 make_slice_composite_literal(Type*, Expression_list*, source_location);
264 // Take a composite literal and allocate it on the heap.
266 make_heap_composite(Expression*, source_location);
268 // Make a receive expression. VAL is NULL for a unary receive.
269 static Receive_expression*
270 make_receive(Expression* channel, source_location);
272 // Make an expression which evaluates to the type descriptor of a
275 make_type_descriptor(Type* type, source_location);
277 // Make an expression which evaluates to some characteristic of a
278 // type. These are only used for type descriptors, so there is no
279 // location parameter.
282 // The size of a value of the type.
284 // The required alignment of a value of the type.
286 // The required alignment of a value of the type when used as a
287 // field in a struct.
288 TYPE_INFO_FIELD_ALIGNMENT
292 make_type_info(Type* type, Type_info);
294 // Make an expression which evaluates to the offset of a field in a
295 // struct. This is only used for type descriptors, so there is no
296 // location parameter.
298 make_struct_field_offset(Struct_type*, const Struct_field*);
300 // Make an expression which evaluates to the address of an unnamed
303 make_label_addr(Label*, source_location);
305 // Return the expression classification.
306 Expression_classification
307 classification() const
308 { return this->classification_; }
310 // Return the location of the expression.
313 { return this->location_; }
315 // Return whether this is a constant expression.
318 { return this->do_is_constant(); }
320 // If this is not a constant expression with integral type, return
321 // false. If it is one, return true, and set VAL to the value. VAL
322 // should already be initialized. If this returns true, it sets
323 // *PTYPE to the type of the value, or NULL for an abstract type.
324 // If IOTA_IS_CONSTANT is true, then an iota expression is assumed
325 // to have its final value.
327 integer_constant_value(bool iota_is_constant, mpz_t val, Type** ptype) const;
329 // If this is not a constant expression with floating point type,
330 // return false. If it is one, return true, and set VAL to the
331 // value. VAL should already be initialized. If this returns true,
332 // it sets *PTYPE to the type of the value, or NULL for an abstract
335 float_constant_value(mpfr_t val, Type** ptype) const;
337 // If this is not a constant expression with complex type, return
338 // false. If it is one, return true, and set REAL and IMAG to the
339 // value. REAL and IMAG should already be initialized. If this
340 // return strue, it sets *PTYPE to the type of the value, or NULL
341 // for an abstract type.
343 complex_constant_value(mpfr_t real, mpfr_t imag, Type** ptype) const;
345 // If this is not a constant expression with string type, return
346 // false. If it is one, return true, and set VAL to the value.
348 string_constant_value(std::string* val) const
349 { return this->do_string_constant_value(val); }
351 // This is called by the parser if the value of this expression is
352 // being discarded. This issues warnings about computed values
356 { this->do_discarding_value(); }
358 // Return whether this is an error expression.
360 is_error_expression() const
361 { return this->classification_ == EXPRESSION_ERROR; }
363 // Return whether this expression really represents a type.
365 is_type_expression() const
366 { return this->classification_ == EXPRESSION_TYPE; }
368 // If this is a variable reference, return the Var_expression
369 // structure. Otherwise, return NULL. This is a controlled dynamic
373 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
375 const Var_expression*
376 var_expression() const
377 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
379 // If this is a reference to a temporary variable, return the
380 // Temporary_reference_expression. Otherwise, return NULL.
381 Temporary_reference_expression*
382 temporary_reference_expression()
384 return this->convert<Temporary_reference_expression,
385 EXPRESSION_TEMPORARY_REFERENCE>();
388 // Return whether this is a sink expression.
390 is_sink_expression() const
391 { return this->classification_ == EXPRESSION_SINK; }
393 // If this is a string expression, return the String_expression
394 // structure. Otherwise, return NULL.
397 { return this->convert<String_expression, EXPRESSION_STRING>(); }
399 // Return whether this is the expression nil.
401 is_nil_expression() const
402 { return this->classification_ == EXPRESSION_NIL; }
404 // If this is an indirection through a pointer, return the
405 // expression being pointed through. Otherwise return this.
409 // If this is a binary expression, return the Binary_expression
410 // structure. Otherwise return NULL.
413 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
415 // If this is a call expression, return the Call_expression
416 // structure. Otherwise, return NULL. This is a controlled dynamic
420 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
422 // If this is an expression which refers to a function, return the
423 // Func_expression structure. Otherwise, return NULL.
426 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
428 const Func_expression*
429 func_expression() const
430 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
432 // If this is an expression which refers to an unknown name, return
433 // the Unknown_expression structure. Otherwise, return NULL.
436 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
438 const Unknown_expression*
439 unknown_expression() const
441 return this->convert<const Unknown_expression,
442 EXPRESSION_UNKNOWN_REFERENCE>();
445 // If this is an index expression, return the Index_expression
446 // structure. Otherwise, return NULL.
449 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
451 // If this is an expression which refers to indexing in a map,
452 // return the Map_index_expression structure. Otherwise, return
454 Map_index_expression*
455 map_index_expression()
456 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
458 // If this is a bound method expression, return the
459 // Bound_method_expression structure. Otherwise, return NULL.
460 Bound_method_expression*
461 bound_method_expression()
462 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
464 // If this is a reference to a field in a struct, return the
465 // Field_reference_expression structure. Otherwise, return NULL.
466 Field_reference_expression*
467 field_reference_expression()
469 return this->convert<Field_reference_expression,
470 EXPRESSION_FIELD_REFERENCE>();
473 // If this is a reference to a field in an interface, return the
474 // Interface_field_reference_expression structure. Otherwise,
476 Interface_field_reference_expression*
477 interface_field_reference_expression()
479 return this->convert<Interface_field_reference_expression,
480 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
483 // If this is a type guard expression, return the
484 // Type_guard_expression structure. Otherwise, return NULL.
485 Type_guard_expression*
486 type_guard_expression()
487 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
489 // If this is a receive expression, return the Receive_expression
490 // structure. Otherwise, return NULL.
493 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
495 // Return true if this is a composite literal.
497 is_composite_literal() const;
499 // Return true if this is a composite literal which is not constant.
501 is_nonconstant_composite_literal() const;
503 // Return true if this is a reference to a local variable.
505 is_local_variable() const;
507 // Traverse an expression.
509 traverse(Expression**, Traverse*);
511 // Traverse subexpressions of this expression.
513 traverse_subexpressions(Traverse*);
515 // Lower an expression. This is called immediately after parsing.
516 // IOTA_VALUE is the value that we should give to any iota
517 // expressions. This function must resolve expressions which could
518 // not be fully parsed into their final form. It returns the same
519 // Expression or a new one.
521 lower(Gogo* gogo, Named_object* function, int iota_value)
522 { return this->do_lower(gogo, function, iota_value); }
524 // Determine the real type of an expression with abstract integer,
525 // floating point, or complex type. TYPE_CONTEXT describes the
528 determine_type(const Type_context*);
530 // Check types in an expression.
532 check_types(Gogo* gogo)
533 { this->do_check_types(gogo); }
535 // Determine the type when there is no context.
537 determine_type_no_context();
539 // Return the current type of the expression. This may be changed
540 // by determine_type.
543 { return this->do_type(); }
545 // Return a copy of an expression.
548 { return this->do_copy(); }
550 // Return whether the expression is addressable--something which may
551 // be used as the operand of the unary & operator.
553 is_addressable() const
554 { return this->do_is_addressable(); }
556 // Note that we are taking the address of this expression. ESCAPES
557 // is true if this address escapes the current function.
559 address_taken(bool escapes)
560 { this->do_address_taken(escapes); }
562 // Return whether this expression must be evaluated in order
563 // according to the order of evaluation rules. This is basically
564 // true of all expressions with side-effects.
566 must_eval_in_order() const
567 { return this->do_must_eval_in_order(); }
569 // Return the tree for this expression.
571 get_tree(Translate_context*);
573 // Return a tree handling any conversions which must be done during
576 convert_for_assignment(Translate_context*, Type* lhs_type, Type* rhs_type,
577 tree rhs_tree, source_location location);
579 // Return a tree converting a value of one interface type to another
580 // interface type. If FOR_TYPE_GUARD is true this is for a type
583 convert_interface_to_interface(Translate_context*, Type* lhs_type,
584 Type* rhs_type, tree rhs_tree,
585 bool for_type_guard, source_location);
587 // Return a tree implementing the comparison LHS_TREE OP RHS_TREE.
588 // TYPE is the type of both sides.
590 comparison_tree(Translate_context*, Operator op, Type* left_type,
591 tree left_tree, Type* right_type, tree right_tree,
594 // Return a tree for the multi-precision integer VAL in TYPE.
596 integer_constant_tree(mpz_t val, tree type);
598 // Return a tree for the floating point value VAL in TYPE.
600 float_constant_tree(mpfr_t val, tree type);
602 // Return a tree for the complex value REAL/IMAG in TYPE.
604 complex_constant_tree(mpfr_t real, mpfr_t imag, tree type);
606 // Export the expression. This is only used for constants. It will
607 // be used for things like values of named constants and sizes of
610 export_expression(Export* exp) const
611 { this->do_export(exp); }
613 // Import an expression.
615 import_expression(Import*);
617 // Return a tree which checks that VAL, of arbitrary integer type,
618 // is non-negative and is not more than the maximum value of
619 // BOUND_TYPE. If SOFAR is not NULL, it is or'red into the result.
620 // The return value may be NULL if SOFAR is NULL.
622 check_bounds(tree val, tree bound_type, tree sofar, source_location);
625 // May be implemented by child class: traverse the expressions.
627 do_traverse(Traverse*);
629 // Return a lowered expression.
631 do_lower(Gogo*, Named_object*, int)
634 // Return whether this is a constant expression.
636 do_is_constant() const
639 // Return whether this is a constant expression of integral type,
640 // and set VAL to the value.
642 do_integer_constant_value(bool, mpz_t, Type**) const
645 // Return whether this is a constant expression of floating point
646 // type, and set VAL to the value.
648 do_float_constant_value(mpfr_t, Type**) const
651 // Return whether this is a constant expression of complex type, and
652 // set REAL and IMAGE to the value.
654 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const
657 // Return whether this is a constant expression of string type, and
658 // set VAL to the value.
660 do_string_constant_value(std::string*) const
663 // Called by the parser if the value is being discarded.
665 do_discarding_value();
667 // Child class holds type.
671 // Child class implements determining type information.
673 do_determine_type(const Type_context*) = 0;
675 // Child class implements type checking if needed.
677 do_check_types(Gogo*)
680 // Child class implements copying.
684 // Child class implements whether the expression is addressable.
686 do_is_addressable() const
689 // Child class implements taking the address of an expression.
691 do_address_taken(bool)
694 // Child class implements whether this expression must be evaluated
697 do_must_eval_in_order() const
700 // Child class implements conversion to tree.
702 do_get_tree(Translate_context*) = 0;
704 // Child class implements export.
706 do_export(Export*) const;
708 // For children to call to warn about an unused value.
710 warn_about_unused_value();
712 // For children to call when they detect that they are in error.
716 // For children to call to report an error conveniently.
718 report_error(const char*);
721 // Convert to the desired statement classification, or return NULL.
722 // This is a controlled dynamic cast.
723 template<typename Expression_class,
724 Expression_classification expr_classification>
728 return (this->classification_ == expr_classification
729 ? static_cast<Expression_class*>(this)
733 template<typename Expression_class,
734 Expression_classification expr_classification>
735 const Expression_class*
738 return (this->classification_ == expr_classification
739 ? static_cast<const Expression_class*>(this)
744 convert_type_to_interface(Translate_context*, Type*, Type*, tree,
748 get_interface_type_descriptor(Translate_context*, Type*, tree,
752 convert_interface_to_type(Translate_context*, Type*, Type*, tree,
755 // The expression classification.
756 Expression_classification classification_;
757 // The location in the input file.
758 source_location location_;
761 // A list of Expressions.
763 class Expression_list
770 // Return whether the list is empty.
773 { return this->entries_.empty(); }
775 // Return the number of entries in the list.
778 { return this->entries_.size(); }
780 // Add an entry to the end of the list.
782 push_back(Expression* expr)
783 { this->entries_.push_back(expr); }
786 append(Expression_list* add)
787 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
789 // Reserve space in the list.
792 { this->entries_.reserve(size); }
794 // Traverse the expressions in the list.
802 // Return true if the list contains an error expression.
804 contains_error() const;
806 // Return the first and last elements.
809 { return this->entries_.front(); }
813 { return this->entries_.front(); }
817 { return this->entries_.back(); }
821 { return this->entries_.back(); }
825 typedef std::vector<Expression*>::iterator iterator;
826 typedef std::vector<Expression*>::const_iterator const_iterator;
830 { return this->entries_.begin(); }
834 { return this->entries_.begin(); }
838 { return this->entries_.end(); }
842 { return this->entries_.end(); }
847 { this->entries_.erase(p); }
850 std::vector<Expression*> entries_;
853 // An abstract base class for an expression which is only used by the
854 // parser, and is lowered in the lowering pass.
856 class Parser_expression : public Expression
859 Parser_expression(Expression_classification classification,
860 source_location location)
861 : Expression(classification, location)
866 do_lower(Gogo*, Named_object*, int) = 0;
872 do_determine_type(const Type_context*)
873 { gcc_unreachable(); }
876 do_check_types(Gogo*)
877 { gcc_unreachable(); }
880 do_get_tree(Translate_context*)
881 { gcc_unreachable(); }
884 // An expression which is simply a variable.
886 class Var_expression : public Expression
889 Var_expression(Named_object* variable, source_location location)
890 : Expression(EXPRESSION_VAR_REFERENCE, location),
894 // Return the variable.
897 { return this->variable_; }
901 do_lower(Gogo*, Named_object*, int);
907 do_determine_type(const Type_context*);
914 do_is_addressable() const
918 do_address_taken(bool);
921 do_get_tree(Translate_context*);
924 // The variable we are referencing.
925 Named_object* variable_;
928 // A reference to a temporary variable.
930 class Temporary_reference_expression : public Expression
933 Temporary_reference_expression(Temporary_statement* statement,
934 source_location location)
935 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
936 statement_(statement)
944 do_determine_type(const Type_context*)
949 { return make_temporary_reference(this->statement_, this->location()); }
952 do_is_addressable() const
956 do_address_taken(bool);
959 do_get_tree(Translate_context*);
962 // The statement where the temporary variable is defined.
963 Temporary_statement* statement_;
966 // A string expression.
968 class String_expression : public Expression
971 String_expression(const std::string& val, source_location location)
972 : Expression(EXPRESSION_STRING, location),
973 val_(val), type_(NULL)
978 { return this->val_; }
985 do_is_constant() const
989 do_string_constant_value(std::string* val) const
999 do_determine_type(const Type_context*);
1006 do_get_tree(Translate_context*);
1009 do_export(Export*) const;
1012 // The string value. This is immutable.
1013 const std::string val_;
1014 // The type as determined by context.
1018 // A binary expression.
1020 class Binary_expression : public Expression
1023 Binary_expression(Operator op, Expression* left, Expression* right,
1024 source_location location)
1025 : Expression(EXPRESSION_BINARY, location),
1026 op_(op), left_(left), right_(right)
1029 // Return the operator.
1032 { return this->op_; }
1034 // Return the left hand expression.
1037 { return this->left_; }
1039 // Return the right hand expression.
1042 { return this->right_; }
1044 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1045 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
1046 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true
1047 // if this could be done, false if not.
1049 eval_integer(Operator op, Type* left_type, mpz_t left_val,
1050 Type* right_type, mpz_t right_val, source_location,
1053 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1054 // Return true if this could be done, false if not.
1056 eval_float(Operator op, Type* left_type, mpfr_t left_val,
1057 Type* right_type, mpfr_t right_val, mpfr_t val,
1060 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
1061 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
1062 // could be done, false if not.
1064 eval_complex(Operator op, Type* left_type, mpfr_t left_real,
1065 mpfr_t left_imag, Type* right_type, mpfr_t right_real,
1066 mpfr_t right_imag, mpfr_t real, mpfr_t imag, source_location);
1068 // Compare integer constants according to OP.
1070 compare_integer(Operator op, mpz_t left_val, mpz_t right_val);
1072 // Compare floating point constants according to OP.
1074 compare_float(Operator op, Type* type, mpfr_t left_val, mpfr_t right_val);
1076 // Compare complex constants according to OP.
1078 compare_complex(Operator op, Type* type, mpfr_t left_real, mpfr_t left_imag,
1079 mpfr_t right_val, mpfr_t right_imag);
1084 // Report an error if OP can not be applied to TYPE. Return whether
1087 check_operator_type(Operator op, Type* type, source_location);
1091 do_traverse(Traverse* traverse);
1094 do_lower(Gogo*, Named_object*, int);
1097 do_is_constant() const
1098 { return this->left_->is_constant() && this->right_->is_constant(); }
1101 do_integer_constant_value(bool, mpz_t val, Type**) const;
1104 do_float_constant_value(mpfr_t val, Type**) const;
1107 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
1110 do_discarding_value();
1116 do_determine_type(const Type_context*);
1119 do_check_types(Gogo*);
1124 return Expression::make_binary(this->op_, this->left_->copy(),
1125 this->right_->copy(), this->location());
1129 do_get_tree(Translate_context*);
1132 do_export(Export*) const;
1135 // The binary operator to apply.
1137 // The left hand side operand.
1139 // The right hand side operand.
1143 // A call expression. The go statement needs to dig inside this.
1145 class Call_expression : public Expression
1148 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
1149 source_location location)
1150 : Expression(EXPRESSION_CALL, location),
1151 fn_(fn), args_(args), type_(NULL), tree_(NULL), is_varargs_(is_varargs),
1152 varargs_are_lowered_(false), types_are_determined_(false),
1156 // The function to call.
1159 { return this->fn_; }
1164 { return this->args_; }
1166 const Expression_list*
1168 { return this->args_; }
1170 // Get the function type.
1172 get_function_type() const;
1174 // Return the number of values this call will return.
1176 result_count() const;
1178 // Return whether this is a call to the predeclared function
1181 is_recover_call() const;
1183 // Set the argument for a call to recover.
1185 set_recover_arg(Expression*);
1187 // Whether the last argument is a varargs argument (f(a...)).
1190 { return this->is_varargs_; }
1192 // Whether this call is being deferred.
1195 { return this->is_deferred_; }
1197 // Note that the call is being deferred.
1200 { this->is_deferred_ = true; }
1204 do_traverse(Traverse*);
1207 do_lower(Gogo*, Named_object*, int);
1210 do_discarding_value()
1217 do_determine_type(const Type_context*);
1220 do_check_types(Gogo*);
1225 return Expression::make_call(this->fn_->copy(),
1226 (this->args_ == NULL
1228 : this->args_->copy()),
1229 this->is_varargs_, this->location());
1233 do_must_eval_in_order() const;
1236 do_get_tree(Translate_context*);
1239 do_is_recover_call() const;
1242 do_set_recover_arg(Expression*);
1244 // Let a builtin expression change the argument list.
1246 set_args(Expression_list* args)
1247 { this->args_ = args; }
1249 // Let a builtin expression lower varargs.
1251 lower_varargs(Gogo*, Named_object* function, Type* varargs_type,
1252 size_t param_count);
1254 // Let a builtin expression check whether types have been
1257 determining_types();
1261 check_argument_type(int, const Type*, const Type*, source_location, bool);
1264 bound_method_function(Translate_context*, Bound_method_expression*, tree*);
1267 interface_method_function(Translate_context*,
1268 Interface_field_reference_expression*,
1271 // The function to call.
1273 // The arguments to pass. This may be NULL if there are no
1275 Expression_list* args_;
1276 // The type of the expression, to avoid recomputing it.
1278 // The tree for the call, used for a call which returns a tuple.
1280 // True if the last argument is a varargs argument (f(a...)).
1282 // True if varargs have already been lowered.
1283 bool varargs_are_lowered_;
1284 // True if types have been determined.
1285 bool types_are_determined_;
1286 // True if the call is an argument to a defer statement.
1290 // An expression which represents a pointer to a function.
1292 class Func_expression : public Expression
1295 Func_expression(Named_object* function, Expression* closure,
1296 source_location location)
1297 : Expression(EXPRESSION_FUNC_REFERENCE, location),
1298 function_(function), closure_(closure)
1301 // Return the object associated with the function.
1303 named_object() const
1304 { return this->function_; }
1306 // Return the closure for this function. This will return NULL if
1307 // the function has no closure, which is the normal case.
1310 { return this->closure_; }
1312 // Return a tree for this function without evaluating the closure.
1314 get_tree_without_closure(Gogo*);
1318 do_traverse(Traverse*);
1324 do_determine_type(const Type_context*)
1326 if (this->closure_ != NULL)
1327 this->closure_->determine_type_no_context();
1333 return Expression::make_func_reference(this->function_,
1334 (this->closure_ == NULL
1336 : this->closure_->copy()),
1341 do_get_tree(Translate_context*);
1344 // The function itself.
1345 Named_object* function_;
1346 // A closure. This is normally NULL. For a nested function, it may
1347 // be a heap-allocated struct holding pointers to all the variables
1348 // referenced by this function and defined in enclosing functions.
1349 Expression* closure_;
1352 // A reference to an unknown name.
1354 class Unknown_expression : public Parser_expression
1357 Unknown_expression(Named_object* named_object, source_location location)
1358 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
1359 named_object_(named_object), is_composite_literal_key_(false)
1362 // The associated named object.
1364 named_object() const
1365 { return this->named_object_; }
1367 // The name of the identifier which was unknown.
1371 // Note that this expression is being used as the key in a composite
1372 // literal, so it may be OK if it is not resolved.
1374 set_is_composite_literal_key()
1375 { this->is_composite_literal_key_ = true; }
1377 // Note that this expression should no longer be treated as a
1378 // composite literal key.
1380 clear_is_composite_literal_key()
1381 { this->is_composite_literal_key_ = false; }
1385 do_lower(Gogo*, Named_object*, int);
1389 { return new Unknown_expression(this->named_object_, this->location()); }
1392 // The unknown name.
1393 Named_object* named_object_;
1394 // True if this is the key in a composite literal.
1395 bool is_composite_literal_key_;
1398 // An index expression. This is lowered to an array index, a string
1399 // index, or a map index.
1401 class Index_expression : public Parser_expression
1404 Index_expression(Expression* left, Expression* start, Expression* end,
1405 source_location location)
1406 : Parser_expression(EXPRESSION_INDEX, location),
1407 left_(left), start_(start), end_(end), is_lvalue_(false)
1410 // Record that this expression is an lvalue.
1413 { this->is_lvalue_ = true; }
1417 do_traverse(Traverse*);
1420 do_lower(Gogo*, Named_object*, int);
1425 return new Index_expression(this->left_->copy(), this->start_->copy(),
1428 : this->end_->copy()),
1433 // The expression being indexed.
1437 // The second index. This is NULL for an index, non-NULL for a
1440 // Whether this is being used as an l-value. We set this during the
1441 // parse because map index expressions need to know.
1445 // An index into a map.
1447 class Map_index_expression : public Expression
1450 Map_index_expression(Expression* map, Expression* index,
1451 source_location location)
1452 : Expression(EXPRESSION_MAP_INDEX, location),
1453 map_(map), index_(index), is_lvalue_(false),
1454 is_in_tuple_assignment_(false)
1460 { return this->map_; }
1464 { return this->map_; }
1466 // Return the index.
1469 { return this->index_; }
1473 { return this->index_; }
1475 // Get the type of the map being indexed.
1477 get_map_type() const;
1479 // Record that this map expression is an lvalue. The difference is
1480 // that an lvalue always inserts the key.
1483 { this->is_lvalue_ = true; }
1485 // Return whether this map expression occurs in an assignment to a
1488 is_in_tuple_assignment() const
1489 { return this->is_in_tuple_assignment_; }
1491 // Record that this map expression occurs in an assignment to a pair
1494 set_is_in_tuple_assignment()
1495 { this->is_in_tuple_assignment_ = true; }
1497 // Return a tree for the map index. This returns a tree which
1498 // evaluates to a pointer to a value in the map. If INSERT is true,
1499 // the key will be inserted if not present, and the value pointer
1500 // will be zero initialized. If INSERT is false, and the key is not
1501 // present in the map, the pointer will be NULL.
1503 get_value_pointer(Translate_context*, bool insert);
1507 do_traverse(Traverse*);
1513 do_determine_type(const Type_context*);
1516 do_check_types(Gogo*);
1521 return Expression::make_map_index(this->map_->copy(),
1522 this->index_->copy(),
1526 // A map index expression is an lvalue but it is not addressable.
1529 do_get_tree(Translate_context*);
1532 // The map we are looking into.
1536 // Whether this is an lvalue.
1538 // Whether this is in a tuple assignment to a pair of values.
1539 bool is_in_tuple_assignment_;
1542 // An expression which represents a method bound to its first
1545 class Bound_method_expression : public Expression
1548 Bound_method_expression(Expression* expr, Expression* method,
1549 source_location location)
1550 : Expression(EXPRESSION_BOUND_METHOD, location),
1551 expr_(expr), expr_type_(NULL), method_(method)
1554 // Return the object which is the first argument.
1557 { return this->expr_; }
1559 // Return the implicit type of the first argument. This will be
1560 // non-NULL when using a method from an anonymous field without
1561 // using an explicit stub.
1563 first_argument_type() const
1564 { return this->expr_type_; }
1566 // Return the reference to the method function.
1569 { return this->method_; }
1571 // Set the implicit type of the expression.
1573 set_first_argument_type(Type* type)
1574 { this->expr_type_ = type; }
1578 do_traverse(Traverse*);
1584 do_determine_type(const Type_context*);
1587 do_check_types(Gogo*);
1592 return new Bound_method_expression(this->expr_->copy(),
1593 this->method_->copy(),
1598 do_get_tree(Translate_context*);
1601 // The object used to find the method. This is passed to the method
1602 // as the first argument.
1604 // The implicit type of the object to pass to the method. This is
1605 // NULL in the normal case, non-NULL when using a method from an
1606 // anonymous field which does not require a stub.
1608 // The method itself. This is a Func_expression.
1609 Expression* method_;
1612 // A reference to a field in a struct.
1614 class Field_reference_expression : public Expression
1617 Field_reference_expression(Expression* expr, unsigned int field_index,
1618 source_location location)
1619 : Expression(EXPRESSION_FIELD_REFERENCE, location),
1620 expr_(expr), field_index_(field_index)
1623 // Return the struct expression.
1626 { return this->expr_; }
1628 // Return the field index.
1631 { return this->field_index_; }
1633 // Set the struct expression. This is used when parsing.
1635 set_struct_expression(Expression* expr)
1637 gcc_assert(this->expr_ == NULL);
1643 do_traverse(Traverse* traverse)
1644 { return Expression::traverse(&this->expr_, traverse); }
1650 do_determine_type(const Type_context*)
1651 { this->expr_->determine_type_no_context(); }
1654 do_check_types(Gogo*);
1659 return Expression::make_field_reference(this->expr_->copy(),
1665 do_is_addressable() const
1666 { return this->expr_->is_addressable(); }
1669 do_get_tree(Translate_context*);
1672 // The expression we are looking into. This should have a type of
1675 // The zero-based index of the field we are retrieving.
1676 unsigned int field_index_;
1679 // A reference to a field of an interface.
1681 class Interface_field_reference_expression : public Expression
1684 Interface_field_reference_expression(Expression* expr,
1685 const std::string& name,
1686 source_location location)
1687 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
1688 expr_(expr), name_(name)
1691 // Return the expression for the interface object.
1694 { return this->expr_; }
1696 // Return the name of the method to call.
1699 { return this->name_; }
1701 // Return a tree for the pointer to the function to call, given a
1702 // tree for the expression.
1704 get_function_tree(Translate_context*, tree);
1706 // Return a tree for the first argument to pass to the interface
1707 // function, given a tree for the expression. This is the real
1708 // object associated with the interface object.
1710 get_underlying_object_tree(Translate_context*, tree);
1714 do_traverse(Traverse* traverse);
1720 do_determine_type(const Type_context*);
1723 do_check_types(Gogo*);
1728 return Expression::make_interface_field_reference(this->expr_->copy(),
1734 do_get_tree(Translate_context*);
1737 // The expression for the interface object. This should have a type
1738 // of interface or pointer to interface.
1740 // The field we are retrieving--the name of the method.
1744 // A type guard expression.
1746 class Type_guard_expression : public Expression
1749 Type_guard_expression(Expression* expr, Type* type, source_location location)
1750 : Expression(EXPRESSION_TYPE_GUARD, location),
1751 expr_(expr), type_(type)
1754 // Return the expression to convert.
1757 { return this->expr_; }
1759 // Return the type to which to convert.
1762 { return this->type_; }
1766 do_traverse(Traverse* traverse);
1770 { return this->type_; }
1773 do_determine_type(const Type_context*)
1774 { this->expr_->determine_type_no_context(); }
1777 do_check_types(Gogo*);
1782 return new Type_guard_expression(this->expr_->copy(), this->type_,
1787 do_get_tree(Translate_context*);
1790 // The expression to convert.
1792 // The type to which to convert.
1796 // A receive expression.
1798 class Receive_expression : public Expression
1801 Receive_expression(Expression* channel, source_location location)
1802 : Expression(EXPRESSION_RECEIVE, location),
1803 channel_(channel), for_select_(false)
1806 // Return the channel.
1809 { return this->channel_; }
1811 // Note that this is for a select statement.
1814 { this->for_select_ = true; }
1818 do_traverse(Traverse* traverse)
1819 { return Expression::traverse(&this->channel_, traverse); }
1822 do_discarding_value()
1829 do_determine_type(const Type_context*)
1830 { this->channel_->determine_type_no_context(); }
1833 do_check_types(Gogo*);
1838 return Expression::make_receive(this->channel_->copy(), this->location());
1842 do_must_eval_in_order() const
1846 do_get_tree(Translate_context*);
1849 // The channel from which we are receiving.
1850 Expression* channel_;
1851 // Whether this is for a select statement.
1855 #endif // !defined(GO_EXPRESSIONS_H)