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;
28 class Struct_field_list;
34 class Forward_declaration_type;
39 class Translate_context;
43 // This file declares the basic classes used to hold the internal
44 // representation of Go which is built by the parser.
46 // An initialization function for an imported package. This is a
47 // magic function which initializes variables and runs the "init"
53 Import_init(const std::string& package_name, const std::string& init_name,
55 : package_name_(package_name), init_name_(init_name), priority_(priority)
58 // The name of the package being imported.
61 { return this->package_name_; }
63 // The name of the package's init function.
66 { return this->init_name_; }
68 // The priority of the initialization function. Functions with a
69 // lower priority number must be run first.
72 { return this->priority_; }
75 // The name of the package being imported.
76 std::string package_name_;
77 // The name of the package's init function.
78 std::string init_name_;
83 // For sorting purposes.
86 operator<(const Import_init& i1, const Import_init& i2)
88 if (i1.priority() < i2.priority())
90 if (i1.priority() > i2.priority())
92 if (i1.package_name() != i2.package_name())
93 return i1.package_name() < i2.package_name();
94 return i1.init_name() < i2.init_name();
97 // The holder for the internal representation of the entire
103 // Create the IR, passing in the sizes of the types "int" and
104 // "uintptr" in bits.
105 Gogo(int int_type_size, int pointer_size);
107 // Get the package name.
109 package_name() const;
111 // Set the package name.
113 set_package_name(const std::string&, source_location);
115 // Return whether this is the "main" package.
117 is_main_package() const;
119 // If necessary, adjust the name to use for a hidden symbol. We add
120 // a prefix of the package name, so that hidden symbols in different
121 // packages do not collide.
123 pack_hidden_name(const std::string& name, bool is_exported) const
127 : ('.' + this->unique_prefix()
128 + '.' + this->package_name()
132 // Unpack a name which may have been hidden. Returns the
133 // user-visible name of the object.
135 unpack_hidden_name(const std::string& name)
136 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
138 // Return whether a possibly packed name is hidden.
140 is_hidden_name(const std::string& name)
141 { return name[0] == '.'; }
143 // Return the package prefix of a hidden name.
145 hidden_name_prefix(const std::string& name)
147 gcc_assert(Gogo::is_hidden_name(name));
148 return name.substr(1, name.rfind('.') - 1);
151 // Given a name which may or may not have been hidden, return the
152 // name to use in an error message.
154 message_name(const std::string& name);
156 // Return whether a name is the blank identifier _.
158 is_sink_name(const std::string& name)
160 return (name[0] == '.'
161 && name[name.length() - 1] == '_'
162 && name[name.length() - 2] == '.');
165 // Return the unique prefix to use for all exported symbols.
167 unique_prefix() const;
169 // Set the unique prefix.
171 set_unique_prefix(const std::string&);
173 // Return the priority to use for the package we are compiling.
174 // This is two more than the largest priority of any package we
177 package_priority() const;
179 // Import a package. FILENAME is the file name argument, LOCAL_NAME
180 // is the local name to give to the package. If LOCAL_NAME is empty
181 // the declarations are added to the global scope.
183 import_package(const std::string& filename, const std::string& local_name,
184 bool is_local_name_exported, source_location);
186 // Whether we are the global binding level.
188 in_global_scope() const;
190 // Look up a name in the current binding contours.
192 lookup(const std::string&, Named_object** pfunction) const;
194 // Look up a name in the current block.
196 lookup_in_block(const std::string&) const;
198 // Look up a name in the global namespace--the universal scope.
200 lookup_global(const char*) const;
202 // Add a new imported package. REAL_NAME is the real name of the
203 // package. ALIAS is the alias of the package; this may be the same
204 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
205 // this package should be added to the global namespace; this is
206 // true if the alias is ".". LOCATION is the location of the import
207 // statement. This returns the new package, or NULL on error.
209 add_imported_package(const std::string& real_name, const std::string& alias,
210 bool is_alias_exported,
211 const std::string& unique_prefix,
212 source_location location,
213 bool* padd_to_globals);
215 // Register a package. This package may or may not be imported.
216 // This returns the Package structure for the package, creating if
219 register_package(const std::string& name, const std::string& unique_prefix,
222 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
223 // method function should be added to the type of its receiver.
225 start_function(const std::string& name, Function_type* type,
226 bool add_method_to_type, source_location);
228 // Finish compiling a function.
230 finish_function(source_location);
232 // Return the current function.
234 current_function() const;
236 // Start a new block. This is not initially associated with a
239 start_block(source_location);
241 // Finish the current block and return it.
243 finish_block(source_location);
245 // Declare an unknown name. This is used while parsing. The name
246 // must be resolved by the end of the parse. Unknown names are
247 // always added at the package level.
249 add_unknown_name(const std::string& name, source_location);
251 // Declare a function.
253 declare_function(const std::string&, Function_type*, source_location);
257 add_label_definition(const std::string&, source_location);
259 // Add a label reference.
261 add_label_reference(const std::string&);
263 // Add a statement to the current block.
265 add_statement(Statement*);
267 // Add a block to the current block.
269 add_block(Block*, source_location);
273 add_constant(const Typed_identifier&, Expression*, int iota_value);
277 add_type(const std::string&, Type*, source_location);
279 // Add a named type. This is used for builtin types, and to add an
280 // imported type to the global scope.
282 add_named_type(Named_type*);
286 declare_type(const std::string&, source_location);
288 // Declare a type at the package level. This is used when the
289 // parser sees an unknown name where a type name is required.
291 declare_package_type(const std::string&, source_location);
293 // Define a type which was already declared.
295 define_type(Named_object*, Named_type*);
299 add_variable(const std::string&, Variable*);
301 // Add a sink--a reference to the blank identifier _.
305 // Add a named object to the current namespace. This is used for
306 // import . "package".
308 add_named_object(Named_object*);
310 // Return a name to use for a thunk function. A thunk function is
311 // one we create during the compilation, for a go statement or a
312 // defer statement or a method expression.
316 // Return whether an object is a thunk.
318 is_thunk(const Named_object*);
320 // Note that we've seen an interface type. This is used to build
321 // all required interface method tables.
323 record_interface_type(Interface_type*);
325 // Note that we need an initialization function.
328 { this->need_init_fn_ = true; }
330 // Clear out all names in file scope. This is called when we start
331 // parsing a new file.
335 // Traverse the tree. See the Traverse class.
339 // Define the predeclared global names.
341 define_global_names();
343 // Verify and complete all types.
347 // Lower the parse tree.
351 // Lower all the statements in a block.
353 lower_block(Named_object* function, Block*);
355 // Lower an expression.
357 lower_expression(Named_object* function, Expression**);
361 lower_constant(Named_object*);
363 // Finalize the method lists and build stub methods for named types.
367 // Work out the types to use for unspecified variables and
372 // Type check the program.
376 // Check the types in a single block. This is used for complicated
379 check_types_in_block(Block*);
381 // Check for return statements.
383 check_return_statements();
389 // Add an import control function for an imported package to the
392 add_import_init_fn(const std::string& package_name,
393 const std::string& init_name, int prio);
395 // Turn short-cut operators (&&, ||) into explicit if statements.
399 // Use temporary variables to force order of evaluation.
403 // Build thunks for functions which call recover.
405 build_recover_thunks();
407 // Simplify statements which might use thunks: go and defer
410 simplify_thunk_statements();
412 // Convert named types to the backend representation.
414 convert_named_types();
416 // Convert named types in a list of bindings.
418 convert_named_types_in_bindings(Bindings*);
420 // True if named types have been converted to the backend
423 named_types_are_converted() const
424 { return this->named_types_are_converted_; }
426 // Write out the global values.
430 // Build a call to a builtin function. PDECL should point to a NULL
431 // initialized static pointer which will hold the fndecl. NAME is
432 // the name of the function. NARGS is the number of arguments.
433 // RETTYPE is the return type. It is followed by NARGS pairs of
434 // type and argument (both trees).
436 call_builtin(tree* pdecl, source_location, const char* name, int nargs,
439 // Build a call to the runtime error function.
441 runtime_error(int code, source_location);
443 // Build a builtin struct with a list of fields.
445 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
448 // Mark a function declaration as a builtin library function.
450 mark_fndecl_as_builtin_library(tree fndecl);
452 // Build the type of the struct that holds a slice for the given
455 slice_type_tree(tree element_type_tree);
457 // Given a tree for a slice type, return the tree for the element
460 slice_element_type_tree(tree slice_type_tree);
462 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
463 // the slice. VALUES points to the values. COUNT is the size,
464 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
467 slice_constructor(tree slice_type_tree, tree values, tree count,
470 // Build a constructor for an empty slice. SLICE_TYPE_TREE is the
471 // type of the slice.
473 empty_slice_constructor(tree slice_type_tree);
475 // Build a map descriptor.
477 map_descriptor(Map_type*);
479 // Return a tree for the type of a map descriptor. This is struct
480 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
483 map_descriptor_type();
485 // Build a type descriptor for TYPE using INITIALIZER as the type
486 // descriptor. This builds a new decl stored in *PDECL.
488 build_type_descriptor_decl(const Type*, Expression* initializer,
491 // Build required interface method tables.
493 build_interface_method_tables();
495 // Build an interface method table for a type: a list of function
496 // pointers, one for each interface method. This returns a decl.
498 interface_method_table_for_type(const Interface_type*, Named_type*,
501 // Return a tree which allocate SIZE bytes to hold values of type
504 allocate_memory(Type *type, tree size, source_location);
506 // Return a type to use for pointer to const char.
508 const_char_pointer_type_tree();
510 // Build a string constant with the right type.
512 string_constant_tree(const std::string&);
514 // Build a Go string constant. This returns a pointer to the
517 go_string_constant_tree(const std::string&);
519 // Send a value on a channel.
521 send_on_channel(tree channel, tree val, bool blocking, bool for_select,
524 // Receive a value from a channel.
526 receive_from_channel(tree type_tree, tree channel, bool for_select,
529 // Return a tree for receiving an integer on a channel.
531 receive_as_64bit_integer(tree type, tree channel, bool blocking,
535 // Make a trampoline which calls FNADDR passing CLOSURE.
537 make_trampoline(tree fnaddr, tree closure, source_location);
540 // During parsing, we keep a stack of functions. Each function on
541 // the stack is one that we are currently parsing. For each
542 // function, we keep track of the current stack of blocks.
546 Named_object* function;
547 // The stack of active blocks in the function.
548 std::vector<Block*> blocks;
551 // The stack of functions.
552 typedef std::vector<Open_function> Open_functions;
554 // Create trees for implicit builtin functions.
556 define_builtin_function_trees();
558 // Set up the built-in unsafe package.
560 import_unsafe(const std::string&, bool is_exported, source_location);
562 // Add a new imported package.
564 add_package(const std::string& real_name, const std::string& alias,
565 const std::string& unique_prefix, source_location location);
567 // Return the current binding contour.
572 current_bindings() const;
574 // Return the current block.
578 // Get the name of the magic initialization function.
582 // Get the decl for the magic initialization function.
584 initialization_function_decl();
586 // Write the magic initialization function.
588 write_initialization_function(tree fndecl, tree init_stmt_list);
590 // Initialize imported packages.
594 // Register variables with the garbage collector.
596 register_gc_vars(const std::vector<Named_object*>&, tree*);
598 // Build a pointer to a Go string constant. This returns a pointer
601 ptr_go_string_constant_tree(const std::string&);
603 // Return the name to use for a type descriptor decl for an unnamed
606 unnamed_type_descriptor_decl_name(const Type* type);
608 // Return the name to use for a type descriptor decl for a type
609 // named NO, defined in IN_FUNCTION.
611 type_descriptor_decl_name(const Named_object* no,
612 const Named_object* in_function);
614 // Where a type descriptor should be defined.
615 enum Type_descriptor_location
617 // Defined in this file.
618 TYPE_DESCRIPTOR_DEFINED,
619 // Defined in some other file.
620 TYPE_DESCRIPTOR_UNDEFINED,
621 // Common definition which may occur in multiple files.
622 TYPE_DESCRIPTOR_COMMON
625 // Return where the decl for TYPE should be defined.
626 Type_descriptor_location
627 type_descriptor_location(const Type* type);
629 // Return the type of a trampoline.
631 trampoline_type_tree();
633 // Type used to map import names to packages.
634 typedef std::map<std::string, Package*> Imports;
636 // Type used to map package names to packages.
637 typedef std::map<std::string, Package*> Packages;
639 // Type used to map special names in the sys package.
640 typedef std::map<std::string, std::string> Sys_names;
642 // Hash table mapping map types to map descriptor decls.
643 typedef Unordered_map_hash(const Map_type*, tree, Type_hash_identical,
644 Type_identical) Map_descriptors;
646 // Map unnamed types to type descriptor decls.
647 typedef Unordered_map_hash(const Type*, tree, Type_hash_identical,
648 Type_identical) Type_descriptor_decls;
650 // The package we are compiling.
652 // The list of currently open functions during parsing.
653 Open_functions functions_;
654 // The global binding contour. This includes the builtin functions
655 // and the package we are compiling.
657 // Mapping from import file names to packages.
659 // Whether the magic unsafe package was imported.
660 bool imported_unsafe_;
661 // Mapping from package names we have seen to packages. This does
662 // not include the package we are compiling.
664 // Mapping from map types to map descriptors.
665 Map_descriptors* map_descriptors_;
666 // Mapping from unnamed types to type descriptor decls.
667 Type_descriptor_decls* type_descriptor_decls_;
668 // The functions named "init", if there are any.
669 std::vector<Named_object*> init_functions_;
670 // Whether we need a magic initialization function.
672 // The name of the magic initialization function.
673 std::string init_fn_name_;
674 // A list of import control variables for packages that we import.
675 std::set<Import_init> imported_init_fns_;
676 // The unique prefix used for all global symbols.
677 std::string unique_prefix_;
678 // Whether an explicit unique prefix was set by -fgo-prefix.
679 bool unique_prefix_specified_;
680 // A list of interface types defined while parsing.
681 std::vector<Interface_type*> interface_types_;
682 // Whether named types have been converted.
683 bool named_types_are_converted_;
686 // A block of statements.
691 Block(Block* enclosing, source_location);
693 // Return the enclosing block.
696 { return this->enclosing_; }
698 // Return the bindings of the block.
701 { return this->bindings_; }
705 { return this->bindings_; }
707 // Look at the block's statements.
708 const std::vector<Statement*>*
710 { return &this->statements_; }
712 // Return the start location. This is normally the location of the
713 // left curly brace which starts the block.
715 start_location() const
716 { return this->start_location_; }
718 // Return the end location. This is normally the location of the
719 // right curly brace which ends the block.
722 { return this->end_location_; }
724 // Add a statement to the block.
726 add_statement(Statement*);
728 // Add a statement to the front of the block.
730 add_statement_at_front(Statement*);
732 // Replace a statement in a block.
734 replace_statement(size_t index, Statement*);
736 // Add a Statement before statement number INDEX.
738 insert_statement_before(size_t index, Statement*);
740 // Add a Statement after statement number INDEX.
742 insert_statement_after(size_t index, Statement*);
744 // Set the end location of the block.
746 set_end_location(source_location location)
747 { this->end_location_ = location; }
749 // Traverse the tree.
753 // Set final types for unspecified variables and constants.
757 // Return true if execution of this block may fall through to the
760 may_fall_through() const;
762 // Return a tree of the code in this block.
764 get_tree(Translate_context*);
766 // Iterate over statements.
768 typedef std::vector<Statement*>::iterator iterator;
772 { return this->statements_.begin(); }
776 { return this->statements_.end(); }
781 // Statements in the block.
782 std::vector<Statement*> statements_;
785 // Location of start of block.
786 source_location start_location_;
787 // Location of end of block.
788 source_location end_location_;
796 Function(Function_type* type, Function*, Block*, source_location);
798 // Return the function's type.
801 { return this->type_; }
803 // Return the enclosing function if there is one.
806 { return this->enclosing_; }
808 // Set the enclosing function. This is used when building thunks
809 // for functions which call recover.
811 set_enclosing(Function* enclosing)
813 gcc_assert(this->enclosing_ == NULL);
814 this->enclosing_ = enclosing;
817 // Create the named result variables in the outer block.
819 create_named_result_variables(Gogo*);
821 // Update the named result variables when cloning a function which
824 update_named_result_variables();
826 // Add a new field to the closure variable.
828 add_closure_field(Named_object* var, source_location loc)
829 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
831 // Whether this function needs a closure.
833 needs_closure() const
834 { return !this->closure_fields_.empty(); }
836 // Return the closure variable, creating it if necessary. This is
837 // passed to the function as a static chain parameter.
841 // Set the closure variable. This is used when building thunks for
842 // functions which call recover.
844 set_closure_var(Named_object* v)
846 gcc_assert(this->closure_var_ == NULL);
847 this->closure_var_ = v;
850 // Return the variable for a reference to field INDEX in the closure
853 enclosing_var(unsigned int index)
855 gcc_assert(index < this->closure_fields_.size());
856 return closure_fields_[index].first;
859 // Set the type of the closure variable if there is one.
863 // Get the block of statements associated with the function.
866 { return this->block_; }
868 // Get the location of the start of the function.
871 { return this->location_; }
873 // Return whether this function is actually a method.
877 // Add a label definition to the function.
879 add_label_definition(const std::string& label_name, source_location);
881 // Add a label reference to a function.
883 add_label_reference(const std::string& label_name);
885 // Whether this function calls the predeclared recover function.
887 calls_recover() const
888 { return this->calls_recover_; }
890 // Record that this function calls the predeclared recover function.
891 // This is set during the lowering pass.
894 { this->calls_recover_ = true; }
896 // Whether this is a recover thunk function.
898 is_recover_thunk() const
899 { return this->is_recover_thunk_; }
901 // Record that this is a thunk built for a function which calls
904 set_is_recover_thunk()
905 { this->is_recover_thunk_ = true; }
907 // Whether this function already has a recover thunk.
909 has_recover_thunk() const
910 { return this->has_recover_thunk_; }
912 // Record that this function already has a recover thunk.
914 set_has_recover_thunk()
915 { this->has_recover_thunk_ = true; }
917 // Swap with another function. Used only for the thunk which calls
920 swap_for_recover(Function *);
922 // Traverse the tree.
926 // Determine types in the function.
930 // Return the function's decl given an identifier.
932 get_or_make_decl(Gogo*, Named_object*, tree id);
934 // Return the function's decl after it has been built.
938 gcc_assert(this->fndecl_ != NULL);
939 return this->fndecl_;
942 // Set the function decl to hold a tree of the function code.
944 build_tree(Gogo*, Named_object*);
946 // Get the value to return when not explicitly specified. May also
947 // add statements to execute first to STMT_LIST.
949 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
951 // Get a tree for the variable holding the defer stack.
953 defer_stack(source_location);
955 // Export the function.
957 export_func(Export*, const std::string& name) const;
959 // Export a function with a type.
961 export_func_with_type(Export*, const std::string& name,
962 const Function_type*);
964 // Import a function.
966 import_func(Import*, std::string* pname, Typed_identifier** receiver,
967 Typed_identifier_list** pparameters,
968 Typed_identifier_list** presults, bool* is_varargs);
971 // Type for mapping from label names to Label objects.
972 typedef Unordered_map(std::string, Label*) Labels;
975 make_receiver_parm_decl(Gogo*, Named_object*, tree);
978 copy_parm_to_heap(Gogo*, Named_object*, tree);
981 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
983 typedef std::vector<Named_object*> Named_results;
985 typedef std::vector<std::pair<Named_object*,
986 source_location> > Closure_fields;
988 // The function's type.
989 Function_type* type_;
990 // The enclosing function. This is NULL when there isn't one, which
991 // is the normal case.
992 Function* enclosing_;
993 // The named result variables, if any.
994 Named_results* named_results_;
995 // If there is a closure, this is the list of variables which appear
996 // in the closure. This is created by the parser, and then resolved
997 // to a real type when we lower parse trees.
998 Closure_fields closure_fields_;
999 // The closure variable, passed as a parameter using the static
1000 // chain parameter. Normally NULL.
1001 Named_object* closure_var_;
1002 // The outer block of statements in the function.
1004 // The source location of the start of the function.
1005 source_location location_;
1006 // Labels defined or referenced in the function.
1008 // The function decl.
1010 // A variable holding the defer stack variable. This is NULL unless
1011 // we actually need a defer stack.
1013 // True if this function calls the predeclared recover function.
1014 bool calls_recover_;
1015 // True if this a thunk built for a function which calls recover.
1016 bool is_recover_thunk_;
1017 // True if this function already has a recover thunk.
1018 bool has_recover_thunk_;
1021 // A function declaration.
1023 class Function_declaration
1026 Function_declaration(Function_type* fntype, source_location location)
1027 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1032 { return this->fntype_; }
1036 { return this->location_; }
1040 { return this->asm_name_; }
1042 // Set the assembler name.
1044 set_asm_name(const std::string& asm_name)
1045 { this->asm_name_ = asm_name; }
1047 // Return a decl for the function given an identifier.
1049 get_or_make_decl(Gogo*, Named_object*, tree id);
1051 // Export a function declaration.
1053 export_func(Export* exp, const std::string& name) const
1054 { Function::export_func_with_type(exp, name, this->fntype_); }
1057 // The type of the function.
1058 Function_type* fntype_;
1059 // The location of the declaration.
1060 source_location location_;
1061 // The assembler name: this is the name to use in references to the
1062 // function. This is normally empty.
1063 std::string asm_name_;
1064 // The function decl if needed.
1073 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1074 bool is_receiver, source_location);
1076 // Get the type of the variable.
1083 // Return whether the type is defined yet.
1086 { return this->type_ != NULL; }
1088 // Get the initial value.
1091 { return this->init_; }
1093 // Return whether there are any preinit statements.
1095 has_pre_init() const
1096 { return this->preinit_ != NULL; }
1098 // Return the preinit statements if any.
1101 { return this->preinit_; }
1103 // Return whether this is a global variable.
1106 { return this->is_global_; }
1108 // Return whether this is a function parameter.
1110 is_parameter() const
1111 { return this->is_parameter_; }
1113 // Return whether this is the receiver parameter of a method.
1116 { return this->is_receiver_; }
1118 // Change this parameter to be a receiver. This is used when
1119 // creating the thunks created for functions which call recover.
1123 gcc_assert(this->is_parameter_);
1124 this->is_receiver_ = true;
1127 // Change this parameter to not be a receiver. This is used when
1128 // creating the thunks created for functions which call recover.
1130 set_is_not_receiver()
1132 gcc_assert(this->is_parameter_);
1133 this->is_receiver_ = false;
1136 // Return whether this is the varargs parameter of a function.
1138 is_varargs_parameter() const
1139 { return this->is_varargs_parameter_; }
1141 // Whether this variable's address is taken.
1143 is_address_taken() const
1144 { return this->is_address_taken_; }
1146 // Whether this variable should live in the heap.
1149 { return this->is_address_taken_ && !this->is_global_; }
1151 // Get the source location of the variable's declaration.
1154 { return this->location_; }
1156 // Record that this is the varargs parameter of a function.
1158 set_is_varargs_parameter()
1160 gcc_assert(this->is_parameter_);
1161 this->is_varargs_parameter_ = true;
1164 // Clear the initial value; used for error handling.
1167 { this->init_ = NULL; }
1169 // Set the initial value; used for converting shortcuts.
1171 set_init(Expression* init)
1172 { this->init_ = init; }
1174 // Get the preinit block, a block of statements to be run before the
1175 // initialization expression.
1177 preinit_block(Gogo*);
1179 // Add a statement to be run before the initialization expression.
1180 // This is only used for global variables.
1182 add_preinit_statement(Gogo*, Statement*);
1184 // Lower the initialization expression after parsing is complete.
1186 lower_init_expression(Gogo*, Named_object*);
1188 // A special case: the init value is used only to determine the
1189 // type. This is used if the variable is defined using := with the
1190 // comma-ok form of a map index or a receive expression. The init
1191 // value is actually the map index expression or receive expression.
1192 // We use this because we may not know the right type at parse time.
1194 set_type_from_init_tuple()
1195 { this->type_from_init_tuple_ = true; }
1197 // Another special case: the init value is used only to determine
1198 // the type. This is used if the variable is defined using := with
1199 // a range clause. The init value is the range expression. The
1200 // type of the variable is the index type of the range expression
1201 // (i.e., the first value returned by a range).
1203 set_type_from_range_index()
1204 { this->type_from_range_index_ = true; }
1206 // Another special case: like set_type_from_range_index, but the
1207 // type is the value type of the range expression (i.e., the second
1208 // value returned by a range).
1210 set_type_from_range_value()
1211 { this->type_from_range_value_ = true; }
1213 // Another special case: the init value is used only to determine
1214 // the type. This is used if the variable is defined using := with
1215 // a case in a select statement. The init value is the channel.
1216 // The type of the variable is the channel's element type.
1218 set_type_from_chan_element()
1219 { this->type_from_chan_element_ = true; }
1221 // After we lower the select statement, we once again set the type
1222 // from the initialization expression.
1224 clear_type_from_chan_element()
1226 gcc_assert(this->type_from_chan_element_);
1227 this->type_from_chan_element_ = false;
1230 // Note that this variable was created for a type switch clause.
1232 set_is_type_switch_var()
1233 { this->is_type_switch_var_ = true; }
1235 // Traverse the initializer expression.
1237 traverse_expression(Traverse*);
1239 // Determine the type of the variable if necessary.
1243 // Note that something takes the address of this variable.
1246 { this->is_address_taken_ = true; }
1248 // Get the initial value of the variable as a tree. This may only
1249 // be called if has_pre_init() returns false.
1251 get_init_tree(Gogo*, Named_object* function);
1253 // Return a series of statements which sets the value of the
1254 // variable in DECL. This should only be called is has_pre_init()
1255 // returns true. DECL may be NULL for a sink variable.
1257 get_init_block(Gogo*, Named_object* function, tree decl);
1259 // Export the variable.
1261 export_var(Export*, const std::string& name) const;
1263 // Import a variable.
1265 import_var(Import*, std::string* pname, Type** ptype);
1268 // The type of a tuple.
1270 type_from_tuple(Expression*, bool) const;
1272 // The type of a range.
1274 type_from_range(Expression*, bool, bool) const;
1276 // The element type of a channel.
1278 type_from_chan_element(Expression*, bool) const;
1280 // The variable's type. This may be NULL if the type is set from
1283 // The initial value. This may be NULL if the variable should be
1284 // initialized to the default value for the type.
1286 // Statements to run before the init statement.
1288 // Location of variable definition.
1289 source_location location_;
1290 // Whether this is a global variable.
1291 bool is_global_ : 1;
1292 // Whether this is a function parameter.
1293 bool is_parameter_ : 1;
1294 // Whether this is the receiver parameter of a method.
1295 bool is_receiver_ : 1;
1296 // Whether this is the varargs parameter of a function.
1297 bool is_varargs_parameter_ : 1;
1298 // Whether something takes the address of this variable.
1299 bool is_address_taken_ : 1;
1300 // True if we have seen this variable in a traversal.
1302 // True if we have lowered the initialization expression.
1303 bool init_is_lowered_ : 1;
1304 // True if init is a tuple used to set the type.
1305 bool type_from_init_tuple_ : 1;
1306 // True if init is a range clause and the type is the index type.
1307 bool type_from_range_index_ : 1;
1308 // True if init is a range clause and the type is the value type.
1309 bool type_from_range_value_ : 1;
1310 // True if init is a channel and the type is the channel's element type.
1311 bool type_from_chan_element_ : 1;
1312 // True if this is a variable created for a type switch case.
1313 bool is_type_switch_var_ : 1;
1314 // True if we have determined types.
1315 bool determined_type_ : 1;
1318 // A variable which is really the name for a function return value, or
1321 class Result_variable
1324 Result_variable(Type* type, Function* function, int index)
1325 : type_(type), function_(function), index_(index),
1326 is_address_taken_(false)
1329 // Get the type of the result variable.
1332 { return this->type_; }
1334 // Get the function that this is associated with.
1337 { return this->function_; }
1339 // Index in the list of function results.
1342 { return this->index_; }
1344 // Whether this variable's address is taken.
1346 is_address_taken() const
1347 { return this->is_address_taken_; }
1349 // Note that something takes the address of this variable.
1352 { this->is_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; }
1366 // Type of result variable.
1368 // Function with which this is associated.
1369 Function* function_;
1370 // Index in list of results.
1372 // Whether something takes the address of this variable.
1373 bool is_address_taken_;
1376 // The value we keep for a named constant. This lets us hold a type
1377 // and an expression.
1379 class Named_constant
1382 Named_constant(Type* type, Expression* expr, int iota_value,
1383 source_location location)
1384 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1390 { return this->type_; }
1394 { return this->expr_; }
1398 { return this->iota_value_; }
1402 { return this->location_; }
1404 // Whether we are lowering.
1407 { return this->lowering_; }
1409 // Set that we are lowering.
1412 { this->lowering_ = true; }
1414 // We are no longer lowering.
1417 { this->lowering_ = false; }
1419 // Traverse the expression.
1421 traverse_expression(Traverse*);
1423 // Determine the type of the constant if necessary.
1427 // Indicate that we found and reported an error for this constant.
1431 // Export the constant.
1433 export_const(Export*, const std::string& name) const;
1435 // Import a constant.
1437 import_const(Import*, std::string*, Type**, Expression**);
1440 // The type of the constant.
1442 // The expression for the constant.
1444 // If the predeclared constant iota is used in EXPR_, this is the
1445 // value it will have. We do this because at parse time we don't
1446 // know whether the name "iota" will refer to the predeclared
1447 // constant or to something else. We put in the right value in when
1450 // The location of the definition.
1451 source_location location_;
1452 // Whether we are currently lowering this constant.
1456 // A type declaration.
1458 class Type_declaration
1461 Type_declaration(source_location location)
1462 : location_(location), in_function_(NULL), methods_(),
1463 issued_warning_(false)
1466 // Return the location.
1469 { return this->location_; }
1471 // Return the function in which this type is declared. This will
1472 // return NULL for a type declared in global scope.
1475 { return this->in_function_; }
1477 // Set the function in which this type is declared.
1479 set_in_function(Named_object* f)
1480 { this->in_function_ = f; }
1482 // Add a method to this type. This is used when methods are defined
1485 add_method(const std::string& name, Function* function);
1487 // Add a method declaration to this type.
1489 add_method_declaration(const std::string& name, Function_type* type,
1490 source_location location);
1492 // Return whether any methods were defined.
1494 has_methods() const;
1496 // Define methods when the real type is known.
1498 define_methods(Named_type*);
1500 // This is called if we are trying to use this type. It returns
1501 // true if we should issue a warning.
1506 typedef std::vector<Named_object*> Methods;
1508 // The location of the type declaration.
1509 source_location location_;
1510 // If this type is declared in a function, a pointer back to the
1511 // function in which it is defined.
1512 Named_object* in_function_;
1513 // Methods defined before the type is defined.
1515 // True if we have issued a warning about a use of this type
1516 // declaration when it is undefined.
1517 bool issued_warning_;
1520 // An unknown object. These are created by the parser for forward
1521 // references to names which have not been seen before. In a correct
1522 // program, these will always point to a real definition by the end of
1523 // the parse. Because they point to another Named_object, these may
1524 // only be referenced by Unknown_expression objects.
1529 Unknown_name(source_location location)
1530 : location_(location), real_named_object_(NULL)
1533 // Return the location where this name was first seen.
1536 { return this->location_; }
1538 // Return the real named object that this points to, or NULL if it
1539 // was never resolved.
1541 real_named_object() const
1542 { return this->real_named_object_; }
1544 // Set the real named object that this points to.
1546 set_real_named_object(Named_object* no);
1549 // The location where this name was first seen.
1550 source_location location_;
1551 // The real named object when it is known.
1556 // A named object named. This is the result of a declaration. We
1557 // don't use a superclass because they all have to be handled
1565 // An uninitialized Named_object. We should never see this.
1566 NAMED_OBJECT_UNINITIALIZED,
1567 // An unknown name. This is used for forward references. In a
1568 // correct program, these will all be resolved by the end of the
1570 NAMED_OBJECT_UNKNOWN,
1575 // A forward type declaration.
1576 NAMED_OBJECT_TYPE_DECLARATION,
1579 // A result variable in a function.
1580 NAMED_OBJECT_RESULT_VAR,
1581 // The blank identifier--the special variable named _.
1585 // A forward func declaration.
1586 NAMED_OBJECT_FUNC_DECLARATION,
1588 NAMED_OBJECT_PACKAGE
1591 // Return the classification.
1593 classification() const
1594 { return this->classification_; }
1600 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1604 { return this->classification_ == NAMED_OBJECT_CONST; }
1608 { return this->classification_ == NAMED_OBJECT_TYPE; }
1611 is_type_declaration() const
1612 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1616 { return this->classification_ == NAMED_OBJECT_VAR; }
1619 is_result_variable() const
1620 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1624 { return this->classification_ == NAMED_OBJECT_SINK; }
1628 { return this->classification_ == NAMED_OBJECT_FUNC; }
1631 is_function_declaration() const
1632 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1636 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1640 static Named_object*
1641 make_unknown_name(const std::string& name, source_location);
1643 static Named_object*
1644 make_constant(const Typed_identifier&, const Package*, Expression*,
1647 static Named_object*
1648 make_type(const std::string&, const Package*, Type*, source_location);
1650 static Named_object*
1651 make_type_declaration(const std::string&, const Package*, source_location);
1653 static Named_object*
1654 make_variable(const std::string&, const Package*, Variable*);
1656 static Named_object*
1657 make_result_variable(const std::string&, Result_variable*);
1659 static Named_object*
1662 static Named_object*
1663 make_function(const std::string&, const Package*, Function*);
1665 static Named_object*
1666 make_function_declaration(const std::string&, const Package*, Function_type*,
1669 static Named_object*
1670 make_package(const std::string& alias, Package* package);
1677 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1678 return this->u_.unknown_value;
1682 unknown_value() const
1684 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1685 return this->u_.unknown_value;
1691 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1692 return this->u_.const_value;
1695 const Named_constant*
1698 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1699 return this->u_.const_value;
1705 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1706 return this->u_.type_value;
1712 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1713 return this->u_.type_value;
1717 type_declaration_value()
1719 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1720 return this->u_.type_declaration;
1723 const Type_declaration*
1724 type_declaration_value() const
1726 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1727 return this->u_.type_declaration;
1733 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1734 return this->u_.var_value;
1740 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1741 return this->u_.var_value;
1747 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1748 return this->u_.result_var_value;
1751 const Result_variable*
1752 result_var_value() const
1754 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1755 return this->u_.result_var_value;
1761 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1762 return this->u_.func_value;
1768 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1769 return this->u_.func_value;
1772 Function_declaration*
1773 func_declaration_value()
1775 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1776 return this->u_.func_declaration_value;
1779 const Function_declaration*
1780 func_declaration_value() const
1782 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1783 return this->u_.func_declaration_value;
1789 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1790 return this->u_.package_value;
1794 package_value() const
1796 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1797 return this->u_.package_value;
1802 { return this->name_; }
1804 // Return the name to use in an error message. The difference is
1805 // that if this Named_object is defined in a different package, this
1806 // will return PACKAGE.NAME.
1808 message_name() const;
1812 { return this->package_; }
1814 // Resolve an unknown value if possible. This returns the same
1815 // Named_object or a new one.
1819 Named_object* ret = this;
1820 if (this->is_unknown())
1822 Named_object* r = this->unknown_value()->real_named_object();
1832 const Named_object* ret = this;
1833 if (this->is_unknown())
1835 const Named_object* r = this->unknown_value()->real_named_object();
1842 // The location where this object was defined or referenced.
1846 // Return a tree for the external identifier for this object.
1850 // Return a tree representing this object.
1852 get_tree(Gogo*, Named_object* function);
1854 // Define a type declaration.
1856 set_type_value(Named_type*);
1858 // Define a function declaration.
1860 set_function_value(Function*);
1862 // Declare an unknown name as a type declaration.
1866 // Export this object.
1868 export_named_object(Export*) const;
1871 Named_object(const std::string&, const Package*, Classification);
1873 // The name of the object.
1875 // The package that this object is in. This is NULL if it is in the
1876 // file we are compiling.
1877 const Package* package_;
1878 // The type of object this is.
1879 Classification classification_;
1883 Unknown_name* unknown_value;
1884 Named_constant* const_value;
1885 Named_type* type_value;
1886 Type_declaration* type_declaration;
1887 Variable* var_value;
1888 Result_variable* result_var_value;
1889 Function* func_value;
1890 Function_declaration* func_declaration_value;
1891 Package* package_value;
1893 // The DECL tree for this object if we have already converted it.
1897 // A binding contour. This binds names to objects.
1902 // Type for mapping from names to objects.
1903 typedef Unordered_map(std::string, Named_object*) Contour;
1905 Bindings(Bindings* enclosing);
1907 // Add an unknown name.
1909 add_unknown_name(const std::string& name, source_location location)
1911 return this->add_named_object(Named_object::make_unknown_name(name,
1917 add_constant(const Typed_identifier& tid, const Package* package,
1918 Expression* expr, int iota_value)
1920 return this->add_named_object(Named_object::make_constant(tid, package,
1927 add_type(const std::string& name, const Package* package, Type* type,
1928 source_location location)
1930 return this->add_named_object(Named_object::make_type(name, package, type,
1934 // Add a named type. This is used for builtin types, and to add an
1935 // imported type to the global scope.
1937 add_named_type(Named_type* named_type);
1939 // Add a type declaration.
1941 add_type_declaration(const std::string& name, const Package* package,
1942 source_location location)
1944 Named_object* no = Named_object::make_type_declaration(name, package,
1946 return this->add_named_object(no);
1951 add_variable(const std::string& name, const Package* package,
1954 return this->add_named_object(Named_object::make_variable(name, package,
1958 // Add a result variable.
1960 add_result_variable(const std::string& name, Result_variable* result)
1962 return this->add_named_object(Named_object::make_result_variable(name,
1968 add_function(const std::string& name, const Package*, Function* function);
1970 // Add a function declaration.
1972 add_function_declaration(const std::string& name, const Package* package,
1973 Function_type* type, source_location location);
1975 // Add a package. The location is the location of the import
1978 add_package(const std::string& alias, Package* package)
1980 Named_object* no = Named_object::make_package(alias, package);
1981 return this->add_named_object(no);
1984 // Define a type which was already declared.
1986 define_type(Named_object*, Named_type*);
1988 // Add a method to the list of objects. This is not added to the
1991 add_method(Named_object*);
1993 // Add a named object to this binding.
1995 add_named_object(Named_object* no)
1996 { return this->add_named_object_to_contour(&this->bindings_, no); }
1998 // Clear all names in file scope from the bindings.
2002 // Look up a name in this binding contour and in any enclosing
2003 // binding contours. This returns NULL if the name is not found.
2005 lookup(const std::string&) const;
2007 // Look up a name in this binding contour without looking in any
2008 // enclosing binding contours. Returns NULL if the name is not found.
2010 lookup_local(const std::string&) const;
2014 remove_binding(Named_object*);
2016 // Traverse the tree. See the Traverse class.
2018 traverse(Traverse*, bool is_global);
2020 // Iterate over definitions. This does not include things which
2021 // were only declared.
2023 typedef std::vector<Named_object*>::const_iterator
2024 const_definitions_iterator;
2026 const_definitions_iterator
2027 begin_definitions() const
2028 { return this->named_objects_.begin(); }
2030 const_definitions_iterator
2031 end_definitions() const
2032 { return this->named_objects_.end(); }
2034 // Return the number of definitions.
2036 size_definitions() const
2037 { return this->named_objects_.size(); }
2039 // Return whether there are no definitions.
2041 empty_definitions() const
2042 { return this->named_objects_.empty(); }
2044 // Iterate over declarations. This is everything that has been
2045 // declared, which includes everything which has been defined.
2047 typedef Contour::const_iterator const_declarations_iterator;
2049 const_declarations_iterator
2050 begin_declarations() const
2051 { return this->bindings_.begin(); }
2053 const_declarations_iterator
2054 end_declarations() const
2055 { return this->bindings_.end(); }
2057 // Return the number of declarations.
2059 size_declarations() const
2060 { return this->bindings_.size(); }
2062 // Return whether there are no declarations.
2064 empty_declarations() const
2065 { return this->bindings_.empty(); }
2067 // Return the first declaration.
2070 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2074 add_named_object_to_contour(Contour*, Named_object*);
2077 new_definition(Named_object*, Named_object*);
2079 // Enclosing bindings.
2080 Bindings* enclosing_;
2081 // The list of objects.
2082 std::vector<Named_object*> named_objects_;
2083 // The mapping from names to objects.
2092 Label(const std::string& name)
2093 : name_(name), location_(0), decl_(NULL)
2096 // Return the label's name.
2099 { return this->name_; }
2101 // Return whether the label has been defined.
2104 { return this->location_ != 0; }
2106 // Return the location of the definition.
2109 { return this->location_; }
2111 // Define the label at LOCATION.
2113 define(source_location location)
2115 gcc_assert(this->location_ == 0);
2116 this->location_ = location;
2119 // Return the LABEL_DECL for this decl.
2123 // Return an expression for the address of this label.
2125 get_addr(source_location location);
2128 // The name of the label.
2130 // The location of the definition. This is 0 if the label has not
2131 // yet been defined.
2132 source_location location_;
2137 // An unnamed label. These are used when lowering loops.
2142 Unnamed_label(source_location location)
2143 : location_(location), decl_(NULL)
2146 // Get the location where the label is defined.
2149 { return this->location_; }
2151 // Set the location where the label is defined.
2153 set_location(source_location location)
2154 { this->location_ = location; }
2156 // Return a statement which defines this label.
2160 // Return a goto to this label from LOCATION.
2162 get_goto(source_location location);
2165 // Return the LABEL_DECL to use with GOTO_EXPR.
2169 // The location where the label is defined.
2170 source_location location_;
2175 // An imported package.
2180 Package(const std::string& name, const std::string& unique_prefix,
2181 source_location location);
2183 // The real name of this package. This may be different from the
2184 // name in the associated Named_object if the import statement used
2188 { return this->name_; }
2190 // Return the location of the import statement.
2193 { return this->location_; }
2195 // Get the unique prefix used for all symbols exported from this
2198 unique_prefix() const
2200 gcc_assert(!this->unique_prefix_.empty());
2201 return this->unique_prefix_;
2204 // The priority of this package. The init function of packages with
2205 // lower priority must be run before the init function of packages
2206 // with higher priority.
2209 { return this->priority_; }
2211 // Set the priority.
2213 set_priority(int priority);
2215 // Return the bindings.
2218 { return this->bindings_; }
2220 // Whether some symbol from the package was used.
2223 { return this->used_; }
2225 // Note that some symbol from this package was used.
2228 { this->used_ = true; }
2230 // Clear the used field for the next file.
2233 { this->used_ = false; }
2235 // Whether this package was imported in the current file.
2238 { return this->is_imported_; }
2240 // Note that this package was imported in the current file.
2243 { this->is_imported_ = true; }
2245 // Clear the imported field for the next file.
2248 { this->is_imported_ = false; }
2250 // Whether this package was imported with a name of "_".
2252 uses_sink_alias() const
2253 { return this->uses_sink_alias_; }
2255 // Note that this package was imported with a name of "_".
2257 set_uses_sink_alias()
2258 { this->uses_sink_alias_ = true; }
2260 // Clear the sink alias field for the next file.
2262 clear_uses_sink_alias()
2263 { this->uses_sink_alias_ = false; }
2265 // Look up a name in the package. Returns NULL if the name is not
2268 lookup(const std::string& name) const
2269 { return this->bindings_->lookup(name); }
2271 // Set the location of the package. This is used if it is seen in a
2272 // different import before it is really imported.
2274 set_location(source_location location)
2275 { this->location_ = location; }
2277 // Add a constant to the package.
2279 add_constant(const Typed_identifier& tid, Expression* expr)
2280 { return this->bindings_->add_constant(tid, this, expr, 0); }
2282 // Add a type to the package.
2284 add_type(const std::string& name, Type* type, source_location location)
2285 { return this->bindings_->add_type(name, this, type, location); }
2287 // Add a type declaration to the package.
2289 add_type_declaration(const std::string& name, source_location location)
2290 { return this->bindings_->add_type_declaration(name, this, location); }
2292 // Add a variable to the package.
2294 add_variable(const std::string& name, Variable* variable)
2295 { return this->bindings_->add_variable(name, this, variable); }
2297 // Add a function declaration to the package.
2299 add_function_declaration(const std::string& name, Function_type* type,
2300 source_location loc)
2301 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2303 // Determine types of constants.
2308 // The real name of this package.
2310 // The unique prefix for all exported global symbols.
2311 std::string unique_prefix_;
2312 // The names in this package.
2313 Bindings* bindings_;
2314 // The priority of this package. A package has a priority higher
2315 // than the priority of all of the packages that it imports. This
2316 // is used to run init functions in the right order.
2318 // The location of the import statement.
2319 source_location location_;
2320 // True if some name from this package was used. This is mutable
2321 // because we can use a package even if we have a const pointer to
2324 // True if this package was imported in the current file.
2326 // True if this package was imported with a name of "_".
2327 bool uses_sink_alias_;
2330 // Return codes for the traversal functions. This is not an enum
2331 // because we want to be able to declare traversal functions in other
2332 // header files without including this one.
2334 // Continue traversal as usual.
2335 const int TRAVERSE_CONTINUE = -1;
2338 const int TRAVERSE_EXIT = 0;
2340 // Continue traversal, but skip components of the current object.
2341 // E.g., if this is returned by Traverse::statement, we do not
2342 // traverse the expressions in the statement even if
2343 // traverse_expressions is set in the traverse_mask.
2344 const int TRAVERSE_SKIP_COMPONENTS = 1;
2346 // This class is used when traversing the parse tree. The caller uses
2347 // a subclass which overrides functions as desired.
2352 // These bitmasks say what to traverse.
2353 static const unsigned int traverse_variables = 0x1;
2354 static const unsigned int traverse_constants = 0x2;
2355 static const unsigned int traverse_functions = 0x4;
2356 static const unsigned int traverse_blocks = 0x8;
2357 static const unsigned int traverse_statements = 0x10;
2358 static const unsigned int traverse_expressions = 0x20;
2359 static const unsigned int traverse_types = 0x40;
2361 Traverse(unsigned int traverse_mask)
2362 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2365 virtual ~Traverse();
2367 // The bitmask of what to traverse.
2369 traverse_mask() const
2370 { return this->traverse_mask_; }
2372 // Record that we are going to traverse a type. This returns true
2373 // if the type has already been seen in this traversal. This is
2374 // required because types, unlike expressions, can form a circular
2377 remember_type(const Type*);
2379 // Record that we are going to see an expression. This returns true
2380 // if the expression has already been seen in this traversal. This
2381 // is only needed for cases where multiple expressions can point to
2384 remember_expression(const Expression*);
2386 // These functions return one of the TRAVERSE codes defined above.
2388 // If traverse_variables is set in the mask, this is called for
2389 // every variable in the tree.
2391 variable(Named_object*);
2393 // If traverse_constants is set in the mask, this is called for
2394 // every named constant in the tree. The bool parameter is true for
2395 // a global constant.
2397 constant(Named_object*, bool);
2399 // If traverse_functions is set in the mask, this is called for
2400 // every function in the tree.
2402 function(Named_object*);
2404 // If traverse_blocks is set in the mask, this is called for every
2405 // block in the tree.
2409 // If traverse_statements is set in the mask, this is called for
2410 // every statement in the tree.
2412 statement(Block*, size_t* index, Statement*);
2414 // If traverse_expressions is set in the mask, this is called for
2415 // every expression in the tree.
2417 expression(Expression**);
2419 // If traverse_types is set in the mask, this is called for every
2420 // type in the tree.
2425 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2426 Type_identical) Types_seen;
2428 typedef Unordered_set(const Expression*) Expressions_seen;
2430 // Bitmask of what sort of objects to traverse.
2431 unsigned int traverse_mask_;
2432 // Types which have been seen in this traversal.
2433 Types_seen* types_seen_;
2434 // Expressions which have been seen in this traversal.
2435 Expressions_seen* expressions_seen_;
2438 // When translating the gogo IR into trees, this is the context we
2439 // pass down the blocks and statements.
2441 class Translate_context
2444 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2446 : gogo_(gogo), function_(function), block_(block), block_tree_(block_tree),
2454 { return this->gogo_; }
2458 { return this->function_; }
2462 { return this->block_; }
2466 { return this->block_tree_; }
2470 { return this->is_const_; }
2472 // Make a constant context.
2475 { this->is_const_ = true; }
2478 // The IR for the entire compilation unit.
2480 // The function we are currently translating.
2481 Named_object* function_;
2482 // The block we are currently translating.
2484 // The BLOCK node for the current block.
2486 // Whether this is being evaluated in a constant context. This is
2487 // used for type descriptor initializers.
2491 // Runtime error codes. These must match the values in
2492 // libgo/runtime/go-runtime-error.c.
2494 // Slice index out of bounds: negative or larger than the length of
2496 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2498 // Array index out of bounds.
2499 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2501 // String index out of bounds.
2502 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2504 // Slice slice out of bounds: negative or larger than the length of
2505 // the slice or high bound less than low bound.
2506 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2508 // Array slice out of bounds.
2509 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2511 // String slice out of bounds.
2512 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2514 // Dereference of nil pointer. This is used when there is a
2515 // dereference of a pointer to a very large struct or array, to ensure
2516 // that a gigantic array is not used a proxy to access random memory
2518 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2520 // Slice length or capacity out of bounds in make: negative or
2521 // overflow or length greater than capacity.
2522 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2524 // Map capacity out of bounds in make: negative or overflow.
2525 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2527 // Channel capacity out of bounds in make: negative or overflow.
2528 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2530 // This is used by some of the langhooks.
2531 extern Gogo* go_get_gogo();
2533 // Whether we have seen any errors. FIXME: Replace with a backend
2535 extern bool saw_errors();
2537 #endif // !defined(GO_GOGO_H)