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
;
22 class Expression_list
;
24 class Temporary_statement
;
28 class Bindings_snapshot
;
34 class Struct_field_list
;
40 class Forward_declaration_type
;
43 class Translate_context
;
57 // This file declares the basic classes used to hold the internal
58 // representation of Go which is built by the parser.
60 // An initialization function for an imported package. This is a
61 // magic function which initializes variables and runs the "init"
67 Import_init(const std::string
& package_name
, const std::string
& init_name
,
69 : package_name_(package_name
), init_name_(init_name
), priority_(priority
)
72 // The name of the package being imported.
75 { return this->package_name_
; }
77 // The name of the package's init function.
80 { return this->init_name_
; }
82 // Older V1 export data uses a priority scheme to order
83 // initialization functions; functions with a lower priority number
84 // must be run first. This value will be set to -1 for current
85 // generation objects, and will take on a non-negative value only
86 // when importing a V1-vintage object.
89 { return this->priority_
; }
93 set_priority(int new_priority
)
94 { this->priority_
= new_priority
; }
96 // Record the fact that some other init fcn must be run before this init fcn.
98 record_precursor_fcn(std::string init_fcn_name
)
99 { this->precursor_functions_
.insert(init_fcn_name
); }
101 // Return the list of precursor fcns for this fcn (must be run before it).
102 const std::set
<std::string
>&
104 { return this->precursor_functions_
; }
107 // The name of the package being imported.
108 std::string package_name_
;
109 // The name of the package's init function.
110 std::string init_name_
;
111 // Names of init functions that must be run before this fcn.
112 std::set
<std::string
> precursor_functions_
;
113 // Priority for this function. See note above on obsolescence.
117 // For sorting purposes.
119 struct Import_init_lt
{
120 bool operator()(const Import_init
* i1
, const Import_init
* i2
) const
122 return i1
->init_name() < i2
->init_name();
126 // Set of import init objects.
127 class Import_init_set
: public std::set
<Import_init
*, Import_init_lt
> {
131 priority_compare(const Import_init
* i1
, const Import_init
* i2
)
133 if (i1
->priority() < i2
->priority())
135 if (i1
->priority() > i2
->priority())
137 if (i1
->package_name() != i2
->package_name())
138 return i1
->package_name() < i2
->package_name();
139 return i1
->init_name() < i2
->init_name();
142 // The holder for the internal representation of the entire
148 // Create the IR, passing in the sizes of the types "int" and
149 // "uintptr" in bits.
150 Gogo(Backend
* backend
, Linemap
*linemap
, int int_type_size
, int pointer_size
);
152 // Get the backend generator.
155 { return this->backend_
; }
157 // Get the Location generator.
160 { return this->linemap_
; }
162 // Get the package name.
164 package_name() const;
166 // Set the package name.
168 set_package_name(const std::string
&, Location
);
170 // Return whether this is the "main" package.
172 is_main_package() const;
174 // If necessary, adjust the name to use for a hidden symbol. We add
175 // the package name, so that hidden symbols in different packages do
178 pack_hidden_name(const std::string
& name
, bool is_exported
) const
182 : '.' + this->pkgpath() + '.' + name
);
185 // Unpack a name which may have been hidden. Returns the
186 // user-visible name of the object.
188 unpack_hidden_name(const std::string
& name
)
189 { return name
[0] != '.' ? name
: name
.substr(name
.rfind('.') + 1); }
191 // Return whether a possibly packed name is hidden.
193 is_hidden_name(const std::string
& name
)
194 { return name
[0] == '.'; }
196 // Return the package path of a hidden name.
198 hidden_name_pkgpath(const std::string
& name
)
200 go_assert(Gogo::is_hidden_name(name
));
201 return name
.substr(1, name
.rfind('.') - 1);
204 // Given a name which may or may not have been hidden, return the
205 // name to use within a mangled symbol name.
207 mangle_possibly_hidden_name(const std::string
& name
)
209 // FIXME: This adds in pkgpath twice for hidden symbols, which is
212 if (!Gogo::is_hidden_name(name
))
217 std::string pkgpath
= Gogo::hidden_name_pkgpath(name
);
218 n
.append(Gogo::pkgpath_for_symbol(pkgpath
));
220 n
.append(Gogo::unpack_hidden_name(name
));
225 // Given a name which may or may not have been hidden, return the
226 // name to use in an error message.
228 message_name(const std::string
& name
);
230 // Return whether a name is the blank identifier _.
232 is_sink_name(const std::string
& name
)
234 return (name
[0] == '.'
235 && name
[name
.length() - 1] == '_'
236 && name
[name
.length() - 2] == '.');
239 // Convert a pkgpath into a string suitable for a symbol
241 pkgpath_for_symbol(const std::string
& pkgpath
);
243 // Return the package path to use for reflect.Type.PkgPath.
247 // Return the package path to use for a symbol name.
249 pkgpath_symbol() const;
251 // Set the package path from a command line option.
253 set_pkgpath(const std::string
&);
255 // Set the prefix from a command line option.
257 set_prefix(const std::string
&);
259 // Return whether pkgpath was set from a command line option.
261 pkgpath_from_option() const
262 { return this->pkgpath_from_option_
; }
264 // Return the relative import path as set from the command line.
265 // Returns an empty string if it was not set.
267 relative_import_path() const
268 { return this->relative_import_path_
; }
270 // Set the relative import path from a command line option.
272 set_relative_import_path(const std::string
& s
)
273 { this->relative_import_path_
= s
; }
275 // Set the C header file to write. This is used for the runtime
278 set_c_header(const std::string
& s
)
279 { this->c_header_
= s
; }
281 // Return whether to check for division by zero in binary operations.
283 check_divide_by_zero() const
284 { return this->check_divide_by_zero_
; }
286 // Set the option to check division by zero from a command line option.
288 set_check_divide_by_zero(bool b
)
289 { this->check_divide_by_zero_
= b
; }
291 // Return whether to check for division overflow in binary operations.
293 check_divide_overflow() const
294 { return this->check_divide_overflow_
; }
296 // Set the option to check division overflow from a command line option.
298 set_check_divide_overflow(bool b
)
299 { this->check_divide_overflow_
= b
; }
301 // Return whether we are compiling the runtime package.
303 compiling_runtime() const
304 { return this->compiling_runtime_
; }
306 // Set whether we are compiling the runtime package.
308 set_compiling_runtime(bool b
)
309 { this->compiling_runtime_
= b
; }
311 // Return the level of escape analysis debug information to emit.
313 debug_escape_level() const
314 { return this->debug_escape_level_
; }
316 // Set the level of escape analysis debugging from a command line option.
318 set_debug_escape_level(int level
)
319 { this->debug_escape_level_
= level
; }
321 // Return the size threshold used to determine whether to issue
322 // a nil-check for a given pointer dereference. A threshold of -1
323 // implies that all potentially faulting dereference ops should
324 // be nil-checked. A positive threshold of N implies that a deref
325 // of *P where P has size less than N doesn't need a nil check.
327 nil_check_size_threshold() const
328 { return this->nil_check_size_threshold_
; }
330 // Set the nil-check size threshold, as described above.
332 set_nil_check_size_threshold(int64_t bytes
)
333 { this->nil_check_size_threshold_
= bytes
; }
335 // Import a package. FILENAME is the file name argument, LOCAL_NAME
336 // is the local name to give to the package. If LOCAL_NAME is empty
337 // the declarations are added to the global scope.
339 import_package(const std::string
& filename
, const std::string
& local_name
,
340 bool is_local_name_exported
, bool must_exist
, Location
);
342 // Whether we are the global binding level.
344 in_global_scope() const;
346 // Look up a name in the current binding contours.
348 lookup(const std::string
&, Named_object
** pfunction
) const;
350 // Look up a name in the current block.
352 lookup_in_block(const std::string
&) const;
354 // Look up a name in the global namespace--the universal scope.
356 lookup_global(const char*) const;
358 // Add a new imported package. REAL_NAME is the real name of the
359 // package. ALIAS is the alias of the package; this may be the same
360 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
361 // this package should be added to the global namespace; this is
362 // true if the alias is ".". LOCATION is the location of the import
363 // statement. This returns the new package, or NULL on error.
365 add_imported_package(const std::string
& real_name
, const std::string
& alias
,
366 bool is_alias_exported
,
367 const std::string
& pkgpath
,
368 const std::string
& pkgpath_symbol
,
370 bool* padd_to_globals
);
372 // Register a package. This package may or may not be imported.
373 // This returns the Package structure for the package, creating if
376 register_package(const std::string
& pkgpath
,
377 const std::string
& pkgpath_symbol
, Location
);
379 // Look up a package by pkgpath, and return its pkgpath_symbol.
381 pkgpath_symbol_for_package(const std::string
&);
383 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
384 // method function should be added to the type of its receiver.
386 start_function(const std::string
& name
, Function_type
* type
,
387 bool add_method_to_type
, Location
);
389 // Finish compiling a function.
391 finish_function(Location
);
393 // Return the current function.
395 current_function() const;
397 // Return the current block.
401 // Start a new block. This is not initially associated with a
404 start_block(Location
);
406 // Finish the current block and return it.
408 finish_block(Location
);
410 // Declare an erroneous name. This is used to avoid knock-on errors
411 // after a parsing error.
413 add_erroneous_name(const std::string
& name
);
415 // Declare an unknown name. This is used while parsing. The name
416 // must be resolved by the end of the parse. Unknown names are
417 // always added at the package level.
419 add_unknown_name(const std::string
& name
, Location
);
421 // Declare a function.
423 declare_function(const std::string
&, Function_type
*, Location
);
425 // Declare a function at the package level. This is used for
426 // functions generated for a type.
428 declare_package_function(const std::string
&, Function_type
*, Location
);
432 add_label_definition(const std::string
&, Location
);
434 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
435 // report errors for a goto from the current location to the label
438 add_label_reference(const std::string
&, Location
,
439 bool issue_goto_errors
);
441 // An analysis set is a list of functions paired with a boolean that indicates
442 // whether the list of functions are recursive.
443 typedef std::pair
<std::vector
<Named_object
*>, bool> Analysis_set
;
445 // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
448 add_analysis_set(const std::vector
<Named_object
*>& group
, bool recursive
)
449 { this->analysis_sets_
.push_back(std::make_pair(group
, recursive
)); }
451 // Return a snapshot of the current binding state.
453 bindings_snapshot(Location
);
455 // Add a statement to the current block.
457 add_statement(Statement
*);
459 // Add a block to the current block.
461 add_block(Block
*, Location
);
465 add_constant(const Typed_identifier
&, Expression
*, int iota_value
);
469 add_type(const std::string
&, Type
*, Location
);
471 // Add a named type. This is used for builtin types, and to add an
472 // imported type to the global scope.
474 add_named_type(Named_type
*);
478 declare_type(const std::string
&, Location
);
480 // Declare a type at the package level. This is used when the
481 // parser sees an unknown name where a type name is required.
483 declare_package_type(const std::string
&, Location
);
485 // Define a type which was already declared.
487 define_type(Named_object
*, Named_type
*);
491 add_variable(const std::string
&, Variable
*);
493 // Add a sink--a reference to the blank identifier _.
497 // Add a type which needs to be verified. This is used for sink
498 // types, just to give appropriate error messages.
500 add_type_to_verify(Type
* type
);
502 // Add a named object to the current namespace. This is used for
503 // import . "package".
505 add_dot_import_object(Named_object
*);
507 // Add an identifier to the list of names seen in the file block.
509 add_file_block_name(const std::string
& name
, Location location
)
510 { this->file_block_names_
[name
] = location
; }
512 // Add a linkname, from the go:linkname compiler directive. This
513 // changes the externally visible name of go_name to be ext_name.
515 add_linkname(const std::string
& go_name
, bool is_exported
,
516 const std::string
& ext_name
, Location location
);
518 // Mark all local variables in current bindings as used. This is
519 // used when there is a parse error to avoid useless errors.
523 // Note that we've seen an interface type. This is used to build
524 // all required interface method tables.
526 record_interface_type(Interface_type
*);
528 // Note that we need an initialization function.
531 { this->need_init_fn_
= true; }
533 // Return whether the current file imported the unsafe package.
535 current_file_imported_unsafe() const
536 { return this->current_file_imported_unsafe_
; }
538 // Clear out all names in file scope. This is called when we start
539 // parsing a new file.
543 // Record that VAR1 must be initialized after VAR2. This is used
544 // when VAR2 does not appear in VAR1's INIT or PREINIT.
546 record_var_depends_on(Variable
* var1
, Named_object
* var2
)
548 go_assert(this->var_deps_
.find(var1
) == this->var_deps_
.end());
549 this->var_deps_
[var1
] = var2
;
552 // Return the variable that VAR depends on, or NULL if none.
554 var_depends_on(Variable
* var
) const
556 Var_deps::const_iterator p
= this->var_deps_
.find(var
);
557 return p
!= this->var_deps_
.end() ? p
->second
: NULL
;
560 // Queue up a type-specific function to be written out. This is
561 // used when a type-specific function is needed when not at the top
564 queue_specific_type_function(Type
* type
, Named_type
* name
, int64_t size
,
565 const std::string
& hash_name
,
566 Function_type
* hash_fntype
,
567 const std::string
& equal_name
,
568 Function_type
* equal_fntype
);
570 // Write out queued specific type functions.
572 write_specific_type_functions();
574 // Whether we are done writing out specific type functions.
576 specific_type_functions_are_written() const
577 { return this->specific_type_functions_are_written_
; }
579 // Add a pointer that needs to be added to the list of objects
580 // traversed by the garbage collector. This should be an expression
581 // of pointer type that points to static storage. It's not
582 // necessary to add global variables to this list, just global
583 // variable initializers that would otherwise not be seen.
585 add_gc_root(Expression
* expr
)
587 this->set_need_init_fn();
588 this->gc_roots_
.push_back(expr
);
591 // Traverse the tree. See the Traverse class.
595 // Define the predeclared global names.
597 define_global_names();
599 // Verify and complete all types.
603 // Lower the parse tree.
607 // Lower all the statements in a block.
609 lower_block(Named_object
* function
, Block
*);
611 // Lower an expression.
613 lower_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
617 lower_constant(Named_object
*);
619 // Flatten all the statements in a block.
621 flatten_block(Named_object
* function
, Block
*);
623 // Flatten an expression.
625 flatten_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
627 // Create all necessary function descriptors.
629 create_function_descriptors();
631 // Finalize the method lists and build stub methods for named types.
635 // Work out the types to use for unspecified variables and
640 // Type check the program.
644 // Check the types in a single block. This is used for complicated
647 check_types_in_block(Block
*);
649 // Check for return statements.
651 check_return_statements();
653 // Analyze the program flow for escape information.
657 // Discover the groups of possibly recursive functions in this package.
659 discover_analysis_sets();
661 // Build a connectivity graph between the objects in each analyzed function.
663 assign_connectivity(Escape_context
*, Named_object
*);
665 // Traverse the objects in the connecitivty graph from the sink, adjusting the
666 // escape levels of each object.
668 propagate_escape(Escape_context
*, Node
*);
670 // Add notes about the escape level of a function's input and output
671 // parameters for exporting and importing top level functions.
673 tag_function(Escape_context
*, Named_object
*);
679 // Add an import control function for an imported package to the
682 add_import_init_fn(const std::string
& package_name
,
683 const std::string
& init_name
, int prio
);
685 // Return the Import_init for a given init name.
687 lookup_init(const std::string
& init_name
);
689 // Turn short-cut operators (&&, ||) into explicit if statements.
693 // Use temporary variables to force order of evaluation.
697 // Add write barriers as needed.
699 add_write_barriers();
701 // Return whether an assignment that sets LHS to RHS needs a write
704 assign_needs_write_barrier(Expression
* lhs
);
706 // Return an assignment that sets LHS to RHS using a write barrier.
707 // This returns an if statement that checks whether write barriers
708 // are enabled. If not, it does LHS = RHS, otherwise it calls the
709 // appropriate write barrier function.
711 assign_with_write_barrier(Function
*, Block
*, Statement_inserter
*,
712 Expression
* lhs
, Expression
* rhs
, Location
);
714 // Flatten parse tree.
718 // Build thunks for functions which call recover.
720 build_recover_thunks();
722 // Return a declaration for __builtin_return_address or
723 // __builtin_frame_address.
725 declare_builtin_rf_address(const char* name
);
727 // Simplify statements which might use thunks: go and defer
730 simplify_thunk_statements();
732 // Dump AST if -fgo-dump-ast is set
734 dump_ast(const char* basename
);
736 // Dump Call Graph if -fgo-dump-calls is set.
738 dump_call_graph(const char* basename
);
740 // Dump Connection Graphs if -fgo-dump-connections is set.
742 dump_connection_graphs(const char* basename
);
744 // Convert named types to the backend representation.
746 convert_named_types();
748 // Convert named types in a list of bindings.
750 convert_named_types_in_bindings(Bindings
*);
752 // True if named types have been converted to the backend
755 named_types_are_converted() const
756 { return this->named_types_are_converted_
; }
758 // Give an error if the initialization of VAR depends on itself.
760 check_self_dep(Named_object
*);
762 // Write out the global values.
766 // Build a call to the runtime error function.
768 runtime_error(int code
, Location
);
770 // Build required interface method tables.
772 build_interface_method_tables();
774 // Return an expression which allocates memory to hold values of type TYPE.
776 allocate_memory(Type
*type
, Location
);
778 // Return the assembler name to use for an exported function, a
779 // method, or a function/method declaration.
781 function_asm_name(const std::string
& go_name
, const Package
*,
782 const Type
* receiver
);
784 // Return the name to use for a function descriptor.
786 function_descriptor_name(Named_object
*);
788 // Return the name to use for a generated stub method.
790 stub_method_name(const std::string
& method_name
);
792 // Return the names of the hash and equality functions for TYPE.
794 specific_type_function_names(const Type
*, const Named_type
*,
795 std::string
* hash_name
,
796 std::string
* equal_name
);
798 // Return the assembler name to use for a global variable.
800 global_var_asm_name(const std::string
& go_name
, const Package
*);
802 // Return a name to use for an error case. This should only be used
803 // after reporting an error, and is used to avoid useless knockon
808 // Return whether the name indicates an error.
810 is_erroneous_name(const std::string
&);
812 // Return a name to use for a thunk function. A thunk function is
813 // one we create during the compilation, for a go statement or a
814 // defer statement or a method expression.
818 // Return whether an object is a thunk.
820 is_thunk(const Named_object
*);
822 // Return the name to use for an init function.
824 init_function_name();
826 // Return the name to use for a nested function.
828 nested_function_name();
830 // Return the name to use for a sink funciton.
832 sink_function_name();
834 // Return the name to use for an (erroneous) redefined function.
836 redefined_function_name();
838 // Return the name for use for a recover thunk.
840 recover_thunk_name(const std::string
& name
, const Type
* rtype
);
842 // Return the name to use for the GC root variable.
846 // Return the name to use for a composite literal or string
851 // Return the name of the variable used to represent the zero value
854 map_zero_value_name();
856 // Get the name of the magic initialization function.
860 // Return the name for a type descriptor symbol.
862 type_descriptor_name(Type
*, Named_type
*);
864 // Return the assembler name for the GC symbol for a type.
866 gc_symbol_name(Type
*);
868 // Return the assembler name for a ptrmask variable.
870 ptrmask_symbol_name(const std::string
& ptrmask_sym_name
);
872 // Return the name to use for an interface method table.
874 interface_method_table_name(Interface_type
*, Type
*, bool is_pointer
);
877 // During parsing, we keep a stack of functions. Each function on
878 // the stack is one that we are currently parsing. For each
879 // function, we keep track of the current stack of blocks.
883 Named_object
* function
;
884 // The stack of active blocks in the function.
885 std::vector
<Block
*> blocks
;
888 // The stack of functions.
889 typedef std::vector
<Open_function
> Open_functions
;
891 // Set up the built-in unsafe package.
893 import_unsafe(const std::string
&, bool is_exported
, Location
);
895 // Return the current binding contour.
900 current_bindings() const;
905 // Get the decl for the magic initialization function.
907 initialization_function_decl();
909 // Create the magic initialization function.
911 create_initialization_function(Named_object
* fndecl
, Bstatement
* code_stmt
);
913 // Initialize imported packages. BFUNCTION is the function
914 // into which the package init calls will be placed.
916 init_imports(std::vector
<Bstatement
*>&, Bfunction
* bfunction
);
918 // Register variables with the garbage collector.
920 register_gc_vars(const std::vector
<Named_object
*>&,
921 std::vector
<Bstatement
*>&,
922 Bfunction
* init_bfunction
);
925 write_barrier_variable();
928 check_write_barrier(Block
*, Statement
*, Statement
*);
930 // Type used to map import names to packages.
931 typedef std::map
<std::string
, Package
*> Imports
;
933 // Type used to map package names to packages.
934 typedef std::map
<std::string
, Package
*> Packages
;
936 // Type used to map variables to the function calls that set them.
937 // This is used for initialization dependency analysis.
938 typedef std::map
<Variable
*, Named_object
*> Var_deps
;
940 // Type used to map identifiers in the file block to the location
941 // where they were defined.
942 typedef Unordered_map(std::string
, Location
) File_block_names
;
944 // Type used to queue writing a type specific function.
945 struct Specific_type_function
950 std::string hash_name
;
951 Function_type
* hash_fntype
;
952 std::string equal_name
;
953 Function_type
* equal_fntype
;
955 Specific_type_function(Type
* atype
, Named_type
* aname
, int64_t asize
,
956 const std::string
& ahash_name
,
957 Function_type
* ahash_fntype
,
958 const std::string
& aequal_name
,
959 Function_type
* aequal_fntype
)
960 : type(atype
), name(aname
), size(asize
), hash_name(ahash_name
),
961 hash_fntype(ahash_fntype
), equal_name(aequal_name
),
962 equal_fntype(aequal_fntype
)
966 // Recompute init priorities.
968 recompute_init_priorities();
970 // Recursive helper used by the routine above.
972 update_init_priority(Import_init
* ii
,
973 std::set
<const Import_init
*>* visited
);
975 // The backend generator.
977 // The object used to keep track of file names and line numbers.
979 // The package we are compiling.
981 // The list of currently open functions during parsing.
982 Open_functions functions_
;
983 // The global binding contour. This includes the builtin functions
984 // and the package we are compiling.
986 // The list of names we have seen in the file block.
987 File_block_names file_block_names_
;
988 // Mapping from import file names to packages.
990 // Whether the magic unsafe package was imported.
991 bool imported_unsafe_
;
992 // Whether the magic unsafe package was imported by the current file.
993 bool current_file_imported_unsafe_
;
994 // Mapping from package names we have seen to packages. This does
995 // not include the package we are compiling.
997 // The functions named "init", if there are any.
998 std::vector
<Named_object
*> init_functions_
;
999 // A mapping from variables to the function calls that initialize
1000 // them, if it is not stored in the variable's init or preinit.
1001 // This is used for dependency analysis.
1003 // Whether we need a magic initialization function.
1005 // The name of the magic initialization function.
1006 std::string init_fn_name_
;
1007 // A list of import control variables for packages that we import.
1008 Import_init_set imported_init_fns_
;
1009 // The package path used for reflection data.
1010 std::string pkgpath_
;
1011 // The package path to use for a symbol name.
1012 std::string pkgpath_symbol_
;
1013 // The prefix to use for symbols, from the -fgo-prefix option.
1014 std::string prefix_
;
1015 // Whether pkgpath_ has been set.
1017 // Whether an explicit package path was set by -fgo-pkgpath.
1018 bool pkgpath_from_option_
;
1019 // Whether an explicit prefix was set by -fgo-prefix.
1020 bool prefix_from_option_
;
1021 // The relative import path, from the -fgo-relative-import-path
1023 std::string relative_import_path_
;
1024 // The C header file to write, from the -fgo-c-header option.
1025 std::string c_header_
;
1026 // Whether or not to check for division by zero, from the
1027 // -fgo-check-divide-zero option.
1028 bool check_divide_by_zero_
;
1029 // Whether or not to check for division overflow, from the
1030 // -fgo-check-divide-overflow option.
1031 bool check_divide_overflow_
;
1032 // Whether we are compiling the runtime package, from the
1033 // -fgo-compiling-runtime option.
1034 bool compiling_runtime_
;
1035 // The level of escape analysis debug information to emit, from the
1036 // -fgo-debug-escape option.
1037 int debug_escape_level_
;
1038 // Nil-check size threshhold.
1039 int64_t nil_check_size_threshold_
;
1040 // A list of types to verify.
1041 std::vector
<Type
*> verify_types_
;
1042 // A list of interface types defined while parsing.
1043 std::vector
<Interface_type
*> interface_types_
;
1044 // Type specific functions to write out.
1045 std::vector
<Specific_type_function
*> specific_type_functions_
;
1046 // Whether we are done writing out specific type functions.
1047 bool specific_type_functions_are_written_
;
1048 // Whether named types have been converted.
1049 bool named_types_are_converted_
;
1050 // A list containing groups of possibly mutually recursive functions to be
1051 // considered during escape analysis.
1052 std::vector
<Analysis_set
> analysis_sets_
;
1053 // A list of objects to add to the GC roots.
1054 std::vector
<Expression
*> gc_roots_
;
1057 // A block of statements.
1062 Block(Block
* enclosing
, Location
);
1064 // Return the enclosing block.
1067 { return this->enclosing_
; }
1069 // Return the bindings of the block.
1072 { return this->bindings_
; }
1076 { return this->bindings_
; }
1078 // Look at the block's statements.
1079 const std::vector
<Statement
*>*
1081 { return &this->statements_
; }
1083 // Return the start location. This is normally the location of the
1084 // left curly brace which starts the block.
1086 start_location() const
1087 { return this->start_location_
; }
1089 // Return the end location. This is normally the location of the
1090 // right curly brace which ends the block.
1092 end_location() const
1093 { return this->end_location_
; }
1095 // Add a statement to the block.
1097 add_statement(Statement
*);
1099 // Add a statement to the front of the block.
1101 add_statement_at_front(Statement
*);
1103 // Replace a statement in a block.
1105 replace_statement(size_t index
, Statement
*);
1107 // Add a Statement before statement number INDEX.
1109 insert_statement_before(size_t index
, Statement
*);
1111 // Add a Statement after statement number INDEX.
1113 insert_statement_after(size_t index
, Statement
*);
1115 // Set the end location of the block.
1117 set_end_location(Location location
)
1118 { this->end_location_
= location
; }
1120 // Traverse the tree.
1122 traverse(Traverse
*);
1124 // Set final types for unspecified variables and constants.
1128 // Return true if execution of this block may fall through to the
1131 may_fall_through() const;
1133 // Convert the block to the backend representation.
1135 get_backend(Translate_context
*);
1137 // Iterate over statements.
1139 typedef std::vector
<Statement
*>::iterator iterator
;
1143 { return this->statements_
.begin(); }
1147 { return this->statements_
.end(); }
1152 // Statements in the block.
1153 std::vector
<Statement
*> statements_
;
1155 Bindings
* bindings_
;
1156 // Location of start of block.
1157 Location start_location_
;
1158 // Location of end of block.
1159 Location end_location_
;
1167 Function(Function_type
* type
, Named_object
*, Block
*, Location
);
1169 // Return the function's type.
1172 { return this->type_
; }
1174 // Return the enclosing function if there is one.
1177 { return this->enclosing_
; }
1179 // Set the enclosing function. This is used when building thunks
1180 // for functions which call recover.
1182 set_enclosing(Named_object
* enclosing
)
1184 go_assert(this->enclosing_
== NULL
);
1185 this->enclosing_
= enclosing
;
1188 // The result variables.
1189 typedef std::vector
<Named_object
*> Results
;
1191 // Create the result variables in the outer block.
1193 create_result_variables(Gogo
*);
1195 // Update the named result variables when cloning a function which
1198 update_result_variables();
1200 // Return the result variables.
1203 { return this->results_
; }
1207 { return this->is_sink_
; }
1211 { this->is_sink_
= true; }
1213 // Whether the result variables have names.
1215 results_are_named() const
1216 { return this->results_are_named_
; }
1218 // Set the assembler name.
1220 set_asm_name(const std::string
& asm_name
)
1221 { this->asm_name_
= asm_name
; }
1223 // Return the pragmas for this function.
1226 { return this->pragmas_
; }
1228 // Set the pragmas for this function.
1230 set_pragmas(unsigned int pragmas
)
1232 this->pragmas_
= pragmas
;
1235 // Whether this method should not be included in the type
1238 nointerface() const;
1240 // Record that this method should not be included in the type
1245 // Record that this function is a stub method created for an unnamed
1248 set_is_unnamed_type_stub_method()
1250 go_assert(this->is_method());
1251 this->is_unnamed_type_stub_method_
= true;
1254 // Return the amount of enclosed variables in this closure.
1256 closure_field_count() const
1257 { return this->closure_fields_
.size(); }
1259 // Add a new field to the closure variable.
1261 add_closure_field(Named_object
* var
, Location loc
)
1262 { this->closure_fields_
.push_back(std::make_pair(var
, loc
)); }
1264 // Whether this function needs a closure.
1266 needs_closure() const
1267 { return !this->closure_fields_
.empty(); }
1269 // Return the closure variable, creating it if necessary. This is
1270 // passed to the function as a static chain parameter.
1274 // Set the closure variable. This is used when building thunks for
1275 // functions which call recover.
1277 set_closure_var(Named_object
* v
)
1279 go_assert(this->closure_var_
== NULL
);
1280 this->closure_var_
= v
;
1283 // Return the variable for a reference to field INDEX in the closure
1286 enclosing_var(unsigned int index
)
1288 go_assert(index
< this->closure_fields_
.size());
1289 return closure_fields_
[index
].first
;
1292 // Set the type of the closure variable if there is one.
1296 // Get the block of statements associated with the function.
1299 { return this->block_
; }
1301 // Get the location of the start of the function.
1304 { return this->location_
; }
1306 // Return whether this function is actually a method.
1310 // Add a label definition to the function.
1312 add_label_definition(Gogo
*, const std::string
& label_name
, Location
);
1314 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1315 // if we should report errors for a goto from the current location
1316 // to the label location.
1318 add_label_reference(Gogo
*, const std::string
& label_name
,
1319 Location
, bool issue_goto_errors
);
1321 // Warn about labels that are defined but not used.
1323 check_labels() const;
1325 // Note that a new local type has been added. Return its index.
1327 new_local_type_index()
1328 { return this->local_type_count_
++; }
1330 // Whether this function calls the predeclared recover function.
1332 calls_recover() const
1333 { return this->calls_recover_
; }
1335 // Record that this function calls the predeclared recover function.
1336 // This is set during the lowering pass.
1339 { this->calls_recover_
= true; }
1341 // Whether this is a recover thunk function.
1343 is_recover_thunk() const
1344 { return this->is_recover_thunk_
; }
1346 // Record that this is a thunk built for a function which calls
1349 set_is_recover_thunk()
1350 { this->is_recover_thunk_
= true; }
1352 // Whether this function already has a recover thunk.
1354 has_recover_thunk() const
1355 { return this->has_recover_thunk_
; }
1357 // Record that this function already has a recover thunk.
1359 set_has_recover_thunk()
1360 { this->has_recover_thunk_
= true; }
1362 // Record that this function is a thunk created for a defer
1363 // statement that calls the __go_set_defer_retaddr runtime function.
1365 set_calls_defer_retaddr()
1366 { this->calls_defer_retaddr_
= true; }
1368 // Whether this is a type hash or equality function created by the
1371 is_type_specific_function()
1372 { return this->is_type_specific_function_
; }
1374 // Record that this function is a type hash or equality function
1375 // created by the compiler.
1377 set_is_type_specific_function()
1378 { this->is_type_specific_function_
= true; }
1380 // Mark the function as going into a unique section.
1382 set_in_unique_section()
1383 { this->in_unique_section_
= true; }
1385 // Swap with another function. Used only for the thunk which calls
1388 swap_for_recover(Function
*);
1390 // Traverse the tree.
1392 traverse(Traverse
*);
1394 // Determine types in the function.
1398 // Return an expression for the function descriptor, given the named
1399 // object for this function. This may only be called for functions
1400 // without a closure. This will be an immutable struct with one
1401 // field that points to the function's code.
1403 descriptor(Gogo
*, Named_object
*);
1405 // Set the descriptor for this function. This is used when a
1406 // function declaration is followed by a function definition.
1408 set_descriptor(Expression
* descriptor
)
1410 go_assert(this->descriptor_
== NULL
);
1411 this->descriptor_
= descriptor
;
1414 // Return the backend representation.
1416 get_or_make_decl(Gogo
*, Named_object
*);
1418 // Return the function's decl after it has been built.
1422 // Set the function decl to hold a backend representation of the function
1425 build(Gogo
*, Named_object
*);
1427 // Get the statement that assigns values to this function's result struct.
1429 return_value(Gogo
*, Named_object
*, Location
) const;
1431 // Get an expression for the variable holding the defer stack.
1433 defer_stack(Location
);
1435 // Export the function.
1437 export_func(Export
*, const std::string
& name
) const;
1439 // Export a function with a type.
1441 export_func_with_type(Export
*, const std::string
& name
,
1442 const Function_type
*);
1444 // Import a function.
1446 import_func(Import
*, std::string
* pname
, Typed_identifier
** receiver
,
1447 Typed_identifier_list
** pparameters
,
1448 Typed_identifier_list
** presults
, bool* is_varargs
);
1451 // Type for mapping from label names to Label objects.
1452 typedef Unordered_map(std::string
, Label
*) Labels
;
1455 build_defer_wrapper(Gogo
*, Named_object
*, Bstatement
**, Bstatement
**);
1457 typedef std::vector
<std::pair
<Named_object
*,
1458 Location
> > Closure_fields
;
1460 // The function's type.
1461 Function_type
* type_
;
1462 // The enclosing function. This is NULL when there isn't one, which
1463 // is the normal case.
1464 Named_object
* enclosing_
;
1465 // The result variables, if any.
1467 // If there is a closure, this is the list of variables which appear
1468 // in the closure. This is created by the parser, and then resolved
1469 // to a real type when we lower parse trees.
1470 Closure_fields closure_fields_
;
1471 // The closure variable, passed as a parameter using the static
1472 // chain parameter. Normally NULL.
1473 Named_object
* closure_var_
;
1474 // The outer block of statements in the function.
1476 // The source location of the start of the function.
1478 // Labels defined or referenced in the function.
1480 // The number of local types defined in this function.
1481 unsigned int local_type_count_
;
1482 // The assembler name: this is the name that will be put in the object file.
1483 // Set by the go:linkname compiler directive. This is normally empty.
1484 std::string asm_name_
;
1485 // The function descriptor, if any.
1486 Expression
* descriptor_
;
1487 // The function decl.
1489 // The defer stack variable. A pointer to this variable is used to
1490 // distinguish the defer stack for one function from another. This
1491 // is NULL unless we actually need a defer stack.
1492 Temporary_statement
* defer_stack_
;
1493 // Pragmas for this function. This is a set of GOPRAGMA bits.
1494 unsigned int pragmas_
;
1495 // True if this function is sink-named. No code is generated.
1497 // True if the result variables are named.
1498 bool results_are_named_
: 1;
1499 // True if this function is a stub method created for an unnamed
1501 bool is_unnamed_type_stub_method_
: 1;
1502 // True if this function calls the predeclared recover function.
1503 bool calls_recover_
: 1;
1504 // True if this a thunk built for a function which calls recover.
1505 bool is_recover_thunk_
: 1;
1506 // True if this function already has a recover thunk.
1507 bool has_recover_thunk_
: 1;
1508 // True if this is a thunk built for a defer statement that calls
1509 // the __go_set_defer_retaddr runtime function.
1510 bool calls_defer_retaddr_
: 1;
1511 // True if this is a function built by the compiler to as a hash or
1512 // equality function for some type.
1513 bool is_type_specific_function_
: 1;
1514 // True if this function should be put in a unique section. This is
1515 // turned on for field tracking.
1516 bool in_unique_section_
: 1;
1519 // A snapshot of the current binding state.
1521 class Bindings_snapshot
1524 Bindings_snapshot(const Block
*, Location
);
1526 // Report any errors appropriate for a goto from the current binding
1527 // state of B to this one.
1529 check_goto_from(const Block
* b
, Location
);
1531 // Report any errors appropriate for a goto from this binding state
1532 // to the current state of B.
1534 check_goto_to(const Block
* b
);
1538 check_goto_block(Location
, const Block
*, const Block
*, size_t*);
1541 check_goto_defs(Location
, const Block
*, size_t, size_t);
1543 // The current block.
1544 const Block
* block_
;
1545 // The number of names currently defined in each open block.
1546 // Element 0 is this->block_, element 1 is
1547 // this->block_->enclosing(), etc.
1548 std::vector
<size_t> counts_
;
1549 // The location where this snapshot was taken.
1553 // A function declaration.
1555 class Function_declaration
1558 Function_declaration(Function_type
* fntype
, Location location
)
1559 : fntype_(fntype
), location_(location
), asm_name_(), descriptor_(NULL
),
1560 fndecl_(NULL
), pragmas_(0)
1565 { return this->fntype_
; }
1569 { return this->location_
; }
1573 { return this->asm_name_
; }
1575 // Set the assembler name.
1577 set_asm_name(const std::string
& asm_name
)
1578 { this->asm_name_
= asm_name
; }
1580 // Set the pragmas for this function.
1582 set_pragmas(unsigned int pragmas
)
1584 this->pragmas_
= pragmas
;
1587 // Return an expression for the function descriptor, given the named
1588 // object for this function. This may only be called for functions
1589 // without a closure. This will be an immutable struct with one
1590 // field that points to the function's code.
1592 descriptor(Gogo
*, Named_object
*);
1594 // Return true if we have created a descriptor for this declaration.
1596 has_descriptor() const
1597 { return this->descriptor_
!= NULL
; }
1599 // Return a backend representation.
1601 get_or_make_decl(Gogo
*, Named_object
*);
1603 // If there is a descriptor, build it into the backend
1606 build_backend_descriptor(Gogo
*);
1608 // Export a function declaration.
1610 export_func(Export
* exp
, const std::string
& name
) const
1611 { Function::export_func_with_type(exp
, name
, this->fntype_
); }
1613 // Check that the types used in this declaration's signature are defined.
1615 check_types() const;
1618 // The type of the function.
1619 Function_type
* fntype_
;
1620 // The location of the declaration.
1622 // The assembler name: this is the name to use in references to the
1623 // function. This is normally empty.
1624 std::string asm_name_
;
1625 // The function descriptor, if any.
1626 Expression
* descriptor_
;
1627 // The function decl if needed.
1629 // Pragmas for this function. This is a set of GOPRAGMA bits.
1630 unsigned int pragmas_
;
1638 Variable(Type
*, Expression
*, bool is_global
, bool is_parameter
,
1639 bool is_receiver
, Location
);
1641 // Get the type of the variable.
1648 // Return whether the type is defined yet.
1652 // Get the initial value.
1655 { return this->init_
; }
1657 // Return whether there are any preinit statements.
1659 has_pre_init() const
1660 { return this->preinit_
!= NULL
; }
1662 // Return the preinit statements if any.
1665 { return this->preinit_
; }
1667 // Return whether this is a global variable.
1670 { return this->is_global_
; }
1672 // Return whether this is a function parameter.
1674 is_parameter() const
1675 { return this->is_parameter_
; }
1677 // Return whether this is a closure (static chain) parameter.
1680 { return this->is_closure_
; }
1682 // Change this parameter to be a closure.
1686 this->is_closure_
= true;
1689 // Return whether this is the receiver parameter of a method.
1692 { return this->is_receiver_
; }
1694 // Change this parameter to be a receiver. This is used when
1695 // creating the thunks created for functions which call recover.
1699 go_assert(this->is_parameter_
);
1700 this->is_receiver_
= true;
1703 // Change this parameter to not be a receiver. This is used when
1704 // creating the thunks created for functions which call recover.
1706 set_is_not_receiver()
1708 go_assert(this->is_parameter_
);
1709 this->is_receiver_
= false;
1712 // Return whether this is the varargs parameter of a function.
1714 is_varargs_parameter() const
1715 { return this->is_varargs_parameter_
; }
1717 // Whether this variable's address is taken.
1719 is_address_taken() const
1720 { return this->is_address_taken_
; }
1722 // Whether this variable should live in the heap.
1726 return this->is_address_taken_
1728 && !this->is_global_
;
1731 // Note that something takes the address of this variable.
1734 { this->is_address_taken_
= true; }
1736 // Return whether the address is taken but does not escape.
1738 is_non_escaping_address_taken() const
1739 { return this->is_non_escaping_address_taken_
; }
1741 // Note that something takes the address of this variable such that
1742 // the address does not escape the function.
1744 set_non_escaping_address_taken()
1745 { this->is_non_escaping_address_taken_
= true; }
1747 // Return whether this variable escapes the function it is declared in.
1750 { return this->escapes_
; }
1752 // Note that this variable does not escape the function it is declared in.
1754 set_does_not_escape()
1755 { this->escapes_
= false; }
1757 // Get the source location of the variable's declaration.
1760 { return this->location_
; }
1762 // Record that this is the varargs parameter of a function.
1764 set_is_varargs_parameter()
1766 go_assert(this->is_parameter_
);
1767 this->is_varargs_parameter_
= true;
1770 // Return whether the variable has been used.
1773 { return this->is_used_
; }
1775 // Mark that the variable has been used.
1778 { this->is_used_
= true; }
1780 // Clear the initial value; used for error handling and write barriers.
1783 { this->init_
= NULL
; }
1785 // Set the initial value; used for converting shortcuts.
1787 set_init(Expression
* init
)
1788 { this->init_
= init
; }
1790 // Get the preinit block, a block of statements to be run before the
1791 // initialization expression.
1793 preinit_block(Gogo
*);
1795 // Add a statement to be run before the initialization expression.
1796 // This is only used for global variables.
1798 add_preinit_statement(Gogo
*, Statement
*);
1800 // Lower the initialization expression after parsing is complete.
1802 lower_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1804 // Flatten the initialization expression after ordering evaluations.
1806 flatten_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1808 // A special case: the init value is used only to determine the
1809 // type. This is used if the variable is defined using := with the
1810 // comma-ok form of a map index or a receive expression. The init
1811 // value is actually the map index expression or receive expression.
1812 // We use this because we may not know the right type at parse time.
1814 set_type_from_init_tuple()
1815 { this->type_from_init_tuple_
= true; }
1817 // Another special case: the init value is used only to determine
1818 // the type. This is used if the variable is defined using := with
1819 // a range clause. The init value is the range expression. The
1820 // type of the variable is the index type of the range expression
1821 // (i.e., the first value returned by a range).
1823 set_type_from_range_index()
1824 { this->type_from_range_index_
= true; }
1826 // Another special case: like set_type_from_range_index, but the
1827 // type is the value type of the range expression (i.e., the second
1828 // value returned by a range).
1830 set_type_from_range_value()
1831 { this->type_from_range_value_
= true; }
1833 // Another special case: the init value is used only to determine
1834 // the type. This is used if the variable is defined using := with
1835 // a case in a select statement. The init value is the channel.
1836 // The type of the variable is the channel's element type.
1838 set_type_from_chan_element()
1839 { this->type_from_chan_element_
= true; }
1841 // After we lower the select statement, we once again set the type
1842 // from the initialization expression.
1844 clear_type_from_chan_element()
1846 go_assert(this->type_from_chan_element_
);
1847 this->type_from_chan_element_
= false;
1850 // TRUE if this variable was created for a type switch clause.
1852 is_type_switch_var() const
1853 { return this->is_type_switch_var_
; }
1855 // Note that this variable was created for a type switch clause.
1857 set_is_type_switch_var()
1858 { this->is_type_switch_var_
= true; }
1860 // Mark the variable as going into a unique section.
1862 set_in_unique_section()
1864 go_assert(this->is_global_
);
1865 this->in_unique_section_
= true;
1868 // Traverse the initializer expression.
1870 traverse_expression(Traverse
*, unsigned int traverse_mask
);
1872 // Determine the type of the variable if necessary.
1876 // Get the backend representation of the variable.
1878 get_backend_variable(Gogo
*, Named_object
*, const Package
*,
1879 const std::string
&);
1881 // Get the initial value of the variable. This may only
1882 // be called if has_pre_init() returns false.
1884 get_init(Gogo
*, Named_object
* function
);
1886 // Return a series of statements which sets the value of the
1887 // variable in DECL. This should only be called is has_pre_init()
1888 // returns true. DECL may be NULL for a sink variable.
1890 get_init_block(Gogo
*, Named_object
* function
, Bvariable
* decl
);
1892 // Export the variable.
1894 export_var(Export
*, const std::string
& name
) const;
1896 // Import a variable.
1898 import_var(Import
*, std::string
* pname
, Type
** ptype
);
1901 // The type of a tuple.
1903 type_from_tuple(Expression
*, bool) const;
1905 // The type of a range.
1907 type_from_range(Expression
*, bool, bool) const;
1909 // The element type of a channel.
1911 type_from_chan_element(Expression
*, bool) const;
1913 // The variable's type. This may be NULL if the type is set from
1916 // The initial value. This may be NULL if the variable should be
1917 // initialized to the default value for the type.
1919 // Statements to run before the init statement.
1921 // Location of variable definition.
1923 // Backend representation.
1924 Bvariable
* backend_
;
1925 // Whether this is a global variable.
1926 bool is_global_
: 1;
1927 // Whether this is a function parameter.
1928 bool is_parameter_
: 1;
1929 // Whether this is a closure parameter.
1930 bool is_closure_
: 1;
1931 // Whether this is the receiver parameter of a method.
1932 bool is_receiver_
: 1;
1933 // Whether this is the varargs parameter of a function.
1934 bool is_varargs_parameter_
: 1;
1935 // Whether this variable is ever referenced.
1937 // Whether something takes the address of this variable. For a
1938 // local variable this implies that the variable has to be on the
1939 // heap if it escapes from its function.
1940 bool is_address_taken_
: 1;
1941 // Whether something takes the address of this variable such that
1942 // the address does not escape the function.
1943 bool is_non_escaping_address_taken_
: 1;
1944 // True if we have seen this variable in a traversal.
1946 // True if we have lowered the initialization expression.
1947 bool init_is_lowered_
: 1;
1948 // True if we have flattened the initialization expression.
1949 bool init_is_flattened_
: 1;
1950 // True if init is a tuple used to set the type.
1951 bool type_from_init_tuple_
: 1;
1952 // True if init is a range clause and the type is the index type.
1953 bool type_from_range_index_
: 1;
1954 // True if init is a range clause and the type is the value type.
1955 bool type_from_range_value_
: 1;
1956 // True if init is a channel and the type is the channel's element type.
1957 bool type_from_chan_element_
: 1;
1958 // True if this is a variable created for a type switch case.
1959 bool is_type_switch_var_
: 1;
1960 // True if we have determined types.
1961 bool determined_type_
: 1;
1962 // True if this variable should be put in a unique section. This is
1963 // used for field tracking.
1964 bool in_unique_section_
: 1;
1965 // Whether this variable escapes the function it is created in. This is
1966 // true until shown otherwise.
1970 // A variable which is really the name for a function return value, or
1973 class Result_variable
1976 Result_variable(Type
* type
, Function
* function
, int index
,
1978 : type_(type
), function_(function
), index_(index
), location_(location
),
1979 backend_(NULL
), is_address_taken_(false),
1980 is_non_escaping_address_taken_(false), escapes_(true)
1983 // Get the type of the result variable.
1986 { return this->type_
; }
1988 // Get the function that this is associated with.
1991 { return this->function_
; }
1993 // Index in the list of function results.
1996 { return this->index_
; }
1998 // The location of the variable definition.
2001 { return this->location_
; }
2003 // Whether this variable's address is taken.
2005 is_address_taken() const
2006 { return this->is_address_taken_
; }
2008 // Note that something takes the address of this variable.
2011 { this->is_address_taken_
= true; }
2013 // Return whether the address is taken but does not escape.
2015 is_non_escaping_address_taken() const
2016 { return this->is_non_escaping_address_taken_
; }
2018 // Note that something takes the address of this variable such that
2019 // the address does not escape the function.
2021 set_non_escaping_address_taken()
2022 { this->is_non_escaping_address_taken_
= true; }
2024 // Return whether this variable escapes the function it is declared in.
2027 { return this->escapes_
; }
2029 // Note that this variable does not escape the function it is declared in.
2031 set_does_not_escape()
2032 { this->escapes_
= false; }
2034 // Whether this variable should live in the heap.
2038 return this->is_address_taken_
2042 // Set the function. This is used when cloning functions which call
2045 set_function(Function
* function
)
2046 { this->function_
= function
; }
2048 // Get the backend representation of the variable.
2050 get_backend_variable(Gogo
*, Named_object
*, const std::string
&);
2053 // Type of result variable.
2055 // Function with which this is associated.
2056 Function
* function_
;
2057 // Index in list of results.
2059 // Where the result variable is defined.
2061 // Backend representation.
2062 Bvariable
* backend_
;
2063 // Whether something takes the address of this variable.
2064 bool is_address_taken_
;
2065 // Whether something takes the address of this variable such that
2066 // the address does not escape the function.
2067 bool is_non_escaping_address_taken_
;
2068 // Whether this variable escapes the function it is created in. This is
2069 // true until shown otherwise.
2073 // The value we keep for a named constant. This lets us hold a type
2074 // and an expression.
2076 class Named_constant
2079 Named_constant(Type
* type
, Expression
* expr
, int iota_value
,
2081 : type_(type
), expr_(expr
), iota_value_(iota_value
), location_(location
),
2082 lowering_(false), is_sink_(false), bconst_(NULL
)
2087 { return this->type_
; }
2091 { return this->expr_
; }
2095 { return this->iota_value_
; }
2099 { return this->location_
; }
2101 // Whether we are lowering.
2104 { return this->lowering_
; }
2106 // Set that we are lowering.
2109 { this->lowering_
= true; }
2111 // We are no longer lowering.
2114 { this->lowering_
= false; }
2118 { return this->is_sink_
; }
2122 { this->is_sink_
= true; }
2124 // Traverse the expression.
2126 traverse_expression(Traverse
*);
2128 // Determine the type of the constant if necessary.
2132 // Indicate that we found and reported an error for this constant.
2136 // Export the constant.
2138 export_const(Export
*, const std::string
& name
) const;
2140 // Import a constant.
2142 import_const(Import
*, std::string
*, Type
**, Expression
**);
2144 // Get the backend representation of the constant value.
2146 get_backend(Gogo
*, Named_object
*);
2149 // The type of the constant.
2151 // The expression for the constant.
2153 // If the predeclared constant iota is used in EXPR_, this is the
2154 // value it will have. We do this because at parse time we don't
2155 // know whether the name "iota" will refer to the predeclared
2156 // constant or to something else. We put in the right value in when
2159 // The location of the definition.
2161 // Whether we are currently lowering this constant.
2163 // Whether this constant is blank named and needs only type checking.
2165 // The backend representation of the constant value.
2166 Bexpression
* bconst_
;
2169 // A type declaration.
2171 class Type_declaration
2174 Type_declaration(Location location
)
2175 : location_(location
), in_function_(NULL
), in_function_index_(0),
2176 methods_(), issued_warning_(false)
2179 // Return the location.
2182 { return this->location_
; }
2184 // Return the function in which this type is declared. This will
2185 // return NULL for a type declared in global scope.
2187 in_function(unsigned int* pindex
)
2189 *pindex
= this->in_function_index_
;
2190 return this->in_function_
;
2193 // Set the function in which this type is declared.
2195 set_in_function(Named_object
* f
, unsigned int index
)
2197 this->in_function_
= f
;
2198 this->in_function_index_
= index
;
2201 // Add a method to this type. This is used when methods are defined
2204 add_method(const std::string
& name
, Function
* function
);
2206 // Add a method declaration to this type.
2208 add_method_declaration(const std::string
& name
, Package
*,
2209 Function_type
* type
, Location location
);
2211 // Add an already created object as a method.
2213 add_existing_method(Named_object
* no
)
2214 { this->methods_
.push_back(no
); }
2216 // Return whether any methods were defined.
2218 has_methods() const;
2220 // Return the methods.
2221 const std::vector
<Named_object
*>*
2223 { return &this->methods_
; }
2225 // Define methods when the real type is known.
2227 define_methods(Named_type
*);
2229 // This is called if we are trying to use this type. It returns
2230 // true if we should issue a warning.
2235 // The location of the type declaration.
2237 // If this type is declared in a function, a pointer back to the
2238 // function in which it is defined.
2239 Named_object
* in_function_
;
2240 // The index of this type in IN_FUNCTION_.
2241 unsigned int in_function_index_
;
2242 // Methods defined before the type is defined.
2243 std::vector
<Named_object
*> methods_
;
2244 // True if we have issued a warning about a use of this type
2245 // declaration when it is undefined.
2246 bool issued_warning_
;
2249 // An unknown object. These are created by the parser for forward
2250 // references to names which have not been seen before. In a correct
2251 // program, these will always point to a real definition by the end of
2252 // the parse. Because they point to another Named_object, these may
2253 // only be referenced by Unknown_expression objects.
2258 Unknown_name(Location location
)
2259 : location_(location
), real_named_object_(NULL
)
2262 // Return the location where this name was first seen.
2265 { return this->location_
; }
2267 // Return the real named object that this points to, or NULL if it
2268 // was never resolved.
2270 real_named_object() const
2271 { return this->real_named_object_
; }
2273 // Set the real named object that this points to.
2275 set_real_named_object(Named_object
* no
);
2278 // The location where this name was first seen.
2280 // The real named object when it is known.
2285 // A named object named. This is the result of a declaration. We
2286 // don't use a superclass because they all have to be handled
2294 // An uninitialized Named_object. We should never see this.
2295 NAMED_OBJECT_UNINITIALIZED
,
2296 // An erroneous name. This indicates a parse error, to avoid
2297 // later errors about undefined references.
2298 NAMED_OBJECT_ERRONEOUS
,
2299 // An unknown name. This is used for forward references. In a
2300 // correct program, these will all be resolved by the end of the
2302 NAMED_OBJECT_UNKNOWN
,
2307 // A forward type declaration.
2308 NAMED_OBJECT_TYPE_DECLARATION
,
2311 // A result variable in a function.
2312 NAMED_OBJECT_RESULT_VAR
,
2313 // The blank identifier--the special variable named _.
2317 // A forward func declaration.
2318 NAMED_OBJECT_FUNC_DECLARATION
,
2320 NAMED_OBJECT_PACKAGE
2323 // Return the classification.
2325 classification() const
2326 { return this->classification_
; }
2331 is_erroneous() const
2332 { return this->classification_
== NAMED_OBJECT_ERRONEOUS
; }
2336 { return this->classification_
== NAMED_OBJECT_UNKNOWN
; }
2340 { return this->classification_
== NAMED_OBJECT_CONST
; }
2344 { return this->classification_
== NAMED_OBJECT_TYPE
; }
2347 is_type_declaration() const
2348 { return this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
; }
2352 { return this->classification_
== NAMED_OBJECT_VAR
; }
2355 is_result_variable() const
2356 { return this->classification_
== NAMED_OBJECT_RESULT_VAR
; }
2360 { return this->classification_
== NAMED_OBJECT_SINK
; }
2364 { return this->classification_
== NAMED_OBJECT_FUNC
; }
2367 is_function_declaration() const
2368 { return this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
; }
2372 { return this->classification_
== NAMED_OBJECT_PACKAGE
; }
2376 static Named_object
*
2377 make_erroneous_name(const std::string
& name
)
2378 { return new Named_object(name
, NULL
, NAMED_OBJECT_ERRONEOUS
); }
2380 static Named_object
*
2381 make_unknown_name(const std::string
& name
, Location
);
2383 static Named_object
*
2384 make_constant(const Typed_identifier
&, const Package
*, Expression
*,
2387 static Named_object
*
2388 make_type(const std::string
&, const Package
*, Type
*, Location
);
2390 static Named_object
*
2391 make_type_declaration(const std::string
&, const Package
*, Location
);
2393 static Named_object
*
2394 make_variable(const std::string
&, const Package
*, Variable
*);
2396 static Named_object
*
2397 make_result_variable(const std::string
&, Result_variable
*);
2399 static Named_object
*
2402 static Named_object
*
2403 make_function(const std::string
&, const Package
*, Function
*);
2405 static Named_object
*
2406 make_function_declaration(const std::string
&, const Package
*, Function_type
*,
2409 static Named_object
*
2410 make_package(const std::string
& alias
, Package
* package
);
2417 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2418 return this->u_
.unknown_value
;
2422 unknown_value() const
2424 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2425 return this->u_
.unknown_value
;
2431 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2432 return this->u_
.const_value
;
2435 const Named_constant
*
2438 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2439 return this->u_
.const_value
;
2445 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2446 return this->u_
.type_value
;
2452 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2453 return this->u_
.type_value
;
2457 type_declaration_value()
2459 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2460 return this->u_
.type_declaration
;
2463 const Type_declaration
*
2464 type_declaration_value() const
2466 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2467 return this->u_
.type_declaration
;
2473 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2474 return this->u_
.var_value
;
2480 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2481 return this->u_
.var_value
;
2487 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2488 return this->u_
.result_var_value
;
2491 const Result_variable
*
2492 result_var_value() const
2494 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2495 return this->u_
.result_var_value
;
2501 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2502 return this->u_
.func_value
;
2508 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2509 return this->u_
.func_value
;
2512 Function_declaration
*
2513 func_declaration_value()
2515 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2516 return this->u_
.func_declaration_value
;
2519 const Function_declaration
*
2520 func_declaration_value() const
2522 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2523 return this->u_
.func_declaration_value
;
2529 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2530 return this->u_
.package_value
;
2534 package_value() const
2536 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2537 return this->u_
.package_value
;
2542 { return this->name_
; }
2544 // Return the name to use in an error message. The difference is
2545 // that if this Named_object is defined in a different package, this
2546 // will return PACKAGE.NAME.
2548 message_name() const;
2552 { return this->package_
; }
2554 // Resolve an unknown value if possible. This returns the same
2555 // Named_object or a new one.
2559 Named_object
* ret
= this;
2560 if (this->is_unknown())
2562 Named_object
* r
= this->unknown_value()->real_named_object();
2572 const Named_object
* ret
= this;
2573 if (this->is_unknown())
2575 const Named_object
* r
= this->unknown_value()->real_named_object();
2582 // The location where this object was defined or referenced.
2586 // Convert a variable to the backend representation.
2588 get_backend_variable(Gogo
*, Named_object
* function
);
2590 // Return the external identifier for this object.
2594 // Get the backend representation of this object.
2596 get_backend(Gogo
*, std::vector
<Bexpression
*>&, std::vector
<Btype
*>&,
2597 std::vector
<Bfunction
*>&);
2599 // Define a type declaration.
2601 set_type_value(Named_type
*);
2603 // Define a function declaration.
2605 set_function_value(Function
*);
2607 // Declare an unknown name as a type declaration.
2611 // Export this object.
2613 export_named_object(Export
*) const;
2615 // Mark this named object as an invalid redefinition of another object.
2617 set_is_redefinition()
2618 { this->is_redefinition_
= true; }
2620 // Return whether or not this object is a invalid redefinition of another
2623 is_redefinition() const
2624 { return this->is_redefinition_
; }
2627 Named_object(const std::string
&, const Package
*, Classification
);
2629 // The name of the object.
2631 // The package that this object is in. This is NULL if it is in the
2632 // file we are compiling.
2633 const Package
* package_
;
2634 // The type of object this is.
2635 Classification classification_
;
2639 Unknown_name
* unknown_value
;
2640 Named_constant
* const_value
;
2641 Named_type
* type_value
;
2642 Type_declaration
* type_declaration
;
2643 Variable
* var_value
;
2644 Result_variable
* result_var_value
;
2645 Function
* func_value
;
2646 Function_declaration
* func_declaration_value
;
2647 Package
* package_value
;
2649 // True if this object is an invalid redefinition of another object.
2650 bool is_redefinition_
;
2653 // A binding contour. This binds names to objects.
2658 // Type for mapping from names to objects.
2659 typedef Unordered_map(std::string
, Named_object
*) Contour
;
2661 Bindings(Bindings
* enclosing
);
2663 // Add an erroneous name.
2665 add_erroneous_name(const std::string
& name
)
2666 { return this->add_named_object(Named_object::make_erroneous_name(name
)); }
2668 // Add an unknown name.
2670 add_unknown_name(const std::string
& name
, Location location
)
2672 return this->add_named_object(Named_object::make_unknown_name(name
,
2678 add_constant(const Typed_identifier
& tid
, const Package
* package
,
2679 Expression
* expr
, int iota_value
)
2681 return this->add_named_object(Named_object::make_constant(tid
, package
,
2688 add_type(const std::string
& name
, const Package
* package
, Type
* type
,
2691 return this->add_named_object(Named_object::make_type(name
, package
, type
,
2695 // Add a named type. This is used for builtin types, and to add an
2696 // imported type to the global scope.
2698 add_named_type(Named_type
* named_type
);
2700 // Add a type declaration.
2702 add_type_declaration(const std::string
& name
, const Package
* package
,
2705 Named_object
* no
= Named_object::make_type_declaration(name
, package
,
2707 return this->add_named_object(no
);
2712 add_variable(const std::string
& name
, const Package
* package
,
2715 return this->add_named_object(Named_object::make_variable(name
, package
,
2719 // Add a result variable.
2721 add_result_variable(const std::string
& name
, Result_variable
* result
)
2723 return this->add_named_object(Named_object::make_result_variable(name
,
2729 add_function(const std::string
& name
, const Package
*, Function
* function
);
2731 // Add a function declaration.
2733 add_function_declaration(const std::string
& name
, const Package
* package
,
2734 Function_type
* type
, Location location
);
2736 // Add a package. The location is the location of the import
2739 add_package(const std::string
& alias
, Package
* package
)
2741 Named_object
* no
= Named_object::make_package(alias
, package
);
2742 return this->add_named_object(no
);
2745 // Define a type which was already declared.
2747 define_type(Named_object
*, Named_type
*);
2749 // Add a method to the list of objects. This is not added to the
2752 add_method(Named_object
*);
2754 // Add a named object to this binding.
2756 add_named_object(Named_object
* no
)
2757 { return this->add_named_object_to_contour(&this->bindings_
, no
); }
2759 // Clear all names in file scope from the bindings.
2761 clear_file_scope(Gogo
*);
2763 // Look up a name in this binding contour and in any enclosing
2764 // binding contours. This returns NULL if the name is not found.
2766 lookup(const std::string
&) const;
2768 // Look up a name in this binding contour without looking in any
2769 // enclosing binding contours. Returns NULL if the name is not found.
2771 lookup_local(const std::string
&) const;
2775 remove_binding(Named_object
*);
2777 // Mark all variables as used. This is used for some types of parse
2782 // Traverse the tree. See the Traverse class.
2784 traverse(Traverse
*, bool is_global
);
2786 // Iterate over definitions. This does not include things which
2787 // were only declared.
2789 typedef std::vector
<Named_object
*>::const_iterator
2790 const_definitions_iterator
;
2792 const_definitions_iterator
2793 begin_definitions() const
2794 { return this->named_objects_
.begin(); }
2796 const_definitions_iterator
2797 end_definitions() const
2798 { return this->named_objects_
.end(); }
2800 // Return the number of definitions.
2802 size_definitions() const
2803 { return this->named_objects_
.size(); }
2805 // Return whether there are no definitions.
2807 empty_definitions() const
2808 { return this->named_objects_
.empty(); }
2810 // Iterate over declarations. This is everything that has been
2811 // declared, which includes everything which has been defined.
2813 typedef Contour::const_iterator const_declarations_iterator
;
2815 const_declarations_iterator
2816 begin_declarations() const
2817 { return this->bindings_
.begin(); }
2819 const_declarations_iterator
2820 end_declarations() const
2821 { return this->bindings_
.end(); }
2823 // Return the number of declarations.
2825 size_declarations() const
2826 { return this->bindings_
.size(); }
2828 // Return whether there are no declarations.
2830 empty_declarations() const
2831 { return this->bindings_
.empty(); }
2833 // Return the first declaration.
2836 { return this->bindings_
.empty() ? NULL
: this->bindings_
.begin()->second
; }
2840 add_named_object_to_contour(Contour
*, Named_object
*);
2843 new_definition(Named_object
*, Named_object
*);
2845 // Enclosing bindings.
2846 Bindings
* enclosing_
;
2847 // The list of objects.
2848 std::vector
<Named_object
*> named_objects_
;
2849 // The mapping from names to objects.
2858 Label(const std::string
& name
)
2859 : name_(name
), location_(Linemap::unknown_location()), snapshot_(NULL
),
2860 refs_(), is_used_(false), blabel_(NULL
), depth_(DEPTH_UNKNOWN
)
2863 // Return the label's name.
2866 { return this->name_
; }
2868 // Return whether the label has been defined.
2871 { return !Linemap::is_unknown_location(this->location_
); }
2873 // Return whether the label has been used.
2876 { return this->is_used_
; }
2878 // Record that the label is used.
2881 { this->is_used_
= true; }
2883 // Return whether this label is looping.
2886 { return this->depth_
== DEPTH_LOOPING
; }
2888 // Set this label as looping.
2891 { this->depth_
= DEPTH_LOOPING
; }
2893 // Return whether this label is nonlooping.
2896 { return this->depth_
== DEPTH_NONLOOPING
; }
2898 // Set this label as nonlooping.
2901 { this->depth_
= DEPTH_NONLOOPING
; }
2903 // Return the location of the definition.
2906 { return this->location_
; }
2908 // Return the bindings snapshot.
2911 { return this->snapshot_
; }
2913 // Add a snapshot of a goto which refers to this label.
2915 add_snapshot_ref(Bindings_snapshot
* snapshot
)
2917 go_assert(Linemap::is_unknown_location(this->location_
));
2918 this->refs_
.push_back(snapshot
);
2921 // Return the list of snapshots of goto statements which refer to
2923 const std::vector
<Bindings_snapshot
*>&
2925 { return this->refs_
; }
2927 // Clear the references.
2931 // Define the label at LOCATION with the given bindings snapshot.
2933 define(Location location
, Bindings_snapshot
* snapshot
)
2935 if (this->is_dummy_label())
2937 go_assert(Linemap::is_unknown_location(this->location_
)
2938 && this->snapshot_
== NULL
);
2939 this->location_
= location
;
2940 this->snapshot_
= snapshot
;
2943 // Return the backend representation for this label.
2945 get_backend_label(Translate_context
*);
2947 // Return an expression for the address of this label. This is used
2948 // to get the return address of a deferred function to see whether
2949 // the function may call recover.
2951 get_addr(Translate_context
*, Location location
);
2953 // Return a dummy label, representing any instance of the blank label.
2955 create_dummy_label();
2957 // Return TRUE if this is a dummy label.
2959 is_dummy_label() const
2960 { return this->name_
== "_"; }
2962 // A classification of a label's looping depth.
2966 // A label never jumped to.
2968 // A label jumped to.
2973 // The name of the label.
2975 // The location of the definition. This is 0 if the label has not
2976 // yet been defined.
2978 // A snapshot of the set of bindings defined at this label, used to
2979 // issue errors about invalid goto statements.
2980 Bindings_snapshot
* snapshot_
;
2981 // A list of snapshots of goto statements which refer to this label.
2982 std::vector
<Bindings_snapshot
*> refs_
;
2983 // Whether the label has been used.
2985 // The backend representation.
2987 // The looping depth of this label, for escape analysis.
2991 // An unnamed label. These are used when lowering loops.
2996 Unnamed_label(Location location
)
2997 : location_(location
), derived_from_(NULL
), blabel_(NULL
)
3000 // Get the location where the label is defined.
3003 { return this->location_
; }
3005 // Set the location where the label is defined.
3007 set_location(Location location
)
3008 { this->location_
= location
; }
3010 // Get the top level statement this unnamed label is derived from.
3012 derived_from() const
3013 { return this->derived_from_
; }
3015 // Set the top level statement this unnamed label is derived from.
3017 set_derived_from(Statement
* s
)
3018 { this->derived_from_
= s
; }
3020 // Return a statement which defines this label.
3022 get_definition(Translate_context
*);
3024 // Return a goto to this label from LOCATION.
3026 get_goto(Translate_context
*, Location location
);
3029 // Return the backend representation.
3031 get_blabel(Translate_context
*);
3033 // The location where the label is defined.
3035 // The top-level statement this unnamed label was derived/lowered from.
3036 // This is NULL is this label is not the top-level of a lowered statement.
3037 Statement
* derived_from_
;
3038 // The backend representation of this label.
3042 // An alias for an imported package.
3047 Package_alias(Location location
)
3048 : location_(location
), used_(0)
3051 // The location of the import statement.
3054 { return this->location_
; }
3056 // How many symbols from the package were used under this alias.
3059 { return this->used_
; }
3061 // Note that some symbol was used under this alias.
3067 // The location of the import statement.
3069 // The amount of times some name from this package was used under this alias.
3073 // An imported package.
3078 Package(const std::string
& pkgpath
, const std::string
& pkgpath_symbol
,
3081 // Get the package path used for all symbols exported from this
3085 { return this->pkgpath_
; }
3087 // Return the package path to use for a symbol name.
3089 pkgpath_symbol() const;
3091 // Set the package path symbol.
3093 set_pkgpath_symbol(const std::string
&);
3095 // Return the location of the most recent import statement.
3098 { return this->location_
; }
3100 // Return whether we know the name of this package yet.
3102 has_package_name() const
3103 { return !this->package_name_
.empty(); }
3105 // The name that this package uses in its package clause. This may
3106 // be different from the name in the associated Named_object if the
3107 // import statement used an alias.
3109 package_name() const
3111 go_assert(!this->package_name_
.empty());
3112 return this->package_name_
;
3115 // Return the bindings.
3118 { return this->bindings_
; }
3120 // Type used to map import names to package aliases.
3121 typedef std::map
<std::string
, Package_alias
*> Aliases
;
3123 // Return the set of package aliases.
3126 { return this->aliases_
; }
3128 // Note that some symbol from this package was used and qualified by ALIAS.
3129 // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3131 note_usage(const std::string
& alias
) const;
3133 // Note that USAGE might be a fake usage of this package.
3135 note_fake_usage(Expression
* usage
) const
3136 { this->fake_uses_
.insert(usage
); }
3138 // Forget a given USAGE of this package.
3140 forget_usage(Expression
* usage
) const;
3142 // Clear the used field for the next file.
3146 // Look up a name in the package. Returns NULL if the name is not
3149 lookup(const std::string
& name
) const
3150 { return this->bindings_
->lookup(name
); }
3152 // Set the name of the package.
3154 set_package_name(const std::string
& name
, Location
);
3156 // Set the location of the package. This is used to record the most
3157 // recent import location.
3159 set_location(Location location
)
3160 { this->location_
= location
; }
3162 // Add a package name as an ALIAS for this package.
3164 add_alias(const std::string
& alias
, Location
);
3166 // Add a constant to the package.
3168 add_constant(const Typed_identifier
& tid
, Expression
* expr
)
3169 { return this->bindings_
->add_constant(tid
, this, expr
, 0); }
3171 // Add a type to the package.
3173 add_type(const std::string
& name
, Type
* type
, Location location
)
3174 { return this->bindings_
->add_type(name
, this, type
, location
); }
3176 // Add a type declaration to the package.
3178 add_type_declaration(const std::string
& name
, Location location
)
3179 { return this->bindings_
->add_type_declaration(name
, this, location
); }
3181 // Add a variable to the package.
3183 add_variable(const std::string
& name
, Variable
* variable
)
3184 { return this->bindings_
->add_variable(name
, this, variable
); }
3186 // Add a function declaration to the package.
3188 add_function_declaration(const std::string
& name
, Function_type
* type
,
3190 { return this->bindings_
->add_function_declaration(name
, this, type
, loc
); }
3192 // Determine types of constants.
3197 // The package path for type reflection data.
3198 std::string pkgpath_
;
3199 // The package path for symbol names.
3200 std::string pkgpath_symbol_
;
3201 // The name that this package uses in the package clause. This may
3202 // be the empty string if it is not yet known.
3203 std::string package_name_
;
3204 // The names in this package.
3205 Bindings
* bindings_
;
3206 // The location of the most recent import statement.
3208 // The set of aliases associated with this package.
3210 // A set of possibly fake uses of this package. This is mutable because we
3211 // can track fake uses of a package even if we have a const pointer to it.
3212 mutable std::set
<Expression
*> fake_uses_
;
3215 // Return codes for the traversal functions. This is not an enum
3216 // because we want to be able to declare traversal functions in other
3217 // header files without including this one.
3219 // Continue traversal as usual.
3220 const int TRAVERSE_CONTINUE
= -1;
3223 const int TRAVERSE_EXIT
= 0;
3225 // Continue traversal, but skip components of the current object.
3226 // E.g., if this is returned by Traverse::statement, we do not
3227 // traverse the expressions in the statement even if
3228 // traverse_expressions is set in the traverse_mask.
3229 const int TRAVERSE_SKIP_COMPONENTS
= 1;
3231 // This class is used when traversing the parse tree. The caller uses
3232 // a subclass which overrides functions as desired.
3237 // These bitmasks say what to traverse.
3238 static const unsigned int traverse_variables
= 0x1;
3239 static const unsigned int traverse_constants
= 0x2;
3240 static const unsigned int traverse_functions
= 0x4;
3241 static const unsigned int traverse_blocks
= 0x8;
3242 static const unsigned int traverse_statements
= 0x10;
3243 static const unsigned int traverse_expressions
= 0x20;
3244 static const unsigned int traverse_types
= 0x40;
3246 Traverse(unsigned int traverse_mask
)
3247 : traverse_mask_(traverse_mask
), types_seen_(NULL
), expressions_seen_(NULL
)
3250 virtual ~Traverse();
3252 // The bitmask of what to traverse.
3254 traverse_mask() const
3255 { return this->traverse_mask_
; }
3257 // Record that we are going to traverse a type. This returns true
3258 // if the type has already been seen in this traversal. This is
3259 // required because types, unlike expressions, can form a circular
3262 remember_type(const Type
*);
3264 // Record that we are going to see an expression. This returns true
3265 // if the expression has already been seen in this traversal. This
3266 // is only needed for cases where multiple expressions can point to
3269 remember_expression(const Expression
*);
3271 // These functions return one of the TRAVERSE codes defined above.
3273 // If traverse_variables is set in the mask, this is called for
3274 // every variable in the tree.
3276 variable(Named_object
*);
3278 // If traverse_constants is set in the mask, this is called for
3279 // every named constant in the tree. The bool parameter is true for
3280 // a global constant.
3282 constant(Named_object
*, bool);
3284 // If traverse_functions is set in the mask, this is called for
3285 // every function in the tree.
3287 function(Named_object
*);
3289 // If traverse_blocks is set in the mask, this is called for every
3290 // block in the tree.
3294 // If traverse_statements is set in the mask, this is called for
3295 // every statement in the tree.
3297 statement(Block
*, size_t* index
, Statement
*);
3299 // If traverse_expressions is set in the mask, this is called for
3300 // every expression in the tree.
3302 expression(Expression
**);
3304 // If traverse_types is set in the mask, this is called for every
3305 // type in the tree.
3310 // A hash table for types we have seen during this traversal. Note
3311 // that this uses the default hash functions for pointers rather
3312 // than Type_hash_identical and Type_identical. This is because for
3313 // traversal we care about seeing a specific type structure. If
3314 // there are two separate instances of identical types, we want to
3316 typedef Unordered_set(const Type
*) Types_seen
;
3318 typedef Unordered_set(const Expression
*) Expressions_seen
;
3320 // Bitmask of what sort of objects to traverse.
3321 unsigned int traverse_mask_
;
3322 // Types which have been seen in this traversal.
3323 Types_seen
* types_seen_
;
3324 // Expressions which have been seen in this traversal.
3325 Expressions_seen
* expressions_seen_
;
3328 // A class which makes it easier to insert new statements before the
3329 // current statement during a traversal.
3331 class Statement_inserter
3334 // Empty constructor.
3335 Statement_inserter()
3336 : block_(NULL
), pindex_(NULL
), gogo_(NULL
), var_(NULL
)
3339 // Constructor for a statement in a block.
3340 Statement_inserter(Block
* block
, size_t *pindex
)
3341 : block_(block
), pindex_(pindex
), gogo_(NULL
), var_(NULL
)
3344 // Constructor for a global variable.
3345 Statement_inserter(Gogo
* gogo
, Variable
* var
)
3346 : block_(NULL
), pindex_(NULL
), gogo_(gogo
), var_(var
)
3347 { go_assert(var
->is_global()); }
3349 // We use the default copy constructor and assignment operator.
3351 // Insert S before the statement we are traversing, or before the
3352 // initialization expression of a global variable.
3354 insert(Statement
* s
);
3357 // The block that the statement is in.
3359 // The index of the statement that we are traversing.
3361 // The IR, needed when looking at an initializer expression for a
3364 // The global variable, when looking at an initializer expression.
3368 // When translating the gogo IR into the backend data structure, this
3369 // is the context we pass down the blocks and statements.
3371 class Translate_context
3374 Translate_context(Gogo
* gogo
, Named_object
* function
, Block
* block
,
3376 : gogo_(gogo
), backend_(gogo
->backend()), function_(function
),
3377 block_(block
), bblock_(bblock
), is_const_(false)
3384 { return this->gogo_
; }
3388 { return this->backend_
; }
3392 { return this->function_
; }
3396 { return this->block_
; }
3400 { return this->bblock_
; }
3404 { return this->is_const_
; }
3406 // Make a constant context.
3409 { this->is_const_
= true; }
3412 // The IR for the entire compilation unit.
3414 // The generator for the backend data structures.
3416 // The function we are currently translating. NULL if not in a
3417 // function, e.g., the initializer of a global variable.
3418 Named_object
* function_
;
3419 // The block we are currently translating. NULL if not in a
3422 // The backend representation of the current block. NULL if block_
3425 // Whether this is being evaluated in a constant context. This is
3426 // used for type descriptor initializers.
3430 // Runtime error codes. These must match the values in
3431 // libgo/runtime/go-runtime-error.c.
3433 // Slice index out of bounds: negative or larger than the length of
3435 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
= 0;
3437 // Array index out of bounds.
3438 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
= 1;
3440 // String index out of bounds.
3441 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
= 2;
3443 // Slice slice out of bounds: negative or larger than the length of
3444 // the slice or high bound less than low bound.
3445 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
= 3;
3447 // Array slice out of bounds.
3448 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
= 4;
3450 // String slice out of bounds.
3451 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
= 5;
3453 // Dereference of nil pointer. This is used when there is a
3454 // dereference of a pointer to a very large struct or array, to ensure
3455 // that a gigantic array is not used a proxy to access random memory
3457 static const int RUNTIME_ERROR_NIL_DEREFERENCE
= 6;
3459 // Slice length or capacity out of bounds in make: negative or
3460 // overflow or length greater than capacity.
3461 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
= 7;
3463 // Map capacity out of bounds in make: negative or overflow.
3464 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
= 8;
3466 // Channel capacity out of bounds in make: negative or overflow.
3467 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
= 9;
3469 // Division by zero.
3470 static const int RUNTIME_ERROR_DIVISION_BY_ZERO
= 10;
3472 // Go statement with nil function.
3473 static const int RUNTIME_ERROR_GO_NIL
= 11;
3475 // This is used by some of the langhooks.
3476 extern Gogo
* go_get_gogo();
3478 // Whether we have seen any errors. FIXME: Replace with a backend
3480 extern bool saw_errors();
3482 #endif // !defined(GO_GOGO_H)