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 the priority to use for the package we are compiling.
221 // This is two more than the largest priority of any package we
224 package_priority() const;
226 // Import a package. FILENAME is the file name argument, LOCAL_NAME
227 // is the local name to give to the package. If LOCAL_NAME is empty
228 // the declarations are added to the global scope.
230 import_package(const std::string
& filename
, const std::string
& local_name
,
231 bool is_local_name_exported
, Location
);
233 // Whether we are the global binding level.
235 in_global_scope() const;
237 // Look up a name in the current binding contours.
239 lookup(const std::string
&, Named_object
** pfunction
) const;
241 // Look up a name in the current block.
243 lookup_in_block(const std::string
&) const;
245 // Look up a name in the global namespace--the universal scope.
247 lookup_global(const char*) const;
249 // Add a new imported package. REAL_NAME is the real name of the
250 // package. ALIAS is the alias of the package; this may be the same
251 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
252 // this package should be added to the global namespace; this is
253 // true if the alias is ".". LOCATION is the location of the import
254 // statement. This returns the new package, or NULL on error.
256 add_imported_package(const std::string
& real_name
, const std::string
& alias
,
257 bool is_alias_exported
,
258 const std::string
& pkgpath
,
260 bool* padd_to_globals
);
262 // Register a package. This package may or may not be imported.
263 // This returns the Package structure for the package, creating if
266 register_package(const std::string
& pkgpath
, Location
);
268 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
269 // method function should be added to the type of its receiver.
271 start_function(const std::string
& name
, Function_type
* type
,
272 bool add_method_to_type
, Location
);
274 // Finish compiling a function.
276 finish_function(Location
);
278 // Return the current function.
280 current_function() const;
282 // Return the current block.
286 // Start a new block. This is not initially associated with a
289 start_block(Location
);
291 // Finish the current block and return it.
293 finish_block(Location
);
295 // Declare an erroneous name. This is used to avoid knock-on errors
296 // after a parsing error.
298 add_erroneous_name(const std::string
& name
);
300 // Declare an unknown name. This is used while parsing. The name
301 // must be resolved by the end of the parse. Unknown names are
302 // always added at the package level.
304 add_unknown_name(const std::string
& name
, Location
);
306 // Declare a function.
308 declare_function(const std::string
&, Function_type
*, Location
);
310 // Declare a function at the package level. This is used for
311 // functions generated for a type.
313 declare_package_function(const std::string
&, Function_type
*, Location
);
317 add_label_definition(const std::string
&, Location
);
319 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
320 // report errors for a goto from the current location to the label
323 add_label_reference(const std::string
&, Location
,
324 bool issue_goto_errors
);
326 // Return a snapshot of the current binding state.
328 bindings_snapshot(Location
);
330 // Add a statement to the current block.
332 add_statement(Statement
*);
334 // Add a block to the current block.
336 add_block(Block
*, Location
);
340 add_constant(const Typed_identifier
&, Expression
*, int iota_value
);
344 add_type(const std::string
&, Type
*, Location
);
346 // Add a named type. This is used for builtin types, and to add an
347 // imported type to the global scope.
349 add_named_type(Named_type
*);
353 declare_type(const std::string
&, Location
);
355 // Declare a type at the package level. This is used when the
356 // parser sees an unknown name where a type name is required.
358 declare_package_type(const std::string
&, Location
);
360 // Define a type which was already declared.
362 define_type(Named_object
*, Named_type
*);
366 add_variable(const std::string
&, Variable
*);
368 // Add a sink--a reference to the blank identifier _.
372 // Add a type which needs to be verified. This is used for sink
373 // types, just to give appropriate error messages.
375 add_type_to_verify(Type
* type
);
377 // Add a named object to the current namespace. This is used for
378 // import . "package".
380 add_named_object(Named_object
*);
382 // Add an identifier to the list of names seen in the file block.
384 add_file_block_name(const std::string
& name
, Location location
)
385 { this->file_block_names_
[name
] = location
; }
387 // Mark all local variables in current bindings as used. This is
388 // used when there is a parse error to avoid useless errors.
392 // Return a name to use for an error case. This should only be used
393 // after reporting an error, and is used to avoid useless knockon
398 // Return whether the name indicates an error.
400 is_erroneous_name(const std::string
&);
402 // Return a name to use for a thunk function. A thunk function is
403 // one we create during the compilation, for a go statement or a
404 // defer statement or a method expression.
408 // Return whether an object is a thunk.
410 is_thunk(const Named_object
*);
412 // Note that we've seen an interface type. This is used to build
413 // all required interface method tables.
415 record_interface_type(Interface_type
*);
417 // Note that we need an initialization function.
420 { this->need_init_fn_
= true; }
422 // Clear out all names in file scope. This is called when we start
423 // parsing a new file.
427 // Record that VAR1 must be initialized after VAR2. This is used
428 // when VAR2 does not appear in VAR1's INIT or PREINIT.
430 record_var_depends_on(Variable
* var1
, Named_object
* var2
)
432 go_assert(this->var_deps_
.find(var1
) == this->var_deps_
.end());
433 this->var_deps_
[var1
] = var2
;
436 // Return the variable that VAR depends on, or NULL if none.
438 var_depends_on(Variable
* var
) const
440 Var_deps::const_iterator p
= this->var_deps_
.find(var
);
441 return p
!= this->var_deps_
.end() ? p
->second
: NULL
;
444 // Queue up a type-specific function to be written out. This is
445 // used when a type-specific function is needed when not at the top
448 queue_specific_type_function(Type
* type
, Named_type
* name
,
449 const std::string
& hash_name
,
450 Function_type
* hash_fntype
,
451 const std::string
& equal_name
,
452 Function_type
* equal_fntype
);
454 // Write out queued specific type functions.
456 write_specific_type_functions();
458 // Whether we are done writing out specific type functions.
460 specific_type_functions_are_written() const
461 { return this->specific_type_functions_are_written_
; }
463 // Traverse the tree. See the Traverse class.
467 // Define the predeclared global names.
469 define_global_names();
471 // Verify and complete all types.
475 // Lower the parse tree.
479 // Lower all the statements in a block.
481 lower_block(Named_object
* function
, Block
*);
483 // Lower an expression.
485 lower_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
489 lower_constant(Named_object
*);
491 // Flatten all the statements in a block.
493 flatten_block(Named_object
* function
, Block
*);
495 // Flatten an expression.
497 flatten_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
499 // Create all necessary function descriptors.
501 create_function_descriptors();
503 // Finalize the method lists and build stub methods for named types.
507 // Work out the types to use for unspecified variables and
512 // Type check the program.
516 // Check the types in a single block. This is used for complicated
519 check_types_in_block(Block
*);
521 // Check for return statements.
523 check_return_statements();
529 // Add an import control function for an imported package to the
532 add_import_init_fn(const std::string
& package_name
,
533 const std::string
& init_name
, int prio
);
535 // Turn short-cut operators (&&, ||) into explicit if statements.
539 // Use temporary variables to force order of evaluation.
543 // Flatten parse tree.
547 // Build thunks for functions which call recover.
549 build_recover_thunks();
551 // Simplify statements which might use thunks: go and defer
554 simplify_thunk_statements();
556 // Dump AST if -fgo-dump-ast is set
558 dump_ast(const char* basename
);
560 // Convert named types to the backend representation.
562 convert_named_types();
564 // Convert named types in a list of bindings.
566 convert_named_types_in_bindings(Bindings
*);
568 // True if named types have been converted to the backend
571 named_types_are_converted() const
572 { return this->named_types_are_converted_
; }
574 // Write out the global values.
578 // Build a call to the runtime error function.
580 runtime_error(int code
, Location
);
582 // Build required interface method tables.
584 build_interface_method_tables();
586 // Return an expression which allocates memory to hold values of type TYPE.
588 allocate_memory(Type
*type
, Location
);
590 // Get the name of the magic initialization function.
595 // During parsing, we keep a stack of functions. Each function on
596 // the stack is one that we are currently parsing. For each
597 // function, we keep track of the current stack of blocks.
601 Named_object
* function
;
602 // The stack of active blocks in the function.
603 std::vector
<Block
*> blocks
;
606 // The stack of functions.
607 typedef std::vector
<Open_function
> Open_functions
;
609 // Set up the built-in unsafe package.
611 import_unsafe(const std::string
&, bool is_exported
, Location
);
613 // Return the current binding contour.
618 current_bindings() const;
620 // Get the decl for the magic initialization function.
622 initialization_function_decl();
624 // Create the magic initialization function.
626 create_initialization_function(Named_object
* fndecl
, Bstatement
* code_stmt
);
628 // Initialize imported packages.
630 init_imports(std::vector
<Bstatement
*>&);
632 // Register variables with the garbage collector.
634 register_gc_vars(const std::vector
<Named_object
*>&,
635 std::vector
<Bstatement
*>&);
637 // Type used to map import names to packages.
638 typedef std::map
<std::string
, Package
*> Imports
;
640 // Type used to map package names to packages.
641 typedef std::map
<std::string
, Package
*> Packages
;
643 // Type used to map variables to the function calls that set them.
644 // This is used for initialization dependency analysis.
645 typedef std::map
<Variable
*, Named_object
*> Var_deps
;
647 // Type used to map identifiers in the file block to the location
648 // where they were defined.
649 typedef Unordered_map(std::string
, Location
) File_block_names
;
651 // Type used to queue writing a type specific function.
652 struct Specific_type_function
656 std::string hash_name
;
657 Function_type
* hash_fntype
;
658 std::string equal_name
;
659 Function_type
* equal_fntype
;
661 Specific_type_function(Type
* atype
, Named_type
* aname
,
662 const std::string
& ahash_name
,
663 Function_type
* ahash_fntype
,
664 const std::string
& aequal_name
,
665 Function_type
* aequal_fntype
)
666 : type(atype
), name(aname
), hash_name(ahash_name
),
667 hash_fntype(ahash_fntype
), equal_name(aequal_name
),
668 equal_fntype(aequal_fntype
)
672 // The backend generator.
674 // The object used to keep track of file names and line numbers.
676 // The package we are compiling.
678 // The list of currently open functions during parsing.
679 Open_functions functions_
;
680 // The global binding contour. This includes the builtin functions
681 // and the package we are compiling.
683 // The list of names we have seen in the file block.
684 File_block_names file_block_names_
;
685 // Mapping from import file names to packages.
687 // Whether the magic unsafe package was imported.
688 bool imported_unsafe_
;
689 // Mapping from package names we have seen to packages. This does
690 // not include the package we are compiling.
692 // The functions named "init", if there are any.
693 std::vector
<Named_object
*> init_functions_
;
694 // A mapping from variables to the function calls that initialize
695 // them, if it is not stored in the variable's init or preinit.
696 // This is used for dependency analysis.
698 // Whether we need a magic initialization function.
700 // The name of the magic initialization function.
701 std::string init_fn_name_
;
702 // A list of import control variables for packages that we import.
703 std::set
<Import_init
> imported_init_fns_
;
704 // The package path used for reflection data.
705 std::string pkgpath_
;
706 // The package path to use for a symbol name.
707 std::string pkgpath_symbol_
;
708 // The prefix to use for symbols, from the -fgo-prefix option.
710 // Whether pkgpath_ has been set.
712 // Whether an explicit package path was set by -fgo-pkgpath.
713 bool pkgpath_from_option_
;
714 // Whether an explicit prefix was set by -fgo-prefix.
715 bool prefix_from_option_
;
716 // The relative import path, from the -fgo-relative-import-path
718 std::string relative_import_path_
;
719 // A list of types to verify.
720 std::vector
<Type
*> verify_types_
;
721 // A list of interface types defined while parsing.
722 std::vector
<Interface_type
*> interface_types_
;
723 // Type specific functions to write out.
724 std::vector
<Specific_type_function
*> specific_type_functions_
;
725 // Whether we are done writing out specific type functions.
726 bool specific_type_functions_are_written_
;
727 // Whether named types have been converted.
728 bool named_types_are_converted_
;
731 // A block of statements.
736 Block(Block
* enclosing
, Location
);
738 // Return the enclosing block.
741 { return this->enclosing_
; }
743 // Return the bindings of the block.
746 { return this->bindings_
; }
750 { return this->bindings_
; }
752 // Look at the block's statements.
753 const std::vector
<Statement
*>*
755 { return &this->statements_
; }
757 // Return the start location. This is normally the location of the
758 // left curly brace which starts the block.
760 start_location() const
761 { return this->start_location_
; }
763 // Return the end location. This is normally the location of the
764 // right curly brace which ends the block.
767 { return this->end_location_
; }
769 // Add a statement to the block.
771 add_statement(Statement
*);
773 // Add a statement to the front of the block.
775 add_statement_at_front(Statement
*);
777 // Replace a statement in a block.
779 replace_statement(size_t index
, Statement
*);
781 // Add a Statement before statement number INDEX.
783 insert_statement_before(size_t index
, Statement
*);
785 // Add a Statement after statement number INDEX.
787 insert_statement_after(size_t index
, Statement
*);
789 // Set the end location of the block.
791 set_end_location(Location location
)
792 { this->end_location_
= location
; }
794 // Traverse the tree.
798 // Set final types for unspecified variables and constants.
802 // Return true if execution of this block may fall through to the
805 may_fall_through() const;
807 // Convert the block to the backend representation.
809 get_backend(Translate_context
*);
811 // Iterate over statements.
813 typedef std::vector
<Statement
*>::iterator iterator
;
817 { return this->statements_
.begin(); }
821 { return this->statements_
.end(); }
826 // Statements in the block.
827 std::vector
<Statement
*> statements_
;
830 // Location of start of block.
831 Location start_location_
;
832 // Location of end of block.
833 Location end_location_
;
841 Function(Function_type
* type
, Function
*, Block
*, Location
);
843 // Return the function's type.
846 { return this->type_
; }
848 // Return the enclosing function if there is one.
851 { return this->enclosing_
; }
853 // Set the enclosing function. This is used when building thunks
854 // for functions which call recover.
856 set_enclosing(Function
* enclosing
)
858 go_assert(this->enclosing_
== NULL
);
859 this->enclosing_
= enclosing
;
862 // The result variables.
863 typedef std::vector
<Named_object
*> Results
;
865 // Create the result variables in the outer block.
867 create_result_variables(Gogo
*);
869 // Update the named result variables when cloning a function which
872 update_result_variables();
874 // Return the result variables.
877 { return this->results_
; }
881 { return this->is_sink_
; }
885 { this->is_sink_
= true; }
887 // Whether the result variables have names.
889 results_are_named() const
890 { return this->results_are_named_
; }
892 // Whether this method should not be included in the type
897 go_assert(this->is_method());
898 return this->nointerface_
;
901 // Record that this method should not be included in the type
906 go_assert(this->is_method());
907 this->nointerface_
= true;
910 // Record that this function is a stub method created for an unnamed
913 set_is_unnamed_type_stub_method()
915 go_assert(this->is_method());
916 this->is_unnamed_type_stub_method_
= true;
919 // Add a new field to the closure variable.
921 add_closure_field(Named_object
* var
, Location loc
)
922 { this->closure_fields_
.push_back(std::make_pair(var
, loc
)); }
924 // Whether this function needs a closure.
926 needs_closure() const
927 { return !this->closure_fields_
.empty(); }
929 // Return the closure variable, creating it if necessary. This is
930 // passed to the function as a static chain parameter.
934 // Set the closure variable. This is used when building thunks for
935 // functions which call recover.
937 set_closure_var(Named_object
* v
)
939 go_assert(this->closure_var_
== NULL
);
940 this->closure_var_
= v
;
943 // Return the variable for a reference to field INDEX in the closure
946 enclosing_var(unsigned int index
)
948 go_assert(index
< this->closure_fields_
.size());
949 return closure_fields_
[index
].first
;
952 // Set the type of the closure variable if there is one.
956 // Get the block of statements associated with the function.
959 { return this->block_
; }
961 // Get the location of the start of the function.
964 { return this->location_
; }
966 // Return whether this function is actually a method.
970 // Add a label definition to the function.
972 add_label_definition(Gogo
*, const std::string
& label_name
, Location
);
974 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
975 // if we should report errors for a goto from the current location
976 // to the label location.
978 add_label_reference(Gogo
*, const std::string
& label_name
,
979 Location
, bool issue_goto_errors
);
981 // Warn about labels that are defined but not used.
983 check_labels() const;
985 // Note that a new local type has been added. Return its index.
987 new_local_type_index()
988 { return this->local_type_count_
++; }
990 // Whether this function calls the predeclared recover function.
992 calls_recover() const
993 { return this->calls_recover_
; }
995 // Record that this function calls the predeclared recover function.
996 // This is set during the lowering pass.
999 { this->calls_recover_
= true; }
1001 // Whether this is a recover thunk function.
1003 is_recover_thunk() const
1004 { return this->is_recover_thunk_
; }
1006 // Record that this is a thunk built for a function which calls
1009 set_is_recover_thunk()
1010 { this->is_recover_thunk_
= true; }
1012 // Whether this function already has a recover thunk.
1014 has_recover_thunk() const
1015 { return this->has_recover_thunk_
; }
1017 // Record that this function already has a recover thunk.
1019 set_has_recover_thunk()
1020 { this->has_recover_thunk_
= true; }
1022 // Mark the function as going into a unique section.
1024 set_in_unique_section()
1025 { this->in_unique_section_
= true; }
1027 // Swap with another function. Used only for the thunk which calls
1030 swap_for_recover(Function
*);
1032 // Traverse the tree.
1034 traverse(Traverse
*);
1036 // Determine types in the function.
1040 // Return an expression for the function descriptor, given the named
1041 // object for this function. This may only be called for functions
1042 // without a closure. This will be an immutable struct with one
1043 // field that points to the function's code.
1045 descriptor(Gogo
*, Named_object
*);
1047 // Set the descriptor for this function. This is used when a
1048 // function declaration is followed by a function definition.
1050 set_descriptor(Expression
* descriptor
)
1052 go_assert(this->descriptor_
== NULL
);
1053 this->descriptor_
= descriptor
;
1056 // Return the backend representation.
1058 get_or_make_decl(Gogo
*, Named_object
*);
1060 // Return the function's decl after it has been built.
1064 // Set the function decl to hold a backend representation of the function
1067 build(Gogo
*, Named_object
*);
1069 // Get the statement that assigns values to this function's result struct.
1071 return_value(Gogo
*, Named_object
*, Location
) const;
1073 // Get a tree for the variable holding the defer stack.
1075 defer_stack(Location
);
1077 // Export the function.
1079 export_func(Export
*, const std::string
& name
) const;
1081 // Export a function with a type.
1083 export_func_with_type(Export
*, const std::string
& name
,
1084 const Function_type
*);
1086 // Import a function.
1088 import_func(Import
*, std::string
* pname
, Typed_identifier
** receiver
,
1089 Typed_identifier_list
** pparameters
,
1090 Typed_identifier_list
** presults
, bool* is_varargs
);
1093 // Type for mapping from label names to Label objects.
1094 typedef Unordered_map(std::string
, Label
*) Labels
;
1097 build_defer_wrapper(Gogo
*, Named_object
*, Bstatement
**, Bstatement
**);
1099 typedef std::vector
<std::pair
<Named_object
*,
1100 Location
> > Closure_fields
;
1102 // The function's type.
1103 Function_type
* type_
;
1104 // The enclosing function. This is NULL when there isn't one, which
1105 // is the normal case.
1106 Function
* enclosing_
;
1107 // The result variables, if any.
1109 // If there is a closure, this is the list of variables which appear
1110 // in the closure. This is created by the parser, and then resolved
1111 // to a real type when we lower parse trees.
1112 Closure_fields closure_fields_
;
1113 // The closure variable, passed as a parameter using the static
1114 // chain parameter. Normally NULL.
1115 Named_object
* closure_var_
;
1116 // The outer block of statements in the function.
1118 // The source location of the start of the function.
1120 // Labels defined or referenced in the function.
1122 // The number of local types defined in this function.
1123 unsigned int local_type_count_
;
1124 // The function descriptor, if any.
1125 Expression
* descriptor_
;
1126 // The function decl.
1128 // The defer stack variable. A pointer to this variable is used to
1129 // distinguish the defer stack for one function from another. This
1130 // is NULL unless we actually need a defer stack.
1131 Temporary_statement
* defer_stack_
;
1132 // True if this function is sink-named. No code is generated.
1134 // True if the result variables are named.
1135 bool results_are_named_
: 1;
1136 // True if this method should not be included in the type descriptor.
1137 bool nointerface_
: 1;
1138 // True if this function is a stub method created for an unnamed
1140 bool is_unnamed_type_stub_method_
: 1;
1141 // True if this function calls the predeclared recover function.
1142 bool calls_recover_
: 1;
1143 // True if this a thunk built for a function which calls recover.
1144 bool is_recover_thunk_
: 1;
1145 // True if this function already has a recover thunk.
1146 bool has_recover_thunk_
: 1;
1147 // True if this function should be put in a unique section. This is
1148 // turned on for field tracking.
1149 bool in_unique_section_
: 1;
1152 // A snapshot of the current binding state.
1154 class Bindings_snapshot
1157 Bindings_snapshot(const Block
*, Location
);
1159 // Report any errors appropriate for a goto from the current binding
1160 // state of B to this one.
1162 check_goto_from(const Block
* b
, Location
);
1164 // Report any errors appropriate for a goto from this binding state
1165 // to the current state of B.
1167 check_goto_to(const Block
* b
);
1171 check_goto_block(Location
, const Block
*, const Block
*, size_t*);
1174 check_goto_defs(Location
, const Block
*, size_t, size_t);
1176 // The current block.
1177 const Block
* block_
;
1178 // The number of names currently defined in each open block.
1179 // Element 0 is this->block_, element 1 is
1180 // this->block_->enclosing(), etc.
1181 std::vector
<size_t> counts_
;
1182 // The location where this snapshot was taken.
1186 // A function declaration.
1188 class Function_declaration
1191 Function_declaration(Function_type
* fntype
, Location location
)
1192 : fntype_(fntype
), location_(location
), asm_name_(), descriptor_(NULL
),
1198 { return this->fntype_
; }
1202 { return this->location_
; }
1206 { return this->asm_name_
; }
1208 // Set the assembler name.
1210 set_asm_name(const std::string
& asm_name
)
1211 { this->asm_name_
= asm_name
; }
1213 // Return an expression for the function descriptor, given the named
1214 // object for this function. This may only be called for functions
1215 // without a closure. This will be an immutable struct with one
1216 // field that points to the function's code.
1218 descriptor(Gogo
*, Named_object
*);
1220 // Return true if we have created a descriptor for this declaration.
1222 has_descriptor() const
1223 { return this->descriptor_
!= NULL
; }
1225 // Return a backend representation.
1227 get_or_make_decl(Gogo
*, Named_object
*);
1229 // If there is a descriptor, build it into the backend
1232 build_backend_descriptor(Gogo
*);
1234 // Export a function declaration.
1236 export_func(Export
* exp
, const std::string
& name
) const
1237 { Function::export_func_with_type(exp
, name
, this->fntype_
); }
1240 // The type of the function.
1241 Function_type
* fntype_
;
1242 // The location of the declaration.
1244 // The assembler name: this is the name to use in references to the
1245 // function. This is normally empty.
1246 std::string asm_name_
;
1247 // The function descriptor, if any.
1248 Expression
* descriptor_
;
1249 // The function decl if needed.
1258 Variable(Type
*, Expression
*, bool is_global
, bool is_parameter
,
1259 bool is_receiver
, Location
);
1261 // Get the type of the variable.
1268 // Return whether the type is defined yet.
1272 // Get the initial value.
1275 { return this->init_
; }
1277 // Return whether there are any preinit statements.
1279 has_pre_init() const
1280 { return this->preinit_
!= NULL
; }
1282 // Return the preinit statements if any.
1285 { return this->preinit_
; }
1287 // Return whether this is a global variable.
1290 { return this->is_global_
; }
1292 // Return whether this is a function parameter.
1294 is_parameter() const
1295 { return this->is_parameter_
; }
1297 // Return whether this is the receiver parameter of a method.
1300 { return this->is_receiver_
; }
1302 // Change this parameter to be a receiver. This is used when
1303 // creating the thunks created for functions which call recover.
1307 go_assert(this->is_parameter_
);
1308 this->is_receiver_
= true;
1311 // Change this parameter to not be a receiver. This is used when
1312 // creating the thunks created for functions which call recover.
1314 set_is_not_receiver()
1316 go_assert(this->is_parameter_
);
1317 this->is_receiver_
= false;
1320 // Return whether this is the varargs parameter of a function.
1322 is_varargs_parameter() const
1323 { return this->is_varargs_parameter_
; }
1325 // Whether this variable's address is taken.
1327 is_address_taken() const
1328 { return this->is_address_taken_
; }
1330 // Whether this variable should live in the heap.
1333 { return this->is_address_taken_
&& !this->is_global_
; }
1335 // Note that something takes the address of this variable.
1338 { this->is_address_taken_
= true; }
1340 // Return whether the address is taken but does not escape.
1342 is_non_escaping_address_taken() const
1343 { return this->is_non_escaping_address_taken_
; }
1345 // Note that something takes the address of this variable such that
1346 // the address does not escape the function.
1348 set_non_escaping_address_taken()
1349 { this->is_non_escaping_address_taken_
= true; }
1351 // Get the source location of the variable's declaration.
1354 { return this->location_
; }
1356 // Record that this is the varargs parameter of a function.
1358 set_is_varargs_parameter()
1360 go_assert(this->is_parameter_
);
1361 this->is_varargs_parameter_
= true;
1364 // Return whether the variable has been used.
1367 { return this->is_used_
; }
1369 // Mark that the variable has been used.
1372 { this->is_used_
= true; }
1374 // Clear the initial value; used for error handling.
1377 { this->init_
= NULL
; }
1379 // Set the initial value; used for converting shortcuts.
1381 set_init(Expression
* init
)
1382 { this->init_
= init
; }
1384 // Get the preinit block, a block of statements to be run before the
1385 // initialization expression.
1387 preinit_block(Gogo
*);
1389 // Add a statement to be run before the initialization expression.
1390 // This is only used for global variables.
1392 add_preinit_statement(Gogo
*, Statement
*);
1394 // Lower the initialization expression after parsing is complete.
1396 lower_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1398 // Flatten the initialization expression after ordering evaluations.
1400 flatten_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1402 // A special case: the init value is used only to determine the
1403 // type. This is used if the variable is defined using := with the
1404 // comma-ok form of a map index or a receive expression. The init
1405 // value is actually the map index expression or receive expression.
1406 // We use this because we may not know the right type at parse time.
1408 set_type_from_init_tuple()
1409 { this->type_from_init_tuple_
= true; }
1411 // Another special case: the init value is used only to determine
1412 // the type. This is used if the variable is defined using := with
1413 // a range clause. The init value is the range expression. The
1414 // type of the variable is the index type of the range expression
1415 // (i.e., the first value returned by a range).
1417 set_type_from_range_index()
1418 { this->type_from_range_index_
= true; }
1420 // Another special case: like set_type_from_range_index, but the
1421 // type is the value type of the range expression (i.e., the second
1422 // value returned by a range).
1424 set_type_from_range_value()
1425 { this->type_from_range_value_
= true; }
1427 // Another special case: the init value is used only to determine
1428 // the type. This is used if the variable is defined using := with
1429 // a case in a select statement. The init value is the channel.
1430 // The type of the variable is the channel's element type.
1432 set_type_from_chan_element()
1433 { this->type_from_chan_element_
= true; }
1435 // After we lower the select statement, we once again set the type
1436 // from the initialization expression.
1438 clear_type_from_chan_element()
1440 go_assert(this->type_from_chan_element_
);
1441 this->type_from_chan_element_
= false;
1444 // Note that this variable was created for a type switch clause.
1446 set_is_type_switch_var()
1447 { this->is_type_switch_var_
= true; }
1449 // Mark the variable as going into a unique section.
1451 set_in_unique_section()
1453 go_assert(this->is_global_
);
1454 this->in_unique_section_
= true;
1457 // Traverse the initializer expression.
1459 traverse_expression(Traverse
*, unsigned int traverse_mask
);
1461 // Determine the type of the variable if necessary.
1465 // Get the backend representation of the variable.
1467 get_backend_variable(Gogo
*, Named_object
*, const Package
*,
1468 const std::string
&);
1470 // Get the initial value of the variable. This may only
1471 // be called if has_pre_init() returns false.
1473 get_init(Gogo
*, Named_object
* function
);
1475 // Return a series of statements which sets the value of the
1476 // variable in DECL. This should only be called is has_pre_init()
1477 // returns true. DECL may be NULL for a sink variable.
1479 get_init_block(Gogo
*, Named_object
* function
, Bvariable
* decl
);
1481 // Export the variable.
1483 export_var(Export
*, const std::string
& name
) const;
1485 // Import a variable.
1487 import_var(Import
*, std::string
* pname
, Type
** ptype
);
1490 // The type of a tuple.
1492 type_from_tuple(Expression
*, bool) const;
1494 // The type of a range.
1496 type_from_range(Expression
*, bool, bool) const;
1498 // The element type of a channel.
1500 type_from_chan_element(Expression
*, bool) const;
1502 // The variable's type. This may be NULL if the type is set from
1505 // The initial value. This may be NULL if the variable should be
1506 // initialized to the default value for the type.
1508 // Statements to run before the init statement.
1510 // Location of variable definition.
1512 // Backend representation.
1513 Bvariable
* backend_
;
1514 // Whether this is a global variable.
1515 bool is_global_
: 1;
1516 // Whether this is a function parameter.
1517 bool is_parameter_
: 1;
1518 // Whether this is the receiver parameter of a method.
1519 bool is_receiver_
: 1;
1520 // Whether this is the varargs parameter of a function.
1521 bool is_varargs_parameter_
: 1;
1522 // Whether this variable is ever referenced.
1524 // Whether something takes the address of this variable. For a
1525 // local variable this implies that the variable has to be on the
1527 bool is_address_taken_
: 1;
1528 // Whether something takes the address of this variable such that
1529 // the address does not escape the function.
1530 bool is_non_escaping_address_taken_
: 1;
1531 // True if we have seen this variable in a traversal.
1533 // True if we have lowered the initialization expression.
1534 bool init_is_lowered_
: 1;
1535 // True if we have flattened the initialization expression.
1536 bool init_is_flattened_
: 1;
1537 // True if init is a tuple used to set the type.
1538 bool type_from_init_tuple_
: 1;
1539 // True if init is a range clause and the type is the index type.
1540 bool type_from_range_index_
: 1;
1541 // True if init is a range clause and the type is the value type.
1542 bool type_from_range_value_
: 1;
1543 // True if init is a channel and the type is the channel's element type.
1544 bool type_from_chan_element_
: 1;
1545 // True if this is a variable created for a type switch case.
1546 bool is_type_switch_var_
: 1;
1547 // True if we have determined types.
1548 bool determined_type_
: 1;
1549 // True if this variable should be put in a unique section. This is
1550 // used for field tracking.
1551 bool in_unique_section_
: 1;
1554 // A variable which is really the name for a function return value, or
1557 class Result_variable
1560 Result_variable(Type
* type
, Function
* function
, int index
,
1562 : type_(type
), function_(function
), index_(index
), location_(location
),
1563 backend_(NULL
), is_address_taken_(false),
1564 is_non_escaping_address_taken_(false)
1567 // Get the type of the result variable.
1570 { return this->type_
; }
1572 // Get the function that this is associated with.
1575 { return this->function_
; }
1577 // Index in the list of function results.
1580 { return this->index_
; }
1582 // The location of the variable definition.
1585 { return this->location_
; }
1587 // Whether this variable's address is taken.
1589 is_address_taken() const
1590 { return this->is_address_taken_
; }
1592 // Note that something takes the address of this variable.
1595 { this->is_address_taken_
= true; }
1597 // Return whether the address is taken but does not escape.
1599 is_non_escaping_address_taken() const
1600 { return this->is_non_escaping_address_taken_
; }
1602 // Note that something takes the address of this variable such that
1603 // the address does not escape the function.
1605 set_non_escaping_address_taken()
1606 { this->is_non_escaping_address_taken_
= true; }
1608 // Whether this variable should live in the heap.
1611 { return this->is_address_taken_
; }
1613 // Set the function. This is used when cloning functions which call
1616 set_function(Function
* function
)
1617 { this->function_
= function
; }
1619 // Get the backend representation of the variable.
1621 get_backend_variable(Gogo
*, Named_object
*, const std::string
&);
1624 // Type of result variable.
1626 // Function with which this is associated.
1627 Function
* function_
;
1628 // Index in list of results.
1630 // Where the result variable is defined.
1632 // Backend representation.
1633 Bvariable
* backend_
;
1634 // Whether something takes the address of this variable.
1635 bool is_address_taken_
;
1636 // Whether something takes the address of this variable such that
1637 // the address does not escape the function.
1638 bool is_non_escaping_address_taken_
;
1641 // The value we keep for a named constant. This lets us hold a type
1642 // and an expression.
1644 class Named_constant
1647 Named_constant(Type
* type
, Expression
* expr
, int iota_value
,
1649 : type_(type
), expr_(expr
), iota_value_(iota_value
), location_(location
),
1650 lowering_(false), is_sink_(false), bconst_(NULL
)
1655 { return this->type_
; }
1659 { return this->expr_
; }
1663 { return this->iota_value_
; }
1667 { return this->location_
; }
1669 // Whether we are lowering.
1672 { return this->lowering_
; }
1674 // Set that we are lowering.
1677 { this->lowering_
= true; }
1679 // We are no longer lowering.
1682 { this->lowering_
= false; }
1686 { return this->is_sink_
; }
1690 { this->is_sink_
= true; }
1692 // Traverse the expression.
1694 traverse_expression(Traverse
*);
1696 // Determine the type of the constant if necessary.
1700 // Indicate that we found and reported an error for this constant.
1704 // Export the constant.
1706 export_const(Export
*, const std::string
& name
) const;
1708 // Import a constant.
1710 import_const(Import
*, std::string
*, Type
**, Expression
**);
1712 // Get the backend representation of the constant value.
1714 get_backend(Gogo
*, Named_object
*);
1717 // The type of the constant.
1719 // The expression for the constant.
1721 // If the predeclared constant iota is used in EXPR_, this is the
1722 // value it will have. We do this because at parse time we don't
1723 // know whether the name "iota" will refer to the predeclared
1724 // constant or to something else. We put in the right value in when
1727 // The location of the definition.
1729 // Whether we are currently lowering this constant.
1731 // Whether this constant is blank named and needs only type checking.
1733 // The backend representation of the constant value.
1734 Bexpression
* bconst_
;
1737 // A type declaration.
1739 class Type_declaration
1742 Type_declaration(Location location
)
1743 : location_(location
), in_function_(NULL
), in_function_index_(0),
1744 methods_(), issued_warning_(false)
1747 // Return the location.
1750 { return this->location_
; }
1752 // Return the function in which this type is declared. This will
1753 // return NULL for a type declared in global scope.
1755 in_function(unsigned int* pindex
)
1757 *pindex
= this->in_function_index_
;
1758 return this->in_function_
;
1761 // Set the function in which this type is declared.
1763 set_in_function(Named_object
* f
, unsigned int index
)
1765 this->in_function_
= f
;
1766 this->in_function_index_
= index
;
1769 // Add a method to this type. This is used when methods are defined
1772 add_method(const std::string
& name
, Function
* function
);
1774 // Add a method declaration to this type.
1776 add_method_declaration(const std::string
& name
, Package
*,
1777 Function_type
* type
, Location location
);
1779 // Return whether any methods were defined.
1781 has_methods() const;
1783 // Return the methods.
1784 const std::vector
<Named_object
*>*
1786 { return &this->methods_
; }
1788 // Define methods when the real type is known.
1790 define_methods(Named_type
*);
1792 // This is called if we are trying to use this type. It returns
1793 // true if we should issue a warning.
1798 // The location of the type declaration.
1800 // If this type is declared in a function, a pointer back to the
1801 // function in which it is defined.
1802 Named_object
* in_function_
;
1803 // The index of this type in IN_FUNCTION_.
1804 unsigned int in_function_index_
;
1805 // Methods defined before the type is defined.
1806 std::vector
<Named_object
*> methods_
;
1807 // True if we have issued a warning about a use of this type
1808 // declaration when it is undefined.
1809 bool issued_warning_
;
1812 // An unknown object. These are created by the parser for forward
1813 // references to names which have not been seen before. In a correct
1814 // program, these will always point to a real definition by the end of
1815 // the parse. Because they point to another Named_object, these may
1816 // only be referenced by Unknown_expression objects.
1821 Unknown_name(Location location
)
1822 : location_(location
), real_named_object_(NULL
)
1825 // Return the location where this name was first seen.
1828 { return this->location_
; }
1830 // Return the real named object that this points to, or NULL if it
1831 // was never resolved.
1833 real_named_object() const
1834 { return this->real_named_object_
; }
1836 // Set the real named object that this points to.
1838 set_real_named_object(Named_object
* no
);
1841 // The location where this name was first seen.
1843 // The real named object when it is known.
1848 // A named object named. This is the result of a declaration. We
1849 // don't use a superclass because they all have to be handled
1857 // An uninitialized Named_object. We should never see this.
1858 NAMED_OBJECT_UNINITIALIZED
,
1859 // An erroneous name. This indicates a parse error, to avoid
1860 // later errors about undefined references.
1861 NAMED_OBJECT_ERRONEOUS
,
1862 // An unknown name. This is used for forward references. In a
1863 // correct program, these will all be resolved by the end of the
1865 NAMED_OBJECT_UNKNOWN
,
1870 // A forward type declaration.
1871 NAMED_OBJECT_TYPE_DECLARATION
,
1874 // A result variable in a function.
1875 NAMED_OBJECT_RESULT_VAR
,
1876 // The blank identifier--the special variable named _.
1880 // A forward func declaration.
1881 NAMED_OBJECT_FUNC_DECLARATION
,
1883 NAMED_OBJECT_PACKAGE
1886 // Return the classification.
1888 classification() const
1889 { return this->classification_
; }
1894 is_erroneous() const
1895 { return this->classification_
== NAMED_OBJECT_ERRONEOUS
; }
1899 { return this->classification_
== NAMED_OBJECT_UNKNOWN
; }
1903 { return this->classification_
== NAMED_OBJECT_CONST
; }
1907 { return this->classification_
== NAMED_OBJECT_TYPE
; }
1910 is_type_declaration() const
1911 { return this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
; }
1915 { return this->classification_
== NAMED_OBJECT_VAR
; }
1918 is_result_variable() const
1919 { return this->classification_
== NAMED_OBJECT_RESULT_VAR
; }
1923 { return this->classification_
== NAMED_OBJECT_SINK
; }
1927 { return this->classification_
== NAMED_OBJECT_FUNC
; }
1930 is_function_declaration() const
1931 { return this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
; }
1935 { return this->classification_
== NAMED_OBJECT_PACKAGE
; }
1939 static Named_object
*
1940 make_erroneous_name(const std::string
& name
)
1941 { return new Named_object(name
, NULL
, NAMED_OBJECT_ERRONEOUS
); }
1943 static Named_object
*
1944 make_unknown_name(const std::string
& name
, Location
);
1946 static Named_object
*
1947 make_constant(const Typed_identifier
&, const Package
*, Expression
*,
1950 static Named_object
*
1951 make_type(const std::string
&, const Package
*, Type
*, Location
);
1953 static Named_object
*
1954 make_type_declaration(const std::string
&, const Package
*, Location
);
1956 static Named_object
*
1957 make_variable(const std::string
&, const Package
*, Variable
*);
1959 static Named_object
*
1960 make_result_variable(const std::string
&, Result_variable
*);
1962 static Named_object
*
1965 static Named_object
*
1966 make_function(const std::string
&, const Package
*, Function
*);
1968 static Named_object
*
1969 make_function_declaration(const std::string
&, const Package
*, Function_type
*,
1972 static Named_object
*
1973 make_package(const std::string
& alias
, Package
* package
);
1980 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
1981 return this->u_
.unknown_value
;
1985 unknown_value() const
1987 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
1988 return this->u_
.unknown_value
;
1994 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
1995 return this->u_
.const_value
;
1998 const Named_constant
*
2001 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
2002 return this->u_
.const_value
;
2008 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2009 return this->u_
.type_value
;
2015 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
2016 return this->u_
.type_value
;
2020 type_declaration_value()
2022 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2023 return this->u_
.type_declaration
;
2026 const Type_declaration
*
2027 type_declaration_value() const
2029 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
2030 return this->u_
.type_declaration
;
2036 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2037 return this->u_
.var_value
;
2043 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
2044 return this->u_
.var_value
;
2050 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2051 return this->u_
.result_var_value
;
2054 const Result_variable
*
2055 result_var_value() const
2057 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
2058 return this->u_
.result_var_value
;
2064 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2065 return this->u_
.func_value
;
2071 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
2072 return this->u_
.func_value
;
2075 Function_declaration
*
2076 func_declaration_value()
2078 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2079 return this->u_
.func_declaration_value
;
2082 const Function_declaration
*
2083 func_declaration_value() const
2085 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
2086 return this->u_
.func_declaration_value
;
2092 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2093 return this->u_
.package_value
;
2097 package_value() const
2099 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
2100 return this->u_
.package_value
;
2105 { return this->name_
; }
2107 // Return the name to use in an error message. The difference is
2108 // that if this Named_object is defined in a different package, this
2109 // will return PACKAGE.NAME.
2111 message_name() const;
2115 { return this->package_
; }
2117 // Resolve an unknown value if possible. This returns the same
2118 // Named_object or a new one.
2122 Named_object
* ret
= this;
2123 if (this->is_unknown())
2125 Named_object
* r
= this->unknown_value()->real_named_object();
2135 const Named_object
* ret
= this;
2136 if (this->is_unknown())
2138 const Named_object
* r
= this->unknown_value()->real_named_object();
2145 // The location where this object was defined or referenced.
2149 // Convert a variable to the backend representation.
2151 get_backend_variable(Gogo
*, Named_object
* function
);
2153 // Return the external identifier for this object.
2157 // Get the backend representation of this object.
2159 get_backend(Gogo
*, std::vector
<Bexpression
*>&, std::vector
<Btype
*>&,
2160 std::vector
<Bfunction
*>&);
2162 // Define a type declaration.
2164 set_type_value(Named_type
*);
2166 // Define a function declaration.
2168 set_function_value(Function
*);
2170 // Declare an unknown name as a type declaration.
2174 // Export this object.
2176 export_named_object(Export
*) const;
2179 Named_object(const std::string
&, const Package
*, Classification
);
2181 // The name of the object.
2183 // The package that this object is in. This is NULL if it is in the
2184 // file we are compiling.
2185 const Package
* package_
;
2186 // The type of object this is.
2187 Classification classification_
;
2191 Unknown_name
* unknown_value
;
2192 Named_constant
* const_value
;
2193 Named_type
* type_value
;
2194 Type_declaration
* type_declaration
;
2195 Variable
* var_value
;
2196 Result_variable
* result_var_value
;
2197 Function
* func_value
;
2198 Function_declaration
* func_declaration_value
;
2199 Package
* package_value
;
2203 // A binding contour. This binds names to objects.
2208 // Type for mapping from names to objects.
2209 typedef Unordered_map(std::string
, Named_object
*) Contour
;
2211 Bindings(Bindings
* enclosing
);
2213 // Add an erroneous name.
2215 add_erroneous_name(const std::string
& name
)
2216 { return this->add_named_object(Named_object::make_erroneous_name(name
)); }
2218 // Add an unknown name.
2220 add_unknown_name(const std::string
& name
, Location location
)
2222 return this->add_named_object(Named_object::make_unknown_name(name
,
2228 add_constant(const Typed_identifier
& tid
, const Package
* package
,
2229 Expression
* expr
, int iota_value
)
2231 return this->add_named_object(Named_object::make_constant(tid
, package
,
2238 add_type(const std::string
& name
, const Package
* package
, Type
* type
,
2241 return this->add_named_object(Named_object::make_type(name
, package
, type
,
2245 // Add a named type. This is used for builtin types, and to add an
2246 // imported type to the global scope.
2248 add_named_type(Named_type
* named_type
);
2250 // Add a type declaration.
2252 add_type_declaration(const std::string
& name
, const Package
* package
,
2255 Named_object
* no
= Named_object::make_type_declaration(name
, package
,
2257 return this->add_named_object(no
);
2262 add_variable(const std::string
& name
, const Package
* package
,
2265 return this->add_named_object(Named_object::make_variable(name
, package
,
2269 // Add a result variable.
2271 add_result_variable(const std::string
& name
, Result_variable
* result
)
2273 return this->add_named_object(Named_object::make_result_variable(name
,
2279 add_function(const std::string
& name
, const Package
*, Function
* function
);
2281 // Add a function declaration.
2283 add_function_declaration(const std::string
& name
, const Package
* package
,
2284 Function_type
* type
, Location location
);
2286 // Add a package. The location is the location of the import
2289 add_package(const std::string
& alias
, Package
* package
)
2291 Named_object
* no
= Named_object::make_package(alias
, package
);
2292 return this->add_named_object(no
);
2295 // Define a type which was already declared.
2297 define_type(Named_object
*, Named_type
*);
2299 // Add a method to the list of objects. This is not added to the
2302 add_method(Named_object
*);
2304 // Add a named object to this binding.
2306 add_named_object(Named_object
* no
)
2307 { return this->add_named_object_to_contour(&this->bindings_
, no
); }
2309 // Clear all names in file scope from the bindings.
2311 clear_file_scope(Gogo
*);
2313 // Look up a name in this binding contour and in any enclosing
2314 // binding contours. This returns NULL if the name is not found.
2316 lookup(const std::string
&) const;
2318 // Look up a name in this binding contour without looking in any
2319 // enclosing binding contours. Returns NULL if the name is not found.
2321 lookup_local(const std::string
&) const;
2325 remove_binding(Named_object
*);
2327 // Mark all variables as used. This is used for some types of parse
2332 // Traverse the tree. See the Traverse class.
2334 traverse(Traverse
*, bool is_global
);
2336 // Iterate over definitions. This does not include things which
2337 // were only declared.
2339 typedef std::vector
<Named_object
*>::const_iterator
2340 const_definitions_iterator
;
2342 const_definitions_iterator
2343 begin_definitions() const
2344 { return this->named_objects_
.begin(); }
2346 const_definitions_iterator
2347 end_definitions() const
2348 { return this->named_objects_
.end(); }
2350 // Return the number of definitions.
2352 size_definitions() const
2353 { return this->named_objects_
.size(); }
2355 // Return whether there are no definitions.
2357 empty_definitions() const
2358 { return this->named_objects_
.empty(); }
2360 // Iterate over declarations. This is everything that has been
2361 // declared, which includes everything which has been defined.
2363 typedef Contour::const_iterator const_declarations_iterator
;
2365 const_declarations_iterator
2366 begin_declarations() const
2367 { return this->bindings_
.begin(); }
2369 const_declarations_iterator
2370 end_declarations() const
2371 { return this->bindings_
.end(); }
2373 // Return the number of declarations.
2375 size_declarations() const
2376 { return this->bindings_
.size(); }
2378 // Return whether there are no declarations.
2380 empty_declarations() const
2381 { return this->bindings_
.empty(); }
2383 // Return the first declaration.
2386 { return this->bindings_
.empty() ? NULL
: this->bindings_
.begin()->second
; }
2390 add_named_object_to_contour(Contour
*, Named_object
*);
2393 new_definition(Named_object
*, Named_object
*);
2395 // Enclosing bindings.
2396 Bindings
* enclosing_
;
2397 // The list of objects.
2398 std::vector
<Named_object
*> named_objects_
;
2399 // The mapping from names to objects.
2408 Label(const std::string
& name
)
2409 : name_(name
), location_(Linemap::unknown_location()), snapshot_(NULL
),
2410 refs_(), is_used_(false), blabel_(NULL
)
2413 // Return the label's name.
2416 { return this->name_
; }
2418 // Return whether the label has been defined.
2421 { return !Linemap::is_unknown_location(this->location_
); }
2423 // Return whether the label has been used.
2426 { return this->is_used_
; }
2428 // Record that the label is used.
2431 { this->is_used_
= true; }
2433 // Return the location of the definition.
2436 { return this->location_
; }
2438 // Return the bindings snapshot.
2441 { return this->snapshot_
; }
2443 // Add a snapshot of a goto which refers to this label.
2445 add_snapshot_ref(Bindings_snapshot
* snapshot
)
2447 go_assert(Linemap::is_unknown_location(this->location_
));
2448 this->refs_
.push_back(snapshot
);
2451 // Return the list of snapshots of goto statements which refer to
2453 const std::vector
<Bindings_snapshot
*>&
2455 { return this->refs_
; }
2457 // Clear the references.
2461 // Define the label at LOCATION with the given bindings snapshot.
2463 define(Location location
, Bindings_snapshot
* snapshot
)
2465 go_assert(Linemap::is_unknown_location(this->location_
)
2466 && this->snapshot_
== NULL
);
2467 this->location_
= location
;
2468 this->snapshot_
= snapshot
;
2471 // Return the backend representation for this label.
2473 get_backend_label(Translate_context
*);
2475 // Return an expression for the address of this label. This is used
2476 // to get the return address of a deferred function to see whether
2477 // the function may call recover.
2479 get_addr(Translate_context
*, Location location
);
2482 // The name of the label.
2484 // The location of the definition. This is 0 if the label has not
2485 // yet been defined.
2487 // A snapshot of the set of bindings defined at this label, used to
2488 // issue errors about invalid goto statements.
2489 Bindings_snapshot
* snapshot_
;
2490 // A list of snapshots of goto statements which refer to this label.
2491 std::vector
<Bindings_snapshot
*> refs_
;
2492 // Whether the label has been used.
2494 // The backend representation.
2498 // An unnamed label. These are used when lowering loops.
2503 Unnamed_label(Location location
)
2504 : location_(location
), blabel_(NULL
)
2507 // Get the location where the label is defined.
2510 { return this->location_
; }
2512 // Set the location where the label is defined.
2514 set_location(Location location
)
2515 { this->location_
= location
; }
2517 // Return a statement which defines this label.
2519 get_definition(Translate_context
*);
2521 // Return a goto to this label from LOCATION.
2523 get_goto(Translate_context
*, Location location
);
2526 // Return the backend representation.
2528 get_blabel(Translate_context
*);
2530 // The location where the label is defined.
2532 // The backend representation of this label.
2536 // An imported package.
2541 Package(const std::string
& pkgpath
, Location location
);
2543 // Get the package path used for all symbols exported from this
2547 { return this->pkgpath_
; }
2549 // Return the package path to use for a symbol name.
2551 pkgpath_symbol() const
2552 { return this->pkgpath_symbol_
; }
2554 // Return the location of the import statement.
2557 { return this->location_
; }
2559 // Return whether we know the name of this package yet.
2561 has_package_name() const
2562 { return !this->package_name_
.empty(); }
2564 // The name that this package uses in its package clause. This may
2565 // be different from the name in the associated Named_object if the
2566 // import statement used an alias.
2568 package_name() const
2570 go_assert(!this->package_name_
.empty());
2571 return this->package_name_
;
2574 // The priority of this package. The init function of packages with
2575 // lower priority must be run before the init function of packages
2576 // with higher priority.
2579 { return this->priority_
; }
2581 // Set the priority.
2583 set_priority(int priority
);
2585 // Return the bindings.
2588 { return this->bindings_
; }
2590 // Whether some symbol from the package was used.
2593 { return this->used_
; }
2595 // Note that some symbol from this package was used.
2598 { this->used_
= true; }
2600 // Clear the used field for the next file.
2603 { this->used_
= false; }
2605 // Whether this package was imported in the current file.
2608 { return this->is_imported_
; }
2610 // Note that this package was imported in the current file.
2613 { this->is_imported_
= true; }
2615 // Clear the imported field for the next file.
2618 { this->is_imported_
= false; }
2620 // Whether this package was imported with a name of "_".
2622 uses_sink_alias() const
2623 { return this->uses_sink_alias_
; }
2625 // Note that this package was imported with a name of "_".
2627 set_uses_sink_alias()
2628 { this->uses_sink_alias_
= true; }
2630 // Clear the sink alias field for the next file.
2632 clear_uses_sink_alias()
2633 { this->uses_sink_alias_
= false; }
2635 // Look up a name in the package. Returns NULL if the name is not
2638 lookup(const std::string
& name
) const
2639 { return this->bindings_
->lookup(name
); }
2641 // Set the name of the package.
2643 set_package_name(const std::string
& name
, Location
);
2645 // Set the location of the package. This is used to record the most
2646 // recent import location.
2648 set_location(Location location
)
2649 { this->location_
= location
; }
2651 // Add a constant to the package.
2653 add_constant(const Typed_identifier
& tid
, Expression
* expr
)
2654 { return this->bindings_
->add_constant(tid
, this, expr
, 0); }
2656 // Add a type to the package.
2658 add_type(const std::string
& name
, Type
* type
, Location location
)
2659 { return this->bindings_
->add_type(name
, this, type
, location
); }
2661 // Add a type declaration to the package.
2663 add_type_declaration(const std::string
& name
, Location location
)
2664 { return this->bindings_
->add_type_declaration(name
, this, location
); }
2666 // Add a variable to the package.
2668 add_variable(const std::string
& name
, Variable
* variable
)
2669 { return this->bindings_
->add_variable(name
, this, variable
); }
2671 // Add a function declaration to the package.
2673 add_function_declaration(const std::string
& name
, Function_type
* type
,
2675 { return this->bindings_
->add_function_declaration(name
, this, type
, loc
); }
2677 // Determine types of constants.
2682 // The package path for type reflection data.
2683 std::string pkgpath_
;
2684 // The package path for symbol names.
2685 std::string pkgpath_symbol_
;
2686 // The name that this package uses in the package clause. This may
2687 // be the empty string if it is not yet known.
2688 std::string package_name_
;
2689 // The names in this package.
2690 Bindings
* bindings_
;
2691 // The priority of this package. A package has a priority higher
2692 // than the priority of all of the packages that it imports. This
2693 // is used to run init functions in the right order.
2695 // The location of the import statement.
2697 // True if some name from this package was used. This is mutable
2698 // because we can use a package even if we have a const pointer to
2701 // True if this package was imported in the current file.
2703 // True if this package was imported with a name of "_".
2704 bool uses_sink_alias_
;
2707 // Return codes for the traversal functions. This is not an enum
2708 // because we want to be able to declare traversal functions in other
2709 // header files without including this one.
2711 // Continue traversal as usual.
2712 const int TRAVERSE_CONTINUE
= -1;
2715 const int TRAVERSE_EXIT
= 0;
2717 // Continue traversal, but skip components of the current object.
2718 // E.g., if this is returned by Traverse::statement, we do not
2719 // traverse the expressions in the statement even if
2720 // traverse_expressions is set in the traverse_mask.
2721 const int TRAVERSE_SKIP_COMPONENTS
= 1;
2723 // This class is used when traversing the parse tree. The caller uses
2724 // a subclass which overrides functions as desired.
2729 // These bitmasks say what to traverse.
2730 static const unsigned int traverse_variables
= 0x1;
2731 static const unsigned int traverse_constants
= 0x2;
2732 static const unsigned int traverse_functions
= 0x4;
2733 static const unsigned int traverse_blocks
= 0x8;
2734 static const unsigned int traverse_statements
= 0x10;
2735 static const unsigned int traverse_expressions
= 0x20;
2736 static const unsigned int traverse_types
= 0x40;
2738 Traverse(unsigned int traverse_mask
)
2739 : traverse_mask_(traverse_mask
), types_seen_(NULL
), expressions_seen_(NULL
)
2742 virtual ~Traverse();
2744 // The bitmask of what to traverse.
2746 traverse_mask() const
2747 { return this->traverse_mask_
; }
2749 // Record that we are going to traverse a type. This returns true
2750 // if the type has already been seen in this traversal. This is
2751 // required because types, unlike expressions, can form a circular
2754 remember_type(const Type
*);
2756 // Record that we are going to see an expression. This returns true
2757 // if the expression has already been seen in this traversal. This
2758 // is only needed for cases where multiple expressions can point to
2761 remember_expression(const Expression
*);
2763 // These functions return one of the TRAVERSE codes defined above.
2765 // If traverse_variables is set in the mask, this is called for
2766 // every variable in the tree.
2768 variable(Named_object
*);
2770 // If traverse_constants is set in the mask, this is called for
2771 // every named constant in the tree. The bool parameter is true for
2772 // a global constant.
2774 constant(Named_object
*, bool);
2776 // If traverse_functions is set in the mask, this is called for
2777 // every function in the tree.
2779 function(Named_object
*);
2781 // If traverse_blocks is set in the mask, this is called for every
2782 // block in the tree.
2786 // If traverse_statements is set in the mask, this is called for
2787 // every statement in the tree.
2789 statement(Block
*, size_t* index
, Statement
*);
2791 // If traverse_expressions is set in the mask, this is called for
2792 // every expression in the tree.
2794 expression(Expression
**);
2796 // If traverse_types is set in the mask, this is called for every
2797 // type in the tree.
2802 // A hash table for types we have seen during this traversal. Note
2803 // that this uses the default hash functions for pointers rather
2804 // than Type_hash_identical and Type_identical. This is because for
2805 // traversal we care about seeing a specific type structure. If
2806 // there are two separate instances of identical types, we want to
2808 typedef Unordered_set(const Type
*) Types_seen
;
2810 typedef Unordered_set(const Expression
*) Expressions_seen
;
2812 // Bitmask of what sort of objects to traverse.
2813 unsigned int traverse_mask_
;
2814 // Types which have been seen in this traversal.
2815 Types_seen
* types_seen_
;
2816 // Expressions which have been seen in this traversal.
2817 Expressions_seen
* expressions_seen_
;
2820 // A class which makes it easier to insert new statements before the
2821 // current statement during a traversal.
2823 class Statement_inserter
2826 // Empty constructor.
2827 Statement_inserter()
2828 : block_(NULL
), pindex_(NULL
), gogo_(NULL
), var_(NULL
)
2831 // Constructor for a statement in a block.
2832 Statement_inserter(Block
* block
, size_t *pindex
)
2833 : block_(block
), pindex_(pindex
), gogo_(NULL
), var_(NULL
)
2836 // Constructor for a global variable.
2837 Statement_inserter(Gogo
* gogo
, Variable
* var
)
2838 : block_(NULL
), pindex_(NULL
), gogo_(gogo
), var_(var
)
2839 { go_assert(var
->is_global()); }
2841 // We use the default copy constructor and assignment operator.
2843 // Insert S before the statement we are traversing, or before the
2844 // initialization expression of a global variable.
2846 insert(Statement
* s
);
2849 // The block that the statement is in.
2851 // The index of the statement that we are traversing.
2853 // The IR, needed when looking at an initializer expression for a
2856 // The global variable, when looking at an initializer expression.
2860 // When translating the gogo IR into the backend data structure, this
2861 // is the context we pass down the blocks and statements.
2863 class Translate_context
2866 Translate_context(Gogo
* gogo
, Named_object
* function
, Block
* block
,
2868 : gogo_(gogo
), backend_(gogo
->backend()), function_(function
),
2869 block_(block
), bblock_(bblock
), is_const_(false)
2876 { return this->gogo_
; }
2880 { return this->backend_
; }
2884 { return this->function_
; }
2888 { return this->block_
; }
2892 { return this->bblock_
; }
2896 { return this->is_const_
; }
2898 // Make a constant context.
2901 { this->is_const_
= true; }
2904 // The IR for the entire compilation unit.
2906 // The generator for the backend data structures.
2908 // The function we are currently translating. NULL if not in a
2909 // function, e.g., the initializer of a global variable.
2910 Named_object
* function_
;
2911 // The block we are currently translating. NULL if not in a
2914 // The backend representation of the current block. NULL if block_
2917 // Whether this is being evaluated in a constant context. This is
2918 // used for type descriptor initializers.
2922 // Runtime error codes. These must match the values in
2923 // libgo/runtime/go-runtime-error.c.
2925 // Slice index out of bounds: negative or larger than the length of
2927 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
= 0;
2929 // Array index out of bounds.
2930 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
= 1;
2932 // String index out of bounds.
2933 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
= 2;
2935 // Slice slice out of bounds: negative or larger than the length of
2936 // the slice or high bound less than low bound.
2937 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
= 3;
2939 // Array slice out of bounds.
2940 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
= 4;
2942 // String slice out of bounds.
2943 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
= 5;
2945 // Dereference of nil pointer. This is used when there is a
2946 // dereference of a pointer to a very large struct or array, to ensure
2947 // that a gigantic array is not used a proxy to access random memory
2949 static const int RUNTIME_ERROR_NIL_DEREFERENCE
= 6;
2951 // Slice length or capacity out of bounds in make: negative or
2952 // overflow or length greater than capacity.
2953 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
= 7;
2955 // Map capacity out of bounds in make: negative or overflow.
2956 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
= 8;
2958 // Channel capacity out of bounds in make: negative or overflow.
2959 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
= 9;
2961 // Division by zero.
2962 static const int RUNTIME_ERROR_DIVISION_BY_ZERO
= 10;
2964 // This is used by some of the langhooks.
2965 extern Gogo
* go_get_gogo();
2967 // Whether we have seen any errors. FIXME: Replace with a backend
2969 extern bool saw_errors();
2971 #endif // !defined(GO_GOGO_H)