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", "float",
104 // and "uintptr" in bits.
105 Gogo(int int_type_size, int float_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 // If necessary, adjust the name to use for a hidden symbol. We add
116 // a prefix of the package name, so that hidden symbols in different
117 // packages do not collide.
119 pack_hidden_name(const std::string& name, bool is_exported) const
123 : ('.' + this->unique_prefix()
124 + '.' + this->package_name()
128 // Unpack a name which may have been hidden. Returns the
129 // user-visible name of the object.
131 unpack_hidden_name(const std::string& name)
132 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
134 // Return whether a possibly packed name is hidden.
136 is_hidden_name(const std::string& name)
137 { return name[0] == '.'; }
139 // Return the package prefix of a hidden name.
141 hidden_name_prefix(const std::string& name)
143 gcc_assert(Gogo::is_hidden_name(name));
144 return name.substr(1, name.rfind('.') - 1);
147 // Given a name which may or may not have been hidden, return the
148 // name to use in an error message.
150 message_name(const std::string& name);
152 // Return whether a name is the blank identifier _.
154 is_sink_name(const std::string& name)
156 return (name[0] == '.'
157 && name[name.length() - 1] == '_'
158 && name[name.length() - 2] == '.');
161 // Return the unique prefix to use for all exported symbols.
163 unique_prefix() const;
165 // Set the unique prefix.
167 set_unique_prefix(const std::string&);
169 // Return the priority to use for the package we are compiling.
170 // This is two more than the largest priority of any package we
173 package_priority() const;
175 // Import a package. FILENAME is the file name argument, LOCAL_NAME
176 // is the local name to give to the package. If LOCAL_NAME is empty
177 // the declarations are added to the global scope.
179 import_package(const std::string& filename, const std::string& local_name,
180 bool is_local_name_exported, source_location);
182 // Whether we are the global binding level.
184 in_global_scope() const;
186 // Look up a name in the current binding contours.
188 lookup(const std::string&, Named_object** pfunction) const;
190 // Look up a name in the current block.
192 lookup_in_block(const std::string&) const;
194 // Look up a name in the global namespace--the universal scope.
196 lookup_global(const char*) const;
198 // Add a new imported package. REAL_NAME is the real name of the
199 // package. ALIAS is the alias of the package; this may be the same
200 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
201 // this package should be added to the global namespace; this is
202 // true if the alias is ".". LOCATION is the location of the import
203 // statement. This returns the new package, or NULL on error.
205 add_imported_package(const std::string& real_name, const std::string& alias,
206 bool is_alias_exported,
207 const std::string& unique_prefix,
208 source_location location,
209 bool* padd_to_globals);
211 // Register a package. This package may or may not be imported.
212 // This returns the Package structure for the package, creating if
215 register_package(const std::string& name, const std::string& unique_prefix,
218 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
219 // method function should be added to the type of its receiver.
221 start_function(const std::string& name, Function_type* type,
222 bool add_method_to_type, source_location);
224 // Finish compiling a function.
226 finish_function(source_location);
228 // Return the current function.
230 current_function() const;
232 // Start a new block. This is not initially associated with a
235 start_block(source_location);
237 // Finish the current block and return it.
239 finish_block(source_location);
241 // Declare an unknown name. This is used while parsing. The name
242 // must be resolved by the end of the parse. Unknown names are
243 // always added at the package level.
245 add_unknown_name(const std::string& name, source_location);
247 // Declare a function.
249 declare_function(const std::string&, Function_type*, source_location);
253 add_label_definition(const std::string&, source_location);
255 // Add a label reference.
257 add_label_reference(const std::string&);
259 // Add a statement to the current block.
261 add_statement(Statement*);
263 // Add a block to the current block.
265 add_block(Block*, source_location);
269 add_constant(const Typed_identifier&, Expression*, int iota_value);
273 add_type(const std::string&, Type*, source_location);
275 // Add a named type. This is used for builtin types, and to add an
276 // imported type to the global scope.
278 add_named_type(Named_type*);
282 declare_type(const std::string&, source_location);
284 // Declare a type at the package level. This is used when the
285 // parser sees an unknown name where a type name is required.
287 declare_package_type(const std::string&, source_location);
289 // Define a type which was already declared.
291 define_type(Named_object*, Named_type*);
295 add_variable(const std::string&, Variable*);
297 // Add a sink--a reference to the blank identifier _.
301 // Add a named object to the current namespace. This is used for
302 // import . "package".
304 add_named_object(Named_object*);
306 // Return a name to use for a thunk function. A thunk function is
307 // one we create during the compilation, for a go statement or a
308 // defer statement or a method expression.
312 // Return whether an object is a thunk.
314 is_thunk(const Named_object*);
316 // Note that we've seen an interface type. This is used to build
317 // all required interface method tables.
319 record_interface_type(Interface_type*);
321 // Clear out all names in file scope. This is called when we start
322 // parsing a new file.
326 // Traverse the tree. See the Traverse class.
330 // Define the predeclared global names.
332 define_global_names();
334 // Verify and complete all types.
338 // Lower the parse tree.
342 // Lower an expression.
344 lower_expression(Named_object* function, Expression**);
348 lower_constant(Named_object*);
350 // Finalize the method lists and build stub methods for named types.
354 // Work out the types to use for unspecified variables and
359 // Type check the program.
363 // Check the types in a single block. This is used for complicated
366 check_types_in_block(Block*);
368 // Check for return statements.
370 check_return_statements();
376 // Add an import control function for an imported package to the
379 add_import_init_fn(const std::string& package_name,
380 const std::string& init_name, int prio);
382 // Turn short-cut operators (&&, ||) into explicit if statements.
386 // Use temporary variables to force order of evaluation.
390 // Build thunks for functions which call recover.
392 build_recover_thunks();
394 // Simplify statements which might use thunks: go and defer
397 simplify_thunk_statements();
399 // Write out the global values.
403 // Build a call to a builtin function. PDECL should point to a NULL
404 // initialized static pointer which will hold the fndecl. NAME is
405 // the name of the function. NARGS is the number of arguments.
406 // RETTYPE is the return type. It is followed by NARGS pairs of
407 // type and argument (both trees).
409 call_builtin(tree* pdecl, source_location, const char* name, int nargs,
412 // Build a call to the runtime error function.
414 runtime_error(int code, source_location);
416 // Build a builtin struct with a list of fields.
418 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
421 // Mark a function declaration as a builtin library function.
423 mark_fndecl_as_builtin_library(tree fndecl);
425 // Build the type of the struct that holds a slice for the given
428 slice_type_tree(tree element_type_tree);
430 // Given a tree for a slice type, return the tree for the element
433 slice_element_type_tree(tree slice_type_tree);
435 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
436 // the slice. VALUES points to the values. COUNT is the size,
437 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
440 slice_constructor(tree slice_type_tree, tree values, tree count,
443 // Build a constructor for an empty slice. SLICE_TYPE_TREE is the
444 // type of the slice.
446 empty_slice_constructor(tree slice_type_tree);
448 // Build a map descriptor.
450 map_descriptor(Map_type*);
452 // Return a tree for the type of a map descriptor. This is struct
453 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
456 map_descriptor_type();
458 // Build a type descriptor for TYPE using INITIALIZER as the type
459 // descriptor. This builds a new decl stored in *PDECL.
461 build_type_descriptor_decl(const Type*, Expression* initializer,
464 // Build required interface method tables.
466 build_interface_method_tables();
468 // Build an interface method table for a type: a list of function
469 // pointers, one for each interface method. This returns a decl.
471 interface_method_table_for_type(const Interface_type*, Named_type*,
474 // Return a tree which allocate SIZE bytes to hold values of type
477 allocate_memory(Type *type, tree size, source_location);
479 // Return a type to use for pointer to const char.
481 const_char_pointer_type_tree();
483 // Build a string constant with the right type.
485 string_constant_tree(const std::string&);
487 // Build a Go string constant. This returns a pointer to the
490 go_string_constant_tree(const std::string&);
492 // Send a value on a channel.
494 send_on_channel(tree channel, tree val, bool blocking, bool for_select,
497 // Receive a value from a channel.
499 receive_from_channel(tree type_tree, tree channel, bool for_select,
502 // Return a tree for receiving an integer on a channel.
504 receive_as_64bit_integer(tree type, tree channel, bool blocking,
508 // Make a trampoline which calls FNADDR passing CLOSURE.
510 make_trampoline(tree fnaddr, tree closure, source_location);
513 // During parsing, we keep a stack of functions. Each function on
514 // the stack is one that we are currently parsing. For each
515 // function, we keep track of the current stack of blocks.
519 Named_object* function;
520 // The stack of active blocks in the function.
521 std::vector<Block*> blocks;
524 // The stack of functions.
525 typedef std::vector<Open_function> Open_functions;
527 // Create trees for implicit builtin functions.
529 define_builtin_function_trees();
531 // Set up the built-in unsafe package.
533 import_unsafe(const std::string&, bool is_exported, source_location);
535 // Add a new imported package.
537 add_package(const std::string& real_name, const std::string& alias,
538 const std::string& unique_prefix, source_location location);
540 // Return the current binding contour.
545 current_bindings() const;
547 // Return the current block.
551 // Get the name of the magic initialization function.
555 // Get the decl for the magic initialization function.
557 initialization_function_decl();
559 // Write the magic initialization function.
561 write_initialization_function(tree fndecl, tree init_stmt_list);
563 // Initialize imported packages.
567 // Register variables with the garbage collector.
569 register_gc_vars(const std::vector<Named_object*>&, tree*);
571 // Build a pointer to a Go string constant. This returns a pointer
574 ptr_go_string_constant_tree(const std::string&);
576 // Return the name to use for a type descriptor decl for an unnamed
579 unnamed_type_descriptor_decl_name(const Type* type);
581 // Return the name to use for a type descriptor decl for a type
582 // named NO, defined in IN_FUNCTION.
584 type_descriptor_decl_name(const Named_object* no,
585 const Named_object* in_function);
587 // Where a type descriptor should be defined.
588 enum Type_descriptor_location
590 // Defined in this file.
591 TYPE_DESCRIPTOR_DEFINED,
592 // Defined in some other file.
593 TYPE_DESCRIPTOR_UNDEFINED,
594 // Common definition which may occur in multiple files.
595 TYPE_DESCRIPTOR_COMMON
598 // Return where the decl for TYPE should be defined.
599 Type_descriptor_location
600 type_descriptor_location(const Type* type);
602 // Return the type of a trampoline.
604 trampoline_type_tree();
606 // Type used to map import names to packages.
607 typedef std::map<std::string, Package*> Imports;
609 // Type used to map package names to packages.
610 typedef std::map<std::string, Package*> Packages;
612 // Type used to map special names in the sys package.
613 typedef std::map<std::string, std::string> Sys_names;
615 // Hash table mapping map types to map descriptor decls.
616 typedef Unordered_map_hash(const Map_type*, tree, Type_hash_identical,
617 Type_identical) Map_descriptors;
619 // Map unnamed types to type descriptor decls.
620 typedef Unordered_map_hash(const Type*, tree, Type_hash_identical,
621 Type_identical) Type_descriptor_decls;
623 // The package we are compiling.
625 // The list of currently open functions during parsing.
626 Open_functions functions_;
627 // The global binding contour. This includes the builtin functions
628 // and the package we are compiling.
630 // Mapping from import file names to packages.
632 // Whether the magic unsafe package was imported.
633 bool imported_unsafe_;
634 // Mapping from package names we have seen to packages. This does
635 // not include the package we are compiling.
637 // Mapping from map types to map descriptors.
638 Map_descriptors* map_descriptors_;
639 // Mapping from unnamed types to type descriptor decls.
640 Type_descriptor_decls* type_descriptor_decls_;
641 // The functions named "init", if there are any.
642 std::vector<Named_object*> init_functions_;
643 // Whether we need a magic initialization function.
645 // The name of the magic initialization function.
646 std::string init_fn_name_;
647 // A list of import control variables for packages that we import.
648 std::set<Import_init> imported_init_fns_;
649 // The unique prefix used for all global symbols.
650 std::string unique_prefix_;
651 // A list of interface types defined while parsing.
652 std::vector<Interface_type*> interface_types_;
655 // A block of statements.
660 Block(Block* enclosing, source_location);
662 // Return the enclosing block.
665 { return this->enclosing_; }
667 // Return the bindings of the block.
670 { return this->bindings_; }
674 { return this->bindings_; }
676 // Look at the block's statements.
677 const std::vector<Statement*>*
679 { return &this->statements_; }
681 // Return the start location. This is normally the location of the
682 // left curly brace which starts the block.
684 start_location() const
685 { return this->start_location_; }
687 // Return the end location. This is normally the location of the
688 // right curly brace which ends the block.
691 { return this->end_location_; }
693 // Add a statement to the block.
695 add_statement(Statement*);
697 // Add a statement to the front of the block.
699 add_statement_at_front(Statement*);
701 // Replace a statement in a block.
703 replace_statement(size_t index, Statement*);
705 // Add a Statement before statement number INDEX.
707 insert_statement_before(size_t index, Statement*);
709 // Add a Statement after statement number INDEX.
711 insert_statement_after(size_t index, Statement*);
713 // Set the end location of the block.
715 set_end_location(source_location location)
716 { this->end_location_ = location; }
718 // Traverse the tree.
722 // Set final types for unspecified variables and constants.
726 // Return true if execution of this block may fall through to the
729 may_fall_through() const;
731 // Return a tree of the code in this block.
733 get_tree(Translate_context*);
735 // Iterate over statements.
737 typedef std::vector<Statement*>::iterator iterator;
741 { return this->statements_.begin(); }
745 { return this->statements_.end(); }
750 // Statements in the block.
751 std::vector<Statement*> statements_;
754 // Location of start of block.
755 source_location start_location_;
756 // Location of end of block.
757 source_location end_location_;
765 Function(Function_type* type, Function*, Block*, source_location);
767 // Return the function's type.
770 { return this->type_; }
772 // Return the enclosing function if there is one.
775 { return this->enclosing_; }
777 // Set the enclosing function. This is used when building thunks
778 // for functions which call recover.
780 set_enclosing(Function* enclosing)
782 gcc_assert(this->enclosing_ == NULL);
783 this->enclosing_ = enclosing;
786 // Create the named result variables in the outer block.
788 create_named_result_variables();
790 // Add a new field to the closure variable.
792 add_closure_field(Named_object* var, source_location loc)
793 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
795 // Whether this function needs a closure.
797 needs_closure() const
798 { return !this->closure_fields_.empty(); }
800 // Return the closure variable, creating it if necessary. This is
801 // passed to the function as a static chain parameter.
805 // Set the closure variable. This is used when building thunks for
806 // functions which call recover.
808 set_closure_var(Named_object* v)
810 gcc_assert(this->closure_var_ == NULL);
811 this->closure_var_ = v;
814 // Return the variable for a reference to field INDEX in the closure
817 enclosing_var(unsigned int index)
819 gcc_assert(index < this->closure_fields_.size());
820 return closure_fields_[index].first;
823 // Set the type of the closure variable if there is one.
827 // Get the block of statements associated with the function.
830 { return this->block_; }
832 // Get the location of the start of the function.
835 { return this->location_; }
837 // Return whether this function is actually a method.
841 // Add a label definition to the function.
843 add_label_definition(const std::string& label_name, source_location);
845 // Add a label reference to a function.
847 add_label_reference(const std::string& label_name);
849 // Whether this function calls the predeclared recover function.
851 calls_recover() const
852 { return this->calls_recover_; }
854 // Record that this function calls the predeclared recover function.
855 // This is set during the lowering pass.
858 { this->calls_recover_ = true; }
860 // Whether this is a recover thunk function.
862 is_recover_thunk() const
863 { return this->is_recover_thunk_; }
865 // Record that this is a thunk built for a function which calls
868 set_is_recover_thunk()
869 { this->is_recover_thunk_ = true; }
871 // Whether this function already has a recover thunk.
873 has_recover_thunk() const
874 { return this->has_recover_thunk_; }
876 // Record that this function already has a recover thunk.
878 set_has_recover_thunk()
879 { this->has_recover_thunk_ = true; }
881 // Swap with another function. Used only for the thunk which calls
884 swap_for_recover(Function *);
886 // Traverse the tree.
890 // Determine types in the function.
894 // Return the function's decl given an identifier.
896 get_or_make_decl(Gogo*, Named_object*, tree id);
898 // Return the function's decl after it has been built.
902 gcc_assert(this->fndecl_ != NULL);
903 return this->fndecl_;
906 // Set the function decl to hold a tree of the function code.
908 build_tree(Gogo*, Named_object*);
910 // Get the value to return when not explicitly specified. May also
911 // add statements to execute first to STMT_LIST.
913 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
915 // Get a tree for the variable holding the defer stack.
917 defer_stack(source_location);
919 // Export the function.
921 export_func(Export*, const std::string& name) const;
923 // Export a function with a type.
925 export_func_with_type(Export*, const std::string& name,
926 const Function_type*);
928 // Import a function.
930 import_func(Import*, std::string* pname, Typed_identifier** receiver,
931 Typed_identifier_list** pparameters,
932 Typed_identifier_list** presults, bool* is_varargs);
935 // Type for mapping from label names to Label objects.
936 typedef Unordered_map(std::string, Label*) Labels;
939 make_receiver_parm_decl(Gogo*, Named_object*, tree);
942 copy_parm_to_heap(Gogo*, Named_object*, tree);
945 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
947 typedef std::vector<Named_object*> Named_results;
949 typedef std::vector<std::pair<Named_object*,
950 source_location> > Closure_fields;
952 // The function's type.
953 Function_type* type_;
954 // The enclosing function. This is NULL when there isn't one, which
955 // is the normal case.
956 Function* enclosing_;
957 // The named result variables, if any.
958 Named_results* named_results_;
959 // If there is a closure, this is the list of variables which appear
960 // in the closure. This is created by the parser, and then resolved
961 // to a real type when we lower parse trees.
962 Closure_fields closure_fields_;
963 // The closure variable, passed as a parameter using the static
964 // chain parameter. Normally NULL.
965 Named_object* closure_var_;
966 // The outer block of statements in the function.
968 // The source location of the start of the function.
969 source_location location_;
970 // Labels defined or referenced in the function.
972 // The function decl.
974 // A variable holding the defer stack variable. This is NULL unless
975 // we actually need a defer stack.
977 // True if this function calls the predeclared recover function.
979 // True if this a thunk built for a function which calls recover.
980 bool is_recover_thunk_;
981 // True if this function already has a recover thunk.
982 bool has_recover_thunk_;
985 // A function declaration.
987 class Function_declaration
990 Function_declaration(Function_type* fntype, source_location location)
991 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
996 { return this->fntype_; }
1000 { return this->location_; }
1004 { return this->asm_name_; }
1006 // Set the assembler name.
1008 set_asm_name(const std::string& asm_name)
1009 { this->asm_name_ = asm_name; }
1011 // Return a decl for the function given an identifier.
1013 get_or_make_decl(Gogo*, Named_object*, tree id);
1015 // Export a function declaration.
1017 export_func(Export* exp, const std::string& name) const
1018 { Function::export_func_with_type(exp, name, this->fntype_); }
1021 // The type of the function.
1022 Function_type* fntype_;
1023 // The location of the declaration.
1024 source_location location_;
1025 // The assembler name: this is the name to use in references to the
1026 // function. This is normally empty.
1027 std::string asm_name_;
1028 // The function decl if needed.
1037 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1038 bool is_receiver, source_location);
1040 // Get the type of the variable.
1044 // Return whether the type is defined yet.
1047 { return this->type_ != NULL; }
1049 // Get the initial value.
1052 { return this->init_; }
1054 // Return whether there are any preinit statements.
1056 has_pre_init() const
1057 { return this->preinit_ != NULL; }
1059 // Return the preinit statements if any.
1062 { return this->preinit_; }
1064 // Return whether this is a global variable.
1067 { return this->is_global_; }
1069 // Return whether this is a function parameter.
1071 is_parameter() const
1072 { return this->is_parameter_; }
1074 // Return whether this is the receiver parameter of a method.
1077 { return this->is_receiver_; }
1079 // Change this parameter to be a receiver. This is used when
1080 // creating the thunks created for functions which call recover.
1084 gcc_assert(this->is_parameter_);
1085 this->is_receiver_ = true;
1088 // Change this parameter to not be a receiver. This is used when
1089 // creating the thunks created for functions which call recover.
1091 set_is_not_receiver()
1093 gcc_assert(this->is_parameter_);
1094 this->is_receiver_ = false;
1097 // Return whether this is the varargs parameter of a function.
1099 is_varargs_parameter() const
1100 { return this->is_varargs_parameter_; }
1102 // Whether this variable's address is taken.
1104 is_address_taken() const
1105 { return this->is_address_taken_; }
1107 // Whether this variable should live in the heap.
1110 { return this->is_address_taken_ && !this->is_global_; }
1112 // Get the source location of the variable's declaration.
1115 { return this->location_; }
1117 // Record that this is the varargs parameter of a function.
1119 set_is_varargs_parameter()
1121 gcc_assert(this->is_parameter_);
1122 this->is_varargs_parameter_ = true;
1125 // Clear the initial value; used for error handling.
1128 { this->init_ = NULL; }
1130 // Set the initial value; used for converting shortcuts.
1132 set_init(Expression* init)
1133 { this->init_ = init; }
1135 // Get the preinit block, a block of statements to be run before the
1136 // initialization expression.
1140 // Add a statement to be run before the initialization expression.
1141 // This is only used for global variables.
1143 add_preinit_statement(Statement*);
1145 // Lower the initialization expression after parsing is complete.
1147 lower_init_expression(Gogo*, Named_object*);
1149 // A special case: the init value is used only to determine the
1150 // type. This is used if the variable is defined using := with the
1151 // comma-ok form of a map index or a receive expression. The init
1152 // value is actually the map index expression or receive expression.
1153 // We use this because we may not know the right type at parse time.
1155 set_type_from_init_tuple()
1156 { this->type_from_init_tuple_ = true; }
1158 // Another special case: the init value is used only to determine
1159 // the type. This is used if the variable is defined using := with
1160 // a range clause. The init value is the range expression. The
1161 // type of the variable is the index type of the range expression
1162 // (i.e., the first value returned by a range).
1164 set_type_from_range_index()
1165 { this->type_from_range_index_ = true; }
1167 // Another special case: like set_type_from_range_index, but the
1168 // type is the value type of the range expression (i.e., the second
1169 // value returned by a range).
1171 set_type_from_range_value()
1172 { this->type_from_range_value_ = true; }
1174 // Another special case: the init value is used only to determine
1175 // the type. This is used if the variable is defined using := with
1176 // a case in a select statement. The init value is the channel.
1177 // The type of the variable is the channel's element type.
1179 set_type_from_chan_element()
1180 { this->type_from_chan_element_ = true; }
1182 // After we lower the select statement, we once again set the type
1183 // from the initialization expression.
1185 clear_type_from_chan_element()
1187 gcc_assert(this->type_from_chan_element_);
1188 this->type_from_chan_element_ = false;
1191 // Note that this variable was created for a type switch clause.
1193 set_is_type_switch_var()
1194 { this->is_type_switch_var_ = true; }
1196 // Traverse the initializer expression.
1198 traverse_expression(Traverse*);
1200 // Determine the type of the variable if necessary.
1204 // Note that something takes the address of this variable.
1207 { this->is_address_taken_ = true; }
1209 // Get the initial value of the variable as a tree. This may only
1210 // be called if has_pre_init() returns false.
1212 get_init_tree(Gogo*, Named_object* function);
1214 // Return a series of statements which sets the value of the
1215 // variable in DECL. This should only be called is has_pre_init()
1216 // returns true. DECL may be NULL for a sink variable.
1218 get_init_block(Gogo*, Named_object* function, tree decl);
1220 // Export the variable.
1222 export_var(Export*, const std::string& name) const;
1224 // Import a variable.
1226 import_var(Import*, std::string* pname, Type** ptype);
1229 // The type of a tuple.
1231 type_from_tuple(Expression*, bool) const;
1233 // The type of a range.
1235 type_from_range(Expression*, bool, bool) const;
1237 // The element type of a channel.
1239 type_from_chan_element(Expression*, bool) const;
1241 // The variable's type. This may be NULL if the type is set from
1244 // The initial value. This may be NULL if the variable should be
1245 // initialized to the default value for the type.
1247 // Statements to run before the init statement.
1249 // Location of variable definition.
1250 source_location location_;
1251 // Whether this is a global variable.
1252 bool is_global_ : 1;
1253 // Whether this is a function parameter.
1254 bool is_parameter_ : 1;
1255 // Whether this is the receiver parameter of a method.
1256 bool is_receiver_ : 1;
1257 // Whether this is the varargs parameter of a function.
1258 bool is_varargs_parameter_ : 1;
1259 // Whether something takes the address of this variable.
1260 bool is_address_taken_ : 1;
1261 // True if we have lowered the initialization expression.
1262 bool init_is_lowered_ : 1;
1263 // True if init is a tuple used to set the type.
1264 bool type_from_init_tuple_ : 1;
1265 // True if init is a range clause and the type is the index type.
1266 bool type_from_range_index_ : 1;
1267 // True if init is a range clause and the type is the value type.
1268 bool type_from_range_value_ : 1;
1269 // True if init is a channel and the type is the channel's element type.
1270 bool type_from_chan_element_ : 1;
1271 // True if this is a variable created for a type switch case.
1272 bool is_type_switch_var_ : 1;
1275 // A variable which is really the name for a function return value, or
1278 class Result_variable
1281 Result_variable(Type* type, Function* function, int index)
1282 : type_(type), function_(function), index_(index),
1283 is_address_taken_(false)
1286 // Get the type of the result variable.
1289 { return this->type_; }
1291 // Get the function that this is associated with.
1294 { return this->function_; }
1296 // Index in the list of function results.
1299 { return this->index_; }
1301 // Whether this variable's address is taken.
1303 is_address_taken() const
1304 { return this->is_address_taken_; }
1306 // Note that something takes the address of this variable.
1309 { this->is_address_taken_ = true; }
1311 // Whether this variable should live in the heap.
1314 { return this->is_address_taken_; }
1317 // Type of result variable.
1319 // Function with which this is associated.
1320 Function* function_;
1321 // Index in list of results.
1323 // Whether something takes the address of this variable.
1324 bool is_address_taken_;
1327 // The value we keep for a named constant. This lets us hold a type
1328 // and an expression.
1330 class Named_constant
1333 Named_constant(Type* type, Expression* expr, int iota_value,
1334 source_location location)
1335 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1341 { return this->type_; }
1345 { return this->expr_; }
1349 { return this->iota_value_; }
1353 { return this->location_; }
1355 // Whether we are lowering.
1358 { return this->lowering_; }
1360 // Set that we are lowering.
1363 { this->lowering_ = true; }
1365 // We are no longer lowering.
1368 { this->lowering_ = false; }
1370 // Traverse the expression.
1372 traverse_expression(Traverse*);
1374 // Determine the type of the constant if necessary.
1378 // Indicate that we found and reported an error for this constant.
1382 // Export the constant.
1384 export_const(Export*, const std::string& name) const;
1386 // Import a constant.
1388 import_const(Import*, std::string*, Type**, Expression**);
1391 // The type of the constant.
1393 // The expression for the constant.
1395 // If the predeclared constant iota is used in EXPR_, this is the
1396 // value it will have. We do this because at parse time we don't
1397 // know whether the name "iota" will refer to the predeclared
1398 // constant or to something else. We put in the right value in when
1401 // The location of the definition.
1402 source_location location_;
1403 // Whether we are currently lowering this constant.
1407 // A type declaration.
1409 class Type_declaration
1412 Type_declaration(source_location location)
1413 : location_(location), in_function_(NULL), methods_(),
1414 issued_warning_(false)
1417 // Return the location.
1420 { return this->location_; }
1422 // Return the function in which this type is declared. This will
1423 // return NULL for a type declared in global scope.
1426 { return this->in_function_; }
1428 // Set the function in which this type is declared.
1430 set_in_function(Named_object* f)
1431 { this->in_function_ = f; }
1433 // Add a method to this type. This is used when methods are defined
1436 add_method(const std::string& name, Function* function);
1438 // Add a method declaration to this type.
1440 add_method_declaration(const std::string& name, Function_type* type,
1441 source_location location);
1443 // Return whether any methods were defined.
1445 has_methods() const;
1447 // Define methods when the real type is known.
1449 define_methods(Named_type*);
1451 // This is called if we are trying to use this type. It returns
1452 // true if we should issue a warning.
1457 typedef std::vector<Named_object*> Methods;
1459 // The location of the type declaration.
1460 source_location location_;
1461 // If this type is declared in a function, a pointer back to the
1462 // function in which it is defined.
1463 Named_object* in_function_;
1464 // Methods defined before the type is defined.
1466 // True if we have issued a warning about a use of this type
1467 // declaration when it is undefined.
1468 bool issued_warning_;
1471 // An unknown object. These are created by the parser for forward
1472 // references to names which have not been seen before. In a correct
1473 // program, these will always point to a real definition by the end of
1474 // the parse. Because they point to another Named_object, these may
1475 // only be referenced by Unknown_expression objects.
1480 Unknown_name(source_location location)
1481 : location_(location), real_named_object_(NULL)
1484 // Return the location where this name was first seen.
1487 { return this->location_; }
1489 // Return the real named object that this points to, or NULL if it
1490 // was never resolved.
1492 real_named_object() const
1493 { return this->real_named_object_; }
1495 // Set the real named object that this points to.
1497 set_real_named_object(Named_object* no);
1500 // The location where this name was first seen.
1501 source_location location_;
1502 // The real named object when it is known.
1507 // A named object named. This is the result of a declaration. We
1508 // don't use a superclass because they all have to be handled
1516 // An uninitialized Named_object. We should never see this.
1517 NAMED_OBJECT_UNINITIALIZED,
1518 // An unknown name. This is used for forward references. In a
1519 // correct program, these will all be resolved by the end of the
1521 NAMED_OBJECT_UNKNOWN,
1526 // A forward type declaration.
1527 NAMED_OBJECT_TYPE_DECLARATION,
1530 // A result variable in a function.
1531 NAMED_OBJECT_RESULT_VAR,
1532 // The blank identifier--the special variable named _.
1536 // A forward func declaration.
1537 NAMED_OBJECT_FUNC_DECLARATION,
1539 NAMED_OBJECT_PACKAGE
1542 // Return the classification.
1544 classification() const
1545 { return this->classification_; }
1551 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1555 { return this->classification_ == NAMED_OBJECT_CONST; }
1559 { return this->classification_ == NAMED_OBJECT_TYPE; }
1562 is_type_declaration() const
1563 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1567 { return this->classification_ == NAMED_OBJECT_VAR; }
1570 is_result_variable() const
1571 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1575 { return this->classification_ == NAMED_OBJECT_SINK; }
1579 { return this->classification_ == NAMED_OBJECT_FUNC; }
1582 is_function_declaration() const
1583 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1587 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1591 static Named_object*
1592 make_unknown_name(const std::string& name, source_location);
1594 static Named_object*
1595 make_constant(const Typed_identifier&, const Package*, Expression*,
1598 static Named_object*
1599 make_type(const std::string&, const Package*, Type*, source_location);
1601 static Named_object*
1602 make_type_declaration(const std::string&, const Package*, source_location);
1604 static Named_object*
1605 make_variable(const std::string&, const Package*, Variable*);
1607 static Named_object*
1608 make_result_variable(const std::string&, Result_variable*);
1610 static Named_object*
1613 static Named_object*
1614 make_function(const std::string&, const Package*, Function*);
1616 static Named_object*
1617 make_function_declaration(const std::string&, const Package*, Function_type*,
1620 static Named_object*
1621 make_package(const std::string& alias, Package* package);
1628 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1629 return this->u_.unknown_value;
1633 unknown_value() const
1635 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1636 return this->u_.unknown_value;
1642 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1643 return this->u_.const_value;
1646 const Named_constant*
1649 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1650 return this->u_.const_value;
1656 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1657 return this->u_.type_value;
1663 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1664 return this->u_.type_value;
1668 type_declaration_value()
1670 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1671 return this->u_.type_declaration;
1674 const Type_declaration*
1675 type_declaration_value() const
1677 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1678 return this->u_.type_declaration;
1684 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1685 return this->u_.var_value;
1691 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1692 return this->u_.var_value;
1698 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1699 return this->u_.result_var_value;
1702 const Result_variable*
1703 result_var_value() const
1705 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1706 return this->u_.result_var_value;
1712 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1713 return this->u_.func_value;
1719 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1720 return this->u_.func_value;
1723 Function_declaration*
1724 func_declaration_value()
1726 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1727 return this->u_.func_declaration_value;
1730 const Function_declaration*
1731 func_declaration_value() const
1733 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1734 return this->u_.func_declaration_value;
1740 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1741 return this->u_.package_value;
1745 package_value() const
1747 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1748 return this->u_.package_value;
1753 { return this->name_; }
1755 // Return the name to use in an error message. The difference is
1756 // that if this Named_object is defined in a different package, this
1757 // will return PACKAGE.NAME.
1759 message_name() const;
1763 { return this->package_; }
1765 // Resolve an unknown value if possible. This returns the same
1766 // Named_object or a new one.
1770 Named_object* ret = this;
1771 if (this->is_unknown())
1773 Named_object* r = this->unknown_value()->real_named_object();
1783 const Named_object* ret = this;
1784 if (this->is_unknown())
1786 const Named_object* r = this->unknown_value()->real_named_object();
1793 // The location where this object was defined or referenced.
1797 // Return a tree for the external identifier for this object.
1801 // Return a tree representing this object.
1803 get_tree(Gogo*, Named_object* function);
1805 // Define a type declaration.
1807 set_type_value(Named_type*);
1809 // Define a function declaration.
1811 set_function_value(Function*);
1813 // Export this object.
1815 export_named_object(Export*) const;
1818 Named_object(const std::string&, const Package*, Classification);
1820 // The name of the object.
1822 // The package that this object is in. This is NULL if it is in the
1823 // file we are compiling.
1824 const Package* package_;
1825 // The type of object this is.
1826 Classification classification_;
1830 Unknown_name* unknown_value;
1831 Named_constant* const_value;
1832 Named_type* type_value;
1833 Type_declaration* type_declaration;
1834 Variable* var_value;
1835 Result_variable* result_var_value;
1836 Function* func_value;
1837 Function_declaration* func_declaration_value;
1838 Package* package_value;
1840 // The DECL tree for this object if we have already converted it.
1844 // A binding contour. This binds names to objects.
1849 // Type for mapping from names to objects.
1850 typedef Unordered_map(std::string, Named_object*) Contour;
1852 Bindings(Bindings* enclosing);
1854 // Add an unknown name.
1856 add_unknown_name(const std::string& name, source_location location)
1858 return this->add_named_object(Named_object::make_unknown_name(name,
1864 add_constant(const Typed_identifier& tid, const Package* package,
1865 Expression* expr, int iota_value)
1867 return this->add_named_object(Named_object::make_constant(tid, package,
1874 add_type(const std::string& name, const Package* package, Type* type,
1875 source_location location)
1877 return this->add_named_object(Named_object::make_type(name, package, type,
1881 // Add a named type. This is used for builtin types, and to add an
1882 // imported type to the global scope.
1884 add_named_type(Named_type* named_type);
1886 // Add a type declaration.
1888 add_type_declaration(const std::string& name, const Package* package,
1889 source_location location)
1891 Named_object* no = Named_object::make_type_declaration(name, package,
1893 return this->add_named_object(no);
1898 add_variable(const std::string& name, const Package* package,
1901 return this->add_named_object(Named_object::make_variable(name, package,
1905 // Add a result variable.
1907 add_result_variable(const std::string& name, Result_variable* result)
1909 return this->add_named_object(Named_object::make_result_variable(name,
1915 add_function(const std::string& name, const Package*, Function* function);
1917 // Add a function declaration.
1919 add_function_declaration(const std::string& name, const Package* package,
1920 Function_type* type, source_location location);
1922 // Add a package. The location is the location of the import
1925 add_package(const std::string& alias, Package* package)
1927 Named_object* no = Named_object::make_package(alias, package);
1928 return this->add_named_object(no);
1931 // Define a type which was already declared.
1933 define_type(Named_object*, Named_type*);
1935 // Add a method to the list of objects. This is not added to the
1938 add_method(Named_object*);
1940 // Add a named object to this binding.
1942 add_named_object(Named_object* no)
1943 { return this->add_named_object_to_contour(&this->bindings_, no); }
1945 // Clear all names in file scope from the bindings.
1949 // Look up a name in this binding contour and in any enclosing
1950 // binding contours. This returns NULL if the name is not found.
1952 lookup(const std::string&) const;
1954 // Look up a name in this binding contour without looking in any
1955 // enclosing binding contours. Returns NULL if the name is not found.
1957 lookup_local(const std::string&) const;
1961 remove_binding(Named_object*);
1963 // Traverse the tree. See the Traverse class.
1965 traverse(Traverse*, bool is_global);
1967 // Iterate over definitions. This does not include things which
1968 // were only declared.
1970 typedef std::vector<Named_object*>::const_iterator
1971 const_definitions_iterator;
1973 const_definitions_iterator
1974 begin_definitions() const
1975 { return this->named_objects_.begin(); }
1977 const_definitions_iterator
1978 end_definitions() const
1979 { return this->named_objects_.end(); }
1981 // Return the number of definitions.
1983 size_definitions() const
1984 { return this->named_objects_.size(); }
1986 // Return whether there are no definitions.
1988 empty_definitions() const
1989 { return this->named_objects_.empty(); }
1991 // Iterate over declarations. This is everything that has been
1992 // declared, which includes everything which has been defined.
1994 typedef Contour::const_iterator const_declarations_iterator;
1996 const_declarations_iterator
1997 begin_declarations() const
1998 { return this->bindings_.begin(); }
2000 const_declarations_iterator
2001 end_declarations() const
2002 { return this->bindings_.end(); }
2004 // Return the number of declarations.
2006 size_declarations() const
2007 { return this->bindings_.size(); }
2009 // Return whether there are no declarations.
2011 empty_declarations() const
2012 { return this->bindings_.empty(); }
2014 // Return the first declaration.
2017 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2021 add_named_object_to_contour(Contour*, Named_object*);
2024 new_definition(Named_object*, Named_object*);
2026 // Enclosing bindings.
2027 Bindings* enclosing_;
2028 // The list of objects.
2029 std::vector<Named_object*> named_objects_;
2030 // The mapping from names to objects.
2039 Label(const std::string& name)
2040 : name_(name), location_(0), decl_(NULL)
2043 // Return the label's name.
2046 { return this->name_; }
2048 // Return whether the label has been defined.
2051 { return this->location_ != 0; }
2053 // Return the location of the definition.
2056 { return this->location_; }
2058 // Define the label at LOCATION.
2060 define(source_location location)
2062 gcc_assert(this->location_ == 0);
2063 this->location_ = location;
2066 // Return the LABEL_DECL for this decl.
2070 // Return an expression for the address of this label.
2072 get_addr(source_location location);
2075 // The name of the label.
2077 // The location of the definition. This is 0 if the label has not
2078 // yet been defined.
2079 source_location location_;
2084 // An unnamed label. These are used when lowering loops.
2089 Unnamed_label(source_location location)
2090 : location_(location), decl_(NULL)
2093 // Get the location where the label is defined.
2096 { return this->location_; }
2098 // Set the location where the label is defined.
2100 set_location(source_location location)
2101 { this->location_ = location; }
2103 // Return a statement which defines this label.
2107 // Return a goto to this label from LOCATION.
2109 get_goto(source_location location);
2112 // Return the LABEL_DECL to use with GOTO_EXPR.
2116 // The location where the label is defined.
2117 source_location location_;
2122 // An imported package.
2127 Package(const std::string& name, const std::string& unique_prefix,
2128 source_location location);
2130 // The real name of this package. This may be different from the
2131 // name in the associated Named_object if the import statement used
2135 { return this->name_; }
2137 // Return the location of the import statement.
2140 { return this->location_; }
2142 // Get the unique prefix used for all symbols exported from this
2145 unique_prefix() const
2147 gcc_assert(!this->unique_prefix_.empty());
2148 return this->unique_prefix_;
2151 // The priority of this package. The init function of packages with
2152 // lower priority must be run before the init function of packages
2153 // with higher priority.
2156 { return this->priority_; }
2158 // Set the priority.
2160 set_priority(int priority);
2162 // Return the bindings.
2165 { return this->bindings_; }
2167 // Whether some symbol from the package was used.
2170 { return this->used_; }
2172 // Note that some symbol from this package was used.
2175 { this->used_ = true; }
2177 // Clear the used field for the next file.
2180 { this->used_ = false; }
2182 // Whether this package was imported in the current file.
2185 { return this->is_imported_; }
2187 // Note that this package was imported in the current file.
2190 { this->is_imported_ = true; }
2192 // Clear the imported field for the next file.
2195 { this->is_imported_ = false; }
2197 // Whether this package was imported with a name of "_".
2199 uses_sink_alias() const
2200 { return this->uses_sink_alias_; }
2202 // Note that this package was imported with a name of "_".
2204 set_uses_sink_alias()
2205 { this->uses_sink_alias_ = true; }
2207 // Clear the sink alias field for the next file.
2209 clear_uses_sink_alias()
2210 { this->uses_sink_alias_ = false; }
2212 // Look up a name in the package. Returns NULL if the name is not
2215 lookup(const std::string& name) const
2216 { return this->bindings_->lookup(name); }
2218 // Set the location of the package. This is used if it is seen in a
2219 // different import before it is really imported.
2221 set_location(source_location location)
2222 { this->location_ = location; }
2224 // Add a constant to the package.
2226 add_constant(const Typed_identifier& tid, Expression* expr)
2227 { return this->bindings_->add_constant(tid, this, expr, 0); }
2229 // Add a type to the package.
2231 add_type(const std::string& name, Type* type, source_location location)
2232 { return this->bindings_->add_type(name, this, type, location); }
2234 // Add a type declaration to the package.
2236 add_type_declaration(const std::string& name, source_location location)
2237 { return this->bindings_->add_type_declaration(name, this, location); }
2239 // Add a variable to the package.
2241 add_variable(const std::string& name, Variable* variable)
2242 { return this->bindings_->add_variable(name, this, variable); }
2244 // Add a function declaration to the package.
2246 add_function_declaration(const std::string& name, Function_type* type,
2247 source_location loc)
2248 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2250 // Determine types of constants.
2255 // The real name of this package.
2257 // The unique prefix for all exported global symbols.
2258 std::string unique_prefix_;
2259 // The names in this package.
2260 Bindings* bindings_;
2261 // The priority of this package. A package has a priority higher
2262 // than the priority of all of the packages that it imports. This
2263 // is used to run init functions in the right order.
2265 // The location of the import statement.
2266 source_location location_;
2267 // True if some name from this package was used. This is mutable
2268 // because we can use a package even if we have a const pointer to
2271 // True if this package was imported in the current file.
2273 // True if this package was imported with a name of "_".
2274 bool uses_sink_alias_;
2277 // Return codes for the traversal functions. This is not an enum
2278 // because we want to be able to declare traversal functions in other
2279 // header files without including this one.
2281 // Continue traversal as usual.
2282 const int TRAVERSE_CONTINUE = -1;
2285 const int TRAVERSE_EXIT = 0;
2287 // Continue traversal, but skip components of the current object.
2288 // E.g., if this is returned by Traverse::statement, we do not
2289 // traverse the expressions in the statement even if
2290 // traverse_expressions is set in the traverse_mask.
2291 const int TRAVERSE_SKIP_COMPONENTS = 1;
2293 // This class is used when traversing the parse tree. The caller uses
2294 // a subclass which overrides functions as desired.
2299 // These bitmasks say what to traverse.
2300 static const unsigned int traverse_variables = 0x1;
2301 static const unsigned int traverse_constants = 0x2;
2302 static const unsigned int traverse_functions = 0x4;
2303 static const unsigned int traverse_blocks = 0x8;
2304 static const unsigned int traverse_statements = 0x10;
2305 static const unsigned int traverse_expressions = 0x20;
2306 static const unsigned int traverse_types = 0x40;
2308 Traverse(unsigned int traverse_mask)
2309 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2312 virtual ~Traverse();
2314 // The bitmask of what to traverse.
2316 traverse_mask() const
2317 { return this->traverse_mask_; }
2319 // Record that we are going to traverse a type. This returns true
2320 // if the type has already been seen in this traversal. This is
2321 // required because types, unlike expressions, can form a circular
2324 remember_type(const Type*);
2326 // Record that we are going to see an expression. This returns true
2327 // if the expression has already been seen in this traversal. This
2328 // is only needed for cases where multiple expressions can point to
2331 remember_expression(const Expression*);
2333 // These functions return one of the TRAVERSE codes defined above.
2335 // If traverse_variables is set in the mask, this is called for
2336 // every variable in the tree.
2338 variable(Named_object*);
2340 // If traverse_constants is set in the mask, this is called for
2341 // every named constant in the tree. The bool parameter is true for
2342 // a global constant.
2344 constant(Named_object*, bool);
2346 // If traverse_functions is set in the mask, this is called for
2347 // every function in the tree.
2349 function(Named_object*);
2351 // If traverse_blocks is set in the mask, this is called for every
2352 // block in the tree.
2356 // If traverse_statements is set in the mask, this is called for
2357 // every statement in the tree.
2359 statement(Block*, size_t* index, Statement*);
2361 // If traverse_expressions is set in the mask, this is called for
2362 // every expression in the tree.
2364 expression(Expression**);
2366 // If traverse_types is set in the mask, this is called for every
2367 // type in the tree.
2372 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2373 Type_identical) Types_seen;
2375 typedef Unordered_set(const Expression*) Expressions_seen;
2377 // Bitmask of what sort of objects to traverse.
2378 unsigned int traverse_mask_;
2379 // Types which have been seen in this traversal.
2380 Types_seen* types_seen_;
2381 // Expressions which have been seen in this traversal.
2382 Expressions_seen* expressions_seen_;
2385 // When translating the gogo IR into trees, this is the context we
2386 // pass down the blocks and statements.
2388 class Translate_context
2391 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2393 : gogo_(gogo), function_(function), block_(block), block_tree_(block_tree),
2401 { return this->gogo_; }
2405 { return this->function_; }
2409 { return this->block_; }
2413 { return this->block_tree_; }
2417 { return this->is_const_; }
2419 // Make a constant context.
2422 { this->is_const_ = true; }
2425 // The IR for the entire compilation unit.
2427 // The function we are currently translating.
2428 Named_object* function_;
2429 // The block we are currently translating.
2431 // The BLOCK node for the current block.
2433 // Whether this is being evaluated in a constant context. This is
2434 // used for type descriptor initializers.
2438 // Runtime error codes. These must match the values in
2439 // libgo/runtime/go-runtime-error.c.
2441 // Slice index out of bounds: negative or larger than the length of
2443 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2445 // Array index out of bounds.
2446 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2448 // String index out of bounds.
2449 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2451 // Slice slice out of bounds: negative or larger than the length of
2452 // the slice or high bound less than low bound.
2453 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2455 // Array slice out of bounds.
2456 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2458 // String slice out of bounds.
2459 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2461 // Dereference of nil pointer. This is used when there is a
2462 // dereference of a pointer to a very large struct or array, to ensure
2463 // that a gigantic array is not used a proxy to access random memory
2465 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2467 // Slice length or capacity out of bounds in make: negative or
2468 // overflow or length greater than capacity.
2469 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2471 // Map capacity out of bounds in make: negative or overflow.
2472 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2474 // Channel capacity out of bounds in make: negative or overflow.
2475 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2477 // This is used by some of the langhooks.
2478 extern Gogo* go_get_gogo();
2480 // Whether we have seen any errors. FIXME: Replace with a backend
2482 extern bool saw_errors();
2484 #endif // !defined(GO_GOGO_H)