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;
47 // This file declares the basic classes used to hold the internal
48 // representation of Go which is built by the parser.
50 // An initialization function for an imported package. This is a
51 // magic function which initializes variables and runs the "init"
57 Import_init(const std::string& package_name, const std::string& init_name,
59 : package_name_(package_name), init_name_(init_name), priority_(priority)
62 // The name of the package being imported.
65 { return this->package_name_; }
67 // The name of the package's init function.
70 { return this->init_name_; }
72 // The priority of the initialization function. Functions with a
73 // lower priority number must be run first.
76 { return this->priority_; }
79 // The name of the package being imported.
80 std::string package_name_;
81 // The name of the package's init function.
82 std::string init_name_;
87 // For sorting purposes.
90 operator<(const Import_init& i1, const Import_init& i2)
92 if (i1.priority() < i2.priority())
94 if (i1.priority() > i2.priority())
96 if (i1.package_name() != i2.package_name())
97 return i1.package_name() < i2.package_name();
98 return i1.init_name() < i2.init_name();
101 // The holder for the internal representation of the entire
107 // Create the IR, passing in the sizes of the types "int" and
108 // "uintptr" in bits.
109 Gogo(Backend* backend, int int_type_size, int pointer_size);
111 // Get the backend generator.
114 { return this->backend_; }
116 // Get the package name.
118 package_name() const;
120 // Set the package name.
122 set_package_name(const std::string&, source_location);
124 // Return whether this is the "main" package.
126 is_main_package() const;
128 // If necessary, adjust the name to use for a hidden symbol. We add
129 // a prefix of the package name, so that hidden symbols in different
130 // packages do not collide.
132 pack_hidden_name(const std::string& name, bool is_exported) const
136 : ('.' + this->unique_prefix()
137 + '.' + this->package_name()
141 // Unpack a name which may have been hidden. Returns the
142 // user-visible name of the object.
144 unpack_hidden_name(const std::string& name)
145 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
147 // Return whether a possibly packed name is hidden.
149 is_hidden_name(const std::string& name)
150 { return name[0] == '.'; }
152 // Return the package prefix of a hidden name.
154 hidden_name_prefix(const std::string& name)
156 gcc_assert(Gogo::is_hidden_name(name));
157 return name.substr(1, name.rfind('.') - 1);
160 // Given a name which may or may not have been hidden, return the
161 // name to use in an error message.
163 message_name(const std::string& name);
165 // Return whether a name is the blank identifier _.
167 is_sink_name(const std::string& name)
169 return (name[0] == '.'
170 && name[name.length() - 1] == '_'
171 && name[name.length() - 2] == '.');
174 // Return the unique prefix to use for all exported symbols.
176 unique_prefix() const;
178 // Set the unique prefix.
180 set_unique_prefix(const std::string&);
182 // Return the priority to use for the package we are compiling.
183 // This is two more than the largest priority of any package we
186 package_priority() const;
188 // Import a package. FILENAME is the file name argument, LOCAL_NAME
189 // is the local name to give to the package. If LOCAL_NAME is empty
190 // the declarations are added to the global scope.
192 import_package(const std::string& filename, const std::string& local_name,
193 bool is_local_name_exported, source_location);
195 // Whether we are the global binding level.
197 in_global_scope() const;
199 // Look up a name in the current binding contours.
201 lookup(const std::string&, Named_object** pfunction) const;
203 // Look up a name in the current block.
205 lookup_in_block(const std::string&) const;
207 // Look up a name in the global namespace--the universal scope.
209 lookup_global(const char*) const;
211 // Add a new imported package. REAL_NAME is the real name of the
212 // package. ALIAS is the alias of the package; this may be the same
213 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
214 // this package should be added to the global namespace; this is
215 // true if the alias is ".". LOCATION is the location of the import
216 // statement. This returns the new package, or NULL on error.
218 add_imported_package(const std::string& real_name, const std::string& alias,
219 bool is_alias_exported,
220 const std::string& unique_prefix,
221 source_location location,
222 bool* padd_to_globals);
224 // Register a package. This package may or may not be imported.
225 // This returns the Package structure for the package, creating if
228 register_package(const std::string& name, const std::string& unique_prefix,
231 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
232 // method function should be added to the type of its receiver.
234 start_function(const std::string& name, Function_type* type,
235 bool add_method_to_type, source_location);
237 // Finish compiling a function.
239 finish_function(source_location);
241 // Return the current function.
243 current_function() const;
245 // Start a new block. This is not initially associated with a
248 start_block(source_location);
250 // Finish the current block and return it.
252 finish_block(source_location);
254 // Declare an unknown name. This is used while parsing. The name
255 // must be resolved by the end of the parse. Unknown names are
256 // always added at the package level.
258 add_unknown_name(const std::string& name, source_location);
260 // Declare a function.
262 declare_function(const std::string&, Function_type*, source_location);
266 add_label_definition(const std::string&, source_location);
268 // Add a label reference.
270 add_label_reference(const std::string&);
272 // Add a statement to the current block.
274 add_statement(Statement*);
276 // Add a block to the current block.
278 add_block(Block*, source_location);
282 add_constant(const Typed_identifier&, Expression*, int iota_value);
286 add_type(const std::string&, Type*, source_location);
288 // Add a named type. This is used for builtin types, and to add an
289 // imported type to the global scope.
291 add_named_type(Named_type*);
295 declare_type(const std::string&, source_location);
297 // Declare a type at the package level. This is used when the
298 // parser sees an unknown name where a type name is required.
300 declare_package_type(const std::string&, source_location);
302 // Define a type which was already declared.
304 define_type(Named_object*, Named_type*);
308 add_variable(const std::string&, Variable*);
310 // Add a sink--a reference to the blank identifier _.
314 // Add a named object to the current namespace. This is used for
315 // import . "package".
317 add_named_object(Named_object*);
319 // Return a name to use for a thunk function. A thunk function is
320 // one we create during the compilation, for a go statement or a
321 // defer statement or a method expression.
325 // Return whether an object is a thunk.
327 is_thunk(const Named_object*);
329 // Note that we've seen an interface type. This is used to build
330 // all required interface method tables.
332 record_interface_type(Interface_type*);
334 // Note that we need an initialization function.
337 { this->need_init_fn_ = true; }
339 // Clear out all names in file scope. This is called when we start
340 // parsing a new file.
344 // Traverse the tree. See the Traverse class.
348 // Define the predeclared global names.
350 define_global_names();
352 // Verify and complete all types.
356 // Lower the parse tree.
360 // Lower all the statements in a block.
362 lower_block(Named_object* function, Block*);
364 // Lower an expression.
366 lower_expression(Named_object* function, Expression**);
370 lower_constant(Named_object*);
372 // Finalize the method lists and build stub methods for named types.
376 // Work out the types to use for unspecified variables and
381 // Type check the program.
385 // Check the types in a single block. This is used for complicated
388 check_types_in_block(Block*);
390 // Check for return statements.
392 check_return_statements();
398 // Add an import control function for an imported package to the
401 add_import_init_fn(const std::string& package_name,
402 const std::string& init_name, int prio);
404 // Turn short-cut operators (&&, ||) into explicit if statements.
408 // Use temporary variables to force order of evaluation.
412 // Build thunks for functions which call recover.
414 build_recover_thunks();
416 // Simplify statements which might use thunks: go and defer
419 simplify_thunk_statements();
421 // Convert named types to the backend representation.
423 convert_named_types();
425 // Convert named types in a list of bindings.
427 convert_named_types_in_bindings(Bindings*);
429 // True if named types have been converted to the backend
432 named_types_are_converted() const
433 { return this->named_types_are_converted_; }
435 // Write out the global values.
439 // Build a call to a builtin function. PDECL should point to a NULL
440 // initialized static pointer which will hold the fndecl. NAME is
441 // the name of the function. NARGS is the number of arguments.
442 // RETTYPE is the return type. It is followed by NARGS pairs of
443 // type and argument (both trees).
445 call_builtin(tree* pdecl, source_location, const char* name, int nargs,
448 // Build a call to the runtime error function.
450 runtime_error(int code, source_location);
452 // Build a builtin struct with a list of fields.
454 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
457 // Mark a function declaration as a builtin library function.
459 mark_fndecl_as_builtin_library(tree fndecl);
461 // Build the type of the struct that holds a slice for the given
464 slice_type_tree(tree element_type_tree);
466 // Given a tree for a slice type, return the tree for the element
469 slice_element_type_tree(tree slice_type_tree);
471 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
472 // the slice. VALUES points to the values. COUNT is the size,
473 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
476 slice_constructor(tree slice_type_tree, tree values, tree count,
479 // Build a constructor for an empty slice. SLICE_TYPE_TREE is the
480 // type of the slice.
482 empty_slice_constructor(tree slice_type_tree);
484 // Build a map descriptor.
486 map_descriptor(Map_type*);
488 // Return a tree for the type of a map descriptor. This is struct
489 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
492 map_descriptor_type();
494 // Build a type descriptor for TYPE using INITIALIZER as the type
495 // descriptor. This builds a new decl stored in *PDECL.
497 build_type_descriptor_decl(const Type*, Expression* initializer,
500 // Build required interface method tables.
502 build_interface_method_tables();
504 // Build an interface method table for a type: a list of function
505 // pointers, one for each interface method. This returns a decl.
507 interface_method_table_for_type(const Interface_type*, Named_type*,
510 // Return a tree which allocate SIZE bytes to hold values of type
513 allocate_memory(Type *type, tree size, source_location);
515 // Return a type to use for pointer to const char.
517 const_char_pointer_type_tree();
519 // Build a string constant with the right type.
521 string_constant_tree(const std::string&);
523 // Build a Go string constant. This returns a pointer to the
526 go_string_constant_tree(const std::string&);
528 // Send a value on a channel.
530 send_on_channel(tree channel, tree val, bool blocking, bool for_select,
533 // Receive a value from a channel.
535 receive_from_channel(tree type_tree, tree channel, bool for_select,
538 // Return a tree for receiving an integer on a channel.
540 receive_as_64bit_integer(tree type, tree channel, bool blocking,
544 // Make a trampoline which calls FNADDR passing CLOSURE.
546 make_trampoline(tree fnaddr, tree closure, source_location);
549 // During parsing, we keep a stack of functions. Each function on
550 // the stack is one that we are currently parsing. For each
551 // function, we keep track of the current stack of blocks.
555 Named_object* function;
556 // The stack of active blocks in the function.
557 std::vector<Block*> blocks;
560 // The stack of functions.
561 typedef std::vector<Open_function> Open_functions;
563 // Create trees for implicit builtin functions.
565 define_builtin_function_trees();
567 // Set up the built-in unsafe package.
569 import_unsafe(const std::string&, bool is_exported, source_location);
571 // Add a new imported package.
573 add_package(const std::string& real_name, const std::string& alias,
574 const std::string& unique_prefix, source_location location);
576 // Return the current binding contour.
581 current_bindings() const;
583 // Return the current block.
587 // Get the name of the magic initialization function.
591 // Get the decl for the magic initialization function.
593 initialization_function_decl();
595 // Write the magic initialization function.
597 write_initialization_function(tree fndecl, tree init_stmt_list);
599 // Initialize imported packages.
603 // Register variables with the garbage collector.
605 register_gc_vars(const std::vector<Named_object*>&, tree*);
607 // Build a pointer to a Go string constant. This returns a pointer
610 ptr_go_string_constant_tree(const std::string&);
612 // Return the name to use for a type descriptor decl for an unnamed
615 unnamed_type_descriptor_decl_name(const Type* type);
617 // Return the name to use for a type descriptor decl for a type
618 // named NO, defined in IN_FUNCTION.
620 type_descriptor_decl_name(const Named_object* no,
621 const Named_object* in_function);
623 // Where a type descriptor should be defined.
624 enum Type_descriptor_location
626 // Defined in this file.
627 TYPE_DESCRIPTOR_DEFINED,
628 // Defined in some other file.
629 TYPE_DESCRIPTOR_UNDEFINED,
630 // Common definition which may occur in multiple files.
631 TYPE_DESCRIPTOR_COMMON
634 // Return where the decl for TYPE should be defined.
635 Type_descriptor_location
636 type_descriptor_location(const Type* type);
638 // Return the type of a trampoline.
640 trampoline_type_tree();
642 // Type used to map import names to packages.
643 typedef std::map<std::string, Package*> Imports;
645 // Type used to map package names to packages.
646 typedef std::map<std::string, Package*> Packages;
648 // Type used to map special names in the sys package.
649 typedef std::map<std::string, std::string> Sys_names;
651 // Hash table mapping map types to map descriptor decls.
652 typedef Unordered_map_hash(const Map_type*, tree, Type_hash_identical,
653 Type_identical) Map_descriptors;
655 // Map unnamed types to type descriptor decls.
656 typedef Unordered_map_hash(const Type*, tree, Type_hash_identical,
657 Type_identical) Type_descriptor_decls;
659 // The backend generator.
661 // The package we are compiling.
663 // The list of currently open functions during parsing.
664 Open_functions functions_;
665 // The global binding contour. This includes the builtin functions
666 // and the package we are compiling.
668 // Mapping from import file names to packages.
670 // Whether the magic unsafe package was imported.
671 bool imported_unsafe_;
672 // Mapping from package names we have seen to packages. This does
673 // not include the package we are compiling.
675 // Mapping from map types to map descriptors.
676 Map_descriptors* map_descriptors_;
677 // Mapping from unnamed types to type descriptor decls.
678 Type_descriptor_decls* type_descriptor_decls_;
679 // The functions named "init", if there are any.
680 std::vector<Named_object*> init_functions_;
681 // Whether we need a magic initialization function.
683 // The name of the magic initialization function.
684 std::string init_fn_name_;
685 // A list of import control variables for packages that we import.
686 std::set<Import_init> imported_init_fns_;
687 // The unique prefix used for all global symbols.
688 std::string unique_prefix_;
689 // Whether an explicit unique prefix was set by -fgo-prefix.
690 bool unique_prefix_specified_;
691 // A list of interface types defined while parsing.
692 std::vector<Interface_type*> interface_types_;
693 // Whether named types have been converted.
694 bool named_types_are_converted_;
697 // A block of statements.
702 Block(Block* enclosing, source_location);
704 // Return the enclosing block.
707 { return this->enclosing_; }
709 // Return the bindings of the block.
712 { return this->bindings_; }
716 { return this->bindings_; }
718 // Look at the block's statements.
719 const std::vector<Statement*>*
721 { return &this->statements_; }
723 // Return the start location. This is normally the location of the
724 // left curly brace which starts the block.
726 start_location() const
727 { return this->start_location_; }
729 // Return the end location. This is normally the location of the
730 // right curly brace which ends the block.
733 { return this->end_location_; }
735 // Add a statement to the block.
737 add_statement(Statement*);
739 // Add a statement to the front of the block.
741 add_statement_at_front(Statement*);
743 // Replace a statement in a block.
745 replace_statement(size_t index, Statement*);
747 // Add a Statement before statement number INDEX.
749 insert_statement_before(size_t index, Statement*);
751 // Add a Statement after statement number INDEX.
753 insert_statement_after(size_t index, Statement*);
755 // Set the end location of the block.
757 set_end_location(source_location location)
758 { this->end_location_ = location; }
760 // Traverse the tree.
764 // Set final types for unspecified variables and constants.
768 // Return true if execution of this block may fall through to the
771 may_fall_through() const;
773 // Return a tree of the code in this block.
775 get_tree(Translate_context*);
777 // Iterate over statements.
779 typedef std::vector<Statement*>::iterator iterator;
783 { return this->statements_.begin(); }
787 { return this->statements_.end(); }
792 // Statements in the block.
793 std::vector<Statement*> statements_;
796 // Location of start of block.
797 source_location start_location_;
798 // Location of end of block.
799 source_location end_location_;
807 Function(Function_type* type, Function*, Block*, source_location);
809 // Return the function's type.
812 { return this->type_; }
814 // Return the enclosing function if there is one.
817 { return this->enclosing_; }
819 // Set the enclosing function. This is used when building thunks
820 // for functions which call recover.
822 set_enclosing(Function* enclosing)
824 gcc_assert(this->enclosing_ == NULL);
825 this->enclosing_ = enclosing;
828 // The result variables.
829 typedef std::vector<Named_object*> Results;
831 // Create the result variables in the outer block.
833 create_result_variables(Gogo*);
835 // Update the named result variables when cloning a function which
838 update_result_variables();
840 // Return the result variables.
843 { return this->results_; }
845 // Whether the result variables have names.
847 results_are_named() const
848 { return this->results_are_named_; }
850 // Add a new field to the closure variable.
852 add_closure_field(Named_object* var, source_location loc)
853 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
855 // Whether this function needs a closure.
857 needs_closure() const
858 { return !this->closure_fields_.empty(); }
860 // Return the closure variable, creating it if necessary. This is
861 // passed to the function as a static chain parameter.
865 // Set the closure variable. This is used when building thunks for
866 // functions which call recover.
868 set_closure_var(Named_object* v)
870 gcc_assert(this->closure_var_ == NULL);
871 this->closure_var_ = v;
874 // Return the variable for a reference to field INDEX in the closure
877 enclosing_var(unsigned int index)
879 gcc_assert(index < this->closure_fields_.size());
880 return closure_fields_[index].first;
883 // Set the type of the closure variable if there is one.
887 // Get the block of statements associated with the function.
890 { return this->block_; }
892 // Get the location of the start of the function.
895 { return this->location_; }
897 // Return whether this function is actually a method.
901 // Add a label definition to the function.
903 add_label_definition(const std::string& label_name, source_location);
905 // Add a label reference to a function.
907 add_label_reference(const std::string& label_name);
909 // Warn about labels that are defined but not used.
911 check_labels() const;
913 // Whether this function calls the predeclared recover function.
915 calls_recover() const
916 { return this->calls_recover_; }
918 // Record that this function calls the predeclared recover function.
919 // This is set during the lowering pass.
922 { this->calls_recover_ = true; }
924 // Whether this is a recover thunk function.
926 is_recover_thunk() const
927 { return this->is_recover_thunk_; }
929 // Record that this is a thunk built for a function which calls
932 set_is_recover_thunk()
933 { this->is_recover_thunk_ = true; }
935 // Whether this function already has a recover thunk.
937 has_recover_thunk() const
938 { return this->has_recover_thunk_; }
940 // Record that this function already has a recover thunk.
942 set_has_recover_thunk()
943 { this->has_recover_thunk_ = true; }
945 // Swap with another function. Used only for the thunk which calls
948 swap_for_recover(Function *);
950 // Traverse the tree.
954 // Determine types in the function.
958 // Return the function's decl given an identifier.
960 get_or_make_decl(Gogo*, Named_object*, tree id);
962 // Return the function's decl after it has been built.
966 gcc_assert(this->fndecl_ != NULL);
967 return this->fndecl_;
970 // Set the function decl to hold a tree of the function code.
972 build_tree(Gogo*, Named_object*);
974 // Get the value to return when not explicitly specified. May also
975 // add statements to execute first to STMT_LIST.
977 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
979 // Get a tree for the variable holding the defer stack.
981 defer_stack(source_location);
983 // Export the function.
985 export_func(Export*, const std::string& name) const;
987 // Export a function with a type.
989 export_func_with_type(Export*, const std::string& name,
990 const Function_type*);
992 // Import a function.
994 import_func(Import*, std::string* pname, Typed_identifier** receiver,
995 Typed_identifier_list** pparameters,
996 Typed_identifier_list** presults, bool* is_varargs);
999 // Type for mapping from label names to Label objects.
1000 typedef Unordered_map(std::string, Label*) Labels;
1003 make_receiver_parm_decl(Gogo*, Named_object*, tree);
1006 copy_parm_to_heap(Gogo*, Named_object*, tree);
1009 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1011 typedef std::vector<std::pair<Named_object*,
1012 source_location> > Closure_fields;
1014 // The function's type.
1015 Function_type* type_;
1016 // The enclosing function. This is NULL when there isn't one, which
1017 // is the normal case.
1018 Function* enclosing_;
1019 // The result variables, if any.
1021 // If there is a closure, this is the list of variables which appear
1022 // in the closure. This is created by the parser, and then resolved
1023 // to a real type when we lower parse trees.
1024 Closure_fields closure_fields_;
1025 // The closure variable, passed as a parameter using the static
1026 // chain parameter. Normally NULL.
1027 Named_object* closure_var_;
1028 // The outer block of statements in the function.
1030 // The source location of the start of the function.
1031 source_location location_;
1032 // Labels defined or referenced in the function.
1034 // The function decl.
1036 // A variable holding the defer stack variable. This is NULL unless
1037 // we actually need a defer stack.
1039 // True if the result variables are named.
1040 bool results_are_named_;
1041 // True if this function calls the predeclared recover function.
1042 bool calls_recover_;
1043 // True if this a thunk built for a function which calls recover.
1044 bool is_recover_thunk_;
1045 // True if this function already has a recover thunk.
1046 bool has_recover_thunk_;
1049 // A function declaration.
1051 class Function_declaration
1054 Function_declaration(Function_type* fntype, source_location location)
1055 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1060 { return this->fntype_; }
1064 { return this->location_; }
1068 { return this->asm_name_; }
1070 // Set the assembler name.
1072 set_asm_name(const std::string& asm_name)
1073 { this->asm_name_ = asm_name; }
1075 // Return a decl for the function given an identifier.
1077 get_or_make_decl(Gogo*, Named_object*, tree id);
1079 // Export a function declaration.
1081 export_func(Export* exp, const std::string& name) const
1082 { Function::export_func_with_type(exp, name, this->fntype_); }
1085 // The type of the function.
1086 Function_type* fntype_;
1087 // The location of the declaration.
1088 source_location location_;
1089 // The assembler name: this is the name to use in references to the
1090 // function. This is normally empty.
1091 std::string asm_name_;
1092 // The function decl if needed.
1101 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1102 bool is_receiver, source_location);
1104 // Get the type of the variable.
1111 // Return whether the type is defined yet.
1114 { return this->type_ != NULL; }
1116 // Get the initial value.
1119 { return this->init_; }
1121 // Return whether there are any preinit statements.
1123 has_pre_init() const
1124 { return this->preinit_ != NULL; }
1126 // Return the preinit statements if any.
1129 { return this->preinit_; }
1131 // Return whether this is a global variable.
1134 { return this->is_global_; }
1136 // Return whether this is a function parameter.
1138 is_parameter() const
1139 { return this->is_parameter_; }
1141 // Return whether this is the receiver parameter of a method.
1144 { return this->is_receiver_; }
1146 // Change this parameter to be a receiver. This is used when
1147 // creating the thunks created for functions which call recover.
1151 gcc_assert(this->is_parameter_);
1152 this->is_receiver_ = true;
1155 // Change this parameter to not be a receiver. This is used when
1156 // creating the thunks created for functions which call recover.
1158 set_is_not_receiver()
1160 gcc_assert(this->is_parameter_);
1161 this->is_receiver_ = false;
1164 // Return whether this is the varargs parameter of a function.
1166 is_varargs_parameter() const
1167 { return this->is_varargs_parameter_; }
1169 // Whether this variable's address is taken.
1171 is_address_taken() const
1172 { return this->is_address_taken_; }
1174 // Whether this variable should live in the heap.
1177 { return this->is_address_taken_ && !this->is_global_; }
1179 // Get the source location of the variable's declaration.
1182 { return this->location_; }
1184 // Record that this is the varargs parameter of a function.
1186 set_is_varargs_parameter()
1188 gcc_assert(this->is_parameter_);
1189 this->is_varargs_parameter_ = true;
1192 // Clear the initial value; used for error handling.
1195 { this->init_ = NULL; }
1197 // Set the initial value; used for converting shortcuts.
1199 set_init(Expression* init)
1200 { this->init_ = init; }
1202 // Get the preinit block, a block of statements to be run before the
1203 // initialization expression.
1205 preinit_block(Gogo*);
1207 // Add a statement to be run before the initialization expression.
1208 // This is only used for global variables.
1210 add_preinit_statement(Gogo*, Statement*);
1212 // Lower the initialization expression after parsing is complete.
1214 lower_init_expression(Gogo*, Named_object*);
1216 // A special case: the init value is used only to determine the
1217 // type. This is used if the variable is defined using := with the
1218 // comma-ok form of a map index or a receive expression. The init
1219 // value is actually the map index expression or receive expression.
1220 // We use this because we may not know the right type at parse time.
1222 set_type_from_init_tuple()
1223 { this->type_from_init_tuple_ = 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 range clause. The init value is the range expression. The
1228 // type of the variable is the index type of the range expression
1229 // (i.e., the first value returned by a range).
1231 set_type_from_range_index()
1232 { this->type_from_range_index_ = true; }
1234 // Another special case: like set_type_from_range_index, but the
1235 // type is the value type of the range expression (i.e., the second
1236 // value returned by a range).
1238 set_type_from_range_value()
1239 { this->type_from_range_value_ = true; }
1241 // Another special case: the init value is used only to determine
1242 // the type. This is used if the variable is defined using := with
1243 // a case in a select statement. The init value is the channel.
1244 // The type of the variable is the channel's element type.
1246 set_type_from_chan_element()
1247 { this->type_from_chan_element_ = true; }
1249 // After we lower the select statement, we once again set the type
1250 // from the initialization expression.
1252 clear_type_from_chan_element()
1254 gcc_assert(this->type_from_chan_element_);
1255 this->type_from_chan_element_ = false;
1258 // Note that this variable was created for a type switch clause.
1260 set_is_type_switch_var()
1261 { this->is_type_switch_var_ = true; }
1263 // Traverse the initializer expression.
1265 traverse_expression(Traverse*);
1267 // Determine the type of the variable if necessary.
1271 // Note that something takes the address of this variable.
1274 { this->is_address_taken_ = true; }
1276 // Get the initial value of the variable as a tree. This may only
1277 // be called if has_pre_init() returns false.
1279 get_init_tree(Gogo*, Named_object* function);
1281 // Return a series of statements which sets the value of the
1282 // variable in DECL. This should only be called is has_pre_init()
1283 // returns true. DECL may be NULL for a sink variable.
1285 get_init_block(Gogo*, Named_object* function, tree decl);
1287 // Export the variable.
1289 export_var(Export*, const std::string& name) const;
1291 // Import a variable.
1293 import_var(Import*, std::string* pname, Type** ptype);
1296 // The type of a tuple.
1298 type_from_tuple(Expression*, bool) const;
1300 // The type of a range.
1302 type_from_range(Expression*, bool, bool) const;
1304 // The element type of a channel.
1306 type_from_chan_element(Expression*, bool) const;
1308 // The variable's type. This may be NULL if the type is set from
1311 // The initial value. This may be NULL if the variable should be
1312 // initialized to the default value for the type.
1314 // Statements to run before the init statement.
1316 // Location of variable definition.
1317 source_location location_;
1318 // Whether this is a global variable.
1319 bool is_global_ : 1;
1320 // Whether this is a function parameter.
1321 bool is_parameter_ : 1;
1322 // Whether this is the receiver parameter of a method.
1323 bool is_receiver_ : 1;
1324 // Whether this is the varargs parameter of a function.
1325 bool is_varargs_parameter_ : 1;
1326 // Whether something takes the address of this variable.
1327 bool is_address_taken_ : 1;
1328 // True if we have seen this variable in a traversal.
1330 // True if we have lowered the initialization expression.
1331 bool init_is_lowered_ : 1;
1332 // True if init is a tuple used to set the type.
1333 bool type_from_init_tuple_ : 1;
1334 // True if init is a range clause and the type is the index type.
1335 bool type_from_range_index_ : 1;
1336 // True if init is a range clause and the type is the value type.
1337 bool type_from_range_value_ : 1;
1338 // True if init is a channel and the type is the channel's element type.
1339 bool type_from_chan_element_ : 1;
1340 // True if this is a variable created for a type switch case.
1341 bool is_type_switch_var_ : 1;
1342 // True if we have determined types.
1343 bool determined_type_ : 1;
1346 // A variable which is really the name for a function return value, or
1349 class Result_variable
1352 Result_variable(Type* type, Function* function, int index)
1353 : type_(type), function_(function), index_(index),
1354 is_address_taken_(false)
1357 // Get the type of the result variable.
1360 { return this->type_; }
1362 // Get the function that this is associated with.
1365 { return this->function_; }
1367 // Index in the list of function results.
1370 { return this->index_; }
1372 // Whether this variable's address is taken.
1374 is_address_taken() const
1375 { return this->is_address_taken_; }
1377 // Note that something takes the address of this variable.
1380 { this->is_address_taken_ = true; }
1382 // Whether this variable should live in the heap.
1385 { return this->is_address_taken_; }
1387 // Set the function. This is used when cloning functions which call
1390 set_function(Function* function)
1391 { this->function_ = function; }
1394 // Type of result variable.
1396 // Function with which this is associated.
1397 Function* function_;
1398 // Index in list of results.
1400 // Whether something takes the address of this variable.
1401 bool is_address_taken_;
1404 // The value we keep for a named constant. This lets us hold a type
1405 // and an expression.
1407 class Named_constant
1410 Named_constant(Type* type, Expression* expr, int iota_value,
1411 source_location location)
1412 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1418 { return this->type_; }
1422 { return this->expr_; }
1426 { return this->iota_value_; }
1430 { return this->location_; }
1432 // Whether we are lowering.
1435 { return this->lowering_; }
1437 // Set that we are lowering.
1440 { this->lowering_ = true; }
1442 // We are no longer lowering.
1445 { this->lowering_ = false; }
1447 // Traverse the expression.
1449 traverse_expression(Traverse*);
1451 // Determine the type of the constant if necessary.
1455 // Indicate that we found and reported an error for this constant.
1459 // Export the constant.
1461 export_const(Export*, const std::string& name) const;
1463 // Import a constant.
1465 import_const(Import*, std::string*, Type**, Expression**);
1468 // The type of the constant.
1470 // The expression for the constant.
1472 // If the predeclared constant iota is used in EXPR_, this is the
1473 // value it will have. We do this because at parse time we don't
1474 // know whether the name "iota" will refer to the predeclared
1475 // constant or to something else. We put in the right value in when
1478 // The location of the definition.
1479 source_location location_;
1480 // Whether we are currently lowering this constant.
1484 // A type declaration.
1486 class Type_declaration
1489 Type_declaration(source_location location)
1490 : location_(location), in_function_(NULL), methods_(),
1491 issued_warning_(false)
1494 // Return the location.
1497 { return this->location_; }
1499 // Return the function in which this type is declared. This will
1500 // return NULL for a type declared in global scope.
1503 { return this->in_function_; }
1505 // Set the function in which this type is declared.
1507 set_in_function(Named_object* f)
1508 { this->in_function_ = f; }
1510 // Add a method to this type. This is used when methods are defined
1513 add_method(const std::string& name, Function* function);
1515 // Add a method declaration to this type.
1517 add_method_declaration(const std::string& name, Function_type* type,
1518 source_location location);
1520 // Return whether any methods were defined.
1522 has_methods() const;
1524 // Define methods when the real type is known.
1526 define_methods(Named_type*);
1528 // This is called if we are trying to use this type. It returns
1529 // true if we should issue a warning.
1534 typedef std::vector<Named_object*> Methods;
1536 // The location of the type declaration.
1537 source_location location_;
1538 // If this type is declared in a function, a pointer back to the
1539 // function in which it is defined.
1540 Named_object* in_function_;
1541 // Methods defined before the type is defined.
1543 // True if we have issued a warning about a use of this type
1544 // declaration when it is undefined.
1545 bool issued_warning_;
1548 // An unknown object. These are created by the parser for forward
1549 // references to names which have not been seen before. In a correct
1550 // program, these will always point to a real definition by the end of
1551 // the parse. Because they point to another Named_object, these may
1552 // only be referenced by Unknown_expression objects.
1557 Unknown_name(source_location location)
1558 : location_(location), real_named_object_(NULL)
1561 // Return the location where this name was first seen.
1564 { return this->location_; }
1566 // Return the real named object that this points to, or NULL if it
1567 // was never resolved.
1569 real_named_object() const
1570 { return this->real_named_object_; }
1572 // Set the real named object that this points to.
1574 set_real_named_object(Named_object* no);
1577 // The location where this name was first seen.
1578 source_location location_;
1579 // The real named object when it is known.
1584 // A named object named. This is the result of a declaration. We
1585 // don't use a superclass because they all have to be handled
1593 // An uninitialized Named_object. We should never see this.
1594 NAMED_OBJECT_UNINITIALIZED,
1595 // An unknown name. This is used for forward references. In a
1596 // correct program, these will all be resolved by the end of the
1598 NAMED_OBJECT_UNKNOWN,
1603 // A forward type declaration.
1604 NAMED_OBJECT_TYPE_DECLARATION,
1607 // A result variable in a function.
1608 NAMED_OBJECT_RESULT_VAR,
1609 // The blank identifier--the special variable named _.
1613 // A forward func declaration.
1614 NAMED_OBJECT_FUNC_DECLARATION,
1616 NAMED_OBJECT_PACKAGE
1619 // Return the classification.
1621 classification() const
1622 { return this->classification_; }
1628 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1632 { return this->classification_ == NAMED_OBJECT_CONST; }
1636 { return this->classification_ == NAMED_OBJECT_TYPE; }
1639 is_type_declaration() const
1640 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1644 { return this->classification_ == NAMED_OBJECT_VAR; }
1647 is_result_variable() const
1648 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1652 { return this->classification_ == NAMED_OBJECT_SINK; }
1656 { return this->classification_ == NAMED_OBJECT_FUNC; }
1659 is_function_declaration() const
1660 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1664 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1668 static Named_object*
1669 make_unknown_name(const std::string& name, source_location);
1671 static Named_object*
1672 make_constant(const Typed_identifier&, const Package*, Expression*,
1675 static Named_object*
1676 make_type(const std::string&, const Package*, Type*, source_location);
1678 static Named_object*
1679 make_type_declaration(const std::string&, const Package*, source_location);
1681 static Named_object*
1682 make_variable(const std::string&, const Package*, Variable*);
1684 static Named_object*
1685 make_result_variable(const std::string&, Result_variable*);
1687 static Named_object*
1690 static Named_object*
1691 make_function(const std::string&, const Package*, Function*);
1693 static Named_object*
1694 make_function_declaration(const std::string&, const Package*, Function_type*,
1697 static Named_object*
1698 make_package(const std::string& alias, Package* package);
1705 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1706 return this->u_.unknown_value;
1710 unknown_value() const
1712 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1713 return this->u_.unknown_value;
1719 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1720 return this->u_.const_value;
1723 const Named_constant*
1726 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1727 return this->u_.const_value;
1733 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1734 return this->u_.type_value;
1740 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1741 return this->u_.type_value;
1745 type_declaration_value()
1747 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1748 return this->u_.type_declaration;
1751 const Type_declaration*
1752 type_declaration_value() const
1754 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1755 return this->u_.type_declaration;
1761 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1762 return this->u_.var_value;
1768 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1769 return this->u_.var_value;
1775 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1776 return this->u_.result_var_value;
1779 const Result_variable*
1780 result_var_value() const
1782 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1783 return this->u_.result_var_value;
1789 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1790 return this->u_.func_value;
1796 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1797 return this->u_.func_value;
1800 Function_declaration*
1801 func_declaration_value()
1803 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1804 return this->u_.func_declaration_value;
1807 const Function_declaration*
1808 func_declaration_value() const
1810 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1811 return this->u_.func_declaration_value;
1817 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1818 return this->u_.package_value;
1822 package_value() const
1824 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1825 return this->u_.package_value;
1830 { return this->name_; }
1832 // Return the name to use in an error message. The difference is
1833 // that if this Named_object is defined in a different package, this
1834 // will return PACKAGE.NAME.
1836 message_name() const;
1840 { return this->package_; }
1842 // Resolve an unknown value if possible. This returns the same
1843 // Named_object or a new one.
1847 Named_object* ret = this;
1848 if (this->is_unknown())
1850 Named_object* r = this->unknown_value()->real_named_object();
1860 const Named_object* ret = this;
1861 if (this->is_unknown())
1863 const Named_object* r = this->unknown_value()->real_named_object();
1870 // The location where this object was defined or referenced.
1874 // Return a tree for the external identifier for this object.
1878 // Return a tree representing this object.
1880 get_tree(Gogo*, Named_object* function);
1882 // Define a type declaration.
1884 set_type_value(Named_type*);
1886 // Define a function declaration.
1888 set_function_value(Function*);
1890 // Declare an unknown name as a type declaration.
1894 // Export this object.
1896 export_named_object(Export*) const;
1899 Named_object(const std::string&, const Package*, Classification);
1901 // The name of the object.
1903 // The package that this object is in. This is NULL if it is in the
1904 // file we are compiling.
1905 const Package* package_;
1906 // The type of object this is.
1907 Classification classification_;
1911 Unknown_name* unknown_value;
1912 Named_constant* const_value;
1913 Named_type* type_value;
1914 Type_declaration* type_declaration;
1915 Variable* var_value;
1916 Result_variable* result_var_value;
1917 Function* func_value;
1918 Function_declaration* func_declaration_value;
1919 Package* package_value;
1921 // The DECL tree for this object if we have already converted it.
1925 // A binding contour. This binds names to objects.
1930 // Type for mapping from names to objects.
1931 typedef Unordered_map(std::string, Named_object*) Contour;
1933 Bindings(Bindings* enclosing);
1935 // Add an unknown name.
1937 add_unknown_name(const std::string& name, source_location location)
1939 return this->add_named_object(Named_object::make_unknown_name(name,
1945 add_constant(const Typed_identifier& tid, const Package* package,
1946 Expression* expr, int iota_value)
1948 return this->add_named_object(Named_object::make_constant(tid, package,
1955 add_type(const std::string& name, const Package* package, Type* type,
1956 source_location location)
1958 return this->add_named_object(Named_object::make_type(name, package, type,
1962 // Add a named type. This is used for builtin types, and to add an
1963 // imported type to the global scope.
1965 add_named_type(Named_type* named_type);
1967 // Add a type declaration.
1969 add_type_declaration(const std::string& name, const Package* package,
1970 source_location location)
1972 Named_object* no = Named_object::make_type_declaration(name, package,
1974 return this->add_named_object(no);
1979 add_variable(const std::string& name, const Package* package,
1982 return this->add_named_object(Named_object::make_variable(name, package,
1986 // Add a result variable.
1988 add_result_variable(const std::string& name, Result_variable* result)
1990 return this->add_named_object(Named_object::make_result_variable(name,
1996 add_function(const std::string& name, const Package*, Function* function);
1998 // Add a function declaration.
2000 add_function_declaration(const std::string& name, const Package* package,
2001 Function_type* type, source_location location);
2003 // Add a package. The location is the location of the import
2006 add_package(const std::string& alias, Package* package)
2008 Named_object* no = Named_object::make_package(alias, package);
2009 return this->add_named_object(no);
2012 // Define a type which was already declared.
2014 define_type(Named_object*, Named_type*);
2016 // Add a method to the list of objects. This is not added to the
2019 add_method(Named_object*);
2021 // Add a named object to this binding.
2023 add_named_object(Named_object* no)
2024 { return this->add_named_object_to_contour(&this->bindings_, no); }
2026 // Clear all names in file scope from the bindings.
2030 // Look up a name in this binding contour and in any enclosing
2031 // binding contours. This returns NULL if the name is not found.
2033 lookup(const std::string&) const;
2035 // Look up a name in this binding contour without looking in any
2036 // enclosing binding contours. Returns NULL if the name is not found.
2038 lookup_local(const std::string&) const;
2042 remove_binding(Named_object*);
2044 // Traverse the tree. See the Traverse class.
2046 traverse(Traverse*, bool is_global);
2048 // Iterate over definitions. This does not include things which
2049 // were only declared.
2051 typedef std::vector<Named_object*>::const_iterator
2052 const_definitions_iterator;
2054 const_definitions_iterator
2055 begin_definitions() const
2056 { return this->named_objects_.begin(); }
2058 const_definitions_iterator
2059 end_definitions() const
2060 { return this->named_objects_.end(); }
2062 // Return the number of definitions.
2064 size_definitions() const
2065 { return this->named_objects_.size(); }
2067 // Return whether there are no definitions.
2069 empty_definitions() const
2070 { return this->named_objects_.empty(); }
2072 // Iterate over declarations. This is everything that has been
2073 // declared, which includes everything which has been defined.
2075 typedef Contour::const_iterator const_declarations_iterator;
2077 const_declarations_iterator
2078 begin_declarations() const
2079 { return this->bindings_.begin(); }
2081 const_declarations_iterator
2082 end_declarations() const
2083 { return this->bindings_.end(); }
2085 // Return the number of declarations.
2087 size_declarations() const
2088 { return this->bindings_.size(); }
2090 // Return whether there are no declarations.
2092 empty_declarations() const
2093 { return this->bindings_.empty(); }
2095 // Return the first declaration.
2098 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2102 add_named_object_to_contour(Contour*, Named_object*);
2105 new_definition(Named_object*, Named_object*);
2107 // Enclosing bindings.
2108 Bindings* enclosing_;
2109 // The list of objects.
2110 std::vector<Named_object*> named_objects_;
2111 // The mapping from names to objects.
2120 Label(const std::string& name)
2121 : name_(name), location_(0), is_used_(false), blabel_(NULL)
2124 // Return the label's name.
2127 { return this->name_; }
2129 // Return whether the label has been defined.
2132 { return this->location_ != 0; }
2134 // Return whether the label has been used.
2137 { return this->is_used_; }
2139 // Record that the label is used.
2142 { this->is_used_ = true; }
2144 // Return the location of the definition.
2147 { return this->location_; }
2149 // Define the label at LOCATION.
2151 define(source_location location)
2153 gcc_assert(this->location_ == 0);
2154 this->location_ = location;
2157 // Return the backend representation for this label.
2159 get_backend_label(Translate_context*);
2161 // Return an expression for the address of this label. This is used
2162 // to get the return address of a deferred function to see whether
2163 // the function may call recover.
2165 get_addr(Translate_context*, source_location location);
2168 // The name of the label.
2170 // The location of the definition. This is 0 if the label has not
2171 // yet been defined.
2172 source_location location_;
2173 // Whether the label has been used.
2175 // The backend representation.
2179 // An unnamed label. These are used when lowering loops.
2184 Unnamed_label(source_location location)
2185 : location_(location), blabel_(NULL)
2188 // Get the location where the label is defined.
2191 { return this->location_; }
2193 // Set the location where the label is defined.
2195 set_location(source_location location)
2196 { this->location_ = location; }
2198 // Return a statement which defines this label.
2200 get_definition(Translate_context*);
2202 // Return a goto to this label from LOCATION.
2204 get_goto(Translate_context*, source_location location);
2207 // Return the backend representation.
2209 get_blabel(Translate_context*);
2211 // The location where the label is defined.
2212 source_location location_;
2213 // The backend representation of this label.
2217 // An imported package.
2222 Package(const std::string& name, const std::string& unique_prefix,
2223 source_location location);
2225 // The real name of this package. This may be different from the
2226 // name in the associated Named_object if the import statement used
2230 { return this->name_; }
2232 // Return the location of the import statement.
2235 { return this->location_; }
2237 // Get the unique prefix used for all symbols exported from this
2240 unique_prefix() const
2242 gcc_assert(!this->unique_prefix_.empty());
2243 return this->unique_prefix_;
2246 // The priority of this package. The init function of packages with
2247 // lower priority must be run before the init function of packages
2248 // with higher priority.
2251 { return this->priority_; }
2253 // Set the priority.
2255 set_priority(int priority);
2257 // Return the bindings.
2260 { return this->bindings_; }
2262 // Whether some symbol from the package was used.
2265 { return this->used_; }
2267 // Note that some symbol from this package was used.
2270 { this->used_ = true; }
2272 // Clear the used field for the next file.
2275 { this->used_ = false; }
2277 // Whether this package was imported in the current file.
2280 { return this->is_imported_; }
2282 // Note that this package was imported in the current file.
2285 { this->is_imported_ = true; }
2287 // Clear the imported field for the next file.
2290 { this->is_imported_ = false; }
2292 // Whether this package was imported with a name of "_".
2294 uses_sink_alias() const
2295 { return this->uses_sink_alias_; }
2297 // Note that this package was imported with a name of "_".
2299 set_uses_sink_alias()
2300 { this->uses_sink_alias_ = true; }
2302 // Clear the sink alias field for the next file.
2304 clear_uses_sink_alias()
2305 { this->uses_sink_alias_ = false; }
2307 // Look up a name in the package. Returns NULL if the name is not
2310 lookup(const std::string& name) const
2311 { return this->bindings_->lookup(name); }
2313 // Set the location of the package. This is used if it is seen in a
2314 // different import before it is really imported.
2316 set_location(source_location location)
2317 { this->location_ = location; }
2319 // Add a constant to the package.
2321 add_constant(const Typed_identifier& tid, Expression* expr)
2322 { return this->bindings_->add_constant(tid, this, expr, 0); }
2324 // Add a type to the package.
2326 add_type(const std::string& name, Type* type, source_location location)
2327 { return this->bindings_->add_type(name, this, type, location); }
2329 // Add a type declaration to the package.
2331 add_type_declaration(const std::string& name, source_location location)
2332 { return this->bindings_->add_type_declaration(name, this, location); }
2334 // Add a variable to the package.
2336 add_variable(const std::string& name, Variable* variable)
2337 { return this->bindings_->add_variable(name, this, variable); }
2339 // Add a function declaration to the package.
2341 add_function_declaration(const std::string& name, Function_type* type,
2342 source_location loc)
2343 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2345 // Determine types of constants.
2350 // The real name of this package.
2352 // The unique prefix for all exported global symbols.
2353 std::string unique_prefix_;
2354 // The names in this package.
2355 Bindings* bindings_;
2356 // The priority of this package. A package has a priority higher
2357 // than the priority of all of the packages that it imports. This
2358 // is used to run init functions in the right order.
2360 // The location of the import statement.
2361 source_location location_;
2362 // True if some name from this package was used. This is mutable
2363 // because we can use a package even if we have a const pointer to
2366 // True if this package was imported in the current file.
2368 // True if this package was imported with a name of "_".
2369 bool uses_sink_alias_;
2372 // Return codes for the traversal functions. This is not an enum
2373 // because we want to be able to declare traversal functions in other
2374 // header files without including this one.
2376 // Continue traversal as usual.
2377 const int TRAVERSE_CONTINUE = -1;
2380 const int TRAVERSE_EXIT = 0;
2382 // Continue traversal, but skip components of the current object.
2383 // E.g., if this is returned by Traverse::statement, we do not
2384 // traverse the expressions in the statement even if
2385 // traverse_expressions is set in the traverse_mask.
2386 const int TRAVERSE_SKIP_COMPONENTS = 1;
2388 // This class is used when traversing the parse tree. The caller uses
2389 // a subclass which overrides functions as desired.
2394 // These bitmasks say what to traverse.
2395 static const unsigned int traverse_variables = 0x1;
2396 static const unsigned int traverse_constants = 0x2;
2397 static const unsigned int traverse_functions = 0x4;
2398 static const unsigned int traverse_blocks = 0x8;
2399 static const unsigned int traverse_statements = 0x10;
2400 static const unsigned int traverse_expressions = 0x20;
2401 static const unsigned int traverse_types = 0x40;
2403 Traverse(unsigned int traverse_mask)
2404 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2407 virtual ~Traverse();
2409 // The bitmask of what to traverse.
2411 traverse_mask() const
2412 { return this->traverse_mask_; }
2414 // Record that we are going to traverse a type. This returns true
2415 // if the type has already been seen in this traversal. This is
2416 // required because types, unlike expressions, can form a circular
2419 remember_type(const Type*);
2421 // Record that we are going to see an expression. This returns true
2422 // if the expression has already been seen in this traversal. This
2423 // is only needed for cases where multiple expressions can point to
2426 remember_expression(const Expression*);
2428 // These functions return one of the TRAVERSE codes defined above.
2430 // If traverse_variables is set in the mask, this is called for
2431 // every variable in the tree.
2433 variable(Named_object*);
2435 // If traverse_constants is set in the mask, this is called for
2436 // every named constant in the tree. The bool parameter is true for
2437 // a global constant.
2439 constant(Named_object*, bool);
2441 // If traverse_functions is set in the mask, this is called for
2442 // every function in the tree.
2444 function(Named_object*);
2446 // If traverse_blocks is set in the mask, this is called for every
2447 // block in the tree.
2451 // If traverse_statements is set in the mask, this is called for
2452 // every statement in the tree.
2454 statement(Block*, size_t* index, Statement*);
2456 // If traverse_expressions is set in the mask, this is called for
2457 // every expression in the tree.
2459 expression(Expression**);
2461 // If traverse_types is set in the mask, this is called for every
2462 // type in the tree.
2467 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2468 Type_identical) Types_seen;
2470 typedef Unordered_set(const Expression*) Expressions_seen;
2472 // Bitmask of what sort of objects to traverse.
2473 unsigned int traverse_mask_;
2474 // Types which have been seen in this traversal.
2475 Types_seen* types_seen_;
2476 // Expressions which have been seen in this traversal.
2477 Expressions_seen* expressions_seen_;
2480 // When translating the gogo IR into the backend data structure, this
2481 // is the context we pass down the blocks and statements.
2483 class Translate_context
2486 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2488 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2489 block_(block), block_tree_(block_tree), is_const_(false)
2496 { return this->gogo_; }
2500 { return this->backend_; }
2504 { return this->function_; }
2508 { return this->block_; }
2512 { return this->block_tree_; }
2516 { return this->is_const_; }
2518 // Make a constant context.
2521 { this->is_const_ = true; }
2524 // The IR for the entire compilation unit.
2526 // The generator for the backend data structures.
2528 // The function we are currently translating.
2529 Named_object* function_;
2530 // The block we are currently translating.
2532 // The BLOCK node for the current block.
2534 // Whether this is being evaluated in a constant context. This is
2535 // used for type descriptor initializers.
2539 // Runtime error codes. These must match the values in
2540 // libgo/runtime/go-runtime-error.c.
2542 // Slice index out of bounds: negative or larger than the length of
2544 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2546 // Array index out of bounds.
2547 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2549 // String index out of bounds.
2550 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2552 // Slice slice out of bounds: negative or larger than the length of
2553 // the slice or high bound less than low bound.
2554 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2556 // Array slice out of bounds.
2557 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2559 // String slice out of bounds.
2560 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2562 // Dereference of nil pointer. This is used when there is a
2563 // dereference of a pointer to a very large struct or array, to ensure
2564 // that a gigantic array is not used a proxy to access random memory
2566 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2568 // Slice length or capacity out of bounds in make: negative or
2569 // overflow or length greater than capacity.
2570 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2572 // Map capacity out of bounds in make: negative or overflow.
2573 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2575 // Channel capacity out of bounds in make: negative or overflow.
2576 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2578 // This is used by some of the langhooks.
2579 extern Gogo* go_get_gogo();
2581 // Whether we have seen any errors. FIXME: Replace with a backend
2583 extern bool saw_errors();
2585 #endif // !defined(GO_GOGO_H)