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 unknown name. This is used while parsing. The name
271 // must be resolved by the end of the parse. Unknown names are
272 // always added at the package level.
274 add_unknown_name(const std::string
& name
, Location
);
276 // Declare a function.
278 declare_function(const std::string
&, Function_type
*, Location
);
280 // Declare a function at the package level. This is used for
281 // functions generated for a type.
283 declare_package_function(const std::string
&, Function_type
*, Location
);
287 add_label_definition(const std::string
&, Location
);
289 // Add a label reference. ISSUE_GOTO_ERRORS is true if we should
290 // report errors for a goto from the current location to the label
293 add_label_reference(const std::string
&, Location
,
294 bool issue_goto_errors
);
296 // Return a snapshot of the current binding state.
298 bindings_snapshot(Location
);
300 // Add a statement to the current block.
302 add_statement(Statement
*);
304 // Add a block to the current block.
306 add_block(Block
*, Location
);
310 add_constant(const Typed_identifier
&, Expression
*, int iota_value
);
314 add_type(const std::string
&, Type
*, Location
);
316 // Add a named type. This is used for builtin types, and to add an
317 // imported type to the global scope.
319 add_named_type(Named_type
*);
323 declare_type(const std::string
&, Location
);
325 // Declare a type at the package level. This is used when the
326 // parser sees an unknown name where a type name is required.
328 declare_package_type(const std::string
&, Location
);
330 // Define a type which was already declared.
332 define_type(Named_object
*, Named_type
*);
336 add_variable(const std::string
&, Variable
*);
338 // Add a sink--a reference to the blank identifier _.
342 // Add a named object to the current namespace. This is used for
343 // import . "package".
345 add_named_object(Named_object
*);
347 // Mark all local variables in current bindings as used. This is
348 // used when there is a parse error to avoid useless errors.
352 // Return a name to use for a thunk function. A thunk function is
353 // one we create during the compilation, for a go statement or a
354 // defer statement or a method expression.
358 // Return whether an object is a thunk.
360 is_thunk(const Named_object
*);
362 // Note that we've seen an interface type. This is used to build
363 // all required interface method tables.
365 record_interface_type(Interface_type
*);
367 // Note that we need an initialization function.
370 { this->need_init_fn_
= true; }
372 // Clear out all names in file scope. This is called when we start
373 // parsing a new file.
377 // Queue up a type-specific function to be written out. This is
378 // used when a type-specific function is needed when not at the top
381 queue_specific_type_function(Type
* type
, Named_type
* name
,
382 const std::string
& hash_name
,
383 Function_type
* hash_fntype
,
384 const std::string
& equal_name
,
385 Function_type
* equal_fntype
);
387 // Write out queued specific type functions.
389 write_specific_type_functions();
391 // Traverse the tree. See the Traverse class.
395 // Define the predeclared global names.
397 define_global_names();
399 // Verify and complete all types.
403 // Lower the parse tree.
407 // Lower all the statements in a block.
409 lower_block(Named_object
* function
, Block
*);
411 // Lower an expression.
413 lower_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
417 lower_constant(Named_object
*);
419 // Finalize the method lists and build stub methods for named types.
423 // Work out the types to use for unspecified variables and
428 // Type check the program.
432 // Check the types in a single block. This is used for complicated
435 check_types_in_block(Block
*);
437 // Check for return statements.
439 check_return_statements();
445 // Add an import control function for an imported package to the
448 add_import_init_fn(const std::string
& package_name
,
449 const std::string
& init_name
, int prio
);
451 // Turn short-cut operators (&&, ||) into explicit if statements.
455 // Use temporary variables to force order of evaluation.
459 // Build thunks for functions which call recover.
461 build_recover_thunks();
463 // Simplify statements which might use thunks: go and defer
466 simplify_thunk_statements();
468 // Dump AST if -fgo-dump-ast is set
470 dump_ast(const char* basename
);
472 // Convert named types to the backend representation.
474 convert_named_types();
476 // Convert named types in a list of bindings.
478 convert_named_types_in_bindings(Bindings
*);
480 // True if named types have been converted to the backend
483 named_types_are_converted() const
484 { return this->named_types_are_converted_
; }
486 // Write out the global values.
490 // Create trees for implicit builtin functions.
492 define_builtin_function_trees();
494 // Build a call to a builtin function. PDECL should point to a NULL
495 // initialized static pointer which will hold the fndecl. NAME is
496 // the name of the function. NARGS is the number of arguments.
497 // RETTYPE is the return type. It is followed by NARGS pairs of
498 // type and argument (both trees).
500 call_builtin(tree
* pdecl
, Location
, const char* name
, int nargs
,
503 // Build a call to the runtime error function.
505 runtime_error(int code
, Location
);
507 // Build a builtin struct with a list of fields.
509 builtin_struct(tree
* ptype
, const char* struct_name
, tree struct_type
,
512 // Mark a function declaration as a builtin library function.
514 mark_fndecl_as_builtin_library(tree fndecl
);
516 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
517 // the slice. VALUES points to the values. COUNT is the size,
518 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
521 slice_constructor(tree slice_type_tree
, tree values
, tree count
,
524 // Build required interface method tables.
526 build_interface_method_tables();
528 // Build an interface method table for a type: a list of function
529 // pointers, one for each interface method. This returns a decl.
531 interface_method_table_for_type(const Interface_type
*, Named_type
*,
534 // Return a tree which allocate SIZE bytes to hold values of type
537 allocate_memory(Type
*type
, tree size
, Location
);
539 // Return a type to use for pointer to const char.
541 const_char_pointer_type_tree();
543 // Build a string constant with the right type.
545 string_constant_tree(const std::string
&);
547 // Build a Go string constant. This returns a pointer to the
550 go_string_constant_tree(const std::string
&);
552 // Receive a value from a channel.
554 receive_from_channel(tree type_tree
, tree type_descriptor_tree
, tree channel
,
557 // Make a trampoline which calls FNADDR passing CLOSURE.
559 make_trampoline(tree fnaddr
, tree closure
, Location
);
562 // During parsing, we keep a stack of functions. Each function on
563 // the stack is one that we are currently parsing. For each
564 // function, we keep track of the current stack of blocks.
568 Named_object
* function
;
569 // The stack of active blocks in the function.
570 std::vector
<Block
*> blocks
;
573 // The stack of functions.
574 typedef std::vector
<Open_function
> Open_functions
;
576 // Set up the built-in unsafe package.
578 import_unsafe(const std::string
&, bool is_exported
, Location
);
580 // Add a new imported package.
582 add_package(const std::string
& real_name
, const std::string
& alias
,
583 const std::string
& unique_prefix
, Location location
);
585 // Return the current binding contour.
590 current_bindings() const;
592 // Get the name of the magic initialization function.
596 // Get the decl for the magic initialization function.
598 initialization_function_decl();
600 // Write the magic initialization function.
602 write_initialization_function(tree fndecl
, tree init_stmt_list
);
604 // Initialize imported packages.
608 // Register variables with the garbage collector.
610 register_gc_vars(const std::vector
<Named_object
*>&, tree
*);
612 // Build a pointer to a Go string constant. This returns a pointer
615 ptr_go_string_constant_tree(const std::string
&);
617 // Return the type of a trampoline.
619 trampoline_type_tree();
621 // Type used to map import names to packages.
622 typedef std::map
<std::string
, Package
*> Imports
;
624 // Type used to map package names to packages.
625 typedef std::map
<std::string
, Package
*> Packages
;
627 // Type used to map special names in the sys package.
628 typedef std::map
<std::string
, std::string
> Sys_names
;
630 // Type used to queue writing a type specific function.
631 struct Specific_type_function
635 std::string hash_name
;
636 Function_type
* hash_fntype
;
637 std::string equal_name
;
638 Function_type
* equal_fntype
;
640 Specific_type_function(Type
* atype
, Named_type
* aname
,
641 const std::string
& ahash_name
,
642 Function_type
* ahash_fntype
,
643 const std::string
& aequal_name
,
644 Function_type
* aequal_fntype
)
645 : type(atype
), name(aname
), hash_name(ahash_name
),
646 hash_fntype(ahash_fntype
), equal_name(aequal_name
),
647 equal_fntype(aequal_fntype
)
651 // The backend generator.
653 // The object used to keep track of file names and line numbers.
655 // The package we are compiling.
657 // The list of currently open functions during parsing.
658 Open_functions functions_
;
659 // The global binding contour. This includes the builtin functions
660 // and the package we are compiling.
662 // Mapping from import file names to packages.
664 // Whether the magic unsafe package was imported.
665 bool imported_unsafe_
;
666 // Mapping from package names we have seen to packages. This does
667 // not include the package we are compiling.
669 // The functions named "init", if there are any.
670 std::vector
<Named_object
*> init_functions_
;
671 // Whether we need a magic initialization function.
673 // The name of the magic initialization function.
674 std::string init_fn_name_
;
675 // A list of import control variables for packages that we import.
676 std::set
<Import_init
> imported_init_fns_
;
677 // The unique prefix used for all global symbols.
678 std::string unique_prefix_
;
679 // Whether an explicit unique prefix was set by -fgo-prefix.
680 bool unique_prefix_specified_
;
681 // A list of interface types defined while parsing.
682 std::vector
<Interface_type
*> interface_types_
;
683 // Type specific functions to write out.
684 std::vector
<Specific_type_function
*> specific_type_functions_
;
685 // Whether we are done writing out specific type functions.
686 bool specific_type_functions_are_written_
;
687 // Whether named types have been converted.
688 bool named_types_are_converted_
;
691 // A block of statements.
696 Block(Block
* enclosing
, Location
);
698 // Return the enclosing block.
701 { return this->enclosing_
; }
703 // Return the bindings of the block.
706 { return this->bindings_
; }
710 { return this->bindings_
; }
712 // Look at the block's statements.
713 const std::vector
<Statement
*>*
715 { return &this->statements_
; }
717 // Return the start location. This is normally the location of the
718 // left curly brace which starts the block.
720 start_location() const
721 { return this->start_location_
; }
723 // Return the end location. This is normally the location of the
724 // right curly brace which ends the block.
727 { return this->end_location_
; }
729 // Add a statement to the block.
731 add_statement(Statement
*);
733 // Add a statement to the front of the block.
735 add_statement_at_front(Statement
*);
737 // Replace a statement in a block.
739 replace_statement(size_t index
, Statement
*);
741 // Add a Statement before statement number INDEX.
743 insert_statement_before(size_t index
, Statement
*);
745 // Add a Statement after statement number INDEX.
747 insert_statement_after(size_t index
, Statement
*);
749 // Set the end location of the block.
751 set_end_location(Location location
)
752 { this->end_location_
= location
; }
754 // Traverse the tree.
758 // Set final types for unspecified variables and constants.
762 // Return true if execution of this block may fall through to the
765 may_fall_through() const;
767 // Convert the block to the backend representation.
769 get_backend(Translate_context
*);
771 // Iterate over statements.
773 typedef std::vector
<Statement
*>::iterator iterator
;
777 { return this->statements_
.begin(); }
781 { return this->statements_
.end(); }
786 // Statements in the block.
787 std::vector
<Statement
*> statements_
;
790 // Location of start of block.
791 Location start_location_
;
792 // Location of end of block.
793 Location end_location_
;
801 Function(Function_type
* type
, Function
*, Block
*, Location
);
803 // Return the function's type.
806 { return this->type_
; }
808 // Return the enclosing function if there is one.
811 { return this->enclosing_
; }
813 // Set the enclosing function. This is used when building thunks
814 // for functions which call recover.
816 set_enclosing(Function
* enclosing
)
818 go_assert(this->enclosing_
== NULL
);
819 this->enclosing_
= enclosing
;
822 // The result variables.
823 typedef std::vector
<Named_object
*> Results
;
825 // Create the result variables in the outer block.
827 create_result_variables(Gogo
*);
829 // Update the named result variables when cloning a function which
832 update_result_variables();
834 // Return the result variables.
837 { return this->results_
; }
839 // Whether the result variables have names.
841 results_are_named() const
842 { return this->results_are_named_
; }
844 // Add a new field to the closure variable.
846 add_closure_field(Named_object
* var
, Location loc
)
847 { this->closure_fields_
.push_back(std::make_pair(var
, loc
)); }
849 // Whether this function needs a closure.
851 needs_closure() const
852 { return !this->closure_fields_
.empty(); }
854 // Return the closure variable, creating it if necessary. This is
855 // passed to the function as a static chain parameter.
859 // Set the closure variable. This is used when building thunks for
860 // functions which call recover.
862 set_closure_var(Named_object
* v
)
864 go_assert(this->closure_var_
== NULL
);
865 this->closure_var_
= v
;
868 // Return the variable for a reference to field INDEX in the closure
871 enclosing_var(unsigned int index
)
873 go_assert(index
< this->closure_fields_
.size());
874 return closure_fields_
[index
].first
;
877 // Set the type of the closure variable if there is one.
881 // Get the block of statements associated with the function.
884 { return this->block_
; }
886 // Get the location of the start of the function.
889 { return this->location_
; }
891 // Return whether this function is actually a method.
895 // Add a label definition to the function.
897 add_label_definition(Gogo
*, const std::string
& label_name
, Location
);
899 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
900 // if we should report errors for a goto from the current location
901 // to the label location.
903 add_label_reference(Gogo
*, const std::string
& label_name
,
904 Location
, bool issue_goto_errors
);
906 // Warn about labels that are defined but not used.
908 check_labels() const;
910 // Whether this function calls the predeclared recover function.
912 calls_recover() const
913 { return this->calls_recover_
; }
915 // Record that this function calls the predeclared recover function.
916 // This is set during the lowering pass.
919 { this->calls_recover_
= true; }
921 // Whether this is a recover thunk function.
923 is_recover_thunk() const
924 { return this->is_recover_thunk_
; }
926 // Record that this is a thunk built for a function which calls
929 set_is_recover_thunk()
930 { this->is_recover_thunk_
= true; }
932 // Whether this function already has a recover thunk.
934 has_recover_thunk() const
935 { return this->has_recover_thunk_
; }
937 // Record that this function already has a recover thunk.
939 set_has_recover_thunk()
940 { this->has_recover_thunk_
= true; }
942 // Swap with another function. Used only for the thunk which calls
945 swap_for_recover(Function
*);
947 // Traverse the tree.
951 // Determine types in the function.
955 // Return the function's decl given an identifier.
957 get_or_make_decl(Gogo
*, Named_object
*, tree id
);
959 // Return the function's decl after it has been built.
963 go_assert(this->fndecl_
!= NULL
);
964 return this->fndecl_
;
967 // Set the function decl to hold a tree of the function code.
969 build_tree(Gogo
*, Named_object
*);
971 // Get the value to return when not explicitly specified. May also
972 // add statements to execute first to STMT_LIST.
974 return_value(Gogo
*, Named_object
*, Location
, tree
* stmt_list
) const;
976 // Get a tree for the variable holding the defer stack.
978 defer_stack(Location
);
980 // Export the function.
982 export_func(Export
*, const std::string
& name
) const;
984 // Export a function with a type.
986 export_func_with_type(Export
*, const std::string
& name
,
987 const Function_type
*);
989 // Import a function.
991 import_func(Import
*, std::string
* pname
, Typed_identifier
** receiver
,
992 Typed_identifier_list
** pparameters
,
993 Typed_identifier_list
** presults
, bool* is_varargs
);
996 // Type for mapping from label names to Label objects.
997 typedef Unordered_map(std::string
, Label
*) Labels
;
1000 make_receiver_parm_decl(Gogo
*, Named_object
*, tree
);
1003 copy_parm_to_heap(Gogo
*, Named_object
*, tree
);
1006 build_defer_wrapper(Gogo
*, Named_object
*, tree
*, tree
*);
1008 typedef std::vector
<std::pair
<Named_object
*,
1009 Location
> > Closure_fields
;
1011 // The function's type.
1012 Function_type
* type_
;
1013 // The enclosing function. This is NULL when there isn't one, which
1014 // is the normal case.
1015 Function
* enclosing_
;
1016 // The result variables, if any.
1018 // If there is a closure, this is the list of variables which appear
1019 // in the closure. This is created by the parser, and then resolved
1020 // to a real type when we lower parse trees.
1021 Closure_fields closure_fields_
;
1022 // The closure variable, passed as a parameter using the static
1023 // chain parameter. Normally NULL.
1024 Named_object
* closure_var_
;
1025 // The outer block of statements in the function.
1027 // The source location of the start of the function.
1029 // Labels defined or referenced in the function.
1031 // The function decl.
1033 // The defer stack variable. A pointer to this variable is used to
1034 // distinguish the defer stack for one function from another. This
1035 // is NULL unless we actually need a defer stack.
1036 Temporary_statement
* defer_stack_
;
1037 // True if the result variables are named.
1038 bool results_are_named_
;
1039 // True if this function calls the predeclared recover function.
1040 bool calls_recover_
;
1041 // True if this a thunk built for a function which calls recover.
1042 bool is_recover_thunk_
;
1043 // True if this function already has a recover thunk.
1044 bool has_recover_thunk_
;
1047 // A snapshot of the current binding state.
1049 class Bindings_snapshot
1052 Bindings_snapshot(const Block
*, Location
);
1054 // Report any errors appropriate for a goto from the current binding
1055 // state of B to this one.
1057 check_goto_from(const Block
* b
, Location
);
1059 // Report any errors appropriate for a goto from this binding state
1060 // to the current state of B.
1062 check_goto_to(const Block
* b
);
1066 check_goto_block(Location
, const Block
*, const Block
*, size_t*);
1069 check_goto_defs(Location
, const Block
*, size_t, size_t);
1071 // The current block.
1072 const Block
* block_
;
1073 // The number of names currently defined in each open block.
1074 // Element 0 is this->block_, element 1 is
1075 // this->block_->enclosing(), etc.
1076 std::vector
<size_t> counts_
;
1077 // The location where this snapshot was taken.
1081 // A function declaration.
1083 class Function_declaration
1086 Function_declaration(Function_type
* fntype
, Location location
)
1087 : fntype_(fntype
), location_(location
), asm_name_(), fndecl_(NULL
)
1092 { return this->fntype_
; }
1096 { return this->location_
; }
1100 { return this->asm_name_
; }
1102 // Set the assembler name.
1104 set_asm_name(const std::string
& asm_name
)
1105 { this->asm_name_
= asm_name
; }
1107 // Return a decl for the function given an identifier.
1109 get_or_make_decl(Gogo
*, Named_object
*, tree id
);
1111 // Export a function declaration.
1113 export_func(Export
* exp
, const std::string
& name
) const
1114 { Function::export_func_with_type(exp
, name
, this->fntype_
); }
1117 // The type of the function.
1118 Function_type
* fntype_
;
1119 // The location of the declaration.
1121 // The assembler name: this is the name to use in references to the
1122 // function. This is normally empty.
1123 std::string asm_name_
;
1124 // The function decl if needed.
1133 Variable(Type
*, Expression
*, bool is_global
, bool is_parameter
,
1134 bool is_receiver
, Location
);
1136 // Get the type of the variable.
1143 // Return whether the type is defined yet.
1146 { return this->type_
!= NULL
; }
1148 // Get the initial value.
1151 { return this->init_
; }
1153 // Return whether there are any preinit statements.
1155 has_pre_init() const
1156 { return this->preinit_
!= NULL
; }
1158 // Return the preinit statements if any.
1161 { return this->preinit_
; }
1163 // Return whether this is a global variable.
1166 { return this->is_global_
; }
1168 // Return whether this is a function parameter.
1170 is_parameter() const
1171 { return this->is_parameter_
; }
1173 // Return whether this is the receiver parameter of a method.
1176 { return this->is_receiver_
; }
1178 // Change this parameter to be a receiver. This is used when
1179 // creating the thunks created for functions which call recover.
1183 go_assert(this->is_parameter_
);
1184 this->is_receiver_
= true;
1187 // Change this parameter to not be a receiver. This is used when
1188 // creating the thunks created for functions which call recover.
1190 set_is_not_receiver()
1192 go_assert(this->is_parameter_
);
1193 this->is_receiver_
= false;
1196 // Return whether this is the varargs parameter of a function.
1198 is_varargs_parameter() const
1199 { return this->is_varargs_parameter_
; }
1201 // Whether this variable's address is taken.
1203 is_address_taken() const
1204 { return this->is_address_taken_
; }
1206 // Whether this variable should live in the heap.
1209 { return this->is_address_taken_
&& !this->is_global_
; }
1211 // Note that something takes the address of this variable.
1214 { this->is_address_taken_
= true; }
1216 // Return whether the address is taken but does not escape.
1218 is_non_escaping_address_taken() const
1219 { return this->is_non_escaping_address_taken_
; }
1221 // Note that something takes the address of this variable such that
1222 // the address does not escape the function.
1224 set_non_escaping_address_taken()
1225 { this->is_non_escaping_address_taken_
= true; }
1227 // Get the source location of the variable's declaration.
1230 { return this->location_
; }
1232 // Record that this is the varargs parameter of a function.
1234 set_is_varargs_parameter()
1236 go_assert(this->is_parameter_
);
1237 this->is_varargs_parameter_
= true;
1240 // Return whether the variable has been used.
1243 { return this->is_used_
; }
1245 // Mark that the variable has been used.
1248 { this->is_used_
= true; }
1250 // Clear the initial value; used for error handling.
1253 { this->init_
= NULL
; }
1255 // Set the initial value; used for converting shortcuts.
1257 set_init(Expression
* init
)
1258 { this->init_
= init
; }
1260 // Get the preinit block, a block of statements to be run before the
1261 // initialization expression.
1263 preinit_block(Gogo
*);
1265 // Add a statement to be run before the initialization expression.
1266 // This is only used for global variables.
1268 add_preinit_statement(Gogo
*, Statement
*);
1270 // Lower the initialization expression after parsing is complete.
1272 lower_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1274 // A special case: the init value is used only to determine the
1275 // type. This is used if the variable is defined using := with the
1276 // comma-ok form of a map index or a receive expression. The init
1277 // value is actually the map index expression or receive expression.
1278 // We use this because we may not know the right type at parse time.
1280 set_type_from_init_tuple()
1281 { this->type_from_init_tuple_
= true; }
1283 // Another special case: the init value is used only to determine
1284 // the type. This is used if the variable is defined using := with
1285 // a range clause. The init value is the range expression. The
1286 // type of the variable is the index type of the range expression
1287 // (i.e., the first value returned by a range).
1289 set_type_from_range_index()
1290 { this->type_from_range_index_
= true; }
1292 // Another special case: like set_type_from_range_index, but the
1293 // type is the value type of the range expression (i.e., the second
1294 // value returned by a range).
1296 set_type_from_range_value()
1297 { this->type_from_range_value_
= true; }
1299 // Another special case: the init value is used only to determine
1300 // the type. This is used if the variable is defined using := with
1301 // a case in a select statement. The init value is the channel.
1302 // The type of the variable is the channel's element type.
1304 set_type_from_chan_element()
1305 { this->type_from_chan_element_
= true; }
1307 // After we lower the select statement, we once again set the type
1308 // from the initialization expression.
1310 clear_type_from_chan_element()
1312 go_assert(this->type_from_chan_element_
);
1313 this->type_from_chan_element_
= false;
1316 // Note that this variable was created for a type switch clause.
1318 set_is_type_switch_var()
1319 { this->is_type_switch_var_
= true; }
1321 // Traverse the initializer expression.
1323 traverse_expression(Traverse
*, unsigned int traverse_mask
);
1325 // Determine the type of the variable if necessary.
1329 // Get the backend representation of the variable.
1331 get_backend_variable(Gogo
*, Named_object
*, const Package
*,
1332 const std::string
&);
1334 // Get the initial value of the variable as a tree. This may only
1335 // be called if has_pre_init() returns false.
1337 get_init_tree(Gogo
*, Named_object
* function
);
1339 // Return a series of statements which sets the value of the
1340 // variable in DECL. This should only be called is has_pre_init()
1341 // returns true. DECL may be NULL for a sink variable.
1343 get_init_block(Gogo
*, Named_object
* function
, tree decl
);
1345 // Export the variable.
1347 export_var(Export
*, const std::string
& name
) const;
1349 // Import a variable.
1351 import_var(Import
*, std::string
* pname
, Type
** ptype
);
1354 // The type of a tuple.
1356 type_from_tuple(Expression
*, bool) const;
1358 // The type of a range.
1360 type_from_range(Expression
*, bool, bool) const;
1362 // The element type of a channel.
1364 type_from_chan_element(Expression
*, bool) const;
1366 // The variable's type. This may be NULL if the type is set from
1369 // The initial value. This may be NULL if the variable should be
1370 // initialized to the default value for the type.
1372 // Statements to run before the init statement.
1374 // Location of variable definition.
1376 // Backend representation.
1377 Bvariable
* backend_
;
1378 // Whether this is a global variable.
1379 bool is_global_
: 1;
1380 // Whether this is a function parameter.
1381 bool is_parameter_
: 1;
1382 // Whether this is the receiver parameter of a method.
1383 bool is_receiver_
: 1;
1384 // Whether this is the varargs parameter of a function.
1385 bool is_varargs_parameter_
: 1;
1386 // Whether this variable is ever referenced.
1388 // Whether something takes the address of this variable. For a
1389 // local variable this implies that the variable has to be on the
1391 bool is_address_taken_
: 1;
1392 // Whether something takes the address of this variable such that
1393 // the address does not escape the function.
1394 bool is_non_escaping_address_taken_
: 1;
1395 // True if we have seen this variable in a traversal.
1397 // True if we have lowered the initialization expression.
1398 bool init_is_lowered_
: 1;
1399 // True if init is a tuple used to set the type.
1400 bool type_from_init_tuple_
: 1;
1401 // True if init is a range clause and the type is the index type.
1402 bool type_from_range_index_
: 1;
1403 // True if init is a range clause and the type is the value type.
1404 bool type_from_range_value_
: 1;
1405 // True if init is a channel and the type is the channel's element type.
1406 bool type_from_chan_element_
: 1;
1407 // True if this is a variable created for a type switch case.
1408 bool is_type_switch_var_
: 1;
1409 // True if we have determined types.
1410 bool determined_type_
: 1;
1413 // A variable which is really the name for a function return value, or
1416 class Result_variable
1419 Result_variable(Type
* type
, Function
* function
, int index
,
1421 : type_(type
), function_(function
), index_(index
), location_(location
),
1422 backend_(NULL
), is_address_taken_(false),
1423 is_non_escaping_address_taken_(false)
1426 // Get the type of the result variable.
1429 { return this->type_
; }
1431 // Get the function that this is associated with.
1434 { return this->function_
; }
1436 // Index in the list of function results.
1439 { return this->index_
; }
1441 // The location of the variable definition.
1444 { return this->location_
; }
1446 // Whether this variable's address is taken.
1448 is_address_taken() const
1449 { return this->is_address_taken_
; }
1451 // Note that something takes the address of this variable.
1454 { this->is_address_taken_
= true; }
1456 // Return whether the address is taken but does not escape.
1458 is_non_escaping_address_taken() const
1459 { return this->is_non_escaping_address_taken_
; }
1461 // Note that something takes the address of this variable such that
1462 // the address does not escape the function.
1464 set_non_escaping_address_taken()
1465 { this->is_non_escaping_address_taken_
= true; }
1467 // Whether this variable should live in the heap.
1470 { return this->is_address_taken_
; }
1472 // Set the function. This is used when cloning functions which call
1475 set_function(Function
* function
)
1476 { this->function_
= function
; }
1478 // Get the backend representation of the variable.
1480 get_backend_variable(Gogo
*, Named_object
*, const std::string
&);
1483 // Type of result variable.
1485 // Function with which this is associated.
1486 Function
* function_
;
1487 // Index in list of results.
1489 // Where the result variable is defined.
1491 // Backend representation.
1492 Bvariable
* backend_
;
1493 // Whether something takes the address of this variable.
1494 bool is_address_taken_
;
1495 // Whether something takes the address of this variable such that
1496 // the address does not escape the function.
1497 bool is_non_escaping_address_taken_
;
1500 // The value we keep for a named constant. This lets us hold a type
1501 // and an expression.
1503 class Named_constant
1506 Named_constant(Type
* type
, Expression
* expr
, int iota_value
,
1508 : type_(type
), expr_(expr
), iota_value_(iota_value
), location_(location
),
1514 { return this->type_
; }
1518 { return this->expr_
; }
1522 { return this->iota_value_
; }
1526 { return this->location_
; }
1528 // Whether we are lowering.
1531 { return this->lowering_
; }
1533 // Set that we are lowering.
1536 { this->lowering_
= true; }
1538 // We are no longer lowering.
1541 { this->lowering_
= false; }
1543 // Traverse the expression.
1545 traverse_expression(Traverse
*);
1547 // Determine the type of the constant if necessary.
1551 // Indicate that we found and reported an error for this constant.
1555 // Export the constant.
1557 export_const(Export
*, const std::string
& name
) const;
1559 // Import a constant.
1561 import_const(Import
*, std::string
*, Type
**, Expression
**);
1564 // The type of the constant.
1566 // The expression for the constant.
1568 // If the predeclared constant iota is used in EXPR_, this is the
1569 // value it will have. We do this because at parse time we don't
1570 // know whether the name "iota" will refer to the predeclared
1571 // constant or to something else. We put in the right value in when
1574 // The location of the definition.
1576 // Whether we are currently lowering this constant.
1580 // A type declaration.
1582 class Type_declaration
1585 Type_declaration(Location location
)
1586 : location_(location
), in_function_(NULL
), methods_(),
1587 issued_warning_(false)
1590 // Return the location.
1593 { return this->location_
; }
1595 // Return the function in which this type is declared. This will
1596 // return NULL for a type declared in global scope.
1599 { return this->in_function_
; }
1601 // Set the function in which this type is declared.
1603 set_in_function(Named_object
* f
)
1604 { this->in_function_
= f
; }
1606 // Add a method to this type. This is used when methods are defined
1609 add_method(const std::string
& name
, Function
* function
);
1611 // Add a method declaration to this type.
1613 add_method_declaration(const std::string
& name
, Function_type
* type
,
1616 // Return whether any methods were defined.
1618 has_methods() const;
1620 // Define methods when the real type is known.
1622 define_methods(Named_type
*);
1624 // This is called if we are trying to use this type. It returns
1625 // true if we should issue a warning.
1630 typedef std::vector
<Named_object
*> Methods
;
1632 // The location of the type declaration.
1634 // If this type is declared in a function, a pointer back to the
1635 // function in which it is defined.
1636 Named_object
* in_function_
;
1637 // Methods defined before the type is defined.
1639 // True if we have issued a warning about a use of this type
1640 // declaration when it is undefined.
1641 bool issued_warning_
;
1644 // An unknown object. These are created by the parser for forward
1645 // references to names which have not been seen before. In a correct
1646 // program, these will always point to a real definition by the end of
1647 // the parse. Because they point to another Named_object, these may
1648 // only be referenced by Unknown_expression objects.
1653 Unknown_name(Location location
)
1654 : location_(location
), real_named_object_(NULL
)
1657 // Return the location where this name was first seen.
1660 { return this->location_
; }
1662 // Return the real named object that this points to, or NULL if it
1663 // was never resolved.
1665 real_named_object() const
1666 { return this->real_named_object_
; }
1668 // Set the real named object that this points to.
1670 set_real_named_object(Named_object
* no
);
1673 // The location where this name was first seen.
1675 // The real named object when it is known.
1680 // A named object named. This is the result of a declaration. We
1681 // don't use a superclass because they all have to be handled
1689 // An uninitialized Named_object. We should never see this.
1690 NAMED_OBJECT_UNINITIALIZED
,
1691 // An unknown name. This is used for forward references. In a
1692 // correct program, these will all be resolved by the end of the
1694 NAMED_OBJECT_UNKNOWN
,
1699 // A forward type declaration.
1700 NAMED_OBJECT_TYPE_DECLARATION
,
1703 // A result variable in a function.
1704 NAMED_OBJECT_RESULT_VAR
,
1705 // The blank identifier--the special variable named _.
1709 // A forward func declaration.
1710 NAMED_OBJECT_FUNC_DECLARATION
,
1712 NAMED_OBJECT_PACKAGE
1715 // Return the classification.
1717 classification() const
1718 { return this->classification_
; }
1724 { return this->classification_
== NAMED_OBJECT_UNKNOWN
; }
1728 { return this->classification_
== NAMED_OBJECT_CONST
; }
1732 { return this->classification_
== NAMED_OBJECT_TYPE
; }
1735 is_type_declaration() const
1736 { return this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
; }
1740 { return this->classification_
== NAMED_OBJECT_VAR
; }
1743 is_result_variable() const
1744 { return this->classification_
== NAMED_OBJECT_RESULT_VAR
; }
1748 { return this->classification_
== NAMED_OBJECT_SINK
; }
1752 { return this->classification_
== NAMED_OBJECT_FUNC
; }
1755 is_function_declaration() const
1756 { return this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
; }
1760 { return this->classification_
== NAMED_OBJECT_PACKAGE
; }
1764 static Named_object
*
1765 make_unknown_name(const std::string
& name
, Location
);
1767 static Named_object
*
1768 make_constant(const Typed_identifier
&, const Package
*, Expression
*,
1771 static Named_object
*
1772 make_type(const std::string
&, const Package
*, Type
*, Location
);
1774 static Named_object
*
1775 make_type_declaration(const std::string
&, const Package
*, Location
);
1777 static Named_object
*
1778 make_variable(const std::string
&, const Package
*, Variable
*);
1780 static Named_object
*
1781 make_result_variable(const std::string
&, Result_variable
*);
1783 static Named_object
*
1786 static Named_object
*
1787 make_function(const std::string
&, const Package
*, Function
*);
1789 static Named_object
*
1790 make_function_declaration(const std::string
&, const Package
*, Function_type
*,
1793 static Named_object
*
1794 make_package(const std::string
& alias
, Package
* package
);
1801 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
1802 return this->u_
.unknown_value
;
1806 unknown_value() const
1808 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
1809 return this->u_
.unknown_value
;
1815 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
1816 return this->u_
.const_value
;
1819 const Named_constant
*
1822 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
1823 return this->u_
.const_value
;
1829 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
1830 return this->u_
.type_value
;
1836 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
1837 return this->u_
.type_value
;
1841 type_declaration_value()
1843 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
1844 return this->u_
.type_declaration
;
1847 const Type_declaration
*
1848 type_declaration_value() const
1850 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
1851 return this->u_
.type_declaration
;
1857 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
1858 return this->u_
.var_value
;
1864 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
1865 return this->u_
.var_value
;
1871 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
1872 return this->u_
.result_var_value
;
1875 const Result_variable
*
1876 result_var_value() const
1878 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
1879 return this->u_
.result_var_value
;
1885 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
1886 return this->u_
.func_value
;
1892 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
1893 return this->u_
.func_value
;
1896 Function_declaration
*
1897 func_declaration_value()
1899 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
1900 return this->u_
.func_declaration_value
;
1903 const Function_declaration
*
1904 func_declaration_value() const
1906 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
1907 return this->u_
.func_declaration_value
;
1913 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
1914 return this->u_
.package_value
;
1918 package_value() const
1920 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
1921 return this->u_
.package_value
;
1926 { return this->name_
; }
1928 // Return the name to use in an error message. The difference is
1929 // that if this Named_object is defined in a different package, this
1930 // will return PACKAGE.NAME.
1932 message_name() const;
1936 { return this->package_
; }
1938 // Resolve an unknown value if possible. This returns the same
1939 // Named_object or a new one.
1943 Named_object
* ret
= this;
1944 if (this->is_unknown())
1946 Named_object
* r
= this->unknown_value()->real_named_object();
1956 const Named_object
* ret
= this;
1957 if (this->is_unknown())
1959 const Named_object
* r
= this->unknown_value()->real_named_object();
1966 // The location where this object was defined or referenced.
1970 // Convert a variable to the backend representation.
1972 get_backend_variable(Gogo
*, Named_object
* function
);
1974 // Return a tree for the external identifier for this object.
1978 // Return a tree representing this object.
1980 get_tree(Gogo
*, Named_object
* function
);
1982 // Define a type declaration.
1984 set_type_value(Named_type
*);
1986 // Define a function declaration.
1988 set_function_value(Function
*);
1990 // Declare an unknown name as a type declaration.
1994 // Export this object.
1996 export_named_object(Export
*) const;
1999 Named_object(const std::string
&, const Package
*, Classification
);
2001 // The name of the object.
2003 // The package that this object is in. This is NULL if it is in the
2004 // file we are compiling.
2005 const Package
* package_
;
2006 // The type of object this is.
2007 Classification classification_
;
2011 Unknown_name
* unknown_value
;
2012 Named_constant
* const_value
;
2013 Named_type
* type_value
;
2014 Type_declaration
* type_declaration
;
2015 Variable
* var_value
;
2016 Result_variable
* result_var_value
;
2017 Function
* func_value
;
2018 Function_declaration
* func_declaration_value
;
2019 Package
* package_value
;
2021 // The DECL tree for this object if we have already converted it.
2025 // A binding contour. This binds names to objects.
2030 // Type for mapping from names to objects.
2031 typedef Unordered_map(std::string
, Named_object
*) Contour
;
2033 Bindings(Bindings
* enclosing
);
2035 // Add an unknown name.
2037 add_unknown_name(const std::string
& name
, Location location
)
2039 return this->add_named_object(Named_object::make_unknown_name(name
,
2045 add_constant(const Typed_identifier
& tid
, const Package
* package
,
2046 Expression
* expr
, int iota_value
)
2048 return this->add_named_object(Named_object::make_constant(tid
, package
,
2055 add_type(const std::string
& name
, const Package
* package
, Type
* type
,
2058 return this->add_named_object(Named_object::make_type(name
, package
, type
,
2062 // Add a named type. This is used for builtin types, and to add an
2063 // imported type to the global scope.
2065 add_named_type(Named_type
* named_type
);
2067 // Add a type declaration.
2069 add_type_declaration(const std::string
& name
, const Package
* package
,
2072 Named_object
* no
= Named_object::make_type_declaration(name
, package
,
2074 return this->add_named_object(no
);
2079 add_variable(const std::string
& name
, const Package
* package
,
2082 return this->add_named_object(Named_object::make_variable(name
, package
,
2086 // Add a result variable.
2088 add_result_variable(const std::string
& name
, Result_variable
* result
)
2090 return this->add_named_object(Named_object::make_result_variable(name
,
2096 add_function(const std::string
& name
, const Package
*, Function
* function
);
2098 // Add a function declaration.
2100 add_function_declaration(const std::string
& name
, const Package
* package
,
2101 Function_type
* type
, Location location
);
2103 // Add a package. The location is the location of the import
2106 add_package(const std::string
& alias
, Package
* package
)
2108 Named_object
* no
= Named_object::make_package(alias
, package
);
2109 return this->add_named_object(no
);
2112 // Define a type which was already declared.
2114 define_type(Named_object
*, Named_type
*);
2116 // Add a method to the list of objects. This is not added to the
2119 add_method(Named_object
*);
2121 // Add a named object to this binding.
2123 add_named_object(Named_object
* no
)
2124 { return this->add_named_object_to_contour(&this->bindings_
, no
); }
2126 // Clear all names in file scope from the bindings.
2130 // Look up a name in this binding contour and in any enclosing
2131 // binding contours. This returns NULL if the name is not found.
2133 lookup(const std::string
&) const;
2135 // Look up a name in this binding contour without looking in any
2136 // enclosing binding contours. Returns NULL if the name is not found.
2138 lookup_local(const std::string
&) const;
2142 remove_binding(Named_object
*);
2144 // Mark all variables as used. This is used for some types of parse
2149 // Traverse the tree. See the Traverse class.
2151 traverse(Traverse
*, bool is_global
);
2153 // Iterate over definitions. This does not include things which
2154 // were only declared.
2156 typedef std::vector
<Named_object
*>::const_iterator
2157 const_definitions_iterator
;
2159 const_definitions_iterator
2160 begin_definitions() const
2161 { return this->named_objects_
.begin(); }
2163 const_definitions_iterator
2164 end_definitions() const
2165 { return this->named_objects_
.end(); }
2167 // Return the number of definitions.
2169 size_definitions() const
2170 { return this->named_objects_
.size(); }
2172 // Return whether there are no definitions.
2174 empty_definitions() const
2175 { return this->named_objects_
.empty(); }
2177 // Iterate over declarations. This is everything that has been
2178 // declared, which includes everything which has been defined.
2180 typedef Contour::const_iterator const_declarations_iterator
;
2182 const_declarations_iterator
2183 begin_declarations() const
2184 { return this->bindings_
.begin(); }
2186 const_declarations_iterator
2187 end_declarations() const
2188 { return this->bindings_
.end(); }
2190 // Return the number of declarations.
2192 size_declarations() const
2193 { return this->bindings_
.size(); }
2195 // Return whether there are no declarations.
2197 empty_declarations() const
2198 { return this->bindings_
.empty(); }
2200 // Return the first declaration.
2203 { return this->bindings_
.empty() ? NULL
: this->bindings_
.begin()->second
; }
2207 add_named_object_to_contour(Contour
*, Named_object
*);
2210 new_definition(Named_object
*, Named_object
*);
2212 // Enclosing bindings.
2213 Bindings
* enclosing_
;
2214 // The list of objects.
2215 std::vector
<Named_object
*> named_objects_
;
2216 // The mapping from names to objects.
2225 Label(const std::string
& name
)
2226 : name_(name
), location_(Linemap::unknown_location()), snapshot_(NULL
),
2227 refs_(), is_used_(false), blabel_(NULL
)
2230 // Return the label's name.
2233 { return this->name_
; }
2235 // Return whether the label has been defined.
2238 { return !Linemap::is_unknown_location(this->location_
); }
2240 // Return whether the label has been used.
2243 { return this->is_used_
; }
2245 // Record that the label is used.
2248 { this->is_used_
= true; }
2250 // Return the location of the definition.
2253 { return this->location_
; }
2255 // Return the bindings snapshot.
2258 { return this->snapshot_
; }
2260 // Add a snapshot of a goto which refers to this label.
2262 add_snapshot_ref(Bindings_snapshot
* snapshot
)
2264 go_assert(Linemap::is_unknown_location(this->location_
));
2265 this->refs_
.push_back(snapshot
);
2268 // Return the list of snapshots of goto statements which refer to
2270 const std::vector
<Bindings_snapshot
*>&
2272 { return this->refs_
; }
2274 // Clear the references.
2278 // Define the label at LOCATION with the given bindings snapshot.
2280 define(Location location
, Bindings_snapshot
* snapshot
)
2282 go_assert(Linemap::is_unknown_location(this->location_
)
2283 && this->snapshot_
== NULL
);
2284 this->location_
= location
;
2285 this->snapshot_
= snapshot
;
2288 // Return the backend representation for this label.
2290 get_backend_label(Translate_context
*);
2292 // Return an expression for the address of this label. This is used
2293 // to get the return address of a deferred function to see whether
2294 // the function may call recover.
2296 get_addr(Translate_context
*, Location location
);
2299 // The name of the label.
2301 // The location of the definition. This is 0 if the label has not
2302 // yet been defined.
2304 // A snapshot of the set of bindings defined at this label, used to
2305 // issue errors about invalid goto statements.
2306 Bindings_snapshot
* snapshot_
;
2307 // A list of snapshots of goto statements which refer to this label.
2308 std::vector
<Bindings_snapshot
*> refs_
;
2309 // Whether the label has been used.
2311 // The backend representation.
2315 // An unnamed label. These are used when lowering loops.
2320 Unnamed_label(Location location
)
2321 : location_(location
), blabel_(NULL
)
2324 // Get the location where the label is defined.
2327 { return this->location_
; }
2329 // Set the location where the label is defined.
2331 set_location(Location location
)
2332 { this->location_
= location
; }
2334 // Return a statement which defines this label.
2336 get_definition(Translate_context
*);
2338 // Return a goto to this label from LOCATION.
2340 get_goto(Translate_context
*, Location location
);
2343 // Return the backend representation.
2345 get_blabel(Translate_context
*);
2347 // The location where the label is defined.
2349 // The backend representation of this label.
2353 // An imported package.
2358 Package(const std::string
& name
, const std::string
& unique_prefix
,
2361 // The real name of this package. This may be different from the
2362 // name in the associated Named_object if the import statement used
2366 { return this->name_
; }
2368 // Return the location of the import statement.
2371 { return this->location_
; }
2373 // Get the unique prefix used for all symbols exported from this
2376 unique_prefix() const
2378 go_assert(!this->unique_prefix_
.empty());
2379 return this->unique_prefix_
;
2382 // The priority of this package. The init function of packages with
2383 // lower priority must be run before the init function of packages
2384 // with higher priority.
2387 { return this->priority_
; }
2389 // Set the priority.
2391 set_priority(int priority
);
2393 // Return the bindings.
2396 { return this->bindings_
; }
2398 // Whether some symbol from the package was used.
2401 { return this->used_
; }
2403 // Note that some symbol from this package was used.
2406 { this->used_
= true; }
2408 // Clear the used field for the next file.
2411 { this->used_
= false; }
2413 // Whether this package was imported in the current file.
2416 { return this->is_imported_
; }
2418 // Note that this package was imported in the current file.
2421 { this->is_imported_
= true; }
2423 // Clear the imported field for the next file.
2426 { this->is_imported_
= false; }
2428 // Whether this package was imported with a name of "_".
2430 uses_sink_alias() const
2431 { return this->uses_sink_alias_
; }
2433 // Note that this package was imported with a name of "_".
2435 set_uses_sink_alias()
2436 { this->uses_sink_alias_
= true; }
2438 // Clear the sink alias field for the next file.
2440 clear_uses_sink_alias()
2441 { this->uses_sink_alias_
= false; }
2443 // Look up a name in the package. Returns NULL if the name is not
2446 lookup(const std::string
& name
) const
2447 { return this->bindings_
->lookup(name
); }
2449 // Set the location of the package. This is used if it is seen in a
2450 // different import before it is really imported.
2452 set_location(Location location
)
2453 { this->location_
= location
; }
2455 // Add a constant to the package.
2457 add_constant(const Typed_identifier
& tid
, Expression
* expr
)
2458 { return this->bindings_
->add_constant(tid
, this, expr
, 0); }
2460 // Add a type to the package.
2462 add_type(const std::string
& name
, Type
* type
, Location location
)
2463 { return this->bindings_
->add_type(name
, this, type
, location
); }
2465 // Add a type declaration to the package.
2467 add_type_declaration(const std::string
& name
, Location location
)
2468 { return this->bindings_
->add_type_declaration(name
, this, location
); }
2470 // Add a variable to the package.
2472 add_variable(const std::string
& name
, Variable
* variable
)
2473 { return this->bindings_
->add_variable(name
, this, variable
); }
2475 // Add a function declaration to the package.
2477 add_function_declaration(const std::string
& name
, Function_type
* type
,
2479 { return this->bindings_
->add_function_declaration(name
, this, type
, loc
); }
2481 // Determine types of constants.
2486 // The real name of this package.
2488 // The unique prefix for all exported global symbols.
2489 std::string unique_prefix_
;
2490 // The names in this package.
2491 Bindings
* bindings_
;
2492 // The priority of this package. A package has a priority higher
2493 // than the priority of all of the packages that it imports. This
2494 // is used to run init functions in the right order.
2496 // The location of the import statement.
2498 // True if some name from this package was used. This is mutable
2499 // because we can use a package even if we have a const pointer to
2502 // True if this package was imported in the current file.
2504 // True if this package was imported with a name of "_".
2505 bool uses_sink_alias_
;
2508 // Return codes for the traversal functions. This is not an enum
2509 // because we want to be able to declare traversal functions in other
2510 // header files without including this one.
2512 // Continue traversal as usual.
2513 const int TRAVERSE_CONTINUE
= -1;
2516 const int TRAVERSE_EXIT
= 0;
2518 // Continue traversal, but skip components of the current object.
2519 // E.g., if this is returned by Traverse::statement, we do not
2520 // traverse the expressions in the statement even if
2521 // traverse_expressions is set in the traverse_mask.
2522 const int TRAVERSE_SKIP_COMPONENTS
= 1;
2524 // This class is used when traversing the parse tree. The caller uses
2525 // a subclass which overrides functions as desired.
2530 // These bitmasks say what to traverse.
2531 static const unsigned int traverse_variables
= 0x1;
2532 static const unsigned int traverse_constants
= 0x2;
2533 static const unsigned int traverse_functions
= 0x4;
2534 static const unsigned int traverse_blocks
= 0x8;
2535 static const unsigned int traverse_statements
= 0x10;
2536 static const unsigned int traverse_expressions
= 0x20;
2537 static const unsigned int traverse_types
= 0x40;
2539 Traverse(unsigned int traverse_mask
)
2540 : traverse_mask_(traverse_mask
), types_seen_(NULL
), expressions_seen_(NULL
)
2543 virtual ~Traverse();
2545 // The bitmask of what to traverse.
2547 traverse_mask() const
2548 { return this->traverse_mask_
; }
2550 // Record that we are going to traverse a type. This returns true
2551 // if the type has already been seen in this traversal. This is
2552 // required because types, unlike expressions, can form a circular
2555 remember_type(const Type
*);
2557 // Record that we are going to see an expression. This returns true
2558 // if the expression has already been seen in this traversal. This
2559 // is only needed for cases where multiple expressions can point to
2562 remember_expression(const Expression
*);
2564 // These functions return one of the TRAVERSE codes defined above.
2566 // If traverse_variables is set in the mask, this is called for
2567 // every variable in the tree.
2569 variable(Named_object
*);
2571 // If traverse_constants is set in the mask, this is called for
2572 // every named constant in the tree. The bool parameter is true for
2573 // a global constant.
2575 constant(Named_object
*, bool);
2577 // If traverse_functions is set in the mask, this is called for
2578 // every function in the tree.
2580 function(Named_object
*);
2582 // If traverse_blocks is set in the mask, this is called for every
2583 // block in the tree.
2587 // If traverse_statements is set in the mask, this is called for
2588 // every statement in the tree.
2590 statement(Block
*, size_t* index
, Statement
*);
2592 // If traverse_expressions is set in the mask, this is called for
2593 // every expression in the tree.
2595 expression(Expression
**);
2597 // If traverse_types is set in the mask, this is called for every
2598 // type in the tree.
2603 // A hash table for types we have seen during this traversal. Note
2604 // that this uses the default hash functions for pointers rather
2605 // than Type_hash_identical and Type_identical. This is because for
2606 // traversal we care about seeing a specific type structure. If
2607 // there are two separate instances of identical types, we want to
2609 typedef Unordered_set(const Type
*) Types_seen
;
2611 typedef Unordered_set(const Expression
*) Expressions_seen
;
2613 // Bitmask of what sort of objects to traverse.
2614 unsigned int traverse_mask_
;
2615 // Types which have been seen in this traversal.
2616 Types_seen
* types_seen_
;
2617 // Expressions which have been seen in this traversal.
2618 Expressions_seen
* expressions_seen_
;
2621 // A class which makes it easier to insert new statements before the
2622 // current statement during a traversal.
2624 class Statement_inserter
2627 // Empty constructor.
2628 Statement_inserter()
2629 : block_(NULL
), pindex_(NULL
), gogo_(NULL
), var_(NULL
)
2632 // Constructor for a statement in a block.
2633 Statement_inserter(Block
* block
, size_t *pindex
)
2634 : block_(block
), pindex_(pindex
), gogo_(NULL
), var_(NULL
)
2637 // Constructor for a global variable.
2638 Statement_inserter(Gogo
* gogo
, Variable
* var
)
2639 : block_(NULL
), pindex_(NULL
), gogo_(gogo
), var_(var
)
2640 { go_assert(var
->is_global()); }
2642 // We use the default copy constructor and assignment operator.
2644 // Insert S before the statement we are traversing, or before the
2645 // initialization expression of a global variable.
2647 insert(Statement
* s
);
2650 // The block that the statement is in.
2652 // The index of the statement that we are traversing.
2654 // The IR, needed when looking at an initializer expression for a
2657 // The global variable, when looking at an initializer expression.
2661 // When translating the gogo IR into the backend data structure, this
2662 // is the context we pass down the blocks and statements.
2664 class Translate_context
2667 Translate_context(Gogo
* gogo
, Named_object
* function
, Block
* block
,
2669 : gogo_(gogo
), backend_(gogo
->backend()), function_(function
),
2670 block_(block
), bblock_(bblock
), is_const_(false)
2677 { return this->gogo_
; }
2681 { return this->backend_
; }
2685 { return this->function_
; }
2689 { return this->block_
; }
2693 { return this->bblock_
; }
2697 { return this->is_const_
; }
2699 // Make a constant context.
2702 { this->is_const_
= true; }
2705 // The IR for the entire compilation unit.
2707 // The generator for the backend data structures.
2709 // The function we are currently translating. NULL if not in a
2710 // function, e.g., the initializer of a global variable.
2711 Named_object
* function_
;
2712 // The block we are currently translating. NULL if not in a
2715 // The backend representation of the current block. NULL if block_
2718 // Whether this is being evaluated in a constant context. This is
2719 // used for type descriptor initializers.
2723 // Runtime error codes. These must match the values in
2724 // libgo/runtime/go-runtime-error.c.
2726 // Slice index out of bounds: negative or larger than the length of
2728 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
= 0;
2730 // Array index out of bounds.
2731 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
= 1;
2733 // String index out of bounds.
2734 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
= 2;
2736 // Slice slice out of bounds: negative or larger than the length of
2737 // the slice or high bound less than low bound.
2738 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
= 3;
2740 // Array slice out of bounds.
2741 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
= 4;
2743 // String slice out of bounds.
2744 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
= 5;
2746 // Dereference of nil pointer. This is used when there is a
2747 // dereference of a pointer to a very large struct or array, to ensure
2748 // that a gigantic array is not used a proxy to access random memory
2750 static const int RUNTIME_ERROR_NIL_DEREFERENCE
= 6;
2752 // Slice length or capacity out of bounds in make: negative or
2753 // overflow or length greater than capacity.
2754 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
= 7;
2756 // Map capacity out of bounds in make: negative or overflow.
2757 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
= 8;
2759 // Channel capacity out of bounds in make: negative or overflow.
2760 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
= 9;
2762 // This is used by some of the langhooks.
2763 extern Gogo
* go_get_gogo();
2765 // Whether we have seen any errors. FIXME: Replace with a backend
2767 extern bool saw_errors();
2769 #endif // !defined(GO_GOGO_H)