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;
20 class Temporary_statement;
29 class Struct_field_list;
35 class Forward_declaration_type;
40 class Translate_context;
48 // This file declares the basic classes used to hold the internal
49 // representation of Go which is built by the parser.
51 // An initialization function for an imported package. This is a
52 // magic function which initializes variables and runs the "init"
58 Import_init(const std::string& package_name, const std::string& init_name,
60 : package_name_(package_name), init_name_(init_name), priority_(priority)
63 // The name of the package being imported.
66 { return this->package_name_; }
68 // The name of the package's init function.
71 { return this->init_name_; }
73 // The priority of the initialization function. Functions with a
74 // lower priority number must be run first.
77 { return this->priority_; }
80 // The name of the package being imported.
81 std::string package_name_;
82 // The name of the package's init function.
83 std::string init_name_;
88 // For sorting purposes.
91 operator<(const Import_init& i1, const Import_init& i2)
93 if (i1.priority() < i2.priority())
95 if (i1.priority() > i2.priority())
97 if (i1.package_name() != i2.package_name())
98 return i1.package_name() < i2.package_name();
99 return i1.init_name() < i2.init_name();
102 // The holder for the internal representation of the entire
108 // Create the IR, passing in the sizes of the types "int" and
109 // "uintptr" in bits.
110 Gogo(Backend* backend, int int_type_size, int pointer_size);
112 // Get the backend generator.
115 { return this->backend_; }
117 // Get the package name.
119 package_name() const;
121 // Set the package name.
123 set_package_name(const std::string&, source_location);
125 // Return whether this is the "main" package.
127 is_main_package() const;
129 // If necessary, adjust the name to use for a hidden symbol. We add
130 // a prefix of the package name, so that hidden symbols in different
131 // packages do not collide.
133 pack_hidden_name(const std::string& name, bool is_exported) const
137 : ('.' + this->unique_prefix()
138 + '.' + this->package_name()
142 // Unpack a name which may have been hidden. Returns the
143 // user-visible name of the object.
145 unpack_hidden_name(const std::string& name)
146 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
148 // Return whether a possibly packed name is hidden.
150 is_hidden_name(const std::string& name)
151 { return name[0] == '.'; }
153 // Return the package prefix of a hidden name.
155 hidden_name_prefix(const std::string& name)
157 gcc_assert(Gogo::is_hidden_name(name));
158 return name.substr(1, name.rfind('.') - 1);
161 // Given a name which may or may not have been hidden, return the
162 // name to use in an error message.
164 message_name(const std::string& name);
166 // Return whether a name is the blank identifier _.
168 is_sink_name(const std::string& name)
170 return (name[0] == '.'
171 && name[name.length() - 1] == '_'
172 && name[name.length() - 2] == '.');
175 // Return the unique prefix to use for all exported symbols.
177 unique_prefix() const;
179 // Set the unique prefix.
181 set_unique_prefix(const std::string&);
183 // Return the priority to use for the package we are compiling.
184 // This is two more than the largest priority of any package we
187 package_priority() const;
189 // Import a package. FILENAME is the file name argument, LOCAL_NAME
190 // is the local name to give to the package. If LOCAL_NAME is empty
191 // the declarations are added to the global scope.
193 import_package(const std::string& filename, const std::string& local_name,
194 bool is_local_name_exported, source_location);
196 // Whether we are the global binding level.
198 in_global_scope() const;
200 // Look up a name in the current binding contours.
202 lookup(const std::string&, Named_object** pfunction) const;
204 // Look up a name in the current block.
206 lookup_in_block(const std::string&) const;
208 // Look up a name in the global namespace--the universal scope.
210 lookup_global(const char*) const;
212 // Add a new imported package. REAL_NAME is the real name of the
213 // package. ALIAS is the alias of the package; this may be the same
214 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
215 // this package should be added to the global namespace; this is
216 // true if the alias is ".". LOCATION is the location of the import
217 // statement. This returns the new package, or NULL on error.
219 add_imported_package(const std::string& real_name, const std::string& alias,
220 bool is_alias_exported,
221 const std::string& unique_prefix,
222 source_location location,
223 bool* padd_to_globals);
225 // Register a package. This package may or may not be imported.
226 // This returns the Package structure for the package, creating if
229 register_package(const std::string& name, const std::string& unique_prefix,
232 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
233 // method function should be added to the type of its receiver.
235 start_function(const std::string& name, Function_type* type,
236 bool add_method_to_type, source_location);
238 // Finish compiling a function.
240 finish_function(source_location);
242 // Return the current function.
244 current_function() const;
246 // Start a new block. This is not initially associated with a
249 start_block(source_location);
251 // Finish the current block and return it.
253 finish_block(source_location);
255 // Declare an unknown name. This is used while parsing. The name
256 // must be resolved by the end of the parse. Unknown names are
257 // always added at the package level.
259 add_unknown_name(const std::string& name, source_location);
261 // Declare a function.
263 declare_function(const std::string&, Function_type*, source_location);
267 add_label_definition(const std::string&, source_location);
269 // Add a label reference.
271 add_label_reference(const std::string&);
273 // Add a statement to the current block.
275 add_statement(Statement*);
277 // Add a block to the current block.
279 add_block(Block*, source_location);
283 add_constant(const Typed_identifier&, Expression*, int iota_value);
287 add_type(const std::string&, Type*, source_location);
289 // Add a named type. This is used for builtin types, and to add an
290 // imported type to the global scope.
292 add_named_type(Named_type*);
296 declare_type(const std::string&, source_location);
298 // Declare a type at the package level. This is used when the
299 // parser sees an unknown name where a type name is required.
301 declare_package_type(const std::string&, source_location);
303 // Define a type which was already declared.
305 define_type(Named_object*, Named_type*);
309 add_variable(const std::string&, Variable*);
311 // Add a sink--a reference to the blank identifier _.
315 // Add a named object to the current namespace. This is used for
316 // import . "package".
318 add_named_object(Named_object*);
320 // Return a name to use for a thunk function. A thunk function is
321 // one we create during the compilation, for a go statement or a
322 // defer statement or a method expression.
326 // Return whether an object is a thunk.
328 is_thunk(const Named_object*);
330 // Note that we've seen an interface type. This is used to build
331 // all required interface method tables.
333 record_interface_type(Interface_type*);
335 // Note that we need an initialization function.
338 { this->need_init_fn_ = true; }
340 // Clear out all names in file scope. This is called when we start
341 // parsing a new file.
345 // Traverse the tree. See the Traverse class.
349 // Define the predeclared global names.
351 define_global_names();
353 // Verify and complete all types.
357 // Lower the parse tree.
361 // Lower all the statements in a block.
363 lower_block(Named_object* function, Block*);
365 // Lower an expression.
367 lower_expression(Named_object* function, Expression**);
371 lower_constant(Named_object*);
373 // Finalize the method lists and build stub methods for named types.
377 // Work out the types to use for unspecified variables and
382 // Type check the program.
386 // Check the types in a single block. This is used for complicated
389 check_types_in_block(Block*);
391 // Check for return statements.
393 check_return_statements();
399 // Add an import control function for an imported package to the
402 add_import_init_fn(const std::string& package_name,
403 const std::string& init_name, int prio);
405 // Turn short-cut operators (&&, ||) into explicit if statements.
409 // Use temporary variables to force order of evaluation.
413 // Build thunks for functions which call recover.
415 build_recover_thunks();
417 // Simplify statements which might use thunks: go and defer
420 simplify_thunk_statements();
422 // Convert named types to the backend representation.
424 convert_named_types();
426 // Convert named types in a list of bindings.
428 convert_named_types_in_bindings(Bindings*);
430 // True if named types have been converted to the backend
433 named_types_are_converted() const
434 { return this->named_types_are_converted_; }
436 // Write out the global values.
440 // Build a call to a builtin function. PDECL should point to a NULL
441 // initialized static pointer which will hold the fndecl. NAME is
442 // the name of the function. NARGS is the number of arguments.
443 // RETTYPE is the return type. It is followed by NARGS pairs of
444 // type and argument (both trees).
446 call_builtin(tree* pdecl, source_location, const char* name, int nargs,
449 // Build a call to the runtime error function.
451 runtime_error(int code, source_location);
453 // Build a builtin struct with a list of fields.
455 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
458 // Mark a function declaration as a builtin library function.
460 mark_fndecl_as_builtin_library(tree fndecl);
462 // Build the type of the struct that holds a slice for the given
465 slice_type_tree(tree element_type_tree);
467 // Given a tree for a slice type, return the tree for the element
470 slice_element_type_tree(tree slice_type_tree);
472 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
473 // the slice. VALUES points to the values. COUNT is the size,
474 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
477 slice_constructor(tree slice_type_tree, tree values, tree count,
480 // Build a constructor for an empty slice. SLICE_TYPE_TREE is the
481 // type of the slice.
483 empty_slice_constructor(tree slice_type_tree);
485 // Build a map descriptor.
487 map_descriptor(Map_type*);
489 // Return a tree for the type of a map descriptor. This is struct
490 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
493 map_descriptor_type();
495 // Build a type descriptor for TYPE using INITIALIZER as the type
496 // descriptor. This builds a new decl stored in *PDECL.
498 build_type_descriptor_decl(const Type*, Expression* initializer,
501 // Build required interface method tables.
503 build_interface_method_tables();
505 // Build an interface method table for a type: a list of function
506 // pointers, one for each interface method. This returns a decl.
508 interface_method_table_for_type(const Interface_type*, Named_type*,
511 // Return a tree which allocate SIZE bytes to hold values of type
514 allocate_memory(Type *type, tree size, source_location);
516 // Return a type to use for pointer to const char.
518 const_char_pointer_type_tree();
520 // Build a string constant with the right type.
522 string_constant_tree(const std::string&);
524 // Build a Go string constant. This returns a pointer to the
527 go_string_constant_tree(const std::string&);
529 // Send a value on a channel.
531 send_on_channel(tree channel, tree val, bool blocking, bool for_select,
534 // Receive a value from a channel.
536 receive_from_channel(tree type_tree, tree channel, bool for_select,
539 // Return a tree for receiving an integer on a channel.
541 receive_as_64bit_integer(tree type, tree channel, bool blocking,
545 // Make a trampoline which calls FNADDR passing CLOSURE.
547 make_trampoline(tree fnaddr, tree closure, source_location);
550 // During parsing, we keep a stack of functions. Each function on
551 // the stack is one that we are currently parsing. For each
552 // function, we keep track of the current stack of blocks.
556 Named_object* function;
557 // The stack of active blocks in the function.
558 std::vector<Block*> blocks;
561 // The stack of functions.
562 typedef std::vector<Open_function> Open_functions;
564 // Create trees for implicit builtin functions.
566 define_builtin_function_trees();
568 // Set up the built-in unsafe package.
570 import_unsafe(const std::string&, bool is_exported, source_location);
572 // Add a new imported package.
574 add_package(const std::string& real_name, const std::string& alias,
575 const std::string& unique_prefix, source_location location);
577 // Return the current binding contour.
582 current_bindings() const;
584 // Return the current block.
588 // Get the name of the magic initialization function.
592 // Get the decl for the magic initialization function.
594 initialization_function_decl();
596 // Write the magic initialization function.
598 write_initialization_function(tree fndecl, tree init_stmt_list);
600 // Initialize imported packages.
604 // Register variables with the garbage collector.
606 register_gc_vars(const std::vector<Named_object*>&, tree*);
608 // Build a pointer to a Go string constant. This returns a pointer
611 ptr_go_string_constant_tree(const std::string&);
613 // Return the name to use for a type descriptor decl for an unnamed
616 unnamed_type_descriptor_decl_name(const Type* type);
618 // Return the name to use for a type descriptor decl for a type
619 // named NO, defined in IN_FUNCTION.
621 type_descriptor_decl_name(const Named_object* no,
622 const Named_object* in_function);
624 // Where a type descriptor should be defined.
625 enum Type_descriptor_location
627 // Defined in this file.
628 TYPE_DESCRIPTOR_DEFINED,
629 // Defined in some other file.
630 TYPE_DESCRIPTOR_UNDEFINED,
631 // Common definition which may occur in multiple files.
632 TYPE_DESCRIPTOR_COMMON
635 // Return where the decl for TYPE should be defined.
636 Type_descriptor_location
637 type_descriptor_location(const Type* type);
639 // Return the type of a trampoline.
641 trampoline_type_tree();
643 // Type used to map import names to packages.
644 typedef std::map<std::string, Package*> Imports;
646 // Type used to map package names to packages.
647 typedef std::map<std::string, Package*> Packages;
649 // Type used to map special names in the sys package.
650 typedef std::map<std::string, std::string> Sys_names;
652 // Hash table mapping map types to map descriptor decls.
653 typedef Unordered_map_hash(const Map_type*, tree, Type_hash_identical,
654 Type_identical) Map_descriptors;
656 // Map unnamed types to type descriptor decls.
657 typedef Unordered_map_hash(const Type*, tree, Type_hash_identical,
658 Type_identical) Type_descriptor_decls;
660 // The backend generator.
662 // The package we are compiling.
664 // The list of currently open functions during parsing.
665 Open_functions functions_;
666 // The global binding contour. This includes the builtin functions
667 // and the package we are compiling.
669 // Mapping from import file names to packages.
671 // Whether the magic unsafe package was imported.
672 bool imported_unsafe_;
673 // Mapping from package names we have seen to packages. This does
674 // not include the package we are compiling.
676 // Mapping from map types to map descriptors.
677 Map_descriptors* map_descriptors_;
678 // Mapping from unnamed types to type descriptor decls.
679 Type_descriptor_decls* type_descriptor_decls_;
680 // The functions named "init", if there are any.
681 std::vector<Named_object*> init_functions_;
682 // Whether we need a magic initialization function.
684 // The name of the magic initialization function.
685 std::string init_fn_name_;
686 // A list of import control variables for packages that we import.
687 std::set<Import_init> imported_init_fns_;
688 // The unique prefix used for all global symbols.
689 std::string unique_prefix_;
690 // Whether an explicit unique prefix was set by -fgo-prefix.
691 bool unique_prefix_specified_;
692 // A list of interface types defined while parsing.
693 std::vector<Interface_type*> interface_types_;
694 // Whether named types have been converted.
695 bool named_types_are_converted_;
698 // A block of statements.
703 Block(Block* enclosing, source_location);
705 // Return the enclosing block.
708 { return this->enclosing_; }
710 // Return the bindings of the block.
713 { return this->bindings_; }
717 { return this->bindings_; }
719 // Look at the block's statements.
720 const std::vector<Statement*>*
722 { return &this->statements_; }
724 // Return the start location. This is normally the location of the
725 // left curly brace which starts the block.
727 start_location() const
728 { return this->start_location_; }
730 // Return the end location. This is normally the location of the
731 // right curly brace which ends the block.
734 { return this->end_location_; }
736 // Add a statement to the block.
738 add_statement(Statement*);
740 // Add a statement to the front of the block.
742 add_statement_at_front(Statement*);
744 // Replace a statement in a block.
746 replace_statement(size_t index, Statement*);
748 // Add a Statement before statement number INDEX.
750 insert_statement_before(size_t index, Statement*);
752 // Add a Statement after statement number INDEX.
754 insert_statement_after(size_t index, Statement*);
756 // Set the end location of the block.
758 set_end_location(source_location location)
759 { this->end_location_ = location; }
761 // Traverse the tree.
765 // Set final types for unspecified variables and constants.
769 // Return true if execution of this block may fall through to the
772 may_fall_through() const;
774 // Return a tree of the code in this block.
776 get_tree(Translate_context*);
778 // Iterate over statements.
780 typedef std::vector<Statement*>::iterator iterator;
784 { return this->statements_.begin(); }
788 { return this->statements_.end(); }
793 // Statements in the block.
794 std::vector<Statement*> statements_;
797 // Location of start of block.
798 source_location start_location_;
799 // Location of end of block.
800 source_location end_location_;
808 Function(Function_type* type, Function*, Block*, source_location);
810 // Return the function's type.
813 { return this->type_; }
815 // Return the enclosing function if there is one.
818 { return this->enclosing_; }
820 // Set the enclosing function. This is used when building thunks
821 // for functions which call recover.
823 set_enclosing(Function* enclosing)
825 gcc_assert(this->enclosing_ == NULL);
826 this->enclosing_ = enclosing;
829 // The result variables.
830 typedef std::vector<Named_object*> Results;
832 // Create the result variables in the outer block.
834 create_result_variables(Gogo*);
836 // Update the named result variables when cloning a function which
839 update_result_variables();
841 // Return the result variables.
844 { return this->results_; }
846 // Whether the result variables have names.
848 results_are_named() const
849 { return this->results_are_named_; }
851 // Add a new field to the closure variable.
853 add_closure_field(Named_object* var, source_location loc)
854 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
856 // Whether this function needs a closure.
858 needs_closure() const
859 { return !this->closure_fields_.empty(); }
861 // Return the closure variable, creating it if necessary. This is
862 // passed to the function as a static chain parameter.
866 // Set the closure variable. This is used when building thunks for
867 // functions which call recover.
869 set_closure_var(Named_object* v)
871 gcc_assert(this->closure_var_ == NULL);
872 this->closure_var_ = v;
875 // Return the variable for a reference to field INDEX in the closure
878 enclosing_var(unsigned int index)
880 gcc_assert(index < this->closure_fields_.size());
881 return closure_fields_[index].first;
884 // Set the type of the closure variable if there is one.
888 // Get the block of statements associated with the function.
891 { return this->block_; }
893 // Get the location of the start of the function.
896 { return this->location_; }
898 // Return whether this function is actually a method.
902 // Add a label definition to the function.
904 add_label_definition(const std::string& label_name, source_location);
906 // Add a label reference to a function.
908 add_label_reference(const std::string& label_name);
910 // Warn about labels that are defined but not used.
912 check_labels() const;
914 // Whether this function calls the predeclared recover function.
916 calls_recover() const
917 { return this->calls_recover_; }
919 // Record that this function calls the predeclared recover function.
920 // This is set during the lowering pass.
923 { this->calls_recover_ = true; }
925 // Whether this is a recover thunk function.
927 is_recover_thunk() const
928 { return this->is_recover_thunk_; }
930 // Record that this is a thunk built for a function which calls
933 set_is_recover_thunk()
934 { this->is_recover_thunk_ = true; }
936 // Whether this function already has a recover thunk.
938 has_recover_thunk() const
939 { return this->has_recover_thunk_; }
941 // Record that this function already has a recover thunk.
943 set_has_recover_thunk()
944 { this->has_recover_thunk_ = true; }
946 // Swap with another function. Used only for the thunk which calls
949 swap_for_recover(Function *);
951 // Traverse the tree.
955 // Determine types in the function.
959 // Return the function's decl given an identifier.
961 get_or_make_decl(Gogo*, Named_object*, tree id);
963 // Return the function's decl after it has been built.
967 gcc_assert(this->fndecl_ != NULL);
968 return this->fndecl_;
971 // Set the function decl to hold a tree of the function code.
973 build_tree(Gogo*, Named_object*);
975 // Get the value to return when not explicitly specified. May also
976 // add statements to execute first to STMT_LIST.
978 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
980 // Get a tree for the variable holding the defer stack.
982 defer_stack(source_location);
984 // Export the function.
986 export_func(Export*, const std::string& name) const;
988 // Export a function with a type.
990 export_func_with_type(Export*, const std::string& name,
991 const Function_type*);
993 // Import a function.
995 import_func(Import*, std::string* pname, Typed_identifier** receiver,
996 Typed_identifier_list** pparameters,
997 Typed_identifier_list** presults, bool* is_varargs);
1000 // Type for mapping from label names to Label objects.
1001 typedef Unordered_map(std::string, Label*) Labels;
1004 make_receiver_parm_decl(Gogo*, Named_object*, tree);
1007 copy_parm_to_heap(Gogo*, Named_object*, tree);
1010 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1012 typedef std::vector<std::pair<Named_object*,
1013 source_location> > Closure_fields;
1015 // The function's type.
1016 Function_type* type_;
1017 // The enclosing function. This is NULL when there isn't one, which
1018 // is the normal case.
1019 Function* enclosing_;
1020 // The result variables, if any.
1022 // If there is a closure, this is the list of variables which appear
1023 // in the closure. This is created by the parser, and then resolved
1024 // to a real type when we lower parse trees.
1025 Closure_fields closure_fields_;
1026 // The closure variable, passed as a parameter using the static
1027 // chain parameter. Normally NULL.
1028 Named_object* closure_var_;
1029 // The outer block of statements in the function.
1031 // The source location of the start of the function.
1032 source_location location_;
1033 // Labels defined or referenced in the function.
1035 // The function decl.
1037 // The defer stack variable. A pointer to this variable is used to
1038 // distinguish the defer stack for one function from another. This
1039 // is NULL unless we actually need a defer stack.
1040 Temporary_statement* defer_stack_;
1041 // True if the result variables are named.
1042 bool results_are_named_;
1043 // True if this function calls the predeclared recover function.
1044 bool calls_recover_;
1045 // True if this a thunk built for a function which calls recover.
1046 bool is_recover_thunk_;
1047 // True if this function already has a recover thunk.
1048 bool has_recover_thunk_;
1051 // A function declaration.
1053 class Function_declaration
1056 Function_declaration(Function_type* fntype, source_location location)
1057 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1062 { return this->fntype_; }
1066 { return this->location_; }
1070 { return this->asm_name_; }
1072 // Set the assembler name.
1074 set_asm_name(const std::string& asm_name)
1075 { this->asm_name_ = asm_name; }
1077 // Return a decl for the function given an identifier.
1079 get_or_make_decl(Gogo*, Named_object*, tree id);
1081 // Export a function declaration.
1083 export_func(Export* exp, const std::string& name) const
1084 { Function::export_func_with_type(exp, name, this->fntype_); }
1087 // The type of the function.
1088 Function_type* fntype_;
1089 // The location of the declaration.
1090 source_location location_;
1091 // The assembler name: this is the name to use in references to the
1092 // function. This is normally empty.
1093 std::string asm_name_;
1094 // The function decl if needed.
1103 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1104 bool is_receiver, source_location);
1106 // Get the type of the variable.
1113 // Return whether the type is defined yet.
1116 { return this->type_ != NULL; }
1118 // Get the initial value.
1121 { return this->init_; }
1123 // Return whether there are any preinit statements.
1125 has_pre_init() const
1126 { return this->preinit_ != NULL; }
1128 // Return the preinit statements if any.
1131 { return this->preinit_; }
1133 // Return whether this is a global variable.
1136 { return this->is_global_; }
1138 // Return whether this is a function parameter.
1140 is_parameter() const
1141 { return this->is_parameter_; }
1143 // Return whether this is the receiver parameter of a method.
1146 { return this->is_receiver_; }
1148 // Change this parameter to be a receiver. This is used when
1149 // creating the thunks created for functions which call recover.
1153 gcc_assert(this->is_parameter_);
1154 this->is_receiver_ = true;
1157 // Change this parameter to not be a receiver. This is used when
1158 // creating the thunks created for functions which call recover.
1160 set_is_not_receiver()
1162 gcc_assert(this->is_parameter_);
1163 this->is_receiver_ = false;
1166 // Return whether this is the varargs parameter of a function.
1168 is_varargs_parameter() const
1169 { return this->is_varargs_parameter_; }
1171 // Whether this variable's address is taken.
1173 is_address_taken() const
1174 { return this->is_address_taken_; }
1176 // Whether this variable should live in the heap.
1179 { return this->is_address_taken_ && !this->is_global_; }
1181 // Get the source location of the variable's declaration.
1184 { return this->location_; }
1186 // Record that this is the varargs parameter of a function.
1188 set_is_varargs_parameter()
1190 gcc_assert(this->is_parameter_);
1191 this->is_varargs_parameter_ = true;
1194 // Clear the initial value; used for error handling.
1197 { this->init_ = NULL; }
1199 // Set the initial value; used for converting shortcuts.
1201 set_init(Expression* init)
1202 { this->init_ = init; }
1204 // Get the preinit block, a block of statements to be run before the
1205 // initialization expression.
1207 preinit_block(Gogo*);
1209 // Add a statement to be run before the initialization expression.
1210 // This is only used for global variables.
1212 add_preinit_statement(Gogo*, Statement*);
1214 // Lower the initialization expression after parsing is complete.
1216 lower_init_expression(Gogo*, Named_object*);
1218 // A special case: the init value is used only to determine the
1219 // type. This is used if the variable is defined using := with the
1220 // comma-ok form of a map index or a receive expression. The init
1221 // value is actually the map index expression or receive expression.
1222 // We use this because we may not know the right type at parse time.
1224 set_type_from_init_tuple()
1225 { this->type_from_init_tuple_ = true; }
1227 // Another special case: the init value is used only to determine
1228 // the type. This is used if the variable is defined using := with
1229 // a range clause. The init value is the range expression. The
1230 // type of the variable is the index type of the range expression
1231 // (i.e., the first value returned by a range).
1233 set_type_from_range_index()
1234 { this->type_from_range_index_ = true; }
1236 // Another special case: like set_type_from_range_index, but the
1237 // type is the value type of the range expression (i.e., the second
1238 // value returned by a range).
1240 set_type_from_range_value()
1241 { this->type_from_range_value_ = true; }
1243 // Another special case: the init value is used only to determine
1244 // the type. This is used if the variable is defined using := with
1245 // a case in a select statement. The init value is the channel.
1246 // The type of the variable is the channel's element type.
1248 set_type_from_chan_element()
1249 { this->type_from_chan_element_ = true; }
1251 // After we lower the select statement, we once again set the type
1252 // from the initialization expression.
1254 clear_type_from_chan_element()
1256 gcc_assert(this->type_from_chan_element_);
1257 this->type_from_chan_element_ = false;
1260 // Note that this variable was created for a type switch clause.
1262 set_is_type_switch_var()
1263 { this->is_type_switch_var_ = true; }
1265 // Traverse the initializer expression.
1267 traverse_expression(Traverse*);
1269 // Determine the type of the variable if necessary.
1273 // Note that something takes the address of this variable.
1276 { this->is_address_taken_ = true; }
1278 // Get the initial value of the variable as a tree. This may only
1279 // be called if has_pre_init() returns false.
1281 get_init_tree(Gogo*, Named_object* function);
1283 // Return a series of statements which sets the value of the
1284 // variable in DECL. This should only be called is has_pre_init()
1285 // returns true. DECL may be NULL for a sink variable.
1287 get_init_block(Gogo*, Named_object* function, tree decl);
1289 // Export the variable.
1291 export_var(Export*, const std::string& name) const;
1293 // Import a variable.
1295 import_var(Import*, std::string* pname, Type** ptype);
1298 // The type of a tuple.
1300 type_from_tuple(Expression*, bool) const;
1302 // The type of a range.
1304 type_from_range(Expression*, bool, bool) const;
1306 // The element type of a channel.
1308 type_from_chan_element(Expression*, bool) const;
1310 // The variable's type. This may be NULL if the type is set from
1313 // The initial value. This may be NULL if the variable should be
1314 // initialized to the default value for the type.
1316 // Statements to run before the init statement.
1318 // Location of variable definition.
1319 source_location location_;
1320 // Whether this is a global variable.
1321 bool is_global_ : 1;
1322 // Whether this is a function parameter.
1323 bool is_parameter_ : 1;
1324 // Whether this is the receiver parameter of a method.
1325 bool is_receiver_ : 1;
1326 // Whether this is the varargs parameter of a function.
1327 bool is_varargs_parameter_ : 1;
1328 // Whether something takes the address of this variable.
1329 bool is_address_taken_ : 1;
1330 // True if we have seen this variable in a traversal.
1332 // True if we have lowered the initialization expression.
1333 bool init_is_lowered_ : 1;
1334 // True if init is a tuple used to set the type.
1335 bool type_from_init_tuple_ : 1;
1336 // True if init is a range clause and the type is the index type.
1337 bool type_from_range_index_ : 1;
1338 // True if init is a range clause and the type is the value type.
1339 bool type_from_range_value_ : 1;
1340 // True if init is a channel and the type is the channel's element type.
1341 bool type_from_chan_element_ : 1;
1342 // True if this is a variable created for a type switch case.
1343 bool is_type_switch_var_ : 1;
1344 // True if we have determined types.
1345 bool determined_type_ : 1;
1348 // A variable which is really the name for a function return value, or
1351 class Result_variable
1354 Result_variable(Type* type, Function* function, int index)
1355 : type_(type), function_(function), index_(index),
1356 is_address_taken_(false)
1359 // Get the type of the result variable.
1362 { return this->type_; }
1364 // Get the function that this is associated with.
1367 { return this->function_; }
1369 // Index in the list of function results.
1372 { return this->index_; }
1374 // Whether this variable's address is taken.
1376 is_address_taken() const
1377 { return this->is_address_taken_; }
1379 // Note that something takes the address of this variable.
1382 { this->is_address_taken_ = true; }
1384 // Whether this variable should live in the heap.
1387 { return this->is_address_taken_; }
1389 // Set the function. This is used when cloning functions which call
1392 set_function(Function* function)
1393 { this->function_ = function; }
1396 // Type of result variable.
1398 // Function with which this is associated.
1399 Function* function_;
1400 // Index in list of results.
1402 // Whether something takes the address of this variable.
1403 bool is_address_taken_;
1406 // The value we keep for a named constant. This lets us hold a type
1407 // and an expression.
1409 class Named_constant
1412 Named_constant(Type* type, Expression* expr, int iota_value,
1413 source_location location)
1414 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1420 { return this->type_; }
1424 { return this->expr_; }
1428 { return this->iota_value_; }
1432 { return this->location_; }
1434 // Whether we are lowering.
1437 { return this->lowering_; }
1439 // Set that we are lowering.
1442 { this->lowering_ = true; }
1444 // We are no longer lowering.
1447 { this->lowering_ = false; }
1449 // Traverse the expression.
1451 traverse_expression(Traverse*);
1453 // Determine the type of the constant if necessary.
1457 // Indicate that we found and reported an error for this constant.
1461 // Export the constant.
1463 export_const(Export*, const std::string& name) const;
1465 // Import a constant.
1467 import_const(Import*, std::string*, Type**, Expression**);
1470 // The type of the constant.
1472 // The expression for the constant.
1474 // If the predeclared constant iota is used in EXPR_, this is the
1475 // value it will have. We do this because at parse time we don't
1476 // know whether the name "iota" will refer to the predeclared
1477 // constant or to something else. We put in the right value in when
1480 // The location of the definition.
1481 source_location location_;
1482 // Whether we are currently lowering this constant.
1486 // A type declaration.
1488 class Type_declaration
1491 Type_declaration(source_location location)
1492 : location_(location), in_function_(NULL), methods_(),
1493 issued_warning_(false)
1496 // Return the location.
1499 { return this->location_; }
1501 // Return the function in which this type is declared. This will
1502 // return NULL for a type declared in global scope.
1505 { return this->in_function_; }
1507 // Set the function in which this type is declared.
1509 set_in_function(Named_object* f)
1510 { this->in_function_ = f; }
1512 // Add a method to this type. This is used when methods are defined
1515 add_method(const std::string& name, Function* function);
1517 // Add a method declaration to this type.
1519 add_method_declaration(const std::string& name, Function_type* type,
1520 source_location location);
1522 // Return whether any methods were defined.
1524 has_methods() const;
1526 // Define methods when the real type is known.
1528 define_methods(Named_type*);
1530 // This is called if we are trying to use this type. It returns
1531 // true if we should issue a warning.
1536 typedef std::vector<Named_object*> Methods;
1538 // The location of the type declaration.
1539 source_location location_;
1540 // If this type is declared in a function, a pointer back to the
1541 // function in which it is defined.
1542 Named_object* in_function_;
1543 // Methods defined before the type is defined.
1545 // True if we have issued a warning about a use of this type
1546 // declaration when it is undefined.
1547 bool issued_warning_;
1550 // An unknown object. These are created by the parser for forward
1551 // references to names which have not been seen before. In a correct
1552 // program, these will always point to a real definition by the end of
1553 // the parse. Because they point to another Named_object, these may
1554 // only be referenced by Unknown_expression objects.
1559 Unknown_name(source_location location)
1560 : location_(location), real_named_object_(NULL)
1563 // Return the location where this name was first seen.
1566 { return this->location_; }
1568 // Return the real named object that this points to, or NULL if it
1569 // was never resolved.
1571 real_named_object() const
1572 { return this->real_named_object_; }
1574 // Set the real named object that this points to.
1576 set_real_named_object(Named_object* no);
1579 // The location where this name was first seen.
1580 source_location location_;
1581 // The real named object when it is known.
1586 // A named object named. This is the result of a declaration. We
1587 // don't use a superclass because they all have to be handled
1595 // An uninitialized Named_object. We should never see this.
1596 NAMED_OBJECT_UNINITIALIZED,
1597 // An unknown name. This is used for forward references. In a
1598 // correct program, these will all be resolved by the end of the
1600 NAMED_OBJECT_UNKNOWN,
1605 // A forward type declaration.
1606 NAMED_OBJECT_TYPE_DECLARATION,
1609 // A result variable in a function.
1610 NAMED_OBJECT_RESULT_VAR,
1611 // The blank identifier--the special variable named _.
1615 // A forward func declaration.
1616 NAMED_OBJECT_FUNC_DECLARATION,
1618 NAMED_OBJECT_PACKAGE
1621 // Return the classification.
1623 classification() const
1624 { return this->classification_; }
1630 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1634 { return this->classification_ == NAMED_OBJECT_CONST; }
1638 { return this->classification_ == NAMED_OBJECT_TYPE; }
1641 is_type_declaration() const
1642 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1646 { return this->classification_ == NAMED_OBJECT_VAR; }
1649 is_result_variable() const
1650 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1654 { return this->classification_ == NAMED_OBJECT_SINK; }
1658 { return this->classification_ == NAMED_OBJECT_FUNC; }
1661 is_function_declaration() const
1662 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1666 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1670 static Named_object*
1671 make_unknown_name(const std::string& name, source_location);
1673 static Named_object*
1674 make_constant(const Typed_identifier&, const Package*, Expression*,
1677 static Named_object*
1678 make_type(const std::string&, const Package*, Type*, source_location);
1680 static Named_object*
1681 make_type_declaration(const std::string&, const Package*, source_location);
1683 static Named_object*
1684 make_variable(const std::string&, const Package*, Variable*);
1686 static Named_object*
1687 make_result_variable(const std::string&, Result_variable*);
1689 static Named_object*
1692 static Named_object*
1693 make_function(const std::string&, const Package*, Function*);
1695 static Named_object*
1696 make_function_declaration(const std::string&, const Package*, Function_type*,
1699 static Named_object*
1700 make_package(const std::string& alias, Package* package);
1707 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1708 return this->u_.unknown_value;
1712 unknown_value() const
1714 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1715 return this->u_.unknown_value;
1721 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1722 return this->u_.const_value;
1725 const Named_constant*
1728 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1729 return this->u_.const_value;
1735 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1736 return this->u_.type_value;
1742 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1743 return this->u_.type_value;
1747 type_declaration_value()
1749 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1750 return this->u_.type_declaration;
1753 const Type_declaration*
1754 type_declaration_value() const
1756 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1757 return this->u_.type_declaration;
1763 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1764 return this->u_.var_value;
1770 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1771 return this->u_.var_value;
1777 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1778 return this->u_.result_var_value;
1781 const Result_variable*
1782 result_var_value() const
1784 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1785 return this->u_.result_var_value;
1791 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1792 return this->u_.func_value;
1798 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1799 return this->u_.func_value;
1802 Function_declaration*
1803 func_declaration_value()
1805 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1806 return this->u_.func_declaration_value;
1809 const Function_declaration*
1810 func_declaration_value() const
1812 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1813 return this->u_.func_declaration_value;
1819 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1820 return this->u_.package_value;
1824 package_value() const
1826 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1827 return this->u_.package_value;
1832 { return this->name_; }
1834 // Return the name to use in an error message. The difference is
1835 // that if this Named_object is defined in a different package, this
1836 // will return PACKAGE.NAME.
1838 message_name() const;
1842 { return this->package_; }
1844 // Resolve an unknown value if possible. This returns the same
1845 // Named_object or a new one.
1849 Named_object* ret = this;
1850 if (this->is_unknown())
1852 Named_object* r = this->unknown_value()->real_named_object();
1862 const Named_object* ret = this;
1863 if (this->is_unknown())
1865 const Named_object* r = this->unknown_value()->real_named_object();
1872 // The location where this object was defined or referenced.
1876 // Return a tree for the external identifier for this object.
1880 // Return a tree representing this object.
1882 get_tree(Gogo*, Named_object* function);
1884 // Define a type declaration.
1886 set_type_value(Named_type*);
1888 // Define a function declaration.
1890 set_function_value(Function*);
1892 // Declare an unknown name as a type declaration.
1896 // Export this object.
1898 export_named_object(Export*) const;
1901 Named_object(const std::string&, const Package*, Classification);
1903 // The name of the object.
1905 // The package that this object is in. This is NULL if it is in the
1906 // file we are compiling.
1907 const Package* package_;
1908 // The type of object this is.
1909 Classification classification_;
1913 Unknown_name* unknown_value;
1914 Named_constant* const_value;
1915 Named_type* type_value;
1916 Type_declaration* type_declaration;
1917 Variable* var_value;
1918 Result_variable* result_var_value;
1919 Function* func_value;
1920 Function_declaration* func_declaration_value;
1921 Package* package_value;
1923 // The DECL tree for this object if we have already converted it.
1927 // A binding contour. This binds names to objects.
1932 // Type for mapping from names to objects.
1933 typedef Unordered_map(std::string, Named_object*) Contour;
1935 Bindings(Bindings* enclosing);
1937 // Add an unknown name.
1939 add_unknown_name(const std::string& name, source_location location)
1941 return this->add_named_object(Named_object::make_unknown_name(name,
1947 add_constant(const Typed_identifier& tid, const Package* package,
1948 Expression* expr, int iota_value)
1950 return this->add_named_object(Named_object::make_constant(tid, package,
1957 add_type(const std::string& name, const Package* package, Type* type,
1958 source_location location)
1960 return this->add_named_object(Named_object::make_type(name, package, type,
1964 // Add a named type. This is used for builtin types, and to add an
1965 // imported type to the global scope.
1967 add_named_type(Named_type* named_type);
1969 // Add a type declaration.
1971 add_type_declaration(const std::string& name, const Package* package,
1972 source_location location)
1974 Named_object* no = Named_object::make_type_declaration(name, package,
1976 return this->add_named_object(no);
1981 add_variable(const std::string& name, const Package* package,
1984 return this->add_named_object(Named_object::make_variable(name, package,
1988 // Add a result variable.
1990 add_result_variable(const std::string& name, Result_variable* result)
1992 return this->add_named_object(Named_object::make_result_variable(name,
1998 add_function(const std::string& name, const Package*, Function* function);
2000 // Add a function declaration.
2002 add_function_declaration(const std::string& name, const Package* package,
2003 Function_type* type, source_location location);
2005 // Add a package. The location is the location of the import
2008 add_package(const std::string& alias, Package* package)
2010 Named_object* no = Named_object::make_package(alias, package);
2011 return this->add_named_object(no);
2014 // Define a type which was already declared.
2016 define_type(Named_object*, Named_type*);
2018 // Add a method to the list of objects. This is not added to the
2021 add_method(Named_object*);
2023 // Add a named object to this binding.
2025 add_named_object(Named_object* no)
2026 { return this->add_named_object_to_contour(&this->bindings_, no); }
2028 // Clear all names in file scope from the bindings.
2032 // Look up a name in this binding contour and in any enclosing
2033 // binding contours. This returns NULL if the name is not found.
2035 lookup(const std::string&) const;
2037 // Look up a name in this binding contour without looking in any
2038 // enclosing binding contours. Returns NULL if the name is not found.
2040 lookup_local(const std::string&) const;
2044 remove_binding(Named_object*);
2046 // Traverse the tree. See the Traverse class.
2048 traverse(Traverse*, bool is_global);
2050 // Iterate over definitions. This does not include things which
2051 // were only declared.
2053 typedef std::vector<Named_object*>::const_iterator
2054 const_definitions_iterator;
2056 const_definitions_iterator
2057 begin_definitions() const
2058 { return this->named_objects_.begin(); }
2060 const_definitions_iterator
2061 end_definitions() const
2062 { return this->named_objects_.end(); }
2064 // Return the number of definitions.
2066 size_definitions() const
2067 { return this->named_objects_.size(); }
2069 // Return whether there are no definitions.
2071 empty_definitions() const
2072 { return this->named_objects_.empty(); }
2074 // Iterate over declarations. This is everything that has been
2075 // declared, which includes everything which has been defined.
2077 typedef Contour::const_iterator const_declarations_iterator;
2079 const_declarations_iterator
2080 begin_declarations() const
2081 { return this->bindings_.begin(); }
2083 const_declarations_iterator
2084 end_declarations() const
2085 { return this->bindings_.end(); }
2087 // Return the number of declarations.
2089 size_declarations() const
2090 { return this->bindings_.size(); }
2092 // Return whether there are no declarations.
2094 empty_declarations() const
2095 { return this->bindings_.empty(); }
2097 // Return the first declaration.
2100 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2104 add_named_object_to_contour(Contour*, Named_object*);
2107 new_definition(Named_object*, Named_object*);
2109 // Enclosing bindings.
2110 Bindings* enclosing_;
2111 // The list of objects.
2112 std::vector<Named_object*> named_objects_;
2113 // The mapping from names to objects.
2122 Label(const std::string& name)
2123 : name_(name), location_(0), is_used_(false), blabel_(NULL)
2126 // Return the label's name.
2129 { return this->name_; }
2131 // Return whether the label has been defined.
2134 { return this->location_ != 0; }
2136 // Return whether the label has been used.
2139 { return this->is_used_; }
2141 // Record that the label is used.
2144 { this->is_used_ = true; }
2146 // Return the location of the definition.
2149 { return this->location_; }
2151 // Define the label at LOCATION.
2153 define(source_location location)
2155 gcc_assert(this->location_ == 0);
2156 this->location_ = location;
2159 // Return the backend representation for this label.
2161 get_backend_label(Translate_context*);
2163 // Return an expression for the address of this label. This is used
2164 // to get the return address of a deferred function to see whether
2165 // the function may call recover.
2167 get_addr(Translate_context*, source_location location);
2170 // The name of the label.
2172 // The location of the definition. This is 0 if the label has not
2173 // yet been defined.
2174 source_location location_;
2175 // Whether the label has been used.
2177 // The backend representation.
2181 // An unnamed label. These are used when lowering loops.
2186 Unnamed_label(source_location location)
2187 : location_(location), blabel_(NULL)
2190 // Get the location where the label is defined.
2193 { return this->location_; }
2195 // Set the location where the label is defined.
2197 set_location(source_location location)
2198 { this->location_ = location; }
2200 // Return a statement which defines this label.
2202 get_definition(Translate_context*);
2204 // Return a goto to this label from LOCATION.
2206 get_goto(Translate_context*, source_location location);
2209 // Return the backend representation.
2211 get_blabel(Translate_context*);
2213 // The location where the label is defined.
2214 source_location location_;
2215 // The backend representation of this label.
2219 // An imported package.
2224 Package(const std::string& name, const std::string& unique_prefix,
2225 source_location location);
2227 // The real name of this package. This may be different from the
2228 // name in the associated Named_object if the import statement used
2232 { return this->name_; }
2234 // Return the location of the import statement.
2237 { return this->location_; }
2239 // Get the unique prefix used for all symbols exported from this
2242 unique_prefix() const
2244 gcc_assert(!this->unique_prefix_.empty());
2245 return this->unique_prefix_;
2248 // The priority of this package. The init function of packages with
2249 // lower priority must be run before the init function of packages
2250 // with higher priority.
2253 { return this->priority_; }
2255 // Set the priority.
2257 set_priority(int priority);
2259 // Return the bindings.
2262 { return this->bindings_; }
2264 // Whether some symbol from the package was used.
2267 { return this->used_; }
2269 // Note that some symbol from this package was used.
2272 { this->used_ = true; }
2274 // Clear the used field for the next file.
2277 { this->used_ = false; }
2279 // Whether this package was imported in the current file.
2282 { return this->is_imported_; }
2284 // Note that this package was imported in the current file.
2287 { this->is_imported_ = true; }
2289 // Clear the imported field for the next file.
2292 { this->is_imported_ = false; }
2294 // Whether this package was imported with a name of "_".
2296 uses_sink_alias() const
2297 { return this->uses_sink_alias_; }
2299 // Note that this package was imported with a name of "_".
2301 set_uses_sink_alias()
2302 { this->uses_sink_alias_ = true; }
2304 // Clear the sink alias field for the next file.
2306 clear_uses_sink_alias()
2307 { this->uses_sink_alias_ = false; }
2309 // Look up a name in the package. Returns NULL if the name is not
2312 lookup(const std::string& name) const
2313 { return this->bindings_->lookup(name); }
2315 // Set the location of the package. This is used if it is seen in a
2316 // different import before it is really imported.
2318 set_location(source_location location)
2319 { this->location_ = location; }
2321 // Add a constant to the package.
2323 add_constant(const Typed_identifier& tid, Expression* expr)
2324 { return this->bindings_->add_constant(tid, this, expr, 0); }
2326 // Add a type to the package.
2328 add_type(const std::string& name, Type* type, source_location location)
2329 { return this->bindings_->add_type(name, this, type, location); }
2331 // Add a type declaration to the package.
2333 add_type_declaration(const std::string& name, source_location location)
2334 { return this->bindings_->add_type_declaration(name, this, location); }
2336 // Add a variable to the package.
2338 add_variable(const std::string& name, Variable* variable)
2339 { return this->bindings_->add_variable(name, this, variable); }
2341 // Add a function declaration to the package.
2343 add_function_declaration(const std::string& name, Function_type* type,
2344 source_location loc)
2345 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2347 // Determine types of constants.
2352 // The real name of this package.
2354 // The unique prefix for all exported global symbols.
2355 std::string unique_prefix_;
2356 // The names in this package.
2357 Bindings* bindings_;
2358 // The priority of this package. A package has a priority higher
2359 // than the priority of all of the packages that it imports. This
2360 // is used to run init functions in the right order.
2362 // The location of the import statement.
2363 source_location location_;
2364 // True if some name from this package was used. This is mutable
2365 // because we can use a package even if we have a const pointer to
2368 // True if this package was imported in the current file.
2370 // True if this package was imported with a name of "_".
2371 bool uses_sink_alias_;
2374 // Return codes for the traversal functions. This is not an enum
2375 // because we want to be able to declare traversal functions in other
2376 // header files without including this one.
2378 // Continue traversal as usual.
2379 const int TRAVERSE_CONTINUE = -1;
2382 const int TRAVERSE_EXIT = 0;
2384 // Continue traversal, but skip components of the current object.
2385 // E.g., if this is returned by Traverse::statement, we do not
2386 // traverse the expressions in the statement even if
2387 // traverse_expressions is set in the traverse_mask.
2388 const int TRAVERSE_SKIP_COMPONENTS = 1;
2390 // This class is used when traversing the parse tree. The caller uses
2391 // a subclass which overrides functions as desired.
2396 // These bitmasks say what to traverse.
2397 static const unsigned int traverse_variables = 0x1;
2398 static const unsigned int traverse_constants = 0x2;
2399 static const unsigned int traverse_functions = 0x4;
2400 static const unsigned int traverse_blocks = 0x8;
2401 static const unsigned int traverse_statements = 0x10;
2402 static const unsigned int traverse_expressions = 0x20;
2403 static const unsigned int traverse_types = 0x40;
2405 Traverse(unsigned int traverse_mask)
2406 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2409 virtual ~Traverse();
2411 // The bitmask of what to traverse.
2413 traverse_mask() const
2414 { return this->traverse_mask_; }
2416 // Record that we are going to traverse a type. This returns true
2417 // if the type has already been seen in this traversal. This is
2418 // required because types, unlike expressions, can form a circular
2421 remember_type(const Type*);
2423 // Record that we are going to see an expression. This returns true
2424 // if the expression has already been seen in this traversal. This
2425 // is only needed for cases where multiple expressions can point to
2428 remember_expression(const Expression*);
2430 // These functions return one of the TRAVERSE codes defined above.
2432 // If traverse_variables is set in the mask, this is called for
2433 // every variable in the tree.
2435 variable(Named_object*);
2437 // If traverse_constants is set in the mask, this is called for
2438 // every named constant in the tree. The bool parameter is true for
2439 // a global constant.
2441 constant(Named_object*, bool);
2443 // If traverse_functions is set in the mask, this is called for
2444 // every function in the tree.
2446 function(Named_object*);
2448 // If traverse_blocks is set in the mask, this is called for every
2449 // block in the tree.
2453 // If traverse_statements is set in the mask, this is called for
2454 // every statement in the tree.
2456 statement(Block*, size_t* index, Statement*);
2458 // If traverse_expressions is set in the mask, this is called for
2459 // every expression in the tree.
2461 expression(Expression**);
2463 // If traverse_types is set in the mask, this is called for every
2464 // type in the tree.
2469 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2470 Type_identical) Types_seen;
2472 typedef Unordered_set(const Expression*) Expressions_seen;
2474 // Bitmask of what sort of objects to traverse.
2475 unsigned int traverse_mask_;
2476 // Types which have been seen in this traversal.
2477 Types_seen* types_seen_;
2478 // Expressions which have been seen in this traversal.
2479 Expressions_seen* expressions_seen_;
2482 // When translating the gogo IR into the backend data structure, this
2483 // is the context we pass down the blocks and statements.
2485 class Translate_context
2488 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2490 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2491 block_(block), block_tree_(block_tree), is_const_(false)
2498 { return this->gogo_; }
2502 { return this->backend_; }
2506 { return this->function_; }
2510 { return this->block_; }
2514 { return this->block_tree_; }
2518 { return this->is_const_; }
2520 // Make a constant context.
2523 { this->is_const_ = true; }
2526 // The IR for the entire compilation unit.
2528 // The generator for the backend data structures.
2530 // The function we are currently translating.
2531 Named_object* function_;
2532 // The block we are currently translating.
2534 // The BLOCK node for the current block.
2536 // Whether this is being evaluated in a constant context. This is
2537 // used for type descriptor initializers.
2541 // Runtime error codes. These must match the values in
2542 // libgo/runtime/go-runtime-error.c.
2544 // Slice index out of bounds: negative or larger than the length of
2546 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2548 // Array index out of bounds.
2549 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2551 // String index out of bounds.
2552 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2554 // Slice slice out of bounds: negative or larger than the length of
2555 // the slice or high bound less than low bound.
2556 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2558 // Array slice out of bounds.
2559 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2561 // String slice out of bounds.
2562 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2564 // Dereference of nil pointer. This is used when there is a
2565 // dereference of a pointer to a very large struct or array, to ensure
2566 // that a gigantic array is not used a proxy to access random memory
2568 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2570 // Slice length or capacity out of bounds in make: negative or
2571 // overflow or length greater than capacity.
2572 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2574 // Map capacity out of bounds in make: negative or overflow.
2575 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2577 // Channel capacity out of bounds in make: negative or overflow.
2578 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2580 // This is used by some of the langhooks.
2581 extern Gogo* go_get_gogo();
2583 // Whether we have seen any errors. FIXME: Replace with a backend
2585 extern bool saw_errors();
2587 #endif // !defined(GO_GOGO_H)