Line data Source code
1 : // expressions.h -- Go frontend expression handling. -*- C++ -*-
2 :
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.
6 :
7 : #ifndef GO_EXPRESSIONS_H
8 : #define GO_EXPRESSIONS_H
9 :
10 : #include <mpfr.h>
11 : #include <mpc.h>
12 :
13 : #include "operator.h"
14 : #include "runtime.h"
15 :
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 Const_expression;
32 : class Var_expression;
33 : class Enclosed_var_expression;
34 : class Temporary_reference_expression;
35 : class Set_and_use_temporary_expression;
36 : class String_expression;
37 : class Type_conversion_expression;
38 : class Unsafe_type_conversion_expression;
39 : class Unary_expression;
40 : class Binary_expression;
41 : class String_concat_expression;
42 : class Call_expression;
43 : class Builtin_call_expression;
44 : class Call_result_expression;
45 : class Func_expression;
46 : class Func_descriptor_expression;
47 : class Unknown_expression;
48 : class Index_expression;
49 : class Array_index_expression;
50 : class String_index_expression;
51 : class Map_index_expression;
52 : class Bound_method_expression;
53 : class Field_reference_expression;
54 : class Interface_field_reference_expression;
55 : class Allocation_expression;
56 : class Composite_literal_expression;
57 : class Struct_construction_expression;
58 : class Array_construction_expression;
59 : class Fixed_array_construction_expression;
60 : class Slice_construction_expression;
61 : class Map_construction_expression;
62 : class Type_guard_expression;
63 : class Heap_expression;
64 : class Receive_expression;
65 : class Slice_value_expression;
66 : class Slice_info_expression;
67 : class Conditional_expression;
68 : class Compound_expression;
69 : class Numeric_constant;
70 : class Named_object;
71 : class Export_function_body;
72 : class Import_expression;
73 : class Temporary_statement;
74 : class Label;
75 : class Ast_dump_context;
76 : class String_dump;
77 :
78 : // The precision to use for complex values represented as an mpc_t.
79 : const int mpc_precision = 256;
80 :
81 : // The base class for all expressions.
82 :
83 : class Expression
84 : {
85 : public:
86 : // The types of expressions.
87 : enum Expression_classification
88 : {
89 : EXPRESSION_ERROR,
90 : EXPRESSION_TYPE,
91 : EXPRESSION_UNARY,
92 : EXPRESSION_BINARY,
93 : EXPRESSION_STRING_CONCAT,
94 : EXPRESSION_CONST_REFERENCE,
95 : EXPRESSION_VAR_REFERENCE,
96 : EXPRESSION_ENCLOSED_VAR_REFERENCE,
97 : EXPRESSION_TEMPORARY_REFERENCE,
98 : EXPRESSION_SET_AND_USE_TEMPORARY,
99 : EXPRESSION_SINK,
100 : EXPRESSION_FUNC_REFERENCE,
101 : EXPRESSION_FUNC_DESCRIPTOR,
102 : EXPRESSION_FUNC_CODE_REFERENCE,
103 : EXPRESSION_UNKNOWN_REFERENCE,
104 : EXPRESSION_BOOLEAN,
105 : EXPRESSION_STRING,
106 : EXPRESSION_STRING_INFO,
107 : EXPRESSION_STRING_VALUE,
108 : EXPRESSION_INTEGER,
109 : EXPRESSION_FLOAT,
110 : EXPRESSION_COMPLEX,
111 : EXPRESSION_NIL,
112 : EXPRESSION_IOTA,
113 : EXPRESSION_CALL,
114 : EXPRESSION_CALL_RESULT,
115 : EXPRESSION_BOUND_METHOD,
116 : EXPRESSION_INDEX,
117 : EXPRESSION_ARRAY_INDEX,
118 : EXPRESSION_STRING_INDEX,
119 : EXPRESSION_MAP_INDEX,
120 : EXPRESSION_SELECTOR,
121 : EXPRESSION_FIELD_REFERENCE,
122 : EXPRESSION_INTERFACE_FIELD_REFERENCE,
123 : EXPRESSION_ALLOCATION,
124 : EXPRESSION_TYPE_GUARD,
125 : EXPRESSION_CONVERSION,
126 : EXPRESSION_UNSAFE_CONVERSION,
127 : EXPRESSION_STRUCT_CONSTRUCTION,
128 : EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
129 : EXPRESSION_SLICE_CONSTRUCTION,
130 : EXPRESSION_MAP_CONSTRUCTION,
131 : EXPRESSION_COMPOSITE_LITERAL,
132 : EXPRESSION_COMPOSITE_LITERAL_KEY,
133 : EXPRESSION_HEAP,
134 : EXPRESSION_RECEIVE,
135 : EXPRESSION_TYPE_DESCRIPTOR,
136 : EXPRESSION_GC_SYMBOL,
137 : EXPRESSION_PTRMASK_SYMBOL,
138 : EXPRESSION_TYPE_INFO,
139 : EXPRESSION_SLICE_INFO,
140 : EXPRESSION_SLICE_VALUE,
141 : EXPRESSION_INTERFACE_INFO,
142 : EXPRESSION_INTERFACE_VALUE,
143 : EXPRESSION_INTERFACE_MTABLE,
144 : EXPRESSION_STRUCT_FIELD_OFFSET,
145 : EXPRESSION_LABEL_ADDR,
146 : EXPRESSION_CONDITIONAL,
147 : EXPRESSION_COMPOUND,
148 : EXPRESSION_BACKEND
149 : };
150 :
151 : Expression(Expression_classification, Location);
152 :
153 : virtual ~Expression();
154 :
155 : // Make an error expression. This is used when a parse error occurs
156 : // to prevent cascading errors.
157 : static Expression*
158 : make_error(Location);
159 :
160 : // Make an expression which is really a type. This is used during
161 : // parsing.
162 : static Expression*
163 : make_type(Type*, Location);
164 :
165 : // Make a unary expression.
166 : static Expression*
167 : make_unary(Operator, Expression*, Location);
168 :
169 : // Make a binary expression.
170 : static Expression*
171 : make_binary(Operator, Expression*, Expression*, Location);
172 :
173 : // Make a string concatenation expression.
174 : static Expression*
175 : make_string_concat(Expression_list*);
176 :
177 : // Make a reference to a constant in an expression.
178 : static Expression*
179 : make_const_reference(Named_object*, Location);
180 :
181 : // Make a reference to a variable in an expression.
182 : static Expression*
183 : make_var_reference(Named_object*, Location);
184 :
185 : // Make a reference to a variable within an enclosing function.
186 : static Expression*
187 : make_enclosing_var_reference(Expression*, Named_object*, Location);
188 :
189 : // Make a reference to a temporary variable. Temporary variables
190 : // are always created by a single statement, which is what we use to
191 : // refer to them.
192 : static Temporary_reference_expression*
193 : make_temporary_reference(Temporary_statement*, Location);
194 :
195 : // Make an expressions which sets a temporary variable and then
196 : // evaluates to a reference to that temporary variable. This is
197 : // used to set a temporary variable while retaining the order of
198 : // evaluation.
199 : static Set_and_use_temporary_expression*
200 : make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
201 :
202 : // Make a sink expression--a reference to the blank identifier _.
203 : static Expression*
204 : make_sink(Location);
205 :
206 : // Make a reference to a function in an expression. This returns a
207 : // pointer to the struct holding the address of the function
208 : // followed by any closed-over variables.
209 : static Expression*
210 : make_func_reference(Named_object*, Expression* closure, Location);
211 :
212 : // Make a function descriptor, an immutable struct with a single
213 : // field that points to the function code. This may only be used
214 : // with functions that do not have closures. FN is the function for
215 : // which we are making the descriptor.
216 : static Func_descriptor_expression*
217 : make_func_descriptor(Named_object* fn);
218 :
219 : // Make a reference to the code of a function. This is used to set
220 : // descriptor and closure fields.
221 : static Expression*
222 : make_func_code_reference(Named_object*, Location);
223 :
224 : // Make a reference to an unknown name. In a correct program this
225 : // will always be lowered to a real const/var/func reference.
226 : static Unknown_expression*
227 : make_unknown_reference(Named_object*, Location);
228 :
229 : // Make a constant bool expression.
230 : static Expression*
231 : make_boolean(bool val, Location);
232 :
233 : // Make a constant string expression.
234 : static Expression*
235 : make_string(const std::string&, Location);
236 :
237 : // Make a constant string expression with a specific string subtype.
238 : static Expression*
239 : make_string_typed(const std::string&, Type*, Location);
240 :
241 : // Make an expression that evaluates to some characteristic of an string.
242 : // For simplicity, the enum values must match the field indexes in the
243 : // underlying struct. This returns an lvalue.
244 : enum String_info
245 : {
246 : // The underlying data in the string.
247 : STRING_INFO_DATA,
248 : // The length of the string.
249 : STRING_INFO_LENGTH
250 : };
251 :
252 : static Expression*
253 : make_string_info(Expression* string, String_info, Location);
254 :
255 : // Make an expression for a string value.
256 : static Expression*
257 : make_string_value(Expression* valptr, Expression* len, Location);
258 :
259 : // Make a character constant expression. TYPE should be NULL for an
260 : // abstract type.
261 : static Expression*
262 : make_character(const mpz_t*, Type*, Location);
263 :
264 : // Make a constant integer expression from a multi-precision
265 : // integer. TYPE should be NULL for an abstract type.
266 : static Expression*
267 : make_integer_z(const mpz_t*, Type*, Location);
268 :
269 : // Make a constant integer expression from an unsigned long. TYPE
270 : // should be NULL for an abstract type.
271 : static Expression*
272 : make_integer_ul(unsigned long, Type*, Location);
273 :
274 : // Make a constant integer expression from a signed long. TYPE
275 : // should be NULL for an abstract type.
276 : static Expression*
277 : make_integer_sl(long, Type*, Location);
278 :
279 : // Make a constant integer expression from an int64_t. TYPE should
280 : // be NULL for an abstract type.
281 : static Expression*
282 : make_integer_int64(int64_t, Type*, Location);
283 :
284 : // Make a constant float expression. TYPE should be NULL for an
285 : // abstract type.
286 : static Expression*
287 : make_float(const mpfr_t*, Type*, Location);
288 :
289 : // Make a constant complex expression. TYPE should be NULL for an
290 : // abstract type.
291 : static Expression*
292 : make_complex(const mpc_t*, Type*, Location);
293 :
294 : // Make a nil expression.
295 : static Expression*
296 : make_nil(Location);
297 :
298 : // Make an iota expression. This is used for the predeclared
299 : // constant iota.
300 : static Expression*
301 : make_iota();
302 :
303 : // Make a call expression.
304 : static Call_expression*
305 : make_call(Expression* func, Expression_list* args, bool is_varargs,
306 : Location);
307 :
308 : // Make a reference to a specific result of a call expression which
309 : // returns a tuple.
310 : static Expression*
311 : make_call_result(Call_expression*, unsigned int index);
312 :
313 : // Make an expression which is a method bound to its first
314 : // parameter. METHOD is the method being called, FUNCTION is the
315 : // function to call.
316 : static Bound_method_expression*
317 : make_bound_method(Expression* object, const Method* method,
318 : Named_object* function, Location);
319 :
320 : // Make an index or slice expression. This is a parser expression
321 : // which represents LEFT[START:END:CAP]. END may be NULL, meaning an
322 : // index rather than a slice. CAP may be NULL, meaning we use the default
323 : // capacity of LEFT. At parse time we may not know the type of LEFT.
324 : // After parsing this is lowered to an array index, a string index,
325 : // or a map index.
326 : static Expression*
327 : make_index(Expression* left, Expression* start, Expression* end,
328 : Expression* cap, Location);
329 :
330 : // Make an array index expression. END may be NULL, in which case
331 : // this is an lvalue. CAP may be NULL, in which case it defaults
332 : // to cap(ARRAY).
333 : static Expression*
334 : make_array_index(Expression* array, Expression* start, Expression* end,
335 : Expression* cap, Location);
336 :
337 : // Make a string index expression. END may be NULL. This is never
338 : // an lvalue.
339 : static Expression*
340 : make_string_index(Expression* string, Expression* start, Expression* end,
341 : Location);
342 :
343 : // Make a map index expression. This is an lvalue.
344 : static Map_index_expression*
345 : make_map_index(Expression* map, Expression* val, Location);
346 :
347 : // Make a selector. This is a parser expression which represents
348 : // LEFT.NAME. At parse time we may not know the type of the left
349 : // hand side.
350 : static Expression*
351 : make_selector(Expression* left, const std::string& name, Location);
352 :
353 : // Make a reference to a field in a struct.
354 : static Field_reference_expression*
355 : make_field_reference(Expression*, unsigned int field_index, Location);
356 :
357 : // Make a reference to a field of an interface, with an associated
358 : // object.
359 : static Expression*
360 : make_interface_field_reference(Expression*, const std::string&,
361 : Location);
362 :
363 : // Make an allocation expression.
364 : static Expression*
365 : make_allocation(Type*, Location);
366 :
367 : // Make a type guard expression.
368 : static Expression*
369 : make_type_guard(Expression*, Type*, Location);
370 :
371 : // Make a type cast expression.
372 : static Expression*
373 : make_cast(Type*, Expression*, Location);
374 :
375 : // Make an unsafe type cast expression. This is only used when
376 : // passing parameter to builtin functions that are part of the Go
377 : // runtime.
378 : static Expression*
379 : make_unsafe_cast(Type*, Expression*, Location);
380 :
381 : // Make a composite literal. The DEPTH parameter is how far down we
382 : // are in a list of composite literals with omitted types. HAS_KEYS
383 : // is true if the expression list has keys alternating with values.
384 : // ALL_ARE_NAMES is true if all the keys could be struct field
385 : // names.
386 : static Expression*
387 : make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
388 : bool all_are_names, Location);
389 :
390 : // Make a composite literal key.
391 : static Expression*
392 : make_composite_literal_key(const std::string& name, Location);
393 :
394 : // Make a struct composite literal.
395 : static Expression*
396 : make_struct_composite_literal(Type*, Expression_list*, Location);
397 :
398 : // Make an array composite literal.
399 : static Expression*
400 : make_array_composite_literal(Type*, Expression_list*, Location);
401 :
402 : // Make a slice composite literal.
403 : static Slice_construction_expression*
404 : make_slice_composite_literal(Type*, Expression_list*, Location);
405 :
406 : // Take an expression and allocate it on the heap.
407 : static Expression*
408 : make_heap_expression(Expression*, Location);
409 :
410 : // Make a receive expression. VAL is NULL for a unary receive.
411 : static Receive_expression*
412 : make_receive(Expression* channel, Location);
413 :
414 : // Make an expression which evaluates to the address of the type
415 : // descriptor for TYPE.
416 : static Expression*
417 : make_type_descriptor(Type* type, Location);
418 :
419 : // Make an expression which evaluates to the address of the gc
420 : // symbol for TYPE.
421 : static Expression*
422 : make_gc_symbol(Type* type);
423 :
424 : // Make an expression that evaluates to the address of a ptrmask
425 : // symbol for TYPE. For most types this will be the same as
426 : // make_gc_symbol, but for larger types make_gc_symbol will return a
427 : // gcprog while this will return a ptrmask.
428 : static Expression*
429 : make_ptrmask_symbol(Type* type);
430 :
431 : // Make an expression which evaluates to some characteristic of a
432 : // type. These are only used for type descriptors, so there is no
433 : // location parameter.
434 : enum Type_info
435 : {
436 : // The size of a value of the type.
437 : TYPE_INFO_SIZE,
438 : // The required alignment of a value of the type.
439 : TYPE_INFO_ALIGNMENT,
440 : // The required alignment of a value of the type when used as a
441 : // field in a struct.
442 : TYPE_INFO_FIELD_ALIGNMENT,
443 : // The size of the prefix of a value of the type that contains
444 : // all the pointers. This is 0 for a type that contains no
445 : // pointers. It is always <= TYPE_INFO_SIZE.
446 : TYPE_INFO_BACKEND_PTRDATA,
447 : // Like TYPE_INFO_BACKEND_PTRDATA, but the ptrdata value that we
448 : // want to store in a type descriptor. They are the same for
449 : // most types, but can differ for a type that uses a gcprog.
450 : TYPE_INFO_DESCRIPTOR_PTRDATA
451 : };
452 :
453 : static Expression*
454 : make_type_info(Type* type, Type_info);
455 :
456 : // Make an expression that evaluates to some characteristic of a
457 : // slice. For simplicity, the enum values must match the field indexes
458 : // in the underlying struct. This returns an lvalue.
459 : enum Slice_info
460 : {
461 : // The underlying data of the slice.
462 : SLICE_INFO_VALUE_POINTER,
463 : // The length of the slice.
464 : SLICE_INFO_LENGTH,
465 : // The capacity of the slice.
466 : SLICE_INFO_CAPACITY
467 : };
468 :
469 : static Expression*
470 : make_slice_info(Expression* slice, Slice_info, Location);
471 :
472 : // Make an expression for a slice value.
473 : static Expression*
474 : make_slice_value(Type*, Expression* valptr, Expression* len, Expression* cap,
475 : Location);
476 :
477 : // Make an expression that evaluates to some characteristic of an
478 : // interface. For simplicity, the enum values must match the field indexes
479 : // in the underlying struct. This returns an lvalue.
480 : enum Interface_info
481 : {
482 : // The type descriptor of an empty interface.
483 : INTERFACE_INFO_TYPE_DESCRIPTOR = 0,
484 : // The methods of an interface.
485 : INTERFACE_INFO_METHODS = 0,
486 : // The first argument to pass to an interface method.
487 : INTERFACE_INFO_OBJECT
488 : };
489 :
490 : static Expression*
491 : make_interface_info(Expression* iface, Interface_info, Location);
492 :
493 : // Make an expression for an interface value.
494 : static Expression*
495 : make_interface_value(Type*, Expression*, Expression*, Location);
496 :
497 : // Make an expression that builds a reference to the interface method table
498 : // for TYPE that satisfies interface ITYPE. IS_POINTER is true if this is a
499 : // reference to the interface method table for the pointer receiver type.
500 : static Expression*
501 : make_interface_mtable_ref(Interface_type* itype, Type* type,
502 : bool is_pointer, Location);
503 :
504 : // Make an expression which evaluates to the offset of a field in a
505 : // struct. This is only used for type descriptors, so there is no
506 : // location parameter.
507 : static Expression*
508 : make_struct_field_offset(Struct_type*, const Struct_field*);
509 :
510 : // Make an expression which evaluates to the address of an unnamed
511 : // label.
512 : static Expression*
513 : make_label_addr(Label*, Location);
514 :
515 : // Make a conditional expression.
516 : static Expression*
517 : make_conditional(Expression*, Expression*, Expression*, Location);
518 :
519 : // Make a compound expression.
520 : static Expression*
521 : make_compound(Expression*, Expression*, Location);
522 :
523 : // Make a backend expression.
524 : static Expression*
525 : make_backend(Bexpression*, Type*, Location);
526 :
527 : enum Nil_check_classification
528 : {
529 : // Use the default policy for deciding if this deref needs a check.
530 : NIL_CHECK_DEFAULT,
531 : // An explicit check is required for this dereference operation.
532 : NIL_CHECK_NEEDED,
533 : // No check needed for this dereference operation.
534 : NIL_CHECK_NOT_NEEDED,
535 : // A type error or error construct was encountered when determining
536 : // whether this deref needs an explicit check.
537 : NIL_CHECK_ERROR_ENCOUNTERED
538 : };
539 :
540 : // Make a dereference expression.
541 : static Expression*
542 : make_dereference(Expression*, Nil_check_classification, Location);
543 :
544 : // Return the expression classification.
545 : Expression_classification
546 60210028 : classification() const
547 60210028 : { return this->classification_; }
548 :
549 : // Return the location of the expression.
550 : Location
551 64522142 : location() const
552 60936829 : { return this->location_; }
553 :
554 : // Set the location of an expression and all its subexpressions.
555 : // This is used for const declarations where the expression is
556 : // copied from an earlier declaration.
557 : void
558 : set_location(Location loc);
559 :
560 : // For set_location. This should really be a local class in
561 : // Expression, but it needs types defined in gogo.h.
562 : friend class Set_location;
563 :
564 : // Return whether this is a constant expression.
565 : bool
566 215917643 : is_constant() const
567 6795479 : { return this->do_is_constant(); }
568 :
569 : // Return whether this expression is untyped. This isn't quite the
570 : // same as is_constant with an abstract type, as 1<<val is untyped
571 : // even if val is a variable. If this returns true, it sets *PTYPE
572 : // to an abstract type, which is the type the expression will have
573 : // if there is no context.
574 : bool
575 210811737 : is_untyped(Type** ptype) const
576 210811737 : { return this->do_is_untyped(ptype); }
577 :
578 : // Return whether this is the zero value of its type.
579 : bool
580 1427 : is_zero_value() const
581 1424 : { return this->do_is_zero_value(); }
582 :
583 : // Return whether this expression can be used as a static
584 : // initializer. This is true for an expression that has only
585 : // numbers and pointers to global variables or composite literals
586 : // that do not require runtime initialization. It is false if we
587 : // must generate code to compute this expression when it is used to
588 : // initialize a global variable. This is not a language-level
589 : // concept, but an implementation-level one. If this expression is
590 : // used to initialize a global variable, this is true if we can pass
591 : // an initializer to the backend, false if we must generate code to
592 : // initialize the variable. It is always safe for this method to
593 : // return false, but the resulting code may be less efficient.
594 : bool
595 8379510 : is_static_initializer() const
596 8379510 : { return this->do_is_static_initializer(); }
597 :
598 : // If this is not a numeric constant, return false. If it is one,
599 : // return true, and set VAL to hold the value. This method can be
600 : // called before the determine_types pass.
601 : bool
602 30622769 : numeric_constant_value(Numeric_constant* val)
603 30610630 : { return this->do_numeric_constant_value(val); }
604 :
605 : // If this is not a constant expression with string type, return
606 : // false. If it is one, return true, and set VAL to the value.
607 : bool
608 954244 : string_constant_value(std::string* val)
609 954244 : { return this->do_string_constant_value(val); }
610 :
611 : // If this is not a constant expression with boolean type, return
612 : // false. If it is one, return true, and set VAL to the value.
613 : bool
614 1890936 : boolean_constant_value(bool* val)
615 1890936 : { return this->do_boolean_constant_value(val); }
616 :
617 : // If this is a const reference expression, return the named
618 : // object to which the expression refers, otherwise return NULL.
619 : const Named_object*
620 : named_constant() const;
621 :
622 : // This is called if the value of this expression is being
623 : // discarded. This issues warnings about computed values being
624 : // unused. This returns true if all is well, false if it issued an
625 : // error message.
626 : bool
627 279341 : discarding_value()
628 279341 : { return this->do_discarding_value(); }
629 :
630 : // Return whether this is an error expression.
631 : bool
632 98018032 : is_error_expression() const
633 82044947 : { return this->classification_ == EXPRESSION_ERROR; }
634 :
635 : // Return whether this expression really represents a type.
636 : bool
637 : is_type_expression() const;
638 :
639 : // If this is a const reference, return the Const_expression
640 : // structure. Otherwise, return NULL. This is a controlled dynamic
641 : // cast.
642 : Const_expression*
643 1137420 : const_expression()
644 1137427 : { return this->convert<Const_expression, EXPRESSION_CONST_REFERENCE>(); }
645 :
646 : const Const_expression*
647 : const_expression() const
648 : {
649 : return this->convert<const Const_expression,
650 : EXPRESSION_CONST_REFERENCE>();
651 : }
652 :
653 : // If this is a variable reference, return the Var_expression
654 : // structure. Otherwise, return NULL. This is a controlled dynamic
655 : // cast.
656 : Var_expression*
657 105183527 : var_expression()
658 127217113 : { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
659 :
660 : const Var_expression*
661 615552 : var_expression() const
662 615552 : { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
663 :
664 : // If this is a enclosed_variable reference, return the
665 : // Enclosed_var_expression structure. Otherwise, return NULL.
666 : // This is a controlled dynamic cast.
667 : Enclosed_var_expression*
668 31123695 : enclosed_var_expression()
669 31154277 : { return this->convert<Enclosed_var_expression,
670 30816292 : EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
671 :
672 : const Enclosed_var_expression*
673 : enclosed_var_expression() const
674 : { return this->convert<const Enclosed_var_expression,
675 : EXPRESSION_ENCLOSED_VAR_REFERENCE>(); }
676 :
677 :
678 : // If this is a reference to a temporary variable, return the
679 : // Temporary_reference_expression. Otherwise, return NULL.
680 : Temporary_reference_expression*
681 23711082 : temporary_reference_expression()
682 : {
683 8350360 : return this->convert<Temporary_reference_expression,
684 40980277 : EXPRESSION_TEMPORARY_REFERENCE>();
685 : }
686 :
687 : // If this is a set-and-use-temporary, return the
688 : // Set_and_use_temporary_expression. Otherwise, return NULL.
689 : Set_and_use_temporary_expression*
690 1911247 : set_and_use_temporary_expression()
691 : {
692 1911247 : return this->convert<Set_and_use_temporary_expression,
693 3120526 : EXPRESSION_SET_AND_USE_TEMPORARY>();
694 : }
695 :
696 : // Return whether this is a sink expression.
697 : bool
698 7441050 : is_sink_expression() const
699 7441050 : { return this->classification_ == EXPRESSION_SINK; }
700 :
701 : // If this is a string expression, return the String_expression
702 : // structure. Otherwise, return NULL.
703 : String_expression*
704 2497591 : string_expression()
705 2573568 : { return this->convert<String_expression, EXPRESSION_STRING>(); }
706 :
707 : // If this is a conversion expression, return the Type_conversion_expression
708 : // structure. Otherwise, return NULL.
709 : Type_conversion_expression*
710 25285303 : conversion_expression()
711 26764818 : { return this->convert<Type_conversion_expression, EXPRESSION_CONVERSION>(); }
712 :
713 : // If this is an unsafe conversion expression, return the
714 : // Unsafe_type_conversion_expression structure. Otherwise, return NULL.
715 : Unsafe_type_conversion_expression*
716 153493 : unsafe_conversion_expression()
717 : {
718 261699 : return this->convert<Unsafe_type_conversion_expression,
719 209999 : EXPRESSION_UNSAFE_CONVERSION>();
720 : }
721 :
722 : // Return whether this is the expression nil.
723 : bool
724 2373367 : is_nil_expression() const
725 2148860 : { return this->classification_ == EXPRESSION_NIL; }
726 :
727 : // If this is an indirection through a pointer, return the
728 : // expression being pointed through. Otherwise return this.
729 : Expression*
730 : deref();
731 :
732 : // If this is a unary expression, return the Unary_expression
733 : // structure. Otherwise return NULL.
734 : Unary_expression*
735 79570048 : unary_expression()
736 116355542 : { return this->convert<Unary_expression, EXPRESSION_UNARY>(); }
737 :
738 : const Unary_expression*
739 25617254 : unary_expression() const
740 149942 : { return this->convert<const Unary_expression, EXPRESSION_UNARY>(); }
741 :
742 : // If this is a binary expression, return the Binary_expression
743 : // structure. Otherwise return NULL.
744 : Binary_expression*
745 37489771 : binary_expression()
746 14852457 : { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
747 :
748 : // If this is a string concatenation expression, return the
749 : // String_concat_expression structure. Otherwise, return NULL.
750 : String_concat_expression*
751 25065229 : string_concat_expression()
752 : {
753 25099583 : return this->convert<String_concat_expression, EXPRESSION_STRING_CONCAT>();
754 : }
755 :
756 : // If this is a call expression, return the Call_expression
757 : // structure. Otherwise, return NULL. This is a controlled dynamic
758 : // cast.
759 : Call_expression*
760 67762320 : call_expression()
761 42767785 : { return this->convert<Call_expression, EXPRESSION_CALL>(); }
762 :
763 : const Call_expression*
764 309595 : call_expression() const
765 58801 : { return this->convert<const Call_expression, EXPRESSION_CALL>(); }
766 :
767 : // If this is a call_result expression, return the Call_result_expression
768 : // structure. Otherwise, return NULL. This is a controlled dynamic
769 : // cast.
770 : Call_result_expression*
771 : call_result_expression()
772 172188 : { return this->convert<Call_result_expression, EXPRESSION_CALL_RESULT>(); }
773 :
774 : // If this is an expression which refers to a function, return the
775 : // Func_expression structure. Otherwise, return NULL.
776 : Func_expression*
777 77757533 : func_expression()
778 77183920 : { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
779 :
780 : const Func_expression*
781 407439 : func_expression() const
782 407439 : { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
783 :
784 : // If this is an expression which refers to an unknown name, return
785 : // the Unknown_expression structure. Otherwise, return NULL.
786 : Unknown_expression*
787 36704340 : unknown_expression()
788 36945420 : { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
789 :
790 : const Unknown_expression*
791 26450893 : unknown_expression() const
792 : {
793 27284532 : return this->convert<const Unknown_expression,
794 26450893 : EXPRESSION_UNKNOWN_REFERENCE>();
795 : }
796 :
797 : // If this is an index expression, return the Index_expression
798 : // structure. Otherwise, return NULL.
799 : Index_expression*
800 47821 : index_expression()
801 13761 : { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
802 :
803 : // If this is an expression which refers to indexing in a array,
804 : // return the Array_index_expression structure. Otherwise, return
805 : // NULL.
806 : Array_index_expression*
807 86514245 : array_index_expression()
808 95578253 : { return this->convert<Array_index_expression, EXPRESSION_ARRAY_INDEX>(); }
809 :
810 : // If this is an expression which refers to indexing in a string,
811 : // return the String_index_expression structure. Otherwise, return
812 : // NULL.
813 : String_index_expression*
814 26279394 : string_index_expression()
815 26285367 : { return this->convert<String_index_expression, EXPRESSION_STRING_INDEX>(); }
816 :
817 : // If this is an expression which refers to indexing in a map,
818 : // return the Map_index_expression structure. Otherwise, return
819 : // NULL.
820 : Map_index_expression*
821 18240484 : map_index_expression()
822 27051773 : { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
823 :
824 : // If this is a bound method expression, return the
825 : // Bound_method_expression structure. Otherwise, return NULL.
826 : Bound_method_expression*
827 43186586 : bound_method_expression()
828 43482777 : { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
829 :
830 : // If this is a reference to a field in a struct, return the
831 : // Field_reference_expression structure. Otherwise, return NULL.
832 : Field_reference_expression*
833 59840427 : field_reference_expression()
834 : {
835 13002274 : return this->convert<Field_reference_expression,
836 59839273 : EXPRESSION_FIELD_REFERENCE>();
837 : }
838 :
839 : // If this is a reference to a field in an interface, return the
840 : // Interface_field_reference_expression structure. Otherwise,
841 : // return NULL.
842 : Interface_field_reference_expression*
843 14827872 : interface_field_reference_expression()
844 : {
845 14254138 : return this->convert<Interface_field_reference_expression,
846 15437323 : EXPRESSION_INTERFACE_FIELD_REFERENCE>();
847 : }
848 :
849 : // If this is an allocation expression, return the Allocation_expression
850 : // structure. Otherwise, return NULL.
851 : Allocation_expression*
852 47556564 : allocation_expression()
853 47561265 : { return this->convert<Allocation_expression, EXPRESSION_ALLOCATION>(); }
854 :
855 : // If this is a general composite literal, return the
856 : // Composite_literal_expression structure. Otherwise, return NULL.
857 : Composite_literal_expression*
858 35 : complit()
859 : {
860 35 : return this->convert<Composite_literal_expression,
861 35 : EXPRESSION_COMPOSITE_LITERAL>();
862 : }
863 :
864 : // If this is a struct composite literal, return the
865 : // Struct_construction_expression structure. Otherwise, return NULL.
866 : Struct_construction_expression*
867 66668 : struct_literal()
868 : {
869 66668 : return this->convert<Struct_construction_expression,
870 66668 : EXPRESSION_STRUCT_CONSTRUCTION>();
871 : }
872 :
873 : // If this is a array composite literal, return the
874 : // Array_construction_expression structure. Otherwise, return NULL.
875 : Fixed_array_construction_expression*
876 7965 : array_literal()
877 : {
878 7965 : return this->convert<Fixed_array_construction_expression,
879 7965 : EXPRESSION_FIXED_ARRAY_CONSTRUCTION>();
880 : }
881 :
882 : // If this is a slice composite literal, return the
883 : // Slice_construction_expression structure. Otherwise, return NULL.
884 : Slice_construction_expression*
885 39980080 : slice_literal()
886 : {
887 14428255 : return this->convert<Slice_construction_expression,
888 39960125 : EXPRESSION_SLICE_CONSTRUCTION>();
889 : }
890 :
891 : // If this is a map composite literal, return the
892 : // Map_construction_expression structure. Otherwise, return NULL.
893 : Map_construction_expression*
894 25038840 : map_literal()
895 : {
896 25040944 : return this->convert<Map_construction_expression,
897 25069127 : EXPRESSION_MAP_CONSTRUCTION>();
898 : }
899 :
900 : // If this is a type guard expression, return the
901 : // Type_guard_expression structure. Otherwise, return NULL.
902 : Type_guard_expression*
903 24297383 : type_guard_expression()
904 24424798 : { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
905 :
906 : // If this is a heap expression, returhn the Heap_expression structure.
907 : // Otherwise, return NULL.
908 : Heap_expression*
909 43560506 : heap_expression()
910 43242199 : { return this->convert<Heap_expression, EXPRESSION_HEAP>(); }
911 :
912 : // If this is a receive expression, return the Receive_expression
913 : // structure. Otherwise, return NULL.
914 : Receive_expression*
915 38948 : receive_expression()
916 5484 : { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
917 :
918 : // If this is a slice value expression, return the Slice_valiue_expression
919 : // structure. Otherwise, return NULL.
920 : Slice_value_expression*
921 13763134 : slice_value_expression()
922 29004 : { return this->convert<Slice_value_expression, EXPRESSION_SLICE_VALUE>(); }
923 :
924 : // If this is a conditional expression, return the Conditional_expression
925 : // structure. Otherwise, return NULL.
926 : Conditional_expression*
927 : conditional_expression()
928 1718 : { return this->convert<Conditional_expression, EXPRESSION_CONDITIONAL>(); }
929 :
930 : // If this is a compound expression, return the Compound_expression structure.
931 : // Otherwise, return NULL.
932 : Compound_expression*
933 : compound_expression()
934 30 : { return this->convert<Compound_expression, EXPRESSION_COMPOUND>(); }
935 :
936 : // If this is a slice info expression, return the
937 : // Slice_info_expression structure. Otherwise, return NULL.
938 : Slice_info_expression*
939 : slice_info_expression()
940 : {
941 72 : return this->convert<Slice_info_expression, EXPRESSION_SLICE_INFO>();
942 : }
943 :
944 : // Return true if this is a composite literal.
945 : bool
946 : is_composite_literal() const;
947 :
948 : // Return true if this is a composite literal which is not constant.
949 : bool
950 : is_nonconstant_composite_literal() const;
951 :
952 : // Return true if this is a variable or temporary variable.
953 : bool
954 : is_variable() const;
955 :
956 : // Return true if this is a reference to a local variable.
957 : bool
958 : is_local_variable() const;
959 :
960 : // Return true if multiple evaluations of this expression are OK.
961 : // This is true for simple variable references and constants.
962 : bool
963 : is_multi_eval_safe();
964 :
965 : // Return true if two expressions refer to the same variable or
966 : // struct field.
967 : static bool
968 : is_same_variable(Expression*, Expression*);
969 :
970 : // Make the builtin function descriptor type, so that it can be
971 : // converted.
972 : static void
973 : make_func_descriptor_type();
974 :
975 : // Traverse an expression.
976 : static int
977 : traverse(Expression**, Traverse*);
978 :
979 : // Traverse subexpressions of this expression.
980 : int
981 : traverse_subexpressions(Traverse*);
982 :
983 : // Lower an expression. This is called immediately after parsing.
984 : // FUNCTION is the function we are in; it will be NULL for an
985 : // expression initializing a global variable. INSERTER may be used
986 : // to insert statements before the statement or initializer
987 : // containing this expression; it is normally used to create
988 : // temporary variables. IOTA_VALUE is the value that we should give
989 : // to any iota expressions. This function must resolve expressions
990 : // which could not be fully parsed into their final form. It
991 : // returns the same Expression or a new one.
992 : Expression*
993 31703876 : lower(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
994 31703876 : { return this->do_lower(gogo, function, inserter); }
995 :
996 : // Flatten an expression. This is called after order_evaluation.
997 : // FUNCTION is the function we are in; it will be NULL for an
998 : // expression initializing a global variable. INSERTER may be used
999 : // to insert statements before the statement or initializer
1000 : // containing this expression; it is normally used to create
1001 : // temporary variables. This function must resolve expressions
1002 : // which could not be fully parsed into their final form. It
1003 : // returns the same Expression or a new one.
1004 : Expression*
1005 18348843 : flatten(Gogo* gogo, Named_object* function, Statement_inserter* inserter)
1006 18348843 : { return this->do_flatten(gogo, function, inserter); }
1007 :
1008 : // Make implicit type conversions explicit.
1009 : void
1010 11386106 : add_conversions()
1011 11386106 : { this->do_add_conversions(); }
1012 :
1013 : // Determine the real type of an expression with abstract integer,
1014 : // floating point, or complex type. TYPE_CONTEXT describes the
1015 : // expected type.
1016 : void
1017 : determine_type(Gogo*, const Type_context*);
1018 :
1019 : // Check types in an expression.
1020 : void
1021 10046743 : check_types(Gogo* gogo)
1022 10046743 : { this->do_check_types(gogo); }
1023 :
1024 : // Determine the type when there is no context.
1025 : void
1026 : determine_type_no_context(Gogo*);
1027 :
1028 : // Return the type of the expression. This should not be called
1029 : // before the determine_types pass.
1030 : Type*
1031 218015497 : type()
1032 207245648 : { return this->do_type(); }
1033 :
1034 : // Return a copy of an expression.
1035 : Expression*
1036 2132451 : copy()
1037 1668735 : { return this->do_copy(); }
1038 :
1039 : // Return the cost of this statement for inlining purposes.
1040 : int
1041 9245900 : inlining_cost() const
1042 9245900 : { return this->do_inlining_cost(); }
1043 :
1044 : // Return whether the expression is addressable--something which may
1045 : // be used as the operand of the unary & operator.
1046 : bool
1047 1190632 : is_addressable() const
1048 967206 : { return this->do_is_addressable(); }
1049 :
1050 : // Note that we are taking the address of this expression. ESCAPES
1051 : // is true if this address escapes the current function.
1052 : void
1053 2219128 : address_taken(bool escapes)
1054 2064295 : { this->do_address_taken(escapes); }
1055 :
1056 : // Note that a nil check must be issued for this expression.
1057 : void
1058 282063 : issue_nil_check()
1059 203921 : { this->do_issue_nil_check(); }
1060 :
1061 : // Return whether this expression must be evaluated in order
1062 : // according to the order of evaluation rules. This is basically
1063 : // true of all expressions with side-effects.
1064 : bool
1065 12631851 : must_eval_in_order() const
1066 12631851 : { return this->do_must_eval_in_order(); }
1067 :
1068 : // Return whether subexpressions of this expression must be
1069 : // evaluated in order. This is true of index expressions and
1070 : // pointer indirections. This sets *SKIP to the number of
1071 : // subexpressions to skip during traversing, as index expressions
1072 : // only requiring moving the index, not the array.
1073 : bool
1074 361371 : must_eval_subexpressions_in_order(int* skip) const
1075 : {
1076 361371 : *skip = 0;
1077 361371 : return this->do_must_eval_subexpressions_in_order(skip);
1078 : }
1079 :
1080 : // Return the backend representation for this expression.
1081 : Bexpression*
1082 : get_backend(Translate_context*);
1083 :
1084 : // Return an expression handling any conversions which must be done during
1085 : // assignment.
1086 : static Expression*
1087 : convert_for_assignment(Gogo*, Type* lhs_type, Expression* rhs,
1088 : Location location);
1089 :
1090 : // Return an expression converting a value of one interface type to another
1091 : // interface type. If FOR_TYPE_GUARD is true this is for a type
1092 : // assertion.
1093 : static Expression*
1094 : convert_interface_to_interface(Gogo*, Type* lhs_type,
1095 : Expression* rhs, bool for_type_guard,
1096 : Location);
1097 :
1098 : // Return an expression for a conversion from a non-interface type to an
1099 : // interface type. If ON_STACK is true, it can allocate the storage on
1100 : // stack.
1101 : static Expression*
1102 : convert_type_to_interface(Type* lhs_type, Expression* rhs,
1103 : bool on_stack, Location);
1104 :
1105 : // Return a backend expression implementing the comparison LEFT OP RIGHT.
1106 : // TYPE is the type of both sides.
1107 : static Bexpression*
1108 : comparison(Translate_context*, Type* result_type, Operator op,
1109 : Expression* left, Expression* right, Location);
1110 :
1111 : // Return the backend expression for the numeric constant VAL.
1112 : static Bexpression*
1113 : backend_numeric_constant_expression(Translate_context*,
1114 : Numeric_constant* val);
1115 :
1116 : // Export the expression.
1117 : void
1118 219171 : export_expression(Export_function_body* efb) const
1119 219171 : { this->do_export(efb); }
1120 :
1121 : // Import an expression. The location should be used for the
1122 : // returned expression. Errors should be reported using the
1123 : // Import's location method.
1124 : static Expression*
1125 : import_expression(Import_expression*, Location);
1126 :
1127 : // Insert bounds checks for an index expression.
1128 : static void
1129 : check_bounds(Gogo*, Expression* val, Operator, Expression* bound,
1130 : Runtime::Function, Runtime::Function, Runtime::Function,
1131 : Runtime::Function, Statement_inserter*, Location);
1132 :
1133 : // Return an expression for constructing a direct interface type from a
1134 : // pointer.
1135 : static Expression*
1136 : pack_direct_iface(Type*, Expression*, Location);
1137 :
1138 : // Return an expression of the underlying pointer for a direct interface
1139 : // type (the opposite of pack_direct_iface).
1140 : static Expression*
1141 : unpack_direct_iface(Expression*, Location);
1142 :
1143 : // Return an expression representing the type descriptor field of an
1144 : // interface.
1145 : static Expression*
1146 : get_interface_type_descriptor(Expression*);
1147 :
1148 : // Look through the expression of a Slice_value_expression's valmem to
1149 : // find an call to makeslice.
1150 : static std::pair<Call_expression*, Temporary_statement*>
1151 : find_makeslice_call(Expression*);
1152 :
1153 : // Dump an expression to a dump constext.
1154 : void
1155 : dump_expression(Ast_dump_context*) const;
1156 :
1157 : protected:
1158 : // May be implemented by child class: traverse the expressions.
1159 : virtual int
1160 : do_traverse(Traverse*);
1161 :
1162 : // Return a lowered expression.
1163 : virtual Expression*
1164 15560444 : do_lower(Gogo*, Named_object*, Statement_inserter*)
1165 15560444 : { return this; }
1166 :
1167 : // Return a flattened expression.
1168 : virtual Expression*
1169 13503066 : do_flatten(Gogo*, Named_object*, Statement_inserter*)
1170 13503066 : { return this; }
1171 :
1172 : // Make implicit type conversions explicit.
1173 : virtual void
1174 10238475 : do_add_conversions()
1175 10238475 : { }
1176 :
1177 : // Return whether this is a constant expression.
1178 : virtual bool
1179 3796902 : do_is_constant() const
1180 3796902 : { return false; }
1181 :
1182 : // Return whether this expression is untyped.
1183 : virtual bool
1184 2065758 : do_is_untyped(Type**) const
1185 2065758 : { return false; }
1186 :
1187 : // Return whether this is the zero value of its type.
1188 : virtual bool
1189 667 : do_is_zero_value() const
1190 667 : { return false; }
1191 :
1192 : // Return whether this expression can be used as a constant
1193 : // initializer.
1194 : virtual bool
1195 17824 : do_is_static_initializer() const
1196 17824 : { return false; }
1197 :
1198 : // Return whether this is a constant expression of numeric type, and
1199 : // set the Numeric_constant to the value.
1200 : virtual bool
1201 2289501 : do_numeric_constant_value(Numeric_constant*)
1202 2289501 : { return false; }
1203 :
1204 : // Return whether this is a constant expression of string type, and
1205 : // set VAL to the value.
1206 : virtual bool
1207 813904 : do_string_constant_value(std::string*)
1208 813904 : { return false; }
1209 :
1210 : // Return whether this is a constant expression of boolean type, and
1211 : // set VAL to the value.
1212 : virtual bool
1213 707295 : do_boolean_constant_value(bool*)
1214 707295 : { return false; }
1215 :
1216 : // Called by the parser if the value is being discarded.
1217 : virtual bool
1218 : do_discarding_value();
1219 :
1220 : // Child class holds type.
1221 : virtual Type*
1222 : do_type() = 0;
1223 :
1224 : // Child class implements determining type information.
1225 : virtual void
1226 : do_determine_type(Gogo*, const Type_context*) = 0;
1227 :
1228 : // Child class implements type checking if needed.
1229 : virtual void
1230 4475935 : do_check_types(Gogo*)
1231 4475935 : { }
1232 :
1233 : // Child class implements copying.
1234 : virtual Expression*
1235 : do_copy() = 0;
1236 :
1237 : // Child class implements determining the cost of this statement for
1238 : // inlining. The default cost is high, so we only need to define
1239 : // this method for expressions that can be inlined.
1240 : virtual int
1241 998946 : do_inlining_cost() const
1242 998946 : { return 0x100000; }
1243 :
1244 : // Child class implements whether the expression is addressable.
1245 : virtual bool
1246 46576 : do_is_addressable() const
1247 46576 : { return false; }
1248 :
1249 : // Child class implements taking the address of an expression.
1250 : virtual void
1251 566233 : do_address_taken(bool)
1252 566233 : { }
1253 :
1254 : // Child class implements issuing a nil check if the address is taken.
1255 : virtual void
1256 114469 : do_issue_nil_check()
1257 114469 : { }
1258 :
1259 : // Child class implements whether this expression must be evaluated
1260 : // in order.
1261 : virtual bool
1262 11470008 : do_must_eval_in_order() const
1263 11470008 : { return false; }
1264 :
1265 : // Child class implements whether this expressions requires that
1266 : // subexpressions be evaluated in order. The child implementation
1267 : // may set *SKIP if it should be non-zero.
1268 : virtual bool
1269 342266 : do_must_eval_subexpressions_in_order(int* /* skip */) const
1270 342266 : { return false; }
1271 :
1272 : // Child class implements conversion to backend representation.
1273 : virtual Bexpression*
1274 : do_get_backend(Translate_context*) = 0;
1275 :
1276 : // Child class implements export.
1277 : virtual void
1278 : do_export(Export_function_body*) const;
1279 :
1280 : // For children to call to give an error for an unused value.
1281 : void
1282 : unused_value_error();
1283 :
1284 : // For children to call when they detect that they are in error.
1285 : void
1286 : set_is_error();
1287 :
1288 : // For children to call to report an error conveniently.
1289 : void
1290 : report_error(const char*);
1291 :
1292 : // A convenience function for handling a type in do_is_untyped. If
1293 : // TYPE is not abstract, return false. Otherwise set *PTYPE to TYPE
1294 : // and return true.
1295 : static bool
1296 : is_untyped_type(Type* type, Type** ptype);
1297 :
1298 : // Write a name to export data.
1299 : static void
1300 : export_name(Export_function_body* efb, const Named_object*);
1301 :
1302 : // Child class implements dumping to a dump context.
1303 : virtual void
1304 : do_dump_expression(Ast_dump_context*) const = 0;
1305 :
1306 : // Start exporting a type conversion for a constant, if needed.
1307 : static bool
1308 : export_constant_type(Export_function_body*, Type*);
1309 :
1310 : // Finish exporting a type conversion for a constant.
1311 : static void
1312 : finish_export_constant_type(Export_function_body*, bool);
1313 :
1314 : // Varargs lowering creates a slice object (unnamed compiler temp)
1315 : // to contain the variable length collection of values. The enum
1316 : // below tells the lowering routine whether it can mark that temp
1317 : // as non-escaping or not. For general varargs calls it is not always
1318 : // safe to stack-allocated the storage, but for specific cases (ex:
1319 : // call to append()) it is legal.
1320 : enum Slice_storage_escape_disp
1321 : {
1322 : SLICE_STORAGE_MAY_ESCAPE,
1323 : SLICE_STORAGE_DOES_NOT_ESCAPE
1324 : };
1325 :
1326 : private:
1327 : // Convert to the desired statement classification, or return NULL.
1328 : // This is a controlled dynamic cast.
1329 : template<typename Expression_class,
1330 : Expression_classification expr_classification>
1331 : Expression_class*
1332 958358785 : convert()
1333 : {
1334 343625914 : return (this->classification_ == expr_classification
1335 333470560 : ? static_cast<Expression_class*>(this)
1336 : : NULL);
1337 : }
1338 :
1339 : template<typename Expression_class,
1340 : Expression_classification expr_classification>
1341 : const Expression_class*
1342 53400733 : convert() const
1343 : {
1344 925147 : return (this->classification_ == expr_classification
1345 27376040 : ? static_cast<const Expression_class*>(this)
1346 : : NULL);
1347 : }
1348 :
1349 : static Expression*
1350 : convert_interface_to_type(Gogo*, Type*, Expression*, Location);
1351 :
1352 : static Expression*
1353 : import_identifier(Import_function_body*, Location);
1354 :
1355 : static Expression*
1356 : import_expression_without_suffix(Import_expression*, Location);
1357 :
1358 : // The expression classification.
1359 : Expression_classification classification_;
1360 : // The location in the input file.
1361 : Location location_;
1362 : };
1363 :
1364 : // A list of Expressions.
1365 :
1366 2378316 : class Expression_list
1367 : {
1368 : public:
1369 7708625 : Expression_list()
1370 7065848 : : entries_()
1371 : { }
1372 :
1373 : // Return whether the list is empty.
1374 : bool
1375 5244514 : empty() const
1376 5244514 : { return this->entries_.empty(); }
1377 :
1378 : // Return the number of entries in the list.
1379 : size_t
1380 7633535 : size() const
1381 8152338 : { return this->entries_.size(); }
1382 :
1383 : // Add an entry to the end of the list.
1384 : void
1385 18739560 : push_back(Expression* expr)
1386 18124698 : { this->entries_.push_back(expr); }
1387 :
1388 : void
1389 401 : append(Expression_list* add)
1390 802 : { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
1391 :
1392 : // Reserve space in the list.
1393 : void
1394 2534462 : reserve(size_t size)
1395 2534462 : { this->entries_.reserve(size); }
1396 :
1397 : // Traverse the expressions in the list.
1398 : int
1399 : traverse(Traverse*);
1400 :
1401 : // Copy the list.
1402 : Expression_list*
1403 : copy();
1404 :
1405 : // Return true if the list contains an error expression.
1406 : bool
1407 : contains_error() const;
1408 :
1409 : // Retrieve an element by index.
1410 : Expression*&
1411 7624 : at(size_t i)
1412 2373792 : { return this->entries_.at(i); }
1413 :
1414 : // Return the first and last elements.
1415 : Expression*&
1416 1874013 : front()
1417 1828358 : { return this->entries_.front(); }
1418 :
1419 : Expression*
1420 1397282 : front() const
1421 1279239 : { return this->entries_.front(); }
1422 :
1423 : Expression*&
1424 22594 : back()
1425 22594 : { return this->entries_.back(); }
1426 :
1427 : Expression*
1428 94078 : back() const
1429 79325 : { return this->entries_.back(); }
1430 :
1431 : // Iterators.
1432 :
1433 : typedef std::vector<Expression*>::iterator iterator;
1434 : typedef std::vector<Expression*>::const_iterator const_iterator;
1435 :
1436 : iterator
1437 38921939 : begin()
1438 34574175 : { return this->entries_.begin(); }
1439 :
1440 : const_iterator
1441 347600 : begin() const
1442 347600 : { return this->entries_.begin(); }
1443 :
1444 : iterator
1445 144432299 : end()
1446 106606438 : { return this->entries_.end(); }
1447 :
1448 : const_iterator
1449 777102 : end() const
1450 763568 : { return this->entries_.end(); }
1451 :
1452 : // Erase an entry.
1453 : void
1454 : erase(iterator p)
1455 : { this->entries_.erase(p); }
1456 :
1457 : private:
1458 : std::vector<Expression*> entries_;
1459 : };
1460 :
1461 : // An abstract base class for an expression which is only used by the
1462 : // parser, and is lowered in the lowering pass.
1463 :
1464 : class Parser_expression : public Expression
1465 : {
1466 : public:
1467 1829787 : Parser_expression(Expression_classification classification,
1468 : Location location)
1469 820310 : : Expression(classification, location)
1470 : { }
1471 :
1472 : protected:
1473 : virtual Expression*
1474 : do_lower(Gogo*, Named_object*, Statement_inserter*) = 0;
1475 :
1476 : Bexpression*
1477 0 : do_get_backend(Translate_context*)
1478 0 : { go_unreachable(); }
1479 : };
1480 :
1481 : // A reference to a const in an expression.
1482 :
1483 : class Const_expression : public Expression
1484 : {
1485 : public:
1486 450984 : Const_expression(Named_object* constant, Location location)
1487 450984 : : Expression(EXPRESSION_CONST_REFERENCE, location),
1488 450984 : constant_(constant), type_(NULL), iota_value_(0), seen_(false),
1489 450984 : is_iota_(false)
1490 : { }
1491 :
1492 : Named_object*
1493 75155 : named_object()
1494 75155 : { return this->constant_; }
1495 :
1496 : const Named_object*
1497 38611 : named_object() const
1498 38611 : { return this->constant_; }
1499 :
1500 : // Check that the initializer does not refer to the constant itself.
1501 : void
1502 : check_for_init_loop();
1503 :
1504 : // Set the iota value if this is a reference to iota.
1505 : void
1506 : set_iota_value(int);
1507 :
1508 : protected:
1509 : int
1510 : do_traverse(Traverse*);
1511 :
1512 : Expression*
1513 : do_lower(Gogo*, Named_object*, Statement_inserter*);
1514 :
1515 : bool
1516 241130 : do_is_constant() const
1517 241130 : { return true; }
1518 :
1519 : bool
1520 : do_is_untyped(Type**) const;
1521 :
1522 : bool
1523 : do_is_zero_value() const;
1524 :
1525 : bool
1526 70369 : do_is_static_initializer() const
1527 70369 : { return true; }
1528 :
1529 : bool
1530 : do_numeric_constant_value(Numeric_constant* nc);
1531 :
1532 : bool
1533 : do_string_constant_value(std::string* val);
1534 :
1535 : bool
1536 : do_boolean_constant_value(bool* val);
1537 :
1538 : Type*
1539 : do_type();
1540 :
1541 : // The type of a const is set by the declaration, not the use.
1542 : void
1543 : do_determine_type(Gogo*, const Type_context*);
1544 :
1545 : void
1546 : do_check_types(Gogo*);
1547 :
1548 : Expression*
1549 31814 : do_copy()
1550 31814 : { return this; }
1551 :
1552 : Bexpression*
1553 : do_get_backend(Translate_context* context);
1554 :
1555 : int
1556 494312 : do_inlining_cost() const
1557 494312 : { return 1; }
1558 :
1559 : // When exporting a reference to a const as part of a const
1560 : // expression, we export the value. We ignore the fact that it has
1561 : // a name.
1562 : void
1563 : do_export(Export_function_body* efb) const;
1564 :
1565 : void
1566 : do_dump_expression(Ast_dump_context*) const;
1567 :
1568 : private:
1569 : // The constant.
1570 : Named_object* constant_;
1571 : // The type of this reference. This is used if the constant has an
1572 : // abstract type.
1573 : Type* type_;
1574 : // If this const is a reference to the predeclared iota value, the
1575 : // value to use.
1576 : int iota_value_;
1577 : // Used to prevent infinite recursion when a constant incorrectly
1578 : // refers to itself.
1579 : mutable bool seen_;
1580 : // Whether this const is a reference to the predeclared iota value.
1581 : bool is_iota_;
1582 : };
1583 :
1584 : // An expression which is simply a variable.
1585 :
1586 : class Var_expression : public Expression
1587 : {
1588 : public:
1589 3502090 : Var_expression(Named_object* variable, Location location)
1590 3502090 : : Expression(EXPRESSION_VAR_REFERENCE, location),
1591 3502090 : variable_(variable)
1592 : { }
1593 :
1594 : // Return the variable.
1595 : Named_object*
1596 43728857 : named_object() const
1597 43728857 : { return this->variable_; }
1598 :
1599 : protected:
1600 : Expression*
1601 : do_lower(Gogo*, Named_object*, Statement_inserter*);
1602 :
1603 : Type*
1604 : do_type();
1605 :
1606 : void
1607 : do_determine_type(Gogo*, const Type_context*);
1608 :
1609 : Expression*
1610 431131 : do_copy()
1611 431131 : { return this; }
1612 :
1613 : int
1614 2017316 : do_inlining_cost() const
1615 2017316 : { return 1; }
1616 :
1617 : void
1618 : do_export(Export_function_body*) const;
1619 :
1620 : bool
1621 530551 : do_is_addressable() const
1622 530551 : { return true; }
1623 :
1624 : void
1625 : do_address_taken(bool);
1626 :
1627 : Bexpression*
1628 : do_get_backend(Translate_context*);
1629 :
1630 : void
1631 : do_dump_expression(Ast_dump_context*) const;
1632 :
1633 : private:
1634 : // The variable we are referencing.
1635 : Named_object* variable_;
1636 : };
1637 :
1638 : // A reference to a variable within an enclosing function.
1639 :
1640 : class Enclosed_var_expression : public Expression
1641 : {
1642 : public:
1643 43132 : Enclosed_var_expression(Expression* reference, Named_object* variable,
1644 : Location location)
1645 43132 : : Expression(EXPRESSION_ENCLOSED_VAR_REFERENCE, location),
1646 43132 : reference_(reference), variable_(variable)
1647 : { }
1648 :
1649 : // The reference to the enclosed variable. This will be an indirection of the
1650 : // the field stored within closure variable.
1651 : Expression*
1652 : reference() const
1653 : { return this->reference_; }
1654 :
1655 : // The variable being enclosed and referenced.
1656 : Named_object*
1657 324232 : variable() const
1658 324232 : { return this->variable_; }
1659 :
1660 : protected:
1661 : int
1662 : do_traverse(Traverse*);
1663 :
1664 : Expression*
1665 : do_lower(Gogo*, Named_object*, Statement_inserter*);
1666 :
1667 : Expression*
1668 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
1669 :
1670 : Type*
1671 938572 : do_type()
1672 938572 : { return this->reference_->type(); }
1673 :
1674 : void
1675 89220 : do_determine_type(Gogo* gogo, const Type_context* context)
1676 89220 : { return this->reference_->determine_type(gogo, context); }
1677 :
1678 : Expression*
1679 427 : do_copy()
1680 427 : { return this; }
1681 :
1682 : bool
1683 13278 : do_is_addressable() const
1684 13278 : { return this->reference_->is_addressable(); }
1685 :
1686 : void
1687 : do_address_taken(bool escapes);
1688 :
1689 : Bexpression*
1690 45673 : do_get_backend(Translate_context* context)
1691 45673 : { return this->reference_->get_backend(context); }
1692 :
1693 : void
1694 : do_dump_expression(Ast_dump_context*) const;
1695 :
1696 : private:
1697 : // The reference to the enclosed variable.
1698 : Expression* reference_;
1699 : // The variable being enclosed.
1700 : Named_object* variable_;
1701 : };
1702 :
1703 : // A reference to a temporary variable.
1704 :
1705 : class Temporary_reference_expression : public Expression
1706 : {
1707 : public:
1708 3860104 : Temporary_reference_expression(Temporary_statement* statement,
1709 : Location location)
1710 3860104 : : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
1711 3860104 : statement_(statement), is_lvalue_(false)
1712 : { }
1713 :
1714 : // The temporary that this expression refers to.
1715 : Temporary_statement*
1716 2179999 : statement() const
1717 2179999 : { return this->statement_; }
1718 :
1719 : // Indicate that this reference appears on the left hand side of an
1720 : // assignment statement.
1721 : void
1722 94674 : set_is_lvalue()
1723 94573 : { this->is_lvalue_ = true; }
1724 :
1725 : static Expression*
1726 : do_import(Import_function_body*, Location);
1727 :
1728 : protected:
1729 : Type*
1730 : do_type();
1731 :
1732 : void
1733 3889664 : do_determine_type(Gogo*, const Type_context*)
1734 3889664 : { }
1735 :
1736 : Expression*
1737 887384 : do_copy()
1738 887384 : { return make_temporary_reference(this->statement_, this->location()); }
1739 :
1740 : int
1741 339255 : do_inlining_cost() const
1742 339255 : { return 1; }
1743 :
1744 : void
1745 : do_export(Export_function_body*) const;
1746 :
1747 : bool
1748 1003 : do_is_addressable() const
1749 1003 : { return true; }
1750 :
1751 : void
1752 : do_address_taken(bool);
1753 :
1754 : Bexpression*
1755 : do_get_backend(Translate_context*);
1756 :
1757 : void
1758 : do_dump_expression(Ast_dump_context*) const;
1759 :
1760 : private:
1761 : // The statement where the temporary variable is defined.
1762 : Temporary_statement* statement_;
1763 : // Whether this reference appears on the left hand side of an
1764 : // assignment statement.
1765 : bool is_lvalue_;
1766 : };
1767 :
1768 : // Set and use a temporary variable.
1769 :
1770 : class Set_and_use_temporary_expression : public Expression
1771 : {
1772 : public:
1773 19060 : Set_and_use_temporary_expression(Temporary_statement* statement,
1774 : Expression* expr, Location location)
1775 19060 : : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1776 19060 : statement_(statement), expr_(expr)
1777 : { }
1778 :
1779 : // Return the temporary.
1780 : Temporary_statement*
1781 619 : temporary() const
1782 619 : { return this->statement_; }
1783 :
1784 : // Return the expression.
1785 : Expression*
1786 619 : expression() const
1787 619 : { return this->expr_; }
1788 :
1789 : protected:
1790 : int
1791 161291 : do_traverse(Traverse* traverse)
1792 161291 : { return Expression::traverse(&this->expr_, traverse); }
1793 :
1794 : Type*
1795 : do_type();
1796 :
1797 : void
1798 : do_determine_type(Gogo*, const Type_context*);
1799 :
1800 : Expression*
1801 0 : do_copy()
1802 : {
1803 0 : return make_set_and_use_temporary(this->statement_, this->expr_,
1804 0 : this->location());
1805 : }
1806 :
1807 : bool
1808 17988 : do_must_eval_in_order() const
1809 17988 : { return true; }
1810 :
1811 : bool
1812 1560 : do_is_addressable() const
1813 1560 : { return true; }
1814 :
1815 : void
1816 : do_address_taken(bool);
1817 :
1818 : Bexpression*
1819 : do_get_backend(Translate_context*);
1820 :
1821 : void
1822 : do_dump_expression(Ast_dump_context*) const;
1823 :
1824 : private:
1825 : // The statement where the temporary variable is defined.
1826 : Temporary_statement* statement_;
1827 : // The expression to assign to the temporary.
1828 : Expression* expr_;
1829 : };
1830 :
1831 : // A string expression.
1832 :
1833 : class String_expression : public Expression
1834 : {
1835 : public:
1836 1598486 : String_expression(const std::string& val, Type* type, Location location)
1837 1598486 : : Expression(EXPRESSION_STRING, location),
1838 1598486 : val_(val), type_(type)
1839 1598486 : { }
1840 :
1841 : const std::string&
1842 9760 : val() const
1843 9760 : { return this->val_; }
1844 :
1845 : static Expression*
1846 : do_import(Import_expression*, Location);
1847 :
1848 : protected:
1849 : int
1850 : do_traverse(Traverse*);
1851 :
1852 : bool
1853 102971102 : do_is_constant() const
1854 102971102 : { return true; }
1855 :
1856 : bool
1857 : do_is_untyped(Type**) const;
1858 :
1859 : bool
1860 16 : do_is_zero_value() const
1861 16 : { return this->val_ == ""; }
1862 :
1863 : bool
1864 853949 : do_is_static_initializer() const
1865 853949 : { return true; }
1866 :
1867 : bool
1868 121574 : do_string_constant_value(std::string* val)
1869 : {
1870 121574 : *val = this->val_;
1871 121574 : return true;
1872 : }
1873 :
1874 : Type*
1875 : do_type();
1876 :
1877 : void
1878 : do_determine_type(Gogo*, const Type_context*);
1879 :
1880 : Expression*
1881 2 : do_copy()
1882 2 : { return this; }
1883 :
1884 : Bexpression*
1885 : do_get_backend(Translate_context*);
1886 :
1887 : // Write string literal to a string dump.
1888 : static void
1889 : export_string(String_dump* exp, const String_expression* str);
1890 :
1891 : // Set the inlining cost a bit high since inlining may cause
1892 : // duplicated string literals.
1893 : int
1894 85551 : do_inlining_cost() const
1895 85551 : { return 5; }
1896 :
1897 : void
1898 : do_export(Export_function_body*) const;
1899 :
1900 : void
1901 : do_dump_expression(Ast_dump_context*) const;
1902 :
1903 : private:
1904 : // The string value. This is immutable.
1905 : const std::string val_;
1906 : // The type as determined by context.
1907 : Type* type_;
1908 : };
1909 :
1910 : // A type conversion expression.
1911 :
1912 : class Type_conversion_expression : public Expression
1913 : {
1914 : public:
1915 3018065 : Type_conversion_expression(Type* type, Expression* expr,
1916 : Location location)
1917 3018065 : : Expression(EXPRESSION_CONVERSION, location),
1918 3018065 : type_(type), expr_(expr), may_convert_function_types_(false),
1919 3018065 : no_copy_(false), no_escape_(false)
1920 : { }
1921 :
1922 : // Return the type to which we are converting.
1923 : Type*
1924 4498975 : type() const
1925 4498975 : { return this->type_; }
1926 :
1927 : // Return the expression which we are converting.
1928 : Expression*
1929 2020925 : expr() const
1930 2020925 : { return this->expr_; }
1931 :
1932 : // Permit converting from one function type to another. This is
1933 : // used internally for method expressions.
1934 : void
1935 : set_may_convert_function_types()
1936 : {
1937 : this->may_convert_function_types_ = true;
1938 : }
1939 :
1940 : // Mark string([]byte) conversion to reuse the backing store
1941 : // without copying.
1942 : void
1943 7919 : set_no_copy(bool b)
1944 1970 : { this->no_copy_ = b; };
1945 :
1946 : // Import a type conversion expression.
1947 : static Expression*
1948 : do_import(Import_expression*, Location);
1949 :
1950 : protected:
1951 : int
1952 : do_traverse(Traverse* traverse);
1953 :
1954 : Expression*
1955 : do_lower(Gogo*, Named_object*, Statement_inserter*);
1956 :
1957 : Expression*
1958 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
1959 :
1960 : bool
1961 : do_is_constant() const;
1962 :
1963 : bool
1964 : do_is_zero_value() const;
1965 :
1966 : bool
1967 : do_is_static_initializer() const;
1968 :
1969 : bool
1970 : do_numeric_constant_value(Numeric_constant*);
1971 :
1972 : bool
1973 : do_string_constant_value(std::string*);
1974 :
1975 : bool
1976 : do_boolean_constant_value(bool*);
1977 :
1978 : Type*
1979 : do_type();
1980 :
1981 : void
1982 : do_determine_type(Gogo*, const Type_context*);
1983 :
1984 : void
1985 : do_check_types(Gogo*);
1986 :
1987 : Expression*
1988 : do_copy();
1989 :
1990 : Bexpression*
1991 : do_get_backend(Translate_context* context);
1992 :
1993 : int
1994 : do_inlining_cost() const;
1995 :
1996 : void
1997 : do_export(Export_function_body*) const;
1998 :
1999 : void
2000 : do_dump_expression(Ast_dump_context*) const;
2001 :
2002 : private:
2003 : // The type to convert to.
2004 : Type* type_;
2005 : // The expression to convert.
2006 : Expression* expr_;
2007 : // True if this is permitted to convert function types. This is
2008 : // used internally for method expressions.
2009 : bool may_convert_function_types_;
2010 : // True if a string([]byte) conversion can reuse the backing store
2011 : // without copying. Only used in string([]byte) conversion.
2012 : bool no_copy_;
2013 : // True if a conversion does not escape. Used in type-to-interface
2014 : // conversions and slice-to/from-string conversions.
2015 : bool no_escape_;
2016 : };
2017 :
2018 : // An unsafe type conversion, used to pass values to builtin functions.
2019 :
2020 : class Unsafe_type_conversion_expression : public Expression
2021 : {
2022 : public:
2023 780304 : Unsafe_type_conversion_expression(Type* type, Expression* expr,
2024 : Location location)
2025 780304 : : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
2026 780304 : type_(type), expr_(expr)
2027 : { }
2028 :
2029 : Expression*
2030 116827 : expr() const
2031 116827 : { return this->expr_; }
2032 :
2033 : protected:
2034 : int
2035 : do_traverse(Traverse* traverse);
2036 :
2037 : bool
2038 3 : do_is_zero_value() const
2039 3 : { return this->expr_->is_zero_value(); }
2040 :
2041 : bool
2042 : do_is_static_initializer() const;
2043 :
2044 : Type*
2045 1612579 : do_type()
2046 1612579 : { return this->type_; }
2047 :
2048 : void
2049 514875 : do_determine_type(Gogo* gogo, const Type_context*)
2050 514875 : { this->expr_->determine_type_no_context(gogo); }
2051 :
2052 : Expression*
2053 : do_copy();
2054 :
2055 : Bexpression*
2056 : do_get_backend(Translate_context*);
2057 :
2058 : void
2059 : do_dump_expression(Ast_dump_context*) const;
2060 :
2061 : private:
2062 : // The type to convert to.
2063 : Type* type_;
2064 : // The expression to convert.
2065 : Expression* expr_;
2066 : };
2067 :
2068 : // A Unary expression.
2069 :
2070 : class Unary_expression : public Expression
2071 : {
2072 : public:
2073 3785625 : Unary_expression(Operator op, Expression* expr, Location location)
2074 3785625 : : Expression(EXPRESSION_UNARY, location),
2075 3785625 : op_(op), escapes_(true), create_temp_(false), is_gc_root_(false),
2076 3785625 : is_slice_init_(false), expr_(expr), type_(NULL),
2077 3785625 : issue_nil_check_(NIL_CHECK_DEFAULT)
2078 3785625 : { }
2079 :
2080 : // Return the operator.
2081 : Operator
2082 4808621 : op() const
2083 4802538 : { return this->op_; }
2084 :
2085 : // Return the operand.
2086 : Expression*
2087 9143249 : operand() const
2088 9143249 : { return this->expr_; }
2089 :
2090 : // Record that an address expression does not escape.
2091 : void
2092 112980 : set_does_not_escape()
2093 : {
2094 112980 : go_assert(this->op_ == OPERATOR_AND);
2095 112980 : this->escapes_ = false;
2096 112980 : }
2097 :
2098 : // Record that this is an address expression which should create a
2099 : // temporary variable if necessary. This is used for method calls.
2100 : void
2101 35412 : set_create_temp()
2102 : {
2103 35412 : go_assert(this->op_ == OPERATOR_AND);
2104 35412 : this->create_temp_ = true;
2105 35412 : }
2106 :
2107 : // Record that this is an address expression of a GC root, which is a
2108 : // mutable composite literal. This used for registering GC variables.
2109 : void
2110 2111 : set_is_gc_root()
2111 : {
2112 2111 : go_assert(this->op_ == OPERATOR_AND);
2113 2111 : this->is_gc_root_ = true;
2114 2111 : }
2115 :
2116 : // Record that this is an address expression of a slice value initializer,
2117 : // which is mutable if the values are not copied to the heap.
2118 : void
2119 287144 : set_is_slice_init()
2120 : {
2121 287144 : go_assert(this->op_ == OPERATOR_AND);
2122 287144 : this->is_slice_init_ = true;
2123 287144 : }
2124 :
2125 : // Call the address_taken method on the operand if necessary.
2126 : void
2127 : check_operand_address_taken(Gogo*);
2128 :
2129 : // Apply unary opcode OP to UNC, setting NC. Return true if this
2130 : // could be done, false if not. On overflow, issues an error and
2131 : // sets *ISSUED_ERROR.
2132 : static bool
2133 : eval_constant(Type*, Operator op, const Numeric_constant* unc,
2134 : Location, Numeric_constant* nc, bool *issued_error);
2135 :
2136 : static Expression*
2137 : do_import(Import_expression*, Location);
2138 :
2139 : // Declare that this deref does or does not require an explicit nil check.
2140 : void
2141 515813 : set_requires_nil_check(bool needed)
2142 : {
2143 515813 : go_assert(this->op_ == OPERATOR_MULT);
2144 515813 : if (needed)
2145 81496 : this->issue_nil_check_ = NIL_CHECK_NEEDED;
2146 : else
2147 434317 : this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED;
2148 515813 : }
2149 :
2150 : protected:
2151 : int
2152 12628342 : do_traverse(Traverse* traverse)
2153 12628342 : { return Expression::traverse(&this->expr_, traverse); }
2154 :
2155 : Expression*
2156 : do_lower(Gogo*, Named_object*, Statement_inserter*);
2157 :
2158 : Expression*
2159 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
2160 :
2161 : bool
2162 : do_is_constant() const;
2163 :
2164 : bool
2165 : do_is_untyped(Type**) const;
2166 :
2167 : bool
2168 : do_is_static_initializer() const;
2169 :
2170 : bool
2171 : do_numeric_constant_value(Numeric_constant*);
2172 :
2173 : bool
2174 : do_boolean_constant_value(bool*);
2175 :
2176 : Type*
2177 : do_type();
2178 :
2179 : void
2180 : do_determine_type(Gogo*, const Type_context*);
2181 :
2182 : void
2183 : do_check_types(Gogo*);
2184 :
2185 : Expression*
2186 109175 : do_copy()
2187 : {
2188 109175 : return Expression::make_unary(this->op_, this->expr_->copy(),
2189 109175 : this->location());
2190 : }
2191 :
2192 : bool
2193 14743 : do_must_eval_subexpressions_in_order(int*) const
2194 14743 : { return this->op_ == OPERATOR_MULT; }
2195 :
2196 : bool
2197 191080 : do_is_addressable() const
2198 191080 : { return this->op_ == OPERATOR_MULT; }
2199 :
2200 : Bexpression*
2201 : do_get_backend(Translate_context*);
2202 :
2203 : int
2204 185884 : do_inlining_cost() const
2205 185884 : { return 1; }
2206 :
2207 : void
2208 : do_export(Export_function_body*) const;
2209 :
2210 : void
2211 : do_dump_expression(Ast_dump_context*) const;
2212 :
2213 : void
2214 68361 : do_issue_nil_check()
2215 : {
2216 68361 : if (this->op_ == OPERATOR_MULT)
2217 68361 : this->set_requires_nil_check(true);
2218 68361 : }
2219 :
2220 : private:
2221 : static bool
2222 : base_is_static_initializer(Expression*);
2223 :
2224 : // Return a determination as to whether this dereference expression
2225 : // requires a nil check.
2226 : Nil_check_classification
2227 : requires_nil_check(Gogo*);
2228 :
2229 : // The unary operator to apply.
2230 : Operator op_;
2231 : // Normally true. False if this is an address expression which does
2232 : // not escape the current function.
2233 : bool escapes_;
2234 : // True if this is an address expression which should create a
2235 : // temporary variable if necessary.
2236 : bool create_temp_;
2237 : // True if this is an address expression for a GC root. A GC root is a
2238 : // special struct composite literal that is mutable when addressed, meaning
2239 : // it cannot be represented as an immutable_struct in the backend.
2240 : bool is_gc_root_;
2241 : // True if this is an address expression for a slice value with an immutable
2242 : // initializer. The initializer for a slice's value pointer has an array
2243 : // type, meaning it cannot be represented as an immutable_struct in the
2244 : // backend.
2245 : bool is_slice_init_;
2246 : // The operand.
2247 : Expression* expr_;
2248 : // The type of the expression. Not used for AND and MULT.
2249 : Type* type_;
2250 : // Whether or not to issue a nil check for this expression if its address
2251 : // is being taken.
2252 : Nil_check_classification issue_nil_check_;
2253 : };
2254 :
2255 : // A binary expression.
2256 :
2257 : class Binary_expression : public Expression
2258 : {
2259 : public:
2260 2341942 : Binary_expression(Operator op, Expression* left, Expression* right,
2261 : Location location)
2262 2341942 : : Expression(EXPRESSION_BINARY, location),
2263 2341942 : op_(op), left_(left), right_(right), type_(NULL)
2264 : { }
2265 :
2266 : // Return the operator.
2267 : Operator
2268 1777939 : op()
2269 1777939 : { return this->op_; }
2270 :
2271 : // Return the left hand expression.
2272 : Expression*
2273 275178 : left()
2274 275178 : { return this->left_; }
2275 :
2276 : // Return the right hand expression.
2277 : Expression*
2278 275178 : right()
2279 275178 : { return this->right_; }
2280 :
2281 : // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
2282 : // Return true if this could be done, false if not. Issue errors at
2283 : // LOCATION as appropriate, and sets *ISSUED_ERROR if it did.
2284 : static bool
2285 : eval_constant(Operator op, Numeric_constant* left_nc,
2286 : Numeric_constant* right_nc, Location location,
2287 : Numeric_constant* nc, bool* issued_error);
2288 :
2289 : // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
2290 : // *RESULT. Return true if this could be done, false if not. Issue
2291 : // errors at LOCATION as appropriate.
2292 : static bool
2293 : compare_constant(Operator op, Numeric_constant* left_nc,
2294 : Numeric_constant* right_nc, Location location,
2295 : bool* result);
2296 :
2297 : static Expression*
2298 : do_import(Import_expression*, Location);
2299 :
2300 : // Report an error if OP can not be applied to TYPE. Return whether
2301 : // it can. OTYPE is the type of the other operand.
2302 : static bool
2303 : check_operator_type(Operator op, Type* type, Type* otype, Location);
2304 :
2305 : // Set *RESULT_TYPE to the resulting type when OP is applied to
2306 : // operands of type LEFT_TYPE and RIGHT_TYPE. Return true on
2307 : // success, false on failure.
2308 : static bool
2309 : operation_type(Operator op, Type* left_type, Type* right_type,
2310 : Type** result_type);
2311 :
2312 : protected:
2313 : int
2314 : do_traverse(Traverse* traverse);
2315 :
2316 : Expression*
2317 : do_lower(Gogo*, Named_object*, Statement_inserter*);
2318 :
2319 : Expression*
2320 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
2321 :
2322 : bool
2323 103807626 : do_is_constant() const
2324 103807626 : { return this->left_->is_constant() && this->right_->is_constant(); }
2325 :
2326 : bool
2327 : do_is_untyped(Type**) const;
2328 :
2329 : bool
2330 : do_is_static_initializer() const;
2331 :
2332 : bool
2333 : do_numeric_constant_value(Numeric_constant*);
2334 :
2335 : bool
2336 : do_boolean_constant_value(bool*);
2337 :
2338 : bool
2339 : do_discarding_value();
2340 :
2341 : Type*
2342 : do_type();
2343 :
2344 : void
2345 : do_determine_type(Gogo*, const Type_context*);
2346 :
2347 : void
2348 : do_check_types(Gogo*);
2349 :
2350 : Expression*
2351 2753 : do_copy()
2352 : {
2353 2753 : return Expression::make_binary(this->op_, this->left_->copy(),
2354 2753 : this->right_->copy(), this->location());
2355 : }
2356 :
2357 : Bexpression*
2358 : do_get_backend(Translate_context*);
2359 :
2360 : int
2361 1134099 : do_inlining_cost() const
2362 1134099 : { return 1; }
2363 :
2364 : void
2365 : do_export(Export_function_body*) const;
2366 :
2367 : void
2368 : do_dump_expression(Ast_dump_context*) const;
2369 :
2370 : private:
2371 : static bool
2372 : cmp_to_bool(Operator op, int cmp);
2373 :
2374 : static bool
2375 : eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
2376 : Location, Numeric_constant*, bool* issued_error);
2377 :
2378 : static bool
2379 : eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
2380 : Location, Numeric_constant*, bool* issued_error);
2381 :
2382 : static bool
2383 : eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
2384 : Location, Numeric_constant*, bool* issued_error);
2385 :
2386 : static bool
2387 : compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
2388 :
2389 : static bool
2390 : compare_float(const Numeric_constant*, const Numeric_constant *, int*);
2391 :
2392 : static bool
2393 : compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
2394 :
2395 : Expression*
2396 : lower_struct_comparison(Gogo*, Statement_inserter*);
2397 :
2398 : Expression*
2399 : lower_array_comparison(Gogo*, Statement_inserter*);
2400 :
2401 : Expression*
2402 : lower_interface_value_comparison(Gogo*, Statement_inserter*);
2403 :
2404 : Expression*
2405 : lower_compare_to_memcmp(Gogo*, Statement_inserter*);
2406 :
2407 : Expression*
2408 : operand_address(Statement_inserter*, Expression*);
2409 :
2410 : // The binary operator to apply.
2411 : Operator op_;
2412 : // The left hand side operand.
2413 : Expression* left_;
2414 : // The right hand side operand.
2415 : Expression* right_;
2416 : // The type of the expression.
2417 : Type* type_;
2418 : };
2419 :
2420 : // A string concatenation expression. This is a sequence of strings
2421 : // added together. It is created when lowering Binary_expression.
2422 :
2423 : class String_concat_expression : public Expression
2424 : {
2425 : public:
2426 16280 : String_concat_expression(Expression_list* exprs)
2427 16280 : : Expression(EXPRESSION_STRING_CONCAT, exprs->front()->location()),
2428 16280 : exprs_(exprs)
2429 16280 : { }
2430 :
2431 : // Return the list of string expressions to be concatenated.
2432 : Expression_list*
2433 5760 : exprs()
2434 5760 : { return this->exprs_; }
2435 :
2436 : protected:
2437 : int
2438 157482 : do_traverse(Traverse* traverse)
2439 157482 : { return this->exprs_->traverse(traverse); }
2440 :
2441 : Expression*
2442 27149 : do_lower(Gogo*, Named_object*, Statement_inserter*)
2443 27149 : { return this; }
2444 :
2445 : Expression*
2446 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
2447 :
2448 : bool
2449 : do_is_constant() const;
2450 :
2451 : bool
2452 : do_is_untyped(Type**) const;
2453 :
2454 : bool
2455 : do_is_zero_value() const;
2456 :
2457 : bool
2458 : do_is_static_initializer() const;
2459 :
2460 : Type*
2461 : do_type();
2462 :
2463 : void
2464 : do_determine_type(Gogo*, const Type_context*);
2465 :
2466 : void
2467 : do_check_types(Gogo*);
2468 :
2469 : Expression*
2470 0 : do_copy()
2471 0 : { return Expression::make_string_concat(this->exprs_->copy()); }
2472 :
2473 : Bexpression*
2474 0 : do_get_backend(Translate_context*)
2475 0 : { go_unreachable(); }
2476 :
2477 : void
2478 0 : do_export(Export_function_body*) const
2479 0 : { go_unreachable(); }
2480 :
2481 : void
2482 : do_dump_expression(Ast_dump_context*) const;
2483 :
2484 : private:
2485 : // The string expressions to concatenate.
2486 : Expression_list* exprs_;
2487 : };
2488 :
2489 : // A call expression. The go statement needs to dig inside this.
2490 :
2491 : class Call_expression : public Expression
2492 : {
2493 : public:
2494 2369723 : Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
2495 : Location location)
2496 2369723 : : Expression(EXPRESSION_CALL, location),
2497 2369723 : fn_(fn), args_(args), type_(NULL), lowered_(NULL), call_(NULL),
2498 2369723 : call_temp_(NULL), expected_result_count_(0), is_varargs_(is_varargs),
2499 2369723 : varargs_are_lowered_(false), types_are_determined_(false),
2500 2369723 : is_deferred_(false), is_concurrent_(false), is_equal_function_(false),
2501 2369723 : issued_error_(false), is_multi_value_arg_(false), is_flattened_(false)
2502 2369723 : { }
2503 :
2504 : // The function to call.
2505 : Expression*
2506 6278201 : fn() const
2507 6278201 : { return this->fn_; }
2508 :
2509 : // The arguments.
2510 : Expression_list*
2511 9011661 : args()
2512 8963195 : { return this->args_; }
2513 :
2514 : const Expression_list*
2515 655967 : args() const
2516 655967 : { return this->args_; }
2517 :
2518 : // Get the function type.
2519 : Function_type*
2520 : get_function_type() const;
2521 :
2522 : // Return the number of values this call will return.
2523 : size_t
2524 : result_count() const;
2525 :
2526 : // Return the temporary variable that holds the results. This is
2527 : // only valid after the expression has been lowered, and is only
2528 : // valid for calls which return multiple results.
2529 : Temporary_statement*
2530 : results() const;
2531 :
2532 : // Set the number of results expected from this call. This is used
2533 : // when the call appears in a context that expects multiple results,
2534 : // such as a, b = f(). This must be called before the
2535 : // determine_types pass.
2536 : void
2537 : set_expected_result_count(size_t);
2538 :
2539 : // Return whether this is a call to the predeclared function
2540 : // recover.
2541 : bool
2542 : is_recover_call() const;
2543 :
2544 : // Set the argument for a call to recover.
2545 : void
2546 : set_recover_arg(Expression*);
2547 :
2548 : // Whether the last argument is a varargs argument (f(a...)).
2549 : bool
2550 224002 : is_varargs() const
2551 224002 : { return this->is_varargs_; }
2552 :
2553 : // Return whether varargs have already been lowered.
2554 : bool
2555 0 : varargs_are_lowered() const
2556 0 : { return this->varargs_are_lowered_; }
2557 :
2558 : // Note that varargs have already been lowered.
2559 : void
2560 33374 : set_varargs_are_lowered()
2561 33374 : { this->varargs_are_lowered_ = true; }
2562 :
2563 : // Whether this call is being deferred.
2564 : bool
2565 55493 : is_deferred() const
2566 55493 : { return this->is_deferred_; }
2567 :
2568 : // Note that the call is being deferred.
2569 : void
2570 20536 : set_is_deferred()
2571 20536 : { this->is_deferred_ = true; }
2572 :
2573 : // Whether this call is concurrently executed.
2574 : bool
2575 54469 : is_concurrent() const
2576 54469 : { return this->is_concurrent_; }
2577 :
2578 : // Note that the call is concurrently executed.
2579 : void
2580 4968 : set_is_concurrent()
2581 4968 : { this->is_concurrent_ = true; }
2582 :
2583 : // Note that this is a call to a generated equality function.
2584 : void
2585 961 : set_is_equal_function()
2586 961 : { this->is_equal_function_ = true; }
2587 :
2588 : // We have found an error with this call expression; return true if
2589 : // we should report it.
2590 : bool
2591 : issue_error();
2592 :
2593 : // Whether or not this call contains errors, either in the call or the
2594 : // arguments to the call.
2595 : bool
2596 : is_erroneous_call();
2597 :
2598 : // Whether this call returns multiple results that are used as an
2599 : // multi-valued argument.
2600 : bool
2601 666 : is_multi_value_arg() const
2602 666 : { return this->is_multi_value_arg_; }
2603 :
2604 : // Note this call is used as a multi-valued argument.
2605 : void
2606 300 : set_is_multi_value_arg()
2607 300 : { this->is_multi_value_arg_ = true; }
2608 :
2609 : // Whether this is a call to builtin function.
2610 : virtual bool
2611 1442101 : is_builtin() const
2612 1442101 : { return false; }
2613 :
2614 : // Convert to a Builtin_call_expression, or return NULL.
2615 : inline Builtin_call_expression*
2616 : builtin_call_expression();
2617 :
2618 : inline const Builtin_call_expression*
2619 : builtin_call_expression() const;
2620 :
2621 : // Lower to a Builtin_call_expression if appropriate.
2622 : Expression*
2623 : lower_builtin(Gogo*);
2624 :
2625 : protected:
2626 : int
2627 : do_traverse(Traverse*);
2628 :
2629 : virtual Expression*
2630 : do_lower(Gogo*, Named_object*, Statement_inserter*);
2631 :
2632 : virtual Expression*
2633 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
2634 :
2635 : bool
2636 : do_discarding_value();
2637 :
2638 : virtual Type*
2639 : do_type();
2640 :
2641 : virtual bool
2642 : do_is_constant() const;
2643 :
2644 : bool
2645 : do_is_untyped(Type**) const;
2646 :
2647 : bool
2648 : do_numeric_constant_value(Numeric_constant*);
2649 :
2650 : virtual void
2651 : do_determine_type(Gogo*, const Type_context*);
2652 :
2653 : virtual void
2654 : do_check_types(Gogo*);
2655 :
2656 : Expression*
2657 : do_copy();
2658 :
2659 : bool
2660 : do_must_eval_in_order() const;
2661 :
2662 : virtual Bexpression*
2663 : do_get_backend(Translate_context*);
2664 :
2665 : int
2666 : do_inlining_cost() const;
2667 :
2668 : void
2669 : do_export(Export_function_body*) const;
2670 :
2671 : virtual bool
2672 : do_is_recover_call() const;
2673 :
2674 : virtual void
2675 : do_set_recover_arg(Expression*);
2676 :
2677 : // Let a builtin expression change the argument list.
2678 : void
2679 857 : set_args(Expression_list* args)
2680 857 : { this->args_ = args; }
2681 :
2682 : // Let a builtin expression check whether types have been
2683 : // determined.
2684 : bool
2685 : determining_types();
2686 :
2687 : // Let a builtin expression retrieve the expected type.
2688 : Type*
2689 1466811 : type()
2690 1466811 : { return this->type_; }
2691 :
2692 : // Let a builtin expression set the expected type.
2693 : void
2694 1529 : set_type(Type* type)
2695 1529 : { this->type_ = type; }
2696 :
2697 : // Let a builtin expression simply f(g()) where g returns multiple
2698 : // results.
2699 : void
2700 : simplify_multiple_results(Gogo*);
2701 :
2702 : void
2703 : export_arguments(Export_function_body*) const;
2704 :
2705 : void
2706 : do_dump_expression(Ast_dump_context*) const;
2707 :
2708 : void
2709 : do_add_conversions();
2710 :
2711 : private:
2712 : bool
2713 : rewrite_varargs();
2714 :
2715 : bool
2716 : check_argument_type(int, const Type*, const Type*, Location);
2717 :
2718 : Expression*
2719 : intrinsify(Gogo*, Statement_inserter*);
2720 :
2721 : Expression*
2722 : interface_method_function(Interface_field_reference_expression*,
2723 : Expression**, Location);
2724 :
2725 : Bexpression*
2726 : set_results(Translate_context*);
2727 :
2728 : // The function to call.
2729 : Expression* fn_;
2730 : // The arguments to pass. This may be NULL if there are no
2731 : // arguments.
2732 : Expression_list* args_;
2733 : // The type of the expression, to avoid recomputing it.
2734 : Type* type_;
2735 : // If not NULL, this is a lowered version of this Call_expression.
2736 : Expression* lowered_;
2737 : // The backend expression for the call, used for a call which returns a tuple.
2738 : Bexpression* call_;
2739 : // A temporary variable to store this call if the function returns a tuple.
2740 : Temporary_statement* call_temp_;
2741 : // If not 0, the number of results expected from this call, when
2742 : // used in a context that expects multiple values.
2743 : size_t expected_result_count_;
2744 : // True if the last argument is a varargs argument (f(a...)).
2745 : bool is_varargs_;
2746 : // True if varargs have already been lowered.
2747 : bool varargs_are_lowered_;
2748 : // True if types have been determined.
2749 : bool types_are_determined_;
2750 : // True if the call is an argument to a defer statement.
2751 : bool is_deferred_;
2752 : // True if the call is an argument to a go statement.
2753 : bool is_concurrent_;
2754 : // True if this is a call to a generated equality function.
2755 : bool is_equal_function_;
2756 : // True if we reported an error about a mismatch between call
2757 : // results and uses. This is to avoid producing multiple errors
2758 : // when there are multiple Call_result_expressions.
2759 : bool issued_error_;
2760 : // True if this call is used as an argument that returns multiple results.
2761 : bool is_multi_value_arg_;
2762 : // True if this expression has already been flattened.
2763 : bool is_flattened_;
2764 : };
2765 :
2766 : // A call expression to a builtin function.
2767 :
2768 : class Builtin_call_expression : public Call_expression
2769 : {
2770 : public:
2771 : Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
2772 : bool is_varargs, Location location);
2773 :
2774 : // The builtin functions.
2775 : enum Builtin_function_code
2776 : {
2777 : BUILTIN_INVALID,
2778 :
2779 : // Predeclared builtin functions.
2780 : BUILTIN_APPEND,
2781 : BUILTIN_CAP,
2782 : BUILTIN_CLOSE,
2783 : BUILTIN_COMPLEX,
2784 : BUILTIN_COPY,
2785 : BUILTIN_DELETE,
2786 : BUILTIN_IMAG,
2787 : BUILTIN_LEN,
2788 : BUILTIN_MAKE,
2789 : BUILTIN_NEW,
2790 : BUILTIN_PANIC,
2791 : BUILTIN_PRINT,
2792 : BUILTIN_PRINTLN,
2793 : BUILTIN_REAL,
2794 : BUILTIN_RECOVER,
2795 :
2796 : // Builtin functions from the unsafe package.
2797 : BUILTIN_ADD,
2798 : BUILTIN_ALIGNOF,
2799 : BUILTIN_OFFSETOF,
2800 : BUILTIN_SIZEOF,
2801 : BUILTIN_SLICE
2802 : };
2803 :
2804 : Builtin_function_code
2805 243383 : code() const
2806 243383 : { return this->code_; }
2807 :
2808 : // This overrides Call_expression::is_builtin.
2809 : bool
2810 483540 : is_builtin() const
2811 483540 : { return true; }
2812 :
2813 : // Return whether EXPR, of array type, is a constant if passed to
2814 : // len or cap.
2815 : static bool
2816 : array_len_is_constant(Expression* expr);
2817 :
2818 : Expression*
2819 : flatten_append(Gogo*, Named_object*, Statement_inserter*, Expression*,
2820 : Block*);
2821 :
2822 : protected:
2823 : // This overrides Call_expression::do_lower.
2824 : Expression*
2825 : do_lower(Gogo*, Named_object*, Statement_inserter*);
2826 :
2827 : Expression*
2828 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
2829 :
2830 : bool
2831 : do_is_constant() const;
2832 :
2833 : bool
2834 : do_is_untyped(Type**) const;
2835 :
2836 : bool
2837 : do_numeric_constant_value(Numeric_constant*);
2838 :
2839 : bool
2840 : do_discarding_value();
2841 :
2842 : Type*
2843 : do_type();
2844 :
2845 : void
2846 : do_determine_type(Gogo*, const Type_context*);
2847 :
2848 : void
2849 : do_check_types(Gogo*);
2850 :
2851 : Expression*
2852 : do_copy();
2853 :
2854 : Bexpression*
2855 : do_get_backend(Translate_context*);
2856 :
2857 : int
2858 246597 : do_inlining_cost() const
2859 246597 : { return 1; }
2860 :
2861 : void
2862 : do_export(Export_function_body*) const;
2863 :
2864 : virtual bool
2865 : do_is_recover_call() const;
2866 :
2867 : virtual void
2868 : do_set_recover_arg(Expression*);
2869 :
2870 : private:
2871 : Expression*
2872 : one_arg() const;
2873 :
2874 : bool
2875 : check_one_arg();
2876 :
2877 : static Type*
2878 : real_imag_type(Type*);
2879 :
2880 : static Type*
2881 : complex_type(Type*);
2882 :
2883 : Expression*
2884 : lower_make(Gogo*, Statement_inserter*);
2885 :
2886 : bool
2887 : check_int_value(Expression*, bool is_length, bool* small);
2888 :
2889 : // A pointer back to the general IR structure. This avoids a global
2890 : // variable, or passing it around everywhere.
2891 : Gogo* gogo_;
2892 : // The builtin function being called.
2893 : Builtin_function_code code_;
2894 : // Used to stop endless loops when the length of an array uses len
2895 : // or cap of the array itself.
2896 : mutable bool seen_;
2897 : // Whether the argument is set for calls to BUILTIN_RECOVER.
2898 : bool recover_arg_is_set_;
2899 : };
2900 :
2901 : inline Builtin_call_expression*
2902 253891 : Call_expression::builtin_call_expression()
2903 : {
2904 253891 : return (this->is_builtin()
2905 253891 : ? static_cast<Builtin_call_expression*>(this)
2906 : : NULL);
2907 : }
2908 :
2909 : inline const Builtin_call_expression*
2910 60040 : Call_expression::builtin_call_expression() const
2911 : {
2912 60040 : return (this->is_builtin()
2913 60040 : ? static_cast<const Builtin_call_expression*>(this)
2914 : : NULL);
2915 : }
2916 :
2917 : // A single result from a call which returns multiple results.
2918 :
2919 : class Call_result_expression : public Expression
2920 : {
2921 : public:
2922 168017 : Call_result_expression(Call_expression* call, unsigned int index)
2923 168017 : : Expression(EXPRESSION_CALL_RESULT, call->location()),
2924 168017 : call_(call), index_(index)
2925 : { }
2926 :
2927 : Expression*
2928 172188 : call() const
2929 172188 : { return this->call_; }
2930 :
2931 : unsigned int
2932 159073 : index() const
2933 159073 : { return this->index_; }
2934 :
2935 : protected:
2936 : int
2937 : do_traverse(Traverse*);
2938 :
2939 : Type*
2940 : do_type();
2941 :
2942 : void
2943 : do_determine_type(Gogo*, const Type_context*);
2944 :
2945 : void
2946 : do_check_types(Gogo*);
2947 :
2948 : Expression*
2949 0 : do_copy()
2950 : {
2951 0 : return new Call_result_expression(this->call_->call_expression(),
2952 0 : this->index_);
2953 : }
2954 :
2955 : bool
2956 167706 : do_must_eval_in_order() const
2957 167706 : { return true; }
2958 :
2959 : Bexpression*
2960 : do_get_backend(Translate_context*);
2961 :
2962 : void
2963 : do_dump_expression(Ast_dump_context*) const;
2964 :
2965 : private:
2966 : // The underlying call expression.
2967 : Expression* call_;
2968 : // Which result we want.
2969 : unsigned int index_;
2970 : };
2971 :
2972 : // An expression which represents a pointer to a function.
2973 :
2974 : class Func_expression : public Expression
2975 : {
2976 : public:
2977 2156076 : Func_expression(Named_object* function, Expression* closure,
2978 : Location location)
2979 2156076 : : Expression(EXPRESSION_FUNC_REFERENCE, location),
2980 2156076 : function_(function), closure_(closure),
2981 2156076 : runtime_code_(Runtime::NUMBER_OF_FUNCTIONS)
2982 : { }
2983 :
2984 : // Return the object associated with the function.
2985 : Named_object*
2986 10507019 : named_object() const
2987 10507019 : { return this->function_; }
2988 :
2989 : // Return the closure for this function. This will return NULL if
2990 : // the function has no closure, which is the normal case.
2991 : Expression*
2992 3556905 : closure()
2993 3556905 : { return this->closure_; }
2994 :
2995 : // Return whether this is a reference to a runtime function.
2996 : bool
2997 1707421 : is_runtime_function() const
2998 1707421 : { return this->runtime_code_ != Runtime::NUMBER_OF_FUNCTIONS; }
2999 :
3000 : // Return the runtime code for this function expression.
3001 : // Returns Runtime::NUMBER_OF_FUNCTIONS if this is not a reference to a
3002 : // runtime function.
3003 : Runtime::Function
3004 8576 : runtime_code() const
3005 8576 : { return this->runtime_code_; }
3006 :
3007 : // Set the runtime code for this function expression.
3008 : void
3009 1798779 : set_runtime_code(Runtime::Function code)
3010 1798779 : { this->runtime_code_ = code; }
3011 :
3012 : // Return a backend expression for the code of a function.
3013 : static Bexpression*
3014 : get_code_pointer(Gogo*, Named_object* function, Location loc);
3015 :
3016 : protected:
3017 : int
3018 : do_traverse(Traverse*);
3019 :
3020 : Type*
3021 : do_type();
3022 :
3023 : void
3024 1676927 : do_determine_type(Gogo* gogo, const Type_context*)
3025 : {
3026 1676927 : if (this->closure_ != NULL)
3027 15891 : this->closure_->determine_type_no_context(gogo);
3028 1676927 : }
3029 :
3030 : Expression*
3031 4 : do_copy()
3032 : {
3033 4 : return Expression::make_func_reference(this->function_,
3034 4 : (this->closure_ == NULL
3035 : ? NULL
3036 0 : : this->closure_->copy()),
3037 4 : this->location());
3038 : }
3039 :
3040 : Bexpression*
3041 : do_get_backend(Translate_context*);
3042 :
3043 : int
3044 : do_inlining_cost() const;
3045 :
3046 : void
3047 : do_export(Export_function_body*) const;
3048 :
3049 : void
3050 : do_dump_expression(Ast_dump_context*) const;
3051 :
3052 : private:
3053 : // The function itself.
3054 : Named_object* function_;
3055 : // A closure. This is normally NULL. For a nested function, it may
3056 : // be a struct holding pointers to all the variables referenced by
3057 : // this function and defined in enclosing functions.
3058 : Expression* closure_;
3059 : // The runtime code for the referenced function.
3060 : Runtime::Function runtime_code_;
3061 : };
3062 :
3063 : // A function descriptor. A function descriptor is a struct with a
3064 : // single field pointing to the function code. This is used for
3065 : // functions without closures.
3066 :
3067 : class Func_descriptor_expression : public Expression
3068 : {
3069 : public:
3070 : Func_descriptor_expression(Named_object* fn);
3071 :
3072 : // Make the function descriptor type, so that it can be converted.
3073 : static void
3074 : make_func_descriptor_type();
3075 :
3076 : protected:
3077 : int
3078 : do_traverse(Traverse*);
3079 :
3080 : Type*
3081 : do_type();
3082 :
3083 : void
3084 0 : do_determine_type(Gogo*, const Type_context*)
3085 0 : { }
3086 :
3087 : Expression*
3088 0 : do_copy()
3089 0 : { return Expression::make_func_descriptor(this->fn_); }
3090 :
3091 : bool
3092 0 : do_is_addressable() const
3093 0 : { return true; }
3094 :
3095 : Bexpression*
3096 : do_get_backend(Translate_context*);
3097 :
3098 : void
3099 : do_dump_expression(Ast_dump_context* context) const;
3100 :
3101 : private:
3102 : // The type of all function descriptors.
3103 : static Type* descriptor_type;
3104 :
3105 : // The function for which this is the descriptor.
3106 : Named_object* fn_;
3107 : // The descriptor variable.
3108 : Bvariable* dvar_;
3109 : };
3110 :
3111 : // A reference to an unknown name.
3112 :
3113 : class Unknown_expression : public Parser_expression
3114 : {
3115 : public:
3116 601465 : Unknown_expression(Named_object* named_object, Location location)
3117 601465 : : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
3118 601465 : named_object_(named_object), lowered_(NULL), iota_value_(0),
3119 601465 : no_error_message_(false), is_iota_(false)
3120 601465 : { }
3121 :
3122 : // The associated named object.
3123 : Named_object*
3124 1306547 : named_object() const
3125 1306547 : { return this->named_object_; }
3126 :
3127 : // The name of the identifier which was unknown.
3128 : const std::string&
3129 : name() const;
3130 :
3131 : // Call this to indicate that we should not give an error if this
3132 : // name is never defined. This is used to avoid knock-on errors
3133 : // during an erroneous parse.
3134 : void
3135 2 : set_no_error_message()
3136 2 : { this->no_error_message_ = true; }
3137 :
3138 : // Set the iota value if this is a reference to iota.
3139 : void
3140 : set_iota_value(int);
3141 :
3142 : protected:
3143 : int
3144 : do_traverse(Traverse*);
3145 :
3146 : Type*
3147 : do_type();
3148 :
3149 : void
3150 : do_determine_type(Gogo*, const Type_context*);
3151 :
3152 : bool
3153 : do_is_constant() const;
3154 :
3155 : bool
3156 : do_is_untyped(Type**) const;
3157 :
3158 : virtual bool
3159 : do_numeric_constant_value(Numeric_constant*);
3160 :
3161 : virtual bool
3162 : do_string_constant_value(std::string*);
3163 :
3164 : virtual bool
3165 : do_boolean_constant_value(bool*);
3166 :
3167 : bool
3168 : do_is_addressable() const;
3169 :
3170 : Expression*
3171 : do_lower(Gogo*, Named_object*, Statement_inserter*);
3172 :
3173 : Expression*
3174 9207 : do_copy()
3175 9207 : { return new Unknown_expression(this->named_object_, this->location()); }
3176 :
3177 : void
3178 : do_dump_expression(Ast_dump_context*) const;
3179 :
3180 : private:
3181 : // The unknown name.
3182 : Named_object* named_object_;
3183 : // The fully resolved expression.
3184 : Expression* lowered_;
3185 : // The iota value if this turns out to be a reference to iota.
3186 : int iota_value_;
3187 : // True if we should not give errors if this is undefined. This is
3188 : // used if there was a parse failure.
3189 : bool no_error_message_;
3190 : // True if this could be a reference to iota.
3191 : bool is_iota_;
3192 : };
3193 :
3194 : // An index expression. This is lowered to an array index, a string
3195 : // index, or a map index.
3196 :
3197 : class Index_expression : public Parser_expression
3198 : {
3199 : public:
3200 181155 : Index_expression(Expression* left, Expression* start, Expression* end,
3201 : Expression* cap, Location location)
3202 181155 : : Parser_expression(EXPRESSION_INDEX, location),
3203 181155 : left_(left), start_(start), end_(end), cap_(cap),
3204 181155 : needs_nil_check_(false)
3205 : { }
3206 :
3207 : // Return the expression being indexed.
3208 : Expression*
3209 3918 : left() const
3210 3918 : { return this->left_; }
3211 :
3212 : // Dump an index expression, i.e. an expression of the form
3213 : // expr[expr], expr[expr:expr], or expr[expr:expr:expr] to a dump context.
3214 : static void
3215 : dump_index_expression(Ast_dump_context*, const Expression* expr,
3216 : const Expression* start, const Expression* end,
3217 : const Expression* cap);
3218 :
3219 : // Report whether EXPR is a map index expression.
3220 : static bool
3221 : is_map_index(Expression* expr);
3222 :
3223 : protected:
3224 : int
3225 : do_traverse(Traverse*);
3226 :
3227 : Type*
3228 : do_type();
3229 :
3230 : void
3231 : do_determine_type(Gogo*, const Type_context*);
3232 :
3233 : void
3234 : do_check_types(Gogo*);
3235 :
3236 : bool
3237 : do_is_addressable() const;
3238 :
3239 : Expression*
3240 : do_lower(Gogo*, Named_object*, Statement_inserter*s);
3241 :
3242 : Expression*
3243 0 : do_copy()
3244 : {
3245 0 : Index_expression* ret =
3246 0 : new Index_expression(this->left_->copy(), this->start_->copy(),
3247 0 : (this->end_ == NULL
3248 : ? NULL
3249 0 : : this->end_->copy()),
3250 0 : (this->cap_ == NULL
3251 : ? NULL
3252 0 : : this->cap_->copy()),
3253 0 : this->location());
3254 0 : if (this->needs_nil_check_)
3255 0 : ret->needs_nil_check_ = true;
3256 0 : return ret;
3257 : }
3258 :
3259 : // This shouldn't be called--we don't know yet.
3260 : bool
3261 0 : do_must_eval_subexpressions_in_order(int*) const
3262 0 : { go_unreachable(); }
3263 :
3264 : void
3265 : do_dump_expression(Ast_dump_context*) const;
3266 :
3267 : void
3268 : do_issue_nil_check();
3269 :
3270 : private:
3271 : // The expression being indexed.
3272 : Expression* left_;
3273 : // The first index.
3274 : Expression* start_;
3275 : // The second index. This is NULL for an index, non-NULL for a
3276 : // slice.
3277 : Expression* end_;
3278 : // The capacity argument. This is NULL for indices and slices that use the
3279 : // default capacity, non-NULL for indices and slices that specify the
3280 : // capacity.
3281 : Expression* cap_;
3282 : // True if this needs a nil check. This changes how we handle
3283 : // dereferencing a pointer to an array.
3284 : bool needs_nil_check_;
3285 : };
3286 :
3287 : // An array index. This is used for both indexing and slicing.
3288 :
3289 : class Array_index_expression : public Expression
3290 : {
3291 : public:
3292 218694 : Array_index_expression(Expression* array, Expression* start,
3293 : Expression* end, Expression* cap, Location location)
3294 218694 : : Expression(EXPRESSION_ARRAY_INDEX, location),
3295 218694 : array_(array), start_(start), end_(end), cap_(cap), type_(NULL),
3296 218694 : needs_bounds_check_(true), is_flattened_(false)
3297 : { }
3298 :
3299 : // Return the array.
3300 : Expression*
3301 4347965 : array()
3302 4347965 : { return this->array_; }
3303 :
3304 : const Expression*
3305 : array() const
3306 : { return this->array_; }
3307 :
3308 : // Return the index of a simple index expression, or the start index
3309 : // of a slice expression.
3310 : Expression*
3311 8581 : start()
3312 8581 : { return this->start_; }
3313 :
3314 : const Expression*
3315 : start() const
3316 : { return this->start_; }
3317 :
3318 : // Return the end index of a slice expression. This is NULL for a
3319 : // simple index expression.
3320 : Expression*
3321 2477589 : end()
3322 2477503 : { return this->end_; }
3323 :
3324 : const Expression*
3325 : end() const
3326 : { return this->end_; }
3327 :
3328 : void
3329 41807 : set_needs_bounds_check(bool b)
3330 33694 : { this->needs_bounds_check_ = b; }
3331 :
3332 : // Check indexes.
3333 : static bool
3334 : check_indexes(Expression* array, Expression* start, Expression* len,
3335 : Expression* cap, Location);
3336 :
3337 : protected:
3338 : int
3339 : do_traverse(Traverse*);
3340 :
3341 : Expression*
3342 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
3343 :
3344 : Type*
3345 : do_type();
3346 :
3347 : void
3348 : do_determine_type(Gogo*, const Type_context*);
3349 :
3350 : void
3351 : do_check_types(Gogo*);
3352 :
3353 : Expression*
3354 8113 : do_copy()
3355 : {
3356 8113 : Expression* ret = Expression::make_array_index(this->array_->copy(),
3357 8113 : this->start_->copy(),
3358 8113 : (this->end_ == NULL
3359 : ? NULL
3360 0 : : this->end_->copy()),
3361 8113 : (this->cap_ == NULL
3362 : ? NULL
3363 0 : : this->cap_->copy()),
3364 : this->location());
3365 8113 : ret->array_index_expression()->set_needs_bounds_check(this->needs_bounds_check_);
3366 8113 : return ret;
3367 : }
3368 :
3369 : bool
3370 : do_must_eval_subexpressions_in_order(int* skip) const;
3371 :
3372 : bool
3373 : do_is_addressable() const;
3374 :
3375 : void
3376 : do_address_taken(bool escapes);
3377 :
3378 : void
3379 1068 : do_issue_nil_check()
3380 1068 : { this->array_->issue_nil_check(); }
3381 :
3382 : Bexpression*
3383 : do_get_backend(Translate_context*);
3384 :
3385 : int
3386 182751 : do_inlining_cost() const
3387 182751 : { return this->end_ != NULL ? 2 : 1; }
3388 :
3389 : void
3390 : do_export(Export_function_body*) const;
3391 :
3392 : void
3393 : do_dump_expression(Ast_dump_context*) const;
3394 :
3395 : private:
3396 : // The array we are getting a value from.
3397 : Expression* array_;
3398 : // The start or only index.
3399 : Expression* start_;
3400 : // The end index of a slice. This may be NULL for a simple array
3401 : // index, or it may be a nil expression for the length of the array.
3402 : Expression* end_;
3403 : // The capacity argument of a slice. This may be NULL for an array index or
3404 : // slice.
3405 : Expression* cap_;
3406 : // The type of the expression.
3407 : Type* type_;
3408 : // Whether bounds check is needed.
3409 : bool needs_bounds_check_;
3410 : // Whether this has already been flattened.
3411 : bool is_flattened_;
3412 : };
3413 :
3414 : // A string index. This is used for both indexing and slicing.
3415 :
3416 : class String_index_expression : public Expression
3417 : {
3418 : public:
3419 20293 : String_index_expression(Expression* string, Expression* start,
3420 : Expression* end, Location location)
3421 20293 : : Expression(EXPRESSION_STRING_INDEX, location),
3422 20293 : string_(string), start_(start), end_(end), is_flattened_(false)
3423 : { }
3424 :
3425 : // Return the string being indexed.
3426 : Expression*
3427 16854 : string() const
3428 16854 : { return this->string_; }
3429 :
3430 : // Return the index of a simple index expression, or the start index
3431 : // of a slice expression.
3432 : Expression*
3433 3978 : start() const
3434 3978 : { return this->start_; }
3435 :
3436 : // Return the end index of a slice expression. This is NULL for a
3437 : // simple index expression.
3438 : Expression*
3439 6799 : end() const
3440 6799 : { return this->end_; }
3441 :
3442 : // Check indexes.
3443 : static bool
3444 : check_indexes(Expression* string, Expression* start, Expression* len,
3445 : Location);
3446 :
3447 : protected:
3448 : int
3449 : do_traverse(Traverse*);
3450 :
3451 : Expression*
3452 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
3453 :
3454 : Type*
3455 : do_type();
3456 :
3457 : void
3458 : do_determine_type(Gogo*, const Type_context*);
3459 :
3460 : void
3461 : do_check_types(Gogo*);
3462 :
3463 : Expression*
3464 0 : do_copy()
3465 : {
3466 0 : return Expression::make_string_index(this->string_->copy(),
3467 0 : this->start_->copy(),
3468 0 : (this->end_ == NULL
3469 : ? NULL
3470 0 : : this->end_->copy()),
3471 0 : this->location());
3472 : }
3473 :
3474 : bool
3475 27 : do_must_eval_subexpressions_in_order(int*) const
3476 27 : { return true; }
3477 :
3478 : Bexpression*
3479 : do_get_backend(Translate_context*);
3480 :
3481 : int
3482 14207 : do_inlining_cost() const
3483 14207 : { return this->end_ != NULL ? 2 : 1; }
3484 :
3485 : void
3486 : do_export(Export_function_body*) const;
3487 :
3488 : void
3489 : do_dump_expression(Ast_dump_context*) const;
3490 :
3491 : private:
3492 : // The string we are getting a value from.
3493 : Expression* string_;
3494 : // The start or only index.
3495 : Expression* start_;
3496 : // The end index of a slice. This may be NULL for a single index,
3497 : // or it may be a nil expression for the length of the string.
3498 : Expression* end_;
3499 : // Whether this has already been flattened.
3500 : bool is_flattened_;
3501 : };
3502 :
3503 : // An index into a map.
3504 :
3505 : class Map_index_expression : public Expression
3506 : {
3507 : public:
3508 14200 : Map_index_expression(Expression* map, Expression* index,
3509 : Location location)
3510 14200 : : Expression(EXPRESSION_MAP_INDEX, location),
3511 14200 : map_(map), index_(index), value_pointer_(NULL)
3512 : { }
3513 :
3514 : // Return the map.
3515 : Expression*
3516 14203 : map()
3517 14203 : { return this->map_; }
3518 :
3519 : const Expression*
3520 : map() const
3521 : { return this->map_; }
3522 :
3523 : // Return the index.
3524 : Expression*
3525 16646 : index()
3526 16646 : { return this->index_; }
3527 :
3528 : const Expression*
3529 : index() const
3530 : { return this->index_; }
3531 :
3532 : // Get the type of the map being indexed.
3533 : Map_type*
3534 : get_map_type() const;
3535 :
3536 : // Return an expression for the map index. This returns an
3537 : // expression that evaluates to a pointer to a value in the map. If
3538 : // the key is not present in the map, this will return a pointer to
3539 : // the zero value.
3540 : Expression*
3541 : get_value_pointer(Gogo*);
3542 :
3543 : protected:
3544 : int
3545 : do_traverse(Traverse*);
3546 :
3547 : Expression*
3548 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
3549 :
3550 : Type*
3551 : do_type();
3552 :
3553 : void
3554 : do_determine_type(Gogo*, const Type_context*);
3555 :
3556 : void
3557 : do_check_types(Gogo*);
3558 :
3559 : Expression*
3560 141 : do_copy()
3561 : {
3562 141 : return Expression::make_map_index(this->map_->copy(),
3563 141 : this->index_->copy(),
3564 141 : this->location());
3565 : }
3566 :
3567 : bool
3568 169 : do_must_eval_subexpressions_in_order(int*) const
3569 169 : { return true; }
3570 :
3571 : // A map index expression is an lvalue but it is not addressable.
3572 :
3573 : Bexpression*
3574 : do_get_backend(Translate_context*);
3575 :
3576 : int
3577 14712 : do_inlining_cost() const
3578 14712 : { return 5; }
3579 :
3580 : void
3581 : do_export(Export_function_body*) const;
3582 :
3583 : void
3584 : do_dump_expression(Ast_dump_context*) const;
3585 :
3586 : void
3587 : do_add_conversions();
3588 :
3589 : private:
3590 : // The map we are looking into.
3591 : Expression* map_;
3592 : // The index.
3593 : Expression* index_;
3594 : // A pointer to the value at this index.
3595 : Expression* value_pointer_;
3596 : };
3597 :
3598 : // An expression which represents a method bound to its first
3599 : // argument.
3600 :
3601 : class Bound_method_expression : public Expression
3602 : {
3603 : public:
3604 264730 : Bound_method_expression(Expression* expr, const Method *method,
3605 : Named_object* function, Location location)
3606 264730 : : Expression(EXPRESSION_BOUND_METHOD, location),
3607 264730 : expr_(expr), expr_type_(NULL), method_(method), function_(function)
3608 : { }
3609 :
3610 : // Return the object which is the first argument.
3611 : Expression*
3612 275722 : first_argument()
3613 275722 : { return this->expr_; }
3614 :
3615 : // Return the implicit type of the first argument. This will be
3616 : // non-NULL when using a method from an anonymous field without
3617 : // using an explicit stub.
3618 : Type*
3619 258280 : first_argument_type() const
3620 258280 : { return this->expr_type_; }
3621 :
3622 : // Return the method.
3623 : const Method*
3624 6447 : method() const
3625 6447 : { return this->method_; }
3626 :
3627 : // Return the function to call.
3628 : Named_object*
3629 288920 : function() const
3630 288920 : { return this->function_; }
3631 :
3632 : // Set the implicit type of the expression.
3633 : void
3634 0 : set_first_argument_type(Type* type)
3635 0 : { this->expr_type_ = type; }
3636 :
3637 : // Create a thunk to call FUNCTION, for METHOD, when it is used as
3638 : // part of a method value.
3639 : static Named_object*
3640 : create_thunk(Gogo*, const Method* method, Named_object* function);
3641 :
3642 : // Look up a thunk.
3643 : static Named_object*
3644 : lookup_thunk(Named_object* function);
3645 :
3646 : protected:
3647 : int
3648 : do_traverse(Traverse*);
3649 :
3650 : Expression*
3651 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
3652 :
3653 : Type*
3654 : do_type();
3655 :
3656 : void
3657 : do_determine_type(Gogo*, const Type_context*);
3658 :
3659 : void
3660 : do_check_types(Gogo*);
3661 :
3662 : Expression*
3663 0 : do_copy()
3664 : {
3665 0 : return new Bound_method_expression(this->expr_->copy(), this->method_,
3666 0 : this->function_, this->location());
3667 : }
3668 :
3669 : Bexpression*
3670 0 : do_get_backend(Translate_context*)
3671 0 : { go_unreachable(); }
3672 :
3673 : void
3674 : do_dump_expression(Ast_dump_context*) const;
3675 :
3676 : private:
3677 : // A mapping from method functions to the thunks we have created for
3678 : // them.
3679 : typedef Unordered_map(Named_object*, Named_object*) Method_value_thunks;
3680 : static Method_value_thunks method_value_thunks;
3681 :
3682 : // The object used to find the method. This is passed to the method
3683 : // as the first argument.
3684 : Expression* expr_;
3685 : // The implicit type of the object to pass to the method. This is
3686 : // NULL in the normal case, non-NULL when using a method from an
3687 : // anonymous field which does not require a stub.
3688 : Type* expr_type_;
3689 : // The method.
3690 : const Method* method_;
3691 : // The function to call. This is not the same as
3692 : // method_->named_object() when the method has a stub. This will be
3693 : // the real function rather than the stub.
3694 : Named_object* function_;
3695 : };
3696 :
3697 : // A reference to a field in a struct.
3698 :
3699 : class Field_reference_expression : public Expression
3700 : {
3701 : public:
3702 987040 : Field_reference_expression(Expression* expr, unsigned int field_index,
3703 : Location location)
3704 987040 : : Expression(EXPRESSION_FIELD_REFERENCE, location),
3705 987040 : expr_(expr), field_index_(field_index), implicit_(false), called_fieldtrack_(false)
3706 : { }
3707 :
3708 : // Return the struct expression.
3709 : Expression*
3710 19627961 : expr() const
3711 19626120 : { return this->expr_; }
3712 :
3713 : // Return the field index.
3714 : unsigned int
3715 7052 : field_index() const
3716 7052 : { return this->field_index_; }
3717 :
3718 : // Return whether this node was implied by an anonymous field.
3719 : bool
3720 2708 : implicit() const
3721 2708 : { return this->implicit_; }
3722 :
3723 : void
3724 24696 : set_implicit(bool implicit)
3725 24696 : { this->implicit_ = implicit; }
3726 :
3727 : // Set the struct expression. This is used when parsing.
3728 : void
3729 24696 : set_struct_expression(Expression* expr)
3730 : {
3731 24696 : go_assert(this->expr_ == NULL);
3732 24696 : this->expr_ = expr;
3733 24696 : }
3734 :
3735 : protected:
3736 : int
3737 9128572 : do_traverse(Traverse* traverse)
3738 9128572 : { return Expression::traverse(&this->expr_, traverse); }
3739 :
3740 : Expression*
3741 : do_lower(Gogo*, Named_object*, Statement_inserter*);
3742 :
3743 : Type*
3744 : do_type();
3745 :
3746 : void
3747 1351470 : do_determine_type(Gogo* gogo, const Type_context*)
3748 1351470 : { this->expr_->determine_type_no_context(gogo); }
3749 :
3750 : void
3751 : do_check_types(Gogo*);
3752 :
3753 : Expression*
3754 15505 : do_copy()
3755 : {
3756 15505 : return Expression::make_field_reference(this->expr_->copy(),
3757 : this->field_index_,
3758 15505 : this->location());
3759 : }
3760 :
3761 : bool
3762 210148 : do_is_addressable() const
3763 210148 : { return this->expr_->is_addressable(); }
3764 :
3765 : void
3766 154833 : do_address_taken(bool escapes)
3767 154833 : { this->expr_->address_taken(escapes); }
3768 :
3769 : void
3770 77074 : do_issue_nil_check()
3771 77074 : { this->expr_->issue_nil_check(); }
3772 :
3773 : Bexpression*
3774 : do_get_backend(Translate_context*);
3775 :
3776 : void
3777 : do_dump_expression(Ast_dump_context*) const;
3778 :
3779 : private:
3780 : // The expression we are looking into. This should have a type of
3781 : // struct.
3782 : Expression* expr_;
3783 : // The zero-based index of the field we are retrieving.
3784 : unsigned int field_index_;
3785 : // Whether this node was emitted implicitly for an embedded field,
3786 : // that is, expr_ is not the expr_ of the original user node.
3787 : bool implicit_;
3788 : // Whether we have already emitted a fieldtrack call.
3789 : bool called_fieldtrack_;
3790 : };
3791 :
3792 : // A reference to a field of an interface.
3793 :
3794 : class Interface_field_reference_expression : public Expression
3795 : {
3796 : public:
3797 32971 : Interface_field_reference_expression(Expression* expr,
3798 : const std::string& name,
3799 : Location location)
3800 32971 : : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
3801 32971 : expr_(expr), name_(name)
3802 32971 : { }
3803 :
3804 : // Return the expression for the interface object.
3805 : Expression*
3806 33382 : expr()
3807 33382 : { return this->expr_; }
3808 :
3809 : // Return the name of the method to call.
3810 : const std::string&
3811 1019 : name() const
3812 1019 : { return this->name_; }
3813 :
3814 : // Create a thunk to call the method NAME in TYPE when it is used as
3815 : // part of a method value.
3816 : static Named_object*
3817 : create_thunk(Gogo*, Interface_type* type, const std::string& name);
3818 :
3819 : // Look up a thunk.
3820 : static Named_object*
3821 : lookup_thunk(Interface_type* type, const std::string& name);
3822 :
3823 : // Return an expression for the pointer to the function to call.
3824 : Expression*
3825 : get_function();
3826 :
3827 : // Return an expression for the first argument to pass to the interface
3828 : // function. This is the real object associated with the interface object.
3829 : Expression*
3830 : get_underlying_object();
3831 :
3832 : protected:
3833 : int
3834 : do_traverse(Traverse* traverse);
3835 :
3836 : Expression*
3837 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
3838 :
3839 : Type*
3840 : do_type();
3841 :
3842 : void
3843 : do_determine_type(Gogo*, const Type_context*);
3844 :
3845 : void
3846 : do_check_types(Gogo*);
3847 :
3848 : Expression*
3849 0 : do_copy()
3850 : {
3851 0 : return Expression::make_interface_field_reference(this->expr_->copy(),
3852 0 : this->name_,
3853 0 : this->location());
3854 : }
3855 :
3856 : Bexpression*
3857 : do_get_backend(Translate_context*);
3858 :
3859 : void
3860 : do_dump_expression(Ast_dump_context*) const;
3861 :
3862 : private:
3863 : // A mapping from interface types to a list of thunks we have
3864 : // created for methods.
3865 : typedef std::vector<std::pair<std::string, Named_object*> > Method_thunks;
3866 : typedef Unordered_map(Interface_type*, Method_thunks*)
3867 : Interface_method_thunks;
3868 : static Interface_method_thunks interface_method_thunks;
3869 :
3870 : // The expression for the interface object. This should have a type
3871 : // of interface or pointer to interface.
3872 : Expression* expr_;
3873 : // The field we are retrieving--the name of the method.
3874 : std::string name_;
3875 : };
3876 :
3877 : // Implement the builtin function new.
3878 :
3879 : class Allocation_expression : public Expression
3880 : {
3881 : public:
3882 241329 : Allocation_expression(Type* type, Location location)
3883 241329 : : Expression(EXPRESSION_ALLOCATION, location),
3884 241329 : type_(type), allocate_on_stack_(false),
3885 241329 : no_zero_(false)
3886 : { }
3887 :
3888 : void
3889 35152 : set_allocate_on_stack()
3890 35152 : { this->allocate_on_stack_ = true; }
3891 :
3892 : // Mark that the allocated memory doesn't need zeroing.
3893 : void
3894 13218 : set_no_zero()
3895 13218 : { this->no_zero_ = true; }
3896 :
3897 : protected:
3898 : int
3899 : do_traverse(Traverse*);
3900 :
3901 : Type*
3902 : do_type();
3903 :
3904 : void
3905 31145 : do_determine_type(Gogo*, const Type_context*)
3906 31145 : { }
3907 :
3908 : void
3909 : do_check_types(Gogo*);
3910 :
3911 : Expression*
3912 : do_copy();
3913 :
3914 : Bexpression*
3915 : do_get_backend(Translate_context*);
3916 :
3917 : void
3918 : do_dump_expression(Ast_dump_context*) const;
3919 :
3920 : private:
3921 : // The type we are allocating.
3922 : Type* type_;
3923 : // Whether or not this is a stack allocation.
3924 : bool allocate_on_stack_;
3925 : // Whether we don't need to zero the allocated memory.
3926 : bool no_zero_;
3927 : };
3928 :
3929 : // A general composite literal. This is lowered to a type specific
3930 : // version.
3931 :
3932 : class Composite_literal_expression : public Parser_expression
3933 : {
3934 : public:
3935 226857 : Composite_literal_expression(Type* type, int depth, bool has_keys,
3936 : Expression_list* vals, bool all_are_names,
3937 : Location location)
3938 226857 : : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
3939 226857 : type_(type), depth_(depth), vals_(vals), has_keys_(has_keys),
3940 453714 : all_are_names_(all_are_names), key_path_(std::vector<bool>(depth)),
3941 226857 : traverse_order_(NULL)
3942 226857 : {}
3943 :
3944 :
3945 : // Mark the DEPTH entry of KEY_PATH as containing a key.
3946 : void
3947 35 : update_key_path(size_t depth)
3948 : {
3949 35 : go_assert(depth < this->key_path_.size());
3950 35 : this->key_path_[depth] = true;
3951 35 : }
3952 :
3953 : protected:
3954 : int
3955 : do_traverse(Traverse* traverse);
3956 :
3957 : Type*
3958 : do_type();
3959 :
3960 : void
3961 : do_determine_type(Gogo*, const Type_context*);
3962 :
3963 : void
3964 : do_check_types(Gogo*);
3965 :
3966 : Expression*
3967 : do_lower(Gogo*, Named_object*, Statement_inserter*);
3968 :
3969 : Expression*
3970 : do_copy();
3971 :
3972 : void
3973 : do_dump_expression(Ast_dump_context*) const;
3974 :
3975 : private:
3976 : bool
3977 : resolve_struct_keys(Gogo*, Type* type);
3978 :
3979 : void
3980 : resolve_array_length(Type*);
3981 :
3982 : Expression*
3983 : lower_struct(Type*);
3984 :
3985 : Expression*
3986 : lower_array(Type*);
3987 :
3988 : Expression*
3989 : make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
3990 :
3991 : Expression*
3992 : lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
3993 :
3994 : // The type of the composite literal.
3995 : Type* type_;
3996 : // The depth within a list of composite literals within a composite
3997 : // literal, when the type is omitted.
3998 : int depth_;
3999 : // The values to put in the composite literal.
4000 : Expression_list* vals_;
4001 : // If this is true, then VALS_ is a list of pairs: a key and a
4002 : // value. In an array initializer, a missing key will be NULL.
4003 : bool has_keys_;
4004 : // If this is true, then HAS_KEYS_ is true, and every key is a
4005 : // simple identifier.
4006 : bool all_are_names_;
4007 : // A complement to DEPTH that indicates for each level starting from 0 to
4008 : // DEPTH-1 whether or not this composite literal is nested inside of key or
4009 : // a value. This is used to decide which type to use when given a map literal
4010 : // with omitted key types.
4011 : std::vector<bool> key_path_;
4012 : // If not NULL, the order in which to traverse vals_ for a struct
4013 : // composite literal. This is used so that we implement the order
4014 : // of evaluation rules correctly.
4015 : std::vector<unsigned long>* traverse_order_;
4016 : };
4017 :
4018 : // Helper/mixin class for struct and array construction expressions;
4019 : // encapsulates a list of values plus an optional traversal order
4020 : // recording the order in which the values should be visited.
4021 :
4022 : class Ordered_value_list
4023 : {
4024 : public:
4025 2413948 : Ordered_value_list(Expression_list* vals)
4026 2413948 : : vals_(vals), traverse_order_(NULL)
4027 : { }
4028 :
4029 : Expression_list*
4030 42545061 : vals() const
4031 41448536 : { return this->vals_; }
4032 :
4033 : int
4034 : traverse_vals(Traverse* traverse);
4035 :
4036 : // Get the traversal order (may be NULL)
4037 : std::vector<unsigned long>*
4038 0 : traverse_order()
4039 0 : { return traverse_order_; }
4040 :
4041 : // Set the traversal order, used to ensure that we implement the
4042 : // order of evaluation rules. Takes ownership of the argument.
4043 : void
4044 174515 : set_traverse_order(std::vector<unsigned long>* traverse_order)
4045 55 : { this->traverse_order_ = traverse_order; }
4046 :
4047 : private:
4048 : // The list of values, in order of the fields in the struct or in
4049 : // order of indices in an array. A NULL value of vals_ means that
4050 : // all fields/slots should be zero-initialized; a single NULL entry
4051 : // in the list means that the corresponding field or array slot
4052 : // should be zero-initialized.
4053 : Expression_list* vals_;
4054 : // If not NULL, the order in which to traverse vals_. This is used
4055 : // so that we implement the order of evaluation rules correctly.
4056 : std::vector<unsigned long>* traverse_order_;
4057 : };
4058 :
4059 : // Construct a struct.
4060 :
4061 : class Struct_construction_expression : public Expression,
4062 : public Ordered_value_list
4063 : {
4064 : public:
4065 1545377 : Struct_construction_expression(Type* type, Expression_list* vals,
4066 : Location location)
4067 1545377 : : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
4068 : Ordered_value_list(vals),
4069 1545377 : type_(type)
4070 : { }
4071 :
4072 : // Return whether this is a constant initializer.
4073 : bool
4074 : is_constant_struct() const;
4075 :
4076 : // Check types of a struct composite literal.
4077 : static bool
4078 : check_value_types(Gogo*, Type*, Expression_list*, Location);
4079 :
4080 : protected:
4081 : int
4082 : do_traverse(Traverse* traverse);
4083 :
4084 : bool
4085 : do_is_zero_value() const;
4086 :
4087 : bool
4088 : do_is_static_initializer() const;
4089 :
4090 : Type*
4091 2376299 : do_type()
4092 2376299 : { return this->type_; }
4093 :
4094 : void
4095 : do_determine_type(Gogo*, const Type_context*);
4096 :
4097 : void
4098 : do_check_types(Gogo*);
4099 :
4100 : Expression*
4101 : do_copy();
4102 :
4103 : Bexpression*
4104 : do_get_backend(Translate_context*);
4105 :
4106 : void
4107 : do_export(Export_function_body*) const;
4108 :
4109 : void
4110 : do_dump_expression(Ast_dump_context*) const;
4111 :
4112 : void
4113 : do_add_conversions();
4114 :
4115 : private:
4116 : // The type of the struct to construct.
4117 : Type* type_;
4118 : };
4119 :
4120 : // Construct an array. This class is not used directly; instead we
4121 : // use the child classes, Fixed_array_construction_expression and
4122 : // Slice_construction_expression.
4123 :
4124 : class Array_construction_expression : public Expression,
4125 : public Ordered_value_list
4126 : {
4127 : protected:
4128 868571 : Array_construction_expression(Expression_classification classification,
4129 : Type* type,
4130 : const std::vector<unsigned long>* indexes,
4131 : Expression_list* vals, Location location)
4132 868571 : : Expression(classification, location),
4133 : Ordered_value_list(vals),
4134 868571 : type_(type), indexes_(indexes)
4135 869149 : { go_assert(indexes == NULL || indexes->size() == vals->size()); }
4136 :
4137 : public:
4138 : // Return whether this is a constant initializer.
4139 : bool
4140 : is_constant_array() const;
4141 :
4142 : // Return the number of elements.
4143 : size_t
4144 33167 : element_count() const
4145 33167 : { return this->vals() == NULL ? 0 : this->vals()->size(); }
4146 :
4147 : protected:
4148 : virtual int
4149 : do_traverse(Traverse* traverse);
4150 :
4151 : bool
4152 : do_is_zero_value() const;
4153 :
4154 : bool
4155 : do_is_static_initializer() const;
4156 :
4157 : Type*
4158 3994108 : do_type()
4159 3994108 : { return this->type_; }
4160 :
4161 : void
4162 : do_determine_type(Gogo*, const Type_context*);
4163 :
4164 : void
4165 : do_check_types(Gogo*);
4166 :
4167 : void
4168 : do_export(Export_function_body*) const;
4169 :
4170 : // The indexes.
4171 : const std::vector<unsigned long>*
4172 722619 : indexes()
4173 722619 : { return this->indexes_; }
4174 :
4175 : // Get the backend constructor for the array values.
4176 : Bexpression*
4177 : get_constructor(Translate_context* context, Btype* btype);
4178 :
4179 : void
4180 : do_dump_expression(Ast_dump_context*) const;
4181 :
4182 : virtual void
4183 0 : dump_slice_storage_expression(Ast_dump_context*) const { }
4184 :
4185 : void
4186 : do_add_conversions();
4187 :
4188 : private:
4189 : // The type of the array to construct.
4190 : Type* type_;
4191 : // The list of indexes into the array, one for each value. This may
4192 : // be NULL, in which case the indexes start at zero and increment.
4193 : const std::vector<unsigned long>* indexes_;
4194 : };
4195 :
4196 : // Construct a fixed array.
4197 :
4198 : class Fixed_array_construction_expression :
4199 : public Array_construction_expression
4200 : {
4201 : public:
4202 : Fixed_array_construction_expression(Type* type,
4203 : const std::vector<unsigned long>* indexes,
4204 : Expression_list* vals, Location location);
4205 :
4206 : protected:
4207 : Expression*
4208 : do_copy();
4209 :
4210 : Bexpression*
4211 : do_get_backend(Translate_context*);
4212 : };
4213 :
4214 : // Construct a slice.
4215 :
4216 : class Slice_construction_expression : public Array_construction_expression
4217 : {
4218 : public:
4219 : Slice_construction_expression(Type* type,
4220 : const std::vector<unsigned long>* indexes,
4221 : Expression_list* vals, Location location);
4222 :
4223 : Expression*
4224 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
4225 :
4226 : // Record that the storage for this slice (e.g. vals) cannot escape,
4227 : // hence it can be stack-allocated.
4228 : void
4229 33704 : set_storage_does_not_escape()
4230 : {
4231 33704 : this->storage_escapes_ = false;
4232 33704 : }
4233 :
4234 : protected:
4235 : // Note that taking the address of a slice literal is invalid.
4236 :
4237 : int
4238 : do_traverse(Traverse* traverse);
4239 :
4240 : Expression*
4241 : do_copy();
4242 :
4243 : Bexpression*
4244 : do_get_backend(Translate_context*);
4245 :
4246 : void
4247 : dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const;
4248 :
4249 : // Create an array value for the constructed slice. Invoked during
4250 : // flattening if slice storage does not escape, otherwise invoked
4251 : // later on during do_get_backend().
4252 : Expression*
4253 : create_array_val();
4254 :
4255 : private:
4256 : // The type of the values in this slice.
4257 : Type* valtype_;
4258 : // Array value expression, optionally filled in during flattening.
4259 : Expression* array_val_;
4260 : // Slice storage expression, optionally filled in during flattening.
4261 : Expression* slice_storage_;
4262 : // Normally true. Can be set to false if we know that the resulting
4263 : // storage for the slice cannot escape.
4264 : bool storage_escapes_;
4265 : };
4266 :
4267 : // Construct a map.
4268 :
4269 : class Map_construction_expression : public Expression
4270 : {
4271 : public:
4272 3493 : Map_construction_expression(Type* type, Expression_list* vals,
4273 : Location location)
4274 3493 : : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
4275 3493 : type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL)
4276 6143 : { go_assert(vals == NULL || vals->size() % 2 == 0); }
4277 :
4278 : Expression_list*
4279 38457 : vals() const
4280 38457 : { return this->vals_; }
4281 :
4282 : protected:
4283 : int
4284 : do_traverse(Traverse* traverse);
4285 :
4286 : Expression*
4287 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
4288 :
4289 : Type*
4290 27796 : do_type()
4291 27796 : { return this->type_; }
4292 :
4293 : void
4294 : do_determine_type(Gogo*, const Type_context*);
4295 :
4296 : void
4297 : do_check_types(Gogo*);
4298 :
4299 : Expression*
4300 : do_copy();
4301 :
4302 : Bexpression*
4303 : do_get_backend(Translate_context*);
4304 :
4305 : void
4306 : do_export(Export_function_body*) const;
4307 :
4308 : void
4309 : do_dump_expression(Ast_dump_context*) const;
4310 :
4311 : void
4312 : do_add_conversions();
4313 :
4314 : private:
4315 : // The type of the map to construct.
4316 : Type* type_;
4317 : // The list of values.
4318 : Expression_list* vals_;
4319 : // The type of the key-value pair struct for each map element.
4320 : Struct_type* element_type_;
4321 : // A temporary reference to the variable storing the constructor initializer.
4322 : Temporary_statement* constructor_temp_;
4323 : };
4324 :
4325 : // A type guard expression.
4326 :
4327 : class Type_guard_expression : public Expression
4328 : {
4329 : public:
4330 20347 : Type_guard_expression(Expression* expr, Type* type, Location location)
4331 20347 : : Expression(EXPRESSION_TYPE_GUARD, location),
4332 20347 : expr_(expr), type_(type)
4333 : { }
4334 :
4335 : // Return the expression to convert.
4336 : Expression*
4337 21011 : expr()
4338 21011 : { return this->expr_; }
4339 :
4340 : // Return the type to which to convert.
4341 : Type*
4342 13059 : type()
4343 13059 : { return this->type_; }
4344 :
4345 : protected:
4346 : int
4347 : do_traverse(Traverse* traverse);
4348 :
4349 : Expression*
4350 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
4351 :
4352 : Type*
4353 163754 : do_type()
4354 163754 : { return this->type_; }
4355 :
4356 : void
4357 16779 : do_determine_type(Gogo* gogo, const Type_context*)
4358 16779 : { this->expr_->determine_type_no_context(gogo); }
4359 :
4360 : void
4361 : do_check_types(Gogo*);
4362 :
4363 : Expression*
4364 : do_copy();
4365 :
4366 : Bexpression*
4367 : do_get_backend(Translate_context*);
4368 :
4369 : void
4370 : do_dump_expression(Ast_dump_context*) const;
4371 :
4372 : private:
4373 : // The expression to convert.
4374 : Expression* expr_;
4375 : // The type to which to convert.
4376 : Type* type_;
4377 : };
4378 :
4379 : // Class Heap_expression.
4380 :
4381 : // When you take the address of an escaping expression, it is allocated
4382 : // on the heap. This class implements that.
4383 :
4384 : class Heap_expression : public Expression
4385 : {
4386 : public:
4387 204263 : Heap_expression(Expression* expr, Location location)
4388 204263 : : Expression(EXPRESSION_HEAP, location),
4389 204263 : expr_(expr), allocate_on_stack_(false)
4390 : { }
4391 :
4392 : Expression*
4393 387751 : expr() const
4394 387751 : { return this->expr_; }
4395 :
4396 : void
4397 19511 : set_allocate_on_stack()
4398 19511 : { this->allocate_on_stack_ = true; }
4399 :
4400 : protected:
4401 : int
4402 734124 : do_traverse(Traverse* traverse)
4403 734124 : { return Expression::traverse(&this->expr_, traverse); }
4404 :
4405 : Type*
4406 : do_type();
4407 : void
4408 191067 : do_determine_type(Gogo* gogo, const Type_context*)
4409 191067 : { this->expr_->determine_type_no_context(gogo); }
4410 :
4411 : Expression*
4412 0 : do_copy()
4413 : {
4414 0 : return Expression::make_heap_expression(this->expr_->copy(),
4415 0 : this->location());
4416 : }
4417 :
4418 : Bexpression*
4419 : do_get_backend(Translate_context*);
4420 :
4421 : // We only export global objects, and the parser does not generate
4422 : // this in global scope.
4423 : void
4424 0 : do_export(Export_function_body*) const
4425 0 : { go_unreachable(); }
4426 :
4427 : void
4428 : do_dump_expression(Ast_dump_context*) const;
4429 :
4430 : private:
4431 : // The expression which is being put on the heap.
4432 : Expression* expr_;
4433 : // Whether or not this is a stack allocation.
4434 : bool allocate_on_stack_;
4435 : };
4436 :
4437 : // A receive expression.
4438 :
4439 : class Receive_expression : public Expression
4440 : {
4441 : public:
4442 4139 : Receive_expression(Expression* channel, Location location)
4443 4139 : : Expression(EXPRESSION_RECEIVE, location),
4444 4139 : channel_(channel), temp_receiver_(NULL)
4445 : { }
4446 :
4447 : // Return the channel.
4448 : Expression*
4449 2142 : channel()
4450 2142 : { return this->channel_; }
4451 :
4452 : static Expression*
4453 : do_import(Import_expression*, Location);
4454 :
4455 : protected:
4456 : int
4457 35561 : do_traverse(Traverse* traverse)
4458 35561 : { return Expression::traverse(&this->channel_, traverse); }
4459 :
4460 : bool
4461 1298 : do_discarding_value()
4462 1298 : { return true; }
4463 :
4464 : Type*
4465 : do_type();
4466 :
4467 : Expression*
4468 : do_flatten(Gogo*, Named_object*, Statement_inserter*);
4469 :
4470 : void
4471 2243 : do_determine_type(Gogo* gogo, const Type_context*)
4472 2243 : { this->channel_->determine_type_no_context(gogo); }
4473 :
4474 : void
4475 : do_check_types(Gogo*);
4476 :
4477 : Expression*
4478 0 : do_copy()
4479 : {
4480 0 : return Expression::make_receive(this->channel_->copy(), this->location());
4481 : }
4482 :
4483 : int
4484 600 : do_inlining_cost() const
4485 600 : { return 1; }
4486 :
4487 : bool
4488 2035 : do_must_eval_in_order() const
4489 2035 : { return true; }
4490 :
4491 : Bexpression*
4492 : do_get_backend(Translate_context*);
4493 :
4494 : void
4495 : do_export(Export_function_body*) const;
4496 :
4497 : void
4498 : do_dump_expression(Ast_dump_context*) const;
4499 :
4500 : private:
4501 : // The channel from which we are receiving.
4502 : Expression* channel_;
4503 : // A temporary reference to the variable storing the received data.
4504 : Temporary_statement* temp_receiver_;
4505 : };
4506 :
4507 : // An expression that represents a slice value: a struct with value pointer,
4508 : // length, and capacity fields.
4509 :
4510 : class Slice_value_expression : public Expression
4511 : {
4512 : public:
4513 405750 : Slice_value_expression(Type* type, Expression* valmem, Expression* len,
4514 : Expression* cap, Location location)
4515 405750 : : Expression(EXPRESSION_SLICE_VALUE, location),
4516 405750 : type_(type), valmem_(valmem), len_(len), cap_(cap)
4517 : { }
4518 :
4519 : // The memory holding the values in the slice. The type should be a
4520 : // pointer to the element value of the slice.
4521 : Expression*
4522 16899 : valmem() const
4523 16899 : { return this->valmem_; }
4524 :
4525 : protected:
4526 : int
4527 : do_traverse(Traverse*);
4528 :
4529 : Type*
4530 73544 : do_type()
4531 73544 : { return this->type_; }
4532 :
4533 : void
4534 : do_determine_type(Gogo*, const Type_context*);
4535 :
4536 : Expression*
4537 : do_copy();
4538 :
4539 : Bexpression*
4540 : do_get_backend(Translate_context* context);
4541 :
4542 : void
4543 : do_dump_expression(Ast_dump_context*) const;
4544 :
4545 : private:
4546 : // The type of the slice value.
4547 : Type* type_;
4548 : // The memory holding the values in the slice.
4549 : Expression* valmem_;
4550 : // The length of the slice.
4551 : Expression* len_;
4552 : // The capacity of the slice.
4553 : Expression* cap_;
4554 : };
4555 :
4556 : // An expression that evaluates to some characteristic of a slice.
4557 : // This is used when indexing, bound-checking, or nil checking a slice.
4558 :
4559 : class Slice_info_expression : public Expression
4560 : {
4561 : public:
4562 692526 : Slice_info_expression(Expression* slice, Slice_info slice_info,
4563 : Location location)
4564 692526 : : Expression(EXPRESSION_SLICE_INFO, location),
4565 692526 : slice_(slice), slice_info_(slice_info)
4566 : { }
4567 :
4568 : // The slice operand of this slice info expression.
4569 : Expression*
4570 72 : slice() const
4571 72 : { return this->slice_; }
4572 :
4573 : // The info this expression is about.
4574 : Slice_info
4575 72 : info() const
4576 72 : { return this->slice_info_; }
4577 :
4578 : protected:
4579 : int
4580 497523 : do_traverse(Traverse* traverse)
4581 497523 : { return Expression::traverse(&this->slice_, traverse); }
4582 :
4583 : Type*
4584 : do_type();
4585 :
4586 : void
4587 521303 : do_determine_type(Gogo* gogo, const Type_context*)
4588 521303 : { this->slice_->determine_type_no_context(gogo); }
4589 :
4590 : Expression*
4591 0 : do_copy()
4592 : {
4593 0 : return new Slice_info_expression(this->slice_->copy(), this->slice_info_,
4594 0 : this->location());
4595 : }
4596 :
4597 : Bexpression*
4598 : do_get_backend(Translate_context* context);
4599 :
4600 : void
4601 : do_dump_expression(Ast_dump_context*) const;
4602 :
4603 : void
4604 0 : do_issue_nil_check()
4605 0 : { this->slice_->issue_nil_check(); }
4606 :
4607 : private:
4608 : // The slice for which we are getting information.
4609 : Expression* slice_;
4610 : // What information we want.
4611 : Slice_info slice_info_;
4612 : };
4613 :
4614 : // Conditional expressions.
4615 :
4616 : class Conditional_expression : public Expression
4617 : {
4618 : public:
4619 381973 : Conditional_expression(Expression* cond, Expression* then_expr,
4620 : Expression* else_expr, Location location)
4621 381973 : : Expression(EXPRESSION_CONDITIONAL, location),
4622 381973 : cond_(cond), then_(then_expr), else_(else_expr)
4623 : {}
4624 :
4625 : Expression*
4626 : condition() const
4627 : { return this->cond_; }
4628 :
4629 : Expression*
4630 1718 : then_expr() const
4631 1718 : { return this->then_; }
4632 :
4633 : Expression*
4634 1718 : else_expr() const
4635 1718 : { return this->else_; }
4636 :
4637 : protected:
4638 : int
4639 : do_traverse(Traverse*);
4640 :
4641 : Type*
4642 : do_type();
4643 :
4644 : void
4645 : do_determine_type(Gogo*, const Type_context*);
4646 :
4647 : Expression*
4648 0 : do_copy()
4649 : {
4650 0 : return new Conditional_expression(this->cond_->copy(), this->then_->copy(),
4651 0 : this->else_->copy(), this->location());
4652 : }
4653 :
4654 : Bexpression*
4655 : do_get_backend(Translate_context* context);
4656 :
4657 : void
4658 : do_dump_expression(Ast_dump_context*) const;
4659 :
4660 : private:
4661 : // The condition to be checked.
4662 : Expression* cond_;
4663 : // The expression to execute if the condition is true.
4664 : Expression* then_;
4665 : // The expression to execute if the condition is false.
4666 : Expression* else_;
4667 : };
4668 :
4669 : // Compound expressions.
4670 :
4671 : class Compound_expression : public Expression
4672 : {
4673 : public:
4674 100033 : Compound_expression(Expression* init, Expression* expr, Location location)
4675 100033 : : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr)
4676 : {}
4677 :
4678 : Expression*
4679 : init() const
4680 : { return this->init_; }
4681 :
4682 : Expression*
4683 30 : expr() const
4684 30 : { return this->expr_; }
4685 :
4686 : protected:
4687 : int
4688 : do_traverse(Traverse*);
4689 :
4690 : Type*
4691 : do_type();
4692 :
4693 : void
4694 : do_determine_type(Gogo*, const Type_context*);
4695 :
4696 : Expression*
4697 0 : do_copy()
4698 : {
4699 0 : return new Compound_expression(this->init_->copy(), this->expr_->copy(),
4700 0 : this->location());
4701 : }
4702 :
4703 : Bexpression*
4704 : do_get_backend(Translate_context* context);
4705 :
4706 : void
4707 : do_dump_expression(Ast_dump_context*) const;
4708 :
4709 : private:
4710 : // The expression that is evaluated first and discarded.
4711 : Expression* init_;
4712 : // The expression that is evaluated and returned.
4713 : Expression* expr_;
4714 : };
4715 :
4716 : // A backend expression. This is a backend expression wrapped in an
4717 : // Expression, for convenience during backend generation.
4718 :
4719 : class Backend_expression : public Expression
4720 : {
4721 : public:
4722 769407 : Backend_expression(Bexpression* bexpr, Type* type, Location location)
4723 769407 : : Expression(EXPRESSION_BACKEND, location), bexpr_(bexpr), type_(type)
4724 : {}
4725 :
4726 : protected:
4727 : int
4728 : do_traverse(Traverse*);
4729 :
4730 : // For now these are always valid static initializers. If that
4731 : // changes we can change this.
4732 : bool
4733 2660 : do_is_static_initializer() const
4734 2660 : { return true; }
4735 :
4736 : Type*
4737 929703 : do_type()
4738 929703 : { return this->type_; }
4739 :
4740 : void
4741 784909 : do_determine_type(Gogo*, const Type_context*)
4742 784909 : { }
4743 :
4744 : Expression*
4745 : do_copy();
4746 :
4747 : Bexpression*
4748 769400 : do_get_backend(Translate_context*)
4749 769400 : { return this->bexpr_; }
4750 :
4751 : void
4752 : do_dump_expression(Ast_dump_context*) const;
4753 :
4754 : private:
4755 : // The backend expression we are wrapping.
4756 : Bexpression* bexpr_;
4757 : // The type of the expression;
4758 : Type* type_;
4759 : };
4760 :
4761 : // A numeric constant. This is used both for untyped constants and
4762 : // for constants that have a type.
4763 :
4764 : class Numeric_constant
4765 : {
4766 : public:
4767 27637154 : Numeric_constant()
4768 27637154 : : classification_(NC_INVALID), type_(NULL)
4769 : { }
4770 :
4771 : ~Numeric_constant();
4772 :
4773 : Numeric_constant(const Numeric_constant&);
4774 :
4775 : Numeric_constant& operator=(const Numeric_constant&);
4776 :
4777 : // Check equality with another numeric constant.
4778 : bool
4779 : equals(const Numeric_constant&) const;
4780 :
4781 : // Set to an unsigned long value.
4782 : void
4783 : set_unsigned_long(Type*, unsigned long);
4784 :
4785 : // Set to an integer value.
4786 : void
4787 : set_int(Type*, const mpz_t);
4788 :
4789 : // Set to a rune value.
4790 : void
4791 : set_rune(Type*, const mpz_t);
4792 :
4793 : // Set to a floating point value.
4794 : void
4795 : set_float(Type*, const mpfr_t);
4796 :
4797 : // Set to a complex value.
4798 : void
4799 : set_complex(Type*, const mpc_t);
4800 :
4801 : // Mark numeric constant as invalid.
4802 : void
4803 998 : set_invalid()
4804 998 : { this->classification_ = NC_INVALID; }
4805 :
4806 : // Classifiers.
4807 : bool
4808 225968 : is_int() const
4809 158533 : { return this->classification_ == Numeric_constant::NC_INT; }
4810 :
4811 : bool
4812 546209 : is_rune() const
4813 546209 : { return this->classification_ == Numeric_constant::NC_RUNE; }
4814 :
4815 : bool
4816 4051 : is_float() const
4817 4051 : { return this->classification_ == Numeric_constant::NC_FLOAT; }
4818 :
4819 : bool
4820 10 : is_complex() const
4821 10 : { return this->classification_ == Numeric_constant::NC_COMPLEX; }
4822 :
4823 : bool
4824 : is_invalid() const
4825 : { return this->classification_ == Numeric_constant::NC_INVALID; }
4826 :
4827 : // Value retrievers. These will initialize the values as well as
4828 : // set them. GET_INT is only valid if IS_INT returns true, and
4829 : // likewise respectively.
4830 : void
4831 : get_int(mpz_t*) const;
4832 :
4833 : void
4834 : get_rune(mpz_t*) const;
4835 :
4836 : void
4837 : get_float(mpfr_t*) const;
4838 :
4839 : void
4840 : get_complex(mpc_t*) const;
4841 :
4842 : // Codes returned by to_unsigned_long.
4843 : enum To_unsigned_long
4844 : {
4845 : // Value is integer and fits in unsigned long.
4846 : NC_UL_VALID,
4847 : // Value is not integer.
4848 : NC_UL_NOTINT,
4849 : // Value is integer but is negative.
4850 : NC_UL_NEGATIVE,
4851 : // Value is non-negative integer but does not fit in unsigned
4852 : // long.
4853 : NC_UL_BIG
4854 : };
4855 :
4856 : // If the value can be expressed as an integer that fits in an
4857 : // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
4858 : // one of the other To_unsigned_long codes.
4859 : To_unsigned_long
4860 : to_unsigned_long(unsigned long* val) const;
4861 :
4862 : // If the value can be expressed as an integer that describes the
4863 : // size of an object in memory, set *VAL and return true.
4864 : // Otherwise, return false. Currently we use int64_t to represent a
4865 : // memory size, as in Type::backend_type_size.
4866 : bool
4867 : to_memory_size(int64_t* val) const;
4868 :
4869 : // If the value can be expressed as an int, return true and
4870 : // initialize and set VAL. This will return false for a value with
4871 : // an explicit float or complex type, even if the value is integral.
4872 : bool
4873 : to_int(mpz_t* val) const;
4874 :
4875 : // If the value can be expressed as a float, return true and
4876 : // initialize and set VAL.
4877 : bool
4878 : to_float(mpfr_t* val) const;
4879 :
4880 : // If the value can be expressed as a complex, return true and
4881 : // initialize and set VR and VI.
4882 : bool
4883 : to_complex(mpc_t* val) const;
4884 :
4885 : // Get the type.
4886 : Type*
4887 : type() const;
4888 :
4889 : // If the constant can be expressed in TYPE, then set the type of
4890 : // the constant to TYPE and return true. Otherwise return false,
4891 : // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
4892 : // the location to use for the error.
4893 : bool
4894 : set_type(Type* type, bool issue_error, Location location);
4895 :
4896 : // Return an Expression for this value.
4897 : Expression*
4898 : expression(Location) const;
4899 :
4900 : // Calculate a hash code with a given seed.
4901 : unsigned int
4902 : hash(unsigned int seed) const;
4903 :
4904 : private:
4905 : void
4906 : clear();
4907 :
4908 : To_unsigned_long
4909 : mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
4910 :
4911 : To_unsigned_long
4912 : mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
4913 :
4914 : bool
4915 : mpz_to_memory_size(const mpz_t ival, int64_t* val) const;
4916 :
4917 : bool
4918 : mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const;
4919 :
4920 : bool
4921 : check_int_type(Integer_type*, bool, Location);
4922 :
4923 : bool
4924 : check_float_type(Float_type*, bool, Location);
4925 :
4926 : bool
4927 : check_complex_type(Complex_type*, bool, Location);
4928 :
4929 : static bool
4930 : is_float_neg_zero(const mpfr_t, int bits);
4931 :
4932 : // The kinds of constants.
4933 : enum Classification
4934 : {
4935 : NC_INVALID,
4936 : NC_RUNE,
4937 : NC_INT,
4938 : NC_FLOAT,
4939 : NC_COMPLEX
4940 : };
4941 :
4942 : // The kind of constant.
4943 : Classification classification_;
4944 : // The value.
4945 : union
4946 : {
4947 : // If NC_INT or NC_RUNE.
4948 : mpz_t int_val;
4949 : // If NC_FLOAT.
4950 : mpfr_t float_val;
4951 : // If NC_COMPLEX.
4952 : mpc_t complex_val;
4953 : } u_;
4954 : // The type if there is one. This will be NULL for an untyped
4955 : // constant.
4956 : Type* type_;
4957 : };
4958 :
4959 : // Temporary buffer size for string conversions.
4960 : // Also known to the runtime as tmpStringBufSize in runtime/string.go.
4961 : static const int tmp_string_buf_size = 32;
4962 :
4963 : #endif // !defined(GO_EXPRESSIONS_H)
|