compiler: omit a couple of write barriers
[official-gcc.git] / gcc / go / gofrontend / expressions.h
blobf53cc6e62aa45ae2ffc996caa42622f3f0241f1c
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 Builtin_call_expression;
43 class Call_result_expression;
44 class Func_expression;
45 class Func_descriptor_expression;
46 class Unknown_expression;
47 class Index_expression;
48 class Array_index_expression;
49 class String_index_expression;
50 class Map_index_expression;
51 class Bound_method_expression;
52 class Field_reference_expression;
53 class Interface_field_reference_expression;
54 class Allocation_expression;
55 class Composite_literal_expression;
56 class Struct_construction_expression;
57 class Array_construction_expression;
58 class Fixed_array_construction_expression;
59 class Slice_construction_expression;
60 class Map_construction_expression;
61 class Type_guard_expression;
62 class Heap_expression;
63 class Receive_expression;
64 class Conditional_expression;
65 class Compound_expression;
66 class Numeric_constant;
67 class Named_object;
68 class Export;
69 class Import;
70 class Temporary_statement;
71 class Label;
72 class Ast_dump_context;
73 class String_dump;
75 // The precision to use for complex values represented as an mpc_t.
76 const int mpc_precision = 256;
78 // The base class for all expressions.
80 class Expression
82 public:
83 // The types of expressions.
84 enum Expression_classification
86 EXPRESSION_ERROR,
87 EXPRESSION_TYPE,
88 EXPRESSION_UNARY,
89 EXPRESSION_BINARY,
90 EXPRESSION_STRING_CONCAT,
91 EXPRESSION_CONST_REFERENCE,
92 EXPRESSION_VAR_REFERENCE,
93 EXPRESSION_ENCLOSED_VAR_REFERENCE,
94 EXPRESSION_TEMPORARY_REFERENCE,
95 EXPRESSION_SET_AND_USE_TEMPORARY,
96 EXPRESSION_SINK,
97 EXPRESSION_FUNC_REFERENCE,
98 EXPRESSION_FUNC_DESCRIPTOR,
99 EXPRESSION_FUNC_CODE_REFERENCE,
100 EXPRESSION_UNKNOWN_REFERENCE,
101 EXPRESSION_BOOLEAN,
102 EXPRESSION_STRING,
103 EXPRESSION_STRING_INFO,
104 EXPRESSION_INTEGER,
105 EXPRESSION_FLOAT,
106 EXPRESSION_COMPLEX,
107 EXPRESSION_NIL,
108 EXPRESSION_IOTA,
109 EXPRESSION_CALL,
110 EXPRESSION_CALL_RESULT,
111 EXPRESSION_BOUND_METHOD,
112 EXPRESSION_INDEX,
113 EXPRESSION_ARRAY_INDEX,
114 EXPRESSION_STRING_INDEX,
115 EXPRESSION_MAP_INDEX,
116 EXPRESSION_SELECTOR,
117 EXPRESSION_FIELD_REFERENCE,
118 EXPRESSION_INTERFACE_FIELD_REFERENCE,
119 EXPRESSION_ALLOCATION,
120 EXPRESSION_TYPE_GUARD,
121 EXPRESSION_CONVERSION,
122 EXPRESSION_UNSAFE_CONVERSION,
123 EXPRESSION_STRUCT_CONSTRUCTION,
124 EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
125 EXPRESSION_SLICE_CONSTRUCTION,
126 EXPRESSION_MAP_CONSTRUCTION,
127 EXPRESSION_COMPOSITE_LITERAL,
128 EXPRESSION_HEAP,
129 EXPRESSION_RECEIVE,
130 EXPRESSION_TYPE_DESCRIPTOR,
131 EXPRESSION_GC_SYMBOL,
132 EXPRESSION_PTRMASK_SYMBOL,
133 EXPRESSION_TYPE_INFO,
134 EXPRESSION_SLICE_INFO,
135 EXPRESSION_SLICE_VALUE,
136 EXPRESSION_INTERFACE_INFO,
137 EXPRESSION_INTERFACE_VALUE,
138 EXPRESSION_INTERFACE_MTABLE,
139 EXPRESSION_STRUCT_FIELD_OFFSET,
140 EXPRESSION_LABEL_ADDR,
141 EXPRESSION_CONDITIONAL,
142 EXPRESSION_COMPOUND,
143 EXPRESSION_BACKEND
146 Expression(Expression_classification, Location);
148 virtual ~Expression();
150 // Make an error expression. This is used when a parse error occurs
151 // to prevent cascading errors.
152 static Expression*
153 make_error(Location);
155 // Make an expression which is really a type. This is used during
156 // parsing.
157 static Expression*
158 make_type(Type*, Location);
160 // Make a unary expression.
161 static Expression*
162 make_unary(Operator, Expression*, Location);
164 // Make a binary expression.
165 static Expression*
166 make_binary(Operator, Expression*, Expression*, Location);
168 // Make a string concatenation expression.
169 static Expression*
170 make_string_concat(Expression_list*);
172 // Make a reference to a constant in an expression.
173 static Expression*
174 make_const_reference(Named_object*, Location);
176 // Make a reference to a variable in an expression.
177 static Expression*
178 make_var_reference(Named_object*, Location);
180 // Make a reference to a variable within an enclosing function.
181 static Expression*
182 make_enclosing_var_reference(Expression*, Named_object*, Location);
184 // Make a reference to a temporary variable. Temporary variables
185 // are always created by a single statement, which is what we use to
186 // refer to them.
187 static Temporary_reference_expression*
188 make_temporary_reference(Temporary_statement*, Location);
190 // Make an expressions which sets a temporary variable and then
191 // evaluates to a reference to that temporary variable. This is
192 // used to set a temporary variable while retaining the order of
193 // evaluation.
194 static Set_and_use_temporary_expression*
195 make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
197 // Make a sink expression--a reference to the blank identifier _.
198 static Expression*
199 make_sink(Location);
201 // Make a reference to a function in an expression. This returns a
202 // pointer to the struct holding the address of the function
203 // followed by any closed-over variables.
204 static Expression*
205 make_func_reference(Named_object*, Expression* closure, Location);
207 // Make a function descriptor, an immutable struct with a single
208 // field that points to the function code. This may only be used
209 // with functions that do not have closures. FN is the function for
210 // which we are making the descriptor.
211 static Func_descriptor_expression*
212 make_func_descriptor(Named_object* fn);
214 // Make a reference to the code of a function. This is used to set
215 // descriptor and closure fields.
216 static Expression*
217 make_func_code_reference(Named_object*, Location);
219 // Make a reference to an unknown name. In a correct program this
220 // will always be lowered to a real const/var/func reference.
221 static Unknown_expression*
222 make_unknown_reference(Named_object*, Location);
224 // Make a constant bool expression.
225 static Expression*
226 make_boolean(bool val, Location);
228 // Make a constant string expression.
229 static Expression*
230 make_string(const std::string&, Location);
232 // Make an expression that evaluates to some characteristic of an string.
233 // For simplicity, the enum values must match the field indexes in the
234 // underlying struct.
235 enum String_info
237 // The underlying data in the string.
238 STRING_INFO_DATA,
239 // The length of the string.
240 STRING_INFO_LENGTH
243 static Expression*
244 make_string_info(Expression* string, String_info, Location);
246 // Make a character constant expression. TYPE should be NULL for an
247 // abstract type.
248 static Expression*
249 make_character(const mpz_t*, Type*, Location);
251 // Make a constant integer expression from a multi-precision
252 // integer. TYPE should be NULL for an abstract type.
253 static Expression*
254 make_integer_z(const mpz_t*, Type*, Location);
256 // Make a constant integer expression from an unsigned long. TYPE
257 // should be NULL for an abstract type.
258 static Expression*
259 make_integer_ul(unsigned long, Type*, Location);
261 // Make a constant integer expression from a signed long. TYPE
262 // should be NULL for an abstract type.
263 static Expression*
264 make_integer_sl(long, Type*, Location);
266 // Make a constant integer expression from an int64_t. TYPE should
267 // be NULL for an abstract type.
268 static Expression*
269 make_integer_int64(int64_t, Type*, Location);
271 // Make a constant float expression. TYPE should be NULL for an
272 // abstract type.
273 static Expression*
274 make_float(const mpfr_t*, Type*, Location);
276 // Make a constant complex expression. TYPE should be NULL for an
277 // abstract type.
278 static Expression*
279 make_complex(const mpc_t*, Type*, Location);
281 // Make a nil expression.
282 static Expression*
283 make_nil(Location);
285 // Make an iota expression. This is used for the predeclared
286 // constant iota.
287 static Expression*
288 make_iota();
290 // Make a call expression.
291 static Call_expression*
292 make_call(Expression* func, Expression_list* args, bool is_varargs,
293 Location);
295 // Make a reference to a specific result of a call expression which
296 // returns a tuple.
297 static Expression*
298 make_call_result(Call_expression*, unsigned int index);
300 // Make an expression which is a method bound to its first
301 // parameter. METHOD is the method being called, FUNCTION is the
302 // function to call.
303 static Bound_method_expression*
304 make_bound_method(Expression* object, const Method* method,
305 Named_object* function, Location);
307 // Make an index or slice expression. This is a parser expression
308 // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
309 // index rather than a slice. CAP may be NULL, meaning we use the default
310 // capacity of LEFT. At parse time we may not know the type of LEFT.
311 // After parsing this is lowered to an array index, a string index,
312 // or a map index.
313 static Expression*
314 make_index(Expression* left, Expression* start, Expression* end,
315 Expression* cap, Location);
317 // Make an array index expression. END may be NULL, in which case
318 // this is an lvalue. CAP may be NULL, in which case it defaults
319 // to cap(ARRAY).
320 static Expression*
321 make_array_index(Expression* array, Expression* start, Expression* end,
322 Expression* cap, Location);
324 // Make a string index expression. END may be NULL. This is never
325 // an lvalue.
326 static Expression*
327 make_string_index(Expression* string, Expression* start, Expression* end,
328 Location);
330 // Make a map index expression. This is an lvalue.
331 static Map_index_expression*
332 make_map_index(Expression* map, Expression* val, Location);
334 // Make a selector. This is a parser expression which represents
335 // LEFT.NAME. At parse time we may not know the type of the left
336 // hand side.
337 static Expression*
338 make_selector(Expression* left, const std::string& name, Location);
340 // Make a reference to a field in a struct.
341 static Field_reference_expression*
342 make_field_reference(Expression*, unsigned int field_index, Location);
344 // Make a reference to a field of an interface, with an associated
345 // object.
346 static Expression*
347 make_interface_field_reference(Expression*, const std::string&,
348 Location);
350 // Make an allocation expression.
351 static Expression*
352 make_allocation(Type*, Location);
354 // Make a type guard expression.
355 static Expression*
356 make_type_guard(Expression*, Type*, Location);
358 // Make a type cast expression.
359 static Expression*
360 make_cast(Type*, Expression*, Location);
362 // Make an unsafe type cast expression. This is only used when
363 // passing parameter to builtin functions that are part of the Go
364 // runtime.
365 static Expression*
366 make_unsafe_cast(Type*, Expression*, Location);
368 // Make a composite literal. The DEPTH parameter is how far down we
369 // are in a list of composite literals with omitted types. HAS_KEYS
370 // is true if the expression list has keys alternating with values.
371 // ALL_ARE_NAMES is true if all the keys could be struct field
372 // names.
373 static Expression*
374 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
375 bool all_are_names, Location);
377 // Make a struct composite literal.
378 static Expression*
379 make_struct_composite_literal(Type*, Expression_list*, Location);
381 // Make an array composite literal.
382 static Expression*
383 make_array_composite_literal(Type*, Expression_list*, Location);
385 // Make a slice composite literal.
386 static Slice_construction_expression*
387 make_slice_composite_literal(Type*, Expression_list*, Location);
389 // Take an expression and allocate it on the heap.
390 static Expression*
391 make_heap_expression(Expression*, Location);
393 // Make a receive expression. VAL is NULL for a unary receive.
394 static Receive_expression*
395 make_receive(Expression* channel, Location);
397 // Make an expression which evaluates to the address of the type
398 // descriptor for TYPE.
399 static Expression*
400 make_type_descriptor(Type* type, Location);
402 // Make an expression which evaluates to the address of the gc
403 // symbol for TYPE.
404 static Expression*
405 make_gc_symbol(Type* type);
407 // Make an expression that evaluates to the address of a ptrmask
408 // symbol for TYPE. For most types this will be the same as
409 // make_gc_symbol, but for larger types make_gc_symbol will return a
410 // gcprog while this will return a ptrmask.
411 static Expression*
412 make_ptrmask_symbol(Type* type);
414 // Make an expression which evaluates to some characteristic of a
415 // type. These are only used for type descriptors, so there is no
416 // location parameter.
417 enum Type_info
419 // The size of a value of the type.
420 TYPE_INFO_SIZE,
421 // The required alignment of a value of the type.
422 TYPE_INFO_ALIGNMENT,
423 // The required alignment of a value of the type when used as a
424 // field in a struct.
425 TYPE_INFO_FIELD_ALIGNMENT,
426 // The size of the prefix of a value of the type that contains
427 // all the pointers. This is 0 for a type that contains no
428 // pointers. It is always <= TYPE_INFO_SIZE.
429 TYPE_INFO_BACKEND_PTRDATA,
430 // Like TYPE_INFO_BACKEND_PTRDATA, but the ptrdata value that we
431 // want to store in a type descriptor. They are the same for
432 // most types, but can differ for a type that uses a gcprog.
433 TYPE_INFO_DESCRIPTOR_PTRDATA
436 static Expression*
437 make_type_info(Type* type, Type_info);
439 // Make an expression that evaluates to some characteristic of a
440 // slice. For simplicity, the enum values must match the field indexes
441 // in the underlying struct.
442 enum Slice_info
444 // The underlying data of the slice.
445 SLICE_INFO_VALUE_POINTER,
446 // The length of the slice.
447 SLICE_INFO_LENGTH,
448 // The capacity of the slice.
449 SLICE_INFO_CAPACITY
452 static Expression*
453 make_slice_info(Expression* slice, Slice_info, Location);
455 // Make an expression for a slice value.
456 static Expression*
457 make_slice_value(Type*, Expression* valptr, Expression* len, Expression* cap,
458 Location);
460 // Make an expression that evaluates to some characteristic of an
461 // interface. For simplicity, the enum values must match the field indexes
462 // in the underlying struct.
463 enum Interface_info
465 // The type descriptor of an empty interface.
466 INTERFACE_INFO_TYPE_DESCRIPTOR = 0,
467 // The methods of an interface.
468 INTERFACE_INFO_METHODS = 0,
469 // The first argument to pass to an interface method.
470 INTERFACE_INFO_OBJECT
473 static Expression*
474 make_interface_info(Expression* iface, Interface_info, Location);
476 // Make an expression for an interface value.
477 static Expression*
478 make_interface_value(Type*, Expression*, Expression*, Location);
480 // Make an expression that builds a reference to the interface method table
481 // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
482 // reference to the interface method table for the pointer receiver type.
483 static Expression*
484 make_interface_mtable_ref(Interface_type* itype, Type* type,
485 bool is_pointer, Location);
487 // Make an expression which evaluates to the offset of a field in a
488 // struct. This is only used for type descriptors, so there is no
489 // location parameter.
490 static Expression*
491 make_struct_field_offset(Struct_type*, const Struct_field*);
493 // Make an expression which evaluates to the address of an unnamed
494 // label.
495 static Expression*
496 make_label_addr(Label*, Location);
498 // Make a conditional expression.
499 static Expression*
500 make_conditional(Expression*, Expression*, Expression*, Location);
502 // Make a compound expression.
503 static Expression*
504 make_compound(Expression*, Expression*, Location);
506 // Make a backend expression.
507 static Expression*
508 make_backend(Bexpression*, Type*, Location);
510 enum Nil_check_classification
512 // Use the default policy for deciding if this deref needs a check.
513 NIL_CHECK_DEFAULT,
514 // An explicit check is required for this dereference operation.
515 NIL_CHECK_NEEDED,
516 // No check needed for this dereference operation.
517 NIL_CHECK_NOT_NEEDED,
518 // A type error or error construct was encountered when determining
519 // whether this deref needs an explicit check.
520 NIL_CHECK_ERROR_ENCOUNTERED
523 // Make a dereference expression.
524 static Expression*
525 make_dereference(Expression*, Nil_check_classification, Location);
527 // Return the expression classification.
528 Expression_classification
529 classification() const
530 { return this->classification_; }
532 // Return the location of the expression.
533 Location
534 location() const
535 { return this->location_; }
537 // Return whether this is a constant expression.
538 bool
539 is_constant() const
540 { return this->do_is_constant(); }
542 // Return whether this expression can be used as a static
543 // initializer. This is true for an expression that has only
544 // numbers and pointers to global variables or composite literals
545 // that do not require runtime initialization. It is false if we
546 // must generate code to compute this expression when it is used to
547 // initialize a global variable. This is not a language-level
548 // concept, but an implementation-level one. If this expression is
549 // used to initialize a global variable, this is true if we can pass
550 // an initializer to the backend, false if we must generate code to
551 // initialize the variable. It is always safe for this method to
552 // return false, but the resulting code may be less efficient.
553 bool
554 is_static_initializer() const
555 { return this->do_is_static_initializer(); }
557 // If this is not a numeric constant, return false. If it is one,
558 // return true, and set VAL to hold the value.
559 bool
560 numeric_constant_value(Numeric_constant* val) const
561 { return this->do_numeric_constant_value(val); }
563 // If this is not a constant expression with string type, return
564 // false. If it is one, return true, and set VAL to the value.
565 bool
566 string_constant_value(std::string* val) const
567 { return this->do_string_constant_value(val); }
569 // This is called if the value of this expression is being
570 // discarded. This issues warnings about computed values being
571 // unused. This returns true if all is well, false if it issued an
572 // error message.
573 bool
574 discarding_value()
575 { return this->do_discarding_value(); }
577 // Return whether this is an error expression.
578 bool
579 is_error_expression() const
580 { return this->classification_ == EXPRESSION_ERROR; }
582 // Return whether this expression really represents a type.
583 bool
584 is_type_expression() const
585 { return this->classification_ == EXPRESSION_TYPE; }
587 // If this is a variable reference, return the Var_expression
588 // structure. Otherwise, return NULL. This is a controlled dynamic
589 // cast.
590 Var_expression*
591 var_expression()
592 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
594 const Var_expression*
595 var_expression() const
596 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
598 // If this is a enclosed_variable reference, return the
599 // Enclosed_var_expression structure. Otherwise, return NULL.
600 // This is a controlled dynamic cast.
601 Enclosed_var_expression*
602 enclosed_var_expression()
603 { return this->convert<Enclosed_var_expression,
604 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
606 const Enclosed_var_expression*
607 enclosed_var_expression() const
608 { return this->convert<const Enclosed_var_expression,
609 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
612 // If this is a reference to a temporary variable, return the
613 // Temporary_reference_expression. Otherwise, return NULL.
614 Temporary_reference_expression*
615 temporary_reference_expression()
617 return this->convert<Temporary_reference_expression,
618 EXPRESSION_TEMPORARY_REFERENCE>();
621 // If this is a set-and-use-temporary, return the
622 // Set_and_use_temporary_expression. Otherwise, return NULL.
623 Set_and_use_temporary_expression*
624 set_and_use_temporary_expression()
626 return this->convert<Set_and_use_temporary_expression,
627 EXPRESSION_SET_AND_USE_TEMPORARY>();
630 // Return whether this is a sink expression.
631 bool
632 is_sink_expression() const
633 { return this->classification_ == EXPRESSION_SINK; }
635 // If this is a string expression, return the String_expression
636 // structure. Otherwise, return NULL.
637 String_expression*
638 string_expression()
639 { return this->convert<String_expression, EXPRESSION_STRING>(); }
641 // If this is a conversion expression, return the Type_conversion_expression
642 // structure. Otherwise, return NULL.
643 Type_conversion_expression*
644 conversion_expression()
645 { return this->convert<Type_conversion_expression, EXPRESSION_CONVERSION>(); }
647 // If this is an unsafe conversion expression, return the
648 // Unsafe_type_conversion_expression structure. Otherwise, return NULL.
649 Unsafe_type_conversion_expression*
650 unsafe_conversion_expression()
652 return this->convert<Unsafe_type_conversion_expression,
653 EXPRESSION_UNSAFE_CONVERSION>();
656 // Return whether this is the expression nil.
657 bool
658 is_nil_expression() const
659 { return this->classification_ == EXPRESSION_NIL; }
661 // If this is an indirection through a pointer, return the
662 // expression being pointed through. Otherwise return this.
663 Expression*
664 deref();
666 // If this is a unary expression, return the Unary_expression
667 // structure. Otherwise return NULL.
668 Unary_expression*
669 unary_expression()
670 { return this->convert<Unary_expression, EXPRESSION_UNARY>(); }
672 // If this is a binary expression, return the Binary_expression
673 // structure. Otherwise return NULL.
674 Binary_expression*
675 binary_expression()
676 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
678 // If this is a string concatenation expression, return the
679 // String_concat_expression structure. Otherwise, return NULL.
680 String_concat_expression*
681 string_concat_expression()
683 return this->convert<String_concat_expression, EXPRESSION_STRING_CONCAT>();
686 // If this is a call expression, return the Call_expression
687 // structure. Otherwise, return NULL. This is a controlled dynamic
688 // cast.
689 Call_expression*
690 call_expression()
691 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
693 // If this is a call_result expression, return the Call_result_expression
694 // structure. Otherwise, return NULL. This is a controlled dynamic
695 // cast.
696 Call_result_expression*
697 call_result_expression()
698 { return this->convert<Call_result_expression, EXPRESSION_CALL_RESULT>(); }
700 // If this is an expression which refers to a function, return the
701 // Func_expression structure. Otherwise, return NULL.
702 Func_expression*
703 func_expression()
704 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
706 const Func_expression*
707 func_expression() const
708 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
710 // If this is an expression which refers to an unknown name, return
711 // the Unknown_expression structure. Otherwise, return NULL.
712 Unknown_expression*
713 unknown_expression()
714 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
716 const Unknown_expression*
717 unknown_expression() const
719 return this->convert<const Unknown_expression,
720 EXPRESSION_UNKNOWN_REFERENCE>();
723 // If this is an index expression, return the Index_expression
724 // structure. Otherwise, return NULL.
725 Index_expression*
726 index_expression()
727 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
729 // If this is an expression which refers to indexing in a array,
730 // return the Array_index_expression structure. Otherwise, return
731 // NULL.
732 Array_index_expression*
733 array_index_expression()
734 { return this->convert<Array_index_expression, EXPRESSION_ARRAY_INDEX>(); }
736 // If this is an expression which refers to indexing in a string,
737 // return the String_index_expression structure. Otherwise, return
738 // NULL.
739 String_index_expression*
740 string_index_expression()
741 { return this->convert<String_index_expression, EXPRESSION_STRING_INDEX>(); }
743 // If this is an expression which refers to indexing in a map,
744 // return the Map_index_expression structure. Otherwise, return
745 // NULL.
746 Map_index_expression*
747 map_index_expression()
748 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
750 // If this is a bound method expression, return the
751 // Bound_method_expression structure. Otherwise, return NULL.
752 Bound_method_expression*
753 bound_method_expression()
754 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
756 // If this is a reference to a field in a struct, return the
757 // Field_reference_expression structure. Otherwise, return NULL.
758 Field_reference_expression*
759 field_reference_expression()
761 return this->convert<Field_reference_expression,
762 EXPRESSION_FIELD_REFERENCE>();
765 // If this is a reference to a field in an interface, return the
766 // Interface_field_reference_expression structure. Otherwise,
767 // return NULL.
768 Interface_field_reference_expression*
769 interface_field_reference_expression()
771 return this->convert<Interface_field_reference_expression,
772 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
775 // If this is an allocation expression, return the Allocation_expression
776 // structure. Otherwise, return NULL.
777 Allocation_expression*
778 allocation_expression()
779 { return this->convert<Allocation_expression, EXPRESSION_ALLOCATION>(); }
781 // If this is a general composite literal, return the
782 // Composite_literal_expression structure. Otherwise, return NULL.
783 Composite_literal_expression*
784 complit()
786 return this->convert<Composite_literal_expression,
787 EXPRESSION_COMPOSITE_LITERAL>();
790 // If this is a struct composite literal, return the
791 // Struct_construction_expression structure. Otherwise, return NULL.
792 Struct_construction_expression*
793 struct_literal()
795 return this->convert<Struct_construction_expression,
796 EXPRESSION_STRUCT_CONSTRUCTION>();
799 // If this is a array composite literal, return the
800 // Array_construction_expression structure. Otherwise, return NULL.
801 Fixed_array_construction_expression*
802 array_literal()
804 return this->convert<Fixed_array_construction_expression,
805 EXPRESSION_FIXED_ARRAY_CONSTRUCTION>();
808 // If this is a slice composite literal, return the
809 // Slice_construction_expression structure. Otherwise, return NULL.
810 Slice_construction_expression*
811 slice_literal()
813 return this->convert<Slice_construction_expression,
814 EXPRESSION_SLICE_CONSTRUCTION>();
817 // If this is a map composite literal, return the
818 // Map_construction_expression structure. Otherwise, return NULL.
819 Map_construction_expression*
820 map_literal()
822 return this->convert<Map_construction_expression,
823 EXPRESSION_MAP_CONSTRUCTION>();
826 // If this is a type guard expression, return the
827 // Type_guard_expression structure. Otherwise, return NULL.
828 Type_guard_expression*
829 type_guard_expression()
830 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
832 // If this is a heap expression, returhn the Heap_expression structure.
833 // Otherwise, return NULL.
834 Heap_expression*
835 heap_expression()
836 { return this->convert<Heap_expression, EXPRESSION_HEAP>(); }
838 // If this is a receive expression, return the Receive_expression
839 // structure. Otherwise, return NULL.
840 Receive_expression*
841 receive_expression()
842 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
844 // If this is a conditional expression, return the Conditional_expression
845 // structure. Otherwise, return NULL.
846 Conditional_expression*
847 conditional_expression()
848 { return this->convert<Conditional_expression, EXPRESSION_CONDITIONAL>(); }
850 // If this is a compound expression, return the Compound_expression structure.
851 // Otherwise, return NULL.
852 Compound_expression*
853 compound_expression()
854 { return this->convert<Compound_expression, EXPRESSION_COMPOUND>(); }
856 // Return true if this is a composite literal.
857 bool
858 is_composite_literal() const;
860 // Return true if this is a composite literal which is not constant.
861 bool
862 is_nonconstant_composite_literal() const;
864 // Return true if this is a variable or temporary variable.
865 bool
866 is_variable() const;
868 // Return true if this is a reference to a local variable.
869 bool
870 is_local_variable() const;
872 // Return true if two expressions refer to the same variable or
873 // struct field.
874 static bool
875 is_same_variable(Expression*, Expression*);
877 // Make the builtin function descriptor type, so that it can be
878 // converted.
879 static void
880 make_func_descriptor_type();
882 // Traverse an expression.
883 static int
884 traverse(Expression**, Traverse*);
886 // Traverse subexpressions of this expression.
888 traverse_subexpressions(Traverse*);
890 // Lower an expression. This is called immediately after parsing.
891 // FUNCTION is the function we are in; it will be NULL for an
892 // expression initializing a global variable. INSERTER may be used
893 // to insert statements before the statement or initializer
894 // containing this expression; it is normally used to create
895 // temporary variables. IOTA_VALUE is the value that we should give
896 // to any iota expressions. This function must resolve expressions
897 // which could not be fully parsed into their final form. It
898 // returns the same Expression or a new one.
899 Expression*
900 lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
901 int iota_value)
902 { return this->do_lower(gogo, function, inserter, iota_value); }
904 // Flatten an expression. This is called after order_evaluation.
905 // FUNCTION is the function we are in; it will be NULL for an
906 // expression initializing a global variable. INSERTER may be used
907 // to insert statements before the statement or initializer
908 // containing this expression; it is normally used to create
909 // temporary variables. This function must resolve expressions
910 // which could not be fully parsed into their final form. It
911 // returns the same Expression or a new one.
912 Expression*
913 flatten(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
914 { return this->do_flatten(gogo, function, inserter); }
916 // Determine the real type of an expression with abstract integer,
917 // floating point, or complex type. TYPE_CONTEXT describes the
918 // expected type.
919 void
920 determine_type(const Type_context*);
922 // Check types in an expression.
923 void
924 check_types(Gogo* gogo)
925 { this->do_check_types(gogo); }
927 // Determine the type when there is no context.
928 void
929 determine_type_no_context();
931 // Return the current type of the expression. This may be changed
932 // by determine_type.
933 Type*
934 type()
935 { return this->do_type(); }
937 // Return a copy of an expression.
938 Expression*
939 copy()
940 { return this->do_copy(); }
942 // Return whether the expression is addressable--something which may
943 // be used as the operand of the unary & operator.
944 bool
945 is_addressable() const
946 { return this->do_is_addressable(); }
948 // Note that we are taking the address of this expression. ESCAPES
949 // is true if this address escapes the current function.
950 void
951 address_taken(bool escapes)
952 { this->do_address_taken(escapes); }
954 // Note that a nil check must be issued for this expression.
955 void
956 issue_nil_check()
957 { this->do_issue_nil_check(); }
959 // Return whether this expression must be evaluated in order
960 // according to the order of evaluation rules. This is basically
961 // true of all expressions with side-effects.
962 bool
963 must_eval_in_order() const
964 { return this->do_must_eval_in_order(); }
966 // Return whether subexpressions of this expression must be
967 // evaluated in order. This is true of index expressions and
968 // pointer indirections. This sets *SKIP to the number of
969 // subexpressions to skip during traversing, as index expressions
970 // only requiring moving the index, not the array.
971 bool
972 must_eval_subexpressions_in_order(int* skip) const
974 *skip = 0;
975 return this->do_must_eval_subexpressions_in_order(skip);
978 // Return the backend representation for this expression.
979 Bexpression*
980 get_backend(Translate_context*);
982 // Return an expression handling any conversions which must be done during
983 // assignment.
984 static Expression*
985 convert_for_assignment(Gogo*, Type* lhs_type, Expression* rhs,
986 Location location);
988 // Return an expression converting a value of one interface type to another
989 // interface type. If FOR_TYPE_GUARD is true this is for a type
990 // assertion.
991 static Expression*
992 convert_interface_to_interface(Type* lhs_type,
993 Expression* rhs, bool for_type_guard,
994 Location);
996 // Return a backend expression implementing the comparison LEFT OP RIGHT.
997 // TYPE is the type of both sides.
998 static Bexpression*
999 comparison(Translate_context*, Type* result_type, Operator op,
1000 Expression* left, Expression* right, Location);
1002 // Return the backend expression for the numeric constant VAL.
1003 static Bexpression*
1004 backend_numeric_constant_expression(Translate_context*,
1005 Numeric_constant* val);
1007 // Export the expression. This is only used for constants. It will
1008 // be used for things like values of named constants and sizes of
1009 // arrays.
1010 void
1011 export_expression(Export* exp) const
1012 { this->do_export(exp); }
1014 // Import an expression.
1015 static Expression*
1016 import_expression(Import*);
1018 // Return an expression which checks that VAL, of arbitrary integer type,
1019 // is non-negative and is not more than the maximum integer value.
1020 static Expression*
1021 check_bounds(Expression* val, Location);
1023 // Dump an expression to a dump constext.
1024 void
1025 dump_expression(Ast_dump_context*) const;
1027 protected:
1028 // May be implemented by child class: traverse the expressions.
1029 virtual int
1030 do_traverse(Traverse*);
1032 // Return a lowered expression.
1033 virtual Expression*
1034 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1035 { return this; }
1037 // Return a flattened expression.
1038 virtual Expression*
1039 do_flatten(Gogo*, Named_object*, Statement_inserter*)
1040 { return this; }
1043 // Return whether this is a constant expression.
1044 virtual bool
1045 do_is_constant() const
1046 { return false; }
1048 // Return whether this expression can be used as a constant
1049 // initializer.
1050 virtual bool
1051 do_is_static_initializer() const
1052 { return false; }
1054 // Return whether this is a constant expression of numeric type, and
1055 // set the Numeric_constant to the value.
1056 virtual bool
1057 do_numeric_constant_value(Numeric_constant*) const
1058 { return false; }
1060 // Return whether this is a constant expression of string type, and
1061 // set VAL to the value.
1062 virtual bool
1063 do_string_constant_value(std::string*) const
1064 { return false; }
1066 // Called by the parser if the value is being discarded.
1067 virtual bool
1068 do_discarding_value();
1070 // Child class holds type.
1071 virtual Type*
1072 do_type() = 0;
1074 // Child class implements determining type information.
1075 virtual void
1076 do_determine_type(const Type_context*) = 0;
1078 // Child class implements type checking if needed.
1079 virtual void
1080 do_check_types(Gogo*)
1083 // Child class implements copying.
1084 virtual Expression*
1085 do_copy() = 0;
1087 // Child class implements whether the expression is addressable.
1088 virtual bool
1089 do_is_addressable() const
1090 { return false; }
1092 // Child class implements taking the address of an expression.
1093 virtual void
1094 do_address_taken(bool)
1097 // Child class implements issuing a nil check if the address is taken.
1098 virtual void
1099 do_issue_nil_check()
1102 // Child class implements whether this expression must be evaluated
1103 // in order.
1104 virtual bool
1105 do_must_eval_in_order() const
1106 { return false; }
1108 // Child class implements whether this expressions requires that
1109 // subexpressions be evaluated in order. The child implementation
1110 // may set *SKIP if it should be non-zero.
1111 virtual bool
1112 do_must_eval_subexpressions_in_order(int* /* skip */) const
1113 { return false; }
1115 // Child class implements conversion to backend representation.
1116 virtual Bexpression*
1117 do_get_backend(Translate_context*) = 0;
1119 // Child class implements export.
1120 virtual void
1121 do_export(Export*) const;
1123 // For children to call to give an error for an unused value.
1124 void
1125 unused_value_error();
1127 // For children to call when they detect that they are in error.
1128 void
1129 set_is_error();
1131 // For children to call to report an error conveniently.
1132 void
1133 report_error(const char*);
1135 // Child class implements dumping to a dump context.
1136 virtual void
1137 do_dump_expression(Ast_dump_context*) const = 0;
1139 // Varargs lowering creates a slice object (unnamed compiler temp)
1140 // to contain the variable length collection of values. The enum
1141 // below tells the lowering routine whether it can mark that temp
1142 // as non-escaping or not. For general varargs calls it is not always
1143 // safe to stack-allocated the storage, but for specific cases (ex:
1144 // call to append()) it is legal.
1145 enum Slice_storage_escape_disp
1147 SLICE_STORAGE_MAY_ESCAPE,
1148 SLICE_STORAGE_DOES_NOT_ESCAPE
1151 private:
1152 // Convert to the desired statement classification, or return NULL.
1153 // This is a controlled dynamic cast.
1154 template<typename Expression_class,
1155 Expression_classification expr_classification>
1156 Expression_class*
1157 convert()
1159 return (this->classification_ == expr_classification
1160 ? static_cast<Expression_class*>(this)
1161 : NULL);
1164 template<typename Expression_class,
1165 Expression_classification expr_classification>
1166 const Expression_class*
1167 convert() const
1169 return (this->classification_ == expr_classification
1170 ? static_cast<const Expression_class*>(this)
1171 : NULL);
1174 static Expression*
1175 convert_type_to_interface(Type*, Expression*, Location);
1177 static Expression*
1178 get_interface_type_descriptor(Expression*);
1180 static Expression*
1181 convert_interface_to_type(Type*, Expression*, Location);
1183 // The expression classification.
1184 Expression_classification classification_;
1185 // The location in the input file.
1186 Location location_;
1189 // A list of Expressions.
1191 class Expression_list
1193 public:
1194 Expression_list()
1195 : entries_()
1198 // Return whether the list is empty.
1199 bool
1200 empty() const
1201 { return this->entries_.empty(); }
1203 // Return the number of entries in the list.
1204 size_t
1205 size() const
1206 { return this->entries_.size(); }
1208 // Add an entry to the end of the list.
1209 void
1210 push_back(Expression* expr)
1211 { this->entries_.push_back(expr); }
1213 void
1214 append(Expression_list* add)
1215 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
1217 // Reserve space in the list.
1218 void
1219 reserve(size_t size)
1220 { this->entries_.reserve(size); }
1222 // Traverse the expressions in the list.
1224 traverse(Traverse*);
1226 // Copy the list.
1227 Expression_list*
1228 copy();
1230 // Return true if the list contains an error expression.
1231 bool
1232 contains_error() const;
1234 // Retrieve an element by index.
1235 Expression*&
1236 at(size_t i)
1237 { return this->entries_.at(i); }
1239 // Return the first and last elements.
1240 Expression*&
1241 front()
1242 { return this->entries_.front(); }
1244 Expression*
1245 front() const
1246 { return this->entries_.front(); }
1248 Expression*&
1249 back()
1250 { return this->entries_.back(); }
1252 Expression*
1253 back() const
1254 { return this->entries_.back(); }
1256 // Iterators.
1258 typedef std::vector<Expression*>::iterator iterator;
1259 typedef std::vector<Expression*>::const_iterator const_iterator;
1261 iterator
1262 begin()
1263 { return this->entries_.begin(); }
1265 const_iterator
1266 begin() const
1267 { return this->entries_.begin(); }
1269 iterator
1270 end()
1271 { return this->entries_.end(); }
1273 const_iterator
1274 end() const
1275 { return this->entries_.end(); }
1277 // Erase an entry.
1278 void
1279 erase(iterator p)
1280 { this->entries_.erase(p); }
1282 private:
1283 std::vector<Expression*> entries_;
1286 // An abstract base class for an expression which is only used by the
1287 // parser, and is lowered in the lowering pass.
1289 class Parser_expression : public Expression
1291 public:
1292 Parser_expression(Expression_classification classification,
1293 Location location)
1294 : Expression(classification, location)
1297 protected:
1298 virtual Expression*
1299 do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
1301 Type*
1302 do_type();
1304 void
1305 do_determine_type(const Type_context*)
1306 { go_unreachable(); }
1308 void
1309 do_check_types(Gogo*)
1310 { go_unreachable(); }
1312 Bexpression*
1313 do_get_backend(Translate_context*)
1314 { go_unreachable(); }
1317 // An expression which is simply a variable.
1319 class Var_expression : public Expression
1321 public:
1322 Var_expression(Named_object* variable, Location location)
1323 : Expression(EXPRESSION_VAR_REFERENCE, location),
1324 variable_(variable)
1327 // Return the variable.
1328 Named_object*
1329 named_object() const
1330 { return this->variable_; }
1332 protected:
1333 Expression*
1334 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1336 Type*
1337 do_type();
1339 void
1340 do_determine_type(const Type_context*);
1342 Expression*
1343 do_copy()
1344 { return this; }
1346 bool
1347 do_is_addressable() const
1348 { return true; }
1350 void
1351 do_address_taken(bool);
1353 Bexpression*
1354 do_get_backend(Translate_context*);
1356 void
1357 do_dump_expression(Ast_dump_context*) const;
1359 private:
1360 // The variable we are referencing.
1361 Named_object* variable_;
1364 // A reference to a variable within an enclosing function.
1366 class Enclosed_var_expression : public Expression
1368 public:
1369 Enclosed_var_expression(Expression* reference, Named_object* variable,
1370 Location location)
1371 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE, location),
1372 reference_(reference), variable_(variable)
1375 // The reference to the enclosed variable. This will be an indirection of the
1376 // the field stored within closure variable.
1377 Expression*
1378 reference() const
1379 { return this->reference_; }
1381 // The variable being enclosed and referenced.
1382 Named_object*
1383 variable() const
1384 { return this->variable_; }
1386 protected:
1388 do_traverse(Traverse*);
1390 Expression*
1391 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1393 Expression*
1394 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1396 Type*
1397 do_type()
1398 { return this->reference_->type(); }
1400 void
1401 do_determine_type(const Type_context* context)
1402 { return this->reference_->determine_type(context); }
1404 Expression*
1405 do_copy()
1406 { return this; }
1408 bool
1409 do_is_addressable() const
1410 { return this->reference_->is_addressable(); }
1412 void
1413 do_address_taken(bool escapes);
1415 Bexpression*
1416 do_get_backend(Translate_context* context)
1417 { return this->reference_->get_backend(context); }
1419 void
1420 do_dump_expression(Ast_dump_context*) const;
1422 private:
1423 // The reference to the enclosed variable.
1424 Expression* reference_;
1425 // The variable being enclosed.
1426 Named_object* variable_;
1429 // A reference to a temporary variable.
1431 class Temporary_reference_expression : public Expression
1433 public:
1434 Temporary_reference_expression(Temporary_statement* statement,
1435 Location location)
1436 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1437 statement_(statement), is_lvalue_(false)
1440 // The temporary that this expression refers to.
1441 Temporary_statement*
1442 statement() const
1443 { return this->statement_; }
1445 // Indicate that this reference appears on the left hand side of an
1446 // assignment statement.
1447 void
1448 set_is_lvalue()
1449 { this->is_lvalue_ = true; }
1451 protected:
1452 Type*
1453 do_type();
1455 void
1456 do_determine_type(const Type_context*)
1459 Expression*
1460 do_copy()
1461 { return make_temporary_reference(this->statement_, this->location()); }
1463 bool
1464 do_is_addressable() const
1465 { return true; }
1467 void
1468 do_address_taken(bool);
1470 Bexpression*
1471 do_get_backend(Translate_context*);
1473 void
1474 do_dump_expression(Ast_dump_context*) const;
1476 private:
1477 // The statement where the temporary variable is defined.
1478 Temporary_statement* statement_;
1479 // Whether this reference appears on the left hand side of an
1480 // assignment statement.
1481 bool is_lvalue_;
1484 // Set and use a temporary variable.
1486 class Set_and_use_temporary_expression : public Expression
1488 public:
1489 Set_and_use_temporary_expression(Temporary_statement* statement,
1490 Expression* expr, Location location)
1491 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1492 statement_(statement), expr_(expr)
1495 // Return the temporary.
1496 Temporary_statement*
1497 temporary() const
1498 { return this->statement_; }
1500 // Return the expression.
1501 Expression*
1502 expression() const
1503 { return this->expr_; }
1505 protected:
1507 do_traverse(Traverse* traverse)
1508 { return Expression::traverse(&this->expr_, traverse); }
1510 Type*
1511 do_type();
1513 void
1514 do_determine_type(const Type_context*);
1516 Expression*
1517 do_copy()
1519 return make_set_and_use_temporary(this->statement_, this->expr_,
1520 this->location());
1523 bool
1524 do_is_addressable() const
1525 { return true; }
1527 void
1528 do_address_taken(bool);
1530 Bexpression*
1531 do_get_backend(Translate_context*);
1533 void
1534 do_dump_expression(Ast_dump_context*) const;
1536 private:
1537 // The statement where the temporary variable is defined.
1538 Temporary_statement* statement_;
1539 // The expression to assign to the temporary.
1540 Expression* expr_;
1543 // A string expression.
1545 class String_expression : public Expression
1547 public:
1548 String_expression(const std::string& val, Location location)
1549 : Expression(EXPRESSION_STRING, location),
1550 val_(val), type_(NULL)
1553 const std::string&
1554 val() const
1555 { return this->val_; }
1557 static Expression*
1558 do_import(Import*);
1560 protected:
1561 bool
1562 do_is_constant() const
1563 { return true; }
1565 bool
1566 do_is_static_initializer() const
1567 { return true; }
1569 bool
1570 do_string_constant_value(std::string* val) const
1572 *val = this->val_;
1573 return true;
1576 Type*
1577 do_type();
1579 void
1580 do_determine_type(const Type_context*);
1582 Expression*
1583 do_copy()
1584 { return this; }
1586 Bexpression*
1587 do_get_backend(Translate_context*);
1589 // Write string literal to a string dump.
1590 static void
1591 export_string(String_dump* exp, const String_expression* str);
1593 void
1594 do_export(Export*) const;
1596 void
1597 do_dump_expression(Ast_dump_context*) const;
1599 private:
1600 // The string value. This is immutable.
1601 const std::string val_;
1602 // The type as determined by context.
1603 Type* type_;
1606 // A type conversion expression.
1608 class Type_conversion_expression : public Expression
1610 public:
1611 Type_conversion_expression(Type* type, Expression* expr,
1612 Location location)
1613 : Expression(EXPRESSION_CONVERSION, location),
1614 type_(type), expr_(expr), may_convert_function_types_(false)
1617 // Return the type to which we are converting.
1618 Type*
1619 type() const
1620 { return this->type_; }
1622 // Return the expression which we are converting.
1623 Expression*
1624 expr() const
1625 { return this->expr_; }
1627 // Permit converting from one function type to another. This is
1628 // used internally for method expressions.
1629 void
1630 set_may_convert_function_types()
1632 this->may_convert_function_types_ = true;
1635 // Import a type conversion expression.
1636 static Expression*
1637 do_import(Import*);
1639 protected:
1641 do_traverse(Traverse* traverse);
1643 Expression*
1644 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1646 Expression*
1647 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1649 bool
1650 do_is_constant() const;
1652 bool
1653 do_is_static_initializer() const;
1655 bool
1656 do_numeric_constant_value(Numeric_constant*) const;
1658 bool
1659 do_string_constant_value(std::string*) const;
1661 Type*
1662 do_type()
1663 { return this->type_; }
1665 void
1666 do_determine_type(const Type_context*);
1668 void
1669 do_check_types(Gogo*);
1671 Expression*
1672 do_copy();
1674 Bexpression*
1675 do_get_backend(Translate_context* context);
1677 void
1678 do_export(Export*) const;
1680 void
1681 do_dump_expression(Ast_dump_context*) const;
1683 private:
1684 // The type to convert to.
1685 Type* type_;
1686 // The expression to convert.
1687 Expression* expr_;
1688 // True if this is permitted to convert function types. This is
1689 // used internally for method expressions.
1690 bool may_convert_function_types_;
1693 // An unsafe type conversion, used to pass values to builtin functions.
1695 class Unsafe_type_conversion_expression : public Expression
1697 public:
1698 Unsafe_type_conversion_expression(Type* type, Expression* expr,
1699 Location location)
1700 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
1701 type_(type), expr_(expr)
1704 Expression*
1705 expr() const
1706 { return this->expr_; }
1708 protected:
1710 do_traverse(Traverse* traverse);
1712 bool
1713 do_is_static_initializer() const;
1715 Type*
1716 do_type()
1717 { return this->type_; }
1719 void
1720 do_determine_type(const Type_context*)
1721 { this->expr_->determine_type_no_context(); }
1723 Expression*
1724 do_copy();
1726 Bexpression*
1727 do_get_backend(Translate_context*);
1729 void
1730 do_dump_expression(Ast_dump_context*) const;
1732 private:
1733 // The type to convert to.
1734 Type* type_;
1735 // The expression to convert.
1736 Expression* expr_;
1739 // A Unary expression.
1741 class Unary_expression : public Expression
1743 public:
1744 Unary_expression(Operator op, Expression* expr, Location location)
1745 : Expression(EXPRESSION_UNARY, location),
1746 op_(op), escapes_(true), create_temp_(false), is_gc_root_(false),
1747 is_slice_init_(false), expr_(expr),
1748 issue_nil_check_(NIL_CHECK_DEFAULT)
1751 // Return the operator.
1752 Operator
1753 op() const
1754 { return this->op_; }
1756 // Return the operand.
1757 Expression*
1758 operand() const
1759 { return this->expr_; }
1761 // Record that an address expression does not escape.
1762 void
1763 set_does_not_escape()
1765 go_assert(this->op_ == OPERATOR_AND);
1766 this->escapes_ = false;
1769 // Record that this is an address expression which should create a
1770 // temporary variable if necessary. This is used for method calls.
1771 void
1772 set_create_temp()
1774 go_assert(this->op_ == OPERATOR_AND);
1775 this->create_temp_ = true;
1778 // Record that this is an address expression of a GC root, which is a
1779 // mutable composite literal. This used for registering GC variables.
1780 void
1781 set_is_gc_root()
1783 go_assert(this->op_ == OPERATOR_AND);
1784 this->is_gc_root_ = true;
1787 // Record that this is an address expression of a slice value initializer,
1788 // which is mutable if the values are not copied to the heap.
1789 void
1790 set_is_slice_init()
1792 go_assert(this->op_ == OPERATOR_AND);
1793 this->is_slice_init_ = true;
1796 // Call the address_taken method on the operand if necessary.
1797 void
1798 check_operand_address_taken(Gogo*);
1800 // Apply unary opcode OP to UNC, setting NC. Return true if this
1801 // could be done, false if not. On overflow, issues an error and
1802 // sets *ISSUED_ERROR.
1803 static bool
1804 eval_constant(Operator op, const Numeric_constant* unc,
1805 Location, Numeric_constant* nc, bool *issued_error);
1807 static Expression*
1808 do_import(Import*);
1810 // Declare that this deref does or does not require an explicit nil check.
1811 void
1812 set_requires_nil_check(bool needed)
1814 go_assert(this->op_ == OPERATOR_MULT);
1815 if (needed)
1816 this->issue_nil_check_ = NIL_CHECK_NEEDED;
1817 else
1818 this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED;
1821 protected:
1823 do_traverse(Traverse* traverse)
1824 { return Expression::traverse(&this->expr_, traverse); }
1826 Expression*
1827 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1829 Expression*
1830 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1832 bool
1833 do_is_constant() const;
1835 bool
1836 do_is_static_initializer() const;
1838 bool
1839 do_numeric_constant_value(Numeric_constant*) const;
1841 Type*
1842 do_type();
1844 void
1845 do_determine_type(const Type_context*);
1847 void
1848 do_check_types(Gogo*);
1850 Expression*
1851 do_copy()
1853 return Expression::make_unary(this->op_, this->expr_->copy(),
1854 this->location());
1857 bool
1858 do_must_eval_subexpressions_in_order(int*) const
1859 { return this->op_ == OPERATOR_MULT; }
1861 bool
1862 do_is_addressable() const
1863 { return this->op_ == OPERATOR_MULT; }
1865 Bexpression*
1866 do_get_backend(Translate_context*);
1868 void
1869 do_export(Export*) const;
1871 void
1872 do_dump_expression(Ast_dump_context*) const;
1874 void
1875 do_issue_nil_check()
1877 if (this->op_ == OPERATOR_MULT)
1878 this->set_requires_nil_check(true);
1881 private:
1882 static bool
1883 base_is_static_initializer(Expression*);
1885 // Return a determination as to whether this dereference expression
1886 // requires a nil check.
1887 Nil_check_classification
1888 requires_nil_check(Gogo*);
1890 // The unary operator to apply.
1891 Operator op_;
1892 // Normally true. False if this is an address expression which does
1893 // not escape the current function.
1894 bool escapes_;
1895 // True if this is an address expression which should create a
1896 // temporary variable if necessary.
1897 bool create_temp_;
1898 // True if this is an address expression for a GC root. A GC root is a
1899 // special struct composite literal that is mutable when addressed, meaning
1900 // it cannot be represented as an immutable_struct in the backend.
1901 bool is_gc_root_;
1902 // True if this is an address expression for a slice value with an immutable
1903 // initializer. The initializer for a slice's value pointer has an array
1904 // type, meaning it cannot be represented as an immutable_struct in the
1905 // backend.
1906 bool is_slice_init_;
1907 // The operand.
1908 Expression* expr_;
1909 // Whether or not to issue a nil check for this expression if its address
1910 // is being taken.
1911 Nil_check_classification issue_nil_check_;
1914 // A binary expression.
1916 class Binary_expression : public Expression
1918 public:
1919 Binary_expression(Operator op, Expression* left, Expression* right,
1920 Location location)
1921 : Expression(EXPRESSION_BINARY, location),
1922 op_(op), left_(left), right_(right), type_(NULL)
1925 // Return the operator.
1926 Operator
1927 op()
1928 { return this->op_; }
1930 // Return the left hand expression.
1931 Expression*
1932 left()
1933 { return this->left_; }
1935 // Return the right hand expression.
1936 Expression*
1937 right()
1938 { return this->right_; }
1940 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1941 // Return true if this could be done, false if not. Issue errors at
1942 // LOCATION as appropriate, and sets *ISSUED_ERROR if it did.
1943 static bool
1944 eval_constant(Operator op, Numeric_constant* left_nc,
1945 Numeric_constant* right_nc, Location location,
1946 Numeric_constant* nc, bool* issued_error);
1948 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1949 // *RESULT. Return true if this could be done, false if not. Issue
1950 // errors at LOCATION as appropriate.
1951 static bool
1952 compare_constant(Operator op, Numeric_constant* left_nc,
1953 Numeric_constant* right_nc, Location location,
1954 bool* result);
1956 static Expression*
1957 do_import(Import*);
1959 // Report an error if OP can not be applied to TYPE. Return whether
1960 // it can. OTYPE is the type of the other operand.
1961 static bool
1962 check_operator_type(Operator op, Type* type, Type* otype, Location);
1964 // Set *RESULT_TYPE to the resulting type when OP is applied to
1965 // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
1966 // success, false on failure.
1967 static bool
1968 operation_type(Operator op, Type* left_type, Type* right_type,
1969 Type** result_type);
1971 protected:
1973 do_traverse(Traverse* traverse);
1975 Expression*
1976 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1978 Expression*
1979 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1981 bool
1982 do_is_constant() const
1983 { return this->left_->is_constant() && this->right_->is_constant(); }
1985 bool
1986 do_is_static_initializer() const;
1988 bool
1989 do_numeric_constant_value(Numeric_constant*) const;
1991 bool
1992 do_discarding_value();
1994 Type*
1995 do_type();
1997 void
1998 do_determine_type(const Type_context*);
2000 void
2001 do_check_types(Gogo*);
2003 Expression*
2004 do_copy()
2006 return Expression::make_binary(this->op_, this->left_->copy(),
2007 this->right_->copy(), this->location());
2010 Bexpression*
2011 do_get_backend(Translate_context*);
2013 void
2014 do_export(Export*) const;
2016 void
2017 do_dump_expression(Ast_dump_context*) const;
2019 private:
2020 static bool
2021 cmp_to_bool(Operator op, int cmp);
2023 static bool
2024 eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
2025 Location, Numeric_constant*);
2027 static bool
2028 eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
2029 Location, Numeric_constant*);
2031 static bool
2032 eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
2033 Location, Numeric_constant*);
2035 static bool
2036 compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
2038 static bool
2039 compare_float(const Numeric_constant*, const Numeric_constant *, int*);
2041 static bool
2042 compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
2044 Expression*
2045 lower_struct_comparison(Gogo*, Statement_inserter*);
2047 Expression*
2048 lower_array_comparison(Gogo*, Statement_inserter*);
2050 Expression*
2051 lower_interface_value_comparison(Gogo*, Statement_inserter*);
2053 Expression*
2054 lower_compare_to_memcmp(Gogo*, Statement_inserter*);
2056 Expression*
2057 operand_address(Statement_inserter*, Expression*);
2059 // The binary operator to apply.
2060 Operator op_;
2061 // The left hand side operand.
2062 Expression* left_;
2063 // The right hand side operand.
2064 Expression* right_;
2065 // The type of a comparison operation.
2066 Type* type_;
2069 // A string concatenation expression. This is a sequence of strings
2070 // added together. It is created when lowering Binary_expression.
2072 class String_concat_expression : public Expression
2074 public:
2075 String_concat_expression(Expression_list* exprs)
2076 : Expression(EXPRESSION_STRING_CONCAT, exprs->front()->location()),
2077 exprs_(exprs)
2080 // Return the list of string expressions to be concatenated.
2081 Expression_list*
2082 exprs()
2083 { return this->exprs_; }
2085 protected:
2087 do_traverse(Traverse* traverse)
2088 { return this->exprs_->traverse(traverse); }
2090 Expression*
2091 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2092 { return this; }
2094 Expression*
2095 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2097 bool
2098 do_is_constant() const;
2100 bool
2101 do_is_static_initializer() const;
2103 Type*
2104 do_type();
2106 void
2107 do_determine_type(const Type_context*);
2109 void
2110 do_check_types(Gogo*);
2112 Expression*
2113 do_copy()
2114 { return Expression::make_string_concat(this->exprs_->copy()); }
2116 Bexpression*
2117 do_get_backend(Translate_context*)
2118 { go_unreachable(); }
2120 void
2121 do_export(Export*) const
2122 { go_unreachable(); }
2124 void
2125 do_dump_expression(Ast_dump_context*) const;
2127 private:
2128 // The string expressions to concatenate.
2129 Expression_list* exprs_;
2132 // A call expression. The go statement needs to dig inside this.
2134 class Call_expression : public Expression
2136 public:
2137 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
2138 Location location)
2139 : Expression(EXPRESSION_CALL, location),
2140 fn_(fn), args_(args), type_(NULL), call_(NULL), call_temp_(NULL)
2141 , expected_result_count_(0), is_varargs_(is_varargs),
2142 varargs_are_lowered_(false), types_are_determined_(false),
2143 is_deferred_(false), is_concurrent_(false), issued_error_(false),
2144 is_multi_value_arg_(false), is_flattened_(false)
2147 // The function to call.
2148 Expression*
2149 fn() const
2150 { return this->fn_; }
2152 // The arguments.
2153 Expression_list*
2154 args()
2155 { return this->args_; }
2157 const Expression_list*
2158 args() const
2159 { return this->args_; }
2161 // Get the function type.
2162 Function_type*
2163 get_function_type() const;
2165 // Return the number of values this call will return.
2166 size_t
2167 result_count() const;
2169 // Return the temporary variable that holds the results. This is
2170 // only valid after the expression has been lowered, and is only
2171 // valid for calls which return multiple results.
2172 Temporary_statement*
2173 results() const;
2175 // Set the number of results expected from this call. This is used
2176 // when the call appears in a context that expects multiple results,
2177 // such as a, b = f().
2178 void
2179 set_expected_result_count(size_t);
2181 // Return whether this is a call to the predeclared function
2182 // recover.
2183 bool
2184 is_recover_call() const;
2186 // Set the argument for a call to recover.
2187 void
2188 set_recover_arg(Expression*);
2190 // Whether the last argument is a varargs argument (f(a...)).
2191 bool
2192 is_varargs() const
2193 { return this->is_varargs_; }
2195 // Return whether varargs have already been lowered.
2196 bool
2197 varargs_are_lowered() const
2198 { return this->varargs_are_lowered_; }
2200 // Note that varargs have already been lowered.
2201 void
2202 set_varargs_are_lowered()
2203 { this->varargs_are_lowered_ = true; }
2205 // Whether this call is being deferred.
2206 bool
2207 is_deferred() const
2208 { return this->is_deferred_; }
2210 // Note that the call is being deferred.
2211 void
2212 set_is_deferred()
2213 { this->is_deferred_ = true; }
2215 // Whether this call is concurrently executed.
2216 bool
2217 is_concurrent() const
2218 { return this->is_concurrent_; }
2220 // Note that the call is concurrently executed.
2221 void
2222 set_is_concurrent()
2223 { this->is_concurrent_ = true; }
2225 // We have found an error with this call expression; return true if
2226 // we should report it.
2227 bool
2228 issue_error();
2230 // Whether or not this call contains errors, either in the call or the
2231 // arguments to the call.
2232 bool
2233 is_erroneous_call();
2235 // Whether this call returns multiple results that are used as an
2236 // multi-valued argument.
2237 bool
2238 is_multi_value_arg() const
2239 { return this->is_multi_value_arg_; }
2241 // Note this call is used as a multi-valued argument.
2242 void
2243 set_is_multi_value_arg()
2244 { this->is_multi_value_arg_ = true; }
2246 // Whether this is a call to builtin function.
2247 virtual bool
2248 is_builtin()
2249 { return false; }
2251 // Convert to a Builtin_call_expression, or return NULL.
2252 inline Builtin_call_expression*
2253 builtin_call_expression();
2255 protected:
2257 do_traverse(Traverse*);
2259 virtual Expression*
2260 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2262 virtual Expression*
2263 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2265 bool
2266 do_discarding_value()
2267 { return true; }
2269 virtual Type*
2270 do_type();
2272 virtual void
2273 do_determine_type(const Type_context*);
2275 virtual void
2276 do_check_types(Gogo*);
2278 Expression*
2279 do_copy();
2281 bool
2282 do_must_eval_in_order() const;
2284 virtual Bexpression*
2285 do_get_backend(Translate_context*);
2287 virtual bool
2288 do_is_recover_call() const;
2290 virtual void
2291 do_set_recover_arg(Expression*);
2293 // Let a builtin expression change the argument list.
2294 void
2295 set_args(Expression_list* args)
2296 { this->args_ = args; }
2298 // Let a builtin expression lower varargs.
2299 void
2300 lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
2301 Type* varargs_type, size_t param_count,
2302 Slice_storage_escape_disp escape_disp);
2304 // Let a builtin expression check whether types have been
2305 // determined.
2306 bool
2307 determining_types();
2309 void
2310 do_dump_expression(Ast_dump_context*) const;
2312 private:
2313 bool
2314 check_argument_type(int, const Type*, const Type*, Location, bool);
2316 Expression*
2317 lower_to_builtin(Named_object**, const char*, int);
2319 Expression*
2320 interface_method_function(Interface_field_reference_expression*,
2321 Expression**, Location);
2323 Bexpression*
2324 set_results(Translate_context*);
2326 // The function to call.
2327 Expression* fn_;
2328 // The arguments to pass. This may be NULL if there are no
2329 // arguments.
2330 Expression_list* args_;
2331 // The type of the expression, to avoid recomputing it.
2332 Type* type_;
2333 // The backend expression for the call, used for a call which returns a tuple.
2334 Bexpression* call_;
2335 // A temporary variable to store this call if the function returns a tuple.
2336 Temporary_statement* call_temp_;
2337 // If not 0, the number of results expected from this call, when
2338 // used in a context that expects multiple values.
2339 size_t expected_result_count_;
2340 // True if the last argument is a varargs argument (f(a...)).
2341 bool is_varargs_;
2342 // True if varargs have already been lowered.
2343 bool varargs_are_lowered_;
2344 // True if types have been determined.
2345 bool types_are_determined_;
2346 // True if the call is an argument to a defer statement.
2347 bool is_deferred_;
2348 // True if the call is an argument to a go statement.
2349 bool is_concurrent_;
2350 // True if we reported an error about a mismatch between call
2351 // results and uses. This is to avoid producing multiple errors
2352 // when there are multiple Call_result_expressions.
2353 bool issued_error_;
2354 // True if this call is used as an argument that returns multiple results.
2355 bool is_multi_value_arg_;
2356 // True if this expression has already been flattened.
2357 bool is_flattened_;
2360 // A call expression to a builtin function.
2362 class Builtin_call_expression : public Call_expression
2364 public:
2365 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
2366 bool is_varargs, Location location);
2368 // The builtin functions.
2369 enum Builtin_function_code
2371 BUILTIN_INVALID,
2373 // Predeclared builtin functions.
2374 BUILTIN_APPEND,
2375 BUILTIN_CAP,
2376 BUILTIN_CLOSE,
2377 BUILTIN_COMPLEX,
2378 BUILTIN_COPY,
2379 BUILTIN_DELETE,
2380 BUILTIN_IMAG,
2381 BUILTIN_LEN,
2382 BUILTIN_MAKE,
2383 BUILTIN_NEW,
2384 BUILTIN_PANIC,
2385 BUILTIN_PRINT,
2386 BUILTIN_PRINTLN,
2387 BUILTIN_REAL,
2388 BUILTIN_RECOVER,
2390 // Builtin functions from the unsafe package.
2391 BUILTIN_ALIGNOF,
2392 BUILTIN_OFFSETOF,
2393 BUILTIN_SIZEOF
2396 Builtin_function_code
2397 code()
2398 { return this->code_; }
2400 // This overrides Call_expression::is_builtin.
2401 bool
2402 is_builtin()
2403 { return true; }
2405 // Return whether EXPR, of array type, is a constant if passed to
2406 // len or cap.
2407 static bool
2408 array_len_is_constant(Expression* expr);
2410 Expression*
2411 flatten_append(Gogo*, Named_object*, Statement_inserter*, Expression*,
2412 Block*);
2414 protected:
2415 // This overrides Call_expression::do_lower.
2416 Expression*
2417 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2419 Expression*
2420 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2422 bool
2423 do_is_constant() const;
2425 bool
2426 do_numeric_constant_value(Numeric_constant*) const;
2428 bool
2429 do_discarding_value();
2431 Type*
2432 do_type();
2434 void
2435 do_determine_type(const Type_context*);
2437 void
2438 do_check_types(Gogo*);
2440 Expression*
2441 do_copy();
2443 Bexpression*
2444 do_get_backend(Translate_context*);
2446 void
2447 do_export(Export*) const;
2449 virtual bool
2450 do_is_recover_call() const;
2452 virtual void
2453 do_set_recover_arg(Expression*);
2455 private:
2456 Expression*
2457 one_arg() const;
2459 bool
2460 check_one_arg();
2462 static Type*
2463 real_imag_type(Type*);
2465 static Type*
2466 complex_type(Type*);
2468 Expression*
2469 lower_make(Statement_inserter*);
2471 bool
2472 check_int_value(Expression*, bool is_length, bool* small);
2474 // A pointer back to the general IR structure. This avoids a global
2475 // variable, or passing it around everywhere.
2476 Gogo* gogo_;
2477 // The builtin function being called.
2478 Builtin_function_code code_;
2479 // Used to stop endless loops when the length of an array uses len
2480 // or cap of the array itself.
2481 mutable bool seen_;
2482 // Whether the argument is set for calls to BUILTIN_RECOVER.
2483 bool recover_arg_is_set_;
2486 inline Builtin_call_expression*
2487 Call_expression::builtin_call_expression()
2489 return (this->is_builtin()
2490 ? static_cast<Builtin_call_expression*>(this)
2491 : NULL);
2494 // A single result from a call which returns multiple results.
2496 class Call_result_expression : public Expression
2498 public:
2499 Call_result_expression(Call_expression* call, unsigned int index)
2500 : Expression(EXPRESSION_CALL_RESULT, call->location()),
2501 call_(call), index_(index)
2504 Expression*
2505 call() const
2506 { return this->call_; }
2508 unsigned int
2509 index() const
2510 { return this->index_; }
2512 protected:
2514 do_traverse(Traverse*);
2516 Type*
2517 do_type();
2519 void
2520 do_determine_type(const Type_context*);
2522 void
2523 do_check_types(Gogo*);
2525 Expression*
2526 do_copy()
2528 return new Call_result_expression(this->call_->call_expression(),
2529 this->index_);
2532 bool
2533 do_must_eval_in_order() const
2534 { return true; }
2536 Bexpression*
2537 do_get_backend(Translate_context*);
2539 void
2540 do_dump_expression(Ast_dump_context*) const;
2542 private:
2543 // The underlying call expression.
2544 Expression* call_;
2545 // Which result we want.
2546 unsigned int index_;
2549 // An expression which represents a pointer to a function.
2551 class Func_expression : public Expression
2553 public:
2554 Func_expression(Named_object* function, Expression* closure,
2555 Location location)
2556 : Expression(EXPRESSION_FUNC_REFERENCE, location),
2557 function_(function), closure_(closure),
2558 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS)
2561 // Return the object associated with the function.
2562 Named_object*
2563 named_object() const
2564 { return this->function_; }
2566 // Return the closure for this function. This will return NULL if
2567 // the function has no closure, which is the normal case.
2568 Expression*
2569 closure()
2570 { return this->closure_; }
2572 // Return whether this is a reference to a runtime function.
2573 bool
2574 is_runtime_function() const
2575 { return this->runtime_code_ != Runtime::NUMBER_OF_FUNCTIONS; }
2577 // Return the runtime code for this function expression.
2578 // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
2579 // runtime function.
2580 Runtime::Function
2581 runtime_code() const
2582 { return this->runtime_code_; }
2584 // Set the runtime code for this function expression.
2585 void
2586 set_runtime_code(Runtime::Function code)
2587 { this->runtime_code_ = code; }
2589 // Return a backend expression for the code of a function.
2590 static Bexpression*
2591 get_code_pointer(Gogo*, Named_object* function, Location loc);
2593 protected:
2595 do_traverse(Traverse*);
2597 Type*
2598 do_type();
2600 void
2601 do_determine_type(const Type_context*)
2603 if (this->closure_ != NULL)
2604 this->closure_->determine_type_no_context();
2607 Expression*
2608 do_copy()
2610 return Expression::make_func_reference(this->function_,
2611 (this->closure_ == NULL
2612 ? NULL
2613 : this->closure_->copy()),
2614 this->location());
2617 Bexpression*
2618 do_get_backend(Translate_context*);
2620 void
2621 do_dump_expression(Ast_dump_context*) const;
2623 private:
2624 // The function itself.
2625 Named_object* function_;
2626 // A closure. This is normally NULL. For a nested function, it may
2627 // be a struct holding pointers to all the variables referenced by
2628 // this function and defined in enclosing functions.
2629 Expression* closure_;
2630 // The runtime code for the referenced function.
2631 Runtime::Function runtime_code_;
2634 // A function descriptor. A function descriptor is a struct with a
2635 // single field pointing to the function code. This is used for
2636 // functions without closures.
2638 class Func_descriptor_expression : public Expression
2640 public:
2641 Func_descriptor_expression(Named_object* fn);
2643 // Make the function descriptor type, so that it can be converted.
2644 static void
2645 make_func_descriptor_type();
2647 protected:
2649 do_traverse(Traverse*);
2651 Type*
2652 do_type();
2654 void
2655 do_determine_type(const Type_context*)
2658 Expression*
2659 do_copy()
2660 { return Expression::make_func_descriptor(this->fn_); }
2662 bool
2663 do_is_addressable() const
2664 { return true; }
2666 Bexpression*
2667 do_get_backend(Translate_context*);
2669 void
2670 do_dump_expression(Ast_dump_context* context) const;
2672 private:
2673 // The type of all function descriptors.
2674 static Type* descriptor_type;
2676 // The function for which this is the descriptor.
2677 Named_object* fn_;
2678 // The descriptor variable.
2679 Bvariable* dvar_;
2682 // A reference to an unknown name.
2684 class Unknown_expression : public Parser_expression
2686 public:
2687 Unknown_expression(Named_object* named_object, Location location)
2688 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
2689 named_object_(named_object), no_error_message_(false),
2690 is_composite_literal_key_(false)
2693 // The associated named object.
2694 Named_object*
2695 named_object() const
2696 { return this->named_object_; }
2698 // The name of the identifier which was unknown.
2699 const std::string&
2700 name() const;
2702 // Call this to indicate that we should not give an error if this
2703 // name is never defined. This is used to avoid knock-on errors
2704 // during an erroneous parse.
2705 void
2706 set_no_error_message()
2707 { this->no_error_message_ = true; }
2709 // Note that this expression is being used as the key in a composite
2710 // literal, so it may be OK if it is not resolved.
2711 void
2712 set_is_composite_literal_key()
2713 { this->is_composite_literal_key_ = true; }
2715 // Note that this expression should no longer be treated as a
2716 // composite literal key.
2717 void
2718 clear_is_composite_literal_key()
2719 { this->is_composite_literal_key_ = false; }
2721 protected:
2722 Expression*
2723 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2725 Expression*
2726 do_copy()
2727 { return new Unknown_expression(this->named_object_, this->location()); }
2729 void
2730 do_dump_expression(Ast_dump_context*) const;
2732 private:
2733 // The unknown name.
2734 Named_object* named_object_;
2735 // True if we should not give errors if this is undefined. This is
2736 // used if there was a parse failure.
2737 bool no_error_message_;
2738 // True if this is the key in a composite literal.
2739 bool is_composite_literal_key_;
2742 // An index expression. This is lowered to an array index, a string
2743 // index, or a map index.
2745 class Index_expression : public Parser_expression
2747 public:
2748 Index_expression(Expression* left, Expression* start, Expression* end,
2749 Expression* cap, Location location)
2750 : Parser_expression(EXPRESSION_INDEX, location),
2751 left_(left), start_(start), end_(end), cap_(cap)
2754 // Dump an index expression, i.e. an expression of the form
2755 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
2756 static void
2757 dump_index_expression(Ast_dump_context*, const Expression* expr,
2758 const Expression* start, const Expression* end,
2759 const Expression* cap);
2761 protected:
2763 do_traverse(Traverse*);
2765 Expression*
2766 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2768 Expression*
2769 do_copy()
2771 return new Index_expression(this->left_->copy(), this->start_->copy(),
2772 (this->end_ == NULL
2773 ? NULL
2774 : this->end_->copy()),
2775 (this->cap_ == NULL
2776 ? NULL
2777 : this->cap_->copy()),
2778 this->location());
2781 // This shouldn't be called--we don't know yet.
2782 bool
2783 do_must_eval_subexpressions_in_order(int*) const
2784 { go_unreachable(); }
2786 void
2787 do_dump_expression(Ast_dump_context*) const;
2789 void
2790 do_issue_nil_check()
2791 { this->left_->issue_nil_check(); }
2792 private:
2793 // The expression being indexed.
2794 Expression* left_;
2795 // The first index.
2796 Expression* start_;
2797 // The second index. This is NULL for an index, non-NULL for a
2798 // slice.
2799 Expression* end_;
2800 // The capacity argument. This is NULL for indices and slices that use the
2801 // default capacity, non-NULL for indices and slices that specify the
2802 // capacity.
2803 Expression* cap_;
2806 // An array index. This is used for both indexing and slicing.
2808 class Array_index_expression : public Expression
2810 public:
2811 Array_index_expression(Expression* array, Expression* start,
2812 Expression* end, Expression* cap, Location location)
2813 : Expression(EXPRESSION_ARRAY_INDEX, location),
2814 array_(array), start_(start), end_(end), cap_(cap), type_(NULL),
2815 is_lvalue_(false)
2818 // Return the array.
2819 Expression*
2820 array()
2821 { return this->array_; }
2823 const Expression*
2824 array() const
2825 { return this->array_; }
2827 // Return the index of a simple index expression, or the start index
2828 // of a slice expression.
2829 Expression*
2830 start()
2831 { return this->start_; }
2833 const Expression*
2834 start() const
2835 { return this->start_; }
2837 // Return the end index of a slice expression. This is NULL for a
2838 // simple index expression.
2839 Expression*
2840 end()
2841 { return this->end_; }
2843 const Expression*
2844 end() const
2845 { return this->end_; }
2847 // Return whether this array index expression appears in an lvalue
2848 // (left hand side of assignment) context.
2849 bool
2850 is_lvalue() const
2851 { return this->is_lvalue_; }
2853 // Update this array index expression to indicate that it appears
2854 // in a left-hand-side or lvalue context.
2855 void
2856 set_is_lvalue()
2857 { this->is_lvalue_ = true; }
2859 protected:
2861 do_traverse(Traverse*);
2863 Expression*
2864 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2866 Type*
2867 do_type();
2869 void
2870 do_determine_type(const Type_context*);
2872 void
2873 do_check_types(Gogo*);
2875 Expression*
2876 do_copy()
2878 return Expression::make_array_index(this->array_->copy(),
2879 this->start_->copy(),
2880 (this->end_ == NULL
2881 ? NULL
2882 : this->end_->copy()),
2883 (this->cap_ == NULL
2884 ? NULL
2885 : this->cap_->copy()),
2886 this->location());
2889 bool
2890 do_must_eval_subexpressions_in_order(int* skip) const;
2892 bool
2893 do_is_addressable() const;
2895 void
2896 do_address_taken(bool escapes);
2898 void
2899 do_issue_nil_check()
2900 { this->array_->issue_nil_check(); }
2902 Bexpression*
2903 do_get_backend(Translate_context*);
2905 void
2906 do_dump_expression(Ast_dump_context*) const;
2908 private:
2909 // The array we are getting a value from.
2910 Expression* array_;
2911 // The start or only index.
2912 Expression* start_;
2913 // The end index of a slice. This may be NULL for a simple array
2914 // index, or it may be a nil expression for the length of the array.
2915 Expression* end_;
2916 // The capacity argument of a slice. This may be NULL for an array index or
2917 // slice.
2918 Expression* cap_;
2919 // The type of the expression.
2920 Type* type_;
2921 // Whether expr appears in an lvalue context.
2922 bool is_lvalue_;
2925 // A string index. This is used for both indexing and slicing.
2927 class String_index_expression : public Expression
2929 public:
2930 String_index_expression(Expression* string, Expression* start,
2931 Expression* end, Location location)
2932 : Expression(EXPRESSION_STRING_INDEX, location),
2933 string_(string), start_(start), end_(end)
2936 // Return the string being indexed.
2937 Expression*
2938 string() const
2939 { return this->string_; }
2941 protected:
2943 do_traverse(Traverse*);
2945 Expression*
2946 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2948 Type*
2949 do_type();
2951 void
2952 do_determine_type(const Type_context*);
2954 void
2955 do_check_types(Gogo*);
2957 Expression*
2958 do_copy()
2960 return Expression::make_string_index(this->string_->copy(),
2961 this->start_->copy(),
2962 (this->end_ == NULL
2963 ? NULL
2964 : this->end_->copy()),
2965 this->location());
2968 bool
2969 do_must_eval_subexpressions_in_order(int*) const
2970 { return true; }
2972 Bexpression*
2973 do_get_backend(Translate_context*);
2975 void
2976 do_dump_expression(Ast_dump_context*) const;
2978 private:
2979 // The string we are getting a value from.
2980 Expression* string_;
2981 // The start or only index.
2982 Expression* start_;
2983 // The end index of a slice. This may be NULL for a single index,
2984 // or it may be a nil expression for the length of the string.
2985 Expression* end_;
2988 // An index into a map.
2990 class Map_index_expression : public Expression
2992 public:
2993 Map_index_expression(Expression* map, Expression* index,
2994 Location location)
2995 : Expression(EXPRESSION_MAP_INDEX, location),
2996 map_(map), index_(index), value_pointer_(NULL)
2999 // Return the map.
3000 Expression*
3001 map()
3002 { return this->map_; }
3004 const Expression*
3005 map() const
3006 { return this->map_; }
3008 // Return the index.
3009 Expression*
3010 index()
3011 { return this->index_; }
3013 const Expression*
3014 index() const
3015 { return this->index_; }
3017 // Get the type of the map being indexed.
3018 Map_type*
3019 get_map_type() const;
3021 // Return an expression for the map index. This returns an
3022 // expression that evaluates to a pointer to a value in the map. If
3023 // the key is not present in the map, this will return a pointer to
3024 // the zero value.
3025 Expression*
3026 get_value_pointer(Gogo*);
3028 protected:
3030 do_traverse(Traverse*);
3032 Expression*
3033 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3035 Type*
3036 do_type();
3038 void
3039 do_determine_type(const Type_context*);
3041 void
3042 do_check_types(Gogo*);
3044 Expression*
3045 do_copy()
3047 return Expression::make_map_index(this->map_->copy(),
3048 this->index_->copy(),
3049 this->location());
3052 bool
3053 do_must_eval_subexpressions_in_order(int*) const
3054 { return true; }
3056 // A map index expression is an lvalue but it is not addressable.
3058 Bexpression*
3059 do_get_backend(Translate_context*);
3061 void
3062 do_dump_expression(Ast_dump_context*) const;
3064 private:
3065 // The map we are looking into.
3066 Expression* map_;
3067 // The index.
3068 Expression* index_;
3069 // A pointer to the value at this index.
3070 Expression* value_pointer_;
3073 // An expression which represents a method bound to its first
3074 // argument.
3076 class Bound_method_expression : public Expression
3078 public:
3079 Bound_method_expression(Expression* expr, const Method *method,
3080 Named_object* function, Location location)
3081 : Expression(EXPRESSION_BOUND_METHOD, location),
3082 expr_(expr), expr_type_(NULL), method_(method), function_(function)
3085 // Return the object which is the first argument.
3086 Expression*
3087 first_argument()
3088 { return this->expr_; }
3090 // Return the implicit type of the first argument. This will be
3091 // non-NULL when using a method from an anonymous field without
3092 // using an explicit stub.
3093 Type*
3094 first_argument_type() const
3095 { return this->expr_type_; }
3097 // Return the method.
3098 const Method*
3099 method() const
3100 { return this->method_; }
3102 // Return the function to call.
3103 Named_object*
3104 function() const
3105 { return this->function_; }
3107 // Set the implicit type of the expression.
3108 void
3109 set_first_argument_type(Type* type)
3110 { this->expr_type_ = type; }
3112 // Create a thunk to call FUNCTION, for METHOD, when it is used as
3113 // part of a method value.
3114 static Named_object*
3115 create_thunk(Gogo*, const Method* method, Named_object* function);
3117 protected:
3119 do_traverse(Traverse*);
3121 Expression*
3122 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3124 Type*
3125 do_type();
3127 void
3128 do_determine_type(const Type_context*);
3130 void
3131 do_check_types(Gogo*);
3133 Expression*
3134 do_copy()
3136 return new Bound_method_expression(this->expr_->copy(), this->method_,
3137 this->function_, this->location());
3140 Bexpression*
3141 do_get_backend(Translate_context*)
3142 { go_unreachable(); }
3144 void
3145 do_dump_expression(Ast_dump_context*) const;
3147 private:
3148 // A mapping from method functions to the thunks we have created for
3149 // them.
3150 typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
3151 static Method_value_thunks method_value_thunks;
3153 // The object used to find the method. This is passed to the method
3154 // as the first argument.
3155 Expression* expr_;
3156 // The implicit type of the object to pass to the method. This is
3157 // NULL in the normal case, non-NULL when using a method from an
3158 // anonymous field which does not require a stub.
3159 Type* expr_type_;
3160 // The method.
3161 const Method* method_;
3162 // The function to call. This is not the same as
3163 // method_->named_object() when the method has a stub. This will be
3164 // the real function rather than the stub.
3165 Named_object* function_;
3168 // A reference to a field in a struct.
3170 class Field_reference_expression : public Expression
3172 public:
3173 Field_reference_expression(Expression* expr, unsigned int field_index,
3174 Location location)
3175 : Expression(EXPRESSION_FIELD_REFERENCE, location),
3176 expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
3179 // Return the struct expression.
3180 Expression*
3181 expr() const
3182 { return this->expr_; }
3184 // Return the field index.
3185 unsigned int
3186 field_index() const
3187 { return this->field_index_; }
3189 // Return whether this node was implied by an anonymous field.
3190 bool
3191 implicit() const
3192 { return this->implicit_; }
3194 void
3195 set_implicit(bool implicit)
3196 { this->implicit_ = implicit; }
3198 // Set the struct expression. This is used when parsing.
3199 void
3200 set_struct_expression(Expression* expr)
3202 go_assert(this->expr_ == NULL);
3203 this->expr_ = expr;
3206 protected:
3208 do_traverse(Traverse* traverse)
3209 { return Expression::traverse(&this->expr_, traverse); }
3211 Expression*
3212 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3214 Type*
3215 do_type();
3217 void
3218 do_determine_type(const Type_context*)
3219 { this->expr_->determine_type_no_context(); }
3221 void
3222 do_check_types(Gogo*);
3224 Expression*
3225 do_copy()
3227 return Expression::make_field_reference(this->expr_->copy(),
3228 this->field_index_,
3229 this->location());
3232 bool
3233 do_is_addressable() const
3234 { return this->expr_->is_addressable(); }
3236 void
3237 do_address_taken(bool escapes)
3238 { this->expr_->address_taken(escapes); }
3240 void
3241 do_issue_nil_check()
3242 { this->expr_->issue_nil_check(); }
3244 Bexpression*
3245 do_get_backend(Translate_context*);
3247 void
3248 do_dump_expression(Ast_dump_context*) const;
3250 private:
3251 // The expression we are looking into. This should have a type of
3252 // struct.
3253 Expression* expr_;
3254 // The zero-based index of the field we are retrieving.
3255 unsigned int field_index_;
3256 // Whether this node was emitted implicitly for an embedded field,
3257 // that is, expr_ is not the expr_ of the original user node.
3258 bool implicit_;
3259 // Whether we have already emitted a fieldtrack call.
3260 bool called_fieldtrack_;
3263 // A reference to a field of an interface.
3265 class Interface_field_reference_expression : public Expression
3267 public:
3268 Interface_field_reference_expression(Expression* expr,
3269 const std::string& name,
3270 Location location)
3271 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
3272 expr_(expr), name_(name)
3275 // Return the expression for the interface object.
3276 Expression*
3277 expr()
3278 { return this->expr_; }
3280 // Return the name of the method to call.
3281 const std::string&
3282 name() const
3283 { return this->name_; }
3285 // Create a thunk to call the method NAME in TYPE when it is used as
3286 // part of a method value.
3287 static Named_object*
3288 create_thunk(Gogo*, Interface_type* type, const std::string& name);
3290 // Return an expression for the pointer to the function to call.
3291 Expression*
3292 get_function();
3294 // Return an expression for the first argument to pass to the interface
3295 // function. This is the real object associated with the interface object.
3296 Expression*
3297 get_underlying_object();
3299 protected:
3301 do_traverse(Traverse* traverse);
3303 Expression*
3304 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3306 Type*
3307 do_type();
3309 void
3310 do_determine_type(const Type_context*);
3312 void
3313 do_check_types(Gogo*);
3315 Expression*
3316 do_copy()
3318 return Expression::make_interface_field_reference(this->expr_->copy(),
3319 this->name_,
3320 this->location());
3323 Bexpression*
3324 do_get_backend(Translate_context*);
3326 void
3327 do_dump_expression(Ast_dump_context*) const;
3329 private:
3330 // A mapping from interface types to a list of thunks we have
3331 // created for methods.
3332 typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
3333 typedef Unordered_map(Interface_type*, Method_thunks*)
3334 Interface_method_thunks;
3335 static Interface_method_thunks interface_method_thunks;
3337 // The expression for the interface object. This should have a type
3338 // of interface or pointer to interface.
3339 Expression* expr_;
3340 // The field we are retrieving--the name of the method.
3341 std::string name_;
3344 // Implement the builtin function new.
3346 class Allocation_expression : public Expression
3348 public:
3349 Allocation_expression(Type* type, Location location)
3350 : Expression(EXPRESSION_ALLOCATION, location),
3351 type_(type), allocate_on_stack_(false)
3354 void
3355 set_allocate_on_stack()
3356 { this->allocate_on_stack_ = true; }
3358 protected:
3360 do_traverse(Traverse*);
3362 Type*
3363 do_type();
3365 void
3366 do_determine_type(const Type_context*)
3369 void
3370 do_check_types(Gogo*);
3372 Expression*
3373 do_copy();
3375 Bexpression*
3376 do_get_backend(Translate_context*);
3378 void
3379 do_dump_expression(Ast_dump_context*) const;
3381 private:
3382 // The type we are allocating.
3383 Type* type_;
3384 // Whether or not this is a stack allocation.
3385 bool allocate_on_stack_;
3388 // A general composite literal. This is lowered to a type specific
3389 // version.
3391 class Composite_literal_expression : public Parser_expression
3393 public:
3394 Composite_literal_expression(Type* type, int depth, bool has_keys,
3395 Expression_list* vals, bool all_are_names,
3396 Location location)
3397 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
3398 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys),
3399 all_are_names_(all_are_names), key_path_(std::vector<bool>(depth))
3403 // Mark the DEPTH entry of KEY_PATH as containing a key.
3404 void
3405 update_key_path(size_t depth)
3407 go_assert(depth < this->key_path_.size());
3408 this->key_path_[depth] = true;
3411 protected:
3413 do_traverse(Traverse* traverse);
3415 Expression*
3416 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3418 Expression*
3419 do_copy();
3421 void
3422 do_dump_expression(Ast_dump_context*) const;
3424 private:
3425 Expression*
3426 lower_struct(Gogo*, Type*);
3428 Expression*
3429 lower_array(Type*);
3431 Expression*
3432 make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
3434 Expression*
3435 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
3437 // The type of the composite literal.
3438 Type* type_;
3439 // The depth within a list of composite literals within a composite
3440 // literal, when the type is omitted.
3441 int depth_;
3442 // The values to put in the composite literal.
3443 Expression_list* vals_;
3444 // If this is true, then VALS_ is a list of pairs: a key and a
3445 // value. In an array initializer, a missing key will be NULL.
3446 bool has_keys_;
3447 // If this is true, then HAS_KEYS_ is true, and every key is a
3448 // simple identifier.
3449 bool all_are_names_;
3450 // A complement to DEPTH that indicates for each level starting from 0 to
3451 // DEPTH-1 whether or not this composite literal is nested inside of key or
3452 // a value. This is used to decide which type to use when given a map literal
3453 // with omitted key types.
3454 std::vector<bool> key_path_;
3457 // Helper/mixin class for struct and array construction expressions;
3458 // encapsulates a list of values plus an optional traversal order
3459 // recording the order in which the values should be visited.
3461 class Ordered_value_list
3463 public:
3464 Ordered_value_list(Expression_list* vals)
3465 : vals_(vals), traverse_order_(NULL)
3468 Expression_list*
3469 vals() const
3470 { return this->vals_; }
3473 traverse_vals(Traverse* traverse);
3475 // Get the traversal order (may be NULL)
3476 std::vector<unsigned long>*
3477 traverse_order()
3478 { return traverse_order_; }
3480 // Set the traversal order, used to ensure that we implement the
3481 // order of evaluation rules. Takes ownership of the argument.
3482 void
3483 set_traverse_order(std::vector<unsigned long>* traverse_order)
3484 { this->traverse_order_ = traverse_order; }
3486 private:
3487 // The list of values, in order of the fields in the struct or in
3488 // order of indices in an array. A NULL value of vals_ means that
3489 // all fields/slots should be zero-initialized; a single NULL entry
3490 // in the list means that the corresponding field or array slot
3491 // should be zero-initialized.
3492 Expression_list* vals_;
3493 // If not NULL, the order in which to traverse vals_. This is used
3494 // so that we implement the order of evaluation rules correctly.
3495 std::vector<unsigned long>* traverse_order_;
3498 // Construct a struct.
3500 class Struct_construction_expression : public Expression,
3501 public Ordered_value_list
3503 public:
3504 Struct_construction_expression(Type* type, Expression_list* vals,
3505 Location location)
3506 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
3507 Ordered_value_list(vals),
3508 type_(type)
3511 // Return whether this is a constant initializer.
3512 bool
3513 is_constant_struct() const;
3515 protected:
3517 do_traverse(Traverse* traverse);
3519 bool
3520 do_is_static_initializer() const;
3522 Type*
3523 do_type()
3524 { return this->type_; }
3526 void
3527 do_determine_type(const Type_context*);
3529 void
3530 do_check_types(Gogo*);
3532 Expression*
3533 do_copy();
3535 Expression*
3536 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3538 Bexpression*
3539 do_get_backend(Translate_context*);
3541 void
3542 do_export(Export*) const;
3544 void
3545 do_dump_expression(Ast_dump_context*) const;
3547 private:
3548 // The type of the struct to construct.
3549 Type* type_;
3552 // Construct an array. This class is not used directly; instead we
3553 // use the child classes, Fixed_array_construction_expression and
3554 // Slice_construction_expression.
3556 class Array_construction_expression : public Expression,
3557 public Ordered_value_list
3559 protected:
3560 Array_construction_expression(Expression_classification classification,
3561 Type* type,
3562 const std::vector<unsigned long>* indexes,
3563 Expression_list* vals, Location location)
3564 : Expression(classification, location),
3565 Ordered_value_list(vals),
3566 type_(type), indexes_(indexes)
3567 { go_assert(indexes == NULL || indexes->size() == vals->size()); }
3569 public:
3570 // Return whether this is a constant initializer.
3571 bool
3572 is_constant_array() const;
3574 // Return the number of elements.
3575 size_t
3576 element_count() const
3577 { return this->vals() == NULL ? 0 : this->vals()->size(); }
3579 protected:
3580 virtual int
3581 do_traverse(Traverse* traverse);
3583 bool
3584 do_is_static_initializer() const;
3586 Type*
3587 do_type()
3588 { return this->type_; }
3590 void
3591 do_determine_type(const Type_context*);
3593 void
3594 do_check_types(Gogo*);
3596 void
3597 do_export(Export*) const;
3599 // The indexes.
3600 const std::vector<unsigned long>*
3601 indexes()
3602 { return this->indexes_; }
3604 Expression*
3605 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3607 // Get the backend constructor for the array values.
3608 Bexpression*
3609 get_constructor(Translate_context* context, Btype* btype);
3611 void
3612 do_dump_expression(Ast_dump_context*) const;
3614 virtual void
3615 dump_slice_storage_expression(Ast_dump_context*) const { }
3617 private:
3618 // The type of the array to construct.
3619 Type* type_;
3620 // The list of indexes into the array, one for each value. This may
3621 // be NULL, in which case the indexes start at zero and increment.
3622 const std::vector<unsigned long>* indexes_;
3625 // Construct a fixed array.
3627 class Fixed_array_construction_expression :
3628 public Array_construction_expression
3630 public:
3631 Fixed_array_construction_expression(Type* type,
3632 const std::vector<unsigned long>* indexes,
3633 Expression_list* vals, Location location);
3635 protected:
3636 Expression*
3637 do_copy();
3639 Bexpression*
3640 do_get_backend(Translate_context*);
3643 // Construct a slice.
3645 class Slice_construction_expression : public Array_construction_expression
3647 public:
3648 Slice_construction_expression(Type* type,
3649 const std::vector<unsigned long>* indexes,
3650 Expression_list* vals, Location location);
3652 Expression*
3653 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3655 // Record that the storage for this slice (e.g. vals) cannot escape,
3656 // hence it can be stack-allocated.
3657 void
3658 set_storage_does_not_escape()
3660 this->storage_escapes_ = false;
3663 protected:
3664 // Note that taking the address of a slice literal is invalid.
3667 do_traverse(Traverse* traverse);
3669 Expression*
3670 do_copy();
3672 Bexpression*
3673 do_get_backend(Translate_context*);
3675 void
3676 dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const;
3678 // Create an array value for the constructed slice. Invoked during
3679 // flattening if slice storage does not escape, otherwise invoked
3680 // later on during do_get_backend().
3681 Expression*
3682 create_array_val();
3684 private:
3685 // The type of the values in this slice.
3686 Type* valtype_;
3687 // Array value expression, optionally filled in during flattening.
3688 Expression* array_val_;
3689 // Slice storage expression, optionally filled in during flattening.
3690 Expression* slice_storage_;
3691 // Normally true. Can be set to false if we know that the resulting
3692 // storage for the slice cannot escape.
3693 bool storage_escapes_;
3696 // Construct a map.
3698 class Map_construction_expression : public Expression
3700 public:
3701 Map_construction_expression(Type* type, Expression_list* vals,
3702 Location location)
3703 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
3704 type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL)
3705 { go_assert(vals == NULL || vals->size() % 2 == 0); }
3707 Expression_list*
3708 vals() const
3709 { return this->vals_; }
3711 protected:
3713 do_traverse(Traverse* traverse);
3715 Expression*
3716 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3718 Type*
3719 do_type()
3720 { return this->type_; }
3722 void
3723 do_determine_type(const Type_context*);
3725 void
3726 do_check_types(Gogo*);
3728 Expression*
3729 do_copy();
3731 Bexpression*
3732 do_get_backend(Translate_context*);
3734 void
3735 do_export(Export*) const;
3737 void
3738 do_dump_expression(Ast_dump_context*) const;
3740 private:
3741 // The type of the map to construct.
3742 Type* type_;
3743 // The list of values.
3744 Expression_list* vals_;
3745 // The type of the key-value pair struct for each map element.
3746 Struct_type* element_type_;
3747 // A temporary reference to the variable storing the constructor initializer.
3748 Temporary_statement* constructor_temp_;
3751 // A type guard expression.
3753 class Type_guard_expression : public Expression
3755 public:
3756 Type_guard_expression(Expression* expr, Type* type, Location location)
3757 : Expression(EXPRESSION_TYPE_GUARD, location),
3758 expr_(expr), type_(type)
3761 // Return the expression to convert.
3762 Expression*
3763 expr()
3764 { return this->expr_; }
3766 // Return the type to which to convert.
3767 Type*
3768 type()
3769 { return this->type_; }
3771 protected:
3773 do_traverse(Traverse* traverse);
3775 Expression*
3776 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3778 Type*
3779 do_type()
3780 { return this->type_; }
3782 void
3783 do_determine_type(const Type_context*)
3784 { this->expr_->determine_type_no_context(); }
3786 void
3787 do_check_types(Gogo*);
3789 Expression*
3790 do_copy();
3792 Bexpression*
3793 do_get_backend(Translate_context*);
3795 void
3796 do_dump_expression(Ast_dump_context*) const;
3798 private:
3799 // The expression to convert.
3800 Expression* expr_;
3801 // The type to which to convert.
3802 Type* type_;
3805 // Class Heap_expression.
3807 // When you take the address of an escaping expression, it is allocated
3808 // on the heap. This class implements that.
3810 class Heap_expression : public Expression
3812 public:
3813 Heap_expression(Expression* expr, Location location)
3814 : Expression(EXPRESSION_HEAP, location),
3815 expr_(expr), allocate_on_stack_(false)
3818 Expression*
3819 expr() const
3820 { return this->expr_; }
3822 void
3823 set_allocate_on_stack()
3824 { this->allocate_on_stack_ = true; }
3826 protected:
3828 do_traverse(Traverse* traverse)
3829 { return Expression::traverse(&this->expr_, traverse); }
3831 Type*
3832 do_type();
3833 void
3834 do_determine_type(const Type_context*)
3835 { this->expr_->determine_type_no_context(); }
3837 Expression*
3838 do_copy()
3840 return Expression::make_heap_expression(this->expr_->copy(),
3841 this->location());
3844 Bexpression*
3845 do_get_backend(Translate_context*);
3847 // We only export global objects, and the parser does not generate
3848 // this in global scope.
3849 void
3850 do_export(Export*) const
3851 { go_unreachable(); }
3853 void
3854 do_dump_expression(Ast_dump_context*) const;
3856 private:
3857 // The expression which is being put on the heap.
3858 Expression* expr_;
3859 // Whether or not this is a stack allocation.
3860 bool allocate_on_stack_;
3863 // A receive expression.
3865 class Receive_expression : public Expression
3867 public:
3868 Receive_expression(Expression* channel, Location location)
3869 : Expression(EXPRESSION_RECEIVE, location),
3870 channel_(channel), temp_receiver_(NULL)
3873 // Return the channel.
3874 Expression*
3875 channel()
3876 { return this->channel_; }
3878 protected:
3880 do_traverse(Traverse* traverse)
3881 { return Expression::traverse(&this->channel_, traverse); }
3883 bool
3884 do_discarding_value()
3885 { return true; }
3887 Type*
3888 do_type();
3890 Expression*
3891 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3893 void
3894 do_determine_type(const Type_context*)
3895 { this->channel_->determine_type_no_context(); }
3897 void
3898 do_check_types(Gogo*);
3900 Expression*
3901 do_copy()
3903 return Expression::make_receive(this->channel_->copy(), this->location());
3906 bool
3907 do_must_eval_in_order() const
3908 { return true; }
3910 Bexpression*
3911 do_get_backend(Translate_context*);
3913 void
3914 do_dump_expression(Ast_dump_context*) const;
3916 private:
3917 // The channel from which we are receiving.
3918 Expression* channel_;
3919 // A temporary reference to the variable storing the received data.
3920 Temporary_statement* temp_receiver_;
3923 // Conditional expressions.
3925 class Conditional_expression : public Expression
3927 public:
3928 Conditional_expression(Expression* cond, Expression* then_expr,
3929 Expression* else_expr, Location location)
3930 : Expression(EXPRESSION_CONDITIONAL, location),
3931 cond_(cond), then_(then_expr), else_(else_expr)
3934 Expression*
3935 condition() const
3936 { return this->cond_; }
3938 protected:
3940 do_traverse(Traverse*);
3942 Type*
3943 do_type();
3945 void
3946 do_determine_type(const Type_context*);
3948 Expression*
3949 do_copy()
3951 return new Conditional_expression(this->cond_->copy(), this->then_->copy(),
3952 this->else_->copy(), this->location());
3955 Bexpression*
3956 do_get_backend(Translate_context* context);
3958 void
3959 do_dump_expression(Ast_dump_context*) const;
3961 private:
3962 // The condition to be checked.
3963 Expression* cond_;
3964 // The expression to execute if the condition is true.
3965 Expression* then_;
3966 // The expression to execute if the condition is false.
3967 Expression* else_;
3970 // Compound expressions.
3972 class Compound_expression : public Expression
3974 public:
3975 Compound_expression(Expression* init, Expression* expr, Location location)
3976 : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr)
3979 Expression*
3980 init() const
3981 { return this->init_; }
3983 protected:
3985 do_traverse(Traverse*);
3987 Type*
3988 do_type();
3990 void
3991 do_determine_type(const Type_context*);
3993 Expression*
3994 do_copy()
3996 return new Compound_expression(this->init_->copy(), this->expr_->copy(),
3997 this->location());
4000 Bexpression*
4001 do_get_backend(Translate_context* context);
4003 void
4004 do_dump_expression(Ast_dump_context*) const;
4006 private:
4007 // The expression that is evaluated first and discarded.
4008 Expression* init_;
4009 // The expression that is evaluated and returned.
4010 Expression* expr_;
4013 // A backend expression. This is a backend expression wrapped in an
4014 // Expression, for convenience during backend generation.
4016 class Backend_expression : public Expression
4018 public:
4019 Backend_expression(Bexpression* bexpr, Type* type, Location location)
4020 : Expression(EXPRESSION_BACKEND, location), bexpr_(bexpr), type_(type)
4023 protected:
4025 do_traverse(Traverse*);
4027 // For now these are always valid static initializers. If that
4028 // changes we can change this.
4029 bool
4030 do_is_static_initializer() const
4031 { return true; }
4033 Type*
4034 do_type()
4035 { return this->type_; }
4037 void
4038 do_determine_type(const Type_context*)
4041 Expression*
4042 do_copy();
4044 Bexpression*
4045 do_get_backend(Translate_context*)
4046 { return this->bexpr_; }
4048 void
4049 do_dump_expression(Ast_dump_context*) const;
4051 private:
4052 // The backend expression we are wrapping.
4053 Bexpression* bexpr_;
4054 // The type of the expression;
4055 Type* type_;
4058 // A numeric constant. This is used both for untyped constants and
4059 // for constants that have a type.
4061 class Numeric_constant
4063 public:
4064 Numeric_constant()
4065 : classification_(NC_INVALID), type_(NULL)
4068 ~Numeric_constant();
4070 Numeric_constant(const Numeric_constant&);
4072 Numeric_constant& operator=(const Numeric_constant&);
4074 // Set to an unsigned long value.
4075 void
4076 set_unsigned_long(Type*, unsigned long);
4078 // Set to an integer value.
4079 void
4080 set_int(Type*, const mpz_t);
4082 // Set to a rune value.
4083 void
4084 set_rune(Type*, const mpz_t);
4086 // Set to a floating point value.
4087 void
4088 set_float(Type*, const mpfr_t);
4090 // Set to a complex value.
4091 void
4092 set_complex(Type*, const mpc_t);
4094 // Mark numeric constant as invalid.
4095 void
4096 set_invalid()
4097 { this->classification_ = NC_INVALID; }
4099 // Classifiers.
4100 bool
4101 is_int() const
4102 { return this->classification_ == Numeric_constant::NC_INT; }
4104 bool
4105 is_rune() const
4106 { return this->classification_ == Numeric_constant::NC_RUNE; }
4108 bool
4109 is_float() const
4110 { return this->classification_ == Numeric_constant::NC_FLOAT; }
4112 bool
4113 is_complex() const
4114 { return this->classification_ == Numeric_constant::NC_COMPLEX; }
4116 bool
4117 is_invalid() const
4118 { return this->classification_ == Numeric_constant::NC_INVALID; }
4120 // Value retrievers. These will initialize the values as well as
4121 // set them. GET_INT is only valid if IS_INT returns true, and
4122 // likewise respectively.
4123 void
4124 get_int(mpz_t*) const;
4126 void
4127 get_rune(mpz_t*) const;
4129 void
4130 get_float(mpfr_t*) const;
4132 void
4133 get_complex(mpc_t*) const;
4135 // Codes returned by to_unsigned_long.
4136 enum To_unsigned_long
4138 // Value is integer and fits in unsigned long.
4139 NC_UL_VALID,
4140 // Value is not integer.
4141 NC_UL_NOTINT,
4142 // Value is integer but is negative.
4143 NC_UL_NEGATIVE,
4144 // Value is non-negative integer but does not fit in unsigned
4145 // long.
4146 NC_UL_BIG
4149 // If the value can be expressed as an integer that fits in an
4150 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
4151 // one of the other To_unsigned_long codes.
4152 To_unsigned_long
4153 to_unsigned_long(unsigned long* val) const;
4155 // If the value can be expressed as an integer that describes the
4156 // size of an object in memory, set *VAL and return true.
4157 // Otherwise, return false. Currently we use int64_t to represent a
4158 // memory size, as in Type::backend_type_size.
4159 bool
4160 to_memory_size(int64_t* val) const;
4162 // If the value can be expressed as an int, return true and
4163 // initialize and set VAL. This will return false for a value with
4164 // an explicit float or complex type, even if the value is integral.
4165 bool
4166 to_int(mpz_t* val) const;
4168 // If the value can be expressed as a float, return true and
4169 // initialize and set VAL.
4170 bool
4171 to_float(mpfr_t* val) const;
4173 // If the value can be expressed as a complex, return true and
4174 // initialize and set VR and VI.
4175 bool
4176 to_complex(mpc_t* val) const;
4178 // Get the type.
4179 Type*
4180 type() const;
4182 // If the constant can be expressed in TYPE, then set the type of
4183 // the constant to TYPE and return true. Otherwise return false,
4184 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
4185 // the location to use for the error.
4186 bool
4187 set_type(Type* type, bool issue_error, Location location);
4189 // Return an Expression for this value.
4190 Expression*
4191 expression(Location) const;
4193 private:
4194 void
4195 clear();
4197 To_unsigned_long
4198 mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
4200 To_unsigned_long
4201 mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
4203 bool
4204 mpz_to_memory_size(const mpz_t ival, int64_t* val) const;
4206 bool
4207 mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const;
4209 bool
4210 check_int_type(Integer_type*, bool, Location);
4212 bool
4213 check_float_type(Float_type*, bool, Location);
4215 bool
4216 check_complex_type(Complex_type*, bool, Location);
4218 static bool
4219 is_float_neg_zero(const mpfr_t, int bits);
4221 // The kinds of constants.
4222 enum Classification
4224 NC_INVALID,
4225 NC_RUNE,
4226 NC_INT,
4227 NC_FLOAT,
4228 NC_COMPLEX
4231 // The kind of constant.
4232 Classification classification_;
4233 // The value.
4234 union
4236 // If NC_INT or NC_RUNE.
4237 mpz_t int_val;
4238 // If NC_FLOAT.
4239 mpfr_t float_val;
4240 // If NC_COMPLEX.
4241 mpc_t complex_val;
4242 } u_;
4243 // The type if there is one. This will be NULL for an untyped
4244 // constant.
4245 Type* type_;
4248 #endif // !defined(GO_EXPRESSIONS_H)