* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / gcc / go / gofrontend / expressions.h
blob67a4bb985e66f52efd7646c10c4ffe6e762f5fff
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
10 #include <mpfr.h>
12 #include "operator.h"
14 class Gogo;
15 class Translate_context;
16 class Traverse;
17 class Statement_inserter;
18 class Type;
19 class Method;
20 struct Type_context;
21 class Integer_type;
22 class Float_type;
23 class Complex_type;
24 class Function_type;
25 class Map_type;
26 class Struct_type;
27 class Struct_field;
28 class Expression_list;
29 class Var_expression;
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 Func_descriptor_expression;
37 class Unknown_expression;
38 class Index_expression;
39 class Map_index_expression;
40 class Bound_method_expression;
41 class Field_reference_expression;
42 class Interface_field_reference_expression;
43 class Type_guard_expression;
44 class Receive_expression;
45 class Numeric_constant;
46 class Named_object;
47 class Export;
48 class Import;
49 class Temporary_statement;
50 class Label;
51 class Ast_dump_context;
52 class String_dump;
54 // The base class for all expressions.
56 class Expression
58 public:
59 // The types of expressions.
60 enum Expression_classification
62 EXPRESSION_ERROR,
63 EXPRESSION_TYPE,
64 EXPRESSION_UNARY,
65 EXPRESSION_BINARY,
66 EXPRESSION_CONST_REFERENCE,
67 EXPRESSION_VAR_REFERENCE,
68 EXPRESSION_TEMPORARY_REFERENCE,
69 EXPRESSION_SET_AND_USE_TEMPORARY,
70 EXPRESSION_SINK,
71 EXPRESSION_FUNC_REFERENCE,
72 EXPRESSION_FUNC_DESCRIPTOR,
73 EXPRESSION_FUNC_CODE_REFERENCE,
74 EXPRESSION_UNKNOWN_REFERENCE,
75 EXPRESSION_BOOLEAN,
76 EXPRESSION_STRING,
77 EXPRESSION_INTEGER,
78 EXPRESSION_FLOAT,
79 EXPRESSION_COMPLEX,
80 EXPRESSION_NIL,
81 EXPRESSION_IOTA,
82 EXPRESSION_CALL,
83 EXPRESSION_CALL_RESULT,
84 EXPRESSION_BOUND_METHOD,
85 EXPRESSION_INDEX,
86 EXPRESSION_ARRAY_INDEX,
87 EXPRESSION_STRING_INDEX,
88 EXPRESSION_MAP_INDEX,
89 EXPRESSION_SELECTOR,
90 EXPRESSION_FIELD_REFERENCE,
91 EXPRESSION_INTERFACE_FIELD_REFERENCE,
92 EXPRESSION_ALLOCATION,
93 EXPRESSION_TYPE_GUARD,
94 EXPRESSION_CONVERSION,
95 EXPRESSION_UNSAFE_CONVERSION,
96 EXPRESSION_STRUCT_CONSTRUCTION,
97 EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
98 EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
99 EXPRESSION_MAP_CONSTRUCTION,
100 EXPRESSION_COMPOSITE_LITERAL,
101 EXPRESSION_HEAP_COMPOSITE,
102 EXPRESSION_RECEIVE,
103 EXPRESSION_TYPE_DESCRIPTOR,
104 EXPRESSION_TYPE_INFO,
105 EXPRESSION_STRUCT_FIELD_OFFSET,
106 EXPRESSION_MAP_DESCRIPTOR,
107 EXPRESSION_LABEL_ADDR
110 Expression(Expression_classification, Location);
112 virtual ~Expression();
114 // Make an error expression. This is used when a parse error occurs
115 // to prevent cascading errors.
116 static Expression*
117 make_error(Location);
119 // Make an expression which is really a type. This is used during
120 // parsing.
121 static Expression*
122 make_type(Type*, Location);
124 // Make a unary expression.
125 static Expression*
126 make_unary(Operator, Expression*, Location);
128 // Make a binary expression.
129 static Expression*
130 make_binary(Operator, Expression*, Expression*, Location);
132 // Make a reference to a constant in an expression.
133 static Expression*
134 make_const_reference(Named_object*, Location);
136 // Make a reference to a variable in an expression.
137 static Expression*
138 make_var_reference(Named_object*, Location);
140 // Make a reference to a temporary variable. Temporary variables
141 // are always created by a single statement, which is what we use to
142 // refer to them.
143 static Temporary_reference_expression*
144 make_temporary_reference(Temporary_statement*, Location);
146 // Make an expressions which sets a temporary variable and then
147 // evaluates to a reference to that temporary variable. This is
148 // used to set a temporary variable while retaining the order of
149 // evaluation.
150 static Set_and_use_temporary_expression*
151 make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
153 // Make a sink expression--a reference to the blank identifier _.
154 static Expression*
155 make_sink(Location);
157 // Make a reference to a function in an expression. This returns a
158 // pointer to the struct holding the address of the function
159 // followed by any closed-over variables.
160 static Expression*
161 make_func_reference(Named_object*, Expression* closure, Location);
163 // Make a function descriptor, an immutable struct with a single
164 // field that points to the function code. This may only be used
165 // with functions that do not have closures. FN is the function for
166 // which we are making the descriptor.
167 static Func_descriptor_expression*
168 make_func_descriptor(Named_object* fn);
170 // Make a reference to the code of a function. This is used to set
171 // descriptor and closure fields.
172 static Expression*
173 make_func_code_reference(Named_object*, Location);
175 // Make a reference to an unknown name. In a correct program this
176 // will always be lowered to a real const/var/func reference.
177 static Unknown_expression*
178 make_unknown_reference(Named_object*, Location);
180 // Make a constant bool expression.
181 static Expression*
182 make_boolean(bool val, Location);
184 // Make a constant string expression.
185 static Expression*
186 make_string(const std::string&, Location);
188 // Make a character constant expression. TYPE should be NULL for an
189 // abstract type.
190 static Expression*
191 make_character(const mpz_t*, Type*, Location);
193 // Make a constant integer expression. TYPE should be NULL for an
194 // abstract type.
195 static Expression*
196 make_integer(const mpz_t*, Type*, Location);
198 // Make a constant float expression. TYPE should be NULL for an
199 // abstract type.
200 static Expression*
201 make_float(const mpfr_t*, Type*, Location);
203 // Make a constant complex expression. TYPE should be NULL for an
204 // abstract type.
205 static Expression*
206 make_complex(const mpfr_t* real, const mpfr_t* imag, Type*, Location);
208 // Make a nil expression.
209 static Expression*
210 make_nil(Location);
212 // Make an iota expression. This is used for the predeclared
213 // constant iota.
214 static Expression*
215 make_iota();
217 // Make a call expression.
218 static Call_expression*
219 make_call(Expression* func, Expression_list* args, bool is_varargs,
220 Location);
222 // Make a reference to a specific result of a call expression which
223 // returns a tuple.
224 static Expression*
225 make_call_result(Call_expression*, unsigned int index);
227 // Make an expression which is a method bound to its first
228 // parameter. METHOD is the method being called, FUNCTION is the
229 // function to call.
230 static Bound_method_expression*
231 make_bound_method(Expression* object, const Method* method,
232 Named_object* function, Location);
234 // Make an index or slice expression. This is a parser expression
235 // which represents LEFT[START:END]. END may be NULL, meaning an
236 // index rather than a slice. At parse time we may not know the
237 // type of LEFT. After parsing this is lowered to an array index, a
238 // string index, or a map index.
239 static Expression*
240 make_index(Expression* left, Expression* start, Expression* end,
241 Location);
243 // Make an array index expression. END may be NULL, in which case
244 // this is an lvalue.
245 static Expression*
246 make_array_index(Expression* array, Expression* start, Expression* end,
247 Location);
249 // Make a string index expression. END may be NULL. This is never
250 // an lvalue.
251 static Expression*
252 make_string_index(Expression* string, Expression* start, Expression* end,
253 Location);
255 // Make a map index expression. This is an lvalue.
256 static Map_index_expression*
257 make_map_index(Expression* map, Expression* val, Location);
259 // Make a selector. This is a parser expression which represents
260 // LEFT.NAME. At parse time we may not know the type of the left
261 // hand side.
262 static Expression*
263 make_selector(Expression* left, const std::string& name, Location);
265 // Make a reference to a field in a struct.
266 static Field_reference_expression*
267 make_field_reference(Expression*, unsigned int field_index, Location);
269 // Make a reference to a field of an interface, with an associated
270 // object.
271 static Expression*
272 make_interface_field_reference(Expression*, const std::string&,
273 Location);
275 // Make an allocation expression.
276 static Expression*
277 make_allocation(Type*, Location);
279 // Make a type guard expression.
280 static Expression*
281 make_type_guard(Expression*, Type*, Location);
283 // Make a type cast expression.
284 static Expression*
285 make_cast(Type*, Expression*, Location);
287 // Make an unsafe type cast expression. This is only used when
288 // passing parameter to builtin functions that are part of the Go
289 // runtime.
290 static Expression*
291 make_unsafe_cast(Type*, Expression*, Location);
293 // Make a composite literal. The DEPTH parameter is how far down we
294 // are in a list of composite literals with omitted types.
295 static Expression*
296 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
297 Location);
299 // Make a struct composite literal.
300 static Expression*
301 make_struct_composite_literal(Type*, Expression_list*, Location);
303 // Make a slice composite literal.
304 static Expression*
305 make_slice_composite_literal(Type*, Expression_list*, Location);
307 // Take a composite literal and allocate it on the heap.
308 static Expression*
309 make_heap_composite(Expression*, Location);
311 // Make a receive expression. VAL is NULL for a unary receive.
312 static Receive_expression*
313 make_receive(Expression* channel, Location);
315 // Make an expression which evaluates to the address of the type
316 // descriptor for TYPE.
317 static Expression*
318 make_type_descriptor(Type* type, Location);
320 // Make an expression which evaluates to some characteristic of a
321 // type. These are only used for type descriptors, so there is no
322 // location parameter.
323 enum Type_info
325 // The size of a value of the type.
326 TYPE_INFO_SIZE,
327 // The required alignment of a value of the type.
328 TYPE_INFO_ALIGNMENT,
329 // The required alignment of a value of the type when used as a
330 // field in a struct.
331 TYPE_INFO_FIELD_ALIGNMENT
334 static Expression*
335 make_type_info(Type* type, Type_info);
337 // Make an expression which evaluates to the offset of a field in a
338 // struct. This is only used for type descriptors, so there is no
339 // location parameter.
340 static Expression*
341 make_struct_field_offset(Struct_type*, const Struct_field*);
343 // Make an expression which evaluates to the address of the map
344 // descriptor for TYPE.
345 static Expression*
346 make_map_descriptor(Map_type* type, Location);
348 // Make an expression which evaluates to the address of an unnamed
349 // label.
350 static Expression*
351 make_label_addr(Label*, Location);
353 // Return the expression classification.
354 Expression_classification
355 classification() const
356 { return this->classification_; }
358 // Return the location of the expression.
359 Location
360 location() const
361 { return this->location_; }
363 // Return whether this is a constant expression.
364 bool
365 is_constant() const
366 { return this->do_is_constant(); }
368 // If this is not a numeric constant, return false. If it is one,
369 // return true, and set VAL to hold the value.
370 bool
371 numeric_constant_value(Numeric_constant* val) const
372 { return this->do_numeric_constant_value(val); }
374 // If this is not a constant expression with string type, return
375 // false. If it is one, return true, and set VAL to the value.
376 bool
377 string_constant_value(std::string* val) const
378 { return this->do_string_constant_value(val); }
380 // This is called if the value of this expression is being
381 // discarded. This issues warnings about computed values being
382 // unused. This returns true if all is well, false if it issued an
383 // error message.
384 bool
385 discarding_value()
386 { return this->do_discarding_value(); }
388 // Return whether this is an error expression.
389 bool
390 is_error_expression() const
391 { return this->classification_ == EXPRESSION_ERROR; }
393 // Return whether this expression really represents a type.
394 bool
395 is_type_expression() const
396 { return this->classification_ == EXPRESSION_TYPE; }
398 // If this is a variable reference, return the Var_expression
399 // structure. Otherwise, return NULL. This is a controlled dynamic
400 // cast.
401 Var_expression*
402 var_expression()
403 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
405 const Var_expression*
406 var_expression() const
407 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
409 // If this is a reference to a temporary variable, return the
410 // Temporary_reference_expression. Otherwise, return NULL.
411 Temporary_reference_expression*
412 temporary_reference_expression()
414 return this->convert<Temporary_reference_expression,
415 EXPRESSION_TEMPORARY_REFERENCE>();
418 // If this is a set-and-use-temporary, return the
419 // Set_and_use_temporary_expression. Otherwise, return NULL.
420 Set_and_use_temporary_expression*
421 set_and_use_temporary_expression()
423 return this->convert<Set_and_use_temporary_expression,
424 EXPRESSION_SET_AND_USE_TEMPORARY>();
427 // Return whether this is a sink expression.
428 bool
429 is_sink_expression() const
430 { return this->classification_ == EXPRESSION_SINK; }
432 // If this is a string expression, return the String_expression
433 // structure. Otherwise, return NULL.
434 String_expression*
435 string_expression()
436 { return this->convert<String_expression, EXPRESSION_STRING>(); }
438 // Return whether this is the expression nil.
439 bool
440 is_nil_expression() const
441 { return this->classification_ == EXPRESSION_NIL; }
443 // If this is an indirection through a pointer, return the
444 // expression being pointed through. Otherwise return this.
445 Expression*
446 deref();
448 // If this is a binary expression, return the Binary_expression
449 // structure. Otherwise return NULL.
450 Binary_expression*
451 binary_expression()
452 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
454 // If this is a call expression, return the Call_expression
455 // structure. Otherwise, return NULL. This is a controlled dynamic
456 // cast.
457 Call_expression*
458 call_expression()
459 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
461 // If this is an expression which refers to a function, return the
462 // Func_expression structure. Otherwise, return NULL.
463 Func_expression*
464 func_expression()
465 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
467 const Func_expression*
468 func_expression() const
469 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
471 // If this is an expression which refers to an unknown name, return
472 // the Unknown_expression structure. Otherwise, return NULL.
473 Unknown_expression*
474 unknown_expression()
475 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
477 const Unknown_expression*
478 unknown_expression() const
480 return this->convert<const Unknown_expression,
481 EXPRESSION_UNKNOWN_REFERENCE>();
484 // If this is an index expression, return the Index_expression
485 // structure. Otherwise, return NULL.
486 Index_expression*
487 index_expression()
488 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
490 // If this is an expression which refers to indexing in a map,
491 // return the Map_index_expression structure. Otherwise, return
492 // NULL.
493 Map_index_expression*
494 map_index_expression()
495 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
497 // If this is a bound method expression, return the
498 // Bound_method_expression structure. Otherwise, return NULL.
499 Bound_method_expression*
500 bound_method_expression()
501 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
503 // If this is a reference to a field in a struct, return the
504 // Field_reference_expression structure. Otherwise, return NULL.
505 Field_reference_expression*
506 field_reference_expression()
508 return this->convert<Field_reference_expression,
509 EXPRESSION_FIELD_REFERENCE>();
512 // If this is a reference to a field in an interface, return the
513 // Interface_field_reference_expression structure. Otherwise,
514 // return NULL.
515 Interface_field_reference_expression*
516 interface_field_reference_expression()
518 return this->convert<Interface_field_reference_expression,
519 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
522 // If this is a type guard expression, return the
523 // Type_guard_expression structure. Otherwise, return NULL.
524 Type_guard_expression*
525 type_guard_expression()
526 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
528 // If this is a receive expression, return the Receive_expression
529 // structure. Otherwise, return NULL.
530 Receive_expression*
531 receive_expression()
532 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
534 // Return true if this is a composite literal.
535 bool
536 is_composite_literal() const;
538 // Return true if this is a composite literal which is not constant.
539 bool
540 is_nonconstant_composite_literal() const;
542 // Return true if this is a reference to a local variable.
543 bool
544 is_local_variable() const;
546 // Make the builtin function descriptor type, so that it can be
547 // converted.
548 static void
549 make_func_descriptor_type();
551 // Traverse an expression.
552 static int
553 traverse(Expression**, Traverse*);
555 // Traverse subexpressions of this expression.
557 traverse_subexpressions(Traverse*);
559 // Lower an expression. This is called immediately after parsing.
560 // FUNCTION is the function we are in; it will be NULL for an
561 // expression initializing a global variable. INSERTER may be used
562 // to insert statements before the statement or initializer
563 // containing this expression; it is normally used to create
564 // temporary variables. IOTA_VALUE is the value that we should give
565 // to any iota expressions. This function must resolve expressions
566 // which could not be fully parsed into their final form. It
567 // returns the same Expression or a new one.
568 Expression*
569 lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
570 int iota_value)
571 { return this->do_lower(gogo, function, inserter, iota_value); }
573 // Determine the real type of an expression with abstract integer,
574 // floating point, or complex type. TYPE_CONTEXT describes the
575 // expected type.
576 void
577 determine_type(const Type_context*);
579 // Check types in an expression.
580 void
581 check_types(Gogo* gogo)
582 { this->do_check_types(gogo); }
584 // Determine the type when there is no context.
585 void
586 determine_type_no_context();
588 // Return the current type of the expression. This may be changed
589 // by determine_type.
590 Type*
591 type()
592 { return this->do_type(); }
594 // Return a copy of an expression.
595 Expression*
596 copy()
597 { return this->do_copy(); }
599 // Return whether the expression is addressable--something which may
600 // be used as the operand of the unary & operator.
601 bool
602 is_addressable() const
603 { return this->do_is_addressable(); }
605 // Note that we are taking the address of this expression. ESCAPES
606 // is true if this address escapes the current function.
607 void
608 address_taken(bool escapes)
609 { this->do_address_taken(escapes); }
611 // Return whether this expression must be evaluated in order
612 // according to the order of evaluation rules. This is basically
613 // true of all expressions with side-effects.
614 bool
615 must_eval_in_order() const
616 { return this->do_must_eval_in_order(); }
618 // Return whether subexpressions of this expression must be
619 // evaluated in order. This is true of index expressions and
620 // pointer indirections. This sets *SKIP to the number of
621 // subexpressions to skip during traversing, as index expressions
622 // only requiring moving the index, not the array.
623 bool
624 must_eval_subexpressions_in_order(int* skip) const
626 *skip = 0;
627 return this->do_must_eval_subexpressions_in_order(skip);
630 // Return the tree for this expression.
631 tree
632 get_tree(Translate_context*);
634 // Return a tree handling any conversions which must be done during
635 // assignment.
636 static tree
637 convert_for_assignment(Translate_context*, Type* lhs_type, Type* rhs_type,
638 tree rhs_tree, Location location);
640 // Return a tree converting a value of one interface type to another
641 // interface type. If FOR_TYPE_GUARD is true this is for a type
642 // assertion.
643 static tree
644 convert_interface_to_interface(Translate_context*, Type* lhs_type,
645 Type* rhs_type, tree rhs_tree,
646 bool for_type_guard, Location);
648 // Return a tree implementing the comparison LHS_TREE OP RHS_TREE.
649 // TYPE is the type of both sides.
650 static tree
651 comparison_tree(Translate_context*, Type* result_type, Operator op,
652 Type* left_type, tree left_tree, Type* right_type,
653 tree right_tree, Location);
655 // Return a tree for the multi-precision integer VAL in TYPE.
656 static tree
657 integer_constant_tree(mpz_t val, tree type);
659 // Return a tree for the floating point value VAL in TYPE.
660 static tree
661 float_constant_tree(mpfr_t val, tree type);
663 // Return a tree for the complex value REAL/IMAG in TYPE.
664 static tree
665 complex_constant_tree(mpfr_t real, mpfr_t imag, tree type);
667 // Export the expression. This is only used for constants. It will
668 // be used for things like values of named constants and sizes of
669 // arrays.
670 void
671 export_expression(Export* exp) const
672 { this->do_export(exp); }
674 // Import an expression.
675 static Expression*
676 import_expression(Import*);
678 // Return a tree which checks that VAL, of arbitrary integer type,
679 // is non-negative and is not more than the maximum value of
680 // BOUND_TYPE. If SOFAR is not NULL, it is or'red into the result.
681 // The return value may be NULL if SOFAR is NULL.
682 static tree
683 check_bounds(tree val, tree bound_type, tree sofar, Location);
685 // Dump an expression to a dump constext.
686 void
687 dump_expression(Ast_dump_context*) const;
689 protected:
690 // May be implemented by child class: traverse the expressions.
691 virtual int
692 do_traverse(Traverse*);
694 // Return a lowered expression.
695 virtual Expression*
696 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
697 { return this; }
699 // Return whether this is a constant expression.
700 virtual bool
701 do_is_constant() const
702 { return false; }
704 // Return whether this is a constant expression of numeric type, and
705 // set the Numeric_constant to the value.
706 virtual bool
707 do_numeric_constant_value(Numeric_constant*) const
708 { return false; }
710 // Return whether this is a constant expression of string type, and
711 // set VAL to the value.
712 virtual bool
713 do_string_constant_value(std::string*) const
714 { return false; }
716 // Called by the parser if the value is being discarded.
717 virtual bool
718 do_discarding_value();
720 // Child class holds type.
721 virtual Type*
722 do_type() = 0;
724 // Child class implements determining type information.
725 virtual void
726 do_determine_type(const Type_context*) = 0;
728 // Child class implements type checking if needed.
729 virtual void
730 do_check_types(Gogo*)
733 // Child class implements copying.
734 virtual Expression*
735 do_copy() = 0;
737 // Child class implements whether the expression is addressable.
738 virtual bool
739 do_is_addressable() const
740 { return false; }
742 // Child class implements taking the address of an expression.
743 virtual void
744 do_address_taken(bool)
747 // Child class implements whether this expression must be evaluated
748 // in order.
749 virtual bool
750 do_must_eval_in_order() const
751 { return false; }
753 // Child class implements whether this expressions requires that
754 // subexpressions be evaluated in order. The child implementation
755 // may set *SKIP if it should be non-zero.
756 virtual bool
757 do_must_eval_subexpressions_in_order(int* /* skip */) const
758 { return false; }
760 // Child class implements conversion to tree.
761 virtual tree
762 do_get_tree(Translate_context*) = 0;
764 // Child class implements export.
765 virtual void
766 do_export(Export*) const;
768 // For children to call to give an error for an unused value.
769 void
770 unused_value_error();
772 // For children to call when they detect that they are in error.
773 void
774 set_is_error();
776 // For children to call to report an error conveniently.
777 void
778 report_error(const char*);
780 // Child class implements dumping to a dump context.
781 virtual void
782 do_dump_expression(Ast_dump_context*) const = 0;
784 private:
785 // Convert to the desired statement classification, or return NULL.
786 // This is a controlled dynamic cast.
787 template<typename Expression_class,
788 Expression_classification expr_classification>
789 Expression_class*
790 convert()
792 return (this->classification_ == expr_classification
793 ? static_cast<Expression_class*>(this)
794 : NULL);
797 template<typename Expression_class,
798 Expression_classification expr_classification>
799 const Expression_class*
800 convert() const
802 return (this->classification_ == expr_classification
803 ? static_cast<const Expression_class*>(this)
804 : NULL);
807 static tree
808 convert_type_to_interface(Translate_context*, Type*, Type*, tree,
809 Location);
811 static tree
812 get_interface_type_descriptor(Translate_context*, Type*, tree,
813 Location);
815 static tree
816 convert_interface_to_type(Translate_context*, Type*, Type*, tree,
817 Location);
819 // The expression classification.
820 Expression_classification classification_;
821 // The location in the input file.
822 Location location_;
825 // A list of Expressions.
827 class Expression_list
829 public:
830 Expression_list()
831 : entries_()
834 // Return whether the list is empty.
835 bool
836 empty() const
837 { return this->entries_.empty(); }
839 // Return the number of entries in the list.
840 size_t
841 size() const
842 { return this->entries_.size(); }
844 // Add an entry to the end of the list.
845 void
846 push_back(Expression* expr)
847 { this->entries_.push_back(expr); }
849 void
850 append(Expression_list* add)
851 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
853 // Reserve space in the list.
854 void
855 reserve(size_t size)
856 { this->entries_.reserve(size); }
858 // Traverse the expressions in the list.
860 traverse(Traverse*);
862 // Copy the list.
863 Expression_list*
864 copy();
866 // Return true if the list contains an error expression.
867 bool
868 contains_error() const;
870 // Retrieve an element by index.
871 Expression*&
872 at(size_t i)
873 { return this->entries_.at(i); }
875 // Return the first and last elements.
876 Expression*&
877 front()
878 { return this->entries_.front(); }
880 Expression*
881 front() const
882 { return this->entries_.front(); }
884 Expression*&
885 back()
886 { return this->entries_.back(); }
888 Expression*
889 back() const
890 { return this->entries_.back(); }
892 // Iterators.
894 typedef std::vector<Expression*>::iterator iterator;
895 typedef std::vector<Expression*>::const_iterator const_iterator;
897 iterator
898 begin()
899 { return this->entries_.begin(); }
901 const_iterator
902 begin() const
903 { return this->entries_.begin(); }
905 iterator
906 end()
907 { return this->entries_.end(); }
909 const_iterator
910 end() const
911 { return this->entries_.end(); }
913 // Erase an entry.
914 void
915 erase(iterator p)
916 { this->entries_.erase(p); }
918 private:
919 std::vector<Expression*> entries_;
922 // An abstract base class for an expression which is only used by the
923 // parser, and is lowered in the lowering pass.
925 class Parser_expression : public Expression
927 public:
928 Parser_expression(Expression_classification classification,
929 Location location)
930 : Expression(classification, location)
933 protected:
934 virtual Expression*
935 do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
937 Type*
938 do_type();
940 void
941 do_determine_type(const Type_context*)
942 { go_unreachable(); }
944 void
945 do_check_types(Gogo*)
946 { go_unreachable(); }
948 tree
949 do_get_tree(Translate_context*)
950 { go_unreachable(); }
953 // An expression which is simply a variable.
955 class Var_expression : public Expression
957 public:
958 Var_expression(Named_object* variable, Location location)
959 : Expression(EXPRESSION_VAR_REFERENCE, location),
960 variable_(variable)
963 // Return the variable.
964 Named_object*
965 named_object() const
966 { return this->variable_; }
968 protected:
969 Expression*
970 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
972 Type*
973 do_type();
975 void
976 do_determine_type(const Type_context*);
978 Expression*
979 do_copy()
980 { return this; }
982 bool
983 do_is_addressable() const
984 { return true; }
986 void
987 do_address_taken(bool);
989 tree
990 do_get_tree(Translate_context*);
992 void
993 do_dump_expression(Ast_dump_context*) const;
995 private:
996 // The variable we are referencing.
997 Named_object* variable_;
1000 // A reference to a temporary variable.
1002 class Temporary_reference_expression : public Expression
1004 public:
1005 Temporary_reference_expression(Temporary_statement* statement,
1006 Location location)
1007 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1008 statement_(statement), is_lvalue_(false)
1011 // The temporary that this expression refers to.
1012 Temporary_statement*
1013 statement() const
1014 { return this->statement_; }
1016 // Indicate that this reference appears on the left hand side of an
1017 // assignment statement.
1018 void
1019 set_is_lvalue()
1020 { this->is_lvalue_ = true; }
1022 protected:
1023 Type*
1024 do_type();
1026 void
1027 do_determine_type(const Type_context*)
1030 Expression*
1031 do_copy()
1032 { return make_temporary_reference(this->statement_, this->location()); }
1034 bool
1035 do_is_addressable() const
1036 { return true; }
1038 void
1039 do_address_taken(bool);
1041 tree
1042 do_get_tree(Translate_context*);
1044 void
1045 do_dump_expression(Ast_dump_context*) const;
1047 private:
1048 // The statement where the temporary variable is defined.
1049 Temporary_statement* statement_;
1050 // Whether this reference appears on the left hand side of an
1051 // assignment statement.
1052 bool is_lvalue_;
1055 // Set and use a temporary variable.
1057 class Set_and_use_temporary_expression : public Expression
1059 public:
1060 Set_and_use_temporary_expression(Temporary_statement* statement,
1061 Expression* expr, Location location)
1062 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1063 statement_(statement), expr_(expr)
1066 // Return the temporary.
1067 Temporary_statement*
1068 temporary() const
1069 { return this->statement_; }
1071 // Return the expression.
1072 Expression*
1073 expression() const
1074 { return this->expr_; }
1076 protected:
1078 do_traverse(Traverse* traverse)
1079 { return Expression::traverse(&this->expr_, traverse); }
1081 Type*
1082 do_type();
1084 void
1085 do_determine_type(const Type_context*);
1087 Expression*
1088 do_copy()
1090 return make_set_and_use_temporary(this->statement_, this->expr_,
1091 this->location());
1094 bool
1095 do_is_addressable() const
1096 { return true; }
1098 void
1099 do_address_taken(bool);
1101 tree
1102 do_get_tree(Translate_context*);
1104 void
1105 do_dump_expression(Ast_dump_context*) const;
1107 private:
1108 // The statement where the temporary variable is defined.
1109 Temporary_statement* statement_;
1110 // The expression to assign to the temporary.
1111 Expression* expr_;
1114 // A string expression.
1116 class String_expression : public Expression
1118 public:
1119 String_expression(const std::string& val, Location location)
1120 : Expression(EXPRESSION_STRING, location),
1121 val_(val), type_(NULL)
1124 const std::string&
1125 val() const
1126 { return this->val_; }
1128 static Expression*
1129 do_import(Import*);
1131 protected:
1132 bool
1133 do_is_constant() const
1134 { return true; }
1136 bool
1137 do_string_constant_value(std::string* val) const
1139 *val = this->val_;
1140 return true;
1143 Type*
1144 do_type();
1146 void
1147 do_determine_type(const Type_context*);
1149 Expression*
1150 do_copy()
1151 { return this; }
1153 tree
1154 do_get_tree(Translate_context*);
1156 // Write string literal to a string dump.
1157 static void
1158 export_string(String_dump* exp, const String_expression* str);
1160 void
1161 do_export(Export*) const;
1163 void
1164 do_dump_expression(Ast_dump_context*) const;
1166 private:
1167 // The string value. This is immutable.
1168 const std::string val_;
1169 // The type as determined by context.
1170 Type* type_;
1173 // A binary expression.
1175 class Binary_expression : public Expression
1177 public:
1178 Binary_expression(Operator op, Expression* left, Expression* right,
1179 Location location)
1180 : Expression(EXPRESSION_BINARY, location),
1181 op_(op), left_(left), right_(right), type_(NULL)
1184 // Return the operator.
1185 Operator
1186 op()
1187 { return this->op_; }
1189 // Return the left hand expression.
1190 Expression*
1191 left()
1192 { return this->left_; }
1194 // Return the right hand expression.
1195 Expression*
1196 right()
1197 { return this->right_; }
1199 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1200 // Return true if this could be done, false if not. Issue errors at
1201 // LOCATION as appropriate.
1202 static bool
1203 eval_constant(Operator op, Numeric_constant* left_nc,
1204 Numeric_constant* right_nc, Location location,
1205 Numeric_constant* nc);
1207 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1208 // *RESULT. Return true if this could be done, false if not. Issue
1209 // errors at LOCATION as appropriate.
1210 static bool
1211 compare_constant(Operator op, Numeric_constant* left_nc,
1212 Numeric_constant* right_nc, Location location,
1213 bool* result);
1215 static Expression*
1216 do_import(Import*);
1218 // Report an error if OP can not be applied to TYPE. Return whether
1219 // it can. OTYPE is the type of the other operand.
1220 static bool
1221 check_operator_type(Operator op, Type* type, Type* otype, Location);
1223 protected:
1225 do_traverse(Traverse* traverse);
1227 Expression*
1228 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1230 bool
1231 do_is_constant() const
1232 { return this->left_->is_constant() && this->right_->is_constant(); }
1234 bool
1235 do_numeric_constant_value(Numeric_constant*) const;
1237 bool
1238 do_discarding_value();
1240 Type*
1241 do_type();
1243 void
1244 do_determine_type(const Type_context*);
1246 void
1247 do_check_types(Gogo*);
1249 Expression*
1250 do_copy()
1252 return Expression::make_binary(this->op_, this->left_->copy(),
1253 this->right_->copy(), this->location());
1256 tree
1257 do_get_tree(Translate_context*);
1259 void
1260 do_export(Export*) const;
1262 void
1263 do_dump_expression(Ast_dump_context*) const;
1265 private:
1266 static bool
1267 operation_type(Operator op, Type* left_type, Type* right_type,
1268 Type** result_type);
1270 static bool
1271 cmp_to_bool(Operator op, int cmp);
1273 static bool
1274 eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
1275 Location, Numeric_constant*);
1277 static bool
1278 eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
1279 Location, Numeric_constant*);
1281 static bool
1282 eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
1283 Location, Numeric_constant*);
1285 static bool
1286 compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
1288 static bool
1289 compare_float(const Numeric_constant*, const Numeric_constant *, int*);
1291 static bool
1292 compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
1294 Expression*
1295 lower_struct_comparison(Gogo*, Statement_inserter*);
1297 Expression*
1298 lower_array_comparison(Gogo*, Statement_inserter*);
1300 Expression*
1301 lower_compare_to_memcmp(Gogo*, Statement_inserter*);
1303 Expression*
1304 operand_address(Statement_inserter*, Expression*);
1306 // The binary operator to apply.
1307 Operator op_;
1308 // The left hand side operand.
1309 Expression* left_;
1310 // The right hand side operand.
1311 Expression* right_;
1312 // The type of a comparison operation.
1313 Type* type_;
1316 // A call expression. The go statement needs to dig inside this.
1318 class Call_expression : public Expression
1320 public:
1321 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
1322 Location location)
1323 : Expression(EXPRESSION_CALL, location),
1324 fn_(fn), args_(args), type_(NULL), results_(NULL), tree_(NULL),
1325 is_varargs_(is_varargs), are_hidden_fields_ok_(false),
1326 varargs_are_lowered_(false), types_are_determined_(false),
1327 is_deferred_(false), issued_error_(false)
1330 // The function to call.
1331 Expression*
1332 fn() const
1333 { return this->fn_; }
1335 // The arguments.
1336 Expression_list*
1337 args()
1338 { return this->args_; }
1340 const Expression_list*
1341 args() const
1342 { return this->args_; }
1344 // Get the function type.
1345 Function_type*
1346 get_function_type() const;
1348 // Return the number of values this call will return.
1349 size_t
1350 result_count() const;
1352 // Return the temporary variable which holds result I. This is only
1353 // valid after the expression has been lowered, and is only valid
1354 // for calls which return multiple results.
1355 Temporary_statement*
1356 result(size_t i) const;
1358 // Return whether this is a call to the predeclared function
1359 // recover.
1360 bool
1361 is_recover_call() const;
1363 // Set the argument for a call to recover.
1364 void
1365 set_recover_arg(Expression*);
1367 // Whether the last argument is a varargs argument (f(a...)).
1368 bool
1369 is_varargs() const
1370 { return this->is_varargs_; }
1372 // Note that varargs have already been lowered.
1373 void
1374 set_varargs_are_lowered()
1375 { this->varargs_are_lowered_ = true; }
1377 // Note that it is OK for this call to set hidden fields when
1378 // passing arguments.
1379 void
1380 set_hidden_fields_are_ok()
1381 { this->are_hidden_fields_ok_ = true; }
1383 // Whether this call is being deferred.
1384 bool
1385 is_deferred() const
1386 { return this->is_deferred_; }
1388 // Note that the call is being deferred.
1389 void
1390 set_is_deferred()
1391 { this->is_deferred_ = true; }
1393 // We have found an error with this call expression; return true if
1394 // we should report it.
1395 bool
1396 issue_error();
1398 protected:
1400 do_traverse(Traverse*);
1402 virtual Expression*
1403 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1405 bool
1406 do_discarding_value()
1407 { return true; }
1409 virtual Type*
1410 do_type();
1412 virtual void
1413 do_determine_type(const Type_context*);
1415 virtual void
1416 do_check_types(Gogo*);
1418 Expression*
1419 do_copy()
1421 return Expression::make_call(this->fn_->copy(),
1422 (this->args_ == NULL
1423 ? NULL
1424 : this->args_->copy()),
1425 this->is_varargs_, this->location());
1428 bool
1429 do_must_eval_in_order() const;
1431 virtual tree
1432 do_get_tree(Translate_context*);
1434 virtual bool
1435 do_is_recover_call() const;
1437 virtual void
1438 do_set_recover_arg(Expression*);
1440 // Let a builtin expression change the argument list.
1441 void
1442 set_args(Expression_list* args)
1443 { this->args_ = args; }
1445 // Let a builtin expression lower varargs.
1446 void
1447 lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
1448 Type* varargs_type, size_t param_count);
1450 // Let a builtin expression check whether types have been
1451 // determined.
1452 bool
1453 determining_types();
1455 void
1456 do_dump_expression(Ast_dump_context*) const;
1458 private:
1459 bool
1460 check_argument_type(int, const Type*, const Type*, Location, bool);
1462 tree
1463 interface_method_function(Translate_context*,
1464 Interface_field_reference_expression*,
1465 tree*);
1467 tree
1468 set_results(Translate_context*, tree);
1470 // The function to call.
1471 Expression* fn_;
1472 // The arguments to pass. This may be NULL if there are no
1473 // arguments.
1474 Expression_list* args_;
1475 // The type of the expression, to avoid recomputing it.
1476 Type* type_;
1477 // The list of temporaries which will hold the results if the
1478 // function returns a tuple.
1479 std::vector<Temporary_statement*>* results_;
1480 // The tree for the call, used for a call which returns a tuple.
1481 tree tree_;
1482 // True if the last argument is a varargs argument (f(a...)).
1483 bool is_varargs_;
1484 // True if this statement may pass hidden fields in the arguments.
1485 // This is used for generated method stubs.
1486 bool are_hidden_fields_ok_;
1487 // True if varargs have already been lowered.
1488 bool varargs_are_lowered_;
1489 // True if types have been determined.
1490 bool types_are_determined_;
1491 // True if the call is an argument to a defer statement.
1492 bool is_deferred_;
1493 // True if we reported an error about a mismatch between call
1494 // results and uses. This is to avoid producing multiple errors
1495 // when there are multiple Call_result_expressions.
1496 bool issued_error_;
1499 // An expression which represents a pointer to a function.
1501 class Func_expression : public Expression
1503 public:
1504 Func_expression(Named_object* function, Expression* closure,
1505 Location location)
1506 : Expression(EXPRESSION_FUNC_REFERENCE, location),
1507 function_(function), closure_(closure)
1510 // Return the object associated with the function.
1511 Named_object*
1512 named_object() const
1513 { return this->function_; }
1515 // Return the closure for this function. This will return NULL if
1516 // the function has no closure, which is the normal case.
1517 Expression*
1518 closure()
1519 { return this->closure_; }
1521 // Return a tree for the code for a function.
1522 static tree
1523 get_code_pointer(Gogo*, Named_object* function, Location loc);
1525 protected:
1527 do_traverse(Traverse*);
1529 Type*
1530 do_type();
1532 void
1533 do_determine_type(const Type_context*)
1535 if (this->closure_ != NULL)
1536 this->closure_->determine_type_no_context();
1539 Expression*
1540 do_copy()
1542 return Expression::make_func_reference(this->function_,
1543 (this->closure_ == NULL
1544 ? NULL
1545 : this->closure_->copy()),
1546 this->location());
1549 tree
1550 do_get_tree(Translate_context*);
1552 void
1553 do_dump_expression(Ast_dump_context*) const;
1555 private:
1556 // The function itself.
1557 Named_object* function_;
1558 // A closure. This is normally NULL. For a nested function, it may
1559 // be a struct holding pointers to all the variables referenced by
1560 // this function and defined in enclosing functions.
1561 Expression* closure_;
1564 // A function descriptor. A function descriptor is a struct with a
1565 // single field pointing to the function code. This is used for
1566 // functions without closures.
1568 class Func_descriptor_expression : public Expression
1570 public:
1571 Func_descriptor_expression(Named_object* fn);
1573 // Set the descriptor wrapper.
1574 void
1575 set_descriptor_wrapper(Named_object* dfn)
1577 go_assert(this->dfn_ == NULL);
1578 this->dfn_ = dfn;
1581 // Make the function descriptor type, so that it can be converted.
1582 static void
1583 make_func_descriptor_type();
1585 protected:
1587 do_traverse(Traverse*);
1589 Type*
1590 do_type();
1592 void
1593 do_determine_type(const Type_context*)
1596 Expression*
1597 do_copy();
1599 bool
1600 do_is_addressable() const
1601 { return true; }
1603 tree
1604 do_get_tree(Translate_context*);
1606 void
1607 do_dump_expression(Ast_dump_context* context) const;
1609 private:
1610 // The type of all function descriptors.
1611 static Type* descriptor_type;
1613 // The function for which this is the descriptor.
1614 Named_object* fn_;
1615 // The descriptor function.
1616 Named_object* dfn_;
1617 // The descriptor variable.
1618 Bvariable* dvar_;
1621 // A reference to an unknown name.
1623 class Unknown_expression : public Parser_expression
1625 public:
1626 Unknown_expression(Named_object* named_object, Location location)
1627 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
1628 named_object_(named_object), no_error_message_(false),
1629 is_composite_literal_key_(false)
1632 // The associated named object.
1633 Named_object*
1634 named_object() const
1635 { return this->named_object_; }
1637 // The name of the identifier which was unknown.
1638 const std::string&
1639 name() const;
1641 // Call this to indicate that we should not give an error if this
1642 // name is never defined. This is used to avoid knock-on errors
1643 // during an erroneous parse.
1644 void
1645 set_no_error_message()
1646 { this->no_error_message_ = true; }
1648 // Note that this expression is being used as the key in a composite
1649 // literal, so it may be OK if it is not resolved.
1650 void
1651 set_is_composite_literal_key()
1652 { this->is_composite_literal_key_ = true; }
1654 // Note that this expression should no longer be treated as a
1655 // composite literal key.
1656 void
1657 clear_is_composite_literal_key()
1658 { this->is_composite_literal_key_ = false; }
1660 protected:
1661 Expression*
1662 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1664 Expression*
1665 do_copy()
1666 { return new Unknown_expression(this->named_object_, this->location()); }
1668 void
1669 do_dump_expression(Ast_dump_context*) const;
1671 private:
1672 // The unknown name.
1673 Named_object* named_object_;
1674 // True if we should not give errors if this is undefined. This is
1675 // used if there was a parse failure.
1676 bool no_error_message_;
1677 // True if this is the key in a composite literal.
1678 bool is_composite_literal_key_;
1681 // An index expression. This is lowered to an array index, a string
1682 // index, or a map index.
1684 class Index_expression : public Parser_expression
1686 public:
1687 Index_expression(Expression* left, Expression* start, Expression* end,
1688 Location location)
1689 : Parser_expression(EXPRESSION_INDEX, location),
1690 left_(left), start_(start), end_(end), is_lvalue_(false)
1693 // Record that this expression is an lvalue.
1694 void
1695 set_is_lvalue()
1696 { this->is_lvalue_ = true; }
1698 // Dump an index expression, i.e. an expression of the form
1699 // expr[expr] or expr[expr:expr], to a dump context.
1700 static void
1701 dump_index_expression(Ast_dump_context*, const Expression* expr,
1702 const Expression* start, const Expression* end);
1704 protected:
1706 do_traverse(Traverse*);
1708 Expression*
1709 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1711 Expression*
1712 do_copy()
1714 return new Index_expression(this->left_->copy(), this->start_->copy(),
1715 (this->end_ == NULL
1716 ? NULL
1717 : this->end_->copy()),
1718 this->location());
1721 bool
1722 do_must_eval_subexpressions_in_order(int* skip) const
1724 *skip = 1;
1725 return true;
1728 void
1729 do_dump_expression(Ast_dump_context*) const;
1731 private:
1732 // The expression being indexed.
1733 Expression* left_;
1734 // The first index.
1735 Expression* start_;
1736 // The second index. This is NULL for an index, non-NULL for a
1737 // slice.
1738 Expression* end_;
1739 // Whether this is being used as an l-value. We set this during the
1740 // parse because map index expressions need to know.
1741 bool is_lvalue_;
1744 // An index into a map.
1746 class Map_index_expression : public Expression
1748 public:
1749 Map_index_expression(Expression* map, Expression* index,
1750 Location location)
1751 : Expression(EXPRESSION_MAP_INDEX, location),
1752 map_(map), index_(index), is_lvalue_(false),
1753 is_in_tuple_assignment_(false)
1756 // Return the map.
1757 Expression*
1758 map()
1759 { return this->map_; }
1761 const Expression*
1762 map() const
1763 { return this->map_; }
1765 // Return the index.
1766 Expression*
1767 index()
1768 { return this->index_; }
1770 const Expression*
1771 index() const
1772 { return this->index_; }
1774 // Get the type of the map being indexed.
1775 Map_type*
1776 get_map_type() const;
1778 // Record that this map expression is an lvalue. The difference is
1779 // that an lvalue always inserts the key.
1780 void
1781 set_is_lvalue()
1782 { this->is_lvalue_ = true; }
1784 // Return whether this map expression occurs in an assignment to a
1785 // pair of values.
1786 bool
1787 is_in_tuple_assignment() const
1788 { return this->is_in_tuple_assignment_; }
1790 // Record that this map expression occurs in an assignment to a pair
1791 // of values.
1792 void
1793 set_is_in_tuple_assignment()
1794 { this->is_in_tuple_assignment_ = true; }
1796 // Return a tree for the map index. This returns a tree which
1797 // evaluates to a pointer to a value in the map. If INSERT is true,
1798 // the key will be inserted if not present, and the value pointer
1799 // will be zero initialized. If INSERT is false, and the key is not
1800 // present in the map, the pointer will be NULL.
1801 tree
1802 get_value_pointer(Translate_context*, bool insert);
1804 protected:
1806 do_traverse(Traverse*);
1808 Type*
1809 do_type();
1811 void
1812 do_determine_type(const Type_context*);
1814 void
1815 do_check_types(Gogo*);
1817 Expression*
1818 do_copy()
1820 return Expression::make_map_index(this->map_->copy(),
1821 this->index_->copy(),
1822 this->location());
1825 bool
1826 do_must_eval_subexpressions_in_order(int* skip) const
1828 *skip = 1;
1829 return true;
1832 // A map index expression is an lvalue but it is not addressable.
1834 tree
1835 do_get_tree(Translate_context*);
1837 void
1838 do_dump_expression(Ast_dump_context*) const;
1840 private:
1841 // The map we are looking into.
1842 Expression* map_;
1843 // The index.
1844 Expression* index_;
1845 // Whether this is an lvalue.
1846 bool is_lvalue_;
1847 // Whether this is in a tuple assignment to a pair of values.
1848 bool is_in_tuple_assignment_;
1851 // An expression which represents a method bound to its first
1852 // argument.
1854 class Bound_method_expression : public Expression
1856 public:
1857 Bound_method_expression(Expression* expr, const Method *method,
1858 Named_object* function, Location location)
1859 : Expression(EXPRESSION_BOUND_METHOD, location),
1860 expr_(expr), expr_type_(NULL), method_(method), function_(function)
1863 // Return the object which is the first argument.
1864 Expression*
1865 first_argument()
1866 { return this->expr_; }
1868 // Return the implicit type of the first argument. This will be
1869 // non-NULL when using a method from an anonymous field without
1870 // using an explicit stub.
1871 Type*
1872 first_argument_type() const
1873 { return this->expr_type_; }
1875 // Return the method.
1876 const Method*
1877 method() const
1878 { return this->method_; }
1880 // Return the function to call.
1881 Named_object*
1882 function() const
1883 { return this->function_; }
1885 // Set the implicit type of the expression.
1886 void
1887 set_first_argument_type(Type* type)
1888 { this->expr_type_ = type; }
1890 // Create a thunk to call FUNCTION, for METHOD, when it is used as
1891 // part of a method value.
1892 static Named_object*
1893 create_thunk(Gogo*, const Method* method, Named_object* function);
1895 protected:
1897 do_traverse(Traverse*);
1899 Expression*
1900 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1902 Type*
1903 do_type();
1905 void
1906 do_determine_type(const Type_context*);
1908 void
1909 do_check_types(Gogo*);
1911 Expression*
1912 do_copy()
1914 return new Bound_method_expression(this->expr_->copy(), this->method_,
1915 this->function_, this->location());
1918 tree
1919 do_get_tree(Translate_context*);
1921 void
1922 do_dump_expression(Ast_dump_context*) const;
1924 private:
1925 // A mapping from method functions to the thunks we have created for
1926 // them.
1927 typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
1928 static Method_value_thunks method_value_thunks;
1930 // The object used to find the method. This is passed to the method
1931 // as the first argument.
1932 Expression* expr_;
1933 // The implicit type of the object to pass to the method. This is
1934 // NULL in the normal case, non-NULL when using a method from an
1935 // anonymous field which does not require a stub.
1936 Type* expr_type_;
1937 // The method.
1938 const Method* method_;
1939 // The function to call. This is not the same as
1940 // method_->named_object() when the method has a stub. This will be
1941 // the real function rather than the stub.
1942 Named_object* function_;
1945 // A reference to a field in a struct.
1947 class Field_reference_expression : public Expression
1949 public:
1950 Field_reference_expression(Expression* expr, unsigned int field_index,
1951 Location location)
1952 : Expression(EXPRESSION_FIELD_REFERENCE, location),
1953 expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
1956 // Return the struct expression.
1957 Expression*
1958 expr() const
1959 { return this->expr_; }
1961 // Return the field index.
1962 unsigned int
1963 field_index() const
1964 { return this->field_index_; }
1966 // Return whether this node was implied by an anonymous field.
1967 bool
1968 implicit() const
1969 { return this->implicit_; }
1971 void
1972 set_implicit(bool implicit)
1973 { this->implicit_ = implicit; }
1975 // Set the struct expression. This is used when parsing.
1976 void
1977 set_struct_expression(Expression* expr)
1979 go_assert(this->expr_ == NULL);
1980 this->expr_ = expr;
1983 protected:
1985 do_traverse(Traverse* traverse)
1986 { return Expression::traverse(&this->expr_, traverse); }
1988 Expression*
1989 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1991 Type*
1992 do_type();
1994 void
1995 do_determine_type(const Type_context*)
1996 { this->expr_->determine_type_no_context(); }
1998 void
1999 do_check_types(Gogo*);
2001 Expression*
2002 do_copy()
2004 return Expression::make_field_reference(this->expr_->copy(),
2005 this->field_index_,
2006 this->location());
2009 bool
2010 do_is_addressable() const
2011 { return this->expr_->is_addressable(); }
2013 void
2014 do_address_taken(bool escapes)
2015 { this->expr_->address_taken(escapes); }
2017 tree
2018 do_get_tree(Translate_context*);
2020 void
2021 do_dump_expression(Ast_dump_context*) const;
2023 private:
2024 // The expression we are looking into. This should have a type of
2025 // struct.
2026 Expression* expr_;
2027 // The zero-based index of the field we are retrieving.
2028 unsigned int field_index_;
2029 // Whether this node was emitted implicitly for an embedded field,
2030 // that is, expr_ is not the expr_ of the original user node.
2031 bool implicit_;
2032 // Whether we have already emitted a fieldtrack call.
2033 bool called_fieldtrack_;
2036 // A reference to a field of an interface.
2038 class Interface_field_reference_expression : public Expression
2040 public:
2041 Interface_field_reference_expression(Expression* expr,
2042 const std::string& name,
2043 Location location)
2044 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
2045 expr_(expr), name_(name)
2048 // Return the expression for the interface object.
2049 Expression*
2050 expr()
2051 { return this->expr_; }
2053 // Return the name of the method to call.
2054 const std::string&
2055 name() const
2056 { return this->name_; }
2058 // Create a thunk to call the method NAME in TYPE when it is used as
2059 // part of a method value.
2060 static Named_object*
2061 create_thunk(Gogo*, Interface_type* type, const std::string& name);
2063 // Return a tree for the pointer to the function to call, given a
2064 // tree for the expression.
2065 tree
2066 get_function_tree(Translate_context*, tree);
2068 // Return a tree for the first argument to pass to the interface
2069 // function, given a tree for the expression. This is the real
2070 // object associated with the interface object.
2071 tree
2072 get_underlying_object_tree(Translate_context*, tree);
2074 protected:
2076 do_traverse(Traverse* traverse);
2078 Expression*
2079 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2081 Type*
2082 do_type();
2084 void
2085 do_determine_type(const Type_context*);
2087 void
2088 do_check_types(Gogo*);
2090 Expression*
2091 do_copy()
2093 return Expression::make_interface_field_reference(this->expr_->copy(),
2094 this->name_,
2095 this->location());
2098 tree
2099 do_get_tree(Translate_context*);
2101 void
2102 do_dump_expression(Ast_dump_context*) const;
2104 private:
2105 // A mapping from interface types to a list of thunks we have
2106 // created for methods.
2107 typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
2108 typedef Unordered_map(Interface_type*, Method_thunks*)
2109 Interface_method_thunks;
2110 static Interface_method_thunks interface_method_thunks;
2112 // The expression for the interface object. This should have a type
2113 // of interface or pointer to interface.
2114 Expression* expr_;
2115 // The field we are retrieving--the name of the method.
2116 std::string name_;
2119 // A type guard expression.
2121 class Type_guard_expression : public Expression
2123 public:
2124 Type_guard_expression(Expression* expr, Type* type, Location location)
2125 : Expression(EXPRESSION_TYPE_GUARD, location),
2126 expr_(expr), type_(type)
2129 // Return the expression to convert.
2130 Expression*
2131 expr()
2132 { return this->expr_; }
2134 // Return the type to which to convert.
2135 Type*
2136 type()
2137 { return this->type_; }
2139 protected:
2141 do_traverse(Traverse* traverse);
2143 Type*
2144 do_type()
2145 { return this->type_; }
2147 void
2148 do_determine_type(const Type_context*)
2149 { this->expr_->determine_type_no_context(); }
2151 void
2152 do_check_types(Gogo*);
2154 Expression*
2155 do_copy()
2157 return new Type_guard_expression(this->expr_->copy(), this->type_,
2158 this->location());
2161 tree
2162 do_get_tree(Translate_context*);
2164 void
2165 do_dump_expression(Ast_dump_context*) const;
2167 private:
2168 // The expression to convert.
2169 Expression* expr_;
2170 // The type to which to convert.
2171 Type* type_;
2174 // A receive expression.
2176 class Receive_expression : public Expression
2178 public:
2179 Receive_expression(Expression* channel, Location location)
2180 : Expression(EXPRESSION_RECEIVE, location),
2181 channel_(channel)
2184 // Return the channel.
2185 Expression*
2186 channel()
2187 { return this->channel_; }
2189 protected:
2191 do_traverse(Traverse* traverse)
2192 { return Expression::traverse(&this->channel_, traverse); }
2194 bool
2195 do_discarding_value()
2196 { return true; }
2198 Type*
2199 do_type();
2201 void
2202 do_determine_type(const Type_context*)
2203 { this->channel_->determine_type_no_context(); }
2205 void
2206 do_check_types(Gogo*);
2208 Expression*
2209 do_copy()
2211 return Expression::make_receive(this->channel_->copy(), this->location());
2214 bool
2215 do_must_eval_in_order() const
2216 { return true; }
2218 tree
2219 do_get_tree(Translate_context*);
2221 void
2222 do_dump_expression(Ast_dump_context*) const;
2224 private:
2225 // The channel from which we are receiving.
2226 Expression* channel_;
2229 // A numeric constant. This is used both for untyped constants and
2230 // for constants that have a type.
2232 class Numeric_constant
2234 public:
2235 Numeric_constant()
2236 : classification_(NC_INVALID), type_(NULL)
2239 ~Numeric_constant();
2241 Numeric_constant(const Numeric_constant&);
2243 Numeric_constant& operator=(const Numeric_constant&);
2245 // Set to an unsigned long value.
2246 void
2247 set_unsigned_long(Type*, unsigned long);
2249 // Set to an integer value.
2250 void
2251 set_int(Type*, const mpz_t);
2253 // Set to a rune value.
2254 void
2255 set_rune(Type*, const mpz_t);
2257 // Set to a floating point value.
2258 void
2259 set_float(Type*, const mpfr_t);
2261 // Set to a complex value.
2262 void
2263 set_complex(Type*, const mpfr_t, const mpfr_t);
2265 // Classifiers.
2266 bool
2267 is_int() const
2268 { return this->classification_ == Numeric_constant::NC_INT; }
2270 bool
2271 is_rune() const
2272 { return this->classification_ == Numeric_constant::NC_RUNE; }
2274 bool
2275 is_float() const
2276 { return this->classification_ == Numeric_constant::NC_FLOAT; }
2278 bool
2279 is_complex() const
2280 { return this->classification_ == Numeric_constant::NC_COMPLEX; }
2282 // Value retrievers. These will initialize the values as well as
2283 // set them. GET_INT is only valid if IS_INT returns true, and
2284 // likewise respectively.
2285 void
2286 get_int(mpz_t*) const;
2288 void
2289 get_rune(mpz_t*) const;
2291 void
2292 get_float(mpfr_t*) const;
2294 void
2295 get_complex(mpfr_t*, mpfr_t*) const;
2297 // Codes returned by to_unsigned_long.
2298 enum To_unsigned_long
2300 // Value is integer and fits in unsigned long.
2301 NC_UL_VALID,
2302 // Value is not integer.
2303 NC_UL_NOTINT,
2304 // Value is integer but is negative.
2305 NC_UL_NEGATIVE,
2306 // Value is non-negative integer but does not fit in unsigned
2307 // long.
2308 NC_UL_BIG
2311 // If the value can be expressed as an integer that fits in an
2312 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
2313 // one of the other To_unsigned_long codes.
2314 To_unsigned_long
2315 to_unsigned_long(unsigned long* val) const;
2317 // If the value can be expressed as an int, return true and
2318 // initialize and set VAL. This will return false for a value with
2319 // an explicit float or complex type, even if the value is integral.
2320 bool
2321 to_int(mpz_t* val) const;
2323 // If the value can be expressed as a float, return true and
2324 // initialize and set VAL.
2325 bool
2326 to_float(mpfr_t* val) const;
2328 // If the value can be expressed as a complex, return true and
2329 // initialize and set VR and VI.
2330 bool
2331 to_complex(mpfr_t* vr, mpfr_t* vi) const;
2333 // Get the type.
2334 Type*
2335 type() const;
2337 // If the constant can be expressed in TYPE, then set the type of
2338 // the constant to TYPE and return true. Otherwise return false,
2339 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
2340 // the location to use for the error.
2341 bool
2342 set_type(Type* type, bool issue_error, Location location);
2344 // Return an Expression for this value.
2345 Expression*
2346 expression(Location) const;
2348 private:
2349 void
2350 clear();
2352 To_unsigned_long
2353 mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
2355 To_unsigned_long
2356 mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
2358 bool
2359 check_int_type(Integer_type*, bool, Location) const;
2361 bool
2362 check_float_type(Float_type*, bool, Location);
2364 bool
2365 check_complex_type(Complex_type*, bool, Location);
2367 // The kinds of constants.
2368 enum Classification
2370 NC_INVALID,
2371 NC_RUNE,
2372 NC_INT,
2373 NC_FLOAT,
2374 NC_COMPLEX
2377 // The kind of constant.
2378 Classification classification_;
2379 // The value.
2380 union
2382 // If NC_INT or NC_RUNE.
2383 mpz_t int_val;
2384 // If NC_FLOAT.
2385 mpfr_t float_val;
2386 // If NC_COMPLEX.
2387 struct
2389 mpfr_t real;
2390 mpfr_t imag;
2391 } complex_val;
2392 } u_;
2393 // The type if there is one. This will be NULL for an untyped
2394 // constant.
2395 Type* type_;
2398 #endif // !defined(GO_EXPRESSIONS_H)