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
;
16 class Typed_identifier
;
17 class Typed_identifier_list
;
20 class Expression_list
;
22 class Temporary_statement
;
26 class Bindings_snapshot
;
32 class Struct_field_list
;
38 class Forward_declaration_type
;
41 class Translate_context
;
44 class Export_function_body
;
46 class Import_function_body
;
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, append the
205 // appropriate version of the name to the result string.
207 append_possibly_hidden_name(std::string
*result
, const std::string
& name
);
209 // Given a name which may or may not have been hidden, return the
210 // name to use in an error message.
212 message_name(const std::string
& name
);
214 // Return whether a name is the blank identifier _.
216 is_sink_name(const std::string
& name
)
218 return (name
[0] == '.'
219 && name
[name
.length() - 1] == '_'
220 && name
[name
.length() - 2] == '.');
223 // Helper used when adding parameters (including receiver param) to the
224 // bindings of a function. If the specified parameter name is empty or
225 // corresponds to the sink name, param name is replaced with a new unique
226 // name. PNAME is the address of a string containing the parameter variable
227 // name to be checked/updated; TAG is a descriptive tag to be used in
228 // manufacturing the new unique name, and COUNT is the address of a counter
229 // holding the number of params renamed so far with the tag in question.
231 rename_if_empty(std::string
* pname
, const char* tag
, unsigned* count
);
233 // Convert a pkgpath into a string suitable for a symbol
235 pkgpath_for_symbol(const std::string
& pkgpath
);
237 // Compute a hash code for a string, given a seed.
239 hash_string(const std::string
&, unsigned int);
241 // Return the package path to use for reflect.Type.PkgPath.
245 // Return the package path to use for a symbol name.
247 pkgpath_symbol() const;
249 // Set the package path from a command line option.
251 set_pkgpath(const std::string
&);
253 // Set the prefix from a command line option.
255 set_prefix(const std::string
&);
257 // Return whether pkgpath was set from a command line option.
259 pkgpath_from_option() const
260 { return this->pkgpath_from_option_
; }
262 // Return the relative import path as set from the command line.
263 // Returns an empty string if it was not set.
265 relative_import_path() const
266 { return this->relative_import_path_
; }
268 // Set the relative import path from a command line option.
270 set_relative_import_path(const std::string
& s
)
271 { this->relative_import_path_
= s
; }
273 // Set the C header file to write. This is used for the runtime
276 set_c_header(const std::string
& s
)
277 { this->c_header_
= s
; }
279 // Return whether to check for division by zero in binary operations.
281 check_divide_by_zero() const
282 { return this->check_divide_by_zero_
; }
284 // Set the option to check division by zero from a command line option.
286 set_check_divide_by_zero(bool b
)
287 { this->check_divide_by_zero_
= b
; }
289 // Return whether to check for division overflow in binary operations.
291 check_divide_overflow() const
292 { return this->check_divide_overflow_
; }
294 // Set the option to check division overflow from a command line option.
296 set_check_divide_overflow(bool b
)
297 { this->check_divide_overflow_
= b
; }
299 // Return whether we are compiling the runtime package.
301 compiling_runtime() const
302 { return this->compiling_runtime_
; }
304 // Set whether we are compiling the runtime package.
306 set_compiling_runtime(bool b
)
307 { this->compiling_runtime_
= b
; }
309 // Return the level of escape analysis debug information to emit.
311 debug_escape_level() const
312 { return this->debug_escape_level_
; }
314 // Set the level of escape analysis debugging from a command line option.
316 set_debug_escape_level(int level
)
317 { this->debug_escape_level_
= level
; }
319 // Return the hash for debug escape analysis.
321 debug_escape_hash() const
322 { return this->debug_escape_hash_
; }
324 // Set the hash value for debug escape analysis.
326 set_debug_escape_hash(const std::string
& s
)
327 { this->debug_escape_hash_
= s
; }
329 // Return the size threshold used to determine whether to issue
330 // a nil-check for a given pointer dereference. A threshold of -1
331 // implies that all potentially faulting dereference ops should
332 // be nil-checked. A positive threshold of N implies that a deref
333 // of *P where P has size less than N doesn't need a nil check.
335 nil_check_size_threshold() const
336 { return this->nil_check_size_threshold_
; }
338 // Set the nil-check size threshold, as described above.
340 set_nil_check_size_threshold(int64_t bytes
)
341 { this->nil_check_size_threshold_
= bytes
; }
343 // Import a package. FILENAME is the file name argument, LOCAL_NAME
344 // is the local name to give to the package. If LOCAL_NAME is empty
345 // the declarations are added to the global scope.
347 import_package(const std::string
& filename
, const std::string
& local_name
,
348 bool is_local_name_exported
, bool must_exist
, Location
);
350 // Whether we are the global binding level.
352 in_global_scope() const;
354 // Look up a name in the current binding contours.
356 lookup(const std::string
&, Named_object
** pfunction
) const;
358 // Look up a name in the current block.
360 lookup_in_block(const std::string
&) const;
362 // Look up a name in the global namespace--the universal scope.
364 lookup_global(const char*) const;
366 // Add a new imported package. REAL_NAME is the real name of the
367 // package. ALIAS is the alias of the package; this may be the same
368 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
369 // this package should be added to the global namespace; this is
370 // true if the alias is ".". LOCATION is the location of the import
371 // statement. This returns the new package, or NULL on error.
373 add_imported_package(const std::string
& real_name
, const std::string
& alias
,
374 bool is_alias_exported
,
375 const std::string
& pkgpath
,
376 const std::string
& pkgpath_symbol
,
378 bool* padd_to_globals
);
380 // Register a package. This package may or may not be imported.
381 // This returns the Package structure for the package, creating if
384 register_package(const std::string
& pkgpath
,
385 const std::string
& pkgpath_symbol
, Location
);
387 // Look up a package by pkgpath, and return its pkgpath_symbol.
389 pkgpath_symbol_for_package(const std::string
&);
391 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
392 // method function should be added to the type of its receiver.
394 start_function(const std::string
& name
, Function_type
* type
,
395 bool add_method_to_type
, Location
);
397 // Finish compiling a function.
399 finish_function(Location
);
401 // Return the current function.
403 current_function() const;
405 // Return the current block.
409 // Start a new block. This is not initially associated with a
412 start_block(Location
);
414 // Finish the current block and return it.
416 finish_block(Location
);
418 // Declare an erroneous name. This is used to avoid knock-on errors
419 // after a parsing error.
421 add_erroneous_name(const std::string
& name
);
423 // Declare an unknown name. This is used while parsing. The name
424 // must be resolved by the end of the parse. Unknown names are
425 // always added at the package level.
427 add_unknown_name(const std::string
& name
, Location
);
429 // Declare a function.
431 declare_function(const std::string
&, Function_type
*, Location
);
433 // Declare a function at the package level. This is used for
434 // functions generated for a type.
436 declare_package_function(const std::string
&, Function_type
*, Location
);
438 // Add a function declaration to the list of functions we may want
441 add_imported_inlinable_function(Named_object
*);
443 // Add a function to the list of functions that we do want to
446 add_imported_inline_function(Named_object
* no
)
447 { this->imported_inline_functions_
.push_back(no
); }
451 add_label_definition(const std::string
&, Location
);
453 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
454 // report errors for a goto from the current location to the label
457 add_label_reference(const std::string
&, Location
,
458 bool issue_goto_errors
);
460 // An analysis set is a list of functions paired with a boolean that indicates
461 // whether the list of functions are recursive.
462 typedef std::pair
<std::vector
<Named_object
*>, bool> Analysis_set
;
464 // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
467 add_analysis_set(const std::vector
<Named_object
*>& group
, bool recursive
)
468 { this->analysis_sets_
.push_back(std::make_pair(group
, recursive
)); }
470 // Return a snapshot of the current binding state.
472 bindings_snapshot(Location
);
474 // Add a statement to the current block.
476 add_statement(Statement
*);
478 // Add a block to the current block.
480 add_block(Block
*, Location
);
484 add_constant(const Typed_identifier
&, Expression
*, int iota_value
);
488 add_type(const std::string
&, Type
*, Location
);
490 // Add a named type. This is used for builtin types, and to add an
491 // imported type to the global scope.
493 add_named_type(Named_type
*);
497 declare_type(const std::string
&, Location
);
499 // Declare a type at the package level. This is used when the
500 // parser sees an unknown name where a type name is required.
502 declare_package_type(const std::string
&, Location
);
504 // Define a type which was already declared.
506 define_type(Named_object
*, Named_type
*);
510 add_variable(const std::string
&, Variable
*);
512 // Add a sink--a reference to the blank identifier _.
516 // Add a type which needs to be verified. This is used for sink
517 // types, just to give appropriate error messages.
519 add_type_to_verify(Type
* type
);
521 // Add a named object to the current namespace. This is used for
522 // import . "package".
524 add_dot_import_object(Named_object
*);
526 // Add an identifier to the list of names seen in the file block.
528 add_file_block_name(const std::string
& name
, Location location
)
529 { this->file_block_names_
[name
] = location
; }
531 // Add a linkname, from the go:linkname compiler directive. This
532 // changes the externally visible name of go_name to be ext_name.
534 add_linkname(const std::string
& go_name
, bool is_exported
,
535 const std::string
& ext_name
, Location location
);
537 // Mark all local variables in current bindings as used. This is
538 // used when there is a parse error to avoid useless errors.
542 // Note that we've seen an interface type. This is used to build
543 // all required interface method tables.
545 record_interface_type(Interface_type
*);
547 // Note that we need an initialization function.
550 { this->need_init_fn_
= true; }
552 // Return whether the current file imported the unsafe package.
554 current_file_imported_unsafe() const
555 { return this->current_file_imported_unsafe_
; }
557 // Clear out all names in file scope. This is called when we start
558 // parsing a new file.
562 // Record that VAR1 must be initialized after VAR2. This is used
563 // when VAR2 does not appear in VAR1's INIT or PREINIT.
565 record_var_depends_on(Variable
* var1
, Named_object
* var2
)
567 go_assert(this->var_deps_
.find(var1
) == this->var_deps_
.end());
568 this->var_deps_
[var1
] = var2
;
571 // Return the variable that VAR depends on, or NULL if none.
573 var_depends_on(Variable
* var
) const
575 Var_deps::const_iterator p
= this->var_deps_
.find(var
);
576 return p
!= this->var_deps_
.end() ? p
->second
: NULL
;
579 // Queue up a type-specific function to be written out. This is
580 // used when a type-specific function is needed when not at the top
583 queue_specific_type_function(Type
* type
, Named_type
* name
, int64_t size
,
584 const std::string
& hash_name
,
585 Function_type
* hash_fntype
,
586 const std::string
& equal_name
,
587 Function_type
* equal_fntype
);
589 // Write out queued specific type functions.
591 write_specific_type_functions();
593 // Whether we are done writing out specific type functions.
595 specific_type_functions_are_written() const
596 { return this->specific_type_functions_are_written_
; }
598 // Add a pointer that needs to be added to the list of objects
599 // traversed by the garbage collector. This should be an expression
600 // of pointer type that points to static storage. It's not
601 // necessary to add global variables to this list, just global
602 // variable initializers that would otherwise not be seen.
604 add_gc_root(Expression
* expr
)
606 this->set_need_init_fn();
607 this->gc_roots_
.push_back(expr
);
610 // Traverse the tree. See the Traverse class.
614 // Define the predeclared global names.
616 define_global_names();
618 // Verify and complete all types.
622 // Lower the parse tree.
626 // Lower all the statements in a block.
628 lower_block(Named_object
* function
, Block
*);
630 // Lower an expression.
632 lower_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
636 lower_constant(Named_object
*);
638 // Flatten all the statements in a block.
640 flatten_block(Named_object
* function
, Block
*);
642 // Flatten an expression.
644 flatten_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
646 // Create all necessary function descriptors.
648 create_function_descriptors();
650 // Finalize the method lists and build stub methods for named types.
654 // Finalize the method list for one type.
656 finalize_methods_for_type(Type
*);
658 // Work out the types to use for unspecified variables and
663 // Type check the program.
667 // Check the types in a single block. This is used for complicated
670 check_types_in_block(Block
*);
672 // Check for return statements.
674 check_return_statements();
676 // Analyze the program flow for escape information.
680 // Discover the groups of possibly recursive functions in this package.
682 discover_analysis_sets();
684 // Build a connectivity graph between the objects in each analyzed function.
686 assign_connectivity(Escape_context
*, Named_object
*);
688 // Traverse the objects in the connecitivty graph from the sink, adjusting the
689 // escape levels of each object.
691 propagate_escape(Escape_context
*, Node
*);
693 // Add notes about the escape level of a function's input and output
694 // parameters for exporting and importing top level functions.
696 tag_function(Escape_context
*, Named_object
*);
698 // Reclaim memory of escape analysis Nodes.
700 reclaim_escape_nodes();
706 // Add an import control function for an imported package to the
709 add_import_init_fn(const std::string
& package_name
,
710 const std::string
& init_name
, int prio
);
712 // Return the Import_init for a given init name.
714 lookup_init(const std::string
& init_name
);
716 // Turn short-cut operators (&&, ||) into explicit if statements.
720 // Use temporary variables to force order of evaluation.
724 // Add write barriers as needed.
726 add_write_barriers();
728 // Return whether an assignment that sets LHS to RHS needs a write
731 assign_needs_write_barrier(Expression
* lhs
);
733 // Return an assignment that sets LHS to RHS using a write barrier.
734 // This returns an if statement that checks whether write barriers
735 // are enabled. If not, it does LHS = RHS, otherwise it calls the
736 // appropriate write barrier function.
738 assign_with_write_barrier(Function
*, Block
*, Statement_inserter
*,
739 Expression
* lhs
, Expression
* rhs
, Location
);
741 // Flatten parse tree.
745 // Build thunks for functions which call recover.
747 build_recover_thunks();
749 // Return a declaration for __builtin_return_address or
750 // __builtin_dwarf_cfa.
752 declare_builtin_rf_address(const char* name
, bool hasarg
);
754 // Simplify statements which might use thunks: go and defer
757 simplify_thunk_statements();
759 // Dump AST if -fgo-dump-ast is set.
761 dump_ast(const char* basename
);
763 // Dump Call Graph if -fgo-dump-calls is set.
765 dump_call_graph(const char* basename
);
767 // Dump Connection Graphs if -fgo-dump-connections is set.
769 dump_connection_graphs(const char* basename
);
771 // Convert named types to the backend representation.
773 convert_named_types();
775 // Convert named types in a list of bindings.
777 convert_named_types_in_bindings(Bindings
*);
779 // True if named types have been converted to the backend
782 named_types_are_converted() const
783 { return this->named_types_are_converted_
; }
785 // Give an error if the initialization of VAR depends on itself.
787 check_self_dep(Named_object
*);
789 // Write out the global values.
793 // Build a call to the runtime error function.
795 runtime_error(int code
, Location
);
797 // Build required interface method tables.
799 build_interface_method_tables();
801 // Return an expression which allocates memory to hold values of type TYPE.
803 allocate_memory(Type
*type
, Location
);
805 // Return the assembler name to use for an exported function, a
806 // method, or a function/method declaration.
808 function_asm_name(const std::string
& go_name
, const Package
*,
809 const Type
* receiver
);
811 // Return the name to use for a function descriptor.
813 function_descriptor_name(Named_object
*);
815 // Return the name to use for a generated stub method.
817 stub_method_name(const Package
*, const std::string
& method_name
);
819 // Return the names of the hash and equality functions for TYPE.
821 specific_type_function_names(const Type
*, const Named_type
*,
822 std::string
* hash_name
,
823 std::string
* equal_name
);
825 // Return the assembler name to use for a global variable.
827 global_var_asm_name(const std::string
& go_name
, const Package
*);
829 // Return a name to use for an error case. This should only be used
830 // after reporting an error, and is used to avoid useless knockon
835 // Return whether the name indicates an error.
837 is_erroneous_name(const std::string
&);
839 // Return a name to use for a thunk function. A thunk function is
840 // one we create during the compilation, for a go statement or a
841 // defer statement or a method expression.
845 // Return whether an object is a thunk.
847 is_thunk(const Named_object
*);
849 // Return the name to use for an init function.
851 init_function_name();
853 // Return the name to use for a nested function.
855 nested_function_name(Named_object
* enclosing
);
857 // Return the name to use for a sink funciton.
859 sink_function_name();
861 // Return the name to use for an (erroneous) redefined function.
863 redefined_function_name();
865 // Return the name for use for a recover thunk.
867 recover_thunk_name(const std::string
& name
, const Type
* rtype
);
869 // Return the name to use for the GC root variable.
873 // Return the name to use for a composite literal or string
878 // Return the name of the variable used to represent the zero value
881 map_zero_value_name();
883 // Get the name of the magic initialization function.
887 // Return the name for a type descriptor symbol.
889 type_descriptor_name(Type
*, Named_type
*);
891 // Return the assembler name for the GC symbol for a type.
893 gc_symbol_name(Type
*);
895 // Return the assembler name for a ptrmask variable.
897 ptrmask_symbol_name(const std::string
& ptrmask_sym_name
);
899 // Return the name to use for an interface method table.
901 interface_method_table_name(Interface_type
*, Type
*, bool is_pointer
);
903 // Return whether NAME is a special name that can not be passed to
904 // unpack_hidden_name. This is needed because various special names
905 // use "..SUFFIX", but unpack_hidden_name just looks for '.'.
907 is_special_name(const std::string
& name
);
910 // During parsing, we keep a stack of functions. Each function on
911 // the stack is one that we are currently parsing. For each
912 // function, we keep track of the current stack of blocks.
916 Named_object
* function
;
917 // The stack of active blocks in the function.
918 std::vector
<Block
*> blocks
;
921 // The stack of functions.
922 typedef std::vector
<Open_function
> Open_functions
;
924 // Set up the built-in unsafe package.
926 import_unsafe(const std::string
&, bool is_exported
, Location
);
928 // Return the current binding contour.
933 current_bindings() const;
938 // Get the decl for the magic initialization function.
940 initialization_function_decl();
942 // Create the magic initialization function.
944 create_initialization_function(Named_object
* fndecl
, Bstatement
* code_stmt
);
946 // Initialize imported packages. BFUNCTION is the function
947 // into which the package init calls will be placed.
949 init_imports(std::vector
<Bstatement
*>&, Bfunction
* bfunction
);
951 // Register variables with the garbage collector.
953 register_gc_vars(const std::vector
<Named_object
*>&,
954 std::vector
<Bstatement
*>&,
955 Bfunction
* init_bfunction
);
958 propagate_writebarrierrec();
961 write_barrier_variable();
964 check_write_barrier(Block
*, Statement
*, Statement
*);
966 // Type used to map import names to packages.
967 typedef std::map
<std::string
, Package
*> Imports
;
969 // Type used to map package names to packages.
970 typedef std::map
<std::string
, Package
*> Packages
;
972 // Type used to map variables to the function calls that set them.
973 // This is used for initialization dependency analysis.
974 typedef std::map
<Variable
*, Named_object
*> Var_deps
;
976 // Type used to map identifiers in the file block to the location
977 // where they were defined.
978 typedef Unordered_map(std::string
, Location
) File_block_names
;
980 // Type used to queue writing a type specific function.
981 struct Specific_type_function
986 std::string hash_name
;
987 Function_type
* hash_fntype
;
988 std::string equal_name
;
989 Function_type
* equal_fntype
;
991 Specific_type_function(Type
* atype
, Named_type
* aname
, int64_t asize
,
992 const std::string
& ahash_name
,
993 Function_type
* ahash_fntype
,
994 const std::string
& aequal_name
,
995 Function_type
* aequal_fntype
)
996 : type(atype
), name(aname
), size(asize
), hash_name(ahash_name
),
997 hash_fntype(ahash_fntype
), equal_name(aequal_name
),
998 equal_fntype(aequal_fntype
)
1002 // Recompute init priorities.
1004 recompute_init_priorities();
1006 // Recursive helper used by the routine above.
1008 update_init_priority(Import_init
* ii
,
1009 std::set
<const Import_init
*>* visited
);
1011 // The backend generator.
1013 // The object used to keep track of file names and line numbers.
1015 // The package we are compiling.
1017 // The list of currently open functions during parsing.
1018 Open_functions functions_
;
1019 // The global binding contour. This includes the builtin functions
1020 // and the package we are compiling.
1022 // The list of names we have seen in the file block.
1023 File_block_names file_block_names_
;
1024 // Mapping from import file names to packages.
1026 // Whether the magic unsafe package was imported.
1027 bool imported_unsafe_
;
1028 // Whether the magic unsafe package was imported by the current file.
1029 bool current_file_imported_unsafe_
;
1030 // Mapping from package names we have seen to packages. This does
1031 // not include the package we are compiling.
1033 // The functions named "init", if there are any.
1034 std::vector
<Named_object
*> init_functions_
;
1035 // A mapping from variables to the function calls that initialize
1036 // them, if it is not stored in the variable's init or preinit.
1037 // This is used for dependency analysis.
1039 // Whether we need a magic initialization function.
1041 // The name of the magic initialization function.
1042 std::string init_fn_name_
;
1043 // A list of import control variables for packages that we import.
1044 Import_init_set imported_init_fns_
;
1045 // The package path used for reflection data.
1046 std::string pkgpath_
;
1047 // The package path to use for a symbol name.
1048 std::string pkgpath_symbol_
;
1049 // The prefix to use for symbols, from the -fgo-prefix option.
1050 std::string prefix_
;
1051 // Whether pkgpath_ has been set.
1053 // Whether an explicit package path was set by -fgo-pkgpath.
1054 bool pkgpath_from_option_
;
1055 // Whether an explicit prefix was set by -fgo-prefix.
1056 bool prefix_from_option_
;
1057 // The relative import path, from the -fgo-relative-import-path
1059 std::string relative_import_path_
;
1060 // The C header file to write, from the -fgo-c-header option.
1061 std::string c_header_
;
1062 // Whether or not to check for division by zero, from the
1063 // -fgo-check-divide-zero option.
1064 bool check_divide_by_zero_
;
1065 // Whether or not to check for division overflow, from the
1066 // -fgo-check-divide-overflow option.
1067 bool check_divide_overflow_
;
1068 // Whether we are compiling the runtime package, from the
1069 // -fgo-compiling-runtime option.
1070 bool compiling_runtime_
;
1071 // The level of escape analysis debug information to emit, from the
1072 // -fgo-debug-escape option.
1073 int debug_escape_level_
;
1074 // A hash value for debug escape analysis, from the
1075 // -fgo-debug-escape-hash option. The analysis is run only on
1076 // functions with names that hash to the matching value.
1077 std::string debug_escape_hash_
;
1078 // Nil-check size threshhold.
1079 int64_t nil_check_size_threshold_
;
1080 // A list of types to verify.
1081 std::vector
<Type
*> verify_types_
;
1082 // A list of interface types defined while parsing.
1083 std::vector
<Interface_type
*> interface_types_
;
1084 // Type specific functions to write out.
1085 std::vector
<Specific_type_function
*> specific_type_functions_
;
1086 // Whether we are done writing out specific type functions.
1087 bool specific_type_functions_are_written_
;
1088 // Whether named types have been converted.
1089 bool named_types_are_converted_
;
1090 // A list containing groups of possibly mutually recursive functions to be
1091 // considered during escape analysis.
1092 std::vector
<Analysis_set
> analysis_sets_
;
1093 // A list of objects to add to the GC roots.
1094 std::vector
<Expression
*> gc_roots_
;
1095 // A list of function declarations with imported bodies that we may
1097 std::vector
<Named_object
*> imported_inlinable_functions_
;
1098 // A list of functions that we want to inline. These will be sent
1100 std::vector
<Named_object
*> imported_inline_functions_
;
1103 // A block of statements.
1108 Block(Block
* enclosing
, Location
);
1110 // Return the enclosing block.
1113 { return this->enclosing_
; }
1115 // Return the bindings of the block.
1118 { return this->bindings_
; }
1122 { return this->bindings_
; }
1124 // Look at the block's statements.
1125 const std::vector
<Statement
*>*
1127 { return &this->statements_
; }
1129 // Return the start location. This is normally the location of the
1130 // left curly brace which starts the block.
1132 start_location() const
1133 { return this->start_location_
; }
1135 // Return the end location. This is normally the location of the
1136 // right curly brace which ends the block.
1138 end_location() const
1139 { return this->end_location_
; }
1141 // Add a statement to the block.
1143 add_statement(Statement
*);
1145 // Add a statement to the front of the block.
1147 add_statement_at_front(Statement
*);
1149 // Replace a statement in a block.
1151 replace_statement(size_t index
, Statement
*);
1153 // Add a Statement before statement number INDEX.
1155 insert_statement_before(size_t index
, Statement
*);
1157 // Add a Statement after statement number INDEX.
1159 insert_statement_after(size_t index
, Statement
*);
1161 // Set the end location of the block.
1163 set_end_location(Location location
)
1164 { this->end_location_
= location
; }
1166 // Traverse the tree.
1168 traverse(Traverse
*);
1170 // Set final types for unspecified variables and constants.
1174 // Return true if execution of this block may fall through to the
1177 may_fall_through() const;
1179 // Write the export data for the block's statements to the string.
1181 export_block(Export_function_body
*);
1183 // Turn exported block data into a block.
1185 import_block(Block
*, Import_function_body
*, Location
);
1187 // Convert the block to the backend representation.
1189 get_backend(Translate_context
*);
1191 // Iterate over statements.
1193 typedef std::vector
<Statement
*>::iterator iterator
;
1197 { return this->statements_
.begin(); }
1201 { return this->statements_
.end(); }
1206 // Statements in the block.
1207 std::vector
<Statement
*> statements_
;
1209 Bindings
* bindings_
;
1210 // Location of start of block.
1211 Location start_location_
;
1212 // Location of end of block.
1213 Location end_location_
;
1221 Function(Function_type
* type
, Named_object
*, Block
*, Location
);
1223 // Return the function's type.
1226 { return this->type_
; }
1228 // Return the enclosing function if there is one.
1231 { return this->enclosing_
; }
1233 // Set the enclosing function. This is used when building thunks
1234 // for functions which call recover.
1236 set_enclosing(Named_object
* enclosing
)
1238 go_assert(this->enclosing_
== NULL
);
1239 this->enclosing_
= enclosing
;
1242 // The result variables.
1243 typedef std::vector
<Named_object
*> Results
;
1245 // Create the result variables in the outer block.
1247 create_result_variables(Gogo
*);
1249 // Update the named result variables when cloning a function which
1252 update_result_variables();
1254 // Return the result variables.
1257 { return this->results_
; }
1261 { return this->is_sink_
; }
1265 { this->is_sink_
= true; }
1267 // Whether the result variables have names.
1269 results_are_named() const
1270 { return this->results_are_named_
; }
1272 // Return the assembler name.
1275 { return this->asm_name_
; }
1277 // Set the assembler name.
1279 set_asm_name(const std::string
& asm_name
)
1280 { this->asm_name_
= asm_name
; }
1282 // Return the pragmas for this function.
1285 { return this->pragmas_
; }
1287 // Set the pragmas for this function.
1289 set_pragmas(unsigned int pragmas
)
1291 this->pragmas_
= pragmas
;
1294 // Return the index to use for a nested function.
1296 next_nested_function_index()
1298 ++this->nested_functions_
;
1299 return this->nested_functions_
;
1302 // Whether this method should not be included in the type
1305 nointerface() const;
1307 // Record that this method should not be included in the type
1312 // Record that this function is a stub method created for an unnamed
1315 set_is_unnamed_type_stub_method()
1317 go_assert(this->is_method());
1318 this->is_unnamed_type_stub_method_
= true;
1321 // Return the amount of enclosed variables in this closure.
1323 closure_field_count() const
1324 { return this->closure_fields_
.size(); }
1326 // Add a new field to the closure variable.
1328 add_closure_field(Named_object
* var
, Location loc
)
1329 { this->closure_fields_
.push_back(std::make_pair(var
, loc
)); }
1331 // Whether this function needs a closure.
1333 needs_closure() const
1334 { return !this->closure_fields_
.empty(); }
1336 // Return the closure variable, creating it if necessary. This is
1337 // passed to the function as a static chain parameter.
1341 // Set the closure variable. This is used when building thunks for
1342 // functions which call recover.
1344 set_closure_var(Named_object
* v
)
1346 go_assert(this->closure_var_
== NULL
);
1347 this->closure_var_
= v
;
1350 // Return the variable for a reference to field INDEX in the closure
1353 enclosing_var(unsigned int index
)
1355 go_assert(index
< this->closure_fields_
.size());
1356 return closure_fields_
[index
].first
;
1359 // Set the type of the closure variable if there is one.
1363 // Get the block of statements associated with the function.
1366 { return this->block_
; }
1368 // Get the location of the start of the function.
1371 { return this->location_
; }
1373 // Return whether this function is actually a method.
1377 // Add a label definition to the function.
1379 add_label_definition(Gogo
*, const std::string
& label_name
, Location
);
1381 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1382 // if we should report errors for a goto from the current location
1383 // to the label location.
1385 add_label_reference(Gogo
*, const std::string
& label_name
,
1386 Location
, bool issue_goto_errors
);
1388 // Warn about labels that are defined but not used.
1390 check_labels() const;
1392 // Note that a new local type has been added. Return its index.
1394 new_local_type_index()
1395 { return this->local_type_count_
++; }
1397 // Whether this function calls the predeclared recover function.
1399 calls_recover() const
1400 { return this->calls_recover_
; }
1402 // Record that this function calls the predeclared recover function.
1403 // This is set during the lowering pass.
1406 { this->calls_recover_
= true; }
1408 // Whether this is a recover thunk function.
1410 is_recover_thunk() const
1411 { return this->is_recover_thunk_
; }
1413 // Record that this is a thunk built for a function which calls
1416 set_is_recover_thunk()
1417 { this->is_recover_thunk_
= true; }
1419 // Whether this function already has a recover thunk.
1421 has_recover_thunk() const
1422 { return this->has_recover_thunk_
; }
1424 // Record that this function already has a recover thunk.
1426 set_has_recover_thunk()
1427 { this->has_recover_thunk_
= true; }
1429 // Record that this function is a thunk created for a defer
1430 // statement that calls the __go_set_defer_retaddr runtime function.
1432 set_calls_defer_retaddr()
1433 { this->calls_defer_retaddr_
= true; }
1435 // Whether this is a type hash or equality function created by the
1438 is_type_specific_function()
1439 { return this->is_type_specific_function_
; }
1441 // Record that this function is a type hash or equality function
1442 // created by the compiler.
1444 set_is_type_specific_function()
1445 { this->is_type_specific_function_
= true; }
1447 // Mark the function as going into a unique section.
1449 set_in_unique_section()
1450 { this->in_unique_section_
= true; }
1452 // Return whether this function should be exported for inlining.
1454 export_for_inlining() const
1455 { return this->export_for_inlining_
; }
1457 // Mark the function to be exported for inlining.
1459 set_export_for_inlining()
1460 { this->export_for_inlining_
= true; }
1462 // Return whether this function is inline only.
1464 is_inline_only() const
1465 { return this->is_inline_only_
; }
1467 // Mark the function as inline only: the body should not be emitted
1468 // if it is not inlined.
1470 set_is_inline_only()
1471 { this->is_inline_only_
= true; }
1473 // Swap with another function. Used only for the thunk which calls
1476 swap_for_recover(Function
*);
1478 // Traverse the tree.
1480 traverse(Traverse
*);
1482 // Determine types in the function.
1486 // Return an expression for the function descriptor, given the named
1487 // object for this function. This may only be called for functions
1488 // without a closure. This will be an immutable struct with one
1489 // field that points to the function's code.
1491 descriptor(Gogo
*, Named_object
*);
1493 // Set the descriptor for this function. This is used when a
1494 // function declaration is followed by a function definition.
1496 set_descriptor(Expression
* descriptor
)
1498 go_assert(this->descriptor_
== NULL
);
1499 this->descriptor_
= descriptor
;
1502 // Return the backend representation.
1504 get_or_make_decl(Gogo
*, Named_object
*);
1506 // Return the function's decl after it has been built.
1510 // Set the function decl to hold a backend representation of the function
1513 build(Gogo
*, Named_object
*);
1515 // Get the statement that assigns values to this function's result struct.
1517 return_value(Gogo
*, Named_object
*, Location
) const;
1519 // Get an expression for the variable holding the defer stack.
1521 defer_stack(Location
);
1523 // Export the function.
1525 export_func(Export
*, const std::string
& name
) const;
1527 // Export a function with a type.
1529 export_func_with_type(Export
*, const std::string
& name
,
1530 const Function_type
*, Results
*, bool nointerface
,
1531 Block
* block
, Location
);
1533 // Import a function.
1535 import_func(Import
*, std::string
* pname
, Typed_identifier
** receiver
,
1536 Typed_identifier_list
** pparameters
,
1537 Typed_identifier_list
** presults
, bool* is_varargs
,
1538 bool* nointerface
, std::string
* body
);
1541 // Type for mapping from label names to Label objects.
1542 typedef Unordered_map(std::string
, Label
*) Labels
;
1545 build_defer_wrapper(Gogo
*, Named_object
*, Bstatement
**, Bstatement
**);
1547 typedef std::vector
<std::pair
<Named_object
*,
1548 Location
> > Closure_fields
;
1550 // The function's type.
1551 Function_type
* type_
;
1552 // The enclosing function. This is NULL when there isn't one, which
1553 // is the normal case.
1554 Named_object
* enclosing_
;
1555 // The result variables, if any.
1557 // If there is a closure, this is the list of variables which appear
1558 // in the closure. This is created by the parser, and then resolved
1559 // to a real type when we lower parse trees.
1560 Closure_fields closure_fields_
;
1561 // The closure variable, passed as a parameter using the static
1562 // chain parameter. Normally NULL.
1563 Named_object
* closure_var_
;
1564 // The outer block of statements in the function.
1566 // The source location of the start of the function.
1568 // Labels defined or referenced in the function.
1570 // The number of local types defined in this function.
1571 unsigned int local_type_count_
;
1572 // The assembler name: this is the name that will be put in the object file.
1573 // Set by the go:linkname compiler directive. This is normally empty.
1574 std::string asm_name_
;
1575 // The function descriptor, if any.
1576 Expression
* descriptor_
;
1577 // The function decl.
1579 // The defer stack variable. A pointer to this variable is used to
1580 // distinguish the defer stack for one function from another. This
1581 // is NULL unless we actually need a defer stack.
1582 Temporary_statement
* defer_stack_
;
1583 // Pragmas for this function. This is a set of GOPRAGMA bits.
1584 unsigned int pragmas_
;
1585 // Number of nested functions defined within this function.
1586 unsigned int nested_functions_
;
1587 // True if this function is sink-named. No code is generated.
1589 // True if the result variables are named.
1590 bool results_are_named_
: 1;
1591 // True if this function is a stub method created for an unnamed
1593 bool is_unnamed_type_stub_method_
: 1;
1594 // True if this function calls the predeclared recover function.
1595 bool calls_recover_
: 1;
1596 // True if this a thunk built for a function which calls recover.
1597 bool is_recover_thunk_
: 1;
1598 // True if this function already has a recover thunk.
1599 bool has_recover_thunk_
: 1;
1600 // True if this is a thunk built for a defer statement that calls
1601 // the __go_set_defer_retaddr runtime function.
1602 bool calls_defer_retaddr_
: 1;
1603 // True if this is a function built by the compiler to as a hash or
1604 // equality function for some type.
1605 bool is_type_specific_function_
: 1;
1606 // True if this function should be put in a unique section. This is
1607 // turned on for field tracking.
1608 bool in_unique_section_
: 1;
1609 // True if we should export the body of this function for
1610 // cross-package inlining.
1611 bool export_for_inlining_
: 1;
1612 // True if this function is inline only: if it should not be emitted
1613 // if it is not inlined.
1614 bool is_inline_only_
: 1;
1617 // A snapshot of the current binding state.
1619 class Bindings_snapshot
1622 Bindings_snapshot(const Block
*, Location
);
1624 // Report any errors appropriate for a goto from the current binding
1625 // state of B to this one.
1627 check_goto_from(const Block
* b
, Location
);
1629 // Report any errors appropriate for a goto from this binding state
1630 // to the current state of B.
1632 check_goto_to(const Block
* b
);
1636 check_goto_block(Location
, const Block
*, const Block
*, size_t*);
1639 check_goto_defs(Location
, const Block
*, size_t, size_t);
1641 // The current block.
1642 const Block
* block_
;
1643 // The number of names currently defined in each open block.
1644 // Element 0 is this->block_, element 1 is
1645 // this->block_->enclosing(), etc.
1646 std::vector
<size_t> counts_
;
1647 // The location where this snapshot was taken.
1651 // A function declaration.
1653 class Function_declaration
1656 Function_declaration(Function_type
* fntype
, Location location
)
1657 : fntype_(fntype
), location_(location
), asm_name_(), descriptor_(NULL
),
1658 fndecl_(NULL
), pragmas_(0), imported_body_(),
1659 is_on_inlinable_list_(false)
1664 { return this->fntype_
; }
1668 { return this->location_
; }
1670 // Return whether this function declaration is a method.
1676 { return this->asm_name_
; }
1678 // Set the assembler name.
1680 set_asm_name(const std::string
& asm_name
)
1681 { this->asm_name_
= asm_name
; }
1683 // Return the pragmas for this function.
1686 { return this->pragmas_
; }
1688 // Set the pragmas for this function.
1690 set_pragmas(unsigned int pragmas
)
1692 this->pragmas_
= pragmas
;
1695 // Whether this method should not be included in the type
1698 nointerface() const;
1700 // Record that this method should not be included in the type
1705 // Whether we have an imported function body.
1707 has_imported_body() const
1708 { return !this->imported_body_
.empty(); }
1710 // Record the imported body of this function.
1712 set_imported_body(Import
* imp
, const std::string
& imported_body
)
1715 this->imported_body_
= imported_body
;
1718 // Whether this declaration is on the list of inlinable functions.
1720 is_on_inlinable_list() const
1721 { return this->is_on_inlinable_list_
; }
1723 // Set that this function is on the list of inlinable functions.
1725 set_is_on_inlinable_list()
1726 { this->is_on_inlinable_list_
= true; }
1728 // Import the function body, creating a function.
1730 import_function_body(Gogo
*, Named_object
*);
1732 // Return an expression for the function descriptor, given the named
1733 // object for this function. This may only be called for functions
1734 // without a closure. This will be an immutable struct with one
1735 // field that points to the function's code.
1737 descriptor(Gogo
*, Named_object
*);
1739 // Return true if we have created a descriptor for this declaration.
1741 has_descriptor() const
1742 { return this->descriptor_
!= NULL
; }
1744 // Return a backend representation.
1746 get_or_make_decl(Gogo
*, Named_object
*);
1748 // If there is a descriptor, build it into the backend
1751 build_backend_descriptor(Gogo
*);
1753 // Export a function declaration.
1755 export_func(Export
* exp
, const std::string
& name
) const
1757 Function::export_func_with_type(exp
, name
, this->fntype_
, NULL
,
1758 this->is_method() && this->nointerface(),
1759 NULL
, this->location_
);
1762 // Check that the types used in this declaration's signature are defined.
1764 check_types() const;
1767 // The type of the function.
1768 Function_type
* fntype_
;
1769 // The location of the declaration.
1771 // The assembler name: this is the name to use in references to the
1772 // function. This is normally empty.
1773 std::string asm_name_
;
1774 // The function descriptor, if any.
1775 Expression
* descriptor_
;
1776 // The function decl if needed.
1778 // Pragmas for this function. This is a set of GOPRAGMA bits.
1779 unsigned int pragmas_
;
1780 // Importer for function body if imported from a different package.
1782 // Export data for function body if imported from a different package.
1783 std::string imported_body_
;
1784 // Whether this declaration is already on the list of inlinable functions.
1785 bool is_on_inlinable_list_
;
1793 Variable(Type
*, Expression
*, bool is_global
, bool is_parameter
,
1794 bool is_receiver
, Location
);
1796 // Get the type of the variable.
1803 // Return whether the type is defined yet.
1807 // Get the initial value.
1810 { return this->init_
; }
1812 // Return whether there are any preinit statements.
1814 has_pre_init() const
1815 { return this->preinit_
!= NULL
; }
1817 // Return the preinit statements if any.
1820 { return this->preinit_
; }
1822 // Return whether this is a global variable.
1825 { return this->is_global_
; }
1827 // Return whether this is a function parameter.
1829 is_parameter() const
1830 { return this->is_parameter_
; }
1832 // Return whether this is a closure (static chain) parameter.
1835 { return this->is_closure_
; }
1837 // Change this parameter to be a closure.
1841 this->is_closure_
= true;
1844 // Return whether this is the receiver parameter of a method.
1847 { return this->is_receiver_
; }
1849 // Change this parameter to be a receiver. This is used when
1850 // creating the thunks created for functions which call recover.
1854 go_assert(this->is_parameter_
);
1855 this->is_receiver_
= true;
1858 // Change this parameter to not be a receiver. This is used when
1859 // creating the thunks created for functions which call recover.
1861 set_is_not_receiver()
1863 go_assert(this->is_parameter_
);
1864 this->is_receiver_
= false;
1867 // Return whether this is the varargs parameter of a function.
1869 is_varargs_parameter() const
1870 { return this->is_varargs_parameter_
; }
1872 // Whether this variable's address is taken.
1874 is_address_taken() const
1875 { return this->is_address_taken_
; }
1877 // Whether this variable should live in the heap.
1880 { return this->is_address_taken_
&& !this->is_global_
; }
1882 // Note that something takes the address of this variable.
1885 { this->is_address_taken_
= true; }
1887 // Return whether the address is taken but does not escape.
1889 is_non_escaping_address_taken() const
1890 { return this->is_non_escaping_address_taken_
; }
1892 // Note that something takes the address of this variable such that
1893 // the address does not escape the function.
1895 set_non_escaping_address_taken()
1896 { this->is_non_escaping_address_taken_
= true; }
1898 // Get the source location of the variable's declaration.
1901 { return this->location_
; }
1903 // Record that this is the varargs parameter of a function.
1905 set_is_varargs_parameter()
1907 go_assert(this->is_parameter_
);
1908 this->is_varargs_parameter_
= true;
1911 // Return whether the variable has been used.
1914 { return this->is_used_
; }
1916 // Mark that the variable has been used.
1919 { this->is_used_
= true; }
1921 // Clear the initial value; used for error handling and write barriers.
1924 { this->init_
= NULL
; }
1926 // Set the initial value; used for converting shortcuts.
1928 set_init(Expression
* init
)
1929 { this->init_
= init
; }
1931 // Get the preinit block, a block of statements to be run before the
1932 // initialization expression.
1934 preinit_block(Gogo
*);
1936 // Add a statement to be run before the initialization expression.
1937 // This is only used for global variables.
1939 add_preinit_statement(Gogo
*, Statement
*);
1941 // Lower the initialization expression after parsing is complete.
1943 lower_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1945 // Flatten the initialization expression after ordering evaluations.
1947 flatten_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1949 // A special case: the init value is used only to determine the
1950 // type. This is used if the variable is defined using := with the
1951 // comma-ok form of a map index or a receive expression. The init
1952 // value is actually the map index expression or receive expression.
1953 // We use this because we may not know the right type at parse time.
1955 set_type_from_init_tuple()
1956 { this->type_from_init_tuple_
= true; }
1958 // Another special case: the init value is used only to determine
1959 // the type. This is used if the variable is defined using := with
1960 // a range clause. The init value is the range expression. The
1961 // type of the variable is the index type of the range expression
1962 // (i.e., the first value returned by a range).
1964 set_type_from_range_index()
1965 { this->type_from_range_index_
= true; }
1967 // Another special case: like set_type_from_range_index, but the
1968 // type is the value type of the range expression (i.e., the second
1969 // value returned by a range).
1971 set_type_from_range_value()
1972 { this->type_from_range_value_
= true; }
1974 // Another special case: the init value is used only to determine
1975 // the type. This is used if the variable is defined using := with
1976 // a case in a select statement. The init value is the channel.
1977 // The type of the variable is the channel's element type.
1979 set_type_from_chan_element()
1980 { this->type_from_chan_element_
= true; }
1982 // After we lower the select statement, we once again set the type
1983 // from the initialization expression.
1985 clear_type_from_chan_element()
1987 go_assert(this->type_from_chan_element_
);
1988 this->type_from_chan_element_
= false;
1991 // TRUE if this variable was created for a type switch clause.
1993 is_type_switch_var() const
1994 { return this->is_type_switch_var_
; }
1996 // Note that this variable was created for a type switch clause.
1998 set_is_type_switch_var()
1999 { this->is_type_switch_var_
= true; }
2001 // Mark the variable as going into a unique section.
2003 set_in_unique_section()
2005 go_assert(this->is_global_
);
2006 this->in_unique_section_
= true;
2009 // Return the top-level declaration for this variable.
2012 { return this->toplevel_decl_
; }
2014 // Set the top-level declaration for this variable. Only used for local
2017 set_toplevel_decl(Statement
* s
)
2019 go_assert(!this->is_global_
&& !this->is_parameter_
&& !this->is_receiver_
);
2020 this->toplevel_decl_
= s
;
2023 // Traverse the initializer expression.
2025 traverse_expression(Traverse
*, unsigned int traverse_mask
);
2027 // Determine the type of the variable if necessary.
2031 // Get the backend representation of the variable.
2033 get_backend_variable(Gogo
*, Named_object
*, const Package
*,
2034 const std::string
&);
2036 // Get the initial value of the variable. This may only
2037 // be called if has_pre_init() returns false.
2039 get_init(Gogo
*, Named_object
* function
);
2041 // Return a series of statements which sets the value of the
2042 // variable in DECL. This should only be called is has_pre_init()
2043 // returns true. DECL may be NULL for a sink variable.
2045 get_init_block(Gogo
*, Named_object
* function
, Bvariable
* decl
);
2047 // Export the variable.
2049 export_var(Export
*, const std::string
& name
) const;
2051 // Import a variable.
2053 import_var(Import
*, std::string
* pname
, Type
** ptype
);
2056 // The type of a tuple.
2058 type_from_tuple(Expression
*, bool) const;
2060 // The type of a range.
2062 type_from_range(Expression
*, bool, bool) const;
2064 // The element type of a channel.
2066 type_from_chan_element(Expression
*, bool) const;
2068 // The variable's type. This may be NULL if the type is set from
2071 // The initial value. This may be NULL if the variable should be
2072 // initialized to the default value for the type.
2074 // Statements to run before the init statement.
2076 // Location of variable definition.
2078 // Backend representation.
2079 Bvariable
* backend_
;
2080 // Whether this is a global variable.
2081 bool is_global_
: 1;
2082 // Whether this is a function parameter.
2083 bool is_parameter_
: 1;
2084 // Whether this is a closure parameter.
2085 bool is_closure_
: 1;
2086 // Whether this is the receiver parameter of a method.
2087 bool is_receiver_
: 1;
2088 // Whether this is the varargs parameter of a function.
2089 bool is_varargs_parameter_
: 1;
2090 // Whether this variable is ever referenced.
2092 // Whether something takes the address of this variable. For a
2093 // local variable this implies that the variable has to be on the
2094 // heap if it escapes from its function.
2095 bool is_address_taken_
: 1;
2096 // Whether something takes the address of this variable such that
2097 // the address does not escape the function.
2098 bool is_non_escaping_address_taken_
: 1;
2099 // True if we have seen this variable in a traversal.
2101 // True if we have lowered the initialization expression.
2102 bool init_is_lowered_
: 1;
2103 // True if we have flattened the initialization expression.
2104 bool init_is_flattened_
: 1;
2105 // True if init is a tuple used to set the type.
2106 bool type_from_init_tuple_
: 1;
2107 // True if init is a range clause and the type is the index type.
2108 bool type_from_range_index_
: 1;
2109 // True if init is a range clause and the type is the value type.
2110 bool type_from_range_value_
: 1;
2111 // True if init is a channel and the type is the channel's element type.
2112 bool type_from_chan_element_
: 1;
2113 // True if this is a variable created for a type switch case.
2114 bool is_type_switch_var_
: 1;
2115 // True if we have determined types.
2116 bool determined_type_
: 1;
2117 // True if this variable should be put in a unique section. This is
2118 // used for field tracking.
2119 bool in_unique_section_
: 1;
2120 // The top-level declaration for this variable. Only used for local
2121 // variables. Must be a Temporary_statement if not NULL.
2122 Statement
* toplevel_decl_
;
2125 // A variable which is really the name for a function return value, or
2128 class Result_variable
2131 Result_variable(Type
* type
, Function
* function
, int index
,
2133 : type_(type
), function_(function
), index_(index
), location_(location
),
2134 backend_(NULL
), is_address_taken_(false),
2135 is_non_escaping_address_taken_(false)
2138 // Get the type of the result variable.
2141 { return this->type_
; }
2143 // Get the function that this is associated with.
2146 { return this->function_
; }
2148 // Index in the list of function results.
2151 { return this->index_
; }
2153 // The location of the variable definition.
2156 { return this->location_
; }
2158 // Whether this variable's address is taken.
2160 is_address_taken() const
2161 { return this->is_address_taken_
; }
2163 // Note that something takes the address of this variable.
2166 { this->is_address_taken_
= true; }
2168 // Return whether the address is taken but does not escape.
2170 is_non_escaping_address_taken() const
2171 { return this->is_non_escaping_address_taken_
; }
2173 // Note that something takes the address of this variable such that
2174 // the address does not escape the function.
2176 set_non_escaping_address_taken()
2177 { this->is_non_escaping_address_taken_
= true; }
2179 // Whether this variable should live in the heap.
2182 { return this->is_address_taken_
; }
2184 // Set the function. This is used when cloning functions which call
2187 set_function(Function
* function
)
2188 { this->function_
= function
; }
2190 // Get the backend representation of the variable.
2192 get_backend_variable(Gogo
*, Named_object
*, const std::string
&);
2195 // Type of result variable.
2197 // Function with which this is associated.
2198 Function
* function_
;
2199 // Index in list of results.
2201 // Where the result variable is defined.
2203 // Backend representation.
2204 Bvariable
* backend_
;
2205 // Whether something takes the address of this variable.
2206 bool is_address_taken_
;
2207 // Whether something takes the address of this variable such that
2208 // the address does not escape the function.
2209 bool is_non_escaping_address_taken_
;
2212 // The value we keep for a named constant. This lets us hold a type
2213 // and an expression.
2215 class Named_constant
2218 Named_constant(Type
* type
, Expression
* expr
, int iota_value
,
2220 : type_(type
), expr_(expr
), iota_value_(iota_value
), location_(location
),
2221 lowering_(false), is_sink_(false), bconst_(NULL
)
2226 { return this->type_
; }
2233 { return this->expr_
; }
2237 { return this->iota_value_
; }
2241 { return this->location_
; }
2243 // Whether we are lowering.
2246 { return this->lowering_
; }
2248 // Set that we are lowering.
2251 { this->lowering_
= true; }
2253 // We are no longer lowering.
2256 { this->lowering_
= false; }
2260 { return this->is_sink_
; }
2264 { this->is_sink_
= true; }
2266 // Traverse the expression.
2268 traverse_expression(Traverse
*);
2270 // Determine the type of the constant if necessary.
2274 // Indicate that we found and reported an error for this constant.
2278 // Export the constant.
2280 export_const(Export
*, const std::string
& name
) const;
2282 // Import a constant.
2284 import_const(Import
*, std::string
*, Type
**, Expression
**);
2286 // Get the backend representation of the constant value.
2288 get_backend(Gogo
*, Named_object
*);
2291 // The type of the constant.
2293 // The expression for the constant.
2295 // If the predeclared constant iota is used in EXPR_, this is the
2296 // value it will have. We do this because at parse time we don't
2297 // know whether the name "iota" will refer to the predeclared
2298 // constant or to something else. We put in the right value in when
2301 // The location of the definition.
2303 // Whether we are currently lowering this constant.
2305 // Whether this constant is blank named and needs only type checking.
2307 // The backend representation of the constant value.
2308 Bexpression
* bconst_
;
2311 // A type declaration.
2313 class Type_declaration
2316 Type_declaration(Location location
)
2317 : location_(location
), in_function_(NULL
), in_function_index_(0),
2318 methods_(), issued_warning_(false)
2321 // Return the location.
2324 { return this->location_
; }
2326 // Return the function in which this type is declared. This will
2327 // return NULL for a type declared in global scope.
2329 in_function(unsigned int* pindex
)
2331 *pindex
= this->in_function_index_
;
2332 return this->in_function_
;
2335 // Set the function in which this type is declared.
2337 set_in_function(Named_object
* f
, unsigned int index
)
2339 this->in_function_
= f
;
2340 this->in_function_index_
= index
;
2343 // Add a method to this type. This is used when methods are defined
2346 add_method(const std::string
& name
, Function
* function
);
2348 // Add a method declaration to this type.
2350 add_method_declaration(const std::string
& name
, Package
*,
2351 Function_type
* type
, Location location
);
2353 // Add an already created object as a method.
2355 add_existing_method(Named_object
* no
)
2356 { this->methods_
.push_back(no
); }
2358 // Return whether any methods were defined.
2360 has_methods() const;
2362 // Return the methods.
2363 const std::vector
<Named_object
*>*
2365 { return &this->methods_
; }
2367 // Define methods when the real type is known.
2369 define_methods(Named_type
*);
2371 // This is called if we are trying to use this type. It returns
2372 // true if we should issue a warning.
2377 // The location of the type declaration.
2379 // If this type is declared in a function, a pointer back to the
2380 // function in which it is defined.
2381 Named_object
* in_function_
;
2382 // The index of this type in IN_FUNCTION_.
2383 unsigned int in_function_index_
;
2384 // Methods defined before the type is defined.
2385 std::vector
<Named_object
*> methods_
;
2386 // True if we have issued a warning about a use of this type
2387 // declaration when it is undefined.
2388 bool issued_warning_
;
2391 // An unknown object. These are created by the parser for forward
2392 // references to names which have not been seen before. In a correct
2393 // program, these will always point to a real definition by the end of
2394 // the parse. Because they point to another Named_object, these may
2395 // only be referenced by Unknown_expression objects.
2400 Unknown_name(Location location
)
2401 : location_(location
), real_named_object_(NULL
)
2404 // Return the location where this name was first seen.
2407 { return this->location_
; }
2409 // Return the real named object that this points to, or NULL if it
2410 // was never resolved.
2412 real_named_object() const
2413 { return this->real_named_object_
; }
2415 // Set the real named object that this points to.
2417 set_real_named_object(Named_object
* no
);
2420 // The location where this name was first seen.
2422 // The real named object when it is known.
2427 // A named object named. This is the result of a declaration. We
2428 // don't use a superclass because they all have to be handled
2436 // An uninitialized Named_object. We should never see this.
2437 NAMED_OBJECT_UNINITIALIZED
,
2438 // An erroneous name. This indicates a parse error, to avoid
2439 // later errors about undefined references.
2440 NAMED_OBJECT_ERRONEOUS
,
2441 // An unknown name. This is used for forward references. In a
2442 // correct program, these will all be resolved by the end of the
2444 NAMED_OBJECT_UNKNOWN
,
2449 // A forward type declaration.
2450 NAMED_OBJECT_TYPE_DECLARATION
,
2453 // A result variable in a function.
2454 NAMED_OBJECT_RESULT_VAR
,
2455 // The blank identifier--the special variable named _.
2459 // A forward func declaration.
2460 NAMED_OBJECT_FUNC_DECLARATION
,
2462 NAMED_OBJECT_PACKAGE
2465 // Return the classification.
2467 classification() const
2468 { return this->classification_
; }
2473 is_erroneous() const
2474 { return this->classification_
== NAMED_OBJECT_ERRONEOUS
; }
2478 { return this->classification_
== NAMED_OBJECT_UNKNOWN
; }
2482 { return this->classification_
== NAMED_OBJECT_CONST
; }
2486 { return this->classification_
== NAMED_OBJECT_TYPE
; }
2489 is_type_declaration() const
2490 { return this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
; }
2494 { return this->classification_
== NAMED_OBJECT_VAR
; }
2497 is_result_variable() const
2498 { return this->classification_
== NAMED_OBJECT_RESULT_VAR
; }
2502 { return this->classification_
== NAMED_OBJECT_SINK
; }
2506 { return this->classification_
== NAMED_OBJECT_FUNC
; }
2509 is_function_declaration() const
2510 { return this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
; }
2514 { return this->classification_
== NAMED_OBJECT_PACKAGE
; }
2518 static Named_object
*
2519 make_erroneous_name(const std::string
& name
)
2520 { return new Named_object(name
, NULL
, NAMED_OBJECT_ERRONEOUS
); }
2522 static Named_object
*
2523 make_unknown_name(const std::string
& name
, Location
);
2525 static Named_object
*
2526 make_constant(const Typed_identifier
&, const Package
*, Expression
*,
2529 static Named_object
*
2530 make_type(const std::string
&, const Package
*, Type
*, Location
);
2532 static Named_object
*
2533 make_type_declaration(const std::string
&, const Package
*, Location
);
2535 static Named_object
*
2536 make_variable(const std::string
&, const Package
*, Variable
*);
2538 static Named_object
*
2539 make_result_variable(const std::string
&, Result_variable
*);
2541 static Named_object
*
2544 static Named_object
*
2545 make_function(const std::string
&, const Package
*, Function
*);
2547 static Named_object
*
2548 make_function_declaration(const std::string
&, const Package
*, Function_type
*,
2551 static Named_object
*
2552 make_package(const std::string
& alias
, Package
* package
);
2559 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2560 return this->u_
.unknown_value
;
2564 unknown_value() const
2566 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2567 return this->u_
.unknown_value
;
2573 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2574 return this->u_
.const_value
;
2577 const Named_constant
*
2580 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2581 return this->u_
.const_value
;
2587 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2588 return this->u_
.type_value
;
2594 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2595 return this->u_
.type_value
;
2599 type_declaration_value()
2601 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2602 return this->u_
.type_declaration
;
2605 const Type_declaration
*
2606 type_declaration_value() const
2608 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2609 return this->u_
.type_declaration
;
2615 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2616 return this->u_
.var_value
;
2622 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2623 return this->u_
.var_value
;
2629 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2630 return this->u_
.result_var_value
;
2633 const Result_variable
*
2634 result_var_value() const
2636 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2637 return this->u_
.result_var_value
;
2643 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2644 return this->u_
.func_value
;
2650 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2651 return this->u_
.func_value
;
2654 Function_declaration
*
2655 func_declaration_value()
2657 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2658 return this->u_
.func_declaration_value
;
2661 const Function_declaration
*
2662 func_declaration_value() const
2664 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2665 return this->u_
.func_declaration_value
;
2671 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2672 return this->u_
.package_value
;
2676 package_value() const
2678 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2679 return this->u_
.package_value
;
2684 { return this->name_
; }
2686 // Return the name to use in an error message. The difference is
2687 // that if this Named_object is defined in a different package, this
2688 // will return PACKAGE.NAME.
2690 message_name() const;
2694 { return this->package_
; }
2696 // Resolve an unknown value if possible. This returns the same
2697 // Named_object or a new one.
2701 Named_object
* ret
= this;
2702 if (this->is_unknown())
2704 Named_object
* r
= this->unknown_value()->real_named_object();
2714 const Named_object
* ret
= this;
2715 if (this->is_unknown())
2717 const Named_object
* r
= this->unknown_value()->real_named_object();
2724 // The location where this object was defined or referenced.
2728 // Convert a variable to the backend representation.
2730 get_backend_variable(Gogo
*, Named_object
* function
);
2732 // Return the external identifier for this object.
2736 // Get the backend representation of this object.
2738 get_backend(Gogo
*, std::vector
<Bexpression
*>&, std::vector
<Btype
*>&,
2739 std::vector
<Bfunction
*>&);
2741 // Define a type declaration.
2743 set_type_value(Named_type
*);
2745 // Define a function declaration.
2747 set_function_value(Function
*);
2749 // Declare an unknown name as a type declaration.
2753 // Export this object.
2755 export_named_object(Export
*) const;
2757 // Mark this named object as an invalid redefinition of another object.
2759 set_is_redefinition()
2760 { this->is_redefinition_
= true; }
2762 // Return whether or not this object is a invalid redefinition of another
2765 is_redefinition() const
2766 { return this->is_redefinition_
; }
2769 Named_object(const std::string
&, const Package
*, Classification
);
2771 // The name of the object.
2773 // The package that this object is in. This is NULL if it is in the
2774 // file we are compiling.
2775 const Package
* package_
;
2776 // The type of object this is.
2777 Classification classification_
;
2781 Unknown_name
* unknown_value
;
2782 Named_constant
* const_value
;
2783 Named_type
* type_value
;
2784 Type_declaration
* type_declaration
;
2785 Variable
* var_value
;
2786 Result_variable
* result_var_value
;
2787 Function
* func_value
;
2788 Function_declaration
* func_declaration_value
;
2789 Package
* package_value
;
2791 // True if this object is an invalid redefinition of another object.
2792 bool is_redefinition_
;
2795 // A binding contour. This binds names to objects.
2800 // Type for mapping from names to objects.
2801 typedef Unordered_map(std::string
, Named_object
*) Contour
;
2803 Bindings(Bindings
* enclosing
);
2805 // Add an erroneous name.
2807 add_erroneous_name(const std::string
& name
)
2808 { return this->add_named_object(Named_object::make_erroneous_name(name
)); }
2810 // Add an unknown name.
2812 add_unknown_name(const std::string
& name
, Location location
)
2814 return this->add_named_object(Named_object::make_unknown_name(name
,
2820 add_constant(const Typed_identifier
& tid
, const Package
* package
,
2821 Expression
* expr
, int iota_value
)
2823 return this->add_named_object(Named_object::make_constant(tid
, package
,
2830 add_type(const std::string
& name
, const Package
* package
, Type
* type
,
2833 return this->add_named_object(Named_object::make_type(name
, package
, type
,
2837 // Add a named type. This is used for builtin types, and to add an
2838 // imported type to the global scope.
2840 add_named_type(Named_type
* named_type
);
2842 // Add a type declaration.
2844 add_type_declaration(const std::string
& name
, const Package
* package
,
2847 Named_object
* no
= Named_object::make_type_declaration(name
, package
,
2849 return this->add_named_object(no
);
2854 add_variable(const std::string
& name
, const Package
* package
,
2857 return this->add_named_object(Named_object::make_variable(name
, package
,
2861 // Add a result variable.
2863 add_result_variable(const std::string
& name
, Result_variable
* result
)
2865 return this->add_named_object(Named_object::make_result_variable(name
,
2871 add_function(const std::string
& name
, const Package
*, Function
* function
);
2873 // Add a function declaration.
2875 add_function_declaration(const std::string
& name
, const Package
* package
,
2876 Function_type
* type
, Location location
);
2878 // Add a package. The location is the location of the import
2881 add_package(const std::string
& alias
, Package
* package
)
2883 Named_object
* no
= Named_object::make_package(alias
, package
);
2884 return this->add_named_object(no
);
2887 // Define a type which was already declared.
2889 define_type(Named_object
*, Named_type
*);
2891 // Add a method to the list of objects. This is not added to the
2894 add_method(Named_object
*);
2896 // Add a named object to this binding.
2898 add_named_object(Named_object
* no
)
2899 { return this->add_named_object_to_contour(&this->bindings_
, no
); }
2901 // Clear all names in file scope from the bindings.
2903 clear_file_scope(Gogo
*);
2905 // Look up a name in this binding contour and in any enclosing
2906 // binding contours. This returns NULL if the name is not found.
2908 lookup(const std::string
&) const;
2910 // Look up a name in this binding contour without looking in any
2911 // enclosing binding contours. Returns NULL if the name is not found.
2913 lookup_local(const std::string
&) const;
2917 remove_binding(Named_object
*);
2919 // Mark all variables as used. This is used for some types of parse
2924 // Traverse the tree. See the Traverse class.
2926 traverse(Traverse
*, bool is_global
);
2928 // Iterate over definitions. This does not include things which
2929 // were only declared.
2931 typedef std::vector
<Named_object
*>::const_iterator
2932 const_definitions_iterator
;
2934 const_definitions_iterator
2935 begin_definitions() const
2936 { return this->named_objects_
.begin(); }
2938 const_definitions_iterator
2939 end_definitions() const
2940 { return this->named_objects_
.end(); }
2942 // Return the number of definitions.
2944 size_definitions() const
2945 { return this->named_objects_
.size(); }
2947 // Return whether there are no definitions.
2949 empty_definitions() const
2950 { return this->named_objects_
.empty(); }
2952 // Iterate over declarations. This is everything that has been
2953 // declared, which includes everything which has been defined.
2955 typedef Contour::const_iterator const_declarations_iterator
;
2957 const_declarations_iterator
2958 begin_declarations() const
2959 { return this->bindings_
.begin(); }
2961 const_declarations_iterator
2962 end_declarations() const
2963 { return this->bindings_
.end(); }
2965 // Return the number of declarations.
2967 size_declarations() const
2968 { return this->bindings_
.size(); }
2970 // Return whether there are no declarations.
2972 empty_declarations() const
2973 { return this->bindings_
.empty(); }
2975 // Return the first declaration.
2978 { return this->bindings_
.empty() ? NULL
: this->bindings_
.begin()->second
; }
2982 add_named_object_to_contour(Contour
*, Named_object
*);
2985 new_definition(Named_object
*, Named_object
*);
2987 // Enclosing bindings.
2988 Bindings
* enclosing_
;
2989 // The list of objects.
2990 std::vector
<Named_object
*> named_objects_
;
2991 // The mapping from names to objects.
3000 Label(const std::string
& name
)
3001 : name_(name
), location_(Linemap::unknown_location()), snapshot_(NULL
),
3002 refs_(), is_used_(false), blabel_(NULL
), depth_(DEPTH_UNKNOWN
)
3005 // Return the label's name.
3008 { return this->name_
; }
3010 // Return whether the label has been defined.
3013 { return !Linemap::is_unknown_location(this->location_
); }
3015 // Return whether the label has been used.
3018 { return this->is_used_
; }
3020 // Record that the label is used.
3023 { this->is_used_
= true; }
3025 // Return whether this label is looping.
3028 { return this->depth_
== DEPTH_LOOPING
; }
3030 // Set this label as looping.
3033 { this->depth_
= DEPTH_LOOPING
; }
3035 // Return whether this label is nonlooping.
3038 { return this->depth_
== DEPTH_NONLOOPING
; }
3040 // Set this label as nonlooping.
3043 { this->depth_
= DEPTH_NONLOOPING
; }
3045 // Return the location of the definition.
3048 { return this->location_
; }
3050 // Return the bindings snapshot.
3053 { return this->snapshot_
; }
3055 // Add a snapshot of a goto which refers to this label.
3057 add_snapshot_ref(Bindings_snapshot
* snapshot
)
3059 go_assert(Linemap::is_unknown_location(this->location_
));
3060 this->refs_
.push_back(snapshot
);
3063 // Return the list of snapshots of goto statements which refer to
3065 const std::vector
<Bindings_snapshot
*>&
3067 { return this->refs_
; }
3069 // Clear the references.
3073 // Define the label at LOCATION with the given bindings snapshot.
3075 define(Location location
, Bindings_snapshot
* snapshot
)
3077 if (this->is_dummy_label())
3079 go_assert(Linemap::is_unknown_location(this->location_
)
3080 && this->snapshot_
== NULL
);
3081 this->location_
= location
;
3082 this->snapshot_
= snapshot
;
3085 // Return the backend representation for this label.
3087 get_backend_label(Translate_context
*);
3089 // Return an expression for the address of this label. This is used
3090 // to get the return address of a deferred function to see whether
3091 // the function may call recover.
3093 get_addr(Translate_context
*, Location location
);
3095 // Return a dummy label, representing any instance of the blank label.
3097 create_dummy_label();
3099 // Return TRUE if this is a dummy label.
3101 is_dummy_label() const
3102 { return this->name_
== "_"; }
3104 // A classification of a label's looping depth.
3108 // A label never jumped to.
3110 // A label jumped to.
3115 // The name of the label.
3117 // The location of the definition. This is 0 if the label has not
3118 // yet been defined.
3120 // A snapshot of the set of bindings defined at this label, used to
3121 // issue errors about invalid goto statements.
3122 Bindings_snapshot
* snapshot_
;
3123 // A list of snapshots of goto statements which refer to this label.
3124 std::vector
<Bindings_snapshot
*> refs_
;
3125 // Whether the label has been used.
3127 // The backend representation.
3129 // The looping depth of this label, for escape analysis.
3133 // An unnamed label. These are used when lowering loops.
3138 Unnamed_label(Location location
)
3139 : location_(location
), derived_from_(NULL
), blabel_(NULL
)
3142 // Get the location where the label is defined.
3145 { return this->location_
; }
3147 // Set the location where the label is defined.
3149 set_location(Location location
)
3150 { this->location_
= location
; }
3152 // Get the top level statement this unnamed label is derived from.
3154 derived_from() const
3155 { return this->derived_from_
; }
3157 // Set the top level statement this unnamed label is derived from.
3159 set_derived_from(Statement
* s
)
3160 { this->derived_from_
= s
; }
3162 // Return a statement which defines this label.
3164 get_definition(Translate_context
*);
3166 // Return a goto to this label from LOCATION.
3168 get_goto(Translate_context
*, Location location
);
3171 // Return the backend representation.
3173 get_blabel(Translate_context
*);
3175 // The location where the label is defined.
3177 // The top-level statement this unnamed label was derived/lowered from.
3178 // This is NULL is this label is not the top-level of a lowered statement.
3179 Statement
* derived_from_
;
3180 // The backend representation of this label.
3184 // An alias for an imported package.
3189 Package_alias(Location location
)
3190 : location_(location
), used_(0)
3193 // The location of the import statement.
3196 { return this->location_
; }
3198 // How many symbols from the package were used under this alias.
3201 { return this->used_
; }
3203 // Note that some symbol was used under this alias.
3209 // The location of the import statement.
3211 // The amount of times some name from this package was used under this alias.
3215 // An imported package.
3220 Package(const std::string
& pkgpath
, const std::string
& pkgpath_symbol
,
3223 // Get the package path used for all symbols exported from this
3227 { return this->pkgpath_
; }
3229 // Return the package path to use for a symbol name.
3231 pkgpath_symbol() const;
3233 // Set the package path symbol.
3235 set_pkgpath_symbol(const std::string
&);
3237 // Return the location of the most recent import statement.
3240 { return this->location_
; }
3242 // Return whether we know the name of this package yet.
3244 has_package_name() const
3245 { return !this->package_name_
.empty(); }
3247 // The name that this package uses in its package clause. This may
3248 // be different from the name in the associated Named_object if the
3249 // import statement used an alias.
3251 package_name() const
3253 go_assert(!this->package_name_
.empty());
3254 return this->package_name_
;
3257 // Return the bindings.
3260 { return this->bindings_
; }
3262 // Type used to map import names to package aliases.
3263 typedef std::map
<std::string
, Package_alias
*> Aliases
;
3265 // Return the set of package aliases.
3268 { return this->aliases_
; }
3270 // Note that some symbol from this package was used and qualified by ALIAS.
3271 // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3273 note_usage(const std::string
& alias
) const;
3275 // Note that USAGE might be a fake usage of this package.
3277 note_fake_usage(Expression
* usage
) const
3278 { this->fake_uses_
.insert(usage
); }
3280 // Forget a given USAGE of this package.
3282 forget_usage(Expression
* usage
) const;
3284 // Clear the used field for the next file.
3288 // Look up a name in the package. Returns NULL if the name is not
3291 lookup(const std::string
& name
) const
3292 { return this->bindings_
->lookup(name
); }
3294 // Set the name of the package.
3296 set_package_name(const std::string
& name
, Location
);
3298 // Set the location of the package. This is used to record the most
3299 // recent import location.
3301 set_location(Location location
)
3302 { this->location_
= location
; }
3304 // Add a package name as an ALIAS for this package.
3306 add_alias(const std::string
& alias
, Location
);
3308 // Add a constant to the package.
3310 add_constant(const Typed_identifier
& tid
, Expression
* expr
)
3311 { return this->bindings_
->add_constant(tid
, this, expr
, 0); }
3313 // Add a type to the package.
3315 add_type(const std::string
& name
, Type
* type
, Location location
)
3316 { return this->bindings_
->add_type(name
, this, type
, location
); }
3318 // Add a type declaration to the package.
3320 add_type_declaration(const std::string
& name
, Location location
)
3321 { return this->bindings_
->add_type_declaration(name
, this, location
); }
3323 // Add a variable to the package.
3325 add_variable(const std::string
& name
, Variable
* variable
)
3326 { return this->bindings_
->add_variable(name
, this, variable
); }
3328 // Add a function declaration to the package.
3330 add_function_declaration(const std::string
& name
, Function_type
* type
,
3332 { return this->bindings_
->add_function_declaration(name
, this, type
, loc
); }
3334 // Determine types of constants.
3339 // The package path for type reflection data.
3340 std::string pkgpath_
;
3341 // The package path for symbol names.
3342 std::string pkgpath_symbol_
;
3343 // The name that this package uses in the package clause. This may
3344 // be the empty string if it is not yet known.
3345 std::string package_name_
;
3346 // The names in this package.
3347 Bindings
* bindings_
;
3348 // The location of the most recent import statement.
3350 // The set of aliases associated with this package.
3352 // A set of possibly fake uses of this package. This is mutable because we
3353 // can track fake uses of a package even if we have a const pointer to it.
3354 mutable std::set
<Expression
*> fake_uses_
;
3357 // Return codes for the traversal functions. This is not an enum
3358 // because we want to be able to declare traversal functions in other
3359 // header files without including this one.
3361 // Continue traversal as usual.
3362 const int TRAVERSE_CONTINUE
= -1;
3365 const int TRAVERSE_EXIT
= 0;
3367 // Continue traversal, but skip components of the current object.
3368 // E.g., if this is returned by Traverse::statement, we do not
3369 // traverse the expressions in the statement even if
3370 // traverse_expressions is set in the traverse_mask.
3371 const int TRAVERSE_SKIP_COMPONENTS
= 1;
3373 // This class is used when traversing the parse tree. The caller uses
3374 // a subclass which overrides functions as desired.
3379 // These bitmasks say what to traverse.
3380 static const unsigned int traverse_variables
= 0x1;
3381 static const unsigned int traverse_constants
= 0x2;
3382 static const unsigned int traverse_functions
= 0x4;
3383 static const unsigned int traverse_blocks
= 0x8;
3384 static const unsigned int traverse_statements
= 0x10;
3385 static const unsigned int traverse_expressions
= 0x20;
3386 static const unsigned int traverse_types
= 0x40;
3387 static const unsigned int traverse_func_declarations
= 0x80;
3389 Traverse(unsigned int traverse_mask
)
3390 : traverse_mask_(traverse_mask
), types_seen_(NULL
), expressions_seen_(NULL
)
3393 virtual ~Traverse();
3395 // The bitmask of what to traverse.
3397 traverse_mask() const
3398 { return this->traverse_mask_
; }
3400 // Record that we are going to traverse a type. This returns true
3401 // if the type has already been seen in this traversal. This is
3402 // required because types, unlike expressions, can form a circular
3405 remember_type(const Type
*);
3407 // Record that we are going to see an expression. This returns true
3408 // if the expression has already been seen in this traversal. This
3409 // is only needed for cases where multiple expressions can point to
3412 remember_expression(const Expression
*);
3414 // These functions return one of the TRAVERSE codes defined above.
3416 // If traverse_variables is set in the mask, this is called for
3417 // every variable in the tree.
3419 variable(Named_object
*);
3421 // If traverse_constants is set in the mask, this is called for
3422 // every named constant in the tree. The bool parameter is true for
3423 // a global constant.
3425 constant(Named_object
*, bool);
3427 // If traverse_functions is set in the mask, this is called for
3428 // every function in the tree.
3430 function(Named_object
*);
3432 // If traverse_blocks is set in the mask, this is called for every
3433 // block in the tree.
3437 // If traverse_statements is set in the mask, this is called for
3438 // every statement in the tree.
3440 statement(Block
*, size_t* index
, Statement
*);
3442 // If traverse_expressions is set in the mask, this is called for
3443 // every expression in the tree.
3445 expression(Expression
**);
3447 // If traverse_types is set in the mask, this is called for every
3448 // type in the tree.
3452 // If traverse_func_declarations is set in the mask, this is called
3453 // for every function declarations in the tree.
3455 function_declaration(Named_object
*);
3458 // A hash table for types we have seen during this traversal. Note
3459 // that this uses the default hash functions for pointers rather
3460 // than Type_hash_identical and Type_identical. This is because for
3461 // traversal we care about seeing a specific type structure. If
3462 // there are two separate instances of identical types, we want to
3464 typedef Unordered_set(const Type
*) Types_seen
;
3466 typedef Unordered_set(const Expression
*) Expressions_seen
;
3468 // Bitmask of what sort of objects to traverse.
3469 unsigned int traverse_mask_
;
3470 // Types which have been seen in this traversal.
3471 Types_seen
* types_seen_
;
3472 // Expressions which have been seen in this traversal.
3473 Expressions_seen
* expressions_seen_
;
3476 // A class which makes it easier to insert new statements before the
3477 // current statement during a traversal.
3479 class Statement_inserter
3482 typedef Unordered_set(Statement
*) Statements
;
3484 // Empty constructor.
3485 Statement_inserter()
3486 : block_(NULL
), pindex_(NULL
), gogo_(NULL
), var_(NULL
),
3487 statements_added_(NULL
)
3490 // Constructor for a statement in a block.
3491 Statement_inserter(Block
* block
, size_t *pindex
, Statements
*added
= NULL
)
3492 : block_(block
), pindex_(pindex
), gogo_(NULL
), var_(NULL
),
3493 statements_added_(added
)
3496 // Constructor for a global variable.
3497 Statement_inserter(Gogo
* gogo
, Variable
* var
, Statements
*added
= NULL
)
3498 : block_(NULL
), pindex_(NULL
), gogo_(gogo
), var_(var
),
3499 statements_added_(added
)
3500 { go_assert(var
->is_global()); }
3502 // We use the default copy constructor and assignment operator.
3504 // Insert S before the statement we are traversing, or before the
3505 // initialization expression of a global variable.
3507 insert(Statement
* s
);
3510 // The block that the statement is in.
3512 // The index of the statement that we are traversing.
3514 // The IR, needed when looking at an initializer expression for a
3517 // The global variable, when looking at an initializer expression.
3519 // If non-null, a set to record new statements inserted (non-owned).
3520 Statements
* statements_added_
;
3523 // When translating the gogo IR into the backend data structure, this
3524 // is the context we pass down the blocks and statements.
3526 class Translate_context
3529 Translate_context(Gogo
* gogo
, Named_object
* function
, Block
* block
,
3531 : gogo_(gogo
), backend_(gogo
->backend()), function_(function
),
3532 block_(block
), bblock_(bblock
), is_const_(false)
3539 { return this->gogo_
; }
3543 { return this->backend_
; }
3547 { return this->function_
; }
3551 { return this->block_
; }
3555 { return this->bblock_
; }
3559 { return this->is_const_
; }
3561 // Make a constant context.
3564 { this->is_const_
= true; }
3567 // The IR for the entire compilation unit.
3569 // The generator for the backend data structures.
3571 // The function we are currently translating. NULL if not in a
3572 // function, e.g., the initializer of a global variable.
3573 Named_object
* function_
;
3574 // The block we are currently translating. NULL if not in a
3577 // The backend representation of the current block. NULL if block_
3580 // Whether this is being evaluated in a constant context. This is
3581 // used for type descriptor initializers.
3585 // Runtime error codes. These must match the values in
3586 // libgo/runtime/go-runtime-error.c.
3588 // Slice index out of bounds: negative or larger than the length of
3590 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
= 0;
3592 // Array index out of bounds.
3593 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
= 1;
3595 // String index out of bounds.
3596 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
= 2;
3598 // Slice slice out of bounds: negative or larger than the length of
3599 // the slice or high bound less than low bound.
3600 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
= 3;
3602 // Array slice out of bounds.
3603 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
= 4;
3605 // String slice out of bounds.
3606 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
= 5;
3608 // Dereference of nil pointer. This is used when there is a
3609 // dereference of a pointer to a very large struct or array, to ensure
3610 // that a gigantic array is not used a proxy to access random memory
3612 static const int RUNTIME_ERROR_NIL_DEREFERENCE
= 6;
3614 // Slice length or capacity out of bounds in make: negative or
3615 // overflow or length greater than capacity.
3616 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
= 7;
3618 // Map capacity out of bounds in make: negative or overflow.
3619 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
= 8;
3621 // Channel capacity out of bounds in make: negative or overflow.
3622 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
= 9;
3624 // Division by zero.
3625 static const int RUNTIME_ERROR_DIVISION_BY_ZERO
= 10;
3627 // Go statement with nil function.
3628 static const int RUNTIME_ERROR_GO_NIL
= 11;
3630 // This is used by some of the langhooks.
3631 extern Gogo
* go_get_gogo();
3633 // Whether we have seen any errors. FIXME: Replace with a backend
3635 extern bool saw_errors();
3637 #endif // !defined(GO_GOGO_H)