1 // gogo.h -- Go frontend parsed representation. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
10 #include "go-linemap.h"
13 class Statement_inserter
;
16 class Typed_identifier
;
17 class Typed_identifier_list
;
20 class Expression_list
;
22 class Temporary_statement
;
26 class Bindings_snapshot
;
32 class Struct_field_list
;
38 class Forward_declaration_type
;
41 class Translate_context
;
44 class Export_function_body
;
46 class Import_function_body
;
57 // This file declares the basic classes used to hold the internal
58 // representation of Go which is built by the parser.
60 // The name of some backend object. Backend objects have a
61 // user-visible name and an assembler name. The user visible name
62 // might include arbitrary Unicode characters. The assembler name
69 : prefix_(NULL
), components_(), count_(0), suffix_(),
70 is_asm_name_(false), is_non_identifier_(false)
73 // Set the prefix. Prefixes are always constant strings.
75 set_prefix(const char* p
)
77 go_assert(this->prefix_
== NULL
&& !this->is_asm_name_
);
83 set_suffix(const std::string
& s
)
85 go_assert(this->suffix_
.empty() && !this->is_asm_name_
);
89 // Append to the suffix.
91 append_suffix(const std::string
& s
)
93 if (this->is_asm_name_
)
94 this->components_
[0].append(s
);
96 this->suffix_
.append(s
);
101 add(const std::string
& c
)
103 go_assert(this->count_
< Backend_name::max_components
104 && !this->is_asm_name_
);
105 this->components_
[this->count_
] = c
;
109 // Set an assembler name specified by the user. This overrides both
110 // the user-visible name and the assembler name. No further
111 // encoding is applied.
113 set_asm_name(const std::string
& n
)
115 go_assert(this->prefix_
== NULL
117 && this->suffix_
.empty()
118 && !this->is_asm_name_
);
119 this->components_
[0] = n
;
120 this->is_asm_name_
= true;
123 // Whether some component includes some characters that can't appear
126 is_non_identifier() const
127 { return this->is_non_identifier_
; }
129 // Record that some component includes some character that can't
130 // appear in an identifier.
132 set_is_non_identifier()
133 { this->is_non_identifier_
= true; }
135 // Get the user visible name.
139 // Get the assembler name. This may be the same as the user visible
144 // Get an optional assembler name: if it would be the same as the
145 // user visible name, this returns the empty string.
147 optional_asm_name() const;
150 // The maximum number of components.
151 static const int max_components
= 4;
153 // An optional prefix that does not require encoding.
155 // Up to four components. The name will include these components
156 // separated by dots. Each component will be underscore-encoded
157 // (see the long comment near the top of names.cc).
158 std::string components_
[Backend_name::max_components
];
159 // Number of components.
161 // An optional suffix that does not require encoding.
163 // True if components_[0] is an assembler name specified by the user.
165 // True if some component includes some character that can't
166 // normally appear in an identifier.
167 bool is_non_identifier_
;
170 // An initialization function for an imported package. This is a
171 // magic function which initializes variables and runs the "init"
177 Import_init(const std::string
& package_name
, const std::string
& init_name
,
179 : package_name_(package_name
), init_name_(init_name
), priority_(priority
)
182 // The name of the package being imported.
185 { return this->package_name_
; }
187 // The name of the package's init function.
190 { return this->init_name_
; }
192 // Older V1 export data uses a priority scheme to order
193 // initialization functions; functions with a lower priority number
194 // must be run first. This value will be set to -1 for current
195 // generation objects, and will take on a non-negative value only
196 // when importing a V1-vintage object.
199 { return this->priority_
; }
203 set_priority(int new_priority
)
204 { this->priority_
= new_priority
; }
206 // Record the fact that some other init fcn must be run before this init fcn.
208 record_precursor_fcn(std::string init_fcn_name
)
209 { this->precursor_functions_
.insert(init_fcn_name
); }
211 // Return the list of precursor fcns for this fcn (must be run before it).
212 const std::set
<std::string
>&
214 { return this->precursor_functions_
; }
216 // Whether this is a dummy init, which is used only to record transitive import.
219 { return this->init_name_
[0] == '~'; }
222 // The name of the package being imported.
223 std::string package_name_
;
224 // The name of the package's init function.
225 std::string init_name_
;
226 // Names of init functions that must be run before this fcn.
227 std::set
<std::string
> precursor_functions_
;
228 // Priority for this function. See note above on obsolescence.
232 // For sorting purposes.
234 struct Import_init_lt
{
235 bool operator()(const Import_init
* i1
, const Import_init
* i2
) const
237 return i1
->init_name() < i2
->init_name();
241 // Set of import init objects.
242 class Import_init_set
: public std::set
<Import_init
*, Import_init_lt
> {
246 priority_compare(const Import_init
* i1
, const Import_init
* i2
)
248 if (i1
->priority() < i2
->priority())
250 if (i1
->priority() > i2
->priority())
252 if (i1
->package_name() != i2
->package_name())
253 return i1
->package_name() < i2
->package_name();
254 return i1
->init_name() < i2
->init_name();
257 // The holder for the internal representation of the entire
263 // Create the IR, passing in the sizes of the types "int" and
264 // "uintptr" in bits.
265 Gogo(Backend
* backend
, Linemap
*linemap
, int int_type_size
, int pointer_size
);
267 // Get the backend generator.
270 { return this->backend_
; }
272 // Get the Location generator.
275 { return this->linemap_
; }
277 // Get the package name.
279 package_name() const;
281 // Set the package name.
283 set_package_name(const std::string
&, Location
);
285 // Return whether this is the "main" package.
287 is_main_package() const;
289 // If necessary, adjust the name to use for a hidden symbol. We add
290 // the package name, so that hidden symbols in different packages do
293 pack_hidden_name(const std::string
& name
, bool is_exported
) const
297 : '.' + this->pkgpath() + '.' + name
);
300 // Unpack a name which may have been hidden. Returns the
301 // user-visible name of the object.
303 unpack_hidden_name(const std::string
& name
)
304 { return name
[0] != '.' ? name
: name
.substr(name
.rfind('.') + 1); }
306 // Return whether a possibly packed name is hidden.
308 is_hidden_name(const std::string
& name
)
309 { return name
[0] == '.'; }
311 // Return the package path of a hidden name.
313 hidden_name_pkgpath(const std::string
& name
)
315 go_assert(Gogo::is_hidden_name(name
));
316 return name
.substr(1, name
.rfind('.') - 1);
319 // Given a name which may or may not have been hidden, append the
320 // appropriate version of the name to the result string.
322 append_possibly_hidden_name(std::string
*result
, const std::string
& name
);
324 // Given a name which may or may not have been hidden, return the
325 // name to use in an error message.
327 message_name(const std::string
& name
);
329 // Return whether a name is the blank identifier _.
331 is_sink_name(const std::string
& name
)
333 return (name
[0] == '.'
334 && name
[name
.length() - 1] == '_'
335 && name
[name
.length() - 2] == '.')
337 && name
.length() == 1);
340 // Helper used when adding parameters (including receiver param) to the
341 // bindings of a function. If the specified parameter name is empty or
342 // corresponds to the sink name, param name is replaced with a new unique
343 // name. PNAME is the address of a string containing the parameter variable
344 // name to be checked/updated; TAG is a descriptive tag to be used in
345 // manufacturing the new unique name, and COUNT is the address of a counter
346 // holding the number of params renamed so far with the tag in question.
348 rename_if_empty(std::string
* pname
, const char* tag
, unsigned* count
);
350 // Convert a pkgpath into a string suitable for a symbol
352 pkgpath_for_symbol(const std::string
& pkgpath
);
354 // Compute a hash code for a string, given a seed.
356 hash_string(const std::string
&, unsigned int);
358 // Return the package path to use for reflect.Type.PkgPath.
362 // Return the package path to use for a symbol name.
364 pkgpath_symbol() const;
366 // Set the package path from a command line option.
368 set_pkgpath(const std::string
&);
370 // Set the prefix from a command line option.
372 set_prefix(const std::string
&);
374 // Return whether pkgpath was set from a command line option.
376 pkgpath_from_option() const
377 { return this->pkgpath_from_option_
; }
379 // Return the relative import path as set from the command line.
380 // Returns an empty string if it was not set.
382 relative_import_path() const
383 { return this->relative_import_path_
; }
385 // Set the relative import path from a command line option.
387 set_relative_import_path(const std::string
& s
)
388 { this->relative_import_path_
= s
; }
390 // Set the C header file to write. This is used for the runtime
393 set_c_header(const std::string
& s
)
394 { this->c_header_
= s
; }
396 // Read an embedcfg file.
398 read_embedcfg(const char* filename
);
400 // Return whether to check for division by zero in binary operations.
402 check_divide_by_zero() const
403 { return this->check_divide_by_zero_
; }
405 // Set the option to check division by zero from a command line option.
407 set_check_divide_by_zero(bool b
)
408 { this->check_divide_by_zero_
= b
; }
410 // Return whether to check for division overflow in binary operations.
412 check_divide_overflow() const
413 { return this->check_divide_overflow_
; }
415 // Set the option to check division overflow from a command line option.
417 set_check_divide_overflow(bool b
)
418 { this->check_divide_overflow_
= b
; }
420 // Return whether we are compiling the runtime package.
422 compiling_runtime() const
423 { return this->compiling_runtime_
; }
425 // Set whether we are compiling the runtime package.
427 set_compiling_runtime(bool b
)
428 { this->compiling_runtime_
= b
; }
430 // Return the level of escape analysis debug information to emit.
432 debug_escape_level() const
433 { return this->debug_escape_level_
; }
435 // Set the level of escape analysis debugging from a command line option.
437 set_debug_escape_level(int level
)
438 { this->debug_escape_level_
= level
; }
440 // Return the hash for debug escape analysis.
442 debug_escape_hash() const
443 { return this->debug_escape_hash_
; }
445 // Set the hash value for debug escape analysis.
447 set_debug_escape_hash(const std::string
& s
)
448 { this->debug_escape_hash_
= s
; }
450 // Return whether to output optimization diagnostics.
452 debug_optimization() const
453 { return this->debug_optimization_
; }
455 // Set the option to output optimization diagnostics.
457 set_debug_optimization(bool b
)
458 { this->debug_optimization_
= b
; }
460 // Dump to stderr for debugging
463 // Return the size threshold used to determine whether to issue
464 // a nil-check for a given pointer dereference. A threshold of -1
465 // implies that all potentially faulting dereference ops should
466 // be nil-checked. A positive threshold of N implies that a deref
467 // of *P where P has size less than N doesn't need a nil check.
469 nil_check_size_threshold() const
470 { return this->nil_check_size_threshold_
; }
472 // Set the nil-check size threshold, as described above.
474 set_nil_check_size_threshold(int64_t bytes
)
475 { this->nil_check_size_threshold_
= bytes
; }
477 // Return whether runtime.eqtype calls are needed when comparing
481 { return this->need_eqtype_
; }
483 // Set if calls to runtime.eqtype are needed.
485 set_need_eqtype(bool b
)
486 { this->need_eqtype_
= b
; }
488 // Import a package. FILENAME is the file name argument, LOCAL_NAME
489 // is the local name to give to the package. If LOCAL_NAME is empty
490 // the declarations are added to the global scope.
492 import_package(const std::string
& filename
, const std::string
& local_name
,
493 bool is_local_name_exported
, bool must_exist
, Location
);
495 // Whether we are the global binding level.
497 in_global_scope() const;
499 // Look up a name in the current binding contours.
501 lookup(const std::string
&, Named_object
** pfunction
) const;
503 // Look up a name in the current block.
505 lookup_in_block(const std::string
&) const;
507 // Look up a name in the global namespace--the universal scope.
509 lookup_global(const char*) const;
511 // Add a new imported package. REAL_NAME is the real name of the
512 // package. ALIAS is the alias of the package; this may be the same
513 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
514 // this package should be added to the global namespace; this is
515 // true if the alias is ".". LOCATION is the location of the import
516 // statement. This returns the new package, or NULL on error.
518 add_imported_package(const std::string
& real_name
, const std::string
& alias
,
519 bool is_alias_exported
,
520 const std::string
& pkgpath
,
521 const std::string
& pkgpath_symbol
,
523 bool* padd_to_globals
);
525 // Register a package. This package may or may not be imported.
526 // This returns the Package structure for the package, creating if
529 register_package(const std::string
& pkgpath
,
530 const std::string
& pkgpath_symbol
, Location
);
532 // Look up a package by pkgpath, and return its pkgpath_symbol.
534 pkgpath_symbol_for_package(const std::string
&);
536 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
537 // method function should be added to the type of its receiver.
539 start_function(const std::string
& name
, Function_type
* type
,
540 bool add_method_to_type
, Location
);
542 // Finish compiling a function.
544 finish_function(Location
);
546 // Return the current function.
548 current_function() const;
550 // Return the current block.
554 // Start a new block. This is not initially associated with a
557 start_block(Location
);
559 // Finish the current block and return it.
561 finish_block(Location
);
563 // Declare an erroneous name. This is used to avoid knock-on errors
564 // after a parsing error.
566 add_erroneous_name(const std::string
& name
);
568 // Declare an unknown name. This is used while parsing. The name
569 // must be resolved by the end of the parse. Unknown names are
570 // always added at the package level.
572 add_unknown_name(const std::string
& name
, Location
);
574 // Declare a function.
576 declare_function(const std::string
&, Function_type
*, Location
);
578 // Declare a function at the package level. This is used for
579 // functions generated for a type.
581 declare_package_function(const std::string
&, Function_type
*, Location
);
583 // Add a function declaration to the list of functions we may want
586 add_imported_inlinable_function(Named_object
*);
588 // Add a function to the list of functions that we do want to
591 add_imported_inline_function(Named_object
* no
)
592 { this->imported_inline_functions_
.push_back(no
); }
596 add_label_definition(const std::string
&, Location
);
598 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
599 // report errors for a goto from the current location to the label
602 add_label_reference(const std::string
&, Location
,
603 bool issue_goto_errors
);
605 // An analysis set is a list of functions paired with a boolean that indicates
606 // whether the list of functions are recursive.
607 typedef std::pair
<std::vector
<Named_object
*>, bool> Analysis_set
;
609 // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
612 add_analysis_set(const std::vector
<Named_object
*>& group
, bool recursive
)
613 { this->analysis_sets_
.push_back(std::make_pair(group
, recursive
)); }
615 // Return a snapshot of the current binding state.
617 bindings_snapshot(Location
);
619 // Add a statement to the current block.
621 add_statement(Statement
*);
623 // Add a block to the current block.
625 add_block(Block
*, Location
);
629 add_constant(const Typed_identifier
&, Expression
*, int iota_value
);
633 add_type(const std::string
&, Type
*, Location
);
635 // Add a named type. This is used for builtin types, and to add an
636 // imported type to the global scope.
638 add_named_type(Named_type
*);
642 declare_type(const std::string
&, Location
);
644 // Declare a type at the package level. This is used when the
645 // parser sees an unknown name where a type name is required.
647 declare_package_type(const std::string
&, Location
);
649 // Define a type which was already declared.
651 define_type(Named_object
*, Named_type
*);
655 add_variable(const std::string
&, Variable
*);
657 // Add a sink--a reference to the blank identifier _.
661 // Add a type which needs to be verified. This is used for sink
662 // types, just to give appropriate error messages.
664 add_type_to_verify(Type
* type
);
666 // Add a named object to the current namespace. This is used for
667 // import . "package".
669 add_dot_import_object(Named_object
*);
671 // Add an identifier to the list of names seen in the file block.
673 add_file_block_name(const std::string
& name
, Location location
)
674 { this->file_block_names_
[name
] = location
; }
676 // Add a linkname, from the go:linkname compiler directive. This
677 // changes the externally visible name of GO_NAME to be EXT_NAME.
678 // If EXT_NAME is the empty string, GO_NAME is unchanged, but the
679 // symbol is made publicly visible.
681 add_linkname(const std::string
& go_name
, bool is_exported
,
682 const std::string
& ext_name
, Location location
);
684 // Mark all local variables in current bindings as used. This is
685 // used when there is a parse error to avoid useless errors.
689 // Note that we've seen an interface type. This is used to build
690 // all required interface method tables.
692 record_interface_type(Interface_type
*);
694 // Note that we need an initialization function.
697 { this->need_init_fn_
= true; }
699 // Return whether the current file imported the unsafe package.
701 current_file_imported_unsafe() const
702 { return this->current_file_imported_unsafe_
; }
704 // Clear out all names in file scope. This is called when we start
705 // parsing a new file.
709 // Record that VAR1 must be initialized after VAR2. This is used
710 // when VAR2 does not appear in VAR1's INIT or PREINIT.
712 record_var_depends_on(Variable
* var1
, Named_object
* var2
)
714 go_assert(this->var_deps_
.find(var1
) == this->var_deps_
.end());
715 this->var_deps_
[var1
] = var2
;
718 // Return the variable that VAR depends on, or NULL if none.
720 var_depends_on(Variable
* var
) const
722 Var_deps::const_iterator p
= this->var_deps_
.find(var
);
723 return p
!= this->var_deps_
.end() ? p
->second
: NULL
;
726 // Queue up a type-specific hash function to be written out. This
727 // is used when a type-specific hash function is needed when not at
730 queue_hash_function(Type
* type
, int64_t size
, Backend_name
*,
731 Function_type
* hash_fntype
);
733 // Queue up a type-specific equal function to be written out. This
734 // is used when a type-specific equal function is needed when not at
737 queue_equal_function(Type
* type
, Named_type
* name
, int64_t size
,
738 Backend_name
*, Function_type
* equal_fntype
);
740 // Write out queued specific type functions.
742 write_specific_type_functions();
744 // Whether we are done writing out specific type functions.
746 specific_type_functions_are_written() const
747 { return this->specific_type_functions_are_written_
; }
749 // Add a pointer that needs to be added to the list of objects
750 // traversed by the garbage collector. This should be an expression
751 // of pointer type that points to static storage. It's not
752 // necessary to add global variables to this list, just global
753 // variable initializers that would otherwise not be seen.
755 add_gc_root(Expression
* expr
)
757 this->set_need_init_fn();
758 this->gc_roots_
.push_back(expr
);
761 // Add a type to the descriptor list.
763 add_type_descriptor(Type
* type
)
764 { this->type_descriptors_
.push_back(type
); }
766 // Traverse the tree. See the Traverse class.
770 // Define the predeclared global names.
772 define_global_names();
774 // Verify and complete all types.
778 // Lower the parse tree.
782 // Lower all the statements in a block.
784 lower_block(Named_object
* function
, Block
*);
786 // Lower an expression.
788 lower_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
792 lower_constant(Named_object
*);
794 // Flatten all the statements in a block.
796 flatten_block(Named_object
* function
, Block
*);
798 // Flatten an expression.
800 flatten_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
802 // Create all necessary function descriptors.
804 create_function_descriptors();
806 // Finalize the method lists and build stub methods for named types.
810 // Finalize the method list for one type.
812 finalize_methods_for_type(Type
*);
814 // Work out the types to use for unspecified variables and
819 // Type check the program.
823 // Check the types in a single block. This is used for complicated
826 check_types_in_block(Block
*);
828 // Check for return statements.
830 check_return_statements();
836 // Make implicit type conversions explicit.
840 // Make implicit type conversions explicit in a block.
842 add_conversions_in_block(Block
*);
844 // Analyze the program flow for escape information.
848 // Discover the groups of possibly recursive functions in this package.
850 discover_analysis_sets();
852 // Build a connectivity graph between the objects in each analyzed function.
854 assign_connectivity(Escape_context
*, Named_object
*);
856 // Traverse the objects in the connecitivty graph from the sink, adjusting the
857 // escape levels of each object.
859 propagate_escape(Escape_context
*, Node
*);
861 // Add notes about the escape level of a function's input and output
862 // parameters for exporting and importing top level functions.
864 tag_function(Escape_context
*, Named_object
*);
866 // Reclaim memory of escape analysis Nodes.
868 reclaim_escape_nodes();
874 // Add an import control function for an imported package to the
877 add_import_init_fn(const std::string
& package_name
,
878 const std::string
& init_name
, int prio
);
880 // Return the Import_init for a given init name.
882 lookup_init(const std::string
& init_name
);
884 // Turn short-cut operators (&&, ||) into explicit if statements.
888 // Turn short-cut operators into explicit if statements in a block.
890 remove_shortcuts_in_block(Block
*);
892 // Use temporary variables to force order of evaluation.
896 // Order evaluations in a block.
900 // Add write barriers as needed.
902 add_write_barriers();
904 // Return whether an assignment that sets LHS to RHS needs a write
907 assign_needs_write_barrier(Expression
* lhs
,
908 Unordered_set(const Named_object
*)*);
910 // Return whether EXPR is the address of a variable that can be set
911 // without a write barrier. That is, if this returns true, then an
912 // assignment to *EXPR does not require a write barrier.
914 is_nonwb_pointer(Expression
* expr
, Unordered_set(const Named_object
*)*);
916 // Return an assignment that sets LHS to RHS using a write barrier.
917 // This returns an if statement that checks whether write barriers
918 // are enabled. If not, it does LHS = RHS, otherwise it calls the
919 // appropriate write barrier function.
921 assign_with_write_barrier(Function
*, Block
*, Statement_inserter
*,
922 Expression
* lhs
, Expression
* rhs
, Location
);
924 // Return a statement that tests whether write barriers are enabled
925 // and executes either the efficient code (WITHOUT) or the write
926 // barrier function call (WITH), depending.
928 check_write_barrier(Block
*, Statement
* without
, Statement
* with
);
930 // Flatten parse tree.
934 // Build thunks for functions which call recover.
936 build_recover_thunks();
938 // Simplify statements which might use thunks: go and defer
941 simplify_thunk_statements();
943 // Dump AST if -fgo-dump-ast is set.
945 dump_ast(const char* basename
);
947 // Dump Call Graph if -fgo-dump-calls is set.
949 dump_call_graph(const char* basename
);
951 // Dump Connection Graphs if -fgo-dump-connections is set.
953 dump_connection_graphs(const char* basename
);
955 // Convert named types to the backend representation.
957 convert_named_types();
959 // Convert named types in a list of bindings.
961 convert_named_types_in_bindings(Bindings
*);
963 // True if named types have been converted to the backend
966 named_types_are_converted() const
967 { return this->named_types_are_converted_
; }
969 // Give an error if the initialization of VAR depends on itself.
971 check_self_dep(Named_object
*);
973 // Write out the global values.
977 // Build required interface method tables.
979 build_interface_method_tables();
981 // Return an expression which allocates memory to hold values of type TYPE.
983 allocate_memory(Type
*type
, Location
);
985 // Get the backend name to use for an exported function, a method,
986 // or a function/method declaration.
988 function_backend_name(const std::string
& go_name
, const Package
*,
989 const Type
* receiver
, Backend_name
*);
991 // Return the name to use for a function descriptor.
993 function_descriptor_backend_name(Named_object
*, Backend_name
*);
995 // Return the name to use for a generated stub method.
997 stub_method_name(const Package
*, const std::string
& method_name
);
999 // Get the backend name of the hash function for TYPE.
1001 hash_function_name(const Type
*, Backend_name
*);
1003 // Get the backend name of the equal function for TYPE.
1005 equal_function_name(const Type
*, const Named_type
*, Backend_name
*);
1007 // Get the backend name to use for a global variable.
1009 global_var_backend_name(const std::string
& go_name
, const Package
*,
1012 // Return a name to use for an error case. This should only be used
1013 // after reporting an error, and is used to avoid useless knockon
1018 // Return whether the name indicates an error.
1020 is_erroneous_name(const std::string
&);
1022 // Return a name to use for a thunk function. A thunk function is
1023 // one we create during the compilation, for a go statement or a
1024 // defer statement or a method expression.
1028 // Return whether an object is a thunk.
1030 is_thunk(const Named_object
*);
1032 // Return the name to use for an init function.
1034 init_function_name();
1036 // Return the name to use for a nested function.
1038 nested_function_name(Named_object
* enclosing
);
1040 // Return the name to use for a sink funciton.
1042 sink_function_name();
1044 // Return the name to use for an (erroneous) redefined function.
1046 redefined_function_name();
1048 // Return the name for use for a recover thunk.
1050 recover_thunk_name(const std::string
& name
, const Type
* rtype
);
1052 // Return the name to use for the GC root variable.
1056 // Return the name to use for a composite literal or string
1061 // Return the name of the variable used to represent the zero value
1064 map_zero_value_name();
1066 // Get the name of the magic initialization function.
1070 // Return the name for a dummy init function, which is not a real
1071 // function but only for tracking transitive import.
1073 dummy_init_fn_name();
1075 // Return the package path symbol from an init function name, which
1076 // can be a real init function or a dummy one.
1078 pkgpath_symbol_from_init_fn_name(std::string
);
1080 // Get the backend name for a type descriptor symbol.
1082 type_descriptor_backend_name(const Type
*, Named_type
*, Backend_name
*);
1084 // Return the name of the type descriptor list symbol of a package.
1085 // The argument is an encoded pkgpath, as with pkgpath_symbol.
1087 type_descriptor_list_symbol(const std::string
&);
1089 // Return the name of the list of all type descriptor lists.
1093 // Return the assembler name for the GC symbol for a type.
1095 gc_symbol_name(Type
*);
1097 // Return the assembler name for a ptrmask variable.
1099 ptrmask_symbol_name(const std::string
& ptrmask_sym_name
);
1101 // Return the name to use for an interface method table.
1103 interface_method_table_name(Interface_type
*, Type
*, bool is_pointer
);
1105 // If NAME is a special name used as a Go identifier, return the
1106 // position within the string where the special part of the name
1109 special_name_pos(const std::string
& name
);
1112 // During parsing, we keep a stack of functions. Each function on
1113 // the stack is one that we are currently parsing. For each
1114 // function, we keep track of the current stack of blocks.
1115 struct Open_function
1118 Named_object
* function
;
1119 // The stack of active blocks in the function.
1120 std::vector
<Block
*> blocks
;
1123 // The stack of functions.
1124 typedef std::vector
<Open_function
> Open_functions
;
1126 // Set up the built-in unsafe package.
1128 import_unsafe(const std::string
&, bool is_exported
, Location
);
1130 // Return the current binding contour.
1135 current_bindings() const;
1140 // Get the decl for the magic initialization function.
1142 initialization_function_decl();
1144 // Create the magic initialization function.
1146 create_initialization_function(Named_object
* fndecl
, Bstatement
* code_stmt
);
1148 // Initialize imported packages. BFUNCTION is the function
1149 // into which the package init calls will be placed.
1151 init_imports(std::vector
<Bstatement
*>&, Bfunction
* bfunction
);
1153 // Register variables with the garbage collector.
1155 register_gc_vars(const std::vector
<Named_object
*>&,
1156 std::vector
<Bstatement
*>&,
1157 Bfunction
* init_bfunction
);
1159 // Build the list of type descriptors.
1161 build_type_descriptor_list();
1163 // Register the type descriptors with the runtime.
1165 register_type_descriptors(std::vector
<Bstatement
*>&,
1166 Bfunction
* init_bfunction
);
1169 propagate_writebarrierrec();
1172 write_barrier_variable();
1175 is_digits(const std::string
&);
1177 // Type used to map import names to packages.
1178 typedef std::map
<std::string
, Package
*> Imports
;
1180 // Type used to map package names to packages.
1181 typedef std::map
<std::string
, Package
*> Packages
;
1183 // Type used to map variables to the function calls that set them.
1184 // This is used for initialization dependency analysis.
1185 typedef std::map
<Variable
*, Named_object
*> Var_deps
;
1187 // Type used to map identifiers in the file block to the location
1188 // where they were defined.
1189 typedef Unordered_map(std::string
, Location
) File_block_names
;
1191 // Type used to queue writing a type specific function.
1192 struct Specific_type_function
1194 enum Specific_type_function_kind
{ SPECIFIC_HASH
, SPECIFIC_EQUAL
};
1199 Specific_type_function_kind kind
;
1201 Function_type
* fntype
;
1203 Specific_type_function(Type
* atype
, Named_type
* aname
, int64_t asize
,
1204 Specific_type_function_kind akind
,
1205 Backend_name
* abname
,
1206 Function_type
* afntype
)
1207 : type(atype
), name(aname
), size(asize
), kind(akind
),
1208 bname(*abname
), fntype(afntype
)
1212 // Recompute init priorities.
1214 recompute_init_priorities();
1216 // Recursive helper used by the routine above.
1218 update_init_priority(Import_init
* ii
,
1219 std::set
<const Import_init
*>* visited
);
1221 // The backend generator.
1223 // The object used to keep track of file names and line numbers.
1225 // The package we are compiling.
1227 // The list of currently open functions during parsing.
1228 Open_functions functions_
;
1229 // The global binding contour. This includes the builtin functions
1230 // and the package we are compiling.
1232 // The list of names we have seen in the file block.
1233 File_block_names file_block_names_
;
1234 // Mapping from import file names to packages.
1236 // Whether the magic unsafe package was imported.
1237 bool imported_unsafe_
;
1238 // Whether the magic unsafe package was imported by the current file.
1239 bool current_file_imported_unsafe_
;
1240 // Mapping from package names we have seen to packages. This does
1241 // not include the package we are compiling.
1243 // The functions named "init", if there are any.
1244 std::vector
<Named_object
*> init_functions_
;
1245 // A mapping from variables to the function calls that initialize
1246 // them, if it is not stored in the variable's init or preinit.
1247 // This is used for dependency analysis.
1249 // Whether we need a magic initialization function.
1251 // The name of the magic initialization function.
1252 std::string init_fn_name_
;
1253 // A list of import control variables for packages that we import.
1254 Import_init_set imported_init_fns_
;
1255 // The package path used for reflection data.
1256 std::string pkgpath_
;
1257 // The package path to use for a symbol name.
1258 std::string pkgpath_symbol_
;
1259 // The prefix to use for symbols, from the -fgo-prefix option.
1260 std::string prefix_
;
1261 // Whether pkgpath_ has been set.
1263 // Whether an explicit package path was set by -fgo-pkgpath.
1264 bool pkgpath_from_option_
;
1265 // Whether an explicit prefix was set by -fgo-prefix.
1266 bool prefix_from_option_
;
1267 // The relative import path, from the -fgo-relative-import-path
1269 std::string relative_import_path_
;
1270 // The C header file to write, from the -fgo-c-header option.
1271 std::string c_header_
;
1272 // Whether or not to check for division by zero, from the
1273 // -fgo-check-divide-zero option.
1274 bool check_divide_by_zero_
;
1275 // Whether or not to check for division overflow, from the
1276 // -fgo-check-divide-overflow option.
1277 bool check_divide_overflow_
;
1278 // Whether we are compiling the runtime package, from the
1279 // -fgo-compiling-runtime option.
1280 bool compiling_runtime_
;
1281 // The level of escape analysis debug information to emit, from the
1282 // -fgo-debug-escape option.
1283 int debug_escape_level_
;
1284 // A hash value for debug escape analysis, from the
1285 // -fgo-debug-escape-hash option. The analysis is run only on
1286 // functions with names that hash to the matching value.
1287 std::string debug_escape_hash_
;
1288 // Whether to output optimization diagnostics, from the
1289 // -fgo-debug-optimization option.
1290 bool debug_optimization_
;
1291 // Nil-check size threshhold.
1292 int64_t nil_check_size_threshold_
;
1293 // Whether runtime.eqtype calls are needed when comparing type
1296 // A list of types to verify.
1297 std::vector
<Type
*> verify_types_
;
1298 // A list of interface types defined while parsing.
1299 std::vector
<Interface_type
*> interface_types_
;
1300 // Type specific functions to write out.
1301 std::vector
<Specific_type_function
*> specific_type_functions_
;
1302 // Whether we are done writing out specific type functions.
1303 bool specific_type_functions_are_written_
;
1304 // Whether named types have been converted.
1305 bool named_types_are_converted_
;
1306 // A list containing groups of possibly mutually recursive functions to be
1307 // considered during escape analysis.
1308 std::vector
<Analysis_set
> analysis_sets_
;
1309 // A list of objects to add to the GC roots.
1310 std::vector
<Expression
*> gc_roots_
;
1311 // A list of type descriptors that we need to register.
1312 std::vector
<Type
*> type_descriptors_
;
1313 // A list of function declarations with imported bodies that we may
1315 std::vector
<Named_object
*> imported_inlinable_functions_
;
1316 // A list of functions that we want to inline. These will be sent
1318 std::vector
<Named_object
*> imported_inline_functions_
;
1321 // A block of statements.
1326 Block(Block
* enclosing
, Location
);
1328 // Return the enclosing block.
1331 { return this->enclosing_
; }
1333 // Return the bindings of the block.
1336 { return this->bindings_
; }
1340 { return this->bindings_
; }
1342 // Look at the block's statements.
1343 const std::vector
<Statement
*>*
1345 { return &this->statements_
; }
1347 // Return the start location. This is normally the location of the
1348 // left curly brace which starts the block.
1350 start_location() const
1351 { return this->start_location_
; }
1353 // Return the end location. This is normally the location of the
1354 // right curly brace which ends the block.
1356 end_location() const
1357 { return this->end_location_
; }
1359 // Add a statement to the block.
1361 add_statement(Statement
*);
1363 // Add a statement to the front of the block.
1365 add_statement_at_front(Statement
*);
1367 // Replace a statement in a block.
1369 replace_statement(size_t index
, Statement
*);
1371 // Add a Statement before statement number INDEX.
1373 insert_statement_before(size_t index
, Statement
*);
1375 // Add a Statement after statement number INDEX.
1377 insert_statement_after(size_t index
, Statement
*);
1379 // Set the end location of the block.
1381 set_end_location(Location location
)
1382 { this->end_location_
= location
; }
1384 // Traverse the tree.
1386 traverse(Traverse
*);
1388 // Set final types for unspecified variables and constants.
1392 // Return true if execution of this block may fall through to the
1395 may_fall_through() const;
1397 // Write the export data for the block's statements to the string.
1399 export_block(Export_function_body
*);
1401 // Turn exported block data into a block.
1403 import_block(Block
*, Import_function_body
*, Location
);
1405 // Convert the block to the backend representation.
1407 get_backend(Translate_context
*);
1409 // Iterate over statements.
1411 typedef std::vector
<Statement
*>::iterator iterator
;
1415 { return this->statements_
.begin(); }
1419 { return this->statements_
.end(); }
1424 // Statements in the block.
1425 std::vector
<Statement
*> statements_
;
1427 Bindings
* bindings_
;
1428 // Location of start of block.
1429 Location start_location_
;
1430 // Location of end of block.
1431 Location end_location_
;
1439 Function(Function_type
* type
, Named_object
*, Block
*, Location
);
1441 // Return the function's type.
1444 { return this->type_
; }
1446 // Return the enclosing function if there is one.
1449 { return this->enclosing_
; }
1451 // Set the enclosing function. This is used when building thunks
1452 // for functions which call recover.
1454 set_enclosing(Named_object
* enclosing
)
1456 go_assert(this->enclosing_
== NULL
);
1457 this->enclosing_
= enclosing
;
1460 // The result variables.
1461 typedef std::vector
<Named_object
*> Results
;
1463 // Create the result variables in the outer block.
1465 create_result_variables(Gogo
*);
1467 // Update the named result variables when cloning a function which
1470 update_result_variables();
1472 // Return the result variables.
1475 { return this->results_
; }
1479 { return this->is_sink_
; }
1483 { this->is_sink_
= true; }
1485 // Whether the result variables have names.
1487 results_are_named() const
1488 { return this->results_are_named_
; }
1490 // Return the assembler name.
1493 { return this->asm_name_
; }
1495 // Set the assembler name.
1497 set_asm_name(const std::string
& asm_name
)
1498 { this->asm_name_
= asm_name
; }
1500 // Mark this symbol as exported by a linkname directive.
1502 set_is_exported_by_linkname()
1503 { this->is_exported_by_linkname_
= true; }
1505 // Return the pragmas for this function.
1508 { return this->pragmas_
; }
1510 // Set the pragmas for this function.
1512 set_pragmas(unsigned int pragmas
)
1514 this->pragmas_
= pragmas
;
1517 // Return the index to use for a nested function.
1519 next_nested_function_index()
1521 ++this->nested_functions_
;
1522 return this->nested_functions_
;
1525 // Whether this method should not be included in the type
1528 nointerface() const;
1530 // Record that this method should not be included in the type
1535 // Record that this function is a stub method created for an unnamed
1538 set_is_unnamed_type_stub_method()
1540 go_assert(this->is_method());
1541 this->is_unnamed_type_stub_method_
= true;
1544 // Return the amount of enclosed variables in this closure.
1546 closure_field_count() const
1547 { return this->closure_fields_
.size(); }
1549 // Add a new field to the closure variable.
1551 add_closure_field(Named_object
* var
, Location loc
)
1552 { this->closure_fields_
.push_back(std::make_pair(var
, loc
)); }
1554 // Whether this function needs a closure.
1556 needs_closure() const
1557 { return !this->closure_fields_
.empty(); }
1559 // Return the closure variable, creating it if necessary. This is
1560 // passed to the function as a static chain parameter.
1564 // Set the closure variable. This is used when building thunks for
1565 // functions which call recover.
1567 set_closure_var(Named_object
* v
)
1569 go_assert(this->closure_var_
== NULL
);
1570 this->closure_var_
= v
;
1573 // Return the variable for a reference to field INDEX in the closure
1576 enclosing_var(unsigned int index
)
1578 go_assert(index
< this->closure_fields_
.size());
1579 return closure_fields_
[index
].first
;
1582 // Set the type of the closure variable if there is one.
1586 // Get the block of statements associated with the function.
1589 { return this->block_
; }
1591 // Get the location of the start of the function.
1594 { return this->location_
; }
1596 // Return whether this function is actually a method.
1600 // Add a label definition to the function.
1602 add_label_definition(Gogo
*, const std::string
& label_name
, Location
);
1604 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1605 // if we should report errors for a goto from the current location
1606 // to the label location.
1608 add_label_reference(Gogo
*, const std::string
& label_name
,
1609 Location
, bool issue_goto_errors
);
1611 // Warn about labels that are defined but not used.
1613 check_labels() const;
1615 // Note that a new local type has been added. Return its index.
1617 new_local_type_index()
1618 { return this->local_type_count_
++; }
1620 // Whether this function calls the predeclared recover function.
1622 calls_recover() const
1623 { return this->calls_recover_
; }
1625 // Record that this function calls the predeclared recover function.
1626 // This is set during the lowering pass.
1629 { this->calls_recover_
= true; }
1631 // Whether this is a recover thunk function.
1633 is_recover_thunk() const
1634 { return this->is_recover_thunk_
; }
1636 // Record that this is a thunk built for a function which calls
1639 set_is_recover_thunk()
1640 { this->is_recover_thunk_
= true; }
1642 // Whether this function already has a recover thunk.
1644 has_recover_thunk() const
1645 { return this->has_recover_thunk_
; }
1647 // Record that this function already has a recover thunk.
1649 set_has_recover_thunk()
1650 { this->has_recover_thunk_
= true; }
1652 // Record that this function is a thunk created for a defer
1653 // statement that calls the __go_set_defer_retaddr runtime function.
1655 set_calls_defer_retaddr()
1656 { this->calls_defer_retaddr_
= true; }
1658 // Whether this is a type hash or equality function created by the
1661 is_type_specific_function()
1662 { return this->is_type_specific_function_
; }
1664 // Record that this function is a type hash or equality function
1665 // created by the compiler.
1667 set_is_type_specific_function()
1668 { this->is_type_specific_function_
= true; }
1670 // Mark the function as going into a unique section.
1672 set_in_unique_section()
1673 { this->in_unique_section_
= true; }
1675 // Return whether this function should be exported for inlining.
1677 export_for_inlining() const
1678 { return this->export_for_inlining_
; }
1680 // Mark the function to be exported for inlining.
1682 set_export_for_inlining()
1683 { this->export_for_inlining_
= true; }
1685 // Return whether this function is inline only.
1687 is_inline_only() const
1688 { return this->is_inline_only_
; }
1690 // Mark the function as inline only: the body should not be emitted
1691 // if it is not inlined.
1693 set_is_inline_only()
1694 { this->is_inline_only_
= true; }
1696 // Report whether the function is referenced by an inline body.
1698 is_referenced_by_inline() const
1699 { return this->is_referenced_by_inline_
; }
1701 // Mark the function as referenced by an inline body.
1703 set_is_referenced_by_inline()
1704 { this->is_referenced_by_inline_
= true; }
1706 // Swap with another function. Used only for the thunk which calls
1709 swap_for_recover(Function
*);
1711 // Traverse the tree.
1713 traverse(Traverse
*);
1715 // Determine types in the function.
1719 // Return an expression for the function descriptor, given the named
1720 // object for this function. This may only be called for functions
1721 // without a closure. This will be an immutable struct with one
1722 // field that points to the function's code.
1724 descriptor(Gogo
*, Named_object
*);
1726 // Set the descriptor for this function. This is used when a
1727 // function declaration is followed by a function definition.
1729 set_descriptor(Expression
* descriptor
)
1731 go_assert(this->descriptor_
== NULL
);
1732 this->descriptor_
= descriptor
;
1735 // Return the backend representation.
1737 get_or_make_decl(Gogo
*, Named_object
*);
1739 // Return the function's decl after it has been built.
1743 // Set the function decl to hold a backend representation of the function
1746 build(Gogo
*, Named_object
*);
1748 // Get the statement that assigns values to this function's result struct.
1750 return_value(Gogo
*, Named_object
*, Location
) const;
1752 // Get the backend name of this function.
1754 backend_name(Gogo
*, Named_object
*, Backend_name
*);
1756 // Get an expression for the variable holding the defer stack.
1758 defer_stack(Location
);
1760 // Export the function.
1762 export_func(Export
*, const Named_object
*) const;
1764 // Export a function with a type.
1766 export_func_with_type(Export
*, const Named_object
*,
1767 const Function_type
*, Results
*, bool nointerface
,
1768 const std::string
& asm_name
, Block
* block
, Location
);
1770 // Import a function. Reports whether the import succeeded.
1772 import_func(Import
*, std::string
* pname
, Package
** pkg
,
1773 bool* is_exported
, Typed_identifier
** receiver
,
1774 Typed_identifier_list
** pparameters
,
1775 Typed_identifier_list
** presults
, bool* is_varargs
,
1776 bool* nointerface
, std::string
* asm_name
, std::string
* body
);
1779 // Type for mapping from label names to Label objects.
1780 typedef Unordered_map(std::string
, Label
*) Labels
;
1783 build_defer_wrapper(Gogo
*, Named_object
*, Bstatement
**, Bstatement
**);
1785 typedef std::vector
<std::pair
<Named_object
*,
1786 Location
> > Closure_fields
;
1788 // The function's type.
1789 Function_type
* type_
;
1790 // The enclosing function. This is NULL when there isn't one, which
1791 // is the normal case.
1792 Named_object
* enclosing_
;
1793 // The result variables, if any.
1795 // If there is a closure, this is the list of variables which appear
1796 // in the closure. This is created by the parser, and then resolved
1797 // to a real type when we lower parse trees.
1798 Closure_fields closure_fields_
;
1799 // The closure variable, passed as a parameter using the static
1800 // chain parameter. Normally NULL.
1801 Named_object
* closure_var_
;
1802 // The outer block of statements in the function.
1804 // The source location of the start of the function.
1806 // Labels defined or referenced in the function.
1808 // The number of local types defined in this function.
1809 unsigned int local_type_count_
;
1810 // The assembler name: this is the name that will be put in the object file.
1811 // Set by the go:linkname compiler directive. This is normally empty.
1812 std::string asm_name_
;
1813 // The function descriptor, if any.
1814 Expression
* descriptor_
;
1815 // The function decl.
1817 // The defer stack variable. A pointer to this variable is used to
1818 // distinguish the defer stack for one function from another. This
1819 // is NULL unless we actually need a defer stack.
1820 Temporary_statement
* defer_stack_
;
1821 // Pragmas for this function. This is a set of GOPRAGMA bits.
1822 unsigned int pragmas_
;
1823 // Number of nested functions defined within this function.
1824 unsigned int nested_functions_
;
1825 // True if this function is sink-named. No code is generated.
1827 // True if the result variables are named.
1828 bool results_are_named_
: 1;
1829 // True if this function is a stub method created for an unnamed
1831 bool is_unnamed_type_stub_method_
: 1;
1832 // True if this function calls the predeclared recover function.
1833 bool calls_recover_
: 1;
1834 // True if this a thunk built for a function which calls recover.
1835 bool is_recover_thunk_
: 1;
1836 // True if this function already has a recover thunk.
1837 bool has_recover_thunk_
: 1;
1838 // True if this is a thunk built for a defer statement that calls
1839 // the __go_set_defer_retaddr runtime function.
1840 bool calls_defer_retaddr_
: 1;
1841 // True if this is a function built by the compiler to as a hash or
1842 // equality function for some type.
1843 bool is_type_specific_function_
: 1;
1844 // True if this function should be put in a unique section. This is
1845 // turned on for field tracking.
1846 bool in_unique_section_
: 1;
1847 // True if we should export the body of this function for
1848 // cross-package inlining.
1849 bool export_for_inlining_
: 1;
1850 // True if this function is inline only: if it should not be emitted
1851 // if it is not inlined.
1852 bool is_inline_only_
: 1;
1853 // True if this function is referenced from an inlined body that
1854 // will be put into the export data.
1855 bool is_referenced_by_inline_
: 1;
1856 // True if we should make this function visible to other packages
1857 // because of a go:linkname directive.
1858 bool is_exported_by_linkname_
: 1;
1861 // A snapshot of the current binding state.
1863 class Bindings_snapshot
1866 Bindings_snapshot(const Block
*, Location
);
1868 // Report any errors appropriate for a goto from the current binding
1869 // state of B to this one.
1871 check_goto_from(const Block
* b
, Location
);
1873 // Report any errors appropriate for a goto from this binding state
1874 // to the current state of B.
1876 check_goto_to(const Block
* b
);
1880 check_goto_block(Location
, const Block
*, const Block
*, size_t*);
1883 check_goto_defs(Location
, const Block
*, size_t, size_t);
1885 // The current block.
1886 const Block
* block_
;
1887 // The number of names currently defined in each open block.
1888 // Element 0 is this->block_, element 1 is
1889 // this->block_->enclosing(), etc.
1890 std::vector
<size_t> counts_
;
1891 // The location where this snapshot was taken.
1895 // A function declaration.
1897 class Function_declaration
1900 Function_declaration(Function_type
* fntype
, Location location
)
1901 : fntype_(fntype
), location_(location
), asm_name_(), descriptor_(NULL
),
1902 fndecl_(NULL
), pragmas_(0), imported_body_(),
1903 is_on_inlinable_list_(false)
1908 { return this->fntype_
; }
1912 { return this->location_
; }
1914 // Return whether this function declaration is a method.
1920 { return this->asm_name_
; }
1922 // Set the assembler name.
1924 set_asm_name(const std::string
& asm_name
)
1925 { this->asm_name_
= asm_name
; }
1927 // Return the pragmas for this function.
1930 { return this->pragmas_
; }
1932 // Set the pragmas for this function.
1934 set_pragmas(unsigned int pragmas
)
1936 this->pragmas_
= pragmas
;
1939 // Whether this method should not be included in the type
1942 nointerface() const;
1944 // Record that this method should not be included in the type
1949 // Whether we have an imported function body.
1951 has_imported_body() const
1952 { return !this->imported_body_
.empty(); }
1954 // Record the imported body of this function.
1956 set_imported_body(Import
* imp
, const std::string
& imported_body
)
1959 this->imported_body_
= imported_body
;
1962 // Whether this declaration is on the list of inlinable functions.
1964 is_on_inlinable_list() const
1965 { return this->is_on_inlinable_list_
; }
1967 // Set that this function is on the list of inlinable functions.
1969 set_is_on_inlinable_list()
1970 { this->is_on_inlinable_list_
= true; }
1972 // Import the function body, creating a function.
1974 import_function_body(Gogo
*, Named_object
*);
1976 // Return an expression for the function descriptor, given the named
1977 // object for this function. This may only be called for functions
1978 // without a closure. This will be an immutable struct with one
1979 // field that points to the function's code.
1981 descriptor(Gogo
*, Named_object
*);
1983 // Return true if we have created a descriptor for this declaration.
1985 has_descriptor() const
1986 { return this->descriptor_
!= NULL
; }
1988 // Return a backend representation.
1990 get_or_make_decl(Gogo
*, Named_object
*);
1992 // If there is a descriptor, build it into the backend
1995 build_backend_descriptor(Gogo
*);
1997 // Get the backend name of this function declaration.
1999 backend_name(Gogo
*, Named_object
*, Backend_name
*);
2001 // Export a function declaration.
2003 export_func(Export
* exp
, const Named_object
* no
) const
2005 Function::export_func_with_type(exp
, no
, this->fntype_
, NULL
,
2006 this->is_method() && this->nointerface(),
2007 this->asm_name_
, NULL
, this->location_
);
2010 // Check that the types used in this declaration's signature are defined.
2012 check_types() const;
2015 // The type of the function.
2016 Function_type
* fntype_
;
2017 // The location of the declaration.
2019 // The assembler name: this is the name to use in references to the
2020 // function. This is normally empty.
2021 std::string asm_name_
;
2022 // The function descriptor, if any.
2023 Expression
* descriptor_
;
2024 // The function decl if needed.
2026 // Pragmas for this function. This is a set of GOPRAGMA bits.
2027 unsigned int pragmas_
;
2028 // Importer for function body if imported from a different package.
2030 // Export data for function body if imported from a different package.
2031 std::string imported_body_
;
2032 // Whether this declaration is already on the list of inlinable functions.
2033 bool is_on_inlinable_list_
;
2041 Variable(Type
*, Expression
*, bool is_global
, bool is_parameter
,
2042 bool is_receiver
, Location
);
2044 // Get the type of the variable.
2051 // Return whether the type is defined yet.
2055 // Get the initial value.
2058 { return this->init_
; }
2060 // Return whether there are any preinit statements.
2062 has_pre_init() const
2063 { return this->preinit_
!= NULL
; }
2065 // Return the preinit statements if any.
2068 { return this->preinit_
; }
2070 // Return whether this is a global variable.
2073 { return this->is_global_
; }
2075 // Return whether this is a function parameter.
2077 is_parameter() const
2078 { return this->is_parameter_
; }
2080 // Return whether this is a closure (static chain) parameter.
2083 { return this->is_closure_
; }
2085 // Change this parameter to be a closure.
2089 this->is_closure_
= true;
2092 // Return whether this is the receiver parameter of a method.
2095 { return this->is_receiver_
; }
2097 // Change this parameter to be a receiver. This is used when
2098 // creating the thunks created for functions which call recover.
2102 go_assert(this->is_parameter_
);
2103 this->is_receiver_
= true;
2106 // Change this parameter to not be a receiver. This is used when
2107 // creating the thunks created for functions which call recover.
2109 set_is_not_receiver()
2111 go_assert(this->is_parameter_
);
2112 this->is_receiver_
= false;
2115 // Return whether this is the varargs parameter of a function.
2117 is_varargs_parameter() const
2118 { return this->is_varargs_parameter_
; }
2120 // Return whether this is a global sink variable, created only to
2121 // run an initializer.
2123 is_global_sink() const
2124 { return this->is_global_sink_
; }
2126 // Record that this is a global sink variable.
2128 set_is_global_sink()
2130 go_assert(this->is_global_
);
2131 this->is_global_sink_
= true;
2134 // Whether this variable's address is taken.
2136 is_address_taken() const
2137 { return this->is_address_taken_
; }
2139 // Whether this variable should live in the heap.
2142 { return this->is_address_taken_
&& !this->is_global_
; }
2144 // Note that something takes the address of this variable.
2147 { this->is_address_taken_
= true; }
2149 // Return whether the address is taken but does not escape.
2151 is_non_escaping_address_taken() const
2152 { return this->is_non_escaping_address_taken_
; }
2154 // Note that something takes the address of this variable such that
2155 // the address does not escape the function.
2157 set_non_escaping_address_taken()
2158 { this->is_non_escaping_address_taken_
= true; }
2160 // Get the source location of the variable's declaration.
2163 { return this->location_
; }
2165 // Record that this is the varargs parameter of a function.
2167 set_is_varargs_parameter()
2169 go_assert(this->is_parameter_
);
2170 this->is_varargs_parameter_
= true;
2173 // Return whether the variable has been used.
2176 { return this->is_used_
; }
2178 // Mark that the variable has been used.
2181 { this->is_used_
= true; }
2183 // Clear the initial value; used for error handling and write barriers.
2186 { this->init_
= NULL
; }
2188 // Set the initial value; used for converting shortcuts.
2190 set_init(Expression
* init
)
2191 { this->init_
= init
; }
2193 // Get the preinit block, a block of statements to be run before the
2194 // initialization expression.
2196 preinit_block(Gogo
*);
2198 // Add a statement to be run before the initialization expression.
2199 // This is only used for global variables.
2201 add_preinit_statement(Gogo
*, Statement
*);
2203 // Lower the initialization expression after parsing is complete.
2205 lower_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
2207 // Flatten the initialization expression after ordering evaluations.
2209 flatten_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
2211 // A special case: the init value is used only to determine the
2212 // type. This is used if the variable is defined using := with the
2213 // comma-ok form of a map index or a receive expression. The init
2214 // value is actually the map index expression or receive expression.
2215 // We use this because we may not know the right type at parse time.
2217 set_type_from_init_tuple()
2218 { this->type_from_init_tuple_
= true; }
2220 // Another special case: the init value is used only to determine
2221 // the type. This is used if the variable is defined using := with
2222 // a range clause. The init value is the range expression. The
2223 // type of the variable is the index type of the range expression
2224 // (i.e., the first value returned by a range).
2226 set_type_from_range_index()
2227 { this->type_from_range_index_
= true; }
2229 // Another special case: like set_type_from_range_index, but the
2230 // type is the value type of the range expression (i.e., the second
2231 // value returned by a range).
2233 set_type_from_range_value()
2234 { this->type_from_range_value_
= true; }
2236 // Another special case: the init value is used only to determine
2237 // the type. This is used if the variable is defined using := with
2238 // a case in a select statement. The init value is the channel.
2239 // The type of the variable is the channel's element type.
2241 set_type_from_chan_element()
2242 { this->type_from_chan_element_
= true; }
2244 // After we lower the select statement, we once again set the type
2245 // from the initialization expression.
2247 clear_type_from_chan_element()
2249 go_assert(this->type_from_chan_element_
);
2250 this->type_from_chan_element_
= false;
2253 // TRUE if this variable was created for a type switch clause.
2255 is_type_switch_var() const
2256 { return this->is_type_switch_var_
; }
2258 // Note that this variable was created for a type switch clause.
2260 set_is_type_switch_var()
2261 { this->is_type_switch_var_
= true; }
2263 // Mark the variable as going into a unique section.
2265 set_in_unique_section()
2267 go_assert(this->is_global_
);
2268 this->in_unique_section_
= true;
2271 // Mark the variable as referenced by an inline body.
2273 set_is_referenced_by_inline()
2275 go_assert(this->is_global_
);
2276 this->is_referenced_by_inline_
= true;
2279 // Return the top-level declaration for this variable.
2282 { return this->toplevel_decl_
; }
2284 // Set the top-level declaration for this variable. Only used for local
2287 set_toplevel_decl(Statement
* s
)
2289 go_assert(!this->is_global_
&& !this->is_parameter_
&& !this->is_receiver_
);
2290 this->toplevel_decl_
= s
;
2293 // Traverse the initializer expression.
2295 traverse_expression(Traverse
*, unsigned int traverse_mask
);
2297 // Determine the type of the variable if necessary.
2301 // Get the backend representation of the variable.
2303 get_backend_variable(Gogo
*, Named_object
*, const Package
*,
2304 const std::string
&);
2306 // Get the initial value of the variable. This may only
2307 // be called if has_pre_init() returns false.
2309 get_init(Gogo
*, Named_object
* function
);
2311 // Return a series of statements which sets the value of the
2312 // variable in DECL. This should only be called is has_pre_init()
2313 // returns true. DECL may be NULL for a sink variable.
2315 get_init_block(Gogo
*, Named_object
* function
, Bvariable
* decl
);
2317 // Export the variable.
2319 export_var(Export
*, const Named_object
*) const;
2321 // Import a variable. Reports whether the import succeeded.
2323 import_var(Import
*, std::string
* pname
, Package
** pkg
, bool* is_exported
,
2327 // The type of a tuple.
2329 type_from_tuple(Expression
*, bool) const;
2331 // The type of a range.
2333 type_from_range(Expression
*, bool, bool) const;
2335 // The element type of a channel.
2337 type_from_chan_element(Expression
*, bool) const;
2339 // The variable's type. This may be NULL if the type is set from
2342 // The initial value. This may be NULL if the variable should be
2343 // initialized to the default value for the type.
2345 // Statements to run before the init statement.
2347 // Location of variable definition.
2349 // Backend representation.
2350 Bvariable
* backend_
;
2351 // Whether this is a global variable.
2352 bool is_global_
: 1;
2353 // Whether this is a function parameter.
2354 bool is_parameter_
: 1;
2355 // Whether this is a closure parameter.
2356 bool is_closure_
: 1;
2357 // Whether this is the receiver parameter of a method.
2358 bool is_receiver_
: 1;
2359 // Whether this is the varargs parameter of a function.
2360 bool is_varargs_parameter_
: 1;
2361 // Whether this is a global sink variable created to run an
2363 bool is_global_sink_
: 1;
2364 // Whether this variable is ever referenced.
2366 // Whether something takes the address of this variable. For a
2367 // local variable this implies that the variable has to be on the
2368 // heap if it escapes from its function.
2369 bool is_address_taken_
: 1;
2370 // Whether something takes the address of this variable such that
2371 // the address does not escape the function.
2372 bool is_non_escaping_address_taken_
: 1;
2373 // True if we have seen this variable in a traversal.
2375 // True if we have lowered the initialization expression.
2376 bool init_is_lowered_
: 1;
2377 // True if we have flattened the initialization expression.
2378 bool init_is_flattened_
: 1;
2379 // True if init is a tuple used to set the type.
2380 bool type_from_init_tuple_
: 1;
2381 // True if init is a range clause and the type is the index type.
2382 bool type_from_range_index_
: 1;
2383 // True if init is a range clause and the type is the value type.
2384 bool type_from_range_value_
: 1;
2385 // True if init is a channel and the type is the channel's element type.
2386 bool type_from_chan_element_
: 1;
2387 // True if this is a variable created for a type switch case.
2388 bool is_type_switch_var_
: 1;
2389 // True if we have determined types.
2390 bool determined_type_
: 1;
2391 // True if this variable should be put in a unique section. This is
2392 // used for field tracking.
2393 bool in_unique_section_
: 1;
2394 // True if this variable is referenced from an inlined body that
2395 // will be put into the export data.
2396 bool is_referenced_by_inline_
: 1;
2397 // The top-level declaration for this variable. Only used for local
2398 // variables. Must be a Temporary_statement if not NULL.
2399 Statement
* toplevel_decl_
;
2402 // A variable which is really the name for a function return value, or
2405 class Result_variable
2408 Result_variable(Type
* type
, Function
* function
, int index
,
2410 : type_(type
), function_(function
), index_(index
), location_(location
),
2411 backend_(NULL
), is_address_taken_(false),
2412 is_non_escaping_address_taken_(false)
2415 // Get the type of the result variable.
2418 { return this->type_
; }
2420 // Get the function that this is associated with.
2423 { return this->function_
; }
2425 // Index in the list of function results.
2428 { return this->index_
; }
2430 // The location of the variable definition.
2433 { return this->location_
; }
2435 // Whether this variable's address is taken.
2437 is_address_taken() const
2438 { return this->is_address_taken_
; }
2440 // Note that something takes the address of this variable.
2443 { this->is_address_taken_
= true; }
2445 // Return whether the address is taken but does not escape.
2447 is_non_escaping_address_taken() const
2448 { return this->is_non_escaping_address_taken_
; }
2450 // Note that something takes the address of this variable such that
2451 // the address does not escape the function.
2453 set_non_escaping_address_taken()
2454 { this->is_non_escaping_address_taken_
= true; }
2456 // Whether this variable should live in the heap.
2459 { return this->is_address_taken_
; }
2461 // Set the function. This is used when cloning functions which call
2464 set_function(Function
* function
)
2465 { this->function_
= function
; }
2467 // Get the backend representation of the variable.
2469 get_backend_variable(Gogo
*, Named_object
*, const std::string
&);
2472 // Type of result variable.
2474 // Function with which this is associated.
2475 Function
* function_
;
2476 // Index in list of results.
2478 // Where the result variable is defined.
2480 // Backend representation.
2481 Bvariable
* backend_
;
2482 // Whether something takes the address of this variable.
2483 bool is_address_taken_
;
2484 // Whether something takes the address of this variable such that
2485 // the address does not escape the function.
2486 bool is_non_escaping_address_taken_
;
2489 // The value we keep for a named constant. This lets us hold a type
2490 // and an expression.
2492 class Named_constant
2495 Named_constant(Type
* type
, Expression
* expr
, int iota_value
,
2497 : type_(type
), expr_(expr
), iota_value_(iota_value
), location_(location
),
2498 lowering_(false), is_sink_(false), bconst_(NULL
)
2503 { return this->type_
; }
2510 { return this->expr_
; }
2514 { return this->iota_value_
; }
2518 { return this->location_
; }
2520 // Whether we are lowering.
2523 { return this->lowering_
; }
2525 // Set that we are lowering.
2528 { this->lowering_
= true; }
2530 // We are no longer lowering.
2533 { this->lowering_
= false; }
2537 { return this->is_sink_
; }
2541 { this->is_sink_
= true; }
2543 // Traverse the expression.
2545 traverse_expression(Traverse
*);
2547 // Determine the type of the constant if necessary.
2551 // Indicate that we found and reported an error for this constant.
2555 // Export the constant.
2557 export_const(Export
*, const std::string
& name
) const;
2559 // Import a constant.
2561 import_const(Import
*, std::string
*, Type
**, Expression
**);
2563 // Get the backend representation of the constant value.
2565 get_backend(Gogo
*, Named_object
*);
2568 // The type of the constant.
2570 // The expression for the constant.
2572 // If the predeclared constant iota is used in EXPR_, this is the
2573 // value it will have. We do this because at parse time we don't
2574 // know whether the name "iota" will refer to the predeclared
2575 // constant or to something else. We put in the right value in when
2578 // The location of the definition.
2580 // Whether we are currently lowering this constant.
2582 // Whether this constant is blank named and needs only type checking.
2584 // The backend representation of the constant value.
2585 Bexpression
* bconst_
;
2588 // A type declaration.
2590 class Type_declaration
2593 Type_declaration(Location location
)
2594 : location_(location
), in_function_(NULL
), in_function_index_(0),
2595 methods_(), issued_warning_(false)
2598 // Return the location.
2601 { return this->location_
; }
2603 // Return the function in which this type is declared. This will
2604 // return NULL for a type declared in global scope.
2606 in_function(unsigned int* pindex
)
2608 *pindex
= this->in_function_index_
;
2609 return this->in_function_
;
2612 // Set the function in which this type is declared.
2614 set_in_function(Named_object
* f
, unsigned int index
)
2616 this->in_function_
= f
;
2617 this->in_function_index_
= index
;
2620 // Add a method to this type. This is used when methods are defined
2623 add_method(const std::string
& name
, Function
* function
);
2625 // Add a method declaration to this type.
2627 add_method_declaration(const std::string
& name
, Package
*,
2628 Function_type
* type
, Location location
);
2630 // Add an already created object as a method.
2632 add_existing_method(Named_object
* no
)
2633 { this->methods_
.push_back(no
); }
2635 // Return whether any methods were defined.
2637 has_methods() const;
2639 // Return the methods.
2640 const std::vector
<Named_object
*>*
2642 { return &this->methods_
; }
2644 // Define methods when the real type is known.
2646 define_methods(Named_type
*);
2648 // This is called if we are trying to use this type. It returns
2649 // true if we should issue a warning.
2654 // The location of the type declaration.
2656 // If this type is declared in a function, a pointer back to the
2657 // function in which it is defined.
2658 Named_object
* in_function_
;
2659 // The index of this type in IN_FUNCTION_.
2660 unsigned int in_function_index_
;
2661 // Methods defined before the type is defined.
2662 std::vector
<Named_object
*> methods_
;
2663 // True if we have issued a warning about a use of this type
2664 // declaration when it is undefined.
2665 bool issued_warning_
;
2668 // An unknown object. These are created by the parser for forward
2669 // references to names which have not been seen before. In a correct
2670 // program, these will always point to a real definition by the end of
2671 // the parse. Because they point to another Named_object, these may
2672 // only be referenced by Unknown_expression objects.
2677 Unknown_name(Location location
)
2678 : location_(location
), real_named_object_(NULL
)
2681 // Return the location where this name was first seen.
2684 { return this->location_
; }
2686 // Return the real named object that this points to, or NULL if it
2687 // was never resolved.
2689 real_named_object() const
2690 { return this->real_named_object_
; }
2692 // Set the real named object that this points to.
2694 set_real_named_object(Named_object
* no
);
2697 // The location where this name was first seen.
2699 // The real named object when it is known.
2704 // A named object named. This is the result of a declaration. We
2705 // don't use a superclass because they all have to be handled
2713 // An uninitialized Named_object. We should never see this.
2714 NAMED_OBJECT_UNINITIALIZED
,
2715 // An erroneous name. This indicates a parse error, to avoid
2716 // later errors about undefined references.
2717 NAMED_OBJECT_ERRONEOUS
,
2718 // An unknown name. This is used for forward references. In a
2719 // correct program, these will all be resolved by the end of the
2721 NAMED_OBJECT_UNKNOWN
,
2726 // A forward type declaration.
2727 NAMED_OBJECT_TYPE_DECLARATION
,
2730 // A result variable in a function.
2731 NAMED_OBJECT_RESULT_VAR
,
2732 // The blank identifier--the special variable named _.
2736 // A forward func declaration.
2737 NAMED_OBJECT_FUNC_DECLARATION
,
2739 NAMED_OBJECT_PACKAGE
2742 // Return the classification.
2744 classification() const
2745 { return this->classification_
; }
2750 is_erroneous() const
2751 { return this->classification_
== NAMED_OBJECT_ERRONEOUS
; }
2755 { return this->classification_
== NAMED_OBJECT_UNKNOWN
; }
2759 { return this->classification_
== NAMED_OBJECT_CONST
; }
2763 { return this->classification_
== NAMED_OBJECT_TYPE
; }
2766 is_type_declaration() const
2767 { return this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
; }
2771 { return this->classification_
== NAMED_OBJECT_VAR
; }
2774 is_result_variable() const
2775 { return this->classification_
== NAMED_OBJECT_RESULT_VAR
; }
2779 { return this->classification_
== NAMED_OBJECT_SINK
; }
2783 { return this->classification_
== NAMED_OBJECT_FUNC
; }
2786 is_function_declaration() const
2787 { return this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
; }
2791 { return this->classification_
== NAMED_OBJECT_PACKAGE
; }
2795 static Named_object
*
2796 make_erroneous_name(const std::string
& name
)
2797 { return new Named_object(name
, NULL
, NAMED_OBJECT_ERRONEOUS
); }
2799 static Named_object
*
2800 make_unknown_name(const std::string
& name
, Location
);
2802 static Named_object
*
2803 make_constant(const Typed_identifier
&, const Package
*, Expression
*,
2806 static Named_object
*
2807 make_type(const std::string
&, const Package
*, Type
*, Location
);
2809 static Named_object
*
2810 make_type_declaration(const std::string
&, const Package
*, Location
);
2812 static Named_object
*
2813 make_variable(const std::string
&, const Package
*, Variable
*);
2815 static Named_object
*
2816 make_result_variable(const std::string
&, Result_variable
*);
2818 static Named_object
*
2821 static Named_object
*
2822 make_function(const std::string
&, const Package
*, Function
*);
2824 static Named_object
*
2825 make_function_declaration(const std::string
&, const Package
*, Function_type
*,
2828 static Named_object
*
2829 make_package(const std::string
& alias
, Package
* package
);
2836 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2837 return this->u_
.unknown_value
;
2841 unknown_value() const
2843 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2844 return this->u_
.unknown_value
;
2850 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2851 return this->u_
.const_value
;
2854 const Named_constant
*
2857 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2858 return this->u_
.const_value
;
2864 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2865 return this->u_
.type_value
;
2871 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2872 return this->u_
.type_value
;
2876 type_declaration_value()
2878 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2879 return this->u_
.type_declaration
;
2882 const Type_declaration
*
2883 type_declaration_value() const
2885 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2886 return this->u_
.type_declaration
;
2892 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2893 return this->u_
.var_value
;
2899 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2900 return this->u_
.var_value
;
2906 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2907 return this->u_
.result_var_value
;
2910 const Result_variable
*
2911 result_var_value() const
2913 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2914 return this->u_
.result_var_value
;
2920 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2921 return this->u_
.func_value
;
2927 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2928 return this->u_
.func_value
;
2931 Function_declaration
*
2932 func_declaration_value()
2934 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2935 return this->u_
.func_declaration_value
;
2938 const Function_declaration
*
2939 func_declaration_value() const
2941 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2942 return this->u_
.func_declaration_value
;
2948 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2949 return this->u_
.package_value
;
2953 package_value() const
2955 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2956 return this->u_
.package_value
;
2961 { return this->name_
; }
2963 // Return the name to use in an error message. The difference is
2964 // that if this Named_object is defined in a different package, this
2965 // will return PACKAGE.NAME.
2967 message_name() const;
2971 { return this->package_
; }
2973 // Resolve an unknown value if possible. This returns the same
2974 // Named_object or a new one.
2978 Named_object
* ret
= this;
2979 if (this->is_unknown())
2981 Named_object
* r
= this->unknown_value()->real_named_object();
2991 const Named_object
* ret
= this;
2992 if (this->is_unknown())
2994 const Named_object
* r
= this->unknown_value()->real_named_object();
3001 // The location where this object was defined or referenced.
3005 // Convert a variable to the backend representation.
3007 get_backend_variable(Gogo
*, Named_object
* function
);
3009 // Get the backend representation of this object.
3011 get_backend(Gogo
*, std::vector
<Bexpression
*>&, std::vector
<Btype
*>&,
3012 std::vector
<Bfunction
*>&);
3014 // Define a type declaration.
3016 set_type_value(Named_type
*);
3018 // Define a function declaration.
3020 set_function_value(Function
*);
3022 // Declare an unknown name as a type declaration.
3026 // Export this object.
3028 export_named_object(Export
*) const;
3030 // Mark this named object as an invalid redefinition of another object.
3032 set_is_redefinition()
3033 { this->is_redefinition_
= true; }
3035 // Return whether or not this object is a invalid redefinition of another
3038 is_redefinition() const
3039 { return this->is_redefinition_
; }
3042 Named_object(const std::string
&, const Package
*, Classification
);
3044 // The name of the object.
3046 // The package that this object is in. This is NULL if it is in the
3047 // file we are compiling.
3048 const Package
* package_
;
3049 // The type of object this is.
3050 Classification classification_
;
3054 Unknown_name
* unknown_value
;
3055 Named_constant
* const_value
;
3056 Named_type
* type_value
;
3057 Type_declaration
* type_declaration
;
3058 Variable
* var_value
;
3059 Result_variable
* result_var_value
;
3060 Function
* func_value
;
3061 Function_declaration
* func_declaration_value
;
3062 Package
* package_value
;
3064 // True if this object is an invalid redefinition of another object.
3065 bool is_redefinition_
;
3068 // A binding contour. This binds names to objects.
3073 // Type for mapping from names to objects.
3074 typedef Unordered_map(std::string
, Named_object
*) Contour
;
3076 Bindings(Bindings
* enclosing
);
3078 // Add an erroneous name.
3080 add_erroneous_name(const std::string
& name
)
3081 { return this->add_named_object(Named_object::make_erroneous_name(name
)); }
3083 // Add an unknown name.
3085 add_unknown_name(const std::string
& name
, Location location
)
3087 return this->add_named_object(Named_object::make_unknown_name(name
,
3093 add_constant(const Typed_identifier
& tid
, const Package
* package
,
3094 Expression
* expr
, int iota_value
)
3096 return this->add_named_object(Named_object::make_constant(tid
, package
,
3103 add_type(const std::string
& name
, const Package
* package
, Type
* type
,
3106 return this->add_named_object(Named_object::make_type(name
, package
, type
,
3110 // Add a named type. This is used for builtin types, and to add an
3111 // imported type to the global scope.
3113 add_named_type(Named_type
* named_type
);
3115 // Add a type declaration.
3117 add_type_declaration(const std::string
& name
, const Package
* package
,
3120 Named_object
* no
= Named_object::make_type_declaration(name
, package
,
3122 return this->add_named_object(no
);
3127 add_variable(const std::string
& name
, const Package
* package
,
3130 return this->add_named_object(Named_object::make_variable(name
, package
,
3134 // Add a result variable.
3136 add_result_variable(const std::string
& name
, Result_variable
* result
)
3138 return this->add_named_object(Named_object::make_result_variable(name
,
3144 add_function(const std::string
& name
, const Package
*, Function
* function
);
3146 // Add a function declaration.
3148 add_function_declaration(const std::string
& name
, const Package
* package
,
3149 Function_type
* type
, Location location
);
3151 // Add a package. The location is the location of the import
3154 add_package(const std::string
& alias
, Package
* package
)
3156 Named_object
* no
= Named_object::make_package(alias
, package
);
3157 return this->add_named_object(no
);
3160 // Define a type which was already declared.
3162 define_type(Named_object
*, Named_type
*);
3164 // Add a method to the list of objects. This is not added to the
3167 add_method(Named_object
*);
3169 // Add a named object to this binding.
3171 add_named_object(Named_object
* no
)
3172 { return this->add_named_object_to_contour(&this->bindings_
, no
); }
3174 // Clear all names in file scope from the bindings.
3176 clear_file_scope(Gogo
*);
3178 // Look up a name in this binding contour and in any enclosing
3179 // binding contours. This returns NULL if the name is not found.
3181 lookup(const std::string
&) const;
3183 // Look up a name in this binding contour without looking in any
3184 // enclosing binding contours. Returns NULL if the name is not found.
3186 lookup_local(const std::string
&) const;
3190 remove_binding(Named_object
*);
3192 // Mark all variables as used. This is used for some types of parse
3197 // Traverse the tree. See the Traverse class.
3199 traverse(Traverse
*, bool is_global
);
3201 // Iterate over definitions. This does not include things which
3202 // were only declared.
3204 typedef std::vector
<Named_object
*>::const_iterator
3205 const_definitions_iterator
;
3207 const_definitions_iterator
3208 begin_definitions() const
3209 { return this->named_objects_
.begin(); }
3211 const_definitions_iterator
3212 end_definitions() const
3213 { return this->named_objects_
.end(); }
3215 // Return the number of definitions.
3217 size_definitions() const
3218 { return this->named_objects_
.size(); }
3220 // Return whether there are no definitions.
3222 empty_definitions() const
3223 { return this->named_objects_
.empty(); }
3225 // Iterate over declarations. This is everything that has been
3226 // declared, which includes everything which has been defined.
3228 typedef Contour::const_iterator const_declarations_iterator
;
3230 const_declarations_iterator
3231 begin_declarations() const
3232 { return this->bindings_
.begin(); }
3234 const_declarations_iterator
3235 end_declarations() const
3236 { return this->bindings_
.end(); }
3238 // Return the number of declarations.
3240 size_declarations() const
3241 { return this->bindings_
.size(); }
3243 // Return whether there are no declarations.
3245 empty_declarations() const
3246 { return this->bindings_
.empty(); }
3248 // Return the first declaration.
3251 { return this->bindings_
.empty() ? NULL
: this->bindings_
.begin()->second
; }
3253 // Dump to stderr for debugging
3258 add_named_object_to_contour(Contour
*, Named_object
*);
3261 new_definition(Named_object
*, Named_object
*);
3263 // Enclosing bindings.
3264 Bindings
* enclosing_
;
3265 // The list of objects.
3266 std::vector
<Named_object
*> named_objects_
;
3267 // The mapping from names to objects.
3276 Label(const std::string
& name
)
3277 : name_(name
), location_(Linemap::unknown_location()), snapshot_(NULL
),
3278 refs_(), is_used_(false), blabel_(NULL
), depth_(DEPTH_UNKNOWN
)
3281 // Return the label's name.
3284 { return this->name_
; }
3286 // Return whether the label has been defined.
3289 { return !Linemap::is_unknown_location(this->location_
); }
3291 // Return whether the label has been used.
3294 { return this->is_used_
; }
3296 // Record that the label is used.
3299 { this->is_used_
= true; }
3301 // Return whether this label is looping.
3304 { return this->depth_
== DEPTH_LOOPING
; }
3306 // Set this label as looping.
3309 { this->depth_
= DEPTH_LOOPING
; }
3311 // Return whether this label is nonlooping.
3314 { return this->depth_
== DEPTH_NONLOOPING
; }
3316 // Set this label as nonlooping.
3319 { this->depth_
= DEPTH_NONLOOPING
; }
3321 // Return the location of the definition.
3324 { return this->location_
; }
3326 // Return the bindings snapshot.
3329 { return this->snapshot_
; }
3331 // Add a snapshot of a goto which refers to this label.
3333 add_snapshot_ref(Bindings_snapshot
* snapshot
)
3335 go_assert(Linemap::is_unknown_location(this->location_
));
3336 this->refs_
.push_back(snapshot
);
3339 // Return the list of snapshots of goto statements which refer to
3341 const std::vector
<Bindings_snapshot
*>&
3343 { return this->refs_
; }
3345 // Clear the references.
3349 // Define the label at LOCATION with the given bindings snapshot.
3351 define(Location location
, Bindings_snapshot
* snapshot
)
3353 if (this->is_dummy_label())
3355 go_assert(Linemap::is_unknown_location(this->location_
)
3356 && this->snapshot_
== NULL
);
3357 this->location_
= location
;
3358 this->snapshot_
= snapshot
;
3361 // Return the backend representation for this label.
3363 get_backend_label(Translate_context
*);
3365 // Return an expression for the address of this label. This is used
3366 // to get the return address of a deferred function to see whether
3367 // the function may call recover.
3369 get_addr(Translate_context
*, Location location
);
3371 // Return a dummy label, representing any instance of the blank label.
3373 create_dummy_label();
3375 // Return TRUE if this is a dummy label.
3377 is_dummy_label() const
3378 { return this->name_
== "_"; }
3380 // A classification of a label's looping depth.
3384 // A label never jumped to.
3386 // A label jumped to.
3391 // The name of the label.
3393 // The location of the definition. This is 0 if the label has not
3394 // yet been defined.
3396 // A snapshot of the set of bindings defined at this label, used to
3397 // issue errors about invalid goto statements.
3398 Bindings_snapshot
* snapshot_
;
3399 // A list of snapshots of goto statements which refer to this label.
3400 std::vector
<Bindings_snapshot
*> refs_
;
3401 // Whether the label has been used.
3403 // The backend representation.
3405 // The looping depth of this label, for escape analysis.
3409 // An unnamed label. These are used when lowering loops.
3414 Unnamed_label(Location location
)
3415 : location_(location
), derived_from_(NULL
), blabel_(NULL
)
3418 // Get the location where the label is defined.
3421 { return this->location_
; }
3423 // Set the location where the label is defined.
3425 set_location(Location location
)
3426 { this->location_
= location
; }
3428 // Get the top level statement this unnamed label is derived from.
3430 derived_from() const
3431 { return this->derived_from_
; }
3433 // Set the top level statement this unnamed label is derived from.
3435 set_derived_from(Statement
* s
)
3436 { this->derived_from_
= s
; }
3438 // Return a statement which defines this label.
3440 get_definition(Translate_context
*);
3442 // Return a goto to this label from LOCATION.
3444 get_goto(Translate_context
*, Location location
);
3447 // Return the backend representation.
3449 get_blabel(Translate_context
*);
3451 // The location where the label is defined.
3453 // The top-level statement this unnamed label was derived/lowered from.
3454 // This is NULL is this label is not the top-level of a lowered statement.
3455 Statement
* derived_from_
;
3456 // The backend representation of this label.
3460 // An alias for an imported package.
3465 Package_alias(Location location
)
3466 : location_(location
), used_(0)
3469 // The location of the import statement.
3472 { return this->location_
; }
3474 // How many symbols from the package were used under this alias.
3477 { return this->used_
; }
3479 // Note that some symbol was used under this alias.
3485 // The location of the import statement.
3487 // The amount of times some name from this package was used under this alias.
3491 // An imported package.
3496 Package(const std::string
& pkgpath
, const std::string
& pkgpath_symbol
,
3499 // Get the package path used for all symbols exported from this
3503 { return this->pkgpath_
; }
3505 // Return the package path to use for a symbol name.
3507 pkgpath_symbol() const;
3509 // Set the package path symbol.
3511 set_pkgpath_symbol(const std::string
&);
3513 // Return the location of the most recent import statement.
3516 { return this->location_
; }
3518 // Return whether we know the name of this package yet.
3520 has_package_name() const
3521 { return !this->package_name_
.empty(); }
3523 // The name that this package uses in its package clause. This may
3524 // be different from the name in the associated Named_object if the
3525 // import statement used an alias.
3527 package_name() const
3529 go_assert(!this->package_name_
.empty());
3530 return this->package_name_
;
3533 // Return the bindings.
3536 { return this->bindings_
; }
3538 // Type used to map import names to package aliases.
3539 typedef std::map
<std::string
, Package_alias
*> Aliases
;
3541 // Return the set of package aliases.
3544 { return this->aliases_
; }
3546 // Note that some symbol from this package was used and qualified by ALIAS.
3547 // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3549 note_usage(const std::string
& alias
) const;
3551 // Note that USAGE might be a fake usage of this package.
3553 note_fake_usage(Expression
* usage
) const
3554 { this->fake_uses_
.insert(usage
); }
3556 // Forget a given USAGE of this package.
3558 forget_usage(Expression
* usage
) const;
3560 // Clear the used field for the next file.
3564 // Look up a name in the package. Returns NULL if the name is not
3567 lookup(const std::string
& name
) const
3568 { return this->bindings_
->lookup(name
); }
3570 // Set the name of the package.
3572 set_package_name(const std::string
& name
, Location
);
3574 // Set the location of the package. This is used to record the most
3575 // recent import location.
3577 set_location(Location location
)
3578 { this->location_
= location
; }
3580 // Add a package name as an ALIAS for this package.
3582 add_alias(const std::string
& alias
, Location
);
3584 // Add a constant to the package.
3586 add_constant(const Typed_identifier
& tid
, Expression
* expr
)
3587 { return this->bindings_
->add_constant(tid
, this, expr
, 0); }
3589 // Add a type to the package.
3591 add_type(const std::string
& name
, Type
* type
, Location location
)
3592 { return this->bindings_
->add_type(name
, this, type
, location
); }
3594 // Add a type declaration to the package.
3596 add_type_declaration(const std::string
& name
, Location location
)
3597 { return this->bindings_
->add_type_declaration(name
, this, location
); }
3599 // Add a variable to the package.
3601 add_variable(const std::string
& name
, Variable
* variable
)
3602 { return this->bindings_
->add_variable(name
, this, variable
); }
3604 // Add a function declaration to the package.
3606 add_function_declaration(const std::string
& name
, Function_type
* type
,
3608 { return this->bindings_
->add_function_declaration(name
, this, type
, loc
); }
3610 // Determine types of constants.
3615 // The package path for type reflection data.
3616 std::string pkgpath_
;
3617 // The package path for symbol names.
3618 std::string pkgpath_symbol_
;
3619 // The name that this package uses in the package clause. This may
3620 // be the empty string if it is not yet known.
3621 std::string package_name_
;
3622 // The names in this package.
3623 Bindings
* bindings_
;
3624 // The location of the most recent import statement.
3626 // The set of aliases associated with this package.
3628 // A set of possibly fake uses of this package. This is mutable because we
3629 // can track fake uses of a package even if we have a const pointer to it.
3630 mutable std::set
<Expression
*> fake_uses_
;
3633 // Return codes for the traversal functions. This is not an enum
3634 // because we want to be able to declare traversal functions in other
3635 // header files without including this one.
3637 // Continue traversal as usual.
3638 const int TRAVERSE_CONTINUE
= -1;
3641 const int TRAVERSE_EXIT
= 0;
3643 // Continue traversal, but skip components of the current object.
3644 // E.g., if this is returned by Traverse::statement, we do not
3645 // traverse the expressions in the statement even if
3646 // traverse_expressions is set in the traverse_mask.
3647 const int TRAVERSE_SKIP_COMPONENTS
= 1;
3649 // This class is used when traversing the parse tree. The caller uses
3650 // a subclass which overrides functions as desired.
3655 // These bitmasks say what to traverse.
3656 static const unsigned int traverse_variables
= 0x1;
3657 static const unsigned int traverse_constants
= 0x2;
3658 static const unsigned int traverse_functions
= 0x4;
3659 static const unsigned int traverse_blocks
= 0x8;
3660 static const unsigned int traverse_statements
= 0x10;
3661 static const unsigned int traverse_expressions
= 0x20;
3662 static const unsigned int traverse_types
= 0x40;
3663 static const unsigned int traverse_func_declarations
= 0x80;
3665 Traverse(unsigned int traverse_mask
)
3666 : traverse_mask_(traverse_mask
), types_seen_(NULL
), expressions_seen_(NULL
)
3669 virtual ~Traverse();
3671 // The bitmask of what to traverse.
3673 traverse_mask() const
3674 { return this->traverse_mask_
; }
3676 // Record that we are going to traverse a type. This returns true
3677 // if the type has already been seen in this traversal. This is
3678 // required because types, unlike expressions, can form a circular
3681 remember_type(const Type
*);
3683 // Record that we are going to see an expression. This returns true
3684 // if the expression has already been seen in this traversal. This
3685 // is only needed for cases where multiple expressions can point to
3688 remember_expression(const Expression
*);
3690 // These functions return one of the TRAVERSE codes defined above.
3692 // If traverse_variables is set in the mask, this is called for
3693 // every variable in the tree.
3695 variable(Named_object
*);
3697 // If traverse_constants is set in the mask, this is called for
3698 // every named constant in the tree. The bool parameter is true for
3699 // a global constant.
3701 constant(Named_object
*, bool);
3703 // If traverse_functions is set in the mask, this is called for
3704 // every function in the tree.
3706 function(Named_object
*);
3708 // If traverse_blocks is set in the mask, this is called for every
3709 // block in the tree.
3713 // If traverse_statements is set in the mask, this is called for
3714 // every statement in the tree.
3716 statement(Block
*, size_t* index
, Statement
*);
3718 // If traverse_expressions is set in the mask, this is called for
3719 // every expression in the tree.
3721 expression(Expression
**);
3723 // If traverse_types is set in the mask, this is called for every
3724 // type in the tree.
3728 // If traverse_func_declarations is set in the mask, this is called
3729 // for every function declarations in the tree.
3731 function_declaration(Named_object
*);
3734 // A hash table for types we have seen during this traversal. Note
3735 // that this uses the default hash functions for pointers rather
3736 // than Type_hash_identical and Type_identical. This is because for
3737 // traversal we care about seeing a specific type structure. If
3738 // there are two separate instances of identical types, we want to
3740 typedef Unordered_set(const Type
*) Types_seen
;
3742 typedef Unordered_set(const Expression
*) Expressions_seen
;
3744 // Bitmask of what sort of objects to traverse.
3745 unsigned int traverse_mask_
;
3746 // Types which have been seen in this traversal.
3747 Types_seen
* types_seen_
;
3748 // Expressions which have been seen in this traversal.
3749 Expressions_seen
* expressions_seen_
;
3752 // This class looks for interface types to finalize methods of inherited
3755 class Finalize_methods
: public Traverse
3758 Finalize_methods(Gogo
* gogo
)
3759 : Traverse(traverse_types
),
3770 // A class which makes it easier to insert new statements before the
3771 // current statement during a traversal.
3773 class Statement_inserter
3776 typedef Unordered_set(Statement
*) Statements
;
3778 // Empty constructor.
3779 Statement_inserter()
3780 : block_(NULL
), pindex_(NULL
), gogo_(NULL
), var_(NULL
),
3781 statements_added_(NULL
)
3784 // Constructor for a statement in a block.
3785 Statement_inserter(Block
* block
, size_t *pindex
, Statements
*added
= NULL
)
3786 : block_(block
), pindex_(pindex
), gogo_(NULL
), var_(NULL
),
3787 statements_added_(added
)
3790 // Constructor for a global variable.
3791 Statement_inserter(Gogo
* gogo
, Variable
* var
, Statements
*added
= NULL
)
3792 : block_(NULL
), pindex_(NULL
), gogo_(gogo
), var_(var
),
3793 statements_added_(added
)
3794 { go_assert(var
->is_global()); }
3796 // We use the default copy constructor and assignment operator.
3798 // Insert S before the statement we are traversing, or before the
3799 // initialization expression of a global variable.
3801 insert(Statement
* s
);
3804 // The block that the statement is in.
3806 // The index of the statement that we are traversing.
3808 // The IR, needed when looking at an initializer expression for a
3811 // The global variable, when looking at an initializer expression.
3813 // If non-null, a set to record new statements inserted (non-owned).
3814 Statements
* statements_added_
;
3817 // When translating the gogo IR into the backend data structure, this
3818 // is the context we pass down the blocks and statements.
3820 class Translate_context
3823 Translate_context(Gogo
* gogo
, Named_object
* function
, Block
* block
,
3825 : gogo_(gogo
), backend_(gogo
->backend()), function_(function
),
3826 block_(block
), bblock_(bblock
), is_const_(false)
3833 { return this->gogo_
; }
3837 { return this->backend_
; }
3841 { return this->function_
; }
3845 { return this->block_
; }
3849 { return this->bblock_
; }
3853 { return this->is_const_
; }
3855 // Make a constant context.
3858 { this->is_const_
= true; }
3861 // The IR for the entire compilation unit.
3863 // The generator for the backend data structures.
3865 // The function we are currently translating. NULL if not in a
3866 // function, e.g., the initializer of a global variable.
3867 Named_object
* function_
;
3868 // The block we are currently translating. NULL if not in a
3871 // The backend representation of the current block. NULL if block_
3874 // Whether this is being evaluated in a constant context. This is
3875 // used for type descriptor initializers.
3879 // This is used by some of the langhooks.
3880 extern Gogo
* go_get_gogo();
3882 // Whether we have seen any errors. FIXME: Replace with a backend
3884 extern bool saw_errors();
3886 // For use in the debugger
3887 extern void debug_go_gogo(Gogo
*);
3888 extern void debug_go_named_object(Named_object
*);
3889 extern void debug_go_bindings(Bindings
*);
3892 #endif // !defined(GO_GOGO_H)