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.
10 #include "go-linemap.h"
13 class Statement_inserter;
15 class Type_hash_identical;
18 class Typed_identifier;
19 class Typed_identifier_list;
23 class Temporary_statement;
27 class Bindings_snapshot;
33 class Struct_field_list;
39 class Forward_declaration_type;
44 class Translate_context;
54 // This file declares the basic classes used to hold the internal
55 // representation of Go which is built by the parser.
57 // An initialization function for an imported package. This is a
58 // magic function which initializes variables and runs the "init"
64 Import_init(const std::string& package_name, const std::string& init_name,
66 : package_name_(package_name), init_name_(init_name), priority_(priority)
69 // The name of the package being imported.
72 { return this->package_name_; }
74 // The name of the package's init function.
77 { return this->init_name_; }
79 // The priority of the initialization function. Functions with a
80 // lower priority number must be run first.
83 { return this->priority_; }
86 // The name of the package being imported.
87 std::string package_name_;
88 // The name of the package's init function.
89 std::string init_name_;
94 // For sorting purposes.
97 operator<(const Import_init& i1, const Import_init& i2)
99 if (i1.priority() < i2.priority())
101 if (i1.priority() > i2.priority())
103 if (i1.package_name() != i2.package_name())
104 return i1.package_name() < i2.package_name();
105 return i1.init_name() < i2.init_name();
108 // The holder for the internal representation of the entire
114 // Create the IR, passing in the sizes of the types "int" and
115 // "uintptr" in bits.
116 Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size);
118 // Get the backend generator.
121 { return this->backend_; }
123 // Get the Location generator.
126 { return this->linemap_; }
128 // Get the package name.
130 package_name() const;
132 // Set the package name.
134 set_package_name(const std::string&, Location);
136 // Return whether this is the "main" package.
138 is_main_package() const;
140 // If necessary, adjust the name to use for a hidden symbol. We add
141 // a prefix of the package name, so that hidden symbols in different
142 // packages do not collide.
144 pack_hidden_name(const std::string& name, bool is_exported) const
148 : ('.' + this->unique_prefix()
149 + '.' + this->package_name()
153 // Unpack a name which may have been hidden. Returns the
154 // user-visible name of the object.
156 unpack_hidden_name(const std::string& name)
157 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
159 // Return whether a possibly packed name is hidden.
161 is_hidden_name(const std::string& name)
162 { return name[0] == '.'; }
164 // Return the package prefix of a hidden name.
166 hidden_name_prefix(const std::string& name)
168 go_assert(Gogo::is_hidden_name(name));
169 return name.substr(1, name.rfind('.') - 1);
172 // Given a name which may or may not have been hidden, return the
173 // name to use in an error message.
175 message_name(const std::string& name);
177 // Return whether a name is the blank identifier _.
179 is_sink_name(const std::string& name)
181 return (name[0] == '.'
182 && name[name.length() - 1] == '_'
183 && name[name.length() - 2] == '.');
186 // Return the unique prefix to use for all exported symbols.
188 unique_prefix() const;
190 // Set the unique prefix.
192 set_unique_prefix(const std::string&);
194 // Return the priority to use for the package we are compiling.
195 // This is two more than the largest priority of any package we
198 package_priority() const;
200 // Import a package. FILENAME is the file name argument, LOCAL_NAME
201 // is the local name to give to the package. If LOCAL_NAME is empty
202 // the declarations are added to the global scope.
204 import_package(const std::string& filename, const std::string& local_name,
205 bool is_local_name_exported, Location);
207 // Whether we are the global binding level.
209 in_global_scope() const;
211 // Look up a name in the current binding contours.
213 lookup(const std::string&, Named_object** pfunction) const;
215 // Look up a name in the current block.
217 lookup_in_block(const std::string&) const;
219 // Look up a name in the global namespace--the universal scope.
221 lookup_global(const char*) const;
223 // Add a new imported package. REAL_NAME is the real name of the
224 // package. ALIAS is the alias of the package; this may be the same
225 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
226 // this package should be added to the global namespace; this is
227 // true if the alias is ".". LOCATION is the location of the import
228 // statement. This returns the new package, or NULL on error.
230 add_imported_package(const std::string& real_name, const std::string& alias,
231 bool is_alias_exported,
232 const std::string& unique_prefix,
234 bool* padd_to_globals);
236 // Register a package. This package may or may not be imported.
237 // This returns the Package structure for the package, creating if
240 register_package(const std::string& name, const std::string& unique_prefix,
243 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
244 // method function should be added to the type of its receiver.
246 start_function(const std::string& name, Function_type* type,
247 bool add_method_to_type, Location);
249 // Finish compiling a function.
251 finish_function(Location);
253 // Return the current function.
255 current_function() const;
257 // Return the current block.
261 // Start a new block. This is not initially associated with a
264 start_block(Location);
266 // Finish the current block and return it.
268 finish_block(Location);
270 // Declare an unknown name. This is used while parsing. The name
271 // must be resolved by the end of the parse. Unknown names are
272 // always added at the package level.
274 add_unknown_name(const std::string& name, Location);
276 // Declare a function.
278 declare_function(const std::string&, Function_type*, Location);
282 add_label_definition(const std::string&, Location);
284 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
285 // report errors for a goto from the current location to the label
288 add_label_reference(const std::string&, Location,
289 bool issue_goto_errors);
291 // Return a snapshot of the current binding state.
293 bindings_snapshot(Location);
295 // Add a statement to the current block.
297 add_statement(Statement*);
299 // Add a block to the current block.
301 add_block(Block*, Location);
305 add_constant(const Typed_identifier&, Expression*, int iota_value);
309 add_type(const std::string&, Type*, Location);
311 // Add a named type. This is used for builtin types, and to add an
312 // imported type to the global scope.
314 add_named_type(Named_type*);
318 declare_type(const std::string&, Location);
320 // Declare a type at the package level. This is used when the
321 // parser sees an unknown name where a type name is required.
323 declare_package_type(const std::string&, Location);
325 // Define a type which was already declared.
327 define_type(Named_object*, Named_type*);
331 add_variable(const std::string&, Variable*);
333 // Add a sink--a reference to the blank identifier _.
337 // Add a named object to the current namespace. This is used for
338 // import . "package".
340 add_named_object(Named_object*);
342 // Return a name to use for a thunk function. A thunk function is
343 // one we create during the compilation, for a go statement or a
344 // defer statement or a method expression.
348 // Return whether an object is a thunk.
350 is_thunk(const Named_object*);
352 // Note that we've seen an interface type. This is used to build
353 // all required interface method tables.
355 record_interface_type(Interface_type*);
357 // Note that we need an initialization function.
360 { this->need_init_fn_ = true; }
362 // Clear out all names in file scope. This is called when we start
363 // parsing a new file.
367 // Traverse the tree. See the Traverse class.
371 // Define the predeclared global names.
373 define_global_names();
375 // Verify and complete all types.
379 // Lower the parse tree.
383 // Lower all the statements in a block.
385 lower_block(Named_object* function, Block*);
387 // Lower an expression.
389 lower_expression(Named_object* function, Statement_inserter*, Expression**);
393 lower_constant(Named_object*);
395 // Finalize the method lists and build stub methods for named types.
399 // Work out the types to use for unspecified variables and
404 // Type check the program.
408 // Check the types in a single block. This is used for complicated
411 check_types_in_block(Block*);
413 // Check for return statements.
415 check_return_statements();
421 // Add an import control function for an imported package to the
424 add_import_init_fn(const std::string& package_name,
425 const std::string& init_name, int prio);
427 // Turn short-cut operators (&&, ||) into explicit if statements.
431 // Use temporary variables to force order of evaluation.
435 // Build thunks for functions which call recover.
437 build_recover_thunks();
439 // Simplify statements which might use thunks: go and defer
442 simplify_thunk_statements();
444 // Dump AST if -fgo-dump-ast is set
446 dump_ast(const char* basename);
448 // Convert named types to the backend representation.
450 convert_named_types();
452 // Convert named types in a list of bindings.
454 convert_named_types_in_bindings(Bindings*);
456 // True if named types have been converted to the backend
459 named_types_are_converted() const
460 { return this->named_types_are_converted_; }
462 // Write out the global values.
466 // Create trees for implicit builtin functions.
468 define_builtin_function_trees();
470 // Build a call to a builtin function. PDECL should point to a NULL
471 // initialized static pointer which will hold the fndecl. NAME is
472 // the name of the function. NARGS is the number of arguments.
473 // RETTYPE is the return type. It is followed by NARGS pairs of
474 // type and argument (both trees).
476 call_builtin(tree* pdecl, Location, const char* name, int nargs,
479 // Build a call to the runtime error function.
481 runtime_error(int code, Location);
483 // Build a builtin struct with a list of fields.
485 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
488 // Mark a function declaration as a builtin library function.
490 mark_fndecl_as_builtin_library(tree fndecl);
492 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
493 // the slice. VALUES points to the values. COUNT is the size,
494 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
497 slice_constructor(tree slice_type_tree, tree values, tree count,
500 // Build required interface method tables.
502 build_interface_method_tables();
504 // Build an interface method table for a type: a list of function
505 // pointers, one for each interface method. This returns a decl.
507 interface_method_table_for_type(const Interface_type*, Named_type*,
510 // Return a tree which allocate SIZE bytes to hold values of type
513 allocate_memory(Type *type, tree size, Location);
515 // Return a type to use for pointer to const char.
517 const_char_pointer_type_tree();
519 // Build a string constant with the right type.
521 string_constant_tree(const std::string&);
523 // Build a Go string constant. This returns a pointer to the
526 go_string_constant_tree(const std::string&);
528 // Receive a value from a channel.
530 receive_from_channel(tree type_tree, tree channel, bool for_select,
533 // Return a tree for receiving an integer on a channel.
535 receive_as_64bit_integer(tree type, tree channel, bool blocking,
538 // Make a trampoline which calls FNADDR passing CLOSURE.
540 make_trampoline(tree fnaddr, tree closure, Location);
543 // During parsing, we keep a stack of functions. Each function on
544 // the stack is one that we are currently parsing. For each
545 // function, we keep track of the current stack of blocks.
549 Named_object* function;
550 // The stack of active blocks in the function.
551 std::vector<Block*> blocks;
554 // The stack of functions.
555 typedef std::vector<Open_function> Open_functions;
557 // Set up the built-in unsafe package.
559 import_unsafe(const std::string&, bool is_exported, Location);
561 // Add a new imported package.
563 add_package(const std::string& real_name, const std::string& alias,
564 const std::string& unique_prefix, Location location);
566 // Return the current binding contour.
571 current_bindings() const;
573 // Get the name of the magic initialization function.
577 // Get the decl for the magic initialization function.
579 initialization_function_decl();
581 // Write the magic initialization function.
583 write_initialization_function(tree fndecl, tree init_stmt_list);
585 // Initialize imported packages.
589 // Register variables with the garbage collector.
591 register_gc_vars(const std::vector<Named_object*>&, tree*);
593 // Build a pointer to a Go string constant. This returns a pointer
596 ptr_go_string_constant_tree(const std::string&);
598 // Return the type of a trampoline.
600 trampoline_type_tree();
602 // Type used to map import names to packages.
603 typedef std::map<std::string, Package*> Imports;
605 // Type used to map package names to packages.
606 typedef std::map<std::string, Package*> Packages;
608 // Type used to map special names in the sys package.
609 typedef std::map<std::string, std::string> Sys_names;
611 // The backend generator.
613 // The object used to keep track of file names and line numbers.
615 // The package we are compiling.
617 // The list of currently open functions during parsing.
618 Open_functions functions_;
619 // The global binding contour. This includes the builtin functions
620 // and the package we are compiling.
622 // Mapping from import file names to packages.
624 // Whether the magic unsafe package was imported.
625 bool imported_unsafe_;
626 // Mapping from package names we have seen to packages. This does
627 // not include the package we are compiling.
629 // The functions named "init", if there are any.
630 std::vector<Named_object*> init_functions_;
631 // Whether we need a magic initialization function.
633 // The name of the magic initialization function.
634 std::string init_fn_name_;
635 // A list of import control variables for packages that we import.
636 std::set<Import_init> imported_init_fns_;
637 // The unique prefix used for all global symbols.
638 std::string unique_prefix_;
639 // Whether an explicit unique prefix was set by -fgo-prefix.
640 bool unique_prefix_specified_;
641 // A list of interface types defined while parsing.
642 std::vector<Interface_type*> interface_types_;
643 // Whether named types have been converted.
644 bool named_types_are_converted_;
647 // A block of statements.
652 Block(Block* enclosing, Location);
654 // Return the enclosing block.
657 { return this->enclosing_; }
659 // Return the bindings of the block.
662 { return this->bindings_; }
666 { return this->bindings_; }
668 // Look at the block's statements.
669 const std::vector<Statement*>*
671 { return &this->statements_; }
673 // Return the start location. This is normally the location of the
674 // left curly brace which starts the block.
676 start_location() const
677 { return this->start_location_; }
679 // Return the end location. This is normally the location of the
680 // right curly brace which ends the block.
683 { return this->end_location_; }
685 // Add a statement to the block.
687 add_statement(Statement*);
689 // Add a statement to the front of the block.
691 add_statement_at_front(Statement*);
693 // Replace a statement in a block.
695 replace_statement(size_t index, Statement*);
697 // Add a Statement before statement number INDEX.
699 insert_statement_before(size_t index, Statement*);
701 // Add a Statement after statement number INDEX.
703 insert_statement_after(size_t index, Statement*);
705 // Set the end location of the block.
707 set_end_location(Location location)
708 { this->end_location_ = location; }
710 // Traverse the tree.
714 // Set final types for unspecified variables and constants.
718 // Return true if execution of this block may fall through to the
721 may_fall_through() const;
723 // Convert the block to the backend representation.
725 get_backend(Translate_context*);
727 // Iterate over statements.
729 typedef std::vector<Statement*>::iterator iterator;
733 { return this->statements_.begin(); }
737 { return this->statements_.end(); }
742 // Statements in the block.
743 std::vector<Statement*> statements_;
746 // Location of start of block.
747 Location start_location_;
748 // Location of end of block.
749 Location end_location_;
757 Function(Function_type* type, Function*, Block*, Location);
759 // Return the function's type.
762 { return this->type_; }
764 // Return the enclosing function if there is one.
767 { return this->enclosing_; }
769 // Set the enclosing function. This is used when building thunks
770 // for functions which call recover.
772 set_enclosing(Function* enclosing)
774 go_assert(this->enclosing_ == NULL);
775 this->enclosing_ = enclosing;
778 // The result variables.
779 typedef std::vector<Named_object*> Results;
781 // Create the result variables in the outer block.
783 create_result_variables(Gogo*);
785 // Update the named result variables when cloning a function which
788 update_result_variables();
790 // Return the result variables.
793 { return this->results_; }
795 // Whether the result variables have names.
797 results_are_named() const
798 { return this->results_are_named_; }
800 // Add a new field to the closure variable.
802 add_closure_field(Named_object* var, Location loc)
803 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
805 // Whether this function needs a closure.
807 needs_closure() const
808 { return !this->closure_fields_.empty(); }
810 // Return the closure variable, creating it if necessary. This is
811 // passed to the function as a static chain parameter.
815 // Set the closure variable. This is used when building thunks for
816 // functions which call recover.
818 set_closure_var(Named_object* v)
820 go_assert(this->closure_var_ == NULL);
821 this->closure_var_ = v;
824 // Return the variable for a reference to field INDEX in the closure
827 enclosing_var(unsigned int index)
829 go_assert(index < this->closure_fields_.size());
830 return closure_fields_[index].first;
833 // Set the type of the closure variable if there is one.
837 // Get the block of statements associated with the function.
840 { return this->block_; }
842 // Get the location of the start of the function.
845 { return this->location_; }
847 // Return whether this function is actually a method.
851 // Add a label definition to the function.
853 add_label_definition(Gogo*, const std::string& label_name, Location);
855 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
856 // if we should report errors for a goto from the current location
857 // to the label location.
859 add_label_reference(Gogo*, const std::string& label_name,
860 Location, bool issue_goto_errors);
862 // Warn about labels that are defined but not used.
864 check_labels() const;
866 // Whether this function calls the predeclared recover function.
868 calls_recover() const
869 { return this->calls_recover_; }
871 // Record that this function calls the predeclared recover function.
872 // This is set during the lowering pass.
875 { this->calls_recover_ = true; }
877 // Whether this is a recover thunk function.
879 is_recover_thunk() const
880 { return this->is_recover_thunk_; }
882 // Record that this is a thunk built for a function which calls
885 set_is_recover_thunk()
886 { this->is_recover_thunk_ = true; }
888 // Whether this function already has a recover thunk.
890 has_recover_thunk() const
891 { return this->has_recover_thunk_; }
893 // Record that this function already has a recover thunk.
895 set_has_recover_thunk()
896 { this->has_recover_thunk_ = true; }
898 // Swap with another function. Used only for the thunk which calls
901 swap_for_recover(Function *);
903 // Traverse the tree.
907 // Determine types in the function.
911 // Return the function's decl given an identifier.
913 get_or_make_decl(Gogo*, Named_object*, tree id);
915 // Return the function's decl after it has been built.
919 go_assert(this->fndecl_ != NULL);
920 return this->fndecl_;
923 // Set the function decl to hold a tree of the function code.
925 build_tree(Gogo*, Named_object*);
927 // Get the value to return when not explicitly specified. May also
928 // add statements to execute first to STMT_LIST.
930 return_value(Gogo*, Named_object*, Location, tree* stmt_list) const;
932 // Get a tree for the variable holding the defer stack.
934 defer_stack(Location);
936 // Export the function.
938 export_func(Export*, const std::string& name) const;
940 // Export a function with a type.
942 export_func_with_type(Export*, const std::string& name,
943 const Function_type*);
945 // Import a function.
947 import_func(Import*, std::string* pname, Typed_identifier** receiver,
948 Typed_identifier_list** pparameters,
949 Typed_identifier_list** presults, bool* is_varargs);
952 // Type for mapping from label names to Label objects.
953 typedef Unordered_map(std::string, Label*) Labels;
956 make_receiver_parm_decl(Gogo*, Named_object*, tree);
959 copy_parm_to_heap(Gogo*, Named_object*, tree);
962 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
964 typedef std::vector<std::pair<Named_object*,
965 Location> > Closure_fields;
967 // The function's type.
968 Function_type* type_;
969 // The enclosing function. This is NULL when there isn't one, which
970 // is the normal case.
971 Function* enclosing_;
972 // The result variables, if any.
974 // If there is a closure, this is the list of variables which appear
975 // in the closure. This is created by the parser, and then resolved
976 // to a real type when we lower parse trees.
977 Closure_fields closure_fields_;
978 // The closure variable, passed as a parameter using the static
979 // chain parameter. Normally NULL.
980 Named_object* closure_var_;
981 // The outer block of statements in the function.
983 // The source location of the start of the function.
985 // Labels defined or referenced in the function.
987 // The function decl.
989 // The defer stack variable. A pointer to this variable is used to
990 // distinguish the defer stack for one function from another. This
991 // is NULL unless we actually need a defer stack.
992 Temporary_statement* defer_stack_;
993 // True if the result variables are named.
994 bool results_are_named_;
995 // True if this function calls the predeclared recover function.
997 // True if this a thunk built for a function which calls recover.
998 bool is_recover_thunk_;
999 // True if this function already has a recover thunk.
1000 bool has_recover_thunk_;
1003 // A snapshot of the current binding state.
1005 class Bindings_snapshot
1008 Bindings_snapshot(const Block*, Location);
1010 // Report any errors appropriate for a goto from the current binding
1011 // state of B to this one.
1013 check_goto_from(const Block* b, Location);
1015 // Report any errors appropriate for a goto from this binding state
1016 // to the current state of B.
1018 check_goto_to(const Block* b);
1022 check_goto_block(Location, const Block*, const Block*, size_t*);
1025 check_goto_defs(Location, const Block*, size_t, size_t);
1027 // The current block.
1028 const Block* block_;
1029 // The number of names currently defined in each open block.
1030 // Element 0 is this->block_, element 1 is
1031 // this->block_->enclosing(), etc.
1032 std::vector<size_t> counts_;
1033 // The location where this snapshot was taken.
1037 // A function declaration.
1039 class Function_declaration
1042 Function_declaration(Function_type* fntype, Location location)
1043 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1048 { return this->fntype_; }
1052 { return this->location_; }
1056 { return this->asm_name_; }
1058 // Set the assembler name.
1060 set_asm_name(const std::string& asm_name)
1061 { this->asm_name_ = asm_name; }
1063 // Return a decl for the function given an identifier.
1065 get_or_make_decl(Gogo*, Named_object*, tree id);
1067 // Export a function declaration.
1069 export_func(Export* exp, const std::string& name) const
1070 { Function::export_func_with_type(exp, name, this->fntype_); }
1073 // The type of the function.
1074 Function_type* fntype_;
1075 // The location of the declaration.
1077 // The assembler name: this is the name to use in references to the
1078 // function. This is normally empty.
1079 std::string asm_name_;
1080 // The function decl if needed.
1089 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1090 bool is_receiver, Location);
1092 // Get the type of the variable.
1099 // Return whether the type is defined yet.
1102 { return this->type_ != NULL; }
1104 // Get the initial value.
1107 { return this->init_; }
1109 // Return whether there are any preinit statements.
1111 has_pre_init() const
1112 { return this->preinit_ != NULL; }
1114 // Return the preinit statements if any.
1117 { return this->preinit_; }
1119 // Return whether this is a global variable.
1122 { return this->is_global_; }
1124 // Return whether this is a function parameter.
1126 is_parameter() const
1127 { return this->is_parameter_; }
1129 // Return whether this is the receiver parameter of a method.
1132 { return this->is_receiver_; }
1134 // Change this parameter to be a receiver. This is used when
1135 // creating the thunks created for functions which call recover.
1139 go_assert(this->is_parameter_);
1140 this->is_receiver_ = true;
1143 // Change this parameter to not be a receiver. This is used when
1144 // creating the thunks created for functions which call recover.
1146 set_is_not_receiver()
1148 go_assert(this->is_parameter_);
1149 this->is_receiver_ = false;
1152 // Return whether this is the varargs parameter of a function.
1154 is_varargs_parameter() const
1155 { return this->is_varargs_parameter_; }
1157 // Whether this variable's address is taken.
1159 is_address_taken() const
1160 { return this->is_address_taken_; }
1162 // Whether this variable should live in the heap.
1165 { return this->is_address_taken_ && !this->is_global_; }
1167 // Note that something takes the address of this variable.
1170 { this->is_address_taken_ = true; }
1172 // Return whether the address is taken but does not escape.
1174 is_non_escaping_address_taken() const
1175 { return this->is_non_escaping_address_taken_; }
1177 // Note that something takes the address of this variable such that
1178 // the address does not escape the function.
1180 set_non_escaping_address_taken()
1181 { this->is_non_escaping_address_taken_ = true; }
1183 // Get the source location of the variable's declaration.
1186 { return this->location_; }
1188 // Record that this is the varargs parameter of a function.
1190 set_is_varargs_parameter()
1192 go_assert(this->is_parameter_);
1193 this->is_varargs_parameter_ = true;
1196 // Clear the initial value; used for error handling.
1199 { this->init_ = NULL; }
1201 // Set the initial value; used for converting shortcuts.
1203 set_init(Expression* init)
1204 { this->init_ = init; }
1206 // Get the preinit block, a block of statements to be run before the
1207 // initialization expression.
1209 preinit_block(Gogo*);
1211 // Add a statement to be run before the initialization expression.
1212 // This is only used for global variables.
1214 add_preinit_statement(Gogo*, Statement*);
1216 // Lower the initialization expression after parsing is complete.
1218 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1220 // A special case: the init value is used only to determine the
1221 // type. This is used if the variable is defined using := with the
1222 // comma-ok form of a map index or a receive expression. The init
1223 // value is actually the map index expression or receive expression.
1224 // We use this because we may not know the right type at parse time.
1226 set_type_from_init_tuple()
1227 { this->type_from_init_tuple_ = true; }
1229 // Another special case: the init value is used only to determine
1230 // the type. This is used if the variable is defined using := with
1231 // a range clause. The init value is the range expression. The
1232 // type of the variable is the index type of the range expression
1233 // (i.e., the first value returned by a range).
1235 set_type_from_range_index()
1236 { this->type_from_range_index_ = true; }
1238 // Another special case: like set_type_from_range_index, but the
1239 // type is the value type of the range expression (i.e., the second
1240 // value returned by a range).
1242 set_type_from_range_value()
1243 { this->type_from_range_value_ = true; }
1245 // Another special case: the init value is used only to determine
1246 // the type. This is used if the variable is defined using := with
1247 // a case in a select statement. The init value is the channel.
1248 // The type of the variable is the channel's element type.
1250 set_type_from_chan_element()
1251 { this->type_from_chan_element_ = true; }
1253 // After we lower the select statement, we once again set the type
1254 // from the initialization expression.
1256 clear_type_from_chan_element()
1258 go_assert(this->type_from_chan_element_);
1259 this->type_from_chan_element_ = false;
1262 // Note that this variable was created for a type switch clause.
1264 set_is_type_switch_var()
1265 { this->is_type_switch_var_ = true; }
1267 // Traverse the initializer expression.
1269 traverse_expression(Traverse*, unsigned int traverse_mask);
1271 // Determine the type of the variable if necessary.
1275 // Get the backend representation of the variable.
1277 get_backend_variable(Gogo*, Named_object*, const Package*,
1278 const std::string&);
1280 // Get the initial value of the variable as a tree. This may only
1281 // be called if has_pre_init() returns false.
1283 get_init_tree(Gogo*, Named_object* function);
1285 // Return a series of statements which sets the value of the
1286 // variable in DECL. This should only be called is has_pre_init()
1287 // returns true. DECL may be NULL for a sink variable.
1289 get_init_block(Gogo*, Named_object* function, tree decl);
1291 // Export the variable.
1293 export_var(Export*, const std::string& name) const;
1295 // Import a variable.
1297 import_var(Import*, std::string* pname, Type** ptype);
1300 // The type of a tuple.
1302 type_from_tuple(Expression*, bool) const;
1304 // The type of a range.
1306 type_from_range(Expression*, bool, bool) const;
1308 // The element type of a channel.
1310 type_from_chan_element(Expression*, bool) const;
1312 // The variable's type. This may be NULL if the type is set from
1315 // The initial value. This may be NULL if the variable should be
1316 // initialized to the default value for the type.
1318 // Statements to run before the init statement.
1320 // Location of variable definition.
1322 // Backend representation.
1323 Bvariable* backend_;
1324 // Whether this is a global variable.
1325 bool is_global_ : 1;
1326 // Whether this is a function parameter.
1327 bool is_parameter_ : 1;
1328 // Whether this is the receiver parameter of a method.
1329 bool is_receiver_ : 1;
1330 // Whether this is the varargs parameter of a function.
1331 bool is_varargs_parameter_ : 1;
1332 // Whether something takes the address of this variable. For a
1333 // local variable this implies that the variable has to be on the
1335 bool is_address_taken_ : 1;
1336 // Whether something takes the address of this variable such that
1337 // the address does not escape the function.
1338 bool is_non_escaping_address_taken_ : 1;
1339 // True if we have seen this variable in a traversal.
1341 // True if we have lowered the initialization expression.
1342 bool init_is_lowered_ : 1;
1343 // True if init is a tuple used to set the type.
1344 bool type_from_init_tuple_ : 1;
1345 // True if init is a range clause and the type is the index type.
1346 bool type_from_range_index_ : 1;
1347 // True if init is a range clause and the type is the value type.
1348 bool type_from_range_value_ : 1;
1349 // True if init is a channel and the type is the channel's element type.
1350 bool type_from_chan_element_ : 1;
1351 // True if this is a variable created for a type switch case.
1352 bool is_type_switch_var_ : 1;
1353 // True if we have determined types.
1354 bool determined_type_ : 1;
1357 // A variable which is really the name for a function return value, or
1360 class Result_variable
1363 Result_variable(Type* type, Function* function, int index,
1365 : type_(type), function_(function), index_(index), location_(location),
1366 backend_(NULL), is_address_taken_(false),
1367 is_non_escaping_address_taken_(false)
1370 // Get the type of the result variable.
1373 { return this->type_; }
1375 // Get the function that this is associated with.
1378 { return this->function_; }
1380 // Index in the list of function results.
1383 { return this->index_; }
1385 // The location of the variable definition.
1388 { return this->location_; }
1390 // Whether this variable's address is taken.
1392 is_address_taken() const
1393 { return this->is_address_taken_; }
1395 // Note that something takes the address of this variable.
1398 { this->is_address_taken_ = true; }
1400 // Return whether the address is taken but does not escape.
1402 is_non_escaping_address_taken() const
1403 { return this->is_non_escaping_address_taken_; }
1405 // Note that something takes the address of this variable such that
1406 // the address does not escape the function.
1408 set_non_escaping_address_taken()
1409 { this->is_non_escaping_address_taken_ = true; }
1411 // Whether this variable should live in the heap.
1414 { return this->is_address_taken_; }
1416 // Set the function. This is used when cloning functions which call
1419 set_function(Function* function)
1420 { this->function_ = function; }
1422 // Get the backend representation of the variable.
1424 get_backend_variable(Gogo*, Named_object*, const std::string&);
1427 // Type of result variable.
1429 // Function with which this is associated.
1430 Function* function_;
1431 // Index in list of results.
1433 // Where the result variable is defined.
1435 // Backend representation.
1436 Bvariable* backend_;
1437 // Whether something takes the address of this variable.
1438 bool is_address_taken_;
1439 // Whether something takes the address of this variable such that
1440 // the address does not escape the function.
1441 bool is_non_escaping_address_taken_;
1444 // The value we keep for a named constant. This lets us hold a type
1445 // and an expression.
1447 class Named_constant
1450 Named_constant(Type* type, Expression* expr, int iota_value,
1452 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1458 { return this->type_; }
1462 { return this->expr_; }
1466 { return this->iota_value_; }
1470 { return this->location_; }
1472 // Whether we are lowering.
1475 { return this->lowering_; }
1477 // Set that we are lowering.
1480 { this->lowering_ = true; }
1482 // We are no longer lowering.
1485 { this->lowering_ = false; }
1487 // Traverse the expression.
1489 traverse_expression(Traverse*);
1491 // Determine the type of the constant if necessary.
1495 // Indicate that we found and reported an error for this constant.
1499 // Export the constant.
1501 export_const(Export*, const std::string& name) const;
1503 // Import a constant.
1505 import_const(Import*, std::string*, Type**, Expression**);
1508 // The type of the constant.
1510 // The expression for the constant.
1512 // If the predeclared constant iota is used in EXPR_, this is the
1513 // value it will have. We do this because at parse time we don't
1514 // know whether the name "iota" will refer to the predeclared
1515 // constant or to something else. We put in the right value in when
1518 // The location of the definition.
1520 // Whether we are currently lowering this constant.
1524 // A type declaration.
1526 class Type_declaration
1529 Type_declaration(Location location)
1530 : location_(location), in_function_(NULL), methods_(),
1531 issued_warning_(false)
1534 // Return the location.
1537 { return this->location_; }
1539 // Return the function in which this type is declared. This will
1540 // return NULL for a type declared in global scope.
1543 { return this->in_function_; }
1545 // Set the function in which this type is declared.
1547 set_in_function(Named_object* f)
1548 { this->in_function_ = f; }
1550 // Add a method to this type. This is used when methods are defined
1553 add_method(const std::string& name, Function* function);
1555 // Add a method declaration to this type.
1557 add_method_declaration(const std::string& name, Function_type* type,
1560 // Return whether any methods were defined.
1562 has_methods() const;
1564 // Define methods when the real type is known.
1566 define_methods(Named_type*);
1568 // This is called if we are trying to use this type. It returns
1569 // true if we should issue a warning.
1574 typedef std::vector<Named_object*> Methods;
1576 // The location of the type declaration.
1578 // If this type is declared in a function, a pointer back to the
1579 // function in which it is defined.
1580 Named_object* in_function_;
1581 // Methods defined before the type is defined.
1583 // True if we have issued a warning about a use of this type
1584 // declaration when it is undefined.
1585 bool issued_warning_;
1588 // An unknown object. These are created by the parser for forward
1589 // references to names which have not been seen before. In a correct
1590 // program, these will always point to a real definition by the end of
1591 // the parse. Because they point to another Named_object, these may
1592 // only be referenced by Unknown_expression objects.
1597 Unknown_name(Location location)
1598 : location_(location), real_named_object_(NULL)
1601 // Return the location where this name was first seen.
1604 { return this->location_; }
1606 // Return the real named object that this points to, or NULL if it
1607 // was never resolved.
1609 real_named_object() const
1610 { return this->real_named_object_; }
1612 // Set the real named object that this points to.
1614 set_real_named_object(Named_object* no);
1617 // The location where this name was first seen.
1619 // The real named object when it is known.
1624 // A named object named. This is the result of a declaration. We
1625 // don't use a superclass because they all have to be handled
1633 // An uninitialized Named_object. We should never see this.
1634 NAMED_OBJECT_UNINITIALIZED,
1635 // An unknown name. This is used for forward references. In a
1636 // correct program, these will all be resolved by the end of the
1638 NAMED_OBJECT_UNKNOWN,
1643 // A forward type declaration.
1644 NAMED_OBJECT_TYPE_DECLARATION,
1647 // A result variable in a function.
1648 NAMED_OBJECT_RESULT_VAR,
1649 // The blank identifier--the special variable named _.
1653 // A forward func declaration.
1654 NAMED_OBJECT_FUNC_DECLARATION,
1656 NAMED_OBJECT_PACKAGE
1659 // Return the classification.
1661 classification() const
1662 { return this->classification_; }
1668 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1672 { return this->classification_ == NAMED_OBJECT_CONST; }
1676 { return this->classification_ == NAMED_OBJECT_TYPE; }
1679 is_type_declaration() const
1680 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1684 { return this->classification_ == NAMED_OBJECT_VAR; }
1687 is_result_variable() const
1688 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1692 { return this->classification_ == NAMED_OBJECT_SINK; }
1696 { return this->classification_ == NAMED_OBJECT_FUNC; }
1699 is_function_declaration() const
1700 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1704 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1708 static Named_object*
1709 make_unknown_name(const std::string& name, Location);
1711 static Named_object*
1712 make_constant(const Typed_identifier&, const Package*, Expression*,
1715 static Named_object*
1716 make_type(const std::string&, const Package*, Type*, Location);
1718 static Named_object*
1719 make_type_declaration(const std::string&, const Package*, Location);
1721 static Named_object*
1722 make_variable(const std::string&, const Package*, Variable*);
1724 static Named_object*
1725 make_result_variable(const std::string&, Result_variable*);
1727 static Named_object*
1730 static Named_object*
1731 make_function(const std::string&, const Package*, Function*);
1733 static Named_object*
1734 make_function_declaration(const std::string&, const Package*, Function_type*,
1737 static Named_object*
1738 make_package(const std::string& alias, Package* package);
1745 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1746 return this->u_.unknown_value;
1750 unknown_value() const
1752 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1753 return this->u_.unknown_value;
1759 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1760 return this->u_.const_value;
1763 const Named_constant*
1766 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1767 return this->u_.const_value;
1773 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1774 return this->u_.type_value;
1780 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1781 return this->u_.type_value;
1785 type_declaration_value()
1787 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1788 return this->u_.type_declaration;
1791 const Type_declaration*
1792 type_declaration_value() const
1794 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1795 return this->u_.type_declaration;
1801 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1802 return this->u_.var_value;
1808 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1809 return this->u_.var_value;
1815 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1816 return this->u_.result_var_value;
1819 const Result_variable*
1820 result_var_value() const
1822 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1823 return this->u_.result_var_value;
1829 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1830 return this->u_.func_value;
1836 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1837 return this->u_.func_value;
1840 Function_declaration*
1841 func_declaration_value()
1843 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1844 return this->u_.func_declaration_value;
1847 const Function_declaration*
1848 func_declaration_value() const
1850 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1851 return this->u_.func_declaration_value;
1857 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1858 return this->u_.package_value;
1862 package_value() const
1864 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1865 return this->u_.package_value;
1870 { return this->name_; }
1872 // Return the name to use in an error message. The difference is
1873 // that if this Named_object is defined in a different package, this
1874 // will return PACKAGE.NAME.
1876 message_name() const;
1880 { return this->package_; }
1882 // Resolve an unknown value if possible. This returns the same
1883 // Named_object or a new one.
1887 Named_object* ret = this;
1888 if (this->is_unknown())
1890 Named_object* r = this->unknown_value()->real_named_object();
1900 const Named_object* ret = this;
1901 if (this->is_unknown())
1903 const Named_object* r = this->unknown_value()->real_named_object();
1910 // The location where this object was defined or referenced.
1914 // Convert a variable to the backend representation.
1916 get_backend_variable(Gogo*, Named_object* function);
1918 // Return a tree for the external identifier for this object.
1922 // Return a tree representing this object.
1924 get_tree(Gogo*, Named_object* function);
1926 // Define a type declaration.
1928 set_type_value(Named_type*);
1930 // Define a function declaration.
1932 set_function_value(Function*);
1934 // Declare an unknown name as a type declaration.
1938 // Export this object.
1940 export_named_object(Export*) const;
1943 Named_object(const std::string&, const Package*, Classification);
1945 // The name of the object.
1947 // The package that this object is in. This is NULL if it is in the
1948 // file we are compiling.
1949 const Package* package_;
1950 // The type of object this is.
1951 Classification classification_;
1955 Unknown_name* unknown_value;
1956 Named_constant* const_value;
1957 Named_type* type_value;
1958 Type_declaration* type_declaration;
1959 Variable* var_value;
1960 Result_variable* result_var_value;
1961 Function* func_value;
1962 Function_declaration* func_declaration_value;
1963 Package* package_value;
1965 // The DECL tree for this object if we have already converted it.
1969 // A binding contour. This binds names to objects.
1974 // Type for mapping from names to objects.
1975 typedef Unordered_map(std::string, Named_object*) Contour;
1977 Bindings(Bindings* enclosing);
1979 // Add an unknown name.
1981 add_unknown_name(const std::string& name, Location location)
1983 return this->add_named_object(Named_object::make_unknown_name(name,
1989 add_constant(const Typed_identifier& tid, const Package* package,
1990 Expression* expr, int iota_value)
1992 return this->add_named_object(Named_object::make_constant(tid, package,
1999 add_type(const std::string& name, const Package* package, Type* type,
2002 return this->add_named_object(Named_object::make_type(name, package, type,
2006 // Add a named type. This is used for builtin types, and to add an
2007 // imported type to the global scope.
2009 add_named_type(Named_type* named_type);
2011 // Add a type declaration.
2013 add_type_declaration(const std::string& name, const Package* package,
2016 Named_object* no = Named_object::make_type_declaration(name, package,
2018 return this->add_named_object(no);
2023 add_variable(const std::string& name, const Package* package,
2026 return this->add_named_object(Named_object::make_variable(name, package,
2030 // Add a result variable.
2032 add_result_variable(const std::string& name, Result_variable* result)
2034 return this->add_named_object(Named_object::make_result_variable(name,
2040 add_function(const std::string& name, const Package*, Function* function);
2042 // Add a function declaration.
2044 add_function_declaration(const std::string& name, const Package* package,
2045 Function_type* type, Location location);
2047 // Add a package. The location is the location of the import
2050 add_package(const std::string& alias, Package* package)
2052 Named_object* no = Named_object::make_package(alias, package);
2053 return this->add_named_object(no);
2056 // Define a type which was already declared.
2058 define_type(Named_object*, Named_type*);
2060 // Add a method to the list of objects. This is not added to the
2063 add_method(Named_object*);
2065 // Add a named object to this binding.
2067 add_named_object(Named_object* no)
2068 { return this->add_named_object_to_contour(&this->bindings_, no); }
2070 // Clear all names in file scope from the bindings.
2074 // Look up a name in this binding contour and in any enclosing
2075 // binding contours. This returns NULL if the name is not found.
2077 lookup(const std::string&) const;
2079 // Look up a name in this binding contour without looking in any
2080 // enclosing binding contours. Returns NULL if the name is not found.
2082 lookup_local(const std::string&) const;
2086 remove_binding(Named_object*);
2088 // Traverse the tree. See the Traverse class.
2090 traverse(Traverse*, bool is_global);
2092 // Iterate over definitions. This does not include things which
2093 // were only declared.
2095 typedef std::vector<Named_object*>::const_iterator
2096 const_definitions_iterator;
2098 const_definitions_iterator
2099 begin_definitions() const
2100 { return this->named_objects_.begin(); }
2102 const_definitions_iterator
2103 end_definitions() const
2104 { return this->named_objects_.end(); }
2106 // Return the number of definitions.
2108 size_definitions() const
2109 { return this->named_objects_.size(); }
2111 // Return whether there are no definitions.
2113 empty_definitions() const
2114 { return this->named_objects_.empty(); }
2116 // Iterate over declarations. This is everything that has been
2117 // declared, which includes everything which has been defined.
2119 typedef Contour::const_iterator const_declarations_iterator;
2121 const_declarations_iterator
2122 begin_declarations() const
2123 { return this->bindings_.begin(); }
2125 const_declarations_iterator
2126 end_declarations() const
2127 { return this->bindings_.end(); }
2129 // Return the number of declarations.
2131 size_declarations() const
2132 { return this->bindings_.size(); }
2134 // Return whether there are no declarations.
2136 empty_declarations() const
2137 { return this->bindings_.empty(); }
2139 // Return the first declaration.
2142 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2146 add_named_object_to_contour(Contour*, Named_object*);
2149 new_definition(Named_object*, Named_object*);
2151 // Enclosing bindings.
2152 Bindings* enclosing_;
2153 // The list of objects.
2154 std::vector<Named_object*> named_objects_;
2155 // The mapping from names to objects.
2164 Label(const std::string& name)
2165 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2166 refs_(), is_used_(false), blabel_(NULL)
2169 // Return the label's name.
2172 { return this->name_; }
2174 // Return whether the label has been defined.
2177 { return !Linemap::is_unknown_location(this->location_); }
2179 // Return whether the label has been used.
2182 { return this->is_used_; }
2184 // Record that the label is used.
2187 { this->is_used_ = true; }
2189 // Return the location of the definition.
2192 { return this->location_; }
2194 // Return the bindings snapshot.
2197 { return this->snapshot_; }
2199 // Add a snapshot of a goto which refers to this label.
2201 add_snapshot_ref(Bindings_snapshot* snapshot)
2203 go_assert(Linemap::is_unknown_location(this->location_));
2204 this->refs_.push_back(snapshot);
2207 // Return the list of snapshots of goto statements which refer to
2209 const std::vector<Bindings_snapshot*>&
2211 { return this->refs_; }
2213 // Clear the references.
2217 // Define the label at LOCATION with the given bindings snapshot.
2219 define(Location location, Bindings_snapshot* snapshot)
2221 go_assert(Linemap::is_unknown_location(this->location_)
2222 && this->snapshot_ == NULL);
2223 this->location_ = location;
2224 this->snapshot_ = snapshot;
2227 // Return the backend representation for this label.
2229 get_backend_label(Translate_context*);
2231 // Return an expression for the address of this label. This is used
2232 // to get the return address of a deferred function to see whether
2233 // the function may call recover.
2235 get_addr(Translate_context*, Location location);
2238 // The name of the label.
2240 // The location of the definition. This is 0 if the label has not
2241 // yet been defined.
2243 // A snapshot of the set of bindings defined at this label, used to
2244 // issue errors about invalid goto statements.
2245 Bindings_snapshot* snapshot_;
2246 // A list of snapshots of goto statements which refer to this label.
2247 std::vector<Bindings_snapshot*> refs_;
2248 // Whether the label has been used.
2250 // The backend representation.
2254 // An unnamed label. These are used when lowering loops.
2259 Unnamed_label(Location location)
2260 : location_(location), blabel_(NULL)
2263 // Get the location where the label is defined.
2266 { return this->location_; }
2268 // Set the location where the label is defined.
2270 set_location(Location location)
2271 { this->location_ = location; }
2273 // Return a statement which defines this label.
2275 get_definition(Translate_context*);
2277 // Return a goto to this label from LOCATION.
2279 get_goto(Translate_context*, Location location);
2282 // Return the backend representation.
2284 get_blabel(Translate_context*);
2286 // The location where the label is defined.
2288 // The backend representation of this label.
2292 // An imported package.
2297 Package(const std::string& name, const std::string& unique_prefix,
2300 // The real name of this package. This may be different from the
2301 // name in the associated Named_object if the import statement used
2305 { return this->name_; }
2307 // Return the location of the import statement.
2310 { return this->location_; }
2312 // Get the unique prefix used for all symbols exported from this
2315 unique_prefix() const
2317 go_assert(!this->unique_prefix_.empty());
2318 return this->unique_prefix_;
2321 // The priority of this package. The init function of packages with
2322 // lower priority must be run before the init function of packages
2323 // with higher priority.
2326 { return this->priority_; }
2328 // Set the priority.
2330 set_priority(int priority);
2332 // Return the bindings.
2335 { return this->bindings_; }
2337 // Whether some symbol from the package was used.
2340 { return this->used_; }
2342 // Note that some symbol from this package was used.
2345 { this->used_ = true; }
2347 // Clear the used field for the next file.
2350 { this->used_ = false; }
2352 // Whether this package was imported in the current file.
2355 { return this->is_imported_; }
2357 // Note that this package was imported in the current file.
2360 { this->is_imported_ = true; }
2362 // Clear the imported field for the next file.
2365 { this->is_imported_ = false; }
2367 // Whether this package was imported with a name of "_".
2369 uses_sink_alias() const
2370 { return this->uses_sink_alias_; }
2372 // Note that this package was imported with a name of "_".
2374 set_uses_sink_alias()
2375 { this->uses_sink_alias_ = true; }
2377 // Clear the sink alias field for the next file.
2379 clear_uses_sink_alias()
2380 { this->uses_sink_alias_ = false; }
2382 // Look up a name in the package. Returns NULL if the name is not
2385 lookup(const std::string& name) const
2386 { return this->bindings_->lookup(name); }
2388 // Set the location of the package. This is used if it is seen in a
2389 // different import before it is really imported.
2391 set_location(Location location)
2392 { this->location_ = location; }
2394 // Add a constant to the package.
2396 add_constant(const Typed_identifier& tid, Expression* expr)
2397 { return this->bindings_->add_constant(tid, this, expr, 0); }
2399 // Add a type to the package.
2401 add_type(const std::string& name, Type* type, Location location)
2402 { return this->bindings_->add_type(name, this, type, location); }
2404 // Add a type declaration to the package.
2406 add_type_declaration(const std::string& name, Location location)
2407 { return this->bindings_->add_type_declaration(name, this, location); }
2409 // Add a variable to the package.
2411 add_variable(const std::string& name, Variable* variable)
2412 { return this->bindings_->add_variable(name, this, variable); }
2414 // Add a function declaration to the package.
2416 add_function_declaration(const std::string& name, Function_type* type,
2418 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2420 // Determine types of constants.
2425 // The real name of this package.
2427 // The unique prefix for all exported global symbols.
2428 std::string unique_prefix_;
2429 // The names in this package.
2430 Bindings* bindings_;
2431 // The priority of this package. A package has a priority higher
2432 // than the priority of all of the packages that it imports. This
2433 // is used to run init functions in the right order.
2435 // The location of the import statement.
2437 // True if some name from this package was used. This is mutable
2438 // because we can use a package even if we have a const pointer to
2441 // True if this package was imported in the current file.
2443 // True if this package was imported with a name of "_".
2444 bool uses_sink_alias_;
2447 // Return codes for the traversal functions. This is not an enum
2448 // because we want to be able to declare traversal functions in other
2449 // header files without including this one.
2451 // Continue traversal as usual.
2452 const int TRAVERSE_CONTINUE = -1;
2455 const int TRAVERSE_EXIT = 0;
2457 // Continue traversal, but skip components of the current object.
2458 // E.g., if this is returned by Traverse::statement, we do not
2459 // traverse the expressions in the statement even if
2460 // traverse_expressions is set in the traverse_mask.
2461 const int TRAVERSE_SKIP_COMPONENTS = 1;
2463 // This class is used when traversing the parse tree. The caller uses
2464 // a subclass which overrides functions as desired.
2469 // These bitmasks say what to traverse.
2470 static const unsigned int traverse_variables = 0x1;
2471 static const unsigned int traverse_constants = 0x2;
2472 static const unsigned int traverse_functions = 0x4;
2473 static const unsigned int traverse_blocks = 0x8;
2474 static const unsigned int traverse_statements = 0x10;
2475 static const unsigned int traverse_expressions = 0x20;
2476 static const unsigned int traverse_types = 0x40;
2478 Traverse(unsigned int traverse_mask)
2479 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2482 virtual ~Traverse();
2484 // The bitmask of what to traverse.
2486 traverse_mask() const
2487 { return this->traverse_mask_; }
2489 // Record that we are going to traverse a type. This returns true
2490 // if the type has already been seen in this traversal. This is
2491 // required because types, unlike expressions, can form a circular
2494 remember_type(const Type*);
2496 // Record that we are going to see an expression. This returns true
2497 // if the expression has already been seen in this traversal. This
2498 // is only needed for cases where multiple expressions can point to
2501 remember_expression(const Expression*);
2503 // These functions return one of the TRAVERSE codes defined above.
2505 // If traverse_variables is set in the mask, this is called for
2506 // every variable in the tree.
2508 variable(Named_object*);
2510 // If traverse_constants is set in the mask, this is called for
2511 // every named constant in the tree. The bool parameter is true for
2512 // a global constant.
2514 constant(Named_object*, bool);
2516 // If traverse_functions is set in the mask, this is called for
2517 // every function in the tree.
2519 function(Named_object*);
2521 // If traverse_blocks is set in the mask, this is called for every
2522 // block in the tree.
2526 // If traverse_statements is set in the mask, this is called for
2527 // every statement in the tree.
2529 statement(Block*, size_t* index, Statement*);
2531 // If traverse_expressions is set in the mask, this is called for
2532 // every expression in the tree.
2534 expression(Expression**);
2536 // If traverse_types is set in the mask, this is called for every
2537 // type in the tree.
2542 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2543 Type_identical) Types_seen;
2545 typedef Unordered_set(const Expression*) Expressions_seen;
2547 // Bitmask of what sort of objects to traverse.
2548 unsigned int traverse_mask_;
2549 // Types which have been seen in this traversal.
2550 Types_seen* types_seen_;
2551 // Expressions which have been seen in this traversal.
2552 Expressions_seen* expressions_seen_;
2555 // A class which makes it easier to insert new statements before the
2556 // current statement during a traversal.
2558 class Statement_inserter
2561 // Empty constructor.
2562 Statement_inserter()
2563 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2566 // Constructor for a statement in a block.
2567 Statement_inserter(Block* block, size_t *pindex)
2568 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2571 // Constructor for a global variable.
2572 Statement_inserter(Gogo* gogo, Variable* var)
2573 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2574 { go_assert(var->is_global()); }
2576 // We use the default copy constructor and assignment operator.
2578 // Insert S before the statement we are traversing, or before the
2579 // initialization expression of a global variable.
2581 insert(Statement* s);
2584 // The block that the statement is in.
2586 // The index of the statement that we are traversing.
2588 // The IR, needed when looking at an initializer expression for a
2591 // The global variable, when looking at an initializer expression.
2595 // When translating the gogo IR into the backend data structure, this
2596 // is the context we pass down the blocks and statements.
2598 class Translate_context
2601 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2603 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2604 block_(block), bblock_(bblock), is_const_(false)
2611 { return this->gogo_; }
2615 { return this->backend_; }
2619 { return this->function_; }
2623 { return this->block_; }
2627 { return this->bblock_; }
2631 { return this->is_const_; }
2633 // Make a constant context.
2636 { this->is_const_ = true; }
2639 // The IR for the entire compilation unit.
2641 // The generator for the backend data structures.
2643 // The function we are currently translating. NULL if not in a
2644 // function, e.g., the initializer of a global variable.
2645 Named_object* function_;
2646 // The block we are currently translating. NULL if not in a
2649 // The backend representation of the current block. NULL if block_
2652 // Whether this is being evaluated in a constant context. This is
2653 // used for type descriptor initializers.
2657 // Runtime error codes. These must match the values in
2658 // libgo/runtime/go-runtime-error.c.
2660 // Slice index out of bounds: negative or larger than the length of
2662 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2664 // Array index out of bounds.
2665 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2667 // String index out of bounds.
2668 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2670 // Slice slice out of bounds: negative or larger than the length of
2671 // the slice or high bound less than low bound.
2672 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2674 // Array slice out of bounds.
2675 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2677 // String slice out of bounds.
2678 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2680 // Dereference of nil pointer. This is used when there is a
2681 // dereference of a pointer to a very large struct or array, to ensure
2682 // that a gigantic array is not used a proxy to access random memory
2684 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2686 // Slice length or capacity out of bounds in make: negative or
2687 // overflow or length greater than capacity.
2688 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2690 // Map capacity out of bounds in make: negative or overflow.
2691 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2693 // Channel capacity out of bounds in make: negative or overflow.
2694 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2696 // This is used by some of the langhooks.
2697 extern Gogo* go_get_gogo();
2699 // Whether we have seen any errors. FIXME: Replace with a backend
2701 extern bool saw_errors();
2703 #endif // !defined(GO_GOGO_H)