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 12class Traverse; 13class Statement_inserter; 14class Type; 15class Type_hash_identical; 16class Type_equal; 17class Type_identical; 18class Typed_identifier; 19class Typed_identifier_list; 20class Function_type; 21class Expression; 22class Statement; 23class Temporary_statement; 24class Block; 25class Function; 26class Bindings; 27class Bindings_snapshot; 28class Package; 29class Variable; 30class Pointer_type; 31class Struct_type; 32class Struct_field; 33class Struct_field_list; 34class Array_type; 35class Map_type; 36class Channel_type; 37class Interface_type; 38class Named_type; 39class Forward_declaration_type; 40class Named_object; 41class Label; 42class Translate_context; 43class Backend; 44class Export; 45class Import; 46class Bexpression; 47class Btype; 48class Bstatement; 49class Bblock; 50class Bvariable; 51class Blabel; 52class Bfunction; 53 54// This file declares the basic classes used to hold the internal 55// representation of Go which is built by the parser. 56 57// An initialization function for an imported package. This is a 58// magic function which initializes variables and runs the "init" 59// function. 60 61class Import_init 62{ 63 public: 64 Import_init(const std::string& package_name, const std::string& init_name, 65 int priority) 66 : package_name_(package_name), init_name_(init_name), priority_(priority) 67 { } 68 69 // The name of the package being imported. 70 const std::string& 71 package_name() const 72 { return this->package_name_; } 73 74 // The name of the package's init function. 75 const std::string& 76 init_name() const 77 { return this->init_name_; } 78 79 // The priority of the initialization function. Functions with a 80 // lower priority number must be run first. 81 int 82 priority() const 83 { return this->priority_; } 84 85 private: 86 // The name of the package being imported. 87 std::string package_name_; 88 // The name of the package's init function. 89 std::string init_name_; 90 // The priority. 91 int priority_; 92}; 93 94// For sorting purposes. 95 96inline bool 97operator<(const Import_init& i1, const Import_init& i2) 98{ 99 if (i1.priority() < i2.priority()) 100 return true; 101 if (i1.priority() > i2.priority()) 102 return false; 103 if (i1.package_name() != i2.package_name()) 104 return i1.package_name() < i2.package_name(); 105 return i1.init_name() < i2.init_name(); 106} 107 108// The holder for the internal representation of the entire 109// compilation unit. 110 111class Gogo 112{ 113 public: 114 // Create the IR, passing in the sizes of the types "int" and 115 // "uintptr" in bits. 116 Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size); 117 118 // Get the backend generator. 119 Backend* 120 backend() 121 { return this->backend_; } 122 123 // Get the Location generator. 124 Linemap* 125 linemap() 126 { return this->linemap_; } 127 128 // Get the package name. 129 const std::string& 130 package_name() const; 131 132 // Set the package name. 133 void 134 set_package_name(const std::string&, Location); 135 136 // Return whether this is the "main" package. 137 bool 138 is_main_package() const; 139 140 // If necessary, adjust the name to use for a hidden symbol. We add 141 // the package name, so that hidden symbols in different packages do 142 // not collide. 143 std::string 144 pack_hidden_name(const std::string& name, bool is_exported) const 145 { 146 return (is_exported 147 ? name 148 : '.' + this->pkgpath() + '.' + name); 149 } 150 151 // Unpack a name which may have been hidden. Returns the 152 // user-visible name of the object. 153 static std::string 154 unpack_hidden_name(const std::string& name) 155 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); } 156 157 // Return whether a possibly packed name is hidden. 158 static bool 159 is_hidden_name(const std::string& name) 160 { return name[0] == '.'; } 161 162 // Return the package path of a hidden name. 163 static std::string 164 hidden_name_pkgpath(const std::string& name) 165 { 166 go_assert(Gogo::is_hidden_name(name)); 167 return name.substr(1, name.rfind('.') - 1); 168 } 169 170 // Given a name which may or may not have been hidden, return the 171 // name to use in an error message. 172 static std::string 173 message_name(const std::string& name); 174 175 // Return whether a name is the blank identifier _. 176 static bool 177 is_sink_name(const std::string& name) 178 { 179 return (name[0] == '.' 180 && name[name.length() - 1] == '_' 181 && name[name.length() - 2] == '.'); 182 } 183 184 // Convert a pkgpath into a string suitable for a symbol 185 static std::string 186 pkgpath_for_symbol(const std::string& pkgpath); 187 188 // Return the package path to use for reflect.Type.PkgPath. 189 const std::string& 190 pkgpath() const; 191 192 // Return the package path to use for a symbol name. 193 const std::string& 194 pkgpath_symbol() const; 195 196 // Set the package path from a command line option. 197 void 198 set_pkgpath(const std::string&); 199 200 // Set the prefix from a command line option. 201 void 202 set_prefix(const std::string&); 203 204 // Return whether pkgpath was set from a command line option. 205 bool 206 pkgpath_from_option() const 207 { return this->pkgpath_from_option_; } 208 209 // Return the relative import path as set from the command line. 210 // Returns an empty string if it was not set. 211 const std::string& 212 relative_import_path() const 213 { return this->relative_import_path_; } 214 215 // Set the relative import path from a command line option. 216 void 217 set_relative_import_path(const std::string& s) 218 { this->relative_import_path_ = s; } 219 220 // Return whether to check for division by zero in binary operations. 221 bool 222 check_divide_by_zero() const 223 { return this->check_divide_by_zero_; } 224 225 // Set the option to check division by zero from a command line option. 226 void 227 set_check_divide_by_zero(bool b) 228 { this->check_divide_by_zero_ = b; } 229 230 // Return whether to check for division overflow in binary operations. 231 bool 232 check_divide_overflow() const 233 { return this->check_divide_overflow_; } 234 235 // Set the option to check division overflow from a command line option. 236 void 237 set_check_divide_overflow(bool b) 238 { this->check_divide_overflow_ = b; } 239 240 // Return the priority to use for the package we are compiling. 241 // This is two more than the largest priority of any package we 242 // import. 243 int 244 package_priority() const; 245 246 // Import a package. FILENAME is the file name argument, LOCAL_NAME 247 // is the local name to give to the package. If LOCAL_NAME is empty 248 // the declarations are added to the global scope. 249 void 250 import_package(const std::string& filename, const std::string& local_name, 251 bool is_local_name_exported, Location); 252 253 // Whether we are the global binding level. 254 bool 255 in_global_scope() const; 256 257 // Look up a name in the current binding contours. 258 Named_object* 259 lookup(const std::string&, Named_object** pfunction) const; 260 261 // Look up a name in the current block. 262 Named_object* 263 lookup_in_block(const std::string&) const; 264 265 // Look up a name in the global namespace--the universal scope. 266 Named_object* 267 lookup_global(const char*) const; 268 269 // Add a new imported package. REAL_NAME is the real name of the 270 // package. ALIAS is the alias of the package; this may be the same 271 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to 272 // this package should be added to the global namespace; this is 273 // true if the alias is ".". LOCATION is the location of the import 274 // statement. This returns the new package, or NULL on error. 275 Package* 276 add_imported_package(const std::string& real_name, const std::string& alias, 277 bool is_alias_exported, 278 const std::string& pkgpath, 279 const std::string& pkgpath_symbol, 280 Location location, 281 bool* padd_to_globals); 282 283 // Register a package. This package may or may not be imported. 284 // This returns the Package structure for the package, creating if 285 // it necessary. 286 Package* 287 register_package(const std::string& pkgpath, 288 const std::string& pkgpath_symbol, Location); 289 290 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a 291 // method function should be added to the type of its receiver. 292 Named_object* 293 start_function(const std::string& name, Function_type* type, 294 bool add_method_to_type, Location); 295 296 // Finish compiling a function. 297 void 298 finish_function(Location); 299 300 // Return the current function. 301 Named_object* 302 current_function() const; 303 304 // Return the current block. 305 Block* 306 current_block(); 307 308 // Start a new block. This is not initially associated with a 309 // function. 310 void 311 start_block(Location); 312 313 // Finish the current block and return it. 314 Block* 315 finish_block(Location); 316 317 // Declare an erroneous name. This is used to avoid knock-on errors 318 // after a parsing error. 319 Named_object* 320 add_erroneous_name(const std::string& name); 321 322 // Declare an unknown name. This is used while parsing. The name 323 // must be resolved by the end of the parse. Unknown names are 324 // always added at the package level. 325 Named_object* 326 add_unknown_name(const std::string& name, Location); 327 328 // Declare a function. 329 Named_object* 330 declare_function(const std::string&, Function_type*, Location); 331 332 // Declare a function at the package level. This is used for 333 // functions generated for a type. 334 Named_object* 335 declare_package_function(const std::string&, Function_type*, Location); 336 337 // Add a label. 338 Label* 339 add_label_definition(const std::string&, Location); 340 341 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should 342 // report errors for a goto from the current location to the label 343 // location. 344 Label* 345 add_label_reference(const std::string&, Location, 346 bool issue_goto_errors); 347 348 // Return a snapshot of the current binding state. 349 Bindings_snapshot* 350 bindings_snapshot(Location); 351 352 // Add a statement to the current block. 353 void 354 add_statement(Statement*); 355 356 // Add a block to the current block. 357 void 358 add_block(Block*, Location); 359 360 // Add a constant. 361 Named_object* 362 add_constant(const Typed_identifier&, Expression*, int iota_value); 363 364 // Add a type. 365 void 366 add_type(const std::string&, Type*, Location); 367 368 // Add a named type. This is used for builtin types, and to add an 369 // imported type to the global scope. 370 void 371 add_named_type(Named_type*); 372 373 // Declare a type. 374 Named_object* 375 declare_type(const std::string&, Location); 376 377 // Declare a type at the package level. This is used when the 378 // parser sees an unknown name where a type name is required. 379 Named_object* 380 declare_package_type(const std::string&, Location); 381 382 // Define a type which was already declared. 383 void 384 define_type(Named_object*, Named_type*); 385 386 // Add a variable. 387 Named_object* 388 add_variable(const std::string&, Variable*); 389 390 // Add a sink--a reference to the blank identifier _. 391 Named_object* 392 add_sink(); 393 394 // Add a type which needs to be verified. This is used for sink 395 // types, just to give appropriate error messages. 396 void 397 add_type_to_verify(Type* type); 398 399 // Add a named object to the current namespace. This is used for 400 // import . "package". 401 void 402 add_dot_import_object(Named_object*); 403 404 // Add an identifier to the list of names seen in the file block. 405 void 406 add_file_block_name(const std::string& name, Location location) 407 { this->file_block_names_[name] = location; } 408 409 // Mark all local variables in current bindings as used. This is 410 // used when there is a parse error to avoid useless errors. 411 void 412 mark_locals_used(); 413 414 // Return a name to use for an error case. This should only be used 415 // after reporting an error, and is used to avoid useless knockon 416 // errors. 417 static std::string 418 erroneous_name(); 419 420 // Return whether the name indicates an error. 421 static bool 422 is_erroneous_name(const std::string&); 423 424 // Return a name to use for a thunk function. A thunk function is 425 // one we create during the compilation, for a go statement or a 426 // defer statement or a method expression. 427 static std::string 428 thunk_name(); 429 430 // Return whether an object is a thunk. 431 static bool 432 is_thunk(const Named_object*); 433 434 // Note that we've seen an interface type. This is used to build 435 // all required interface method tables. 436 void 437 record_interface_type(Interface_type*); 438 439 // Note that we need an initialization function. 440 void 441 set_need_init_fn() 442 { this->need_init_fn_ = true; } 443 444 // Clear out all names in file scope. This is called when we start 445 // parsing a new file. 446 void 447 clear_file_scope(); 448 449 // Record that VAR1 must be initialized after VAR2. This is used 450 // when VAR2 does not appear in VAR1's INIT or PREINIT. 451 void 452 record_var_depends_on(Variable* var1, Named_object* var2) 453 { 454 go_assert(this->var_deps_.find(var1) == this->var_deps_.end()); 455 this->var_deps_[var1] = var2; 456 } 457 458 // Return the variable that VAR depends on, or NULL if none. 459 Named_object* 460 var_depends_on(Variable* var) const 461 { 462 Var_deps::const_iterator p = this->var_deps_.find(var); 463 return p != this->var_deps_.end() ? p->second : NULL; 464 } 465 466 // Queue up a type-specific function to be written out. This is 467 // used when a type-specific function is needed when not at the top 468 // level. 469 void 470 queue_specific_type_function(Type* type, Named_type* name, 471 const std::string& hash_name, 472 Function_type* hash_fntype, 473 const std::string& equal_name, 474 Function_type* equal_fntype); 475 476 // Write out queued specific type functions. 477 void 478 write_specific_type_functions(); 479 480 // Whether we are done writing out specific type functions. 481 bool 482 specific_type_functions_are_written() const 483 { return this->specific_type_functions_are_written_; } 484 485 // Traverse the tree. See the Traverse class. 486 void 487 traverse(Traverse*); 488 489 // Define the predeclared global names. 490 void 491 define_global_names(); 492 493 // Verify and complete all types. 494 void 495 verify_types(); 496 497 // Lower the parse tree. 498 void 499 lower_parse_tree(); 500 501 // Lower all the statements in a block. 502 void 503 lower_block(Named_object* function, Block*); 504 505 // Lower an expression. 506 void 507 lower_expression(Named_object* function, Statement_inserter*, Expression**); 508 509 // Lower a constant. 510 void 511 lower_constant(Named_object*); 512 513 // Flatten all the statements in a block. 514 void 515 flatten_block(Named_object* function, Block*); 516 517 // Flatten an expression. 518 void 519 flatten_expression(Named_object* function, Statement_inserter*, Expression**); 520 521 // Create all necessary function descriptors. 522 void 523 create_function_descriptors(); 524 525 // Finalize the method lists and build stub methods for named types. 526 void 527 finalize_methods(); 528 529 // Work out the types to use for unspecified variables and 530 // constants. 531 void 532 determine_types(); 533 534 // Type check the program. 535 void 536 check_types(); 537 538 // Check the types in a single block. This is used for complicated 539 // go statements. 540 void 541 check_types_in_block(Block*); 542 543 // Check for return statements. 544 void 545 check_return_statements(); 546 547 // Do all exports. 548 void 549 do_exports(); 550 551 // Add an import control function for an imported package to the 552 // list. 553 void 554 add_import_init_fn(const std::string& package_name, 555 const std::string& init_name, int prio); 556 557 // Turn short-cut operators (&&, ||) into explicit if statements. 558 void 559 remove_shortcuts(); 560 561 // Use temporary variables to force order of evaluation. 562 void 563 order_evaluations(); 564 565 // Flatten parse tree. 566 void 567 flatten(); 568 569 // Build thunks for functions which call recover. 570 void 571 build_recover_thunks(); 572 573 // Simplify statements which might use thunks: go and defer 574 // statements. 575 void 576 simplify_thunk_statements(); 577 578 // Dump AST if -fgo-dump-ast is set 579 void 580 dump_ast(const char* basename); 581 582 // Convert named types to the backend representation. 583 void 584 convert_named_types(); 585 586 // Convert named types in a list of bindings. 587 void 588 convert_named_types_in_bindings(Bindings*); 589 590 // True if named types have been converted to the backend 591 // representation. 592 bool 593 named_types_are_converted() const 594 { return this->named_types_are_converted_; } 595 596 // Return the variable to use for the zero value of TYPE. All types 597 // shared the same zero value, and we make sure that it is large 598 // enough. 599 Named_object* 600 zero_value(Type *type); 601 602 // Return whether a variable is the zero value variable. 603 bool 604 is_zero_value(Variable* v) const; 605 606 // Create the zero value variable. 607 Bvariable* 608 backend_zero_value(); 609 610 // Write out the global values. 611 void 612 write_globals(); 613 614 // Build a call to the runtime error function. 615 Expression* 616 runtime_error(int code, Location); 617 618 // Build required interface method tables. 619 void 620 build_interface_method_tables(); 621 622 // Return an expression which allocates memory to hold values of type TYPE. 623 Expression* 624 allocate_memory(Type *type, Location); 625 626 // Get the name of the magic initialization function. 627 const std::string& 628 get_init_fn_name(); 629 630 private: 631 // During parsing, we keep a stack of functions. Each function on 632 // the stack is one that we are currently parsing. For each 633 // function, we keep track of the current stack of blocks. 634 struct Open_function 635 { 636 // The function. 637 Named_object* function; 638 // The stack of active blocks in the function. 639 std::vector<Block*> blocks; 640 }; 641 642 // The stack of functions. 643 typedef std::vector<Open_function> Open_functions; 644 645 // Set up the built-in unsafe package. 646 void 647 import_unsafe(const std::string&, bool is_exported, Location); 648 649 // Return the current binding contour. 650 Bindings* 651 current_bindings(); 652 653 const Bindings* 654 current_bindings() const; 655 656 // Get the decl for the magic initialization function. 657 Named_object* 658 initialization_function_decl(); 659 660 // Create the magic initialization function. 661 Named_object* 662 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt); 663 664 // Initialize imported packages. 665 void 666 init_imports(std::vector<Bstatement*>&); 667 668 // Register variables with the garbage collector. 669 void 670 register_gc_vars(const std::vector<Named_object*>&, 671 std::vector<Bstatement*>&); 672 673 // Type used to map import names to packages. 674 typedef std::map<std::string, Package*> Imports; 675 676 // Type used to map package names to packages. 677 typedef std::map<std::string, Package*> Packages; 678 679 // Type used to map variables to the function calls that set them. 680 // This is used for initialization dependency analysis. 681 typedef std::map<Variable*, Named_object*> Var_deps; 682 683 // Type used to map identifiers in the file block to the location 684 // where they were defined. 685 typedef Unordered_map(std::string, Location) File_block_names; 686 687 // Type used to queue writing a type specific function. 688 struct Specific_type_function 689 { 690 Type* type; 691 Named_type* name; 692 std::string hash_name; 693 Function_type* hash_fntype; 694 std::string equal_name; 695 Function_type* equal_fntype; 696 697 Specific_type_function(Type* atype, Named_type* aname, 698 const std::string& ahash_name, 699 Function_type* ahash_fntype, 700 const std::string& aequal_name, 701 Function_type* aequal_fntype) 702 : type(atype), name(aname), hash_name(ahash_name), 703 hash_fntype(ahash_fntype), equal_name(aequal_name), 704 equal_fntype(aequal_fntype) 705 { } 706 }; 707 708 // The backend generator. 709 Backend* backend_; 710 // The object used to keep track of file names and line numbers. 711 Linemap* linemap_; 712 // The package we are compiling. 713 Package* package_; 714 // The list of currently open functions during parsing. 715 Open_functions functions_; 716 // The global binding contour. This includes the builtin functions 717 // and the package we are compiling. 718 Bindings* globals_; 719 // The list of names we have seen in the file block. 720 File_block_names file_block_names_; 721 // Mapping from import file names to packages. 722 Imports imports_; 723 // Whether the magic unsafe package was imported. 724 bool imported_unsafe_; 725 // Mapping from package names we have seen to packages. This does 726 // not include the package we are compiling. 727 Packages packages_; 728 // The functions named "init", if there are any. 729 std::vector<Named_object*> init_functions_; 730 // A mapping from variables to the function calls that initialize 731 // them, if it is not stored in the variable's init or preinit. 732 // This is used for dependency analysis. 733 Var_deps var_deps_; 734 // Whether we need a magic initialization function. 735 bool need_init_fn_; 736 // The name of the magic initialization function. 737 std::string init_fn_name_; 738 // A list of import control variables for packages that we import. 739 std::set<Import_init> imported_init_fns_; 740 // The package path used for reflection data. 741 std::string pkgpath_; 742 // The package path to use for a symbol name. 743 std::string pkgpath_symbol_; 744 // The prefix to use for symbols, from the -fgo-prefix option. 745 std::string prefix_; 746 // The special zero value variable. 747 Named_object* zero_value_; 748 // The size of the zero value variable. 749 int64_t zero_value_size_; 750 // The alignment of the zero value variable, in bytes. 751 int64_t zero_value_align_; 752 // Whether pkgpath_ has been set. 753 bool pkgpath_set_; 754 // Whether an explicit package path was set by -fgo-pkgpath. 755 bool pkgpath_from_option_; 756 // Whether an explicit prefix was set by -fgo-prefix. 757 bool prefix_from_option_; 758 // The relative import path, from the -fgo-relative-import-path 759 // option. 760 std::string relative_import_path_; 761 // Whether or not to check for division by zero, from the 762 // -fgo-check-divide-zero option. 763 bool check_divide_by_zero_; 764 // Whether or not to check for division overflow, from the 765 // -fgo-check-divide-overflow option. 766 bool check_divide_overflow_; 767 // A list of types to verify. 768 std::vector<Type*> verify_types_; 769 // A list of interface types defined while parsing. 770 std::vector<Interface_type*> interface_types_; 771 // Type specific functions to write out. 772 std::vector<Specific_type_function*> specific_type_functions_; 773 // Whether we are done writing out specific type functions. 774 bool specific_type_functions_are_written_; 775 // Whether named types have been converted. 776 bool named_types_are_converted_; 777}; 778 779// A block of statements. 780 781class Block 782{ 783 public: 784 Block(Block* enclosing, Location); 785 786 // Return the enclosing block. 787 const Block* 788 enclosing() const 789 { return this->enclosing_; } 790 791 // Return the bindings of the block. 792 Bindings* 793 bindings() 794 { return this->bindings_; } 795 796 const Bindings* 797 bindings() const 798 { return this->bindings_; } 799 800 // Look at the block's statements. 801 const std::vector<Statement*>* 802 statements() const 803 { return &this->statements_; } 804 805 // Return the start location. This is normally the location of the 806 // left curly brace which starts the block. 807 Location 808 start_location() const 809 { return this->start_location_; } 810 811 // Return the end location. This is normally the location of the 812 // right curly brace which ends the block. 813 Location 814 end_location() const 815 { return this->end_location_; } 816 817 // Add a statement to the block. 818 void 819 add_statement(Statement*); 820 821 // Add a statement to the front of the block. 822 void 823 add_statement_at_front(Statement*); 824 825 // Replace a statement in a block. 826 void 827 replace_statement(size_t index, Statement*); 828 829 // Add a Statement before statement number INDEX. 830 void 831 insert_statement_before(size_t index, Statement*); 832 833 // Add a Statement after statement number INDEX. 834 void 835 insert_statement_after(size_t index, Statement*); 836 837 // Set the end location of the block. 838 void 839 set_end_location(Location location) 840 { this->end_location_ = location; } 841 842 // Traverse the tree. 843 int 844 traverse(Traverse*); 845 846 // Set final types for unspecified variables and constants. 847 void 848 determine_types(); 849 850 // Return true if execution of this block may fall through to the 851 // next block. 852 bool 853 may_fall_through() const; 854 855 // Convert the block to the backend representation. 856 Bblock* 857 get_backend(Translate_context*); 858 859 // Iterate over statements. 860 861 typedef std::vector<Statement*>::iterator iterator; 862 863 iterator 864 begin() 865 { return this->statements_.begin(); } 866 867 iterator 868 end() 869 { return this->statements_.end(); } 870 871 private: 872 // Enclosing block. 873 Block* enclosing_; 874 // Statements in the block. 875 std::vector<Statement*> statements_; 876 // Binding contour. 877 Bindings* bindings_; 878 // Location of start of block. 879 Location start_location_; 880 // Location of end of block. 881 Location end_location_; 882}; 883 884// A function. 885 886class Function 887{ 888 public: 889 Function(Function_type* type, Function*, Block*, Location); 890 891 // Return the function's type. 892 Function_type* 893 type() const 894 { return this->type_; } 895 896 // Return the enclosing function if there is one. 897 Function* 898 enclosing() 899 { return this->enclosing_; } 900 901 // Set the enclosing function. This is used when building thunks 902 // for functions which call recover. 903 void 904 set_enclosing(Function* enclosing) 905 { 906 go_assert(this->enclosing_ == NULL); 907 this->enclosing_ = enclosing; 908 } 909 910 // The result variables. 911 typedef std::vector<Named_object*> Results; 912 913 // Create the result variables in the outer block. 914 void 915 create_result_variables(Gogo*); 916 917 // Update the named result variables when cloning a function which 918 // calls recover. 919 void 920 update_result_variables(); 921 922 // Return the result variables. 923 Results* 924 result_variables() 925 { return this->results_; } 926 927 bool 928 is_sink() const 929 { return this->is_sink_; } 930 931 void 932 set_is_sink() 933 { this->is_sink_ = true; } 934 935 // Whether the result variables have names. 936 bool 937 results_are_named() const 938 { return this->results_are_named_; } 939 940 // Whether this method should not be included in the type 941 // descriptor. 942 bool 943 nointerface() const 944 { 945 go_assert(this->is_method()); 946 return this->nointerface_; 947 } 948 949 // Record that this method should not be included in the type 950 // descriptor. 951 void 952 set_nointerface() 953 { 954 go_assert(this->is_method()); 955 this->nointerface_ = true; 956 } 957 958 // Record that this function is a stub method created for an unnamed 959 // type. 960 void 961 set_is_unnamed_type_stub_method() 962 { 963 go_assert(this->is_method()); 964 this->is_unnamed_type_stub_method_ = true; 965 } 966 967 // Add a new field to the closure variable. 968 void 969 add_closure_field(Named_object* var, Location loc) 970 { this->closure_fields_.push_back(std::make_pair(var, loc)); } 971 972 // Whether this function needs a closure. 973 bool 974 needs_closure() const 975 { return !this->closure_fields_.empty(); } 976 977 // Return the closure variable, creating it if necessary. This is 978 // passed to the function as a static chain parameter. 979 Named_object* 980 closure_var(); 981 982 // Set the closure variable. This is used when building thunks for 983 // functions which call recover. 984 void 985 set_closure_var(Named_object* v) 986 { 987 go_assert(this->closure_var_ == NULL); 988 this->closure_var_ = v; 989 } 990 991 // Return the variable for a reference to field INDEX in the closure 992 // variable. 993 Named_object* 994 enclosing_var(unsigned int index) 995 { 996 go_assert(index < this->closure_fields_.size()); 997 return closure_fields_[index].first; 998 } 999 1000 // Set the type of the closure variable if there is one. 1001 void 1002 set_closure_type(); 1003 1004 // Get the block of statements associated with the function. 1005 Block* 1006 block() const 1007 { return this->block_; } 1008 1009 // Get the location of the start of the function. 1010 Location 1011 location() const 1012 { return this->location_; } 1013 1014 // Return whether this function is actually a method. 1015 bool 1016 is_method() const; 1017 1018 // Add a label definition to the function. 1019 Label* 1020 add_label_definition(Gogo*, const std::string& label_name, Location); 1021 1022 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true 1023 // if we should report errors for a goto from the current location 1024 // to the label location. 1025 Label* 1026 add_label_reference(Gogo*, const std::string& label_name, 1027 Location, bool issue_goto_errors); 1028 1029 // Warn about labels that are defined but not used. 1030 void 1031 check_labels() const; 1032 1033 // Note that a new local type has been added. Return its index. 1034 unsigned int 1035 new_local_type_index() 1036 { return this->local_type_count_++; } 1037 1038 // Whether this function calls the predeclared recover function. 1039 bool 1040 calls_recover() const 1041 { return this->calls_recover_; } 1042 1043 // Record that this function calls the predeclared recover function. 1044 // This is set during the lowering pass. 1045 void 1046 set_calls_recover() 1047 { this->calls_recover_ = true; } 1048 1049 // Whether this is a recover thunk function. 1050 bool 1051 is_recover_thunk() const 1052 { return this->is_recover_thunk_; } 1053 1054 // Record that this is a thunk built for a function which calls 1055 // recover. 1056 void 1057 set_is_recover_thunk() 1058 { this->is_recover_thunk_ = true; } 1059 1060 // Whether this function already has a recover thunk. 1061 bool 1062 has_recover_thunk() const 1063 { return this->has_recover_thunk_; } 1064 1065 // Record that this function already has a recover thunk. 1066 void 1067 set_has_recover_thunk() 1068 { this->has_recover_thunk_ = true; } 1069 1070 // Record that this function is a thunk created for a defer 1071 // statement that calls the __go_set_defer_retaddr runtime function. 1072 void 1073 set_calls_defer_retaddr() 1074 { this->calls_defer_retaddr_ = true; } 1075 1076 // Whether this is a type hash or equality function created by the 1077 // compiler. 1078 bool 1079 is_type_specific_function() 1080 { return this->is_type_specific_function_; } 1081 1082 // Record that this function is a type hash or equality function 1083 // created by the compiler. 1084 void 1085 set_is_type_specific_function() 1086 { this->is_type_specific_function_ = true; } 1087 1088 // Mark the function as going into a unique section. 1089 void 1090 set_in_unique_section() 1091 { this->in_unique_section_ = true; } 1092 1093 // Swap with another function. Used only for the thunk which calls 1094 // recover. 1095 void 1096 swap_for_recover(Function *); 1097 1098 // Traverse the tree. 1099 int 1100 traverse(Traverse*); 1101 1102 // Determine types in the function. 1103 void 1104 determine_types(); 1105 1106 // Return an expression for the function descriptor, given the named 1107 // object for this function. This may only be called for functions 1108 // without a closure. This will be an immutable struct with one 1109 // field that points to the function's code. 1110 Expression* 1111 descriptor(Gogo*, Named_object*); 1112 1113 // Set the descriptor for this function. This is used when a 1114 // function declaration is followed by a function definition. 1115 void 1116 set_descriptor(Expression* descriptor) 1117 { 1118 go_assert(this->descriptor_ == NULL); 1119 this->descriptor_ = descriptor; 1120 } 1121 1122 // Return the backend representation. 1123 Bfunction* 1124 get_or_make_decl(Gogo*, Named_object*); 1125 1126 // Return the function's decl after it has been built. 1127 Bfunction* 1128 get_decl() const; 1129 1130 // Set the function decl to hold a backend representation of the function 1131 // code. 1132 void 1133 build(Gogo*, Named_object*); 1134 1135 // Get the statement that assigns values to this function's result struct. 1136 Bstatement* 1137 return_value(Gogo*, Named_object*, Location) const; 1138 1139 // Get an expression for the variable holding the defer stack. 1140 Expression* 1141 defer_stack(Location); 1142 1143 // Export the function. 1144 void 1145 export_func(Export*, const std::string& name) const; 1146 1147 // Export a function with a type. 1148 static void 1149 export_func_with_type(Export*, const std::string& name, 1150 const Function_type*); 1151 1152 // Import a function. 1153 static void 1154 import_func(Import*, std::string* pname, Typed_identifier** receiver, 1155 Typed_identifier_list** pparameters, 1156 Typed_identifier_list** presults, bool* is_varargs); 1157 1158 private: 1159 // Type for mapping from label names to Label objects. 1160 typedef Unordered_map(std::string, Label*) Labels; 1161 1162 void 1163 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**); 1164 1165 typedef std::vector<std::pair<Named_object*, 1166 Location> > Closure_fields; 1167 1168 // The function's type. 1169 Function_type* type_; 1170 // The enclosing function. This is NULL when there isn't one, which 1171 // is the normal case. 1172 Function* enclosing_; 1173 // The result variables, if any. 1174 Results* results_; 1175 // If there is a closure, this is the list of variables which appear 1176 // in the closure. This is created by the parser, and then resolved 1177 // to a real type when we lower parse trees. 1178 Closure_fields closure_fields_; 1179 // The closure variable, passed as a parameter using the static 1180 // chain parameter. Normally NULL. 1181 Named_object* closure_var_; 1182 // The outer block of statements in the function. 1183 Block* block_; 1184 // The source location of the start of the function. 1185 Location location_; 1186 // Labels defined or referenced in the function. 1187 Labels labels_; 1188 // The number of local types defined in this function. 1189 unsigned int local_type_count_; 1190 // The function descriptor, if any. 1191 Expression* descriptor_; 1192 // The function decl. 1193 Bfunction* fndecl_; 1194 // The defer stack variable. A pointer to this variable is used to 1195 // distinguish the defer stack for one function from another. This 1196 // is NULL unless we actually need a defer stack. 1197 Temporary_statement* defer_stack_; 1198 // True if this function is sink-named. No code is generated. 1199 bool is_sink_ : 1; 1200 // True if the result variables are named. 1201 bool results_are_named_ : 1; 1202 // True if this method should not be included in the type descriptor. 1203 bool nointerface_ : 1; 1204 // True if this function is a stub method created for an unnamed 1205 // type. 1206 bool is_unnamed_type_stub_method_ : 1; 1207 // True if this function calls the predeclared recover function. 1208 bool calls_recover_ : 1; 1209 // True if this a thunk built for a function which calls recover. 1210 bool is_recover_thunk_ : 1; 1211 // True if this function already has a recover thunk. 1212 bool has_recover_thunk_ : 1; 1213 // True if this is a thunk built for a defer statement that calls 1214 // the __go_set_defer_retaddr runtime function. 1215 bool calls_defer_retaddr_ : 1; 1216 // True if this is a function built by the compiler to as a hash or 1217 // equality function for some type. 1218 bool is_type_specific_function_ : 1; 1219 // True if this function should be put in a unique section. This is 1220 // turned on for field tracking. 1221 bool in_unique_section_ : 1; 1222}; 1223 1224// A snapshot of the current binding state. 1225 1226class Bindings_snapshot 1227{ 1228 public: 1229 Bindings_snapshot(const Block*, Location); 1230 1231 // Report any errors appropriate for a goto from the current binding 1232 // state of B to this one. 1233 void 1234 check_goto_from(const Block* b, Location); 1235 1236 // Report any errors appropriate for a goto from this binding state 1237 // to the current state of B. 1238 void 1239 check_goto_to(const Block* b); 1240 1241 private: 1242 bool 1243 check_goto_block(Location, const Block*, const Block*, size_t*); 1244 1245 void 1246 check_goto_defs(Location, const Block*, size_t, size_t); 1247 1248 // The current block. 1249 const Block* block_; 1250 // The number of names currently defined in each open block. 1251 // Element 0 is this->block_, element 1 is 1252 // this->block_->enclosing(), etc. 1253 std::vector<size_t> counts_; 1254 // The location where this snapshot was taken. 1255 Location location_; 1256}; 1257 1258// A function declaration. 1259 1260class Function_declaration 1261{ 1262 public: 1263 Function_declaration(Function_type* fntype, Location location) 1264 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL), 1265 fndecl_(NULL) 1266 { } 1267 1268 Function_type* 1269 type() const 1270 { return this->fntype_; } 1271 1272 Location 1273 location() const 1274 { return this->location_; } 1275 1276 const std::string& 1277 asm_name() const 1278 { return this->asm_name_; } 1279 1280 // Set the assembler name. 1281 void 1282 set_asm_name(const std::string& asm_name) 1283 { this->asm_name_ = asm_name; } 1284 1285 // Return an expression for the function descriptor, given the named 1286 // object for this function. This may only be called for functions 1287 // without a closure. This will be an immutable struct with one 1288 // field that points to the function's code. 1289 Expression* 1290 descriptor(Gogo*, Named_object*); 1291 1292 // Return true if we have created a descriptor for this declaration. 1293 bool 1294 has_descriptor() const 1295 { return this->descriptor_ != NULL; } 1296 1297 // Return a backend representation. 1298 Bfunction* 1299 get_or_make_decl(Gogo*, Named_object*); 1300 1301 // If there is a descriptor, build it into the backend 1302 // representation. 1303 void 1304 build_backend_descriptor(Gogo*); 1305 1306 // Export a function declaration. 1307 void 1308 export_func(Export* exp, const std::string& name) const 1309 { Function::export_func_with_type(exp, name, this->fntype_); } 1310 1311 private: 1312 // The type of the function. 1313 Function_type* fntype_; 1314 // The location of the declaration. 1315 Location location_; 1316 // The assembler name: this is the name to use in references to the 1317 // function. This is normally empty. 1318 std::string asm_name_; 1319 // The function descriptor, if any. 1320 Expression* descriptor_; 1321 // The function decl if needed. 1322 Bfunction* fndecl_; 1323}; 1324 1325// A variable. 1326 1327class Variable 1328{ 1329 public: 1330 Variable(Type*, Expression*, bool is_global, bool is_parameter, 1331 bool is_receiver, Location); 1332 1333 // Get the type of the variable. 1334 Type* 1335 type(); 1336 1337 Type* 1338 type() const; 1339 1340 // Return whether the type is defined yet. 1341 bool 1342 has_type() const; 1343 1344 // Get the initial value. 1345 Expression* 1346 init() const 1347 { return this->init_; } 1348 1349 // Return whether there are any preinit statements. 1350 bool 1351 has_pre_init() const 1352 { return this->preinit_ != NULL; } 1353 1354 // Return the preinit statements if any. 1355 Block* 1356 preinit() const 1357 { return this->preinit_; } 1358 1359 // Return whether this is a global variable. 1360 bool 1361 is_global() const 1362 { return this->is_global_; } 1363 1364 // Return whether this is a function parameter. 1365 bool 1366 is_parameter() const 1367 { return this->is_parameter_; } 1368 1369 // Return whether this is a closure (static chain) parameter. 1370 bool 1371 is_closure() const 1372 { return this->is_closure_; } 1373 1374 // Change this parameter to be a closure. 1375 void 1376 set_is_closure() 1377 { 1378 this->is_closure_ = true; 1379 } 1380 1381 // Return whether this is the receiver parameter of a method. 1382 bool 1383 is_receiver() const 1384 { return this->is_receiver_; } 1385 1386 // Change this parameter to be a receiver. This is used when 1387 // creating the thunks created for functions which call recover. 1388 void 1389 set_is_receiver() 1390 { 1391 go_assert(this->is_parameter_); 1392 this->is_receiver_ = true; 1393 } 1394 1395 // Change this parameter to not be a receiver. This is used when 1396 // creating the thunks created for functions which call recover. 1397 void 1398 set_is_not_receiver() 1399 { 1400 go_assert(this->is_parameter_); 1401 this->is_receiver_ = false; 1402 } 1403 1404 // Return whether this is the varargs parameter of a function. 1405 bool 1406 is_varargs_parameter() const 1407 { return this->is_varargs_parameter_; } 1408 1409 // Whether this variable's address is taken. 1410 bool 1411 is_address_taken() const 1412 { return this->is_address_taken_; } 1413 1414 // Whether this variable should live in the heap. 1415 bool 1416 is_in_heap() const 1417 { return this->is_address_taken_ && !this->is_global_; } 1418 1419 // Note that something takes the address of this variable. 1420 void 1421 set_address_taken() 1422 { this->is_address_taken_ = true; } 1423 1424 // Return whether the address is taken but does not escape. 1425 bool 1426 is_non_escaping_address_taken() const 1427 { return this->is_non_escaping_address_taken_; } 1428 1429 // Note that something takes the address of this variable such that 1430 // the address does not escape the function. 1431 void 1432 set_non_escaping_address_taken() 1433 { this->is_non_escaping_address_taken_ = true; } 1434 1435 // Get the source location of the variable's declaration. 1436 Location 1437 location() const 1438 { return this->location_; } 1439 1440 // Record that this is the varargs parameter of a function. 1441 void 1442 set_is_varargs_parameter() 1443 { 1444 go_assert(this->is_parameter_); 1445 this->is_varargs_parameter_ = true; 1446 } 1447 1448 // Return whether the variable has been used. 1449 bool 1450 is_used() const 1451 { return this->is_used_; } 1452 1453 // Mark that the variable has been used. 1454 void 1455 set_is_used() 1456 { this->is_used_ = true; } 1457 1458 // Clear the initial value; used for error handling. 1459 void 1460 clear_init() 1461 { this->init_ = NULL; } 1462 1463 // Set the initial value; used for converting shortcuts. 1464 void 1465 set_init(Expression* init) 1466 { this->init_ = init; } 1467 1468 // Get the preinit block, a block of statements to be run before the 1469 // initialization expression. 1470 Block* 1471 preinit_block(Gogo*); 1472 1473 // Add a statement to be run before the initialization expression. 1474 // This is only used for global variables. 1475 void 1476 add_preinit_statement(Gogo*, Statement*); 1477 1478 // Lower the initialization expression after parsing is complete. 1479 void 1480 lower_init_expression(Gogo*, Named_object*, Statement_inserter*); 1481 1482 // Flatten the initialization expression after ordering evaluations. 1483 void 1484 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*); 1485 1486 // A special case: the init value is used only to determine the 1487 // type. This is used if the variable is defined using := with the 1488 // comma-ok form of a map index or a receive expression. The init 1489 // value is actually the map index expression or receive expression. 1490 // We use this because we may not know the right type at parse time. 1491 void 1492 set_type_from_init_tuple() 1493 { this->type_from_init_tuple_ = true; } 1494 1495 // Another special case: the init value is used only to determine 1496 // the type. This is used if the variable is defined using := with 1497 // a range clause. The init value is the range expression. The 1498 // type of the variable is the index type of the range expression 1499 // (i.e., the first value returned by a range). 1500 void 1501 set_type_from_range_index() 1502 { this->type_from_range_index_ = true; } 1503 1504 // Another special case: like set_type_from_range_index, but the 1505 // type is the value type of the range expression (i.e., the second 1506 // value returned by a range). 1507 void 1508 set_type_from_range_value() 1509 { this->type_from_range_value_ = true; } 1510 1511 // Another special case: the init value is used only to determine 1512 // the type. This is used if the variable is defined using := with 1513 // a case in a select statement. The init value is the channel. 1514 // The type of the variable is the channel's element type. 1515 void 1516 set_type_from_chan_element() 1517 { this->type_from_chan_element_ = true; } 1518 1519 // After we lower the select statement, we once again set the type 1520 // from the initialization expression. 1521 void 1522 clear_type_from_chan_element() 1523 { 1524 go_assert(this->type_from_chan_element_); 1525 this->type_from_chan_element_ = false; 1526 } 1527 1528 // Note that this variable was created for a type switch clause. 1529 void 1530 set_is_type_switch_var() 1531 { this->is_type_switch_var_ = true; } 1532 1533 // Mark the variable as going into a unique section. 1534 void 1535 set_in_unique_section() 1536 { 1537 go_assert(this->is_global_); 1538 this->in_unique_section_ = true; 1539 } 1540 1541 // Traverse the initializer expression. 1542 int 1543 traverse_expression(Traverse*, unsigned int traverse_mask); 1544 1545 // Determine the type of the variable if necessary. 1546 void 1547 determine_type(); 1548 1549 // Get the backend representation of the variable. 1550 Bvariable* 1551 get_backend_variable(Gogo*, Named_object*, const Package*, 1552 const std::string&); 1553 1554 // Get the initial value of the variable. This may only 1555 // be called if has_pre_init() returns false. 1556 Bexpression* 1557 get_init(Gogo*, Named_object* function); 1558 1559 // Return a series of statements which sets the value of the 1560 // variable in DECL. This should only be called is has_pre_init() 1561 // returns true. DECL may be NULL for a sink variable. 1562 Bstatement* 1563 get_init_block(Gogo*, Named_object* function, Bvariable* decl); 1564 1565 // Export the variable. 1566 void 1567 export_var(Export*, const std::string& name) const; 1568 1569 // Import a variable. 1570 static void 1571 import_var(Import*, std::string* pname, Type** ptype); 1572 1573 private: 1574 // The type of a tuple. 1575 Type* 1576 type_from_tuple(Expression*, bool) const; 1577 1578 // The type of a range. 1579 Type* 1580 type_from_range(Expression*, bool, bool) const; 1581 1582 // The element type of a channel. 1583 Type* 1584 type_from_chan_element(Expression*, bool) const; 1585 1586 // The variable's type. This may be NULL if the type is set from 1587 // the expression. 1588 Type* type_; 1589 // The initial value. This may be NULL if the variable should be 1590 // initialized to the default value for the type. 1591 Expression* init_; 1592 // Statements to run before the init statement. 1593 Block* preinit_; 1594 // Location of variable definition. 1595 Location location_; 1596 // Backend representation. 1597 Bvariable* backend_; 1598 // Whether this is a global variable. 1599 bool is_global_ : 1; 1600 // Whether this is a function parameter. 1601 bool is_parameter_ : 1; 1602 // Whether this is a closure parameter. 1603 bool is_closure_ : 1; 1604 // Whether this is the receiver parameter of a method. 1605 bool is_receiver_ : 1; 1606 // Whether this is the varargs parameter of a function. 1607 bool is_varargs_parameter_ : 1; 1608 // Whether this variable is ever referenced. 1609 bool is_used_ : 1; 1610 // Whether something takes the address of this variable. For a 1611 // local variable this implies that the variable has to be on the 1612 // heap. 1613 bool is_address_taken_ : 1; 1614 // Whether something takes the address of this variable such that 1615 // the address does not escape the function. 1616 bool is_non_escaping_address_taken_ : 1; 1617 // True if we have seen this variable in a traversal. 1618 bool seen_ : 1; 1619 // True if we have lowered the initialization expression. 1620 bool init_is_lowered_ : 1; 1621 // True if we have flattened the initialization expression. 1622 bool init_is_flattened_ : 1; 1623 // True if init is a tuple used to set the type. 1624 bool type_from_init_tuple_ : 1; 1625 // True if init is a range clause and the type is the index type. 1626 bool type_from_range_index_ : 1; 1627 // True if init is a range clause and the type is the value type. 1628 bool type_from_range_value_ : 1; 1629 // True if init is a channel and the type is the channel's element type. 1630 bool type_from_chan_element_ : 1; 1631 // True if this is a variable created for a type switch case. 1632 bool is_type_switch_var_ : 1; 1633 // True if we have determined types. 1634 bool determined_type_ : 1; 1635 // True if this variable should be put in a unique section. This is 1636 // used for field tracking. 1637 bool in_unique_section_ : 1; 1638}; 1639 1640// A variable which is really the name for a function return value, or 1641// part of one. 1642 1643class Result_variable 1644{ 1645 public: 1646 Result_variable(Type* type, Function* function, int index, 1647 Location location) 1648 : type_(type), function_(function), index_(index), location_(location), 1649 backend_(NULL), is_address_taken_(false), 1650 is_non_escaping_address_taken_(false) 1651 { } 1652 1653 // Get the type of the result variable. 1654 Type* 1655 type() const 1656 { return this->type_; } 1657 1658 // Get the function that this is associated with. 1659 Function* 1660 function() const 1661 { return this->function_; } 1662 1663 // Index in the list of function results. 1664 int 1665 index() const 1666 { return this->index_; } 1667 1668 // The location of the variable definition. 1669 Location 1670 location() const 1671 { return this->location_; } 1672 1673 // Whether this variable's address is taken. 1674 bool 1675 is_address_taken() const 1676 { return this->is_address_taken_; } 1677 1678 // Note that something takes the address of this variable. 1679 void 1680 set_address_taken() 1681 { this->is_address_taken_ = true; } 1682 1683 // Return whether the address is taken but does not escape. 1684 bool 1685 is_non_escaping_address_taken() const 1686 { return this->is_non_escaping_address_taken_; } 1687 1688 // Note that something takes the address of this variable such that 1689 // the address does not escape the function. 1690 void 1691 set_non_escaping_address_taken() 1692 { this->is_non_escaping_address_taken_ = true; } 1693 1694 // Whether this variable should live in the heap. 1695 bool 1696 is_in_heap() const 1697 { return this->is_address_taken_; } 1698 1699 // Set the function. This is used when cloning functions which call 1700 // recover. 1701 void 1702 set_function(Function* function) 1703 { this->function_ = function; } 1704 1705 // Get the backend representation of the variable. 1706 Bvariable* 1707 get_backend_variable(Gogo*, Named_object*, const std::string&); 1708 1709 private: 1710 // Type of result variable. 1711 Type* type_; 1712 // Function with which this is associated. 1713 Function* function_; 1714 // Index in list of results. 1715 int index_; 1716 // Where the result variable is defined. 1717 Location location_; 1718 // Backend representation. 1719 Bvariable* backend_; 1720 // Whether something takes the address of this variable. 1721 bool is_address_taken_; 1722 // Whether something takes the address of this variable such that 1723 // the address does not escape the function. 1724 bool is_non_escaping_address_taken_; 1725}; 1726 1727// The value we keep for a named constant. This lets us hold a type 1728// and an expression. 1729 1730class Named_constant 1731{ 1732 public: 1733 Named_constant(Type* type, Expression* expr, int iota_value, 1734 Location location) 1735 : type_(type), expr_(expr), iota_value_(iota_value), location_(location), 1736 lowering_(false), is_sink_(false), bconst_(NULL) 1737 { } 1738 1739 Type* 1740 type() const 1741 { return this->type_; } 1742 1743 Expression* 1744 expr() const 1745 { return this->expr_; } 1746 1747 int 1748 iota_value() const 1749 { return this->iota_value_; } 1750 1751 Location 1752 location() const 1753 { return this->location_; } 1754 1755 // Whether we are lowering. 1756 bool 1757 lowering() const 1758 { return this->lowering_; } 1759 1760 // Set that we are lowering. 1761 void 1762 set_lowering() 1763 { this->lowering_ = true; } 1764 1765 // We are no longer lowering. 1766 void 1767 clear_lowering() 1768 { this->lowering_ = false; } 1769 1770 bool 1771 is_sink() const 1772 { return this->is_sink_; } 1773 1774 void 1775 set_is_sink() 1776 { this->is_sink_ = true; } 1777 1778 // Traverse the expression. 1779 int 1780 traverse_expression(Traverse*); 1781 1782 // Determine the type of the constant if necessary. 1783 void 1784 determine_type(); 1785 1786 // Indicate that we found and reported an error for this constant. 1787 void 1788 set_error(); 1789 1790 // Export the constant. 1791 void 1792 export_const(Export*, const std::string& name) const; 1793 1794 // Import a constant. 1795 static void 1796 import_const(Import*, std::string*, Type**, Expression**); 1797 1798 // Get the backend representation of the constant value. 1799 Bexpression* 1800 get_backend(Gogo*, Named_object*); 1801 1802 private: 1803 // The type of the constant. 1804 Type* type_; 1805 // The expression for the constant. 1806 Expression* expr_; 1807 // If the predeclared constant iota is used in EXPR_, this is the 1808 // value it will have. We do this because at parse time we don't 1809 // know whether the name "iota" will refer to the predeclared 1810 // constant or to something else. We put in the right value in when 1811 // we lower. 1812 int iota_value_; 1813 // The location of the definition. 1814 Location location_; 1815 // Whether we are currently lowering this constant. 1816 bool lowering_; 1817 // Whether this constant is blank named and needs only type checking. 1818 bool is_sink_; 1819 // The backend representation of the constant value. 1820 Bexpression* bconst_; 1821}; 1822 1823// A type declaration. 1824 1825class Type_declaration 1826{ 1827 public: 1828 Type_declaration(Location location) 1829 : location_(location), in_function_(NULL), in_function_index_(0), 1830 methods_(), issued_warning_(false) 1831 { } 1832 1833 // Return the location. 1834 Location 1835 location() const 1836 { return this->location_; } 1837 1838 // Return the function in which this type is declared. This will 1839 // return NULL for a type declared in global scope. 1840 Named_object* 1841 in_function(unsigned int* pindex) 1842 { 1843 *pindex = this->in_function_index_; 1844 return this->in_function_; 1845 } 1846 1847 // Set the function in which this type is declared. 1848 void 1849 set_in_function(Named_object* f, unsigned int index) 1850 { 1851 this->in_function_ = f; 1852 this->in_function_index_ = index; 1853 } 1854 1855 // Add a method to this type. This is used when methods are defined 1856 // before the type. 1857 Named_object* 1858 add_method(const std::string& name, Function* function); 1859 1860 // Add a method declaration to this type. 1861 Named_object* 1862 add_method_declaration(const std::string& name, Package*, 1863 Function_type* type, Location location); 1864 1865 // Return whether any methods were defined. 1866 bool 1867 has_methods() const; 1868 1869 // Return the methods. 1870 const std::vector<Named_object*>* 1871 methods() const 1872 { return &this->methods_; } 1873 1874 // Define methods when the real type is known. 1875 void 1876 define_methods(Named_type*); 1877 1878 // This is called if we are trying to use this type. It returns 1879 // true if we should issue a warning. 1880 bool 1881 using_type(); 1882 1883 private: 1884 // The location of the type declaration. 1885 Location location_; 1886 // If this type is declared in a function, a pointer back to the 1887 // function in which it is defined. 1888 Named_object* in_function_; 1889 // The index of this type in IN_FUNCTION_. 1890 unsigned int in_function_index_; 1891 // Methods defined before the type is defined. 1892 std::vector<Named_object*> methods_; 1893 // True if we have issued a warning about a use of this type 1894 // declaration when it is undefined. 1895 bool issued_warning_; 1896}; 1897 1898// An unknown object. These are created by the parser for forward 1899// references to names which have not been seen before. In a correct 1900// program, these will always point to a real definition by the end of 1901// the parse. Because they point to another Named_object, these may 1902// only be referenced by Unknown_expression objects. 1903 1904class Unknown_name 1905{ 1906 public: 1907 Unknown_name(Location location) 1908 : location_(location), real_named_object_(NULL) 1909 { } 1910 1911 // Return the location where this name was first seen. 1912 Location 1913 location() const 1914 { return this->location_; } 1915 1916 // Return the real named object that this points to, or NULL if it 1917 // was never resolved. 1918 Named_object* 1919 real_named_object() const 1920 { return this->real_named_object_; } 1921 1922 // Set the real named object that this points to. 1923 void 1924 set_real_named_object(Named_object* no); 1925 1926 private: 1927 // The location where this name was first seen. 1928 Location location_; 1929 // The real named object when it is known. 1930 Named_object* 1931 real_named_object_; 1932}; 1933 1934// A named object named. This is the result of a declaration. We 1935// don't use a superclass because they all have to be handled 1936// differently. 1937 1938class Named_object 1939{ 1940 public: 1941 enum Classification 1942 { 1943 // An uninitialized Named_object. We should never see this. 1944 NAMED_OBJECT_UNINITIALIZED, 1945 // An erroneous name. This indicates a parse error, to avoid 1946 // later errors about undefined references. 1947 NAMED_OBJECT_ERRONEOUS, 1948 // An unknown name. This is used for forward references. In a 1949 // correct program, these will all be resolved by the end of the 1950 // parse. 1951 NAMED_OBJECT_UNKNOWN, 1952 // A const. 1953 NAMED_OBJECT_CONST, 1954 // A type. 1955 NAMED_OBJECT_TYPE, 1956 // A forward type declaration. 1957 NAMED_OBJECT_TYPE_DECLARATION, 1958 // A var. 1959 NAMED_OBJECT_VAR, 1960 // A result variable in a function. 1961 NAMED_OBJECT_RESULT_VAR, 1962 // The blank identifier--the special variable named _. 1963 NAMED_OBJECT_SINK, 1964 // A func. 1965 NAMED_OBJECT_FUNC, 1966 // A forward func declaration. 1967 NAMED_OBJECT_FUNC_DECLARATION, 1968 // A package. 1969 NAMED_OBJECT_PACKAGE 1970 }; 1971 1972 // Return the classification. 1973 Classification 1974 classification() const 1975 { return this->classification_; } 1976 1977 // Classifiers. 1978 1979 bool 1980 is_erroneous() const 1981 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; } 1982 1983 bool 1984 is_unknown() const 1985 { return this->classification_ == NAMED_OBJECT_UNKNOWN; } 1986 1987 bool 1988 is_const() const 1989 { return this->classification_ == NAMED_OBJECT_CONST; } 1990 1991 bool 1992 is_type() const 1993 { return this->classification_ == NAMED_OBJECT_TYPE; } 1994 1995 bool 1996 is_type_declaration() const 1997 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; } 1998 1999 bool 2000 is_variable() const 2001 { return this->classification_ == NAMED_OBJECT_VAR; } 2002 2003 bool 2004 is_result_variable() const 2005 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; } 2006 2007 bool 2008 is_sink() const 2009 { return this->classification_ == NAMED_OBJECT_SINK; } 2010 2011 bool 2012 is_function() const 2013 { return this->classification_ == NAMED_OBJECT_FUNC; } 2014 2015 bool 2016 is_function_declaration() const 2017 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; } 2018 2019 bool 2020 is_package() const 2021 { return this->classification_ == NAMED_OBJECT_PACKAGE; } 2022 2023 // Creators. 2024 2025 static Named_object* 2026 make_erroneous_name(const std::string& name) 2027 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); } 2028 2029 static Named_object* 2030 make_unknown_name(const std::string& name, Location); 2031 2032 static Named_object* 2033 make_constant(const Typed_identifier&, const Package*, Expression*, 2034 int iota_value); 2035 2036 static Named_object* 2037 make_type(const std::string&, const Package*, Type*, Location); 2038 2039 static Named_object* 2040 make_type_declaration(const std::string&, const Package*, Location); 2041 2042 static Named_object* 2043 make_variable(const std::string&, const Package*, Variable*); 2044 2045 static Named_object* 2046 make_result_variable(const std::string&, Result_variable*); 2047 2048 static Named_object* 2049 make_sink(); 2050 2051 static Named_object* 2052 make_function(const std::string&, const Package*, Function*); 2053 2054 static Named_object* 2055 make_function_declaration(const std::string&, const Package*, Function_type*, 2056 Location); 2057 2058 static Named_object* 2059 make_package(const std::string& alias, Package* package); 2060 2061 // Getters. 2062 2063 Unknown_name* 2064 unknown_value() 2065 { 2066 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN); 2067 return this->u_.unknown_value; 2068 } 2069 2070 const Unknown_name* 2071 unknown_value() const 2072 { 2073 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN); 2074 return this->u_.unknown_value; 2075 } 2076 2077 Named_constant* 2078 const_value() 2079 { 2080 go_assert(this->classification_ == NAMED_OBJECT_CONST); 2081 return this->u_.const_value; 2082 } 2083 2084 const Named_constant* 2085 const_value() const 2086 { 2087 go_assert(this->classification_ == NAMED_OBJECT_CONST); 2088 return this->u_.const_value; 2089 } 2090 2091 Named_type* 2092 type_value() 2093 { 2094 go_assert(this->classification_ == NAMED_OBJECT_TYPE); 2095 return this->u_.type_value; 2096 } 2097 2098 const Named_type* 2099 type_value() const 2100 { 2101 go_assert(this->classification_ == NAMED_OBJECT_TYPE); 2102 return this->u_.type_value; 2103 } 2104 2105 Type_declaration* 2106 type_declaration_value() 2107 { 2108 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION); 2109 return this->u_.type_declaration; 2110 } 2111 2112 const Type_declaration* 2113 type_declaration_value() const 2114 { 2115 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION); 2116 return this->u_.type_declaration; 2117 } 2118 2119 Variable* 2120 var_value() 2121 { 2122 go_assert(this->classification_ == NAMED_OBJECT_VAR); 2123 return this->u_.var_value; 2124 } 2125 2126 const Variable* 2127 var_value() const 2128 { 2129 go_assert(this->classification_ == NAMED_OBJECT_VAR); 2130 return this->u_.var_value; 2131 } 2132 2133 Result_variable* 2134 result_var_value() 2135 { 2136 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR); 2137 return this->u_.result_var_value; 2138 } 2139 2140 const Result_variable* 2141 result_var_value() const 2142 { 2143 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR); 2144 return this->u_.result_var_value; 2145 } 2146 2147 Function* 2148 func_value() 2149 { 2150 go_assert(this->classification_ == NAMED_OBJECT_FUNC); 2151 return this->u_.func_value; 2152 } 2153 2154 const Function* 2155 func_value() const 2156 { 2157 go_assert(this->classification_ == NAMED_OBJECT_FUNC); 2158 return this->u_.func_value; 2159 } 2160 2161 Function_declaration* 2162 func_declaration_value() 2163 { 2164 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION); 2165 return this->u_.func_declaration_value; 2166 } 2167 2168 const Function_declaration* 2169 func_declaration_value() const 2170 { 2171 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION); 2172 return this->u_.func_declaration_value; 2173 } 2174 2175 Package* 2176 package_value() 2177 { 2178 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE); 2179 return this->u_.package_value; 2180 } 2181 2182 const Package* 2183 package_value() const 2184 { 2185 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE); 2186 return this->u_.package_value; 2187 } 2188 2189 const std::string& 2190 name() const 2191 { return this->name_; } 2192 2193 // Return the name to use in an error message. The difference is 2194 // that if this Named_object is defined in a different package, this 2195 // will return PACKAGE.NAME. 2196 std::string 2197 message_name() const; 2198 2199 const Package* 2200 package() const 2201 { return this->package_; } 2202 2203 // Resolve an unknown value if possible. This returns the same 2204 // Named_object or a new one. 2205 Named_object* 2206 resolve() 2207 { 2208 Named_object* ret = this; 2209 if (this->is_unknown()) 2210 { 2211 Named_object* r = this->unknown_value()->real_named_object(); 2212 if (r != NULL) 2213 ret = r; 2214 } 2215 return ret; 2216 } 2217 2218 const Named_object* 2219 resolve() const 2220 { 2221 const Named_object* ret = this; 2222 if (this->is_unknown()) 2223 { 2224 const Named_object* r = this->unknown_value()->real_named_object(); 2225 if (r != NULL) 2226 ret = r; 2227 } 2228 return ret; 2229 } 2230 2231 // The location where this object was defined or referenced. 2232 Location 2233 location() const; 2234 2235 // Convert a variable to the backend representation. 2236 Bvariable* 2237 get_backend_variable(Gogo*, Named_object* function); 2238 2239 // Return the external identifier for this object. 2240 std::string 2241 get_id(Gogo*); 2242 2243 // Get the backend representation of this object. 2244 void 2245 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&, 2246 std::vector<Bfunction*>&); 2247 2248 // Define a type declaration. 2249 void 2250 set_type_value(Named_type*); 2251 2252 // Define a function declaration. 2253 void 2254 set_function_value(Function*); 2255 2256 // Declare an unknown name as a type declaration. 2257 void 2258 declare_as_type(); 2259 2260 // Export this object. 2261 void 2262 export_named_object(Export*) const; 2263 2264 private: 2265 Named_object(const std::string&, const Package*, Classification); 2266 2267 // The name of the object. 2268 std::string name_; 2269 // The package that this object is in. This is NULL if it is in the 2270 // file we are compiling. 2271 const Package* package_; 2272 // The type of object this is. 2273 Classification classification_; 2274 // The real data. 2275 union 2276 { 2277 Unknown_name* unknown_value; 2278 Named_constant* const_value; 2279 Named_type* type_value; 2280 Type_declaration* type_declaration; 2281 Variable* var_value; 2282 Result_variable* result_var_value; 2283 Function* func_value; 2284 Function_declaration* func_declaration_value; 2285 Package* package_value; 2286 } u_; 2287}; 2288 2289// A binding contour. This binds names to objects. 2290 2291class Bindings 2292{ 2293 public: 2294 // Type for mapping from names to objects. 2295 typedef Unordered_map(std::string, Named_object*) Contour; 2296 2297 Bindings(Bindings* enclosing); 2298 2299 // Add an erroneous name. 2300 Named_object* 2301 add_erroneous_name(const std::string& name) 2302 { return this->add_named_object(Named_object::make_erroneous_name(name)); } 2303 2304 // Add an unknown name. 2305 Named_object* 2306 add_unknown_name(const std::string& name, Location location) 2307 { 2308 return this->add_named_object(Named_object::make_unknown_name(name, 2309 location)); 2310 } 2311 2312 // Add a constant. 2313 Named_object* 2314 add_constant(const Typed_identifier& tid, const Package* package, 2315 Expression* expr, int iota_value) 2316 { 2317 return this->add_named_object(Named_object::make_constant(tid, package, 2318 expr, 2319 iota_value)); 2320 } 2321 2322 // Add a type. 2323 Named_object* 2324 add_type(const std::string& name, const Package* package, Type* type, 2325 Location location) 2326 { 2327 return this->add_named_object(Named_object::make_type(name, package, type, 2328 location)); 2329 } 2330 2331 // Add a named type. This is used for builtin types, and to add an 2332 // imported type to the global scope. 2333 Named_object* 2334 add_named_type(Named_type* named_type); 2335 2336 // Add a type declaration. 2337 Named_object* 2338 add_type_declaration(const std::string& name, const Package* package, 2339 Location location) 2340 { 2341 Named_object* no = Named_object::make_type_declaration(name, package, 2342 location); 2343 return this->add_named_object(no); 2344 } 2345 2346 // Add a variable. 2347 Named_object* 2348 add_variable(const std::string& name, const Package* package, 2349 Variable* variable) 2350 { 2351 return this->add_named_object(Named_object::make_variable(name, package, 2352 variable)); 2353 } 2354 2355 // Add a result variable. 2356 Named_object* 2357 add_result_variable(const std::string& name, Result_variable* result) 2358 { 2359 return this->add_named_object(Named_object::make_result_variable(name, 2360 result)); 2361 } 2362 2363 // Add a function. 2364 Named_object* 2365 add_function(const std::string& name, const Package*, Function* function); 2366 2367 // Add a function declaration. 2368 Named_object* 2369 add_function_declaration(const std::string& name, const Package* package, 2370 Function_type* type, Location location); 2371 2372 // Add a package. The location is the location of the import 2373 // statement. 2374 Named_object* 2375 add_package(const std::string& alias, Package* package) 2376 { 2377 Named_object* no = Named_object::make_package(alias, package); 2378 return this->add_named_object(no); 2379 } 2380 2381 // Define a type which was already declared. 2382 void 2383 define_type(Named_object*, Named_type*); 2384 2385 // Add a method to the list of objects. This is not added to the 2386 // lookup table. 2387 void 2388 add_method(Named_object*); 2389 2390 // Add a named object to this binding. 2391 Named_object* 2392 add_named_object(Named_object* no) 2393 { return this->add_named_object_to_contour(&this->bindings_, no); } 2394 2395 // Clear all names in file scope from the bindings. 2396 void 2397 clear_file_scope(Gogo*); 2398 2399 // Look up a name in this binding contour and in any enclosing 2400 // binding contours. This returns NULL if the name is not found. 2401 Named_object* 2402 lookup(const std::string&) const; 2403 2404 // Look up a name in this binding contour without looking in any 2405 // enclosing binding contours. Returns NULL if the name is not found. 2406 Named_object* 2407 lookup_local(const std::string&) const; 2408 2409 // Remove a name. 2410 void 2411 remove_binding(Named_object*); 2412 2413 // Mark all variables as used. This is used for some types of parse 2414 // error. 2415 void 2416 mark_locals_used(); 2417 2418 // Traverse the tree. See the Traverse class. 2419 int 2420 traverse(Traverse*, bool is_global); 2421 2422 // Iterate over definitions. This does not include things which 2423 // were only declared. 2424 2425 typedef std::vector<Named_object*>::const_iterator 2426 const_definitions_iterator; 2427 2428 const_definitions_iterator 2429 begin_definitions() const 2430 { return this->named_objects_.begin(); } 2431 2432 const_definitions_iterator 2433 end_definitions() const 2434 { return this->named_objects_.end(); } 2435 2436 // Return the number of definitions. 2437 size_t 2438 size_definitions() const 2439 { return this->named_objects_.size(); } 2440 2441 // Return whether there are no definitions. 2442 bool 2443 empty_definitions() const 2444 { return this->named_objects_.empty(); } 2445 2446 // Iterate over declarations. This is everything that has been 2447 // declared, which includes everything which has been defined. 2448 2449 typedef Contour::const_iterator const_declarations_iterator; 2450 2451 const_declarations_iterator 2452 begin_declarations() const 2453 { return this->bindings_.begin(); } 2454 2455 const_declarations_iterator 2456 end_declarations() const 2457 { return this->bindings_.end(); } 2458 2459 // Return the number of declarations. 2460 size_t 2461 size_declarations() const 2462 { return this->bindings_.size(); } 2463 2464 // Return whether there are no declarations. 2465 bool 2466 empty_declarations() const 2467 { return this->bindings_.empty(); } 2468 2469 // Return the first declaration. 2470 Named_object* 2471 first_declaration() 2472 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; } 2473 2474 private: 2475 Named_object* 2476 add_named_object_to_contour(Contour*, Named_object*); 2477 2478 Named_object* 2479 new_definition(Named_object*, Named_object*); 2480 2481 // Enclosing bindings. 2482 Bindings* enclosing_; 2483 // The list of objects. 2484 std::vector<Named_object*> named_objects_; 2485 // The mapping from names to objects. 2486 Contour bindings_; 2487}; 2488 2489// A label. 2490 2491class Label 2492{ 2493 public: 2494 Label(const std::string& name) 2495 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL), 2496 refs_(), is_used_(false), blabel_(NULL) 2497 { } 2498 2499 // Return the label's name. 2500 const std::string& 2501 name() const 2502 { return this->name_; } 2503 2504 // Return whether the label has been defined. 2505 bool 2506 is_defined() const 2507 { return !Linemap::is_unknown_location(this->location_); } 2508 2509 // Return whether the label has been used. 2510 bool 2511 is_used() const 2512 { return this->is_used_; } 2513 2514 // Record that the label is used. 2515 void 2516 set_is_used() 2517 { this->is_used_ = true; } 2518 2519 // Return the location of the definition. 2520 Location 2521 location() const 2522 { return this->location_; } 2523 2524 // Return the bindings snapshot. 2525 Bindings_snapshot* 2526 snapshot() const 2527 { return this->snapshot_; } 2528 2529 // Add a snapshot of a goto which refers to this label. 2530 void 2531 add_snapshot_ref(Bindings_snapshot* snapshot) 2532 { 2533 go_assert(Linemap::is_unknown_location(this->location_)); 2534 this->refs_.push_back(snapshot); 2535 } 2536 2537 // Return the list of snapshots of goto statements which refer to 2538 // this label. 2539 const std::vector<Bindings_snapshot*>& 2540 refs() const 2541 { return this->refs_; } 2542 2543 // Clear the references. 2544 void 2545 clear_refs(); 2546 2547 // Define the label at LOCATION with the given bindings snapshot. 2548 void 2549 define(Location location, Bindings_snapshot* snapshot) 2550 { 2551 go_assert(Linemap::is_unknown_location(this->location_) 2552 && this->snapshot_ == NULL); 2553 this->location_ = location; 2554 this->snapshot_ = snapshot; 2555 } 2556 2557 // Return the backend representation for this label. 2558 Blabel* 2559 get_backend_label(Translate_context*); 2560 2561 // Return an expression for the address of this label. This is used 2562 // to get the return address of a deferred function to see whether 2563 // the function may call recover. 2564 Bexpression* 2565 get_addr(Translate_context*, Location location); 2566 2567 private: 2568 // The name of the label. 2569 std::string name_; 2570 // The location of the definition. This is 0 if the label has not 2571 // yet been defined. 2572 Location location_; 2573 // A snapshot of the set of bindings defined at this label, used to 2574 // issue errors about invalid goto statements. 2575 Bindings_snapshot* snapshot_; 2576 // A list of snapshots of goto statements which refer to this label. 2577 std::vector<Bindings_snapshot*> refs_; 2578 // Whether the label has been used. 2579 bool is_used_; 2580 // The backend representation. 2581 Blabel* blabel_; 2582}; 2583 2584// An unnamed label. These are used when lowering loops. 2585 2586class Unnamed_label 2587{ 2588 public: 2589 Unnamed_label(Location location) 2590 : location_(location), blabel_(NULL) 2591 { } 2592 2593 // Get the location where the label is defined. 2594 Location 2595 location() const 2596 { return this->location_; } 2597 2598 // Set the location where the label is defined. 2599 void 2600 set_location(Location location) 2601 { this->location_ = location; } 2602 2603 // Return a statement which defines this label. 2604 Bstatement* 2605 get_definition(Translate_context*); 2606 2607 // Return a goto to this label from LOCATION. 2608 Bstatement* 2609 get_goto(Translate_context*, Location location); 2610 2611 private: 2612 // Return the backend representation. 2613 Blabel* 2614 get_blabel(Translate_context*); 2615 2616 // The location where the label is defined. 2617 Location location_; 2618 // The backend representation of this label. 2619 Blabel* blabel_; 2620}; 2621 2622// An imported package. 2623 2624class Package 2625{ 2626 public: 2627 Package(const std::string& pkgpath, const std::string& pkgpath_symbol, 2628 Location location); 2629 2630 // Get the package path used for all symbols exported from this 2631 // package. 2632 const std::string& 2633 pkgpath() const 2634 { return this->pkgpath_; } 2635 2636 // Return the package path to use for a symbol name. 2637 std::string 2638 pkgpath_symbol() const; 2639 2640 // Set the package path symbol. 2641 void 2642 set_pkgpath_symbol(const std::string&); 2643 2644 // Return the location of the import statement. 2645 Location 2646 location() const 2647 { return this->location_; } 2648 2649 // Return whether we know the name of this package yet. 2650 bool 2651 has_package_name() const 2652 { return !this->package_name_.empty(); } 2653 2654 // The name that this package uses in its package clause. This may 2655 // be different from the name in the associated Named_object if the 2656 // import statement used an alias. 2657 const std::string& 2658 package_name() const 2659 { 2660 go_assert(!this->package_name_.empty()); 2661 return this->package_name_; 2662 } 2663 2664 // The priority of this package. The init function of packages with 2665 // lower priority must be run before the init function of packages 2666 // with higher priority. 2667 int 2668 priority() const 2669 { return this->priority_; } 2670 2671 // Set the priority. 2672 void 2673 set_priority(int priority); 2674 2675 // Return the bindings. 2676 Bindings* 2677 bindings() 2678 { return this->bindings_; } 2679 2680 // Whether some symbol from the package was used. 2681 bool 2682 used() const 2683 { return this->used_ > 0; } 2684 2685 // Note that some symbol from this package was used. 2686 void 2687 note_usage() const 2688 { this->used_++; } 2689 2690 // Note that USAGE might be a fake usage of this package. 2691 void 2692 note_fake_usage(Expression* usage) const 2693 { this->fake_uses_.insert(usage); } 2694 2695 // Forget a given USAGE of this package. 2696 void 2697 forget_usage(Expression* usage) const; 2698 2699 // Clear the used field for the next file. 2700 void 2701 clear_used(); 2702 2703 // Whether this package was imported in the current file. 2704 bool 2705 is_imported() const 2706 { return this->is_imported_; } 2707 2708 // Note that this package was imported in the current file. 2709 void 2710 set_is_imported() 2711 { this->is_imported_ = true; } 2712 2713 // Clear the imported field for the next file. 2714 void 2715 clear_is_imported() 2716 { this->is_imported_ = false; } 2717 2718 // Whether this package was imported with a name of "_". 2719 bool 2720 uses_sink_alias() const 2721 { return this->uses_sink_alias_; } 2722 2723 // Note that this package was imported with a name of "_". 2724 void 2725 set_uses_sink_alias() 2726 { this->uses_sink_alias_ = true; } 2727 2728 // Clear the sink alias field for the next file. 2729 void 2730 clear_uses_sink_alias() 2731 { this->uses_sink_alias_ = false; } 2732 2733 // Look up a name in the package. Returns NULL if the name is not 2734 // found. 2735 Named_object* 2736 lookup(const std::string& name) const 2737 { return this->bindings_->lookup(name); } 2738 2739 // Set the name of the package. 2740 void 2741 set_package_name(const std::string& name, Location); 2742 2743 // Set the location of the package. This is used to record the most 2744 // recent import location. 2745 void 2746 set_location(Location location) 2747 { this->location_ = location; } 2748 2749 // Add a constant to the package. 2750 Named_object* 2751 add_constant(const Typed_identifier& tid, Expression* expr) 2752 { return this->bindings_->add_constant(tid, this, expr, 0); } 2753 2754 // Add a type to the package. 2755 Named_object* 2756 add_type(const std::string& name, Type* type, Location location) 2757 { return this->bindings_->add_type(name, this, type, location); } 2758 2759 // Add a type declaration to the package. 2760 Named_object* 2761 add_type_declaration(const std::string& name, Location location) 2762 { return this->bindings_->add_type_declaration(name, this, location); } 2763 2764 // Add a variable to the package. 2765 Named_object* 2766 add_variable(const std::string& name, Variable* variable) 2767 { return this->bindings_->add_variable(name, this, variable); } 2768 2769 // Add a function declaration to the package. 2770 Named_object* 2771 add_function_declaration(const std::string& name, Function_type* type, 2772 Location loc) 2773 { return this->bindings_->add_function_declaration(name, this, type, loc); } 2774 2775 // Determine types of constants. 2776 void 2777 determine_types(); 2778 2779 private: 2780 // The package path for type reflection data. 2781 std::string pkgpath_; 2782 // The package path for symbol names. 2783 std::string pkgpath_symbol_; 2784 // The name that this package uses in the package clause. This may 2785 // be the empty string if it is not yet known. 2786 std::string package_name_; 2787 // The names in this package. 2788 Bindings* bindings_; 2789 // The priority of this package. A package has a priority higher 2790 // than the priority of all of the packages that it imports. This 2791 // is used to run init functions in the right order. 2792 int priority_; 2793 // The location of the import statement. 2794 Location location_; 2795 // The amount of times some name from this package was used. This is mutable 2796 // because we can use a package even if we have a const pointer to it. 2797 mutable size_t used_; 2798 // A set of possibly fake uses of this package. This is mutable because we 2799 // can track fake uses of a package even if we have a const pointer to it. 2800 mutable std::set<Expression*> fake_uses_; 2801 // True if this package was imported in the current file. 2802 bool is_imported_; 2803 // True if this package was imported with a name of "_". 2804 bool uses_sink_alias_; 2805}; 2806 2807// Return codes for the traversal functions. This is not an enum 2808// because we want to be able to declare traversal functions in other 2809// header files without including this one. 2810 2811// Continue traversal as usual. 2812const int TRAVERSE_CONTINUE = -1; 2813 2814// Exit traversal. 2815const int TRAVERSE_EXIT = 0; 2816 2817// Continue traversal, but skip components of the current object. 2818// E.g., if this is returned by Traverse::statement, we do not 2819// traverse the expressions in the statement even if 2820// traverse_expressions is set in the traverse_mask. 2821const int TRAVERSE_SKIP_COMPONENTS = 1; 2822 2823// This class is used when traversing the parse tree. The caller uses 2824// a subclass which overrides functions as desired. 2825 2826class Traverse 2827{ 2828 public: 2829 // These bitmasks say what to traverse. 2830 static const unsigned int traverse_variables = 0x1; 2831 static const unsigned int traverse_constants = 0x2; 2832 static const unsigned int traverse_functions = 0x4; 2833 static const unsigned int traverse_blocks = 0x8; 2834 static const unsigned int traverse_statements = 0x10; 2835 static const unsigned int traverse_expressions = 0x20; 2836 static const unsigned int traverse_types = 0x40; 2837 2838 Traverse(unsigned int traverse_mask) 2839 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL) 2840 { } 2841 2842 virtual ~Traverse(); 2843 2844 // The bitmask of what to traverse. 2845 unsigned int 2846 traverse_mask() const 2847 { return this->traverse_mask_; } 2848 2849 // Record that we are going to traverse a type. This returns true 2850 // if the type has already been seen in this traversal. This is 2851 // required because types, unlike expressions, can form a circular 2852 // graph. 2853 bool 2854 remember_type(const Type*); 2855 2856 // Record that we are going to see an expression. This returns true 2857 // if the expression has already been seen in this traversal. This 2858 // is only needed for cases where multiple expressions can point to 2859 // a single one. 2860 bool 2861 remember_expression(const Expression*); 2862 2863 // These functions return one of the TRAVERSE codes defined above. 2864 2865 // If traverse_variables is set in the mask, this is called for 2866 // every variable in the tree. 2867 virtual int 2868 variable(Named_object*); 2869 2870 // If traverse_constants is set in the mask, this is called for 2871 // every named constant in the tree. The bool parameter is true for 2872 // a global constant. 2873 virtual int 2874 constant(Named_object*, bool); 2875 2876 // If traverse_functions is set in the mask, this is called for 2877 // every function in the tree. 2878 virtual int 2879 function(Named_object*); 2880 2881 // If traverse_blocks is set in the mask, this is called for every 2882 // block in the tree. 2883 virtual int 2884 block(Block*); 2885 2886 // If traverse_statements is set in the mask, this is called for 2887 // every statement in the tree. 2888 virtual int 2889 statement(Block*, size_t* index, Statement*); 2890 2891 // If traverse_expressions is set in the mask, this is called for 2892 // every expression in the tree. 2893 virtual int 2894 expression(Expression**); 2895 2896 // If traverse_types is set in the mask, this is called for every 2897 // type in the tree. 2898 virtual int 2899 type(Type*); 2900 2901 private: 2902 // A hash table for types we have seen during this traversal. Note 2903 // that this uses the default hash functions for pointers rather 2904 // than Type_hash_identical and Type_identical. This is because for 2905 // traversal we care about seeing a specific type structure. If 2906 // there are two separate instances of identical types, we want to 2907 // traverse both. 2908 typedef Unordered_set(const Type*) Types_seen; 2909 2910 typedef Unordered_set(const Expression*) Expressions_seen; 2911 2912 // Bitmask of what sort of objects to traverse. 2913 unsigned int traverse_mask_; 2914 // Types which have been seen in this traversal. 2915 Types_seen* types_seen_; 2916 // Expressions which have been seen in this traversal. 2917 Expressions_seen* expressions_seen_; 2918}; 2919 2920// A class which makes it easier to insert new statements before the 2921// current statement during a traversal. 2922 2923class Statement_inserter 2924{ 2925 public: 2926 // Empty constructor. 2927 Statement_inserter() 2928 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL) 2929 { } 2930 2931 // Constructor for a statement in a block. 2932 Statement_inserter(Block* block, size_t *pindex) 2933 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL) 2934 { } 2935 2936 // Constructor for a global variable. 2937 Statement_inserter(Gogo* gogo, Variable* var) 2938 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var) 2939 { go_assert(var->is_global()); } 2940 2941 // We use the default copy constructor and assignment operator. 2942 2943 // Insert S before the statement we are traversing, or before the 2944 // initialization expression of a global variable. 2945 void 2946 insert(Statement* s); 2947 2948 private: 2949 // The block that the statement is in. 2950 Block* block_; 2951 // The index of the statement that we are traversing. 2952 size_t* pindex_; 2953 // The IR, needed when looking at an initializer expression for a 2954 // global variable. 2955 Gogo* gogo_; 2956 // The global variable, when looking at an initializer expression. 2957 Variable* var_; 2958}; 2959 2960// When translating the gogo IR into the backend data structure, this 2961// is the context we pass down the blocks and statements. 2962 2963class Translate_context 2964{ 2965 public: 2966 Translate_context(Gogo* gogo, Named_object* function, Block* block, 2967 Bblock* bblock) 2968 : gogo_(gogo), backend_(gogo->backend()), function_(function), 2969 block_(block), bblock_(bblock), is_const_(false) 2970 { } 2971 2972 // Accessors. 2973 2974 Gogo* 2975 gogo() 2976 { return this->gogo_; } 2977 2978 Backend* 2979 backend() 2980 { return this->backend_; } 2981 2982 Named_object* 2983 function() 2984 { return this->function_; } 2985 2986 Block* 2987 block() 2988 { return this->block_; } 2989 2990 Bblock* 2991 bblock() 2992 { return this->bblock_; } 2993 2994 bool 2995 is_const() 2996 { return this->is_const_; } 2997 2998 // Make a constant context. 2999 void 3000 set_is_const() 3001 { this->is_const_ = true; } 3002 3003 private: 3004 // The IR for the entire compilation unit. 3005 Gogo* gogo_; 3006 // The generator for the backend data structures. 3007 Backend* backend_; 3008 // The function we are currently translating. NULL if not in a 3009 // function, e.g., the initializer of a global variable. 3010 Named_object* function_; 3011 // The block we are currently translating. NULL if not in a 3012 // function. 3013 Block *block_; 3014 // The backend representation of the current block. NULL if block_ 3015 // is NULL. 3016 Bblock* bblock_; 3017 // Whether this is being evaluated in a constant context. This is 3018 // used for type descriptor initializers. 3019 bool is_const_; 3020}; 3021 3022// Runtime error codes. These must match the values in 3023// libgo/runtime/go-runtime-error.c. 3024 3025// Slice index out of bounds: negative or larger than the length of 3026// the slice. 3027static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0; 3028 3029// Array index out of bounds. 3030static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1; 3031 3032// String index out of bounds. 3033static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2; 3034 3035// Slice slice out of bounds: negative or larger than the length of 3036// the slice or high bound less than low bound. 3037static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3; 3038 3039// Array slice out of bounds. 3040static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4; 3041 3042// String slice out of bounds. 3043static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5; 3044 3045// Dereference of nil pointer. This is used when there is a 3046// dereference of a pointer to a very large struct or array, to ensure 3047// that a gigantic array is not used a proxy to access random memory 3048// locations. 3049static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6; 3050 3051// Slice length or capacity out of bounds in make: negative or 3052// overflow or length greater than capacity. 3053static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7; 3054 3055// Map capacity out of bounds in make: negative or overflow. 3056static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8; 3057 3058// Channel capacity out of bounds in make: negative or overflow. 3059static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9; 3060 3061// Division by zero. 3062static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10; 3063 3064// This is used by some of the langhooks. 3065extern Gogo* go_get_gogo(); 3066 3067// Whether we have seen any errors. FIXME: Replace with a backend 3068// interface. 3069extern bool saw_errors(); 3070 3071#endif // !defined(GO_GOGO_H) 3072