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.
11 class Statement_inserter;
13 class Type_hash_identical;
16 class Typed_identifier;
17 class Typed_identifier_list;
21 class Temporary_statement;
30 class Struct_field_list;
36 class Forward_declaration_type;
41 class Translate_context;
51 // This file declares the basic classes used to hold the internal
52 // representation of Go which is built by the parser.
54 // An initialization function for an imported package. This is a
55 // magic function which initializes variables and runs the "init"
61 Import_init(const std::string& package_name, const std::string& init_name,
63 : package_name_(package_name), init_name_(init_name), priority_(priority)
66 // The name of the package being imported.
69 { return this->package_name_; }
71 // The name of the package's init function.
74 { return this->init_name_; }
76 // The priority of the initialization function. Functions with a
77 // lower priority number must be run first.
80 { return this->priority_; }
83 // The name of the package being imported.
84 std::string package_name_;
85 // The name of the package's init function.
86 std::string init_name_;
91 // For sorting purposes.
94 operator<(const Import_init& i1, const Import_init& i2)
96 if (i1.priority() < i2.priority())
98 if (i1.priority() > i2.priority())
100 if (i1.package_name() != i2.package_name())
101 return i1.package_name() < i2.package_name();
102 return i1.init_name() < i2.init_name();
105 // The holder for the internal representation of the entire
111 // Create the IR, passing in the sizes of the types "int" and
112 // "uintptr" in bits.
113 Gogo(Backend* backend, int int_type_size, int pointer_size);
115 // Get the backend generator.
118 { return this->backend_; }
120 // Get the package name.
122 package_name() const;
124 // Set the package name.
126 set_package_name(const std::string&, source_location);
128 // Return whether this is the "main" package.
130 is_main_package() const;
132 // If necessary, adjust the name to use for a hidden symbol. We add
133 // a prefix of the package name, so that hidden symbols in different
134 // packages do not collide.
136 pack_hidden_name(const std::string& name, bool is_exported) const
140 : ('.' + this->unique_prefix()
141 + '.' + this->package_name()
145 // Unpack a name which may have been hidden. Returns the
146 // user-visible name of the object.
148 unpack_hidden_name(const std::string& name)
149 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
151 // Return whether a possibly packed name is hidden.
153 is_hidden_name(const std::string& name)
154 { return name[0] == '.'; }
156 // Return the package prefix of a hidden name.
158 hidden_name_prefix(const std::string& name)
160 go_assert(Gogo::is_hidden_name(name));
161 return name.substr(1, name.rfind('.') - 1);
164 // Given a name which may or may not have been hidden, return the
165 // name to use in an error message.
167 message_name(const std::string& name);
169 // Return whether a name is the blank identifier _.
171 is_sink_name(const std::string& name)
173 return (name[0] == '.'
174 && name[name.length() - 1] == '_'
175 && name[name.length() - 2] == '.');
178 // Return the unique prefix to use for all exported symbols.
180 unique_prefix() const;
182 // Set the unique prefix.
184 set_unique_prefix(const std::string&);
186 // Return the priority to use for the package we are compiling.
187 // This is two more than the largest priority of any package we
190 package_priority() const;
192 // Import a package. FILENAME is the file name argument, LOCAL_NAME
193 // is the local name to give to the package. If LOCAL_NAME is empty
194 // the declarations are added to the global scope.
196 import_package(const std::string& filename, const std::string& local_name,
197 bool is_local_name_exported, source_location);
199 // Whether we are the global binding level.
201 in_global_scope() const;
203 // Look up a name in the current binding contours.
205 lookup(const std::string&, Named_object** pfunction) const;
207 // Look up a name in the current block.
209 lookup_in_block(const std::string&) const;
211 // Look up a name in the global namespace--the universal scope.
213 lookup_global(const char*) const;
215 // Add a new imported package. REAL_NAME is the real name of the
216 // package. ALIAS is the alias of the package; this may be the same
217 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
218 // this package should be added to the global namespace; this is
219 // true if the alias is ".". LOCATION is the location of the import
220 // statement. This returns the new package, or NULL on error.
222 add_imported_package(const std::string& real_name, const std::string& alias,
223 bool is_alias_exported,
224 const std::string& unique_prefix,
225 source_location location,
226 bool* padd_to_globals);
228 // Register a package. This package may or may not be imported.
229 // This returns the Package structure for the package, creating if
232 register_package(const std::string& name, const std::string& unique_prefix,
235 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
236 // method function should be added to the type of its receiver.
238 start_function(const std::string& name, Function_type* type,
239 bool add_method_to_type, source_location);
241 // Finish compiling a function.
243 finish_function(source_location);
245 // Return the current function.
247 current_function() const;
249 // Start a new block. This is not initially associated with a
252 start_block(source_location);
254 // Finish the current block and return it.
256 finish_block(source_location);
258 // Declare an unknown name. This is used while parsing. The name
259 // must be resolved by the end of the parse. Unknown names are
260 // always added at the package level.
262 add_unknown_name(const std::string& name, source_location);
264 // Declare a function.
266 declare_function(const std::string&, Function_type*, source_location);
270 add_label_definition(const std::string&, source_location);
272 // Add a label reference.
274 add_label_reference(const std::string&);
276 // Add a statement to the current block.
278 add_statement(Statement*);
280 // Add a block to the current block.
282 add_block(Block*, source_location);
286 add_constant(const Typed_identifier&, Expression*, int iota_value);
290 add_type(const std::string&, Type*, source_location);
292 // Add a named type. This is used for builtin types, and to add an
293 // imported type to the global scope.
295 add_named_type(Named_type*);
299 declare_type(const std::string&, source_location);
301 // Declare a type at the package level. This is used when the
302 // parser sees an unknown name where a type name is required.
304 declare_package_type(const std::string&, source_location);
306 // Define a type which was already declared.
308 define_type(Named_object*, Named_type*);
312 add_variable(const std::string&, Variable*);
314 // Add a sink--a reference to the blank identifier _.
318 // Add a named object to the current namespace. This is used for
319 // import . "package".
321 add_named_object(Named_object*);
323 // Return a name to use for a thunk function. A thunk function is
324 // one we create during the compilation, for a go statement or a
325 // defer statement or a method expression.
329 // Return whether an object is a thunk.
331 is_thunk(const Named_object*);
333 // Note that we've seen an interface type. This is used to build
334 // all required interface method tables.
336 record_interface_type(Interface_type*);
338 // Note that we need an initialization function.
341 { this->need_init_fn_ = true; }
343 // Clear out all names in file scope. This is called when we start
344 // parsing a new file.
348 // Traverse the tree. See the Traverse class.
352 // Define the predeclared global names.
354 define_global_names();
356 // Verify and complete all types.
360 // Lower the parse tree.
364 // Lower all the statements in a block.
366 lower_block(Named_object* function, Block*);
368 // Lower an expression.
370 lower_expression(Named_object* function, Statement_inserter*, Expression**);
374 lower_constant(Named_object*);
376 // Finalize the method lists and build stub methods for named types.
380 // Work out the types to use for unspecified variables and
385 // Type check the program.
389 // Check the types in a single block. This is used for complicated
392 check_types_in_block(Block*);
394 // Check for return statements.
396 check_return_statements();
402 // Add an import control function for an imported package to the
405 add_import_init_fn(const std::string& package_name,
406 const std::string& init_name, int prio);
408 // Turn short-cut operators (&&, ||) into explicit if statements.
412 // Use temporary variables to force order of evaluation.
416 // Build thunks for functions which call recover.
418 build_recover_thunks();
420 // Simplify statements which might use thunks: go and defer
423 simplify_thunk_statements();
425 // Convert named types to the backend representation.
427 convert_named_types();
429 // Convert named types in a list of bindings.
431 convert_named_types_in_bindings(Bindings*);
433 // True if named types have been converted to the backend
436 named_types_are_converted() const
437 { return this->named_types_are_converted_; }
439 // Write out the global values.
443 // Create trees for implicit builtin functions.
445 define_builtin_function_trees();
447 // Build a call to a builtin function. PDECL should point to a NULL
448 // initialized static pointer which will hold the fndecl. NAME is
449 // the name of the function. NARGS is the number of arguments.
450 // RETTYPE is the return type. It is followed by NARGS pairs of
451 // type and argument (both trees).
453 call_builtin(tree* pdecl, source_location, const char* name, int nargs,
456 // Build a call to the runtime error function.
458 runtime_error(int code, source_location);
460 // Build a builtin struct with a list of fields.
462 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
465 // Mark a function declaration as a builtin library function.
467 mark_fndecl_as_builtin_library(tree fndecl);
469 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
470 // the slice. VALUES points to the values. COUNT is the size,
471 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
474 slice_constructor(tree slice_type_tree, tree values, tree count,
477 // Build required interface method tables.
479 build_interface_method_tables();
481 // Build an interface method table for a type: a list of function
482 // pointers, one for each interface method. This returns a decl.
484 interface_method_table_for_type(const Interface_type*, Named_type*,
487 // Return a tree which allocate SIZE bytes to hold values of type
490 allocate_memory(Type *type, tree size, source_location);
492 // Return a type to use for pointer to const char.
494 const_char_pointer_type_tree();
496 // Build a string constant with the right type.
498 string_constant_tree(const std::string&);
500 // Build a Go string constant. This returns a pointer to the
503 go_string_constant_tree(const std::string&);
505 // Receive a value from a channel.
507 receive_from_channel(tree type_tree, tree channel, bool for_select,
510 // Return a tree for receiving an integer on a channel.
512 receive_as_64bit_integer(tree type, tree channel, bool blocking,
516 // Make a trampoline which calls FNADDR passing CLOSURE.
518 make_trampoline(tree fnaddr, tree closure, source_location);
521 // During parsing, we keep a stack of functions. Each function on
522 // the stack is one that we are currently parsing. For each
523 // function, we keep track of the current stack of blocks.
527 Named_object* function;
528 // The stack of active blocks in the function.
529 std::vector<Block*> blocks;
532 // The stack of functions.
533 typedef std::vector<Open_function> Open_functions;
535 // Set up the built-in unsafe package.
537 import_unsafe(const std::string&, bool is_exported, source_location);
539 // Add a new imported package.
541 add_package(const std::string& real_name, const std::string& alias,
542 const std::string& unique_prefix, source_location location);
544 // Return the current binding contour.
549 current_bindings() const;
551 // Return the current block.
555 // Get the name of the magic initialization function.
559 // Get the decl for the magic initialization function.
561 initialization_function_decl();
563 // Write the magic initialization function.
565 write_initialization_function(tree fndecl, tree init_stmt_list);
567 // Initialize imported packages.
571 // Register variables with the garbage collector.
573 register_gc_vars(const std::vector<Named_object*>&, tree*);
575 // Build a pointer to a Go string constant. This returns a pointer
578 ptr_go_string_constant_tree(const std::string&);
580 // Return the type of a trampoline.
582 trampoline_type_tree();
584 // Type used to map import names to packages.
585 typedef std::map<std::string, Package*> Imports;
587 // Type used to map package names to packages.
588 typedef std::map<std::string, Package*> Packages;
590 // Type used to map special names in the sys package.
591 typedef std::map<std::string, std::string> Sys_names;
593 // The backend generator.
595 // The package we are compiling.
597 // The list of currently open functions during parsing.
598 Open_functions functions_;
599 // The global binding contour. This includes the builtin functions
600 // and the package we are compiling.
602 // Mapping from import file names to packages.
604 // Whether the magic unsafe package was imported.
605 bool imported_unsafe_;
606 // Mapping from package names we have seen to packages. This does
607 // not include the package we are compiling.
609 // The functions named "init", if there are any.
610 std::vector<Named_object*> init_functions_;
611 // Whether we need a magic initialization function.
613 // The name of the magic initialization function.
614 std::string init_fn_name_;
615 // A list of import control variables for packages that we import.
616 std::set<Import_init> imported_init_fns_;
617 // The unique prefix used for all global symbols.
618 std::string unique_prefix_;
619 // Whether an explicit unique prefix was set by -fgo-prefix.
620 bool unique_prefix_specified_;
621 // A list of interface types defined while parsing.
622 std::vector<Interface_type*> interface_types_;
623 // Whether named types have been converted.
624 bool named_types_are_converted_;
627 // A block of statements.
632 Block(Block* enclosing, source_location);
634 // Return the enclosing block.
637 { return this->enclosing_; }
639 // Return the bindings of the block.
642 { return this->bindings_; }
646 { return this->bindings_; }
648 // Look at the block's statements.
649 const std::vector<Statement*>*
651 { return &this->statements_; }
653 // Return the start location. This is normally the location of the
654 // left curly brace which starts the block.
656 start_location() const
657 { return this->start_location_; }
659 // Return the end location. This is normally the location of the
660 // right curly brace which ends the block.
663 { return this->end_location_; }
665 // Add a statement to the block.
667 add_statement(Statement*);
669 // Add a statement to the front of the block.
671 add_statement_at_front(Statement*);
673 // Replace a statement in a block.
675 replace_statement(size_t index, Statement*);
677 // Add a Statement before statement number INDEX.
679 insert_statement_before(size_t index, Statement*);
681 // Add a Statement after statement number INDEX.
683 insert_statement_after(size_t index, Statement*);
685 // Set the end location of the block.
687 set_end_location(source_location location)
688 { this->end_location_ = location; }
690 // Traverse the tree.
694 // Set final types for unspecified variables and constants.
698 // Return true if execution of this block may fall through to the
701 may_fall_through() const;
703 // Convert the block to the backend representation.
705 get_backend(Translate_context*);
707 // Iterate over statements.
709 typedef std::vector<Statement*>::iterator iterator;
713 { return this->statements_.begin(); }
717 { return this->statements_.end(); }
722 // Statements in the block.
723 std::vector<Statement*> statements_;
726 // Location of start of block.
727 source_location start_location_;
728 // Location of end of block.
729 source_location end_location_;
737 Function(Function_type* type, Function*, Block*, source_location);
739 // Return the function's type.
742 { return this->type_; }
744 // Return the enclosing function if there is one.
747 { return this->enclosing_; }
749 // Set the enclosing function. This is used when building thunks
750 // for functions which call recover.
752 set_enclosing(Function* enclosing)
754 go_assert(this->enclosing_ == NULL);
755 this->enclosing_ = enclosing;
758 // The result variables.
759 typedef std::vector<Named_object*> Results;
761 // Create the result variables in the outer block.
763 create_result_variables(Gogo*);
765 // Update the named result variables when cloning a function which
768 update_result_variables();
770 // Return the result variables.
773 { return this->results_; }
775 // Whether the result variables have names.
777 results_are_named() const
778 { return this->results_are_named_; }
780 // Add a new field to the closure variable.
782 add_closure_field(Named_object* var, source_location loc)
783 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
785 // Whether this function needs a closure.
787 needs_closure() const
788 { return !this->closure_fields_.empty(); }
790 // Return the closure variable, creating it if necessary. This is
791 // passed to the function as a static chain parameter.
795 // Set the closure variable. This is used when building thunks for
796 // functions which call recover.
798 set_closure_var(Named_object* v)
800 go_assert(this->closure_var_ == NULL);
801 this->closure_var_ = v;
804 // Return the variable for a reference to field INDEX in the closure
807 enclosing_var(unsigned int index)
809 go_assert(index < this->closure_fields_.size());
810 return closure_fields_[index].first;
813 // Set the type of the closure variable if there is one.
817 // Get the block of statements associated with the function.
820 { return this->block_; }
822 // Get the location of the start of the function.
825 { return this->location_; }
827 // Return whether this function is actually a method.
831 // Add a label definition to the function.
833 add_label_definition(const std::string& label_name, source_location);
835 // Add a label reference to a function.
837 add_label_reference(const std::string& label_name);
839 // Warn about labels that are defined but not used.
841 check_labels() const;
843 // Whether this function calls the predeclared recover function.
845 calls_recover() const
846 { return this->calls_recover_; }
848 // Record that this function calls the predeclared recover function.
849 // This is set during the lowering pass.
852 { this->calls_recover_ = true; }
854 // Whether this is a recover thunk function.
856 is_recover_thunk() const
857 { return this->is_recover_thunk_; }
859 // Record that this is a thunk built for a function which calls
862 set_is_recover_thunk()
863 { this->is_recover_thunk_ = true; }
865 // Whether this function already has a recover thunk.
867 has_recover_thunk() const
868 { return this->has_recover_thunk_; }
870 // Record that this function already has a recover thunk.
872 set_has_recover_thunk()
873 { this->has_recover_thunk_ = true; }
875 // Swap with another function. Used only for the thunk which calls
878 swap_for_recover(Function *);
880 // Traverse the tree.
884 // Determine types in the function.
888 // Return the function's decl given an identifier.
890 get_or_make_decl(Gogo*, Named_object*, tree id);
892 // Return the function's decl after it has been built.
896 go_assert(this->fndecl_ != NULL);
897 return this->fndecl_;
900 // Set the function decl to hold a tree of the function code.
902 build_tree(Gogo*, Named_object*);
904 // Get the value to return when not explicitly specified. May also
905 // add statements to execute first to STMT_LIST.
907 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
909 // Get a tree for the variable holding the defer stack.
911 defer_stack(source_location);
913 // Export the function.
915 export_func(Export*, const std::string& name) const;
917 // Export a function with a type.
919 export_func_with_type(Export*, const std::string& name,
920 const Function_type*);
922 // Import a function.
924 import_func(Import*, std::string* pname, Typed_identifier** receiver,
925 Typed_identifier_list** pparameters,
926 Typed_identifier_list** presults, bool* is_varargs);
929 // Type for mapping from label names to Label objects.
930 typedef Unordered_map(std::string, Label*) Labels;
933 make_receiver_parm_decl(Gogo*, Named_object*, tree);
936 copy_parm_to_heap(Gogo*, Named_object*, tree);
939 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
941 typedef std::vector<std::pair<Named_object*,
942 source_location> > Closure_fields;
944 // The function's type.
945 Function_type* type_;
946 // The enclosing function. This is NULL when there isn't one, which
947 // is the normal case.
948 Function* enclosing_;
949 // The result variables, if any.
951 // If there is a closure, this is the list of variables which appear
952 // in the closure. This is created by the parser, and then resolved
953 // to a real type when we lower parse trees.
954 Closure_fields closure_fields_;
955 // The closure variable, passed as a parameter using the static
956 // chain parameter. Normally NULL.
957 Named_object* closure_var_;
958 // The outer block of statements in the function.
960 // The source location of the start of the function.
961 source_location location_;
962 // Labels defined or referenced in the function.
964 // The function decl.
966 // The defer stack variable. A pointer to this variable is used to
967 // distinguish the defer stack for one function from another. This
968 // is NULL unless we actually need a defer stack.
969 Temporary_statement* defer_stack_;
970 // True if the result variables are named.
971 bool results_are_named_;
972 // True if this function calls the predeclared recover function.
974 // True if this a thunk built for a function which calls recover.
975 bool is_recover_thunk_;
976 // True if this function already has a recover thunk.
977 bool has_recover_thunk_;
980 // A function declaration.
982 class Function_declaration
985 Function_declaration(Function_type* fntype, source_location location)
986 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
991 { return this->fntype_; }
995 { return this->location_; }
999 { return this->asm_name_; }
1001 // Set the assembler name.
1003 set_asm_name(const std::string& asm_name)
1004 { this->asm_name_ = asm_name; }
1006 // Return a decl for the function given an identifier.
1008 get_or_make_decl(Gogo*, Named_object*, tree id);
1010 // Export a function declaration.
1012 export_func(Export* exp, const std::string& name) const
1013 { Function::export_func_with_type(exp, name, this->fntype_); }
1016 // The type of the function.
1017 Function_type* fntype_;
1018 // The location of the declaration.
1019 source_location location_;
1020 // The assembler name: this is the name to use in references to the
1021 // function. This is normally empty.
1022 std::string asm_name_;
1023 // The function decl if needed.
1032 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1033 bool is_receiver, source_location);
1035 // Get the type of the variable.
1042 // Return whether the type is defined yet.
1045 { return this->type_ != NULL; }
1047 // Get the initial value.
1050 { return this->init_; }
1052 // Return whether there are any preinit statements.
1054 has_pre_init() const
1055 { return this->preinit_ != NULL; }
1057 // Return the preinit statements if any.
1060 { return this->preinit_; }
1062 // Return whether this is a global variable.
1065 { return this->is_global_; }
1067 // Return whether this is a function parameter.
1069 is_parameter() const
1070 { return this->is_parameter_; }
1072 // Return whether this is the receiver parameter of a method.
1075 { return this->is_receiver_; }
1077 // Change this parameter to be a receiver. This is used when
1078 // creating the thunks created for functions which call recover.
1082 go_assert(this->is_parameter_);
1083 this->is_receiver_ = true;
1086 // Change this parameter to not be a receiver. This is used when
1087 // creating the thunks created for functions which call recover.
1089 set_is_not_receiver()
1091 go_assert(this->is_parameter_);
1092 this->is_receiver_ = false;
1095 // Return whether this is the varargs parameter of a function.
1097 is_varargs_parameter() const
1098 { return this->is_varargs_parameter_; }
1100 // Whether this variable's address is taken.
1102 is_address_taken() const
1103 { return this->is_address_taken_; }
1105 // Whether this variable should live in the heap.
1108 { return this->is_address_taken_ && !this->is_global_; }
1110 // Note that something takes the address of this variable.
1113 { this->is_address_taken_ = true; }
1115 // Return whether the address is taken but does not escape.
1117 is_non_escaping_address_taken() const
1118 { return this->is_non_escaping_address_taken_; }
1120 // Note that something takes the address of this variable such that
1121 // the address does not escape the function.
1123 set_non_escaping_address_taken()
1124 { this->is_non_escaping_address_taken_ = true; }
1126 // Get the source location of the variable's declaration.
1129 { return this->location_; }
1131 // Record that this is the varargs parameter of a function.
1133 set_is_varargs_parameter()
1135 go_assert(this->is_parameter_);
1136 this->is_varargs_parameter_ = true;
1139 // Clear the initial value; used for error handling.
1142 { this->init_ = NULL; }
1144 // Set the initial value; used for converting shortcuts.
1146 set_init(Expression* init)
1147 { this->init_ = init; }
1149 // Get the preinit block, a block of statements to be run before the
1150 // initialization expression.
1152 preinit_block(Gogo*);
1154 // Add a statement to be run before the initialization expression.
1155 // This is only used for global variables.
1157 add_preinit_statement(Gogo*, Statement*);
1159 // Lower the initialization expression after parsing is complete.
1161 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1163 // A special case: the init value is used only to determine the
1164 // type. This is used if the variable is defined using := with the
1165 // comma-ok form of a map index or a receive expression. The init
1166 // value is actually the map index expression or receive expression.
1167 // We use this because we may not know the right type at parse time.
1169 set_type_from_init_tuple()
1170 { this->type_from_init_tuple_ = true; }
1172 // Another special case: the init value is used only to determine
1173 // the type. This is used if the variable is defined using := with
1174 // a range clause. The init value is the range expression. The
1175 // type of the variable is the index type of the range expression
1176 // (i.e., the first value returned by a range).
1178 set_type_from_range_index()
1179 { this->type_from_range_index_ = true; }
1181 // Another special case: like set_type_from_range_index, but the
1182 // type is the value type of the range expression (i.e., the second
1183 // value returned by a range).
1185 set_type_from_range_value()
1186 { this->type_from_range_value_ = true; }
1188 // Another special case: the init value is used only to determine
1189 // the type. This is used if the variable is defined using := with
1190 // a case in a select statement. The init value is the channel.
1191 // The type of the variable is the channel's element type.
1193 set_type_from_chan_element()
1194 { this->type_from_chan_element_ = true; }
1196 // After we lower the select statement, we once again set the type
1197 // from the initialization expression.
1199 clear_type_from_chan_element()
1201 go_assert(this->type_from_chan_element_);
1202 this->type_from_chan_element_ = false;
1205 // Note that this variable was created for a type switch clause.
1207 set_is_type_switch_var()
1208 { this->is_type_switch_var_ = true; }
1210 // Traverse the initializer expression.
1212 traverse_expression(Traverse*, unsigned int traverse_mask);
1214 // Determine the type of the variable if necessary.
1218 // Get the backend representation of the variable.
1220 get_backend_variable(Gogo*, Named_object*, const Package*,
1221 const std::string&);
1223 // Get the initial value of the variable as a tree. This may only
1224 // be called if has_pre_init() returns false.
1226 get_init_tree(Gogo*, Named_object* function);
1228 // Return a series of statements which sets the value of the
1229 // variable in DECL. This should only be called is has_pre_init()
1230 // returns true. DECL may be NULL for a sink variable.
1232 get_init_block(Gogo*, Named_object* function, tree decl);
1234 // Export the variable.
1236 export_var(Export*, const std::string& name) const;
1238 // Import a variable.
1240 import_var(Import*, std::string* pname, Type** ptype);
1243 // The type of a tuple.
1245 type_from_tuple(Expression*, bool) const;
1247 // The type of a range.
1249 type_from_range(Expression*, bool, bool) const;
1251 // The element type of a channel.
1253 type_from_chan_element(Expression*, bool) const;
1255 // The variable's type. This may be NULL if the type is set from
1258 // The initial value. This may be NULL if the variable should be
1259 // initialized to the default value for the type.
1261 // Statements to run before the init statement.
1263 // Location of variable definition.
1264 source_location location_;
1265 // Backend representation.
1266 Bvariable* backend_;
1267 // Whether this is a global variable.
1268 bool is_global_ : 1;
1269 // Whether this is a function parameter.
1270 bool is_parameter_ : 1;
1271 // Whether this is the receiver parameter of a method.
1272 bool is_receiver_ : 1;
1273 // Whether this is the varargs parameter of a function.
1274 bool is_varargs_parameter_ : 1;
1275 // Whether something takes the address of this variable. For a
1276 // local variable this implies that the variable has to be on the
1278 bool is_address_taken_ : 1;
1279 // Whether something takes the address of this variable such that
1280 // the address does not escape the function.
1281 bool is_non_escaping_address_taken_ : 1;
1282 // True if we have seen this variable in a traversal.
1284 // True if we have lowered the initialization expression.
1285 bool init_is_lowered_ : 1;
1286 // True if init is a tuple used to set the type.
1287 bool type_from_init_tuple_ : 1;
1288 // True if init is a range clause and the type is the index type.
1289 bool type_from_range_index_ : 1;
1290 // True if init is a range clause and the type is the value type.
1291 bool type_from_range_value_ : 1;
1292 // True if init is a channel and the type is the channel's element type.
1293 bool type_from_chan_element_ : 1;
1294 // True if this is a variable created for a type switch case.
1295 bool is_type_switch_var_ : 1;
1296 // True if we have determined types.
1297 bool determined_type_ : 1;
1300 // A variable which is really the name for a function return value, or
1303 class Result_variable
1306 Result_variable(Type* type, Function* function, int index,
1307 source_location location)
1308 : type_(type), function_(function), index_(index), location_(location),
1309 backend_(NULL), is_address_taken_(false),
1310 is_non_escaping_address_taken_(false)
1313 // Get the type of the result variable.
1316 { return this->type_; }
1318 // Get the function that this is associated with.
1321 { return this->function_; }
1323 // Index in the list of function results.
1326 { return this->index_; }
1328 // The location of the variable definition.
1331 { return this->location_; }
1333 // Whether this variable's address is taken.
1335 is_address_taken() const
1336 { return this->is_address_taken_; }
1338 // Note that something takes the address of this variable.
1341 { this->is_address_taken_ = true; }
1343 // Return whether the address is taken but does not escape.
1345 is_non_escaping_address_taken() const
1346 { return this->is_non_escaping_address_taken_; }
1348 // Note that something takes the address of this variable such that
1349 // the address does not escape the function.
1351 set_non_escaping_address_taken()
1352 { this->is_non_escaping_address_taken_ = true; }
1354 // Whether this variable should live in the heap.
1357 { return this->is_address_taken_; }
1359 // Set the function. This is used when cloning functions which call
1362 set_function(Function* function)
1363 { this->function_ = function; }
1365 // Get the backend representation of the variable.
1367 get_backend_variable(Gogo*, Named_object*, const std::string&);
1370 // Type of result variable.
1372 // Function with which this is associated.
1373 Function* function_;
1374 // Index in list of results.
1376 // Where the result variable is defined.
1377 source_location location_;
1378 // Backend representation.
1379 Bvariable* backend_;
1380 // Whether something takes the address of this variable.
1381 bool is_address_taken_;
1382 // Whether something takes the address of this variable such that
1383 // the address does not escape the function.
1384 bool is_non_escaping_address_taken_;
1387 // The value we keep for a named constant. This lets us hold a type
1388 // and an expression.
1390 class Named_constant
1393 Named_constant(Type* type, Expression* expr, int iota_value,
1394 source_location location)
1395 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1401 { return this->type_; }
1405 { return this->expr_; }
1409 { return this->iota_value_; }
1413 { return this->location_; }
1415 // Whether we are lowering.
1418 { return this->lowering_; }
1420 // Set that we are lowering.
1423 { this->lowering_ = true; }
1425 // We are no longer lowering.
1428 { this->lowering_ = false; }
1430 // Traverse the expression.
1432 traverse_expression(Traverse*);
1434 // Determine the type of the constant if necessary.
1438 // Indicate that we found and reported an error for this constant.
1442 // Export the constant.
1444 export_const(Export*, const std::string& name) const;
1446 // Import a constant.
1448 import_const(Import*, std::string*, Type**, Expression**);
1451 // The type of the constant.
1453 // The expression for the constant.
1455 // If the predeclared constant iota is used in EXPR_, this is the
1456 // value it will have. We do this because at parse time we don't
1457 // know whether the name "iota" will refer to the predeclared
1458 // constant or to something else. We put in the right value in when
1461 // The location of the definition.
1462 source_location location_;
1463 // Whether we are currently lowering this constant.
1467 // A type declaration.
1469 class Type_declaration
1472 Type_declaration(source_location location)
1473 : location_(location), in_function_(NULL), methods_(),
1474 issued_warning_(false)
1477 // Return the location.
1480 { return this->location_; }
1482 // Return the function in which this type is declared. This will
1483 // return NULL for a type declared in global scope.
1486 { return this->in_function_; }
1488 // Set the function in which this type is declared.
1490 set_in_function(Named_object* f)
1491 { this->in_function_ = f; }
1493 // Add a method to this type. This is used when methods are defined
1496 add_method(const std::string& name, Function* function);
1498 // Add a method declaration to this type.
1500 add_method_declaration(const std::string& name, Function_type* type,
1501 source_location location);
1503 // Return whether any methods were defined.
1505 has_methods() const;
1507 // Define methods when the real type is known.
1509 define_methods(Named_type*);
1511 // This is called if we are trying to use this type. It returns
1512 // true if we should issue a warning.
1517 typedef std::vector<Named_object*> Methods;
1519 // The location of the type declaration.
1520 source_location location_;
1521 // If this type is declared in a function, a pointer back to the
1522 // function in which it is defined.
1523 Named_object* in_function_;
1524 // Methods defined before the type is defined.
1526 // True if we have issued a warning about a use of this type
1527 // declaration when it is undefined.
1528 bool issued_warning_;
1531 // An unknown object. These are created by the parser for forward
1532 // references to names which have not been seen before. In a correct
1533 // program, these will always point to a real definition by the end of
1534 // the parse. Because they point to another Named_object, these may
1535 // only be referenced by Unknown_expression objects.
1540 Unknown_name(source_location location)
1541 : location_(location), real_named_object_(NULL)
1544 // Return the location where this name was first seen.
1547 { return this->location_; }
1549 // Return the real named object that this points to, or NULL if it
1550 // was never resolved.
1552 real_named_object() const
1553 { return this->real_named_object_; }
1555 // Set the real named object that this points to.
1557 set_real_named_object(Named_object* no);
1560 // The location where this name was first seen.
1561 source_location location_;
1562 // The real named object when it is known.
1567 // A named object named. This is the result of a declaration. We
1568 // don't use a superclass because they all have to be handled
1576 // An uninitialized Named_object. We should never see this.
1577 NAMED_OBJECT_UNINITIALIZED,
1578 // An unknown name. This is used for forward references. In a
1579 // correct program, these will all be resolved by the end of the
1581 NAMED_OBJECT_UNKNOWN,
1586 // A forward type declaration.
1587 NAMED_OBJECT_TYPE_DECLARATION,
1590 // A result variable in a function.
1591 NAMED_OBJECT_RESULT_VAR,
1592 // The blank identifier--the special variable named _.
1596 // A forward func declaration.
1597 NAMED_OBJECT_FUNC_DECLARATION,
1599 NAMED_OBJECT_PACKAGE
1602 // Return the classification.
1604 classification() const
1605 { return this->classification_; }
1611 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1615 { return this->classification_ == NAMED_OBJECT_CONST; }
1619 { return this->classification_ == NAMED_OBJECT_TYPE; }
1622 is_type_declaration() const
1623 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1627 { return this->classification_ == NAMED_OBJECT_VAR; }
1630 is_result_variable() const
1631 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1635 { return this->classification_ == NAMED_OBJECT_SINK; }
1639 { return this->classification_ == NAMED_OBJECT_FUNC; }
1642 is_function_declaration() const
1643 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1647 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1651 static Named_object*
1652 make_unknown_name(const std::string& name, source_location);
1654 static Named_object*
1655 make_constant(const Typed_identifier&, const Package*, Expression*,
1658 static Named_object*
1659 make_type(const std::string&, const Package*, Type*, source_location);
1661 static Named_object*
1662 make_type_declaration(const std::string&, const Package*, source_location);
1664 static Named_object*
1665 make_variable(const std::string&, const Package*, Variable*);
1667 static Named_object*
1668 make_result_variable(const std::string&, Result_variable*);
1670 static Named_object*
1673 static Named_object*
1674 make_function(const std::string&, const Package*, Function*);
1676 static Named_object*
1677 make_function_declaration(const std::string&, const Package*, Function_type*,
1680 static Named_object*
1681 make_package(const std::string& alias, Package* package);
1688 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1689 return this->u_.unknown_value;
1693 unknown_value() const
1695 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1696 return this->u_.unknown_value;
1702 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1703 return this->u_.const_value;
1706 const Named_constant*
1709 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1710 return this->u_.const_value;
1716 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1717 return this->u_.type_value;
1723 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1724 return this->u_.type_value;
1728 type_declaration_value()
1730 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1731 return this->u_.type_declaration;
1734 const Type_declaration*
1735 type_declaration_value() const
1737 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1738 return this->u_.type_declaration;
1744 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1745 return this->u_.var_value;
1751 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1752 return this->u_.var_value;
1758 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1759 return this->u_.result_var_value;
1762 const Result_variable*
1763 result_var_value() const
1765 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1766 return this->u_.result_var_value;
1772 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1773 return this->u_.func_value;
1779 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1780 return this->u_.func_value;
1783 Function_declaration*
1784 func_declaration_value()
1786 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1787 return this->u_.func_declaration_value;
1790 const Function_declaration*
1791 func_declaration_value() const
1793 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1794 return this->u_.func_declaration_value;
1800 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1801 return this->u_.package_value;
1805 package_value() const
1807 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1808 return this->u_.package_value;
1813 { return this->name_; }
1815 // Return the name to use in an error message. The difference is
1816 // that if this Named_object is defined in a different package, this
1817 // will return PACKAGE.NAME.
1819 message_name() const;
1823 { return this->package_; }
1825 // Resolve an unknown value if possible. This returns the same
1826 // Named_object or a new one.
1830 Named_object* ret = this;
1831 if (this->is_unknown())
1833 Named_object* r = this->unknown_value()->real_named_object();
1843 const Named_object* ret = this;
1844 if (this->is_unknown())
1846 const Named_object* r = this->unknown_value()->real_named_object();
1853 // The location where this object was defined or referenced.
1857 // Convert a variable to the backend representation.
1859 get_backend_variable(Gogo*, Named_object* function);
1861 // Return a tree for the external identifier for this object.
1865 // Return a tree representing this object.
1867 get_tree(Gogo*, Named_object* function);
1869 // Define a type declaration.
1871 set_type_value(Named_type*);
1873 // Define a function declaration.
1875 set_function_value(Function*);
1877 // Declare an unknown name as a type declaration.
1881 // Export this object.
1883 export_named_object(Export*) const;
1886 Named_object(const std::string&, const Package*, Classification);
1888 // The name of the object.
1890 // The package that this object is in. This is NULL if it is in the
1891 // file we are compiling.
1892 const Package* package_;
1893 // The type of object this is.
1894 Classification classification_;
1898 Unknown_name* unknown_value;
1899 Named_constant* const_value;
1900 Named_type* type_value;
1901 Type_declaration* type_declaration;
1902 Variable* var_value;
1903 Result_variable* result_var_value;
1904 Function* func_value;
1905 Function_declaration* func_declaration_value;
1906 Package* package_value;
1908 // The DECL tree for this object if we have already converted it.
1912 // A binding contour. This binds names to objects.
1917 // Type for mapping from names to objects.
1918 typedef Unordered_map(std::string, Named_object*) Contour;
1920 Bindings(Bindings* enclosing);
1922 // Add an unknown name.
1924 add_unknown_name(const std::string& name, source_location location)
1926 return this->add_named_object(Named_object::make_unknown_name(name,
1932 add_constant(const Typed_identifier& tid, const Package* package,
1933 Expression* expr, int iota_value)
1935 return this->add_named_object(Named_object::make_constant(tid, package,
1942 add_type(const std::string& name, const Package* package, Type* type,
1943 source_location location)
1945 return this->add_named_object(Named_object::make_type(name, package, type,
1949 // Add a named type. This is used for builtin types, and to add an
1950 // imported type to the global scope.
1952 add_named_type(Named_type* named_type);
1954 // Add a type declaration.
1956 add_type_declaration(const std::string& name, const Package* package,
1957 source_location location)
1959 Named_object* no = Named_object::make_type_declaration(name, package,
1961 return this->add_named_object(no);
1966 add_variable(const std::string& name, const Package* package,
1969 return this->add_named_object(Named_object::make_variable(name, package,
1973 // Add a result variable.
1975 add_result_variable(const std::string& name, Result_variable* result)
1977 return this->add_named_object(Named_object::make_result_variable(name,
1983 add_function(const std::string& name, const Package*, Function* function);
1985 // Add a function declaration.
1987 add_function_declaration(const std::string& name, const Package* package,
1988 Function_type* type, source_location location);
1990 // Add a package. The location is the location of the import
1993 add_package(const std::string& alias, Package* package)
1995 Named_object* no = Named_object::make_package(alias, package);
1996 return this->add_named_object(no);
1999 // Define a type which was already declared.
2001 define_type(Named_object*, Named_type*);
2003 // Add a method to the list of objects. This is not added to the
2006 add_method(Named_object*);
2008 // Add a named object to this binding.
2010 add_named_object(Named_object* no)
2011 { return this->add_named_object_to_contour(&this->bindings_, no); }
2013 // Clear all names in file scope from the bindings.
2017 // Look up a name in this binding contour and in any enclosing
2018 // binding contours. This returns NULL if the name is not found.
2020 lookup(const std::string&) const;
2022 // Look up a name in this binding contour without looking in any
2023 // enclosing binding contours. Returns NULL if the name is not found.
2025 lookup_local(const std::string&) const;
2029 remove_binding(Named_object*);
2031 // Traverse the tree. See the Traverse class.
2033 traverse(Traverse*, bool is_global);
2035 // Iterate over definitions. This does not include things which
2036 // were only declared.
2038 typedef std::vector<Named_object*>::const_iterator
2039 const_definitions_iterator;
2041 const_definitions_iterator
2042 begin_definitions() const
2043 { return this->named_objects_.begin(); }
2045 const_definitions_iterator
2046 end_definitions() const
2047 { return this->named_objects_.end(); }
2049 // Return the number of definitions.
2051 size_definitions() const
2052 { return this->named_objects_.size(); }
2054 // Return whether there are no definitions.
2056 empty_definitions() const
2057 { return this->named_objects_.empty(); }
2059 // Iterate over declarations. This is everything that has been
2060 // declared, which includes everything which has been defined.
2062 typedef Contour::const_iterator const_declarations_iterator;
2064 const_declarations_iterator
2065 begin_declarations() const
2066 { return this->bindings_.begin(); }
2068 const_declarations_iterator
2069 end_declarations() const
2070 { return this->bindings_.end(); }
2072 // Return the number of declarations.
2074 size_declarations() const
2075 { return this->bindings_.size(); }
2077 // Return whether there are no declarations.
2079 empty_declarations() const
2080 { return this->bindings_.empty(); }
2082 // Return the first declaration.
2085 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2089 add_named_object_to_contour(Contour*, Named_object*);
2092 new_definition(Named_object*, Named_object*);
2094 // Enclosing bindings.
2095 Bindings* enclosing_;
2096 // The list of objects.
2097 std::vector<Named_object*> named_objects_;
2098 // The mapping from names to objects.
2107 Label(const std::string& name)
2108 : name_(name), location_(0), is_used_(false), blabel_(NULL)
2111 // Return the label's name.
2114 { return this->name_; }
2116 // Return whether the label has been defined.
2119 { return this->location_ != 0; }
2121 // Return whether the label has been used.
2124 { return this->is_used_; }
2126 // Record that the label is used.
2129 { this->is_used_ = true; }
2131 // Return the location of the definition.
2134 { return this->location_; }
2136 // Define the label at LOCATION.
2138 define(source_location location)
2140 go_assert(this->location_ == 0);
2141 this->location_ = location;
2144 // Return the backend representation for this label.
2146 get_backend_label(Translate_context*);
2148 // Return an expression for the address of this label. This is used
2149 // to get the return address of a deferred function to see whether
2150 // the function may call recover.
2152 get_addr(Translate_context*, source_location location);
2155 // The name of the label.
2157 // The location of the definition. This is 0 if the label has not
2158 // yet been defined.
2159 source_location location_;
2160 // Whether the label has been used.
2162 // The backend representation.
2166 // An unnamed label. These are used when lowering loops.
2171 Unnamed_label(source_location location)
2172 : location_(location), blabel_(NULL)
2175 // Get the location where the label is defined.
2178 { return this->location_; }
2180 // Set the location where the label is defined.
2182 set_location(source_location location)
2183 { this->location_ = location; }
2185 // Return a statement which defines this label.
2187 get_definition(Translate_context*);
2189 // Return a goto to this label from LOCATION.
2191 get_goto(Translate_context*, source_location location);
2194 // Return the backend representation.
2196 get_blabel(Translate_context*);
2198 // The location where the label is defined.
2199 source_location location_;
2200 // The backend representation of this label.
2204 // An imported package.
2209 Package(const std::string& name, const std::string& unique_prefix,
2210 source_location location);
2212 // The real name of this package. This may be different from the
2213 // name in the associated Named_object if the import statement used
2217 { return this->name_; }
2219 // Return the location of the import statement.
2222 { return this->location_; }
2224 // Get the unique prefix used for all symbols exported from this
2227 unique_prefix() const
2229 go_assert(!this->unique_prefix_.empty());
2230 return this->unique_prefix_;
2233 // The priority of this package. The init function of packages with
2234 // lower priority must be run before the init function of packages
2235 // with higher priority.
2238 { return this->priority_; }
2240 // Set the priority.
2242 set_priority(int priority);
2244 // Return the bindings.
2247 { return this->bindings_; }
2249 // Whether some symbol from the package was used.
2252 { return this->used_; }
2254 // Note that some symbol from this package was used.
2257 { this->used_ = true; }
2259 // Clear the used field for the next file.
2262 { this->used_ = false; }
2264 // Whether this package was imported in the current file.
2267 { return this->is_imported_; }
2269 // Note that this package was imported in the current file.
2272 { this->is_imported_ = true; }
2274 // Clear the imported field for the next file.
2277 { this->is_imported_ = false; }
2279 // Whether this package was imported with a name of "_".
2281 uses_sink_alias() const
2282 { return this->uses_sink_alias_; }
2284 // Note that this package was imported with a name of "_".
2286 set_uses_sink_alias()
2287 { this->uses_sink_alias_ = true; }
2289 // Clear the sink alias field for the next file.
2291 clear_uses_sink_alias()
2292 { this->uses_sink_alias_ = false; }
2294 // Look up a name in the package. Returns NULL if the name is not
2297 lookup(const std::string& name) const
2298 { return this->bindings_->lookup(name); }
2300 // Set the location of the package. This is used if it is seen in a
2301 // different import before it is really imported.
2303 set_location(source_location location)
2304 { this->location_ = location; }
2306 // Add a constant to the package.
2308 add_constant(const Typed_identifier& tid, Expression* expr)
2309 { return this->bindings_->add_constant(tid, this, expr, 0); }
2311 // Add a type to the package.
2313 add_type(const std::string& name, Type* type, source_location location)
2314 { return this->bindings_->add_type(name, this, type, location); }
2316 // Add a type declaration to the package.
2318 add_type_declaration(const std::string& name, source_location location)
2319 { return this->bindings_->add_type_declaration(name, this, location); }
2321 // Add a variable to the package.
2323 add_variable(const std::string& name, Variable* variable)
2324 { return this->bindings_->add_variable(name, this, variable); }
2326 // Add a function declaration to the package.
2328 add_function_declaration(const std::string& name, Function_type* type,
2329 source_location loc)
2330 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2332 // Determine types of constants.
2337 // The real name of this package.
2339 // The unique prefix for all exported global symbols.
2340 std::string unique_prefix_;
2341 // The names in this package.
2342 Bindings* bindings_;
2343 // The priority of this package. A package has a priority higher
2344 // than the priority of all of the packages that it imports. This
2345 // is used to run init functions in the right order.
2347 // The location of the import statement.
2348 source_location location_;
2349 // True if some name from this package was used. This is mutable
2350 // because we can use a package even if we have a const pointer to
2353 // True if this package was imported in the current file.
2355 // True if this package was imported with a name of "_".
2356 bool uses_sink_alias_;
2359 // Return codes for the traversal functions. This is not an enum
2360 // because we want to be able to declare traversal functions in other
2361 // header files without including this one.
2363 // Continue traversal as usual.
2364 const int TRAVERSE_CONTINUE = -1;
2367 const int TRAVERSE_EXIT = 0;
2369 // Continue traversal, but skip components of the current object.
2370 // E.g., if this is returned by Traverse::statement, we do not
2371 // traverse the expressions in the statement even if
2372 // traverse_expressions is set in the traverse_mask.
2373 const int TRAVERSE_SKIP_COMPONENTS = 1;
2375 // This class is used when traversing the parse tree. The caller uses
2376 // a subclass which overrides functions as desired.
2381 // These bitmasks say what to traverse.
2382 static const unsigned int traverse_variables = 0x1;
2383 static const unsigned int traverse_constants = 0x2;
2384 static const unsigned int traverse_functions = 0x4;
2385 static const unsigned int traverse_blocks = 0x8;
2386 static const unsigned int traverse_statements = 0x10;
2387 static const unsigned int traverse_expressions = 0x20;
2388 static const unsigned int traverse_types = 0x40;
2390 Traverse(unsigned int traverse_mask)
2391 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2394 virtual ~Traverse();
2396 // The bitmask of what to traverse.
2398 traverse_mask() const
2399 { return this->traverse_mask_; }
2401 // Record that we are going to traverse a type. This returns true
2402 // if the type has already been seen in this traversal. This is
2403 // required because types, unlike expressions, can form a circular
2406 remember_type(const Type*);
2408 // Record that we are going to see an expression. This returns true
2409 // if the expression has already been seen in this traversal. This
2410 // is only needed for cases where multiple expressions can point to
2413 remember_expression(const Expression*);
2415 // These functions return one of the TRAVERSE codes defined above.
2417 // If traverse_variables is set in the mask, this is called for
2418 // every variable in the tree.
2420 variable(Named_object*);
2422 // If traverse_constants is set in the mask, this is called for
2423 // every named constant in the tree. The bool parameter is true for
2424 // a global constant.
2426 constant(Named_object*, bool);
2428 // If traverse_functions is set in the mask, this is called for
2429 // every function in the tree.
2431 function(Named_object*);
2433 // If traverse_blocks is set in the mask, this is called for every
2434 // block in the tree.
2438 // If traverse_statements is set in the mask, this is called for
2439 // every statement in the tree.
2441 statement(Block*, size_t* index, Statement*);
2443 // If traverse_expressions is set in the mask, this is called for
2444 // every expression in the tree.
2446 expression(Expression**);
2448 // If traverse_types is set in the mask, this is called for every
2449 // type in the tree.
2454 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2455 Type_identical) Types_seen;
2457 typedef Unordered_set(const Expression*) Expressions_seen;
2459 // Bitmask of what sort of objects to traverse.
2460 unsigned int traverse_mask_;
2461 // Types which have been seen in this traversal.
2462 Types_seen* types_seen_;
2463 // Expressions which have been seen in this traversal.
2464 Expressions_seen* expressions_seen_;
2467 // A class which makes it easier to insert new statements before the
2468 // current statement during a traversal.
2470 class Statement_inserter
2473 // Empty constructor.
2474 Statement_inserter()
2475 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2478 // Constructor for a statement in a block.
2479 Statement_inserter(Block* block, size_t *pindex)
2480 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2483 // Constructor for a global variable.
2484 Statement_inserter(Gogo* gogo, Variable* var)
2485 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2486 { go_assert(var->is_global()); }
2488 // We use the default copy constructor and assignment operator.
2490 // Insert S before the statement we are traversing, or before the
2491 // initialization expression of a global variable.
2493 insert(Statement* s);
2496 // The block that the statement is in.
2498 // The index of the statement that we are traversing.
2500 // The IR, needed when looking at an initializer expression for a
2503 // The global variable, when looking at an initializer expression.
2507 // When translating the gogo IR into the backend data structure, this
2508 // is the context we pass down the blocks and statements.
2510 class Translate_context
2513 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2515 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2516 block_(block), bblock_(bblock), is_const_(false)
2523 { return this->gogo_; }
2527 { return this->backend_; }
2531 { return this->function_; }
2535 { return this->block_; }
2539 { return this->bblock_; }
2543 { return this->is_const_; }
2545 // Make a constant context.
2548 { this->is_const_ = true; }
2551 // The IR for the entire compilation unit.
2553 // The generator for the backend data structures.
2555 // The function we are currently translating. NULL if not in a
2556 // function, e.g., the initializer of a global variable.
2557 Named_object* function_;
2558 // The block we are currently translating. NULL if not in a
2561 // The backend representation of the current block. NULL if block_
2564 // Whether this is being evaluated in a constant context. This is
2565 // used for type descriptor initializers.
2569 // Runtime error codes. These must match the values in
2570 // libgo/runtime/go-runtime-error.c.
2572 // Slice index out of bounds: negative or larger than the length of
2574 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2576 // Array index out of bounds.
2577 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2579 // String index out of bounds.
2580 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2582 // Slice slice out of bounds: negative or larger than the length of
2583 // the slice or high bound less than low bound.
2584 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2586 // Array slice out of bounds.
2587 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2589 // String slice out of bounds.
2590 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2592 // Dereference of nil pointer. This is used when there is a
2593 // dereference of a pointer to a very large struct or array, to ensure
2594 // that a gigantic array is not used a proxy to access random memory
2596 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2598 // Slice length or capacity out of bounds in make: negative or
2599 // overflow or length greater than capacity.
2600 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2602 // Map capacity out of bounds in make: negative or overflow.
2603 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2605 // Channel capacity out of bounds in make: negative or overflow.
2606 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2608 // This is used by some of the langhooks.
2609 extern Gogo* go_get_gogo();
2611 // Whether we have seen any errors. FIXME: Replace with a backend
2613 extern bool saw_errors();
2615 #endif // !defined(GO_GOGO_H)