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