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 // Create trees for implicit builtin functions.
444 define_builtin_function_trees();
446 // Build a call to a builtin function. PDECL should point to a NULL
447 // initialized static pointer which will hold the fndecl. NAME is
448 // the name of the function. NARGS is the number of arguments.
449 // RETTYPE is the return type. It is followed by NARGS pairs of
450 // type and argument (both trees).
452 call_builtin(tree* pdecl, source_location, const char* name, int nargs,
455 // Build a call to the runtime error function.
457 runtime_error(int code, source_location);
459 // Build a builtin struct with a list of fields.
461 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
464 // Mark a function declaration as a builtin library function.
466 mark_fndecl_as_builtin_library(tree fndecl);
468 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
469 // the slice. VALUES points to the values. COUNT is the size,
470 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
473 slice_constructor(tree slice_type_tree, tree values, tree count,
476 // Build a map descriptor.
478 map_descriptor(Map_type*);
480 // Return a tree for the type of a map descriptor. This is struct
481 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
484 map_descriptor_type();
486 // Build a type descriptor for TYPE using INITIALIZER as the type
487 // descriptor. This builds a new decl stored in *PDECL.
489 build_type_descriptor_decl(const Type*, Expression* initializer,
492 // Build required interface method tables.
494 build_interface_method_tables();
496 // Build an interface method table for a type: a list of function
497 // pointers, one for each interface method. This returns a decl.
499 interface_method_table_for_type(const Interface_type*, Named_type*,
502 // Return a tree which allocate SIZE bytes to hold values of type
505 allocate_memory(Type *type, tree size, source_location);
507 // Return a type to use for pointer to const char.
509 const_char_pointer_type_tree();
511 // Build a string constant with the right type.
513 string_constant_tree(const std::string&);
515 // Build a Go string constant. This returns a pointer to the
518 go_string_constant_tree(const std::string&);
520 // Receive a value from a channel.
522 receive_from_channel(tree type_tree, tree channel, bool for_select,
525 // Return a tree for receiving an integer on a channel.
527 receive_as_64bit_integer(tree type, tree channel, bool blocking,
531 // Make a trampoline which calls FNADDR passing CLOSURE.
533 make_trampoline(tree fnaddr, tree closure, source_location);
536 // During parsing, we keep a stack of functions. Each function on
537 // the stack is one that we are currently parsing. For each
538 // function, we keep track of the current stack of blocks.
542 Named_object* function;
543 // The stack of active blocks in the function.
544 std::vector<Block*> blocks;
547 // The stack of functions.
548 typedef std::vector<Open_function> Open_functions;
550 // Set up the built-in unsafe package.
552 import_unsafe(const std::string&, bool is_exported, source_location);
554 // Add a new imported package.
556 add_package(const std::string& real_name, const std::string& alias,
557 const std::string& unique_prefix, source_location location);
559 // Return the current binding contour.
564 current_bindings() const;
566 // Return the current block.
570 // Get the name of the magic initialization function.
574 // Get the decl for the magic initialization function.
576 initialization_function_decl();
578 // Write the magic initialization function.
580 write_initialization_function(tree fndecl, tree init_stmt_list);
582 // Initialize imported packages.
586 // Register variables with the garbage collector.
588 register_gc_vars(const std::vector<Named_object*>&, tree*);
590 // Build a pointer to a Go string constant. This returns a pointer
593 ptr_go_string_constant_tree(const std::string&);
595 // Return the name to use for a type descriptor decl for an unnamed
598 unnamed_type_descriptor_decl_name(const Type* type);
600 // Return the name to use for a type descriptor decl for a type
601 // named NO, defined in IN_FUNCTION.
603 type_descriptor_decl_name(const Named_object* no,
604 const Named_object* in_function);
606 // Where a type descriptor should be defined.
607 enum Type_descriptor_location
609 // Defined in this file.
610 TYPE_DESCRIPTOR_DEFINED,
611 // Defined in some other file.
612 TYPE_DESCRIPTOR_UNDEFINED,
613 // Common definition which may occur in multiple files.
614 TYPE_DESCRIPTOR_COMMON
617 // Return where the decl for TYPE should be defined.
618 Type_descriptor_location
619 type_descriptor_location(const Type* type);
621 // Return the type of a trampoline.
623 trampoline_type_tree();
625 // Type used to map import names to packages.
626 typedef std::map<std::string, Package*> Imports;
628 // Type used to map package names to packages.
629 typedef std::map<std::string, Package*> Packages;
631 // Type used to map special names in the sys package.
632 typedef std::map<std::string, std::string> Sys_names;
634 // Hash table mapping map types to map descriptor decls.
635 typedef Unordered_map_hash(const Map_type*, tree, Type_hash_identical,
636 Type_identical) Map_descriptors;
638 // Map unnamed types to type descriptor decls.
639 typedef Unordered_map_hash(const Type*, tree, Type_hash_identical,
640 Type_identical) Type_descriptor_decls;
642 // The backend generator.
644 // The package we are compiling.
646 // The list of currently open functions during parsing.
647 Open_functions functions_;
648 // The global binding contour. This includes the builtin functions
649 // and the package we are compiling.
651 // Mapping from import file names to packages.
653 // Whether the magic unsafe package was imported.
654 bool imported_unsafe_;
655 // Mapping from package names we have seen to packages. This does
656 // not include the package we are compiling.
658 // Mapping from map types to map descriptors.
659 Map_descriptors* map_descriptors_;
660 // Mapping from unnamed types to type descriptor decls.
661 Type_descriptor_decls* type_descriptor_decls_;
662 // The functions named "init", if there are any.
663 std::vector<Named_object*> init_functions_;
664 // Whether we need a magic initialization function.
666 // The name of the magic initialization function.
667 std::string init_fn_name_;
668 // A list of import control variables for packages that we import.
669 std::set<Import_init> imported_init_fns_;
670 // The unique prefix used for all global symbols.
671 std::string unique_prefix_;
672 // Whether an explicit unique prefix was set by -fgo-prefix.
673 bool unique_prefix_specified_;
674 // A list of interface types defined while parsing.
675 std::vector<Interface_type*> interface_types_;
676 // Whether named types have been converted.
677 bool named_types_are_converted_;
680 // A block of statements.
685 Block(Block* enclosing, source_location);
687 // Return the enclosing block.
690 { return this->enclosing_; }
692 // Return the bindings of the block.
695 { return this->bindings_; }
699 { return this->bindings_; }
701 // Look at the block's statements.
702 const std::vector<Statement*>*
704 { return &this->statements_; }
706 // Return the start location. This is normally the location of the
707 // left curly brace which starts the block.
709 start_location() const
710 { return this->start_location_; }
712 // Return the end location. This is normally the location of the
713 // right curly brace which ends the block.
716 { return this->end_location_; }
718 // Add a statement to the block.
720 add_statement(Statement*);
722 // Add a statement to the front of the block.
724 add_statement_at_front(Statement*);
726 // Replace a statement in a block.
728 replace_statement(size_t index, Statement*);
730 // Add a Statement before statement number INDEX.
732 insert_statement_before(size_t index, Statement*);
734 // Add a Statement after statement number INDEX.
736 insert_statement_after(size_t index, Statement*);
738 // Set the end location of the block.
740 set_end_location(source_location location)
741 { this->end_location_ = location; }
743 // Traverse the tree.
747 // Set final types for unspecified variables and constants.
751 // Return true if execution of this block may fall through to the
754 may_fall_through() const;
756 // Convert the block to the backend representation.
758 get_backend(Translate_context*);
760 // Iterate over statements.
762 typedef std::vector<Statement*>::iterator iterator;
766 { return this->statements_.begin(); }
770 { return this->statements_.end(); }
775 // Statements in the block.
776 std::vector<Statement*> statements_;
779 // Location of start of block.
780 source_location start_location_;
781 // Location of end of block.
782 source_location end_location_;
790 Function(Function_type* type, Function*, Block*, source_location);
792 // Return the function's type.
795 { return this->type_; }
797 // Return the enclosing function if there is one.
800 { return this->enclosing_; }
802 // Set the enclosing function. This is used when building thunks
803 // for functions which call recover.
805 set_enclosing(Function* enclosing)
807 go_assert(this->enclosing_ == NULL);
808 this->enclosing_ = enclosing;
811 // The result variables.
812 typedef std::vector<Named_object*> Results;
814 // Create the result variables in the outer block.
816 create_result_variables(Gogo*);
818 // Update the named result variables when cloning a function which
821 update_result_variables();
823 // Return the result variables.
826 { return this->results_; }
828 // Whether the result variables have names.
830 results_are_named() const
831 { return this->results_are_named_; }
833 // Add a new field to the closure variable.
835 add_closure_field(Named_object* var, source_location loc)
836 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
838 // Whether this function needs a closure.
840 needs_closure() const
841 { return !this->closure_fields_.empty(); }
843 // Return the closure variable, creating it if necessary. This is
844 // passed to the function as a static chain parameter.
848 // Set the closure variable. This is used when building thunks for
849 // functions which call recover.
851 set_closure_var(Named_object* v)
853 go_assert(this->closure_var_ == NULL);
854 this->closure_var_ = v;
857 // Return the variable for a reference to field INDEX in the closure
860 enclosing_var(unsigned int index)
862 go_assert(index < this->closure_fields_.size());
863 return closure_fields_[index].first;
866 // Set the type of the closure variable if there is one.
870 // Get the block of statements associated with the function.
873 { return this->block_; }
875 // Get the location of the start of the function.
878 { return this->location_; }
880 // Return whether this function is actually a method.
884 // Add a label definition to the function.
886 add_label_definition(const std::string& label_name, source_location);
888 // Add a label reference to a function.
890 add_label_reference(const std::string& label_name);
892 // Warn about labels that are defined but not used.
894 check_labels() const;
896 // Whether this function calls the predeclared recover function.
898 calls_recover() const
899 { return this->calls_recover_; }
901 // Record that this function calls the predeclared recover function.
902 // This is set during the lowering pass.
905 { this->calls_recover_ = true; }
907 // Whether this is a recover thunk function.
909 is_recover_thunk() const
910 { return this->is_recover_thunk_; }
912 // Record that this is a thunk built for a function which calls
915 set_is_recover_thunk()
916 { this->is_recover_thunk_ = true; }
918 // Whether this function already has a recover thunk.
920 has_recover_thunk() const
921 { return this->has_recover_thunk_; }
923 // Record that this function already has a recover thunk.
925 set_has_recover_thunk()
926 { this->has_recover_thunk_ = true; }
928 // Swap with another function. Used only for the thunk which calls
931 swap_for_recover(Function *);
933 // Traverse the tree.
937 // Determine types in the function.
941 // Return the function's decl given an identifier.
943 get_or_make_decl(Gogo*, Named_object*, tree id);
945 // Return the function's decl after it has been built.
949 go_assert(this->fndecl_ != NULL);
950 return this->fndecl_;
953 // Set the function decl to hold a tree of the function code.
955 build_tree(Gogo*, Named_object*);
957 // Get the value to return when not explicitly specified. May also
958 // add statements to execute first to STMT_LIST.
960 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
962 // Get a tree for the variable holding the defer stack.
964 defer_stack(source_location);
966 // Export the function.
968 export_func(Export*, const std::string& name) const;
970 // Export a function with a type.
972 export_func_with_type(Export*, const std::string& name,
973 const Function_type*);
975 // Import a function.
977 import_func(Import*, std::string* pname, Typed_identifier** receiver,
978 Typed_identifier_list** pparameters,
979 Typed_identifier_list** presults, bool* is_varargs);
982 // Type for mapping from label names to Label objects.
983 typedef Unordered_map(std::string, Label*) Labels;
986 make_receiver_parm_decl(Gogo*, Named_object*, tree);
989 copy_parm_to_heap(Gogo*, Named_object*, tree);
992 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
994 typedef std::vector<std::pair<Named_object*,
995 source_location> > Closure_fields;
997 // The function's type.
998 Function_type* type_;
999 // The enclosing function. This is NULL when there isn't one, which
1000 // is the normal case.
1001 Function* enclosing_;
1002 // The result variables, if any.
1004 // If there is a closure, this is the list of variables which appear
1005 // in the closure. This is created by the parser, and then resolved
1006 // to a real type when we lower parse trees.
1007 Closure_fields closure_fields_;
1008 // The closure variable, passed as a parameter using the static
1009 // chain parameter. Normally NULL.
1010 Named_object* closure_var_;
1011 // The outer block of statements in the function.
1013 // The source location of the start of the function.
1014 source_location location_;
1015 // Labels defined or referenced in the function.
1017 // The function decl.
1019 // The defer stack variable. A pointer to this variable is used to
1020 // distinguish the defer stack for one function from another. This
1021 // is NULL unless we actually need a defer stack.
1022 Temporary_statement* defer_stack_;
1023 // True if the result variables are named.
1024 bool results_are_named_;
1025 // True if this function calls the predeclared recover function.
1026 bool calls_recover_;
1027 // True if this a thunk built for a function which calls recover.
1028 bool is_recover_thunk_;
1029 // True if this function already has a recover thunk.
1030 bool has_recover_thunk_;
1033 // A function declaration.
1035 class Function_declaration
1038 Function_declaration(Function_type* fntype, source_location location)
1039 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1044 { return this->fntype_; }
1048 { return this->location_; }
1052 { return this->asm_name_; }
1054 // Set the assembler name.
1056 set_asm_name(const std::string& asm_name)
1057 { this->asm_name_ = asm_name; }
1059 // Return a decl for the function given an identifier.
1061 get_or_make_decl(Gogo*, Named_object*, tree id);
1063 // Export a function declaration.
1065 export_func(Export* exp, const std::string& name) const
1066 { Function::export_func_with_type(exp, name, this->fntype_); }
1069 // The type of the function.
1070 Function_type* fntype_;
1071 // The location of the declaration.
1072 source_location location_;
1073 // The assembler name: this is the name to use in references to the
1074 // function. This is normally empty.
1075 std::string asm_name_;
1076 // The function decl if needed.
1085 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1086 bool is_receiver, source_location);
1088 // Get the type of the variable.
1095 // Return whether the type is defined yet.
1098 { return this->type_ != NULL; }
1100 // Get the initial value.
1103 { return this->init_; }
1105 // Return whether there are any preinit statements.
1107 has_pre_init() const
1108 { return this->preinit_ != NULL; }
1110 // Return the preinit statements if any.
1113 { return this->preinit_; }
1115 // Return whether this is a global variable.
1118 { return this->is_global_; }
1120 // Return whether this is a function parameter.
1122 is_parameter() const
1123 { return this->is_parameter_; }
1125 // Return whether this is the receiver parameter of a method.
1128 { return this->is_receiver_; }
1130 // Change this parameter to be a receiver. This is used when
1131 // creating the thunks created for functions which call recover.
1135 go_assert(this->is_parameter_);
1136 this->is_receiver_ = true;
1139 // Change this parameter to not be a receiver. This is used when
1140 // creating the thunks created for functions which call recover.
1142 set_is_not_receiver()
1144 go_assert(this->is_parameter_);
1145 this->is_receiver_ = false;
1148 // Return whether this is the varargs parameter of a function.
1150 is_varargs_parameter() const
1151 { return this->is_varargs_parameter_; }
1153 // Whether this variable's address is taken.
1155 is_address_taken() const
1156 { return this->is_address_taken_; }
1158 // Whether this variable should live in the heap.
1161 { return this->is_address_taken_ && !this->is_global_; }
1163 // Note that something takes the address of this variable.
1166 { this->is_address_taken_ = true; }
1168 // Return whether the address is taken but does not escape.
1170 is_non_escaping_address_taken() const
1171 { return this->is_non_escaping_address_taken_; }
1173 // Note that something takes the address of this variable such that
1174 // the address does not escape the function.
1176 set_non_escaping_address_taken()
1177 { this->is_non_escaping_address_taken_ = true; }
1179 // Get the source location of the variable's declaration.
1182 { return this->location_; }
1184 // Record that this is the varargs parameter of a function.
1186 set_is_varargs_parameter()
1188 go_assert(this->is_parameter_);
1189 this->is_varargs_parameter_ = true;
1192 // Clear the initial value; used for error handling.
1195 { this->init_ = NULL; }
1197 // Set the initial value; used for converting shortcuts.
1199 set_init(Expression* init)
1200 { this->init_ = init; }
1202 // Get the preinit block, a block of statements to be run before the
1203 // initialization expression.
1205 preinit_block(Gogo*);
1207 // Add a statement to be run before the initialization expression.
1208 // This is only used for global variables.
1210 add_preinit_statement(Gogo*, Statement*);
1212 // Lower the initialization expression after parsing is complete.
1214 lower_init_expression(Gogo*, Named_object*);
1216 // A special case: the init value is used only to determine the
1217 // type. This is used if the variable is defined using := with the
1218 // comma-ok form of a map index or a receive expression. The init
1219 // value is actually the map index expression or receive expression.
1220 // We use this because we may not know the right type at parse time.
1222 set_type_from_init_tuple()
1223 { this->type_from_init_tuple_ = true; }
1225 // Another special case: the init value is used only to determine
1226 // the type. This is used if the variable is defined using := with
1227 // a range clause. The init value is the range expression. The
1228 // type of the variable is the index type of the range expression
1229 // (i.e., the first value returned by a range).
1231 set_type_from_range_index()
1232 { this->type_from_range_index_ = true; }
1234 // Another special case: like set_type_from_range_index, but the
1235 // type is the value type of the range expression (i.e., the second
1236 // value returned by a range).
1238 set_type_from_range_value()
1239 { this->type_from_range_value_ = true; }
1241 // Another special case: the init value is used only to determine
1242 // the type. This is used if the variable is defined using := with
1243 // a case in a select statement. The init value is the channel.
1244 // The type of the variable is the channel's element type.
1246 set_type_from_chan_element()
1247 { this->type_from_chan_element_ = true; }
1249 // After we lower the select statement, we once again set the type
1250 // from the initialization expression.
1252 clear_type_from_chan_element()
1254 go_assert(this->type_from_chan_element_);
1255 this->type_from_chan_element_ = false;
1258 // Note that this variable was created for a type switch clause.
1260 set_is_type_switch_var()
1261 { this->is_type_switch_var_ = true; }
1263 // Traverse the initializer expression.
1265 traverse_expression(Traverse*);
1267 // Determine the type of the variable if necessary.
1271 // Get the backend representation of the variable.
1273 get_backend_variable(Gogo*, Named_object*, const Package*,
1274 const std::string&);
1276 // Get the initial value of the variable as a tree. This may only
1277 // be called if has_pre_init() returns false.
1279 get_init_tree(Gogo*, Named_object* function);
1281 // Return a series of statements which sets the value of the
1282 // variable in DECL. This should only be called is has_pre_init()
1283 // returns true. DECL may be NULL for a sink variable.
1285 get_init_block(Gogo*, Named_object* function, tree decl);
1287 // Export the variable.
1289 export_var(Export*, const std::string& name) const;
1291 // Import a variable.
1293 import_var(Import*, std::string* pname, Type** ptype);
1296 // The type of a tuple.
1298 type_from_tuple(Expression*, bool) const;
1300 // The type of a range.
1302 type_from_range(Expression*, bool, bool) const;
1304 // The element type of a channel.
1306 type_from_chan_element(Expression*, bool) const;
1308 // The variable's type. This may be NULL if the type is set from
1311 // The initial value. This may be NULL if the variable should be
1312 // initialized to the default value for the type.
1314 // Statements to run before the init statement.
1316 // Location of variable definition.
1317 source_location location_;
1318 // Backend representation.
1319 Bvariable* backend_;
1320 // Whether this is a global variable.
1321 bool is_global_ : 1;
1322 // Whether this is a function parameter.
1323 bool is_parameter_ : 1;
1324 // Whether this is the receiver parameter of a method.
1325 bool is_receiver_ : 1;
1326 // Whether this is the varargs parameter of a function.
1327 bool is_varargs_parameter_ : 1;
1328 // Whether something takes the address of this variable. For a
1329 // local variable this implies that the variable has to be on the
1331 bool is_address_taken_ : 1;
1332 // Whether something takes the address of this variable such that
1333 // the address does not escape the function.
1334 bool is_non_escaping_address_taken_ : 1;
1335 // True if we have seen this variable in a traversal.
1337 // True if we have lowered the initialization expression.
1338 bool init_is_lowered_ : 1;
1339 // True if init is a tuple used to set the type.
1340 bool type_from_init_tuple_ : 1;
1341 // True if init is a range clause and the type is the index type.
1342 bool type_from_range_index_ : 1;
1343 // True if init is a range clause and the type is the value type.
1344 bool type_from_range_value_ : 1;
1345 // True if init is a channel and the type is the channel's element type.
1346 bool type_from_chan_element_ : 1;
1347 // True if this is a variable created for a type switch case.
1348 bool is_type_switch_var_ : 1;
1349 // True if we have determined types.
1350 bool determined_type_ : 1;
1353 // A variable which is really the name for a function return value, or
1356 class Result_variable
1359 Result_variable(Type* type, Function* function, int index,
1360 source_location location)
1361 : type_(type), function_(function), index_(index), location_(location),
1362 backend_(NULL), is_address_taken_(false),
1363 is_non_escaping_address_taken_(false)
1366 // Get the type of the result variable.
1369 { return this->type_; }
1371 // Get the function that this is associated with.
1374 { return this->function_; }
1376 // Index in the list of function results.
1379 { return this->index_; }
1381 // The location of the variable definition.
1384 { return this->location_; }
1386 // Whether this variable's address is taken.
1388 is_address_taken() const
1389 { return this->is_address_taken_; }
1391 // Note that something takes the address of this variable.
1394 { this->is_address_taken_ = true; }
1396 // Return whether the address is taken but does not escape.
1398 is_non_escaping_address_taken() const
1399 { return this->is_non_escaping_address_taken_; }
1401 // Note that something takes the address of this variable such that
1402 // the address does not escape the function.
1404 set_non_escaping_address_taken()
1405 { this->is_non_escaping_address_taken_ = true; }
1407 // Whether this variable should live in the heap.
1410 { return this->is_address_taken_; }
1412 // Set the function. This is used when cloning functions which call
1415 set_function(Function* function)
1416 { this->function_ = function; }
1418 // Get the backend representation of the variable.
1420 get_backend_variable(Gogo*, Named_object*, const std::string&);
1423 // Type of result variable.
1425 // Function with which this is associated.
1426 Function* function_;
1427 // Index in list of results.
1429 // Where the result variable is defined.
1430 source_location location_;
1431 // Backend representation.
1432 Bvariable* backend_;
1433 // Whether something takes the address of this variable.
1434 bool is_address_taken_;
1435 // Whether something takes the address of this variable such that
1436 // the address does not escape the function.
1437 bool is_non_escaping_address_taken_;
1440 // The value we keep for a named constant. This lets us hold a type
1441 // and an expression.
1443 class Named_constant
1446 Named_constant(Type* type, Expression* expr, int iota_value,
1447 source_location location)
1448 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1454 { return this->type_; }
1458 { return this->expr_; }
1462 { return this->iota_value_; }
1466 { return this->location_; }
1468 // Whether we are lowering.
1471 { return this->lowering_; }
1473 // Set that we are lowering.
1476 { this->lowering_ = true; }
1478 // We are no longer lowering.
1481 { this->lowering_ = false; }
1483 // Traverse the expression.
1485 traverse_expression(Traverse*);
1487 // Determine the type of the constant if necessary.
1491 // Indicate that we found and reported an error for this constant.
1495 // Export the constant.
1497 export_const(Export*, const std::string& name) const;
1499 // Import a constant.
1501 import_const(Import*, std::string*, Type**, Expression**);
1504 // The type of the constant.
1506 // The expression for the constant.
1508 // If the predeclared constant iota is used in EXPR_, this is the
1509 // value it will have. We do this because at parse time we don't
1510 // know whether the name "iota" will refer to the predeclared
1511 // constant or to something else. We put in the right value in when
1514 // The location of the definition.
1515 source_location location_;
1516 // Whether we are currently lowering this constant.
1520 // A type declaration.
1522 class Type_declaration
1525 Type_declaration(source_location location)
1526 : location_(location), in_function_(NULL), methods_(),
1527 issued_warning_(false)
1530 // Return the location.
1533 { return this->location_; }
1535 // Return the function in which this type is declared. This will
1536 // return NULL for a type declared in global scope.
1539 { return this->in_function_; }
1541 // Set the function in which this type is declared.
1543 set_in_function(Named_object* f)
1544 { this->in_function_ = f; }
1546 // Add a method to this type. This is used when methods are defined
1549 add_method(const std::string& name, Function* function);
1551 // Add a method declaration to this type.
1553 add_method_declaration(const std::string& name, Function_type* type,
1554 source_location location);
1556 // Return whether any methods were defined.
1558 has_methods() const;
1560 // Define methods when the real type is known.
1562 define_methods(Named_type*);
1564 // This is called if we are trying to use this type. It returns
1565 // true if we should issue a warning.
1570 typedef std::vector<Named_object*> Methods;
1572 // The location of the type declaration.
1573 source_location location_;
1574 // If this type is declared in a function, a pointer back to the
1575 // function in which it is defined.
1576 Named_object* in_function_;
1577 // Methods defined before the type is defined.
1579 // True if we have issued a warning about a use of this type
1580 // declaration when it is undefined.
1581 bool issued_warning_;
1584 // An unknown object. These are created by the parser for forward
1585 // references to names which have not been seen before. In a correct
1586 // program, these will always point to a real definition by the end of
1587 // the parse. Because they point to another Named_object, these may
1588 // only be referenced by Unknown_expression objects.
1593 Unknown_name(source_location location)
1594 : location_(location), real_named_object_(NULL)
1597 // Return the location where this name was first seen.
1600 { return this->location_; }
1602 // Return the real named object that this points to, or NULL if it
1603 // was never resolved.
1605 real_named_object() const
1606 { return this->real_named_object_; }
1608 // Set the real named object that this points to.
1610 set_real_named_object(Named_object* no);
1613 // The location where this name was first seen.
1614 source_location location_;
1615 // The real named object when it is known.
1620 // A named object named. This is the result of a declaration. We
1621 // don't use a superclass because they all have to be handled
1629 // An uninitialized Named_object. We should never see this.
1630 NAMED_OBJECT_UNINITIALIZED,
1631 // An unknown name. This is used for forward references. In a
1632 // correct program, these will all be resolved by the end of the
1634 NAMED_OBJECT_UNKNOWN,
1639 // A forward type declaration.
1640 NAMED_OBJECT_TYPE_DECLARATION,
1643 // A result variable in a function.
1644 NAMED_OBJECT_RESULT_VAR,
1645 // The blank identifier--the special variable named _.
1649 // A forward func declaration.
1650 NAMED_OBJECT_FUNC_DECLARATION,
1652 NAMED_OBJECT_PACKAGE
1655 // Return the classification.
1657 classification() const
1658 { return this->classification_; }
1664 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1668 { return this->classification_ == NAMED_OBJECT_CONST; }
1672 { return this->classification_ == NAMED_OBJECT_TYPE; }
1675 is_type_declaration() const
1676 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1680 { return this->classification_ == NAMED_OBJECT_VAR; }
1683 is_result_variable() const
1684 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1688 { return this->classification_ == NAMED_OBJECT_SINK; }
1692 { return this->classification_ == NAMED_OBJECT_FUNC; }
1695 is_function_declaration() const
1696 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1700 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1704 static Named_object*
1705 make_unknown_name(const std::string& name, source_location);
1707 static Named_object*
1708 make_constant(const Typed_identifier&, const Package*, Expression*,
1711 static Named_object*
1712 make_type(const std::string&, const Package*, Type*, source_location);
1714 static Named_object*
1715 make_type_declaration(const std::string&, const Package*, source_location);
1717 static Named_object*
1718 make_variable(const std::string&, const Package*, Variable*);
1720 static Named_object*
1721 make_result_variable(const std::string&, Result_variable*);
1723 static Named_object*
1726 static Named_object*
1727 make_function(const std::string&, const Package*, Function*);
1729 static Named_object*
1730 make_function_declaration(const std::string&, const Package*, Function_type*,
1733 static Named_object*
1734 make_package(const std::string& alias, Package* package);
1741 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1742 return this->u_.unknown_value;
1746 unknown_value() const
1748 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1749 return this->u_.unknown_value;
1755 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1756 return this->u_.const_value;
1759 const Named_constant*
1762 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1763 return this->u_.const_value;
1769 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1770 return this->u_.type_value;
1776 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1777 return this->u_.type_value;
1781 type_declaration_value()
1783 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1784 return this->u_.type_declaration;
1787 const Type_declaration*
1788 type_declaration_value() const
1790 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1791 return this->u_.type_declaration;
1797 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1798 return this->u_.var_value;
1804 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1805 return this->u_.var_value;
1811 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1812 return this->u_.result_var_value;
1815 const Result_variable*
1816 result_var_value() const
1818 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1819 return this->u_.result_var_value;
1825 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1826 return this->u_.func_value;
1832 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1833 return this->u_.func_value;
1836 Function_declaration*
1837 func_declaration_value()
1839 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1840 return this->u_.func_declaration_value;
1843 const Function_declaration*
1844 func_declaration_value() const
1846 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1847 return this->u_.func_declaration_value;
1853 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1854 return this->u_.package_value;
1858 package_value() const
1860 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1861 return this->u_.package_value;
1866 { return this->name_; }
1868 // Return the name to use in an error message. The difference is
1869 // that if this Named_object is defined in a different package, this
1870 // will return PACKAGE.NAME.
1872 message_name() const;
1876 { return this->package_; }
1878 // Resolve an unknown value if possible. This returns the same
1879 // Named_object or a new one.
1883 Named_object* ret = this;
1884 if (this->is_unknown())
1886 Named_object* r = this->unknown_value()->real_named_object();
1896 const Named_object* ret = this;
1897 if (this->is_unknown())
1899 const Named_object* r = this->unknown_value()->real_named_object();
1906 // The location where this object was defined or referenced.
1910 // Convert a variable to the backend representation.
1912 get_backend_variable(Gogo*, Named_object* function);
1914 // Return a tree for the external identifier for this object.
1918 // Return a tree representing this object.
1920 get_tree(Gogo*, Named_object* function);
1922 // Define a type declaration.
1924 set_type_value(Named_type*);
1926 // Define a function declaration.
1928 set_function_value(Function*);
1930 // Declare an unknown name as a type declaration.
1934 // Export this object.
1936 export_named_object(Export*) const;
1939 Named_object(const std::string&, const Package*, Classification);
1941 // The name of the object.
1943 // The package that this object is in. This is NULL if it is in the
1944 // file we are compiling.
1945 const Package* package_;
1946 // The type of object this is.
1947 Classification classification_;
1951 Unknown_name* unknown_value;
1952 Named_constant* const_value;
1953 Named_type* type_value;
1954 Type_declaration* type_declaration;
1955 Variable* var_value;
1956 Result_variable* result_var_value;
1957 Function* func_value;
1958 Function_declaration* func_declaration_value;
1959 Package* package_value;
1961 // The DECL tree for this object if we have already converted it.
1965 // A binding contour. This binds names to objects.
1970 // Type for mapping from names to objects.
1971 typedef Unordered_map(std::string, Named_object*) Contour;
1973 Bindings(Bindings* enclosing);
1975 // Add an unknown name.
1977 add_unknown_name(const std::string& name, source_location location)
1979 return this->add_named_object(Named_object::make_unknown_name(name,
1985 add_constant(const Typed_identifier& tid, const Package* package,
1986 Expression* expr, int iota_value)
1988 return this->add_named_object(Named_object::make_constant(tid, package,
1995 add_type(const std::string& name, const Package* package, Type* type,
1996 source_location location)
1998 return this->add_named_object(Named_object::make_type(name, package, type,
2002 // Add a named type. This is used for builtin types, and to add an
2003 // imported type to the global scope.
2005 add_named_type(Named_type* named_type);
2007 // Add a type declaration.
2009 add_type_declaration(const std::string& name, const Package* package,
2010 source_location location)
2012 Named_object* no = Named_object::make_type_declaration(name, package,
2014 return this->add_named_object(no);
2019 add_variable(const std::string& name, const Package* package,
2022 return this->add_named_object(Named_object::make_variable(name, package,
2026 // Add a result variable.
2028 add_result_variable(const std::string& name, Result_variable* result)
2030 return this->add_named_object(Named_object::make_result_variable(name,
2036 add_function(const std::string& name, const Package*, Function* function);
2038 // Add a function declaration.
2040 add_function_declaration(const std::string& name, const Package* package,
2041 Function_type* type, source_location location);
2043 // Add a package. The location is the location of the import
2046 add_package(const std::string& alias, Package* package)
2048 Named_object* no = Named_object::make_package(alias, package);
2049 return this->add_named_object(no);
2052 // Define a type which was already declared.
2054 define_type(Named_object*, Named_type*);
2056 // Add a method to the list of objects. This is not added to the
2059 add_method(Named_object*);
2061 // Add a named object to this binding.
2063 add_named_object(Named_object* no)
2064 { return this->add_named_object_to_contour(&this->bindings_, no); }
2066 // Clear all names in file scope from the bindings.
2070 // Look up a name in this binding contour and in any enclosing
2071 // binding contours. This returns NULL if the name is not found.
2073 lookup(const std::string&) const;
2075 // Look up a name in this binding contour without looking in any
2076 // enclosing binding contours. Returns NULL if the name is not found.
2078 lookup_local(const std::string&) const;
2082 remove_binding(Named_object*);
2084 // Traverse the tree. See the Traverse class.
2086 traverse(Traverse*, bool is_global);
2088 // Iterate over definitions. This does not include things which
2089 // were only declared.
2091 typedef std::vector<Named_object*>::const_iterator
2092 const_definitions_iterator;
2094 const_definitions_iterator
2095 begin_definitions() const
2096 { return this->named_objects_.begin(); }
2098 const_definitions_iterator
2099 end_definitions() const
2100 { return this->named_objects_.end(); }
2102 // Return the number of definitions.
2104 size_definitions() const
2105 { return this->named_objects_.size(); }
2107 // Return whether there are no definitions.
2109 empty_definitions() const
2110 { return this->named_objects_.empty(); }
2112 // Iterate over declarations. This is everything that has been
2113 // declared, which includes everything which has been defined.
2115 typedef Contour::const_iterator const_declarations_iterator;
2117 const_declarations_iterator
2118 begin_declarations() const
2119 { return this->bindings_.begin(); }
2121 const_declarations_iterator
2122 end_declarations() const
2123 { return this->bindings_.end(); }
2125 // Return the number of declarations.
2127 size_declarations() const
2128 { return this->bindings_.size(); }
2130 // Return whether there are no declarations.
2132 empty_declarations() const
2133 { return this->bindings_.empty(); }
2135 // Return the first declaration.
2138 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2142 add_named_object_to_contour(Contour*, Named_object*);
2145 new_definition(Named_object*, Named_object*);
2147 // Enclosing bindings.
2148 Bindings* enclosing_;
2149 // The list of objects.
2150 std::vector<Named_object*> named_objects_;
2151 // The mapping from names to objects.
2160 Label(const std::string& name)
2161 : name_(name), location_(0), is_used_(false), blabel_(NULL)
2164 // Return the label's name.
2167 { return this->name_; }
2169 // Return whether the label has been defined.
2172 { return this->location_ != 0; }
2174 // Return whether the label has been used.
2177 { return this->is_used_; }
2179 // Record that the label is used.
2182 { this->is_used_ = true; }
2184 // Return the location of the definition.
2187 { return this->location_; }
2189 // Define the label at LOCATION.
2191 define(source_location location)
2193 go_assert(this->location_ == 0);
2194 this->location_ = location;
2197 // Return the backend representation for this label.
2199 get_backend_label(Translate_context*);
2201 // Return an expression for the address of this label. This is used
2202 // to get the return address of a deferred function to see whether
2203 // the function may call recover.
2205 get_addr(Translate_context*, source_location location);
2208 // The name of the label.
2210 // The location of the definition. This is 0 if the label has not
2211 // yet been defined.
2212 source_location location_;
2213 // Whether the label has been used.
2215 // The backend representation.
2219 // An unnamed label. These are used when lowering loops.
2224 Unnamed_label(source_location location)
2225 : location_(location), blabel_(NULL)
2228 // Get the location where the label is defined.
2231 { return this->location_; }
2233 // Set the location where the label is defined.
2235 set_location(source_location location)
2236 { this->location_ = location; }
2238 // Return a statement which defines this label.
2240 get_definition(Translate_context*);
2242 // Return a goto to this label from LOCATION.
2244 get_goto(Translate_context*, source_location location);
2247 // Return the backend representation.
2249 get_blabel(Translate_context*);
2251 // The location where the label is defined.
2252 source_location location_;
2253 // The backend representation of this label.
2257 // An imported package.
2262 Package(const std::string& name, const std::string& unique_prefix,
2263 source_location location);
2265 // The real name of this package. This may be different from the
2266 // name in the associated Named_object if the import statement used
2270 { return this->name_; }
2272 // Return the location of the import statement.
2275 { return this->location_; }
2277 // Get the unique prefix used for all symbols exported from this
2280 unique_prefix() const
2282 go_assert(!this->unique_prefix_.empty());
2283 return this->unique_prefix_;
2286 // The priority of this package. The init function of packages with
2287 // lower priority must be run before the init function of packages
2288 // with higher priority.
2291 { return this->priority_; }
2293 // Set the priority.
2295 set_priority(int priority);
2297 // Return the bindings.
2300 { return this->bindings_; }
2302 // Whether some symbol from the package was used.
2305 { return this->used_; }
2307 // Note that some symbol from this package was used.
2310 { this->used_ = true; }
2312 // Clear the used field for the next file.
2315 { this->used_ = false; }
2317 // Whether this package was imported in the current file.
2320 { return this->is_imported_; }
2322 // Note that this package was imported in the current file.
2325 { this->is_imported_ = true; }
2327 // Clear the imported field for the next file.
2330 { this->is_imported_ = false; }
2332 // Whether this package was imported with a name of "_".
2334 uses_sink_alias() const
2335 { return this->uses_sink_alias_; }
2337 // Note that this package was imported with a name of "_".
2339 set_uses_sink_alias()
2340 { this->uses_sink_alias_ = true; }
2342 // Clear the sink alias field for the next file.
2344 clear_uses_sink_alias()
2345 { this->uses_sink_alias_ = false; }
2347 // Look up a name in the package. Returns NULL if the name is not
2350 lookup(const std::string& name) const
2351 { return this->bindings_->lookup(name); }
2353 // Set the location of the package. This is used if it is seen in a
2354 // different import before it is really imported.
2356 set_location(source_location location)
2357 { this->location_ = location; }
2359 // Add a constant to the package.
2361 add_constant(const Typed_identifier& tid, Expression* expr)
2362 { return this->bindings_->add_constant(tid, this, expr, 0); }
2364 // Add a type to the package.
2366 add_type(const std::string& name, Type* type, source_location location)
2367 { return this->bindings_->add_type(name, this, type, location); }
2369 // Add a type declaration to the package.
2371 add_type_declaration(const std::string& name, source_location location)
2372 { return this->bindings_->add_type_declaration(name, this, location); }
2374 // Add a variable to the package.
2376 add_variable(const std::string& name, Variable* variable)
2377 { return this->bindings_->add_variable(name, this, variable); }
2379 // Add a function declaration to the package.
2381 add_function_declaration(const std::string& name, Function_type* type,
2382 source_location loc)
2383 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2385 // Determine types of constants.
2390 // The real name of this package.
2392 // The unique prefix for all exported global symbols.
2393 std::string unique_prefix_;
2394 // The names in this package.
2395 Bindings* bindings_;
2396 // The priority of this package. A package has a priority higher
2397 // than the priority of all of the packages that it imports. This
2398 // is used to run init functions in the right order.
2400 // The location of the import statement.
2401 source_location location_;
2402 // True if some name from this package was used. This is mutable
2403 // because we can use a package even if we have a const pointer to
2406 // True if this package was imported in the current file.
2408 // True if this package was imported with a name of "_".
2409 bool uses_sink_alias_;
2412 // Return codes for the traversal functions. This is not an enum
2413 // because we want to be able to declare traversal functions in other
2414 // header files without including this one.
2416 // Continue traversal as usual.
2417 const int TRAVERSE_CONTINUE = -1;
2420 const int TRAVERSE_EXIT = 0;
2422 // Continue traversal, but skip components of the current object.
2423 // E.g., if this is returned by Traverse::statement, we do not
2424 // traverse the expressions in the statement even if
2425 // traverse_expressions is set in the traverse_mask.
2426 const int TRAVERSE_SKIP_COMPONENTS = 1;
2428 // This class is used when traversing the parse tree. The caller uses
2429 // a subclass which overrides functions as desired.
2434 // These bitmasks say what to traverse.
2435 static const unsigned int traverse_variables = 0x1;
2436 static const unsigned int traverse_constants = 0x2;
2437 static const unsigned int traverse_functions = 0x4;
2438 static const unsigned int traverse_blocks = 0x8;
2439 static const unsigned int traverse_statements = 0x10;
2440 static const unsigned int traverse_expressions = 0x20;
2441 static const unsigned int traverse_types = 0x40;
2443 Traverse(unsigned int traverse_mask)
2444 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2447 virtual ~Traverse();
2449 // The bitmask of what to traverse.
2451 traverse_mask() const
2452 { return this->traverse_mask_; }
2454 // Record that we are going to traverse a type. This returns true
2455 // if the type has already been seen in this traversal. This is
2456 // required because types, unlike expressions, can form a circular
2459 remember_type(const Type*);
2461 // Record that we are going to see an expression. This returns true
2462 // if the expression has already been seen in this traversal. This
2463 // is only needed for cases where multiple expressions can point to
2466 remember_expression(const Expression*);
2468 // These functions return one of the TRAVERSE codes defined above.
2470 // If traverse_variables is set in the mask, this is called for
2471 // every variable in the tree.
2473 variable(Named_object*);
2475 // If traverse_constants is set in the mask, this is called for
2476 // every named constant in the tree. The bool parameter is true for
2477 // a global constant.
2479 constant(Named_object*, bool);
2481 // If traverse_functions is set in the mask, this is called for
2482 // every function in the tree.
2484 function(Named_object*);
2486 // If traverse_blocks is set in the mask, this is called for every
2487 // block in the tree.
2491 // If traverse_statements is set in the mask, this is called for
2492 // every statement in the tree.
2494 statement(Block*, size_t* index, Statement*);
2496 // If traverse_expressions is set in the mask, this is called for
2497 // every expression in the tree.
2499 expression(Expression**);
2501 // If traverse_types is set in the mask, this is called for every
2502 // type in the tree.
2507 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2508 Type_identical) Types_seen;
2510 typedef Unordered_set(const Expression*) Expressions_seen;
2512 // Bitmask of what sort of objects to traverse.
2513 unsigned int traverse_mask_;
2514 // Types which have been seen in this traversal.
2515 Types_seen* types_seen_;
2516 // Expressions which have been seen in this traversal.
2517 Expressions_seen* expressions_seen_;
2520 // When translating the gogo IR into the backend data structure, this
2521 // is the context we pass down the blocks and statements.
2523 class Translate_context
2526 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2528 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2529 block_(block), bblock_(bblock), is_const_(false)
2536 { return this->gogo_; }
2540 { return this->backend_; }
2544 { return this->function_; }
2548 { return this->block_; }
2552 { return this->bblock_; }
2556 { return this->is_const_; }
2558 // Make a constant context.
2561 { this->is_const_ = true; }
2564 // The IR for the entire compilation unit.
2566 // The generator for the backend data structures.
2568 // The function we are currently translating. NULL if not in a
2569 // function, e.g., the initializer of a global variable.
2570 Named_object* function_;
2571 // The block we are currently translating. NULL if not in a
2574 // The backend representation of the current block. NULL if block_
2577 // Whether this is being evaluated in a constant context. This is
2578 // used for type descriptor initializers.
2582 // Runtime error codes. These must match the values in
2583 // libgo/runtime/go-runtime-error.c.
2585 // Slice index out of bounds: negative or larger than the length of
2587 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2589 // Array index out of bounds.
2590 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2592 // String index out of bounds.
2593 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2595 // Slice slice out of bounds: negative or larger than the length of
2596 // the slice or high bound less than low bound.
2597 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2599 // Array slice out of bounds.
2600 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2602 // String slice out of bounds.
2603 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2605 // Dereference of nil pointer. This is used when there is a
2606 // dereference of a pointer to a very large struct or array, to ensure
2607 // that a gigantic array is not used a proxy to access random memory
2609 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2611 // Slice length or capacity out of bounds in make: negative or
2612 // overflow or length greater than capacity.
2613 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2615 // Map capacity out of bounds in make: negative or overflow.
2616 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2618 // Channel capacity out of bounds in make: negative or overflow.
2619 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2621 // This is used by some of the langhooks.
2622 extern Gogo* go_get_gogo();
2624 // Whether we have seen any errors. FIXME: Replace with a backend
2626 extern bool saw_errors();
2628 #endif // !defined(GO_GOGO_H)