compiler: fix test for mismatch between function results and uses
[official-gcc.git] / gcc / go / gofrontend / expressions.h
blob0ce6f22706a8bbd664113dbd86b44a34ff62e4bb
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 Unary_expression;
34 class Binary_expression;
35 class Call_expression;
36 class Func_expression;
37 class Func_descriptor_expression;
38 class Unknown_expression;
39 class Index_expression;
40 class Map_index_expression;
41 class Bound_method_expression;
42 class Field_reference_expression;
43 class Interface_field_reference_expression;
44 class Type_guard_expression;
45 class Receive_expression;
46 class Numeric_constant;
47 class Named_object;
48 class Export;
49 class Import;
50 class Temporary_statement;
51 class Label;
52 class Ast_dump_context;
53 class String_dump;
55 // The base class for all expressions.
57 class Expression
59 public:
60 // The types of expressions.
61 enum Expression_classification
63 EXPRESSION_ERROR,
64 EXPRESSION_TYPE,
65 EXPRESSION_UNARY,
66 EXPRESSION_BINARY,
67 EXPRESSION_CONST_REFERENCE,
68 EXPRESSION_VAR_REFERENCE,
69 EXPRESSION_TEMPORARY_REFERENCE,
70 EXPRESSION_SET_AND_USE_TEMPORARY,
71 EXPRESSION_SINK,
72 EXPRESSION_FUNC_REFERENCE,
73 EXPRESSION_FUNC_DESCRIPTOR,
74 EXPRESSION_FUNC_CODE_REFERENCE,
75 EXPRESSION_UNKNOWN_REFERENCE,
76 EXPRESSION_BOOLEAN,
77 EXPRESSION_STRING,
78 EXPRESSION_STRING_INFO,
79 EXPRESSION_INTEGER,
80 EXPRESSION_FLOAT,
81 EXPRESSION_COMPLEX,
82 EXPRESSION_NIL,
83 EXPRESSION_IOTA,
84 EXPRESSION_CALL,
85 EXPRESSION_CALL_RESULT,
86 EXPRESSION_BOUND_METHOD,
87 EXPRESSION_INDEX,
88 EXPRESSION_ARRAY_INDEX,
89 EXPRESSION_STRING_INDEX,
90 EXPRESSION_MAP_INDEX,
91 EXPRESSION_SELECTOR,
92 EXPRESSION_FIELD_REFERENCE,
93 EXPRESSION_INTERFACE_FIELD_REFERENCE,
94 EXPRESSION_ALLOCATION,
95 EXPRESSION_TYPE_GUARD,
96 EXPRESSION_CONVERSION,
97 EXPRESSION_UNSAFE_CONVERSION,
98 EXPRESSION_STRUCT_CONSTRUCTION,
99 EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
100 EXPRESSION_SLICE_CONSTRUCTION,
101 EXPRESSION_MAP_CONSTRUCTION,
102 EXPRESSION_COMPOSITE_LITERAL,
103 EXPRESSION_HEAP,
104 EXPRESSION_RECEIVE,
105 EXPRESSION_TYPE_DESCRIPTOR,
106 EXPRESSION_TYPE_INFO,
107 EXPRESSION_SLICE_INFO,
108 EXPRESSION_SLICE_VALUE,
109 EXPRESSION_INTERFACE_INFO,
110 EXPRESSION_INTERFACE_VALUE,
111 EXPRESSION_INTERFACE_MTABLE,
112 EXPRESSION_STRUCT_FIELD_OFFSET,
113 EXPRESSION_MAP_DESCRIPTOR,
114 EXPRESSION_LABEL_ADDR,
115 EXPRESSION_CONDITIONAL,
116 EXPRESSION_COMPOUND
119 Expression(Expression_classification, Location);
121 virtual ~Expression();
123 // Make an error expression. This is used when a parse error occurs
124 // to prevent cascading errors.
125 static Expression*
126 make_error(Location);
128 // Make an expression which is really a type. This is used during
129 // parsing.
130 static Expression*
131 make_type(Type*, Location);
133 // Make a unary expression.
134 static Expression*
135 make_unary(Operator, Expression*, Location);
137 // Make a binary expression.
138 static Expression*
139 make_binary(Operator, Expression*, Expression*, Location);
141 // Make a reference to a constant in an expression.
142 static Expression*
143 make_const_reference(Named_object*, Location);
145 // Make a reference to a variable in an expression.
146 static Expression*
147 make_var_reference(Named_object*, Location);
149 // Make a reference to a temporary variable. Temporary variables
150 // are always created by a single statement, which is what we use to
151 // refer to them.
152 static Temporary_reference_expression*
153 make_temporary_reference(Temporary_statement*, Location);
155 // Make an expressions which sets a temporary variable and then
156 // evaluates to a reference to that temporary variable. This is
157 // used to set a temporary variable while retaining the order of
158 // evaluation.
159 static Set_and_use_temporary_expression*
160 make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
162 // Make a sink expression--a reference to the blank identifier _.
163 static Expression*
164 make_sink(Location);
166 // Make a reference to a function in an expression. This returns a
167 // pointer to the struct holding the address of the function
168 // followed by any closed-over variables.
169 static Expression*
170 make_func_reference(Named_object*, Expression* closure, Location);
172 // Make a function descriptor, an immutable struct with a single
173 // field that points to the function code. This may only be used
174 // with functions that do not have closures. FN is the function for
175 // which we are making the descriptor.
176 static Func_descriptor_expression*
177 make_func_descriptor(Named_object* fn);
179 // Make a reference to the code of a function. This is used to set
180 // descriptor and closure fields.
181 static Expression*
182 make_func_code_reference(Named_object*, Location);
184 // Make a reference to an unknown name. In a correct program this
185 // will always be lowered to a real const/var/func reference.
186 static Unknown_expression*
187 make_unknown_reference(Named_object*, Location);
189 // Make a constant bool expression.
190 static Expression*
191 make_boolean(bool val, Location);
193 // Make a constant string expression.
194 static Expression*
195 make_string(const std::string&, Location);
197 // Make an expression that evaluates to some characteristic of an string.
198 // For simplicity, the enum values must match the field indexes in the
199 // underlying struct.
200 enum String_info
202 // The underlying data in the string.
203 STRING_INFO_DATA,
204 // The length of the string.
205 STRING_INFO_LENGTH
208 static Expression*
209 make_string_info(Expression* string, String_info, Location);
211 // Make a character constant expression. TYPE should be NULL for an
212 // abstract type.
213 static Expression*
214 make_character(const mpz_t*, Type*, Location);
216 // Make a constant integer expression. TYPE should be NULL for an
217 // abstract type.
218 static Expression*
219 make_integer(const mpz_t*, Type*, Location);
221 // Make a constant float expression. TYPE should be NULL for an
222 // abstract type.
223 static Expression*
224 make_float(const mpfr_t*, Type*, Location);
226 // Make a constant complex expression. TYPE should be NULL for an
227 // abstract type.
228 static Expression*
229 make_complex(const mpfr_t* real, const mpfr_t* imag, Type*, Location);
231 // Make a nil expression.
232 static Expression*
233 make_nil(Location);
235 // Make an iota expression. This is used for the predeclared
236 // constant iota.
237 static Expression*
238 make_iota();
240 // Make a call expression.
241 static Call_expression*
242 make_call(Expression* func, Expression_list* args, bool is_varargs,
243 Location);
245 // Make a reference to a specific result of a call expression which
246 // returns a tuple.
247 static Expression*
248 make_call_result(Call_expression*, unsigned int index);
250 // Make an expression which is a method bound to its first
251 // parameter. METHOD is the method being called, FUNCTION is the
252 // function to call.
253 static Bound_method_expression*
254 make_bound_method(Expression* object, const Method* method,
255 Named_object* function, Location);
257 // Make an index or slice expression. This is a parser expression
258 // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
259 // index rather than a slice. CAP may be NULL, meaning we use the default
260 // capacity of LEFT. At parse time we may not know the type of LEFT.
261 // After parsing this is lowered to an array index, a string index,
262 // or a map index.
263 static Expression*
264 make_index(Expression* left, Expression* start, Expression* end,
265 Expression* cap, Location);
267 // Make an array index expression. END may be NULL, in which case
268 // this is an lvalue. CAP may be NULL, in which case it defaults
269 // to cap(ARRAY).
270 static Expression*
271 make_array_index(Expression* array, Expression* start, Expression* end,
272 Expression* cap, Location);
274 // Make a string index expression. END may be NULL. This is never
275 // an lvalue.
276 static Expression*
277 make_string_index(Expression* string, Expression* start, Expression* end,
278 Location);
280 // Make a map index expression. This is an lvalue.
281 static Map_index_expression*
282 make_map_index(Expression* map, Expression* val, Location);
284 // Make a selector. This is a parser expression which represents
285 // LEFT.NAME. At parse time we may not know the type of the left
286 // hand side.
287 static Expression*
288 make_selector(Expression* left, const std::string& name, Location);
290 // Make a reference to a field in a struct.
291 static Field_reference_expression*
292 make_field_reference(Expression*, unsigned int field_index, Location);
294 // Make a reference to a field of an interface, with an associated
295 // object.
296 static Expression*
297 make_interface_field_reference(Expression*, const std::string&,
298 Location);
300 // Make an allocation expression.
301 static Expression*
302 make_allocation(Type*, Location);
304 // Make a type guard expression.
305 static Expression*
306 make_type_guard(Expression*, Type*, Location);
308 // Make a type cast expression.
309 static Expression*
310 make_cast(Type*, Expression*, Location);
312 // Make an unsafe type cast expression. This is only used when
313 // passing parameter to builtin functions that are part of the Go
314 // runtime.
315 static Expression*
316 make_unsafe_cast(Type*, Expression*, Location);
318 // Make a composite literal. The DEPTH parameter is how far down we
319 // are in a list of composite literals with omitted types. HAS_KEYS
320 // is true if the expression list has keys alternating with values.
321 // ALL_ARE_NAMES is true if all the keys could be struct field
322 // names.
323 static Expression*
324 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
325 bool all_are_names, Location);
327 // Make a struct composite literal.
328 static Expression*
329 make_struct_composite_literal(Type*, Expression_list*, Location);
331 // Make an array composite literal.
332 static Expression*
333 make_array_composite_literal(Type*, Expression_list*, Location);
335 // Make a slice composite literal.
336 static Expression*
337 make_slice_composite_literal(Type*, Expression_list*, Location);
339 // Take an expression and allocate it on the heap.
340 static Expression*
341 make_heap_expression(Expression*, Location);
343 // Make a receive expression. VAL is NULL for a unary receive.
344 static Receive_expression*
345 make_receive(Expression* channel, Location);
347 // Make an expression which evaluates to the address of the type
348 // descriptor for TYPE.
349 static Expression*
350 make_type_descriptor(Type* type, Location);
352 // Make an expression which evaluates to some characteristic of a
353 // type. These are only used for type descriptors, so there is no
354 // location parameter.
355 enum Type_info
357 // The size of a value of the type.
358 TYPE_INFO_SIZE,
359 // The required alignment of a value of the type.
360 TYPE_INFO_ALIGNMENT,
361 // The required alignment of a value of the type when used as a
362 // field in a struct.
363 TYPE_INFO_FIELD_ALIGNMENT
366 static Expression*
367 make_type_info(Type* type, Type_info);
369 // Make an expression that evaluates to some characteristic of a
370 // slice. For simplicity, the enum values must match the field indexes
371 // in the underlying struct.
372 enum Slice_info
374 // The underlying data of the slice.
375 SLICE_INFO_VALUE_POINTER,
376 // The length of the slice.
377 SLICE_INFO_LENGTH,
378 // The capacity of the slice.
379 SLICE_INFO_CAPACITY
382 static Expression*
383 make_slice_info(Expression* slice, Slice_info, Location);
385 // Make an expression for a slice value.
386 static Expression*
387 make_slice_value(Type*, Expression* valptr, Expression* len, Expression* cap,
388 Location);
390 // Make an expression that evaluates to some characteristic of an
391 // interface. For simplicity, the enum values must match the field indexes
392 // in the underlying struct.
393 enum Interface_info
395 // The type descriptor of an empty interface.
396 INTERFACE_INFO_TYPE_DESCRIPTOR = 0,
397 // The methods of an interface.
398 INTERFACE_INFO_METHODS = 0,
399 // The first argument to pass to an interface method.
400 INTERFACE_INFO_OBJECT
403 static Expression*
404 make_interface_info(Expression* iface, Interface_info, Location);
406 // Make an expression for an interface value.
407 static Expression*
408 make_interface_value(Type*, Expression*, Expression*, Location);
410 // Make an expression that builds a reference to the interface method table
411 // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
412 // reference to the interface method table for the pointer receiver type.
413 static Expression*
414 make_interface_mtable_ref(Interface_type* itype, Type* type,
415 bool is_pointer, Location);
417 // Make an expression which evaluates to the offset of a field in a
418 // struct. This is only used for type descriptors, so there is no
419 // location parameter.
420 static Expression*
421 make_struct_field_offset(Struct_type*, const Struct_field*);
423 // Make an expression which evaluates to the address of the map
424 // descriptor for TYPE.
425 static Expression*
426 make_map_descriptor(Map_type* type, Location);
428 // Make an expression which evaluates to the address of an unnamed
429 // label.
430 static Expression*
431 make_label_addr(Label*, Location);
433 // Make a conditional expression.
434 static Expression*
435 make_conditional(Expression*, Expression*, Expression*, Location);
437 // Make a compound expression.
438 static Expression*
439 make_compound(Expression*, Expression*, Location);
441 // Return the expression classification.
442 Expression_classification
443 classification() const
444 { return this->classification_; }
446 // Return the location of the expression.
447 Location
448 location() const
449 { return this->location_; }
451 // Return whether this is a constant expression.
452 bool
453 is_constant() const
454 { return this->do_is_constant(); }
456 // Return whether this is an immutable expression.
457 bool
458 is_immutable() const
459 { return this->do_is_immutable(); }
461 // If this is not a numeric constant, return false. If it is one,
462 // return true, and set VAL to hold the value.
463 bool
464 numeric_constant_value(Numeric_constant* val) const
465 { return this->do_numeric_constant_value(val); }
467 // If this is not a constant expression with string type, return
468 // false. If it is one, return true, and set VAL to the value.
469 bool
470 string_constant_value(std::string* val) const
471 { return this->do_string_constant_value(val); }
473 // This is called if the value of this expression is being
474 // discarded. This issues warnings about computed values being
475 // unused. This returns true if all is well, false if it issued an
476 // error message.
477 bool
478 discarding_value()
479 { return this->do_discarding_value(); }
481 // Return whether this is an error expression.
482 bool
483 is_error_expression() const
484 { return this->classification_ == EXPRESSION_ERROR; }
486 // Return whether this expression really represents a type.
487 bool
488 is_type_expression() const
489 { return this->classification_ == EXPRESSION_TYPE; }
491 // If this is a variable reference, return the Var_expression
492 // structure. Otherwise, return NULL. This is a controlled dynamic
493 // cast.
494 Var_expression*
495 var_expression()
496 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
498 const Var_expression*
499 var_expression() const
500 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
502 // If this is a reference to a temporary variable, return the
503 // Temporary_reference_expression. Otherwise, return NULL.
504 Temporary_reference_expression*
505 temporary_reference_expression()
507 return this->convert<Temporary_reference_expression,
508 EXPRESSION_TEMPORARY_REFERENCE>();
511 // If this is a set-and-use-temporary, return the
512 // Set_and_use_temporary_expression. Otherwise, return NULL.
513 Set_and_use_temporary_expression*
514 set_and_use_temporary_expression()
516 return this->convert<Set_and_use_temporary_expression,
517 EXPRESSION_SET_AND_USE_TEMPORARY>();
520 // Return whether this is a sink expression.
521 bool
522 is_sink_expression() const
523 { return this->classification_ == EXPRESSION_SINK; }
525 // If this is a string expression, return the String_expression
526 // structure. Otherwise, return NULL.
527 String_expression*
528 string_expression()
529 { return this->convert<String_expression, EXPRESSION_STRING>(); }
531 // Return whether this is the expression nil.
532 bool
533 is_nil_expression() const
534 { return this->classification_ == EXPRESSION_NIL; }
536 // If this is an indirection through a pointer, return the
537 // expression being pointed through. Otherwise return this.
538 Expression*
539 deref();
541 // If this is a unary expression, return the Unary_expression
542 // structure. Otherwise return NULL.
543 Unary_expression*
544 unary_expression()
545 { return this->convert<Unary_expression, EXPRESSION_UNARY>(); }
547 // If this is a binary expression, return the Binary_expression
548 // structure. Otherwise return NULL.
549 Binary_expression*
550 binary_expression()
551 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
553 // If this is a call expression, return the Call_expression
554 // structure. Otherwise, return NULL. This is a controlled dynamic
555 // cast.
556 Call_expression*
557 call_expression()
558 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
560 // If this is an expression which refers to a function, return the
561 // Func_expression structure. Otherwise, return NULL.
562 Func_expression*
563 func_expression()
564 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
566 const Func_expression*
567 func_expression() const
568 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
570 // If this is an expression which refers to an unknown name, return
571 // the Unknown_expression structure. Otherwise, return NULL.
572 Unknown_expression*
573 unknown_expression()
574 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
576 const Unknown_expression*
577 unknown_expression() const
579 return this->convert<const Unknown_expression,
580 EXPRESSION_UNKNOWN_REFERENCE>();
583 // If this is an index expression, return the Index_expression
584 // structure. Otherwise, return NULL.
585 Index_expression*
586 index_expression()
587 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
589 // If this is an expression which refers to indexing in a map,
590 // return the Map_index_expression structure. Otherwise, return
591 // NULL.
592 Map_index_expression*
593 map_index_expression()
594 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
596 // If this is a bound method expression, return the
597 // Bound_method_expression structure. Otherwise, return NULL.
598 Bound_method_expression*
599 bound_method_expression()
600 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
602 // If this is a reference to a field in a struct, return the
603 // Field_reference_expression structure. Otherwise, return NULL.
604 Field_reference_expression*
605 field_reference_expression()
607 return this->convert<Field_reference_expression,
608 EXPRESSION_FIELD_REFERENCE>();
611 // If this is a reference to a field in an interface, return the
612 // Interface_field_reference_expression structure. Otherwise,
613 // return NULL.
614 Interface_field_reference_expression*
615 interface_field_reference_expression()
617 return this->convert<Interface_field_reference_expression,
618 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
621 // If this is a type guard expression, return the
622 // Type_guard_expression structure. Otherwise, return NULL.
623 Type_guard_expression*
624 type_guard_expression()
625 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
627 // If this is a receive expression, return the Receive_expression
628 // structure. Otherwise, return NULL.
629 Receive_expression*
630 receive_expression()
631 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
633 // Return true if this is a composite literal.
634 bool
635 is_composite_literal() const;
637 // Return true if this is a composite literal which is not constant.
638 bool
639 is_nonconstant_composite_literal() const;
641 // Return true if this is a variable or temporary variable.
642 bool
643 is_variable() const;
645 // Return true if this is a reference to a local variable.
646 bool
647 is_local_variable() const;
649 // Make the builtin function descriptor type, so that it can be
650 // converted.
651 static void
652 make_func_descriptor_type();
654 // Traverse an expression.
655 static int
656 traverse(Expression**, Traverse*);
658 // Traverse subexpressions of this expression.
660 traverse_subexpressions(Traverse*);
662 // Lower an expression. This is called immediately after parsing.
663 // FUNCTION is the function we are in; it will be NULL for an
664 // expression initializing a global variable. INSERTER may be used
665 // to insert statements before the statement or initializer
666 // containing this expression; it is normally used to create
667 // temporary variables. IOTA_VALUE is the value that we should give
668 // to any iota expressions. This function must resolve expressions
669 // which could not be fully parsed into their final form. It
670 // returns the same Expression or a new one.
671 Expression*
672 lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
673 int iota_value)
674 { return this->do_lower(gogo, function, inserter, iota_value); }
676 // Flatten an expression. This is called after order_evaluation.
677 // FUNCTION is the function we are in; it will be NULL for an
678 // expression initializing a global variable. INSERTER may be used
679 // to insert statements before the statement or initializer
680 // containing this expression; it is normally used to create
681 // temporary variables. This function must resolve expressions
682 // which could not be fully parsed into their final form. It
683 // returns the same Expression or a new one.
684 Expression*
685 flatten(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
686 { return this->do_flatten(gogo, function, inserter); }
688 // Determine the real type of an expression with abstract integer,
689 // floating point, or complex type. TYPE_CONTEXT describes the
690 // expected type.
691 void
692 determine_type(const Type_context*);
694 // Check types in an expression.
695 void
696 check_types(Gogo* gogo)
697 { this->do_check_types(gogo); }
699 // Determine the type when there is no context.
700 void
701 determine_type_no_context();
703 // Return the current type of the expression. This may be changed
704 // by determine_type.
705 Type*
706 type()
707 { return this->do_type(); }
709 // Return a copy of an expression.
710 Expression*
711 copy()
712 { return this->do_copy(); }
714 // Return whether the expression is addressable--something which may
715 // be used as the operand of the unary & operator.
716 bool
717 is_addressable() const
718 { return this->do_is_addressable(); }
720 // Note that we are taking the address of this expression. ESCAPES
721 // is true if this address escapes the current function.
722 void
723 address_taken(bool escapes)
724 { this->do_address_taken(escapes); }
726 // Note that a nil check must be issued for this expression.
727 void
728 issue_nil_check()
729 { this->do_issue_nil_check(); }
731 // Return whether this expression must be evaluated in order
732 // according to the order of evaluation rules. This is basically
733 // true of all expressions with side-effects.
734 bool
735 must_eval_in_order() const
736 { return this->do_must_eval_in_order(); }
738 // Return whether subexpressions of this expression must be
739 // evaluated in order. This is true of index expressions and
740 // pointer indirections. This sets *SKIP to the number of
741 // subexpressions to skip during traversing, as index expressions
742 // only requiring moving the index, not the array.
743 bool
744 must_eval_subexpressions_in_order(int* skip) const
746 *skip = 0;
747 return this->do_must_eval_subexpressions_in_order(skip);
750 // Return the backend representation for this expression.
751 Bexpression*
752 get_backend(Translate_context*);
754 // Return an expression handling any conversions which must be done during
755 // assignment.
756 static Expression*
757 convert_for_assignment(Gogo*, Type* lhs_type, Expression* rhs,
758 Location location);
760 // Return an expression converting a value of one interface type to another
761 // interface type. If FOR_TYPE_GUARD is true this is for a type
762 // assertion.
763 static Expression*
764 convert_interface_to_interface(Type* lhs_type,
765 Expression* rhs, bool for_type_guard,
766 Location);
768 // Return a backend expression implementing the comparison LEFT OP RIGHT.
769 // TYPE is the type of both sides.
770 static Bexpression*
771 comparison(Translate_context*, Type* result_type, Operator op,
772 Expression* left, Expression* right, Location);
774 // Return the backend expression for the numeric constant VAL.
775 static Bexpression*
776 backend_numeric_constant_expression(Translate_context*,
777 Numeric_constant* val);
779 // Export the expression. This is only used for constants. It will
780 // be used for things like values of named constants and sizes of
781 // arrays.
782 void
783 export_expression(Export* exp) const
784 { this->do_export(exp); }
786 // Import an expression.
787 static Expression*
788 import_expression(Import*);
790 // Return an expression which checks that VAL, of arbitrary integer type,
791 // is non-negative and is not more than the maximum integer value.
792 static Expression*
793 check_bounds(Expression* val, Location);
795 // Dump an expression to a dump constext.
796 void
797 dump_expression(Ast_dump_context*) const;
799 protected:
800 // May be implemented by child class: traverse the expressions.
801 virtual int
802 do_traverse(Traverse*);
804 // Return a lowered expression.
805 virtual Expression*
806 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
807 { return this; }
809 // Return a flattened expression.
810 virtual Expression*
811 do_flatten(Gogo*, Named_object*, Statement_inserter*)
812 { return this; }
815 // Return whether this is a constant expression.
816 virtual bool
817 do_is_constant() const
818 { return false; }
820 // Return whether this is an immutable expression.
821 virtual bool
822 do_is_immutable() const
823 { return false; }
825 // Return whether this is a constant expression of numeric type, and
826 // set the Numeric_constant to the value.
827 virtual bool
828 do_numeric_constant_value(Numeric_constant*) const
829 { return false; }
831 // Return whether this is a constant expression of string type, and
832 // set VAL to the value.
833 virtual bool
834 do_string_constant_value(std::string*) const
835 { return false; }
837 // Called by the parser if the value is being discarded.
838 virtual bool
839 do_discarding_value();
841 // Child class holds type.
842 virtual Type*
843 do_type() = 0;
845 // Child class implements determining type information.
846 virtual void
847 do_determine_type(const Type_context*) = 0;
849 // Child class implements type checking if needed.
850 virtual void
851 do_check_types(Gogo*)
854 // Child class implements copying.
855 virtual Expression*
856 do_copy() = 0;
858 // Child class implements whether the expression is addressable.
859 virtual bool
860 do_is_addressable() const
861 { return false; }
863 // Child class implements taking the address of an expression.
864 virtual void
865 do_address_taken(bool)
868 // Child class implements issuing a nil check if the address is taken.
869 virtual void
870 do_issue_nil_check()
873 // Child class implements whether this expression must be evaluated
874 // in order.
875 virtual bool
876 do_must_eval_in_order() const
877 { return false; }
879 // Child class implements whether this expressions requires that
880 // subexpressions be evaluated in order. The child implementation
881 // may set *SKIP if it should be non-zero.
882 virtual bool
883 do_must_eval_subexpressions_in_order(int* /* skip */) const
884 { return false; }
886 // Child class implements conversion to backend representation.
887 virtual Bexpression*
888 do_get_backend(Translate_context*) = 0;
890 // Child class implements export.
891 virtual void
892 do_export(Export*) const;
894 // For children to call to give an error for an unused value.
895 void
896 unused_value_error();
898 // For children to call when they detect that they are in error.
899 void
900 set_is_error();
902 // For children to call to report an error conveniently.
903 void
904 report_error(const char*);
906 // Child class implements dumping to a dump context.
907 virtual void
908 do_dump_expression(Ast_dump_context*) const = 0;
910 private:
911 // Convert to the desired statement classification, or return NULL.
912 // This is a controlled dynamic cast.
913 template<typename Expression_class,
914 Expression_classification expr_classification>
915 Expression_class*
916 convert()
918 return (this->classification_ == expr_classification
919 ? static_cast<Expression_class*>(this)
920 : NULL);
923 template<typename Expression_class,
924 Expression_classification expr_classification>
925 const Expression_class*
926 convert() const
928 return (this->classification_ == expr_classification
929 ? static_cast<const Expression_class*>(this)
930 : NULL);
933 static Expression*
934 convert_type_to_interface(Type*, Expression*, Location);
936 static Expression*
937 get_interface_type_descriptor(Expression*);
939 static Expression*
940 convert_interface_to_type(Type*, Expression*, Location);
942 // The expression classification.
943 Expression_classification classification_;
944 // The location in the input file.
945 Location location_;
948 // A list of Expressions.
950 class Expression_list
952 public:
953 Expression_list()
954 : entries_()
957 // Return whether the list is empty.
958 bool
959 empty() const
960 { return this->entries_.empty(); }
962 // Return the number of entries in the list.
963 size_t
964 size() const
965 { return this->entries_.size(); }
967 // Add an entry to the end of the list.
968 void
969 push_back(Expression* expr)
970 { this->entries_.push_back(expr); }
972 void
973 append(Expression_list* add)
974 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
976 // Reserve space in the list.
977 void
978 reserve(size_t size)
979 { this->entries_.reserve(size); }
981 // Traverse the expressions in the list.
983 traverse(Traverse*);
985 // Copy the list.
986 Expression_list*
987 copy();
989 // Return true if the list contains an error expression.
990 bool
991 contains_error() const;
993 // Retrieve an element by index.
994 Expression*&
995 at(size_t i)
996 { return this->entries_.at(i); }
998 // Return the first and last elements.
999 Expression*&
1000 front()
1001 { return this->entries_.front(); }
1003 Expression*
1004 front() const
1005 { return this->entries_.front(); }
1007 Expression*&
1008 back()
1009 { return this->entries_.back(); }
1011 Expression*
1012 back() const
1013 { return this->entries_.back(); }
1015 // Iterators.
1017 typedef std::vector<Expression*>::iterator iterator;
1018 typedef std::vector<Expression*>::const_iterator const_iterator;
1020 iterator
1021 begin()
1022 { return this->entries_.begin(); }
1024 const_iterator
1025 begin() const
1026 { return this->entries_.begin(); }
1028 iterator
1029 end()
1030 { return this->entries_.end(); }
1032 const_iterator
1033 end() const
1034 { return this->entries_.end(); }
1036 // Erase an entry.
1037 void
1038 erase(iterator p)
1039 { this->entries_.erase(p); }
1041 private:
1042 std::vector<Expression*> entries_;
1045 // An abstract base class for an expression which is only used by the
1046 // parser, and is lowered in the lowering pass.
1048 class Parser_expression : public Expression
1050 public:
1051 Parser_expression(Expression_classification classification,
1052 Location location)
1053 : Expression(classification, location)
1056 protected:
1057 virtual Expression*
1058 do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
1060 Type*
1061 do_type();
1063 void
1064 do_determine_type(const Type_context*)
1065 { go_unreachable(); }
1067 void
1068 do_check_types(Gogo*)
1069 { go_unreachable(); }
1071 Bexpression*
1072 do_get_backend(Translate_context*)
1073 { go_unreachable(); }
1076 // An expression which is simply a variable.
1078 class Var_expression : public Expression
1080 public:
1081 Var_expression(Named_object* variable, Location location)
1082 : Expression(EXPRESSION_VAR_REFERENCE, location),
1083 variable_(variable)
1086 // Return the variable.
1087 Named_object*
1088 named_object() const
1089 { return this->variable_; }
1091 protected:
1092 Expression*
1093 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1095 Type*
1096 do_type();
1098 void
1099 do_determine_type(const Type_context*);
1101 Expression*
1102 do_copy()
1103 { return this; }
1105 bool
1106 do_is_addressable() const
1107 { return true; }
1109 void
1110 do_address_taken(bool);
1112 Bexpression*
1113 do_get_backend(Translate_context*);
1115 void
1116 do_dump_expression(Ast_dump_context*) const;
1118 private:
1119 // The variable we are referencing.
1120 Named_object* variable_;
1123 // A reference to a temporary variable.
1125 class Temporary_reference_expression : public Expression
1127 public:
1128 Temporary_reference_expression(Temporary_statement* statement,
1129 Location location)
1130 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1131 statement_(statement), is_lvalue_(false)
1134 // The temporary that this expression refers to.
1135 Temporary_statement*
1136 statement() const
1137 { return this->statement_; }
1139 // Indicate that this reference appears on the left hand side of an
1140 // assignment statement.
1141 void
1142 set_is_lvalue()
1143 { this->is_lvalue_ = true; }
1145 protected:
1146 Type*
1147 do_type();
1149 void
1150 do_determine_type(const Type_context*)
1153 Expression*
1154 do_copy()
1155 { return make_temporary_reference(this->statement_, this->location()); }
1157 bool
1158 do_is_addressable() const
1159 { return true; }
1161 void
1162 do_address_taken(bool);
1164 Bexpression*
1165 do_get_backend(Translate_context*);
1167 void
1168 do_dump_expression(Ast_dump_context*) const;
1170 private:
1171 // The statement where the temporary variable is defined.
1172 Temporary_statement* statement_;
1173 // Whether this reference appears on the left hand side of an
1174 // assignment statement.
1175 bool is_lvalue_;
1178 // Set and use a temporary variable.
1180 class Set_and_use_temporary_expression : public Expression
1182 public:
1183 Set_and_use_temporary_expression(Temporary_statement* statement,
1184 Expression* expr, Location location)
1185 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1186 statement_(statement), expr_(expr)
1189 // Return the temporary.
1190 Temporary_statement*
1191 temporary() const
1192 { return this->statement_; }
1194 // Return the expression.
1195 Expression*
1196 expression() const
1197 { return this->expr_; }
1199 protected:
1201 do_traverse(Traverse* traverse)
1202 { return Expression::traverse(&this->expr_, traverse); }
1204 Type*
1205 do_type();
1207 void
1208 do_determine_type(const Type_context*);
1210 Expression*
1211 do_copy()
1213 return make_set_and_use_temporary(this->statement_, this->expr_,
1214 this->location());
1217 bool
1218 do_is_addressable() const
1219 { return true; }
1221 void
1222 do_address_taken(bool);
1224 Bexpression*
1225 do_get_backend(Translate_context*);
1227 void
1228 do_dump_expression(Ast_dump_context*) const;
1230 private:
1231 // The statement where the temporary variable is defined.
1232 Temporary_statement* statement_;
1233 // The expression to assign to the temporary.
1234 Expression* expr_;
1237 // A string expression.
1239 class String_expression : public Expression
1241 public:
1242 String_expression(const std::string& val, Location location)
1243 : Expression(EXPRESSION_STRING, location),
1244 val_(val), type_(NULL)
1247 const std::string&
1248 val() const
1249 { return this->val_; }
1251 static Expression*
1252 do_import(Import*);
1254 protected:
1255 bool
1256 do_is_constant() const
1257 { return true; }
1259 bool
1260 do_is_immutable() const
1261 { return true; }
1263 bool
1264 do_string_constant_value(std::string* val) const
1266 *val = this->val_;
1267 return true;
1270 Type*
1271 do_type();
1273 void
1274 do_determine_type(const Type_context*);
1276 Expression*
1277 do_copy()
1278 { return this; }
1280 Bexpression*
1281 do_get_backend(Translate_context*);
1283 // Write string literal to a string dump.
1284 static void
1285 export_string(String_dump* exp, const String_expression* str);
1287 void
1288 do_export(Export*) const;
1290 void
1291 do_dump_expression(Ast_dump_context*) const;
1293 private:
1294 // The string value. This is immutable.
1295 const std::string val_;
1296 // The type as determined by context.
1297 Type* type_;
1300 // A Unary expression.
1302 class Unary_expression : public Expression
1304 public:
1305 Unary_expression(Operator op, Expression* expr, Location location)
1306 : Expression(EXPRESSION_UNARY, location),
1307 op_(op), escapes_(true), create_temp_(false), is_gc_root_(false),
1308 is_slice_init_(false), expr_(expr), issue_nil_check_(false)
1311 // Return the operator.
1312 Operator
1313 op() const
1314 { return this->op_; }
1316 // Return the operand.
1317 Expression*
1318 operand() const
1319 { return this->expr_; }
1321 // Record that an address expression does not escape.
1322 void
1323 set_does_not_escape()
1325 go_assert(this->op_ == OPERATOR_AND);
1326 this->escapes_ = false;
1329 // Record that this is an address expression which should create a
1330 // temporary variable if necessary. This is used for method calls.
1331 void
1332 set_create_temp()
1334 go_assert(this->op_ == OPERATOR_AND);
1335 this->create_temp_ = true;
1338 // Record that this is an address expression of a GC root, which is a
1339 // mutable composite literal. This used for registering GC variables.
1340 void
1341 set_is_gc_root()
1343 go_assert(this->op_ == OPERATOR_AND);
1344 this->is_gc_root_ = true;
1347 // Record that this is an address expression of a slice value initializer,
1348 // which is mutable if the values are not copied to the heap.
1349 void
1350 set_is_slice_init()
1352 go_assert(this->op_ == OPERATOR_AND);
1353 this->is_slice_init_ = true;
1356 // Apply unary opcode OP to UNC, setting NC. Return true if this
1357 // could be done, false if not. Issue errors for overflow.
1358 static bool
1359 eval_constant(Operator op, const Numeric_constant* unc,
1360 Location, Numeric_constant* nc);
1362 static Expression*
1363 do_import(Import*);
1365 protected:
1367 do_traverse(Traverse* traverse)
1368 { return Expression::traverse(&this->expr_, traverse); }
1370 Expression*
1371 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1373 Expression*
1374 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1376 bool
1377 do_is_constant() const;
1379 bool
1380 do_is_immutable() const
1382 return (this->expr_->is_immutable()
1383 || (this->op_ == OPERATOR_AND && this->expr_->is_variable()));
1386 bool
1387 do_numeric_constant_value(Numeric_constant*) const;
1389 Type*
1390 do_type();
1392 void
1393 do_determine_type(const Type_context*);
1395 void
1396 do_check_types(Gogo*);
1398 Expression*
1399 do_copy()
1401 return Expression::make_unary(this->op_, this->expr_->copy(),
1402 this->location());
1405 bool
1406 do_must_eval_subexpressions_in_order(int*) const
1407 { return this->op_ == OPERATOR_MULT; }
1409 bool
1410 do_is_addressable() const
1411 { return this->op_ == OPERATOR_MULT; }
1413 Bexpression*
1414 do_get_backend(Translate_context*);
1416 void
1417 do_export(Export*) const;
1419 void
1420 do_dump_expression(Ast_dump_context*) const;
1422 void
1423 do_issue_nil_check()
1424 { this->issue_nil_check_ = (this->op_ == OPERATOR_MULT); }
1426 private:
1427 // The unary operator to apply.
1428 Operator op_;
1429 // Normally true. False if this is an address expression which does
1430 // not escape the current function.
1431 bool escapes_;
1432 // True if this is an address expression which should create a
1433 // temporary variable if necessary.
1434 bool create_temp_;
1435 // True if this is an address expression for a GC root. A GC root is a
1436 // special struct composite literal that is mutable when addressed, meaning
1437 // it cannot be represented as an immutable_struct in the backend.
1438 bool is_gc_root_;
1439 // True if this is an address expression for a slice value with an immutable
1440 // initializer. The initializer for a slice's value pointer has an array
1441 // type, meaning it cannot be represented as an immutable_struct in the
1442 // backend.
1443 bool is_slice_init_;
1444 // The operand.
1445 Expression* expr_;
1446 // Whether or not to issue a nil check for this expression if its address
1447 // is being taken.
1448 bool issue_nil_check_;
1451 // A binary expression.
1453 class Binary_expression : public Expression
1455 public:
1456 Binary_expression(Operator op, Expression* left, Expression* right,
1457 Location location)
1458 : Expression(EXPRESSION_BINARY, location),
1459 op_(op), left_(left), right_(right), type_(NULL)
1462 // Return the operator.
1463 Operator
1464 op()
1465 { return this->op_; }
1467 // Return the left hand expression.
1468 Expression*
1469 left()
1470 { return this->left_; }
1472 // Return the right hand expression.
1473 Expression*
1474 right()
1475 { return this->right_; }
1477 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1478 // Return true if this could be done, false if not. Issue errors at
1479 // LOCATION as appropriate.
1480 static bool
1481 eval_constant(Operator op, Numeric_constant* left_nc,
1482 Numeric_constant* right_nc, Location location,
1483 Numeric_constant* nc);
1485 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1486 // *RESULT. Return true if this could be done, false if not. Issue
1487 // errors at LOCATION as appropriate.
1488 static bool
1489 compare_constant(Operator op, Numeric_constant* left_nc,
1490 Numeric_constant* right_nc, Location location,
1491 bool* result);
1493 static Expression*
1494 do_import(Import*);
1496 // Report an error if OP can not be applied to TYPE. Return whether
1497 // it can. OTYPE is the type of the other operand.
1498 static bool
1499 check_operator_type(Operator op, Type* type, Type* otype, Location);
1501 protected:
1503 do_traverse(Traverse* traverse);
1505 Expression*
1506 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1508 Expression*
1509 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1511 bool
1512 do_is_constant() const
1513 { return this->left_->is_constant() && this->right_->is_constant(); }
1515 bool
1516 do_numeric_constant_value(Numeric_constant*) const;
1518 bool
1519 do_discarding_value();
1521 Type*
1522 do_type();
1524 void
1525 do_determine_type(const Type_context*);
1527 void
1528 do_check_types(Gogo*);
1530 Expression*
1531 do_copy()
1533 return Expression::make_binary(this->op_, this->left_->copy(),
1534 this->right_->copy(), this->location());
1537 Bexpression*
1538 do_get_backend(Translate_context*);
1540 void
1541 do_export(Export*) const;
1543 void
1544 do_dump_expression(Ast_dump_context*) const;
1546 private:
1547 static bool
1548 operation_type(Operator op, Type* left_type, Type* right_type,
1549 Type** result_type);
1551 static bool
1552 cmp_to_bool(Operator op, int cmp);
1554 static bool
1555 eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
1556 Location, Numeric_constant*);
1558 static bool
1559 eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
1560 Location, Numeric_constant*);
1562 static bool
1563 eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
1564 Location, Numeric_constant*);
1566 static bool
1567 compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
1569 static bool
1570 compare_float(const Numeric_constant*, const Numeric_constant *, int*);
1572 static bool
1573 compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
1575 Expression*
1576 lower_struct_comparison(Gogo*, Statement_inserter*);
1578 Expression*
1579 lower_array_comparison(Gogo*, Statement_inserter*);
1581 Expression*
1582 lower_interface_value_comparison(Gogo*, Statement_inserter*);
1584 Expression*
1585 lower_compare_to_memcmp(Gogo*, Statement_inserter*);
1587 Expression*
1588 operand_address(Statement_inserter*, Expression*);
1590 // The binary operator to apply.
1591 Operator op_;
1592 // The left hand side operand.
1593 Expression* left_;
1594 // The right hand side operand.
1595 Expression* right_;
1596 // The type of a comparison operation.
1597 Type* type_;
1600 // A call expression. The go statement needs to dig inside this.
1602 class Call_expression : public Expression
1604 public:
1605 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
1606 Location location)
1607 : Expression(EXPRESSION_CALL, location),
1608 fn_(fn), args_(args), type_(NULL), results_(NULL), call_(NULL),
1609 call_temp_(NULL), expected_result_count_(0), is_varargs_(is_varargs),
1610 are_hidden_fields_ok_(false), varargs_are_lowered_(false),
1611 types_are_determined_(false), is_deferred_(false), issued_error_(false)
1614 // The function to call.
1615 Expression*
1616 fn() const
1617 { return this->fn_; }
1619 // The arguments.
1620 Expression_list*
1621 args()
1622 { return this->args_; }
1624 const Expression_list*
1625 args() const
1626 { return this->args_; }
1628 // Get the function type.
1629 Function_type*
1630 get_function_type() const;
1632 // Return the number of values this call will return.
1633 size_t
1634 result_count() const;
1636 // Return the temporary variable which holds result I. This is only
1637 // valid after the expression has been lowered, and is only valid
1638 // for calls which return multiple results.
1639 Temporary_statement*
1640 result(size_t i) const;
1642 // Set the number of results expected from this call. This is used
1643 // when the call appears in a context that expects multiple results,
1644 // such as a, b = f().
1645 void
1646 set_expected_result_count(size_t);
1648 // Return whether this is a call to the predeclared function
1649 // recover.
1650 bool
1651 is_recover_call() const;
1653 // Set the argument for a call to recover.
1654 void
1655 set_recover_arg(Expression*);
1657 // Whether the last argument is a varargs argument (f(a...)).
1658 bool
1659 is_varargs() const
1660 { return this->is_varargs_; }
1662 // Note that varargs have already been lowered.
1663 void
1664 set_varargs_are_lowered()
1665 { this->varargs_are_lowered_ = true; }
1667 // Note that it is OK for this call to set hidden fields when
1668 // passing arguments.
1669 void
1670 set_hidden_fields_are_ok()
1671 { this->are_hidden_fields_ok_ = true; }
1673 // Whether this call is being deferred.
1674 bool
1675 is_deferred() const
1676 { return this->is_deferred_; }
1678 // Note that the call is being deferred.
1679 void
1680 set_is_deferred()
1681 { this->is_deferred_ = true; }
1683 // We have found an error with this call expression; return true if
1684 // we should report it.
1685 bool
1686 issue_error();
1688 protected:
1690 do_traverse(Traverse*);
1692 virtual Expression*
1693 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1695 virtual Expression*
1696 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1698 bool
1699 do_discarding_value()
1700 { return true; }
1702 virtual Type*
1703 do_type();
1705 virtual void
1706 do_determine_type(const Type_context*);
1708 virtual void
1709 do_check_types(Gogo*);
1711 Expression*
1712 do_copy()
1714 return Expression::make_call(this->fn_->copy(),
1715 (this->args_ == NULL
1716 ? NULL
1717 : this->args_->copy()),
1718 this->is_varargs_, this->location());
1721 bool
1722 do_must_eval_in_order() const;
1724 virtual Bexpression*
1725 do_get_backend(Translate_context*);
1727 virtual bool
1728 do_is_recover_call() const;
1730 virtual void
1731 do_set_recover_arg(Expression*);
1733 // Let a builtin expression change the argument list.
1734 void
1735 set_args(Expression_list* args)
1736 { this->args_ = args; }
1738 // Let a builtin expression lower varargs.
1739 void
1740 lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
1741 Type* varargs_type, size_t param_count);
1743 // Let a builtin expression check whether types have been
1744 // determined.
1745 bool
1746 determining_types();
1748 void
1749 do_dump_expression(Ast_dump_context*) const;
1751 private:
1752 bool
1753 check_argument_type(int, const Type*, const Type*, Location, bool);
1755 Expression*
1756 interface_method_function(Interface_field_reference_expression*,
1757 Expression**);
1759 Bexpression*
1760 set_results(Translate_context*, Bexpression*);
1762 // The function to call.
1763 Expression* fn_;
1764 // The arguments to pass. This may be NULL if there are no
1765 // arguments.
1766 Expression_list* args_;
1767 // The type of the expression, to avoid recomputing it.
1768 Type* type_;
1769 // The list of temporaries which will hold the results if the
1770 // function returns a tuple.
1771 std::vector<Temporary_statement*>* results_;
1772 // The backend expression for the call, used for a call which returns a tuple.
1773 Bexpression* call_;
1774 // A temporary variable to store this call if the function returns a tuple.
1775 Temporary_statement* call_temp_;
1776 // If not 0, the number of results expected from this call, when
1777 // used in a context that expects multiple values.
1778 size_t expected_result_count_;
1779 // True if the last argument is a varargs argument (f(a...)).
1780 bool is_varargs_;
1781 // True if this statement may pass hidden fields in the arguments.
1782 // This is used for generated method stubs.
1783 bool are_hidden_fields_ok_;
1784 // True if varargs have already been lowered.
1785 bool varargs_are_lowered_;
1786 // True if types have been determined.
1787 bool types_are_determined_;
1788 // True if the call is an argument to a defer statement.
1789 bool is_deferred_;
1790 // True if we reported an error about a mismatch between call
1791 // results and uses. This is to avoid producing multiple errors
1792 // when there are multiple Call_result_expressions.
1793 bool issued_error_;
1796 // An expression which represents a pointer to a function.
1798 class Func_expression : public Expression
1800 public:
1801 Func_expression(Named_object* function, Expression* closure,
1802 Location location)
1803 : Expression(EXPRESSION_FUNC_REFERENCE, location),
1804 function_(function), closure_(closure)
1807 // Return the object associated with the function.
1808 Named_object*
1809 named_object() const
1810 { return this->function_; }
1812 // Return the closure for this function. This will return NULL if
1813 // the function has no closure, which is the normal case.
1814 Expression*
1815 closure()
1816 { return this->closure_; }
1818 // Return a backend expression for the code of a function.
1819 static Bexpression*
1820 get_code_pointer(Gogo*, Named_object* function, Location loc);
1822 protected:
1824 do_traverse(Traverse*);
1826 Type*
1827 do_type();
1829 void
1830 do_determine_type(const Type_context*)
1832 if (this->closure_ != NULL)
1833 this->closure_->determine_type_no_context();
1836 Expression*
1837 do_copy()
1839 return Expression::make_func_reference(this->function_,
1840 (this->closure_ == NULL
1841 ? NULL
1842 : this->closure_->copy()),
1843 this->location());
1846 Bexpression*
1847 do_get_backend(Translate_context*);
1849 void
1850 do_dump_expression(Ast_dump_context*) const;
1852 private:
1853 // The function itself.
1854 Named_object* function_;
1855 // A closure. This is normally NULL. For a nested function, it may
1856 // be a struct holding pointers to all the variables referenced by
1857 // this function and defined in enclosing functions.
1858 Expression* closure_;
1861 // A function descriptor. A function descriptor is a struct with a
1862 // single field pointing to the function code. This is used for
1863 // functions without closures.
1865 class Func_descriptor_expression : public Expression
1867 public:
1868 Func_descriptor_expression(Named_object* fn);
1870 // Make the function descriptor type, so that it can be converted.
1871 static void
1872 make_func_descriptor_type();
1874 protected:
1876 do_traverse(Traverse*);
1878 Type*
1879 do_type();
1881 void
1882 do_determine_type(const Type_context*)
1885 Expression*
1886 do_copy()
1887 { return Expression::make_func_descriptor(this->fn_); }
1889 bool
1890 do_is_addressable() const
1891 { return true; }
1893 Bexpression*
1894 do_get_backend(Translate_context*);
1896 void
1897 do_dump_expression(Ast_dump_context* context) const;
1899 private:
1900 // The type of all function descriptors.
1901 static Type* descriptor_type;
1903 // The function for which this is the descriptor.
1904 Named_object* fn_;
1905 // The descriptor variable.
1906 Bvariable* dvar_;
1909 // A reference to an unknown name.
1911 class Unknown_expression : public Parser_expression
1913 public:
1914 Unknown_expression(Named_object* named_object, Location location)
1915 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
1916 named_object_(named_object), no_error_message_(false),
1917 is_composite_literal_key_(false)
1920 // The associated named object.
1921 Named_object*
1922 named_object() const
1923 { return this->named_object_; }
1925 // The name of the identifier which was unknown.
1926 const std::string&
1927 name() const;
1929 // Call this to indicate that we should not give an error if this
1930 // name is never defined. This is used to avoid knock-on errors
1931 // during an erroneous parse.
1932 void
1933 set_no_error_message()
1934 { this->no_error_message_ = true; }
1936 // Note that this expression is being used as the key in a composite
1937 // literal, so it may be OK if it is not resolved.
1938 void
1939 set_is_composite_literal_key()
1940 { this->is_composite_literal_key_ = true; }
1942 // Note that this expression should no longer be treated as a
1943 // composite literal key.
1944 void
1945 clear_is_composite_literal_key()
1946 { this->is_composite_literal_key_ = false; }
1948 protected:
1949 Expression*
1950 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1952 Expression*
1953 do_copy()
1954 { return new Unknown_expression(this->named_object_, this->location()); }
1956 void
1957 do_dump_expression(Ast_dump_context*) const;
1959 private:
1960 // The unknown name.
1961 Named_object* named_object_;
1962 // True if we should not give errors if this is undefined. This is
1963 // used if there was a parse failure.
1964 bool no_error_message_;
1965 // True if this is the key in a composite literal.
1966 bool is_composite_literal_key_;
1969 // An index expression. This is lowered to an array index, a string
1970 // index, or a map index.
1972 class Index_expression : public Parser_expression
1974 public:
1975 Index_expression(Expression* left, Expression* start, Expression* end,
1976 Expression* cap, Location location)
1977 : Parser_expression(EXPRESSION_INDEX, location),
1978 left_(left), start_(start), end_(end), cap_(cap), is_lvalue_(false)
1981 // Record that this expression is an lvalue.
1982 void
1983 set_is_lvalue()
1984 { this->is_lvalue_ = true; }
1986 // Dump an index expression, i.e. an expression of the form
1987 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
1988 static void
1989 dump_index_expression(Ast_dump_context*, const Expression* expr,
1990 const Expression* start, const Expression* end,
1991 const Expression* cap);
1993 protected:
1995 do_traverse(Traverse*);
1997 Expression*
1998 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2000 Expression*
2001 do_copy()
2003 return new Index_expression(this->left_->copy(), this->start_->copy(),
2004 (this->end_ == NULL
2005 ? NULL
2006 : this->end_->copy()),
2007 (this->cap_ == NULL
2008 ? NULL
2009 : this->cap_->copy()),
2010 this->location());
2013 bool
2014 do_must_eval_subexpressions_in_order(int* skip) const
2016 *skip = 1;
2017 return true;
2020 void
2021 do_dump_expression(Ast_dump_context*) const;
2023 void
2024 do_issue_nil_check()
2025 { this->left_->issue_nil_check(); }
2026 private:
2027 // The expression being indexed.
2028 Expression* left_;
2029 // The first index.
2030 Expression* start_;
2031 // The second index. This is NULL for an index, non-NULL for a
2032 // slice.
2033 Expression* end_;
2034 // The capacity argument. This is NULL for indices and slices that use the
2035 // default capacity, non-NULL for indices and slices that specify the
2036 // capacity.
2037 Expression* cap_;
2038 // Whether this is being used as an l-value. We set this during the
2039 // parse because map index expressions need to know.
2040 bool is_lvalue_;
2043 // An index into a map.
2045 class Map_index_expression : public Expression
2047 public:
2048 Map_index_expression(Expression* map, Expression* index,
2049 Location location)
2050 : Expression(EXPRESSION_MAP_INDEX, location),
2051 map_(map), index_(index), is_lvalue_(false),
2052 is_in_tuple_assignment_(false), value_pointer_(NULL)
2055 // Return the map.
2056 Expression*
2057 map()
2058 { return this->map_; }
2060 const Expression*
2061 map() const
2062 { return this->map_; }
2064 // Return the index.
2065 Expression*
2066 index()
2067 { return this->index_; }
2069 const Expression*
2070 index() const
2071 { return this->index_; }
2073 // Get the type of the map being indexed.
2074 Map_type*
2075 get_map_type() const;
2077 // Record that this map expression is an lvalue. The difference is
2078 // that an lvalue always inserts the key.
2079 void
2080 set_is_lvalue()
2081 { this->is_lvalue_ = true; }
2083 // Return whether this map expression occurs in an assignment to a
2084 // pair of values.
2085 bool
2086 is_in_tuple_assignment() const
2087 { return this->is_in_tuple_assignment_; }
2089 // Record that this map expression occurs in an assignment to a pair
2090 // of values.
2091 void
2092 set_is_in_tuple_assignment()
2093 { this->is_in_tuple_assignment_ = true; }
2095 // Return an expression for the map index. This returns an expression which
2096 // evaluates to a pointer to a value in the map. If INSERT is true,
2097 // the key will be inserted if not present, and the value pointer
2098 // will be zero initialized. If INSERT is false, and the key is not
2099 // present in the map, the pointer will be NULL.
2100 Expression*
2101 get_value_pointer(bool insert);
2103 protected:
2105 do_traverse(Traverse*);
2107 Expression*
2108 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2110 Type*
2111 do_type();
2113 void
2114 do_determine_type(const Type_context*);
2116 void
2117 do_check_types(Gogo*);
2119 Expression*
2120 do_copy()
2122 return Expression::make_map_index(this->map_->copy(),
2123 this->index_->copy(),
2124 this->location());
2127 bool
2128 do_must_eval_subexpressions_in_order(int* skip) const
2130 *skip = 1;
2131 return true;
2134 // A map index expression is an lvalue but it is not addressable.
2136 Bexpression*
2137 do_get_backend(Translate_context*);
2139 void
2140 do_dump_expression(Ast_dump_context*) const;
2142 private:
2143 // The map we are looking into.
2144 Expression* map_;
2145 // The index.
2146 Expression* index_;
2147 // Whether this is an lvalue.
2148 bool is_lvalue_;
2149 // Whether this is in a tuple assignment to a pair of values.
2150 bool is_in_tuple_assignment_;
2151 // A pointer to the value at this index.
2152 Expression* value_pointer_;
2155 // An expression which represents a method bound to its first
2156 // argument.
2158 class Bound_method_expression : public Expression
2160 public:
2161 Bound_method_expression(Expression* expr, const Method *method,
2162 Named_object* function, Location location)
2163 : Expression(EXPRESSION_BOUND_METHOD, location),
2164 expr_(expr), expr_type_(NULL), method_(method), function_(function)
2167 // Return the object which is the first argument.
2168 Expression*
2169 first_argument()
2170 { return this->expr_; }
2172 // Return the implicit type of the first argument. This will be
2173 // non-NULL when using a method from an anonymous field without
2174 // using an explicit stub.
2175 Type*
2176 first_argument_type() const
2177 { return this->expr_type_; }
2179 // Return the method.
2180 const Method*
2181 method() const
2182 { return this->method_; }
2184 // Return the function to call.
2185 Named_object*
2186 function() const
2187 { return this->function_; }
2189 // Set the implicit type of the expression.
2190 void
2191 set_first_argument_type(Type* type)
2192 { this->expr_type_ = type; }
2194 // Create a thunk to call FUNCTION, for METHOD, when it is used as
2195 // part of a method value.
2196 static Named_object*
2197 create_thunk(Gogo*, const Method* method, Named_object* function);
2199 protected:
2201 do_traverse(Traverse*);
2203 Expression*
2204 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2206 Type*
2207 do_type();
2209 void
2210 do_determine_type(const Type_context*);
2212 void
2213 do_check_types(Gogo*);
2215 Expression*
2216 do_copy()
2218 return new Bound_method_expression(this->expr_->copy(), this->method_,
2219 this->function_, this->location());
2222 Bexpression*
2223 do_get_backend(Translate_context*);
2225 void
2226 do_dump_expression(Ast_dump_context*) const;
2228 private:
2229 // A mapping from method functions to the thunks we have created for
2230 // them.
2231 typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
2232 static Method_value_thunks method_value_thunks;
2234 // The object used to find the method. This is passed to the method
2235 // as the first argument.
2236 Expression* expr_;
2237 // The implicit type of the object to pass to the method. This is
2238 // NULL in the normal case, non-NULL when using a method from an
2239 // anonymous field which does not require a stub.
2240 Type* expr_type_;
2241 // The method.
2242 const Method* method_;
2243 // The function to call. This is not the same as
2244 // method_->named_object() when the method has a stub. This will be
2245 // the real function rather than the stub.
2246 Named_object* function_;
2249 // A reference to a field in a struct.
2251 class Field_reference_expression : public Expression
2253 public:
2254 Field_reference_expression(Expression* expr, unsigned int field_index,
2255 Location location)
2256 : Expression(EXPRESSION_FIELD_REFERENCE, location),
2257 expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
2260 // Return the struct expression.
2261 Expression*
2262 expr() const
2263 { return this->expr_; }
2265 // Return the field index.
2266 unsigned int
2267 field_index() const
2268 { return this->field_index_; }
2270 // Return whether this node was implied by an anonymous field.
2271 bool
2272 implicit() const
2273 { return this->implicit_; }
2275 void
2276 set_implicit(bool implicit)
2277 { this->implicit_ = implicit; }
2279 // Set the struct expression. This is used when parsing.
2280 void
2281 set_struct_expression(Expression* expr)
2283 go_assert(this->expr_ == NULL);
2284 this->expr_ = expr;
2287 protected:
2289 do_traverse(Traverse* traverse)
2290 { return Expression::traverse(&this->expr_, traverse); }
2292 Expression*
2293 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2295 Type*
2296 do_type();
2298 void
2299 do_determine_type(const Type_context*)
2300 { this->expr_->determine_type_no_context(); }
2302 void
2303 do_check_types(Gogo*);
2305 Expression*
2306 do_copy()
2308 return Expression::make_field_reference(this->expr_->copy(),
2309 this->field_index_,
2310 this->location());
2313 bool
2314 do_is_addressable() const
2315 { return this->expr_->is_addressable(); }
2317 void
2318 do_address_taken(bool escapes)
2319 { this->expr_->address_taken(escapes); }
2321 void
2322 do_issue_nil_check()
2323 { this->expr_->issue_nil_check(); }
2325 Bexpression*
2326 do_get_backend(Translate_context*);
2328 void
2329 do_dump_expression(Ast_dump_context*) const;
2331 private:
2332 // The expression we are looking into. This should have a type of
2333 // struct.
2334 Expression* expr_;
2335 // The zero-based index of the field we are retrieving.
2336 unsigned int field_index_;
2337 // Whether this node was emitted implicitly for an embedded field,
2338 // that is, expr_ is not the expr_ of the original user node.
2339 bool implicit_;
2340 // Whether we have already emitted a fieldtrack call.
2341 bool called_fieldtrack_;
2344 // A reference to a field of an interface.
2346 class Interface_field_reference_expression : public Expression
2348 public:
2349 Interface_field_reference_expression(Expression* expr,
2350 const std::string& name,
2351 Location location)
2352 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
2353 expr_(expr), name_(name)
2356 // Return the expression for the interface object.
2357 Expression*
2358 expr()
2359 { return this->expr_; }
2361 // Return the name of the method to call.
2362 const std::string&
2363 name() const
2364 { return this->name_; }
2366 // Create a thunk to call the method NAME in TYPE when it is used as
2367 // part of a method value.
2368 static Named_object*
2369 create_thunk(Gogo*, Interface_type* type, const std::string& name);
2371 // Return an expression for the pointer to the function to call.
2372 Expression*
2373 get_function();
2375 // Return an expression for the first argument to pass to the interface
2376 // function. This is the real object associated with the interface object.
2377 Expression*
2378 get_underlying_object();
2380 protected:
2382 do_traverse(Traverse* traverse);
2384 Expression*
2385 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2387 Type*
2388 do_type();
2390 void
2391 do_determine_type(const Type_context*);
2393 void
2394 do_check_types(Gogo*);
2396 Expression*
2397 do_copy()
2399 return Expression::make_interface_field_reference(this->expr_->copy(),
2400 this->name_,
2401 this->location());
2404 Bexpression*
2405 do_get_backend(Translate_context*);
2407 void
2408 do_dump_expression(Ast_dump_context*) const;
2410 private:
2411 // A mapping from interface types to a list of thunks we have
2412 // created for methods.
2413 typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
2414 typedef Unordered_map(Interface_type*, Method_thunks*)
2415 Interface_method_thunks;
2416 static Interface_method_thunks interface_method_thunks;
2418 // The expression for the interface object. This should have a type
2419 // of interface or pointer to interface.
2420 Expression* expr_;
2421 // The field we are retrieving--the name of the method.
2422 std::string name_;
2425 // A type guard expression.
2427 class Type_guard_expression : public Expression
2429 public:
2430 Type_guard_expression(Expression* expr, Type* type, Location location)
2431 : Expression(EXPRESSION_TYPE_GUARD, location),
2432 expr_(expr), type_(type)
2435 // Return the expression to convert.
2436 Expression*
2437 expr()
2438 { return this->expr_; }
2440 // Return the type to which to convert.
2441 Type*
2442 type()
2443 { return this->type_; }
2445 protected:
2447 do_traverse(Traverse* traverse);
2449 Expression*
2450 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2452 Type*
2453 do_type()
2454 { return this->type_; }
2456 void
2457 do_determine_type(const Type_context*)
2458 { this->expr_->determine_type_no_context(); }
2460 void
2461 do_check_types(Gogo*);
2463 Expression*
2464 do_copy()
2466 return new Type_guard_expression(this->expr_->copy(), this->type_,
2467 this->location());
2470 Bexpression*
2471 do_get_backend(Translate_context*);
2473 void
2474 do_dump_expression(Ast_dump_context*) const;
2476 private:
2477 // The expression to convert.
2478 Expression* expr_;
2479 // The type to which to convert.
2480 Type* type_;
2483 // A receive expression.
2485 class Receive_expression : public Expression
2487 public:
2488 Receive_expression(Expression* channel, Location location)
2489 : Expression(EXPRESSION_RECEIVE, location),
2490 channel_(channel), temp_receiver_(NULL)
2493 // Return the channel.
2494 Expression*
2495 channel()
2496 { return this->channel_; }
2498 protected:
2500 do_traverse(Traverse* traverse)
2501 { return Expression::traverse(&this->channel_, traverse); }
2503 bool
2504 do_discarding_value()
2505 { return true; }
2507 Type*
2508 do_type();
2510 Expression*
2511 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2513 void
2514 do_determine_type(const Type_context*)
2515 { this->channel_->determine_type_no_context(); }
2517 void
2518 do_check_types(Gogo*);
2520 Expression*
2521 do_copy()
2523 return Expression::make_receive(this->channel_->copy(), this->location());
2526 bool
2527 do_must_eval_in_order() const
2528 { return true; }
2530 Bexpression*
2531 do_get_backend(Translate_context*);
2533 void
2534 do_dump_expression(Ast_dump_context*) const;
2536 private:
2537 // The channel from which we are receiving.
2538 Expression* channel_;
2539 // A temporary reference to the variable storing the received data.
2540 Temporary_statement* temp_receiver_;
2543 // A numeric constant. This is used both for untyped constants and
2544 // for constants that have a type.
2546 class Numeric_constant
2548 public:
2549 Numeric_constant()
2550 : classification_(NC_INVALID), type_(NULL)
2553 ~Numeric_constant();
2555 Numeric_constant(const Numeric_constant&);
2557 Numeric_constant& operator=(const Numeric_constant&);
2559 // Set to an unsigned long value.
2560 void
2561 set_unsigned_long(Type*, unsigned long);
2563 // Set to an integer value.
2564 void
2565 set_int(Type*, const mpz_t);
2567 // Set to a rune value.
2568 void
2569 set_rune(Type*, const mpz_t);
2571 // Set to a floating point value.
2572 void
2573 set_float(Type*, const mpfr_t);
2575 // Set to a complex value.
2576 void
2577 set_complex(Type*, const mpfr_t, const mpfr_t);
2579 // Classifiers.
2580 bool
2581 is_int() const
2582 { return this->classification_ == Numeric_constant::NC_INT; }
2584 bool
2585 is_rune() const
2586 { return this->classification_ == Numeric_constant::NC_RUNE; }
2588 bool
2589 is_float() const
2590 { return this->classification_ == Numeric_constant::NC_FLOAT; }
2592 bool
2593 is_complex() const
2594 { return this->classification_ == Numeric_constant::NC_COMPLEX; }
2596 // Value retrievers. These will initialize the values as well as
2597 // set them. GET_INT is only valid if IS_INT returns true, and
2598 // likewise respectively.
2599 void
2600 get_int(mpz_t*) const;
2602 void
2603 get_rune(mpz_t*) const;
2605 void
2606 get_float(mpfr_t*) const;
2608 void
2609 get_complex(mpfr_t*, mpfr_t*) const;
2611 // Codes returned by to_unsigned_long.
2612 enum To_unsigned_long
2614 // Value is integer and fits in unsigned long.
2615 NC_UL_VALID,
2616 // Value is not integer.
2617 NC_UL_NOTINT,
2618 // Value is integer but is negative.
2619 NC_UL_NEGATIVE,
2620 // Value is non-negative integer but does not fit in unsigned
2621 // long.
2622 NC_UL_BIG
2625 // If the value can be expressed as an integer that fits in an
2626 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
2627 // one of the other To_unsigned_long codes.
2628 To_unsigned_long
2629 to_unsigned_long(unsigned long* val) const;
2631 // If the value can be expressed as an int, return true and
2632 // initialize and set VAL. This will return false for a value with
2633 // an explicit float or complex type, even if the value is integral.
2634 bool
2635 to_int(mpz_t* val) const;
2637 // If the value can be expressed as a float, return true and
2638 // initialize and set VAL.
2639 bool
2640 to_float(mpfr_t* val) const;
2642 // If the value can be expressed as a complex, return true and
2643 // initialize and set VR and VI.
2644 bool
2645 to_complex(mpfr_t* vr, mpfr_t* vi) const;
2647 // Get the type.
2648 Type*
2649 type() const;
2651 // If the constant can be expressed in TYPE, then set the type of
2652 // the constant to TYPE and return true. Otherwise return false,
2653 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
2654 // the location to use for the error.
2655 bool
2656 set_type(Type* type, bool issue_error, Location location);
2658 // Return an Expression for this value.
2659 Expression*
2660 expression(Location) const;
2662 private:
2663 void
2664 clear();
2666 To_unsigned_long
2667 mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
2669 To_unsigned_long
2670 mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
2672 bool
2673 check_int_type(Integer_type*, bool, Location) const;
2675 bool
2676 check_float_type(Float_type*, bool, Location);
2678 bool
2679 check_complex_type(Complex_type*, bool, Location);
2681 // The kinds of constants.
2682 enum Classification
2684 NC_INVALID,
2685 NC_RUNE,
2686 NC_INT,
2687 NC_FLOAT,
2688 NC_COMPLEX
2691 // The kind of constant.
2692 Classification classification_;
2693 // The value.
2694 union
2696 // If NC_INT or NC_RUNE.
2697 mpz_t int_val;
2698 // If NC_FLOAT.
2699 mpfr_t float_val;
2700 // If NC_COMPLEX.
2701 struct
2703 mpfr_t real;
2704 mpfr_t imag;
2705 } complex_val;
2706 } u_;
2707 // The type if there is one. This will be NULL for an untyped
2708 // constant.
2709 Type* type_;
2712 #endif // !defined(GO_EXPRESSIONS_H)