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 // Whether we are done writing out specific type functions.
403 specific_type_functions_are_written() const
404 { return this->specific_type_functions_are_written_
; }
406 // Traverse the tree. See the Traverse class.
410 // Define the predeclared global names.
412 define_global_names();
414 // Verify and complete all types.
418 // Lower the parse tree.
422 // Lower all the statements in a block.
424 lower_block(Named_object
* function
, Block
*);
426 // Lower an expression.
428 lower_expression(Named_object
* function
, Statement_inserter
*, Expression
**);
432 lower_constant(Named_object
*);
434 // Finalize the method lists and build stub methods for named types.
438 // Work out the types to use for unspecified variables and
443 // Type check the program.
447 // Check the types in a single block. This is used for complicated
450 check_types_in_block(Block
*);
452 // Check for return statements.
454 check_return_statements();
460 // Add an import control function for an imported package to the
463 add_import_init_fn(const std::string
& package_name
,
464 const std::string
& init_name
, int prio
);
466 // Turn short-cut operators (&&, ||) into explicit if statements.
470 // Use temporary variables to force order of evaluation.
474 // Build thunks for functions which call recover.
476 build_recover_thunks();
478 // Simplify statements which might use thunks: go and defer
481 simplify_thunk_statements();
483 // Dump AST if -fgo-dump-ast is set
485 dump_ast(const char* basename
);
487 // Convert named types to the backend representation.
489 convert_named_types();
491 // Convert named types in a list of bindings.
493 convert_named_types_in_bindings(Bindings
*);
495 // True if named types have been converted to the backend
498 named_types_are_converted() const
499 { return this->named_types_are_converted_
; }
501 // Write out the global values.
505 // Create trees for implicit builtin functions.
507 define_builtin_function_trees();
509 // Build a call to a builtin function. PDECL should point to a NULL
510 // initialized static pointer which will hold the fndecl. NAME is
511 // the name of the function. NARGS is the number of arguments.
512 // RETTYPE is the return type. It is followed by NARGS pairs of
513 // type and argument (both trees).
515 call_builtin(tree
* pdecl
, Location
, const char* name
, int nargs
,
518 // Build a call to the runtime error function.
520 runtime_error(int code
, Location
);
522 // Build a builtin struct with a list of fields.
524 builtin_struct(tree
* ptype
, const char* struct_name
, tree struct_type
,
527 // Mark a function declaration as a builtin library function.
529 mark_fndecl_as_builtin_library(tree fndecl
);
531 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
532 // the slice. VALUES points to the values. COUNT is the size,
533 // CAPACITY is the capacity. If CAPACITY is NULL, it is set to
536 slice_constructor(tree slice_type_tree
, tree values
, tree count
,
539 // Build required interface method tables.
541 build_interface_method_tables();
543 // Build an interface method table for a type: a list of function
544 // pointers, one for each interface method. This returns a decl.
546 interface_method_table_for_type(const Interface_type
*, Named_type
*,
549 // Return a tree which allocate SIZE bytes to hold values of type
552 allocate_memory(Type
*type
, tree size
, Location
);
554 // Return a type to use for pointer to const char.
556 const_char_pointer_type_tree();
558 // Build a string constant with the right type.
560 string_constant_tree(const std::string
&);
562 // Build a Go string constant. This returns a pointer to the
565 go_string_constant_tree(const std::string
&);
567 // Receive a value from a channel.
569 receive_from_channel(tree type_tree
, tree type_descriptor_tree
, tree channel
,
572 // Make a trampoline which calls FNADDR passing CLOSURE.
574 make_trampoline(tree fnaddr
, tree closure
, Location
);
577 // During parsing, we keep a stack of functions. Each function on
578 // the stack is one that we are currently parsing. For each
579 // function, we keep track of the current stack of blocks.
583 Named_object
* function
;
584 // The stack of active blocks in the function.
585 std::vector
<Block
*> blocks
;
588 // The stack of functions.
589 typedef std::vector
<Open_function
> Open_functions
;
591 // Set up the built-in unsafe package.
593 import_unsafe(const std::string
&, bool is_exported
, Location
);
595 // Add a new imported package.
597 add_package(const std::string
& real_name
, const std::string
& alias
,
598 const std::string
& unique_prefix
, Location location
);
600 // Return the current binding contour.
605 current_bindings() const;
607 // Get the name of the magic initialization function.
611 // Get the decl for the magic initialization function.
613 initialization_function_decl();
615 // Write the magic initialization function.
617 write_initialization_function(tree fndecl
, tree init_stmt_list
);
619 // Initialize imported packages.
623 // Register variables with the garbage collector.
625 register_gc_vars(const std::vector
<Named_object
*>&, tree
*);
627 // Build a pointer to a Go string constant. This returns a pointer
630 ptr_go_string_constant_tree(const std::string
&);
632 // Return the type of a trampoline.
634 trampoline_type_tree();
636 // Type used to map import names to packages.
637 typedef std::map
<std::string
, Package
*> Imports
;
639 // Type used to map package names to packages.
640 typedef std::map
<std::string
, Package
*> Packages
;
642 // Type used to map special names in the sys package.
643 typedef std::map
<std::string
, std::string
> Sys_names
;
645 // Type used to queue writing a type specific function.
646 struct Specific_type_function
650 std::string hash_name
;
651 Function_type
* hash_fntype
;
652 std::string equal_name
;
653 Function_type
* equal_fntype
;
655 Specific_type_function(Type
* atype
, Named_type
* aname
,
656 const std::string
& ahash_name
,
657 Function_type
* ahash_fntype
,
658 const std::string
& aequal_name
,
659 Function_type
* aequal_fntype
)
660 : type(atype
), name(aname
), hash_name(ahash_name
),
661 hash_fntype(ahash_fntype
), equal_name(aequal_name
),
662 equal_fntype(aequal_fntype
)
666 // The backend generator.
668 // The object used to keep track of file names and line numbers.
670 // The package we are compiling.
672 // The list of currently open functions during parsing.
673 Open_functions functions_
;
674 // The global binding contour. This includes the builtin functions
675 // and the package we are compiling.
677 // Mapping from import file names to packages.
679 // Whether the magic unsafe package was imported.
680 bool imported_unsafe_
;
681 // Mapping from package names we have seen to packages. This does
682 // not include the package we are compiling.
684 // The functions named "init", if there are any.
685 std::vector
<Named_object
*> init_functions_
;
686 // Whether we need a magic initialization function.
688 // The name of the magic initialization function.
689 std::string init_fn_name_
;
690 // A list of import control variables for packages that we import.
691 std::set
<Import_init
> imported_init_fns_
;
692 // The unique prefix used for all global symbols.
693 std::string unique_prefix_
;
694 // Whether an explicit unique prefix was set by -fgo-prefix.
695 bool unique_prefix_specified_
;
696 // A list of types to verify.
697 std::vector
<Type
*> verify_types_
;
698 // A list of interface types defined while parsing.
699 std::vector
<Interface_type
*> interface_types_
;
700 // Type specific functions to write out.
701 std::vector
<Specific_type_function
*> specific_type_functions_
;
702 // Whether we are done writing out specific type functions.
703 bool specific_type_functions_are_written_
;
704 // Whether named types have been converted.
705 bool named_types_are_converted_
;
708 // A block of statements.
713 Block(Block
* enclosing
, Location
);
715 // Return the enclosing block.
718 { return this->enclosing_
; }
720 // Return the bindings of the block.
723 { return this->bindings_
; }
727 { return this->bindings_
; }
729 // Look at the block's statements.
730 const std::vector
<Statement
*>*
732 { return &this->statements_
; }
734 // Return the start location. This is normally the location of the
735 // left curly brace which starts the block.
737 start_location() const
738 { return this->start_location_
; }
740 // Return the end location. This is normally the location of the
741 // right curly brace which ends the block.
744 { return this->end_location_
; }
746 // Add a statement to the block.
748 add_statement(Statement
*);
750 // Add a statement to the front of the block.
752 add_statement_at_front(Statement
*);
754 // Replace a statement in a block.
756 replace_statement(size_t index
, Statement
*);
758 // Add a Statement before statement number INDEX.
760 insert_statement_before(size_t index
, Statement
*);
762 // Add a Statement after statement number INDEX.
764 insert_statement_after(size_t index
, Statement
*);
766 // Set the end location of the block.
768 set_end_location(Location location
)
769 { this->end_location_
= location
; }
771 // Traverse the tree.
775 // Set final types for unspecified variables and constants.
779 // Return true if execution of this block may fall through to the
782 may_fall_through() const;
784 // Convert the block to the backend representation.
786 get_backend(Translate_context
*);
788 // Iterate over statements.
790 typedef std::vector
<Statement
*>::iterator iterator
;
794 { return this->statements_
.begin(); }
798 { return this->statements_
.end(); }
803 // Statements in the block.
804 std::vector
<Statement
*> statements_
;
807 // Location of start of block.
808 Location start_location_
;
809 // Location of end of block.
810 Location end_location_
;
818 Function(Function_type
* type
, Function
*, Block
*, Location
);
820 // Return the function's type.
823 { return this->type_
; }
825 // Return the enclosing function if there is one.
828 { return this->enclosing_
; }
830 // Set the enclosing function. This is used when building thunks
831 // for functions which call recover.
833 set_enclosing(Function
* enclosing
)
835 go_assert(this->enclosing_
== NULL
);
836 this->enclosing_
= enclosing
;
839 // The result variables.
840 typedef std::vector
<Named_object
*> Results
;
842 // Create the result variables in the outer block.
844 create_result_variables(Gogo
*);
846 // Update the named result variables when cloning a function which
849 update_result_variables();
851 // Return the result variables.
854 { return this->results_
; }
856 // Whether the result variables have names.
858 results_are_named() const
859 { return this->results_are_named_
; }
861 // Add a new field to the closure variable.
863 add_closure_field(Named_object
* var
, Location loc
)
864 { this->closure_fields_
.push_back(std::make_pair(var
, loc
)); }
866 // Whether this function needs a closure.
868 needs_closure() const
869 { return !this->closure_fields_
.empty(); }
871 // Return the closure variable, creating it if necessary. This is
872 // passed to the function as a static chain parameter.
876 // Set the closure variable. This is used when building thunks for
877 // functions which call recover.
879 set_closure_var(Named_object
* v
)
881 go_assert(this->closure_var_
== NULL
);
882 this->closure_var_
= v
;
885 // Return the variable for a reference to field INDEX in the closure
888 enclosing_var(unsigned int index
)
890 go_assert(index
< this->closure_fields_
.size());
891 return closure_fields_
[index
].first
;
894 // Set the type of the closure variable if there is one.
898 // Get the block of statements associated with the function.
901 { return this->block_
; }
903 // Get the location of the start of the function.
906 { return this->location_
; }
908 // Return whether this function is actually a method.
912 // Add a label definition to the function.
914 add_label_definition(Gogo
*, const std::string
& label_name
, Location
);
916 // Add a label reference to a function. ISSUE_GOTO_ERRORS is true
917 // if we should report errors for a goto from the current location
918 // to the label location.
920 add_label_reference(Gogo
*, const std::string
& label_name
,
921 Location
, bool issue_goto_errors
);
923 // Warn about labels that are defined but not used.
925 check_labels() const;
927 // Whether this function calls the predeclared recover function.
929 calls_recover() const
930 { return this->calls_recover_
; }
932 // Record that this function calls the predeclared recover function.
933 // This is set during the lowering pass.
936 { this->calls_recover_
= true; }
938 // Whether this is a recover thunk function.
940 is_recover_thunk() const
941 { return this->is_recover_thunk_
; }
943 // Record that this is a thunk built for a function which calls
946 set_is_recover_thunk()
947 { this->is_recover_thunk_
= true; }
949 // Whether this function already has a recover thunk.
951 has_recover_thunk() const
952 { return this->has_recover_thunk_
; }
954 // Record that this function already has a recover thunk.
956 set_has_recover_thunk()
957 { this->has_recover_thunk_
= true; }
959 // Swap with another function. Used only for the thunk which calls
962 swap_for_recover(Function
*);
964 // Traverse the tree.
968 // Determine types in the function.
972 // Return the function's decl given an identifier.
974 get_or_make_decl(Gogo
*, Named_object
*, tree id
);
976 // Return the function's decl after it has been built.
980 go_assert(this->fndecl_
!= NULL
);
981 return this->fndecl_
;
984 // Set the function decl to hold a tree of the function code.
986 build_tree(Gogo
*, Named_object
*);
988 // Get the value to return when not explicitly specified. May also
989 // add statements to execute first to STMT_LIST.
991 return_value(Gogo
*, Named_object
*, Location
, tree
* stmt_list
) const;
993 // Get a tree for the variable holding the defer stack.
995 defer_stack(Location
);
997 // Export the function.
999 export_func(Export
*, const std::string
& name
) const;
1001 // Export a function with a type.
1003 export_func_with_type(Export
*, const std::string
& name
,
1004 const Function_type
*);
1006 // Import a function.
1008 import_func(Import
*, std::string
* pname
, Typed_identifier
** receiver
,
1009 Typed_identifier_list
** pparameters
,
1010 Typed_identifier_list
** presults
, bool* is_varargs
);
1013 // Type for mapping from label names to Label objects.
1014 typedef Unordered_map(std::string
, Label
*) Labels
;
1017 make_receiver_parm_decl(Gogo
*, Named_object
*, tree
);
1020 copy_parm_to_heap(Gogo
*, Named_object
*, tree
);
1023 build_defer_wrapper(Gogo
*, Named_object
*, tree
*, tree
*);
1025 typedef std::vector
<std::pair
<Named_object
*,
1026 Location
> > Closure_fields
;
1028 // The function's type.
1029 Function_type
* type_
;
1030 // The enclosing function. This is NULL when there isn't one, which
1031 // is the normal case.
1032 Function
* enclosing_
;
1033 // The result variables, if any.
1035 // If there is a closure, this is the list of variables which appear
1036 // in the closure. This is created by the parser, and then resolved
1037 // to a real type when we lower parse trees.
1038 Closure_fields closure_fields_
;
1039 // The closure variable, passed as a parameter using the static
1040 // chain parameter. Normally NULL.
1041 Named_object
* closure_var_
;
1042 // The outer block of statements in the function.
1044 // The source location of the start of the function.
1046 // Labels defined or referenced in the function.
1048 // The function decl.
1050 // The defer stack variable. A pointer to this variable is used to
1051 // distinguish the defer stack for one function from another. This
1052 // is NULL unless we actually need a defer stack.
1053 Temporary_statement
* defer_stack_
;
1054 // True if the result variables are named.
1055 bool results_are_named_
;
1056 // True if this function calls the predeclared recover function.
1057 bool calls_recover_
;
1058 // True if this a thunk built for a function which calls recover.
1059 bool is_recover_thunk_
;
1060 // True if this function already has a recover thunk.
1061 bool has_recover_thunk_
;
1064 // A snapshot of the current binding state.
1066 class Bindings_snapshot
1069 Bindings_snapshot(const Block
*, Location
);
1071 // Report any errors appropriate for a goto from the current binding
1072 // state of B to this one.
1074 check_goto_from(const Block
* b
, Location
);
1076 // Report any errors appropriate for a goto from this binding state
1077 // to the current state of B.
1079 check_goto_to(const Block
* b
);
1083 check_goto_block(Location
, const Block
*, const Block
*, size_t*);
1086 check_goto_defs(Location
, const Block
*, size_t, size_t);
1088 // The current block.
1089 const Block
* block_
;
1090 // The number of names currently defined in each open block.
1091 // Element 0 is this->block_, element 1 is
1092 // this->block_->enclosing(), etc.
1093 std::vector
<size_t> counts_
;
1094 // The location where this snapshot was taken.
1098 // A function declaration.
1100 class Function_declaration
1103 Function_declaration(Function_type
* fntype
, Location location
)
1104 : fntype_(fntype
), location_(location
), asm_name_(), fndecl_(NULL
)
1109 { return this->fntype_
; }
1113 { return this->location_
; }
1117 { return this->asm_name_
; }
1119 // Set the assembler name.
1121 set_asm_name(const std::string
& asm_name
)
1122 { this->asm_name_
= asm_name
; }
1124 // Return a decl for the function given an identifier.
1126 get_or_make_decl(Gogo
*, Named_object
*, tree id
);
1128 // Export a function declaration.
1130 export_func(Export
* exp
, const std::string
& name
) const
1131 { Function::export_func_with_type(exp
, name
, this->fntype_
); }
1134 // The type of the function.
1135 Function_type
* fntype_
;
1136 // The location of the declaration.
1138 // The assembler name: this is the name to use in references to the
1139 // function. This is normally empty.
1140 std::string asm_name_
;
1141 // The function decl if needed.
1150 Variable(Type
*, Expression
*, bool is_global
, bool is_parameter
,
1151 bool is_receiver
, Location
);
1153 // Get the type of the variable.
1160 // Return whether the type is defined yet.
1164 // Get the initial value.
1167 { return this->init_
; }
1169 // Return whether there are any preinit statements.
1171 has_pre_init() const
1172 { return this->preinit_
!= NULL
; }
1174 // Return the preinit statements if any.
1177 { return this->preinit_
; }
1179 // Return whether this is a global variable.
1182 { return this->is_global_
; }
1184 // Return whether this is a function parameter.
1186 is_parameter() const
1187 { return this->is_parameter_
; }
1189 // Return whether this is the receiver parameter of a method.
1192 { return this->is_receiver_
; }
1194 // Change this parameter to be a receiver. This is used when
1195 // creating the thunks created for functions which call recover.
1199 go_assert(this->is_parameter_
);
1200 this->is_receiver_
= true;
1203 // Change this parameter to not be a receiver. This is used when
1204 // creating the thunks created for functions which call recover.
1206 set_is_not_receiver()
1208 go_assert(this->is_parameter_
);
1209 this->is_receiver_
= false;
1212 // Return whether this is the varargs parameter of a function.
1214 is_varargs_parameter() const
1215 { return this->is_varargs_parameter_
; }
1217 // Whether this variable's address is taken.
1219 is_address_taken() const
1220 { return this->is_address_taken_
; }
1222 // Whether this variable should live in the heap.
1225 { return this->is_address_taken_
&& !this->is_global_
; }
1227 // Note that something takes the address of this variable.
1230 { this->is_address_taken_
= true; }
1232 // Return whether the address is taken but does not escape.
1234 is_non_escaping_address_taken() const
1235 { return this->is_non_escaping_address_taken_
; }
1237 // Note that something takes the address of this variable such that
1238 // the address does not escape the function.
1240 set_non_escaping_address_taken()
1241 { this->is_non_escaping_address_taken_
= true; }
1243 // Get the source location of the variable's declaration.
1246 { return this->location_
; }
1248 // Record that this is the varargs parameter of a function.
1250 set_is_varargs_parameter()
1252 go_assert(this->is_parameter_
);
1253 this->is_varargs_parameter_
= true;
1256 // Return whether the variable has been used.
1259 { return this->is_used_
; }
1261 // Mark that the variable has been used.
1264 { this->is_used_
= true; }
1266 // Clear the initial value; used for error handling.
1269 { this->init_
= NULL
; }
1271 // Set the initial value; used for converting shortcuts.
1273 set_init(Expression
* init
)
1274 { this->init_
= init
; }
1276 // Get the preinit block, a block of statements to be run before the
1277 // initialization expression.
1279 preinit_block(Gogo
*);
1281 // Add a statement to be run before the initialization expression.
1282 // This is only used for global variables.
1284 add_preinit_statement(Gogo
*, Statement
*);
1286 // Lower the initialization expression after parsing is complete.
1288 lower_init_expression(Gogo
*, Named_object
*, Statement_inserter
*);
1290 // A special case: the init value is used only to determine the
1291 // type. This is used if the variable is defined using := with the
1292 // comma-ok form of a map index or a receive expression. The init
1293 // value is actually the map index expression or receive expression.
1294 // We use this because we may not know the right type at parse time.
1296 set_type_from_init_tuple()
1297 { this->type_from_init_tuple_
= 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 range clause. The init value is the range expression. The
1302 // type of the variable is the index type of the range expression
1303 // (i.e., the first value returned by a range).
1305 set_type_from_range_index()
1306 { this->type_from_range_index_
= true; }
1308 // Another special case: like set_type_from_range_index, but the
1309 // type is the value type of the range expression (i.e., the second
1310 // value returned by a range).
1312 set_type_from_range_value()
1313 { this->type_from_range_value_
= true; }
1315 // Another special case: the init value is used only to determine
1316 // the type. This is used if the variable is defined using := with
1317 // a case in a select statement. The init value is the channel.
1318 // The type of the variable is the channel's element type.
1320 set_type_from_chan_element()
1321 { this->type_from_chan_element_
= true; }
1323 // After we lower the select statement, we once again set the type
1324 // from the initialization expression.
1326 clear_type_from_chan_element()
1328 go_assert(this->type_from_chan_element_
);
1329 this->type_from_chan_element_
= false;
1332 // Note that this variable was created for a type switch clause.
1334 set_is_type_switch_var()
1335 { this->is_type_switch_var_
= true; }
1337 // Traverse the initializer expression.
1339 traverse_expression(Traverse
*, unsigned int traverse_mask
);
1341 // Determine the type of the variable if necessary.
1345 // Get the backend representation of the variable.
1347 get_backend_variable(Gogo
*, Named_object
*, const Package
*,
1348 const std::string
&);
1350 // Get the initial value of the variable as a tree. This may only
1351 // be called if has_pre_init() returns false.
1353 get_init_tree(Gogo
*, Named_object
* function
);
1355 // Return a series of statements which sets the value of the
1356 // variable in DECL. This should only be called is has_pre_init()
1357 // returns true. DECL may be NULL for a sink variable.
1359 get_init_block(Gogo
*, Named_object
* function
, tree decl
);
1361 // Export the variable.
1363 export_var(Export
*, const std::string
& name
) const;
1365 // Import a variable.
1367 import_var(Import
*, std::string
* pname
, Type
** ptype
);
1370 // The type of a tuple.
1372 type_from_tuple(Expression
*, bool) const;
1374 // The type of a range.
1376 type_from_range(Expression
*, bool, bool) const;
1378 // The element type of a channel.
1380 type_from_chan_element(Expression
*, bool) const;
1382 // The variable's type. This may be NULL if the type is set from
1385 // The initial value. This may be NULL if the variable should be
1386 // initialized to the default value for the type.
1388 // Statements to run before the init statement.
1390 // Location of variable definition.
1392 // Backend representation.
1393 Bvariable
* backend_
;
1394 // Whether this is a global variable.
1395 bool is_global_
: 1;
1396 // Whether this is a function parameter.
1397 bool is_parameter_
: 1;
1398 // Whether this is the receiver parameter of a method.
1399 bool is_receiver_
: 1;
1400 // Whether this is the varargs parameter of a function.
1401 bool is_varargs_parameter_
: 1;
1402 // Whether this variable is ever referenced.
1404 // Whether something takes the address of this variable. For a
1405 // local variable this implies that the variable has to be on the
1407 bool is_address_taken_
: 1;
1408 // Whether something takes the address of this variable such that
1409 // the address does not escape the function.
1410 bool is_non_escaping_address_taken_
: 1;
1411 // True if we have seen this variable in a traversal.
1413 // True if we have lowered the initialization expression.
1414 bool init_is_lowered_
: 1;
1415 // True if init is a tuple used to set the type.
1416 bool type_from_init_tuple_
: 1;
1417 // True if init is a range clause and the type is the index type.
1418 bool type_from_range_index_
: 1;
1419 // True if init is a range clause and the type is the value type.
1420 bool type_from_range_value_
: 1;
1421 // True if init is a channel and the type is the channel's element type.
1422 bool type_from_chan_element_
: 1;
1423 // True if this is a variable created for a type switch case.
1424 bool is_type_switch_var_
: 1;
1425 // True if we have determined types.
1426 bool determined_type_
: 1;
1429 // A variable which is really the name for a function return value, or
1432 class Result_variable
1435 Result_variable(Type
* type
, Function
* function
, int index
,
1437 : type_(type
), function_(function
), index_(index
), location_(location
),
1438 backend_(NULL
), is_address_taken_(false),
1439 is_non_escaping_address_taken_(false)
1442 // Get the type of the result variable.
1445 { return this->type_
; }
1447 // Get the function that this is associated with.
1450 { return this->function_
; }
1452 // Index in the list of function results.
1455 { return this->index_
; }
1457 // The location of the variable definition.
1460 { return this->location_
; }
1462 // Whether this variable's address is taken.
1464 is_address_taken() const
1465 { return this->is_address_taken_
; }
1467 // Note that something takes the address of this variable.
1470 { this->is_address_taken_
= true; }
1472 // Return whether the address is taken but does not escape.
1474 is_non_escaping_address_taken() const
1475 { return this->is_non_escaping_address_taken_
; }
1477 // Note that something takes the address of this variable such that
1478 // the address does not escape the function.
1480 set_non_escaping_address_taken()
1481 { this->is_non_escaping_address_taken_
= true; }
1483 // Whether this variable should live in the heap.
1486 { return this->is_address_taken_
; }
1488 // Set the function. This is used when cloning functions which call
1491 set_function(Function
* function
)
1492 { this->function_
= function
; }
1494 // Get the backend representation of the variable.
1496 get_backend_variable(Gogo
*, Named_object
*, const std::string
&);
1499 // Type of result variable.
1501 // Function with which this is associated.
1502 Function
* function_
;
1503 // Index in list of results.
1505 // Where the result variable is defined.
1507 // Backend representation.
1508 Bvariable
* backend_
;
1509 // Whether something takes the address of this variable.
1510 bool is_address_taken_
;
1511 // Whether something takes the address of this variable such that
1512 // the address does not escape the function.
1513 bool is_non_escaping_address_taken_
;
1516 // The value we keep for a named constant. This lets us hold a type
1517 // and an expression.
1519 class Named_constant
1522 Named_constant(Type
* type
, Expression
* expr
, int iota_value
,
1524 : type_(type
), expr_(expr
), iota_value_(iota_value
), location_(location
),
1530 { return this->type_
; }
1534 { return this->expr_
; }
1538 { return this->iota_value_
; }
1542 { return this->location_
; }
1544 // Whether we are lowering.
1547 { return this->lowering_
; }
1549 // Set that we are lowering.
1552 { this->lowering_
= true; }
1554 // We are no longer lowering.
1557 { this->lowering_
= false; }
1559 // Traverse the expression.
1561 traverse_expression(Traverse
*);
1563 // Determine the type of the constant if necessary.
1567 // Indicate that we found and reported an error for this constant.
1571 // Export the constant.
1573 export_const(Export
*, const std::string
& name
) const;
1575 // Import a constant.
1577 import_const(Import
*, std::string
*, Type
**, Expression
**);
1580 // The type of the constant.
1582 // The expression for the constant.
1584 // If the predeclared constant iota is used in EXPR_, this is the
1585 // value it will have. We do this because at parse time we don't
1586 // know whether the name "iota" will refer to the predeclared
1587 // constant or to something else. We put in the right value in when
1590 // The location of the definition.
1592 // Whether we are currently lowering this constant.
1596 // A type declaration.
1598 class Type_declaration
1601 Type_declaration(Location location
)
1602 : location_(location
), in_function_(NULL
), methods_(),
1603 issued_warning_(false)
1606 // Return the location.
1609 { return this->location_
; }
1611 // Return the function in which this type is declared. This will
1612 // return NULL for a type declared in global scope.
1615 { return this->in_function_
; }
1617 // Set the function in which this type is declared.
1619 set_in_function(Named_object
* f
)
1620 { this->in_function_
= f
; }
1622 // Add a method to this type. This is used when methods are defined
1625 add_method(const std::string
& name
, Function
* function
);
1627 // Add a method declaration to this type.
1629 add_method_declaration(const std::string
& name
, Package
*,
1630 Function_type
* type
, Location location
);
1632 // Return whether any methods were defined.
1634 has_methods() const;
1636 // Define methods when the real type is known.
1638 define_methods(Named_type
*);
1640 // This is called if we are trying to use this type. It returns
1641 // true if we should issue a warning.
1646 typedef std::vector
<Named_object
*> Methods
;
1648 // The location of the type declaration.
1650 // If this type is declared in a function, a pointer back to the
1651 // function in which it is defined.
1652 Named_object
* in_function_
;
1653 // Methods defined before the type is defined.
1655 // True if we have issued a warning about a use of this type
1656 // declaration when it is undefined.
1657 bool issued_warning_
;
1660 // An unknown object. These are created by the parser for forward
1661 // references to names which have not been seen before. In a correct
1662 // program, these will always point to a real definition by the end of
1663 // the parse. Because they point to another Named_object, these may
1664 // only be referenced by Unknown_expression objects.
1669 Unknown_name(Location location
)
1670 : location_(location
), real_named_object_(NULL
)
1673 // Return the location where this name was first seen.
1676 { return this->location_
; }
1678 // Return the real named object that this points to, or NULL if it
1679 // was never resolved.
1681 real_named_object() const
1682 { return this->real_named_object_
; }
1684 // Set the real named object that this points to.
1686 set_real_named_object(Named_object
* no
);
1689 // The location where this name was first seen.
1691 // The real named object when it is known.
1696 // A named object named. This is the result of a declaration. We
1697 // don't use a superclass because they all have to be handled
1705 // An uninitialized Named_object. We should never see this.
1706 NAMED_OBJECT_UNINITIALIZED
,
1707 // An erroneous name. This indicates a parse error, to avoid
1708 // later errors about undefined references.
1709 NAMED_OBJECT_ERRONEOUS
,
1710 // An unknown name. This is used for forward references. In a
1711 // correct program, these will all be resolved by the end of the
1713 NAMED_OBJECT_UNKNOWN
,
1718 // A forward type declaration.
1719 NAMED_OBJECT_TYPE_DECLARATION
,
1722 // A result variable in a function.
1723 NAMED_OBJECT_RESULT_VAR
,
1724 // The blank identifier--the special variable named _.
1728 // A forward func declaration.
1729 NAMED_OBJECT_FUNC_DECLARATION
,
1731 NAMED_OBJECT_PACKAGE
1734 // Return the classification.
1736 classification() const
1737 { return this->classification_
; }
1742 is_erroneous() const
1743 { return this->classification_
== NAMED_OBJECT_ERRONEOUS
; }
1747 { return this->classification_
== NAMED_OBJECT_UNKNOWN
; }
1751 { return this->classification_
== NAMED_OBJECT_CONST
; }
1755 { return this->classification_
== NAMED_OBJECT_TYPE
; }
1758 is_type_declaration() const
1759 { return this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
; }
1763 { return this->classification_
== NAMED_OBJECT_VAR
; }
1766 is_result_variable() const
1767 { return this->classification_
== NAMED_OBJECT_RESULT_VAR
; }
1771 { return this->classification_
== NAMED_OBJECT_SINK
; }
1775 { return this->classification_
== NAMED_OBJECT_FUNC
; }
1778 is_function_declaration() const
1779 { return this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
; }
1783 { return this->classification_
== NAMED_OBJECT_PACKAGE
; }
1787 static Named_object
*
1788 make_erroneous_name(const std::string
& name
)
1789 { return new Named_object(name
, NULL
, NAMED_OBJECT_ERRONEOUS
); }
1791 static Named_object
*
1792 make_unknown_name(const std::string
& name
, Location
);
1794 static Named_object
*
1795 make_constant(const Typed_identifier
&, const Package
*, Expression
*,
1798 static Named_object
*
1799 make_type(const std::string
&, const Package
*, Type
*, Location
);
1801 static Named_object
*
1802 make_type_declaration(const std::string
&, const Package
*, Location
);
1804 static Named_object
*
1805 make_variable(const std::string
&, const Package
*, Variable
*);
1807 static Named_object
*
1808 make_result_variable(const std::string
&, Result_variable
*);
1810 static Named_object
*
1813 static Named_object
*
1814 make_function(const std::string
&, const Package
*, Function
*);
1816 static Named_object
*
1817 make_function_declaration(const std::string
&, const Package
*, Function_type
*,
1820 static Named_object
*
1821 make_package(const std::string
& alias
, Package
* package
);
1828 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
1829 return this->u_
.unknown_value
;
1833 unknown_value() const
1835 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
1836 return this->u_
.unknown_value
;
1842 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
1843 return this->u_
.const_value
;
1846 const Named_constant
*
1849 go_assert(this->classification_
== NAMED_OBJECT_CONST
);
1850 return this->u_
.const_value
;
1856 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
1857 return this->u_
.type_value
;
1863 go_assert(this->classification_
== NAMED_OBJECT_TYPE
);
1864 return this->u_
.type_value
;
1868 type_declaration_value()
1870 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
1871 return this->u_
.type_declaration
;
1874 const Type_declaration
*
1875 type_declaration_value() const
1877 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
1878 return this->u_
.type_declaration
;
1884 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
1885 return this->u_
.var_value
;
1891 go_assert(this->classification_
== NAMED_OBJECT_VAR
);
1892 return this->u_
.var_value
;
1898 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
1899 return this->u_
.result_var_value
;
1902 const Result_variable
*
1903 result_var_value() const
1905 go_assert(this->classification_
== NAMED_OBJECT_RESULT_VAR
);
1906 return this->u_
.result_var_value
;
1912 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
1913 return this->u_
.func_value
;
1919 go_assert(this->classification_
== NAMED_OBJECT_FUNC
);
1920 return this->u_
.func_value
;
1923 Function_declaration
*
1924 func_declaration_value()
1926 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
1927 return this->u_
.func_declaration_value
;
1930 const Function_declaration
*
1931 func_declaration_value() const
1933 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
1934 return this->u_
.func_declaration_value
;
1940 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
1941 return this->u_
.package_value
;
1945 package_value() const
1947 go_assert(this->classification_
== NAMED_OBJECT_PACKAGE
);
1948 return this->u_
.package_value
;
1953 { return this->name_
; }
1955 // Return the name to use in an error message. The difference is
1956 // that if this Named_object is defined in a different package, this
1957 // will return PACKAGE.NAME.
1959 message_name() const;
1963 { return this->package_
; }
1965 // Resolve an unknown value if possible. This returns the same
1966 // Named_object or a new one.
1970 Named_object
* ret
= this;
1971 if (this->is_unknown())
1973 Named_object
* r
= this->unknown_value()->real_named_object();
1983 const Named_object
* ret
= this;
1984 if (this->is_unknown())
1986 const Named_object
* r
= this->unknown_value()->real_named_object();
1993 // The location where this object was defined or referenced.
1997 // Convert a variable to the backend representation.
1999 get_backend_variable(Gogo
*, Named_object
* function
);
2001 // Return a tree for the external identifier for this object.
2005 // Return a tree representing this object.
2007 get_tree(Gogo
*, Named_object
* function
);
2009 // Define a type declaration.
2011 set_type_value(Named_type
*);
2013 // Define a function declaration.
2015 set_function_value(Function
*);
2017 // Declare an unknown name as a type declaration.
2021 // Export this object.
2023 export_named_object(Export
*) const;
2026 Named_object(const std::string
&, const Package
*, Classification
);
2028 // The name of the object.
2030 // The package that this object is in. This is NULL if it is in the
2031 // file we are compiling.
2032 const Package
* package_
;
2033 // The type of object this is.
2034 Classification classification_
;
2038 Unknown_name
* unknown_value
;
2039 Named_constant
* const_value
;
2040 Named_type
* type_value
;
2041 Type_declaration
* type_declaration
;
2042 Variable
* var_value
;
2043 Result_variable
* result_var_value
;
2044 Function
* func_value
;
2045 Function_declaration
* func_declaration_value
;
2046 Package
* package_value
;
2048 // The DECL tree for this object if we have already converted it.
2052 // A binding contour. This binds names to objects.
2057 // Type for mapping from names to objects.
2058 typedef Unordered_map(std::string
, Named_object
*) Contour
;
2060 Bindings(Bindings
* enclosing
);
2062 // Add an erroneous name.
2064 add_erroneous_name(const std::string
& name
)
2065 { return this->add_named_object(Named_object::make_erroneous_name(name
)); }
2067 // Add an unknown name.
2069 add_unknown_name(const std::string
& name
, Location location
)
2071 return this->add_named_object(Named_object::make_unknown_name(name
,
2077 add_constant(const Typed_identifier
& tid
, const Package
* package
,
2078 Expression
* expr
, int iota_value
)
2080 return this->add_named_object(Named_object::make_constant(tid
, package
,
2087 add_type(const std::string
& name
, const Package
* package
, Type
* type
,
2090 return this->add_named_object(Named_object::make_type(name
, package
, type
,
2094 // Add a named type. This is used for builtin types, and to add an
2095 // imported type to the global scope.
2097 add_named_type(Named_type
* named_type
);
2099 // Add a type declaration.
2101 add_type_declaration(const std::string
& name
, const Package
* package
,
2104 Named_object
* no
= Named_object::make_type_declaration(name
, package
,
2106 return this->add_named_object(no
);
2111 add_variable(const std::string
& name
, const Package
* package
,
2114 return this->add_named_object(Named_object::make_variable(name
, package
,
2118 // Add a result variable.
2120 add_result_variable(const std::string
& name
, Result_variable
* result
)
2122 return this->add_named_object(Named_object::make_result_variable(name
,
2128 add_function(const std::string
& name
, const Package
*, Function
* function
);
2130 // Add a function declaration.
2132 add_function_declaration(const std::string
& name
, const Package
* package
,
2133 Function_type
* type
, Location location
);
2135 // Add a package. The location is the location of the import
2138 add_package(const std::string
& alias
, Package
* package
)
2140 Named_object
* no
= Named_object::make_package(alias
, package
);
2141 return this->add_named_object(no
);
2144 // Define a type which was already declared.
2146 define_type(Named_object
*, Named_type
*);
2148 // Add a method to the list of objects. This is not added to the
2151 add_method(Named_object
*);
2153 // Add a named object to this binding.
2155 add_named_object(Named_object
* no
)
2156 { return this->add_named_object_to_contour(&this->bindings_
, no
); }
2158 // Clear all names in file scope from the bindings.
2162 // Look up a name in this binding contour and in any enclosing
2163 // binding contours. This returns NULL if the name is not found.
2165 lookup(const std::string
&) const;
2167 // Look up a name in this binding contour without looking in any
2168 // enclosing binding contours. Returns NULL if the name is not found.
2170 lookup_local(const std::string
&) const;
2174 remove_binding(Named_object
*);
2176 // Mark all variables as used. This is used for some types of parse
2181 // Traverse the tree. See the Traverse class.
2183 traverse(Traverse
*, bool is_global
);
2185 // Iterate over definitions. This does not include things which
2186 // were only declared.
2188 typedef std::vector
<Named_object
*>::const_iterator
2189 const_definitions_iterator
;
2191 const_definitions_iterator
2192 begin_definitions() const
2193 { return this->named_objects_
.begin(); }
2195 const_definitions_iterator
2196 end_definitions() const
2197 { return this->named_objects_
.end(); }
2199 // Return the number of definitions.
2201 size_definitions() const
2202 { return this->named_objects_
.size(); }
2204 // Return whether there are no definitions.
2206 empty_definitions() const
2207 { return this->named_objects_
.empty(); }
2209 // Iterate over declarations. This is everything that has been
2210 // declared, which includes everything which has been defined.
2212 typedef Contour::const_iterator const_declarations_iterator
;
2214 const_declarations_iterator
2215 begin_declarations() const
2216 { return this->bindings_
.begin(); }
2218 const_declarations_iterator
2219 end_declarations() const
2220 { return this->bindings_
.end(); }
2222 // Return the number of declarations.
2224 size_declarations() const
2225 { return this->bindings_
.size(); }
2227 // Return whether there are no declarations.
2229 empty_declarations() const
2230 { return this->bindings_
.empty(); }
2232 // Return the first declaration.
2235 { return this->bindings_
.empty() ? NULL
: this->bindings_
.begin()->second
; }
2239 add_named_object_to_contour(Contour
*, Named_object
*);
2242 new_definition(Named_object
*, Named_object
*);
2244 // Enclosing bindings.
2245 Bindings
* enclosing_
;
2246 // The list of objects.
2247 std::vector
<Named_object
*> named_objects_
;
2248 // The mapping from names to objects.
2257 Label(const std::string
& name
)
2258 : name_(name
), location_(Linemap::unknown_location()), snapshot_(NULL
),
2259 refs_(), is_used_(false), blabel_(NULL
)
2262 // Return the label's name.
2265 { return this->name_
; }
2267 // Return whether the label has been defined.
2270 { return !Linemap::is_unknown_location(this->location_
); }
2272 // Return whether the label has been used.
2275 { return this->is_used_
; }
2277 // Record that the label is used.
2280 { this->is_used_
= true; }
2282 // Return the location of the definition.
2285 { return this->location_
; }
2287 // Return the bindings snapshot.
2290 { return this->snapshot_
; }
2292 // Add a snapshot of a goto which refers to this label.
2294 add_snapshot_ref(Bindings_snapshot
* snapshot
)
2296 go_assert(Linemap::is_unknown_location(this->location_
));
2297 this->refs_
.push_back(snapshot
);
2300 // Return the list of snapshots of goto statements which refer to
2302 const std::vector
<Bindings_snapshot
*>&
2304 { return this->refs_
; }
2306 // Clear the references.
2310 // Define the label at LOCATION with the given bindings snapshot.
2312 define(Location location
, Bindings_snapshot
* snapshot
)
2314 go_assert(Linemap::is_unknown_location(this->location_
)
2315 && this->snapshot_
== NULL
);
2316 this->location_
= location
;
2317 this->snapshot_
= snapshot
;
2320 // Return the backend representation for this label.
2322 get_backend_label(Translate_context
*);
2324 // Return an expression for the address of this label. This is used
2325 // to get the return address of a deferred function to see whether
2326 // the function may call recover.
2328 get_addr(Translate_context
*, Location location
);
2331 // The name of the label.
2333 // The location of the definition. This is 0 if the label has not
2334 // yet been defined.
2336 // A snapshot of the set of bindings defined at this label, used to
2337 // issue errors about invalid goto statements.
2338 Bindings_snapshot
* snapshot_
;
2339 // A list of snapshots of goto statements which refer to this label.
2340 std::vector
<Bindings_snapshot
*> refs_
;
2341 // Whether the label has been used.
2343 // The backend representation.
2347 // An unnamed label. These are used when lowering loops.
2352 Unnamed_label(Location location
)
2353 : location_(location
), blabel_(NULL
)
2356 // Get the location where the label is defined.
2359 { return this->location_
; }
2361 // Set the location where the label is defined.
2363 set_location(Location location
)
2364 { this->location_
= location
; }
2366 // Return a statement which defines this label.
2368 get_definition(Translate_context
*);
2370 // Return a goto to this label from LOCATION.
2372 get_goto(Translate_context
*, Location location
);
2375 // Return the backend representation.
2377 get_blabel(Translate_context
*);
2379 // The location where the label is defined.
2381 // The backend representation of this label.
2385 // An imported package.
2390 Package(const std::string
& name
, const std::string
& unique_prefix
,
2393 // The real name of this package. This may be different from the
2394 // name in the associated Named_object if the import statement used
2398 { return this->name_
; }
2400 // Return the location of the import statement.
2403 { return this->location_
; }
2405 // Get the unique prefix used for all symbols exported from this
2408 unique_prefix() const
2410 go_assert(!this->unique_prefix_
.empty());
2411 return this->unique_prefix_
;
2414 // The priority of this package. The init function of packages with
2415 // lower priority must be run before the init function of packages
2416 // with higher priority.
2419 { return this->priority_
; }
2421 // Set the priority.
2423 set_priority(int priority
);
2425 // Return the bindings.
2428 { return this->bindings_
; }
2430 // Whether some symbol from the package was used.
2433 { return this->used_
; }
2435 // Note that some symbol from this package was used.
2438 { this->used_
= true; }
2440 // Clear the used field for the next file.
2443 { this->used_
= false; }
2445 // Whether this package was imported in the current file.
2448 { return this->is_imported_
; }
2450 // Note that this package was imported in the current file.
2453 { this->is_imported_
= true; }
2455 // Clear the imported field for the next file.
2458 { this->is_imported_
= false; }
2460 // Whether this package was imported with a name of "_".
2462 uses_sink_alias() const
2463 { return this->uses_sink_alias_
; }
2465 // Note that this package was imported with a name of "_".
2467 set_uses_sink_alias()
2468 { this->uses_sink_alias_
= true; }
2470 // Clear the sink alias field for the next file.
2472 clear_uses_sink_alias()
2473 { this->uses_sink_alias_
= false; }
2475 // Look up a name in the package. Returns NULL if the name is not
2478 lookup(const std::string
& name
) const
2479 { return this->bindings_
->lookup(name
); }
2481 // Set the location of the package. This is used if it is seen in a
2482 // different import before it is really imported.
2484 set_location(Location location
)
2485 { this->location_
= location
; }
2487 // Add a constant to the package.
2489 add_constant(const Typed_identifier
& tid
, Expression
* expr
)
2490 { return this->bindings_
->add_constant(tid
, this, expr
, 0); }
2492 // Add a type to the package.
2494 add_type(const std::string
& name
, Type
* type
, Location location
)
2495 { return this->bindings_
->add_type(name
, this, type
, location
); }
2497 // Add a type declaration to the package.
2499 add_type_declaration(const std::string
& name
, Location location
)
2500 { return this->bindings_
->add_type_declaration(name
, this, location
); }
2502 // Add a variable to the package.
2504 add_variable(const std::string
& name
, Variable
* variable
)
2505 { return this->bindings_
->add_variable(name
, this, variable
); }
2507 // Add a function declaration to the package.
2509 add_function_declaration(const std::string
& name
, Function_type
* type
,
2511 { return this->bindings_
->add_function_declaration(name
, this, type
, loc
); }
2513 // Determine types of constants.
2518 // The real name of this package.
2520 // The unique prefix for all exported global symbols.
2521 std::string unique_prefix_
;
2522 // The names in this package.
2523 Bindings
* bindings_
;
2524 // The priority of this package. A package has a priority higher
2525 // than the priority of all of the packages that it imports. This
2526 // is used to run init functions in the right order.
2528 // The location of the import statement.
2530 // True if some name from this package was used. This is mutable
2531 // because we can use a package even if we have a const pointer to
2534 // True if this package was imported in the current file.
2536 // True if this package was imported with a name of "_".
2537 bool uses_sink_alias_
;
2540 // Return codes for the traversal functions. This is not an enum
2541 // because we want to be able to declare traversal functions in other
2542 // header files without including this one.
2544 // Continue traversal as usual.
2545 const int TRAVERSE_CONTINUE
= -1;
2548 const int TRAVERSE_EXIT
= 0;
2550 // Continue traversal, but skip components of the current object.
2551 // E.g., if this is returned by Traverse::statement, we do not
2552 // traverse the expressions in the statement even if
2553 // traverse_expressions is set in the traverse_mask.
2554 const int TRAVERSE_SKIP_COMPONENTS
= 1;
2556 // This class is used when traversing the parse tree. The caller uses
2557 // a subclass which overrides functions as desired.
2562 // These bitmasks say what to traverse.
2563 static const unsigned int traverse_variables
= 0x1;
2564 static const unsigned int traverse_constants
= 0x2;
2565 static const unsigned int traverse_functions
= 0x4;
2566 static const unsigned int traverse_blocks
= 0x8;
2567 static const unsigned int traverse_statements
= 0x10;
2568 static const unsigned int traverse_expressions
= 0x20;
2569 static const unsigned int traverse_types
= 0x40;
2571 Traverse(unsigned int traverse_mask
)
2572 : traverse_mask_(traverse_mask
), types_seen_(NULL
), expressions_seen_(NULL
)
2575 virtual ~Traverse();
2577 // The bitmask of what to traverse.
2579 traverse_mask() const
2580 { return this->traverse_mask_
; }
2582 // Record that we are going to traverse a type. This returns true
2583 // if the type has already been seen in this traversal. This is
2584 // required because types, unlike expressions, can form a circular
2587 remember_type(const Type
*);
2589 // Record that we are going to see an expression. This returns true
2590 // if the expression has already been seen in this traversal. This
2591 // is only needed for cases where multiple expressions can point to
2594 remember_expression(const Expression
*);
2596 // These functions return one of the TRAVERSE codes defined above.
2598 // If traverse_variables is set in the mask, this is called for
2599 // every variable in the tree.
2601 variable(Named_object
*);
2603 // If traverse_constants is set in the mask, this is called for
2604 // every named constant in the tree. The bool parameter is true for
2605 // a global constant.
2607 constant(Named_object
*, bool);
2609 // If traverse_functions is set in the mask, this is called for
2610 // every function in the tree.
2612 function(Named_object
*);
2614 // If traverse_blocks is set in the mask, this is called for every
2615 // block in the tree.
2619 // If traverse_statements is set in the mask, this is called for
2620 // every statement in the tree.
2622 statement(Block
*, size_t* index
, Statement
*);
2624 // If traverse_expressions is set in the mask, this is called for
2625 // every expression in the tree.
2627 expression(Expression
**);
2629 // If traverse_types is set in the mask, this is called for every
2630 // type in the tree.
2635 // A hash table for types we have seen during this traversal. Note
2636 // that this uses the default hash functions for pointers rather
2637 // than Type_hash_identical and Type_identical. This is because for
2638 // traversal we care about seeing a specific type structure. If
2639 // there are two separate instances of identical types, we want to
2641 typedef Unordered_set(const Type
*) Types_seen
;
2643 typedef Unordered_set(const Expression
*) Expressions_seen
;
2645 // Bitmask of what sort of objects to traverse.
2646 unsigned int traverse_mask_
;
2647 // Types which have been seen in this traversal.
2648 Types_seen
* types_seen_
;
2649 // Expressions which have been seen in this traversal.
2650 Expressions_seen
* expressions_seen_
;
2653 // A class which makes it easier to insert new statements before the
2654 // current statement during a traversal.
2656 class Statement_inserter
2659 // Empty constructor.
2660 Statement_inserter()
2661 : block_(NULL
), pindex_(NULL
), gogo_(NULL
), var_(NULL
)
2664 // Constructor for a statement in a block.
2665 Statement_inserter(Block
* block
, size_t *pindex
)
2666 : block_(block
), pindex_(pindex
), gogo_(NULL
), var_(NULL
)
2669 // Constructor for a global variable.
2670 Statement_inserter(Gogo
* gogo
, Variable
* var
)
2671 : block_(NULL
), pindex_(NULL
), gogo_(gogo
), var_(var
)
2672 { go_assert(var
->is_global()); }
2674 // We use the default copy constructor and assignment operator.
2676 // Insert S before the statement we are traversing, or before the
2677 // initialization expression of a global variable.
2679 insert(Statement
* s
);
2682 // The block that the statement is in.
2684 // The index of the statement that we are traversing.
2686 // The IR, needed when looking at an initializer expression for a
2689 // The global variable, when looking at an initializer expression.
2693 // When translating the gogo IR into the backend data structure, this
2694 // is the context we pass down the blocks and statements.
2696 class Translate_context
2699 Translate_context(Gogo
* gogo
, Named_object
* function
, Block
* block
,
2701 : gogo_(gogo
), backend_(gogo
->backend()), function_(function
),
2702 block_(block
), bblock_(bblock
), is_const_(false)
2709 { return this->gogo_
; }
2713 { return this->backend_
; }
2717 { return this->function_
; }
2721 { return this->block_
; }
2725 { return this->bblock_
; }
2729 { return this->is_const_
; }
2731 // Make a constant context.
2734 { this->is_const_
= true; }
2737 // The IR for the entire compilation unit.
2739 // The generator for the backend data structures.
2741 // The function we are currently translating. NULL if not in a
2742 // function, e.g., the initializer of a global variable.
2743 Named_object
* function_
;
2744 // The block we are currently translating. NULL if not in a
2747 // The backend representation of the current block. NULL if block_
2750 // Whether this is being evaluated in a constant context. This is
2751 // used for type descriptor initializers.
2755 // Runtime error codes. These must match the values in
2756 // libgo/runtime/go-runtime-error.c.
2758 // Slice index out of bounds: negative or larger than the length of
2760 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
= 0;
2762 // Array index out of bounds.
2763 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
= 1;
2765 // String index out of bounds.
2766 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
= 2;
2768 // Slice slice out of bounds: negative or larger than the length of
2769 // the slice or high bound less than low bound.
2770 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
= 3;
2772 // Array slice out of bounds.
2773 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
= 4;
2775 // String slice out of bounds.
2776 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
= 5;
2778 // Dereference of nil pointer. This is used when there is a
2779 // dereference of a pointer to a very large struct or array, to ensure
2780 // that a gigantic array is not used a proxy to access random memory
2782 static const int RUNTIME_ERROR_NIL_DEREFERENCE
= 6;
2784 // Slice length or capacity out of bounds in make: negative or
2785 // overflow or length greater than capacity.
2786 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS
= 7;
2788 // Map capacity out of bounds in make: negative or overflow.
2789 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS
= 8;
2791 // Channel capacity out of bounds in make: negative or overflow.
2792 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS
= 9;
2794 // Division by zero.
2795 static const int RUNTIME_ERROR_DIVISION_BY_ZERO
= 10;
2797 // This is used by some of the langhooks.
2798 extern Gogo
* go_get_gogo();
2800 // Whether we have seen any errors. FIXME: Replace with a backend
2802 extern bool saw_errors();
2804 #endif // !defined(GO_GOGO_H)