1 // expressions.h -- Go frontend expression handling. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #ifndef GO_EXPRESSIONS_H
8 #define GO_EXPRESSIONS_H
15 class Translate_context
;
17 class Statement_inserter
;
28 class Expression_list
;
30 class Temporary_reference_expression
;
31 class Set_and_use_temporary_expression
;
32 class String_expression
;
33 class Binary_expression
;
34 class Call_expression
;
35 class Func_expression
;
36 class Func_descriptor_expression
;
37 class Unknown_expression
;
38 class Index_expression
;
39 class Map_index_expression
;
40 class Bound_method_expression
;
41 class Field_reference_expression
;
42 class Interface_field_reference_expression
;
43 class Type_guard_expression
;
44 class Receive_expression
;
45 class Numeric_constant
;
49 class Temporary_statement
;
51 class Ast_dump_context
;
54 // The base class for all expressions.
59 // The types of expressions.
60 enum Expression_classification
66 EXPRESSION_CONST_REFERENCE
,
67 EXPRESSION_VAR_REFERENCE
,
68 EXPRESSION_TEMPORARY_REFERENCE
,
69 EXPRESSION_SET_AND_USE_TEMPORARY
,
71 EXPRESSION_FUNC_REFERENCE
,
72 EXPRESSION_FUNC_DESCRIPTOR
,
73 EXPRESSION_FUNC_CODE_REFERENCE
,
74 EXPRESSION_UNKNOWN_REFERENCE
,
83 EXPRESSION_CALL_RESULT
,
84 EXPRESSION_BOUND_METHOD
,
86 EXPRESSION_ARRAY_INDEX
,
87 EXPRESSION_STRING_INDEX
,
90 EXPRESSION_FIELD_REFERENCE
,
91 EXPRESSION_INTERFACE_FIELD_REFERENCE
,
92 EXPRESSION_ALLOCATION
,
93 EXPRESSION_TYPE_GUARD
,
94 EXPRESSION_CONVERSION
,
95 EXPRESSION_UNSAFE_CONVERSION
,
96 EXPRESSION_STRUCT_CONSTRUCTION
,
97 EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
98 EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
99 EXPRESSION_MAP_CONSTRUCTION
,
100 EXPRESSION_COMPOSITE_LITERAL
,
101 EXPRESSION_HEAP_COMPOSITE
,
103 EXPRESSION_TYPE_DESCRIPTOR
,
104 EXPRESSION_TYPE_INFO
,
105 EXPRESSION_STRUCT_FIELD_OFFSET
,
106 EXPRESSION_MAP_DESCRIPTOR
,
107 EXPRESSION_LABEL_ADDR
110 Expression(Expression_classification
, Location
);
112 virtual ~Expression();
114 // Make an error expression. This is used when a parse error occurs
115 // to prevent cascading errors.
117 make_error(Location
);
119 // Make an expression which is really a type. This is used during
122 make_type(Type
*, Location
);
124 // Make a unary expression.
126 make_unary(Operator
, Expression
*, Location
);
128 // Make a binary expression.
130 make_binary(Operator
, Expression
*, Expression
*, Location
);
132 // Make a reference to a constant in an expression.
134 make_const_reference(Named_object
*, Location
);
136 // Make a reference to a variable in an expression.
138 make_var_reference(Named_object
*, Location
);
140 // Make a reference to a temporary variable. Temporary variables
141 // are always created by a single statement, which is what we use to
143 static Temporary_reference_expression
*
144 make_temporary_reference(Temporary_statement
*, Location
);
146 // Make an expressions which sets a temporary variable and then
147 // evaluates to a reference to that temporary variable. This is
148 // used to set a temporary variable while retaining the order of
150 static Set_and_use_temporary_expression
*
151 make_set_and_use_temporary(Temporary_statement
*, Expression
*, Location
);
153 // Make a sink expression--a reference to the blank identifier _.
157 // Make a reference to a function in an expression. This returns a
158 // pointer to the struct holding the address of the function
159 // followed by any closed-over variables.
161 make_func_reference(Named_object
*, Expression
* closure
, Location
);
163 // Make a function descriptor, an immutable struct with a single
164 // field that points to the function code. This may only be used
165 // with functions that do not have closures. FN is the function for
166 // which we are making the descriptor.
167 static Func_descriptor_expression
*
168 make_func_descriptor(Named_object
* fn
);
170 // Make a reference to the code of a function. This is used to set
171 // descriptor and closure fields.
173 make_func_code_reference(Named_object
*, Location
);
175 // Make a reference to an unknown name. In a correct program this
176 // will always be lowered to a real const/var/func reference.
177 static Unknown_expression
*
178 make_unknown_reference(Named_object
*, Location
);
180 // Make a constant bool expression.
182 make_boolean(bool val
, Location
);
184 // Make a constant string expression.
186 make_string(const std::string
&, Location
);
188 // Make a character constant expression. TYPE should be NULL for an
191 make_character(const mpz_t
*, Type
*, Location
);
193 // Make a constant integer expression. TYPE should be NULL for an
196 make_integer(const mpz_t
*, Type
*, Location
);
198 // Make a constant float expression. TYPE should be NULL for an
201 make_float(const mpfr_t
*, Type
*, Location
);
203 // Make a constant complex expression. TYPE should be NULL for an
206 make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
*, Location
);
208 // Make a nil expression.
212 // Make an iota expression. This is used for the predeclared
217 // Make a call expression.
218 static Call_expression
*
219 make_call(Expression
* func
, Expression_list
* args
, bool is_varargs
,
222 // Make a reference to a specific result of a call expression which
225 make_call_result(Call_expression
*, unsigned int index
);
227 // Make an expression which is a method bound to its first
228 // parameter. METHOD is the method being called, FUNCTION is the
230 static Bound_method_expression
*
231 make_bound_method(Expression
* object
, const Method
* method
,
232 Named_object
* function
, Location
);
234 // Make an index or slice expression. This is a parser expression
235 // which represents LEFT[START:END]. END may be NULL, meaning an
236 // index rather than a slice. At parse time we may not know the
237 // type of LEFT. After parsing this is lowered to an array index, a
238 // string index, or a map index.
240 make_index(Expression
* left
, Expression
* start
, Expression
* end
,
243 // Make an array index expression. END may be NULL, in which case
244 // this is an lvalue.
246 make_array_index(Expression
* array
, Expression
* start
, Expression
* end
,
249 // Make a string index expression. END may be NULL. This is never
252 make_string_index(Expression
* string
, Expression
* start
, Expression
* end
,
255 // Make a map index expression. This is an lvalue.
256 static Map_index_expression
*
257 make_map_index(Expression
* map
, Expression
* val
, Location
);
259 // Make a selector. This is a parser expression which represents
260 // LEFT.NAME. At parse time we may not know the type of the left
263 make_selector(Expression
* left
, const std::string
& name
, Location
);
265 // Make a reference to a field in a struct.
266 static Field_reference_expression
*
267 make_field_reference(Expression
*, unsigned int field_index
, Location
);
269 // Make a reference to a field of an interface, with an associated
272 make_interface_field_reference(Expression
*, const std::string
&,
275 // Make an allocation expression.
277 make_allocation(Type
*, Location
);
279 // Make a type guard expression.
281 make_type_guard(Expression
*, Type
*, Location
);
283 // Make a type cast expression.
285 make_cast(Type
*, Expression
*, Location
);
287 // Make an unsafe type cast expression. This is only used when
288 // passing parameter to builtin functions that are part of the Go
291 make_unsafe_cast(Type
*, Expression
*, Location
);
293 // Make a composite literal. The DEPTH parameter is how far down we
294 // are in a list of composite literals with omitted types.
296 make_composite_literal(Type
*, int depth
, bool has_keys
, Expression_list
*,
299 // Make a struct composite literal.
301 make_struct_composite_literal(Type
*, Expression_list
*, Location
);
303 // Make a slice composite literal.
305 make_slice_composite_literal(Type
*, Expression_list
*, Location
);
307 // Take a composite literal and allocate it on the heap.
309 make_heap_composite(Expression
*, Location
);
311 // Make a receive expression. VAL is NULL for a unary receive.
312 static Receive_expression
*
313 make_receive(Expression
* channel
, Location
);
315 // Make an expression which evaluates to the address of the type
316 // descriptor for TYPE.
318 make_type_descriptor(Type
* type
, Location
);
320 // Make an expression which evaluates to some characteristic of a
321 // type. These are only used for type descriptors, so there is no
322 // location parameter.
325 // The size of a value of the type.
327 // The required alignment of a value of the type.
329 // The required alignment of a value of the type when used as a
330 // field in a struct.
331 TYPE_INFO_FIELD_ALIGNMENT
335 make_type_info(Type
* type
, Type_info
);
337 // Make an expression which evaluates to the offset of a field in a
338 // struct. This is only used for type descriptors, so there is no
339 // location parameter.
341 make_struct_field_offset(Struct_type
*, const Struct_field
*);
343 // Make an expression which evaluates to the address of the map
344 // descriptor for TYPE.
346 make_map_descriptor(Map_type
* type
, Location
);
348 // Make an expression which evaluates to the address of an unnamed
351 make_label_addr(Label
*, Location
);
353 // Return the expression classification.
354 Expression_classification
355 classification() const
356 { return this->classification_
; }
358 // Return the location of the expression.
361 { return this->location_
; }
363 // Return whether this is a constant expression.
366 { return this->do_is_constant(); }
368 // If this is not a numeric constant, return false. If it is one,
369 // return true, and set VAL to hold the value.
371 numeric_constant_value(Numeric_constant
* val
) const
372 { return this->do_numeric_constant_value(val
); }
374 // If this is not a constant expression with string type, return
375 // false. If it is one, return true, and set VAL to the value.
377 string_constant_value(std::string
* val
) const
378 { return this->do_string_constant_value(val
); }
380 // This is called if the value of this expression is being
381 // discarded. This issues warnings about computed values being
382 // unused. This returns true if all is well, false if it issued an
386 { return this->do_discarding_value(); }
388 // Return whether this is an error expression.
390 is_error_expression() const
391 { return this->classification_
== EXPRESSION_ERROR
; }
393 // Return whether this expression really represents a type.
395 is_type_expression() const
396 { return this->classification_
== EXPRESSION_TYPE
; }
398 // If this is a variable reference, return the Var_expression
399 // structure. Otherwise, return NULL. This is a controlled dynamic
403 { return this->convert
<Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
405 const Var_expression
*
406 var_expression() const
407 { return this->convert
<const Var_expression
, EXPRESSION_VAR_REFERENCE
>(); }
409 // If this is a reference to a temporary variable, return the
410 // Temporary_reference_expression. Otherwise, return NULL.
411 Temporary_reference_expression
*
412 temporary_reference_expression()
414 return this->convert
<Temporary_reference_expression
,
415 EXPRESSION_TEMPORARY_REFERENCE
>();
418 // If this is a set-and-use-temporary, return the
419 // Set_and_use_temporary_expression. Otherwise, return NULL.
420 Set_and_use_temporary_expression
*
421 set_and_use_temporary_expression()
423 return this->convert
<Set_and_use_temporary_expression
,
424 EXPRESSION_SET_AND_USE_TEMPORARY
>();
427 // Return whether this is a sink expression.
429 is_sink_expression() const
430 { return this->classification_
== EXPRESSION_SINK
; }
432 // If this is a string expression, return the String_expression
433 // structure. Otherwise, return NULL.
436 { return this->convert
<String_expression
, EXPRESSION_STRING
>(); }
438 // Return whether this is the expression nil.
440 is_nil_expression() const
441 { return this->classification_
== EXPRESSION_NIL
; }
443 // If this is an indirection through a pointer, return the
444 // expression being pointed through. Otherwise return this.
448 // If this is a binary expression, return the Binary_expression
449 // structure. Otherwise return NULL.
452 { return this->convert
<Binary_expression
, EXPRESSION_BINARY
>(); }
454 // If this is a call expression, return the Call_expression
455 // structure. Otherwise, return NULL. This is a controlled dynamic
459 { return this->convert
<Call_expression
, EXPRESSION_CALL
>(); }
461 // If this is an expression which refers to a function, return the
462 // Func_expression structure. Otherwise, return NULL.
465 { return this->convert
<Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
467 const Func_expression
*
468 func_expression() const
469 { return this->convert
<const Func_expression
, EXPRESSION_FUNC_REFERENCE
>(); }
471 // If this is an expression which refers to an unknown name, return
472 // the Unknown_expression structure. Otherwise, return NULL.
475 { return this->convert
<Unknown_expression
, EXPRESSION_UNKNOWN_REFERENCE
>(); }
477 const Unknown_expression
*
478 unknown_expression() const
480 return this->convert
<const Unknown_expression
,
481 EXPRESSION_UNKNOWN_REFERENCE
>();
484 // If this is an index expression, return the Index_expression
485 // structure. Otherwise, return NULL.
488 { return this->convert
<Index_expression
, EXPRESSION_INDEX
>(); }
490 // If this is an expression which refers to indexing in a map,
491 // return the Map_index_expression structure. Otherwise, return
493 Map_index_expression
*
494 map_index_expression()
495 { return this->convert
<Map_index_expression
, EXPRESSION_MAP_INDEX
>(); }
497 // If this is a bound method expression, return the
498 // Bound_method_expression structure. Otherwise, return NULL.
499 Bound_method_expression
*
500 bound_method_expression()
501 { return this->convert
<Bound_method_expression
, EXPRESSION_BOUND_METHOD
>(); }
503 // If this is a reference to a field in a struct, return the
504 // Field_reference_expression structure. Otherwise, return NULL.
505 Field_reference_expression
*
506 field_reference_expression()
508 return this->convert
<Field_reference_expression
,
509 EXPRESSION_FIELD_REFERENCE
>();
512 // If this is a reference to a field in an interface, return the
513 // Interface_field_reference_expression structure. Otherwise,
515 Interface_field_reference_expression
*
516 interface_field_reference_expression()
518 return this->convert
<Interface_field_reference_expression
,
519 EXPRESSION_INTERFACE_FIELD_REFERENCE
>();
522 // If this is a type guard expression, return the
523 // Type_guard_expression structure. Otherwise, return NULL.
524 Type_guard_expression
*
525 type_guard_expression()
526 { return this->convert
<Type_guard_expression
, EXPRESSION_TYPE_GUARD
>(); }
528 // If this is a receive expression, return the Receive_expression
529 // structure. Otherwise, return NULL.
532 { return this->convert
<Receive_expression
, EXPRESSION_RECEIVE
>(); }
534 // Return true if this is a composite literal.
536 is_composite_literal() const;
538 // Return true if this is a composite literal which is not constant.
540 is_nonconstant_composite_literal() const;
542 // Return true if this is a reference to a local variable.
544 is_local_variable() const;
546 // Make the builtin function descriptor type, so that it can be
549 make_func_descriptor_type();
551 // Traverse an expression.
553 traverse(Expression
**, Traverse
*);
555 // Traverse subexpressions of this expression.
557 traverse_subexpressions(Traverse
*);
559 // Lower an expression. This is called immediately after parsing.
560 // FUNCTION is the function we are in; it will be NULL for an
561 // expression initializing a global variable. INSERTER may be used
562 // to insert statements before the statement or initializer
563 // containing this expression; it is normally used to create
564 // temporary variables. IOTA_VALUE is the value that we should give
565 // to any iota expressions. This function must resolve expressions
566 // which could not be fully parsed into their final form. It
567 // returns the same Expression or a new one.
569 lower(Gogo
* gogo
, Named_object
* function
, Statement_inserter
* inserter
,
571 { return this->do_lower(gogo
, function
, inserter
, iota_value
); }
573 // Determine the real type of an expression with abstract integer,
574 // floating point, or complex type. TYPE_CONTEXT describes the
577 determine_type(const Type_context
*);
579 // Check types in an expression.
581 check_types(Gogo
* gogo
)
582 { this->do_check_types(gogo
); }
584 // Determine the type when there is no context.
586 determine_type_no_context();
588 // Return the current type of the expression. This may be changed
589 // by determine_type.
592 { return this->do_type(); }
594 // Return a copy of an expression.
597 { return this->do_copy(); }
599 // Return whether the expression is addressable--something which may
600 // be used as the operand of the unary & operator.
602 is_addressable() const
603 { return this->do_is_addressable(); }
605 // Note that we are taking the address of this expression. ESCAPES
606 // is true if this address escapes the current function.
608 address_taken(bool escapes
)
609 { this->do_address_taken(escapes
); }
611 // Return whether this expression must be evaluated in order
612 // according to the order of evaluation rules. This is basically
613 // true of all expressions with side-effects.
615 must_eval_in_order() const
616 { return this->do_must_eval_in_order(); }
618 // Return whether subexpressions of this expression must be
619 // evaluated in order. This is true of index expressions and
620 // pointer indirections. This sets *SKIP to the number of
621 // subexpressions to skip during traversing, as index expressions
622 // only requiring moving the index, not the array.
624 must_eval_subexpressions_in_order(int* skip
) const
627 return this->do_must_eval_subexpressions_in_order(skip
);
630 // Return the tree for this expression.
632 get_tree(Translate_context
*);
634 // Return a tree handling any conversions which must be done during
637 convert_for_assignment(Translate_context
*, Type
* lhs_type
, Type
* rhs_type
,
638 tree rhs_tree
, Location location
);
640 // Return a tree converting a value of one interface type to another
641 // interface type. If FOR_TYPE_GUARD is true this is for a type
644 convert_interface_to_interface(Translate_context
*, Type
* lhs_type
,
645 Type
* rhs_type
, tree rhs_tree
,
646 bool for_type_guard
, Location
);
648 // Return a tree implementing the comparison LHS_TREE OP RHS_TREE.
649 // TYPE is the type of both sides.
651 comparison_tree(Translate_context
*, Type
* result_type
, Operator op
,
652 Type
* left_type
, tree left_tree
, Type
* right_type
,
653 tree right_tree
, Location
);
655 // Return a tree for the multi-precision integer VAL in TYPE.
657 integer_constant_tree(mpz_t val
, tree type
);
659 // Return a tree for the floating point value VAL in TYPE.
661 float_constant_tree(mpfr_t val
, tree type
);
663 // Return a tree for the complex value REAL/IMAG in TYPE.
665 complex_constant_tree(mpfr_t real
, mpfr_t imag
, tree type
);
667 // Export the expression. This is only used for constants. It will
668 // be used for things like values of named constants and sizes of
671 export_expression(Export
* exp
) const
672 { this->do_export(exp
); }
674 // Import an expression.
676 import_expression(Import
*);
678 // Return a tree which checks that VAL, of arbitrary integer type,
679 // is non-negative and is not more than the maximum value of
680 // BOUND_TYPE. If SOFAR is not NULL, it is or'red into the result.
681 // The return value may be NULL if SOFAR is NULL.
683 check_bounds(tree val
, tree bound_type
, tree sofar
, Location
);
685 // Dump an expression to a dump constext.
687 dump_expression(Ast_dump_context
*) const;
690 // May be implemented by child class: traverse the expressions.
692 do_traverse(Traverse
*);
694 // Return a lowered expression.
696 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int)
699 // Return whether this is a constant expression.
701 do_is_constant() const
704 // Return whether this is a constant expression of numeric type, and
705 // set the Numeric_constant to the value.
707 do_numeric_constant_value(Numeric_constant
*) const
710 // Return whether this is a constant expression of string type, and
711 // set VAL to the value.
713 do_string_constant_value(std::string
*) const
716 // Called by the parser if the value is being discarded.
718 do_discarding_value();
720 // Child class holds type.
724 // Child class implements determining type information.
726 do_determine_type(const Type_context
*) = 0;
728 // Child class implements type checking if needed.
730 do_check_types(Gogo
*)
733 // Child class implements copying.
737 // Child class implements whether the expression is addressable.
739 do_is_addressable() const
742 // Child class implements taking the address of an expression.
744 do_address_taken(bool)
747 // Child class implements whether this expression must be evaluated
750 do_must_eval_in_order() const
753 // Child class implements whether this expressions requires that
754 // subexpressions be evaluated in order. The child implementation
755 // may set *SKIP if it should be non-zero.
757 do_must_eval_subexpressions_in_order(int* /* skip */) const
760 // Child class implements conversion to tree.
762 do_get_tree(Translate_context
*) = 0;
764 // Child class implements export.
766 do_export(Export
*) const;
768 // For children to call to give an error for an unused value.
770 unused_value_error();
772 // For children to call when they detect that they are in error.
776 // For children to call to report an error conveniently.
778 report_error(const char*);
780 // Child class implements dumping to a dump context.
782 do_dump_expression(Ast_dump_context
*) const = 0;
785 // Convert to the desired statement classification, or return NULL.
786 // This is a controlled dynamic cast.
787 template<typename Expression_class
,
788 Expression_classification expr_classification
>
792 return (this->classification_
== expr_classification
793 ? static_cast<Expression_class
*>(this)
797 template<typename Expression_class
,
798 Expression_classification expr_classification
>
799 const Expression_class
*
802 return (this->classification_
== expr_classification
803 ? static_cast<const Expression_class
*>(this)
808 convert_type_to_interface(Translate_context
*, Type
*, Type
*, tree
,
812 get_interface_type_descriptor(Translate_context
*, Type
*, tree
,
816 convert_interface_to_type(Translate_context
*, Type
*, Type
*, tree
,
819 // The expression classification.
820 Expression_classification classification_
;
821 // The location in the input file.
825 // A list of Expressions.
827 class Expression_list
834 // Return whether the list is empty.
837 { return this->entries_
.empty(); }
839 // Return the number of entries in the list.
842 { return this->entries_
.size(); }
844 // Add an entry to the end of the list.
846 push_back(Expression
* expr
)
847 { this->entries_
.push_back(expr
); }
850 append(Expression_list
* add
)
851 { this->entries_
.insert(this->entries_
.end(), add
->begin(), add
->end()); }
853 // Reserve space in the list.
856 { this->entries_
.reserve(size
); }
858 // Traverse the expressions in the list.
866 // Return true if the list contains an error expression.
868 contains_error() const;
870 // Retrieve an element by index.
873 { return this->entries_
.at(i
); }
875 // Return the first and last elements.
878 { return this->entries_
.front(); }
882 { return this->entries_
.front(); }
886 { return this->entries_
.back(); }
890 { return this->entries_
.back(); }
894 typedef std::vector
<Expression
*>::iterator iterator
;
895 typedef std::vector
<Expression
*>::const_iterator const_iterator
;
899 { return this->entries_
.begin(); }
903 { return this->entries_
.begin(); }
907 { return this->entries_
.end(); }
911 { return this->entries_
.end(); }
916 { this->entries_
.erase(p
); }
919 std::vector
<Expression
*> entries_
;
922 // An abstract base class for an expression which is only used by the
923 // parser, and is lowered in the lowering pass.
925 class Parser_expression
: public Expression
928 Parser_expression(Expression_classification classification
,
930 : Expression(classification
, location
)
935 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int) = 0;
941 do_determine_type(const Type_context
*)
942 { go_unreachable(); }
945 do_check_types(Gogo
*)
946 { go_unreachable(); }
949 do_get_tree(Translate_context
*)
950 { go_unreachable(); }
953 // An expression which is simply a variable.
955 class Var_expression
: public Expression
958 Var_expression(Named_object
* variable
, Location location
)
959 : Expression(EXPRESSION_VAR_REFERENCE
, location
),
963 // Return the variable.
966 { return this->variable_
; }
970 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
976 do_determine_type(const Type_context
*);
983 do_is_addressable() const
987 do_address_taken(bool);
990 do_get_tree(Translate_context
*);
993 do_dump_expression(Ast_dump_context
*) const;
996 // The variable we are referencing.
997 Named_object
* variable_
;
1000 // A reference to a temporary variable.
1002 class Temporary_reference_expression
: public Expression
1005 Temporary_reference_expression(Temporary_statement
* statement
,
1007 : Expression(EXPRESSION_TEMPORARY_REFERENCE
, location
),
1008 statement_(statement
), is_lvalue_(false)
1011 // The temporary that this expression refers to.
1012 Temporary_statement
*
1014 { return this->statement_
; }
1016 // Indicate that this reference appears on the left hand side of an
1017 // assignment statement.
1020 { this->is_lvalue_
= true; }
1027 do_determine_type(const Type_context
*)
1032 { return make_temporary_reference(this->statement_
, this->location()); }
1035 do_is_addressable() const
1039 do_address_taken(bool);
1042 do_get_tree(Translate_context
*);
1045 do_dump_expression(Ast_dump_context
*) const;
1048 // The statement where the temporary variable is defined.
1049 Temporary_statement
* statement_
;
1050 // Whether this reference appears on the left hand side of an
1051 // assignment statement.
1055 // Set and use a temporary variable.
1057 class Set_and_use_temporary_expression
: public Expression
1060 Set_and_use_temporary_expression(Temporary_statement
* statement
,
1061 Expression
* expr
, Location location
)
1062 : Expression(EXPRESSION_SET_AND_USE_TEMPORARY
, location
),
1063 statement_(statement
), expr_(expr
)
1066 // Return the temporary.
1067 Temporary_statement
*
1069 { return this->statement_
; }
1071 // Return the expression.
1074 { return this->expr_
; }
1078 do_traverse(Traverse
* traverse
)
1079 { return Expression::traverse(&this->expr_
, traverse
); }
1085 do_determine_type(const Type_context
*);
1090 return make_set_and_use_temporary(this->statement_
, this->expr_
,
1095 do_is_addressable() const
1099 do_address_taken(bool);
1102 do_get_tree(Translate_context
*);
1105 do_dump_expression(Ast_dump_context
*) const;
1108 // The statement where the temporary variable is defined.
1109 Temporary_statement
* statement_
;
1110 // The expression to assign to the temporary.
1114 // A string expression.
1116 class String_expression
: public Expression
1119 String_expression(const std::string
& val
, Location location
)
1120 : Expression(EXPRESSION_STRING
, location
),
1121 val_(val
), type_(NULL
)
1126 { return this->val_
; }
1133 do_is_constant() const
1137 do_string_constant_value(std::string
* val
) const
1147 do_determine_type(const Type_context
*);
1154 do_get_tree(Translate_context
*);
1156 // Write string literal to a string dump.
1158 export_string(String_dump
* exp
, const String_expression
* str
);
1161 do_export(Export
*) const;
1164 do_dump_expression(Ast_dump_context
*) const;
1167 // The string value. This is immutable.
1168 const std::string val_
;
1169 // The type as determined by context.
1173 // A binary expression.
1175 class Binary_expression
: public Expression
1178 Binary_expression(Operator op
, Expression
* left
, Expression
* right
,
1180 : Expression(EXPRESSION_BINARY
, location
),
1181 op_(op
), left_(left
), right_(right
), type_(NULL
)
1184 // Return the operator.
1187 { return this->op_
; }
1189 // Return the left hand expression.
1192 { return this->left_
; }
1194 // Return the right hand expression.
1197 { return this->right_
; }
1199 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1200 // Return true if this could be done, false if not. Issue errors at
1201 // LOCATION as appropriate.
1203 eval_constant(Operator op
, Numeric_constant
* left_nc
,
1204 Numeric_constant
* right_nc
, Location location
,
1205 Numeric_constant
* nc
);
1207 // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1208 // *RESULT. Return true if this could be done, false if not. Issue
1209 // errors at LOCATION as appropriate.
1211 compare_constant(Operator op
, Numeric_constant
* left_nc
,
1212 Numeric_constant
* right_nc
, Location location
,
1218 // Report an error if OP can not be applied to TYPE. Return whether
1219 // it can. OTYPE is the type of the other operand.
1221 check_operator_type(Operator op
, Type
* type
, Type
* otype
, Location
);
1225 do_traverse(Traverse
* traverse
);
1228 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1231 do_is_constant() const
1232 { return this->left_
->is_constant() && this->right_
->is_constant(); }
1235 do_numeric_constant_value(Numeric_constant
*) const;
1238 do_discarding_value();
1244 do_determine_type(const Type_context
*);
1247 do_check_types(Gogo
*);
1252 return Expression::make_binary(this->op_
, this->left_
->copy(),
1253 this->right_
->copy(), this->location());
1257 do_get_tree(Translate_context
*);
1260 do_export(Export
*) const;
1263 do_dump_expression(Ast_dump_context
*) const;
1267 operation_type(Operator op
, Type
* left_type
, Type
* right_type
,
1268 Type
** result_type
);
1271 cmp_to_bool(Operator op
, int cmp
);
1274 eval_integer(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
1275 Location
, Numeric_constant
*);
1278 eval_float(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
1279 Location
, Numeric_constant
*);
1282 eval_complex(Operator op
, const Numeric_constant
*, const Numeric_constant
*,
1283 Location
, Numeric_constant
*);
1286 compare_integer(const Numeric_constant
*, const Numeric_constant
*, int*);
1289 compare_float(const Numeric_constant
*, const Numeric_constant
*, int*);
1292 compare_complex(const Numeric_constant
*, const Numeric_constant
*, int*);
1295 lower_struct_comparison(Gogo
*, Statement_inserter
*);
1298 lower_array_comparison(Gogo
*, Statement_inserter
*);
1301 lower_compare_to_memcmp(Gogo
*, Statement_inserter
*);
1304 operand_address(Statement_inserter
*, Expression
*);
1306 // The binary operator to apply.
1308 // The left hand side operand.
1310 // The right hand side operand.
1312 // The type of a comparison operation.
1316 // A call expression. The go statement needs to dig inside this.
1318 class Call_expression
: public Expression
1321 Call_expression(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
1323 : Expression(EXPRESSION_CALL
, location
),
1324 fn_(fn
), args_(args
), type_(NULL
), results_(NULL
), tree_(NULL
),
1325 is_varargs_(is_varargs
), are_hidden_fields_ok_(false),
1326 varargs_are_lowered_(false), types_are_determined_(false),
1327 is_deferred_(false), issued_error_(false)
1330 // The function to call.
1333 { return this->fn_
; }
1338 { return this->args_
; }
1340 const Expression_list
*
1342 { return this->args_
; }
1344 // Get the function type.
1346 get_function_type() const;
1348 // Return the number of values this call will return.
1350 result_count() const;
1352 // Return the temporary variable which holds result I. This is only
1353 // valid after the expression has been lowered, and is only valid
1354 // for calls which return multiple results.
1355 Temporary_statement
*
1356 result(size_t i
) const;
1358 // Return whether this is a call to the predeclared function
1361 is_recover_call() const;
1363 // Set the argument for a call to recover.
1365 set_recover_arg(Expression
*);
1367 // Whether the last argument is a varargs argument (f(a...)).
1370 { return this->is_varargs_
; }
1372 // Note that varargs have already been lowered.
1374 set_varargs_are_lowered()
1375 { this->varargs_are_lowered_
= true; }
1377 // Note that it is OK for this call to set hidden fields when
1378 // passing arguments.
1380 set_hidden_fields_are_ok()
1381 { this->are_hidden_fields_ok_
= true; }
1383 // Whether this call is being deferred.
1386 { return this->is_deferred_
; }
1388 // Note that the call is being deferred.
1391 { this->is_deferred_
= true; }
1393 // We have found an error with this call expression; return true if
1394 // we should report it.
1400 do_traverse(Traverse
*);
1403 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1406 do_discarding_value()
1413 do_determine_type(const Type_context
*);
1416 do_check_types(Gogo
*);
1421 return Expression::make_call(this->fn_
->copy(),
1422 (this->args_
== NULL
1424 : this->args_
->copy()),
1425 this->is_varargs_
, this->location());
1429 do_must_eval_in_order() const;
1432 do_get_tree(Translate_context
*);
1435 do_is_recover_call() const;
1438 do_set_recover_arg(Expression
*);
1440 // Let a builtin expression change the argument list.
1442 set_args(Expression_list
* args
)
1443 { this->args_
= args
; }
1445 // Let a builtin expression lower varargs.
1447 lower_varargs(Gogo
*, Named_object
* function
, Statement_inserter
* inserter
,
1448 Type
* varargs_type
, size_t param_count
);
1450 // Let a builtin expression check whether types have been
1453 determining_types();
1456 do_dump_expression(Ast_dump_context
*) const;
1460 check_argument_type(int, const Type
*, const Type
*, Location
, bool);
1463 interface_method_function(Translate_context
*,
1464 Interface_field_reference_expression
*,
1468 set_results(Translate_context
*, tree
);
1470 // The function to call.
1472 // The arguments to pass. This may be NULL if there are no
1474 Expression_list
* args_
;
1475 // The type of the expression, to avoid recomputing it.
1477 // The list of temporaries which will hold the results if the
1478 // function returns a tuple.
1479 std::vector
<Temporary_statement
*>* results_
;
1480 // The tree for the call, used for a call which returns a tuple.
1482 // True if the last argument is a varargs argument (f(a...)).
1484 // True if this statement may pass hidden fields in the arguments.
1485 // This is used for generated method stubs.
1486 bool are_hidden_fields_ok_
;
1487 // True if varargs have already been lowered.
1488 bool varargs_are_lowered_
;
1489 // True if types have been determined.
1490 bool types_are_determined_
;
1491 // True if the call is an argument to a defer statement.
1493 // True if we reported an error about a mismatch between call
1494 // results and uses. This is to avoid producing multiple errors
1495 // when there are multiple Call_result_expressions.
1499 // An expression which represents a pointer to a function.
1501 class Func_expression
: public Expression
1504 Func_expression(Named_object
* function
, Expression
* closure
,
1506 : Expression(EXPRESSION_FUNC_REFERENCE
, location
),
1507 function_(function
), closure_(closure
)
1510 // Return the object associated with the function.
1512 named_object() const
1513 { return this->function_
; }
1515 // Return the closure for this function. This will return NULL if
1516 // the function has no closure, which is the normal case.
1519 { return this->closure_
; }
1521 // Return a tree for the code for a function.
1523 get_code_pointer(Gogo
*, Named_object
* function
, Location loc
);
1527 do_traverse(Traverse
*);
1533 do_determine_type(const Type_context
*)
1535 if (this->closure_
!= NULL
)
1536 this->closure_
->determine_type_no_context();
1542 return Expression::make_func_reference(this->function_
,
1543 (this->closure_
== NULL
1545 : this->closure_
->copy()),
1550 do_get_tree(Translate_context
*);
1553 do_dump_expression(Ast_dump_context
*) const;
1556 // The function itself.
1557 Named_object
* function_
;
1558 // A closure. This is normally NULL. For a nested function, it may
1559 // be a struct holding pointers to all the variables referenced by
1560 // this function and defined in enclosing functions.
1561 Expression
* closure_
;
1564 // A function descriptor. A function descriptor is a struct with a
1565 // single field pointing to the function code. This is used for
1566 // functions without closures.
1568 class Func_descriptor_expression
: public Expression
1571 Func_descriptor_expression(Named_object
* fn
);
1573 // Set the descriptor wrapper.
1575 set_descriptor_wrapper(Named_object
* dfn
)
1577 go_assert(this->dfn_
== NULL
);
1581 // Make the function descriptor type, so that it can be converted.
1583 make_func_descriptor_type();
1587 do_traverse(Traverse
*);
1593 do_determine_type(const Type_context
*)
1600 do_is_addressable() const
1604 do_get_tree(Translate_context
*);
1607 do_dump_expression(Ast_dump_context
* context
) const;
1610 // The type of all function descriptors.
1611 static Type
* descriptor_type
;
1613 // The function for which this is the descriptor.
1615 // The descriptor function.
1617 // The descriptor variable.
1621 // A reference to an unknown name.
1623 class Unknown_expression
: public Parser_expression
1626 Unknown_expression(Named_object
* named_object
, Location location
)
1627 : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE
, location
),
1628 named_object_(named_object
), no_error_message_(false),
1629 is_composite_literal_key_(false)
1632 // The associated named object.
1634 named_object() const
1635 { return this->named_object_
; }
1637 // The name of the identifier which was unknown.
1641 // Call this to indicate that we should not give an error if this
1642 // name is never defined. This is used to avoid knock-on errors
1643 // during an erroneous parse.
1645 set_no_error_message()
1646 { this->no_error_message_
= true; }
1648 // Note that this expression is being used as the key in a composite
1649 // literal, so it may be OK if it is not resolved.
1651 set_is_composite_literal_key()
1652 { this->is_composite_literal_key_
= true; }
1654 // Note that this expression should no longer be treated as a
1655 // composite literal key.
1657 clear_is_composite_literal_key()
1658 { this->is_composite_literal_key_
= false; }
1662 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1666 { return new Unknown_expression(this->named_object_
, this->location()); }
1669 do_dump_expression(Ast_dump_context
*) const;
1672 // The unknown name.
1673 Named_object
* named_object_
;
1674 // True if we should not give errors if this is undefined. This is
1675 // used if there was a parse failure.
1676 bool no_error_message_
;
1677 // True if this is the key in a composite literal.
1678 bool is_composite_literal_key_
;
1681 // An index expression. This is lowered to an array index, a string
1682 // index, or a map index.
1684 class Index_expression
: public Parser_expression
1687 Index_expression(Expression
* left
, Expression
* start
, Expression
* end
,
1689 : Parser_expression(EXPRESSION_INDEX
, location
),
1690 left_(left
), start_(start
), end_(end
), is_lvalue_(false)
1693 // Record that this expression is an lvalue.
1696 { this->is_lvalue_
= true; }
1698 // Dump an index expression, i.e. an expression of the form
1699 // expr[expr] or expr[expr:expr], to a dump context.
1701 dump_index_expression(Ast_dump_context
*, const Expression
* expr
,
1702 const Expression
* start
, const Expression
* end
);
1706 do_traverse(Traverse
*);
1709 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1714 return new Index_expression(this->left_
->copy(), this->start_
->copy(),
1717 : this->end_
->copy()),
1722 do_must_eval_subexpressions_in_order(int* skip
) const
1729 do_dump_expression(Ast_dump_context
*) const;
1732 // The expression being indexed.
1736 // The second index. This is NULL for an index, non-NULL for a
1739 // Whether this is being used as an l-value. We set this during the
1740 // parse because map index expressions need to know.
1744 // An index into a map.
1746 class Map_index_expression
: public Expression
1749 Map_index_expression(Expression
* map
, Expression
* index
,
1751 : Expression(EXPRESSION_MAP_INDEX
, location
),
1752 map_(map
), index_(index
), is_lvalue_(false),
1753 is_in_tuple_assignment_(false)
1759 { return this->map_
; }
1763 { return this->map_
; }
1765 // Return the index.
1768 { return this->index_
; }
1772 { return this->index_
; }
1774 // Get the type of the map being indexed.
1776 get_map_type() const;
1778 // Record that this map expression is an lvalue. The difference is
1779 // that an lvalue always inserts the key.
1782 { this->is_lvalue_
= true; }
1784 // Return whether this map expression occurs in an assignment to a
1787 is_in_tuple_assignment() const
1788 { return this->is_in_tuple_assignment_
; }
1790 // Record that this map expression occurs in an assignment to a pair
1793 set_is_in_tuple_assignment()
1794 { this->is_in_tuple_assignment_
= true; }
1796 // Return a tree for the map index. This returns a tree which
1797 // evaluates to a pointer to a value in the map. If INSERT is true,
1798 // the key will be inserted if not present, and the value pointer
1799 // will be zero initialized. If INSERT is false, and the key is not
1800 // present in the map, the pointer will be NULL.
1802 get_value_pointer(Translate_context
*, bool insert
);
1806 do_traverse(Traverse
*);
1812 do_determine_type(const Type_context
*);
1815 do_check_types(Gogo
*);
1820 return Expression::make_map_index(this->map_
->copy(),
1821 this->index_
->copy(),
1826 do_must_eval_subexpressions_in_order(int* skip
) const
1832 // A map index expression is an lvalue but it is not addressable.
1835 do_get_tree(Translate_context
*);
1838 do_dump_expression(Ast_dump_context
*) const;
1841 // The map we are looking into.
1845 // Whether this is an lvalue.
1847 // Whether this is in a tuple assignment to a pair of values.
1848 bool is_in_tuple_assignment_
;
1851 // An expression which represents a method bound to its first
1854 class Bound_method_expression
: public Expression
1857 Bound_method_expression(Expression
* expr
, const Method
*method
,
1858 Named_object
* function
, Location location
)
1859 : Expression(EXPRESSION_BOUND_METHOD
, location
),
1860 expr_(expr
), expr_type_(NULL
), method_(method
), function_(function
)
1863 // Return the object which is the first argument.
1866 { return this->expr_
; }
1868 // Return the implicit type of the first argument. This will be
1869 // non-NULL when using a method from an anonymous field without
1870 // using an explicit stub.
1872 first_argument_type() const
1873 { return this->expr_type_
; }
1875 // Return the method.
1878 { return this->method_
; }
1880 // Return the function to call.
1883 { return this->function_
; }
1885 // Set the implicit type of the expression.
1887 set_first_argument_type(Type
* type
)
1888 { this->expr_type_
= type
; }
1890 // Create a thunk to call FUNCTION, for METHOD, when it is used as
1891 // part of a method value.
1892 static Named_object
*
1893 create_thunk(Gogo
*, const Method
* method
, Named_object
* function
);
1897 do_traverse(Traverse
*);
1900 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1906 do_determine_type(const Type_context
*);
1909 do_check_types(Gogo
*);
1914 return new Bound_method_expression(this->expr_
->copy(), this->method_
,
1915 this->function_
, this->location());
1919 do_get_tree(Translate_context
*);
1922 do_dump_expression(Ast_dump_context
*) const;
1925 // A mapping from method functions to the thunks we have created for
1927 typedef Unordered_map(Named_object
*, Named_object
*) Method_value_thunks
;
1928 static Method_value_thunks method_value_thunks
;
1930 // The object used to find the method. This is passed to the method
1931 // as the first argument.
1933 // The implicit type of the object to pass to the method. This is
1934 // NULL in the normal case, non-NULL when using a method from an
1935 // anonymous field which does not require a stub.
1938 const Method
* method_
;
1939 // The function to call. This is not the same as
1940 // method_->named_object() when the method has a stub. This will be
1941 // the real function rather than the stub.
1942 Named_object
* function_
;
1945 // A reference to a field in a struct.
1947 class Field_reference_expression
: public Expression
1950 Field_reference_expression(Expression
* expr
, unsigned int field_index
,
1952 : Expression(EXPRESSION_FIELD_REFERENCE
, location
),
1953 expr_(expr
), field_index_(field_index
), implicit_(false), called_fieldtrack_(false)
1956 // Return the struct expression.
1959 { return this->expr_
; }
1961 // Return the field index.
1964 { return this->field_index_
; }
1966 // Return whether this node was implied by an anonymous field.
1969 { return this->implicit_
; }
1972 set_implicit(bool implicit
)
1973 { this->implicit_
= implicit
; }
1975 // Set the struct expression. This is used when parsing.
1977 set_struct_expression(Expression
* expr
)
1979 go_assert(this->expr_
== NULL
);
1985 do_traverse(Traverse
* traverse
)
1986 { return Expression::traverse(&this->expr_
, traverse
); }
1989 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
1995 do_determine_type(const Type_context
*)
1996 { this->expr_
->determine_type_no_context(); }
1999 do_check_types(Gogo
*);
2004 return Expression::make_field_reference(this->expr_
->copy(),
2010 do_is_addressable() const
2011 { return this->expr_
->is_addressable(); }
2014 do_address_taken(bool escapes
)
2015 { this->expr_
->address_taken(escapes
); }
2018 do_get_tree(Translate_context
*);
2021 do_dump_expression(Ast_dump_context
*) const;
2024 // The expression we are looking into. This should have a type of
2027 // The zero-based index of the field we are retrieving.
2028 unsigned int field_index_
;
2029 // Whether this node was emitted implicitly for an embedded field,
2030 // that is, expr_ is not the expr_ of the original user node.
2032 // Whether we have already emitted a fieldtrack call.
2033 bool called_fieldtrack_
;
2036 // A reference to a field of an interface.
2038 class Interface_field_reference_expression
: public Expression
2041 Interface_field_reference_expression(Expression
* expr
,
2042 const std::string
& name
,
2044 : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE
, location
),
2045 expr_(expr
), name_(name
)
2048 // Return the expression for the interface object.
2051 { return this->expr_
; }
2053 // Return the name of the method to call.
2056 { return this->name_
; }
2058 // Create a thunk to call the method NAME in TYPE when it is used as
2059 // part of a method value.
2060 static Named_object
*
2061 create_thunk(Gogo
*, Interface_type
* type
, const std::string
& name
);
2063 // Return a tree for the pointer to the function to call, given a
2064 // tree for the expression.
2066 get_function_tree(Translate_context
*, tree
);
2068 // Return a tree for the first argument to pass to the interface
2069 // function, given a tree for the expression. This is the real
2070 // object associated with the interface object.
2072 get_underlying_object_tree(Translate_context
*, tree
);
2076 do_traverse(Traverse
* traverse
);
2079 do_lower(Gogo
*, Named_object
*, Statement_inserter
*, int);
2085 do_determine_type(const Type_context
*);
2088 do_check_types(Gogo
*);
2093 return Expression::make_interface_field_reference(this->expr_
->copy(),
2099 do_get_tree(Translate_context
*);
2102 do_dump_expression(Ast_dump_context
*) const;
2105 // A mapping from interface types to a list of thunks we have
2106 // created for methods.
2107 typedef std::vector
<std::pair
<std::string
, Named_object
*> > Method_thunks
;
2108 typedef Unordered_map(Interface_type
*, Method_thunks
*)
2109 Interface_method_thunks
;
2110 static Interface_method_thunks interface_method_thunks
;
2112 // The expression for the interface object. This should have a type
2113 // of interface or pointer to interface.
2115 // The field we are retrieving--the name of the method.
2119 // A type guard expression.
2121 class Type_guard_expression
: public Expression
2124 Type_guard_expression(Expression
* expr
, Type
* type
, Location location
)
2125 : Expression(EXPRESSION_TYPE_GUARD
, location
),
2126 expr_(expr
), type_(type
)
2129 // Return the expression to convert.
2132 { return this->expr_
; }
2134 // Return the type to which to convert.
2137 { return this->type_
; }
2141 do_traverse(Traverse
* traverse
);
2145 { return this->type_
; }
2148 do_determine_type(const Type_context
*)
2149 { this->expr_
->determine_type_no_context(); }
2152 do_check_types(Gogo
*);
2157 return new Type_guard_expression(this->expr_
->copy(), this->type_
,
2162 do_get_tree(Translate_context
*);
2165 do_dump_expression(Ast_dump_context
*) const;
2168 // The expression to convert.
2170 // The type to which to convert.
2174 // A receive expression.
2176 class Receive_expression
: public Expression
2179 Receive_expression(Expression
* channel
, Location location
)
2180 : Expression(EXPRESSION_RECEIVE
, location
),
2184 // Return the channel.
2187 { return this->channel_
; }
2191 do_traverse(Traverse
* traverse
)
2192 { return Expression::traverse(&this->channel_
, traverse
); }
2195 do_discarding_value()
2202 do_determine_type(const Type_context
*)
2203 { this->channel_
->determine_type_no_context(); }
2206 do_check_types(Gogo
*);
2211 return Expression::make_receive(this->channel_
->copy(), this->location());
2215 do_must_eval_in_order() const
2219 do_get_tree(Translate_context
*);
2222 do_dump_expression(Ast_dump_context
*) const;
2225 // The channel from which we are receiving.
2226 Expression
* channel_
;
2229 // A numeric constant. This is used both for untyped constants and
2230 // for constants that have a type.
2232 class Numeric_constant
2236 : classification_(NC_INVALID
), type_(NULL
)
2239 ~Numeric_constant();
2241 Numeric_constant(const Numeric_constant
&);
2243 Numeric_constant
& operator=(const Numeric_constant
&);
2245 // Set to an unsigned long value.
2247 set_unsigned_long(Type
*, unsigned long);
2249 // Set to an integer value.
2251 set_int(Type
*, const mpz_t
);
2253 // Set to a rune value.
2255 set_rune(Type
*, const mpz_t
);
2257 // Set to a floating point value.
2259 set_float(Type
*, const mpfr_t
);
2261 // Set to a complex value.
2263 set_complex(Type
*, const mpfr_t
, const mpfr_t
);
2268 { return this->classification_
== Numeric_constant::NC_INT
; }
2272 { return this->classification_
== Numeric_constant::NC_RUNE
; }
2276 { return this->classification_
== Numeric_constant::NC_FLOAT
; }
2280 { return this->classification_
== Numeric_constant::NC_COMPLEX
; }
2282 // Value retrievers. These will initialize the values as well as
2283 // set them. GET_INT is only valid if IS_INT returns true, and
2284 // likewise respectively.
2286 get_int(mpz_t
*) const;
2289 get_rune(mpz_t
*) const;
2292 get_float(mpfr_t
*) const;
2295 get_complex(mpfr_t
*, mpfr_t
*) const;
2297 // Codes returned by to_unsigned_long.
2298 enum To_unsigned_long
2300 // Value is integer and fits in unsigned long.
2302 // Value is not integer.
2304 // Value is integer but is negative.
2306 // Value is non-negative integer but does not fit in unsigned
2311 // If the value can be expressed as an integer that fits in an
2312 // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return
2313 // one of the other To_unsigned_long codes.
2315 to_unsigned_long(unsigned long* val
) const;
2317 // If the value can be expressed as an int, return true and
2318 // initialize and set VAL. This will return false for a value with
2319 // an explicit float or complex type, even if the value is integral.
2321 to_int(mpz_t
* val
) const;
2323 // If the value can be expressed as a float, return true and
2324 // initialize and set VAL.
2326 to_float(mpfr_t
* val
) const;
2328 // If the value can be expressed as a complex, return true and
2329 // initialize and set VR and VI.
2331 to_complex(mpfr_t
* vr
, mpfr_t
* vi
) const;
2337 // If the constant can be expressed in TYPE, then set the type of
2338 // the constant to TYPE and return true. Otherwise return false,
2339 // and, if ISSUE_ERROR is true, issue an error message. LOCATION is
2340 // the location to use for the error.
2342 set_type(Type
* type
, bool issue_error
, Location location
);
2344 // Return an Expression for this value.
2346 expression(Location
) const;
2353 mpz_to_unsigned_long(const mpz_t ival
, unsigned long *val
) const;
2356 mpfr_to_unsigned_long(const mpfr_t fval
, unsigned long *val
) const;
2359 check_int_type(Integer_type
*, bool, Location
) const;
2362 check_float_type(Float_type
*, bool, Location
);
2365 check_complex_type(Complex_type
*, bool, Location
);
2367 // The kinds of constants.
2377 // The kind of constant.
2378 Classification classification_
;
2382 // If NC_INT or NC_RUNE.
2393 // The type if there is one. This will be NULL for an untyped
2398 #endif // !defined(GO_EXPRESSIONS_H)