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 // Record that VAR1 must be initialized after VAR2. This is used
388 // when VAR2 does not appear in VAR1's INIT or PREINIT.
390 record_var_depends_on(Variable* var1, Named_object* var2)
392 go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
393 this->var_deps_[var1] = var2;
396 // Return the variable that VAR depends on, or NULL if none.
398 var_depends_on(Variable* var) const
400 Var_deps::const_iterator p = this->var_deps_.find(var);
401 return p != this->var_deps_.end() ? p->second : NULL;
404 // Queue up a type-specific function to be written out. This is
405 // used when a type-specific function is needed when not at the top
408 queue_specific_type_function(Type* type, Named_type* name,
409 const std::string& hash_name,
410 Function_type* hash_fntype,
411 const std::string& equal_name,
412 Function_type* equal_fntype);
414 // Write out queued specific type functions.
416 write_specific_type_functions();
418 // Whether we are done writing out specific type functions.
420 specific_type_functions_are_written() const
421 { return this->specific_type_functions_are_written_; }
423 // Traverse the tree. See the Traverse class.
427 // Define the predeclared global names.
429 define_global_names();
431 // Verify and complete all types.
435 // Lower the parse tree.
439 // Lower all the statements in a block.
441 lower_block(Named_object* function, Block*);
443 // Lower an expression.
445 lower_expression(Named_object* function, Statement_inserter*, Expression**);
449 lower_constant(Named_object*);
451 // Finalize the method lists and build stub methods for named types.
455 // Work out the types to use for unspecified variables and
460 // Type check the program.
464 // Check the types in a single block. This is used for complicated
467 check_types_in_block(Block*);
469 // Check for return statements.
471 check_return_statements();
477 // Add an import control function for an imported package to the
480 add_import_init_fn(const std::string& package_name,
481 const std::string& init_name, int prio);
483 // Turn short-cut operators (&&, ||) into explicit if statements.
487 // Use temporary variables to force order of evaluation.
491 // Build thunks for functions which call recover.
493 build_recover_thunks();
495 // Simplify statements which might use thunks: go and defer
498 simplify_thunk_statements();
500 // Dump AST if -fgo-dump-ast is set
502 dump_ast(const char* basename);
504 // Convert named types to the backend representation.
506 convert_named_types();
508 // Convert named types in a list of bindings.
510 convert_named_types_in_bindings(Bindings*);
512 // True if named types have been converted to the backend
515 named_types_are_converted() const
516 { return this->named_types_are_converted_; }
518 // Write out the global values.
522 // Create trees for implicit builtin functions.
524 define_builtin_function_trees();
526 // Build a call to a builtin function. PDECL should point to a NULL
527 // initialized static pointer which will hold the fndecl. NAME is
528 // the name of the function. NARGS is the number of arguments.
529 // RETTYPE is the return type. It is followed by NARGS pairs of
530 // type and argument (both trees).
532 call_builtin(tree* pdecl, Location, const char* name, int nargs,
535 // Build a call to the runtime error function.
537 runtime_error(int code, Location);
539 // Build a builtin struct with a list of fields.
541 builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
544 // Mark a function declaration as a builtin library function.
546 mark_fndecl_as_builtin_library(tree fndecl);
548 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
549 // the slice. VALUES points to the values. COUNT is the size,
550 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
553 slice_constructor(tree slice_type_tree, tree values, tree count,
556 // Build required interface method tables.
558 build_interface_method_tables();
560 // Build an interface method table for a type: a list of function
561 // pointers, one for each interface method. This returns a decl.
563 interface_method_table_for_type(const Interface_type*, Named_type*,
566 // Return a tree which allocate SIZE bytes to hold values of type
569 allocate_memory(Type *type, tree size, Location);
571 // Return a type to use for pointer to const char.
573 const_char_pointer_type_tree();
575 // Build a string constant with the right type.
577 string_constant_tree(const std::string&);
579 // Build a Go string constant. This returns a pointer to the
582 go_string_constant_tree(const std::string&);
584 // Receive a value from a channel.
586 receive_from_channel(tree type_tree, tree type_descriptor_tree, tree channel,
589 // Make a trampoline which calls FNADDR passing CLOSURE.
591 make_trampoline(tree fnaddr, tree closure, Location);
594 // During parsing, we keep a stack of functions. Each function on
595 // the stack is one that we are currently parsing. For each
596 // function, we keep track of the current stack of blocks.
600 Named_object* function;
601 // The stack of active blocks in the function.
602 std::vector<Block*> blocks;
605 // The stack of functions.
606 typedef std::vector<Open_function> Open_functions;
608 // Set up the built-in unsafe package.
610 import_unsafe(const std::string&, bool is_exported, Location);
612 // Add a new imported package.
614 add_package(const std::string& real_name, const std::string& alias,
615 const std::string& unique_prefix, Location location);
617 // Return the current binding contour.
622 current_bindings() const;
624 // Get the name of the magic initialization function.
628 // Get the decl for the magic initialization function.
630 initialization_function_decl();
632 // Write the magic initialization function.
634 write_initialization_function(tree fndecl, tree init_stmt_list);
636 // Initialize imported packages.
640 // Register variables with the garbage collector.
642 register_gc_vars(const std::vector<Named_object*>&, tree*);
644 // Build a pointer to a Go string constant. This returns a pointer
647 ptr_go_string_constant_tree(const std::string&);
649 // Return the type of a trampoline.
651 trampoline_type_tree();
653 // Type used to map import names to packages.
654 typedef std::map<std::string, Package*> Imports;
656 // Type used to map package names to packages.
657 typedef std::map<std::string, Package*> Packages;
659 // Type used to map variables to the function calls that set them.
660 // This is used for initialization dependency analysis.
661 typedef std::map<Variable*, Named_object*> Var_deps;
663 // Type used to queue writing a type specific function.
664 struct Specific_type_function
668 std::string hash_name;
669 Function_type* hash_fntype;
670 std::string equal_name;
671 Function_type* equal_fntype;
673 Specific_type_function(Type* atype, Named_type* aname,
674 const std::string& ahash_name,
675 Function_type* ahash_fntype,
676 const std::string& aequal_name,
677 Function_type* aequal_fntype)
678 : type(atype), name(aname), hash_name(ahash_name),
679 hash_fntype(ahash_fntype), equal_name(aequal_name),
680 equal_fntype(aequal_fntype)
684 // The backend generator.
686 // The object used to keep track of file names and line numbers.
688 // The package we are compiling.
690 // The list of currently open functions during parsing.
691 Open_functions functions_;
692 // The global binding contour. This includes the builtin functions
693 // and the package we are compiling.
695 // Mapping from import file names to packages.
697 // Whether the magic unsafe package was imported.
698 bool imported_unsafe_;
699 // Mapping from package names we have seen to packages. This does
700 // not include the package we are compiling.
702 // The functions named "init", if there are any.
703 std::vector<Named_object*> init_functions_;
704 // A mapping from variables to the function calls that initialize
705 // them, if it is not stored in the variable's init or preinit.
706 // This is used for dependency analysis.
708 // Whether we need a magic initialization function.
710 // The name of the magic initialization function.
711 std::string init_fn_name_;
712 // A list of import control variables for packages that we import.
713 std::set<Import_init> imported_init_fns_;
714 // The unique prefix used for all global symbols.
715 std::string unique_prefix_;
716 // Whether an explicit unique prefix was set by -fgo-prefix.
717 bool unique_prefix_specified_;
718 // A list of types to verify.
719 std::vector<Type*> verify_types_;
720 // A list of interface types defined while parsing.
721 std::vector<Interface_type*> interface_types_;
722 // Type specific functions to write out.
723 std::vector<Specific_type_function*> specific_type_functions_;
724 // Whether we are done writing out specific type functions.
725 bool specific_type_functions_are_written_;
726 // Whether named types have been converted.
727 bool named_types_are_converted_;
730 // A block of statements.
735 Block(Block* enclosing, Location);
737 // Return the enclosing block.
740 { return this->enclosing_; }
742 // Return the bindings of the block.
745 { return this->bindings_; }
749 { return this->bindings_; }
751 // Look at the block's statements.
752 const std::vector<Statement*>*
754 { return &this->statements_; }
756 // Return the start location. This is normally the location of the
757 // left curly brace which starts the block.
759 start_location() const
760 { return this->start_location_; }
762 // Return the end location. This is normally the location of the
763 // right curly brace which ends the block.
766 { return this->end_location_; }
768 // Add a statement to the block.
770 add_statement(Statement*);
772 // Add a statement to the front of the block.
774 add_statement_at_front(Statement*);
776 // Replace a statement in a block.
778 replace_statement(size_t index, Statement*);
780 // Add a Statement before statement number INDEX.
782 insert_statement_before(size_t index, Statement*);
784 // Add a Statement after statement number INDEX.
786 insert_statement_after(size_t index, Statement*);
788 // Set the end location of the block.
790 set_end_location(Location location)
791 { this->end_location_ = location; }
793 // Traverse the tree.
797 // Set final types for unspecified variables and constants.
801 // Return true if execution of this block may fall through to the
804 may_fall_through() const;
806 // Convert the block to the backend representation.
808 get_backend(Translate_context*);
810 // Iterate over statements.
812 typedef std::vector<Statement*>::iterator iterator;
816 { return this->statements_.begin(); }
820 { return this->statements_.end(); }
825 // Statements in the block.
826 std::vector<Statement*> statements_;
829 // Location of start of block.
830 Location start_location_;
831 // Location of end of block.
832 Location end_location_;
840 Function(Function_type* type, Function*, Block*, Location);
842 // Return the function's type.
845 { return this->type_; }
847 // Return the enclosing function if there is one.
850 { return this->enclosing_; }
852 // Set the enclosing function. This is used when building thunks
853 // for functions which call recover.
855 set_enclosing(Function* enclosing)
857 go_assert(this->enclosing_ == NULL);
858 this->enclosing_ = enclosing;
861 // The result variables.
862 typedef std::vector<Named_object*> Results;
864 // Create the result variables in the outer block.
866 create_result_variables(Gogo*);
868 // Update the named result variables when cloning a function which
871 update_result_variables();
873 // Return the result variables.
876 { return this->results_; }
878 // Whether the result variables have names.
880 results_are_named() const
881 { return this->results_are_named_; }
883 // Add a new field to the closure variable.
885 add_closure_field(Named_object* var, Location loc)
886 { this->closure_fields_.push_back(std::make_pair(var, loc)); }
888 // Whether this function needs a closure.
890 needs_closure() const
891 { return !this->closure_fields_.empty(); }
893 // Return the closure variable, creating it if necessary. This is
894 // passed to the function as a static chain parameter.
898 // Set the closure variable. This is used when building thunks for
899 // functions which call recover.
901 set_closure_var(Named_object* v)
903 go_assert(this->closure_var_ == NULL);
904 this->closure_var_ = v;
907 // Return the variable for a reference to field INDEX in the closure
910 enclosing_var(unsigned int index)
912 go_assert(index < this->closure_fields_.size());
913 return closure_fields_[index].first;
916 // Set the type of the closure variable if there is one.
920 // Get the block of statements associated with the function.
923 { return this->block_; }
925 // Get the location of the start of the function.
928 { return this->location_; }
930 // Return whether this function is actually a method.
934 // Add a label definition to the function.
936 add_label_definition(Gogo*, const std::string& label_name, Location);
938 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
939 // if we should report errors for a goto from the current location
940 // to the label location.
942 add_label_reference(Gogo*, const std::string& label_name,
943 Location, bool issue_goto_errors);
945 // Warn about labels that are defined but not used.
947 check_labels() const;
949 // Whether this function calls the predeclared recover function.
951 calls_recover() const
952 { return this->calls_recover_; }
954 // Record that this function calls the predeclared recover function.
955 // This is set during the lowering pass.
958 { this->calls_recover_ = true; }
960 // Whether this is a recover thunk function.
962 is_recover_thunk() const
963 { return this->is_recover_thunk_; }
965 // Record that this is a thunk built for a function which calls
968 set_is_recover_thunk()
969 { this->is_recover_thunk_ = true; }
971 // Whether this function already has a recover thunk.
973 has_recover_thunk() const
974 { return this->has_recover_thunk_; }
976 // Record that this function already has a recover thunk.
978 set_has_recover_thunk()
979 { this->has_recover_thunk_ = true; }
981 // Swap with another function. Used only for the thunk which calls
984 swap_for_recover(Function *);
986 // Traverse the tree.
990 // Determine types in the function.
994 // Return the function's decl given an identifier.
996 get_or_make_decl(Gogo*, Named_object*, tree id);
998 // Return the function's decl after it has been built.
1002 go_assert(this->fndecl_ != NULL);
1003 return this->fndecl_;
1006 // Set the function decl to hold a tree of the function code.
1008 build_tree(Gogo*, Named_object*);
1010 // Get the value to return when not explicitly specified. May also
1011 // add statements to execute first to STMT_LIST.
1013 return_value(Gogo*, Named_object*, Location, tree* stmt_list) const;
1015 // Get a tree for the variable holding the defer stack.
1017 defer_stack(Location);
1019 // Export the function.
1021 export_func(Export*, const std::string& name) const;
1023 // Export a function with a type.
1025 export_func_with_type(Export*, const std::string& name,
1026 const Function_type*);
1028 // Import a function.
1030 import_func(Import*, std::string* pname, Typed_identifier** receiver,
1031 Typed_identifier_list** pparameters,
1032 Typed_identifier_list** presults, bool* is_varargs);
1035 // Type for mapping from label names to Label objects.
1036 typedef Unordered_map(std::string, Label*) Labels;
1039 make_receiver_parm_decl(Gogo*, Named_object*, tree);
1042 copy_parm_to_heap(Gogo*, Named_object*, tree);
1045 build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1047 typedef std::vector<std::pair<Named_object*,
1048 Location> > Closure_fields;
1050 // The function's type.
1051 Function_type* type_;
1052 // The enclosing function. This is NULL when there isn't one, which
1053 // is the normal case.
1054 Function* enclosing_;
1055 // The result variables, if any.
1057 // If there is a closure, this is the list of variables which appear
1058 // in the closure. This is created by the parser, and then resolved
1059 // to a real type when we lower parse trees.
1060 Closure_fields closure_fields_;
1061 // The closure variable, passed as a parameter using the static
1062 // chain parameter. Normally NULL.
1063 Named_object* closure_var_;
1064 // The outer block of statements in the function.
1066 // The source location of the start of the function.
1068 // Labels defined or referenced in the function.
1070 // The function decl.
1072 // The defer stack variable. A pointer to this variable is used to
1073 // distinguish the defer stack for one function from another. This
1074 // is NULL unless we actually need a defer stack.
1075 Temporary_statement* defer_stack_;
1076 // True if the result variables are named.
1077 bool results_are_named_;
1078 // True if this function calls the predeclared recover function.
1079 bool calls_recover_;
1080 // True if this a thunk built for a function which calls recover.
1081 bool is_recover_thunk_;
1082 // True if this function already has a recover thunk.
1083 bool has_recover_thunk_;
1086 // A snapshot of the current binding state.
1088 class Bindings_snapshot
1091 Bindings_snapshot(const Block*, Location);
1093 // Report any errors appropriate for a goto from the current binding
1094 // state of B to this one.
1096 check_goto_from(const Block* b, Location);
1098 // Report any errors appropriate for a goto from this binding state
1099 // to the current state of B.
1101 check_goto_to(const Block* b);
1105 check_goto_block(Location, const Block*, const Block*, size_t*);
1108 check_goto_defs(Location, const Block*, size_t, size_t);
1110 // The current block.
1111 const Block* block_;
1112 // The number of names currently defined in each open block.
1113 // Element 0 is this->block_, element 1 is
1114 // this->block_->enclosing(), etc.
1115 std::vector<size_t> counts_;
1116 // The location where this snapshot was taken.
1120 // A function declaration.
1122 class Function_declaration
1125 Function_declaration(Function_type* fntype, Location location)
1126 : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1131 { return this->fntype_; }
1135 { return this->location_; }
1139 { return this->asm_name_; }
1141 // Set the assembler name.
1143 set_asm_name(const std::string& asm_name)
1144 { this->asm_name_ = asm_name; }
1146 // Return a decl for the function given an identifier.
1148 get_or_make_decl(Gogo*, Named_object*, tree id);
1150 // Export a function declaration.
1152 export_func(Export* exp, const std::string& name) const
1153 { Function::export_func_with_type(exp, name, this->fntype_); }
1156 // The type of the function.
1157 Function_type* fntype_;
1158 // The location of the declaration.
1160 // The assembler name: this is the name to use in references to the
1161 // function. This is normally empty.
1162 std::string asm_name_;
1163 // The function decl if needed.
1172 Variable(Type*, Expression*, bool is_global, bool is_parameter,
1173 bool is_receiver, Location);
1175 // Get the type of the variable.
1182 // Return whether the type is defined yet.
1186 // Get the initial value.
1189 { return this->init_; }
1191 // Return whether there are any preinit statements.
1193 has_pre_init() const
1194 { return this->preinit_ != NULL; }
1196 // Return the preinit statements if any.
1199 { return this->preinit_; }
1201 // Return whether this is a global variable.
1204 { return this->is_global_; }
1206 // Return whether this is a function parameter.
1208 is_parameter() const
1209 { return this->is_parameter_; }
1211 // Return whether this is the receiver parameter of a method.
1214 { return this->is_receiver_; }
1216 // Change this parameter to be a receiver. This is used when
1217 // creating the thunks created for functions which call recover.
1221 go_assert(this->is_parameter_);
1222 this->is_receiver_ = true;
1225 // Change this parameter to not be a receiver. This is used when
1226 // creating the thunks created for functions which call recover.
1228 set_is_not_receiver()
1230 go_assert(this->is_parameter_);
1231 this->is_receiver_ = false;
1234 // Return whether this is the varargs parameter of a function.
1236 is_varargs_parameter() const
1237 { return this->is_varargs_parameter_; }
1239 // Whether this variable's address is taken.
1241 is_address_taken() const
1242 { return this->is_address_taken_; }
1244 // Whether this variable should live in the heap.
1247 { return this->is_address_taken_ && !this->is_global_; }
1249 // Note that something takes the address of this variable.
1252 { this->is_address_taken_ = true; }
1254 // Return whether the address is taken but does not escape.
1256 is_non_escaping_address_taken() const
1257 { return this->is_non_escaping_address_taken_; }
1259 // Note that something takes the address of this variable such that
1260 // the address does not escape the function.
1262 set_non_escaping_address_taken()
1263 { this->is_non_escaping_address_taken_ = true; }
1265 // Get the source location of the variable's declaration.
1268 { return this->location_; }
1270 // Record that this is the varargs parameter of a function.
1272 set_is_varargs_parameter()
1274 go_assert(this->is_parameter_);
1275 this->is_varargs_parameter_ = true;
1278 // Return whether the variable has been used.
1281 { return this->is_used_; }
1283 // Mark that the variable has been used.
1286 { this->is_used_ = true; }
1288 // Clear the initial value; used for error handling.
1291 { this->init_ = NULL; }
1293 // Set the initial value; used for converting shortcuts.
1295 set_init(Expression* init)
1296 { this->init_ = init; }
1298 // Get the preinit block, a block of statements to be run before the
1299 // initialization expression.
1301 preinit_block(Gogo*);
1303 // Add a statement to be run before the initialization expression.
1304 // This is only used for global variables.
1306 add_preinit_statement(Gogo*, Statement*);
1308 // Lower the initialization expression after parsing is complete.
1310 lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1312 // A special case: the init value is used only to determine the
1313 // type. This is used if the variable is defined using := with the
1314 // comma-ok form of a map index or a receive expression. The init
1315 // value is actually the map index expression or receive expression.
1316 // We use this because we may not know the right type at parse time.
1318 set_type_from_init_tuple()
1319 { this->type_from_init_tuple_ = true; }
1321 // Another special case: the init value is used only to determine
1322 // the type. This is used if the variable is defined using := with
1323 // a range clause. The init value is the range expression. The
1324 // type of the variable is the index type of the range expression
1325 // (i.e., the first value returned by a range).
1327 set_type_from_range_index()
1328 { this->type_from_range_index_ = true; }
1330 // Another special case: like set_type_from_range_index, but the
1331 // type is the value type of the range expression (i.e., the second
1332 // value returned by a range).
1334 set_type_from_range_value()
1335 { this->type_from_range_value_ = true; }
1337 // Another special case: the init value is used only to determine
1338 // the type. This is used if the variable is defined using := with
1339 // a case in a select statement. The init value is the channel.
1340 // The type of the variable is the channel's element type.
1342 set_type_from_chan_element()
1343 { this->type_from_chan_element_ = true; }
1345 // After we lower the select statement, we once again set the type
1346 // from the initialization expression.
1348 clear_type_from_chan_element()
1350 go_assert(this->type_from_chan_element_);
1351 this->type_from_chan_element_ = false;
1354 // Note that this variable was created for a type switch clause.
1356 set_is_type_switch_var()
1357 { this->is_type_switch_var_ = true; }
1359 // Traverse the initializer expression.
1361 traverse_expression(Traverse*, unsigned int traverse_mask);
1363 // Determine the type of the variable if necessary.
1367 // Get the backend representation of the variable.
1369 get_backend_variable(Gogo*, Named_object*, const Package*,
1370 const std::string&);
1372 // Get the initial value of the variable as a tree. This may only
1373 // be called if has_pre_init() returns false.
1375 get_init_tree(Gogo*, Named_object* function);
1377 // Return a series of statements which sets the value of the
1378 // variable in DECL. This should only be called is has_pre_init()
1379 // returns true. DECL may be NULL for a sink variable.
1381 get_init_block(Gogo*, Named_object* function, tree decl);
1383 // Export the variable.
1385 export_var(Export*, const std::string& name) const;
1387 // Import a variable.
1389 import_var(Import*, std::string* pname, Type** ptype);
1392 // The type of a tuple.
1394 type_from_tuple(Expression*, bool) const;
1396 // The type of a range.
1398 type_from_range(Expression*, bool, bool) const;
1400 // The element type of a channel.
1402 type_from_chan_element(Expression*, bool) const;
1404 // The variable's type. This may be NULL if the type is set from
1407 // The initial value. This may be NULL if the variable should be
1408 // initialized to the default value for the type.
1410 // Statements to run before the init statement.
1412 // Location of variable definition.
1414 // Backend representation.
1415 Bvariable* backend_;
1416 // Whether this is a global variable.
1417 bool is_global_ : 1;
1418 // Whether this is a function parameter.
1419 bool is_parameter_ : 1;
1420 // Whether this is the receiver parameter of a method.
1421 bool is_receiver_ : 1;
1422 // Whether this is the varargs parameter of a function.
1423 bool is_varargs_parameter_ : 1;
1424 // Whether this variable is ever referenced.
1426 // Whether something takes the address of this variable. For a
1427 // local variable this implies that the variable has to be on the
1429 bool is_address_taken_ : 1;
1430 // Whether something takes the address of this variable such that
1431 // the address does not escape the function.
1432 bool is_non_escaping_address_taken_ : 1;
1433 // True if we have seen this variable in a traversal.
1435 // True if we have lowered the initialization expression.
1436 bool init_is_lowered_ : 1;
1437 // True if init is a tuple used to set the type.
1438 bool type_from_init_tuple_ : 1;
1439 // True if init is a range clause and the type is the index type.
1440 bool type_from_range_index_ : 1;
1441 // True if init is a range clause and the type is the value type.
1442 bool type_from_range_value_ : 1;
1443 // True if init is a channel and the type is the channel's element type.
1444 bool type_from_chan_element_ : 1;
1445 // True if this is a variable created for a type switch case.
1446 bool is_type_switch_var_ : 1;
1447 // True if we have determined types.
1448 bool determined_type_ : 1;
1451 // A variable which is really the name for a function return value, or
1454 class Result_variable
1457 Result_variable(Type* type, Function* function, int index,
1459 : type_(type), function_(function), index_(index), location_(location),
1460 backend_(NULL), is_address_taken_(false),
1461 is_non_escaping_address_taken_(false)
1464 // Get the type of the result variable.
1467 { return this->type_; }
1469 // Get the function that this is associated with.
1472 { return this->function_; }
1474 // Index in the list of function results.
1477 { return this->index_; }
1479 // The location of the variable definition.
1482 { return this->location_; }
1484 // Whether this variable's address is taken.
1486 is_address_taken() const
1487 { return this->is_address_taken_; }
1489 // Note that something takes the address of this variable.
1492 { this->is_address_taken_ = true; }
1494 // Return whether the address is taken but does not escape.
1496 is_non_escaping_address_taken() const
1497 { return this->is_non_escaping_address_taken_; }
1499 // Note that something takes the address of this variable such that
1500 // the address does not escape the function.
1502 set_non_escaping_address_taken()
1503 { this->is_non_escaping_address_taken_ = true; }
1505 // Whether this variable should live in the heap.
1508 { return this->is_address_taken_; }
1510 // Set the function. This is used when cloning functions which call
1513 set_function(Function* function)
1514 { this->function_ = function; }
1516 // Get the backend representation of the variable.
1518 get_backend_variable(Gogo*, Named_object*, const std::string&);
1521 // Type of result variable.
1523 // Function with which this is associated.
1524 Function* function_;
1525 // Index in list of results.
1527 // Where the result variable is defined.
1529 // Backend representation.
1530 Bvariable* backend_;
1531 // Whether something takes the address of this variable.
1532 bool is_address_taken_;
1533 // Whether something takes the address of this variable such that
1534 // the address does not escape the function.
1535 bool is_non_escaping_address_taken_;
1538 // The value we keep for a named constant. This lets us hold a type
1539 // and an expression.
1541 class Named_constant
1544 Named_constant(Type* type, Expression* expr, int iota_value,
1546 : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1552 { return this->type_; }
1556 { return this->expr_; }
1560 { return this->iota_value_; }
1564 { return this->location_; }
1566 // Whether we are lowering.
1569 { return this->lowering_; }
1571 // Set that we are lowering.
1574 { this->lowering_ = true; }
1576 // We are no longer lowering.
1579 { this->lowering_ = false; }
1581 // Traverse the expression.
1583 traverse_expression(Traverse*);
1585 // Determine the type of the constant if necessary.
1589 // Indicate that we found and reported an error for this constant.
1593 // Export the constant.
1595 export_const(Export*, const std::string& name) const;
1597 // Import a constant.
1599 import_const(Import*, std::string*, Type**, Expression**);
1602 // The type of the constant.
1604 // The expression for the constant.
1606 // If the predeclared constant iota is used in EXPR_, this is the
1607 // value it will have. We do this because at parse time we don't
1608 // know whether the name "iota" will refer to the predeclared
1609 // constant or to something else. We put in the right value in when
1612 // The location of the definition.
1614 // Whether we are currently lowering this constant.
1618 // A type declaration.
1620 class Type_declaration
1623 Type_declaration(Location location)
1624 : location_(location), in_function_(NULL), methods_(),
1625 issued_warning_(false)
1628 // Return the location.
1631 { return this->location_; }
1633 // Return the function in which this type is declared. This will
1634 // return NULL for a type declared in global scope.
1637 { return this->in_function_; }
1639 // Set the function in which this type is declared.
1641 set_in_function(Named_object* f)
1642 { this->in_function_ = f; }
1644 // Add a method to this type. This is used when methods are defined
1647 add_method(const std::string& name, Function* function);
1649 // Add a method declaration to this type.
1651 add_method_declaration(const std::string& name, Package*,
1652 Function_type* type, Location location);
1654 // Return whether any methods were defined.
1656 has_methods() const;
1658 // Define methods when the real type is known.
1660 define_methods(Named_type*);
1662 // This is called if we are trying to use this type. It returns
1663 // true if we should issue a warning.
1668 typedef std::vector<Named_object*> Methods;
1670 // The location of the type declaration.
1672 // If this type is declared in a function, a pointer back to the
1673 // function in which it is defined.
1674 Named_object* in_function_;
1675 // Methods defined before the type is defined.
1677 // True if we have issued a warning about a use of this type
1678 // declaration when it is undefined.
1679 bool issued_warning_;
1682 // An unknown object. These are created by the parser for forward
1683 // references to names which have not been seen before. In a correct
1684 // program, these will always point to a real definition by the end of
1685 // the parse. Because they point to another Named_object, these may
1686 // only be referenced by Unknown_expression objects.
1691 Unknown_name(Location location)
1692 : location_(location), real_named_object_(NULL)
1695 // Return the location where this name was first seen.
1698 { return this->location_; }
1700 // Return the real named object that this points to, or NULL if it
1701 // was never resolved.
1703 real_named_object() const
1704 { return this->real_named_object_; }
1706 // Set the real named object that this points to.
1708 set_real_named_object(Named_object* no);
1711 // The location where this name was first seen.
1713 // The real named object when it is known.
1718 // A named object named. This is the result of a declaration. We
1719 // don't use a superclass because they all have to be handled
1727 // An uninitialized Named_object. We should never see this.
1728 NAMED_OBJECT_UNINITIALIZED,
1729 // An erroneous name. This indicates a parse error, to avoid
1730 // later errors about undefined references.
1731 NAMED_OBJECT_ERRONEOUS,
1732 // An unknown name. This is used for forward references. In a
1733 // correct program, these will all be resolved by the end of the
1735 NAMED_OBJECT_UNKNOWN,
1740 // A forward type declaration.
1741 NAMED_OBJECT_TYPE_DECLARATION,
1744 // A result variable in a function.
1745 NAMED_OBJECT_RESULT_VAR,
1746 // The blank identifier--the special variable named _.
1750 // A forward func declaration.
1751 NAMED_OBJECT_FUNC_DECLARATION,
1753 NAMED_OBJECT_PACKAGE
1756 // Return the classification.
1758 classification() const
1759 { return this->classification_; }
1764 is_erroneous() const
1765 { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1769 { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1773 { return this->classification_ == NAMED_OBJECT_CONST; }
1777 { return this->classification_ == NAMED_OBJECT_TYPE; }
1780 is_type_declaration() const
1781 { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1785 { return this->classification_ == NAMED_OBJECT_VAR; }
1788 is_result_variable() const
1789 { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1793 { return this->classification_ == NAMED_OBJECT_SINK; }
1797 { return this->classification_ == NAMED_OBJECT_FUNC; }
1800 is_function_declaration() const
1801 { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1805 { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1809 static Named_object*
1810 make_erroneous_name(const std::string& name)
1811 { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1813 static Named_object*
1814 make_unknown_name(const std::string& name, Location);
1816 static Named_object*
1817 make_constant(const Typed_identifier&, const Package*, Expression*,
1820 static Named_object*
1821 make_type(const std::string&, const Package*, Type*, Location);
1823 static Named_object*
1824 make_type_declaration(const std::string&, const Package*, Location);
1826 static Named_object*
1827 make_variable(const std::string&, const Package*, Variable*);
1829 static Named_object*
1830 make_result_variable(const std::string&, Result_variable*);
1832 static Named_object*
1835 static Named_object*
1836 make_function(const std::string&, const Package*, Function*);
1838 static Named_object*
1839 make_function_declaration(const std::string&, const Package*, Function_type*,
1842 static Named_object*
1843 make_package(const std::string& alias, Package* package);
1850 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1851 return this->u_.unknown_value;
1855 unknown_value() const
1857 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1858 return this->u_.unknown_value;
1864 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1865 return this->u_.const_value;
1868 const Named_constant*
1871 go_assert(this->classification_ == NAMED_OBJECT_CONST);
1872 return this->u_.const_value;
1878 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1879 return this->u_.type_value;
1885 go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1886 return this->u_.type_value;
1890 type_declaration_value()
1892 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1893 return this->u_.type_declaration;
1896 const Type_declaration*
1897 type_declaration_value() const
1899 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1900 return this->u_.type_declaration;
1906 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1907 return this->u_.var_value;
1913 go_assert(this->classification_ == NAMED_OBJECT_VAR);
1914 return this->u_.var_value;
1920 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1921 return this->u_.result_var_value;
1924 const Result_variable*
1925 result_var_value() const
1927 go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1928 return this->u_.result_var_value;
1934 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1935 return this->u_.func_value;
1941 go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1942 return this->u_.func_value;
1945 Function_declaration*
1946 func_declaration_value()
1948 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1949 return this->u_.func_declaration_value;
1952 const Function_declaration*
1953 func_declaration_value() const
1955 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1956 return this->u_.func_declaration_value;
1962 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1963 return this->u_.package_value;
1967 package_value() const
1969 go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1970 return this->u_.package_value;
1975 { return this->name_; }
1977 // Return the name to use in an error message. The difference is
1978 // that if this Named_object is defined in a different package, this
1979 // will return PACKAGE.NAME.
1981 message_name() const;
1985 { return this->package_; }
1987 // Resolve an unknown value if possible. This returns the same
1988 // Named_object or a new one.
1992 Named_object* ret = this;
1993 if (this->is_unknown())
1995 Named_object* r = this->unknown_value()->real_named_object();
2005 const Named_object* ret = this;
2006 if (this->is_unknown())
2008 const Named_object* r = this->unknown_value()->real_named_object();
2015 // The location where this object was defined or referenced.
2019 // Convert a variable to the backend representation.
2021 get_backend_variable(Gogo*, Named_object* function);
2023 // Return a tree for the external identifier for this object.
2027 // Return a tree representing this object.
2029 get_tree(Gogo*, Named_object* function);
2031 // Define a type declaration.
2033 set_type_value(Named_type*);
2035 // Define a function declaration.
2037 set_function_value(Function*);
2039 // Declare an unknown name as a type declaration.
2043 // Export this object.
2045 export_named_object(Export*) const;
2048 Named_object(const std::string&, const Package*, Classification);
2050 // The name of the object.
2052 // The package that this object is in. This is NULL if it is in the
2053 // file we are compiling.
2054 const Package* package_;
2055 // The type of object this is.
2056 Classification classification_;
2060 Unknown_name* unknown_value;
2061 Named_constant* const_value;
2062 Named_type* type_value;
2063 Type_declaration* type_declaration;
2064 Variable* var_value;
2065 Result_variable* result_var_value;
2066 Function* func_value;
2067 Function_declaration* func_declaration_value;
2068 Package* package_value;
2070 // The DECL tree for this object if we have already converted it.
2074 // A binding contour. This binds names to objects.
2079 // Type for mapping from names to objects.
2080 typedef Unordered_map(std::string, Named_object*) Contour;
2082 Bindings(Bindings* enclosing);
2084 // Add an erroneous name.
2086 add_erroneous_name(const std::string& name)
2087 { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2089 // Add an unknown name.
2091 add_unknown_name(const std::string& name, Location location)
2093 return this->add_named_object(Named_object::make_unknown_name(name,
2099 add_constant(const Typed_identifier& tid, const Package* package,
2100 Expression* expr, int iota_value)
2102 return this->add_named_object(Named_object::make_constant(tid, package,
2109 add_type(const std::string& name, const Package* package, Type* type,
2112 return this->add_named_object(Named_object::make_type(name, package, type,
2116 // Add a named type. This is used for builtin types, and to add an
2117 // imported type to the global scope.
2119 add_named_type(Named_type* named_type);
2121 // Add a type declaration.
2123 add_type_declaration(const std::string& name, const Package* package,
2126 Named_object* no = Named_object::make_type_declaration(name, package,
2128 return this->add_named_object(no);
2133 add_variable(const std::string& name, const Package* package,
2136 return this->add_named_object(Named_object::make_variable(name, package,
2140 // Add a result variable.
2142 add_result_variable(const std::string& name, Result_variable* result)
2144 return this->add_named_object(Named_object::make_result_variable(name,
2150 add_function(const std::string& name, const Package*, Function* function);
2152 // Add a function declaration.
2154 add_function_declaration(const std::string& name, const Package* package,
2155 Function_type* type, Location location);
2157 // Add a package. The location is the location of the import
2160 add_package(const std::string& alias, Package* package)
2162 Named_object* no = Named_object::make_package(alias, package);
2163 return this->add_named_object(no);
2166 // Define a type which was already declared.
2168 define_type(Named_object*, Named_type*);
2170 // Add a method to the list of objects. This is not added to the
2173 add_method(Named_object*);
2175 // Add a named object to this binding.
2177 add_named_object(Named_object* no)
2178 { return this->add_named_object_to_contour(&this->bindings_, no); }
2180 // Clear all names in file scope from the bindings.
2184 // Look up a name in this binding contour and in any enclosing
2185 // binding contours. This returns NULL if the name is not found.
2187 lookup(const std::string&) const;
2189 // Look up a name in this binding contour without looking in any
2190 // enclosing binding contours. Returns NULL if the name is not found.
2192 lookup_local(const std::string&) const;
2196 remove_binding(Named_object*);
2198 // Mark all variables as used. This is used for some types of parse
2203 // Traverse the tree. See the Traverse class.
2205 traverse(Traverse*, bool is_global);
2207 // Iterate over definitions. This does not include things which
2208 // were only declared.
2210 typedef std::vector<Named_object*>::const_iterator
2211 const_definitions_iterator;
2213 const_definitions_iterator
2214 begin_definitions() const
2215 { return this->named_objects_.begin(); }
2217 const_definitions_iterator
2218 end_definitions() const
2219 { return this->named_objects_.end(); }
2221 // Return the number of definitions.
2223 size_definitions() const
2224 { return this->named_objects_.size(); }
2226 // Return whether there are no definitions.
2228 empty_definitions() const
2229 { return this->named_objects_.empty(); }
2231 // Iterate over declarations. This is everything that has been
2232 // declared, which includes everything which has been defined.
2234 typedef Contour::const_iterator const_declarations_iterator;
2236 const_declarations_iterator
2237 begin_declarations() const
2238 { return this->bindings_.begin(); }
2240 const_declarations_iterator
2241 end_declarations() const
2242 { return this->bindings_.end(); }
2244 // Return the number of declarations.
2246 size_declarations() const
2247 { return this->bindings_.size(); }
2249 // Return whether there are no declarations.
2251 empty_declarations() const
2252 { return this->bindings_.empty(); }
2254 // Return the first declaration.
2257 { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2261 add_named_object_to_contour(Contour*, Named_object*);
2264 new_definition(Named_object*, Named_object*);
2266 // Enclosing bindings.
2267 Bindings* enclosing_;
2268 // The list of objects.
2269 std::vector<Named_object*> named_objects_;
2270 // The mapping from names to objects.
2279 Label(const std::string& name)
2280 : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2281 refs_(), is_used_(false), blabel_(NULL)
2284 // Return the label's name.
2287 { return this->name_; }
2289 // Return whether the label has been defined.
2292 { return !Linemap::is_unknown_location(this->location_); }
2294 // Return whether the label has been used.
2297 { return this->is_used_; }
2299 // Record that the label is used.
2302 { this->is_used_ = true; }
2304 // Return the location of the definition.
2307 { return this->location_; }
2309 // Return the bindings snapshot.
2312 { return this->snapshot_; }
2314 // Add a snapshot of a goto which refers to this label.
2316 add_snapshot_ref(Bindings_snapshot* snapshot)
2318 go_assert(Linemap::is_unknown_location(this->location_));
2319 this->refs_.push_back(snapshot);
2322 // Return the list of snapshots of goto statements which refer to
2324 const std::vector<Bindings_snapshot*>&
2326 { return this->refs_; }
2328 // Clear the references.
2332 // Define the label at LOCATION with the given bindings snapshot.
2334 define(Location location, Bindings_snapshot* snapshot)
2336 go_assert(Linemap::is_unknown_location(this->location_)
2337 && this->snapshot_ == NULL);
2338 this->location_ = location;
2339 this->snapshot_ = snapshot;
2342 // Return the backend representation for this label.
2344 get_backend_label(Translate_context*);
2346 // Return an expression for the address of this label. This is used
2347 // to get the return address of a deferred function to see whether
2348 // the function may call recover.
2350 get_addr(Translate_context*, Location location);
2353 // The name of the label.
2355 // The location of the definition. This is 0 if the label has not
2356 // yet been defined.
2358 // A snapshot of the set of bindings defined at this label, used to
2359 // issue errors about invalid goto statements.
2360 Bindings_snapshot* snapshot_;
2361 // A list of snapshots of goto statements which refer to this label.
2362 std::vector<Bindings_snapshot*> refs_;
2363 // Whether the label has been used.
2365 // The backend representation.
2369 // An unnamed label. These are used when lowering loops.
2374 Unnamed_label(Location location)
2375 : location_(location), blabel_(NULL)
2378 // Get the location where the label is defined.
2381 { return this->location_; }
2383 // Set the location where the label is defined.
2385 set_location(Location location)
2386 { this->location_ = location; }
2388 // Return a statement which defines this label.
2390 get_definition(Translate_context*);
2392 // Return a goto to this label from LOCATION.
2394 get_goto(Translate_context*, Location location);
2397 // Return the backend representation.
2399 get_blabel(Translate_context*);
2401 // The location where the label is defined.
2403 // The backend representation of this label.
2407 // An imported package.
2412 Package(const std::string& name, const std::string& unique_prefix,
2415 // The real name of this package. This may be different from the
2416 // name in the associated Named_object if the import statement used
2420 { return this->name_; }
2422 // Return the location of the import statement.
2425 { return this->location_; }
2427 // Get the unique prefix used for all symbols exported from this
2430 unique_prefix() const
2432 go_assert(!this->unique_prefix_.empty());
2433 return this->unique_prefix_;
2436 // The priority of this package. The init function of packages with
2437 // lower priority must be run before the init function of packages
2438 // with higher priority.
2441 { return this->priority_; }
2443 // Set the priority.
2445 set_priority(int priority);
2447 // Return the bindings.
2450 { return this->bindings_; }
2452 // Whether some symbol from the package was used.
2455 { return this->used_; }
2457 // Note that some symbol from this package was used.
2460 { this->used_ = true; }
2462 // Clear the used field for the next file.
2465 { this->used_ = false; }
2467 // Whether this package was imported in the current file.
2470 { return this->is_imported_; }
2472 // Note that this package was imported in the current file.
2475 { this->is_imported_ = true; }
2477 // Clear the imported field for the next file.
2480 { this->is_imported_ = false; }
2482 // Whether this package was imported with a name of "_".
2484 uses_sink_alias() const
2485 { return this->uses_sink_alias_; }
2487 // Note that this package was imported with a name of "_".
2489 set_uses_sink_alias()
2490 { this->uses_sink_alias_ = true; }
2492 // Clear the sink alias field for the next file.
2494 clear_uses_sink_alias()
2495 { this->uses_sink_alias_ = false; }
2497 // Look up a name in the package. Returns NULL if the name is not
2500 lookup(const std::string& name) const
2501 { return this->bindings_->lookup(name); }
2503 // Set the location of the package. This is used if it is seen in a
2504 // different import before it is really imported.
2506 set_location(Location location)
2507 { this->location_ = location; }
2509 // Add a constant to the package.
2511 add_constant(const Typed_identifier& tid, Expression* expr)
2512 { return this->bindings_->add_constant(tid, this, expr, 0); }
2514 // Add a type to the package.
2516 add_type(const std::string& name, Type* type, Location location)
2517 { return this->bindings_->add_type(name, this, type, location); }
2519 // Add a type declaration to the package.
2521 add_type_declaration(const std::string& name, Location location)
2522 { return this->bindings_->add_type_declaration(name, this, location); }
2524 // Add a variable to the package.
2526 add_variable(const std::string& name, Variable* variable)
2527 { return this->bindings_->add_variable(name, this, variable); }
2529 // Add a function declaration to the package.
2531 add_function_declaration(const std::string& name, Function_type* type,
2533 { return this->bindings_->add_function_declaration(name, this, type, loc); }
2535 // Determine types of constants.
2540 // The real name of this package.
2542 // The unique prefix for all exported global symbols.
2543 std::string unique_prefix_;
2544 // The names in this package.
2545 Bindings* bindings_;
2546 // The priority of this package. A package has a priority higher
2547 // than the priority of all of the packages that it imports. This
2548 // is used to run init functions in the right order.
2550 // The location of the import statement.
2552 // True if some name from this package was used. This is mutable
2553 // because we can use a package even if we have a const pointer to
2556 // True if this package was imported in the current file.
2558 // True if this package was imported with a name of "_".
2559 bool uses_sink_alias_;
2562 // Return codes for the traversal functions. This is not an enum
2563 // because we want to be able to declare traversal functions in other
2564 // header files without including this one.
2566 // Continue traversal as usual.
2567 const int TRAVERSE_CONTINUE = -1;
2570 const int TRAVERSE_EXIT = 0;
2572 // Continue traversal, but skip components of the current object.
2573 // E.g., if this is returned by Traverse::statement, we do not
2574 // traverse the expressions in the statement even if
2575 // traverse_expressions is set in the traverse_mask.
2576 const int TRAVERSE_SKIP_COMPONENTS = 1;
2578 // This class is used when traversing the parse tree. The caller uses
2579 // a subclass which overrides functions as desired.
2584 // These bitmasks say what to traverse.
2585 static const unsigned int traverse_variables = 0x1;
2586 static const unsigned int traverse_constants = 0x2;
2587 static const unsigned int traverse_functions = 0x4;
2588 static const unsigned int traverse_blocks = 0x8;
2589 static const unsigned int traverse_statements = 0x10;
2590 static const unsigned int traverse_expressions = 0x20;
2591 static const unsigned int traverse_types = 0x40;
2593 Traverse(unsigned int traverse_mask)
2594 : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2597 virtual ~Traverse();
2599 // The bitmask of what to traverse.
2601 traverse_mask() const
2602 { return this->traverse_mask_; }
2604 // Record that we are going to traverse a type. This returns true
2605 // if the type has already been seen in this traversal. This is
2606 // required because types, unlike expressions, can form a circular
2609 remember_type(const Type*);
2611 // Record that we are going to see an expression. This returns true
2612 // if the expression has already been seen in this traversal. This
2613 // is only needed for cases where multiple expressions can point to
2616 remember_expression(const Expression*);
2618 // These functions return one of the TRAVERSE codes defined above.
2620 // If traverse_variables is set in the mask, this is called for
2621 // every variable in the tree.
2623 variable(Named_object*);
2625 // If traverse_constants is set in the mask, this is called for
2626 // every named constant in the tree. The bool parameter is true for
2627 // a global constant.
2629 constant(Named_object*, bool);
2631 // If traverse_functions is set in the mask, this is called for
2632 // every function in the tree.
2634 function(Named_object*);
2636 // If traverse_blocks is set in the mask, this is called for every
2637 // block in the tree.
2641 // If traverse_statements is set in the mask, this is called for
2642 // every statement in the tree.
2644 statement(Block*, size_t* index, Statement*);
2646 // If traverse_expressions is set in the mask, this is called for
2647 // every expression in the tree.
2649 expression(Expression**);
2651 // If traverse_types is set in the mask, this is called for every
2652 // type in the tree.
2657 // A hash table for types we have seen during this traversal. Note
2658 // that this uses the default hash functions for pointers rather
2659 // than Type_hash_identical and Type_identical. This is because for
2660 // traversal we care about seeing a specific type structure. If
2661 // there are two separate instances of identical types, we want to
2663 typedef Unordered_set(const Type*) Types_seen;
2665 typedef Unordered_set(const Expression*) Expressions_seen;
2667 // Bitmask of what sort of objects to traverse.
2668 unsigned int traverse_mask_;
2669 // Types which have been seen in this traversal.
2670 Types_seen* types_seen_;
2671 // Expressions which have been seen in this traversal.
2672 Expressions_seen* expressions_seen_;
2675 // A class which makes it easier to insert new statements before the
2676 // current statement during a traversal.
2678 class Statement_inserter
2681 // Empty constructor.
2682 Statement_inserter()
2683 : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2686 // Constructor for a statement in a block.
2687 Statement_inserter(Block* block, size_t *pindex)
2688 : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2691 // Constructor for a global variable.
2692 Statement_inserter(Gogo* gogo, Variable* var)
2693 : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2694 { go_assert(var->is_global()); }
2696 // We use the default copy constructor and assignment operator.
2698 // Insert S before the statement we are traversing, or before the
2699 // initialization expression of a global variable.
2701 insert(Statement* s);
2704 // The block that the statement is in.
2706 // The index of the statement that we are traversing.
2708 // The IR, needed when looking at an initializer expression for a
2711 // The global variable, when looking at an initializer expression.
2715 // When translating the gogo IR into the backend data structure, this
2716 // is the context we pass down the blocks and statements.
2718 class Translate_context
2721 Translate_context(Gogo* gogo, Named_object* function, Block* block,
2723 : gogo_(gogo), backend_(gogo->backend()), function_(function),
2724 block_(block), bblock_(bblock), is_const_(false)
2731 { return this->gogo_; }
2735 { return this->backend_; }
2739 { return this->function_; }
2743 { return this->block_; }
2747 { return this->bblock_; }
2751 { return this->is_const_; }
2753 // Make a constant context.
2756 { this->is_const_ = true; }
2759 // The IR for the entire compilation unit.
2761 // The generator for the backend data structures.
2763 // The function we are currently translating. NULL if not in a
2764 // function, e.g., the initializer of a global variable.
2765 Named_object* function_;
2766 // The block we are currently translating. NULL if not in a
2769 // The backend representation of the current block. NULL if block_
2772 // Whether this is being evaluated in a constant context. This is
2773 // used for type descriptor initializers.
2777 // Runtime error codes. These must match the values in
2778 // libgo/runtime/go-runtime-error.c.
2780 // Slice index out of bounds: negative or larger than the length of
2782 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2784 // Array index out of bounds.
2785 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2787 // String index out of bounds.
2788 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2790 // Slice slice out of bounds: negative or larger than the length of
2791 // the slice or high bound less than low bound.
2792 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2794 // Array slice out of bounds.
2795 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2797 // String slice out of bounds.
2798 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2800 // Dereference of nil pointer. This is used when there is a
2801 // dereference of a pointer to a very large struct or array, to ensure
2802 // that a gigantic array is not used a proxy to access random memory
2804 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2806 // Slice length or capacity out of bounds in make: negative or
2807 // overflow or length greater than capacity.
2808 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2810 // Map capacity out of bounds in make: negative or overflow.
2811 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2813 // Channel capacity out of bounds in make: negative or overflow.
2814 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2816 // Division by zero.
2817 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
2819 // This is used by some of the langhooks.
2820 extern Gogo* go_get_gogo();
2822 // Whether we have seen any errors. FIXME: Replace with a backend
2824 extern bool saw_errors();
2826 #endif // !defined(GO_GOGO_H)