0d00f458c38d32ffefa9bea5f4a71d39d8e04b9c
[gcc.git] / gcc / go / gofrontend / expressions.h
1 // expressions.h -- Go frontend expression handling. -*- C++ -*-
2
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.
6
7 #ifndef GO_EXPRESSIONS_H
8 #define GO_EXPRESSIONS_H
9
10 #include <mpfr.h>
11 #include <mpc.h>
12
13 #include "operator.h"
14 #include "runtime.h"
15
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;
73
74 // The precision to use for complex values represented as an mpc_t.
75 const int mpc_precision = 256;
76
77 // The base class for all expressions.
78
79 class Expression
80 {
81 public:
82 // The types of expressions.
83 enum Expression_classification
84 {
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_TYPE_INFO,
132 EXPRESSION_SLICE_INFO,
133 EXPRESSION_SLICE_VALUE,
134 EXPRESSION_INTERFACE_INFO,
135 EXPRESSION_INTERFACE_VALUE,
136 EXPRESSION_INTERFACE_MTABLE,
137 EXPRESSION_STRUCT_FIELD_OFFSET,
138 EXPRESSION_LABEL_ADDR,
139 EXPRESSION_CONDITIONAL,
140 EXPRESSION_COMPOUND
141 };
142
143 Expression(Expression_classification, Location);
144
145 virtual ~Expression();
146
147 // Make an error expression. This is used when a parse error occurs
148 // to prevent cascading errors.
149 static Expression*
150 make_error(Location);
151
152 // Make an expression which is really a type. This is used during
153 // parsing.
154 static Expression*
155 make_type(Type*, Location);
156
157 // Make a unary expression.
158 static Expression*
159 make_unary(Operator, Expression*, Location);
160
161 // Make a binary expression.
162 static Expression*
163 make_binary(Operator, Expression*, Expression*, Location);
164
165 // Make a string concatenation expression.
166 static Expression*
167 make_string_concat(Expression_list*);
168
169 // Make a reference to a constant in an expression.
170 static Expression*
171 make_const_reference(Named_object*, Location);
172
173 // Make a reference to a variable in an expression.
174 static Expression*
175 make_var_reference(Named_object*, Location);
176
177 // Make a reference to a variable within an enclosing function.
178 static Expression*
179 make_enclosing_var_reference(Expression*, Named_object*, Location);
180
181 // Make a reference to a temporary variable. Temporary variables
182 // are always created by a single statement, which is what we use to
183 // refer to them.
184 static Temporary_reference_expression*
185 make_temporary_reference(Temporary_statement*, Location);
186
187 // Make an expressions which sets a temporary variable and then
188 // evaluates to a reference to that temporary variable. This is
189 // used to set a temporary variable while retaining the order of
190 // evaluation.
191 static Set_and_use_temporary_expression*
192 make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
193
194 // Make a sink expression--a reference to the blank identifier _.
195 static Expression*
196 make_sink(Location);
197
198 // Make a reference to a function in an expression. This returns a
199 // pointer to the struct holding the address of the function
200 // followed by any closed-over variables.
201 static Expression*
202 make_func_reference(Named_object*, Expression* closure, Location);
203
204 // Make a function descriptor, an immutable struct with a single
205 // field that points to the function code. This may only be used
206 // with functions that do not have closures. FN is the function for
207 // which we are making the descriptor.
208 static Func_descriptor_expression*
209 make_func_descriptor(Named_object* fn);
210
211 // Make a reference to the code of a function. This is used to set
212 // descriptor and closure fields.
213 static Expression*
214 make_func_code_reference(Named_object*, Location);
215
216 // Make a reference to an unknown name. In a correct program this
217 // will always be lowered to a real const/var/func reference.
218 static Unknown_expression*
219 make_unknown_reference(Named_object*, Location);
220
221 // Make a constant bool expression.
222 static Expression*
223 make_boolean(bool val, Location);
224
225 // Make a constant string expression.
226 static Expression*
227 make_string(const std::string&, Location);
228
229 // Make an expression that evaluates to some characteristic of an string.
230 // For simplicity, the enum values must match the field indexes in the
231 // underlying struct.
232 enum String_info
233 {
234 // The underlying data in the string.
235 STRING_INFO_DATA,
236 // The length of the string.
237 STRING_INFO_LENGTH
238 };
239
240 static Expression*
241 make_string_info(Expression* string, String_info, Location);
242
243 // Make a character constant expression. TYPE should be NULL for an
244 // abstract type.
245 static Expression*
246 make_character(const mpz_t*, Type*, Location);
247
248 // Make a constant integer expression from a multi-precision
249 // integer. TYPE should be NULL for an abstract type.
250 static Expression*
251 make_integer_z(const mpz_t*, Type*, Location);
252
253 // Make a constant integer expression from an unsigned long. TYPE
254 // should be NULL for an abstract type.
255 static Expression*
256 make_integer_ul(unsigned long, Type*, Location);
257
258 // Make a constant integer expression from a signed long. TYPE
259 // should be NULL for an abstract type.
260 static Expression*
261 make_integer_sl(long, Type*, Location);
262
263 // Make a constant integer expression from an int64_t. TYPE should
264 // be NULL for an abstract type.
265 static Expression*
266 make_integer_int64(int64_t, Type*, Location);
267
268 // Make a constant float expression. TYPE should be NULL for an
269 // abstract type.
270 static Expression*
271 make_float(const mpfr_t*, Type*, Location);
272
273 // Make a constant complex expression. TYPE should be NULL for an
274 // abstract type.
275 static Expression*
276 make_complex(const mpc_t*, Type*, Location);
277
278 // Make a nil expression.
279 static Expression*
280 make_nil(Location);
281
282 // Make an iota expression. This is used for the predeclared
283 // constant iota.
284 static Expression*
285 make_iota();
286
287 // Make a call expression.
288 static Call_expression*
289 make_call(Expression* func, Expression_list* args, bool is_varargs,
290 Location);
291
292 // Make a reference to a specific result of a call expression which
293 // returns a tuple.
294 static Expression*
295 make_call_result(Call_expression*, unsigned int index);
296
297 // Make an expression which is a method bound to its first
298 // parameter. METHOD is the method being called, FUNCTION is the
299 // function to call.
300 static Bound_method_expression*
301 make_bound_method(Expression* object, const Method* method,
302 Named_object* function, Location);
303
304 // Make an index or slice expression. This is a parser expression
305 // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
306 // index rather than a slice. CAP may be NULL, meaning we use the default
307 // capacity of LEFT. At parse time we may not know the type of LEFT.
308 // After parsing this is lowered to an array index, a string index,
309 // or a map index.
310 static Expression*
311 make_index(Expression* left, Expression* start, Expression* end,
312 Expression* cap, Location);
313
314 // Make an array index expression. END may be NULL, in which case
315 // this is an lvalue. CAP may be NULL, in which case it defaults
316 // to cap(ARRAY).
317 static Expression*
318 make_array_index(Expression* array, Expression* start, Expression* end,
319 Expression* cap, Location);
320
321 // Make a string index expression. END may be NULL. This is never
322 // an lvalue.
323 static Expression*
324 make_string_index(Expression* string, Expression* start, Expression* end,
325 Location);
326
327 // Make a map index expression. This is an lvalue.
328 static Map_index_expression*
329 make_map_index(Expression* map, Expression* val, Location);
330
331 // Make a selector. This is a parser expression which represents
332 // LEFT.NAME. At parse time we may not know the type of the left
333 // hand side.
334 static Expression*
335 make_selector(Expression* left, const std::string& name, Location);
336
337 // Make a reference to a field in a struct.
338 static Field_reference_expression*
339 make_field_reference(Expression*, unsigned int field_index, Location);
340
341 // Make a reference to a field of an interface, with an associated
342 // object.
343 static Expression*
344 make_interface_field_reference(Expression*, const std::string&,
345 Location);
346
347 // Make an allocation expression.
348 static Expression*
349 make_allocation(Type*, Location);
350
351 // Make a type guard expression.
352 static Expression*
353 make_type_guard(Expression*, Type*, Location);
354
355 // Make a type cast expression.
356 static Expression*
357 make_cast(Type*, Expression*, Location);
358
359 // Make an unsafe type cast expression. This is only used when
360 // passing parameter to builtin functions that are part of the Go
361 // runtime.
362 static Expression*
363 make_unsafe_cast(Type*, Expression*, Location);
364
365 // Make a composite literal. The DEPTH parameter is how far down we
366 // are in a list of composite literals with omitted types. HAS_KEYS
367 // is true if the expression list has keys alternating with values.
368 // ALL_ARE_NAMES is true if all the keys could be struct field
369 // names.
370 static Expression*
371 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
372 bool all_are_names, Location);
373
374 // Make a struct composite literal.
375 static Expression*
376 make_struct_composite_literal(Type*, Expression_list*, Location);
377
378 // Make an array composite literal.
379 static Expression*
380 make_array_composite_literal(Type*, Expression_list*, Location);
381
382 // Make a slice composite literal.
383 static Slice_construction_expression*
384 make_slice_composite_literal(Type*, Expression_list*, Location);
385
386 // Take an expression and allocate it on the heap.
387 static Expression*
388 make_heap_expression(Expression*, Location);
389
390 // Make a receive expression. VAL is NULL for a unary receive.
391 static Receive_expression*
392 make_receive(Expression* channel, Location);
393
394 // Make an expression which evaluates to the address of the type
395 // descriptor for TYPE.
396 static Expression*
397 make_type_descriptor(Type* type, Location);
398
399 // Make an expression which evaluates to the address of the gc
400 // symbol for TYPE.
401 static Expression*
402 make_gc_symbol(Type* type);
403
404 // Make an expression which evaluates to some characteristic of a
405 // type. These are only used for type descriptors, so there is no
406 // location parameter.
407 enum Type_info
408 {
409 // The size of a value of the type.
410 TYPE_INFO_SIZE,
411 // The required alignment of a value of the type.
412 TYPE_INFO_ALIGNMENT,
413 // The required alignment of a value of the type when used as a
414 // field in a struct.
415 TYPE_INFO_FIELD_ALIGNMENT
416 };
417
418 static Expression*
419 make_type_info(Type* type, Type_info);
420
421 // Make an expression that evaluates to some characteristic of a
422 // slice. For simplicity, the enum values must match the field indexes
423 // in the underlying struct.
424 enum Slice_info
425 {
426 // The underlying data of the slice.
427 SLICE_INFO_VALUE_POINTER,
428 // The length of the slice.
429 SLICE_INFO_LENGTH,
430 // The capacity of the slice.
431 SLICE_INFO_CAPACITY
432 };
433
434 static Expression*
435 make_slice_info(Expression* slice, Slice_info, Location);
436
437 // Make an expression for a slice value.
438 static Expression*
439 make_slice_value(Type*, Expression* valptr, Expression* len, Expression* cap,
440 Location);
441
442 // Make an expression that evaluates to some characteristic of an
443 // interface. For simplicity, the enum values must match the field indexes
444 // in the underlying struct.
445 enum Interface_info
446 {
447 // The type descriptor of an empty interface.
448 INTERFACE_INFO_TYPE_DESCRIPTOR = 0,
449 // The methods of an interface.
450 INTERFACE_INFO_METHODS = 0,
451 // The first argument to pass to an interface method.
452 INTERFACE_INFO_OBJECT
453 };
454
455 static Expression*
456 make_interface_info(Expression* iface, Interface_info, Location);
457
458 // Make an expression for an interface value.
459 static Expression*
460 make_interface_value(Type*, Expression*, Expression*, Location);
461
462 // Make an expression that builds a reference to the interface method table
463 // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
464 // reference to the interface method table for the pointer receiver type.
465 static Expression*
466 make_interface_mtable_ref(Interface_type* itype, Type* type,
467 bool is_pointer, Location);
468
469 // Make an expression which evaluates to the offset of a field in a
470 // struct. This is only used for type descriptors, so there is no
471 // location parameter.
472 static Expression*
473 make_struct_field_offset(Struct_type*, const Struct_field*);
474
475 // Make an expression which evaluates to the address of an unnamed
476 // label.
477 static Expression*
478 make_label_addr(Label*, Location);
479
480 // Make a conditional expression.
481 static Expression*
482 make_conditional(Expression*, Expression*, Expression*, Location);
483
484 // Make a compound expression.
485 static Expression*
486 make_compound(Expression*, Expression*, Location);
487
488 // Return the expression classification.
489 Expression_classification
490 classification() const
491 { return this->classification_; }
492
493 // Return the location of the expression.
494 Location
495 location() const
496 { return this->location_; }
497
498 // Return whether this is a constant expression.
499 bool
500 is_constant() const
501 { return this->do_is_constant(); }
502
503 // Return whether this is an immutable expression.
504 bool
505 is_immutable() const
506 { return this->do_is_immutable(); }
507
508 // If this is not a numeric constant, return false. If it is one,
509 // return true, and set VAL to hold the value.
510 bool
511 numeric_constant_value(Numeric_constant* val) const
512 { return this->do_numeric_constant_value(val); }
513
514 // If this is not a constant expression with string type, return
515 // false. If it is one, return true, and set VAL to the value.
516 bool
517 string_constant_value(std::string* val) const
518 { return this->do_string_constant_value(val); }
519
520 // This is called if the value of this expression is being
521 // discarded. This issues warnings about computed values being
522 // unused. This returns true if all is well, false if it issued an
523 // error message.
524 bool
525 discarding_value()
526 { return this->do_discarding_value(); }
527
528 // Return whether this is an error expression.
529 bool
530 is_error_expression() const
531 { return this->classification_ == EXPRESSION_ERROR; }
532
533 // Return whether this expression really represents a type.
534 bool
535 is_type_expression() const
536 { return this->classification_ == EXPRESSION_TYPE; }
537
538 // If this is a variable reference, return the Var_expression
539 // structure. Otherwise, return NULL. This is a controlled dynamic
540 // cast.
541 Var_expression*
542 var_expression()
543 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
544
545 const Var_expression*
546 var_expression() const
547 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
548
549 // If this is a enclosed_variable reference, return the
550 // Enclosed_var_expression structure. Otherwise, return NULL.
551 // This is a controlled dynamic cast.
552 Enclosed_var_expression*
553 enclosed_var_expression()
554 { return this->convert<Enclosed_var_expression,
555 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
556
557 const Enclosed_var_expression*
558 enclosed_var_expression() const
559 { return this->convert<const Enclosed_var_expression,
560 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
561
562
563 // If this is a reference to a temporary variable, return the
564 // Temporary_reference_expression. Otherwise, return NULL.
565 Temporary_reference_expression*
566 temporary_reference_expression()
567 {
568 return this->convert<Temporary_reference_expression,
569 EXPRESSION_TEMPORARY_REFERENCE>();
570 }
571
572 // If this is a set-and-use-temporary, return the
573 // Set_and_use_temporary_expression. Otherwise, return NULL.
574 Set_and_use_temporary_expression*
575 set_and_use_temporary_expression()
576 {
577 return this->convert<Set_and_use_temporary_expression,
578 EXPRESSION_SET_AND_USE_TEMPORARY>();
579 }
580
581 // Return whether this is a sink expression.
582 bool
583 is_sink_expression() const
584 { return this->classification_ == EXPRESSION_SINK; }
585
586 // If this is a string expression, return the String_expression
587 // structure. Otherwise, return NULL.
588 String_expression*
589 string_expression()
590 { return this->convert<String_expression, EXPRESSION_STRING>(); }
591
592 // If this is a conversion expression, return the Type_conversion_expression
593 // structure. Otherwise, return NULL.
594 Type_conversion_expression*
595 conversion_expression()
596 { return this->convert<Type_conversion_expression, EXPRESSION_CONVERSION>(); }
597
598 // If this is an unsafe conversion expression, return the
599 // Unsafe_type_conversion_expression structure. Otherwise, return NULL.
600 Unsafe_type_conversion_expression*
601 unsafe_conversion_expression()
602 {
603 return this->convert<Unsafe_type_conversion_expression,
604 EXPRESSION_UNSAFE_CONVERSION>();
605 }
606
607 // Return whether this is the expression nil.
608 bool
609 is_nil_expression() const
610 { return this->classification_ == EXPRESSION_NIL; }
611
612 // If this is an indirection through a pointer, return the
613 // expression being pointed through. Otherwise return this.
614 Expression*
615 deref();
616
617 // If this is a unary expression, return the Unary_expression
618 // structure. Otherwise return NULL.
619 Unary_expression*
620 unary_expression()
621 { return this->convert<Unary_expression, EXPRESSION_UNARY>(); }
622
623 // If this is a binary expression, return the Binary_expression
624 // structure. Otherwise return NULL.
625 Binary_expression*
626 binary_expression()
627 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
628
629 // If this is a string concatenation expression, return the
630 // String_concat_expression structure. Otherwise, return NULL.
631 String_concat_expression*
632 string_concat_expression()
633 {
634 return this->convert<String_concat_expression, EXPRESSION_STRING_CONCAT>();
635 }
636
637 // If this is a call expression, return the Call_expression
638 // structure. Otherwise, return NULL. This is a controlled dynamic
639 // cast.
640 Call_expression*
641 call_expression()
642 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
643
644 // If this is a call_result expression, return the Call_result_expression
645 // structure. Otherwise, return NULL. This is a controlled dynamic
646 // cast.
647 Call_result_expression*
648 call_result_expression()
649 { return this->convert<Call_result_expression, EXPRESSION_CALL_RESULT>(); }
650
651 // If this is an expression which refers to a function, return the
652 // Func_expression structure. Otherwise, return NULL.
653 Func_expression*
654 func_expression()
655 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
656
657 const Func_expression*
658 func_expression() const
659 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
660
661 // If this is an expression which refers to an unknown name, return
662 // the Unknown_expression structure. Otherwise, return NULL.
663 Unknown_expression*
664 unknown_expression()
665 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
666
667 const Unknown_expression*
668 unknown_expression() const
669 {
670 return this->convert<const Unknown_expression,
671 EXPRESSION_UNKNOWN_REFERENCE>();
672 }
673
674 // If this is an index expression, return the Index_expression
675 // structure. Otherwise, return NULL.
676 Index_expression*
677 index_expression()
678 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
679
680 // If this is an expression which refers to indexing in a array,
681 // return the Array_index_expression structure. Otherwise, return
682 // NULL.
683 Array_index_expression*
684 array_index_expression()
685 { return this->convert<Array_index_expression, EXPRESSION_ARRAY_INDEX>(); }
686
687 // If this is an expression which refers to indexing in a string,
688 // return the String_index_expression structure. Otherwise, return
689 // NULL.
690 String_index_expression*
691 string_index_expression()
692 { return this->convert<String_index_expression, EXPRESSION_STRING_INDEX>(); }
693
694 // If this is an expression which refers to indexing in a map,
695 // return the Map_index_expression structure. Otherwise, return
696 // NULL.
697 Map_index_expression*
698 map_index_expression()
699 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
700
701 // If this is a bound method expression, return the
702 // Bound_method_expression structure. Otherwise, return NULL.
703 Bound_method_expression*
704 bound_method_expression()
705 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
706
707 // If this is a reference to a field in a struct, return the
708 // Field_reference_expression structure. Otherwise, return NULL.
709 Field_reference_expression*
710 field_reference_expression()
711 {
712 return this->convert<Field_reference_expression,
713 EXPRESSION_FIELD_REFERENCE>();
714 }
715
716 // If this is a reference to a field in an interface, return the
717 // Interface_field_reference_expression structure. Otherwise,
718 // return NULL.
719 Interface_field_reference_expression*
720 interface_field_reference_expression()
721 {
722 return this->convert<Interface_field_reference_expression,
723 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
724 }
725
726 // If this is an allocation expression, return the Allocation_expression
727 // structure. Otherwise, return NULL.
728 Allocation_expression*
729 allocation_expression()
730 { return this->convert<Allocation_expression, EXPRESSION_ALLOCATION>(); }
731
732 // If this is a general composite literal, return the
733 // Composite_literal_expression structure. Otherwise, return NULL.
734 Composite_literal_expression*
735 complit()
736 {
737 return this->convert<Composite_literal_expression,
738 EXPRESSION_COMPOSITE_LITERAL>();
739 }
740
741 // If this is a struct composite literal, return the
742 // Struct_construction_expression structure. Otherwise, return NULL.
743 Struct_construction_expression*
744 struct_literal()
745 {
746 return this->convert<Struct_construction_expression,
747 EXPRESSION_STRUCT_CONSTRUCTION>();
748 }
749
750 // If this is a array composite literal, return the
751 // Array_construction_expression structure. Otherwise, return NULL.
752 Fixed_array_construction_expression*
753 array_literal()
754 {
755 return this->convert<Fixed_array_construction_expression,
756 EXPRESSION_FIXED_ARRAY_CONSTRUCTION>();
757 }
758
759 // If this is a slice composite literal, return the
760 // Slice_construction_expression structure. Otherwise, return NULL.
761 Slice_construction_expression*
762 slice_literal()
763 {
764 return this->convert<Slice_construction_expression,
765 EXPRESSION_SLICE_CONSTRUCTION>();
766 }
767
768 // If this is a map composite literal, return the
769 // Map_construction_expression structure. Otherwise, return NULL.
770 Map_construction_expression*
771 map_literal()
772 {
773 return this->convert<Map_construction_expression,
774 EXPRESSION_MAP_CONSTRUCTION>();
775 }
776
777 // If this is a type guard expression, return the
778 // Type_guard_expression structure. Otherwise, return NULL.
779 Type_guard_expression*
780 type_guard_expression()
781 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
782
783 // If this is a heap expression, returhn the Heap_expression structure.
784 // Otherwise, return NULL.
785 Heap_expression*
786 heap_expression()
787 { return this->convert<Heap_expression, EXPRESSION_HEAP>(); }
788
789 // If this is a receive expression, return the Receive_expression
790 // structure. Otherwise, return NULL.
791 Receive_expression*
792 receive_expression()
793 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
794
795 // If this is a conditional expression, return the Conditional_expression
796 // structure. Otherwise, return NULL.
797 Conditional_expression*
798 conditional_expression()
799 { return this->convert<Conditional_expression, EXPRESSION_CONDITIONAL>(); }
800
801 // If this is a compound expression, return the Compound_expression structure.
802 // Otherwise, return NULL.
803 Compound_expression*
804 compound_expression()
805 { return this->convert<Compound_expression, EXPRESSION_COMPOUND>(); }
806
807 // Return true if this is a composite literal.
808 bool
809 is_composite_literal() const;
810
811 // Return true if this is a composite literal which is not constant.
812 bool
813 is_nonconstant_composite_literal() const;
814
815 // Return true if this is a variable or temporary variable.
816 bool
817 is_variable() const;
818
819 // Return true if this is a reference to a local variable.
820 bool
821 is_local_variable() const;
822
823 // Make the builtin function descriptor type, so that it can be
824 // converted.
825 static void
826 make_func_descriptor_type();
827
828 // Traverse an expression.
829 static int
830 traverse(Expression**, Traverse*);
831
832 // Traverse subexpressions of this expression.
833 int
834 traverse_subexpressions(Traverse*);
835
836 // Lower an expression. This is called immediately after parsing.
837 // FUNCTION is the function we are in; it will be NULL for an
838 // expression initializing a global variable. INSERTER may be used
839 // to insert statements before the statement or initializer
840 // containing this expression; it is normally used to create
841 // temporary variables. IOTA_VALUE is the value that we should give
842 // to any iota expressions. This function must resolve expressions
843 // which could not be fully parsed into their final form. It
844 // returns the same Expression or a new one.
845 Expression*
846 lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
847 int iota_value)
848 { return this->do_lower(gogo, function, inserter, iota_value); }
849
850 // Flatten an expression. This is called after order_evaluation.
851 // FUNCTION is the function we are in; it will be NULL for an
852 // expression initializing a global variable. INSERTER may be used
853 // to insert statements before the statement or initializer
854 // containing this expression; it is normally used to create
855 // temporary variables. This function must resolve expressions
856 // which could not be fully parsed into their final form. It
857 // returns the same Expression or a new one.
858 Expression*
859 flatten(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
860 { return this->do_flatten(gogo, function, inserter); }
861
862 // Determine the real type of an expression with abstract integer,
863 // floating point, or complex type. TYPE_CONTEXT describes the
864 // expected type.
865 void
866 determine_type(const Type_context*);
867
868 // Check types in an expression.
869 void
870 check_types(Gogo* gogo)
871 { this->do_check_types(gogo); }
872
873 // Determine the type when there is no context.
874 void
875 determine_type_no_context();
876
877 // Return the current type of the expression. This may be changed
878 // by determine_type.
879 Type*
880 type()
881 { return this->do_type(); }
882
883 // Return a copy of an expression.
884 Expression*
885 copy()
886 { return this->do_copy(); }
887
888 // Return whether the expression is addressable--something which may
889 // be used as the operand of the unary & operator.
890 bool
891 is_addressable() const
892 { return this->do_is_addressable(); }
893
894 // Note that we are taking the address of this expression. ESCAPES
895 // is true if this address escapes the current function.
896 void
897 address_taken(bool escapes)
898 { this->do_address_taken(escapes); }
899
900 // Note that a nil check must be issued for this expression.
901 void
902 issue_nil_check()
903 { this->do_issue_nil_check(); }
904
905 // Return whether this expression must be evaluated in order
906 // according to the order of evaluation rules. This is basically
907 // true of all expressions with side-effects.
908 bool
909 must_eval_in_order() const
910 { return this->do_must_eval_in_order(); }
911
912 // Return whether subexpressions of this expression must be
913 // evaluated in order. This is true of index expressions and
914 // pointer indirections. This sets *SKIP to the number of
915 // subexpressions to skip during traversing, as index expressions
916 // only requiring moving the index, not the array.
917 bool
918 must_eval_subexpressions_in_order(int* skip) const
919 {
920 *skip = 0;
921 return this->do_must_eval_subexpressions_in_order(skip);
922 }
923
924 // Return the backend representation for this expression.
925 Bexpression*
926 get_backend(Translate_context*);
927
928 // Return an expression handling any conversions which must be done during
929 // assignment.
930 static Expression*
931 convert_for_assignment(Gogo*, Type* lhs_type, Expression* rhs,
932 Location location);
933
934 // Return an expression converting a value of one interface type to another
935 // interface type. If FOR_TYPE_GUARD is true this is for a type
936 // assertion.
937 static Expression*
938 convert_interface_to_interface(Type* lhs_type,
939 Expression* rhs, bool for_type_guard,
940 Location);
941
942 // Return a backend expression implementing the comparison LEFT OP RIGHT.
943 // TYPE is the type of both sides.
944 static Bexpression*
945 comparison(Translate_context*, Type* result_type, Operator op,
946 Expression* left, Expression* right, Location);
947
948 // Return the backend expression for the numeric constant VAL.
949 static Bexpression*
950 backend_numeric_constant_expression(Translate_context*,
951 Numeric_constant* val);
952
953 // Export the expression. This is only used for constants. It will
954 // be used for things like values of named constants and sizes of
955 // arrays.
956 void
957 export_expression(Export* exp) const
958 { this->do_export(exp); }
959
960 // Import an expression.
961 static Expression*
962 import_expression(Import*);
963
964 // Return an expression which checks that VAL, of arbitrary integer type,
965 // is non-negative and is not more than the maximum integer value.
966 static Expression*
967 check_bounds(Expression* val, Location);
968
969 // Dump an expression to a dump constext.
970 void
971 dump_expression(Ast_dump_context*) const;
972
973 protected:
974 // May be implemented by child class: traverse the expressions.
975 virtual int
976 do_traverse(Traverse*);
977
978 // Return a lowered expression.
979 virtual Expression*
980 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
981 { return this; }
982
983 // Return a flattened expression.
984 virtual Expression*
985 do_flatten(Gogo*, Named_object*, Statement_inserter*)
986 { return this; }
987
988
989 // Return whether this is a constant expression.
990 virtual bool
991 do_is_constant() const
992 { return false; }
993
994 // Return whether this is an immutable expression.
995 virtual bool
996 do_is_immutable() const
997 { return false; }
998
999 // Return whether this is a constant expression of numeric type, and
1000 // set the Numeric_constant to the value.
1001 virtual bool
1002 do_numeric_constant_value(Numeric_constant*) const
1003 { return false; }
1004
1005 // Return whether this is a constant expression of string type, and
1006 // set VAL to the value.
1007 virtual bool
1008 do_string_constant_value(std::string*) const
1009 { return false; }
1010
1011 // Called by the parser if the value is being discarded.
1012 virtual bool
1013 do_discarding_value();
1014
1015 // Child class holds type.
1016 virtual Type*
1017 do_type() = 0;
1018
1019 // Child class implements determining type information.
1020 virtual void
1021 do_determine_type(const Type_context*) = 0;
1022
1023 // Child class implements type checking if needed.
1024 virtual void
1025 do_check_types(Gogo*)
1026 { }
1027
1028 // Child class implements copying.
1029 virtual Expression*
1030 do_copy() = 0;
1031
1032 // Child class implements whether the expression is addressable.
1033 virtual bool
1034 do_is_addressable() const
1035 { return false; }
1036
1037 // Child class implements taking the address of an expression.
1038 virtual void
1039 do_address_taken(bool)
1040 { }
1041
1042 // Child class implements issuing a nil check if the address is taken.
1043 virtual void
1044 do_issue_nil_check()
1045 { }
1046
1047 // Child class implements whether this expression must be evaluated
1048 // in order.
1049 virtual bool
1050 do_must_eval_in_order() const
1051 { return false; }
1052
1053 // Child class implements whether this expressions requires that
1054 // subexpressions be evaluated in order. The child implementation
1055 // may set *SKIP if it should be non-zero.
1056 virtual bool
1057 do_must_eval_subexpressions_in_order(int* /* skip */) const
1058 { return false; }
1059
1060 // Child class implements conversion to backend representation.
1061 virtual Bexpression*
1062 do_get_backend(Translate_context*) = 0;
1063
1064 // Child class implements export.
1065 virtual void
1066 do_export(Export*) const;
1067
1068 // For children to call to give an error for an unused value.
1069 void
1070 unused_value_error();
1071
1072 // For children to call when they detect that they are in error.
1073 void
1074 set_is_error();
1075
1076 // For children to call to report an error conveniently.
1077 void
1078 report_error(const char*);
1079
1080 // Child class implements dumping to a dump context.
1081 virtual void
1082 do_dump_expression(Ast_dump_context*) const = 0;
1083
1084 // Varargs lowering creates a slice object (unnamed compiler temp)
1085 // to contain the variable length collection of values. The enum
1086 // below tells the lowering routine whether it can mark that temp
1087 // as non-escaping or not. For general varargs calls it is not always
1088 // safe to stack-allocated the storage, but for specific cases (ex:
1089 // call to append()) it is legal.
1090 enum Slice_storage_escape_disp
1091 {
1092 SLICE_STORAGE_MAY_ESCAPE,
1093 SLICE_STORAGE_DOES_NOT_ESCAPE
1094 };
1095
1096 private:
1097 // Convert to the desired statement classification, or return NULL.
1098 // This is a controlled dynamic cast.
1099 template<typename Expression_class,
1100 Expression_classification expr_classification>
1101 Expression_class*
1102 convert()
1103 {
1104 return (this->classification_ == expr_classification
1105 ? static_cast<Expression_class*>(this)
1106 : NULL);
1107 }
1108
1109 template<typename Expression_class,
1110 Expression_classification expr_classification>
1111 const Expression_class*
1112 convert() const
1113 {
1114 return (this->classification_ == expr_classification
1115 ? static_cast<const Expression_class*>(this)
1116 : NULL);
1117 }
1118
1119 static Expression*
1120 convert_type_to_interface(Type*, Expression*, Location);
1121
1122 static Expression*
1123 get_interface_type_descriptor(Expression*);
1124
1125 static Expression*
1126 convert_interface_to_type(Type*, Expression*, Location);
1127
1128 // The expression classification.
1129 Expression_classification classification_;
1130 // The location in the input file.
1131 Location location_;
1132 };
1133
1134 // A list of Expressions.
1135
1136 class Expression_list
1137 {
1138 public:
1139 Expression_list()
1140 : entries_()
1141 { }
1142
1143 // Return whether the list is empty.
1144 bool
1145 empty() const
1146 { return this->entries_.empty(); }
1147
1148 // Return the number of entries in the list.
1149 size_t
1150 size() const
1151 { return this->entries_.size(); }
1152
1153 // Add an entry to the end of the list.
1154 void
1155 push_back(Expression* expr)
1156 { this->entries_.push_back(expr); }
1157
1158 void
1159 append(Expression_list* add)
1160 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
1161
1162 // Reserve space in the list.
1163 void
1164 reserve(size_t size)
1165 { this->entries_.reserve(size); }
1166
1167 // Traverse the expressions in the list.
1168 int
1169 traverse(Traverse*);
1170
1171 // Copy the list.
1172 Expression_list*
1173 copy();
1174
1175 // Return true if the list contains an error expression.
1176 bool
1177 contains_error() const;
1178
1179 // Retrieve an element by index.
1180 Expression*&
1181 at(size_t i)
1182 { return this->entries_.at(i); }
1183
1184 // Return the first and last elements.
1185 Expression*&
1186 front()
1187 { return this->entries_.front(); }
1188
1189 Expression*
1190 front() const
1191 { return this->entries_.front(); }
1192
1193 Expression*&
1194 back()
1195 { return this->entries_.back(); }
1196
1197 Expression*
1198 back() const
1199 { return this->entries_.back(); }
1200
1201 // Iterators.
1202
1203 typedef std::vector<Expression*>::iterator iterator;
1204 typedef std::vector<Expression*>::const_iterator const_iterator;
1205
1206 iterator
1207 begin()
1208 { return this->entries_.begin(); }
1209
1210 const_iterator
1211 begin() const
1212 { return this->entries_.begin(); }
1213
1214 iterator
1215 end()
1216 { return this->entries_.end(); }
1217
1218 const_iterator
1219 end() const
1220 { return this->entries_.end(); }
1221
1222 // Erase an entry.
1223 void
1224 erase(iterator p)
1225 { this->entries_.erase(p); }
1226
1227 private:
1228 std::vector<Expression*> entries_;
1229 };
1230
1231 // An abstract base class for an expression which is only used by the
1232 // parser, and is lowered in the lowering pass.
1233
1234 class Parser_expression : public Expression
1235 {
1236 public:
1237 Parser_expression(Expression_classification classification,
1238 Location location)
1239 : Expression(classification, location)
1240 { }
1241
1242 protected:
1243 virtual Expression*
1244 do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
1245
1246 Type*
1247 do_type();
1248
1249 void
1250 do_determine_type(const Type_context*)
1251 { go_unreachable(); }
1252
1253 void
1254 do_check_types(Gogo*)
1255 { go_unreachable(); }
1256
1257 Bexpression*
1258 do_get_backend(Translate_context*)
1259 { go_unreachable(); }
1260 };
1261
1262 // An expression which is simply a variable.
1263
1264 class Var_expression : public Expression
1265 {
1266 public:
1267 Var_expression(Named_object* variable, Location location)
1268 : Expression(EXPRESSION_VAR_REFERENCE, location),
1269 variable_(variable)
1270 { }
1271
1272 // Return the variable.
1273 Named_object*
1274 named_object() const
1275 { return this->variable_; }
1276
1277 protected:
1278 Expression*
1279 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1280
1281 Type*
1282 do_type();
1283
1284 void
1285 do_determine_type(const Type_context*);
1286
1287 Expression*
1288 do_copy()
1289 { return this; }
1290
1291 bool
1292 do_is_addressable() const
1293 { return true; }
1294
1295 void
1296 do_address_taken(bool);
1297
1298 Bexpression*
1299 do_get_backend(Translate_context*);
1300
1301 void
1302 do_dump_expression(Ast_dump_context*) const;
1303
1304 private:
1305 // The variable we are referencing.
1306 Named_object* variable_;
1307 };
1308
1309 // A reference to a variable within an enclosing function.
1310
1311 class Enclosed_var_expression : public Expression
1312 {
1313 public:
1314 Enclosed_var_expression(Expression* reference, Named_object* variable,
1315 Location location)
1316 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE, location),
1317 reference_(reference), variable_(variable)
1318 { }
1319
1320 // The reference to the enclosed variable. This will be an indirection of the
1321 // the field stored within closure variable.
1322 Expression*
1323 reference() const
1324 { return this->reference_; }
1325
1326 // The variable being enclosed and referenced.
1327 Named_object*
1328 variable() const
1329 { return this->variable_; }
1330
1331 protected:
1332 int
1333 do_traverse(Traverse*);
1334
1335 Expression*
1336 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1337
1338 Expression*
1339 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1340
1341 Type*
1342 do_type()
1343 { return this->reference_->type(); }
1344
1345 void
1346 do_determine_type(const Type_context* context)
1347 { return this->reference_->determine_type(context); }
1348
1349 Expression*
1350 do_copy()
1351 { return this; }
1352
1353 bool
1354 do_is_addressable() const
1355 { return this->reference_->is_addressable(); }
1356
1357 void
1358 do_address_taken(bool escapes);
1359
1360 Bexpression*
1361 do_get_backend(Translate_context* context)
1362 { return this->reference_->get_backend(context); }
1363
1364 void
1365 do_dump_expression(Ast_dump_context*) const;
1366
1367 private:
1368 // The reference to the enclosed variable.
1369 Expression* reference_;
1370 // The variable being enclosed.
1371 Named_object* variable_;
1372 };
1373
1374 // A reference to a temporary variable.
1375
1376 class Temporary_reference_expression : public Expression
1377 {
1378 public:
1379 Temporary_reference_expression(Temporary_statement* statement,
1380 Location location)
1381 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1382 statement_(statement), is_lvalue_(false)
1383 { }
1384
1385 // The temporary that this expression refers to.
1386 Temporary_statement*
1387 statement() const
1388 { return this->statement_; }
1389
1390 // Indicate that this reference appears on the left hand side of an
1391 // assignment statement.
1392 void
1393 set_is_lvalue()
1394 { this->is_lvalue_ = true; }
1395
1396 protected:
1397 Type*
1398 do_type();
1399
1400 void
1401 do_determine_type(const Type_context*)
1402 { }
1403
1404 Expression*
1405 do_copy()
1406 { return make_temporary_reference(this->statement_, this->location()); }
1407
1408 bool
1409 do_is_addressable() const
1410 { return true; }
1411
1412 void
1413 do_address_taken(bool);
1414
1415 Bexpression*
1416 do_get_backend(Translate_context*);
1417
1418 void
1419 do_dump_expression(Ast_dump_context*) const;
1420
1421 private:
1422 // The statement where the temporary variable is defined.
1423 Temporary_statement* statement_;
1424 // Whether this reference appears on the left hand side of an
1425 // assignment statement.
1426 bool is_lvalue_;
1427 };
1428
1429 // Set and use a temporary variable.
1430
1431 class Set_and_use_temporary_expression : public Expression
1432 {
1433 public:
1434 Set_and_use_temporary_expression(Temporary_statement* statement,
1435 Expression* expr, Location location)
1436 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1437 statement_(statement), expr_(expr)
1438 { }
1439
1440 // Return the temporary.
1441 Temporary_statement*
1442 temporary() const
1443 { return this->statement_; }
1444
1445 // Return the expression.
1446 Expression*
1447 expression() const
1448 { return this->expr_; }
1449
1450 protected:
1451 int
1452 do_traverse(Traverse* traverse)
1453 { return Expression::traverse(&this->expr_, traverse); }
1454
1455 Type*
1456 do_type();
1457
1458 void
1459 do_determine_type(const Type_context*);
1460
1461 Expression*
1462 do_copy()
1463 {
1464 return make_set_and_use_temporary(this->statement_, this->expr_,
1465 this->location());
1466 }
1467
1468 bool
1469 do_is_addressable() const
1470 { return true; }
1471
1472 void
1473 do_address_taken(bool);
1474
1475 Bexpression*
1476 do_get_backend(Translate_context*);
1477
1478 void
1479 do_dump_expression(Ast_dump_context*) const;
1480
1481 private:
1482 // The statement where the temporary variable is defined.
1483 Temporary_statement* statement_;
1484 // The expression to assign to the temporary.
1485 Expression* expr_;
1486 };
1487
1488 // A string expression.
1489
1490 class String_expression : public Expression
1491 {
1492 public:
1493 String_expression(const std::string& val, Location location)
1494 : Expression(EXPRESSION_STRING, location),
1495 val_(val), type_(NULL)
1496 { }
1497
1498 const std::string&
1499 val() const
1500 { return this->val_; }
1501
1502 static Expression*
1503 do_import(Import*);
1504
1505 protected:
1506 bool
1507 do_is_constant() const
1508 { return true; }
1509
1510 bool
1511 do_is_immutable() const
1512 { return true; }
1513
1514 bool
1515 do_string_constant_value(std::string* val) const
1516 {
1517 *val = this->val_;
1518 return true;
1519 }
1520
1521 Type*
1522 do_type();
1523
1524 void
1525 do_determine_type(const Type_context*);
1526
1527 Expression*
1528 do_copy()
1529 { return this; }
1530
1531 Bexpression*
1532 do_get_backend(Translate_context*);
1533
1534 // Write string literal to a string dump.
1535 static void
1536 export_string(String_dump* exp, const String_expression* str);
1537
1538 void
1539 do_export(Export*) const;
1540
1541 void
1542 do_dump_expression(Ast_dump_context*) const;
1543
1544 private:
1545 // The string value. This is immutable.
1546 const std::string val_;
1547 // The type as determined by context.
1548 Type* type_;
1549 };
1550
1551 // A type conversion expression.
1552
1553 class Type_conversion_expression : public Expression
1554 {
1555 public:
1556 Type_conversion_expression(Type* type, Expression* expr,
1557 Location location)
1558 : Expression(EXPRESSION_CONVERSION, location),
1559 type_(type), expr_(expr), may_convert_function_types_(false)
1560 { }
1561
1562 // Return the type to which we are converting.
1563 Type*
1564 type() const
1565 { return this->type_; }
1566
1567 // Return the expression which we are converting.
1568 Expression*
1569 expr() const
1570 { return this->expr_; }
1571
1572 // Permit converting from one function type to another. This is
1573 // used internally for method expressions.
1574 void
1575 set_may_convert_function_types()
1576 {
1577 this->may_convert_function_types_ = true;
1578 }
1579
1580 // Import a type conversion expression.
1581 static Expression*
1582 do_import(Import*);
1583
1584 protected:
1585 int
1586 do_traverse(Traverse* traverse);
1587
1588 Expression*
1589 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1590
1591 Expression*
1592 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1593
1594 bool
1595 do_is_constant() const;
1596
1597 bool
1598 do_is_immutable() const;
1599
1600 bool
1601 do_numeric_constant_value(Numeric_constant*) const;
1602
1603 bool
1604 do_string_constant_value(std::string*) const;
1605
1606 Type*
1607 do_type()
1608 { return this->type_; }
1609
1610 void
1611 do_determine_type(const Type_context*);
1612
1613 void
1614 do_check_types(Gogo*);
1615
1616 Expression*
1617 do_copy()
1618 {
1619 return new Type_conversion_expression(this->type_, this->expr_->copy(),
1620 this->location());
1621 }
1622
1623 Bexpression*
1624 do_get_backend(Translate_context* context);
1625
1626 void
1627 do_export(Export*) const;
1628
1629 void
1630 do_dump_expression(Ast_dump_context*) const;
1631
1632 private:
1633 // The type to convert to.
1634 Type* type_;
1635 // The expression to convert.
1636 Expression* expr_;
1637 // True if this is permitted to convert function types. This is
1638 // used internally for method expressions.
1639 bool may_convert_function_types_;
1640 };
1641
1642 // An unsafe type conversion, used to pass values to builtin functions.
1643
1644 class Unsafe_type_conversion_expression : public Expression
1645 {
1646 public:
1647 Unsafe_type_conversion_expression(Type* type, Expression* expr,
1648 Location location)
1649 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
1650 type_(type), expr_(expr)
1651 { }
1652
1653 Expression*
1654 expr() const
1655 { return this->expr_; }
1656
1657 protected:
1658 int
1659 do_traverse(Traverse* traverse);
1660
1661 bool
1662 do_is_immutable() const;
1663
1664 Type*
1665 do_type()
1666 { return this->type_; }
1667
1668 void
1669 do_determine_type(const Type_context*)
1670 { this->expr_->determine_type_no_context(); }
1671
1672 Expression*
1673 do_copy()
1674 {
1675 return new Unsafe_type_conversion_expression(this->type_,
1676 this->expr_->copy(),
1677 this->location());
1678 }
1679
1680 Bexpression*
1681 do_get_backend(Translate_context*);
1682
1683 void
1684 do_dump_expression(Ast_dump_context*) const;
1685
1686 private:
1687 // The type to convert to.
1688 Type* type_;
1689 // The expression to convert.
1690 Expression* expr_;
1691 };
1692
1693 // A Unary expression.
1694
1695 class Unary_expression : public Expression
1696 {
1697 public:
1698 Unary_expression(Operator op, Expression* expr, Location location)
1699 : Expression(EXPRESSION_UNARY, location),
1700 op_(op), escapes_(true), create_temp_(false), is_gc_root_(false),
1701 is_slice_init_(false), expr_(expr), issue_nil_check_(false)
1702 { }
1703
1704 // Return the operator.
1705 Operator
1706 op() const
1707 { return this->op_; }
1708
1709 // Return the operand.
1710 Expression*
1711 operand() const
1712 { return this->expr_; }
1713
1714 // Record that an address expression does not escape.
1715 void
1716 set_does_not_escape()
1717 {
1718 go_assert(this->op_ == OPERATOR_AND);
1719 this->escapes_ = false;
1720 }
1721
1722 // Record that this is an address expression which should create a
1723 // temporary variable if necessary. This is used for method calls.
1724 void
1725 set_create_temp()
1726 {
1727 go_assert(this->op_ == OPERATOR_AND);
1728 this->create_temp_ = true;
1729 }
1730
1731 // Record that this is an address expression of a GC root, which is a
1732 // mutable composite literal. This used for registering GC variables.
1733 void
1734 set_is_gc_root()
1735 {
1736 go_assert(this->op_ == OPERATOR_AND);
1737 this->is_gc_root_ = true;
1738 }
1739
1740 // Record that this is an address expression of a slice value initializer,
1741 // which is mutable if the values are not copied to the heap.
1742 void
1743 set_is_slice_init()
1744 {
1745 go_assert(this->op_ == OPERATOR_AND);
1746 this->is_slice_init_ = true;
1747 }
1748
1749 // Apply unary opcode OP to UNC, setting NC. Return true if this
1750 // could be done, false if not. Issue errors for overflow.
1751 static bool
1752 eval_constant(Operator op, const Numeric_constant* unc,
1753 Location, Numeric_constant* nc);
1754
1755 static Expression*
1756 do_import(Import*);
1757
1758 protected:
1759 int
1760 do_traverse(Traverse* traverse)
1761 { return Expression::traverse(&this->expr_, traverse); }
1762
1763 Expression*
1764 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1765
1766 Expression*
1767 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1768
1769 bool
1770 do_is_constant() const;
1771
1772 bool
1773 do_is_immutable() const
1774 {
1775 return (this->expr_->is_immutable()
1776 || (this->op_ == OPERATOR_AND && this->expr_->is_variable()));
1777 }
1778
1779 bool
1780 do_numeric_constant_value(Numeric_constant*) const;
1781
1782 Type*
1783 do_type();
1784
1785 void
1786 do_determine_type(const Type_context*);
1787
1788 void
1789 do_check_types(Gogo*);
1790
1791 Expression*
1792 do_copy()
1793 {
1794 return Expression::make_unary(this->op_, this->expr_->copy(),
1795 this->location());
1796 }
1797
1798 bool
1799 do_must_eval_subexpressions_in_order(int*) const
1800 { return this->op_ == OPERATOR_MULT; }
1801
1802 bool
1803 do_is_addressable() const
1804 { return this->op_ == OPERATOR_MULT; }
1805
1806 Bexpression*
1807 do_get_backend(Translate_context*);
1808
1809 void
1810 do_export(Export*) const;
1811
1812 void
1813 do_dump_expression(Ast_dump_context*) const;
1814
1815 void
1816 do_issue_nil_check()
1817 { this->issue_nil_check_ = (this->op_ == OPERATOR_MULT); }
1818
1819 private:
1820 // The unary operator to apply.
1821 Operator op_;
1822 // Normally true. False if this is an address expression which does
1823 // not escape the current function.
1824 bool escapes_;
1825 // True if this is an address expression which should create a
1826 // temporary variable if necessary.
1827 bool create_temp_;
1828 // True if this is an address expression for a GC root. A GC root is a
1829 // special struct composite literal that is mutable when addressed, meaning
1830 // it cannot be represented as an immutable_struct in the backend.
1831 bool is_gc_root_;
1832 // True if this is an address expression for a slice value with an immutable
1833 // initializer. The initializer for a slice's value pointer has an array
1834 // type, meaning it cannot be represented as an immutable_struct in the
1835 // backend.
1836 bool is_slice_init_;
1837 // The operand.
1838 Expression* expr_;
1839 // Whether or not to issue a nil check for this expression if its address
1840 // is being taken.
1841 bool issue_nil_check_;
1842 };
1843
1844 // A binary expression.
1845
1846 class Binary_expression : public Expression
1847 {
1848 public:
1849 Binary_expression(Operator op, Expression* left, Expression* right,
1850 Location location)
1851 : Expression(EXPRESSION_BINARY, location),
1852 op_(op), left_(left), right_(right), type_(NULL)
1853 { }
1854
1855 // Return the operator.
1856 Operator
1857 op()
1858 { return this->op_; }
1859
1860 // Return the left hand expression.
1861 Expression*
1862 left()
1863 { return this->left_; }
1864
1865 // Return the right hand expression.
1866 Expression*
1867 right()
1868 { return this->right_; }
1869
1870 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1871 // Return true if this could be done, false if not. Issue errors at
1872 // LOCATION as appropriate.
1873 static bool
1874 eval_constant(Operator op, Numeric_constant* left_nc,
1875 Numeric_constant* right_nc, Location location,
1876 Numeric_constant* nc);
1877
1878 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1879 // *RESULT. Return true if this could be done, false if not. Issue
1880 // errors at LOCATION as appropriate.
1881 static bool
1882 compare_constant(Operator op, Numeric_constant* left_nc,
1883 Numeric_constant* right_nc, Location location,
1884 bool* result);
1885
1886 static Expression*
1887 do_import(Import*);
1888
1889 // Report an error if OP can not be applied to TYPE. Return whether
1890 // it can. OTYPE is the type of the other operand.
1891 static bool
1892 check_operator_type(Operator op, Type* type, Type* otype, Location);
1893
1894 // Set *RESULT_TYPE to the resulting type when OP is applied to
1895 // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
1896 // success, false on failure.
1897 static bool
1898 operation_type(Operator op, Type* left_type, Type* right_type,
1899 Type** result_type);
1900
1901 protected:
1902 int
1903 do_traverse(Traverse* traverse);
1904
1905 Expression*
1906 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1907
1908 Expression*
1909 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1910
1911 bool
1912 do_is_constant() const
1913 { return this->left_->is_constant() && this->right_->is_constant(); }
1914
1915 bool
1916 do_is_immutable() const
1917 { return this->left_->is_immutable() && this->right_->is_immutable(); }
1918
1919 bool
1920 do_numeric_constant_value(Numeric_constant*) const;
1921
1922 bool
1923 do_discarding_value();
1924
1925 Type*
1926 do_type();
1927
1928 void
1929 do_determine_type(const Type_context*);
1930
1931 void
1932 do_check_types(Gogo*);
1933
1934 Expression*
1935 do_copy()
1936 {
1937 return Expression::make_binary(this->op_, this->left_->copy(),
1938 this->right_->copy(), this->location());
1939 }
1940
1941 Bexpression*
1942 do_get_backend(Translate_context*);
1943
1944 void
1945 do_export(Export*) const;
1946
1947 void
1948 do_dump_expression(Ast_dump_context*) const;
1949
1950 private:
1951 static bool
1952 cmp_to_bool(Operator op, int cmp);
1953
1954 static bool
1955 eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
1956 Location, Numeric_constant*);
1957
1958 static bool
1959 eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
1960 Location, Numeric_constant*);
1961
1962 static bool
1963 eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
1964 Location, Numeric_constant*);
1965
1966 static bool
1967 compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
1968
1969 static bool
1970 compare_float(const Numeric_constant*, const Numeric_constant *, int*);
1971
1972 static bool
1973 compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
1974
1975 Expression*
1976 lower_struct_comparison(Gogo*, Statement_inserter*);
1977
1978 Expression*
1979 lower_array_comparison(Gogo*, Statement_inserter*);
1980
1981 Expression*
1982 lower_interface_value_comparison(Gogo*, Statement_inserter*);
1983
1984 Expression*
1985 lower_compare_to_memcmp(Gogo*, Statement_inserter*);
1986
1987 Expression*
1988 operand_address(Statement_inserter*, Expression*);
1989
1990 // The binary operator to apply.
1991 Operator op_;
1992 // The left hand side operand.
1993 Expression* left_;
1994 // The right hand side operand.
1995 Expression* right_;
1996 // The type of a comparison operation.
1997 Type* type_;
1998 };
1999
2000 // A string concatenation expression. This is a sequence of strings
2001 // added together. It is created when lowering Binary_expression.
2002
2003 class String_concat_expression : public Expression
2004 {
2005 public:
2006 String_concat_expression(Expression_list* exprs)
2007 : Expression(EXPRESSION_STRING_CONCAT, exprs->front()->location()),
2008 exprs_(exprs)
2009 { }
2010
2011 // Return the list of string expressions to be concatenated.
2012 Expression_list*
2013 exprs()
2014 { return this->exprs_; }
2015
2016 protected:
2017 int
2018 do_traverse(Traverse* traverse)
2019 { return this->exprs_->traverse(traverse); }
2020
2021 Expression*
2022 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2023 { return this; }
2024
2025 Expression*
2026 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2027
2028 bool
2029 do_is_constant() const;
2030
2031 bool
2032 do_is_immutable() const;
2033
2034 Type*
2035 do_type();
2036
2037 void
2038 do_determine_type(const Type_context*);
2039
2040 void
2041 do_check_types(Gogo*);
2042
2043 Expression*
2044 do_copy()
2045 { return Expression::make_string_concat(this->exprs_->copy()); }
2046
2047 Bexpression*
2048 do_get_backend(Translate_context*)
2049 { go_unreachable(); }
2050
2051 void
2052 do_export(Export*) const
2053 { go_unreachable(); }
2054
2055 void
2056 do_dump_expression(Ast_dump_context*) const;
2057
2058 private:
2059 // The string expressions to concatenate.
2060 Expression_list* exprs_;
2061 };
2062
2063 // A call expression. The go statement needs to dig inside this.
2064
2065 class Call_expression : public Expression
2066 {
2067 public:
2068 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
2069 Location location)
2070 : Expression(EXPRESSION_CALL, location),
2071 fn_(fn), args_(args), type_(NULL), results_(NULL), call_(NULL),
2072 call_temp_(NULL), expected_result_count_(0), is_varargs_(is_varargs),
2073 varargs_are_lowered_(false), types_are_determined_(false),
2074 is_deferred_(false), is_concurrent_(false), issued_error_(false),
2075 is_multi_value_arg_(false), is_flattened_(false)
2076 { }
2077
2078 // The function to call.
2079 Expression*
2080 fn() const
2081 { return this->fn_; }
2082
2083 // The arguments.
2084 Expression_list*
2085 args()
2086 { return this->args_; }
2087
2088 const Expression_list*
2089 args() const
2090 { return this->args_; }
2091
2092 // Get the function type.
2093 Function_type*
2094 get_function_type() const;
2095
2096 // Return the number of values this call will return.
2097 size_t
2098 result_count() const;
2099
2100 // Return the temporary variable which holds result I. This is only
2101 // valid after the expression has been lowered, and is only valid
2102 // for calls which return multiple results.
2103 Temporary_statement*
2104 result(size_t i) const;
2105
2106 // Set the number of results expected from this call. This is used
2107 // when the call appears in a context that expects multiple results,
2108 // such as a, b = f().
2109 void
2110 set_expected_result_count(size_t);
2111
2112 // Return whether this is a call to the predeclared function
2113 // recover.
2114 bool
2115 is_recover_call() const;
2116
2117 // Set the argument for a call to recover.
2118 void
2119 set_recover_arg(Expression*);
2120
2121 // Whether the last argument is a varargs argument (f(a...)).
2122 bool
2123 is_varargs() const
2124 { return this->is_varargs_; }
2125
2126 // Return whether varargs have already been lowered.
2127 bool
2128 varargs_are_lowered() const
2129 { return this->varargs_are_lowered_; }
2130
2131 // Note that varargs have already been lowered.
2132 void
2133 set_varargs_are_lowered()
2134 { this->varargs_are_lowered_ = true; }
2135
2136 // Whether this call is being deferred.
2137 bool
2138 is_deferred() const
2139 { return this->is_deferred_; }
2140
2141 // Note that the call is being deferred.
2142 void
2143 set_is_deferred()
2144 { this->is_deferred_ = true; }
2145
2146 // Whether this call is concurrently executed.
2147 bool
2148 is_concurrent() const
2149 { return this->is_concurrent_; }
2150
2151 // Note that the call is concurrently executed.
2152 void
2153 set_is_concurrent()
2154 { this->is_concurrent_ = true; }
2155
2156 // We have found an error with this call expression; return true if
2157 // we should report it.
2158 bool
2159 issue_error();
2160
2161 // Whether or not this call contains errors, either in the call or the
2162 // arguments to the call.
2163 bool
2164 is_erroneous_call();
2165
2166 // Whether this call returns multiple results that are used as an
2167 // multi-valued argument.
2168 bool
2169 is_multi_value_arg() const
2170 { return this->is_multi_value_arg_; }
2171
2172 // Note this call is used as a multi-valued argument.
2173 void
2174 set_is_multi_value_arg()
2175 { this->is_multi_value_arg_ = true; }
2176
2177 protected:
2178 int
2179 do_traverse(Traverse*);
2180
2181 virtual Expression*
2182 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2183
2184 virtual Expression*
2185 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2186
2187 bool
2188 do_discarding_value()
2189 { return true; }
2190
2191 virtual Type*
2192 do_type();
2193
2194 virtual void
2195 do_determine_type(const Type_context*);
2196
2197 virtual void
2198 do_check_types(Gogo*);
2199
2200 Expression*
2201 do_copy();
2202
2203 bool
2204 do_must_eval_in_order() const;
2205
2206 virtual Bexpression*
2207 do_get_backend(Translate_context*);
2208
2209 virtual bool
2210 do_is_recover_call() const;
2211
2212 virtual void
2213 do_set_recover_arg(Expression*);
2214
2215 // Let a builtin expression change the argument list.
2216 void
2217 set_args(Expression_list* args)
2218 { this->args_ = args; }
2219
2220 // Let a builtin expression lower varargs.
2221 void
2222 lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
2223 Type* varargs_type, size_t param_count,
2224 Slice_storage_escape_disp escape_disp);
2225
2226 // Let a builtin expression check whether types have been
2227 // determined.
2228 bool
2229 determining_types();
2230
2231 void
2232 do_dump_expression(Ast_dump_context*) const;
2233
2234 private:
2235 bool
2236 check_argument_type(int, const Type*, const Type*, Location, bool);
2237
2238 Expression*
2239 lower_to_builtin(Named_object**, const char*, int);
2240
2241 Expression*
2242 interface_method_function(Interface_field_reference_expression*,
2243 Expression**);
2244
2245 Bexpression*
2246 set_results(Translate_context*, Bexpression*);
2247
2248 // The function to call.
2249 Expression* fn_;
2250 // The arguments to pass. This may be NULL if there are no
2251 // arguments.
2252 Expression_list* args_;
2253 // The type of the expression, to avoid recomputing it.
2254 Type* type_;
2255 // The list of temporaries which will hold the results if the
2256 // function returns a tuple.
2257 std::vector<Temporary_statement*>* results_;
2258 // The backend expression for the call, used for a call which returns a tuple.
2259 Bexpression* call_;
2260 // A temporary variable to store this call if the function returns a tuple.
2261 Temporary_statement* call_temp_;
2262 // If not 0, the number of results expected from this call, when
2263 // used in a context that expects multiple values.
2264 size_t expected_result_count_;
2265 // True if the last argument is a varargs argument (f(a...)).
2266 bool is_varargs_;
2267 // True if varargs have already been lowered.
2268 bool varargs_are_lowered_;
2269 // True if types have been determined.
2270 bool types_are_determined_;
2271 // True if the call is an argument to a defer statement.
2272 bool is_deferred_;
2273 // True if the call is an argument to a go statement.
2274 bool is_concurrent_;
2275 // True if we reported an error about a mismatch between call
2276 // results and uses. This is to avoid producing multiple errors
2277 // when there are multiple Call_result_expressions.
2278 bool issued_error_;
2279 // True if this call is used as an argument that returns multiple results.
2280 bool is_multi_value_arg_;
2281 // True if this expression has already been flattened.
2282 bool is_flattened_;
2283 };
2284
2285 // A single result from a call which returns multiple results.
2286
2287 class Call_result_expression : public Expression
2288 {
2289 public:
2290 Call_result_expression(Call_expression* call, unsigned int index)
2291 : Expression(EXPRESSION_CALL_RESULT, call->location()),
2292 call_(call), index_(index)
2293 { }
2294
2295 Expression*
2296 call() const
2297 { return this->call_; }
2298
2299 unsigned int
2300 index() const
2301 { return this->index_; }
2302
2303 protected:
2304 int
2305 do_traverse(Traverse*);
2306
2307 Type*
2308 do_type();
2309
2310 void
2311 do_determine_type(const Type_context*);
2312
2313 void
2314 do_check_types(Gogo*);
2315
2316 Expression*
2317 do_copy()
2318 {
2319 return new Call_result_expression(this->call_->call_expression(),
2320 this->index_);
2321 }
2322
2323 bool
2324 do_must_eval_in_order() const
2325 { return true; }
2326
2327 Bexpression*
2328 do_get_backend(Translate_context*);
2329
2330 void
2331 do_dump_expression(Ast_dump_context*) const;
2332
2333 private:
2334 // The underlying call expression.
2335 Expression* call_;
2336 // Which result we want.
2337 unsigned int index_;
2338 };
2339
2340 // An expression which represents a pointer to a function.
2341
2342 class Func_expression : public Expression
2343 {
2344 public:
2345 Func_expression(Named_object* function, Expression* closure,
2346 Location location)
2347 : Expression(EXPRESSION_FUNC_REFERENCE, location),
2348 function_(function), closure_(closure),
2349 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS)
2350 { }
2351
2352 // Return the object associated with the function.
2353 Named_object*
2354 named_object() const
2355 { return this->function_; }
2356
2357 // Return the closure for this function. This will return NULL if
2358 // the function has no closure, which is the normal case.
2359 Expression*
2360 closure()
2361 { return this->closure_; }
2362
2363 // Return whether this is a reference to a runtime function.
2364 bool
2365 is_runtime_function() const
2366 { return this->runtime_code_ != Runtime::NUMBER_OF_FUNCTIONS; }
2367
2368 // Return the runtime code for this function expression.
2369 // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
2370 // runtime function.
2371 Runtime::Function
2372 runtime_code() const
2373 { return this->runtime_code_; }
2374
2375 // Set the runtime code for this function expression.
2376 void
2377 set_runtime_code(Runtime::Function code)
2378 { this->runtime_code_ = code; }
2379
2380 // Return a backend expression for the code of a function.
2381 static Bexpression*
2382 get_code_pointer(Gogo*, Named_object* function, Location loc);
2383
2384 protected:
2385 int
2386 do_traverse(Traverse*);
2387
2388 Type*
2389 do_type();
2390
2391 void
2392 do_determine_type(const Type_context*)
2393 {
2394 if (this->closure_ != NULL)
2395 this->closure_->determine_type_no_context();
2396 }
2397
2398 Expression*
2399 do_copy()
2400 {
2401 return Expression::make_func_reference(this->function_,
2402 (this->closure_ == NULL
2403 ? NULL
2404 : this->closure_->copy()),
2405 this->location());
2406 }
2407
2408 Bexpression*
2409 do_get_backend(Translate_context*);
2410
2411 void
2412 do_dump_expression(Ast_dump_context*) const;
2413
2414 private:
2415 // The function itself.
2416 Named_object* function_;
2417 // A closure. This is normally NULL. For a nested function, it may
2418 // be a struct holding pointers to all the variables referenced by
2419 // this function and defined in enclosing functions.
2420 Expression* closure_;
2421 // The runtime code for the referenced function.
2422 Runtime::Function runtime_code_;
2423 };
2424
2425 // A function descriptor. A function descriptor is a struct with a
2426 // single field pointing to the function code. This is used for
2427 // functions without closures.
2428
2429 class Func_descriptor_expression : public Expression
2430 {
2431 public:
2432 Func_descriptor_expression(Named_object* fn);
2433
2434 // Make the function descriptor type, so that it can be converted.
2435 static void
2436 make_func_descriptor_type();
2437
2438 protected:
2439 int
2440 do_traverse(Traverse*);
2441
2442 Type*
2443 do_type();
2444
2445 void
2446 do_determine_type(const Type_context*)
2447 { }
2448
2449 Expression*
2450 do_copy()
2451 { return Expression::make_func_descriptor(this->fn_); }
2452
2453 bool
2454 do_is_addressable() const
2455 { return true; }
2456
2457 Bexpression*
2458 do_get_backend(Translate_context*);
2459
2460 void
2461 do_dump_expression(Ast_dump_context* context) const;
2462
2463 private:
2464 // The type of all function descriptors.
2465 static Type* descriptor_type;
2466
2467 // The function for which this is the descriptor.
2468 Named_object* fn_;
2469 // The descriptor variable.
2470 Bvariable* dvar_;
2471 };
2472
2473 // A reference to an unknown name.
2474
2475 class Unknown_expression : public Parser_expression
2476 {
2477 public:
2478 Unknown_expression(Named_object* named_object, Location location)
2479 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
2480 named_object_(named_object), no_error_message_(false),
2481 is_composite_literal_key_(false)
2482 { }
2483
2484 // The associated named object.
2485 Named_object*
2486 named_object() const
2487 { return this->named_object_; }
2488
2489 // The name of the identifier which was unknown.
2490 const std::string&
2491 name() const;
2492
2493 // Call this to indicate that we should not give an error if this
2494 // name is never defined. This is used to avoid knock-on errors
2495 // during an erroneous parse.
2496 void
2497 set_no_error_message()
2498 { this->no_error_message_ = true; }
2499
2500 // Note that this expression is being used as the key in a composite
2501 // literal, so it may be OK if it is not resolved.
2502 void
2503 set_is_composite_literal_key()
2504 { this->is_composite_literal_key_ = true; }
2505
2506 // Note that this expression should no longer be treated as a
2507 // composite literal key.
2508 void
2509 clear_is_composite_literal_key()
2510 { this->is_composite_literal_key_ = false; }
2511
2512 protected:
2513 Expression*
2514 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2515
2516 Expression*
2517 do_copy()
2518 { return new Unknown_expression(this->named_object_, this->location()); }
2519
2520 void
2521 do_dump_expression(Ast_dump_context*) const;
2522
2523 private:
2524 // The unknown name.
2525 Named_object* named_object_;
2526 // True if we should not give errors if this is undefined. This is
2527 // used if there was a parse failure.
2528 bool no_error_message_;
2529 // True if this is the key in a composite literal.
2530 bool is_composite_literal_key_;
2531 };
2532
2533 // An index expression. This is lowered to an array index, a string
2534 // index, or a map index.
2535
2536 class Index_expression : public Parser_expression
2537 {
2538 public:
2539 Index_expression(Expression* left, Expression* start, Expression* end,
2540 Expression* cap, Location location)
2541 : Parser_expression(EXPRESSION_INDEX, location),
2542 left_(left), start_(start), end_(end), cap_(cap)
2543 { }
2544
2545 // Dump an index expression, i.e. an expression of the form
2546 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
2547 static void
2548 dump_index_expression(Ast_dump_context*, const Expression* expr,
2549 const Expression* start, const Expression* end,
2550 const Expression* cap);
2551
2552 protected:
2553 int
2554 do_traverse(Traverse*);
2555
2556 Expression*
2557 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2558
2559 Expression*
2560 do_copy()
2561 {
2562 return new Index_expression(this->left_->copy(), this->start_->copy(),
2563 (this->end_ == NULL
2564 ? NULL
2565 : this->end_->copy()),
2566 (this->cap_ == NULL
2567 ? NULL
2568 : this->cap_->copy()),
2569 this->location());
2570 }
2571
2572 bool
2573 do_must_eval_subexpressions_in_order(int* skip) const
2574 {
2575 *skip = 1;
2576 return true;
2577 }
2578
2579 void
2580 do_dump_expression(Ast_dump_context*) const;
2581
2582 void
2583 do_issue_nil_check()
2584 { this->left_->issue_nil_check(); }
2585 private:
2586 // The expression being indexed.
2587 Expression* left_;
2588 // The first index.
2589 Expression* start_;
2590 // The second index. This is NULL for an index, non-NULL for a
2591 // slice.
2592 Expression* end_;
2593 // The capacity argument. This is NULL for indices and slices that use the
2594 // default capacity, non-NULL for indices and slices that specify the
2595 // capacity.
2596 Expression* cap_;
2597 };
2598
2599 // An array index. This is used for both indexing and slicing.
2600
2601 class Array_index_expression : public Expression
2602 {
2603 public:
2604 Array_index_expression(Expression* array, Expression* start,
2605 Expression* end, Expression* cap, Location location)
2606 : Expression(EXPRESSION_ARRAY_INDEX, location),
2607 array_(array), start_(start), end_(end), cap_(cap), type_(NULL)
2608 { }
2609
2610 // Return the array.
2611 Expression*
2612 array()
2613 { return this->array_; }
2614
2615 const Expression*
2616 array() const
2617 { return this->array_; }
2618
2619 protected:
2620 int
2621 do_traverse(Traverse*);
2622
2623 Expression*
2624 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2625
2626 Type*
2627 do_type();
2628
2629 void
2630 do_determine_type(const Type_context*);
2631
2632 void
2633 do_check_types(Gogo*);
2634
2635 Expression*
2636 do_copy()
2637 {
2638 return Expression::make_array_index(this->array_->copy(),
2639 this->start_->copy(),
2640 (this->end_ == NULL
2641 ? NULL
2642 : this->end_->copy()),
2643 (this->cap_ == NULL
2644 ? NULL
2645 : this->cap_->copy()),
2646 this->location());
2647 }
2648
2649 bool
2650 do_must_eval_subexpressions_in_order(int* skip) const
2651 {
2652 *skip = 1;
2653 return true;
2654 }
2655
2656 bool
2657 do_is_addressable() const;
2658
2659 void
2660 do_address_taken(bool escapes)
2661 { this->array_->address_taken(escapes); }
2662
2663 void
2664 do_issue_nil_check()
2665 { this->array_->issue_nil_check(); }
2666
2667 Bexpression*
2668 do_get_backend(Translate_context*);
2669
2670 void
2671 do_dump_expression(Ast_dump_context*) const;
2672
2673 private:
2674 // The array we are getting a value from.
2675 Expression* array_;
2676 // The start or only index.
2677 Expression* start_;
2678 // The end index of a slice. This may be NULL for a simple array
2679 // index, or it may be a nil expression for the length of the array.
2680 Expression* end_;
2681 // The capacity argument of a slice. This may be NULL for an array index or
2682 // slice.
2683 Expression* cap_;
2684 // The type of the expression.
2685 Type* type_;
2686 };
2687
2688 // A string index. This is used for both indexing and slicing.
2689
2690 class String_index_expression : public Expression
2691 {
2692 public:
2693 String_index_expression(Expression* string, Expression* start,
2694 Expression* end, Location location)
2695 : Expression(EXPRESSION_STRING_INDEX, location),
2696 string_(string), start_(start), end_(end)
2697 { }
2698
2699 // Return the string being indexed.
2700 Expression*
2701 string() const
2702 { return this->string_; }
2703
2704 protected:
2705 int
2706 do_traverse(Traverse*);
2707
2708 Expression*
2709 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2710
2711 Type*
2712 do_type();
2713
2714 void
2715 do_determine_type(const Type_context*);
2716
2717 void
2718 do_check_types(Gogo*);
2719
2720 Expression*
2721 do_copy()
2722 {
2723 return Expression::make_string_index(this->string_->copy(),
2724 this->start_->copy(),
2725 (this->end_ == NULL
2726 ? NULL
2727 : this->end_->copy()),
2728 this->location());
2729 }
2730
2731 bool
2732 do_must_eval_subexpressions_in_order(int* skip) const
2733 {
2734 *skip = 1;
2735 return true;
2736 }
2737
2738 Bexpression*
2739 do_get_backend(Translate_context*);
2740
2741 void
2742 do_dump_expression(Ast_dump_context*) const;
2743
2744 private:
2745 // The string we are getting a value from.
2746 Expression* string_;
2747 // The start or only index.
2748 Expression* start_;
2749 // The end index of a slice. This may be NULL for a single index,
2750 // or it may be a nil expression for the length of the string.
2751 Expression* end_;
2752 };
2753
2754 // An index into a map.
2755
2756 class Map_index_expression : public Expression
2757 {
2758 public:
2759 Map_index_expression(Expression* map, Expression* index,
2760 Location location)
2761 : Expression(EXPRESSION_MAP_INDEX, location),
2762 map_(map), index_(index), value_pointer_(NULL)
2763 { }
2764
2765 // Return the map.
2766 Expression*
2767 map()
2768 { return this->map_; }
2769
2770 const Expression*
2771 map() const
2772 { return this->map_; }
2773
2774 // Return the index.
2775 Expression*
2776 index()
2777 { return this->index_; }
2778
2779 const Expression*
2780 index() const
2781 { return this->index_; }
2782
2783 // Get the type of the map being indexed.
2784 Map_type*
2785 get_map_type() const;
2786
2787 // Return an expression for the map index. This returns an
2788 // expression that evaluates to a pointer to a value in the map. If
2789 // the key is not present in the map, this will return a pointer to
2790 // the zero value.
2791 Expression*
2792 get_value_pointer(Gogo*);
2793
2794 protected:
2795 int
2796 do_traverse(Traverse*);
2797
2798 Expression*
2799 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2800
2801 Type*
2802 do_type();
2803
2804 void
2805 do_determine_type(const Type_context*);
2806
2807 void
2808 do_check_types(Gogo*);
2809
2810 Expression*
2811 do_copy()
2812 {
2813 return Expression::make_map_index(this->map_->copy(),
2814 this->index_->copy(),
2815 this->location());
2816 }
2817
2818 bool
2819 do_must_eval_subexpressions_in_order(int* skip) const
2820 {
2821 *skip = 1;
2822 return true;
2823 }
2824
2825 // A map index expression is an lvalue but it is not addressable.
2826
2827 Bexpression*
2828 do_get_backend(Translate_context*);
2829
2830 void
2831 do_dump_expression(Ast_dump_context*) const;
2832
2833 private:
2834 // The map we are looking into.
2835 Expression* map_;
2836 // The index.
2837 Expression* index_;
2838 // A pointer to the value at this index.
2839 Expression* value_pointer_;
2840 };
2841
2842 // An expression which represents a method bound to its first
2843 // argument.
2844
2845 class Bound_method_expression : public Expression
2846 {
2847 public:
2848 Bound_method_expression(Expression* expr, const Method *method,
2849 Named_object* function, Location location)
2850 : Expression(EXPRESSION_BOUND_METHOD, location),
2851 expr_(expr), expr_type_(NULL), method_(method), function_(function)
2852 { }
2853
2854 // Return the object which is the first argument.
2855 Expression*
2856 first_argument()
2857 { return this->expr_; }
2858
2859 // Return the implicit type of the first argument. This will be
2860 // non-NULL when using a method from an anonymous field without
2861 // using an explicit stub.
2862 Type*
2863 first_argument_type() const
2864 { return this->expr_type_; }
2865
2866 // Return the method.
2867 const Method*
2868 method() const
2869 { return this->method_; }
2870
2871 // Return the function to call.
2872 Named_object*
2873 function() const
2874 { return this->function_; }
2875
2876 // Set the implicit type of the expression.
2877 void
2878 set_first_argument_type(Type* type)
2879 { this->expr_type_ = type; }
2880
2881 // Create a thunk to call FUNCTION, for METHOD, when it is used as
2882 // part of a method value.
2883 static Named_object*
2884 create_thunk(Gogo*, const Method* method, Named_object* function);
2885
2886 protected:
2887 int
2888 do_traverse(Traverse*);
2889
2890 Expression*
2891 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2892
2893 Type*
2894 do_type();
2895
2896 void
2897 do_determine_type(const Type_context*);
2898
2899 void
2900 do_check_types(Gogo*);
2901
2902 Expression*
2903 do_copy()
2904 {
2905 return new Bound_method_expression(this->expr_->copy(), this->method_,
2906 this->function_, this->location());
2907 }
2908
2909 Bexpression*
2910 do_get_backend(Translate_context*)
2911 { go_unreachable(); }
2912
2913 void
2914 do_dump_expression(Ast_dump_context*) const;
2915
2916 private:
2917 // A mapping from method functions to the thunks we have created for
2918 // them.
2919 typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
2920 static Method_value_thunks method_value_thunks;
2921
2922 // The object used to find the method. This is passed to the method
2923 // as the first argument.
2924 Expression* expr_;
2925 // The implicit type of the object to pass to the method. This is
2926 // NULL in the normal case, non-NULL when using a method from an
2927 // anonymous field which does not require a stub.
2928 Type* expr_type_;
2929 // The method.
2930 const Method* method_;
2931 // The function to call. This is not the same as
2932 // method_->named_object() when the method has a stub. This will be
2933 // the real function rather than the stub.
2934 Named_object* function_;
2935 };
2936
2937 // A reference to a field in a struct.
2938
2939 class Field_reference_expression : public Expression
2940 {
2941 public:
2942 Field_reference_expression(Expression* expr, unsigned int field_index,
2943 Location location)
2944 : Expression(EXPRESSION_FIELD_REFERENCE, location),
2945 expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
2946 { }
2947
2948 // Return the struct expression.
2949 Expression*
2950 expr() const
2951 { return this->expr_; }
2952
2953 // Return the field index.
2954 unsigned int
2955 field_index() const
2956 { return this->field_index_; }
2957
2958 // Return whether this node was implied by an anonymous field.
2959 bool
2960 implicit() const
2961 { return this->implicit_; }
2962
2963 void
2964 set_implicit(bool implicit)
2965 { this->implicit_ = implicit; }
2966
2967 // Set the struct expression. This is used when parsing.
2968 void
2969 set_struct_expression(Expression* expr)
2970 {
2971 go_assert(this->expr_ == NULL);
2972 this->expr_ = expr;
2973 }
2974
2975 protected:
2976 int
2977 do_traverse(Traverse* traverse)
2978 { return Expression::traverse(&this->expr_, traverse); }
2979
2980 Expression*
2981 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2982
2983 Type*
2984 do_type();
2985
2986 void
2987 do_determine_type(const Type_context*)
2988 { this->expr_->determine_type_no_context(); }
2989
2990 void
2991 do_check_types(Gogo*);
2992
2993 Expression*
2994 do_copy()
2995 {
2996 return Expression::make_field_reference(this->expr_->copy(),
2997 this->field_index_,
2998 this->location());
2999 }
3000
3001 bool
3002 do_is_addressable() const
3003 { return this->expr_->is_addressable(); }
3004
3005 void
3006 do_address_taken(bool escapes)
3007 { this->expr_->address_taken(escapes); }
3008
3009 void
3010 do_issue_nil_check()
3011 { this->expr_->issue_nil_check(); }
3012
3013 Bexpression*
3014 do_get_backend(Translate_context*);
3015
3016 void
3017 do_dump_expression(Ast_dump_context*) const;
3018
3019 private:
3020 // The expression we are looking into. This should have a type of
3021 // struct.
3022 Expression* expr_;
3023 // The zero-based index of the field we are retrieving.
3024 unsigned int field_index_;
3025 // Whether this node was emitted implicitly for an embedded field,
3026 // that is, expr_ is not the expr_ of the original user node.
3027 bool implicit_;
3028 // Whether we have already emitted a fieldtrack call.
3029 bool called_fieldtrack_;
3030 };
3031
3032 // A reference to a field of an interface.
3033
3034 class Interface_field_reference_expression : public Expression
3035 {
3036 public:
3037 Interface_field_reference_expression(Expression* expr,
3038 const std::string& name,
3039 Location location)
3040 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
3041 expr_(expr), name_(name)
3042 { }
3043
3044 // Return the expression for the interface object.
3045 Expression*
3046 expr()
3047 { return this->expr_; }
3048
3049 // Return the name of the method to call.
3050 const std::string&
3051 name() const
3052 { return this->name_; }
3053
3054 // Create a thunk to call the method NAME in TYPE when it is used as
3055 // part of a method value.
3056 static Named_object*
3057 create_thunk(Gogo*, Interface_type* type, const std::string& name);
3058
3059 // Return an expression for the pointer to the function to call.
3060 Expression*
3061 get_function();
3062
3063 // Return an expression for the first argument to pass to the interface
3064 // function. This is the real object associated with the interface object.
3065 Expression*
3066 get_underlying_object();
3067
3068 protected:
3069 int
3070 do_traverse(Traverse* traverse);
3071
3072 Expression*
3073 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3074
3075 Type*
3076 do_type();
3077
3078 void
3079 do_determine_type(const Type_context*);
3080
3081 void
3082 do_check_types(Gogo*);
3083
3084 Expression*
3085 do_copy()
3086 {
3087 return Expression::make_interface_field_reference(this->expr_->copy(),
3088 this->name_,
3089 this->location());
3090 }
3091
3092 Bexpression*
3093 do_get_backend(Translate_context*);
3094
3095 void
3096 do_dump_expression(Ast_dump_context*) const;
3097
3098 private:
3099 // A mapping from interface types to a list of thunks we have
3100 // created for methods.
3101 typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
3102 typedef Unordered_map(Interface_type*, Method_thunks*)
3103 Interface_method_thunks;
3104 static Interface_method_thunks interface_method_thunks;
3105
3106 // The expression for the interface object. This should have a type
3107 // of interface or pointer to interface.
3108 Expression* expr_;
3109 // The field we are retrieving--the name of the method.
3110 std::string name_;
3111 };
3112
3113 // Implement the builtin function new.
3114
3115 class Allocation_expression : public Expression
3116 {
3117 public:
3118 Allocation_expression(Type* type, Location location)
3119 : Expression(EXPRESSION_ALLOCATION, location),
3120 type_(type), allocate_on_stack_(false)
3121 { }
3122
3123 void
3124 set_allocate_on_stack()
3125 { this->allocate_on_stack_ = true; }
3126
3127 protected:
3128 int
3129 do_traverse(Traverse*);
3130
3131 Type*
3132 do_type();
3133
3134 void
3135 do_determine_type(const Type_context*)
3136 { }
3137
3138 Expression*
3139 do_copy();
3140
3141 Bexpression*
3142 do_get_backend(Translate_context*);
3143
3144 void
3145 do_dump_expression(Ast_dump_context*) const;
3146
3147 private:
3148 // The type we are allocating.
3149 Type* type_;
3150 // Whether or not this is a stack allocation.
3151 bool allocate_on_stack_;
3152 };
3153
3154 // A general composite literal. This is lowered to a type specific
3155 // version.
3156
3157 class Composite_literal_expression : public Parser_expression
3158 {
3159 public:
3160 Composite_literal_expression(Type* type, int depth, bool has_keys,
3161 Expression_list* vals, bool all_are_names,
3162 Location location)
3163 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
3164 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys),
3165 all_are_names_(all_are_names), key_path_(std::vector<bool>(depth))
3166 {}
3167
3168
3169 // Mark the DEPTH entry of KEY_PATH as containing a key.
3170 void
3171 update_key_path(size_t depth)
3172 {
3173 go_assert(depth < this->key_path_.size());
3174 this->key_path_[depth] = true;
3175 }
3176
3177 protected:
3178 int
3179 do_traverse(Traverse* traverse);
3180
3181 Expression*
3182 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3183
3184 Expression*
3185 do_copy()
3186 {
3187 Composite_literal_expression *ret =
3188 new Composite_literal_expression(this->type_, this->depth_,
3189 this->has_keys_,
3190 (this->vals_ == NULL
3191 ? NULL
3192 : this->vals_->copy()),
3193 this->all_are_names_,
3194 this->location());
3195 ret->key_path_ = this->key_path_;
3196 return ret;
3197 }
3198
3199 void
3200 do_dump_expression(Ast_dump_context*) const;
3201
3202 private:
3203 Expression*
3204 lower_struct(Gogo*, Type*);
3205
3206 Expression*
3207 lower_array(Type*);
3208
3209 Expression*
3210 make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
3211
3212 Expression*
3213 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
3214
3215 // The type of the composite literal.
3216 Type* type_;
3217 // The depth within a list of composite literals within a composite
3218 // literal, when the type is omitted.
3219 int depth_;
3220 // The values to put in the composite literal.
3221 Expression_list* vals_;
3222 // If this is true, then VALS_ is a list of pairs: a key and a
3223 // value. In an array initializer, a missing key will be NULL.
3224 bool has_keys_;
3225 // If this is true, then HAS_KEYS_ is true, and every key is a
3226 // simple identifier.
3227 bool all_are_names_;
3228 // A complement to DEPTH that indicates for each level starting from 0 to
3229 // DEPTH-1 whether or not this composite literal is nested inside of key or
3230 // a value. This is used to decide which type to use when given a map literal
3231 // with omitted key types.
3232 std::vector<bool> key_path_;
3233 };
3234
3235 // Construct a struct.
3236
3237 class Struct_construction_expression : public Expression
3238 {
3239 public:
3240 Struct_construction_expression(Type* type, Expression_list* vals,
3241 Location location)
3242 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
3243 type_(type), vals_(vals), traverse_order_(NULL)
3244 { }
3245
3246 // Set the traversal order, used to ensure that we implement the
3247 // order of evaluation rules. Takes ownership of the argument.
3248 void
3249 set_traverse_order(std::vector<int>* traverse_order)
3250 { this->traverse_order_ = traverse_order; }
3251
3252 // Return whether this is a constant initializer.
3253 bool
3254 is_constant_struct() const;
3255
3256 Expression_list*
3257 vals() const
3258 { return this->vals_; }
3259
3260 protected:
3261 int
3262 do_traverse(Traverse* traverse);
3263
3264 bool
3265 do_is_immutable() const;
3266
3267 Type*
3268 do_type()
3269 { return this->type_; }
3270
3271 void
3272 do_determine_type(const Type_context*);
3273
3274 void
3275 do_check_types(Gogo*);
3276
3277 Expression*
3278 do_copy()
3279 {
3280 Struct_construction_expression* ret =
3281 new Struct_construction_expression(this->type_,
3282 (this->vals_ == NULL
3283 ? NULL
3284 : this->vals_->copy()),
3285 this->location());
3286 if (this->traverse_order_ != NULL)
3287 ret->set_traverse_order(this->traverse_order_);
3288 return ret;
3289 }
3290
3291 Expression*
3292 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3293
3294 Bexpression*
3295 do_get_backend(Translate_context*);
3296
3297 void
3298 do_export(Export*) const;
3299
3300 void
3301 do_dump_expression(Ast_dump_context*) const;
3302
3303 private:
3304 // The type of the struct to construct.
3305 Type* type_;
3306 // The list of values, in order of the fields in the struct. A NULL
3307 // entry means that the field should be zero-initialized.
3308 Expression_list* vals_;
3309 // If not NULL, the order in which to traverse vals_. This is used
3310 // so that we implement the order of evaluation rules correctly.
3311 std::vector<int>* traverse_order_;
3312 };
3313
3314 // Construct an array. This class is not used directly; instead we
3315 // use the child classes, Fixed_array_construction_expression and
3316 // Slice_construction_expression.
3317
3318 class Array_construction_expression : public Expression
3319 {
3320 protected:
3321 Array_construction_expression(Expression_classification classification,
3322 Type* type,
3323 const std::vector<unsigned long>* indexes,
3324 Expression_list* vals, Location location)
3325 : Expression(classification, location),
3326 type_(type), indexes_(indexes), vals_(vals)
3327 { go_assert(indexes == NULL || indexes->size() == vals->size()); }
3328
3329 public:
3330 // Return whether this is a constant initializer.
3331 bool
3332 is_constant_array() const;
3333
3334 // Return the number of elements.
3335 size_t
3336 element_count() const
3337 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
3338
3339 // The list of values.
3340 Expression_list*
3341 vals() const
3342 { return this->vals_; }
3343
3344 protected:
3345 virtual int
3346 do_traverse(Traverse* traverse);
3347
3348 bool
3349 do_is_immutable() const;
3350
3351 Type*
3352 do_type()
3353 { return this->type_; }
3354
3355 void
3356 do_determine_type(const Type_context*);
3357
3358 void
3359 do_check_types(Gogo*);
3360
3361 void
3362 do_export(Export*) const;
3363
3364 // The indexes.
3365 const std::vector<unsigned long>*
3366 indexes()
3367 { return this->indexes_; }
3368
3369 Expression*
3370 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3371
3372 // Get the backend constructor for the array values.
3373 Bexpression*
3374 get_constructor(Translate_context* context, Btype* btype);
3375
3376 void
3377 do_dump_expression(Ast_dump_context*) const;
3378
3379 virtual void
3380 dump_slice_storage_expression(Ast_dump_context*) const { }
3381
3382 private:
3383 // The type of the array to construct.
3384 Type* type_;
3385 // The list of indexes into the array, one for each value. This may
3386 // be NULL, in which case the indexes start at zero and increment.
3387 const std::vector<unsigned long>* indexes_;
3388 // The list of values. This may be NULL if there are no values.
3389 Expression_list* vals_;
3390 };
3391
3392 // Construct a fixed array.
3393
3394 class Fixed_array_construction_expression :
3395 public Array_construction_expression
3396 {
3397 public:
3398 Fixed_array_construction_expression(Type* type,
3399 const std::vector<unsigned long>* indexes,
3400 Expression_list* vals, Location location);
3401
3402 protected:
3403 Expression*
3404 do_copy()
3405 {
3406 return new Fixed_array_construction_expression(this->type(),
3407 this->indexes(),
3408 (this->vals() == NULL
3409 ? NULL
3410 : this->vals()->copy()),
3411 this->location());
3412 }
3413
3414 Bexpression*
3415 do_get_backend(Translate_context*);
3416 };
3417
3418 // Construct a slice.
3419
3420 class Slice_construction_expression : public Array_construction_expression
3421 {
3422 public:
3423 Slice_construction_expression(Type* type,
3424 const std::vector<unsigned long>* indexes,
3425 Expression_list* vals, Location location);
3426
3427 Expression*
3428 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3429
3430 // Record that the storage for this slice (e.g. vals) cannot escape,
3431 // hence it can be stack-allocated.
3432 void
3433 set_storage_does_not_escape()
3434 {
3435 this->storage_escapes_ = false;
3436 }
3437
3438 protected:
3439 // Note that taking the address of a slice literal is invalid.
3440
3441 int
3442 do_traverse(Traverse* traverse);
3443
3444 Expression*
3445 do_copy()
3446 {
3447 return new Slice_construction_expression(this->type(), this->indexes(),
3448 (this->vals() == NULL
3449 ? NULL
3450 : this->vals()->copy()),
3451 this->location());
3452 }
3453
3454 Bexpression*
3455 do_get_backend(Translate_context*);
3456
3457 void
3458 dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const;
3459
3460 // Create an array value for the constructed slice. Invoked during
3461 // flattening if slice storage does not escape, otherwise invoked
3462 // later on during do_get_backend().
3463 Expression*
3464 create_array_val();
3465
3466 private:
3467 // The type of the values in this slice.
3468 Type* valtype_;
3469 // Array value expression, optionally filled in during flattening.
3470 Expression* array_val_;
3471 // Slice storage expression, optionally filled in during flattening.
3472 Expression* slice_storage_;
3473 // Normally true. Can be set to false if we know that the resulting
3474 // storage for the slice cannot escape.
3475 bool storage_escapes_;
3476 };
3477
3478 // Construct a map.
3479
3480 class Map_construction_expression : public Expression
3481 {
3482 public:
3483 Map_construction_expression(Type* type, Expression_list* vals,
3484 Location location)
3485 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
3486 type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL)
3487 { go_assert(vals == NULL || vals->size() % 2 == 0); }
3488
3489 Expression_list*
3490 vals() const
3491 { return this->vals_; }
3492
3493 protected:
3494 int
3495 do_traverse(Traverse* traverse);
3496
3497 Expression*
3498 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3499
3500 Type*
3501 do_type()
3502 { return this->type_; }
3503
3504 void
3505 do_determine_type(const Type_context*);
3506
3507 void
3508 do_check_types(Gogo*);
3509
3510 Expression*
3511 do_copy()
3512 {
3513 return new Map_construction_expression(this->type_,
3514 (this->vals_ == NULL
3515 ? NULL
3516 : this->vals_->copy()),
3517 this->location());
3518 }
3519
3520 Bexpression*
3521 do_get_backend(Translate_context*);
3522
3523 void
3524 do_export(Export*) const;
3525
3526 void
3527 do_dump_expression(Ast_dump_context*) const;
3528
3529 private:
3530 // The type of the map to construct.
3531 Type* type_;
3532 // The list of values.
3533 Expression_list* vals_;
3534 // The type of the key-value pair struct for each map element.
3535 Struct_type* element_type_;
3536 // A temporary reference to the variable storing the constructor initializer.
3537 Temporary_statement* constructor_temp_;
3538 };
3539
3540 // A type guard expression.
3541
3542 class Type_guard_expression : public Expression
3543 {
3544 public:
3545 Type_guard_expression(Expression* expr, Type* type, Location location)
3546 : Expression(EXPRESSION_TYPE_GUARD, location),
3547 expr_(expr), type_(type)
3548 { }
3549
3550 // Return the expression to convert.
3551 Expression*
3552 expr()
3553 { return this->expr_; }
3554
3555 // Return the type to which to convert.
3556 Type*
3557 type()
3558 { return this->type_; }
3559
3560 protected:
3561 int
3562 do_traverse(Traverse* traverse);
3563
3564 Expression*
3565 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3566
3567 Type*
3568 do_type()
3569 { return this->type_; }
3570
3571 void
3572 do_determine_type(const Type_context*)
3573 { this->expr_->determine_type_no_context(); }
3574
3575 void
3576 do_check_types(Gogo*);
3577
3578 Expression*
3579 do_copy()
3580 {
3581 return new Type_guard_expression(this->expr_->copy(), this->type_,
3582 this->location());
3583 }
3584
3585 Bexpression*
3586 do_get_backend(Translate_context*);
3587
3588 void
3589 do_dump_expression(Ast_dump_context*) const;
3590
3591 private:
3592 // The expression to convert.
3593 Expression* expr_;
3594 // The type to which to convert.
3595 Type* type_;
3596 };
3597
3598 // Class Heap_expression.
3599
3600 // When you take the address of an escaping expression, it is allocated
3601 // on the heap. This class implements that.
3602
3603 class Heap_expression : public Expression
3604 {
3605 public:
3606 Heap_expression(Expression* expr, Location location)
3607 : Expression(EXPRESSION_HEAP, location),
3608 expr_(expr)
3609 { }
3610
3611 Expression*
3612 expr() const
3613 { return this->expr_; }
3614
3615 protected:
3616 int
3617 do_traverse(Traverse* traverse)
3618 { return Expression::traverse(&this->expr_, traverse); }
3619
3620 Type*
3621 do_type();
3622 void
3623 do_determine_type(const Type_context*)
3624 { this->expr_->determine_type_no_context(); }
3625
3626 Expression*
3627 do_copy()
3628 {
3629 return Expression::make_heap_expression(this->expr_->copy(),
3630 this->location());
3631 }
3632
3633 Bexpression*
3634 do_get_backend(Translate_context*);
3635
3636 // We only export global objects, and the parser does not generate
3637 // this in global scope.
3638 void
3639 do_export(Export*) const
3640 { go_unreachable(); }
3641
3642 void
3643 do_dump_expression(Ast_dump_context*) const;
3644
3645 private:
3646 // The expression which is being put on the heap.
3647 Expression* expr_;
3648 };
3649
3650 // A receive expression.
3651
3652 class Receive_expression : public Expression
3653 {
3654 public:
3655 Receive_expression(Expression* channel, Location location)
3656 : Expression(EXPRESSION_RECEIVE, location),
3657 channel_(channel), temp_receiver_(NULL)
3658 { }
3659
3660 // Return the channel.
3661 Expression*
3662 channel()
3663 { return this->channel_; }
3664
3665 protected:
3666 int
3667 do_traverse(Traverse* traverse)
3668 { return Expression::traverse(&this->channel_, traverse); }
3669
3670 bool
3671 do_discarding_value()
3672 { return true; }
3673
3674 Type*
3675 do_type();
3676
3677 Expression*
3678 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3679
3680 void
3681 do_determine_type(const Type_context*)
3682 { this->channel_->determine_type_no_context(); }
3683
3684 void
3685 do_check_types(Gogo*);
3686
3687 Expression*
3688 do_copy()
3689 {
3690 return Expression::make_receive(this->channel_->copy(), this->location());
3691 }
3692
3693 bool
3694 do_must_eval_in_order() const
3695 { return true; }
3696
3697 Bexpression*
3698 do_get_backend(Translate_context*);
3699
3700 void
3701 do_dump_expression(Ast_dump_context*) const;
3702
3703 private:
3704 // The channel from which we are receiving.
3705 Expression* channel_;
3706 // A temporary reference to the variable storing the received data.
3707 Temporary_statement* temp_receiver_;
3708 };
3709
3710 // Conditional expressions.
3711
3712 class Conditional_expression : public Expression
3713 {
3714 public:
3715 Conditional_expression(Expression* cond, Expression* then_expr,
3716 Expression* else_expr, Location location)
3717 : Expression(EXPRESSION_CONDITIONAL, location),
3718 cond_(cond), then_(then_expr), else_(else_expr)
3719 {}
3720
3721 Expression*
3722 condition() const
3723 { return this->cond_; }
3724
3725 protected:
3726 int
3727 do_traverse(Traverse*);
3728
3729 Type*
3730 do_type();
3731
3732 void
3733 do_determine_type(const Type_context*);
3734
3735 Expression*
3736 do_copy()
3737 {
3738 return new Conditional_expression(this->cond_->copy(), this->then_->copy(),
3739 this->else_->copy(), this->location());
3740 }
3741
3742 Bexpression*
3743 do_get_backend(Translate_context* context);
3744
3745 void
3746 do_dump_expression(Ast_dump_context*) const;
3747
3748 private:
3749 // The condition to be checked.
3750 Expression* cond_;
3751 // The expression to execute if the condition is true.
3752 Expression* then_;
3753 // The expression to execute if the condition is false.
3754 Expression* else_;
3755 };
3756
3757 // Compound expressions.
3758
3759 class Compound_expression : public Expression
3760 {
3761 public:
3762 Compound_expression(Expression* init, Expression* expr, Location location)
3763 : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr)
3764 {}
3765
3766 Expression*
3767 init() const
3768 { return this->init_; }
3769
3770 protected:
3771 int
3772 do_traverse(Traverse*);
3773
3774 Type*
3775 do_type();
3776
3777 void
3778 do_determine_type(const Type_context*);
3779
3780 Expression*
3781 do_copy()
3782 {
3783 return new Compound_expression(this->init_->copy(), this->expr_->copy(),
3784 this->location());
3785 }
3786
3787 Bexpression*
3788 do_get_backend(Translate_context* context);
3789
3790 void
3791 do_dump_expression(Ast_dump_context*) const;
3792
3793 private:
3794 // The expression that is evaluated first and discarded.
3795 Expression* init_;
3796 // The expression that is evaluated and returned.
3797 Expression* expr_;
3798 };
3799
3800 // A numeric constant. This is used both for untyped constants and
3801 // for constants that have a type.
3802
3803 class Numeric_constant
3804 {
3805 public:
3806 Numeric_constant()
3807 : classification_(NC_INVALID), type_(NULL)
3808 { }
3809
3810 ~Numeric_constant();
3811
3812 Numeric_constant(const Numeric_constant&);
3813
3814 Numeric_constant& operator=(const Numeric_constant&);
3815
3816 // Set to an unsigned long value.
3817 void
3818 set_unsigned_long(Type*, unsigned long);
3819
3820 // Set to an integer value.
3821 void
3822 set_int(Type*, const mpz_t);
3823
3824 // Set to a rune value.
3825 void
3826 set_rune(Type*, const mpz_t);
3827
3828 // Set to a floating point value.
3829 void
3830 set_float(Type*, const mpfr_t);
3831
3832 // Set to a complex value.
3833 void
3834 set_complex(Type*, const mpc_t);
3835
3836 // Mark numeric constant as invalid.
3837 void
3838 set_invalid()
3839 { this->classification_ = NC_INVALID; }
3840
3841 // Classifiers.
3842 bool
3843 is_int() const
3844 { return this->classification_ == Numeric_constant::NC_INT; }
3845
3846 bool
3847 is_rune() const
3848 { return this->classification_ == Numeric_constant::NC_RUNE; }
3849
3850 bool
3851 is_float() const
3852 { return this->classification_ == Numeric_constant::NC_FLOAT; }
3853
3854 bool
3855 is_complex() const
3856 { return this->classification_ == Numeric_constant::NC_COMPLEX; }
3857
3858 bool
3859 is_invalid() const
3860 { return this->classification_ == Numeric_constant::NC_INVALID; }
3861
3862 // Value retrievers. These will initialize the values as well as
3863 // set them. GET_INT is only valid if IS_INT returns true, and
3864 // likewise respectively.
3865 void
3866 get_int(mpz_t*) const;
3867
3868 void
3869 get_rune(mpz_t*) const;
3870
3871 void
3872 get_float(mpfr_t*) const;
3873
3874 void
3875 get_complex(mpc_t*) const;
3876
3877 // Codes returned by to_unsigned_long.
3878 enum To_unsigned_long
3879 {
3880 // Value is integer and fits in unsigned long.
3881 NC_UL_VALID,
3882 // Value is not integer.
3883 NC_UL_NOTINT,
3884 // Value is integer but is negative.
3885 NC_UL_NEGATIVE,
3886 // Value is non-negative integer but does not fit in unsigned
3887 // long.
3888 NC_UL_BIG
3889 };
3890
3891 // If the value can be expressed as an integer that fits in an
3892 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
3893 // one of the other To_unsigned_long codes.
3894 To_unsigned_long
3895 to_unsigned_long(unsigned long* val) const;
3896
3897 // If the value can be expressed as an int, return true and
3898 // initialize and set VAL. This will return false for a value with
3899 // an explicit float or complex type, even if the value is integral.
3900 bool
3901 to_int(mpz_t* val) const;
3902
3903 // If the value can be expressed as a float, return true and
3904 // initialize and set VAL.
3905 bool
3906 to_float(mpfr_t* val) const;
3907
3908 // If the value can be expressed as a complex, return true and
3909 // initialize and set VR and VI.
3910 bool
3911 to_complex(mpc_t* val) const;
3912
3913 // Get the type.
3914 Type*
3915 type() const;
3916
3917 // If the constant can be expressed in TYPE, then set the type of
3918 // the constant to TYPE and return true. Otherwise return false,
3919 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
3920 // the location to use for the error.
3921 bool
3922 set_type(Type* type, bool issue_error, Location location);
3923
3924 // Return an Expression for this value.
3925 Expression*
3926 expression(Location) const;
3927
3928 private:
3929 void
3930 clear();
3931
3932 To_unsigned_long
3933 mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
3934
3935 To_unsigned_long
3936 mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
3937
3938 bool
3939 check_int_type(Integer_type*, bool, Location);
3940
3941 bool
3942 check_float_type(Float_type*, bool, Location);
3943
3944 bool
3945 check_complex_type(Complex_type*, bool, Location);
3946
3947 // The kinds of constants.
3948 enum Classification
3949 {
3950 NC_INVALID,
3951 NC_RUNE,
3952 NC_INT,
3953 NC_FLOAT,
3954 NC_COMPLEX
3955 };
3956
3957 // The kind of constant.
3958 Classification classification_;
3959 // The value.
3960 union
3961 {
3962 // If NC_INT or NC_RUNE.
3963 mpz_t int_val;
3964 // If NC_FLOAT.
3965 mpfr_t float_val;
3966 // If NC_COMPLEX.
3967 mpc_t complex_val;
3968 } u_;
3969 // The type if there is one. This will be NULL for an untyped
3970 // constant.
3971 Type* type_;
3972 };
3973
3974 #endif // !defined(GO_EXPRESSIONS_H)