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;
25 class Bindings_snapshot;
31 class Struct_field_list;
37 class Forward_declaration_type;
42 class Translate_context;
52 // This file declares the basic classes used to hold the internal
53 // representation of Go which is built by the parser.
55 // An initialization function for an imported package. This is a
56 // magic function which initializes variables and runs the "init"
62 Import_init(const std::string& package_name, const std::string& init_name,
64 : package_name_(package_name), init_name_(init_name), priority_(priority)
67 // The name of the package being imported.
70 { return this->package_name_; }
72 // The name of the package's init function.
75 { return this->init_name_; }
77 // The priority of the initialization function. Functions with a
78 // lower priority number must be run first.
81 { return this->priority_; }
84 // The name of the package being imported.
85 std::string package_name_;
86 // The name of the package's init function.
87 std::string init_name_;
92 // For sorting purposes.
95 operator<(const Import_init& i1, const Import_init& i2)
97 if (i1.priority() < i2.priority())
99 if (i1.priority() > i2.priority())
101 if (i1.package_name() != i2.package_name())
102 return i1.package_name() < i2.package_name();
103 return i1.init_name() < i2.init_name();
106 // The holder for the internal representation of the entire
112 // Create the IR, passing in the sizes of the types "int" and
113 // "uintptr" in bits.
114 Gogo(Backend* backend, int int_type_size, int pointer_size);
116 // Get the backend generator.
119 { return this->backend_; }
121 // Get the package name.
123 package_name() const;
125 // Set the package name.
127 set_package_name(const std::string&, source_location);
129 // Return whether this is the "main" package.
131 is_main_package() const;
133 // If necessary, adjust the name to use for a hidden symbol. We add
134 // a prefix of the package name, so that hidden symbols in different
135 // packages do not collide.
137 pack_hidden_name(const std::string& name, bool is_exported) const
141 : ('.' + this->unique_prefix()
142 + '.' + this->package_name()
146 // Unpack a name which may have been hidden. Returns the
147 // user-visible name of the object.
149 unpack_hidden_name(const std::string& name)
150 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
152 // Return whether a possibly packed name is hidden.
154 is_hidden_name(const std::string& name)
155 { return name[0] == '.'; }
157 // Return the package prefix of a hidden name.
159 hidden_name_prefix(const std::string& name)
161 go_assert(Gogo::is_hidden_name(name));
162 return name.substr(1, name.rfind('.') - 1);
165 // Given a name which may or may not have been hidden, return the
166 // name to use in an error message.
168 message_name(const std::string& name);
170 // Return whether a name is the blank identifier _.
172 is_sink_name(const std::string& name)
174 return (name[0] == '.'
175 && name[name.length() - 1] == '_'
176 && name[name.length() - 2] == '.');
179 // Return the unique prefix to use for all exported symbols.
181 unique_prefix() const;
183 // Set the unique prefix.
185 set_unique_prefix(const std::string&);
187 // Return the priority to use for the package we are compiling.
188 // This is two more than the largest priority of any package we
191 package_priority() const;
193 // Import a package. FILENAME is the file name argument, LOCAL_NAME
194 // is the local name to give to the package. If LOCAL_NAME is empty
195 // the declarations are added to the global scope.
197 import_package(const std::string& filename, const std::string& local_name,
198 bool is_local_name_exported, source_location);
200 // Whether we are the global binding level.
202 in_global_scope() const;
204 // Look up a name in the current binding contours.
206 lookup(const std::string&, Named_object** pfunction) const;
208 // Look up a name in the current block.
210 lookup_in_block(const std::string&) const;
212 // Look up a name in the global namespace--the universal scope.
214 lookup_global(const char*) const;
216 // Add a new imported package. REAL_NAME is the real name of the
217 // package. ALIAS is the alias of the package; this may be the same
218 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
219 // this package should be added to the global namespace; this is
220 // true if the alias is ".". LOCATION is the location of the import
221 // statement. This returns the new package, or NULL on error.
223 add_imported_package(const std::string& real_name, const std::string& alias,
224 bool is_alias_exported,
225 const std::string& unique_prefix,
226 source_location location,
227 bool* padd_to_globals);
229 // Register a package. This package may or may not be imported.
230 // This returns the Package structure for the package, creating if
233 register_package(const std::string& name, const std::string& unique_prefix,
236 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
237 // method function should be added to the type of its receiver.
239 start_function(const std::string& name, Function_type* type,
240 bool add_method_to_type, source_location);
242 // Finish compiling a function.
244 finish_function(source_location);
246 // Return the current function.
248 current_function() const;
250 // Return the current block.
254 // Start a new block. This is not initially associated with a
257 start_block(source_location);
259 // Finish the current block and return it.
261 finish_block(source_location);
263 // Declare an unknown name. This is used while parsing. The name
264 // must be resolved by the end of the parse. Unknown names are
265 // always added at the package level.
267 add_unknown_name(const std::string& name, source_location);
269 // Declare a function.
271 declare_function(const std::string&, Function_type*, source_location);
275 add_label_definition(const std::string&, source_location);
277 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
278 // report errors for a goto from the current location to the label
281 add_label_reference(const std::string&, source_location,
282 bool issue_goto_errors);
284 // Return a snapshot of the current binding state.
286 bindings_snapshot(source_location);
288 // Add a statement to the current block.
290 add_statement(Statement*);
292 // Add a block to the current block.
294 add_block(Block*, source_location);
298 add_constant(const Typed_identifier&, Expression*, int iota_value);
302 add_type(const std::string&, Type*, source_location);
304 // Add a named type. This is used for builtin types, and to add an
305 // imported type to the global scope.
307 add_named_type(Named_type*);
311 declare_type(const std::string&, source_location);
313 // Declare a type at the package level. This is used when the
314 // parser sees an unknown name where a type name is required.
316 declare_package_type(const std::string&, source_location);
318 // Define a type which was already declared.
320 define_type(Named_object*, Named_type*);
324 add_variable(const std::string&, Variable*);
326 // Add a sink--a reference to the blank identifier _.
330 // Add a named object to the current namespace. This is used for
331 // import . "package".
333 add_named_object(Named_object*);
335 // Return a name to use for a thunk function. A thunk function is
336 // one we create during the compilation, for a go statement or a
337 // defer statement or a method expression.
341 // Return whether an object is a thunk.
343 is_thunk(const Named_object*);
345 // Note that we've seen an interface type. This is used to build
346 // all required interface method tables.
348 record_interface_type(Interface_type*);
350 // Note that we need an initialization function.
353 { this->need_init_fn_ = true; }
355 // Clear out all names in file scope. This is called when we start
356 // parsing a new file.
360 // Traverse the tree. See the Traverse class.
364 // Define the predeclared global names.
366 define_global_names();
368 // Verify and complete all types.
372 // Lower the parse tree.
376 // Lower all the statements in a block.
378 lower_block(Named_object* function, Block*);
380 // Lower an expression.
382 lower_expression(Named_object* function, Statement_inserter*, Expression**);
386 lower_constant(Named_object*);
388 // Finalize the method lists and build stub methods for named types.
392 // Work out the types to use for unspecified variables and
397 // Type check the program.
401 // Check the types in a single block. This is used for complicated
404 check_types_in_block(Block*);
406 // Check for return statements.
408 check_return_statements();
414 // Add an import control function for an imported package to the
417 add_import_init_fn(const std::string& package_name,
418 const std::string& init_name, int prio);
420 // Turn short-cut operators (&&, ||) into explicit if statements.
424 // Use temporary variables to force order of evaluation.
428 // Build thunks for functions which call recover.
430 build_recover_thunks();
432 // Simplify statements which might use thunks: go and defer
435 simplify_thunk_statements();
437 // Dump AST if -fgo-dump-ast is set
439 dump_ast(const char* basename);
441 // Convert named types to the backend representation.
443 convert_named_types();
445 // Convert named types in a list of bindings.
447 convert_named_types_in_bindings(Bindings*);
449 // True if named types have been converted to the backend
452 named_types_are_converted() const
453 { return this->named_types_are_converted_; }
455 // Write out the global values.
459 // Create trees for implicit builtin functions.
461 define_builtin_function_trees();
463 // Build a call to a builtin function. PDECL should point to a NULL
464 // initialized static pointer which will hold the fndecl. NAME is
465 // the name of the function. NARGS is the number of arguments.
466 // RETTYPE is the return type. It is followed by NARGS pairs of
467 // type and argument (both trees).
469 call_builtin(tree* pdecl, source_location, const char* name, int nargs,
472 // Build a call to the runtime error function.
474 runtime_error(int code, source_location);
476 // Build a builtin struct with a list of fields.
478 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
481 // Mark a function declaration as a builtin library function.
483 mark_fndecl_as_builtin_library(tree fndecl);
485 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
486 // the slice. VALUES points to the values. COUNT is the size,
487 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
490 slice_constructor(tree slice_type_tree, tree values, tree count,
493 // Build required interface method tables.
495 build_interface_method_tables();
497 // Build an interface method table for a type: a list of function
498 // pointers, one for each interface method. This returns a decl.
500 interface_method_table_for_type(const Interface_type*, Named_type*,
503 // Return a tree which allocate SIZE bytes to hold values of type
506 allocate_memory(Type *type, tree size, source_location);
508 // Return a type to use for pointer to const char.
510 const_char_pointer_type_tree();
512 // Build a string constant with the right type.
514 string_constant_tree(const std::string&);
516 // Build a Go string constant. This returns a pointer to the
519 go_string_constant_tree(const std::string&);
521 // Receive a value from a channel.
523 receive_from_channel(tree type_tree, tree channel, bool for_select,
526 // Return a tree for receiving an integer on a channel.
528 receive_as_64bit_integer(tree type, tree channel, bool blocking,
531 // Make a trampoline which calls FNADDR passing CLOSURE.
533 make_trampoline(tree fnaddr, tree closure, source_location);
536 // During parsing, we keep a stack of functions. Each function on
537 // the stack is one that we are currently parsing. For each
538 // function, we keep track of the current stack of blocks.
542 Named_object* function;
543 // The stack of active blocks in the function.
544 std::vector<Block*> blocks;
547 // The stack of functions.
548 typedef std::vector<Open_function> Open_functions;
550 // Set up the built-in unsafe package.
552 import_unsafe(const std::string&, bool is_exported, source_location);
554 // Add a new imported package.
556 add_package(const std::string& real_name, const std::string& alias,
557 const std::string& unique_prefix, source_location location);
559 // Return the current binding contour.
564 current_bindings() const;
566 // Get the name of the magic initialization function.
570 // Get the decl for the magic initialization function.
572 initialization_function_decl();
574 // Write the magic initialization function.
576 write_initialization_function(tree fndecl, tree init_stmt_list);
578 // Initialize imported packages.
582 // Register variables with the garbage collector.
584 register_gc_vars(const std::vector<Named_object*>&, tree*);
586 // Build a pointer to a Go string constant. This returns a pointer
589 ptr_go_string_constant_tree(const std::string&);
591 // Return the type of a trampoline.
593 trampoline_type_tree();
595 // Type used to map import names to packages.
596 typedef std::map<std::string, Package*> Imports;
598 // Type used to map package names to packages.
599 typedef std::map<std::string, Package*> Packages;
601 // Type used to map special names in the sys package.
602 typedef std::map<std::string, std::string> Sys_names;
604 // The backend generator.
606 // The package we are compiling.
608 // The list of currently open functions during parsing.
609 Open_functions functions_;
610 // The global binding contour. This includes the builtin functions
611 // and the package we are compiling.
613 // Mapping from import file names to packages.
615 // Whether the magic unsafe package was imported.
616 bool imported_unsafe_;
617 // Mapping from package names we have seen to packages. This does
618 // not include the package we are compiling.
620 // The functions named "init", if there are any.
621 std::vector<Named_object*> init_functions_;
622 // Whether we need a magic initialization function.
624 // The name of the magic initialization function.
625 std::string init_fn_name_;
626 // A list of import control variables for packages that we import.
627 std::set<Import_init> imported_init_fns_;
628 // The unique prefix used for all global symbols.
629 std::string unique_prefix_;
630 // Whether an explicit unique prefix was set by -fgo-prefix.
631 bool unique_prefix_specified_;
632 // A list of interface types defined while parsing.
633 std::vector<Interface_type*> interface_types_;
634 // Whether named types have been converted.
635 bool named_types_are_converted_;
638 // A block of statements.
643 Block(Block* enclosing, source_location);
645 // Return the enclosing block.
648 { return this->enclosing_; }
650 // Return the bindings of the block.
653 { return this->bindings_; }
657 { return this->bindings_; }
659 // Look at the block's statements.
660 const std::vector<Statement*>*
662 { return &this->statements_; }
664 // Return the start location. This is normally the location of the
665 // left curly brace which starts the block.
667 start_location() const
668 { return this->start_location_; }
670 // Return the end location. This is normally the location of the
671 // right curly brace which ends the block.
674 { return this->end_location_; }
676 // Add a statement to the block.
678 add_statement(Statement*);
680 // Add a statement to the front of the block.
682 add_statement_at_front(Statement*);
684 // Replace a statement in a block.
686 replace_statement(size_t index, Statement*);
688 // Add a Statement before statement number INDEX.
690 insert_statement_before(size_t index, Statement*);
692 // Add a Statement after statement number INDEX.
694 insert_statement_after(size_t index, Statement*);
696 // Set the end location of the block.
698 set_end_location(source_location location)
699 { this->end_location_ = location; }
701 // Traverse the tree.
705 // Set final types for unspecified variables and constants.
709 // Return true if execution of this block may fall through to the
712 may_fall_through() const;
714 // Convert the block to the backend representation.
716 get_backend(Translate_context*);
718 // Iterate over statements.
720 typedef std::vector<Statement*>::iterator iterator;
724 { return this->statements_.begin(); }
728 { return this->statements_.end(); }
733 // Statements in the block.
734 std::vector<Statement*> statements_;
737 // Location of start of block.
738 source_location start_location_;
739 // Location of end of block.
740 source_location end_location_;
748 Function(Function_type* type, Function*, Block*, source_location);
750 // Return the function's type.
753 { return this->type_; }
755 // Return the enclosing function if there is one.
758 { return this->enclosing_; }
760 // Set the enclosing function. This is used when building thunks
761 // for functions which call recover.
763 set_enclosing(Function* enclosing)
765 go_assert(this->enclosing_ == NULL);
766 this->enclosing_ = enclosing;
769 // The result variables.
770 typedef std::vector<Named_object*> Results;
772 // Create the result variables in the outer block.
774 create_result_variables(Gogo*);
776 // Update the named result variables when cloning a function which
779 update_result_variables();
781 // Return the result variables.
784 { return this->results_; }
786 // Whether the result variables have names.
788 results_are_named() const
789 { return this->results_are_named_; }
791 // Add a new field to the closure variable.
793 add_closure_field(Named_object* var, source_location loc)
794 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
796 // Whether this function needs a closure.
798 needs_closure() const
799 { return !this->closure_fields_.empty(); }
801 // Return the closure variable, creating it if necessary. This is
802 // passed to the function as a static chain parameter.
806 // Set the closure variable. This is used when building thunks for
807 // functions which call recover.
809 set_closure_var(Named_object* v)
811 go_assert(this->closure_var_ == NULL);
812 this->closure_var_ = v;
815 // Return the variable for a reference to field INDEX in the closure
818 enclosing_var(unsigned int index)
820 go_assert(index < this->closure_fields_.size());
821 return closure_fields_[index].first;
824 // Set the type of the closure variable if there is one.
828 // Get the block of statements associated with the function.
831 { return this->block_; }
833 // Get the location of the start of the function.
836 { return this->location_; }
838 // Return whether this function is actually a method.
842 // Add a label definition to the function.
844 add_label_definition(Gogo*, const std::string& label_name, source_location);
846 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
847 // if we should report errors for a goto from the current location
848 // to the label location.
850 add_label_reference(Gogo*, const std::string& label_name,
851 source_location, bool issue_goto_errors);
853 // Warn about labels that are defined but not used.
855 check_labels() const;
857 // Whether this function calls the predeclared recover function.
859 calls_recover() const
860 { return this->calls_recover_; }
862 // Record that this function calls the predeclared recover function.
863 // This is set during the lowering pass.
866 { this->calls_recover_ = true; }
868 // Whether this is a recover thunk function.
870 is_recover_thunk() const
871 { return this->is_recover_thunk_; }
873 // Record that this is a thunk built for a function which calls
876 set_is_recover_thunk()
877 { this->is_recover_thunk_ = true; }
879 // Whether this function already has a recover thunk.
881 has_recover_thunk() const
882 { return this->has_recover_thunk_; }
884 // Record that this function already has a recover thunk.
886 set_has_recover_thunk()
887 { this->has_recover_thunk_ = true; }
889 // Swap with another function. Used only for the thunk which calls
892 swap_for_recover(Function *);
894 // Traverse the tree.
898 // Determine types in the function.
902 // Return the function's decl given an identifier.
904 get_or_make_decl(Gogo*, Named_object*, tree id);
906 // Return the function's decl after it has been built.
910 go_assert(this->fndecl_ != NULL);
911 return this->fndecl_;
914 // Set the function decl to hold a tree of the function code.
916 build_tree(Gogo*, Named_object*);
918 // Get the value to return when not explicitly specified. May also
919 // add statements to execute first to STMT_LIST.
921 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
923 // Get a tree for the variable holding the defer stack.
925 defer_stack(source_location);
927 // Export the function.
929 export_func(Export*, const std::string& name) const;
931 // Export a function with a type.
933 export_func_with_type(Export*, const std::string& name,
934 const Function_type*);
936 // Import a function.
938 import_func(Import*, std::string* pname, Typed_identifier** receiver,
939 Typed_identifier_list** pparameters,
940 Typed_identifier_list** presults, bool* is_varargs);
943 // Type for mapping from label names to Label objects.
944 typedef Unordered_map(std::string, Label*) Labels;
947 make_receiver_parm_decl(Gogo*, Named_object*, tree);
950 copy_parm_to_heap(Gogo*, Named_object*, tree);
953 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
955 typedef std::vector<std::pair<Named_object*,
956 source_location> > Closure_fields;
958 // The function's type.
959 Function_type* type_;
960 // The enclosing function. This is NULL when there isn't one, which
961 // is the normal case.
962 Function* enclosing_;
963 // The result variables, if any.
965 // If there is a closure, this is the list of variables which appear
966 // in the closure. This is created by the parser, and then resolved
967 // to a real type when we lower parse trees.
968 Closure_fields closure_fields_;
969 // The closure variable, passed as a parameter using the static
970 // chain parameter. Normally NULL.
971 Named_object* closure_var_;
972 // The outer block of statements in the function.
974 // The source location of the start of the function.
975 source_location location_;
976 // Labels defined or referenced in the function.
978 // The function decl.
980 // The defer stack variable. A pointer to this variable is used to
981 // distinguish the defer stack for one function from another. This
982 // is NULL unless we actually need a defer stack.
983 Temporary_statement* defer_stack_;
984 // True if the result variables are named.
985 bool results_are_named_;
986 // True if this function calls the predeclared recover function.
988 // True if this a thunk built for a function which calls recover.
989 bool is_recover_thunk_;
990 // True if this function already has a recover thunk.
991 bool has_recover_thunk_;
994 // A snapshot of the current binding state.
996 class Bindings_snapshot
999 Bindings_snapshot(const Block*, source_location);
1001 // Report any errors appropriate for a goto from the current binding
1002 // state of B to this one.
1004 check_goto_from(const Block* b, source_location);
1006 // Report any errors appropriate for a goto from this binding state
1007 // to the current state of B.
1009 check_goto_to(const Block* b);
1013 check_goto_block(source_location, const Block*, const Block*, size_t*);
1016 check_goto_defs(source_location, const Block*, size_t, size_t);
1018 // The current block.
1019 const Block* block_;
1020 // The number of names currently defined in each open block.
1021 // Element 0 is this->block_, element 1 is
1022 // this->block_->enclosing(), etc.
1023 std::vector<size_t> counts_;
1024 // The location where this snapshot was taken.
1025 source_location location_;
1028 // A function declaration.
1030 class Function_declaration
1033 Function_declaration(Function_type* fntype, source_location location)
1034 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1039 { return this->fntype_; }
1043 { return this->location_; }
1047 { return this->asm_name_; }
1049 // Set the assembler name.
1051 set_asm_name(const std::string& asm_name)
1052 { this->asm_name_ = asm_name; }
1054 // Return a decl for the function given an identifier.
1056 get_or_make_decl(Gogo*, Named_object*, tree id);
1058 // Export a function declaration.
1060 export_func(Export* exp, const std::string& name) const
1061 { Function::export_func_with_type(exp, name, this->fntype_); }
1064 // The type of the function.
1065 Function_type* fntype_;
1066 // The location of the declaration.
1067 source_location location_;
1068 // The assembler name: this is the name to use in references to the
1069 // function. This is normally empty.
1070 std::string asm_name_;
1071 // The function decl if needed.
1080 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1081 bool is_receiver, source_location);
1083 // Get the type of the variable.
1090 // Return whether the type is defined yet.
1093 { return this->type_ != NULL; }
1095 // Get the initial value.
1098 { return this->init_; }
1100 // Return whether there are any preinit statements.
1102 has_pre_init() const
1103 { return this->preinit_ != NULL; }
1105 // Return the preinit statements if any.
1108 { return this->preinit_; }
1110 // Return whether this is a global variable.
1113 { return this->is_global_; }
1115 // Return whether this is a function parameter.
1117 is_parameter() const
1118 { return this->is_parameter_; }
1120 // Return whether this is the receiver parameter of a method.
1123 { return this->is_receiver_; }
1125 // Change this parameter to be a receiver. This is used when
1126 // creating the thunks created for functions which call recover.
1130 go_assert(this->is_parameter_);
1131 this->is_receiver_ = true;
1134 // Change this parameter to not be a receiver. This is used when
1135 // creating the thunks created for functions which call recover.
1137 set_is_not_receiver()
1139 go_assert(this->is_parameter_);
1140 this->is_receiver_ = false;
1143 // Return whether this is the varargs parameter of a function.
1145 is_varargs_parameter() const
1146 { return this->is_varargs_parameter_; }
1148 // Whether this variable's address is taken.
1150 is_address_taken() const
1151 { return this->is_address_taken_; }
1153 // Whether this variable should live in the heap.
1156 { return this->is_address_taken_ && !this->is_global_; }
1158 // Note that something takes the address of this variable.
1161 { this->is_address_taken_ = true; }
1163 // Return whether the address is taken but does not escape.
1165 is_non_escaping_address_taken() const
1166 { return this->is_non_escaping_address_taken_; }
1168 // Note that something takes the address of this variable such that
1169 // the address does not escape the function.
1171 set_non_escaping_address_taken()
1172 { this->is_non_escaping_address_taken_ = true; }
1174 // Get the source location of the variable's declaration.
1177 { return this->location_; }
1179 // Record that this is the varargs parameter of a function.
1181 set_is_varargs_parameter()
1183 go_assert(this->is_parameter_);
1184 this->is_varargs_parameter_ = true;
1187 // Clear the initial value; used for error handling.
1190 { this->init_ = NULL; }
1192 // Set the initial value; used for converting shortcuts.
1194 set_init(Expression* init)
1195 { this->init_ = init; }
1197 // Get the preinit block, a block of statements to be run before the
1198 // initialization expression.
1200 preinit_block(Gogo*);
1202 // Add a statement to be run before the initialization expression.
1203 // This is only used for global variables.
1205 add_preinit_statement(Gogo*, Statement*);
1207 // Lower the initialization expression after parsing is complete.
1209 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1211 // A special case: the init value is used only to determine the
1212 // type. This is used if the variable is defined using := with the
1213 // comma-ok form of a map index or a receive expression. The init
1214 // value is actually the map index expression or receive expression.
1215 // We use this because we may not know the right type at parse time.
1217 set_type_from_init_tuple()
1218 { this->type_from_init_tuple_ = true; }
1220 // Another special case: the init value is used only to determine
1221 // the type. This is used if the variable is defined using := with
1222 // a range clause. The init value is the range expression. The
1223 // type of the variable is the index type of the range expression
1224 // (i.e., the first value returned by a range).
1226 set_type_from_range_index()
1227 { this->type_from_range_index_ = true; }
1229 // Another special case: like set_type_from_range_index, but the
1230 // type is the value type of the range expression (i.e., the second
1231 // value returned by a range).
1233 set_type_from_range_value()
1234 { this->type_from_range_value_ = true; }
1236 // Another special case: the init value is used only to determine
1237 // the type. This is used if the variable is defined using := with
1238 // a case in a select statement. The init value is the channel.
1239 // The type of the variable is the channel's element type.
1241 set_type_from_chan_element()
1242 { this->type_from_chan_element_ = true; }
1244 // After we lower the select statement, we once again set the type
1245 // from the initialization expression.
1247 clear_type_from_chan_element()
1249 go_assert(this->type_from_chan_element_);
1250 this->type_from_chan_element_ = false;
1253 // Note that this variable was created for a type switch clause.
1255 set_is_type_switch_var()
1256 { this->is_type_switch_var_ = true; }
1258 // Traverse the initializer expression.
1260 traverse_expression(Traverse*, unsigned int traverse_mask);
1262 // Determine the type of the variable if necessary.
1266 // Get the backend representation of the variable.
1268 get_backend_variable(Gogo*, Named_object*, const Package*,
1269 const std::string&);
1271 // Get the initial value of the variable as a tree. This may only
1272 // be called if has_pre_init() returns false.
1274 get_init_tree(Gogo*, Named_object* function);
1276 // Return a series of statements which sets the value of the
1277 // variable in DECL. This should only be called is has_pre_init()
1278 // returns true. DECL may be NULL for a sink variable.
1280 get_init_block(Gogo*, Named_object* function, tree decl);
1282 // Export the variable.
1284 export_var(Export*, const std::string& name) const;
1286 // Import a variable.
1288 import_var(Import*, std::string* pname, Type** ptype);
1291 // The type of a tuple.
1293 type_from_tuple(Expression*, bool) const;
1295 // The type of a range.
1297 type_from_range(Expression*, bool, bool) const;
1299 // The element type of a channel.
1301 type_from_chan_element(Expression*, bool) const;
1303 // The variable's type. This may be NULL if the type is set from
1306 // The initial value. This may be NULL if the variable should be
1307 // initialized to the default value for the type.
1309 // Statements to run before the init statement.
1311 // Location of variable definition.
1312 source_location location_;
1313 // Backend representation.
1314 Bvariable* backend_;
1315 // Whether this is a global variable.
1316 bool is_global_ : 1;
1317 // Whether this is a function parameter.
1318 bool is_parameter_ : 1;
1319 // Whether this is the receiver parameter of a method.
1320 bool is_receiver_ : 1;
1321 // Whether this is the varargs parameter of a function.
1322 bool is_varargs_parameter_ : 1;
1323 // Whether something takes the address of this variable. For a
1324 // local variable this implies that the variable has to be on the
1326 bool is_address_taken_ : 1;
1327 // Whether something takes the address of this variable such that
1328 // the address does not escape the function.
1329 bool is_non_escaping_address_taken_ : 1;
1330 // True if we have seen this variable in a traversal.
1332 // True if we have lowered the initialization expression.
1333 bool init_is_lowered_ : 1;
1334 // True if init is a tuple used to set the type.
1335 bool type_from_init_tuple_ : 1;
1336 // True if init is a range clause and the type is the index type.
1337 bool type_from_range_index_ : 1;
1338 // True if init is a range clause and the type is the value type.
1339 bool type_from_range_value_ : 1;
1340 // True if init is a channel and the type is the channel's element type.
1341 bool type_from_chan_element_ : 1;
1342 // True if this is a variable created for a type switch case.
1343 bool is_type_switch_var_ : 1;
1344 // True if we have determined types.
1345 bool determined_type_ : 1;
1348 // A variable which is really the name for a function return value, or
1351 class Result_variable
1354 Result_variable(Type* type, Function* function, int index,
1355 source_location location)
1356 : type_(type), function_(function), index_(index), location_(location),
1357 backend_(NULL), is_address_taken_(false),
1358 is_non_escaping_address_taken_(false)
1361 // Get the type of the result variable.
1364 { return this->type_; }
1366 // Get the function that this is associated with.
1369 { return this->function_; }
1371 // Index in the list of function results.
1374 { return this->index_; }
1376 // The location of the variable definition.
1379 { return this->location_; }
1381 // Whether this variable's address is taken.
1383 is_address_taken() const
1384 { return this->is_address_taken_; }
1386 // Note that something takes the address of this variable.
1389 { this->is_address_taken_ = true; }
1391 // Return whether the address is taken but does not escape.
1393 is_non_escaping_address_taken() const
1394 { return this->is_non_escaping_address_taken_; }
1396 // Note that something takes the address of this variable such that
1397 // the address does not escape the function.
1399 set_non_escaping_address_taken()
1400 { this->is_non_escaping_address_taken_ = true; }
1402 // Whether this variable should live in the heap.
1405 { return this->is_address_taken_; }
1407 // Set the function. This is used when cloning functions which call
1410 set_function(Function* function)
1411 { this->function_ = function; }
1413 // Get the backend representation of the variable.
1415 get_backend_variable(Gogo*, Named_object*, const std::string&);
1418 // Type of result variable.
1420 // Function with which this is associated.
1421 Function* function_;
1422 // Index in list of results.
1424 // Where the result variable is defined.
1425 source_location location_;
1426 // Backend representation.
1427 Bvariable* backend_;
1428 // Whether something takes the address of this variable.
1429 bool is_address_taken_;
1430 // Whether something takes the address of this variable such that
1431 // the address does not escape the function.
1432 bool is_non_escaping_address_taken_;
1435 // The value we keep for a named constant. This lets us hold a type
1436 // and an expression.
1438 class Named_constant
1441 Named_constant(Type* type, Expression* expr, int iota_value,
1442 source_location location)
1443 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1449 { return this->type_; }
1453 { return this->expr_; }
1457 { return this->iota_value_; }
1461 { return this->location_; }
1463 // Whether we are lowering.
1466 { return this->lowering_; }
1468 // Set that we are lowering.
1471 { this->lowering_ = true; }
1473 // We are no longer lowering.
1476 { this->lowering_ = false; }
1478 // Traverse the expression.
1480 traverse_expression(Traverse*);
1482 // Determine the type of the constant if necessary.
1486 // Indicate that we found and reported an error for this constant.
1490 // Export the constant.
1492 export_const(Export*, const std::string& name) const;
1494 // Import a constant.
1496 import_const(Import*, std::string*, Type**, Expression**);
1499 // The type of the constant.
1501 // The expression for the constant.
1503 // If the predeclared constant iota is used in EXPR_, this is the
1504 // value it will have. We do this because at parse time we don't
1505 // know whether the name "iota" will refer to the predeclared
1506 // constant or to something else. We put in the right value in when
1509 // The location of the definition.
1510 source_location location_;
1511 // Whether we are currently lowering this constant.
1515 // A type declaration.
1517 class Type_declaration
1520 Type_declaration(source_location location)
1521 : location_(location), in_function_(NULL), methods_(),
1522 issued_warning_(false)
1525 // Return the location.
1528 { return this->location_; }
1530 // Return the function in which this type is declared. This will
1531 // return NULL for a type declared in global scope.
1534 { return this->in_function_; }
1536 // Set the function in which this type is declared.
1538 set_in_function(Named_object* f)
1539 { this->in_function_ = f; }
1541 // Add a method to this type. This is used when methods are defined
1544 add_method(const std::string& name, Function* function);
1546 // Add a method declaration to this type.
1548 add_method_declaration(const std::string& name, Function_type* type,
1549 source_location location);
1551 // Return whether any methods were defined.
1553 has_methods() const;
1555 // Define methods when the real type is known.
1557 define_methods(Named_type*);
1559 // This is called if we are trying to use this type. It returns
1560 // true if we should issue a warning.
1565 typedef std::vector<Named_object*> Methods;
1567 // The location of the type declaration.
1568 source_location location_;
1569 // If this type is declared in a function, a pointer back to the
1570 // function in which it is defined.
1571 Named_object* in_function_;
1572 // Methods defined before the type is defined.
1574 // True if we have issued a warning about a use of this type
1575 // declaration when it is undefined.
1576 bool issued_warning_;
1579 // An unknown object. These are created by the parser for forward
1580 // references to names which have not been seen before. In a correct
1581 // program, these will always point to a real definition by the end of
1582 // the parse. Because they point to another Named_object, these may
1583 // only be referenced by Unknown_expression objects.
1588 Unknown_name(source_location location)
1589 : location_(location), real_named_object_(NULL)
1592 // Return the location where this name was first seen.
1595 { return this->location_; }
1597 // Return the real named object that this points to, or NULL if it
1598 // was never resolved.
1600 real_named_object() const
1601 { return this->real_named_object_; }
1603 // Set the real named object that this points to.
1605 set_real_named_object(Named_object* no);
1608 // The location where this name was first seen.
1609 source_location location_;
1610 // The real named object when it is known.
1615 // A named object named. This is the result of a declaration. We
1616 // don't use a superclass because they all have to be handled
1624 // An uninitialized Named_object. We should never see this.
1625 NAMED_OBJECT_UNINITIALIZED,
1626 // An unknown name. This is used for forward references. In a
1627 // correct program, these will all be resolved by the end of the
1629 NAMED_OBJECT_UNKNOWN,
1634 // A forward type declaration.
1635 NAMED_OBJECT_TYPE_DECLARATION,
1638 // A result variable in a function.
1639 NAMED_OBJECT_RESULT_VAR,
1640 // The blank identifier--the special variable named _.
1644 // A forward func declaration.
1645 NAMED_OBJECT_FUNC_DECLARATION,
1647 NAMED_OBJECT_PACKAGE
1650 // Return the classification.
1652 classification() const
1653 { return this->classification_; }
1659 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1663 { return this->classification_ == NAMED_OBJECT_CONST; }
1667 { return this->classification_ == NAMED_OBJECT_TYPE; }
1670 is_type_declaration() const
1671 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1675 { return this->classification_ == NAMED_OBJECT_VAR; }
1678 is_result_variable() const
1679 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1683 { return this->classification_ == NAMED_OBJECT_SINK; }
1687 { return this->classification_ == NAMED_OBJECT_FUNC; }
1690 is_function_declaration() const
1691 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1695 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1699 static Named_object*
1700 make_unknown_name(const std::string& name, source_location);
1702 static Named_object*
1703 make_constant(const Typed_identifier&, const Package*, Expression*,
1706 static Named_object*
1707 make_type(const std::string&, const Package*, Type*, source_location);
1709 static Named_object*
1710 make_type_declaration(const std::string&, const Package*, source_location);
1712 static Named_object*
1713 make_variable(const std::string&, const Package*, Variable*);
1715 static Named_object*
1716 make_result_variable(const std::string&, Result_variable*);
1718 static Named_object*
1721 static Named_object*
1722 make_function(const std::string&, const Package*, Function*);
1724 static Named_object*
1725 make_function_declaration(const std::string&, const Package*, Function_type*,
1728 static Named_object*
1729 make_package(const std::string& alias, Package* package);
1736 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1737 return this->u_.unknown_value;
1741 unknown_value() const
1743 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1744 return this->u_.unknown_value;
1750 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1751 return this->u_.const_value;
1754 const Named_constant*
1757 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1758 return this->u_.const_value;
1764 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1765 return this->u_.type_value;
1771 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1772 return this->u_.type_value;
1776 type_declaration_value()
1778 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1779 return this->u_.type_declaration;
1782 const Type_declaration*
1783 type_declaration_value() const
1785 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1786 return this->u_.type_declaration;
1792 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1793 return this->u_.var_value;
1799 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1800 return this->u_.var_value;
1806 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1807 return this->u_.result_var_value;
1810 const Result_variable*
1811 result_var_value() const
1813 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1814 return this->u_.result_var_value;
1820 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1821 return this->u_.func_value;
1827 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1828 return this->u_.func_value;
1831 Function_declaration*
1832 func_declaration_value()
1834 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1835 return this->u_.func_declaration_value;
1838 const Function_declaration*
1839 func_declaration_value() const
1841 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1842 return this->u_.func_declaration_value;
1848 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1849 return this->u_.package_value;
1853 package_value() const
1855 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1856 return this->u_.package_value;
1861 { return this->name_; }
1863 // Return the name to use in an error message. The difference is
1864 // that if this Named_object is defined in a different package, this
1865 // will return PACKAGE.NAME.
1867 message_name() const;
1871 { return this->package_; }
1873 // Resolve an unknown value if possible. This returns the same
1874 // Named_object or a new one.
1878 Named_object* ret = this;
1879 if (this->is_unknown())
1881 Named_object* r = this->unknown_value()->real_named_object();
1891 const Named_object* ret = this;
1892 if (this->is_unknown())
1894 const Named_object* r = this->unknown_value()->real_named_object();
1901 // The location where this object was defined or referenced.
1905 // Convert a variable to the backend representation.
1907 get_backend_variable(Gogo*, Named_object* function);
1909 // Return a tree for the external identifier for this object.
1913 // Return a tree representing this object.
1915 get_tree(Gogo*, Named_object* function);
1917 // Define a type declaration.
1919 set_type_value(Named_type*);
1921 // Define a function declaration.
1923 set_function_value(Function*);
1925 // Declare an unknown name as a type declaration.
1929 // Export this object.
1931 export_named_object(Export*) const;
1934 Named_object(const std::string&, const Package*, Classification);
1936 // The name of the object.
1938 // The package that this object is in. This is NULL if it is in the
1939 // file we are compiling.
1940 const Package* package_;
1941 // The type of object this is.
1942 Classification classification_;
1946 Unknown_name* unknown_value;
1947 Named_constant* const_value;
1948 Named_type* type_value;
1949 Type_declaration* type_declaration;
1950 Variable* var_value;
1951 Result_variable* result_var_value;
1952 Function* func_value;
1953 Function_declaration* func_declaration_value;
1954 Package* package_value;
1956 // The DECL tree for this object if we have already converted it.
1960 // A binding contour. This binds names to objects.
1965 // Type for mapping from names to objects.
1966 typedef Unordered_map(std::string, Named_object*) Contour;
1968 Bindings(Bindings* enclosing);
1970 // Add an unknown name.
1972 add_unknown_name(const std::string& name, source_location location)
1974 return this->add_named_object(Named_object::make_unknown_name(name,
1980 add_constant(const Typed_identifier& tid, const Package* package,
1981 Expression* expr, int iota_value)
1983 return this->add_named_object(Named_object::make_constant(tid, package,
1990 add_type(const std::string& name, const Package* package, Type* type,
1991 source_location location)
1993 return this->add_named_object(Named_object::make_type(name, package, type,
1997 // Add a named type. This is used for builtin types, and to add an
1998 // imported type to the global scope.
2000 add_named_type(Named_type* named_type);
2002 // Add a type declaration.
2004 add_type_declaration(const std::string& name, const Package* package,
2005 source_location location)
2007 Named_object* no = Named_object::make_type_declaration(name, package,
2009 return this->add_named_object(no);
2014 add_variable(const std::string& name, const Package* package,
2017 return this->add_named_object(Named_object::make_variable(name, package,
2021 // Add a result variable.
2023 add_result_variable(const std::string& name, Result_variable* result)
2025 return this->add_named_object(Named_object::make_result_variable(name,
2031 add_function(const std::string& name, const Package*, Function* function);
2033 // Add a function declaration.
2035 add_function_declaration(const std::string& name, const Package* package,
2036 Function_type* type, source_location location);
2038 // Add a package. The location is the location of the import
2041 add_package(const std::string& alias, Package* package)
2043 Named_object* no = Named_object::make_package(alias, package);
2044 return this->add_named_object(no);
2047 // Define a type which was already declared.
2049 define_type(Named_object*, Named_type*);
2051 // Add a method to the list of objects. This is not added to the
2054 add_method(Named_object*);
2056 // Add a named object to this binding.
2058 add_named_object(Named_object* no)
2059 { return this->add_named_object_to_contour(&this->bindings_, no); }
2061 // Clear all names in file scope from the bindings.
2065 // Look up a name in this binding contour and in any enclosing
2066 // binding contours. This returns NULL if the name is not found.
2068 lookup(const std::string&) const;
2070 // Look up a name in this binding contour without looking in any
2071 // enclosing binding contours. Returns NULL if the name is not found.
2073 lookup_local(const std::string&) const;
2077 remove_binding(Named_object*);
2079 // Traverse the tree. See the Traverse class.
2081 traverse(Traverse*, bool is_global);
2083 // Iterate over definitions. This does not include things which
2084 // were only declared.
2086 typedef std::vector<Named_object*>::const_iterator
2087 const_definitions_iterator;
2089 const_definitions_iterator
2090 begin_definitions() const
2091 { return this->named_objects_.begin(); }
2093 const_definitions_iterator
2094 end_definitions() const
2095 { return this->named_objects_.end(); }
2097 // Return the number of definitions.
2099 size_definitions() const
2100 { return this->named_objects_.size(); }
2102 // Return whether there are no definitions.
2104 empty_definitions() const
2105 { return this->named_objects_.empty(); }
2107 // Iterate over declarations. This is everything that has been
2108 // declared, which includes everything which has been defined.
2110 typedef Contour::const_iterator const_declarations_iterator;
2112 const_declarations_iterator
2113 begin_declarations() const
2114 { return this->bindings_.begin(); }
2116 const_declarations_iterator
2117 end_declarations() const
2118 { return this->bindings_.end(); }
2120 // Return the number of declarations.
2122 size_declarations() const
2123 { return this->bindings_.size(); }
2125 // Return whether there are no declarations.
2127 empty_declarations() const
2128 { return this->bindings_.empty(); }
2130 // Return the first declaration.
2133 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2137 add_named_object_to_contour(Contour*, Named_object*);
2140 new_definition(Named_object*, Named_object*);
2142 // Enclosing bindings.
2143 Bindings* enclosing_;
2144 // The list of objects.
2145 std::vector<Named_object*> named_objects_;
2146 // The mapping from names to objects.
2155 Label(const std::string& name)
2156 : name_(name), location_(0), snapshot_(NULL), refs_(), is_used_(false),
2160 // Return the label's name.
2163 { return this->name_; }
2165 // Return whether the label has been defined.
2168 { return this->location_ != 0; }
2170 // Return whether the label has been used.
2173 { return this->is_used_; }
2175 // Record that the label is used.
2178 { this->is_used_ = true; }
2180 // Return the location of the definition.
2183 { return this->location_; }
2185 // Return the bindings snapshot.
2188 { return this->snapshot_; }
2190 // Add a snapshot of a goto which refers to this label.
2192 add_snapshot_ref(Bindings_snapshot* snapshot)
2194 go_assert(this->location_ == 0);
2195 this->refs_.push_back(snapshot);
2198 // Return the list of snapshots of goto statements which refer to
2200 const std::vector<Bindings_snapshot*>&
2202 { return this->refs_; }
2204 // Clear the references.
2208 // Define the label at LOCATION with the given bindings snapshot.
2210 define(source_location location, Bindings_snapshot* snapshot)
2212 go_assert(this->location_ == 0 && this->snapshot_ == NULL);
2213 this->location_ = location;
2214 this->snapshot_ = snapshot;
2217 // Return the backend representation for this label.
2219 get_backend_label(Translate_context*);
2221 // Return an expression for the address of this label. This is used
2222 // to get the return address of a deferred function to see whether
2223 // the function may call recover.
2225 get_addr(Translate_context*, source_location location);
2228 // The name of the label.
2230 // The location of the definition. This is 0 if the label has not
2231 // yet been defined.
2232 source_location location_;
2233 // A snapshot of the set of bindings defined at this label, used to
2234 // issue errors about invalid goto statements.
2235 Bindings_snapshot* snapshot_;
2236 // A list of snapshots of goto statements which refer to this label.
2237 std::vector<Bindings_snapshot*> refs_;
2238 // Whether the label has been used.
2240 // The backend representation.
2244 // An unnamed label. These are used when lowering loops.
2249 Unnamed_label(source_location location)
2250 : location_(location), blabel_(NULL)
2253 // Get the location where the label is defined.
2256 { return this->location_; }
2258 // Set the location where the label is defined.
2260 set_location(source_location location)
2261 { this->location_ = location; }
2263 // Return a statement which defines this label.
2265 get_definition(Translate_context*);
2267 // Return a goto to this label from LOCATION.
2269 get_goto(Translate_context*, source_location location);
2272 // Return the backend representation.
2274 get_blabel(Translate_context*);
2276 // The location where the label is defined.
2277 source_location location_;
2278 // The backend representation of this label.
2282 // An imported package.
2287 Package(const std::string& name, const std::string& unique_prefix,
2288 source_location location);
2290 // The real name of this package. This may be different from the
2291 // name in the associated Named_object if the import statement used
2295 { return this->name_; }
2297 // Return the location of the import statement.
2300 { return this->location_; }
2302 // Get the unique prefix used for all symbols exported from this
2305 unique_prefix() const
2307 go_assert(!this->unique_prefix_.empty());
2308 return this->unique_prefix_;
2311 // The priority of this package. The init function of packages with
2312 // lower priority must be run before the init function of packages
2313 // with higher priority.
2316 { return this->priority_; }
2318 // Set the priority.
2320 set_priority(int priority);
2322 // Return the bindings.
2325 { return this->bindings_; }
2327 // Whether some symbol from the package was used.
2330 { return this->used_; }
2332 // Note that some symbol from this package was used.
2335 { this->used_ = true; }
2337 // Clear the used field for the next file.
2340 { this->used_ = false; }
2342 // Whether this package was imported in the current file.
2345 { return this->is_imported_; }
2347 // Note that this package was imported in the current file.
2350 { this->is_imported_ = true; }
2352 // Clear the imported field for the next file.
2355 { this->is_imported_ = false; }
2357 // Whether this package was imported with a name of "_".
2359 uses_sink_alias() const
2360 { return this->uses_sink_alias_; }
2362 // Note that this package was imported with a name of "_".
2364 set_uses_sink_alias()
2365 { this->uses_sink_alias_ = true; }
2367 // Clear the sink alias field for the next file.
2369 clear_uses_sink_alias()
2370 { this->uses_sink_alias_ = false; }
2372 // Look up a name in the package. Returns NULL if the name is not
2375 lookup(const std::string& name) const
2376 { return this->bindings_->lookup(name); }
2378 // Set the location of the package. This is used if it is seen in a
2379 // different import before it is really imported.
2381 set_location(source_location location)
2382 { this->location_ = location; }
2384 // Add a constant to the package.
2386 add_constant(const Typed_identifier& tid, Expression* expr)
2387 { return this->bindings_->add_constant(tid, this, expr, 0); }
2389 // Add a type to the package.
2391 add_type(const std::string& name, Type* type, source_location location)
2392 { return this->bindings_->add_type(name, this, type, location); }
2394 // Add a type declaration to the package.
2396 add_type_declaration(const std::string& name, source_location location)
2397 { return this->bindings_->add_type_declaration(name, this, location); }
2399 // Add a variable to the package.
2401 add_variable(const std::string& name, Variable* variable)
2402 { return this->bindings_->add_variable(name, this, variable); }
2404 // Add a function declaration to the package.
2406 add_function_declaration(const std::string& name, Function_type* type,
2407 source_location loc)
2408 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2410 // Determine types of constants.
2415 // The real name of this package.
2417 // The unique prefix for all exported global symbols.
2418 std::string unique_prefix_;
2419 // The names in this package.
2420 Bindings* bindings_;
2421 // The priority of this package. A package has a priority higher
2422 // than the priority of all of the packages that it imports. This
2423 // is used to run init functions in the right order.
2425 // The location of the import statement.
2426 source_location location_;
2427 // True if some name from this package was used. This is mutable
2428 // because we can use a package even if we have a const pointer to
2431 // True if this package was imported in the current file.
2433 // True if this package was imported with a name of "_".
2434 bool uses_sink_alias_;
2437 // Return codes for the traversal functions. This is not an enum
2438 // because we want to be able to declare traversal functions in other
2439 // header files without including this one.
2441 // Continue traversal as usual.
2442 const int TRAVERSE_CONTINUE = -1;
2445 const int TRAVERSE_EXIT = 0;
2447 // Continue traversal, but skip components of the current object.
2448 // E.g., if this is returned by Traverse::statement, we do not
2449 // traverse the expressions in the statement even if
2450 // traverse_expressions is set in the traverse_mask.
2451 const int TRAVERSE_SKIP_COMPONENTS = 1;
2453 // This class is used when traversing the parse tree. The caller uses
2454 // a subclass which overrides functions as desired.
2459 // These bitmasks say what to traverse.
2460 static const unsigned int traverse_variables = 0x1;
2461 static const unsigned int traverse_constants = 0x2;
2462 static const unsigned int traverse_functions = 0x4;
2463 static const unsigned int traverse_blocks = 0x8;
2464 static const unsigned int traverse_statements = 0x10;
2465 static const unsigned int traverse_expressions = 0x20;
2466 static const unsigned int traverse_types = 0x40;
2468 Traverse(unsigned int traverse_mask)
2469 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2472 virtual ~Traverse();
2474 // The bitmask of what to traverse.
2476 traverse_mask() const
2477 { return this->traverse_mask_; }
2479 // Record that we are going to traverse a type. This returns true
2480 // if the type has already been seen in this traversal. This is
2481 // required because types, unlike expressions, can form a circular
2484 remember_type(const Type*);
2486 // Record that we are going to see an expression. This returns true
2487 // if the expression has already been seen in this traversal. This
2488 // is only needed for cases where multiple expressions can point to
2491 remember_expression(const Expression*);
2493 // These functions return one of the TRAVERSE codes defined above.
2495 // If traverse_variables is set in the mask, this is called for
2496 // every variable in the tree.
2498 variable(Named_object*);
2500 // If traverse_constants is set in the mask, this is called for
2501 // every named constant in the tree. The bool parameter is true for
2502 // a global constant.
2504 constant(Named_object*, bool);
2506 // If traverse_functions is set in the mask, this is called for
2507 // every function in the tree.
2509 function(Named_object*);
2511 // If traverse_blocks is set in the mask, this is called for every
2512 // block in the tree.
2516 // If traverse_statements is set in the mask, this is called for
2517 // every statement in the tree.
2519 statement(Block*, size_t* index, Statement*);
2521 // If traverse_expressions is set in the mask, this is called for
2522 // every expression in the tree.
2524 expression(Expression**);
2526 // If traverse_types is set in the mask, this is called for every
2527 // type in the tree.
2532 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2533 Type_identical) Types_seen;
2535 typedef Unordered_set(const Expression*) Expressions_seen;
2537 // Bitmask of what sort of objects to traverse.
2538 unsigned int traverse_mask_;
2539 // Types which have been seen in this traversal.
2540 Types_seen* types_seen_;
2541 // Expressions which have been seen in this traversal.
2542 Expressions_seen* expressions_seen_;
2545 // A class which makes it easier to insert new statements before the
2546 // current statement during a traversal.
2548 class Statement_inserter
2551 // Empty constructor.
2552 Statement_inserter()
2553 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2556 // Constructor for a statement in a block.
2557 Statement_inserter(Block* block, size_t *pindex)
2558 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2561 // Constructor for a global variable.
2562 Statement_inserter(Gogo* gogo, Variable* var)
2563 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2564 { go_assert(var->is_global()); }
2566 // We use the default copy constructor and assignment operator.
2568 // Insert S before the statement we are traversing, or before the
2569 // initialization expression of a global variable.
2571 insert(Statement* s);
2574 // The block that the statement is in.
2576 // The index of the statement that we are traversing.
2578 // The IR, needed when looking at an initializer expression for a
2581 // The global variable, when looking at an initializer expression.
2585 // When translating the gogo IR into the backend data structure, this
2586 // is the context we pass down the blocks and statements.
2588 class Translate_context
2591 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2593 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2594 block_(block), bblock_(bblock), is_const_(false)
2601 { return this->gogo_; }
2605 { return this->backend_; }
2609 { return this->function_; }
2613 { return this->block_; }
2617 { return this->bblock_; }
2621 { return this->is_const_; }
2623 // Make a constant context.
2626 { this->is_const_ = true; }
2629 // The IR for the entire compilation unit.
2631 // The generator for the backend data structures.
2633 // The function we are currently translating. NULL if not in a
2634 // function, e.g., the initializer of a global variable.
2635 Named_object* function_;
2636 // The block we are currently translating. NULL if not in a
2639 // The backend representation of the current block. NULL if block_
2642 // Whether this is being evaluated in a constant context. This is
2643 // used for type descriptor initializers.
2647 // Runtime error codes. These must match the values in
2648 // libgo/runtime/go-runtime-error.c.
2650 // Slice index out of bounds: negative or larger than the length of
2652 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2654 // Array index out of bounds.
2655 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2657 // String index out of bounds.
2658 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2660 // Slice slice out of bounds: negative or larger than the length of
2661 // the slice or high bound less than low bound.
2662 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2664 // Array slice out of bounds.
2665 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2667 // String slice out of bounds.
2668 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2670 // Dereference of nil pointer. This is used when there is a
2671 // dereference of a pointer to a very large struct or array, to ensure
2672 // that a gigantic array is not used a proxy to access random memory
2674 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2676 // Slice length or capacity out of bounds in make: negative or
2677 // overflow or length greater than capacity.
2678 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2680 // Map capacity out of bounds in make: negative or overflow.
2681 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2683 // Channel capacity out of bounds in make: negative or overflow.
2684 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2686 // This is used by some of the langhooks.
2687 extern Gogo* go_get_gogo();
2689 // Whether we have seen any errors. FIXME: Replace with a backend
2691 extern bool saw_errors();
2693 #endif // !defined(GO_GOGO_H)