aix: Use lcomm for TLS static data.
[official-gcc.git] / gcc / go / gofrontend / expressions.h
blob712f6870211088f4fd275a560a1776285fdc01b8
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 // Set the location of an expression and all its subexpressions.
553 // This is used for const declarations where the expression is
554 // copied from an earlier declaration.
555 void
556 set_location(Location loc);
558 // For set_location. This should really be a local class in
559 // Expression, but it needs types defined in gogo.h.
560 friend class Set_location;
562 // Return whether this is a constant expression.
563 bool
564 is_constant() const
565 { return this->do_is_constant(); }
567 // Return whether this is the zero value of its type.
568 bool
569 is_zero_value() const
570 { return this->do_is_zero_value(); }
572 // Return whether this expression can be used as a static
573 // initializer. This is true for an expression that has only
574 // numbers and pointers to global variables or composite literals
575 // that do not require runtime initialization. It is false if we
576 // must generate code to compute this expression when it is used to
577 // initialize a global variable. This is not a language-level
578 // concept, but an implementation-level one. If this expression is
579 // used to initialize a global variable, this is true if we can pass
580 // an initializer to the backend, false if we must generate code to
581 // initialize the variable. It is always safe for this method to
582 // return false, but the resulting code may be less efficient.
583 bool
584 is_static_initializer() const
585 { return this->do_is_static_initializer(); }
587 // If this is not a numeric constant, return false. If it is one,
588 // return true, and set VAL to hold the value.
589 bool
590 numeric_constant_value(Numeric_constant* val) const
591 { return this->do_numeric_constant_value(val); }
593 // If this is not a constant expression with string type, return
594 // false. If it is one, return true, and set VAL to the value.
595 bool
596 string_constant_value(std::string* val) const
597 { return this->do_string_constant_value(val); }
599 // If this is not a constant expression with boolean type, return
600 // false. If it is one, return true, and set VAL to the value.
601 bool
602 boolean_constant_value(bool* val) const
603 { return this->do_boolean_constant_value(val); }
605 // If this is a const reference expression, return the named
606 // object to which the expression refers, otherwise return NULL.
607 const Named_object*
608 named_constant() const;
610 // This is called if the value of this expression is being
611 // discarded. This issues warnings about computed values being
612 // unused. This returns true if all is well, false if it issued an
613 // error message.
614 bool
615 discarding_value()
616 { return this->do_discarding_value(); }
618 // Return whether this is an error expression.
619 bool
620 is_error_expression() const
621 { return this->classification_ == EXPRESSION_ERROR; }
623 // Return whether this expression really represents a type.
624 bool
625 is_type_expression() const
626 { return this->classification_ == EXPRESSION_TYPE; }
628 // If this is a variable reference, return the Var_expression
629 // structure. Otherwise, return NULL. This is a controlled dynamic
630 // cast.
631 Var_expression*
632 var_expression()
633 { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
635 const Var_expression*
636 var_expression() const
637 { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
639 // If this is a enclosed_variable reference, return the
640 // Enclosed_var_expression structure. Otherwise, return NULL.
641 // This is a controlled dynamic cast.
642 Enclosed_var_expression*
643 enclosed_var_expression()
644 { return this->convert<Enclosed_var_expression,
645 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
647 const Enclosed_var_expression*
648 enclosed_var_expression() const
649 { return this->convert<const Enclosed_var_expression,
650 EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
653 // If this is a reference to a temporary variable, return the
654 // Temporary_reference_expression. Otherwise, return NULL.
655 Temporary_reference_expression*
656 temporary_reference_expression()
658 return this->convert<Temporary_reference_expression,
659 EXPRESSION_TEMPORARY_REFERENCE>();
662 // If this is a set-and-use-temporary, return the
663 // Set_and_use_temporary_expression. Otherwise, return NULL.
664 Set_and_use_temporary_expression*
665 set_and_use_temporary_expression()
667 return this->convert<Set_and_use_temporary_expression,
668 EXPRESSION_SET_AND_USE_TEMPORARY>();
671 // Return whether this is a sink expression.
672 bool
673 is_sink_expression() const
674 { return this->classification_ == EXPRESSION_SINK; }
676 // If this is a string expression, return the String_expression
677 // structure. Otherwise, return NULL.
678 String_expression*
679 string_expression()
680 { return this->convert<String_expression, EXPRESSION_STRING>(); }
682 // If this is a conversion expression, return the Type_conversion_expression
683 // structure. Otherwise, return NULL.
684 Type_conversion_expression*
685 conversion_expression()
686 { return this->convert<Type_conversion_expression, EXPRESSION_CONVERSION>(); }
688 // If this is an unsafe conversion expression, return the
689 // Unsafe_type_conversion_expression structure. Otherwise, return NULL.
690 Unsafe_type_conversion_expression*
691 unsafe_conversion_expression()
693 return this->convert<Unsafe_type_conversion_expression,
694 EXPRESSION_UNSAFE_CONVERSION>();
697 // Return whether this is the expression nil.
698 bool
699 is_nil_expression() const
700 { return this->classification_ == EXPRESSION_NIL; }
702 // If this is an indirection through a pointer, return the
703 // expression being pointed through. Otherwise return this.
704 Expression*
705 deref();
707 // If this is a unary expression, return the Unary_expression
708 // structure. Otherwise return NULL.
709 Unary_expression*
710 unary_expression()
711 { return this->convert<Unary_expression, EXPRESSION_UNARY>(); }
713 // If this is a binary expression, return the Binary_expression
714 // structure. Otherwise return NULL.
715 Binary_expression*
716 binary_expression()
717 { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
719 // If this is a string concatenation expression, return the
720 // String_concat_expression structure. Otherwise, return NULL.
721 String_concat_expression*
722 string_concat_expression()
724 return this->convert<String_concat_expression, EXPRESSION_STRING_CONCAT>();
727 // If this is a call expression, return the Call_expression
728 // structure. Otherwise, return NULL. This is a controlled dynamic
729 // cast.
730 Call_expression*
731 call_expression()
732 { return this->convert<Call_expression, EXPRESSION_CALL>(); }
734 // If this is a call_result expression, return the Call_result_expression
735 // structure. Otherwise, return NULL. This is a controlled dynamic
736 // cast.
737 Call_result_expression*
738 call_result_expression()
739 { return this->convert<Call_result_expression, EXPRESSION_CALL_RESULT>(); }
741 // If this is an expression which refers to a function, return the
742 // Func_expression structure. Otherwise, return NULL.
743 Func_expression*
744 func_expression()
745 { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
747 const Func_expression*
748 func_expression() const
749 { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
751 // If this is an expression which refers to an unknown name, return
752 // the Unknown_expression structure. Otherwise, return NULL.
753 Unknown_expression*
754 unknown_expression()
755 { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
757 const Unknown_expression*
758 unknown_expression() const
760 return this->convert<const Unknown_expression,
761 EXPRESSION_UNKNOWN_REFERENCE>();
764 // If this is an index expression, return the Index_expression
765 // structure. Otherwise, return NULL.
766 Index_expression*
767 index_expression()
768 { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
770 // If this is an expression which refers to indexing in a array,
771 // return the Array_index_expression structure. Otherwise, return
772 // NULL.
773 Array_index_expression*
774 array_index_expression()
775 { return this->convert<Array_index_expression, EXPRESSION_ARRAY_INDEX>(); }
777 // If this is an expression which refers to indexing in a string,
778 // return the String_index_expression structure. Otherwise, return
779 // NULL.
780 String_index_expression*
781 string_index_expression()
782 { return this->convert<String_index_expression, EXPRESSION_STRING_INDEX>(); }
784 // If this is an expression which refers to indexing in a map,
785 // return the Map_index_expression structure. Otherwise, return
786 // NULL.
787 Map_index_expression*
788 map_index_expression()
789 { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
791 // If this is a bound method expression, return the
792 // Bound_method_expression structure. Otherwise, return NULL.
793 Bound_method_expression*
794 bound_method_expression()
795 { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
797 // If this is a reference to a field in a struct, return the
798 // Field_reference_expression structure. Otherwise, return NULL.
799 Field_reference_expression*
800 field_reference_expression()
802 return this->convert<Field_reference_expression,
803 EXPRESSION_FIELD_REFERENCE>();
806 // If this is a reference to a field in an interface, return the
807 // Interface_field_reference_expression structure. Otherwise,
808 // return NULL.
809 Interface_field_reference_expression*
810 interface_field_reference_expression()
812 return this->convert<Interface_field_reference_expression,
813 EXPRESSION_INTERFACE_FIELD_REFERENCE>();
816 // If this is an allocation expression, return the Allocation_expression
817 // structure. Otherwise, return NULL.
818 Allocation_expression*
819 allocation_expression()
820 { return this->convert<Allocation_expression, EXPRESSION_ALLOCATION>(); }
822 // If this is a general composite literal, return the
823 // Composite_literal_expression structure. Otherwise, return NULL.
824 Composite_literal_expression*
825 complit()
827 return this->convert<Composite_literal_expression,
828 EXPRESSION_COMPOSITE_LITERAL>();
831 // If this is a struct composite literal, return the
832 // Struct_construction_expression structure. Otherwise, return NULL.
833 Struct_construction_expression*
834 struct_literal()
836 return this->convert<Struct_construction_expression,
837 EXPRESSION_STRUCT_CONSTRUCTION>();
840 // If this is a array composite literal, return the
841 // Array_construction_expression structure. Otherwise, return NULL.
842 Fixed_array_construction_expression*
843 array_literal()
845 return this->convert<Fixed_array_construction_expression,
846 EXPRESSION_FIXED_ARRAY_CONSTRUCTION>();
849 // If this is a slice composite literal, return the
850 // Slice_construction_expression structure. Otherwise, return NULL.
851 Slice_construction_expression*
852 slice_literal()
854 return this->convert<Slice_construction_expression,
855 EXPRESSION_SLICE_CONSTRUCTION>();
858 // If this is a map composite literal, return the
859 // Map_construction_expression structure. Otherwise, return NULL.
860 Map_construction_expression*
861 map_literal()
863 return this->convert<Map_construction_expression,
864 EXPRESSION_MAP_CONSTRUCTION>();
867 // If this is a type guard expression, return the
868 // Type_guard_expression structure. Otherwise, return NULL.
869 Type_guard_expression*
870 type_guard_expression()
871 { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
873 // If this is a heap expression, returhn the Heap_expression structure.
874 // Otherwise, return NULL.
875 Heap_expression*
876 heap_expression()
877 { return this->convert<Heap_expression, EXPRESSION_HEAP>(); }
879 // If this is a receive expression, return the Receive_expression
880 // structure. Otherwise, return NULL.
881 Receive_expression*
882 receive_expression()
883 { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
885 // If this is a slice value expression, return the Slice_valiue_expression
886 // structure. Otherwise, return NULL.
887 Slice_value_expression*
888 slice_value_expression()
889 { return this->convert<Slice_value_expression, EXPRESSION_SLICE_VALUE>(); }
891 // If this is a conditional expression, return the Conditional_expression
892 // structure. Otherwise, return NULL.
893 Conditional_expression*
894 conditional_expression()
895 { return this->convert<Conditional_expression, EXPRESSION_CONDITIONAL>(); }
897 // If this is a compound expression, return the Compound_expression structure.
898 // Otherwise, return NULL.
899 Compound_expression*
900 compound_expression()
901 { return this->convert<Compound_expression, EXPRESSION_COMPOUND>(); }
903 // Return true if this is a composite literal.
904 bool
905 is_composite_literal() const;
907 // Return true if this is a composite literal which is not constant.
908 bool
909 is_nonconstant_composite_literal() const;
911 // Return true if this is a variable or temporary variable.
912 bool
913 is_variable() const;
915 // Return true if this is a reference to a local variable.
916 bool
917 is_local_variable() const;
919 // Return true if two expressions refer to the same variable or
920 // struct field.
921 static bool
922 is_same_variable(Expression*, Expression*);
924 // Make the builtin function descriptor type, so that it can be
925 // converted.
926 static void
927 make_func_descriptor_type();
929 // Traverse an expression.
930 static int
931 traverse(Expression**, Traverse*);
933 // Traverse subexpressions of this expression.
935 traverse_subexpressions(Traverse*);
937 // Lower an expression. This is called immediately after parsing.
938 // FUNCTION is the function we are in; it will be NULL for an
939 // expression initializing a global variable. INSERTER may be used
940 // to insert statements before the statement or initializer
941 // containing this expression; it is normally used to create
942 // temporary variables. IOTA_VALUE is the value that we should give
943 // to any iota expressions. This function must resolve expressions
944 // which could not be fully parsed into their final form. It
945 // returns the same Expression or a new one.
946 Expression*
947 lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter,
948 int iota_value)
949 { return this->do_lower(gogo, function, inserter, iota_value); }
951 // Flatten an expression. This is called after order_evaluation.
952 // FUNCTION is the function we are in; it will be NULL for an
953 // expression initializing a global variable. INSERTER may be used
954 // to insert statements before the statement or initializer
955 // containing this expression; it is normally used to create
956 // temporary variables. This function must resolve expressions
957 // which could not be fully parsed into their final form. It
958 // returns the same Expression or a new one.
959 Expression*
960 flatten(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
961 { return this->do_flatten(gogo, function, inserter); }
963 // Make implicit type conversions explicit.
964 void
965 add_conversions()
966 { this->do_add_conversions(); }
968 // Determine the real type of an expression with abstract integer,
969 // floating point, or complex type. TYPE_CONTEXT describes the
970 // expected type.
971 void
972 determine_type(const Type_context*);
974 // Check types in an expression.
975 void
976 check_types(Gogo* gogo)
977 { this->do_check_types(gogo); }
979 // Determine the type when there is no context.
980 void
981 determine_type_no_context();
983 // Return the current type of the expression. This may be changed
984 // by determine_type.
985 Type*
986 type()
987 { return this->do_type(); }
989 // Return a copy of an expression.
990 Expression*
991 copy()
992 { return this->do_copy(); }
994 // Return the cost of this statement for inlining purposes.
996 inlining_cost() const
997 { return this->do_inlining_cost(); }
999 // Return whether the expression is addressable--something which may
1000 // be used as the operand of the unary & operator.
1001 bool
1002 is_addressable() const
1003 { return this->do_is_addressable(); }
1005 // Note that we are taking the address of this expression. ESCAPES
1006 // is true if this address escapes the current function.
1007 void
1008 address_taken(bool escapes)
1009 { this->do_address_taken(escapes); }
1011 // Note that a nil check must be issued for this expression.
1012 void
1013 issue_nil_check()
1014 { this->do_issue_nil_check(); }
1016 // Return whether this expression must be evaluated in order
1017 // according to the order of evaluation rules. This is basically
1018 // true of all expressions with side-effects.
1019 bool
1020 must_eval_in_order() const
1021 { return this->do_must_eval_in_order(); }
1023 // Return whether subexpressions of this expression must be
1024 // evaluated in order. This is true of index expressions and
1025 // pointer indirections. This sets *SKIP to the number of
1026 // subexpressions to skip during traversing, as index expressions
1027 // only requiring moving the index, not the array.
1028 bool
1029 must_eval_subexpressions_in_order(int* skip) const
1031 *skip = 0;
1032 return this->do_must_eval_subexpressions_in_order(skip);
1035 // Return the backend representation for this expression.
1036 Bexpression*
1037 get_backend(Translate_context*);
1039 // Return an expression handling any conversions which must be done during
1040 // assignment.
1041 static Expression*
1042 convert_for_assignment(Gogo*, Type* lhs_type, Expression* rhs,
1043 Location location);
1045 // Return an expression converting a value of one interface type to another
1046 // interface type. If FOR_TYPE_GUARD is true this is for a type
1047 // assertion.
1048 static Expression*
1049 convert_interface_to_interface(Type* lhs_type,
1050 Expression* rhs, bool for_type_guard,
1051 Location);
1053 // Return an expression for a conversion from a non-interface type to an
1054 // interface type. If ON_STACK is true, it can allocate the storage on
1055 // stack.
1056 static Expression*
1057 convert_type_to_interface(Type* lhs_type, Expression* rhs,
1058 bool on_stack, Location);
1060 // Return a backend expression implementing the comparison LEFT OP RIGHT.
1061 // TYPE is the type of both sides.
1062 static Bexpression*
1063 comparison(Translate_context*, Type* result_type, Operator op,
1064 Expression* left, Expression* right, Location);
1066 // Return the backend expression for the numeric constant VAL.
1067 static Bexpression*
1068 backend_numeric_constant_expression(Translate_context*,
1069 Numeric_constant* val);
1071 // Export the expression.
1072 void
1073 export_expression(Export_function_body* efb) const
1074 { this->do_export(efb); }
1076 // Import an expression. The location should be used for the
1077 // returned expression. Errors should be reported using the
1078 // Import's location method.
1079 static Expression*
1080 import_expression(Import_expression*, Location);
1082 // Insert bounds checks for an index expression.
1083 static void
1084 check_bounds(Expression* val, Operator, Expression* bound, Runtime::Function,
1085 Runtime::Function, Runtime::Function, Runtime::Function,
1086 Statement_inserter*, Location);
1088 // Return an expression for constructing a direct interface type from a
1089 // pointer.
1090 static Expression*
1091 pack_direct_iface(Type*, Expression*, Location);
1093 // Return an expression of the underlying pointer for a direct interface
1094 // type (the opposite of pack_direct_iface).
1095 static Expression*
1096 unpack_direct_iface(Expression*, Location);
1098 // Return an expression representing the type descriptor field of an
1099 // interface.
1100 static Expression*
1101 get_interface_type_descriptor(Expression*);
1103 // Look through the expression of a Slice_value_expression's valmem to
1104 // find an call to makeslice.
1105 static std::pair<Call_expression*, Temporary_statement*>
1106 find_makeslice_call(Expression*);
1108 // Dump an expression to a dump constext.
1109 void
1110 dump_expression(Ast_dump_context*) const;
1112 protected:
1113 // May be implemented by child class: traverse the expressions.
1114 virtual int
1115 do_traverse(Traverse*);
1117 // Return a lowered expression.
1118 virtual Expression*
1119 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1120 { return this; }
1122 // Return a flattened expression.
1123 virtual Expression*
1124 do_flatten(Gogo*, Named_object*, Statement_inserter*)
1125 { return this; }
1127 // Make implicit type conversions explicit.
1128 virtual void
1129 do_add_conversions()
1132 // Return whether this is a constant expression.
1133 virtual bool
1134 do_is_constant() const
1135 { return false; }
1137 // Return whether this is the zero value of its type.
1138 virtual bool
1139 do_is_zero_value() const
1140 { return false; }
1142 // Return whether this expression can be used as a constant
1143 // initializer.
1144 virtual bool
1145 do_is_static_initializer() const
1146 { return false; }
1148 // Return whether this is a constant expression of numeric type, and
1149 // set the Numeric_constant to the value.
1150 virtual bool
1151 do_numeric_constant_value(Numeric_constant*) const
1152 { return false; }
1154 // Return whether this is a constant expression of string type, and
1155 // set VAL to the value.
1156 virtual bool
1157 do_string_constant_value(std::string*) const
1158 { return false; }
1160 // Return whether this is a constant expression of boolean type, and
1161 // set VAL to the value.
1162 virtual bool
1163 do_boolean_constant_value(bool*) const
1164 { return false; }
1166 // Called by the parser if the value is being discarded.
1167 virtual bool
1168 do_discarding_value();
1170 // Child class holds type.
1171 virtual Type*
1172 do_type() = 0;
1174 // Child class implements determining type information.
1175 virtual void
1176 do_determine_type(const Type_context*) = 0;
1178 // Child class implements type checking if needed.
1179 virtual void
1180 do_check_types(Gogo*)
1183 // Child class implements copying.
1184 virtual Expression*
1185 do_copy() = 0;
1187 // Child class implements determining the cost of this statement for
1188 // inlining. The default cost is high, so we only need to define
1189 // this method for expressions that can be inlined.
1190 virtual int
1191 do_inlining_cost() const
1192 { return 0x100000; }
1194 // Child class implements whether the expression is addressable.
1195 virtual bool
1196 do_is_addressable() const
1197 { return false; }
1199 // Child class implements taking the address of an expression.
1200 virtual void
1201 do_address_taken(bool)
1204 // Child class implements issuing a nil check if the address is taken.
1205 virtual void
1206 do_issue_nil_check()
1209 // Child class implements whether this expression must be evaluated
1210 // in order.
1211 virtual bool
1212 do_must_eval_in_order() const
1213 { return false; }
1215 // Child class implements whether this expressions requires that
1216 // subexpressions be evaluated in order. The child implementation
1217 // may set *SKIP if it should be non-zero.
1218 virtual bool
1219 do_must_eval_subexpressions_in_order(int* /* skip */) const
1220 { return false; }
1222 // Child class implements conversion to backend representation.
1223 virtual Bexpression*
1224 do_get_backend(Translate_context*) = 0;
1226 // Child class implements export.
1227 virtual void
1228 do_export(Export_function_body*) const;
1230 // For children to call to give an error for an unused value.
1231 void
1232 unused_value_error();
1234 // For children to call when they detect that they are in error.
1235 void
1236 set_is_error();
1238 // For children to call to report an error conveniently.
1239 void
1240 report_error(const char*);
1242 // Write a name to export data.
1243 static void
1244 export_name(Export_function_body* efb, const Named_object*);
1246 // Child class implements dumping to a dump context.
1247 virtual void
1248 do_dump_expression(Ast_dump_context*) const = 0;
1250 // Start exporting a type conversion for a constant, if needed.
1251 static bool
1252 export_constant_type(Export_function_body*, Type*);
1254 // Finish exporting a type conversion for a constant.
1255 static void
1256 finish_export_constant_type(Export_function_body*, bool);
1258 // Varargs lowering creates a slice object (unnamed compiler temp)
1259 // to contain the variable length collection of values. The enum
1260 // below tells the lowering routine whether it can mark that temp
1261 // as non-escaping or not. For general varargs calls it is not always
1262 // safe to stack-allocated the storage, but for specific cases (ex:
1263 // call to append()) it is legal.
1264 enum Slice_storage_escape_disp
1266 SLICE_STORAGE_MAY_ESCAPE,
1267 SLICE_STORAGE_DOES_NOT_ESCAPE
1270 private:
1271 // Convert to the desired statement classification, or return NULL.
1272 // This is a controlled dynamic cast.
1273 template<typename Expression_class,
1274 Expression_classification expr_classification>
1275 Expression_class*
1276 convert()
1278 return (this->classification_ == expr_classification
1279 ? static_cast<Expression_class*>(this)
1280 : NULL);
1283 template<typename Expression_class,
1284 Expression_classification expr_classification>
1285 const Expression_class*
1286 convert() const
1288 return (this->classification_ == expr_classification
1289 ? static_cast<const Expression_class*>(this)
1290 : NULL);
1293 static Expression*
1294 convert_interface_to_type(Gogo*, Type*, Expression*, Location);
1296 static Expression*
1297 import_identifier(Import_function_body*, Location);
1299 static Expression*
1300 import_expression_without_suffix(Import_expression*, Location);
1302 // The expression classification.
1303 Expression_classification classification_;
1304 // The location in the input file.
1305 Location location_;
1308 // A list of Expressions.
1310 class Expression_list
1312 public:
1313 Expression_list()
1314 : entries_()
1317 // Return whether the list is empty.
1318 bool
1319 empty() const
1320 { return this->entries_.empty(); }
1322 // Return the number of entries in the list.
1323 size_t
1324 size() const
1325 { return this->entries_.size(); }
1327 // Add an entry to the end of the list.
1328 void
1329 push_back(Expression* expr)
1330 { this->entries_.push_back(expr); }
1332 void
1333 append(Expression_list* add)
1334 { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
1336 // Reserve space in the list.
1337 void
1338 reserve(size_t size)
1339 { this->entries_.reserve(size); }
1341 // Traverse the expressions in the list.
1343 traverse(Traverse*);
1345 // Copy the list.
1346 Expression_list*
1347 copy();
1349 // Return true if the list contains an error expression.
1350 bool
1351 contains_error() const;
1353 // Retrieve an element by index.
1354 Expression*&
1355 at(size_t i)
1356 { return this->entries_.at(i); }
1358 // Return the first and last elements.
1359 Expression*&
1360 front()
1361 { return this->entries_.front(); }
1363 Expression*
1364 front() const
1365 { return this->entries_.front(); }
1367 Expression*&
1368 back()
1369 { return this->entries_.back(); }
1371 Expression*
1372 back() const
1373 { return this->entries_.back(); }
1375 // Iterators.
1377 typedef std::vector<Expression*>::iterator iterator;
1378 typedef std::vector<Expression*>::const_iterator const_iterator;
1380 iterator
1381 begin()
1382 { return this->entries_.begin(); }
1384 const_iterator
1385 begin() const
1386 { return this->entries_.begin(); }
1388 iterator
1389 end()
1390 { return this->entries_.end(); }
1392 const_iterator
1393 end() const
1394 { return this->entries_.end(); }
1396 // Erase an entry.
1397 void
1398 erase(iterator p)
1399 { this->entries_.erase(p); }
1401 private:
1402 std::vector<Expression*> entries_;
1405 // An abstract base class for an expression which is only used by the
1406 // parser, and is lowered in the lowering pass.
1408 class Parser_expression : public Expression
1410 public:
1411 Parser_expression(Expression_classification classification,
1412 Location location)
1413 : Expression(classification, location)
1416 protected:
1417 virtual Expression*
1418 do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
1420 Type*
1421 do_type();
1423 void
1424 do_determine_type(const Type_context*)
1425 { go_unreachable(); }
1427 void
1428 do_check_types(Gogo*)
1429 { go_unreachable(); }
1431 Bexpression*
1432 do_get_backend(Translate_context*)
1433 { go_unreachable(); }
1436 // An expression which is simply a variable.
1438 class Var_expression : public Expression
1440 public:
1441 Var_expression(Named_object* variable, Location location)
1442 : Expression(EXPRESSION_VAR_REFERENCE, location),
1443 variable_(variable)
1446 // Return the variable.
1447 Named_object*
1448 named_object() const
1449 { return this->variable_; }
1451 protected:
1452 Expression*
1453 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1455 Type*
1456 do_type();
1458 void
1459 do_determine_type(const Type_context*);
1461 Expression*
1462 do_copy()
1463 { return this; }
1466 do_inlining_cost() const
1467 { return 1; }
1469 void
1470 do_export(Export_function_body*) const;
1472 bool
1473 do_is_addressable() const
1474 { return true; }
1476 void
1477 do_address_taken(bool);
1479 Bexpression*
1480 do_get_backend(Translate_context*);
1482 void
1483 do_dump_expression(Ast_dump_context*) const;
1485 private:
1486 // The variable we are referencing.
1487 Named_object* variable_;
1490 // A reference to a variable within an enclosing function.
1492 class Enclosed_var_expression : public Expression
1494 public:
1495 Enclosed_var_expression(Expression* reference, Named_object* variable,
1496 Location location)
1497 : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE, location),
1498 reference_(reference), variable_(variable)
1501 // The reference to the enclosed variable. This will be an indirection of the
1502 // the field stored within closure variable.
1503 Expression*
1504 reference() const
1505 { return this->reference_; }
1507 // The variable being enclosed and referenced.
1508 Named_object*
1509 variable() const
1510 { return this->variable_; }
1512 protected:
1514 do_traverse(Traverse*);
1516 Expression*
1517 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1519 Expression*
1520 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1522 Type*
1523 do_type()
1524 { return this->reference_->type(); }
1526 void
1527 do_determine_type(const Type_context* context)
1528 { return this->reference_->determine_type(context); }
1530 Expression*
1531 do_copy()
1532 { return this; }
1534 bool
1535 do_is_addressable() const
1536 { return this->reference_->is_addressable(); }
1538 void
1539 do_address_taken(bool escapes);
1541 Bexpression*
1542 do_get_backend(Translate_context* context)
1543 { return this->reference_->get_backend(context); }
1545 void
1546 do_dump_expression(Ast_dump_context*) const;
1548 private:
1549 // The reference to the enclosed variable.
1550 Expression* reference_;
1551 // The variable being enclosed.
1552 Named_object* variable_;
1555 // A reference to a temporary variable.
1557 class Temporary_reference_expression : public Expression
1559 public:
1560 Temporary_reference_expression(Temporary_statement* statement,
1561 Location location)
1562 : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1563 statement_(statement), is_lvalue_(false)
1566 // The temporary that this expression refers to.
1567 Temporary_statement*
1568 statement() const
1569 { return this->statement_; }
1571 // Indicate that this reference appears on the left hand side of an
1572 // assignment statement.
1573 void
1574 set_is_lvalue()
1575 { this->is_lvalue_ = true; }
1577 static Expression*
1578 do_import(Import_function_body*, Location);
1580 protected:
1581 Type*
1582 do_type();
1584 void
1585 do_determine_type(const Type_context*)
1588 Expression*
1589 do_copy()
1590 { return make_temporary_reference(this->statement_, this->location()); }
1593 do_inlining_cost() const
1594 { return 1; }
1596 void
1597 do_export(Export_function_body*) const;
1599 bool
1600 do_is_addressable() const
1601 { return true; }
1603 void
1604 do_address_taken(bool);
1606 Bexpression*
1607 do_get_backend(Translate_context*);
1609 void
1610 do_dump_expression(Ast_dump_context*) const;
1612 private:
1613 // The statement where the temporary variable is defined.
1614 Temporary_statement* statement_;
1615 // Whether this reference appears on the left hand side of an
1616 // assignment statement.
1617 bool is_lvalue_;
1620 // Set and use a temporary variable.
1622 class Set_and_use_temporary_expression : public Expression
1624 public:
1625 Set_and_use_temporary_expression(Temporary_statement* statement,
1626 Expression* expr, Location location)
1627 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1628 statement_(statement), expr_(expr)
1631 // Return the temporary.
1632 Temporary_statement*
1633 temporary() const
1634 { return this->statement_; }
1636 // Return the expression.
1637 Expression*
1638 expression() const
1639 { return this->expr_; }
1641 protected:
1643 do_traverse(Traverse* traverse)
1644 { return Expression::traverse(&this->expr_, traverse); }
1646 Type*
1647 do_type();
1649 void
1650 do_determine_type(const Type_context*);
1652 Expression*
1653 do_copy()
1655 return make_set_and_use_temporary(this->statement_, this->expr_,
1656 this->location());
1659 bool
1660 do_must_eval_in_order() const
1661 { return true; }
1663 bool
1664 do_is_addressable() const
1665 { return true; }
1667 void
1668 do_address_taken(bool);
1670 Bexpression*
1671 do_get_backend(Translate_context*);
1673 void
1674 do_dump_expression(Ast_dump_context*) const;
1676 private:
1677 // The statement where the temporary variable is defined.
1678 Temporary_statement* statement_;
1679 // The expression to assign to the temporary.
1680 Expression* expr_;
1683 // A string expression.
1685 class String_expression : public Expression
1687 public:
1688 String_expression(const std::string& val, Type* type, Location location)
1689 : Expression(EXPRESSION_STRING, location),
1690 val_(val), type_(type)
1693 const std::string&
1694 val() const
1695 { return this->val_; }
1697 static Expression*
1698 do_import(Import_expression*, Location);
1700 protected:
1702 do_traverse(Traverse*);
1704 bool
1705 do_is_constant() const
1706 { return true; }
1708 bool
1709 do_is_zero_value() const
1710 { return this->val_ == ""; }
1712 bool
1713 do_is_static_initializer() const
1714 { return true; }
1716 bool
1717 do_string_constant_value(std::string* val) const
1719 *val = this->val_;
1720 return true;
1723 Type*
1724 do_type();
1726 void
1727 do_determine_type(const Type_context*);
1729 Expression*
1730 do_copy()
1731 { return this; }
1733 Bexpression*
1734 do_get_backend(Translate_context*);
1736 // Write string literal to a string dump.
1737 static void
1738 export_string(String_dump* exp, const String_expression* str);
1740 // Set the inlining cost a bit high since inlining may cause
1741 // duplicated string literals.
1743 do_inlining_cost() const
1744 { return 5; }
1746 void
1747 do_export(Export_function_body*) const;
1749 void
1750 do_dump_expression(Ast_dump_context*) const;
1752 private:
1753 // The string value. This is immutable.
1754 const std::string val_;
1755 // The type as determined by context.
1756 Type* type_;
1759 // A type conversion expression.
1761 class Type_conversion_expression : public Expression
1763 public:
1764 Type_conversion_expression(Type* type, Expression* expr,
1765 Location location)
1766 : Expression(EXPRESSION_CONVERSION, location),
1767 type_(type), expr_(expr), may_convert_function_types_(false),
1768 no_copy_(false), no_escape_(false)
1771 // Return the type to which we are converting.
1772 Type*
1773 type() const
1774 { return this->type_; }
1776 // Return the expression which we are converting.
1777 Expression*
1778 expr() const
1779 { return this->expr_; }
1781 // Permit converting from one function type to another. This is
1782 // used internally for method expressions.
1783 void
1784 set_may_convert_function_types()
1786 this->may_convert_function_types_ = true;
1789 // Mark string([]byte) conversion to reuse the backing store
1790 // without copying.
1791 void
1792 set_no_copy(bool b)
1793 { this->no_copy_ = b; };
1795 // Import a type conversion expression.
1796 static Expression*
1797 do_import(Import_expression*, Location);
1799 protected:
1801 do_traverse(Traverse* traverse);
1803 Expression*
1804 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1806 Expression*
1807 do_flatten(Gogo*, Named_object*, Statement_inserter*);
1809 bool
1810 do_is_constant() const;
1812 bool
1813 do_is_zero_value() const;
1815 bool
1816 do_is_static_initializer() const;
1818 bool
1819 do_numeric_constant_value(Numeric_constant*) const;
1821 bool
1822 do_string_constant_value(std::string*) const;
1824 bool
1825 do_boolean_constant_value(bool*) const;
1827 Type*
1828 do_type()
1829 { return this->type_; }
1831 void
1832 do_determine_type(const Type_context*);
1834 void
1835 do_check_types(Gogo*);
1837 Expression*
1838 do_copy();
1840 Bexpression*
1841 do_get_backend(Translate_context* context);
1844 do_inlining_cost() const;
1846 void
1847 do_export(Export_function_body*) const;
1849 void
1850 do_dump_expression(Ast_dump_context*) const;
1852 private:
1853 // The type to convert to.
1854 Type* type_;
1855 // The expression to convert.
1856 Expression* expr_;
1857 // True if this is permitted to convert function types. This is
1858 // used internally for method expressions.
1859 bool may_convert_function_types_;
1860 // True if a string([]byte) conversion can reuse the backing store
1861 // without copying. Only used in string([]byte) conversion.
1862 bool no_copy_;
1863 // True if a conversion does not escape. Used in type-to-interface
1864 // conversions and slice-to/from-string conversions.
1865 bool no_escape_;
1868 // An unsafe type conversion, used to pass values to builtin functions.
1870 class Unsafe_type_conversion_expression : public Expression
1872 public:
1873 Unsafe_type_conversion_expression(Type* type, Expression* expr,
1874 Location location)
1875 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
1876 type_(type), expr_(expr)
1879 Expression*
1880 expr() const
1881 { return this->expr_; }
1883 protected:
1885 do_traverse(Traverse* traverse);
1887 bool
1888 do_is_zero_value() const
1889 { return this->expr_->is_zero_value(); }
1891 bool
1892 do_is_static_initializer() const;
1894 Type*
1895 do_type()
1896 { return this->type_; }
1898 void
1899 do_determine_type(const Type_context*)
1900 { this->expr_->determine_type_no_context(); }
1902 Expression*
1903 do_copy();
1905 Bexpression*
1906 do_get_backend(Translate_context*);
1908 void
1909 do_dump_expression(Ast_dump_context*) const;
1911 private:
1912 // The type to convert to.
1913 Type* type_;
1914 // The expression to convert.
1915 Expression* expr_;
1918 // A Unary expression.
1920 class Unary_expression : public Expression
1922 public:
1923 Unary_expression(Operator op, Expression* expr, Location location)
1924 : Expression(EXPRESSION_UNARY, location),
1925 op_(op), escapes_(true), create_temp_(false), is_gc_root_(false),
1926 is_slice_init_(false), expr_(expr),
1927 issue_nil_check_(NIL_CHECK_DEFAULT)
1930 // Return the operator.
1931 Operator
1932 op() const
1933 { return this->op_; }
1935 // Return the operand.
1936 Expression*
1937 operand() const
1938 { return this->expr_; }
1940 // Record that an address expression does not escape.
1941 void
1942 set_does_not_escape()
1944 go_assert(this->op_ == OPERATOR_AND);
1945 this->escapes_ = false;
1948 // Record that this is an address expression which should create a
1949 // temporary variable if necessary. This is used for method calls.
1950 void
1951 set_create_temp()
1953 go_assert(this->op_ == OPERATOR_AND);
1954 this->create_temp_ = true;
1957 // Record that this is an address expression of a GC root, which is a
1958 // mutable composite literal. This used for registering GC variables.
1959 void
1960 set_is_gc_root()
1962 go_assert(this->op_ == OPERATOR_AND);
1963 this->is_gc_root_ = true;
1966 // Record that this is an address expression of a slice value initializer,
1967 // which is mutable if the values are not copied to the heap.
1968 void
1969 set_is_slice_init()
1971 go_assert(this->op_ == OPERATOR_AND);
1972 this->is_slice_init_ = true;
1975 // Call the address_taken method on the operand if necessary.
1976 void
1977 check_operand_address_taken(Gogo*);
1979 // Apply unary opcode OP to UNC, setting NC. Return true if this
1980 // could be done, false if not. On overflow, issues an error and
1981 // sets *ISSUED_ERROR.
1982 static bool
1983 eval_constant(Operator op, const Numeric_constant* unc,
1984 Location, Numeric_constant* nc, bool *issued_error);
1986 static Expression*
1987 do_import(Import_expression*, Location);
1989 // Declare that this deref does or does not require an explicit nil check.
1990 void
1991 set_requires_nil_check(bool needed)
1993 go_assert(this->op_ == OPERATOR_MULT);
1994 if (needed)
1995 this->issue_nil_check_ = NIL_CHECK_NEEDED;
1996 else
1997 this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED;
2000 protected:
2002 do_traverse(Traverse* traverse)
2003 { return Expression::traverse(&this->expr_, traverse); }
2005 Expression*
2006 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2008 Expression*
2009 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2011 bool
2012 do_is_constant() const;
2014 bool
2015 do_is_static_initializer() const;
2017 bool
2018 do_numeric_constant_value(Numeric_constant*) const;
2020 bool
2021 do_boolean_constant_value(bool*) const;
2023 Type*
2024 do_type();
2026 void
2027 do_determine_type(const Type_context*);
2029 void
2030 do_check_types(Gogo*);
2032 Expression*
2033 do_copy()
2035 return Expression::make_unary(this->op_, this->expr_->copy(),
2036 this->location());
2039 bool
2040 do_must_eval_subexpressions_in_order(int*) const
2041 { return this->op_ == OPERATOR_MULT; }
2043 bool
2044 do_is_addressable() const
2045 { return this->op_ == OPERATOR_MULT; }
2047 Bexpression*
2048 do_get_backend(Translate_context*);
2051 do_inlining_cost() const
2052 { return 1; }
2054 void
2055 do_export(Export_function_body*) const;
2057 void
2058 do_dump_expression(Ast_dump_context*) const;
2060 void
2061 do_issue_nil_check()
2063 if (this->op_ == OPERATOR_MULT)
2064 this->set_requires_nil_check(true);
2067 private:
2068 static bool
2069 base_is_static_initializer(Expression*);
2071 // Return a determination as to whether this dereference expression
2072 // requires a nil check.
2073 Nil_check_classification
2074 requires_nil_check(Gogo*);
2076 // The unary operator to apply.
2077 Operator op_;
2078 // Normally true. False if this is an address expression which does
2079 // not escape the current function.
2080 bool escapes_;
2081 // True if this is an address expression which should create a
2082 // temporary variable if necessary.
2083 bool create_temp_;
2084 // True if this is an address expression for a GC root. A GC root is a
2085 // special struct composite literal that is mutable when addressed, meaning
2086 // it cannot be represented as an immutable_struct in the backend.
2087 bool is_gc_root_;
2088 // True if this is an address expression for a slice value with an immutable
2089 // initializer. The initializer for a slice's value pointer has an array
2090 // type, meaning it cannot be represented as an immutable_struct in the
2091 // backend.
2092 bool is_slice_init_;
2093 // The operand.
2094 Expression* expr_;
2095 // Whether or not to issue a nil check for this expression if its address
2096 // is being taken.
2097 Nil_check_classification issue_nil_check_;
2100 // A binary expression.
2102 class Binary_expression : public Expression
2104 public:
2105 Binary_expression(Operator op, Expression* left, Expression* right,
2106 Location location)
2107 : Expression(EXPRESSION_BINARY, location),
2108 op_(op), left_(left), right_(right), type_(NULL)
2111 // Return the operator.
2112 Operator
2113 op()
2114 { return this->op_; }
2116 // Return the left hand expression.
2117 Expression*
2118 left()
2119 { return this->left_; }
2121 // Return the right hand expression.
2122 Expression*
2123 right()
2124 { return this->right_; }
2126 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
2127 // Return true if this could be done, false if not. Issue errors at
2128 // LOCATION as appropriate, and sets *ISSUED_ERROR if it did.
2129 static bool
2130 eval_constant(Operator op, Numeric_constant* left_nc,
2131 Numeric_constant* right_nc, Location location,
2132 Numeric_constant* nc, bool* issued_error);
2134 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
2135 // *RESULT. Return true if this could be done, false if not. Issue
2136 // errors at LOCATION as appropriate.
2137 static bool
2138 compare_constant(Operator op, Numeric_constant* left_nc,
2139 Numeric_constant* right_nc, Location location,
2140 bool* result);
2142 static Expression*
2143 do_import(Import_expression*, Location);
2145 // Report an error if OP can not be applied to TYPE. Return whether
2146 // it can. OTYPE is the type of the other operand.
2147 static bool
2148 check_operator_type(Operator op, Type* type, Type* otype, Location);
2150 // Set *RESULT_TYPE to the resulting type when OP is applied to
2151 // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
2152 // success, false on failure.
2153 static bool
2154 operation_type(Operator op, Type* left_type, Type* right_type,
2155 Type** result_type);
2157 protected:
2159 do_traverse(Traverse* traverse);
2161 Expression*
2162 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2164 Expression*
2165 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2167 bool
2168 do_is_constant() const
2169 { return this->left_->is_constant() && this->right_->is_constant(); }
2171 bool
2172 do_is_static_initializer() const;
2174 bool
2175 do_numeric_constant_value(Numeric_constant*) const;
2177 bool
2178 do_boolean_constant_value(bool*) const;
2180 bool
2181 do_discarding_value();
2183 Type*
2184 do_type();
2186 void
2187 do_determine_type(const Type_context*);
2189 void
2190 do_check_types(Gogo*);
2192 Expression*
2193 do_copy()
2195 return Expression::make_binary(this->op_, this->left_->copy(),
2196 this->right_->copy(), this->location());
2199 Bexpression*
2200 do_get_backend(Translate_context*);
2203 do_inlining_cost() const
2204 { return 1; }
2206 void
2207 do_export(Export_function_body*) const;
2209 void
2210 do_dump_expression(Ast_dump_context*) const;
2212 private:
2213 static bool
2214 cmp_to_bool(Operator op, int cmp);
2216 static bool
2217 eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
2218 Location, Numeric_constant*);
2220 static bool
2221 eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
2222 Location, Numeric_constant*);
2224 static bool
2225 eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
2226 Location, Numeric_constant*);
2228 static bool
2229 compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
2231 static bool
2232 compare_float(const Numeric_constant*, const Numeric_constant *, int*);
2234 static bool
2235 compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
2237 Expression*
2238 lower_struct_comparison(Gogo*, Statement_inserter*);
2240 Expression*
2241 lower_array_comparison(Gogo*, Statement_inserter*);
2243 Expression*
2244 lower_interface_value_comparison(Gogo*, Statement_inserter*);
2246 Expression*
2247 lower_compare_to_memcmp(Gogo*, Statement_inserter*);
2249 Expression*
2250 operand_address(Statement_inserter*, Expression*);
2252 // The binary operator to apply.
2253 Operator op_;
2254 // The left hand side operand.
2255 Expression* left_;
2256 // The right hand side operand.
2257 Expression* right_;
2258 // The type of a comparison operation.
2259 Type* type_;
2262 // A string concatenation expression. This is a sequence of strings
2263 // added together. It is created when lowering Binary_expression.
2265 class String_concat_expression : public Expression
2267 public:
2268 String_concat_expression(Expression_list* exprs)
2269 : Expression(EXPRESSION_STRING_CONCAT, exprs->front()->location()),
2270 exprs_(exprs)
2273 // Return the list of string expressions to be concatenated.
2274 Expression_list*
2275 exprs()
2276 { return this->exprs_; }
2278 protected:
2280 do_traverse(Traverse* traverse)
2281 { return this->exprs_->traverse(traverse); }
2283 Expression*
2284 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2285 { return this; }
2287 Expression*
2288 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2290 bool
2291 do_is_constant() const;
2293 bool
2294 do_is_zero_value() const;
2296 bool
2297 do_is_static_initializer() const;
2299 Type*
2300 do_type();
2302 void
2303 do_determine_type(const Type_context*);
2305 void
2306 do_check_types(Gogo*);
2308 Expression*
2309 do_copy()
2310 { return Expression::make_string_concat(this->exprs_->copy()); }
2312 Bexpression*
2313 do_get_backend(Translate_context*)
2314 { go_unreachable(); }
2316 void
2317 do_export(Export_function_body*) const
2318 { go_unreachable(); }
2320 void
2321 do_dump_expression(Ast_dump_context*) const;
2323 private:
2324 // The string expressions to concatenate.
2325 Expression_list* exprs_;
2328 // A call expression. The go statement needs to dig inside this.
2330 class Call_expression : public Expression
2332 public:
2333 Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
2334 Location location)
2335 : Expression(EXPRESSION_CALL, location),
2336 fn_(fn), args_(args), type_(NULL), call_(NULL), call_temp_(NULL)
2337 , expected_result_count_(0), is_varargs_(is_varargs),
2338 varargs_are_lowered_(false), types_are_determined_(false),
2339 is_deferred_(false), is_concurrent_(false), is_equal_function_(false),
2340 issued_error_(false), is_multi_value_arg_(false), is_flattened_(false)
2343 // The function to call.
2344 Expression*
2345 fn() const
2346 { return this->fn_; }
2348 // The arguments.
2349 Expression_list*
2350 args()
2351 { return this->args_; }
2353 const Expression_list*
2354 args() const
2355 { return this->args_; }
2357 // Get the function type.
2358 Function_type*
2359 get_function_type() const;
2361 // Return the number of values this call will return.
2362 size_t
2363 result_count() const;
2365 // Return the temporary variable that holds the results. This is
2366 // only valid after the expression has been lowered, and is only
2367 // valid for calls which return multiple results.
2368 Temporary_statement*
2369 results() const;
2371 // Set the number of results expected from this call. This is used
2372 // when the call appears in a context that expects multiple results,
2373 // such as a, b = f().
2374 void
2375 set_expected_result_count(size_t);
2377 // Return whether this is a call to the predeclared function
2378 // recover.
2379 bool
2380 is_recover_call() const;
2382 // Set the argument for a call to recover.
2383 void
2384 set_recover_arg(Expression*);
2386 // Whether the last argument is a varargs argument (f(a...)).
2387 bool
2388 is_varargs() const
2389 { return this->is_varargs_; }
2391 // Return whether varargs have already been lowered.
2392 bool
2393 varargs_are_lowered() const
2394 { return this->varargs_are_lowered_; }
2396 // Note that varargs have already been lowered.
2397 void
2398 set_varargs_are_lowered()
2399 { this->varargs_are_lowered_ = true; }
2401 // Whether this call is being deferred.
2402 bool
2403 is_deferred() const
2404 { return this->is_deferred_; }
2406 // Note that the call is being deferred.
2407 void
2408 set_is_deferred()
2409 { this->is_deferred_ = true; }
2411 // Whether this call is concurrently executed.
2412 bool
2413 is_concurrent() const
2414 { return this->is_concurrent_; }
2416 // Note that the call is concurrently executed.
2417 void
2418 set_is_concurrent()
2419 { this->is_concurrent_ = true; }
2421 // Note that this is a call to a generated equality function.
2422 void
2423 set_is_equal_function()
2424 { this->is_equal_function_ = true; }
2426 // We have found an error with this call expression; return true if
2427 // we should report it.
2428 bool
2429 issue_error();
2431 // Whether or not this call contains errors, either in the call or the
2432 // arguments to the call.
2433 bool
2434 is_erroneous_call();
2436 // Whether this call returns multiple results that are used as an
2437 // multi-valued argument.
2438 bool
2439 is_multi_value_arg() const
2440 { return this->is_multi_value_arg_; }
2442 // Note this call is used as a multi-valued argument.
2443 void
2444 set_is_multi_value_arg()
2445 { this->is_multi_value_arg_ = true; }
2447 // Whether this is a call to builtin function.
2448 virtual bool
2449 is_builtin()
2450 { return false; }
2452 // Convert to a Builtin_call_expression, or return NULL.
2453 inline Builtin_call_expression*
2454 builtin_call_expression();
2456 protected:
2458 do_traverse(Traverse*);
2460 virtual Expression*
2461 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2463 virtual Expression*
2464 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2466 bool
2467 do_discarding_value()
2468 { return true; }
2470 virtual Type*
2471 do_type();
2473 virtual void
2474 do_determine_type(const Type_context*);
2476 virtual void
2477 do_check_types(Gogo*);
2479 Expression*
2480 do_copy();
2482 bool
2483 do_must_eval_in_order() const;
2485 virtual Bexpression*
2486 do_get_backend(Translate_context*);
2489 do_inlining_cost() const;
2491 void
2492 do_export(Export_function_body*) const;
2494 virtual bool
2495 do_is_recover_call() const;
2497 virtual void
2498 do_set_recover_arg(Expression*);
2500 // Let a builtin expression change the argument list.
2501 void
2502 set_args(Expression_list* args)
2503 { this->args_ = args; }
2505 // Let a builtin expression lower varargs.
2506 void
2507 lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
2508 Type* varargs_type, size_t param_count,
2509 Slice_storage_escape_disp escape_disp);
2511 // Let a builtin expression check whether types have been
2512 // determined.
2513 bool
2514 determining_types();
2516 void
2517 export_arguments(Export_function_body*) const;
2519 void
2520 do_dump_expression(Ast_dump_context*) const;
2522 void
2523 do_add_conversions();
2525 private:
2526 bool
2527 check_argument_type(int, const Type*, const Type*, Location, bool);
2529 Expression*
2530 intrinsify(Gogo*, Statement_inserter*);
2532 Expression*
2533 interface_method_function(Interface_field_reference_expression*,
2534 Expression**, Location);
2536 Bexpression*
2537 set_results(Translate_context*);
2539 // The function to call.
2540 Expression* fn_;
2541 // The arguments to pass. This may be NULL if there are no
2542 // arguments.
2543 Expression_list* args_;
2544 // The type of the expression, to avoid recomputing it.
2545 Type* type_;
2546 // The backend expression for the call, used for a call which returns a tuple.
2547 Bexpression* call_;
2548 // A temporary variable to store this call if the function returns a tuple.
2549 Temporary_statement* call_temp_;
2550 // If not 0, the number of results expected from this call, when
2551 // used in a context that expects multiple values.
2552 size_t expected_result_count_;
2553 // True if the last argument is a varargs argument (f(a...)).
2554 bool is_varargs_;
2555 // True if varargs have already been lowered.
2556 bool varargs_are_lowered_;
2557 // True if types have been determined.
2558 bool types_are_determined_;
2559 // True if the call is an argument to a defer statement.
2560 bool is_deferred_;
2561 // True if the call is an argument to a go statement.
2562 bool is_concurrent_;
2563 // True if this is a call to a generated equality function.
2564 bool is_equal_function_;
2565 // True if we reported an error about a mismatch between call
2566 // results and uses. This is to avoid producing multiple errors
2567 // when there are multiple Call_result_expressions.
2568 bool issued_error_;
2569 // True if this call is used as an argument that returns multiple results.
2570 bool is_multi_value_arg_;
2571 // True if this expression has already been flattened.
2572 bool is_flattened_;
2575 // A call expression to a builtin function.
2577 class Builtin_call_expression : public Call_expression
2579 public:
2580 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
2581 bool is_varargs, Location location);
2583 // The builtin functions.
2584 enum Builtin_function_code
2586 BUILTIN_INVALID,
2588 // Predeclared builtin functions.
2589 BUILTIN_APPEND,
2590 BUILTIN_CAP,
2591 BUILTIN_CLOSE,
2592 BUILTIN_COMPLEX,
2593 BUILTIN_COPY,
2594 BUILTIN_DELETE,
2595 BUILTIN_IMAG,
2596 BUILTIN_LEN,
2597 BUILTIN_MAKE,
2598 BUILTIN_NEW,
2599 BUILTIN_PANIC,
2600 BUILTIN_PRINT,
2601 BUILTIN_PRINTLN,
2602 BUILTIN_REAL,
2603 BUILTIN_RECOVER,
2605 // Builtin functions from the unsafe package.
2606 BUILTIN_ALIGNOF,
2607 BUILTIN_OFFSETOF,
2608 BUILTIN_SIZEOF
2611 Builtin_function_code
2612 code()
2613 { return this->code_; }
2615 // This overrides Call_expression::is_builtin.
2616 bool
2617 is_builtin()
2618 { return true; }
2620 // Return whether EXPR, of array type, is a constant if passed to
2621 // len or cap.
2622 static bool
2623 array_len_is_constant(Expression* expr);
2625 Expression*
2626 flatten_append(Gogo*, Named_object*, Statement_inserter*, Expression*,
2627 Block*);
2629 protected:
2630 // This overrides Call_expression::do_lower.
2631 Expression*
2632 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2634 Expression*
2635 do_flatten(Gogo*, Named_object*, Statement_inserter*);
2637 bool
2638 do_is_constant() const;
2640 bool
2641 do_numeric_constant_value(Numeric_constant*) const;
2643 bool
2644 do_discarding_value();
2646 Type*
2647 do_type();
2649 void
2650 do_determine_type(const Type_context*);
2652 void
2653 do_check_types(Gogo*);
2655 Expression*
2656 do_copy();
2658 Bexpression*
2659 do_get_backend(Translate_context*);
2662 do_inlining_cost() const
2663 { return 1; }
2665 void
2666 do_export(Export_function_body*) const;
2668 virtual bool
2669 do_is_recover_call() const;
2671 virtual void
2672 do_set_recover_arg(Expression*);
2674 private:
2675 Expression*
2676 one_arg() const;
2678 bool
2679 check_one_arg();
2681 static Type*
2682 real_imag_type(Type*);
2684 static Type*
2685 complex_type(Type*);
2687 Expression*
2688 lower_make(Statement_inserter*);
2690 bool
2691 check_int_value(Expression*, bool is_length, bool* small);
2693 // A pointer back to the general IR structure. This avoids a global
2694 // variable, or passing it around everywhere.
2695 Gogo* gogo_;
2696 // The builtin function being called.
2697 Builtin_function_code code_;
2698 // Used to stop endless loops when the length of an array uses len
2699 // or cap of the array itself.
2700 mutable bool seen_;
2701 // Whether the argument is set for calls to BUILTIN_RECOVER.
2702 bool recover_arg_is_set_;
2705 inline Builtin_call_expression*
2706 Call_expression::builtin_call_expression()
2708 return (this->is_builtin()
2709 ? static_cast<Builtin_call_expression*>(this)
2710 : NULL);
2713 // A single result from a call which returns multiple results.
2715 class Call_result_expression : public Expression
2717 public:
2718 Call_result_expression(Call_expression* call, unsigned int index)
2719 : Expression(EXPRESSION_CALL_RESULT, call->location()),
2720 call_(call), index_(index)
2723 Expression*
2724 call() const
2725 { return this->call_; }
2727 unsigned int
2728 index() const
2729 { return this->index_; }
2731 protected:
2733 do_traverse(Traverse*);
2735 Type*
2736 do_type();
2738 void
2739 do_determine_type(const Type_context*);
2741 void
2742 do_check_types(Gogo*);
2744 Expression*
2745 do_copy()
2747 return new Call_result_expression(this->call_->call_expression(),
2748 this->index_);
2751 bool
2752 do_must_eval_in_order() const
2753 { return true; }
2755 Bexpression*
2756 do_get_backend(Translate_context*);
2758 void
2759 do_dump_expression(Ast_dump_context*) const;
2761 private:
2762 // The underlying call expression.
2763 Expression* call_;
2764 // Which result we want.
2765 unsigned int index_;
2768 // An expression which represents a pointer to a function.
2770 class Func_expression : public Expression
2772 public:
2773 Func_expression(Named_object* function, Expression* closure,
2774 Location location)
2775 : Expression(EXPRESSION_FUNC_REFERENCE, location),
2776 function_(function), closure_(closure),
2777 runtime_code_(Runtime::NUMBER_OF_FUNCTIONS)
2780 // Return the object associated with the function.
2781 Named_object*
2782 named_object() const
2783 { return this->function_; }
2785 // Return the closure for this function. This will return NULL if
2786 // the function has no closure, which is the normal case.
2787 Expression*
2788 closure()
2789 { return this->closure_; }
2791 // Return whether this is a reference to a runtime function.
2792 bool
2793 is_runtime_function() const
2794 { return this->runtime_code_ != Runtime::NUMBER_OF_FUNCTIONS; }
2796 // Return the runtime code for this function expression.
2797 // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
2798 // runtime function.
2799 Runtime::Function
2800 runtime_code() const
2801 { return this->runtime_code_; }
2803 // Set the runtime code for this function expression.
2804 void
2805 set_runtime_code(Runtime::Function code)
2806 { this->runtime_code_ = code; }
2808 // Return a backend expression for the code of a function.
2809 static Bexpression*
2810 get_code_pointer(Gogo*, Named_object* function, Location loc);
2812 protected:
2814 do_traverse(Traverse*);
2816 Type*
2817 do_type();
2819 void
2820 do_determine_type(const Type_context*)
2822 if (this->closure_ != NULL)
2823 this->closure_->determine_type_no_context();
2826 Expression*
2827 do_copy()
2829 return Expression::make_func_reference(this->function_,
2830 (this->closure_ == NULL
2831 ? NULL
2832 : this->closure_->copy()),
2833 this->location());
2836 Bexpression*
2837 do_get_backend(Translate_context*);
2840 do_inlining_cost() const;
2842 void
2843 do_export(Export_function_body*) const;
2845 void
2846 do_dump_expression(Ast_dump_context*) const;
2848 private:
2849 // The function itself.
2850 Named_object* function_;
2851 // A closure. This is normally NULL. For a nested function, it may
2852 // be a struct holding pointers to all the variables referenced by
2853 // this function and defined in enclosing functions.
2854 Expression* closure_;
2855 // The runtime code for the referenced function.
2856 Runtime::Function runtime_code_;
2859 // A function descriptor. A function descriptor is a struct with a
2860 // single field pointing to the function code. This is used for
2861 // functions without closures.
2863 class Func_descriptor_expression : public Expression
2865 public:
2866 Func_descriptor_expression(Named_object* fn);
2868 // Make the function descriptor type, so that it can be converted.
2869 static void
2870 make_func_descriptor_type();
2872 protected:
2874 do_traverse(Traverse*);
2876 Type*
2877 do_type();
2879 void
2880 do_determine_type(const Type_context*)
2883 Expression*
2884 do_copy()
2885 { return Expression::make_func_descriptor(this->fn_); }
2887 bool
2888 do_is_addressable() const
2889 { return true; }
2891 Bexpression*
2892 do_get_backend(Translate_context*);
2894 void
2895 do_dump_expression(Ast_dump_context* context) const;
2897 private:
2898 // The type of all function descriptors.
2899 static Type* descriptor_type;
2901 // The function for which this is the descriptor.
2902 Named_object* fn_;
2903 // The descriptor variable.
2904 Bvariable* dvar_;
2907 // A reference to an unknown name.
2909 class Unknown_expression : public Parser_expression
2911 public:
2912 Unknown_expression(Named_object* named_object, Location location)
2913 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
2914 named_object_(named_object), no_error_message_(false)
2917 // The associated named object.
2918 Named_object*
2919 named_object() const
2920 { return this->named_object_; }
2922 // The name of the identifier which was unknown.
2923 const std::string&
2924 name() const;
2926 // Call this to indicate that we should not give an error if this
2927 // name is never defined. This is used to avoid knock-on errors
2928 // during an erroneous parse.
2929 void
2930 set_no_error_message()
2931 { this->no_error_message_ = true; }
2933 protected:
2934 Expression*
2935 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2937 Expression*
2938 do_copy()
2939 { return new Unknown_expression(this->named_object_, this->location()); }
2941 void
2942 do_dump_expression(Ast_dump_context*) const;
2944 private:
2945 // The unknown name.
2946 Named_object* named_object_;
2947 // True if we should not give errors if this is undefined. This is
2948 // used if there was a parse failure.
2949 bool no_error_message_;
2952 // An index expression. This is lowered to an array index, a string
2953 // index, or a map index.
2955 class Index_expression : public Parser_expression
2957 public:
2958 Index_expression(Expression* left, Expression* start, Expression* end,
2959 Expression* cap, Location location)
2960 : Parser_expression(EXPRESSION_INDEX, location),
2961 left_(left), start_(start), end_(end), cap_(cap)
2964 // Dump an index expression, i.e. an expression of the form
2965 // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
2966 static void
2967 dump_index_expression(Ast_dump_context*, const Expression* expr,
2968 const Expression* start, const Expression* end,
2969 const Expression* cap);
2971 protected:
2973 do_traverse(Traverse*);
2975 Expression*
2976 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2978 Expression*
2979 do_copy()
2981 return new Index_expression(this->left_->copy(), this->start_->copy(),
2982 (this->end_ == NULL
2983 ? NULL
2984 : this->end_->copy()),
2985 (this->cap_ == NULL
2986 ? NULL
2987 : this->cap_->copy()),
2988 this->location());
2991 // This shouldn't be called--we don't know yet.
2992 bool
2993 do_must_eval_subexpressions_in_order(int*) const
2994 { go_unreachable(); }
2996 void
2997 do_dump_expression(Ast_dump_context*) const;
2999 void
3000 do_issue_nil_check()
3001 { this->left_->issue_nil_check(); }
3002 private:
3003 // The expression being indexed.
3004 Expression* left_;
3005 // The first index.
3006 Expression* start_;
3007 // The second index. This is NULL for an index, non-NULL for a
3008 // slice.
3009 Expression* end_;
3010 // The capacity argument. This is NULL for indices and slices that use the
3011 // default capacity, non-NULL for indices and slices that specify the
3012 // capacity.
3013 Expression* cap_;
3016 // An array index. This is used for both indexing and slicing.
3018 class Array_index_expression : public Expression
3020 public:
3021 Array_index_expression(Expression* array, Expression* start,
3022 Expression* end, Expression* cap, Location location)
3023 : Expression(EXPRESSION_ARRAY_INDEX, location),
3024 array_(array), start_(start), end_(end), cap_(cap), type_(NULL),
3025 is_lvalue_(false), needs_bounds_check_(true), is_flattened_(false)
3028 // Return the array.
3029 Expression*
3030 array()
3031 { return this->array_; }
3033 const Expression*
3034 array() const
3035 { return this->array_; }
3037 // Return the index of a simple index expression, or the start index
3038 // of a slice expression.
3039 Expression*
3040 start()
3041 { return this->start_; }
3043 const Expression*
3044 start() const
3045 { return this->start_; }
3047 // Return the end index of a slice expression. This is NULL for a
3048 // simple index expression.
3049 Expression*
3050 end()
3051 { return this->end_; }
3053 const Expression*
3054 end() const
3055 { return this->end_; }
3057 // Return whether this array index expression appears in an lvalue
3058 // (left hand side of assignment) context.
3059 bool
3060 is_lvalue() const
3061 { return this->is_lvalue_; }
3063 // Update this array index expression to indicate that it appears
3064 // in a left-hand-side or lvalue context.
3065 void
3066 set_is_lvalue()
3067 { this->is_lvalue_ = true; }
3069 void
3070 set_needs_bounds_check(bool b)
3071 { this->needs_bounds_check_ = b; }
3073 protected:
3075 do_traverse(Traverse*);
3077 Expression*
3078 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3080 Type*
3081 do_type();
3083 void
3084 do_determine_type(const Type_context*);
3086 void
3087 do_check_types(Gogo*);
3089 Expression*
3090 do_copy()
3092 Expression* ret = Expression::make_array_index(this->array_->copy(),
3093 this->start_->copy(),
3094 (this->end_ == NULL
3095 ? NULL
3096 : this->end_->copy()),
3097 (this->cap_ == NULL
3098 ? NULL
3099 : this->cap_->copy()),
3100 this->location());
3101 ret->array_index_expression()->set_needs_bounds_check(this->needs_bounds_check_);
3102 return ret;
3105 bool
3106 do_must_eval_subexpressions_in_order(int* skip) const;
3108 bool
3109 do_is_addressable() const;
3111 void
3112 do_address_taken(bool escapes);
3114 void
3115 do_issue_nil_check()
3116 { this->array_->issue_nil_check(); }
3118 Bexpression*
3119 do_get_backend(Translate_context*);
3122 do_inlining_cost() const
3123 { return this->end_ != NULL ? 2 : 1; }
3125 void
3126 do_export(Export_function_body*) const;
3128 void
3129 do_dump_expression(Ast_dump_context*) const;
3131 private:
3132 // The array we are getting a value from.
3133 Expression* array_;
3134 // The start or only index.
3135 Expression* start_;
3136 // The end index of a slice. This may be NULL for a simple array
3137 // index, or it may be a nil expression for the length of the array.
3138 Expression* end_;
3139 // The capacity argument of a slice. This may be NULL for an array index or
3140 // slice.
3141 Expression* cap_;
3142 // The type of the expression.
3143 Type* type_;
3144 // Whether expr appears in an lvalue context.
3145 bool is_lvalue_;
3146 // Whether bounds check is needed.
3147 bool needs_bounds_check_;
3148 // Whether this has already been flattened.
3149 bool is_flattened_;
3152 // A string index. This is used for both indexing and slicing.
3154 class String_index_expression : public Expression
3156 public:
3157 String_index_expression(Expression* string, Expression* start,
3158 Expression* end, Location location)
3159 : Expression(EXPRESSION_STRING_INDEX, location),
3160 string_(string), start_(start), end_(end), is_flattened_(false)
3163 // Return the string being indexed.
3164 Expression*
3165 string() const
3166 { return this->string_; }
3168 // Return the index of a simple index expression, or the start index
3169 // of a slice expression.
3170 Expression*
3171 start() const
3172 { return this->start_; }
3174 // Return the end index of a slice expression. This is NULL for a
3175 // simple index expression.
3176 Expression*
3177 end() const
3178 { return this->end_; }
3180 protected:
3182 do_traverse(Traverse*);
3184 Expression*
3185 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3187 Type*
3188 do_type();
3190 void
3191 do_determine_type(const Type_context*);
3193 void
3194 do_check_types(Gogo*);
3196 Expression*
3197 do_copy()
3199 return Expression::make_string_index(this->string_->copy(),
3200 this->start_->copy(),
3201 (this->end_ == NULL
3202 ? NULL
3203 : this->end_->copy()),
3204 this->location());
3207 bool
3208 do_must_eval_subexpressions_in_order(int*) const
3209 { return true; }
3211 Bexpression*
3212 do_get_backend(Translate_context*);
3215 do_inlining_cost() const
3216 { return this->end_ != NULL ? 2 : 1; }
3218 void
3219 do_export(Export_function_body*) const;
3221 void
3222 do_dump_expression(Ast_dump_context*) const;
3224 private:
3225 // The string we are getting a value from.
3226 Expression* string_;
3227 // The start or only index.
3228 Expression* start_;
3229 // The end index of a slice. This may be NULL for a single index,
3230 // or it may be a nil expression for the length of the string.
3231 Expression* end_;
3232 // Whether this has already been flattened.
3233 bool is_flattened_;
3236 // An index into a map.
3238 class Map_index_expression : public Expression
3240 public:
3241 Map_index_expression(Expression* map, Expression* index,
3242 Location location)
3243 : Expression(EXPRESSION_MAP_INDEX, location),
3244 map_(map), index_(index), value_pointer_(NULL)
3247 // Return the map.
3248 Expression*
3249 map()
3250 { return this->map_; }
3252 const Expression*
3253 map() const
3254 { return this->map_; }
3256 // Return the index.
3257 Expression*
3258 index()
3259 { return this->index_; }
3261 const Expression*
3262 index() const
3263 { return this->index_; }
3265 // Get the type of the map being indexed.
3266 Map_type*
3267 get_map_type() const;
3269 // Return an expression for the map index. This returns an
3270 // expression that evaluates to a pointer to a value in the map. If
3271 // the key is not present in the map, this will return a pointer to
3272 // the zero value.
3273 Expression*
3274 get_value_pointer(Gogo*);
3276 protected:
3278 do_traverse(Traverse*);
3280 Expression*
3281 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3283 Type*
3284 do_type();
3286 void
3287 do_determine_type(const Type_context*);
3289 void
3290 do_check_types(Gogo*);
3292 Expression*
3293 do_copy()
3295 return Expression::make_map_index(this->map_->copy(),
3296 this->index_->copy(),
3297 this->location());
3300 bool
3301 do_must_eval_subexpressions_in_order(int*) const
3302 { return true; }
3304 // A map index expression is an lvalue but it is not addressable.
3306 Bexpression*
3307 do_get_backend(Translate_context*);
3310 do_inlining_cost() const
3311 { return 5; }
3313 void
3314 do_export(Export_function_body*) const;
3316 void
3317 do_dump_expression(Ast_dump_context*) const;
3319 void
3320 do_add_conversions();
3322 private:
3323 // The map we are looking into.
3324 Expression* map_;
3325 // The index.
3326 Expression* index_;
3327 // A pointer to the value at this index.
3328 Expression* value_pointer_;
3331 // An expression which represents a method bound to its first
3332 // argument.
3334 class Bound_method_expression : public Expression
3336 public:
3337 Bound_method_expression(Expression* expr, const Method *method,
3338 Named_object* function, Location location)
3339 : Expression(EXPRESSION_BOUND_METHOD, location),
3340 expr_(expr), expr_type_(NULL), method_(method), function_(function)
3343 // Return the object which is the first argument.
3344 Expression*
3345 first_argument()
3346 { return this->expr_; }
3348 // Return the implicit type of the first argument. This will be
3349 // non-NULL when using a method from an anonymous field without
3350 // using an explicit stub.
3351 Type*
3352 first_argument_type() const
3353 { return this->expr_type_; }
3355 // Return the method.
3356 const Method*
3357 method() const
3358 { return this->method_; }
3360 // Return the function to call.
3361 Named_object*
3362 function() const
3363 { return this->function_; }
3365 // Set the implicit type of the expression.
3366 void
3367 set_first_argument_type(Type* type)
3368 { this->expr_type_ = type; }
3370 // Create a thunk to call FUNCTION, for METHOD, when it is used as
3371 // part of a method value.
3372 static Named_object*
3373 create_thunk(Gogo*, const Method* method, Named_object* function);
3375 protected:
3377 do_traverse(Traverse*);
3379 Expression*
3380 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3382 Type*
3383 do_type();
3385 void
3386 do_determine_type(const Type_context*);
3388 void
3389 do_check_types(Gogo*);
3391 Expression*
3392 do_copy()
3394 return new Bound_method_expression(this->expr_->copy(), this->method_,
3395 this->function_, this->location());
3398 Bexpression*
3399 do_get_backend(Translate_context*)
3400 { go_unreachable(); }
3402 void
3403 do_dump_expression(Ast_dump_context*) const;
3405 private:
3406 // A mapping from method functions to the thunks we have created for
3407 // them.
3408 typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
3409 static Method_value_thunks method_value_thunks;
3411 // The object used to find the method. This is passed to the method
3412 // as the first argument.
3413 Expression* expr_;
3414 // The implicit type of the object to pass to the method. This is
3415 // NULL in the normal case, non-NULL when using a method from an
3416 // anonymous field which does not require a stub.
3417 Type* expr_type_;
3418 // The method.
3419 const Method* method_;
3420 // The function to call. This is not the same as
3421 // method_->named_object() when the method has a stub. This will be
3422 // the real function rather than the stub.
3423 Named_object* function_;
3426 // A reference to a field in a struct.
3428 class Field_reference_expression : public Expression
3430 public:
3431 Field_reference_expression(Expression* expr, unsigned int field_index,
3432 Location location)
3433 : Expression(EXPRESSION_FIELD_REFERENCE, location),
3434 expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
3437 // Return the struct expression.
3438 Expression*
3439 expr() const
3440 { return this->expr_; }
3442 // Return the field index.
3443 unsigned int
3444 field_index() const
3445 { return this->field_index_; }
3447 // Return whether this node was implied by an anonymous field.
3448 bool
3449 implicit() const
3450 { return this->implicit_; }
3452 void
3453 set_implicit(bool implicit)
3454 { this->implicit_ = implicit; }
3456 // Set the struct expression. This is used when parsing.
3457 void
3458 set_struct_expression(Expression* expr)
3460 go_assert(this->expr_ == NULL);
3461 this->expr_ = expr;
3464 protected:
3466 do_traverse(Traverse* traverse)
3467 { return Expression::traverse(&this->expr_, traverse); }
3469 Expression*
3470 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3472 Type*
3473 do_type();
3475 void
3476 do_determine_type(const Type_context*)
3477 { this->expr_->determine_type_no_context(); }
3479 void
3480 do_check_types(Gogo*);
3482 Expression*
3483 do_copy()
3485 return Expression::make_field_reference(this->expr_->copy(),
3486 this->field_index_,
3487 this->location());
3490 bool
3491 do_is_addressable() const
3492 { return this->expr_->is_addressable(); }
3494 void
3495 do_address_taken(bool escapes)
3496 { this->expr_->address_taken(escapes); }
3498 void
3499 do_issue_nil_check()
3500 { this->expr_->issue_nil_check(); }
3502 Bexpression*
3503 do_get_backend(Translate_context*);
3505 void
3506 do_dump_expression(Ast_dump_context*) const;
3508 private:
3509 // The expression we are looking into. This should have a type of
3510 // struct.
3511 Expression* expr_;
3512 // The zero-based index of the field we are retrieving.
3513 unsigned int field_index_;
3514 // Whether this node was emitted implicitly for an embedded field,
3515 // that is, expr_ is not the expr_ of the original user node.
3516 bool implicit_;
3517 // Whether we have already emitted a fieldtrack call.
3518 bool called_fieldtrack_;
3521 // A reference to a field of an interface.
3523 class Interface_field_reference_expression : public Expression
3525 public:
3526 Interface_field_reference_expression(Expression* expr,
3527 const std::string& name,
3528 Location location)
3529 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
3530 expr_(expr), name_(name)
3533 // Return the expression for the interface object.
3534 Expression*
3535 expr()
3536 { return this->expr_; }
3538 // Return the name of the method to call.
3539 const std::string&
3540 name() const
3541 { return this->name_; }
3543 // Create a thunk to call the method NAME in TYPE when it is used as
3544 // part of a method value.
3545 static Named_object*
3546 create_thunk(Gogo*, Interface_type* type, const std::string& name);
3548 // Return an expression for the pointer to the function to call.
3549 Expression*
3550 get_function();
3552 // Return an expression for the first argument to pass to the interface
3553 // function. This is the real object associated with the interface object.
3554 Expression*
3555 get_underlying_object();
3557 protected:
3559 do_traverse(Traverse* traverse);
3561 Expression*
3562 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3564 Type*
3565 do_type();
3567 void
3568 do_determine_type(const Type_context*);
3570 void
3571 do_check_types(Gogo*);
3573 Expression*
3574 do_copy()
3576 return Expression::make_interface_field_reference(this->expr_->copy(),
3577 this->name_,
3578 this->location());
3581 Bexpression*
3582 do_get_backend(Translate_context*);
3584 void
3585 do_dump_expression(Ast_dump_context*) const;
3587 private:
3588 // A mapping from interface types to a list of thunks we have
3589 // created for methods.
3590 typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
3591 typedef Unordered_map(Interface_type*, Method_thunks*)
3592 Interface_method_thunks;
3593 static Interface_method_thunks interface_method_thunks;
3595 // The expression for the interface object. This should have a type
3596 // of interface or pointer to interface.
3597 Expression* expr_;
3598 // The field we are retrieving--the name of the method.
3599 std::string name_;
3602 // Implement the builtin function new.
3604 class Allocation_expression : public Expression
3606 public:
3607 Allocation_expression(Type* type, Location location)
3608 : Expression(EXPRESSION_ALLOCATION, location),
3609 type_(type), allocate_on_stack_(false),
3610 no_zero_(false)
3613 void
3614 set_allocate_on_stack()
3615 { this->allocate_on_stack_ = true; }
3617 // Mark that the allocated memory doesn't need zeroing.
3618 void
3619 set_no_zero()
3620 { this->no_zero_ = true; }
3622 protected:
3624 do_traverse(Traverse*);
3626 Type*
3627 do_type();
3629 void
3630 do_determine_type(const Type_context*)
3633 void
3634 do_check_types(Gogo*);
3636 Expression*
3637 do_copy();
3639 Bexpression*
3640 do_get_backend(Translate_context*);
3642 void
3643 do_dump_expression(Ast_dump_context*) const;
3645 private:
3646 // The type we are allocating.
3647 Type* type_;
3648 // Whether or not this is a stack allocation.
3649 bool allocate_on_stack_;
3650 // Whether we don't need to zero the allocated memory.
3651 bool no_zero_;
3654 // A general composite literal. This is lowered to a type specific
3655 // version.
3657 class Composite_literal_expression : public Parser_expression
3659 public:
3660 Composite_literal_expression(Type* type, int depth, bool has_keys,
3661 Expression_list* vals, bool all_are_names,
3662 Location location)
3663 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
3664 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys),
3665 all_are_names_(all_are_names), key_path_(std::vector<bool>(depth))
3669 // Mark the DEPTH entry of KEY_PATH as containing a key.
3670 void
3671 update_key_path(size_t depth)
3673 go_assert(depth < this->key_path_.size());
3674 this->key_path_[depth] = true;
3677 protected:
3679 do_traverse(Traverse* traverse);
3681 Expression*
3682 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3684 Expression*
3685 do_copy();
3687 void
3688 do_dump_expression(Ast_dump_context*) const;
3690 private:
3691 Expression*
3692 lower_struct(Gogo*, Type*);
3694 Expression*
3695 lower_array(Type*);
3697 Expression*
3698 make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
3700 Expression*
3701 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
3703 // The type of the composite literal.
3704 Type* type_;
3705 // The depth within a list of composite literals within a composite
3706 // literal, when the type is omitted.
3707 int depth_;
3708 // The values to put in the composite literal.
3709 Expression_list* vals_;
3710 // If this is true, then VALS_ is a list of pairs: a key and a
3711 // value. In an array initializer, a missing key will be NULL.
3712 bool has_keys_;
3713 // If this is true, then HAS_KEYS_ is true, and every key is a
3714 // simple identifier.
3715 bool all_are_names_;
3716 // A complement to DEPTH that indicates for each level starting from 0 to
3717 // DEPTH-1 whether or not this composite literal is nested inside of key or
3718 // a value. This is used to decide which type to use when given a map literal
3719 // with omitted key types.
3720 std::vector<bool> key_path_;
3723 // Helper/mixin class for struct and array construction expressions;
3724 // encapsulates a list of values plus an optional traversal order
3725 // recording the order in which the values should be visited.
3727 class Ordered_value_list
3729 public:
3730 Ordered_value_list(Expression_list* vals)
3731 : vals_(vals), traverse_order_(NULL)
3734 Expression_list*
3735 vals() const
3736 { return this->vals_; }
3739 traverse_vals(Traverse* traverse);
3741 // Get the traversal order (may be NULL)
3742 std::vector<unsigned long>*
3743 traverse_order()
3744 { return traverse_order_; }
3746 // Set the traversal order, used to ensure that we implement the
3747 // order of evaluation rules. Takes ownership of the argument.
3748 void
3749 set_traverse_order(std::vector<unsigned long>* traverse_order)
3750 { this->traverse_order_ = traverse_order; }
3752 private:
3753 // The list of values, in order of the fields in the struct or in
3754 // order of indices in an array. A NULL value of vals_ means that
3755 // all fields/slots should be zero-initialized; a single NULL entry
3756 // in the list means that the corresponding field or array slot
3757 // should be zero-initialized.
3758 Expression_list* vals_;
3759 // If not NULL, the order in which to traverse vals_. This is used
3760 // so that we implement the order of evaluation rules correctly.
3761 std::vector<unsigned long>* traverse_order_;
3764 // Construct a struct.
3766 class Struct_construction_expression : public Expression,
3767 public Ordered_value_list
3769 public:
3770 Struct_construction_expression(Type* type, Expression_list* vals,
3771 Location location)
3772 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
3773 Ordered_value_list(vals),
3774 type_(type)
3777 // Return whether this is a constant initializer.
3778 bool
3779 is_constant_struct() const;
3781 protected:
3783 do_traverse(Traverse* traverse);
3785 bool
3786 do_is_zero_value() const;
3788 bool
3789 do_is_static_initializer() const;
3791 Type*
3792 do_type()
3793 { return this->type_; }
3795 void
3796 do_determine_type(const Type_context*);
3798 void
3799 do_check_types(Gogo*);
3801 Expression*
3802 do_copy();
3804 Expression*
3805 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3807 Bexpression*
3808 do_get_backend(Translate_context*);
3810 void
3811 do_export(Export_function_body*) const;
3813 void
3814 do_dump_expression(Ast_dump_context*) const;
3816 void
3817 do_add_conversions();
3819 private:
3820 // The type of the struct to construct.
3821 Type* type_;
3824 // Construct an array. This class is not used directly; instead we
3825 // use the child classes, Fixed_array_construction_expression and
3826 // Slice_construction_expression.
3828 class Array_construction_expression : public Expression,
3829 public Ordered_value_list
3831 protected:
3832 Array_construction_expression(Expression_classification classification,
3833 Type* type,
3834 const std::vector<unsigned long>* indexes,
3835 Expression_list* vals, Location location)
3836 : Expression(classification, location),
3837 Ordered_value_list(vals),
3838 type_(type), indexes_(indexes)
3839 { go_assert(indexes == NULL || indexes->size() == vals->size()); }
3841 public:
3842 // Return whether this is a constant initializer.
3843 bool
3844 is_constant_array() const;
3846 // Return the number of elements.
3847 size_t
3848 element_count() const
3849 { return this->vals() == NULL ? 0 : this->vals()->size(); }
3851 protected:
3852 virtual int
3853 do_traverse(Traverse* traverse);
3855 bool
3856 do_is_zero_value() const;
3858 bool
3859 do_is_static_initializer() const;
3861 Type*
3862 do_type()
3863 { return this->type_; }
3865 void
3866 do_determine_type(const Type_context*);
3868 void
3869 do_check_types(Gogo*);
3871 void
3872 do_export(Export_function_body*) const;
3874 // The indexes.
3875 const std::vector<unsigned long>*
3876 indexes()
3877 { return this->indexes_; }
3879 Expression*
3880 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3882 // Get the backend constructor for the array values.
3883 Bexpression*
3884 get_constructor(Translate_context* context, Btype* btype);
3886 void
3887 do_dump_expression(Ast_dump_context*) const;
3889 virtual void
3890 dump_slice_storage_expression(Ast_dump_context*) const { }
3892 void
3893 do_add_conversions();
3895 private:
3896 // The type of the array to construct.
3897 Type* type_;
3898 // The list of indexes into the array, one for each value. This may
3899 // be NULL, in which case the indexes start at zero and increment.
3900 const std::vector<unsigned long>* indexes_;
3903 // Construct a fixed array.
3905 class Fixed_array_construction_expression :
3906 public Array_construction_expression
3908 public:
3909 Fixed_array_construction_expression(Type* type,
3910 const std::vector<unsigned long>* indexes,
3911 Expression_list* vals, Location location);
3913 protected:
3914 Expression*
3915 do_copy();
3917 Bexpression*
3918 do_get_backend(Translate_context*);
3921 // Construct a slice.
3923 class Slice_construction_expression : public Array_construction_expression
3925 public:
3926 Slice_construction_expression(Type* type,
3927 const std::vector<unsigned long>* indexes,
3928 Expression_list* vals, Location location);
3930 Expression*
3931 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3933 // Record that the storage for this slice (e.g. vals) cannot escape,
3934 // hence it can be stack-allocated.
3935 void
3936 set_storage_does_not_escape()
3938 this->storage_escapes_ = false;
3941 protected:
3942 // Note that taking the address of a slice literal is invalid.
3945 do_traverse(Traverse* traverse);
3947 Expression*
3948 do_copy();
3950 Bexpression*
3951 do_get_backend(Translate_context*);
3953 void
3954 dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const;
3956 // Create an array value for the constructed slice. Invoked during
3957 // flattening if slice storage does not escape, otherwise invoked
3958 // later on during do_get_backend().
3959 Expression*
3960 create_array_val();
3962 private:
3963 // The type of the values in this slice.
3964 Type* valtype_;
3965 // Array value expression, optionally filled in during flattening.
3966 Expression* array_val_;
3967 // Slice storage expression, optionally filled in during flattening.
3968 Expression* slice_storage_;
3969 // Normally true. Can be set to false if we know that the resulting
3970 // storage for the slice cannot escape.
3971 bool storage_escapes_;
3974 // Construct a map.
3976 class Map_construction_expression : public Expression
3978 public:
3979 Map_construction_expression(Type* type, Expression_list* vals,
3980 Location location)
3981 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
3982 type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL)
3983 { go_assert(vals == NULL || vals->size() % 2 == 0); }
3985 Expression_list*
3986 vals() const
3987 { return this->vals_; }
3989 protected:
3991 do_traverse(Traverse* traverse);
3993 Expression*
3994 do_flatten(Gogo*, Named_object*, Statement_inserter*);
3996 Type*
3997 do_type()
3998 { return this->type_; }
4000 void
4001 do_determine_type(const Type_context*);
4003 void
4004 do_check_types(Gogo*);
4006 Expression*
4007 do_copy();
4009 Bexpression*
4010 do_get_backend(Translate_context*);
4012 void
4013 do_export(Export_function_body*) const;
4015 void
4016 do_dump_expression(Ast_dump_context*) const;
4018 void
4019 do_add_conversions();
4021 private:
4022 // The type of the map to construct.
4023 Type* type_;
4024 // The list of values.
4025 Expression_list* vals_;
4026 // The type of the key-value pair struct for each map element.
4027 Struct_type* element_type_;
4028 // A temporary reference to the variable storing the constructor initializer.
4029 Temporary_statement* constructor_temp_;
4032 // A type guard expression.
4034 class Type_guard_expression : public Expression
4036 public:
4037 Type_guard_expression(Expression* expr, Type* type, Location location)
4038 : Expression(EXPRESSION_TYPE_GUARD, location),
4039 expr_(expr), type_(type)
4042 // Return the expression to convert.
4043 Expression*
4044 expr()
4045 { return this->expr_; }
4047 // Return the type to which to convert.
4048 Type*
4049 type()
4050 { return this->type_; }
4052 protected:
4054 do_traverse(Traverse* traverse);
4056 Expression*
4057 do_flatten(Gogo*, Named_object*, Statement_inserter*);
4059 Type*
4060 do_type()
4061 { return this->type_; }
4063 void
4064 do_determine_type(const Type_context*)
4065 { this->expr_->determine_type_no_context(); }
4067 void
4068 do_check_types(Gogo*);
4070 Expression*
4071 do_copy();
4073 Bexpression*
4074 do_get_backend(Translate_context*);
4076 void
4077 do_dump_expression(Ast_dump_context*) const;
4079 private:
4080 // The expression to convert.
4081 Expression* expr_;
4082 // The type to which to convert.
4083 Type* type_;
4086 // Class Heap_expression.
4088 // When you take the address of an escaping expression, it is allocated
4089 // on the heap. This class implements that.
4091 class Heap_expression : public Expression
4093 public:
4094 Heap_expression(Expression* expr, Location location)
4095 : Expression(EXPRESSION_HEAP, location),
4096 expr_(expr), allocate_on_stack_(false)
4099 Expression*
4100 expr() const
4101 { return this->expr_; }
4103 void
4104 set_allocate_on_stack()
4105 { this->allocate_on_stack_ = true; }
4107 protected:
4109 do_traverse(Traverse* traverse)
4110 { return Expression::traverse(&this->expr_, traverse); }
4112 Type*
4113 do_type();
4114 void
4115 do_determine_type(const Type_context*)
4116 { this->expr_->determine_type_no_context(); }
4118 Expression*
4119 do_copy()
4121 return Expression::make_heap_expression(this->expr_->copy(),
4122 this->location());
4125 Bexpression*
4126 do_get_backend(Translate_context*);
4128 // We only export global objects, and the parser does not generate
4129 // this in global scope.
4130 void
4131 do_export(Export_function_body*) const
4132 { go_unreachable(); }
4134 void
4135 do_dump_expression(Ast_dump_context*) const;
4137 private:
4138 // The expression which is being put on the heap.
4139 Expression* expr_;
4140 // Whether or not this is a stack allocation.
4141 bool allocate_on_stack_;
4144 // A receive expression.
4146 class Receive_expression : public Expression
4148 public:
4149 Receive_expression(Expression* channel, Location location)
4150 : Expression(EXPRESSION_RECEIVE, location),
4151 channel_(channel), temp_receiver_(NULL)
4154 // Return the channel.
4155 Expression*
4156 channel()
4157 { return this->channel_; }
4159 static Expression*
4160 do_import(Import_expression*, Location);
4162 protected:
4164 do_traverse(Traverse* traverse)
4165 { return Expression::traverse(&this->channel_, traverse); }
4167 bool
4168 do_discarding_value()
4169 { return true; }
4171 Type*
4172 do_type();
4174 Expression*
4175 do_flatten(Gogo*, Named_object*, Statement_inserter*);
4177 void
4178 do_determine_type(const Type_context*)
4179 { this->channel_->determine_type_no_context(); }
4181 void
4182 do_check_types(Gogo*);
4184 Expression*
4185 do_copy()
4187 return Expression::make_receive(this->channel_->copy(), this->location());
4191 do_inlining_cost() const
4192 { return 1; }
4194 bool
4195 do_must_eval_in_order() const
4196 { return true; }
4198 Bexpression*
4199 do_get_backend(Translate_context*);
4201 void
4202 do_export(Export_function_body*) const;
4204 void
4205 do_dump_expression(Ast_dump_context*) const;
4207 private:
4208 // The channel from which we are receiving.
4209 Expression* channel_;
4210 // A temporary reference to the variable storing the received data.
4211 Temporary_statement* temp_receiver_;
4214 // An expression that represents a slice value: a struct with value pointer,
4215 // length, and capacity fields.
4217 class Slice_value_expression : public Expression
4219 public:
4220 Slice_value_expression(Type* type, Expression* valmem, Expression* len,
4221 Expression* cap, Location location)
4222 : Expression(EXPRESSION_SLICE_VALUE, location),
4223 type_(type), valmem_(valmem), len_(len), cap_(cap)
4226 // The memory holding the values in the slice. The type should be a
4227 // pointer to the element value of the slice.
4228 Expression*
4229 valmem() const
4230 { return this->valmem_; }
4232 protected:
4234 do_traverse(Traverse*);
4236 Type*
4237 do_type()
4238 { return this->type_; }
4240 void
4241 do_determine_type(const Type_context*)
4244 Expression*
4245 do_copy();
4247 Bexpression*
4248 do_get_backend(Translate_context* context);
4250 void
4251 do_dump_expression(Ast_dump_context*) const;
4253 private:
4254 // The type of the slice value.
4255 Type* type_;
4256 // The memory holding the values in the slice.
4257 Expression* valmem_;
4258 // The length of the slice.
4259 Expression* len_;
4260 // The capacity of the slice.
4261 Expression* cap_;
4264 // Conditional expressions.
4266 class Conditional_expression : public Expression
4268 public:
4269 Conditional_expression(Expression* cond, Expression* then_expr,
4270 Expression* else_expr, Location location)
4271 : Expression(EXPRESSION_CONDITIONAL, location),
4272 cond_(cond), then_(then_expr), else_(else_expr)
4275 Expression*
4276 condition() const
4277 { return this->cond_; }
4279 protected:
4281 do_traverse(Traverse*);
4283 Type*
4284 do_type();
4286 void
4287 do_determine_type(const Type_context*);
4289 Expression*
4290 do_copy()
4292 return new Conditional_expression(this->cond_->copy(), this->then_->copy(),
4293 this->else_->copy(), this->location());
4296 Bexpression*
4297 do_get_backend(Translate_context* context);
4299 void
4300 do_dump_expression(Ast_dump_context*) const;
4302 private:
4303 // The condition to be checked.
4304 Expression* cond_;
4305 // The expression to execute if the condition is true.
4306 Expression* then_;
4307 // The expression to execute if the condition is false.
4308 Expression* else_;
4311 // Compound expressions.
4313 class Compound_expression : public Expression
4315 public:
4316 Compound_expression(Expression* init, Expression* expr, Location location)
4317 : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr)
4320 Expression*
4321 init() const
4322 { return this->init_; }
4324 protected:
4326 do_traverse(Traverse*);
4328 Type*
4329 do_type();
4331 void
4332 do_determine_type(const Type_context*);
4334 Expression*
4335 do_copy()
4337 return new Compound_expression(this->init_->copy(), this->expr_->copy(),
4338 this->location());
4341 Bexpression*
4342 do_get_backend(Translate_context* context);
4344 void
4345 do_dump_expression(Ast_dump_context*) const;
4347 private:
4348 // The expression that is evaluated first and discarded.
4349 Expression* init_;
4350 // The expression that is evaluated and returned.
4351 Expression* expr_;
4354 // A backend expression. This is a backend expression wrapped in an
4355 // Expression, for convenience during backend generation.
4357 class Backend_expression : public Expression
4359 public:
4360 Backend_expression(Bexpression* bexpr, Type* type, Location location)
4361 : Expression(EXPRESSION_BACKEND, location), bexpr_(bexpr), type_(type)
4364 protected:
4366 do_traverse(Traverse*);
4368 // For now these are always valid static initializers. If that
4369 // changes we can change this.
4370 bool
4371 do_is_static_initializer() const
4372 { return true; }
4374 Type*
4375 do_type()
4376 { return this->type_; }
4378 void
4379 do_determine_type(const Type_context*)
4382 Expression*
4383 do_copy();
4385 Bexpression*
4386 do_get_backend(Translate_context*)
4387 { return this->bexpr_; }
4389 void
4390 do_dump_expression(Ast_dump_context*) const;
4392 private:
4393 // The backend expression we are wrapping.
4394 Bexpression* bexpr_;
4395 // The type of the expression;
4396 Type* type_;
4399 // A numeric constant. This is used both for untyped constants and
4400 // for constants that have a type.
4402 class Numeric_constant
4404 public:
4405 Numeric_constant()
4406 : classification_(NC_INVALID), type_(NULL)
4409 ~Numeric_constant();
4411 Numeric_constant(const Numeric_constant&);
4413 Numeric_constant& operator=(const Numeric_constant&);
4415 // Check equality with another numeric constant.
4416 bool
4417 equals(const Numeric_constant&) const;
4419 // Set to an unsigned long value.
4420 void
4421 set_unsigned_long(Type*, unsigned long);
4423 // Set to an integer value.
4424 void
4425 set_int(Type*, const mpz_t);
4427 // Set to a rune value.
4428 void
4429 set_rune(Type*, const mpz_t);
4431 // Set to a floating point value.
4432 void
4433 set_float(Type*, const mpfr_t);
4435 // Set to a complex value.
4436 void
4437 set_complex(Type*, const mpc_t);
4439 // Mark numeric constant as invalid.
4440 void
4441 set_invalid()
4442 { this->classification_ = NC_INVALID; }
4444 // Classifiers.
4445 bool
4446 is_int() const
4447 { return this->classification_ == Numeric_constant::NC_INT; }
4449 bool
4450 is_rune() const
4451 { return this->classification_ == Numeric_constant::NC_RUNE; }
4453 bool
4454 is_float() const
4455 { return this->classification_ == Numeric_constant::NC_FLOAT; }
4457 bool
4458 is_complex() const
4459 { return this->classification_ == Numeric_constant::NC_COMPLEX; }
4461 bool
4462 is_invalid() const
4463 { return this->classification_ == Numeric_constant::NC_INVALID; }
4465 // Value retrievers. These will initialize the values as well as
4466 // set them. GET_INT is only valid if IS_INT returns true, and
4467 // likewise respectively.
4468 void
4469 get_int(mpz_t*) const;
4471 void
4472 get_rune(mpz_t*) const;
4474 void
4475 get_float(mpfr_t*) const;
4477 void
4478 get_complex(mpc_t*) const;
4480 // Codes returned by to_unsigned_long.
4481 enum To_unsigned_long
4483 // Value is integer and fits in unsigned long.
4484 NC_UL_VALID,
4485 // Value is not integer.
4486 NC_UL_NOTINT,
4487 // Value is integer but is negative.
4488 NC_UL_NEGATIVE,
4489 // Value is non-negative integer but does not fit in unsigned
4490 // long.
4491 NC_UL_BIG
4494 // If the value can be expressed as an integer that fits in an
4495 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
4496 // one of the other To_unsigned_long codes.
4497 To_unsigned_long
4498 to_unsigned_long(unsigned long* val) const;
4500 // If the value can be expressed as an integer that describes the
4501 // size of an object in memory, set *VAL and return true.
4502 // Otherwise, return false. Currently we use int64_t to represent a
4503 // memory size, as in Type::backend_type_size.
4504 bool
4505 to_memory_size(int64_t* val) const;
4507 // If the value can be expressed as an int, return true and
4508 // initialize and set VAL. This will return false for a value with
4509 // an explicit float or complex type, even if the value is integral.
4510 bool
4511 to_int(mpz_t* val) const;
4513 // If the value can be expressed as a float, return true and
4514 // initialize and set VAL.
4515 bool
4516 to_float(mpfr_t* val) const;
4518 // If the value can be expressed as a complex, return true and
4519 // initialize and set VR and VI.
4520 bool
4521 to_complex(mpc_t* val) const;
4523 // Get the type.
4524 Type*
4525 type() const;
4527 // If the constant can be expressed in TYPE, then set the type of
4528 // the constant to TYPE and return true. Otherwise return false,
4529 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
4530 // the location to use for the error.
4531 bool
4532 set_type(Type* type, bool issue_error, Location location);
4534 // Return an Expression for this value.
4535 Expression*
4536 expression(Location) const;
4538 // Calculate a hash code with a given seed.
4539 unsigned int
4540 hash(unsigned int seed) const;
4542 private:
4543 void
4544 clear();
4546 To_unsigned_long
4547 mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
4549 To_unsigned_long
4550 mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
4552 bool
4553 mpz_to_memory_size(const mpz_t ival, int64_t* val) const;
4555 bool
4556 mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const;
4558 bool
4559 check_int_type(Integer_type*, bool, Location);
4561 bool
4562 check_float_type(Float_type*, bool, Location);
4564 bool
4565 check_complex_type(Complex_type*, bool, Location);
4567 static bool
4568 is_float_neg_zero(const mpfr_t, int bits);
4570 // The kinds of constants.
4571 enum Classification
4573 NC_INVALID,
4574 NC_RUNE,
4575 NC_INT,
4576 NC_FLOAT,
4577 NC_COMPLEX
4580 // The kind of constant.
4581 Classification classification_;
4582 // The value.
4583 union
4585 // If NC_INT or NC_RUNE.
4586 mpz_t int_val;
4587 // If NC_FLOAT.
4588 mpfr_t float_val;
4589 // If NC_COMPLEX.
4590 mpc_t complex_val;
4591 } u_;
4592 // The type if there is one. This will be NULL for an untyped
4593 // constant.
4594 Type* type_;
4597 // Temporary buffer size for string conversions.
4598 // Also known to the runtime as tmpStringBufSize in runtime/string.go.
4599 static const int tmp_string_buf_size = 32;
4601 #endif // !defined(GO_EXPRESSIONS_H)