compiler: do not propagate address-taken of a slice element to the slice
[official-gcc.git] / gcc / go / gofrontend / expressions.h
blobcb6089005660b7a8ebedb34badd711d9b7c8a69b
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 // Make the builtin function descriptor type, so that it can be
873 // converted.
874 static void
875 make_func_descriptor_type();
877 // Traverse an expression.
878 static int
879 traverse(Expression**, Traverse*);
881 // Traverse subexpressions of this expression.
883 traverse_subexpressions(Traverse*);
885 // Lower an expression. This is called immediately after parsing.
886 // FUNCTION is the function we are in; it will be NULL for an
887 // expression initializing a global variable. INSERTER may be used
888 // to insert statements before the statement or initializer
889 // containing this expression; it is normally used to create
890 // temporary variables. IOTA_VALUE is the value that we should give
891 // to any iota expressions. This function must resolve expressions
892 // which could not be fully parsed into their final form. It
893 // returns the same Expression or a new one.
894 Expression*
895 lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
896 int iota_value)
897 { return this->do_lower(gogo, function, inserter, iota_value); }
899 // Flatten an expression. This is called after order_evaluation.
900 // FUNCTION is the function we are in; it will be NULL for an
901 // expression initializing a global variable. INSERTER may be used
902 // to insert statements before the statement or initializer
903 // containing this expression; it is normally used to create
904 // temporary variables. This function must resolve expressions
905 // which could not be fully parsed into their final form. It
906 // returns the same Expression or a new one.
907 Expression*
908 flatten(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
909 { return this->do_flatten(gogo, function, inserter); }
911 // Determine the real type of an expression with abstract integer,
912 // floating point, or complex type. TYPE_CONTEXT describes the
913 // expected type.
914 void
915 determine_type(const Type_context*);
917 // Check types in an expression.
918 void
919 check_types(Gogo* gogo)
920 { this->do_check_types(gogo); }
922 // Determine the type when there is no context.
923 void
924 determine_type_no_context();
926 // Return the current type of the expression. This may be changed
927 // by determine_type.
928 Type*
929 type()
930 { return this->do_type(); }
932 // Return a copy of an expression.
933 Expression*
934 copy()
935 { return this->do_copy(); }
937 // Return whether the expression is addressable--something which may
938 // be used as the operand of the unary & operator.
939 bool
940 is_addressable() const
941 { return this->do_is_addressable(); }
943 // Note that we are taking the address of this expression. ESCAPES
944 // is true if this address escapes the current function.
945 void
946 address_taken(bool escapes)
947 { this->do_address_taken(escapes); }
949 // Note that a nil check must be issued for this expression.
950 void
951 issue_nil_check()
952 { this->do_issue_nil_check(); }
954 // Return whether this expression must be evaluated in order
955 // according to the order of evaluation rules. This is basically
956 // true of all expressions with side-effects.
957 bool
958 must_eval_in_order() const
959 { return this->do_must_eval_in_order(); }
961 // Return whether subexpressions of this expression must be
962 // evaluated in order. This is true of index expressions and
963 // pointer indirections. This sets *SKIP to the number of
964 // subexpressions to skip during traversing, as index expressions
965 // only requiring moving the index, not the array.
966 bool
967 must_eval_subexpressions_in_order(int* skip) const
969 *skip = 0;
970 return this->do_must_eval_subexpressions_in_order(skip);
973 // Return the backend representation for this expression.
974 Bexpression*
975 get_backend(Translate_context*);
977 // Return an expression handling any conversions which must be done during
978 // assignment.
979 static Expression*
980 convert_for_assignment(Gogo*, Type* lhs_type, Expression* rhs,
981 Location location);
983 // Return an expression converting a value of one interface type to another
984 // interface type. If FOR_TYPE_GUARD is true this is for a type
985 // assertion.
986 static Expression*
987 convert_interface_to_interface(Type* lhs_type,
988 Expression* rhs, bool for_type_guard,
989 Location);
991 // Return a backend expression implementing the comparison LEFT OP RIGHT.
992 // TYPE is the type of both sides.
993 static Bexpression*
994 comparison(Translate_context*, Type* result_type, Operator op,
995 Expression* left, Expression* right, Location);
997 // Return the backend expression for the numeric constant VAL.
998 static Bexpression*
999 backend_numeric_constant_expression(Translate_context*,
1000 Numeric_constant* val);
1002 // Export the expression. This is only used for constants. It will
1003 // be used for things like values of named constants and sizes of
1004 // arrays.
1005 void
1006 export_expression(Export* exp) const
1007 { this->do_export(exp); }
1009 // Import an expression.
1010 static Expression*
1011 import_expression(Import*);
1013 // Return an expression which checks that VAL, of arbitrary integer type,
1014 // is non-negative and is not more than the maximum integer value.
1015 static Expression*
1016 check_bounds(Expression* val, Location);
1018 // Dump an expression to a dump constext.
1019 void
1020 dump_expression(Ast_dump_context*) const;
1022 protected:
1023 // May be implemented by child class: traverse the expressions.
1024 virtual int
1025 do_traverse(Traverse*);
1027 // Return a lowered expression.
1028 virtual Expression*
1029 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1030 { return this; }
1032 // Return a flattened expression.
1033 virtual Expression*
1034 do_flatten(Gogo*, Named_object*, Statement_inserter*)
1035 { return this; }
1038 // Return whether this is a constant expression.
1039 virtual bool
1040 do_is_constant() const
1041 { return false; }
1043 // Return whether this expression can be used as a constant
1044 // initializer.
1045 virtual bool
1046 do_is_static_initializer() const
1047 { return false; }
1049 // Return whether this is a constant expression of numeric type, and
1050 // set the Numeric_constant to the value.
1051 virtual bool
1052 do_numeric_constant_value(Numeric_constant*) const
1053 { return false; }
1055 // Return whether this is a constant expression of string type, and
1056 // set VAL to the value.
1057 virtual bool
1058 do_string_constant_value(std::string*) const
1059 { return false; }
1061 // Called by the parser if the value is being discarded.
1062 virtual bool
1063 do_discarding_value();
1065 // Child class holds type.
1066 virtual Type*
1067 do_type() = 0;
1069 // Child class implements determining type information.
1070 virtual void
1071 do_determine_type(const Type_context*) = 0;
1073 // Child class implements type checking if needed.
1074 virtual void
1075 do_check_types(Gogo*)
1078 // Child class implements copying.
1079 virtual Expression*
1080 do_copy() = 0;
1082 // Child class implements whether the expression is addressable.
1083 virtual bool
1084 do_is_addressable() const
1085 { return false; }
1087 // Child class implements taking the address of an expression.
1088 virtual void
1089 do_address_taken(bool)
1092 // Child class implements issuing a nil check if the address is taken.
1093 virtual void
1094 do_issue_nil_check()
1097 // Child class implements whether this expression must be evaluated
1098 // in order.
1099 virtual bool
1100 do_must_eval_in_order() const
1101 { return false; }
1103 // Child class implements whether this expressions requires that
1104 // subexpressions be evaluated in order. The child implementation
1105 // may set *SKIP if it should be non-zero.
1106 virtual bool
1107 do_must_eval_subexpressions_in_order(int* /* skip */) const
1108 { return false; }
1110 // Child class implements conversion to backend representation.
1111 virtual Bexpression*
1112 do_get_backend(Translate_context*) = 0;
1114 // Child class implements export.
1115 virtual void
1116 do_export(Export*) const;
1118 // For children to call to give an error for an unused value.
1119 void
1120 unused_value_error();
1122 // For children to call when they detect that they are in error.
1123 void
1124 set_is_error();
1126 // For children to call to report an error conveniently.
1127 void
1128 report_error(const char*);
1130 // Child class implements dumping to a dump context.
1131 virtual void
1132 do_dump_expression(Ast_dump_context*) const = 0;
1134 // Varargs lowering creates a slice object (unnamed compiler temp)
1135 // to contain the variable length collection of values. The enum
1136 // below tells the lowering routine whether it can mark that temp
1137 // as non-escaping or not. For general varargs calls it is not always
1138 // safe to stack-allocated the storage, but for specific cases (ex:
1139 // call to append()) it is legal.
1140 enum Slice_storage_escape_disp
1142 SLICE_STORAGE_MAY_ESCAPE,
1143 SLICE_STORAGE_DOES_NOT_ESCAPE
1146 private:
1147 // Convert to the desired statement classification, or return NULL.
1148 // This is a controlled dynamic cast.
1149 template<typename Expression_class,
1150 Expression_classification expr_classification>
1151 Expression_class*
1152 convert()
1154 return (this->classification_ == expr_classification
1155 ? static_cast<Expression_class*>(this)
1156 : NULL);
1159 template<typename Expression_class,
1160 Expression_classification expr_classification>
1161 const Expression_class*
1162 convert() const
1164 return (this->classification_ == expr_classification
1165 ? static_cast<const Expression_class*>(this)
1166 : NULL);
1169 static Expression*
1170 convert_type_to_interface(Type*, Expression*, Location);
1172 static Expression*
1173 get_interface_type_descriptor(Expression*);
1175 static Expression*
1176 convert_interface_to_type(Type*, Expression*, Location);
1178 // The expression classification.
1179 Expression_classification classification_;
1180 // The location in the input file.
1181 Location location_;
1184 // A list of Expressions.
1186 class Expression_list
1188 public:
1189 Expression_list()
1190 : entries_()
1193 // Return whether the list is empty.
1194 bool
1195 empty() const
1196 { return this->entries_.empty(); }
1198 // Return the number of entries in the list.
1199 size_t
1200 size() const
1201 { return this->entries_.size(); }
1203 // Add an entry to the end of the list.
1204 void
1205 push_back(Expression* expr)
1206 { this->entries_.push_back(expr); }
1208 void
1209 append(Expression_list* add)
1210 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
1212 // Reserve space in the list.
1213 void
1214 reserve(size_t size)
1215 { this->entries_.reserve(size); }
1217 // Traverse the expressions in the list.
1219 traverse(Traverse*);
1221 // Copy the list.
1222 Expression_list*
1223 copy();
1225 // Return true if the list contains an error expression.
1226 bool
1227 contains_error() const;
1229 // Retrieve an element by index.
1230 Expression*&
1231 at(size_t i)
1232 { return this->entries_.at(i); }
1234 // Return the first and last elements.
1235 Expression*&
1236 front()
1237 { return this->entries_.front(); }
1239 Expression*
1240 front() const
1241 { return this->entries_.front(); }
1243 Expression*&
1244 back()
1245 { return this->entries_.back(); }
1247 Expression*
1248 back() const
1249 { return this->entries_.back(); }
1251 // Iterators.
1253 typedef std::vector<Expression*>::iterator iterator;
1254 typedef std::vector<Expression*>::const_iterator const_iterator;
1256 iterator
1257 begin()
1258 { return this->entries_.begin(); }
1260 const_iterator
1261 begin() const
1262 { return this->entries_.begin(); }
1264 iterator
1265 end()
1266 { return this->entries_.end(); }
1268 const_iterator
1269 end() const
1270 { return this->entries_.end(); }
1272 // Erase an entry.
1273 void
1274 erase(iterator p)
1275 { this->entries_.erase(p); }
1277 private:
1278 std::vector<Expression*> entries_;
1281 // An abstract base class for an expression which is only used by the
1282 // parser, and is lowered in the lowering pass.
1284 class Parser_expression : public Expression
1286 public:
1287 Parser_expression(Expression_classification classification,
1288 Location location)
1289 : Expression(classification, location)
1292 protected:
1293 virtual Expression*
1294 do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
1296 Type*
1297 do_type();
1299 void
1300 do_determine_type(const Type_context*)
1301 { go_unreachable(); }
1303 void
1304 do_check_types(Gogo*)
1305 { go_unreachable(); }
1307 Bexpression*
1308 do_get_backend(Translate_context*)
1309 { go_unreachable(); }
1312 // An expression which is simply a variable.
1314 class Var_expression : public Expression
1316 public:
1317 Var_expression(Named_object* variable, Location location)
1318 : Expression(EXPRESSION_VAR_REFERENCE, location),
1319 variable_(variable)
1322 // Return the variable.
1323 Named_object*
1324 named_object() const
1325 { return this->variable_; }
1327 protected:
1328 Expression*
1329 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1331 Type*
1332 do_type();
1334 void
1335 do_determine_type(const Type_context*);
1337 Expression*
1338 do_copy()
1339 { return this; }
1341 bool
1342 do_is_addressable() const
1343 { return true; }
1345 void
1346 do_address_taken(bool);
1348 Bexpression*
1349 do_get_backend(Translate_context*);
1351 void
1352 do_dump_expression(Ast_dump_context*) const;
1354 private:
1355 // The variable we are referencing.
1356 Named_object* variable_;
1359 // A reference to a variable within an enclosing function.
1361 class Enclosed_var_expression : public Expression
1363 public:
1364 Enclosed_var_expression(Expression* reference, Named_object* variable,
1365 Location location)
1366 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE, location),
1367 reference_(reference), variable_(variable)
1370 // The reference to the enclosed variable. This will be an indirection of the
1371 // the field stored within closure variable.
1372 Expression*
1373 reference() const
1374 { return this->reference_; }
1376 // The variable being enclosed and referenced.
1377 Named_object*
1378 variable() const
1379 { return this->variable_; }
1381 protected:
1383 do_traverse(Traverse*);
1385 Expression*
1386 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1388 Expression*
1389 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1391 Type*
1392 do_type()
1393 { return this->reference_->type(); }
1395 void
1396 do_determine_type(const Type_context* context)
1397 { return this->reference_->determine_type(context); }
1399 Expression*
1400 do_copy()
1401 { return this; }
1403 bool
1404 do_is_addressable() const
1405 { return this->reference_->is_addressable(); }
1407 void
1408 do_address_taken(bool escapes);
1410 Bexpression*
1411 do_get_backend(Translate_context* context)
1412 { return this->reference_->get_backend(context); }
1414 void
1415 do_dump_expression(Ast_dump_context*) const;
1417 private:
1418 // The reference to the enclosed variable.
1419 Expression* reference_;
1420 // The variable being enclosed.
1421 Named_object* variable_;
1424 // A reference to a temporary variable.
1426 class Temporary_reference_expression : public Expression
1428 public:
1429 Temporary_reference_expression(Temporary_statement* statement,
1430 Location location)
1431 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1432 statement_(statement), is_lvalue_(false)
1435 // The temporary that this expression refers to.
1436 Temporary_statement*
1437 statement() const
1438 { return this->statement_; }
1440 // Indicate that this reference appears on the left hand side of an
1441 // assignment statement.
1442 void
1443 set_is_lvalue()
1444 { this->is_lvalue_ = true; }
1446 protected:
1447 Type*
1448 do_type();
1450 void
1451 do_determine_type(const Type_context*)
1454 Expression*
1455 do_copy()
1456 { return make_temporary_reference(this->statement_, this->location()); }
1458 bool
1459 do_is_addressable() const
1460 { return true; }
1462 void
1463 do_address_taken(bool);
1465 Bexpression*
1466 do_get_backend(Translate_context*);
1468 void
1469 do_dump_expression(Ast_dump_context*) const;
1471 private:
1472 // The statement where the temporary variable is defined.
1473 Temporary_statement* statement_;
1474 // Whether this reference appears on the left hand side of an
1475 // assignment statement.
1476 bool is_lvalue_;
1479 // Set and use a temporary variable.
1481 class Set_and_use_temporary_expression : public Expression
1483 public:
1484 Set_and_use_temporary_expression(Temporary_statement* statement,
1485 Expression* expr, Location location)
1486 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1487 statement_(statement), expr_(expr)
1490 // Return the temporary.
1491 Temporary_statement*
1492 temporary() const
1493 { return this->statement_; }
1495 // Return the expression.
1496 Expression*
1497 expression() const
1498 { return this->expr_; }
1500 protected:
1502 do_traverse(Traverse* traverse)
1503 { return Expression::traverse(&this->expr_, traverse); }
1505 Type*
1506 do_type();
1508 void
1509 do_determine_type(const Type_context*);
1511 Expression*
1512 do_copy()
1514 return make_set_and_use_temporary(this->statement_, this->expr_,
1515 this->location());
1518 bool
1519 do_is_addressable() const
1520 { return true; }
1522 void
1523 do_address_taken(bool);
1525 Bexpression*
1526 do_get_backend(Translate_context*);
1528 void
1529 do_dump_expression(Ast_dump_context*) const;
1531 private:
1532 // The statement where the temporary variable is defined.
1533 Temporary_statement* statement_;
1534 // The expression to assign to the temporary.
1535 Expression* expr_;
1538 // A string expression.
1540 class String_expression : public Expression
1542 public:
1543 String_expression(const std::string& val, Location location)
1544 : Expression(EXPRESSION_STRING, location),
1545 val_(val), type_(NULL)
1548 const std::string&
1549 val() const
1550 { return this->val_; }
1552 static Expression*
1553 do_import(Import*);
1555 protected:
1556 bool
1557 do_is_constant() const
1558 { return true; }
1560 bool
1561 do_is_static_initializer() const
1562 { return true; }
1564 bool
1565 do_string_constant_value(std::string* val) const
1567 *val = this->val_;
1568 return true;
1571 Type*
1572 do_type();
1574 void
1575 do_determine_type(const Type_context*);
1577 Expression*
1578 do_copy()
1579 { return this; }
1581 Bexpression*
1582 do_get_backend(Translate_context*);
1584 // Write string literal to a string dump.
1585 static void
1586 export_string(String_dump* exp, const String_expression* str);
1588 void
1589 do_export(Export*) const;
1591 void
1592 do_dump_expression(Ast_dump_context*) const;
1594 private:
1595 // The string value. This is immutable.
1596 const std::string val_;
1597 // The type as determined by context.
1598 Type* type_;
1601 // A type conversion expression.
1603 class Type_conversion_expression : public Expression
1605 public:
1606 Type_conversion_expression(Type* type, Expression* expr,
1607 Location location)
1608 : Expression(EXPRESSION_CONVERSION, location),
1609 type_(type), expr_(expr), may_convert_function_types_(false)
1612 // Return the type to which we are converting.
1613 Type*
1614 type() const
1615 { return this->type_; }
1617 // Return the expression which we are converting.
1618 Expression*
1619 expr() const
1620 { return this->expr_; }
1622 // Permit converting from one function type to another. This is
1623 // used internally for method expressions.
1624 void
1625 set_may_convert_function_types()
1627 this->may_convert_function_types_ = true;
1630 // Import a type conversion expression.
1631 static Expression*
1632 do_import(Import*);
1634 protected:
1636 do_traverse(Traverse* traverse);
1638 Expression*
1639 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1641 Expression*
1642 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1644 bool
1645 do_is_constant() const;
1647 bool
1648 do_is_static_initializer() const;
1650 bool
1651 do_numeric_constant_value(Numeric_constant*) const;
1653 bool
1654 do_string_constant_value(std::string*) const;
1656 Type*
1657 do_type()
1658 { return this->type_; }
1660 void
1661 do_determine_type(const Type_context*);
1663 void
1664 do_check_types(Gogo*);
1666 Expression*
1667 do_copy()
1669 return new Type_conversion_expression(this->type_, this->expr_->copy(),
1670 this->location());
1673 Bexpression*
1674 do_get_backend(Translate_context* context);
1676 void
1677 do_export(Export*) const;
1679 void
1680 do_dump_expression(Ast_dump_context*) const;
1682 private:
1683 // The type to convert to.
1684 Type* type_;
1685 // The expression to convert.
1686 Expression* expr_;
1687 // True if this is permitted to convert function types. This is
1688 // used internally for method expressions.
1689 bool may_convert_function_types_;
1692 // An unsafe type conversion, used to pass values to builtin functions.
1694 class Unsafe_type_conversion_expression : public Expression
1696 public:
1697 Unsafe_type_conversion_expression(Type* type, Expression* expr,
1698 Location location)
1699 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
1700 type_(type), expr_(expr)
1703 Expression*
1704 expr() const
1705 { return this->expr_; }
1707 protected:
1709 do_traverse(Traverse* traverse);
1711 bool
1712 do_is_static_initializer() const;
1714 Type*
1715 do_type()
1716 { return this->type_; }
1718 void
1719 do_determine_type(const Type_context*)
1720 { this->expr_->determine_type_no_context(); }
1722 Expression*
1723 do_copy()
1725 return new Unsafe_type_conversion_expression(this->type_,
1726 this->expr_->copy(),
1727 this->location());
1730 Bexpression*
1731 do_get_backend(Translate_context*);
1733 void
1734 do_dump_expression(Ast_dump_context*) const;
1736 private:
1737 // The type to convert to.
1738 Type* type_;
1739 // The expression to convert.
1740 Expression* expr_;
1743 // A Unary expression.
1745 class Unary_expression : public Expression
1747 public:
1748 Unary_expression(Operator op, Expression* expr, Location location)
1749 : Expression(EXPRESSION_UNARY, location),
1750 op_(op), escapes_(true), create_temp_(false), is_gc_root_(false),
1751 is_slice_init_(false), expr_(expr),
1752 issue_nil_check_(NIL_CHECK_DEFAULT)
1755 // Return the operator.
1756 Operator
1757 op() const
1758 { return this->op_; }
1760 // Return the operand.
1761 Expression*
1762 operand() const
1763 { return this->expr_; }
1765 // Record that an address expression does not escape.
1766 void
1767 set_does_not_escape()
1769 go_assert(this->op_ == OPERATOR_AND);
1770 this->escapes_ = false;
1773 // Record that this is an address expression which should create a
1774 // temporary variable if necessary. This is used for method calls.
1775 void
1776 set_create_temp()
1778 go_assert(this->op_ == OPERATOR_AND);
1779 this->create_temp_ = true;
1782 // Record that this is an address expression of a GC root, which is a
1783 // mutable composite literal. This used for registering GC variables.
1784 void
1785 set_is_gc_root()
1787 go_assert(this->op_ == OPERATOR_AND);
1788 this->is_gc_root_ = true;
1791 // Record that this is an address expression of a slice value initializer,
1792 // which is mutable if the values are not copied to the heap.
1793 void
1794 set_is_slice_init()
1796 go_assert(this->op_ == OPERATOR_AND);
1797 this->is_slice_init_ = true;
1800 // Call the address_taken method on the operand if necessary.
1801 void
1802 check_operand_address_taken(Gogo*);
1804 // Apply unary opcode OP to UNC, setting NC. Return true if this
1805 // could be done, false if not. On overflow, issues an error and
1806 // sets *ISSUED_ERROR.
1807 static bool
1808 eval_constant(Operator op, const Numeric_constant* unc,
1809 Location, Numeric_constant* nc, bool *issued_error);
1811 static Expression*
1812 do_import(Import*);
1814 // Declare that this deref does or does not require an explicit nil check.
1815 void
1816 set_requires_nil_check(bool needed)
1818 go_assert(this->op_ == OPERATOR_MULT);
1819 if (needed)
1820 this->issue_nil_check_ = NIL_CHECK_NEEDED;
1821 else
1822 this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED;
1825 protected:
1827 do_traverse(Traverse* traverse)
1828 { return Expression::traverse(&this->expr_, traverse); }
1830 Expression*
1831 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1833 Expression*
1834 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1836 bool
1837 do_is_constant() const;
1839 bool
1840 do_is_static_initializer() const;
1842 bool
1843 do_numeric_constant_value(Numeric_constant*) const;
1845 Type*
1846 do_type();
1848 void
1849 do_determine_type(const Type_context*);
1851 void
1852 do_check_types(Gogo*);
1854 Expression*
1855 do_copy()
1857 return Expression::make_unary(this->op_, this->expr_->copy(),
1858 this->location());
1861 bool
1862 do_must_eval_subexpressions_in_order(int*) const
1863 { return this->op_ == OPERATOR_MULT; }
1865 bool
1866 do_is_addressable() const
1867 { return this->op_ == OPERATOR_MULT; }
1869 Bexpression*
1870 do_get_backend(Translate_context*);
1872 void
1873 do_export(Export*) const;
1875 void
1876 do_dump_expression(Ast_dump_context*) const;
1878 void
1879 do_issue_nil_check()
1881 if (this->op_ == OPERATOR_MULT)
1882 this->set_requires_nil_check(true);
1885 private:
1886 static bool
1887 base_is_static_initializer(Expression*);
1889 // Return a determination as to whether this dereference expression
1890 // requires a nil check.
1891 Nil_check_classification
1892 requires_nil_check(Gogo*);
1894 // The unary operator to apply.
1895 Operator op_;
1896 // Normally true. False if this is an address expression which does
1897 // not escape the current function.
1898 bool escapes_;
1899 // True if this is an address expression which should create a
1900 // temporary variable if necessary.
1901 bool create_temp_;
1902 // True if this is an address expression for a GC root. A GC root is a
1903 // special struct composite literal that is mutable when addressed, meaning
1904 // it cannot be represented as an immutable_struct in the backend.
1905 bool is_gc_root_;
1906 // True if this is an address expression for a slice value with an immutable
1907 // initializer. The initializer for a slice's value pointer has an array
1908 // type, meaning it cannot be represented as an immutable_struct in the
1909 // backend.
1910 bool is_slice_init_;
1911 // The operand.
1912 Expression* expr_;
1913 // Whether or not to issue a nil check for this expression if its address
1914 // is being taken.
1915 Nil_check_classification issue_nil_check_;
1918 // A binary expression.
1920 class Binary_expression : public Expression
1922 public:
1923 Binary_expression(Operator op, Expression* left, Expression* right,
1924 Location location)
1925 : Expression(EXPRESSION_BINARY, location),
1926 op_(op), left_(left), right_(right), type_(NULL)
1929 // Return the operator.
1930 Operator
1931 op()
1932 { return this->op_; }
1934 // Return the left hand expression.
1935 Expression*
1936 left()
1937 { return this->left_; }
1939 // Return the right hand expression.
1940 Expression*
1941 right()
1942 { return this->right_; }
1944 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1945 // Return true if this could be done, false if not. Issue errors at
1946 // LOCATION as appropriate, and sets *ISSUED_ERROR if it did.
1947 static bool
1948 eval_constant(Operator op, Numeric_constant* left_nc,
1949 Numeric_constant* right_nc, Location location,
1950 Numeric_constant* nc, bool* issued_error);
1952 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1953 // *RESULT. Return true if this could be done, false if not. Issue
1954 // errors at LOCATION as appropriate.
1955 static bool
1956 compare_constant(Operator op, Numeric_constant* left_nc,
1957 Numeric_constant* right_nc, Location location,
1958 bool* result);
1960 static Expression*
1961 do_import(Import*);
1963 // Report an error if OP can not be applied to TYPE. Return whether
1964 // it can. OTYPE is the type of the other operand.
1965 static bool
1966 check_operator_type(Operator op, Type* type, Type* otype, Location);
1968 // Set *RESULT_TYPE to the resulting type when OP is applied to
1969 // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
1970 // success, false on failure.
1971 static bool
1972 operation_type(Operator op, Type* left_type, Type* right_type,
1973 Type** result_type);
1975 protected:
1977 do_traverse(Traverse* traverse);
1979 Expression*
1980 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1982 Expression*
1983 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1985 bool
1986 do_is_constant() const
1987 { return this->left_->is_constant() && this->right_->is_constant(); }
1989 bool
1990 do_is_static_initializer() const;
1992 bool
1993 do_numeric_constant_value(Numeric_constant*) const;
1995 bool
1996 do_discarding_value();
1998 Type*
1999 do_type();
2001 void
2002 do_determine_type(const Type_context*);
2004 void
2005 do_check_types(Gogo*);
2007 Expression*
2008 do_copy()
2010 return Expression::make_binary(this->op_, this->left_->copy(),
2011 this->right_->copy(), this->location());
2014 Bexpression*
2015 do_get_backend(Translate_context*);
2017 void
2018 do_export(Export*) const;
2020 void
2021 do_dump_expression(Ast_dump_context*) const;
2023 private:
2024 static bool
2025 cmp_to_bool(Operator op, int cmp);
2027 static bool
2028 eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
2029 Location, Numeric_constant*);
2031 static bool
2032 eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
2033 Location, Numeric_constant*);
2035 static bool
2036 eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
2037 Location, Numeric_constant*);
2039 static bool
2040 compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
2042 static bool
2043 compare_float(const Numeric_constant*, const Numeric_constant *, int*);
2045 static bool
2046 compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
2048 Expression*
2049 lower_struct_comparison(Gogo*, Statement_inserter*);
2051 Expression*
2052 lower_array_comparison(Gogo*, Statement_inserter*);
2054 Expression*
2055 lower_interface_value_comparison(Gogo*, Statement_inserter*);
2057 Expression*
2058 lower_compare_to_memcmp(Gogo*, Statement_inserter*);
2060 Expression*
2061 operand_address(Statement_inserter*, Expression*);
2063 // The binary operator to apply.
2064 Operator op_;
2065 // The left hand side operand.
2066 Expression* left_;
2067 // The right hand side operand.
2068 Expression* right_;
2069 // The type of a comparison operation.
2070 Type* type_;
2073 // A string concatenation expression. This is a sequence of strings
2074 // added together. It is created when lowering Binary_expression.
2076 class String_concat_expression : public Expression
2078 public:
2079 String_concat_expression(Expression_list* exprs)
2080 : Expression(EXPRESSION_STRING_CONCAT, exprs->front()->location()),
2081 exprs_(exprs)
2084 // Return the list of string expressions to be concatenated.
2085 Expression_list*
2086 exprs()
2087 { return this->exprs_; }
2089 protected:
2091 do_traverse(Traverse* traverse)
2092 { return this->exprs_->traverse(traverse); }
2094 Expression*
2095 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2096 { return this; }
2098 Expression*
2099 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2101 bool
2102 do_is_constant() const;
2104 bool
2105 do_is_static_initializer() const;
2107 Type*
2108 do_type();
2110 void
2111 do_determine_type(const Type_context*);
2113 void
2114 do_check_types(Gogo*);
2116 Expression*
2117 do_copy()
2118 { return Expression::make_string_concat(this->exprs_->copy()); }
2120 Bexpression*
2121 do_get_backend(Translate_context*)
2122 { go_unreachable(); }
2124 void
2125 do_export(Export*) const
2126 { go_unreachable(); }
2128 void
2129 do_dump_expression(Ast_dump_context*) const;
2131 private:
2132 // The string expressions to concatenate.
2133 Expression_list* exprs_;
2136 // A call expression. The go statement needs to dig inside this.
2138 class Call_expression : public Expression
2140 public:
2141 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
2142 Location location)
2143 : Expression(EXPRESSION_CALL, location),
2144 fn_(fn), args_(args), type_(NULL), call_(NULL), call_temp_(NULL)
2145 , expected_result_count_(0), is_varargs_(is_varargs),
2146 varargs_are_lowered_(false), types_are_determined_(false),
2147 is_deferred_(false), is_concurrent_(false), issued_error_(false),
2148 is_multi_value_arg_(false), is_flattened_(false)
2151 // The function to call.
2152 Expression*
2153 fn() const
2154 { return this->fn_; }
2156 // The arguments.
2157 Expression_list*
2158 args()
2159 { return this->args_; }
2161 const Expression_list*
2162 args() const
2163 { return this->args_; }
2165 // Get the function type.
2166 Function_type*
2167 get_function_type() const;
2169 // Return the number of values this call will return.
2170 size_t
2171 result_count() const;
2173 // Return the temporary variable that holds the results. This is
2174 // only valid after the expression has been lowered, and is only
2175 // valid for calls which return multiple results.
2176 Temporary_statement*
2177 results() const;
2179 // Set the number of results expected from this call. This is used
2180 // when the call appears in a context that expects multiple results,
2181 // such as a, b = f().
2182 void
2183 set_expected_result_count(size_t);
2185 // Return whether this is a call to the predeclared function
2186 // recover.
2187 bool
2188 is_recover_call() const;
2190 // Set the argument for a call to recover.
2191 void
2192 set_recover_arg(Expression*);
2194 // Whether the last argument is a varargs argument (f(a...)).
2195 bool
2196 is_varargs() const
2197 { return this->is_varargs_; }
2199 // Return whether varargs have already been lowered.
2200 bool
2201 varargs_are_lowered() const
2202 { return this->varargs_are_lowered_; }
2204 // Note that varargs have already been lowered.
2205 void
2206 set_varargs_are_lowered()
2207 { this->varargs_are_lowered_ = true; }
2209 // Whether this call is being deferred.
2210 bool
2211 is_deferred() const
2212 { return this->is_deferred_; }
2214 // Note that the call is being deferred.
2215 void
2216 set_is_deferred()
2217 { this->is_deferred_ = true; }
2219 // Whether this call is concurrently executed.
2220 bool
2221 is_concurrent() const
2222 { return this->is_concurrent_; }
2224 // Note that the call is concurrently executed.
2225 void
2226 set_is_concurrent()
2227 { this->is_concurrent_ = true; }
2229 // We have found an error with this call expression; return true if
2230 // we should report it.
2231 bool
2232 issue_error();
2234 // Whether or not this call contains errors, either in the call or the
2235 // arguments to the call.
2236 bool
2237 is_erroneous_call();
2239 // Whether this call returns multiple results that are used as an
2240 // multi-valued argument.
2241 bool
2242 is_multi_value_arg() const
2243 { return this->is_multi_value_arg_; }
2245 // Note this call is used as a multi-valued argument.
2246 void
2247 set_is_multi_value_arg()
2248 { this->is_multi_value_arg_ = true; }
2250 // Whether this is a call to builtin function.
2251 virtual bool
2252 is_builtin()
2253 { return false; }
2255 // Convert to a Builtin_call_expression, or return NULL.
2256 inline Builtin_call_expression*
2257 builtin_call_expression();
2259 protected:
2261 do_traverse(Traverse*);
2263 virtual Expression*
2264 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2266 virtual Expression*
2267 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2269 bool
2270 do_discarding_value()
2271 { return true; }
2273 virtual Type*
2274 do_type();
2276 virtual void
2277 do_determine_type(const Type_context*);
2279 virtual void
2280 do_check_types(Gogo*);
2282 Expression*
2283 do_copy();
2285 bool
2286 do_must_eval_in_order() const;
2288 virtual Bexpression*
2289 do_get_backend(Translate_context*);
2291 virtual bool
2292 do_is_recover_call() const;
2294 virtual void
2295 do_set_recover_arg(Expression*);
2297 // Let a builtin expression change the argument list.
2298 void
2299 set_args(Expression_list* args)
2300 { this->args_ = args; }
2302 // Let a builtin expression lower varargs.
2303 void
2304 lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
2305 Type* varargs_type, size_t param_count,
2306 Slice_storage_escape_disp escape_disp);
2308 // Let a builtin expression check whether types have been
2309 // determined.
2310 bool
2311 determining_types();
2313 void
2314 do_dump_expression(Ast_dump_context*) const;
2316 private:
2317 bool
2318 check_argument_type(int, const Type*, const Type*, Location, bool);
2320 Expression*
2321 lower_to_builtin(Named_object**, const char*, int);
2323 Expression*
2324 interface_method_function(Interface_field_reference_expression*,
2325 Expression**, Location);
2327 Bexpression*
2328 set_results(Translate_context*);
2330 // The function to call.
2331 Expression* fn_;
2332 // The arguments to pass. This may be NULL if there are no
2333 // arguments.
2334 Expression_list* args_;
2335 // The type of the expression, to avoid recomputing it.
2336 Type* type_;
2337 // The backend expression for the call, used for a call which returns a tuple.
2338 Bexpression* call_;
2339 // A temporary variable to store this call if the function returns a tuple.
2340 Temporary_statement* call_temp_;
2341 // If not 0, the number of results expected from this call, when
2342 // used in a context that expects multiple values.
2343 size_t expected_result_count_;
2344 // True if the last argument is a varargs argument (f(a...)).
2345 bool is_varargs_;
2346 // True if varargs have already been lowered.
2347 bool varargs_are_lowered_;
2348 // True if types have been determined.
2349 bool types_are_determined_;
2350 // True if the call is an argument to a defer statement.
2351 bool is_deferred_;
2352 // True if the call is an argument to a go statement.
2353 bool is_concurrent_;
2354 // True if we reported an error about a mismatch between call
2355 // results and uses. This is to avoid producing multiple errors
2356 // when there are multiple Call_result_expressions.
2357 bool issued_error_;
2358 // True if this call is used as an argument that returns multiple results.
2359 bool is_multi_value_arg_;
2360 // True if this expression has already been flattened.
2361 bool is_flattened_;
2364 // A call expression to a builtin function.
2366 class Builtin_call_expression : public Call_expression
2368 public:
2369 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
2370 bool is_varargs, Location location);
2372 // The builtin functions.
2373 enum Builtin_function_code
2375 BUILTIN_INVALID,
2377 // Predeclared builtin functions.
2378 BUILTIN_APPEND,
2379 BUILTIN_CAP,
2380 BUILTIN_CLOSE,
2381 BUILTIN_COMPLEX,
2382 BUILTIN_COPY,
2383 BUILTIN_DELETE,
2384 BUILTIN_IMAG,
2385 BUILTIN_LEN,
2386 BUILTIN_MAKE,
2387 BUILTIN_NEW,
2388 BUILTIN_PANIC,
2389 BUILTIN_PRINT,
2390 BUILTIN_PRINTLN,
2391 BUILTIN_REAL,
2392 BUILTIN_RECOVER,
2394 // Builtin functions from the unsafe package.
2395 BUILTIN_ALIGNOF,
2396 BUILTIN_OFFSETOF,
2397 BUILTIN_SIZEOF
2400 Builtin_function_code
2401 code()
2402 { return this->code_; }
2404 // This overrides Call_expression::is_builtin.
2405 bool
2406 is_builtin()
2407 { return true; }
2409 protected:
2410 // This overrides Call_expression::do_lower.
2411 Expression*
2412 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2414 Expression*
2415 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2417 bool
2418 do_is_constant() const;
2420 bool
2421 do_numeric_constant_value(Numeric_constant*) const;
2423 bool
2424 do_discarding_value();
2426 Type*
2427 do_type();
2429 void
2430 do_determine_type(const Type_context*);
2432 void
2433 do_check_types(Gogo*);
2435 Expression*
2436 do_copy();
2438 Bexpression*
2439 do_get_backend(Translate_context*);
2441 void
2442 do_export(Export*) const;
2444 virtual bool
2445 do_is_recover_call() const;
2447 virtual void
2448 do_set_recover_arg(Expression*);
2450 private:
2451 Expression*
2452 one_arg() const;
2454 bool
2455 check_one_arg();
2457 static Type*
2458 real_imag_type(Type*);
2460 static Type*
2461 complex_type(Type*);
2463 Expression*
2464 lower_make(Statement_inserter*);
2466 Expression* flatten_append(Gogo*, Named_object*, Statement_inserter*);
2468 bool
2469 check_int_value(Expression*, bool is_length, bool* small);
2471 // A pointer back to the general IR structure. This avoids a global
2472 // variable, or passing it around everywhere.
2473 Gogo* gogo_;
2474 // The builtin function being called.
2475 Builtin_function_code code_;
2476 // Used to stop endless loops when the length of an array uses len
2477 // or cap of the array itself.
2478 mutable bool seen_;
2479 // Whether the argument is set for calls to BUILTIN_RECOVER.
2480 bool recover_arg_is_set_;
2483 inline Builtin_call_expression*
2484 Call_expression::builtin_call_expression()
2486 return (this->is_builtin()
2487 ? static_cast<Builtin_call_expression*>(this)
2488 : NULL);
2491 // A single result from a call which returns multiple results.
2493 class Call_result_expression : public Expression
2495 public:
2496 Call_result_expression(Call_expression* call, unsigned int index)
2497 : Expression(EXPRESSION_CALL_RESULT, call->location()),
2498 call_(call), index_(index)
2501 Expression*
2502 call() const
2503 { return this->call_; }
2505 unsigned int
2506 index() const
2507 { return this->index_; }
2509 protected:
2511 do_traverse(Traverse*);
2513 Type*
2514 do_type();
2516 void
2517 do_determine_type(const Type_context*);
2519 void
2520 do_check_types(Gogo*);
2522 Expression*
2523 do_copy()
2525 return new Call_result_expression(this->call_->call_expression(),
2526 this->index_);
2529 bool
2530 do_must_eval_in_order() const
2531 { return true; }
2533 Bexpression*
2534 do_get_backend(Translate_context*);
2536 void
2537 do_dump_expression(Ast_dump_context*) const;
2539 private:
2540 // The underlying call expression.
2541 Expression* call_;
2542 // Which result we want.
2543 unsigned int index_;
2546 // An expression which represents a pointer to a function.
2548 class Func_expression : public Expression
2550 public:
2551 Func_expression(Named_object* function, Expression* closure,
2552 Location location)
2553 : Expression(EXPRESSION_FUNC_REFERENCE, location),
2554 function_(function), closure_(closure),
2555 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS)
2558 // Return the object associated with the function.
2559 Named_object*
2560 named_object() const
2561 { return this->function_; }
2563 // Return the closure for this function. This will return NULL if
2564 // the function has no closure, which is the normal case.
2565 Expression*
2566 closure()
2567 { return this->closure_; }
2569 // Return whether this is a reference to a runtime function.
2570 bool
2571 is_runtime_function() const
2572 { return this->runtime_code_ != Runtime::NUMBER_OF_FUNCTIONS; }
2574 // Return the runtime code for this function expression.
2575 // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
2576 // runtime function.
2577 Runtime::Function
2578 runtime_code() const
2579 { return this->runtime_code_; }
2581 // Set the runtime code for this function expression.
2582 void
2583 set_runtime_code(Runtime::Function code)
2584 { this->runtime_code_ = code; }
2586 // Return a backend expression for the code of a function.
2587 static Bexpression*
2588 get_code_pointer(Gogo*, Named_object* function, Location loc);
2590 protected:
2592 do_traverse(Traverse*);
2594 Type*
2595 do_type();
2597 void
2598 do_determine_type(const Type_context*)
2600 if (this->closure_ != NULL)
2601 this->closure_->determine_type_no_context();
2604 Expression*
2605 do_copy()
2607 return Expression::make_func_reference(this->function_,
2608 (this->closure_ == NULL
2609 ? NULL
2610 : this->closure_->copy()),
2611 this->location());
2614 Bexpression*
2615 do_get_backend(Translate_context*);
2617 void
2618 do_dump_expression(Ast_dump_context*) const;
2620 private:
2621 // The function itself.
2622 Named_object* function_;
2623 // A closure. This is normally NULL. For a nested function, it may
2624 // be a struct holding pointers to all the variables referenced by
2625 // this function and defined in enclosing functions.
2626 Expression* closure_;
2627 // The runtime code for the referenced function.
2628 Runtime::Function runtime_code_;
2631 // A function descriptor. A function descriptor is a struct with a
2632 // single field pointing to the function code. This is used for
2633 // functions without closures.
2635 class Func_descriptor_expression : public Expression
2637 public:
2638 Func_descriptor_expression(Named_object* fn);
2640 // Make the function descriptor type, so that it can be converted.
2641 static void
2642 make_func_descriptor_type();
2644 protected:
2646 do_traverse(Traverse*);
2648 Type*
2649 do_type();
2651 void
2652 do_determine_type(const Type_context*)
2655 Expression*
2656 do_copy()
2657 { return Expression::make_func_descriptor(this->fn_); }
2659 bool
2660 do_is_addressable() const
2661 { return true; }
2663 Bexpression*
2664 do_get_backend(Translate_context*);
2666 void
2667 do_dump_expression(Ast_dump_context* context) const;
2669 private:
2670 // The type of all function descriptors.
2671 static Type* descriptor_type;
2673 // The function for which this is the descriptor.
2674 Named_object* fn_;
2675 // The descriptor variable.
2676 Bvariable* dvar_;
2679 // A reference to an unknown name.
2681 class Unknown_expression : public Parser_expression
2683 public:
2684 Unknown_expression(Named_object* named_object, Location location)
2685 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
2686 named_object_(named_object), no_error_message_(false),
2687 is_composite_literal_key_(false)
2690 // The associated named object.
2691 Named_object*
2692 named_object() const
2693 { return this->named_object_; }
2695 // The name of the identifier which was unknown.
2696 const std::string&
2697 name() const;
2699 // Call this to indicate that we should not give an error if this
2700 // name is never defined. This is used to avoid knock-on errors
2701 // during an erroneous parse.
2702 void
2703 set_no_error_message()
2704 { this->no_error_message_ = true; }
2706 // Note that this expression is being used as the key in a composite
2707 // literal, so it may be OK if it is not resolved.
2708 void
2709 set_is_composite_literal_key()
2710 { this->is_composite_literal_key_ = true; }
2712 // Note that this expression should no longer be treated as a
2713 // composite literal key.
2714 void
2715 clear_is_composite_literal_key()
2716 { this->is_composite_literal_key_ = false; }
2718 protected:
2719 Expression*
2720 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2722 Expression*
2723 do_copy()
2724 { return new Unknown_expression(this->named_object_, this->location()); }
2726 void
2727 do_dump_expression(Ast_dump_context*) const;
2729 private:
2730 // The unknown name.
2731 Named_object* named_object_;
2732 // True if we should not give errors if this is undefined. This is
2733 // used if there was a parse failure.
2734 bool no_error_message_;
2735 // True if this is the key in a composite literal.
2736 bool is_composite_literal_key_;
2739 // An index expression. This is lowered to an array index, a string
2740 // index, or a map index.
2742 class Index_expression : public Parser_expression
2744 public:
2745 Index_expression(Expression* left, Expression* start, Expression* end,
2746 Expression* cap, Location location)
2747 : Parser_expression(EXPRESSION_INDEX, location),
2748 left_(left), start_(start), end_(end), cap_(cap)
2751 // Dump an index expression, i.e. an expression of the form
2752 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
2753 static void
2754 dump_index_expression(Ast_dump_context*, const Expression* expr,
2755 const Expression* start, const Expression* end,
2756 const Expression* cap);
2758 protected:
2760 do_traverse(Traverse*);
2762 Expression*
2763 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2765 Expression*
2766 do_copy()
2768 return new Index_expression(this->left_->copy(), this->start_->copy(),
2769 (this->end_ == NULL
2770 ? NULL
2771 : this->end_->copy()),
2772 (this->cap_ == NULL
2773 ? NULL
2774 : this->cap_->copy()),
2775 this->location());
2778 bool
2779 do_must_eval_subexpressions_in_order(int* skip) const
2781 *skip = 1;
2782 return true;
2785 void
2786 do_dump_expression(Ast_dump_context*) const;
2788 void
2789 do_issue_nil_check()
2790 { this->left_->issue_nil_check(); }
2791 private:
2792 // The expression being indexed.
2793 Expression* left_;
2794 // The first index.
2795 Expression* start_;
2796 // The second index. This is NULL for an index, non-NULL for a
2797 // slice.
2798 Expression* end_;
2799 // The capacity argument. This is NULL for indices and slices that use the
2800 // default capacity, non-NULL for indices and slices that specify the
2801 // capacity.
2802 Expression* cap_;
2805 // An array index. This is used for both indexing and slicing.
2807 class Array_index_expression : public Expression
2809 public:
2810 Array_index_expression(Expression* array, Expression* start,
2811 Expression* end, Expression* cap, Location location)
2812 : Expression(EXPRESSION_ARRAY_INDEX, location),
2813 array_(array), start_(start), end_(end), cap_(cap), type_(NULL),
2814 is_lvalue_(false)
2817 // Return the array.
2818 Expression*
2819 array()
2820 { return this->array_; }
2822 const Expression*
2823 array() const
2824 { return this->array_; }
2826 // Return the index of a simple index expression, or the start index
2827 // of a slice expression.
2828 Expression*
2829 start()
2830 { return this->start_; }
2832 const Expression*
2833 start() const
2834 { return this->start_; }
2836 // Return the end index of a slice expression. This is NULL for a
2837 // simple index expression.
2838 Expression*
2839 end()
2840 { return this->end_; }
2842 const Expression*
2843 end() const
2844 { return this->end_; }
2846 // Return whether this array index expression appears in an lvalue
2847 // (left hand side of assignment) context.
2848 bool
2849 is_lvalue() const
2850 { return this->is_lvalue_; }
2852 // Update this array index expression to indicate that it appears
2853 // in a left-hand-side or lvalue context.
2854 void
2855 set_is_lvalue()
2856 { this->is_lvalue_ = true; }
2858 protected:
2860 do_traverse(Traverse*);
2862 Expression*
2863 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2865 Type*
2866 do_type();
2868 void
2869 do_determine_type(const Type_context*);
2871 void
2872 do_check_types(Gogo*);
2874 Expression*
2875 do_copy()
2877 return Expression::make_array_index(this->array_->copy(),
2878 this->start_->copy(),
2879 (this->end_ == NULL
2880 ? NULL
2881 : this->end_->copy()),
2882 (this->cap_ == NULL
2883 ? NULL
2884 : this->cap_->copy()),
2885 this->location());
2888 bool
2889 do_must_eval_subexpressions_in_order(int* skip) const
2891 *skip = 1;
2892 return true;
2895 bool
2896 do_is_addressable() const;
2898 void
2899 do_address_taken(bool escapes);
2901 void
2902 do_issue_nil_check()
2903 { this->array_->issue_nil_check(); }
2905 Bexpression*
2906 do_get_backend(Translate_context*);
2908 void
2909 do_dump_expression(Ast_dump_context*) const;
2911 private:
2912 // The array we are getting a value from.
2913 Expression* array_;
2914 // The start or only index.
2915 Expression* start_;
2916 // The end index of a slice. This may be NULL for a simple array
2917 // index, or it may be a nil expression for the length of the array.
2918 Expression* end_;
2919 // The capacity argument of a slice. This may be NULL for an array index or
2920 // slice.
2921 Expression* cap_;
2922 // The type of the expression.
2923 Type* type_;
2924 // Whether expr appears in an lvalue context.
2925 bool is_lvalue_;
2928 // A string index. This is used for both indexing and slicing.
2930 class String_index_expression : public Expression
2932 public:
2933 String_index_expression(Expression* string, Expression* start,
2934 Expression* end, Location location)
2935 : Expression(EXPRESSION_STRING_INDEX, location),
2936 string_(string), start_(start), end_(end)
2939 // Return the string being indexed.
2940 Expression*
2941 string() const
2942 { return this->string_; }
2944 protected:
2946 do_traverse(Traverse*);
2948 Expression*
2949 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2951 Type*
2952 do_type();
2954 void
2955 do_determine_type(const Type_context*);
2957 void
2958 do_check_types(Gogo*);
2960 Expression*
2961 do_copy()
2963 return Expression::make_string_index(this->string_->copy(),
2964 this->start_->copy(),
2965 (this->end_ == NULL
2966 ? NULL
2967 : this->end_->copy()),
2968 this->location());
2971 bool
2972 do_must_eval_subexpressions_in_order(int* skip) const
2974 *skip = 1;
2975 return true;
2978 Bexpression*
2979 do_get_backend(Translate_context*);
2981 void
2982 do_dump_expression(Ast_dump_context*) const;
2984 private:
2985 // The string we are getting a value from.
2986 Expression* string_;
2987 // The start or only index.
2988 Expression* start_;
2989 // The end index of a slice. This may be NULL for a single index,
2990 // or it may be a nil expression for the length of the string.
2991 Expression* end_;
2994 // An index into a map.
2996 class Map_index_expression : public Expression
2998 public:
2999 Map_index_expression(Expression* map, Expression* index,
3000 Location location)
3001 : Expression(EXPRESSION_MAP_INDEX, location),
3002 map_(map), index_(index), value_pointer_(NULL)
3005 // Return the map.
3006 Expression*
3007 map()
3008 { return this->map_; }
3010 const Expression*
3011 map() const
3012 { return this->map_; }
3014 // Return the index.
3015 Expression*
3016 index()
3017 { return this->index_; }
3019 const Expression*
3020 index() const
3021 { return this->index_; }
3023 // Get the type of the map being indexed.
3024 Map_type*
3025 get_map_type() const;
3027 // Return an expression for the map index. This returns an
3028 // expression that evaluates to a pointer to a value in the map. If
3029 // the key is not present in the map, this will return a pointer to
3030 // the zero value.
3031 Expression*
3032 get_value_pointer(Gogo*);
3034 protected:
3036 do_traverse(Traverse*);
3038 Expression*
3039 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3041 Type*
3042 do_type();
3044 void
3045 do_determine_type(const Type_context*);
3047 void
3048 do_check_types(Gogo*);
3050 Expression*
3051 do_copy()
3053 return Expression::make_map_index(this->map_->copy(),
3054 this->index_->copy(),
3055 this->location());
3058 bool
3059 do_must_eval_subexpressions_in_order(int* skip) const
3061 *skip = 1;
3062 return true;
3065 // A map index expression is an lvalue but it is not addressable.
3067 Bexpression*
3068 do_get_backend(Translate_context*);
3070 void
3071 do_dump_expression(Ast_dump_context*) const;
3073 private:
3074 // The map we are looking into.
3075 Expression* map_;
3076 // The index.
3077 Expression* index_;
3078 // A pointer to the value at this index.
3079 Expression* value_pointer_;
3082 // An expression which represents a method bound to its first
3083 // argument.
3085 class Bound_method_expression : public Expression
3087 public:
3088 Bound_method_expression(Expression* expr, const Method *method,
3089 Named_object* function, Location location)
3090 : Expression(EXPRESSION_BOUND_METHOD, location),
3091 expr_(expr), expr_type_(NULL), method_(method), function_(function)
3094 // Return the object which is the first argument.
3095 Expression*
3096 first_argument()
3097 { return this->expr_; }
3099 // Return the implicit type of the first argument. This will be
3100 // non-NULL when using a method from an anonymous field without
3101 // using an explicit stub.
3102 Type*
3103 first_argument_type() const
3104 { return this->expr_type_; }
3106 // Return the method.
3107 const Method*
3108 method() const
3109 { return this->method_; }
3111 // Return the function to call.
3112 Named_object*
3113 function() const
3114 { return this->function_; }
3116 // Set the implicit type of the expression.
3117 void
3118 set_first_argument_type(Type* type)
3119 { this->expr_type_ = type; }
3121 // Create a thunk to call FUNCTION, for METHOD, when it is used as
3122 // part of a method value.
3123 static Named_object*
3124 create_thunk(Gogo*, const Method* method, Named_object* function);
3126 protected:
3128 do_traverse(Traverse*);
3130 Expression*
3131 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3133 Type*
3134 do_type();
3136 void
3137 do_determine_type(const Type_context*);
3139 void
3140 do_check_types(Gogo*);
3142 Expression*
3143 do_copy()
3145 return new Bound_method_expression(this->expr_->copy(), this->method_,
3146 this->function_, this->location());
3149 Bexpression*
3150 do_get_backend(Translate_context*)
3151 { go_unreachable(); }
3153 void
3154 do_dump_expression(Ast_dump_context*) const;
3156 private:
3157 // A mapping from method functions to the thunks we have created for
3158 // them.
3159 typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
3160 static Method_value_thunks method_value_thunks;
3162 // The object used to find the method. This is passed to the method
3163 // as the first argument.
3164 Expression* expr_;
3165 // The implicit type of the object to pass to the method. This is
3166 // NULL in the normal case, non-NULL when using a method from an
3167 // anonymous field which does not require a stub.
3168 Type* expr_type_;
3169 // The method.
3170 const Method* method_;
3171 // The function to call. This is not the same as
3172 // method_->named_object() when the method has a stub. This will be
3173 // the real function rather than the stub.
3174 Named_object* function_;
3177 // A reference to a field in a struct.
3179 class Field_reference_expression : public Expression
3181 public:
3182 Field_reference_expression(Expression* expr, unsigned int field_index,
3183 Location location)
3184 : Expression(EXPRESSION_FIELD_REFERENCE, location),
3185 expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
3188 // Return the struct expression.
3189 Expression*
3190 expr() const
3191 { return this->expr_; }
3193 // Return the field index.
3194 unsigned int
3195 field_index() const
3196 { return this->field_index_; }
3198 // Return whether this node was implied by an anonymous field.
3199 bool
3200 implicit() const
3201 { return this->implicit_; }
3203 void
3204 set_implicit(bool implicit)
3205 { this->implicit_ = implicit; }
3207 // Set the struct expression. This is used when parsing.
3208 void
3209 set_struct_expression(Expression* expr)
3211 go_assert(this->expr_ == NULL);
3212 this->expr_ = expr;
3215 protected:
3217 do_traverse(Traverse* traverse)
3218 { return Expression::traverse(&this->expr_, traverse); }
3220 Expression*
3221 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3223 Type*
3224 do_type();
3226 void
3227 do_determine_type(const Type_context*)
3228 { this->expr_->determine_type_no_context(); }
3230 void
3231 do_check_types(Gogo*);
3233 Expression*
3234 do_copy()
3236 return Expression::make_field_reference(this->expr_->copy(),
3237 this->field_index_,
3238 this->location());
3241 bool
3242 do_is_addressable() const
3243 { return this->expr_->is_addressable(); }
3245 void
3246 do_address_taken(bool escapes)
3247 { this->expr_->address_taken(escapes); }
3249 void
3250 do_issue_nil_check()
3251 { this->expr_->issue_nil_check(); }
3253 Bexpression*
3254 do_get_backend(Translate_context*);
3256 void
3257 do_dump_expression(Ast_dump_context*) const;
3259 private:
3260 // The expression we are looking into. This should have a type of
3261 // struct.
3262 Expression* expr_;
3263 // The zero-based index of the field we are retrieving.
3264 unsigned int field_index_;
3265 // Whether this node was emitted implicitly for an embedded field,
3266 // that is, expr_ is not the expr_ of the original user node.
3267 bool implicit_;
3268 // Whether we have already emitted a fieldtrack call.
3269 bool called_fieldtrack_;
3272 // A reference to a field of an interface.
3274 class Interface_field_reference_expression : public Expression
3276 public:
3277 Interface_field_reference_expression(Expression* expr,
3278 const std::string& name,
3279 Location location)
3280 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
3281 expr_(expr), name_(name)
3284 // Return the expression for the interface object.
3285 Expression*
3286 expr()
3287 { return this->expr_; }
3289 // Return the name of the method to call.
3290 const std::string&
3291 name() const
3292 { return this->name_; }
3294 // Create a thunk to call the method NAME in TYPE when it is used as
3295 // part of a method value.
3296 static Named_object*
3297 create_thunk(Gogo*, Interface_type* type, const std::string& name);
3299 // Return an expression for the pointer to the function to call.
3300 Expression*
3301 get_function();
3303 // Return an expression for the first argument to pass to the interface
3304 // function. This is the real object associated with the interface object.
3305 Expression*
3306 get_underlying_object();
3308 protected:
3310 do_traverse(Traverse* traverse);
3312 Expression*
3313 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3315 Type*
3316 do_type();
3318 void
3319 do_determine_type(const Type_context*);
3321 void
3322 do_check_types(Gogo*);
3324 Expression*
3325 do_copy()
3327 return Expression::make_interface_field_reference(this->expr_->copy(),
3328 this->name_,
3329 this->location());
3332 Bexpression*
3333 do_get_backend(Translate_context*);
3335 void
3336 do_dump_expression(Ast_dump_context*) const;
3338 private:
3339 // A mapping from interface types to a list of thunks we have
3340 // created for methods.
3341 typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
3342 typedef Unordered_map(Interface_type*, Method_thunks*)
3343 Interface_method_thunks;
3344 static Interface_method_thunks interface_method_thunks;
3346 // The expression for the interface object. This should have a type
3347 // of interface or pointer to interface.
3348 Expression* expr_;
3349 // The field we are retrieving--the name of the method.
3350 std::string name_;
3353 // Implement the builtin function new.
3355 class Allocation_expression : public Expression
3357 public:
3358 Allocation_expression(Type* type, Location location)
3359 : Expression(EXPRESSION_ALLOCATION, location),
3360 type_(type), allocate_on_stack_(false)
3363 void
3364 set_allocate_on_stack()
3365 { this->allocate_on_stack_ = true; }
3367 protected:
3369 do_traverse(Traverse*);
3371 Type*
3372 do_type();
3374 void
3375 do_determine_type(const Type_context*)
3378 void
3379 do_check_types(Gogo*);
3381 Expression*
3382 do_copy();
3384 Bexpression*
3385 do_get_backend(Translate_context*);
3387 void
3388 do_dump_expression(Ast_dump_context*) const;
3390 private:
3391 // The type we are allocating.
3392 Type* type_;
3393 // Whether or not this is a stack allocation.
3394 bool allocate_on_stack_;
3397 // A general composite literal. This is lowered to a type specific
3398 // version.
3400 class Composite_literal_expression : public Parser_expression
3402 public:
3403 Composite_literal_expression(Type* type, int depth, bool has_keys,
3404 Expression_list* vals, bool all_are_names,
3405 Location location)
3406 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
3407 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys),
3408 all_are_names_(all_are_names), key_path_(std::vector<bool>(depth))
3412 // Mark the DEPTH entry of KEY_PATH as containing a key.
3413 void
3414 update_key_path(size_t depth)
3416 go_assert(depth < this->key_path_.size());
3417 this->key_path_[depth] = true;
3420 protected:
3422 do_traverse(Traverse* traverse);
3424 Expression*
3425 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3427 Expression*
3428 do_copy()
3430 Composite_literal_expression *ret =
3431 new Composite_literal_expression(this->type_, this->depth_,
3432 this->has_keys_,
3433 (this->vals_ == NULL
3434 ? NULL
3435 : this->vals_->copy()),
3436 this->all_are_names_,
3437 this->location());
3438 ret->key_path_ = this->key_path_;
3439 return ret;
3442 void
3443 do_dump_expression(Ast_dump_context*) const;
3445 private:
3446 Expression*
3447 lower_struct(Gogo*, Type*);
3449 Expression*
3450 lower_array(Type*);
3452 Expression*
3453 make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
3455 Expression*
3456 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
3458 // The type of the composite literal.
3459 Type* type_;
3460 // The depth within a list of composite literals within a composite
3461 // literal, when the type is omitted.
3462 int depth_;
3463 // The values to put in the composite literal.
3464 Expression_list* vals_;
3465 // If this is true, then VALS_ is a list of pairs: a key and a
3466 // value. In an array initializer, a missing key will be NULL.
3467 bool has_keys_;
3468 // If this is true, then HAS_KEYS_ is true, and every key is a
3469 // simple identifier.
3470 bool all_are_names_;
3471 // A complement to DEPTH that indicates for each level starting from 0 to
3472 // DEPTH-1 whether or not this composite literal is nested inside of key or
3473 // a value. This is used to decide which type to use when given a map literal
3474 // with omitted key types.
3475 std::vector<bool> key_path_;
3478 // Helper/mixin class for struct and array construction expressions;
3479 // encapsulates a list of values plus an optional traversal order
3480 // recording the order in which the values should be visited.
3482 class Ordered_value_list
3484 public:
3485 Ordered_value_list(Expression_list* vals)
3486 : vals_(vals), traverse_order_(NULL)
3489 Expression_list*
3490 vals() const
3491 { return this->vals_; }
3494 traverse_vals(Traverse* traverse);
3496 // Get the traversal order (may be NULL)
3497 std::vector<unsigned long>*
3498 traverse_order()
3499 { return traverse_order_; }
3501 // Set the traversal order, used to ensure that we implement the
3502 // order of evaluation rules. Takes ownership of the argument.
3503 void
3504 set_traverse_order(std::vector<unsigned long>* traverse_order)
3505 { this->traverse_order_ = traverse_order; }
3507 private:
3508 // The list of values, in order of the fields in the struct or in
3509 // order of indices in an array. A NULL value of vals_ means that
3510 // all fields/slots should be zero-initialized; a single NULL entry
3511 // in the list means that the corresponding field or array slot
3512 // should be zero-initialized.
3513 Expression_list* vals_;
3514 // If not NULL, the order in which to traverse vals_. This is used
3515 // so that we implement the order of evaluation rules correctly.
3516 std::vector<unsigned long>* traverse_order_;
3519 // Construct a struct.
3521 class Struct_construction_expression : public Expression,
3522 public Ordered_value_list
3524 public:
3525 Struct_construction_expression(Type* type, Expression_list* vals,
3526 Location location)
3527 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
3528 Ordered_value_list(vals),
3529 type_(type)
3532 // Return whether this is a constant initializer.
3533 bool
3534 is_constant_struct() const;
3536 protected:
3538 do_traverse(Traverse* traverse);
3540 bool
3541 do_is_static_initializer() const;
3543 Type*
3544 do_type()
3545 { return this->type_; }
3547 void
3548 do_determine_type(const Type_context*);
3550 void
3551 do_check_types(Gogo*);
3553 Expression*
3554 do_copy()
3556 Struct_construction_expression* ret =
3557 new Struct_construction_expression(this->type_,
3558 (this->vals() == NULL
3559 ? NULL
3560 : this->vals()->copy()),
3561 this->location());
3562 if (this->traverse_order() != NULL)
3563 ret->set_traverse_order(this->traverse_order());
3564 return ret;
3567 Expression*
3568 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3570 Bexpression*
3571 do_get_backend(Translate_context*);
3573 void
3574 do_export(Export*) const;
3576 void
3577 do_dump_expression(Ast_dump_context*) const;
3579 private:
3580 // The type of the struct to construct.
3581 Type* type_;
3584 // Construct an array. This class is not used directly; instead we
3585 // use the child classes, Fixed_array_construction_expression and
3586 // Slice_construction_expression.
3588 class Array_construction_expression : public Expression,
3589 public Ordered_value_list
3591 protected:
3592 Array_construction_expression(Expression_classification classification,
3593 Type* type,
3594 const std::vector<unsigned long>* indexes,
3595 Expression_list* vals, Location location)
3596 : Expression(classification, location),
3597 Ordered_value_list(vals),
3598 type_(type), indexes_(indexes)
3599 { go_assert(indexes == NULL || indexes->size() == vals->size()); }
3601 public:
3602 // Return whether this is a constant initializer.
3603 bool
3604 is_constant_array() const;
3606 // Return the number of elements.
3607 size_t
3608 element_count() const
3609 { return this->vals() == NULL ? 0 : this->vals()->size(); }
3611 protected:
3612 virtual int
3613 do_traverse(Traverse* traverse);
3615 bool
3616 do_is_static_initializer() const;
3618 Type*
3619 do_type()
3620 { return this->type_; }
3622 void
3623 do_determine_type(const Type_context*);
3625 void
3626 do_check_types(Gogo*);
3628 void
3629 do_export(Export*) const;
3631 // The indexes.
3632 const std::vector<unsigned long>*
3633 indexes()
3634 { return this->indexes_; }
3636 Expression*
3637 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3639 // Get the backend constructor for the array values.
3640 Bexpression*
3641 get_constructor(Translate_context* context, Btype* btype);
3643 void
3644 do_dump_expression(Ast_dump_context*) const;
3646 virtual void
3647 dump_slice_storage_expression(Ast_dump_context*) const { }
3649 private:
3650 // The type of the array to construct.
3651 Type* type_;
3652 // The list of indexes into the array, one for each value. This may
3653 // be NULL, in which case the indexes start at zero and increment.
3654 const std::vector<unsigned long>* indexes_;
3657 // Construct a fixed array.
3659 class Fixed_array_construction_expression :
3660 public Array_construction_expression
3662 public:
3663 Fixed_array_construction_expression(Type* type,
3664 const std::vector<unsigned long>* indexes,
3665 Expression_list* vals, Location location);
3667 protected:
3668 Expression*
3669 do_copy()
3671 return new Fixed_array_construction_expression(this->type(),
3672 this->indexes(),
3673 (this->vals() == NULL
3674 ? NULL
3675 : this->vals()->copy()),
3676 this->location());
3679 Bexpression*
3680 do_get_backend(Translate_context*);
3683 // Construct a slice.
3685 class Slice_construction_expression : public Array_construction_expression
3687 public:
3688 Slice_construction_expression(Type* type,
3689 const std::vector<unsigned long>* indexes,
3690 Expression_list* vals, Location location);
3692 Expression*
3693 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3695 // Record that the storage for this slice (e.g. vals) cannot escape,
3696 // hence it can be stack-allocated.
3697 void
3698 set_storage_does_not_escape()
3700 this->storage_escapes_ = false;
3703 protected:
3704 // Note that taking the address of a slice literal is invalid.
3707 do_traverse(Traverse* traverse);
3709 Expression*
3710 do_copy()
3712 return new Slice_construction_expression(this->type(), this->indexes(),
3713 (this->vals() == NULL
3714 ? NULL
3715 : this->vals()->copy()),
3716 this->location());
3719 Bexpression*
3720 do_get_backend(Translate_context*);
3722 void
3723 dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const;
3725 // Create an array value for the constructed slice. Invoked during
3726 // flattening if slice storage does not escape, otherwise invoked
3727 // later on during do_get_backend().
3728 Expression*
3729 create_array_val();
3731 private:
3732 // The type of the values in this slice.
3733 Type* valtype_;
3734 // Array value expression, optionally filled in during flattening.
3735 Expression* array_val_;
3736 // Slice storage expression, optionally filled in during flattening.
3737 Expression* slice_storage_;
3738 // Normally true. Can be set to false if we know that the resulting
3739 // storage for the slice cannot escape.
3740 bool storage_escapes_;
3743 // Construct a map.
3745 class Map_construction_expression : public Expression
3747 public:
3748 Map_construction_expression(Type* type, Expression_list* vals,
3749 Location location)
3750 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
3751 type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL)
3752 { go_assert(vals == NULL || vals->size() % 2 == 0); }
3754 Expression_list*
3755 vals() const
3756 { return this->vals_; }
3758 protected:
3760 do_traverse(Traverse* traverse);
3762 Expression*
3763 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3765 Type*
3766 do_type()
3767 { return this->type_; }
3769 void
3770 do_determine_type(const Type_context*);
3772 void
3773 do_check_types(Gogo*);
3775 Expression*
3776 do_copy()
3778 return new Map_construction_expression(this->type_,
3779 (this->vals_ == NULL
3780 ? NULL
3781 : this->vals_->copy()),
3782 this->location());
3785 Bexpression*
3786 do_get_backend(Translate_context*);
3788 void
3789 do_export(Export*) const;
3791 void
3792 do_dump_expression(Ast_dump_context*) const;
3794 private:
3795 // The type of the map to construct.
3796 Type* type_;
3797 // The list of values.
3798 Expression_list* vals_;
3799 // The type of the key-value pair struct for each map element.
3800 Struct_type* element_type_;
3801 // A temporary reference to the variable storing the constructor initializer.
3802 Temporary_statement* constructor_temp_;
3805 // A type guard expression.
3807 class Type_guard_expression : public Expression
3809 public:
3810 Type_guard_expression(Expression* expr, Type* type, Location location)
3811 : Expression(EXPRESSION_TYPE_GUARD, location),
3812 expr_(expr), type_(type)
3815 // Return the expression to convert.
3816 Expression*
3817 expr()
3818 { return this->expr_; }
3820 // Return the type to which to convert.
3821 Type*
3822 type()
3823 { return this->type_; }
3825 protected:
3827 do_traverse(Traverse* traverse);
3829 Expression*
3830 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3832 Type*
3833 do_type()
3834 { return this->type_; }
3836 void
3837 do_determine_type(const Type_context*)
3838 { this->expr_->determine_type_no_context(); }
3840 void
3841 do_check_types(Gogo*);
3843 Expression*
3844 do_copy()
3846 return new Type_guard_expression(this->expr_->copy(), this->type_,
3847 this->location());
3850 Bexpression*
3851 do_get_backend(Translate_context*);
3853 void
3854 do_dump_expression(Ast_dump_context*) const;
3856 private:
3857 // The expression to convert.
3858 Expression* expr_;
3859 // The type to which to convert.
3860 Type* type_;
3863 // Class Heap_expression.
3865 // When you take the address of an escaping expression, it is allocated
3866 // on the heap. This class implements that.
3868 class Heap_expression : public Expression
3870 public:
3871 Heap_expression(Expression* expr, Location location)
3872 : Expression(EXPRESSION_HEAP, location),
3873 expr_(expr)
3876 Expression*
3877 expr() const
3878 { return this->expr_; }
3880 protected:
3882 do_traverse(Traverse* traverse)
3883 { return Expression::traverse(&this->expr_, traverse); }
3885 Type*
3886 do_type();
3887 void
3888 do_determine_type(const Type_context*)
3889 { this->expr_->determine_type_no_context(); }
3891 Expression*
3892 do_copy()
3894 return Expression::make_heap_expression(this->expr_->copy(),
3895 this->location());
3898 Bexpression*
3899 do_get_backend(Translate_context*);
3901 // We only export global objects, and the parser does not generate
3902 // this in global scope.
3903 void
3904 do_export(Export*) const
3905 { go_unreachable(); }
3907 void
3908 do_dump_expression(Ast_dump_context*) const;
3910 private:
3911 // The expression which is being put on the heap.
3912 Expression* expr_;
3915 // A receive expression.
3917 class Receive_expression : public Expression
3919 public:
3920 Receive_expression(Expression* channel, Location location)
3921 : Expression(EXPRESSION_RECEIVE, location),
3922 channel_(channel), temp_receiver_(NULL)
3925 // Return the channel.
3926 Expression*
3927 channel()
3928 { return this->channel_; }
3930 protected:
3932 do_traverse(Traverse* traverse)
3933 { return Expression::traverse(&this->channel_, traverse); }
3935 bool
3936 do_discarding_value()
3937 { return true; }
3939 Type*
3940 do_type();
3942 Expression*
3943 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3945 void
3946 do_determine_type(const Type_context*)
3947 { this->channel_->determine_type_no_context(); }
3949 void
3950 do_check_types(Gogo*);
3952 Expression*
3953 do_copy()
3955 return Expression::make_receive(this->channel_->copy(), this->location());
3958 bool
3959 do_must_eval_in_order() const
3960 { return true; }
3962 Bexpression*
3963 do_get_backend(Translate_context*);
3965 void
3966 do_dump_expression(Ast_dump_context*) const;
3968 private:
3969 // The channel from which we are receiving.
3970 Expression* channel_;
3971 // A temporary reference to the variable storing the received data.
3972 Temporary_statement* temp_receiver_;
3975 // Conditional expressions.
3977 class Conditional_expression : public Expression
3979 public:
3980 Conditional_expression(Expression* cond, Expression* then_expr,
3981 Expression* else_expr, Location location)
3982 : Expression(EXPRESSION_CONDITIONAL, location),
3983 cond_(cond), then_(then_expr), else_(else_expr)
3986 Expression*
3987 condition() const
3988 { return this->cond_; }
3990 protected:
3992 do_traverse(Traverse*);
3994 Type*
3995 do_type();
3997 void
3998 do_determine_type(const Type_context*);
4000 Expression*
4001 do_copy()
4003 return new Conditional_expression(this->cond_->copy(), this->then_->copy(),
4004 this->else_->copy(), this->location());
4007 Bexpression*
4008 do_get_backend(Translate_context* context);
4010 void
4011 do_dump_expression(Ast_dump_context*) const;
4013 private:
4014 // The condition to be checked.
4015 Expression* cond_;
4016 // The expression to execute if the condition is true.
4017 Expression* then_;
4018 // The expression to execute if the condition is false.
4019 Expression* else_;
4022 // Compound expressions.
4024 class Compound_expression : public Expression
4026 public:
4027 Compound_expression(Expression* init, Expression* expr, Location location)
4028 : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr)
4031 Expression*
4032 init() const
4033 { return this->init_; }
4035 protected:
4037 do_traverse(Traverse*);
4039 Type*
4040 do_type();
4042 void
4043 do_determine_type(const Type_context*);
4045 Expression*
4046 do_copy()
4048 return new Compound_expression(this->init_->copy(), this->expr_->copy(),
4049 this->location());
4052 Bexpression*
4053 do_get_backend(Translate_context* context);
4055 void
4056 do_dump_expression(Ast_dump_context*) const;
4058 private:
4059 // The expression that is evaluated first and discarded.
4060 Expression* init_;
4061 // The expression that is evaluated and returned.
4062 Expression* expr_;
4065 // A backend expression. This is a backend expression wrapped in an
4066 // Expression, for convenience during backend generation.
4068 class Backend_expression : public Expression
4070 public:
4071 Backend_expression(Bexpression* bexpr, Type* type, Location location)
4072 : Expression(EXPRESSION_BACKEND, location), bexpr_(bexpr), type_(type)
4075 protected:
4077 do_traverse(Traverse*);
4079 // For now these are always valid static initializers. If that
4080 // changes we can change this.
4081 bool
4082 do_is_static_initializer() const
4083 { return true; }
4085 Type*
4086 do_type()
4087 { return this->type_; }
4089 void
4090 do_determine_type(const Type_context*)
4093 Expression*
4094 do_copy()
4096 return new Backend_expression(this->bexpr_, this->type_, this->location());
4099 Bexpression*
4100 do_get_backend(Translate_context*)
4101 { return this->bexpr_; }
4103 void
4104 do_dump_expression(Ast_dump_context*) const;
4106 private:
4107 // The backend expression we are wrapping.
4108 Bexpression* bexpr_;
4109 // The type of the expression;
4110 Type* type_;
4113 // A numeric constant. This is used both for untyped constants and
4114 // for constants that have a type.
4116 class Numeric_constant
4118 public:
4119 Numeric_constant()
4120 : classification_(NC_INVALID), type_(NULL)
4123 ~Numeric_constant();
4125 Numeric_constant(const Numeric_constant&);
4127 Numeric_constant& operator=(const Numeric_constant&);
4129 // Set to an unsigned long value.
4130 void
4131 set_unsigned_long(Type*, unsigned long);
4133 // Set to an integer value.
4134 void
4135 set_int(Type*, const mpz_t);
4137 // Set to a rune value.
4138 void
4139 set_rune(Type*, const mpz_t);
4141 // Set to a floating point value.
4142 void
4143 set_float(Type*, const mpfr_t);
4145 // Set to a complex value.
4146 void
4147 set_complex(Type*, const mpc_t);
4149 // Mark numeric constant as invalid.
4150 void
4151 set_invalid()
4152 { this->classification_ = NC_INVALID; }
4154 // Classifiers.
4155 bool
4156 is_int() const
4157 { return this->classification_ == Numeric_constant::NC_INT; }
4159 bool
4160 is_rune() const
4161 { return this->classification_ == Numeric_constant::NC_RUNE; }
4163 bool
4164 is_float() const
4165 { return this->classification_ == Numeric_constant::NC_FLOAT; }
4167 bool
4168 is_complex() const
4169 { return this->classification_ == Numeric_constant::NC_COMPLEX; }
4171 bool
4172 is_invalid() const
4173 { return this->classification_ == Numeric_constant::NC_INVALID; }
4175 // Value retrievers. These will initialize the values as well as
4176 // set them. GET_INT is only valid if IS_INT returns true, and
4177 // likewise respectively.
4178 void
4179 get_int(mpz_t*) const;
4181 void
4182 get_rune(mpz_t*) const;
4184 void
4185 get_float(mpfr_t*) const;
4187 void
4188 get_complex(mpc_t*) const;
4190 // Codes returned by to_unsigned_long.
4191 enum To_unsigned_long
4193 // Value is integer and fits in unsigned long.
4194 NC_UL_VALID,
4195 // Value is not integer.
4196 NC_UL_NOTINT,
4197 // Value is integer but is negative.
4198 NC_UL_NEGATIVE,
4199 // Value is non-negative integer but does not fit in unsigned
4200 // long.
4201 NC_UL_BIG
4204 // If the value can be expressed as an integer that fits in an
4205 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
4206 // one of the other To_unsigned_long codes.
4207 To_unsigned_long
4208 to_unsigned_long(unsigned long* val) const;
4210 // If the value can be expressed as an integer that describes the
4211 // size of an object in memory, set *VAL and return true.
4212 // Otherwise, return false. Currently we use int64_t to represent a
4213 // memory size, as in Type::backend_type_size.
4214 bool
4215 to_memory_size(int64_t* val) const;
4217 // If the value can be expressed as an int, return true and
4218 // initialize and set VAL. This will return false for a value with
4219 // an explicit float or complex type, even if the value is integral.
4220 bool
4221 to_int(mpz_t* val) const;
4223 // If the value can be expressed as a float, return true and
4224 // initialize and set VAL.
4225 bool
4226 to_float(mpfr_t* val) const;
4228 // If the value can be expressed as a complex, return true and
4229 // initialize and set VR and VI.
4230 bool
4231 to_complex(mpc_t* val) const;
4233 // Get the type.
4234 Type*
4235 type() const;
4237 // If the constant can be expressed in TYPE, then set the type of
4238 // the constant to TYPE and return true. Otherwise return false,
4239 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
4240 // the location to use for the error.
4241 bool
4242 set_type(Type* type, bool issue_error, Location location);
4244 // Return an Expression for this value.
4245 Expression*
4246 expression(Location) const;
4248 private:
4249 void
4250 clear();
4252 To_unsigned_long
4253 mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
4255 To_unsigned_long
4256 mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
4258 bool
4259 mpz_to_memory_size(const mpz_t ival, int64_t* val) const;
4261 bool
4262 mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const;
4264 bool
4265 check_int_type(Integer_type*, bool, Location);
4267 bool
4268 check_float_type(Float_type*, bool, Location);
4270 bool
4271 check_complex_type(Complex_type*, bool, Location);
4273 // The kinds of constants.
4274 enum Classification
4276 NC_INVALID,
4277 NC_RUNE,
4278 NC_INT,
4279 NC_FLOAT,
4280 NC_COMPLEX
4283 // The kind of constant.
4284 Classification classification_;
4285 // The value.
4286 union
4288 // If NC_INT or NC_RUNE.
4289 mpz_t int_val;
4290 // If NC_FLOAT.
4291 mpfr_t float_val;
4292 // If NC_COMPLEX.
4293 mpc_t complex_val;
4294 } u_;
4295 // The type if there is one. This will be NULL for an untyped
4296 // constant.
4297 Type* type_;
4300 #endif // !defined(GO_EXPRESSIONS_H)