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 importcfg file.
398 read_importcfg(const char* filename
);
400 // Read an embedcfg file.
402 read_embedcfg(const char* filename
);
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 // Add the unsafe bindings to the unsafe package.
542 add_unsafe_bindings(Package
*);
544 // Look up a package by pkgpath, and return its pkgpath_symbol.
546 pkgpath_symbol_for_package(const std::string
&);
548 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
549 // method function should be added to the type of its receiver.
551 start_function(const std::string
& name
, Function_type
* type
,
552 bool add_method_to_type
, Location
);
554 // Finish compiling a function.
556 finish_function(Location
);
558 // Return the current function.
560 current_function() const;
562 // Return the current block.
566 // Start a new block. This is not initially associated with a
569 start_block(Location
);
571 // Finish the current block and return it.
573 finish_block(Location
);
575 // Declare an erroneous name. This is used to avoid knock-on errors
576 // after a parsing error.
578 add_erroneous_name(const std::string
& name
);
580 // Declare an unknown name. This is used while parsing. The name
581 // must be resolved by the end of the parse. Unknown names are
582 // always added at the package level.
584 add_unknown_name(const std::string
& name
, Location
);
586 // Declare a function.
588 declare_function(const std::string
&, Function_type
*, Location
);
590 // Declare a function at the package level. This is used for
591 // functions generated for a type.
593 declare_package_function(const std::string
&, Function_type
*, Location
);
595 // Add a function declaration to the list of functions we may want
598 add_imported_inlinable_function(Named_object
*);
600 // Add a function to the list of functions that we do want to
603 add_imported_inline_function(Named_object
* no
)
604 { this->imported_inline_functions_
.push_back(no
); }
608 add_label_definition(const std::string
&, Location
);
610 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
611 // report errors for a goto from the current location to the label
614 add_label_reference(const std::string
&, Location
,
615 bool issue_goto_errors
);
617 // An analysis set is a list of functions paired with a boolean that indicates
618 // whether the list of functions are recursive.
619 typedef std::pair
<std::vector
<Named_object
*>, bool> Analysis_set
;
621 // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
624 add_analysis_set(const std::vector
<Named_object
*>& group
, bool recursive
)
625 { this->analysis_sets_
.push_back(std::make_pair(group
, recursive
)); }
627 // Return a snapshot of the current binding state.
629 bindings_snapshot(Location
);
631 // Add a statement to the current block.
633 add_statement(Statement
*);
635 // Add a block to the current block.
637 add_block(Block
*, Location
);
641 add_constant(const Typed_identifier
&, Expression
*, int iota_value
);
645 add_type(const std::string
&, Type
*, Location
);
647 // Add a named type. This is used for builtin types, and to add an
648 // imported type to the global scope.
650 add_named_type(Named_type
*);
654 declare_type(const std::string
&, Location
);
656 // Declare a type at the package level. This is used when the
657 // parser sees an unknown name where a type name is required.
659 declare_package_type(const std::string
&, Location
);
661 // Define a type which was already declared.
663 define_type(Named_object
*, Named_type
*);
667 add_variable(const std::string
&, Variable
*);
669 // Add a sink--a reference to the blank identifier _.
673 // Add a type which needs to be verified. This is used for sink
674 // types, just to give appropriate error messages.
676 add_type_to_verify(Type
* type
);
678 // Add a named object to the current namespace. This is used for
679 // import . "package".
681 add_dot_import_object(Named_object
*);
683 // Add an identifier to the list of names seen in the file block.
685 add_file_block_name(const std::string
& name
, Location location
)
686 { this->file_block_names_
[name
] = location
; }
688 // Add a linkname, from the go:linkname compiler directive. This
689 // changes the externally visible name of GO_NAME to be EXT_NAME.
690 // If EXT_NAME is the empty string, GO_NAME is unchanged, but the
691 // symbol is made publicly visible.
693 add_linkname(const std::string
& go_name
, bool is_exported
,
694 const std::string
& ext_name
, Location location
);
696 // Mark all local variables in current bindings as used. This is
697 // used when there is a parse error to avoid useless errors.
701 // Note that we've seen an interface type. This is used to build
702 // all required interface method tables.
704 record_interface_type(Interface_type
*);
706 // Whether we need an initialization function.
709 { return this->need_init_fn_
; }
711 // Note that we need an initialization function.
714 { this->need_init_fn_
= true; }
716 // Return whether the current file imported the unsafe package.
718 current_file_imported_unsafe() const
719 { return this->current_file_imported_unsafe_
; }
721 // Return whether the current file imported the embed package.
723 current_file_imported_embed() const
724 { return this->current_file_imported_embed_
; }
726 // Clear out all names in file scope. This is called when we start
727 // parsing a new file.
731 // Record that VAR1 must be initialized after VAR2. This is used
732 // when VAR2 does not appear in VAR1's INIT or PREINIT.
734 record_var_depends_on(Variable
* var1
, Named_object
* var2
)
736 go_assert(this->var_deps_
.find(var1
) == this->var_deps_
.end());
737 this->var_deps_
[var1
] = var2
;
740 // Return the variable that VAR depends on, or NULL if none.
742 var_depends_on(Variable
* var
) const
744 Var_deps::const_iterator p
= this->var_deps_
.find(var
);
745 return p
!= this->var_deps_
.end() ? p
->second
: NULL
;
748 // Queue up a type-specific hash function to be written out. This
749 // is used when a type-specific hash function is needed when not at
752 queue_hash_function(Type
* type
, int64_t size
, Backend_name
*,
753 Function_type
* hash_fntype
);
755 // Queue up a type-specific equal function to be written out. This
756 // is used when a type-specific equal function is needed when not at
759 queue_equal_function(Type
* type
, Named_type
* name
, int64_t size
,
760 Backend_name
*, Function_type
* equal_fntype
);
762 // Write out queued specific type functions.
764 write_specific_type_functions();
766 // Whether we are done writing out specific type functions.
768 specific_type_functions_are_written() const
769 { return this->specific_type_functions_are_written_
; }
771 // Add a pointer that needs to be added to the list of objects
772 // traversed by the garbage collector. This should be an expression
773 // of pointer type that points to static storage. It's not
774 // necessary to add global variables to this list, just global
775 // variable initializers that would otherwise not be seen.
777 add_gc_root(Expression
* expr
)
779 this->set_need_init_fn();
780 this->gc_roots_
.push_back(expr
);
783 // Add a type to the descriptor list.
785 add_type_descriptor(Type
* type
)
786 { this->type_descriptors_
.push_back(type
); }
788 // Traverse the tree. See the Traverse class.
792 // Define the predeclared global names.
794 define_global_names();
796 // Verify and complete all types.
800 // Lower the parse tree.
804 // Lower all the statements in a block.
806 lower_block(Named_object
* function
, Block
*);
808 // Lower an expression.
810 lower_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
814 lower_constant(Named_object
*);
816 // Flatten all the statements in a block.
818 flatten_block(Named_object
* function
, Block
*);
820 // Flatten an expression.
822 flatten_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
824 // Create all necessary function descriptors.
826 create_function_descriptors();
828 // Lower calls to builtin functions.
830 lower_builtin_calls();
832 // Finalize the method lists and build stub methods for named types.
836 // Finalize the method list for one type.
838 finalize_methods_for_type(Type
*);
840 // Work out the types to use for unspecified variables and
845 // Type check the program.
849 // Check the types in a single block. This is used for complicated
852 check_types_in_block(Block
*);
854 // Check for return statements.
856 check_return_statements();
858 // Gather references from global variables initializers to other
861 record_global_init_refs();
867 // Make implicit type conversions explicit.
871 // Make implicit type conversions explicit in a block.
873 add_conversions_in_block(Block
*);
875 // Analyze the program flow for escape information.
879 // Discover the groups of possibly recursive functions in this package.
881 discover_analysis_sets();
883 // Build a connectivity graph between the objects in each analyzed function.
885 assign_connectivity(Escape_context
*, Named_object
*);
887 // Traverse the objects in the connecitivty graph from the sink, adjusting the
888 // escape levels of each object.
890 propagate_escape(Escape_context
*, Node
*);
892 // Add notes about the escape level of a function's input and output
893 // parameters for exporting and importing top level functions.
895 tag_function(Named_object
*);
897 // Reclaim memory of escape analysis Nodes.
899 reclaim_escape_nodes();
905 // Add an import control function for an imported package to the
908 add_import_init_fn(const std::string
& package_name
,
909 const std::string
& init_name
, int prio
);
911 // Return the Import_init for a given init name.
913 lookup_init(const std::string
& init_name
);
915 // Turn short-cut operators (&&, ||) into explicit if statements.
919 // Turn short-cut operators into explicit if statements in a block.
921 remove_shortcuts_in_block(Block
*);
923 // Use temporary variables to force order of evaluation.
927 // Order evaluations in a block.
931 // Add write barriers as needed.
933 add_write_barriers();
935 // Return whether an assignment that sets LHS to RHS needs a write
938 assign_needs_write_barrier(Expression
* lhs
,
939 Unordered_set(const Named_object
*)*);
941 // Return whether EXPR is the address of a variable that can be set
942 // without a write barrier. That is, if this returns true, then an
943 // assignment to *EXPR does not require a write barrier.
945 is_nonwb_pointer(Expression
* expr
, Unordered_set(const Named_object
*)*);
947 // Return an assignment that sets LHS to RHS using a write barrier.
948 // This returns an if statement that checks whether write barriers
949 // are enabled. If not, it does LHS = RHS, otherwise it calls the
950 // appropriate write barrier function.
952 assign_with_write_barrier(Function
*, Block
*, Statement_inserter
*,
953 Expression
* lhs
, Expression
* rhs
, Location
);
955 // Return a statement that tests whether write barriers are enabled
956 // and executes either the efficient code (WITHOUT) or the write
957 // barrier function call (WITH), depending.
959 check_write_barrier(Block
*, Statement
* without
, Statement
* with
);
961 // Flatten parse tree.
965 // Build thunks for functions which call recover.
967 build_recover_thunks();
969 // Simplify statements which might use thunks: go and defer
972 simplify_thunk_statements();
974 // Dump AST if -fgo-dump-ast is set.
976 dump_ast(const char* basename
);
978 // Dump Call Graph if -fgo-dump-calls is set.
980 dump_call_graph(const char* basename
);
982 // Dump Connection Graphs if -fgo-dump-connections is set.
984 dump_connection_graphs(const char* basename
);
986 // Convert named types to the backend representation.
988 convert_named_types();
990 // Convert named types in a list of bindings.
992 convert_named_types_in_bindings(Bindings
*);
994 // True if named types have been converted to the backend
997 named_types_are_converted() const
998 { return this->named_types_are_converted_
; }
1000 // Give an error if the initialization of VAR depends on itself.
1002 check_self_dep(Named_object
*);
1004 // Write out the global values.
1008 // Build required interface method tables.
1010 build_interface_method_tables();
1012 // Return an expression which allocates memory to hold values of type TYPE.
1014 allocate_memory(Type
*type
, Location
);
1016 // Get the backend name to use for an exported function, a method,
1017 // or a function/method declaration.
1019 function_backend_name(const std::string
& go_name
, const Package
*,
1020 const Type
* receiver
, Backend_name
*);
1022 // Return the name to use for a function descriptor.
1024 function_descriptor_backend_name(Named_object
*, Backend_name
*);
1026 // Return the name to use for a generated stub method.
1028 stub_method_name(const Package
*, const std::string
& method_name
);
1030 // Get the backend name of the hash function for TYPE.
1032 hash_function_name(const Type
*, Backend_name
*);
1034 // Get the backend name of the equal function for TYPE.
1036 equal_function_name(const Type
*, const Named_type
*, Backend_name
*);
1038 // Get the backend name to use for a global variable.
1040 global_var_backend_name(const std::string
& go_name
, const Package
*,
1043 // Return a name to use for an error case. This should only be used
1044 // after reporting an error, and is used to avoid useless knockon
1049 // Return whether the name indicates an error.
1051 is_erroneous_name(const std::string
&);
1053 // Return a name to use for a thunk function. A thunk function is
1054 // one we create during the compilation, for a go statement or a
1055 // defer statement or a method expression.
1059 // Return whether an object is a thunk.
1061 is_thunk(const Named_object
*);
1063 // Return the name to use for an init function.
1065 init_function_name();
1067 // Return the name to use for a nested function.
1069 nested_function_name(Named_object
* enclosing
);
1071 // Return the name to use for a sink funciton.
1073 sink_function_name();
1075 // Return the name to use for an (erroneous) redefined function.
1077 redefined_function_name();
1079 // Return the name for use for a recover thunk.
1081 recover_thunk_name(const std::string
& name
, const Type
* rtype
);
1083 // Return the name to use for the GC root variable.
1087 // Return the name to use for a composite literal or string
1092 // Return the name of the variable used to represent the zero value
1095 map_zero_value_name();
1097 // Get the name of the magic initialization function.
1101 // Return the name for a dummy init function, which is not a real
1102 // function but only for tracking transitive import.
1104 dummy_init_fn_name();
1106 // Return the package path symbol from an init function name, which
1107 // can be a real init function or a dummy one.
1109 pkgpath_symbol_from_init_fn_name(std::string
);
1111 // Get the backend name for a type descriptor symbol.
1113 type_descriptor_backend_name(const Type
*, Named_type
*, Backend_name
*);
1115 // Return the name of the type descriptor list symbol of a package.
1116 // The argument is an encoded pkgpath, as with pkgpath_symbol.
1118 type_descriptor_list_symbol(const std::string
&);
1120 // Return the name of the list of all type descriptor lists.
1124 // Return the assembler name for the GC symbol for a type.
1126 gc_symbol_name(Type
*);
1128 // Return the assembler name for a ptrmask variable.
1130 ptrmask_symbol_name(const std::string
& ptrmask_sym_name
);
1132 // Return the name to use for an interface method table.
1134 interface_method_table_name(Interface_type
*, Type
*, bool is_pointer
);
1136 // If NAME is a special name used as a Go identifier, return the
1137 // position within the string where the special part of the name
1140 special_name_pos(const std::string
& name
);
1142 // Read a file into memory.
1144 read_file(const char* filename
, Location loc
, std::string
* data
);
1147 // During parsing, we keep a stack of functions. Each function on
1148 // the stack is one that we are currently parsing. For each
1149 // function, we keep track of the current stack of blocks.
1150 struct Open_function
1153 Named_object
* function
;
1154 // The stack of active blocks in the function.
1155 std::vector
<Block
*> blocks
;
1158 // The stack of functions.
1159 typedef std::vector
<Open_function
> Open_functions
;
1161 // Set up the built-in unsafe package.
1163 import_unsafe(const std::string
&, bool is_exported
, Location
);
1165 // Return the current binding contour.
1170 current_bindings() const;
1175 // Get the decl for the magic initialization function.
1177 initialization_function_decl();
1179 // Create the magic initialization function.
1181 create_initialization_function(Named_object
* fndecl
, Bstatement
* code_stmt
);
1183 // Initialize imported packages. BFUNCTION is the function
1184 // into which the package init calls will be placed.
1186 init_imports(std::vector
<Bstatement
*>&, Bfunction
* bfunction
);
1188 // Register variables with the garbage collector.
1190 register_gc_vars(const std::vector
<Named_object
*>&,
1191 std::vector
<Bstatement
*>&,
1192 Bfunction
* init_bfunction
);
1194 // Build the list of type descriptors.
1196 build_type_descriptor_list();
1198 // Register the type descriptors with the runtime.
1200 register_type_descriptors(std::vector
<Bstatement
*>&,
1201 Bfunction
* init_bfunction
);
1204 propagate_writebarrierrec();
1207 write_barrier_variable();
1210 is_digits(const std::string
&);
1212 // Type used to map go:embed patterns to a list of files.
1213 typedef Unordered_map(std::string
, std::vector
<std::string
>) Embed_patterns
;
1215 // Type used to map go:embed file names to their full path.
1216 typedef Unordered_map(std::string
, std::string
) Embed_files
;
1218 // Type used to map import names to packages.
1219 typedef std::map
<std::string
, Package
*> Imports
;
1221 // Type used to map package names to packages.
1222 typedef std::map
<std::string
, Package
*> Packages
;
1224 // Type used to map variables to the function calls that set them.
1225 // This is used for initialization dependency analysis.
1226 typedef std::map
<Variable
*, Named_object
*> Var_deps
;
1228 // Type used to map identifiers in the file block to the location
1229 // where they were defined.
1230 typedef Unordered_map(std::string
, Location
) File_block_names
;
1232 // Type used to queue writing a type specific function.
1233 struct Specific_type_function
1235 enum Specific_type_function_kind
{ SPECIFIC_HASH
, SPECIFIC_EQUAL
};
1240 Specific_type_function_kind kind
;
1242 Function_type
* fntype
;
1244 Specific_type_function(Type
* atype
, Named_type
* aname
, int64_t asize
,
1245 Specific_type_function_kind akind
,
1246 Backend_name
* abname
,
1247 Function_type
* afntype
)
1248 : type(atype
), name(aname
), size(asize
), kind(akind
),
1249 bname(*abname
), fntype(afntype
)
1253 // Recompute init priorities.
1255 recompute_init_priorities();
1257 // Recursive helper used by the routine above.
1259 update_init_priority(Import_init
* ii
,
1260 std::set
<const Import_init
*>* visited
);
1262 // The backend generator.
1264 // The object used to keep track of file names and line numbers.
1266 // The package we are compiling.
1268 // The list of currently open functions during parsing.
1269 Open_functions functions_
;
1270 // The global binding contour. This includes the builtin functions
1271 // and the package we are compiling.
1273 // The list of names we have seen in the file block.
1274 File_block_names file_block_names_
;
1275 // Mapping from import file names to packages.
1277 // Whether the magic unsafe package was imported.
1278 bool imported_unsafe_
;
1279 // Whether the magic unsafe package was imported by the current file.
1280 bool current_file_imported_unsafe_
;
1281 // Whether the embed package was imported by the current file.
1282 bool current_file_imported_embed_
;
1283 // Mapping from package names we have seen to packages. This does
1284 // not include the package we are compiling.
1286 // The functions named "init", if there are any.
1287 std::vector
<Named_object
*> init_functions_
;
1288 // A mapping from variables to the function calls that initialize
1289 // them, if it is not stored in the variable's init or preinit.
1290 // This is used for dependency analysis.
1292 // Whether we need a magic initialization function.
1294 // The name of the magic initialization function.
1295 std::string init_fn_name_
;
1296 // A list of import control variables for packages that we import.
1297 Import_init_set imported_init_fns_
;
1298 // The package path used for reflection data.
1299 std::string pkgpath_
;
1300 // The package path to use for a symbol name.
1301 std::string pkgpath_symbol_
;
1302 // The prefix to use for symbols, from the -fgo-prefix option.
1303 std::string prefix_
;
1304 // Whether pkgpath_ has been set.
1306 // Whether an explicit package path was set by -fgo-pkgpath.
1307 bool pkgpath_from_option_
;
1308 // Whether an explicit prefix was set by -fgo-prefix.
1309 bool prefix_from_option_
;
1310 // The relative import path, from the -fgo-relative-import-path
1312 std::string relative_import_path_
;
1313 // The C header file to write, from the -fgo-c-header option.
1314 std::string c_header_
;
1315 // Mapping from imports in the source file to the real import paths.
1316 Unordered_map(std::string
, std::string
) import_map_
;
1317 // Mapping from import paths to files to read.
1318 Unordered_map(std::string
, std::string
) package_file_
;
1319 // Patterns from an embedcfg file.
1320 Embed_patterns embed_patterns_
;
1321 // Mapping from file to full path from an embedcfg file.
1322 Embed_files embed_files_
;
1323 // Whether or not to check for division by zero, from the
1324 // -fgo-check-divide-zero option.
1325 bool check_divide_by_zero_
;
1326 // Whether or not to check for division overflow, from the
1327 // -fgo-check-divide-overflow option.
1328 bool check_divide_overflow_
;
1329 // Whether we are compiling the runtime package, from the
1330 // -fgo-compiling-runtime option.
1331 bool compiling_runtime_
;
1332 // The level of escape analysis debug information to emit, from the
1333 // -fgo-debug-escape option.
1334 int debug_escape_level_
;
1335 // A hash value for debug escape analysis, from the
1336 // -fgo-debug-escape-hash option. The analysis is run only on
1337 // functions with names that hash to the matching value.
1338 std::string debug_escape_hash_
;
1339 // Whether to output optimization diagnostics, from the
1340 // -fgo-debug-optimization option.
1341 bool debug_optimization_
;
1342 // Nil-check size threshhold.
1343 int64_t nil_check_size_threshold_
;
1344 // Whether runtime.eqtype calls are needed when comparing type
1347 // A list of types to verify.
1348 std::vector
<Type
*> verify_types_
;
1349 // A list of interface types defined while parsing.
1350 std::vector
<Interface_type
*> interface_types_
;
1351 // Type specific functions to write out.
1352 std::vector
<Specific_type_function
*> specific_type_functions_
;
1353 // Whether we are done writing out specific type functions.
1354 bool specific_type_functions_are_written_
;
1355 // Whether named types have been converted.
1356 bool named_types_are_converted_
;
1357 // A list containing groups of possibly mutually recursive functions to be
1358 // considered during escape analysis.
1359 std::vector
<Analysis_set
> analysis_sets_
;
1360 // A list of objects to add to the GC roots.
1361 std::vector
<Expression
*> gc_roots_
;
1362 // A list of type descriptors that we need to register.
1363 std::vector
<Type
*> type_descriptors_
;
1364 // A list of function declarations with imported bodies that we may
1366 std::vector
<Named_object
*> imported_inlinable_functions_
;
1367 // A list of functions that we want to inline. These will be sent
1369 std::vector
<Named_object
*> imported_inline_functions_
;
1372 // A block of statements.
1377 Block(Block
* enclosing
, Location
);
1379 // Return the enclosing block.
1382 { return this->enclosing_
; }
1384 // Return the bindings of the block.
1387 { return this->bindings_
; }
1391 { return this->bindings_
; }
1393 // Look at the block's statements.
1394 const std::vector
<Statement
*>*
1396 { return &this->statements_
; }
1398 // Return the start location. This is normally the location of the
1399 // left curly brace which starts the block.
1401 start_location() const
1402 { return this->start_location_
; }
1404 // Return the end location. This is normally the location of the
1405 // right curly brace which ends the block.
1407 end_location() const
1408 { return this->end_location_
; }
1410 // Add a statement to the block.
1412 add_statement(Statement
*);
1414 // Add a statement to the front of the block.
1416 add_statement_at_front(Statement
*);
1418 // Replace a statement in a block.
1420 replace_statement(size_t index
, Statement
*);
1422 // Add a Statement before statement number INDEX.
1424 insert_statement_before(size_t index
, Statement
*);
1426 // Add a Statement after statement number INDEX.
1428 insert_statement_after(size_t index
, Statement
*);
1430 // Set the end location of the block.
1432 set_end_location(Location location
)
1433 { this->end_location_
= location
; }
1435 // Traverse the tree.
1437 traverse(Traverse
*);
1439 // Set final types for unspecified variables and constants.
1441 determine_types(Gogo
*);
1443 // Return true if execution of this block may fall through to the
1446 may_fall_through() const;
1448 // Write the export data for the block's statements to the string.
1450 export_block(Export_function_body
*);
1452 // Turn exported block data into a block.
1454 import_block(Block
*, Import_function_body
*, Location
);
1456 // Convert the block to the backend representation.
1458 get_backend(Translate_context
*);
1460 // Iterate over statements.
1462 typedef std::vector
<Statement
*>::iterator iterator
;
1466 { return this->statements_
.begin(); }
1470 { return this->statements_
.end(); }
1475 // Statements in the block.
1476 std::vector
<Statement
*> statements_
;
1478 Bindings
* bindings_
;
1479 // Location of start of block.
1480 Location start_location_
;
1481 // Location of end of block.
1482 Location end_location_
;
1490 Function(Function_type
* type
, Named_object
*, Block
*, Location
);
1492 // Return the function's type.
1495 { return this->type_
; }
1497 // Return the enclosing function if there is one.
1500 { return this->enclosing_
; }
1502 // Set the enclosing function. This is used when building thunks
1503 // for functions which call recover.
1505 set_enclosing(Named_object
* enclosing
)
1507 go_assert(this->enclosing_
== NULL
);
1508 this->enclosing_
= enclosing
;
1511 // The result variables.
1512 typedef std::vector
<Named_object
*> Results
;
1514 // Create the result variables in the outer block.
1516 create_result_variables(Gogo
*);
1518 // Update the named result variables when cloning a function which
1521 update_result_variables();
1523 // Return the result variables.
1526 { return this->results_
; }
1530 { return this->is_sink_
; }
1534 { this->is_sink_
= true; }
1536 // Whether the result variables have names.
1538 results_are_named() const
1539 { return this->results_are_named_
; }
1541 // Return the assembler name.
1544 { return this->asm_name_
; }
1546 // Set the assembler name.
1548 set_asm_name(const std::string
& asm_name
)
1549 { this->asm_name_
= asm_name
; }
1551 // Mark this symbol as exported by a linkname directive.
1553 set_is_exported_by_linkname()
1554 { this->is_exported_by_linkname_
= true; }
1556 // Return the pragmas for this function.
1559 { return this->pragmas_
; }
1561 // Set the pragmas for this function.
1563 set_pragmas(unsigned int pragmas
)
1565 this->pragmas_
= pragmas
;
1568 // Return the index to use for a nested function.
1570 next_nested_function_index()
1572 ++this->nested_functions_
;
1573 return this->nested_functions_
;
1576 // Whether this method should not be included in the type
1579 nointerface() const;
1581 // Record that this method should not be included in the type
1586 // Record that this function is a stub method created for an unnamed
1589 set_is_unnamed_type_stub_method()
1591 go_assert(this->is_method());
1592 this->is_unnamed_type_stub_method_
= true;
1595 // Return the amount of enclosed variables in this closure.
1597 closure_field_count() const
1598 { return this->closure_fields_
.size(); }
1600 // Add a new field to the closure variable.
1602 add_closure_field(Named_object
* var
, Location loc
)
1603 { this->closure_fields_
.push_back(std::make_pair(var
, loc
)); }
1605 // Whether this function needs a closure.
1607 needs_closure() const
1608 { return !this->closure_fields_
.empty(); }
1610 // Return the closure variable, creating it if necessary. This is
1611 // passed to the function as a static chain parameter.
1615 // Set the closure variable. This is used when building thunks for
1616 // functions which call recover.
1618 set_closure_var(Named_object
* v
)
1620 go_assert(this->closure_var_
== NULL
);
1621 this->closure_var_
= v
;
1624 // Return the variable for a reference to field INDEX in the closure
1627 enclosing_var(unsigned int index
)
1629 go_assert(index
< this->closure_fields_
.size());
1630 return closure_fields_
[index
].first
;
1633 // Set the type of the closure variable if there is one.
1637 // Get the block of statements associated with the function.
1640 { return this->block_
; }
1642 // Get the location of the start of the function.
1645 { return this->location_
; }
1647 // Return whether this function is actually a method.
1651 // Add a label definition to the function.
1653 add_label_definition(Gogo
*, const std::string
& label_name
, Location
);
1655 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1656 // if we should report errors for a goto from the current location
1657 // to the label location.
1659 add_label_reference(Gogo
*, const std::string
& label_name
,
1660 Location
, bool issue_goto_errors
);
1662 // Warn about labels that are defined but not used.
1664 check_labels() const;
1666 // Note that a new local type has been added. Return its index.
1668 new_local_type_index()
1669 { return this->local_type_count_
++; }
1671 // Whether this function calls the predeclared recover function.
1673 calls_recover() const
1674 { return this->calls_recover_
; }
1676 // Record that this function calls the predeclared recover function.
1677 // This is set during the lowering pass.
1680 { this->calls_recover_
= true; }
1682 // Whether this is a recover thunk function.
1684 is_recover_thunk() const
1685 { return this->is_recover_thunk_
; }
1687 // Record that this is a thunk built for a function which calls
1690 set_is_recover_thunk()
1691 { this->is_recover_thunk_
= true; }
1693 // Whether this function already has a recover thunk.
1695 has_recover_thunk() const
1696 { return this->has_recover_thunk_
; }
1698 // Record that this function already has a recover thunk.
1700 set_has_recover_thunk()
1701 { this->has_recover_thunk_
= true; }
1703 // Record that this function is a thunk created for a defer
1704 // statement that calls the __go_set_defer_retaddr runtime function.
1706 set_calls_defer_retaddr()
1707 { this->calls_defer_retaddr_
= true; }
1709 // Whether this is a type hash or equality function created by the
1712 is_type_specific_function()
1713 { return this->is_type_specific_function_
; }
1715 // Record that this function is a type hash or equality function
1716 // created by the compiler.
1718 set_is_type_specific_function()
1719 { this->is_type_specific_function_
= true; }
1721 // Mark the function as going into a unique section.
1723 set_in_unique_section()
1724 { this->in_unique_section_
= true; }
1726 // Return whether this function should be exported for inlining.
1728 export_for_inlining() const
1729 { return this->export_for_inlining_
; }
1731 // Mark the function to be exported for inlining.
1733 set_export_for_inlining()
1734 { this->export_for_inlining_
= true; }
1736 // Return whether this function is inline only.
1738 is_inline_only() const
1739 { return this->is_inline_only_
; }
1741 // Mark the function as inline only: the body should not be emitted
1742 // if it is not inlined.
1744 set_is_inline_only()
1745 { this->is_inline_only_
= true; }
1747 // Report whether the function is referenced by an inline body.
1749 is_referenced_by_inline() const
1750 { return this->is_referenced_by_inline_
; }
1752 // Mark the function as referenced by an inline body.
1754 set_is_referenced_by_inline()
1755 { this->is_referenced_by_inline_
= true; }
1757 // Set the receiver type. This is used to remove aliases.
1759 set_receiver_type(Type
* rtype
);
1761 // Swap with another function. Used only for the thunk which calls
1764 swap_for_recover(Function
*);
1766 // Traverse the tree.
1768 traverse(Traverse
*);
1770 // Determine types in the function.
1772 determine_types(Gogo
*);
1774 // Return an expression for the function descriptor, given the named
1775 // object for this function. This may only be called for functions
1776 // without a closure. This will be an immutable struct with one
1777 // field that points to the function's code.
1779 descriptor(Gogo
*, Named_object
*);
1781 // Set the descriptor for this function. This is used when a
1782 // function declaration is followed by a function definition.
1784 set_descriptor(Expression
* descriptor
)
1786 go_assert(this->descriptor_
== NULL
);
1787 this->descriptor_
= descriptor
;
1790 // Return the backend representation.
1792 get_or_make_decl(Gogo
*, Named_object
*);
1794 // Return the function's decl after it has been built.
1798 // Set the function decl to hold a backend representation of the function
1801 build(Gogo
*, Named_object
*);
1803 // Get the statement that assigns values to this function's result struct.
1805 return_value(Gogo
*, Named_object
*, Location
) const;
1807 // Get the backend name of this function.
1809 backend_name(Gogo
*, Named_object
*, Backend_name
*);
1811 // Get an expression for the variable holding the defer stack.
1813 defer_stack(Location
);
1815 // Export the function.
1817 export_func(Export
*, const Named_object
*) const;
1819 // Export a function with a type.
1821 export_func_with_type(Export
*, const Named_object
*,
1822 const Function_type
*, Results
*, bool nointerface
,
1823 const std::string
& asm_name
, Block
* block
, Location
);
1825 // Import a function. Reports whether the import succeeded.
1827 import_func(Import
*, std::string
* pname
, Package
** pkg
,
1828 bool* is_exported
, Typed_identifier
** receiver
,
1829 Typed_identifier_list
** pparameters
,
1830 Typed_identifier_list
** presults
, bool* is_varargs
,
1831 bool* nointerface
, std::string
* asm_name
, std::string
* body
);
1834 // Type for mapping from label names to Label objects.
1835 typedef Unordered_map(std::string
, Label
*) Labels
;
1838 build_defer_wrapper(Gogo
*, Named_object
*, Bstatement
**, Bstatement
**);
1840 typedef std::vector
<std::pair
<Named_object
*,
1841 Location
> > Closure_fields
;
1843 // The function's type.
1844 Function_type
* type_
;
1845 // The enclosing function. This is NULL when there isn't one, which
1846 // is the normal case.
1847 Named_object
* enclosing_
;
1848 // The result variables, if any.
1850 // If there is a closure, this is the list of variables which appear
1851 // in the closure. This is created by the parser, and then resolved
1852 // to a real type when we lower parse trees.
1853 Closure_fields closure_fields_
;
1854 // The closure variable, passed as a parameter using the static
1855 // chain parameter. Normally NULL.
1856 Named_object
* closure_var_
;
1857 // The outer block of statements in the function.
1859 // The source location of the start of the function.
1861 // Labels defined or referenced in the function.
1863 // The number of local types defined in this function.
1864 unsigned int local_type_count_
;
1865 // The assembler name: this is the name that will be put in the object file.
1866 // Set by the go:linkname compiler directive. This is normally empty.
1867 std::string asm_name_
;
1868 // The function descriptor, if any.
1869 Expression
* descriptor_
;
1870 // The function decl.
1872 // The defer stack variable. A pointer to this variable is used to
1873 // distinguish the defer stack for one function from another. This
1874 // is NULL unless we actually need a defer stack.
1875 Temporary_statement
* defer_stack_
;
1876 // Pragmas for this function. This is a set of GOPRAGMA bits.
1877 unsigned int pragmas_
;
1878 // Number of nested functions defined within this function.
1879 unsigned int nested_functions_
;
1880 // True if this function is sink-named. No code is generated.
1882 // True if the result variables are named.
1883 bool results_are_named_
: 1;
1884 // True if this function is a stub method created for an unnamed
1886 bool is_unnamed_type_stub_method_
: 1;
1887 // True if this function calls the predeclared recover function.
1888 bool calls_recover_
: 1;
1889 // True if this a thunk built for a function which calls recover.
1890 bool is_recover_thunk_
: 1;
1891 // True if this function already has a recover thunk.
1892 bool has_recover_thunk_
: 1;
1893 // True if this is a thunk built for a defer statement that calls
1894 // the __go_set_defer_retaddr runtime function.
1895 bool calls_defer_retaddr_
: 1;
1896 // True if this is a function built by the compiler to as a hash or
1897 // equality function for some type.
1898 bool is_type_specific_function_
: 1;
1899 // True if this function should be put in a unique section. This is
1900 // turned on for field tracking.
1901 bool in_unique_section_
: 1;
1902 // True if we should export the body of this function for
1903 // cross-package inlining.
1904 bool export_for_inlining_
: 1;
1905 // True if this function is inline only: if it should not be emitted
1906 // if it is not inlined.
1907 bool is_inline_only_
: 1;
1908 // True if this function is referenced from an inlined body that
1909 // will be put into the export data.
1910 bool is_referenced_by_inline_
: 1;
1911 // True if we should make this function visible to other packages
1912 // because of a go:linkname directive.
1913 bool is_exported_by_linkname_
: 1;
1916 // A snapshot of the current binding state.
1918 class Bindings_snapshot
1921 Bindings_snapshot(const Block
*, Location
);
1923 // Report any errors appropriate for a goto from the current binding
1924 // state of B to this one.
1926 check_goto_from(const Block
* b
, Location
);
1928 // Report any errors appropriate for a goto from this binding state
1929 // to the current state of B.
1931 check_goto_to(const Block
* b
);
1935 check_goto_block(Location
, const Block
*, const Block
*, size_t*);
1938 check_goto_defs(Location
, const Block
*, size_t, size_t);
1940 // The current block.
1941 const Block
* block_
;
1942 // The number of names currently defined in each open block.
1943 // Element 0 is this->block_, element 1 is
1944 // this->block_->enclosing(), etc.
1945 std::vector
<size_t> counts_
;
1946 // The location where this snapshot was taken.
1950 // A function declaration.
1952 class Function_declaration
1955 Function_declaration(Function_type
* fntype
, Location location
)
1956 : fntype_(fntype
), location_(location
), asm_name_(), descriptor_(NULL
),
1957 fndecl_(NULL
), pragmas_(0), imported_body_(),
1958 is_on_inlinable_list_(false)
1963 { return this->fntype_
; }
1967 { return this->location_
; }
1969 // Return whether this function declaration is a method.
1975 { return this->asm_name_
; }
1977 // Set the assembler name.
1979 set_asm_name(const std::string
& asm_name
)
1980 { this->asm_name_
= asm_name
; }
1982 // Return the pragmas for this function.
1985 { return this->pragmas_
; }
1987 // Set the pragmas for this function.
1989 set_pragmas(unsigned int pragmas
)
1991 this->pragmas_
= pragmas
;
1994 // Whether this method should not be included in the type
1997 nointerface() const;
1999 // Record that this method should not be included in the type
2004 // Whether we have an imported function body.
2006 has_imported_body() const
2007 { return !this->imported_body_
.empty(); }
2009 // Record the imported body of this function.
2011 set_imported_body(Import
* imp
, const std::string
& imported_body
)
2014 this->imported_body_
= imported_body
;
2017 // Whether this declaration is on the list of inlinable functions.
2019 is_on_inlinable_list() const
2020 { return this->is_on_inlinable_list_
; }
2022 // Set that this function is on the list of inlinable functions.
2024 set_is_on_inlinable_list()
2025 { this->is_on_inlinable_list_
= true; }
2027 // Set the receiver type. This is used to remove aliases.
2029 set_receiver_type(Type
* rtype
);
2031 // Import the function body, creating a function.
2033 import_function_body(Gogo
*, Named_object
*);
2035 // Return an expression for the function descriptor, given the named
2036 // object for this function. This may only be called for functions
2037 // without a closure. This will be an immutable struct with one
2038 // field that points to the function's code.
2040 descriptor(Gogo
*, Named_object
*);
2042 // Return true if we have created a descriptor for this declaration.
2044 has_descriptor() const
2045 { return this->descriptor_
!= NULL
; }
2047 // Return a backend representation.
2049 get_or_make_decl(Gogo
*, Named_object
*);
2051 // If there is a descriptor, build it into the backend
2054 build_backend_descriptor(Gogo
*);
2056 // Get the backend name of this function declaration.
2058 backend_name(Gogo
*, Named_object
*, Backend_name
*);
2060 // Export a function declaration.
2062 export_func(Export
* exp
, const Named_object
* no
) const
2064 Function::export_func_with_type(exp
, no
, this->fntype_
, NULL
,
2065 this->is_method() && this->nointerface(),
2066 this->asm_name_
, NULL
, this->location_
);
2069 // Check that the types used in this declaration's signature are defined.
2071 check_types() const;
2074 // The type of the function.
2075 Function_type
* fntype_
;
2076 // The location of the declaration.
2078 // The assembler name: this is the name to use in references to the
2079 // function. This is normally empty.
2080 std::string asm_name_
;
2081 // The function descriptor, if any.
2082 Expression
* descriptor_
;
2083 // The function decl if needed.
2085 // Pragmas for this function. This is a set of GOPRAGMA bits.
2086 unsigned int pragmas_
;
2087 // Importer for function body if imported from a different package.
2089 // Export data for function body if imported from a different package.
2090 std::string imported_body_
;
2091 // Whether this declaration is already on the list of inlinable functions.
2092 bool is_on_inlinable_list_
;
2100 Variable(Type
*, Expression
*, bool is_global
, bool is_parameter
,
2101 bool is_receiver
, Location
);
2103 // Get the type of the variable.
2110 // Return whether the type is defined yet.
2114 // Get the initial value.
2117 { return this->init_
; }
2119 // Return whether there are any preinit statements.
2121 has_pre_init() const
2122 { return this->preinit_
!= NULL
; }
2124 // Return the preinit statements if any.
2127 { return this->preinit_
; }
2129 // Return whether this is a global variable.
2132 { return this->is_global_
; }
2134 // Return whether this is a function parameter.
2136 is_parameter() const
2137 { return this->is_parameter_
; }
2139 // Return whether this is a closure (static chain) parameter.
2142 { return this->is_closure_
; }
2144 // Change this parameter to be a closure.
2148 this->is_closure_
= true;
2151 // Return whether this is the receiver parameter of a method.
2154 { return this->is_receiver_
; }
2156 // Change this parameter to be a receiver. This is used when
2157 // creating the thunks created for functions which call recover.
2161 go_assert(this->is_parameter_
);
2162 this->is_receiver_
= true;
2165 // Change this parameter to not be a receiver. This is used when
2166 // creating the thunks created for functions which call recover.
2168 set_is_not_receiver()
2170 go_assert(this->is_parameter_
);
2171 this->is_receiver_
= false;
2174 // Return whether this is the varargs parameter of a function.
2176 is_varargs_parameter() const
2177 { return this->is_varargs_parameter_
; }
2179 // Return whether this is a global sink variable, created only to
2180 // run an initializer.
2182 is_global_sink() const
2183 { return this->is_global_sink_
; }
2185 // Record that this is a global sink variable.
2187 set_is_global_sink()
2189 go_assert(this->is_global_
);
2190 this->is_global_sink_
= true;
2193 // Whether this variable's address is taken.
2195 is_address_taken() const
2196 { return this->is_address_taken_
; }
2198 // Whether this variable should live in the heap.
2201 { return this->is_address_taken_
&& !this->is_global_
; }
2203 // Note that something takes the address of this variable.
2206 { this->is_address_taken_
= true; }
2208 // Return whether the address is taken but does not escape.
2210 is_non_escaping_address_taken() const
2211 { return this->is_non_escaping_address_taken_
; }
2213 // Note that something takes the address of this variable such that
2214 // the address does not escape the function.
2216 set_non_escaping_address_taken()
2217 { this->is_non_escaping_address_taken_
= true; }
2219 // Get the source location of the variable's declaration.
2222 { return this->location_
; }
2224 // Record that this is the varargs parameter of a function.
2226 set_is_varargs_parameter()
2228 go_assert(this->is_parameter_
);
2229 this->is_varargs_parameter_
= true;
2232 // Return whether the variable has been used.
2235 { return this->is_used_
; }
2237 // Mark that the variable has been used.
2240 { this->is_used_
= true; }
2242 // Clear the initial value; used for error handling and write barriers.
2245 { this->init_
= NULL
; }
2247 // Set the initial value; used for converting shortcuts.
2249 set_init(Expression
* init
)
2250 { this->init_
= init
; }
2252 // Get the preinit block, a block of statements to be run before the
2253 // initialization expression.
2255 preinit_block(Gogo
*);
2257 // Add a statement to be run before the initialization expression.
2258 // This is only used for global variables.
2260 add_preinit_statement(Gogo
*, Statement
*);
2262 // Lower the initialization expression after parsing is complete.
2264 lower_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
2266 // Flatten the initialization expression after ordering evaluations.
2268 flatten_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
2270 // A special case: the init value is used only to determine the
2271 // type. This is used if the variable is defined using := with the
2272 // comma-ok form of a map index or a receive expression. The init
2273 // value is actually the map index expression or receive expression.
2274 // We use this because we may not know the right type at parse time.
2276 set_type_from_init_tuple()
2277 { this->type_from_init_tuple_
= true; }
2279 // Another special case: the init value is used only to determine
2280 // the type. This is used if the variable is defined using := with
2281 // a range clause. The init value is the range expression. The
2282 // type of the variable is the index type of the range expression
2283 // (i.e., the first value returned by a range).
2285 set_type_from_range_index()
2286 { this->type_from_range_index_
= true; }
2288 // Another special case: like set_type_from_range_index, but the
2289 // type is the value type of the range expression (i.e., the second
2290 // value returned by a range).
2292 set_type_from_range_value()
2293 { this->type_from_range_value_
= true; }
2295 // Another special case: the init value is used only to determine
2296 // the type. This is used if the variable is defined using := with
2297 // a case in a select statement. The init value is the channel.
2298 // The type of the variable is the channel's element type.
2300 set_type_from_chan_element()
2301 { this->type_from_chan_element_
= true; }
2303 // After we lower the select statement, we once again set the type
2304 // from the initialization expression.
2306 clear_type_from_chan_element()
2308 go_assert(this->type_from_chan_element_
);
2309 this->type_from_chan_element_
= false;
2312 // TRUE if this variable was created for a type switch clause.
2314 is_type_switch_var() const
2315 { return this->is_type_switch_var_
; }
2317 // Note that this variable was created for a type switch clause.
2319 set_is_type_switch_var()
2320 { this->is_type_switch_var_
= true; }
2322 // Mark the variable as going into a unique section.
2324 set_in_unique_section()
2326 go_assert(this->is_global_
);
2327 this->in_unique_section_
= true;
2330 // Mark the variable as referenced by an inline body.
2332 set_is_referenced_by_inline()
2334 go_assert(this->is_global_
);
2335 this->is_referenced_by_inline_
= true;
2338 // Attach any go:embed comments for this variable.
2340 set_embeds(std::vector
<std::string
>* embeds
)
2342 go_assert(this->is_global_
2343 && this->init_
== NULL
2344 && this->preinit_
== NULL
);
2345 this->embeds_
= embeds
;
2348 // Return the top-level declaration for this variable.
2351 { return this->toplevel_decl_
; }
2353 // Set the top-level declaration for this variable. Only used for local
2356 set_toplevel_decl(Statement
* s
)
2358 go_assert(!this->is_global_
&& !this->is_parameter_
&& !this->is_receiver_
);
2359 this->toplevel_decl_
= s
;
2362 // Note that the initializer of this global variable refers to VAR.
2364 add_init_ref(Named_object
* var
);
2366 // The variables that this variable's initializers refer to.
2367 const std::vector
<Named_object
*>*
2369 { return this->init_refs_
; }
2371 // Traverse the initializer expression.
2373 traverse_expression(Traverse
*, unsigned int traverse_mask
);
2375 // Determine the type of the variable if necessary.
2377 determine_type(Gogo
*);
2379 // Get the backend representation of the variable.
2381 get_backend_variable(Gogo
*, Named_object
*, const Package
*,
2382 const std::string
&);
2384 // Get the initial value of the variable. This may only
2385 // be called if has_pre_init() returns false.
2387 get_init(Gogo
*, Named_object
* function
);
2389 // Return a series of statements which sets the value of the
2390 // variable in DECL. This should only be called is has_pre_init()
2391 // returns true. DECL may be NULL for a sink variable.
2393 get_init_block(Gogo
*, Named_object
* function
, Bvariable
* decl
);
2395 // Export the variable.
2397 export_var(Export
*, const Named_object
*) const;
2399 // Import a variable. Reports whether the import succeeded.
2401 import_var(Import
*, std::string
* pname
, Package
** pkg
, bool* is_exported
,
2405 // The type of a tuple.
2407 type_from_tuple(Expression
*, bool) const;
2409 // The type of a range.
2411 type_from_range(Expression
*, bool, bool) const;
2413 // The element type of a channel.
2415 type_from_chan_element(Expression
*, bool) const;
2417 // The variable's type. This may be NULL if the type is set from
2420 // The initial value. This may be NULL if the variable should be
2421 // initialized to the default value for the type.
2423 // Statements to run before the init statement.
2425 // Location of variable definition.
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_
;
2430 // Variables that the initializer of a global variable refers to.
2431 // Used for initializer ordering.
2432 std::vector
<Named_object
*>* init_refs_
;
2433 // Any associated go:embed comments.
2434 std::vector
<std::string
>* embeds_
;
2435 // Backend representation.
2436 Bvariable
* backend_
;
2437 // Whether this is a global variable.
2438 bool is_global_
: 1;
2439 // Whether this is a function parameter.
2440 bool is_parameter_
: 1;
2441 // Whether this is a closure parameter.
2442 bool is_closure_
: 1;
2443 // Whether this is the receiver parameter of a method.
2444 bool is_receiver_
: 1;
2445 // Whether this is the varargs parameter of a function.
2446 bool is_varargs_parameter_
: 1;
2447 // Whether this is a global sink variable created to run an
2449 bool is_global_sink_
: 1;
2450 // Whether this variable is ever referenced.
2452 // Whether something takes the address of this variable. For a
2453 // local variable this implies that the variable has to be on the
2454 // heap if it escapes from its function.
2455 bool is_address_taken_
: 1;
2456 // Whether something takes the address of this variable such that
2457 // the address does not escape the function.
2458 bool is_non_escaping_address_taken_
: 1;
2459 // True if we have seen this variable in a traversal.
2461 // True if we have lowered the initialization expression.
2462 bool init_is_lowered_
: 1;
2463 // True if we have flattened the initialization expression.
2464 bool init_is_flattened_
: 1;
2465 // True if init is a tuple used to set the type.
2466 bool type_from_init_tuple_
: 1;
2467 // True if init is a range clause and the type is the index type.
2468 bool type_from_range_index_
: 1;
2469 // True if init is a range clause and the type is the value type.
2470 bool type_from_range_value_
: 1;
2471 // True if init is a channel and the type is the channel's element type.
2472 bool type_from_chan_element_
: 1;
2473 // True if this is a variable created for a type switch case.
2474 bool is_type_switch_var_
: 1;
2475 // True if we have determined types.
2476 bool determined_type_
: 1;
2477 // True if this variable should be put in a unique section. This is
2478 // used for field tracking.
2479 bool in_unique_section_
: 1;
2480 // True if this variable is referenced from an inlined body that
2481 // will be put into the export data.
2482 bool is_referenced_by_inline_
: 1;
2485 // A variable which is really the name for a function return value, or
2488 class Result_variable
2491 Result_variable(Type
* type
, Function
* function
, int index
,
2493 : type_(type
), function_(function
), index_(index
), location_(location
),
2494 backend_(NULL
), is_address_taken_(false),
2495 is_non_escaping_address_taken_(false)
2498 // Get the type of the result variable.
2501 { return this->type_
; }
2503 // Get the function that this is associated with.
2506 { return this->function_
; }
2508 // Index in the list of function results.
2511 { return this->index_
; }
2513 // The location of the variable definition.
2516 { return this->location_
; }
2518 // Whether this variable's address is taken.
2520 is_address_taken() const
2521 { return this->is_address_taken_
; }
2523 // Note that something takes the address of this variable.
2526 { this->is_address_taken_
= true; }
2528 // Return whether the address is taken but does not escape.
2530 is_non_escaping_address_taken() const
2531 { return this->is_non_escaping_address_taken_
; }
2533 // Note that something takes the address of this variable such that
2534 // the address does not escape the function.
2536 set_non_escaping_address_taken()
2537 { this->is_non_escaping_address_taken_
= true; }
2539 // Whether this variable should live in the heap.
2542 { return this->is_address_taken_
; }
2544 // Set the function. This is used when cloning functions which call
2547 set_function(Function
* function
)
2548 { this->function_
= function
; }
2550 // Get the backend representation of the variable.
2552 get_backend_variable(Gogo
*, Named_object
*, const std::string
&);
2555 // Type of result variable.
2557 // Function with which this is associated.
2558 Function
* function_
;
2559 // Index in list of results.
2561 // Where the result variable is defined.
2563 // Backend representation.
2564 Bvariable
* backend_
;
2565 // Whether something takes the address of this variable.
2566 bool is_address_taken_
;
2567 // Whether something takes the address of this variable such that
2568 // the address does not escape the function.
2569 bool is_non_escaping_address_taken_
;
2572 // The value we keep for a named constant. This lets us hold a type
2573 // and an expression.
2575 class Named_constant
2578 Named_constant(Type
* type
, Expression
* expr
, int iota_value
,
2580 : type_(type
), expr_(expr
), iota_value_(iota_value
), location_(location
),
2581 lowering_(false), is_sink_(false), type_is_determined_(false),
2587 { return this->type_
; }
2594 { return this->expr_
; }
2598 { return this->iota_value_
; }
2602 { return this->location_
; }
2604 // Whether we are lowering.
2607 { return this->lowering_
; }
2609 // Set that we are lowering.
2612 { this->lowering_
= true; }
2614 // We are no longer lowering.
2617 { this->lowering_
= false; }
2621 { return this->is_sink_
; }
2625 { this->is_sink_
= true; }
2627 // Traverse the expression.
2629 traverse_expression(Traverse
*);
2631 // Determine the type of the constant if necessary.
2633 determine_type(Gogo
*);
2635 // Indicate that we found and reported an error for this constant.
2639 // Export the constant.
2641 export_const(Export
*, const std::string
& name
) const;
2643 // Import a constant.
2645 import_const(Import
*, std::string
*, Type
**, Expression
**);
2647 // Get the backend representation of the constant value.
2649 get_backend(Gogo
*, Named_object
*);
2652 // The type of the constant.
2654 // The expression for the constant.
2656 // If the predeclared constant iota is used in EXPR_, this is the
2657 // value it will have. We do this because at parse time we don't
2658 // know whether the name "iota" will refer to the predeclared
2659 // constant or to something else. We put in the right value in when
2662 // The location of the definition.
2664 // Whether we are currently lowering this constant.
2666 // Whether this constant is blank named and needs only type checking.
2668 // Whether we have determined the type of the constants.
2669 bool type_is_determined_
;
2670 // The backend representation of the constant value.
2671 Bexpression
* bconst_
;
2674 // A type declaration.
2676 class Type_declaration
2679 Type_declaration(Location location
)
2680 : location_(location
), in_function_(NULL
), in_function_index_(0),
2681 methods_(), issued_warning_(false)
2684 // Return the location.
2687 { return this->location_
; }
2689 // Return the function in which this type is declared. This will
2690 // return NULL for a type declared in global scope.
2692 in_function(unsigned int* pindex
)
2694 *pindex
= this->in_function_index_
;
2695 return this->in_function_
;
2698 // Set the function in which this type is declared.
2700 set_in_function(Named_object
* f
, unsigned int index
)
2702 this->in_function_
= f
;
2703 this->in_function_index_
= index
;
2706 // Add a method to this type. This is used when methods are defined
2709 add_method(const std::string
& name
, Function
* function
);
2711 // Add a method declaration to this type.
2713 add_method_declaration(const std::string
& name
, Package
*,
2714 Function_type
* type
, Location location
);
2716 // Add an already created object as a method.
2718 add_existing_method(Named_object
* no
)
2719 { this->methods_
.push_back(no
); }
2721 // Return whether any methods were defined.
2723 has_methods() const;
2725 // Return the methods.
2726 const std::vector
<Named_object
*>*
2728 { return &this->methods_
; }
2730 // Define methods when the real type is known.
2732 define_methods(Named_type
*);
2734 // This is called if we are trying to use this type. It returns
2735 // true if we should issue a warning.
2740 // The location of the type declaration.
2742 // If this type is declared in a function, a pointer back to the
2743 // function in which it is defined.
2744 Named_object
* in_function_
;
2745 // The index of this type in IN_FUNCTION_.
2746 unsigned int in_function_index_
;
2747 // Methods defined before the type is defined.
2748 std::vector
<Named_object
*> methods_
;
2749 // True if we have issued a warning about a use of this type
2750 // declaration when it is undefined.
2751 bool issued_warning_
;
2754 // An unknown object. These are created by the parser for forward
2755 // references to names which have not been seen before. In a correct
2756 // program, these will always point to a real definition by the end of
2757 // the parse. Because they point to another Named_object, these may
2758 // only be referenced by Unknown_expression objects.
2763 Unknown_name(Location location
)
2764 : location_(location
), real_named_object_(NULL
)
2767 // Return the location where this name was first seen.
2770 { return this->location_
; }
2772 // Return the real named object that this points to, or NULL if it
2773 // was never resolved.
2775 real_named_object() const
2776 { return this->real_named_object_
; }
2778 // Set the real named object that this points to.
2780 set_real_named_object(Named_object
* no
);
2783 // The location where this name was first seen.
2785 // The real named object when it is known.
2790 // A named object named. This is the result of a declaration. We
2791 // don't use a superclass because they all have to be handled
2799 // An uninitialized Named_object. We should never see this.
2800 NAMED_OBJECT_UNINITIALIZED
,
2801 // An erroneous name. This indicates a parse error, to avoid
2802 // later errors about undefined references.
2803 NAMED_OBJECT_ERRONEOUS
,
2804 // An unknown name. This is used for forward references. In a
2805 // correct program, these will all be resolved by the end of the
2807 NAMED_OBJECT_UNKNOWN
,
2812 // A forward type declaration.
2813 NAMED_OBJECT_TYPE_DECLARATION
,
2816 // A result variable in a function.
2817 NAMED_OBJECT_RESULT_VAR
,
2818 // The blank identifier--the special variable named _.
2822 // A forward func declaration.
2823 NAMED_OBJECT_FUNC_DECLARATION
,
2825 NAMED_OBJECT_PACKAGE
2828 // Return the classification.
2830 classification() const
2831 { return this->classification_
; }
2836 is_erroneous() const
2837 { return this->classification_
== NAMED_OBJECT_ERRONEOUS
; }
2841 { return this->classification_
== NAMED_OBJECT_UNKNOWN
; }
2845 { return this->classification_
== NAMED_OBJECT_CONST
; }
2849 { return this->classification_
== NAMED_OBJECT_TYPE
; }
2852 is_type_declaration() const
2853 { return this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
; }
2857 { return this->classification_
== NAMED_OBJECT_VAR
; }
2860 is_result_variable() const
2861 { return this->classification_
== NAMED_OBJECT_RESULT_VAR
; }
2865 { return this->classification_
== NAMED_OBJECT_SINK
; }
2869 { return this->classification_
== NAMED_OBJECT_FUNC
; }
2872 is_function_declaration() const
2873 { return this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
; }
2877 { return this->classification_
== NAMED_OBJECT_PACKAGE
; }
2881 static Named_object
*
2882 make_erroneous_name(const std::string
& name
)
2883 { return new Named_object(name
, NULL
, NAMED_OBJECT_ERRONEOUS
); }
2885 static Named_object
*
2886 make_unknown_name(const std::string
& name
, Location
);
2888 static Named_object
*
2889 make_constant(const Typed_identifier
&, const Package
*, Expression
*,
2892 static Named_object
*
2893 make_type(const std::string
&, const Package
*, Type
*, Location
);
2895 static Named_object
*
2896 make_type_declaration(const std::string
&, const Package
*, Location
);
2898 static Named_object
*
2899 make_variable(const std::string
&, const Package
*, Variable
*);
2901 static Named_object
*
2902 make_result_variable(const std::string
&, Result_variable
*);
2904 static Named_object
*
2907 static Named_object
*
2908 make_function(const std::string
&, const Package
*, Function
*);
2910 static Named_object
*
2911 make_function_declaration(const std::string
&, const Package
*, Function_type
*,
2914 static Named_object
*
2915 make_package(const std::string
& alias
, Package
* package
);
2922 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2923 return this->u_
.unknown_value
;
2927 unknown_value() const
2929 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2930 return this->u_
.unknown_value
;
2936 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2937 return this->u_
.const_value
;
2940 const Named_constant
*
2943 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2944 return this->u_
.const_value
;
2950 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2951 return this->u_
.type_value
;
2957 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2958 return this->u_
.type_value
;
2962 type_declaration_value()
2964 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2965 return this->u_
.type_declaration
;
2968 const Type_declaration
*
2969 type_declaration_value() const
2971 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2972 return this->u_
.type_declaration
;
2978 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2979 return this->u_
.var_value
;
2985 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2986 return this->u_
.var_value
;
2992 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2993 return this->u_
.result_var_value
;
2996 const Result_variable
*
2997 result_var_value() const
2999 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
3000 return this->u_
.result_var_value
;
3006 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
3007 return this->u_
.func_value
;
3013 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
3014 return this->u_
.func_value
;
3017 Function_declaration
*
3018 func_declaration_value()
3020 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
3021 return this->u_
.func_declaration_value
;
3024 const Function_declaration
*
3025 func_declaration_value() const
3027 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
3028 return this->u_
.func_declaration_value
;
3034 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
3035 return this->u_
.package_value
;
3039 package_value() const
3041 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
3042 return this->u_
.package_value
;
3047 { return this->name_
; }
3049 // Return the name to use in an error message. The difference is
3050 // that if this Named_object is defined in a different package, this
3051 // will return PACKAGE.NAME.
3053 message_name() const;
3057 { return this->package_
; }
3059 // Resolve an unknown value if possible. This returns the same
3060 // Named_object or a new one.
3064 Named_object
* ret
= this;
3065 if (this->is_unknown())
3067 Named_object
* r
= this->unknown_value()->real_named_object();
3077 const Named_object
* ret
= this;
3078 if (this->is_unknown())
3080 const Named_object
* r
= this->unknown_value()->real_named_object();
3087 // The location where this object was defined or referenced.
3091 // Traverse a Named_object.
3093 traverse(Traverse
*, bool is_global
);
3095 // Convert a variable to the backend representation.
3097 get_backend_variable(Gogo
*, Named_object
* function
);
3099 // Get the backend representation of this object.
3101 get_backend(Gogo
*, std::vector
<Bexpression
*>&, std::vector
<Btype
*>&,
3102 std::vector
<Bfunction
*>&);
3104 // Define a type declaration.
3106 set_type_value(Named_type
*);
3108 // Define a function declaration.
3110 set_function_value(Function
*);
3112 // Declare an unknown name as a type declaration.
3116 // Export this object.
3118 export_named_object(Export
*) const;
3120 // Mark this named object as an invalid redefinition of another object.
3122 set_is_redefinition()
3123 { this->is_redefinition_
= true; }
3125 // Return whether or not this object is a invalid redefinition of another
3128 is_redefinition() const
3129 { return this->is_redefinition_
; }
3132 Named_object(const std::string
&, const Package
*, Classification
);
3134 // The name of the object.
3136 // The package that this object is in. This is NULL if it is in the
3137 // file we are compiling.
3138 const Package
* package_
;
3139 // The type of object this is.
3140 Classification classification_
;
3144 Unknown_name
* unknown_value
;
3145 Named_constant
* const_value
;
3146 Named_type
* type_value
;
3147 Type_declaration
* type_declaration
;
3148 Variable
* var_value
;
3149 Result_variable
* result_var_value
;
3150 Function
* func_value
;
3151 Function_declaration
* func_declaration_value
;
3152 Package
* package_value
;
3154 // True if this object is an invalid redefinition of another object.
3155 bool is_redefinition_
;
3158 // A binding contour. This binds names to objects.
3163 // Type for mapping from names to objects.
3164 typedef Unordered_map(std::string
, Named_object
*) Contour
;
3166 Bindings(Bindings
* enclosing
);
3168 // Add an erroneous name.
3170 add_erroneous_name(const std::string
& name
)
3171 { return this->add_named_object(Named_object::make_erroneous_name(name
)); }
3173 // Add an unknown name.
3175 add_unknown_name(const std::string
& name
, Location location
)
3177 return this->add_named_object(Named_object::make_unknown_name(name
,
3183 add_constant(const Typed_identifier
& tid
, const Package
* package
,
3184 Expression
* expr
, int iota_value
)
3186 return this->add_named_object(Named_object::make_constant(tid
, package
,
3193 add_type(const std::string
& name
, const Package
* package
, Type
* type
,
3196 return this->add_named_object(Named_object::make_type(name
, package
, type
,
3200 // Add a named type. This is used for builtin types, and to add an
3201 // imported type to the global scope.
3203 add_named_type(Named_type
* named_type
);
3205 // Add a type declaration.
3207 add_type_declaration(const std::string
& name
, const Package
* package
,
3210 Named_object
* no
= Named_object::make_type_declaration(name
, package
,
3212 return this->add_named_object(no
);
3217 add_variable(const std::string
& name
, const Package
* package
,
3220 return this->add_named_object(Named_object::make_variable(name
, package
,
3224 // Add a result variable.
3226 add_result_variable(const std::string
& name
, Result_variable
* result
)
3228 return this->add_named_object(Named_object::make_result_variable(name
,
3234 add_function(const std::string
& name
, const Package
*, Function
* function
);
3236 // Add a function declaration.
3238 add_function_declaration(const std::string
& name
, const Package
* package
,
3239 Function_type
* type
, Location location
);
3241 // Add a package. The location is the location of the import
3244 add_package(const std::string
& alias
, Package
* package
)
3246 Named_object
* no
= Named_object::make_package(alias
, package
);
3247 return this->add_named_object(no
);
3250 // Define a type which was already declared.
3252 define_type(Named_object
*, Named_type
*);
3254 // Add a method to the list of objects. This is not added to the
3257 add_method(Named_object
*);
3259 // Add a named object to this binding.
3261 add_named_object(Named_object
* no
)
3262 { return this->add_named_object_to_contour(&this->bindings_
, no
); }
3264 // Clear all names in file scope from the bindings.
3266 clear_file_scope(Gogo
*);
3268 // Look up a name in this binding contour and in any enclosing
3269 // binding contours. This returns NULL if the name is not found.
3271 lookup(const std::string
&) const;
3273 // Look up a name in this binding contour without looking in any
3274 // enclosing binding contours. Returns NULL if the name is not found.
3276 lookup_local(const std::string
&) const;
3280 remove_binding(Named_object
*);
3282 // Mark all variables as used. This is used for some types of parse
3287 // Traverse the tree. See the Traverse class.
3289 traverse(Traverse
*, bool is_global
);
3291 // Determine types for the objects.
3293 determine_types(Gogo
*);
3295 // Iterate over definitions. This does not include things which
3296 // were only declared.
3298 typedef std::vector
<Named_object
*>::const_iterator
3299 const_definitions_iterator
;
3301 const_definitions_iterator
3302 begin_definitions() const
3303 { return this->named_objects_
.begin(); }
3305 const_definitions_iterator
3306 end_definitions() const
3307 { return this->named_objects_
.end(); }
3309 // Return the number of definitions.
3311 size_definitions() const
3312 { return this->named_objects_
.size(); }
3314 // Return whether there are no definitions.
3316 empty_definitions() const
3317 { return this->named_objects_
.empty(); }
3319 // Iterate over declarations. This is everything that has been
3320 // declared, which includes everything which has been defined.
3322 typedef Contour::const_iterator const_declarations_iterator
;
3324 const_declarations_iterator
3325 begin_declarations() const
3326 { return this->bindings_
.begin(); }
3328 const_declarations_iterator
3329 end_declarations() const
3330 { return this->bindings_
.end(); }
3332 // Return the number of declarations.
3334 size_declarations() const
3335 { return this->bindings_
.size(); }
3337 // Return whether there are no declarations.
3339 empty_declarations() const
3340 { return this->bindings_
.empty(); }
3342 // Return the first declaration.
3345 { return this->bindings_
.empty() ? NULL
: this->bindings_
.begin()->second
; }
3347 // Dump to stderr for debugging
3352 add_named_object_to_contour(Contour
*, Named_object
*);
3355 new_definition(Named_object
*, Named_object
*);
3357 // Enclosing bindings.
3358 Bindings
* enclosing_
;
3359 // The list of objects.
3360 std::vector
<Named_object
*> named_objects_
;
3361 // The mapping from names to objects.
3370 Label(const std::string
& name
)
3371 : name_(name
), location_(Linemap::unknown_location()), snapshot_(NULL
),
3372 refs_(), is_used_(false), blabel_(NULL
), depth_(DEPTH_UNKNOWN
)
3375 // Return the label's name.
3378 { return this->name_
; }
3380 // Return whether the label has been defined.
3383 { return !Linemap::is_unknown_location(this->location_
); }
3385 // Return whether the label has been used.
3388 { return this->is_used_
; }
3390 // Record that the label is used.
3393 { this->is_used_
= true; }
3395 // Return whether this label is looping.
3398 { return this->depth_
== DEPTH_LOOPING
; }
3400 // Set this label as looping.
3403 { this->depth_
= DEPTH_LOOPING
; }
3405 // Return whether this label is nonlooping.
3408 { return this->depth_
== DEPTH_NONLOOPING
; }
3410 // Set this label as nonlooping.
3413 { this->depth_
= DEPTH_NONLOOPING
; }
3415 // Return the location of the definition.
3418 { return this->location_
; }
3420 // Return the bindings snapshot.
3423 { return this->snapshot_
; }
3425 // Add a snapshot of a goto which refers to this label.
3427 add_snapshot_ref(Bindings_snapshot
* snapshot
)
3429 go_assert(Linemap::is_unknown_location(this->location_
));
3430 this->refs_
.push_back(snapshot
);
3433 // Return the list of snapshots of goto statements which refer to
3435 const std::vector
<Bindings_snapshot
*>&
3437 { return this->refs_
; }
3439 // Clear the references.
3443 // Define the label at LOCATION with the given bindings snapshot.
3445 define(Location location
, Bindings_snapshot
* snapshot
)
3447 if (this->is_dummy_label())
3449 go_assert(Linemap::is_unknown_location(this->location_
)
3450 && this->snapshot_
== NULL
);
3451 this->location_
= location
;
3452 this->snapshot_
= snapshot
;
3455 // Return the backend representation for this label.
3457 get_backend_label(Translate_context
*);
3459 // Return an expression for the address of this label. This is used
3460 // to get the return address of a deferred function to see whether
3461 // the function may call recover.
3463 get_addr(Translate_context
*, Location location
);
3465 // Return a dummy label, representing any instance of the blank label.
3467 create_dummy_label();
3469 // Return TRUE if this is a dummy label.
3471 is_dummy_label() const
3472 { return this->name_
== "_"; }
3474 // A classification of a label's looping depth.
3478 // A label never jumped to.
3480 // A label jumped to.
3485 // The name of the label.
3487 // The location of the definition. This is 0 if the label has not
3488 // yet been defined.
3490 // A snapshot of the set of bindings defined at this label, used to
3491 // issue errors about invalid goto statements.
3492 Bindings_snapshot
* snapshot_
;
3493 // A list of snapshots of goto statements which refer to this label.
3494 std::vector
<Bindings_snapshot
*> refs_
;
3495 // Whether the label has been used.
3497 // The backend representation.
3499 // The looping depth of this label, for escape analysis.
3503 // An unnamed label. These are used when lowering loops.
3508 Unnamed_label(Location location
)
3509 : location_(location
), derived_from_(NULL
), blabel_(NULL
)
3512 // Get the location where the label is defined.
3515 { return this->location_
; }
3517 // Set the location where the label is defined.
3519 set_location(Location location
)
3520 { this->location_
= location
; }
3522 // Get the top level statement this unnamed label is derived from.
3524 derived_from() const
3525 { return this->derived_from_
; }
3527 // Set the top level statement this unnamed label is derived from.
3529 set_derived_from(Statement
* s
)
3530 { this->derived_from_
= s
; }
3532 // Return a statement which defines this label.
3534 get_definition(Translate_context
*);
3536 // Return a goto to this label from LOCATION.
3538 get_goto(Translate_context
*, Location location
);
3541 // Return the backend representation.
3543 get_blabel(Translate_context
*);
3545 // The location where the label is defined.
3547 // The top-level statement this unnamed label was derived/lowered from.
3548 // This is NULL is this label is not the top-level of a lowered statement.
3549 Statement
* derived_from_
;
3550 // The backend representation of this label.
3554 // An alias for an imported package.
3559 Package_alias(Location location
)
3560 : location_(location
), used_(0)
3563 // The location of the import statement.
3566 { return this->location_
; }
3568 // How many symbols from the package were used under this alias.
3571 { return this->used_
; }
3573 // Note that some symbol was used under this alias.
3579 // The location of the import statement.
3581 // The amount of times some name from this package was used under this alias.
3585 // An imported package.
3590 Package(const std::string
& pkgpath
, const std::string
& pkgpath_symbol
,
3593 // Get the package path used for all symbols exported from this
3597 { return this->pkgpath_
; }
3599 // Return the package path to use for a symbol name.
3601 pkgpath_symbol() const;
3603 // Set the package path symbol.
3605 set_pkgpath_symbol(const std::string
&);
3607 // Return the location of the most recent import statement.
3610 { return this->location_
; }
3612 // Return whether we know the name of this package yet.
3614 has_package_name() const
3615 { return !this->package_name_
.empty(); }
3617 // The name that this package uses in its package clause. This may
3618 // be different from the name in the associated Named_object if the
3619 // import statement used an alias.
3621 package_name() const
3623 go_assert(!this->package_name_
.empty());
3624 return this->package_name_
;
3627 // Return the bindings.
3630 { return this->bindings_
; }
3632 // Type used to map import names to package aliases.
3633 typedef std::map
<std::string
, Package_alias
*> Aliases
;
3635 // Return the set of package aliases.
3638 { return this->aliases_
; }
3640 // Note that some symbol from this package was used and qualified by ALIAS.
3641 // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3643 note_usage(const std::string
& alias
) const;
3645 // Note that USAGE might be a fake usage of this package.
3647 note_fake_usage(Expression
* usage
) const
3648 { this->fake_uses_
.insert(usage
); }
3650 // Forget a given USAGE of this package.
3652 forget_usage(Expression
* usage
) const;
3654 // Clear the used field for the next file.
3658 // Look up a name in the package. Returns NULL if the name is not
3661 lookup(const std::string
& name
) const
3662 { return this->bindings_
->lookup(name
); }
3664 // Set the name of the package.
3666 set_package_name(const std::string
& name
, Location
);
3668 // Set the location of the package. This is used to record the most
3669 // recent import location.
3671 set_location(Location location
)
3672 { this->location_
= location
; }
3674 // Add a package name as an ALIAS for this package.
3676 add_alias(const std::string
& alias
, Location
);
3678 // Add a constant to the package.
3680 add_constant(const Typed_identifier
& tid
, Expression
* expr
)
3681 { return this->bindings_
->add_constant(tid
, this, expr
, 0); }
3683 // Add a type to the package.
3685 add_type(const std::string
& name
, Type
* type
, Location location
)
3686 { return this->bindings_
->add_type(name
, this, type
, location
); }
3688 // Add a type declaration to the package.
3690 add_type_declaration(const std::string
& name
, Location location
)
3691 { return this->bindings_
->add_type_declaration(name
, this, location
); }
3693 // Add a variable to the package.
3695 add_variable(const std::string
& name
, Variable
* variable
)
3696 { return this->bindings_
->add_variable(name
, this, variable
); }
3698 // Add a function declaration to the package.
3700 add_function_declaration(const std::string
& name
, Function_type
* type
,
3702 { return this->bindings_
->add_function_declaration(name
, this, type
, loc
); }
3704 // Determine types of constants.
3706 determine_types(Gogo
*);
3709 // The package path for type reflection data.
3710 std::string pkgpath_
;
3711 // The package path for symbol names.
3712 std::string pkgpath_symbol_
;
3713 // The name that this package uses in the package clause. This may
3714 // be the empty string if it is not yet known.
3715 std::string package_name_
;
3716 // The names in this package.
3717 Bindings
* bindings_
;
3718 // The location of the most recent import statement.
3720 // The set of aliases associated with this package.
3722 // A set of possibly fake uses of this package. This is mutable because we
3723 // can track fake uses of a package even if we have a const pointer to it.
3724 mutable std::set
<Expression
*> fake_uses_
;
3727 // Return codes for the traversal functions. This is not an enum
3728 // because we want to be able to declare traversal functions in other
3729 // header files without including this one.
3731 // Continue traversal as usual.
3732 const int TRAVERSE_CONTINUE
= -1;
3735 const int TRAVERSE_EXIT
= 0;
3737 // Continue traversal, but skip components of the current object.
3738 // E.g., if this is returned by Traverse::statement, we do not
3739 // traverse the expressions in the statement even if
3740 // traverse_expressions is set in the traverse_mask.
3741 const int TRAVERSE_SKIP_COMPONENTS
= 1;
3743 // This class is used when traversing the parse tree. The caller uses
3744 // a subclass which overrides functions as desired.
3749 // These bitmasks say what to traverse.
3750 static const unsigned int traverse_variables
= 0x1;
3751 static const unsigned int traverse_constants
= 0x2;
3752 static const unsigned int traverse_functions
= 0x4;
3753 static const unsigned int traverse_blocks
= 0x8;
3754 static const unsigned int traverse_statements
= 0x10;
3755 static const unsigned int traverse_expressions
= 0x20;
3756 static const unsigned int traverse_types
= 0x40;
3757 static const unsigned int traverse_func_declarations
= 0x80;
3759 Traverse(unsigned int traverse_mask
)
3760 : traverse_mask_(traverse_mask
), types_seen_(NULL
), expressions_seen_(NULL
)
3763 virtual ~Traverse();
3765 // The bitmask of what to traverse.
3767 traverse_mask() const
3768 { return this->traverse_mask_
; }
3770 // Record that we are going to traverse a type. This returns true
3771 // if the type has already been seen in this traversal. This is
3772 // required because types, unlike expressions, can form a circular
3775 remember_type(const Type
*);
3777 // Record that we are going to see an expression. This returns true
3778 // if the expression has already been seen in this traversal. This
3779 // is only needed for cases where multiple expressions can point to
3782 remember_expression(const Expression
*);
3784 // These functions return one of the TRAVERSE codes defined above.
3786 // If traverse_variables is set in the mask, this is called for
3787 // every variable in the tree.
3789 variable(Named_object
*);
3791 // If traverse_constants is set in the mask, this is called for
3792 // every named constant in the tree. The bool parameter is true for
3793 // a global constant.
3795 constant(Named_object
*, bool);
3797 // If traverse_functions is set in the mask, this is called for
3798 // every function in the tree.
3800 function(Named_object
*);
3802 // If traverse_blocks is set in the mask, this is called for every
3803 // block in the tree.
3807 // If traverse_statements is set in the mask, this is called for
3808 // every statement in the tree.
3810 statement(Block
*, size_t* index
, Statement
*);
3812 // If traverse_expressions is set in the mask, this is called for
3813 // every expression in the tree.
3815 expression(Expression
**);
3817 // If traverse_types is set in the mask, this is called for every
3818 // type in the tree.
3822 // If traverse_func_declarations is set in the mask, this is called
3823 // for every function declarations in the tree.
3825 function_declaration(Named_object
*);
3828 // A hash table for types we have seen during this traversal. Note
3829 // that this uses the default hash functions for pointers rather
3830 // than Type_hash_identical and Type_identical. This is because for
3831 // traversal we care about seeing a specific type structure. If
3832 // there are two separate instances of identical types, we want to
3834 typedef Unordered_set(const Type
*) Types_seen
;
3836 typedef Unordered_set(const Expression
*) Expressions_seen
;
3838 // Bitmask of what sort of objects to traverse.
3839 unsigned int traverse_mask_
;
3840 // Types which have been seen in this traversal.
3841 Types_seen
* types_seen_
;
3842 // Expressions which have been seen in this traversal.
3843 Expressions_seen
* expressions_seen_
;
3846 // This class looks for interface types to finalize methods of inherited
3849 class Finalize_methods
: public Traverse
3852 Finalize_methods(Gogo
* gogo
)
3853 : Traverse(traverse_types
),
3864 // A class which makes it easier to insert new statements before the
3865 // current statement during a traversal.
3867 class Statement_inserter
3870 typedef Unordered_set(Statement
*) Statements
;
3872 // Empty constructor.
3873 Statement_inserter()
3874 : block_(NULL
), pindex_(NULL
), gogo_(NULL
), var_(NULL
),
3875 statements_added_(NULL
)
3878 // Constructor for a statement in a block.
3879 Statement_inserter(Block
* block
, size_t *pindex
, Statements
*added
= NULL
)
3880 : block_(block
), pindex_(pindex
), gogo_(NULL
), var_(NULL
),
3881 statements_added_(added
)
3884 // Constructor for a global variable.
3885 Statement_inserter(Gogo
* gogo
, Variable
* var
, Statements
*added
= NULL
)
3886 : block_(NULL
), pindex_(NULL
), gogo_(gogo
), var_(var
),
3887 statements_added_(added
)
3888 { go_assert(var
->is_global()); }
3890 // We use the default copy constructor and assignment operator.
3892 // Insert S before the statement we are traversing, or before the
3893 // initialization expression of a global variable.
3895 insert(Statement
* s
);
3898 // The block that the statement is in.
3900 // The index of the statement that we are traversing.
3902 // The IR, needed when looking at an initializer expression for a
3905 // The global variable, when looking at an initializer expression.
3907 // If non-null, a set to record new statements inserted (non-owned).
3908 Statements
* statements_added_
;
3911 // When translating the gogo IR into the backend data structure, this
3912 // is the context we pass down the blocks and statements.
3914 class Translate_context
3917 Translate_context(Gogo
* gogo
, Named_object
* function
, Block
* block
,
3919 : gogo_(gogo
), backend_(gogo
->backend()), function_(function
),
3920 block_(block
), bblock_(bblock
), is_const_(false)
3927 { return this->gogo_
; }
3931 { return this->backend_
; }
3935 { return this->function_
; }
3939 { return this->block_
; }
3943 { return this->bblock_
; }
3947 { return this->is_const_
; }
3949 // Make a constant context.
3952 { this->is_const_
= true; }
3955 // The IR for the entire compilation unit.
3957 // The generator for the backend data structures.
3959 // The function we are currently translating. NULL if not in a
3960 // function, e.g., the initializer of a global variable.
3961 Named_object
* function_
;
3962 // The block we are currently translating. NULL if not in a
3965 // The backend representation of the current block. NULL if block_
3968 // Whether this is being evaluated in a constant context. This is
3969 // used for type descriptor initializers.
3973 // This is used by some of the langhooks.
3974 extern Gogo
* go_get_gogo();
3976 // Whether we have seen any errors. FIXME: Replace with a backend
3978 extern bool saw_errors();
3980 // For use in the debugger
3981 extern void debug_go_gogo(Gogo
*);
3982 extern void debug_go_named_object(Named_object
*);
3983 extern void debug_go_bindings(Bindings
*);
3986 #endif // !defined(GO_GOGO_H)