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*, Named_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 // Whether this function calls the predeclared recover function.
968 calls_recover() const
969 { return this->calls_recover_; }
971 // Record that this function calls the predeclared recover function.
972 // This is set during the lowering pass.
975 { this->calls_recover_ = true; }
977 // Whether this is a recover thunk function.
979 is_recover_thunk() const
980 { return this->is_recover_thunk_; }
982 // Record that this is a thunk built for a function which calls
985 set_is_recover_thunk()
986 { this->is_recover_thunk_ = true; }
988 // Whether this function already has a recover thunk.
990 has_recover_thunk() const
991 { return this->has_recover_thunk_; }
993 // Record that this function already has a recover thunk.
995 set_has_recover_thunk()
996 { this->has_recover_thunk_ = true; }
998 // Swap with another function. Used only for the thunk which calls
1001 swap_for_recover(Function *);
1003 // Traverse the tree.
1005 traverse(Traverse*);
1007 // Determine types in the function.
1011 // Return the function's decl given an identifier.
1013 get_or_make_decl(Gogo*, Named_object*, tree id);
1015 // Return the function's decl after it has been built.
1019 go_assert(this->fndecl_ != NULL);
1020 return this->fndecl_;
1023 // Set the function decl to hold a tree of the function code.
1025 build_tree(Gogo*, Named_object*);
1027 // Get the value to return when not explicitly specified. May also
1028 // add statements to execute first to STMT_LIST.
1030 return_value(Gogo*, Named_object*, Location, tree* stmt_list) const;
1032 // Get a tree for the variable holding the defer stack.
1034 defer_stack(Location);
1036 // Export the function.
1038 export_func(Export*, const std::string& name) const;
1040 // Export a function with a type.
1042 export_func_with_type(Export*, const std::string& name,
1043 const Function_type*);
1045 // Import a function.
1047 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1048 Typed_identifier_list** pparameters,
1049 Typed_identifier_list** presults, bool* is_varargs);
1052 // Type for mapping from label names to Label objects.
1053 typedef Unordered_map(std::string, Label*) Labels;
1056 make_receiver_parm_decl(Gogo*, Named_object*, tree);
1059 copy_parm_to_heap(Gogo*, Named_object*, tree);
1062 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1064 typedef std::vector<std::pair<Named_object*,
1065 Location> > Closure_fields;
1067 // The function's type.
1068 Function_type* type_;
1069 // The enclosing function. This is NULL when there isn't one, which
1070 // is the normal case.
1071 Function* enclosing_;
1072 // The result variables, if any.
1074 // If there is a closure, this is the list of variables which appear
1075 // in the closure. This is created by the parser, and then resolved
1076 // to a real type when we lower parse trees.
1077 Closure_fields closure_fields_;
1078 // The closure variable, passed as a parameter using the static
1079 // chain parameter. Normally NULL.
1080 Named_object* closure_var_;
1081 // The outer block of statements in the function.
1083 // The source location of the start of the function.
1085 // Labels defined or referenced in the function.
1087 // The function decl.
1089 // The defer stack variable. A pointer to this variable is used to
1090 // distinguish the defer stack for one function from another. This
1091 // is NULL unless we actually need a defer stack.
1092 Temporary_statement* defer_stack_;
1093 // True if the result variables are named.
1094 bool results_are_named_;
1095 // True if this function calls the predeclared recover function.
1096 bool calls_recover_;
1097 // True if this a thunk built for a function which calls recover.
1098 bool is_recover_thunk_;
1099 // True if this function already has a recover thunk.
1100 bool has_recover_thunk_;
1103 // A snapshot of the current binding state.
1105 class Bindings_snapshot
1108 Bindings_snapshot(const Block*, Location);
1110 // Report any errors appropriate for a goto from the current binding
1111 // state of B to this one.
1113 check_goto_from(const Block* b, Location);
1115 // Report any errors appropriate for a goto from this binding state
1116 // to the current state of B.
1118 check_goto_to(const Block* b);
1122 check_goto_block(Location, const Block*, const Block*, size_t*);
1125 check_goto_defs(Location, const Block*, size_t, size_t);
1127 // The current block.
1128 const Block* block_;
1129 // The number of names currently defined in each open block.
1130 // Element 0 is this->block_, element 1 is
1131 // this->block_->enclosing(), etc.
1132 std::vector<size_t> counts_;
1133 // The location where this snapshot was taken.
1137 // A function declaration.
1139 class Function_declaration
1142 Function_declaration(Function_type* fntype, Location location)
1143 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1148 { return this->fntype_; }
1152 { return this->location_; }
1156 { return this->asm_name_; }
1158 // Set the assembler name.
1160 set_asm_name(const std::string& asm_name)
1161 { this->asm_name_ = asm_name; }
1163 // Return a decl for the function given an identifier.
1165 get_or_make_decl(Gogo*, Named_object*, tree id);
1167 // Export a function declaration.
1169 export_func(Export* exp, const std::string& name) const
1170 { Function::export_func_with_type(exp, name, this->fntype_); }
1173 // The type of the function.
1174 Function_type* fntype_;
1175 // The location of the declaration.
1177 // The assembler name: this is the name to use in references to the
1178 // function. This is normally empty.
1179 std::string asm_name_;
1180 // The function decl if needed.
1189 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1190 bool is_receiver, Location);
1192 // Get the type of the variable.
1199 // Return whether the type is defined yet.
1203 // Get the initial value.
1206 { return this->init_; }
1208 // Return whether there are any preinit statements.
1210 has_pre_init() const
1211 { return this->preinit_ != NULL; }
1213 // Return the preinit statements if any.
1216 { return this->preinit_; }
1218 // Return whether this is a global variable.
1221 { return this->is_global_; }
1223 // Return whether this is a function parameter.
1225 is_parameter() const
1226 { return this->is_parameter_; }
1228 // Return whether this is the receiver parameter of a method.
1231 { return this->is_receiver_; }
1233 // Change this parameter to be a receiver. This is used when
1234 // creating the thunks created for functions which call recover.
1238 go_assert(this->is_parameter_);
1239 this->is_receiver_ = true;
1242 // Change this parameter to not be a receiver. This is used when
1243 // creating the thunks created for functions which call recover.
1245 set_is_not_receiver()
1247 go_assert(this->is_parameter_);
1248 this->is_receiver_ = false;
1251 // Return whether this is the varargs parameter of a function.
1253 is_varargs_parameter() const
1254 { return this->is_varargs_parameter_; }
1256 // Whether this variable's address is taken.
1258 is_address_taken() const
1259 { return this->is_address_taken_; }
1261 // Whether this variable should live in the heap.
1264 { return this->is_address_taken_ && !this->is_global_; }
1266 // Note that something takes the address of this variable.
1269 { this->is_address_taken_ = true; }
1271 // Return whether the address is taken but does not escape.
1273 is_non_escaping_address_taken() const
1274 { return this->is_non_escaping_address_taken_; }
1276 // Note that something takes the address of this variable such that
1277 // the address does not escape the function.
1279 set_non_escaping_address_taken()
1280 { this->is_non_escaping_address_taken_ = true; }
1282 // Get the source location of the variable's declaration.
1285 { return this->location_; }
1287 // Record that this is the varargs parameter of a function.
1289 set_is_varargs_parameter()
1291 go_assert(this->is_parameter_);
1292 this->is_varargs_parameter_ = true;
1295 // Return whether the variable has been used.
1298 { return this->is_used_; }
1300 // Mark that the variable has been used.
1303 { this->is_used_ = true; }
1305 // Clear the initial value; used for error handling.
1308 { this->init_ = NULL; }
1310 // Set the initial value; used for converting shortcuts.
1312 set_init(Expression* init)
1313 { this->init_ = init; }
1315 // Get the preinit block, a block of statements to be run before the
1316 // initialization expression.
1318 preinit_block(Gogo*);
1320 // Add a statement to be run before the initialization expression.
1321 // This is only used for global variables.
1323 add_preinit_statement(Gogo*, Statement*);
1325 // Lower the initialization expression after parsing is complete.
1327 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1329 // A special case: the init value is used only to determine the
1330 // type. This is used if the variable is defined using := with the
1331 // comma-ok form of a map index or a receive expression. The init
1332 // value is actually the map index expression or receive expression.
1333 // We use this because we may not know the right type at parse time.
1335 set_type_from_init_tuple()
1336 { this->type_from_init_tuple_ = true; }
1338 // Another special case: the init value is used only to determine
1339 // the type. This is used if the variable is defined using := with
1340 // a range clause. The init value is the range expression. The
1341 // type of the variable is the index type of the range expression
1342 // (i.e., the first value returned by a range).
1344 set_type_from_range_index()
1345 { this->type_from_range_index_ = true; }
1347 // Another special case: like set_type_from_range_index, but the
1348 // type is the value type of the range expression (i.e., the second
1349 // value returned by a range).
1351 set_type_from_range_value()
1352 { this->type_from_range_value_ = true; }
1354 // Another special case: the init value is used only to determine
1355 // the type. This is used if the variable is defined using := with
1356 // a case in a select statement. The init value is the channel.
1357 // The type of the variable is the channel's element type.
1359 set_type_from_chan_element()
1360 { this->type_from_chan_element_ = true; }
1362 // After we lower the select statement, we once again set the type
1363 // from the initialization expression.
1365 clear_type_from_chan_element()
1367 go_assert(this->type_from_chan_element_);
1368 this->type_from_chan_element_ = false;
1371 // Note that this variable was created for a type switch clause.
1373 set_is_type_switch_var()
1374 { this->is_type_switch_var_ = true; }
1376 // Traverse the initializer expression.
1378 traverse_expression(Traverse*, unsigned int traverse_mask);
1380 // Determine the type of the variable if necessary.
1384 // Get the backend representation of the variable.
1386 get_backend_variable(Gogo*, Named_object*, const Package*,
1387 const std::string&);
1389 // Get the initial value of the variable as a tree. This may only
1390 // be called if has_pre_init() returns false.
1392 get_init_tree(Gogo*, Named_object* function);
1394 // Return a series of statements which sets the value of the
1395 // variable in DECL. This should only be called is has_pre_init()
1396 // returns true. DECL may be NULL for a sink variable.
1398 get_init_block(Gogo*, Named_object* function, tree decl);
1400 // Export the variable.
1402 export_var(Export*, const std::string& name) const;
1404 // Import a variable.
1406 import_var(Import*, std::string* pname, Type** ptype);
1409 // The type of a tuple.
1411 type_from_tuple(Expression*, bool) const;
1413 // The type of a range.
1415 type_from_range(Expression*, bool, bool) const;
1417 // The element type of a channel.
1419 type_from_chan_element(Expression*, bool) const;
1421 // The variable's type. This may be NULL if the type is set from
1424 // The initial value. This may be NULL if the variable should be
1425 // initialized to the default value for the type.
1427 // Statements to run before the init statement.
1429 // Location of variable definition.
1431 // Backend representation.
1432 Bvariable* backend_;
1433 // Whether this is a global variable.
1434 bool is_global_ : 1;
1435 // Whether this is a function parameter.
1436 bool is_parameter_ : 1;
1437 // Whether this is the receiver parameter of a method.
1438 bool is_receiver_ : 1;
1439 // Whether this is the varargs parameter of a function.
1440 bool is_varargs_parameter_ : 1;
1441 // Whether this variable is ever referenced.
1443 // Whether something takes the address of this variable. For a
1444 // local variable this implies that the variable has to be on the
1446 bool is_address_taken_ : 1;
1447 // Whether something takes the address of this variable such that
1448 // the address does not escape the function.
1449 bool is_non_escaping_address_taken_ : 1;
1450 // True if we have seen this variable in a traversal.
1452 // True if we have lowered the initialization expression.
1453 bool init_is_lowered_ : 1;
1454 // True if init is a tuple used to set the type.
1455 bool type_from_init_tuple_ : 1;
1456 // True if init is a range clause and the type is the index type.
1457 bool type_from_range_index_ : 1;
1458 // True if init is a range clause and the type is the value type.
1459 bool type_from_range_value_ : 1;
1460 // True if init is a channel and the type is the channel's element type.
1461 bool type_from_chan_element_ : 1;
1462 // True if this is a variable created for a type switch case.
1463 bool is_type_switch_var_ : 1;
1464 // True if we have determined types.
1465 bool determined_type_ : 1;
1468 // A variable which is really the name for a function return value, or
1471 class Result_variable
1474 Result_variable(Type* type, Function* function, int index,
1476 : type_(type), function_(function), index_(index), location_(location),
1477 backend_(NULL), is_address_taken_(false),
1478 is_non_escaping_address_taken_(false)
1481 // Get the type of the result variable.
1484 { return this->type_; }
1486 // Get the function that this is associated with.
1489 { return this->function_; }
1491 // Index in the list of function results.
1494 { return this->index_; }
1496 // The location of the variable definition.
1499 { return this->location_; }
1501 // Whether this variable's address is taken.
1503 is_address_taken() const
1504 { return this->is_address_taken_; }
1506 // Note that something takes the address of this variable.
1509 { this->is_address_taken_ = true; }
1511 // Return whether the address is taken but does not escape.
1513 is_non_escaping_address_taken() const
1514 { return this->is_non_escaping_address_taken_; }
1516 // Note that something takes the address of this variable such that
1517 // the address does not escape the function.
1519 set_non_escaping_address_taken()
1520 { this->is_non_escaping_address_taken_ = true; }
1522 // Whether this variable should live in the heap.
1525 { return this->is_address_taken_; }
1527 // Set the function. This is used when cloning functions which call
1530 set_function(Function* function)
1531 { this->function_ = function; }
1533 // Get the backend representation of the variable.
1535 get_backend_variable(Gogo*, Named_object*, const std::string&);
1538 // Type of result variable.
1540 // Function with which this is associated.
1541 Function* function_;
1542 // Index in list of results.
1544 // Where the result variable is defined.
1546 // Backend representation.
1547 Bvariable* backend_;
1548 // Whether something takes the address of this variable.
1549 bool is_address_taken_;
1550 // Whether something takes the address of this variable such that
1551 // the address does not escape the function.
1552 bool is_non_escaping_address_taken_;
1555 // The value we keep for a named constant. This lets us hold a type
1556 // and an expression.
1558 class Named_constant
1561 Named_constant(Type* type, Expression* expr, int iota_value,
1563 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1569 { return this->type_; }
1573 { return this->expr_; }
1577 { return this->iota_value_; }
1581 { return this->location_; }
1583 // Whether we are lowering.
1586 { return this->lowering_; }
1588 // Set that we are lowering.
1591 { this->lowering_ = true; }
1593 // We are no longer lowering.
1596 { this->lowering_ = false; }
1598 // Traverse the expression.
1600 traverse_expression(Traverse*);
1602 // Determine the type of the constant if necessary.
1606 // Indicate that we found and reported an error for this constant.
1610 // Export the constant.
1612 export_const(Export*, const std::string& name) const;
1614 // Import a constant.
1616 import_const(Import*, std::string*, Type**, Expression**);
1619 // The type of the constant.
1621 // The expression for the constant.
1623 // If the predeclared constant iota is used in EXPR_, this is the
1624 // value it will have. We do this because at parse time we don't
1625 // know whether the name "iota" will refer to the predeclared
1626 // constant or to something else. We put in the right value in when
1629 // The location of the definition.
1631 // Whether we are currently lowering this constant.
1635 // A type declaration.
1637 class Type_declaration
1640 Type_declaration(Location location)
1641 : location_(location), in_function_(NULL), methods_(),
1642 issued_warning_(false)
1645 // Return the location.
1648 { return this->location_; }
1650 // Return the function in which this type is declared. This will
1651 // return NULL for a type declared in global scope.
1654 { return this->in_function_; }
1656 // Set the function in which this type is declared.
1658 set_in_function(Named_object* f)
1659 { this->in_function_ = f; }
1661 // Add a method to this type. This is used when methods are defined
1664 add_method(const std::string& name, Function* function);
1666 // Add a method declaration to this type.
1668 add_method_declaration(const std::string& name, Package*,
1669 Function_type* type, Location location);
1671 // Return whether any methods were defined.
1673 has_methods() const;
1675 // Define methods when the real type is known.
1677 define_methods(Named_type*);
1679 // This is called if we are trying to use this type. It returns
1680 // true if we should issue a warning.
1685 typedef std::vector<Named_object*> Methods;
1687 // The location of the type declaration.
1689 // If this type is declared in a function, a pointer back to the
1690 // function in which it is defined.
1691 Named_object* in_function_;
1692 // Methods defined before the type is defined.
1694 // True if we have issued a warning about a use of this type
1695 // declaration when it is undefined.
1696 bool issued_warning_;
1699 // An unknown object. These are created by the parser for forward
1700 // references to names which have not been seen before. In a correct
1701 // program, these will always point to a real definition by the end of
1702 // the parse. Because they point to another Named_object, these may
1703 // only be referenced by Unknown_expression objects.
1708 Unknown_name(Location location)
1709 : location_(location), real_named_object_(NULL)
1712 // Return the location where this name was first seen.
1715 { return this->location_; }
1717 // Return the real named object that this points to, or NULL if it
1718 // was never resolved.
1720 real_named_object() const
1721 { return this->real_named_object_; }
1723 // Set the real named object that this points to.
1725 set_real_named_object(Named_object* no);
1728 // The location where this name was first seen.
1730 // The real named object when it is known.
1735 // A named object named. This is the result of a declaration. We
1736 // don't use a superclass because they all have to be handled
1744 // An uninitialized Named_object. We should never see this.
1745 NAMED_OBJECT_UNINITIALIZED,
1746 // An erroneous name. This indicates a parse error, to avoid
1747 // later errors about undefined references.
1748 NAMED_OBJECT_ERRONEOUS,
1749 // An unknown name. This is used for forward references. In a
1750 // correct program, these will all be resolved by the end of the
1752 NAMED_OBJECT_UNKNOWN,
1757 // A forward type declaration.
1758 NAMED_OBJECT_TYPE_DECLARATION,
1761 // A result variable in a function.
1762 NAMED_OBJECT_RESULT_VAR,
1763 // The blank identifier--the special variable named _.
1767 // A forward func declaration.
1768 NAMED_OBJECT_FUNC_DECLARATION,
1770 NAMED_OBJECT_PACKAGE
1773 // Return the classification.
1775 classification() const
1776 { return this->classification_; }
1781 is_erroneous() const
1782 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1786 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1790 { return this->classification_ == NAMED_OBJECT_CONST; }
1794 { return this->classification_ == NAMED_OBJECT_TYPE; }
1797 is_type_declaration() const
1798 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1802 { return this->classification_ == NAMED_OBJECT_VAR; }
1805 is_result_variable() const
1806 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1810 { return this->classification_ == NAMED_OBJECT_SINK; }
1814 { return this->classification_ == NAMED_OBJECT_FUNC; }
1817 is_function_declaration() const
1818 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1822 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1826 static Named_object*
1827 make_erroneous_name(const std::string& name)
1828 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1830 static Named_object*
1831 make_unknown_name(const std::string& name, Location);
1833 static Named_object*
1834 make_constant(const Typed_identifier&, const Package*, Expression*,
1837 static Named_object*
1838 make_type(const std::string&, const Package*, Type*, Location);
1840 static Named_object*
1841 make_type_declaration(const std::string&, const Package*, Location);
1843 static Named_object*
1844 make_variable(const std::string&, const Package*, Variable*);
1846 static Named_object*
1847 make_result_variable(const std::string&, Result_variable*);
1849 static Named_object*
1852 static Named_object*
1853 make_function(const std::string&, const Package*, Function*);
1855 static Named_object*
1856 make_function_declaration(const std::string&, const Package*, Function_type*,
1859 static Named_object*
1860 make_package(const std::string& alias, Package* package);
1867 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1868 return this->u_.unknown_value;
1872 unknown_value() const
1874 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1875 return this->u_.unknown_value;
1881 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1882 return this->u_.const_value;
1885 const Named_constant*
1888 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1889 return this->u_.const_value;
1895 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1896 return this->u_.type_value;
1902 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1903 return this->u_.type_value;
1907 type_declaration_value()
1909 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1910 return this->u_.type_declaration;
1913 const Type_declaration*
1914 type_declaration_value() const
1916 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1917 return this->u_.type_declaration;
1923 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1924 return this->u_.var_value;
1930 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1931 return this->u_.var_value;
1937 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1938 return this->u_.result_var_value;
1941 const Result_variable*
1942 result_var_value() const
1944 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1945 return this->u_.result_var_value;
1951 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1952 return this->u_.func_value;
1958 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1959 return this->u_.func_value;
1962 Function_declaration*
1963 func_declaration_value()
1965 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1966 return this->u_.func_declaration_value;
1969 const Function_declaration*
1970 func_declaration_value() const
1972 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1973 return this->u_.func_declaration_value;
1979 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1980 return this->u_.package_value;
1984 package_value() const
1986 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1987 return this->u_.package_value;
1992 { return this->name_; }
1994 // Return the name to use in an error message. The difference is
1995 // that if this Named_object is defined in a different package, this
1996 // will return PACKAGE.NAME.
1998 message_name() const;
2002 { return this->package_; }
2004 // Resolve an unknown value if possible. This returns the same
2005 // Named_object or a new one.
2009 Named_object* ret = this;
2010 if (this->is_unknown())
2012 Named_object* r = this->unknown_value()->real_named_object();
2022 const Named_object* ret = this;
2023 if (this->is_unknown())
2025 const Named_object* r = this->unknown_value()->real_named_object();
2032 // The location where this object was defined or referenced.
2036 // Convert a variable to the backend representation.
2038 get_backend_variable(Gogo*, Named_object* function);
2040 // Return a tree for the external identifier for this object.
2044 // Return a tree representing this object.
2046 get_tree(Gogo*, Named_object* function);
2048 // Define a type declaration.
2050 set_type_value(Named_type*);
2052 // Define a function declaration.
2054 set_function_value(Function*);
2056 // Declare an unknown name as a type declaration.
2060 // Export this object.
2062 export_named_object(Export*) const;
2065 Named_object(const std::string&, const Package*, Classification);
2067 // The name of the object.
2069 // The package that this object is in. This is NULL if it is in the
2070 // file we are compiling.
2071 const Package* package_;
2072 // The type of object this is.
2073 Classification classification_;
2077 Unknown_name* unknown_value;
2078 Named_constant* const_value;
2079 Named_type* type_value;
2080 Type_declaration* type_declaration;
2081 Variable* var_value;
2082 Result_variable* result_var_value;
2083 Function* func_value;
2084 Function_declaration* func_declaration_value;
2085 Package* package_value;
2087 // The DECL tree for this object if we have already converted it.
2091 // A binding contour. This binds names to objects.
2096 // Type for mapping from names to objects.
2097 typedef Unordered_map(std::string, Named_object*) Contour;
2099 Bindings(Bindings* enclosing);
2101 // Add an erroneous name.
2103 add_erroneous_name(const std::string& name)
2104 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2106 // Add an unknown name.
2108 add_unknown_name(const std::string& name, Location location)
2110 return this->add_named_object(Named_object::make_unknown_name(name,
2116 add_constant(const Typed_identifier& tid, const Package* package,
2117 Expression* expr, int iota_value)
2119 return this->add_named_object(Named_object::make_constant(tid, package,
2126 add_type(const std::string& name, const Package* package, Type* type,
2129 return this->add_named_object(Named_object::make_type(name, package, type,
2133 // Add a named type. This is used for builtin types, and to add an
2134 // imported type to the global scope.
2136 add_named_type(Named_type* named_type);
2138 // Add a type declaration.
2140 add_type_declaration(const std::string& name, const Package* package,
2143 Named_object* no = Named_object::make_type_declaration(name, package,
2145 return this->add_named_object(no);
2150 add_variable(const std::string& name, const Package* package,
2153 return this->add_named_object(Named_object::make_variable(name, package,
2157 // Add a result variable.
2159 add_result_variable(const std::string& name, Result_variable* result)
2161 return this->add_named_object(Named_object::make_result_variable(name,
2167 add_function(const std::string& name, const Package*, Function* function);
2169 // Add a function declaration.
2171 add_function_declaration(const std::string& name, const Package* package,
2172 Function_type* type, Location location);
2174 // Add a package. The location is the location of the import
2177 add_package(const std::string& alias, Package* package)
2179 Named_object* no = Named_object::make_package(alias, package);
2180 return this->add_named_object(no);
2183 // Define a type which was already declared.
2185 define_type(Named_object*, Named_type*);
2187 // Add a method to the list of objects. This is not added to the
2190 add_method(Named_object*);
2192 // Add a named object to this binding.
2194 add_named_object(Named_object* no)
2195 { return this->add_named_object_to_contour(&this->bindings_, no); }
2197 // Clear all names in file scope from the bindings.
2201 // Look up a name in this binding contour and in any enclosing
2202 // binding contours. This returns NULL if the name is not found.
2204 lookup(const std::string&) const;
2206 // Look up a name in this binding contour without looking in any
2207 // enclosing binding contours. Returns NULL if the name is not found.
2209 lookup_local(const std::string&) const;
2213 remove_binding(Named_object*);
2215 // Mark all variables as used. This is used for some types of parse
2220 // Traverse the tree. See the Traverse class.
2222 traverse(Traverse*, bool is_global);
2224 // Iterate over definitions. This does not include things which
2225 // were only declared.
2227 typedef std::vector<Named_object*>::const_iterator
2228 const_definitions_iterator;
2230 const_definitions_iterator
2231 begin_definitions() const
2232 { return this->named_objects_.begin(); }
2234 const_definitions_iterator
2235 end_definitions() const
2236 { return this->named_objects_.end(); }
2238 // Return the number of definitions.
2240 size_definitions() const
2241 { return this->named_objects_.size(); }
2243 // Return whether there are no definitions.
2245 empty_definitions() const
2246 { return this->named_objects_.empty(); }
2248 // Iterate over declarations. This is everything that has been
2249 // declared, which includes everything which has been defined.
2251 typedef Contour::const_iterator const_declarations_iterator;
2253 const_declarations_iterator
2254 begin_declarations() const
2255 { return this->bindings_.begin(); }
2257 const_declarations_iterator
2258 end_declarations() const
2259 { return this->bindings_.end(); }
2261 // Return the number of declarations.
2263 size_declarations() const
2264 { return this->bindings_.size(); }
2266 // Return whether there are no declarations.
2268 empty_declarations() const
2269 { return this->bindings_.empty(); }
2271 // Return the first declaration.
2274 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2278 add_named_object_to_contour(Contour*, Named_object*);
2281 new_definition(Named_object*, Named_object*);
2283 // Enclosing bindings.
2284 Bindings* enclosing_;
2285 // The list of objects.
2286 std::vector<Named_object*> named_objects_;
2287 // The mapping from names to objects.
2296 Label(const std::string& name)
2297 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2298 refs_(), is_used_(false), blabel_(NULL)
2301 // Return the label's name.
2304 { return this->name_; }
2306 // Return whether the label has been defined.
2309 { return !Linemap::is_unknown_location(this->location_); }
2311 // Return whether the label has been used.
2314 { return this->is_used_; }
2316 // Record that the label is used.
2319 { this->is_used_ = true; }
2321 // Return the location of the definition.
2324 { return this->location_; }
2326 // Return the bindings snapshot.
2329 { return this->snapshot_; }
2331 // Add a snapshot of a goto which refers to this label.
2333 add_snapshot_ref(Bindings_snapshot* snapshot)
2335 go_assert(Linemap::is_unknown_location(this->location_));
2336 this->refs_.push_back(snapshot);
2339 // Return the list of snapshots of goto statements which refer to
2341 const std::vector<Bindings_snapshot*>&
2343 { return this->refs_; }
2345 // Clear the references.
2349 // Define the label at LOCATION with the given bindings snapshot.
2351 define(Location location, Bindings_snapshot* snapshot)
2353 go_assert(Linemap::is_unknown_location(this->location_)
2354 && this->snapshot_ == NULL);
2355 this->location_ = location;
2356 this->snapshot_ = snapshot;
2359 // Return the backend representation for this label.
2361 get_backend_label(Translate_context*);
2363 // Return an expression for the address of this label. This is used
2364 // to get the return address of a deferred function to see whether
2365 // the function may call recover.
2367 get_addr(Translate_context*, Location location);
2370 // The name of the label.
2372 // The location of the definition. This is 0 if the label has not
2373 // yet been defined.
2375 // A snapshot of the set of bindings defined at this label, used to
2376 // issue errors about invalid goto statements.
2377 Bindings_snapshot* snapshot_;
2378 // A list of snapshots of goto statements which refer to this label.
2379 std::vector<Bindings_snapshot*> refs_;
2380 // Whether the label has been used.
2382 // The backend representation.
2386 // An unnamed label. These are used when lowering loops.
2391 Unnamed_label(Location location)
2392 : location_(location), blabel_(NULL)
2395 // Get the location where the label is defined.
2398 { return this->location_; }
2400 // Set the location where the label is defined.
2402 set_location(Location location)
2403 { this->location_ = location; }
2405 // Return a statement which defines this label.
2407 get_definition(Translate_context*);
2409 // Return a goto to this label from LOCATION.
2411 get_goto(Translate_context*, Location location);
2414 // Return the backend representation.
2416 get_blabel(Translate_context*);
2418 // The location where the label is defined.
2420 // The backend representation of this label.
2424 // An imported package.
2429 Package(const std::string& pkgpath, Location location);
2431 // Get the package path used for all symbols exported from this
2435 { return this->pkgpath_; }
2437 // Return the package path to use for a symbol name.
2439 pkgpath_symbol() const
2440 { return this->pkgpath_symbol_; }
2442 // Return the location of the import statement.
2445 { return this->location_; }
2447 // Return whether we know the name of this package yet.
2449 has_package_name() const
2450 { return !this->package_name_.empty(); }
2452 // The name that this package uses in its package clause. This may
2453 // be different from the name in the associated Named_object if the
2454 // import statement used an alias.
2456 package_name() const
2458 go_assert(!this->package_name_.empty());
2459 return this->package_name_;
2462 // The priority of this package. The init function of packages with
2463 // lower priority must be run before the init function of packages
2464 // with higher priority.
2467 { return this->priority_; }
2469 // Set the priority.
2471 set_priority(int priority);
2473 // Return the bindings.
2476 { return this->bindings_; }
2478 // Whether some symbol from the package was used.
2481 { return this->used_; }
2483 // Note that some symbol from this package was used.
2486 { this->used_ = true; }
2488 // Clear the used field for the next file.
2491 { this->used_ = false; }
2493 // Whether this package was imported in the current file.
2496 { return this->is_imported_; }
2498 // Note that this package was imported in the current file.
2501 { this->is_imported_ = true; }
2503 // Clear the imported field for the next file.
2506 { this->is_imported_ = false; }
2508 // Whether this package was imported with a name of "_".
2510 uses_sink_alias() const
2511 { return this->uses_sink_alias_; }
2513 // Note that this package was imported with a name of "_".
2515 set_uses_sink_alias()
2516 { this->uses_sink_alias_ = true; }
2518 // Clear the sink alias field for the next file.
2520 clear_uses_sink_alias()
2521 { this->uses_sink_alias_ = false; }
2523 // Look up a name in the package. Returns NULL if the name is not
2526 lookup(const std::string& name) const
2527 { return this->bindings_->lookup(name); }
2529 // Set the name of the package.
2531 set_package_name(const std::string& name, Location);
2533 // Set the location of the package. This is used to record the most
2534 // recent import location.
2536 set_location(Location location)
2537 { this->location_ = location; }
2539 // Add a constant to the package.
2541 add_constant(const Typed_identifier& tid, Expression* expr)
2542 { return this->bindings_->add_constant(tid, this, expr, 0); }
2544 // Add a type to the package.
2546 add_type(const std::string& name, Type* type, Location location)
2547 { return this->bindings_->add_type(name, this, type, location); }
2549 // Add a type declaration to the package.
2551 add_type_declaration(const std::string& name, Location location)
2552 { return this->bindings_->add_type_declaration(name, this, location); }
2554 // Add a variable to the package.
2556 add_variable(const std::string& name, Variable* variable)
2557 { return this->bindings_->add_variable(name, this, variable); }
2559 // Add a function declaration to the package.
2561 add_function_declaration(const std::string& name, Function_type* type,
2563 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2565 // Determine types of constants.
2570 // The package path for type reflection data.
2571 std::string pkgpath_;
2572 // The package path for symbol names.
2573 std::string pkgpath_symbol_;
2574 // The name that this package uses in the package clause. This may
2575 // be the empty string if it is not yet known.
2576 std::string package_name_;
2577 // The names in this package.
2578 Bindings* bindings_;
2579 // The priority of this package. A package has a priority higher
2580 // than the priority of all of the packages that it imports. This
2581 // is used to run init functions in the right order.
2583 // The location of the import statement.
2585 // True if some name from this package was used. This is mutable
2586 // because we can use a package even if we have a const pointer to
2589 // True if this package was imported in the current file.
2591 // True if this package was imported with a name of "_".
2592 bool uses_sink_alias_;
2595 // Return codes for the traversal functions. This is not an enum
2596 // because we want to be able to declare traversal functions in other
2597 // header files without including this one.
2599 // Continue traversal as usual.
2600 const int TRAVERSE_CONTINUE = -1;
2603 const int TRAVERSE_EXIT = 0;
2605 // Continue traversal, but skip components of the current object.
2606 // E.g., if this is returned by Traverse::statement, we do not
2607 // traverse the expressions in the statement even if
2608 // traverse_expressions is set in the traverse_mask.
2609 const int TRAVERSE_SKIP_COMPONENTS = 1;
2611 // This class is used when traversing the parse tree. The caller uses
2612 // a subclass which overrides functions as desired.
2617 // These bitmasks say what to traverse.
2618 static const unsigned int traverse_variables = 0x1;
2619 static const unsigned int traverse_constants = 0x2;
2620 static const unsigned int traverse_functions = 0x4;
2621 static const unsigned int traverse_blocks = 0x8;
2622 static const unsigned int traverse_statements = 0x10;
2623 static const unsigned int traverse_expressions = 0x20;
2624 static const unsigned int traverse_types = 0x40;
2626 Traverse(unsigned int traverse_mask)
2627 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2630 virtual ~Traverse();
2632 // The bitmask of what to traverse.
2634 traverse_mask() const
2635 { return this->traverse_mask_; }
2637 // Record that we are going to traverse a type. This returns true
2638 // if the type has already been seen in this traversal. This is
2639 // required because types, unlike expressions, can form a circular
2642 remember_type(const Type*);
2644 // Record that we are going to see an expression. This returns true
2645 // if the expression has already been seen in this traversal. This
2646 // is only needed for cases where multiple expressions can point to
2649 remember_expression(const Expression*);
2651 // These functions return one of the TRAVERSE codes defined above.
2653 // If traverse_variables is set in the mask, this is called for
2654 // every variable in the tree.
2656 variable(Named_object*);
2658 // If traverse_constants is set in the mask, this is called for
2659 // every named constant in the tree. The bool parameter is true for
2660 // a global constant.
2662 constant(Named_object*, bool);
2664 // If traverse_functions is set in the mask, this is called for
2665 // every function in the tree.
2667 function(Named_object*);
2669 // If traverse_blocks is set in the mask, this is called for every
2670 // block in the tree.
2674 // If traverse_statements is set in the mask, this is called for
2675 // every statement in the tree.
2677 statement(Block*, size_t* index, Statement*);
2679 // If traverse_expressions is set in the mask, this is called for
2680 // every expression in the tree.
2682 expression(Expression**);
2684 // If traverse_types is set in the mask, this is called for every
2685 // type in the tree.
2690 // A hash table for types we have seen during this traversal. Note
2691 // that this uses the default hash functions for pointers rather
2692 // than Type_hash_identical and Type_identical. This is because for
2693 // traversal we care about seeing a specific type structure. If
2694 // there are two separate instances of identical types, we want to
2696 typedef Unordered_set(const Type*) Types_seen;
2698 typedef Unordered_set(const Expression*) Expressions_seen;
2700 // Bitmask of what sort of objects to traverse.
2701 unsigned int traverse_mask_;
2702 // Types which have been seen in this traversal.
2703 Types_seen* types_seen_;
2704 // Expressions which have been seen in this traversal.
2705 Expressions_seen* expressions_seen_;
2708 // A class which makes it easier to insert new statements before the
2709 // current statement during a traversal.
2711 class Statement_inserter
2714 // Empty constructor.
2715 Statement_inserter()
2716 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2719 // Constructor for a statement in a block.
2720 Statement_inserter(Block* block, size_t *pindex)
2721 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2724 // Constructor for a global variable.
2725 Statement_inserter(Gogo* gogo, Variable* var)
2726 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2727 { go_assert(var->is_global()); }
2729 // We use the default copy constructor and assignment operator.
2731 // Insert S before the statement we are traversing, or before the
2732 // initialization expression of a global variable.
2734 insert(Statement* s);
2737 // The block that the statement is in.
2739 // The index of the statement that we are traversing.
2741 // The IR, needed when looking at an initializer expression for a
2744 // The global variable, when looking at an initializer expression.
2748 // When translating the gogo IR into the backend data structure, this
2749 // is the context we pass down the blocks and statements.
2751 class Translate_context
2754 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2756 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2757 block_(block), bblock_(bblock), is_const_(false)
2764 { return this->gogo_; }
2768 { return this->backend_; }
2772 { return this->function_; }
2776 { return this->block_; }
2780 { return this->bblock_; }
2784 { return this->is_const_; }
2786 // Make a constant context.
2789 { this->is_const_ = true; }
2792 // The IR for the entire compilation unit.
2794 // The generator for the backend data structures.
2796 // The function we are currently translating. NULL if not in a
2797 // function, e.g., the initializer of a global variable.
2798 Named_object* function_;
2799 // The block we are currently translating. NULL if not in a
2802 // The backend representation of the current block. NULL if block_
2805 // Whether this is being evaluated in a constant context. This is
2806 // used for type descriptor initializers.
2810 // Runtime error codes. These must match the values in
2811 // libgo/runtime/go-runtime-error.c.
2813 // Slice index out of bounds: negative or larger than the length of
2815 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2817 // Array index out of bounds.
2818 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2820 // String index out of bounds.
2821 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2823 // Slice slice out of bounds: negative or larger than the length of
2824 // the slice or high bound less than low bound.
2825 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2827 // Array slice out of bounds.
2828 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2830 // String slice out of bounds.
2831 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2833 // Dereference of nil pointer. This is used when there is a
2834 // dereference of a pointer to a very large struct or array, to ensure
2835 // that a gigantic array is not used a proxy to access random memory
2837 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2839 // Slice length or capacity out of bounds in make: negative or
2840 // overflow or length greater than capacity.
2841 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2843 // Map capacity out of bounds in make: negative or overflow.
2844 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2846 // Channel capacity out of bounds in make: negative or overflow.
2847 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2849 // Division by zero.
2850 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
2852 // This is used by some of the langhooks.
2853 extern Gogo* go_get_gogo();
2855 // Whether we have seen any errors. FIXME: Replace with a backend
2857 extern bool saw_errors();
2859 #endif // !defined(GO_GOGO_H)