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.
11 class Statement_inserter;
13 class Type_hash_identical;
16 class Typed_identifier;
17 class Typed_identifier_list;
21 class Temporary_statement;
30 class Struct_field_list;
36 class Forward_declaration_type;
41 class Translate_context;
51 // This file declares the basic classes used to hold the internal
52 // representation of Go which is built by the parser.
54 // An initialization function for an imported package. This is a
55 // magic function which initializes variables and runs the "init"
61 Import_init(const std::string& package_name, const std::string& init_name,
63 : package_name_(package_name), init_name_(init_name), priority_(priority)
66 // The name of the package being imported.
69 { return this->package_name_; }
71 // The name of the package's init function.
74 { return this->init_name_; }
76 // The priority of the initialization function. Functions with a
77 // lower priority number must be run first.
80 { return this->priority_; }
83 // The name of the package being imported.
84 std::string package_name_;
85 // The name of the package's init function.
86 std::string init_name_;
91 // For sorting purposes.
94 operator<(const Import_init& i1, const Import_init& i2)
96 if (i1.priority() < i2.priority())
98 if (i1.priority() > i2.priority())
100 if (i1.package_name() != i2.package_name())
101 return i1.package_name() < i2.package_name();
102 return i1.init_name() < i2.init_name();
105 // The holder for the internal representation of the entire
111 // Create the IR, passing in the sizes of the types "int" and
112 // "uintptr" in bits.
113 Gogo(Backend* backend, int int_type_size, int pointer_size);
115 // Get the backend generator.
118 { return this->backend_; }
120 // Get the package name.
122 package_name() const;
124 // Set the package name.
126 set_package_name(const std::string&, source_location);
128 // Return whether this is the "main" package.
130 is_main_package() const;
132 // If necessary, adjust the name to use for a hidden symbol. We add
133 // a prefix of the package name, so that hidden symbols in different
134 // packages do not collide.
136 pack_hidden_name(const std::string& name, bool is_exported) const
140 : ('.' + this->unique_prefix()
141 + '.' + this->package_name()
145 // Unpack a name which may have been hidden. Returns the
146 // user-visible name of the object.
148 unpack_hidden_name(const std::string& name)
149 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
151 // Return whether a possibly packed name is hidden.
153 is_hidden_name(const std::string& name)
154 { return name[0] == '.'; }
156 // Return the package prefix of a hidden name.
158 hidden_name_prefix(const std::string& name)
160 go_assert(Gogo::is_hidden_name(name));
161 return name.substr(1, name.rfind('.') - 1);
164 // Given a name which may or may not have been hidden, return the
165 // name to use in an error message.
167 message_name(const std::string& name);
169 // Return whether a name is the blank identifier _.
171 is_sink_name(const std::string& name)
173 return (name[0] == '.'
174 && name[name.length() - 1] == '_'
175 && name[name.length() - 2] == '.');
178 // Return the unique prefix to use for all exported symbols.
180 unique_prefix() const;
182 // Set the unique prefix.
184 set_unique_prefix(const std::string&);
186 // Return the priority to use for the package we are compiling.
187 // This is two more than the largest priority of any package we
190 package_priority() const;
192 // Import a package. FILENAME is the file name argument, LOCAL_NAME
193 // is the local name to give to the package. If LOCAL_NAME is empty
194 // the declarations are added to the global scope.
196 import_package(const std::string& filename, const std::string& local_name,
197 bool is_local_name_exported, source_location);
199 // Whether we are the global binding level.
201 in_global_scope() const;
203 // Look up a name in the current binding contours.
205 lookup(const std::string&, Named_object** pfunction) const;
207 // Look up a name in the current block.
209 lookup_in_block(const std::string&) const;
211 // Look up a name in the global namespace--the universal scope.
213 lookup_global(const char*) const;
215 // Add a new imported package. REAL_NAME is the real name of the
216 // package. ALIAS is the alias of the package; this may be the same
217 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
218 // this package should be added to the global namespace; this is
219 // true if the alias is ".". LOCATION is the location of the import
220 // statement. This returns the new package, or NULL on error.
222 add_imported_package(const std::string& real_name, const std::string& alias,
223 bool is_alias_exported,
224 const std::string& unique_prefix,
225 source_location location,
226 bool* padd_to_globals);
228 // Register a package. This package may or may not be imported.
229 // This returns the Package structure for the package, creating if
232 register_package(const std::string& name, const std::string& unique_prefix,
235 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
236 // method function should be added to the type of its receiver.
238 start_function(const std::string& name, Function_type* type,
239 bool add_method_to_type, source_location);
241 // Finish compiling a function.
243 finish_function(source_location);
245 // Return the current function.
247 current_function() const;
249 // Start a new block. This is not initially associated with a
252 start_block(source_location);
254 // Finish the current block and return it.
256 finish_block(source_location);
258 // Declare an unknown name. This is used while parsing. The name
259 // must be resolved by the end of the parse. Unknown names are
260 // always added at the package level.
262 add_unknown_name(const std::string& name, source_location);
264 // Declare a function.
266 declare_function(const std::string&, Function_type*, source_location);
270 add_label_definition(const std::string&, source_location);
272 // Add a label reference.
274 add_label_reference(const std::string&);
276 // Add a statement to the current block.
278 add_statement(Statement*);
280 // Add a block to the current block.
282 add_block(Block*, source_location);
286 add_constant(const Typed_identifier&, Expression*, int iota_value);
290 add_type(const std::string&, Type*, source_location);
292 // Add a named type. This is used for builtin types, and to add an
293 // imported type to the global scope.
295 add_named_type(Named_type*);
299 declare_type(const std::string&, source_location);
301 // Declare a type at the package level. This is used when the
302 // parser sees an unknown name where a type name is required.
304 declare_package_type(const std::string&, source_location);
306 // Define a type which was already declared.
308 define_type(Named_object*, Named_type*);
312 add_variable(const std::string&, Variable*);
314 // Add a sink--a reference to the blank identifier _.
318 // Add a named object to the current namespace. This is used for
319 // import . "package".
321 add_named_object(Named_object*);
323 // Return a name to use for a thunk function. A thunk function is
324 // one we create during the compilation, for a go statement or a
325 // defer statement or a method expression.
329 // Return whether an object is a thunk.
331 is_thunk(const Named_object*);
333 // Note that we've seen an interface type. This is used to build
334 // all required interface method tables.
336 record_interface_type(Interface_type*);
338 // Note that we need an initialization function.
341 { this->need_init_fn_ = true; }
343 // Clear out all names in file scope. This is called when we start
344 // parsing a new file.
348 // Traverse the tree. See the Traverse class.
352 // Define the predeclared global names.
354 define_global_names();
356 // Verify and complete all types.
360 // Lower the parse tree.
364 // Lower all the statements in a block.
366 lower_block(Named_object* function, Block*);
368 // Lower an expression.
370 lower_expression(Named_object* function, Statement_inserter*, Expression**);
374 lower_constant(Named_object*);
376 // Finalize the method lists and build stub methods for named types.
380 // Work out the types to use for unspecified variables and
385 // Type check the program.
389 // Check the types in a single block. This is used for complicated
392 check_types_in_block(Block*);
394 // Check for return statements.
396 check_return_statements();
402 // Add an import control function for an imported package to the
405 add_import_init_fn(const std::string& package_name,
406 const std::string& init_name, int prio);
408 // Turn short-cut operators (&&, ||) into explicit if statements.
412 // Use temporary variables to force order of evaluation.
416 // Build thunks for functions which call recover.
418 build_recover_thunks();
420 // Simplify statements which might use thunks: go and defer
423 simplify_thunk_statements();
425 // Dump AST if -fgo-dump-ast is set
427 dump_ast(const char* basename);
429 // Convert named types to the backend representation.
431 convert_named_types();
433 // Convert named types in a list of bindings.
435 convert_named_types_in_bindings(Bindings*);
437 // True if named types have been converted to the backend
440 named_types_are_converted() const
441 { return this->named_types_are_converted_; }
443 // Write out the global values.
447 // Create trees for implicit builtin functions.
449 define_builtin_function_trees();
451 // Build a call to a builtin function. PDECL should point to a NULL
452 // initialized static pointer which will hold the fndecl. NAME is
453 // the name of the function. NARGS is the number of arguments.
454 // RETTYPE is the return type. It is followed by NARGS pairs of
455 // type and argument (both trees).
457 call_builtin(tree* pdecl, source_location, const char* name, int nargs,
460 // Build a call to the runtime error function.
462 runtime_error(int code, source_location);
464 // Build a builtin struct with a list of fields.
466 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
469 // Mark a function declaration as a builtin library function.
471 mark_fndecl_as_builtin_library(tree fndecl);
473 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
474 // the slice. VALUES points to the values. COUNT is the size,
475 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
478 slice_constructor(tree slice_type_tree, tree values, tree count,
481 // Build required interface method tables.
483 build_interface_method_tables();
485 // Build an interface method table for a type: a list of function
486 // pointers, one for each interface method. This returns a decl.
488 interface_method_table_for_type(const Interface_type*, Named_type*,
491 // Return a tree which allocate SIZE bytes to hold values of type
494 allocate_memory(Type *type, tree size, source_location);
496 // Return a type to use for pointer to const char.
498 const_char_pointer_type_tree();
500 // Build a string constant with the right type.
502 string_constant_tree(const std::string&);
504 // Build a Go string constant. This returns a pointer to the
507 go_string_constant_tree(const std::string&);
509 // Receive a value from a channel.
511 receive_from_channel(tree type_tree, tree channel, bool for_select,
514 // Return a tree for receiving an integer on a channel.
516 receive_as_64bit_integer(tree type, tree channel, bool blocking,
519 // Make a trampoline which calls FNADDR passing CLOSURE.
521 make_trampoline(tree fnaddr, tree closure, source_location);
524 // During parsing, we keep a stack of functions. Each function on
525 // the stack is one that we are currently parsing. For each
526 // function, we keep track of the current stack of blocks.
530 Named_object* function;
531 // The stack of active blocks in the function.
532 std::vector<Block*> blocks;
535 // The stack of functions.
536 typedef std::vector<Open_function> Open_functions;
538 // Set up the built-in unsafe package.
540 import_unsafe(const std::string&, bool is_exported, source_location);
542 // Add a new imported package.
544 add_package(const std::string& real_name, const std::string& alias,
545 const std::string& unique_prefix, source_location location);
547 // Return the current binding contour.
552 current_bindings() const;
554 // Return the current block.
558 // Get the name of the magic initialization function.
562 // Get the decl for the magic initialization function.
564 initialization_function_decl();
566 // Write the magic initialization function.
568 write_initialization_function(tree fndecl, tree init_stmt_list);
570 // Initialize imported packages.
574 // Register variables with the garbage collector.
576 register_gc_vars(const std::vector<Named_object*>&, tree*);
578 // Build a pointer to a Go string constant. This returns a pointer
581 ptr_go_string_constant_tree(const std::string&);
583 // Return the type of a trampoline.
585 trampoline_type_tree();
587 // Type used to map import names to packages.
588 typedef std::map<std::string, Package*> Imports;
590 // Type used to map package names to packages.
591 typedef std::map<std::string, Package*> Packages;
593 // Type used to map special names in the sys package.
594 typedef std::map<std::string, std::string> Sys_names;
596 // The backend generator.
598 // The package we are compiling.
600 // The list of currently open functions during parsing.
601 Open_functions functions_;
602 // The global binding contour. This includes the builtin functions
603 // and the package we are compiling.
605 // Mapping from import file names to packages.
607 // Whether the magic unsafe package was imported.
608 bool imported_unsafe_;
609 // Mapping from package names we have seen to packages. This does
610 // not include the package we are compiling.
612 // The functions named "init", if there are any.
613 std::vector<Named_object*> init_functions_;
614 // Whether we need a magic initialization function.
616 // The name of the magic initialization function.
617 std::string init_fn_name_;
618 // A list of import control variables for packages that we import.
619 std::set<Import_init> imported_init_fns_;
620 // The unique prefix used for all global symbols.
621 std::string unique_prefix_;
622 // Whether an explicit unique prefix was set by -fgo-prefix.
623 bool unique_prefix_specified_;
624 // A list of interface types defined while parsing.
625 std::vector<Interface_type*> interface_types_;
626 // Whether named types have been converted.
627 bool named_types_are_converted_;
630 // A block of statements.
635 Block(Block* enclosing, source_location);
637 // Return the enclosing block.
640 { return this->enclosing_; }
642 // Return the bindings of the block.
645 { return this->bindings_; }
649 { return this->bindings_; }
651 // Look at the block's statements.
652 const std::vector<Statement*>*
654 { return &this->statements_; }
656 // Return the start location. This is normally the location of the
657 // left curly brace which starts the block.
659 start_location() const
660 { return this->start_location_; }
662 // Return the end location. This is normally the location of the
663 // right curly brace which ends the block.
666 { return this->end_location_; }
668 // Add a statement to the block.
670 add_statement(Statement*);
672 // Add a statement to the front of the block.
674 add_statement_at_front(Statement*);
676 // Replace a statement in a block.
678 replace_statement(size_t index, Statement*);
680 // Add a Statement before statement number INDEX.
682 insert_statement_before(size_t index, Statement*);
684 // Add a Statement after statement number INDEX.
686 insert_statement_after(size_t index, Statement*);
688 // Set the end location of the block.
690 set_end_location(source_location location)
691 { this->end_location_ = location; }
693 // Traverse the tree.
697 // Set final types for unspecified variables and constants.
701 // Return true if execution of this block may fall through to the
704 may_fall_through() const;
706 // Convert the block to the backend representation.
708 get_backend(Translate_context*);
710 // Iterate over statements.
712 typedef std::vector<Statement*>::iterator iterator;
716 { return this->statements_.begin(); }
720 { return this->statements_.end(); }
725 // Statements in the block.
726 std::vector<Statement*> statements_;
729 // Location of start of block.
730 source_location start_location_;
731 // Location of end of block.
732 source_location end_location_;
740 Function(Function_type* type, Function*, Block*, source_location);
742 // Return the function's type.
745 { return this->type_; }
747 // Return the enclosing function if there is one.
750 { return this->enclosing_; }
752 // Set the enclosing function. This is used when building thunks
753 // for functions which call recover.
755 set_enclosing(Function* enclosing)
757 go_assert(this->enclosing_ == NULL);
758 this->enclosing_ = enclosing;
761 // The result variables.
762 typedef std::vector<Named_object*> Results;
764 // Create the result variables in the outer block.
766 create_result_variables(Gogo*);
768 // Update the named result variables when cloning a function which
771 update_result_variables();
773 // Return the result variables.
776 { return this->results_; }
778 // Whether the result variables have names.
780 results_are_named() const
781 { return this->results_are_named_; }
783 // Add a new field to the closure variable.
785 add_closure_field(Named_object* var, source_location loc)
786 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
788 // Whether this function needs a closure.
790 needs_closure() const
791 { return !this->closure_fields_.empty(); }
793 // Return the closure variable, creating it if necessary. This is
794 // passed to the function as a static chain parameter.
798 // Set the closure variable. This is used when building thunks for
799 // functions which call recover.
801 set_closure_var(Named_object* v)
803 go_assert(this->closure_var_ == NULL);
804 this->closure_var_ = v;
807 // Return the variable for a reference to field INDEX in the closure
810 enclosing_var(unsigned int index)
812 go_assert(index < this->closure_fields_.size());
813 return closure_fields_[index].first;
816 // Set the type of the closure variable if there is one.
820 // Get the block of statements associated with the function.
823 { return this->block_; }
825 // Get the location of the start of the function.
828 { return this->location_; }
830 // Return whether this function is actually a method.
834 // Add a label definition to the function.
836 add_label_definition(const std::string& label_name, source_location);
838 // Add a label reference to a function.
840 add_label_reference(const std::string& label_name);
842 // Warn about labels that are defined but not used.
844 check_labels() const;
846 // Whether this function calls the predeclared recover function.
848 calls_recover() const
849 { return this->calls_recover_; }
851 // Record that this function calls the predeclared recover function.
852 // This is set during the lowering pass.
855 { this->calls_recover_ = true; }
857 // Whether this is a recover thunk function.
859 is_recover_thunk() const
860 { return this->is_recover_thunk_; }
862 // Record that this is a thunk built for a function which calls
865 set_is_recover_thunk()
866 { this->is_recover_thunk_ = true; }
868 // Whether this function already has a recover thunk.
870 has_recover_thunk() const
871 { return this->has_recover_thunk_; }
873 // Record that this function already has a recover thunk.
875 set_has_recover_thunk()
876 { this->has_recover_thunk_ = true; }
878 // Swap with another function. Used only for the thunk which calls
881 swap_for_recover(Function *);
883 // Traverse the tree.
887 // Determine types in the function.
891 // Return the function's decl given an identifier.
893 get_or_make_decl(Gogo*, Named_object*, tree id);
895 // Return the function's decl after it has been built.
899 go_assert(this->fndecl_ != NULL);
900 return this->fndecl_;
903 // Set the function decl to hold a tree of the function code.
905 build_tree(Gogo*, Named_object*);
907 // Get the value to return when not explicitly specified. May also
908 // add statements to execute first to STMT_LIST.
910 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
912 // Get a tree for the variable holding the defer stack.
914 defer_stack(source_location);
916 // Export the function.
918 export_func(Export*, const std::string& name) const;
920 // Export a function with a type.
922 export_func_with_type(Export*, const std::string& name,
923 const Function_type*);
925 // Import a function.
927 import_func(Import*, std::string* pname, Typed_identifier** receiver,
928 Typed_identifier_list** pparameters,
929 Typed_identifier_list** presults, bool* is_varargs);
932 // Type for mapping from label names to Label objects.
933 typedef Unordered_map(std::string, Label*) Labels;
936 make_receiver_parm_decl(Gogo*, Named_object*, tree);
939 copy_parm_to_heap(Gogo*, Named_object*, tree);
942 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
944 typedef std::vector<std::pair<Named_object*,
945 source_location> > Closure_fields;
947 // The function's type.
948 Function_type* type_;
949 // The enclosing function. This is NULL when there isn't one, which
950 // is the normal case.
951 Function* enclosing_;
952 // The result variables, if any.
954 // If there is a closure, this is the list of variables which appear
955 // in the closure. This is created by the parser, and then resolved
956 // to a real type when we lower parse trees.
957 Closure_fields closure_fields_;
958 // The closure variable, passed as a parameter using the static
959 // chain parameter. Normally NULL.
960 Named_object* closure_var_;
961 // The outer block of statements in the function.
963 // The source location of the start of the function.
964 source_location location_;
965 // Labels defined or referenced in the function.
967 // The function decl.
969 // The defer stack variable. A pointer to this variable is used to
970 // distinguish the defer stack for one function from another. This
971 // is NULL unless we actually need a defer stack.
972 Temporary_statement* defer_stack_;
973 // True if the result variables are named.
974 bool results_are_named_;
975 // True if this function calls the predeclared recover function.
977 // True if this a thunk built for a function which calls recover.
978 bool is_recover_thunk_;
979 // True if this function already has a recover thunk.
980 bool has_recover_thunk_;
983 // A function declaration.
985 class Function_declaration
988 Function_declaration(Function_type* fntype, source_location location)
989 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
994 { return this->fntype_; }
998 { return this->location_; }
1002 { return this->asm_name_; }
1004 // Set the assembler name.
1006 set_asm_name(const std::string& asm_name)
1007 { this->asm_name_ = asm_name; }
1009 // Return a decl for the function given an identifier.
1011 get_or_make_decl(Gogo*, Named_object*, tree id);
1013 // Export a function declaration.
1015 export_func(Export* exp, const std::string& name) const
1016 { Function::export_func_with_type(exp, name, this->fntype_); }
1019 // The type of the function.
1020 Function_type* fntype_;
1021 // The location of the declaration.
1022 source_location location_;
1023 // The assembler name: this is the name to use in references to the
1024 // function. This is normally empty.
1025 std::string asm_name_;
1026 // The function decl if needed.
1035 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1036 bool is_receiver, source_location);
1038 // Get the type of the variable.
1045 // Return whether the type is defined yet.
1048 { return this->type_ != NULL; }
1050 // Get the initial value.
1053 { return this->init_; }
1055 // Return whether there are any preinit statements.
1057 has_pre_init() const
1058 { return this->preinit_ != NULL; }
1060 // Return the preinit statements if any.
1063 { return this->preinit_; }
1065 // Return whether this is a global variable.
1068 { return this->is_global_; }
1070 // Return whether this is a function parameter.
1072 is_parameter() const
1073 { return this->is_parameter_; }
1075 // Return whether this is the receiver parameter of a method.
1078 { return this->is_receiver_; }
1080 // Change this parameter to be a receiver. This is used when
1081 // creating the thunks created for functions which call recover.
1085 go_assert(this->is_parameter_);
1086 this->is_receiver_ = true;
1089 // Change this parameter to not be a receiver. This is used when
1090 // creating the thunks created for functions which call recover.
1092 set_is_not_receiver()
1094 go_assert(this->is_parameter_);
1095 this->is_receiver_ = false;
1098 // Return whether this is the varargs parameter of a function.
1100 is_varargs_parameter() const
1101 { return this->is_varargs_parameter_; }
1103 // Whether this variable's address is taken.
1105 is_address_taken() const
1106 { return this->is_address_taken_; }
1108 // Whether this variable should live in the heap.
1111 { return this->is_address_taken_ && !this->is_global_; }
1113 // Note that something takes the address of this variable.
1116 { this->is_address_taken_ = true; }
1118 // Return whether the address is taken but does not escape.
1120 is_non_escaping_address_taken() const
1121 { return this->is_non_escaping_address_taken_; }
1123 // Note that something takes the address of this variable such that
1124 // the address does not escape the function.
1126 set_non_escaping_address_taken()
1127 { this->is_non_escaping_address_taken_ = true; }
1129 // Get the source location of the variable's declaration.
1132 { return this->location_; }
1134 // Record that this is the varargs parameter of a function.
1136 set_is_varargs_parameter()
1138 go_assert(this->is_parameter_);
1139 this->is_varargs_parameter_ = true;
1142 // Clear the initial value; used for error handling.
1145 { this->init_ = NULL; }
1147 // Set the initial value; used for converting shortcuts.
1149 set_init(Expression* init)
1150 { this->init_ = init; }
1152 // Get the preinit block, a block of statements to be run before the
1153 // initialization expression.
1155 preinit_block(Gogo*);
1157 // Add a statement to be run before the initialization expression.
1158 // This is only used for global variables.
1160 add_preinit_statement(Gogo*, Statement*);
1162 // Lower the initialization expression after parsing is complete.
1164 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1166 // A special case: the init value is used only to determine the
1167 // type. This is used if the variable is defined using := with the
1168 // comma-ok form of a map index or a receive expression. The init
1169 // value is actually the map index expression or receive expression.
1170 // We use this because we may not know the right type at parse time.
1172 set_type_from_init_tuple()
1173 { this->type_from_init_tuple_ = true; }
1175 // Another special case: the init value is used only to determine
1176 // the type. This is used if the variable is defined using := with
1177 // a range clause. The init value is the range expression. The
1178 // type of the variable is the index type of the range expression
1179 // (i.e., the first value returned by a range).
1181 set_type_from_range_index()
1182 { this->type_from_range_index_ = true; }
1184 // Another special case: like set_type_from_range_index, but the
1185 // type is the value type of the range expression (i.e., the second
1186 // value returned by a range).
1188 set_type_from_range_value()
1189 { this->type_from_range_value_ = true; }
1191 // Another special case: the init value is used only to determine
1192 // the type. This is used if the variable is defined using := with
1193 // a case in a select statement. The init value is the channel.
1194 // The type of the variable is the channel's element type.
1196 set_type_from_chan_element()
1197 { this->type_from_chan_element_ = true; }
1199 // After we lower the select statement, we once again set the type
1200 // from the initialization expression.
1202 clear_type_from_chan_element()
1204 go_assert(this->type_from_chan_element_);
1205 this->type_from_chan_element_ = false;
1208 // Note that this variable was created for a type switch clause.
1210 set_is_type_switch_var()
1211 { this->is_type_switch_var_ = true; }
1213 // Traverse the initializer expression.
1215 traverse_expression(Traverse*, unsigned int traverse_mask);
1217 // Determine the type of the variable if necessary.
1221 // Get the backend representation of the variable.
1223 get_backend_variable(Gogo*, Named_object*, const Package*,
1224 const std::string&);
1226 // Get the initial value of the variable as a tree. This may only
1227 // be called if has_pre_init() returns false.
1229 get_init_tree(Gogo*, Named_object* function);
1231 // Return a series of statements which sets the value of the
1232 // variable in DECL. This should only be called is has_pre_init()
1233 // returns true. DECL may be NULL for a sink variable.
1235 get_init_block(Gogo*, Named_object* function, tree decl);
1237 // Export the variable.
1239 export_var(Export*, const std::string& name) const;
1241 // Import a variable.
1243 import_var(Import*, std::string* pname, Type** ptype);
1246 // The type of a tuple.
1248 type_from_tuple(Expression*, bool) const;
1250 // The type of a range.
1252 type_from_range(Expression*, bool, bool) const;
1254 // The element type of a channel.
1256 type_from_chan_element(Expression*, bool) const;
1258 // The variable's type. This may be NULL if the type is set from
1261 // The initial value. This may be NULL if the variable should be
1262 // initialized to the default value for the type.
1264 // Statements to run before the init statement.
1266 // Location of variable definition.
1267 source_location location_;
1268 // Backend representation.
1269 Bvariable* backend_;
1270 // Whether this is a global variable.
1271 bool is_global_ : 1;
1272 // Whether this is a function parameter.
1273 bool is_parameter_ : 1;
1274 // Whether this is the receiver parameter of a method.
1275 bool is_receiver_ : 1;
1276 // Whether this is the varargs parameter of a function.
1277 bool is_varargs_parameter_ : 1;
1278 // Whether something takes the address of this variable. For a
1279 // local variable this implies that the variable has to be on the
1281 bool is_address_taken_ : 1;
1282 // Whether something takes the address of this variable such that
1283 // the address does not escape the function.
1284 bool is_non_escaping_address_taken_ : 1;
1285 // True if we have seen this variable in a traversal.
1287 // True if we have lowered the initialization expression.
1288 bool init_is_lowered_ : 1;
1289 // True if init is a tuple used to set the type.
1290 bool type_from_init_tuple_ : 1;
1291 // True if init is a range clause and the type is the index type.
1292 bool type_from_range_index_ : 1;
1293 // True if init is a range clause and the type is the value type.
1294 bool type_from_range_value_ : 1;
1295 // True if init is a channel and the type is the channel's element type.
1296 bool type_from_chan_element_ : 1;
1297 // True if this is a variable created for a type switch case.
1298 bool is_type_switch_var_ : 1;
1299 // True if we have determined types.
1300 bool determined_type_ : 1;
1303 // A variable which is really the name for a function return value, or
1306 class Result_variable
1309 Result_variable(Type* type, Function* function, int index,
1310 source_location location)
1311 : type_(type), function_(function), index_(index), location_(location),
1312 backend_(NULL), is_address_taken_(false),
1313 is_non_escaping_address_taken_(false)
1316 // Get the type of the result variable.
1319 { return this->type_; }
1321 // Get the function that this is associated with.
1324 { return this->function_; }
1326 // Index in the list of function results.
1329 { return this->index_; }
1331 // The location of the variable definition.
1334 { return this->location_; }
1336 // Whether this variable's address is taken.
1338 is_address_taken() const
1339 { return this->is_address_taken_; }
1341 // Note that something takes the address of this variable.
1344 { this->is_address_taken_ = true; }
1346 // Return whether the address is taken but does not escape.
1348 is_non_escaping_address_taken() const
1349 { return this->is_non_escaping_address_taken_; }
1351 // Note that something takes the address of this variable such that
1352 // the address does not escape the function.
1354 set_non_escaping_address_taken()
1355 { this->is_non_escaping_address_taken_ = true; }
1357 // Whether this variable should live in the heap.
1360 { return this->is_address_taken_; }
1362 // Set the function. This is used when cloning functions which call
1365 set_function(Function* function)
1366 { this->function_ = function; }
1368 // Get the backend representation of the variable.
1370 get_backend_variable(Gogo*, Named_object*, const std::string&);
1373 // Type of result variable.
1375 // Function with which this is associated.
1376 Function* function_;
1377 // Index in list of results.
1379 // Where the result variable is defined.
1380 source_location location_;
1381 // Backend representation.
1382 Bvariable* backend_;
1383 // Whether something takes the address of this variable.
1384 bool is_address_taken_;
1385 // Whether something takes the address of this variable such that
1386 // the address does not escape the function.
1387 bool is_non_escaping_address_taken_;
1390 // The value we keep for a named constant. This lets us hold a type
1391 // and an expression.
1393 class Named_constant
1396 Named_constant(Type* type, Expression* expr, int iota_value,
1397 source_location location)
1398 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1404 { return this->type_; }
1408 { return this->expr_; }
1412 { return this->iota_value_; }
1416 { return this->location_; }
1418 // Whether we are lowering.
1421 { return this->lowering_; }
1423 // Set that we are lowering.
1426 { this->lowering_ = true; }
1428 // We are no longer lowering.
1431 { this->lowering_ = false; }
1433 // Traverse the expression.
1435 traverse_expression(Traverse*);
1437 // Determine the type of the constant if necessary.
1441 // Indicate that we found and reported an error for this constant.
1445 // Export the constant.
1447 export_const(Export*, const std::string& name) const;
1449 // Import a constant.
1451 import_const(Import*, std::string*, Type**, Expression**);
1454 // The type of the constant.
1456 // The expression for the constant.
1458 // If the predeclared constant iota is used in EXPR_, this is the
1459 // value it will have. We do this because at parse time we don't
1460 // know whether the name "iota" will refer to the predeclared
1461 // constant or to something else. We put in the right value in when
1464 // The location of the definition.
1465 source_location location_;
1466 // Whether we are currently lowering this constant.
1470 // A type declaration.
1472 class Type_declaration
1475 Type_declaration(source_location location)
1476 : location_(location), in_function_(NULL), methods_(),
1477 issued_warning_(false)
1480 // Return the location.
1483 { return this->location_; }
1485 // Return the function in which this type is declared. This will
1486 // return NULL for a type declared in global scope.
1489 { return this->in_function_; }
1491 // Set the function in which this type is declared.
1493 set_in_function(Named_object* f)
1494 { this->in_function_ = f; }
1496 // Add a method to this type. This is used when methods are defined
1499 add_method(const std::string& name, Function* function);
1501 // Add a method declaration to this type.
1503 add_method_declaration(const std::string& name, Function_type* type,
1504 source_location location);
1506 // Return whether any methods were defined.
1508 has_methods() const;
1510 // Define methods when the real type is known.
1512 define_methods(Named_type*);
1514 // This is called if we are trying to use this type. It returns
1515 // true if we should issue a warning.
1520 typedef std::vector<Named_object*> Methods;
1522 // The location of the type declaration.
1523 source_location location_;
1524 // If this type is declared in a function, a pointer back to the
1525 // function in which it is defined.
1526 Named_object* in_function_;
1527 // Methods defined before the type is defined.
1529 // True if we have issued a warning about a use of this type
1530 // declaration when it is undefined.
1531 bool issued_warning_;
1534 // An unknown object. These are created by the parser for forward
1535 // references to names which have not been seen before. In a correct
1536 // program, these will always point to a real definition by the end of
1537 // the parse. Because they point to another Named_object, these may
1538 // only be referenced by Unknown_expression objects.
1543 Unknown_name(source_location location)
1544 : location_(location), real_named_object_(NULL)
1547 // Return the location where this name was first seen.
1550 { return this->location_; }
1552 // Return the real named object that this points to, or NULL if it
1553 // was never resolved.
1555 real_named_object() const
1556 { return this->real_named_object_; }
1558 // Set the real named object that this points to.
1560 set_real_named_object(Named_object* no);
1563 // The location where this name was first seen.
1564 source_location location_;
1565 // The real named object when it is known.
1570 // A named object named. This is the result of a declaration. We
1571 // don't use a superclass because they all have to be handled
1579 // An uninitialized Named_object. We should never see this.
1580 NAMED_OBJECT_UNINITIALIZED,
1581 // An unknown name. This is used for forward references. In a
1582 // correct program, these will all be resolved by the end of the
1584 NAMED_OBJECT_UNKNOWN,
1589 // A forward type declaration.
1590 NAMED_OBJECT_TYPE_DECLARATION,
1593 // A result variable in a function.
1594 NAMED_OBJECT_RESULT_VAR,
1595 // The blank identifier--the special variable named _.
1599 // A forward func declaration.
1600 NAMED_OBJECT_FUNC_DECLARATION,
1602 NAMED_OBJECT_PACKAGE
1605 // Return the classification.
1607 classification() const
1608 { return this->classification_; }
1614 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1618 { return this->classification_ == NAMED_OBJECT_CONST; }
1622 { return this->classification_ == NAMED_OBJECT_TYPE; }
1625 is_type_declaration() const
1626 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1630 { return this->classification_ == NAMED_OBJECT_VAR; }
1633 is_result_variable() const
1634 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1638 { return this->classification_ == NAMED_OBJECT_SINK; }
1642 { return this->classification_ == NAMED_OBJECT_FUNC; }
1645 is_function_declaration() const
1646 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1650 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1654 static Named_object*
1655 make_unknown_name(const std::string& name, source_location);
1657 static Named_object*
1658 make_constant(const Typed_identifier&, const Package*, Expression*,
1661 static Named_object*
1662 make_type(const std::string&, const Package*, Type*, source_location);
1664 static Named_object*
1665 make_type_declaration(const std::string&, const Package*, source_location);
1667 static Named_object*
1668 make_variable(const std::string&, const Package*, Variable*);
1670 static Named_object*
1671 make_result_variable(const std::string&, Result_variable*);
1673 static Named_object*
1676 static Named_object*
1677 make_function(const std::string&, const Package*, Function*);
1679 static Named_object*
1680 make_function_declaration(const std::string&, const Package*, Function_type*,
1683 static Named_object*
1684 make_package(const std::string& alias, Package* package);
1691 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1692 return this->u_.unknown_value;
1696 unknown_value() const
1698 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1699 return this->u_.unknown_value;
1705 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1706 return this->u_.const_value;
1709 const Named_constant*
1712 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1713 return this->u_.const_value;
1719 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1720 return this->u_.type_value;
1726 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1727 return this->u_.type_value;
1731 type_declaration_value()
1733 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1734 return this->u_.type_declaration;
1737 const Type_declaration*
1738 type_declaration_value() const
1740 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1741 return this->u_.type_declaration;
1747 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1748 return this->u_.var_value;
1754 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1755 return this->u_.var_value;
1761 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1762 return this->u_.result_var_value;
1765 const Result_variable*
1766 result_var_value() const
1768 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1769 return this->u_.result_var_value;
1775 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1776 return this->u_.func_value;
1782 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1783 return this->u_.func_value;
1786 Function_declaration*
1787 func_declaration_value()
1789 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1790 return this->u_.func_declaration_value;
1793 const Function_declaration*
1794 func_declaration_value() const
1796 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1797 return this->u_.func_declaration_value;
1803 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1804 return this->u_.package_value;
1808 package_value() const
1810 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1811 return this->u_.package_value;
1816 { return this->name_; }
1818 // Return the name to use in an error message. The difference is
1819 // that if this Named_object is defined in a different package, this
1820 // will return PACKAGE.NAME.
1822 message_name() const;
1826 { return this->package_; }
1828 // Resolve an unknown value if possible. This returns the same
1829 // Named_object or a new one.
1833 Named_object* ret = this;
1834 if (this->is_unknown())
1836 Named_object* r = this->unknown_value()->real_named_object();
1846 const Named_object* ret = this;
1847 if (this->is_unknown())
1849 const Named_object* r = this->unknown_value()->real_named_object();
1856 // The location where this object was defined or referenced.
1860 // Convert a variable to the backend representation.
1862 get_backend_variable(Gogo*, Named_object* function);
1864 // Return a tree for the external identifier for this object.
1868 // Return a tree representing this object.
1870 get_tree(Gogo*, Named_object* function);
1872 // Define a type declaration.
1874 set_type_value(Named_type*);
1876 // Define a function declaration.
1878 set_function_value(Function*);
1880 // Declare an unknown name as a type declaration.
1884 // Export this object.
1886 export_named_object(Export*) const;
1889 Named_object(const std::string&, const Package*, Classification);
1891 // The name of the object.
1893 // The package that this object is in. This is NULL if it is in the
1894 // file we are compiling.
1895 const Package* package_;
1896 // The type of object this is.
1897 Classification classification_;
1901 Unknown_name* unknown_value;
1902 Named_constant* const_value;
1903 Named_type* type_value;
1904 Type_declaration* type_declaration;
1905 Variable* var_value;
1906 Result_variable* result_var_value;
1907 Function* func_value;
1908 Function_declaration* func_declaration_value;
1909 Package* package_value;
1911 // The DECL tree for this object if we have already converted it.
1915 // A binding contour. This binds names to objects.
1920 // Type for mapping from names to objects.
1921 typedef Unordered_map(std::string, Named_object*) Contour;
1923 Bindings(Bindings* enclosing);
1925 // Add an unknown name.
1927 add_unknown_name(const std::string& name, source_location location)
1929 return this->add_named_object(Named_object::make_unknown_name(name,
1935 add_constant(const Typed_identifier& tid, const Package* package,
1936 Expression* expr, int iota_value)
1938 return this->add_named_object(Named_object::make_constant(tid, package,
1945 add_type(const std::string& name, const Package* package, Type* type,
1946 source_location location)
1948 return this->add_named_object(Named_object::make_type(name, package, type,
1952 // Add a named type. This is used for builtin types, and to add an
1953 // imported type to the global scope.
1955 add_named_type(Named_type* named_type);
1957 // Add a type declaration.
1959 add_type_declaration(const std::string& name, const Package* package,
1960 source_location location)
1962 Named_object* no = Named_object::make_type_declaration(name, package,
1964 return this->add_named_object(no);
1969 add_variable(const std::string& name, const Package* package,
1972 return this->add_named_object(Named_object::make_variable(name, package,
1976 // Add a result variable.
1978 add_result_variable(const std::string& name, Result_variable* result)
1980 return this->add_named_object(Named_object::make_result_variable(name,
1986 add_function(const std::string& name, const Package*, Function* function);
1988 // Add a function declaration.
1990 add_function_declaration(const std::string& name, const Package* package,
1991 Function_type* type, source_location location);
1993 // Add a package. The location is the location of the import
1996 add_package(const std::string& alias, Package* package)
1998 Named_object* no = Named_object::make_package(alias, package);
1999 return this->add_named_object(no);
2002 // Define a type which was already declared.
2004 define_type(Named_object*, Named_type*);
2006 // Add a method to the list of objects. This is not added to the
2009 add_method(Named_object*);
2011 // Add a named object to this binding.
2013 add_named_object(Named_object* no)
2014 { return this->add_named_object_to_contour(&this->bindings_, no); }
2016 // Clear all names in file scope from the bindings.
2020 // Look up a name in this binding contour and in any enclosing
2021 // binding contours. This returns NULL if the name is not found.
2023 lookup(const std::string&) const;
2025 // Look up a name in this binding contour without looking in any
2026 // enclosing binding contours. Returns NULL if the name is not found.
2028 lookup_local(const std::string&) const;
2032 remove_binding(Named_object*);
2034 // Traverse the tree. See the Traverse class.
2036 traverse(Traverse*, bool is_global);
2038 // Iterate over definitions. This does not include things which
2039 // were only declared.
2041 typedef std::vector<Named_object*>::const_iterator
2042 const_definitions_iterator;
2044 const_definitions_iterator
2045 begin_definitions() const
2046 { return this->named_objects_.begin(); }
2048 const_definitions_iterator
2049 end_definitions() const
2050 { return this->named_objects_.end(); }
2052 // Return the number of definitions.
2054 size_definitions() const
2055 { return this->named_objects_.size(); }
2057 // Return whether there are no definitions.
2059 empty_definitions() const
2060 { return this->named_objects_.empty(); }
2062 // Iterate over declarations. This is everything that has been
2063 // declared, which includes everything which has been defined.
2065 typedef Contour::const_iterator const_declarations_iterator;
2067 const_declarations_iterator
2068 begin_declarations() const
2069 { return this->bindings_.begin(); }
2071 const_declarations_iterator
2072 end_declarations() const
2073 { return this->bindings_.end(); }
2075 // Return the number of declarations.
2077 size_declarations() const
2078 { return this->bindings_.size(); }
2080 // Return whether there are no declarations.
2082 empty_declarations() const
2083 { return this->bindings_.empty(); }
2085 // Return the first declaration.
2088 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2092 add_named_object_to_contour(Contour*, Named_object*);
2095 new_definition(Named_object*, Named_object*);
2097 // Enclosing bindings.
2098 Bindings* enclosing_;
2099 // The list of objects.
2100 std::vector<Named_object*> named_objects_;
2101 // The mapping from names to objects.
2110 Label(const std::string& name)
2111 : name_(name), location_(0), is_used_(false), blabel_(NULL)
2114 // Return the label's name.
2117 { return this->name_; }
2119 // Return whether the label has been defined.
2122 { return this->location_ != 0; }
2124 // Return whether the label has been used.
2127 { return this->is_used_; }
2129 // Record that the label is used.
2132 { this->is_used_ = true; }
2134 // Return the location of the definition.
2137 { return this->location_; }
2139 // Define the label at LOCATION.
2141 define(source_location location)
2143 go_assert(this->location_ == 0);
2144 this->location_ = location;
2147 // Return the backend representation for this label.
2149 get_backend_label(Translate_context*);
2151 // Return an expression for the address of this label. This is used
2152 // to get the return address of a deferred function to see whether
2153 // the function may call recover.
2155 get_addr(Translate_context*, source_location location);
2158 // The name of the label.
2160 // The location of the definition. This is 0 if the label has not
2161 // yet been defined.
2162 source_location location_;
2163 // Whether the label has been used.
2165 // The backend representation.
2169 // An unnamed label. These are used when lowering loops.
2174 Unnamed_label(source_location location)
2175 : location_(location), blabel_(NULL)
2178 // Get the location where the label is defined.
2181 { return this->location_; }
2183 // Set the location where the label is defined.
2185 set_location(source_location location)
2186 { this->location_ = location; }
2188 // Return a statement which defines this label.
2190 get_definition(Translate_context*);
2192 // Return a goto to this label from LOCATION.
2194 get_goto(Translate_context*, source_location location);
2197 // Return the backend representation.
2199 get_blabel(Translate_context*);
2201 // The location where the label is defined.
2202 source_location location_;
2203 // The backend representation of this label.
2207 // An imported package.
2212 Package(const std::string& name, const std::string& unique_prefix,
2213 source_location location);
2215 // The real name of this package. This may be different from the
2216 // name in the associated Named_object if the import statement used
2220 { return this->name_; }
2222 // Return the location of the import statement.
2225 { return this->location_; }
2227 // Get the unique prefix used for all symbols exported from this
2230 unique_prefix() const
2232 go_assert(!this->unique_prefix_.empty());
2233 return this->unique_prefix_;
2236 // The priority of this package. The init function of packages with
2237 // lower priority must be run before the init function of packages
2238 // with higher priority.
2241 { return this->priority_; }
2243 // Set the priority.
2245 set_priority(int priority);
2247 // Return the bindings.
2250 { return this->bindings_; }
2252 // Whether some symbol from the package was used.
2255 { return this->used_; }
2257 // Note that some symbol from this package was used.
2260 { this->used_ = true; }
2262 // Clear the used field for the next file.
2265 { this->used_ = false; }
2267 // Whether this package was imported in the current file.
2270 { return this->is_imported_; }
2272 // Note that this package was imported in the current file.
2275 { this->is_imported_ = true; }
2277 // Clear the imported field for the next file.
2280 { this->is_imported_ = false; }
2282 // Whether this package was imported with a name of "_".
2284 uses_sink_alias() const
2285 { return this->uses_sink_alias_; }
2287 // Note that this package was imported with a name of "_".
2289 set_uses_sink_alias()
2290 { this->uses_sink_alias_ = true; }
2292 // Clear the sink alias field for the next file.
2294 clear_uses_sink_alias()
2295 { this->uses_sink_alias_ = false; }
2297 // Look up a name in the package. Returns NULL if the name is not
2300 lookup(const std::string& name) const
2301 { return this->bindings_->lookup(name); }
2303 // Set the location of the package. This is used if it is seen in a
2304 // different import before it is really imported.
2306 set_location(source_location location)
2307 { this->location_ = location; }
2309 // Add a constant to the package.
2311 add_constant(const Typed_identifier& tid, Expression* expr)
2312 { return this->bindings_->add_constant(tid, this, expr, 0); }
2314 // Add a type to the package.
2316 add_type(const std::string& name, Type* type, source_location location)
2317 { return this->bindings_->add_type(name, this, type, location); }
2319 // Add a type declaration to the package.
2321 add_type_declaration(const std::string& name, source_location location)
2322 { return this->bindings_->add_type_declaration(name, this, location); }
2324 // Add a variable to the package.
2326 add_variable(const std::string& name, Variable* variable)
2327 { return this->bindings_->add_variable(name, this, variable); }
2329 // Add a function declaration to the package.
2331 add_function_declaration(const std::string& name, Function_type* type,
2332 source_location loc)
2333 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2335 // Determine types of constants.
2340 // The real name of this package.
2342 // The unique prefix for all exported global symbols.
2343 std::string unique_prefix_;
2344 // The names in this package.
2345 Bindings* bindings_;
2346 // The priority of this package. A package has a priority higher
2347 // than the priority of all of the packages that it imports. This
2348 // is used to run init functions in the right order.
2350 // The location of the import statement.
2351 source_location location_;
2352 // True if some name from this package was used. This is mutable
2353 // because we can use a package even if we have a const pointer to
2356 // True if this package was imported in the current file.
2358 // True if this package was imported with a name of "_".
2359 bool uses_sink_alias_;
2362 // Return codes for the traversal functions. This is not an enum
2363 // because we want to be able to declare traversal functions in other
2364 // header files without including this one.
2366 // Continue traversal as usual.
2367 const int TRAVERSE_CONTINUE = -1;
2370 const int TRAVERSE_EXIT = 0;
2372 // Continue traversal, but skip components of the current object.
2373 // E.g., if this is returned by Traverse::statement, we do not
2374 // traverse the expressions in the statement even if
2375 // traverse_expressions is set in the traverse_mask.
2376 const int TRAVERSE_SKIP_COMPONENTS = 1;
2378 // This class is used when traversing the parse tree. The caller uses
2379 // a subclass which overrides functions as desired.
2384 // These bitmasks say what to traverse.
2385 static const unsigned int traverse_variables = 0x1;
2386 static const unsigned int traverse_constants = 0x2;
2387 static const unsigned int traverse_functions = 0x4;
2388 static const unsigned int traverse_blocks = 0x8;
2389 static const unsigned int traverse_statements = 0x10;
2390 static const unsigned int traverse_expressions = 0x20;
2391 static const unsigned int traverse_types = 0x40;
2393 Traverse(unsigned int traverse_mask)
2394 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2397 virtual ~Traverse();
2399 // The bitmask of what to traverse.
2401 traverse_mask() const
2402 { return this->traverse_mask_; }
2404 // Record that we are going to traverse a type. This returns true
2405 // if the type has already been seen in this traversal. This is
2406 // required because types, unlike expressions, can form a circular
2409 remember_type(const Type*);
2411 // Record that we are going to see an expression. This returns true
2412 // if the expression has already been seen in this traversal. This
2413 // is only needed for cases where multiple expressions can point to
2416 remember_expression(const Expression*);
2418 // These functions return one of the TRAVERSE codes defined above.
2420 // If traverse_variables is set in the mask, this is called for
2421 // every variable in the tree.
2423 variable(Named_object*);
2425 // If traverse_constants is set in the mask, this is called for
2426 // every named constant in the tree. The bool parameter is true for
2427 // a global constant.
2429 constant(Named_object*, bool);
2431 // If traverse_functions is set in the mask, this is called for
2432 // every function in the tree.
2434 function(Named_object*);
2436 // If traverse_blocks is set in the mask, this is called for every
2437 // block in the tree.
2441 // If traverse_statements is set in the mask, this is called for
2442 // every statement in the tree.
2444 statement(Block*, size_t* index, Statement*);
2446 // If traverse_expressions is set in the mask, this is called for
2447 // every expression in the tree.
2449 expression(Expression**);
2451 // If traverse_types is set in the mask, this is called for every
2452 // type in the tree.
2457 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2458 Type_identical) Types_seen;
2460 typedef Unordered_set(const Expression*) Expressions_seen;
2462 // Bitmask of what sort of objects to traverse.
2463 unsigned int traverse_mask_;
2464 // Types which have been seen in this traversal.
2465 Types_seen* types_seen_;
2466 // Expressions which have been seen in this traversal.
2467 Expressions_seen* expressions_seen_;
2470 // A class which makes it easier to insert new statements before the
2471 // current statement during a traversal.
2473 class Statement_inserter
2476 // Empty constructor.
2477 Statement_inserter()
2478 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2481 // Constructor for a statement in a block.
2482 Statement_inserter(Block* block, size_t *pindex)
2483 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2486 // Constructor for a global variable.
2487 Statement_inserter(Gogo* gogo, Variable* var)
2488 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2489 { go_assert(var->is_global()); }
2491 // We use the default copy constructor and assignment operator.
2493 // Insert S before the statement we are traversing, or before the
2494 // initialization expression of a global variable.
2496 insert(Statement* s);
2499 // The block that the statement is in.
2501 // The index of the statement that we are traversing.
2503 // The IR, needed when looking at an initializer expression for a
2506 // The global variable, when looking at an initializer expression.
2510 // When translating the gogo IR into the backend data structure, this
2511 // is the context we pass down the blocks and statements.
2513 class Translate_context
2516 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2518 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2519 block_(block), bblock_(bblock), is_const_(false)
2526 { return this->gogo_; }
2530 { return this->backend_; }
2534 { return this->function_; }
2538 { return this->block_; }
2542 { return this->bblock_; }
2546 { return this->is_const_; }
2548 // Make a constant context.
2551 { this->is_const_ = true; }
2554 // The IR for the entire compilation unit.
2556 // The generator for the backend data structures.
2558 // The function we are currently translating. NULL if not in a
2559 // function, e.g., the initializer of a global variable.
2560 Named_object* function_;
2561 // The block we are currently translating. NULL if not in a
2564 // The backend representation of the current block. NULL if block_
2567 // Whether this is being evaluated in a constant context. This is
2568 // used for type descriptor initializers.
2572 // Runtime error codes. These must match the values in
2573 // libgo/runtime/go-runtime-error.c.
2575 // Slice index out of bounds: negative or larger than the length of
2577 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2579 // Array index out of bounds.
2580 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2582 // String index out of bounds.
2583 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2585 // Slice slice out of bounds: negative or larger than the length of
2586 // the slice or high bound less than low bound.
2587 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2589 // Array slice out of bounds.
2590 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2592 // String slice out of bounds.
2593 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2595 // Dereference of nil pointer. This is used when there is a
2596 // dereference of a pointer to a very large struct or array, to ensure
2597 // that a gigantic array is not used a proxy to access random memory
2599 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2601 // Slice length or capacity out of bounds in make: negative or
2602 // overflow or length greater than capacity.
2603 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2605 // Map capacity out of bounds in make: negative or overflow.
2606 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2608 // Channel capacity out of bounds in make: negative or overflow.
2609 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2611 // This is used by some of the langhooks.
2612 extern Gogo* go_get_gogo();
2614 // Whether we have seen any errors. FIXME: Replace with a backend
2616 extern bool saw_errors();
2618 #endif // !defined(GO_GOGO_H)