1 // gogo.h -- Go frontend parsed representation. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
10 #include "go-linemap.h"
13 class Statement_inserter
;
15 class Type_hash_identical
;
18 class Typed_identifier
;
19 class Typed_identifier_list
;
23 class Temporary_statement
;
27 class Bindings_snapshot
;
33 class Struct_field_list
;
39 class Forward_declaration_type
;
42 class Translate_context
;
52 // This file declares the basic classes used to hold the internal
53 // representation of Go which is built by the parser.
55 // An initialization function for an imported package. This is a
56 // magic function which initializes variables and runs the "init"
62 Import_init(const std::string
& package_name
, const std::string
& init_name
,
64 : package_name_(package_name
), init_name_(init_name
), priority_(priority
)
67 // The name of the package being imported.
70 { return this->package_name_
; }
72 // The name of the package's init function.
75 { return this->init_name_
; }
77 // The priority of the initialization function. Functions with a
78 // lower priority number must be run first.
81 { return this->priority_
; }
84 // The name of the package being imported.
85 std::string package_name_
;
86 // The name of the package's init function.
87 std::string init_name_
;
92 // For sorting purposes.
95 operator<(const Import_init
& i1
, const Import_init
& i2
)
97 if (i1
.priority() < i2
.priority())
99 if (i1
.priority() > i2
.priority())
101 if (i1
.package_name() != i2
.package_name())
102 return i1
.package_name() < i2
.package_name();
103 return i1
.init_name() < i2
.init_name();
106 // The holder for the internal representation of the entire
112 // Create the IR, passing in the sizes of the types "int" and
113 // "uintptr" in bits.
114 Gogo(Backend
* backend
, Linemap
*linemap
, int int_type_size
, int pointer_size
);
116 // Get the backend generator.
119 { return this->backend_
; }
121 // Get the Location generator.
124 { return this->linemap_
; }
126 // Get the package name.
128 package_name() const;
130 // Set the package name.
132 set_package_name(const std::string
&, Location
);
134 // Return whether this is the "main" package.
136 is_main_package() const;
138 // If necessary, adjust the name to use for a hidden symbol. We add
139 // the package name, so that hidden symbols in different packages do
142 pack_hidden_name(const std::string
& name
, bool is_exported
) const
146 : '.' + this->pkgpath() + '.' + name
);
149 // Unpack a name which may have been hidden. Returns the
150 // user-visible name of the object.
152 unpack_hidden_name(const std::string
& name
)
153 { return name
[0] != '.' ? name
: name
.substr(name
.rfind('.') + 1); }
155 // Return whether a possibly packed name is hidden.
157 is_hidden_name(const std::string
& name
)
158 { return name
[0] == '.'; }
160 // Return the package path of a hidden name.
162 hidden_name_pkgpath(const std::string
& name
)
164 go_assert(Gogo::is_hidden_name(name
));
165 return name
.substr(1, name
.rfind('.') - 1);
168 // Given a name which may or may not have been hidden, return the
169 // name to use in an error message.
171 message_name(const std::string
& name
);
173 // Return whether a name is the blank identifier _.
175 is_sink_name(const std::string
& name
)
177 return (name
[0] == '.'
178 && name
[name
.length() - 1] == '_'
179 && name
[name
.length() - 2] == '.');
182 // Convert a pkgpath into a string suitable for a symbol
184 pkgpath_for_symbol(const std::string
& pkgpath
);
186 // Return the package path to use for reflect.Type.PkgPath.
190 // Return the package path to use for a symbol name.
192 pkgpath_symbol() const;
194 // Set the package path from a command line option.
196 set_pkgpath(const std::string
&);
198 // Set the prefix from a command line option.
200 set_prefix(const std::string
&);
202 // Return whether pkgpath was set from a command line option.
204 pkgpath_from_option() const
205 { return this->pkgpath_from_option_
; }
207 // Return the relative import path as set from the command line.
208 // Returns an empty string if it was not set.
210 relative_import_path() const
211 { return this->relative_import_path_
; }
213 // Set the relative import path from a command line option.
215 set_relative_import_path(const std::string
& s
)
216 {this->relative_import_path_
= s
; }
218 // Return the priority to use for the package we are compiling.
219 // This is two more than the largest priority of any package we
222 package_priority() const;
224 // Import a package. FILENAME is the file name argument, LOCAL_NAME
225 // is the local name to give to the package. If LOCAL_NAME is empty
226 // the declarations are added to the global scope.
228 import_package(const std::string
& filename
, const std::string
& local_name
,
229 bool is_local_name_exported
, Location
);
231 // Whether we are the global binding level.
233 in_global_scope() const;
235 // Look up a name in the current binding contours.
237 lookup(const std::string
&, Named_object
** pfunction
) const;
239 // Look up a name in the current block.
241 lookup_in_block(const std::string
&) const;
243 // Look up a name in the global namespace--the universal scope.
245 lookup_global(const char*) const;
247 // Add a new imported package. REAL_NAME is the real name of the
248 // package. ALIAS is the alias of the package; this may be the same
249 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
250 // this package should be added to the global namespace; this is
251 // true if the alias is ".". LOCATION is the location of the import
252 // statement. This returns the new package, or NULL on error.
254 add_imported_package(const std::string
& real_name
, const std::string
& alias
,
255 bool is_alias_exported
,
256 const std::string
& pkgpath
,
258 bool* padd_to_globals
);
260 // Register a package. This package may or may not be imported.
261 // This returns the Package structure for the package, creating if
264 register_package(const std::string
& pkgpath
, Location
);
266 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
267 // method function should be added to the type of its receiver.
269 start_function(const std::string
& name
, Function_type
* type
,
270 bool add_method_to_type
, Location
);
272 // Finish compiling a function.
274 finish_function(Location
);
276 // Return the current function.
278 current_function() const;
280 // Return the current block.
284 // Start a new block. This is not initially associated with a
287 start_block(Location
);
289 // Finish the current block and return it.
291 finish_block(Location
);
293 // Declare an erroneous name. This is used to avoid knock-on errors
294 // after a parsing error.
296 add_erroneous_name(const std::string
& name
);
298 // Declare an unknown name. This is used while parsing. The name
299 // must be resolved by the end of the parse. Unknown names are
300 // always added at the package level.
302 add_unknown_name(const std::string
& name
, Location
);
304 // Declare a function.
306 declare_function(const std::string
&, Function_type
*, Location
);
308 // Declare a function at the package level. This is used for
309 // functions generated for a type.
311 declare_package_function(const std::string
&, Function_type
*, Location
);
315 add_label_definition(const std::string
&, Location
);
317 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
318 // report errors for a goto from the current location to the label
321 add_label_reference(const std::string
&, Location
,
322 bool issue_goto_errors
);
324 // Return a snapshot of the current binding state.
326 bindings_snapshot(Location
);
328 // Add a statement to the current block.
330 add_statement(Statement
*);
332 // Add a block to the current block.
334 add_block(Block
*, Location
);
338 add_constant(const Typed_identifier
&, Expression
*, int iota_value
);
342 add_type(const std::string
&, Type
*, Location
);
344 // Add a named type. This is used for builtin types, and to add an
345 // imported type to the global scope.
347 add_named_type(Named_type
*);
351 declare_type(const std::string
&, Location
);
353 // Declare a type at the package level. This is used when the
354 // parser sees an unknown name where a type name is required.
356 declare_package_type(const std::string
&, Location
);
358 // Define a type which was already declared.
360 define_type(Named_object
*, Named_type
*);
364 add_variable(const std::string
&, Variable
*);
366 // Add a sink--a reference to the blank identifier _.
370 // Add a type which needs to be verified. This is used for sink
371 // types, just to give appropriate error messages.
373 add_type_to_verify(Type
* type
);
375 // Add a named object to the current namespace. This is used for
376 // import . "package".
378 add_named_object(Named_object
*);
380 // Add an identifier to the list of names seen in the file block.
382 add_file_block_name(const std::string
& name
, Location location
)
383 { this->file_block_names_
[name
] = location
; }
385 // Mark all local variables in current bindings as used. This is
386 // used when there is a parse error to avoid useless errors.
390 // Return a name to use for a thunk function. A thunk function is
391 // one we create during the compilation, for a go statement or a
392 // defer statement or a method expression.
396 // Return whether an object is a thunk.
398 is_thunk(const Named_object
*);
400 // Note that we've seen an interface type. This is used to build
401 // all required interface method tables.
403 record_interface_type(Interface_type
*);
405 // Note that we need an initialization function.
408 { this->need_init_fn_
= true; }
410 // Clear out all names in file scope. This is called when we start
411 // parsing a new file.
415 // Record that VAR1 must be initialized after VAR2. This is used
416 // when VAR2 does not appear in VAR1's INIT or PREINIT.
418 record_var_depends_on(Variable
* var1
, Named_object
* var2
)
420 go_assert(this->var_deps_
.find(var1
) == this->var_deps_
.end());
421 this->var_deps_
[var1
] = var2
;
424 // Return the variable that VAR depends on, or NULL if none.
426 var_depends_on(Variable
* var
) const
428 Var_deps::const_iterator p
= this->var_deps_
.find(var
);
429 return p
!= this->var_deps_
.end() ? p
->second
: NULL
;
432 // Queue up a type-specific function to be written out. This is
433 // used when a type-specific function is needed when not at the top
436 queue_specific_type_function(Type
* type
, Named_type
* name
,
437 const std::string
& hash_name
,
438 Function_type
* hash_fntype
,
439 const std::string
& equal_name
,
440 Function_type
* equal_fntype
);
442 // Write out queued specific type functions.
444 write_specific_type_functions();
446 // Whether we are done writing out specific type functions.
448 specific_type_functions_are_written() const
449 { return this->specific_type_functions_are_written_
; }
451 // Traverse the tree. See the Traverse class.
455 // Define the predeclared global names.
457 define_global_names();
459 // Verify and complete all types.
463 // Lower the parse tree.
467 // Lower all the statements in a block.
469 lower_block(Named_object
* function
, Block
*);
471 // Lower an expression.
473 lower_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
477 lower_constant(Named_object
*);
479 // Create all necessary function descriptors.
481 create_function_descriptors();
483 // Finalize the method lists and build stub methods for named types.
487 // Work out the types to use for unspecified variables and
492 // Type check the program.
496 // Check the types in a single block. This is used for complicated
499 check_types_in_block(Block
*);
501 // Check for return statements.
503 check_return_statements();
509 // Add an import control function for an imported package to the
512 add_import_init_fn(const std::string
& package_name
,
513 const std::string
& init_name
, int prio
);
515 // Turn short-cut operators (&&, ||) into explicit if statements.
519 // Use temporary variables to force order of evaluation.
523 // Build thunks for functions which call recover.
525 build_recover_thunks();
527 // Simplify statements which might use thunks: go and defer
530 simplify_thunk_statements();
532 // Dump AST if -fgo-dump-ast is set
534 dump_ast(const char* basename
);
536 // Convert named types to the backend representation.
538 convert_named_types();
540 // Convert named types in a list of bindings.
542 convert_named_types_in_bindings(Bindings
*);
544 // True if named types have been converted to the backend
547 named_types_are_converted() const
548 { return this->named_types_are_converted_
; }
550 // Write out the global values.
554 // Create trees for implicit builtin functions.
556 define_builtin_function_trees();
558 // Build a call to a builtin function. PDECL should point to a NULL
559 // initialized static pointer which will hold the fndecl. NAME is
560 // the name of the function. NARGS is the number of arguments.
561 // RETTYPE is the return type. It is followed by NARGS pairs of
562 // type and argument (both trees).
564 call_builtin(tree
* pdecl
, Location
, const char* name
, int nargs
,
567 // Build a call to the runtime error function.
569 runtime_error(int code
, Location
);
571 // Build a builtin struct with a list of fields.
573 builtin_struct(tree
* ptype
, const char* struct_name
, tree struct_type
,
576 // Mark a function declaration as a builtin library function.
578 mark_fndecl_as_builtin_library(tree fndecl
);
580 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
581 // the slice. VALUES points to the values. COUNT is the size,
582 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
585 slice_constructor(tree slice_type_tree
, tree values
, tree count
,
588 // Build required interface method tables.
590 build_interface_method_tables();
592 // Build an interface method table for a type: a list of function
593 // pointers, one for each interface method. This returns a decl.
595 interface_method_table_for_type(const Interface_type
*, Type
*,
598 // Return a tree which allocate SIZE bytes to hold values of type
601 allocate_memory(Type
*type
, tree size
, Location
);
603 // Return a type to use for pointer to const char.
605 const_char_pointer_type_tree();
607 // Build a string constant with the right type.
609 string_constant_tree(const std::string
&);
611 // Build a Go string constant. This returns a pointer to the
614 go_string_constant_tree(const std::string
&);
616 // Receive a value from a channel.
618 receive_from_channel(tree type_tree
, tree type_descriptor_tree
, tree channel
,
622 // During parsing, we keep a stack of functions. Each function on
623 // the stack is one that we are currently parsing. For each
624 // function, we keep track of the current stack of blocks.
628 Named_object
* function
;
629 // The stack of active blocks in the function.
630 std::vector
<Block
*> blocks
;
633 // The stack of functions.
634 typedef std::vector
<Open_function
> Open_functions
;
636 // Set up the built-in unsafe package.
638 import_unsafe(const std::string
&, bool is_exported
, Location
);
640 // Return the current binding contour.
645 current_bindings() const;
647 // Get the name of the magic initialization function.
651 // Get the decl for the magic initialization function.
653 initialization_function_decl();
655 // Write the magic initialization function.
657 write_initialization_function(tree fndecl
, tree init_stmt_list
);
659 // Initialize imported packages.
663 // Register variables with the garbage collector.
665 register_gc_vars(const std::vector
<Named_object
*>&, tree
*);
667 // Build a pointer to a Go string constant. This returns a pointer
670 ptr_go_string_constant_tree(const std::string
&);
672 // Type used to map import names to packages.
673 typedef std::map
<std::string
, Package
*> Imports
;
675 // Type used to map package names to packages.
676 typedef std::map
<std::string
, Package
*> Packages
;
678 // Type used to map variables to the function calls that set them.
679 // This is used for initialization dependency analysis.
680 typedef std::map
<Variable
*, Named_object
*> Var_deps
;
682 // Type used to map identifiers in the file block to the location
683 // where they were defined.
684 typedef Unordered_map(std::string
, Location
) File_block_names
;
686 // Type used to queue writing a type specific function.
687 struct Specific_type_function
691 std::string hash_name
;
692 Function_type
* hash_fntype
;
693 std::string equal_name
;
694 Function_type
* equal_fntype
;
696 Specific_type_function(Type
* atype
, Named_type
* aname
,
697 const std::string
& ahash_name
,
698 Function_type
* ahash_fntype
,
699 const std::string
& aequal_name
,
700 Function_type
* aequal_fntype
)
701 : type(atype
), name(aname
), hash_name(ahash_name
),
702 hash_fntype(ahash_fntype
), equal_name(aequal_name
),
703 equal_fntype(aequal_fntype
)
707 // The backend generator.
709 // The object used to keep track of file names and line numbers.
711 // The package we are compiling.
713 // The list of currently open functions during parsing.
714 Open_functions functions_
;
715 // The global binding contour. This includes the builtin functions
716 // and the package we are compiling.
718 // The list of names we have seen in the file block.
719 File_block_names file_block_names_
;
720 // Mapping from import file names to packages.
722 // Whether the magic unsafe package was imported.
723 bool imported_unsafe_
;
724 // Mapping from package names we have seen to packages. This does
725 // not include the package we are compiling.
727 // The functions named "init", if there are any.
728 std::vector
<Named_object
*> init_functions_
;
729 // A mapping from variables to the function calls that initialize
730 // them, if it is not stored in the variable's init or preinit.
731 // This is used for dependency analysis.
733 // Whether we need a magic initialization function.
735 // The name of the magic initialization function.
736 std::string init_fn_name_
;
737 // A list of import control variables for packages that we import.
738 std::set
<Import_init
> imported_init_fns_
;
739 // The package path used for reflection data.
740 std::string pkgpath_
;
741 // The package path to use for a symbol name.
742 std::string pkgpath_symbol_
;
743 // The prefix to use for symbols, from the -fgo-prefix option.
745 // Whether pkgpath_ has been set.
747 // Whether an explicit package path was set by -fgo-pkgpath.
748 bool pkgpath_from_option_
;
749 // Whether an explicit prefix was set by -fgo-prefix.
750 bool prefix_from_option_
;
751 // The relative import path, from the -fgo-relative-import-path
753 std::string relative_import_path_
;
754 // A list of types to verify.
755 std::vector
<Type
*> verify_types_
;
756 // A list of interface types defined while parsing.
757 std::vector
<Interface_type
*> interface_types_
;
758 // Type specific functions to write out.
759 std::vector
<Specific_type_function
*> specific_type_functions_
;
760 // Whether we are done writing out specific type functions.
761 bool specific_type_functions_are_written_
;
762 // Whether named types have been converted.
763 bool named_types_are_converted_
;
766 // A block of statements.
771 Block(Block
* enclosing
, Location
);
773 // Return the enclosing block.
776 { return this->enclosing_
; }
778 // Return the bindings of the block.
781 { return this->bindings_
; }
785 { return this->bindings_
; }
787 // Look at the block's statements.
788 const std::vector
<Statement
*>*
790 { return &this->statements_
; }
792 // Return the start location. This is normally the location of the
793 // left curly brace which starts the block.
795 start_location() const
796 { return this->start_location_
; }
798 // Return the end location. This is normally the location of the
799 // right curly brace which ends the block.
802 { return this->end_location_
; }
804 // Add a statement to the block.
806 add_statement(Statement
*);
808 // Add a statement to the front of the block.
810 add_statement_at_front(Statement
*);
812 // Replace a statement in a block.
814 replace_statement(size_t index
, Statement
*);
816 // Add a Statement before statement number INDEX.
818 insert_statement_before(size_t index
, Statement
*);
820 // Add a Statement after statement number INDEX.
822 insert_statement_after(size_t index
, Statement
*);
824 // Set the end location of the block.
826 set_end_location(Location location
)
827 { this->end_location_
= location
; }
829 // Traverse the tree.
833 // Set final types for unspecified variables and constants.
837 // Return true if execution of this block may fall through to the
840 may_fall_through() const;
842 // Convert the block to the backend representation.
844 get_backend(Translate_context
*);
846 // Iterate over statements.
848 typedef std::vector
<Statement
*>::iterator iterator
;
852 { return this->statements_
.begin(); }
856 { return this->statements_
.end(); }
861 // Statements in the block.
862 std::vector
<Statement
*> statements_
;
865 // Location of start of block.
866 Location start_location_
;
867 // Location of end of block.
868 Location end_location_
;
876 Function(Function_type
* type
, Function
*, Block
*, Location
);
878 // Return the function's type.
881 { return this->type_
; }
883 // Return the enclosing function if there is one.
886 { return this->enclosing_
; }
888 // Set the enclosing function. This is used when building thunks
889 // for functions which call recover.
891 set_enclosing(Function
* enclosing
)
893 go_assert(this->enclosing_
== NULL
);
894 this->enclosing_
= enclosing
;
897 // The result variables.
898 typedef std::vector
<Named_object
*> Results
;
900 // Create the result variables in the outer block.
902 create_result_variables(Gogo
*);
904 // Update the named result variables when cloning a function which
907 update_result_variables();
909 // Return the result variables.
912 { return this->results_
; }
914 // Whether the result variables have names.
916 results_are_named() const
917 { return this->results_are_named_
; }
919 // Whether this method should not be included in the type
924 go_assert(this->is_method());
925 return this->nointerface_
;
928 // Record that this method should not be included in the type
933 go_assert(this->is_method());
934 this->nointerface_
= true;
937 // Add a new field to the closure variable.
939 add_closure_field(Named_object
* var
, Location loc
)
940 { this->closure_fields_
.push_back(std::make_pair(var
, loc
)); }
942 // Whether this function needs a closure.
944 needs_closure() const
945 { return !this->closure_fields_
.empty(); }
947 // Return the closure variable, creating it if necessary. This is
948 // passed to the function as a static chain parameter.
952 // Set the closure variable. This is used when building thunks for
953 // functions which call recover.
955 set_closure_var(Named_object
* v
)
957 go_assert(this->closure_var_
== NULL
);
958 this->closure_var_
= v
;
961 // Return the variable for a reference to field INDEX in the closure
964 enclosing_var(unsigned int index
)
966 go_assert(index
< this->closure_fields_
.size());
967 return closure_fields_
[index
].first
;
970 // Set the type of the closure variable if there is one.
974 // Get the block of statements associated with the function.
977 { return this->block_
; }
979 // Get the location of the start of the function.
982 { return this->location_
; }
984 // Return whether this function is actually a method.
988 // Add a label definition to the function.
990 add_label_definition(Gogo
*, const std::string
& label_name
, Location
);
992 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
993 // if we should report errors for a goto from the current location
994 // to the label location.
996 add_label_reference(Gogo
*, const std::string
& label_name
,
997 Location
, bool issue_goto_errors
);
999 // Warn about labels that are defined but not used.
1001 check_labels() const;
1003 // Note that a new local type has been added. Return its index.
1005 new_local_type_index()
1006 { return this->local_type_count_
++; }
1008 // Whether this function calls the predeclared recover function.
1010 calls_recover() const
1011 { return this->calls_recover_
; }
1013 // Record that this function calls the predeclared recover function.
1014 // This is set during the lowering pass.
1017 { this->calls_recover_
= true; }
1019 // Whether this is a recover thunk function.
1021 is_recover_thunk() const
1022 { return this->is_recover_thunk_
; }
1024 // Record that this is a thunk built for a function which calls
1027 set_is_recover_thunk()
1028 { this->is_recover_thunk_
= true; }
1030 // Whether this function already has a recover thunk.
1032 has_recover_thunk() const
1033 { return this->has_recover_thunk_
; }
1035 // Record that this function already has a recover thunk.
1037 set_has_recover_thunk()
1038 { this->has_recover_thunk_
= true; }
1040 // Mark the function as going into a unique section.
1042 set_in_unique_section()
1043 { this->in_unique_section_
= true; }
1045 // Whether this function was created as a descriptor wrapper for
1046 // another function.
1048 is_descriptor_wrapper() const
1049 { return this->is_descriptor_wrapper_
; }
1051 // Swap with another function. Used only for the thunk which calls
1054 swap_for_recover(Function
*);
1056 // Traverse the tree.
1058 traverse(Traverse
*);
1060 // Determine types in the function.
1064 // Return an expression for the function descriptor, given the named
1065 // object for this function. This may only be called for functions
1066 // without a closure. This will be an immutable struct with one
1067 // field that points to the function's code.
1069 descriptor(Gogo
*, Named_object
*);
1071 // Set the descriptor for this function. This is used when a
1072 // function declaration is followed by a function definition.
1074 set_descriptor(Expression
* descriptor
)
1076 go_assert(this->descriptor_
== NULL
);
1077 this->descriptor_
= descriptor
;
1080 // Build a descriptor wrapper function.
1081 static Named_object
*
1082 make_descriptor_wrapper(Gogo
*, Named_object
*, Function_type
*);
1084 // Return the function's decl given an identifier.
1086 get_or_make_decl(Gogo
*, Named_object
*, tree id
);
1088 // Return the function's decl after it has been built.
1092 go_assert(this->fndecl_
!= NULL
);
1093 return this->fndecl_
;
1096 // Set the function decl to hold a tree of the function code.
1098 build_tree(Gogo
*, Named_object
*);
1100 // Get the value to return when not explicitly specified. May also
1101 // add statements to execute first to STMT_LIST.
1103 return_value(Gogo
*, Named_object
*, Location
, tree
* stmt_list
) const;
1105 // Get a tree for the variable holding the defer stack.
1107 defer_stack(Location
);
1109 // Export the function.
1111 export_func(Export
*, const std::string
& name
) const;
1113 // Export a function with a type.
1115 export_func_with_type(Export
*, const std::string
& name
,
1116 const Function_type
*);
1118 // Import a function.
1120 import_func(Import
*, std::string
* pname
, Typed_identifier
** receiver
,
1121 Typed_identifier_list
** pparameters
,
1122 Typed_identifier_list
** presults
, bool* is_varargs
);
1125 // Type for mapping from label names to Label objects.
1126 typedef Unordered_map(std::string
, Label
*) Labels
;
1129 make_receiver_parm_decl(Gogo
*, Named_object
*, tree
);
1132 copy_parm_to_heap(Gogo
*, Named_object
*, tree
);
1135 build_defer_wrapper(Gogo
*, Named_object
*, tree
*, tree
*);
1137 typedef std::vector
<std::pair
<Named_object
*,
1138 Location
> > Closure_fields
;
1140 // The function's type.
1141 Function_type
* type_
;
1142 // The enclosing function. This is NULL when there isn't one, which
1143 // is the normal case.
1144 Function
* enclosing_
;
1145 // The result variables, if any.
1147 // If there is a closure, this is the list of variables which appear
1148 // in the closure. This is created by the parser, and then resolved
1149 // to a real type when we lower parse trees.
1150 Closure_fields closure_fields_
;
1151 // The closure variable, passed as a parameter using the static
1152 // chain parameter. Normally NULL.
1153 Named_object
* closure_var_
;
1154 // The outer block of statements in the function.
1156 // The source location of the start of the function.
1158 // Labels defined or referenced in the function.
1160 // The number of local types defined in this function.
1161 unsigned int local_type_count_
;
1162 // The function descriptor, if any.
1163 Expression
* descriptor_
;
1164 // The function decl.
1166 // The defer stack variable. A pointer to this variable is used to
1167 // distinguish the defer stack for one function from another. This
1168 // is NULL unless we actually need a defer stack.
1169 Temporary_statement
* defer_stack_
;
1170 // True if the result variables are named.
1171 bool results_are_named_
: 1;
1172 // True if this method should not be included in the type descriptor.
1173 bool nointerface_
: 1;
1174 // True if this function calls the predeclared recover function.
1175 bool calls_recover_
: 1;
1176 // True if this a thunk built for a function which calls recover.
1177 bool is_recover_thunk_
: 1;
1178 // True if this function already has a recover thunk.
1179 bool has_recover_thunk_
: 1;
1180 // True if this function should be put in a unique section. This is
1181 // turned on for field tracking.
1182 bool in_unique_section_
: 1;
1183 // True if this is a function wrapper created to put in a function
1185 bool is_descriptor_wrapper_
: 1;
1188 // A snapshot of the current binding state.
1190 class Bindings_snapshot
1193 Bindings_snapshot(const Block
*, Location
);
1195 // Report any errors appropriate for a goto from the current binding
1196 // state of B to this one.
1198 check_goto_from(const Block
* b
, Location
);
1200 // Report any errors appropriate for a goto from this binding state
1201 // to the current state of B.
1203 check_goto_to(const Block
* b
);
1207 check_goto_block(Location
, const Block
*, const Block
*, size_t*);
1210 check_goto_defs(Location
, const Block
*, size_t, size_t);
1212 // The current block.
1213 const Block
* block_
;
1214 // The number of names currently defined in each open block.
1215 // Element 0 is this->block_, element 1 is
1216 // this->block_->enclosing(), etc.
1217 std::vector
<size_t> counts_
;
1218 // The location where this snapshot was taken.
1222 // A function declaration.
1224 class Function_declaration
1227 Function_declaration(Function_type
* fntype
, Location location
)
1228 : fntype_(fntype
), location_(location
), asm_name_(), descriptor_(NULL
),
1234 { return this->fntype_
; }
1238 { return this->location_
; }
1242 { return this->asm_name_
; }
1244 // Set the assembler name.
1246 set_asm_name(const std::string
& asm_name
)
1247 { this->asm_name_
= asm_name
; }
1249 // Return an expression for the function descriptor, given the named
1250 // object for this function. This may only be called for functions
1251 // without a closure. This will be an immutable struct with one
1252 // field that points to the function's code.
1254 descriptor(Gogo
*, Named_object
*);
1256 // Return true if we have created a descriptor for this declaration.
1258 has_descriptor() const
1259 { return this->descriptor_
!= NULL
; }
1261 // Return a decl for the function given an identifier.
1263 get_or_make_decl(Gogo
*, Named_object
*, tree id
);
1265 // If there is a descriptor, build it into the backend
1268 build_backend_descriptor(Gogo
*);
1270 // Export a function declaration.
1272 export_func(Export
* exp
, const std::string
& name
) const
1273 { Function::export_func_with_type(exp
, name
, this->fntype_
); }
1276 // The type of the function.
1277 Function_type
* fntype_
;
1278 // The location of the declaration.
1280 // The assembler name: this is the name to use in references to the
1281 // function. This is normally empty.
1282 std::string asm_name_
;
1283 // The function descriptor, if any.
1284 Expression
* descriptor_
;
1285 // The function decl if needed.
1294 Variable(Type
*, Expression
*, bool is_global
, bool is_parameter
,
1295 bool is_receiver
, Location
);
1297 // Get the type of the variable.
1304 // Return whether the type is defined yet.
1308 // Get the initial value.
1311 { return this->init_
; }
1313 // Return whether there are any preinit statements.
1315 has_pre_init() const
1316 { return this->preinit_
!= NULL
; }
1318 // Return the preinit statements if any.
1321 { return this->preinit_
; }
1323 // Return whether this is a global variable.
1326 { return this->is_global_
; }
1328 // Return whether this is a function parameter.
1330 is_parameter() const
1331 { return this->is_parameter_
; }
1333 // Return whether this is the receiver parameter of a method.
1336 { return this->is_receiver_
; }
1338 // Change this parameter to be a receiver. This is used when
1339 // creating the thunks created for functions which call recover.
1343 go_assert(this->is_parameter_
);
1344 this->is_receiver_
= true;
1347 // Change this parameter to not be a receiver. This is used when
1348 // creating the thunks created for functions which call recover.
1350 set_is_not_receiver()
1352 go_assert(this->is_parameter_
);
1353 this->is_receiver_
= false;
1356 // Return whether this is the varargs parameter of a function.
1358 is_varargs_parameter() const
1359 { return this->is_varargs_parameter_
; }
1361 // Whether this variable's address is taken.
1363 is_address_taken() const
1364 { return this->is_address_taken_
; }
1366 // Whether this variable should live in the heap.
1369 { return this->is_address_taken_
&& !this->is_global_
; }
1371 // Note that something takes the address of this variable.
1374 { this->is_address_taken_
= true; }
1376 // Return whether the address is taken but does not escape.
1378 is_non_escaping_address_taken() const
1379 { return this->is_non_escaping_address_taken_
; }
1381 // Note that something takes the address of this variable such that
1382 // the address does not escape the function.
1384 set_non_escaping_address_taken()
1385 { this->is_non_escaping_address_taken_
= true; }
1387 // Get the source location of the variable's declaration.
1390 { return this->location_
; }
1392 // Record that this is the varargs parameter of a function.
1394 set_is_varargs_parameter()
1396 go_assert(this->is_parameter_
);
1397 this->is_varargs_parameter_
= true;
1400 // Return whether the variable has been used.
1403 { return this->is_used_
; }
1405 // Mark that the variable has been used.
1408 { this->is_used_
= true; }
1410 // Clear the initial value; used for error handling.
1413 { this->init_
= NULL
; }
1415 // Set the initial value; used for converting shortcuts.
1417 set_init(Expression
* init
)
1418 { this->init_
= init
; }
1420 // Get the preinit block, a block of statements to be run before the
1421 // initialization expression.
1423 preinit_block(Gogo
*);
1425 // Add a statement to be run before the initialization expression.
1426 // This is only used for global variables.
1428 add_preinit_statement(Gogo
*, Statement
*);
1430 // Lower the initialization expression after parsing is complete.
1432 lower_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1434 // A special case: the init value is used only to determine the
1435 // type. This is used if the variable is defined using := with the
1436 // comma-ok form of a map index or a receive expression. The init
1437 // value is actually the map index expression or receive expression.
1438 // We use this because we may not know the right type at parse time.
1440 set_type_from_init_tuple()
1441 { this->type_from_init_tuple_
= true; }
1443 // Another special case: the init value is used only to determine
1444 // the type. This is used if the variable is defined using := with
1445 // a range clause. The init value is the range expression. The
1446 // type of the variable is the index type of the range expression
1447 // (i.e., the first value returned by a range).
1449 set_type_from_range_index()
1450 { this->type_from_range_index_
= true; }
1452 // Another special case: like set_type_from_range_index, but the
1453 // type is the value type of the range expression (i.e., the second
1454 // value returned by a range).
1456 set_type_from_range_value()
1457 { this->type_from_range_value_
= true; }
1459 // Another special case: the init value is used only to determine
1460 // the type. This is used if the variable is defined using := with
1461 // a case in a select statement. The init value is the channel.
1462 // The type of the variable is the channel's element type.
1464 set_type_from_chan_element()
1465 { this->type_from_chan_element_
= true; }
1467 // After we lower the select statement, we once again set the type
1468 // from the initialization expression.
1470 clear_type_from_chan_element()
1472 go_assert(this->type_from_chan_element_
);
1473 this->type_from_chan_element_
= false;
1476 // Note that this variable was created for a type switch clause.
1478 set_is_type_switch_var()
1479 { this->is_type_switch_var_
= true; }
1481 // Mark the variable as going into a unique section.
1483 set_in_unique_section()
1485 go_assert(this->is_global_
);
1486 this->in_unique_section_
= true;
1489 // Traverse the initializer expression.
1491 traverse_expression(Traverse
*, unsigned int traverse_mask
);
1493 // Determine the type of the variable if necessary.
1497 // Get the backend representation of the variable.
1499 get_backend_variable(Gogo
*, Named_object
*, const Package
*,
1500 const std::string
&);
1502 // Get the initial value of the variable as a tree. This may only
1503 // be called if has_pre_init() returns false.
1505 get_init_tree(Gogo
*, Named_object
* function
);
1507 // Return a series of statements which sets the value of the
1508 // variable in DECL. This should only be called is has_pre_init()
1509 // returns true. DECL may be NULL for a sink variable.
1511 get_init_block(Gogo
*, Named_object
* function
, tree decl
);
1513 // Export the variable.
1515 export_var(Export
*, const std::string
& name
) const;
1517 // Import a variable.
1519 import_var(Import
*, std::string
* pname
, Type
** ptype
);
1522 // The type of a tuple.
1524 type_from_tuple(Expression
*, bool) const;
1526 // The type of a range.
1528 type_from_range(Expression
*, bool, bool) const;
1530 // The element type of a channel.
1532 type_from_chan_element(Expression
*, bool) const;
1534 // The variable's type. This may be NULL if the type is set from
1537 // The initial value. This may be NULL if the variable should be
1538 // initialized to the default value for the type.
1540 // Statements to run before the init statement.
1542 // Location of variable definition.
1544 // Backend representation.
1545 Bvariable
* backend_
;
1546 // Whether this is a global variable.
1547 bool is_global_
: 1;
1548 // Whether this is a function parameter.
1549 bool is_parameter_
: 1;
1550 // Whether this is the receiver parameter of a method.
1551 bool is_receiver_
: 1;
1552 // Whether this is the varargs parameter of a function.
1553 bool is_varargs_parameter_
: 1;
1554 // Whether this variable is ever referenced.
1556 // Whether something takes the address of this variable. For a
1557 // local variable this implies that the variable has to be on the
1559 bool is_address_taken_
: 1;
1560 // Whether something takes the address of this variable such that
1561 // the address does not escape the function.
1562 bool is_non_escaping_address_taken_
: 1;
1563 // True if we have seen this variable in a traversal.
1565 // True if we have lowered the initialization expression.
1566 bool init_is_lowered_
: 1;
1567 // True if init is a tuple used to set the type.
1568 bool type_from_init_tuple_
: 1;
1569 // True if init is a range clause and the type is the index type.
1570 bool type_from_range_index_
: 1;
1571 // True if init is a range clause and the type is the value type.
1572 bool type_from_range_value_
: 1;
1573 // True if init is a channel and the type is the channel's element type.
1574 bool type_from_chan_element_
: 1;
1575 // True if this is a variable created for a type switch case.
1576 bool is_type_switch_var_
: 1;
1577 // True if we have determined types.
1578 bool determined_type_
: 1;
1579 // True if this variable should be put in a unique section. This is
1580 // used for field tracking.
1581 bool in_unique_section_
: 1;
1584 // A variable which is really the name for a function return value, or
1587 class Result_variable
1590 Result_variable(Type
* type
, Function
* function
, int index
,
1592 : type_(type
), function_(function
), index_(index
), location_(location
),
1593 backend_(NULL
), is_address_taken_(false),
1594 is_non_escaping_address_taken_(false)
1597 // Get the type of the result variable.
1600 { return this->type_
; }
1602 // Get the function that this is associated with.
1605 { return this->function_
; }
1607 // Index in the list of function results.
1610 { return this->index_
; }
1612 // The location of the variable definition.
1615 { return this->location_
; }
1617 // Whether this variable's address is taken.
1619 is_address_taken() const
1620 { return this->is_address_taken_
; }
1622 // Note that something takes the address of this variable.
1625 { this->is_address_taken_
= true; }
1627 // Return whether the address is taken but does not escape.
1629 is_non_escaping_address_taken() const
1630 { return this->is_non_escaping_address_taken_
; }
1632 // Note that something takes the address of this variable such that
1633 // the address does not escape the function.
1635 set_non_escaping_address_taken()
1636 { this->is_non_escaping_address_taken_
= true; }
1638 // Whether this variable should live in the heap.
1641 { return this->is_address_taken_
; }
1643 // Set the function. This is used when cloning functions which call
1646 set_function(Function
* function
)
1647 { this->function_
= function
; }
1649 // Get the backend representation of the variable.
1651 get_backend_variable(Gogo
*, Named_object
*, const std::string
&);
1654 // Type of result variable.
1656 // Function with which this is associated.
1657 Function
* function_
;
1658 // Index in list of results.
1660 // Where the result variable is defined.
1662 // Backend representation.
1663 Bvariable
* backend_
;
1664 // Whether something takes the address of this variable.
1665 bool is_address_taken_
;
1666 // Whether something takes the address of this variable such that
1667 // the address does not escape the function.
1668 bool is_non_escaping_address_taken_
;
1671 // The value we keep for a named constant. This lets us hold a type
1672 // and an expression.
1674 class Named_constant
1677 Named_constant(Type
* type
, Expression
* expr
, int iota_value
,
1679 : type_(type
), expr_(expr
), iota_value_(iota_value
), location_(location
),
1685 { return this->type_
; }
1689 { return this->expr_
; }
1693 { return this->iota_value_
; }
1697 { return this->location_
; }
1699 // Whether we are lowering.
1702 { return this->lowering_
; }
1704 // Set that we are lowering.
1707 { this->lowering_
= true; }
1709 // We are no longer lowering.
1712 { this->lowering_
= false; }
1714 // Traverse the expression.
1716 traverse_expression(Traverse
*);
1718 // Determine the type of the constant if necessary.
1722 // Indicate that we found and reported an error for this constant.
1726 // Export the constant.
1728 export_const(Export
*, const std::string
& name
) const;
1730 // Import a constant.
1732 import_const(Import
*, std::string
*, Type
**, Expression
**);
1735 // The type of the constant.
1737 // The expression for the constant.
1739 // If the predeclared constant iota is used in EXPR_, this is the
1740 // value it will have. We do this because at parse time we don't
1741 // know whether the name "iota" will refer to the predeclared
1742 // constant or to something else. We put in the right value in when
1745 // The location of the definition.
1747 // Whether we are currently lowering this constant.
1751 // A type declaration.
1753 class Type_declaration
1756 Type_declaration(Location location
)
1757 : location_(location
), in_function_(NULL
), in_function_index_(0),
1758 methods_(), issued_warning_(false)
1761 // Return the location.
1764 { return this->location_
; }
1766 // Return the function in which this type is declared. This will
1767 // return NULL for a type declared in global scope.
1769 in_function(unsigned int* pindex
)
1771 *pindex
= this->in_function_index_
;
1772 return this->in_function_
;
1775 // Set the function in which this type is declared.
1777 set_in_function(Named_object
* f
, unsigned int index
)
1779 this->in_function_
= f
;
1780 this->in_function_index_
= index
;
1783 // Add a method to this type. This is used when methods are defined
1786 add_method(const std::string
& name
, Function
* function
);
1788 // Add a method declaration to this type.
1790 add_method_declaration(const std::string
& name
, Package
*,
1791 Function_type
* type
, Location location
);
1793 // Return whether any methods were defined.
1795 has_methods() const;
1797 // Return the methods.
1798 const std::vector
<Named_object
*>*
1800 { return &this->methods_
; }
1802 // Define methods when the real type is known.
1804 define_methods(Named_type
*);
1806 // This is called if we are trying to use this type. It returns
1807 // true if we should issue a warning.
1812 // The location of the type declaration.
1814 // If this type is declared in a function, a pointer back to the
1815 // function in which it is defined.
1816 Named_object
* in_function_
;
1817 // The index of this type in IN_FUNCTION_.
1818 unsigned int in_function_index_
;
1819 // Methods defined before the type is defined.
1820 std::vector
<Named_object
*> methods_
;
1821 // True if we have issued a warning about a use of this type
1822 // declaration when it is undefined.
1823 bool issued_warning_
;
1826 // An unknown object. These are created by the parser for forward
1827 // references to names which have not been seen before. In a correct
1828 // program, these will always point to a real definition by the end of
1829 // the parse. Because they point to another Named_object, these may
1830 // only be referenced by Unknown_expression objects.
1835 Unknown_name(Location location
)
1836 : location_(location
), real_named_object_(NULL
)
1839 // Return the location where this name was first seen.
1842 { return this->location_
; }
1844 // Return the real named object that this points to, or NULL if it
1845 // was never resolved.
1847 real_named_object() const
1848 { return this->real_named_object_
; }
1850 // Set the real named object that this points to.
1852 set_real_named_object(Named_object
* no
);
1855 // The location where this name was first seen.
1857 // The real named object when it is known.
1862 // A named object named. This is the result of a declaration. We
1863 // don't use a superclass because they all have to be handled
1871 // An uninitialized Named_object. We should never see this.
1872 NAMED_OBJECT_UNINITIALIZED
,
1873 // An erroneous name. This indicates a parse error, to avoid
1874 // later errors about undefined references.
1875 NAMED_OBJECT_ERRONEOUS
,
1876 // An unknown name. This is used for forward references. In a
1877 // correct program, these will all be resolved by the end of the
1879 NAMED_OBJECT_UNKNOWN
,
1884 // A forward type declaration.
1885 NAMED_OBJECT_TYPE_DECLARATION
,
1888 // A result variable in a function.
1889 NAMED_OBJECT_RESULT_VAR
,
1890 // The blank identifier--the special variable named _.
1894 // A forward func declaration.
1895 NAMED_OBJECT_FUNC_DECLARATION
,
1897 NAMED_OBJECT_PACKAGE
1900 // Return the classification.
1902 classification() const
1903 { return this->classification_
; }
1908 is_erroneous() const
1909 { return this->classification_
== NAMED_OBJECT_ERRONEOUS
; }
1913 { return this->classification_
== NAMED_OBJECT_UNKNOWN
; }
1917 { return this->classification_
== NAMED_OBJECT_CONST
; }
1921 { return this->classification_
== NAMED_OBJECT_TYPE
; }
1924 is_type_declaration() const
1925 { return this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
; }
1929 { return this->classification_
== NAMED_OBJECT_VAR
; }
1932 is_result_variable() const
1933 { return this->classification_
== NAMED_OBJECT_RESULT_VAR
; }
1937 { return this->classification_
== NAMED_OBJECT_SINK
; }
1941 { return this->classification_
== NAMED_OBJECT_FUNC
; }
1944 is_function_declaration() const
1945 { return this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
; }
1949 { return this->classification_
== NAMED_OBJECT_PACKAGE
; }
1953 static Named_object
*
1954 make_erroneous_name(const std::string
& name
)
1955 { return new Named_object(name
, NULL
, NAMED_OBJECT_ERRONEOUS
); }
1957 static Named_object
*
1958 make_unknown_name(const std::string
& name
, Location
);
1960 static Named_object
*
1961 make_constant(const Typed_identifier
&, const Package
*, Expression
*,
1964 static Named_object
*
1965 make_type(const std::string
&, const Package
*, Type
*, Location
);
1967 static Named_object
*
1968 make_type_declaration(const std::string
&, const Package
*, Location
);
1970 static Named_object
*
1971 make_variable(const std::string
&, const Package
*, Variable
*);
1973 static Named_object
*
1974 make_result_variable(const std::string
&, Result_variable
*);
1976 static Named_object
*
1979 static Named_object
*
1980 make_function(const std::string
&, const Package
*, Function
*);
1982 static Named_object
*
1983 make_function_declaration(const std::string
&, const Package
*, Function_type
*,
1986 static Named_object
*
1987 make_package(const std::string
& alias
, Package
* package
);
1994 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
1995 return this->u_
.unknown_value
;
1999 unknown_value() const
2001 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2002 return this->u_
.unknown_value
;
2008 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2009 return this->u_
.const_value
;
2012 const Named_constant
*
2015 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2016 return this->u_
.const_value
;
2022 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2023 return this->u_
.type_value
;
2029 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2030 return this->u_
.type_value
;
2034 type_declaration_value()
2036 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2037 return this->u_
.type_declaration
;
2040 const Type_declaration
*
2041 type_declaration_value() const
2043 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2044 return this->u_
.type_declaration
;
2050 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2051 return this->u_
.var_value
;
2057 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2058 return this->u_
.var_value
;
2064 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2065 return this->u_
.result_var_value
;
2068 const Result_variable
*
2069 result_var_value() const
2071 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2072 return this->u_
.result_var_value
;
2078 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2079 return this->u_
.func_value
;
2085 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2086 return this->u_
.func_value
;
2089 Function_declaration
*
2090 func_declaration_value()
2092 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2093 return this->u_
.func_declaration_value
;
2096 const Function_declaration
*
2097 func_declaration_value() const
2099 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2100 return this->u_
.func_declaration_value
;
2106 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2107 return this->u_
.package_value
;
2111 package_value() const
2113 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2114 return this->u_
.package_value
;
2119 { return this->name_
; }
2121 // Return the name to use in an error message. The difference is
2122 // that if this Named_object is defined in a different package, this
2123 // will return PACKAGE.NAME.
2125 message_name() const;
2129 { return this->package_
; }
2131 // Resolve an unknown value if possible. This returns the same
2132 // Named_object or a new one.
2136 Named_object
* ret
= this;
2137 if (this->is_unknown())
2139 Named_object
* r
= this->unknown_value()->real_named_object();
2149 const Named_object
* ret
= this;
2150 if (this->is_unknown())
2152 const Named_object
* r
= this->unknown_value()->real_named_object();
2159 // The location where this object was defined or referenced.
2163 // Convert a variable to the backend representation.
2165 get_backend_variable(Gogo
*, Named_object
* function
);
2167 // Return a tree for the external identifier for this object.
2171 // Return a tree representing this object.
2173 get_tree(Gogo
*, Named_object
* function
);
2175 // Define a type declaration.
2177 set_type_value(Named_type
*);
2179 // Define a function declaration.
2181 set_function_value(Function
*);
2183 // Declare an unknown name as a type declaration.
2187 // Export this object.
2189 export_named_object(Export
*) const;
2192 Named_object(const std::string
&, const Package
*, Classification
);
2194 // The name of the object.
2196 // The package that this object is in. This is NULL if it is in the
2197 // file we are compiling.
2198 const Package
* package_
;
2199 // The type of object this is.
2200 Classification classification_
;
2204 Unknown_name
* unknown_value
;
2205 Named_constant
* const_value
;
2206 Named_type
* type_value
;
2207 Type_declaration
* type_declaration
;
2208 Variable
* var_value
;
2209 Result_variable
* result_var_value
;
2210 Function
* func_value
;
2211 Function_declaration
* func_declaration_value
;
2212 Package
* package_value
;
2214 // The DECL tree for this object if we have already converted it.
2218 // A binding contour. This binds names to objects.
2223 // Type for mapping from names to objects.
2224 typedef Unordered_map(std::string
, Named_object
*) Contour
;
2226 Bindings(Bindings
* enclosing
);
2228 // Add an erroneous name.
2230 add_erroneous_name(const std::string
& name
)
2231 { return this->add_named_object(Named_object::make_erroneous_name(name
)); }
2233 // Add an unknown name.
2235 add_unknown_name(const std::string
& name
, Location location
)
2237 return this->add_named_object(Named_object::make_unknown_name(name
,
2243 add_constant(const Typed_identifier
& tid
, const Package
* package
,
2244 Expression
* expr
, int iota_value
)
2246 return this->add_named_object(Named_object::make_constant(tid
, package
,
2253 add_type(const std::string
& name
, const Package
* package
, Type
* type
,
2256 return this->add_named_object(Named_object::make_type(name
, package
, type
,
2260 // Add a named type. This is used for builtin types, and to add an
2261 // imported type to the global scope.
2263 add_named_type(Named_type
* named_type
);
2265 // Add a type declaration.
2267 add_type_declaration(const std::string
& name
, const Package
* package
,
2270 Named_object
* no
= Named_object::make_type_declaration(name
, package
,
2272 return this->add_named_object(no
);
2277 add_variable(const std::string
& name
, const Package
* package
,
2280 return this->add_named_object(Named_object::make_variable(name
, package
,
2284 // Add a result variable.
2286 add_result_variable(const std::string
& name
, Result_variable
* result
)
2288 return this->add_named_object(Named_object::make_result_variable(name
,
2294 add_function(const std::string
& name
, const Package
*, Function
* function
);
2296 // Add a function declaration.
2298 add_function_declaration(const std::string
& name
, const Package
* package
,
2299 Function_type
* type
, Location location
);
2301 // Add a package. The location is the location of the import
2304 add_package(const std::string
& alias
, Package
* package
)
2306 Named_object
* no
= Named_object::make_package(alias
, package
);
2307 return this->add_named_object(no
);
2310 // Define a type which was already declared.
2312 define_type(Named_object
*, Named_type
*);
2314 // Add a method to the list of objects. This is not added to the
2317 add_method(Named_object
*);
2319 // Add a named object to this binding.
2321 add_named_object(Named_object
* no
)
2322 { return this->add_named_object_to_contour(&this->bindings_
, no
); }
2324 // Clear all names in file scope from the bindings.
2326 clear_file_scope(Gogo
*);
2328 // Look up a name in this binding contour and in any enclosing
2329 // binding contours. This returns NULL if the name is not found.
2331 lookup(const std::string
&) const;
2333 // Look up a name in this binding contour without looking in any
2334 // enclosing binding contours. Returns NULL if the name is not found.
2336 lookup_local(const std::string
&) const;
2340 remove_binding(Named_object
*);
2342 // Mark all variables as used. This is used for some types of parse
2347 // Traverse the tree. See the Traverse class.
2349 traverse(Traverse
*, bool is_global
);
2351 // Iterate over definitions. This does not include things which
2352 // were only declared.
2354 typedef std::vector
<Named_object
*>::const_iterator
2355 const_definitions_iterator
;
2357 const_definitions_iterator
2358 begin_definitions() const
2359 { return this->named_objects_
.begin(); }
2361 const_definitions_iterator
2362 end_definitions() const
2363 { return this->named_objects_
.end(); }
2365 // Return the number of definitions.
2367 size_definitions() const
2368 { return this->named_objects_
.size(); }
2370 // Return whether there are no definitions.
2372 empty_definitions() const
2373 { return this->named_objects_
.empty(); }
2375 // Iterate over declarations. This is everything that has been
2376 // declared, which includes everything which has been defined.
2378 typedef Contour::const_iterator const_declarations_iterator
;
2380 const_declarations_iterator
2381 begin_declarations() const
2382 { return this->bindings_
.begin(); }
2384 const_declarations_iterator
2385 end_declarations() const
2386 { return this->bindings_
.end(); }
2388 // Return the number of declarations.
2390 size_declarations() const
2391 { return this->bindings_
.size(); }
2393 // Return whether there are no declarations.
2395 empty_declarations() const
2396 { return this->bindings_
.empty(); }
2398 // Return the first declaration.
2401 { return this->bindings_
.empty() ? NULL
: this->bindings_
.begin()->second
; }
2405 add_named_object_to_contour(Contour
*, Named_object
*);
2408 new_definition(Named_object
*, Named_object
*);
2410 // Enclosing bindings.
2411 Bindings
* enclosing_
;
2412 // The list of objects.
2413 std::vector
<Named_object
*> named_objects_
;
2414 // The mapping from names to objects.
2423 Label(const std::string
& name
)
2424 : name_(name
), location_(Linemap::unknown_location()), snapshot_(NULL
),
2425 refs_(), is_used_(false), blabel_(NULL
)
2428 // Return the label's name.
2431 { return this->name_
; }
2433 // Return whether the label has been defined.
2436 { return !Linemap::is_unknown_location(this->location_
); }
2438 // Return whether the label has been used.
2441 { return this->is_used_
; }
2443 // Record that the label is used.
2446 { this->is_used_
= true; }
2448 // Return the location of the definition.
2451 { return this->location_
; }
2453 // Return the bindings snapshot.
2456 { return this->snapshot_
; }
2458 // Add a snapshot of a goto which refers to this label.
2460 add_snapshot_ref(Bindings_snapshot
* snapshot
)
2462 go_assert(Linemap::is_unknown_location(this->location_
));
2463 this->refs_
.push_back(snapshot
);
2466 // Return the list of snapshots of goto statements which refer to
2468 const std::vector
<Bindings_snapshot
*>&
2470 { return this->refs_
; }
2472 // Clear the references.
2476 // Define the label at LOCATION with the given bindings snapshot.
2478 define(Location location
, Bindings_snapshot
* snapshot
)
2480 go_assert(Linemap::is_unknown_location(this->location_
)
2481 && this->snapshot_
== NULL
);
2482 this->location_
= location
;
2483 this->snapshot_
= snapshot
;
2486 // Return the backend representation for this label.
2488 get_backend_label(Translate_context
*);
2490 // Return an expression for the address of this label. This is used
2491 // to get the return address of a deferred function to see whether
2492 // the function may call recover.
2494 get_addr(Translate_context
*, Location location
);
2497 // The name of the label.
2499 // The location of the definition. This is 0 if the label has not
2500 // yet been defined.
2502 // A snapshot of the set of bindings defined at this label, used to
2503 // issue errors about invalid goto statements.
2504 Bindings_snapshot
* snapshot_
;
2505 // A list of snapshots of goto statements which refer to this label.
2506 std::vector
<Bindings_snapshot
*> refs_
;
2507 // Whether the label has been used.
2509 // The backend representation.
2513 // An unnamed label. These are used when lowering loops.
2518 Unnamed_label(Location location
)
2519 : location_(location
), blabel_(NULL
)
2522 // Get the location where the label is defined.
2525 { return this->location_
; }
2527 // Set the location where the label is defined.
2529 set_location(Location location
)
2530 { this->location_
= location
; }
2532 // Return a statement which defines this label.
2534 get_definition(Translate_context
*);
2536 // Return a goto to this label from LOCATION.
2538 get_goto(Translate_context
*, Location location
);
2541 // Return the backend representation.
2543 get_blabel(Translate_context
*);
2545 // The location where the label is defined.
2547 // The backend representation of this label.
2551 // An imported package.
2556 Package(const std::string
& pkgpath
, Location location
);
2558 // Get the package path used for all symbols exported from this
2562 { return this->pkgpath_
; }
2564 // Return the package path to use for a symbol name.
2566 pkgpath_symbol() const
2567 { return this->pkgpath_symbol_
; }
2569 // Return the location of the import statement.
2572 { return this->location_
; }
2574 // Return whether we know the name of this package yet.
2576 has_package_name() const
2577 { return !this->package_name_
.empty(); }
2579 // The name that this package uses in its package clause. This may
2580 // be different from the name in the associated Named_object if the
2581 // import statement used an alias.
2583 package_name() const
2585 go_assert(!this->package_name_
.empty());
2586 return this->package_name_
;
2589 // The priority of this package. The init function of packages with
2590 // lower priority must be run before the init function of packages
2591 // with higher priority.
2594 { return this->priority_
; }
2596 // Set the priority.
2598 set_priority(int priority
);
2600 // Return the bindings.
2603 { return this->bindings_
; }
2605 // Whether some symbol from the package was used.
2608 { return this->used_
; }
2610 // Note that some symbol from this package was used.
2613 { this->used_
= true; }
2615 // Clear the used field for the next file.
2618 { this->used_
= false; }
2620 // Whether this package was imported in the current file.
2623 { return this->is_imported_
; }
2625 // Note that this package was imported in the current file.
2628 { this->is_imported_
= true; }
2630 // Clear the imported field for the next file.
2633 { this->is_imported_
= false; }
2635 // Whether this package was imported with a name of "_".
2637 uses_sink_alias() const
2638 { return this->uses_sink_alias_
; }
2640 // Note that this package was imported with a name of "_".
2642 set_uses_sink_alias()
2643 { this->uses_sink_alias_
= true; }
2645 // Clear the sink alias field for the next file.
2647 clear_uses_sink_alias()
2648 { this->uses_sink_alias_
= false; }
2650 // Look up a name in the package. Returns NULL if the name is not
2653 lookup(const std::string
& name
) const
2654 { return this->bindings_
->lookup(name
); }
2656 // Set the name of the package.
2658 set_package_name(const std::string
& name
, Location
);
2660 // Set the location of the package. This is used to record the most
2661 // recent import location.
2663 set_location(Location location
)
2664 { this->location_
= location
; }
2666 // Add a constant to the package.
2668 add_constant(const Typed_identifier
& tid
, Expression
* expr
)
2669 { return this->bindings_
->add_constant(tid
, this, expr
, 0); }
2671 // Add a type to the package.
2673 add_type(const std::string
& name
, Type
* type
, Location location
)
2674 { return this->bindings_
->add_type(name
, this, type
, location
); }
2676 // Add a type declaration to the package.
2678 add_type_declaration(const std::string
& name
, Location location
)
2679 { return this->bindings_
->add_type_declaration(name
, this, location
); }
2681 // Add a variable to the package.
2683 add_variable(const std::string
& name
, Variable
* variable
)
2684 { return this->bindings_
->add_variable(name
, this, variable
); }
2686 // Add a function declaration to the package.
2688 add_function_declaration(const std::string
& name
, Function_type
* type
,
2690 { return this->bindings_
->add_function_declaration(name
, this, type
, loc
); }
2692 // Determine types of constants.
2697 // The package path for type reflection data.
2698 std::string pkgpath_
;
2699 // The package path for symbol names.
2700 std::string pkgpath_symbol_
;
2701 // The name that this package uses in the package clause. This may
2702 // be the empty string if it is not yet known.
2703 std::string package_name_
;
2704 // The names in this package.
2705 Bindings
* bindings_
;
2706 // The priority of this package. A package has a priority higher
2707 // than the priority of all of the packages that it imports. This
2708 // is used to run init functions in the right order.
2710 // The location of the import statement.
2712 // True if some name from this package was used. This is mutable
2713 // because we can use a package even if we have a const pointer to
2716 // True if this package was imported in the current file.
2718 // True if this package was imported with a name of "_".
2719 bool uses_sink_alias_
;
2722 // Return codes for the traversal functions. This is not an enum
2723 // because we want to be able to declare traversal functions in other
2724 // header files without including this one.
2726 // Continue traversal as usual.
2727 const int TRAVERSE_CONTINUE
= -1;
2730 const int TRAVERSE_EXIT
= 0;
2732 // Continue traversal, but skip components of the current object.
2733 // E.g., if this is returned by Traverse::statement, we do not
2734 // traverse the expressions in the statement even if
2735 // traverse_expressions is set in the traverse_mask.
2736 const int TRAVERSE_SKIP_COMPONENTS
= 1;
2738 // This class is used when traversing the parse tree. The caller uses
2739 // a subclass which overrides functions as desired.
2744 // These bitmasks say what to traverse.
2745 static const unsigned int traverse_variables
= 0x1;
2746 static const unsigned int traverse_constants
= 0x2;
2747 static const unsigned int traverse_functions
= 0x4;
2748 static const unsigned int traverse_blocks
= 0x8;
2749 static const unsigned int traverse_statements
= 0x10;
2750 static const unsigned int traverse_expressions
= 0x20;
2751 static const unsigned int traverse_types
= 0x40;
2753 Traverse(unsigned int traverse_mask
)
2754 : traverse_mask_(traverse_mask
), types_seen_(NULL
), expressions_seen_(NULL
)
2757 virtual ~Traverse();
2759 // The bitmask of what to traverse.
2761 traverse_mask() const
2762 { return this->traverse_mask_
; }
2764 // Record that we are going to traverse a type. This returns true
2765 // if the type has already been seen in this traversal. This is
2766 // required because types, unlike expressions, can form a circular
2769 remember_type(const Type
*);
2771 // Record that we are going to see an expression. This returns true
2772 // if the expression has already been seen in this traversal. This
2773 // is only needed for cases where multiple expressions can point to
2776 remember_expression(const Expression
*);
2778 // These functions return one of the TRAVERSE codes defined above.
2780 // If traverse_variables is set in the mask, this is called for
2781 // every variable in the tree.
2783 variable(Named_object
*);
2785 // If traverse_constants is set in the mask, this is called for
2786 // every named constant in the tree. The bool parameter is true for
2787 // a global constant.
2789 constant(Named_object
*, bool);
2791 // If traverse_functions is set in the mask, this is called for
2792 // every function in the tree.
2794 function(Named_object
*);
2796 // If traverse_blocks is set in the mask, this is called for every
2797 // block in the tree.
2801 // If traverse_statements is set in the mask, this is called for
2802 // every statement in the tree.
2804 statement(Block
*, size_t* index
, Statement
*);
2806 // If traverse_expressions is set in the mask, this is called for
2807 // every expression in the tree.
2809 expression(Expression
**);
2811 // If traverse_types is set in the mask, this is called for every
2812 // type in the tree.
2817 // A hash table for types we have seen during this traversal. Note
2818 // that this uses the default hash functions for pointers rather
2819 // than Type_hash_identical and Type_identical. This is because for
2820 // traversal we care about seeing a specific type structure. If
2821 // there are two separate instances of identical types, we want to
2823 typedef Unordered_set(const Type
*) Types_seen
;
2825 typedef Unordered_set(const Expression
*) Expressions_seen
;
2827 // Bitmask of what sort of objects to traverse.
2828 unsigned int traverse_mask_
;
2829 // Types which have been seen in this traversal.
2830 Types_seen
* types_seen_
;
2831 // Expressions which have been seen in this traversal.
2832 Expressions_seen
* expressions_seen_
;
2835 // A class which makes it easier to insert new statements before the
2836 // current statement during a traversal.
2838 class Statement_inserter
2841 // Empty constructor.
2842 Statement_inserter()
2843 : block_(NULL
), pindex_(NULL
), gogo_(NULL
), var_(NULL
)
2846 // Constructor for a statement in a block.
2847 Statement_inserter(Block
* block
, size_t *pindex
)
2848 : block_(block
), pindex_(pindex
), gogo_(NULL
), var_(NULL
)
2851 // Constructor for a global variable.
2852 Statement_inserter(Gogo
* gogo
, Variable
* var
)
2853 : block_(NULL
), pindex_(NULL
), gogo_(gogo
), var_(var
)
2854 { go_assert(var
->is_global()); }
2856 // We use the default copy constructor and assignment operator.
2858 // Insert S before the statement we are traversing, or before the
2859 // initialization expression of a global variable.
2861 insert(Statement
* s
);
2864 // The block that the statement is in.
2866 // The index of the statement that we are traversing.
2868 // The IR, needed when looking at an initializer expression for a
2871 // The global variable, when looking at an initializer expression.
2875 // When translating the gogo IR into the backend data structure, this
2876 // is the context we pass down the blocks and statements.
2878 class Translate_context
2881 Translate_context(Gogo
* gogo
, Named_object
* function
, Block
* block
,
2883 : gogo_(gogo
), backend_(gogo
->backend()), function_(function
),
2884 block_(block
), bblock_(bblock
), is_const_(false)
2891 { return this->gogo_
; }
2895 { return this->backend_
; }
2899 { return this->function_
; }
2903 { return this->block_
; }
2907 { return this->bblock_
; }
2911 { return this->is_const_
; }
2913 // Make a constant context.
2916 { this->is_const_
= true; }
2919 // The IR for the entire compilation unit.
2921 // The generator for the backend data structures.
2923 // The function we are currently translating. NULL if not in a
2924 // function, e.g., the initializer of a global variable.
2925 Named_object
* function_
;
2926 // The block we are currently translating. NULL if not in a
2929 // The backend representation of the current block. NULL if block_
2932 // Whether this is being evaluated in a constant context. This is
2933 // used for type descriptor initializers.
2937 // Runtime error codes. These must match the values in
2938 // libgo/runtime/go-runtime-error.c.
2940 // Slice index out of bounds: negative or larger than the length of
2942 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
= 0;
2944 // Array index out of bounds.
2945 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
= 1;
2947 // String index out of bounds.
2948 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
= 2;
2950 // Slice slice out of bounds: negative or larger than the length of
2951 // the slice or high bound less than low bound.
2952 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
= 3;
2954 // Array slice out of bounds.
2955 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
= 4;
2957 // String slice out of bounds.
2958 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
= 5;
2960 // Dereference of nil pointer. This is used when there is a
2961 // dereference of a pointer to a very large struct or array, to ensure
2962 // that a gigantic array is not used a proxy to access random memory
2964 static const int RUNTIME_ERROR_NIL_DEREFERENCE
= 6;
2966 // Slice length or capacity out of bounds in make: negative or
2967 // overflow or length greater than capacity.
2968 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
= 7;
2970 // Map capacity out of bounds in make: negative or overflow.
2971 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
= 8;
2973 // Channel capacity out of bounds in make: negative or overflow.
2974 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
= 9;
2976 // Division by zero.
2977 static const int RUNTIME_ERROR_DIVISION_BY_ZERO
= 10;
2979 // This is used by some of the langhooks.
2980 extern Gogo
* go_get_gogo();
2982 // Whether we have seen any errors. FIXME: Replace with a backend
2984 extern bool saw_errors();
2986 #endif // !defined(GO_GOGO_H)