1 // gogo.h -- Go frontend parsed representation. -*- C++ -*-
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
10 #include "go-linemap.h"
13 class Statement_inserter
;
15 class Type_hash_identical
;
18 class Typed_identifier
;
19 class Typed_identifier_list
;
23 class Temporary_statement
;
27 class Bindings_snapshot
;
33 class Struct_field_list
;
39 class Forward_declaration_type
;
42 class Translate_context
;
54 // This file declares the basic classes used to hold the internal
55 // representation of Go which is built by the parser.
57 // An initialization function for an imported package. This is a
58 // magic function which initializes variables and runs the "init"
64 Import_init(const std::string
& package_name
, const std::string
& init_name
,
66 : package_name_(package_name
), init_name_(init_name
), priority_(priority
)
69 // The name of the package being imported.
72 { return this->package_name_
; }
74 // The name of the package's init function.
77 { return this->init_name_
; }
79 // The priority of the initialization function. Functions with a
80 // lower priority number must be run first.
83 { return this->priority_
; }
86 // The name of the package being imported.
87 std::string package_name_
;
88 // The name of the package's init function.
89 std::string init_name_
;
94 // For sorting purposes.
97 operator<(const Import_init
& i1
, const Import_init
& i2
)
99 if (i1
.priority() < i2
.priority())
101 if (i1
.priority() > i2
.priority())
103 if (i1
.package_name() != i2
.package_name())
104 return i1
.package_name() < i2
.package_name();
105 return i1
.init_name() < i2
.init_name();
108 // The holder for the internal representation of the entire
114 // Create the IR, passing in the sizes of the types "int" and
115 // "uintptr" in bits.
116 Gogo(Backend
* backend
, Linemap
*linemap
, int int_type_size
, int pointer_size
);
118 // Get the backend generator.
121 { return this->backend_
; }
123 // Get the Location generator.
126 { return this->linemap_
; }
128 // Get the package name.
130 package_name() const;
132 // Set the package name.
134 set_package_name(const std::string
&, Location
);
136 // Return whether this is the "main" package.
138 is_main_package() const;
140 // If necessary, adjust the name to use for a hidden symbol. We add
141 // the package name, so that hidden symbols in different packages do
144 pack_hidden_name(const std::string
& name
, bool is_exported
) const
148 : '.' + this->pkgpath() + '.' + name
);
151 // Unpack a name which may have been hidden. Returns the
152 // user-visible name of the object.
154 unpack_hidden_name(const std::string
& name
)
155 { return name
[0] != '.' ? name
: name
.substr(name
.rfind('.') + 1); }
157 // Return whether a possibly packed name is hidden.
159 is_hidden_name(const std::string
& name
)
160 { return name
[0] == '.'; }
162 // Return the package path of a hidden name.
164 hidden_name_pkgpath(const std::string
& name
)
166 go_assert(Gogo::is_hidden_name(name
));
167 return name
.substr(1, name
.rfind('.') - 1);
170 // Given a name which may or may not have been hidden, return the
171 // name to use in an error message.
173 message_name(const std::string
& name
);
175 // Return whether a name is the blank identifier _.
177 is_sink_name(const std::string
& name
)
179 return (name
[0] == '.'
180 && name
[name
.length() - 1] == '_'
181 && name
[name
.length() - 2] == '.');
184 // Convert a pkgpath into a string suitable for a symbol
186 pkgpath_for_symbol(const std::string
& pkgpath
);
188 // Return the package path to use for reflect.Type.PkgPath.
192 // Return the package path to use for a symbol name.
194 pkgpath_symbol() const;
196 // Set the package path from a command line option.
198 set_pkgpath(const std::string
&);
200 // Set the prefix from a command line option.
202 set_prefix(const std::string
&);
204 // Return whether pkgpath was set from a command line option.
206 pkgpath_from_option() const
207 { return this->pkgpath_from_option_
; }
209 // Return the relative import path as set from the command line.
210 // Returns an empty string if it was not set.
212 relative_import_path() const
213 { return this->relative_import_path_
; }
215 // Set the relative import path from a command line option.
217 set_relative_import_path(const std::string
& s
)
218 { this->relative_import_path_
= s
; }
220 // Return whether to check for division by zero in binary operations.
222 check_divide_by_zero() const
223 { return this->check_divide_by_zero_
; }
225 // Set the option to check division by zero from a command line option.
227 set_check_divide_by_zero(bool b
)
228 { this->check_divide_by_zero_
= b
; }
230 // Return whether to check for division overflow in binary operations.
232 check_divide_overflow() const
233 { return this->check_divide_overflow_
; }
235 // Set the option to check division overflow from a command line option.
237 set_check_divide_overflow(bool b
)
238 { this->check_divide_overflow_
= b
; }
240 // Return the priority to use for the package we are compiling.
241 // This is two more than the largest priority of any package we
244 package_priority() const;
246 // Import a package. FILENAME is the file name argument, LOCAL_NAME
247 // is the local name to give to the package. If LOCAL_NAME is empty
248 // the declarations are added to the global scope.
250 import_package(const std::string
& filename
, const std::string
& local_name
,
251 bool is_local_name_exported
, Location
);
253 // Whether we are the global binding level.
255 in_global_scope() const;
257 // Look up a name in the current binding contours.
259 lookup(const std::string
&, Named_object
** pfunction
) const;
261 // Look up a name in the current block.
263 lookup_in_block(const std::string
&) const;
265 // Look up a name in the global namespace--the universal scope.
267 lookup_global(const char*) const;
269 // Add a new imported package. REAL_NAME is the real name of the
270 // package. ALIAS is the alias of the package; this may be the same
271 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
272 // this package should be added to the global namespace; this is
273 // true if the alias is ".". LOCATION is the location of the import
274 // statement. This returns the new package, or NULL on error.
276 add_imported_package(const std::string
& real_name
, const std::string
& alias
,
277 bool is_alias_exported
,
278 const std::string
& pkgpath
,
280 bool* padd_to_globals
);
282 // Register a package. This package may or may not be imported.
283 // This returns the Package structure for the package, creating if
286 register_package(const std::string
& pkgpath
, Location
);
288 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
289 // method function should be added to the type of its receiver.
291 start_function(const std::string
& name
, Function_type
* type
,
292 bool add_method_to_type
, Location
);
294 // Finish compiling a function.
296 finish_function(Location
);
298 // Return the current function.
300 current_function() const;
302 // Return the current block.
306 // Start a new block. This is not initially associated with a
309 start_block(Location
);
311 // Finish the current block and return it.
313 finish_block(Location
);
315 // Declare an erroneous name. This is used to avoid knock-on errors
316 // after a parsing error.
318 add_erroneous_name(const std::string
& name
);
320 // Declare an unknown name. This is used while parsing. The name
321 // must be resolved by the end of the parse. Unknown names are
322 // always added at the package level.
324 add_unknown_name(const std::string
& name
, Location
);
326 // Declare a function.
328 declare_function(const std::string
&, Function_type
*, Location
);
330 // Declare a function at the package level. This is used for
331 // functions generated for a type.
333 declare_package_function(const std::string
&, Function_type
*, Location
);
337 add_label_definition(const std::string
&, Location
);
339 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
340 // report errors for a goto from the current location to the label
343 add_label_reference(const std::string
&, Location
,
344 bool issue_goto_errors
);
346 // Return a snapshot of the current binding state.
348 bindings_snapshot(Location
);
350 // Add a statement to the current block.
352 add_statement(Statement
*);
354 // Add a block to the current block.
356 add_block(Block
*, Location
);
360 add_constant(const Typed_identifier
&, Expression
*, int iota_value
);
364 add_type(const std::string
&, Type
*, Location
);
366 // Add a named type. This is used for builtin types, and to add an
367 // imported type to the global scope.
369 add_named_type(Named_type
*);
373 declare_type(const std::string
&, Location
);
375 // Declare a type at the package level. This is used when the
376 // parser sees an unknown name where a type name is required.
378 declare_package_type(const std::string
&, Location
);
380 // Define a type which was already declared.
382 define_type(Named_object
*, Named_type
*);
386 add_variable(const std::string
&, Variable
*);
388 // Add a sink--a reference to the blank identifier _.
392 // Add a type which needs to be verified. This is used for sink
393 // types, just to give appropriate error messages.
395 add_type_to_verify(Type
* type
);
397 // Add a named object to the current namespace. This is used for
398 // import . "package".
400 add_dot_import_object(Named_object
*);
402 // Add an identifier to the list of names seen in the file block.
404 add_file_block_name(const std::string
& name
, Location location
)
405 { this->file_block_names_
[name
] = location
; }
407 // Mark all local variables in current bindings as used. This is
408 // used when there is a parse error to avoid useless errors.
412 // Return a name to use for an error case. This should only be used
413 // after reporting an error, and is used to avoid useless knockon
418 // Return whether the name indicates an error.
420 is_erroneous_name(const std::string
&);
422 // Return a name to use for a thunk function. A thunk function is
423 // one we create during the compilation, for a go statement or a
424 // defer statement or a method expression.
428 // Return whether an object is a thunk.
430 is_thunk(const Named_object
*);
432 // Note that we've seen an interface type. This is used to build
433 // all required interface method tables.
435 record_interface_type(Interface_type
*);
437 // Note that we need an initialization function.
440 { this->need_init_fn_
= true; }
442 // Clear out all names in file scope. This is called when we start
443 // parsing a new file.
447 // Record that VAR1 must be initialized after VAR2. This is used
448 // when VAR2 does not appear in VAR1's INIT or PREINIT.
450 record_var_depends_on(Variable
* var1
, Named_object
* var2
)
452 go_assert(this->var_deps_
.find(var1
) == this->var_deps_
.end());
453 this->var_deps_
[var1
] = var2
;
456 // Return the variable that VAR depends on, or NULL if none.
458 var_depends_on(Variable
* var
) const
460 Var_deps::const_iterator p
= this->var_deps_
.find(var
);
461 return p
!= this->var_deps_
.end() ? p
->second
: NULL
;
464 // Queue up a type-specific function to be written out. This is
465 // used when a type-specific function is needed when not at the top
468 queue_specific_type_function(Type
* type
, Named_type
* name
,
469 const std::string
& hash_name
,
470 Function_type
* hash_fntype
,
471 const std::string
& equal_name
,
472 Function_type
* equal_fntype
);
474 // Write out queued specific type functions.
476 write_specific_type_functions();
478 // Whether we are done writing out specific type functions.
480 specific_type_functions_are_written() const
481 { return this->specific_type_functions_are_written_
; }
483 // Traverse the tree. See the Traverse class.
487 // Define the predeclared global names.
489 define_global_names();
491 // Verify and complete all types.
495 // Lower the parse tree.
499 // Lower all the statements in a block.
501 lower_block(Named_object
* function
, Block
*);
503 // Lower an expression.
505 lower_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
509 lower_constant(Named_object
*);
511 // Flatten all the statements in a block.
513 flatten_block(Named_object
* function
, Block
*);
515 // Flatten an expression.
517 flatten_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
519 // Create all necessary function descriptors.
521 create_function_descriptors();
523 // Finalize the method lists and build stub methods for named types.
527 // Work out the types to use for unspecified variables and
532 // Type check the program.
536 // Check the types in a single block. This is used for complicated
539 check_types_in_block(Block
*);
541 // Check for return statements.
543 check_return_statements();
549 // Add an import control function for an imported package to the
552 add_import_init_fn(const std::string
& package_name
,
553 const std::string
& init_name
, int prio
);
555 // Turn short-cut operators (&&, ||) into explicit if statements.
559 // Use temporary variables to force order of evaluation.
563 // Flatten parse tree.
567 // Build thunks for functions which call recover.
569 build_recover_thunks();
571 // Simplify statements which might use thunks: go and defer
574 simplify_thunk_statements();
576 // Dump AST if -fgo-dump-ast is set
578 dump_ast(const char* basename
);
580 // Convert named types to the backend representation.
582 convert_named_types();
584 // Convert named types in a list of bindings.
586 convert_named_types_in_bindings(Bindings
*);
588 // True if named types have been converted to the backend
591 named_types_are_converted() const
592 { return this->named_types_are_converted_
; }
594 // Return the variable to use for the zero value of TYPE. All types
595 // shared the same zero value, and we make sure that it is large
598 zero_value(Type
*type
);
600 // Return whether a variable is the zero value variable.
602 is_zero_value(Variable
* v
) const;
604 // Create the zero value variable.
606 backend_zero_value();
608 // Write out the global values.
612 // Build a call to the runtime error function.
614 runtime_error(int code
, Location
);
616 // Build required interface method tables.
618 build_interface_method_tables();
620 // Return an expression which allocates memory to hold values of type TYPE.
622 allocate_memory(Type
*type
, Location
);
624 // Get the name of the magic initialization function.
629 // During parsing, we keep a stack of functions. Each function on
630 // the stack is one that we are currently parsing. For each
631 // function, we keep track of the current stack of blocks.
635 Named_object
* function
;
636 // The stack of active blocks in the function.
637 std::vector
<Block
*> blocks
;
640 // The stack of functions.
641 typedef std::vector
<Open_function
> Open_functions
;
643 // Set up the built-in unsafe package.
645 import_unsafe(const std::string
&, bool is_exported
, Location
);
647 // Return the current binding contour.
652 current_bindings() const;
654 // Get the decl for the magic initialization function.
656 initialization_function_decl();
658 // Create the magic initialization function.
660 create_initialization_function(Named_object
* fndecl
, Bstatement
* code_stmt
);
662 // Initialize imported packages.
664 init_imports(std::vector
<Bstatement
*>&);
666 // Register variables with the garbage collector.
668 register_gc_vars(const std::vector
<Named_object
*>&,
669 std::vector
<Bstatement
*>&);
671 // Type used to map import names to packages.
672 typedef std::map
<std::string
, Package
*> Imports
;
674 // Type used to map package names to packages.
675 typedef std::map
<std::string
, Package
*> Packages
;
677 // Type used to map variables to the function calls that set them.
678 // This is used for initialization dependency analysis.
679 typedef std::map
<Variable
*, Named_object
*> Var_deps
;
681 // Type used to map identifiers in the file block to the location
682 // where they were defined.
683 typedef Unordered_map(std::string
, Location
) File_block_names
;
685 // Type used to queue writing a type specific function.
686 struct Specific_type_function
690 std::string hash_name
;
691 Function_type
* hash_fntype
;
692 std::string equal_name
;
693 Function_type
* equal_fntype
;
695 Specific_type_function(Type
* atype
, Named_type
* aname
,
696 const std::string
& ahash_name
,
697 Function_type
* ahash_fntype
,
698 const std::string
& aequal_name
,
699 Function_type
* aequal_fntype
)
700 : type(atype
), name(aname
), hash_name(ahash_name
),
701 hash_fntype(ahash_fntype
), equal_name(aequal_name
),
702 equal_fntype(aequal_fntype
)
706 // The backend generator.
708 // The object used to keep track of file names and line numbers.
710 // The package we are compiling.
712 // The list of currently open functions during parsing.
713 Open_functions functions_
;
714 // The global binding contour. This includes the builtin functions
715 // and the package we are compiling.
717 // The list of names we have seen in the file block.
718 File_block_names file_block_names_
;
719 // Mapping from import file names to packages.
721 // Whether the magic unsafe package was imported.
722 bool imported_unsafe_
;
723 // Mapping from package names we have seen to packages. This does
724 // not include the package we are compiling.
726 // The functions named "init", if there are any.
727 std::vector
<Named_object
*> init_functions_
;
728 // A mapping from variables to the function calls that initialize
729 // them, if it is not stored in the variable's init or preinit.
730 // This is used for dependency analysis.
732 // Whether we need a magic initialization function.
734 // The name of the magic initialization function.
735 std::string init_fn_name_
;
736 // A list of import control variables for packages that we import.
737 std::set
<Import_init
> imported_init_fns_
;
738 // The package path used for reflection data.
739 std::string pkgpath_
;
740 // The package path to use for a symbol name.
741 std::string pkgpath_symbol_
;
742 // The prefix to use for symbols, from the -fgo-prefix option.
744 // The special zero value variable.
745 Named_object
* zero_value_
;
746 // The size of the zero value variable.
747 unsigned long zero_value_size_
;
748 // The alignment of the zero value variable, in bytes.
749 unsigned long zero_value_align_
;
750 // Whether pkgpath_ has been set.
752 // Whether an explicit package path was set by -fgo-pkgpath.
753 bool pkgpath_from_option_
;
754 // Whether an explicit prefix was set by -fgo-prefix.
755 bool prefix_from_option_
;
756 // The relative import path, from the -fgo-relative-import-path
758 std::string relative_import_path_
;
759 // Whether or not to check for division by zero, from the
760 // -fgo-check-divide-zero option.
761 bool check_divide_by_zero_
;
762 // Whether or not to check for division overflow, from the
763 // -fgo-check-divide-overflow option.
764 bool check_divide_overflow_
;
765 // A list of types to verify.
766 std::vector
<Type
*> verify_types_
;
767 // A list of interface types defined while parsing.
768 std::vector
<Interface_type
*> interface_types_
;
769 // Type specific functions to write out.
770 std::vector
<Specific_type_function
*> specific_type_functions_
;
771 // Whether we are done writing out specific type functions.
772 bool specific_type_functions_are_written_
;
773 // Whether named types have been converted.
774 bool named_types_are_converted_
;
777 // A block of statements.
782 Block(Block
* enclosing
, Location
);
784 // Return the enclosing block.
787 { return this->enclosing_
; }
789 // Return the bindings of the block.
792 { return this->bindings_
; }
796 { return this->bindings_
; }
798 // Look at the block's statements.
799 const std::vector
<Statement
*>*
801 { return &this->statements_
; }
803 // Return the start location. This is normally the location of the
804 // left curly brace which starts the block.
806 start_location() const
807 { return this->start_location_
; }
809 // Return the end location. This is normally the location of the
810 // right curly brace which ends the block.
813 { return this->end_location_
; }
815 // Add a statement to the block.
817 add_statement(Statement
*);
819 // Add a statement to the front of the block.
821 add_statement_at_front(Statement
*);
823 // Replace a statement in a block.
825 replace_statement(size_t index
, Statement
*);
827 // Add a Statement before statement number INDEX.
829 insert_statement_before(size_t index
, Statement
*);
831 // Add a Statement after statement number INDEX.
833 insert_statement_after(size_t index
, Statement
*);
835 // Set the end location of the block.
837 set_end_location(Location location
)
838 { this->end_location_
= location
; }
840 // Traverse the tree.
844 // Set final types for unspecified variables and constants.
848 // Return true if execution of this block may fall through to the
851 may_fall_through() const;
853 // Convert the block to the backend representation.
855 get_backend(Translate_context
*);
857 // Iterate over statements.
859 typedef std::vector
<Statement
*>::iterator iterator
;
863 { return this->statements_
.begin(); }
867 { return this->statements_
.end(); }
872 // Statements in the block.
873 std::vector
<Statement
*> statements_
;
876 // Location of start of block.
877 Location start_location_
;
878 // Location of end of block.
879 Location end_location_
;
887 Function(Function_type
* type
, Function
*, Block
*, Location
);
889 // Return the function's type.
892 { return this->type_
; }
894 // Return the enclosing function if there is one.
897 { return this->enclosing_
; }
899 // Set the enclosing function. This is used when building thunks
900 // for functions which call recover.
902 set_enclosing(Function
* enclosing
)
904 go_assert(this->enclosing_
== NULL
);
905 this->enclosing_
= enclosing
;
908 // The result variables.
909 typedef std::vector
<Named_object
*> Results
;
911 // Create the result variables in the outer block.
913 create_result_variables(Gogo
*);
915 // Update the named result variables when cloning a function which
918 update_result_variables();
920 // Return the result variables.
923 { return this->results_
; }
927 { return this->is_sink_
; }
931 { this->is_sink_
= true; }
933 // Whether the result variables have names.
935 results_are_named() const
936 { return this->results_are_named_
; }
938 // Whether this method should not be included in the type
943 go_assert(this->is_method());
944 return this->nointerface_
;
947 // Record that this method should not be included in the type
952 go_assert(this->is_method());
953 this->nointerface_
= true;
956 // Record that this function is a stub method created for an unnamed
959 set_is_unnamed_type_stub_method()
961 go_assert(this->is_method());
962 this->is_unnamed_type_stub_method_
= true;
965 // Add a new field to the closure variable.
967 add_closure_field(Named_object
* var
, Location loc
)
968 { this->closure_fields_
.push_back(std::make_pair(var
, loc
)); }
970 // Whether this function needs a closure.
972 needs_closure() const
973 { return !this->closure_fields_
.empty(); }
975 // Return the closure variable, creating it if necessary. This is
976 // passed to the function as a static chain parameter.
980 // Set the closure variable. This is used when building thunks for
981 // functions which call recover.
983 set_closure_var(Named_object
* v
)
985 go_assert(this->closure_var_
== NULL
);
986 this->closure_var_
= v
;
989 // Return the variable for a reference to field INDEX in the closure
992 enclosing_var(unsigned int index
)
994 go_assert(index
< this->closure_fields_
.size());
995 return closure_fields_
[index
].first
;
998 // Set the type of the closure variable if there is one.
1002 // Get the block of statements associated with the function.
1005 { return this->block_
; }
1007 // Get the location of the start of the function.
1010 { return this->location_
; }
1012 // Return whether this function is actually a method.
1016 // Add a label definition to the function.
1018 add_label_definition(Gogo
*, const std::string
& label_name
, Location
);
1020 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
1021 // if we should report errors for a goto from the current location
1022 // to the label location.
1024 add_label_reference(Gogo
*, const std::string
& label_name
,
1025 Location
, bool issue_goto_errors
);
1027 // Warn about labels that are defined but not used.
1029 check_labels() const;
1031 // Note that a new local type has been added. Return its index.
1033 new_local_type_index()
1034 { return this->local_type_count_
++; }
1036 // Whether this function calls the predeclared recover function.
1038 calls_recover() const
1039 { return this->calls_recover_
; }
1041 // Record that this function calls the predeclared recover function.
1042 // This is set during the lowering pass.
1045 { this->calls_recover_
= true; }
1047 // Whether this is a recover thunk function.
1049 is_recover_thunk() const
1050 { return this->is_recover_thunk_
; }
1052 // Record that this is a thunk built for a function which calls
1055 set_is_recover_thunk()
1056 { this->is_recover_thunk_
= true; }
1058 // Whether this function already has a recover thunk.
1060 has_recover_thunk() const
1061 { return this->has_recover_thunk_
; }
1063 // Record that this function already has a recover thunk.
1065 set_has_recover_thunk()
1066 { this->has_recover_thunk_
= true; }
1068 // Mark the function as going into a unique section.
1070 set_in_unique_section()
1071 { this->in_unique_section_
= true; }
1073 // Swap with another function. Used only for the thunk which calls
1076 swap_for_recover(Function
*);
1078 // Traverse the tree.
1080 traverse(Traverse
*);
1082 // Determine types in the function.
1086 // Return an expression for the function descriptor, given the named
1087 // object for this function. This may only be called for functions
1088 // without a closure. This will be an immutable struct with one
1089 // field that points to the function's code.
1091 descriptor(Gogo
*, Named_object
*);
1093 // Set the descriptor for this function. This is used when a
1094 // function declaration is followed by a function definition.
1096 set_descriptor(Expression
* descriptor
)
1098 go_assert(this->descriptor_
== NULL
);
1099 this->descriptor_
= descriptor
;
1102 // Return the backend representation.
1104 get_or_make_decl(Gogo
*, Named_object
*);
1106 // Return the function's decl after it has been built.
1110 // Set the function decl to hold a backend representation of the function
1113 build(Gogo
*, Named_object
*);
1115 // Get the statement that assigns values to this function's result struct.
1117 return_value(Gogo
*, Named_object
*, Location
) const;
1119 // Get an expression for the variable holding the defer stack.
1121 defer_stack(Location
);
1123 // Export the function.
1125 export_func(Export
*, const std::string
& name
) const;
1127 // Export a function with a type.
1129 export_func_with_type(Export
*, const std::string
& name
,
1130 const Function_type
*);
1132 // Import a function.
1134 import_func(Import
*, std::string
* pname
, Typed_identifier
** receiver
,
1135 Typed_identifier_list
** pparameters
,
1136 Typed_identifier_list
** presults
, bool* is_varargs
);
1139 // Type for mapping from label names to Label objects.
1140 typedef Unordered_map(std::string
, Label
*) Labels
;
1143 build_defer_wrapper(Gogo
*, Named_object
*, Bstatement
**, Bstatement
**);
1145 typedef std::vector
<std::pair
<Named_object
*,
1146 Location
> > Closure_fields
;
1148 // The function's type.
1149 Function_type
* type_
;
1150 // The enclosing function. This is NULL when there isn't one, which
1151 // is the normal case.
1152 Function
* enclosing_
;
1153 // The result variables, if any.
1155 // If there is a closure, this is the list of variables which appear
1156 // in the closure. This is created by the parser, and then resolved
1157 // to a real type when we lower parse trees.
1158 Closure_fields closure_fields_
;
1159 // The closure variable, passed as a parameter using the static
1160 // chain parameter. Normally NULL.
1161 Named_object
* closure_var_
;
1162 // The outer block of statements in the function.
1164 // The source location of the start of the function.
1166 // Labels defined or referenced in the function.
1168 // The number of local types defined in this function.
1169 unsigned int local_type_count_
;
1170 // The function descriptor, if any.
1171 Expression
* descriptor_
;
1172 // The function decl.
1174 // The defer stack variable. A pointer to this variable is used to
1175 // distinguish the defer stack for one function from another. This
1176 // is NULL unless we actually need a defer stack.
1177 Temporary_statement
* defer_stack_
;
1178 // True if this function is sink-named. No code is generated.
1180 // True if the result variables are named.
1181 bool results_are_named_
: 1;
1182 // True if this method should not be included in the type descriptor.
1183 bool nointerface_
: 1;
1184 // True if this function is a stub method created for an unnamed
1186 bool is_unnamed_type_stub_method_
: 1;
1187 // True if this function calls the predeclared recover function.
1188 bool calls_recover_
: 1;
1189 // True if this a thunk built for a function which calls recover.
1190 bool is_recover_thunk_
: 1;
1191 // True if this function already has a recover thunk.
1192 bool has_recover_thunk_
: 1;
1193 // True if this function should be put in a unique section. This is
1194 // turned on for field tracking.
1195 bool in_unique_section_
: 1;
1198 // A snapshot of the current binding state.
1200 class Bindings_snapshot
1203 Bindings_snapshot(const Block
*, Location
);
1205 // Report any errors appropriate for a goto from the current binding
1206 // state of B to this one.
1208 check_goto_from(const Block
* b
, Location
);
1210 // Report any errors appropriate for a goto from this binding state
1211 // to the current state of B.
1213 check_goto_to(const Block
* b
);
1217 check_goto_block(Location
, const Block
*, const Block
*, size_t*);
1220 check_goto_defs(Location
, const Block
*, size_t, size_t);
1222 // The current block.
1223 const Block
* block_
;
1224 // The number of names currently defined in each open block.
1225 // Element 0 is this->block_, element 1 is
1226 // this->block_->enclosing(), etc.
1227 std::vector
<size_t> counts_
;
1228 // The location where this snapshot was taken.
1232 // A function declaration.
1234 class Function_declaration
1237 Function_declaration(Function_type
* fntype
, Location location
)
1238 : fntype_(fntype
), location_(location
), asm_name_(), descriptor_(NULL
),
1244 { return this->fntype_
; }
1248 { return this->location_
; }
1252 { return this->asm_name_
; }
1254 // Set the assembler name.
1256 set_asm_name(const std::string
& asm_name
)
1257 { this->asm_name_
= asm_name
; }
1259 // Return an expression for the function descriptor, given the named
1260 // object for this function. This may only be called for functions
1261 // without a closure. This will be an immutable struct with one
1262 // field that points to the function's code.
1264 descriptor(Gogo
*, Named_object
*);
1266 // Return true if we have created a descriptor for this declaration.
1268 has_descriptor() const
1269 { return this->descriptor_
!= NULL
; }
1271 // Return a backend representation.
1273 get_or_make_decl(Gogo
*, Named_object
*);
1275 // If there is a descriptor, build it into the backend
1278 build_backend_descriptor(Gogo
*);
1280 // Export a function declaration.
1282 export_func(Export
* exp
, const std::string
& name
) const
1283 { Function::export_func_with_type(exp
, name
, this->fntype_
); }
1286 // The type of the function.
1287 Function_type
* fntype_
;
1288 // The location of the declaration.
1290 // The assembler name: this is the name to use in references to the
1291 // function. This is normally empty.
1292 std::string asm_name_
;
1293 // The function descriptor, if any.
1294 Expression
* descriptor_
;
1295 // The function decl if needed.
1304 Variable(Type
*, Expression
*, bool is_global
, bool is_parameter
,
1305 bool is_receiver
, Location
);
1307 // Get the type of the variable.
1314 // Return whether the type is defined yet.
1318 // Get the initial value.
1321 { return this->init_
; }
1323 // Return whether there are any preinit statements.
1325 has_pre_init() const
1326 { return this->preinit_
!= NULL
; }
1328 // Return the preinit statements if any.
1331 { return this->preinit_
; }
1333 // Return whether this is a global variable.
1336 { return this->is_global_
; }
1338 // Return whether this is a function parameter.
1340 is_parameter() const
1341 { return this->is_parameter_
; }
1343 // Return whether this is the receiver parameter of a method.
1346 { return this->is_receiver_
; }
1348 // Change this parameter to be a receiver. This is used when
1349 // creating the thunks created for functions which call recover.
1353 go_assert(this->is_parameter_
);
1354 this->is_receiver_
= true;
1357 // Change this parameter to not be a receiver. This is used when
1358 // creating the thunks created for functions which call recover.
1360 set_is_not_receiver()
1362 go_assert(this->is_parameter_
);
1363 this->is_receiver_
= false;
1366 // Return whether this is the varargs parameter of a function.
1368 is_varargs_parameter() const
1369 { return this->is_varargs_parameter_
; }
1371 // Whether this variable's address is taken.
1373 is_address_taken() const
1374 { return this->is_address_taken_
; }
1376 // Whether this variable should live in the heap.
1379 { return this->is_address_taken_
&& !this->is_global_
; }
1381 // Note that something takes the address of this variable.
1384 { this->is_address_taken_
= true; }
1386 // Return whether the address is taken but does not escape.
1388 is_non_escaping_address_taken() const
1389 { return this->is_non_escaping_address_taken_
; }
1391 // Note that something takes the address of this variable such that
1392 // the address does not escape the function.
1394 set_non_escaping_address_taken()
1395 { this->is_non_escaping_address_taken_
= true; }
1397 // Get the source location of the variable's declaration.
1400 { return this->location_
; }
1402 // Record that this is the varargs parameter of a function.
1404 set_is_varargs_parameter()
1406 go_assert(this->is_parameter_
);
1407 this->is_varargs_parameter_
= true;
1410 // Return whether the variable has been used.
1413 { return this->is_used_
; }
1415 // Mark that the variable has been used.
1418 { this->is_used_
= true; }
1420 // Clear the initial value; used for error handling.
1423 { this->init_
= NULL
; }
1425 // Set the initial value; used for converting shortcuts.
1427 set_init(Expression
* init
)
1428 { this->init_
= init
; }
1430 // Get the preinit block, a block of statements to be run before the
1431 // initialization expression.
1433 preinit_block(Gogo
*);
1435 // Add a statement to be run before the initialization expression.
1436 // This is only used for global variables.
1438 add_preinit_statement(Gogo
*, Statement
*);
1440 // Lower the initialization expression after parsing is complete.
1442 lower_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1444 // Flatten the initialization expression after ordering evaluations.
1446 flatten_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1448 // A special case: the init value is used only to determine the
1449 // type. This is used if the variable is defined using := with the
1450 // comma-ok form of a map index or a receive expression. The init
1451 // value is actually the map index expression or receive expression.
1452 // We use this because we may not know the right type at parse time.
1454 set_type_from_init_tuple()
1455 { this->type_from_init_tuple_
= true; }
1457 // Another special case: the init value is used only to determine
1458 // the type. This is used if the variable is defined using := with
1459 // a range clause. The init value is the range expression. The
1460 // type of the variable is the index type of the range expression
1461 // (i.e., the first value returned by a range).
1463 set_type_from_range_index()
1464 { this->type_from_range_index_
= true; }
1466 // Another special case: like set_type_from_range_index, but the
1467 // type is the value type of the range expression (i.e., the second
1468 // value returned by a range).
1470 set_type_from_range_value()
1471 { this->type_from_range_value_
= true; }
1473 // Another special case: the init value is used only to determine
1474 // the type. This is used if the variable is defined using := with
1475 // a case in a select statement. The init value is the channel.
1476 // The type of the variable is the channel's element type.
1478 set_type_from_chan_element()
1479 { this->type_from_chan_element_
= true; }
1481 // After we lower the select statement, we once again set the type
1482 // from the initialization expression.
1484 clear_type_from_chan_element()
1486 go_assert(this->type_from_chan_element_
);
1487 this->type_from_chan_element_
= false;
1490 // Note that this variable was created for a type switch clause.
1492 set_is_type_switch_var()
1493 { this->is_type_switch_var_
= true; }
1495 // Mark the variable as going into a unique section.
1497 set_in_unique_section()
1499 go_assert(this->is_global_
);
1500 this->in_unique_section_
= true;
1503 // Traverse the initializer expression.
1505 traverse_expression(Traverse
*, unsigned int traverse_mask
);
1507 // Determine the type of the variable if necessary.
1511 // Get the backend representation of the variable.
1513 get_backend_variable(Gogo
*, Named_object
*, const Package
*,
1514 const std::string
&);
1516 // Get the initial value of the variable. This may only
1517 // be called if has_pre_init() returns false.
1519 get_init(Gogo
*, Named_object
* function
);
1521 // Return a series of statements which sets the value of the
1522 // variable in DECL. This should only be called is has_pre_init()
1523 // returns true. DECL may be NULL for a sink variable.
1525 get_init_block(Gogo
*, Named_object
* function
, Bvariable
* decl
);
1527 // Export the variable.
1529 export_var(Export
*, const std::string
& name
) const;
1531 // Import a variable.
1533 import_var(Import
*, std::string
* pname
, Type
** ptype
);
1536 // The type of a tuple.
1538 type_from_tuple(Expression
*, bool) const;
1540 // The type of a range.
1542 type_from_range(Expression
*, bool, bool) const;
1544 // The element type of a channel.
1546 type_from_chan_element(Expression
*, bool) const;
1548 // The variable's type. This may be NULL if the type is set from
1551 // The initial value. This may be NULL if the variable should be
1552 // initialized to the default value for the type.
1554 // Statements to run before the init statement.
1556 // Location of variable definition.
1558 // Backend representation.
1559 Bvariable
* backend_
;
1560 // Whether this is a global variable.
1561 bool is_global_
: 1;
1562 // Whether this is a function parameter.
1563 bool is_parameter_
: 1;
1564 // Whether this is the receiver parameter of a method.
1565 bool is_receiver_
: 1;
1566 // Whether this is the varargs parameter of a function.
1567 bool is_varargs_parameter_
: 1;
1568 // Whether this variable is ever referenced.
1570 // Whether something takes the address of this variable. For a
1571 // local variable this implies that the variable has to be on the
1573 bool is_address_taken_
: 1;
1574 // Whether something takes the address of this variable such that
1575 // the address does not escape the function.
1576 bool is_non_escaping_address_taken_
: 1;
1577 // True if we have seen this variable in a traversal.
1579 // True if we have lowered the initialization expression.
1580 bool init_is_lowered_
: 1;
1581 // True if we have flattened the initialization expression.
1582 bool init_is_flattened_
: 1;
1583 // True if init is a tuple used to set the type.
1584 bool type_from_init_tuple_
: 1;
1585 // True if init is a range clause and the type is the index type.
1586 bool type_from_range_index_
: 1;
1587 // True if init is a range clause and the type is the value type.
1588 bool type_from_range_value_
: 1;
1589 // True if init is a channel and the type is the channel's element type.
1590 bool type_from_chan_element_
: 1;
1591 // True if this is a variable created for a type switch case.
1592 bool is_type_switch_var_
: 1;
1593 // True if we have determined types.
1594 bool determined_type_
: 1;
1595 // True if this variable should be put in a unique section. This is
1596 // used for field tracking.
1597 bool in_unique_section_
: 1;
1600 // A variable which is really the name for a function return value, or
1603 class Result_variable
1606 Result_variable(Type
* type
, Function
* function
, int index
,
1608 : type_(type
), function_(function
), index_(index
), location_(location
),
1609 backend_(NULL
), is_address_taken_(false),
1610 is_non_escaping_address_taken_(false)
1613 // Get the type of the result variable.
1616 { return this->type_
; }
1618 // Get the function that this is associated with.
1621 { return this->function_
; }
1623 // Index in the list of function results.
1626 { return this->index_
; }
1628 // The location of the variable definition.
1631 { return this->location_
; }
1633 // Whether this variable's address is taken.
1635 is_address_taken() const
1636 { return this->is_address_taken_
; }
1638 // Note that something takes the address of this variable.
1641 { this->is_address_taken_
= true; }
1643 // Return whether the address is taken but does not escape.
1645 is_non_escaping_address_taken() const
1646 { return this->is_non_escaping_address_taken_
; }
1648 // Note that something takes the address of this variable such that
1649 // the address does not escape the function.
1651 set_non_escaping_address_taken()
1652 { this->is_non_escaping_address_taken_
= true; }
1654 // Whether this variable should live in the heap.
1657 { return this->is_address_taken_
; }
1659 // Set the function. This is used when cloning functions which call
1662 set_function(Function
* function
)
1663 { this->function_
= function
; }
1665 // Get the backend representation of the variable.
1667 get_backend_variable(Gogo
*, Named_object
*, const std::string
&);
1670 // Type of result variable.
1672 // Function with which this is associated.
1673 Function
* function_
;
1674 // Index in list of results.
1676 // Where the result variable is defined.
1678 // Backend representation.
1679 Bvariable
* backend_
;
1680 // Whether something takes the address of this variable.
1681 bool is_address_taken_
;
1682 // Whether something takes the address of this variable such that
1683 // the address does not escape the function.
1684 bool is_non_escaping_address_taken_
;
1687 // The value we keep for a named constant. This lets us hold a type
1688 // and an expression.
1690 class Named_constant
1693 Named_constant(Type
* type
, Expression
* expr
, int iota_value
,
1695 : type_(type
), expr_(expr
), iota_value_(iota_value
), location_(location
),
1696 lowering_(false), is_sink_(false), bconst_(NULL
)
1701 { return this->type_
; }
1705 { return this->expr_
; }
1709 { return this->iota_value_
; }
1713 { return this->location_
; }
1715 // Whether we are lowering.
1718 { return this->lowering_
; }
1720 // Set that we are lowering.
1723 { this->lowering_
= true; }
1725 // We are no longer lowering.
1728 { this->lowering_
= false; }
1732 { return this->is_sink_
; }
1736 { this->is_sink_
= true; }
1738 // Traverse the expression.
1740 traverse_expression(Traverse
*);
1742 // Determine the type of the constant if necessary.
1746 // Indicate that we found and reported an error for this constant.
1750 // Export the constant.
1752 export_const(Export
*, const std::string
& name
) const;
1754 // Import a constant.
1756 import_const(Import
*, std::string
*, Type
**, Expression
**);
1758 // Get the backend representation of the constant value.
1760 get_backend(Gogo
*, Named_object
*);
1763 // The type of the constant.
1765 // The expression for the constant.
1767 // If the predeclared constant iota is used in EXPR_, this is the
1768 // value it will have. We do this because at parse time we don't
1769 // know whether the name "iota" will refer to the predeclared
1770 // constant or to something else. We put in the right value in when
1773 // The location of the definition.
1775 // Whether we are currently lowering this constant.
1777 // Whether this constant is blank named and needs only type checking.
1779 // The backend representation of the constant value.
1780 Bexpression
* bconst_
;
1783 // A type declaration.
1785 class Type_declaration
1788 Type_declaration(Location location
)
1789 : location_(location
), in_function_(NULL
), in_function_index_(0),
1790 methods_(), issued_warning_(false)
1793 // Return the location.
1796 { return this->location_
; }
1798 // Return the function in which this type is declared. This will
1799 // return NULL for a type declared in global scope.
1801 in_function(unsigned int* pindex
)
1803 *pindex
= this->in_function_index_
;
1804 return this->in_function_
;
1807 // Set the function in which this type is declared.
1809 set_in_function(Named_object
* f
, unsigned int index
)
1811 this->in_function_
= f
;
1812 this->in_function_index_
= index
;
1815 // Add a method to this type. This is used when methods are defined
1818 add_method(const std::string
& name
, Function
* function
);
1820 // Add a method declaration to this type.
1822 add_method_declaration(const std::string
& name
, Package
*,
1823 Function_type
* type
, Location location
);
1825 // Return whether any methods were defined.
1827 has_methods() const;
1829 // Return the methods.
1830 const std::vector
<Named_object
*>*
1832 { return &this->methods_
; }
1834 // Define methods when the real type is known.
1836 define_methods(Named_type
*);
1838 // This is called if we are trying to use this type. It returns
1839 // true if we should issue a warning.
1844 // The location of the type declaration.
1846 // If this type is declared in a function, a pointer back to the
1847 // function in which it is defined.
1848 Named_object
* in_function_
;
1849 // The index of this type in IN_FUNCTION_.
1850 unsigned int in_function_index_
;
1851 // Methods defined before the type is defined.
1852 std::vector
<Named_object
*> methods_
;
1853 // True if we have issued a warning about a use of this type
1854 // declaration when it is undefined.
1855 bool issued_warning_
;
1858 // An unknown object. These are created by the parser for forward
1859 // references to names which have not been seen before. In a correct
1860 // program, these will always point to a real definition by the end of
1861 // the parse. Because they point to another Named_object, these may
1862 // only be referenced by Unknown_expression objects.
1867 Unknown_name(Location location
)
1868 : location_(location
), real_named_object_(NULL
)
1871 // Return the location where this name was first seen.
1874 { return this->location_
; }
1876 // Return the real named object that this points to, or NULL if it
1877 // was never resolved.
1879 real_named_object() const
1880 { return this->real_named_object_
; }
1882 // Set the real named object that this points to.
1884 set_real_named_object(Named_object
* no
);
1887 // The location where this name was first seen.
1889 // The real named object when it is known.
1894 // A named object named. This is the result of a declaration. We
1895 // don't use a superclass because they all have to be handled
1903 // An uninitialized Named_object. We should never see this.
1904 NAMED_OBJECT_UNINITIALIZED
,
1905 // An erroneous name. This indicates a parse error, to avoid
1906 // later errors about undefined references.
1907 NAMED_OBJECT_ERRONEOUS
,
1908 // An unknown name. This is used for forward references. In a
1909 // correct program, these will all be resolved by the end of the
1911 NAMED_OBJECT_UNKNOWN
,
1916 // A forward type declaration.
1917 NAMED_OBJECT_TYPE_DECLARATION
,
1920 // A result variable in a function.
1921 NAMED_OBJECT_RESULT_VAR
,
1922 // The blank identifier--the special variable named _.
1926 // A forward func declaration.
1927 NAMED_OBJECT_FUNC_DECLARATION
,
1929 NAMED_OBJECT_PACKAGE
1932 // Return the classification.
1934 classification() const
1935 { return this->classification_
; }
1940 is_erroneous() const
1941 { return this->classification_
== NAMED_OBJECT_ERRONEOUS
; }
1945 { return this->classification_
== NAMED_OBJECT_UNKNOWN
; }
1949 { return this->classification_
== NAMED_OBJECT_CONST
; }
1953 { return this->classification_
== NAMED_OBJECT_TYPE
; }
1956 is_type_declaration() const
1957 { return this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
; }
1961 { return this->classification_
== NAMED_OBJECT_VAR
; }
1964 is_result_variable() const
1965 { return this->classification_
== NAMED_OBJECT_RESULT_VAR
; }
1969 { return this->classification_
== NAMED_OBJECT_SINK
; }
1973 { return this->classification_
== NAMED_OBJECT_FUNC
; }
1976 is_function_declaration() const
1977 { return this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
; }
1981 { return this->classification_
== NAMED_OBJECT_PACKAGE
; }
1985 static Named_object
*
1986 make_erroneous_name(const std::string
& name
)
1987 { return new Named_object(name
, NULL
, NAMED_OBJECT_ERRONEOUS
); }
1989 static Named_object
*
1990 make_unknown_name(const std::string
& name
, Location
);
1992 static Named_object
*
1993 make_constant(const Typed_identifier
&, const Package
*, Expression
*,
1996 static Named_object
*
1997 make_type(const std::string
&, const Package
*, Type
*, Location
);
1999 static Named_object
*
2000 make_type_declaration(const std::string
&, const Package
*, Location
);
2002 static Named_object
*
2003 make_variable(const std::string
&, const Package
*, Variable
*);
2005 static Named_object
*
2006 make_result_variable(const std::string
&, Result_variable
*);
2008 static Named_object
*
2011 static Named_object
*
2012 make_function(const std::string
&, const Package
*, Function
*);
2014 static Named_object
*
2015 make_function_declaration(const std::string
&, const Package
*, Function_type
*,
2018 static Named_object
*
2019 make_package(const std::string
& alias
, Package
* package
);
2026 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2027 return this->u_
.unknown_value
;
2031 unknown_value() const
2033 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
2034 return this->u_
.unknown_value
;
2040 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2041 return this->u_
.const_value
;
2044 const Named_constant
*
2047 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2048 return this->u_
.const_value
;
2054 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2055 return this->u_
.type_value
;
2061 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2062 return this->u_
.type_value
;
2066 type_declaration_value()
2068 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2069 return this->u_
.type_declaration
;
2072 const Type_declaration
*
2073 type_declaration_value() const
2075 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2076 return this->u_
.type_declaration
;
2082 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2083 return this->u_
.var_value
;
2089 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2090 return this->u_
.var_value
;
2096 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2097 return this->u_
.result_var_value
;
2100 const Result_variable
*
2101 result_var_value() const
2103 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2104 return this->u_
.result_var_value
;
2110 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2111 return this->u_
.func_value
;
2117 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2118 return this->u_
.func_value
;
2121 Function_declaration
*
2122 func_declaration_value()
2124 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2125 return this->u_
.func_declaration_value
;
2128 const Function_declaration
*
2129 func_declaration_value() const
2131 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2132 return this->u_
.func_declaration_value
;
2138 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2139 return this->u_
.package_value
;
2143 package_value() const
2145 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2146 return this->u_
.package_value
;
2151 { return this->name_
; }
2153 // Return the name to use in an error message. The difference is
2154 // that if this Named_object is defined in a different package, this
2155 // will return PACKAGE.NAME.
2157 message_name() const;
2161 { return this->package_
; }
2163 // Resolve an unknown value if possible. This returns the same
2164 // Named_object or a new one.
2168 Named_object
* ret
= this;
2169 if (this->is_unknown())
2171 Named_object
* r
= this->unknown_value()->real_named_object();
2181 const Named_object
* ret
= this;
2182 if (this->is_unknown())
2184 const Named_object
* r
= this->unknown_value()->real_named_object();
2191 // The location where this object was defined or referenced.
2195 // Convert a variable to the backend representation.
2197 get_backend_variable(Gogo
*, Named_object
* function
);
2199 // Return the external identifier for this object.
2203 // Get the backend representation of this object.
2205 get_backend(Gogo
*, std::vector
<Bexpression
*>&, std::vector
<Btype
*>&,
2206 std::vector
<Bfunction
*>&);
2208 // Define a type declaration.
2210 set_type_value(Named_type
*);
2212 // Define a function declaration.
2214 set_function_value(Function
*);
2216 // Declare an unknown name as a type declaration.
2220 // Export this object.
2222 export_named_object(Export
*) const;
2225 Named_object(const std::string
&, const Package
*, Classification
);
2227 // The name of the object.
2229 // The package that this object is in. This is NULL if it is in the
2230 // file we are compiling.
2231 const Package
* package_
;
2232 // The type of object this is.
2233 Classification classification_
;
2237 Unknown_name
* unknown_value
;
2238 Named_constant
* const_value
;
2239 Named_type
* type_value
;
2240 Type_declaration
* type_declaration
;
2241 Variable
* var_value
;
2242 Result_variable
* result_var_value
;
2243 Function
* func_value
;
2244 Function_declaration
* func_declaration_value
;
2245 Package
* package_value
;
2249 // A binding contour. This binds names to objects.
2254 // Type for mapping from names to objects.
2255 typedef Unordered_map(std::string
, Named_object
*) Contour
;
2257 Bindings(Bindings
* enclosing
);
2259 // Add an erroneous name.
2261 add_erroneous_name(const std::string
& name
)
2262 { return this->add_named_object(Named_object::make_erroneous_name(name
)); }
2264 // Add an unknown name.
2266 add_unknown_name(const std::string
& name
, Location location
)
2268 return this->add_named_object(Named_object::make_unknown_name(name
,
2274 add_constant(const Typed_identifier
& tid
, const Package
* package
,
2275 Expression
* expr
, int iota_value
)
2277 return this->add_named_object(Named_object::make_constant(tid
, package
,
2284 add_type(const std::string
& name
, const Package
* package
, Type
* type
,
2287 return this->add_named_object(Named_object::make_type(name
, package
, type
,
2291 // Add a named type. This is used for builtin types, and to add an
2292 // imported type to the global scope.
2294 add_named_type(Named_type
* named_type
);
2296 // Add a type declaration.
2298 add_type_declaration(const std::string
& name
, const Package
* package
,
2301 Named_object
* no
= Named_object::make_type_declaration(name
, package
,
2303 return this->add_named_object(no
);
2308 add_variable(const std::string
& name
, const Package
* package
,
2311 return this->add_named_object(Named_object::make_variable(name
, package
,
2315 // Add a result variable.
2317 add_result_variable(const std::string
& name
, Result_variable
* result
)
2319 return this->add_named_object(Named_object::make_result_variable(name
,
2325 add_function(const std::string
& name
, const Package
*, Function
* function
);
2327 // Add a function declaration.
2329 add_function_declaration(const std::string
& name
, const Package
* package
,
2330 Function_type
* type
, Location location
);
2332 // Add a package. The location is the location of the import
2335 add_package(const std::string
& alias
, Package
* package
)
2337 Named_object
* no
= Named_object::make_package(alias
, package
);
2338 return this->add_named_object(no
);
2341 // Define a type which was already declared.
2343 define_type(Named_object
*, Named_type
*);
2345 // Add a method to the list of objects. This is not added to the
2348 add_method(Named_object
*);
2350 // Add a named object to this binding.
2352 add_named_object(Named_object
* no
)
2353 { return this->add_named_object_to_contour(&this->bindings_
, no
); }
2355 // Clear all names in file scope from the bindings.
2357 clear_file_scope(Gogo
*);
2359 // Look up a name in this binding contour and in any enclosing
2360 // binding contours. This returns NULL if the name is not found.
2362 lookup(const std::string
&) const;
2364 // Look up a name in this binding contour without looking in any
2365 // enclosing binding contours. Returns NULL if the name is not found.
2367 lookup_local(const std::string
&) const;
2371 remove_binding(Named_object
*);
2373 // Mark all variables as used. This is used for some types of parse
2378 // Traverse the tree. See the Traverse class.
2380 traverse(Traverse
*, bool is_global
);
2382 // Iterate over definitions. This does not include things which
2383 // were only declared.
2385 typedef std::vector
<Named_object
*>::const_iterator
2386 const_definitions_iterator
;
2388 const_definitions_iterator
2389 begin_definitions() const
2390 { return this->named_objects_
.begin(); }
2392 const_definitions_iterator
2393 end_definitions() const
2394 { return this->named_objects_
.end(); }
2396 // Return the number of definitions.
2398 size_definitions() const
2399 { return this->named_objects_
.size(); }
2401 // Return whether there are no definitions.
2403 empty_definitions() const
2404 { return this->named_objects_
.empty(); }
2406 // Iterate over declarations. This is everything that has been
2407 // declared, which includes everything which has been defined.
2409 typedef Contour::const_iterator const_declarations_iterator
;
2411 const_declarations_iterator
2412 begin_declarations() const
2413 { return this->bindings_
.begin(); }
2415 const_declarations_iterator
2416 end_declarations() const
2417 { return this->bindings_
.end(); }
2419 // Return the number of declarations.
2421 size_declarations() const
2422 { return this->bindings_
.size(); }
2424 // Return whether there are no declarations.
2426 empty_declarations() const
2427 { return this->bindings_
.empty(); }
2429 // Return the first declaration.
2432 { return this->bindings_
.empty() ? NULL
: this->bindings_
.begin()->second
; }
2436 add_named_object_to_contour(Contour
*, Named_object
*);
2439 new_definition(Named_object
*, Named_object
*);
2441 // Enclosing bindings.
2442 Bindings
* enclosing_
;
2443 // The list of objects.
2444 std::vector
<Named_object
*> named_objects_
;
2445 // The mapping from names to objects.
2454 Label(const std::string
& name
)
2455 : name_(name
), location_(Linemap::unknown_location()), snapshot_(NULL
),
2456 refs_(), is_used_(false), blabel_(NULL
)
2459 // Return the label's name.
2462 { return this->name_
; }
2464 // Return whether the label has been defined.
2467 { return !Linemap::is_unknown_location(this->location_
); }
2469 // Return whether the label has been used.
2472 { return this->is_used_
; }
2474 // Record that the label is used.
2477 { this->is_used_
= true; }
2479 // Return the location of the definition.
2482 { return this->location_
; }
2484 // Return the bindings snapshot.
2487 { return this->snapshot_
; }
2489 // Add a snapshot of a goto which refers to this label.
2491 add_snapshot_ref(Bindings_snapshot
* snapshot
)
2493 go_assert(Linemap::is_unknown_location(this->location_
));
2494 this->refs_
.push_back(snapshot
);
2497 // Return the list of snapshots of goto statements which refer to
2499 const std::vector
<Bindings_snapshot
*>&
2501 { return this->refs_
; }
2503 // Clear the references.
2507 // Define the label at LOCATION with the given bindings snapshot.
2509 define(Location location
, Bindings_snapshot
* snapshot
)
2511 go_assert(Linemap::is_unknown_location(this->location_
)
2512 && this->snapshot_
== NULL
);
2513 this->location_
= location
;
2514 this->snapshot_
= snapshot
;
2517 // Return the backend representation for this label.
2519 get_backend_label(Translate_context
*);
2521 // Return an expression for the address of this label. This is used
2522 // to get the return address of a deferred function to see whether
2523 // the function may call recover.
2525 get_addr(Translate_context
*, Location location
);
2528 // The name of the label.
2530 // The location of the definition. This is 0 if the label has not
2531 // yet been defined.
2533 // A snapshot of the set of bindings defined at this label, used to
2534 // issue errors about invalid goto statements.
2535 Bindings_snapshot
* snapshot_
;
2536 // A list of snapshots of goto statements which refer to this label.
2537 std::vector
<Bindings_snapshot
*> refs_
;
2538 // Whether the label has been used.
2540 // The backend representation.
2544 // An unnamed label. These are used when lowering loops.
2549 Unnamed_label(Location location
)
2550 : location_(location
), blabel_(NULL
)
2553 // Get the location where the label is defined.
2556 { return this->location_
; }
2558 // Set the location where the label is defined.
2560 set_location(Location location
)
2561 { this->location_
= location
; }
2563 // Return a statement which defines this label.
2565 get_definition(Translate_context
*);
2567 // Return a goto to this label from LOCATION.
2569 get_goto(Translate_context
*, Location location
);
2572 // Return the backend representation.
2574 get_blabel(Translate_context
*);
2576 // The location where the label is defined.
2578 // The backend representation of this label.
2582 // An imported package.
2587 Package(const std::string
& pkgpath
, Location location
);
2589 // Get the package path used for all symbols exported from this
2593 { return this->pkgpath_
; }
2595 // Return the package path to use for a symbol name.
2597 pkgpath_symbol() const
2598 { return this->pkgpath_symbol_
; }
2600 // Return the location of the import statement.
2603 { return this->location_
; }
2605 // Return whether we know the name of this package yet.
2607 has_package_name() const
2608 { return !this->package_name_
.empty(); }
2610 // The name that this package uses in its package clause. This may
2611 // be different from the name in the associated Named_object if the
2612 // import statement used an alias.
2614 package_name() const
2616 go_assert(!this->package_name_
.empty());
2617 return this->package_name_
;
2620 // The priority of this package. The init function of packages with
2621 // lower priority must be run before the init function of packages
2622 // with higher priority.
2625 { return this->priority_
; }
2627 // Set the priority.
2629 set_priority(int priority
);
2631 // Return the bindings.
2634 { return this->bindings_
; }
2636 // Whether some symbol from the package was used.
2639 { return this->used_
; }
2641 // Note that some symbol from this package was used.
2644 { this->used_
= true; }
2646 // Clear the used field for the next file.
2649 { this->used_
= false; }
2651 // Whether this package was imported in the current file.
2654 { return this->is_imported_
; }
2656 // Note that this package was imported in the current file.
2659 { this->is_imported_
= true; }
2661 // Clear the imported field for the next file.
2664 { this->is_imported_
= false; }
2666 // Whether this package was imported with a name of "_".
2668 uses_sink_alias() const
2669 { return this->uses_sink_alias_
; }
2671 // Note that this package was imported with a name of "_".
2673 set_uses_sink_alias()
2674 { this->uses_sink_alias_
= true; }
2676 // Clear the sink alias field for the next file.
2678 clear_uses_sink_alias()
2679 { this->uses_sink_alias_
= false; }
2681 // Look up a name in the package. Returns NULL if the name is not
2684 lookup(const std::string
& name
) const
2685 { return this->bindings_
->lookup(name
); }
2687 // Set the name of the package.
2689 set_package_name(const std::string
& name
, Location
);
2691 // Set the location of the package. This is used to record the most
2692 // recent import location.
2694 set_location(Location location
)
2695 { this->location_
= location
; }
2697 // Add a constant to the package.
2699 add_constant(const Typed_identifier
& tid
, Expression
* expr
)
2700 { return this->bindings_
->add_constant(tid
, this, expr
, 0); }
2702 // Add a type to the package.
2704 add_type(const std::string
& name
, Type
* type
, Location location
)
2705 { return this->bindings_
->add_type(name
, this, type
, location
); }
2707 // Add a type declaration to the package.
2709 add_type_declaration(const std::string
& name
, Location location
)
2710 { return this->bindings_
->add_type_declaration(name
, this, location
); }
2712 // Add a variable to the package.
2714 add_variable(const std::string
& name
, Variable
* variable
)
2715 { return this->bindings_
->add_variable(name
, this, variable
); }
2717 // Add a function declaration to the package.
2719 add_function_declaration(const std::string
& name
, Function_type
* type
,
2721 { return this->bindings_
->add_function_declaration(name
, this, type
, loc
); }
2723 // Determine types of constants.
2728 // The package path for type reflection data.
2729 std::string pkgpath_
;
2730 // The package path for symbol names.
2731 std::string pkgpath_symbol_
;
2732 // The name that this package uses in the package clause. This may
2733 // be the empty string if it is not yet known.
2734 std::string package_name_
;
2735 // The names in this package.
2736 Bindings
* bindings_
;
2737 // The priority of this package. A package has a priority higher
2738 // than the priority of all of the packages that it imports. This
2739 // is used to run init functions in the right order.
2741 // The location of the import statement.
2743 // True if some name from this package was used. This is mutable
2744 // because we can use a package even if we have a const pointer to
2747 // True if this package was imported in the current file.
2749 // True if this package was imported with a name of "_".
2750 bool uses_sink_alias_
;
2753 // Return codes for the traversal functions. This is not an enum
2754 // because we want to be able to declare traversal functions in other
2755 // header files without including this one.
2757 // Continue traversal as usual.
2758 const int TRAVERSE_CONTINUE
= -1;
2761 const int TRAVERSE_EXIT
= 0;
2763 // Continue traversal, but skip components of the current object.
2764 // E.g., if this is returned by Traverse::statement, we do not
2765 // traverse the expressions in the statement even if
2766 // traverse_expressions is set in the traverse_mask.
2767 const int TRAVERSE_SKIP_COMPONENTS
= 1;
2769 // This class is used when traversing the parse tree. The caller uses
2770 // a subclass which overrides functions as desired.
2775 // These bitmasks say what to traverse.
2776 static const unsigned int traverse_variables
= 0x1;
2777 static const unsigned int traverse_constants
= 0x2;
2778 static const unsigned int traverse_functions
= 0x4;
2779 static const unsigned int traverse_blocks
= 0x8;
2780 static const unsigned int traverse_statements
= 0x10;
2781 static const unsigned int traverse_expressions
= 0x20;
2782 static const unsigned int traverse_types
= 0x40;
2784 Traverse(unsigned int traverse_mask
)
2785 : traverse_mask_(traverse_mask
), types_seen_(NULL
), expressions_seen_(NULL
)
2788 virtual ~Traverse();
2790 // The bitmask of what to traverse.
2792 traverse_mask() const
2793 { return this->traverse_mask_
; }
2795 // Record that we are going to traverse a type. This returns true
2796 // if the type has already been seen in this traversal. This is
2797 // required because types, unlike expressions, can form a circular
2800 remember_type(const Type
*);
2802 // Record that we are going to see an expression. This returns true
2803 // if the expression has already been seen in this traversal. This
2804 // is only needed for cases where multiple expressions can point to
2807 remember_expression(const Expression
*);
2809 // These functions return one of the TRAVERSE codes defined above.
2811 // If traverse_variables is set in the mask, this is called for
2812 // every variable in the tree.
2814 variable(Named_object
*);
2816 // If traverse_constants is set in the mask, this is called for
2817 // every named constant in the tree. The bool parameter is true for
2818 // a global constant.
2820 constant(Named_object
*, bool);
2822 // If traverse_functions is set in the mask, this is called for
2823 // every function in the tree.
2825 function(Named_object
*);
2827 // If traverse_blocks is set in the mask, this is called for every
2828 // block in the tree.
2832 // If traverse_statements is set in the mask, this is called for
2833 // every statement in the tree.
2835 statement(Block
*, size_t* index
, Statement
*);
2837 // If traverse_expressions is set in the mask, this is called for
2838 // every expression in the tree.
2840 expression(Expression
**);
2842 // If traverse_types is set in the mask, this is called for every
2843 // type in the tree.
2848 // A hash table for types we have seen during this traversal. Note
2849 // that this uses the default hash functions for pointers rather
2850 // than Type_hash_identical and Type_identical. This is because for
2851 // traversal we care about seeing a specific type structure. If
2852 // there are two separate instances of identical types, we want to
2854 typedef Unordered_set(const Type
*) Types_seen
;
2856 typedef Unordered_set(const Expression
*) Expressions_seen
;
2858 // Bitmask of what sort of objects to traverse.
2859 unsigned int traverse_mask_
;
2860 // Types which have been seen in this traversal.
2861 Types_seen
* types_seen_
;
2862 // Expressions which have been seen in this traversal.
2863 Expressions_seen
* expressions_seen_
;
2866 // A class which makes it easier to insert new statements before the
2867 // current statement during a traversal.
2869 class Statement_inserter
2872 // Empty constructor.
2873 Statement_inserter()
2874 : block_(NULL
), pindex_(NULL
), gogo_(NULL
), var_(NULL
)
2877 // Constructor for a statement in a block.
2878 Statement_inserter(Block
* block
, size_t *pindex
)
2879 : block_(block
), pindex_(pindex
), gogo_(NULL
), var_(NULL
)
2882 // Constructor for a global variable.
2883 Statement_inserter(Gogo
* gogo
, Variable
* var
)
2884 : block_(NULL
), pindex_(NULL
), gogo_(gogo
), var_(var
)
2885 { go_assert(var
->is_global()); }
2887 // We use the default copy constructor and assignment operator.
2889 // Insert S before the statement we are traversing, or before the
2890 // initialization expression of a global variable.
2892 insert(Statement
* s
);
2895 // The block that the statement is in.
2897 // The index of the statement that we are traversing.
2899 // The IR, needed when looking at an initializer expression for a
2902 // The global variable, when looking at an initializer expression.
2906 // When translating the gogo IR into the backend data structure, this
2907 // is the context we pass down the blocks and statements.
2909 class Translate_context
2912 Translate_context(Gogo
* gogo
, Named_object
* function
, Block
* block
,
2914 : gogo_(gogo
), backend_(gogo
->backend()), function_(function
),
2915 block_(block
), bblock_(bblock
), is_const_(false)
2922 { return this->gogo_
; }
2926 { return this->backend_
; }
2930 { return this->function_
; }
2934 { return this->block_
; }
2938 { return this->bblock_
; }
2942 { return this->is_const_
; }
2944 // Make a constant context.
2947 { this->is_const_
= true; }
2950 // The IR for the entire compilation unit.
2952 // The generator for the backend data structures.
2954 // The function we are currently translating. NULL if not in a
2955 // function, e.g., the initializer of a global variable.
2956 Named_object
* function_
;
2957 // The block we are currently translating. NULL if not in a
2960 // The backend representation of the current block. NULL if block_
2963 // Whether this is being evaluated in a constant context. This is
2964 // used for type descriptor initializers.
2968 // Runtime error codes. These must match the values in
2969 // libgo/runtime/go-runtime-error.c.
2971 // Slice index out of bounds: negative or larger than the length of
2973 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
= 0;
2975 // Array index out of bounds.
2976 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
= 1;
2978 // String index out of bounds.
2979 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
= 2;
2981 // Slice slice out of bounds: negative or larger than the length of
2982 // the slice or high bound less than low bound.
2983 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
= 3;
2985 // Array slice out of bounds.
2986 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
= 4;
2988 // String slice out of bounds.
2989 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
= 5;
2991 // Dereference of nil pointer. This is used when there is a
2992 // dereference of a pointer to a very large struct or array, to ensure
2993 // that a gigantic array is not used a proxy to access random memory
2995 static const int RUNTIME_ERROR_NIL_DEREFERENCE
= 6;
2997 // Slice length or capacity out of bounds in make: negative or
2998 // overflow or length greater than capacity.
2999 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
= 7;
3001 // Map capacity out of bounds in make: negative or overflow.
3002 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
= 8;
3004 // Channel capacity out of bounds in make: negative or overflow.
3005 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
= 9;
3007 // Division by zero.
3008 static const int RUNTIME_ERROR_DIVISION_BY_ZERO
= 10;
3010 // This is used by some of the langhooks.
3011 extern Gogo
* go_get_gogo();
3013 // Whether we have seen any errors. FIXME: Replace with a backend
3015 extern bool saw_errors();
3017 #endif // !defined(GO_GOGO_H)