runtime: revert eqtype for AIX
[official-gcc.git] / gcc / go / gofrontend / expressions.h
blobacb2732bddea4ba6e59e33f3617900a7c1077119
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 Slice_value_expression;
65 class Conditional_expression;
66 class Compound_expression;
67 class Numeric_constant;
68 class Named_object;
69 class Export_function_body;
70 class Import_expression;
71 class Temporary_statement;
72 class Label;
73 class Ast_dump_context;
74 class String_dump;
76 // The precision to use for complex values represented as an mpc_t.
77 const int mpc_precision = 256;
79 // The base class for all expressions.
81 class Expression
83 public:
84 // The types of expressions.
85 enum Expression_classification
87 EXPRESSION_ERROR,
88 EXPRESSION_TYPE,
89 EXPRESSION_UNARY,
90 EXPRESSION_BINARY,
91 EXPRESSION_STRING_CONCAT,
92 EXPRESSION_CONST_REFERENCE,
93 EXPRESSION_VAR_REFERENCE,
94 EXPRESSION_ENCLOSED_VAR_REFERENCE,
95 EXPRESSION_TEMPORARY_REFERENCE,
96 EXPRESSION_SET_AND_USE_TEMPORARY,
97 EXPRESSION_SINK,
98 EXPRESSION_FUNC_REFERENCE,
99 EXPRESSION_FUNC_DESCRIPTOR,
100 EXPRESSION_FUNC_CODE_REFERENCE,
101 EXPRESSION_UNKNOWN_REFERENCE,
102 EXPRESSION_BOOLEAN,
103 EXPRESSION_STRING,
104 EXPRESSION_STRING_INFO,
105 EXPRESSION_STRING_VALUE,
106 EXPRESSION_INTEGER,
107 EXPRESSION_FLOAT,
108 EXPRESSION_COMPLEX,
109 EXPRESSION_NIL,
110 EXPRESSION_IOTA,
111 EXPRESSION_CALL,
112 EXPRESSION_CALL_RESULT,
113 EXPRESSION_BOUND_METHOD,
114 EXPRESSION_INDEX,
115 EXPRESSION_ARRAY_INDEX,
116 EXPRESSION_STRING_INDEX,
117 EXPRESSION_MAP_INDEX,
118 EXPRESSION_SELECTOR,
119 EXPRESSION_FIELD_REFERENCE,
120 EXPRESSION_INTERFACE_FIELD_REFERENCE,
121 EXPRESSION_ALLOCATION,
122 EXPRESSION_TYPE_GUARD,
123 EXPRESSION_CONVERSION,
124 EXPRESSION_UNSAFE_CONVERSION,
125 EXPRESSION_STRUCT_CONSTRUCTION,
126 EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
127 EXPRESSION_SLICE_CONSTRUCTION,
128 EXPRESSION_MAP_CONSTRUCTION,
129 EXPRESSION_COMPOSITE_LITERAL,
130 EXPRESSION_COMPOSITE_LITERAL_KEY,
131 EXPRESSION_HEAP,
132 EXPRESSION_RECEIVE,
133 EXPRESSION_TYPE_DESCRIPTOR,
134 EXPRESSION_GC_SYMBOL,
135 EXPRESSION_PTRMASK_SYMBOL,
136 EXPRESSION_TYPE_INFO,
137 EXPRESSION_SLICE_INFO,
138 EXPRESSION_SLICE_VALUE,
139 EXPRESSION_INTERFACE_INFO,
140 EXPRESSION_INTERFACE_VALUE,
141 EXPRESSION_INTERFACE_MTABLE,
142 EXPRESSION_STRUCT_FIELD_OFFSET,
143 EXPRESSION_LABEL_ADDR,
144 EXPRESSION_CONDITIONAL,
145 EXPRESSION_COMPOUND,
146 EXPRESSION_BACKEND
149 Expression(Expression_classification, Location);
151 virtual ~Expression();
153 // Make an error expression. This is used when a parse error occurs
154 // to prevent cascading errors.
155 static Expression*
156 make_error(Location);
158 // Make an expression which is really a type. This is used during
159 // parsing.
160 static Expression*
161 make_type(Type*, Location);
163 // Make a unary expression.
164 static Expression*
165 make_unary(Operator, Expression*, Location);
167 // Make a binary expression.
168 static Expression*
169 make_binary(Operator, Expression*, Expression*, Location);
171 // Make a string concatenation expression.
172 static Expression*
173 make_string_concat(Expression_list*);
175 // Make a reference to a constant in an expression.
176 static Expression*
177 make_const_reference(Named_object*, Location);
179 // Make a reference to a variable in an expression.
180 static Expression*
181 make_var_reference(Named_object*, Location);
183 // Make a reference to a variable within an enclosing function.
184 static Expression*
185 make_enclosing_var_reference(Expression*, Named_object*, Location);
187 // Make a reference to a temporary variable. Temporary variables
188 // are always created by a single statement, which is what we use to
189 // refer to them.
190 static Temporary_reference_expression*
191 make_temporary_reference(Temporary_statement*, Location);
193 // Make an expressions which sets a temporary variable and then
194 // evaluates to a reference to that temporary variable. This is
195 // used to set a temporary variable while retaining the order of
196 // evaluation.
197 static Set_and_use_temporary_expression*
198 make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
200 // Make a sink expression--a reference to the blank identifier _.
201 static Expression*
202 make_sink(Location);
204 // Make a reference to a function in an expression. This returns a
205 // pointer to the struct holding the address of the function
206 // followed by any closed-over variables.
207 static Expression*
208 make_func_reference(Named_object*, Expression* closure, Location);
210 // Make a function descriptor, an immutable struct with a single
211 // field that points to the function code. This may only be used
212 // with functions that do not have closures. FN is the function for
213 // which we are making the descriptor.
214 static Func_descriptor_expression*
215 make_func_descriptor(Named_object* fn);
217 // Make a reference to the code of a function. This is used to set
218 // descriptor and closure fields.
219 static Expression*
220 make_func_code_reference(Named_object*, Location);
222 // Make a reference to an unknown name. In a correct program this
223 // will always be lowered to a real const/var/func reference.
224 static Unknown_expression*
225 make_unknown_reference(Named_object*, Location);
227 // Make a constant bool expression.
228 static Expression*
229 make_boolean(bool val, Location);
231 // Make a constant string expression.
232 static Expression*
233 make_string(const std::string&, Location);
235 // Make a constant string expression with a specific string subtype.
236 static Expression*
237 make_string_typed(const std::string&, Type*, Location);
239 // Make an expression that evaluates to some characteristic of an string.
240 // For simplicity, the enum values must match the field indexes in the
241 // underlying struct. This returns an lvalue.
242 enum String_info
244 // The underlying data in the string.
245 STRING_INFO_DATA,
246 // The length of the string.
247 STRING_INFO_LENGTH
250 static Expression*
251 make_string_info(Expression* string, String_info, Location);
253 // Make an expression for a string value.
254 static Expression*
255 make_string_value(Expression* valptr, Expression* len, Location);
257 // Make a character constant expression. TYPE should be NULL for an
258 // abstract type.
259 static Expression*
260 make_character(const mpz_t*, Type*, Location);
262 // Make a constant integer expression from a multi-precision
263 // integer. TYPE should be NULL for an abstract type.
264 static Expression*
265 make_integer_z(const mpz_t*, Type*, Location);
267 // Make a constant integer expression from an unsigned long. TYPE
268 // should be NULL for an abstract type.
269 static Expression*
270 make_integer_ul(unsigned long, Type*, Location);
272 // Make a constant integer expression from a signed long. TYPE
273 // should be NULL for an abstract type.
274 static Expression*
275 make_integer_sl(long, Type*, Location);
277 // Make a constant integer expression from an int64_t. TYPE should
278 // be NULL for an abstract type.
279 static Expression*
280 make_integer_int64(int64_t, Type*, Location);
282 // Make a constant float expression. TYPE should be NULL for an
283 // abstract type.
284 static Expression*
285 make_float(const mpfr_t*, Type*, Location);
287 // Make a constant complex expression. TYPE should be NULL for an
288 // abstract type.
289 static Expression*
290 make_complex(const mpc_t*, Type*, Location);
292 // Make a nil expression.
293 static Expression*
294 make_nil(Location);
296 // Make an iota expression. This is used for the predeclared
297 // constant iota.
298 static Expression*
299 make_iota();
301 // Make a call expression.
302 static Call_expression*
303 make_call(Expression* func, Expression_list* args, bool is_varargs,
304 Location);
306 // Make a reference to a specific result of a call expression which
307 // returns a tuple.
308 static Expression*
309 make_call_result(Call_expression*, unsigned int index);
311 // Make an expression which is a method bound to its first
312 // parameter. METHOD is the method being called, FUNCTION is the
313 // function to call.
314 static Bound_method_expression*
315 make_bound_method(Expression* object, const Method* method,
316 Named_object* function, Location);
318 // Make an index or slice expression. This is a parser expression
319 // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
320 // index rather than a slice. CAP may be NULL, meaning we use the default
321 // capacity of LEFT. At parse time we may not know the type of LEFT.
322 // After parsing this is lowered to an array index, a string index,
323 // or a map index.
324 static Expression*
325 make_index(Expression* left, Expression* start, Expression* end,
326 Expression* cap, Location);
328 // Make an array index expression. END may be NULL, in which case
329 // this is an lvalue. CAP may be NULL, in which case it defaults
330 // to cap(ARRAY).
331 static Expression*
332 make_array_index(Expression* array, Expression* start, Expression* end,
333 Expression* cap, Location);
335 // Make a string index expression. END may be NULL. This is never
336 // an lvalue.
337 static Expression*
338 make_string_index(Expression* string, Expression* start, Expression* end,
339 Location);
341 // Make a map index expression. This is an lvalue.
342 static Map_index_expression*
343 make_map_index(Expression* map, Expression* val, Location);
345 // Make a selector. This is a parser expression which represents
346 // LEFT.NAME. At parse time we may not know the type of the left
347 // hand side.
348 static Expression*
349 make_selector(Expression* left, const std::string& name, Location);
351 // Make a reference to a field in a struct.
352 static Field_reference_expression*
353 make_field_reference(Expression*, unsigned int field_index, Location);
355 // Make a reference to a field of an interface, with an associated
356 // object.
357 static Expression*
358 make_interface_field_reference(Expression*, const std::string&,
359 Location);
361 // Make an allocation expression.
362 static Expression*
363 make_allocation(Type*, Location);
365 // Make a type guard expression.
366 static Expression*
367 make_type_guard(Expression*, Type*, Location);
369 // Make a type cast expression.
370 static Expression*
371 make_cast(Type*, Expression*, Location);
373 // Make an unsafe type cast expression. This is only used when
374 // passing parameter to builtin functions that are part of the Go
375 // runtime.
376 static Expression*
377 make_unsafe_cast(Type*, Expression*, Location);
379 // Make a composite literal. The DEPTH parameter is how far down we
380 // are in a list of composite literals with omitted types. HAS_KEYS
381 // is true if the expression list has keys alternating with values.
382 // ALL_ARE_NAMES is true if all the keys could be struct field
383 // names.
384 static Expression*
385 make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
386 bool all_are_names, Location);
388 // Make a composite literal key.
389 static Expression*
390 make_composite_literal_key(const std::string& name, Location);
392 // Make a struct composite literal.
393 static Expression*
394 make_struct_composite_literal(Type*, Expression_list*, Location);
396 // Make an array composite literal.
397 static Expression*
398 make_array_composite_literal(Type*, Expression_list*, Location);
400 // Make a slice composite literal.
401 static Slice_construction_expression*
402 make_slice_composite_literal(Type*, Expression_list*, Location);
404 // Take an expression and allocate it on the heap.
405 static Expression*
406 make_heap_expression(Expression*, Location);
408 // Make a receive expression. VAL is NULL for a unary receive.
409 static Receive_expression*
410 make_receive(Expression* channel, Location);
412 // Make an expression which evaluates to the address of the type
413 // descriptor for TYPE.
414 static Expression*
415 make_type_descriptor(Type* type, Location);
417 // Make an expression which evaluates to the address of the gc
418 // symbol for TYPE.
419 static Expression*
420 make_gc_symbol(Type* type);
422 // Make an expression that evaluates to the address of a ptrmask
423 // symbol for TYPE. For most types this will be the same as
424 // make_gc_symbol, but for larger types make_gc_symbol will return a
425 // gcprog while this will return a ptrmask.
426 static Expression*
427 make_ptrmask_symbol(Type* type);
429 // Make an expression which evaluates to some characteristic of a
430 // type. These are only used for type descriptors, so there is no
431 // location parameter.
432 enum Type_info
434 // The size of a value of the type.
435 TYPE_INFO_SIZE,
436 // The required alignment of a value of the type.
437 TYPE_INFO_ALIGNMENT,
438 // The required alignment of a value of the type when used as a
439 // field in a struct.
440 TYPE_INFO_FIELD_ALIGNMENT,
441 // The size of the prefix of a value of the type that contains
442 // all the pointers. This is 0 for a type that contains no
443 // pointers. It is always <= TYPE_INFO_SIZE.
444 TYPE_INFO_BACKEND_PTRDATA,
445 // Like TYPE_INFO_BACKEND_PTRDATA, but the ptrdata value that we
446 // want to store in a type descriptor. They are the same for
447 // most types, but can differ for a type that uses a gcprog.
448 TYPE_INFO_DESCRIPTOR_PTRDATA
451 static Expression*
452 make_type_info(Type* type, Type_info);
454 // Make an expression that evaluates to some characteristic of a
455 // slice. For simplicity, the enum values must match the field indexes
456 // in the underlying struct. This returns an lvalue.
457 enum Slice_info
459 // The underlying data of the slice.
460 SLICE_INFO_VALUE_POINTER,
461 // The length of the slice.
462 SLICE_INFO_LENGTH,
463 // The capacity of the slice.
464 SLICE_INFO_CAPACITY
467 static Expression*
468 make_slice_info(Expression* slice, Slice_info, Location);
470 // Make an expression for a slice value.
471 static Expression*
472 make_slice_value(Type*, Expression* valptr, Expression* len, Expression* cap,
473 Location);
475 // Make an expression that evaluates to some characteristic of an
476 // interface. For simplicity, the enum values must match the field indexes
477 // in the underlying struct. This returns an lvalue.
478 enum Interface_info
480 // The type descriptor of an empty interface.
481 INTERFACE_INFO_TYPE_DESCRIPTOR = 0,
482 // The methods of an interface.
483 INTERFACE_INFO_METHODS = 0,
484 // The first argument to pass to an interface method.
485 INTERFACE_INFO_OBJECT
488 static Expression*
489 make_interface_info(Expression* iface, Interface_info, Location);
491 // Make an expression for an interface value.
492 static Expression*
493 make_interface_value(Type*, Expression*, Expression*, Location);
495 // Make an expression that builds a reference to the interface method table
496 // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
497 // reference to the interface method table for the pointer receiver type.
498 static Expression*
499 make_interface_mtable_ref(Interface_type* itype, Type* type,
500 bool is_pointer, Location);
502 // Make an expression which evaluates to the offset of a field in a
503 // struct. This is only used for type descriptors, so there is no
504 // location parameter.
505 static Expression*
506 make_struct_field_offset(Struct_type*, const Struct_field*);
508 // Make an expression which evaluates to the address of an unnamed
509 // label.
510 static Expression*
511 make_label_addr(Label*, Location);
513 // Make a conditional expression.
514 static Expression*
515 make_conditional(Expression*, Expression*, Expression*, Location);
517 // Make a compound expression.
518 static Expression*
519 make_compound(Expression*, Expression*, Location);
521 // Make a backend expression.
522 static Expression*
523 make_backend(Bexpression*, Type*, Location);
525 enum Nil_check_classification
527 // Use the default policy for deciding if this deref needs a check.
528 NIL_CHECK_DEFAULT,
529 // An explicit check is required for this dereference operation.
530 NIL_CHECK_NEEDED,
531 // No check needed for this dereference operation.
532 NIL_CHECK_NOT_NEEDED,
533 // A type error or error construct was encountered when determining
534 // whether this deref needs an explicit check.
535 NIL_CHECK_ERROR_ENCOUNTERED
538 // Make a dereference expression.
539 static Expression*
540 make_dereference(Expression*, Nil_check_classification, Location);
542 // Return the expression classification.
543 Expression_classification
544 classification() const
545 { return this->classification_; }
547 // Return the location of the expression.
548 Location
549 location() const
550 { return this->location_; }
552 // Return whether this is a constant expression.
553 bool
554 is_constant() const
555 { return this->do_is_constant(); }
557 // Return whether this is the zero value of its type.
558 bool
559 is_zero_value() const
560 { return this->do_is_zero_value(); }
562 // Return whether this expression can be used as a static
563 // initializer. This is true for an expression that has only
564 // numbers and pointers to global variables or composite literals
565 // that do not require runtime initialization. It is false if we
566 // must generate code to compute this expression when it is used to
567 // initialize a global variable. This is not a language-level
568 // concept, but an implementation-level one. If this expression is
569 // used to initialize a global variable, this is true if we can pass
570 // an initializer to the backend, false if we must generate code to
571 // initialize the variable. It is always safe for this method to
572 // return false, but the resulting code may be less efficient.
573 bool
574 is_static_initializer() const
575 { return this->do_is_static_initializer(); }
577 // If this is not a numeric constant, return false. If it is one,
578 // return true, and set VAL to hold the value.
579 bool
580 numeric_constant_value(Numeric_constant* val) const
581 { return this->do_numeric_constant_value(val); }
583 // If this is not a constant expression with string type, return
584 // false. If it is one, return true, and set VAL to the value.
585 bool
586 string_constant_value(std::string* val) const
587 { return this->do_string_constant_value(val); }
589 // If this is not a constant expression with boolean type, return
590 // false. If it is one, return true, and set VAL to the value.
591 bool
592 boolean_constant_value(bool* val) const
593 { return this->do_boolean_constant_value(val); }
595 // If this is a const reference expression, return the named
596 // object to which the expression refers, otherwise return NULL.
597 const Named_object*
598 named_constant() const;
600 // This is called if the value of this expression is being
601 // discarded. This issues warnings about computed values being
602 // unused. This returns true if all is well, false if it issued an
603 // error message.
604 bool
605 discarding_value()
606 { return this->do_discarding_value(); }
608 // Return whether this is an error expression.
609 bool
610 is_error_expression() const
611 { return this->classification_ == EXPRESSION_ERROR; }
613 // Return whether this expression really represents a type.
614 bool
615 is_type_expression() const
616 { return this->classification_ == EXPRESSION_TYPE; }
618 // If this is a variable reference, return the Var_expression
619 // structure. Otherwise, return NULL. This is a controlled dynamic
620 // cast.
621 Var_expression*
622 var_expression()
623 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
625 const Var_expression*
626 var_expression() const
627 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
629 // If this is a enclosed_variable reference, return the
630 // Enclosed_var_expression structure. Otherwise, return NULL.
631 // This is a controlled dynamic cast.
632 Enclosed_var_expression*
633 enclosed_var_expression()
634 { return this->convert<Enclosed_var_expression,
635 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
637 const Enclosed_var_expression*
638 enclosed_var_expression() const
639 { return this->convert<const Enclosed_var_expression,
640 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
643 // If this is a reference to a temporary variable, return the
644 // Temporary_reference_expression. Otherwise, return NULL.
645 Temporary_reference_expression*
646 temporary_reference_expression()
648 return this->convert<Temporary_reference_expression,
649 EXPRESSION_TEMPORARY_REFERENCE>();
652 // If this is a set-and-use-temporary, return the
653 // Set_and_use_temporary_expression. Otherwise, return NULL.
654 Set_and_use_temporary_expression*
655 set_and_use_temporary_expression()
657 return this->convert<Set_and_use_temporary_expression,
658 EXPRESSION_SET_AND_USE_TEMPORARY>();
661 // Return whether this is a sink expression.
662 bool
663 is_sink_expression() const
664 { return this->classification_ == EXPRESSION_SINK; }
666 // If this is a string expression, return the String_expression
667 // structure. Otherwise, return NULL.
668 String_expression*
669 string_expression()
670 { return this->convert<String_expression, EXPRESSION_STRING>(); }
672 // If this is a conversion expression, return the Type_conversion_expression
673 // structure. Otherwise, return NULL.
674 Type_conversion_expression*
675 conversion_expression()
676 { return this->convert<Type_conversion_expression, EXPRESSION_CONVERSION>(); }
678 // If this is an unsafe conversion expression, return the
679 // Unsafe_type_conversion_expression structure. Otherwise, return NULL.
680 Unsafe_type_conversion_expression*
681 unsafe_conversion_expression()
683 return this->convert<Unsafe_type_conversion_expression,
684 EXPRESSION_UNSAFE_CONVERSION>();
687 // Return whether this is the expression nil.
688 bool
689 is_nil_expression() const
690 { return this->classification_ == EXPRESSION_NIL; }
692 // If this is an indirection through a pointer, return the
693 // expression being pointed through. Otherwise return this.
694 Expression*
695 deref();
697 // If this is a unary expression, return the Unary_expression
698 // structure. Otherwise return NULL.
699 Unary_expression*
700 unary_expression()
701 { return this->convert<Unary_expression, EXPRESSION_UNARY>(); }
703 // If this is a binary expression, return the Binary_expression
704 // structure. Otherwise return NULL.
705 Binary_expression*
706 binary_expression()
707 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
709 // If this is a string concatenation expression, return the
710 // String_concat_expression structure. Otherwise, return NULL.
711 String_concat_expression*
712 string_concat_expression()
714 return this->convert<String_concat_expression, EXPRESSION_STRING_CONCAT>();
717 // If this is a call expression, return the Call_expression
718 // structure. Otherwise, return NULL. This is a controlled dynamic
719 // cast.
720 Call_expression*
721 call_expression()
722 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
724 // If this is a call_result expression, return the Call_result_expression
725 // structure. Otherwise, return NULL. This is a controlled dynamic
726 // cast.
727 Call_result_expression*
728 call_result_expression()
729 { return this->convert<Call_result_expression, EXPRESSION_CALL_RESULT>(); }
731 // If this is an expression which refers to a function, return the
732 // Func_expression structure. Otherwise, return NULL.
733 Func_expression*
734 func_expression()
735 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
737 const Func_expression*
738 func_expression() const
739 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
741 // If this is an expression which refers to an unknown name, return
742 // the Unknown_expression structure. Otherwise, return NULL.
743 Unknown_expression*
744 unknown_expression()
745 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
747 const Unknown_expression*
748 unknown_expression() const
750 return this->convert<const Unknown_expression,
751 EXPRESSION_UNKNOWN_REFERENCE>();
754 // If this is an index expression, return the Index_expression
755 // structure. Otherwise, return NULL.
756 Index_expression*
757 index_expression()
758 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
760 // If this is an expression which refers to indexing in a array,
761 // return the Array_index_expression structure. Otherwise, return
762 // NULL.
763 Array_index_expression*
764 array_index_expression()
765 { return this->convert<Array_index_expression, EXPRESSION_ARRAY_INDEX>(); }
767 // If this is an expression which refers to indexing in a string,
768 // return the String_index_expression structure. Otherwise, return
769 // NULL.
770 String_index_expression*
771 string_index_expression()
772 { return this->convert<String_index_expression, EXPRESSION_STRING_INDEX>(); }
774 // If this is an expression which refers to indexing in a map,
775 // return the Map_index_expression structure. Otherwise, return
776 // NULL.
777 Map_index_expression*
778 map_index_expression()
779 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
781 // If this is a bound method expression, return the
782 // Bound_method_expression structure. Otherwise, return NULL.
783 Bound_method_expression*
784 bound_method_expression()
785 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
787 // If this is a reference to a field in a struct, return the
788 // Field_reference_expression structure. Otherwise, return NULL.
789 Field_reference_expression*
790 field_reference_expression()
792 return this->convert<Field_reference_expression,
793 EXPRESSION_FIELD_REFERENCE>();
796 // If this is a reference to a field in an interface, return the
797 // Interface_field_reference_expression structure. Otherwise,
798 // return NULL.
799 Interface_field_reference_expression*
800 interface_field_reference_expression()
802 return this->convert<Interface_field_reference_expression,
803 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
806 // If this is an allocation expression, return the Allocation_expression
807 // structure. Otherwise, return NULL.
808 Allocation_expression*
809 allocation_expression()
810 { return this->convert<Allocation_expression, EXPRESSION_ALLOCATION>(); }
812 // If this is a general composite literal, return the
813 // Composite_literal_expression structure. Otherwise, return NULL.
814 Composite_literal_expression*
815 complit()
817 return this->convert<Composite_literal_expression,
818 EXPRESSION_COMPOSITE_LITERAL>();
821 // If this is a struct composite literal, return the
822 // Struct_construction_expression structure. Otherwise, return NULL.
823 Struct_construction_expression*
824 struct_literal()
826 return this->convert<Struct_construction_expression,
827 EXPRESSION_STRUCT_CONSTRUCTION>();
830 // If this is a array composite literal, return the
831 // Array_construction_expression structure. Otherwise, return NULL.
832 Fixed_array_construction_expression*
833 array_literal()
835 return this->convert<Fixed_array_construction_expression,
836 EXPRESSION_FIXED_ARRAY_CONSTRUCTION>();
839 // If this is a slice composite literal, return the
840 // Slice_construction_expression structure. Otherwise, return NULL.
841 Slice_construction_expression*
842 slice_literal()
844 return this->convert<Slice_construction_expression,
845 EXPRESSION_SLICE_CONSTRUCTION>();
848 // If this is a map composite literal, return the
849 // Map_construction_expression structure. Otherwise, return NULL.
850 Map_construction_expression*
851 map_literal()
853 return this->convert<Map_construction_expression,
854 EXPRESSION_MAP_CONSTRUCTION>();
857 // If this is a type guard expression, return the
858 // Type_guard_expression structure. Otherwise, return NULL.
859 Type_guard_expression*
860 type_guard_expression()
861 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
863 // If this is a heap expression, returhn the Heap_expression structure.
864 // Otherwise, return NULL.
865 Heap_expression*
866 heap_expression()
867 { return this->convert<Heap_expression, EXPRESSION_HEAP>(); }
869 // If this is a receive expression, return the Receive_expression
870 // structure. Otherwise, return NULL.
871 Receive_expression*
872 receive_expression()
873 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
875 // If this is a slice value expression, return the Slice_valiue_expression
876 // structure. Otherwise, return NULL.
877 Slice_value_expression*
878 slice_value_expression()
879 { return this->convert<Slice_value_expression, EXPRESSION_SLICE_VALUE>(); }
881 // If this is a conditional expression, return the Conditional_expression
882 // structure. Otherwise, return NULL.
883 Conditional_expression*
884 conditional_expression()
885 { return this->convert<Conditional_expression, EXPRESSION_CONDITIONAL>(); }
887 // If this is a compound expression, return the Compound_expression structure.
888 // Otherwise, return NULL.
889 Compound_expression*
890 compound_expression()
891 { return this->convert<Compound_expression, EXPRESSION_COMPOUND>(); }
893 // Return true if this is a composite literal.
894 bool
895 is_composite_literal() const;
897 // Return true if this is a composite literal which is not constant.
898 bool
899 is_nonconstant_composite_literal() const;
901 // Return true if this is a variable or temporary variable.
902 bool
903 is_variable() const;
905 // Return true if this is a reference to a local variable.
906 bool
907 is_local_variable() const;
909 // Return true if two expressions refer to the same variable or
910 // struct field.
911 static bool
912 is_same_variable(Expression*, Expression*);
914 // Make the builtin function descriptor type, so that it can be
915 // converted.
916 static void
917 make_func_descriptor_type();
919 // Traverse an expression.
920 static int
921 traverse(Expression**, Traverse*);
923 // Traverse subexpressions of this expression.
925 traverse_subexpressions(Traverse*);
927 // Lower an expression. This is called immediately after parsing.
928 // FUNCTION is the function we are in; it will be NULL for an
929 // expression initializing a global variable. INSERTER may be used
930 // to insert statements before the statement or initializer
931 // containing this expression; it is normally used to create
932 // temporary variables. IOTA_VALUE is the value that we should give
933 // to any iota expressions. This function must resolve expressions
934 // which could not be fully parsed into their final form. It
935 // returns the same Expression or a new one.
936 Expression*
937 lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
938 int iota_value)
939 { return this->do_lower(gogo, function, inserter, iota_value); }
941 // Flatten an expression. This is called after order_evaluation.
942 // FUNCTION is the function we are in; it will be NULL for an
943 // expression initializing a global variable. INSERTER may be used
944 // to insert statements before the statement or initializer
945 // containing this expression; it is normally used to create
946 // temporary variables. This function must resolve expressions
947 // which could not be fully parsed into their final form. It
948 // returns the same Expression or a new one.
949 Expression*
950 flatten(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
951 { return this->do_flatten(gogo, function, inserter); }
953 // Make implicit type conversions explicit.
954 void
955 add_conversions()
956 { this->do_add_conversions(); }
958 // Determine the real type of an expression with abstract integer,
959 // floating point, or complex type. TYPE_CONTEXT describes the
960 // expected type.
961 void
962 determine_type(const Type_context*);
964 // Check types in an expression.
965 void
966 check_types(Gogo* gogo)
967 { this->do_check_types(gogo); }
969 // Determine the type when there is no context.
970 void
971 determine_type_no_context();
973 // Return the current type of the expression. This may be changed
974 // by determine_type.
975 Type*
976 type()
977 { return this->do_type(); }
979 // Return a copy of an expression.
980 Expression*
981 copy()
982 { return this->do_copy(); }
984 // Return the cost of this statement for inlining purposes.
986 inlining_cost() const
987 { return this->do_inlining_cost(); }
989 // Return whether the expression is addressable--something which may
990 // be used as the operand of the unary & operator.
991 bool
992 is_addressable() const
993 { return this->do_is_addressable(); }
995 // Note that we are taking the address of this expression. ESCAPES
996 // is true if this address escapes the current function.
997 void
998 address_taken(bool escapes)
999 { this->do_address_taken(escapes); }
1001 // Note that a nil check must be issued for this expression.
1002 void
1003 issue_nil_check()
1004 { this->do_issue_nil_check(); }
1006 // Return whether this expression must be evaluated in order
1007 // according to the order of evaluation rules. This is basically
1008 // true of all expressions with side-effects.
1009 bool
1010 must_eval_in_order() const
1011 { return this->do_must_eval_in_order(); }
1013 // Return whether subexpressions of this expression must be
1014 // evaluated in order. This is true of index expressions and
1015 // pointer indirections. This sets *SKIP to the number of
1016 // subexpressions to skip during traversing, as index expressions
1017 // only requiring moving the index, not the array.
1018 bool
1019 must_eval_subexpressions_in_order(int* skip) const
1021 *skip = 0;
1022 return this->do_must_eval_subexpressions_in_order(skip);
1025 // Return the backend representation for this expression.
1026 Bexpression*
1027 get_backend(Translate_context*);
1029 // Return an expression handling any conversions which must be done during
1030 // assignment.
1031 static Expression*
1032 convert_for_assignment(Gogo*, Type* lhs_type, Expression* rhs,
1033 Location location);
1035 // Return an expression converting a value of one interface type to another
1036 // interface type. If FOR_TYPE_GUARD is true this is for a type
1037 // assertion.
1038 static Expression*
1039 convert_interface_to_interface(Type* lhs_type,
1040 Expression* rhs, bool for_type_guard,
1041 Location);
1043 // Return an expression for a conversion from a non-interface type to an
1044 // interface type. If ON_STACK is true, it can allocate the storage on
1045 // stack.
1046 static Expression*
1047 convert_type_to_interface(Type* lhs_type, Expression* rhs,
1048 bool on_stack, Location);
1050 // Return a backend expression implementing the comparison LEFT OP RIGHT.
1051 // TYPE is the type of both sides.
1052 static Bexpression*
1053 comparison(Translate_context*, Type* result_type, Operator op,
1054 Expression* left, Expression* right, Location);
1056 // Return the backend expression for the numeric constant VAL.
1057 static Bexpression*
1058 backend_numeric_constant_expression(Translate_context*,
1059 Numeric_constant* val);
1061 // Export the expression.
1062 void
1063 export_expression(Export_function_body* efb) const
1064 { this->do_export(efb); }
1066 // Import an expression. The location should be used for the
1067 // returned expression. Errors should be reported using the
1068 // Import's location method.
1069 static Expression*
1070 import_expression(Import_expression*, Location);
1072 // Insert bounds checks for an index expression.
1073 static void
1074 check_bounds(Expression* val, Operator, Expression* bound, Runtime::Function,
1075 Runtime::Function, Runtime::Function, Runtime::Function,
1076 Statement_inserter*, Location);
1078 // Return an expression for constructing a direct interface type from a
1079 // pointer.
1080 static Expression*
1081 pack_direct_iface(Type*, Expression*, Location);
1083 // Return an expression of the underlying pointer for a direct interface
1084 // type (the opposite of pack_direct_iface).
1085 static Expression*
1086 unpack_direct_iface(Expression*, Location);
1088 // Return an expression representing the type descriptor field of an
1089 // interface.
1090 static Expression*
1091 get_interface_type_descriptor(Expression*);
1093 // Look through the expression of a Slice_value_expression's valmem to
1094 // find an call to makeslice.
1095 static std::pair<Call_expression*, Temporary_statement*>
1096 find_makeslice_call(Expression*);
1098 // Dump an expression to a dump constext.
1099 void
1100 dump_expression(Ast_dump_context*) const;
1102 protected:
1103 // May be implemented by child class: traverse the expressions.
1104 virtual int
1105 do_traverse(Traverse*);
1107 // Return a lowered expression.
1108 virtual Expression*
1109 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1110 { return this; }
1112 // Return a flattened expression.
1113 virtual Expression*
1114 do_flatten(Gogo*, Named_object*, Statement_inserter*)
1115 { return this; }
1117 // Make implicit type conversions explicit.
1118 virtual void
1119 do_add_conversions()
1122 // Return whether this is a constant expression.
1123 virtual bool
1124 do_is_constant() const
1125 { return false; }
1127 // Return whether this is the zero value of its type.
1128 virtual bool
1129 do_is_zero_value() const
1130 { return false; }
1132 // Return whether this expression can be used as a constant
1133 // initializer.
1134 virtual bool
1135 do_is_static_initializer() const
1136 { return false; }
1138 // Return whether this is a constant expression of numeric type, and
1139 // set the Numeric_constant to the value.
1140 virtual bool
1141 do_numeric_constant_value(Numeric_constant*) const
1142 { return false; }
1144 // Return whether this is a constant expression of string type, and
1145 // set VAL to the value.
1146 virtual bool
1147 do_string_constant_value(std::string*) const
1148 { return false; }
1150 // Return whether this is a constant expression of boolean type, and
1151 // set VAL to the value.
1152 virtual bool
1153 do_boolean_constant_value(bool*) const
1154 { return false; }
1156 // Called by the parser if the value is being discarded.
1157 virtual bool
1158 do_discarding_value();
1160 // Child class holds type.
1161 virtual Type*
1162 do_type() = 0;
1164 // Child class implements determining type information.
1165 virtual void
1166 do_determine_type(const Type_context*) = 0;
1168 // Child class implements type checking if needed.
1169 virtual void
1170 do_check_types(Gogo*)
1173 // Child class implements copying.
1174 virtual Expression*
1175 do_copy() = 0;
1177 // Child class implements determining the cost of this statement for
1178 // inlining. The default cost is high, so we only need to define
1179 // this method for expressions that can be inlined.
1180 virtual int
1181 do_inlining_cost() const
1182 { return 0x100000; }
1184 // Child class implements whether the expression is addressable.
1185 virtual bool
1186 do_is_addressable() const
1187 { return false; }
1189 // Child class implements taking the address of an expression.
1190 virtual void
1191 do_address_taken(bool)
1194 // Child class implements issuing a nil check if the address is taken.
1195 virtual void
1196 do_issue_nil_check()
1199 // Child class implements whether this expression must be evaluated
1200 // in order.
1201 virtual bool
1202 do_must_eval_in_order() const
1203 { return false; }
1205 // Child class implements whether this expressions requires that
1206 // subexpressions be evaluated in order. The child implementation
1207 // may set *SKIP if it should be non-zero.
1208 virtual bool
1209 do_must_eval_subexpressions_in_order(int* /* skip */) const
1210 { return false; }
1212 // Child class implements conversion to backend representation.
1213 virtual Bexpression*
1214 do_get_backend(Translate_context*) = 0;
1216 // Child class implements export.
1217 virtual void
1218 do_export(Export_function_body*) const;
1220 // For children to call to give an error for an unused value.
1221 void
1222 unused_value_error();
1224 // For children to call when they detect that they are in error.
1225 void
1226 set_is_error();
1228 // For children to call to report an error conveniently.
1229 void
1230 report_error(const char*);
1232 // Write a name to export data.
1233 static void
1234 export_name(Export_function_body* efb, const Named_object*);
1236 // Child class implements dumping to a dump context.
1237 virtual void
1238 do_dump_expression(Ast_dump_context*) const = 0;
1240 // Varargs lowering creates a slice object (unnamed compiler temp)
1241 // to contain the variable length collection of values. The enum
1242 // below tells the lowering routine whether it can mark that temp
1243 // as non-escaping or not. For general varargs calls it is not always
1244 // safe to stack-allocated the storage, but for specific cases (ex:
1245 // call to append()) it is legal.
1246 enum Slice_storage_escape_disp
1248 SLICE_STORAGE_MAY_ESCAPE,
1249 SLICE_STORAGE_DOES_NOT_ESCAPE
1252 private:
1253 // Convert to the desired statement classification, or return NULL.
1254 // This is a controlled dynamic cast.
1255 template<typename Expression_class,
1256 Expression_classification expr_classification>
1257 Expression_class*
1258 convert()
1260 return (this->classification_ == expr_classification
1261 ? static_cast<Expression_class*>(this)
1262 : NULL);
1265 template<typename Expression_class,
1266 Expression_classification expr_classification>
1267 const Expression_class*
1268 convert() const
1270 return (this->classification_ == expr_classification
1271 ? static_cast<const Expression_class*>(this)
1272 : NULL);
1275 static Expression*
1276 convert_interface_to_type(Gogo*, Type*, Expression*, Location);
1278 static Expression*
1279 import_identifier(Import_function_body*, Location);
1281 static Expression*
1282 import_expression_without_suffix(Import_expression*, Location);
1284 // The expression classification.
1285 Expression_classification classification_;
1286 // The location in the input file.
1287 Location location_;
1290 // A list of Expressions.
1292 class Expression_list
1294 public:
1295 Expression_list()
1296 : entries_()
1299 // Return whether the list is empty.
1300 bool
1301 empty() const
1302 { return this->entries_.empty(); }
1304 // Return the number of entries in the list.
1305 size_t
1306 size() const
1307 { return this->entries_.size(); }
1309 // Add an entry to the end of the list.
1310 void
1311 push_back(Expression* expr)
1312 { this->entries_.push_back(expr); }
1314 void
1315 append(Expression_list* add)
1316 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
1318 // Reserve space in the list.
1319 void
1320 reserve(size_t size)
1321 { this->entries_.reserve(size); }
1323 // Traverse the expressions in the list.
1325 traverse(Traverse*);
1327 // Copy the list.
1328 Expression_list*
1329 copy();
1331 // Return true if the list contains an error expression.
1332 bool
1333 contains_error() const;
1335 // Retrieve an element by index.
1336 Expression*&
1337 at(size_t i)
1338 { return this->entries_.at(i); }
1340 // Return the first and last elements.
1341 Expression*&
1342 front()
1343 { return this->entries_.front(); }
1345 Expression*
1346 front() const
1347 { return this->entries_.front(); }
1349 Expression*&
1350 back()
1351 { return this->entries_.back(); }
1353 Expression*
1354 back() const
1355 { return this->entries_.back(); }
1357 // Iterators.
1359 typedef std::vector<Expression*>::iterator iterator;
1360 typedef std::vector<Expression*>::const_iterator const_iterator;
1362 iterator
1363 begin()
1364 { return this->entries_.begin(); }
1366 const_iterator
1367 begin() const
1368 { return this->entries_.begin(); }
1370 iterator
1371 end()
1372 { return this->entries_.end(); }
1374 const_iterator
1375 end() const
1376 { return this->entries_.end(); }
1378 // Erase an entry.
1379 void
1380 erase(iterator p)
1381 { this->entries_.erase(p); }
1383 private:
1384 std::vector<Expression*> entries_;
1387 // An abstract base class for an expression which is only used by the
1388 // parser, and is lowered in the lowering pass.
1390 class Parser_expression : public Expression
1392 public:
1393 Parser_expression(Expression_classification classification,
1394 Location location)
1395 : Expression(classification, location)
1398 protected:
1399 virtual Expression*
1400 do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
1402 Type*
1403 do_type();
1405 void
1406 do_determine_type(const Type_context*)
1407 { go_unreachable(); }
1409 void
1410 do_check_types(Gogo*)
1411 { go_unreachable(); }
1413 Bexpression*
1414 do_get_backend(Translate_context*)
1415 { go_unreachable(); }
1418 // An expression which is simply a variable.
1420 class Var_expression : public Expression
1422 public:
1423 Var_expression(Named_object* variable, Location location)
1424 : Expression(EXPRESSION_VAR_REFERENCE, location),
1425 variable_(variable)
1428 // Return the variable.
1429 Named_object*
1430 named_object() const
1431 { return this->variable_; }
1433 protected:
1434 Expression*
1435 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1437 Type*
1438 do_type();
1440 void
1441 do_determine_type(const Type_context*);
1443 Expression*
1444 do_copy()
1445 { return this; }
1448 do_inlining_cost() const
1449 { return 1; }
1451 void
1452 do_export(Export_function_body*) const;
1454 bool
1455 do_is_addressable() const
1456 { return true; }
1458 void
1459 do_address_taken(bool);
1461 Bexpression*
1462 do_get_backend(Translate_context*);
1464 void
1465 do_dump_expression(Ast_dump_context*) const;
1467 private:
1468 // The variable we are referencing.
1469 Named_object* variable_;
1472 // A reference to a variable within an enclosing function.
1474 class Enclosed_var_expression : public Expression
1476 public:
1477 Enclosed_var_expression(Expression* reference, Named_object* variable,
1478 Location location)
1479 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE, location),
1480 reference_(reference), variable_(variable)
1483 // The reference to the enclosed variable. This will be an indirection of the
1484 // the field stored within closure variable.
1485 Expression*
1486 reference() const
1487 { return this->reference_; }
1489 // The variable being enclosed and referenced.
1490 Named_object*
1491 variable() const
1492 { return this->variable_; }
1494 protected:
1496 do_traverse(Traverse*);
1498 Expression*
1499 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1501 Expression*
1502 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1504 Type*
1505 do_type()
1506 { return this->reference_->type(); }
1508 void
1509 do_determine_type(const Type_context* context)
1510 { return this->reference_->determine_type(context); }
1512 Expression*
1513 do_copy()
1514 { return this; }
1516 bool
1517 do_is_addressable() const
1518 { return this->reference_->is_addressable(); }
1520 void
1521 do_address_taken(bool escapes);
1523 Bexpression*
1524 do_get_backend(Translate_context* context)
1525 { return this->reference_->get_backend(context); }
1527 void
1528 do_dump_expression(Ast_dump_context*) const;
1530 private:
1531 // The reference to the enclosed variable.
1532 Expression* reference_;
1533 // The variable being enclosed.
1534 Named_object* variable_;
1537 // A reference to a temporary variable.
1539 class Temporary_reference_expression : public Expression
1541 public:
1542 Temporary_reference_expression(Temporary_statement* statement,
1543 Location location)
1544 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1545 statement_(statement), is_lvalue_(false)
1548 // The temporary that this expression refers to.
1549 Temporary_statement*
1550 statement() const
1551 { return this->statement_; }
1553 // Indicate that this reference appears on the left hand side of an
1554 // assignment statement.
1555 void
1556 set_is_lvalue()
1557 { this->is_lvalue_ = true; }
1559 static Expression*
1560 do_import(Import_function_body*, Location);
1562 protected:
1563 Type*
1564 do_type();
1566 void
1567 do_determine_type(const Type_context*)
1570 Expression*
1571 do_copy()
1572 { return make_temporary_reference(this->statement_, this->location()); }
1575 do_inlining_cost() const
1576 { return 1; }
1578 void
1579 do_export(Export_function_body*) const;
1581 bool
1582 do_is_addressable() const
1583 { return true; }
1585 void
1586 do_address_taken(bool);
1588 Bexpression*
1589 do_get_backend(Translate_context*);
1591 void
1592 do_dump_expression(Ast_dump_context*) const;
1594 private:
1595 // The statement where the temporary variable is defined.
1596 Temporary_statement* statement_;
1597 // Whether this reference appears on the left hand side of an
1598 // assignment statement.
1599 bool is_lvalue_;
1602 // Set and use a temporary variable.
1604 class Set_and_use_temporary_expression : public Expression
1606 public:
1607 Set_and_use_temporary_expression(Temporary_statement* statement,
1608 Expression* expr, Location location)
1609 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1610 statement_(statement), expr_(expr)
1613 // Return the temporary.
1614 Temporary_statement*
1615 temporary() const
1616 { return this->statement_; }
1618 // Return the expression.
1619 Expression*
1620 expression() const
1621 { return this->expr_; }
1623 protected:
1625 do_traverse(Traverse* traverse)
1626 { return Expression::traverse(&this->expr_, traverse); }
1628 Type*
1629 do_type();
1631 void
1632 do_determine_type(const Type_context*);
1634 Expression*
1635 do_copy()
1637 return make_set_and_use_temporary(this->statement_, this->expr_,
1638 this->location());
1641 bool
1642 do_must_eval_in_order() const
1643 { return true; }
1645 bool
1646 do_is_addressable() const
1647 { return true; }
1649 void
1650 do_address_taken(bool);
1652 Bexpression*
1653 do_get_backend(Translate_context*);
1655 void
1656 do_dump_expression(Ast_dump_context*) const;
1658 private:
1659 // The statement where the temporary variable is defined.
1660 Temporary_statement* statement_;
1661 // The expression to assign to the temporary.
1662 Expression* expr_;
1665 // A string expression.
1667 class String_expression : public Expression
1669 public:
1670 String_expression(const std::string& val, Type* type, Location location)
1671 : Expression(EXPRESSION_STRING, location),
1672 val_(val), type_(type)
1675 const std::string&
1676 val() const
1677 { return this->val_; }
1679 static Expression*
1680 do_import(Import_expression*, Location);
1682 protected:
1684 do_traverse(Traverse*);
1686 bool
1687 do_is_constant() const
1688 { return true; }
1690 bool
1691 do_is_zero_value() const
1692 { return this->val_ == ""; }
1694 bool
1695 do_is_static_initializer() const
1696 { return true; }
1698 bool
1699 do_string_constant_value(std::string* val) const
1701 *val = this->val_;
1702 return true;
1705 Type*
1706 do_type();
1708 void
1709 do_determine_type(const Type_context*);
1711 Expression*
1712 do_copy()
1713 { return this; }
1715 Bexpression*
1716 do_get_backend(Translate_context*);
1718 // Write string literal to a string dump.
1719 static void
1720 export_string(String_dump* exp, const String_expression* str);
1722 // Set the inlining cost a bit high since inlining may cause
1723 // duplicated string literals.
1725 do_inlining_cost() const
1726 { return 5; }
1728 void
1729 do_export(Export_function_body*) const;
1731 void
1732 do_dump_expression(Ast_dump_context*) const;
1734 private:
1735 // The string value. This is immutable.
1736 const std::string val_;
1737 // The type as determined by context.
1738 Type* type_;
1741 // A type conversion expression.
1743 class Type_conversion_expression : public Expression
1745 public:
1746 Type_conversion_expression(Type* type, Expression* expr,
1747 Location location)
1748 : Expression(EXPRESSION_CONVERSION, location),
1749 type_(type), expr_(expr), may_convert_function_types_(false),
1750 no_copy_(false), no_escape_(false)
1753 // Return the type to which we are converting.
1754 Type*
1755 type() const
1756 { return this->type_; }
1758 // Return the expression which we are converting.
1759 Expression*
1760 expr() const
1761 { return this->expr_; }
1763 // Permit converting from one function type to another. This is
1764 // used internally for method expressions.
1765 void
1766 set_may_convert_function_types()
1768 this->may_convert_function_types_ = true;
1771 // Mark string([]byte) conversion to reuse the backing store
1772 // without copying.
1773 void
1774 set_no_copy(bool b)
1775 { this->no_copy_ = b; };
1777 // Import a type conversion expression.
1778 static Expression*
1779 do_import(Import_expression*, Location);
1781 protected:
1783 do_traverse(Traverse* traverse);
1785 Expression*
1786 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1788 Expression*
1789 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1791 bool
1792 do_is_constant() const;
1794 bool
1795 do_is_zero_value() const;
1797 bool
1798 do_is_static_initializer() const;
1800 bool
1801 do_numeric_constant_value(Numeric_constant*) const;
1803 bool
1804 do_string_constant_value(std::string*) const;
1806 bool
1807 do_boolean_constant_value(bool*) const;
1809 Type*
1810 do_type()
1811 { return this->type_; }
1813 void
1814 do_determine_type(const Type_context*);
1816 void
1817 do_check_types(Gogo*);
1819 Expression*
1820 do_copy();
1822 Bexpression*
1823 do_get_backend(Translate_context* context);
1826 do_inlining_cost() const;
1828 void
1829 do_export(Export_function_body*) const;
1831 void
1832 do_dump_expression(Ast_dump_context*) const;
1834 private:
1835 // The type to convert to.
1836 Type* type_;
1837 // The expression to convert.
1838 Expression* expr_;
1839 // True if this is permitted to convert function types. This is
1840 // used internally for method expressions.
1841 bool may_convert_function_types_;
1842 // True if a string([]byte) conversion can reuse the backing store
1843 // without copying. Only used in string([]byte) conversion.
1844 bool no_copy_;
1845 // True if a conversion does not escape. Used in type-to-interface
1846 // conversions and slice-to/from-string conversions.
1847 bool no_escape_;
1850 // An unsafe type conversion, used to pass values to builtin functions.
1852 class Unsafe_type_conversion_expression : public Expression
1854 public:
1855 Unsafe_type_conversion_expression(Type* type, Expression* expr,
1856 Location location)
1857 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
1858 type_(type), expr_(expr)
1861 Expression*
1862 expr() const
1863 { return this->expr_; }
1865 protected:
1867 do_traverse(Traverse* traverse);
1869 bool
1870 do_is_zero_value() const
1871 { return this->expr_->is_zero_value(); }
1873 bool
1874 do_is_static_initializer() const;
1876 Type*
1877 do_type()
1878 { return this->type_; }
1880 void
1881 do_determine_type(const Type_context*)
1882 { this->expr_->determine_type_no_context(); }
1884 Expression*
1885 do_copy();
1887 Bexpression*
1888 do_get_backend(Translate_context*);
1890 void
1891 do_dump_expression(Ast_dump_context*) const;
1893 private:
1894 // The type to convert to.
1895 Type* type_;
1896 // The expression to convert.
1897 Expression* expr_;
1900 // A Unary expression.
1902 class Unary_expression : public Expression
1904 public:
1905 Unary_expression(Operator op, Expression* expr, Location location)
1906 : Expression(EXPRESSION_UNARY, location),
1907 op_(op), escapes_(true), create_temp_(false), is_gc_root_(false),
1908 is_slice_init_(false), expr_(expr),
1909 issue_nil_check_(NIL_CHECK_DEFAULT)
1912 // Return the operator.
1913 Operator
1914 op() const
1915 { return this->op_; }
1917 // Return the operand.
1918 Expression*
1919 operand() const
1920 { return this->expr_; }
1922 // Record that an address expression does not escape.
1923 void
1924 set_does_not_escape()
1926 go_assert(this->op_ == OPERATOR_AND);
1927 this->escapes_ = false;
1930 // Record that this is an address expression which should create a
1931 // temporary variable if necessary. This is used for method calls.
1932 void
1933 set_create_temp()
1935 go_assert(this->op_ == OPERATOR_AND);
1936 this->create_temp_ = true;
1939 // Record that this is an address expression of a GC root, which is a
1940 // mutable composite literal. This used for registering GC variables.
1941 void
1942 set_is_gc_root()
1944 go_assert(this->op_ == OPERATOR_AND);
1945 this->is_gc_root_ = true;
1948 // Record that this is an address expression of a slice value initializer,
1949 // which is mutable if the values are not copied to the heap.
1950 void
1951 set_is_slice_init()
1953 go_assert(this->op_ == OPERATOR_AND);
1954 this->is_slice_init_ = true;
1957 // Call the address_taken method on the operand if necessary.
1958 void
1959 check_operand_address_taken(Gogo*);
1961 // Apply unary opcode OP to UNC, setting NC. Return true if this
1962 // could be done, false if not. On overflow, issues an error and
1963 // sets *ISSUED_ERROR.
1964 static bool
1965 eval_constant(Operator op, const Numeric_constant* unc,
1966 Location, Numeric_constant* nc, bool *issued_error);
1968 static Expression*
1969 do_import(Import_expression*, Location);
1971 // Declare that this deref does or does not require an explicit nil check.
1972 void
1973 set_requires_nil_check(bool needed)
1975 go_assert(this->op_ == OPERATOR_MULT);
1976 if (needed)
1977 this->issue_nil_check_ = NIL_CHECK_NEEDED;
1978 else
1979 this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED;
1982 protected:
1984 do_traverse(Traverse* traverse)
1985 { return Expression::traverse(&this->expr_, traverse); }
1987 Expression*
1988 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1990 Expression*
1991 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1993 bool
1994 do_is_constant() const;
1996 bool
1997 do_is_static_initializer() const;
1999 bool
2000 do_numeric_constant_value(Numeric_constant*) const;
2002 bool
2003 do_boolean_constant_value(bool*) const;
2005 Type*
2006 do_type();
2008 void
2009 do_determine_type(const Type_context*);
2011 void
2012 do_check_types(Gogo*);
2014 Expression*
2015 do_copy()
2017 return Expression::make_unary(this->op_, this->expr_->copy(),
2018 this->location());
2021 bool
2022 do_must_eval_subexpressions_in_order(int*) const
2023 { return this->op_ == OPERATOR_MULT; }
2025 bool
2026 do_is_addressable() const
2027 { return this->op_ == OPERATOR_MULT; }
2029 Bexpression*
2030 do_get_backend(Translate_context*);
2033 do_inlining_cost() const
2034 { return 1; }
2036 void
2037 do_export(Export_function_body*) const;
2039 void
2040 do_dump_expression(Ast_dump_context*) const;
2042 void
2043 do_issue_nil_check()
2045 if (this->op_ == OPERATOR_MULT)
2046 this->set_requires_nil_check(true);
2049 private:
2050 static bool
2051 base_is_static_initializer(Expression*);
2053 // Return a determination as to whether this dereference expression
2054 // requires a nil check.
2055 Nil_check_classification
2056 requires_nil_check(Gogo*);
2058 // The unary operator to apply.
2059 Operator op_;
2060 // Normally true. False if this is an address expression which does
2061 // not escape the current function.
2062 bool escapes_;
2063 // True if this is an address expression which should create a
2064 // temporary variable if necessary.
2065 bool create_temp_;
2066 // True if this is an address expression for a GC root. A GC root is a
2067 // special struct composite literal that is mutable when addressed, meaning
2068 // it cannot be represented as an immutable_struct in the backend.
2069 bool is_gc_root_;
2070 // True if this is an address expression for a slice value with an immutable
2071 // initializer. The initializer for a slice's value pointer has an array
2072 // type, meaning it cannot be represented as an immutable_struct in the
2073 // backend.
2074 bool is_slice_init_;
2075 // The operand.
2076 Expression* expr_;
2077 // Whether or not to issue a nil check for this expression if its address
2078 // is being taken.
2079 Nil_check_classification issue_nil_check_;
2082 // A binary expression.
2084 class Binary_expression : public Expression
2086 public:
2087 Binary_expression(Operator op, Expression* left, Expression* right,
2088 Location location)
2089 : Expression(EXPRESSION_BINARY, location),
2090 op_(op), left_(left), right_(right), type_(NULL)
2093 // Return the operator.
2094 Operator
2095 op()
2096 { return this->op_; }
2098 // Return the left hand expression.
2099 Expression*
2100 left()
2101 { return this->left_; }
2103 // Return the right hand expression.
2104 Expression*
2105 right()
2106 { return this->right_; }
2108 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
2109 // Return true if this could be done, false if not. Issue errors at
2110 // LOCATION as appropriate, and sets *ISSUED_ERROR if it did.
2111 static bool
2112 eval_constant(Operator op, Numeric_constant* left_nc,
2113 Numeric_constant* right_nc, Location location,
2114 Numeric_constant* nc, bool* issued_error);
2116 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
2117 // *RESULT. Return true if this could be done, false if not. Issue
2118 // errors at LOCATION as appropriate.
2119 static bool
2120 compare_constant(Operator op, Numeric_constant* left_nc,
2121 Numeric_constant* right_nc, Location location,
2122 bool* result);
2124 static Expression*
2125 do_import(Import_expression*, Location);
2127 // Report an error if OP can not be applied to TYPE. Return whether
2128 // it can. OTYPE is the type of the other operand.
2129 static bool
2130 check_operator_type(Operator op, Type* type, Type* otype, Location);
2132 // Set *RESULT_TYPE to the resulting type when OP is applied to
2133 // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
2134 // success, false on failure.
2135 static bool
2136 operation_type(Operator op, Type* left_type, Type* right_type,
2137 Type** result_type);
2139 protected:
2141 do_traverse(Traverse* traverse);
2143 Expression*
2144 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2146 Expression*
2147 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2149 bool
2150 do_is_constant() const
2151 { return this->left_->is_constant() && this->right_->is_constant(); }
2153 bool
2154 do_is_static_initializer() const;
2156 bool
2157 do_numeric_constant_value(Numeric_constant*) const;
2159 bool
2160 do_boolean_constant_value(bool*) const;
2162 bool
2163 do_discarding_value();
2165 Type*
2166 do_type();
2168 void
2169 do_determine_type(const Type_context*);
2171 void
2172 do_check_types(Gogo*);
2174 Expression*
2175 do_copy()
2177 return Expression::make_binary(this->op_, this->left_->copy(),
2178 this->right_->copy(), this->location());
2181 Bexpression*
2182 do_get_backend(Translate_context*);
2185 do_inlining_cost() const
2186 { return 1; }
2188 void
2189 do_export(Export_function_body*) const;
2191 void
2192 do_dump_expression(Ast_dump_context*) const;
2194 private:
2195 static bool
2196 cmp_to_bool(Operator op, int cmp);
2198 static bool
2199 eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
2200 Location, Numeric_constant*);
2202 static bool
2203 eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
2204 Location, Numeric_constant*);
2206 static bool
2207 eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
2208 Location, Numeric_constant*);
2210 static bool
2211 compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
2213 static bool
2214 compare_float(const Numeric_constant*, const Numeric_constant *, int*);
2216 static bool
2217 compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
2219 Expression*
2220 lower_struct_comparison(Gogo*, Statement_inserter*);
2222 Expression*
2223 lower_array_comparison(Gogo*, Statement_inserter*);
2225 Expression*
2226 lower_interface_value_comparison(Gogo*, Statement_inserter*);
2228 Expression*
2229 lower_compare_to_memcmp(Gogo*, Statement_inserter*);
2231 Expression*
2232 operand_address(Statement_inserter*, Expression*);
2234 // The binary operator to apply.
2235 Operator op_;
2236 // The left hand side operand.
2237 Expression* left_;
2238 // The right hand side operand.
2239 Expression* right_;
2240 // The type of a comparison operation.
2241 Type* type_;
2244 // A string concatenation expression. This is a sequence of strings
2245 // added together. It is created when lowering Binary_expression.
2247 class String_concat_expression : public Expression
2249 public:
2250 String_concat_expression(Expression_list* exprs)
2251 : Expression(EXPRESSION_STRING_CONCAT, exprs->front()->location()),
2252 exprs_(exprs)
2255 // Return the list of string expressions to be concatenated.
2256 Expression_list*
2257 exprs()
2258 { return this->exprs_; }
2260 protected:
2262 do_traverse(Traverse* traverse)
2263 { return this->exprs_->traverse(traverse); }
2265 Expression*
2266 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2267 { return this; }
2269 Expression*
2270 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2272 bool
2273 do_is_constant() const;
2275 bool
2276 do_is_zero_value() const;
2278 bool
2279 do_is_static_initializer() const;
2281 Type*
2282 do_type();
2284 void
2285 do_determine_type(const Type_context*);
2287 void
2288 do_check_types(Gogo*);
2290 Expression*
2291 do_copy()
2292 { return Expression::make_string_concat(this->exprs_->copy()); }
2294 Bexpression*
2295 do_get_backend(Translate_context*)
2296 { go_unreachable(); }
2298 void
2299 do_export(Export_function_body*) const
2300 { go_unreachable(); }
2302 void
2303 do_dump_expression(Ast_dump_context*) const;
2305 private:
2306 // The string expressions to concatenate.
2307 Expression_list* exprs_;
2310 // A call expression. The go statement needs to dig inside this.
2312 class Call_expression : public Expression
2314 public:
2315 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
2316 Location location)
2317 : Expression(EXPRESSION_CALL, location),
2318 fn_(fn), args_(args), type_(NULL), call_(NULL), call_temp_(NULL)
2319 , expected_result_count_(0), is_varargs_(is_varargs),
2320 varargs_are_lowered_(false), types_are_determined_(false),
2321 is_deferred_(false), is_concurrent_(false), issued_error_(false),
2322 is_multi_value_arg_(false), is_flattened_(false)
2325 // The function to call.
2326 Expression*
2327 fn() const
2328 { return this->fn_; }
2330 // The arguments.
2331 Expression_list*
2332 args()
2333 { return this->args_; }
2335 const Expression_list*
2336 args() const
2337 { return this->args_; }
2339 // Get the function type.
2340 Function_type*
2341 get_function_type() const;
2343 // Return the number of values this call will return.
2344 size_t
2345 result_count() const;
2347 // Return the temporary variable that holds the results. This is
2348 // only valid after the expression has been lowered, and is only
2349 // valid for calls which return multiple results.
2350 Temporary_statement*
2351 results() const;
2353 // Set the number of results expected from this call. This is used
2354 // when the call appears in a context that expects multiple results,
2355 // such as a, b = f().
2356 void
2357 set_expected_result_count(size_t);
2359 // Return whether this is a call to the predeclared function
2360 // recover.
2361 bool
2362 is_recover_call() const;
2364 // Set the argument for a call to recover.
2365 void
2366 set_recover_arg(Expression*);
2368 // Whether the last argument is a varargs argument (f(a...)).
2369 bool
2370 is_varargs() const
2371 { return this->is_varargs_; }
2373 // Return whether varargs have already been lowered.
2374 bool
2375 varargs_are_lowered() const
2376 { return this->varargs_are_lowered_; }
2378 // Note that varargs have already been lowered.
2379 void
2380 set_varargs_are_lowered()
2381 { this->varargs_are_lowered_ = true; }
2383 // Whether this call is being deferred.
2384 bool
2385 is_deferred() const
2386 { return this->is_deferred_; }
2388 // Note that the call is being deferred.
2389 void
2390 set_is_deferred()
2391 { this->is_deferred_ = true; }
2393 // Whether this call is concurrently executed.
2394 bool
2395 is_concurrent() const
2396 { return this->is_concurrent_; }
2398 // Note that the call is concurrently executed.
2399 void
2400 set_is_concurrent()
2401 { this->is_concurrent_ = true; }
2403 // We have found an error with this call expression; return true if
2404 // we should report it.
2405 bool
2406 issue_error();
2408 // Whether or not this call contains errors, either in the call or the
2409 // arguments to the call.
2410 bool
2411 is_erroneous_call();
2413 // Whether this call returns multiple results that are used as an
2414 // multi-valued argument.
2415 bool
2416 is_multi_value_arg() const
2417 { return this->is_multi_value_arg_; }
2419 // Note this call is used as a multi-valued argument.
2420 void
2421 set_is_multi_value_arg()
2422 { this->is_multi_value_arg_ = true; }
2424 // Whether this is a call to builtin function.
2425 virtual bool
2426 is_builtin()
2427 { return false; }
2429 // Convert to a Builtin_call_expression, or return NULL.
2430 inline Builtin_call_expression*
2431 builtin_call_expression();
2433 protected:
2435 do_traverse(Traverse*);
2437 virtual Expression*
2438 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2440 virtual Expression*
2441 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2443 bool
2444 do_discarding_value()
2445 { return true; }
2447 virtual Type*
2448 do_type();
2450 virtual void
2451 do_determine_type(const Type_context*);
2453 virtual void
2454 do_check_types(Gogo*);
2456 Expression*
2457 do_copy();
2459 bool
2460 do_must_eval_in_order() const;
2462 virtual Bexpression*
2463 do_get_backend(Translate_context*);
2466 do_inlining_cost() const;
2468 void
2469 do_export(Export_function_body*) const;
2471 virtual bool
2472 do_is_recover_call() const;
2474 virtual void
2475 do_set_recover_arg(Expression*);
2477 // Let a builtin expression change the argument list.
2478 void
2479 set_args(Expression_list* args)
2480 { this->args_ = args; }
2482 // Let a builtin expression lower varargs.
2483 void
2484 lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
2485 Type* varargs_type, size_t param_count,
2486 Slice_storage_escape_disp escape_disp);
2488 // Let a builtin expression check whether types have been
2489 // determined.
2490 bool
2491 determining_types();
2493 void
2494 export_arguments(Export_function_body*) const;
2496 void
2497 do_dump_expression(Ast_dump_context*) const;
2499 void
2500 do_add_conversions();
2502 private:
2503 bool
2504 check_argument_type(int, const Type*, const Type*, Location, bool);
2506 Expression*
2507 intrinsify(Gogo*, Statement_inserter*);
2509 Expression*
2510 interface_method_function(Interface_field_reference_expression*,
2511 Expression**, Location);
2513 Bexpression*
2514 set_results(Translate_context*);
2516 // The function to call.
2517 Expression* fn_;
2518 // The arguments to pass. This may be NULL if there are no
2519 // arguments.
2520 Expression_list* args_;
2521 // The type of the expression, to avoid recomputing it.
2522 Type* type_;
2523 // The backend expression for the call, used for a call which returns a tuple.
2524 Bexpression* call_;
2525 // A temporary variable to store this call if the function returns a tuple.
2526 Temporary_statement* call_temp_;
2527 // If not 0, the number of results expected from this call, when
2528 // used in a context that expects multiple values.
2529 size_t expected_result_count_;
2530 // True if the last argument is a varargs argument (f(a...)).
2531 bool is_varargs_;
2532 // True if varargs have already been lowered.
2533 bool varargs_are_lowered_;
2534 // True if types have been determined.
2535 bool types_are_determined_;
2536 // True if the call is an argument to a defer statement.
2537 bool is_deferred_;
2538 // True if the call is an argument to a go statement.
2539 bool is_concurrent_;
2540 // True if we reported an error about a mismatch between call
2541 // results and uses. This is to avoid producing multiple errors
2542 // when there are multiple Call_result_expressions.
2543 bool issued_error_;
2544 // True if this call is used as an argument that returns multiple results.
2545 bool is_multi_value_arg_;
2546 // True if this expression has already been flattened.
2547 bool is_flattened_;
2550 // A call expression to a builtin function.
2552 class Builtin_call_expression : public Call_expression
2554 public:
2555 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
2556 bool is_varargs, Location location);
2558 // The builtin functions.
2559 enum Builtin_function_code
2561 BUILTIN_INVALID,
2563 // Predeclared builtin functions.
2564 BUILTIN_APPEND,
2565 BUILTIN_CAP,
2566 BUILTIN_CLOSE,
2567 BUILTIN_COMPLEX,
2568 BUILTIN_COPY,
2569 BUILTIN_DELETE,
2570 BUILTIN_IMAG,
2571 BUILTIN_LEN,
2572 BUILTIN_MAKE,
2573 BUILTIN_NEW,
2574 BUILTIN_PANIC,
2575 BUILTIN_PRINT,
2576 BUILTIN_PRINTLN,
2577 BUILTIN_REAL,
2578 BUILTIN_RECOVER,
2580 // Builtin functions from the unsafe package.
2581 BUILTIN_ALIGNOF,
2582 BUILTIN_OFFSETOF,
2583 BUILTIN_SIZEOF
2586 Builtin_function_code
2587 code()
2588 { return this->code_; }
2590 // This overrides Call_expression::is_builtin.
2591 bool
2592 is_builtin()
2593 { return true; }
2595 // Return whether EXPR, of array type, is a constant if passed to
2596 // len or cap.
2597 static bool
2598 array_len_is_constant(Expression* expr);
2600 Expression*
2601 flatten_append(Gogo*, Named_object*, Statement_inserter*, Expression*,
2602 Block*);
2604 protected:
2605 // This overrides Call_expression::do_lower.
2606 Expression*
2607 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2609 Expression*
2610 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2612 bool
2613 do_is_constant() const;
2615 bool
2616 do_numeric_constant_value(Numeric_constant*) const;
2618 bool
2619 do_discarding_value();
2621 Type*
2622 do_type();
2624 void
2625 do_determine_type(const Type_context*);
2627 void
2628 do_check_types(Gogo*);
2630 Expression*
2631 do_copy();
2633 Bexpression*
2634 do_get_backend(Translate_context*);
2637 do_inlining_cost() const
2638 { return 1; }
2640 void
2641 do_export(Export_function_body*) const;
2643 virtual bool
2644 do_is_recover_call() const;
2646 virtual void
2647 do_set_recover_arg(Expression*);
2649 private:
2650 Expression*
2651 one_arg() const;
2653 bool
2654 check_one_arg();
2656 static Type*
2657 real_imag_type(Type*);
2659 static Type*
2660 complex_type(Type*);
2662 Expression*
2663 lower_make(Statement_inserter*);
2665 bool
2666 check_int_value(Expression*, bool is_length, bool* small);
2668 // A pointer back to the general IR structure. This avoids a global
2669 // variable, or passing it around everywhere.
2670 Gogo* gogo_;
2671 // The builtin function being called.
2672 Builtin_function_code code_;
2673 // Used to stop endless loops when the length of an array uses len
2674 // or cap of the array itself.
2675 mutable bool seen_;
2676 // Whether the argument is set for calls to BUILTIN_RECOVER.
2677 bool recover_arg_is_set_;
2680 inline Builtin_call_expression*
2681 Call_expression::builtin_call_expression()
2683 return (this->is_builtin()
2684 ? static_cast<Builtin_call_expression*>(this)
2685 : NULL);
2688 // A single result from a call which returns multiple results.
2690 class Call_result_expression : public Expression
2692 public:
2693 Call_result_expression(Call_expression* call, unsigned int index)
2694 : Expression(EXPRESSION_CALL_RESULT, call->location()),
2695 call_(call), index_(index)
2698 Expression*
2699 call() const
2700 { return this->call_; }
2702 unsigned int
2703 index() const
2704 { return this->index_; }
2706 protected:
2708 do_traverse(Traverse*);
2710 Type*
2711 do_type();
2713 void
2714 do_determine_type(const Type_context*);
2716 void
2717 do_check_types(Gogo*);
2719 Expression*
2720 do_copy()
2722 return new Call_result_expression(this->call_->call_expression(),
2723 this->index_);
2726 bool
2727 do_must_eval_in_order() const
2728 { return true; }
2730 Bexpression*
2731 do_get_backend(Translate_context*);
2733 void
2734 do_dump_expression(Ast_dump_context*) const;
2736 private:
2737 // The underlying call expression.
2738 Expression* call_;
2739 // Which result we want.
2740 unsigned int index_;
2743 // An expression which represents a pointer to a function.
2745 class Func_expression : public Expression
2747 public:
2748 Func_expression(Named_object* function, Expression* closure,
2749 Location location)
2750 : Expression(EXPRESSION_FUNC_REFERENCE, location),
2751 function_(function), closure_(closure),
2752 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS)
2755 // Return the object associated with the function.
2756 Named_object*
2757 named_object() const
2758 { return this->function_; }
2760 // Return the closure for this function. This will return NULL if
2761 // the function has no closure, which is the normal case.
2762 Expression*
2763 closure()
2764 { return this->closure_; }
2766 // Return whether this is a reference to a runtime function.
2767 bool
2768 is_runtime_function() const
2769 { return this->runtime_code_ != Runtime::NUMBER_OF_FUNCTIONS; }
2771 // Return the runtime code for this function expression.
2772 // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
2773 // runtime function.
2774 Runtime::Function
2775 runtime_code() const
2776 { return this->runtime_code_; }
2778 // Set the runtime code for this function expression.
2779 void
2780 set_runtime_code(Runtime::Function code)
2781 { this->runtime_code_ = code; }
2783 // Return a backend expression for the code of a function.
2784 static Bexpression*
2785 get_code_pointer(Gogo*, Named_object* function, Location loc);
2787 protected:
2789 do_traverse(Traverse*);
2791 Type*
2792 do_type();
2794 void
2795 do_determine_type(const Type_context*)
2797 if (this->closure_ != NULL)
2798 this->closure_->determine_type_no_context();
2801 Expression*
2802 do_copy()
2804 return Expression::make_func_reference(this->function_,
2805 (this->closure_ == NULL
2806 ? NULL
2807 : this->closure_->copy()),
2808 this->location());
2811 Bexpression*
2812 do_get_backend(Translate_context*);
2815 do_inlining_cost() const;
2817 void
2818 do_export(Export_function_body*) const;
2820 void
2821 do_dump_expression(Ast_dump_context*) const;
2823 private:
2824 // The function itself.
2825 Named_object* function_;
2826 // A closure. This is normally NULL. For a nested function, it may
2827 // be a struct holding pointers to all the variables referenced by
2828 // this function and defined in enclosing functions.
2829 Expression* closure_;
2830 // The runtime code for the referenced function.
2831 Runtime::Function runtime_code_;
2834 // A function descriptor. A function descriptor is a struct with a
2835 // single field pointing to the function code. This is used for
2836 // functions without closures.
2838 class Func_descriptor_expression : public Expression
2840 public:
2841 Func_descriptor_expression(Named_object* fn);
2843 // Make the function descriptor type, so that it can be converted.
2844 static void
2845 make_func_descriptor_type();
2847 protected:
2849 do_traverse(Traverse*);
2851 Type*
2852 do_type();
2854 void
2855 do_determine_type(const Type_context*)
2858 Expression*
2859 do_copy()
2860 { return Expression::make_func_descriptor(this->fn_); }
2862 bool
2863 do_is_addressable() const
2864 { return true; }
2866 Bexpression*
2867 do_get_backend(Translate_context*);
2869 void
2870 do_dump_expression(Ast_dump_context* context) const;
2872 private:
2873 // The type of all function descriptors.
2874 static Type* descriptor_type;
2876 // The function for which this is the descriptor.
2877 Named_object* fn_;
2878 // The descriptor variable.
2879 Bvariable* dvar_;
2882 // A reference to an unknown name.
2884 class Unknown_expression : public Parser_expression
2886 public:
2887 Unknown_expression(Named_object* named_object, Location location)
2888 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
2889 named_object_(named_object), no_error_message_(false)
2892 // The associated named object.
2893 Named_object*
2894 named_object() const
2895 { return this->named_object_; }
2897 // The name of the identifier which was unknown.
2898 const std::string&
2899 name() const;
2901 // Call this to indicate that we should not give an error if this
2902 // name is never defined. This is used to avoid knock-on errors
2903 // during an erroneous parse.
2904 void
2905 set_no_error_message()
2906 { this->no_error_message_ = true; }
2908 protected:
2909 Expression*
2910 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2912 Expression*
2913 do_copy()
2914 { return new Unknown_expression(this->named_object_, this->location()); }
2916 void
2917 do_dump_expression(Ast_dump_context*) const;
2919 private:
2920 // The unknown name.
2921 Named_object* named_object_;
2922 // True if we should not give errors if this is undefined. This is
2923 // used if there was a parse failure.
2924 bool no_error_message_;
2927 // An index expression. This is lowered to an array index, a string
2928 // index, or a map index.
2930 class Index_expression : public Parser_expression
2932 public:
2933 Index_expression(Expression* left, Expression* start, Expression* end,
2934 Expression* cap, Location location)
2935 : Parser_expression(EXPRESSION_INDEX, location),
2936 left_(left), start_(start), end_(end), cap_(cap)
2939 // Dump an index expression, i.e. an expression of the form
2940 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
2941 static void
2942 dump_index_expression(Ast_dump_context*, const Expression* expr,
2943 const Expression* start, const Expression* end,
2944 const Expression* cap);
2946 protected:
2948 do_traverse(Traverse*);
2950 Expression*
2951 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2953 Expression*
2954 do_copy()
2956 return new Index_expression(this->left_->copy(), this->start_->copy(),
2957 (this->end_ == NULL
2958 ? NULL
2959 : this->end_->copy()),
2960 (this->cap_ == NULL
2961 ? NULL
2962 : this->cap_->copy()),
2963 this->location());
2966 // This shouldn't be called--we don't know yet.
2967 bool
2968 do_must_eval_subexpressions_in_order(int*) const
2969 { go_unreachable(); }
2971 void
2972 do_dump_expression(Ast_dump_context*) const;
2974 void
2975 do_issue_nil_check()
2976 { this->left_->issue_nil_check(); }
2977 private:
2978 // The expression being indexed.
2979 Expression* left_;
2980 // The first index.
2981 Expression* start_;
2982 // The second index. This is NULL for an index, non-NULL for a
2983 // slice.
2984 Expression* end_;
2985 // The capacity argument. This is NULL for indices and slices that use the
2986 // default capacity, non-NULL for indices and slices that specify the
2987 // capacity.
2988 Expression* cap_;
2991 // An array index. This is used for both indexing and slicing.
2993 class Array_index_expression : public Expression
2995 public:
2996 Array_index_expression(Expression* array, Expression* start,
2997 Expression* end, Expression* cap, Location location)
2998 : Expression(EXPRESSION_ARRAY_INDEX, location),
2999 array_(array), start_(start), end_(end), cap_(cap), type_(NULL),
3000 is_lvalue_(false), needs_bounds_check_(true), is_flattened_(false)
3003 // Return the array.
3004 Expression*
3005 array()
3006 { return this->array_; }
3008 const Expression*
3009 array() const
3010 { return this->array_; }
3012 // Return the index of a simple index expression, or the start index
3013 // of a slice expression.
3014 Expression*
3015 start()
3016 { return this->start_; }
3018 const Expression*
3019 start() const
3020 { return this->start_; }
3022 // Return the end index of a slice expression. This is NULL for a
3023 // simple index expression.
3024 Expression*
3025 end()
3026 { return this->end_; }
3028 const Expression*
3029 end() const
3030 { return this->end_; }
3032 // Return whether this array index expression appears in an lvalue
3033 // (left hand side of assignment) context.
3034 bool
3035 is_lvalue() const
3036 { return this->is_lvalue_; }
3038 // Update this array index expression to indicate that it appears
3039 // in a left-hand-side or lvalue context.
3040 void
3041 set_is_lvalue()
3042 { this->is_lvalue_ = true; }
3044 void
3045 set_needs_bounds_check(bool b)
3046 { this->needs_bounds_check_ = b; }
3048 protected:
3050 do_traverse(Traverse*);
3052 Expression*
3053 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3055 Type*
3056 do_type();
3058 void
3059 do_determine_type(const Type_context*);
3061 void
3062 do_check_types(Gogo*);
3064 Expression*
3065 do_copy()
3067 Expression* ret = Expression::make_array_index(this->array_->copy(),
3068 this->start_->copy(),
3069 (this->end_ == NULL
3070 ? NULL
3071 : this->end_->copy()),
3072 (this->cap_ == NULL
3073 ? NULL
3074 : this->cap_->copy()),
3075 this->location());
3076 ret->array_index_expression()->set_needs_bounds_check(this->needs_bounds_check_);
3077 return ret;
3080 bool
3081 do_must_eval_subexpressions_in_order(int* skip) const;
3083 bool
3084 do_is_addressable() const;
3086 void
3087 do_address_taken(bool escapes);
3089 void
3090 do_issue_nil_check()
3091 { this->array_->issue_nil_check(); }
3093 Bexpression*
3094 do_get_backend(Translate_context*);
3097 do_inlining_cost() const
3098 { return this->end_ != NULL ? 2 : 1; }
3100 void
3101 do_export(Export_function_body*) const;
3103 void
3104 do_dump_expression(Ast_dump_context*) const;
3106 private:
3107 // The array we are getting a value from.
3108 Expression* array_;
3109 // The start or only index.
3110 Expression* start_;
3111 // The end index of a slice. This may be NULL for a simple array
3112 // index, or it may be a nil expression for the length of the array.
3113 Expression* end_;
3114 // The capacity argument of a slice. This may be NULL for an array index or
3115 // slice.
3116 Expression* cap_;
3117 // The type of the expression.
3118 Type* type_;
3119 // Whether expr appears in an lvalue context.
3120 bool is_lvalue_;
3121 // Whether bounds check is needed.
3122 bool needs_bounds_check_;
3123 // Whether this has already been flattened.
3124 bool is_flattened_;
3127 // A string index. This is used for both indexing and slicing.
3129 class String_index_expression : public Expression
3131 public:
3132 String_index_expression(Expression* string, Expression* start,
3133 Expression* end, Location location)
3134 : Expression(EXPRESSION_STRING_INDEX, location),
3135 string_(string), start_(start), end_(end), is_flattened_(false)
3138 // Return the string being indexed.
3139 Expression*
3140 string() const
3141 { return this->string_; }
3143 // Return the index of a simple index expression, or the start index
3144 // of a slice expression.
3145 Expression*
3146 start() const
3147 { return this->start_; }
3149 // Return the end index of a slice expression. This is NULL for a
3150 // simple index expression.
3151 Expression*
3152 end() const
3153 { return this->end_; }
3155 protected:
3157 do_traverse(Traverse*);
3159 Expression*
3160 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3162 Type*
3163 do_type();
3165 void
3166 do_determine_type(const Type_context*);
3168 void
3169 do_check_types(Gogo*);
3171 Expression*
3172 do_copy()
3174 return Expression::make_string_index(this->string_->copy(),
3175 this->start_->copy(),
3176 (this->end_ == NULL
3177 ? NULL
3178 : this->end_->copy()),
3179 this->location());
3182 bool
3183 do_must_eval_subexpressions_in_order(int*) const
3184 { return true; }
3186 Bexpression*
3187 do_get_backend(Translate_context*);
3190 do_inlining_cost() const
3191 { return this->end_ != NULL ? 2 : 1; }
3193 void
3194 do_export(Export_function_body*) const;
3196 void
3197 do_dump_expression(Ast_dump_context*) const;
3199 private:
3200 // The string we are getting a value from.
3201 Expression* string_;
3202 // The start or only index.
3203 Expression* start_;
3204 // The end index of a slice. This may be NULL for a single index,
3205 // or it may be a nil expression for the length of the string.
3206 Expression* end_;
3207 // Whether this has already been flattened.
3208 bool is_flattened_;
3211 // An index into a map.
3213 class Map_index_expression : public Expression
3215 public:
3216 Map_index_expression(Expression* map, Expression* index,
3217 Location location)
3218 : Expression(EXPRESSION_MAP_INDEX, location),
3219 map_(map), index_(index), value_pointer_(NULL)
3222 // Return the map.
3223 Expression*
3224 map()
3225 { return this->map_; }
3227 const Expression*
3228 map() const
3229 { return this->map_; }
3231 // Return the index.
3232 Expression*
3233 index()
3234 { return this->index_; }
3236 const Expression*
3237 index() const
3238 { return this->index_; }
3240 // Get the type of the map being indexed.
3241 Map_type*
3242 get_map_type() const;
3244 // Return an expression for the map index. This returns an
3245 // expression that evaluates to a pointer to a value in the map. If
3246 // the key is not present in the map, this will return a pointer to
3247 // the zero value.
3248 Expression*
3249 get_value_pointer(Gogo*);
3251 protected:
3253 do_traverse(Traverse*);
3255 Expression*
3256 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3258 Type*
3259 do_type();
3261 void
3262 do_determine_type(const Type_context*);
3264 void
3265 do_check_types(Gogo*);
3267 Expression*
3268 do_copy()
3270 return Expression::make_map_index(this->map_->copy(),
3271 this->index_->copy(),
3272 this->location());
3275 bool
3276 do_must_eval_subexpressions_in_order(int*) const
3277 { return true; }
3279 // A map index expression is an lvalue but it is not addressable.
3281 Bexpression*
3282 do_get_backend(Translate_context*);
3285 do_inlining_cost() const
3286 { return 5; }
3288 void
3289 do_export(Export_function_body*) const;
3291 void
3292 do_dump_expression(Ast_dump_context*) const;
3294 void
3295 do_add_conversions();
3297 private:
3298 // The map we are looking into.
3299 Expression* map_;
3300 // The index.
3301 Expression* index_;
3302 // A pointer to the value at this index.
3303 Expression* value_pointer_;
3306 // An expression which represents a method bound to its first
3307 // argument.
3309 class Bound_method_expression : public Expression
3311 public:
3312 Bound_method_expression(Expression* expr, const Method *method,
3313 Named_object* function, Location location)
3314 : Expression(EXPRESSION_BOUND_METHOD, location),
3315 expr_(expr), expr_type_(NULL), method_(method), function_(function)
3318 // Return the object which is the first argument.
3319 Expression*
3320 first_argument()
3321 { return this->expr_; }
3323 // Return the implicit type of the first argument. This will be
3324 // non-NULL when using a method from an anonymous field without
3325 // using an explicit stub.
3326 Type*
3327 first_argument_type() const
3328 { return this->expr_type_; }
3330 // Return the method.
3331 const Method*
3332 method() const
3333 { return this->method_; }
3335 // Return the function to call.
3336 Named_object*
3337 function() const
3338 { return this->function_; }
3340 // Set the implicit type of the expression.
3341 void
3342 set_first_argument_type(Type* type)
3343 { this->expr_type_ = type; }
3345 // Create a thunk to call FUNCTION, for METHOD, when it is used as
3346 // part of a method value.
3347 static Named_object*
3348 create_thunk(Gogo*, const Method* method, Named_object* function);
3350 protected:
3352 do_traverse(Traverse*);
3354 Expression*
3355 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3357 Type*
3358 do_type();
3360 void
3361 do_determine_type(const Type_context*);
3363 void
3364 do_check_types(Gogo*);
3366 Expression*
3367 do_copy()
3369 return new Bound_method_expression(this->expr_->copy(), this->method_,
3370 this->function_, this->location());
3373 Bexpression*
3374 do_get_backend(Translate_context*)
3375 { go_unreachable(); }
3377 void
3378 do_dump_expression(Ast_dump_context*) const;
3380 private:
3381 // A mapping from method functions to the thunks we have created for
3382 // them.
3383 typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
3384 static Method_value_thunks method_value_thunks;
3386 // The object used to find the method. This is passed to the method
3387 // as the first argument.
3388 Expression* expr_;
3389 // The implicit type of the object to pass to the method. This is
3390 // NULL in the normal case, non-NULL when using a method from an
3391 // anonymous field which does not require a stub.
3392 Type* expr_type_;
3393 // The method.
3394 const Method* method_;
3395 // The function to call. This is not the same as
3396 // method_->named_object() when the method has a stub. This will be
3397 // the real function rather than the stub.
3398 Named_object* function_;
3401 // A reference to a field in a struct.
3403 class Field_reference_expression : public Expression
3405 public:
3406 Field_reference_expression(Expression* expr, unsigned int field_index,
3407 Location location)
3408 : Expression(EXPRESSION_FIELD_REFERENCE, location),
3409 expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
3412 // Return the struct expression.
3413 Expression*
3414 expr() const
3415 { return this->expr_; }
3417 // Return the field index.
3418 unsigned int
3419 field_index() const
3420 { return this->field_index_; }
3422 // Return whether this node was implied by an anonymous field.
3423 bool
3424 implicit() const
3425 { return this->implicit_; }
3427 void
3428 set_implicit(bool implicit)
3429 { this->implicit_ = implicit; }
3431 // Set the struct expression. This is used when parsing.
3432 void
3433 set_struct_expression(Expression* expr)
3435 go_assert(this->expr_ == NULL);
3436 this->expr_ = expr;
3439 protected:
3441 do_traverse(Traverse* traverse)
3442 { return Expression::traverse(&this->expr_, traverse); }
3444 Expression*
3445 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3447 Type*
3448 do_type();
3450 void
3451 do_determine_type(const Type_context*)
3452 { this->expr_->determine_type_no_context(); }
3454 void
3455 do_check_types(Gogo*);
3457 Expression*
3458 do_copy()
3460 return Expression::make_field_reference(this->expr_->copy(),
3461 this->field_index_,
3462 this->location());
3465 bool
3466 do_is_addressable() const
3467 { return this->expr_->is_addressable(); }
3469 void
3470 do_address_taken(bool escapes)
3471 { this->expr_->address_taken(escapes); }
3473 void
3474 do_issue_nil_check()
3475 { this->expr_->issue_nil_check(); }
3477 Bexpression*
3478 do_get_backend(Translate_context*);
3480 void
3481 do_dump_expression(Ast_dump_context*) const;
3483 private:
3484 // The expression we are looking into. This should have a type of
3485 // struct.
3486 Expression* expr_;
3487 // The zero-based index of the field we are retrieving.
3488 unsigned int field_index_;
3489 // Whether this node was emitted implicitly for an embedded field,
3490 // that is, expr_ is not the expr_ of the original user node.
3491 bool implicit_;
3492 // Whether we have already emitted a fieldtrack call.
3493 bool called_fieldtrack_;
3496 // A reference to a field of an interface.
3498 class Interface_field_reference_expression : public Expression
3500 public:
3501 Interface_field_reference_expression(Expression* expr,
3502 const std::string& name,
3503 Location location)
3504 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
3505 expr_(expr), name_(name)
3508 // Return the expression for the interface object.
3509 Expression*
3510 expr()
3511 { return this->expr_; }
3513 // Return the name of the method to call.
3514 const std::string&
3515 name() const
3516 { return this->name_; }
3518 // Create a thunk to call the method NAME in TYPE when it is used as
3519 // part of a method value.
3520 static Named_object*
3521 create_thunk(Gogo*, Interface_type* type, const std::string& name);
3523 // Return an expression for the pointer to the function to call.
3524 Expression*
3525 get_function();
3527 // Return an expression for the first argument to pass to the interface
3528 // function. This is the real object associated with the interface object.
3529 Expression*
3530 get_underlying_object();
3532 protected:
3534 do_traverse(Traverse* traverse);
3536 Expression*
3537 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3539 Type*
3540 do_type();
3542 void
3543 do_determine_type(const Type_context*);
3545 void
3546 do_check_types(Gogo*);
3548 Expression*
3549 do_copy()
3551 return Expression::make_interface_field_reference(this->expr_->copy(),
3552 this->name_,
3553 this->location());
3556 Bexpression*
3557 do_get_backend(Translate_context*);
3559 void
3560 do_dump_expression(Ast_dump_context*) const;
3562 private:
3563 // A mapping from interface types to a list of thunks we have
3564 // created for methods.
3565 typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
3566 typedef Unordered_map(Interface_type*, Method_thunks*)
3567 Interface_method_thunks;
3568 static Interface_method_thunks interface_method_thunks;
3570 // The expression for the interface object. This should have a type
3571 // of interface or pointer to interface.
3572 Expression* expr_;
3573 // The field we are retrieving--the name of the method.
3574 std::string name_;
3577 // Implement the builtin function new.
3579 class Allocation_expression : public Expression
3581 public:
3582 Allocation_expression(Type* type, Location location)
3583 : Expression(EXPRESSION_ALLOCATION, location),
3584 type_(type), allocate_on_stack_(false),
3585 no_zero_(false)
3588 void
3589 set_allocate_on_stack()
3590 { this->allocate_on_stack_ = true; }
3592 // Mark that the allocated memory doesn't need zeroing.
3593 void
3594 set_no_zero()
3595 { this->no_zero_ = true; }
3597 protected:
3599 do_traverse(Traverse*);
3601 Type*
3602 do_type();
3604 void
3605 do_determine_type(const Type_context*)
3608 void
3609 do_check_types(Gogo*);
3611 Expression*
3612 do_copy();
3614 Bexpression*
3615 do_get_backend(Translate_context*);
3617 void
3618 do_dump_expression(Ast_dump_context*) const;
3620 private:
3621 // The type we are allocating.
3622 Type* type_;
3623 // Whether or not this is a stack allocation.
3624 bool allocate_on_stack_;
3625 // Whether we don't need to zero the allocated memory.
3626 bool no_zero_;
3629 // A general composite literal. This is lowered to a type specific
3630 // version.
3632 class Composite_literal_expression : public Parser_expression
3634 public:
3635 Composite_literal_expression(Type* type, int depth, bool has_keys,
3636 Expression_list* vals, bool all_are_names,
3637 Location location)
3638 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
3639 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys),
3640 all_are_names_(all_are_names), key_path_(std::vector<bool>(depth))
3644 // Mark the DEPTH entry of KEY_PATH as containing a key.
3645 void
3646 update_key_path(size_t depth)
3648 go_assert(depth < this->key_path_.size());
3649 this->key_path_[depth] = true;
3652 protected:
3654 do_traverse(Traverse* traverse);
3656 Expression*
3657 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3659 Expression*
3660 do_copy();
3662 void
3663 do_dump_expression(Ast_dump_context*) const;
3665 private:
3666 Expression*
3667 lower_struct(Gogo*, Type*);
3669 Expression*
3670 lower_array(Type*);
3672 Expression*
3673 make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
3675 Expression*
3676 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
3678 // The type of the composite literal.
3679 Type* type_;
3680 // The depth within a list of composite literals within a composite
3681 // literal, when the type is omitted.
3682 int depth_;
3683 // The values to put in the composite literal.
3684 Expression_list* vals_;
3685 // If this is true, then VALS_ is a list of pairs: a key and a
3686 // value. In an array initializer, a missing key will be NULL.
3687 bool has_keys_;
3688 // If this is true, then HAS_KEYS_ is true, and every key is a
3689 // simple identifier.
3690 bool all_are_names_;
3691 // A complement to DEPTH that indicates for each level starting from 0 to
3692 // DEPTH-1 whether or not this composite literal is nested inside of key or
3693 // a value. This is used to decide which type to use when given a map literal
3694 // with omitted key types.
3695 std::vector<bool> key_path_;
3698 // Helper/mixin class for struct and array construction expressions;
3699 // encapsulates a list of values plus an optional traversal order
3700 // recording the order in which the values should be visited.
3702 class Ordered_value_list
3704 public:
3705 Ordered_value_list(Expression_list* vals)
3706 : vals_(vals), traverse_order_(NULL)
3709 Expression_list*
3710 vals() const
3711 { return this->vals_; }
3714 traverse_vals(Traverse* traverse);
3716 // Get the traversal order (may be NULL)
3717 std::vector<unsigned long>*
3718 traverse_order()
3719 { return traverse_order_; }
3721 // Set the traversal order, used to ensure that we implement the
3722 // order of evaluation rules. Takes ownership of the argument.
3723 void
3724 set_traverse_order(std::vector<unsigned long>* traverse_order)
3725 { this->traverse_order_ = traverse_order; }
3727 private:
3728 // The list of values, in order of the fields in the struct or in
3729 // order of indices in an array. A NULL value of vals_ means that
3730 // all fields/slots should be zero-initialized; a single NULL entry
3731 // in the list means that the corresponding field or array slot
3732 // should be zero-initialized.
3733 Expression_list* vals_;
3734 // If not NULL, the order in which to traverse vals_. This is used
3735 // so that we implement the order of evaluation rules correctly.
3736 std::vector<unsigned long>* traverse_order_;
3739 // Construct a struct.
3741 class Struct_construction_expression : public Expression,
3742 public Ordered_value_list
3744 public:
3745 Struct_construction_expression(Type* type, Expression_list* vals,
3746 Location location)
3747 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
3748 Ordered_value_list(vals),
3749 type_(type)
3752 // Return whether this is a constant initializer.
3753 bool
3754 is_constant_struct() const;
3756 protected:
3758 do_traverse(Traverse* traverse);
3760 bool
3761 do_is_zero_value() const;
3763 bool
3764 do_is_static_initializer() const;
3766 Type*
3767 do_type()
3768 { return this->type_; }
3770 void
3771 do_determine_type(const Type_context*);
3773 void
3774 do_check_types(Gogo*);
3776 Expression*
3777 do_copy();
3779 Expression*
3780 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3782 Bexpression*
3783 do_get_backend(Translate_context*);
3785 void
3786 do_export(Export_function_body*) const;
3788 void
3789 do_dump_expression(Ast_dump_context*) const;
3791 void
3792 do_add_conversions();
3794 private:
3795 // The type of the struct to construct.
3796 Type* type_;
3799 // Construct an array. This class is not used directly; instead we
3800 // use the child classes, Fixed_array_construction_expression and
3801 // Slice_construction_expression.
3803 class Array_construction_expression : public Expression,
3804 public Ordered_value_list
3806 protected:
3807 Array_construction_expression(Expression_classification classification,
3808 Type* type,
3809 const std::vector<unsigned long>* indexes,
3810 Expression_list* vals, Location location)
3811 : Expression(classification, location),
3812 Ordered_value_list(vals),
3813 type_(type), indexes_(indexes)
3814 { go_assert(indexes == NULL || indexes->size() == vals->size()); }
3816 public:
3817 // Return whether this is a constant initializer.
3818 bool
3819 is_constant_array() const;
3821 // Return the number of elements.
3822 size_t
3823 element_count() const
3824 { return this->vals() == NULL ? 0 : this->vals()->size(); }
3826 protected:
3827 virtual int
3828 do_traverse(Traverse* traverse);
3830 bool
3831 do_is_zero_value() const;
3833 bool
3834 do_is_static_initializer() const;
3836 Type*
3837 do_type()
3838 { return this->type_; }
3840 void
3841 do_determine_type(const Type_context*);
3843 void
3844 do_check_types(Gogo*);
3846 void
3847 do_export(Export_function_body*) const;
3849 // The indexes.
3850 const std::vector<unsigned long>*
3851 indexes()
3852 { return this->indexes_; }
3854 Expression*
3855 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3857 // Get the backend constructor for the array values.
3858 Bexpression*
3859 get_constructor(Translate_context* context, Btype* btype);
3861 void
3862 do_dump_expression(Ast_dump_context*) const;
3864 virtual void
3865 dump_slice_storage_expression(Ast_dump_context*) const { }
3867 void
3868 do_add_conversions();
3870 private:
3871 // The type of the array to construct.
3872 Type* type_;
3873 // The list of indexes into the array, one for each value. This may
3874 // be NULL, in which case the indexes start at zero and increment.
3875 const std::vector<unsigned long>* indexes_;
3878 // Construct a fixed array.
3880 class Fixed_array_construction_expression :
3881 public Array_construction_expression
3883 public:
3884 Fixed_array_construction_expression(Type* type,
3885 const std::vector<unsigned long>* indexes,
3886 Expression_list* vals, Location location);
3888 protected:
3889 Expression*
3890 do_copy();
3892 Bexpression*
3893 do_get_backend(Translate_context*);
3896 // Construct a slice.
3898 class Slice_construction_expression : public Array_construction_expression
3900 public:
3901 Slice_construction_expression(Type* type,
3902 const std::vector<unsigned long>* indexes,
3903 Expression_list* vals, Location location);
3905 Expression*
3906 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3908 // Record that the storage for this slice (e.g. vals) cannot escape,
3909 // hence it can be stack-allocated.
3910 void
3911 set_storage_does_not_escape()
3913 this->storage_escapes_ = false;
3916 protected:
3917 // Note that taking the address of a slice literal is invalid.
3920 do_traverse(Traverse* traverse);
3922 Expression*
3923 do_copy();
3925 Bexpression*
3926 do_get_backend(Translate_context*);
3928 void
3929 dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const;
3931 // Create an array value for the constructed slice. Invoked during
3932 // flattening if slice storage does not escape, otherwise invoked
3933 // later on during do_get_backend().
3934 Expression*
3935 create_array_val();
3937 private:
3938 // The type of the values in this slice.
3939 Type* valtype_;
3940 // Array value expression, optionally filled in during flattening.
3941 Expression* array_val_;
3942 // Slice storage expression, optionally filled in during flattening.
3943 Expression* slice_storage_;
3944 // Normally true. Can be set to false if we know that the resulting
3945 // storage for the slice cannot escape.
3946 bool storage_escapes_;
3949 // Construct a map.
3951 class Map_construction_expression : public Expression
3953 public:
3954 Map_construction_expression(Type* type, Expression_list* vals,
3955 Location location)
3956 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
3957 type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL)
3958 { go_assert(vals == NULL || vals->size() % 2 == 0); }
3960 Expression_list*
3961 vals() const
3962 { return this->vals_; }
3964 protected:
3966 do_traverse(Traverse* traverse);
3968 Expression*
3969 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3971 Type*
3972 do_type()
3973 { return this->type_; }
3975 void
3976 do_determine_type(const Type_context*);
3978 void
3979 do_check_types(Gogo*);
3981 Expression*
3982 do_copy();
3984 Bexpression*
3985 do_get_backend(Translate_context*);
3987 void
3988 do_export(Export_function_body*) const;
3990 void
3991 do_dump_expression(Ast_dump_context*) const;
3993 void
3994 do_add_conversions();
3996 private:
3997 // The type of the map to construct.
3998 Type* type_;
3999 // The list of values.
4000 Expression_list* vals_;
4001 // The type of the key-value pair struct for each map element.
4002 Struct_type* element_type_;
4003 // A temporary reference to the variable storing the constructor initializer.
4004 Temporary_statement* constructor_temp_;
4007 // A type guard expression.
4009 class Type_guard_expression : public Expression
4011 public:
4012 Type_guard_expression(Expression* expr, Type* type, Location location)
4013 : Expression(EXPRESSION_TYPE_GUARD, location),
4014 expr_(expr), type_(type)
4017 // Return the expression to convert.
4018 Expression*
4019 expr()
4020 { return this->expr_; }
4022 // Return the type to which to convert.
4023 Type*
4024 type()
4025 { return this->type_; }
4027 protected:
4029 do_traverse(Traverse* traverse);
4031 Expression*
4032 do_flatten(Gogo*, Named_object*, Statement_inserter*);
4034 Type*
4035 do_type()
4036 { return this->type_; }
4038 void
4039 do_determine_type(const Type_context*)
4040 { this->expr_->determine_type_no_context(); }
4042 void
4043 do_check_types(Gogo*);
4045 Expression*
4046 do_copy();
4048 Bexpression*
4049 do_get_backend(Translate_context*);
4051 void
4052 do_dump_expression(Ast_dump_context*) const;
4054 private:
4055 // The expression to convert.
4056 Expression* expr_;
4057 // The type to which to convert.
4058 Type* type_;
4061 // Class Heap_expression.
4063 // When you take the address of an escaping expression, it is allocated
4064 // on the heap. This class implements that.
4066 class Heap_expression : public Expression
4068 public:
4069 Heap_expression(Expression* expr, Location location)
4070 : Expression(EXPRESSION_HEAP, location),
4071 expr_(expr), allocate_on_stack_(false)
4074 Expression*
4075 expr() const
4076 { return this->expr_; }
4078 void
4079 set_allocate_on_stack()
4080 { this->allocate_on_stack_ = true; }
4082 protected:
4084 do_traverse(Traverse* traverse)
4085 { return Expression::traverse(&this->expr_, traverse); }
4087 Type*
4088 do_type();
4089 void
4090 do_determine_type(const Type_context*)
4091 { this->expr_->determine_type_no_context(); }
4093 Expression*
4094 do_copy()
4096 return Expression::make_heap_expression(this->expr_->copy(),
4097 this->location());
4100 Bexpression*
4101 do_get_backend(Translate_context*);
4103 // We only export global objects, and the parser does not generate
4104 // this in global scope.
4105 void
4106 do_export(Export_function_body*) const
4107 { go_unreachable(); }
4109 void
4110 do_dump_expression(Ast_dump_context*) const;
4112 private:
4113 // The expression which is being put on the heap.
4114 Expression* expr_;
4115 // Whether or not this is a stack allocation.
4116 bool allocate_on_stack_;
4119 // A receive expression.
4121 class Receive_expression : public Expression
4123 public:
4124 Receive_expression(Expression* channel, Location location)
4125 : Expression(EXPRESSION_RECEIVE, location),
4126 channel_(channel), temp_receiver_(NULL)
4129 // Return the channel.
4130 Expression*
4131 channel()
4132 { return this->channel_; }
4134 static Expression*
4135 do_import(Import_expression*, Location);
4137 protected:
4139 do_traverse(Traverse* traverse)
4140 { return Expression::traverse(&this->channel_, traverse); }
4142 bool
4143 do_discarding_value()
4144 { return true; }
4146 Type*
4147 do_type();
4149 Expression*
4150 do_flatten(Gogo*, Named_object*, Statement_inserter*);
4152 void
4153 do_determine_type(const Type_context*)
4154 { this->channel_->determine_type_no_context(); }
4156 void
4157 do_check_types(Gogo*);
4159 Expression*
4160 do_copy()
4162 return Expression::make_receive(this->channel_->copy(), this->location());
4166 do_inlining_cost() const
4167 { return 1; }
4169 bool
4170 do_must_eval_in_order() const
4171 { return true; }
4173 Bexpression*
4174 do_get_backend(Translate_context*);
4176 void
4177 do_export(Export_function_body*) const;
4179 void
4180 do_dump_expression(Ast_dump_context*) const;
4182 private:
4183 // The channel from which we are receiving.
4184 Expression* channel_;
4185 // A temporary reference to the variable storing the received data.
4186 Temporary_statement* temp_receiver_;
4189 // An expression that represents a slice value: a struct with value pointer,
4190 // length, and capacity fields.
4192 class Slice_value_expression : public Expression
4194 public:
4195 Slice_value_expression(Type* type, Expression* valmem, Expression* len,
4196 Expression* cap, Location location)
4197 : Expression(EXPRESSION_SLICE_VALUE, location),
4198 type_(type), valmem_(valmem), len_(len), cap_(cap)
4201 // The memory holding the values in the slice. The type should be a
4202 // pointer to the element value of the slice.
4203 Expression*
4204 valmem() const
4205 { return this->valmem_; }
4207 protected:
4209 do_traverse(Traverse*);
4211 Type*
4212 do_type()
4213 { return this->type_; }
4215 void
4216 do_determine_type(const Type_context*)
4219 Expression*
4220 do_copy();
4222 Bexpression*
4223 do_get_backend(Translate_context* context);
4225 void
4226 do_dump_expression(Ast_dump_context*) const;
4228 private:
4229 // The type of the slice value.
4230 Type* type_;
4231 // The memory holding the values in the slice.
4232 Expression* valmem_;
4233 // The length of the slice.
4234 Expression* len_;
4235 // The capacity of the slice.
4236 Expression* cap_;
4239 // Conditional expressions.
4241 class Conditional_expression : public Expression
4243 public:
4244 Conditional_expression(Expression* cond, Expression* then_expr,
4245 Expression* else_expr, Location location)
4246 : Expression(EXPRESSION_CONDITIONAL, location),
4247 cond_(cond), then_(then_expr), else_(else_expr)
4250 Expression*
4251 condition() const
4252 { return this->cond_; }
4254 protected:
4256 do_traverse(Traverse*);
4258 Type*
4259 do_type();
4261 void
4262 do_determine_type(const Type_context*);
4264 Expression*
4265 do_copy()
4267 return new Conditional_expression(this->cond_->copy(), this->then_->copy(),
4268 this->else_->copy(), this->location());
4271 Bexpression*
4272 do_get_backend(Translate_context* context);
4274 void
4275 do_dump_expression(Ast_dump_context*) const;
4277 private:
4278 // The condition to be checked.
4279 Expression* cond_;
4280 // The expression to execute if the condition is true.
4281 Expression* then_;
4282 // The expression to execute if the condition is false.
4283 Expression* else_;
4286 // Compound expressions.
4288 class Compound_expression : public Expression
4290 public:
4291 Compound_expression(Expression* init, Expression* expr, Location location)
4292 : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr)
4295 Expression*
4296 init() const
4297 { return this->init_; }
4299 protected:
4301 do_traverse(Traverse*);
4303 Type*
4304 do_type();
4306 void
4307 do_determine_type(const Type_context*);
4309 Expression*
4310 do_copy()
4312 return new Compound_expression(this->init_->copy(), this->expr_->copy(),
4313 this->location());
4316 Bexpression*
4317 do_get_backend(Translate_context* context);
4319 void
4320 do_dump_expression(Ast_dump_context*) const;
4322 private:
4323 // The expression that is evaluated first and discarded.
4324 Expression* init_;
4325 // The expression that is evaluated and returned.
4326 Expression* expr_;
4329 // A backend expression. This is a backend expression wrapped in an
4330 // Expression, for convenience during backend generation.
4332 class Backend_expression : public Expression
4334 public:
4335 Backend_expression(Bexpression* bexpr, Type* type, Location location)
4336 : Expression(EXPRESSION_BACKEND, location), bexpr_(bexpr), type_(type)
4339 protected:
4341 do_traverse(Traverse*);
4343 // For now these are always valid static initializers. If that
4344 // changes we can change this.
4345 bool
4346 do_is_static_initializer() const
4347 { return true; }
4349 Type*
4350 do_type()
4351 { return this->type_; }
4353 void
4354 do_determine_type(const Type_context*)
4357 Expression*
4358 do_copy();
4360 Bexpression*
4361 do_get_backend(Translate_context*)
4362 { return this->bexpr_; }
4364 void
4365 do_dump_expression(Ast_dump_context*) const;
4367 private:
4368 // The backend expression we are wrapping.
4369 Bexpression* bexpr_;
4370 // The type of the expression;
4371 Type* type_;
4374 // A numeric constant. This is used both for untyped constants and
4375 // for constants that have a type.
4377 class Numeric_constant
4379 public:
4380 Numeric_constant()
4381 : classification_(NC_INVALID), type_(NULL)
4384 ~Numeric_constant();
4386 Numeric_constant(const Numeric_constant&);
4388 Numeric_constant& operator=(const Numeric_constant&);
4390 // Check equality with another numeric constant.
4391 bool
4392 equals(const Numeric_constant&) const;
4394 // Set to an unsigned long value.
4395 void
4396 set_unsigned_long(Type*, unsigned long);
4398 // Set to an integer value.
4399 void
4400 set_int(Type*, const mpz_t);
4402 // Set to a rune value.
4403 void
4404 set_rune(Type*, const mpz_t);
4406 // Set to a floating point value.
4407 void
4408 set_float(Type*, const mpfr_t);
4410 // Set to a complex value.
4411 void
4412 set_complex(Type*, const mpc_t);
4414 // Mark numeric constant as invalid.
4415 void
4416 set_invalid()
4417 { this->classification_ = NC_INVALID; }
4419 // Classifiers.
4420 bool
4421 is_int() const
4422 { return this->classification_ == Numeric_constant::NC_INT; }
4424 bool
4425 is_rune() const
4426 { return this->classification_ == Numeric_constant::NC_RUNE; }
4428 bool
4429 is_float() const
4430 { return this->classification_ == Numeric_constant::NC_FLOAT; }
4432 bool
4433 is_complex() const
4434 { return this->classification_ == Numeric_constant::NC_COMPLEX; }
4436 bool
4437 is_invalid() const
4438 { return this->classification_ == Numeric_constant::NC_INVALID; }
4440 // Value retrievers. These will initialize the values as well as
4441 // set them. GET_INT is only valid if IS_INT returns true, and
4442 // likewise respectively.
4443 void
4444 get_int(mpz_t*) const;
4446 void
4447 get_rune(mpz_t*) const;
4449 void
4450 get_float(mpfr_t*) const;
4452 void
4453 get_complex(mpc_t*) const;
4455 // Codes returned by to_unsigned_long.
4456 enum To_unsigned_long
4458 // Value is integer and fits in unsigned long.
4459 NC_UL_VALID,
4460 // Value is not integer.
4461 NC_UL_NOTINT,
4462 // Value is integer but is negative.
4463 NC_UL_NEGATIVE,
4464 // Value is non-negative integer but does not fit in unsigned
4465 // long.
4466 NC_UL_BIG
4469 // If the value can be expressed as an integer that fits in an
4470 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
4471 // one of the other To_unsigned_long codes.
4472 To_unsigned_long
4473 to_unsigned_long(unsigned long* val) const;
4475 // If the value can be expressed as an integer that describes the
4476 // size of an object in memory, set *VAL and return true.
4477 // Otherwise, return false. Currently we use int64_t to represent a
4478 // memory size, as in Type::backend_type_size.
4479 bool
4480 to_memory_size(int64_t* val) const;
4482 // If the value can be expressed as an int, return true and
4483 // initialize and set VAL. This will return false for a value with
4484 // an explicit float or complex type, even if the value is integral.
4485 bool
4486 to_int(mpz_t* val) const;
4488 // If the value can be expressed as a float, return true and
4489 // initialize and set VAL.
4490 bool
4491 to_float(mpfr_t* val) const;
4493 // If the value can be expressed as a complex, return true and
4494 // initialize and set VR and VI.
4495 bool
4496 to_complex(mpc_t* val) const;
4498 // Get the type.
4499 Type*
4500 type() const;
4502 // If the constant can be expressed in TYPE, then set the type of
4503 // the constant to TYPE and return true. Otherwise return false,
4504 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
4505 // the location to use for the error.
4506 bool
4507 set_type(Type* type, bool issue_error, Location location);
4509 // Return an Expression for this value.
4510 Expression*
4511 expression(Location) const;
4513 // Calculate a hash code with a given seed.
4514 unsigned int
4515 hash(unsigned int seed) const;
4517 private:
4518 void
4519 clear();
4521 To_unsigned_long
4522 mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
4524 To_unsigned_long
4525 mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
4527 bool
4528 mpz_to_memory_size(const mpz_t ival, int64_t* val) const;
4530 bool
4531 mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const;
4533 bool
4534 check_int_type(Integer_type*, bool, Location);
4536 bool
4537 check_float_type(Float_type*, bool, Location);
4539 bool
4540 check_complex_type(Complex_type*, bool, Location);
4542 static bool
4543 is_float_neg_zero(const mpfr_t, int bits);
4545 // The kinds of constants.
4546 enum Classification
4548 NC_INVALID,
4549 NC_RUNE,
4550 NC_INT,
4551 NC_FLOAT,
4552 NC_COMPLEX
4555 // The kind of constant.
4556 Classification classification_;
4557 // The value.
4558 union
4560 // If NC_INT or NC_RUNE.
4561 mpz_t int_val;
4562 // If NC_FLOAT.
4563 mpfr_t float_val;
4564 // If NC_COMPLEX.
4565 mpc_t complex_val;
4566 } u_;
4567 // The type if there is one. This will be NULL for an untyped
4568 // constant.
4569 Type* type_;
4572 // Temporary buffer size for string conversions.
4573 // Also known to the runtime as tmpStringBufSize in runtime/string.go.
4574 static const int tmp_string_buf_size = 32;
4576 #endif // !defined(GO_EXPRESSIONS_H)