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_UNSAFE_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,
92 EXPRESSION_TYPE_DESCRIPTOR,
94 EXPRESSION_STRUCT_FIELD_OFFSET,
98 Expression(Expression_classification, source_location);
100 virtual ~Expression();
102 // Make an error expression. This is used when a parse error occurs
103 // to prevent cascading errors.
105 make_error(source_location);
107 // Make an expression which is really a type. This is used during
110 make_type(Type*, source_location);
112 // Make a unary expression.
114 make_unary(Operator, Expression*, source_location);
116 // Make a binary expression.
118 make_binary(Operator, Expression*, Expression*, source_location);
120 // Make a reference to a constant in an expression.
122 make_const_reference(Named_object*, source_location);
124 // Make a reference to a variable in an expression.
126 make_var_reference(Named_object*, source_location);
128 // Make a reference to a temporary variable. Temporary variables
129 // are always created by a single statement, which is what we use to
132 make_temporary_reference(Temporary_statement*, source_location);
134 // Make a sink expression--a reference to the blank identifier _.
136 make_sink(source_location);
138 // Make a reference to a function in an expression.
140 make_func_reference(Named_object*, Expression* closure, source_location);
142 // Make a reference to an unknown name. In a correct program this
143 // will always be lowered to a real const/var/func reference.
145 make_unknown_reference(Named_object*, source_location);
147 // Make a constant bool expression.
149 make_boolean(bool val, source_location);
151 // Make a constant string expression.
153 make_string(const std::string&, source_location);
155 // Make a constant integer expression. TYPE should be NULL for an
158 make_integer(const mpz_t*, Type*, source_location);
160 // Make a constant float expression. TYPE should be NULL for an
163 make_float(const mpfr_t*, Type*, source_location);
165 // Make a constant complex expression. TYPE should be NULL for an
168 make_complex(const mpfr_t* real, const mpfr_t* imag, Type*, source_location);
170 // Make a nil expression.
172 make_nil(source_location);
174 // Make an iota expression. This is used for the predeclared
179 // Make a call expression.
180 static Call_expression*
181 make_call(Expression* func, Expression_list* args, bool is_varargs,
184 // Make a reference to a specific result of a call expression which
187 make_call_result(Call_expression*, unsigned int index);
189 // Make an expression which is a method bound to its first
191 static Bound_method_expression*
192 make_bound_method(Expression* object, Expression* method, source_location);
194 // Make an index or slice expression. This is a parser expression
195 // which represents LEFT[START:END]. END may be NULL, meaning an
196 // index rather than a slice. At parse time we may not know the
197 // type of LEFT. After parsing this is lowered to an array index, a
198 // string index, or a map index.
200 make_index(Expression* left, Expression* start, Expression* end,
203 // Make an array index expression. END may be NULL, in which case
204 // this is an lvalue.
206 make_array_index(Expression* array, Expression* start, Expression* end,
209 // Make a string index expression. END may be NULL. This is never
212 make_string_index(Expression* string, Expression* start, Expression* end,
215 // Make a map index expression. This is an lvalue.
216 static Map_index_expression*
217 make_map_index(Expression* map, Expression* val, source_location);
219 // Make a selector. This is a parser expression which represents
220 // LEFT.NAME. At parse time we may not know the type of the left
223 make_selector(Expression* left, const std::string& name, source_location);
225 // Make a reference to a field in a struct.
226 static Field_reference_expression*
227 make_field_reference(Expression*, unsigned int field_index, source_location);
229 // Make a reference to a field of an interface, with an associated
232 make_interface_field_reference(Expression*, const std::string&,
235 // Make an allocation expression.
237 make_allocation(Type*, source_location);
239 // Make a call to the builtin function make.
241 make_make(Type*, Expression_list*, source_location);
243 // Make a type guard expression.
245 make_type_guard(Expression*, Type*, source_location);
247 // Make a type cast expression.
249 make_cast(Type*, Expression*, source_location);
251 // Make an unsafe type cast expression. This is only used when
252 // passing parameter to builtin functions that are part of the Go
255 make_unsafe_cast(Type*, Expression*, source_location);
257 // Make a composite literal. The DEPTH parameter is how far down we
258 // are in a list of composite literals with omitted types.
260 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
263 // Make a struct composite literal.
265 make_struct_composite_literal(Type*, Expression_list*, source_location);
267 // Make a slice composite literal.
269 make_slice_composite_literal(Type*, Expression_list*, source_location);
271 // Take a composite literal and allocate it on the heap.
273 make_heap_composite(Expression*, source_location);
275 // Make a receive expression. VAL is NULL for a unary receive.
276 static Receive_expression*
277 make_receive(Expression* channel, source_location);
279 // Make an expression which evaluates to the type descriptor of a
282 make_type_descriptor(Type* type, source_location);
284 // Make an expression which evaluates to some characteristic of a
285 // type. These are only used for type descriptors, so there is no
286 // location parameter.
289 // The size of a value of the type.
291 // The required alignment of a value of the type.
293 // The required alignment of a value of the type when used as a
294 // field in a struct.
295 TYPE_INFO_FIELD_ALIGNMENT
299 make_type_info(Type* type, Type_info);
301 // Make an expression which evaluates to the offset of a field in a
302 // struct. This is only used for type descriptors, so there is no
303 // location parameter.
305 make_struct_field_offset(Struct_type*, const Struct_field*);
307 // Make an expression which evaluates to the address of an unnamed
310 make_label_addr(Label*, source_location);
312 // Return the expression classification.
313 Expression_classification
314 classification() const
315 { return this->classification_; }
317 // Return the location of the expression.
320 { return this->location_; }
322 // Return whether this is a constant expression.
325 { return this->do_is_constant(); }
327 // If this is not a constant expression with integral type, return
328 // false. If it is one, return true, and set VAL to the value. VAL
329 // should already be initialized. If this returns true, it sets
330 // *PTYPE to the type of the value, or NULL for an abstract type.
331 // If IOTA_IS_CONSTANT is true, then an iota expression is assumed
332 // to have its final value.
334 integer_constant_value(bool iota_is_constant, mpz_t val, Type** ptype) const;
336 // If this is not a constant expression with floating point type,
337 // return false. If it is one, return true, and set VAL to the
338 // value. VAL should already be initialized. If this returns true,
339 // it sets *PTYPE to the type of the value, or NULL for an abstract
342 float_constant_value(mpfr_t val, Type** ptype) const;
344 // If this is not a constant expression with complex type, return
345 // false. If it is one, return true, and set REAL and IMAG to the
346 // value. REAL and IMAG should already be initialized. If this
347 // return strue, it sets *PTYPE to the type of the value, or NULL
348 // for an abstract type.
350 complex_constant_value(mpfr_t real, mpfr_t imag, Type** ptype) const;
352 // If this is not a constant expression with string type, return
353 // false. If it is one, return true, and set VAL to the value.
355 string_constant_value(std::string* val) const
356 { return this->do_string_constant_value(val); }
358 // This is called by the parser if the value of this expression is
359 // being discarded. This issues warnings about computed values
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_; }
908 do_lower(Gogo*, Named_object*, int);
914 do_determine_type(const Type_context*);
921 do_is_addressable() const
925 do_address_taken(bool);
928 do_get_tree(Translate_context*);
931 // The variable we are referencing.
932 Named_object* variable_;
935 // A reference to a temporary variable.
937 class Temporary_reference_expression : public Expression
940 Temporary_reference_expression(Temporary_statement* statement,
941 source_location location)
942 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
943 statement_(statement)
951 do_determine_type(const Type_context*)
956 { return make_temporary_reference(this->statement_, this->location()); }
959 do_is_addressable() const
963 do_address_taken(bool);
966 do_get_tree(Translate_context*);
969 // The statement where the temporary variable is defined.
970 Temporary_statement* statement_;
973 // A string expression.
975 class String_expression : public Expression
978 String_expression(const std::string& val, source_location location)
979 : Expression(EXPRESSION_STRING, location),
980 val_(val), type_(NULL)
985 { return this->val_; }
992 do_is_constant() const
996 do_string_constant_value(std::string* val) const
1006 do_determine_type(const Type_context*);
1013 do_get_tree(Translate_context*);
1016 do_export(Export*) const;
1019 // The string value. This is immutable.
1020 const std::string val_;
1021 // The type as determined by context.
1025 // A binary expression.
1027 class Binary_expression : public Expression
1030 Binary_expression(Operator op, Expression* left, Expression* right,
1031 source_location location)
1032 : Expression(EXPRESSION_BINARY, location),
1033 op_(op), left_(left), right_(right)
1036 // Return the operator.
1039 { return this->op_; }
1041 // Return the left hand expression.
1044 { return this->left_; }
1046 // Return the right hand expression.
1049 { return this->right_; }
1051 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1052 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
1053 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true
1054 // if this could be done, false if not.
1056 eval_integer(Operator op, Type* left_type, mpz_t left_val,
1057 Type* right_type, mpz_t right_val, source_location,
1060 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1061 // Return true if this could be done, false if not.
1063 eval_float(Operator op, Type* left_type, mpfr_t left_val,
1064 Type* right_type, mpfr_t right_val, mpfr_t val,
1067 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
1068 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
1069 // could be done, false if not.
1071 eval_complex(Operator op, Type* left_type, mpfr_t left_real,
1072 mpfr_t left_imag, Type* right_type, mpfr_t right_real,
1073 mpfr_t right_imag, mpfr_t real, mpfr_t imag, source_location);
1075 // Compare integer constants according to OP.
1077 compare_integer(Operator op, mpz_t left_val, mpz_t right_val);
1079 // Compare floating point constants according to OP.
1081 compare_float(Operator op, Type* type, mpfr_t left_val, mpfr_t right_val);
1083 // Compare complex constants according to OP.
1085 compare_complex(Operator op, Type* type, mpfr_t left_real, mpfr_t left_imag,
1086 mpfr_t right_val, mpfr_t right_imag);
1091 // Report an error if OP can not be applied to TYPE. Return whether
1094 check_operator_type(Operator op, Type* type, source_location);
1098 do_traverse(Traverse* traverse);
1101 do_lower(Gogo*, Named_object*, int);
1104 do_is_constant() const
1105 { return this->left_->is_constant() && this->right_->is_constant(); }
1108 do_integer_constant_value(bool, mpz_t val, Type**) const;
1111 do_float_constant_value(mpfr_t val, Type**) const;
1114 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
1117 do_discarding_value();
1123 do_determine_type(const Type_context*);
1126 do_check_types(Gogo*);
1131 return Expression::make_binary(this->op_, this->left_->copy(),
1132 this->right_->copy(), this->location());
1136 do_get_tree(Translate_context*);
1139 do_export(Export*) const;
1142 // The binary operator to apply.
1144 // The left hand side operand.
1146 // The right hand side operand.
1150 // A call expression. The go statement needs to dig inside this.
1152 class Call_expression : public Expression
1155 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
1156 source_location location)
1157 : Expression(EXPRESSION_CALL, location),
1158 fn_(fn), args_(args), type_(NULL), tree_(NULL), is_varargs_(is_varargs),
1159 varargs_are_lowered_(false), types_are_determined_(false),
1163 // The function to call.
1166 { return this->fn_; }
1171 { return this->args_; }
1173 const Expression_list*
1175 { return this->args_; }
1177 // Get the function type.
1179 get_function_type() const;
1181 // Return the number of values this call will return.
1183 result_count() const;
1185 // Return whether this is a call to the predeclared function
1188 is_recover_call() const;
1190 // Set the argument for a call to recover.
1192 set_recover_arg(Expression*);
1194 // Whether the last argument is a varargs argument (f(a...)).
1197 { return this->is_varargs_; }
1199 // Whether this call is being deferred.
1202 { return this->is_deferred_; }
1204 // Note that the call is being deferred.
1207 { this->is_deferred_ = true; }
1211 do_traverse(Traverse*);
1214 do_lower(Gogo*, Named_object*, int);
1217 do_discarding_value()
1224 do_determine_type(const Type_context*);
1227 do_check_types(Gogo*);
1232 return Expression::make_call(this->fn_->copy(),
1233 (this->args_ == NULL
1235 : this->args_->copy()),
1236 this->is_varargs_, this->location());
1240 do_must_eval_in_order() const;
1243 do_get_tree(Translate_context*);
1246 do_is_recover_call() const;
1249 do_set_recover_arg(Expression*);
1251 // Let a builtin expression change the argument list.
1253 set_args(Expression_list* args)
1254 { this->args_ = args; }
1256 // Let a builtin expression lower varargs.
1258 lower_varargs(Gogo*, Named_object* function, Type* varargs_type,
1259 size_t param_count);
1261 // Let a builtin expression check whether types have been
1264 determining_types();
1268 check_argument_type(int, const Type*, const Type*, source_location, bool);
1271 bound_method_function(Translate_context*, Bound_method_expression*, tree*);
1274 interface_method_function(Translate_context*,
1275 Interface_field_reference_expression*,
1278 // The function to call.
1280 // The arguments to pass. This may be NULL if there are no
1282 Expression_list* args_;
1283 // The type of the expression, to avoid recomputing it.
1285 // The tree for the call, used for a call which returns a tuple.
1287 // True if the last argument is a varargs argument (f(a...)).
1289 // True if varargs have already been lowered.
1290 bool varargs_are_lowered_;
1291 // True if types have been determined.
1292 bool types_are_determined_;
1293 // True if the call is an argument to a defer statement.
1297 // An expression which represents a pointer to a function.
1299 class Func_expression : public Expression
1302 Func_expression(Named_object* function, Expression* closure,
1303 source_location location)
1304 : Expression(EXPRESSION_FUNC_REFERENCE, location),
1305 function_(function), closure_(closure)
1308 // Return the object associated with the function.
1310 named_object() const
1311 { return this->function_; }
1313 // Return the closure for this function. This will return NULL if
1314 // the function has no closure, which is the normal case.
1317 { return this->closure_; }
1319 // Return a tree for this function without evaluating the closure.
1321 get_tree_without_closure(Gogo*);
1325 do_traverse(Traverse*);
1331 do_determine_type(const Type_context*)
1333 if (this->closure_ != NULL)
1334 this->closure_->determine_type_no_context();
1340 return Expression::make_func_reference(this->function_,
1341 (this->closure_ == NULL
1343 : this->closure_->copy()),
1348 do_get_tree(Translate_context*);
1351 // The function itself.
1352 Named_object* function_;
1353 // A closure. This is normally NULL. For a nested function, it may
1354 // be a heap-allocated struct holding pointers to all the variables
1355 // referenced by this function and defined in enclosing functions.
1356 Expression* closure_;
1359 // A reference to an unknown name.
1361 class Unknown_expression : public Parser_expression
1364 Unknown_expression(Named_object* named_object, source_location location)
1365 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
1366 named_object_(named_object), is_composite_literal_key_(false)
1369 // The associated named object.
1371 named_object() const
1372 { return this->named_object_; }
1374 // The name of the identifier which was unknown.
1378 // Note that this expression is being used as the key in a composite
1379 // literal, so it may be OK if it is not resolved.
1381 set_is_composite_literal_key()
1382 { this->is_composite_literal_key_ = true; }
1384 // Note that this expression should no longer be treated as a
1385 // composite literal key.
1387 clear_is_composite_literal_key()
1388 { this->is_composite_literal_key_ = false; }
1392 do_lower(Gogo*, Named_object*, int);
1396 { return new Unknown_expression(this->named_object_, this->location()); }
1399 // The unknown name.
1400 Named_object* named_object_;
1401 // True if this is the key in a composite literal.
1402 bool is_composite_literal_key_;
1405 // An index expression. This is lowered to an array index, a string
1406 // index, or a map index.
1408 class Index_expression : public Parser_expression
1411 Index_expression(Expression* left, Expression* start, Expression* end,
1412 source_location location)
1413 : Parser_expression(EXPRESSION_INDEX, location),
1414 left_(left), start_(start), end_(end), is_lvalue_(false)
1417 // Record that this expression is an lvalue.
1420 { this->is_lvalue_ = true; }
1424 do_traverse(Traverse*);
1427 do_lower(Gogo*, Named_object*, int);
1432 return new Index_expression(this->left_->copy(), this->start_->copy(),
1435 : this->end_->copy()),
1440 // The expression being indexed.
1444 // The second index. This is NULL for an index, non-NULL for a
1447 // Whether this is being used as an l-value. We set this during the
1448 // parse because map index expressions need to know.
1452 // An index into a map.
1454 class Map_index_expression : public Expression
1457 Map_index_expression(Expression* map, Expression* index,
1458 source_location location)
1459 : Expression(EXPRESSION_MAP_INDEX, location),
1460 map_(map), index_(index), is_lvalue_(false),
1461 is_in_tuple_assignment_(false)
1467 { return this->map_; }
1471 { return this->map_; }
1473 // Return the index.
1476 { return this->index_; }
1480 { return this->index_; }
1482 // Get the type of the map being indexed.
1484 get_map_type() const;
1486 // Record that this map expression is an lvalue. The difference is
1487 // that an lvalue always inserts the key.
1490 { this->is_lvalue_ = true; }
1492 // Return whether this map expression occurs in an assignment to a
1495 is_in_tuple_assignment() const
1496 { return this->is_in_tuple_assignment_; }
1498 // Record that this map expression occurs in an assignment to a pair
1501 set_is_in_tuple_assignment()
1502 { this->is_in_tuple_assignment_ = true; }
1504 // Return a tree for the map index. This returns a tree which
1505 // evaluates to a pointer to a value in the map. If INSERT is true,
1506 // the key will be inserted if not present, and the value pointer
1507 // will be zero initialized. If INSERT is false, and the key is not
1508 // present in the map, the pointer will be NULL.
1510 get_value_pointer(Translate_context*, bool insert);
1514 do_traverse(Traverse*);
1520 do_determine_type(const Type_context*);
1523 do_check_types(Gogo*);
1528 return Expression::make_map_index(this->map_->copy(),
1529 this->index_->copy(),
1533 // A map index expression is an lvalue but it is not addressable.
1536 do_get_tree(Translate_context*);
1539 // The map we are looking into.
1543 // Whether this is an lvalue.
1545 // Whether this is in a tuple assignment to a pair of values.
1546 bool is_in_tuple_assignment_;
1549 // An expression which represents a method bound to its first
1552 class Bound_method_expression : public Expression
1555 Bound_method_expression(Expression* expr, Expression* method,
1556 source_location location)
1557 : Expression(EXPRESSION_BOUND_METHOD, location),
1558 expr_(expr), expr_type_(NULL), method_(method)
1561 // Return the object which is the first argument.
1564 { return this->expr_; }
1566 // Return the implicit type of the first argument. This will be
1567 // non-NULL when using a method from an anonymous field without
1568 // using an explicit stub.
1570 first_argument_type() const
1571 { return this->expr_type_; }
1573 // Return the reference to the method function.
1576 { return this->method_; }
1578 // Set the implicit type of the expression.
1580 set_first_argument_type(Type* type)
1581 { this->expr_type_ = type; }
1585 do_traverse(Traverse*);
1591 do_determine_type(const Type_context*);
1594 do_check_types(Gogo*);
1599 return new Bound_method_expression(this->expr_->copy(),
1600 this->method_->copy(),
1605 do_get_tree(Translate_context*);
1608 // The object used to find the method. This is passed to the method
1609 // as the first argument.
1611 // The implicit type of the object to pass to the method. This is
1612 // NULL in the normal case, non-NULL when using a method from an
1613 // anonymous field which does not require a stub.
1615 // The method itself. This is a Func_expression.
1616 Expression* method_;
1619 // A reference to a field in a struct.
1621 class Field_reference_expression : public Expression
1624 Field_reference_expression(Expression* expr, unsigned int field_index,
1625 source_location location)
1626 : Expression(EXPRESSION_FIELD_REFERENCE, location),
1627 expr_(expr), field_index_(field_index)
1630 // Return the struct expression.
1633 { return this->expr_; }
1635 // Return the field index.
1638 { return this->field_index_; }
1640 // Set the struct expression. This is used when parsing.
1642 set_struct_expression(Expression* expr)
1644 go_assert(this->expr_ == NULL);
1650 do_traverse(Traverse* traverse)
1651 { return Expression::traverse(&this->expr_, traverse); }
1657 do_determine_type(const Type_context*)
1658 { this->expr_->determine_type_no_context(); }
1661 do_check_types(Gogo*);
1666 return Expression::make_field_reference(this->expr_->copy(),
1672 do_is_addressable() const
1673 { return this->expr_->is_addressable(); }
1676 do_get_tree(Translate_context*);
1679 // The expression we are looking into. This should have a type of
1682 // The zero-based index of the field we are retrieving.
1683 unsigned int field_index_;
1686 // A reference to a field of an interface.
1688 class Interface_field_reference_expression : public Expression
1691 Interface_field_reference_expression(Expression* expr,
1692 const std::string& name,
1693 source_location location)
1694 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
1695 expr_(expr), name_(name)
1698 // Return the expression for the interface object.
1701 { return this->expr_; }
1703 // Return the name of the method to call.
1706 { return this->name_; }
1708 // Return a tree for the pointer to the function to call, given a
1709 // tree for the expression.
1711 get_function_tree(Translate_context*, tree);
1713 // Return a tree for the first argument to pass to the interface
1714 // function, given a tree for the expression. This is the real
1715 // object associated with the interface object.
1717 get_underlying_object_tree(Translate_context*, tree);
1721 do_traverse(Traverse* traverse);
1727 do_determine_type(const Type_context*);
1730 do_check_types(Gogo*);
1735 return Expression::make_interface_field_reference(this->expr_->copy(),
1741 do_get_tree(Translate_context*);
1744 // The expression for the interface object. This should have a type
1745 // of interface or pointer to interface.
1747 // The field we are retrieving--the name of the method.
1751 // A type guard expression.
1753 class Type_guard_expression : public Expression
1756 Type_guard_expression(Expression* expr, Type* type, source_location location)
1757 : Expression(EXPRESSION_TYPE_GUARD, location),
1758 expr_(expr), type_(type)
1761 // Return the expression to convert.
1764 { return this->expr_; }
1766 // Return the type to which to convert.
1769 { return this->type_; }
1773 do_traverse(Traverse* traverse);
1777 { return this->type_; }
1780 do_determine_type(const Type_context*)
1781 { this->expr_->determine_type_no_context(); }
1784 do_check_types(Gogo*);
1789 return new Type_guard_expression(this->expr_->copy(), this->type_,
1794 do_get_tree(Translate_context*);
1797 // The expression to convert.
1799 // The type to which to convert.
1803 // A receive expression.
1805 class Receive_expression : public Expression
1808 Receive_expression(Expression* channel, source_location location)
1809 : Expression(EXPRESSION_RECEIVE, location),
1810 channel_(channel), for_select_(false)
1813 // Return the channel.
1816 { return this->channel_; }
1818 // Note that this is for a select statement.
1821 { this->for_select_ = true; }
1825 do_traverse(Traverse* traverse)
1826 { return Expression::traverse(&this->channel_, traverse); }
1829 do_discarding_value()
1836 do_determine_type(const Type_context*)
1837 { this->channel_->determine_type_no_context(); }
1840 do_check_types(Gogo*);
1845 return Expression::make_receive(this->channel_->copy(), this->location());
1849 do_must_eval_in_order() const
1853 do_get_tree(Translate_context*);
1856 // The channel from which we are receiving.
1857 Expression* channel_;
1858 // Whether this is for a select statement.
1862 #endif // !defined(GO_EXPRESSIONS_H)