1 // gogo.h -- Go frontend parsed representation. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
10 #include "go-linemap.h"
13 class Statement_inserter;
15 class Type_hash_identical;
18 class Typed_identifier;
19 class Typed_identifier_list;
23 class Temporary_statement;
27 class Bindings_snapshot;
33 class Struct_field_list;
39 class Forward_declaration_type;
44 class Translate_context;
54 // This file declares the basic classes used to hold the internal
55 // representation of Go which is built by the parser.
57 // An initialization function for an imported package. This is a
58 // magic function which initializes variables and runs the "init"
64 Import_init(const std::string& package_name, const std::string& init_name,
66 : package_name_(package_name), init_name_(init_name), priority_(priority)
69 // The name of the package being imported.
72 { return this->package_name_; }
74 // The name of the package's init function.
77 { return this->init_name_; }
79 // The priority of the initialization function. Functions with a
80 // lower priority number must be run first.
83 { return this->priority_; }
86 // The name of the package being imported.
87 std::string package_name_;
88 // The name of the package's init function.
89 std::string init_name_;
94 // For sorting purposes.
97 operator<(const Import_init& i1, const Import_init& i2)
99 if (i1.priority() < i2.priority())
101 if (i1.priority() > i2.priority())
103 if (i1.package_name() != i2.package_name())
104 return i1.package_name() < i2.package_name();
105 return i1.init_name() < i2.init_name();
108 // The holder for the internal representation of the entire
114 // Create the IR, passing in the sizes of the types "int" and
115 // "uintptr" in bits.
116 Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size);
118 // Get the backend generator.
121 { return this->backend_; }
123 // Get the Location generator.
126 { return this->linemap_; }
128 // Get the package name.
130 package_name() const;
132 // Set the package name.
134 set_package_name(const std::string&, Location);
136 // Return whether this is the "main" package.
138 is_main_package() const;
140 // If necessary, adjust the name to use for a hidden symbol. We add
141 // a prefix of the package name, so that hidden symbols in different
142 // packages do not collide.
144 pack_hidden_name(const std::string& name, bool is_exported) const
148 : ('.' + this->unique_prefix()
149 + '.' + this->package_name()
153 // Unpack a name which may have been hidden. Returns the
154 // user-visible name of the object.
156 unpack_hidden_name(const std::string& name)
157 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
159 // Return whether a possibly packed name is hidden.
161 is_hidden_name(const std::string& name)
162 { return name[0] == '.'; }
164 // Return the package prefix of a hidden name.
166 hidden_name_prefix(const std::string& name)
168 go_assert(Gogo::is_hidden_name(name));
169 return name.substr(1, name.rfind('.') - 1);
172 // Given a name which may or may not have been hidden, return the
173 // name to use in an error message.
175 message_name(const std::string& name);
177 // Return whether a name is the blank identifier _.
179 is_sink_name(const std::string& name)
181 return (name[0] == '.'
182 && name[name.length() - 1] == '_'
183 && name[name.length() - 2] == '.');
186 // Return the unique prefix to use for all exported symbols.
188 unique_prefix() const;
190 // Set the unique prefix.
192 set_unique_prefix(const std::string&);
194 // Return the priority to use for the package we are compiling.
195 // This is two more than the largest priority of any package we
198 package_priority() const;
200 // Import a package. FILENAME is the file name argument, LOCAL_NAME
201 // is the local name to give to the package. If LOCAL_NAME is empty
202 // the declarations are added to the global scope.
204 import_package(const std::string& filename, const std::string& local_name,
205 bool is_local_name_exported, Location);
207 // Whether we are the global binding level.
209 in_global_scope() const;
211 // Look up a name in the current binding contours.
213 lookup(const std::string&, Named_object** pfunction) const;
215 // Look up a name in the current block.
217 lookup_in_block(const std::string&) const;
219 // Look up a name in the global namespace--the universal scope.
221 lookup_global(const char*) const;
223 // Add a new imported package. REAL_NAME is the real name of the
224 // package. ALIAS is the alias of the package; this may be the same
225 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
226 // this package should be added to the global namespace; this is
227 // true if the alias is ".". LOCATION is the location of the import
228 // statement. This returns the new package, or NULL on error.
230 add_imported_package(const std::string& real_name, const std::string& alias,
231 bool is_alias_exported,
232 const std::string& unique_prefix,
234 bool* padd_to_globals);
236 // Register a package. This package may or may not be imported.
237 // This returns the Package structure for the package, creating if
240 register_package(const std::string& name, const std::string& unique_prefix,
243 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
244 // method function should be added to the type of its receiver.
246 start_function(const std::string& name, Function_type* type,
247 bool add_method_to_type, Location);
249 // Finish compiling a function.
251 finish_function(Location);
253 // Return the current function.
255 current_function() const;
257 // Return the current block.
261 // Start a new block. This is not initially associated with a
264 start_block(Location);
266 // Finish the current block and return it.
268 finish_block(Location);
270 // Declare an unknown name. This is used while parsing. The name
271 // must be resolved by the end of the parse. Unknown names are
272 // always added at the package level.
274 add_unknown_name(const std::string& name, Location);
276 // Declare a function.
278 declare_function(const std::string&, Function_type*, Location);
280 // Declare a function at the package level. This is used for
281 // functions generated for a type.
283 declare_package_function(const std::string&, Function_type*, Location);
287 add_label_definition(const std::string&, Location);
289 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
290 // report errors for a goto from the current location to the label
293 add_label_reference(const std::string&, Location,
294 bool issue_goto_errors);
296 // Return a snapshot of the current binding state.
298 bindings_snapshot(Location);
300 // Add a statement to the current block.
302 add_statement(Statement*);
304 // Add a block to the current block.
306 add_block(Block*, Location);
310 add_constant(const Typed_identifier&, Expression*, int iota_value);
314 add_type(const std::string&, Type*, Location);
316 // Add a named type. This is used for builtin types, and to add an
317 // imported type to the global scope.
319 add_named_type(Named_type*);
323 declare_type(const std::string&, Location);
325 // Declare a type at the package level. This is used when the
326 // parser sees an unknown name where a type name is required.
328 declare_package_type(const std::string&, Location);
330 // Define a type which was already declared.
332 define_type(Named_object*, Named_type*);
336 add_variable(const std::string&, Variable*);
338 // Add a sink--a reference to the blank identifier _.
342 // Add a named object to the current namespace. This is used for
343 // import . "package".
345 add_named_object(Named_object*);
347 // Return a name to use for a thunk function. A thunk function is
348 // one we create during the compilation, for a go statement or a
349 // defer statement or a method expression.
353 // Return whether an object is a thunk.
355 is_thunk(const Named_object*);
357 // Note that we've seen an interface type. This is used to build
358 // all required interface method tables.
360 record_interface_type(Interface_type*);
362 // Note that we need an initialization function.
365 { this->need_init_fn_ = true; }
367 // Clear out all names in file scope. This is called when we start
368 // parsing a new file.
372 // Queue up a type-specific function to be written out. This is
373 // used when a type-specific function is needed when not at the top
376 queue_specific_type_function(Type* type, Named_type* name,
377 const std::string& hash_name,
378 Function_type* hash_fntype,
379 const std::string& equal_name,
380 Function_type* equal_fntype);
382 // Write out queued specific type functions.
384 write_specific_type_functions();
386 // Traverse the tree. See the Traverse class.
390 // Define the predeclared global names.
392 define_global_names();
394 // Verify and complete all types.
398 // Lower the parse tree.
402 // Lower all the statements in a block.
404 lower_block(Named_object* function, Block*);
406 // Lower an expression.
408 lower_expression(Named_object* function, Statement_inserter*, Expression**);
412 lower_constant(Named_object*);
414 // Finalize the method lists and build stub methods for named types.
418 // Work out the types to use for unspecified variables and
423 // Type check the program.
427 // Check the types in a single block. This is used for complicated
430 check_types_in_block(Block*);
432 // Check for return statements.
434 check_return_statements();
440 // Add an import control function for an imported package to the
443 add_import_init_fn(const std::string& package_name,
444 const std::string& init_name, int prio);
446 // Turn short-cut operators (&&, ||) into explicit if statements.
450 // Use temporary variables to force order of evaluation.
454 // Build thunks for functions which call recover.
456 build_recover_thunks();
458 // Simplify statements which might use thunks: go and defer
461 simplify_thunk_statements();
463 // Dump AST if -fgo-dump-ast is set
465 dump_ast(const char* basename);
467 // Convert named types to the backend representation.
469 convert_named_types();
471 // Convert named types in a list of bindings.
473 convert_named_types_in_bindings(Bindings*);
475 // True if named types have been converted to the backend
478 named_types_are_converted() const
479 { return this->named_types_are_converted_; }
481 // Write out the global values.
485 // Create trees for implicit builtin functions.
487 define_builtin_function_trees();
489 // Build a call to a builtin function. PDECL should point to a NULL
490 // initialized static pointer which will hold the fndecl. NAME is
491 // the name of the function. NARGS is the number of arguments.
492 // RETTYPE is the return type. It is followed by NARGS pairs of
493 // type and argument (both trees).
495 call_builtin(tree* pdecl, Location, const char* name, int nargs,
498 // Build a call to the runtime error function.
500 runtime_error(int code, Location);
502 // Build a builtin struct with a list of fields.
504 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
507 // Mark a function declaration as a builtin library function.
509 mark_fndecl_as_builtin_library(tree fndecl);
511 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
512 // the slice. VALUES points to the values. COUNT is the size,
513 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
516 slice_constructor(tree slice_type_tree, tree values, tree count,
519 // Build required interface method tables.
521 build_interface_method_tables();
523 // Build an interface method table for a type: a list of function
524 // pointers, one for each interface method. This returns a decl.
526 interface_method_table_for_type(const Interface_type*, Named_type*,
529 // Return a tree which allocate SIZE bytes to hold values of type
532 allocate_memory(Type *type, tree size, Location);
534 // Return a type to use for pointer to const char.
536 const_char_pointer_type_tree();
538 // Build a string constant with the right type.
540 string_constant_tree(const std::string&);
542 // Build a Go string constant. This returns a pointer to the
545 go_string_constant_tree(const std::string&);
547 // Receive a value from a channel.
549 receive_from_channel(tree type_tree, tree type_descriptor_tree, tree channel,
552 // Make a trampoline which calls FNADDR passing CLOSURE.
554 make_trampoline(tree fnaddr, tree closure, Location);
557 // During parsing, we keep a stack of functions. Each function on
558 // the stack is one that we are currently parsing. For each
559 // function, we keep track of the current stack of blocks.
563 Named_object* function;
564 // The stack of active blocks in the function.
565 std::vector<Block*> blocks;
568 // The stack of functions.
569 typedef std::vector<Open_function> Open_functions;
571 // Set up the built-in unsafe package.
573 import_unsafe(const std::string&, bool is_exported, Location);
575 // Add a new imported package.
577 add_package(const std::string& real_name, const std::string& alias,
578 const std::string& unique_prefix, Location location);
580 // Return the current binding contour.
585 current_bindings() const;
587 // Get the name of the magic initialization function.
591 // Get the decl for the magic initialization function.
593 initialization_function_decl();
595 // Write the magic initialization function.
597 write_initialization_function(tree fndecl, tree init_stmt_list);
599 // Initialize imported packages.
603 // Register variables with the garbage collector.
605 register_gc_vars(const std::vector<Named_object*>&, tree*);
607 // Build a pointer to a Go string constant. This returns a pointer
610 ptr_go_string_constant_tree(const std::string&);
612 // Return the type of a trampoline.
614 trampoline_type_tree();
616 // Type used to map import names to packages.
617 typedef std::map<std::string, Package*> Imports;
619 // Type used to map package names to packages.
620 typedef std::map<std::string, Package*> Packages;
622 // Type used to map special names in the sys package.
623 typedef std::map<std::string, std::string> Sys_names;
625 // Type used to queue writing a type specific function.
626 struct Specific_type_function
630 std::string hash_name;
631 Function_type* hash_fntype;
632 std::string equal_name;
633 Function_type* equal_fntype;
635 Specific_type_function(Type* atype, Named_type* aname,
636 const std::string& ahash_name,
637 Function_type* ahash_fntype,
638 const std::string& aequal_name,
639 Function_type* aequal_fntype)
640 : type(atype), name(aname), hash_name(ahash_name),
641 hash_fntype(ahash_fntype), equal_name(aequal_name),
642 equal_fntype(aequal_fntype)
646 // The backend generator.
648 // The object used to keep track of file names and line numbers.
650 // The package we are compiling.
652 // The list of currently open functions during parsing.
653 Open_functions functions_;
654 // The global binding contour. This includes the builtin functions
655 // and the package we are compiling.
657 // Mapping from import file names to packages.
659 // Whether the magic unsafe package was imported.
660 bool imported_unsafe_;
661 // Mapping from package names we have seen to packages. This does
662 // not include the package we are compiling.
664 // The functions named "init", if there are any.
665 std::vector<Named_object*> init_functions_;
666 // Whether we need a magic initialization function.
668 // The name of the magic initialization function.
669 std::string init_fn_name_;
670 // A list of import control variables for packages that we import.
671 std::set<Import_init> imported_init_fns_;
672 // The unique prefix used for all global symbols.
673 std::string unique_prefix_;
674 // Whether an explicit unique prefix was set by -fgo-prefix.
675 bool unique_prefix_specified_;
676 // A list of interface types defined while parsing.
677 std::vector<Interface_type*> interface_types_;
678 // Type specific functions to write out.
679 std::vector<Specific_type_function*> specific_type_functions_;
680 // Whether we are done writing out specific type functions.
681 bool specific_type_functions_are_written_;
682 // Whether named types have been converted.
683 bool named_types_are_converted_;
686 // A block of statements.
691 Block(Block* enclosing, Location);
693 // Return the enclosing block.
696 { return this->enclosing_; }
698 // Return the bindings of the block.
701 { return this->bindings_; }
705 { return this->bindings_; }
707 // Look at the block's statements.
708 const std::vector<Statement*>*
710 { return &this->statements_; }
712 // Return the start location. This is normally the location of the
713 // left curly brace which starts the block.
715 start_location() const
716 { return this->start_location_; }
718 // Return the end location. This is normally the location of the
719 // right curly brace which ends the block.
722 { return this->end_location_; }
724 // Add a statement to the block.
726 add_statement(Statement*);
728 // Add a statement to the front of the block.
730 add_statement_at_front(Statement*);
732 // Replace a statement in a block.
734 replace_statement(size_t index, Statement*);
736 // Add a Statement before statement number INDEX.
738 insert_statement_before(size_t index, Statement*);
740 // Add a Statement after statement number INDEX.
742 insert_statement_after(size_t index, Statement*);
744 // Set the end location of the block.
746 set_end_location(Location location)
747 { this->end_location_ = location; }
749 // Traverse the tree.
753 // Set final types for unspecified variables and constants.
757 // Return true if execution of this block may fall through to the
760 may_fall_through() const;
762 // Convert the block to the backend representation.
764 get_backend(Translate_context*);
766 // Iterate over statements.
768 typedef std::vector<Statement*>::iterator iterator;
772 { return this->statements_.begin(); }
776 { return this->statements_.end(); }
781 // Statements in the block.
782 std::vector<Statement*> statements_;
785 // Location of start of block.
786 Location start_location_;
787 // Location of end of block.
788 Location end_location_;
796 Function(Function_type* type, Function*, Block*, Location);
798 // Return the function's type.
801 { return this->type_; }
803 // Return the enclosing function if there is one.
806 { return this->enclosing_; }
808 // Set the enclosing function. This is used when building thunks
809 // for functions which call recover.
811 set_enclosing(Function* enclosing)
813 go_assert(this->enclosing_ == NULL);
814 this->enclosing_ = enclosing;
817 // The result variables.
818 typedef std::vector<Named_object*> Results;
820 // Create the result variables in the outer block.
822 create_result_variables(Gogo*);
824 // Update the named result variables when cloning a function which
827 update_result_variables();
829 // Return the result variables.
832 { return this->results_; }
834 // Whether the result variables have names.
836 results_are_named() const
837 { return this->results_are_named_; }
839 // Add a new field to the closure variable.
841 add_closure_field(Named_object* var, Location loc)
842 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
844 // Whether this function needs a closure.
846 needs_closure() const
847 { return !this->closure_fields_.empty(); }
849 // Return the closure variable, creating it if necessary. This is
850 // passed to the function as a static chain parameter.
854 // Set the closure variable. This is used when building thunks for
855 // functions which call recover.
857 set_closure_var(Named_object* v)
859 go_assert(this->closure_var_ == NULL);
860 this->closure_var_ = v;
863 // Return the variable for a reference to field INDEX in the closure
866 enclosing_var(unsigned int index)
868 go_assert(index < this->closure_fields_.size());
869 return closure_fields_[index].first;
872 // Set the type of the closure variable if there is one.
876 // Get the block of statements associated with the function.
879 { return this->block_; }
881 // Get the location of the start of the function.
884 { return this->location_; }
886 // Return whether this function is actually a method.
890 // Add a label definition to the function.
892 add_label_definition(Gogo*, const std::string& label_name, Location);
894 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
895 // if we should report errors for a goto from the current location
896 // to the label location.
898 add_label_reference(Gogo*, const std::string& label_name,
899 Location, bool issue_goto_errors);
901 // Warn about labels that are defined but not used.
903 check_labels() const;
905 // Whether this function calls the predeclared recover function.
907 calls_recover() const
908 { return this->calls_recover_; }
910 // Record that this function calls the predeclared recover function.
911 // This is set during the lowering pass.
914 { this->calls_recover_ = true; }
916 // Whether this is a recover thunk function.
918 is_recover_thunk() const
919 { return this->is_recover_thunk_; }
921 // Record that this is a thunk built for a function which calls
924 set_is_recover_thunk()
925 { this->is_recover_thunk_ = true; }
927 // Whether this function already has a recover thunk.
929 has_recover_thunk() const
930 { return this->has_recover_thunk_; }
932 // Record that this function already has a recover thunk.
934 set_has_recover_thunk()
935 { this->has_recover_thunk_ = true; }
937 // Swap with another function. Used only for the thunk which calls
940 swap_for_recover(Function *);
942 // Traverse the tree.
946 // Determine types in the function.
950 // Return the function's decl given an identifier.
952 get_or_make_decl(Gogo*, Named_object*, tree id);
954 // Return the function's decl after it has been built.
958 go_assert(this->fndecl_ != NULL);
959 return this->fndecl_;
962 // Set the function decl to hold a tree of the function code.
964 build_tree(Gogo*, Named_object*);
966 // Get the value to return when not explicitly specified. May also
967 // add statements to execute first to STMT_LIST.
969 return_value(Gogo*, Named_object*, Location, tree* stmt_list) const;
971 // Get a tree for the variable holding the defer stack.
973 defer_stack(Location);
975 // Export the function.
977 export_func(Export*, const std::string& name) const;
979 // Export a function with a type.
981 export_func_with_type(Export*, const std::string& name,
982 const Function_type*);
984 // Import a function.
986 import_func(Import*, std::string* pname, Typed_identifier** receiver,
987 Typed_identifier_list** pparameters,
988 Typed_identifier_list** presults, bool* is_varargs);
991 // Type for mapping from label names to Label objects.
992 typedef Unordered_map(std::string, Label*) Labels;
995 make_receiver_parm_decl(Gogo*, Named_object*, tree);
998 copy_parm_to_heap(Gogo*, Named_object*, tree);
1001 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1003 typedef std::vector<std::pair<Named_object*,
1004 Location> > Closure_fields;
1006 // The function's type.
1007 Function_type* type_;
1008 // The enclosing function. This is NULL when there isn't one, which
1009 // is the normal case.
1010 Function* enclosing_;
1011 // The result variables, if any.
1013 // If there is a closure, this is the list of variables which appear
1014 // in the closure. This is created by the parser, and then resolved
1015 // to a real type when we lower parse trees.
1016 Closure_fields closure_fields_;
1017 // The closure variable, passed as a parameter using the static
1018 // chain parameter. Normally NULL.
1019 Named_object* closure_var_;
1020 // The outer block of statements in the function.
1022 // The source location of the start of the function.
1024 // Labels defined or referenced in the function.
1026 // The function decl.
1028 // The defer stack variable. A pointer to this variable is used to
1029 // distinguish the defer stack for one function from another. This
1030 // is NULL unless we actually need a defer stack.
1031 Temporary_statement* defer_stack_;
1032 // True if the result variables are named.
1033 bool results_are_named_;
1034 // True if this function calls the predeclared recover function.
1035 bool calls_recover_;
1036 // True if this a thunk built for a function which calls recover.
1037 bool is_recover_thunk_;
1038 // True if this function already has a recover thunk.
1039 bool has_recover_thunk_;
1042 // A snapshot of the current binding state.
1044 class Bindings_snapshot
1047 Bindings_snapshot(const Block*, Location);
1049 // Report any errors appropriate for a goto from the current binding
1050 // state of B to this one.
1052 check_goto_from(const Block* b, Location);
1054 // Report any errors appropriate for a goto from this binding state
1055 // to the current state of B.
1057 check_goto_to(const Block* b);
1061 check_goto_block(Location, const Block*, const Block*, size_t*);
1064 check_goto_defs(Location, const Block*, size_t, size_t);
1066 // The current block.
1067 const Block* block_;
1068 // The number of names currently defined in each open block.
1069 // Element 0 is this->block_, element 1 is
1070 // this->block_->enclosing(), etc.
1071 std::vector<size_t> counts_;
1072 // The location where this snapshot was taken.
1076 // A function declaration.
1078 class Function_declaration
1081 Function_declaration(Function_type* fntype, Location location)
1082 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1087 { return this->fntype_; }
1091 { return this->location_; }
1095 { return this->asm_name_; }
1097 // Set the assembler name.
1099 set_asm_name(const std::string& asm_name)
1100 { this->asm_name_ = asm_name; }
1102 // Return a decl for the function given an identifier.
1104 get_or_make_decl(Gogo*, Named_object*, tree id);
1106 // Export a function declaration.
1108 export_func(Export* exp, const std::string& name) const
1109 { Function::export_func_with_type(exp, name, this->fntype_); }
1112 // The type of the function.
1113 Function_type* fntype_;
1114 // The location of the declaration.
1116 // The assembler name: this is the name to use in references to the
1117 // function. This is normally empty.
1118 std::string asm_name_;
1119 // The function decl if needed.
1128 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1129 bool is_receiver, Location);
1131 // Get the type of the variable.
1138 // Return whether the type is defined yet.
1141 { return this->type_ != NULL; }
1143 // Get the initial value.
1146 { return this->init_; }
1148 // Return whether there are any preinit statements.
1150 has_pre_init() const
1151 { return this->preinit_ != NULL; }
1153 // Return the preinit statements if any.
1156 { return this->preinit_; }
1158 // Return whether this is a global variable.
1161 { return this->is_global_; }
1163 // Return whether this is a function parameter.
1165 is_parameter() const
1166 { return this->is_parameter_; }
1168 // Return whether this is the receiver parameter of a method.
1171 { return this->is_receiver_; }
1173 // Change this parameter to be a receiver. This is used when
1174 // creating the thunks created for functions which call recover.
1178 go_assert(this->is_parameter_);
1179 this->is_receiver_ = true;
1182 // Change this parameter to not be a receiver. This is used when
1183 // creating the thunks created for functions which call recover.
1185 set_is_not_receiver()
1187 go_assert(this->is_parameter_);
1188 this->is_receiver_ = false;
1191 // Return whether this is the varargs parameter of a function.
1193 is_varargs_parameter() const
1194 { return this->is_varargs_parameter_; }
1196 // Whether this variable's address is taken.
1198 is_address_taken() const
1199 { return this->is_address_taken_; }
1201 // Whether this variable should live in the heap.
1204 { return this->is_address_taken_ && !this->is_global_; }
1206 // Note that something takes the address of this variable.
1209 { this->is_address_taken_ = true; }
1211 // Return whether the address is taken but does not escape.
1213 is_non_escaping_address_taken() const
1214 { return this->is_non_escaping_address_taken_; }
1216 // Note that something takes the address of this variable such that
1217 // the address does not escape the function.
1219 set_non_escaping_address_taken()
1220 { this->is_non_escaping_address_taken_ = true; }
1222 // Get the source location of the variable's declaration.
1225 { return this->location_; }
1227 // Record that this is the varargs parameter of a function.
1229 set_is_varargs_parameter()
1231 go_assert(this->is_parameter_);
1232 this->is_varargs_parameter_ = true;
1235 // Clear the initial value; used for error handling.
1238 { this->init_ = NULL; }
1240 // Set the initial value; used for converting shortcuts.
1242 set_init(Expression* init)
1243 { this->init_ = init; }
1245 // Get the preinit block, a block of statements to be run before the
1246 // initialization expression.
1248 preinit_block(Gogo*);
1250 // Add a statement to be run before the initialization expression.
1251 // This is only used for global variables.
1253 add_preinit_statement(Gogo*, Statement*);
1255 // Lower the initialization expression after parsing is complete.
1257 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1259 // A special case: the init value is used only to determine the
1260 // type. This is used if the variable is defined using := with the
1261 // comma-ok form of a map index or a receive expression. The init
1262 // value is actually the map index expression or receive expression.
1263 // We use this because we may not know the right type at parse time.
1265 set_type_from_init_tuple()
1266 { this->type_from_init_tuple_ = true; }
1268 // Another special case: the init value is used only to determine
1269 // the type. This is used if the variable is defined using := with
1270 // a range clause. The init value is the range expression. The
1271 // type of the variable is the index type of the range expression
1272 // (i.e., the first value returned by a range).
1274 set_type_from_range_index()
1275 { this->type_from_range_index_ = true; }
1277 // Another special case: like set_type_from_range_index, but the
1278 // type is the value type of the range expression (i.e., the second
1279 // value returned by a range).
1281 set_type_from_range_value()
1282 { this->type_from_range_value_ = true; }
1284 // Another special case: the init value is used only to determine
1285 // the type. This is used if the variable is defined using := with
1286 // a case in a select statement. The init value is the channel.
1287 // The type of the variable is the channel's element type.
1289 set_type_from_chan_element()
1290 { this->type_from_chan_element_ = true; }
1292 // After we lower the select statement, we once again set the type
1293 // from the initialization expression.
1295 clear_type_from_chan_element()
1297 go_assert(this->type_from_chan_element_);
1298 this->type_from_chan_element_ = false;
1301 // Note that this variable was created for a type switch clause.
1303 set_is_type_switch_var()
1304 { this->is_type_switch_var_ = true; }
1306 // Traverse the initializer expression.
1308 traverse_expression(Traverse*, unsigned int traverse_mask);
1310 // Determine the type of the variable if necessary.
1314 // Get the backend representation of the variable.
1316 get_backend_variable(Gogo*, Named_object*, const Package*,
1317 const std::string&);
1319 // Get the initial value of the variable as a tree. This may only
1320 // be called if has_pre_init() returns false.
1322 get_init_tree(Gogo*, Named_object* function);
1324 // Return a series of statements which sets the value of the
1325 // variable in DECL. This should only be called is has_pre_init()
1326 // returns true. DECL may be NULL for a sink variable.
1328 get_init_block(Gogo*, Named_object* function, tree decl);
1330 // Export the variable.
1332 export_var(Export*, const std::string& name) const;
1334 // Import a variable.
1336 import_var(Import*, std::string* pname, Type** ptype);
1339 // The type of a tuple.
1341 type_from_tuple(Expression*, bool) const;
1343 // The type of a range.
1345 type_from_range(Expression*, bool, bool) const;
1347 // The element type of a channel.
1349 type_from_chan_element(Expression*, bool) const;
1351 // The variable's type. This may be NULL if the type is set from
1354 // The initial value. This may be NULL if the variable should be
1355 // initialized to the default value for the type.
1357 // Statements to run before the init statement.
1359 // Location of variable definition.
1361 // Backend representation.
1362 Bvariable* backend_;
1363 // Whether this is a global variable.
1364 bool is_global_ : 1;
1365 // Whether this is a function parameter.
1366 bool is_parameter_ : 1;
1367 // Whether this is the receiver parameter of a method.
1368 bool is_receiver_ : 1;
1369 // Whether this is the varargs parameter of a function.
1370 bool is_varargs_parameter_ : 1;
1371 // Whether something takes the address of this variable. For a
1372 // local variable this implies that the variable has to be on the
1374 bool is_address_taken_ : 1;
1375 // Whether something takes the address of this variable such that
1376 // the address does not escape the function.
1377 bool is_non_escaping_address_taken_ : 1;
1378 // True if we have seen this variable in a traversal.
1380 // True if we have lowered the initialization expression.
1381 bool init_is_lowered_ : 1;
1382 // True if init is a tuple used to set the type.
1383 bool type_from_init_tuple_ : 1;
1384 // True if init is a range clause and the type is the index type.
1385 bool type_from_range_index_ : 1;
1386 // True if init is a range clause and the type is the value type.
1387 bool type_from_range_value_ : 1;
1388 // True if init is a channel and the type is the channel's element type.
1389 bool type_from_chan_element_ : 1;
1390 // True if this is a variable created for a type switch case.
1391 bool is_type_switch_var_ : 1;
1392 // True if we have determined types.
1393 bool determined_type_ : 1;
1396 // A variable which is really the name for a function return value, or
1399 class Result_variable
1402 Result_variable(Type* type, Function* function, int index,
1404 : type_(type), function_(function), index_(index), location_(location),
1405 backend_(NULL), is_address_taken_(false),
1406 is_non_escaping_address_taken_(false)
1409 // Get the type of the result variable.
1412 { return this->type_; }
1414 // Get the function that this is associated with.
1417 { return this->function_; }
1419 // Index in the list of function results.
1422 { return this->index_; }
1424 // The location of the variable definition.
1427 { return this->location_; }
1429 // Whether this variable's address is taken.
1431 is_address_taken() const
1432 { return this->is_address_taken_; }
1434 // Note that something takes the address of this variable.
1437 { this->is_address_taken_ = true; }
1439 // Return whether the address is taken but does not escape.
1441 is_non_escaping_address_taken() const
1442 { return this->is_non_escaping_address_taken_; }
1444 // Note that something takes the address of this variable such that
1445 // the address does not escape the function.
1447 set_non_escaping_address_taken()
1448 { this->is_non_escaping_address_taken_ = true; }
1450 // Whether this variable should live in the heap.
1453 { return this->is_address_taken_; }
1455 // Set the function. This is used when cloning functions which call
1458 set_function(Function* function)
1459 { this->function_ = function; }
1461 // Get the backend representation of the variable.
1463 get_backend_variable(Gogo*, Named_object*, const std::string&);
1466 // Type of result variable.
1468 // Function with which this is associated.
1469 Function* function_;
1470 // Index in list of results.
1472 // Where the result variable is defined.
1474 // Backend representation.
1475 Bvariable* backend_;
1476 // Whether something takes the address of this variable.
1477 bool is_address_taken_;
1478 // Whether something takes the address of this variable such that
1479 // the address does not escape the function.
1480 bool is_non_escaping_address_taken_;
1483 // The value we keep for a named constant. This lets us hold a type
1484 // and an expression.
1486 class Named_constant
1489 Named_constant(Type* type, Expression* expr, int iota_value,
1491 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1497 { return this->type_; }
1501 { return this->expr_; }
1505 { return this->iota_value_; }
1509 { return this->location_; }
1511 // Whether we are lowering.
1514 { return this->lowering_; }
1516 // Set that we are lowering.
1519 { this->lowering_ = true; }
1521 // We are no longer lowering.
1524 { this->lowering_ = false; }
1526 // Traverse the expression.
1528 traverse_expression(Traverse*);
1530 // Determine the type of the constant if necessary.
1534 // Indicate that we found and reported an error for this constant.
1538 // Export the constant.
1540 export_const(Export*, const std::string& name) const;
1542 // Import a constant.
1544 import_const(Import*, std::string*, Type**, Expression**);
1547 // The type of the constant.
1549 // The expression for the constant.
1551 // If the predeclared constant iota is used in EXPR_, this is the
1552 // value it will have. We do this because at parse time we don't
1553 // know whether the name "iota" will refer to the predeclared
1554 // constant or to something else. We put in the right value in when
1557 // The location of the definition.
1559 // Whether we are currently lowering this constant.
1563 // A type declaration.
1565 class Type_declaration
1568 Type_declaration(Location location)
1569 : location_(location), in_function_(NULL), methods_(),
1570 issued_warning_(false)
1573 // Return the location.
1576 { return this->location_; }
1578 // Return the function in which this type is declared. This will
1579 // return NULL for a type declared in global scope.
1582 { return this->in_function_; }
1584 // Set the function in which this type is declared.
1586 set_in_function(Named_object* f)
1587 { this->in_function_ = f; }
1589 // Add a method to this type. This is used when methods are defined
1592 add_method(const std::string& name, Function* function);
1594 // Add a method declaration to this type.
1596 add_method_declaration(const std::string& name, Function_type* type,
1599 // Return whether any methods were defined.
1601 has_methods() const;
1603 // Define methods when the real type is known.
1605 define_methods(Named_type*);
1607 // This is called if we are trying to use this type. It returns
1608 // true if we should issue a warning.
1613 typedef std::vector<Named_object*> Methods;
1615 // The location of the type declaration.
1617 // If this type is declared in a function, a pointer back to the
1618 // function in which it is defined.
1619 Named_object* in_function_;
1620 // Methods defined before the type is defined.
1622 // True if we have issued a warning about a use of this type
1623 // declaration when it is undefined.
1624 bool issued_warning_;
1627 // An unknown object. These are created by the parser for forward
1628 // references to names which have not been seen before. In a correct
1629 // program, these will always point to a real definition by the end of
1630 // the parse. Because they point to another Named_object, these may
1631 // only be referenced by Unknown_expression objects.
1636 Unknown_name(Location location)
1637 : location_(location), real_named_object_(NULL)
1640 // Return the location where this name was first seen.
1643 { return this->location_; }
1645 // Return the real named object that this points to, or NULL if it
1646 // was never resolved.
1648 real_named_object() const
1649 { return this->real_named_object_; }
1651 // Set the real named object that this points to.
1653 set_real_named_object(Named_object* no);
1656 // The location where this name was first seen.
1658 // The real named object when it is known.
1663 // A named object named. This is the result of a declaration. We
1664 // don't use a superclass because they all have to be handled
1672 // An uninitialized Named_object. We should never see this.
1673 NAMED_OBJECT_UNINITIALIZED,
1674 // An unknown name. This is used for forward references. In a
1675 // correct program, these will all be resolved by the end of the
1677 NAMED_OBJECT_UNKNOWN,
1682 // A forward type declaration.
1683 NAMED_OBJECT_TYPE_DECLARATION,
1686 // A result variable in a function.
1687 NAMED_OBJECT_RESULT_VAR,
1688 // The blank identifier--the special variable named _.
1692 // A forward func declaration.
1693 NAMED_OBJECT_FUNC_DECLARATION,
1695 NAMED_OBJECT_PACKAGE
1698 // Return the classification.
1700 classification() const
1701 { return this->classification_; }
1707 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1711 { return this->classification_ == NAMED_OBJECT_CONST; }
1715 { return this->classification_ == NAMED_OBJECT_TYPE; }
1718 is_type_declaration() const
1719 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1723 { return this->classification_ == NAMED_OBJECT_VAR; }
1726 is_result_variable() const
1727 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1731 { return this->classification_ == NAMED_OBJECT_SINK; }
1735 { return this->classification_ == NAMED_OBJECT_FUNC; }
1738 is_function_declaration() const
1739 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1743 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1747 static Named_object*
1748 make_unknown_name(const std::string& name, Location);
1750 static Named_object*
1751 make_constant(const Typed_identifier&, const Package*, Expression*,
1754 static Named_object*
1755 make_type(const std::string&, const Package*, Type*, Location);
1757 static Named_object*
1758 make_type_declaration(const std::string&, const Package*, Location);
1760 static Named_object*
1761 make_variable(const std::string&, const Package*, Variable*);
1763 static Named_object*
1764 make_result_variable(const std::string&, Result_variable*);
1766 static Named_object*
1769 static Named_object*
1770 make_function(const std::string&, const Package*, Function*);
1772 static Named_object*
1773 make_function_declaration(const std::string&, const Package*, Function_type*,
1776 static Named_object*
1777 make_package(const std::string& alias, Package* package);
1784 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1785 return this->u_.unknown_value;
1789 unknown_value() const
1791 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1792 return this->u_.unknown_value;
1798 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1799 return this->u_.const_value;
1802 const Named_constant*
1805 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1806 return this->u_.const_value;
1812 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1813 return this->u_.type_value;
1819 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1820 return this->u_.type_value;
1824 type_declaration_value()
1826 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1827 return this->u_.type_declaration;
1830 const Type_declaration*
1831 type_declaration_value() const
1833 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1834 return this->u_.type_declaration;
1840 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1841 return this->u_.var_value;
1847 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1848 return this->u_.var_value;
1854 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1855 return this->u_.result_var_value;
1858 const Result_variable*
1859 result_var_value() const
1861 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1862 return this->u_.result_var_value;
1868 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1869 return this->u_.func_value;
1875 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1876 return this->u_.func_value;
1879 Function_declaration*
1880 func_declaration_value()
1882 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1883 return this->u_.func_declaration_value;
1886 const Function_declaration*
1887 func_declaration_value() const
1889 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1890 return this->u_.func_declaration_value;
1896 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1897 return this->u_.package_value;
1901 package_value() const
1903 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1904 return this->u_.package_value;
1909 { return this->name_; }
1911 // Return the name to use in an error message. The difference is
1912 // that if this Named_object is defined in a different package, this
1913 // will return PACKAGE.NAME.
1915 message_name() const;
1919 { return this->package_; }
1921 // Resolve an unknown value if possible. This returns the same
1922 // Named_object or a new one.
1926 Named_object* ret = this;
1927 if (this->is_unknown())
1929 Named_object* r = this->unknown_value()->real_named_object();
1939 const Named_object* ret = this;
1940 if (this->is_unknown())
1942 const Named_object* r = this->unknown_value()->real_named_object();
1949 // The location where this object was defined or referenced.
1953 // Convert a variable to the backend representation.
1955 get_backend_variable(Gogo*, Named_object* function);
1957 // Return a tree for the external identifier for this object.
1961 // Return a tree representing this object.
1963 get_tree(Gogo*, Named_object* function);
1965 // Define a type declaration.
1967 set_type_value(Named_type*);
1969 // Define a function declaration.
1971 set_function_value(Function*);
1973 // Declare an unknown name as a type declaration.
1977 // Export this object.
1979 export_named_object(Export*) const;
1982 Named_object(const std::string&, const Package*, Classification);
1984 // The name of the object.
1986 // The package that this object is in. This is NULL if it is in the
1987 // file we are compiling.
1988 const Package* package_;
1989 // The type of object this is.
1990 Classification classification_;
1994 Unknown_name* unknown_value;
1995 Named_constant* const_value;
1996 Named_type* type_value;
1997 Type_declaration* type_declaration;
1998 Variable* var_value;
1999 Result_variable* result_var_value;
2000 Function* func_value;
2001 Function_declaration* func_declaration_value;
2002 Package* package_value;
2004 // The DECL tree for this object if we have already converted it.
2008 // A binding contour. This binds names to objects.
2013 // Type for mapping from names to objects.
2014 typedef Unordered_map(std::string, Named_object*) Contour;
2016 Bindings(Bindings* enclosing);
2018 // Add an unknown name.
2020 add_unknown_name(const std::string& name, Location location)
2022 return this->add_named_object(Named_object::make_unknown_name(name,
2028 add_constant(const Typed_identifier& tid, const Package* package,
2029 Expression* expr, int iota_value)
2031 return this->add_named_object(Named_object::make_constant(tid, package,
2038 add_type(const std::string& name, const Package* package, Type* type,
2041 return this->add_named_object(Named_object::make_type(name, package, type,
2045 // Add a named type. This is used for builtin types, and to add an
2046 // imported type to the global scope.
2048 add_named_type(Named_type* named_type);
2050 // Add a type declaration.
2052 add_type_declaration(const std::string& name, const Package* package,
2055 Named_object* no = Named_object::make_type_declaration(name, package,
2057 return this->add_named_object(no);
2062 add_variable(const std::string& name, const Package* package,
2065 return this->add_named_object(Named_object::make_variable(name, package,
2069 // Add a result variable.
2071 add_result_variable(const std::string& name, Result_variable* result)
2073 return this->add_named_object(Named_object::make_result_variable(name,
2079 add_function(const std::string& name, const Package*, Function* function);
2081 // Add a function declaration.
2083 add_function_declaration(const std::string& name, const Package* package,
2084 Function_type* type, Location location);
2086 // Add a package. The location is the location of the import
2089 add_package(const std::string& alias, Package* package)
2091 Named_object* no = Named_object::make_package(alias, package);
2092 return this->add_named_object(no);
2095 // Define a type which was already declared.
2097 define_type(Named_object*, Named_type*);
2099 // Add a method to the list of objects. This is not added to the
2102 add_method(Named_object*);
2104 // Add a named object to this binding.
2106 add_named_object(Named_object* no)
2107 { return this->add_named_object_to_contour(&this->bindings_, no); }
2109 // Clear all names in file scope from the bindings.
2113 // Look up a name in this binding contour and in any enclosing
2114 // binding contours. This returns NULL if the name is not found.
2116 lookup(const std::string&) const;
2118 // Look up a name in this binding contour without looking in any
2119 // enclosing binding contours. Returns NULL if the name is not found.
2121 lookup_local(const std::string&) const;
2125 remove_binding(Named_object*);
2127 // Traverse the tree. See the Traverse class.
2129 traverse(Traverse*, bool is_global);
2131 // Iterate over definitions. This does not include things which
2132 // were only declared.
2134 typedef std::vector<Named_object*>::const_iterator
2135 const_definitions_iterator;
2137 const_definitions_iterator
2138 begin_definitions() const
2139 { return this->named_objects_.begin(); }
2141 const_definitions_iterator
2142 end_definitions() const
2143 { return this->named_objects_.end(); }
2145 // Return the number of definitions.
2147 size_definitions() const
2148 { return this->named_objects_.size(); }
2150 // Return whether there are no definitions.
2152 empty_definitions() const
2153 { return this->named_objects_.empty(); }
2155 // Iterate over declarations. This is everything that has been
2156 // declared, which includes everything which has been defined.
2158 typedef Contour::const_iterator const_declarations_iterator;
2160 const_declarations_iterator
2161 begin_declarations() const
2162 { return this->bindings_.begin(); }
2164 const_declarations_iterator
2165 end_declarations() const
2166 { return this->bindings_.end(); }
2168 // Return the number of declarations.
2170 size_declarations() const
2171 { return this->bindings_.size(); }
2173 // Return whether there are no declarations.
2175 empty_declarations() const
2176 { return this->bindings_.empty(); }
2178 // Return the first declaration.
2181 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2185 add_named_object_to_contour(Contour*, Named_object*);
2188 new_definition(Named_object*, Named_object*);
2190 // Enclosing bindings.
2191 Bindings* enclosing_;
2192 // The list of objects.
2193 std::vector<Named_object*> named_objects_;
2194 // The mapping from names to objects.
2203 Label(const std::string& name)
2204 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2205 refs_(), is_used_(false), blabel_(NULL)
2208 // Return the label's name.
2211 { return this->name_; }
2213 // Return whether the label has been defined.
2216 { return !Linemap::is_unknown_location(this->location_); }
2218 // Return whether the label has been used.
2221 { return this->is_used_; }
2223 // Record that the label is used.
2226 { this->is_used_ = true; }
2228 // Return the location of the definition.
2231 { return this->location_; }
2233 // Return the bindings snapshot.
2236 { return this->snapshot_; }
2238 // Add a snapshot of a goto which refers to this label.
2240 add_snapshot_ref(Bindings_snapshot* snapshot)
2242 go_assert(Linemap::is_unknown_location(this->location_));
2243 this->refs_.push_back(snapshot);
2246 // Return the list of snapshots of goto statements which refer to
2248 const std::vector<Bindings_snapshot*>&
2250 { return this->refs_; }
2252 // Clear the references.
2256 // Define the label at LOCATION with the given bindings snapshot.
2258 define(Location location, Bindings_snapshot* snapshot)
2260 go_assert(Linemap::is_unknown_location(this->location_)
2261 && this->snapshot_ == NULL);
2262 this->location_ = location;
2263 this->snapshot_ = snapshot;
2266 // Return the backend representation for this label.
2268 get_backend_label(Translate_context*);
2270 // Return an expression for the address of this label. This is used
2271 // to get the return address of a deferred function to see whether
2272 // the function may call recover.
2274 get_addr(Translate_context*, Location location);
2277 // The name of the label.
2279 // The location of the definition. This is 0 if the label has not
2280 // yet been defined.
2282 // A snapshot of the set of bindings defined at this label, used to
2283 // issue errors about invalid goto statements.
2284 Bindings_snapshot* snapshot_;
2285 // A list of snapshots of goto statements which refer to this label.
2286 std::vector<Bindings_snapshot*> refs_;
2287 // Whether the label has been used.
2289 // The backend representation.
2293 // An unnamed label. These are used when lowering loops.
2298 Unnamed_label(Location location)
2299 : location_(location), blabel_(NULL)
2302 // Get the location where the label is defined.
2305 { return this->location_; }
2307 // Set the location where the label is defined.
2309 set_location(Location location)
2310 { this->location_ = location; }
2312 // Return a statement which defines this label.
2314 get_definition(Translate_context*);
2316 // Return a goto to this label from LOCATION.
2318 get_goto(Translate_context*, Location location);
2321 // Return the backend representation.
2323 get_blabel(Translate_context*);
2325 // The location where the label is defined.
2327 // The backend representation of this label.
2331 // An imported package.
2336 Package(const std::string& name, const std::string& unique_prefix,
2339 // The real name of this package. This may be different from the
2340 // name in the associated Named_object if the import statement used
2344 { return this->name_; }
2346 // Return the location of the import statement.
2349 { return this->location_; }
2351 // Get the unique prefix used for all symbols exported from this
2354 unique_prefix() const
2356 go_assert(!this->unique_prefix_.empty());
2357 return this->unique_prefix_;
2360 // The priority of this package. The init function of packages with
2361 // lower priority must be run before the init function of packages
2362 // with higher priority.
2365 { return this->priority_; }
2367 // Set the priority.
2369 set_priority(int priority);
2371 // Return the bindings.
2374 { return this->bindings_; }
2376 // Whether some symbol from the package was used.
2379 { return this->used_; }
2381 // Note that some symbol from this package was used.
2384 { this->used_ = true; }
2386 // Clear the used field for the next file.
2389 { this->used_ = false; }
2391 // Whether this package was imported in the current file.
2394 { return this->is_imported_; }
2396 // Note that this package was imported in the current file.
2399 { this->is_imported_ = true; }
2401 // Clear the imported field for the next file.
2404 { this->is_imported_ = false; }
2406 // Whether this package was imported with a name of "_".
2408 uses_sink_alias() const
2409 { return this->uses_sink_alias_; }
2411 // Note that this package was imported with a name of "_".
2413 set_uses_sink_alias()
2414 { this->uses_sink_alias_ = true; }
2416 // Clear the sink alias field for the next file.
2418 clear_uses_sink_alias()
2419 { this->uses_sink_alias_ = false; }
2421 // Look up a name in the package. Returns NULL if the name is not
2424 lookup(const std::string& name) const
2425 { return this->bindings_->lookup(name); }
2427 // Set the location of the package. This is used if it is seen in a
2428 // different import before it is really imported.
2430 set_location(Location location)
2431 { this->location_ = location; }
2433 // Add a constant to the package.
2435 add_constant(const Typed_identifier& tid, Expression* expr)
2436 { return this->bindings_->add_constant(tid, this, expr, 0); }
2438 // Add a type to the package.
2440 add_type(const std::string& name, Type* type, Location location)
2441 { return this->bindings_->add_type(name, this, type, location); }
2443 // Add a type declaration to the package.
2445 add_type_declaration(const std::string& name, Location location)
2446 { return this->bindings_->add_type_declaration(name, this, location); }
2448 // Add a variable to the package.
2450 add_variable(const std::string& name, Variable* variable)
2451 { return this->bindings_->add_variable(name, this, variable); }
2453 // Add a function declaration to the package.
2455 add_function_declaration(const std::string& name, Function_type* type,
2457 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2459 // Determine types of constants.
2464 // The real name of this package.
2466 // The unique prefix for all exported global symbols.
2467 std::string unique_prefix_;
2468 // The names in this package.
2469 Bindings* bindings_;
2470 // The priority of this package. A package has a priority higher
2471 // than the priority of all of the packages that it imports. This
2472 // is used to run init functions in the right order.
2474 // The location of the import statement.
2476 // True if some name from this package was used. This is mutable
2477 // because we can use a package even if we have a const pointer to
2480 // True if this package was imported in the current file.
2482 // True if this package was imported with a name of "_".
2483 bool uses_sink_alias_;
2486 // Return codes for the traversal functions. This is not an enum
2487 // because we want to be able to declare traversal functions in other
2488 // header files without including this one.
2490 // Continue traversal as usual.
2491 const int TRAVERSE_CONTINUE = -1;
2494 const int TRAVERSE_EXIT = 0;
2496 // Continue traversal, but skip components of the current object.
2497 // E.g., if this is returned by Traverse::statement, we do not
2498 // traverse the expressions in the statement even if
2499 // traverse_expressions is set in the traverse_mask.
2500 const int TRAVERSE_SKIP_COMPONENTS = 1;
2502 // This class is used when traversing the parse tree. The caller uses
2503 // a subclass which overrides functions as desired.
2508 // These bitmasks say what to traverse.
2509 static const unsigned int traverse_variables = 0x1;
2510 static const unsigned int traverse_constants = 0x2;
2511 static const unsigned int traverse_functions = 0x4;
2512 static const unsigned int traverse_blocks = 0x8;
2513 static const unsigned int traverse_statements = 0x10;
2514 static const unsigned int traverse_expressions = 0x20;
2515 static const unsigned int traverse_types = 0x40;
2517 Traverse(unsigned int traverse_mask)
2518 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2521 virtual ~Traverse();
2523 // The bitmask of what to traverse.
2525 traverse_mask() const
2526 { return this->traverse_mask_; }
2528 // Record that we are going to traverse a type. This returns true
2529 // if the type has already been seen in this traversal. This is
2530 // required because types, unlike expressions, can form a circular
2533 remember_type(const Type*);
2535 // Record that we are going to see an expression. This returns true
2536 // if the expression has already been seen in this traversal. This
2537 // is only needed for cases where multiple expressions can point to
2540 remember_expression(const Expression*);
2542 // These functions return one of the TRAVERSE codes defined above.
2544 // If traverse_variables is set in the mask, this is called for
2545 // every variable in the tree.
2547 variable(Named_object*);
2549 // If traverse_constants is set in the mask, this is called for
2550 // every named constant in the tree. The bool parameter is true for
2551 // a global constant.
2553 constant(Named_object*, bool);
2555 // If traverse_functions is set in the mask, this is called for
2556 // every function in the tree.
2558 function(Named_object*);
2560 // If traverse_blocks is set in the mask, this is called for every
2561 // block in the tree.
2565 // If traverse_statements is set in the mask, this is called for
2566 // every statement in the tree.
2568 statement(Block*, size_t* index, Statement*);
2570 // If traverse_expressions is set in the mask, this is called for
2571 // every expression in the tree.
2573 expression(Expression**);
2575 // If traverse_types is set in the mask, this is called for every
2576 // type in the tree.
2581 // A hash table for types we have seen during this traversal. Note
2582 // that this uses the default hash functions for pointers rather
2583 // than Type_hash_identical and Type_identical. This is because for
2584 // traversal we care about seeing a specific type structure. If
2585 // there are two separate instances of identical types, we want to
2587 typedef Unordered_set(const Type*) Types_seen;
2589 typedef Unordered_set(const Expression*) Expressions_seen;
2591 // Bitmask of what sort of objects to traverse.
2592 unsigned int traverse_mask_;
2593 // Types which have been seen in this traversal.
2594 Types_seen* types_seen_;
2595 // Expressions which have been seen in this traversal.
2596 Expressions_seen* expressions_seen_;
2599 // A class which makes it easier to insert new statements before the
2600 // current statement during a traversal.
2602 class Statement_inserter
2605 // Empty constructor.
2606 Statement_inserter()
2607 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2610 // Constructor for a statement in a block.
2611 Statement_inserter(Block* block, size_t *pindex)
2612 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2615 // Constructor for a global variable.
2616 Statement_inserter(Gogo* gogo, Variable* var)
2617 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2618 { go_assert(var->is_global()); }
2620 // We use the default copy constructor and assignment operator.
2622 // Insert S before the statement we are traversing, or before the
2623 // initialization expression of a global variable.
2625 insert(Statement* s);
2628 // The block that the statement is in.
2630 // The index of the statement that we are traversing.
2632 // The IR, needed when looking at an initializer expression for a
2635 // The global variable, when looking at an initializer expression.
2639 // When translating the gogo IR into the backend data structure, this
2640 // is the context we pass down the blocks and statements.
2642 class Translate_context
2645 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2647 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2648 block_(block), bblock_(bblock), is_const_(false)
2655 { return this->gogo_; }
2659 { return this->backend_; }
2663 { return this->function_; }
2667 { return this->block_; }
2671 { return this->bblock_; }
2675 { return this->is_const_; }
2677 // Make a constant context.
2680 { this->is_const_ = true; }
2683 // The IR for the entire compilation unit.
2685 // The generator for the backend data structures.
2687 // The function we are currently translating. NULL if not in a
2688 // function, e.g., the initializer of a global variable.
2689 Named_object* function_;
2690 // The block we are currently translating. NULL if not in a
2693 // The backend representation of the current block. NULL if block_
2696 // Whether this is being evaluated in a constant context. This is
2697 // used for type descriptor initializers.
2701 // Runtime error codes. These must match the values in
2702 // libgo/runtime/go-runtime-error.c.
2704 // Slice index out of bounds: negative or larger than the length of
2706 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2708 // Array index out of bounds.
2709 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2711 // String index out of bounds.
2712 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2714 // Slice slice out of bounds: negative or larger than the length of
2715 // the slice or high bound less than low bound.
2716 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2718 // Array slice out of bounds.
2719 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2721 // String slice out of bounds.
2722 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2724 // Dereference of nil pointer. This is used when there is a
2725 // dereference of a pointer to a very large struct or array, to ensure
2726 // that a gigantic array is not used a proxy to access random memory
2728 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2730 // Slice length or capacity out of bounds in make: negative or
2731 // overflow or length greater than capacity.
2732 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2734 // Map capacity out of bounds in make: negative or overflow.
2735 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2737 // Channel capacity out of bounds in make: negative or overflow.
2738 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2740 // This is used by some of the langhooks.
2741 extern Gogo* go_get_gogo();
2743 // Whether we have seen any errors. FIXME: Replace with a backend
2745 extern bool saw_errors();
2747 #endif // !defined(GO_GOGO_H)