1 // gogo.h -- Go frontend parsed representation. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
12 class Type_hash_identical;
15 class Typed_identifier;
16 class Typed_identifier_list;
20 class Temporary_statement;
29 class Struct_field_list;
35 class Forward_declaration_type;
40 class Translate_context;
49 // This file declares the basic classes used to hold the internal
50 // representation of Go which is built by the parser.
52 // An initialization function for an imported package. This is a
53 // magic function which initializes variables and runs the "init"
59 Import_init(const std::string& package_name, const std::string& init_name,
61 : package_name_(package_name), init_name_(init_name), priority_(priority)
64 // The name of the package being imported.
67 { return this->package_name_; }
69 // The name of the package's init function.
72 { return this->init_name_; }
74 // The priority of the initialization function. Functions with a
75 // lower priority number must be run first.
78 { return this->priority_; }
81 // The name of the package being imported.
82 std::string package_name_;
83 // The name of the package's init function.
84 std::string init_name_;
89 // For sorting purposes.
92 operator<(const Import_init& i1, const Import_init& i2)
94 if (i1.priority() < i2.priority())
96 if (i1.priority() > i2.priority())
98 if (i1.package_name() != i2.package_name())
99 return i1.package_name() < i2.package_name();
100 return i1.init_name() < i2.init_name();
103 // The holder for the internal representation of the entire
109 // Create the IR, passing in the sizes of the types "int" and
110 // "uintptr" in bits.
111 Gogo(Backend* backend, int int_type_size, int pointer_size);
113 // Get the backend generator.
116 { return this->backend_; }
118 // Get the package name.
120 package_name() const;
122 // Set the package name.
124 set_package_name(const std::string&, source_location);
126 // Return whether this is the "main" package.
128 is_main_package() const;
130 // If necessary, adjust the name to use for a hidden symbol. We add
131 // a prefix of the package name, so that hidden symbols in different
132 // packages do not collide.
134 pack_hidden_name(const std::string& name, bool is_exported) const
138 : ('.' + this->unique_prefix()
139 + '.' + this->package_name()
143 // Unpack a name which may have been hidden. Returns the
144 // user-visible name of the object.
146 unpack_hidden_name(const std::string& name)
147 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
149 // Return whether a possibly packed name is hidden.
151 is_hidden_name(const std::string& name)
152 { return name[0] == '.'; }
154 // Return the package prefix of a hidden name.
156 hidden_name_prefix(const std::string& name)
158 gcc_assert(Gogo::is_hidden_name(name));
159 return name.substr(1, name.rfind('.') - 1);
162 // Given a name which may or may not have been hidden, return the
163 // name to use in an error message.
165 message_name(const std::string& name);
167 // Return whether a name is the blank identifier _.
169 is_sink_name(const std::string& name)
171 return (name[0] == '.'
172 && name[name.length() - 1] == '_'
173 && name[name.length() - 2] == '.');
176 // Return the unique prefix to use for all exported symbols.
178 unique_prefix() const;
180 // Set the unique prefix.
182 set_unique_prefix(const std::string&);
184 // Return the priority to use for the package we are compiling.
185 // This is two more than the largest priority of any package we
188 package_priority() const;
190 // Import a package. FILENAME is the file name argument, LOCAL_NAME
191 // is the local name to give to the package. If LOCAL_NAME is empty
192 // the declarations are added to the global scope.
194 import_package(const std::string& filename, const std::string& local_name,
195 bool is_local_name_exported, source_location);
197 // Whether we are the global binding level.
199 in_global_scope() const;
201 // Look up a name in the current binding contours.
203 lookup(const std::string&, Named_object** pfunction) const;
205 // Look up a name in the current block.
207 lookup_in_block(const std::string&) const;
209 // Look up a name in the global namespace--the universal scope.
211 lookup_global(const char*) const;
213 // Add a new imported package. REAL_NAME is the real name of the
214 // package. ALIAS is the alias of the package; this may be the same
215 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
216 // this package should be added to the global namespace; this is
217 // true if the alias is ".". LOCATION is the location of the import
218 // statement. This returns the new package, or NULL on error.
220 add_imported_package(const std::string& real_name, const std::string& alias,
221 bool is_alias_exported,
222 const std::string& unique_prefix,
223 source_location location,
224 bool* padd_to_globals);
226 // Register a package. This package may or may not be imported.
227 // This returns the Package structure for the package, creating if
230 register_package(const std::string& name, const std::string& unique_prefix,
233 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
234 // method function should be added to the type of its receiver.
236 start_function(const std::string& name, Function_type* type,
237 bool add_method_to_type, source_location);
239 // Finish compiling a function.
241 finish_function(source_location);
243 // Return the current function.
245 current_function() const;
247 // Start a new block. This is not initially associated with a
250 start_block(source_location);
252 // Finish the current block and return it.
254 finish_block(source_location);
256 // Declare an unknown name. This is used while parsing. The name
257 // must be resolved by the end of the parse. Unknown names are
258 // always added at the package level.
260 add_unknown_name(const std::string& name, source_location);
262 // Declare a function.
264 declare_function(const std::string&, Function_type*, source_location);
268 add_label_definition(const std::string&, source_location);
270 // Add a label reference.
272 add_label_reference(const std::string&);
274 // Add a statement to the current block.
276 add_statement(Statement*);
278 // Add a block to the current block.
280 add_block(Block*, source_location);
284 add_constant(const Typed_identifier&, Expression*, int iota_value);
288 add_type(const std::string&, Type*, source_location);
290 // Add a named type. This is used for builtin types, and to add an
291 // imported type to the global scope.
293 add_named_type(Named_type*);
297 declare_type(const std::string&, source_location);
299 // Declare a type at the package level. This is used when the
300 // parser sees an unknown name where a type name is required.
302 declare_package_type(const std::string&, source_location);
304 // Define a type which was already declared.
306 define_type(Named_object*, Named_type*);
310 add_variable(const std::string&, Variable*);
312 // Add a sink--a reference to the blank identifier _.
316 // Add a named object to the current namespace. This is used for
317 // import . "package".
319 add_named_object(Named_object*);
321 // Return a name to use for a thunk function. A thunk function is
322 // one we create during the compilation, for a go statement or a
323 // defer statement or a method expression.
327 // Return whether an object is a thunk.
329 is_thunk(const Named_object*);
331 // Note that we've seen an interface type. This is used to build
332 // all required interface method tables.
334 record_interface_type(Interface_type*);
336 // Note that we need an initialization function.
339 { this->need_init_fn_ = true; }
341 // Clear out all names in file scope. This is called when we start
342 // parsing a new file.
346 // Traverse the tree. See the Traverse class.
350 // Define the predeclared global names.
352 define_global_names();
354 // Verify and complete all types.
358 // Lower the parse tree.
362 // Lower all the statements in a block.
364 lower_block(Named_object* function, Block*);
366 // Lower an expression.
368 lower_expression(Named_object* function, Expression**);
372 lower_constant(Named_object*);
374 // Finalize the method lists and build stub methods for named types.
378 // Work out the types to use for unspecified variables and
383 // Type check the program.
387 // Check the types in a single block. This is used for complicated
390 check_types_in_block(Block*);
392 // Check for return statements.
394 check_return_statements();
400 // Add an import control function for an imported package to the
403 add_import_init_fn(const std::string& package_name,
404 const std::string& init_name, int prio);
406 // Turn short-cut operators (&&, ||) into explicit if statements.
410 // Use temporary variables to force order of evaluation.
414 // Build thunks for functions which call recover.
416 build_recover_thunks();
418 // Simplify statements which might use thunks: go and defer
421 simplify_thunk_statements();
423 // Convert named types to the backend representation.
425 convert_named_types();
427 // Convert named types in a list of bindings.
429 convert_named_types_in_bindings(Bindings*);
431 // True if named types have been converted to the backend
434 named_types_are_converted() const
435 { return this->named_types_are_converted_; }
437 // Write out the global values.
441 // Build a call to a builtin function. PDECL should point to a NULL
442 // initialized static pointer which will hold the fndecl. NAME is
443 // the name of the function. NARGS is the number of arguments.
444 // RETTYPE is the return type. It is followed by NARGS pairs of
445 // type and argument (both trees).
447 call_builtin(tree* pdecl, source_location, const char* name, int nargs,
450 // Build a call to the runtime error function.
452 runtime_error(int code, source_location);
454 // Build a builtin struct with a list of fields.
456 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
459 // Mark a function declaration as a builtin library function.
461 mark_fndecl_as_builtin_library(tree fndecl);
463 // Build the type of the struct that holds a slice for the given
466 slice_type_tree(tree element_type_tree);
468 // Given a tree for a slice type, return the tree for the element
471 slice_element_type_tree(tree slice_type_tree);
473 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
474 // the slice. VALUES points to the values. COUNT is the size,
475 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
478 slice_constructor(tree slice_type_tree, tree values, tree count,
481 // Build a constructor for an empty slice. SLICE_TYPE_TREE is the
482 // type of the slice.
484 empty_slice_constructor(tree slice_type_tree);
486 // Build a map descriptor.
488 map_descriptor(Map_type*);
490 // Return a tree for the type of a map descriptor. This is struct
491 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
494 map_descriptor_type();
496 // Build a type descriptor for TYPE using INITIALIZER as the type
497 // descriptor. This builds a new decl stored in *PDECL.
499 build_type_descriptor_decl(const Type*, Expression* initializer,
502 // Build required interface method tables.
504 build_interface_method_tables();
506 // Build an interface method table for a type: a list of function
507 // pointers, one for each interface method. This returns a decl.
509 interface_method_table_for_type(const Interface_type*, Named_type*,
512 // Return a tree which allocate SIZE bytes to hold values of type
515 allocate_memory(Type *type, tree size, source_location);
517 // Return a type to use for pointer to const char.
519 const_char_pointer_type_tree();
521 // Build a string constant with the right type.
523 string_constant_tree(const std::string&);
525 // Build a Go string constant. This returns a pointer to the
528 go_string_constant_tree(const std::string&);
530 // Receive a value from a channel.
532 receive_from_channel(tree type_tree, tree channel, bool for_select,
535 // Return a tree for receiving an integer on a channel.
537 receive_as_64bit_integer(tree type, tree channel, bool blocking,
541 // Make a trampoline which calls FNADDR passing CLOSURE.
543 make_trampoline(tree fnaddr, tree closure, source_location);
546 // During parsing, we keep a stack of functions. Each function on
547 // the stack is one that we are currently parsing. For each
548 // function, we keep track of the current stack of blocks.
552 Named_object* function;
553 // The stack of active blocks in the function.
554 std::vector<Block*> blocks;
557 // The stack of functions.
558 typedef std::vector<Open_function> Open_functions;
560 // Create trees for implicit builtin functions.
562 define_builtin_function_trees();
564 // Set up the built-in unsafe package.
566 import_unsafe(const std::string&, bool is_exported, source_location);
568 // Add a new imported package.
570 add_package(const std::string& real_name, const std::string& alias,
571 const std::string& unique_prefix, source_location location);
573 // Return the current binding contour.
578 current_bindings() const;
580 // Return the current block.
584 // Get the name of the magic initialization function.
588 // Get the decl for the magic initialization function.
590 initialization_function_decl();
592 // Write the magic initialization function.
594 write_initialization_function(tree fndecl, tree init_stmt_list);
596 // Initialize imported packages.
600 // Register variables with the garbage collector.
602 register_gc_vars(const std::vector<Named_object*>&, tree*);
604 // Build a pointer to a Go string constant. This returns a pointer
607 ptr_go_string_constant_tree(const std::string&);
609 // Return the name to use for a type descriptor decl for an unnamed
612 unnamed_type_descriptor_decl_name(const Type* type);
614 // Return the name to use for a type descriptor decl for a type
615 // named NO, defined in IN_FUNCTION.
617 type_descriptor_decl_name(const Named_object* no,
618 const Named_object* in_function);
620 // Where a type descriptor should be defined.
621 enum Type_descriptor_location
623 // Defined in this file.
624 TYPE_DESCRIPTOR_DEFINED,
625 // Defined in some other file.
626 TYPE_DESCRIPTOR_UNDEFINED,
627 // Common definition which may occur in multiple files.
628 TYPE_DESCRIPTOR_COMMON
631 // Return where the decl for TYPE should be defined.
632 Type_descriptor_location
633 type_descriptor_location(const Type* type);
635 // Return the type of a trampoline.
637 trampoline_type_tree();
639 // Type used to map import names to packages.
640 typedef std::map<std::string, Package*> Imports;
642 // Type used to map package names to packages.
643 typedef std::map<std::string, Package*> Packages;
645 // Type used to map special names in the sys package.
646 typedef std::map<std::string, std::string> Sys_names;
648 // Hash table mapping map types to map descriptor decls.
649 typedef Unordered_map_hash(const Map_type*, tree, Type_hash_identical,
650 Type_identical) Map_descriptors;
652 // Map unnamed types to type descriptor decls.
653 typedef Unordered_map_hash(const Type*, tree, Type_hash_identical,
654 Type_identical) Type_descriptor_decls;
656 // The backend generator.
658 // The package we are compiling.
660 // The list of currently open functions during parsing.
661 Open_functions functions_;
662 // The global binding contour. This includes the builtin functions
663 // and the package we are compiling.
665 // Mapping from import file names to packages.
667 // Whether the magic unsafe package was imported.
668 bool imported_unsafe_;
669 // Mapping from package names we have seen to packages. This does
670 // not include the package we are compiling.
672 // Mapping from map types to map descriptors.
673 Map_descriptors* map_descriptors_;
674 // Mapping from unnamed types to type descriptor decls.
675 Type_descriptor_decls* type_descriptor_decls_;
676 // The functions named "init", if there are any.
677 std::vector<Named_object*> init_functions_;
678 // Whether we need a magic initialization function.
680 // The name of the magic initialization function.
681 std::string init_fn_name_;
682 // A list of import control variables for packages that we import.
683 std::set<Import_init> imported_init_fns_;
684 // The unique prefix used for all global symbols.
685 std::string unique_prefix_;
686 // Whether an explicit unique prefix was set by -fgo-prefix.
687 bool unique_prefix_specified_;
688 // A list of interface types defined while parsing.
689 std::vector<Interface_type*> interface_types_;
690 // Whether named types have been converted.
691 bool named_types_are_converted_;
694 // A block of statements.
699 Block(Block* enclosing, source_location);
701 // Return the enclosing block.
704 { return this->enclosing_; }
706 // Return the bindings of the block.
709 { return this->bindings_; }
713 { return this->bindings_; }
715 // Look at the block's statements.
716 const std::vector<Statement*>*
718 { return &this->statements_; }
720 // Return the start location. This is normally the location of the
721 // left curly brace which starts the block.
723 start_location() const
724 { return this->start_location_; }
726 // Return the end location. This is normally the location of the
727 // right curly brace which ends the block.
730 { return this->end_location_; }
732 // Add a statement to the block.
734 add_statement(Statement*);
736 // Add a statement to the front of the block.
738 add_statement_at_front(Statement*);
740 // Replace a statement in a block.
742 replace_statement(size_t index, Statement*);
744 // Add a Statement before statement number INDEX.
746 insert_statement_before(size_t index, Statement*);
748 // Add a Statement after statement number INDEX.
750 insert_statement_after(size_t index, Statement*);
752 // Set the end location of the block.
754 set_end_location(source_location location)
755 { this->end_location_ = location; }
757 // Traverse the tree.
761 // Set final types for unspecified variables and constants.
765 // Return true if execution of this block may fall through to the
768 may_fall_through() const;
770 // Return a tree of the code in this block.
772 get_tree(Translate_context*);
774 // Iterate over statements.
776 typedef std::vector<Statement*>::iterator iterator;
780 { return this->statements_.begin(); }
784 { return this->statements_.end(); }
789 // Statements in the block.
790 std::vector<Statement*> statements_;
793 // Location of start of block.
794 source_location start_location_;
795 // Location of end of block.
796 source_location end_location_;
804 Function(Function_type* type, Function*, Block*, source_location);
806 // Return the function's type.
809 { return this->type_; }
811 // Return the enclosing function if there is one.
814 { return this->enclosing_; }
816 // Set the enclosing function. This is used when building thunks
817 // for functions which call recover.
819 set_enclosing(Function* enclosing)
821 gcc_assert(this->enclosing_ == NULL);
822 this->enclosing_ = enclosing;
825 // The result variables.
826 typedef std::vector<Named_object*> Results;
828 // Create the result variables in the outer block.
830 create_result_variables(Gogo*);
832 // Update the named result variables when cloning a function which
835 update_result_variables();
837 // Return the result variables.
840 { return this->results_; }
842 // Whether the result variables have names.
844 results_are_named() const
845 { return this->results_are_named_; }
847 // Add a new field to the closure variable.
849 add_closure_field(Named_object* var, source_location loc)
850 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
852 // Whether this function needs a closure.
854 needs_closure() const
855 { return !this->closure_fields_.empty(); }
857 // Return the closure variable, creating it if necessary. This is
858 // passed to the function as a static chain parameter.
862 // Set the closure variable. This is used when building thunks for
863 // functions which call recover.
865 set_closure_var(Named_object* v)
867 gcc_assert(this->closure_var_ == NULL);
868 this->closure_var_ = v;
871 // Return the variable for a reference to field INDEX in the closure
874 enclosing_var(unsigned int index)
876 gcc_assert(index < this->closure_fields_.size());
877 return closure_fields_[index].first;
880 // Set the type of the closure variable if there is one.
884 // Get the block of statements associated with the function.
887 { return this->block_; }
889 // Get the location of the start of the function.
892 { return this->location_; }
894 // Return whether this function is actually a method.
898 // Add a label definition to the function.
900 add_label_definition(const std::string& label_name, source_location);
902 // Add a label reference to a function.
904 add_label_reference(const std::string& label_name);
906 // Warn about labels that are defined but not used.
908 check_labels() const;
910 // Whether this function calls the predeclared recover function.
912 calls_recover() const
913 { return this->calls_recover_; }
915 // Record that this function calls the predeclared recover function.
916 // This is set during the lowering pass.
919 { this->calls_recover_ = true; }
921 // Whether this is a recover thunk function.
923 is_recover_thunk() const
924 { return this->is_recover_thunk_; }
926 // Record that this is a thunk built for a function which calls
929 set_is_recover_thunk()
930 { this->is_recover_thunk_ = true; }
932 // Whether this function already has a recover thunk.
934 has_recover_thunk() const
935 { return this->has_recover_thunk_; }
937 // Record that this function already has a recover thunk.
939 set_has_recover_thunk()
940 { this->has_recover_thunk_ = true; }
942 // Swap with another function. Used only for the thunk which calls
945 swap_for_recover(Function *);
947 // Traverse the tree.
951 // Determine types in the function.
955 // Return the function's decl given an identifier.
957 get_or_make_decl(Gogo*, Named_object*, tree id);
959 // Return the function's decl after it has been built.
963 gcc_assert(this->fndecl_ != NULL);
964 return this->fndecl_;
967 // Set the function decl to hold a tree of the function code.
969 build_tree(Gogo*, Named_object*);
971 // Get the value to return when not explicitly specified. May also
972 // add statements to execute first to STMT_LIST.
974 return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
976 // Get a tree for the variable holding the defer stack.
978 defer_stack(source_location);
980 // Export the function.
982 export_func(Export*, const std::string& name) const;
984 // Export a function with a type.
986 export_func_with_type(Export*, const std::string& name,
987 const Function_type*);
989 // Import a function.
991 import_func(Import*, std::string* pname, Typed_identifier** receiver,
992 Typed_identifier_list** pparameters,
993 Typed_identifier_list** presults, bool* is_varargs);
996 // Type for mapping from label names to Label objects.
997 typedef Unordered_map(std::string, Label*) Labels;
1000 make_receiver_parm_decl(Gogo*, Named_object*, tree);
1003 copy_parm_to_heap(Gogo*, Named_object*, tree);
1006 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1008 typedef std::vector<std::pair<Named_object*,
1009 source_location> > Closure_fields;
1011 // The function's type.
1012 Function_type* type_;
1013 // The enclosing function. This is NULL when there isn't one, which
1014 // is the normal case.
1015 Function* enclosing_;
1016 // The result variables, if any.
1018 // If there is a closure, this is the list of variables which appear
1019 // in the closure. This is created by the parser, and then resolved
1020 // to a real type when we lower parse trees.
1021 Closure_fields closure_fields_;
1022 // The closure variable, passed as a parameter using the static
1023 // chain parameter. Normally NULL.
1024 Named_object* closure_var_;
1025 // The outer block of statements in the function.
1027 // The source location of the start of the function.
1028 source_location location_;
1029 // Labels defined or referenced in the function.
1031 // The function decl.
1033 // The defer stack variable. A pointer to this variable is used to
1034 // distinguish the defer stack for one function from another. This
1035 // is NULL unless we actually need a defer stack.
1036 Temporary_statement* defer_stack_;
1037 // True if the result variables are named.
1038 bool results_are_named_;
1039 // True if this function calls the predeclared recover function.
1040 bool calls_recover_;
1041 // True if this a thunk built for a function which calls recover.
1042 bool is_recover_thunk_;
1043 // True if this function already has a recover thunk.
1044 bool has_recover_thunk_;
1047 // A function declaration.
1049 class Function_declaration
1052 Function_declaration(Function_type* fntype, source_location location)
1053 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1058 { return this->fntype_; }
1062 { return this->location_; }
1066 { return this->asm_name_; }
1068 // Set the assembler name.
1070 set_asm_name(const std::string& asm_name)
1071 { this->asm_name_ = asm_name; }
1073 // Return a decl for the function given an identifier.
1075 get_or_make_decl(Gogo*, Named_object*, tree id);
1077 // Export a function declaration.
1079 export_func(Export* exp, const std::string& name) const
1080 { Function::export_func_with_type(exp, name, this->fntype_); }
1083 // The type of the function.
1084 Function_type* fntype_;
1085 // The location of the declaration.
1086 source_location location_;
1087 // The assembler name: this is the name to use in references to the
1088 // function. This is normally empty.
1089 std::string asm_name_;
1090 // The function decl if needed.
1099 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1100 bool is_receiver, source_location);
1102 // Get the type of the variable.
1109 // Return whether the type is defined yet.
1112 { return this->type_ != NULL; }
1114 // Get the initial value.
1117 { return this->init_; }
1119 // Return whether there are any preinit statements.
1121 has_pre_init() const
1122 { return this->preinit_ != NULL; }
1124 // Return the preinit statements if any.
1127 { return this->preinit_; }
1129 // Return whether this is a global variable.
1132 { return this->is_global_; }
1134 // Return whether this is a function parameter.
1136 is_parameter() const
1137 { return this->is_parameter_; }
1139 // Return whether this is the receiver parameter of a method.
1142 { return this->is_receiver_; }
1144 // Change this parameter to be a receiver. This is used when
1145 // creating the thunks created for functions which call recover.
1149 gcc_assert(this->is_parameter_);
1150 this->is_receiver_ = true;
1153 // Change this parameter to not be a receiver. This is used when
1154 // creating the thunks created for functions which call recover.
1156 set_is_not_receiver()
1158 gcc_assert(this->is_parameter_);
1159 this->is_receiver_ = false;
1162 // Return whether this is the varargs parameter of a function.
1164 is_varargs_parameter() const
1165 { return this->is_varargs_parameter_; }
1167 // Whether this variable's address is taken.
1169 is_address_taken() const
1170 { return this->is_address_taken_; }
1172 // Whether this variable should live in the heap.
1175 { return this->is_address_taken_ && !this->is_global_; }
1177 // Get the source location of the variable's declaration.
1180 { return this->location_; }
1182 // Record that this is the varargs parameter of a function.
1184 set_is_varargs_parameter()
1186 gcc_assert(this->is_parameter_);
1187 this->is_varargs_parameter_ = true;
1190 // Clear the initial value; used for error handling.
1193 { this->init_ = NULL; }
1195 // Set the initial value; used for converting shortcuts.
1197 set_init(Expression* init)
1198 { this->init_ = init; }
1200 // Get the preinit block, a block of statements to be run before the
1201 // initialization expression.
1203 preinit_block(Gogo*);
1205 // Add a statement to be run before the initialization expression.
1206 // This is only used for global variables.
1208 add_preinit_statement(Gogo*, Statement*);
1210 // Lower the initialization expression after parsing is complete.
1212 lower_init_expression(Gogo*, Named_object*);
1214 // A special case: the init value is used only to determine the
1215 // type. This is used if the variable is defined using := with the
1216 // comma-ok form of a map index or a receive expression. The init
1217 // value is actually the map index expression or receive expression.
1218 // We use this because we may not know the right type at parse time.
1220 set_type_from_init_tuple()
1221 { this->type_from_init_tuple_ = true; }
1223 // Another special case: the init value is used only to determine
1224 // the type. This is used if the variable is defined using := with
1225 // a range clause. The init value is the range expression. The
1226 // type of the variable is the index type of the range expression
1227 // (i.e., the first value returned by a range).
1229 set_type_from_range_index()
1230 { this->type_from_range_index_ = true; }
1232 // Another special case: like set_type_from_range_index, but the
1233 // type is the value type of the range expression (i.e., the second
1234 // value returned by a range).
1236 set_type_from_range_value()
1237 { this->type_from_range_value_ = true; }
1239 // Another special case: the init value is used only to determine
1240 // the type. This is used if the variable is defined using := with
1241 // a case in a select statement. The init value is the channel.
1242 // The type of the variable is the channel's element type.
1244 set_type_from_chan_element()
1245 { this->type_from_chan_element_ = true; }
1247 // After we lower the select statement, we once again set the type
1248 // from the initialization expression.
1250 clear_type_from_chan_element()
1252 gcc_assert(this->type_from_chan_element_);
1253 this->type_from_chan_element_ = false;
1256 // Note that this variable was created for a type switch clause.
1258 set_is_type_switch_var()
1259 { this->is_type_switch_var_ = true; }
1261 // Traverse the initializer expression.
1263 traverse_expression(Traverse*);
1265 // Determine the type of the variable if necessary.
1269 // Note that something takes the address of this variable.
1272 { this->is_address_taken_ = true; }
1274 // Get the backend representation of the variable.
1276 get_backend_variable(Gogo*, Named_object*, const Package*,
1277 const std::string&);
1279 // Get the initial value of the variable as a tree. This may only
1280 // be called if has_pre_init() returns false.
1282 get_init_tree(Gogo*, Named_object* function);
1284 // Return a series of statements which sets the value of the
1285 // variable in DECL. This should only be called is has_pre_init()
1286 // returns true. DECL may be NULL for a sink variable.
1288 get_init_block(Gogo*, Named_object* function, tree decl);
1290 // Export the variable.
1292 export_var(Export*, const std::string& name) const;
1294 // Import a variable.
1296 import_var(Import*, std::string* pname, Type** ptype);
1299 // The type of a tuple.
1301 type_from_tuple(Expression*, bool) const;
1303 // The type of a range.
1305 type_from_range(Expression*, bool, bool) const;
1307 // The element type of a channel.
1309 type_from_chan_element(Expression*, bool) const;
1311 // The variable's type. This may be NULL if the type is set from
1314 // The initial value. This may be NULL if the variable should be
1315 // initialized to the default value for the type.
1317 // Statements to run before the init statement.
1319 // Location of variable definition.
1320 source_location location_;
1321 // Backend representation.
1322 Bvariable* backend_;
1323 // Whether this is a global variable.
1324 bool is_global_ : 1;
1325 // Whether this is a function parameter.
1326 bool is_parameter_ : 1;
1327 // Whether this is the receiver parameter of a method.
1328 bool is_receiver_ : 1;
1329 // Whether this is the varargs parameter of a function.
1330 bool is_varargs_parameter_ : 1;
1331 // Whether something takes the address of this variable.
1332 bool is_address_taken_ : 1;
1333 // True if we have seen this variable in a traversal.
1335 // True if we have lowered the initialization expression.
1336 bool init_is_lowered_ : 1;
1337 // True if init is a tuple used to set the type.
1338 bool type_from_init_tuple_ : 1;
1339 // True if init is a range clause and the type is the index type.
1340 bool type_from_range_index_ : 1;
1341 // True if init is a range clause and the type is the value type.
1342 bool type_from_range_value_ : 1;
1343 // True if init is a channel and the type is the channel's element type.
1344 bool type_from_chan_element_ : 1;
1345 // True if this is a variable created for a type switch case.
1346 bool is_type_switch_var_ : 1;
1347 // True if we have determined types.
1348 bool determined_type_ : 1;
1351 // A variable which is really the name for a function return value, or
1354 class Result_variable
1357 Result_variable(Type* type, Function* function, int index,
1358 source_location location)
1359 : type_(type), function_(function), index_(index), location_(location),
1360 backend_(NULL), is_address_taken_(false)
1363 // Get the type of the result variable.
1366 { return this->type_; }
1368 // Get the function that this is associated with.
1371 { return this->function_; }
1373 // Index in the list of function results.
1376 { return this->index_; }
1378 // The location of the variable definition.
1381 { return this->location_; }
1383 // Whether this variable's address is taken.
1385 is_address_taken() const
1386 { return this->is_address_taken_; }
1388 // Note that something takes the address of this variable.
1391 { this->is_address_taken_ = true; }
1393 // Whether this variable should live in the heap.
1396 { return this->is_address_taken_; }
1398 // Set the function. This is used when cloning functions which call
1401 set_function(Function* function)
1402 { this->function_ = function; }
1404 // Get the backend representation of the variable.
1406 get_backend_variable(Gogo*, Named_object*, const std::string&);
1409 // Type of result variable.
1411 // Function with which this is associated.
1412 Function* function_;
1413 // Index in list of results.
1415 // Where the result variable is defined.
1416 source_location location_;
1417 // Backend representation.
1418 Bvariable* backend_;
1419 // Whether something takes the address of this variable.
1420 bool is_address_taken_;
1423 // The value we keep for a named constant. This lets us hold a type
1424 // and an expression.
1426 class Named_constant
1429 Named_constant(Type* type, Expression* expr, int iota_value,
1430 source_location location)
1431 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1437 { return this->type_; }
1441 { return this->expr_; }
1445 { return this->iota_value_; }
1449 { return this->location_; }
1451 // Whether we are lowering.
1454 { return this->lowering_; }
1456 // Set that we are lowering.
1459 { this->lowering_ = true; }
1461 // We are no longer lowering.
1464 { this->lowering_ = false; }
1466 // Traverse the expression.
1468 traverse_expression(Traverse*);
1470 // Determine the type of the constant if necessary.
1474 // Indicate that we found and reported an error for this constant.
1478 // Export the constant.
1480 export_const(Export*, const std::string& name) const;
1482 // Import a constant.
1484 import_const(Import*, std::string*, Type**, Expression**);
1487 // The type of the constant.
1489 // The expression for the constant.
1491 // If the predeclared constant iota is used in EXPR_, this is the
1492 // value it will have. We do this because at parse time we don't
1493 // know whether the name "iota" will refer to the predeclared
1494 // constant or to something else. We put in the right value in when
1497 // The location of the definition.
1498 source_location location_;
1499 // Whether we are currently lowering this constant.
1503 // A type declaration.
1505 class Type_declaration
1508 Type_declaration(source_location location)
1509 : location_(location), in_function_(NULL), methods_(),
1510 issued_warning_(false)
1513 // Return the location.
1516 { return this->location_; }
1518 // Return the function in which this type is declared. This will
1519 // return NULL for a type declared in global scope.
1522 { return this->in_function_; }
1524 // Set the function in which this type is declared.
1526 set_in_function(Named_object* f)
1527 { this->in_function_ = f; }
1529 // Add a method to this type. This is used when methods are defined
1532 add_method(const std::string& name, Function* function);
1534 // Add a method declaration to this type.
1536 add_method_declaration(const std::string& name, Function_type* type,
1537 source_location location);
1539 // Return whether any methods were defined.
1541 has_methods() const;
1543 // Define methods when the real type is known.
1545 define_methods(Named_type*);
1547 // This is called if we are trying to use this type. It returns
1548 // true if we should issue a warning.
1553 typedef std::vector<Named_object*> Methods;
1555 // The location of the type declaration.
1556 source_location location_;
1557 // If this type is declared in a function, a pointer back to the
1558 // function in which it is defined.
1559 Named_object* in_function_;
1560 // Methods defined before the type is defined.
1562 // True if we have issued a warning about a use of this type
1563 // declaration when it is undefined.
1564 bool issued_warning_;
1567 // An unknown object. These are created by the parser for forward
1568 // references to names which have not been seen before. In a correct
1569 // program, these will always point to a real definition by the end of
1570 // the parse. Because they point to another Named_object, these may
1571 // only be referenced by Unknown_expression objects.
1576 Unknown_name(source_location location)
1577 : location_(location), real_named_object_(NULL)
1580 // Return the location where this name was first seen.
1583 { return this->location_; }
1585 // Return the real named object that this points to, or NULL if it
1586 // was never resolved.
1588 real_named_object() const
1589 { return this->real_named_object_; }
1591 // Set the real named object that this points to.
1593 set_real_named_object(Named_object* no);
1596 // The location where this name was first seen.
1597 source_location location_;
1598 // The real named object when it is known.
1603 // A named object named. This is the result of a declaration. We
1604 // don't use a superclass because they all have to be handled
1612 // An uninitialized Named_object. We should never see this.
1613 NAMED_OBJECT_UNINITIALIZED,
1614 // An unknown name. This is used for forward references. In a
1615 // correct program, these will all be resolved by the end of the
1617 NAMED_OBJECT_UNKNOWN,
1622 // A forward type declaration.
1623 NAMED_OBJECT_TYPE_DECLARATION,
1626 // A result variable in a function.
1627 NAMED_OBJECT_RESULT_VAR,
1628 // The blank identifier--the special variable named _.
1632 // A forward func declaration.
1633 NAMED_OBJECT_FUNC_DECLARATION,
1635 NAMED_OBJECT_PACKAGE
1638 // Return the classification.
1640 classification() const
1641 { return this->classification_; }
1647 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1651 { return this->classification_ == NAMED_OBJECT_CONST; }
1655 { return this->classification_ == NAMED_OBJECT_TYPE; }
1658 is_type_declaration() const
1659 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1663 { return this->classification_ == NAMED_OBJECT_VAR; }
1666 is_result_variable() const
1667 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1671 { return this->classification_ == NAMED_OBJECT_SINK; }
1675 { return this->classification_ == NAMED_OBJECT_FUNC; }
1678 is_function_declaration() const
1679 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1683 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1687 static Named_object*
1688 make_unknown_name(const std::string& name, source_location);
1690 static Named_object*
1691 make_constant(const Typed_identifier&, const Package*, Expression*,
1694 static Named_object*
1695 make_type(const std::string&, const Package*, Type*, source_location);
1697 static Named_object*
1698 make_type_declaration(const std::string&, const Package*, source_location);
1700 static Named_object*
1701 make_variable(const std::string&, const Package*, Variable*);
1703 static Named_object*
1704 make_result_variable(const std::string&, Result_variable*);
1706 static Named_object*
1709 static Named_object*
1710 make_function(const std::string&, const Package*, Function*);
1712 static Named_object*
1713 make_function_declaration(const std::string&, const Package*, Function_type*,
1716 static Named_object*
1717 make_package(const std::string& alias, Package* package);
1724 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1725 return this->u_.unknown_value;
1729 unknown_value() const
1731 gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1732 return this->u_.unknown_value;
1738 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1739 return this->u_.const_value;
1742 const Named_constant*
1745 gcc_assert(this->classification_ == NAMED_OBJECT_CONST);
1746 return this->u_.const_value;
1752 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1753 return this->u_.type_value;
1759 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE);
1760 return this->u_.type_value;
1764 type_declaration_value()
1766 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1767 return this->u_.type_declaration;
1770 const Type_declaration*
1771 type_declaration_value() const
1773 gcc_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1774 return this->u_.type_declaration;
1780 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1781 return this->u_.var_value;
1787 gcc_assert(this->classification_ == NAMED_OBJECT_VAR);
1788 return this->u_.var_value;
1794 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1795 return this->u_.result_var_value;
1798 const Result_variable*
1799 result_var_value() const
1801 gcc_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1802 return this->u_.result_var_value;
1808 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1809 return this->u_.func_value;
1815 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC);
1816 return this->u_.func_value;
1819 Function_declaration*
1820 func_declaration_value()
1822 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1823 return this->u_.func_declaration_value;
1826 const Function_declaration*
1827 func_declaration_value() const
1829 gcc_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1830 return this->u_.func_declaration_value;
1836 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1837 return this->u_.package_value;
1841 package_value() const
1843 gcc_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1844 return this->u_.package_value;
1849 { return this->name_; }
1851 // Return the name to use in an error message. The difference is
1852 // that if this Named_object is defined in a different package, this
1853 // will return PACKAGE.NAME.
1855 message_name() const;
1859 { return this->package_; }
1861 // Resolve an unknown value if possible. This returns the same
1862 // Named_object or a new one.
1866 Named_object* ret = this;
1867 if (this->is_unknown())
1869 Named_object* r = this->unknown_value()->real_named_object();
1879 const Named_object* ret = this;
1880 if (this->is_unknown())
1882 const Named_object* r = this->unknown_value()->real_named_object();
1889 // The location where this object was defined or referenced.
1893 // Convert a variable to the backend representation.
1895 get_backend_variable(Gogo*, Named_object* function);
1897 // Return a tree for the external identifier for this object.
1901 // Return a tree representing this object.
1903 get_tree(Gogo*, Named_object* function);
1905 // Define a type declaration.
1907 set_type_value(Named_type*);
1909 // Define a function declaration.
1911 set_function_value(Function*);
1913 // Declare an unknown name as a type declaration.
1917 // Export this object.
1919 export_named_object(Export*) const;
1922 Named_object(const std::string&, const Package*, Classification);
1924 // The name of the object.
1926 // The package that this object is in. This is NULL if it is in the
1927 // file we are compiling.
1928 const Package* package_;
1929 // The type of object this is.
1930 Classification classification_;
1934 Unknown_name* unknown_value;
1935 Named_constant* const_value;
1936 Named_type* type_value;
1937 Type_declaration* type_declaration;
1938 Variable* var_value;
1939 Result_variable* result_var_value;
1940 Function* func_value;
1941 Function_declaration* func_declaration_value;
1942 Package* package_value;
1944 // The DECL tree for this object if we have already converted it.
1948 // A binding contour. This binds names to objects.
1953 // Type for mapping from names to objects.
1954 typedef Unordered_map(std::string, Named_object*) Contour;
1956 Bindings(Bindings* enclosing);
1958 // Add an unknown name.
1960 add_unknown_name(const std::string& name, source_location location)
1962 return this->add_named_object(Named_object::make_unknown_name(name,
1968 add_constant(const Typed_identifier& tid, const Package* package,
1969 Expression* expr, int iota_value)
1971 return this->add_named_object(Named_object::make_constant(tid, package,
1978 add_type(const std::string& name, const Package* package, Type* type,
1979 source_location location)
1981 return this->add_named_object(Named_object::make_type(name, package, type,
1985 // Add a named type. This is used for builtin types, and to add an
1986 // imported type to the global scope.
1988 add_named_type(Named_type* named_type);
1990 // Add a type declaration.
1992 add_type_declaration(const std::string& name, const Package* package,
1993 source_location location)
1995 Named_object* no = Named_object::make_type_declaration(name, package,
1997 return this->add_named_object(no);
2002 add_variable(const std::string& name, const Package* package,
2005 return this->add_named_object(Named_object::make_variable(name, package,
2009 // Add a result variable.
2011 add_result_variable(const std::string& name, Result_variable* result)
2013 return this->add_named_object(Named_object::make_result_variable(name,
2019 add_function(const std::string& name, const Package*, Function* function);
2021 // Add a function declaration.
2023 add_function_declaration(const std::string& name, const Package* package,
2024 Function_type* type, source_location location);
2026 // Add a package. The location is the location of the import
2029 add_package(const std::string& alias, Package* package)
2031 Named_object* no = Named_object::make_package(alias, package);
2032 return this->add_named_object(no);
2035 // Define a type which was already declared.
2037 define_type(Named_object*, Named_type*);
2039 // Add a method to the list of objects. This is not added to the
2042 add_method(Named_object*);
2044 // Add a named object to this binding.
2046 add_named_object(Named_object* no)
2047 { return this->add_named_object_to_contour(&this->bindings_, no); }
2049 // Clear all names in file scope from the bindings.
2053 // Look up a name in this binding contour and in any enclosing
2054 // binding contours. This returns NULL if the name is not found.
2056 lookup(const std::string&) const;
2058 // Look up a name in this binding contour without looking in any
2059 // enclosing binding contours. Returns NULL if the name is not found.
2061 lookup_local(const std::string&) const;
2065 remove_binding(Named_object*);
2067 // Traverse the tree. See the Traverse class.
2069 traverse(Traverse*, bool is_global);
2071 // Iterate over definitions. This does not include things which
2072 // were only declared.
2074 typedef std::vector<Named_object*>::const_iterator
2075 const_definitions_iterator;
2077 const_definitions_iterator
2078 begin_definitions() const
2079 { return this->named_objects_.begin(); }
2081 const_definitions_iterator
2082 end_definitions() const
2083 { return this->named_objects_.end(); }
2085 // Return the number of definitions.
2087 size_definitions() const
2088 { return this->named_objects_.size(); }
2090 // Return whether there are no definitions.
2092 empty_definitions() const
2093 { return this->named_objects_.empty(); }
2095 // Iterate over declarations. This is everything that has been
2096 // declared, which includes everything which has been defined.
2098 typedef Contour::const_iterator const_declarations_iterator;
2100 const_declarations_iterator
2101 begin_declarations() const
2102 { return this->bindings_.begin(); }
2104 const_declarations_iterator
2105 end_declarations() const
2106 { return this->bindings_.end(); }
2108 // Return the number of declarations.
2110 size_declarations() const
2111 { return this->bindings_.size(); }
2113 // Return whether there are no declarations.
2115 empty_declarations() const
2116 { return this->bindings_.empty(); }
2118 // Return the first declaration.
2121 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2125 add_named_object_to_contour(Contour*, Named_object*);
2128 new_definition(Named_object*, Named_object*);
2130 // Enclosing bindings.
2131 Bindings* enclosing_;
2132 // The list of objects.
2133 std::vector<Named_object*> named_objects_;
2134 // The mapping from names to objects.
2143 Label(const std::string& name)
2144 : name_(name), location_(0), is_used_(false), blabel_(NULL)
2147 // Return the label's name.
2150 { return this->name_; }
2152 // Return whether the label has been defined.
2155 { return this->location_ != 0; }
2157 // Return whether the label has been used.
2160 { return this->is_used_; }
2162 // Record that the label is used.
2165 { this->is_used_ = true; }
2167 // Return the location of the definition.
2170 { return this->location_; }
2172 // Define the label at LOCATION.
2174 define(source_location location)
2176 gcc_assert(this->location_ == 0);
2177 this->location_ = location;
2180 // Return the backend representation for this label.
2182 get_backend_label(Translate_context*);
2184 // Return an expression for the address of this label. This is used
2185 // to get the return address of a deferred function to see whether
2186 // the function may call recover.
2188 get_addr(Translate_context*, source_location location);
2191 // The name of the label.
2193 // The location of the definition. This is 0 if the label has not
2194 // yet been defined.
2195 source_location location_;
2196 // Whether the label has been used.
2198 // The backend representation.
2202 // An unnamed label. These are used when lowering loops.
2207 Unnamed_label(source_location location)
2208 : location_(location), blabel_(NULL)
2211 // Get the location where the label is defined.
2214 { return this->location_; }
2216 // Set the location where the label is defined.
2218 set_location(source_location location)
2219 { this->location_ = location; }
2221 // Return a statement which defines this label.
2223 get_definition(Translate_context*);
2225 // Return a goto to this label from LOCATION.
2227 get_goto(Translate_context*, source_location location);
2230 // Return the backend representation.
2232 get_blabel(Translate_context*);
2234 // The location where the label is defined.
2235 source_location location_;
2236 // The backend representation of this label.
2240 // An imported package.
2245 Package(const std::string& name, const std::string& unique_prefix,
2246 source_location location);
2248 // The real name of this package. This may be different from the
2249 // name in the associated Named_object if the import statement used
2253 { return this->name_; }
2255 // Return the location of the import statement.
2258 { return this->location_; }
2260 // Get the unique prefix used for all symbols exported from this
2263 unique_prefix() const
2265 gcc_assert(!this->unique_prefix_.empty());
2266 return this->unique_prefix_;
2269 // The priority of this package. The init function of packages with
2270 // lower priority must be run before the init function of packages
2271 // with higher priority.
2274 { return this->priority_; }
2276 // Set the priority.
2278 set_priority(int priority);
2280 // Return the bindings.
2283 { return this->bindings_; }
2285 // Whether some symbol from the package was used.
2288 { return this->used_; }
2290 // Note that some symbol from this package was used.
2293 { this->used_ = true; }
2295 // Clear the used field for the next file.
2298 { this->used_ = false; }
2300 // Whether this package was imported in the current file.
2303 { return this->is_imported_; }
2305 // Note that this package was imported in the current file.
2308 { this->is_imported_ = true; }
2310 // Clear the imported field for the next file.
2313 { this->is_imported_ = false; }
2315 // Whether this package was imported with a name of "_".
2317 uses_sink_alias() const
2318 { return this->uses_sink_alias_; }
2320 // Note that this package was imported with a name of "_".
2322 set_uses_sink_alias()
2323 { this->uses_sink_alias_ = true; }
2325 // Clear the sink alias field for the next file.
2327 clear_uses_sink_alias()
2328 { this->uses_sink_alias_ = false; }
2330 // Look up a name in the package. Returns NULL if the name is not
2333 lookup(const std::string& name) const
2334 { return this->bindings_->lookup(name); }
2336 // Set the location of the package. This is used if it is seen in a
2337 // different import before it is really imported.
2339 set_location(source_location location)
2340 { this->location_ = location; }
2342 // Add a constant to the package.
2344 add_constant(const Typed_identifier& tid, Expression* expr)
2345 { return this->bindings_->add_constant(tid, this, expr, 0); }
2347 // Add a type to the package.
2349 add_type(const std::string& name, Type* type, source_location location)
2350 { return this->bindings_->add_type(name, this, type, location); }
2352 // Add a type declaration to the package.
2354 add_type_declaration(const std::string& name, source_location location)
2355 { return this->bindings_->add_type_declaration(name, this, location); }
2357 // Add a variable to the package.
2359 add_variable(const std::string& name, Variable* variable)
2360 { return this->bindings_->add_variable(name, this, variable); }
2362 // Add a function declaration to the package.
2364 add_function_declaration(const std::string& name, Function_type* type,
2365 source_location loc)
2366 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2368 // Determine types of constants.
2373 // The real name of this package.
2375 // The unique prefix for all exported global symbols.
2376 std::string unique_prefix_;
2377 // The names in this package.
2378 Bindings* bindings_;
2379 // The priority of this package. A package has a priority higher
2380 // than the priority of all of the packages that it imports. This
2381 // is used to run init functions in the right order.
2383 // The location of the import statement.
2384 source_location location_;
2385 // True if some name from this package was used. This is mutable
2386 // because we can use a package even if we have a const pointer to
2389 // True if this package was imported in the current file.
2391 // True if this package was imported with a name of "_".
2392 bool uses_sink_alias_;
2395 // Return codes for the traversal functions. This is not an enum
2396 // because we want to be able to declare traversal functions in other
2397 // header files without including this one.
2399 // Continue traversal as usual.
2400 const int TRAVERSE_CONTINUE = -1;
2403 const int TRAVERSE_EXIT = 0;
2405 // Continue traversal, but skip components of the current object.
2406 // E.g., if this is returned by Traverse::statement, we do not
2407 // traverse the expressions in the statement even if
2408 // traverse_expressions is set in the traverse_mask.
2409 const int TRAVERSE_SKIP_COMPONENTS = 1;
2411 // This class is used when traversing the parse tree. The caller uses
2412 // a subclass which overrides functions as desired.
2417 // These bitmasks say what to traverse.
2418 static const unsigned int traverse_variables = 0x1;
2419 static const unsigned int traverse_constants = 0x2;
2420 static const unsigned int traverse_functions = 0x4;
2421 static const unsigned int traverse_blocks = 0x8;
2422 static const unsigned int traverse_statements = 0x10;
2423 static const unsigned int traverse_expressions = 0x20;
2424 static const unsigned int traverse_types = 0x40;
2426 Traverse(unsigned int traverse_mask)
2427 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2430 virtual ~Traverse();
2432 // The bitmask of what to traverse.
2434 traverse_mask() const
2435 { return this->traverse_mask_; }
2437 // Record that we are going to traverse a type. This returns true
2438 // if the type has already been seen in this traversal. This is
2439 // required because types, unlike expressions, can form a circular
2442 remember_type(const Type*);
2444 // Record that we are going to see an expression. This returns true
2445 // if the expression has already been seen in this traversal. This
2446 // is only needed for cases where multiple expressions can point to
2449 remember_expression(const Expression*);
2451 // These functions return one of the TRAVERSE codes defined above.
2453 // If traverse_variables is set in the mask, this is called for
2454 // every variable in the tree.
2456 variable(Named_object*);
2458 // If traverse_constants is set in the mask, this is called for
2459 // every named constant in the tree. The bool parameter is true for
2460 // a global constant.
2462 constant(Named_object*, bool);
2464 // If traverse_functions is set in the mask, this is called for
2465 // every function in the tree.
2467 function(Named_object*);
2469 // If traverse_blocks is set in the mask, this is called for every
2470 // block in the tree.
2474 // If traverse_statements is set in the mask, this is called for
2475 // every statement in the tree.
2477 statement(Block*, size_t* index, Statement*);
2479 // If traverse_expressions is set in the mask, this is called for
2480 // every expression in the tree.
2482 expression(Expression**);
2484 // If traverse_types is set in the mask, this is called for every
2485 // type in the tree.
2490 typedef Unordered_set_hash(const Type*, Type_hash_identical,
2491 Type_identical) Types_seen;
2493 typedef Unordered_set(const Expression*) Expressions_seen;
2495 // Bitmask of what sort of objects to traverse.
2496 unsigned int traverse_mask_;
2497 // Types which have been seen in this traversal.
2498 Types_seen* types_seen_;
2499 // Expressions which have been seen in this traversal.
2500 Expressions_seen* expressions_seen_;
2503 // When translating the gogo IR into the backend data structure, this
2504 // is the context we pass down the blocks and statements.
2506 class Translate_context
2509 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2511 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2512 block_(block), block_tree_(block_tree), is_const_(false)
2519 { return this->gogo_; }
2523 { return this->backend_; }
2527 { return this->function_; }
2531 { return this->block_; }
2535 { return this->block_tree_; }
2539 { return this->is_const_; }
2541 // Make a constant context.
2544 { this->is_const_ = true; }
2547 // The IR for the entire compilation unit.
2549 // The generator for the backend data structures.
2551 // The function we are currently translating.
2552 Named_object* function_;
2553 // The block we are currently translating.
2555 // The BLOCK node for the current block.
2557 // Whether this is being evaluated in a constant context. This is
2558 // used for type descriptor initializers.
2562 // Runtime error codes. These must match the values in
2563 // libgo/runtime/go-runtime-error.c.
2565 // Slice index out of bounds: negative or larger than the length of
2567 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2569 // Array index out of bounds.
2570 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2572 // String index out of bounds.
2573 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2575 // Slice slice out of bounds: negative or larger than the length of
2576 // the slice or high bound less than low bound.
2577 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2579 // Array slice out of bounds.
2580 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2582 // String slice out of bounds.
2583 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2585 // Dereference of nil pointer. This is used when there is a
2586 // dereference of a pointer to a very large struct or array, to ensure
2587 // that a gigantic array is not used a proxy to access random memory
2589 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2591 // Slice length or capacity out of bounds in make: negative or
2592 // overflow or length greater than capacity.
2593 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2595 // Map capacity out of bounds in make: negative or overflow.
2596 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2598 // Channel capacity out of bounds in make: negative or overflow.
2599 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2601 // This is used by some of the langhooks.
2602 extern Gogo* go_get_gogo();
2604 // Whether we have seen any errors. FIXME: Replace with a backend
2606 extern bool saw_errors();
2608 #endif // !defined(GO_GOGO_H)