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 required interface method tables.
488 build_interface_method_tables();
490 // Build an interface method table for a type: a list of function
491 // pointers, one for each interface method. This returns a decl.
493 interface_method_table_for_type(const Interface_type*, Named_type*,
496 // Return a tree which allocate SIZE bytes to hold values of type
499 allocate_memory(Type *type, tree size, source_location);
501 // Return a type to use for pointer to const char.
503 const_char_pointer_type_tree();
505 // Build a string constant with the right type.
507 string_constant_tree(const std::string&);
509 // Build a Go string constant. This returns a pointer to the
512 go_string_constant_tree(const std::string&);
514 // Receive a value from a channel.
516 receive_from_channel(tree type_tree, tree channel, bool for_select,
519 // Return a tree for receiving an integer on a channel.
521 receive_as_64bit_integer(tree type, tree channel, bool blocking,
525 // Make a trampoline which calls FNADDR passing CLOSURE.
527 make_trampoline(tree fnaddr, tree closure, source_location);
530 // During parsing, we keep a stack of functions. Each function on
531 // the stack is one that we are currently parsing. For each
532 // function, we keep track of the current stack of blocks.
536 Named_object* function;
537 // The stack of active blocks in the function.
538 std::vector<Block*> blocks;
541 // The stack of functions.
542 typedef std::vector<Open_function> Open_functions;
544 // Set up the built-in unsafe package.
546 import_unsafe(const std::string&, bool is_exported, source_location);
548 // Add a new imported package.
550 add_package(const std::string& real_name, const std::string& alias,
551 const std::string& unique_prefix, source_location location);
553 // Return the current binding contour.
558 current_bindings() const;
560 // Return the current block.
564 // Get the name of the magic initialization function.
568 // Get the decl for the magic initialization function.
570 initialization_function_decl();
572 // Write the magic initialization function.
574 write_initialization_function(tree fndecl, tree init_stmt_list);
576 // Initialize imported packages.
580 // Register variables with the garbage collector.
582 register_gc_vars(const std::vector<Named_object*>&, tree*);
584 // Build a pointer to a Go string constant. This returns a pointer
587 ptr_go_string_constant_tree(const std::string&);
589 // Return the type of a trampoline.
591 trampoline_type_tree();
593 // Type used to map import names to packages.
594 typedef std::map<std::string, Package*> Imports;
596 // Type used to map package names to packages.
597 typedef std::map<std::string, Package*> Packages;
599 // Type used to map special names in the sys package.
600 typedef std::map<std::string, std::string> Sys_names;
602 // Hash table mapping map types to map descriptor decls.
603 typedef Unordered_map_hash(const Map_type*, tree, Type_hash_identical,
604 Type_identical) Map_descriptors;
606 // The backend generator.
608 // The package we are compiling.
610 // The list of currently open functions during parsing.
611 Open_functions functions_;
612 // The global binding contour. This includes the builtin functions
613 // and the package we are compiling.
615 // Mapping from import file names to packages.
617 // Whether the magic unsafe package was imported.
618 bool imported_unsafe_;
619 // Mapping from package names we have seen to packages. This does
620 // not include the package we are compiling.
622 // Mapping from map types to map descriptors.
623 Map_descriptors* map_descriptors_;
624 // The functions named "init", if there are any.
625 std::vector<Named_object*> init_functions_;
626 // Whether we need a magic initialization function.
628 // The name of the magic initialization function.
629 std::string init_fn_name_;
630 // A list of import control variables for packages that we import.
631 std::set<Import_init> imported_init_fns_;
632 // The unique prefix used for all global symbols.
633 std::string unique_prefix_;
634 // Whether an explicit unique prefix was set by -fgo-prefix.
635 bool unique_prefix_specified_;
636 // A list of interface types defined while parsing.
637 std::vector<Interface_type*> interface_types_;
638 // Whether named types have been converted.
639 bool named_types_are_converted_;
642 // A block of statements.
647 Block(Block* enclosing, source_location);
649 // Return the enclosing block.
652 { return this->enclosing_; }
654 // Return the bindings of the block.
657 { return this->bindings_; }
661 { return this->bindings_; }
663 // Look at the block's statements.
664 const std::vector<Statement*>*
666 { return &this->statements_; }
668 // Return the start location. This is normally the location of the
669 // left curly brace which starts the block.
671 start_location() const
672 { return this->start_location_; }
674 // Return the end location. This is normally the location of the
675 // right curly brace which ends the block.
678 { return this->end_location_; }
680 // Add a statement to the block.
682 add_statement(Statement*);
684 // Add a statement to the front of the block.
686 add_statement_at_front(Statement*);
688 // Replace a statement in a block.
690 replace_statement(size_t index, Statement*);
692 // Add a Statement before statement number INDEX.
694 insert_statement_before(size_t index, Statement*);
696 // Add a Statement after statement number INDEX.
698 insert_statement_after(size_t index, Statement*);
700 // Set the end location of the block.
702 set_end_location(source_location location)
703 { this->end_location_ = location; }
705 // Traverse the tree.
709 // Set final types for unspecified variables and constants.
713 // Return true if execution of this block may fall through to the
716 may_fall_through() const;
718 // Convert the block to the backend representation.
720 get_backend(Translate_context*);
722 // Iterate over statements.
724 typedef std::vector<Statement*>::iterator iterator;
728 { return this->statements_.begin(); }
732 { return this->statements_.end(); }
737 // Statements in the block.
738 std::vector<Statement*> statements_;
741 // Location of start of block.
742 source_location start_location_;
743 // Location of end of block.
744 source_location end_location_;
752 Function(Function_type* type, Function*, Block*, source_location);
754 // Return the function's type.
757 { return this->type_; }
759 // Return the enclosing function if there is one.
762 { return this->enclosing_; }
764 // Set the enclosing function. This is used when building thunks
765 // for functions which call recover.
767 set_enclosing(Function* enclosing)
769 go_assert(this->enclosing_ == NULL);
770 this->enclosing_ = enclosing;
773 // The result variables.
774 typedef std::vector<Named_object*> Results;
776 // Create the result variables in the outer block.
778 create_result_variables(Gogo*);
780 // Update the named result variables when cloning a function which
783 update_result_variables();
785 // Return the result variables.
788 { return this->results_; }
790 // Whether the result variables have names.
792 results_are_named() const
793 { return this->results_are_named_; }
795 // Add a new field to the closure variable.
797 add_closure_field(Named_object* var, source_location loc)
798 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
800 // Whether this function needs a closure.
802 needs_closure() const
803 { return !this->closure_fields_.empty(); }
805 // Return the closure variable, creating it if necessary. This is
806 // passed to the function as a static chain parameter.
810 // Set the closure variable. This is used when building thunks for
811 // functions which call recover.
813 set_closure_var(Named_object* v)
815 go_assert(this->closure_var_ == NULL);
816 this->closure_var_ = v;
819 // Return the variable for a reference to field INDEX in the closure
822 enclosing_var(unsigned int index)
824 go_assert(index < this->closure_fields_.size());
825 return closure_fields_[index].first;
828 // Set the type of the closure variable if there is one.
832 // Get the block of statements associated with the function.
835 { return this->block_; }
837 // Get the location of the start of the function.
840 { return this->location_; }
842 // Return whether this function is actually a method.
846 // Add a label definition to the function.
848 add_label_definition(const std::string& label_name, source_location);
850 // Add a label reference to a function.
852 add_label_reference(const std::string& label_name);
854 // Warn about labels that are defined but not used.
856 check_labels() const;
858 // Whether this function calls the predeclared recover function.
860 calls_recover() const
861 { return this->calls_recover_; }
863 // Record that this function calls the predeclared recover function.
864 // This is set during the lowering pass.
867 { this->calls_recover_ = true; }
869 // Whether this is a recover thunk function.
871 is_recover_thunk() const
872 { return this->is_recover_thunk_; }
874 // Record that this is a thunk built for a function which calls
877 set_is_recover_thunk()
878 { this->is_recover_thunk_ = true; }
880 // Whether this function already has a recover thunk.
882 has_recover_thunk() const
883 { return this->has_recover_thunk_; }
885 // Record that this function already has a recover thunk.
887 set_has_recover_thunk()
888 { this->has_recover_thunk_ = true; }
890 // Swap with another function. Used only for the thunk which calls
893 swap_for_recover(Function *);
895 // Traverse the tree.
899 // Determine types in the function.
903 // Return the function's decl given an identifier.
905 get_or_make_decl(Gogo*, Named_object*, tree id);
907 // Return the function's decl after it has been built.
911 go_assert(this->fndecl_ != NULL);
912 return this->fndecl_;
915 // Set the function decl to hold a tree of the function code.
917 build_tree(Gogo*, Named_object*);
919 // Get the value to return when not explicitly specified. May also
920 // add statements to execute first to STMT_LIST.
922 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
924 // Get a tree for the variable holding the defer stack.
926 defer_stack(source_location);
928 // Export the function.
930 export_func(Export*, const std::string& name) const;
932 // Export a function with a type.
934 export_func_with_type(Export*, const std::string& name,
935 const Function_type*);
937 // Import a function.
939 import_func(Import*, std::string* pname, Typed_identifier** receiver,
940 Typed_identifier_list** pparameters,
941 Typed_identifier_list** presults, bool* is_varargs);
944 // Type for mapping from label names to Label objects.
945 typedef Unordered_map(std::string, Label*) Labels;
948 make_receiver_parm_decl(Gogo*, Named_object*, tree);
951 copy_parm_to_heap(Gogo*, Named_object*, tree);
954 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
956 typedef std::vector<std::pair<Named_object*,
957 source_location> > Closure_fields;
959 // The function's type.
960 Function_type* type_;
961 // The enclosing function. This is NULL when there isn't one, which
962 // is the normal case.
963 Function* enclosing_;
964 // The result variables, if any.
966 // If there is a closure, this is the list of variables which appear
967 // in the closure. This is created by the parser, and then resolved
968 // to a real type when we lower parse trees.
969 Closure_fields closure_fields_;
970 // The closure variable, passed as a parameter using the static
971 // chain parameter. Normally NULL.
972 Named_object* closure_var_;
973 // The outer block of statements in the function.
975 // The source location of the start of the function.
976 source_location location_;
977 // Labels defined or referenced in the function.
979 // The function decl.
981 // The defer stack variable. A pointer to this variable is used to
982 // distinguish the defer stack for one function from another. This
983 // is NULL unless we actually need a defer stack.
984 Temporary_statement* defer_stack_;
985 // True if the result variables are named.
986 bool results_are_named_;
987 // True if this function calls the predeclared recover function.
989 // True if this a thunk built for a function which calls recover.
990 bool is_recover_thunk_;
991 // True if this function already has a recover thunk.
992 bool has_recover_thunk_;
995 // A function declaration.
997 class Function_declaration
1000 Function_declaration(Function_type* fntype, source_location location)
1001 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1006 { return this->fntype_; }
1010 { return this->location_; }
1014 { return this->asm_name_; }
1016 // Set the assembler name.
1018 set_asm_name(const std::string& asm_name)
1019 { this->asm_name_ = asm_name; }
1021 // Return a decl for the function given an identifier.
1023 get_or_make_decl(Gogo*, Named_object*, tree id);
1025 // Export a function declaration.
1027 export_func(Export* exp, const std::string& name) const
1028 { Function::export_func_with_type(exp, name, this->fntype_); }
1031 // The type of the function.
1032 Function_type* fntype_;
1033 // The location of the declaration.
1034 source_location location_;
1035 // The assembler name: this is the name to use in references to the
1036 // function. This is normally empty.
1037 std::string asm_name_;
1038 // The function decl if needed.
1047 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1048 bool is_receiver, source_location);
1050 // Get the type of the variable.
1057 // Return whether the type is defined yet.
1060 { return this->type_ != NULL; }
1062 // Get the initial value.
1065 { return this->init_; }
1067 // Return whether there are any preinit statements.
1069 has_pre_init() const
1070 { return this->preinit_ != NULL; }
1072 // Return the preinit statements if any.
1075 { return this->preinit_; }
1077 // Return whether this is a global variable.
1080 { return this->is_global_; }
1082 // Return whether this is a function parameter.
1084 is_parameter() const
1085 { return this->is_parameter_; }
1087 // Return whether this is the receiver parameter of a method.
1090 { return this->is_receiver_; }
1092 // Change this parameter to be a receiver. This is used when
1093 // creating the thunks created for functions which call recover.
1097 go_assert(this->is_parameter_);
1098 this->is_receiver_ = true;
1101 // Change this parameter to not be a receiver. This is used when
1102 // creating the thunks created for functions which call recover.
1104 set_is_not_receiver()
1106 go_assert(this->is_parameter_);
1107 this->is_receiver_ = false;
1110 // Return whether this is the varargs parameter of a function.
1112 is_varargs_parameter() const
1113 { return this->is_varargs_parameter_; }
1115 // Whether this variable's address is taken.
1117 is_address_taken() const
1118 { return this->is_address_taken_; }
1120 // Whether this variable should live in the heap.
1123 { return this->is_address_taken_ && !this->is_global_; }
1125 // Note that something takes the address of this variable.
1128 { this->is_address_taken_ = true; }
1130 // Return whether the address is taken but does not escape.
1132 is_non_escaping_address_taken() const
1133 { return this->is_non_escaping_address_taken_; }
1135 // Note that something takes the address of this variable such that
1136 // the address does not escape the function.
1138 set_non_escaping_address_taken()
1139 { this->is_non_escaping_address_taken_ = true; }
1141 // Get the source location of the variable's declaration.
1144 { return this->location_; }
1146 // Record that this is the varargs parameter of a function.
1148 set_is_varargs_parameter()
1150 go_assert(this->is_parameter_);
1151 this->is_varargs_parameter_ = true;
1154 // Clear the initial value; used for error handling.
1157 { this->init_ = NULL; }
1159 // Set the initial value; used for converting shortcuts.
1161 set_init(Expression* init)
1162 { this->init_ = init; }
1164 // Get the preinit block, a block of statements to be run before the
1165 // initialization expression.
1167 preinit_block(Gogo*);
1169 // Add a statement to be run before the initialization expression.
1170 // This is only used for global variables.
1172 add_preinit_statement(Gogo*, Statement*);
1174 // Lower the initialization expression after parsing is complete.
1176 lower_init_expression(Gogo*, Named_object*);
1178 // A special case: the init value is used only to determine the
1179 // type. This is used if the variable is defined using := with the
1180 // comma-ok form of a map index or a receive expression. The init
1181 // value is actually the map index expression or receive expression.
1182 // We use this because we may not know the right type at parse time.
1184 set_type_from_init_tuple()
1185 { this->type_from_init_tuple_ = true; }
1187 // Another special case: the init value is used only to determine
1188 // the type. This is used if the variable is defined using := with
1189 // a range clause. The init value is the range expression. The
1190 // type of the variable is the index type of the range expression
1191 // (i.e., the first value returned by a range).
1193 set_type_from_range_index()
1194 { this->type_from_range_index_ = true; }
1196 // Another special case: like set_type_from_range_index, but the
1197 // type is the value type of the range expression (i.e., the second
1198 // value returned by a range).
1200 set_type_from_range_value()
1201 { this->type_from_range_value_ = true; }
1203 // Another special case: the init value is used only to determine
1204 // the type. This is used if the variable is defined using := with
1205 // a case in a select statement. The init value is the channel.
1206 // The type of the variable is the channel's element type.
1208 set_type_from_chan_element()
1209 { this->type_from_chan_element_ = true; }
1211 // After we lower the select statement, we once again set the type
1212 // from the initialization expression.
1214 clear_type_from_chan_element()
1216 go_assert(this->type_from_chan_element_);
1217 this->type_from_chan_element_ = false;
1220 // Note that this variable was created for a type switch clause.
1222 set_is_type_switch_var()
1223 { this->is_type_switch_var_ = true; }
1225 // Traverse the initializer expression.
1227 traverse_expression(Traverse*);
1229 // Determine the type of the variable if necessary.
1233 // Get the backend representation of the variable.
1235 get_backend_variable(Gogo*, Named_object*, const Package*,
1236 const std::string&);
1238 // Get the initial value of the variable as a tree. This may only
1239 // be called if has_pre_init() returns false.
1241 get_init_tree(Gogo*, Named_object* function);
1243 // Return a series of statements which sets the value of the
1244 // variable in DECL. This should only be called is has_pre_init()
1245 // returns true. DECL may be NULL for a sink variable.
1247 get_init_block(Gogo*, Named_object* function, tree decl);
1249 // Export the variable.
1251 export_var(Export*, const std::string& name) const;
1253 // Import a variable.
1255 import_var(Import*, std::string* pname, Type** ptype);
1258 // The type of a tuple.
1260 type_from_tuple(Expression*, bool) const;
1262 // The type of a range.
1264 type_from_range(Expression*, bool, bool) const;
1266 // The element type of a channel.
1268 type_from_chan_element(Expression*, bool) const;
1270 // The variable's type. This may be NULL if the type is set from
1273 // The initial value. This may be NULL if the variable should be
1274 // initialized to the default value for the type.
1276 // Statements to run before the init statement.
1278 // Location of variable definition.
1279 source_location location_;
1280 // Backend representation.
1281 Bvariable* backend_;
1282 // Whether this is a global variable.
1283 bool is_global_ : 1;
1284 // Whether this is a function parameter.
1285 bool is_parameter_ : 1;
1286 // Whether this is the receiver parameter of a method.
1287 bool is_receiver_ : 1;
1288 // Whether this is the varargs parameter of a function.
1289 bool is_varargs_parameter_ : 1;
1290 // Whether something takes the address of this variable. For a
1291 // local variable this implies that the variable has to be on the
1293 bool is_address_taken_ : 1;
1294 // Whether something takes the address of this variable such that
1295 // the address does not escape the function.
1296 bool is_non_escaping_address_taken_ : 1;
1297 // True if we have seen this variable in a traversal.
1299 // True if we have lowered the initialization expression.
1300 bool init_is_lowered_ : 1;
1301 // True if init is a tuple used to set the type.
1302 bool type_from_init_tuple_ : 1;
1303 // True if init is a range clause and the type is the index type.
1304 bool type_from_range_index_ : 1;
1305 // True if init is a range clause and the type is the value type.
1306 bool type_from_range_value_ : 1;
1307 // True if init is a channel and the type is the channel's element type.
1308 bool type_from_chan_element_ : 1;
1309 // True if this is a variable created for a type switch case.
1310 bool is_type_switch_var_ : 1;
1311 // True if we have determined types.
1312 bool determined_type_ : 1;
1315 // A variable which is really the name for a function return value, or
1318 class Result_variable
1321 Result_variable(Type* type, Function* function, int index,
1322 source_location location)
1323 : type_(type), function_(function), index_(index), location_(location),
1324 backend_(NULL), is_address_taken_(false),
1325 is_non_escaping_address_taken_(false)
1328 // Get the type of the result variable.
1331 { return this->type_; }
1333 // Get the function that this is associated with.
1336 { return this->function_; }
1338 // Index in the list of function results.
1341 { return this->index_; }
1343 // The location of the variable definition.
1346 { return this->location_; }
1348 // Whether this variable's address is taken.
1350 is_address_taken() const
1351 { return this->is_address_taken_; }
1353 // Note that something takes the address of this variable.
1356 { this->is_address_taken_ = true; }
1358 // Return whether the address is taken but does not escape.
1360 is_non_escaping_address_taken() const
1361 { return this->is_non_escaping_address_taken_; }
1363 // Note that something takes the address of this variable such that
1364 // the address does not escape the function.
1366 set_non_escaping_address_taken()
1367 { this->is_non_escaping_address_taken_ = true; }
1369 // Whether this variable should live in the heap.
1372 { return this->is_address_taken_; }
1374 // Set the function. This is used when cloning functions which call
1377 set_function(Function* function)
1378 { this->function_ = function; }
1380 // Get the backend representation of the variable.
1382 get_backend_variable(Gogo*, Named_object*, const std::string&);
1385 // Type of result variable.
1387 // Function with which this is associated.
1388 Function* function_;
1389 // Index in list of results.
1391 // Where the result variable is defined.
1392 source_location location_;
1393 // Backend representation.
1394 Bvariable* backend_;
1395 // Whether something takes the address of this variable.
1396 bool is_address_taken_;
1397 // Whether something takes the address of this variable such that
1398 // the address does not escape the function.
1399 bool is_non_escaping_address_taken_;
1402 // The value we keep for a named constant. This lets us hold a type
1403 // and an expression.
1405 class Named_constant
1408 Named_constant(Type* type, Expression* expr, int iota_value,
1409 source_location location)
1410 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1416 { return this->type_; }
1420 { return this->expr_; }
1424 { return this->iota_value_; }
1428 { return this->location_; }
1430 // Whether we are lowering.
1433 { return this->lowering_; }
1435 // Set that we are lowering.
1438 { this->lowering_ = true; }
1440 // We are no longer lowering.
1443 { this->lowering_ = false; }
1445 // Traverse the expression.
1447 traverse_expression(Traverse*);
1449 // Determine the type of the constant if necessary.
1453 // Indicate that we found and reported an error for this constant.
1457 // Export the constant.
1459 export_const(Export*, const std::string& name) const;
1461 // Import a constant.
1463 import_const(Import*, std::string*, Type**, Expression**);
1466 // The type of the constant.
1468 // The expression for the constant.
1470 // If the predeclared constant iota is used in EXPR_, this is the
1471 // value it will have. We do this because at parse time we don't
1472 // know whether the name "iota" will refer to the predeclared
1473 // constant or to something else. We put in the right value in when
1476 // The location of the definition.
1477 source_location location_;
1478 // Whether we are currently lowering this constant.
1482 // A type declaration.
1484 class Type_declaration
1487 Type_declaration(source_location location)
1488 : location_(location), in_function_(NULL), methods_(),
1489 issued_warning_(false)
1492 // Return the location.
1495 { return this->location_; }
1497 // Return the function in which this type is declared. This will
1498 // return NULL for a type declared in global scope.
1501 { return this->in_function_; }
1503 // Set the function in which this type is declared.
1505 set_in_function(Named_object* f)
1506 { this->in_function_ = f; }
1508 // Add a method to this type. This is used when methods are defined
1511 add_method(const std::string& name, Function* function);
1513 // Add a method declaration to this type.
1515 add_method_declaration(const std::string& name, Function_type* type,
1516 source_location location);
1518 // Return whether any methods were defined.
1520 has_methods() const;
1522 // Define methods when the real type is known.
1524 define_methods(Named_type*);
1526 // This is called if we are trying to use this type. It returns
1527 // true if we should issue a warning.
1532 typedef std::vector<Named_object*> Methods;
1534 // The location of the type declaration.
1535 source_location location_;
1536 // If this type is declared in a function, a pointer back to the
1537 // function in which it is defined.
1538 Named_object* in_function_;
1539 // Methods defined before the type is defined.
1541 // True if we have issued a warning about a use of this type
1542 // declaration when it is undefined.
1543 bool issued_warning_;
1546 // An unknown object. These are created by the parser for forward
1547 // references to names which have not been seen before. In a correct
1548 // program, these will always point to a real definition by the end of
1549 // the parse. Because they point to another Named_object, these may
1550 // only be referenced by Unknown_expression objects.
1555 Unknown_name(source_location location)
1556 : location_(location), real_named_object_(NULL)
1559 // Return the location where this name was first seen.
1562 { return this->location_; }
1564 // Return the real named object that this points to, or NULL if it
1565 // was never resolved.
1567 real_named_object() const
1568 { return this->real_named_object_; }
1570 // Set the real named object that this points to.
1572 set_real_named_object(Named_object* no);
1575 // The location where this name was first seen.
1576 source_location location_;
1577 // The real named object when it is known.
1582 // A named object named. This is the result of a declaration. We
1583 // don't use a superclass because they all have to be handled
1591 // An uninitialized Named_object. We should never see this.
1592 NAMED_OBJECT_UNINITIALIZED,
1593 // An unknown name. This is used for forward references. In a
1594 // correct program, these will all be resolved by the end of the
1596 NAMED_OBJECT_UNKNOWN,
1601 // A forward type declaration.
1602 NAMED_OBJECT_TYPE_DECLARATION,
1605 // A result variable in a function.
1606 NAMED_OBJECT_RESULT_VAR,
1607 // The blank identifier--the special variable named _.
1611 // A forward func declaration.
1612 NAMED_OBJECT_FUNC_DECLARATION,
1614 NAMED_OBJECT_PACKAGE
1617 // Return the classification.
1619 classification() const
1620 { return this->classification_; }
1626 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1630 { return this->classification_ == NAMED_OBJECT_CONST; }
1634 { return this->classification_ == NAMED_OBJECT_TYPE; }
1637 is_type_declaration() const
1638 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1642 { return this->classification_ == NAMED_OBJECT_VAR; }
1645 is_result_variable() const
1646 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1650 { return this->classification_ == NAMED_OBJECT_SINK; }
1654 { return this->classification_ == NAMED_OBJECT_FUNC; }
1657 is_function_declaration() const
1658 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1662 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1666 static Named_object*
1667 make_unknown_name(const std::string& name, source_location);
1669 static Named_object*
1670 make_constant(const Typed_identifier&, const Package*, Expression*,
1673 static Named_object*
1674 make_type(const std::string&, const Package*, Type*, source_location);
1676 static Named_object*
1677 make_type_declaration(const std::string&, const Package*, source_location);
1679 static Named_object*
1680 make_variable(const std::string&, const Package*, Variable*);
1682 static Named_object*
1683 make_result_variable(const std::string&, Result_variable*);
1685 static Named_object*
1688 static Named_object*
1689 make_function(const std::string&, const Package*, Function*);
1691 static Named_object*
1692 make_function_declaration(const std::string&, const Package*, Function_type*,
1695 static Named_object*
1696 make_package(const std::string& alias, Package* package);
1703 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1704 return this->u_.unknown_value;
1708 unknown_value() const
1710 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1711 return this->u_.unknown_value;
1717 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1718 return this->u_.const_value;
1721 const Named_constant*
1724 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1725 return this->u_.const_value;
1731 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1732 return this->u_.type_value;
1738 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1739 return this->u_.type_value;
1743 type_declaration_value()
1745 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1746 return this->u_.type_declaration;
1749 const Type_declaration*
1750 type_declaration_value() const
1752 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1753 return this->u_.type_declaration;
1759 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1760 return this->u_.var_value;
1766 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1767 return this->u_.var_value;
1773 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1774 return this->u_.result_var_value;
1777 const Result_variable*
1778 result_var_value() const
1780 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1781 return this->u_.result_var_value;
1787 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1788 return this->u_.func_value;
1794 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1795 return this->u_.func_value;
1798 Function_declaration*
1799 func_declaration_value()
1801 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1802 return this->u_.func_declaration_value;
1805 const Function_declaration*
1806 func_declaration_value() const
1808 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1809 return this->u_.func_declaration_value;
1815 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1816 return this->u_.package_value;
1820 package_value() const
1822 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1823 return this->u_.package_value;
1828 { return this->name_; }
1830 // Return the name to use in an error message. The difference is
1831 // that if this Named_object is defined in a different package, this
1832 // will return PACKAGE.NAME.
1834 message_name() const;
1838 { return this->package_; }
1840 // Resolve an unknown value if possible. This returns the same
1841 // Named_object or a new one.
1845 Named_object* ret = this;
1846 if (this->is_unknown())
1848 Named_object* r = this->unknown_value()->real_named_object();
1858 const Named_object* ret = this;
1859 if (this->is_unknown())
1861 const Named_object* r = this->unknown_value()->real_named_object();
1868 // The location where this object was defined or referenced.
1872 // Convert a variable to the backend representation.
1874 get_backend_variable(Gogo*, Named_object* function);
1876 // Return a tree for the external identifier for this object.
1880 // Return a tree representing this object.
1882 get_tree(Gogo*, Named_object* function);
1884 // Define a type declaration.
1886 set_type_value(Named_type*);
1888 // Define a function declaration.
1890 set_function_value(Function*);
1892 // Declare an unknown name as a type declaration.
1896 // Export this object.
1898 export_named_object(Export*) const;
1901 Named_object(const std::string&, const Package*, Classification);
1903 // The name of the object.
1905 // The package that this object is in. This is NULL if it is in the
1906 // file we are compiling.
1907 const Package* package_;
1908 // The type of object this is.
1909 Classification classification_;
1913 Unknown_name* unknown_value;
1914 Named_constant* const_value;
1915 Named_type* type_value;
1916 Type_declaration* type_declaration;
1917 Variable* var_value;
1918 Result_variable* result_var_value;
1919 Function* func_value;
1920 Function_declaration* func_declaration_value;
1921 Package* package_value;
1923 // The DECL tree for this object if we have already converted it.
1927 // A binding contour. This binds names to objects.
1932 // Type for mapping from names to objects.
1933 typedef Unordered_map(std::string, Named_object*) Contour;
1935 Bindings(Bindings* enclosing);
1937 // Add an unknown name.
1939 add_unknown_name(const std::string& name, source_location location)
1941 return this->add_named_object(Named_object::make_unknown_name(name,
1947 add_constant(const Typed_identifier& tid, const Package* package,
1948 Expression* expr, int iota_value)
1950 return this->add_named_object(Named_object::make_constant(tid, package,
1957 add_type(const std::string& name, const Package* package, Type* type,
1958 source_location location)
1960 return this->add_named_object(Named_object::make_type(name, package, type,
1964 // Add a named type. This is used for builtin types, and to add an
1965 // imported type to the global scope.
1967 add_named_type(Named_type* named_type);
1969 // Add a type declaration.
1971 add_type_declaration(const std::string& name, const Package* package,
1972 source_location location)
1974 Named_object* no = Named_object::make_type_declaration(name, package,
1976 return this->add_named_object(no);
1981 add_variable(const std::string& name, const Package* package,
1984 return this->add_named_object(Named_object::make_variable(name, package,
1988 // Add a result variable.
1990 add_result_variable(const std::string& name, Result_variable* result)
1992 return this->add_named_object(Named_object::make_result_variable(name,
1998 add_function(const std::string& name, const Package*, Function* function);
2000 // Add a function declaration.
2002 add_function_declaration(const std::string& name, const Package* package,
2003 Function_type* type, source_location location);
2005 // Add a package. The location is the location of the import
2008 add_package(const std::string& alias, Package* package)
2010 Named_object* no = Named_object::make_package(alias, package);
2011 return this->add_named_object(no);
2014 // Define a type which was already declared.
2016 define_type(Named_object*, Named_type*);
2018 // Add a method to the list of objects. This is not added to the
2021 add_method(Named_object*);
2023 // Add a named object to this binding.
2025 add_named_object(Named_object* no)
2026 { return this->add_named_object_to_contour(&this->bindings_, no); }
2028 // Clear all names in file scope from the bindings.
2032 // Look up a name in this binding contour and in any enclosing
2033 // binding contours. This returns NULL if the name is not found.
2035 lookup(const std::string&) const;
2037 // Look up a name in this binding contour without looking in any
2038 // enclosing binding contours. Returns NULL if the name is not found.
2040 lookup_local(const std::string&) const;
2044 remove_binding(Named_object*);
2046 // Traverse the tree. See the Traverse class.
2048 traverse(Traverse*, bool is_global);
2050 // Iterate over definitions. This does not include things which
2051 // were only declared.
2053 typedef std::vector<Named_object*>::const_iterator
2054 const_definitions_iterator;
2056 const_definitions_iterator
2057 begin_definitions() const
2058 { return this->named_objects_.begin(); }
2060 const_definitions_iterator
2061 end_definitions() const
2062 { return this->named_objects_.end(); }
2064 // Return the number of definitions.
2066 size_definitions() const
2067 { return this->named_objects_.size(); }
2069 // Return whether there are no definitions.
2071 empty_definitions() const
2072 { return this->named_objects_.empty(); }
2074 // Iterate over declarations. This is everything that has been
2075 // declared, which includes everything which has been defined.
2077 typedef Contour::const_iterator const_declarations_iterator;
2079 const_declarations_iterator
2080 begin_declarations() const
2081 { return this->bindings_.begin(); }
2083 const_declarations_iterator
2084 end_declarations() const
2085 { return this->bindings_.end(); }
2087 // Return the number of declarations.
2089 size_declarations() const
2090 { return this->bindings_.size(); }
2092 // Return whether there are no declarations.
2094 empty_declarations() const
2095 { return this->bindings_.empty(); }
2097 // Return the first declaration.
2100 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2104 add_named_object_to_contour(Contour*, Named_object*);
2107 new_definition(Named_object*, Named_object*);
2109 // Enclosing bindings.
2110 Bindings* enclosing_;
2111 // The list of objects.
2112 std::vector<Named_object*> named_objects_;
2113 // The mapping from names to objects.
2122 Label(const std::string& name)
2123 : name_(name), location_(0), is_used_(false), blabel_(NULL)
2126 // Return the label's name.
2129 { return this->name_; }
2131 // Return whether the label has been defined.
2134 { return this->location_ != 0; }
2136 // Return whether the label has been used.
2139 { return this->is_used_; }
2141 // Record that the label is used.
2144 { this->is_used_ = true; }
2146 // Return the location of the definition.
2149 { return this->location_; }
2151 // Define the label at LOCATION.
2153 define(source_location location)
2155 go_assert(this->location_ == 0);
2156 this->location_ = location;
2159 // Return the backend representation for this label.
2161 get_backend_label(Translate_context*);
2163 // Return an expression for the address of this label. This is used
2164 // to get the return address of a deferred function to see whether
2165 // the function may call recover.
2167 get_addr(Translate_context*, source_location location);
2170 // The name of the label.
2172 // The location of the definition. This is 0 if the label has not
2173 // yet been defined.
2174 source_location location_;
2175 // Whether the label has been used.
2177 // The backend representation.
2181 // An unnamed label. These are used when lowering loops.
2186 Unnamed_label(source_location location)
2187 : location_(location), blabel_(NULL)
2190 // Get the location where the label is defined.
2193 { return this->location_; }
2195 // Set the location where the label is defined.
2197 set_location(source_location location)
2198 { this->location_ = location; }
2200 // Return a statement which defines this label.
2202 get_definition(Translate_context*);
2204 // Return a goto to this label from LOCATION.
2206 get_goto(Translate_context*, source_location location);
2209 // Return the backend representation.
2211 get_blabel(Translate_context*);
2213 // The location where the label is defined.
2214 source_location location_;
2215 // The backend representation of this label.
2219 // An imported package.
2224 Package(const std::string& name, const std::string& unique_prefix,
2225 source_location location);
2227 // The real name of this package. This may be different from the
2228 // name in the associated Named_object if the import statement used
2232 { return this->name_; }
2234 // Return the location of the import statement.
2237 { return this->location_; }
2239 // Get the unique prefix used for all symbols exported from this
2242 unique_prefix() const
2244 go_assert(!this->unique_prefix_.empty());
2245 return this->unique_prefix_;
2248 // The priority of this package. The init function of packages with
2249 // lower priority must be run before the init function of packages
2250 // with higher priority.
2253 { return this->priority_; }
2255 // Set the priority.
2257 set_priority(int priority);
2259 // Return the bindings.
2262 { return this->bindings_; }
2264 // Whether some symbol from the package was used.
2267 { return this->used_; }
2269 // Note that some symbol from this package was used.
2272 { this->used_ = true; }
2274 // Clear the used field for the next file.
2277 { this->used_ = false; }
2279 // Whether this package was imported in the current file.
2282 { return this->is_imported_; }
2284 // Note that this package was imported in the current file.
2287 { this->is_imported_ = true; }
2289 // Clear the imported field for the next file.
2292 { this->is_imported_ = false; }
2294 // Whether this package was imported with a name of "_".
2296 uses_sink_alias() const
2297 { return this->uses_sink_alias_; }
2299 // Note that this package was imported with a name of "_".
2301 set_uses_sink_alias()
2302 { this->uses_sink_alias_ = true; }
2304 // Clear the sink alias field for the next file.
2306 clear_uses_sink_alias()
2307 { this->uses_sink_alias_ = false; }
2309 // Look up a name in the package. Returns NULL if the name is not
2312 lookup(const std::string& name) const
2313 { return this->bindings_->lookup(name); }
2315 // Set the location of the package. This is used if it is seen in a
2316 // different import before it is really imported.
2318 set_location(source_location location)
2319 { this->location_ = location; }
2321 // Add a constant to the package.
2323 add_constant(const Typed_identifier& tid, Expression* expr)
2324 { return this->bindings_->add_constant(tid, this, expr, 0); }
2326 // Add a type to the package.
2328 add_type(const std::string& name, Type* type, source_location location)
2329 { return this->bindings_->add_type(name, this, type, location); }
2331 // Add a type declaration to the package.
2333 add_type_declaration(const std::string& name, source_location location)
2334 { return this->bindings_->add_type_declaration(name, this, location); }
2336 // Add a variable to the package.
2338 add_variable(const std::string& name, Variable* variable)
2339 { return this->bindings_->add_variable(name, this, variable); }
2341 // Add a function declaration to the package.
2343 add_function_declaration(const std::string& name, Function_type* type,
2344 source_location loc)
2345 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2347 // Determine types of constants.
2352 // The real name of this package.
2354 // The unique prefix for all exported global symbols.
2355 std::string unique_prefix_;
2356 // The names in this package.
2357 Bindings* bindings_;
2358 // The priority of this package. A package has a priority higher
2359 // than the priority of all of the packages that it imports. This
2360 // is used to run init functions in the right order.
2362 // The location of the import statement.
2363 source_location location_;
2364 // True if some name from this package was used. This is mutable
2365 // because we can use a package even if we have a const pointer to
2368 // True if this package was imported in the current file.
2370 // True if this package was imported with a name of "_".
2371 bool uses_sink_alias_;
2374 // Return codes for the traversal functions. This is not an enum
2375 // because we want to be able to declare traversal functions in other
2376 // header files without including this one.
2378 // Continue traversal as usual.
2379 const int TRAVERSE_CONTINUE = -1;
2382 const int TRAVERSE_EXIT = 0;
2384 // Continue traversal, but skip components of the current object.
2385 // E.g., if this is returned by Traverse::statement, we do not
2386 // traverse the expressions in the statement even if
2387 // traverse_expressions is set in the traverse_mask.
2388 const int TRAVERSE_SKIP_COMPONENTS = 1;
2390 // This class is used when traversing the parse tree. The caller uses
2391 // a subclass which overrides functions as desired.
2396 // These bitmasks say what to traverse.
2397 static const unsigned int traverse_variables = 0x1;
2398 static const unsigned int traverse_constants = 0x2;
2399 static const unsigned int traverse_functions = 0x4;
2400 static const unsigned int traverse_blocks = 0x8;
2401 static const unsigned int traverse_statements = 0x10;
2402 static const unsigned int traverse_expressions = 0x20;
2403 static const unsigned int traverse_types = 0x40;
2405 Traverse(unsigned int traverse_mask)
2406 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2409 virtual ~Traverse();
2411 // The bitmask of what to traverse.
2413 traverse_mask() const
2414 { return this->traverse_mask_; }
2416 // Record that we are going to traverse a type. This returns true
2417 // if the type has already been seen in this traversal. This is
2418 // required because types, unlike expressions, can form a circular
2421 remember_type(const Type*);
2423 // Record that we are going to see an expression. This returns true
2424 // if the expression has already been seen in this traversal. This
2425 // is only needed for cases where multiple expressions can point to
2428 remember_expression(const Expression*);
2430 // These functions return one of the TRAVERSE codes defined above.
2432 // If traverse_variables is set in the mask, this is called for
2433 // every variable in the tree.
2435 variable(Named_object*);
2437 // If traverse_constants is set in the mask, this is called for
2438 // every named constant in the tree. The bool parameter is true for
2439 // a global constant.
2441 constant(Named_object*, bool);
2443 // If traverse_functions is set in the mask, this is called for
2444 // every function in the tree.
2446 function(Named_object*);
2448 // If traverse_blocks is set in the mask, this is called for every
2449 // block in the tree.
2453 // If traverse_statements is set in the mask, this is called for
2454 // every statement in the tree.
2456 statement(Block*, size_t* index, Statement*);
2458 // If traverse_expressions is set in the mask, this is called for
2459 // every expression in the tree.
2461 expression(Expression**);
2463 // If traverse_types is set in the mask, this is called for every
2464 // type in the tree.
2469 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2470 Type_identical) Types_seen;
2472 typedef Unordered_set(const Expression*) Expressions_seen;
2474 // Bitmask of what sort of objects to traverse.
2475 unsigned int traverse_mask_;
2476 // Types which have been seen in this traversal.
2477 Types_seen* types_seen_;
2478 // Expressions which have been seen in this traversal.
2479 Expressions_seen* expressions_seen_;
2482 // When translating the gogo IR into the backend data structure, this
2483 // is the context we pass down the blocks and statements.
2485 class Translate_context
2488 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2490 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2491 block_(block), bblock_(bblock), is_const_(false)
2498 { return this->gogo_; }
2502 { return this->backend_; }
2506 { return this->function_; }
2510 { return this->block_; }
2514 { return this->bblock_; }
2518 { return this->is_const_; }
2520 // Make a constant context.
2523 { this->is_const_ = true; }
2526 // The IR for the entire compilation unit.
2528 // The generator for the backend data structures.
2530 // The function we are currently translating. NULL if not in a
2531 // function, e.g., the initializer of a global variable.
2532 Named_object* function_;
2533 // The block we are currently translating. NULL if not in a
2536 // The backend representation of the current block. NULL if block_
2539 // Whether this is being evaluated in a constant context. This is
2540 // used for type descriptor initializers.
2544 // Runtime error codes. These must match the values in
2545 // libgo/runtime/go-runtime-error.c.
2547 // Slice index out of bounds: negative or larger than the length of
2549 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2551 // Array index out of bounds.
2552 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2554 // String index out of bounds.
2555 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2557 // Slice slice out of bounds: negative or larger than the length of
2558 // the slice or high bound less than low bound.
2559 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2561 // Array slice out of bounds.
2562 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2564 // String slice out of bounds.
2565 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2567 // Dereference of nil pointer. This is used when there is a
2568 // dereference of a pointer to a very large struct or array, to ensure
2569 // that a gigantic array is not used a proxy to access random memory
2571 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2573 // Slice length or capacity out of bounds in make: negative or
2574 // overflow or length greater than capacity.
2575 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2577 // Map capacity out of bounds in make: negative or overflow.
2578 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2580 // Channel capacity out of bounds in make: negative or overflow.
2581 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2583 // This is used by some of the langhooks.
2584 extern Gogo* go_get_gogo();
2586 // Whether we have seen any errors. FIXME: Replace with a backend
2588 extern bool saw_errors();
2590 #endif // !defined(GO_GOGO_H)