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;
42 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 relative import path as set from the command line.
210 // Returns an empty string if it was not set.
212 relative_import_path() const
213 { return this->relative_import_path_; }
215 // Set the relative import path from a command line option.
217 set_relative_import_path(const std::string& s)
218 { this->relative_import_path_ = s; }
220 // Return whether to check for division by zero in binary operations.
222 check_divide_by_zero() const
223 { return this->check_divide_by_zero_; }
225 // Set the option to check division by zero from a command line option.
227 set_check_divide_by_zero(bool b)
228 { this->check_divide_by_zero_ = b; }
230 // Return whether to check for division overflow in binary operations.
232 check_divide_overflow() const
233 { return this->check_divide_overflow_; }
235 // Set the option to check division overflow from a command line option.
237 set_check_divide_overflow(bool b)
238 { this->check_divide_overflow_ = b; }
240 // Return the priority to use for the package we are compiling.
241 // This is two more than the largest priority of any package we
244 package_priority() const;
246 // Import a package. FILENAME is the file name argument, LOCAL_NAME
247 // is the local name to give to the package. If LOCAL_NAME is empty
248 // the declarations are added to the global scope.
250 import_package(const std::string& filename, const std::string& local_name,
251 bool is_local_name_exported, Location);
253 // Whether we are the global binding level.
255 in_global_scope() const;
257 // Look up a name in the current binding contours.
259 lookup(const std::string&, Named_object** pfunction) const;
261 // Look up a name in the current block.
263 lookup_in_block(const std::string&) const;
265 // Look up a name in the global namespace--the universal scope.
267 lookup_global(const char*) const;
269 // Add a new imported package. REAL_NAME is the real name of the
270 // package. ALIAS is the alias of the package; this may be the same
271 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
272 // this package should be added to the global namespace; this is
273 // true if the alias is ".". LOCATION is the location of the import
274 // statement. This returns the new package, or NULL on error.
276 add_imported_package(const std::string& real_name, const std::string& alias,
277 bool is_alias_exported,
278 const std::string& pkgpath,
280 bool* padd_to_globals);
282 // Register a package. This package may or may not be imported.
283 // This returns the Package structure for the package, creating if
286 register_package(const std::string& pkgpath, Location);
288 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
289 // method function should be added to the type of its receiver.
291 start_function(const std::string& name, Function_type* type,
292 bool add_method_to_type, Location);
294 // Finish compiling a function.
296 finish_function(Location);
298 // Return the current function.
300 current_function() const;
302 // Return the current block.
306 // Start a new block. This is not initially associated with a
309 start_block(Location);
311 // Finish the current block and return it.
313 finish_block(Location);
315 // Declare an erroneous name. This is used to avoid knock-on errors
316 // after a parsing error.
318 add_erroneous_name(const std::string& name);
320 // Declare an unknown name. This is used while parsing. The name
321 // must be resolved by the end of the parse. Unknown names are
322 // always added at the package level.
324 add_unknown_name(const std::string& name, Location);
326 // Declare a function.
328 declare_function(const std::string&, Function_type*, Location);
330 // Declare a function at the package level. This is used for
331 // functions generated for a type.
333 declare_package_function(const std::string&, Function_type*, Location);
337 add_label_definition(const std::string&, Location);
339 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
340 // report errors for a goto from the current location to the label
343 add_label_reference(const std::string&, Location,
344 bool issue_goto_errors);
346 // Return a snapshot of the current binding state.
348 bindings_snapshot(Location);
350 // Add a statement to the current block.
352 add_statement(Statement*);
354 // Add a block to the current block.
356 add_block(Block*, Location);
360 add_constant(const Typed_identifier&, Expression*, int iota_value);
364 add_type(const std::string&, Type*, Location);
366 // Add a named type. This is used for builtin types, and to add an
367 // imported type to the global scope.
369 add_named_type(Named_type*);
373 declare_type(const std::string&, Location);
375 // Declare a type at the package level. This is used when the
376 // parser sees an unknown name where a type name is required.
378 declare_package_type(const std::string&, Location);
380 // Define a type which was already declared.
382 define_type(Named_object*, Named_type*);
386 add_variable(const std::string&, Variable*);
388 // Add a sink--a reference to the blank identifier _.
392 // Add a type which needs to be verified. This is used for sink
393 // types, just to give appropriate error messages.
395 add_type_to_verify(Type* type);
397 // Add a named object to the current namespace. This is used for
398 // import . "package".
400 add_named_object(Named_object*);
402 // Add an identifier to the list of names seen in the file block.
404 add_file_block_name(const std::string& name, Location location)
405 { this->file_block_names_[name] = location; }
407 // Mark all local variables in current bindings as used. This is
408 // used when there is a parse error to avoid useless errors.
412 // Return a name to use for an error case. This should only be used
413 // after reporting an error, and is used to avoid useless knockon
418 // Return whether the name indicates an error.
420 is_erroneous_name(const std::string&);
422 // Return a name to use for a thunk function. A thunk function is
423 // one we create during the compilation, for a go statement or a
424 // defer statement or a method expression.
428 // Return whether an object is a thunk.
430 is_thunk(const Named_object*);
432 // Note that we've seen an interface type. This is used to build
433 // all required interface method tables.
435 record_interface_type(Interface_type*);
437 // Note that we need an initialization function.
440 { this->need_init_fn_ = true; }
442 // Clear out all names in file scope. This is called when we start
443 // parsing a new file.
447 // Record that VAR1 must be initialized after VAR2. This is used
448 // when VAR2 does not appear in VAR1's INIT or PREINIT.
450 record_var_depends_on(Variable* var1, Named_object* var2)
452 go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
453 this->var_deps_[var1] = var2;
456 // Return the variable that VAR depends on, or NULL if none.
458 var_depends_on(Variable* var) const
460 Var_deps::const_iterator p = this->var_deps_.find(var);
461 return p != this->var_deps_.end() ? p->second : NULL;
464 // Queue up a type-specific function to be written out. This is
465 // used when a type-specific function is needed when not at the top
468 queue_specific_type_function(Type* type, Named_type* name,
469 const std::string& hash_name,
470 Function_type* hash_fntype,
471 const std::string& equal_name,
472 Function_type* equal_fntype);
474 // Write out queued specific type functions.
476 write_specific_type_functions();
478 // Whether we are done writing out specific type functions.
480 specific_type_functions_are_written() const
481 { return this->specific_type_functions_are_written_; }
483 // Traverse the tree. See the Traverse class.
487 // Define the predeclared global names.
489 define_global_names();
491 // Verify and complete all types.
495 // Lower the parse tree.
499 // Lower all the statements in a block.
501 lower_block(Named_object* function, Block*);
503 // Lower an expression.
505 lower_expression(Named_object* function, Statement_inserter*, Expression**);
509 lower_constant(Named_object*);
511 // Flatten all the statements in a block.
513 flatten_block(Named_object* function, Block*);
515 // Flatten an expression.
517 flatten_expression(Named_object* function, Statement_inserter*, Expression**);
519 // Create all necessary function descriptors.
521 create_function_descriptors();
523 // Finalize the method lists and build stub methods for named types.
527 // Work out the types to use for unspecified variables and
532 // Type check the program.
536 // Check the types in a single block. This is used for complicated
539 check_types_in_block(Block*);
541 // Check for return statements.
543 check_return_statements();
549 // Add an import control function for an imported package to the
552 add_import_init_fn(const std::string& package_name,
553 const std::string& init_name, int prio);
555 // Turn short-cut operators (&&, ||) into explicit if statements.
559 // Use temporary variables to force order of evaluation.
563 // Flatten parse tree.
567 // Build thunks for functions which call recover.
569 build_recover_thunks();
571 // Simplify statements which might use thunks: go and defer
574 simplify_thunk_statements();
576 // Dump AST if -fgo-dump-ast is set
578 dump_ast(const char* basename);
580 // Convert named types to the backend representation.
582 convert_named_types();
584 // Convert named types in a list of bindings.
586 convert_named_types_in_bindings(Bindings*);
588 // True if named types have been converted to the backend
591 named_types_are_converted() const
592 { return this->named_types_are_converted_; }
594 // Write out the global values.
598 // Build a call to the runtime error function.
600 runtime_error(int code, Location);
602 // Build required interface method tables.
604 build_interface_method_tables();
606 // Return an expression which allocates memory to hold values of type TYPE.
608 allocate_memory(Type *type, Location);
610 // Get the name of the magic initialization function.
615 // During parsing, we keep a stack of functions. Each function on
616 // the stack is one that we are currently parsing. For each
617 // function, we keep track of the current stack of blocks.
621 Named_object* function;
622 // The stack of active blocks in the function.
623 std::vector<Block*> blocks;
626 // The stack of functions.
627 typedef std::vector<Open_function> Open_functions;
629 // Set up the built-in unsafe package.
631 import_unsafe(const std::string&, bool is_exported, Location);
633 // Return the current binding contour.
638 current_bindings() const;
640 // Get the decl for the magic initialization function.
642 initialization_function_decl();
644 // Create the magic initialization function.
646 create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
648 // Initialize imported packages.
650 init_imports(std::vector<Bstatement*>&);
652 // Register variables with the garbage collector.
654 register_gc_vars(const std::vector<Named_object*>&,
655 std::vector<Bstatement*>&);
657 // Type used to map import names to packages.
658 typedef std::map<std::string, Package*> Imports;
660 // Type used to map package names to packages.
661 typedef std::map<std::string, Package*> Packages;
663 // Type used to map variables to the function calls that set them.
664 // This is used for initialization dependency analysis.
665 typedef std::map<Variable*, Named_object*> Var_deps;
667 // Type used to map identifiers in the file block to the location
668 // where they were defined.
669 typedef Unordered_map(std::string, Location) File_block_names;
671 // Type used to queue writing a type specific function.
672 struct Specific_type_function
676 std::string hash_name;
677 Function_type* hash_fntype;
678 std::string equal_name;
679 Function_type* equal_fntype;
681 Specific_type_function(Type* atype, Named_type* aname,
682 const std::string& ahash_name,
683 Function_type* ahash_fntype,
684 const std::string& aequal_name,
685 Function_type* aequal_fntype)
686 : type(atype), name(aname), hash_name(ahash_name),
687 hash_fntype(ahash_fntype), equal_name(aequal_name),
688 equal_fntype(aequal_fntype)
692 // The backend generator.
694 // The object used to keep track of file names and line numbers.
696 // The package we are compiling.
698 // The list of currently open functions during parsing.
699 Open_functions functions_;
700 // The global binding contour. This includes the builtin functions
701 // and the package we are compiling.
703 // The list of names we have seen in the file block.
704 File_block_names file_block_names_;
705 // Mapping from import file names to packages.
707 // Whether the magic unsafe package was imported.
708 bool imported_unsafe_;
709 // Mapping from package names we have seen to packages. This does
710 // not include the package we are compiling.
712 // The functions named "init", if there are any.
713 std::vector<Named_object*> init_functions_;
714 // A mapping from variables to the function calls that initialize
715 // them, if it is not stored in the variable's init or preinit.
716 // This is used for dependency analysis.
718 // Whether we need a magic initialization function.
720 // The name of the magic initialization function.
721 std::string init_fn_name_;
722 // A list of import control variables for packages that we import.
723 std::set<Import_init> imported_init_fns_;
724 // The package path used for reflection data.
725 std::string pkgpath_;
726 // The package path to use for a symbol name.
727 std::string pkgpath_symbol_;
728 // The prefix to use for symbols, from the -fgo-prefix option.
730 // Whether pkgpath_ has been set.
732 // Whether an explicit package path was set by -fgo-pkgpath.
733 bool pkgpath_from_option_;
734 // Whether an explicit prefix was set by -fgo-prefix.
735 bool prefix_from_option_;
736 // The relative import path, from the -fgo-relative-import-path
738 std::string relative_import_path_;
739 // Whether or not to check for division by zero, from the
740 // -fgo-check-divide-zero option.
741 bool check_divide_by_zero_;
742 // Whether or not to check for division overflow, from the
743 // -fgo-check-divide-overflow option.
744 bool check_divide_overflow_;
745 // A list of types to verify.
746 std::vector<Type*> verify_types_;
747 // A list of interface types defined while parsing.
748 std::vector<Interface_type*> interface_types_;
749 // Type specific functions to write out.
750 std::vector<Specific_type_function*> specific_type_functions_;
751 // Whether we are done writing out specific type functions.
752 bool specific_type_functions_are_written_;
753 // Whether named types have been converted.
754 bool named_types_are_converted_;
757 // A block of statements.
762 Block(Block* enclosing, Location);
764 // Return the enclosing block.
767 { return this->enclosing_; }
769 // Return the bindings of the block.
772 { return this->bindings_; }
776 { return this->bindings_; }
778 // Look at the block's statements.
779 const std::vector<Statement*>*
781 { return &this->statements_; }
783 // Return the start location. This is normally the location of the
784 // left curly brace which starts the block.
786 start_location() const
787 { return this->start_location_; }
789 // Return the end location. This is normally the location of the
790 // right curly brace which ends the block.
793 { return this->end_location_; }
795 // Add a statement to the block.
797 add_statement(Statement*);
799 // Add a statement to the front of the block.
801 add_statement_at_front(Statement*);
803 // Replace a statement in a block.
805 replace_statement(size_t index, Statement*);
807 // Add a Statement before statement number INDEX.
809 insert_statement_before(size_t index, Statement*);
811 // Add a Statement after statement number INDEX.
813 insert_statement_after(size_t index, Statement*);
815 // Set the end location of the block.
817 set_end_location(Location location)
818 { this->end_location_ = location; }
820 // Traverse the tree.
824 // Set final types for unspecified variables and constants.
828 // Return true if execution of this block may fall through to the
831 may_fall_through() const;
833 // Convert the block to the backend representation.
835 get_backend(Translate_context*);
837 // Iterate over statements.
839 typedef std::vector<Statement*>::iterator iterator;
843 { return this->statements_.begin(); }
847 { return this->statements_.end(); }
852 // Statements in the block.
853 std::vector<Statement*> statements_;
856 // Location of start of block.
857 Location start_location_;
858 // Location of end of block.
859 Location end_location_;
867 Function(Function_type* type, Function*, Block*, Location);
869 // Return the function's type.
872 { return this->type_; }
874 // Return the enclosing function if there is one.
877 { return this->enclosing_; }
879 // Set the enclosing function. This is used when building thunks
880 // for functions which call recover.
882 set_enclosing(Function* enclosing)
884 go_assert(this->enclosing_ == NULL);
885 this->enclosing_ = enclosing;
888 // The result variables.
889 typedef std::vector<Named_object*> Results;
891 // Create the result variables in the outer block.
893 create_result_variables(Gogo*);
895 // Update the named result variables when cloning a function which
898 update_result_variables();
900 // Return the result variables.
903 { return this->results_; }
907 { return this->is_sink_; }
911 { this->is_sink_ = true; }
913 // Whether the result variables have names.
915 results_are_named() const
916 { return this->results_are_named_; }
918 // Whether this method should not be included in the type
923 go_assert(this->is_method());
924 return this->nointerface_;
927 // Record that this method should not be included in the type
932 go_assert(this->is_method());
933 this->nointerface_ = true;
936 // Record that this function is a stub method created for an unnamed
939 set_is_unnamed_type_stub_method()
941 go_assert(this->is_method());
942 this->is_unnamed_type_stub_method_ = true;
945 // Add a new field to the closure variable.
947 add_closure_field(Named_object* var, Location loc)
948 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
950 // Whether this function needs a closure.
952 needs_closure() const
953 { return !this->closure_fields_.empty(); }
955 // Return the closure variable, creating it if necessary. This is
956 // passed to the function as a static chain parameter.
960 // Set the closure variable. This is used when building thunks for
961 // functions which call recover.
963 set_closure_var(Named_object* v)
965 go_assert(this->closure_var_ == NULL);
966 this->closure_var_ = v;
969 // Return the variable for a reference to field INDEX in the closure
972 enclosing_var(unsigned int index)
974 go_assert(index < this->closure_fields_.size());
975 return closure_fields_[index].first;
978 // Set the type of the closure variable if there is one.
982 // Get the block of statements associated with the function.
985 { return this->block_; }
987 // Get the location of the start of the function.
990 { return this->location_; }
992 // Return whether this function is actually a method.
996 // Add a label definition to the function.
998 add_label_definition(Gogo*, const std::string& label_name, Location);
1000 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1001 // if we should report errors for a goto from the current location
1002 // to the label location.
1004 add_label_reference(Gogo*, const std::string& label_name,
1005 Location, bool issue_goto_errors);
1007 // Warn about labels that are defined but not used.
1009 check_labels() const;
1011 // Note that a new local type has been added. Return its index.
1013 new_local_type_index()
1014 { return this->local_type_count_++; }
1016 // Whether this function calls the predeclared recover function.
1018 calls_recover() const
1019 { return this->calls_recover_; }
1021 // Record that this function calls the predeclared recover function.
1022 // This is set during the lowering pass.
1025 { this->calls_recover_ = true; }
1027 // Whether this is a recover thunk function.
1029 is_recover_thunk() const
1030 { return this->is_recover_thunk_; }
1032 // Record that this is a thunk built for a function which calls
1035 set_is_recover_thunk()
1036 { this->is_recover_thunk_ = true; }
1038 // Whether this function already has a recover thunk.
1040 has_recover_thunk() const
1041 { return this->has_recover_thunk_; }
1043 // Record that this function already has a recover thunk.
1045 set_has_recover_thunk()
1046 { this->has_recover_thunk_ = true; }
1048 // Mark the function as going into a unique section.
1050 set_in_unique_section()
1051 { this->in_unique_section_ = true; }
1053 // Swap with another function. Used only for the thunk which calls
1056 swap_for_recover(Function *);
1058 // Traverse the tree.
1060 traverse(Traverse*);
1062 // Determine types in the function.
1066 // Return an expression for the function descriptor, given the named
1067 // object for this function. This may only be called for functions
1068 // without a closure. This will be an immutable struct with one
1069 // field that points to the function's code.
1071 descriptor(Gogo*, Named_object*);
1073 // Set the descriptor for this function. This is used when a
1074 // function declaration is followed by a function definition.
1076 set_descriptor(Expression* descriptor)
1078 go_assert(this->descriptor_ == NULL);
1079 this->descriptor_ = descriptor;
1082 // Return the backend representation.
1084 get_or_make_decl(Gogo*, Named_object*);
1086 // Return the function's decl after it has been built.
1090 // Set the function decl to hold a backend representation of the function
1093 build(Gogo*, Named_object*);
1095 // Get the statement that assigns values to this function's result struct.
1097 return_value(Gogo*, Named_object*, Location) const;
1099 // Get a tree for the variable holding the defer stack.
1101 defer_stack(Location);
1103 // Export the function.
1105 export_func(Export*, const std::string& name) const;
1107 // Export a function with a type.
1109 export_func_with_type(Export*, const std::string& name,
1110 const Function_type*);
1112 // Import a function.
1114 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1115 Typed_identifier_list** pparameters,
1116 Typed_identifier_list** presults, bool* is_varargs);
1119 // Type for mapping from label names to Label objects.
1120 typedef Unordered_map(std::string, Label*) Labels;
1123 build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1125 typedef std::vector<std::pair<Named_object*,
1126 Location> > Closure_fields;
1128 // The function's type.
1129 Function_type* type_;
1130 // The enclosing function. This is NULL when there isn't one, which
1131 // is the normal case.
1132 Function* enclosing_;
1133 // The result variables, if any.
1135 // If there is a closure, this is the list of variables which appear
1136 // in the closure. This is created by the parser, and then resolved
1137 // to a real type when we lower parse trees.
1138 Closure_fields closure_fields_;
1139 // The closure variable, passed as a parameter using the static
1140 // chain parameter. Normally NULL.
1141 Named_object* closure_var_;
1142 // The outer block of statements in the function.
1144 // The source location of the start of the function.
1146 // Labels defined or referenced in the function.
1148 // The number of local types defined in this function.
1149 unsigned int local_type_count_;
1150 // The function descriptor, if any.
1151 Expression* descriptor_;
1152 // The function decl.
1154 // The defer stack variable. A pointer to this variable is used to
1155 // distinguish the defer stack for one function from another. This
1156 // is NULL unless we actually need a defer stack.
1157 Temporary_statement* defer_stack_;
1158 // True if this function is sink-named. No code is generated.
1160 // True if the result variables are named.
1161 bool results_are_named_ : 1;
1162 // True if this method should not be included in the type descriptor.
1163 bool nointerface_ : 1;
1164 // True if this function is a stub method created for an unnamed
1166 bool is_unnamed_type_stub_method_ : 1;
1167 // True if this function calls the predeclared recover function.
1168 bool calls_recover_ : 1;
1169 // True if this a thunk built for a function which calls recover.
1170 bool is_recover_thunk_ : 1;
1171 // True if this function already has a recover thunk.
1172 bool has_recover_thunk_ : 1;
1173 // True if this function should be put in a unique section. This is
1174 // turned on for field tracking.
1175 bool in_unique_section_ : 1;
1178 // A snapshot of the current binding state.
1180 class Bindings_snapshot
1183 Bindings_snapshot(const Block*, Location);
1185 // Report any errors appropriate for a goto from the current binding
1186 // state of B to this one.
1188 check_goto_from(const Block* b, Location);
1190 // Report any errors appropriate for a goto from this binding state
1191 // to the current state of B.
1193 check_goto_to(const Block* b);
1197 check_goto_block(Location, const Block*, const Block*, size_t*);
1200 check_goto_defs(Location, const Block*, size_t, size_t);
1202 // The current block.
1203 const Block* block_;
1204 // The number of names currently defined in each open block.
1205 // Element 0 is this->block_, element 1 is
1206 // this->block_->enclosing(), etc.
1207 std::vector<size_t> counts_;
1208 // The location where this snapshot was taken.
1212 // A function declaration.
1214 class Function_declaration
1217 Function_declaration(Function_type* fntype, Location location)
1218 : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1224 { return this->fntype_; }
1228 { return this->location_; }
1232 { return this->asm_name_; }
1234 // Set the assembler name.
1236 set_asm_name(const std::string& asm_name)
1237 { this->asm_name_ = asm_name; }
1239 // Return an expression for the function descriptor, given the named
1240 // object for this function. This may only be called for functions
1241 // without a closure. This will be an immutable struct with one
1242 // field that points to the function's code.
1244 descriptor(Gogo*, Named_object*);
1246 // Return true if we have created a descriptor for this declaration.
1248 has_descriptor() const
1249 { return this->descriptor_ != NULL; }
1251 // Return a backend representation.
1253 get_or_make_decl(Gogo*, Named_object*);
1255 // If there is a descriptor, build it into the backend
1258 build_backend_descriptor(Gogo*);
1260 // Export a function declaration.
1262 export_func(Export* exp, const std::string& name) const
1263 { Function::export_func_with_type(exp, name, this->fntype_); }
1266 // The type of the function.
1267 Function_type* fntype_;
1268 // The location of the declaration.
1270 // The assembler name: this is the name to use in references to the
1271 // function. This is normally empty.
1272 std::string asm_name_;
1273 // The function descriptor, if any.
1274 Expression* descriptor_;
1275 // The function decl if needed.
1284 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1285 bool is_receiver, Location);
1287 // Get the type of the variable.
1294 // Return whether the type is defined yet.
1298 // Get the initial value.
1301 { return this->init_; }
1303 // Return whether there are any preinit statements.
1305 has_pre_init() const
1306 { return this->preinit_ != NULL; }
1308 // Return the preinit statements if any.
1311 { return this->preinit_; }
1313 // Return whether this is a global variable.
1316 { return this->is_global_; }
1318 // Return whether this is a function parameter.
1320 is_parameter() const
1321 { return this->is_parameter_; }
1323 // Return whether this is the receiver parameter of a method.
1326 { return this->is_receiver_; }
1328 // Change this parameter to be a receiver. This is used when
1329 // creating the thunks created for functions which call recover.
1333 go_assert(this->is_parameter_);
1334 this->is_receiver_ = true;
1337 // Change this parameter to not be a receiver. This is used when
1338 // creating the thunks created for functions which call recover.
1340 set_is_not_receiver()
1342 go_assert(this->is_parameter_);
1343 this->is_receiver_ = false;
1346 // Return whether this is the varargs parameter of a function.
1348 is_varargs_parameter() const
1349 { return this->is_varargs_parameter_; }
1351 // Whether this variable's address is taken.
1353 is_address_taken() const
1354 { return this->is_address_taken_; }
1356 // Whether this variable should live in the heap.
1359 { return this->is_address_taken_ && !this->is_global_; }
1361 // Note that something takes the address of this variable.
1364 { this->is_address_taken_ = true; }
1366 // Return whether the address is taken but does not escape.
1368 is_non_escaping_address_taken() const
1369 { return this->is_non_escaping_address_taken_; }
1371 // Note that something takes the address of this variable such that
1372 // the address does not escape the function.
1374 set_non_escaping_address_taken()
1375 { this->is_non_escaping_address_taken_ = true; }
1377 // Get the source location of the variable's declaration.
1380 { return this->location_; }
1382 // Record that this is the varargs parameter of a function.
1384 set_is_varargs_parameter()
1386 go_assert(this->is_parameter_);
1387 this->is_varargs_parameter_ = true;
1390 // Return whether the variable has been used.
1393 { return this->is_used_; }
1395 // Mark that the variable has been used.
1398 { this->is_used_ = true; }
1400 // Clear the initial value; used for error handling.
1403 { this->init_ = NULL; }
1405 // Set the initial value; used for converting shortcuts.
1407 set_init(Expression* init)
1408 { this->init_ = init; }
1410 // Get the preinit block, a block of statements to be run before the
1411 // initialization expression.
1413 preinit_block(Gogo*);
1415 // Add a statement to be run before the initialization expression.
1416 // This is only used for global variables.
1418 add_preinit_statement(Gogo*, Statement*);
1420 // Lower the initialization expression after parsing is complete.
1422 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1424 // Flatten the initialization expression after ordering evaluations.
1426 flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
1428 // A special case: the init value is used only to determine the
1429 // type. This is used if the variable is defined using := with the
1430 // comma-ok form of a map index or a receive expression. The init
1431 // value is actually the map index expression or receive expression.
1432 // We use this because we may not know the right type at parse time.
1434 set_type_from_init_tuple()
1435 { this->type_from_init_tuple_ = true; }
1437 // Another special case: the init value is used only to determine
1438 // the type. This is used if the variable is defined using := with
1439 // a range clause. The init value is the range expression. The
1440 // type of the variable is the index type of the range expression
1441 // (i.e., the first value returned by a range).
1443 set_type_from_range_index()
1444 { this->type_from_range_index_ = true; }
1446 // Another special case: like set_type_from_range_index, but the
1447 // type is the value type of the range expression (i.e., the second
1448 // value returned by a range).
1450 set_type_from_range_value()
1451 { this->type_from_range_value_ = true; }
1453 // Another special case: the init value is used only to determine
1454 // the type. This is used if the variable is defined using := with
1455 // a case in a select statement. The init value is the channel.
1456 // The type of the variable is the channel's element type.
1458 set_type_from_chan_element()
1459 { this->type_from_chan_element_ = true; }
1461 // After we lower the select statement, we once again set the type
1462 // from the initialization expression.
1464 clear_type_from_chan_element()
1466 go_assert(this->type_from_chan_element_);
1467 this->type_from_chan_element_ = false;
1470 // Note that this variable was created for a type switch clause.
1472 set_is_type_switch_var()
1473 { this->is_type_switch_var_ = true; }
1475 // Mark the variable as going into a unique section.
1477 set_in_unique_section()
1479 go_assert(this->is_global_);
1480 this->in_unique_section_ = true;
1483 // Traverse the initializer expression.
1485 traverse_expression(Traverse*, unsigned int traverse_mask);
1487 // Determine the type of the variable if necessary.
1491 // Get the backend representation of the variable.
1493 get_backend_variable(Gogo*, Named_object*, const Package*,
1494 const std::string&);
1496 // Get the initial value of the variable. This may only
1497 // be called if has_pre_init() returns false.
1499 get_init(Gogo*, Named_object* function);
1501 // Return a series of statements which sets the value of the
1502 // variable in DECL. This should only be called is has_pre_init()
1503 // returns true. DECL may be NULL for a sink variable.
1505 get_init_block(Gogo*, Named_object* function, Bvariable* decl);
1507 // Export the variable.
1509 export_var(Export*, const std::string& name) const;
1511 // Import a variable.
1513 import_var(Import*, std::string* pname, Type** ptype);
1516 // The type of a tuple.
1518 type_from_tuple(Expression*, bool) const;
1520 // The type of a range.
1522 type_from_range(Expression*, bool, bool) const;
1524 // The element type of a channel.
1526 type_from_chan_element(Expression*, bool) const;
1528 // The variable's type. This may be NULL if the type is set from
1531 // The initial value. This may be NULL if the variable should be
1532 // initialized to the default value for the type.
1534 // Statements to run before the init statement.
1536 // Location of variable definition.
1538 // Backend representation.
1539 Bvariable* backend_;
1540 // Whether this is a global variable.
1541 bool is_global_ : 1;
1542 // Whether this is a function parameter.
1543 bool is_parameter_ : 1;
1544 // Whether this is the receiver parameter of a method.
1545 bool is_receiver_ : 1;
1546 // Whether this is the varargs parameter of a function.
1547 bool is_varargs_parameter_ : 1;
1548 // Whether this variable is ever referenced.
1550 // Whether something takes the address of this variable. For a
1551 // local variable this implies that the variable has to be on the
1553 bool is_address_taken_ : 1;
1554 // Whether something takes the address of this variable such that
1555 // the address does not escape the function.
1556 bool is_non_escaping_address_taken_ : 1;
1557 // True if we have seen this variable in a traversal.
1559 // True if we have lowered the initialization expression.
1560 bool init_is_lowered_ : 1;
1561 // True if we have flattened the initialization expression.
1562 bool init_is_flattened_ : 1;
1563 // True if init is a tuple used to set the type.
1564 bool type_from_init_tuple_ : 1;
1565 // True if init is a range clause and the type is the index type.
1566 bool type_from_range_index_ : 1;
1567 // True if init is a range clause and the type is the value type.
1568 bool type_from_range_value_ : 1;
1569 // True if init is a channel and the type is the channel's element type.
1570 bool type_from_chan_element_ : 1;
1571 // True if this is a variable created for a type switch case.
1572 bool is_type_switch_var_ : 1;
1573 // True if we have determined types.
1574 bool determined_type_ : 1;
1575 // True if this variable should be put in a unique section. This is
1576 // used for field tracking.
1577 bool in_unique_section_ : 1;
1580 // A variable which is really the name for a function return value, or
1583 class Result_variable
1586 Result_variable(Type* type, Function* function, int index,
1588 : type_(type), function_(function), index_(index), location_(location),
1589 backend_(NULL), is_address_taken_(false),
1590 is_non_escaping_address_taken_(false)
1593 // Get the type of the result variable.
1596 { return this->type_; }
1598 // Get the function that this is associated with.
1601 { return this->function_; }
1603 // Index in the list of function results.
1606 { return this->index_; }
1608 // The location of the variable definition.
1611 { return this->location_; }
1613 // Whether this variable's address is taken.
1615 is_address_taken() const
1616 { return this->is_address_taken_; }
1618 // Note that something takes the address of this variable.
1621 { this->is_address_taken_ = true; }
1623 // Return whether the address is taken but does not escape.
1625 is_non_escaping_address_taken() const
1626 { return this->is_non_escaping_address_taken_; }
1628 // Note that something takes the address of this variable such that
1629 // the address does not escape the function.
1631 set_non_escaping_address_taken()
1632 { this->is_non_escaping_address_taken_ = true; }
1634 // Whether this variable should live in the heap.
1637 { return this->is_address_taken_; }
1639 // Set the function. This is used when cloning functions which call
1642 set_function(Function* function)
1643 { this->function_ = function; }
1645 // Get the backend representation of the variable.
1647 get_backend_variable(Gogo*, Named_object*, const std::string&);
1650 // Type of result variable.
1652 // Function with which this is associated.
1653 Function* function_;
1654 // Index in list of results.
1656 // Where the result variable is defined.
1658 // Backend representation.
1659 Bvariable* backend_;
1660 // Whether something takes the address of this variable.
1661 bool is_address_taken_;
1662 // Whether something takes the address of this variable such that
1663 // the address does not escape the function.
1664 bool is_non_escaping_address_taken_;
1667 // The value we keep for a named constant. This lets us hold a type
1668 // and an expression.
1670 class Named_constant
1673 Named_constant(Type* type, Expression* expr, int iota_value,
1675 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1676 lowering_(false), is_sink_(false), bconst_(NULL)
1681 { return this->type_; }
1685 { return this->expr_; }
1689 { return this->iota_value_; }
1693 { return this->location_; }
1695 // Whether we are lowering.
1698 { return this->lowering_; }
1700 // Set that we are lowering.
1703 { this->lowering_ = true; }
1705 // We are no longer lowering.
1708 { this->lowering_ = false; }
1712 { return this->is_sink_; }
1716 { this->is_sink_ = true; }
1718 // Traverse the expression.
1720 traverse_expression(Traverse*);
1722 // Determine the type of the constant if necessary.
1726 // Indicate that we found and reported an error for this constant.
1730 // Export the constant.
1732 export_const(Export*, const std::string& name) const;
1734 // Import a constant.
1736 import_const(Import*, std::string*, Type**, Expression**);
1738 // Get the backend representation of the constant value.
1740 get_backend(Gogo*, Named_object*);
1743 // The type of the constant.
1745 // The expression for the constant.
1747 // If the predeclared constant iota is used in EXPR_, this is the
1748 // value it will have. We do this because at parse time we don't
1749 // know whether the name "iota" will refer to the predeclared
1750 // constant or to something else. We put in the right value in when
1753 // The location of the definition.
1755 // Whether we are currently lowering this constant.
1757 // Whether this constant is blank named and needs only type checking.
1759 // The backend representation of the constant value.
1760 Bexpression* bconst_;
1763 // A type declaration.
1765 class Type_declaration
1768 Type_declaration(Location location)
1769 : location_(location), in_function_(NULL), in_function_index_(0),
1770 methods_(), issued_warning_(false)
1773 // Return the location.
1776 { return this->location_; }
1778 // Return the function in which this type is declared. This will
1779 // return NULL for a type declared in global scope.
1781 in_function(unsigned int* pindex)
1783 *pindex = this->in_function_index_;
1784 return this->in_function_;
1787 // Set the function in which this type is declared.
1789 set_in_function(Named_object* f, unsigned int index)
1791 this->in_function_ = f;
1792 this->in_function_index_ = index;
1795 // Add a method to this type. This is used when methods are defined
1798 add_method(const std::string& name, Function* function);
1800 // Add a method declaration to this type.
1802 add_method_declaration(const std::string& name, Package*,
1803 Function_type* type, Location location);
1805 // Return whether any methods were defined.
1807 has_methods() const;
1809 // Return the methods.
1810 const std::vector<Named_object*>*
1812 { return &this->methods_; }
1814 // Define methods when the real type is known.
1816 define_methods(Named_type*);
1818 // This is called if we are trying to use this type. It returns
1819 // true if we should issue a warning.
1824 // The location of the type declaration.
1826 // If this type is declared in a function, a pointer back to the
1827 // function in which it is defined.
1828 Named_object* in_function_;
1829 // The index of this type in IN_FUNCTION_.
1830 unsigned int in_function_index_;
1831 // Methods defined before the type is defined.
1832 std::vector<Named_object*> methods_;
1833 // True if we have issued a warning about a use of this type
1834 // declaration when it is undefined.
1835 bool issued_warning_;
1838 // An unknown object. These are created by the parser for forward
1839 // references to names which have not been seen before. In a correct
1840 // program, these will always point to a real definition by the end of
1841 // the parse. Because they point to another Named_object, these may
1842 // only be referenced by Unknown_expression objects.
1847 Unknown_name(Location location)
1848 : location_(location), real_named_object_(NULL)
1851 // Return the location where this name was first seen.
1854 { return this->location_; }
1856 // Return the real named object that this points to, or NULL if it
1857 // was never resolved.
1859 real_named_object() const
1860 { return this->real_named_object_; }
1862 // Set the real named object that this points to.
1864 set_real_named_object(Named_object* no);
1867 // The location where this name was first seen.
1869 // The real named object when it is known.
1874 // A named object named. This is the result of a declaration. We
1875 // don't use a superclass because they all have to be handled
1883 // An uninitialized Named_object. We should never see this.
1884 NAMED_OBJECT_UNINITIALIZED,
1885 // An erroneous name. This indicates a parse error, to avoid
1886 // later errors about undefined references.
1887 NAMED_OBJECT_ERRONEOUS,
1888 // An unknown name. This is used for forward references. In a
1889 // correct program, these will all be resolved by the end of the
1891 NAMED_OBJECT_UNKNOWN,
1896 // A forward type declaration.
1897 NAMED_OBJECT_TYPE_DECLARATION,
1900 // A result variable in a function.
1901 NAMED_OBJECT_RESULT_VAR,
1902 // The blank identifier--the special variable named _.
1906 // A forward func declaration.
1907 NAMED_OBJECT_FUNC_DECLARATION,
1909 NAMED_OBJECT_PACKAGE
1912 // Return the classification.
1914 classification() const
1915 { return this->classification_; }
1920 is_erroneous() const
1921 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1925 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1929 { return this->classification_ == NAMED_OBJECT_CONST; }
1933 { return this->classification_ == NAMED_OBJECT_TYPE; }
1936 is_type_declaration() const
1937 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1941 { return this->classification_ == NAMED_OBJECT_VAR; }
1944 is_result_variable() const
1945 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1949 { return this->classification_ == NAMED_OBJECT_SINK; }
1953 { return this->classification_ == NAMED_OBJECT_FUNC; }
1956 is_function_declaration() const
1957 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1961 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1965 static Named_object*
1966 make_erroneous_name(const std::string& name)
1967 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1969 static Named_object*
1970 make_unknown_name(const std::string& name, Location);
1972 static Named_object*
1973 make_constant(const Typed_identifier&, const Package*, Expression*,
1976 static Named_object*
1977 make_type(const std::string&, const Package*, Type*, Location);
1979 static Named_object*
1980 make_type_declaration(const std::string&, const Package*, Location);
1982 static Named_object*
1983 make_variable(const std::string&, const Package*, Variable*);
1985 static Named_object*
1986 make_result_variable(const std::string&, Result_variable*);
1988 static Named_object*
1991 static Named_object*
1992 make_function(const std::string&, const Package*, Function*);
1994 static Named_object*
1995 make_function_declaration(const std::string&, const Package*, Function_type*,
1998 static Named_object*
1999 make_package(const std::string& alias, Package* package);
2006 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2007 return this->u_.unknown_value;
2011 unknown_value() const
2013 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2014 return this->u_.unknown_value;
2020 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2021 return this->u_.const_value;
2024 const Named_constant*
2027 go_assert(this->classification_ == NAMED_OBJECT_CONST);
2028 return this->u_.const_value;
2034 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2035 return this->u_.type_value;
2041 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2042 return this->u_.type_value;
2046 type_declaration_value()
2048 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2049 return this->u_.type_declaration;
2052 const Type_declaration*
2053 type_declaration_value() const
2055 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2056 return this->u_.type_declaration;
2062 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2063 return this->u_.var_value;
2069 go_assert(this->classification_ == NAMED_OBJECT_VAR);
2070 return this->u_.var_value;
2076 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2077 return this->u_.result_var_value;
2080 const Result_variable*
2081 result_var_value() const
2083 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2084 return this->u_.result_var_value;
2090 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2091 return this->u_.func_value;
2097 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2098 return this->u_.func_value;
2101 Function_declaration*
2102 func_declaration_value()
2104 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2105 return this->u_.func_declaration_value;
2108 const Function_declaration*
2109 func_declaration_value() const
2111 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2112 return this->u_.func_declaration_value;
2118 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2119 return this->u_.package_value;
2123 package_value() const
2125 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2126 return this->u_.package_value;
2131 { return this->name_; }
2133 // Return the name to use in an error message. The difference is
2134 // that if this Named_object is defined in a different package, this
2135 // will return PACKAGE.NAME.
2137 message_name() const;
2141 { return this->package_; }
2143 // Resolve an unknown value if possible. This returns the same
2144 // Named_object or a new one.
2148 Named_object* ret = this;
2149 if (this->is_unknown())
2151 Named_object* r = this->unknown_value()->real_named_object();
2161 const Named_object* ret = this;
2162 if (this->is_unknown())
2164 const Named_object* r = this->unknown_value()->real_named_object();
2171 // The location where this object was defined or referenced.
2175 // Convert a variable to the backend representation.
2177 get_backend_variable(Gogo*, Named_object* function);
2179 // Return the external identifier for this object.
2183 // Get the backend representation of this object.
2185 get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
2186 std::vector<Bfunction*>&);
2188 // Define a type declaration.
2190 set_type_value(Named_type*);
2192 // Define a function declaration.
2194 set_function_value(Function*);
2196 // Declare an unknown name as a type declaration.
2200 // Export this object.
2202 export_named_object(Export*) const;
2205 Named_object(const std::string&, const Package*, Classification);
2207 // The name of the object.
2209 // The package that this object is in. This is NULL if it is in the
2210 // file we are compiling.
2211 const Package* package_;
2212 // The type of object this is.
2213 Classification classification_;
2217 Unknown_name* unknown_value;
2218 Named_constant* const_value;
2219 Named_type* type_value;
2220 Type_declaration* type_declaration;
2221 Variable* var_value;
2222 Result_variable* result_var_value;
2223 Function* func_value;
2224 Function_declaration* func_declaration_value;
2225 Package* package_value;
2229 // A binding contour. This binds names to objects.
2234 // Type for mapping from names to objects.
2235 typedef Unordered_map(std::string, Named_object*) Contour;
2237 Bindings(Bindings* enclosing);
2239 // Add an erroneous name.
2241 add_erroneous_name(const std::string& name)
2242 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2244 // Add an unknown name.
2246 add_unknown_name(const std::string& name, Location location)
2248 return this->add_named_object(Named_object::make_unknown_name(name,
2254 add_constant(const Typed_identifier& tid, const Package* package,
2255 Expression* expr, int iota_value)
2257 return this->add_named_object(Named_object::make_constant(tid, package,
2264 add_type(const std::string& name, const Package* package, Type* type,
2267 return this->add_named_object(Named_object::make_type(name, package, type,
2271 // Add a named type. This is used for builtin types, and to add an
2272 // imported type to the global scope.
2274 add_named_type(Named_type* named_type);
2276 // Add a type declaration.
2278 add_type_declaration(const std::string& name, const Package* package,
2281 Named_object* no = Named_object::make_type_declaration(name, package,
2283 return this->add_named_object(no);
2288 add_variable(const std::string& name, const Package* package,
2291 return this->add_named_object(Named_object::make_variable(name, package,
2295 // Add a result variable.
2297 add_result_variable(const std::string& name, Result_variable* result)
2299 return this->add_named_object(Named_object::make_result_variable(name,
2305 add_function(const std::string& name, const Package*, Function* function);
2307 // Add a function declaration.
2309 add_function_declaration(const std::string& name, const Package* package,
2310 Function_type* type, Location location);
2312 // Add a package. The location is the location of the import
2315 add_package(const std::string& alias, Package* package)
2317 Named_object* no = Named_object::make_package(alias, package);
2318 return this->add_named_object(no);
2321 // Define a type which was already declared.
2323 define_type(Named_object*, Named_type*);
2325 // Add a method to the list of objects. This is not added to the
2328 add_method(Named_object*);
2330 // Add a named object to this binding.
2332 add_named_object(Named_object* no)
2333 { return this->add_named_object_to_contour(&this->bindings_, no); }
2335 // Clear all names in file scope from the bindings.
2337 clear_file_scope(Gogo*);
2339 // Look up a name in this binding contour and in any enclosing
2340 // binding contours. This returns NULL if the name is not found.
2342 lookup(const std::string&) const;
2344 // Look up a name in this binding contour without looking in any
2345 // enclosing binding contours. Returns NULL if the name is not found.
2347 lookup_local(const std::string&) const;
2351 remove_binding(Named_object*);
2353 // Mark all variables as used. This is used for some types of parse
2358 // Traverse the tree. See the Traverse class.
2360 traverse(Traverse*, bool is_global);
2362 // Iterate over definitions. This does not include things which
2363 // were only declared.
2365 typedef std::vector<Named_object*>::const_iterator
2366 const_definitions_iterator;
2368 const_definitions_iterator
2369 begin_definitions() const
2370 { return this->named_objects_.begin(); }
2372 const_definitions_iterator
2373 end_definitions() const
2374 { return this->named_objects_.end(); }
2376 // Return the number of definitions.
2378 size_definitions() const
2379 { return this->named_objects_.size(); }
2381 // Return whether there are no definitions.
2383 empty_definitions() const
2384 { return this->named_objects_.empty(); }
2386 // Iterate over declarations. This is everything that has been
2387 // declared, which includes everything which has been defined.
2389 typedef Contour::const_iterator const_declarations_iterator;
2391 const_declarations_iterator
2392 begin_declarations() const
2393 { return this->bindings_.begin(); }
2395 const_declarations_iterator
2396 end_declarations() const
2397 { return this->bindings_.end(); }
2399 // Return the number of declarations.
2401 size_declarations() const
2402 { return this->bindings_.size(); }
2404 // Return whether there are no declarations.
2406 empty_declarations() const
2407 { return this->bindings_.empty(); }
2409 // Return the first declaration.
2412 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2416 add_named_object_to_contour(Contour*, Named_object*);
2419 new_definition(Named_object*, Named_object*);
2421 // Enclosing bindings.
2422 Bindings* enclosing_;
2423 // The list of objects.
2424 std::vector<Named_object*> named_objects_;
2425 // The mapping from names to objects.
2434 Label(const std::string& name)
2435 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2436 refs_(), is_used_(false), blabel_(NULL)
2439 // Return the label's name.
2442 { return this->name_; }
2444 // Return whether the label has been defined.
2447 { return !Linemap::is_unknown_location(this->location_); }
2449 // Return whether the label has been used.
2452 { return this->is_used_; }
2454 // Record that the label is used.
2457 { this->is_used_ = true; }
2459 // Return the location of the definition.
2462 { return this->location_; }
2464 // Return the bindings snapshot.
2467 { return this->snapshot_; }
2469 // Add a snapshot of a goto which refers to this label.
2471 add_snapshot_ref(Bindings_snapshot* snapshot)
2473 go_assert(Linemap::is_unknown_location(this->location_));
2474 this->refs_.push_back(snapshot);
2477 // Return the list of snapshots of goto statements which refer to
2479 const std::vector<Bindings_snapshot*>&
2481 { return this->refs_; }
2483 // Clear the references.
2487 // Define the label at LOCATION with the given bindings snapshot.
2489 define(Location location, Bindings_snapshot* snapshot)
2491 go_assert(Linemap::is_unknown_location(this->location_)
2492 && this->snapshot_ == NULL);
2493 this->location_ = location;
2494 this->snapshot_ = snapshot;
2497 // Return the backend representation for this label.
2499 get_backend_label(Translate_context*);
2501 // Return an expression for the address of this label. This is used
2502 // to get the return address of a deferred function to see whether
2503 // the function may call recover.
2505 get_addr(Translate_context*, Location location);
2508 // The name of the label.
2510 // The location of the definition. This is 0 if the label has not
2511 // yet been defined.
2513 // A snapshot of the set of bindings defined at this label, used to
2514 // issue errors about invalid goto statements.
2515 Bindings_snapshot* snapshot_;
2516 // A list of snapshots of goto statements which refer to this label.
2517 std::vector<Bindings_snapshot*> refs_;
2518 // Whether the label has been used.
2520 // The backend representation.
2524 // An unnamed label. These are used when lowering loops.
2529 Unnamed_label(Location location)
2530 : location_(location), blabel_(NULL)
2533 // Get the location where the label is defined.
2536 { return this->location_; }
2538 // Set the location where the label is defined.
2540 set_location(Location location)
2541 { this->location_ = location; }
2543 // Return a statement which defines this label.
2545 get_definition(Translate_context*);
2547 // Return a goto to this label from LOCATION.
2549 get_goto(Translate_context*, Location location);
2552 // Return the backend representation.
2554 get_blabel(Translate_context*);
2556 // The location where the label is defined.
2558 // The backend representation of this label.
2562 // An imported package.
2567 Package(const std::string& pkgpath, Location location);
2569 // Get the package path used for all symbols exported from this
2573 { return this->pkgpath_; }
2575 // Return the package path to use for a symbol name.
2577 pkgpath_symbol() const
2578 { return this->pkgpath_symbol_; }
2580 // Return the location of the import statement.
2583 { return this->location_; }
2585 // Return whether we know the name of this package yet.
2587 has_package_name() const
2588 { return !this->package_name_.empty(); }
2590 // The name that this package uses in its package clause. This may
2591 // be different from the name in the associated Named_object if the
2592 // import statement used an alias.
2594 package_name() const
2596 go_assert(!this->package_name_.empty());
2597 return this->package_name_;
2600 // The priority of this package. The init function of packages with
2601 // lower priority must be run before the init function of packages
2602 // with higher priority.
2605 { return this->priority_; }
2607 // Set the priority.
2609 set_priority(int priority);
2611 // Return the bindings.
2614 { return this->bindings_; }
2616 // Whether some symbol from the package was used.
2619 { return this->used_; }
2621 // Note that some symbol from this package was used.
2624 { this->used_ = true; }
2626 // Clear the used field for the next file.
2629 { this->used_ = false; }
2631 // Whether this package was imported in the current file.
2634 { return this->is_imported_; }
2636 // Note that this package was imported in the current file.
2639 { this->is_imported_ = true; }
2641 // Clear the imported field for the next file.
2644 { this->is_imported_ = false; }
2646 // Whether this package was imported with a name of "_".
2648 uses_sink_alias() const
2649 { return this->uses_sink_alias_; }
2651 // Note that this package was imported with a name of "_".
2653 set_uses_sink_alias()
2654 { this->uses_sink_alias_ = true; }
2656 // Clear the sink alias field for the next file.
2658 clear_uses_sink_alias()
2659 { this->uses_sink_alias_ = false; }
2661 // Look up a name in the package. Returns NULL if the name is not
2664 lookup(const std::string& name) const
2665 { return this->bindings_->lookup(name); }
2667 // Set the name of the package.
2669 set_package_name(const std::string& name, Location);
2671 // Set the location of the package. This is used to record the most
2672 // recent import location.
2674 set_location(Location location)
2675 { this->location_ = location; }
2677 // Add a constant to the package.
2679 add_constant(const Typed_identifier& tid, Expression* expr)
2680 { return this->bindings_->add_constant(tid, this, expr, 0); }
2682 // Add a type to the package.
2684 add_type(const std::string& name, Type* type, Location location)
2685 { return this->bindings_->add_type(name, this, type, location); }
2687 // Add a type declaration to the package.
2689 add_type_declaration(const std::string& name, Location location)
2690 { return this->bindings_->add_type_declaration(name, this, location); }
2692 // Add a variable to the package.
2694 add_variable(const std::string& name, Variable* variable)
2695 { return this->bindings_->add_variable(name, this, variable); }
2697 // Add a function declaration to the package.
2699 add_function_declaration(const std::string& name, Function_type* type,
2701 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2703 // Determine types of constants.
2708 // The package path for type reflection data.
2709 std::string pkgpath_;
2710 // The package path for symbol names.
2711 std::string pkgpath_symbol_;
2712 // The name that this package uses in the package clause. This may
2713 // be the empty string if it is not yet known.
2714 std::string package_name_;
2715 // The names in this package.
2716 Bindings* bindings_;
2717 // The priority of this package. A package has a priority higher
2718 // than the priority of all of the packages that it imports. This
2719 // is used to run init functions in the right order.
2721 // The location of the import statement.
2723 // True if some name from this package was used. This is mutable
2724 // because we can use a package even if we have a const pointer to
2727 // True if this package was imported in the current file.
2729 // True if this package was imported with a name of "_".
2730 bool uses_sink_alias_;
2733 // Return codes for the traversal functions. This is not an enum
2734 // because we want to be able to declare traversal functions in other
2735 // header files without including this one.
2737 // Continue traversal as usual.
2738 const int TRAVERSE_CONTINUE = -1;
2741 const int TRAVERSE_EXIT = 0;
2743 // Continue traversal, but skip components of the current object.
2744 // E.g., if this is returned by Traverse::statement, we do not
2745 // traverse the expressions in the statement even if
2746 // traverse_expressions is set in the traverse_mask.
2747 const int TRAVERSE_SKIP_COMPONENTS = 1;
2749 // This class is used when traversing the parse tree. The caller uses
2750 // a subclass which overrides functions as desired.
2755 // These bitmasks say what to traverse.
2756 static const unsigned int traverse_variables = 0x1;
2757 static const unsigned int traverse_constants = 0x2;
2758 static const unsigned int traverse_functions = 0x4;
2759 static const unsigned int traverse_blocks = 0x8;
2760 static const unsigned int traverse_statements = 0x10;
2761 static const unsigned int traverse_expressions = 0x20;
2762 static const unsigned int traverse_types = 0x40;
2764 Traverse(unsigned int traverse_mask)
2765 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2768 virtual ~Traverse();
2770 // The bitmask of what to traverse.
2772 traverse_mask() const
2773 { return this->traverse_mask_; }
2775 // Record that we are going to traverse a type. This returns true
2776 // if the type has already been seen in this traversal. This is
2777 // required because types, unlike expressions, can form a circular
2780 remember_type(const Type*);
2782 // Record that we are going to see an expression. This returns true
2783 // if the expression has already been seen in this traversal. This
2784 // is only needed for cases where multiple expressions can point to
2787 remember_expression(const Expression*);
2789 // These functions return one of the TRAVERSE codes defined above.
2791 // If traverse_variables is set in the mask, this is called for
2792 // every variable in the tree.
2794 variable(Named_object*);
2796 // If traverse_constants is set in the mask, this is called for
2797 // every named constant in the tree. The bool parameter is true for
2798 // a global constant.
2800 constant(Named_object*, bool);
2802 // If traverse_functions is set in the mask, this is called for
2803 // every function in the tree.
2805 function(Named_object*);
2807 // If traverse_blocks is set in the mask, this is called for every
2808 // block in the tree.
2812 // If traverse_statements is set in the mask, this is called for
2813 // every statement in the tree.
2815 statement(Block*, size_t* index, Statement*);
2817 // If traverse_expressions is set in the mask, this is called for
2818 // every expression in the tree.
2820 expression(Expression**);
2822 // If traverse_types is set in the mask, this is called for every
2823 // type in the tree.
2828 // A hash table for types we have seen during this traversal. Note
2829 // that this uses the default hash functions for pointers rather
2830 // than Type_hash_identical and Type_identical. This is because for
2831 // traversal we care about seeing a specific type structure. If
2832 // there are two separate instances of identical types, we want to
2834 typedef Unordered_set(const Type*) Types_seen;
2836 typedef Unordered_set(const Expression*) Expressions_seen;
2838 // Bitmask of what sort of objects to traverse.
2839 unsigned int traverse_mask_;
2840 // Types which have been seen in this traversal.
2841 Types_seen* types_seen_;
2842 // Expressions which have been seen in this traversal.
2843 Expressions_seen* expressions_seen_;
2846 // A class which makes it easier to insert new statements before the
2847 // current statement during a traversal.
2849 class Statement_inserter
2852 // Empty constructor.
2853 Statement_inserter()
2854 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2857 // Constructor for a statement in a block.
2858 Statement_inserter(Block* block, size_t *pindex)
2859 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2862 // Constructor for a global variable.
2863 Statement_inserter(Gogo* gogo, Variable* var)
2864 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2865 { go_assert(var->is_global()); }
2867 // We use the default copy constructor and assignment operator.
2869 // Insert S before the statement we are traversing, or before the
2870 // initialization expression of a global variable.
2872 insert(Statement* s);
2875 // The block that the statement is in.
2877 // The index of the statement that we are traversing.
2879 // The IR, needed when looking at an initializer expression for a
2882 // The global variable, when looking at an initializer expression.
2886 // When translating the gogo IR into the backend data structure, this
2887 // is the context we pass down the blocks and statements.
2889 class Translate_context
2892 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2894 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2895 block_(block), bblock_(bblock), is_const_(false)
2902 { return this->gogo_; }
2906 { return this->backend_; }
2910 { return this->function_; }
2914 { return this->block_; }
2918 { return this->bblock_; }
2922 { return this->is_const_; }
2924 // Make a constant context.
2927 { this->is_const_ = true; }
2930 // The IR for the entire compilation unit.
2932 // The generator for the backend data structures.
2934 // The function we are currently translating. NULL if not in a
2935 // function, e.g., the initializer of a global variable.
2936 Named_object* function_;
2937 // The block we are currently translating. NULL if not in a
2940 // The backend representation of the current block. NULL if block_
2943 // Whether this is being evaluated in a constant context. This is
2944 // used for type descriptor initializers.
2948 // Runtime error codes. These must match the values in
2949 // libgo/runtime/go-runtime-error.c.
2951 // Slice index out of bounds: negative or larger than the length of
2953 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2955 // Array index out of bounds.
2956 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2958 // String index out of bounds.
2959 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2961 // Slice slice out of bounds: negative or larger than the length of
2962 // the slice or high bound less than low bound.
2963 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2965 // Array slice out of bounds.
2966 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2968 // String slice out of bounds.
2969 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2971 // Dereference of nil pointer. This is used when there is a
2972 // dereference of a pointer to a very large struct or array, to ensure
2973 // that a gigantic array is not used a proxy to access random memory
2975 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2977 // Slice length or capacity out of bounds in make: negative or
2978 // overflow or length greater than capacity.
2979 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2981 // Map capacity out of bounds in make: negative or overflow.
2982 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2984 // Channel capacity out of bounds in make: negative or overflow.
2985 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2987 // Division by zero.
2988 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
2990 // This is used by some of the langhooks.
2991 extern Gogo* go_get_gogo();
2993 // Whether we have seen any errors. FIXME: Replace with a backend
2995 extern bool saw_errors();
2997 #endif // !defined(GO_GOGO_H)