compiler: identify array index expressions in lvalue context
[official-gcc.git] / gcc / go / gofrontend / expressions.h
blob5567605b151af91193c4429c43f38a608138ba47
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>
11 #include <mpc.h>
13 #include "operator.h"
14 #include "runtime.h"
16 class Gogo;
17 class Translate_context;
18 class Traverse;
19 class Statement_inserter;
20 class Type;
21 class Method;
22 struct Type_context;
23 class Integer_type;
24 class Float_type;
25 class Complex_type;
26 class Function_type;
27 class Map_type;
28 class Struct_type;
29 class Struct_field;
30 class Expression_list;
31 class Var_expression;
32 class Enclosed_var_expression;
33 class Temporary_reference_expression;
34 class Set_and_use_temporary_expression;
35 class String_expression;
36 class Type_conversion_expression;
37 class Unsafe_type_conversion_expression;
38 class Unary_expression;
39 class Binary_expression;
40 class String_concat_expression;
41 class Call_expression;
42 class Call_result_expression;
43 class Func_expression;
44 class Func_descriptor_expression;
45 class Unknown_expression;
46 class Index_expression;
47 class Array_index_expression;
48 class String_index_expression;
49 class Map_index_expression;
50 class Bound_method_expression;
51 class Field_reference_expression;
52 class Interface_field_reference_expression;
53 class Allocation_expression;
54 class Composite_literal_expression;
55 class Struct_construction_expression;
56 class Array_construction_expression;
57 class Fixed_array_construction_expression;
58 class Slice_construction_expression;
59 class Map_construction_expression;
60 class Type_guard_expression;
61 class Heap_expression;
62 class Receive_expression;
63 class Conditional_expression;
64 class Compound_expression;
65 class Numeric_constant;
66 class Named_object;
67 class Export;
68 class Import;
69 class Temporary_statement;
70 class Label;
71 class Ast_dump_context;
72 class String_dump;
74 // The precision to use for complex values represented as an mpc_t.
75 const int mpc_precision = 256;
77 // The base class for all expressions.
79 class Expression
81 public:
82 // The types of expressions.
83 enum Expression_classification
85 EXPRESSION_ERROR,
86 EXPRESSION_TYPE,
87 EXPRESSION_UNARY,
88 EXPRESSION_BINARY,
89 EXPRESSION_STRING_CONCAT,
90 EXPRESSION_CONST_REFERENCE,
91 EXPRESSION_VAR_REFERENCE,
92 EXPRESSION_ENCLOSED_VAR_REFERENCE,
93 EXPRESSION_TEMPORARY_REFERENCE,
94 EXPRESSION_SET_AND_USE_TEMPORARY,
95 EXPRESSION_SINK,
96 EXPRESSION_FUNC_REFERENCE,
97 EXPRESSION_FUNC_DESCRIPTOR,
98 EXPRESSION_FUNC_CODE_REFERENCE,
99 EXPRESSION_UNKNOWN_REFERENCE,
100 EXPRESSION_BOOLEAN,
101 EXPRESSION_STRING,
102 EXPRESSION_STRING_INFO,
103 EXPRESSION_INTEGER,
104 EXPRESSION_FLOAT,
105 EXPRESSION_COMPLEX,
106 EXPRESSION_NIL,
107 EXPRESSION_IOTA,
108 EXPRESSION_CALL,
109 EXPRESSION_CALL_RESULT,
110 EXPRESSION_BOUND_METHOD,
111 EXPRESSION_INDEX,
112 EXPRESSION_ARRAY_INDEX,
113 EXPRESSION_STRING_INDEX,
114 EXPRESSION_MAP_INDEX,
115 EXPRESSION_SELECTOR,
116 EXPRESSION_FIELD_REFERENCE,
117 EXPRESSION_INTERFACE_FIELD_REFERENCE,
118 EXPRESSION_ALLOCATION,
119 EXPRESSION_TYPE_GUARD,
120 EXPRESSION_CONVERSION,
121 EXPRESSION_UNSAFE_CONVERSION,
122 EXPRESSION_STRUCT_CONSTRUCTION,
123 EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
124 EXPRESSION_SLICE_CONSTRUCTION,
125 EXPRESSION_MAP_CONSTRUCTION,
126 EXPRESSION_COMPOSITE_LITERAL,
127 EXPRESSION_HEAP,
128 EXPRESSION_RECEIVE,
129 EXPRESSION_TYPE_DESCRIPTOR,
130 EXPRESSION_GC_SYMBOL,
131 EXPRESSION_PTRMASK_SYMBOL,
132 EXPRESSION_TYPE_INFO,
133 EXPRESSION_SLICE_INFO,
134 EXPRESSION_SLICE_VALUE,
135 EXPRESSION_INTERFACE_INFO,
136 EXPRESSION_INTERFACE_VALUE,
137 EXPRESSION_INTERFACE_MTABLE,
138 EXPRESSION_STRUCT_FIELD_OFFSET,
139 EXPRESSION_LABEL_ADDR,
140 EXPRESSION_CONDITIONAL,
141 EXPRESSION_COMPOUND,
142 EXPRESSION_BACKEND
145 Expression(Expression_classification, Location);
147 virtual ~Expression();
149 // Make an error expression. This is used when a parse error occurs
150 // to prevent cascading errors.
151 static Expression*
152 make_error(Location);
154 // Make an expression which is really a type. This is used during
155 // parsing.
156 static Expression*
157 make_type(Type*, Location);
159 // Make a unary expression.
160 static Expression*
161 make_unary(Operator, Expression*, Location);
163 // Make a binary expression.
164 static Expression*
165 make_binary(Operator, Expression*, Expression*, Location);
167 // Make a string concatenation expression.
168 static Expression*
169 make_string_concat(Expression_list*);
171 // Make a reference to a constant in an expression.
172 static Expression*
173 make_const_reference(Named_object*, Location);
175 // Make a reference to a variable in an expression.
176 static Expression*
177 make_var_reference(Named_object*, Location);
179 // Make a reference to a variable within an enclosing function.
180 static Expression*
181 make_enclosing_var_reference(Expression*, Named_object*, Location);
183 // Make a reference to a temporary variable. Temporary variables
184 // are always created by a single statement, which is what we use to
185 // refer to them.
186 static Temporary_reference_expression*
187 make_temporary_reference(Temporary_statement*, Location);
189 // Make an expressions which sets a temporary variable and then
190 // evaluates to a reference to that temporary variable. This is
191 // used to set a temporary variable while retaining the order of
192 // evaluation.
193 static Set_and_use_temporary_expression*
194 make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
196 // Make a sink expression--a reference to the blank identifier _.
197 static Expression*
198 make_sink(Location);
200 // Make a reference to a function in an expression. This returns a
201 // pointer to the struct holding the address of the function
202 // followed by any closed-over variables.
203 static Expression*
204 make_func_reference(Named_object*, Expression* closure, Location);
206 // Make a function descriptor, an immutable struct with a single
207 // field that points to the function code. This may only be used
208 // with functions that do not have closures. FN is the function for
209 // which we are making the descriptor.
210 static Func_descriptor_expression*
211 make_func_descriptor(Named_object* fn);
213 // Make a reference to the code of a function. This is used to set
214 // descriptor and closure fields.
215 static Expression*
216 make_func_code_reference(Named_object*, Location);
218 // Make a reference to an unknown name. In a correct program this
219 // will always be lowered to a real const/var/func reference.
220 static Unknown_expression*
221 make_unknown_reference(Named_object*, Location);
223 // Make a constant bool expression.
224 static Expression*
225 make_boolean(bool val, Location);
227 // Make a constant string expression.
228 static Expression*
229 make_string(const std::string&, Location);
231 // Make an expression that evaluates to some characteristic of an string.
232 // For simplicity, the enum values must match the field indexes in the
233 // underlying struct.
234 enum String_info
236 // The underlying data in the string.
237 STRING_INFO_DATA,
238 // The length of the string.
239 STRING_INFO_LENGTH
242 static Expression*
243 make_string_info(Expression* string, String_info, Location);
245 // Make a character constant expression. TYPE should be NULL for an
246 // abstract type.
247 static Expression*
248 make_character(const mpz_t*, Type*, Location);
250 // Make a constant integer expression from a multi-precision
251 // integer. TYPE should be NULL for an abstract type.
252 static Expression*
253 make_integer_z(const mpz_t*, Type*, Location);
255 // Make a constant integer expression from an unsigned long. TYPE
256 // should be NULL for an abstract type.
257 static Expression*
258 make_integer_ul(unsigned long, Type*, Location);
260 // Make a constant integer expression from a signed long. TYPE
261 // should be NULL for an abstract type.
262 static Expression*
263 make_integer_sl(long, Type*, Location);
265 // Make a constant integer expression from an int64_t. TYPE should
266 // be NULL for an abstract type.
267 static Expression*
268 make_integer_int64(int64_t, Type*, Location);
270 // Make a constant float expression. TYPE should be NULL for an
271 // abstract type.
272 static Expression*
273 make_float(const mpfr_t*, Type*, Location);
275 // Make a constant complex expression. TYPE should be NULL for an
276 // abstract type.
277 static Expression*
278 make_complex(const mpc_t*, Type*, Location);
280 // Make a nil expression.
281 static Expression*
282 make_nil(Location);
284 // Make an iota expression. This is used for the predeclared
285 // constant iota.
286 static Expression*
287 make_iota();
289 // Make a call expression.
290 static Call_expression*
291 make_call(Expression* func, Expression_list* args, bool is_varargs,
292 Location);
294 // Make a reference to a specific result of a call expression which
295 // returns a tuple.
296 static Expression*
297 make_call_result(Call_expression*, unsigned int index);
299 // Make an expression which is a method bound to its first
300 // parameter. METHOD is the method being called, FUNCTION is the
301 // function to call.
302 static Bound_method_expression*
303 make_bound_method(Expression* object, const Method* method,
304 Named_object* function, Location);
306 // Make an index or slice expression. This is a parser expression
307 // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
308 // index rather than a slice. CAP may be NULL, meaning we use the default
309 // capacity of LEFT. At parse time we may not know the type of LEFT.
310 // After parsing this is lowered to an array index, a string index,
311 // or a map index.
312 static Expression*
313 make_index(Expression* left, Expression* start, Expression* end,
314 Expression* cap, Location);
316 // Make an array index expression. END may be NULL, in which case
317 // this is an lvalue. CAP may be NULL, in which case it defaults
318 // to cap(ARRAY).
319 static Expression*
320 make_array_index(Expression* array, Expression* start, Expression* end,
321 Expression* cap, Location);
323 // Make a string index expression. END may be NULL. This is never
324 // an lvalue.
325 static Expression*
326 make_string_index(Expression* string, Expression* start, Expression* end,
327 Location);
329 // Make a map index expression. This is an lvalue.
330 static Map_index_expression*
331 make_map_index(Expression* map, Expression* val, Location);
333 // Make a selector. This is a parser expression which represents
334 // LEFT.NAME. At parse time we may not know the type of the left
335 // hand side.
336 static Expression*
337 make_selector(Expression* left, const std::string& name, Location);
339 // Make a reference to a field in a struct.
340 static Field_reference_expression*
341 make_field_reference(Expression*, unsigned int field_index, Location);
343 // Make a reference to a field of an interface, with an associated
344 // object.
345 static Expression*
346 make_interface_field_reference(Expression*, const std::string&,
347 Location);
349 // Make an allocation expression.
350 static Expression*
351 make_allocation(Type*, Location);
353 // Make a type guard expression.
354 static Expression*
355 make_type_guard(Expression*, Type*, Location);
357 // Make a type cast expression.
358 static Expression*
359 make_cast(Type*, Expression*, Location);
361 // Make an unsafe type cast expression. This is only used when
362 // passing parameter to builtin functions that are part of the Go
363 // runtime.
364 static Expression*
365 make_unsafe_cast(Type*, Expression*, Location);
367 // Make a composite literal. The DEPTH parameter is how far down we
368 // are in a list of composite literals with omitted types. HAS_KEYS
369 // is true if the expression list has keys alternating with values.
370 // ALL_ARE_NAMES is true if all the keys could be struct field
371 // names.
372 static Expression*
373 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
374 bool all_are_names, Location);
376 // Make a struct composite literal.
377 static Expression*
378 make_struct_composite_literal(Type*, Expression_list*, Location);
380 // Make an array composite literal.
381 static Expression*
382 make_array_composite_literal(Type*, Expression_list*, Location);
384 // Make a slice composite literal.
385 static Slice_construction_expression*
386 make_slice_composite_literal(Type*, Expression_list*, Location);
388 // Take an expression and allocate it on the heap.
389 static Expression*
390 make_heap_expression(Expression*, Location);
392 // Make a receive expression. VAL is NULL for a unary receive.
393 static Receive_expression*
394 make_receive(Expression* channel, Location);
396 // Make an expression which evaluates to the address of the type
397 // descriptor for TYPE.
398 static Expression*
399 make_type_descriptor(Type* type, Location);
401 // Make an expression which evaluates to the address of the gc
402 // symbol for TYPE.
403 static Expression*
404 make_gc_symbol(Type* type);
406 // Make an expression that evaluates to the address of a ptrmask
407 // symbol for TYPE. For most types this will be the same as
408 // make_gc_symbol, but for larger types make_gc_symbol will return a
409 // gcprog while this will return a ptrmask.
410 static Expression*
411 make_ptrmask_symbol(Type* type);
413 // Make an expression which evaluates to some characteristic of a
414 // type. These are only used for type descriptors, so there is no
415 // location parameter.
416 enum Type_info
418 // The size of a value of the type.
419 TYPE_INFO_SIZE,
420 // The required alignment of a value of the type.
421 TYPE_INFO_ALIGNMENT,
422 // The required alignment of a value of the type when used as a
423 // field in a struct.
424 TYPE_INFO_FIELD_ALIGNMENT,
425 // The size of the prefix of a value of the type that contains
426 // all the pointers. This is 0 for a type that contains no
427 // pointers. It is always <= TYPE_INFO_SIZE.
428 TYPE_INFO_BACKEND_PTRDATA,
429 // Like TYPE_INFO_BACKEND_PTRDATA, but the ptrdata value that we
430 // want to store in a type descriptor. They are the same for
431 // most types, but can differ for a type that uses a gcprog.
432 TYPE_INFO_DESCRIPTOR_PTRDATA
435 static Expression*
436 make_type_info(Type* type, Type_info);
438 // Make an expression that evaluates to some characteristic of a
439 // slice. For simplicity, the enum values must match the field indexes
440 // in the underlying struct.
441 enum Slice_info
443 // The underlying data of the slice.
444 SLICE_INFO_VALUE_POINTER,
445 // The length of the slice.
446 SLICE_INFO_LENGTH,
447 // The capacity of the slice.
448 SLICE_INFO_CAPACITY
451 static Expression*
452 make_slice_info(Expression* slice, Slice_info, Location);
454 // Make an expression for a slice value.
455 static Expression*
456 make_slice_value(Type*, Expression* valptr, Expression* len, Expression* cap,
457 Location);
459 // Make an expression that evaluates to some characteristic of an
460 // interface. For simplicity, the enum values must match the field indexes
461 // in the underlying struct.
462 enum Interface_info
464 // The type descriptor of an empty interface.
465 INTERFACE_INFO_TYPE_DESCRIPTOR = 0,
466 // The methods of an interface.
467 INTERFACE_INFO_METHODS = 0,
468 // The first argument to pass to an interface method.
469 INTERFACE_INFO_OBJECT
472 static Expression*
473 make_interface_info(Expression* iface, Interface_info, Location);
475 // Make an expression for an interface value.
476 static Expression*
477 make_interface_value(Type*, Expression*, Expression*, Location);
479 // Make an expression that builds a reference to the interface method table
480 // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
481 // reference to the interface method table for the pointer receiver type.
482 static Expression*
483 make_interface_mtable_ref(Interface_type* itype, Type* type,
484 bool is_pointer, Location);
486 // Make an expression which evaluates to the offset of a field in a
487 // struct. This is only used for type descriptors, so there is no
488 // location parameter.
489 static Expression*
490 make_struct_field_offset(Struct_type*, const Struct_field*);
492 // Make an expression which evaluates to the address of an unnamed
493 // label.
494 static Expression*
495 make_label_addr(Label*, Location);
497 // Make a conditional expression.
498 static Expression*
499 make_conditional(Expression*, Expression*, Expression*, Location);
501 // Make a compound expression.
502 static Expression*
503 make_compound(Expression*, Expression*, Location);
505 // Make a backend expression.
506 static Expression*
507 make_backend(Bexpression*, Type*, Location);
509 // Return the expression classification.
510 Expression_classification
511 classification() const
512 { return this->classification_; }
514 // Return the location of the expression.
515 Location
516 location() const
517 { return this->location_; }
519 // Return whether this is a constant expression.
520 bool
521 is_constant() const
522 { return this->do_is_constant(); }
524 // Return whether this expression can be used as a static
525 // initializer. This is true for an expression that has only
526 // numbers and pointers to global variables or composite literals
527 // that do not require runtime initialization. It is false if we
528 // must generate code to compute this expression when it is used to
529 // initialize a global variable. This is not a language-level
530 // concept, but an implementation-level one. If this expression is
531 // used to initialize a global variable, this is true if we can pass
532 // an initializer to the backend, false if we must generate code to
533 // initialize the variable. It is always safe for this method to
534 // return false, but the resulting code may be less efficient.
535 bool
536 is_static_initializer() const
537 { return this->do_is_static_initializer(); }
539 // If this is not a numeric constant, return false. If it is one,
540 // return true, and set VAL to hold the value.
541 bool
542 numeric_constant_value(Numeric_constant* val) const
543 { return this->do_numeric_constant_value(val); }
545 // If this is not a constant expression with string type, return
546 // false. If it is one, return true, and set VAL to the value.
547 bool
548 string_constant_value(std::string* val) const
549 { return this->do_string_constant_value(val); }
551 // This is called if the value of this expression is being
552 // discarded. This issues warnings about computed values being
553 // unused. This returns true if all is well, false if it issued an
554 // error message.
555 bool
556 discarding_value()
557 { return this->do_discarding_value(); }
559 // Return whether this is an error expression.
560 bool
561 is_error_expression() const
562 { return this->classification_ == EXPRESSION_ERROR; }
564 // Return whether this expression really represents a type.
565 bool
566 is_type_expression() const
567 { return this->classification_ == EXPRESSION_TYPE; }
569 // If this is a variable reference, return the Var_expression
570 // structure. Otherwise, return NULL. This is a controlled dynamic
571 // cast.
572 Var_expression*
573 var_expression()
574 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
576 const Var_expression*
577 var_expression() const
578 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
580 // If this is a enclosed_variable reference, return the
581 // Enclosed_var_expression structure. Otherwise, return NULL.
582 // This is a controlled dynamic cast.
583 Enclosed_var_expression*
584 enclosed_var_expression()
585 { return this->convert<Enclosed_var_expression,
586 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
588 const Enclosed_var_expression*
589 enclosed_var_expression() const
590 { return this->convert<const Enclosed_var_expression,
591 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
594 // If this is a reference to a temporary variable, return the
595 // Temporary_reference_expression. Otherwise, return NULL.
596 Temporary_reference_expression*
597 temporary_reference_expression()
599 return this->convert<Temporary_reference_expression,
600 EXPRESSION_TEMPORARY_REFERENCE>();
603 // If this is a set-and-use-temporary, return the
604 // Set_and_use_temporary_expression. Otherwise, return NULL.
605 Set_and_use_temporary_expression*
606 set_and_use_temporary_expression()
608 return this->convert<Set_and_use_temporary_expression,
609 EXPRESSION_SET_AND_USE_TEMPORARY>();
612 // Return whether this is a sink expression.
613 bool
614 is_sink_expression() const
615 { return this->classification_ == EXPRESSION_SINK; }
617 // If this is a string expression, return the String_expression
618 // structure. Otherwise, return NULL.
619 String_expression*
620 string_expression()
621 { return this->convert<String_expression, EXPRESSION_STRING>(); }
623 // If this is a conversion expression, return the Type_conversion_expression
624 // structure. Otherwise, return NULL.
625 Type_conversion_expression*
626 conversion_expression()
627 { return this->convert<Type_conversion_expression, EXPRESSION_CONVERSION>(); }
629 // If this is an unsafe conversion expression, return the
630 // Unsafe_type_conversion_expression structure. Otherwise, return NULL.
631 Unsafe_type_conversion_expression*
632 unsafe_conversion_expression()
634 return this->convert<Unsafe_type_conversion_expression,
635 EXPRESSION_UNSAFE_CONVERSION>();
638 // Return whether this is the expression nil.
639 bool
640 is_nil_expression() const
641 { return this->classification_ == EXPRESSION_NIL; }
643 // If this is an indirection through a pointer, return the
644 // expression being pointed through. Otherwise return this.
645 Expression*
646 deref();
648 // If this is a unary expression, return the Unary_expression
649 // structure. Otherwise return NULL.
650 Unary_expression*
651 unary_expression()
652 { return this->convert<Unary_expression, EXPRESSION_UNARY>(); }
654 // If this is a binary expression, return the Binary_expression
655 // structure. Otherwise return NULL.
656 Binary_expression*
657 binary_expression()
658 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
660 // If this is a string concatenation expression, return the
661 // String_concat_expression structure. Otherwise, return NULL.
662 String_concat_expression*
663 string_concat_expression()
665 return this->convert<String_concat_expression, EXPRESSION_STRING_CONCAT>();
668 // If this is a call expression, return the Call_expression
669 // structure. Otherwise, return NULL. This is a controlled dynamic
670 // cast.
671 Call_expression*
672 call_expression()
673 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
675 // If this is a call_result expression, return the Call_result_expression
676 // structure. Otherwise, return NULL. This is a controlled dynamic
677 // cast.
678 Call_result_expression*
679 call_result_expression()
680 { return this->convert<Call_result_expression, EXPRESSION_CALL_RESULT>(); }
682 // If this is an expression which refers to a function, return the
683 // Func_expression structure. Otherwise, return NULL.
684 Func_expression*
685 func_expression()
686 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
688 const Func_expression*
689 func_expression() const
690 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
692 // If this is an expression which refers to an unknown name, return
693 // the Unknown_expression structure. Otherwise, return NULL.
694 Unknown_expression*
695 unknown_expression()
696 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
698 const Unknown_expression*
699 unknown_expression() const
701 return this->convert<const Unknown_expression,
702 EXPRESSION_UNKNOWN_REFERENCE>();
705 // If this is an index expression, return the Index_expression
706 // structure. Otherwise, return NULL.
707 Index_expression*
708 index_expression()
709 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
711 // If this is an expression which refers to indexing in a array,
712 // return the Array_index_expression structure. Otherwise, return
713 // NULL.
714 Array_index_expression*
715 array_index_expression()
716 { return this->convert<Array_index_expression, EXPRESSION_ARRAY_INDEX>(); }
718 // If this is an expression which refers to indexing in a string,
719 // return the String_index_expression structure. Otherwise, return
720 // NULL.
721 String_index_expression*
722 string_index_expression()
723 { return this->convert<String_index_expression, EXPRESSION_STRING_INDEX>(); }
725 // If this is an expression which refers to indexing in a map,
726 // return the Map_index_expression structure. Otherwise, return
727 // NULL.
728 Map_index_expression*
729 map_index_expression()
730 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
732 // If this is a bound method expression, return the
733 // Bound_method_expression structure. Otherwise, return NULL.
734 Bound_method_expression*
735 bound_method_expression()
736 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
738 // If this is a reference to a field in a struct, return the
739 // Field_reference_expression structure. Otherwise, return NULL.
740 Field_reference_expression*
741 field_reference_expression()
743 return this->convert<Field_reference_expression,
744 EXPRESSION_FIELD_REFERENCE>();
747 // If this is a reference to a field in an interface, return the
748 // Interface_field_reference_expression structure. Otherwise,
749 // return NULL.
750 Interface_field_reference_expression*
751 interface_field_reference_expression()
753 return this->convert<Interface_field_reference_expression,
754 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
757 // If this is an allocation expression, return the Allocation_expression
758 // structure. Otherwise, return NULL.
759 Allocation_expression*
760 allocation_expression()
761 { return this->convert<Allocation_expression, EXPRESSION_ALLOCATION>(); }
763 // If this is a general composite literal, return the
764 // Composite_literal_expression structure. Otherwise, return NULL.
765 Composite_literal_expression*
766 complit()
768 return this->convert<Composite_literal_expression,
769 EXPRESSION_COMPOSITE_LITERAL>();
772 // If this is a struct composite literal, return the
773 // Struct_construction_expression structure. Otherwise, return NULL.
774 Struct_construction_expression*
775 struct_literal()
777 return this->convert<Struct_construction_expression,
778 EXPRESSION_STRUCT_CONSTRUCTION>();
781 // If this is a array composite literal, return the
782 // Array_construction_expression structure. Otherwise, return NULL.
783 Fixed_array_construction_expression*
784 array_literal()
786 return this->convert<Fixed_array_construction_expression,
787 EXPRESSION_FIXED_ARRAY_CONSTRUCTION>();
790 // If this is a slice composite literal, return the
791 // Slice_construction_expression structure. Otherwise, return NULL.
792 Slice_construction_expression*
793 slice_literal()
795 return this->convert<Slice_construction_expression,
796 EXPRESSION_SLICE_CONSTRUCTION>();
799 // If this is a map composite literal, return the
800 // Map_construction_expression structure. Otherwise, return NULL.
801 Map_construction_expression*
802 map_literal()
804 return this->convert<Map_construction_expression,
805 EXPRESSION_MAP_CONSTRUCTION>();
808 // If this is a type guard expression, return the
809 // Type_guard_expression structure. Otherwise, return NULL.
810 Type_guard_expression*
811 type_guard_expression()
812 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
814 // If this is a heap expression, returhn the Heap_expression structure.
815 // Otherwise, return NULL.
816 Heap_expression*
817 heap_expression()
818 { return this->convert<Heap_expression, EXPRESSION_HEAP>(); }
820 // If this is a receive expression, return the Receive_expression
821 // structure. Otherwise, return NULL.
822 Receive_expression*
823 receive_expression()
824 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
826 // If this is a conditional expression, return the Conditional_expression
827 // structure. Otherwise, return NULL.
828 Conditional_expression*
829 conditional_expression()
830 { return this->convert<Conditional_expression, EXPRESSION_CONDITIONAL>(); }
832 // If this is a compound expression, return the Compound_expression structure.
833 // Otherwise, return NULL.
834 Compound_expression*
835 compound_expression()
836 { return this->convert<Compound_expression, EXPRESSION_COMPOUND>(); }
838 // Return true if this is a composite literal.
839 bool
840 is_composite_literal() const;
842 // Return true if this is a composite literal which is not constant.
843 bool
844 is_nonconstant_composite_literal() const;
846 // Return true if this is a variable or temporary variable.
847 bool
848 is_variable() const;
850 // Return true if this is a reference to a local variable.
851 bool
852 is_local_variable() const;
854 // Make the builtin function descriptor type, so that it can be
855 // converted.
856 static void
857 make_func_descriptor_type();
859 // Traverse an expression.
860 static int
861 traverse(Expression**, Traverse*);
863 // Traverse subexpressions of this expression.
865 traverse_subexpressions(Traverse*);
867 // Lower an expression. This is called immediately after parsing.
868 // FUNCTION is the function we are in; it will be NULL for an
869 // expression initializing a global variable. INSERTER may be used
870 // to insert statements before the statement or initializer
871 // containing this expression; it is normally used to create
872 // temporary variables. IOTA_VALUE is the value that we should give
873 // to any iota expressions. This function must resolve expressions
874 // which could not be fully parsed into their final form. It
875 // returns the same Expression or a new one.
876 Expression*
877 lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
878 int iota_value)
879 { return this->do_lower(gogo, function, inserter, iota_value); }
881 // Flatten an expression. This is called after order_evaluation.
882 // FUNCTION is the function we are in; it will be NULL for an
883 // expression initializing a global variable. INSERTER may be used
884 // to insert statements before the statement or initializer
885 // containing this expression; it is normally used to create
886 // temporary variables. This function must resolve expressions
887 // which could not be fully parsed into their final form. It
888 // returns the same Expression or a new one.
889 Expression*
890 flatten(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
891 { return this->do_flatten(gogo, function, inserter); }
893 // Determine the real type of an expression with abstract integer,
894 // floating point, or complex type. TYPE_CONTEXT describes the
895 // expected type.
896 void
897 determine_type(const Type_context*);
899 // Check types in an expression.
900 void
901 check_types(Gogo* gogo)
902 { this->do_check_types(gogo); }
904 // Determine the type when there is no context.
905 void
906 determine_type_no_context();
908 // Return the current type of the expression. This may be changed
909 // by determine_type.
910 Type*
911 type()
912 { return this->do_type(); }
914 // Return a copy of an expression.
915 Expression*
916 copy()
917 { return this->do_copy(); }
919 // Return whether the expression is addressable--something which may
920 // be used as the operand of the unary & operator.
921 bool
922 is_addressable() const
923 { return this->do_is_addressable(); }
925 // Note that we are taking the address of this expression. ESCAPES
926 // is true if this address escapes the current function.
927 void
928 address_taken(bool escapes)
929 { this->do_address_taken(escapes); }
931 // Note that a nil check must be issued for this expression.
932 void
933 issue_nil_check()
934 { this->do_issue_nil_check(); }
936 // Return whether this expression must be evaluated in order
937 // according to the order of evaluation rules. This is basically
938 // true of all expressions with side-effects.
939 bool
940 must_eval_in_order() const
941 { return this->do_must_eval_in_order(); }
943 // Return whether subexpressions of this expression must be
944 // evaluated in order. This is true of index expressions and
945 // pointer indirections. This sets *SKIP to the number of
946 // subexpressions to skip during traversing, as index expressions
947 // only requiring moving the index, not the array.
948 bool
949 must_eval_subexpressions_in_order(int* skip) const
951 *skip = 0;
952 return this->do_must_eval_subexpressions_in_order(skip);
955 // Return the backend representation for this expression.
956 Bexpression*
957 get_backend(Translate_context*);
959 // Return an expression handling any conversions which must be done during
960 // assignment.
961 static Expression*
962 convert_for_assignment(Gogo*, Type* lhs_type, Expression* rhs,
963 Location location);
965 // Return an expression converting a value of one interface type to another
966 // interface type. If FOR_TYPE_GUARD is true this is for a type
967 // assertion.
968 static Expression*
969 convert_interface_to_interface(Type* lhs_type,
970 Expression* rhs, bool for_type_guard,
971 Location);
973 // Return a backend expression implementing the comparison LEFT OP RIGHT.
974 // TYPE is the type of both sides.
975 static Bexpression*
976 comparison(Translate_context*, Type* result_type, Operator op,
977 Expression* left, Expression* right, Location);
979 // Return the backend expression for the numeric constant VAL.
980 static Bexpression*
981 backend_numeric_constant_expression(Translate_context*,
982 Numeric_constant* val);
984 // Export the expression. This is only used for constants. It will
985 // be used for things like values of named constants and sizes of
986 // arrays.
987 void
988 export_expression(Export* exp) const
989 { this->do_export(exp); }
991 // Import an expression.
992 static Expression*
993 import_expression(Import*);
995 // Return an expression which checks that VAL, of arbitrary integer type,
996 // is non-negative and is not more than the maximum integer value.
997 static Expression*
998 check_bounds(Expression* val, Location);
1000 // Dump an expression to a dump constext.
1001 void
1002 dump_expression(Ast_dump_context*) const;
1004 protected:
1005 // May be implemented by child class: traverse the expressions.
1006 virtual int
1007 do_traverse(Traverse*);
1009 // Return a lowered expression.
1010 virtual Expression*
1011 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1012 { return this; }
1014 // Return a flattened expression.
1015 virtual Expression*
1016 do_flatten(Gogo*, Named_object*, Statement_inserter*)
1017 { return this; }
1020 // Return whether this is a constant expression.
1021 virtual bool
1022 do_is_constant() const
1023 { return false; }
1025 // Return whether this expression can be used as a constant
1026 // initializer.
1027 virtual bool
1028 do_is_static_initializer() const
1029 { return false; }
1031 // Return whether this is a constant expression of numeric type, and
1032 // set the Numeric_constant to the value.
1033 virtual bool
1034 do_numeric_constant_value(Numeric_constant*) const
1035 { return false; }
1037 // Return whether this is a constant expression of string type, and
1038 // set VAL to the value.
1039 virtual bool
1040 do_string_constant_value(std::string*) const
1041 { return false; }
1043 // Called by the parser if the value is being discarded.
1044 virtual bool
1045 do_discarding_value();
1047 // Child class holds type.
1048 virtual Type*
1049 do_type() = 0;
1051 // Child class implements determining type information.
1052 virtual void
1053 do_determine_type(const Type_context*) = 0;
1055 // Child class implements type checking if needed.
1056 virtual void
1057 do_check_types(Gogo*)
1060 // Child class implements copying.
1061 virtual Expression*
1062 do_copy() = 0;
1064 // Child class implements whether the expression is addressable.
1065 virtual bool
1066 do_is_addressable() const
1067 { return false; }
1069 // Child class implements taking the address of an expression.
1070 virtual void
1071 do_address_taken(bool)
1074 // Child class implements issuing a nil check if the address is taken.
1075 virtual void
1076 do_issue_nil_check()
1079 // Child class implements whether this expression must be evaluated
1080 // in order.
1081 virtual bool
1082 do_must_eval_in_order() const
1083 { return false; }
1085 // Child class implements whether this expressions requires that
1086 // subexpressions be evaluated in order. The child implementation
1087 // may set *SKIP if it should be non-zero.
1088 virtual bool
1089 do_must_eval_subexpressions_in_order(int* /* skip */) const
1090 { return false; }
1092 // Child class implements conversion to backend representation.
1093 virtual Bexpression*
1094 do_get_backend(Translate_context*) = 0;
1096 // Child class implements export.
1097 virtual void
1098 do_export(Export*) const;
1100 // For children to call to give an error for an unused value.
1101 void
1102 unused_value_error();
1104 // For children to call when they detect that they are in error.
1105 void
1106 set_is_error();
1108 // For children to call to report an error conveniently.
1109 void
1110 report_error(const char*);
1112 // Child class implements dumping to a dump context.
1113 virtual void
1114 do_dump_expression(Ast_dump_context*) const = 0;
1116 // Varargs lowering creates a slice object (unnamed compiler temp)
1117 // to contain the variable length collection of values. The enum
1118 // below tells the lowering routine whether it can mark that temp
1119 // as non-escaping or not. For general varargs calls it is not always
1120 // safe to stack-allocated the storage, but for specific cases (ex:
1121 // call to append()) it is legal.
1122 enum Slice_storage_escape_disp
1124 SLICE_STORAGE_MAY_ESCAPE,
1125 SLICE_STORAGE_DOES_NOT_ESCAPE
1128 private:
1129 // Convert to the desired statement classification, or return NULL.
1130 // This is a controlled dynamic cast.
1131 template<typename Expression_class,
1132 Expression_classification expr_classification>
1133 Expression_class*
1134 convert()
1136 return (this->classification_ == expr_classification
1137 ? static_cast<Expression_class*>(this)
1138 : NULL);
1141 template<typename Expression_class,
1142 Expression_classification expr_classification>
1143 const Expression_class*
1144 convert() const
1146 return (this->classification_ == expr_classification
1147 ? static_cast<const Expression_class*>(this)
1148 : NULL);
1151 static Expression*
1152 convert_type_to_interface(Type*, Expression*, Location);
1154 static Expression*
1155 get_interface_type_descriptor(Expression*);
1157 static Expression*
1158 convert_interface_to_type(Type*, Expression*, Location);
1160 // The expression classification.
1161 Expression_classification classification_;
1162 // The location in the input file.
1163 Location location_;
1166 // A list of Expressions.
1168 class Expression_list
1170 public:
1171 Expression_list()
1172 : entries_()
1175 // Return whether the list is empty.
1176 bool
1177 empty() const
1178 { return this->entries_.empty(); }
1180 // Return the number of entries in the list.
1181 size_t
1182 size() const
1183 { return this->entries_.size(); }
1185 // Add an entry to the end of the list.
1186 void
1187 push_back(Expression* expr)
1188 { this->entries_.push_back(expr); }
1190 void
1191 append(Expression_list* add)
1192 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
1194 // Reserve space in the list.
1195 void
1196 reserve(size_t size)
1197 { this->entries_.reserve(size); }
1199 // Traverse the expressions in the list.
1201 traverse(Traverse*);
1203 // Copy the list.
1204 Expression_list*
1205 copy();
1207 // Return true if the list contains an error expression.
1208 bool
1209 contains_error() const;
1211 // Retrieve an element by index.
1212 Expression*&
1213 at(size_t i)
1214 { return this->entries_.at(i); }
1216 // Return the first and last elements.
1217 Expression*&
1218 front()
1219 { return this->entries_.front(); }
1221 Expression*
1222 front() const
1223 { return this->entries_.front(); }
1225 Expression*&
1226 back()
1227 { return this->entries_.back(); }
1229 Expression*
1230 back() const
1231 { return this->entries_.back(); }
1233 // Iterators.
1235 typedef std::vector<Expression*>::iterator iterator;
1236 typedef std::vector<Expression*>::const_iterator const_iterator;
1238 iterator
1239 begin()
1240 { return this->entries_.begin(); }
1242 const_iterator
1243 begin() const
1244 { return this->entries_.begin(); }
1246 iterator
1247 end()
1248 { return this->entries_.end(); }
1250 const_iterator
1251 end() const
1252 { return this->entries_.end(); }
1254 // Erase an entry.
1255 void
1256 erase(iterator p)
1257 { this->entries_.erase(p); }
1259 private:
1260 std::vector<Expression*> entries_;
1263 // An abstract base class for an expression which is only used by the
1264 // parser, and is lowered in the lowering pass.
1266 class Parser_expression : public Expression
1268 public:
1269 Parser_expression(Expression_classification classification,
1270 Location location)
1271 : Expression(classification, location)
1274 protected:
1275 virtual Expression*
1276 do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
1278 Type*
1279 do_type();
1281 void
1282 do_determine_type(const Type_context*)
1283 { go_unreachable(); }
1285 void
1286 do_check_types(Gogo*)
1287 { go_unreachable(); }
1289 Bexpression*
1290 do_get_backend(Translate_context*)
1291 { go_unreachable(); }
1294 // An expression which is simply a variable.
1296 class Var_expression : public Expression
1298 public:
1299 Var_expression(Named_object* variable, Location location)
1300 : Expression(EXPRESSION_VAR_REFERENCE, location),
1301 variable_(variable), in_lvalue_pos_(VE_rvalue)
1304 // Return the variable.
1305 Named_object*
1306 named_object() const
1307 { return this->variable_; }
1309 // Does this var expression appear in an lvalue (assigned-to) context?
1310 bool
1311 in_lvalue_pos() const
1312 { return this->in_lvalue_pos_ == VE_lvalue; }
1314 // Mark a var_expression as appearing in an lvalue context.
1315 void
1316 set_in_lvalue_pos()
1317 { this->in_lvalue_pos_ = VE_lvalue; }
1319 protected:
1320 Expression*
1321 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1323 Type*
1324 do_type();
1326 void
1327 do_determine_type(const Type_context*);
1329 Expression*
1330 do_copy()
1331 { return this; }
1333 bool
1334 do_is_addressable() const
1335 { return true; }
1337 void
1338 do_address_taken(bool);
1340 Bexpression*
1341 do_get_backend(Translate_context*);
1343 void
1344 do_dump_expression(Ast_dump_context*) const;
1346 private:
1347 // The variable we are referencing.
1348 Named_object* variable_;
1349 // Set to TRUE if var expression appears in lvalue context
1350 Varexpr_context in_lvalue_pos_;
1353 // A reference to a variable within an enclosing function.
1355 class Enclosed_var_expression : public Expression
1357 public:
1358 Enclosed_var_expression(Expression* reference, Named_object* variable,
1359 Location location)
1360 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE, location),
1361 reference_(reference), variable_(variable)
1364 // The reference to the enclosed variable. This will be an indirection of the
1365 // the field stored within closure variable.
1366 Expression*
1367 reference() const
1368 { return this->reference_; }
1370 // The variable being enclosed and referenced.
1371 Named_object*
1372 variable() const
1373 { return this->variable_; }
1375 protected:
1377 do_traverse(Traverse*);
1379 Expression*
1380 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1382 Expression*
1383 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1385 Type*
1386 do_type()
1387 { return this->reference_->type(); }
1389 void
1390 do_determine_type(const Type_context* context)
1391 { return this->reference_->determine_type(context); }
1393 Expression*
1394 do_copy()
1395 { return this; }
1397 bool
1398 do_is_addressable() const
1399 { return this->reference_->is_addressable(); }
1401 void
1402 do_address_taken(bool escapes);
1404 Bexpression*
1405 do_get_backend(Translate_context* context)
1406 { return this->reference_->get_backend(context); }
1408 void
1409 do_dump_expression(Ast_dump_context*) const;
1411 private:
1412 // The reference to the enclosed variable.
1413 Expression* reference_;
1414 // The variable being enclosed.
1415 Named_object* variable_;
1418 // A reference to a temporary variable.
1420 class Temporary_reference_expression : public Expression
1422 public:
1423 Temporary_reference_expression(Temporary_statement* statement,
1424 Location location)
1425 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1426 statement_(statement), is_lvalue_(false)
1429 // The temporary that this expression refers to.
1430 Temporary_statement*
1431 statement() const
1432 { return this->statement_; }
1434 // Indicate that this reference appears on the left hand side of an
1435 // assignment statement.
1436 void
1437 set_is_lvalue()
1438 { this->is_lvalue_ = true; }
1440 protected:
1441 Type*
1442 do_type();
1444 void
1445 do_determine_type(const Type_context*)
1448 Expression*
1449 do_copy()
1450 { return make_temporary_reference(this->statement_, this->location()); }
1452 bool
1453 do_is_addressable() const
1454 { return true; }
1456 void
1457 do_address_taken(bool);
1459 Bexpression*
1460 do_get_backend(Translate_context*);
1462 void
1463 do_dump_expression(Ast_dump_context*) const;
1465 private:
1466 // The statement where the temporary variable is defined.
1467 Temporary_statement* statement_;
1468 // Whether this reference appears on the left hand side of an
1469 // assignment statement.
1470 bool is_lvalue_;
1473 // Set and use a temporary variable.
1475 class Set_and_use_temporary_expression : public Expression
1477 public:
1478 Set_and_use_temporary_expression(Temporary_statement* statement,
1479 Expression* expr, Location location)
1480 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1481 statement_(statement), expr_(expr)
1484 // Return the temporary.
1485 Temporary_statement*
1486 temporary() const
1487 { return this->statement_; }
1489 // Return the expression.
1490 Expression*
1491 expression() const
1492 { return this->expr_; }
1494 protected:
1496 do_traverse(Traverse* traverse)
1497 { return Expression::traverse(&this->expr_, traverse); }
1499 Type*
1500 do_type();
1502 void
1503 do_determine_type(const Type_context*);
1505 Expression*
1506 do_copy()
1508 return make_set_and_use_temporary(this->statement_, this->expr_,
1509 this->location());
1512 bool
1513 do_is_addressable() const
1514 { return true; }
1516 void
1517 do_address_taken(bool);
1519 Bexpression*
1520 do_get_backend(Translate_context*);
1522 void
1523 do_dump_expression(Ast_dump_context*) const;
1525 private:
1526 // The statement where the temporary variable is defined.
1527 Temporary_statement* statement_;
1528 // The expression to assign to the temporary.
1529 Expression* expr_;
1532 // A string expression.
1534 class String_expression : public Expression
1536 public:
1537 String_expression(const std::string& val, Location location)
1538 : Expression(EXPRESSION_STRING, location),
1539 val_(val), type_(NULL)
1542 const std::string&
1543 val() const
1544 { return this->val_; }
1546 static Expression*
1547 do_import(Import*);
1549 protected:
1550 bool
1551 do_is_constant() const
1552 { return true; }
1554 bool
1555 do_is_static_initializer() const
1556 { return true; }
1558 bool
1559 do_string_constant_value(std::string* val) const
1561 *val = this->val_;
1562 return true;
1565 Type*
1566 do_type();
1568 void
1569 do_determine_type(const Type_context*);
1571 Expression*
1572 do_copy()
1573 { return this; }
1575 Bexpression*
1576 do_get_backend(Translate_context*);
1578 // Write string literal to a string dump.
1579 static void
1580 export_string(String_dump* exp, const String_expression* str);
1582 void
1583 do_export(Export*) const;
1585 void
1586 do_dump_expression(Ast_dump_context*) const;
1588 private:
1589 // The string value. This is immutable.
1590 const std::string val_;
1591 // The type as determined by context.
1592 Type* type_;
1595 // A type conversion expression.
1597 class Type_conversion_expression : public Expression
1599 public:
1600 Type_conversion_expression(Type* type, Expression* expr,
1601 Location location)
1602 : Expression(EXPRESSION_CONVERSION, location),
1603 type_(type), expr_(expr), may_convert_function_types_(false)
1606 // Return the type to which we are converting.
1607 Type*
1608 type() const
1609 { return this->type_; }
1611 // Return the expression which we are converting.
1612 Expression*
1613 expr() const
1614 { return this->expr_; }
1616 // Permit converting from one function type to another. This is
1617 // used internally for method expressions.
1618 void
1619 set_may_convert_function_types()
1621 this->may_convert_function_types_ = true;
1624 // Import a type conversion expression.
1625 static Expression*
1626 do_import(Import*);
1628 protected:
1630 do_traverse(Traverse* traverse);
1632 Expression*
1633 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1635 Expression*
1636 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1638 bool
1639 do_is_constant() const;
1641 bool
1642 do_is_static_initializer() const;
1644 bool
1645 do_numeric_constant_value(Numeric_constant*) const;
1647 bool
1648 do_string_constant_value(std::string*) const;
1650 Type*
1651 do_type()
1652 { return this->type_; }
1654 void
1655 do_determine_type(const Type_context*);
1657 void
1658 do_check_types(Gogo*);
1660 Expression*
1661 do_copy()
1663 return new Type_conversion_expression(this->type_, this->expr_->copy(),
1664 this->location());
1667 Bexpression*
1668 do_get_backend(Translate_context* context);
1670 void
1671 do_export(Export*) const;
1673 void
1674 do_dump_expression(Ast_dump_context*) const;
1676 private:
1677 // The type to convert to.
1678 Type* type_;
1679 // The expression to convert.
1680 Expression* expr_;
1681 // True if this is permitted to convert function types. This is
1682 // used internally for method expressions.
1683 bool may_convert_function_types_;
1686 // An unsafe type conversion, used to pass values to builtin functions.
1688 class Unsafe_type_conversion_expression : public Expression
1690 public:
1691 Unsafe_type_conversion_expression(Type* type, Expression* expr,
1692 Location location)
1693 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
1694 type_(type), expr_(expr)
1697 Expression*
1698 expr() const
1699 { return this->expr_; }
1701 protected:
1703 do_traverse(Traverse* traverse);
1705 bool
1706 do_is_static_initializer() const;
1708 Type*
1709 do_type()
1710 { return this->type_; }
1712 void
1713 do_determine_type(const Type_context*)
1714 { this->expr_->determine_type_no_context(); }
1716 Expression*
1717 do_copy()
1719 return new Unsafe_type_conversion_expression(this->type_,
1720 this->expr_->copy(),
1721 this->location());
1724 Bexpression*
1725 do_get_backend(Translate_context*);
1727 void
1728 do_dump_expression(Ast_dump_context*) const;
1730 private:
1731 // The type to convert to.
1732 Type* type_;
1733 // The expression to convert.
1734 Expression* expr_;
1737 // A Unary expression.
1739 class Unary_expression : public Expression
1741 public:
1742 Unary_expression(Operator op, Expression* expr, Location location)
1743 : Expression(EXPRESSION_UNARY, location),
1744 op_(op), escapes_(true), create_temp_(false), is_gc_root_(false),
1745 is_slice_init_(false), expr_(expr), issue_nil_check_(false)
1748 // Return the operator.
1749 Operator
1750 op() const
1751 { return this->op_; }
1753 // Return the operand.
1754 Expression*
1755 operand() const
1756 { return this->expr_; }
1758 // Record that an address expression does not escape.
1759 void
1760 set_does_not_escape()
1762 go_assert(this->op_ == OPERATOR_AND);
1763 this->escapes_ = false;
1766 // Record that this is an address expression which should create a
1767 // temporary variable if necessary. This is used for method calls.
1768 void
1769 set_create_temp()
1771 go_assert(this->op_ == OPERATOR_AND);
1772 this->create_temp_ = true;
1775 // Record that this is an address expression of a GC root, which is a
1776 // mutable composite literal. This used for registering GC variables.
1777 void
1778 set_is_gc_root()
1780 go_assert(this->op_ == OPERATOR_AND);
1781 this->is_gc_root_ = true;
1784 // Record that this is an address expression of a slice value initializer,
1785 // which is mutable if the values are not copied to the heap.
1786 void
1787 set_is_slice_init()
1789 go_assert(this->op_ == OPERATOR_AND);
1790 this->is_slice_init_ = true;
1793 // Call the address_taken method on the operand if necessary.
1794 void
1795 check_operand_address_taken(Gogo*);
1797 // Apply unary opcode OP to UNC, setting NC. Return true if this
1798 // could be done, false if not. On overflow, issues an error and
1799 // sets *ISSUED_ERROR.
1800 static bool
1801 eval_constant(Operator op, const Numeric_constant* unc,
1802 Location, Numeric_constant* nc, bool *issued_error);
1804 static Expression*
1805 do_import(Import*);
1807 protected:
1809 do_traverse(Traverse* traverse)
1810 { return Expression::traverse(&this->expr_, traverse); }
1812 Expression*
1813 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1815 Expression*
1816 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1818 bool
1819 do_is_constant() const;
1821 bool
1822 do_is_static_initializer() const;
1824 bool
1825 do_numeric_constant_value(Numeric_constant*) const;
1827 Type*
1828 do_type();
1830 void
1831 do_determine_type(const Type_context*);
1833 void
1834 do_check_types(Gogo*);
1836 Expression*
1837 do_copy()
1839 return Expression::make_unary(this->op_, this->expr_->copy(),
1840 this->location());
1843 bool
1844 do_must_eval_subexpressions_in_order(int*) const
1845 { return this->op_ == OPERATOR_MULT; }
1847 bool
1848 do_is_addressable() const
1849 { return this->op_ == OPERATOR_MULT; }
1851 Bexpression*
1852 do_get_backend(Translate_context*);
1854 void
1855 do_export(Export*) const;
1857 void
1858 do_dump_expression(Ast_dump_context*) const;
1860 void
1861 do_issue_nil_check()
1862 { this->issue_nil_check_ = (this->op_ == OPERATOR_MULT); }
1864 private:
1865 static bool
1866 base_is_static_initializer(Expression*);
1868 // The unary operator to apply.
1869 Operator op_;
1870 // Normally true. False if this is an address expression which does
1871 // not escape the current function.
1872 bool escapes_;
1873 // True if this is an address expression which should create a
1874 // temporary variable if necessary.
1875 bool create_temp_;
1876 // True if this is an address expression for a GC root. A GC root is a
1877 // special struct composite literal that is mutable when addressed, meaning
1878 // it cannot be represented as an immutable_struct in the backend.
1879 bool is_gc_root_;
1880 // True if this is an address expression for a slice value with an immutable
1881 // initializer. The initializer for a slice's value pointer has an array
1882 // type, meaning it cannot be represented as an immutable_struct in the
1883 // backend.
1884 bool is_slice_init_;
1885 // The operand.
1886 Expression* expr_;
1887 // Whether or not to issue a nil check for this expression if its address
1888 // is being taken.
1889 bool issue_nil_check_;
1892 // A binary expression.
1894 class Binary_expression : public Expression
1896 public:
1897 Binary_expression(Operator op, Expression* left, Expression* right,
1898 Location location)
1899 : Expression(EXPRESSION_BINARY, location),
1900 op_(op), left_(left), right_(right), type_(NULL)
1903 // Return the operator.
1904 Operator
1905 op()
1906 { return this->op_; }
1908 // Return the left hand expression.
1909 Expression*
1910 left()
1911 { return this->left_; }
1913 // Return the right hand expression.
1914 Expression*
1915 right()
1916 { return this->right_; }
1918 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1919 // Return true if this could be done, false if not. Issue errors at
1920 // LOCATION as appropriate, and sets *ISSUED_ERROR if it did.
1921 static bool
1922 eval_constant(Operator op, Numeric_constant* left_nc,
1923 Numeric_constant* right_nc, Location location,
1924 Numeric_constant* nc, bool* issued_error);
1926 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1927 // *RESULT. Return true if this could be done, false if not. Issue
1928 // errors at LOCATION as appropriate.
1929 static bool
1930 compare_constant(Operator op, Numeric_constant* left_nc,
1931 Numeric_constant* right_nc, Location location,
1932 bool* result);
1934 static Expression*
1935 do_import(Import*);
1937 // Report an error if OP can not be applied to TYPE. Return whether
1938 // it can. OTYPE is the type of the other operand.
1939 static bool
1940 check_operator_type(Operator op, Type* type, Type* otype, Location);
1942 // Set *RESULT_TYPE to the resulting type when OP is applied to
1943 // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
1944 // success, false on failure.
1945 static bool
1946 operation_type(Operator op, Type* left_type, Type* right_type,
1947 Type** result_type);
1949 protected:
1951 do_traverse(Traverse* traverse);
1953 Expression*
1954 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1956 Expression*
1957 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1959 bool
1960 do_is_constant() const
1961 { return this->left_->is_constant() && this->right_->is_constant(); }
1963 bool
1964 do_is_static_initializer() const;
1966 bool
1967 do_numeric_constant_value(Numeric_constant*) const;
1969 bool
1970 do_discarding_value();
1972 Type*
1973 do_type();
1975 void
1976 do_determine_type(const Type_context*);
1978 void
1979 do_check_types(Gogo*);
1981 Expression*
1982 do_copy()
1984 return Expression::make_binary(this->op_, this->left_->copy(),
1985 this->right_->copy(), this->location());
1988 Bexpression*
1989 do_get_backend(Translate_context*);
1991 void
1992 do_export(Export*) const;
1994 void
1995 do_dump_expression(Ast_dump_context*) const;
1997 private:
1998 static bool
1999 cmp_to_bool(Operator op, int cmp);
2001 static bool
2002 eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
2003 Location, Numeric_constant*);
2005 static bool
2006 eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
2007 Location, Numeric_constant*);
2009 static bool
2010 eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
2011 Location, Numeric_constant*);
2013 static bool
2014 compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
2016 static bool
2017 compare_float(const Numeric_constant*, const Numeric_constant *, int*);
2019 static bool
2020 compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
2022 Expression*
2023 lower_struct_comparison(Gogo*, Statement_inserter*);
2025 Expression*
2026 lower_array_comparison(Gogo*, Statement_inserter*);
2028 Expression*
2029 lower_interface_value_comparison(Gogo*, Statement_inserter*);
2031 Expression*
2032 lower_compare_to_memcmp(Gogo*, Statement_inserter*);
2034 Expression*
2035 operand_address(Statement_inserter*, Expression*);
2037 // The binary operator to apply.
2038 Operator op_;
2039 // The left hand side operand.
2040 Expression* left_;
2041 // The right hand side operand.
2042 Expression* right_;
2043 // The type of a comparison operation.
2044 Type* type_;
2047 // A string concatenation expression. This is a sequence of strings
2048 // added together. It is created when lowering Binary_expression.
2050 class String_concat_expression : public Expression
2052 public:
2053 String_concat_expression(Expression_list* exprs)
2054 : Expression(EXPRESSION_STRING_CONCAT, exprs->front()->location()),
2055 exprs_(exprs)
2058 // Return the list of string expressions to be concatenated.
2059 Expression_list*
2060 exprs()
2061 { return this->exprs_; }
2063 protected:
2065 do_traverse(Traverse* traverse)
2066 { return this->exprs_->traverse(traverse); }
2068 Expression*
2069 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2070 { return this; }
2072 Expression*
2073 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2075 bool
2076 do_is_constant() const;
2078 bool
2079 do_is_static_initializer() const;
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()
2092 { return Expression::make_string_concat(this->exprs_->copy()); }
2094 Bexpression*
2095 do_get_backend(Translate_context*)
2096 { go_unreachable(); }
2098 void
2099 do_export(Export*) const
2100 { go_unreachable(); }
2102 void
2103 do_dump_expression(Ast_dump_context*) const;
2105 private:
2106 // The string expressions to concatenate.
2107 Expression_list* exprs_;
2110 // A call expression. The go statement needs to dig inside this.
2112 class Call_expression : public Expression
2114 public:
2115 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
2116 Location location)
2117 : Expression(EXPRESSION_CALL, location),
2118 fn_(fn), args_(args), type_(NULL), results_(NULL), call_(NULL),
2119 call_temp_(NULL), expected_result_count_(0), is_varargs_(is_varargs),
2120 varargs_are_lowered_(false), types_are_determined_(false),
2121 is_deferred_(false), is_concurrent_(false), issued_error_(false),
2122 is_multi_value_arg_(false), is_flattened_(false)
2125 // The function to call.
2126 Expression*
2127 fn() const
2128 { return this->fn_; }
2130 // The arguments.
2131 Expression_list*
2132 args()
2133 { return this->args_; }
2135 const Expression_list*
2136 args() const
2137 { return this->args_; }
2139 // Get the function type.
2140 Function_type*
2141 get_function_type() const;
2143 // Return the number of values this call will return.
2144 size_t
2145 result_count() const;
2147 // Return the temporary variable which holds result I. This is only
2148 // valid after the expression has been lowered, and is only valid
2149 // for calls which return multiple results.
2150 Temporary_statement*
2151 result(size_t i) const;
2153 // Set the number of results expected from this call. This is used
2154 // when the call appears in a context that expects multiple results,
2155 // such as a, b = f().
2156 void
2157 set_expected_result_count(size_t);
2159 // Return whether this is a call to the predeclared function
2160 // recover.
2161 bool
2162 is_recover_call() const;
2164 // Set the argument for a call to recover.
2165 void
2166 set_recover_arg(Expression*);
2168 // Whether the last argument is a varargs argument (f(a...)).
2169 bool
2170 is_varargs() const
2171 { return this->is_varargs_; }
2173 // Return whether varargs have already been lowered.
2174 bool
2175 varargs_are_lowered() const
2176 { return this->varargs_are_lowered_; }
2178 // Note that varargs have already been lowered.
2179 void
2180 set_varargs_are_lowered()
2181 { this->varargs_are_lowered_ = true; }
2183 // Whether this call is being deferred.
2184 bool
2185 is_deferred() const
2186 { return this->is_deferred_; }
2188 // Note that the call is being deferred.
2189 void
2190 set_is_deferred()
2191 { this->is_deferred_ = true; }
2193 // Whether this call is concurrently executed.
2194 bool
2195 is_concurrent() const
2196 { return this->is_concurrent_; }
2198 // Note that the call is concurrently executed.
2199 void
2200 set_is_concurrent()
2201 { this->is_concurrent_ = true; }
2203 // We have found an error with this call expression; return true if
2204 // we should report it.
2205 bool
2206 issue_error();
2208 // Whether or not this call contains errors, either in the call or the
2209 // arguments to the call.
2210 bool
2211 is_erroneous_call();
2213 // Whether this call returns multiple results that are used as an
2214 // multi-valued argument.
2215 bool
2216 is_multi_value_arg() const
2217 { return this->is_multi_value_arg_; }
2219 // Note this call is used as a multi-valued argument.
2220 void
2221 set_is_multi_value_arg()
2222 { this->is_multi_value_arg_ = true; }
2224 protected:
2226 do_traverse(Traverse*);
2228 virtual Expression*
2229 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2231 virtual Expression*
2232 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2234 bool
2235 do_discarding_value()
2236 { return true; }
2238 virtual Type*
2239 do_type();
2241 virtual void
2242 do_determine_type(const Type_context*);
2244 virtual void
2245 do_check_types(Gogo*);
2247 Expression*
2248 do_copy();
2250 bool
2251 do_must_eval_in_order() const;
2253 virtual Bexpression*
2254 do_get_backend(Translate_context*);
2256 virtual bool
2257 do_is_recover_call() const;
2259 virtual void
2260 do_set_recover_arg(Expression*);
2262 // Let a builtin expression change the argument list.
2263 void
2264 set_args(Expression_list* args)
2265 { this->args_ = args; }
2267 // Let a builtin expression lower varargs.
2268 void
2269 lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
2270 Type* varargs_type, size_t param_count,
2271 Slice_storage_escape_disp escape_disp);
2273 // Let a builtin expression check whether types have been
2274 // determined.
2275 bool
2276 determining_types();
2278 void
2279 do_dump_expression(Ast_dump_context*) const;
2281 private:
2282 bool
2283 check_argument_type(int, const Type*, const Type*, Location, bool);
2285 Expression*
2286 lower_to_builtin(Named_object**, const char*, int);
2288 Expression*
2289 interface_method_function(Interface_field_reference_expression*,
2290 Expression**);
2292 Bexpression*
2293 set_results(Translate_context*);
2295 Bexpression*
2296 call_result_ref(Translate_context* context);
2298 // The function to call.
2299 Expression* fn_;
2300 // The arguments to pass. This may be NULL if there are no
2301 // arguments.
2302 Expression_list* args_;
2303 // The type of the expression, to avoid recomputing it.
2304 Type* type_;
2305 // The list of temporaries which will hold the results if the
2306 // function returns a tuple.
2307 std::vector<Temporary_statement*>* results_;
2308 // The backend expression for the call, used for a call which returns a tuple.
2309 Bexpression* call_;
2310 // A temporary variable to store this call if the function returns a tuple.
2311 Temporary_statement* call_temp_;
2312 // If not 0, the number of results expected from this call, when
2313 // used in a context that expects multiple values.
2314 size_t expected_result_count_;
2315 // True if the last argument is a varargs argument (f(a...)).
2316 bool is_varargs_;
2317 // True if varargs have already been lowered.
2318 bool varargs_are_lowered_;
2319 // True if types have been determined.
2320 bool types_are_determined_;
2321 // True if the call is an argument to a defer statement.
2322 bool is_deferred_;
2323 // True if the call is an argument to a go statement.
2324 bool is_concurrent_;
2325 // True if we reported an error about a mismatch between call
2326 // results and uses. This is to avoid producing multiple errors
2327 // when there are multiple Call_result_expressions.
2328 bool issued_error_;
2329 // True if this call is used as an argument that returns multiple results.
2330 bool is_multi_value_arg_;
2331 // True if this expression has already been flattened.
2332 bool is_flattened_;
2335 // A single result from a call which returns multiple results.
2337 class Call_result_expression : public Expression
2339 public:
2340 Call_result_expression(Call_expression* call, unsigned int index)
2341 : Expression(EXPRESSION_CALL_RESULT, call->location()),
2342 call_(call), index_(index)
2345 Expression*
2346 call() const
2347 { return this->call_; }
2349 unsigned int
2350 index() const
2351 { return this->index_; }
2353 protected:
2355 do_traverse(Traverse*);
2357 Type*
2358 do_type();
2360 void
2361 do_determine_type(const Type_context*);
2363 void
2364 do_check_types(Gogo*);
2366 Expression*
2367 do_copy()
2369 return new Call_result_expression(this->call_->call_expression(),
2370 this->index_);
2373 bool
2374 do_must_eval_in_order() const
2375 { return true; }
2377 Bexpression*
2378 do_get_backend(Translate_context*);
2380 void
2381 do_dump_expression(Ast_dump_context*) const;
2383 private:
2384 // The underlying call expression.
2385 Expression* call_;
2386 // Which result we want.
2387 unsigned int index_;
2390 // An expression which represents a pointer to a function.
2392 class Func_expression : public Expression
2394 public:
2395 Func_expression(Named_object* function, Expression* closure,
2396 Location location)
2397 : Expression(EXPRESSION_FUNC_REFERENCE, location),
2398 function_(function), closure_(closure),
2399 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS)
2402 // Return the object associated with the function.
2403 Named_object*
2404 named_object() const
2405 { return this->function_; }
2407 // Return the closure for this function. This will return NULL if
2408 // the function has no closure, which is the normal case.
2409 Expression*
2410 closure()
2411 { return this->closure_; }
2413 // Return whether this is a reference to a runtime function.
2414 bool
2415 is_runtime_function() const
2416 { return this->runtime_code_ != Runtime::NUMBER_OF_FUNCTIONS; }
2418 // Return the runtime code for this function expression.
2419 // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
2420 // runtime function.
2421 Runtime::Function
2422 runtime_code() const
2423 { return this->runtime_code_; }
2425 // Set the runtime code for this function expression.
2426 void
2427 set_runtime_code(Runtime::Function code)
2428 { this->runtime_code_ = code; }
2430 // Return a backend expression for the code of a function.
2431 static Bexpression*
2432 get_code_pointer(Gogo*, Named_object* function, Location loc);
2434 protected:
2436 do_traverse(Traverse*);
2438 Type*
2439 do_type();
2441 void
2442 do_determine_type(const Type_context*)
2444 if (this->closure_ != NULL)
2445 this->closure_->determine_type_no_context();
2448 Expression*
2449 do_copy()
2451 return Expression::make_func_reference(this->function_,
2452 (this->closure_ == NULL
2453 ? NULL
2454 : this->closure_->copy()),
2455 this->location());
2458 Bexpression*
2459 do_get_backend(Translate_context*);
2461 void
2462 do_dump_expression(Ast_dump_context*) const;
2464 private:
2465 // The function itself.
2466 Named_object* function_;
2467 // A closure. This is normally NULL. For a nested function, it may
2468 // be a struct holding pointers to all the variables referenced by
2469 // this function and defined in enclosing functions.
2470 Expression* closure_;
2471 // The runtime code for the referenced function.
2472 Runtime::Function runtime_code_;
2475 // A function descriptor. A function descriptor is a struct with a
2476 // single field pointing to the function code. This is used for
2477 // functions without closures.
2479 class Func_descriptor_expression : public Expression
2481 public:
2482 Func_descriptor_expression(Named_object* fn);
2484 // Make the function descriptor type, so that it can be converted.
2485 static void
2486 make_func_descriptor_type();
2488 protected:
2490 do_traverse(Traverse*);
2492 Type*
2493 do_type();
2495 void
2496 do_determine_type(const Type_context*)
2499 Expression*
2500 do_copy()
2501 { return Expression::make_func_descriptor(this->fn_); }
2503 bool
2504 do_is_addressable() const
2505 { return true; }
2507 Bexpression*
2508 do_get_backend(Translate_context*);
2510 void
2511 do_dump_expression(Ast_dump_context* context) const;
2513 private:
2514 // The type of all function descriptors.
2515 static Type* descriptor_type;
2517 // The function for which this is the descriptor.
2518 Named_object* fn_;
2519 // The descriptor variable.
2520 Bvariable* dvar_;
2523 // A reference to an unknown name.
2525 class Unknown_expression : public Parser_expression
2527 public:
2528 Unknown_expression(Named_object* named_object, Location location)
2529 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
2530 named_object_(named_object), no_error_message_(false),
2531 is_composite_literal_key_(false)
2534 // The associated named object.
2535 Named_object*
2536 named_object() const
2537 { return this->named_object_; }
2539 // The name of the identifier which was unknown.
2540 const std::string&
2541 name() const;
2543 // Call this to indicate that we should not give an error if this
2544 // name is never defined. This is used to avoid knock-on errors
2545 // during an erroneous parse.
2546 void
2547 set_no_error_message()
2548 { this->no_error_message_ = true; }
2550 // Note that this expression is being used as the key in a composite
2551 // literal, so it may be OK if it is not resolved.
2552 void
2553 set_is_composite_literal_key()
2554 { this->is_composite_literal_key_ = true; }
2556 // Note that this expression should no longer be treated as a
2557 // composite literal key.
2558 void
2559 clear_is_composite_literal_key()
2560 { this->is_composite_literal_key_ = false; }
2562 protected:
2563 Expression*
2564 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2566 Expression*
2567 do_copy()
2568 { return new Unknown_expression(this->named_object_, this->location()); }
2570 void
2571 do_dump_expression(Ast_dump_context*) const;
2573 private:
2574 // The unknown name.
2575 Named_object* named_object_;
2576 // True if we should not give errors if this is undefined. This is
2577 // used if there was a parse failure.
2578 bool no_error_message_;
2579 // True if this is the key in a composite literal.
2580 bool is_composite_literal_key_;
2583 // An index expression. This is lowered to an array index, a string
2584 // index, or a map index.
2586 class Index_expression : public Parser_expression
2588 public:
2589 Index_expression(Expression* left, Expression* start, Expression* end,
2590 Expression* cap, Location location)
2591 : Parser_expression(EXPRESSION_INDEX, location),
2592 left_(left), start_(start), end_(end), cap_(cap)
2595 // Dump an index expression, i.e. an expression of the form
2596 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
2597 static void
2598 dump_index_expression(Ast_dump_context*, const Expression* expr,
2599 const Expression* start, const Expression* end,
2600 const Expression* cap);
2602 protected:
2604 do_traverse(Traverse*);
2606 Expression*
2607 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2609 Expression*
2610 do_copy()
2612 return new Index_expression(this->left_->copy(), this->start_->copy(),
2613 (this->end_ == NULL
2614 ? NULL
2615 : this->end_->copy()),
2616 (this->cap_ == NULL
2617 ? NULL
2618 : this->cap_->copy()),
2619 this->location());
2622 bool
2623 do_must_eval_subexpressions_in_order(int* skip) const
2625 *skip = 1;
2626 return true;
2629 void
2630 do_dump_expression(Ast_dump_context*) const;
2632 void
2633 do_issue_nil_check()
2634 { this->left_->issue_nil_check(); }
2635 private:
2636 // The expression being indexed.
2637 Expression* left_;
2638 // The first index.
2639 Expression* start_;
2640 // The second index. This is NULL for an index, non-NULL for a
2641 // slice.
2642 Expression* end_;
2643 // The capacity argument. This is NULL for indices and slices that use the
2644 // default capacity, non-NULL for indices and slices that specify the
2645 // capacity.
2646 Expression* cap_;
2649 // An array index. This is used for both indexing and slicing.
2651 class Array_index_expression : public Expression
2653 public:
2654 Array_index_expression(Expression* array, Expression* start,
2655 Expression* end, Expression* cap, Location location)
2656 : Expression(EXPRESSION_ARRAY_INDEX, location),
2657 array_(array), start_(start), end_(end), cap_(cap), type_(NULL),
2658 is_lvalue_(false)
2661 // Return the array.
2662 Expression*
2663 array()
2664 { return this->array_; }
2666 const Expression*
2667 array() const
2668 { return this->array_; }
2670 // Return the index of a simple index expression, or the start index
2671 // of a slice expression.
2672 Expression*
2673 start()
2674 { return this->start_; }
2676 const Expression*
2677 start() const
2678 { return this->start_; }
2680 // Return the end index of a slice expression. This is NULL for a
2681 // simple index expression.
2682 Expression*
2683 end()
2684 { return this->end_; }
2686 const Expression*
2687 end() const
2688 { return this->end_; }
2690 // Return whether this array index expression appears in an lvalue
2691 // (left hand side of assignment) context.
2692 bool
2693 is_lvalue() const
2694 { return this->is_lvalue_; }
2696 // Update this array index expression to indicate that it appears
2697 // in a left-hand-side or lvalue context.
2698 void
2699 set_is_lvalue()
2700 { this->is_lvalue_ = true; }
2702 protected:
2704 do_traverse(Traverse*);
2706 Expression*
2707 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2709 Type*
2710 do_type();
2712 void
2713 do_determine_type(const Type_context*);
2715 void
2716 do_check_types(Gogo*);
2718 Expression*
2719 do_copy()
2721 return Expression::make_array_index(this->array_->copy(),
2722 this->start_->copy(),
2723 (this->end_ == NULL
2724 ? NULL
2725 : this->end_->copy()),
2726 (this->cap_ == NULL
2727 ? NULL
2728 : this->cap_->copy()),
2729 this->location());
2732 bool
2733 do_must_eval_subexpressions_in_order(int* skip) const
2735 *skip = 1;
2736 return true;
2739 bool
2740 do_is_addressable() const;
2742 void
2743 do_address_taken(bool escapes)
2744 { this->array_->address_taken(escapes); }
2746 void
2747 do_issue_nil_check()
2748 { this->array_->issue_nil_check(); }
2750 Bexpression*
2751 do_get_backend(Translate_context*);
2753 void
2754 do_dump_expression(Ast_dump_context*) const;
2756 private:
2757 // The array we are getting a value from.
2758 Expression* array_;
2759 // The start or only index.
2760 Expression* start_;
2761 // The end index of a slice. This may be NULL for a simple array
2762 // index, or it may be a nil expression for the length of the array.
2763 Expression* end_;
2764 // The capacity argument of a slice. This may be NULL for an array index or
2765 // slice.
2766 Expression* cap_;
2767 // The type of the expression.
2768 Type* type_;
2769 // Whether expr appears in an lvalue context.
2770 bool is_lvalue_;
2773 // A string index. This is used for both indexing and slicing.
2775 class String_index_expression : public Expression
2777 public:
2778 String_index_expression(Expression* string, Expression* start,
2779 Expression* end, Location location)
2780 : Expression(EXPRESSION_STRING_INDEX, location),
2781 string_(string), start_(start), end_(end)
2784 // Return the string being indexed.
2785 Expression*
2786 string() const
2787 { return this->string_; }
2789 protected:
2791 do_traverse(Traverse*);
2793 Expression*
2794 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2796 Type*
2797 do_type();
2799 void
2800 do_determine_type(const Type_context*);
2802 void
2803 do_check_types(Gogo*);
2805 Expression*
2806 do_copy()
2808 return Expression::make_string_index(this->string_->copy(),
2809 this->start_->copy(),
2810 (this->end_ == NULL
2811 ? NULL
2812 : this->end_->copy()),
2813 this->location());
2816 bool
2817 do_must_eval_subexpressions_in_order(int* skip) const
2819 *skip = 1;
2820 return true;
2823 Bexpression*
2824 do_get_backend(Translate_context*);
2826 void
2827 do_dump_expression(Ast_dump_context*) const;
2829 private:
2830 // The string we are getting a value from.
2831 Expression* string_;
2832 // The start or only index.
2833 Expression* start_;
2834 // The end index of a slice. This may be NULL for a single index,
2835 // or it may be a nil expression for the length of the string.
2836 Expression* end_;
2839 // An index into a map.
2841 class Map_index_expression : public Expression
2843 public:
2844 Map_index_expression(Expression* map, Expression* index,
2845 Location location)
2846 : Expression(EXPRESSION_MAP_INDEX, location),
2847 map_(map), index_(index), value_pointer_(NULL)
2850 // Return the map.
2851 Expression*
2852 map()
2853 { return this->map_; }
2855 const Expression*
2856 map() const
2857 { return this->map_; }
2859 // Return the index.
2860 Expression*
2861 index()
2862 { return this->index_; }
2864 const Expression*
2865 index() const
2866 { return this->index_; }
2868 // Get the type of the map being indexed.
2869 Map_type*
2870 get_map_type() const;
2872 // Return an expression for the map index. This returns an
2873 // expression that evaluates to a pointer to a value in the map. If
2874 // the key is not present in the map, this will return a pointer to
2875 // the zero value.
2876 Expression*
2877 get_value_pointer(Gogo*);
2879 protected:
2881 do_traverse(Traverse*);
2883 Expression*
2884 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2886 Type*
2887 do_type();
2889 void
2890 do_determine_type(const Type_context*);
2892 void
2893 do_check_types(Gogo*);
2895 Expression*
2896 do_copy()
2898 return Expression::make_map_index(this->map_->copy(),
2899 this->index_->copy(),
2900 this->location());
2903 bool
2904 do_must_eval_subexpressions_in_order(int* skip) const
2906 *skip = 1;
2907 return true;
2910 // A map index expression is an lvalue but it is not addressable.
2912 Bexpression*
2913 do_get_backend(Translate_context*);
2915 void
2916 do_dump_expression(Ast_dump_context*) const;
2918 private:
2919 // The map we are looking into.
2920 Expression* map_;
2921 // The index.
2922 Expression* index_;
2923 // A pointer to the value at this index.
2924 Expression* value_pointer_;
2927 // An expression which represents a method bound to its first
2928 // argument.
2930 class Bound_method_expression : public Expression
2932 public:
2933 Bound_method_expression(Expression* expr, const Method *method,
2934 Named_object* function, Location location)
2935 : Expression(EXPRESSION_BOUND_METHOD, location),
2936 expr_(expr), expr_type_(NULL), method_(method), function_(function)
2939 // Return the object which is the first argument.
2940 Expression*
2941 first_argument()
2942 { return this->expr_; }
2944 // Return the implicit type of the first argument. This will be
2945 // non-NULL when using a method from an anonymous field without
2946 // using an explicit stub.
2947 Type*
2948 first_argument_type() const
2949 { return this->expr_type_; }
2951 // Return the method.
2952 const Method*
2953 method() const
2954 { return this->method_; }
2956 // Return the function to call.
2957 Named_object*
2958 function() const
2959 { return this->function_; }
2961 // Set the implicit type of the expression.
2962 void
2963 set_first_argument_type(Type* type)
2964 { this->expr_type_ = type; }
2966 // Create a thunk to call FUNCTION, for METHOD, when it is used as
2967 // part of a method value.
2968 static Named_object*
2969 create_thunk(Gogo*, const Method* method, Named_object* function);
2971 protected:
2973 do_traverse(Traverse*);
2975 Expression*
2976 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2978 Type*
2979 do_type();
2981 void
2982 do_determine_type(const Type_context*);
2984 void
2985 do_check_types(Gogo*);
2987 Expression*
2988 do_copy()
2990 return new Bound_method_expression(this->expr_->copy(), this->method_,
2991 this->function_, this->location());
2994 Bexpression*
2995 do_get_backend(Translate_context*)
2996 { go_unreachable(); }
2998 void
2999 do_dump_expression(Ast_dump_context*) const;
3001 private:
3002 // A mapping from method functions to the thunks we have created for
3003 // them.
3004 typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
3005 static Method_value_thunks method_value_thunks;
3007 // The object used to find the method. This is passed to the method
3008 // as the first argument.
3009 Expression* expr_;
3010 // The implicit type of the object to pass to the method. This is
3011 // NULL in the normal case, non-NULL when using a method from an
3012 // anonymous field which does not require a stub.
3013 Type* expr_type_;
3014 // The method.
3015 const Method* method_;
3016 // The function to call. This is not the same as
3017 // method_->named_object() when the method has a stub. This will be
3018 // the real function rather than the stub.
3019 Named_object* function_;
3022 // A reference to a field in a struct.
3024 class Field_reference_expression : public Expression
3026 public:
3027 Field_reference_expression(Expression* expr, unsigned int field_index,
3028 Location location)
3029 : Expression(EXPRESSION_FIELD_REFERENCE, location),
3030 expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
3033 // Return the struct expression.
3034 Expression*
3035 expr() const
3036 { return this->expr_; }
3038 // Return the field index.
3039 unsigned int
3040 field_index() const
3041 { return this->field_index_; }
3043 // Return whether this node was implied by an anonymous field.
3044 bool
3045 implicit() const
3046 { return this->implicit_; }
3048 void
3049 set_implicit(bool implicit)
3050 { this->implicit_ = implicit; }
3052 // Set the struct expression. This is used when parsing.
3053 void
3054 set_struct_expression(Expression* expr)
3056 go_assert(this->expr_ == NULL);
3057 this->expr_ = expr;
3060 protected:
3062 do_traverse(Traverse* traverse)
3063 { return Expression::traverse(&this->expr_, traverse); }
3065 Expression*
3066 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3068 Type*
3069 do_type();
3071 void
3072 do_determine_type(const Type_context*)
3073 { this->expr_->determine_type_no_context(); }
3075 void
3076 do_check_types(Gogo*);
3078 Expression*
3079 do_copy()
3081 return Expression::make_field_reference(this->expr_->copy(),
3082 this->field_index_,
3083 this->location());
3086 bool
3087 do_is_addressable() const
3088 { return this->expr_->is_addressable(); }
3090 void
3091 do_address_taken(bool escapes)
3092 { this->expr_->address_taken(escapes); }
3094 void
3095 do_issue_nil_check()
3096 { this->expr_->issue_nil_check(); }
3098 Bexpression*
3099 do_get_backend(Translate_context*);
3101 void
3102 do_dump_expression(Ast_dump_context*) const;
3104 private:
3105 // The expression we are looking into. This should have a type of
3106 // struct.
3107 Expression* expr_;
3108 // The zero-based index of the field we are retrieving.
3109 unsigned int field_index_;
3110 // Whether this node was emitted implicitly for an embedded field,
3111 // that is, expr_ is not the expr_ of the original user node.
3112 bool implicit_;
3113 // Whether we have already emitted a fieldtrack call.
3114 bool called_fieldtrack_;
3117 // A reference to a field of an interface.
3119 class Interface_field_reference_expression : public Expression
3121 public:
3122 Interface_field_reference_expression(Expression* expr,
3123 const std::string& name,
3124 Location location)
3125 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
3126 expr_(expr), name_(name)
3129 // Return the expression for the interface object.
3130 Expression*
3131 expr()
3132 { return this->expr_; }
3134 // Return the name of the method to call.
3135 const std::string&
3136 name() const
3137 { return this->name_; }
3139 // Create a thunk to call the method NAME in TYPE when it is used as
3140 // part of a method value.
3141 static Named_object*
3142 create_thunk(Gogo*, Interface_type* type, const std::string& name);
3144 // Return an expression for the pointer to the function to call.
3145 Expression*
3146 get_function();
3148 // Return an expression for the first argument to pass to the interface
3149 // function. This is the real object associated with the interface object.
3150 Expression*
3151 get_underlying_object();
3153 protected:
3155 do_traverse(Traverse* traverse);
3157 Expression*
3158 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3160 Type*
3161 do_type();
3163 void
3164 do_determine_type(const Type_context*);
3166 void
3167 do_check_types(Gogo*);
3169 Expression*
3170 do_copy()
3172 return Expression::make_interface_field_reference(this->expr_->copy(),
3173 this->name_,
3174 this->location());
3177 Bexpression*
3178 do_get_backend(Translate_context*);
3180 void
3181 do_dump_expression(Ast_dump_context*) const;
3183 private:
3184 // A mapping from interface types to a list of thunks we have
3185 // created for methods.
3186 typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
3187 typedef Unordered_map(Interface_type*, Method_thunks*)
3188 Interface_method_thunks;
3189 static Interface_method_thunks interface_method_thunks;
3191 // The expression for the interface object. This should have a type
3192 // of interface or pointer to interface.
3193 Expression* expr_;
3194 // The field we are retrieving--the name of the method.
3195 std::string name_;
3198 // Implement the builtin function new.
3200 class Allocation_expression : public Expression
3202 public:
3203 Allocation_expression(Type* type, Location location)
3204 : Expression(EXPRESSION_ALLOCATION, location),
3205 type_(type), allocate_on_stack_(false)
3208 void
3209 set_allocate_on_stack()
3210 { this->allocate_on_stack_ = true; }
3212 protected:
3214 do_traverse(Traverse*);
3216 Type*
3217 do_type();
3219 void
3220 do_determine_type(const Type_context*)
3223 Expression*
3224 do_copy();
3226 Bexpression*
3227 do_get_backend(Translate_context*);
3229 void
3230 do_dump_expression(Ast_dump_context*) const;
3232 private:
3233 // The type we are allocating.
3234 Type* type_;
3235 // Whether or not this is a stack allocation.
3236 bool allocate_on_stack_;
3239 // A general composite literal. This is lowered to a type specific
3240 // version.
3242 class Composite_literal_expression : public Parser_expression
3244 public:
3245 Composite_literal_expression(Type* type, int depth, bool has_keys,
3246 Expression_list* vals, bool all_are_names,
3247 Location location)
3248 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
3249 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys),
3250 all_are_names_(all_are_names), key_path_(std::vector<bool>(depth))
3254 // Mark the DEPTH entry of KEY_PATH as containing a key.
3255 void
3256 update_key_path(size_t depth)
3258 go_assert(depth < this->key_path_.size());
3259 this->key_path_[depth] = true;
3262 protected:
3264 do_traverse(Traverse* traverse);
3266 Expression*
3267 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3269 Expression*
3270 do_copy()
3272 Composite_literal_expression *ret =
3273 new Composite_literal_expression(this->type_, this->depth_,
3274 this->has_keys_,
3275 (this->vals_ == NULL
3276 ? NULL
3277 : this->vals_->copy()),
3278 this->all_are_names_,
3279 this->location());
3280 ret->key_path_ = this->key_path_;
3281 return ret;
3284 void
3285 do_dump_expression(Ast_dump_context*) const;
3287 private:
3288 Expression*
3289 lower_struct(Gogo*, Type*);
3291 Expression*
3292 lower_array(Type*);
3294 Expression*
3295 make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
3297 Expression*
3298 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
3300 // The type of the composite literal.
3301 Type* type_;
3302 // The depth within a list of composite literals within a composite
3303 // literal, when the type is omitted.
3304 int depth_;
3305 // The values to put in the composite literal.
3306 Expression_list* vals_;
3307 // If this is true, then VALS_ is a list of pairs: a key and a
3308 // value. In an array initializer, a missing key will be NULL.
3309 bool has_keys_;
3310 // If this is true, then HAS_KEYS_ is true, and every key is a
3311 // simple identifier.
3312 bool all_are_names_;
3313 // A complement to DEPTH that indicates for each level starting from 0 to
3314 // DEPTH-1 whether or not this composite literal is nested inside of key or
3315 // a value. This is used to decide which type to use when given a map literal
3316 // with omitted key types.
3317 std::vector<bool> key_path_;
3320 // Helper/mixin class for struct and array construction expressions;
3321 // encapsulates a list of values plus an optional traversal order
3322 // recording the order in which the values should be visited.
3324 class Ordered_value_list
3326 public:
3327 Ordered_value_list(Expression_list* vals)
3328 : vals_(vals), traverse_order_(NULL)
3331 Expression_list*
3332 vals() const
3333 { return this->vals_; }
3336 traverse_vals(Traverse* traverse);
3338 // Get the traversal order (may be NULL)
3339 std::vector<unsigned long>*
3340 traverse_order()
3341 { return traverse_order_; }
3343 // Set the traversal order, used to ensure that we implement the
3344 // order of evaluation rules. Takes ownership of the argument.
3345 void
3346 set_traverse_order(std::vector<unsigned long>* traverse_order)
3347 { this->traverse_order_ = traverse_order; }
3349 private:
3350 // The list of values, in order of the fields in the struct or in
3351 // order of indices in an array. A NULL value of vals_ means that
3352 // all fields/slots should be zero-initialized; a single NULL entry
3353 // in the list means that the corresponding field or array slot
3354 // should be zero-initialized.
3355 Expression_list* vals_;
3356 // If not NULL, the order in which to traverse vals_. This is used
3357 // so that we implement the order of evaluation rules correctly.
3358 std::vector<unsigned long>* traverse_order_;
3361 // Construct a struct.
3363 class Struct_construction_expression : public Expression,
3364 public Ordered_value_list
3366 public:
3367 Struct_construction_expression(Type* type, Expression_list* vals,
3368 Location location)
3369 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
3370 Ordered_value_list(vals),
3371 type_(type)
3374 // Return whether this is a constant initializer.
3375 bool
3376 is_constant_struct() const;
3378 protected:
3380 do_traverse(Traverse* traverse);
3382 bool
3383 do_is_static_initializer() const;
3385 Type*
3386 do_type()
3387 { return this->type_; }
3389 void
3390 do_determine_type(const Type_context*);
3392 void
3393 do_check_types(Gogo*);
3395 Expression*
3396 do_copy()
3398 Struct_construction_expression* ret =
3399 new Struct_construction_expression(this->type_,
3400 (this->vals() == NULL
3401 ? NULL
3402 : this->vals()->copy()),
3403 this->location());
3404 if (this->traverse_order() != NULL)
3405 ret->set_traverse_order(this->traverse_order());
3406 return ret;
3409 Expression*
3410 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3412 Bexpression*
3413 do_get_backend(Translate_context*);
3415 void
3416 do_export(Export*) const;
3418 void
3419 do_dump_expression(Ast_dump_context*) const;
3421 private:
3422 // The type of the struct to construct.
3423 Type* type_;
3426 // Construct an array. This class is not used directly; instead we
3427 // use the child classes, Fixed_array_construction_expression and
3428 // Slice_construction_expression.
3430 class Array_construction_expression : public Expression,
3431 public Ordered_value_list
3433 protected:
3434 Array_construction_expression(Expression_classification classification,
3435 Type* type,
3436 const std::vector<unsigned long>* indexes,
3437 Expression_list* vals, Location location)
3438 : Expression(classification, location),
3439 Ordered_value_list(vals),
3440 type_(type), indexes_(indexes)
3441 { go_assert(indexes == NULL || indexes->size() == vals->size()); }
3443 public:
3444 // Return whether this is a constant initializer.
3445 bool
3446 is_constant_array() const;
3448 // Return the number of elements.
3449 size_t
3450 element_count() const
3451 { return this->vals() == NULL ? 0 : this->vals()->size(); }
3453 protected:
3454 virtual int
3455 do_traverse(Traverse* traverse);
3457 bool
3458 do_is_static_initializer() const;
3460 Type*
3461 do_type()
3462 { return this->type_; }
3464 void
3465 do_determine_type(const Type_context*);
3467 void
3468 do_check_types(Gogo*);
3470 void
3471 do_export(Export*) const;
3473 // The indexes.
3474 const std::vector<unsigned long>*
3475 indexes()
3476 { return this->indexes_; }
3478 Expression*
3479 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3481 // Get the backend constructor for the array values.
3482 Bexpression*
3483 get_constructor(Translate_context* context, Btype* btype);
3485 void
3486 do_dump_expression(Ast_dump_context*) const;
3488 virtual void
3489 dump_slice_storage_expression(Ast_dump_context*) const { }
3491 private:
3492 // The type of the array to construct.
3493 Type* type_;
3494 // The list of indexes into the array, one for each value. This may
3495 // be NULL, in which case the indexes start at zero and increment.
3496 const std::vector<unsigned long>* indexes_;
3499 // Construct a fixed array.
3501 class Fixed_array_construction_expression :
3502 public Array_construction_expression
3504 public:
3505 Fixed_array_construction_expression(Type* type,
3506 const std::vector<unsigned long>* indexes,
3507 Expression_list* vals, Location location);
3509 protected:
3510 Expression*
3511 do_copy()
3513 return new Fixed_array_construction_expression(this->type(),
3514 this->indexes(),
3515 (this->vals() == NULL
3516 ? NULL
3517 : this->vals()->copy()),
3518 this->location());
3521 Bexpression*
3522 do_get_backend(Translate_context*);
3525 // Construct a slice.
3527 class Slice_construction_expression : public Array_construction_expression
3529 public:
3530 Slice_construction_expression(Type* type,
3531 const std::vector<unsigned long>* indexes,
3532 Expression_list* vals, Location location);
3534 Expression*
3535 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3537 // Record that the storage for this slice (e.g. vals) cannot escape,
3538 // hence it can be stack-allocated.
3539 void
3540 set_storage_does_not_escape()
3542 this->storage_escapes_ = false;
3545 protected:
3546 // Note that taking the address of a slice literal is invalid.
3549 do_traverse(Traverse* traverse);
3551 Expression*
3552 do_copy()
3554 return new Slice_construction_expression(this->type(), this->indexes(),
3555 (this->vals() == NULL
3556 ? NULL
3557 : this->vals()->copy()),
3558 this->location());
3561 Bexpression*
3562 do_get_backend(Translate_context*);
3564 void
3565 dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const;
3567 // Create an array value for the constructed slice. Invoked during
3568 // flattening if slice storage does not escape, otherwise invoked
3569 // later on during do_get_backend().
3570 Expression*
3571 create_array_val();
3573 private:
3574 // The type of the values in this slice.
3575 Type* valtype_;
3576 // Array value expression, optionally filled in during flattening.
3577 Expression* array_val_;
3578 // Slice storage expression, optionally filled in during flattening.
3579 Expression* slice_storage_;
3580 // Normally true. Can be set to false if we know that the resulting
3581 // storage for the slice cannot escape.
3582 bool storage_escapes_;
3585 // Construct a map.
3587 class Map_construction_expression : public Expression
3589 public:
3590 Map_construction_expression(Type* type, Expression_list* vals,
3591 Location location)
3592 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
3593 type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL)
3594 { go_assert(vals == NULL || vals->size() % 2 == 0); }
3596 Expression_list*
3597 vals() const
3598 { return this->vals_; }
3600 protected:
3602 do_traverse(Traverse* traverse);
3604 Expression*
3605 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3607 Type*
3608 do_type()
3609 { return this->type_; }
3611 void
3612 do_determine_type(const Type_context*);
3614 void
3615 do_check_types(Gogo*);
3617 Expression*
3618 do_copy()
3620 return new Map_construction_expression(this->type_,
3621 (this->vals_ == NULL
3622 ? NULL
3623 : this->vals_->copy()),
3624 this->location());
3627 Bexpression*
3628 do_get_backend(Translate_context*);
3630 void
3631 do_export(Export*) const;
3633 void
3634 do_dump_expression(Ast_dump_context*) const;
3636 private:
3637 // The type of the map to construct.
3638 Type* type_;
3639 // The list of values.
3640 Expression_list* vals_;
3641 // The type of the key-value pair struct for each map element.
3642 Struct_type* element_type_;
3643 // A temporary reference to the variable storing the constructor initializer.
3644 Temporary_statement* constructor_temp_;
3647 // A type guard expression.
3649 class Type_guard_expression : public Expression
3651 public:
3652 Type_guard_expression(Expression* expr, Type* type, Location location)
3653 : Expression(EXPRESSION_TYPE_GUARD, location),
3654 expr_(expr), type_(type)
3657 // Return the expression to convert.
3658 Expression*
3659 expr()
3660 { return this->expr_; }
3662 // Return the type to which to convert.
3663 Type*
3664 type()
3665 { return this->type_; }
3667 protected:
3669 do_traverse(Traverse* traverse);
3671 Expression*
3672 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3674 Type*
3675 do_type()
3676 { return this->type_; }
3678 void
3679 do_determine_type(const Type_context*)
3680 { this->expr_->determine_type_no_context(); }
3682 void
3683 do_check_types(Gogo*);
3685 Expression*
3686 do_copy()
3688 return new Type_guard_expression(this->expr_->copy(), this->type_,
3689 this->location());
3692 Bexpression*
3693 do_get_backend(Translate_context*);
3695 void
3696 do_dump_expression(Ast_dump_context*) const;
3698 private:
3699 // The expression to convert.
3700 Expression* expr_;
3701 // The type to which to convert.
3702 Type* type_;
3705 // Class Heap_expression.
3707 // When you take the address of an escaping expression, it is allocated
3708 // on the heap. This class implements that.
3710 class Heap_expression : public Expression
3712 public:
3713 Heap_expression(Expression* expr, Location location)
3714 : Expression(EXPRESSION_HEAP, location),
3715 expr_(expr)
3718 Expression*
3719 expr() const
3720 { return this->expr_; }
3722 protected:
3724 do_traverse(Traverse* traverse)
3725 { return Expression::traverse(&this->expr_, traverse); }
3727 Type*
3728 do_type();
3729 void
3730 do_determine_type(const Type_context*)
3731 { this->expr_->determine_type_no_context(); }
3733 Expression*
3734 do_copy()
3736 return Expression::make_heap_expression(this->expr_->copy(),
3737 this->location());
3740 Bexpression*
3741 do_get_backend(Translate_context*);
3743 // We only export global objects, and the parser does not generate
3744 // this in global scope.
3745 void
3746 do_export(Export*) const
3747 { go_unreachable(); }
3749 void
3750 do_dump_expression(Ast_dump_context*) const;
3752 private:
3753 // The expression which is being put on the heap.
3754 Expression* expr_;
3757 // A receive expression.
3759 class Receive_expression : public Expression
3761 public:
3762 Receive_expression(Expression* channel, Location location)
3763 : Expression(EXPRESSION_RECEIVE, location),
3764 channel_(channel), temp_receiver_(NULL)
3767 // Return the channel.
3768 Expression*
3769 channel()
3770 { return this->channel_; }
3772 protected:
3774 do_traverse(Traverse* traverse)
3775 { return Expression::traverse(&this->channel_, traverse); }
3777 bool
3778 do_discarding_value()
3779 { return true; }
3781 Type*
3782 do_type();
3784 Expression*
3785 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3787 void
3788 do_determine_type(const Type_context*)
3789 { this->channel_->determine_type_no_context(); }
3791 void
3792 do_check_types(Gogo*);
3794 Expression*
3795 do_copy()
3797 return Expression::make_receive(this->channel_->copy(), this->location());
3800 bool
3801 do_must_eval_in_order() const
3802 { return true; }
3804 Bexpression*
3805 do_get_backend(Translate_context*);
3807 void
3808 do_dump_expression(Ast_dump_context*) const;
3810 private:
3811 // The channel from which we are receiving.
3812 Expression* channel_;
3813 // A temporary reference to the variable storing the received data.
3814 Temporary_statement* temp_receiver_;
3817 // Conditional expressions.
3819 class Conditional_expression : public Expression
3821 public:
3822 Conditional_expression(Expression* cond, Expression* then_expr,
3823 Expression* else_expr, Location location)
3824 : Expression(EXPRESSION_CONDITIONAL, location),
3825 cond_(cond), then_(then_expr), else_(else_expr)
3828 Expression*
3829 condition() const
3830 { return this->cond_; }
3832 protected:
3834 do_traverse(Traverse*);
3836 Type*
3837 do_type();
3839 void
3840 do_determine_type(const Type_context*);
3842 Expression*
3843 do_copy()
3845 return new Conditional_expression(this->cond_->copy(), this->then_->copy(),
3846 this->else_->copy(), this->location());
3849 Bexpression*
3850 do_get_backend(Translate_context* context);
3852 void
3853 do_dump_expression(Ast_dump_context*) const;
3855 private:
3856 // The condition to be checked.
3857 Expression* cond_;
3858 // The expression to execute if the condition is true.
3859 Expression* then_;
3860 // The expression to execute if the condition is false.
3861 Expression* else_;
3864 // Compound expressions.
3866 class Compound_expression : public Expression
3868 public:
3869 Compound_expression(Expression* init, Expression* expr, Location location)
3870 : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr)
3873 Expression*
3874 init() const
3875 { return this->init_; }
3877 protected:
3879 do_traverse(Traverse*);
3881 Type*
3882 do_type();
3884 void
3885 do_determine_type(const Type_context*);
3887 Expression*
3888 do_copy()
3890 return new Compound_expression(this->init_->copy(), this->expr_->copy(),
3891 this->location());
3894 Bexpression*
3895 do_get_backend(Translate_context* context);
3897 void
3898 do_dump_expression(Ast_dump_context*) const;
3900 private:
3901 // The expression that is evaluated first and discarded.
3902 Expression* init_;
3903 // The expression that is evaluated and returned.
3904 Expression* expr_;
3907 // A backend expression. This is a backend expression wrapped in an
3908 // Expression, for convenience during backend generation.
3910 class Backend_expression : public Expression
3912 public:
3913 Backend_expression(Bexpression* bexpr, Type* type, Location location)
3914 : Expression(EXPRESSION_BACKEND, location), bexpr_(bexpr), type_(type)
3917 protected:
3919 do_traverse(Traverse*);
3921 // For now these are always valid static initializers. If that
3922 // changes we can change this.
3923 bool
3924 do_is_static_initializer() const
3925 { return true; }
3927 Type*
3928 do_type()
3929 { return this->type_; }
3931 void
3932 do_determine_type(const Type_context*)
3935 Expression*
3936 do_copy()
3938 return new Backend_expression(this->bexpr_, this->type_, this->location());
3941 Bexpression*
3942 do_get_backend(Translate_context*)
3943 { return this->bexpr_; }
3945 void
3946 do_dump_expression(Ast_dump_context*) const;
3948 private:
3949 // The backend expression we are wrapping.
3950 Bexpression* bexpr_;
3951 // The type of the expression;
3952 Type* type_;
3955 // A numeric constant. This is used both for untyped constants and
3956 // for constants that have a type.
3958 class Numeric_constant
3960 public:
3961 Numeric_constant()
3962 : classification_(NC_INVALID), type_(NULL)
3965 ~Numeric_constant();
3967 Numeric_constant(const Numeric_constant&);
3969 Numeric_constant& operator=(const Numeric_constant&);
3971 // Set to an unsigned long value.
3972 void
3973 set_unsigned_long(Type*, unsigned long);
3975 // Set to an integer value.
3976 void
3977 set_int(Type*, const mpz_t);
3979 // Set to a rune value.
3980 void
3981 set_rune(Type*, const mpz_t);
3983 // Set to a floating point value.
3984 void
3985 set_float(Type*, const mpfr_t);
3987 // Set to a complex value.
3988 void
3989 set_complex(Type*, const mpc_t);
3991 // Mark numeric constant as invalid.
3992 void
3993 set_invalid()
3994 { this->classification_ = NC_INVALID; }
3996 // Classifiers.
3997 bool
3998 is_int() const
3999 { return this->classification_ == Numeric_constant::NC_INT; }
4001 bool
4002 is_rune() const
4003 { return this->classification_ == Numeric_constant::NC_RUNE; }
4005 bool
4006 is_float() const
4007 { return this->classification_ == Numeric_constant::NC_FLOAT; }
4009 bool
4010 is_complex() const
4011 { return this->classification_ == Numeric_constant::NC_COMPLEX; }
4013 bool
4014 is_invalid() const
4015 { return this->classification_ == Numeric_constant::NC_INVALID; }
4017 // Value retrievers. These will initialize the values as well as
4018 // set them. GET_INT is only valid if IS_INT returns true, and
4019 // likewise respectively.
4020 void
4021 get_int(mpz_t*) const;
4023 void
4024 get_rune(mpz_t*) const;
4026 void
4027 get_float(mpfr_t*) const;
4029 void
4030 get_complex(mpc_t*) const;
4032 // Codes returned by to_unsigned_long.
4033 enum To_unsigned_long
4035 // Value is integer and fits in unsigned long.
4036 NC_UL_VALID,
4037 // Value is not integer.
4038 NC_UL_NOTINT,
4039 // Value is integer but is negative.
4040 NC_UL_NEGATIVE,
4041 // Value is non-negative integer but does not fit in unsigned
4042 // long.
4043 NC_UL_BIG
4046 // If the value can be expressed as an integer that fits in an
4047 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
4048 // one of the other To_unsigned_long codes.
4049 To_unsigned_long
4050 to_unsigned_long(unsigned long* val) const;
4052 // If the value can be expressed as an integer that describes the
4053 // size of an object in memory, set *VAL and return true.
4054 // Otherwise, return false. Currently we use int64_t to represent a
4055 // memory size, as in Type::backend_type_size.
4056 bool
4057 to_memory_size(int64_t* val) const;
4059 // If the value can be expressed as an int, return true and
4060 // initialize and set VAL. This will return false for a value with
4061 // an explicit float or complex type, even if the value is integral.
4062 bool
4063 to_int(mpz_t* val) const;
4065 // If the value can be expressed as a float, return true and
4066 // initialize and set VAL.
4067 bool
4068 to_float(mpfr_t* val) const;
4070 // If the value can be expressed as a complex, return true and
4071 // initialize and set VR and VI.
4072 bool
4073 to_complex(mpc_t* val) const;
4075 // Get the type.
4076 Type*
4077 type() const;
4079 // If the constant can be expressed in TYPE, then set the type of
4080 // the constant to TYPE and return true. Otherwise return false,
4081 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
4082 // the location to use for the error.
4083 bool
4084 set_type(Type* type, bool issue_error, Location location);
4086 // Return an Expression for this value.
4087 Expression*
4088 expression(Location) const;
4090 private:
4091 void
4092 clear();
4094 To_unsigned_long
4095 mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
4097 To_unsigned_long
4098 mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
4100 bool
4101 mpz_to_memory_size(const mpz_t ival, int64_t* val) const;
4103 bool
4104 mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const;
4106 bool
4107 check_int_type(Integer_type*, bool, Location);
4109 bool
4110 check_float_type(Float_type*, bool, Location);
4112 bool
4113 check_complex_type(Complex_type*, bool, Location);
4115 // The kinds of constants.
4116 enum Classification
4118 NC_INVALID,
4119 NC_RUNE,
4120 NC_INT,
4121 NC_FLOAT,
4122 NC_COMPLEX
4125 // The kind of constant.
4126 Classification classification_;
4127 // The value.
4128 union
4130 // If NC_INT or NC_RUNE.
4131 mpz_t int_val;
4132 // If NC_FLOAT.
4133 mpfr_t float_val;
4134 // If NC_COMPLEX.
4135 mpc_t complex_val;
4136 } u_;
4137 // The type if there is one. This will be NULL for an untyped
4138 // constant.
4139 Type* type_;
4142 #endif // !defined(GO_EXPRESSIONS_H)