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
;
44 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 // a prefix of the package name, so that hidden symbols in different
142 // packages do not collide.
144 pack_hidden_name(const std::string
& name
, bool is_exported
) const
148 : ('.' + this->unique_prefix()
149 + '.' + this->package_name()
153 // Unpack a name which may have been hidden. Returns the
154 // user-visible name of the object.
156 unpack_hidden_name(const std::string
& name
)
157 { return name
[0] != '.' ? name
: name
.substr(name
.rfind('.') + 1); }
159 // Return whether a possibly packed name is hidden.
161 is_hidden_name(const std::string
& name
)
162 { return name
[0] == '.'; }
164 // Return the package prefix of a hidden name.
166 hidden_name_prefix(const std::string
& name
)
168 go_assert(Gogo::is_hidden_name(name
));
169 return name
.substr(1, name
.rfind('.') - 1);
172 // Given a name which may or may not have been hidden, return the
173 // name to use in an error message.
175 message_name(const std::string
& name
);
177 // Return whether a name is the blank identifier _.
179 is_sink_name(const std::string
& name
)
181 return (name
[0] == '.'
182 && name
[name
.length() - 1] == '_'
183 && name
[name
.length() - 2] == '.');
186 // Return the unique prefix to use for all exported symbols.
188 unique_prefix() const;
190 // Set the unique prefix.
192 set_unique_prefix(const std::string
&);
194 // Return the priority to use for the package we are compiling.
195 // This is two more than the largest priority of any package we
198 package_priority() const;
200 // Import a package. FILENAME is the file name argument, LOCAL_NAME
201 // is the local name to give to the package. If LOCAL_NAME is empty
202 // the declarations are added to the global scope.
204 import_package(const std::string
& filename
, const std::string
& local_name
,
205 bool is_local_name_exported
, Location
);
207 // Whether we are the global binding level.
209 in_global_scope() const;
211 // Look up a name in the current binding contours.
213 lookup(const std::string
&, Named_object
** pfunction
) const;
215 // Look up a name in the current block.
217 lookup_in_block(const std::string
&) const;
219 // Look up a name in the global namespace--the universal scope.
221 lookup_global(const char*) const;
223 // Add a new imported package. REAL_NAME is the real name of the
224 // package. ALIAS is the alias of the package; this may be the same
225 // as REAL_NAME. This sets *PADD_TO_GLOBALS if symbols added to
226 // this package should be added to the global namespace; this is
227 // true if the alias is ".". LOCATION is the location of the import
228 // statement. This returns the new package, or NULL on error.
230 add_imported_package(const std::string
& real_name
, const std::string
& alias
,
231 bool is_alias_exported
,
232 const std::string
& unique_prefix
,
234 bool* padd_to_globals
);
236 // Register a package. This package may or may not be imported.
237 // This returns the Package structure for the package, creating if
240 register_package(const std::string
& name
, const std::string
& unique_prefix
,
243 // Start compiling a function. ADD_METHOD_TO_TYPE is true if a
244 // method function should be added to the type of its receiver.
246 start_function(const std::string
& name
, Function_type
* type
,
247 bool add_method_to_type
, Location
);
249 // Finish compiling a function.
251 finish_function(Location
);
253 // Return the current function.
255 current_function() const;
257 // Return the current block.
261 // Start a new block. This is not initially associated with a
264 start_block(Location
);
266 // Finish the current block and return it.
268 finish_block(Location
);
270 // Declare an erroneous name. This is used to avoid knock-on errors
271 // after a parsing error.
273 add_erroneous_name(const std::string
& name
);
275 // Declare an unknown name. This is used while parsing. The name
276 // must be resolved by the end of the parse. Unknown names are
277 // always added at the package level.
279 add_unknown_name(const std::string
& name
, Location
);
281 // Declare a function.
283 declare_function(const std::string
&, Function_type
*, Location
);
285 // Declare a function at the package level. This is used for
286 // functions generated for a type.
288 declare_package_function(const std::string
&, Function_type
*, Location
);
292 add_label_definition(const std::string
&, Location
);
294 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
295 // report errors for a goto from the current location to the label
298 add_label_reference(const std::string
&, Location
,
299 bool issue_goto_errors
);
301 // Return a snapshot of the current binding state.
303 bindings_snapshot(Location
);
305 // Add a statement to the current block.
307 add_statement(Statement
*);
309 // Add a block to the current block.
311 add_block(Block
*, Location
);
315 add_constant(const Typed_identifier
&, Expression
*, int iota_value
);
319 add_type(const std::string
&, Type
*, Location
);
321 // Add a named type. This is used for builtin types, and to add an
322 // imported type to the global scope.
324 add_named_type(Named_type
*);
328 declare_type(const std::string
&, Location
);
330 // Declare a type at the package level. This is used when the
331 // parser sees an unknown name where a type name is required.
333 declare_package_type(const std::string
&, Location
);
335 // Define a type which was already declared.
337 define_type(Named_object
*, Named_type
*);
341 add_variable(const std::string
&, Variable
*);
343 // Add a sink--a reference to the blank identifier _.
347 // Add a type which needs to be verified. This is used for sink
348 // types, just to give appropriate error messages.
350 add_type_to_verify(Type
* type
);
352 // Add a named object to the current namespace. This is used for
353 // import . "package".
355 add_named_object(Named_object
*);
357 // Mark all local variables in current bindings as used. This is
358 // used when there is a parse error to avoid useless errors.
362 // Return a name to use for a thunk function. A thunk function is
363 // one we create during the compilation, for a go statement or a
364 // defer statement or a method expression.
368 // Return whether an object is a thunk.
370 is_thunk(const Named_object
*);
372 // Note that we've seen an interface type. This is used to build
373 // all required interface method tables.
375 record_interface_type(Interface_type
*);
377 // Note that we need an initialization function.
380 { this->need_init_fn_
= true; }
382 // Clear out all names in file scope. This is called when we start
383 // parsing a new file.
387 // Queue up a type-specific function to be written out. This is
388 // used when a type-specific function is needed when not at the top
391 queue_specific_type_function(Type
* type
, Named_type
* name
,
392 const std::string
& hash_name
,
393 Function_type
* hash_fntype
,
394 const std::string
& equal_name
,
395 Function_type
* equal_fntype
);
397 // Write out queued specific type functions.
399 write_specific_type_functions();
401 // Traverse the tree. See the Traverse class.
405 // Define the predeclared global names.
407 define_global_names();
409 // Verify and complete all types.
413 // Lower the parse tree.
417 // Lower all the statements in a block.
419 lower_block(Named_object
* function
, Block
*);
421 // Lower an expression.
423 lower_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
427 lower_constant(Named_object
*);
429 // Finalize the method lists and build stub methods for named types.
433 // Work out the types to use for unspecified variables and
438 // Type check the program.
442 // Check the types in a single block. This is used for complicated
445 check_types_in_block(Block
*);
447 // Check for return statements.
449 check_return_statements();
455 // Add an import control function for an imported package to the
458 add_import_init_fn(const std::string
& package_name
,
459 const std::string
& init_name
, int prio
);
461 // Turn short-cut operators (&&, ||) into explicit if statements.
465 // Use temporary variables to force order of evaluation.
469 // Build thunks for functions which call recover.
471 build_recover_thunks();
473 // Simplify statements which might use thunks: go and defer
476 simplify_thunk_statements();
478 // Dump AST if -fgo-dump-ast is set
480 dump_ast(const char* basename
);
482 // Convert named types to the backend representation.
484 convert_named_types();
486 // Convert named types in a list of bindings.
488 convert_named_types_in_bindings(Bindings
*);
490 // True if named types have been converted to the backend
493 named_types_are_converted() const
494 { return this->named_types_are_converted_
; }
496 // Write out the global values.
500 // Create trees for implicit builtin functions.
502 define_builtin_function_trees();
504 // Build a call to a builtin function. PDECL should point to a NULL
505 // initialized static pointer which will hold the fndecl. NAME is
506 // the name of the function. NARGS is the number of arguments.
507 // RETTYPE is the return type. It is followed by NARGS pairs of
508 // type and argument (both trees).
510 call_builtin(tree
* pdecl
, Location
, const char* name
, int nargs
,
513 // Build a call to the runtime error function.
515 runtime_error(int code
, Location
);
517 // Build a builtin struct with a list of fields.
519 builtin_struct(tree
* ptype
, const char* struct_name
, tree struct_type
,
522 // Mark a function declaration as a builtin library function.
524 mark_fndecl_as_builtin_library(tree fndecl
);
526 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
527 // the slice. VALUES points to the values. COUNT is the size,
528 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
531 slice_constructor(tree slice_type_tree
, tree values
, tree count
,
534 // Build required interface method tables.
536 build_interface_method_tables();
538 // Build an interface method table for a type: a list of function
539 // pointers, one for each interface method. This returns a decl.
541 interface_method_table_for_type(const Interface_type
*, Named_type
*,
544 // Return a tree which allocate SIZE bytes to hold values of type
547 allocate_memory(Type
*type
, tree size
, Location
);
549 // Return a type to use for pointer to const char.
551 const_char_pointer_type_tree();
553 // Build a string constant with the right type.
555 string_constant_tree(const std::string
&);
557 // Build a Go string constant. This returns a pointer to the
560 go_string_constant_tree(const std::string
&);
562 // Receive a value from a channel.
564 receive_from_channel(tree type_tree
, tree type_descriptor_tree
, tree channel
,
567 // Make a trampoline which calls FNADDR passing CLOSURE.
569 make_trampoline(tree fnaddr
, tree closure
, Location
);
572 // During parsing, we keep a stack of functions. Each function on
573 // the stack is one that we are currently parsing. For each
574 // function, we keep track of the current stack of blocks.
578 Named_object
* function
;
579 // The stack of active blocks in the function.
580 std::vector
<Block
*> blocks
;
583 // The stack of functions.
584 typedef std::vector
<Open_function
> Open_functions
;
586 // Set up the built-in unsafe package.
588 import_unsafe(const std::string
&, bool is_exported
, Location
);
590 // Add a new imported package.
592 add_package(const std::string
& real_name
, const std::string
& alias
,
593 const std::string
& unique_prefix
, Location location
);
595 // Return the current binding contour.
600 current_bindings() const;
602 // Get the name of the magic initialization function.
606 // Get the decl for the magic initialization function.
608 initialization_function_decl();
610 // Write the magic initialization function.
612 write_initialization_function(tree fndecl
, tree init_stmt_list
);
614 // Initialize imported packages.
618 // Register variables with the garbage collector.
620 register_gc_vars(const std::vector
<Named_object
*>&, tree
*);
622 // Build a pointer to a Go string constant. This returns a pointer
625 ptr_go_string_constant_tree(const std::string
&);
627 // Return the type of a trampoline.
629 trampoline_type_tree();
631 // Type used to map import names to packages.
632 typedef std::map
<std::string
, Package
*> Imports
;
634 // Type used to map package names to packages.
635 typedef std::map
<std::string
, Package
*> Packages
;
637 // Type used to map special names in the sys package.
638 typedef std::map
<std::string
, std::string
> Sys_names
;
640 // Type used to queue writing a type specific function.
641 struct Specific_type_function
645 std::string hash_name
;
646 Function_type
* hash_fntype
;
647 std::string equal_name
;
648 Function_type
* equal_fntype
;
650 Specific_type_function(Type
* atype
, Named_type
* aname
,
651 const std::string
& ahash_name
,
652 Function_type
* ahash_fntype
,
653 const std::string
& aequal_name
,
654 Function_type
* aequal_fntype
)
655 : type(atype
), name(aname
), hash_name(ahash_name
),
656 hash_fntype(ahash_fntype
), equal_name(aequal_name
),
657 equal_fntype(aequal_fntype
)
661 // The backend generator.
663 // The object used to keep track of file names and line numbers.
665 // The package we are compiling.
667 // The list of currently open functions during parsing.
668 Open_functions functions_
;
669 // The global binding contour. This includes the builtin functions
670 // and the package we are compiling.
672 // Mapping from import file names to packages.
674 // Whether the magic unsafe package was imported.
675 bool imported_unsafe_
;
676 // Mapping from package names we have seen to packages. This does
677 // not include the package we are compiling.
679 // The functions named "init", if there are any.
680 std::vector
<Named_object
*> init_functions_
;
681 // Whether we need a magic initialization function.
683 // The name of the magic initialization function.
684 std::string init_fn_name_
;
685 // A list of import control variables for packages that we import.
686 std::set
<Import_init
> imported_init_fns_
;
687 // The unique prefix used for all global symbols.
688 std::string unique_prefix_
;
689 // Whether an explicit unique prefix was set by -fgo-prefix.
690 bool unique_prefix_specified_
;
691 // A list of types to verify.
692 std::vector
<Type
*> verify_types_
;
693 // A list of interface types defined while parsing.
694 std::vector
<Interface_type
*> interface_types_
;
695 // Type specific functions to write out.
696 std::vector
<Specific_type_function
*> specific_type_functions_
;
697 // Whether we are done writing out specific type functions.
698 bool specific_type_functions_are_written_
;
699 // Whether named types have been converted.
700 bool named_types_are_converted_
;
703 // A block of statements.
708 Block(Block
* enclosing
, Location
);
710 // Return the enclosing block.
713 { return this->enclosing_
; }
715 // Return the bindings of the block.
718 { return this->bindings_
; }
722 { return this->bindings_
; }
724 // Look at the block's statements.
725 const std::vector
<Statement
*>*
727 { return &this->statements_
; }
729 // Return the start location. This is normally the location of the
730 // left curly brace which starts the block.
732 start_location() const
733 { return this->start_location_
; }
735 // Return the end location. This is normally the location of the
736 // right curly brace which ends the block.
739 { return this->end_location_
; }
741 // Add a statement to the block.
743 add_statement(Statement
*);
745 // Add a statement to the front of the block.
747 add_statement_at_front(Statement
*);
749 // Replace a statement in a block.
751 replace_statement(size_t index
, Statement
*);
753 // Add a Statement before statement number INDEX.
755 insert_statement_before(size_t index
, Statement
*);
757 // Add a Statement after statement number INDEX.
759 insert_statement_after(size_t index
, Statement
*);
761 // Set the end location of the block.
763 set_end_location(Location location
)
764 { this->end_location_
= location
; }
766 // Traverse the tree.
770 // Set final types for unspecified variables and constants.
774 // Return true if execution of this block may fall through to the
777 may_fall_through() const;
779 // Convert the block to the backend representation.
781 get_backend(Translate_context
*);
783 // Iterate over statements.
785 typedef std::vector
<Statement
*>::iterator iterator
;
789 { return this->statements_
.begin(); }
793 { return this->statements_
.end(); }
798 // Statements in the block.
799 std::vector
<Statement
*> statements_
;
802 // Location of start of block.
803 Location start_location_
;
804 // Location of end of block.
805 Location end_location_
;
813 Function(Function_type
* type
, Function
*, Block
*, Location
);
815 // Return the function's type.
818 { return this->type_
; }
820 // Return the enclosing function if there is one.
823 { return this->enclosing_
; }
825 // Set the enclosing function. This is used when building thunks
826 // for functions which call recover.
828 set_enclosing(Function
* enclosing
)
830 go_assert(this->enclosing_
== NULL
);
831 this->enclosing_
= enclosing
;
834 // The result variables.
835 typedef std::vector
<Named_object
*> Results
;
837 // Create the result variables in the outer block.
839 create_result_variables(Gogo
*);
841 // Update the named result variables when cloning a function which
844 update_result_variables();
846 // Return the result variables.
849 { return this->results_
; }
851 // Whether the result variables have names.
853 results_are_named() const
854 { return this->results_are_named_
; }
856 // Add a new field to the closure variable.
858 add_closure_field(Named_object
* var
, Location loc
)
859 { this->closure_fields_
.push_back(std::make_pair(var
, loc
)); }
861 // Whether this function needs a closure.
863 needs_closure() const
864 { return !this->closure_fields_
.empty(); }
866 // Return the closure variable, creating it if necessary. This is
867 // passed to the function as a static chain parameter.
871 // Set the closure variable. This is used when building thunks for
872 // functions which call recover.
874 set_closure_var(Named_object
* v
)
876 go_assert(this->closure_var_
== NULL
);
877 this->closure_var_
= v
;
880 // Return the variable for a reference to field INDEX in the closure
883 enclosing_var(unsigned int index
)
885 go_assert(index
< this->closure_fields_
.size());
886 return closure_fields_
[index
].first
;
889 // Set the type of the closure variable if there is one.
893 // Get the block of statements associated with the function.
896 { return this->block_
; }
898 // Get the location of the start of the function.
901 { return this->location_
; }
903 // Return whether this function is actually a method.
907 // Add a label definition to the function.
909 add_label_definition(Gogo
*, const std::string
& label_name
, Location
);
911 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
912 // if we should report errors for a goto from the current location
913 // to the label location.
915 add_label_reference(Gogo
*, const std::string
& label_name
,
916 Location
, bool issue_goto_errors
);
918 // Warn about labels that are defined but not used.
920 check_labels() const;
922 // Whether this function calls the predeclared recover function.
924 calls_recover() const
925 { return this->calls_recover_
; }
927 // Record that this function calls the predeclared recover function.
928 // This is set during the lowering pass.
931 { this->calls_recover_
= true; }
933 // Whether this is a recover thunk function.
935 is_recover_thunk() const
936 { return this->is_recover_thunk_
; }
938 // Record that this is a thunk built for a function which calls
941 set_is_recover_thunk()
942 { this->is_recover_thunk_
= true; }
944 // Whether this function already has a recover thunk.
946 has_recover_thunk() const
947 { return this->has_recover_thunk_
; }
949 // Record that this function already has a recover thunk.
951 set_has_recover_thunk()
952 { this->has_recover_thunk_
= true; }
954 // Swap with another function. Used only for the thunk which calls
957 swap_for_recover(Function
*);
959 // Traverse the tree.
963 // Determine types in the function.
967 // Return the function's decl given an identifier.
969 get_or_make_decl(Gogo
*, Named_object
*, tree id
);
971 // Return the function's decl after it has been built.
975 go_assert(this->fndecl_
!= NULL
);
976 return this->fndecl_
;
979 // Set the function decl to hold a tree of the function code.
981 build_tree(Gogo
*, Named_object
*);
983 // Get the value to return when not explicitly specified. May also
984 // add statements to execute first to STMT_LIST.
986 return_value(Gogo
*, Named_object
*, Location
, tree
* stmt_list
) const;
988 // Get a tree for the variable holding the defer stack.
990 defer_stack(Location
);
992 // Export the function.
994 export_func(Export
*, const std::string
& name
) const;
996 // Export a function with a type.
998 export_func_with_type(Export
*, const std::string
& name
,
999 const Function_type
*);
1001 // Import a function.
1003 import_func(Import
*, std::string
* pname
, Typed_identifier
** receiver
,
1004 Typed_identifier_list
** pparameters
,
1005 Typed_identifier_list
** presults
, bool* is_varargs
);
1008 // Type for mapping from label names to Label objects.
1009 typedef Unordered_map(std::string
, Label
*) Labels
;
1012 make_receiver_parm_decl(Gogo
*, Named_object
*, tree
);
1015 copy_parm_to_heap(Gogo
*, Named_object
*, tree
);
1018 build_defer_wrapper(Gogo
*, Named_object
*, tree
*, tree
*);
1020 typedef std::vector
<std::pair
<Named_object
*,
1021 Location
> > Closure_fields
;
1023 // The function's type.
1024 Function_type
* type_
;
1025 // The enclosing function. This is NULL when there isn't one, which
1026 // is the normal case.
1027 Function
* enclosing_
;
1028 // The result variables, if any.
1030 // If there is a closure, this is the list of variables which appear
1031 // in the closure. This is created by the parser, and then resolved
1032 // to a real type when we lower parse trees.
1033 Closure_fields closure_fields_
;
1034 // The closure variable, passed as a parameter using the static
1035 // chain parameter. Normally NULL.
1036 Named_object
* closure_var_
;
1037 // The outer block of statements in the function.
1039 // The source location of the start of the function.
1041 // Labels defined or referenced in the function.
1043 // The function decl.
1045 // The defer stack variable. A pointer to this variable is used to
1046 // distinguish the defer stack for one function from another. This
1047 // is NULL unless we actually need a defer stack.
1048 Temporary_statement
* defer_stack_
;
1049 // True if the result variables are named.
1050 bool results_are_named_
;
1051 // True if this function calls the predeclared recover function.
1052 bool calls_recover_
;
1053 // True if this a thunk built for a function which calls recover.
1054 bool is_recover_thunk_
;
1055 // True if this function already has a recover thunk.
1056 bool has_recover_thunk_
;
1059 // A snapshot of the current binding state.
1061 class Bindings_snapshot
1064 Bindings_snapshot(const Block
*, Location
);
1066 // Report any errors appropriate for a goto from the current binding
1067 // state of B to this one.
1069 check_goto_from(const Block
* b
, Location
);
1071 // Report any errors appropriate for a goto from this binding state
1072 // to the current state of B.
1074 check_goto_to(const Block
* b
);
1078 check_goto_block(Location
, const Block
*, const Block
*, size_t*);
1081 check_goto_defs(Location
, const Block
*, size_t, size_t);
1083 // The current block.
1084 const Block
* block_
;
1085 // The number of names currently defined in each open block.
1086 // Element 0 is this->block_, element 1 is
1087 // this->block_->enclosing(), etc.
1088 std::vector
<size_t> counts_
;
1089 // The location where this snapshot was taken.
1093 // A function declaration.
1095 class Function_declaration
1098 Function_declaration(Function_type
* fntype
, Location location
)
1099 : fntype_(fntype
), location_(location
), asm_name_(), fndecl_(NULL
)
1104 { return this->fntype_
; }
1108 { return this->location_
; }
1112 { return this->asm_name_
; }
1114 // Set the assembler name.
1116 set_asm_name(const std::string
& asm_name
)
1117 { this->asm_name_
= asm_name
; }
1119 // Return a decl for the function given an identifier.
1121 get_or_make_decl(Gogo
*, Named_object
*, tree id
);
1123 // Export a function declaration.
1125 export_func(Export
* exp
, const std::string
& name
) const
1126 { Function::export_func_with_type(exp
, name
, this->fntype_
); }
1129 // The type of the function.
1130 Function_type
* fntype_
;
1131 // The location of the declaration.
1133 // The assembler name: this is the name to use in references to the
1134 // function. This is normally empty.
1135 std::string asm_name_
;
1136 // The function decl if needed.
1145 Variable(Type
*, Expression
*, bool is_global
, bool is_parameter
,
1146 bool is_receiver
, Location
);
1148 // Get the type of the variable.
1155 // Return whether the type is defined yet.
1159 // Get the initial value.
1162 { return this->init_
; }
1164 // Return whether there are any preinit statements.
1166 has_pre_init() const
1167 { return this->preinit_
!= NULL
; }
1169 // Return the preinit statements if any.
1172 { return this->preinit_
; }
1174 // Return whether this is a global variable.
1177 { return this->is_global_
; }
1179 // Return whether this is a function parameter.
1181 is_parameter() const
1182 { return this->is_parameter_
; }
1184 // Return whether this is the receiver parameter of a method.
1187 { return this->is_receiver_
; }
1189 // Change this parameter to be a receiver. This is used when
1190 // creating the thunks created for functions which call recover.
1194 go_assert(this->is_parameter_
);
1195 this->is_receiver_
= true;
1198 // Change this parameter to not be a receiver. This is used when
1199 // creating the thunks created for functions which call recover.
1201 set_is_not_receiver()
1203 go_assert(this->is_parameter_
);
1204 this->is_receiver_
= false;
1207 // Return whether this is the varargs parameter of a function.
1209 is_varargs_parameter() const
1210 { return this->is_varargs_parameter_
; }
1212 // Whether this variable's address is taken.
1214 is_address_taken() const
1215 { return this->is_address_taken_
; }
1217 // Whether this variable should live in the heap.
1220 { return this->is_address_taken_
&& !this->is_global_
; }
1222 // Note that something takes the address of this variable.
1225 { this->is_address_taken_
= true; }
1227 // Return whether the address is taken but does not escape.
1229 is_non_escaping_address_taken() const
1230 { return this->is_non_escaping_address_taken_
; }
1232 // Note that something takes the address of this variable such that
1233 // the address does not escape the function.
1235 set_non_escaping_address_taken()
1236 { this->is_non_escaping_address_taken_
= true; }
1238 // Get the source location of the variable's declaration.
1241 { return this->location_
; }
1243 // Record that this is the varargs parameter of a function.
1245 set_is_varargs_parameter()
1247 go_assert(this->is_parameter_
);
1248 this->is_varargs_parameter_
= true;
1251 // Return whether the variable has been used.
1254 { return this->is_used_
; }
1256 // Mark that the variable has been used.
1259 { this->is_used_
= true; }
1261 // Clear the initial value; used for error handling.
1264 { this->init_
= NULL
; }
1266 // Set the initial value; used for converting shortcuts.
1268 set_init(Expression
* init
)
1269 { this->init_
= init
; }
1271 // Get the preinit block, a block of statements to be run before the
1272 // initialization expression.
1274 preinit_block(Gogo
*);
1276 // Add a statement to be run before the initialization expression.
1277 // This is only used for global variables.
1279 add_preinit_statement(Gogo
*, Statement
*);
1281 // Lower the initialization expression after parsing is complete.
1283 lower_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1285 // A special case: the init value is used only to determine the
1286 // type. This is used if the variable is defined using := with the
1287 // comma-ok form of a map index or a receive expression. The init
1288 // value is actually the map index expression or receive expression.
1289 // We use this because we may not know the right type at parse time.
1291 set_type_from_init_tuple()
1292 { this->type_from_init_tuple_
= true; }
1294 // Another special case: the init value is used only to determine
1295 // the type. This is used if the variable is defined using := with
1296 // a range clause. The init value is the range expression. The
1297 // type of the variable is the index type of the range expression
1298 // (i.e., the first value returned by a range).
1300 set_type_from_range_index()
1301 { this->type_from_range_index_
= true; }
1303 // Another special case: like set_type_from_range_index, but the
1304 // type is the value type of the range expression (i.e., the second
1305 // value returned by a range).
1307 set_type_from_range_value()
1308 { this->type_from_range_value_
= true; }
1310 // Another special case: the init value is used only to determine
1311 // the type. This is used if the variable is defined using := with
1312 // a case in a select statement. The init value is the channel.
1313 // The type of the variable is the channel's element type.
1315 set_type_from_chan_element()
1316 { this->type_from_chan_element_
= true; }
1318 // After we lower the select statement, we once again set the type
1319 // from the initialization expression.
1321 clear_type_from_chan_element()
1323 go_assert(this->type_from_chan_element_
);
1324 this->type_from_chan_element_
= false;
1327 // Note that this variable was created for a type switch clause.
1329 set_is_type_switch_var()
1330 { this->is_type_switch_var_
= true; }
1332 // Traverse the initializer expression.
1334 traverse_expression(Traverse
*, unsigned int traverse_mask
);
1336 // Determine the type of the variable if necessary.
1340 // Get the backend representation of the variable.
1342 get_backend_variable(Gogo
*, Named_object
*, const Package
*,
1343 const std::string
&);
1345 // Get the initial value of the variable as a tree. This may only
1346 // be called if has_pre_init() returns false.
1348 get_init_tree(Gogo
*, Named_object
* function
);
1350 // Return a series of statements which sets the value of the
1351 // variable in DECL. This should only be called is has_pre_init()
1352 // returns true. DECL may be NULL for a sink variable.
1354 get_init_block(Gogo
*, Named_object
* function
, tree decl
);
1356 // Export the variable.
1358 export_var(Export
*, const std::string
& name
) const;
1360 // Import a variable.
1362 import_var(Import
*, std::string
* pname
, Type
** ptype
);
1365 // The type of a tuple.
1367 type_from_tuple(Expression
*, bool) const;
1369 // The type of a range.
1371 type_from_range(Expression
*, bool, bool) const;
1373 // The element type of a channel.
1375 type_from_chan_element(Expression
*, bool) const;
1377 // The variable's type. This may be NULL if the type is set from
1380 // The initial value. This may be NULL if the variable should be
1381 // initialized to the default value for the type.
1383 // Statements to run before the init statement.
1385 // Location of variable definition.
1387 // Backend representation.
1388 Bvariable
* backend_
;
1389 // Whether this is a global variable.
1390 bool is_global_
: 1;
1391 // Whether this is a function parameter.
1392 bool is_parameter_
: 1;
1393 // Whether this is the receiver parameter of a method.
1394 bool is_receiver_
: 1;
1395 // Whether this is the varargs parameter of a function.
1396 bool is_varargs_parameter_
: 1;
1397 // Whether this variable is ever referenced.
1399 // Whether something takes the address of this variable. For a
1400 // local variable this implies that the variable has to be on the
1402 bool is_address_taken_
: 1;
1403 // Whether something takes the address of this variable such that
1404 // the address does not escape the function.
1405 bool is_non_escaping_address_taken_
: 1;
1406 // True if we have seen this variable in a traversal.
1408 // True if we have lowered the initialization expression.
1409 bool init_is_lowered_
: 1;
1410 // True if init is a tuple used to set the type.
1411 bool type_from_init_tuple_
: 1;
1412 // True if init is a range clause and the type is the index type.
1413 bool type_from_range_index_
: 1;
1414 // True if init is a range clause and the type is the value type.
1415 bool type_from_range_value_
: 1;
1416 // True if init is a channel and the type is the channel's element type.
1417 bool type_from_chan_element_
: 1;
1418 // True if this is a variable created for a type switch case.
1419 bool is_type_switch_var_
: 1;
1420 // True if we have determined types.
1421 bool determined_type_
: 1;
1424 // A variable which is really the name for a function return value, or
1427 class Result_variable
1430 Result_variable(Type
* type
, Function
* function
, int index
,
1432 : type_(type
), function_(function
), index_(index
), location_(location
),
1433 backend_(NULL
), is_address_taken_(false),
1434 is_non_escaping_address_taken_(false)
1437 // Get the type of the result variable.
1440 { return this->type_
; }
1442 // Get the function that this is associated with.
1445 { return this->function_
; }
1447 // Index in the list of function results.
1450 { return this->index_
; }
1452 // The location of the variable definition.
1455 { return this->location_
; }
1457 // Whether this variable's address is taken.
1459 is_address_taken() const
1460 { return this->is_address_taken_
; }
1462 // Note that something takes the address of this variable.
1465 { this->is_address_taken_
= true; }
1467 // Return whether the address is taken but does not escape.
1469 is_non_escaping_address_taken() const
1470 { return this->is_non_escaping_address_taken_
; }
1472 // Note that something takes the address of this variable such that
1473 // the address does not escape the function.
1475 set_non_escaping_address_taken()
1476 { this->is_non_escaping_address_taken_
= true; }
1478 // Whether this variable should live in the heap.
1481 { return this->is_address_taken_
; }
1483 // Set the function. This is used when cloning functions which call
1486 set_function(Function
* function
)
1487 { this->function_
= function
; }
1489 // Get the backend representation of the variable.
1491 get_backend_variable(Gogo
*, Named_object
*, const std::string
&);
1494 // Type of result variable.
1496 // Function with which this is associated.
1497 Function
* function_
;
1498 // Index in list of results.
1500 // Where the result variable is defined.
1502 // Backend representation.
1503 Bvariable
* backend_
;
1504 // Whether something takes the address of this variable.
1505 bool is_address_taken_
;
1506 // Whether something takes the address of this variable such that
1507 // the address does not escape the function.
1508 bool is_non_escaping_address_taken_
;
1511 // The value we keep for a named constant. This lets us hold a type
1512 // and an expression.
1514 class Named_constant
1517 Named_constant(Type
* type
, Expression
* expr
, int iota_value
,
1519 : type_(type
), expr_(expr
), iota_value_(iota_value
), location_(location
),
1525 { return this->type_
; }
1529 { return this->expr_
; }
1533 { return this->iota_value_
; }
1537 { return this->location_
; }
1539 // Whether we are lowering.
1542 { return this->lowering_
; }
1544 // Set that we are lowering.
1547 { this->lowering_
= true; }
1549 // We are no longer lowering.
1552 { this->lowering_
= false; }
1554 // Traverse the expression.
1556 traverse_expression(Traverse
*);
1558 // Determine the type of the constant if necessary.
1562 // Indicate that we found and reported an error for this constant.
1566 // Export the constant.
1568 export_const(Export
*, const std::string
& name
) const;
1570 // Import a constant.
1572 import_const(Import
*, std::string
*, Type
**, Expression
**);
1575 // The type of the constant.
1577 // The expression for the constant.
1579 // If the predeclared constant iota is used in EXPR_, this is the
1580 // value it will have. We do this because at parse time we don't
1581 // know whether the name "iota" will refer to the predeclared
1582 // constant or to something else. We put in the right value in when
1585 // The location of the definition.
1587 // Whether we are currently lowering this constant.
1591 // A type declaration.
1593 class Type_declaration
1596 Type_declaration(Location location
)
1597 : location_(location
), in_function_(NULL
), methods_(),
1598 issued_warning_(false)
1601 // Return the location.
1604 { return this->location_
; }
1606 // Return the function in which this type is declared. This will
1607 // return NULL for a type declared in global scope.
1610 { return this->in_function_
; }
1612 // Set the function in which this type is declared.
1614 set_in_function(Named_object
* f
)
1615 { this->in_function_
= f
; }
1617 // Add a method to this type. This is used when methods are defined
1620 add_method(const std::string
& name
, Function
* function
);
1622 // Add a method declaration to this type.
1624 add_method_declaration(const std::string
& name
, Package
*,
1625 Function_type
* type
, Location location
);
1627 // Return whether any methods were defined.
1629 has_methods() const;
1631 // Define methods when the real type is known.
1633 define_methods(Named_type
*);
1635 // This is called if we are trying to use this type. It returns
1636 // true if we should issue a warning.
1641 typedef std::vector
<Named_object
*> Methods
;
1643 // The location of the type declaration.
1645 // If this type is declared in a function, a pointer back to the
1646 // function in which it is defined.
1647 Named_object
* in_function_
;
1648 // Methods defined before the type is defined.
1650 // True if we have issued a warning about a use of this type
1651 // declaration when it is undefined.
1652 bool issued_warning_
;
1655 // An unknown object. These are created by the parser for forward
1656 // references to names which have not been seen before. In a correct
1657 // program, these will always point to a real definition by the end of
1658 // the parse. Because they point to another Named_object, these may
1659 // only be referenced by Unknown_expression objects.
1664 Unknown_name(Location location
)
1665 : location_(location
), real_named_object_(NULL
)
1668 // Return the location where this name was first seen.
1671 { return this->location_
; }
1673 // Return the real named object that this points to, or NULL if it
1674 // was never resolved.
1676 real_named_object() const
1677 { return this->real_named_object_
; }
1679 // Set the real named object that this points to.
1681 set_real_named_object(Named_object
* no
);
1684 // The location where this name was first seen.
1686 // The real named object when it is known.
1691 // A named object named. This is the result of a declaration. We
1692 // don't use a superclass because they all have to be handled
1700 // An uninitialized Named_object. We should never see this.
1701 NAMED_OBJECT_UNINITIALIZED
,
1702 // An erroneous name. This indicates a parse error, to avoid
1703 // later errors about undefined references.
1704 NAMED_OBJECT_ERRONEOUS
,
1705 // An unknown name. This is used for forward references. In a
1706 // correct program, these will all be resolved by the end of the
1708 NAMED_OBJECT_UNKNOWN
,
1713 // A forward type declaration.
1714 NAMED_OBJECT_TYPE_DECLARATION
,
1717 // A result variable in a function.
1718 NAMED_OBJECT_RESULT_VAR
,
1719 // The blank identifier--the special variable named _.
1723 // A forward func declaration.
1724 NAMED_OBJECT_FUNC_DECLARATION
,
1726 NAMED_OBJECT_PACKAGE
1729 // Return the classification.
1731 classification() const
1732 { return this->classification_
; }
1737 is_erroneous() const
1738 { return this->classification_
== NAMED_OBJECT_ERRONEOUS
; }
1742 { return this->classification_
== NAMED_OBJECT_UNKNOWN
; }
1746 { return this->classification_
== NAMED_OBJECT_CONST
; }
1750 { return this->classification_
== NAMED_OBJECT_TYPE
; }
1753 is_type_declaration() const
1754 { return this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
; }
1758 { return this->classification_
== NAMED_OBJECT_VAR
; }
1761 is_result_variable() const
1762 { return this->classification_
== NAMED_OBJECT_RESULT_VAR
; }
1766 { return this->classification_
== NAMED_OBJECT_SINK
; }
1770 { return this->classification_
== NAMED_OBJECT_FUNC
; }
1773 is_function_declaration() const
1774 { return this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
; }
1778 { return this->classification_
== NAMED_OBJECT_PACKAGE
; }
1782 static Named_object
*
1783 make_erroneous_name(const std::string
& name
)
1784 { return new Named_object(name
, NULL
, NAMED_OBJECT_ERRONEOUS
); }
1786 static Named_object
*
1787 make_unknown_name(const std::string
& name
, Location
);
1789 static Named_object
*
1790 make_constant(const Typed_identifier
&, const Package
*, Expression
*,
1793 static Named_object
*
1794 make_type(const std::string
&, const Package
*, Type
*, Location
);
1796 static Named_object
*
1797 make_type_declaration(const std::string
&, const Package
*, Location
);
1799 static Named_object
*
1800 make_variable(const std::string
&, const Package
*, Variable
*);
1802 static Named_object
*
1803 make_result_variable(const std::string
&, Result_variable
*);
1805 static Named_object
*
1808 static Named_object
*
1809 make_function(const std::string
&, const Package
*, Function
*);
1811 static Named_object
*
1812 make_function_declaration(const std::string
&, const Package
*, Function_type
*,
1815 static Named_object
*
1816 make_package(const std::string
& alias
, Package
* package
);
1823 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
1824 return this->u_
.unknown_value
;
1828 unknown_value() const
1830 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
1831 return this->u_
.unknown_value
;
1837 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
1838 return this->u_
.const_value
;
1841 const Named_constant
*
1844 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
1845 return this->u_
.const_value
;
1851 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
1852 return this->u_
.type_value
;
1858 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
1859 return this->u_
.type_value
;
1863 type_declaration_value()
1865 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
1866 return this->u_
.type_declaration
;
1869 const Type_declaration
*
1870 type_declaration_value() const
1872 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
1873 return this->u_
.type_declaration
;
1879 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
1880 return this->u_
.var_value
;
1886 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
1887 return this->u_
.var_value
;
1893 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
1894 return this->u_
.result_var_value
;
1897 const Result_variable
*
1898 result_var_value() const
1900 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
1901 return this->u_
.result_var_value
;
1907 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
1908 return this->u_
.func_value
;
1914 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
1915 return this->u_
.func_value
;
1918 Function_declaration
*
1919 func_declaration_value()
1921 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
1922 return this->u_
.func_declaration_value
;
1925 const Function_declaration
*
1926 func_declaration_value() const
1928 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
1929 return this->u_
.func_declaration_value
;
1935 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
1936 return this->u_
.package_value
;
1940 package_value() const
1942 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
1943 return this->u_
.package_value
;
1948 { return this->name_
; }
1950 // Return the name to use in an error message. The difference is
1951 // that if this Named_object is defined in a different package, this
1952 // will return PACKAGE.NAME.
1954 message_name() const;
1958 { return this->package_
; }
1960 // Resolve an unknown value if possible. This returns the same
1961 // Named_object or a new one.
1965 Named_object
* ret
= this;
1966 if (this->is_unknown())
1968 Named_object
* r
= this->unknown_value()->real_named_object();
1978 const Named_object
* ret
= this;
1979 if (this->is_unknown())
1981 const Named_object
* r
= this->unknown_value()->real_named_object();
1988 // The location where this object was defined or referenced.
1992 // Convert a variable to the backend representation.
1994 get_backend_variable(Gogo
*, Named_object
* function
);
1996 // Return a tree for the external identifier for this object.
2000 // Return a tree representing this object.
2002 get_tree(Gogo
*, Named_object
* function
);
2004 // Define a type declaration.
2006 set_type_value(Named_type
*);
2008 // Define a function declaration.
2010 set_function_value(Function
*);
2012 // Declare an unknown name as a type declaration.
2016 // Export this object.
2018 export_named_object(Export
*) const;
2021 Named_object(const std::string
&, const Package
*, Classification
);
2023 // The name of the object.
2025 // The package that this object is in. This is NULL if it is in the
2026 // file we are compiling.
2027 const Package
* package_
;
2028 // The type of object this is.
2029 Classification classification_
;
2033 Unknown_name
* unknown_value
;
2034 Named_constant
* const_value
;
2035 Named_type
* type_value
;
2036 Type_declaration
* type_declaration
;
2037 Variable
* var_value
;
2038 Result_variable
* result_var_value
;
2039 Function
* func_value
;
2040 Function_declaration
* func_declaration_value
;
2041 Package
* package_value
;
2043 // The DECL tree for this object if we have already converted it.
2047 // A binding contour. This binds names to objects.
2052 // Type for mapping from names to objects.
2053 typedef Unordered_map(std::string
, Named_object
*) Contour
;
2055 Bindings(Bindings
* enclosing
);
2057 // Add an erroneous name.
2059 add_erroneous_name(const std::string
& name
)
2060 { return this->add_named_object(Named_object::make_erroneous_name(name
)); }
2062 // Add an unknown name.
2064 add_unknown_name(const std::string
& name
, Location location
)
2066 return this->add_named_object(Named_object::make_unknown_name(name
,
2072 add_constant(const Typed_identifier
& tid
, const Package
* package
,
2073 Expression
* expr
, int iota_value
)
2075 return this->add_named_object(Named_object::make_constant(tid
, package
,
2082 add_type(const std::string
& name
, const Package
* package
, Type
* type
,
2085 return this->add_named_object(Named_object::make_type(name
, package
, type
,
2089 // Add a named type. This is used for builtin types, and to add an
2090 // imported type to the global scope.
2092 add_named_type(Named_type
* named_type
);
2094 // Add a type declaration.
2096 add_type_declaration(const std::string
& name
, const Package
* package
,
2099 Named_object
* no
= Named_object::make_type_declaration(name
, package
,
2101 return this->add_named_object(no
);
2106 add_variable(const std::string
& name
, const Package
* package
,
2109 return this->add_named_object(Named_object::make_variable(name
, package
,
2113 // Add a result variable.
2115 add_result_variable(const std::string
& name
, Result_variable
* result
)
2117 return this->add_named_object(Named_object::make_result_variable(name
,
2123 add_function(const std::string
& name
, const Package
*, Function
* function
);
2125 // Add a function declaration.
2127 add_function_declaration(const std::string
& name
, const Package
* package
,
2128 Function_type
* type
, Location location
);
2130 // Add a package. The location is the location of the import
2133 add_package(const std::string
& alias
, Package
* package
)
2135 Named_object
* no
= Named_object::make_package(alias
, package
);
2136 return this->add_named_object(no
);
2139 // Define a type which was already declared.
2141 define_type(Named_object
*, Named_type
*);
2143 // Add a method to the list of objects. This is not added to the
2146 add_method(Named_object
*);
2148 // Add a named object to this binding.
2150 add_named_object(Named_object
* no
)
2151 { return this->add_named_object_to_contour(&this->bindings_
, no
); }
2153 // Clear all names in file scope from the bindings.
2157 // Look up a name in this binding contour and in any enclosing
2158 // binding contours. This returns NULL if the name is not found.
2160 lookup(const std::string
&) const;
2162 // Look up a name in this binding contour without looking in any
2163 // enclosing binding contours. Returns NULL if the name is not found.
2165 lookup_local(const std::string
&) const;
2169 remove_binding(Named_object
*);
2171 // Mark all variables as used. This is used for some types of parse
2176 // Traverse the tree. See the Traverse class.
2178 traverse(Traverse
*, bool is_global
);
2180 // Iterate over definitions. This does not include things which
2181 // were only declared.
2183 typedef std::vector
<Named_object
*>::const_iterator
2184 const_definitions_iterator
;
2186 const_definitions_iterator
2187 begin_definitions() const
2188 { return this->named_objects_
.begin(); }
2190 const_definitions_iterator
2191 end_definitions() const
2192 { return this->named_objects_
.end(); }
2194 // Return the number of definitions.
2196 size_definitions() const
2197 { return this->named_objects_
.size(); }
2199 // Return whether there are no definitions.
2201 empty_definitions() const
2202 { return this->named_objects_
.empty(); }
2204 // Iterate over declarations. This is everything that has been
2205 // declared, which includes everything which has been defined.
2207 typedef Contour::const_iterator const_declarations_iterator
;
2209 const_declarations_iterator
2210 begin_declarations() const
2211 { return this->bindings_
.begin(); }
2213 const_declarations_iterator
2214 end_declarations() const
2215 { return this->bindings_
.end(); }
2217 // Return the number of declarations.
2219 size_declarations() const
2220 { return this->bindings_
.size(); }
2222 // Return whether there are no declarations.
2224 empty_declarations() const
2225 { return this->bindings_
.empty(); }
2227 // Return the first declaration.
2230 { return this->bindings_
.empty() ? NULL
: this->bindings_
.begin()->second
; }
2234 add_named_object_to_contour(Contour
*, Named_object
*);
2237 new_definition(Named_object
*, Named_object
*);
2239 // Enclosing bindings.
2240 Bindings
* enclosing_
;
2241 // The list of objects.
2242 std::vector
<Named_object
*> named_objects_
;
2243 // The mapping from names to objects.
2252 Label(const std::string
& name
)
2253 : name_(name
), location_(Linemap::unknown_location()), snapshot_(NULL
),
2254 refs_(), is_used_(false), blabel_(NULL
)
2257 // Return the label's name.
2260 { return this->name_
; }
2262 // Return whether the label has been defined.
2265 { return !Linemap::is_unknown_location(this->location_
); }
2267 // Return whether the label has been used.
2270 { return this->is_used_
; }
2272 // Record that the label is used.
2275 { this->is_used_
= true; }
2277 // Return the location of the definition.
2280 { return this->location_
; }
2282 // Return the bindings snapshot.
2285 { return this->snapshot_
; }
2287 // Add a snapshot of a goto which refers to this label.
2289 add_snapshot_ref(Bindings_snapshot
* snapshot
)
2291 go_assert(Linemap::is_unknown_location(this->location_
));
2292 this->refs_
.push_back(snapshot
);
2295 // Return the list of snapshots of goto statements which refer to
2297 const std::vector
<Bindings_snapshot
*>&
2299 { return this->refs_
; }
2301 // Clear the references.
2305 // Define the label at LOCATION with the given bindings snapshot.
2307 define(Location location
, Bindings_snapshot
* snapshot
)
2309 go_assert(Linemap::is_unknown_location(this->location_
)
2310 && this->snapshot_
== NULL
);
2311 this->location_
= location
;
2312 this->snapshot_
= snapshot
;
2315 // Return the backend representation for this label.
2317 get_backend_label(Translate_context
*);
2319 // Return an expression for the address of this label. This is used
2320 // to get the return address of a deferred function to see whether
2321 // the function may call recover.
2323 get_addr(Translate_context
*, Location location
);
2326 // The name of the label.
2328 // The location of the definition. This is 0 if the label has not
2329 // yet been defined.
2331 // A snapshot of the set of bindings defined at this label, used to
2332 // issue errors about invalid goto statements.
2333 Bindings_snapshot
* snapshot_
;
2334 // A list of snapshots of goto statements which refer to this label.
2335 std::vector
<Bindings_snapshot
*> refs_
;
2336 // Whether the label has been used.
2338 // The backend representation.
2342 // An unnamed label. These are used when lowering loops.
2347 Unnamed_label(Location location
)
2348 : location_(location
), blabel_(NULL
)
2351 // Get the location where the label is defined.
2354 { return this->location_
; }
2356 // Set the location where the label is defined.
2358 set_location(Location location
)
2359 { this->location_
= location
; }
2361 // Return a statement which defines this label.
2363 get_definition(Translate_context
*);
2365 // Return a goto to this label from LOCATION.
2367 get_goto(Translate_context
*, Location location
);
2370 // Return the backend representation.
2372 get_blabel(Translate_context
*);
2374 // The location where the label is defined.
2376 // The backend representation of this label.
2380 // An imported package.
2385 Package(const std::string
& name
, const std::string
& unique_prefix
,
2388 // The real name of this package. This may be different from the
2389 // name in the associated Named_object if the import statement used
2393 { return this->name_
; }
2395 // Return the location of the import statement.
2398 { return this->location_
; }
2400 // Get the unique prefix used for all symbols exported from this
2403 unique_prefix() const
2405 go_assert(!this->unique_prefix_
.empty());
2406 return this->unique_prefix_
;
2409 // The priority of this package. The init function of packages with
2410 // lower priority must be run before the init function of packages
2411 // with higher priority.
2414 { return this->priority_
; }
2416 // Set the priority.
2418 set_priority(int priority
);
2420 // Return the bindings.
2423 { return this->bindings_
; }
2425 // Whether some symbol from the package was used.
2428 { return this->used_
; }
2430 // Note that some symbol from this package was used.
2433 { this->used_
= true; }
2435 // Clear the used field for the next file.
2438 { this->used_
= false; }
2440 // Whether this package was imported in the current file.
2443 { return this->is_imported_
; }
2445 // Note that this package was imported in the current file.
2448 { this->is_imported_
= true; }
2450 // Clear the imported field for the next file.
2453 { this->is_imported_
= false; }
2455 // Whether this package was imported with a name of "_".
2457 uses_sink_alias() const
2458 { return this->uses_sink_alias_
; }
2460 // Note that this package was imported with a name of "_".
2462 set_uses_sink_alias()
2463 { this->uses_sink_alias_
= true; }
2465 // Clear the sink alias field for the next file.
2467 clear_uses_sink_alias()
2468 { this->uses_sink_alias_
= false; }
2470 // Look up a name in the package. Returns NULL if the name is not
2473 lookup(const std::string
& name
) const
2474 { return this->bindings_
->lookup(name
); }
2476 // Set the location of the package. This is used if it is seen in a
2477 // different import before it is really imported.
2479 set_location(Location location
)
2480 { this->location_
= location
; }
2482 // Add a constant to the package.
2484 add_constant(const Typed_identifier
& tid
, Expression
* expr
)
2485 { return this->bindings_
->add_constant(tid
, this, expr
, 0); }
2487 // Add a type to the package.
2489 add_type(const std::string
& name
, Type
* type
, Location location
)
2490 { return this->bindings_
->add_type(name
, this, type
, location
); }
2492 // Add a type declaration to the package.
2494 add_type_declaration(const std::string
& name
, Location location
)
2495 { return this->bindings_
->add_type_declaration(name
, this, location
); }
2497 // Add a variable to the package.
2499 add_variable(const std::string
& name
, Variable
* variable
)
2500 { return this->bindings_
->add_variable(name
, this, variable
); }
2502 // Add a function declaration to the package.
2504 add_function_declaration(const std::string
& name
, Function_type
* type
,
2506 { return this->bindings_
->add_function_declaration(name
, this, type
, loc
); }
2508 // Determine types of constants.
2513 // The real name of this package.
2515 // The unique prefix for all exported global symbols.
2516 std::string unique_prefix_
;
2517 // The names in this package.
2518 Bindings
* bindings_
;
2519 // The priority of this package. A package has a priority higher
2520 // than the priority of all of the packages that it imports. This
2521 // is used to run init functions in the right order.
2523 // The location of the import statement.
2525 // True if some name from this package was used. This is mutable
2526 // because we can use a package even if we have a const pointer to
2529 // True if this package was imported in the current file.
2531 // True if this package was imported with a name of "_".
2532 bool uses_sink_alias_
;
2535 // Return codes for the traversal functions. This is not an enum
2536 // because we want to be able to declare traversal functions in other
2537 // header files without including this one.
2539 // Continue traversal as usual.
2540 const int TRAVERSE_CONTINUE
= -1;
2543 const int TRAVERSE_EXIT
= 0;
2545 // Continue traversal, but skip components of the current object.
2546 // E.g., if this is returned by Traverse::statement, we do not
2547 // traverse the expressions in the statement even if
2548 // traverse_expressions is set in the traverse_mask.
2549 const int TRAVERSE_SKIP_COMPONENTS
= 1;
2551 // This class is used when traversing the parse tree. The caller uses
2552 // a subclass which overrides functions as desired.
2557 // These bitmasks say what to traverse.
2558 static const unsigned int traverse_variables
= 0x1;
2559 static const unsigned int traverse_constants
= 0x2;
2560 static const unsigned int traverse_functions
= 0x4;
2561 static const unsigned int traverse_blocks
= 0x8;
2562 static const unsigned int traverse_statements
= 0x10;
2563 static const unsigned int traverse_expressions
= 0x20;
2564 static const unsigned int traverse_types
= 0x40;
2566 Traverse(unsigned int traverse_mask
)
2567 : traverse_mask_(traverse_mask
), types_seen_(NULL
), expressions_seen_(NULL
)
2570 virtual ~Traverse();
2572 // The bitmask of what to traverse.
2574 traverse_mask() const
2575 { return this->traverse_mask_
; }
2577 // Record that we are going to traverse a type. This returns true
2578 // if the type has already been seen in this traversal. This is
2579 // required because types, unlike expressions, can form a circular
2582 remember_type(const Type
*);
2584 // Record that we are going to see an expression. This returns true
2585 // if the expression has already been seen in this traversal. This
2586 // is only needed for cases where multiple expressions can point to
2589 remember_expression(const Expression
*);
2591 // These functions return one of the TRAVERSE codes defined above.
2593 // If traverse_variables is set in the mask, this is called for
2594 // every variable in the tree.
2596 variable(Named_object
*);
2598 // If traverse_constants is set in the mask, this is called for
2599 // every named constant in the tree. The bool parameter is true for
2600 // a global constant.
2602 constant(Named_object
*, bool);
2604 // If traverse_functions is set in the mask, this is called for
2605 // every function in the tree.
2607 function(Named_object
*);
2609 // If traverse_blocks is set in the mask, this is called for every
2610 // block in the tree.
2614 // If traverse_statements is set in the mask, this is called for
2615 // every statement in the tree.
2617 statement(Block
*, size_t* index
, Statement
*);
2619 // If traverse_expressions is set in the mask, this is called for
2620 // every expression in the tree.
2622 expression(Expression
**);
2624 // If traverse_types is set in the mask, this is called for every
2625 // type in the tree.
2630 // A hash table for types we have seen during this traversal. Note
2631 // that this uses the default hash functions for pointers rather
2632 // than Type_hash_identical and Type_identical. This is because for
2633 // traversal we care about seeing a specific type structure. If
2634 // there are two separate instances of identical types, we want to
2636 typedef Unordered_set(const Type
*) Types_seen
;
2638 typedef Unordered_set(const Expression
*) Expressions_seen
;
2640 // Bitmask of what sort of objects to traverse.
2641 unsigned int traverse_mask_
;
2642 // Types which have been seen in this traversal.
2643 Types_seen
* types_seen_
;
2644 // Expressions which have been seen in this traversal.
2645 Expressions_seen
* expressions_seen_
;
2648 // A class which makes it easier to insert new statements before the
2649 // current statement during a traversal.
2651 class Statement_inserter
2654 // Empty constructor.
2655 Statement_inserter()
2656 : block_(NULL
), pindex_(NULL
), gogo_(NULL
), var_(NULL
)
2659 // Constructor for a statement in a block.
2660 Statement_inserter(Block
* block
, size_t *pindex
)
2661 : block_(block
), pindex_(pindex
), gogo_(NULL
), var_(NULL
)
2664 // Constructor for a global variable.
2665 Statement_inserter(Gogo
* gogo
, Variable
* var
)
2666 : block_(NULL
), pindex_(NULL
), gogo_(gogo
), var_(var
)
2667 { go_assert(var
->is_global()); }
2669 // We use the default copy constructor and assignment operator.
2671 // Insert S before the statement we are traversing, or before the
2672 // initialization expression of a global variable.
2674 insert(Statement
* s
);
2677 // The block that the statement is in.
2679 // The index of the statement that we are traversing.
2681 // The IR, needed when looking at an initializer expression for a
2684 // The global variable, when looking at an initializer expression.
2688 // When translating the gogo IR into the backend data structure, this
2689 // is the context we pass down the blocks and statements.
2691 class Translate_context
2694 Translate_context(Gogo
* gogo
, Named_object
* function
, Block
* block
,
2696 : gogo_(gogo
), backend_(gogo
->backend()), function_(function
),
2697 block_(block
), bblock_(bblock
), is_const_(false)
2704 { return this->gogo_
; }
2708 { return this->backend_
; }
2712 { return this->function_
; }
2716 { return this->block_
; }
2720 { return this->bblock_
; }
2724 { return this->is_const_
; }
2726 // Make a constant context.
2729 { this->is_const_
= true; }
2732 // The IR for the entire compilation unit.
2734 // The generator for the backend data structures.
2736 // The function we are currently translating. NULL if not in a
2737 // function, e.g., the initializer of a global variable.
2738 Named_object
* function_
;
2739 // The block we are currently translating. NULL if not in a
2742 // The backend representation of the current block. NULL if block_
2745 // Whether this is being evaluated in a constant context. This is
2746 // used for type descriptor initializers.
2750 // Runtime error codes. These must match the values in
2751 // libgo/runtime/go-runtime-error.c.
2753 // Slice index out of bounds: negative or larger than the length of
2755 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
= 0;
2757 // Array index out of bounds.
2758 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
= 1;
2760 // String index out of bounds.
2761 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
= 2;
2763 // Slice slice out of bounds: negative or larger than the length of
2764 // the slice or high bound less than low bound.
2765 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
= 3;
2767 // Array slice out of bounds.
2768 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
= 4;
2770 // String slice out of bounds.
2771 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
= 5;
2773 // Dereference of nil pointer. This is used when there is a
2774 // dereference of a pointer to a very large struct or array, to ensure
2775 // that a gigantic array is not used a proxy to access random memory
2777 static const int RUNTIME_ERROR_NIL_DEREFERENCE
= 6;
2779 // Slice length or capacity out of bounds in make: negative or
2780 // overflow or length greater than capacity.
2781 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
= 7;
2783 // Map capacity out of bounds in make: negative or overflow.
2784 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
= 8;
2786 // Channel capacity out of bounds in make: negative or overflow.
2787 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
= 9;
2789 // This is used by some of the langhooks.
2790 extern Gogo
* go_get_gogo();
2792 // Whether we have seen any errors. FIXME: Replace with a backend
2794 extern bool saw_errors();
2796 #endif // !defined(GO_GOGO_H)