github.com/ronhuafeng/gofrontend@v0.0.0-20220715151246-ff23266b8bc5/go/gogo.h (about) 1 // gogo.h -- Go frontend parsed representation. -*- 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_GOGO_H 8 #define GO_GOGO_H 9 10 #include "go-linemap.h" 11 12 class Traverse; 13 class Statement_inserter; 14 class Type; 15 class Type_equal; 16 class Typed_identifier; 17 class Typed_identifier_list; 18 class Function_type; 19 class Expression; 20 class Expression_list; 21 class Statement; 22 class Temporary_statement; 23 class Block; 24 class Function; 25 class Bindings; 26 class Bindings_snapshot; 27 class Package; 28 class Variable; 29 class Pointer_type; 30 class Struct_type; 31 class Struct_field; 32 class Struct_field_list; 33 class Array_type; 34 class Map_type; 35 class Channel_type; 36 class Interface_type; 37 class Named_type; 38 class Forward_declaration_type; 39 class Named_object; 40 class Label; 41 class Translate_context; 42 class Backend; 43 class Export; 44 class Export_function_body; 45 class Import; 46 class Import_function_body; 47 class Bexpression; 48 class Btype; 49 class Bstatement; 50 class Bblock; 51 class Bvariable; 52 class Blabel; 53 class Bfunction; 54 class Escape_context; 55 class Node; 56 57 // This file declares the basic classes used to hold the internal 58 // representation of Go which is built by the parser. 59 60 // The name of some backend object. Backend objects have a 61 // user-visible name and an assembler name. The user visible name 62 // might include arbitrary Unicode characters. The assembler name 63 // will not. 64 65 class Backend_name 66 { 67 public: 68 Backend_name() 69 : prefix_(NULL), components_(), count_(0), suffix_(), 70 is_asm_name_(false), is_non_identifier_(false) 71 {} 72 73 // Set the prefix. Prefixes are always constant strings. 74 void 75 set_prefix(const char* p) 76 { 77 go_assert(this->prefix_ == NULL && !this->is_asm_name_); 78 this->prefix_ = p; 79 } 80 81 // Set the suffix. 82 void 83 set_suffix(const std::string& s) 84 { 85 go_assert(this->suffix_.empty() && !this->is_asm_name_); 86 this->suffix_ = s; 87 } 88 89 // Append to the suffix. 90 void 91 append_suffix(const std::string& s) 92 { 93 if (this->is_asm_name_) 94 this->components_[0].append(s); 95 else 96 this->suffix_.append(s); 97 } 98 99 // Add a component. 100 void 101 add(const std::string& c) 102 { 103 go_assert(this->count_ < Backend_name::max_components 104 && !this->is_asm_name_); 105 this->components_[this->count_] = c; 106 ++this->count_; 107 } 108 109 // Set an assembler name specified by the user. This overrides both 110 // the user-visible name and the assembler name. No further 111 // encoding is applied. 112 void 113 set_asm_name(const std::string& n) 114 { 115 go_assert(this->prefix_ == NULL 116 && this->count_ == 0 117 && this->suffix_.empty() 118 && !this->is_asm_name_); 119 this->components_[0] = n; 120 this->is_asm_name_ = true; 121 } 122 123 // Whether some component includes some characters that can't appear 124 // in an identifier. 125 bool 126 is_non_identifier() const 127 { return this->is_non_identifier_; } 128 129 // Record that some component includes some character that can't 130 // appear in an identifier. 131 void 132 set_is_non_identifier() 133 { this->is_non_identifier_ = true; } 134 135 // Get the user visible name. 136 std::string 137 name() const; 138 139 // Get the assembler name. This may be the same as the user visible 140 // name. 141 std::string 142 asm_name() const; 143 144 // Get an optional assembler name: if it would be the same as the 145 // user visible name, this returns the empty string. 146 std::string 147 optional_asm_name() const; 148 149 private: 150 // The maximum number of components. 151 static const int max_components = 4; 152 153 // An optional prefix that does not require encoding. 154 const char *prefix_; 155 // Up to four components. The name will include these components 156 // separated by dots. Each component will be underscore-encoded 157 // (see the long comment near the top of names.cc). 158 std::string components_[Backend_name::max_components]; 159 // Number of components. 160 int count_; 161 // An optional suffix that does not require encoding. 162 std::string suffix_; 163 // True if components_[0] is an assembler name specified by the user. 164 bool is_asm_name_; 165 // True if some component includes some character that can't 166 // normally appear in an identifier. 167 bool is_non_identifier_; 168 }; 169 170 // An initialization function for an imported package. This is a 171 // magic function which initializes variables and runs the "init" 172 // function. 173 174 class Import_init 175 { 176 public: 177 Import_init(const std::string& package_name, const std::string& init_name, 178 int priority) 179 : package_name_(package_name), init_name_(init_name), priority_(priority) 180 { } 181 182 // The name of the package being imported. 183 const std::string& 184 package_name() const 185 { return this->package_name_; } 186 187 // The name of the package's init function. 188 const std::string& 189 init_name() const 190 { return this->init_name_; } 191 192 // Older V1 export data uses a priority scheme to order 193 // initialization functions; functions with a lower priority number 194 // must be run first. This value will be set to -1 for current 195 // generation objects, and will take on a non-negative value only 196 // when importing a V1-vintage object. 197 int 198 priority() const 199 { return this->priority_; } 200 201 // Reset priority. 202 void 203 set_priority(int new_priority) 204 { this->priority_ = new_priority; } 205 206 // Record the fact that some other init fcn must be run before this init fcn. 207 void 208 record_precursor_fcn(std::string init_fcn_name) 209 { this->precursor_functions_.insert(init_fcn_name); } 210 211 // Return the list of precursor fcns for this fcn (must be run before it). 212 const std::set<std::string>& 213 precursors() const 214 { return this->precursor_functions_; } 215 216 // Whether this is a dummy init, which is used only to record transitive import. 217 bool 218 is_dummy() const 219 { return this->init_name_[0] == '~'; } 220 221 private: 222 // The name of the package being imported. 223 std::string package_name_; 224 // The name of the package's init function. 225 std::string init_name_; 226 // Names of init functions that must be run before this fcn. 227 std::set<std::string> precursor_functions_; 228 // Priority for this function. See note above on obsolescence. 229 int priority_; 230 }; 231 232 // For sorting purposes. 233 234 struct Import_init_lt { 235 bool operator()(const Import_init* i1, const Import_init* i2) const 236 { 237 return i1->init_name() < i2->init_name(); 238 } 239 }; 240 241 // Set of import init objects. 242 class Import_init_set : public std::set<Import_init*, Import_init_lt> { 243 }; 244 245 inline bool 246 priority_compare(const Import_init* i1, const Import_init* i2) 247 { 248 if (i1->priority() < i2->priority()) 249 return true; 250 if (i1->priority() > i2->priority()) 251 return false; 252 if (i1->package_name() != i2->package_name()) 253 return i1->package_name() < i2->package_name(); 254 return i1->init_name() < i2->init_name(); 255 } 256 257 // The holder for the internal representation of the entire 258 // compilation unit. 259 260 class Gogo 261 { 262 public: 263 // Create the IR, passing in the sizes of the types "int" and 264 // "uintptr" in bits. 265 Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size); 266 267 // Get the backend generator. 268 Backend* 269 backend() 270 { return this->backend_; } 271 272 // Get the Location generator. 273 Linemap* 274 linemap() 275 { return this->linemap_; } 276 277 // Get the package name. 278 const std::string& 279 package_name() const; 280 281 // Set the package name. 282 void 283 set_package_name(const std::string&, Location); 284 285 // Return whether this is the "main" package. 286 bool 287 is_main_package() const; 288 289 // If necessary, adjust the name to use for a hidden symbol. We add 290 // the package name, so that hidden symbols in different packages do 291 // not collide. 292 std::string 293 pack_hidden_name(const std::string& name, bool is_exported) const 294 { 295 return (is_exported 296 ? name 297 : '.' + this->pkgpath() + '.' + name); 298 } 299 300 // Unpack a name which may have been hidden. Returns the 301 // user-visible name of the object. 302 static std::string 303 unpack_hidden_name(const std::string& name) 304 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); } 305 306 // Return whether a possibly packed name is hidden. 307 static bool 308 is_hidden_name(const std::string& name) 309 { return name[0] == '.'; } 310 311 // Return the package path of a hidden name. 312 static std::string 313 hidden_name_pkgpath(const std::string& name) 314 { 315 go_assert(Gogo::is_hidden_name(name)); 316 return name.substr(1, name.rfind('.') - 1); 317 } 318 319 // Given a name which may or may not have been hidden, append the 320 // appropriate version of the name to the result string. 321 static void 322 append_possibly_hidden_name(std::string *result, const std::string& name); 323 324 // Given a name which may or may not have been hidden, return the 325 // name to use in an error message. 326 static std::string 327 message_name(const std::string& name); 328 329 // Return whether a name is the blank identifier _. 330 static bool 331 is_sink_name(const std::string& name) 332 { 333 return (name[0] == '.' 334 && name[name.length() - 1] == '_' 335 && name[name.length() - 2] == '.') 336 || (name[0] == '_' 337 && name.length() == 1); 338 } 339 340 // Helper used when adding parameters (including receiver param) to the 341 // bindings of a function. If the specified parameter name is empty or 342 // corresponds to the sink name, param name is replaced with a new unique 343 // name. PNAME is the address of a string containing the parameter variable 344 // name to be checked/updated; TAG is a descriptive tag to be used in 345 // manufacturing the new unique name, and COUNT is the address of a counter 346 // holding the number of params renamed so far with the tag in question. 347 static void 348 rename_if_empty(std::string* pname, const char* tag, unsigned* count); 349 350 // Convert a pkgpath into a string suitable for a symbol 351 static std::string 352 pkgpath_for_symbol(const std::string& pkgpath); 353 354 // Compute a hash code for a string, given a seed. 355 static unsigned int 356 hash_string(const std::string&, unsigned int); 357 358 // Return the package path to use for reflect.Type.PkgPath. 359 const std::string& 360 pkgpath() const; 361 362 // Return the package path to use for a symbol name. 363 const std::string& 364 pkgpath_symbol() const; 365 366 // Set the package path from a command line option. 367 void 368 set_pkgpath(const std::string&); 369 370 // Set the prefix from a command line option. 371 void 372 set_prefix(const std::string&); 373 374 // Return whether pkgpath was set from a command line option. 375 bool 376 pkgpath_from_option() const 377 { return this->pkgpath_from_option_; } 378 379 // Return the relative import path as set from the command line. 380 // Returns an empty string if it was not set. 381 const std::string& 382 relative_import_path() const 383 { return this->relative_import_path_; } 384 385 // Set the relative import path from a command line option. 386 void 387 set_relative_import_path(const std::string& s) 388 { this->relative_import_path_ = s; } 389 390 // Set the C header file to write. This is used for the runtime 391 // package. 392 void 393 set_c_header(const std::string& s) 394 { this->c_header_ = s; } 395 396 // Read an embedcfg file. 397 void 398 read_embedcfg(const char* filename); 399 400 // Build an initializer for a variable with a go:embed directive. 401 Expression* 402 initializer_for_embeds(Type*, const std::vector<std::string>*, Location); 403 404 // Return whether to check for division by zero in binary operations. 405 bool 406 check_divide_by_zero() const 407 { return this->check_divide_by_zero_; } 408 409 // Set the option to check division by zero from a command line option. 410 void 411 set_check_divide_by_zero(bool b) 412 { this->check_divide_by_zero_ = b; } 413 414 // Return whether to check for division overflow in binary operations. 415 bool 416 check_divide_overflow() const 417 { return this->check_divide_overflow_; } 418 419 // Set the option to check division overflow from a command line option. 420 void 421 set_check_divide_overflow(bool b) 422 { this->check_divide_overflow_ = b; } 423 424 // Return whether we are compiling the runtime package. 425 bool 426 compiling_runtime() const 427 { return this->compiling_runtime_; } 428 429 // Set whether we are compiling the runtime package. 430 void 431 set_compiling_runtime(bool b) 432 { this->compiling_runtime_ = b; } 433 434 // Return the level of escape analysis debug information to emit. 435 int 436 debug_escape_level() const 437 { return this->debug_escape_level_; } 438 439 // Set the level of escape analysis debugging from a command line option. 440 void 441 set_debug_escape_level(int level) 442 { this->debug_escape_level_ = level; } 443 444 // Return the hash for debug escape analysis. 445 std::string 446 debug_escape_hash() const 447 { return this->debug_escape_hash_; } 448 449 // Set the hash value for debug escape analysis. 450 void 451 set_debug_escape_hash(const std::string& s) 452 { this->debug_escape_hash_ = s; } 453 454 // Return whether to output optimization diagnostics. 455 bool 456 debug_optimization() const 457 { return this->debug_optimization_; } 458 459 // Set the option to output optimization diagnostics. 460 void 461 set_debug_optimization(bool b) 462 { this->debug_optimization_ = b; } 463 464 // Dump to stderr for debugging 465 void debug_dump(); 466 467 // Return the size threshold used to determine whether to issue 468 // a nil-check for a given pointer dereference. A threshold of -1 469 // implies that all potentially faulting dereference ops should 470 // be nil-checked. A positive threshold of N implies that a deref 471 // of *P where P has size less than N doesn't need a nil check. 472 int64_t 473 nil_check_size_threshold() const 474 { return this->nil_check_size_threshold_; } 475 476 // Set the nil-check size threshold, as described above. 477 void 478 set_nil_check_size_threshold(int64_t bytes) 479 { this->nil_check_size_threshold_ = bytes; } 480 481 // Return whether runtime.eqtype calls are needed when comparing 482 // type descriptors. 483 bool 484 need_eqtype() const 485 { return this->need_eqtype_; } 486 487 // Set if calls to runtime.eqtype are needed. 488 void 489 set_need_eqtype(bool b) 490 { this->need_eqtype_ = b; } 491 492 // Import a package. FILENAME is the file name argument, LOCAL_NAME 493 // is the local name to give to the package. If LOCAL_NAME is empty 494 // the declarations are added to the global scope. 495 void 496 import_package(const std::string& filename, const std::string& local_name, 497 bool is_local_name_exported, bool must_exist, Location); 498 499 // Whether we are the global binding level. 500 bool 501 in_global_scope() const; 502 503 // Look up a name in the current binding contours. 504 Named_object* 505 lookup(const std::string&, Named_object** pfunction) const; 506 507 // Look up a name in the current block. 508 Named_object* 509 lookup_in_block(const std::string&) const; 510 511 // Look up a name in the global namespace--the universal scope. 512 Named_object* 513 lookup_global(const char*) const; 514 515 // Add a new imported package. REAL_NAME is the real name of the 516 // package. ALIAS is the alias of the package; this may be the same 517 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to 518 // this package should be added to the global namespace; this is 519 // true if the alias is ".". LOCATION is the location of the import 520 // statement. This returns the new package, or NULL on error. 521 Package* 522 add_imported_package(const std::string& real_name, const std::string& alias, 523 bool is_alias_exported, 524 const std::string& pkgpath, 525 const std::string& pkgpath_symbol, 526 Location location, 527 bool* padd_to_globals); 528 529 // Register a package. This package may or may not be imported. 530 // This returns the Package structure for the package, creating if 531 // it necessary. 532 Package* 533 register_package(const std::string& pkgpath, 534 const std::string& pkgpath_symbol, Location); 535 536 // Add the unsafe bindings to the unsafe package. 537 void 538 add_unsafe_bindings(Package*); 539 540 // Look up a package by pkgpath, and return its pkgpath_symbol. 541 std::string 542 pkgpath_symbol_for_package(const std::string&); 543 544 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a 545 // method function should be added to the type of its receiver. 546 Named_object* 547 start_function(const std::string& name, Function_type* type, 548 bool add_method_to_type, Location); 549 550 // Finish compiling a function. 551 void 552 finish_function(Location); 553 554 // Return the current function. 555 Named_object* 556 current_function() const; 557 558 // Return the current block. 559 Block* 560 current_block(); 561 562 // Start a new block. This is not initially associated with a 563 // function. 564 void 565 start_block(Location); 566 567 // Finish the current block and return it. 568 Block* 569 finish_block(Location); 570 571 // Declare an erroneous name. This is used to avoid knock-on errors 572 // after a parsing error. 573 Named_object* 574 add_erroneous_name(const std::string& name); 575 576 // Declare an unknown name. This is used while parsing. The name 577 // must be resolved by the end of the parse. Unknown names are 578 // always added at the package level. 579 Named_object* 580 add_unknown_name(const std::string& name, Location); 581 582 // Declare a function. 583 Named_object* 584 declare_function(const std::string&, Function_type*, Location); 585 586 // Declare a function at the package level. This is used for 587 // functions generated for a type. 588 Named_object* 589 declare_package_function(const std::string&, Function_type*, Location); 590 591 // Add a function declaration to the list of functions we may want 592 // to inline. 593 void 594 add_imported_inlinable_function(Named_object*); 595 596 // Add a function to the list of functions that we do want to 597 // inline. 598 void 599 add_imported_inline_function(Named_object* no) 600 { this->imported_inline_functions_.push_back(no); } 601 602 // Add a label. 603 Label* 604 add_label_definition(const std::string&, Location); 605 606 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should 607 // report errors for a goto from the current location to the label 608 // location. 609 Label* 610 add_label_reference(const std::string&, Location, 611 bool issue_goto_errors); 612 613 // An analysis set is a list of functions paired with a boolean that indicates 614 // whether the list of functions are recursive. 615 typedef std::pair<std::vector<Named_object*>, bool> Analysis_set; 616 617 // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this 618 // package. 619 void 620 add_analysis_set(const std::vector<Named_object*>& group, bool recursive) 621 { this->analysis_sets_.push_back(std::make_pair(group, recursive)); } 622 623 // Return a snapshot of the current binding state. 624 Bindings_snapshot* 625 bindings_snapshot(Location); 626 627 // Add a statement to the current block. 628 void 629 add_statement(Statement*); 630 631 // Add a block to the current block. 632 void 633 add_block(Block*, Location); 634 635 // Add a constant. 636 Named_object* 637 add_constant(const Typed_identifier&, Expression*, int iota_value); 638 639 // Add a type. 640 void 641 add_type(const std::string&, Type*, Location); 642 643 // Add a named type. This is used for builtin types, and to add an 644 // imported type to the global scope. 645 void 646 add_named_type(Named_type*); 647 648 // Declare a type. 649 Named_object* 650 declare_type(const std::string&, Location); 651 652 // Declare a type at the package level. This is used when the 653 // parser sees an unknown name where a type name is required. 654 Named_object* 655 declare_package_type(const std::string&, Location); 656 657 // Define a type which was already declared. 658 void 659 define_type(Named_object*, Named_type*); 660 661 // Add a variable. 662 Named_object* 663 add_variable(const std::string&, Variable*); 664 665 // Add a sink--a reference to the blank identifier _. 666 Named_object* 667 add_sink(); 668 669 // Add a type which needs to be verified. This is used for sink 670 // types, just to give appropriate error messages. 671 void 672 add_type_to_verify(Type* type); 673 674 // Add a named object to the current namespace. This is used for 675 // import . "package". 676 void 677 add_dot_import_object(Named_object*); 678 679 // Add an identifier to the list of names seen in the file block. 680 void 681 add_file_block_name(const std::string& name, Location location) 682 { this->file_block_names_[name] = location; } 683 684 // Add a linkname, from the go:linkname compiler directive. This 685 // changes the externally visible name of GO_NAME to be EXT_NAME. 686 // If EXT_NAME is the empty string, GO_NAME is unchanged, but the 687 // symbol is made publicly visible. 688 void 689 add_linkname(const std::string& go_name, bool is_exported, 690 const std::string& ext_name, Location location); 691 692 // Mark all local variables in current bindings as used. This is 693 // used when there is a parse error to avoid useless errors. 694 void 695 mark_locals_used(); 696 697 // Note that we've seen an interface type. This is used to build 698 // all required interface method tables. 699 void 700 record_interface_type(Interface_type*); 701 702 // Note that we need an initialization function. 703 void 704 set_need_init_fn() 705 { this->need_init_fn_ = true; } 706 707 // Return whether the current file imported the unsafe package. 708 bool 709 current_file_imported_unsafe() const 710 { return this->current_file_imported_unsafe_; } 711 712 // Return whether the current file imported the embed package. 713 bool 714 current_file_imported_embed() const 715 { return this->current_file_imported_embed_; } 716 717 // Clear out all names in file scope. This is called when we start 718 // parsing a new file. 719 void 720 clear_file_scope(); 721 722 // Record that VAR1 must be initialized after VAR2. This is used 723 // when VAR2 does not appear in VAR1's INIT or PREINIT. 724 void 725 record_var_depends_on(Variable* var1, Named_object* var2) 726 { 727 go_assert(this->var_deps_.find(var1) == this->var_deps_.end()); 728 this->var_deps_[var1] = var2; 729 } 730 731 // Return the variable that VAR depends on, or NULL if none. 732 Named_object* 733 var_depends_on(Variable* var) const 734 { 735 Var_deps::const_iterator p = this->var_deps_.find(var); 736 return p != this->var_deps_.end() ? p->second : NULL; 737 } 738 739 // Queue up a type-specific hash function to be written out. This 740 // is used when a type-specific hash function is needed when not at 741 // top level. 742 void 743 queue_hash_function(Type* type, int64_t size, Backend_name*, 744 Function_type* hash_fntype); 745 746 // Queue up a type-specific equal function to be written out. This 747 // is used when a type-specific equal function is needed when not at 748 // top level. 749 void 750 queue_equal_function(Type* type, Named_type* name, int64_t size, 751 Backend_name*, Function_type* equal_fntype); 752 753 // Write out queued specific type functions. 754 void 755 write_specific_type_functions(); 756 757 // Whether we are done writing out specific type functions. 758 bool 759 specific_type_functions_are_written() const 760 { return this->specific_type_functions_are_written_; } 761 762 // Add a pointer that needs to be added to the list of objects 763 // traversed by the garbage collector. This should be an expression 764 // of pointer type that points to static storage. It's not 765 // necessary to add global variables to this list, just global 766 // variable initializers that would otherwise not be seen. 767 void 768 add_gc_root(Expression* expr) 769 { 770 this->set_need_init_fn(); 771 this->gc_roots_.push_back(expr); 772 } 773 774 // Add a type to the descriptor list. 775 void 776 add_type_descriptor(Type* type) 777 { this->type_descriptors_.push_back(type); } 778 779 // Traverse the tree. See the Traverse class. 780 void 781 traverse(Traverse*); 782 783 // Define the predeclared global names. 784 void 785 define_global_names(); 786 787 // Verify and complete all types. 788 void 789 verify_types(); 790 791 // Lower the parse tree. 792 void 793 lower_parse_tree(); 794 795 // Lower all the statements in a block. 796 void 797 lower_block(Named_object* function, Block*); 798 799 // Lower an expression. 800 void 801 lower_expression(Named_object* function, Statement_inserter*, Expression**); 802 803 // Lower a constant. 804 void 805 lower_constant(Named_object*); 806 807 // Flatten all the statements in a block. 808 void 809 flatten_block(Named_object* function, Block*); 810 811 // Flatten an expression. 812 void 813 flatten_expression(Named_object* function, Statement_inserter*, Expression**); 814 815 // Create all necessary function descriptors. 816 void 817 create_function_descriptors(); 818 819 // Finalize the method lists and build stub methods for named types. 820 void 821 finalize_methods(); 822 823 // Finalize the method list for one type. 824 void 825 finalize_methods_for_type(Type*); 826 827 // Work out the types to use for unspecified variables and 828 // constants. 829 void 830 determine_types(); 831 832 // Type check the program. 833 void 834 check_types(); 835 836 // Check the types in a single block. This is used for complicated 837 // go statements. 838 void 839 check_types_in_block(Block*); 840 841 // Check for return statements. 842 void 843 check_return_statements(); 844 845 // Gather references from global variables initializers to other 846 // variables. 847 void 848 record_global_init_refs(); 849 850 // Remove deadcode. 851 void 852 remove_deadcode(); 853 854 // Make implicit type conversions explicit. 855 void 856 add_conversions(); 857 858 // Make implicit type conversions explicit in a block. 859 void 860 add_conversions_in_block(Block*); 861 862 // Analyze the program flow for escape information. 863 void 864 analyze_escape(); 865 866 // Discover the groups of possibly recursive functions in this package. 867 void 868 discover_analysis_sets(); 869 870 // Build a connectivity graph between the objects in each analyzed function. 871 void 872 assign_connectivity(Escape_context*, Named_object*); 873 874 // Traverse the objects in the connecitivty graph from the sink, adjusting the 875 // escape levels of each object. 876 void 877 propagate_escape(Escape_context*, Node*); 878 879 // Add notes about the escape level of a function's input and output 880 // parameters for exporting and importing top level functions. 881 void 882 tag_function(Escape_context*, Named_object*); 883 884 // Reclaim memory of escape analysis Nodes. 885 void 886 reclaim_escape_nodes(); 887 888 // Do all exports. 889 void 890 do_exports(); 891 892 // Add an import control function for an imported package to the 893 // list. 894 void 895 add_import_init_fn(const std::string& package_name, 896 const std::string& init_name, int prio); 897 898 // Return the Import_init for a given init name. 899 Import_init* 900 lookup_init(const std::string& init_name); 901 902 // Turn short-cut operators (&&, ||) into explicit if statements. 903 void 904 remove_shortcuts(); 905 906 // Turn short-cut operators into explicit if statements in a block. 907 void 908 remove_shortcuts_in_block(Block*); 909 910 // Use temporary variables to force order of evaluation. 911 void 912 order_evaluations(); 913 914 // Order evaluations in a block. 915 void 916 order_block(Block*); 917 918 // Add write barriers as needed. 919 void 920 add_write_barriers(); 921 922 // Return whether an assignment that sets LHS to RHS needs a write 923 // barrier. 924 bool 925 assign_needs_write_barrier(Expression* lhs, 926 Unordered_set(const Named_object*)*); 927 928 // Return whether EXPR is the address of a variable that can be set 929 // without a write barrier. That is, if this returns true, then an 930 // assignment to *EXPR does not require a write barrier. 931 bool 932 is_nonwb_pointer(Expression* expr, Unordered_set(const Named_object*)*); 933 934 // Return an assignment that sets LHS to RHS using a write barrier. 935 // This returns an if statement that checks whether write barriers 936 // are enabled. If not, it does LHS = RHS, otherwise it calls the 937 // appropriate write barrier function. 938 Statement* 939 assign_with_write_barrier(Function*, Block*, Statement_inserter*, 940 Expression* lhs, Expression* rhs, Location); 941 942 // Return a statement that tests whether write barriers are enabled 943 // and executes either the efficient code (WITHOUT) or the write 944 // barrier function call (WITH), depending. 945 Statement* 946 check_write_barrier(Block*, Statement* without, Statement* with); 947 948 // Flatten parse tree. 949 void 950 flatten(); 951 952 // Build thunks for functions which call recover. 953 void 954 build_recover_thunks(); 955 956 // Simplify statements which might use thunks: go and defer 957 // statements. 958 void 959 simplify_thunk_statements(); 960 961 // Dump AST if -fgo-dump-ast is set. 962 void 963 dump_ast(const char* basename); 964 965 // Dump Call Graph if -fgo-dump-calls is set. 966 void 967 dump_call_graph(const char* basename); 968 969 // Dump Connection Graphs if -fgo-dump-connections is set. 970 void 971 dump_connection_graphs(const char* basename); 972 973 // Convert named types to the backend representation. 974 void 975 convert_named_types(); 976 977 // Convert named types in a list of bindings. 978 void 979 convert_named_types_in_bindings(Bindings*); 980 981 // True if named types have been converted to the backend 982 // representation. 983 bool 984 named_types_are_converted() const 985 { return this->named_types_are_converted_; } 986 987 // Give an error if the initialization of VAR depends on itself. 988 void 989 check_self_dep(Named_object*); 990 991 // Write out the global values. 992 void 993 write_globals(); 994 995 // Build required interface method tables. 996 void 997 build_interface_method_tables(); 998 999 // Return an expression which allocates memory to hold values of type TYPE. 1000 Expression* 1001 allocate_memory(Type *type, Location); 1002 1003 // Get the backend name to use for an exported function, a method, 1004 // or a function/method declaration. 1005 void 1006 function_backend_name(const std::string& go_name, const Package*, 1007 const Type* receiver, Backend_name*); 1008 1009 // Return the name to use for a function descriptor. 1010 void 1011 function_descriptor_backend_name(Named_object*, Backend_name*); 1012 1013 // Return the name to use for a generated stub method. 1014 std::string 1015 stub_method_name(const Package*, const std::string& method_name); 1016 1017 // Get the backend name of the hash function for TYPE. 1018 void 1019 hash_function_name(const Type*, Backend_name*); 1020 1021 // Get the backend name of the equal function for TYPE. 1022 void 1023 equal_function_name(const Type*, const Named_type*, Backend_name*); 1024 1025 // Get the backend name to use for a global variable. 1026 void 1027 global_var_backend_name(const std::string& go_name, const Package*, 1028 Backend_name*); 1029 1030 // Return a name to use for an error case. This should only be used 1031 // after reporting an error, and is used to avoid useless knockon 1032 // errors. 1033 static std::string 1034 erroneous_name(); 1035 1036 // Return whether the name indicates an error. 1037 static bool 1038 is_erroneous_name(const std::string&); 1039 1040 // Return a name to use for a thunk function. A thunk function is 1041 // one we create during the compilation, for a go statement or a 1042 // defer statement or a method expression. 1043 std::string 1044 thunk_name(); 1045 1046 // Return whether an object is a thunk. 1047 static bool 1048 is_thunk(const Named_object*); 1049 1050 // Return the name to use for an init function. 1051 std::string 1052 init_function_name(); 1053 1054 // Return the name to use for a nested function. 1055 std::string 1056 nested_function_name(Named_object* enclosing); 1057 1058 // Return the name to use for a sink funciton. 1059 std::string 1060 sink_function_name(); 1061 1062 // Return the name to use for an (erroneous) redefined function. 1063 std::string 1064 redefined_function_name(); 1065 1066 // Return the name for use for a recover thunk. 1067 std::string 1068 recover_thunk_name(const std::string& name, const Type* rtype); 1069 1070 // Return the name to use for the GC root variable. 1071 std::string 1072 gc_root_name(); 1073 1074 // Return the name to use for a composite literal or string 1075 // initializer. 1076 std::string 1077 initializer_name(); 1078 1079 // Return the name of the variable used to represent the zero value 1080 // of a map. 1081 std::string 1082 map_zero_value_name(); 1083 1084 // Get the name of the magic initialization function. 1085 const std::string& 1086 get_init_fn_name(); 1087 1088 // Return the name for a dummy init function, which is not a real 1089 // function but only for tracking transitive import. 1090 std::string 1091 dummy_init_fn_name(); 1092 1093 // Return the package path symbol from an init function name, which 1094 // can be a real init function or a dummy one. 1095 std::string 1096 pkgpath_symbol_from_init_fn_name(std::string); 1097 1098 // Get the backend name for a type descriptor symbol. 1099 void 1100 type_descriptor_backend_name(const Type*, Named_type*, Backend_name*); 1101 1102 // Return the name of the type descriptor list symbol of a package. 1103 // The argument is an encoded pkgpath, as with pkgpath_symbol. 1104 std::string 1105 type_descriptor_list_symbol(const std::string&); 1106 1107 // Return the name of the list of all type descriptor lists. 1108 std::string 1109 typelists_symbol(); 1110 1111 // Return the assembler name for the GC symbol for a type. 1112 std::string 1113 gc_symbol_name(Type*); 1114 1115 // Return the assembler name for a ptrmask variable. 1116 std::string 1117 ptrmask_symbol_name(const std::string& ptrmask_sym_name); 1118 1119 // Return the name to use for an interface method table. 1120 std::string 1121 interface_method_table_name(Interface_type*, Type*, bool is_pointer); 1122 1123 // If NAME is a special name used as a Go identifier, return the 1124 // position within the string where the special part of the name 1125 // occurs. 1126 static size_t 1127 special_name_pos(const std::string& name); 1128 1129 private: 1130 // During parsing, we keep a stack of functions. Each function on 1131 // the stack is one that we are currently parsing. For each 1132 // function, we keep track of the current stack of blocks. 1133 struct Open_function 1134 { 1135 // The function. 1136 Named_object* function; 1137 // The stack of active blocks in the function. 1138 std::vector<Block*> blocks; 1139 }; 1140 1141 // The stack of functions. 1142 typedef std::vector<Open_function> Open_functions; 1143 1144 // Set up the built-in unsafe package. 1145 void 1146 import_unsafe(const std::string&, bool is_exported, Location); 1147 1148 // Return the current binding contour. 1149 Bindings* 1150 current_bindings(); 1151 1152 const Bindings* 1153 current_bindings() const; 1154 1155 void 1156 write_c_header(); 1157 1158 // Get the decl for the magic initialization function. 1159 Named_object* 1160 initialization_function_decl(); 1161 1162 // Create the magic initialization function. 1163 Named_object* 1164 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt); 1165 1166 // Initialize imported packages. BFUNCTION is the function 1167 // into which the package init calls will be placed. 1168 void 1169 init_imports(std::vector<Bstatement*>&, Bfunction* bfunction); 1170 1171 // Register variables with the garbage collector. 1172 void 1173 register_gc_vars(const std::vector<Named_object*>&, 1174 std::vector<Bstatement*>&, 1175 Bfunction* init_bfunction); 1176 1177 // Build the list of type descriptors. 1178 void 1179 build_type_descriptor_list(); 1180 1181 // Register the type descriptors with the runtime. 1182 void 1183 register_type_descriptors(std::vector<Bstatement*>&, 1184 Bfunction* init_bfunction); 1185 1186 void 1187 propagate_writebarrierrec(); 1188 1189 Named_object* 1190 write_barrier_variable(); 1191 1192 static bool 1193 is_digits(const std::string&); 1194 1195 // Type used to map go:embed patterns to a list of files. 1196 typedef Unordered_map(std::string, std::vector<std::string>) Embed_patterns; 1197 1198 // Type used to map go:embed file names to their full path. 1199 typedef Unordered_map(std::string, std::string) Embed_files; 1200 1201 // Type used to map import names to packages. 1202 typedef std::map<std::string, Package*> Imports; 1203 1204 // Type used to map package names to packages. 1205 typedef std::map<std::string, Package*> Packages; 1206 1207 // Type used to map variables to the function calls that set them. 1208 // This is used for initialization dependency analysis. 1209 typedef std::map<Variable*, Named_object*> Var_deps; 1210 1211 // Type used to map identifiers in the file block to the location 1212 // where they were defined. 1213 typedef Unordered_map(std::string, Location) File_block_names; 1214 1215 // Type used to queue writing a type specific function. 1216 struct Specific_type_function 1217 { 1218 enum Specific_type_function_kind { SPECIFIC_HASH, SPECIFIC_EQUAL }; 1219 1220 Type* type; 1221 Named_type* name; 1222 int64_t size; 1223 Specific_type_function_kind kind; 1224 Backend_name bname; 1225 Function_type* fntype; 1226 1227 Specific_type_function(Type* atype, Named_type* aname, int64_t asize, 1228 Specific_type_function_kind akind, 1229 Backend_name* abname, 1230 Function_type* afntype) 1231 : type(atype), name(aname), size(asize), kind(akind), 1232 bname(*abname), fntype(afntype) 1233 { } 1234 }; 1235 1236 // Recompute init priorities. 1237 void 1238 recompute_init_priorities(); 1239 1240 // Recursive helper used by the routine above. 1241 void 1242 update_init_priority(Import_init* ii, 1243 std::set<const Import_init *>* visited); 1244 1245 // The backend generator. 1246 Backend* backend_; 1247 // The object used to keep track of file names and line numbers. 1248 Linemap* linemap_; 1249 // The package we are compiling. 1250 Package* package_; 1251 // The list of currently open functions during parsing. 1252 Open_functions functions_; 1253 // The global binding contour. This includes the builtin functions 1254 // and the package we are compiling. 1255 Bindings* globals_; 1256 // The list of names we have seen in the file block. 1257 File_block_names file_block_names_; 1258 // Mapping from import file names to packages. 1259 Imports imports_; 1260 // Whether the magic unsafe package was imported. 1261 bool imported_unsafe_; 1262 // Whether the magic unsafe package was imported by the current file. 1263 bool current_file_imported_unsafe_; 1264 // Whether the embed package was imported by the current file. 1265 bool current_file_imported_embed_; 1266 // Mapping from package names we have seen to packages. This does 1267 // not include the package we are compiling. 1268 Packages packages_; 1269 // The functions named "init", if there are any. 1270 std::vector<Named_object*> init_functions_; 1271 // A mapping from variables to the function calls that initialize 1272 // them, if it is not stored in the variable's init or preinit. 1273 // This is used for dependency analysis. 1274 Var_deps var_deps_; 1275 // Whether we need a magic initialization function. 1276 bool need_init_fn_; 1277 // The name of the magic initialization function. 1278 std::string init_fn_name_; 1279 // A list of import control variables for packages that we import. 1280 Import_init_set imported_init_fns_; 1281 // The package path used for reflection data. 1282 std::string pkgpath_; 1283 // The package path to use for a symbol name. 1284 std::string pkgpath_symbol_; 1285 // The prefix to use for symbols, from the -fgo-prefix option. 1286 std::string prefix_; 1287 // Whether pkgpath_ has been set. 1288 bool pkgpath_set_; 1289 // Whether an explicit package path was set by -fgo-pkgpath. 1290 bool pkgpath_from_option_; 1291 // Whether an explicit prefix was set by -fgo-prefix. 1292 bool prefix_from_option_; 1293 // The relative import path, from the -fgo-relative-import-path 1294 // option. 1295 std::string relative_import_path_; 1296 // The C header file to write, from the -fgo-c-header option. 1297 std::string c_header_; 1298 // Patterns from an embedcfg file. 1299 Embed_patterns embed_patterns_; 1300 // Mapping from file to full path from an embedcfg file. 1301 Embed_files embed_files_; 1302 // Whether or not to check for division by zero, from the 1303 // -fgo-check-divide-zero option. 1304 bool check_divide_by_zero_; 1305 // Whether or not to check for division overflow, from the 1306 // -fgo-check-divide-overflow option. 1307 bool check_divide_overflow_; 1308 // Whether we are compiling the runtime package, from the 1309 // -fgo-compiling-runtime option. 1310 bool compiling_runtime_; 1311 // The level of escape analysis debug information to emit, from the 1312 // -fgo-debug-escape option. 1313 int debug_escape_level_; 1314 // A hash value for debug escape analysis, from the 1315 // -fgo-debug-escape-hash option. The analysis is run only on 1316 // functions with names that hash to the matching value. 1317 std::string debug_escape_hash_; 1318 // Whether to output optimization diagnostics, from the 1319 // -fgo-debug-optimization option. 1320 bool debug_optimization_; 1321 // Nil-check size threshhold. 1322 int64_t nil_check_size_threshold_; 1323 // Whether runtime.eqtype calls are needed when comparing type 1324 // descriptors. 1325 bool need_eqtype_; 1326 // A list of types to verify. 1327 std::vector<Type*> verify_types_; 1328 // A list of interface types defined while parsing. 1329 std::vector<Interface_type*> interface_types_; 1330 // Type specific functions to write out. 1331 std::vector<Specific_type_function*> specific_type_functions_; 1332 // Whether we are done writing out specific type functions. 1333 bool specific_type_functions_are_written_; 1334 // Whether named types have been converted. 1335 bool named_types_are_converted_; 1336 // A list containing groups of possibly mutually recursive functions to be 1337 // considered during escape analysis. 1338 std::vector<Analysis_set> analysis_sets_; 1339 // A list of objects to add to the GC roots. 1340 std::vector<Expression*> gc_roots_; 1341 // A list of type descriptors that we need to register. 1342 std::vector<Type*> type_descriptors_; 1343 // A list of function declarations with imported bodies that we may 1344 // want to inline. 1345 std::vector<Named_object*> imported_inlinable_functions_; 1346 // A list of functions that we want to inline. These will be sent 1347 // to the backend. 1348 std::vector<Named_object*> imported_inline_functions_; 1349 }; 1350 1351 // A block of statements. 1352 1353 class Block 1354 { 1355 public: 1356 Block(Block* enclosing, Location); 1357 1358 // Return the enclosing block. 1359 const Block* 1360 enclosing() const 1361 { return this->enclosing_; } 1362 1363 // Return the bindings of the block. 1364 Bindings* 1365 bindings() 1366 { return this->bindings_; } 1367 1368 const Bindings* 1369 bindings() const 1370 { return this->bindings_; } 1371 1372 // Look at the block's statements. 1373 const std::vector<Statement*>* 1374 statements() const 1375 { return &this->statements_; } 1376 1377 // Return the start location. This is normally the location of the 1378 // left curly brace which starts the block. 1379 Location 1380 start_location() const 1381 { return this->start_location_; } 1382 1383 // Return the end location. This is normally the location of the 1384 // right curly brace which ends the block. 1385 Location 1386 end_location() const 1387 { return this->end_location_; } 1388 1389 // Add a statement to the block. 1390 void 1391 add_statement(Statement*); 1392 1393 // Add a statement to the front of the block. 1394 void 1395 add_statement_at_front(Statement*); 1396 1397 // Replace a statement in a block. 1398 void 1399 replace_statement(size_t index, Statement*); 1400 1401 // Add a Statement before statement number INDEX. 1402 void 1403 insert_statement_before(size_t index, Statement*); 1404 1405 // Add a Statement after statement number INDEX. 1406 void 1407 insert_statement_after(size_t index, Statement*); 1408 1409 // Set the end location of the block. 1410 void 1411 set_end_location(Location location) 1412 { this->end_location_ = location; } 1413 1414 // Traverse the tree. 1415 int 1416 traverse(Traverse*); 1417 1418 // Set final types for unspecified variables and constants. 1419 void 1420 determine_types(); 1421 1422 // Return true if execution of this block may fall through to the 1423 // next block. 1424 bool 1425 may_fall_through() const; 1426 1427 // Write the export data for the block's statements to the string. 1428 void 1429 export_block(Export_function_body*); 1430 1431 // Turn exported block data into a block. 1432 static bool 1433 import_block(Block*, Import_function_body*, Location); 1434 1435 // Convert the block to the backend representation. 1436 Bblock* 1437 get_backend(Translate_context*); 1438 1439 // Iterate over statements. 1440 1441 typedef std::vector<Statement*>::iterator iterator; 1442 1443 iterator 1444 begin() 1445 { return this->statements_.begin(); } 1446 1447 iterator 1448 end() 1449 { return this->statements_.end(); } 1450 1451 private: 1452 // Enclosing block. 1453 Block* enclosing_; 1454 // Statements in the block. 1455 std::vector<Statement*> statements_; 1456 // Binding contour. 1457 Bindings* bindings_; 1458 // Location of start of block. 1459 Location start_location_; 1460 // Location of end of block. 1461 Location end_location_; 1462 }; 1463 1464 // A function. 1465 1466 class Function 1467 { 1468 public: 1469 Function(Function_type* type, Named_object*, Block*, Location); 1470 1471 // Return the function's type. 1472 Function_type* 1473 type() const 1474 { return this->type_; } 1475 1476 // Return the enclosing function if there is one. 1477 Named_object* 1478 enclosing() const 1479 { return this->enclosing_; } 1480 1481 // Set the enclosing function. This is used when building thunks 1482 // for functions which call recover. 1483 void 1484 set_enclosing(Named_object* enclosing) 1485 { 1486 go_assert(this->enclosing_ == NULL); 1487 this->enclosing_ = enclosing; 1488 } 1489 1490 // The result variables. 1491 typedef std::vector<Named_object*> Results; 1492 1493 // Create the result variables in the outer block. 1494 void 1495 create_result_variables(Gogo*); 1496 1497 // Update the named result variables when cloning a function which 1498 // calls recover. 1499 void 1500 update_result_variables(); 1501 1502 // Return the result variables. 1503 Results* 1504 result_variables() 1505 { return this->results_; } 1506 1507 bool 1508 is_sink() const 1509 { return this->is_sink_; } 1510 1511 void 1512 set_is_sink() 1513 { this->is_sink_ = true; } 1514 1515 // Whether the result variables have names. 1516 bool 1517 results_are_named() const 1518 { return this->results_are_named_; } 1519 1520 // Return the assembler name. 1521 const std::string& 1522 asm_name() const 1523 { return this->asm_name_; } 1524 1525 // Set the assembler name. 1526 void 1527 set_asm_name(const std::string& asm_name) 1528 { this->asm_name_ = asm_name; } 1529 1530 // Mark this symbol as exported by a linkname directive. 1531 void 1532 set_is_exported_by_linkname() 1533 { this->is_exported_by_linkname_ = true; } 1534 1535 // Return the pragmas for this function. 1536 unsigned int 1537 pragmas() const 1538 { return this->pragmas_; } 1539 1540 // Set the pragmas for this function. 1541 void 1542 set_pragmas(unsigned int pragmas) 1543 { 1544 this->pragmas_ = pragmas; 1545 } 1546 1547 // Return the index to use for a nested function. 1548 unsigned int 1549 next_nested_function_index() 1550 { 1551 ++this->nested_functions_; 1552 return this->nested_functions_; 1553 } 1554 1555 // Whether this method should not be included in the type 1556 // descriptor. 1557 bool 1558 nointerface() const; 1559 1560 // Record that this method should not be included in the type 1561 // descriptor. 1562 void 1563 set_nointerface(); 1564 1565 // Record that this function is a stub method created for an unnamed 1566 // type. 1567 void 1568 set_is_unnamed_type_stub_method() 1569 { 1570 go_assert(this->is_method()); 1571 this->is_unnamed_type_stub_method_ = true; 1572 } 1573 1574 // Return the amount of enclosed variables in this closure. 1575 size_t 1576 closure_field_count() const 1577 { return this->closure_fields_.size(); } 1578 1579 // Add a new field to the closure variable. 1580 void 1581 add_closure_field(Named_object* var, Location loc) 1582 { this->closure_fields_.push_back(std::make_pair(var, loc)); } 1583 1584 // Whether this function needs a closure. 1585 bool 1586 needs_closure() const 1587 { return !this->closure_fields_.empty(); } 1588 1589 // Return the closure variable, creating it if necessary. This is 1590 // passed to the function as a static chain parameter. 1591 Named_object* 1592 closure_var(); 1593 1594 // Set the closure variable. This is used when building thunks for 1595 // functions which call recover. 1596 void 1597 set_closure_var(Named_object* v) 1598 { 1599 go_assert(this->closure_var_ == NULL); 1600 this->closure_var_ = v; 1601 } 1602 1603 // Return the variable for a reference to field INDEX in the closure 1604 // variable. 1605 Named_object* 1606 enclosing_var(unsigned int index) 1607 { 1608 go_assert(index < this->closure_fields_.size()); 1609 return closure_fields_[index].first; 1610 } 1611 1612 // Set the type of the closure variable if there is one. 1613 void 1614 set_closure_type(); 1615 1616 // Get the block of statements associated with the function. 1617 Block* 1618 block() const 1619 { return this->block_; } 1620 1621 // Get the location of the start of the function. 1622 Location 1623 location() const 1624 { return this->location_; } 1625 1626 // Return whether this function is actually a method. 1627 bool 1628 is_method() const; 1629 1630 // Add a label definition to the function. 1631 Label* 1632 add_label_definition(Gogo*, const std::string& label_name, Location); 1633 1634 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true 1635 // if we should report errors for a goto from the current location 1636 // to the label location. 1637 Label* 1638 add_label_reference(Gogo*, const std::string& label_name, 1639 Location, bool issue_goto_errors); 1640 1641 // Warn about labels that are defined but not used. 1642 void 1643 check_labels() const; 1644 1645 // Note that a new local type has been added. Return its index. 1646 unsigned int 1647 new_local_type_index() 1648 { return this->local_type_count_++; } 1649 1650 // Whether this function calls the predeclared recover function. 1651 bool 1652 calls_recover() const 1653 { return this->calls_recover_; } 1654 1655 // Record that this function calls the predeclared recover function. 1656 // This is set during the lowering pass. 1657 void 1658 set_calls_recover() 1659 { this->calls_recover_ = true; } 1660 1661 // Whether this is a recover thunk function. 1662 bool 1663 is_recover_thunk() const 1664 { return this->is_recover_thunk_; } 1665 1666 // Record that this is a thunk built for a function which calls 1667 // recover. 1668 void 1669 set_is_recover_thunk() 1670 { this->is_recover_thunk_ = true; } 1671 1672 // Whether this function already has a recover thunk. 1673 bool 1674 has_recover_thunk() const 1675 { return this->has_recover_thunk_; } 1676 1677 // Record that this function already has a recover thunk. 1678 void 1679 set_has_recover_thunk() 1680 { this->has_recover_thunk_ = true; } 1681 1682 // Record that this function is a thunk created for a defer 1683 // statement that calls the __go_set_defer_retaddr runtime function. 1684 void 1685 set_calls_defer_retaddr() 1686 { this->calls_defer_retaddr_ = true; } 1687 1688 // Whether this is a type hash or equality function created by the 1689 // compiler. 1690 bool 1691 is_type_specific_function() 1692 { return this->is_type_specific_function_; } 1693 1694 // Record that this function is a type hash or equality function 1695 // created by the compiler. 1696 void 1697 set_is_type_specific_function() 1698 { this->is_type_specific_function_ = true; } 1699 1700 // Mark the function as going into a unique section. 1701 void 1702 set_in_unique_section() 1703 { this->in_unique_section_ = true; } 1704 1705 // Return whether this function should be exported for inlining. 1706 bool 1707 export_for_inlining() const 1708 { return this->export_for_inlining_; } 1709 1710 // Mark the function to be exported for inlining. 1711 void 1712 set_export_for_inlining() 1713 { this->export_for_inlining_ = true; } 1714 1715 // Return whether this function is inline only. 1716 bool 1717 is_inline_only() const 1718 { return this->is_inline_only_; } 1719 1720 // Mark the function as inline only: the body should not be emitted 1721 // if it is not inlined. 1722 void 1723 set_is_inline_only() 1724 { this->is_inline_only_ = true; } 1725 1726 // Report whether the function is referenced by an inline body. 1727 bool 1728 is_referenced_by_inline() const 1729 { return this->is_referenced_by_inline_; } 1730 1731 // Mark the function as referenced by an inline body. 1732 void 1733 set_is_referenced_by_inline() 1734 { this->is_referenced_by_inline_ = true; } 1735 1736 // Set the receiver type. This is used to remove aliases. 1737 void 1738 set_receiver_type(Type* rtype); 1739 1740 // Swap with another function. Used only for the thunk which calls 1741 // recover. 1742 void 1743 swap_for_recover(Function *); 1744 1745 // Traverse the tree. 1746 int 1747 traverse(Traverse*); 1748 1749 // Determine types in the function. 1750 void 1751 determine_types(); 1752 1753 // Return an expression for the function descriptor, given the named 1754 // object for this function. This may only be called for functions 1755 // without a closure. This will be an immutable struct with one 1756 // field that points to the function's code. 1757 Expression* 1758 descriptor(Gogo*, Named_object*); 1759 1760 // Set the descriptor for this function. This is used when a 1761 // function declaration is followed by a function definition. 1762 void 1763 set_descriptor(Expression* descriptor) 1764 { 1765 go_assert(this->descriptor_ == NULL); 1766 this->descriptor_ = descriptor; 1767 } 1768 1769 // Return the backend representation. 1770 Bfunction* 1771 get_or_make_decl(Gogo*, Named_object*); 1772 1773 // Return the function's decl after it has been built. 1774 Bfunction* 1775 get_decl() const; 1776 1777 // Set the function decl to hold a backend representation of the function 1778 // code. 1779 void 1780 build(Gogo*, Named_object*); 1781 1782 // Get the statement that assigns values to this function's result struct. 1783 Bstatement* 1784 return_value(Gogo*, Named_object*, Location) const; 1785 1786 // Get the backend name of this function. 1787 void 1788 backend_name(Gogo*, Named_object*, Backend_name*); 1789 1790 // Get an expression for the variable holding the defer stack. 1791 Expression* 1792 defer_stack(Location); 1793 1794 // Export the function. 1795 void 1796 export_func(Export*, const Named_object*) const; 1797 1798 // Export a function with a type. 1799 static void 1800 export_func_with_type(Export*, const Named_object*, 1801 const Function_type*, Results*, bool nointerface, 1802 const std::string& asm_name, Block* block, Location); 1803 1804 // Import a function. Reports whether the import succeeded. 1805 static bool 1806 import_func(Import*, std::string* pname, Package** pkg, 1807 bool* is_exported, Typed_identifier** receiver, 1808 Typed_identifier_list** pparameters, 1809 Typed_identifier_list** presults, bool* is_varargs, 1810 bool* nointerface, std::string* asm_name, std::string* body); 1811 1812 private: 1813 // Type for mapping from label names to Label objects. 1814 typedef Unordered_map(std::string, Label*) Labels; 1815 1816 void 1817 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**); 1818 1819 typedef std::vector<std::pair<Named_object*, 1820 Location> > Closure_fields; 1821 1822 // The function's type. 1823 Function_type* type_; 1824 // The enclosing function. This is NULL when there isn't one, which 1825 // is the normal case. 1826 Named_object* enclosing_; 1827 // The result variables, if any. 1828 Results* results_; 1829 // If there is a closure, this is the list of variables which appear 1830 // in the closure. This is created by the parser, and then resolved 1831 // to a real type when we lower parse trees. 1832 Closure_fields closure_fields_; 1833 // The closure variable, passed as a parameter using the static 1834 // chain parameter. Normally NULL. 1835 Named_object* closure_var_; 1836 // The outer block of statements in the function. 1837 Block* block_; 1838 // The source location of the start of the function. 1839 Location location_; 1840 // Labels defined or referenced in the function. 1841 Labels labels_; 1842 // The number of local types defined in this function. 1843 unsigned int local_type_count_; 1844 // The assembler name: this is the name that will be put in the object file. 1845 // Set by the go:linkname compiler directive. This is normally empty. 1846 std::string asm_name_; 1847 // The function descriptor, if any. 1848 Expression* descriptor_; 1849 // The function decl. 1850 Bfunction* fndecl_; 1851 // The defer stack variable. A pointer to this variable is used to 1852 // distinguish the defer stack for one function from another. This 1853 // is NULL unless we actually need a defer stack. 1854 Temporary_statement* defer_stack_; 1855 // Pragmas for this function. This is a set of GOPRAGMA bits. 1856 unsigned int pragmas_; 1857 // Number of nested functions defined within this function. 1858 unsigned int nested_functions_; 1859 // True if this function is sink-named. No code is generated. 1860 bool is_sink_ : 1; 1861 // True if the result variables are named. 1862 bool results_are_named_ : 1; 1863 // True if this function is a stub method created for an unnamed 1864 // type. 1865 bool is_unnamed_type_stub_method_ : 1; 1866 // True if this function calls the predeclared recover function. 1867 bool calls_recover_ : 1; 1868 // True if this a thunk built for a function which calls recover. 1869 bool is_recover_thunk_ : 1; 1870 // True if this function already has a recover thunk. 1871 bool has_recover_thunk_ : 1; 1872 // True if this is a thunk built for a defer statement that calls 1873 // the __go_set_defer_retaddr runtime function. 1874 bool calls_defer_retaddr_ : 1; 1875 // True if this is a function built by the compiler to as a hash or 1876 // equality function for some type. 1877 bool is_type_specific_function_ : 1; 1878 // True if this function should be put in a unique section. This is 1879 // turned on for field tracking. 1880 bool in_unique_section_ : 1; 1881 // True if we should export the body of this function for 1882 // cross-package inlining. 1883 bool export_for_inlining_ : 1; 1884 // True if this function is inline only: if it should not be emitted 1885 // if it is not inlined. 1886 bool is_inline_only_ : 1; 1887 // True if this function is referenced from an inlined body that 1888 // will be put into the export data. 1889 bool is_referenced_by_inline_ : 1; 1890 // True if we should make this function visible to other packages 1891 // because of a go:linkname directive. 1892 bool is_exported_by_linkname_ : 1; 1893 }; 1894 1895 // A snapshot of the current binding state. 1896 1897 class Bindings_snapshot 1898 { 1899 public: 1900 Bindings_snapshot(const Block*, Location); 1901 1902 // Report any errors appropriate for a goto from the current binding 1903 // state of B to this one. 1904 void 1905 check_goto_from(const Block* b, Location); 1906 1907 // Report any errors appropriate for a goto from this binding state 1908 // to the current state of B. 1909 void 1910 check_goto_to(const Block* b); 1911 1912 private: 1913 bool 1914 check_goto_block(Location, const Block*, const Block*, size_t*); 1915 1916 void 1917 check_goto_defs(Location, const Block*, size_t, size_t); 1918 1919 // The current block. 1920 const Block* block_; 1921 // The number of names currently defined in each open block. 1922 // Element 0 is this->block_, element 1 is 1923 // this->block_->enclosing(), etc. 1924 std::vector<size_t> counts_; 1925 // The location where this snapshot was taken. 1926 Location location_; 1927 }; 1928 1929 // A function declaration. 1930 1931 class Function_declaration 1932 { 1933 public: 1934 Function_declaration(Function_type* fntype, Location location) 1935 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL), 1936 fndecl_(NULL), pragmas_(0), imported_body_(), 1937 is_on_inlinable_list_(false) 1938 { } 1939 1940 Function_type* 1941 type() const 1942 { return this->fntype_; } 1943 1944 Location 1945 location() const 1946 { return this->location_; } 1947 1948 // Return whether this function declaration is a method. 1949 bool 1950 is_method() const; 1951 1952 const std::string& 1953 asm_name() const 1954 { return this->asm_name_; } 1955 1956 // Set the assembler name. 1957 void 1958 set_asm_name(const std::string& asm_name) 1959 { this->asm_name_ = asm_name; } 1960 1961 // Return the pragmas for this function. 1962 unsigned int 1963 pragmas() const 1964 { return this->pragmas_; } 1965 1966 // Set the pragmas for this function. 1967 void 1968 set_pragmas(unsigned int pragmas) 1969 { 1970 this->pragmas_ = pragmas; 1971 } 1972 1973 // Whether this method should not be included in the type 1974 // descriptor. 1975 bool 1976 nointerface() const; 1977 1978 // Record that this method should not be included in the type 1979 // descriptor. 1980 void 1981 set_nointerface(); 1982 1983 // Whether we have an imported function body. 1984 bool 1985 has_imported_body() const 1986 { return !this->imported_body_.empty(); } 1987 1988 // Record the imported body of this function. 1989 void 1990 set_imported_body(Import* imp, const std::string& imported_body) 1991 { 1992 this->imp_ = imp; 1993 this->imported_body_ = imported_body; 1994 } 1995 1996 // Whether this declaration is on the list of inlinable functions. 1997 bool 1998 is_on_inlinable_list() const 1999 { return this->is_on_inlinable_list_; } 2000 2001 // Set that this function is on the list of inlinable functions. 2002 void 2003 set_is_on_inlinable_list() 2004 { this->is_on_inlinable_list_ = true; } 2005 2006 // Set the receiver type. This is used to remove aliases. 2007 void 2008 set_receiver_type(Type* rtype); 2009 2010 // Import the function body, creating a function. 2011 void 2012 import_function_body(Gogo*, Named_object*); 2013 2014 // Return an expression for the function descriptor, given the named 2015 // object for this function. This may only be called for functions 2016 // without a closure. This will be an immutable struct with one 2017 // field that points to the function's code. 2018 Expression* 2019 descriptor(Gogo*, Named_object*); 2020 2021 // Return true if we have created a descriptor for this declaration. 2022 bool 2023 has_descriptor() const 2024 { return this->descriptor_ != NULL; } 2025 2026 // Return a backend representation. 2027 Bfunction* 2028 get_or_make_decl(Gogo*, Named_object*); 2029 2030 // If there is a descriptor, build it into the backend 2031 // representation. 2032 void 2033 build_backend_descriptor(Gogo*); 2034 2035 // Get the backend name of this function declaration. 2036 void 2037 backend_name(Gogo*, Named_object*, Backend_name*); 2038 2039 // Export a function declaration. 2040 void 2041 export_func(Export* exp, const Named_object* no) const 2042 { 2043 Function::export_func_with_type(exp, no, this->fntype_, NULL, 2044 this->is_method() && this->nointerface(), 2045 this->asm_name_, NULL, this->location_); 2046 } 2047 2048 // Check that the types used in this declaration's signature are defined. 2049 void 2050 check_types() const; 2051 2052 private: 2053 // The type of the function. 2054 Function_type* fntype_; 2055 // The location of the declaration. 2056 Location location_; 2057 // The assembler name: this is the name to use in references to the 2058 // function. This is normally empty. 2059 std::string asm_name_; 2060 // The function descriptor, if any. 2061 Expression* descriptor_; 2062 // The function decl if needed. 2063 Bfunction* fndecl_; 2064 // Pragmas for this function. This is a set of GOPRAGMA bits. 2065 unsigned int pragmas_; 2066 // Importer for function body if imported from a different package. 2067 Import* imp_; 2068 // Export data for function body if imported from a different package. 2069 std::string imported_body_; 2070 // Whether this declaration is already on the list of inlinable functions. 2071 bool is_on_inlinable_list_; 2072 }; 2073 2074 // A variable. 2075 2076 class Variable 2077 { 2078 public: 2079 Variable(Type*, Expression*, bool is_global, bool is_parameter, 2080 bool is_receiver, Location); 2081 2082 // Get the type of the variable. 2083 Type* 2084 type(); 2085 2086 Type* 2087 type() const; 2088 2089 // Return whether the type is defined yet. 2090 bool 2091 has_type() const; 2092 2093 // Get the initial value. 2094 Expression* 2095 init() const 2096 { return this->init_; } 2097 2098 // Return whether there are any preinit statements. 2099 bool 2100 has_pre_init() const 2101 { return this->preinit_ != NULL; } 2102 2103 // Return the preinit statements if any. 2104 Block* 2105 preinit() const 2106 { return this->preinit_; } 2107 2108 // Return whether this is a global variable. 2109 bool 2110 is_global() const 2111 { return this->is_global_; } 2112 2113 // Return whether this is a function parameter. 2114 bool 2115 is_parameter() const 2116 { return this->is_parameter_; } 2117 2118 // Return whether this is a closure (static chain) parameter. 2119 bool 2120 is_closure() const 2121 { return this->is_closure_; } 2122 2123 // Change this parameter to be a closure. 2124 void 2125 set_is_closure() 2126 { 2127 this->is_closure_ = true; 2128 } 2129 2130 // Return whether this is the receiver parameter of a method. 2131 bool 2132 is_receiver() const 2133 { return this->is_receiver_; } 2134 2135 // Change this parameter to be a receiver. This is used when 2136 // creating the thunks created for functions which call recover. 2137 void 2138 set_is_receiver() 2139 { 2140 go_assert(this->is_parameter_); 2141 this->is_receiver_ = true; 2142 } 2143 2144 // Change this parameter to not be a receiver. This is used when 2145 // creating the thunks created for functions which call recover. 2146 void 2147 set_is_not_receiver() 2148 { 2149 go_assert(this->is_parameter_); 2150 this->is_receiver_ = false; 2151 } 2152 2153 // Return whether this is the varargs parameter of a function. 2154 bool 2155 is_varargs_parameter() const 2156 { return this->is_varargs_parameter_; } 2157 2158 // Return whether this is a global sink variable, created only to 2159 // run an initializer. 2160 bool 2161 is_global_sink() const 2162 { return this->is_global_sink_; } 2163 2164 // Record that this is a global sink variable. 2165 void 2166 set_is_global_sink() 2167 { 2168 go_assert(this->is_global_); 2169 this->is_global_sink_ = true; 2170 } 2171 2172 // Whether this variable's address is taken. 2173 bool 2174 is_address_taken() const 2175 { return this->is_address_taken_; } 2176 2177 // Whether this variable should live in the heap. 2178 bool 2179 is_in_heap() const 2180 { return this->is_address_taken_ && !this->is_global_; } 2181 2182 // Note that something takes the address of this variable. 2183 void 2184 set_address_taken() 2185 { this->is_address_taken_ = true; } 2186 2187 // Return whether the address is taken but does not escape. 2188 bool 2189 is_non_escaping_address_taken() const 2190 { return this->is_non_escaping_address_taken_; } 2191 2192 // Note that something takes the address of this variable such that 2193 // the address does not escape the function. 2194 void 2195 set_non_escaping_address_taken() 2196 { this->is_non_escaping_address_taken_ = true; } 2197 2198 // Get the source location of the variable's declaration. 2199 Location 2200 location() const 2201 { return this->location_; } 2202 2203 // Record that this is the varargs parameter of a function. 2204 void 2205 set_is_varargs_parameter() 2206 { 2207 go_assert(this->is_parameter_); 2208 this->is_varargs_parameter_ = true; 2209 } 2210 2211 // Return whether the variable has been used. 2212 bool 2213 is_used() const 2214 { return this->is_used_; } 2215 2216 // Mark that the variable has been used. 2217 void 2218 set_is_used() 2219 { this->is_used_ = true; } 2220 2221 // Clear the initial value; used for error handling and write barriers. 2222 void 2223 clear_init() 2224 { this->init_ = NULL; } 2225 2226 // Set the initial value; used for converting shortcuts. 2227 void 2228 set_init(Expression* init) 2229 { this->init_ = init; } 2230 2231 // Get the preinit block, a block of statements to be run before the 2232 // initialization expression. 2233 Block* 2234 preinit_block(Gogo*); 2235 2236 // Add a statement to be run before the initialization expression. 2237 // This is only used for global variables. 2238 void 2239 add_preinit_statement(Gogo*, Statement*); 2240 2241 // Lower the initialization expression after parsing is complete. 2242 void 2243 lower_init_expression(Gogo*, Named_object*, Statement_inserter*); 2244 2245 // Flatten the initialization expression after ordering evaluations. 2246 void 2247 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*); 2248 2249 // A special case: the init value is used only to determine the 2250 // type. This is used if the variable is defined using := with the 2251 // comma-ok form of a map index or a receive expression. The init 2252 // value is actually the map index expression or receive expression. 2253 // We use this because we may not know the right type at parse time. 2254 void 2255 set_type_from_init_tuple() 2256 { this->type_from_init_tuple_ = true; } 2257 2258 // Another special case: the init value is used only to determine 2259 // the type. This is used if the variable is defined using := with 2260 // a range clause. The init value is the range expression. The 2261 // type of the variable is the index type of the range expression 2262 // (i.e., the first value returned by a range). 2263 void 2264 set_type_from_range_index() 2265 { this->type_from_range_index_ = true; } 2266 2267 // Another special case: like set_type_from_range_index, but the 2268 // type is the value type of the range expression (i.e., the second 2269 // value returned by a range). 2270 void 2271 set_type_from_range_value() 2272 { this->type_from_range_value_ = true; } 2273 2274 // Another special case: the init value is used only to determine 2275 // the type. This is used if the variable is defined using := with 2276 // a case in a select statement. The init value is the channel. 2277 // The type of the variable is the channel's element type. 2278 void 2279 set_type_from_chan_element() 2280 { this->type_from_chan_element_ = true; } 2281 2282 // After we lower the select statement, we once again set the type 2283 // from the initialization expression. 2284 void 2285 clear_type_from_chan_element() 2286 { 2287 go_assert(this->type_from_chan_element_); 2288 this->type_from_chan_element_ = false; 2289 } 2290 2291 // TRUE if this variable was created for a type switch clause. 2292 bool 2293 is_type_switch_var() const 2294 { return this->is_type_switch_var_; } 2295 2296 // Note that this variable was created for a type switch clause. 2297 void 2298 set_is_type_switch_var() 2299 { this->is_type_switch_var_ = true; } 2300 2301 // Mark the variable as going into a unique section. 2302 void 2303 set_in_unique_section() 2304 { 2305 go_assert(this->is_global_); 2306 this->in_unique_section_ = true; 2307 } 2308 2309 // Mark the variable as referenced by an inline body. 2310 void 2311 set_is_referenced_by_inline() 2312 { 2313 go_assert(this->is_global_); 2314 this->is_referenced_by_inline_ = true; 2315 } 2316 2317 // Attach any go:embed comments for this variable. 2318 void 2319 set_embeds(std::vector<std::string>* embeds) 2320 { 2321 go_assert(this->is_global_ 2322 && this->init_ == NULL 2323 && this->preinit_ == NULL); 2324 this->embeds_ = embeds; 2325 } 2326 2327 // Return the top-level declaration for this variable. 2328 Statement* 2329 toplevel_decl() 2330 { return this->toplevel_decl_; } 2331 2332 // Set the top-level declaration for this variable. Only used for local 2333 // variables 2334 void 2335 set_toplevel_decl(Statement* s) 2336 { 2337 go_assert(!this->is_global_ && !this->is_parameter_ && !this->is_receiver_); 2338 this->toplevel_decl_ = s; 2339 } 2340 2341 // Note that the initializer of this global variable refers to VAR. 2342 void 2343 add_init_ref(Named_object* var); 2344 2345 // The variables that this variable's initializers refer to. 2346 const std::vector<Named_object*>* 2347 init_refs() const 2348 { return this->init_refs_; } 2349 2350 // Traverse the initializer expression. 2351 int 2352 traverse_expression(Traverse*, unsigned int traverse_mask); 2353 2354 // Determine the type of the variable if necessary. 2355 void 2356 determine_type(); 2357 2358 // Get the backend representation of the variable. 2359 Bvariable* 2360 get_backend_variable(Gogo*, Named_object*, const Package*, 2361 const std::string&); 2362 2363 // Get the initial value of the variable. This may only 2364 // be called if has_pre_init() returns false. 2365 Bexpression* 2366 get_init(Gogo*, Named_object* function); 2367 2368 // Return a series of statements which sets the value of the 2369 // variable in DECL. This should only be called is has_pre_init() 2370 // returns true. DECL may be NULL for a sink variable. 2371 Bstatement* 2372 get_init_block(Gogo*, Named_object* function, Bvariable* decl); 2373 2374 // Export the variable. 2375 void 2376 export_var(Export*, const Named_object*) const; 2377 2378 // Import a variable. Reports whether the import succeeded. 2379 static bool 2380 import_var(Import*, std::string* pname, Package** pkg, bool* is_exported, 2381 Type** ptype); 2382 2383 private: 2384 // The type of a tuple. 2385 Type* 2386 type_from_tuple(Expression*, bool) const; 2387 2388 // The type of a range. 2389 Type* 2390 type_from_range(Expression*, bool, bool) const; 2391 2392 // The element type of a channel. 2393 Type* 2394 type_from_chan_element(Expression*, bool) const; 2395 2396 // The variable's type. This may be NULL if the type is set from 2397 // the expression. 2398 Type* type_; 2399 // The initial value. This may be NULL if the variable should be 2400 // initialized to the default value for the type. 2401 Expression* init_; 2402 // Statements to run before the init statement. 2403 Block* preinit_; 2404 // Location of variable definition. 2405 Location location_; 2406 // The top-level declaration for this variable. Only used for local 2407 // variables. Must be a Temporary_statement if not NULL. 2408 Statement* toplevel_decl_; 2409 // Variables that the initializer of a global variable refers to. 2410 // Used for initializer ordering. 2411 std::vector<Named_object*>* init_refs_; 2412 // Any associated go:embed comments. 2413 std::vector<std::string>* embeds_; 2414 // Backend representation. 2415 Bvariable* backend_; 2416 // Whether this is a global variable. 2417 bool is_global_ : 1; 2418 // Whether this is a function parameter. 2419 bool is_parameter_ : 1; 2420 // Whether this is a closure parameter. 2421 bool is_closure_ : 1; 2422 // Whether this is the receiver parameter of a method. 2423 bool is_receiver_ : 1; 2424 // Whether this is the varargs parameter of a function. 2425 bool is_varargs_parameter_ : 1; 2426 // Whether this is a global sink variable created to run an 2427 // initializer. 2428 bool is_global_sink_ : 1; 2429 // Whether this variable is ever referenced. 2430 bool is_used_ : 1; 2431 // Whether something takes the address of this variable. For a 2432 // local variable this implies that the variable has to be on the 2433 // heap if it escapes from its function. 2434 bool is_address_taken_ : 1; 2435 // Whether something takes the address of this variable such that 2436 // the address does not escape the function. 2437 bool is_non_escaping_address_taken_ : 1; 2438 // True if we have seen this variable in a traversal. 2439 bool seen_ : 1; 2440 // True if we have lowered the initialization expression. 2441 bool init_is_lowered_ : 1; 2442 // True if we have flattened the initialization expression. 2443 bool init_is_flattened_ : 1; 2444 // True if init is a tuple used to set the type. 2445 bool type_from_init_tuple_ : 1; 2446 // True if init is a range clause and the type is the index type. 2447 bool type_from_range_index_ : 1; 2448 // True if init is a range clause and the type is the value type. 2449 bool type_from_range_value_ : 1; 2450 // True if init is a channel and the type is the channel's element type. 2451 bool type_from_chan_element_ : 1; 2452 // True if this is a variable created for a type switch case. 2453 bool is_type_switch_var_ : 1; 2454 // True if we have determined types. 2455 bool determined_type_ : 1; 2456 // True if this variable should be put in a unique section. This is 2457 // used for field tracking. 2458 bool in_unique_section_ : 1; 2459 // True if this variable is referenced from an inlined body that 2460 // will be put into the export data. 2461 bool is_referenced_by_inline_ : 1; 2462 }; 2463 2464 // A variable which is really the name for a function return value, or 2465 // part of one. 2466 2467 class Result_variable 2468 { 2469 public: 2470 Result_variable(Type* type, Function* function, int index, 2471 Location location) 2472 : type_(type), function_(function), index_(index), location_(location), 2473 backend_(NULL), is_address_taken_(false), 2474 is_non_escaping_address_taken_(false) 2475 { } 2476 2477 // Get the type of the result variable. 2478 Type* 2479 type() const 2480 { return this->type_; } 2481 2482 // Get the function that this is associated with. 2483 Function* 2484 function() const 2485 { return this->function_; } 2486 2487 // Index in the list of function results. 2488 int 2489 index() const 2490 { return this->index_; } 2491 2492 // The location of the variable definition. 2493 Location 2494 location() const 2495 { return this->location_; } 2496 2497 // Whether this variable's address is taken. 2498 bool 2499 is_address_taken() const 2500 { return this->is_address_taken_; } 2501 2502 // Note that something takes the address of this variable. 2503 void 2504 set_address_taken() 2505 { this->is_address_taken_ = true; } 2506 2507 // Return whether the address is taken but does not escape. 2508 bool 2509 is_non_escaping_address_taken() const 2510 { return this->is_non_escaping_address_taken_; } 2511 2512 // Note that something takes the address of this variable such that 2513 // the address does not escape the function. 2514 void 2515 set_non_escaping_address_taken() 2516 { this->is_non_escaping_address_taken_ = true; } 2517 2518 // Whether this variable should live in the heap. 2519 bool 2520 is_in_heap() const 2521 { return this->is_address_taken_; } 2522 2523 // Set the function. This is used when cloning functions which call 2524 // recover. 2525 void 2526 set_function(Function* function) 2527 { this->function_ = function; } 2528 2529 // Get the backend representation of the variable. 2530 Bvariable* 2531 get_backend_variable(Gogo*, Named_object*, const std::string&); 2532 2533 private: 2534 // Type of result variable. 2535 Type* type_; 2536 // Function with which this is associated. 2537 Function* function_; 2538 // Index in list of results. 2539 int index_; 2540 // Where the result variable is defined. 2541 Location location_; 2542 // Backend representation. 2543 Bvariable* backend_; 2544 // Whether something takes the address of this variable. 2545 bool is_address_taken_; 2546 // Whether something takes the address of this variable such that 2547 // the address does not escape the function. 2548 bool is_non_escaping_address_taken_; 2549 }; 2550 2551 // The value we keep for a named constant. This lets us hold a type 2552 // and an expression. 2553 2554 class Named_constant 2555 { 2556 public: 2557 Named_constant(Type* type, Expression* expr, int iota_value, 2558 Location location) 2559 : type_(type), expr_(expr), iota_value_(iota_value), location_(location), 2560 lowering_(false), is_sink_(false), bconst_(NULL) 2561 { } 2562 2563 Type* 2564 type() const 2565 { return this->type_; } 2566 2567 void 2568 set_type(Type* t); 2569 2570 Expression* 2571 expr() const 2572 { return this->expr_; } 2573 2574 int 2575 iota_value() const 2576 { return this->iota_value_; } 2577 2578 Location 2579 location() const 2580 { return this->location_; } 2581 2582 // Whether we are lowering. 2583 bool 2584 lowering() const 2585 { return this->lowering_; } 2586 2587 // Set that we are lowering. 2588 void 2589 set_lowering() 2590 { this->lowering_ = true; } 2591 2592 // We are no longer lowering. 2593 void 2594 clear_lowering() 2595 { this->lowering_ = false; } 2596 2597 bool 2598 is_sink() const 2599 { return this->is_sink_; } 2600 2601 void 2602 set_is_sink() 2603 { this->is_sink_ = true; } 2604 2605 // Traverse the expression. 2606 int 2607 traverse_expression(Traverse*); 2608 2609 // Determine the type of the constant if necessary. 2610 void 2611 determine_type(); 2612 2613 // Indicate that we found and reported an error for this constant. 2614 void 2615 set_error(); 2616 2617 // Export the constant. 2618 void 2619 export_const(Export*, const std::string& name) const; 2620 2621 // Import a constant. 2622 static void 2623 import_const(Import*, std::string*, Type**, Expression**); 2624 2625 // Get the backend representation of the constant value. 2626 Bexpression* 2627 get_backend(Gogo*, Named_object*); 2628 2629 private: 2630 // The type of the constant. 2631 Type* type_; 2632 // The expression for the constant. 2633 Expression* expr_; 2634 // If the predeclared constant iota is used in EXPR_, this is the 2635 // value it will have. We do this because at parse time we don't 2636 // know whether the name "iota" will refer to the predeclared 2637 // constant or to something else. We put in the right value in when 2638 // we lower. 2639 int iota_value_; 2640 // The location of the definition. 2641 Location location_; 2642 // Whether we are currently lowering this constant. 2643 bool lowering_; 2644 // Whether this constant is blank named and needs only type checking. 2645 bool is_sink_; 2646 // The backend representation of the constant value. 2647 Bexpression* bconst_; 2648 }; 2649 2650 // A type declaration. 2651 2652 class Type_declaration 2653 { 2654 public: 2655 Type_declaration(Location location) 2656 : location_(location), in_function_(NULL), in_function_index_(0), 2657 methods_(), issued_warning_(false) 2658 { } 2659 2660 // Return the location. 2661 Location 2662 location() const 2663 { return this->location_; } 2664 2665 // Return the function in which this type is declared. This will 2666 // return NULL for a type declared in global scope. 2667 Named_object* 2668 in_function(unsigned int* pindex) 2669 { 2670 *pindex = this->in_function_index_; 2671 return this->in_function_; 2672 } 2673 2674 // Set the function in which this type is declared. 2675 void 2676 set_in_function(Named_object* f, unsigned int index) 2677 { 2678 this->in_function_ = f; 2679 this->in_function_index_ = index; 2680 } 2681 2682 // Add a method to this type. This is used when methods are defined 2683 // before the type. 2684 Named_object* 2685 add_method(const std::string& name, Function* function); 2686 2687 // Add a method declaration to this type. 2688 Named_object* 2689 add_method_declaration(const std::string& name, Package*, 2690 Function_type* type, Location location); 2691 2692 // Add an already created object as a method. 2693 void 2694 add_existing_method(Named_object* no) 2695 { this->methods_.push_back(no); } 2696 2697 // Return whether any methods were defined. 2698 bool 2699 has_methods() const; 2700 2701 // Return the methods. 2702 const std::vector<Named_object*>* 2703 methods() const 2704 { return &this->methods_; } 2705 2706 // Define methods when the real type is known. 2707 void 2708 define_methods(Named_type*); 2709 2710 // This is called if we are trying to use this type. It returns 2711 // true if we should issue a warning. 2712 bool 2713 using_type(); 2714 2715 private: 2716 // The location of the type declaration. 2717 Location location_; 2718 // If this type is declared in a function, a pointer back to the 2719 // function in which it is defined. 2720 Named_object* in_function_; 2721 // The index of this type in IN_FUNCTION_. 2722 unsigned int in_function_index_; 2723 // Methods defined before the type is defined. 2724 std::vector<Named_object*> methods_; 2725 // True if we have issued a warning about a use of this type 2726 // declaration when it is undefined. 2727 bool issued_warning_; 2728 }; 2729 2730 // An unknown object. These are created by the parser for forward 2731 // references to names which have not been seen before. In a correct 2732 // program, these will always point to a real definition by the end of 2733 // the parse. Because they point to another Named_object, these may 2734 // only be referenced by Unknown_expression objects. 2735 2736 class Unknown_name 2737 { 2738 public: 2739 Unknown_name(Location location) 2740 : location_(location), real_named_object_(NULL) 2741 { } 2742 2743 // Return the location where this name was first seen. 2744 Location 2745 location() const 2746 { return this->location_; } 2747 2748 // Return the real named object that this points to, or NULL if it 2749 // was never resolved. 2750 Named_object* 2751 real_named_object() const 2752 { return this->real_named_object_; } 2753 2754 // Set the real named object that this points to. 2755 void 2756 set_real_named_object(Named_object* no); 2757 2758 private: 2759 // The location where this name was first seen. 2760 Location location_; 2761 // The real named object when it is known. 2762 Named_object* 2763 real_named_object_; 2764 }; 2765 2766 // A named object named. This is the result of a declaration. We 2767 // don't use a superclass because they all have to be handled 2768 // differently. 2769 2770 class Named_object 2771 { 2772 public: 2773 enum Classification 2774 { 2775 // An uninitialized Named_object. We should never see this. 2776 NAMED_OBJECT_UNINITIALIZED, 2777 // An erroneous name. This indicates a parse error, to avoid 2778 // later errors about undefined references. 2779 NAMED_OBJECT_ERRONEOUS, 2780 // An unknown name. This is used for forward references. In a 2781 // correct program, these will all be resolved by the end of the 2782 // parse. 2783 NAMED_OBJECT_UNKNOWN, 2784 // A const. 2785 NAMED_OBJECT_CONST, 2786 // A type. 2787 NAMED_OBJECT_TYPE, 2788 // A forward type declaration. 2789 NAMED_OBJECT_TYPE_DECLARATION, 2790 // A var. 2791 NAMED_OBJECT_VAR, 2792 // A result variable in a function. 2793 NAMED_OBJECT_RESULT_VAR, 2794 // The blank identifier--the special variable named _. 2795 NAMED_OBJECT_SINK, 2796 // A func. 2797 NAMED_OBJECT_FUNC, 2798 // A forward func declaration. 2799 NAMED_OBJECT_FUNC_DECLARATION, 2800 // A package. 2801 NAMED_OBJECT_PACKAGE 2802 }; 2803 2804 // Return the classification. 2805 Classification 2806 classification() const 2807 { return this->classification_; } 2808 2809 // Classifiers. 2810 2811 bool 2812 is_erroneous() const 2813 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; } 2814 2815 bool 2816 is_unknown() const 2817 { return this->classification_ == NAMED_OBJECT_UNKNOWN; } 2818 2819 bool 2820 is_const() const 2821 { return this->classification_ == NAMED_OBJECT_CONST; } 2822 2823 bool 2824 is_type() const 2825 { return this->classification_ == NAMED_OBJECT_TYPE; } 2826 2827 bool 2828 is_type_declaration() const 2829 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; } 2830 2831 bool 2832 is_variable() const 2833 { return this->classification_ == NAMED_OBJECT_VAR; } 2834 2835 bool 2836 is_result_variable() const 2837 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; } 2838 2839 bool 2840 is_sink() const 2841 { return this->classification_ == NAMED_OBJECT_SINK; } 2842 2843 bool 2844 is_function() const 2845 { return this->classification_ == NAMED_OBJECT_FUNC; } 2846 2847 bool 2848 is_function_declaration() const 2849 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; } 2850 2851 bool 2852 is_package() const 2853 { return this->classification_ == NAMED_OBJECT_PACKAGE; } 2854 2855 // Creators. 2856 2857 static Named_object* 2858 make_erroneous_name(const std::string& name) 2859 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); } 2860 2861 static Named_object* 2862 make_unknown_name(const std::string& name, Location); 2863 2864 static Named_object* 2865 make_constant(const Typed_identifier&, const Package*, Expression*, 2866 int iota_value); 2867 2868 static Named_object* 2869 make_type(const std::string&, const Package*, Type*, Location); 2870 2871 static Named_object* 2872 make_type_declaration(const std::string&, const Package*, Location); 2873 2874 static Named_object* 2875 make_variable(const std::string&, const Package*, Variable*); 2876 2877 static Named_object* 2878 make_result_variable(const std::string&, Result_variable*); 2879 2880 static Named_object* 2881 make_sink(); 2882 2883 static Named_object* 2884 make_function(const std::string&, const Package*, Function*); 2885 2886 static Named_object* 2887 make_function_declaration(const std::string&, const Package*, Function_type*, 2888 Location); 2889 2890 static Named_object* 2891 make_package(const std::string& alias, Package* package); 2892 2893 // Getters. 2894 2895 Unknown_name* 2896 unknown_value() 2897 { 2898 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN); 2899 return this->u_.unknown_value; 2900 } 2901 2902 const Unknown_name* 2903 unknown_value() const 2904 { 2905 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN); 2906 return this->u_.unknown_value; 2907 } 2908 2909 Named_constant* 2910 const_value() 2911 { 2912 go_assert(this->classification_ == NAMED_OBJECT_CONST); 2913 return this->u_.const_value; 2914 } 2915 2916 const Named_constant* 2917 const_value() const 2918 { 2919 go_assert(this->classification_ == NAMED_OBJECT_CONST); 2920 return this->u_.const_value; 2921 } 2922 2923 Named_type* 2924 type_value() 2925 { 2926 go_assert(this->classification_ == NAMED_OBJECT_TYPE); 2927 return this->u_.type_value; 2928 } 2929 2930 const Named_type* 2931 type_value() const 2932 { 2933 go_assert(this->classification_ == NAMED_OBJECT_TYPE); 2934 return this->u_.type_value; 2935 } 2936 2937 Type_declaration* 2938 type_declaration_value() 2939 { 2940 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION); 2941 return this->u_.type_declaration; 2942 } 2943 2944 const Type_declaration* 2945 type_declaration_value() const 2946 { 2947 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION); 2948 return this->u_.type_declaration; 2949 } 2950 2951 Variable* 2952 var_value() 2953 { 2954 go_assert(this->classification_ == NAMED_OBJECT_VAR); 2955 return this->u_.var_value; 2956 } 2957 2958 const Variable* 2959 var_value() const 2960 { 2961 go_assert(this->classification_ == NAMED_OBJECT_VAR); 2962 return this->u_.var_value; 2963 } 2964 2965 Result_variable* 2966 result_var_value() 2967 { 2968 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR); 2969 return this->u_.result_var_value; 2970 } 2971 2972 const Result_variable* 2973 result_var_value() const 2974 { 2975 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR); 2976 return this->u_.result_var_value; 2977 } 2978 2979 Function* 2980 func_value() 2981 { 2982 go_assert(this->classification_ == NAMED_OBJECT_FUNC); 2983 return this->u_.func_value; 2984 } 2985 2986 const Function* 2987 func_value() const 2988 { 2989 go_assert(this->classification_ == NAMED_OBJECT_FUNC); 2990 return this->u_.func_value; 2991 } 2992 2993 Function_declaration* 2994 func_declaration_value() 2995 { 2996 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION); 2997 return this->u_.func_declaration_value; 2998 } 2999 3000 const Function_declaration* 3001 func_declaration_value() const 3002 { 3003 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION); 3004 return this->u_.func_declaration_value; 3005 } 3006 3007 Package* 3008 package_value() 3009 { 3010 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE); 3011 return this->u_.package_value; 3012 } 3013 3014 const Package* 3015 package_value() const 3016 { 3017 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE); 3018 return this->u_.package_value; 3019 } 3020 3021 const std::string& 3022 name() const 3023 { return this->name_; } 3024 3025 // Return the name to use in an error message. The difference is 3026 // that if this Named_object is defined in a different package, this 3027 // will return PACKAGE.NAME. 3028 std::string 3029 message_name() const; 3030 3031 const Package* 3032 package() const 3033 { return this->package_; } 3034 3035 // Resolve an unknown value if possible. This returns the same 3036 // Named_object or a new one. 3037 Named_object* 3038 resolve() 3039 { 3040 Named_object* ret = this; 3041 if (this->is_unknown()) 3042 { 3043 Named_object* r = this->unknown_value()->real_named_object(); 3044 if (r != NULL) 3045 ret = r; 3046 } 3047 return ret; 3048 } 3049 3050 const Named_object* 3051 resolve() const 3052 { 3053 const Named_object* ret = this; 3054 if (this->is_unknown()) 3055 { 3056 const Named_object* r = this->unknown_value()->real_named_object(); 3057 if (r != NULL) 3058 ret = r; 3059 } 3060 return ret; 3061 } 3062 3063 // The location where this object was defined or referenced. 3064 Location 3065 location() const; 3066 3067 // Traverse a Named_object. 3068 int 3069 traverse(Traverse*, bool is_global); 3070 3071 // Convert a variable to the backend representation. 3072 Bvariable* 3073 get_backend_variable(Gogo*, Named_object* function); 3074 3075 // Get the backend representation of this object. 3076 void 3077 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&, 3078 std::vector<Bfunction*>&); 3079 3080 // Define a type declaration. 3081 void 3082 set_type_value(Named_type*); 3083 3084 // Define a function declaration. 3085 void 3086 set_function_value(Function*); 3087 3088 // Declare an unknown name as a type declaration. 3089 void 3090 declare_as_type(); 3091 3092 // Export this object. 3093 void 3094 export_named_object(Export*) const; 3095 3096 // Mark this named object as an invalid redefinition of another object. 3097 void 3098 set_is_redefinition() 3099 { this->is_redefinition_ = true; } 3100 3101 // Return whether or not this object is a invalid redefinition of another 3102 // object. 3103 bool 3104 is_redefinition() const 3105 { return this->is_redefinition_; } 3106 3107 private: 3108 Named_object(const std::string&, const Package*, Classification); 3109 3110 // The name of the object. 3111 std::string name_; 3112 // The package that this object is in. This is NULL if it is in the 3113 // file we are compiling. 3114 const Package* package_; 3115 // The type of object this is. 3116 Classification classification_; 3117 // The real data. 3118 union 3119 { 3120 Unknown_name* unknown_value; 3121 Named_constant* const_value; 3122 Named_type* type_value; 3123 Type_declaration* type_declaration; 3124 Variable* var_value; 3125 Result_variable* result_var_value; 3126 Function* func_value; 3127 Function_declaration* func_declaration_value; 3128 Package* package_value; 3129 } u_; 3130 // True if this object is an invalid redefinition of another object. 3131 bool is_redefinition_; 3132 }; 3133 3134 // A binding contour. This binds names to objects. 3135 3136 class Bindings 3137 { 3138 public: 3139 // Type for mapping from names to objects. 3140 typedef Unordered_map(std::string, Named_object*) Contour; 3141 3142 Bindings(Bindings* enclosing); 3143 3144 // Add an erroneous name. 3145 Named_object* 3146 add_erroneous_name(const std::string& name) 3147 { return this->add_named_object(Named_object::make_erroneous_name(name)); } 3148 3149 // Add an unknown name. 3150 Named_object* 3151 add_unknown_name(const std::string& name, Location location) 3152 { 3153 return this->add_named_object(Named_object::make_unknown_name(name, 3154 location)); 3155 } 3156 3157 // Add a constant. 3158 Named_object* 3159 add_constant(const Typed_identifier& tid, const Package* package, 3160 Expression* expr, int iota_value) 3161 { 3162 return this->add_named_object(Named_object::make_constant(tid, package, 3163 expr, 3164 iota_value)); 3165 } 3166 3167 // Add a type. 3168 Named_object* 3169 add_type(const std::string& name, const Package* package, Type* type, 3170 Location location) 3171 { 3172 return this->add_named_object(Named_object::make_type(name, package, type, 3173 location)); 3174 } 3175 3176 // Add a named type. This is used for builtin types, and to add an 3177 // imported type to the global scope. 3178 Named_object* 3179 add_named_type(Named_type* named_type); 3180 3181 // Add a type declaration. 3182 Named_object* 3183 add_type_declaration(const std::string& name, const Package* package, 3184 Location location) 3185 { 3186 Named_object* no = Named_object::make_type_declaration(name, package, 3187 location); 3188 return this->add_named_object(no); 3189 } 3190 3191 // Add a variable. 3192 Named_object* 3193 add_variable(const std::string& name, const Package* package, 3194 Variable* variable) 3195 { 3196 return this->add_named_object(Named_object::make_variable(name, package, 3197 variable)); 3198 } 3199 3200 // Add a result variable. 3201 Named_object* 3202 add_result_variable(const std::string& name, Result_variable* result) 3203 { 3204 return this->add_named_object(Named_object::make_result_variable(name, 3205 result)); 3206 } 3207 3208 // Add a function. 3209 Named_object* 3210 add_function(const std::string& name, const Package*, Function* function); 3211 3212 // Add a function declaration. 3213 Named_object* 3214 add_function_declaration(const std::string& name, const Package* package, 3215 Function_type* type, Location location); 3216 3217 // Add a package. The location is the location of the import 3218 // statement. 3219 Named_object* 3220 add_package(const std::string& alias, Package* package) 3221 { 3222 Named_object* no = Named_object::make_package(alias, package); 3223 return this->add_named_object(no); 3224 } 3225 3226 // Define a type which was already declared. 3227 void 3228 define_type(Named_object*, Named_type*); 3229 3230 // Add a method to the list of objects. This is not added to the 3231 // lookup table. 3232 void 3233 add_method(Named_object*); 3234 3235 // Add a named object to this binding. 3236 Named_object* 3237 add_named_object(Named_object* no) 3238 { return this->add_named_object_to_contour(&this->bindings_, no); } 3239 3240 // Clear all names in file scope from the bindings. 3241 void 3242 clear_file_scope(Gogo*); 3243 3244 // Look up a name in this binding contour and in any enclosing 3245 // binding contours. This returns NULL if the name is not found. 3246 Named_object* 3247 lookup(const std::string&) const; 3248 3249 // Look up a name in this binding contour without looking in any 3250 // enclosing binding contours. Returns NULL if the name is not found. 3251 Named_object* 3252 lookup_local(const std::string&) const; 3253 3254 // Remove a name. 3255 void 3256 remove_binding(Named_object*); 3257 3258 // Mark all variables as used. This is used for some types of parse 3259 // error. 3260 void 3261 mark_locals_used(); 3262 3263 // Traverse the tree. See the Traverse class. 3264 int 3265 traverse(Traverse*, bool is_global); 3266 3267 // Iterate over definitions. This does not include things which 3268 // were only declared. 3269 3270 typedef std::vector<Named_object*>::const_iterator 3271 const_definitions_iterator; 3272 3273 const_definitions_iterator 3274 begin_definitions() const 3275 { return this->named_objects_.begin(); } 3276 3277 const_definitions_iterator 3278 end_definitions() const 3279 { return this->named_objects_.end(); } 3280 3281 // Return the number of definitions. 3282 size_t 3283 size_definitions() const 3284 { return this->named_objects_.size(); } 3285 3286 // Return whether there are no definitions. 3287 bool 3288 empty_definitions() const 3289 { return this->named_objects_.empty(); } 3290 3291 // Iterate over declarations. This is everything that has been 3292 // declared, which includes everything which has been defined. 3293 3294 typedef Contour::const_iterator const_declarations_iterator; 3295 3296 const_declarations_iterator 3297 begin_declarations() const 3298 { return this->bindings_.begin(); } 3299 3300 const_declarations_iterator 3301 end_declarations() const 3302 { return this->bindings_.end(); } 3303 3304 // Return the number of declarations. 3305 size_t 3306 size_declarations() const 3307 { return this->bindings_.size(); } 3308 3309 // Return whether there are no declarations. 3310 bool 3311 empty_declarations() const 3312 { return this->bindings_.empty(); } 3313 3314 // Return the first declaration. 3315 Named_object* 3316 first_declaration() 3317 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; } 3318 3319 // Dump to stderr for debugging 3320 void debug_dump(); 3321 3322 private: 3323 Named_object* 3324 add_named_object_to_contour(Contour*, Named_object*); 3325 3326 Named_object* 3327 new_definition(Named_object*, Named_object*); 3328 3329 // Enclosing bindings. 3330 Bindings* enclosing_; 3331 // The list of objects. 3332 std::vector<Named_object*> named_objects_; 3333 // The mapping from names to objects. 3334 Contour bindings_; 3335 }; 3336 3337 // A label. 3338 3339 class Label 3340 { 3341 public: 3342 Label(const std::string& name) 3343 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL), 3344 refs_(), is_used_(false), blabel_(NULL), depth_(DEPTH_UNKNOWN) 3345 { } 3346 3347 // Return the label's name. 3348 const std::string& 3349 name() const 3350 { return this->name_; } 3351 3352 // Return whether the label has been defined. 3353 bool 3354 is_defined() const 3355 { return !Linemap::is_unknown_location(this->location_); } 3356 3357 // Return whether the label has been used. 3358 bool 3359 is_used() const 3360 { return this->is_used_; } 3361 3362 // Record that the label is used. 3363 void 3364 set_is_used() 3365 { this->is_used_ = true; } 3366 3367 // Return whether this label is looping. 3368 bool 3369 looping() const 3370 { return this->depth_ == DEPTH_LOOPING; } 3371 3372 // Set this label as looping. 3373 void 3374 set_looping() 3375 { this->depth_ = DEPTH_LOOPING; } 3376 3377 // Return whether this label is nonlooping. 3378 bool 3379 nonlooping() const 3380 { return this->depth_ == DEPTH_NONLOOPING; } 3381 3382 // Set this label as nonlooping. 3383 void 3384 set_nonlooping() 3385 { this->depth_ = DEPTH_NONLOOPING; } 3386 3387 // Return the location of the definition. 3388 Location 3389 location() const 3390 { return this->location_; } 3391 3392 // Return the bindings snapshot. 3393 Bindings_snapshot* 3394 snapshot() const 3395 { return this->snapshot_; } 3396 3397 // Add a snapshot of a goto which refers to this label. 3398 void 3399 add_snapshot_ref(Bindings_snapshot* snapshot) 3400 { 3401 go_assert(Linemap::is_unknown_location(this->location_)); 3402 this->refs_.push_back(snapshot); 3403 } 3404 3405 // Return the list of snapshots of goto statements which refer to 3406 // this label. 3407 const std::vector<Bindings_snapshot*>& 3408 refs() const 3409 { return this->refs_; } 3410 3411 // Clear the references. 3412 void 3413 clear_refs(); 3414 3415 // Define the label at LOCATION with the given bindings snapshot. 3416 void 3417 define(Location location, Bindings_snapshot* snapshot) 3418 { 3419 if (this->is_dummy_label()) 3420 return; 3421 go_assert(Linemap::is_unknown_location(this->location_) 3422 && this->snapshot_ == NULL); 3423 this->location_ = location; 3424 this->snapshot_ = snapshot; 3425 } 3426 3427 // Return the backend representation for this label. 3428 Blabel* 3429 get_backend_label(Translate_context*); 3430 3431 // Return an expression for the address of this label. This is used 3432 // to get the return address of a deferred function to see whether 3433 // the function may call recover. 3434 Bexpression* 3435 get_addr(Translate_context*, Location location); 3436 3437 // Return a dummy label, representing any instance of the blank label. 3438 static Label* 3439 create_dummy_label(); 3440 3441 // Return TRUE if this is a dummy label. 3442 bool 3443 is_dummy_label() const 3444 { return this->name_ == "_"; } 3445 3446 // A classification of a label's looping depth. 3447 enum Loop_depth 3448 { 3449 DEPTH_UNKNOWN, 3450 // A label never jumped to. 3451 DEPTH_NONLOOPING, 3452 // A label jumped to. 3453 DEPTH_LOOPING 3454 }; 3455 3456 private: 3457 // The name of the label. 3458 std::string name_; 3459 // The location of the definition. This is 0 if the label has not 3460 // yet been defined. 3461 Location location_; 3462 // A snapshot of the set of bindings defined at this label, used to 3463 // issue errors about invalid goto statements. 3464 Bindings_snapshot* snapshot_; 3465 // A list of snapshots of goto statements which refer to this label. 3466 std::vector<Bindings_snapshot*> refs_; 3467 // Whether the label has been used. 3468 bool is_used_; 3469 // The backend representation. 3470 Blabel* blabel_; 3471 // The looping depth of this label, for escape analysis. 3472 Loop_depth depth_; 3473 }; 3474 3475 // An unnamed label. These are used when lowering loops. 3476 3477 class Unnamed_label 3478 { 3479 public: 3480 Unnamed_label(Location location) 3481 : location_(location), derived_from_(NULL), blabel_(NULL) 3482 { } 3483 3484 // Get the location where the label is defined. 3485 Location 3486 location() const 3487 { return this->location_; } 3488 3489 // Set the location where the label is defined. 3490 void 3491 set_location(Location location) 3492 { this->location_ = location; } 3493 3494 // Get the top level statement this unnamed label is derived from. 3495 Statement* 3496 derived_from() const 3497 { return this->derived_from_; } 3498 3499 // Set the top level statement this unnamed label is derived from. 3500 void 3501 set_derived_from(Statement* s) 3502 { this->derived_from_ = s; } 3503 3504 // Return a statement which defines this label. 3505 Bstatement* 3506 get_definition(Translate_context*); 3507 3508 // Return a goto to this label from LOCATION. 3509 Bstatement* 3510 get_goto(Translate_context*, Location location); 3511 3512 private: 3513 // Return the backend representation. 3514 Blabel* 3515 get_blabel(Translate_context*); 3516 3517 // The location where the label is defined. 3518 Location location_; 3519 // The top-level statement this unnamed label was derived/lowered from. 3520 // This is NULL is this label is not the top-level of a lowered statement. 3521 Statement* derived_from_; 3522 // The backend representation of this label. 3523 Blabel* blabel_; 3524 }; 3525 3526 // An alias for an imported package. 3527 3528 class Package_alias 3529 { 3530 public: 3531 Package_alias(Location location) 3532 : location_(location), used_(0) 3533 { } 3534 3535 // The location of the import statement. 3536 Location 3537 location() 3538 { return this->location_; } 3539 3540 // How many symbols from the package were used under this alias. 3541 size_t 3542 used() const 3543 { return this->used_; } 3544 3545 // Note that some symbol was used under this alias. 3546 void 3547 note_usage() 3548 { this->used_++; } 3549 3550 private: 3551 // The location of the import statement. 3552 Location location_; 3553 // The amount of times some name from this package was used under this alias. 3554 size_t used_; 3555 }; 3556 3557 // An imported package. 3558 3559 class Package 3560 { 3561 public: 3562 Package(const std::string& pkgpath, const std::string& pkgpath_symbol, 3563 Location location); 3564 3565 // Get the package path used for all symbols exported from this 3566 // package. 3567 const std::string& 3568 pkgpath() const 3569 { return this->pkgpath_; } 3570 3571 // Return the package path to use for a symbol name. 3572 std::string 3573 pkgpath_symbol() const; 3574 3575 // Set the package path symbol. 3576 void 3577 set_pkgpath_symbol(const std::string&); 3578 3579 // Return the location of the most recent import statement. 3580 Location 3581 location() const 3582 { return this->location_; } 3583 3584 // Return whether we know the name of this package yet. 3585 bool 3586 has_package_name() const 3587 { return !this->package_name_.empty(); } 3588 3589 // The name that this package uses in its package clause. This may 3590 // be different from the name in the associated Named_object if the 3591 // import statement used an alias. 3592 const std::string& 3593 package_name() const 3594 { 3595 go_assert(!this->package_name_.empty()); 3596 return this->package_name_; 3597 } 3598 3599 // Return the bindings. 3600 Bindings* 3601 bindings() const 3602 { return this->bindings_; } 3603 3604 // Type used to map import names to package aliases. 3605 typedef std::map<std::string, Package_alias*> Aliases; 3606 3607 // Return the set of package aliases. 3608 const Aliases& 3609 aliases() const 3610 { return this->aliases_; } 3611 3612 // Note that some symbol from this package was used and qualified by ALIAS. 3613 // For dot imports, the ALIAS should be ".PACKAGE_NAME". 3614 void 3615 note_usage(const std::string& alias) const; 3616 3617 // Note that USAGE might be a fake usage of this package. 3618 void 3619 note_fake_usage(Expression* usage) const 3620 { this->fake_uses_.insert(usage); } 3621 3622 // Forget a given USAGE of this package. 3623 void 3624 forget_usage(Expression* usage) const; 3625 3626 // Clear the used field for the next file. 3627 void 3628 clear_used(); 3629 3630 // Look up a name in the package. Returns NULL if the name is not 3631 // found. 3632 Named_object* 3633 lookup(const std::string& name) const 3634 { return this->bindings_->lookup(name); } 3635 3636 // Set the name of the package. 3637 void 3638 set_package_name(const std::string& name, Location); 3639 3640 // Set the location of the package. This is used to record the most 3641 // recent import location. 3642 void 3643 set_location(Location location) 3644 { this->location_ = location; } 3645 3646 // Add a package name as an ALIAS for this package. 3647 Package_alias* 3648 add_alias(const std::string& alias, Location); 3649 3650 // Add a constant to the package. 3651 Named_object* 3652 add_constant(const Typed_identifier& tid, Expression* expr) 3653 { return this->bindings_->add_constant(tid, this, expr, 0); } 3654 3655 // Add a type to the package. 3656 Named_object* 3657 add_type(const std::string& name, Type* type, Location location) 3658 { return this->bindings_->add_type(name, this, type, location); } 3659 3660 // Add a type declaration to the package. 3661 Named_object* 3662 add_type_declaration(const std::string& name, Location location) 3663 { return this->bindings_->add_type_declaration(name, this, location); } 3664 3665 // Add a variable to the package. 3666 Named_object* 3667 add_variable(const std::string& name, Variable* variable) 3668 { return this->bindings_->add_variable(name, this, variable); } 3669 3670 // Add a function declaration to the package. 3671 Named_object* 3672 add_function_declaration(const std::string& name, Function_type* type, 3673 Location loc) 3674 { return this->bindings_->add_function_declaration(name, this, type, loc); } 3675 3676 // Determine types of constants. 3677 void 3678 determine_types(); 3679 3680 private: 3681 // The package path for type reflection data. 3682 std::string pkgpath_; 3683 // The package path for symbol names. 3684 std::string pkgpath_symbol_; 3685 // The name that this package uses in the package clause. This may 3686 // be the empty string if it is not yet known. 3687 std::string package_name_; 3688 // The names in this package. 3689 Bindings* bindings_; 3690 // The location of the most recent import statement. 3691 Location location_; 3692 // The set of aliases associated with this package. 3693 Aliases aliases_; 3694 // A set of possibly fake uses of this package. This is mutable because we 3695 // can track fake uses of a package even if we have a const pointer to it. 3696 mutable std::set<Expression*> fake_uses_; 3697 }; 3698 3699 // Return codes for the traversal functions. This is not an enum 3700 // because we want to be able to declare traversal functions in other 3701 // header files without including this one. 3702 3703 // Continue traversal as usual. 3704 const int TRAVERSE_CONTINUE = -1; 3705 3706 // Exit traversal. 3707 const int TRAVERSE_EXIT = 0; 3708 3709 // Continue traversal, but skip components of the current object. 3710 // E.g., if this is returned by Traverse::statement, we do not 3711 // traverse the expressions in the statement even if 3712 // traverse_expressions is set in the traverse_mask. 3713 const int TRAVERSE_SKIP_COMPONENTS = 1; 3714 3715 // This class is used when traversing the parse tree. The caller uses 3716 // a subclass which overrides functions as desired. 3717 3718 class Traverse 3719 { 3720 public: 3721 // These bitmasks say what to traverse. 3722 static const unsigned int traverse_variables = 0x1; 3723 static const unsigned int traverse_constants = 0x2; 3724 static const unsigned int traverse_functions = 0x4; 3725 static const unsigned int traverse_blocks = 0x8; 3726 static const unsigned int traverse_statements = 0x10; 3727 static const unsigned int traverse_expressions = 0x20; 3728 static const unsigned int traverse_types = 0x40; 3729 static const unsigned int traverse_func_declarations = 0x80; 3730 3731 Traverse(unsigned int traverse_mask) 3732 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL) 3733 { } 3734 3735 virtual ~Traverse(); 3736 3737 // The bitmask of what to traverse. 3738 unsigned int 3739 traverse_mask() const 3740 { return this->traverse_mask_; } 3741 3742 // Record that we are going to traverse a type. This returns true 3743 // if the type has already been seen in this traversal. This is 3744 // required because types, unlike expressions, can form a circular 3745 // graph. 3746 bool 3747 remember_type(const Type*); 3748 3749 // Record that we are going to see an expression. This returns true 3750 // if the expression has already been seen in this traversal. This 3751 // is only needed for cases where multiple expressions can point to 3752 // a single one. 3753 bool 3754 remember_expression(const Expression*); 3755 3756 // These functions return one of the TRAVERSE codes defined above. 3757 3758 // If traverse_variables is set in the mask, this is called for 3759 // every variable in the tree. 3760 virtual int 3761 variable(Named_object*); 3762 3763 // If traverse_constants is set in the mask, this is called for 3764 // every named constant in the tree. The bool parameter is true for 3765 // a global constant. 3766 virtual int 3767 constant(Named_object*, bool); 3768 3769 // If traverse_functions is set in the mask, this is called for 3770 // every function in the tree. 3771 virtual int 3772 function(Named_object*); 3773 3774 // If traverse_blocks is set in the mask, this is called for every 3775 // block in the tree. 3776 virtual int 3777 block(Block*); 3778 3779 // If traverse_statements is set in the mask, this is called for 3780 // every statement in the tree. 3781 virtual int 3782 statement(Block*, size_t* index, Statement*); 3783 3784 // If traverse_expressions is set in the mask, this is called for 3785 // every expression in the tree. 3786 virtual int 3787 expression(Expression**); 3788 3789 // If traverse_types is set in the mask, this is called for every 3790 // type in the tree. 3791 virtual int 3792 type(Type*); 3793 3794 // If traverse_func_declarations is set in the mask, this is called 3795 // for every function declarations in the tree. 3796 virtual int 3797 function_declaration(Named_object*); 3798 3799 private: 3800 // A hash table for types we have seen during this traversal. Note 3801 // that this uses the default hash functions for pointers rather 3802 // than Type_hash_identical and Type_identical. This is because for 3803 // traversal we care about seeing a specific type structure. If 3804 // there are two separate instances of identical types, we want to 3805 // traverse both. 3806 typedef Unordered_set(const Type*) Types_seen; 3807 3808 typedef Unordered_set(const Expression*) Expressions_seen; 3809 3810 // Bitmask of what sort of objects to traverse. 3811 unsigned int traverse_mask_; 3812 // Types which have been seen in this traversal. 3813 Types_seen* types_seen_; 3814 // Expressions which have been seen in this traversal. 3815 Expressions_seen* expressions_seen_; 3816 }; 3817 3818 // This class looks for interface types to finalize methods of inherited 3819 // interfaces. 3820 3821 class Finalize_methods : public Traverse 3822 { 3823 public: 3824 Finalize_methods(Gogo* gogo) 3825 : Traverse(traverse_types), 3826 gogo_(gogo) 3827 { } 3828 3829 int 3830 type(Type*); 3831 3832 private: 3833 Gogo* gogo_; 3834 }; 3835 3836 // A class which makes it easier to insert new statements before the 3837 // current statement during a traversal. 3838 3839 class Statement_inserter 3840 { 3841 public: 3842 typedef Unordered_set(Statement*) Statements; 3843 3844 // Empty constructor. 3845 Statement_inserter() 3846 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL), 3847 statements_added_(NULL) 3848 { } 3849 3850 // Constructor for a statement in a block. 3851 Statement_inserter(Block* block, size_t *pindex, Statements *added = NULL) 3852 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL), 3853 statements_added_(added) 3854 { } 3855 3856 // Constructor for a global variable. 3857 Statement_inserter(Gogo* gogo, Variable* var, Statements *added = NULL) 3858 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var), 3859 statements_added_(added) 3860 { go_assert(var->is_global()); } 3861 3862 // We use the default copy constructor and assignment operator. 3863 3864 // Insert S before the statement we are traversing, or before the 3865 // initialization expression of a global variable. 3866 void 3867 insert(Statement* s); 3868 3869 private: 3870 // The block that the statement is in. 3871 Block* block_; 3872 // The index of the statement that we are traversing. 3873 size_t* pindex_; 3874 // The IR, needed when looking at an initializer expression for a 3875 // global variable. 3876 Gogo* gogo_; 3877 // The global variable, when looking at an initializer expression. 3878 Variable* var_; 3879 // If non-null, a set to record new statements inserted (non-owned). 3880 Statements* statements_added_; 3881 }; 3882 3883 // When translating the gogo IR into the backend data structure, this 3884 // is the context we pass down the blocks and statements. 3885 3886 class Translate_context 3887 { 3888 public: 3889 Translate_context(Gogo* gogo, Named_object* function, Block* block, 3890 Bblock* bblock) 3891 : gogo_(gogo), backend_(gogo->backend()), function_(function), 3892 block_(block), bblock_(bblock), is_const_(false) 3893 { } 3894 3895 // Accessors. 3896 3897 Gogo* 3898 gogo() 3899 { return this->gogo_; } 3900 3901 Backend* 3902 backend() 3903 { return this->backend_; } 3904 3905 Named_object* 3906 function() 3907 { return this->function_; } 3908 3909 Block* 3910 block() 3911 { return this->block_; } 3912 3913 Bblock* 3914 bblock() 3915 { return this->bblock_; } 3916 3917 bool 3918 is_const() 3919 { return this->is_const_; } 3920 3921 // Make a constant context. 3922 void 3923 set_is_const() 3924 { this->is_const_ = true; } 3925 3926 private: 3927 // The IR for the entire compilation unit. 3928 Gogo* gogo_; 3929 // The generator for the backend data structures. 3930 Backend* backend_; 3931 // The function we are currently translating. NULL if not in a 3932 // function, e.g., the initializer of a global variable. 3933 Named_object* function_; 3934 // The block we are currently translating. NULL if not in a 3935 // function. 3936 Block *block_; 3937 // The backend representation of the current block. NULL if block_ 3938 // is NULL. 3939 Bblock* bblock_; 3940 // Whether this is being evaluated in a constant context. This is 3941 // used for type descriptor initializers. 3942 bool is_const_; 3943 }; 3944 3945 // This is used by some of the langhooks. 3946 extern Gogo* go_get_gogo(); 3947 3948 // Whether we have seen any errors. FIXME: Replace with a backend 3949 // interface. 3950 extern bool saw_errors(); 3951 3952 // For use in the debugger 3953 extern void debug_go_gogo(Gogo*); 3954 extern void debug_go_named_object(Named_object*); 3955 extern void debug_go_bindings(Bindings*); 3956 3957 3958 #endif // !defined(GO_GOGO_H)