1 // gogo.h -- Go frontend parsed representation. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
10 #include "go-linemap.h"
13 class Statement_inserter;
15 class Type_hash_identical;
18 class Typed_identifier;
19 class Typed_identifier_list;
23 class Temporary_statement;
27 class Bindings_snapshot;
33 class Struct_field_list;
39 class Forward_declaration_type;
44 class Translate_context;
54 // This file declares the basic classes used to hold the internal
55 // representation of Go which is built by the parser.
57 // An initialization function for an imported package. This is a
58 // magic function which initializes variables and runs the "init"
64 Import_init(const std::string& package_name, const std::string& init_name,
66 : package_name_(package_name), init_name_(init_name), priority_(priority)
69 // The name of the package being imported.
72 { return this->package_name_; }
74 // The name of the package's init function.
77 { return this->init_name_; }
79 // The priority of the initialization function. Functions with a
80 // lower priority number must be run first.
83 { return this->priority_; }
86 // The name of the package being imported.
87 std::string package_name_;
88 // The name of the package's init function.
89 std::string init_name_;
94 // For sorting purposes.
97 operator<(const Import_init& i1, const Import_init& i2)
99 if (i1.priority() < i2.priority())
101 if (i1.priority() > i2.priority())
103 if (i1.package_name() != i2.package_name())
104 return i1.package_name() < i2.package_name();
105 return i1.init_name() < i2.init_name();
108 // The holder for the internal representation of the entire
114 // Create the IR, passing in the sizes of the types "int" and
115 // "uintptr" in bits.
116 Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size);
118 // Get the backend generator.
121 { return this->backend_; }
123 // Get the Location generator.
126 { return this->linemap_; }
128 // Get the package name.
130 package_name() const;
132 // Set the package name.
134 set_package_name(const std::string&, Location);
136 // Return whether this is the "main" package.
138 is_main_package() const;
140 // If necessary, adjust the name to use for a hidden symbol. We add
141 // a prefix of the package name, so that hidden symbols in different
142 // packages do not collide.
144 pack_hidden_name(const std::string& name, bool is_exported) const
148 : ('.' + this->unique_prefix()
149 + '.' + this->package_name()
153 // Unpack a name which may have been hidden. Returns the
154 // user-visible name of the object.
156 unpack_hidden_name(const std::string& name)
157 { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
159 // Return whether a possibly packed name is hidden.
161 is_hidden_name(const std::string& name)
162 { return name[0] == '.'; }
164 // Return the package prefix of a hidden name.
166 hidden_name_prefix(const std::string& name)
168 go_assert(Gogo::is_hidden_name(name));
169 return name.substr(1, name.rfind('.') - 1);
172 // Given a name which may or may not have been hidden, return the
173 // name to use in an error message.
175 message_name(const std::string& name);
177 // Return whether a name is the blank identifier _.
179 is_sink_name(const std::string& name)
181 return (name[0] == '.'
182 && name[name.length() - 1] == '_'
183 && name[name.length() - 2] == '.');
186 // Return the unique prefix to use for all exported symbols.
188 unique_prefix() const;
190 // Set the unique prefix.
192 set_unique_prefix(const std::string&);
194 // Return the priority to use for the package we are compiling.
195 // This is two more than the largest priority of any package we
198 package_priority() const;
200 // Import a package. FILENAME is the file name argument, LOCAL_NAME
201 // is the local name to give to the package. If LOCAL_NAME is empty
202 // the declarations are added to the global scope.
204 import_package(const std::string& filename, const std::string& local_name,
205 bool is_local_name_exported, Location);
207 // Whether we are the global binding level.
209 in_global_scope() const;
211 // Look up a name in the current binding contours.
213 lookup(const std::string&, Named_object** pfunction) const;
215 // Look up a name in the current block.
217 lookup_in_block(const std::string&) const;
219 // Look up a name in the global namespace--the universal scope.
221 lookup_global(const char*) const;
223 // Add a new imported package. REAL_NAME is the real name of the
224 // package. ALIAS is the alias of the package; this may be the same
225 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
226 // this package should be added to the global namespace; this is
227 // true if the alias is ".". LOCATION is the location of the import
228 // statement. This returns the new package, or NULL on error.
230 add_imported_package(const std::string& real_name, const std::string& alias,
231 bool is_alias_exported,
232 const std::string& unique_prefix,
234 bool* padd_to_globals);
236 // Register a package. This package may or may not be imported.
237 // This returns the Package structure for the package, creating if
240 register_package(const std::string& name, const std::string& unique_prefix,
243 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
244 // method function should be added to the type of its receiver.
246 start_function(const std::string& name, Function_type* type,
247 bool add_method_to_type, Location);
249 // Finish compiling a function.
251 finish_function(Location);
253 // Return the current function.
255 current_function() const;
257 // Return the current block.
261 // Start a new block. This is not initially associated with a
264 start_block(Location);
266 // Finish the current block and return it.
268 finish_block(Location);
270 // Declare an erroneous name. This is used to avoid knock-on errors
271 // after a parsing error.
273 add_erroneous_name(const std::string& name);
275 // Declare an unknown name. This is used while parsing. The name
276 // must be resolved by the end of the parse. Unknown names are
277 // always added at the package level.
279 add_unknown_name(const std::string& name, Location);
281 // Declare a function.
283 declare_function(const std::string&, Function_type*, Location);
285 // Declare a function at the package level. This is used for
286 // functions generated for a type.
288 declare_package_function(const std::string&, Function_type*, Location);
292 add_label_definition(const std::string&, Location);
294 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
295 // report errors for a goto from the current location to the label
298 add_label_reference(const std::string&, Location,
299 bool issue_goto_errors);
301 // Return a snapshot of the current binding state.
303 bindings_snapshot(Location);
305 // Add a statement to the current block.
307 add_statement(Statement*);
309 // Add a block to the current block.
311 add_block(Block*, Location);
315 add_constant(const Typed_identifier&, Expression*, int iota_value);
319 add_type(const std::string&, Type*, Location);
321 // Add a named type. This is used for builtin types, and to add an
322 // imported type to the global scope.
324 add_named_type(Named_type*);
328 declare_type(const std::string&, Location);
330 // Declare a type at the package level. This is used when the
331 // parser sees an unknown name where a type name is required.
333 declare_package_type(const std::string&, Location);
335 // Define a type which was already declared.
337 define_type(Named_object*, Named_type*);
341 add_variable(const std::string&, Variable*);
343 // Add a sink--a reference to the blank identifier _.
347 // Add a type which needs to be verified. This is used for sink
348 // types, just to give appropriate error messages.
350 add_type_to_verify(Type* type);
352 // Add a named object to the current namespace. This is used for
353 // import . "package".
355 add_named_object(Named_object*);
357 // Mark all local variables in current bindings as used. This is
358 // used when there is a parse error to avoid useless errors.
362 // Return a name to use for a thunk function. A thunk function is
363 // one we create during the compilation, for a go statement or a
364 // defer statement or a method expression.
368 // Return whether an object is a thunk.
370 is_thunk(const Named_object*);
372 // Note that we've seen an interface type. This is used to build
373 // all required interface method tables.
375 record_interface_type(Interface_type*);
377 // Note that we need an initialization function.
380 { this->need_init_fn_ = true; }
382 // Clear out all names in file scope. This is called when we start
383 // parsing a new file.
387 // Queue up a type-specific function to be written out. This is
388 // used when a type-specific function is needed when not at the top
391 queue_specific_type_function(Type* type, Named_type* name,
392 const std::string& hash_name,
393 Function_type* hash_fntype,
394 const std::string& equal_name,
395 Function_type* equal_fntype);
397 // Write out queued specific type functions.
399 write_specific_type_functions();
401 // Traverse the tree. See the Traverse class.
405 // Define the predeclared global names.
407 define_global_names();
409 // Verify and complete all types.
413 // Lower the parse tree.
417 // Lower all the statements in a block.
419 lower_block(Named_object* function, Block*);
421 // Lower an expression.
423 lower_expression(Named_object* function, Statement_inserter*, Expression**);
427 lower_constant(Named_object*);
429 // Finalize the method lists and build stub methods for named types.
433 // Work out the types to use for unspecified variables and
438 // Type check the program.
442 // Check the types in a single block. This is used for complicated
445 check_types_in_block(Block*);
447 // Check for return statements.
449 check_return_statements();
455 // Add an import control function for an imported package to the
458 add_import_init_fn(const std::string& package_name,
459 const std::string& init_name, int prio);
461 // Turn short-cut operators (&&, ||) into explicit if statements.
465 // Use temporary variables to force order of evaluation.
469 // Build thunks for functions which call recover.
471 build_recover_thunks();
473 // Simplify statements which might use thunks: go and defer
476 simplify_thunk_statements();
478 // Dump AST if -fgo-dump-ast is set
480 dump_ast(const char* basename);
482 // Convert named types to the backend representation.
484 convert_named_types();
486 // Convert named types in a list of bindings.
488 convert_named_types_in_bindings(Bindings*);
490 // True if named types have been converted to the backend
493 named_types_are_converted() const
494 { return this->named_types_are_converted_; }
496 // Write out the global values.
500 // Create trees for implicit builtin functions.
502 define_builtin_function_trees();
504 // Build a call to a builtin function. PDECL should point to a NULL
505 // initialized static pointer which will hold the fndecl. NAME is
506 // the name of the function. NARGS is the number of arguments.
507 // RETTYPE is the return type. It is followed by NARGS pairs of
508 // type and argument (both trees).
510 call_builtin(tree* pdecl, Location, const char* name, int nargs,
513 // Build a call to the runtime error function.
515 runtime_error(int code, Location);
517 // Build a builtin struct with a list of fields.
519 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
522 // Mark a function declaration as a builtin library function.
524 mark_fndecl_as_builtin_library(tree fndecl);
526 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
527 // the slice. VALUES points to the values. COUNT is the size,
528 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
531 slice_constructor(tree slice_type_tree, tree values, tree count,
534 // Build required interface method tables.
536 build_interface_method_tables();
538 // Build an interface method table for a type: a list of function
539 // pointers, one for each interface method. This returns a decl.
541 interface_method_table_for_type(const Interface_type*, Named_type*,
544 // Return a tree which allocate SIZE bytes to hold values of type
547 allocate_memory(Type *type, tree size, Location);
549 // Return a type to use for pointer to const char.
551 const_char_pointer_type_tree();
553 // Build a string constant with the right type.
555 string_constant_tree(const std::string&);
557 // Build a Go string constant. This returns a pointer to the
560 go_string_constant_tree(const std::string&);
562 // Receive a value from a channel.
564 receive_from_channel(tree type_tree, tree type_descriptor_tree, tree channel,
567 // Make a trampoline which calls FNADDR passing CLOSURE.
569 make_trampoline(tree fnaddr, tree closure, Location);
572 // During parsing, we keep a stack of functions. Each function on
573 // the stack is one that we are currently parsing. For each
574 // function, we keep track of the current stack of blocks.
578 Named_object* function;
579 // The stack of active blocks in the function.
580 std::vector<Block*> blocks;
583 // The stack of functions.
584 typedef std::vector<Open_function> Open_functions;
586 // Set up the built-in unsafe package.
588 import_unsafe(const std::string&, bool is_exported, Location);
590 // Add a new imported package.
592 add_package(const std::string& real_name, const std::string& alias,
593 const std::string& unique_prefix, Location location);
595 // Return the current binding contour.
600 current_bindings() const;
602 // Get the name of the magic initialization function.
606 // Get the decl for the magic initialization function.
608 initialization_function_decl();
610 // Write the magic initialization function.
612 write_initialization_function(tree fndecl, tree init_stmt_list);
614 // Initialize imported packages.
618 // Register variables with the garbage collector.
620 register_gc_vars(const std::vector<Named_object*>&, tree*);
622 // Build a pointer to a Go string constant. This returns a pointer
625 ptr_go_string_constant_tree(const std::string&);
627 // Return the type of a trampoline.
629 trampoline_type_tree();
631 // Type used to map import names to packages.
632 typedef std::map<std::string, Package*> Imports;
634 // Type used to map package names to packages.
635 typedef std::map<std::string, Package*> Packages;
637 // Type used to map special names in the sys package.
638 typedef std::map<std::string, std::string> Sys_names;
640 // Type used to queue writing a type specific function.
641 struct Specific_type_function
645 std::string hash_name;
646 Function_type* hash_fntype;
647 std::string equal_name;
648 Function_type* equal_fntype;
650 Specific_type_function(Type* atype, Named_type* aname,
651 const std::string& ahash_name,
652 Function_type* ahash_fntype,
653 const std::string& aequal_name,
654 Function_type* aequal_fntype)
655 : type(atype), name(aname), hash_name(ahash_name),
656 hash_fntype(ahash_fntype), equal_name(aequal_name),
657 equal_fntype(aequal_fntype)
661 // The backend generator.
663 // The object used to keep track of file names and line numbers.
665 // The package we are compiling.
667 // The list of currently open functions during parsing.
668 Open_functions functions_;
669 // The global binding contour. This includes the builtin functions
670 // and the package we are compiling.
672 // Mapping from import file names to packages.
674 // Whether the magic unsafe package was imported.
675 bool imported_unsafe_;
676 // Mapping from package names we have seen to packages. This does
677 // not include the package we are compiling.
679 // The functions named "init", if there are any.
680 std::vector<Named_object*> init_functions_;
681 // Whether we need a magic initialization function.
683 // The name of the magic initialization function.
684 std::string init_fn_name_;
685 // A list of import control variables for packages that we import.
686 std::set<Import_init> imported_init_fns_;
687 // The unique prefix used for all global symbols.
688 std::string unique_prefix_;
689 // Whether an explicit unique prefix was set by -fgo-prefix.
690 bool unique_prefix_specified_;
691 // A list of types to verify.
692 std::vector<Type*> verify_types_;
693 // A list of interface types defined while parsing.
694 std::vector<Interface_type*> interface_types_;
695 // Type specific functions to write out.
696 std::vector<Specific_type_function*> specific_type_functions_;
697 // Whether we are done writing out specific type functions.
698 bool specific_type_functions_are_written_;
699 // Whether named types have been converted.
700 bool named_types_are_converted_;
703 // A block of statements.
708 Block(Block* enclosing, Location);
710 // Return the enclosing block.
713 { return this->enclosing_; }
715 // Return the bindings of the block.
718 { return this->bindings_; }
722 { return this->bindings_; }
724 // Look at the block's statements.
725 const std::vector<Statement*>*
727 { return &this->statements_; }
729 // Return the start location. This is normally the location of the
730 // left curly brace which starts the block.
732 start_location() const
733 { return this->start_location_; }
735 // Return the end location. This is normally the location of the
736 // right curly brace which ends the block.
739 { return this->end_location_; }
741 // Add a statement to the block.
743 add_statement(Statement*);
745 // Add a statement to the front of the block.
747 add_statement_at_front(Statement*);
749 // Replace a statement in a block.
751 replace_statement(size_t index, Statement*);
753 // Add a Statement before statement number INDEX.
755 insert_statement_before(size_t index, Statement*);
757 // Add a Statement after statement number INDEX.
759 insert_statement_after(size_t index, Statement*);
761 // Set the end location of the block.
763 set_end_location(Location location)
764 { this->end_location_ = location; }
766 // Traverse the tree.
770 // Set final types for unspecified variables and constants.
774 // Return true if execution of this block may fall through to the
777 may_fall_through() const;
779 // Convert the block to the backend representation.
781 get_backend(Translate_context*);
783 // Iterate over statements.
785 typedef std::vector<Statement*>::iterator iterator;
789 { return this->statements_.begin(); }
793 { return this->statements_.end(); }
798 // Statements in the block.
799 std::vector<Statement*> statements_;
802 // Location of start of block.
803 Location start_location_;
804 // Location of end of block.
805 Location end_location_;
813 Function(Function_type* type, Function*, Block*, Location);
815 // Return the function's type.
818 { return this->type_; }
820 // Return the enclosing function if there is one.
823 { return this->enclosing_; }
825 // Set the enclosing function. This is used when building thunks
826 // for functions which call recover.
828 set_enclosing(Function* enclosing)
830 go_assert(this->enclosing_ == NULL);
831 this->enclosing_ = enclosing;
834 // The result variables.
835 typedef std::vector<Named_object*> Results;
837 // Create the result variables in the outer block.
839 create_result_variables(Gogo*);
841 // Update the named result variables when cloning a function which
844 update_result_variables();
846 // Return the result variables.
849 { return this->results_; }
851 // Whether the result variables have names.
853 results_are_named() const
854 { return this->results_are_named_; }
856 // Add a new field to the closure variable.
858 add_closure_field(Named_object* var, Location loc)
859 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
861 // Whether this function needs a closure.
863 needs_closure() const
864 { return !this->closure_fields_.empty(); }
866 // Return the closure variable, creating it if necessary. This is
867 // passed to the function as a static chain parameter.
871 // Set the closure variable. This is used when building thunks for
872 // functions which call recover.
874 set_closure_var(Named_object* v)
876 go_assert(this->closure_var_ == NULL);
877 this->closure_var_ = v;
880 // Return the variable for a reference to field INDEX in the closure
883 enclosing_var(unsigned int index)
885 go_assert(index < this->closure_fields_.size());
886 return closure_fields_[index].first;
889 // Set the type of the closure variable if there is one.
893 // Get the block of statements associated with the function.
896 { return this->block_; }
898 // Get the location of the start of the function.
901 { return this->location_; }
903 // Return whether this function is actually a method.
907 // Add a label definition to the function.
909 add_label_definition(Gogo*, const std::string& label_name, Location);
911 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
912 // if we should report errors for a goto from the current location
913 // to the label location.
915 add_label_reference(Gogo*, const std::string& label_name,
916 Location, bool issue_goto_errors);
918 // Warn about labels that are defined but not used.
920 check_labels() const;
922 // Whether this function calls the predeclared recover function.
924 calls_recover() const
925 { return this->calls_recover_; }
927 // Record that this function calls the predeclared recover function.
928 // This is set during the lowering pass.
931 { this->calls_recover_ = true; }
933 // Whether this is a recover thunk function.
935 is_recover_thunk() const
936 { return this->is_recover_thunk_; }
938 // Record that this is a thunk built for a function which calls
941 set_is_recover_thunk()
942 { this->is_recover_thunk_ = true; }
944 // Whether this function already has a recover thunk.
946 has_recover_thunk() const
947 { return this->has_recover_thunk_; }
949 // Record that this function already has a recover thunk.
951 set_has_recover_thunk()
952 { this->has_recover_thunk_ = true; }
954 // Swap with another function. Used only for the thunk which calls
957 swap_for_recover(Function *);
959 // Traverse the tree.
963 // Determine types in the function.
967 // Return the function's decl given an identifier.
969 get_or_make_decl(Gogo*, Named_object*, tree id);
971 // Return the function's decl after it has been built.
975 go_assert(this->fndecl_ != NULL);
976 return this->fndecl_;
979 // Set the function decl to hold a tree of the function code.
981 build_tree(Gogo*, Named_object*);
983 // Get the value to return when not explicitly specified. May also
984 // add statements to execute first to STMT_LIST.
986 return_value(Gogo*, Named_object*, Location, tree* stmt_list) const;
988 // Get a tree for the variable holding the defer stack.
990 defer_stack(Location);
992 // Export the function.
994 export_func(Export*, const std::string& name) const;
996 // Export a function with a type.
998 export_func_with_type(Export*, const std::string& name,
999 const Function_type*);
1001 // Import a function.
1003 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1004 Typed_identifier_list** pparameters,
1005 Typed_identifier_list** presults, bool* is_varargs);
1008 // Type for mapping from label names to Label objects.
1009 typedef Unordered_map(std::string, Label*) Labels;
1012 make_receiver_parm_decl(Gogo*, Named_object*, tree);
1015 copy_parm_to_heap(Gogo*, Named_object*, tree);
1018 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1020 typedef std::vector<std::pair<Named_object*,
1021 Location> > Closure_fields;
1023 // The function's type.
1024 Function_type* type_;
1025 // The enclosing function. This is NULL when there isn't one, which
1026 // is the normal case.
1027 Function* enclosing_;
1028 // The result variables, if any.
1030 // If there is a closure, this is the list of variables which appear
1031 // in the closure. This is created by the parser, and then resolved
1032 // to a real type when we lower parse trees.
1033 Closure_fields closure_fields_;
1034 // The closure variable, passed as a parameter using the static
1035 // chain parameter. Normally NULL.
1036 Named_object* closure_var_;
1037 // The outer block of statements in the function.
1039 // The source location of the start of the function.
1041 // Labels defined or referenced in the function.
1043 // The function decl.
1045 // The defer stack variable. A pointer to this variable is used to
1046 // distinguish the defer stack for one function from another. This
1047 // is NULL unless we actually need a defer stack.
1048 Temporary_statement* defer_stack_;
1049 // True if the result variables are named.
1050 bool results_are_named_;
1051 // True if this function calls the predeclared recover function.
1052 bool calls_recover_;
1053 // True if this a thunk built for a function which calls recover.
1054 bool is_recover_thunk_;
1055 // True if this function already has a recover thunk.
1056 bool has_recover_thunk_;
1059 // A snapshot of the current binding state.
1061 class Bindings_snapshot
1064 Bindings_snapshot(const Block*, Location);
1066 // Report any errors appropriate for a goto from the current binding
1067 // state of B to this one.
1069 check_goto_from(const Block* b, Location);
1071 // Report any errors appropriate for a goto from this binding state
1072 // to the current state of B.
1074 check_goto_to(const Block* b);
1078 check_goto_block(Location, const Block*, const Block*, size_t*);
1081 check_goto_defs(Location, const Block*, size_t, size_t);
1083 // The current block.
1084 const Block* block_;
1085 // The number of names currently defined in each open block.
1086 // Element 0 is this->block_, element 1 is
1087 // this->block_->enclosing(), etc.
1088 std::vector<size_t> counts_;
1089 // The location where this snapshot was taken.
1093 // A function declaration.
1095 class Function_declaration
1098 Function_declaration(Function_type* fntype, Location location)
1099 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1104 { return this->fntype_; }
1108 { return this->location_; }
1112 { return this->asm_name_; }
1114 // Set the assembler name.
1116 set_asm_name(const std::string& asm_name)
1117 { this->asm_name_ = asm_name; }
1119 // Return a decl for the function given an identifier.
1121 get_or_make_decl(Gogo*, Named_object*, tree id);
1123 // Export a function declaration.
1125 export_func(Export* exp, const std::string& name) const
1126 { Function::export_func_with_type(exp, name, this->fntype_); }
1129 // The type of the function.
1130 Function_type* fntype_;
1131 // The location of the declaration.
1133 // The assembler name: this is the name to use in references to the
1134 // function. This is normally empty.
1135 std::string asm_name_;
1136 // The function decl if needed.
1145 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1146 bool is_receiver, Location);
1148 // Get the type of the variable.
1155 // Return whether the type is defined yet.
1158 { return this->type_ != NULL; }
1160 // Get the initial value.
1163 { return this->init_; }
1165 // Return whether there are any preinit statements.
1167 has_pre_init() const
1168 { return this->preinit_ != NULL; }
1170 // Return the preinit statements if any.
1173 { return this->preinit_; }
1175 // Return whether this is a global variable.
1178 { return this->is_global_; }
1180 // Return whether this is a function parameter.
1182 is_parameter() const
1183 { return this->is_parameter_; }
1185 // Return whether this is the receiver parameter of a method.
1188 { return this->is_receiver_; }
1190 // Change this parameter to be a receiver. This is used when
1191 // creating the thunks created for functions which call recover.
1195 go_assert(this->is_parameter_);
1196 this->is_receiver_ = true;
1199 // Change this parameter to not be a receiver. This is used when
1200 // creating the thunks created for functions which call recover.
1202 set_is_not_receiver()
1204 go_assert(this->is_parameter_);
1205 this->is_receiver_ = false;
1208 // Return whether this is the varargs parameter of a function.
1210 is_varargs_parameter() const
1211 { return this->is_varargs_parameter_; }
1213 // Whether this variable's address is taken.
1215 is_address_taken() const
1216 { return this->is_address_taken_; }
1218 // Whether this variable should live in the heap.
1221 { return this->is_address_taken_ && !this->is_global_; }
1223 // Note that something takes the address of this variable.
1226 { this->is_address_taken_ = true; }
1228 // Return whether the address is taken but does not escape.
1230 is_non_escaping_address_taken() const
1231 { return this->is_non_escaping_address_taken_; }
1233 // Note that something takes the address of this variable such that
1234 // the address does not escape the function.
1236 set_non_escaping_address_taken()
1237 { this->is_non_escaping_address_taken_ = true; }
1239 // Get the source location of the variable's declaration.
1242 { return this->location_; }
1244 // Record that this is the varargs parameter of a function.
1246 set_is_varargs_parameter()
1248 go_assert(this->is_parameter_);
1249 this->is_varargs_parameter_ = true;
1252 // Return whether the variable has been used.
1255 { return this->is_used_; }
1257 // Mark that the variable has been used.
1260 { this->is_used_ = true; }
1262 // Clear the initial value; used for error handling.
1265 { this->init_ = NULL; }
1267 // Set the initial value; used for converting shortcuts.
1269 set_init(Expression* init)
1270 { this->init_ = init; }
1272 // Get the preinit block, a block of statements to be run before the
1273 // initialization expression.
1275 preinit_block(Gogo*);
1277 // Add a statement to be run before the initialization expression.
1278 // This is only used for global variables.
1280 add_preinit_statement(Gogo*, Statement*);
1282 // Lower the initialization expression after parsing is complete.
1284 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1286 // A special case: the init value is used only to determine the
1287 // type. This is used if the variable is defined using := with the
1288 // comma-ok form of a map index or a receive expression. The init
1289 // value is actually the map index expression or receive expression.
1290 // We use this because we may not know the right type at parse time.
1292 set_type_from_init_tuple()
1293 { this->type_from_init_tuple_ = true; }
1295 // Another special case: the init value is used only to determine
1296 // the type. This is used if the variable is defined using := with
1297 // a range clause. The init value is the range expression. The
1298 // type of the variable is the index type of the range expression
1299 // (i.e., the first value returned by a range).
1301 set_type_from_range_index()
1302 { this->type_from_range_index_ = true; }
1304 // Another special case: like set_type_from_range_index, but the
1305 // type is the value type of the range expression (i.e., the second
1306 // value returned by a range).
1308 set_type_from_range_value()
1309 { this->type_from_range_value_ = true; }
1311 // Another special case: the init value is used only to determine
1312 // the type. This is used if the variable is defined using := with
1313 // a case in a select statement. The init value is the channel.
1314 // The type of the variable is the channel's element type.
1316 set_type_from_chan_element()
1317 { this->type_from_chan_element_ = true; }
1319 // After we lower the select statement, we once again set the type
1320 // from the initialization expression.
1322 clear_type_from_chan_element()
1324 go_assert(this->type_from_chan_element_);
1325 this->type_from_chan_element_ = false;
1328 // Note that this variable was created for a type switch clause.
1330 set_is_type_switch_var()
1331 { this->is_type_switch_var_ = true; }
1333 // Traverse the initializer expression.
1335 traverse_expression(Traverse*, unsigned int traverse_mask);
1337 // Determine the type of the variable if necessary.
1341 // Get the backend representation of the variable.
1343 get_backend_variable(Gogo*, Named_object*, const Package*,
1344 const std::string&);
1346 // Get the initial value of the variable as a tree. This may only
1347 // be called if has_pre_init() returns false.
1349 get_init_tree(Gogo*, Named_object* function);
1351 // Return a series of statements which sets the value of the
1352 // variable in DECL. This should only be called is has_pre_init()
1353 // returns true. DECL may be NULL for a sink variable.
1355 get_init_block(Gogo*, Named_object* function, tree decl);
1357 // Export the variable.
1359 export_var(Export*, const std::string& name) const;
1361 // Import a variable.
1363 import_var(Import*, std::string* pname, Type** ptype);
1366 // The type of a tuple.
1368 type_from_tuple(Expression*, bool) const;
1370 // The type of a range.
1372 type_from_range(Expression*, bool, bool) const;
1374 // The element type of a channel.
1376 type_from_chan_element(Expression*, bool) const;
1378 // The variable's type. This may be NULL if the type is set from
1381 // The initial value. This may be NULL if the variable should be
1382 // initialized to the default value for the type.
1384 // Statements to run before the init statement.
1386 // Location of variable definition.
1388 // Backend representation.
1389 Bvariable* backend_;
1390 // Whether this is a global variable.
1391 bool is_global_ : 1;
1392 // Whether this is a function parameter.
1393 bool is_parameter_ : 1;
1394 // Whether this is the receiver parameter of a method.
1395 bool is_receiver_ : 1;
1396 // Whether this is the varargs parameter of a function.
1397 bool is_varargs_parameter_ : 1;
1398 // Whether this variable is ever referenced.
1400 // Whether something takes the address of this variable. For a
1401 // local variable this implies that the variable has to be on the
1403 bool is_address_taken_ : 1;
1404 // Whether something takes the address of this variable such that
1405 // the address does not escape the function.
1406 bool is_non_escaping_address_taken_ : 1;
1407 // True if we have seen this variable in a traversal.
1409 // True if we have lowered the initialization expression.
1410 bool init_is_lowered_ : 1;
1411 // True if init is a tuple used to set the type.
1412 bool type_from_init_tuple_ : 1;
1413 // True if init is a range clause and the type is the index type.
1414 bool type_from_range_index_ : 1;
1415 // True if init is a range clause and the type is the value type.
1416 bool type_from_range_value_ : 1;
1417 // True if init is a channel and the type is the channel's element type.
1418 bool type_from_chan_element_ : 1;
1419 // True if this is a variable created for a type switch case.
1420 bool is_type_switch_var_ : 1;
1421 // True if we have determined types.
1422 bool determined_type_ : 1;
1425 // A variable which is really the name for a function return value, or
1428 class Result_variable
1431 Result_variable(Type* type, Function* function, int index,
1433 : type_(type), function_(function), index_(index), location_(location),
1434 backend_(NULL), is_address_taken_(false),
1435 is_non_escaping_address_taken_(false)
1438 // Get the type of the result variable.
1441 { return this->type_; }
1443 // Get the function that this is associated with.
1446 { return this->function_; }
1448 // Index in the list of function results.
1451 { return this->index_; }
1453 // The location of the variable definition.
1456 { return this->location_; }
1458 // Whether this variable's address is taken.
1460 is_address_taken() const
1461 { return this->is_address_taken_; }
1463 // Note that something takes the address of this variable.
1466 { this->is_address_taken_ = true; }
1468 // Return whether the address is taken but does not escape.
1470 is_non_escaping_address_taken() const
1471 { return this->is_non_escaping_address_taken_; }
1473 // Note that something takes the address of this variable such that
1474 // the address does not escape the function.
1476 set_non_escaping_address_taken()
1477 { this->is_non_escaping_address_taken_ = true; }
1479 // Whether this variable should live in the heap.
1482 { return this->is_address_taken_; }
1484 // Set the function. This is used when cloning functions which call
1487 set_function(Function* function)
1488 { this->function_ = function; }
1490 // Get the backend representation of the variable.
1492 get_backend_variable(Gogo*, Named_object*, const std::string&);
1495 // Type of result variable.
1497 // Function with which this is associated.
1498 Function* function_;
1499 // Index in list of results.
1501 // Where the result variable is defined.
1503 // Backend representation.
1504 Bvariable* backend_;
1505 // Whether something takes the address of this variable.
1506 bool is_address_taken_;
1507 // Whether something takes the address of this variable such that
1508 // the address does not escape the function.
1509 bool is_non_escaping_address_taken_;
1512 // The value we keep for a named constant. This lets us hold a type
1513 // and an expression.
1515 class Named_constant
1518 Named_constant(Type* type, Expression* expr, int iota_value,
1520 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1526 { return this->type_; }
1530 { return this->expr_; }
1534 { return this->iota_value_; }
1538 { return this->location_; }
1540 // Whether we are lowering.
1543 { return this->lowering_; }
1545 // Set that we are lowering.
1548 { this->lowering_ = true; }
1550 // We are no longer lowering.
1553 { this->lowering_ = false; }
1555 // Traverse the expression.
1557 traverse_expression(Traverse*);
1559 // Determine the type of the constant if necessary.
1563 // Indicate that we found and reported an error for this constant.
1567 // Export the constant.
1569 export_const(Export*, const std::string& name) const;
1571 // Import a constant.
1573 import_const(Import*, std::string*, Type**, Expression**);
1576 // The type of the constant.
1578 // The expression for the constant.
1580 // If the predeclared constant iota is used in EXPR_, this is the
1581 // value it will have. We do this because at parse time we don't
1582 // know whether the name "iota" will refer to the predeclared
1583 // constant or to something else. We put in the right value in when
1586 // The location of the definition.
1588 // Whether we are currently lowering this constant.
1592 // A type declaration.
1594 class Type_declaration
1597 Type_declaration(Location location)
1598 : location_(location), in_function_(NULL), methods_(),
1599 issued_warning_(false)
1602 // Return the location.
1605 { return this->location_; }
1607 // Return the function in which this type is declared. This will
1608 // return NULL for a type declared in global scope.
1611 { return this->in_function_; }
1613 // Set the function in which this type is declared.
1615 set_in_function(Named_object* f)
1616 { this->in_function_ = f; }
1618 // Add a method to this type. This is used when methods are defined
1621 add_method(const std::string& name, Function* function);
1623 // Add a method declaration to this type.
1625 add_method_declaration(const std::string& name, Function_type* type,
1628 // Return whether any methods were defined.
1630 has_methods() const;
1632 // Define methods when the real type is known.
1634 define_methods(Named_type*);
1636 // This is called if we are trying to use this type. It returns
1637 // true if we should issue a warning.
1642 typedef std::vector<Named_object*> Methods;
1644 // The location of the type declaration.
1646 // If this type is declared in a function, a pointer back to the
1647 // function in which it is defined.
1648 Named_object* in_function_;
1649 // Methods defined before the type is defined.
1651 // True if we have issued a warning about a use of this type
1652 // declaration when it is undefined.
1653 bool issued_warning_;
1656 // An unknown object. These are created by the parser for forward
1657 // references to names which have not been seen before. In a correct
1658 // program, these will always point to a real definition by the end of
1659 // the parse. Because they point to another Named_object, these may
1660 // only be referenced by Unknown_expression objects.
1665 Unknown_name(Location location)
1666 : location_(location), real_named_object_(NULL)
1669 // Return the location where this name was first seen.
1672 { return this->location_; }
1674 // Return the real named object that this points to, or NULL if it
1675 // was never resolved.
1677 real_named_object() const
1678 { return this->real_named_object_; }
1680 // Set the real named object that this points to.
1682 set_real_named_object(Named_object* no);
1685 // The location where this name was first seen.
1687 // The real named object when it is known.
1692 // A named object named. This is the result of a declaration. We
1693 // don't use a superclass because they all have to be handled
1701 // An uninitialized Named_object. We should never see this.
1702 NAMED_OBJECT_UNINITIALIZED,
1703 // An erroneous name. This indicates a parse error, to avoid
1704 // later errors about undefined references.
1705 NAMED_OBJECT_ERRONEOUS,
1706 // An unknown name. This is used for forward references. In a
1707 // correct program, these will all be resolved by the end of the
1709 NAMED_OBJECT_UNKNOWN,
1714 // A forward type declaration.
1715 NAMED_OBJECT_TYPE_DECLARATION,
1718 // A result variable in a function.
1719 NAMED_OBJECT_RESULT_VAR,
1720 // The blank identifier--the special variable named _.
1724 // A forward func declaration.
1725 NAMED_OBJECT_FUNC_DECLARATION,
1727 NAMED_OBJECT_PACKAGE
1730 // Return the classification.
1732 classification() const
1733 { return this->classification_; }
1738 is_erroneous() const
1739 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1743 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1747 { return this->classification_ == NAMED_OBJECT_CONST; }
1751 { return this->classification_ == NAMED_OBJECT_TYPE; }
1754 is_type_declaration() const
1755 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1759 { return this->classification_ == NAMED_OBJECT_VAR; }
1762 is_result_variable() const
1763 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1767 { return this->classification_ == NAMED_OBJECT_SINK; }
1771 { return this->classification_ == NAMED_OBJECT_FUNC; }
1774 is_function_declaration() const
1775 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1779 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1783 static Named_object*
1784 make_erroneous_name(const std::string& name)
1785 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1787 static Named_object*
1788 make_unknown_name(const std::string& name, Location);
1790 static Named_object*
1791 make_constant(const Typed_identifier&, const Package*, Expression*,
1794 static Named_object*
1795 make_type(const std::string&, const Package*, Type*, Location);
1797 static Named_object*
1798 make_type_declaration(const std::string&, const Package*, Location);
1800 static Named_object*
1801 make_variable(const std::string&, const Package*, Variable*);
1803 static Named_object*
1804 make_result_variable(const std::string&, Result_variable*);
1806 static Named_object*
1809 static Named_object*
1810 make_function(const std::string&, const Package*, Function*);
1812 static Named_object*
1813 make_function_declaration(const std::string&, const Package*, Function_type*,
1816 static Named_object*
1817 make_package(const std::string& alias, Package* package);
1824 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1825 return this->u_.unknown_value;
1829 unknown_value() const
1831 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1832 return this->u_.unknown_value;
1838 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1839 return this->u_.const_value;
1842 const Named_constant*
1845 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1846 return this->u_.const_value;
1852 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1853 return this->u_.type_value;
1859 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1860 return this->u_.type_value;
1864 type_declaration_value()
1866 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1867 return this->u_.type_declaration;
1870 const Type_declaration*
1871 type_declaration_value() const
1873 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1874 return this->u_.type_declaration;
1880 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1881 return this->u_.var_value;
1887 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1888 return this->u_.var_value;
1894 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1895 return this->u_.result_var_value;
1898 const Result_variable*
1899 result_var_value() const
1901 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1902 return this->u_.result_var_value;
1908 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1909 return this->u_.func_value;
1915 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1916 return this->u_.func_value;
1919 Function_declaration*
1920 func_declaration_value()
1922 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1923 return this->u_.func_declaration_value;
1926 const Function_declaration*
1927 func_declaration_value() const
1929 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1930 return this->u_.func_declaration_value;
1936 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1937 return this->u_.package_value;
1941 package_value() const
1943 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1944 return this->u_.package_value;
1949 { return this->name_; }
1951 // Return the name to use in an error message. The difference is
1952 // that if this Named_object is defined in a different package, this
1953 // will return PACKAGE.NAME.
1955 message_name() const;
1959 { return this->package_; }
1961 // Resolve an unknown value if possible. This returns the same
1962 // Named_object or a new one.
1966 Named_object* ret = this;
1967 if (this->is_unknown())
1969 Named_object* r = this->unknown_value()->real_named_object();
1979 const Named_object* ret = this;
1980 if (this->is_unknown())
1982 const Named_object* r = this->unknown_value()->real_named_object();
1989 // The location where this object was defined or referenced.
1993 // Convert a variable to the backend representation.
1995 get_backend_variable(Gogo*, Named_object* function);
1997 // Return a tree for the external identifier for this object.
2001 // Return a tree representing this object.
2003 get_tree(Gogo*, Named_object* function);
2005 // Define a type declaration.
2007 set_type_value(Named_type*);
2009 // Define a function declaration.
2011 set_function_value(Function*);
2013 // Declare an unknown name as a type declaration.
2017 // Export this object.
2019 export_named_object(Export*) const;
2022 Named_object(const std::string&, const Package*, Classification);
2024 // The name of the object.
2026 // The package that this object is in. This is NULL if it is in the
2027 // file we are compiling.
2028 const Package* package_;
2029 // The type of object this is.
2030 Classification classification_;
2034 Unknown_name* unknown_value;
2035 Named_constant* const_value;
2036 Named_type* type_value;
2037 Type_declaration* type_declaration;
2038 Variable* var_value;
2039 Result_variable* result_var_value;
2040 Function* func_value;
2041 Function_declaration* func_declaration_value;
2042 Package* package_value;
2044 // The DECL tree for this object if we have already converted it.
2048 // A binding contour. This binds names to objects.
2053 // Type for mapping from names to objects.
2054 typedef Unordered_map(std::string, Named_object*) Contour;
2056 Bindings(Bindings* enclosing);
2058 // Add an erroneous name.
2060 add_erroneous_name(const std::string& name)
2061 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2063 // Add an unknown name.
2065 add_unknown_name(const std::string& name, Location location)
2067 return this->add_named_object(Named_object::make_unknown_name(name,
2073 add_constant(const Typed_identifier& tid, const Package* package,
2074 Expression* expr, int iota_value)
2076 return this->add_named_object(Named_object::make_constant(tid, package,
2083 add_type(const std::string& name, const Package* package, Type* type,
2086 return this->add_named_object(Named_object::make_type(name, package, type,
2090 // Add a named type. This is used for builtin types, and to add an
2091 // imported type to the global scope.
2093 add_named_type(Named_type* named_type);
2095 // Add a type declaration.
2097 add_type_declaration(const std::string& name, const Package* package,
2100 Named_object* no = Named_object::make_type_declaration(name, package,
2102 return this->add_named_object(no);
2107 add_variable(const std::string& name, const Package* package,
2110 return this->add_named_object(Named_object::make_variable(name, package,
2114 // Add a result variable.
2116 add_result_variable(const std::string& name, Result_variable* result)
2118 return this->add_named_object(Named_object::make_result_variable(name,
2124 add_function(const std::string& name, const Package*, Function* function);
2126 // Add a function declaration.
2128 add_function_declaration(const std::string& name, const Package* package,
2129 Function_type* type, Location location);
2131 // Add a package. The location is the location of the import
2134 add_package(const std::string& alias, Package* package)
2136 Named_object* no = Named_object::make_package(alias, package);
2137 return this->add_named_object(no);
2140 // Define a type which was already declared.
2142 define_type(Named_object*, Named_type*);
2144 // Add a method to the list of objects. This is not added to the
2147 add_method(Named_object*);
2149 // Add a named object to this binding.
2151 add_named_object(Named_object* no)
2152 { return this->add_named_object_to_contour(&this->bindings_, no); }
2154 // Clear all names in file scope from the bindings.
2158 // Look up a name in this binding contour and in any enclosing
2159 // binding contours. This returns NULL if the name is not found.
2161 lookup(const std::string&) const;
2163 // Look up a name in this binding contour without looking in any
2164 // enclosing binding contours. Returns NULL if the name is not found.
2166 lookup_local(const std::string&) const;
2170 remove_binding(Named_object*);
2172 // Mark all variables as used. This is used for some types of parse
2177 // Traverse the tree. See the Traverse class.
2179 traverse(Traverse*, bool is_global);
2181 // Iterate over definitions. This does not include things which
2182 // were only declared.
2184 typedef std::vector<Named_object*>::const_iterator
2185 const_definitions_iterator;
2187 const_definitions_iterator
2188 begin_definitions() const
2189 { return this->named_objects_.begin(); }
2191 const_definitions_iterator
2192 end_definitions() const
2193 { return this->named_objects_.end(); }
2195 // Return the number of definitions.
2197 size_definitions() const
2198 { return this->named_objects_.size(); }
2200 // Return whether there are no definitions.
2202 empty_definitions() const
2203 { return this->named_objects_.empty(); }
2205 // Iterate over declarations. This is everything that has been
2206 // declared, which includes everything which has been defined.
2208 typedef Contour::const_iterator const_declarations_iterator;
2210 const_declarations_iterator
2211 begin_declarations() const
2212 { return this->bindings_.begin(); }
2214 const_declarations_iterator
2215 end_declarations() const
2216 { return this->bindings_.end(); }
2218 // Return the number of declarations.
2220 size_declarations() const
2221 { return this->bindings_.size(); }
2223 // Return whether there are no declarations.
2225 empty_declarations() const
2226 { return this->bindings_.empty(); }
2228 // Return the first declaration.
2231 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2235 add_named_object_to_contour(Contour*, Named_object*);
2238 new_definition(Named_object*, Named_object*);
2240 // Enclosing bindings.
2241 Bindings* enclosing_;
2242 // The list of objects.
2243 std::vector<Named_object*> named_objects_;
2244 // The mapping from names to objects.
2253 Label(const std::string& name)
2254 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2255 refs_(), is_used_(false), blabel_(NULL)
2258 // Return the label's name.
2261 { return this->name_; }
2263 // Return whether the label has been defined.
2266 { return !Linemap::is_unknown_location(this->location_); }
2268 // Return whether the label has been used.
2271 { return this->is_used_; }
2273 // Record that the label is used.
2276 { this->is_used_ = true; }
2278 // Return the location of the definition.
2281 { return this->location_; }
2283 // Return the bindings snapshot.
2286 { return this->snapshot_; }
2288 // Add a snapshot of a goto which refers to this label.
2290 add_snapshot_ref(Bindings_snapshot* snapshot)
2292 go_assert(Linemap::is_unknown_location(this->location_));
2293 this->refs_.push_back(snapshot);
2296 // Return the list of snapshots of goto statements which refer to
2298 const std::vector<Bindings_snapshot*>&
2300 { return this->refs_; }
2302 // Clear the references.
2306 // Define the label at LOCATION with the given bindings snapshot.
2308 define(Location location, Bindings_snapshot* snapshot)
2310 go_assert(Linemap::is_unknown_location(this->location_)
2311 && this->snapshot_ == NULL);
2312 this->location_ = location;
2313 this->snapshot_ = snapshot;
2316 // Return the backend representation for this label.
2318 get_backend_label(Translate_context*);
2320 // Return an expression for the address of this label. This is used
2321 // to get the return address of a deferred function to see whether
2322 // the function may call recover.
2324 get_addr(Translate_context*, Location location);
2327 // The name of the label.
2329 // The location of the definition. This is 0 if the label has not
2330 // yet been defined.
2332 // A snapshot of the set of bindings defined at this label, used to
2333 // issue errors about invalid goto statements.
2334 Bindings_snapshot* snapshot_;
2335 // A list of snapshots of goto statements which refer to this label.
2336 std::vector<Bindings_snapshot*> refs_;
2337 // Whether the label has been used.
2339 // The backend representation.
2343 // An unnamed label. These are used when lowering loops.
2348 Unnamed_label(Location location)
2349 : location_(location), blabel_(NULL)
2352 // Get the location where the label is defined.
2355 { return this->location_; }
2357 // Set the location where the label is defined.
2359 set_location(Location location)
2360 { this->location_ = location; }
2362 // Return a statement which defines this label.
2364 get_definition(Translate_context*);
2366 // Return a goto to this label from LOCATION.
2368 get_goto(Translate_context*, Location location);
2371 // Return the backend representation.
2373 get_blabel(Translate_context*);
2375 // The location where the label is defined.
2377 // The backend representation of this label.
2381 // An imported package.
2386 Package(const std::string& name, const std::string& unique_prefix,
2389 // The real name of this package. This may be different from the
2390 // name in the associated Named_object if the import statement used
2394 { return this->name_; }
2396 // Return the location of the import statement.
2399 { return this->location_; }
2401 // Get the unique prefix used for all symbols exported from this
2404 unique_prefix() const
2406 go_assert(!this->unique_prefix_.empty());
2407 return this->unique_prefix_;
2410 // The priority of this package. The init function of packages with
2411 // lower priority must be run before the init function of packages
2412 // with higher priority.
2415 { return this->priority_; }
2417 // Set the priority.
2419 set_priority(int priority);
2421 // Return the bindings.
2424 { return this->bindings_; }
2426 // Whether some symbol from the package was used.
2429 { return this->used_; }
2431 // Note that some symbol from this package was used.
2434 { this->used_ = true; }
2436 // Clear the used field for the next file.
2439 { this->used_ = false; }
2441 // Whether this package was imported in the current file.
2444 { return this->is_imported_; }
2446 // Note that this package was imported in the current file.
2449 { this->is_imported_ = true; }
2451 // Clear the imported field for the next file.
2454 { this->is_imported_ = false; }
2456 // Whether this package was imported with a name of "_".
2458 uses_sink_alias() const
2459 { return this->uses_sink_alias_; }
2461 // Note that this package was imported with a name of "_".
2463 set_uses_sink_alias()
2464 { this->uses_sink_alias_ = true; }
2466 // Clear the sink alias field for the next file.
2468 clear_uses_sink_alias()
2469 { this->uses_sink_alias_ = false; }
2471 // Look up a name in the package. Returns NULL if the name is not
2474 lookup(const std::string& name) const
2475 { return this->bindings_->lookup(name); }
2477 // Set the location of the package. This is used if it is seen in a
2478 // different import before it is really imported.
2480 set_location(Location location)
2481 { this->location_ = location; }
2483 // Add a constant to the package.
2485 add_constant(const Typed_identifier& tid, Expression* expr)
2486 { return this->bindings_->add_constant(tid, this, expr, 0); }
2488 // Add a type to the package.
2490 add_type(const std::string& name, Type* type, Location location)
2491 { return this->bindings_->add_type(name, this, type, location); }
2493 // Add a type declaration to the package.
2495 add_type_declaration(const std::string& name, Location location)
2496 { return this->bindings_->add_type_declaration(name, this, location); }
2498 // Add a variable to the package.
2500 add_variable(const std::string& name, Variable* variable)
2501 { return this->bindings_->add_variable(name, this, variable); }
2503 // Add a function declaration to the package.
2505 add_function_declaration(const std::string& name, Function_type* type,
2507 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2509 // Determine types of constants.
2514 // The real name of this package.
2516 // The unique prefix for all exported global symbols.
2517 std::string unique_prefix_;
2518 // The names in this package.
2519 Bindings* bindings_;
2520 // The priority of this package. A package has a priority higher
2521 // than the priority of all of the packages that it imports. This
2522 // is used to run init functions in the right order.
2524 // The location of the import statement.
2526 // True if some name from this package was used. This is mutable
2527 // because we can use a package even if we have a const pointer to
2530 // True if this package was imported in the current file.
2532 // True if this package was imported with a name of "_".
2533 bool uses_sink_alias_;
2536 // Return codes for the traversal functions. This is not an enum
2537 // because we want to be able to declare traversal functions in other
2538 // header files without including this one.
2540 // Continue traversal as usual.
2541 const int TRAVERSE_CONTINUE = -1;
2544 const int TRAVERSE_EXIT = 0;
2546 // Continue traversal, but skip components of the current object.
2547 // E.g., if this is returned by Traverse::statement, we do not
2548 // traverse the expressions in the statement even if
2549 // traverse_expressions is set in the traverse_mask.
2550 const int TRAVERSE_SKIP_COMPONENTS = 1;
2552 // This class is used when traversing the parse tree. The caller uses
2553 // a subclass which overrides functions as desired.
2558 // These bitmasks say what to traverse.
2559 static const unsigned int traverse_variables = 0x1;
2560 static const unsigned int traverse_constants = 0x2;
2561 static const unsigned int traverse_functions = 0x4;
2562 static const unsigned int traverse_blocks = 0x8;
2563 static const unsigned int traverse_statements = 0x10;
2564 static const unsigned int traverse_expressions = 0x20;
2565 static const unsigned int traverse_types = 0x40;
2567 Traverse(unsigned int traverse_mask)
2568 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2571 virtual ~Traverse();
2573 // The bitmask of what to traverse.
2575 traverse_mask() const
2576 { return this->traverse_mask_; }
2578 // Record that we are going to traverse a type. This returns true
2579 // if the type has already been seen in this traversal. This is
2580 // required because types, unlike expressions, can form a circular
2583 remember_type(const Type*);
2585 // Record that we are going to see an expression. This returns true
2586 // if the expression has already been seen in this traversal. This
2587 // is only needed for cases where multiple expressions can point to
2590 remember_expression(const Expression*);
2592 // These functions return one of the TRAVERSE codes defined above.
2594 // If traverse_variables is set in the mask, this is called for
2595 // every variable in the tree.
2597 variable(Named_object*);
2599 // If traverse_constants is set in the mask, this is called for
2600 // every named constant in the tree. The bool parameter is true for
2601 // a global constant.
2603 constant(Named_object*, bool);
2605 // If traverse_functions is set in the mask, this is called for
2606 // every function in the tree.
2608 function(Named_object*);
2610 // If traverse_blocks is set in the mask, this is called for every
2611 // block in the tree.
2615 // If traverse_statements is set in the mask, this is called for
2616 // every statement in the tree.
2618 statement(Block*, size_t* index, Statement*);
2620 // If traverse_expressions is set in the mask, this is called for
2621 // every expression in the tree.
2623 expression(Expression**);
2625 // If traverse_types is set in the mask, this is called for every
2626 // type in the tree.
2631 // A hash table for types we have seen during this traversal. Note
2632 // that this uses the default hash functions for pointers rather
2633 // than Type_hash_identical and Type_identical. This is because for
2634 // traversal we care about seeing a specific type structure. If
2635 // there are two separate instances of identical types, we want to
2637 typedef Unordered_set(const Type*) Types_seen;
2639 typedef Unordered_set(const Expression*) Expressions_seen;
2641 // Bitmask of what sort of objects to traverse.
2642 unsigned int traverse_mask_;
2643 // Types which have been seen in this traversal.
2644 Types_seen* types_seen_;
2645 // Expressions which have been seen in this traversal.
2646 Expressions_seen* expressions_seen_;
2649 // A class which makes it easier to insert new statements before the
2650 // current statement during a traversal.
2652 class Statement_inserter
2655 // Empty constructor.
2656 Statement_inserter()
2657 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2660 // Constructor for a statement in a block.
2661 Statement_inserter(Block* block, size_t *pindex)
2662 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2665 // Constructor for a global variable.
2666 Statement_inserter(Gogo* gogo, Variable* var)
2667 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2668 { go_assert(var->is_global()); }
2670 // We use the default copy constructor and assignment operator.
2672 // Insert S before the statement we are traversing, or before the
2673 // initialization expression of a global variable.
2675 insert(Statement* s);
2678 // The block that the statement is in.
2680 // The index of the statement that we are traversing.
2682 // The IR, needed when looking at an initializer expression for a
2685 // The global variable, when looking at an initializer expression.
2689 // When translating the gogo IR into the backend data structure, this
2690 // is the context we pass down the blocks and statements.
2692 class Translate_context
2695 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2697 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2698 block_(block), bblock_(bblock), is_const_(false)
2705 { return this->gogo_; }
2709 { return this->backend_; }
2713 { return this->function_; }
2717 { return this->block_; }
2721 { return this->bblock_; }
2725 { return this->is_const_; }
2727 // Make a constant context.
2730 { this->is_const_ = true; }
2733 // The IR for the entire compilation unit.
2735 // The generator for the backend data structures.
2737 // The function we are currently translating. NULL if not in a
2738 // function, e.g., the initializer of a global variable.
2739 Named_object* function_;
2740 // The block we are currently translating. NULL if not in a
2743 // The backend representation of the current block. NULL if block_
2746 // Whether this is being evaluated in a constant context. This is
2747 // used for type descriptor initializers.
2751 // Runtime error codes. These must match the values in
2752 // libgo/runtime/go-runtime-error.c.
2754 // Slice index out of bounds: negative or larger than the length of
2756 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2758 // Array index out of bounds.
2759 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2761 // String index out of bounds.
2762 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2764 // Slice slice out of bounds: negative or larger than the length of
2765 // the slice or high bound less than low bound.
2766 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2768 // Array slice out of bounds.
2769 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2771 // String slice out of bounds.
2772 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2774 // Dereference of nil pointer. This is used when there is a
2775 // dereference of a pointer to a very large struct or array, to ensure
2776 // that a gigantic array is not used a proxy to access random memory
2778 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2780 // Slice length or capacity out of bounds in make: negative or
2781 // overflow or length greater than capacity.
2782 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2784 // Map capacity out of bounds in make: negative or overflow.
2785 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2787 // Channel capacity out of bounds in make: negative or overflow.
2788 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2790 // This is used by some of the langhooks.
2791 extern Gogo* go_get_gogo();
2793 // Whether we have seen any errors. FIXME: Replace with a backend
2795 extern bool saw_errors();
2797 #endif // !defined(GO_GOGO_H)