1 // gogo.h -- Go frontend parsed representation. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
12 class Type_hash_identical;
15 class Typed_identifier;
16 class Typed_identifier_list;
20 class Temporary_statement;
29 class Struct_field_list;
35 class Forward_declaration_type;
40 class Translate_context;
50 // This file declares the basic classes used to hold the internal
51 // representation of Go which is built by the parser.
53 // An initialization function for an imported package. This is a
54 // magic function which initializes variables and runs the "init"
60 Import_init(const std::string& package_name, const std::string& init_name,
62 : package_name_(package_name), init_name_(init_name), priority_(priority)
65 // The name of the package being imported.
68 { return this->package_name_; }
70 // The name of the package's init function.
73 { return this->init_name_; }
75 // The priority of the initialization function. Functions with a
76 // lower priority number must be run first.
79 { return this->priority_; }
82 // The name of the package being imported.
83 std::string package_name_;
84 // The name of the package's init function.
85 std::string init_name_;
90 // For sorting purposes.
93 operator<(const Import_init& i1, const Import_init& i2)
95 if (i1.priority() < i2.priority())
97 if (i1.priority() > i2.priority())
99 if (i1.package_name() != i2.package_name())
100 return i1.package_name() < i2.package_name();
101 return i1.init_name() < i2.init_name();
104 // The holder for the internal representation of the entire
110 // Create the IR, passing in the sizes of the types "int" and
111 // "uintptr" in bits.
112 Gogo(Backend* backend, int int_type_size, int pointer_size);
114 // Get the backend generator.
117 { return this->backend_; }
119 // Get the package name.
121 package_name() const;
123 // Set the package name.
125 set_package_name(const std::string&, source_location);
127 // Return whether this is the "main" package.
129 is_main_package() const;
131 // If necessary, adjust the name to use for a hidden symbol. We add
132 // a prefix of the package name, so that hidden symbols in different
133 // packages do not collide.
135 pack_hidden_name(const std::string& name, bool is_exported) const
139 : ('.' + this->unique_prefix()
140 + '.' + this->package_name()
144 // Unpack a name which may have been hidden. Returns the
145 // user-visible name of the object.
147 unpack_hidden_name(const std::string& name)
148 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
150 // Return whether a possibly packed name is hidden.
152 is_hidden_name(const std::string& name)
153 { return name[0] == '.'; }
155 // Return the package prefix of a hidden name.
157 hidden_name_prefix(const std::string& name)
159 go_assert(Gogo::is_hidden_name(name));
160 return name.substr(1, name.rfind('.') - 1);
163 // Given a name which may or may not have been hidden, return the
164 // name to use in an error message.
166 message_name(const std::string& name);
168 // Return whether a name is the blank identifier _.
170 is_sink_name(const std::string& name)
172 return (name[0] == '.'
173 && name[name.length() - 1] == '_'
174 && name[name.length() - 2] == '.');
177 // Return the unique prefix to use for all exported symbols.
179 unique_prefix() const;
181 // Set the unique prefix.
183 set_unique_prefix(const std::string&);
185 // Return the priority to use for the package we are compiling.
186 // This is two more than the largest priority of any package we
189 package_priority() const;
191 // Import a package. FILENAME is the file name argument, LOCAL_NAME
192 // is the local name to give to the package. If LOCAL_NAME is empty
193 // the declarations are added to the global scope.
195 import_package(const std::string& filename, const std::string& local_name,
196 bool is_local_name_exported, source_location);
198 // Whether we are the global binding level.
200 in_global_scope() const;
202 // Look up a name in the current binding contours.
204 lookup(const std::string&, Named_object** pfunction) const;
206 // Look up a name in the current block.
208 lookup_in_block(const std::string&) const;
210 // Look up a name in the global namespace--the universal scope.
212 lookup_global(const char*) const;
214 // Add a new imported package. REAL_NAME is the real name of the
215 // package. ALIAS is the alias of the package; this may be the same
216 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
217 // this package should be added to the global namespace; this is
218 // true if the alias is ".". LOCATION is the location of the import
219 // statement. This returns the new package, or NULL on error.
221 add_imported_package(const std::string& real_name, const std::string& alias,
222 bool is_alias_exported,
223 const std::string& unique_prefix,
224 source_location location,
225 bool* padd_to_globals);
227 // Register a package. This package may or may not be imported.
228 // This returns the Package structure for the package, creating if
231 register_package(const std::string& name, const std::string& unique_prefix,
234 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
235 // method function should be added to the type of its receiver.
237 start_function(const std::string& name, Function_type* type,
238 bool add_method_to_type, source_location);
240 // Finish compiling a function.
242 finish_function(source_location);
244 // Return the current function.
246 current_function() const;
248 // Start a new block. This is not initially associated with a
251 start_block(source_location);
253 // Finish the current block and return it.
255 finish_block(source_location);
257 // Declare an unknown name. This is used while parsing. The name
258 // must be resolved by the end of the parse. Unknown names are
259 // always added at the package level.
261 add_unknown_name(const std::string& name, source_location);
263 // Declare a function.
265 declare_function(const std::string&, Function_type*, source_location);
269 add_label_definition(const std::string&, source_location);
271 // Add a label reference.
273 add_label_reference(const std::string&);
275 // Add a statement to the current block.
277 add_statement(Statement*);
279 // Add a block to the current block.
281 add_block(Block*, source_location);
285 add_constant(const Typed_identifier&, Expression*, int iota_value);
289 add_type(const std::string&, Type*, source_location);
291 // Add a named type. This is used for builtin types, and to add an
292 // imported type to the global scope.
294 add_named_type(Named_type*);
298 declare_type(const std::string&, source_location);
300 // Declare a type at the package level. This is used when the
301 // parser sees an unknown name where a type name is required.
303 declare_package_type(const std::string&, source_location);
305 // Define a type which was already declared.
307 define_type(Named_object*, Named_type*);
311 add_variable(const std::string&, Variable*);
313 // Add a sink--a reference to the blank identifier _.
317 // Add a named object to the current namespace. This is used for
318 // import . "package".
320 add_named_object(Named_object*);
322 // Return a name to use for a thunk function. A thunk function is
323 // one we create during the compilation, for a go statement or a
324 // defer statement or a method expression.
328 // Return whether an object is a thunk.
330 is_thunk(const Named_object*);
332 // Note that we've seen an interface type. This is used to build
333 // all required interface method tables.
335 record_interface_type(Interface_type*);
337 // Note that we need an initialization function.
340 { this->need_init_fn_ = true; }
342 // Clear out all names in file scope. This is called when we start
343 // parsing a new file.
347 // Traverse the tree. See the Traverse class.
351 // Define the predeclared global names.
353 define_global_names();
355 // Verify and complete all types.
359 // Lower the parse tree.
363 // Lower all the statements in a block.
365 lower_block(Named_object* function, Block*);
367 // Lower an expression.
369 lower_expression(Named_object* function, Expression**);
373 lower_constant(Named_object*);
375 // Finalize the method lists and build stub methods for named types.
379 // Work out the types to use for unspecified variables and
384 // Type check the program.
388 // Check the types in a single block. This is used for complicated
391 check_types_in_block(Block*);
393 // Check for return statements.
395 check_return_statements();
401 // Add an import control function for an imported package to the
404 add_import_init_fn(const std::string& package_name,
405 const std::string& init_name, int prio);
407 // Turn short-cut operators (&&, ||) into explicit if statements.
411 // Use temporary variables to force order of evaluation.
415 // Build thunks for functions which call recover.
417 build_recover_thunks();
419 // Simplify statements which might use thunks: go and defer
422 simplify_thunk_statements();
424 // Convert named types to the backend representation.
426 convert_named_types();
428 // Convert named types in a list of bindings.
430 convert_named_types_in_bindings(Bindings*);
432 // True if named types have been converted to the backend
435 named_types_are_converted() const
436 { return this->named_types_are_converted_; }
438 // Write out the global values.
442 // Build a call to a builtin function. PDECL should point to a NULL
443 // initialized static pointer which will hold the fndecl. NAME is
444 // the name of the function. NARGS is the number of arguments.
445 // RETTYPE is the return type. It is followed by NARGS pairs of
446 // type and argument (both trees).
448 call_builtin(tree* pdecl, source_location, const char* name, int nargs,
451 // Build a call to the runtime error function.
453 runtime_error(int code, source_location);
455 // Build a builtin struct with a list of fields.
457 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
460 // Mark a function declaration as a builtin library function.
462 mark_fndecl_as_builtin_library(tree fndecl);
464 // Build the type of the struct that holds a slice for the given
467 slice_type_tree(tree element_type_tree);
469 // Given a tree for a slice type, return the tree for the element
472 slice_element_type_tree(tree slice_type_tree);
474 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
475 // the slice. VALUES points to the values. COUNT is the size,
476 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
479 slice_constructor(tree slice_type_tree, tree values, tree count,
482 // Build a constructor for an empty slice. SLICE_TYPE_TREE is the
483 // type of the slice.
485 empty_slice_constructor(tree slice_type_tree);
487 // Build a map descriptor.
489 map_descriptor(Map_type*);
491 // Return a tree for the type of a map descriptor. This is struct
492 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
495 map_descriptor_type();
497 // Build a type descriptor for TYPE using INITIALIZER as the type
498 // descriptor. This builds a new decl stored in *PDECL.
500 build_type_descriptor_decl(const Type*, Expression* initializer,
503 // Build required interface method tables.
505 build_interface_method_tables();
507 // Build an interface method table for a type: a list of function
508 // pointers, one for each interface method. This returns a decl.
510 interface_method_table_for_type(const Interface_type*, Named_type*,
513 // Return a tree which allocate SIZE bytes to hold values of type
516 allocate_memory(Type *type, tree size, source_location);
518 // Return a type to use for pointer to const char.
520 const_char_pointer_type_tree();
522 // Build a string constant with the right type.
524 string_constant_tree(const std::string&);
526 // Build a Go string constant. This returns a pointer to the
529 go_string_constant_tree(const std::string&);
531 // Receive a value from a channel.
533 receive_from_channel(tree type_tree, tree channel, bool for_select,
536 // Return a tree for receiving an integer on a channel.
538 receive_as_64bit_integer(tree type, tree channel, bool blocking,
542 // Make a trampoline which calls FNADDR passing CLOSURE.
544 make_trampoline(tree fnaddr, tree closure, source_location);
547 // During parsing, we keep a stack of functions. Each function on
548 // the stack is one that we are currently parsing. For each
549 // function, we keep track of the current stack of blocks.
553 Named_object* function;
554 // The stack of active blocks in the function.
555 std::vector<Block*> blocks;
558 // The stack of functions.
559 typedef std::vector<Open_function> Open_functions;
561 // Create trees for implicit builtin functions.
563 define_builtin_function_trees();
565 // Set up the built-in unsafe package.
567 import_unsafe(const std::string&, bool is_exported, source_location);
569 // Add a new imported package.
571 add_package(const std::string& real_name, const std::string& alias,
572 const std::string& unique_prefix, source_location location);
574 // Return the current binding contour.
579 current_bindings() const;
581 // Return the current block.
585 // Get the name of the magic initialization function.
589 // Get the decl for the magic initialization function.
591 initialization_function_decl();
593 // Write the magic initialization function.
595 write_initialization_function(tree fndecl, tree init_stmt_list);
597 // Initialize imported packages.
601 // Register variables with the garbage collector.
603 register_gc_vars(const std::vector<Named_object*>&, tree*);
605 // Build a pointer to a Go string constant. This returns a pointer
608 ptr_go_string_constant_tree(const std::string&);
610 // Return the name to use for a type descriptor decl for an unnamed
613 unnamed_type_descriptor_decl_name(const Type* type);
615 // Return the name to use for a type descriptor decl for a type
616 // named NO, defined in IN_FUNCTION.
618 type_descriptor_decl_name(const Named_object* no,
619 const Named_object* in_function);
621 // Where a type descriptor should be defined.
622 enum Type_descriptor_location
624 // Defined in this file.
625 TYPE_DESCRIPTOR_DEFINED,
626 // Defined in some other file.
627 TYPE_DESCRIPTOR_UNDEFINED,
628 // Common definition which may occur in multiple files.
629 TYPE_DESCRIPTOR_COMMON
632 // Return where the decl for TYPE should be defined.
633 Type_descriptor_location
634 type_descriptor_location(const Type* type);
636 // Return the type of a trampoline.
638 trampoline_type_tree();
640 // Type used to map import names to packages.
641 typedef std::map<std::string, Package*> Imports;
643 // Type used to map package names to packages.
644 typedef std::map<std::string, Package*> Packages;
646 // Type used to map special names in the sys package.
647 typedef std::map<std::string, std::string> Sys_names;
649 // Hash table mapping map types to map descriptor decls.
650 typedef Unordered_map_hash(const Map_type*, tree, Type_hash_identical,
651 Type_identical) Map_descriptors;
653 // Map unnamed types to type descriptor decls.
654 typedef Unordered_map_hash(const Type*, tree, Type_hash_identical,
655 Type_identical) Type_descriptor_decls;
657 // The backend generator.
659 // The package we are compiling.
661 // The list of currently open functions during parsing.
662 Open_functions functions_;
663 // The global binding contour. This includes the builtin functions
664 // and the package we are compiling.
666 // Mapping from import file names to packages.
668 // Whether the magic unsafe package was imported.
669 bool imported_unsafe_;
670 // Mapping from package names we have seen to packages. This does
671 // not include the package we are compiling.
673 // Mapping from map types to map descriptors.
674 Map_descriptors* map_descriptors_;
675 // Mapping from unnamed types to type descriptor decls.
676 Type_descriptor_decls* type_descriptor_decls_;
677 // The functions named "init", if there are any.
678 std::vector<Named_object*> init_functions_;
679 // Whether we need a magic initialization function.
681 // The name of the magic initialization function.
682 std::string init_fn_name_;
683 // A list of import control variables for packages that we import.
684 std::set<Import_init> imported_init_fns_;
685 // The unique prefix used for all global symbols.
686 std::string unique_prefix_;
687 // Whether an explicit unique prefix was set by -fgo-prefix.
688 bool unique_prefix_specified_;
689 // A list of interface types defined while parsing.
690 std::vector<Interface_type*> interface_types_;
691 // Whether named types have been converted.
692 bool named_types_are_converted_;
695 // A block of statements.
700 Block(Block* enclosing, source_location);
702 // Return the enclosing block.
705 { return this->enclosing_; }
707 // Return the bindings of the block.
710 { return this->bindings_; }
714 { return this->bindings_; }
716 // Look at the block's statements.
717 const std::vector<Statement*>*
719 { return &this->statements_; }
721 // Return the start location. This is normally the location of the
722 // left curly brace which starts the block.
724 start_location() const
725 { return this->start_location_; }
727 // Return the end location. This is normally the location of the
728 // right curly brace which ends the block.
731 { return this->end_location_; }
733 // Add a statement to the block.
735 add_statement(Statement*);
737 // Add a statement to the front of the block.
739 add_statement_at_front(Statement*);
741 // Replace a statement in a block.
743 replace_statement(size_t index, Statement*);
745 // Add a Statement before statement number INDEX.
747 insert_statement_before(size_t index, Statement*);
749 // Add a Statement after statement number INDEX.
751 insert_statement_after(size_t index, Statement*);
753 // Set the end location of the block.
755 set_end_location(source_location location)
756 { this->end_location_ = location; }
758 // Traverse the tree.
762 // Set final types for unspecified variables and constants.
766 // Return true if execution of this block may fall through to the
769 may_fall_through() const;
771 // Convert the block to the backend representation.
773 get_backend(Translate_context*);
775 // Iterate over statements.
777 typedef std::vector<Statement*>::iterator iterator;
781 { return this->statements_.begin(); }
785 { return this->statements_.end(); }
790 // Statements in the block.
791 std::vector<Statement*> statements_;
794 // Location of start of block.
795 source_location start_location_;
796 // Location of end of block.
797 source_location end_location_;
805 Function(Function_type* type, Function*, Block*, source_location);
807 // Return the function's type.
810 { return this->type_; }
812 // Return the enclosing function if there is one.
815 { return this->enclosing_; }
817 // Set the enclosing function. This is used when building thunks
818 // for functions which call recover.
820 set_enclosing(Function* enclosing)
822 go_assert(this->enclosing_ == NULL);
823 this->enclosing_ = enclosing;
826 // The result variables.
827 typedef std::vector<Named_object*> Results;
829 // Create the result variables in the outer block.
831 create_result_variables(Gogo*);
833 // Update the named result variables when cloning a function which
836 update_result_variables();
838 // Return the result variables.
841 { return this->results_; }
843 // Whether the result variables have names.
845 results_are_named() const
846 { return this->results_are_named_; }
848 // Add a new field to the closure variable.
850 add_closure_field(Named_object* var, source_location loc)
851 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
853 // Whether this function needs a closure.
855 needs_closure() const
856 { return !this->closure_fields_.empty(); }
858 // Return the closure variable, creating it if necessary. This is
859 // passed to the function as a static chain parameter.
863 // Set the closure variable. This is used when building thunks for
864 // functions which call recover.
866 set_closure_var(Named_object* v)
868 go_assert(this->closure_var_ == NULL);
869 this->closure_var_ = v;
872 // Return the variable for a reference to field INDEX in the closure
875 enclosing_var(unsigned int index)
877 go_assert(index < this->closure_fields_.size());
878 return closure_fields_[index].first;
881 // Set the type of the closure variable if there is one.
885 // Get the block of statements associated with the function.
888 { return this->block_; }
890 // Get the location of the start of the function.
893 { return this->location_; }
895 // Return whether this function is actually a method.
899 // Add a label definition to the function.
901 add_label_definition(const std::string& label_name, source_location);
903 // Add a label reference to a function.
905 add_label_reference(const std::string& label_name);
907 // Warn about labels that are defined but not used.
909 check_labels() const;
911 // Whether this function calls the predeclared recover function.
913 calls_recover() const
914 { return this->calls_recover_; }
916 // Record that this function calls the predeclared recover function.
917 // This is set during the lowering pass.
920 { this->calls_recover_ = true; }
922 // Whether this is a recover thunk function.
924 is_recover_thunk() const
925 { return this->is_recover_thunk_; }
927 // Record that this is a thunk built for a function which calls
930 set_is_recover_thunk()
931 { this->is_recover_thunk_ = true; }
933 // Whether this function already has a recover thunk.
935 has_recover_thunk() const
936 { return this->has_recover_thunk_; }
938 // Record that this function already has a recover thunk.
940 set_has_recover_thunk()
941 { this->has_recover_thunk_ = true; }
943 // Swap with another function. Used only for the thunk which calls
946 swap_for_recover(Function *);
948 // Traverse the tree.
952 // Determine types in the function.
956 // Return the function's decl given an identifier.
958 get_or_make_decl(Gogo*, Named_object*, tree id);
960 // Return the function's decl after it has been built.
964 go_assert(this->fndecl_ != NULL);
965 return this->fndecl_;
968 // Set the function decl to hold a tree of the function code.
970 build_tree(Gogo*, Named_object*);
972 // Get the value to return when not explicitly specified. May also
973 // add statements to execute first to STMT_LIST.
975 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
977 // Get a tree for the variable holding the defer stack.
979 defer_stack(source_location);
981 // Export the function.
983 export_func(Export*, const std::string& name) const;
985 // Export a function with a type.
987 export_func_with_type(Export*, const std::string& name,
988 const Function_type*);
990 // Import a function.
992 import_func(Import*, std::string* pname, Typed_identifier** receiver,
993 Typed_identifier_list** pparameters,
994 Typed_identifier_list** presults, bool* is_varargs);
997 // Type for mapping from label names to Label objects.
998 typedef Unordered_map(std::string, Label*) Labels;
1001 make_receiver_parm_decl(Gogo*, Named_object*, tree);
1004 copy_parm_to_heap(Gogo*, Named_object*, tree);
1007 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1009 typedef std::vector<std::pair<Named_object*,
1010 source_location> > Closure_fields;
1012 // The function's type.
1013 Function_type* type_;
1014 // The enclosing function. This is NULL when there isn't one, which
1015 // is the normal case.
1016 Function* enclosing_;
1017 // The result variables, if any.
1019 // If there is a closure, this is the list of variables which appear
1020 // in the closure. This is created by the parser, and then resolved
1021 // to a real type when we lower parse trees.
1022 Closure_fields closure_fields_;
1023 // The closure variable, passed as a parameter using the static
1024 // chain parameter. Normally NULL.
1025 Named_object* closure_var_;
1026 // The outer block of statements in the function.
1028 // The source location of the start of the function.
1029 source_location location_;
1030 // Labels defined or referenced in the function.
1032 // The function decl.
1034 // The defer stack variable. A pointer to this variable is used to
1035 // distinguish the defer stack for one function from another. This
1036 // is NULL unless we actually need a defer stack.
1037 Temporary_statement* defer_stack_;
1038 // True if the result variables are named.
1039 bool results_are_named_;
1040 // True if this function calls the predeclared recover function.
1041 bool calls_recover_;
1042 // True if this a thunk built for a function which calls recover.
1043 bool is_recover_thunk_;
1044 // True if this function already has a recover thunk.
1045 bool has_recover_thunk_;
1048 // A function declaration.
1050 class Function_declaration
1053 Function_declaration(Function_type* fntype, source_location location)
1054 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1059 { return this->fntype_; }
1063 { return this->location_; }
1067 { return this->asm_name_; }
1069 // Set the assembler name.
1071 set_asm_name(const std::string& asm_name)
1072 { this->asm_name_ = asm_name; }
1074 // Return a decl for the function given an identifier.
1076 get_or_make_decl(Gogo*, Named_object*, tree id);
1078 // Export a function declaration.
1080 export_func(Export* exp, const std::string& name) const
1081 { Function::export_func_with_type(exp, name, this->fntype_); }
1084 // The type of the function.
1085 Function_type* fntype_;
1086 // The location of the declaration.
1087 source_location location_;
1088 // The assembler name: this is the name to use in references to the
1089 // function. This is normally empty.
1090 std::string asm_name_;
1091 // The function decl if needed.
1100 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1101 bool is_receiver, source_location);
1103 // Get the type of the variable.
1110 // Return whether the type is defined yet.
1113 { return this->type_ != NULL; }
1115 // Get the initial value.
1118 { return this->init_; }
1120 // Return whether there are any preinit statements.
1122 has_pre_init() const
1123 { return this->preinit_ != NULL; }
1125 // Return the preinit statements if any.
1128 { return this->preinit_; }
1130 // Return whether this is a global variable.
1133 { return this->is_global_; }
1135 // Return whether this is a function parameter.
1137 is_parameter() const
1138 { return this->is_parameter_; }
1140 // Return whether this is the receiver parameter of a method.
1143 { return this->is_receiver_; }
1145 // Change this parameter to be a receiver. This is used when
1146 // creating the thunks created for functions which call recover.
1150 go_assert(this->is_parameter_);
1151 this->is_receiver_ = true;
1154 // Change this parameter to not be a receiver. This is used when
1155 // creating the thunks created for functions which call recover.
1157 set_is_not_receiver()
1159 go_assert(this->is_parameter_);
1160 this->is_receiver_ = false;
1163 // Return whether this is the varargs parameter of a function.
1165 is_varargs_parameter() const
1166 { return this->is_varargs_parameter_; }
1168 // Whether this variable's address is taken.
1170 is_address_taken() const
1171 { return this->is_address_taken_; }
1173 // Whether this variable should live in the heap.
1176 { return this->is_address_taken_ && !this->is_global_; }
1178 // Get the source location of the variable's declaration.
1181 { return this->location_; }
1183 // Record that this is the varargs parameter of a function.
1185 set_is_varargs_parameter()
1187 go_assert(this->is_parameter_);
1188 this->is_varargs_parameter_ = true;
1191 // Clear the initial value; used for error handling.
1194 { this->init_ = NULL; }
1196 // Set the initial value; used for converting shortcuts.
1198 set_init(Expression* init)
1199 { this->init_ = init; }
1201 // Get the preinit block, a block of statements to be run before the
1202 // initialization expression.
1204 preinit_block(Gogo*);
1206 // Add a statement to be run before the initialization expression.
1207 // This is only used for global variables.
1209 add_preinit_statement(Gogo*, Statement*);
1211 // Lower the initialization expression after parsing is complete.
1213 lower_init_expression(Gogo*, Named_object*);
1215 // A special case: the init value is used only to determine the
1216 // type. This is used if the variable is defined using := with the
1217 // comma-ok form of a map index or a receive expression. The init
1218 // value is actually the map index expression or receive expression.
1219 // We use this because we may not know the right type at parse time.
1221 set_type_from_init_tuple()
1222 { this->type_from_init_tuple_ = true; }
1224 // Another special case: the init value is used only to determine
1225 // the type. This is used if the variable is defined using := with
1226 // a range clause. The init value is the range expression. The
1227 // type of the variable is the index type of the range expression
1228 // (i.e., the first value returned by a range).
1230 set_type_from_range_index()
1231 { this->type_from_range_index_ = true; }
1233 // Another special case: like set_type_from_range_index, but the
1234 // type is the value type of the range expression (i.e., the second
1235 // value returned by a range).
1237 set_type_from_range_value()
1238 { this->type_from_range_value_ = true; }
1240 // Another special case: the init value is used only to determine
1241 // the type. This is used if the variable is defined using := with
1242 // a case in a select statement. The init value is the channel.
1243 // The type of the variable is the channel's element type.
1245 set_type_from_chan_element()
1246 { this->type_from_chan_element_ = true; }
1248 // After we lower the select statement, we once again set the type
1249 // from the initialization expression.
1251 clear_type_from_chan_element()
1253 go_assert(this->type_from_chan_element_);
1254 this->type_from_chan_element_ = false;
1257 // Note that this variable was created for a type switch clause.
1259 set_is_type_switch_var()
1260 { this->is_type_switch_var_ = true; }
1262 // Traverse the initializer expression.
1264 traverse_expression(Traverse*);
1266 // Determine the type of the variable if necessary.
1270 // Note that something takes the address of this variable.
1273 { this->is_address_taken_ = true; }
1275 // Get the backend representation of the variable.
1277 get_backend_variable(Gogo*, Named_object*, const Package*,
1278 const std::string&);
1280 // Get the initial value of the variable as a tree. This may only
1281 // be called if has_pre_init() returns false.
1283 get_init_tree(Gogo*, Named_object* function);
1285 // Return a series of statements which sets the value of the
1286 // variable in DECL. This should only be called is has_pre_init()
1287 // returns true. DECL may be NULL for a sink variable.
1289 get_init_block(Gogo*, Named_object* function, tree decl);
1291 // Export the variable.
1293 export_var(Export*, const std::string& name) const;
1295 // Import a variable.
1297 import_var(Import*, std::string* pname, Type** ptype);
1300 // The type of a tuple.
1302 type_from_tuple(Expression*, bool) const;
1304 // The type of a range.
1306 type_from_range(Expression*, bool, bool) const;
1308 // The element type of a channel.
1310 type_from_chan_element(Expression*, bool) const;
1312 // The variable's type. This may be NULL if the type is set from
1315 // The initial value. This may be NULL if the variable should be
1316 // initialized to the default value for the type.
1318 // Statements to run before the init statement.
1320 // Location of variable definition.
1321 source_location location_;
1322 // Backend representation.
1323 Bvariable* backend_;
1324 // Whether this is a global variable.
1325 bool is_global_ : 1;
1326 // Whether this is a function parameter.
1327 bool is_parameter_ : 1;
1328 // Whether this is the receiver parameter of a method.
1329 bool is_receiver_ : 1;
1330 // Whether this is the varargs parameter of a function.
1331 bool is_varargs_parameter_ : 1;
1332 // Whether something takes the address of this variable.
1333 bool is_address_taken_ : 1;
1334 // True if we have seen this variable in a traversal.
1336 // True if we have lowered the initialization expression.
1337 bool init_is_lowered_ : 1;
1338 // True if init is a tuple used to set the type.
1339 bool type_from_init_tuple_ : 1;
1340 // True if init is a range clause and the type is the index type.
1341 bool type_from_range_index_ : 1;
1342 // True if init is a range clause and the type is the value type.
1343 bool type_from_range_value_ : 1;
1344 // True if init is a channel and the type is the channel's element type.
1345 bool type_from_chan_element_ : 1;
1346 // True if this is a variable created for a type switch case.
1347 bool is_type_switch_var_ : 1;
1348 // True if we have determined types.
1349 bool determined_type_ : 1;
1352 // A variable which is really the name for a function return value, or
1355 class Result_variable
1358 Result_variable(Type* type, Function* function, int index,
1359 source_location location)
1360 : type_(type), function_(function), index_(index), location_(location),
1361 backend_(NULL), is_address_taken_(false)
1364 // Get the type of the result variable.
1367 { return this->type_; }
1369 // Get the function that this is associated with.
1372 { return this->function_; }
1374 // Index in the list of function results.
1377 { return this->index_; }
1379 // The location of the variable definition.
1382 { return this->location_; }
1384 // Whether this variable's address is taken.
1386 is_address_taken() const
1387 { return this->is_address_taken_; }
1389 // Note that something takes the address of this variable.
1392 { this->is_address_taken_ = true; }
1394 // Whether this variable should live in the heap.
1397 { return this->is_address_taken_; }
1399 // Set the function. This is used when cloning functions which call
1402 set_function(Function* function)
1403 { this->function_ = function; }
1405 // Get the backend representation of the variable.
1407 get_backend_variable(Gogo*, Named_object*, const std::string&);
1410 // Type of result variable.
1412 // Function with which this is associated.
1413 Function* function_;
1414 // Index in list of results.
1416 // Where the result variable is defined.
1417 source_location location_;
1418 // Backend representation.
1419 Bvariable* backend_;
1420 // Whether something takes the address of this variable.
1421 bool is_address_taken_;
1424 // The value we keep for a named constant. This lets us hold a type
1425 // and an expression.
1427 class Named_constant
1430 Named_constant(Type* type, Expression* expr, int iota_value,
1431 source_location location)
1432 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1438 { return this->type_; }
1442 { return this->expr_; }
1446 { return this->iota_value_; }
1450 { return this->location_; }
1452 // Whether we are lowering.
1455 { return this->lowering_; }
1457 // Set that we are lowering.
1460 { this->lowering_ = true; }
1462 // We are no longer lowering.
1465 { this->lowering_ = false; }
1467 // Traverse the expression.
1469 traverse_expression(Traverse*);
1471 // Determine the type of the constant if necessary.
1475 // Indicate that we found and reported an error for this constant.
1479 // Export the constant.
1481 export_const(Export*, const std::string& name) const;
1483 // Import a constant.
1485 import_const(Import*, std::string*, Type**, Expression**);
1488 // The type of the constant.
1490 // The expression for the constant.
1492 // If the predeclared constant iota is used in EXPR_, this is the
1493 // value it will have. We do this because at parse time we don't
1494 // know whether the name "iota" will refer to the predeclared
1495 // constant or to something else. We put in the right value in when
1498 // The location of the definition.
1499 source_location location_;
1500 // Whether we are currently lowering this constant.
1504 // A type declaration.
1506 class Type_declaration
1509 Type_declaration(source_location location)
1510 : location_(location), in_function_(NULL), methods_(),
1511 issued_warning_(false)
1514 // Return the location.
1517 { return this->location_; }
1519 // Return the function in which this type is declared. This will
1520 // return NULL for a type declared in global scope.
1523 { return this->in_function_; }
1525 // Set the function in which this type is declared.
1527 set_in_function(Named_object* f)
1528 { this->in_function_ = f; }
1530 // Add a method to this type. This is used when methods are defined
1533 add_method(const std::string& name, Function* function);
1535 // Add a method declaration to this type.
1537 add_method_declaration(const std::string& name, Function_type* type,
1538 source_location location);
1540 // Return whether any methods were defined.
1542 has_methods() const;
1544 // Define methods when the real type is known.
1546 define_methods(Named_type*);
1548 // This is called if we are trying to use this type. It returns
1549 // true if we should issue a warning.
1554 typedef std::vector<Named_object*> Methods;
1556 // The location of the type declaration.
1557 source_location location_;
1558 // If this type is declared in a function, a pointer back to the
1559 // function in which it is defined.
1560 Named_object* in_function_;
1561 // Methods defined before the type is defined.
1563 // True if we have issued a warning about a use of this type
1564 // declaration when it is undefined.
1565 bool issued_warning_;
1568 // An unknown object. These are created by the parser for forward
1569 // references to names which have not been seen before. In a correct
1570 // program, these will always point to a real definition by the end of
1571 // the parse. Because they point to another Named_object, these may
1572 // only be referenced by Unknown_expression objects.
1577 Unknown_name(source_location location)
1578 : location_(location), real_named_object_(NULL)
1581 // Return the location where this name was first seen.
1584 { return this->location_; }
1586 // Return the real named object that this points to, or NULL if it
1587 // was never resolved.
1589 real_named_object() const
1590 { return this->real_named_object_; }
1592 // Set the real named object that this points to.
1594 set_real_named_object(Named_object* no);
1597 // The location where this name was first seen.
1598 source_location location_;
1599 // The real named object when it is known.
1604 // A named object named. This is the result of a declaration. We
1605 // don't use a superclass because they all have to be handled
1613 // An uninitialized Named_object. We should never see this.
1614 NAMED_OBJECT_UNINITIALIZED,
1615 // An unknown name. This is used for forward references. In a
1616 // correct program, these will all be resolved by the end of the
1618 NAMED_OBJECT_UNKNOWN,
1623 // A forward type declaration.
1624 NAMED_OBJECT_TYPE_DECLARATION,
1627 // A result variable in a function.
1628 NAMED_OBJECT_RESULT_VAR,
1629 // The blank identifier--the special variable named _.
1633 // A forward func declaration.
1634 NAMED_OBJECT_FUNC_DECLARATION,
1636 NAMED_OBJECT_PACKAGE
1639 // Return the classification.
1641 classification() const
1642 { return this->classification_; }
1648 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1652 { return this->classification_ == NAMED_OBJECT_CONST; }
1656 { return this->classification_ == NAMED_OBJECT_TYPE; }
1659 is_type_declaration() const
1660 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1664 { return this->classification_ == NAMED_OBJECT_VAR; }
1667 is_result_variable() const
1668 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1672 { return this->classification_ == NAMED_OBJECT_SINK; }
1676 { return this->classification_ == NAMED_OBJECT_FUNC; }
1679 is_function_declaration() const
1680 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1684 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1688 static Named_object*
1689 make_unknown_name(const std::string& name, source_location);
1691 static Named_object*
1692 make_constant(const Typed_identifier&, const Package*, Expression*,
1695 static Named_object*
1696 make_type(const std::string&, const Package*, Type*, source_location);
1698 static Named_object*
1699 make_type_declaration(const std::string&, const Package*, source_location);
1701 static Named_object*
1702 make_variable(const std::string&, const Package*, Variable*);
1704 static Named_object*
1705 make_result_variable(const std::string&, Result_variable*);
1707 static Named_object*
1710 static Named_object*
1711 make_function(const std::string&, const Package*, Function*);
1713 static Named_object*
1714 make_function_declaration(const std::string&, const Package*, Function_type*,
1717 static Named_object*
1718 make_package(const std::string& alias, Package* package);
1725 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1726 return this->u_.unknown_value;
1730 unknown_value() const
1732 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1733 return this->u_.unknown_value;
1739 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1740 return this->u_.const_value;
1743 const Named_constant*
1746 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1747 return this->u_.const_value;
1753 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1754 return this->u_.type_value;
1760 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1761 return this->u_.type_value;
1765 type_declaration_value()
1767 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1768 return this->u_.type_declaration;
1771 const Type_declaration*
1772 type_declaration_value() const
1774 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1775 return this->u_.type_declaration;
1781 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1782 return this->u_.var_value;
1788 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1789 return this->u_.var_value;
1795 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1796 return this->u_.result_var_value;
1799 const Result_variable*
1800 result_var_value() const
1802 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1803 return this->u_.result_var_value;
1809 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1810 return this->u_.func_value;
1816 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1817 return this->u_.func_value;
1820 Function_declaration*
1821 func_declaration_value()
1823 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1824 return this->u_.func_declaration_value;
1827 const Function_declaration*
1828 func_declaration_value() const
1830 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1831 return this->u_.func_declaration_value;
1837 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1838 return this->u_.package_value;
1842 package_value() const
1844 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1845 return this->u_.package_value;
1850 { return this->name_; }
1852 // Return the name to use in an error message. The difference is
1853 // that if this Named_object is defined in a different package, this
1854 // will return PACKAGE.NAME.
1856 message_name() const;
1860 { return this->package_; }
1862 // Resolve an unknown value if possible. This returns the same
1863 // Named_object or a new one.
1867 Named_object* ret = this;
1868 if (this->is_unknown())
1870 Named_object* r = this->unknown_value()->real_named_object();
1880 const Named_object* ret = this;
1881 if (this->is_unknown())
1883 const Named_object* r = this->unknown_value()->real_named_object();
1890 // The location where this object was defined or referenced.
1894 // Convert a variable to the backend representation.
1896 get_backend_variable(Gogo*, Named_object* function);
1898 // Return a tree for the external identifier for this object.
1902 // Return a tree representing this object.
1904 get_tree(Gogo*, Named_object* function);
1906 // Define a type declaration.
1908 set_type_value(Named_type*);
1910 // Define a function declaration.
1912 set_function_value(Function*);
1914 // Declare an unknown name as a type declaration.
1918 // Export this object.
1920 export_named_object(Export*) const;
1923 Named_object(const std::string&, const Package*, Classification);
1925 // The name of the object.
1927 // The package that this object is in. This is NULL if it is in the
1928 // file we are compiling.
1929 const Package* package_;
1930 // The type of object this is.
1931 Classification classification_;
1935 Unknown_name* unknown_value;
1936 Named_constant* const_value;
1937 Named_type* type_value;
1938 Type_declaration* type_declaration;
1939 Variable* var_value;
1940 Result_variable* result_var_value;
1941 Function* func_value;
1942 Function_declaration* func_declaration_value;
1943 Package* package_value;
1945 // The DECL tree for this object if we have already converted it.
1949 // A binding contour. This binds names to objects.
1954 // Type for mapping from names to objects.
1955 typedef Unordered_map(std::string, Named_object*) Contour;
1957 Bindings(Bindings* enclosing);
1959 // Add an unknown name.
1961 add_unknown_name(const std::string& name, source_location location)
1963 return this->add_named_object(Named_object::make_unknown_name(name,
1969 add_constant(const Typed_identifier& tid, const Package* package,
1970 Expression* expr, int iota_value)
1972 return this->add_named_object(Named_object::make_constant(tid, package,
1979 add_type(const std::string& name, const Package* package, Type* type,
1980 source_location location)
1982 return this->add_named_object(Named_object::make_type(name, package, type,
1986 // Add a named type. This is used for builtin types, and to add an
1987 // imported type to the global scope.
1989 add_named_type(Named_type* named_type);
1991 // Add a type declaration.
1993 add_type_declaration(const std::string& name, const Package* package,
1994 source_location location)
1996 Named_object* no = Named_object::make_type_declaration(name, package,
1998 return this->add_named_object(no);
2003 add_variable(const std::string& name, const Package* package,
2006 return this->add_named_object(Named_object::make_variable(name, package,
2010 // Add a result variable.
2012 add_result_variable(const std::string& name, Result_variable* result)
2014 return this->add_named_object(Named_object::make_result_variable(name,
2020 add_function(const std::string& name, const Package*, Function* function);
2022 // Add a function declaration.
2024 add_function_declaration(const std::string& name, const Package* package,
2025 Function_type* type, source_location location);
2027 // Add a package. The location is the location of the import
2030 add_package(const std::string& alias, Package* package)
2032 Named_object* no = Named_object::make_package(alias, package);
2033 return this->add_named_object(no);
2036 // Define a type which was already declared.
2038 define_type(Named_object*, Named_type*);
2040 // Add a method to the list of objects. This is not added to the
2043 add_method(Named_object*);
2045 // Add a named object to this binding.
2047 add_named_object(Named_object* no)
2048 { return this->add_named_object_to_contour(&this->bindings_, no); }
2050 // Clear all names in file scope from the bindings.
2054 // Look up a name in this binding contour and in any enclosing
2055 // binding contours. This returns NULL if the name is not found.
2057 lookup(const std::string&) const;
2059 // Look up a name in this binding contour without looking in any
2060 // enclosing binding contours. Returns NULL if the name is not found.
2062 lookup_local(const std::string&) const;
2066 remove_binding(Named_object*);
2068 // Traverse the tree. See the Traverse class.
2070 traverse(Traverse*, bool is_global);
2072 // Iterate over definitions. This does not include things which
2073 // were only declared.
2075 typedef std::vector<Named_object*>::const_iterator
2076 const_definitions_iterator;
2078 const_definitions_iterator
2079 begin_definitions() const
2080 { return this->named_objects_.begin(); }
2082 const_definitions_iterator
2083 end_definitions() const
2084 { return this->named_objects_.end(); }
2086 // Return the number of definitions.
2088 size_definitions() const
2089 { return this->named_objects_.size(); }
2091 // Return whether there are no definitions.
2093 empty_definitions() const
2094 { return this->named_objects_.empty(); }
2096 // Iterate over declarations. This is everything that has been
2097 // declared, which includes everything which has been defined.
2099 typedef Contour::const_iterator const_declarations_iterator;
2101 const_declarations_iterator
2102 begin_declarations() const
2103 { return this->bindings_.begin(); }
2105 const_declarations_iterator
2106 end_declarations() const
2107 { return this->bindings_.end(); }
2109 // Return the number of declarations.
2111 size_declarations() const
2112 { return this->bindings_.size(); }
2114 // Return whether there are no declarations.
2116 empty_declarations() const
2117 { return this->bindings_.empty(); }
2119 // Return the first declaration.
2122 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2126 add_named_object_to_contour(Contour*, Named_object*);
2129 new_definition(Named_object*, Named_object*);
2131 // Enclosing bindings.
2132 Bindings* enclosing_;
2133 // The list of objects.
2134 std::vector<Named_object*> named_objects_;
2135 // The mapping from names to objects.
2144 Label(const std::string& name)
2145 : name_(name), location_(0), is_used_(false), blabel_(NULL)
2148 // Return the label's name.
2151 { return this->name_; }
2153 // Return whether the label has been defined.
2156 { return this->location_ != 0; }
2158 // Return whether the label has been used.
2161 { return this->is_used_; }
2163 // Record that the label is used.
2166 { this->is_used_ = true; }
2168 // Return the location of the definition.
2171 { return this->location_; }
2173 // Define the label at LOCATION.
2175 define(source_location location)
2177 go_assert(this->location_ == 0);
2178 this->location_ = location;
2181 // Return the backend representation for this label.
2183 get_backend_label(Translate_context*);
2185 // Return an expression for the address of this label. This is used
2186 // to get the return address of a deferred function to see whether
2187 // the function may call recover.
2189 get_addr(Translate_context*, source_location location);
2192 // The name of the label.
2194 // The location of the definition. This is 0 if the label has not
2195 // yet been defined.
2196 source_location location_;
2197 // Whether the label has been used.
2199 // The backend representation.
2203 // An unnamed label. These are used when lowering loops.
2208 Unnamed_label(source_location location)
2209 : location_(location), blabel_(NULL)
2212 // Get the location where the label is defined.
2215 { return this->location_; }
2217 // Set the location where the label is defined.
2219 set_location(source_location location)
2220 { this->location_ = location; }
2222 // Return a statement which defines this label.
2224 get_definition(Translate_context*);
2226 // Return a goto to this label from LOCATION.
2228 get_goto(Translate_context*, source_location location);
2231 // Return the backend representation.
2233 get_blabel(Translate_context*);
2235 // The location where the label is defined.
2236 source_location location_;
2237 // The backend representation of this label.
2241 // An imported package.
2246 Package(const std::string& name, const std::string& unique_prefix,
2247 source_location location);
2249 // The real name of this package. This may be different from the
2250 // name in the associated Named_object if the import statement used
2254 { return this->name_; }
2256 // Return the location of the import statement.
2259 { return this->location_; }
2261 // Get the unique prefix used for all symbols exported from this
2264 unique_prefix() const
2266 go_assert(!this->unique_prefix_.empty());
2267 return this->unique_prefix_;
2270 // The priority of this package. The init function of packages with
2271 // lower priority must be run before the init function of packages
2272 // with higher priority.
2275 { return this->priority_; }
2277 // Set the priority.
2279 set_priority(int priority);
2281 // Return the bindings.
2284 { return this->bindings_; }
2286 // Whether some symbol from the package was used.
2289 { return this->used_; }
2291 // Note that some symbol from this package was used.
2294 { this->used_ = true; }
2296 // Clear the used field for the next file.
2299 { this->used_ = false; }
2301 // Whether this package was imported in the current file.
2304 { return this->is_imported_; }
2306 // Note that this package was imported in the current file.
2309 { this->is_imported_ = true; }
2311 // Clear the imported field for the next file.
2314 { this->is_imported_ = false; }
2316 // Whether this package was imported with a name of "_".
2318 uses_sink_alias() const
2319 { return this->uses_sink_alias_; }
2321 // Note that this package was imported with a name of "_".
2323 set_uses_sink_alias()
2324 { this->uses_sink_alias_ = true; }
2326 // Clear the sink alias field for the next file.
2328 clear_uses_sink_alias()
2329 { this->uses_sink_alias_ = false; }
2331 // Look up a name in the package. Returns NULL if the name is not
2334 lookup(const std::string& name) const
2335 { return this->bindings_->lookup(name); }
2337 // Set the location of the package. This is used if it is seen in a
2338 // different import before it is really imported.
2340 set_location(source_location location)
2341 { this->location_ = location; }
2343 // Add a constant to the package.
2345 add_constant(const Typed_identifier& tid, Expression* expr)
2346 { return this->bindings_->add_constant(tid, this, expr, 0); }
2348 // Add a type to the package.
2350 add_type(const std::string& name, Type* type, source_location location)
2351 { return this->bindings_->add_type(name, this, type, location); }
2353 // Add a type declaration to the package.
2355 add_type_declaration(const std::string& name, source_location location)
2356 { return this->bindings_->add_type_declaration(name, this, location); }
2358 // Add a variable to the package.
2360 add_variable(const std::string& name, Variable* variable)
2361 { return this->bindings_->add_variable(name, this, variable); }
2363 // Add a function declaration to the package.
2365 add_function_declaration(const std::string& name, Function_type* type,
2366 source_location loc)
2367 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2369 // Determine types of constants.
2374 // The real name of this package.
2376 // The unique prefix for all exported global symbols.
2377 std::string unique_prefix_;
2378 // The names in this package.
2379 Bindings* bindings_;
2380 // The priority of this package. A package has a priority higher
2381 // than the priority of all of the packages that it imports. This
2382 // is used to run init functions in the right order.
2384 // The location of the import statement.
2385 source_location location_;
2386 // True if some name from this package was used. This is mutable
2387 // because we can use a package even if we have a const pointer to
2390 // True if this package was imported in the current file.
2392 // True if this package was imported with a name of "_".
2393 bool uses_sink_alias_;
2396 // Return codes for the traversal functions. This is not an enum
2397 // because we want to be able to declare traversal functions in other
2398 // header files without including this one.
2400 // Continue traversal as usual.
2401 const int TRAVERSE_CONTINUE = -1;
2404 const int TRAVERSE_EXIT = 0;
2406 // Continue traversal, but skip components of the current object.
2407 // E.g., if this is returned by Traverse::statement, we do not
2408 // traverse the expressions in the statement even if
2409 // traverse_expressions is set in the traverse_mask.
2410 const int TRAVERSE_SKIP_COMPONENTS = 1;
2412 // This class is used when traversing the parse tree. The caller uses
2413 // a subclass which overrides functions as desired.
2418 // These bitmasks say what to traverse.
2419 static const unsigned int traverse_variables = 0x1;
2420 static const unsigned int traverse_constants = 0x2;
2421 static const unsigned int traverse_functions = 0x4;
2422 static const unsigned int traverse_blocks = 0x8;
2423 static const unsigned int traverse_statements = 0x10;
2424 static const unsigned int traverse_expressions = 0x20;
2425 static const unsigned int traverse_types = 0x40;
2427 Traverse(unsigned int traverse_mask)
2428 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2431 virtual ~Traverse();
2433 // The bitmask of what to traverse.
2435 traverse_mask() const
2436 { return this->traverse_mask_; }
2438 // Record that we are going to traverse a type. This returns true
2439 // if the type has already been seen in this traversal. This is
2440 // required because types, unlike expressions, can form a circular
2443 remember_type(const Type*);
2445 // Record that we are going to see an expression. This returns true
2446 // if the expression has already been seen in this traversal. This
2447 // is only needed for cases where multiple expressions can point to
2450 remember_expression(const Expression*);
2452 // These functions return one of the TRAVERSE codes defined above.
2454 // If traverse_variables is set in the mask, this is called for
2455 // every variable in the tree.
2457 variable(Named_object*);
2459 // If traverse_constants is set in the mask, this is called for
2460 // every named constant in the tree. The bool parameter is true for
2461 // a global constant.
2463 constant(Named_object*, bool);
2465 // If traverse_functions is set in the mask, this is called for
2466 // every function in the tree.
2468 function(Named_object*);
2470 // If traverse_blocks is set in the mask, this is called for every
2471 // block in the tree.
2475 // If traverse_statements is set in the mask, this is called for
2476 // every statement in the tree.
2478 statement(Block*, size_t* index, Statement*);
2480 // If traverse_expressions is set in the mask, this is called for
2481 // every expression in the tree.
2483 expression(Expression**);
2485 // If traverse_types is set in the mask, this is called for every
2486 // type in the tree.
2491 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2492 Type_identical) Types_seen;
2494 typedef Unordered_set(const Expression*) Expressions_seen;
2496 // Bitmask of what sort of objects to traverse.
2497 unsigned int traverse_mask_;
2498 // Types which have been seen in this traversal.
2499 Types_seen* types_seen_;
2500 // Expressions which have been seen in this traversal.
2501 Expressions_seen* expressions_seen_;
2504 // When translating the gogo IR into the backend data structure, this
2505 // is the context we pass down the blocks and statements.
2507 class Translate_context
2510 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2512 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2513 block_(block), bblock_(bblock), is_const_(false)
2520 { return this->gogo_; }
2524 { return this->backend_; }
2528 { return this->function_; }
2532 { return this->block_; }
2536 { return this->bblock_; }
2540 { return this->is_const_; }
2542 // Make a constant context.
2545 { this->is_const_ = true; }
2548 // The IR for the entire compilation unit.
2550 // The generator for the backend data structures.
2552 // The function we are currently translating. NULL if not in a
2553 // function, e.g., the initializer of a global variable.
2554 Named_object* function_;
2555 // The block we are currently translating. NULL if not in a
2558 // The backend representation of the current block. NULL if block_
2561 // Whether this is being evaluated in a constant context. This is
2562 // used for type descriptor initializers.
2566 // Runtime error codes. These must match the values in
2567 // libgo/runtime/go-runtime-error.c.
2569 // Slice index out of bounds: negative or larger than the length of
2571 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2573 // Array index out of bounds.
2574 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2576 // String index out of bounds.
2577 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2579 // Slice slice out of bounds: negative or larger than the length of
2580 // the slice or high bound less than low bound.
2581 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2583 // Array slice out of bounds.
2584 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2586 // String slice out of bounds.
2587 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2589 // Dereference of nil pointer. This is used when there is a
2590 // dereference of a pointer to a very large struct or array, to ensure
2591 // that a gigantic array is not used a proxy to access random memory
2593 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2595 // Slice length or capacity out of bounds in make: negative or
2596 // overflow or length greater than capacity.
2597 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2599 // Map capacity out of bounds in make: negative or overflow.
2600 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2602 // Channel capacity out of bounds in make: negative or overflow.
2603 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2605 // This is used by some of the langhooks.
2606 extern Gogo* go_get_gogo();
2608 // Whether we have seen any errors. FIXME: Replace with a backend
2610 extern bool saw_errors();
2612 #endif // !defined(GO_GOGO_H)