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 // The name of some backend object. Backend objects have a
61 // user-visible name and an assembler name. The user visible name
62 // might include arbitrary Unicode characters. The assembler name
69 : prefix_(NULL
), components_(), count_(0), suffix_(),
70 is_asm_name_(false), is_non_identifier_(false)
73 // Set the prefix. Prefixes are always constant strings.
75 set_prefix(const char* p
)
77 go_assert(this->prefix_
== NULL
&& !this->is_asm_name_
);
83 set_suffix(const std::string
& s
)
85 go_assert(this->suffix_
.empty() && !this->is_asm_name_
);
89 // Append to the suffix.
91 append_suffix(const std::string
& s
)
93 if (this->is_asm_name_
)
94 this->components_
[0].append(s
);
96 this->suffix_
.append(s
);
101 add(const std::string
& c
)
103 go_assert(this->count_
< Backend_name::max_components
104 && !this->is_asm_name_
);
105 this->components_
[this->count_
] = c
;
109 // Set an assembler name specified by the user. This overrides both
110 // the user-visible name and the assembler name. No further
111 // encoding is applied.
113 set_asm_name(const std::string
& n
)
115 go_assert(this->prefix_
== NULL
117 && this->suffix_
.empty()
118 && !this->is_asm_name_
);
119 this->components_
[0] = n
;
120 this->is_asm_name_
= true;
123 // Whether some component includes some characters that can't appear
126 is_non_identifier() const
127 { return this->is_non_identifier_
; }
129 // Record that some component includes some character that can't
130 // appear in an identifier.
132 set_is_non_identifier()
133 { this->is_non_identifier_
= true; }
135 // Get the user visible name.
139 // Get the assembler name. This may be the same as the user visible
144 // Get an optional assembler name: if it would be the same as the
145 // user visible name, this returns the empty string.
147 optional_asm_name() const;
150 // The maximum number of components.
151 static const int max_components
= 4;
153 // An optional prefix that does not require encoding.
155 // Up to four components. The name will include these components
156 // separated by dots. Each component will be underscore-encoded
157 // (see the long comment near the top of names.cc).
158 std::string components_
[Backend_name::max_components
];
159 // Number of components.
161 // An optional suffix that does not require encoding.
163 // True if components_[0] is an assembler name specified by the user.
165 // True if some component includes some character that can't
166 // normally appear in an identifier.
167 bool is_non_identifier_
;
170 // An initialization function for an imported package. This is a
171 // magic function which initializes variables and runs the "init"
177 Import_init(const std::string
& package_name
, const std::string
& init_name
,
179 : package_name_(package_name
), init_name_(init_name
), priority_(priority
)
182 // The name of the package being imported.
185 { return this->package_name_
; }
187 // The name of the package's init function.
190 { return this->init_name_
; }
192 // Older V1 export data uses a priority scheme to order
193 // initialization functions; functions with a lower priority number
194 // must be run first. This value will be set to -1 for current
195 // generation objects, and will take on a non-negative value only
196 // when importing a V1-vintage object.
199 { return this->priority_
; }
203 set_priority(int new_priority
)
204 { this->priority_
= new_priority
; }
206 // Record the fact that some other init fcn must be run before this init fcn.
208 record_precursor_fcn(std::string init_fcn_name
)
209 { this->precursor_functions_
.insert(init_fcn_name
); }
211 // Return the list of precursor fcns for this fcn (must be run before it).
212 const std::set
<std::string
>&
214 { return this->precursor_functions_
; }
216 // Whether this is a dummy init, which is used only to record transitive import.
219 { return this->init_name_
[0] == '~'; }
222 // The name of the package being imported.
223 std::string package_name_
;
224 // The name of the package's init function.
225 std::string init_name_
;
226 // Names of init functions that must be run before this fcn.
227 std::set
<std::string
> precursor_functions_
;
228 // Priority for this function. See note above on obsolescence.
232 // For sorting purposes.
234 struct Import_init_lt
{
235 bool operator()(const Import_init
* i1
, const Import_init
* i2
) const
237 return i1
->init_name() < i2
->init_name();
241 // Set of import init objects.
242 class Import_init_set
: public std::set
<Import_init
*, Import_init_lt
> {
246 priority_compare(const Import_init
* i1
, const Import_init
* i2
)
248 if (i1
->priority() < i2
->priority())
250 if (i1
->priority() > i2
->priority())
252 if (i1
->package_name() != i2
->package_name())
253 return i1
->package_name() < i2
->package_name();
254 return i1
->init_name() < i2
->init_name();
257 // The holder for the internal representation of the entire
263 // Create the IR, passing in the sizes of the types "int" and
264 // "uintptr" in bits.
265 Gogo(Backend
* backend
, Linemap
*linemap
, int int_type_size
, int pointer_size
);
267 // Get the backend generator.
270 { return this->backend_
; }
272 // Get the Location generator.
275 { return this->linemap_
; }
277 // Get the package name.
279 package_name() const;
281 // Set the package name.
283 set_package_name(const std::string
&, Location
);
285 // Return whether this is the "main" package.
287 is_main_package() const;
289 // If necessary, adjust the name to use for a hidden symbol. We add
290 // the package name, so that hidden symbols in different packages do
293 pack_hidden_name(const std::string
& name
, bool is_exported
) const
297 : '.' + this->pkgpath() + '.' + name
);
300 // Unpack a name which may have been hidden. Returns the
301 // user-visible name of the object.
303 unpack_hidden_name(const std::string
& name
)
304 { return name
[0] != '.' ? name
: name
.substr(name
.rfind('.') + 1); }
306 // Return whether a possibly packed name is hidden.
308 is_hidden_name(const std::string
& name
)
309 { return name
[0] == '.'; }
311 // Return the package path of a hidden name.
313 hidden_name_pkgpath(const std::string
& name
)
315 go_assert(Gogo::is_hidden_name(name
));
316 return name
.substr(1, name
.rfind('.') - 1);
319 // Given a name which may or may not have been hidden, append the
320 // appropriate version of the name to the result string.
322 append_possibly_hidden_name(std::string
*result
, const std::string
& name
);
324 // Given a name which may or may not have been hidden, return the
325 // name to use in an error message.
327 message_name(const std::string
& name
);
329 // Return whether a name is the blank identifier _.
331 is_sink_name(const std::string
& name
)
333 return (name
[0] == '.'
334 && name
[name
.length() - 1] == '_'
335 && name
[name
.length() - 2] == '.')
337 && name
.length() == 1);
340 // Helper used when adding parameters (including receiver param) to the
341 // bindings of a function. If the specified parameter name is empty or
342 // corresponds to the sink name, param name is replaced with a new unique
343 // name. PNAME is the address of a string containing the parameter variable
344 // name to be checked/updated; TAG is a descriptive tag to be used in
345 // manufacturing the new unique name, and COUNT is the address of a counter
346 // holding the number of params renamed so far with the tag in question.
348 rename_if_empty(std::string
* pname
, const char* tag
, unsigned* count
);
350 // Convert a pkgpath into a string suitable for a symbol
352 pkgpath_for_symbol(const std::string
& pkgpath
);
354 // Compute a hash code for a string, given a seed.
356 hash_string(const std::string
&, unsigned int);
358 // Return the package path to use for reflect.Type.PkgPath.
362 // Return the package path to use for a symbol name.
364 pkgpath_symbol() const;
366 // Set the package path from a command line option.
368 set_pkgpath(const std::string
&);
370 // Set the prefix from a command line option.
372 set_prefix(const std::string
&);
374 // Return whether pkgpath was set from a command line option.
376 pkgpath_from_option() const
377 { return this->pkgpath_from_option_
; }
379 // Return the relative import path as set from the command line.
380 // Returns an empty string if it was not set.
382 relative_import_path() const
383 { return this->relative_import_path_
; }
385 // Set the relative import path from a command line option.
387 set_relative_import_path(const std::string
& s
)
388 { this->relative_import_path_
= s
; }
390 // Set the C header file to write. This is used for the runtime
393 set_c_header(const std::string
& s
)
394 { this->c_header_
= s
; }
396 // Read an embedcfg file.
398 read_embedcfg(const char* filename
);
400 // Return whether the current file imports "embed".
402 is_embed_imported() const;
404 // Build an initializer for a variable with a go:embed directive.
406 initializer_for_embeds(Type
*, const std::vector
<std::string
>*, Location
);
408 // Return whether to check for division by zero in binary operations.
410 check_divide_by_zero() const
411 { return this->check_divide_by_zero_
; }
413 // Set the option to check division by zero from a command line option.
415 set_check_divide_by_zero(bool b
)
416 { this->check_divide_by_zero_
= b
; }
418 // Return whether to check for division overflow in binary operations.
420 check_divide_overflow() const
421 { return this->check_divide_overflow_
; }
423 // Set the option to check division overflow from a command line option.
425 set_check_divide_overflow(bool b
)
426 { this->check_divide_overflow_
= b
; }
428 // Return whether we are compiling the runtime package.
430 compiling_runtime() const
431 { return this->compiling_runtime_
; }
433 // Set whether we are compiling the runtime package.
435 set_compiling_runtime(bool b
)
436 { this->compiling_runtime_
= b
; }
438 // Return the level of escape analysis debug information to emit.
440 debug_escape_level() const
441 { return this->debug_escape_level_
; }
443 // Set the level of escape analysis debugging from a command line option.
445 set_debug_escape_level(int level
)
446 { this->debug_escape_level_
= level
; }
448 // Return the hash for debug escape analysis.
450 debug_escape_hash() const
451 { return this->debug_escape_hash_
; }
453 // Set the hash value for debug escape analysis.
455 set_debug_escape_hash(const std::string
& s
)
456 { this->debug_escape_hash_
= s
; }
458 // Return whether to output optimization diagnostics.
460 debug_optimization() const
461 { return this->debug_optimization_
; }
463 // Set the option to output optimization diagnostics.
465 set_debug_optimization(bool b
)
466 { this->debug_optimization_
= b
; }
468 // Dump to stderr for debugging
471 // Return the size threshold used to determine whether to issue
472 // a nil-check for a given pointer dereference. A threshold of -1
473 // implies that all potentially faulting dereference ops should
474 // be nil-checked. A positive threshold of N implies that a deref
475 // of *P where P has size less than N doesn't need a nil check.
477 nil_check_size_threshold() const
478 { return this->nil_check_size_threshold_
; }
480 // Set the nil-check size threshold, as described above.
482 set_nil_check_size_threshold(int64_t bytes
)
483 { this->nil_check_size_threshold_
= bytes
; }
485 // Return whether runtime.eqtype calls are needed when comparing
489 { return this->need_eqtype_
; }
491 // Set if calls to runtime.eqtype are needed.
493 set_need_eqtype(bool b
)
494 { this->need_eqtype_
= b
; }
496 // Import a package. FILENAME is the file name argument, LOCAL_NAME
497 // is the local name to give to the package. If LOCAL_NAME is empty
498 // the declarations are added to the global scope.
500 import_package(const std::string
& filename
, const std::string
& local_name
,
501 bool is_local_name_exported
, bool must_exist
, Location
);
503 // Whether we are the global binding level.
505 in_global_scope() const;
507 // Look up a name in the current binding contours.
509 lookup(const std::string
&, Named_object
** pfunction
) const;
511 // Look up a name in the current block.
513 lookup_in_block(const std::string
&) const;
515 // Look up a name in the global namespace--the universal scope.
517 lookup_global(const char*) const;
519 // Add a new imported package. REAL_NAME is the real name of the
520 // package. ALIAS is the alias of the package; this may be the same
521 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
522 // this package should be added to the global namespace; this is
523 // true if the alias is ".". LOCATION is the location of the import
524 // statement. This returns the new package, or NULL on error.
526 add_imported_package(const std::string
& real_name
, const std::string
& alias
,
527 bool is_alias_exported
,
528 const std::string
& pkgpath
,
529 const std::string
& pkgpath_symbol
,
531 bool* padd_to_globals
);
533 // Register a package. This package may or may not be imported.
534 // This returns the Package structure for the package, creating if
537 register_package(const std::string
& pkgpath
,
538 const std::string
& pkgpath_symbol
, Location
);
540 // Look up a package by pkgpath, and return its pkgpath_symbol.
542 pkgpath_symbol_for_package(const std::string
&);
544 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
545 // method function should be added to the type of its receiver.
547 start_function(const std::string
& name
, Function_type
* type
,
548 bool add_method_to_type
, Location
);
550 // Finish compiling a function.
552 finish_function(Location
);
554 // Return the current function.
556 current_function() const;
558 // Return the current block.
562 // Start a new block. This is not initially associated with a
565 start_block(Location
);
567 // Finish the current block and return it.
569 finish_block(Location
);
571 // Declare an erroneous name. This is used to avoid knock-on errors
572 // after a parsing error.
574 add_erroneous_name(const std::string
& name
);
576 // Declare an unknown name. This is used while parsing. The name
577 // must be resolved by the end of the parse. Unknown names are
578 // always added at the package level.
580 add_unknown_name(const std::string
& name
, Location
);
582 // Declare a function.
584 declare_function(const std::string
&, Function_type
*, Location
);
586 // Declare a function at the package level. This is used for
587 // functions generated for a type.
589 declare_package_function(const std::string
&, Function_type
*, Location
);
591 // Add a function declaration to the list of functions we may want
594 add_imported_inlinable_function(Named_object
*);
596 // Add a function to the list of functions that we do want to
599 add_imported_inline_function(Named_object
* no
)
600 { this->imported_inline_functions_
.push_back(no
); }
604 add_label_definition(const std::string
&, Location
);
606 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
607 // report errors for a goto from the current location to the label
610 add_label_reference(const std::string
&, Location
,
611 bool issue_goto_errors
);
613 // An analysis set is a list of functions paired with a boolean that indicates
614 // whether the list of functions are recursive.
615 typedef std::pair
<std::vector
<Named_object
*>, bool> Analysis_set
;
617 // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
620 add_analysis_set(const std::vector
<Named_object
*>& group
, bool recursive
)
621 { this->analysis_sets_
.push_back(std::make_pair(group
, recursive
)); }
623 // Return a snapshot of the current binding state.
625 bindings_snapshot(Location
);
627 // Add a statement to the current block.
629 add_statement(Statement
*);
631 // Add a block to the current block.
633 add_block(Block
*, Location
);
637 add_constant(const Typed_identifier
&, Expression
*, int iota_value
);
641 add_type(const std::string
&, Type
*, Location
);
643 // Add a named type. This is used for builtin types, and to add an
644 // imported type to the global scope.
646 add_named_type(Named_type
*);
650 declare_type(const std::string
&, Location
);
652 // Declare a type at the package level. This is used when the
653 // parser sees an unknown name where a type name is required.
655 declare_package_type(const std::string
&, Location
);
657 // Define a type which was already declared.
659 define_type(Named_object
*, Named_type
*);
663 add_variable(const std::string
&, Variable
*);
665 // Add a sink--a reference to the blank identifier _.
669 // Add a type which needs to be verified. This is used for sink
670 // types, just to give appropriate error messages.
672 add_type_to_verify(Type
* type
);
674 // Add a named object to the current namespace. This is used for
675 // import . "package".
677 add_dot_import_object(Named_object
*);
679 // Add an identifier to the list of names seen in the file block.
681 add_file_block_name(const std::string
& name
, Location location
)
682 { this->file_block_names_
[name
] = location
; }
684 // Add a linkname, from the go:linkname compiler directive. This
685 // changes the externally visible name of GO_NAME to be EXT_NAME.
686 // If EXT_NAME is the empty string, GO_NAME is unchanged, but the
687 // symbol is made publicly visible.
689 add_linkname(const std::string
& go_name
, bool is_exported
,
690 const std::string
& ext_name
, Location location
);
692 // Mark all local variables in current bindings as used. This is
693 // used when there is a parse error to avoid useless errors.
697 // Note that we've seen an interface type. This is used to build
698 // all required interface method tables.
700 record_interface_type(Interface_type
*);
702 // Note that we need an initialization function.
705 { this->need_init_fn_
= true; }
707 // Return whether the current file imported the unsafe package.
709 current_file_imported_unsafe() const
710 { return this->current_file_imported_unsafe_
; }
712 // Clear out all names in file scope. This is called when we start
713 // parsing a new file.
717 // Record that VAR1 must be initialized after VAR2. This is used
718 // when VAR2 does not appear in VAR1's INIT or PREINIT.
720 record_var_depends_on(Variable
* var1
, Named_object
* var2
)
722 go_assert(this->var_deps_
.find(var1
) == this->var_deps_
.end());
723 this->var_deps_
[var1
] = var2
;
726 // Return the variable that VAR depends on, or NULL if none.
728 var_depends_on(Variable
* var
) const
730 Var_deps::const_iterator p
= this->var_deps_
.find(var
);
731 return p
!= this->var_deps_
.end() ? p
->second
: NULL
;
734 // Queue up a type-specific hash function to be written out. This
735 // is used when a type-specific hash function is needed when not at
738 queue_hash_function(Type
* type
, int64_t size
, Backend_name
*,
739 Function_type
* hash_fntype
);
741 // Queue up a type-specific equal function to be written out. This
742 // is used when a type-specific equal function is needed when not at
745 queue_equal_function(Type
* type
, Named_type
* name
, int64_t size
,
746 Backend_name
*, Function_type
* equal_fntype
);
748 // Write out queued specific type functions.
750 write_specific_type_functions();
752 // Whether we are done writing out specific type functions.
754 specific_type_functions_are_written() const
755 { return this->specific_type_functions_are_written_
; }
757 // Add a pointer that needs to be added to the list of objects
758 // traversed by the garbage collector. This should be an expression
759 // of pointer type that points to static storage. It's not
760 // necessary to add global variables to this list, just global
761 // variable initializers that would otherwise not be seen.
763 add_gc_root(Expression
* expr
)
765 this->set_need_init_fn();
766 this->gc_roots_
.push_back(expr
);
769 // Add a type to the descriptor list.
771 add_type_descriptor(Type
* type
)
772 { this->type_descriptors_
.push_back(type
); }
774 // Traverse the tree. See the Traverse class.
778 // Define the predeclared global names.
780 define_global_names();
782 // Verify and complete all types.
786 // Lower the parse tree.
790 // Lower all the statements in a block.
792 lower_block(Named_object
* function
, Block
*);
794 // Lower an expression.
796 lower_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
800 lower_constant(Named_object
*);
802 // Flatten all the statements in a block.
804 flatten_block(Named_object
* function
, Block
*);
806 // Flatten an expression.
808 flatten_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
810 // Create all necessary function descriptors.
812 create_function_descriptors();
814 // Finalize the method lists and build stub methods for named types.
818 // Finalize the method list for one type.
820 finalize_methods_for_type(Type
*);
822 // Work out the types to use for unspecified variables and
827 // Type check the program.
831 // Check the types in a single block. This is used for complicated
834 check_types_in_block(Block
*);
836 // Check for return statements.
838 check_return_statements();
844 // Make implicit type conversions explicit.
848 // Make implicit type conversions explicit in a block.
850 add_conversions_in_block(Block
*);
852 // Analyze the program flow for escape information.
856 // Discover the groups of possibly recursive functions in this package.
858 discover_analysis_sets();
860 // Build a connectivity graph between the objects in each analyzed function.
862 assign_connectivity(Escape_context
*, Named_object
*);
864 // Traverse the objects in the connecitivty graph from the sink, adjusting the
865 // escape levels of each object.
867 propagate_escape(Escape_context
*, Node
*);
869 // Add notes about the escape level of a function's input and output
870 // parameters for exporting and importing top level functions.
872 tag_function(Escape_context
*, Named_object
*);
874 // Reclaim memory of escape analysis Nodes.
876 reclaim_escape_nodes();
882 // Add an import control function for an imported package to the
885 add_import_init_fn(const std::string
& package_name
,
886 const std::string
& init_name
, int prio
);
888 // Return the Import_init for a given init name.
890 lookup_init(const std::string
& init_name
);
892 // Turn short-cut operators (&&, ||) into explicit if statements.
896 // Turn short-cut operators into explicit if statements in a block.
898 remove_shortcuts_in_block(Block
*);
900 // Use temporary variables to force order of evaluation.
904 // Order evaluations in a block.
908 // Add write barriers as needed.
910 add_write_barriers();
912 // Return whether an assignment that sets LHS to RHS needs a write
915 assign_needs_write_barrier(Expression
* lhs
,
916 Unordered_set(const Named_object
*)*);
918 // Return whether EXPR is the address of a variable that can be set
919 // without a write barrier. That is, if this returns true, then an
920 // assignment to *EXPR does not require a write barrier.
922 is_nonwb_pointer(Expression
* expr
, Unordered_set(const Named_object
*)*);
924 // Return an assignment that sets LHS to RHS using a write barrier.
925 // This returns an if statement that checks whether write barriers
926 // are enabled. If not, it does LHS = RHS, otherwise it calls the
927 // appropriate write barrier function.
929 assign_with_write_barrier(Function
*, Block
*, Statement_inserter
*,
930 Expression
* lhs
, Expression
* rhs
, Location
);
932 // Return a statement that tests whether write barriers are enabled
933 // and executes either the efficient code (WITHOUT) or the write
934 // barrier function call (WITH), depending.
936 check_write_barrier(Block
*, Statement
* without
, Statement
* with
);
938 // Flatten parse tree.
942 // Build thunks for functions which call recover.
944 build_recover_thunks();
946 // Simplify statements which might use thunks: go and defer
949 simplify_thunk_statements();
951 // Dump AST if -fgo-dump-ast is set.
953 dump_ast(const char* basename
);
955 // Dump Call Graph if -fgo-dump-calls is set.
957 dump_call_graph(const char* basename
);
959 // Dump Connection Graphs if -fgo-dump-connections is set.
961 dump_connection_graphs(const char* basename
);
963 // Convert named types to the backend representation.
965 convert_named_types();
967 // Convert named types in a list of bindings.
969 convert_named_types_in_bindings(Bindings
*);
971 // True if named types have been converted to the backend
974 named_types_are_converted() const
975 { return this->named_types_are_converted_
; }
977 // Give an error if the initialization of VAR depends on itself.
979 check_self_dep(Named_object
*);
981 // Write out the global values.
985 // Build required interface method tables.
987 build_interface_method_tables();
989 // Return an expression which allocates memory to hold values of type TYPE.
991 allocate_memory(Type
*type
, Location
);
993 // Get the backend name to use for an exported function, a method,
994 // or a function/method declaration.
996 function_backend_name(const std::string
& go_name
, const Package
*,
997 const Type
* receiver
, Backend_name
*);
999 // Return the name to use for a function descriptor.
1001 function_descriptor_backend_name(Named_object
*, Backend_name
*);
1003 // Return the name to use for a generated stub method.
1005 stub_method_name(const Package
*, const std::string
& method_name
);
1007 // Get the backend name of the hash function for TYPE.
1009 hash_function_name(const Type
*, Backend_name
*);
1011 // Get the backend name of the equal function for TYPE.
1013 equal_function_name(const Type
*, const Named_type
*, Backend_name
*);
1015 // Get the backend name to use for a global variable.
1017 global_var_backend_name(const std::string
& go_name
, const Package
*,
1020 // Return a name to use for an error case. This should only be used
1021 // after reporting an error, and is used to avoid useless knockon
1026 // Return whether the name indicates an error.
1028 is_erroneous_name(const std::string
&);
1030 // Return a name to use for a thunk function. A thunk function is
1031 // one we create during the compilation, for a go statement or a
1032 // defer statement or a method expression.
1036 // Return whether an object is a thunk.
1038 is_thunk(const Named_object
*);
1040 // Return the name to use for an init function.
1042 init_function_name();
1044 // Return the name to use for a nested function.
1046 nested_function_name(Named_object
* enclosing
);
1048 // Return the name to use for a sink funciton.
1050 sink_function_name();
1052 // Return the name to use for an (erroneous) redefined function.
1054 redefined_function_name();
1056 // Return the name for use for a recover thunk.
1058 recover_thunk_name(const std::string
& name
, const Type
* rtype
);
1060 // Return the name to use for the GC root variable.
1064 // Return the name to use for a composite literal or string
1069 // Return the name of the variable used to represent the zero value
1072 map_zero_value_name();
1074 // Get the name of the magic initialization function.
1078 // Return the name for a dummy init function, which is not a real
1079 // function but only for tracking transitive import.
1081 dummy_init_fn_name();
1083 // Return the package path symbol from an init function name, which
1084 // can be a real init function or a dummy one.
1086 pkgpath_symbol_from_init_fn_name(std::string
);
1088 // Get the backend name for a type descriptor symbol.
1090 type_descriptor_backend_name(const Type
*, Named_type
*, Backend_name
*);
1092 // Return the name of the type descriptor list symbol of a package.
1093 // The argument is an encoded pkgpath, as with pkgpath_symbol.
1095 type_descriptor_list_symbol(const std::string
&);
1097 // Return the name of the list of all type descriptor lists.
1101 // Return the assembler name for the GC symbol for a type.
1103 gc_symbol_name(Type
*);
1105 // Return the assembler name for a ptrmask variable.
1107 ptrmask_symbol_name(const std::string
& ptrmask_sym_name
);
1109 // Return the name to use for an interface method table.
1111 interface_method_table_name(Interface_type
*, Type
*, bool is_pointer
);
1113 // If NAME is a special name used as a Go identifier, return the
1114 // position within the string where the special part of the name
1117 special_name_pos(const std::string
& name
);
1120 // During parsing, we keep a stack of functions. Each function on
1121 // the stack is one that we are currently parsing. For each
1122 // function, we keep track of the current stack of blocks.
1123 struct Open_function
1126 Named_object
* function
;
1127 // The stack of active blocks in the function.
1128 std::vector
<Block
*> blocks
;
1131 // The stack of functions.
1132 typedef std::vector
<Open_function
> Open_functions
;
1134 // Set up the built-in unsafe package.
1136 import_unsafe(const std::string
&, bool is_exported
, Location
);
1138 // Return the current binding contour.
1143 current_bindings() const;
1148 // Get the decl for the magic initialization function.
1150 initialization_function_decl();
1152 // Create the magic initialization function.
1154 create_initialization_function(Named_object
* fndecl
, Bstatement
* code_stmt
);
1156 // Initialize imported packages. BFUNCTION is the function
1157 // into which the package init calls will be placed.
1159 init_imports(std::vector
<Bstatement
*>&, Bfunction
* bfunction
);
1161 // Register variables with the garbage collector.
1163 register_gc_vars(const std::vector
<Named_object
*>&,
1164 std::vector
<Bstatement
*>&,
1165 Bfunction
* init_bfunction
);
1167 // Build the list of type descriptors.
1169 build_type_descriptor_list();
1171 // Register the type descriptors with the runtime.
1173 register_type_descriptors(std::vector
<Bstatement
*>&,
1174 Bfunction
* init_bfunction
);
1177 propagate_writebarrierrec();
1180 write_barrier_variable();
1183 is_digits(const std::string
&);
1185 // Type used to map go:embed patterns to a list of files.
1186 typedef Unordered_map(std::string
, std::vector
<std::string
>) Embed_patterns
;
1188 // Type used to map go:embed file names to their full path.
1189 typedef Unordered_map(std::string
, std::string
) Embed_files
;
1191 // Type used to map import names to packages.
1192 typedef std::map
<std::string
, Package
*> Imports
;
1194 // Type used to map package names to packages.
1195 typedef std::map
<std::string
, Package
*> Packages
;
1197 // Type used to map variables to the function calls that set them.
1198 // This is used for initialization dependency analysis.
1199 typedef std::map
<Variable
*, Named_object
*> Var_deps
;
1201 // Type used to map identifiers in the file block to the location
1202 // where they were defined.
1203 typedef Unordered_map(std::string
, Location
) File_block_names
;
1205 // Type used to queue writing a type specific function.
1206 struct Specific_type_function
1208 enum Specific_type_function_kind
{ SPECIFIC_HASH
, SPECIFIC_EQUAL
};
1213 Specific_type_function_kind kind
;
1215 Function_type
* fntype
;
1217 Specific_type_function(Type
* atype
, Named_type
* aname
, int64_t asize
,
1218 Specific_type_function_kind akind
,
1219 Backend_name
* abname
,
1220 Function_type
* afntype
)
1221 : type(atype
), name(aname
), size(asize
), kind(akind
),
1222 bname(*abname
), fntype(afntype
)
1226 // Recompute init priorities.
1228 recompute_init_priorities();
1230 // Recursive helper used by the routine above.
1232 update_init_priority(Import_init
* ii
,
1233 std::set
<const Import_init
*>* visited
);
1235 // The backend generator.
1237 // The object used to keep track of file names and line numbers.
1239 // The package we are compiling.
1241 // The list of currently open functions during parsing.
1242 Open_functions functions_
;
1243 // The global binding contour. This includes the builtin functions
1244 // and the package we are compiling.
1246 // The list of names we have seen in the file block.
1247 File_block_names file_block_names_
;
1248 // Mapping from import file names to packages.
1250 // Whether the magic unsafe package was imported.
1251 bool imported_unsafe_
;
1252 // Whether the magic unsafe package was imported by the current file.
1253 bool current_file_imported_unsafe_
;
1254 // Mapping from package names we have seen to packages. This does
1255 // not include the package we are compiling.
1257 // The functions named "init", if there are any.
1258 std::vector
<Named_object
*> init_functions_
;
1259 // A mapping from variables to the function calls that initialize
1260 // them, if it is not stored in the variable's init or preinit.
1261 // This is used for dependency analysis.
1263 // Whether we need a magic initialization function.
1265 // The name of the magic initialization function.
1266 std::string init_fn_name_
;
1267 // A list of import control variables for packages that we import.
1268 Import_init_set imported_init_fns_
;
1269 // The package path used for reflection data.
1270 std::string pkgpath_
;
1271 // The package path to use for a symbol name.
1272 std::string pkgpath_symbol_
;
1273 // The prefix to use for symbols, from the -fgo-prefix option.
1274 std::string prefix_
;
1275 // Whether pkgpath_ has been set.
1277 // Whether an explicit package path was set by -fgo-pkgpath.
1278 bool pkgpath_from_option_
;
1279 // Whether an explicit prefix was set by -fgo-prefix.
1280 bool prefix_from_option_
;
1281 // The relative import path, from the -fgo-relative-import-path
1283 std::string relative_import_path_
;
1284 // The C header file to write, from the -fgo-c-header option.
1285 std::string c_header_
;
1286 // Patterns from an embedcfg file.
1287 Embed_patterns embed_patterns_
;
1288 // Mapping from file to full path from an embedcfg file.
1289 Embed_files embed_files_
;
1290 // Whether or not to check for division by zero, from the
1291 // -fgo-check-divide-zero option.
1292 bool check_divide_by_zero_
;
1293 // Whether or not to check for division overflow, from the
1294 // -fgo-check-divide-overflow option.
1295 bool check_divide_overflow_
;
1296 // Whether we are compiling the runtime package, from the
1297 // -fgo-compiling-runtime option.
1298 bool compiling_runtime_
;
1299 // The level of escape analysis debug information to emit, from the
1300 // -fgo-debug-escape option.
1301 int debug_escape_level_
;
1302 // A hash value for debug escape analysis, from the
1303 // -fgo-debug-escape-hash option. The analysis is run only on
1304 // functions with names that hash to the matching value.
1305 std::string debug_escape_hash_
;
1306 // Whether to output optimization diagnostics, from the
1307 // -fgo-debug-optimization option.
1308 bool debug_optimization_
;
1309 // Nil-check size threshhold.
1310 int64_t nil_check_size_threshold_
;
1311 // Whether runtime.eqtype calls are needed when comparing type
1314 // A list of types to verify.
1315 std::vector
<Type
*> verify_types_
;
1316 // A list of interface types defined while parsing.
1317 std::vector
<Interface_type
*> interface_types_
;
1318 // Type specific functions to write out.
1319 std::vector
<Specific_type_function
*> specific_type_functions_
;
1320 // Whether we are done writing out specific type functions.
1321 bool specific_type_functions_are_written_
;
1322 // Whether named types have been converted.
1323 bool named_types_are_converted_
;
1324 // A list containing groups of possibly mutually recursive functions to be
1325 // considered during escape analysis.
1326 std::vector
<Analysis_set
> analysis_sets_
;
1327 // A list of objects to add to the GC roots.
1328 std::vector
<Expression
*> gc_roots_
;
1329 // A list of type descriptors that we need to register.
1330 std::vector
<Type
*> type_descriptors_
;
1331 // A list of function declarations with imported bodies that we may
1333 std::vector
<Named_object
*> imported_inlinable_functions_
;
1334 // A list of functions that we want to inline. These will be sent
1336 std::vector
<Named_object
*> imported_inline_functions_
;
1339 // A block of statements.
1344 Block(Block
* enclosing
, Location
);
1346 // Return the enclosing block.
1349 { return this->enclosing_
; }
1351 // Return the bindings of the block.
1354 { return this->bindings_
; }
1358 { return this->bindings_
; }
1360 // Look at the block's statements.
1361 const std::vector
<Statement
*>*
1363 { return &this->statements_
; }
1365 // Return the start location. This is normally the location of the
1366 // left curly brace which starts the block.
1368 start_location() const
1369 { return this->start_location_
; }
1371 // Return the end location. This is normally the location of the
1372 // right curly brace which ends the block.
1374 end_location() const
1375 { return this->end_location_
; }
1377 // Add a statement to the block.
1379 add_statement(Statement
*);
1381 // Add a statement to the front of the block.
1383 add_statement_at_front(Statement
*);
1385 // Replace a statement in a block.
1387 replace_statement(size_t index
, Statement
*);
1389 // Add a Statement before statement number INDEX.
1391 insert_statement_before(size_t index
, Statement
*);
1393 // Add a Statement after statement number INDEX.
1395 insert_statement_after(size_t index
, Statement
*);
1397 // Set the end location of the block.
1399 set_end_location(Location location
)
1400 { this->end_location_
= location
; }
1402 // Traverse the tree.
1404 traverse(Traverse
*);
1406 // Set final types for unspecified variables and constants.
1410 // Return true if execution of this block may fall through to the
1413 may_fall_through() const;
1415 // Write the export data for the block's statements to the string.
1417 export_block(Export_function_body
*);
1419 // Turn exported block data into a block.
1421 import_block(Block
*, Import_function_body
*, Location
);
1423 // Convert the block to the backend representation.
1425 get_backend(Translate_context
*);
1427 // Iterate over statements.
1429 typedef std::vector
<Statement
*>::iterator iterator
;
1433 { return this->statements_
.begin(); }
1437 { return this->statements_
.end(); }
1442 // Statements in the block.
1443 std::vector
<Statement
*> statements_
;
1445 Bindings
* bindings_
;
1446 // Location of start of block.
1447 Location start_location_
;
1448 // Location of end of block.
1449 Location end_location_
;
1457 Function(Function_type
* type
, Named_object
*, Block
*, Location
);
1459 // Return the function's type.
1462 { return this->type_
; }
1464 // Return the enclosing function if there is one.
1467 { return this->enclosing_
; }
1469 // Set the enclosing function. This is used when building thunks
1470 // for functions which call recover.
1472 set_enclosing(Named_object
* enclosing
)
1474 go_assert(this->enclosing_
== NULL
);
1475 this->enclosing_
= enclosing
;
1478 // The result variables.
1479 typedef std::vector
<Named_object
*> Results
;
1481 // Create the result variables in the outer block.
1483 create_result_variables(Gogo
*);
1485 // Update the named result variables when cloning a function which
1488 update_result_variables();
1490 // Return the result variables.
1493 { return this->results_
; }
1497 { return this->is_sink_
; }
1501 { this->is_sink_
= true; }
1503 // Whether the result variables have names.
1505 results_are_named() const
1506 { return this->results_are_named_
; }
1508 // Return the assembler name.
1511 { return this->asm_name_
; }
1513 // Set the assembler name.
1515 set_asm_name(const std::string
& asm_name
)
1516 { this->asm_name_
= asm_name
; }
1518 // Mark this symbol as exported by a linkname directive.
1520 set_is_exported_by_linkname()
1521 { this->is_exported_by_linkname_
= true; }
1523 // Return the pragmas for this function.
1526 { return this->pragmas_
; }
1528 // Set the pragmas for this function.
1530 set_pragmas(unsigned int pragmas
)
1532 this->pragmas_
= pragmas
;
1535 // Return the index to use for a nested function.
1537 next_nested_function_index()
1539 ++this->nested_functions_
;
1540 return this->nested_functions_
;
1543 // Whether this method should not be included in the type
1546 nointerface() const;
1548 // Record that this method should not be included in the type
1553 // Record that this function is a stub method created for an unnamed
1556 set_is_unnamed_type_stub_method()
1558 go_assert(this->is_method());
1559 this->is_unnamed_type_stub_method_
= true;
1562 // Return the amount of enclosed variables in this closure.
1564 closure_field_count() const
1565 { return this->closure_fields_
.size(); }
1567 // Add a new field to the closure variable.
1569 add_closure_field(Named_object
* var
, Location loc
)
1570 { this->closure_fields_
.push_back(std::make_pair(var
, loc
)); }
1572 // Whether this function needs a closure.
1574 needs_closure() const
1575 { return !this->closure_fields_
.empty(); }
1577 // Return the closure variable, creating it if necessary. This is
1578 // passed to the function as a static chain parameter.
1582 // Set the closure variable. This is used when building thunks for
1583 // functions which call recover.
1585 set_closure_var(Named_object
* v
)
1587 go_assert(this->closure_var_
== NULL
);
1588 this->closure_var_
= v
;
1591 // Return the variable for a reference to field INDEX in the closure
1594 enclosing_var(unsigned int index
)
1596 go_assert(index
< this->closure_fields_
.size());
1597 return closure_fields_
[index
].first
;
1600 // Set the type of the closure variable if there is one.
1604 // Get the block of statements associated with the function.
1607 { return this->block_
; }
1609 // Get the location of the start of the function.
1612 { return this->location_
; }
1614 // Return whether this function is actually a method.
1618 // Add a label definition to the function.
1620 add_label_definition(Gogo
*, const std::string
& label_name
, Location
);
1622 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1623 // if we should report errors for a goto from the current location
1624 // to the label location.
1626 add_label_reference(Gogo
*, const std::string
& label_name
,
1627 Location
, bool issue_goto_errors
);
1629 // Warn about labels that are defined but not used.
1631 check_labels() const;
1633 // Note that a new local type has been added. Return its index.
1635 new_local_type_index()
1636 { return this->local_type_count_
++; }
1638 // Whether this function calls the predeclared recover function.
1640 calls_recover() const
1641 { return this->calls_recover_
; }
1643 // Record that this function calls the predeclared recover function.
1644 // This is set during the lowering pass.
1647 { this->calls_recover_
= true; }
1649 // Whether this is a recover thunk function.
1651 is_recover_thunk() const
1652 { return this->is_recover_thunk_
; }
1654 // Record that this is a thunk built for a function which calls
1657 set_is_recover_thunk()
1658 { this->is_recover_thunk_
= true; }
1660 // Whether this function already has a recover thunk.
1662 has_recover_thunk() const
1663 { return this->has_recover_thunk_
; }
1665 // Record that this function already has a recover thunk.
1667 set_has_recover_thunk()
1668 { this->has_recover_thunk_
= true; }
1670 // Record that this function is a thunk created for a defer
1671 // statement that calls the __go_set_defer_retaddr runtime function.
1673 set_calls_defer_retaddr()
1674 { this->calls_defer_retaddr_
= true; }
1676 // Whether this is a type hash or equality function created by the
1679 is_type_specific_function()
1680 { return this->is_type_specific_function_
; }
1682 // Record that this function is a type hash or equality function
1683 // created by the compiler.
1685 set_is_type_specific_function()
1686 { this->is_type_specific_function_
= true; }
1688 // Mark the function as going into a unique section.
1690 set_in_unique_section()
1691 { this->in_unique_section_
= true; }
1693 // Return whether this function should be exported for inlining.
1695 export_for_inlining() const
1696 { return this->export_for_inlining_
; }
1698 // Mark the function to be exported for inlining.
1700 set_export_for_inlining()
1701 { this->export_for_inlining_
= true; }
1703 // Return whether this function is inline only.
1705 is_inline_only() const
1706 { return this->is_inline_only_
; }
1708 // Mark the function as inline only: the body should not be emitted
1709 // if it is not inlined.
1711 set_is_inline_only()
1712 { this->is_inline_only_
= true; }
1714 // Report whether the function is referenced by an inline body.
1716 is_referenced_by_inline() const
1717 { return this->is_referenced_by_inline_
; }
1719 // Mark the function as referenced by an inline body.
1721 set_is_referenced_by_inline()
1722 { this->is_referenced_by_inline_
= true; }
1724 // Swap with another function. Used only for the thunk which calls
1727 swap_for_recover(Function
*);
1729 // Traverse the tree.
1731 traverse(Traverse
*);
1733 // Determine types in the function.
1737 // Return an expression for the function descriptor, given the named
1738 // object for this function. This may only be called for functions
1739 // without a closure. This will be an immutable struct with one
1740 // field that points to the function's code.
1742 descriptor(Gogo
*, Named_object
*);
1744 // Set the descriptor for this function. This is used when a
1745 // function declaration is followed by a function definition.
1747 set_descriptor(Expression
* descriptor
)
1749 go_assert(this->descriptor_
== NULL
);
1750 this->descriptor_
= descriptor
;
1753 // Return the backend representation.
1755 get_or_make_decl(Gogo
*, Named_object
*);
1757 // Return the function's decl after it has been built.
1761 // Set the function decl to hold a backend representation of the function
1764 build(Gogo
*, Named_object
*);
1766 // Get the statement that assigns values to this function's result struct.
1768 return_value(Gogo
*, Named_object
*, Location
) const;
1770 // Get the backend name of this function.
1772 backend_name(Gogo
*, Named_object
*, Backend_name
*);
1774 // Get an expression for the variable holding the defer stack.
1776 defer_stack(Location
);
1778 // Export the function.
1780 export_func(Export
*, const Named_object
*) const;
1782 // Export a function with a type.
1784 export_func_with_type(Export
*, const Named_object
*,
1785 const Function_type
*, Results
*, bool nointerface
,
1786 const std::string
& asm_name
, Block
* block
, Location
);
1788 // Import a function. Reports whether the import succeeded.
1790 import_func(Import
*, std::string
* pname
, Package
** pkg
,
1791 bool* is_exported
, Typed_identifier
** receiver
,
1792 Typed_identifier_list
** pparameters
,
1793 Typed_identifier_list
** presults
, bool* is_varargs
,
1794 bool* nointerface
, std::string
* asm_name
, std::string
* body
);
1797 // Type for mapping from label names to Label objects.
1798 typedef Unordered_map(std::string
, Label
*) Labels
;
1801 build_defer_wrapper(Gogo
*, Named_object
*, Bstatement
**, Bstatement
**);
1803 typedef std::vector
<std::pair
<Named_object
*,
1804 Location
> > Closure_fields
;
1806 // The function's type.
1807 Function_type
* type_
;
1808 // The enclosing function. This is NULL when there isn't one, which
1809 // is the normal case.
1810 Named_object
* enclosing_
;
1811 // The result variables, if any.
1813 // If there is a closure, this is the list of variables which appear
1814 // in the closure. This is created by the parser, and then resolved
1815 // to a real type when we lower parse trees.
1816 Closure_fields closure_fields_
;
1817 // The closure variable, passed as a parameter using the static
1818 // chain parameter. Normally NULL.
1819 Named_object
* closure_var_
;
1820 // The outer block of statements in the function.
1822 // The source location of the start of the function.
1824 // Labels defined or referenced in the function.
1826 // The number of local types defined in this function.
1827 unsigned int local_type_count_
;
1828 // The assembler name: this is the name that will be put in the object file.
1829 // Set by the go:linkname compiler directive. This is normally empty.
1830 std::string asm_name_
;
1831 // The function descriptor, if any.
1832 Expression
* descriptor_
;
1833 // The function decl.
1835 // The defer stack variable. A pointer to this variable is used to
1836 // distinguish the defer stack for one function from another. This
1837 // is NULL unless we actually need a defer stack.
1838 Temporary_statement
* defer_stack_
;
1839 // Pragmas for this function. This is a set of GOPRAGMA bits.
1840 unsigned int pragmas_
;
1841 // Number of nested functions defined within this function.
1842 unsigned int nested_functions_
;
1843 // True if this function is sink-named. No code is generated.
1845 // True if the result variables are named.
1846 bool results_are_named_
: 1;
1847 // True if this function is a stub method created for an unnamed
1849 bool is_unnamed_type_stub_method_
: 1;
1850 // True if this function calls the predeclared recover function.
1851 bool calls_recover_
: 1;
1852 // True if this a thunk built for a function which calls recover.
1853 bool is_recover_thunk_
: 1;
1854 // True if this function already has a recover thunk.
1855 bool has_recover_thunk_
: 1;
1856 // True if this is a thunk built for a defer statement that calls
1857 // the __go_set_defer_retaddr runtime function.
1858 bool calls_defer_retaddr_
: 1;
1859 // True if this is a function built by the compiler to as a hash or
1860 // equality function for some type.
1861 bool is_type_specific_function_
: 1;
1862 // True if this function should be put in a unique section. This is
1863 // turned on for field tracking.
1864 bool in_unique_section_
: 1;
1865 // True if we should export the body of this function for
1866 // cross-package inlining.
1867 bool export_for_inlining_
: 1;
1868 // True if this function is inline only: if it should not be emitted
1869 // if it is not inlined.
1870 bool is_inline_only_
: 1;
1871 // True if this function is referenced from an inlined body that
1872 // will be put into the export data.
1873 bool is_referenced_by_inline_
: 1;
1874 // True if we should make this function visible to other packages
1875 // because of a go:linkname directive.
1876 bool is_exported_by_linkname_
: 1;
1879 // A snapshot of the current binding state.
1881 class Bindings_snapshot
1884 Bindings_snapshot(const Block
*, Location
);
1886 // Report any errors appropriate for a goto from the current binding
1887 // state of B to this one.
1889 check_goto_from(const Block
* b
, Location
);
1891 // Report any errors appropriate for a goto from this binding state
1892 // to the current state of B.
1894 check_goto_to(const Block
* b
);
1898 check_goto_block(Location
, const Block
*, const Block
*, size_t*);
1901 check_goto_defs(Location
, const Block
*, size_t, size_t);
1903 // The current block.
1904 const Block
* block_
;
1905 // The number of names currently defined in each open block.
1906 // Element 0 is this->block_, element 1 is
1907 // this->block_->enclosing(), etc.
1908 std::vector
<size_t> counts_
;
1909 // The location where this snapshot was taken.
1913 // A function declaration.
1915 class Function_declaration
1918 Function_declaration(Function_type
* fntype
, Location location
)
1919 : fntype_(fntype
), location_(location
), asm_name_(), descriptor_(NULL
),
1920 fndecl_(NULL
), pragmas_(0), imported_body_(),
1921 is_on_inlinable_list_(false)
1926 { return this->fntype_
; }
1930 { return this->location_
; }
1932 // Return whether this function declaration is a method.
1938 { return this->asm_name_
; }
1940 // Set the assembler name.
1942 set_asm_name(const std::string
& asm_name
)
1943 { this->asm_name_
= asm_name
; }
1945 // Return the pragmas for this function.
1948 { return this->pragmas_
; }
1950 // Set the pragmas for this function.
1952 set_pragmas(unsigned int pragmas
)
1954 this->pragmas_
= pragmas
;
1957 // Whether this method should not be included in the type
1960 nointerface() const;
1962 // Record that this method should not be included in the type
1967 // Whether we have an imported function body.
1969 has_imported_body() const
1970 { return !this->imported_body_
.empty(); }
1972 // Record the imported body of this function.
1974 set_imported_body(Import
* imp
, const std::string
& imported_body
)
1977 this->imported_body_
= imported_body
;
1980 // Whether this declaration is on the list of inlinable functions.
1982 is_on_inlinable_list() const
1983 { return this->is_on_inlinable_list_
; }
1985 // Set that this function is on the list of inlinable functions.
1987 set_is_on_inlinable_list()
1988 { this->is_on_inlinable_list_
= true; }
1990 // Import the function body, creating a function.
1992 import_function_body(Gogo
*, Named_object
*);
1994 // Return an expression for the function descriptor, given the named
1995 // object for this function. This may only be called for functions
1996 // without a closure. This will be an immutable struct with one
1997 // field that points to the function's code.
1999 descriptor(Gogo
*, Named_object
*);
2001 // Return true if we have created a descriptor for this declaration.
2003 has_descriptor() const
2004 { return this->descriptor_
!= NULL
; }
2006 // Return a backend representation.
2008 get_or_make_decl(Gogo
*, Named_object
*);
2010 // If there is a descriptor, build it into the backend
2013 build_backend_descriptor(Gogo
*);
2015 // Get the backend name of this function declaration.
2017 backend_name(Gogo
*, Named_object
*, Backend_name
*);
2019 // Export a function declaration.
2021 export_func(Export
* exp
, const Named_object
* no
) const
2023 Function::export_func_with_type(exp
, no
, this->fntype_
, NULL
,
2024 this->is_method() && this->nointerface(),
2025 this->asm_name_
, NULL
, this->location_
);
2028 // Check that the types used in this declaration's signature are defined.
2030 check_types() const;
2033 // The type of the function.
2034 Function_type
* fntype_
;
2035 // The location of the declaration.
2037 // The assembler name: this is the name to use in references to the
2038 // function. This is normally empty.
2039 std::string asm_name_
;
2040 // The function descriptor, if any.
2041 Expression
* descriptor_
;
2042 // The function decl if needed.
2044 // Pragmas for this function. This is a set of GOPRAGMA bits.
2045 unsigned int pragmas_
;
2046 // Importer for function body if imported from a different package.
2048 // Export data for function body if imported from a different package.
2049 std::string imported_body_
;
2050 // Whether this declaration is already on the list of inlinable functions.
2051 bool is_on_inlinable_list_
;
2059 Variable(Type
*, Expression
*, bool is_global
, bool is_parameter
,
2060 bool is_receiver
, Location
);
2062 // Get the type of the variable.
2069 // Return whether the type is defined yet.
2073 // Get the initial value.
2076 { return this->init_
; }
2078 // Return whether there are any preinit statements.
2080 has_pre_init() const
2081 { return this->preinit_
!= NULL
; }
2083 // Return the preinit statements if any.
2086 { return this->preinit_
; }
2088 // Return whether this is a global variable.
2091 { return this->is_global_
; }
2093 // Return whether this is a function parameter.
2095 is_parameter() const
2096 { return this->is_parameter_
; }
2098 // Return whether this is a closure (static chain) parameter.
2101 { return this->is_closure_
; }
2103 // Change this parameter to be a closure.
2107 this->is_closure_
= true;
2110 // Return whether this is the receiver parameter of a method.
2113 { return this->is_receiver_
; }
2115 // Change this parameter to be a receiver. This is used when
2116 // creating the thunks created for functions which call recover.
2120 go_assert(this->is_parameter_
);
2121 this->is_receiver_
= true;
2124 // Change this parameter to not be a receiver. This is used when
2125 // creating the thunks created for functions which call recover.
2127 set_is_not_receiver()
2129 go_assert(this->is_parameter_
);
2130 this->is_receiver_
= false;
2133 // Return whether this is the varargs parameter of a function.
2135 is_varargs_parameter() const
2136 { return this->is_varargs_parameter_
; }
2138 // Return whether this is a global sink variable, created only to
2139 // run an initializer.
2141 is_global_sink() const
2142 { return this->is_global_sink_
; }
2144 // Record that this is a global sink variable.
2146 set_is_global_sink()
2148 go_assert(this->is_global_
);
2149 this->is_global_sink_
= true;
2152 // Whether this variable's address is taken.
2154 is_address_taken() const
2155 { return this->is_address_taken_
; }
2157 // Whether this variable should live in the heap.
2160 { return this->is_address_taken_
&& !this->is_global_
; }
2162 // Note that something takes the address of this variable.
2165 { this->is_address_taken_
= true; }
2167 // Return whether the address is taken but does not escape.
2169 is_non_escaping_address_taken() const
2170 { return this->is_non_escaping_address_taken_
; }
2172 // Note that something takes the address of this variable such that
2173 // the address does not escape the function.
2175 set_non_escaping_address_taken()
2176 { this->is_non_escaping_address_taken_
= true; }
2178 // Get the source location of the variable's declaration.
2181 { return this->location_
; }
2183 // Record that this is the varargs parameter of a function.
2185 set_is_varargs_parameter()
2187 go_assert(this->is_parameter_
);
2188 this->is_varargs_parameter_
= true;
2191 // Return whether the variable has been used.
2194 { return this->is_used_
; }
2196 // Mark that the variable has been used.
2199 { this->is_used_
= true; }
2201 // Clear the initial value; used for error handling and write barriers.
2204 { this->init_
= NULL
; }
2206 // Set the initial value; used for converting shortcuts.
2208 set_init(Expression
* init
)
2209 { this->init_
= init
; }
2211 // Get the preinit block, a block of statements to be run before the
2212 // initialization expression.
2214 preinit_block(Gogo
*);
2216 // Add a statement to be run before the initialization expression.
2217 // This is only used for global variables.
2219 add_preinit_statement(Gogo
*, Statement
*);
2221 // Lower the initialization expression after parsing is complete.
2223 lower_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
2225 // Flatten the initialization expression after ordering evaluations.
2227 flatten_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
2229 // A special case: the init value is used only to determine the
2230 // type. This is used if the variable is defined using := with the
2231 // comma-ok form of a map index or a receive expression. The init
2232 // value is actually the map index expression or receive expression.
2233 // We use this because we may not know the right type at parse time.
2235 set_type_from_init_tuple()
2236 { this->type_from_init_tuple_
= true; }
2238 // Another special case: the init value is used only to determine
2239 // the type. This is used if the variable is defined using := with
2240 // a range clause. The init value is the range expression. The
2241 // type of the variable is the index type of the range expression
2242 // (i.e., the first value returned by a range).
2244 set_type_from_range_index()
2245 { this->type_from_range_index_
= true; }
2247 // Another special case: like set_type_from_range_index, but the
2248 // type is the value type of the range expression (i.e., the second
2249 // value returned by a range).
2251 set_type_from_range_value()
2252 { this->type_from_range_value_
= true; }
2254 // Another special case: the init value is used only to determine
2255 // the type. This is used if the variable is defined using := with
2256 // a case in a select statement. The init value is the channel.
2257 // The type of the variable is the channel's element type.
2259 set_type_from_chan_element()
2260 { this->type_from_chan_element_
= true; }
2262 // After we lower the select statement, we once again set the type
2263 // from the initialization expression.
2265 clear_type_from_chan_element()
2267 go_assert(this->type_from_chan_element_
);
2268 this->type_from_chan_element_
= false;
2271 // TRUE if this variable was created for a type switch clause.
2273 is_type_switch_var() const
2274 { return this->is_type_switch_var_
; }
2276 // Note that this variable was created for a type switch clause.
2278 set_is_type_switch_var()
2279 { this->is_type_switch_var_
= true; }
2281 // Mark the variable as going into a unique section.
2283 set_in_unique_section()
2285 go_assert(this->is_global_
);
2286 this->in_unique_section_
= true;
2289 // Mark the variable as referenced by an inline body.
2291 set_is_referenced_by_inline()
2293 go_assert(this->is_global_
);
2294 this->is_referenced_by_inline_
= true;
2297 // Attach any go:embed comments for this variable.
2299 set_embeds(std::vector
<std::string
>* embeds
)
2301 go_assert(this->is_global_
2302 && this->init_
== NULL
2303 && this->preinit_
== NULL
);
2304 this->embeds_
= embeds
;
2307 // Return the top-level declaration for this variable.
2310 { return this->toplevel_decl_
; }
2312 // Set the top-level declaration for this variable. Only used for local
2315 set_toplevel_decl(Statement
* s
)
2317 go_assert(!this->is_global_
&& !this->is_parameter_
&& !this->is_receiver_
);
2318 this->toplevel_decl_
= s
;
2321 // Traverse the initializer expression.
2323 traverse_expression(Traverse
*, unsigned int traverse_mask
);
2325 // Determine the type of the variable if necessary.
2329 // Get the backend representation of the variable.
2331 get_backend_variable(Gogo
*, Named_object
*, const Package
*,
2332 const std::string
&);
2334 // Get the initial value of the variable. This may only
2335 // be called if has_pre_init() returns false.
2337 get_init(Gogo
*, Named_object
* function
);
2339 // Return a series of statements which sets the value of the
2340 // variable in DECL. This should only be called is has_pre_init()
2341 // returns true. DECL may be NULL for a sink variable.
2343 get_init_block(Gogo
*, Named_object
* function
, Bvariable
* decl
);
2345 // Export the variable.
2347 export_var(Export
*, const Named_object
*) const;
2349 // Import a variable. Reports whether the import succeeded.
2351 import_var(Import
*, std::string
* pname
, Package
** pkg
, bool* is_exported
,
2355 // The type of a tuple.
2357 type_from_tuple(Expression
*, bool) const;
2359 // The type of a range.
2361 type_from_range(Expression
*, bool, bool) const;
2363 // The element type of a channel.
2365 type_from_chan_element(Expression
*, bool) const;
2367 // The variable's type. This may be NULL if the type is set from
2370 // The initial value. This may be NULL if the variable should be
2371 // initialized to the default value for the type.
2373 // Statements to run before the init statement.
2375 // Location of variable definition.
2377 // Any associated go:embed comments.
2378 std::vector
<std::string
>* embeds_
;
2379 // Backend representation.
2380 Bvariable
* backend_
;
2381 // Whether this is a global variable.
2382 bool is_global_
: 1;
2383 // Whether this is a function parameter.
2384 bool is_parameter_
: 1;
2385 // Whether this is a closure parameter.
2386 bool is_closure_
: 1;
2387 // Whether this is the receiver parameter of a method.
2388 bool is_receiver_
: 1;
2389 // Whether this is the varargs parameter of a function.
2390 bool is_varargs_parameter_
: 1;
2391 // Whether this is a global sink variable created to run an
2393 bool is_global_sink_
: 1;
2394 // Whether this variable is ever referenced.
2396 // Whether something takes the address of this variable. For a
2397 // local variable this implies that the variable has to be on the
2398 // heap if it escapes from its function.
2399 bool is_address_taken_
: 1;
2400 // Whether something takes the address of this variable such that
2401 // the address does not escape the function.
2402 bool is_non_escaping_address_taken_
: 1;
2403 // True if we have seen this variable in a traversal.
2405 // True if we have lowered the initialization expression.
2406 bool init_is_lowered_
: 1;
2407 // True if we have flattened the initialization expression.
2408 bool init_is_flattened_
: 1;
2409 // True if init is a tuple used to set the type.
2410 bool type_from_init_tuple_
: 1;
2411 // True if init is a range clause and the type is the index type.
2412 bool type_from_range_index_
: 1;
2413 // True if init is a range clause and the type is the value type.
2414 bool type_from_range_value_
: 1;
2415 // True if init is a channel and the type is the channel's element type.
2416 bool type_from_chan_element_
: 1;
2417 // True if this is a variable created for a type switch case.
2418 bool is_type_switch_var_
: 1;
2419 // True if we have determined types.
2420 bool determined_type_
: 1;
2421 // True if this variable should be put in a unique section. This is
2422 // used for field tracking.
2423 bool in_unique_section_
: 1;
2424 // True if this variable is referenced from an inlined body that
2425 // will be put into the export data.
2426 bool is_referenced_by_inline_
: 1;
2427 // The top-level declaration for this variable. Only used for local
2428 // variables. Must be a Temporary_statement if not NULL.
2429 Statement
* toplevel_decl_
;
2432 // A variable which is really the name for a function return value, or
2435 class Result_variable
2438 Result_variable(Type
* type
, Function
* function
, int index
,
2440 : type_(type
), function_(function
), index_(index
), location_(location
),
2441 backend_(NULL
), is_address_taken_(false),
2442 is_non_escaping_address_taken_(false)
2445 // Get the type of the result variable.
2448 { return this->type_
; }
2450 // Get the function that this is associated with.
2453 { return this->function_
; }
2455 // Index in the list of function results.
2458 { return this->index_
; }
2460 // The location of the variable definition.
2463 { return this->location_
; }
2465 // Whether this variable's address is taken.
2467 is_address_taken() const
2468 { return this->is_address_taken_
; }
2470 // Note that something takes the address of this variable.
2473 { this->is_address_taken_
= true; }
2475 // Return whether the address is taken but does not escape.
2477 is_non_escaping_address_taken() const
2478 { return this->is_non_escaping_address_taken_
; }
2480 // Note that something takes the address of this variable such that
2481 // the address does not escape the function.
2483 set_non_escaping_address_taken()
2484 { this->is_non_escaping_address_taken_
= true; }
2486 // Whether this variable should live in the heap.
2489 { return this->is_address_taken_
; }
2491 // Set the function. This is used when cloning functions which call
2494 set_function(Function
* function
)
2495 { this->function_
= function
; }
2497 // Get the backend representation of the variable.
2499 get_backend_variable(Gogo
*, Named_object
*, const std::string
&);
2502 // Type of result variable.
2504 // Function with which this is associated.
2505 Function
* function_
;
2506 // Index in list of results.
2508 // Where the result variable is defined.
2510 // Backend representation.
2511 Bvariable
* backend_
;
2512 // Whether something takes the address of this variable.
2513 bool is_address_taken_
;
2514 // Whether something takes the address of this variable such that
2515 // the address does not escape the function.
2516 bool is_non_escaping_address_taken_
;
2519 // The value we keep for a named constant. This lets us hold a type
2520 // and an expression.
2522 class Named_constant
2525 Named_constant(Type
* type
, Expression
* expr
, int iota_value
,
2527 : type_(type
), expr_(expr
), iota_value_(iota_value
), location_(location
),
2528 lowering_(false), is_sink_(false), bconst_(NULL
)
2533 { return this->type_
; }
2540 { return this->expr_
; }
2544 { return this->iota_value_
; }
2548 { return this->location_
; }
2550 // Whether we are lowering.
2553 { return this->lowering_
; }
2555 // Set that we are lowering.
2558 { this->lowering_
= true; }
2560 // We are no longer lowering.
2563 { this->lowering_
= false; }
2567 { return this->is_sink_
; }
2571 { this->is_sink_
= true; }
2573 // Traverse the expression.
2575 traverse_expression(Traverse
*);
2577 // Determine the type of the constant if necessary.
2581 // Indicate that we found and reported an error for this constant.
2585 // Export the constant.
2587 export_const(Export
*, const std::string
& name
) const;
2589 // Import a constant.
2591 import_const(Import
*, std::string
*, Type
**, Expression
**);
2593 // Get the backend representation of the constant value.
2595 get_backend(Gogo
*, Named_object
*);
2598 // The type of the constant.
2600 // The expression for the constant.
2602 // If the predeclared constant iota is used in EXPR_, this is the
2603 // value it will have. We do this because at parse time we don't
2604 // know whether the name "iota" will refer to the predeclared
2605 // constant or to something else. We put in the right value in when
2608 // The location of the definition.
2610 // Whether we are currently lowering this constant.
2612 // Whether this constant is blank named and needs only type checking.
2614 // The backend representation of the constant value.
2615 Bexpression
* bconst_
;
2618 // A type declaration.
2620 class Type_declaration
2623 Type_declaration(Location location
)
2624 : location_(location
), in_function_(NULL
), in_function_index_(0),
2625 methods_(), issued_warning_(false)
2628 // Return the location.
2631 { return this->location_
; }
2633 // Return the function in which this type is declared. This will
2634 // return NULL for a type declared in global scope.
2636 in_function(unsigned int* pindex
)
2638 *pindex
= this->in_function_index_
;
2639 return this->in_function_
;
2642 // Set the function in which this type is declared.
2644 set_in_function(Named_object
* f
, unsigned int index
)
2646 this->in_function_
= f
;
2647 this->in_function_index_
= index
;
2650 // Add a method to this type. This is used when methods are defined
2653 add_method(const std::string
& name
, Function
* function
);
2655 // Add a method declaration to this type.
2657 add_method_declaration(const std::string
& name
, Package
*,
2658 Function_type
* type
, Location location
);
2660 // Add an already created object as a method.
2662 add_existing_method(Named_object
* no
)
2663 { this->methods_
.push_back(no
); }
2665 // Return whether any methods were defined.
2667 has_methods() const;
2669 // Return the methods.
2670 const std::vector
<Named_object
*>*
2672 { return &this->methods_
; }
2674 // Define methods when the real type is known.
2676 define_methods(Named_type
*);
2678 // This is called if we are trying to use this type. It returns
2679 // true if we should issue a warning.
2684 // The location of the type declaration.
2686 // If this type is declared in a function, a pointer back to the
2687 // function in which it is defined.
2688 Named_object
* in_function_
;
2689 // The index of this type in IN_FUNCTION_.
2690 unsigned int in_function_index_
;
2691 // Methods defined before the type is defined.
2692 std::vector
<Named_object
*> methods_
;
2693 // True if we have issued a warning about a use of this type
2694 // declaration when it is undefined.
2695 bool issued_warning_
;
2698 // An unknown object. These are created by the parser for forward
2699 // references to names which have not been seen before. In a correct
2700 // program, these will always point to a real definition by the end of
2701 // the parse. Because they point to another Named_object, these may
2702 // only be referenced by Unknown_expression objects.
2707 Unknown_name(Location location
)
2708 : location_(location
), real_named_object_(NULL
)
2711 // Return the location where this name was first seen.
2714 { return this->location_
; }
2716 // Return the real named object that this points to, or NULL if it
2717 // was never resolved.
2719 real_named_object() const
2720 { return this->real_named_object_
; }
2722 // Set the real named object that this points to.
2724 set_real_named_object(Named_object
* no
);
2727 // The location where this name was first seen.
2729 // The real named object when it is known.
2734 // A named object named. This is the result of a declaration. We
2735 // don't use a superclass because they all have to be handled
2743 // An uninitialized Named_object. We should never see this.
2744 NAMED_OBJECT_UNINITIALIZED
,
2745 // An erroneous name. This indicates a parse error, to avoid
2746 // later errors about undefined references.
2747 NAMED_OBJECT_ERRONEOUS
,
2748 // An unknown name. This is used for forward references. In a
2749 // correct program, these will all be resolved by the end of the
2751 NAMED_OBJECT_UNKNOWN
,
2756 // A forward type declaration.
2757 NAMED_OBJECT_TYPE_DECLARATION
,
2760 // A result variable in a function.
2761 NAMED_OBJECT_RESULT_VAR
,
2762 // The blank identifier--the special variable named _.
2766 // A forward func declaration.
2767 NAMED_OBJECT_FUNC_DECLARATION
,
2769 NAMED_OBJECT_PACKAGE
2772 // Return the classification.
2774 classification() const
2775 { return this->classification_
; }
2780 is_erroneous() const
2781 { return this->classification_
== NAMED_OBJECT_ERRONEOUS
; }
2785 { return this->classification_
== NAMED_OBJECT_UNKNOWN
; }
2789 { return this->classification_
== NAMED_OBJECT_CONST
; }
2793 { return this->classification_
== NAMED_OBJECT_TYPE
; }
2796 is_type_declaration() const
2797 { return this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
; }
2801 { return this->classification_
== NAMED_OBJECT_VAR
; }
2804 is_result_variable() const
2805 { return this->classification_
== NAMED_OBJECT_RESULT_VAR
; }
2809 { return this->classification_
== NAMED_OBJECT_SINK
; }
2813 { return this->classification_
== NAMED_OBJECT_FUNC
; }
2816 is_function_declaration() const
2817 { return this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
; }
2821 { return this->classification_
== NAMED_OBJECT_PACKAGE
; }
2825 static Named_object
*
2826 make_erroneous_name(const std::string
& name
)
2827 { return new Named_object(name
, NULL
, NAMED_OBJECT_ERRONEOUS
); }
2829 static Named_object
*
2830 make_unknown_name(const std::string
& name
, Location
);
2832 static Named_object
*
2833 make_constant(const Typed_identifier
&, const Package
*, Expression
*,
2836 static Named_object
*
2837 make_type(const std::string
&, const Package
*, Type
*, Location
);
2839 static Named_object
*
2840 make_type_declaration(const std::string
&, const Package
*, Location
);
2842 static Named_object
*
2843 make_variable(const std::string
&, const Package
*, Variable
*);
2845 static Named_object
*
2846 make_result_variable(const std::string
&, Result_variable
*);
2848 static Named_object
*
2851 static Named_object
*
2852 make_function(const std::string
&, const Package
*, Function
*);
2854 static Named_object
*
2855 make_function_declaration(const std::string
&, const Package
*, Function_type
*,
2858 static Named_object
*
2859 make_package(const std::string
& alias
, Package
* package
);
2866 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2867 return this->u_
.unknown_value
;
2871 unknown_value() const
2873 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2874 return this->u_
.unknown_value
;
2880 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2881 return this->u_
.const_value
;
2884 const Named_constant
*
2887 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2888 return this->u_
.const_value
;
2894 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2895 return this->u_
.type_value
;
2901 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2902 return this->u_
.type_value
;
2906 type_declaration_value()
2908 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2909 return this->u_
.type_declaration
;
2912 const Type_declaration
*
2913 type_declaration_value() const
2915 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2916 return this->u_
.type_declaration
;
2922 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2923 return this->u_
.var_value
;
2929 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2930 return this->u_
.var_value
;
2936 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2937 return this->u_
.result_var_value
;
2940 const Result_variable
*
2941 result_var_value() const
2943 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2944 return this->u_
.result_var_value
;
2950 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2951 return this->u_
.func_value
;
2957 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2958 return this->u_
.func_value
;
2961 Function_declaration
*
2962 func_declaration_value()
2964 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2965 return this->u_
.func_declaration_value
;
2968 const Function_declaration
*
2969 func_declaration_value() const
2971 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2972 return this->u_
.func_declaration_value
;
2978 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2979 return this->u_
.package_value
;
2983 package_value() const
2985 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2986 return this->u_
.package_value
;
2991 { return this->name_
; }
2993 // Return the name to use in an error message. The difference is
2994 // that if this Named_object is defined in a different package, this
2995 // will return PACKAGE.NAME.
2997 message_name() const;
3001 { return this->package_
; }
3003 // Resolve an unknown value if possible. This returns the same
3004 // Named_object or a new one.
3008 Named_object
* ret
= this;
3009 if (this->is_unknown())
3011 Named_object
* r
= this->unknown_value()->real_named_object();
3021 const Named_object
* ret
= this;
3022 if (this->is_unknown())
3024 const Named_object
* r
= this->unknown_value()->real_named_object();
3031 // The location where this object was defined or referenced.
3035 // Convert a variable to the backend representation.
3037 get_backend_variable(Gogo
*, Named_object
* function
);
3039 // Get the backend representation of this object.
3041 get_backend(Gogo
*, std::vector
<Bexpression
*>&, std::vector
<Btype
*>&,
3042 std::vector
<Bfunction
*>&);
3044 // Define a type declaration.
3046 set_type_value(Named_type
*);
3048 // Define a function declaration.
3050 set_function_value(Function
*);
3052 // Declare an unknown name as a type declaration.
3056 // Export this object.
3058 export_named_object(Export
*) const;
3060 // Mark this named object as an invalid redefinition of another object.
3062 set_is_redefinition()
3063 { this->is_redefinition_
= true; }
3065 // Return whether or not this object is a invalid redefinition of another
3068 is_redefinition() const
3069 { return this->is_redefinition_
; }
3072 Named_object(const std::string
&, const Package
*, Classification
);
3074 // The name of the object.
3076 // The package that this object is in. This is NULL if it is in the
3077 // file we are compiling.
3078 const Package
* package_
;
3079 // The type of object this is.
3080 Classification classification_
;
3084 Unknown_name
* unknown_value
;
3085 Named_constant
* const_value
;
3086 Named_type
* type_value
;
3087 Type_declaration
* type_declaration
;
3088 Variable
* var_value
;
3089 Result_variable
* result_var_value
;
3090 Function
* func_value
;
3091 Function_declaration
* func_declaration_value
;
3092 Package
* package_value
;
3094 // True if this object is an invalid redefinition of another object.
3095 bool is_redefinition_
;
3098 // A binding contour. This binds names to objects.
3103 // Type for mapping from names to objects.
3104 typedef Unordered_map(std::string
, Named_object
*) Contour
;
3106 Bindings(Bindings
* enclosing
);
3108 // Add an erroneous name.
3110 add_erroneous_name(const std::string
& name
)
3111 { return this->add_named_object(Named_object::make_erroneous_name(name
)); }
3113 // Add an unknown name.
3115 add_unknown_name(const std::string
& name
, Location location
)
3117 return this->add_named_object(Named_object::make_unknown_name(name
,
3123 add_constant(const Typed_identifier
& tid
, const Package
* package
,
3124 Expression
* expr
, int iota_value
)
3126 return this->add_named_object(Named_object::make_constant(tid
, package
,
3133 add_type(const std::string
& name
, const Package
* package
, Type
* type
,
3136 return this->add_named_object(Named_object::make_type(name
, package
, type
,
3140 // Add a named type. This is used for builtin types, and to add an
3141 // imported type to the global scope.
3143 add_named_type(Named_type
* named_type
);
3145 // Add a type declaration.
3147 add_type_declaration(const std::string
& name
, const Package
* package
,
3150 Named_object
* no
= Named_object::make_type_declaration(name
, package
,
3152 return this->add_named_object(no
);
3157 add_variable(const std::string
& name
, const Package
* package
,
3160 return this->add_named_object(Named_object::make_variable(name
, package
,
3164 // Add a result variable.
3166 add_result_variable(const std::string
& name
, Result_variable
* result
)
3168 return this->add_named_object(Named_object::make_result_variable(name
,
3174 add_function(const std::string
& name
, const Package
*, Function
* function
);
3176 // Add a function declaration.
3178 add_function_declaration(const std::string
& name
, const Package
* package
,
3179 Function_type
* type
, Location location
);
3181 // Add a package. The location is the location of the import
3184 add_package(const std::string
& alias
, Package
* package
)
3186 Named_object
* no
= Named_object::make_package(alias
, package
);
3187 return this->add_named_object(no
);
3190 // Define a type which was already declared.
3192 define_type(Named_object
*, Named_type
*);
3194 // Add a method to the list of objects. This is not added to the
3197 add_method(Named_object
*);
3199 // Add a named object to this binding.
3201 add_named_object(Named_object
* no
)
3202 { return this->add_named_object_to_contour(&this->bindings_
, no
); }
3204 // Clear all names in file scope from the bindings.
3206 clear_file_scope(Gogo
*);
3208 // Look up a name in this binding contour and in any enclosing
3209 // binding contours. This returns NULL if the name is not found.
3211 lookup(const std::string
&) const;
3213 // Look up a name in this binding contour without looking in any
3214 // enclosing binding contours. Returns NULL if the name is not found.
3216 lookup_local(const std::string
&) const;
3220 remove_binding(Named_object
*);
3222 // Mark all variables as used. This is used for some types of parse
3227 // Traverse the tree. See the Traverse class.
3229 traverse(Traverse
*, bool is_global
);
3231 // Iterate over definitions. This does not include things which
3232 // were only declared.
3234 typedef std::vector
<Named_object
*>::const_iterator
3235 const_definitions_iterator
;
3237 const_definitions_iterator
3238 begin_definitions() const
3239 { return this->named_objects_
.begin(); }
3241 const_definitions_iterator
3242 end_definitions() const
3243 { return this->named_objects_
.end(); }
3245 // Return the number of definitions.
3247 size_definitions() const
3248 { return this->named_objects_
.size(); }
3250 // Return whether there are no definitions.
3252 empty_definitions() const
3253 { return this->named_objects_
.empty(); }
3255 // Iterate over declarations. This is everything that has been
3256 // declared, which includes everything which has been defined.
3258 typedef Contour::const_iterator const_declarations_iterator
;
3260 const_declarations_iterator
3261 begin_declarations() const
3262 { return this->bindings_
.begin(); }
3264 const_declarations_iterator
3265 end_declarations() const
3266 { return this->bindings_
.end(); }
3268 // Return the number of declarations.
3270 size_declarations() const
3271 { return this->bindings_
.size(); }
3273 // Return whether there are no declarations.
3275 empty_declarations() const
3276 { return this->bindings_
.empty(); }
3278 // Return the first declaration.
3281 { return this->bindings_
.empty() ? NULL
: this->bindings_
.begin()->second
; }
3283 // Dump to stderr for debugging
3288 add_named_object_to_contour(Contour
*, Named_object
*);
3291 new_definition(Named_object
*, Named_object
*);
3293 // Enclosing bindings.
3294 Bindings
* enclosing_
;
3295 // The list of objects.
3296 std::vector
<Named_object
*> named_objects_
;
3297 // The mapping from names to objects.
3306 Label(const std::string
& name
)
3307 : name_(name
), location_(Linemap::unknown_location()), snapshot_(NULL
),
3308 refs_(), is_used_(false), blabel_(NULL
), depth_(DEPTH_UNKNOWN
)
3311 // Return the label's name.
3314 { return this->name_
; }
3316 // Return whether the label has been defined.
3319 { return !Linemap::is_unknown_location(this->location_
); }
3321 // Return whether the label has been used.
3324 { return this->is_used_
; }
3326 // Record that the label is used.
3329 { this->is_used_
= true; }
3331 // Return whether this label is looping.
3334 { return this->depth_
== DEPTH_LOOPING
; }
3336 // Set this label as looping.
3339 { this->depth_
= DEPTH_LOOPING
; }
3341 // Return whether this label is nonlooping.
3344 { return this->depth_
== DEPTH_NONLOOPING
; }
3346 // Set this label as nonlooping.
3349 { this->depth_
= DEPTH_NONLOOPING
; }
3351 // Return the location of the definition.
3354 { return this->location_
; }
3356 // Return the bindings snapshot.
3359 { return this->snapshot_
; }
3361 // Add a snapshot of a goto which refers to this label.
3363 add_snapshot_ref(Bindings_snapshot
* snapshot
)
3365 go_assert(Linemap::is_unknown_location(this->location_
));
3366 this->refs_
.push_back(snapshot
);
3369 // Return the list of snapshots of goto statements which refer to
3371 const std::vector
<Bindings_snapshot
*>&
3373 { return this->refs_
; }
3375 // Clear the references.
3379 // Define the label at LOCATION with the given bindings snapshot.
3381 define(Location location
, Bindings_snapshot
* snapshot
)
3383 if (this->is_dummy_label())
3385 go_assert(Linemap::is_unknown_location(this->location_
)
3386 && this->snapshot_
== NULL
);
3387 this->location_
= location
;
3388 this->snapshot_
= snapshot
;
3391 // Return the backend representation for this label.
3393 get_backend_label(Translate_context
*);
3395 // Return an expression for the address of this label. This is used
3396 // to get the return address of a deferred function to see whether
3397 // the function may call recover.
3399 get_addr(Translate_context
*, Location location
);
3401 // Return a dummy label, representing any instance of the blank label.
3403 create_dummy_label();
3405 // Return TRUE if this is a dummy label.
3407 is_dummy_label() const
3408 { return this->name_
== "_"; }
3410 // A classification of a label's looping depth.
3414 // A label never jumped to.
3416 // A label jumped to.
3421 // The name of the label.
3423 // The location of the definition. This is 0 if the label has not
3424 // yet been defined.
3426 // A snapshot of the set of bindings defined at this label, used to
3427 // issue errors about invalid goto statements.
3428 Bindings_snapshot
* snapshot_
;
3429 // A list of snapshots of goto statements which refer to this label.
3430 std::vector
<Bindings_snapshot
*> refs_
;
3431 // Whether the label has been used.
3433 // The backend representation.
3435 // The looping depth of this label, for escape analysis.
3439 // An unnamed label. These are used when lowering loops.
3444 Unnamed_label(Location location
)
3445 : location_(location
), derived_from_(NULL
), blabel_(NULL
)
3448 // Get the location where the label is defined.
3451 { return this->location_
; }
3453 // Set the location where the label is defined.
3455 set_location(Location location
)
3456 { this->location_
= location
; }
3458 // Get the top level statement this unnamed label is derived from.
3460 derived_from() const
3461 { return this->derived_from_
; }
3463 // Set the top level statement this unnamed label is derived from.
3465 set_derived_from(Statement
* s
)
3466 { this->derived_from_
= s
; }
3468 // Return a statement which defines this label.
3470 get_definition(Translate_context
*);
3472 // Return a goto to this label from LOCATION.
3474 get_goto(Translate_context
*, Location location
);
3477 // Return the backend representation.
3479 get_blabel(Translate_context
*);
3481 // The location where the label is defined.
3483 // The top-level statement this unnamed label was derived/lowered from.
3484 // This is NULL is this label is not the top-level of a lowered statement.
3485 Statement
* derived_from_
;
3486 // The backend representation of this label.
3490 // An alias for an imported package.
3495 Package_alias(Location location
)
3496 : location_(location
), used_(0)
3499 // The location of the import statement.
3502 { return this->location_
; }
3504 // How many symbols from the package were used under this alias.
3507 { return this->used_
; }
3509 // Note that some symbol was used under this alias.
3515 // The location of the import statement.
3517 // The amount of times some name from this package was used under this alias.
3521 // An imported package.
3526 Package(const std::string
& pkgpath
, const std::string
& pkgpath_symbol
,
3529 // Get the package path used for all symbols exported from this
3533 { return this->pkgpath_
; }
3535 // Return the package path to use for a symbol name.
3537 pkgpath_symbol() const;
3539 // Set the package path symbol.
3541 set_pkgpath_symbol(const std::string
&);
3543 // Return the location of the most recent import statement.
3546 { return this->location_
; }
3548 // Return whether we know the name of this package yet.
3550 has_package_name() const
3551 { return !this->package_name_
.empty(); }
3553 // The name that this package uses in its package clause. This may
3554 // be different from the name in the associated Named_object if the
3555 // import statement used an alias.
3557 package_name() const
3559 go_assert(!this->package_name_
.empty());
3560 return this->package_name_
;
3563 // Return the bindings.
3566 { return this->bindings_
; }
3568 // Type used to map import names to package aliases.
3569 typedef std::map
<std::string
, Package_alias
*> Aliases
;
3571 // Return the set of package aliases.
3574 { return this->aliases_
; }
3576 // Note that some symbol from this package was used and qualified by ALIAS.
3577 // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3579 note_usage(const std::string
& alias
) const;
3581 // Note that USAGE might be a fake usage of this package.
3583 note_fake_usage(Expression
* usage
) const
3584 { this->fake_uses_
.insert(usage
); }
3586 // Forget a given USAGE of this package.
3588 forget_usage(Expression
* usage
) const;
3590 // Clear the used field for the next file.
3594 // Look up a name in the package. Returns NULL if the name is not
3597 lookup(const std::string
& name
) const
3598 { return this->bindings_
->lookup(name
); }
3600 // Set the name of the package.
3602 set_package_name(const std::string
& name
, Location
);
3604 // Set the location of the package. This is used to record the most
3605 // recent import location.
3607 set_location(Location location
)
3608 { this->location_
= location
; }
3610 // Add a package name as an ALIAS for this package.
3612 add_alias(const std::string
& alias
, Location
);
3614 // Add a constant to the package.
3616 add_constant(const Typed_identifier
& tid
, Expression
* expr
)
3617 { return this->bindings_
->add_constant(tid
, this, expr
, 0); }
3619 // Add a type to the package.
3621 add_type(const std::string
& name
, Type
* type
, Location location
)
3622 { return this->bindings_
->add_type(name
, this, type
, location
); }
3624 // Add a type declaration to the package.
3626 add_type_declaration(const std::string
& name
, Location location
)
3627 { return this->bindings_
->add_type_declaration(name
, this, location
); }
3629 // Add a variable to the package.
3631 add_variable(const std::string
& name
, Variable
* variable
)
3632 { return this->bindings_
->add_variable(name
, this, variable
); }
3634 // Add a function declaration to the package.
3636 add_function_declaration(const std::string
& name
, Function_type
* type
,
3638 { return this->bindings_
->add_function_declaration(name
, this, type
, loc
); }
3640 // Determine types of constants.
3645 // The package path for type reflection data.
3646 std::string pkgpath_
;
3647 // The package path for symbol names.
3648 std::string pkgpath_symbol_
;
3649 // The name that this package uses in the package clause. This may
3650 // be the empty string if it is not yet known.
3651 std::string package_name_
;
3652 // The names in this package.
3653 Bindings
* bindings_
;
3654 // The location of the most recent import statement.
3656 // The set of aliases associated with this package.
3658 // A set of possibly fake uses of this package. This is mutable because we
3659 // can track fake uses of a package even if we have a const pointer to it.
3660 mutable std::set
<Expression
*> fake_uses_
;
3663 // Return codes for the traversal functions. This is not an enum
3664 // because we want to be able to declare traversal functions in other
3665 // header files without including this one.
3667 // Continue traversal as usual.
3668 const int TRAVERSE_CONTINUE
= -1;
3671 const int TRAVERSE_EXIT
= 0;
3673 // Continue traversal, but skip components of the current object.
3674 // E.g., if this is returned by Traverse::statement, we do not
3675 // traverse the expressions in the statement even if
3676 // traverse_expressions is set in the traverse_mask.
3677 const int TRAVERSE_SKIP_COMPONENTS
= 1;
3679 // This class is used when traversing the parse tree. The caller uses
3680 // a subclass which overrides functions as desired.
3685 // These bitmasks say what to traverse.
3686 static const unsigned int traverse_variables
= 0x1;
3687 static const unsigned int traverse_constants
= 0x2;
3688 static const unsigned int traverse_functions
= 0x4;
3689 static const unsigned int traverse_blocks
= 0x8;
3690 static const unsigned int traverse_statements
= 0x10;
3691 static const unsigned int traverse_expressions
= 0x20;
3692 static const unsigned int traverse_types
= 0x40;
3693 static const unsigned int traverse_func_declarations
= 0x80;
3695 Traverse(unsigned int traverse_mask
)
3696 : traverse_mask_(traverse_mask
), types_seen_(NULL
), expressions_seen_(NULL
)
3699 virtual ~Traverse();
3701 // The bitmask of what to traverse.
3703 traverse_mask() const
3704 { return this->traverse_mask_
; }
3706 // Record that we are going to traverse a type. This returns true
3707 // if the type has already been seen in this traversal. This is
3708 // required because types, unlike expressions, can form a circular
3711 remember_type(const Type
*);
3713 // Record that we are going to see an expression. This returns true
3714 // if the expression has already been seen in this traversal. This
3715 // is only needed for cases where multiple expressions can point to
3718 remember_expression(const Expression
*);
3720 // These functions return one of the TRAVERSE codes defined above.
3722 // If traverse_variables is set in the mask, this is called for
3723 // every variable in the tree.
3725 variable(Named_object
*);
3727 // If traverse_constants is set in the mask, this is called for
3728 // every named constant in the tree. The bool parameter is true for
3729 // a global constant.
3731 constant(Named_object
*, bool);
3733 // If traverse_functions is set in the mask, this is called for
3734 // every function in the tree.
3736 function(Named_object
*);
3738 // If traverse_blocks is set in the mask, this is called for every
3739 // block in the tree.
3743 // If traverse_statements is set in the mask, this is called for
3744 // every statement in the tree.
3746 statement(Block
*, size_t* index
, Statement
*);
3748 // If traverse_expressions is set in the mask, this is called for
3749 // every expression in the tree.
3751 expression(Expression
**);
3753 // If traverse_types is set in the mask, this is called for every
3754 // type in the tree.
3758 // If traverse_func_declarations is set in the mask, this is called
3759 // for every function declarations in the tree.
3761 function_declaration(Named_object
*);
3764 // A hash table for types we have seen during this traversal. Note
3765 // that this uses the default hash functions for pointers rather
3766 // than Type_hash_identical and Type_identical. This is because for
3767 // traversal we care about seeing a specific type structure. If
3768 // there are two separate instances of identical types, we want to
3770 typedef Unordered_set(const Type
*) Types_seen
;
3772 typedef Unordered_set(const Expression
*) Expressions_seen
;
3774 // Bitmask of what sort of objects to traverse.
3775 unsigned int traverse_mask_
;
3776 // Types which have been seen in this traversal.
3777 Types_seen
* types_seen_
;
3778 // Expressions which have been seen in this traversal.
3779 Expressions_seen
* expressions_seen_
;
3782 // This class looks for interface types to finalize methods of inherited
3785 class Finalize_methods
: public Traverse
3788 Finalize_methods(Gogo
* gogo
)
3789 : Traverse(traverse_types
),
3800 // A class which makes it easier to insert new statements before the
3801 // current statement during a traversal.
3803 class Statement_inserter
3806 typedef Unordered_set(Statement
*) Statements
;
3808 // Empty constructor.
3809 Statement_inserter()
3810 : block_(NULL
), pindex_(NULL
), gogo_(NULL
), var_(NULL
),
3811 statements_added_(NULL
)
3814 // Constructor for a statement in a block.
3815 Statement_inserter(Block
* block
, size_t *pindex
, Statements
*added
= NULL
)
3816 : block_(block
), pindex_(pindex
), gogo_(NULL
), var_(NULL
),
3817 statements_added_(added
)
3820 // Constructor for a global variable.
3821 Statement_inserter(Gogo
* gogo
, Variable
* var
, Statements
*added
= NULL
)
3822 : block_(NULL
), pindex_(NULL
), gogo_(gogo
), var_(var
),
3823 statements_added_(added
)
3824 { go_assert(var
->is_global()); }
3826 // We use the default copy constructor and assignment operator.
3828 // Insert S before the statement we are traversing, or before the
3829 // initialization expression of a global variable.
3831 insert(Statement
* s
);
3834 // The block that the statement is in.
3836 // The index of the statement that we are traversing.
3838 // The IR, needed when looking at an initializer expression for a
3841 // The global variable, when looking at an initializer expression.
3843 // If non-null, a set to record new statements inserted (non-owned).
3844 Statements
* statements_added_
;
3847 // When translating the gogo IR into the backend data structure, this
3848 // is the context we pass down the blocks and statements.
3850 class Translate_context
3853 Translate_context(Gogo
* gogo
, Named_object
* function
, Block
* block
,
3855 : gogo_(gogo
), backend_(gogo
->backend()), function_(function
),
3856 block_(block
), bblock_(bblock
), is_const_(false)
3863 { return this->gogo_
; }
3867 { return this->backend_
; }
3871 { return this->function_
; }
3875 { return this->block_
; }
3879 { return this->bblock_
; }
3883 { return this->is_const_
; }
3885 // Make a constant context.
3888 { this->is_const_
= true; }
3891 // The IR for the entire compilation unit.
3893 // The generator for the backend data structures.
3895 // The function we are currently translating. NULL if not in a
3896 // function, e.g., the initializer of a global variable.
3897 Named_object
* function_
;
3898 // The block we are currently translating. NULL if not in a
3901 // The backend representation of the current block. NULL if block_
3904 // Whether this is being evaluated in a constant context. This is
3905 // used for type descriptor initializers.
3909 // This is used by some of the langhooks.
3910 extern Gogo
* go_get_gogo();
3912 // Whether we have seen any errors. FIXME: Replace with a backend
3914 extern bool saw_errors();
3916 // For use in the debugger
3917 extern void debug_go_gogo(Gogo
*);
3918 extern void debug_go_named_object(Named_object
*);
3919 extern void debug_go_bindings(Bindings
*);
3922 #endif // !defined(GO_GOGO_H)