compiler: introduce size threshold for nil checks
[official-gcc.git] / gcc / go / gofrontend / expressions.h
blob9de734e636f342fba3d4c9606f5364dffe5a6598
1 // expressions.h -- Go frontend expression handling. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #ifndef GO_EXPRESSIONS_H
8 #define GO_EXPRESSIONS_H
10 #include <mpfr.h>
11 #include <mpc.h>
13 #include "operator.h"
14 #include "runtime.h"
16 class Gogo;
17 class Translate_context;
18 class Traverse;
19 class Statement_inserter;
20 class Type;
21 class Method;
22 struct Type_context;
23 class Integer_type;
24 class Float_type;
25 class Complex_type;
26 class Function_type;
27 class Map_type;
28 class Struct_type;
29 class Struct_field;
30 class Expression_list;
31 class Var_expression;
32 class Enclosed_var_expression;
33 class Temporary_reference_expression;
34 class Set_and_use_temporary_expression;
35 class String_expression;
36 class Type_conversion_expression;
37 class Unsafe_type_conversion_expression;
38 class Unary_expression;
39 class Binary_expression;
40 class String_concat_expression;
41 class Call_expression;
42 class Call_result_expression;
43 class Func_expression;
44 class Func_descriptor_expression;
45 class Unknown_expression;
46 class Index_expression;
47 class Array_index_expression;
48 class String_index_expression;
49 class Map_index_expression;
50 class Bound_method_expression;
51 class Field_reference_expression;
52 class Interface_field_reference_expression;
53 class Allocation_expression;
54 class Composite_literal_expression;
55 class Struct_construction_expression;
56 class Array_construction_expression;
57 class Fixed_array_construction_expression;
58 class Slice_construction_expression;
59 class Map_construction_expression;
60 class Type_guard_expression;
61 class Heap_expression;
62 class Receive_expression;
63 class Conditional_expression;
64 class Compound_expression;
65 class Numeric_constant;
66 class Named_object;
67 class Export;
68 class Import;
69 class Temporary_statement;
70 class Label;
71 class Ast_dump_context;
72 class String_dump;
74 // The precision to use for complex values represented as an mpc_t.
75 const int mpc_precision = 256;
77 // The base class for all expressions.
79 class Expression
81 public:
82 // The types of expressions.
83 enum Expression_classification
85 EXPRESSION_ERROR,
86 EXPRESSION_TYPE,
87 EXPRESSION_UNARY,
88 EXPRESSION_BINARY,
89 EXPRESSION_STRING_CONCAT,
90 EXPRESSION_CONST_REFERENCE,
91 EXPRESSION_VAR_REFERENCE,
92 EXPRESSION_ENCLOSED_VAR_REFERENCE,
93 EXPRESSION_TEMPORARY_REFERENCE,
94 EXPRESSION_SET_AND_USE_TEMPORARY,
95 EXPRESSION_SINK,
96 EXPRESSION_FUNC_REFERENCE,
97 EXPRESSION_FUNC_DESCRIPTOR,
98 EXPRESSION_FUNC_CODE_REFERENCE,
99 EXPRESSION_UNKNOWN_REFERENCE,
100 EXPRESSION_BOOLEAN,
101 EXPRESSION_STRING,
102 EXPRESSION_STRING_INFO,
103 EXPRESSION_INTEGER,
104 EXPRESSION_FLOAT,
105 EXPRESSION_COMPLEX,
106 EXPRESSION_NIL,
107 EXPRESSION_IOTA,
108 EXPRESSION_CALL,
109 EXPRESSION_CALL_RESULT,
110 EXPRESSION_BOUND_METHOD,
111 EXPRESSION_INDEX,
112 EXPRESSION_ARRAY_INDEX,
113 EXPRESSION_STRING_INDEX,
114 EXPRESSION_MAP_INDEX,
115 EXPRESSION_SELECTOR,
116 EXPRESSION_FIELD_REFERENCE,
117 EXPRESSION_INTERFACE_FIELD_REFERENCE,
118 EXPRESSION_ALLOCATION,
119 EXPRESSION_TYPE_GUARD,
120 EXPRESSION_CONVERSION,
121 EXPRESSION_UNSAFE_CONVERSION,
122 EXPRESSION_STRUCT_CONSTRUCTION,
123 EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
124 EXPRESSION_SLICE_CONSTRUCTION,
125 EXPRESSION_MAP_CONSTRUCTION,
126 EXPRESSION_COMPOSITE_LITERAL,
127 EXPRESSION_HEAP,
128 EXPRESSION_RECEIVE,
129 EXPRESSION_TYPE_DESCRIPTOR,
130 EXPRESSION_GC_SYMBOL,
131 EXPRESSION_PTRMASK_SYMBOL,
132 EXPRESSION_TYPE_INFO,
133 EXPRESSION_SLICE_INFO,
134 EXPRESSION_SLICE_VALUE,
135 EXPRESSION_INTERFACE_INFO,
136 EXPRESSION_INTERFACE_VALUE,
137 EXPRESSION_INTERFACE_MTABLE,
138 EXPRESSION_STRUCT_FIELD_OFFSET,
139 EXPRESSION_LABEL_ADDR,
140 EXPRESSION_CONDITIONAL,
141 EXPRESSION_COMPOUND,
142 EXPRESSION_BACKEND
145 Expression(Expression_classification, Location);
147 virtual ~Expression();
149 // Make an error expression. This is used when a parse error occurs
150 // to prevent cascading errors.
151 static Expression*
152 make_error(Location);
154 // Make an expression which is really a type. This is used during
155 // parsing.
156 static Expression*
157 make_type(Type*, Location);
159 // Make a unary expression.
160 static Expression*
161 make_unary(Operator, Expression*, Location);
163 // Make a binary expression.
164 static Expression*
165 make_binary(Operator, Expression*, Expression*, Location);
167 // Make a string concatenation expression.
168 static Expression*
169 make_string_concat(Expression_list*);
171 // Make a reference to a constant in an expression.
172 static Expression*
173 make_const_reference(Named_object*, Location);
175 // Make a reference to a variable in an expression.
176 static Expression*
177 make_var_reference(Named_object*, Location);
179 // Make a reference to a variable within an enclosing function.
180 static Expression*
181 make_enclosing_var_reference(Expression*, Named_object*, Location);
183 // Make a reference to a temporary variable. Temporary variables
184 // are always created by a single statement, which is what we use to
185 // refer to them.
186 static Temporary_reference_expression*
187 make_temporary_reference(Temporary_statement*, Location);
189 // Make an expressions which sets a temporary variable and then
190 // evaluates to a reference to that temporary variable. This is
191 // used to set a temporary variable while retaining the order of
192 // evaluation.
193 static Set_and_use_temporary_expression*
194 make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
196 // Make a sink expression--a reference to the blank identifier _.
197 static Expression*
198 make_sink(Location);
200 // Make a reference to a function in an expression. This returns a
201 // pointer to the struct holding the address of the function
202 // followed by any closed-over variables.
203 static Expression*
204 make_func_reference(Named_object*, Expression* closure, Location);
206 // Make a function descriptor, an immutable struct with a single
207 // field that points to the function code. This may only be used
208 // with functions that do not have closures. FN is the function for
209 // which we are making the descriptor.
210 static Func_descriptor_expression*
211 make_func_descriptor(Named_object* fn);
213 // Make a reference to the code of a function. This is used to set
214 // descriptor and closure fields.
215 static Expression*
216 make_func_code_reference(Named_object*, Location);
218 // Make a reference to an unknown name. In a correct program this
219 // will always be lowered to a real const/var/func reference.
220 static Unknown_expression*
221 make_unknown_reference(Named_object*, Location);
223 // Make a constant bool expression.
224 static Expression*
225 make_boolean(bool val, Location);
227 // Make a constant string expression.
228 static Expression*
229 make_string(const std::string&, Location);
231 // Make an expression that evaluates to some characteristic of an string.
232 // For simplicity, the enum values must match the field indexes in the
233 // underlying struct.
234 enum String_info
236 // The underlying data in the string.
237 STRING_INFO_DATA,
238 // The length of the string.
239 STRING_INFO_LENGTH
242 static Expression*
243 make_string_info(Expression* string, String_info, Location);
245 // Make a character constant expression. TYPE should be NULL for an
246 // abstract type.
247 static Expression*
248 make_character(const mpz_t*, Type*, Location);
250 // Make a constant integer expression from a multi-precision
251 // integer. TYPE should be NULL for an abstract type.
252 static Expression*
253 make_integer_z(const mpz_t*, Type*, Location);
255 // Make a constant integer expression from an unsigned long. TYPE
256 // should be NULL for an abstract type.
257 static Expression*
258 make_integer_ul(unsigned long, Type*, Location);
260 // Make a constant integer expression from a signed long. TYPE
261 // should be NULL for an abstract type.
262 static Expression*
263 make_integer_sl(long, Type*, Location);
265 // Make a constant integer expression from an int64_t. TYPE should
266 // be NULL for an abstract type.
267 static Expression*
268 make_integer_int64(int64_t, Type*, Location);
270 // Make a constant float expression. TYPE should be NULL for an
271 // abstract type.
272 static Expression*
273 make_float(const mpfr_t*, Type*, Location);
275 // Make a constant complex expression. TYPE should be NULL for an
276 // abstract type.
277 static Expression*
278 make_complex(const mpc_t*, Type*, Location);
280 // Make a nil expression.
281 static Expression*
282 make_nil(Location);
284 // Make an iota expression. This is used for the predeclared
285 // constant iota.
286 static Expression*
287 make_iota();
289 // Make a call expression.
290 static Call_expression*
291 make_call(Expression* func, Expression_list* args, bool is_varargs,
292 Location);
294 // Make a reference to a specific result of a call expression which
295 // returns a tuple.
296 static Expression*
297 make_call_result(Call_expression*, unsigned int index);
299 // Make an expression which is a method bound to its first
300 // parameter. METHOD is the method being called, FUNCTION is the
301 // function to call.
302 static Bound_method_expression*
303 make_bound_method(Expression* object, const Method* method,
304 Named_object* function, Location);
306 // Make an index or slice expression. This is a parser expression
307 // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
308 // index rather than a slice. CAP may be NULL, meaning we use the default
309 // capacity of LEFT. At parse time we may not know the type of LEFT.
310 // After parsing this is lowered to an array index, a string index,
311 // or a map index.
312 static Expression*
313 make_index(Expression* left, Expression* start, Expression* end,
314 Expression* cap, Location);
316 // Make an array index expression. END may be NULL, in which case
317 // this is an lvalue. CAP may be NULL, in which case it defaults
318 // to cap(ARRAY).
319 static Expression*
320 make_array_index(Expression* array, Expression* start, Expression* end,
321 Expression* cap, Location);
323 // Make a string index expression. END may be NULL. This is never
324 // an lvalue.
325 static Expression*
326 make_string_index(Expression* string, Expression* start, Expression* end,
327 Location);
329 // Make a map index expression. This is an lvalue.
330 static Map_index_expression*
331 make_map_index(Expression* map, Expression* val, Location);
333 // Make a selector. This is a parser expression which represents
334 // LEFT.NAME. At parse time we may not know the type of the left
335 // hand side.
336 static Expression*
337 make_selector(Expression* left, const std::string& name, Location);
339 // Make a reference to a field in a struct.
340 static Field_reference_expression*
341 make_field_reference(Expression*, unsigned int field_index, Location);
343 // Make a reference to a field of an interface, with an associated
344 // object.
345 static Expression*
346 make_interface_field_reference(Expression*, const std::string&,
347 Location);
349 // Make an allocation expression.
350 static Expression*
351 make_allocation(Type*, Location);
353 // Make a type guard expression.
354 static Expression*
355 make_type_guard(Expression*, Type*, Location);
357 // Make a type cast expression.
358 static Expression*
359 make_cast(Type*, Expression*, Location);
361 // Make an unsafe type cast expression. This is only used when
362 // passing parameter to builtin functions that are part of the Go
363 // runtime.
364 static Expression*
365 make_unsafe_cast(Type*, Expression*, Location);
367 // Make a composite literal. The DEPTH parameter is how far down we
368 // are in a list of composite literals with omitted types. HAS_KEYS
369 // is true if the expression list has keys alternating with values.
370 // ALL_ARE_NAMES is true if all the keys could be struct field
371 // names.
372 static Expression*
373 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
374 bool all_are_names, Location);
376 // Make a struct composite literal.
377 static Expression*
378 make_struct_composite_literal(Type*, Expression_list*, Location);
380 // Make an array composite literal.
381 static Expression*
382 make_array_composite_literal(Type*, Expression_list*, Location);
384 // Make a slice composite literal.
385 static Slice_construction_expression*
386 make_slice_composite_literal(Type*, Expression_list*, Location);
388 // Take an expression and allocate it on the heap.
389 static Expression*
390 make_heap_expression(Expression*, Location);
392 // Make a receive expression. VAL is NULL for a unary receive.
393 static Receive_expression*
394 make_receive(Expression* channel, Location);
396 // Make an expression which evaluates to the address of the type
397 // descriptor for TYPE.
398 static Expression*
399 make_type_descriptor(Type* type, Location);
401 // Make an expression which evaluates to the address of the gc
402 // symbol for TYPE.
403 static Expression*
404 make_gc_symbol(Type* type);
406 // Make an expression that evaluates to the address of a ptrmask
407 // symbol for TYPE. For most types this will be the same as
408 // make_gc_symbol, but for larger types make_gc_symbol will return a
409 // gcprog while this will return a ptrmask.
410 static Expression*
411 make_ptrmask_symbol(Type* type);
413 // Make an expression which evaluates to some characteristic of a
414 // type. These are only used for type descriptors, so there is no
415 // location parameter.
416 enum Type_info
418 // The size of a value of the type.
419 TYPE_INFO_SIZE,
420 // The required alignment of a value of the type.
421 TYPE_INFO_ALIGNMENT,
422 // The required alignment of a value of the type when used as a
423 // field in a struct.
424 TYPE_INFO_FIELD_ALIGNMENT,
425 // The size of the prefix of a value of the type that contains
426 // all the pointers. This is 0 for a type that contains no
427 // pointers. It is always <= TYPE_INFO_SIZE.
428 TYPE_INFO_BACKEND_PTRDATA,
429 // Like TYPE_INFO_BACKEND_PTRDATA, but the ptrdata value that we
430 // want to store in a type descriptor. They are the same for
431 // most types, but can differ for a type that uses a gcprog.
432 TYPE_INFO_DESCRIPTOR_PTRDATA
435 static Expression*
436 make_type_info(Type* type, Type_info);
438 // Make an expression that evaluates to some characteristic of a
439 // slice. For simplicity, the enum values must match the field indexes
440 // in the underlying struct.
441 enum Slice_info
443 // The underlying data of the slice.
444 SLICE_INFO_VALUE_POINTER,
445 // The length of the slice.
446 SLICE_INFO_LENGTH,
447 // The capacity of the slice.
448 SLICE_INFO_CAPACITY
451 static Expression*
452 make_slice_info(Expression* slice, Slice_info, Location);
454 // Make an expression for a slice value.
455 static Expression*
456 make_slice_value(Type*, Expression* valptr, Expression* len, Expression* cap,
457 Location);
459 // Make an expression that evaluates to some characteristic of an
460 // interface. For simplicity, the enum values must match the field indexes
461 // in the underlying struct.
462 enum Interface_info
464 // The type descriptor of an empty interface.
465 INTERFACE_INFO_TYPE_DESCRIPTOR = 0,
466 // The methods of an interface.
467 INTERFACE_INFO_METHODS = 0,
468 // The first argument to pass to an interface method.
469 INTERFACE_INFO_OBJECT
472 static Expression*
473 make_interface_info(Expression* iface, Interface_info, Location);
475 // Make an expression for an interface value.
476 static Expression*
477 make_interface_value(Type*, Expression*, Expression*, Location);
479 // Make an expression that builds a reference to the interface method table
480 // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
481 // reference to the interface method table for the pointer receiver type.
482 static Expression*
483 make_interface_mtable_ref(Interface_type* itype, Type* type,
484 bool is_pointer, Location);
486 // Make an expression which evaluates to the offset of a field in a
487 // struct. This is only used for type descriptors, so there is no
488 // location parameter.
489 static Expression*
490 make_struct_field_offset(Struct_type*, const Struct_field*);
492 // Make an expression which evaluates to the address of an unnamed
493 // label.
494 static Expression*
495 make_label_addr(Label*, Location);
497 // Make a conditional expression.
498 static Expression*
499 make_conditional(Expression*, Expression*, Expression*, Location);
501 // Make a compound expression.
502 static Expression*
503 make_compound(Expression*, Expression*, Location);
505 // Make a backend expression.
506 static Expression*
507 make_backend(Bexpression*, Type*, Location);
509 enum Nil_check_classification
511 // Use the default policy for deciding if this deref needs a check.
512 NIL_CHECK_DEFAULT,
513 // An explicit check is required for this dereference operation.
514 NIL_CHECK_NEEDED,
515 // No check needed for this dereference operation.
516 NIL_CHECK_NOT_NEEDED,
517 // A type error or error construct was encountered when determining
518 // whether this deref needs an explicit check.
519 NIL_CHECK_ERROR_ENCOUNTERED
522 // Make a dereference expression.
523 static Expression*
524 make_dereference(Expression*, Nil_check_classification, Location);
526 // Return the expression classification.
527 Expression_classification
528 classification() const
529 { return this->classification_; }
531 // Return the location of the expression.
532 Location
533 location() const
534 { return this->location_; }
536 // Return whether this is a constant expression.
537 bool
538 is_constant() const
539 { return this->do_is_constant(); }
541 // Return whether this expression can be used as a static
542 // initializer. This is true for an expression that has only
543 // numbers and pointers to global variables or composite literals
544 // that do not require runtime initialization. It is false if we
545 // must generate code to compute this expression when it is used to
546 // initialize a global variable. This is not a language-level
547 // concept, but an implementation-level one. If this expression is
548 // used to initialize a global variable, this is true if we can pass
549 // an initializer to the backend, false if we must generate code to
550 // initialize the variable. It is always safe for this method to
551 // return false, but the resulting code may be less efficient.
552 bool
553 is_static_initializer() const
554 { return this->do_is_static_initializer(); }
556 // If this is not a numeric constant, return false. If it is one,
557 // return true, and set VAL to hold the value.
558 bool
559 numeric_constant_value(Numeric_constant* val) const
560 { return this->do_numeric_constant_value(val); }
562 // If this is not a constant expression with string type, return
563 // false. If it is one, return true, and set VAL to the value.
564 bool
565 string_constant_value(std::string* val) const
566 { return this->do_string_constant_value(val); }
568 // This is called if the value of this expression is being
569 // discarded. This issues warnings about computed values being
570 // unused. This returns true if all is well, false if it issued an
571 // error message.
572 bool
573 discarding_value()
574 { return this->do_discarding_value(); }
576 // Return whether this is an error expression.
577 bool
578 is_error_expression() const
579 { return this->classification_ == EXPRESSION_ERROR; }
581 // Return whether this expression really represents a type.
582 bool
583 is_type_expression() const
584 { return this->classification_ == EXPRESSION_TYPE; }
586 // If this is a variable reference, return the Var_expression
587 // structure. Otherwise, return NULL. This is a controlled dynamic
588 // cast.
589 Var_expression*
590 var_expression()
591 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
593 const Var_expression*
594 var_expression() const
595 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
597 // If this is a enclosed_variable reference, return the
598 // Enclosed_var_expression structure. Otherwise, return NULL.
599 // This is a controlled dynamic cast.
600 Enclosed_var_expression*
601 enclosed_var_expression()
602 { return this->convert<Enclosed_var_expression,
603 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
605 const Enclosed_var_expression*
606 enclosed_var_expression() const
607 { return this->convert<const Enclosed_var_expression,
608 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
611 // If this is a reference to a temporary variable, return the
612 // Temporary_reference_expression. Otherwise, return NULL.
613 Temporary_reference_expression*
614 temporary_reference_expression()
616 return this->convert<Temporary_reference_expression,
617 EXPRESSION_TEMPORARY_REFERENCE>();
620 // If this is a set-and-use-temporary, return the
621 // Set_and_use_temporary_expression. Otherwise, return NULL.
622 Set_and_use_temporary_expression*
623 set_and_use_temporary_expression()
625 return this->convert<Set_and_use_temporary_expression,
626 EXPRESSION_SET_AND_USE_TEMPORARY>();
629 // Return whether this is a sink expression.
630 bool
631 is_sink_expression() const
632 { return this->classification_ == EXPRESSION_SINK; }
634 // If this is a string expression, return the String_expression
635 // structure. Otherwise, return NULL.
636 String_expression*
637 string_expression()
638 { return this->convert<String_expression, EXPRESSION_STRING>(); }
640 // If this is a conversion expression, return the Type_conversion_expression
641 // structure. Otherwise, return NULL.
642 Type_conversion_expression*
643 conversion_expression()
644 { return this->convert<Type_conversion_expression, EXPRESSION_CONVERSION>(); }
646 // If this is an unsafe conversion expression, return the
647 // Unsafe_type_conversion_expression structure. Otherwise, return NULL.
648 Unsafe_type_conversion_expression*
649 unsafe_conversion_expression()
651 return this->convert<Unsafe_type_conversion_expression,
652 EXPRESSION_UNSAFE_CONVERSION>();
655 // Return whether this is the expression nil.
656 bool
657 is_nil_expression() const
658 { return this->classification_ == EXPRESSION_NIL; }
660 // If this is an indirection through a pointer, return the
661 // expression being pointed through. Otherwise return this.
662 Expression*
663 deref();
665 // If this is a unary expression, return the Unary_expression
666 // structure. Otherwise return NULL.
667 Unary_expression*
668 unary_expression()
669 { return this->convert<Unary_expression, EXPRESSION_UNARY>(); }
671 // If this is a binary expression, return the Binary_expression
672 // structure. Otherwise return NULL.
673 Binary_expression*
674 binary_expression()
675 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
677 // If this is a string concatenation expression, return the
678 // String_concat_expression structure. Otherwise, return NULL.
679 String_concat_expression*
680 string_concat_expression()
682 return this->convert<String_concat_expression, EXPRESSION_STRING_CONCAT>();
685 // If this is a call expression, return the Call_expression
686 // structure. Otherwise, return NULL. This is a controlled dynamic
687 // cast.
688 Call_expression*
689 call_expression()
690 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
692 // If this is a call_result expression, return the Call_result_expression
693 // structure. Otherwise, return NULL. This is a controlled dynamic
694 // cast.
695 Call_result_expression*
696 call_result_expression()
697 { return this->convert<Call_result_expression, EXPRESSION_CALL_RESULT>(); }
699 // If this is an expression which refers to a function, return the
700 // Func_expression structure. Otherwise, return NULL.
701 Func_expression*
702 func_expression()
703 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
705 const Func_expression*
706 func_expression() const
707 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
709 // If this is an expression which refers to an unknown name, return
710 // the Unknown_expression structure. Otherwise, return NULL.
711 Unknown_expression*
712 unknown_expression()
713 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
715 const Unknown_expression*
716 unknown_expression() const
718 return this->convert<const Unknown_expression,
719 EXPRESSION_UNKNOWN_REFERENCE>();
722 // If this is an index expression, return the Index_expression
723 // structure. Otherwise, return NULL.
724 Index_expression*
725 index_expression()
726 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
728 // If this is an expression which refers to indexing in a array,
729 // return the Array_index_expression structure. Otherwise, return
730 // NULL.
731 Array_index_expression*
732 array_index_expression()
733 { return this->convert<Array_index_expression, EXPRESSION_ARRAY_INDEX>(); }
735 // If this is an expression which refers to indexing in a string,
736 // return the String_index_expression structure. Otherwise, return
737 // NULL.
738 String_index_expression*
739 string_index_expression()
740 { return this->convert<String_index_expression, EXPRESSION_STRING_INDEX>(); }
742 // If this is an expression which refers to indexing in a map,
743 // return the Map_index_expression structure. Otherwise, return
744 // NULL.
745 Map_index_expression*
746 map_index_expression()
747 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
749 // If this is a bound method expression, return the
750 // Bound_method_expression structure. Otherwise, return NULL.
751 Bound_method_expression*
752 bound_method_expression()
753 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
755 // If this is a reference to a field in a struct, return the
756 // Field_reference_expression structure. Otherwise, return NULL.
757 Field_reference_expression*
758 field_reference_expression()
760 return this->convert<Field_reference_expression,
761 EXPRESSION_FIELD_REFERENCE>();
764 // If this is a reference to a field in an interface, return the
765 // Interface_field_reference_expression structure. Otherwise,
766 // return NULL.
767 Interface_field_reference_expression*
768 interface_field_reference_expression()
770 return this->convert<Interface_field_reference_expression,
771 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
774 // If this is an allocation expression, return the Allocation_expression
775 // structure. Otherwise, return NULL.
776 Allocation_expression*
777 allocation_expression()
778 { return this->convert<Allocation_expression, EXPRESSION_ALLOCATION>(); }
780 // If this is a general composite literal, return the
781 // Composite_literal_expression structure. Otherwise, return NULL.
782 Composite_literal_expression*
783 complit()
785 return this->convert<Composite_literal_expression,
786 EXPRESSION_COMPOSITE_LITERAL>();
789 // If this is a struct composite literal, return the
790 // Struct_construction_expression structure. Otherwise, return NULL.
791 Struct_construction_expression*
792 struct_literal()
794 return this->convert<Struct_construction_expression,
795 EXPRESSION_STRUCT_CONSTRUCTION>();
798 // If this is a array composite literal, return the
799 // Array_construction_expression structure. Otherwise, return NULL.
800 Fixed_array_construction_expression*
801 array_literal()
803 return this->convert<Fixed_array_construction_expression,
804 EXPRESSION_FIXED_ARRAY_CONSTRUCTION>();
807 // If this is a slice composite literal, return the
808 // Slice_construction_expression structure. Otherwise, return NULL.
809 Slice_construction_expression*
810 slice_literal()
812 return this->convert<Slice_construction_expression,
813 EXPRESSION_SLICE_CONSTRUCTION>();
816 // If this is a map composite literal, return the
817 // Map_construction_expression structure. Otherwise, return NULL.
818 Map_construction_expression*
819 map_literal()
821 return this->convert<Map_construction_expression,
822 EXPRESSION_MAP_CONSTRUCTION>();
825 // If this is a type guard expression, return the
826 // Type_guard_expression structure. Otherwise, return NULL.
827 Type_guard_expression*
828 type_guard_expression()
829 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
831 // If this is a heap expression, returhn the Heap_expression structure.
832 // Otherwise, return NULL.
833 Heap_expression*
834 heap_expression()
835 { return this->convert<Heap_expression, EXPRESSION_HEAP>(); }
837 // If this is a receive expression, return the Receive_expression
838 // structure. Otherwise, return NULL.
839 Receive_expression*
840 receive_expression()
841 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
843 // If this is a conditional expression, return the Conditional_expression
844 // structure. Otherwise, return NULL.
845 Conditional_expression*
846 conditional_expression()
847 { return this->convert<Conditional_expression, EXPRESSION_CONDITIONAL>(); }
849 // If this is a compound expression, return the Compound_expression structure.
850 // Otherwise, return NULL.
851 Compound_expression*
852 compound_expression()
853 { return this->convert<Compound_expression, EXPRESSION_COMPOUND>(); }
855 // Return true if this is a composite literal.
856 bool
857 is_composite_literal() const;
859 // Return true if this is a composite literal which is not constant.
860 bool
861 is_nonconstant_composite_literal() const;
863 // Return true if this is a variable or temporary variable.
864 bool
865 is_variable() const;
867 // Return true if this is a reference to a local variable.
868 bool
869 is_local_variable() const;
871 // Make the builtin function descriptor type, so that it can be
872 // converted.
873 static void
874 make_func_descriptor_type();
876 // Traverse an expression.
877 static int
878 traverse(Expression**, Traverse*);
880 // Traverse subexpressions of this expression.
882 traverse_subexpressions(Traverse*);
884 // Lower an expression. This is called immediately after parsing.
885 // FUNCTION is the function we are in; it will be NULL for an
886 // expression initializing a global variable. INSERTER may be used
887 // to insert statements before the statement or initializer
888 // containing this expression; it is normally used to create
889 // temporary variables. IOTA_VALUE is the value that we should give
890 // to any iota expressions. This function must resolve expressions
891 // which could not be fully parsed into their final form. It
892 // returns the same Expression or a new one.
893 Expression*
894 lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
895 int iota_value)
896 { return this->do_lower(gogo, function, inserter, iota_value); }
898 // Flatten an expression. This is called after order_evaluation.
899 // FUNCTION is the function we are in; it will be NULL for an
900 // expression initializing a global variable. INSERTER may be used
901 // to insert statements before the statement or initializer
902 // containing this expression; it is normally used to create
903 // temporary variables. This function must resolve expressions
904 // which could not be fully parsed into their final form. It
905 // returns the same Expression or a new one.
906 Expression*
907 flatten(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
908 { return this->do_flatten(gogo, function, inserter); }
910 // Determine the real type of an expression with abstract integer,
911 // floating point, or complex type. TYPE_CONTEXT describes the
912 // expected type.
913 void
914 determine_type(const Type_context*);
916 // Check types in an expression.
917 void
918 check_types(Gogo* gogo)
919 { this->do_check_types(gogo); }
921 // Determine the type when there is no context.
922 void
923 determine_type_no_context();
925 // Return the current type of the expression. This may be changed
926 // by determine_type.
927 Type*
928 type()
929 { return this->do_type(); }
931 // Return a copy of an expression.
932 Expression*
933 copy()
934 { return this->do_copy(); }
936 // Return whether the expression is addressable--something which may
937 // be used as the operand of the unary & operator.
938 bool
939 is_addressable() const
940 { return this->do_is_addressable(); }
942 // Note that we are taking the address of this expression. ESCAPES
943 // is true if this address escapes the current function.
944 void
945 address_taken(bool escapes)
946 { this->do_address_taken(escapes); }
948 // Note that a nil check must be issued for this expression.
949 void
950 issue_nil_check()
951 { this->do_issue_nil_check(); }
953 // Return whether this expression must be evaluated in order
954 // according to the order of evaluation rules. This is basically
955 // true of all expressions with side-effects.
956 bool
957 must_eval_in_order() const
958 { return this->do_must_eval_in_order(); }
960 // Return whether subexpressions of this expression must be
961 // evaluated in order. This is true of index expressions and
962 // pointer indirections. This sets *SKIP to the number of
963 // subexpressions to skip during traversing, as index expressions
964 // only requiring moving the index, not the array.
965 bool
966 must_eval_subexpressions_in_order(int* skip) const
968 *skip = 0;
969 return this->do_must_eval_subexpressions_in_order(skip);
972 // Return the backend representation for this expression.
973 Bexpression*
974 get_backend(Translate_context*);
976 // Return an expression handling any conversions which must be done during
977 // assignment.
978 static Expression*
979 convert_for_assignment(Gogo*, Type* lhs_type, Expression* rhs,
980 Location location);
982 // Return an expression converting a value of one interface type to another
983 // interface type. If FOR_TYPE_GUARD is true this is for a type
984 // assertion.
985 static Expression*
986 convert_interface_to_interface(Type* lhs_type,
987 Expression* rhs, bool for_type_guard,
988 Location);
990 // Return a backend expression implementing the comparison LEFT OP RIGHT.
991 // TYPE is the type of both sides.
992 static Bexpression*
993 comparison(Translate_context*, Type* result_type, Operator op,
994 Expression* left, Expression* right, Location);
996 // Return the backend expression for the numeric constant VAL.
997 static Bexpression*
998 backend_numeric_constant_expression(Translate_context*,
999 Numeric_constant* val);
1001 // Export the expression. This is only used for constants. It will
1002 // be used for things like values of named constants and sizes of
1003 // arrays.
1004 void
1005 export_expression(Export* exp) const
1006 { this->do_export(exp); }
1008 // Import an expression.
1009 static Expression*
1010 import_expression(Import*);
1012 // Return an expression which checks that VAL, of arbitrary integer type,
1013 // is non-negative and is not more than the maximum integer value.
1014 static Expression*
1015 check_bounds(Expression* val, Location);
1017 // Dump an expression to a dump constext.
1018 void
1019 dump_expression(Ast_dump_context*) const;
1021 protected:
1022 // May be implemented by child class: traverse the expressions.
1023 virtual int
1024 do_traverse(Traverse*);
1026 // Return a lowered expression.
1027 virtual Expression*
1028 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1029 { return this; }
1031 // Return a flattened expression.
1032 virtual Expression*
1033 do_flatten(Gogo*, Named_object*, Statement_inserter*)
1034 { return this; }
1037 // Return whether this is a constant expression.
1038 virtual bool
1039 do_is_constant() const
1040 { return false; }
1042 // Return whether this expression can be used as a constant
1043 // initializer.
1044 virtual bool
1045 do_is_static_initializer() const
1046 { return false; }
1048 // Return whether this is a constant expression of numeric type, and
1049 // set the Numeric_constant to the value.
1050 virtual bool
1051 do_numeric_constant_value(Numeric_constant*) const
1052 { return false; }
1054 // Return whether this is a constant expression of string type, and
1055 // set VAL to the value.
1056 virtual bool
1057 do_string_constant_value(std::string*) const
1058 { return false; }
1060 // Called by the parser if the value is being discarded.
1061 virtual bool
1062 do_discarding_value();
1064 // Child class holds type.
1065 virtual Type*
1066 do_type() = 0;
1068 // Child class implements determining type information.
1069 virtual void
1070 do_determine_type(const Type_context*) = 0;
1072 // Child class implements type checking if needed.
1073 virtual void
1074 do_check_types(Gogo*)
1077 // Child class implements copying.
1078 virtual Expression*
1079 do_copy() = 0;
1081 // Child class implements whether the expression is addressable.
1082 virtual bool
1083 do_is_addressable() const
1084 { return false; }
1086 // Child class implements taking the address of an expression.
1087 virtual void
1088 do_address_taken(bool)
1091 // Child class implements issuing a nil check if the address is taken.
1092 virtual void
1093 do_issue_nil_check()
1096 // Child class implements whether this expression must be evaluated
1097 // in order.
1098 virtual bool
1099 do_must_eval_in_order() const
1100 { return false; }
1102 // Child class implements whether this expressions requires that
1103 // subexpressions be evaluated in order. The child implementation
1104 // may set *SKIP if it should be non-zero.
1105 virtual bool
1106 do_must_eval_subexpressions_in_order(int* /* skip */) const
1107 { return false; }
1109 // Child class implements conversion to backend representation.
1110 virtual Bexpression*
1111 do_get_backend(Translate_context*) = 0;
1113 // Child class implements export.
1114 virtual void
1115 do_export(Export*) const;
1117 // For children to call to give an error for an unused value.
1118 void
1119 unused_value_error();
1121 // For children to call when they detect that they are in error.
1122 void
1123 set_is_error();
1125 // For children to call to report an error conveniently.
1126 void
1127 report_error(const char*);
1129 // Child class implements dumping to a dump context.
1130 virtual void
1131 do_dump_expression(Ast_dump_context*) const = 0;
1133 // Varargs lowering creates a slice object (unnamed compiler temp)
1134 // to contain the variable length collection of values. The enum
1135 // below tells the lowering routine whether it can mark that temp
1136 // as non-escaping or not. For general varargs calls it is not always
1137 // safe to stack-allocated the storage, but for specific cases (ex:
1138 // call to append()) it is legal.
1139 enum Slice_storage_escape_disp
1141 SLICE_STORAGE_MAY_ESCAPE,
1142 SLICE_STORAGE_DOES_NOT_ESCAPE
1145 private:
1146 // Convert to the desired statement classification, or return NULL.
1147 // This is a controlled dynamic cast.
1148 template<typename Expression_class,
1149 Expression_classification expr_classification>
1150 Expression_class*
1151 convert()
1153 return (this->classification_ == expr_classification
1154 ? static_cast<Expression_class*>(this)
1155 : NULL);
1158 template<typename Expression_class,
1159 Expression_classification expr_classification>
1160 const Expression_class*
1161 convert() const
1163 return (this->classification_ == expr_classification
1164 ? static_cast<const Expression_class*>(this)
1165 : NULL);
1168 static Expression*
1169 convert_type_to_interface(Type*, Expression*, Location);
1171 static Expression*
1172 get_interface_type_descriptor(Expression*);
1174 static Expression*
1175 convert_interface_to_type(Type*, Expression*, Location);
1177 // The expression classification.
1178 Expression_classification classification_;
1179 // The location in the input file.
1180 Location location_;
1183 // A list of Expressions.
1185 class Expression_list
1187 public:
1188 Expression_list()
1189 : entries_()
1192 // Return whether the list is empty.
1193 bool
1194 empty() const
1195 { return this->entries_.empty(); }
1197 // Return the number of entries in the list.
1198 size_t
1199 size() const
1200 { return this->entries_.size(); }
1202 // Add an entry to the end of the list.
1203 void
1204 push_back(Expression* expr)
1205 { this->entries_.push_back(expr); }
1207 void
1208 append(Expression_list* add)
1209 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
1211 // Reserve space in the list.
1212 void
1213 reserve(size_t size)
1214 { this->entries_.reserve(size); }
1216 // Traverse the expressions in the list.
1218 traverse(Traverse*);
1220 // Copy the list.
1221 Expression_list*
1222 copy();
1224 // Return true if the list contains an error expression.
1225 bool
1226 contains_error() const;
1228 // Retrieve an element by index.
1229 Expression*&
1230 at(size_t i)
1231 { return this->entries_.at(i); }
1233 // Return the first and last elements.
1234 Expression*&
1235 front()
1236 { return this->entries_.front(); }
1238 Expression*
1239 front() const
1240 { return this->entries_.front(); }
1242 Expression*&
1243 back()
1244 { return this->entries_.back(); }
1246 Expression*
1247 back() const
1248 { return this->entries_.back(); }
1250 // Iterators.
1252 typedef std::vector<Expression*>::iterator iterator;
1253 typedef std::vector<Expression*>::const_iterator const_iterator;
1255 iterator
1256 begin()
1257 { return this->entries_.begin(); }
1259 const_iterator
1260 begin() const
1261 { return this->entries_.begin(); }
1263 iterator
1264 end()
1265 { return this->entries_.end(); }
1267 const_iterator
1268 end() const
1269 { return this->entries_.end(); }
1271 // Erase an entry.
1272 void
1273 erase(iterator p)
1274 { this->entries_.erase(p); }
1276 private:
1277 std::vector<Expression*> entries_;
1280 // An abstract base class for an expression which is only used by the
1281 // parser, and is lowered in the lowering pass.
1283 class Parser_expression : public Expression
1285 public:
1286 Parser_expression(Expression_classification classification,
1287 Location location)
1288 : Expression(classification, location)
1291 protected:
1292 virtual Expression*
1293 do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
1295 Type*
1296 do_type();
1298 void
1299 do_determine_type(const Type_context*)
1300 { go_unreachable(); }
1302 void
1303 do_check_types(Gogo*)
1304 { go_unreachable(); }
1306 Bexpression*
1307 do_get_backend(Translate_context*)
1308 { go_unreachable(); }
1311 // An expression which is simply a variable.
1313 class Var_expression : public Expression
1315 public:
1316 Var_expression(Named_object* variable, Location location)
1317 : Expression(EXPRESSION_VAR_REFERENCE, location),
1318 variable_(variable)
1321 // Return the variable.
1322 Named_object*
1323 named_object() const
1324 { return this->variable_; }
1326 protected:
1327 Expression*
1328 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1330 Type*
1331 do_type();
1333 void
1334 do_determine_type(const Type_context*);
1336 Expression*
1337 do_copy()
1338 { return this; }
1340 bool
1341 do_is_addressable() const
1342 { return true; }
1344 void
1345 do_address_taken(bool);
1347 Bexpression*
1348 do_get_backend(Translate_context*);
1350 void
1351 do_dump_expression(Ast_dump_context*) const;
1353 private:
1354 // The variable we are referencing.
1355 Named_object* variable_;
1358 // A reference to a variable within an enclosing function.
1360 class Enclosed_var_expression : public Expression
1362 public:
1363 Enclosed_var_expression(Expression* reference, Named_object* variable,
1364 Location location)
1365 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE, location),
1366 reference_(reference), variable_(variable)
1369 // The reference to the enclosed variable. This will be an indirection of the
1370 // the field stored within closure variable.
1371 Expression*
1372 reference() const
1373 { return this->reference_; }
1375 // The variable being enclosed and referenced.
1376 Named_object*
1377 variable() const
1378 { return this->variable_; }
1380 protected:
1382 do_traverse(Traverse*);
1384 Expression*
1385 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1387 Expression*
1388 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1390 Type*
1391 do_type()
1392 { return this->reference_->type(); }
1394 void
1395 do_determine_type(const Type_context* context)
1396 { return this->reference_->determine_type(context); }
1398 Expression*
1399 do_copy()
1400 { return this; }
1402 bool
1403 do_is_addressable() const
1404 { return this->reference_->is_addressable(); }
1406 void
1407 do_address_taken(bool escapes);
1409 Bexpression*
1410 do_get_backend(Translate_context* context)
1411 { return this->reference_->get_backend(context); }
1413 void
1414 do_dump_expression(Ast_dump_context*) const;
1416 private:
1417 // The reference to the enclosed variable.
1418 Expression* reference_;
1419 // The variable being enclosed.
1420 Named_object* variable_;
1423 // A reference to a temporary variable.
1425 class Temporary_reference_expression : public Expression
1427 public:
1428 Temporary_reference_expression(Temporary_statement* statement,
1429 Location location)
1430 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1431 statement_(statement), is_lvalue_(false)
1434 // The temporary that this expression refers to.
1435 Temporary_statement*
1436 statement() const
1437 { return this->statement_; }
1439 // Indicate that this reference appears on the left hand side of an
1440 // assignment statement.
1441 void
1442 set_is_lvalue()
1443 { this->is_lvalue_ = true; }
1445 protected:
1446 Type*
1447 do_type();
1449 void
1450 do_determine_type(const Type_context*)
1453 Expression*
1454 do_copy()
1455 { return make_temporary_reference(this->statement_, this->location()); }
1457 bool
1458 do_is_addressable() const
1459 { return true; }
1461 void
1462 do_address_taken(bool);
1464 Bexpression*
1465 do_get_backend(Translate_context*);
1467 void
1468 do_dump_expression(Ast_dump_context*) const;
1470 private:
1471 // The statement where the temporary variable is defined.
1472 Temporary_statement* statement_;
1473 // Whether this reference appears on the left hand side of an
1474 // assignment statement.
1475 bool is_lvalue_;
1478 // Set and use a temporary variable.
1480 class Set_and_use_temporary_expression : public Expression
1482 public:
1483 Set_and_use_temporary_expression(Temporary_statement* statement,
1484 Expression* expr, Location location)
1485 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1486 statement_(statement), expr_(expr)
1489 // Return the temporary.
1490 Temporary_statement*
1491 temporary() const
1492 { return this->statement_; }
1494 // Return the expression.
1495 Expression*
1496 expression() const
1497 { return this->expr_; }
1499 protected:
1501 do_traverse(Traverse* traverse)
1502 { return Expression::traverse(&this->expr_, traverse); }
1504 Type*
1505 do_type();
1507 void
1508 do_determine_type(const Type_context*);
1510 Expression*
1511 do_copy()
1513 return make_set_and_use_temporary(this->statement_, this->expr_,
1514 this->location());
1517 bool
1518 do_is_addressable() const
1519 { return true; }
1521 void
1522 do_address_taken(bool);
1524 Bexpression*
1525 do_get_backend(Translate_context*);
1527 void
1528 do_dump_expression(Ast_dump_context*) const;
1530 private:
1531 // The statement where the temporary variable is defined.
1532 Temporary_statement* statement_;
1533 // The expression to assign to the temporary.
1534 Expression* expr_;
1537 // A string expression.
1539 class String_expression : public Expression
1541 public:
1542 String_expression(const std::string& val, Location location)
1543 : Expression(EXPRESSION_STRING, location),
1544 val_(val), type_(NULL)
1547 const std::string&
1548 val() const
1549 { return this->val_; }
1551 static Expression*
1552 do_import(Import*);
1554 protected:
1555 bool
1556 do_is_constant() const
1557 { return true; }
1559 bool
1560 do_is_static_initializer() const
1561 { return true; }
1563 bool
1564 do_string_constant_value(std::string* val) const
1566 *val = this->val_;
1567 return true;
1570 Type*
1571 do_type();
1573 void
1574 do_determine_type(const Type_context*);
1576 Expression*
1577 do_copy()
1578 { return this; }
1580 Bexpression*
1581 do_get_backend(Translate_context*);
1583 // Write string literal to a string dump.
1584 static void
1585 export_string(String_dump* exp, const String_expression* str);
1587 void
1588 do_export(Export*) const;
1590 void
1591 do_dump_expression(Ast_dump_context*) const;
1593 private:
1594 // The string value. This is immutable.
1595 const std::string val_;
1596 // The type as determined by context.
1597 Type* type_;
1600 // A type conversion expression.
1602 class Type_conversion_expression : public Expression
1604 public:
1605 Type_conversion_expression(Type* type, Expression* expr,
1606 Location location)
1607 : Expression(EXPRESSION_CONVERSION, location),
1608 type_(type), expr_(expr), may_convert_function_types_(false)
1611 // Return the type to which we are converting.
1612 Type*
1613 type() const
1614 { return this->type_; }
1616 // Return the expression which we are converting.
1617 Expression*
1618 expr() const
1619 { return this->expr_; }
1621 // Permit converting from one function type to another. This is
1622 // used internally for method expressions.
1623 void
1624 set_may_convert_function_types()
1626 this->may_convert_function_types_ = true;
1629 // Import a type conversion expression.
1630 static Expression*
1631 do_import(Import*);
1633 protected:
1635 do_traverse(Traverse* traverse);
1637 Expression*
1638 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1640 Expression*
1641 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1643 bool
1644 do_is_constant() const;
1646 bool
1647 do_is_static_initializer() const;
1649 bool
1650 do_numeric_constant_value(Numeric_constant*) const;
1652 bool
1653 do_string_constant_value(std::string*) const;
1655 Type*
1656 do_type()
1657 { return this->type_; }
1659 void
1660 do_determine_type(const Type_context*);
1662 void
1663 do_check_types(Gogo*);
1665 Expression*
1666 do_copy()
1668 return new Type_conversion_expression(this->type_, this->expr_->copy(),
1669 this->location());
1672 Bexpression*
1673 do_get_backend(Translate_context* context);
1675 void
1676 do_export(Export*) const;
1678 void
1679 do_dump_expression(Ast_dump_context*) const;
1681 private:
1682 // The type to convert to.
1683 Type* type_;
1684 // The expression to convert.
1685 Expression* expr_;
1686 // True if this is permitted to convert function types. This is
1687 // used internally for method expressions.
1688 bool may_convert_function_types_;
1691 // An unsafe type conversion, used to pass values to builtin functions.
1693 class Unsafe_type_conversion_expression : public Expression
1695 public:
1696 Unsafe_type_conversion_expression(Type* type, Expression* expr,
1697 Location location)
1698 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
1699 type_(type), expr_(expr)
1702 Expression*
1703 expr() const
1704 { return this->expr_; }
1706 protected:
1708 do_traverse(Traverse* traverse);
1710 bool
1711 do_is_static_initializer() const;
1713 Type*
1714 do_type()
1715 { return this->type_; }
1717 void
1718 do_determine_type(const Type_context*)
1719 { this->expr_->determine_type_no_context(); }
1721 Expression*
1722 do_copy()
1724 return new Unsafe_type_conversion_expression(this->type_,
1725 this->expr_->copy(),
1726 this->location());
1729 Bexpression*
1730 do_get_backend(Translate_context*);
1732 void
1733 do_dump_expression(Ast_dump_context*) const;
1735 private:
1736 // The type to convert to.
1737 Type* type_;
1738 // The expression to convert.
1739 Expression* expr_;
1742 // A Unary expression.
1744 class Unary_expression : public Expression
1746 public:
1747 Unary_expression(Operator op, Expression* expr, Location location)
1748 : Expression(EXPRESSION_UNARY, location),
1749 op_(op), escapes_(true), create_temp_(false), is_gc_root_(false),
1750 is_slice_init_(false), expr_(expr),
1751 issue_nil_check_(NIL_CHECK_DEFAULT)
1754 // Return the operator.
1755 Operator
1756 op() const
1757 { return this->op_; }
1759 // Return the operand.
1760 Expression*
1761 operand() const
1762 { return this->expr_; }
1764 // Record that an address expression does not escape.
1765 void
1766 set_does_not_escape()
1768 go_assert(this->op_ == OPERATOR_AND);
1769 this->escapes_ = false;
1772 // Record that this is an address expression which should create a
1773 // temporary variable if necessary. This is used for method calls.
1774 void
1775 set_create_temp()
1777 go_assert(this->op_ == OPERATOR_AND);
1778 this->create_temp_ = true;
1781 // Record that this is an address expression of a GC root, which is a
1782 // mutable composite literal. This used for registering GC variables.
1783 void
1784 set_is_gc_root()
1786 go_assert(this->op_ == OPERATOR_AND);
1787 this->is_gc_root_ = true;
1790 // Record that this is an address expression of a slice value initializer,
1791 // which is mutable if the values are not copied to the heap.
1792 void
1793 set_is_slice_init()
1795 go_assert(this->op_ == OPERATOR_AND);
1796 this->is_slice_init_ = true;
1799 // Call the address_taken method on the operand if necessary.
1800 void
1801 check_operand_address_taken(Gogo*);
1803 // Apply unary opcode OP to UNC, setting NC. Return true if this
1804 // could be done, false if not. On overflow, issues an error and
1805 // sets *ISSUED_ERROR.
1806 static bool
1807 eval_constant(Operator op, const Numeric_constant* unc,
1808 Location, Numeric_constant* nc, bool *issued_error);
1810 static Expression*
1811 do_import(Import*);
1813 // Declare that this deref does or does not require an explicit nil check.
1814 void
1815 set_requires_nil_check(bool needed)
1817 go_assert(this->op_ == OPERATOR_MULT);
1818 if (needed)
1819 this->issue_nil_check_ = NIL_CHECK_NEEDED;
1820 else
1821 this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED;
1824 protected:
1826 do_traverse(Traverse* traverse)
1827 { return Expression::traverse(&this->expr_, traverse); }
1829 Expression*
1830 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1832 Expression*
1833 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1835 bool
1836 do_is_constant() const;
1838 bool
1839 do_is_static_initializer() const;
1841 bool
1842 do_numeric_constant_value(Numeric_constant*) const;
1844 Type*
1845 do_type();
1847 void
1848 do_determine_type(const Type_context*);
1850 void
1851 do_check_types(Gogo*);
1853 Expression*
1854 do_copy()
1856 return Expression::make_unary(this->op_, this->expr_->copy(),
1857 this->location());
1860 bool
1861 do_must_eval_subexpressions_in_order(int*) const
1862 { return this->op_ == OPERATOR_MULT; }
1864 bool
1865 do_is_addressable() const
1866 { return this->op_ == OPERATOR_MULT; }
1868 Bexpression*
1869 do_get_backend(Translate_context*);
1871 void
1872 do_export(Export*) const;
1874 void
1875 do_dump_expression(Ast_dump_context*) const;
1877 void
1878 do_issue_nil_check()
1880 if (this->op_ == OPERATOR_MULT)
1881 this->set_requires_nil_check(true);
1884 private:
1885 static bool
1886 base_is_static_initializer(Expression*);
1888 // Return a determination as to whether this dereference expression
1889 // requires a nil check.
1890 Nil_check_classification
1891 requires_nil_check(Gogo*);
1893 // The unary operator to apply.
1894 Operator op_;
1895 // Normally true. False if this is an address expression which does
1896 // not escape the current function.
1897 bool escapes_;
1898 // True if this is an address expression which should create a
1899 // temporary variable if necessary.
1900 bool create_temp_;
1901 // True if this is an address expression for a GC root. A GC root is a
1902 // special struct composite literal that is mutable when addressed, meaning
1903 // it cannot be represented as an immutable_struct in the backend.
1904 bool is_gc_root_;
1905 // True if this is an address expression for a slice value with an immutable
1906 // initializer. The initializer for a slice's value pointer has an array
1907 // type, meaning it cannot be represented as an immutable_struct in the
1908 // backend.
1909 bool is_slice_init_;
1910 // The operand.
1911 Expression* expr_;
1912 // Whether or not to issue a nil check for this expression if its address
1913 // is being taken.
1914 Nil_check_classification issue_nil_check_;
1917 // A binary expression.
1919 class Binary_expression : public Expression
1921 public:
1922 Binary_expression(Operator op, Expression* left, Expression* right,
1923 Location location)
1924 : Expression(EXPRESSION_BINARY, location),
1925 op_(op), left_(left), right_(right), type_(NULL)
1928 // Return the operator.
1929 Operator
1930 op()
1931 { return this->op_; }
1933 // Return the left hand expression.
1934 Expression*
1935 left()
1936 { return this->left_; }
1938 // Return the right hand expression.
1939 Expression*
1940 right()
1941 { return this->right_; }
1943 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1944 // Return true if this could be done, false if not. Issue errors at
1945 // LOCATION as appropriate, and sets *ISSUED_ERROR if it did.
1946 static bool
1947 eval_constant(Operator op, Numeric_constant* left_nc,
1948 Numeric_constant* right_nc, Location location,
1949 Numeric_constant* nc, bool* issued_error);
1951 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1952 // *RESULT. Return true if this could be done, false if not. Issue
1953 // errors at LOCATION as appropriate.
1954 static bool
1955 compare_constant(Operator op, Numeric_constant* left_nc,
1956 Numeric_constant* right_nc, Location location,
1957 bool* result);
1959 static Expression*
1960 do_import(Import*);
1962 // Report an error if OP can not be applied to TYPE. Return whether
1963 // it can. OTYPE is the type of the other operand.
1964 static bool
1965 check_operator_type(Operator op, Type* type, Type* otype, Location);
1967 // Set *RESULT_TYPE to the resulting type when OP is applied to
1968 // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
1969 // success, false on failure.
1970 static bool
1971 operation_type(Operator op, Type* left_type, Type* right_type,
1972 Type** result_type);
1974 protected:
1976 do_traverse(Traverse* traverse);
1978 Expression*
1979 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1981 Expression*
1982 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1984 bool
1985 do_is_constant() const
1986 { return this->left_->is_constant() && this->right_->is_constant(); }
1988 bool
1989 do_is_static_initializer() const;
1991 bool
1992 do_numeric_constant_value(Numeric_constant*) const;
1994 bool
1995 do_discarding_value();
1997 Type*
1998 do_type();
2000 void
2001 do_determine_type(const Type_context*);
2003 void
2004 do_check_types(Gogo*);
2006 Expression*
2007 do_copy()
2009 return Expression::make_binary(this->op_, this->left_->copy(),
2010 this->right_->copy(), this->location());
2013 Bexpression*
2014 do_get_backend(Translate_context*);
2016 void
2017 do_export(Export*) const;
2019 void
2020 do_dump_expression(Ast_dump_context*) const;
2022 private:
2023 static bool
2024 cmp_to_bool(Operator op, int cmp);
2026 static bool
2027 eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
2028 Location, Numeric_constant*);
2030 static bool
2031 eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
2032 Location, Numeric_constant*);
2034 static bool
2035 eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
2036 Location, Numeric_constant*);
2038 static bool
2039 compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
2041 static bool
2042 compare_float(const Numeric_constant*, const Numeric_constant *, int*);
2044 static bool
2045 compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
2047 Expression*
2048 lower_struct_comparison(Gogo*, Statement_inserter*);
2050 Expression*
2051 lower_array_comparison(Gogo*, Statement_inserter*);
2053 Expression*
2054 lower_interface_value_comparison(Gogo*, Statement_inserter*);
2056 Expression*
2057 lower_compare_to_memcmp(Gogo*, Statement_inserter*);
2059 Expression*
2060 operand_address(Statement_inserter*, Expression*);
2062 // The binary operator to apply.
2063 Operator op_;
2064 // The left hand side operand.
2065 Expression* left_;
2066 // The right hand side operand.
2067 Expression* right_;
2068 // The type of a comparison operation.
2069 Type* type_;
2072 // A string concatenation expression. This is a sequence of strings
2073 // added together. It is created when lowering Binary_expression.
2075 class String_concat_expression : public Expression
2077 public:
2078 String_concat_expression(Expression_list* exprs)
2079 : Expression(EXPRESSION_STRING_CONCAT, exprs->front()->location()),
2080 exprs_(exprs)
2083 // Return the list of string expressions to be concatenated.
2084 Expression_list*
2085 exprs()
2086 { return this->exprs_; }
2088 protected:
2090 do_traverse(Traverse* traverse)
2091 { return this->exprs_->traverse(traverse); }
2093 Expression*
2094 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2095 { return this; }
2097 Expression*
2098 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2100 bool
2101 do_is_constant() const;
2103 bool
2104 do_is_static_initializer() const;
2106 Type*
2107 do_type();
2109 void
2110 do_determine_type(const Type_context*);
2112 void
2113 do_check_types(Gogo*);
2115 Expression*
2116 do_copy()
2117 { return Expression::make_string_concat(this->exprs_->copy()); }
2119 Bexpression*
2120 do_get_backend(Translate_context*)
2121 { go_unreachable(); }
2123 void
2124 do_export(Export*) const
2125 { go_unreachable(); }
2127 void
2128 do_dump_expression(Ast_dump_context*) const;
2130 private:
2131 // The string expressions to concatenate.
2132 Expression_list* exprs_;
2135 // A call expression. The go statement needs to dig inside this.
2137 class Call_expression : public Expression
2139 public:
2140 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
2141 Location location)
2142 : Expression(EXPRESSION_CALL, location),
2143 fn_(fn), args_(args), type_(NULL), call_(NULL), call_temp_(NULL)
2144 , expected_result_count_(0), is_varargs_(is_varargs),
2145 varargs_are_lowered_(false), types_are_determined_(false),
2146 is_deferred_(false), is_concurrent_(false), issued_error_(false),
2147 is_multi_value_arg_(false), is_flattened_(false)
2150 // The function to call.
2151 Expression*
2152 fn() const
2153 { return this->fn_; }
2155 // The arguments.
2156 Expression_list*
2157 args()
2158 { return this->args_; }
2160 const Expression_list*
2161 args() const
2162 { return this->args_; }
2164 // Get the function type.
2165 Function_type*
2166 get_function_type() const;
2168 // Return the number of values this call will return.
2169 size_t
2170 result_count() const;
2172 // Return the temporary variable that holds the results. This is
2173 // only valid after the expression has been lowered, and is only
2174 // valid for calls which return multiple results.
2175 Temporary_statement*
2176 results() const;
2178 // Set the number of results expected from this call. This is used
2179 // when the call appears in a context that expects multiple results,
2180 // such as a, b = f().
2181 void
2182 set_expected_result_count(size_t);
2184 // Return whether this is a call to the predeclared function
2185 // recover.
2186 bool
2187 is_recover_call() const;
2189 // Set the argument for a call to recover.
2190 void
2191 set_recover_arg(Expression*);
2193 // Whether the last argument is a varargs argument (f(a...)).
2194 bool
2195 is_varargs() const
2196 { return this->is_varargs_; }
2198 // Return whether varargs have already been lowered.
2199 bool
2200 varargs_are_lowered() const
2201 { return this->varargs_are_lowered_; }
2203 // Note that varargs have already been lowered.
2204 void
2205 set_varargs_are_lowered()
2206 { this->varargs_are_lowered_ = true; }
2208 // Whether this call is being deferred.
2209 bool
2210 is_deferred() const
2211 { return this->is_deferred_; }
2213 // Note that the call is being deferred.
2214 void
2215 set_is_deferred()
2216 { this->is_deferred_ = true; }
2218 // Whether this call is concurrently executed.
2219 bool
2220 is_concurrent() const
2221 { return this->is_concurrent_; }
2223 // Note that the call is concurrently executed.
2224 void
2225 set_is_concurrent()
2226 { this->is_concurrent_ = true; }
2228 // We have found an error with this call expression; return true if
2229 // we should report it.
2230 bool
2231 issue_error();
2233 // Whether or not this call contains errors, either in the call or the
2234 // arguments to the call.
2235 bool
2236 is_erroneous_call();
2238 // Whether this call returns multiple results that are used as an
2239 // multi-valued argument.
2240 bool
2241 is_multi_value_arg() const
2242 { return this->is_multi_value_arg_; }
2244 // Note this call is used as a multi-valued argument.
2245 void
2246 set_is_multi_value_arg()
2247 { this->is_multi_value_arg_ = true; }
2249 protected:
2251 do_traverse(Traverse*);
2253 virtual Expression*
2254 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2256 virtual Expression*
2257 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2259 bool
2260 do_discarding_value()
2261 { return true; }
2263 virtual Type*
2264 do_type();
2266 virtual void
2267 do_determine_type(const Type_context*);
2269 virtual void
2270 do_check_types(Gogo*);
2272 Expression*
2273 do_copy();
2275 bool
2276 do_must_eval_in_order() const;
2278 virtual Bexpression*
2279 do_get_backend(Translate_context*);
2281 virtual bool
2282 do_is_recover_call() const;
2284 virtual void
2285 do_set_recover_arg(Expression*);
2287 // Let a builtin expression change the argument list.
2288 void
2289 set_args(Expression_list* args)
2290 { this->args_ = args; }
2292 // Let a builtin expression lower varargs.
2293 void
2294 lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
2295 Type* varargs_type, size_t param_count,
2296 Slice_storage_escape_disp escape_disp);
2298 // Let a builtin expression check whether types have been
2299 // determined.
2300 bool
2301 determining_types();
2303 void
2304 do_dump_expression(Ast_dump_context*) const;
2306 private:
2307 bool
2308 check_argument_type(int, const Type*, const Type*, Location, bool);
2310 Expression*
2311 lower_to_builtin(Named_object**, const char*, int);
2313 Expression*
2314 interface_method_function(Interface_field_reference_expression*,
2315 Expression**, Location);
2317 Bexpression*
2318 set_results(Translate_context*);
2320 // The function to call.
2321 Expression* fn_;
2322 // The arguments to pass. This may be NULL if there are no
2323 // arguments.
2324 Expression_list* args_;
2325 // The type of the expression, to avoid recomputing it.
2326 Type* type_;
2327 // The backend expression for the call, used for a call which returns a tuple.
2328 Bexpression* call_;
2329 // A temporary variable to store this call if the function returns a tuple.
2330 Temporary_statement* call_temp_;
2331 // If not 0, the number of results expected from this call, when
2332 // used in a context that expects multiple values.
2333 size_t expected_result_count_;
2334 // True if the last argument is a varargs argument (f(a...)).
2335 bool is_varargs_;
2336 // True if varargs have already been lowered.
2337 bool varargs_are_lowered_;
2338 // True if types have been determined.
2339 bool types_are_determined_;
2340 // True if the call is an argument to a defer statement.
2341 bool is_deferred_;
2342 // True if the call is an argument to a go statement.
2343 bool is_concurrent_;
2344 // True if we reported an error about a mismatch between call
2345 // results and uses. This is to avoid producing multiple errors
2346 // when there are multiple Call_result_expressions.
2347 bool issued_error_;
2348 // True if this call is used as an argument that returns multiple results.
2349 bool is_multi_value_arg_;
2350 // True if this expression has already been flattened.
2351 bool is_flattened_;
2354 // A single result from a call which returns multiple results.
2356 class Call_result_expression : public Expression
2358 public:
2359 Call_result_expression(Call_expression* call, unsigned int index)
2360 : Expression(EXPRESSION_CALL_RESULT, call->location()),
2361 call_(call), index_(index)
2364 Expression*
2365 call() const
2366 { return this->call_; }
2368 unsigned int
2369 index() const
2370 { return this->index_; }
2372 protected:
2374 do_traverse(Traverse*);
2376 Type*
2377 do_type();
2379 void
2380 do_determine_type(const Type_context*);
2382 void
2383 do_check_types(Gogo*);
2385 Expression*
2386 do_copy()
2388 return new Call_result_expression(this->call_->call_expression(),
2389 this->index_);
2392 bool
2393 do_must_eval_in_order() const
2394 { return true; }
2396 Bexpression*
2397 do_get_backend(Translate_context*);
2399 void
2400 do_dump_expression(Ast_dump_context*) const;
2402 private:
2403 // The underlying call expression.
2404 Expression* call_;
2405 // Which result we want.
2406 unsigned int index_;
2409 // An expression which represents a pointer to a function.
2411 class Func_expression : public Expression
2413 public:
2414 Func_expression(Named_object* function, Expression* closure,
2415 Location location)
2416 : Expression(EXPRESSION_FUNC_REFERENCE, location),
2417 function_(function), closure_(closure),
2418 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS)
2421 // Return the object associated with the function.
2422 Named_object*
2423 named_object() const
2424 { return this->function_; }
2426 // Return the closure for this function. This will return NULL if
2427 // the function has no closure, which is the normal case.
2428 Expression*
2429 closure()
2430 { return this->closure_; }
2432 // Return whether this is a reference to a runtime function.
2433 bool
2434 is_runtime_function() const
2435 { return this->runtime_code_ != Runtime::NUMBER_OF_FUNCTIONS; }
2437 // Return the runtime code for this function expression.
2438 // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
2439 // runtime function.
2440 Runtime::Function
2441 runtime_code() const
2442 { return this->runtime_code_; }
2444 // Set the runtime code for this function expression.
2445 void
2446 set_runtime_code(Runtime::Function code)
2447 { this->runtime_code_ = code; }
2449 // Return a backend expression for the code of a function.
2450 static Bexpression*
2451 get_code_pointer(Gogo*, Named_object* function, Location loc);
2453 protected:
2455 do_traverse(Traverse*);
2457 Type*
2458 do_type();
2460 void
2461 do_determine_type(const Type_context*)
2463 if (this->closure_ != NULL)
2464 this->closure_->determine_type_no_context();
2467 Expression*
2468 do_copy()
2470 return Expression::make_func_reference(this->function_,
2471 (this->closure_ == NULL
2472 ? NULL
2473 : this->closure_->copy()),
2474 this->location());
2477 Bexpression*
2478 do_get_backend(Translate_context*);
2480 void
2481 do_dump_expression(Ast_dump_context*) const;
2483 private:
2484 // The function itself.
2485 Named_object* function_;
2486 // A closure. This is normally NULL. For a nested function, it may
2487 // be a struct holding pointers to all the variables referenced by
2488 // this function and defined in enclosing functions.
2489 Expression* closure_;
2490 // The runtime code for the referenced function.
2491 Runtime::Function runtime_code_;
2494 // A function descriptor. A function descriptor is a struct with a
2495 // single field pointing to the function code. This is used for
2496 // functions without closures.
2498 class Func_descriptor_expression : public Expression
2500 public:
2501 Func_descriptor_expression(Named_object* fn);
2503 // Make the function descriptor type, so that it can be converted.
2504 static void
2505 make_func_descriptor_type();
2507 protected:
2509 do_traverse(Traverse*);
2511 Type*
2512 do_type();
2514 void
2515 do_determine_type(const Type_context*)
2518 Expression*
2519 do_copy()
2520 { return Expression::make_func_descriptor(this->fn_); }
2522 bool
2523 do_is_addressable() const
2524 { return true; }
2526 Bexpression*
2527 do_get_backend(Translate_context*);
2529 void
2530 do_dump_expression(Ast_dump_context* context) const;
2532 private:
2533 // The type of all function descriptors.
2534 static Type* descriptor_type;
2536 // The function for which this is the descriptor.
2537 Named_object* fn_;
2538 // The descriptor variable.
2539 Bvariable* dvar_;
2542 // A reference to an unknown name.
2544 class Unknown_expression : public Parser_expression
2546 public:
2547 Unknown_expression(Named_object* named_object, Location location)
2548 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
2549 named_object_(named_object), no_error_message_(false),
2550 is_composite_literal_key_(false)
2553 // The associated named object.
2554 Named_object*
2555 named_object() const
2556 { return this->named_object_; }
2558 // The name of the identifier which was unknown.
2559 const std::string&
2560 name() const;
2562 // Call this to indicate that we should not give an error if this
2563 // name is never defined. This is used to avoid knock-on errors
2564 // during an erroneous parse.
2565 void
2566 set_no_error_message()
2567 { this->no_error_message_ = true; }
2569 // Note that this expression is being used as the key in a composite
2570 // literal, so it may be OK if it is not resolved.
2571 void
2572 set_is_composite_literal_key()
2573 { this->is_composite_literal_key_ = true; }
2575 // Note that this expression should no longer be treated as a
2576 // composite literal key.
2577 void
2578 clear_is_composite_literal_key()
2579 { this->is_composite_literal_key_ = false; }
2581 protected:
2582 Expression*
2583 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2585 Expression*
2586 do_copy()
2587 { return new Unknown_expression(this->named_object_, this->location()); }
2589 void
2590 do_dump_expression(Ast_dump_context*) const;
2592 private:
2593 // The unknown name.
2594 Named_object* named_object_;
2595 // True if we should not give errors if this is undefined. This is
2596 // used if there was a parse failure.
2597 bool no_error_message_;
2598 // True if this is the key in a composite literal.
2599 bool is_composite_literal_key_;
2602 // An index expression. This is lowered to an array index, a string
2603 // index, or a map index.
2605 class Index_expression : public Parser_expression
2607 public:
2608 Index_expression(Expression* left, Expression* start, Expression* end,
2609 Expression* cap, Location location)
2610 : Parser_expression(EXPRESSION_INDEX, location),
2611 left_(left), start_(start), end_(end), cap_(cap)
2614 // Dump an index expression, i.e. an expression of the form
2615 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
2616 static void
2617 dump_index_expression(Ast_dump_context*, const Expression* expr,
2618 const Expression* start, const Expression* end,
2619 const Expression* cap);
2621 protected:
2623 do_traverse(Traverse*);
2625 Expression*
2626 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2628 Expression*
2629 do_copy()
2631 return new Index_expression(this->left_->copy(), this->start_->copy(),
2632 (this->end_ == NULL
2633 ? NULL
2634 : this->end_->copy()),
2635 (this->cap_ == NULL
2636 ? NULL
2637 : this->cap_->copy()),
2638 this->location());
2641 bool
2642 do_must_eval_subexpressions_in_order(int* skip) const
2644 *skip = 1;
2645 return true;
2648 void
2649 do_dump_expression(Ast_dump_context*) const;
2651 void
2652 do_issue_nil_check()
2653 { this->left_->issue_nil_check(); }
2654 private:
2655 // The expression being indexed.
2656 Expression* left_;
2657 // The first index.
2658 Expression* start_;
2659 // The second index. This is NULL for an index, non-NULL for a
2660 // slice.
2661 Expression* end_;
2662 // The capacity argument. This is NULL for indices and slices that use the
2663 // default capacity, non-NULL for indices and slices that specify the
2664 // capacity.
2665 Expression* cap_;
2668 // An array index. This is used for both indexing and slicing.
2670 class Array_index_expression : public Expression
2672 public:
2673 Array_index_expression(Expression* array, Expression* start,
2674 Expression* end, Expression* cap, Location location)
2675 : Expression(EXPRESSION_ARRAY_INDEX, location),
2676 array_(array), start_(start), end_(end), cap_(cap), type_(NULL),
2677 is_lvalue_(false)
2680 // Return the array.
2681 Expression*
2682 array()
2683 { return this->array_; }
2685 const Expression*
2686 array() const
2687 { return this->array_; }
2689 // Return the index of a simple index expression, or the start index
2690 // of a slice expression.
2691 Expression*
2692 start()
2693 { return this->start_; }
2695 const Expression*
2696 start() const
2697 { return this->start_; }
2699 // Return the end index of a slice expression. This is NULL for a
2700 // simple index expression.
2701 Expression*
2702 end()
2703 { return this->end_; }
2705 const Expression*
2706 end() const
2707 { return this->end_; }
2709 // Return whether this array index expression appears in an lvalue
2710 // (left hand side of assignment) context.
2711 bool
2712 is_lvalue() const
2713 { return this->is_lvalue_; }
2715 // Update this array index expression to indicate that it appears
2716 // in a left-hand-side or lvalue context.
2717 void
2718 set_is_lvalue()
2719 { this->is_lvalue_ = true; }
2721 protected:
2723 do_traverse(Traverse*);
2725 Expression*
2726 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2728 Type*
2729 do_type();
2731 void
2732 do_determine_type(const Type_context*);
2734 void
2735 do_check_types(Gogo*);
2737 Expression*
2738 do_copy()
2740 return Expression::make_array_index(this->array_->copy(),
2741 this->start_->copy(),
2742 (this->end_ == NULL
2743 ? NULL
2744 : this->end_->copy()),
2745 (this->cap_ == NULL
2746 ? NULL
2747 : this->cap_->copy()),
2748 this->location());
2751 bool
2752 do_must_eval_subexpressions_in_order(int* skip) const
2754 *skip = 1;
2755 return true;
2758 bool
2759 do_is_addressable() const;
2761 void
2762 do_address_taken(bool escapes)
2763 { this->array_->address_taken(escapes); }
2765 void
2766 do_issue_nil_check()
2767 { this->array_->issue_nil_check(); }
2769 Bexpression*
2770 do_get_backend(Translate_context*);
2772 void
2773 do_dump_expression(Ast_dump_context*) const;
2775 private:
2776 // The array we are getting a value from.
2777 Expression* array_;
2778 // The start or only index.
2779 Expression* start_;
2780 // The end index of a slice. This may be NULL for a simple array
2781 // index, or it may be a nil expression for the length of the array.
2782 Expression* end_;
2783 // The capacity argument of a slice. This may be NULL for an array index or
2784 // slice.
2785 Expression* cap_;
2786 // The type of the expression.
2787 Type* type_;
2788 // Whether expr appears in an lvalue context.
2789 bool is_lvalue_;
2792 // A string index. This is used for both indexing and slicing.
2794 class String_index_expression : public Expression
2796 public:
2797 String_index_expression(Expression* string, Expression* start,
2798 Expression* end, Location location)
2799 : Expression(EXPRESSION_STRING_INDEX, location),
2800 string_(string), start_(start), end_(end)
2803 // Return the string being indexed.
2804 Expression*
2805 string() const
2806 { return this->string_; }
2808 protected:
2810 do_traverse(Traverse*);
2812 Expression*
2813 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2815 Type*
2816 do_type();
2818 void
2819 do_determine_type(const Type_context*);
2821 void
2822 do_check_types(Gogo*);
2824 Expression*
2825 do_copy()
2827 return Expression::make_string_index(this->string_->copy(),
2828 this->start_->copy(),
2829 (this->end_ == NULL
2830 ? NULL
2831 : this->end_->copy()),
2832 this->location());
2835 bool
2836 do_must_eval_subexpressions_in_order(int* skip) const
2838 *skip = 1;
2839 return true;
2842 Bexpression*
2843 do_get_backend(Translate_context*);
2845 void
2846 do_dump_expression(Ast_dump_context*) const;
2848 private:
2849 // The string we are getting a value from.
2850 Expression* string_;
2851 // The start or only index.
2852 Expression* start_;
2853 // The end index of a slice. This may be NULL for a single index,
2854 // or it may be a nil expression for the length of the string.
2855 Expression* end_;
2858 // An index into a map.
2860 class Map_index_expression : public Expression
2862 public:
2863 Map_index_expression(Expression* map, Expression* index,
2864 Location location)
2865 : Expression(EXPRESSION_MAP_INDEX, location),
2866 map_(map), index_(index), value_pointer_(NULL)
2869 // Return the map.
2870 Expression*
2871 map()
2872 { return this->map_; }
2874 const Expression*
2875 map() const
2876 { return this->map_; }
2878 // Return the index.
2879 Expression*
2880 index()
2881 { return this->index_; }
2883 const Expression*
2884 index() const
2885 { return this->index_; }
2887 // Get the type of the map being indexed.
2888 Map_type*
2889 get_map_type() const;
2891 // Return an expression for the map index. This returns an
2892 // expression that evaluates to a pointer to a value in the map. If
2893 // the key is not present in the map, this will return a pointer to
2894 // the zero value.
2895 Expression*
2896 get_value_pointer(Gogo*);
2898 protected:
2900 do_traverse(Traverse*);
2902 Expression*
2903 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2905 Type*
2906 do_type();
2908 void
2909 do_determine_type(const Type_context*);
2911 void
2912 do_check_types(Gogo*);
2914 Expression*
2915 do_copy()
2917 return Expression::make_map_index(this->map_->copy(),
2918 this->index_->copy(),
2919 this->location());
2922 bool
2923 do_must_eval_subexpressions_in_order(int* skip) const
2925 *skip = 1;
2926 return true;
2929 // A map index expression is an lvalue but it is not addressable.
2931 Bexpression*
2932 do_get_backend(Translate_context*);
2934 void
2935 do_dump_expression(Ast_dump_context*) const;
2937 private:
2938 // The map we are looking into.
2939 Expression* map_;
2940 // The index.
2941 Expression* index_;
2942 // A pointer to the value at this index.
2943 Expression* value_pointer_;
2946 // An expression which represents a method bound to its first
2947 // argument.
2949 class Bound_method_expression : public Expression
2951 public:
2952 Bound_method_expression(Expression* expr, const Method *method,
2953 Named_object* function, Location location)
2954 : Expression(EXPRESSION_BOUND_METHOD, location),
2955 expr_(expr), expr_type_(NULL), method_(method), function_(function)
2958 // Return the object which is the first argument.
2959 Expression*
2960 first_argument()
2961 { return this->expr_; }
2963 // Return the implicit type of the first argument. This will be
2964 // non-NULL when using a method from an anonymous field without
2965 // using an explicit stub.
2966 Type*
2967 first_argument_type() const
2968 { return this->expr_type_; }
2970 // Return the method.
2971 const Method*
2972 method() const
2973 { return this->method_; }
2975 // Return the function to call.
2976 Named_object*
2977 function() const
2978 { return this->function_; }
2980 // Set the implicit type of the expression.
2981 void
2982 set_first_argument_type(Type* type)
2983 { this->expr_type_ = type; }
2985 // Create a thunk to call FUNCTION, for METHOD, when it is used as
2986 // part of a method value.
2987 static Named_object*
2988 create_thunk(Gogo*, const Method* method, Named_object* function);
2990 protected:
2992 do_traverse(Traverse*);
2994 Expression*
2995 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2997 Type*
2998 do_type();
3000 void
3001 do_determine_type(const Type_context*);
3003 void
3004 do_check_types(Gogo*);
3006 Expression*
3007 do_copy()
3009 return new Bound_method_expression(this->expr_->copy(), this->method_,
3010 this->function_, this->location());
3013 Bexpression*
3014 do_get_backend(Translate_context*)
3015 { go_unreachable(); }
3017 void
3018 do_dump_expression(Ast_dump_context*) const;
3020 private:
3021 // A mapping from method functions to the thunks we have created for
3022 // them.
3023 typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
3024 static Method_value_thunks method_value_thunks;
3026 // The object used to find the method. This is passed to the method
3027 // as the first argument.
3028 Expression* expr_;
3029 // The implicit type of the object to pass to the method. This is
3030 // NULL in the normal case, non-NULL when using a method from an
3031 // anonymous field which does not require a stub.
3032 Type* expr_type_;
3033 // The method.
3034 const Method* method_;
3035 // The function to call. This is not the same as
3036 // method_->named_object() when the method has a stub. This will be
3037 // the real function rather than the stub.
3038 Named_object* function_;
3041 // A reference to a field in a struct.
3043 class Field_reference_expression : public Expression
3045 public:
3046 Field_reference_expression(Expression* expr, unsigned int field_index,
3047 Location location)
3048 : Expression(EXPRESSION_FIELD_REFERENCE, location),
3049 expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
3052 // Return the struct expression.
3053 Expression*
3054 expr() const
3055 { return this->expr_; }
3057 // Return the field index.
3058 unsigned int
3059 field_index() const
3060 { return this->field_index_; }
3062 // Return whether this node was implied by an anonymous field.
3063 bool
3064 implicit() const
3065 { return this->implicit_; }
3067 void
3068 set_implicit(bool implicit)
3069 { this->implicit_ = implicit; }
3071 // Set the struct expression. This is used when parsing.
3072 void
3073 set_struct_expression(Expression* expr)
3075 go_assert(this->expr_ == NULL);
3076 this->expr_ = expr;
3079 protected:
3081 do_traverse(Traverse* traverse)
3082 { return Expression::traverse(&this->expr_, traverse); }
3084 Expression*
3085 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3087 Type*
3088 do_type();
3090 void
3091 do_determine_type(const Type_context*)
3092 { this->expr_->determine_type_no_context(); }
3094 void
3095 do_check_types(Gogo*);
3097 Expression*
3098 do_copy()
3100 return Expression::make_field_reference(this->expr_->copy(),
3101 this->field_index_,
3102 this->location());
3105 bool
3106 do_is_addressable() const
3107 { return this->expr_->is_addressable(); }
3109 void
3110 do_address_taken(bool escapes)
3111 { this->expr_->address_taken(escapes); }
3113 void
3114 do_issue_nil_check()
3115 { this->expr_->issue_nil_check(); }
3117 Bexpression*
3118 do_get_backend(Translate_context*);
3120 void
3121 do_dump_expression(Ast_dump_context*) const;
3123 private:
3124 // The expression we are looking into. This should have a type of
3125 // struct.
3126 Expression* expr_;
3127 // The zero-based index of the field we are retrieving.
3128 unsigned int field_index_;
3129 // Whether this node was emitted implicitly for an embedded field,
3130 // that is, expr_ is not the expr_ of the original user node.
3131 bool implicit_;
3132 // Whether we have already emitted a fieldtrack call.
3133 bool called_fieldtrack_;
3136 // A reference to a field of an interface.
3138 class Interface_field_reference_expression : public Expression
3140 public:
3141 Interface_field_reference_expression(Expression* expr,
3142 const std::string& name,
3143 Location location)
3144 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
3145 expr_(expr), name_(name)
3148 // Return the expression for the interface object.
3149 Expression*
3150 expr()
3151 { return this->expr_; }
3153 // Return the name of the method to call.
3154 const std::string&
3155 name() const
3156 { return this->name_; }
3158 // Create a thunk to call the method NAME in TYPE when it is used as
3159 // part of a method value.
3160 static Named_object*
3161 create_thunk(Gogo*, Interface_type* type, const std::string& name);
3163 // Return an expression for the pointer to the function to call.
3164 Expression*
3165 get_function();
3167 // Return an expression for the first argument to pass to the interface
3168 // function. This is the real object associated with the interface object.
3169 Expression*
3170 get_underlying_object();
3172 protected:
3174 do_traverse(Traverse* traverse);
3176 Expression*
3177 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3179 Type*
3180 do_type();
3182 void
3183 do_determine_type(const Type_context*);
3185 void
3186 do_check_types(Gogo*);
3188 Expression*
3189 do_copy()
3191 return Expression::make_interface_field_reference(this->expr_->copy(),
3192 this->name_,
3193 this->location());
3196 Bexpression*
3197 do_get_backend(Translate_context*);
3199 void
3200 do_dump_expression(Ast_dump_context*) const;
3202 private:
3203 // A mapping from interface types to a list of thunks we have
3204 // created for methods.
3205 typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
3206 typedef Unordered_map(Interface_type*, Method_thunks*)
3207 Interface_method_thunks;
3208 static Interface_method_thunks interface_method_thunks;
3210 // The expression for the interface object. This should have a type
3211 // of interface or pointer to interface.
3212 Expression* expr_;
3213 // The field we are retrieving--the name of the method.
3214 std::string name_;
3217 // Implement the builtin function new.
3219 class Allocation_expression : public Expression
3221 public:
3222 Allocation_expression(Type* type, Location location)
3223 : Expression(EXPRESSION_ALLOCATION, location),
3224 type_(type), allocate_on_stack_(false)
3227 void
3228 set_allocate_on_stack()
3229 { this->allocate_on_stack_ = true; }
3231 protected:
3233 do_traverse(Traverse*);
3235 Type*
3236 do_type();
3238 void
3239 do_determine_type(const Type_context*)
3242 void
3243 do_check_types(Gogo*);
3245 Expression*
3246 do_copy();
3248 Bexpression*
3249 do_get_backend(Translate_context*);
3251 void
3252 do_dump_expression(Ast_dump_context*) const;
3254 private:
3255 // The type we are allocating.
3256 Type* type_;
3257 // Whether or not this is a stack allocation.
3258 bool allocate_on_stack_;
3261 // A general composite literal. This is lowered to a type specific
3262 // version.
3264 class Composite_literal_expression : public Parser_expression
3266 public:
3267 Composite_literal_expression(Type* type, int depth, bool has_keys,
3268 Expression_list* vals, bool all_are_names,
3269 Location location)
3270 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
3271 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys),
3272 all_are_names_(all_are_names), key_path_(std::vector<bool>(depth))
3276 // Mark the DEPTH entry of KEY_PATH as containing a key.
3277 void
3278 update_key_path(size_t depth)
3280 go_assert(depth < this->key_path_.size());
3281 this->key_path_[depth] = true;
3284 protected:
3286 do_traverse(Traverse* traverse);
3288 Expression*
3289 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3291 Expression*
3292 do_copy()
3294 Composite_literal_expression *ret =
3295 new Composite_literal_expression(this->type_, this->depth_,
3296 this->has_keys_,
3297 (this->vals_ == NULL
3298 ? NULL
3299 : this->vals_->copy()),
3300 this->all_are_names_,
3301 this->location());
3302 ret->key_path_ = this->key_path_;
3303 return ret;
3306 void
3307 do_dump_expression(Ast_dump_context*) const;
3309 private:
3310 Expression*
3311 lower_struct(Gogo*, Type*);
3313 Expression*
3314 lower_array(Type*);
3316 Expression*
3317 make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
3319 Expression*
3320 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
3322 // The type of the composite literal.
3323 Type* type_;
3324 // The depth within a list of composite literals within a composite
3325 // literal, when the type is omitted.
3326 int depth_;
3327 // The values to put in the composite literal.
3328 Expression_list* vals_;
3329 // If this is true, then VALS_ is a list of pairs: a key and a
3330 // value. In an array initializer, a missing key will be NULL.
3331 bool has_keys_;
3332 // If this is true, then HAS_KEYS_ is true, and every key is a
3333 // simple identifier.
3334 bool all_are_names_;
3335 // A complement to DEPTH that indicates for each level starting from 0 to
3336 // DEPTH-1 whether or not this composite literal is nested inside of key or
3337 // a value. This is used to decide which type to use when given a map literal
3338 // with omitted key types.
3339 std::vector<bool> key_path_;
3342 // Helper/mixin class for struct and array construction expressions;
3343 // encapsulates a list of values plus an optional traversal order
3344 // recording the order in which the values should be visited.
3346 class Ordered_value_list
3348 public:
3349 Ordered_value_list(Expression_list* vals)
3350 : vals_(vals), traverse_order_(NULL)
3353 Expression_list*
3354 vals() const
3355 { return this->vals_; }
3358 traverse_vals(Traverse* traverse);
3360 // Get the traversal order (may be NULL)
3361 std::vector<unsigned long>*
3362 traverse_order()
3363 { return traverse_order_; }
3365 // Set the traversal order, used to ensure that we implement the
3366 // order of evaluation rules. Takes ownership of the argument.
3367 void
3368 set_traverse_order(std::vector<unsigned long>* traverse_order)
3369 { this->traverse_order_ = traverse_order; }
3371 private:
3372 // The list of values, in order of the fields in the struct or in
3373 // order of indices in an array. A NULL value of vals_ means that
3374 // all fields/slots should be zero-initialized; a single NULL entry
3375 // in the list means that the corresponding field or array slot
3376 // should be zero-initialized.
3377 Expression_list* vals_;
3378 // If not NULL, the order in which to traverse vals_. This is used
3379 // so that we implement the order of evaluation rules correctly.
3380 std::vector<unsigned long>* traverse_order_;
3383 // Construct a struct.
3385 class Struct_construction_expression : public Expression,
3386 public Ordered_value_list
3388 public:
3389 Struct_construction_expression(Type* type, Expression_list* vals,
3390 Location location)
3391 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
3392 Ordered_value_list(vals),
3393 type_(type)
3396 // Return whether this is a constant initializer.
3397 bool
3398 is_constant_struct() const;
3400 protected:
3402 do_traverse(Traverse* traverse);
3404 bool
3405 do_is_static_initializer() const;
3407 Type*
3408 do_type()
3409 { return this->type_; }
3411 void
3412 do_determine_type(const Type_context*);
3414 void
3415 do_check_types(Gogo*);
3417 Expression*
3418 do_copy()
3420 Struct_construction_expression* ret =
3421 new Struct_construction_expression(this->type_,
3422 (this->vals() == NULL
3423 ? NULL
3424 : this->vals()->copy()),
3425 this->location());
3426 if (this->traverse_order() != NULL)
3427 ret->set_traverse_order(this->traverse_order());
3428 return ret;
3431 Expression*
3432 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3434 Bexpression*
3435 do_get_backend(Translate_context*);
3437 void
3438 do_export(Export*) const;
3440 void
3441 do_dump_expression(Ast_dump_context*) const;
3443 private:
3444 // The type of the struct to construct.
3445 Type* type_;
3448 // Construct an array. This class is not used directly; instead we
3449 // use the child classes, Fixed_array_construction_expression and
3450 // Slice_construction_expression.
3452 class Array_construction_expression : public Expression,
3453 public Ordered_value_list
3455 protected:
3456 Array_construction_expression(Expression_classification classification,
3457 Type* type,
3458 const std::vector<unsigned long>* indexes,
3459 Expression_list* vals, Location location)
3460 : Expression(classification, location),
3461 Ordered_value_list(vals),
3462 type_(type), indexes_(indexes)
3463 { go_assert(indexes == NULL || indexes->size() == vals->size()); }
3465 public:
3466 // Return whether this is a constant initializer.
3467 bool
3468 is_constant_array() const;
3470 // Return the number of elements.
3471 size_t
3472 element_count() const
3473 { return this->vals() == NULL ? 0 : this->vals()->size(); }
3475 protected:
3476 virtual int
3477 do_traverse(Traverse* traverse);
3479 bool
3480 do_is_static_initializer() const;
3482 Type*
3483 do_type()
3484 { return this->type_; }
3486 void
3487 do_determine_type(const Type_context*);
3489 void
3490 do_check_types(Gogo*);
3492 void
3493 do_export(Export*) const;
3495 // The indexes.
3496 const std::vector<unsigned long>*
3497 indexes()
3498 { return this->indexes_; }
3500 Expression*
3501 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3503 // Get the backend constructor for the array values.
3504 Bexpression*
3505 get_constructor(Translate_context* context, Btype* btype);
3507 void
3508 do_dump_expression(Ast_dump_context*) const;
3510 virtual void
3511 dump_slice_storage_expression(Ast_dump_context*) const { }
3513 private:
3514 // The type of the array to construct.
3515 Type* type_;
3516 // The list of indexes into the array, one for each value. This may
3517 // be NULL, in which case the indexes start at zero and increment.
3518 const std::vector<unsigned long>* indexes_;
3521 // Construct a fixed array.
3523 class Fixed_array_construction_expression :
3524 public Array_construction_expression
3526 public:
3527 Fixed_array_construction_expression(Type* type,
3528 const std::vector<unsigned long>* indexes,
3529 Expression_list* vals, Location location);
3531 protected:
3532 Expression*
3533 do_copy()
3535 return new Fixed_array_construction_expression(this->type(),
3536 this->indexes(),
3537 (this->vals() == NULL
3538 ? NULL
3539 : this->vals()->copy()),
3540 this->location());
3543 Bexpression*
3544 do_get_backend(Translate_context*);
3547 // Construct a slice.
3549 class Slice_construction_expression : public Array_construction_expression
3551 public:
3552 Slice_construction_expression(Type* type,
3553 const std::vector<unsigned long>* indexes,
3554 Expression_list* vals, Location location);
3556 Expression*
3557 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3559 // Record that the storage for this slice (e.g. vals) cannot escape,
3560 // hence it can be stack-allocated.
3561 void
3562 set_storage_does_not_escape()
3564 this->storage_escapes_ = false;
3567 protected:
3568 // Note that taking the address of a slice literal is invalid.
3571 do_traverse(Traverse* traverse);
3573 Expression*
3574 do_copy()
3576 return new Slice_construction_expression(this->type(), this->indexes(),
3577 (this->vals() == NULL
3578 ? NULL
3579 : this->vals()->copy()),
3580 this->location());
3583 Bexpression*
3584 do_get_backend(Translate_context*);
3586 void
3587 dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const;
3589 // Create an array value for the constructed slice. Invoked during
3590 // flattening if slice storage does not escape, otherwise invoked
3591 // later on during do_get_backend().
3592 Expression*
3593 create_array_val();
3595 private:
3596 // The type of the values in this slice.
3597 Type* valtype_;
3598 // Array value expression, optionally filled in during flattening.
3599 Expression* array_val_;
3600 // Slice storage expression, optionally filled in during flattening.
3601 Expression* slice_storage_;
3602 // Normally true. Can be set to false if we know that the resulting
3603 // storage for the slice cannot escape.
3604 bool storage_escapes_;
3607 // Construct a map.
3609 class Map_construction_expression : public Expression
3611 public:
3612 Map_construction_expression(Type* type, Expression_list* vals,
3613 Location location)
3614 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
3615 type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL)
3616 { go_assert(vals == NULL || vals->size() % 2 == 0); }
3618 Expression_list*
3619 vals() const
3620 { return this->vals_; }
3622 protected:
3624 do_traverse(Traverse* traverse);
3626 Expression*
3627 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3629 Type*
3630 do_type()
3631 { return this->type_; }
3633 void
3634 do_determine_type(const Type_context*);
3636 void
3637 do_check_types(Gogo*);
3639 Expression*
3640 do_copy()
3642 return new Map_construction_expression(this->type_,
3643 (this->vals_ == NULL
3644 ? NULL
3645 : this->vals_->copy()),
3646 this->location());
3649 Bexpression*
3650 do_get_backend(Translate_context*);
3652 void
3653 do_export(Export*) const;
3655 void
3656 do_dump_expression(Ast_dump_context*) const;
3658 private:
3659 // The type of the map to construct.
3660 Type* type_;
3661 // The list of values.
3662 Expression_list* vals_;
3663 // The type of the key-value pair struct for each map element.
3664 Struct_type* element_type_;
3665 // A temporary reference to the variable storing the constructor initializer.
3666 Temporary_statement* constructor_temp_;
3669 // A type guard expression.
3671 class Type_guard_expression : public Expression
3673 public:
3674 Type_guard_expression(Expression* expr, Type* type, Location location)
3675 : Expression(EXPRESSION_TYPE_GUARD, location),
3676 expr_(expr), type_(type)
3679 // Return the expression to convert.
3680 Expression*
3681 expr()
3682 { return this->expr_; }
3684 // Return the type to which to convert.
3685 Type*
3686 type()
3687 { return this->type_; }
3689 protected:
3691 do_traverse(Traverse* traverse);
3693 Expression*
3694 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3696 Type*
3697 do_type()
3698 { return this->type_; }
3700 void
3701 do_determine_type(const Type_context*)
3702 { this->expr_->determine_type_no_context(); }
3704 void
3705 do_check_types(Gogo*);
3707 Expression*
3708 do_copy()
3710 return new Type_guard_expression(this->expr_->copy(), this->type_,
3711 this->location());
3714 Bexpression*
3715 do_get_backend(Translate_context*);
3717 void
3718 do_dump_expression(Ast_dump_context*) const;
3720 private:
3721 // The expression to convert.
3722 Expression* expr_;
3723 // The type to which to convert.
3724 Type* type_;
3727 // Class Heap_expression.
3729 // When you take the address of an escaping expression, it is allocated
3730 // on the heap. This class implements that.
3732 class Heap_expression : public Expression
3734 public:
3735 Heap_expression(Expression* expr, Location location)
3736 : Expression(EXPRESSION_HEAP, location),
3737 expr_(expr)
3740 Expression*
3741 expr() const
3742 { return this->expr_; }
3744 protected:
3746 do_traverse(Traverse* traverse)
3747 { return Expression::traverse(&this->expr_, traverse); }
3749 Type*
3750 do_type();
3751 void
3752 do_determine_type(const Type_context*)
3753 { this->expr_->determine_type_no_context(); }
3755 Expression*
3756 do_copy()
3758 return Expression::make_heap_expression(this->expr_->copy(),
3759 this->location());
3762 Bexpression*
3763 do_get_backend(Translate_context*);
3765 // We only export global objects, and the parser does not generate
3766 // this in global scope.
3767 void
3768 do_export(Export*) const
3769 { go_unreachable(); }
3771 void
3772 do_dump_expression(Ast_dump_context*) const;
3774 private:
3775 // The expression which is being put on the heap.
3776 Expression* expr_;
3779 // A receive expression.
3781 class Receive_expression : public Expression
3783 public:
3784 Receive_expression(Expression* channel, Location location)
3785 : Expression(EXPRESSION_RECEIVE, location),
3786 channel_(channel), temp_receiver_(NULL)
3789 // Return the channel.
3790 Expression*
3791 channel()
3792 { return this->channel_; }
3794 protected:
3796 do_traverse(Traverse* traverse)
3797 { return Expression::traverse(&this->channel_, traverse); }
3799 bool
3800 do_discarding_value()
3801 { return true; }
3803 Type*
3804 do_type();
3806 Expression*
3807 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3809 void
3810 do_determine_type(const Type_context*)
3811 { this->channel_->determine_type_no_context(); }
3813 void
3814 do_check_types(Gogo*);
3816 Expression*
3817 do_copy()
3819 return Expression::make_receive(this->channel_->copy(), this->location());
3822 bool
3823 do_must_eval_in_order() const
3824 { return true; }
3826 Bexpression*
3827 do_get_backend(Translate_context*);
3829 void
3830 do_dump_expression(Ast_dump_context*) const;
3832 private:
3833 // The channel from which we are receiving.
3834 Expression* channel_;
3835 // A temporary reference to the variable storing the received data.
3836 Temporary_statement* temp_receiver_;
3839 // Conditional expressions.
3841 class Conditional_expression : public Expression
3843 public:
3844 Conditional_expression(Expression* cond, Expression* then_expr,
3845 Expression* else_expr, Location location)
3846 : Expression(EXPRESSION_CONDITIONAL, location),
3847 cond_(cond), then_(then_expr), else_(else_expr)
3850 Expression*
3851 condition() const
3852 { return this->cond_; }
3854 protected:
3856 do_traverse(Traverse*);
3858 Type*
3859 do_type();
3861 void
3862 do_determine_type(const Type_context*);
3864 Expression*
3865 do_copy()
3867 return new Conditional_expression(this->cond_->copy(), this->then_->copy(),
3868 this->else_->copy(), this->location());
3871 Bexpression*
3872 do_get_backend(Translate_context* context);
3874 void
3875 do_dump_expression(Ast_dump_context*) const;
3877 private:
3878 // The condition to be checked.
3879 Expression* cond_;
3880 // The expression to execute if the condition is true.
3881 Expression* then_;
3882 // The expression to execute if the condition is false.
3883 Expression* else_;
3886 // Compound expressions.
3888 class Compound_expression : public Expression
3890 public:
3891 Compound_expression(Expression* init, Expression* expr, Location location)
3892 : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr)
3895 Expression*
3896 init() const
3897 { return this->init_; }
3899 protected:
3901 do_traverse(Traverse*);
3903 Type*
3904 do_type();
3906 void
3907 do_determine_type(const Type_context*);
3909 Expression*
3910 do_copy()
3912 return new Compound_expression(this->init_->copy(), this->expr_->copy(),
3913 this->location());
3916 Bexpression*
3917 do_get_backend(Translate_context* context);
3919 void
3920 do_dump_expression(Ast_dump_context*) const;
3922 private:
3923 // The expression that is evaluated first and discarded.
3924 Expression* init_;
3925 // The expression that is evaluated and returned.
3926 Expression* expr_;
3929 // A backend expression. This is a backend expression wrapped in an
3930 // Expression, for convenience during backend generation.
3932 class Backend_expression : public Expression
3934 public:
3935 Backend_expression(Bexpression* bexpr, Type* type, Location location)
3936 : Expression(EXPRESSION_BACKEND, location), bexpr_(bexpr), type_(type)
3939 protected:
3941 do_traverse(Traverse*);
3943 // For now these are always valid static initializers. If that
3944 // changes we can change this.
3945 bool
3946 do_is_static_initializer() const
3947 { return true; }
3949 Type*
3950 do_type()
3951 { return this->type_; }
3953 void
3954 do_determine_type(const Type_context*)
3957 Expression*
3958 do_copy()
3960 return new Backend_expression(this->bexpr_, this->type_, this->location());
3963 Bexpression*
3964 do_get_backend(Translate_context*)
3965 { return this->bexpr_; }
3967 void
3968 do_dump_expression(Ast_dump_context*) const;
3970 private:
3971 // The backend expression we are wrapping.
3972 Bexpression* bexpr_;
3973 // The type of the expression;
3974 Type* type_;
3977 // A numeric constant. This is used both for untyped constants and
3978 // for constants that have a type.
3980 class Numeric_constant
3982 public:
3983 Numeric_constant()
3984 : classification_(NC_INVALID), type_(NULL)
3987 ~Numeric_constant();
3989 Numeric_constant(const Numeric_constant&);
3991 Numeric_constant& operator=(const Numeric_constant&);
3993 // Set to an unsigned long value.
3994 void
3995 set_unsigned_long(Type*, unsigned long);
3997 // Set to an integer value.
3998 void
3999 set_int(Type*, const mpz_t);
4001 // Set to a rune value.
4002 void
4003 set_rune(Type*, const mpz_t);
4005 // Set to a floating point value.
4006 void
4007 set_float(Type*, const mpfr_t);
4009 // Set to a complex value.
4010 void
4011 set_complex(Type*, const mpc_t);
4013 // Mark numeric constant as invalid.
4014 void
4015 set_invalid()
4016 { this->classification_ = NC_INVALID; }
4018 // Classifiers.
4019 bool
4020 is_int() const
4021 { return this->classification_ == Numeric_constant::NC_INT; }
4023 bool
4024 is_rune() const
4025 { return this->classification_ == Numeric_constant::NC_RUNE; }
4027 bool
4028 is_float() const
4029 { return this->classification_ == Numeric_constant::NC_FLOAT; }
4031 bool
4032 is_complex() const
4033 { return this->classification_ == Numeric_constant::NC_COMPLEX; }
4035 bool
4036 is_invalid() const
4037 { return this->classification_ == Numeric_constant::NC_INVALID; }
4039 // Value retrievers. These will initialize the values as well as
4040 // set them. GET_INT is only valid if IS_INT returns true, and
4041 // likewise respectively.
4042 void
4043 get_int(mpz_t*) const;
4045 void
4046 get_rune(mpz_t*) const;
4048 void
4049 get_float(mpfr_t*) const;
4051 void
4052 get_complex(mpc_t*) const;
4054 // Codes returned by to_unsigned_long.
4055 enum To_unsigned_long
4057 // Value is integer and fits in unsigned long.
4058 NC_UL_VALID,
4059 // Value is not integer.
4060 NC_UL_NOTINT,
4061 // Value is integer but is negative.
4062 NC_UL_NEGATIVE,
4063 // Value is non-negative integer but does not fit in unsigned
4064 // long.
4065 NC_UL_BIG
4068 // If the value can be expressed as an integer that fits in an
4069 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
4070 // one of the other To_unsigned_long codes.
4071 To_unsigned_long
4072 to_unsigned_long(unsigned long* val) const;
4074 // If the value can be expressed as an integer that describes the
4075 // size of an object in memory, set *VAL and return true.
4076 // Otherwise, return false. Currently we use int64_t to represent a
4077 // memory size, as in Type::backend_type_size.
4078 bool
4079 to_memory_size(int64_t* val) const;
4081 // If the value can be expressed as an int, return true and
4082 // initialize and set VAL. This will return false for a value with
4083 // an explicit float or complex type, even if the value is integral.
4084 bool
4085 to_int(mpz_t* val) const;
4087 // If the value can be expressed as a float, return true and
4088 // initialize and set VAL.
4089 bool
4090 to_float(mpfr_t* val) const;
4092 // If the value can be expressed as a complex, return true and
4093 // initialize and set VR and VI.
4094 bool
4095 to_complex(mpc_t* val) const;
4097 // Get the type.
4098 Type*
4099 type() const;
4101 // If the constant can be expressed in TYPE, then set the type of
4102 // the constant to TYPE and return true. Otherwise return false,
4103 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
4104 // the location to use for the error.
4105 bool
4106 set_type(Type* type, bool issue_error, Location location);
4108 // Return an Expression for this value.
4109 Expression*
4110 expression(Location) const;
4112 private:
4113 void
4114 clear();
4116 To_unsigned_long
4117 mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
4119 To_unsigned_long
4120 mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
4122 bool
4123 mpz_to_memory_size(const mpz_t ival, int64_t* val) const;
4125 bool
4126 mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const;
4128 bool
4129 check_int_type(Integer_type*, bool, Location);
4131 bool
4132 check_float_type(Float_type*, bool, Location);
4134 bool
4135 check_complex_type(Complex_type*, bool, Location);
4137 // The kinds of constants.
4138 enum Classification
4140 NC_INVALID,
4141 NC_RUNE,
4142 NC_INT,
4143 NC_FLOAT,
4144 NC_COMPLEX
4147 // The kind of constant.
4148 Classification classification_;
4149 // The value.
4150 union
4152 // If NC_INT or NC_RUNE.
4153 mpz_t int_val;
4154 // If NC_FLOAT.
4155 mpfr_t float_val;
4156 // If NC_COMPLEX.
4157 mpc_t complex_val;
4158 } u_;
4159 // The type if there is one. This will be NULL for an untyped
4160 // constant.
4161 Type* type_;
4164 #endif // !defined(GO_EXPRESSIONS_H)