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;
44 // This file declares the basic classes used to hold the internal
45 // representation of Go which is built by the parser.
47 // An initialization function for an imported package. This is a
48 // magic function which initializes variables and runs the "init"
54 Import_init(const std::string& package_name, const std::string& init_name,
56 : package_name_(package_name), init_name_(init_name), priority_(priority)
59 // The name of the package being imported.
62 { return this->package_name_; }
64 // The name of the package's init function.
67 { return this->init_name_; }
69 // The priority of the initialization function. Functions with a
70 // lower priority number must be run first.
73 { return this->priority_; }
76 // The name of the package being imported.
77 std::string package_name_;
78 // The name of the package's init function.
79 std::string init_name_;
84 // For sorting purposes.
87 operator<(const Import_init& i1, const Import_init& i2)
89 if (i1.priority() < i2.priority())
91 if (i1.priority() > i2.priority())
93 if (i1.package_name() != i2.package_name())
94 return i1.package_name() < i2.package_name();
95 return i1.init_name() < i2.init_name();
98 // The holder for the internal representation of the entire
104 // Create the IR, passing in the sizes of the types "int" and
105 // "uintptr" in bits.
106 Gogo(Backend* backend, int int_type_size, int pointer_size);
108 // Get the backend generator.
111 { return this->backend_; }
113 // Get the package name.
115 package_name() const;
117 // Set the package name.
119 set_package_name(const std::string&, source_location);
121 // Return whether this is the "main" package.
123 is_main_package() const;
125 // If necessary, adjust the name to use for a hidden symbol. We add
126 // a prefix of the package name, so that hidden symbols in different
127 // packages do not collide.
129 pack_hidden_name(const std::string& name, bool is_exported) const
133 : ('.' + this->unique_prefix()
134 + '.' + this->package_name()
138 // Unpack a name which may have been hidden. Returns the
139 // user-visible name of the object.
141 unpack_hidden_name(const std::string& name)
142 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
144 // Return whether a possibly packed name is hidden.
146 is_hidden_name(const std::string& name)
147 { return name[0] == '.'; }
149 // Return the package prefix of a hidden name.
151 hidden_name_prefix(const std::string& name)
153 gcc_assert(Gogo::is_hidden_name(name));
154 return name.substr(1, name.rfind('.') - 1);
157 // Given a name which may or may not have been hidden, return the
158 // name to use in an error message.
160 message_name(const std::string& name);
162 // Return whether a name is the blank identifier _.
164 is_sink_name(const std::string& name)
166 return (name[0] == '.'
167 && name[name.length() - 1] == '_'
168 && name[name.length() - 2] == '.');
171 // Return the unique prefix to use for all exported symbols.
173 unique_prefix() const;
175 // Set the unique prefix.
177 set_unique_prefix(const std::string&);
179 // Return the priority to use for the package we are compiling.
180 // This is two more than the largest priority of any package we
183 package_priority() const;
185 // Import a package. FILENAME is the file name argument, LOCAL_NAME
186 // is the local name to give to the package. If LOCAL_NAME is empty
187 // the declarations are added to the global scope.
189 import_package(const std::string& filename, const std::string& local_name,
190 bool is_local_name_exported, source_location);
192 // Whether we are the global binding level.
194 in_global_scope() const;
196 // Look up a name in the current binding contours.
198 lookup(const std::string&, Named_object** pfunction) const;
200 // Look up a name in the current block.
202 lookup_in_block(const std::string&) const;
204 // Look up a name in the global namespace--the universal scope.
206 lookup_global(const char*) const;
208 // Add a new imported package. REAL_NAME is the real name of the
209 // package. ALIAS is the alias of the package; this may be the same
210 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
211 // this package should be added to the global namespace; this is
212 // true if the alias is ".". LOCATION is the location of the import
213 // statement. This returns the new package, or NULL on error.
215 add_imported_package(const std::string& real_name, const std::string& alias,
216 bool is_alias_exported,
217 const std::string& unique_prefix,
218 source_location location,
219 bool* padd_to_globals);
221 // Register a package. This package may or may not be imported.
222 // This returns the Package structure for the package, creating if
225 register_package(const std::string& name, const std::string& unique_prefix,
228 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
229 // method function should be added to the type of its receiver.
231 start_function(const std::string& name, Function_type* type,
232 bool add_method_to_type, source_location);
234 // Finish compiling a function.
236 finish_function(source_location);
238 // Return the current function.
240 current_function() const;
242 // Start a new block. This is not initially associated with a
245 start_block(source_location);
247 // Finish the current block and return it.
249 finish_block(source_location);
251 // Declare an unknown name. This is used while parsing. The name
252 // must be resolved by the end of the parse. Unknown names are
253 // always added at the package level.
255 add_unknown_name(const std::string& name, source_location);
257 // Declare a function.
259 declare_function(const std::string&, Function_type*, source_location);
263 add_label_definition(const std::string&, source_location);
265 // Add a label reference.
267 add_label_reference(const std::string&);
269 // Add a statement to the current block.
271 add_statement(Statement*);
273 // Add a block to the current block.
275 add_block(Block*, source_location);
279 add_constant(const Typed_identifier&, Expression*, int iota_value);
283 add_type(const std::string&, Type*, source_location);
285 // Add a named type. This is used for builtin types, and to add an
286 // imported type to the global scope.
288 add_named_type(Named_type*);
292 declare_type(const std::string&, source_location);
294 // Declare a type at the package level. This is used when the
295 // parser sees an unknown name where a type name is required.
297 declare_package_type(const std::string&, source_location);
299 // Define a type which was already declared.
301 define_type(Named_object*, Named_type*);
305 add_variable(const std::string&, Variable*);
307 // Add a sink--a reference to the blank identifier _.
311 // Add a named object to the current namespace. This is used for
312 // import . "package".
314 add_named_object(Named_object*);
316 // Return a name to use for a thunk function. A thunk function is
317 // one we create during the compilation, for a go statement or a
318 // defer statement or a method expression.
322 // Return whether an object is a thunk.
324 is_thunk(const Named_object*);
326 // Note that we've seen an interface type. This is used to build
327 // all required interface method tables.
329 record_interface_type(Interface_type*);
331 // Note that we need an initialization function.
334 { this->need_init_fn_ = true; }
336 // Clear out all names in file scope. This is called when we start
337 // parsing a new file.
341 // Traverse the tree. See the Traverse class.
345 // Define the predeclared global names.
347 define_global_names();
349 // Verify and complete all types.
353 // Lower the parse tree.
357 // Lower all the statements in a block.
359 lower_block(Named_object* function, Block*);
361 // Lower an expression.
363 lower_expression(Named_object* function, Expression**);
367 lower_constant(Named_object*);
369 // Finalize the method lists and build stub methods for named types.
373 // Work out the types to use for unspecified variables and
378 // Type check the program.
382 // Check the types in a single block. This is used for complicated
385 check_types_in_block(Block*);
387 // Check for return statements.
389 check_return_statements();
395 // Add an import control function for an imported package to the
398 add_import_init_fn(const std::string& package_name,
399 const std::string& init_name, int prio);
401 // Turn short-cut operators (&&, ||) into explicit if statements.
405 // Use temporary variables to force order of evaluation.
409 // Build thunks for functions which call recover.
411 build_recover_thunks();
413 // Simplify statements which might use thunks: go and defer
416 simplify_thunk_statements();
418 // Convert named types to the backend representation.
420 convert_named_types();
422 // Convert named types in a list of bindings.
424 convert_named_types_in_bindings(Bindings*);
426 // True if named types have been converted to the backend
429 named_types_are_converted() const
430 { return this->named_types_are_converted_; }
432 // Write out the global values.
436 // Build a call to a builtin function. PDECL should point to a NULL
437 // initialized static pointer which will hold the fndecl. NAME is
438 // the name of the function. NARGS is the number of arguments.
439 // RETTYPE is the return type. It is followed by NARGS pairs of
440 // type and argument (both trees).
442 call_builtin(tree* pdecl, source_location, const char* name, int nargs,
445 // Build a call to the runtime error function.
447 runtime_error(int code, source_location);
449 // Build a builtin struct with a list of fields.
451 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
454 // Mark a function declaration as a builtin library function.
456 mark_fndecl_as_builtin_library(tree fndecl);
458 // Build the type of the struct that holds a slice for the given
461 slice_type_tree(tree element_type_tree);
463 // Given a tree for a slice type, return the tree for the element
466 slice_element_type_tree(tree slice_type_tree);
468 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
469 // the slice. VALUES points to the values. COUNT is the size,
470 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
473 slice_constructor(tree slice_type_tree, tree values, tree count,
476 // Build a constructor for an empty slice. SLICE_TYPE_TREE is the
477 // type of the slice.
479 empty_slice_constructor(tree slice_type_tree);
481 // Build a map descriptor.
483 map_descriptor(Map_type*);
485 // Return a tree for the type of a map descriptor. This is struct
486 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
489 map_descriptor_type();
491 // Build a type descriptor for TYPE using INITIALIZER as the type
492 // descriptor. This builds a new decl stored in *PDECL.
494 build_type_descriptor_decl(const Type*, Expression* initializer,
497 // Build required interface method tables.
499 build_interface_method_tables();
501 // Build an interface method table for a type: a list of function
502 // pointers, one for each interface method. This returns a decl.
504 interface_method_table_for_type(const Interface_type*, Named_type*,
507 // Return a tree which allocate SIZE bytes to hold values of type
510 allocate_memory(Type *type, tree size, source_location);
512 // Return a type to use for pointer to const char.
514 const_char_pointer_type_tree();
516 // Build a string constant with the right type.
518 string_constant_tree(const std::string&);
520 // Build a Go string constant. This returns a pointer to the
523 go_string_constant_tree(const std::string&);
525 // Send a value on a channel.
527 send_on_channel(tree channel, tree val, bool blocking, bool for_select,
530 // Receive a value from a channel.
532 receive_from_channel(tree type_tree, tree channel, bool for_select,
535 // Return a tree for receiving an integer on a channel.
537 receive_as_64bit_integer(tree type, tree channel, bool blocking,
541 // Make a trampoline which calls FNADDR passing CLOSURE.
543 make_trampoline(tree fnaddr, tree closure, source_location);
546 // During parsing, we keep a stack of functions. Each function on
547 // the stack is one that we are currently parsing. For each
548 // function, we keep track of the current stack of blocks.
552 Named_object* function;
553 // The stack of active blocks in the function.
554 std::vector<Block*> blocks;
557 // The stack of functions.
558 typedef std::vector<Open_function> Open_functions;
560 // Create trees for implicit builtin functions.
562 define_builtin_function_trees();
564 // Set up the built-in unsafe package.
566 import_unsafe(const std::string&, bool is_exported, source_location);
568 // Add a new imported package.
570 add_package(const std::string& real_name, const std::string& alias,
571 const std::string& unique_prefix, source_location location);
573 // Return the current binding contour.
578 current_bindings() const;
580 // Return the current block.
584 // Get the name of the magic initialization function.
588 // Get the decl for the magic initialization function.
590 initialization_function_decl();
592 // Write the magic initialization function.
594 write_initialization_function(tree fndecl, tree init_stmt_list);
596 // Initialize imported packages.
600 // Register variables with the garbage collector.
602 register_gc_vars(const std::vector<Named_object*>&, tree*);
604 // Build a pointer to a Go string constant. This returns a pointer
607 ptr_go_string_constant_tree(const std::string&);
609 // Return the name to use for a type descriptor decl for an unnamed
612 unnamed_type_descriptor_decl_name(const Type* type);
614 // Return the name to use for a type descriptor decl for a type
615 // named NO, defined in IN_FUNCTION.
617 type_descriptor_decl_name(const Named_object* no,
618 const Named_object* in_function);
620 // Where a type descriptor should be defined.
621 enum Type_descriptor_location
623 // Defined in this file.
624 TYPE_DESCRIPTOR_DEFINED,
625 // Defined in some other file.
626 TYPE_DESCRIPTOR_UNDEFINED,
627 // Common definition which may occur in multiple files.
628 TYPE_DESCRIPTOR_COMMON
631 // Return where the decl for TYPE should be defined.
632 Type_descriptor_location
633 type_descriptor_location(const Type* type);
635 // Return the type of a trampoline.
637 trampoline_type_tree();
639 // Type used to map import names to packages.
640 typedef std::map<std::string, Package*> Imports;
642 // Type used to map package names to packages.
643 typedef std::map<std::string, Package*> Packages;
645 // Type used to map special names in the sys package.
646 typedef std::map<std::string, std::string> Sys_names;
648 // Hash table mapping map types to map descriptor decls.
649 typedef Unordered_map_hash(const Map_type*, tree, Type_hash_identical,
650 Type_identical) Map_descriptors;
652 // Map unnamed types to type descriptor decls.
653 typedef Unordered_map_hash(const Type*, tree, Type_hash_identical,
654 Type_identical) Type_descriptor_decls;
656 // The backend generator.
658 // The package we are compiling.
660 // The list of currently open functions during parsing.
661 Open_functions functions_;
662 // The global binding contour. This includes the builtin functions
663 // and the package we are compiling.
665 // Mapping from import file names to packages.
667 // Whether the magic unsafe package was imported.
668 bool imported_unsafe_;
669 // Mapping from package names we have seen to packages. This does
670 // not include the package we are compiling.
672 // Mapping from map types to map descriptors.
673 Map_descriptors* map_descriptors_;
674 // Mapping from unnamed types to type descriptor decls.
675 Type_descriptor_decls* type_descriptor_decls_;
676 // The functions named "init", if there are any.
677 std::vector<Named_object*> init_functions_;
678 // Whether we need a magic initialization function.
680 // The name of the magic initialization function.
681 std::string init_fn_name_;
682 // A list of import control variables for packages that we import.
683 std::set<Import_init> imported_init_fns_;
684 // The unique prefix used for all global symbols.
685 std::string unique_prefix_;
686 // Whether an explicit unique prefix was set by -fgo-prefix.
687 bool unique_prefix_specified_;
688 // A list of interface types defined while parsing.
689 std::vector<Interface_type*> interface_types_;
690 // Whether named types have been converted.
691 bool named_types_are_converted_;
694 // A block of statements.
699 Block(Block* enclosing, source_location);
701 // Return the enclosing block.
704 { return this->enclosing_; }
706 // Return the bindings of the block.
709 { return this->bindings_; }
713 { return this->bindings_; }
715 // Look at the block's statements.
716 const std::vector<Statement*>*
718 { return &this->statements_; }
720 // Return the start location. This is normally the location of the
721 // left curly brace which starts the block.
723 start_location() const
724 { return this->start_location_; }
726 // Return the end location. This is normally the location of the
727 // right curly brace which ends the block.
730 { return this->end_location_; }
732 // Add a statement to the block.
734 add_statement(Statement*);
736 // Add a statement to the front of the block.
738 add_statement_at_front(Statement*);
740 // Replace a statement in a block.
742 replace_statement(size_t index, Statement*);
744 // Add a Statement before statement number INDEX.
746 insert_statement_before(size_t index, Statement*);
748 // Add a Statement after statement number INDEX.
750 insert_statement_after(size_t index, Statement*);
752 // Set the end location of the block.
754 set_end_location(source_location location)
755 { this->end_location_ = location; }
757 // Traverse the tree.
761 // Set final types for unspecified variables and constants.
765 // Return true if execution of this block may fall through to the
768 may_fall_through() const;
770 // Return a tree of the code in this block.
772 get_tree(Translate_context*);
774 // Iterate over statements.
776 typedef std::vector<Statement*>::iterator iterator;
780 { return this->statements_.begin(); }
784 { return this->statements_.end(); }
789 // Statements in the block.
790 std::vector<Statement*> statements_;
793 // Location of start of block.
794 source_location start_location_;
795 // Location of end of block.
796 source_location end_location_;
804 Function(Function_type* type, Function*, Block*, source_location);
806 // Return the function's type.
809 { return this->type_; }
811 // Return the enclosing function if there is one.
814 { return this->enclosing_; }
816 // Set the enclosing function. This is used when building thunks
817 // for functions which call recover.
819 set_enclosing(Function* enclosing)
821 gcc_assert(this->enclosing_ == NULL);
822 this->enclosing_ = enclosing;
825 // Create the named result variables in the outer block.
827 create_named_result_variables(Gogo*);
829 // Update the named result variables when cloning a function which
832 update_named_result_variables();
834 // Add a new field to the closure variable.
836 add_closure_field(Named_object* var, source_location loc)
837 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
839 // Whether this function needs a closure.
841 needs_closure() const
842 { return !this->closure_fields_.empty(); }
844 // Return the closure variable, creating it if necessary. This is
845 // passed to the function as a static chain parameter.
849 // Set the closure variable. This is used when building thunks for
850 // functions which call recover.
852 set_closure_var(Named_object* v)
854 gcc_assert(this->closure_var_ == NULL);
855 this->closure_var_ = v;
858 // Return the variable for a reference to field INDEX in the closure
861 enclosing_var(unsigned int index)
863 gcc_assert(index < this->closure_fields_.size());
864 return closure_fields_[index].first;
867 // Set the type of the closure variable if there is one.
871 // Get the block of statements associated with the function.
874 { return this->block_; }
876 // Get the location of the start of the function.
879 { return this->location_; }
881 // Return whether this function is actually a method.
885 // Add a label definition to the function.
887 add_label_definition(const std::string& label_name, source_location);
889 // Add a label reference to a function.
891 add_label_reference(const std::string& label_name);
893 // Warn about labels that are defined but not used.
895 check_labels() const;
897 // Whether this function calls the predeclared recover function.
899 calls_recover() const
900 { return this->calls_recover_; }
902 // Record that this function calls the predeclared recover function.
903 // This is set during the lowering pass.
906 { this->calls_recover_ = true; }
908 // Whether this is a recover thunk function.
910 is_recover_thunk() const
911 { return this->is_recover_thunk_; }
913 // Record that this is a thunk built for a function which calls
916 set_is_recover_thunk()
917 { this->is_recover_thunk_ = true; }
919 // Whether this function already has a recover thunk.
921 has_recover_thunk() const
922 { return this->has_recover_thunk_; }
924 // Record that this function already has a recover thunk.
926 set_has_recover_thunk()
927 { this->has_recover_thunk_ = true; }
929 // Swap with another function. Used only for the thunk which calls
932 swap_for_recover(Function *);
934 // Traverse the tree.
938 // Determine types in the function.
942 // Return the function's decl given an identifier.
944 get_or_make_decl(Gogo*, Named_object*, tree id);
946 // Return the function's decl after it has been built.
950 gcc_assert(this->fndecl_ != NULL);
951 return this->fndecl_;
954 // Set the function decl to hold a tree of the function code.
956 build_tree(Gogo*, Named_object*);
958 // Get the value to return when not explicitly specified. May also
959 // add statements to execute first to STMT_LIST.
961 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
963 // Get a tree for the variable holding the defer stack.
965 defer_stack(source_location);
967 // Export the function.
969 export_func(Export*, const std::string& name) const;
971 // Export a function with a type.
973 export_func_with_type(Export*, const std::string& name,
974 const Function_type*);
976 // Import a function.
978 import_func(Import*, std::string* pname, Typed_identifier** receiver,
979 Typed_identifier_list** pparameters,
980 Typed_identifier_list** presults, bool* is_varargs);
983 // Type for mapping from label names to Label objects.
984 typedef Unordered_map(std::string, Label*) Labels;
987 make_receiver_parm_decl(Gogo*, Named_object*, tree);
990 copy_parm_to_heap(Gogo*, Named_object*, tree);
993 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
995 typedef std::vector<Named_object*> Named_results;
997 typedef std::vector<std::pair<Named_object*,
998 source_location> > Closure_fields;
1000 // The function's type.
1001 Function_type* type_;
1002 // The enclosing function. This is NULL when there isn't one, which
1003 // is the normal case.
1004 Function* enclosing_;
1005 // The named result variables, if any.
1006 Named_results* named_results_;
1007 // If there is a closure, this is the list of variables which appear
1008 // in the closure. This is created by the parser, and then resolved
1009 // to a real type when we lower parse trees.
1010 Closure_fields closure_fields_;
1011 // The closure variable, passed as a parameter using the static
1012 // chain parameter. Normally NULL.
1013 Named_object* closure_var_;
1014 // The outer block of statements in the function.
1016 // The source location of the start of the function.
1017 source_location location_;
1018 // Labels defined or referenced in the function.
1020 // The function decl.
1022 // A variable holding the defer stack variable. This is NULL unless
1023 // we actually need a defer stack.
1025 // True if this function calls the predeclared recover function.
1026 bool calls_recover_;
1027 // True if this a thunk built for a function which calls recover.
1028 bool is_recover_thunk_;
1029 // True if this function already has a recover thunk.
1030 bool has_recover_thunk_;
1033 // A function declaration.
1035 class Function_declaration
1038 Function_declaration(Function_type* fntype, source_location location)
1039 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1044 { return this->fntype_; }
1048 { return this->location_; }
1052 { return this->asm_name_; }
1054 // Set the assembler name.
1056 set_asm_name(const std::string& asm_name)
1057 { this->asm_name_ = asm_name; }
1059 // Return a decl for the function given an identifier.
1061 get_or_make_decl(Gogo*, Named_object*, tree id);
1063 // Export a function declaration.
1065 export_func(Export* exp, const std::string& name) const
1066 { Function::export_func_with_type(exp, name, this->fntype_); }
1069 // The type of the function.
1070 Function_type* fntype_;
1071 // The location of the declaration.
1072 source_location location_;
1073 // The assembler name: this is the name to use in references to the
1074 // function. This is normally empty.
1075 std::string asm_name_;
1076 // The function decl if needed.
1085 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1086 bool is_receiver, source_location);
1088 // Get the type of the variable.
1095 // Return whether the type is defined yet.
1098 { return this->type_ != NULL; }
1100 // Get the initial value.
1103 { return this->init_; }
1105 // Return whether there are any preinit statements.
1107 has_pre_init() const
1108 { return this->preinit_ != NULL; }
1110 // Return the preinit statements if any.
1113 { return this->preinit_; }
1115 // Return whether this is a global variable.
1118 { return this->is_global_; }
1120 // Return whether this is a function parameter.
1122 is_parameter() const
1123 { return this->is_parameter_; }
1125 // Return whether this is the receiver parameter of a method.
1128 { return this->is_receiver_; }
1130 // Change this parameter to be a receiver. This is used when
1131 // creating the thunks created for functions which call recover.
1135 gcc_assert(this->is_parameter_);
1136 this->is_receiver_ = true;
1139 // Change this parameter to not be a receiver. This is used when
1140 // creating the thunks created for functions which call recover.
1142 set_is_not_receiver()
1144 gcc_assert(this->is_parameter_);
1145 this->is_receiver_ = false;
1148 // Return whether this is the varargs parameter of a function.
1150 is_varargs_parameter() const
1151 { return this->is_varargs_parameter_; }
1153 // Whether this variable's address is taken.
1155 is_address_taken() const
1156 { return this->is_address_taken_; }
1158 // Whether this variable should live in the heap.
1161 { return this->is_address_taken_ && !this->is_global_; }
1163 // Get the source location of the variable's declaration.
1166 { return this->location_; }
1168 // Record that this is the varargs parameter of a function.
1170 set_is_varargs_parameter()
1172 gcc_assert(this->is_parameter_);
1173 this->is_varargs_parameter_ = true;
1176 // Clear the initial value; used for error handling.
1179 { this->init_ = NULL; }
1181 // Set the initial value; used for converting shortcuts.
1183 set_init(Expression* init)
1184 { this->init_ = init; }
1186 // Get the preinit block, a block of statements to be run before the
1187 // initialization expression.
1189 preinit_block(Gogo*);
1191 // Add a statement to be run before the initialization expression.
1192 // This is only used for global variables.
1194 add_preinit_statement(Gogo*, Statement*);
1196 // Lower the initialization expression after parsing is complete.
1198 lower_init_expression(Gogo*, Named_object*);
1200 // A special case: the init value is used only to determine the
1201 // type. This is used if the variable is defined using := with the
1202 // comma-ok form of a map index or a receive expression. The init
1203 // value is actually the map index expression or receive expression.
1204 // We use this because we may not know the right type at parse time.
1206 set_type_from_init_tuple()
1207 { this->type_from_init_tuple_ = true; }
1209 // Another special case: the init value is used only to determine
1210 // the type. This is used if the variable is defined using := with
1211 // a range clause. The init value is the range expression. The
1212 // type of the variable is the index type of the range expression
1213 // (i.e., the first value returned by a range).
1215 set_type_from_range_index()
1216 { this->type_from_range_index_ = true; }
1218 // Another special case: like set_type_from_range_index, but the
1219 // type is the value type of the range expression (i.e., the second
1220 // value returned by a range).
1222 set_type_from_range_value()
1223 { this->type_from_range_value_ = true; }
1225 // Another special case: the init value is used only to determine
1226 // the type. This is used if the variable is defined using := with
1227 // a case in a select statement. The init value is the channel.
1228 // The type of the variable is the channel's element type.
1230 set_type_from_chan_element()
1231 { this->type_from_chan_element_ = true; }
1233 // After we lower the select statement, we once again set the type
1234 // from the initialization expression.
1236 clear_type_from_chan_element()
1238 gcc_assert(this->type_from_chan_element_);
1239 this->type_from_chan_element_ = false;
1242 // Note that this variable was created for a type switch clause.
1244 set_is_type_switch_var()
1245 { this->is_type_switch_var_ = true; }
1247 // Traverse the initializer expression.
1249 traverse_expression(Traverse*);
1251 // Determine the type of the variable if necessary.
1255 // Note that something takes the address of this variable.
1258 { this->is_address_taken_ = true; }
1260 // Get the initial value of the variable as a tree. This may only
1261 // be called if has_pre_init() returns false.
1263 get_init_tree(Gogo*, Named_object* function);
1265 // Return a series of statements which sets the value of the
1266 // variable in DECL. This should only be called is has_pre_init()
1267 // returns true. DECL may be NULL for a sink variable.
1269 get_init_block(Gogo*, Named_object* function, tree decl);
1271 // Export the variable.
1273 export_var(Export*, const std::string& name) const;
1275 // Import a variable.
1277 import_var(Import*, std::string* pname, Type** ptype);
1280 // The type of a tuple.
1282 type_from_tuple(Expression*, bool) const;
1284 // The type of a range.
1286 type_from_range(Expression*, bool, bool) const;
1288 // The element type of a channel.
1290 type_from_chan_element(Expression*, bool) const;
1292 // The variable's type. This may be NULL if the type is set from
1295 // The initial value. This may be NULL if the variable should be
1296 // initialized to the default value for the type.
1298 // Statements to run before the init statement.
1300 // Location of variable definition.
1301 source_location location_;
1302 // Whether this is a global variable.
1303 bool is_global_ : 1;
1304 // Whether this is a function parameter.
1305 bool is_parameter_ : 1;
1306 // Whether this is the receiver parameter of a method.
1307 bool is_receiver_ : 1;
1308 // Whether this is the varargs parameter of a function.
1309 bool is_varargs_parameter_ : 1;
1310 // Whether something takes the address of this variable.
1311 bool is_address_taken_ : 1;
1312 // True if we have seen this variable in a traversal.
1314 // True if we have lowered the initialization expression.
1315 bool init_is_lowered_ : 1;
1316 // True if init is a tuple used to set the type.
1317 bool type_from_init_tuple_ : 1;
1318 // True if init is a range clause and the type is the index type.
1319 bool type_from_range_index_ : 1;
1320 // True if init is a range clause and the type is the value type.
1321 bool type_from_range_value_ : 1;
1322 // True if init is a channel and the type is the channel's element type.
1323 bool type_from_chan_element_ : 1;
1324 // True if this is a variable created for a type switch case.
1325 bool is_type_switch_var_ : 1;
1326 // True if we have determined types.
1327 bool determined_type_ : 1;
1330 // A variable which is really the name for a function return value, or
1333 class Result_variable
1336 Result_variable(Type* type, Function* function, int index)
1337 : type_(type), function_(function), index_(index),
1338 is_address_taken_(false)
1341 // Get the type of the result variable.
1344 { return this->type_; }
1346 // Get the function that this is associated with.
1349 { return this->function_; }
1351 // Index in the list of function results.
1354 { return this->index_; }
1356 // Whether this variable's address is taken.
1358 is_address_taken() const
1359 { return this->is_address_taken_; }
1361 // Note that something takes the address of this variable.
1364 { this->is_address_taken_ = true; }
1366 // Whether this variable should live in the heap.
1369 { return this->is_address_taken_; }
1371 // Set the function. This is used when cloning functions which call
1374 set_function(Function* function)
1375 { this->function_ = function; }
1378 // Type of result variable.
1380 // Function with which this is associated.
1381 Function* function_;
1382 // Index in list of results.
1384 // Whether something takes the address of this variable.
1385 bool is_address_taken_;
1388 // The value we keep for a named constant. This lets us hold a type
1389 // and an expression.
1391 class Named_constant
1394 Named_constant(Type* type, Expression* expr, int iota_value,
1395 source_location location)
1396 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1402 { return this->type_; }
1406 { return this->expr_; }
1410 { return this->iota_value_; }
1414 { return this->location_; }
1416 // Whether we are lowering.
1419 { return this->lowering_; }
1421 // Set that we are lowering.
1424 { this->lowering_ = true; }
1426 // We are no longer lowering.
1429 { this->lowering_ = false; }
1431 // Traverse the expression.
1433 traverse_expression(Traverse*);
1435 // Determine the type of the constant if necessary.
1439 // Indicate that we found and reported an error for this constant.
1443 // Export the constant.
1445 export_const(Export*, const std::string& name) const;
1447 // Import a constant.
1449 import_const(Import*, std::string*, Type**, Expression**);
1452 // The type of the constant.
1454 // The expression for the constant.
1456 // If the predeclared constant iota is used in EXPR_, this is the
1457 // value it will have. We do this because at parse time we don't
1458 // know whether the name "iota" will refer to the predeclared
1459 // constant or to something else. We put in the right value in when
1462 // The location of the definition.
1463 source_location location_;
1464 // Whether we are currently lowering this constant.
1468 // A type declaration.
1470 class Type_declaration
1473 Type_declaration(source_location location)
1474 : location_(location), in_function_(NULL), methods_(),
1475 issued_warning_(false)
1478 // Return the location.
1481 { return this->location_; }
1483 // Return the function in which this type is declared. This will
1484 // return NULL for a type declared in global scope.
1487 { return this->in_function_; }
1489 // Set the function in which this type is declared.
1491 set_in_function(Named_object* f)
1492 { this->in_function_ = f; }
1494 // Add a method to this type. This is used when methods are defined
1497 add_method(const std::string& name, Function* function);
1499 // Add a method declaration to this type.
1501 add_method_declaration(const std::string& name, Function_type* type,
1502 source_location location);
1504 // Return whether any methods were defined.
1506 has_methods() const;
1508 // Define methods when the real type is known.
1510 define_methods(Named_type*);
1512 // This is called if we are trying to use this type. It returns
1513 // true if we should issue a warning.
1518 typedef std::vector<Named_object*> Methods;
1520 // The location of the type declaration.
1521 source_location location_;
1522 // If this type is declared in a function, a pointer back to the
1523 // function in which it is defined.
1524 Named_object* in_function_;
1525 // Methods defined before the type is defined.
1527 // True if we have issued a warning about a use of this type
1528 // declaration when it is undefined.
1529 bool issued_warning_;
1532 // An unknown object. These are created by the parser for forward
1533 // references to names which have not been seen before. In a correct
1534 // program, these will always point to a real definition by the end of
1535 // the parse. Because they point to another Named_object, these may
1536 // only be referenced by Unknown_expression objects.
1541 Unknown_name(source_location location)
1542 : location_(location), real_named_object_(NULL)
1545 // Return the location where this name was first seen.
1548 { return this->location_; }
1550 // Return the real named object that this points to, or NULL if it
1551 // was never resolved.
1553 real_named_object() const
1554 { return this->real_named_object_; }
1556 // Set the real named object that this points to.
1558 set_real_named_object(Named_object* no);
1561 // The location where this name was first seen.
1562 source_location location_;
1563 // The real named object when it is known.
1568 // A named object named. This is the result of a declaration. We
1569 // don't use a superclass because they all have to be handled
1577 // An uninitialized Named_object. We should never see this.
1578 NAMED_OBJECT_UNINITIALIZED,
1579 // An unknown name. This is used for forward references. In a
1580 // correct program, these will all be resolved by the end of the
1582 NAMED_OBJECT_UNKNOWN,
1587 // A forward type declaration.
1588 NAMED_OBJECT_TYPE_DECLARATION,
1591 // A result variable in a function.
1592 NAMED_OBJECT_RESULT_VAR,
1593 // The blank identifier--the special variable named _.
1597 // A forward func declaration.
1598 NAMED_OBJECT_FUNC_DECLARATION,
1600 NAMED_OBJECT_PACKAGE
1603 // Return the classification.
1605 classification() const
1606 { return this->classification_; }
1612 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1616 { return this->classification_ == NAMED_OBJECT_CONST; }
1620 { return this->classification_ == NAMED_OBJECT_TYPE; }
1623 is_type_declaration() const
1624 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1628 { return this->classification_ == NAMED_OBJECT_VAR; }
1631 is_result_variable() const
1632 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1636 { return this->classification_ == NAMED_OBJECT_SINK; }
1640 { return this->classification_ == NAMED_OBJECT_FUNC; }
1643 is_function_declaration() const
1644 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1648 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1652 static Named_object*
1653 make_unknown_name(const std::string& name, source_location);
1655 static Named_object*
1656 make_constant(const Typed_identifier&, const Package*, Expression*,
1659 static Named_object*
1660 make_type(const std::string&, const Package*, Type*, source_location);
1662 static Named_object*
1663 make_type_declaration(const std::string&, const Package*, source_location);
1665 static Named_object*
1666 make_variable(const std::string&, const Package*, Variable*);
1668 static Named_object*
1669 make_result_variable(const std::string&, Result_variable*);
1671 static Named_object*
1674 static Named_object*
1675 make_function(const std::string&, const Package*, Function*);
1677 static Named_object*
1678 make_function_declaration(const std::string&, const Package*, Function_type*,
1681 static Named_object*
1682 make_package(const std::string& alias, Package* package);
1689 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1690 return this->u_.unknown_value;
1694 unknown_value() const
1696 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1697 return this->u_.unknown_value;
1703 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1704 return this->u_.const_value;
1707 const Named_constant*
1710 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1711 return this->u_.const_value;
1717 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1718 return this->u_.type_value;
1724 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1725 return this->u_.type_value;
1729 type_declaration_value()
1731 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1732 return this->u_.type_declaration;
1735 const Type_declaration*
1736 type_declaration_value() const
1738 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1739 return this->u_.type_declaration;
1745 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1746 return this->u_.var_value;
1752 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1753 return this->u_.var_value;
1759 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1760 return this->u_.result_var_value;
1763 const Result_variable*
1764 result_var_value() const
1766 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1767 return this->u_.result_var_value;
1773 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1774 return this->u_.func_value;
1780 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1781 return this->u_.func_value;
1784 Function_declaration*
1785 func_declaration_value()
1787 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1788 return this->u_.func_declaration_value;
1791 const Function_declaration*
1792 func_declaration_value() const
1794 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1795 return this->u_.func_declaration_value;
1801 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1802 return this->u_.package_value;
1806 package_value() const
1808 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1809 return this->u_.package_value;
1814 { return this->name_; }
1816 // Return the name to use in an error message. The difference is
1817 // that if this Named_object is defined in a different package, this
1818 // will return PACKAGE.NAME.
1820 message_name() const;
1824 { return this->package_; }
1826 // Resolve an unknown value if possible. This returns the same
1827 // Named_object or a new one.
1831 Named_object* ret = this;
1832 if (this->is_unknown())
1834 Named_object* r = this->unknown_value()->real_named_object();
1844 const Named_object* ret = this;
1845 if (this->is_unknown())
1847 const Named_object* r = this->unknown_value()->real_named_object();
1854 // The location where this object was defined or referenced.
1858 // Return a tree for the external identifier for this object.
1862 // Return a tree representing this object.
1864 get_tree(Gogo*, Named_object* function);
1866 // Define a type declaration.
1868 set_type_value(Named_type*);
1870 // Define a function declaration.
1872 set_function_value(Function*);
1874 // Declare an unknown name as a type declaration.
1878 // Export this object.
1880 export_named_object(Export*) const;
1883 Named_object(const std::string&, const Package*, Classification);
1885 // The name of the object.
1887 // The package that this object is in. This is NULL if it is in the
1888 // file we are compiling.
1889 const Package* package_;
1890 // The type of object this is.
1891 Classification classification_;
1895 Unknown_name* unknown_value;
1896 Named_constant* const_value;
1897 Named_type* type_value;
1898 Type_declaration* type_declaration;
1899 Variable* var_value;
1900 Result_variable* result_var_value;
1901 Function* func_value;
1902 Function_declaration* func_declaration_value;
1903 Package* package_value;
1905 // The DECL tree for this object if we have already converted it.
1909 // A binding contour. This binds names to objects.
1914 // Type for mapping from names to objects.
1915 typedef Unordered_map(std::string, Named_object*) Contour;
1917 Bindings(Bindings* enclosing);
1919 // Add an unknown name.
1921 add_unknown_name(const std::string& name, source_location location)
1923 return this->add_named_object(Named_object::make_unknown_name(name,
1929 add_constant(const Typed_identifier& tid, const Package* package,
1930 Expression* expr, int iota_value)
1932 return this->add_named_object(Named_object::make_constant(tid, package,
1939 add_type(const std::string& name, const Package* package, Type* type,
1940 source_location location)
1942 return this->add_named_object(Named_object::make_type(name, package, type,
1946 // Add a named type. This is used for builtin types, and to add an
1947 // imported type to the global scope.
1949 add_named_type(Named_type* named_type);
1951 // Add a type declaration.
1953 add_type_declaration(const std::string& name, const Package* package,
1954 source_location location)
1956 Named_object* no = Named_object::make_type_declaration(name, package,
1958 return this->add_named_object(no);
1963 add_variable(const std::string& name, const Package* package,
1966 return this->add_named_object(Named_object::make_variable(name, package,
1970 // Add a result variable.
1972 add_result_variable(const std::string& name, Result_variable* result)
1974 return this->add_named_object(Named_object::make_result_variable(name,
1980 add_function(const std::string& name, const Package*, Function* function);
1982 // Add a function declaration.
1984 add_function_declaration(const std::string& name, const Package* package,
1985 Function_type* type, source_location location);
1987 // Add a package. The location is the location of the import
1990 add_package(const std::string& alias, Package* package)
1992 Named_object* no = Named_object::make_package(alias, package);
1993 return this->add_named_object(no);
1996 // Define a type which was already declared.
1998 define_type(Named_object*, Named_type*);
2000 // Add a method to the list of objects. This is not added to the
2003 add_method(Named_object*);
2005 // Add a named object to this binding.
2007 add_named_object(Named_object* no)
2008 { return this->add_named_object_to_contour(&this->bindings_, no); }
2010 // Clear all names in file scope from the bindings.
2014 // Look up a name in this binding contour and in any enclosing
2015 // binding contours. This returns NULL if the name is not found.
2017 lookup(const std::string&) const;
2019 // Look up a name in this binding contour without looking in any
2020 // enclosing binding contours. Returns NULL if the name is not found.
2022 lookup_local(const std::string&) const;
2026 remove_binding(Named_object*);
2028 // Traverse the tree. See the Traverse class.
2030 traverse(Traverse*, bool is_global);
2032 // Iterate over definitions. This does not include things which
2033 // were only declared.
2035 typedef std::vector<Named_object*>::const_iterator
2036 const_definitions_iterator;
2038 const_definitions_iterator
2039 begin_definitions() const
2040 { return this->named_objects_.begin(); }
2042 const_definitions_iterator
2043 end_definitions() const
2044 { return this->named_objects_.end(); }
2046 // Return the number of definitions.
2048 size_definitions() const
2049 { return this->named_objects_.size(); }
2051 // Return whether there are no definitions.
2053 empty_definitions() const
2054 { return this->named_objects_.empty(); }
2056 // Iterate over declarations. This is everything that has been
2057 // declared, which includes everything which has been defined.
2059 typedef Contour::const_iterator const_declarations_iterator;
2061 const_declarations_iterator
2062 begin_declarations() const
2063 { return this->bindings_.begin(); }
2065 const_declarations_iterator
2066 end_declarations() const
2067 { return this->bindings_.end(); }
2069 // Return the number of declarations.
2071 size_declarations() const
2072 { return this->bindings_.size(); }
2074 // Return whether there are no declarations.
2076 empty_declarations() const
2077 { return this->bindings_.empty(); }
2079 // Return the first declaration.
2082 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2086 add_named_object_to_contour(Contour*, Named_object*);
2089 new_definition(Named_object*, Named_object*);
2091 // Enclosing bindings.
2092 Bindings* enclosing_;
2093 // The list of objects.
2094 std::vector<Named_object*> named_objects_;
2095 // The mapping from names to objects.
2104 Label(const std::string& name)
2105 : name_(name), location_(0), is_used_(false), decl_(NULL)
2108 // Return the label's name.
2111 { return this->name_; }
2113 // Return whether the label has been defined.
2116 { return this->location_ != 0; }
2118 // Return whether the label has been used.
2121 { return this->is_used_; }
2123 // Record that the label is used.
2126 { this->is_used_ = true; }
2128 // Return the location of the definition.
2131 { return this->location_; }
2133 // Define the label at LOCATION.
2135 define(source_location location)
2137 gcc_assert(this->location_ == 0);
2138 this->location_ = location;
2141 // Return the LABEL_DECL for this decl.
2145 // Return an expression for the address of this label.
2147 get_addr(source_location location);
2150 // The name of the label.
2152 // The location of the definition. This is 0 if the label has not
2153 // yet been defined.
2154 source_location location_;
2155 // Whether the label has been used.
2161 // An unnamed label. These are used when lowering loops.
2166 Unnamed_label(source_location location)
2167 : location_(location), decl_(NULL)
2170 // Get the location where the label is defined.
2173 { return this->location_; }
2175 // Set the location where the label is defined.
2177 set_location(source_location location)
2178 { this->location_ = location; }
2180 // Return a statement which defines this label.
2184 // Return a goto to this label from LOCATION.
2186 get_goto(source_location location);
2189 // Return the LABEL_DECL to use with GOTO_EXPR.
2193 // The location where the label is defined.
2194 source_location location_;
2199 // An imported package.
2204 Package(const std::string& name, const std::string& unique_prefix,
2205 source_location location);
2207 // The real name of this package. This may be different from the
2208 // name in the associated Named_object if the import statement used
2212 { return this->name_; }
2214 // Return the location of the import statement.
2217 { return this->location_; }
2219 // Get the unique prefix used for all symbols exported from this
2222 unique_prefix() const
2224 gcc_assert(!this->unique_prefix_.empty());
2225 return this->unique_prefix_;
2228 // The priority of this package. The init function of packages with
2229 // lower priority must be run before the init function of packages
2230 // with higher priority.
2233 { return this->priority_; }
2235 // Set the priority.
2237 set_priority(int priority);
2239 // Return the bindings.
2242 { return this->bindings_; }
2244 // Whether some symbol from the package was used.
2247 { return this->used_; }
2249 // Note that some symbol from this package was used.
2252 { this->used_ = true; }
2254 // Clear the used field for the next file.
2257 { this->used_ = false; }
2259 // Whether this package was imported in the current file.
2262 { return this->is_imported_; }
2264 // Note that this package was imported in the current file.
2267 { this->is_imported_ = true; }
2269 // Clear the imported field for the next file.
2272 { this->is_imported_ = false; }
2274 // Whether this package was imported with a name of "_".
2276 uses_sink_alias() const
2277 { return this->uses_sink_alias_; }
2279 // Note that this package was imported with a name of "_".
2281 set_uses_sink_alias()
2282 { this->uses_sink_alias_ = true; }
2284 // Clear the sink alias field for the next file.
2286 clear_uses_sink_alias()
2287 { this->uses_sink_alias_ = false; }
2289 // Look up a name in the package. Returns NULL if the name is not
2292 lookup(const std::string& name) const
2293 { return this->bindings_->lookup(name); }
2295 // Set the location of the package. This is used if it is seen in a
2296 // different import before it is really imported.
2298 set_location(source_location location)
2299 { this->location_ = location; }
2301 // Add a constant to the package.
2303 add_constant(const Typed_identifier& tid, Expression* expr)
2304 { return this->bindings_->add_constant(tid, this, expr, 0); }
2306 // Add a type to the package.
2308 add_type(const std::string& name, Type* type, source_location location)
2309 { return this->bindings_->add_type(name, this, type, location); }
2311 // Add a type declaration to the package.
2313 add_type_declaration(const std::string& name, source_location location)
2314 { return this->bindings_->add_type_declaration(name, this, location); }
2316 // Add a variable to the package.
2318 add_variable(const std::string& name, Variable* variable)
2319 { return this->bindings_->add_variable(name, this, variable); }
2321 // Add a function declaration to the package.
2323 add_function_declaration(const std::string& name, Function_type* type,
2324 source_location loc)
2325 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2327 // Determine types of constants.
2332 // The real name of this package.
2334 // The unique prefix for all exported global symbols.
2335 std::string unique_prefix_;
2336 // The names in this package.
2337 Bindings* bindings_;
2338 // The priority of this package. A package has a priority higher
2339 // than the priority of all of the packages that it imports. This
2340 // is used to run init functions in the right order.
2342 // The location of the import statement.
2343 source_location location_;
2344 // True if some name from this package was used. This is mutable
2345 // because we can use a package even if we have a const pointer to
2348 // True if this package was imported in the current file.
2350 // True if this package was imported with a name of "_".
2351 bool uses_sink_alias_;
2354 // Return codes for the traversal functions. This is not an enum
2355 // because we want to be able to declare traversal functions in other
2356 // header files without including this one.
2358 // Continue traversal as usual.
2359 const int TRAVERSE_CONTINUE = -1;
2362 const int TRAVERSE_EXIT = 0;
2364 // Continue traversal, but skip components of the current object.
2365 // E.g., if this is returned by Traverse::statement, we do not
2366 // traverse the expressions in the statement even if
2367 // traverse_expressions is set in the traverse_mask.
2368 const int TRAVERSE_SKIP_COMPONENTS = 1;
2370 // This class is used when traversing the parse tree. The caller uses
2371 // a subclass which overrides functions as desired.
2376 // These bitmasks say what to traverse.
2377 static const unsigned int traverse_variables = 0x1;
2378 static const unsigned int traverse_constants = 0x2;
2379 static const unsigned int traverse_functions = 0x4;
2380 static const unsigned int traverse_blocks = 0x8;
2381 static const unsigned int traverse_statements = 0x10;
2382 static const unsigned int traverse_expressions = 0x20;
2383 static const unsigned int traverse_types = 0x40;
2385 Traverse(unsigned int traverse_mask)
2386 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2389 virtual ~Traverse();
2391 // The bitmask of what to traverse.
2393 traverse_mask() const
2394 { return this->traverse_mask_; }
2396 // Record that we are going to traverse a type. This returns true
2397 // if the type has already been seen in this traversal. This is
2398 // required because types, unlike expressions, can form a circular
2401 remember_type(const Type*);
2403 // Record that we are going to see an expression. This returns true
2404 // if the expression has already been seen in this traversal. This
2405 // is only needed for cases where multiple expressions can point to
2408 remember_expression(const Expression*);
2410 // These functions return one of the TRAVERSE codes defined above.
2412 // If traverse_variables is set in the mask, this is called for
2413 // every variable in the tree.
2415 variable(Named_object*);
2417 // If traverse_constants is set in the mask, this is called for
2418 // every named constant in the tree. The bool parameter is true for
2419 // a global constant.
2421 constant(Named_object*, bool);
2423 // If traverse_functions is set in the mask, this is called for
2424 // every function in the tree.
2426 function(Named_object*);
2428 // If traverse_blocks is set in the mask, this is called for every
2429 // block in the tree.
2433 // If traverse_statements is set in the mask, this is called for
2434 // every statement in the tree.
2436 statement(Block*, size_t* index, Statement*);
2438 // If traverse_expressions is set in the mask, this is called for
2439 // every expression in the tree.
2441 expression(Expression**);
2443 // If traverse_types is set in the mask, this is called for every
2444 // type in the tree.
2449 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2450 Type_identical) Types_seen;
2452 typedef Unordered_set(const Expression*) Expressions_seen;
2454 // Bitmask of what sort of objects to traverse.
2455 unsigned int traverse_mask_;
2456 // Types which have been seen in this traversal.
2457 Types_seen* types_seen_;
2458 // Expressions which have been seen in this traversal.
2459 Expressions_seen* expressions_seen_;
2462 // When translating the gogo IR into the backend data structure, this
2463 // is the context we pass down the blocks and statements.
2465 class Translate_context
2468 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2470 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2471 block_(block), block_tree_(block_tree), is_const_(false)
2478 { return this->gogo_; }
2482 { return this->backend_; }
2486 { return this->function_; }
2490 { return this->block_; }
2494 { return this->block_tree_; }
2498 { return this->is_const_; }
2500 // Make a constant context.
2503 { this->is_const_ = true; }
2506 // The IR for the entire compilation unit.
2508 // The generator for the backend data structures.
2510 // The function we are currently translating.
2511 Named_object* function_;
2512 // The block we are currently translating.
2514 // The BLOCK node for the current block.
2516 // Whether this is being evaluated in a constant context. This is
2517 // used for type descriptor initializers.
2521 // Runtime error codes. These must match the values in
2522 // libgo/runtime/go-runtime-error.c.
2524 // Slice index out of bounds: negative or larger than the length of
2526 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2528 // Array index out of bounds.
2529 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2531 // String index out of bounds.
2532 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2534 // Slice slice out of bounds: negative or larger than the length of
2535 // the slice or high bound less than low bound.
2536 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2538 // Array slice out of bounds.
2539 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2541 // String slice out of bounds.
2542 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2544 // Dereference of nil pointer. This is used when there is a
2545 // dereference of a pointer to a very large struct or array, to ensure
2546 // that a gigantic array is not used a proxy to access random memory
2548 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2550 // Slice length or capacity out of bounds in make: negative or
2551 // overflow or length greater than capacity.
2552 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2554 // Map capacity out of bounds in make: negative or overflow.
2555 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2557 // Channel capacity out of bounds in make: negative or overflow.
2558 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2560 // This is used by some of the langhooks.
2561 extern Gogo* go_get_gogo();
2563 // Whether we have seen any errors. FIXME: Replace with a backend
2565 extern bool saw_errors();
2567 #endif // !defined(GO_GOGO_H)