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;
18 class Statement_inserter;
28 class Expression_list;
30 class Temporary_reference_expression;
31 class Set_and_use_temporary_expression;
32 class String_expression;
33 class Binary_expression;
34 class Call_expression;
35 class Func_expression;
36 class Unknown_expression;
37 class Index_expression;
38 class Map_index_expression;
39 class Bound_method_expression;
40 class Field_reference_expression;
41 class Interface_field_reference_expression;
42 class Type_guard_expression;
43 class Receive_expression;
44 class Numeric_constant;
48 class Temporary_statement;
50 class Ast_dump_context;
53 // The base class for all expressions.
58 // The types of expressions.
59 enum Expression_classification
65 EXPRESSION_CONST_REFERENCE,
66 EXPRESSION_VAR_REFERENCE,
67 EXPRESSION_TEMPORARY_REFERENCE,
68 EXPRESSION_SET_AND_USE_TEMPORARY,
70 EXPRESSION_FUNC_REFERENCE,
71 EXPRESSION_UNKNOWN_REFERENCE,
80 EXPRESSION_CALL_RESULT,
81 EXPRESSION_BOUND_METHOD,
83 EXPRESSION_ARRAY_INDEX,
84 EXPRESSION_STRING_INDEX,
87 EXPRESSION_FIELD_REFERENCE,
88 EXPRESSION_INTERFACE_FIELD_REFERENCE,
89 EXPRESSION_ALLOCATION,
90 EXPRESSION_TYPE_GUARD,
91 EXPRESSION_CONVERSION,
92 EXPRESSION_UNSAFE_CONVERSION,
93 EXPRESSION_STRUCT_CONSTRUCTION,
94 EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
95 EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
96 EXPRESSION_MAP_CONSTRUCTION,
97 EXPRESSION_COMPOSITE_LITERAL,
98 EXPRESSION_HEAP_COMPOSITE,
100 EXPRESSION_TYPE_DESCRIPTOR,
101 EXPRESSION_TYPE_INFO,
102 EXPRESSION_STRUCT_FIELD_OFFSET,
103 EXPRESSION_MAP_DESCRIPTOR,
104 EXPRESSION_LABEL_ADDR
107 Expression(Expression_classification, Location);
109 virtual ~Expression();
111 // Make an error expression. This is used when a parse error occurs
112 // to prevent cascading errors.
114 make_error(Location);
116 // Make an expression which is really a type. This is used during
119 make_type(Type*, Location);
121 // Make a unary expression.
123 make_unary(Operator, Expression*, Location);
125 // Make a binary expression.
127 make_binary(Operator, Expression*, Expression*, Location);
129 // Make a reference to a constant in an expression.
131 make_const_reference(Named_object*, Location);
133 // Make a reference to a variable in an expression.
135 make_var_reference(Named_object*, Location);
137 // Make a reference to a temporary variable. Temporary variables
138 // are always created by a single statement, which is what we use to
140 static Temporary_reference_expression*
141 make_temporary_reference(Temporary_statement*, Location);
143 // Make an expressions which sets a temporary variable and then
144 // evaluates to a reference to that temporary variable. This is
145 // used to set a temporary variable while retaining the order of
147 static Set_and_use_temporary_expression*
148 make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
150 // Make a sink expression--a reference to the blank identifier _.
154 // Make a reference to a function in an expression.
156 make_func_reference(Named_object*, Expression* closure, Location);
158 // Make a reference to an unknown name. In a correct program this
159 // will always be lowered to a real const/var/func reference.
160 static Unknown_expression*
161 make_unknown_reference(Named_object*, Location);
163 // Make a constant bool expression.
165 make_boolean(bool val, Location);
167 // Make a constant string expression.
169 make_string(const std::string&, Location);
171 // Make a character constant expression. TYPE should be NULL for an
174 make_character(const mpz_t*, Type*, Location);
176 // Make a constant integer expression. TYPE should be NULL for an
179 make_integer(const mpz_t*, Type*, Location);
181 // Make a constant float expression. TYPE should be NULL for an
184 make_float(const mpfr_t*, Type*, Location);
186 // Make a constant complex expression. TYPE should be NULL for an
189 make_complex(const mpfr_t* real, const mpfr_t* imag, Type*, Location);
191 // Make a nil expression.
195 // Make an iota expression. This is used for the predeclared
200 // Make a call expression.
201 static Call_expression*
202 make_call(Expression* func, Expression_list* args, bool is_varargs,
205 // Make a reference to a specific result of a call expression which
208 make_call_result(Call_expression*, unsigned int index);
210 // Make an expression which is a method bound to its first
212 static Bound_method_expression*
213 make_bound_method(Expression* object, Named_object* method, Location);
215 // Make an index or slice expression. This is a parser expression
216 // which represents LEFT[START:END]. END may be NULL, meaning an
217 // index rather than a slice. At parse time we may not know the
218 // type of LEFT. After parsing this is lowered to an array index, a
219 // string index, or a map index.
221 make_index(Expression* left, Expression* start, Expression* end,
224 // Make an array index expression. END may be NULL, in which case
225 // this is an lvalue.
227 make_array_index(Expression* array, Expression* start, Expression* end,
230 // Make a string index expression. END may be NULL. This is never
233 make_string_index(Expression* string, Expression* start, Expression* end,
236 // Make a map index expression. This is an lvalue.
237 static Map_index_expression*
238 make_map_index(Expression* map, Expression* val, Location);
240 // Make a selector. This is a parser expression which represents
241 // LEFT.NAME. At parse time we may not know the type of the left
244 make_selector(Expression* left, const std::string& name, Location);
246 // Make a reference to a field in a struct.
247 static Field_reference_expression*
248 make_field_reference(Expression*, unsigned int field_index, Location);
250 // Make a reference to a field of an interface, with an associated
253 make_interface_field_reference(Expression*, const std::string&,
256 // Make an allocation expression.
258 make_allocation(Type*, Location);
260 // Make a type guard expression.
262 make_type_guard(Expression*, Type*, Location);
264 // Make a type cast expression.
266 make_cast(Type*, Expression*, Location);
268 // Make an unsafe type cast expression. This is only used when
269 // passing parameter to builtin functions that are part of the Go
272 make_unsafe_cast(Type*, Expression*, Location);
274 // Make a composite literal. The DEPTH parameter is how far down we
275 // are in a list of composite literals with omitted types.
277 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
280 // Make a struct composite literal.
282 make_struct_composite_literal(Type*, Expression_list*, Location);
284 // Make a slice composite literal.
286 make_slice_composite_literal(Type*, Expression_list*, Location);
288 // Take a composite literal and allocate it on the heap.
290 make_heap_composite(Expression*, Location);
292 // Make a receive expression. VAL is NULL for a unary receive.
293 static Receive_expression*
294 make_receive(Expression* channel, Location);
296 // Make an expression which evaluates to the address of the type
297 // descriptor for TYPE.
299 make_type_descriptor(Type* type, Location);
301 // Make an expression which evaluates to some characteristic of a
302 // type. These are only used for type descriptors, so there is no
303 // location parameter.
306 // The size of a value of the type.
308 // The required alignment of a value of the type.
310 // The required alignment of a value of the type when used as a
311 // field in a struct.
312 TYPE_INFO_FIELD_ALIGNMENT
316 make_type_info(Type* type, Type_info);
318 // Make an expression which evaluates to the offset of a field in a
319 // struct. This is only used for type descriptors, so there is no
320 // location parameter.
322 make_struct_field_offset(Struct_type*, const Struct_field*);
324 // Make an expression which evaluates to the address of the map
325 // descriptor for TYPE.
327 make_map_descriptor(Map_type* type, Location);
329 // Make an expression which evaluates to the address of an unnamed
332 make_label_addr(Label*, Location);
334 // Return the expression classification.
335 Expression_classification
336 classification() const
337 { return this->classification_; }
339 // Return the location of the expression.
342 { return this->location_; }
344 // Return whether this is a constant expression.
347 { return this->do_is_constant(); }
349 // If this is not a numeric constant, return false. If it is one,
350 // return true, and set VAL to hold the value.
352 numeric_constant_value(Numeric_constant* val) const
353 { return this->do_numeric_constant_value(val); }
355 // If this is not a constant expression with string type, return
356 // false. If it is one, return true, and set VAL to the value.
358 string_constant_value(std::string* val) const
359 { return this->do_string_constant_value(val); }
361 // This is called if the value of this expression is being
362 // discarded. This issues warnings about computed values being
366 { this->do_discarding_value(); }
368 // Return whether this is an error expression.
370 is_error_expression() const
371 { return this->classification_ == EXPRESSION_ERROR; }
373 // Return whether this expression really represents a type.
375 is_type_expression() const
376 { return this->classification_ == EXPRESSION_TYPE; }
378 // If this is a variable reference, return the Var_expression
379 // structure. Otherwise, return NULL. This is a controlled dynamic
383 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
385 const Var_expression*
386 var_expression() const
387 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
389 // If this is a reference to a temporary variable, return the
390 // Temporary_reference_expression. Otherwise, return NULL.
391 Temporary_reference_expression*
392 temporary_reference_expression()
394 return this->convert<Temporary_reference_expression,
395 EXPRESSION_TEMPORARY_REFERENCE>();
398 // If this is a set-and-use-temporary, return the
399 // Set_and_use_temporary_expression. Otherwise, return NULL.
400 Set_and_use_temporary_expression*
401 set_and_use_temporary_expression()
403 return this->convert<Set_and_use_temporary_expression,
404 EXPRESSION_SET_AND_USE_TEMPORARY>();
407 // Return whether this is a sink expression.
409 is_sink_expression() const
410 { return this->classification_ == EXPRESSION_SINK; }
412 // If this is a string expression, return the String_expression
413 // structure. Otherwise, return NULL.
416 { return this->convert<String_expression, EXPRESSION_STRING>(); }
418 // Return whether this is the expression nil.
420 is_nil_expression() const
421 { return this->classification_ == EXPRESSION_NIL; }
423 // If this is an indirection through a pointer, return the
424 // expression being pointed through. Otherwise return this.
428 // If this is a binary expression, return the Binary_expression
429 // structure. Otherwise return NULL.
432 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
434 // If this is a call expression, return the Call_expression
435 // structure. Otherwise, return NULL. This is a controlled dynamic
439 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
441 // If this is an expression which refers to a function, return the
442 // Func_expression structure. Otherwise, return NULL.
445 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
447 const Func_expression*
448 func_expression() const
449 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
451 // If this is an expression which refers to an unknown name, return
452 // the Unknown_expression structure. Otherwise, return NULL.
455 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
457 const Unknown_expression*
458 unknown_expression() const
460 return this->convert<const Unknown_expression,
461 EXPRESSION_UNKNOWN_REFERENCE>();
464 // If this is an index expression, return the Index_expression
465 // structure. Otherwise, return NULL.
468 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
470 // If this is an expression which refers to indexing in a map,
471 // return the Map_index_expression structure. Otherwise, return
473 Map_index_expression*
474 map_index_expression()
475 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
477 // If this is a bound method expression, return the
478 // Bound_method_expression structure. Otherwise, return NULL.
479 Bound_method_expression*
480 bound_method_expression()
481 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
483 // If this is a reference to a field in a struct, return the
484 // Field_reference_expression structure. Otherwise, return NULL.
485 Field_reference_expression*
486 field_reference_expression()
488 return this->convert<Field_reference_expression,
489 EXPRESSION_FIELD_REFERENCE>();
492 // If this is a reference to a field in an interface, return the
493 // Interface_field_reference_expression structure. Otherwise,
495 Interface_field_reference_expression*
496 interface_field_reference_expression()
498 return this->convert<Interface_field_reference_expression,
499 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
502 // If this is a type guard expression, return the
503 // Type_guard_expression structure. Otherwise, return NULL.
504 Type_guard_expression*
505 type_guard_expression()
506 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
508 // If this is a receive expression, return the Receive_expression
509 // structure. Otherwise, return NULL.
512 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
514 // Return true if this is a composite literal.
516 is_composite_literal() const;
518 // Return true if this is a composite literal which is not constant.
520 is_nonconstant_composite_literal() const;
522 // Return true if this is a reference to a local variable.
524 is_local_variable() const;
526 // Traverse an expression.
528 traverse(Expression**, Traverse*);
530 // Traverse subexpressions of this expression.
532 traverse_subexpressions(Traverse*);
534 // Lower an expression. This is called immediately after parsing.
535 // FUNCTION is the function we are in; it will be NULL for an
536 // expression initializing a global variable. INSERTER may be used
537 // to insert statements before the statement or initializer
538 // containing this expression; it is normally used to create
539 // temporary variables. IOTA_VALUE is the value that we should give
540 // to any iota expressions. This function must resolve expressions
541 // which could not be fully parsed into their final form. It
542 // returns the same Expression or a new one.
544 lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
546 { return this->do_lower(gogo, function, inserter, iota_value); }
548 // Determine the real type of an expression with abstract integer,
549 // floating point, or complex type. TYPE_CONTEXT describes the
552 determine_type(const Type_context*);
554 // Check types in an expression.
556 check_types(Gogo* gogo)
557 { this->do_check_types(gogo); }
559 // Determine the type when there is no context.
561 determine_type_no_context();
563 // Return the current type of the expression. This may be changed
564 // by determine_type.
567 { return this->do_type(); }
569 // Return a copy of an expression.
572 { return this->do_copy(); }
574 // Return whether the expression is addressable--something which may
575 // be used as the operand of the unary & operator.
577 is_addressable() const
578 { return this->do_is_addressable(); }
580 // Note that we are taking the address of this expression. ESCAPES
581 // is true if this address escapes the current function.
583 address_taken(bool escapes)
584 { this->do_address_taken(escapes); }
586 // Return whether this expression must be evaluated in order
587 // according to the order of evaluation rules. This is basically
588 // true of all expressions with side-effects.
590 must_eval_in_order() const
591 { return this->do_must_eval_in_order(); }
593 // Return whether subexpressions of this expression must be
594 // evaluated in order. This is true of index expressions and
595 // pointer indirections. This sets *SKIP to the number of
596 // subexpressions to skip during traversing, as index expressions
597 // only requiring moving the index, not the array.
599 must_eval_subexpressions_in_order(int* skip) const
602 return this->do_must_eval_subexpressions_in_order(skip);
605 // Return the tree for this expression.
607 get_tree(Translate_context*);
609 // Return a tree handling any conversions which must be done during
612 convert_for_assignment(Translate_context*, Type* lhs_type, Type* rhs_type,
613 tree rhs_tree, Location location);
615 // Return a tree converting a value of one interface type to another
616 // interface type. If FOR_TYPE_GUARD is true this is for a type
619 convert_interface_to_interface(Translate_context*, Type* lhs_type,
620 Type* rhs_type, tree rhs_tree,
621 bool for_type_guard, Location);
623 // Return a tree implementing the comparison LHS_TREE OP RHS_TREE.
624 // TYPE is the type of both sides.
626 comparison_tree(Translate_context*, Operator op, Type* left_type,
627 tree left_tree, Type* right_type, tree right_tree,
630 // Return a tree for the multi-precision integer VAL in TYPE.
632 integer_constant_tree(mpz_t val, tree type);
634 // Return a tree for the floating point value VAL in TYPE.
636 float_constant_tree(mpfr_t val, tree type);
638 // Return a tree for the complex value REAL/IMAG in TYPE.
640 complex_constant_tree(mpfr_t real, mpfr_t imag, tree type);
642 // Export the expression. This is only used for constants. It will
643 // be used for things like values of named constants and sizes of
646 export_expression(Export* exp) const
647 { this->do_export(exp); }
649 // Import an expression.
651 import_expression(Import*);
653 // Return a tree which checks that VAL, of arbitrary integer type,
654 // is non-negative and is not more than the maximum value of
655 // BOUND_TYPE. If SOFAR is not NULL, it is or'red into the result.
656 // The return value may be NULL if SOFAR is NULL.
658 check_bounds(tree val, tree bound_type, tree sofar, Location);
660 // Dump an expression to a dump constext.
662 dump_expression(Ast_dump_context*) const;
665 // May be implemented by child class: traverse the expressions.
667 do_traverse(Traverse*);
669 // Return a lowered expression.
671 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
674 // Return whether this is a constant expression.
676 do_is_constant() const
679 // Return whether this is a constant expression of numeric type, and
680 // set the Numeric_constant to the value.
682 do_numeric_constant_value(Numeric_constant*) const
685 // Return whether this is a constant expression of string type, and
686 // set VAL to the value.
688 do_string_constant_value(std::string*) const
691 // Called by the parser if the value is being discarded.
693 do_discarding_value();
695 // Child class holds type.
699 // Child class implements determining type information.
701 do_determine_type(const Type_context*) = 0;
703 // Child class implements type checking if needed.
705 do_check_types(Gogo*)
708 // Child class implements copying.
712 // Child class implements whether the expression is addressable.
714 do_is_addressable() const
717 // Child class implements taking the address of an expression.
719 do_address_taken(bool)
722 // Child class implements whether this expression must be evaluated
725 do_must_eval_in_order() const
728 // Child class implements whether this expressions requires that
729 // subexpressions be evaluated in order. The child implementation
730 // may set *SKIP if it should be non-zero.
732 do_must_eval_subexpressions_in_order(int* /* skip */) const
735 // Child class implements conversion to tree.
737 do_get_tree(Translate_context*) = 0;
739 // Child class implements export.
741 do_export(Export*) const;
743 // For children to call to give an error for an unused value.
745 unused_value_error();
747 // For children to call when they detect that they are in error.
751 // For children to call to report an error conveniently.
753 report_error(const char*);
755 // Child class implements dumping to a dump context.
757 do_dump_expression(Ast_dump_context*) const = 0;
760 // Convert to the desired statement classification, or return NULL.
761 // This is a controlled dynamic cast.
762 template<typename Expression_class,
763 Expression_classification expr_classification>
767 return (this->classification_ == expr_classification
768 ? static_cast<Expression_class*>(this)
772 template<typename Expression_class,
773 Expression_classification expr_classification>
774 const Expression_class*
777 return (this->classification_ == expr_classification
778 ? static_cast<const Expression_class*>(this)
783 convert_type_to_interface(Translate_context*, Type*, Type*, tree,
787 get_interface_type_descriptor(Translate_context*, Type*, tree,
791 convert_interface_to_type(Translate_context*, Type*, Type*, tree,
794 // The expression classification.
795 Expression_classification classification_;
796 // The location in the input file.
800 // A list of Expressions.
802 class Expression_list
809 // Return whether the list is empty.
812 { return this->entries_.empty(); }
814 // Return the number of entries in the list.
817 { return this->entries_.size(); }
819 // Add an entry to the end of the list.
821 push_back(Expression* expr)
822 { this->entries_.push_back(expr); }
825 append(Expression_list* add)
826 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
828 // Reserve space in the list.
831 { this->entries_.reserve(size); }
833 // Traverse the expressions in the list.
841 // Return true if the list contains an error expression.
843 contains_error() const;
845 // Return the first and last elements.
848 { return this->entries_.front(); }
852 { return this->entries_.front(); }
856 { return this->entries_.back(); }
860 { return this->entries_.back(); }
864 typedef std::vector<Expression*>::iterator iterator;
865 typedef std::vector<Expression*>::const_iterator const_iterator;
869 { return this->entries_.begin(); }
873 { return this->entries_.begin(); }
877 { return this->entries_.end(); }
881 { return this->entries_.end(); }
886 { this->entries_.erase(p); }
889 std::vector<Expression*> entries_;
892 // An abstract base class for an expression which is only used by the
893 // parser, and is lowered in the lowering pass.
895 class Parser_expression : public Expression
898 Parser_expression(Expression_classification classification,
900 : Expression(classification, location)
905 do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
911 do_determine_type(const Type_context*)
912 { go_unreachable(); }
915 do_check_types(Gogo*)
916 { go_unreachable(); }
919 do_get_tree(Translate_context*)
920 { go_unreachable(); }
923 // An expression which is simply a variable.
925 class Var_expression : public Expression
928 Var_expression(Named_object* variable, Location location)
929 : Expression(EXPRESSION_VAR_REFERENCE, location),
933 // Return the variable.
936 { return this->variable_; }
940 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
946 do_determine_type(const Type_context*);
953 do_is_addressable() const
957 do_address_taken(bool);
960 do_get_tree(Translate_context*);
963 do_dump_expression(Ast_dump_context*) const;
966 // The variable we are referencing.
967 Named_object* variable_;
970 // A reference to a temporary variable.
972 class Temporary_reference_expression : public Expression
975 Temporary_reference_expression(Temporary_statement* statement,
977 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
978 statement_(statement), is_lvalue_(false)
981 // Indicate that this reference appears on the left hand side of an
982 // assignment statement.
985 { this->is_lvalue_ = true; }
992 do_determine_type(const Type_context*)
997 { return make_temporary_reference(this->statement_, this->location()); }
1000 do_is_addressable() const
1004 do_address_taken(bool);
1007 do_get_tree(Translate_context*);
1010 do_dump_expression(Ast_dump_context*) const;
1013 // The statement where the temporary variable is defined.
1014 Temporary_statement* statement_;
1015 // Whether this reference appears on the left hand side of an
1016 // assignment statement.
1020 // Set and use a temporary variable.
1022 class Set_and_use_temporary_expression : public Expression
1025 Set_and_use_temporary_expression(Temporary_statement* statement,
1026 Expression* expr, Location location)
1027 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1028 statement_(statement), expr_(expr)
1031 // Return the temporary.
1032 Temporary_statement*
1034 { return this->statement_; }
1036 // Return the expression.
1039 { return this->expr_; }
1043 do_traverse(Traverse* traverse)
1044 { return Expression::traverse(&this->expr_, traverse); }
1050 do_determine_type(const Type_context*)
1056 return make_set_and_use_temporary(this->statement_, this->expr_,
1061 do_is_addressable() const
1065 do_address_taken(bool);
1068 do_get_tree(Translate_context*);
1071 do_dump_expression(Ast_dump_context*) const;
1074 // The statement where the temporary variable is defined.
1075 Temporary_statement* statement_;
1076 // The expression to assign to the temporary.
1080 // A string expression.
1082 class String_expression : public Expression
1085 String_expression(const std::string& val, Location location)
1086 : Expression(EXPRESSION_STRING, location),
1087 val_(val), type_(NULL)
1092 { return this->val_; }
1099 do_is_constant() const
1103 do_string_constant_value(std::string* val) const
1113 do_determine_type(const Type_context*);
1120 do_get_tree(Translate_context*);
1122 // Write string literal to a string dump.
1124 export_string(String_dump* exp, const String_expression* str);
1127 do_export(Export*) const;
1130 do_dump_expression(Ast_dump_context*) const;
1133 // The string value. This is immutable.
1134 const std::string val_;
1135 // The type as determined by context.
1139 // A binary expression.
1141 class Binary_expression : public Expression
1144 Binary_expression(Operator op, Expression* left, Expression* right,
1146 : Expression(EXPRESSION_BINARY, location),
1147 op_(op), left_(left), right_(right)
1150 // Return the operator.
1153 { return this->op_; }
1155 // Return the left hand expression.
1158 { return this->left_; }
1160 // Return the right hand expression.
1163 { return this->right_; }
1165 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1166 // Return true if this could be done, false if not. Issue errors at
1167 // LOCATION as appropriate.
1169 eval_constant(Operator op, Numeric_constant* left_nc,
1170 Numeric_constant* right_nc, Location location,
1171 Numeric_constant* nc);
1173 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1174 // *RESULT. Return true if this could be done, false if not. Issue
1175 // errors at LOCATION as appropriate.
1177 compare_constant(Operator op, Numeric_constant* left_nc,
1178 Numeric_constant* right_nc, Location location,
1184 // Report an error if OP can not be applied to TYPE. Return whether
1185 // it can. OTYPE is the type of the other operand.
1187 check_operator_type(Operator op, Type* type, Type* otype, Location);
1191 do_traverse(Traverse* traverse);
1194 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1197 do_is_constant() const
1198 { return this->left_->is_constant() && this->right_->is_constant(); }
1201 do_numeric_constant_value(Numeric_constant*) const;
1204 do_discarding_value();
1210 do_determine_type(const Type_context*);
1213 do_check_types(Gogo*);
1218 return Expression::make_binary(this->op_, this->left_->copy(),
1219 this->right_->copy(), this->location());
1223 do_get_tree(Translate_context*);
1226 do_export(Export*) const;
1229 do_dump_expression(Ast_dump_context*) const;
1233 operation_type(Operator op, Type* left_type, Type* right_type,
1234 Type** result_type);
1237 cmp_to_bool(Operator op, int cmp);
1240 eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
1241 Location, Numeric_constant*);
1244 eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
1245 Location, Numeric_constant*);
1248 eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
1249 Location, Numeric_constant*);
1252 compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
1255 compare_float(const Numeric_constant*, const Numeric_constant *, int*);
1258 compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
1261 lower_struct_comparison(Gogo*, Statement_inserter*);
1264 lower_array_comparison(Gogo*, Statement_inserter*);
1267 lower_compare_to_memcmp(Gogo*, Statement_inserter*);
1270 operand_address(Statement_inserter*, Expression*);
1272 // The binary operator to apply.
1274 // The left hand side operand.
1276 // The right hand side operand.
1280 // A call expression. The go statement needs to dig inside this.
1282 class Call_expression : public Expression
1285 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
1287 : Expression(EXPRESSION_CALL, location),
1288 fn_(fn), args_(args), type_(NULL), results_(NULL), tree_(NULL),
1289 is_varargs_(is_varargs), are_hidden_fields_ok_(false),
1290 varargs_are_lowered_(false), types_are_determined_(false),
1291 is_deferred_(false), issued_error_(false)
1294 // The function to call.
1297 { return this->fn_; }
1302 { return this->args_; }
1304 const Expression_list*
1306 { return this->args_; }
1308 // Get the function type.
1310 get_function_type() const;
1312 // Return the number of values this call will return.
1314 result_count() const;
1316 // Return the temporary variable which holds result I. This is only
1317 // valid after the expression has been lowered, and is only valid
1318 // for calls which return multiple results.
1319 Temporary_statement*
1320 result(size_t i) const;
1322 // Return whether this is a call to the predeclared function
1325 is_recover_call() const;
1327 // Set the argument for a call to recover.
1329 set_recover_arg(Expression*);
1331 // Whether the last argument is a varargs argument (f(a...)).
1334 { return this->is_varargs_; }
1336 // Note that varargs have already been lowered.
1338 set_varargs_are_lowered()
1339 { this->varargs_are_lowered_ = true; }
1341 // Note that it is OK for this call to set hidden fields when
1342 // passing arguments.
1344 set_hidden_fields_are_ok()
1345 { this->are_hidden_fields_ok_ = true; }
1347 // Whether this call is being deferred.
1350 { return this->is_deferred_; }
1352 // Note that the call is being deferred.
1355 { this->is_deferred_ = true; }
1357 // We have found an error with this call expression; return true if
1358 // we should report it.
1364 do_traverse(Traverse*);
1367 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1370 do_discarding_value()
1377 do_determine_type(const Type_context*);
1380 do_check_types(Gogo*);
1385 return Expression::make_call(this->fn_->copy(),
1386 (this->args_ == NULL
1388 : this->args_->copy()),
1389 this->is_varargs_, this->location());
1393 do_must_eval_in_order() const;
1396 do_get_tree(Translate_context*);
1399 do_is_recover_call() const;
1402 do_set_recover_arg(Expression*);
1404 // Let a builtin expression change the argument list.
1406 set_args(Expression_list* args)
1407 { this->args_ = args; }
1409 // Let a builtin expression lower varargs.
1411 lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
1412 Type* varargs_type, size_t param_count);
1414 // Let a builtin expression check whether types have been
1417 determining_types();
1420 do_dump_expression(Ast_dump_context*) const;
1424 check_argument_type(int, const Type*, const Type*, Location, bool);
1427 interface_method_function(Translate_context*,
1428 Interface_field_reference_expression*,
1432 set_results(Translate_context*, tree);
1434 // The function to call.
1436 // The arguments to pass. This may be NULL if there are no
1438 Expression_list* args_;
1439 // The type of the expression, to avoid recomputing it.
1441 // The list of temporaries which will hold the results if the
1442 // function returns a tuple.
1443 std::vector<Temporary_statement*>* results_;
1444 // The tree for the call, used for a call which returns a tuple.
1446 // True if the last argument is a varargs argument (f(a...)).
1448 // True if this statement may pass hidden fields in the arguments.
1449 // This is used for generated method stubs.
1450 bool are_hidden_fields_ok_;
1451 // True if varargs have already been lowered.
1452 bool varargs_are_lowered_;
1453 // True if types have been determined.
1454 bool types_are_determined_;
1455 // True if the call is an argument to a defer statement.
1457 // True if we reported an error about a mismatch between call
1458 // results and uses. This is to avoid producing multiple errors
1459 // when there are multiple Call_result_expressions.
1463 // An expression which represents a pointer to a function.
1465 class Func_expression : public Expression
1468 Func_expression(Named_object* function, Expression* closure,
1470 : Expression(EXPRESSION_FUNC_REFERENCE, location),
1471 function_(function), closure_(closure)
1474 // Return the object associated with the function.
1476 named_object() const
1477 { return this->function_; }
1479 // Return the closure for this function. This will return NULL if
1480 // the function has no closure, which is the normal case.
1483 { return this->closure_; }
1485 // Return a tree for this function without evaluating the closure.
1487 get_tree_without_closure(Gogo*);
1491 do_traverse(Traverse*);
1497 do_determine_type(const Type_context*)
1499 if (this->closure_ != NULL)
1500 this->closure_->determine_type_no_context();
1506 return Expression::make_func_reference(this->function_,
1507 (this->closure_ == NULL
1509 : this->closure_->copy()),
1514 do_get_tree(Translate_context*);
1517 do_dump_expression(Ast_dump_context*) const;
1520 // The function itself.
1521 Named_object* function_;
1522 // A closure. This is normally NULL. For a nested function, it may
1523 // be a heap-allocated struct holding pointers to all the variables
1524 // referenced by this function and defined in enclosing functions.
1525 Expression* closure_;
1528 // A reference to an unknown name.
1530 class Unknown_expression : public Parser_expression
1533 Unknown_expression(Named_object* named_object, Location location)
1534 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
1535 named_object_(named_object), no_error_message_(false),
1536 is_composite_literal_key_(false)
1539 // The associated named object.
1541 named_object() const
1542 { return this->named_object_; }
1544 // The name of the identifier which was unknown.
1548 // Call this to indicate that we should not give an error if this
1549 // name is never defined. This is used to avoid knock-on errors
1550 // during an erroneous parse.
1552 set_no_error_message()
1553 { this->no_error_message_ = true; }
1555 // Note that this expression is being used as the key in a composite
1556 // literal, so it may be OK if it is not resolved.
1558 set_is_composite_literal_key()
1559 { this->is_composite_literal_key_ = true; }
1561 // Note that this expression should no longer be treated as a
1562 // composite literal key.
1564 clear_is_composite_literal_key()
1565 { this->is_composite_literal_key_ = false; }
1569 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1573 { return new Unknown_expression(this->named_object_, this->location()); }
1576 do_dump_expression(Ast_dump_context*) const;
1579 // The unknown name.
1580 Named_object* named_object_;
1581 // True if we should not give errors if this is undefined. This is
1582 // used if there was a parse failure.
1583 bool no_error_message_;
1584 // True if this is the key in a composite literal.
1585 bool is_composite_literal_key_;
1588 // An index expression. This is lowered to an array index, a string
1589 // index, or a map index.
1591 class Index_expression : public Parser_expression
1594 Index_expression(Expression* left, Expression* start, Expression* end,
1596 : Parser_expression(EXPRESSION_INDEX, location),
1597 left_(left), start_(start), end_(end), is_lvalue_(false)
1600 // Record that this expression is an lvalue.
1603 { this->is_lvalue_ = true; }
1605 // Dump an index expression, i.e. an expression of the form
1606 // expr[expr] or expr[expr:expr], to a dump context.
1608 dump_index_expression(Ast_dump_context*, const Expression* expr,
1609 const Expression* start, const Expression* end);
1613 do_traverse(Traverse*);
1616 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1621 return new Index_expression(this->left_->copy(), this->start_->copy(),
1624 : this->end_->copy()),
1629 do_must_eval_subexpressions_in_order(int* skip) const
1636 do_dump_expression(Ast_dump_context*) const;
1639 // The expression being indexed.
1643 // The second index. This is NULL for an index, non-NULL for a
1646 // Whether this is being used as an l-value. We set this during the
1647 // parse because map index expressions need to know.
1651 // An index into a map.
1653 class Map_index_expression : public Expression
1656 Map_index_expression(Expression* map, Expression* index,
1658 : Expression(EXPRESSION_MAP_INDEX, location),
1659 map_(map), index_(index), is_lvalue_(false),
1660 is_in_tuple_assignment_(false)
1666 { return this->map_; }
1670 { return this->map_; }
1672 // Return the index.
1675 { return this->index_; }
1679 { return this->index_; }
1681 // Get the type of the map being indexed.
1683 get_map_type() const;
1685 // Record that this map expression is an lvalue. The difference is
1686 // that an lvalue always inserts the key.
1689 { this->is_lvalue_ = true; }
1691 // Return whether this map expression occurs in an assignment to a
1694 is_in_tuple_assignment() const
1695 { return this->is_in_tuple_assignment_; }
1697 // Record that this map expression occurs in an assignment to a pair
1700 set_is_in_tuple_assignment()
1701 { this->is_in_tuple_assignment_ = true; }
1703 // Return a tree for the map index. This returns a tree which
1704 // evaluates to a pointer to a value in the map. If INSERT is true,
1705 // the key will be inserted if not present, and the value pointer
1706 // will be zero initialized. If INSERT is false, and the key is not
1707 // present in the map, the pointer will be NULL.
1709 get_value_pointer(Translate_context*, bool insert);
1713 do_traverse(Traverse*);
1719 do_determine_type(const Type_context*);
1722 do_check_types(Gogo*);
1727 return Expression::make_map_index(this->map_->copy(),
1728 this->index_->copy(),
1733 do_must_eval_subexpressions_in_order(int* skip) const
1739 // A map index expression is an lvalue but it is not addressable.
1742 do_get_tree(Translate_context*);
1745 do_dump_expression(Ast_dump_context*) const;
1748 // The map we are looking into.
1752 // Whether this is an lvalue.
1754 // Whether this is in a tuple assignment to a pair of values.
1755 bool is_in_tuple_assignment_;
1758 // An expression which represents a method bound to its first
1761 class Bound_method_expression : public Expression
1764 Bound_method_expression(Expression* expr, Named_object* method,
1766 : Expression(EXPRESSION_BOUND_METHOD, location),
1767 expr_(expr), expr_type_(NULL), method_(method)
1770 // Return the object which is the first argument.
1773 { return this->expr_; }
1775 // Return the implicit type of the first argument. This will be
1776 // non-NULL when using a method from an anonymous field without
1777 // using an explicit stub.
1779 first_argument_type() const
1780 { return this->expr_type_; }
1782 // Return the method function.
1785 { return this->method_; }
1787 // Set the implicit type of the expression.
1789 set_first_argument_type(Type* type)
1790 { this->expr_type_ = type; }
1794 do_traverse(Traverse*);
1800 do_determine_type(const Type_context*);
1803 do_check_types(Gogo*);
1808 return new Bound_method_expression(this->expr_->copy(), this->method_,
1813 do_get_tree(Translate_context*);
1816 do_dump_expression(Ast_dump_context*) const;
1819 // The object used to find the method. This is passed to the method
1820 // as the first argument.
1822 // The implicit type of the object to pass to the method. This is
1823 // NULL in the normal case, non-NULL when using a method from an
1824 // anonymous field which does not require a stub.
1826 // The method itself.
1827 Named_object* method_;
1830 // A reference to a field in a struct.
1832 class Field_reference_expression : public Expression
1835 Field_reference_expression(Expression* expr, unsigned int field_index,
1837 : Expression(EXPRESSION_FIELD_REFERENCE, location),
1838 expr_(expr), field_index_(field_index)
1841 // Return the struct expression.
1844 { return this->expr_; }
1846 // Return the field index.
1849 { return this->field_index_; }
1851 // Set the struct expression. This is used when parsing.
1853 set_struct_expression(Expression* expr)
1855 go_assert(this->expr_ == NULL);
1861 do_traverse(Traverse* traverse)
1862 { return Expression::traverse(&this->expr_, traverse); }
1868 do_determine_type(const Type_context*)
1869 { this->expr_->determine_type_no_context(); }
1872 do_check_types(Gogo*);
1877 return Expression::make_field_reference(this->expr_->copy(),
1883 do_is_addressable() const
1884 { return this->expr_->is_addressable(); }
1887 do_get_tree(Translate_context*);
1890 do_dump_expression(Ast_dump_context*) const;
1893 // The expression we are looking into. This should have a type of
1896 // The zero-based index of the field we are retrieving.
1897 unsigned int field_index_;
1900 // A reference to a field of an interface.
1902 class Interface_field_reference_expression : public Expression
1905 Interface_field_reference_expression(Expression* expr,
1906 const std::string& name,
1908 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
1909 expr_(expr), name_(name)
1912 // Return the expression for the interface object.
1915 { return this->expr_; }
1917 // Return the name of the method to call.
1920 { return this->name_; }
1922 // Return a tree for the pointer to the function to call, given a
1923 // tree for the expression.
1925 get_function_tree(Translate_context*, tree);
1927 // Return a tree for the first argument to pass to the interface
1928 // function, given a tree for the expression. This is the real
1929 // object associated with the interface object.
1931 get_underlying_object_tree(Translate_context*, tree);
1935 do_traverse(Traverse* traverse);
1941 do_determine_type(const Type_context*);
1944 do_check_types(Gogo*);
1949 return Expression::make_interface_field_reference(this->expr_->copy(),
1955 do_get_tree(Translate_context*);
1958 do_dump_expression(Ast_dump_context*) const;
1961 // The expression for the interface object. This should have a type
1962 // of interface or pointer to interface.
1964 // The field we are retrieving--the name of the method.
1968 // A type guard expression.
1970 class Type_guard_expression : public Expression
1973 Type_guard_expression(Expression* expr, Type* type, Location location)
1974 : Expression(EXPRESSION_TYPE_GUARD, location),
1975 expr_(expr), type_(type)
1978 // Return the expression to convert.
1981 { return this->expr_; }
1983 // Return the type to which to convert.
1986 { return this->type_; }
1990 do_traverse(Traverse* traverse);
1994 { return this->type_; }
1997 do_determine_type(const Type_context*)
1998 { this->expr_->determine_type_no_context(); }
2001 do_check_types(Gogo*);
2006 return new Type_guard_expression(this->expr_->copy(), this->type_,
2011 do_get_tree(Translate_context*);
2014 do_dump_expression(Ast_dump_context*) const;
2017 // The expression to convert.
2019 // The type to which to convert.
2023 // A receive expression.
2025 class Receive_expression : public Expression
2028 Receive_expression(Expression* channel, Location location)
2029 : Expression(EXPRESSION_RECEIVE, location),
2033 // Return the channel.
2036 { return this->channel_; }
2040 do_traverse(Traverse* traverse)
2041 { return Expression::traverse(&this->channel_, traverse); }
2044 do_discarding_value()
2051 do_determine_type(const Type_context*)
2052 { this->channel_->determine_type_no_context(); }
2055 do_check_types(Gogo*);
2060 return Expression::make_receive(this->channel_->copy(), this->location());
2064 do_must_eval_in_order() const
2068 do_get_tree(Translate_context*);
2071 do_dump_expression(Ast_dump_context*) const;
2074 // The channel from which we are receiving.
2075 Expression* channel_;
2078 // A numeric constant. This is used both for untyped constants and
2079 // for constants that have a type.
2081 class Numeric_constant
2085 : classification_(NC_INVALID), type_(NULL)
2088 ~Numeric_constant();
2090 Numeric_constant(const Numeric_constant&);
2092 Numeric_constant& operator=(const Numeric_constant&);
2094 // Set to an unsigned long value.
2096 set_unsigned_long(Type*, unsigned long);
2098 // Set to an integer value.
2100 set_int(Type*, const mpz_t);
2102 // Set to a rune value.
2104 set_rune(Type*, const mpz_t);
2106 // Set to a floating point value.
2108 set_float(Type*, const mpfr_t);
2110 // Set to a complex value.
2112 set_complex(Type*, const mpfr_t, const mpfr_t);
2117 { return this->classification_ == Numeric_constant::NC_INT; }
2121 { return this->classification_ == Numeric_constant::NC_RUNE; }
2125 { return this->classification_ == Numeric_constant::NC_FLOAT; }
2129 { return this->classification_ == Numeric_constant::NC_COMPLEX; }
2131 // Value retrievers. These will initialize the values as well as
2132 // set them. GET_INT is only valid if IS_INT returns true, and
2133 // likewise respectively.
2135 get_int(mpz_t*) const;
2138 get_rune(mpz_t*) const;
2141 get_float(mpfr_t*) const;
2144 get_complex(mpfr_t*, mpfr_t*) const;
2146 // Codes returned by to_unsigned_long.
2147 enum To_unsigned_long
2149 // Value is integer and fits in unsigned long.
2151 // Value is not integer.
2153 // Value is integer but is negative.
2155 // Value is non-negative integer but does not fit in unsigned
2160 // If the value can be expressed as an integer that fits in an
2161 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
2162 // one of the other To_unsigned_long codes.
2164 to_unsigned_long(unsigned long* val) const;
2166 // If the value can be expressed as an int, return true and
2167 // initialize and set VAL. This will return false for a value with
2168 // an explicit float or complex type, even if the value is integral.
2170 to_int(mpz_t* val) const;
2172 // If the value can be expressed as a float, return true and
2173 // initialize and set VAL.
2175 to_float(mpfr_t* val) const;
2177 // If the value can be expressed as a complex, return true and
2178 // initialize and set VR and VI.
2180 to_complex(mpfr_t* vr, mpfr_t* vi) const;
2186 // If the constant can be expressed in TYPE, then set the type of
2187 // the constant to TYPE and return true. Otherwise return false,
2188 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
2189 // the location to use for the error.
2191 set_type(Type* type, bool issue_error, Location location);
2193 // Return an Expression for this value.
2195 expression(Location) const;
2202 mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
2205 mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
2208 check_int_type(Integer_type*, bool, Location) const;
2211 check_float_type(Float_type*, bool, Location) const;
2214 check_complex_type(Complex_type*, bool, Location) const;
2216 // The kinds of constants.
2226 // The kind of constant.
2227 Classification classification_;
2231 // If NC_INT or NC_RUNE.
2242 // The type if there is one. This will be NULL for an untyped
2247 #endif // !defined(GO_EXPRESSIONS_H)