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 // the package name, so that hidden symbols in different packages do
144 pack_hidden_name(const std::string& name, bool is_exported) const
148 : '.' + this->pkgpath() + '.' + name);
151 // Unpack a name which may have been hidden. Returns the
152 // user-visible name of the object.
154 unpack_hidden_name(const std::string& name)
155 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
157 // Return whether a possibly packed name is hidden.
159 is_hidden_name(const std::string& name)
160 { return name[0] == '.'; }
162 // Return the package path of a hidden name.
164 hidden_name_pkgpath(const std::string& name)
166 go_assert(Gogo::is_hidden_name(name));
167 return name.substr(1, name.rfind('.') - 1);
170 // Given a name which may or may not have been hidden, return the
171 // name to use in an error message.
173 message_name(const std::string& name);
175 // Return whether a name is the blank identifier _.
177 is_sink_name(const std::string& name)
179 return (name[0] == '.'
180 && name[name.length() - 1] == '_'
181 && name[name.length() - 2] == '.');
184 // Convert a pkgpath into a string suitable for a symbol
186 pkgpath_for_symbol(const std::string& pkgpath);
188 // Return the package path to use for reflect.Type.PkgPath.
192 // Return the package path to use for a symbol name.
194 pkgpath_symbol() const;
196 // Set the package path from a command line option.
198 set_pkgpath(const std::string&);
200 // Set the prefix from a command line option.
202 set_prefix(const std::string&);
204 // Return whether pkgpath was set from a command line option.
206 pkgpath_from_option() const
207 { return this->pkgpath_from_option_; }
209 // Return the priority to use for the package we are compiling.
210 // This is two more than the largest priority of any package we
213 package_priority() const;
215 // Import a package. FILENAME is the file name argument, LOCAL_NAME
216 // is the local name to give to the package. If LOCAL_NAME is empty
217 // the declarations are added to the global scope.
219 import_package(const std::string& filename, const std::string& local_name,
220 bool is_local_name_exported, Location);
222 // Whether we are the global binding level.
224 in_global_scope() const;
226 // Look up a name in the current binding contours.
228 lookup(const std::string&, Named_object** pfunction) const;
230 // Look up a name in the current block.
232 lookup_in_block(const std::string&) const;
234 // Look up a name in the global namespace--the universal scope.
236 lookup_global(const char*) const;
238 // Add a new imported package. REAL_NAME is the real name of the
239 // package. ALIAS is the alias of the package; this may be the same
240 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
241 // this package should be added to the global namespace; this is
242 // true if the alias is ".". LOCATION is the location of the import
243 // statement. This returns the new package, or NULL on error.
245 add_imported_package(const std::string& real_name, const std::string& alias,
246 bool is_alias_exported,
247 const std::string& pkgpath,
249 bool* padd_to_globals);
251 // Register a package. This package may or may not be imported.
252 // This returns the Package structure for the package, creating if
255 register_package(const std::string& pkgpath, Location);
257 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
258 // method function should be added to the type of its receiver.
260 start_function(const std::string& name, Function_type* type,
261 bool add_method_to_type, Location);
263 // Finish compiling a function.
265 finish_function(Location);
267 // Return the current function.
269 current_function() const;
271 // Return the current block.
275 // Start a new block. This is not initially associated with a
278 start_block(Location);
280 // Finish the current block and return it.
282 finish_block(Location);
284 // Declare an erroneous name. This is used to avoid knock-on errors
285 // after a parsing error.
287 add_erroneous_name(const std::string& name);
289 // Declare an unknown name. This is used while parsing. The name
290 // must be resolved by the end of the parse. Unknown names are
291 // always added at the package level.
293 add_unknown_name(const std::string& name, Location);
295 // Declare a function.
297 declare_function(const std::string&, Function_type*, Location);
299 // Declare a function at the package level. This is used for
300 // functions generated for a type.
302 declare_package_function(const std::string&, Function_type*, Location);
306 add_label_definition(const std::string&, Location);
308 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
309 // report errors for a goto from the current location to the label
312 add_label_reference(const std::string&, Location,
313 bool issue_goto_errors);
315 // Return a snapshot of the current binding state.
317 bindings_snapshot(Location);
319 // Add a statement to the current block.
321 add_statement(Statement*);
323 // Add a block to the current block.
325 add_block(Block*, Location);
329 add_constant(const Typed_identifier&, Expression*, int iota_value);
333 add_type(const std::string&, Type*, Location);
335 // Add a named type. This is used for builtin types, and to add an
336 // imported type to the global scope.
338 add_named_type(Named_type*);
342 declare_type(const std::string&, Location);
344 // Declare a type at the package level. This is used when the
345 // parser sees an unknown name where a type name is required.
347 declare_package_type(const std::string&, Location);
349 // Define a type which was already declared.
351 define_type(Named_object*, Named_type*);
355 add_variable(const std::string&, Variable*);
357 // Add a sink--a reference to the blank identifier _.
361 // Add a type which needs to be verified. This is used for sink
362 // types, just to give appropriate error messages.
364 add_type_to_verify(Type* type);
366 // Add a named object to the current namespace. This is used for
367 // import . "package".
369 add_named_object(Named_object*);
371 // Mark all local variables in current bindings as used. This is
372 // used when there is a parse error to avoid useless errors.
376 // Return a name to use for a thunk function. A thunk function is
377 // one we create during the compilation, for a go statement or a
378 // defer statement or a method expression.
382 // Return whether an object is a thunk.
384 is_thunk(const Named_object*);
386 // Note that we've seen an interface type. This is used to build
387 // all required interface method tables.
389 record_interface_type(Interface_type*);
391 // Note that we need an initialization function.
394 { this->need_init_fn_ = true; }
396 // Clear out all names in file scope. This is called when we start
397 // parsing a new file.
401 // Record that VAR1 must be initialized after VAR2. This is used
402 // when VAR2 does not appear in VAR1's INIT or PREINIT.
404 record_var_depends_on(Variable* var1, Named_object* var2)
406 go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
407 this->var_deps_[var1] = var2;
410 // Return the variable that VAR depends on, or NULL if none.
412 var_depends_on(Variable* var) const
414 Var_deps::const_iterator p = this->var_deps_.find(var);
415 return p != this->var_deps_.end() ? p->second : NULL;
418 // Queue up a type-specific function to be written out. This is
419 // used when a type-specific function is needed when not at the top
422 queue_specific_type_function(Type* type, Named_type* name,
423 const std::string& hash_name,
424 Function_type* hash_fntype,
425 const std::string& equal_name,
426 Function_type* equal_fntype);
428 // Write out queued specific type functions.
430 write_specific_type_functions();
432 // Whether we are done writing out specific type functions.
434 specific_type_functions_are_written() const
435 { return this->specific_type_functions_are_written_; }
437 // Traverse the tree. See the Traverse class.
441 // Define the predeclared global names.
443 define_global_names();
445 // Verify and complete all types.
449 // Lower the parse tree.
453 // Lower all the statements in a block.
455 lower_block(Named_object* function, Block*);
457 // Lower an expression.
459 lower_expression(Named_object* function, Statement_inserter*, Expression**);
463 lower_constant(Named_object*);
465 // Finalize the method lists and build stub methods for named types.
469 // Work out the types to use for unspecified variables and
474 // Type check the program.
478 // Check the types in a single block. This is used for complicated
481 check_types_in_block(Block*);
483 // Check for return statements.
485 check_return_statements();
491 // Add an import control function for an imported package to the
494 add_import_init_fn(const std::string& package_name,
495 const std::string& init_name, int prio);
497 // Turn short-cut operators (&&, ||) into explicit if statements.
501 // Use temporary variables to force order of evaluation.
505 // Build thunks for functions which call recover.
507 build_recover_thunks();
509 // Simplify statements which might use thunks: go and defer
512 simplify_thunk_statements();
514 // Dump AST if -fgo-dump-ast is set
516 dump_ast(const char* basename);
518 // Convert named types to the backend representation.
520 convert_named_types();
522 // Convert named types in a list of bindings.
524 convert_named_types_in_bindings(Bindings*);
526 // True if named types have been converted to the backend
529 named_types_are_converted() const
530 { return this->named_types_are_converted_; }
532 // Write out the global values.
536 // Create trees for implicit builtin functions.
538 define_builtin_function_trees();
540 // Build a call to a builtin function. PDECL should point to a NULL
541 // initialized static pointer which will hold the fndecl. NAME is
542 // the name of the function. NARGS is the number of arguments.
543 // RETTYPE is the return type. It is followed by NARGS pairs of
544 // type and argument (both trees).
546 call_builtin(tree* pdecl, Location, const char* name, int nargs,
549 // Build a call to the runtime error function.
551 runtime_error(int code, Location);
553 // Build a builtin struct with a list of fields.
555 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
558 // Mark a function declaration as a builtin library function.
560 mark_fndecl_as_builtin_library(tree fndecl);
562 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
563 // the slice. VALUES points to the values. COUNT is the size,
564 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
567 slice_constructor(tree slice_type_tree, tree values, tree count,
570 // Build required interface method tables.
572 build_interface_method_tables();
574 // Build an interface method table for a type: a list of function
575 // pointers, one for each interface method. This returns a decl.
577 interface_method_table_for_type(const Interface_type*, Type*,
580 // Return a tree which allocate SIZE bytes to hold values of type
583 allocate_memory(Type *type, tree size, Location);
585 // Return a type to use for pointer to const char.
587 const_char_pointer_type_tree();
589 // Build a string constant with the right type.
591 string_constant_tree(const std::string&);
593 // Build a Go string constant. This returns a pointer to the
596 go_string_constant_tree(const std::string&);
598 // Receive a value from a channel.
600 receive_from_channel(tree type_tree, tree type_descriptor_tree, tree channel,
603 // Make a trampoline which calls FNADDR passing CLOSURE.
605 make_trampoline(tree fnaddr, tree closure, Location);
608 // During parsing, we keep a stack of functions. Each function on
609 // the stack is one that we are currently parsing. For each
610 // function, we keep track of the current stack of blocks.
614 Named_object* function;
615 // The stack of active blocks in the function.
616 std::vector<Block*> blocks;
619 // The stack of functions.
620 typedef std::vector<Open_function> Open_functions;
622 // Set up the built-in unsafe package.
624 import_unsafe(const std::string&, bool is_exported, Location);
626 // Return the current binding contour.
631 current_bindings() const;
633 // Get the name of the magic initialization function.
637 // Get the decl for the magic initialization function.
639 initialization_function_decl();
641 // Write the magic initialization function.
643 write_initialization_function(tree fndecl, tree init_stmt_list);
645 // Initialize imported packages.
649 // Register variables with the garbage collector.
651 register_gc_vars(const std::vector<Named_object*>&, tree*);
653 // Build a pointer to a Go string constant. This returns a pointer
656 ptr_go_string_constant_tree(const std::string&);
658 // Return the type of a trampoline.
660 trampoline_type_tree();
662 // Type used to map import names to packages.
663 typedef std::map<std::string, Package*> Imports;
665 // Type used to map package names to packages.
666 typedef std::map<std::string, Package*> Packages;
668 // Type used to map variables to the function calls that set them.
669 // This is used for initialization dependency analysis.
670 typedef std::map<Variable*, Named_object*> Var_deps;
672 // Type used to queue writing a type specific function.
673 struct Specific_type_function
677 std::string hash_name;
678 Function_type* hash_fntype;
679 std::string equal_name;
680 Function_type* equal_fntype;
682 Specific_type_function(Type* atype, Named_type* aname,
683 const std::string& ahash_name,
684 Function_type* ahash_fntype,
685 const std::string& aequal_name,
686 Function_type* aequal_fntype)
687 : type(atype), name(aname), hash_name(ahash_name),
688 hash_fntype(ahash_fntype), equal_name(aequal_name),
689 equal_fntype(aequal_fntype)
693 // The backend generator.
695 // The object used to keep track of file names and line numbers.
697 // The package we are compiling.
699 // The list of currently open functions during parsing.
700 Open_functions functions_;
701 // The global binding contour. This includes the builtin functions
702 // and the package we are compiling.
704 // Mapping from import file names to packages.
706 // Whether the magic unsafe package was imported.
707 bool imported_unsafe_;
708 // Mapping from package names we have seen to packages. This does
709 // not include the package we are compiling.
711 // The functions named "init", if there are any.
712 std::vector<Named_object*> init_functions_;
713 // A mapping from variables to the function calls that initialize
714 // them, if it is not stored in the variable's init or preinit.
715 // This is used for dependency analysis.
717 // Whether we need a magic initialization function.
719 // The name of the magic initialization function.
720 std::string init_fn_name_;
721 // A list of import control variables for packages that we import.
722 std::set<Import_init> imported_init_fns_;
723 // The package path used for reflection data.
724 std::string pkgpath_;
725 // The package path to use for a symbol name.
726 std::string pkgpath_symbol_;
727 // The prefix to use for symbols, from the -fgo-prefix option.
729 // Whether pkgpath_ has been set.
731 // Whether an explicit package path was set by -fgo-pkgpath.
732 bool pkgpath_from_option_;
733 // Whether an explicit prefix was set by -fgo-prefix.
734 bool prefix_from_option_;
735 // A list of types to verify.
736 std::vector<Type*> verify_types_;
737 // A list of interface types defined while parsing.
738 std::vector<Interface_type*> interface_types_;
739 // Type specific functions to write out.
740 std::vector<Specific_type_function*> specific_type_functions_;
741 // Whether we are done writing out specific type functions.
742 bool specific_type_functions_are_written_;
743 // Whether named types have been converted.
744 bool named_types_are_converted_;
747 // A block of statements.
752 Block(Block* enclosing, Location);
754 // Return the enclosing block.
757 { return this->enclosing_; }
759 // Return the bindings of the block.
762 { return this->bindings_; }
766 { return this->bindings_; }
768 // Look at the block's statements.
769 const std::vector<Statement*>*
771 { return &this->statements_; }
773 // Return the start location. This is normally the location of the
774 // left curly brace which starts the block.
776 start_location() const
777 { return this->start_location_; }
779 // Return the end location. This is normally the location of the
780 // right curly brace which ends the block.
783 { return this->end_location_; }
785 // Add a statement to the block.
787 add_statement(Statement*);
789 // Add a statement to the front of the block.
791 add_statement_at_front(Statement*);
793 // Replace a statement in a block.
795 replace_statement(size_t index, Statement*);
797 // Add a Statement before statement number INDEX.
799 insert_statement_before(size_t index, Statement*);
801 // Add a Statement after statement number INDEX.
803 insert_statement_after(size_t index, Statement*);
805 // Set the end location of the block.
807 set_end_location(Location location)
808 { this->end_location_ = location; }
810 // Traverse the tree.
814 // Set final types for unspecified variables and constants.
818 // Return true if execution of this block may fall through to the
821 may_fall_through() const;
823 // Convert the block to the backend representation.
825 get_backend(Translate_context*);
827 // Iterate over statements.
829 typedef std::vector<Statement*>::iterator iterator;
833 { return this->statements_.begin(); }
837 { return this->statements_.end(); }
842 // Statements in the block.
843 std::vector<Statement*> statements_;
846 // Location of start of block.
847 Location start_location_;
848 // Location of end of block.
849 Location end_location_;
857 Function(Function_type* type, Function*, Block*, Location);
859 // Return the function's type.
862 { return this->type_; }
864 // Return the enclosing function if there is one.
867 { return this->enclosing_; }
869 // Set the enclosing function. This is used when building thunks
870 // for functions which call recover.
872 set_enclosing(Function* enclosing)
874 go_assert(this->enclosing_ == NULL);
875 this->enclosing_ = enclosing;
878 // The result variables.
879 typedef std::vector<Named_object*> Results;
881 // Create the result variables in the outer block.
883 create_result_variables(Gogo*);
885 // Update the named result variables when cloning a function which
888 update_result_variables();
890 // Return the result variables.
893 { return this->results_; }
895 // Whether the result variables have names.
897 results_are_named() const
898 { return this->results_are_named_; }
900 // Add a new field to the closure variable.
902 add_closure_field(Named_object* var, Location loc)
903 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
905 // Whether this function needs a closure.
907 needs_closure() const
908 { return !this->closure_fields_.empty(); }
910 // Return the closure variable, creating it if necessary. This is
911 // passed to the function as a static chain parameter.
915 // Set the closure variable. This is used when building thunks for
916 // functions which call recover.
918 set_closure_var(Named_object* v)
920 go_assert(this->closure_var_ == NULL);
921 this->closure_var_ = v;
924 // Return the variable for a reference to field INDEX in the closure
927 enclosing_var(unsigned int index)
929 go_assert(index < this->closure_fields_.size());
930 return closure_fields_[index].first;
933 // Set the type of the closure variable if there is one.
937 // Get the block of statements associated with the function.
940 { return this->block_; }
942 // Get the location of the start of the function.
945 { return this->location_; }
947 // Return whether this function is actually a method.
951 // Add a label definition to the function.
953 add_label_definition(Gogo*, const std::string& label_name, Location);
955 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
956 // if we should report errors for a goto from the current location
957 // to the label location.
959 add_label_reference(Gogo*, const std::string& label_name,
960 Location, bool issue_goto_errors);
962 // Warn about labels that are defined but not used.
964 check_labels() const;
966 // Note that a new local type has been added. Return its index.
968 new_local_type_index()
969 { return this->local_type_count_++; }
971 // Whether this function calls the predeclared recover function.
973 calls_recover() const
974 { return this->calls_recover_; }
976 // Record that this function calls the predeclared recover function.
977 // This is set during the lowering pass.
980 { this->calls_recover_ = true; }
982 // Whether this is a recover thunk function.
984 is_recover_thunk() const
985 { return this->is_recover_thunk_; }
987 // Record that this is a thunk built for a function which calls
990 set_is_recover_thunk()
991 { this->is_recover_thunk_ = true; }
993 // Whether this function already has a recover thunk.
995 has_recover_thunk() const
996 { return this->has_recover_thunk_; }
998 // Record that this function already has a recover thunk.
1000 set_has_recover_thunk()
1001 { this->has_recover_thunk_ = true; }
1003 // Swap with another function. Used only for the thunk which calls
1006 swap_for_recover(Function *);
1008 // Traverse the tree.
1010 traverse(Traverse*);
1012 // Determine types in the function.
1016 // Return the function's decl given an identifier.
1018 get_or_make_decl(Gogo*, Named_object*, tree id);
1020 // Return the function's decl after it has been built.
1024 go_assert(this->fndecl_ != NULL);
1025 return this->fndecl_;
1028 // Set the function decl to hold a tree of the function code.
1030 build_tree(Gogo*, Named_object*);
1032 // Get the value to return when not explicitly specified. May also
1033 // add statements to execute first to STMT_LIST.
1035 return_value(Gogo*, Named_object*, Location, tree* stmt_list) const;
1037 // Get a tree for the variable holding the defer stack.
1039 defer_stack(Location);
1041 // Export the function.
1043 export_func(Export*, const std::string& name) const;
1045 // Export a function with a type.
1047 export_func_with_type(Export*, const std::string& name,
1048 const Function_type*);
1050 // Import a function.
1052 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1053 Typed_identifier_list** pparameters,
1054 Typed_identifier_list** presults, bool* is_varargs);
1057 // Type for mapping from label names to Label objects.
1058 typedef Unordered_map(std::string, Label*) Labels;
1061 make_receiver_parm_decl(Gogo*, Named_object*, tree);
1064 copy_parm_to_heap(Gogo*, Named_object*, tree);
1067 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1069 typedef std::vector<std::pair<Named_object*,
1070 Location> > Closure_fields;
1072 // The function's type.
1073 Function_type* type_;
1074 // The enclosing function. This is NULL when there isn't one, which
1075 // is the normal case.
1076 Function* enclosing_;
1077 // The result variables, if any.
1079 // If there is a closure, this is the list of variables which appear
1080 // in the closure. This is created by the parser, and then resolved
1081 // to a real type when we lower parse trees.
1082 Closure_fields closure_fields_;
1083 // The closure variable, passed as a parameter using the static
1084 // chain parameter. Normally NULL.
1085 Named_object* closure_var_;
1086 // The outer block of statements in the function.
1088 // The source location of the start of the function.
1090 // Labels defined or referenced in the function.
1092 // The number of local types defined in this function.
1093 unsigned int local_type_count_;
1094 // The function decl.
1096 // The defer stack variable. A pointer to this variable is used to
1097 // distinguish the defer stack for one function from another. This
1098 // is NULL unless we actually need a defer stack.
1099 Temporary_statement* defer_stack_;
1100 // True if the result variables are named.
1101 bool results_are_named_;
1102 // True if this function calls the predeclared recover function.
1103 bool calls_recover_;
1104 // True if this a thunk built for a function which calls recover.
1105 bool is_recover_thunk_;
1106 // True if this function already has a recover thunk.
1107 bool has_recover_thunk_;
1110 // A snapshot of the current binding state.
1112 class Bindings_snapshot
1115 Bindings_snapshot(const Block*, Location);
1117 // Report any errors appropriate for a goto from the current binding
1118 // state of B to this one.
1120 check_goto_from(const Block* b, Location);
1122 // Report any errors appropriate for a goto from this binding state
1123 // to the current state of B.
1125 check_goto_to(const Block* b);
1129 check_goto_block(Location, const Block*, const Block*, size_t*);
1132 check_goto_defs(Location, const Block*, size_t, size_t);
1134 // The current block.
1135 const Block* block_;
1136 // The number of names currently defined in each open block.
1137 // Element 0 is this->block_, element 1 is
1138 // this->block_->enclosing(), etc.
1139 std::vector<size_t> counts_;
1140 // The location where this snapshot was taken.
1144 // A function declaration.
1146 class Function_declaration
1149 Function_declaration(Function_type* fntype, Location location)
1150 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1155 { return this->fntype_; }
1159 { return this->location_; }
1163 { return this->asm_name_; }
1165 // Set the assembler name.
1167 set_asm_name(const std::string& asm_name)
1168 { this->asm_name_ = asm_name; }
1170 // Return a decl for the function given an identifier.
1172 get_or_make_decl(Gogo*, Named_object*, tree id);
1174 // Export a function declaration.
1176 export_func(Export* exp, const std::string& name) const
1177 { Function::export_func_with_type(exp, name, this->fntype_); }
1180 // The type of the function.
1181 Function_type* fntype_;
1182 // The location of the declaration.
1184 // The assembler name: this is the name to use in references to the
1185 // function. This is normally empty.
1186 std::string asm_name_;
1187 // The function decl if needed.
1196 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1197 bool is_receiver, Location);
1199 // Get the type of the variable.
1206 // Return whether the type is defined yet.
1210 // Get the initial value.
1213 { return this->init_; }
1215 // Return whether there are any preinit statements.
1217 has_pre_init() const
1218 { return this->preinit_ != NULL; }
1220 // Return the preinit statements if any.
1223 { return this->preinit_; }
1225 // Return whether this is a global variable.
1228 { return this->is_global_; }
1230 // Return whether this is a function parameter.
1232 is_parameter() const
1233 { return this->is_parameter_; }
1235 // Return whether this is the receiver parameter of a method.
1238 { return this->is_receiver_; }
1240 // Change this parameter to be a receiver. This is used when
1241 // creating the thunks created for functions which call recover.
1245 go_assert(this->is_parameter_);
1246 this->is_receiver_ = true;
1249 // Change this parameter to not be a receiver. This is used when
1250 // creating the thunks created for functions which call recover.
1252 set_is_not_receiver()
1254 go_assert(this->is_parameter_);
1255 this->is_receiver_ = false;
1258 // Return whether this is the varargs parameter of a function.
1260 is_varargs_parameter() const
1261 { return this->is_varargs_parameter_; }
1263 // Whether this variable's address is taken.
1265 is_address_taken() const
1266 { return this->is_address_taken_; }
1268 // Whether this variable should live in the heap.
1271 { return this->is_address_taken_ && !this->is_global_; }
1273 // Note that something takes the address of this variable.
1276 { this->is_address_taken_ = true; }
1278 // Return whether the address is taken but does not escape.
1280 is_non_escaping_address_taken() const
1281 { return this->is_non_escaping_address_taken_; }
1283 // Note that something takes the address of this variable such that
1284 // the address does not escape the function.
1286 set_non_escaping_address_taken()
1287 { this->is_non_escaping_address_taken_ = true; }
1289 // Get the source location of the variable's declaration.
1292 { return this->location_; }
1294 // Record that this is the varargs parameter of a function.
1296 set_is_varargs_parameter()
1298 go_assert(this->is_parameter_);
1299 this->is_varargs_parameter_ = true;
1302 // Return whether the variable has been used.
1305 { return this->is_used_; }
1307 // Mark that the variable has been used.
1310 { this->is_used_ = true; }
1312 // Clear the initial value; used for error handling.
1315 { this->init_ = NULL; }
1317 // Set the initial value; used for converting shortcuts.
1319 set_init(Expression* init)
1320 { this->init_ = init; }
1322 // Get the preinit block, a block of statements to be run before the
1323 // initialization expression.
1325 preinit_block(Gogo*);
1327 // Add a statement to be run before the initialization expression.
1328 // This is only used for global variables.
1330 add_preinit_statement(Gogo*, Statement*);
1332 // Lower the initialization expression after parsing is complete.
1334 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1336 // A special case: the init value is used only to determine the
1337 // type. This is used if the variable is defined using := with the
1338 // comma-ok form of a map index or a receive expression. The init
1339 // value is actually the map index expression or receive expression.
1340 // We use this because we may not know the right type at parse time.
1342 set_type_from_init_tuple()
1343 { this->type_from_init_tuple_ = true; }
1345 // Another special case: the init value is used only to determine
1346 // the type. This is used if the variable is defined using := with
1347 // a range clause. The init value is the range expression. The
1348 // type of the variable is the index type of the range expression
1349 // (i.e., the first value returned by a range).
1351 set_type_from_range_index()
1352 { this->type_from_range_index_ = true; }
1354 // Another special case: like set_type_from_range_index, but the
1355 // type is the value type of the range expression (i.e., the second
1356 // value returned by a range).
1358 set_type_from_range_value()
1359 { this->type_from_range_value_ = true; }
1361 // Another special case: the init value is used only to determine
1362 // the type. This is used if the variable is defined using := with
1363 // a case in a select statement. The init value is the channel.
1364 // The type of the variable is the channel's element type.
1366 set_type_from_chan_element()
1367 { this->type_from_chan_element_ = true; }
1369 // After we lower the select statement, we once again set the type
1370 // from the initialization expression.
1372 clear_type_from_chan_element()
1374 go_assert(this->type_from_chan_element_);
1375 this->type_from_chan_element_ = false;
1378 // Note that this variable was created for a type switch clause.
1380 set_is_type_switch_var()
1381 { this->is_type_switch_var_ = true; }
1383 // Traverse the initializer expression.
1385 traverse_expression(Traverse*, unsigned int traverse_mask);
1387 // Determine the type of the variable if necessary.
1391 // Get the backend representation of the variable.
1393 get_backend_variable(Gogo*, Named_object*, const Package*,
1394 const std::string&);
1396 // Get the initial value of the variable as a tree. This may only
1397 // be called if has_pre_init() returns false.
1399 get_init_tree(Gogo*, Named_object* function);
1401 // Return a series of statements which sets the value of the
1402 // variable in DECL. This should only be called is has_pre_init()
1403 // returns true. DECL may be NULL for a sink variable.
1405 get_init_block(Gogo*, Named_object* function, tree decl);
1407 // Export the variable.
1409 export_var(Export*, const std::string& name) const;
1411 // Import a variable.
1413 import_var(Import*, std::string* pname, Type** ptype);
1416 // The type of a tuple.
1418 type_from_tuple(Expression*, bool) const;
1420 // The type of a range.
1422 type_from_range(Expression*, bool, bool) const;
1424 // The element type of a channel.
1426 type_from_chan_element(Expression*, bool) const;
1428 // The variable's type. This may be NULL if the type is set from
1431 // The initial value. This may be NULL if the variable should be
1432 // initialized to the default value for the type.
1434 // Statements to run before the init statement.
1436 // Location of variable definition.
1438 // Backend representation.
1439 Bvariable* backend_;
1440 // Whether this is a global variable.
1441 bool is_global_ : 1;
1442 // Whether this is a function parameter.
1443 bool is_parameter_ : 1;
1444 // Whether this is the receiver parameter of a method.
1445 bool is_receiver_ : 1;
1446 // Whether this is the varargs parameter of a function.
1447 bool is_varargs_parameter_ : 1;
1448 // Whether this variable is ever referenced.
1450 // Whether something takes the address of this variable. For a
1451 // local variable this implies that the variable has to be on the
1453 bool is_address_taken_ : 1;
1454 // Whether something takes the address of this variable such that
1455 // the address does not escape the function.
1456 bool is_non_escaping_address_taken_ : 1;
1457 // True if we have seen this variable in a traversal.
1459 // True if we have lowered the initialization expression.
1460 bool init_is_lowered_ : 1;
1461 // True if init is a tuple used to set the type.
1462 bool type_from_init_tuple_ : 1;
1463 // True if init is a range clause and the type is the index type.
1464 bool type_from_range_index_ : 1;
1465 // True if init is a range clause and the type is the value type.
1466 bool type_from_range_value_ : 1;
1467 // True if init is a channel and the type is the channel's element type.
1468 bool type_from_chan_element_ : 1;
1469 // True if this is a variable created for a type switch case.
1470 bool is_type_switch_var_ : 1;
1471 // True if we have determined types.
1472 bool determined_type_ : 1;
1475 // A variable which is really the name for a function return value, or
1478 class Result_variable
1481 Result_variable(Type* type, Function* function, int index,
1483 : type_(type), function_(function), index_(index), location_(location),
1484 backend_(NULL), is_address_taken_(false),
1485 is_non_escaping_address_taken_(false)
1488 // Get the type of the result variable.
1491 { return this->type_; }
1493 // Get the function that this is associated with.
1496 { return this->function_; }
1498 // Index in the list of function results.
1501 { return this->index_; }
1503 // The location of the variable definition.
1506 { return this->location_; }
1508 // Whether this variable's address is taken.
1510 is_address_taken() const
1511 { return this->is_address_taken_; }
1513 // Note that something takes the address of this variable.
1516 { this->is_address_taken_ = true; }
1518 // Return whether the address is taken but does not escape.
1520 is_non_escaping_address_taken() const
1521 { return this->is_non_escaping_address_taken_; }
1523 // Note that something takes the address of this variable such that
1524 // the address does not escape the function.
1526 set_non_escaping_address_taken()
1527 { this->is_non_escaping_address_taken_ = true; }
1529 // Whether this variable should live in the heap.
1532 { return this->is_address_taken_; }
1534 // Set the function. This is used when cloning functions which call
1537 set_function(Function* function)
1538 { this->function_ = function; }
1540 // Get the backend representation of the variable.
1542 get_backend_variable(Gogo*, Named_object*, const std::string&);
1545 // Type of result variable.
1547 // Function with which this is associated.
1548 Function* function_;
1549 // Index in list of results.
1551 // Where the result variable is defined.
1553 // Backend representation.
1554 Bvariable* backend_;
1555 // Whether something takes the address of this variable.
1556 bool is_address_taken_;
1557 // Whether something takes the address of this variable such that
1558 // the address does not escape the function.
1559 bool is_non_escaping_address_taken_;
1562 // The value we keep for a named constant. This lets us hold a type
1563 // and an expression.
1565 class Named_constant
1568 Named_constant(Type* type, Expression* expr, int iota_value,
1570 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1576 { return this->type_; }
1580 { return this->expr_; }
1584 { return this->iota_value_; }
1588 { return this->location_; }
1590 // Whether we are lowering.
1593 { return this->lowering_; }
1595 // Set that we are lowering.
1598 { this->lowering_ = true; }
1600 // We are no longer lowering.
1603 { this->lowering_ = false; }
1605 // Traverse the expression.
1607 traverse_expression(Traverse*);
1609 // Determine the type of the constant if necessary.
1613 // Indicate that we found and reported an error for this constant.
1617 // Export the constant.
1619 export_const(Export*, const std::string& name) const;
1621 // Import a constant.
1623 import_const(Import*, std::string*, Type**, Expression**);
1626 // The type of the constant.
1628 // The expression for the constant.
1630 // If the predeclared constant iota is used in EXPR_, this is the
1631 // value it will have. We do this because at parse time we don't
1632 // know whether the name "iota" will refer to the predeclared
1633 // constant or to something else. We put in the right value in when
1636 // The location of the definition.
1638 // Whether we are currently lowering this constant.
1642 // A type declaration.
1644 class Type_declaration
1647 Type_declaration(Location location)
1648 : location_(location), in_function_(NULL), in_function_index_(0),
1649 methods_(), issued_warning_(false)
1652 // Return the location.
1655 { return this->location_; }
1657 // Return the function in which this type is declared. This will
1658 // return NULL for a type declared in global scope.
1660 in_function(unsigned int* pindex)
1662 *pindex = this->in_function_index_;
1663 return this->in_function_;
1666 // Set the function in which this type is declared.
1668 set_in_function(Named_object* f, unsigned int index)
1670 this->in_function_ = f;
1671 this->in_function_index_ = index;
1674 // Add a method to this type. This is used when methods are defined
1677 add_method(const std::string& name, Function* function);
1679 // Add a method declaration to this type.
1681 add_method_declaration(const std::string& name, Package*,
1682 Function_type* type, Location location);
1684 // Return whether any methods were defined.
1686 has_methods() const;
1688 // Define methods when the real type is known.
1690 define_methods(Named_type*);
1692 // This is called if we are trying to use this type. It returns
1693 // true if we should issue a warning.
1698 typedef std::vector<Named_object*> Methods;
1700 // The location of the type declaration.
1702 // If this type is declared in a function, a pointer back to the
1703 // function in which it is defined.
1704 Named_object* in_function_;
1705 // The index of this type in IN_FUNCTION_.
1706 unsigned int in_function_index_;
1707 // Methods defined before the type is defined.
1709 // True if we have issued a warning about a use of this type
1710 // declaration when it is undefined.
1711 bool issued_warning_;
1714 // An unknown object. These are created by the parser for forward
1715 // references to names which have not been seen before. In a correct
1716 // program, these will always point to a real definition by the end of
1717 // the parse. Because they point to another Named_object, these may
1718 // only be referenced by Unknown_expression objects.
1723 Unknown_name(Location location)
1724 : location_(location), real_named_object_(NULL)
1727 // Return the location where this name was first seen.
1730 { return this->location_; }
1732 // Return the real named object that this points to, or NULL if it
1733 // was never resolved.
1735 real_named_object() const
1736 { return this->real_named_object_; }
1738 // Set the real named object that this points to.
1740 set_real_named_object(Named_object* no);
1743 // The location where this name was first seen.
1745 // The real named object when it is known.
1750 // A named object named. This is the result of a declaration. We
1751 // don't use a superclass because they all have to be handled
1759 // An uninitialized Named_object. We should never see this.
1760 NAMED_OBJECT_UNINITIALIZED,
1761 // An erroneous name. This indicates a parse error, to avoid
1762 // later errors about undefined references.
1763 NAMED_OBJECT_ERRONEOUS,
1764 // An unknown name. This is used for forward references. In a
1765 // correct program, these will all be resolved by the end of the
1767 NAMED_OBJECT_UNKNOWN,
1772 // A forward type declaration.
1773 NAMED_OBJECT_TYPE_DECLARATION,
1776 // A result variable in a function.
1777 NAMED_OBJECT_RESULT_VAR,
1778 // The blank identifier--the special variable named _.
1782 // A forward func declaration.
1783 NAMED_OBJECT_FUNC_DECLARATION,
1785 NAMED_OBJECT_PACKAGE
1788 // Return the classification.
1790 classification() const
1791 { return this->classification_; }
1796 is_erroneous() const
1797 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1801 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1805 { return this->classification_ == NAMED_OBJECT_CONST; }
1809 { return this->classification_ == NAMED_OBJECT_TYPE; }
1812 is_type_declaration() const
1813 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1817 { return this->classification_ == NAMED_OBJECT_VAR; }
1820 is_result_variable() const
1821 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1825 { return this->classification_ == NAMED_OBJECT_SINK; }
1829 { return this->classification_ == NAMED_OBJECT_FUNC; }
1832 is_function_declaration() const
1833 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1837 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1841 static Named_object*
1842 make_erroneous_name(const std::string& name)
1843 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1845 static Named_object*
1846 make_unknown_name(const std::string& name, Location);
1848 static Named_object*
1849 make_constant(const Typed_identifier&, const Package*, Expression*,
1852 static Named_object*
1853 make_type(const std::string&, const Package*, Type*, Location);
1855 static Named_object*
1856 make_type_declaration(const std::string&, const Package*, Location);
1858 static Named_object*
1859 make_variable(const std::string&, const Package*, Variable*);
1861 static Named_object*
1862 make_result_variable(const std::string&, Result_variable*);
1864 static Named_object*
1867 static Named_object*
1868 make_function(const std::string&, const Package*, Function*);
1870 static Named_object*
1871 make_function_declaration(const std::string&, const Package*, Function_type*,
1874 static Named_object*
1875 make_package(const std::string& alias, Package* package);
1882 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1883 return this->u_.unknown_value;
1887 unknown_value() const
1889 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1890 return this->u_.unknown_value;
1896 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1897 return this->u_.const_value;
1900 const Named_constant*
1903 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1904 return this->u_.const_value;
1910 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1911 return this->u_.type_value;
1917 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1918 return this->u_.type_value;
1922 type_declaration_value()
1924 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1925 return this->u_.type_declaration;
1928 const Type_declaration*
1929 type_declaration_value() const
1931 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1932 return this->u_.type_declaration;
1938 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1939 return this->u_.var_value;
1945 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1946 return this->u_.var_value;
1952 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1953 return this->u_.result_var_value;
1956 const Result_variable*
1957 result_var_value() const
1959 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1960 return this->u_.result_var_value;
1966 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1967 return this->u_.func_value;
1973 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1974 return this->u_.func_value;
1977 Function_declaration*
1978 func_declaration_value()
1980 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1981 return this->u_.func_declaration_value;
1984 const Function_declaration*
1985 func_declaration_value() const
1987 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1988 return this->u_.func_declaration_value;
1994 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1995 return this->u_.package_value;
1999 package_value() const
2001 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2002 return this->u_.package_value;
2007 { return this->name_; }
2009 // Return the name to use in an error message. The difference is
2010 // that if this Named_object is defined in a different package, this
2011 // will return PACKAGE.NAME.
2013 message_name() const;
2017 { return this->package_; }
2019 // Resolve an unknown value if possible. This returns the same
2020 // Named_object or a new one.
2024 Named_object* ret = this;
2025 if (this->is_unknown())
2027 Named_object* r = this->unknown_value()->real_named_object();
2037 const Named_object* ret = this;
2038 if (this->is_unknown())
2040 const Named_object* r = this->unknown_value()->real_named_object();
2047 // The location where this object was defined or referenced.
2051 // Convert a variable to the backend representation.
2053 get_backend_variable(Gogo*, Named_object* function);
2055 // Return a tree for the external identifier for this object.
2059 // Return a tree representing this object.
2061 get_tree(Gogo*, Named_object* function);
2063 // Define a type declaration.
2065 set_type_value(Named_type*);
2067 // Define a function declaration.
2069 set_function_value(Function*);
2071 // Declare an unknown name as a type declaration.
2075 // Export this object.
2077 export_named_object(Export*) const;
2080 Named_object(const std::string&, const Package*, Classification);
2082 // The name of the object.
2084 // The package that this object is in. This is NULL if it is in the
2085 // file we are compiling.
2086 const Package* package_;
2087 // The type of object this is.
2088 Classification classification_;
2092 Unknown_name* unknown_value;
2093 Named_constant* const_value;
2094 Named_type* type_value;
2095 Type_declaration* type_declaration;
2096 Variable* var_value;
2097 Result_variable* result_var_value;
2098 Function* func_value;
2099 Function_declaration* func_declaration_value;
2100 Package* package_value;
2102 // The DECL tree for this object if we have already converted it.
2106 // A binding contour. This binds names to objects.
2111 // Type for mapping from names to objects.
2112 typedef Unordered_map(std::string, Named_object*) Contour;
2114 Bindings(Bindings* enclosing);
2116 // Add an erroneous name.
2118 add_erroneous_name(const std::string& name)
2119 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2121 // Add an unknown name.
2123 add_unknown_name(const std::string& name, Location location)
2125 return this->add_named_object(Named_object::make_unknown_name(name,
2131 add_constant(const Typed_identifier& tid, const Package* package,
2132 Expression* expr, int iota_value)
2134 return this->add_named_object(Named_object::make_constant(tid, package,
2141 add_type(const std::string& name, const Package* package, Type* type,
2144 return this->add_named_object(Named_object::make_type(name, package, type,
2148 // Add a named type. This is used for builtin types, and to add an
2149 // imported type to the global scope.
2151 add_named_type(Named_type* named_type);
2153 // Add a type declaration.
2155 add_type_declaration(const std::string& name, const Package* package,
2158 Named_object* no = Named_object::make_type_declaration(name, package,
2160 return this->add_named_object(no);
2165 add_variable(const std::string& name, const Package* package,
2168 return this->add_named_object(Named_object::make_variable(name, package,
2172 // Add a result variable.
2174 add_result_variable(const std::string& name, Result_variable* result)
2176 return this->add_named_object(Named_object::make_result_variable(name,
2182 add_function(const std::string& name, const Package*, Function* function);
2184 // Add a function declaration.
2186 add_function_declaration(const std::string& name, const Package* package,
2187 Function_type* type, Location location);
2189 // Add a package. The location is the location of the import
2192 add_package(const std::string& alias, Package* package)
2194 Named_object* no = Named_object::make_package(alias, package);
2195 return this->add_named_object(no);
2198 // Define a type which was already declared.
2200 define_type(Named_object*, Named_type*);
2202 // Add a method to the list of objects. This is not added to the
2205 add_method(Named_object*);
2207 // Add a named object to this binding.
2209 add_named_object(Named_object* no)
2210 { return this->add_named_object_to_contour(&this->bindings_, no); }
2212 // Clear all names in file scope from the bindings.
2216 // Look up a name in this binding contour and in any enclosing
2217 // binding contours. This returns NULL if the name is not found.
2219 lookup(const std::string&) const;
2221 // Look up a name in this binding contour without looking in any
2222 // enclosing binding contours. Returns NULL if the name is not found.
2224 lookup_local(const std::string&) const;
2228 remove_binding(Named_object*);
2230 // Mark all variables as used. This is used for some types of parse
2235 // Traverse the tree. See the Traverse class.
2237 traverse(Traverse*, bool is_global);
2239 // Iterate over definitions. This does not include things which
2240 // were only declared.
2242 typedef std::vector<Named_object*>::const_iterator
2243 const_definitions_iterator;
2245 const_definitions_iterator
2246 begin_definitions() const
2247 { return this->named_objects_.begin(); }
2249 const_definitions_iterator
2250 end_definitions() const
2251 { return this->named_objects_.end(); }
2253 // Return the number of definitions.
2255 size_definitions() const
2256 { return this->named_objects_.size(); }
2258 // Return whether there are no definitions.
2260 empty_definitions() const
2261 { return this->named_objects_.empty(); }
2263 // Iterate over declarations. This is everything that has been
2264 // declared, which includes everything which has been defined.
2266 typedef Contour::const_iterator const_declarations_iterator;
2268 const_declarations_iterator
2269 begin_declarations() const
2270 { return this->bindings_.begin(); }
2272 const_declarations_iterator
2273 end_declarations() const
2274 { return this->bindings_.end(); }
2276 // Return the number of declarations.
2278 size_declarations() const
2279 { return this->bindings_.size(); }
2281 // Return whether there are no declarations.
2283 empty_declarations() const
2284 { return this->bindings_.empty(); }
2286 // Return the first declaration.
2289 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2293 add_named_object_to_contour(Contour*, Named_object*);
2296 new_definition(Named_object*, Named_object*);
2298 // Enclosing bindings.
2299 Bindings* enclosing_;
2300 // The list of objects.
2301 std::vector<Named_object*> named_objects_;
2302 // The mapping from names to objects.
2311 Label(const std::string& name)
2312 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2313 refs_(), is_used_(false), blabel_(NULL)
2316 // Return the label's name.
2319 { return this->name_; }
2321 // Return whether the label has been defined.
2324 { return !Linemap::is_unknown_location(this->location_); }
2326 // Return whether the label has been used.
2329 { return this->is_used_; }
2331 // Record that the label is used.
2334 { this->is_used_ = true; }
2336 // Return the location of the definition.
2339 { return this->location_; }
2341 // Return the bindings snapshot.
2344 { return this->snapshot_; }
2346 // Add a snapshot of a goto which refers to this label.
2348 add_snapshot_ref(Bindings_snapshot* snapshot)
2350 go_assert(Linemap::is_unknown_location(this->location_));
2351 this->refs_.push_back(snapshot);
2354 // Return the list of snapshots of goto statements which refer to
2356 const std::vector<Bindings_snapshot*>&
2358 { return this->refs_; }
2360 // Clear the references.
2364 // Define the label at LOCATION with the given bindings snapshot.
2366 define(Location location, Bindings_snapshot* snapshot)
2368 go_assert(Linemap::is_unknown_location(this->location_)
2369 && this->snapshot_ == NULL);
2370 this->location_ = location;
2371 this->snapshot_ = snapshot;
2374 // Return the backend representation for this label.
2376 get_backend_label(Translate_context*);
2378 // Return an expression for the address of this label. This is used
2379 // to get the return address of a deferred function to see whether
2380 // the function may call recover.
2382 get_addr(Translate_context*, Location location);
2385 // The name of the label.
2387 // The location of the definition. This is 0 if the label has not
2388 // yet been defined.
2390 // A snapshot of the set of bindings defined at this label, used to
2391 // issue errors about invalid goto statements.
2392 Bindings_snapshot* snapshot_;
2393 // A list of snapshots of goto statements which refer to this label.
2394 std::vector<Bindings_snapshot*> refs_;
2395 // Whether the label has been used.
2397 // The backend representation.
2401 // An unnamed label. These are used when lowering loops.
2406 Unnamed_label(Location location)
2407 : location_(location), blabel_(NULL)
2410 // Get the location where the label is defined.
2413 { return this->location_; }
2415 // Set the location where the label is defined.
2417 set_location(Location location)
2418 { this->location_ = location; }
2420 // Return a statement which defines this label.
2422 get_definition(Translate_context*);
2424 // Return a goto to this label from LOCATION.
2426 get_goto(Translate_context*, Location location);
2429 // Return the backend representation.
2431 get_blabel(Translate_context*);
2433 // The location where the label is defined.
2435 // The backend representation of this label.
2439 // An imported package.
2444 Package(const std::string& pkgpath, Location location);
2446 // Get the package path used for all symbols exported from this
2450 { return this->pkgpath_; }
2452 // Return the package path to use for a symbol name.
2454 pkgpath_symbol() const
2455 { return this->pkgpath_symbol_; }
2457 // Return the location of the import statement.
2460 { return this->location_; }
2462 // Return whether we know the name of this package yet.
2464 has_package_name() const
2465 { return !this->package_name_.empty(); }
2467 // The name that this package uses in its package clause. This may
2468 // be different from the name in the associated Named_object if the
2469 // import statement used an alias.
2471 package_name() const
2473 go_assert(!this->package_name_.empty());
2474 return this->package_name_;
2477 // The priority of this package. The init function of packages with
2478 // lower priority must be run before the init function of packages
2479 // with higher priority.
2482 { return this->priority_; }
2484 // Set the priority.
2486 set_priority(int priority);
2488 // Return the bindings.
2491 { return this->bindings_; }
2493 // Whether some symbol from the package was used.
2496 { return this->used_; }
2498 // Note that some symbol from this package was used.
2501 { this->used_ = true; }
2503 // Clear the used field for the next file.
2506 { this->used_ = false; }
2508 // Whether this package was imported in the current file.
2511 { return this->is_imported_; }
2513 // Note that this package was imported in the current file.
2516 { this->is_imported_ = true; }
2518 // Clear the imported field for the next file.
2521 { this->is_imported_ = false; }
2523 // Whether this package was imported with a name of "_".
2525 uses_sink_alias() const
2526 { return this->uses_sink_alias_; }
2528 // Note that this package was imported with a name of "_".
2530 set_uses_sink_alias()
2531 { this->uses_sink_alias_ = true; }
2533 // Clear the sink alias field for the next file.
2535 clear_uses_sink_alias()
2536 { this->uses_sink_alias_ = false; }
2538 // Look up a name in the package. Returns NULL if the name is not
2541 lookup(const std::string& name) const
2542 { return this->bindings_->lookup(name); }
2544 // Set the name of the package.
2546 set_package_name(const std::string& name, Location);
2548 // Set the location of the package. This is used to record the most
2549 // recent import location.
2551 set_location(Location location)
2552 { this->location_ = location; }
2554 // Add a constant to the package.
2556 add_constant(const Typed_identifier& tid, Expression* expr)
2557 { return this->bindings_->add_constant(tid, this, expr, 0); }
2559 // Add a type to the package.
2561 add_type(const std::string& name, Type* type, Location location)
2562 { return this->bindings_->add_type(name, this, type, location); }
2564 // Add a type declaration to the package.
2566 add_type_declaration(const std::string& name, Location location)
2567 { return this->bindings_->add_type_declaration(name, this, location); }
2569 // Add a variable to the package.
2571 add_variable(const std::string& name, Variable* variable)
2572 { return this->bindings_->add_variable(name, this, variable); }
2574 // Add a function declaration to the package.
2576 add_function_declaration(const std::string& name, Function_type* type,
2578 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2580 // Determine types of constants.
2585 // The package path for type reflection data.
2586 std::string pkgpath_;
2587 // The package path for symbol names.
2588 std::string pkgpath_symbol_;
2589 // The name that this package uses in the package clause. This may
2590 // be the empty string if it is not yet known.
2591 std::string package_name_;
2592 // The names in this package.
2593 Bindings* bindings_;
2594 // The priority of this package. A package has a priority higher
2595 // than the priority of all of the packages that it imports. This
2596 // is used to run init functions in the right order.
2598 // The location of the import statement.
2600 // True if some name from this package was used. This is mutable
2601 // because we can use a package even if we have a const pointer to
2604 // True if this package was imported in the current file.
2606 // True if this package was imported with a name of "_".
2607 bool uses_sink_alias_;
2610 // Return codes for the traversal functions. This is not an enum
2611 // because we want to be able to declare traversal functions in other
2612 // header files without including this one.
2614 // Continue traversal as usual.
2615 const int TRAVERSE_CONTINUE = -1;
2618 const int TRAVERSE_EXIT = 0;
2620 // Continue traversal, but skip components of the current object.
2621 // E.g., if this is returned by Traverse::statement, we do not
2622 // traverse the expressions in the statement even if
2623 // traverse_expressions is set in the traverse_mask.
2624 const int TRAVERSE_SKIP_COMPONENTS = 1;
2626 // This class is used when traversing the parse tree. The caller uses
2627 // a subclass which overrides functions as desired.
2632 // These bitmasks say what to traverse.
2633 static const unsigned int traverse_variables = 0x1;
2634 static const unsigned int traverse_constants = 0x2;
2635 static const unsigned int traverse_functions = 0x4;
2636 static const unsigned int traverse_blocks = 0x8;
2637 static const unsigned int traverse_statements = 0x10;
2638 static const unsigned int traverse_expressions = 0x20;
2639 static const unsigned int traverse_types = 0x40;
2641 Traverse(unsigned int traverse_mask)
2642 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2645 virtual ~Traverse();
2647 // The bitmask of what to traverse.
2649 traverse_mask() const
2650 { return this->traverse_mask_; }
2652 // Record that we are going to traverse a type. This returns true
2653 // if the type has already been seen in this traversal. This is
2654 // required because types, unlike expressions, can form a circular
2657 remember_type(const Type*);
2659 // Record that we are going to see an expression. This returns true
2660 // if the expression has already been seen in this traversal. This
2661 // is only needed for cases where multiple expressions can point to
2664 remember_expression(const Expression*);
2666 // These functions return one of the TRAVERSE codes defined above.
2668 // If traverse_variables is set in the mask, this is called for
2669 // every variable in the tree.
2671 variable(Named_object*);
2673 // If traverse_constants is set in the mask, this is called for
2674 // every named constant in the tree. The bool parameter is true for
2675 // a global constant.
2677 constant(Named_object*, bool);
2679 // If traverse_functions is set in the mask, this is called for
2680 // every function in the tree.
2682 function(Named_object*);
2684 // If traverse_blocks is set in the mask, this is called for every
2685 // block in the tree.
2689 // If traverse_statements is set in the mask, this is called for
2690 // every statement in the tree.
2692 statement(Block*, size_t* index, Statement*);
2694 // If traverse_expressions is set in the mask, this is called for
2695 // every expression in the tree.
2697 expression(Expression**);
2699 // If traverse_types is set in the mask, this is called for every
2700 // type in the tree.
2705 // A hash table for types we have seen during this traversal. Note
2706 // that this uses the default hash functions for pointers rather
2707 // than Type_hash_identical and Type_identical. This is because for
2708 // traversal we care about seeing a specific type structure. If
2709 // there are two separate instances of identical types, we want to
2711 typedef Unordered_set(const Type*) Types_seen;
2713 typedef Unordered_set(const Expression*) Expressions_seen;
2715 // Bitmask of what sort of objects to traverse.
2716 unsigned int traverse_mask_;
2717 // Types which have been seen in this traversal.
2718 Types_seen* types_seen_;
2719 // Expressions which have been seen in this traversal.
2720 Expressions_seen* expressions_seen_;
2723 // A class which makes it easier to insert new statements before the
2724 // current statement during a traversal.
2726 class Statement_inserter
2729 // Empty constructor.
2730 Statement_inserter()
2731 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2734 // Constructor for a statement in a block.
2735 Statement_inserter(Block* block, size_t *pindex)
2736 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2739 // Constructor for a global variable.
2740 Statement_inserter(Gogo* gogo, Variable* var)
2741 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2742 { go_assert(var->is_global()); }
2744 // We use the default copy constructor and assignment operator.
2746 // Insert S before the statement we are traversing, or before the
2747 // initialization expression of a global variable.
2749 insert(Statement* s);
2752 // The block that the statement is in.
2754 // The index of the statement that we are traversing.
2756 // The IR, needed when looking at an initializer expression for a
2759 // The global variable, when looking at an initializer expression.
2763 // When translating the gogo IR into the backend data structure, this
2764 // is the context we pass down the blocks and statements.
2766 class Translate_context
2769 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2771 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2772 block_(block), bblock_(bblock), is_const_(false)
2779 { return this->gogo_; }
2783 { return this->backend_; }
2787 { return this->function_; }
2791 { return this->block_; }
2795 { return this->bblock_; }
2799 { return this->is_const_; }
2801 // Make a constant context.
2804 { this->is_const_ = true; }
2807 // The IR for the entire compilation unit.
2809 // The generator for the backend data structures.
2811 // The function we are currently translating. NULL if not in a
2812 // function, e.g., the initializer of a global variable.
2813 Named_object* function_;
2814 // The block we are currently translating. NULL if not in a
2817 // The backend representation of the current block. NULL if block_
2820 // Whether this is being evaluated in a constant context. This is
2821 // used for type descriptor initializers.
2825 // Runtime error codes. These must match the values in
2826 // libgo/runtime/go-runtime-error.c.
2828 // Slice index out of bounds: negative or larger than the length of
2830 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2832 // Array index out of bounds.
2833 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2835 // String index out of bounds.
2836 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2838 // Slice slice out of bounds: negative or larger than the length of
2839 // the slice or high bound less than low bound.
2840 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2842 // Array slice out of bounds.
2843 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2845 // String slice out of bounds.
2846 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2848 // Dereference of nil pointer. This is used when there is a
2849 // dereference of a pointer to a very large struct or array, to ensure
2850 // that a gigantic array is not used a proxy to access random memory
2852 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2854 // Slice length or capacity out of bounds in make: negative or
2855 // overflow or length greater than capacity.
2856 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2858 // Map capacity out of bounds in make: negative or overflow.
2859 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2861 // Channel capacity out of bounds in make: negative or overflow.
2862 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2864 // Division by zero.
2865 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
2867 // This is used by some of the langhooks.
2868 extern Gogo* go_get_gogo();
2870 // Whether we have seen any errors. FIXME: Replace with a backend
2872 extern bool saw_errors();
2874 #endif // !defined(GO_GOGO_H)