1 // gogo.cc -- Go frontend parsed representation.
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.
15 #include "statements.h"
16 #include "expressions.h"
26 Gogo::Gogo(Backend
* backend
, Linemap
* linemap
, int int_type_size
,
32 globals_(new Bindings(NULL
)),
34 imported_unsafe_(false),
45 pkgpath_from_option_(false),
46 prefix_from_option_(false),
47 relative_import_path_(),
50 specific_type_functions_(),
51 specific_type_functions_are_written_(false),
52 named_types_are_converted_(false)
54 const Location loc
= Linemap::predeclared_location();
56 Named_type
* uint8_type
= Type::make_integer_type("uint8", true, 8,
57 RUNTIME_TYPE_KIND_UINT8
);
58 this->add_named_type(uint8_type
);
59 this->add_named_type(Type::make_integer_type("uint16", true, 16,
60 RUNTIME_TYPE_KIND_UINT16
));
61 this->add_named_type(Type::make_integer_type("uint32", true, 32,
62 RUNTIME_TYPE_KIND_UINT32
));
63 this->add_named_type(Type::make_integer_type("uint64", true, 64,
64 RUNTIME_TYPE_KIND_UINT64
));
66 this->add_named_type(Type::make_integer_type("int8", false, 8,
67 RUNTIME_TYPE_KIND_INT8
));
68 this->add_named_type(Type::make_integer_type("int16", false, 16,
69 RUNTIME_TYPE_KIND_INT16
));
70 Named_type
* int32_type
= Type::make_integer_type("int32", false, 32,
71 RUNTIME_TYPE_KIND_INT32
);
72 this->add_named_type(int32_type
);
73 this->add_named_type(Type::make_integer_type("int64", false, 64,
74 RUNTIME_TYPE_KIND_INT64
));
76 this->add_named_type(Type::make_float_type("float32", 32,
77 RUNTIME_TYPE_KIND_FLOAT32
));
78 this->add_named_type(Type::make_float_type("float64", 64,
79 RUNTIME_TYPE_KIND_FLOAT64
));
81 this->add_named_type(Type::make_complex_type("complex64", 64,
82 RUNTIME_TYPE_KIND_COMPLEX64
));
83 this->add_named_type(Type::make_complex_type("complex128", 128,
84 RUNTIME_TYPE_KIND_COMPLEX128
));
86 if (int_type_size
< 32)
88 this->add_named_type(Type::make_integer_type("uint", true,
90 RUNTIME_TYPE_KIND_UINT
));
91 Named_type
* int_type
= Type::make_integer_type("int", false, int_type_size
,
92 RUNTIME_TYPE_KIND_INT
);
93 this->add_named_type(int_type
);
95 this->add_named_type(Type::make_integer_type("uintptr", true,
97 RUNTIME_TYPE_KIND_UINTPTR
));
99 // "byte" is an alias for "uint8".
100 uint8_type
->integer_type()->set_is_byte();
101 Named_object
* byte_type
= Named_object::make_type("byte", NULL
, uint8_type
,
103 this->add_named_type(byte_type
->type_value());
105 // "rune" is an alias for "int32".
106 int32_type
->integer_type()->set_is_rune();
107 Named_object
* rune_type
= Named_object::make_type("rune", NULL
, int32_type
,
109 this->add_named_type(rune_type
->type_value());
111 this->add_named_type(Type::make_named_bool_type());
113 this->add_named_type(Type::make_named_string_type());
115 // "error" is interface { Error() string }.
117 Typed_identifier_list
*methods
= new Typed_identifier_list
;
118 Typed_identifier_list
*results
= new Typed_identifier_list
;
119 results
->push_back(Typed_identifier("", Type::lookup_string_type(), loc
));
120 Type
*method_type
= Type::make_function_type(NULL
, NULL
, results
, loc
);
121 methods
->push_back(Typed_identifier("Error", method_type
, loc
));
122 Interface_type
*error_iface
= Type::make_interface_type(methods
, loc
);
123 error_iface
->finalize_methods();
124 Named_type
*error_type
= Named_object::make_type("error", NULL
, error_iface
, loc
)->type_value();
125 this->add_named_type(error_type
);
128 this->globals_
->add_constant(Typed_identifier("true",
129 Type::make_boolean_type(),
132 Expression::make_boolean(true, loc
),
134 this->globals_
->add_constant(Typed_identifier("false",
135 Type::make_boolean_type(),
138 Expression::make_boolean(false, loc
),
141 this->globals_
->add_constant(Typed_identifier("nil", Type::make_nil_type(),
144 Expression::make_nil(loc
),
147 Type
* abstract_int_type
= Type::make_abstract_integer_type();
148 this->globals_
->add_constant(Typed_identifier("iota", abstract_int_type
,
151 Expression::make_iota(),
154 Function_type
* new_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
155 new_type
->set_is_varargs();
156 new_type
->set_is_builtin();
157 this->globals_
->add_function_declaration("new", NULL
, new_type
, loc
);
159 Function_type
* make_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
160 make_type
->set_is_varargs();
161 make_type
->set_is_builtin();
162 this->globals_
->add_function_declaration("make", NULL
, make_type
, loc
);
164 Typed_identifier_list
* len_result
= new Typed_identifier_list();
165 len_result
->push_back(Typed_identifier("", int_type
, loc
));
166 Function_type
* len_type
= Type::make_function_type(NULL
, NULL
, len_result
,
168 len_type
->set_is_builtin();
169 this->globals_
->add_function_declaration("len", NULL
, len_type
, loc
);
171 Typed_identifier_list
* cap_result
= new Typed_identifier_list();
172 cap_result
->push_back(Typed_identifier("", int_type
, loc
));
173 Function_type
* cap_type
= Type::make_function_type(NULL
, NULL
, len_result
,
175 cap_type
->set_is_builtin();
176 this->globals_
->add_function_declaration("cap", NULL
, cap_type
, loc
);
178 Function_type
* print_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
179 print_type
->set_is_varargs();
180 print_type
->set_is_builtin();
181 this->globals_
->add_function_declaration("print", NULL
, print_type
, loc
);
183 print_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
184 print_type
->set_is_varargs();
185 print_type
->set_is_builtin();
186 this->globals_
->add_function_declaration("println", NULL
, print_type
, loc
);
188 Type
*empty
= Type::make_empty_interface_type(loc
);
189 Typed_identifier_list
* panic_parms
= new Typed_identifier_list();
190 panic_parms
->push_back(Typed_identifier("e", empty
, loc
));
191 Function_type
*panic_type
= Type::make_function_type(NULL
, panic_parms
,
193 panic_type
->set_is_builtin();
194 this->globals_
->add_function_declaration("panic", NULL
, panic_type
, loc
);
196 Typed_identifier_list
* recover_result
= new Typed_identifier_list();
197 recover_result
->push_back(Typed_identifier("", empty
, loc
));
198 Function_type
* recover_type
= Type::make_function_type(NULL
, NULL
,
201 recover_type
->set_is_builtin();
202 this->globals_
->add_function_declaration("recover", NULL
, recover_type
, loc
);
204 Function_type
* close_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
205 close_type
->set_is_varargs();
206 close_type
->set_is_builtin();
207 this->globals_
->add_function_declaration("close", NULL
, close_type
, loc
);
209 Typed_identifier_list
* copy_result
= new Typed_identifier_list();
210 copy_result
->push_back(Typed_identifier("", int_type
, loc
));
211 Function_type
* copy_type
= Type::make_function_type(NULL
, NULL
,
213 copy_type
->set_is_varargs();
214 copy_type
->set_is_builtin();
215 this->globals_
->add_function_declaration("copy", NULL
, copy_type
, loc
);
217 Function_type
* append_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
218 append_type
->set_is_varargs();
219 append_type
->set_is_builtin();
220 this->globals_
->add_function_declaration("append", NULL
, append_type
, loc
);
222 Function_type
* complex_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
223 complex_type
->set_is_varargs();
224 complex_type
->set_is_builtin();
225 this->globals_
->add_function_declaration("complex", NULL
, complex_type
, loc
);
227 Function_type
* real_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
228 real_type
->set_is_varargs();
229 real_type
->set_is_builtin();
230 this->globals_
->add_function_declaration("real", NULL
, real_type
, loc
);
232 Function_type
* imag_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
233 imag_type
->set_is_varargs();
234 imag_type
->set_is_builtin();
235 this->globals_
->add_function_declaration("imag", NULL
, imag_type
, loc
);
237 Function_type
* delete_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
238 delete_type
->set_is_varargs();
239 delete_type
->set_is_builtin();
240 this->globals_
->add_function_declaration("delete", NULL
, delete_type
, loc
);
243 // Convert a pkgpath into a string suitable for a symbol. Note that
244 // this transformation is convenient but imperfect. A -fgo-pkgpath
245 // option of a/b_c will conflict with a -fgo-pkgpath option of a_b/c,
246 // possibly leading to link time errors.
249 Gogo::pkgpath_for_symbol(const std::string
& pkgpath
)
251 std::string s
= pkgpath
;
252 for (size_t i
= 0; i
< s
.length(); ++i
)
255 if ((c
>= 'a' && c
<= 'z')
256 || (c
>= 'A' && c
<= 'Z')
257 || (c
>= '0' && c
<= '9')
268 // Get the package path to use for type reflection data. This should
269 // ideally be unique across the entire link.
272 Gogo::pkgpath() const
274 go_assert(this->pkgpath_set_
);
275 return this->pkgpath_
;
278 // Set the package path from the -fgo-pkgpath command line option.
281 Gogo::set_pkgpath(const std::string
& arg
)
283 go_assert(!this->pkgpath_set_
);
284 this->pkgpath_
= arg
;
285 this->pkgpath_set_
= true;
286 this->pkgpath_from_option_
= true;
289 // Get the package path to use for symbol names.
292 Gogo::pkgpath_symbol() const
294 go_assert(this->pkgpath_set_
);
295 return this->pkgpath_symbol_
;
298 // Set the unique prefix to use to determine the package path, from
299 // the -fgo-prefix command line option.
302 Gogo::set_prefix(const std::string
& arg
)
304 go_assert(!this->prefix_from_option_
);
306 this->prefix_from_option_
= true;
309 // Munge name for use in an error message.
312 Gogo::message_name(const std::string
& name
)
314 return go_localize_identifier(Gogo::unpack_hidden_name(name
).c_str());
317 // Get the package name.
320 Gogo::package_name() const
322 go_assert(this->package_
!= NULL
);
323 return this->package_
->package_name();
326 // Set the package name.
329 Gogo::set_package_name(const std::string
& package_name
,
332 if (this->package_
!= NULL
)
334 if (this->package_
->package_name() != package_name
)
335 error_at(location
, "expected package %<%s%>",
336 Gogo::message_name(this->package_
->package_name()).c_str());
340 // Now that we know the name of the package we are compiling, set
341 // the package path to use for reflect.Type.PkgPath and global
343 if (!this->pkgpath_set_
)
345 if (!this->prefix_from_option_
&& package_name
== "main")
346 this->pkgpath_
= package_name
;
349 if (!this->prefix_from_option_
)
350 this->prefix_
= "go";
351 this->pkgpath_
= this->prefix_
+ '.' + package_name
;
353 this->pkgpath_set_
= true;
356 this->pkgpath_symbol_
= Gogo::pkgpath_for_symbol(this->pkgpath_
);
358 this->package_
= this->register_package(this->pkgpath_
, location
);
359 this->package_
->set_package_name(package_name
, location
);
361 if (this->is_main_package())
363 // Declare "main" as a function which takes no parameters and
365 Location uloc
= Linemap::unknown_location();
366 this->declare_function("main",
367 Type::make_function_type (NULL
, NULL
, NULL
, uloc
),
372 // Return whether this is the "main" package. This is not true if
373 // -fgo-pkgpath or -fgo-prefix was used.
376 Gogo::is_main_package() const
378 return (this->package_name() == "main"
379 && !this->pkgpath_from_option_
380 && !this->prefix_from_option_
);
386 Gogo::import_package(const std::string
& filename
,
387 const std::string
& local_name
,
388 bool is_local_name_exported
,
391 if (filename
.empty())
393 error_at(location
, "import path is empty");
397 const char *pf
= filename
.data();
398 const char *pend
= pf
+ filename
.length();
402 int adv
= Lex::fetch_char(pf
, &c
);
405 error_at(location
, "import path contains invalid UTF-8 sequence");
410 error_at(location
, "import path contains NUL");
413 if (c
< 0x20 || c
== 0x7f)
415 error_at(location
, "import path contains control character");
420 error_at(location
, "import path contains backslash; use slash");
423 if (Lex::is_unicode_space(c
))
425 error_at(location
, "import path contains space character");
428 if (c
< 0x7f && strchr("!\"#$%&'()*,:;<=>?[]^`{|}", c
) != NULL
)
430 error_at(location
, "import path contains invalid character '%c'", c
);
436 if (IS_ABSOLUTE_PATH(filename
.c_str()))
438 error_at(location
, "import path cannot be absolute path");
442 if (filename
== "unsafe")
444 this->import_unsafe(local_name
, is_local_name_exported
, location
);
448 Imports::const_iterator p
= this->imports_
.find(filename
);
449 if (p
!= this->imports_
.end())
451 Package
* package
= p
->second
;
452 package
->set_location(location
);
453 package
->set_is_imported();
454 std::string ln
= local_name
;
455 bool is_ln_exported
= is_local_name_exported
;
458 ln
= package
->package_name();
459 go_assert(!ln
.empty());
460 is_ln_exported
= Lex::is_exported_name(ln
);
464 Bindings
* bindings
= package
->bindings();
465 for (Bindings::const_declarations_iterator p
=
466 bindings
->begin_declarations();
467 p
!= bindings
->end_declarations();
469 this->add_named_object(p
->second
);
472 package
->set_uses_sink_alias();
475 ln
= this->pack_hidden_name(ln
, is_ln_exported
);
476 this->package_
->bindings()->add_package(ln
, package
);
481 Import::Stream
* stream
= Import::open_package(filename
, location
,
482 this->relative_import_path_
);
485 error_at(location
, "import file %qs not found", filename
.c_str());
489 Import
imp(stream
, location
);
490 imp
.register_builtin_types(this);
491 Package
* package
= imp
.import(this, local_name
, is_local_name_exported
);
494 if (package
->pkgpath() == this->pkgpath())
496 ("imported package uses same package path as package "
497 "being compiled (see -fgo-pkgpath option)"));
499 this->imports_
.insert(std::make_pair(filename
, package
));
500 package
->set_is_imported();
506 // Add an import control function for an imported package to the list.
509 Gogo::add_import_init_fn(const std::string
& package_name
,
510 const std::string
& init_name
, int prio
)
512 for (std::set
<Import_init
>::const_iterator p
=
513 this->imported_init_fns_
.begin();
514 p
!= this->imported_init_fns_
.end();
517 if (p
->init_name() == init_name
518 && (p
->package_name() != package_name
|| p
->priority() != prio
))
520 error("duplicate package initialization name %qs",
521 Gogo::message_name(init_name
).c_str());
522 inform(UNKNOWN_LOCATION
, "used by package %qs at priority %d",
523 Gogo::message_name(p
->package_name()).c_str(),
525 inform(UNKNOWN_LOCATION
, " and by package %qs at priority %d",
526 Gogo::message_name(package_name
).c_str(), prio
);
531 this->imported_init_fns_
.insert(Import_init(package_name
, init_name
,
535 // Return whether we are at the global binding level.
538 Gogo::in_global_scope() const
540 return this->functions_
.empty();
543 // Return the current binding contour.
546 Gogo::current_bindings()
548 if (!this->functions_
.empty())
549 return this->functions_
.back().blocks
.back()->bindings();
550 else if (this->package_
!= NULL
)
551 return this->package_
->bindings();
553 return this->globals_
;
557 Gogo::current_bindings() const
559 if (!this->functions_
.empty())
560 return this->functions_
.back().blocks
.back()->bindings();
561 else if (this->package_
!= NULL
)
562 return this->package_
->bindings();
564 return this->globals_
;
567 // Return the current block.
570 Gogo::current_block()
572 if (this->functions_
.empty())
575 return this->functions_
.back().blocks
.back();
578 // Look up a name in the current binding contour. If PFUNCTION is not
579 // NULL, set it to the function in which the name is defined, or NULL
580 // if the name is defined in global scope.
583 Gogo::lookup(const std::string
& name
, Named_object
** pfunction
) const
585 if (pfunction
!= NULL
)
588 if (Gogo::is_sink_name(name
))
589 return Named_object::make_sink();
591 for (Open_functions::const_reverse_iterator p
= this->functions_
.rbegin();
592 p
!= this->functions_
.rend();
595 Named_object
* ret
= p
->blocks
.back()->bindings()->lookup(name
);
598 if (pfunction
!= NULL
)
599 *pfunction
= p
->function
;
604 if (this->package_
!= NULL
)
606 Named_object
* ret
= this->package_
->bindings()->lookup(name
);
609 if (ret
->package() != NULL
)
610 ret
->package()->set_used();
615 // We do not look in the global namespace. If we did, the global
616 // namespace would effectively hide names which were defined in
617 // package scope which we have not yet seen. Instead,
618 // define_global_names is called after parsing is over to connect
619 // undefined names at package scope with names defined at global
625 // Look up a name in the current block, without searching enclosing
629 Gogo::lookup_in_block(const std::string
& name
) const
631 go_assert(!this->functions_
.empty());
632 go_assert(!this->functions_
.back().blocks
.empty());
633 return this->functions_
.back().blocks
.back()->bindings()->lookup_local(name
);
636 // Look up a name in the global namespace.
639 Gogo::lookup_global(const char* name
) const
641 return this->globals_
->lookup(name
);
644 // Add an imported package.
647 Gogo::add_imported_package(const std::string
& real_name
,
648 const std::string
& alias_arg
,
649 bool is_alias_exported
,
650 const std::string
& pkgpath
,
652 bool* padd_to_globals
)
654 Package
* ret
= this->register_package(pkgpath
, location
);
655 ret
->set_package_name(real_name
, location
);
657 *padd_to_globals
= false;
659 if (alias_arg
== ".")
660 *padd_to_globals
= true;
661 else if (alias_arg
== "_")
662 ret
->set_uses_sink_alias();
665 std::string alias
= alias_arg
;
669 is_alias_exported
= Lex::is_exported_name(alias
);
671 alias
= this->pack_hidden_name(alias
, is_alias_exported
);
672 Named_object
* no
= this->package_
->bindings()->add_package(alias
, ret
);
673 if (!no
->is_package())
680 // Register a package. This package may or may not be imported. This
681 // returns the Package structure for the package, creating if it
682 // necessary. LOCATION is the location of the import statement that
683 // led us to see this package.
686 Gogo::register_package(const std::string
& pkgpath
, Location location
)
688 Package
* package
= NULL
;
689 std::pair
<Packages::iterator
, bool> ins
=
690 this->packages_
.insert(std::make_pair(pkgpath
, package
));
693 // We have seen this package name before.
694 package
= ins
.first
->second
;
695 go_assert(package
!= NULL
&& package
->pkgpath() == pkgpath
);
696 if (Linemap::is_unknown_location(package
->location()))
697 package
->set_location(location
);
701 // First time we have seen this package name.
702 package
= new Package(pkgpath
, location
);
703 go_assert(ins
.first
->second
== NULL
);
704 ins
.first
->second
= package
;
710 // Start compiling a function.
713 Gogo::start_function(const std::string
& name
, Function_type
* type
,
714 bool add_method_to_type
, Location location
)
716 bool at_top_level
= this->functions_
.empty();
718 Block
* block
= new Block(NULL
, location
);
720 Function
* enclosing
= (at_top_level
722 : this->functions_
.back().function
->func_value());
724 Function
* function
= new Function(type
, enclosing
, block
, location
);
726 if (type
->is_method())
728 const Typed_identifier
* receiver
= type
->receiver();
729 Variable
* this_param
= new Variable(receiver
->type(), NULL
, false,
730 true, true, location
);
731 std::string rname
= receiver
->name();
732 if (rname
.empty() || Gogo::is_sink_name(rname
))
734 // We need to give receivers a name since they wind up in
735 // DECL_ARGUMENTS. FIXME.
736 static unsigned int count
;
738 snprintf(buf
, sizeof buf
, "r.%u", count
);
742 block
->bindings()->add_variable(rname
, NULL
, this_param
);
745 const Typed_identifier_list
* parameters
= type
->parameters();
746 bool is_varargs
= type
->is_varargs();
747 if (parameters
!= NULL
)
749 for (Typed_identifier_list::const_iterator p
= parameters
->begin();
750 p
!= parameters
->end();
753 Variable
* param
= new Variable(p
->type(), NULL
, false, true, false,
755 if (is_varargs
&& p
+ 1 == parameters
->end())
756 param
->set_is_varargs_parameter();
758 std::string pname
= p
->name();
759 if (pname
.empty() || Gogo::is_sink_name(pname
))
761 // We need to give parameters a name since they wind up
762 // in DECL_ARGUMENTS. FIXME.
763 static unsigned int count
;
765 snprintf(buf
, sizeof buf
, "p.%u", count
);
769 block
->bindings()->add_variable(pname
, NULL
, param
);
773 function
->create_result_variables(this);
775 const std::string
* pname
;
776 std::string nested_name
;
777 bool is_init
= false;
778 if (Gogo::unpack_hidden_name(name
) == "init" && !type
->is_method())
780 if ((type
->parameters() != NULL
&& !type
->parameters()->empty())
781 || (type
->results() != NULL
&& !type
->results()->empty()))
783 "func init must have no arguments and no return values");
784 // There can be multiple "init" functions, so give them each a
786 static int init_count
;
788 snprintf(buf
, sizeof buf
, ".$init%d", init_count
);
791 pname
= &nested_name
;
794 else if (!name
.empty())
798 // Invent a name for a nested function.
799 static int nested_count
;
801 snprintf(buf
, sizeof buf
, ".$nested%d", nested_count
);
804 pname
= &nested_name
;
808 if (Gogo::is_sink_name(*pname
))
810 static int sink_count
;
812 snprintf(buf
, sizeof buf
, ".$sink%d", sink_count
);
814 ret
= Named_object::make_function(buf
, NULL
, function
);
816 else if (!type
->is_method())
818 ret
= this->package_
->bindings()->add_function(*pname
, NULL
, function
);
819 if (!ret
->is_function() || ret
->func_value() != function
)
821 // Redefinition error. Invent a name to avoid knockon
823 static int redefinition_count
;
825 snprintf(buf
, sizeof buf
, ".$redefined%d", redefinition_count
);
826 ++redefinition_count
;
827 ret
= this->package_
->bindings()->add_function(buf
, NULL
, function
);
832 if (!add_method_to_type
)
833 ret
= Named_object::make_function(name
, NULL
, function
);
836 go_assert(at_top_level
);
837 Type
* rtype
= type
->receiver()->type();
839 // We want to look through the pointer created by the
840 // parser, without getting an error if the type is not yet
842 if (rtype
->classification() == Type::TYPE_POINTER
)
843 rtype
= rtype
->points_to();
845 if (rtype
->is_error_type())
846 ret
= Named_object::make_function(name
, NULL
, function
);
847 else if (rtype
->named_type() != NULL
)
849 ret
= rtype
->named_type()->add_method(name
, function
);
850 if (!ret
->is_function())
852 // Redefinition error.
853 ret
= Named_object::make_function(name
, NULL
, function
);
856 else if (rtype
->forward_declaration_type() != NULL
)
858 Named_object
* type_no
=
859 rtype
->forward_declaration_type()->named_object();
860 if (type_no
->is_unknown())
862 // If we are seeing methods it really must be a
863 // type. Declare it as such. An alternative would
864 // be to support lists of methods for unknown
865 // expressions. Either way the error messages if
866 // this is not a type are going to get confusing.
867 Named_object
* declared
=
868 this->declare_package_type(type_no
->name(),
869 type_no
->location());
871 == type_no
->unknown_value()->real_named_object());
873 ret
= rtype
->forward_declaration_type()->add_method(name
,
879 this->package_
->bindings()->add_method(ret
);
882 this->functions_
.resize(this->functions_
.size() + 1);
883 Open_function
& of(this->functions_
.back());
885 of
.blocks
.push_back(block
);
889 this->init_functions_
.push_back(ret
);
890 this->need_init_fn_
= true;
896 // Finish compiling a function.
899 Gogo::finish_function(Location location
)
901 this->finish_block(location
);
902 go_assert(this->functions_
.back().blocks
.empty());
903 this->functions_
.pop_back();
906 // Return the current function.
909 Gogo::current_function() const
911 go_assert(!this->functions_
.empty());
912 return this->functions_
.back().function
;
915 // Start a new block.
918 Gogo::start_block(Location location
)
920 go_assert(!this->functions_
.empty());
921 Block
* block
= new Block(this->current_block(), location
);
922 this->functions_
.back().blocks
.push_back(block
);
928 Gogo::finish_block(Location location
)
930 go_assert(!this->functions_
.empty());
931 go_assert(!this->functions_
.back().blocks
.empty());
932 Block
* block
= this->functions_
.back().blocks
.back();
933 this->functions_
.back().blocks
.pop_back();
934 block
->set_end_location(location
);
938 // Add an erroneous name.
941 Gogo::add_erroneous_name(const std::string
& name
)
943 return this->package_
->bindings()->add_erroneous_name(name
);
946 // Add an unknown name.
949 Gogo::add_unknown_name(const std::string
& name
, Location location
)
951 return this->package_
->bindings()->add_unknown_name(name
, location
);
954 // Declare a function.
957 Gogo::declare_function(const std::string
& name
, Function_type
* type
,
960 if (!type
->is_method())
961 return this->current_bindings()->add_function_declaration(name
, NULL
, type
,
965 // We don't bother to add this to the list of global
967 Type
* rtype
= type
->receiver()->type();
969 // We want to look through the pointer created by the
970 // parser, without getting an error if the type is not yet
972 if (rtype
->classification() == Type::TYPE_POINTER
)
973 rtype
= rtype
->points_to();
975 if (rtype
->is_error_type())
977 else if (rtype
->named_type() != NULL
)
978 return rtype
->named_type()->add_method_declaration(name
, NULL
, type
,
980 else if (rtype
->forward_declaration_type() != NULL
)
982 Forward_declaration_type
* ftype
= rtype
->forward_declaration_type();
983 return ftype
->add_method_declaration(name
, NULL
, type
, location
);
990 // Add a label definition.
993 Gogo::add_label_definition(const std::string
& label_name
,
996 go_assert(!this->functions_
.empty());
997 Function
* func
= this->functions_
.back().function
->func_value();
998 Label
* label
= func
->add_label_definition(this, label_name
, location
);
999 this->add_statement(Statement::make_label_statement(label
, location
));
1003 // Add a label reference.
1006 Gogo::add_label_reference(const std::string
& label_name
,
1007 Location location
, bool issue_goto_errors
)
1009 go_assert(!this->functions_
.empty());
1010 Function
* func
= this->functions_
.back().function
->func_value();
1011 return func
->add_label_reference(this, label_name
, location
,
1015 // Return the current binding state.
1018 Gogo::bindings_snapshot(Location location
)
1020 return new Bindings_snapshot(this->current_block(), location
);
1026 Gogo::add_statement(Statement
* statement
)
1028 go_assert(!this->functions_
.empty()
1029 && !this->functions_
.back().blocks
.empty());
1030 this->functions_
.back().blocks
.back()->add_statement(statement
);
1036 Gogo::add_block(Block
* block
, Location location
)
1038 go_assert(!this->functions_
.empty()
1039 && !this->functions_
.back().blocks
.empty());
1040 Statement
* statement
= Statement::make_block_statement(block
, location
);
1041 this->functions_
.back().blocks
.back()->add_statement(statement
);
1047 Gogo::add_constant(const Typed_identifier
& tid
, Expression
* expr
,
1050 return this->current_bindings()->add_constant(tid
, NULL
, expr
, iota_value
);
1056 Gogo::add_type(const std::string
& name
, Type
* type
, Location location
)
1058 Named_object
* no
= this->current_bindings()->add_type(name
, NULL
, type
,
1060 if (!this->in_global_scope() && no
->is_type())
1062 Named_object
* f
= this->functions_
.back().function
;
1064 if (f
->is_function())
1065 index
= f
->func_value()->new_local_type_index();
1068 no
->type_value()->set_in_function(f
, index
);
1072 // Add a named type.
1075 Gogo::add_named_type(Named_type
* type
)
1077 go_assert(this->in_global_scope());
1078 this->current_bindings()->add_named_type(type
);
1084 Gogo::declare_type(const std::string
& name
, Location location
)
1086 Bindings
* bindings
= this->current_bindings();
1087 Named_object
* no
= bindings
->add_type_declaration(name
, NULL
, location
);
1088 if (!this->in_global_scope() && no
->is_type_declaration())
1090 Named_object
* f
= this->functions_
.back().function
;
1092 if (f
->is_function())
1093 index
= f
->func_value()->new_local_type_index();
1096 no
->type_declaration_value()->set_in_function(f
, index
);
1101 // Declare a type at the package level.
1104 Gogo::declare_package_type(const std::string
& name
, Location location
)
1106 return this->package_
->bindings()->add_type_declaration(name
, NULL
, location
);
1109 // Declare a function at the package level.
1112 Gogo::declare_package_function(const std::string
& name
, Function_type
* type
,
1115 return this->package_
->bindings()->add_function_declaration(name
, NULL
, type
,
1119 // Define a type which was already declared.
1122 Gogo::define_type(Named_object
* no
, Named_type
* type
)
1124 this->current_bindings()->define_type(no
, type
);
1130 Gogo::add_variable(const std::string
& name
, Variable
* variable
)
1132 Named_object
* no
= this->current_bindings()->add_variable(name
, NULL
,
1135 // In a function the middle-end wants to see a DECL_EXPR node.
1137 && no
->is_variable()
1138 && !no
->var_value()->is_parameter()
1139 && !this->functions_
.empty())
1140 this->add_statement(Statement::make_variable_declaration(no
));
1145 // Add a sink--a reference to the blank identifier _.
1150 return Named_object::make_sink();
1153 // Add a named object.
1156 Gogo::add_named_object(Named_object
* no
)
1158 this->current_bindings()->add_named_object(no
);
1161 // Mark all local variables used. This is used when some types of
1162 // parse error occur.
1165 Gogo::mark_locals_used()
1167 for (Open_functions::iterator pf
= this->functions_
.begin();
1168 pf
!= this->functions_
.end();
1171 for (std::vector
<Block
*>::iterator pb
= pf
->blocks
.begin();
1172 pb
!= pf
->blocks
.end();
1174 (*pb
)->bindings()->mark_locals_used();
1178 // Record that we've seen an interface type.
1181 Gogo::record_interface_type(Interface_type
* itype
)
1183 this->interface_types_
.push_back(itype
);
1186 // Return a name for a thunk object.
1191 static int thunk_count
;
1192 char thunk_name
[50];
1193 snprintf(thunk_name
, sizeof thunk_name
, "$thunk%d", thunk_count
);
1198 // Return whether a function is a thunk.
1201 Gogo::is_thunk(const Named_object
* no
)
1203 return no
->name().compare(0, 6, "$thunk") == 0;
1206 // Define the global names. We do this only after parsing all the
1207 // input files, because the program might define the global names
1211 Gogo::define_global_names()
1213 for (Bindings::const_declarations_iterator p
=
1214 this->globals_
->begin_declarations();
1215 p
!= this->globals_
->end_declarations();
1218 Named_object
* global_no
= p
->second
;
1219 std::string
name(Gogo::pack_hidden_name(global_no
->name(), false));
1220 Named_object
* no
= this->package_
->bindings()->lookup(name
);
1224 if (no
->is_type_declaration())
1226 if (global_no
->is_type())
1228 if (no
->type_declaration_value()->has_methods())
1229 error_at(no
->location(),
1230 "may not define methods for global type");
1231 no
->set_type_value(global_no
->type_value());
1235 error_at(no
->location(), "expected type");
1236 Type
* errtype
= Type::make_error_type();
1238 Named_object::make_type("erroneous_type", NULL
, errtype
,
1239 Linemap::predeclared_location());
1240 no
->set_type_value(err
->type_value());
1243 else if (no
->is_unknown())
1244 no
->unknown_value()->set_real_named_object(global_no
);
1248 // Clear out names in file scope.
1251 Gogo::clear_file_scope()
1253 this->package_
->bindings()->clear_file_scope();
1255 // Warn about packages which were imported but not used.
1256 bool quiet
= saw_errors();
1257 for (Packages::iterator p
= this->packages_
.begin();
1258 p
!= this->packages_
.end();
1261 Package
* package
= p
->second
;
1262 if (package
!= this->package_
1263 && package
->is_imported()
1265 && !package
->uses_sink_alias()
1267 error_at(package
->location(), "imported and not used: %s",
1268 Gogo::message_name(package
->package_name()).c_str());
1269 package
->clear_is_imported();
1270 package
->clear_uses_sink_alias();
1271 package
->clear_used();
1275 // Queue up a type specific function for later writing. These are
1276 // written out in write_specific_type_functions, called after the
1277 // parse tree is lowered.
1280 Gogo::queue_specific_type_function(Type
* type
, Named_type
* name
,
1281 const std::string
& hash_name
,
1282 Function_type
* hash_fntype
,
1283 const std::string
& equal_name
,
1284 Function_type
* equal_fntype
)
1286 go_assert(!this->specific_type_functions_are_written_
);
1287 go_assert(!this->in_global_scope());
1288 Specific_type_function
* tsf
= new Specific_type_function(type
, name
,
1293 this->specific_type_functions_
.push_back(tsf
);
1296 // Look for types which need specific hash or equality functions.
1298 class Specific_type_functions
: public Traverse
1301 Specific_type_functions(Gogo
* gogo
)
1302 : Traverse(traverse_types
),
1314 Specific_type_functions::type(Type
* t
)
1316 Named_object
* hash_fn
;
1317 Named_object
* equal_fn
;
1318 switch (t
->classification())
1320 case Type::TYPE_NAMED
:
1322 Named_type
* nt
= t
->named_type();
1323 if (!t
->compare_is_identity(this->gogo_
) && t
->is_comparable())
1324 t
->type_functions(this->gogo_
, nt
, NULL
, NULL
, &hash_fn
, &equal_fn
);
1326 // If this is a struct type, we don't want to make functions
1327 // for the unnamed struct.
1328 Type
* rt
= nt
->real_type();
1329 if (rt
->struct_type() == NULL
)
1331 if (Type::traverse(rt
, this) == TRAVERSE_EXIT
)
1332 return TRAVERSE_EXIT
;
1336 // If this type is defined in another package, then we don't
1337 // need to worry about the unexported fields.
1338 bool is_defined_elsewhere
= nt
->named_object()->package() != NULL
;
1339 const Struct_field_list
* fields
= rt
->struct_type()->fields();
1340 for (Struct_field_list::const_iterator p
= fields
->begin();
1344 if (is_defined_elsewhere
1345 && Gogo::is_hidden_name(p
->field_name()))
1347 if (Type::traverse(p
->type(), this) == TRAVERSE_EXIT
)
1348 return TRAVERSE_EXIT
;
1352 return TRAVERSE_SKIP_COMPONENTS
;
1355 case Type::TYPE_STRUCT
:
1356 case Type::TYPE_ARRAY
:
1357 if (!t
->compare_is_identity(this->gogo_
) && t
->is_comparable())
1358 t
->type_functions(this->gogo_
, NULL
, NULL
, NULL
, &hash_fn
, &equal_fn
);
1365 return TRAVERSE_CONTINUE
;
1368 // Write out type specific functions.
1371 Gogo::write_specific_type_functions()
1373 Specific_type_functions
stf(this);
1374 this->traverse(&stf
);
1376 while (!this->specific_type_functions_
.empty())
1378 Specific_type_function
* tsf
= this->specific_type_functions_
.back();
1379 this->specific_type_functions_
.pop_back();
1380 tsf
->type
->write_specific_type_functions(this, tsf
->name
,
1387 this->specific_type_functions_are_written_
= true;
1390 // Traverse the tree.
1393 Gogo::traverse(Traverse
* traverse
)
1395 // Traverse the current package first for consistency. The other
1396 // packages will only contain imported types, constants, and
1398 if (this->package_
->bindings()->traverse(traverse
, true) == TRAVERSE_EXIT
)
1400 for (Packages::const_iterator p
= this->packages_
.begin();
1401 p
!= this->packages_
.end();
1404 if (p
->second
!= this->package_
)
1406 if (p
->second
->bindings()->traverse(traverse
, true) == TRAVERSE_EXIT
)
1412 // Add a type to verify. This is used for types of sink variables, in
1413 // order to give appropriate error messages.
1416 Gogo::add_type_to_verify(Type
* type
)
1418 this->verify_types_
.push_back(type
);
1421 // Traversal class used to verify types.
1423 class Verify_types
: public Traverse
1427 : Traverse(traverse_types
)
1434 // Verify that a type is correct.
1437 Verify_types::type(Type
* t
)
1440 return TRAVERSE_SKIP_COMPONENTS
;
1441 return TRAVERSE_CONTINUE
;
1444 // Verify that all types are correct.
1447 Gogo::verify_types()
1449 Verify_types traverse
;
1450 this->traverse(&traverse
);
1452 for (std::vector
<Type
*>::iterator p
= this->verify_types_
.begin();
1453 p
!= this->verify_types_
.end();
1456 this->verify_types_
.clear();
1459 // Traversal class used to lower parse tree.
1461 class Lower_parse_tree
: public Traverse
1464 Lower_parse_tree(Gogo
* gogo
, Named_object
* function
)
1465 : Traverse(traverse_variables
1466 | traverse_constants
1467 | traverse_functions
1468 | traverse_statements
1469 | traverse_expressions
),
1470 gogo_(gogo
), function_(function
), iota_value_(-1), inserter_()
1474 set_inserter(const Statement_inserter
* inserter
)
1475 { this->inserter_
= *inserter
; }
1478 variable(Named_object
*);
1481 constant(Named_object
*, bool);
1484 function(Named_object
*);
1487 statement(Block
*, size_t* pindex
, Statement
*);
1490 expression(Expression
**);
1495 // The function we are traversing.
1496 Named_object
* function_
;
1497 // Value to use for the predeclared constant iota.
1499 // Current statement inserter for use by expressions.
1500 Statement_inserter inserter_
;
1506 Lower_parse_tree::variable(Named_object
* no
)
1508 if (!no
->is_variable())
1509 return TRAVERSE_CONTINUE
;
1511 if (no
->is_variable() && no
->var_value()->is_global())
1513 // Global variables can have loops in their initialization
1514 // expressions. This is handled in lower_init_expression.
1515 no
->var_value()->lower_init_expression(this->gogo_
, this->function_
,
1517 return TRAVERSE_CONTINUE
;
1520 // This is a local variable. We are going to return
1521 // TRAVERSE_SKIP_COMPONENTS here because we want to traverse the
1522 // initialization expression when we reach the variable declaration
1523 // statement. However, that means that we need to traverse the type
1525 if (no
->var_value()->has_type())
1527 Type
* type
= no
->var_value()->type();
1530 if (Type::traverse(type
, this) == TRAVERSE_EXIT
)
1531 return TRAVERSE_EXIT
;
1534 go_assert(!no
->var_value()->has_pre_init());
1536 return TRAVERSE_SKIP_COMPONENTS
;
1539 // Lower constants. We handle constants specially so that we can set
1540 // the right value for the predeclared constant iota. This works in
1541 // conjunction with the way we lower Const_expression objects.
1544 Lower_parse_tree::constant(Named_object
* no
, bool)
1546 Named_constant
* nc
= no
->const_value();
1548 // Don't get into trouble if the constant's initializer expression
1549 // refers to the constant itself.
1551 return TRAVERSE_CONTINUE
;
1554 go_assert(this->iota_value_
== -1);
1555 this->iota_value_
= nc
->iota_value();
1556 nc
->traverse_expression(this);
1557 this->iota_value_
= -1;
1559 nc
->clear_lowering();
1561 // We will traverse the expression a second time, but that will be
1564 return TRAVERSE_CONTINUE
;
1567 // Lower function closure types. Record the function while lowering
1568 // it, so that we can pass it down when lowering an expression.
1571 Lower_parse_tree::function(Named_object
* no
)
1573 no
->func_value()->set_closure_type();
1575 go_assert(this->function_
== NULL
);
1576 this->function_
= no
;
1577 int t
= no
->func_value()->traverse(this);
1578 this->function_
= NULL
;
1580 if (t
== TRAVERSE_EXIT
)
1582 return TRAVERSE_SKIP_COMPONENTS
;
1585 // Lower statement parse trees.
1588 Lower_parse_tree::statement(Block
* block
, size_t* pindex
, Statement
* sorig
)
1590 // Because we explicitly traverse the statement's contents
1591 // ourselves, we want to skip block statements here. There is
1592 // nothing to lower in a block statement.
1593 if (sorig
->is_block_statement())
1594 return TRAVERSE_CONTINUE
;
1596 Statement_inserter
hold_inserter(this->inserter_
);
1597 this->inserter_
= Statement_inserter(block
, pindex
);
1599 // Lower the expressions first.
1600 int t
= sorig
->traverse_contents(this);
1601 if (t
== TRAVERSE_EXIT
)
1603 this->inserter_
= hold_inserter
;
1607 // Keep lowering until nothing changes.
1608 Statement
* s
= sorig
;
1611 Statement
* snew
= s
->lower(this->gogo_
, this->function_
, block
,
1616 t
= s
->traverse_contents(this);
1617 if (t
== TRAVERSE_EXIT
)
1619 this->inserter_
= hold_inserter
;
1625 block
->replace_statement(*pindex
, s
);
1627 this->inserter_
= hold_inserter
;
1628 return TRAVERSE_SKIP_COMPONENTS
;
1631 // Lower expression parse trees.
1634 Lower_parse_tree::expression(Expression
** pexpr
)
1636 // We have to lower all subexpressions first, so that we can get
1637 // their type if necessary. This is awkward, because we don't have
1638 // a postorder traversal pass.
1639 if ((*pexpr
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
1640 return TRAVERSE_EXIT
;
1641 // Keep lowering until nothing changes.
1644 Expression
* e
= *pexpr
;
1645 Expression
* enew
= e
->lower(this->gogo_
, this->function_
,
1646 &this->inserter_
, this->iota_value_
);
1649 if (enew
->traverse_subexpressions(this) == TRAVERSE_EXIT
)
1650 return TRAVERSE_EXIT
;
1653 return TRAVERSE_SKIP_COMPONENTS
;
1656 // Lower the parse tree. This is called after the parse is complete,
1657 // when all names should be resolved.
1660 Gogo::lower_parse_tree()
1662 Lower_parse_tree
lower_parse_tree(this, NULL
);
1663 this->traverse(&lower_parse_tree
);
1669 Gogo::lower_block(Named_object
* function
, Block
* block
)
1671 Lower_parse_tree
lower_parse_tree(this, function
);
1672 block
->traverse(&lower_parse_tree
);
1675 // Lower an expression. INSERTER may be NULL, in which case the
1676 // expression had better not need to create any temporaries.
1679 Gogo::lower_expression(Named_object
* function
, Statement_inserter
* inserter
,
1682 Lower_parse_tree
lower_parse_tree(this, function
);
1683 if (inserter
!= NULL
)
1684 lower_parse_tree
.set_inserter(inserter
);
1685 lower_parse_tree
.expression(pexpr
);
1688 // Lower a constant. This is called when lowering a reference to a
1689 // constant. We have to make sure that the constant has already been
1693 Gogo::lower_constant(Named_object
* no
)
1695 go_assert(no
->is_const());
1696 Lower_parse_tree
lower(this, NULL
);
1697 lower
.constant(no
, false);
1700 // Look for interface types to finalize methods of inherited
1703 class Finalize_methods
: public Traverse
1706 Finalize_methods(Gogo
* gogo
)
1707 : Traverse(traverse_types
),
1718 // Finalize the methods of an interface type.
1721 Finalize_methods::type(Type
* t
)
1723 // Check the classification so that we don't finalize the methods
1724 // twice for a named interface type.
1725 switch (t
->classification())
1727 case Type::TYPE_INTERFACE
:
1728 t
->interface_type()->finalize_methods();
1731 case Type::TYPE_NAMED
:
1733 // We have to finalize the methods of the real type first.
1734 // But if the real type is a struct type, then we only want to
1735 // finalize the methods of the field types, not of the struct
1736 // type itself. We don't want to add methods to the struct,
1737 // since it has a name.
1738 Named_type
* nt
= t
->named_type();
1739 Type
* rt
= nt
->real_type();
1740 if (rt
->classification() != Type::TYPE_STRUCT
)
1742 if (Type::traverse(rt
, this) == TRAVERSE_EXIT
)
1743 return TRAVERSE_EXIT
;
1747 if (rt
->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT
)
1748 return TRAVERSE_EXIT
;
1751 nt
->finalize_methods(this->gogo_
);
1753 // If this type is defined in a different package, then finalize the
1754 // types of all the methods, since we won't see them otherwise.
1755 if (nt
->named_object()->package() != NULL
&& nt
->has_any_methods())
1757 const Methods
* methods
= nt
->methods();
1758 for (Methods::const_iterator p
= methods
->begin();
1759 p
!= methods
->end();
1762 if (Type::traverse(p
->second
->type(), this) == TRAVERSE_EXIT
)
1763 return TRAVERSE_EXIT
;
1767 return TRAVERSE_SKIP_COMPONENTS
;
1770 case Type::TYPE_STRUCT
:
1771 // Traverse the field types first in case there is an embedded
1772 // field with methods that the struct should inherit.
1773 if (t
->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT
)
1774 return TRAVERSE_EXIT
;
1775 t
->struct_type()->finalize_methods(this->gogo_
);
1776 return TRAVERSE_SKIP_COMPONENTS
;
1782 return TRAVERSE_CONTINUE
;
1785 // Finalize method lists and build stub methods for types.
1788 Gogo::finalize_methods()
1790 Finalize_methods
finalize(this);
1791 this->traverse(&finalize
);
1794 // Set types for unspecified variables and constants.
1797 Gogo::determine_types()
1799 Bindings
* bindings
= this->current_bindings();
1800 for (Bindings::const_definitions_iterator p
= bindings
->begin_definitions();
1801 p
!= bindings
->end_definitions();
1804 if ((*p
)->is_function())
1805 (*p
)->func_value()->determine_types();
1806 else if ((*p
)->is_variable())
1807 (*p
)->var_value()->determine_type();
1808 else if ((*p
)->is_const())
1809 (*p
)->const_value()->determine_type();
1811 // See if a variable requires us to build an initialization
1812 // function. We know that we will see all global variables
1814 if (!this->need_init_fn_
&& (*p
)->is_variable())
1816 Variable
* variable
= (*p
)->var_value();
1818 // If this is a global variable which requires runtime
1819 // initialization, we need an initialization function.
1820 if (!variable
->is_global())
1822 else if (variable
->init() == NULL
)
1824 else if (variable
->type()->interface_type() != NULL
)
1825 this->need_init_fn_
= true;
1826 else if (variable
->init()->is_constant())
1828 else if (!variable
->init()->is_composite_literal())
1829 this->need_init_fn_
= true;
1830 else if (variable
->init()->is_nonconstant_composite_literal())
1831 this->need_init_fn_
= true;
1833 // If this is a global variable which holds a pointer value,
1834 // then we need an initialization function to register it as a
1836 if (variable
->is_global() && variable
->type()->has_pointer())
1837 this->need_init_fn_
= true;
1841 // Determine the types of constants in packages.
1842 for (Packages::const_iterator p
= this->packages_
.begin();
1843 p
!= this->packages_
.end();
1845 p
->second
->determine_types();
1848 // Traversal class used for type checking.
1850 class Check_types_traverse
: public Traverse
1853 Check_types_traverse(Gogo
* gogo
)
1854 : Traverse(traverse_variables
1855 | traverse_constants
1856 | traverse_functions
1857 | traverse_statements
1858 | traverse_expressions
),
1863 variable(Named_object
*);
1866 constant(Named_object
*, bool);
1869 function(Named_object
*);
1872 statement(Block
*, size_t* pindex
, Statement
*);
1875 expression(Expression
**);
1882 // Check that a variable initializer has the right type.
1885 Check_types_traverse::variable(Named_object
* named_object
)
1887 if (named_object
->is_variable())
1889 Variable
* var
= named_object
->var_value();
1891 // Give error if variable type is not defined.
1892 var
->type()->base();
1894 Expression
* init
= var
->init();
1897 && !Type::are_assignable(var
->type(), init
->type(), &reason
))
1900 error_at(var
->location(), "incompatible type in initialization");
1902 error_at(var
->location(),
1903 "incompatible type in initialization (%s)",
1907 else if (!var
->is_used()
1908 && !var
->is_global()
1909 && !var
->is_parameter()
1910 && !var
->is_receiver()
1911 && !var
->type()->is_error()
1912 && (init
== NULL
|| !init
->is_error_expression())
1913 && !Lex::is_invalid_identifier(named_object
->name()))
1914 error_at(var
->location(), "%qs declared and not used",
1915 named_object
->message_name().c_str());
1917 return TRAVERSE_CONTINUE
;
1920 // Check that a constant initializer has the right type.
1923 Check_types_traverse::constant(Named_object
* named_object
, bool)
1925 Named_constant
* constant
= named_object
->const_value();
1926 Type
* ctype
= constant
->type();
1927 if (ctype
->integer_type() == NULL
1928 && ctype
->float_type() == NULL
1929 && ctype
->complex_type() == NULL
1930 && !ctype
->is_boolean_type()
1931 && !ctype
->is_string_type())
1933 if (ctype
->is_nil_type())
1934 error_at(constant
->location(), "const initializer cannot be nil");
1935 else if (!ctype
->is_error())
1936 error_at(constant
->location(), "invalid constant type");
1937 constant
->set_error();
1939 else if (!constant
->expr()->is_constant())
1941 error_at(constant
->expr()->location(), "expression is not constant");
1942 constant
->set_error();
1944 else if (!Type::are_assignable(constant
->type(), constant
->expr()->type(),
1947 error_at(constant
->location(),
1948 "initialization expression has wrong type");
1949 constant
->set_error();
1951 return TRAVERSE_CONTINUE
;
1954 // There are no types to check in a function, but this is where we
1955 // issue warnings about labels which are defined but not referenced.
1958 Check_types_traverse::function(Named_object
* no
)
1960 no
->func_value()->check_labels();
1961 return TRAVERSE_CONTINUE
;
1964 // Check that types are valid in a statement.
1967 Check_types_traverse::statement(Block
*, size_t*, Statement
* s
)
1969 s
->check_types(this->gogo_
);
1970 return TRAVERSE_CONTINUE
;
1973 // Check that types are valid in an expression.
1976 Check_types_traverse::expression(Expression
** expr
)
1978 (*expr
)->check_types(this->gogo_
);
1979 return TRAVERSE_CONTINUE
;
1982 // Check that types are valid.
1987 Check_types_traverse
traverse(this);
1988 this->traverse(&traverse
);
1991 // Check the types in a single block.
1994 Gogo::check_types_in_block(Block
* block
)
1996 Check_types_traverse
traverse(this);
1997 block
->traverse(&traverse
);
2000 // A traversal class used to find a single shortcut operator within an
2003 class Find_shortcut
: public Traverse
2007 : Traverse(traverse_blocks
2008 | traverse_statements
2009 | traverse_expressions
),
2013 // A pointer to the expression which was found, or NULL if none was
2017 { return this->found_
; }
2022 { return TRAVERSE_SKIP_COMPONENTS
; }
2025 statement(Block
*, size_t*, Statement
*)
2026 { return TRAVERSE_SKIP_COMPONENTS
; }
2029 expression(Expression
**);
2032 Expression
** found_
;
2035 // Find a shortcut expression.
2038 Find_shortcut::expression(Expression
** pexpr
)
2040 Expression
* expr
= *pexpr
;
2041 Binary_expression
* be
= expr
->binary_expression();
2043 return TRAVERSE_CONTINUE
;
2044 Operator op
= be
->op();
2045 if (op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
)
2046 return TRAVERSE_CONTINUE
;
2047 go_assert(this->found_
== NULL
);
2048 this->found_
= pexpr
;
2049 return TRAVERSE_EXIT
;
2052 // A traversal class used to turn shortcut operators into explicit if
2055 class Shortcuts
: public Traverse
2058 Shortcuts(Gogo
* gogo
)
2059 : Traverse(traverse_variables
2060 | traverse_statements
),
2066 variable(Named_object
*);
2069 statement(Block
*, size_t*, Statement
*);
2072 // Convert a shortcut operator.
2074 convert_shortcut(Block
* enclosing
, Expression
** pshortcut
);
2080 // Remove shortcut operators in a single statement.
2083 Shortcuts::statement(Block
* block
, size_t* pindex
, Statement
* s
)
2085 // FIXME: This approach doesn't work for switch statements, because
2086 // we add the new statements before the whole switch when we need to
2087 // instead add them just before the switch expression. The right
2088 // fix is probably to lower switch statements with nonconstant cases
2089 // to a series of conditionals.
2090 if (s
->switch_statement() != NULL
)
2091 return TRAVERSE_CONTINUE
;
2095 Find_shortcut find_shortcut
;
2097 // If S is a variable declaration, then ordinary traversal won't
2098 // do anything. We want to explicitly traverse the
2099 // initialization expression if there is one.
2100 Variable_declaration_statement
* vds
= s
->variable_declaration_statement();
2101 Expression
* init
= NULL
;
2103 s
->traverse_contents(&find_shortcut
);
2106 init
= vds
->var()->var_value()->init();
2108 return TRAVERSE_CONTINUE
;
2109 init
->traverse(&init
, &find_shortcut
);
2111 Expression
** pshortcut
= find_shortcut
.found();
2112 if (pshortcut
== NULL
)
2113 return TRAVERSE_CONTINUE
;
2115 Statement
* snew
= this->convert_shortcut(block
, pshortcut
);
2116 block
->insert_statement_before(*pindex
, snew
);
2119 if (pshortcut
== &init
)
2120 vds
->var()->var_value()->set_init(init
);
2124 // Remove shortcut operators in the initializer of a global variable.
2127 Shortcuts::variable(Named_object
* no
)
2129 if (no
->is_result_variable())
2130 return TRAVERSE_CONTINUE
;
2131 Variable
* var
= no
->var_value();
2132 Expression
* init
= var
->init();
2133 if (!var
->is_global() || init
== NULL
)
2134 return TRAVERSE_CONTINUE
;
2138 Find_shortcut find_shortcut
;
2139 init
->traverse(&init
, &find_shortcut
);
2140 Expression
** pshortcut
= find_shortcut
.found();
2141 if (pshortcut
== NULL
)
2142 return TRAVERSE_CONTINUE
;
2144 Statement
* snew
= this->convert_shortcut(NULL
, pshortcut
);
2145 var
->add_preinit_statement(this->gogo_
, snew
);
2146 if (pshortcut
== &init
)
2147 var
->set_init(init
);
2151 // Given an expression which uses a shortcut operator, return a
2152 // statement which implements it, and update *PSHORTCUT accordingly.
2155 Shortcuts::convert_shortcut(Block
* enclosing
, Expression
** pshortcut
)
2157 Binary_expression
* shortcut
= (*pshortcut
)->binary_expression();
2158 Expression
* left
= shortcut
->left();
2159 Expression
* right
= shortcut
->right();
2160 Location loc
= shortcut
->location();
2162 Block
* retblock
= new Block(enclosing
, loc
);
2163 retblock
->set_end_location(loc
);
2165 Temporary_statement
* ts
= Statement::make_temporary(Type::lookup_bool_type(),
2167 retblock
->add_statement(ts
);
2169 Block
* block
= new Block(retblock
, loc
);
2170 block
->set_end_location(loc
);
2171 Expression
* tmpref
= Expression::make_temporary_reference(ts
, loc
);
2172 Statement
* assign
= Statement::make_assignment(tmpref
, right
, loc
);
2173 block
->add_statement(assign
);
2175 Expression
* cond
= Expression::make_temporary_reference(ts
, loc
);
2176 if (shortcut
->binary_expression()->op() == OPERATOR_OROR
)
2177 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
2179 Statement
* if_statement
= Statement::make_if_statement(cond
, block
, NULL
,
2181 retblock
->add_statement(if_statement
);
2183 *pshortcut
= Expression::make_temporary_reference(ts
, loc
);
2187 // Now convert any shortcut operators in LEFT and RIGHT.
2188 Shortcuts
shortcuts(this->gogo_
);
2189 retblock
->traverse(&shortcuts
);
2191 return Statement::make_block_statement(retblock
, loc
);
2194 // Turn shortcut operators into explicit if statements. Doing this
2195 // considerably simplifies the order of evaluation rules.
2198 Gogo::remove_shortcuts()
2200 Shortcuts
shortcuts(this);
2201 this->traverse(&shortcuts
);
2204 // A traversal class which finds all the expressions which must be
2205 // evaluated in order within a statement or larger expression. This
2206 // is used to implement the rules about order of evaluation.
2208 class Find_eval_ordering
: public Traverse
2211 typedef std::vector
<Expression
**> Expression_pointers
;
2214 Find_eval_ordering()
2215 : Traverse(traverse_blocks
2216 | traverse_statements
2217 | traverse_expressions
),
2223 { return this->exprs_
.size(); }
2225 typedef Expression_pointers::const_iterator const_iterator
;
2229 { return this->exprs_
.begin(); }
2233 { return this->exprs_
.end(); }
2238 { return TRAVERSE_SKIP_COMPONENTS
; }
2241 statement(Block
*, size_t*, Statement
*)
2242 { return TRAVERSE_SKIP_COMPONENTS
; }
2245 expression(Expression
**);
2248 // A list of pointers to expressions with side-effects.
2249 Expression_pointers exprs_
;
2252 // If an expression must be evaluated in order, put it on the list.
2255 Find_eval_ordering::expression(Expression
** expression_pointer
)
2257 // We have to look at subexpressions before this one.
2258 if ((*expression_pointer
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
2259 return TRAVERSE_EXIT
;
2260 if ((*expression_pointer
)->must_eval_in_order())
2261 this->exprs_
.push_back(expression_pointer
);
2262 return TRAVERSE_SKIP_COMPONENTS
;
2265 // A traversal class for ordering evaluations.
2267 class Order_eval
: public Traverse
2270 Order_eval(Gogo
* gogo
)
2271 : Traverse(traverse_variables
2272 | traverse_statements
),
2277 variable(Named_object
*);
2280 statement(Block
*, size_t*, Statement
*);
2287 // Implement the order of evaluation rules for a statement.
2290 Order_eval::statement(Block
* block
, size_t* pindex
, Statement
* s
)
2292 // FIXME: This approach doesn't work for switch statements, because
2293 // we add the new statements before the whole switch when we need to
2294 // instead add them just before the switch expression. The right
2295 // fix is probably to lower switch statements with nonconstant cases
2296 // to a series of conditionals.
2297 if (s
->switch_statement() != NULL
)
2298 return TRAVERSE_CONTINUE
;
2300 Find_eval_ordering find_eval_ordering
;
2302 // If S is a variable declaration, then ordinary traversal won't do
2303 // anything. We want to explicitly traverse the initialization
2304 // expression if there is one.
2305 Variable_declaration_statement
* vds
= s
->variable_declaration_statement();
2306 Expression
* init
= NULL
;
2307 Expression
* orig_init
= NULL
;
2309 s
->traverse_contents(&find_eval_ordering
);
2312 init
= vds
->var()->var_value()->init();
2314 return TRAVERSE_CONTINUE
;
2317 // It might seem that this could be
2318 // init->traverse_subexpressions. Unfortunately that can fail
2321 // newvar, err := call(arg())
2322 // Here newvar will have an init of call result 0 of
2323 // call(arg()). If we only traverse subexpressions, we will
2324 // only find arg(), and we won't bother to move anything out.
2325 // Then we get to the assignment to err, we will traverse the
2326 // whole statement, and this time we will find both call() and
2327 // arg(), and so we will move them out. This will cause them to
2328 // be put into temporary variables before the assignment to err
2329 // but after the declaration of newvar. To avoid that problem,
2330 // we traverse the entire expression here.
2331 Expression::traverse(&init
, &find_eval_ordering
);
2334 size_t c
= find_eval_ordering
.size();
2336 return TRAVERSE_CONTINUE
;
2338 // If there is only one expression with a side-effect, we can
2339 // usually leave it in place. However, for an assignment statement,
2340 // we need to evaluate an expression on the right hand side before
2341 // we evaluate any index expression on the left hand side, so for
2342 // that case we always move the expression. Otherwise we mishandle
2343 // m[0] = len(m) where m is a map.
2344 if (c
== 1 && s
->classification() != Statement::STATEMENT_ASSIGNMENT
)
2345 return TRAVERSE_CONTINUE
;
2347 bool is_thunk
= s
->thunk_statement() != NULL
;
2348 for (Find_eval_ordering::const_iterator p
= find_eval_ordering
.begin();
2349 p
!= find_eval_ordering
.end();
2352 Expression
** pexpr
= *p
;
2354 // The last expression in a thunk will be the call passed to go
2355 // or defer, which we must not evaluate early.
2356 if (is_thunk
&& p
+ 1 == find_eval_ordering
.end())
2359 Location loc
= (*pexpr
)->location();
2361 if ((*pexpr
)->call_expression() == NULL
2362 || (*pexpr
)->call_expression()->result_count() < 2)
2364 Temporary_statement
* ts
= Statement::make_temporary(NULL
, *pexpr
,
2367 *pexpr
= Expression::make_temporary_reference(ts
, loc
);
2371 // A call expression which returns multiple results needs to
2372 // be handled specially. We can't create a temporary
2373 // because there is no type to give it. Any actual uses of
2374 // the values will be done via Call_result_expressions.
2375 s
= Statement::make_statement(*pexpr
, true);
2378 block
->insert_statement_before(*pindex
, s
);
2382 if (init
!= orig_init
)
2383 vds
->var()->var_value()->set_init(init
);
2385 return TRAVERSE_CONTINUE
;
2388 // Implement the order of evaluation rules for the initializer of a
2392 Order_eval::variable(Named_object
* no
)
2394 if (no
->is_result_variable())
2395 return TRAVERSE_CONTINUE
;
2396 Variable
* var
= no
->var_value();
2397 Expression
* init
= var
->init();
2398 if (!var
->is_global() || init
== NULL
)
2399 return TRAVERSE_CONTINUE
;
2401 Find_eval_ordering find_eval_ordering
;
2402 Expression::traverse(&init
, &find_eval_ordering
);
2404 if (find_eval_ordering
.size() <= 1)
2406 // If there is only one expression with a side-effect, we can
2407 // leave it in place.
2408 return TRAVERSE_SKIP_COMPONENTS
;
2411 Expression
* orig_init
= init
;
2413 for (Find_eval_ordering::const_iterator p
= find_eval_ordering
.begin();
2414 p
!= find_eval_ordering
.end();
2417 Expression
** pexpr
= *p
;
2418 Location loc
= (*pexpr
)->location();
2420 if ((*pexpr
)->call_expression() == NULL
2421 || (*pexpr
)->call_expression()->result_count() < 2)
2423 Temporary_statement
* ts
= Statement::make_temporary(NULL
, *pexpr
,
2426 *pexpr
= Expression::make_temporary_reference(ts
, loc
);
2430 // A call expression which returns multiple results needs to
2431 // be handled specially.
2432 s
= Statement::make_statement(*pexpr
, true);
2434 var
->add_preinit_statement(this->gogo_
, s
);
2437 if (init
!= orig_init
)
2438 var
->set_init(init
);
2440 return TRAVERSE_SKIP_COMPONENTS
;
2443 // Use temporary variables to implement the order of evaluation rules.
2446 Gogo::order_evaluations()
2448 Order_eval
order_eval(this);
2449 this->traverse(&order_eval
);
2452 // Traversal to convert calls to the predeclared recover function to
2453 // pass in an argument indicating whether it can recover from a panic
2456 class Convert_recover
: public Traverse
2459 Convert_recover(Named_object
* arg
)
2460 : Traverse(traverse_expressions
),
2466 expression(Expression
**);
2469 // The argument to pass to the function.
2473 // Convert calls to recover.
2476 Convert_recover::expression(Expression
** pp
)
2478 Call_expression
* ce
= (*pp
)->call_expression();
2479 if (ce
!= NULL
&& ce
->is_recover_call())
2480 ce
->set_recover_arg(Expression::make_var_reference(this->arg_
,
2482 return TRAVERSE_CONTINUE
;
2485 // Traversal for build_recover_thunks.
2487 class Build_recover_thunks
: public Traverse
2490 Build_recover_thunks(Gogo
* gogo
)
2491 : Traverse(traverse_functions
),
2496 function(Named_object
*);
2500 can_recover_arg(Location
);
2506 // If this function calls recover, turn it into a thunk.
2509 Build_recover_thunks::function(Named_object
* orig_no
)
2511 Function
* orig_func
= orig_no
->func_value();
2512 if (!orig_func
->calls_recover()
2513 || orig_func
->is_recover_thunk()
2514 || orig_func
->has_recover_thunk())
2515 return TRAVERSE_CONTINUE
;
2517 Gogo
* gogo
= this->gogo_
;
2518 Location location
= orig_func
->location();
2523 Function_type
* orig_fntype
= orig_func
->type();
2524 Typed_identifier_list
* new_params
= new Typed_identifier_list();
2525 std::string receiver_name
;
2526 if (orig_fntype
->is_method())
2528 const Typed_identifier
* receiver
= orig_fntype
->receiver();
2529 snprintf(buf
, sizeof buf
, "rt.%u", count
);
2531 receiver_name
= buf
;
2532 new_params
->push_back(Typed_identifier(receiver_name
, receiver
->type(),
2533 receiver
->location()));
2535 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
2536 if (orig_params
!= NULL
&& !orig_params
->empty())
2538 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
2539 p
!= orig_params
->end();
2542 snprintf(buf
, sizeof buf
, "pt.%u", count
);
2544 new_params
->push_back(Typed_identifier(buf
, p
->type(),
2548 snprintf(buf
, sizeof buf
, "pr.%u", count
);
2550 std::string can_recover_name
= buf
;
2551 new_params
->push_back(Typed_identifier(can_recover_name
,
2552 Type::lookup_bool_type(),
2553 orig_fntype
->location()));
2555 const Typed_identifier_list
* orig_results
= orig_fntype
->results();
2556 Typed_identifier_list
* new_results
;
2557 if (orig_results
== NULL
|| orig_results
->empty())
2561 new_results
= new Typed_identifier_list();
2562 for (Typed_identifier_list::const_iterator p
= orig_results
->begin();
2563 p
!= orig_results
->end();
2565 new_results
->push_back(Typed_identifier("", p
->type(), p
->location()));
2568 Function_type
*new_fntype
= Type::make_function_type(NULL
, new_params
,
2570 orig_fntype
->location());
2571 if (orig_fntype
->is_varargs())
2572 new_fntype
->set_is_varargs();
2574 std::string name
= orig_no
->name() + "$recover";
2575 Named_object
*new_no
= gogo
->start_function(name
, new_fntype
, false,
2577 Function
*new_func
= new_no
->func_value();
2578 if (orig_func
->enclosing() != NULL
)
2579 new_func
->set_enclosing(orig_func
->enclosing());
2581 // We build the code for the original function attached to the new
2582 // function, and then swap the original and new function bodies.
2583 // This means that existing references to the original function will
2584 // then refer to the new function. That makes this code a little
2585 // confusing, in that the reference to NEW_NO really refers to the
2586 // other function, not the one we are building.
2588 Expression
* closure
= NULL
;
2589 if (orig_func
->needs_closure())
2591 Named_object
* orig_closure_no
= orig_func
->closure_var();
2592 Variable
* orig_closure_var
= orig_closure_no
->var_value();
2593 Variable
* new_var
= new Variable(orig_closure_var
->type(), NULL
, false,
2594 true, false, location
);
2595 snprintf(buf
, sizeof buf
, "closure.%u", count
);
2597 Named_object
* new_closure_no
= Named_object::make_variable(buf
, NULL
,
2599 new_func
->set_closure_var(new_closure_no
);
2600 closure
= Expression::make_var_reference(new_closure_no
, location
);
2603 Expression
* fn
= Expression::make_func_reference(new_no
, closure
, location
);
2605 Expression_list
* args
= new Expression_list();
2606 if (new_params
!= NULL
)
2608 // Note that we skip the last parameter, which is the boolean
2609 // indicating whether recover can succed.
2610 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
2611 p
+ 1 != new_params
->end();
2614 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
2615 go_assert(p_no
!= NULL
2616 && p_no
->is_variable()
2617 && p_no
->var_value()->is_parameter());
2618 args
->push_back(Expression::make_var_reference(p_no
, location
));
2621 args
->push_back(this->can_recover_arg(location
));
2623 gogo
->start_block(location
);
2625 Call_expression
* call
= Expression::make_call(fn
, args
, false, location
);
2627 // Any varargs call has already been lowered.
2628 call
->set_varargs_are_lowered();
2631 if (orig_fntype
->results() == NULL
|| orig_fntype
->results()->empty())
2632 s
= Statement::make_statement(call
, true);
2635 Expression_list
* vals
= new Expression_list();
2636 size_t rc
= orig_fntype
->results()->size();
2638 vals
->push_back(call
);
2641 for (size_t i
= 0; i
< rc
; ++i
)
2642 vals
->push_back(Expression::make_call_result(call
, i
));
2644 s
= Statement::make_return_statement(vals
, location
);
2646 s
->determine_types();
2647 gogo
->add_statement(s
);
2649 Block
* b
= gogo
->finish_block(location
);
2651 gogo
->add_block(b
, location
);
2653 // Lower the call in case it returns multiple results.
2654 gogo
->lower_block(new_no
, b
);
2656 gogo
->finish_function(location
);
2658 // Swap the function bodies and types.
2659 new_func
->swap_for_recover(orig_func
);
2660 orig_func
->set_is_recover_thunk();
2661 new_func
->set_calls_recover();
2662 new_func
->set_has_recover_thunk();
2664 Bindings
* orig_bindings
= orig_func
->block()->bindings();
2665 Bindings
* new_bindings
= new_func
->block()->bindings();
2666 if (orig_fntype
->is_method())
2668 // We changed the receiver to be a regular parameter. We have
2669 // to update the binding accordingly in both functions.
2670 Named_object
* orig_rec_no
= orig_bindings
->lookup_local(receiver_name
);
2671 go_assert(orig_rec_no
!= NULL
2672 && orig_rec_no
->is_variable()
2673 && !orig_rec_no
->var_value()->is_receiver());
2674 orig_rec_no
->var_value()->set_is_receiver();
2676 const std::string
& new_receiver_name(orig_fntype
->receiver()->name());
2677 Named_object
* new_rec_no
= new_bindings
->lookup_local(new_receiver_name
);
2678 if (new_rec_no
== NULL
)
2679 go_assert(saw_errors());
2682 go_assert(new_rec_no
->is_variable()
2683 && new_rec_no
->var_value()->is_receiver());
2684 new_rec_no
->var_value()->set_is_not_receiver();
2688 // Because we flipped blocks but not types, the can_recover
2689 // parameter appears in the (now) old bindings as a parameter.
2690 // Change it to a local variable, whereupon it will be discarded.
2691 Named_object
* can_recover_no
= orig_bindings
->lookup_local(can_recover_name
);
2692 go_assert(can_recover_no
!= NULL
2693 && can_recover_no
->is_variable()
2694 && can_recover_no
->var_value()->is_parameter());
2695 orig_bindings
->remove_binding(can_recover_no
);
2697 // Add the can_recover argument to the (now) new bindings, and
2698 // attach it to any recover statements.
2699 Variable
* can_recover_var
= new Variable(Type::lookup_bool_type(), NULL
,
2700 false, true, false, location
);
2701 can_recover_no
= new_bindings
->add_variable(can_recover_name
, NULL
,
2703 Convert_recover
convert_recover(can_recover_no
);
2704 new_func
->traverse(&convert_recover
);
2706 // Update the function pointers in any named results.
2707 new_func
->update_result_variables();
2708 orig_func
->update_result_variables();
2710 return TRAVERSE_CONTINUE
;
2713 // Return the expression to pass for the .can_recover parameter to the
2714 // new function. This indicates whether a call to recover may return
2715 // non-nil. The expression is
2716 // __go_can_recover(__builtin_return_address()).
2719 Build_recover_thunks::can_recover_arg(Location location
)
2721 static Named_object
* builtin_return_address
;
2722 if (builtin_return_address
== NULL
)
2724 const Location bloc
= Linemap::predeclared_location();
2726 Typed_identifier_list
* param_types
= new Typed_identifier_list();
2727 Type
* uint_type
= Type::lookup_integer_type("uint");
2728 param_types
->push_back(Typed_identifier("l", uint_type
, bloc
));
2730 Typed_identifier_list
* return_types
= new Typed_identifier_list();
2731 Type
* voidptr_type
= Type::make_pointer_type(Type::make_void_type());
2732 return_types
->push_back(Typed_identifier("", voidptr_type
, bloc
));
2734 Function_type
* fntype
= Type::make_function_type(NULL
, param_types
,
2735 return_types
, bloc
);
2736 builtin_return_address
=
2737 Named_object::make_function_declaration("__builtin_return_address",
2738 NULL
, fntype
, bloc
);
2739 const char* n
= "__builtin_return_address";
2740 builtin_return_address
->func_declaration_value()->set_asm_name(n
);
2743 static Named_object
* can_recover
;
2744 if (can_recover
== NULL
)
2746 const Location bloc
= Linemap::predeclared_location();
2747 Typed_identifier_list
* param_types
= new Typed_identifier_list();
2748 Type
* voidptr_type
= Type::make_pointer_type(Type::make_void_type());
2749 param_types
->push_back(Typed_identifier("a", voidptr_type
, bloc
));
2750 Type
* boolean_type
= Type::lookup_bool_type();
2751 Typed_identifier_list
* results
= new Typed_identifier_list();
2752 results
->push_back(Typed_identifier("", boolean_type
, bloc
));
2753 Function_type
* fntype
= Type::make_function_type(NULL
, param_types
,
2755 can_recover
= Named_object::make_function_declaration("__go_can_recover",
2758 can_recover
->func_declaration_value()->set_asm_name("__go_can_recover");
2761 Expression
* fn
= Expression::make_func_reference(builtin_return_address
,
2765 mpz_init_set_ui(zval
, 0UL);
2766 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, location
);
2768 Expression_list
*args
= new Expression_list();
2769 args
->push_back(zexpr
);
2771 Expression
* call
= Expression::make_call(fn
, args
, false, location
);
2773 args
= new Expression_list();
2774 args
->push_back(call
);
2776 fn
= Expression::make_func_reference(can_recover
, NULL
, location
);
2777 return Expression::make_call(fn
, args
, false, location
);
2780 // Build thunks for functions which call recover. We build a new
2781 // function with an extra parameter, which is whether a call to
2782 // recover can succeed. We then move the body of this function to
2783 // that one. We then turn this function into a thunk which calls the
2784 // new one, passing the value of
2785 // __go_can_recover(__builtin_return_address()). The function will be
2786 // marked as not splitting the stack. This will cooperate with the
2787 // implementation of defer to make recover do the right thing.
2790 Gogo::build_recover_thunks()
2792 Build_recover_thunks
build_recover_thunks(this);
2793 this->traverse(&build_recover_thunks
);
2796 // Look for named types to see whether we need to create an interface
2799 class Build_method_tables
: public Traverse
2802 Build_method_tables(Gogo
* gogo
,
2803 const std::vector
<Interface_type
*>& interfaces
)
2804 : Traverse(traverse_types
),
2805 gogo_(gogo
), interfaces_(interfaces
)
2814 // A list of locally defined interfaces which have hidden methods.
2815 const std::vector
<Interface_type
*>& interfaces_
;
2818 // Build all required interface method tables for types. We need to
2819 // ensure that we have an interface method table for every interface
2820 // which has a hidden method, for every named type which implements
2821 // that interface. Normally we can just build interface method tables
2822 // as we need them. However, in some cases we can require an
2823 // interface method table for an interface defined in a different
2824 // package for a type defined in that package. If that interface and
2825 // type both use a hidden method, that is OK. However, we will not be
2826 // able to build that interface method table when we need it, because
2827 // the type's hidden method will be static. So we have to build it
2828 // here, and just refer it from other packages as needed.
2831 Gogo::build_interface_method_tables()
2836 std::vector
<Interface_type
*> hidden_interfaces
;
2837 hidden_interfaces
.reserve(this->interface_types_
.size());
2838 for (std::vector
<Interface_type
*>::const_iterator pi
=
2839 this->interface_types_
.begin();
2840 pi
!= this->interface_types_
.end();
2843 const Typed_identifier_list
* methods
= (*pi
)->methods();
2844 if (methods
== NULL
)
2846 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
2847 pm
!= methods
->end();
2850 if (Gogo::is_hidden_name(pm
->name()))
2852 hidden_interfaces
.push_back(*pi
);
2858 if (!hidden_interfaces
.empty())
2860 // Now traverse the tree looking for all named types.
2861 Build_method_tables
bmt(this, hidden_interfaces
);
2862 this->traverse(&bmt
);
2865 // We no longer need the list of interfaces.
2867 this->interface_types_
.clear();
2870 // This is called for each type. For a named type, for each of the
2871 // interfaces with hidden methods that it implements, create the
2875 Build_method_tables::type(Type
* type
)
2877 Named_type
* nt
= type
->named_type();
2878 Struct_type
* st
= type
->struct_type();
2879 if (nt
!= NULL
|| st
!= NULL
)
2881 for (std::vector
<Interface_type
*>::const_iterator p
=
2882 this->interfaces_
.begin();
2883 p
!= this->interfaces_
.end();
2886 // We ask whether a pointer to the named type implements the
2887 // interface, because a pointer can implement more methods
2891 if ((*p
)->implements_interface(Type::make_pointer_type(nt
),
2894 nt
->interface_method_table(this->gogo_
, *p
, false);
2895 nt
->interface_method_table(this->gogo_
, *p
, true);
2900 if ((*p
)->implements_interface(Type::make_pointer_type(st
),
2903 st
->interface_method_table(this->gogo_
, *p
, false);
2904 st
->interface_method_table(this->gogo_
, *p
, true);
2909 return TRAVERSE_CONTINUE
;
2912 // Traversal class used to check for return statements.
2914 class Check_return_statements_traverse
: public Traverse
2917 Check_return_statements_traverse()
2918 : Traverse(traverse_functions
)
2922 function(Named_object
*);
2925 // Check that a function has a return statement if it needs one.
2928 Check_return_statements_traverse::function(Named_object
* no
)
2930 Function
* func
= no
->func_value();
2931 const Function_type
* fntype
= func
->type();
2932 const Typed_identifier_list
* results
= fntype
->results();
2934 // We only need a return statement if there is a return value.
2935 if (results
== NULL
|| results
->empty())
2936 return TRAVERSE_CONTINUE
;
2938 if (func
->block()->may_fall_through())
2939 error_at(func
->location(), "control reaches end of non-void function");
2941 return TRAVERSE_CONTINUE
;
2944 // Check return statements.
2947 Gogo::check_return_statements()
2949 Check_return_statements_traverse traverse
;
2950 this->traverse(&traverse
);
2953 // Work out the package priority. It is one more than the maximum
2954 // priority of an imported package.
2957 Gogo::package_priority() const
2960 for (Packages::const_iterator p
= this->packages_
.begin();
2961 p
!= this->packages_
.end();
2963 if (p
->second
->priority() > priority
)
2964 priority
= p
->second
->priority();
2965 return priority
+ 1;
2968 // Export identifiers as requested.
2973 // For now we always stream to a section. Later we may want to
2974 // support streaming to a separate file.
2975 Stream_to_section stream
;
2977 Export
exp(&stream
);
2978 exp
.register_builtin_types(this);
2979 exp
.export_globals(this->package_name(),
2981 this->package_priority(),
2983 (this->need_init_fn_
&& !this->is_main_package()
2984 ? this->get_init_fn_name()
2986 this->imported_init_fns_
,
2987 this->package_
->bindings());
2990 // Find the blocks in order to convert named types defined in blocks.
2992 class Convert_named_types
: public Traverse
2995 Convert_named_types(Gogo
* gogo
)
2996 : Traverse(traverse_blocks
),
3002 block(Block
* block
);
3009 Convert_named_types::block(Block
* block
)
3011 this->gogo_
->convert_named_types_in_bindings(block
->bindings());
3012 return TRAVERSE_CONTINUE
;
3015 // Convert all named types to the backend representation. Since named
3016 // types can refer to other types, this needs to be done in the right
3017 // sequence, which is handled by Named_type::convert. Here we arrange
3018 // to call that for each named type.
3021 Gogo::convert_named_types()
3023 this->convert_named_types_in_bindings(this->globals_
);
3024 for (Packages::iterator p
= this->packages_
.begin();
3025 p
!= this->packages_
.end();
3028 Package
* package
= p
->second
;
3029 this->convert_named_types_in_bindings(package
->bindings());
3032 Convert_named_types
cnt(this);
3033 this->traverse(&cnt
);
3035 // Make all the builtin named types used for type descriptors, and
3036 // then convert them. They will only be written out if they are
3038 Type::make_type_descriptor_type();
3039 Type::make_type_descriptor_ptr_type();
3040 Function_type::make_function_type_descriptor_type();
3041 Pointer_type::make_pointer_type_descriptor_type();
3042 Struct_type::make_struct_type_descriptor_type();
3043 Array_type::make_array_type_descriptor_type();
3044 Array_type::make_slice_type_descriptor_type();
3045 Map_type::make_map_type_descriptor_type();
3046 Map_type::make_map_descriptor_type();
3047 Channel_type::make_chan_type_descriptor_type();
3048 Interface_type::make_interface_type_descriptor_type();
3049 Type::convert_builtin_named_types(this);
3051 Runtime::convert_types(this);
3053 this->named_types_are_converted_
= true;
3056 // Convert all names types in a set of bindings.
3059 Gogo::convert_named_types_in_bindings(Bindings
* bindings
)
3061 for (Bindings::const_definitions_iterator p
= bindings
->begin_definitions();
3062 p
!= bindings
->end_definitions();
3065 if ((*p
)->is_type())
3066 (*p
)->type_value()->convert(this);
3072 Function::Function(Function_type
* type
, Function
* enclosing
, Block
* block
,
3074 : type_(type
), enclosing_(enclosing
), results_(NULL
),
3075 closure_var_(NULL
), block_(block
), location_(location
), labels_(),
3076 local_type_count_(0), fndecl_(NULL
), defer_stack_(NULL
),
3077 results_are_named_(false), calls_recover_(false), is_recover_thunk_(false),
3078 has_recover_thunk_(false)
3082 // Create the named result variables.
3085 Function::create_result_variables(Gogo
* gogo
)
3087 const Typed_identifier_list
* results
= this->type_
->results();
3088 if (results
== NULL
|| results
->empty())
3091 if (!results
->front().name().empty())
3092 this->results_are_named_
= true;
3094 this->results_
= new Results();
3095 this->results_
->reserve(results
->size());
3097 Block
* block
= this->block_
;
3099 for (Typed_identifier_list::const_iterator p
= results
->begin();
3100 p
!= results
->end();
3103 std::string name
= p
->name();
3104 if (name
.empty() || Gogo::is_sink_name(name
))
3106 static int result_counter
;
3108 snprintf(buf
, sizeof buf
, "$ret%d", result_counter
);
3110 name
= gogo
->pack_hidden_name(buf
, false);
3112 Result_variable
* result
= new Result_variable(p
->type(), this, index
,
3114 Named_object
* no
= block
->bindings()->add_result_variable(name
, result
);
3115 if (no
->is_result_variable())
3116 this->results_
->push_back(no
);
3119 static int dummy_result_count
;
3121 snprintf(buf
, sizeof buf
, "$dret%d", dummy_result_count
);
3122 ++dummy_result_count
;
3123 name
= gogo
->pack_hidden_name(buf
, false);
3124 no
= block
->bindings()->add_result_variable(name
, result
);
3125 go_assert(no
->is_result_variable());
3126 this->results_
->push_back(no
);
3131 // Update the named result variables when cloning a function which
3135 Function::update_result_variables()
3137 if (this->results_
== NULL
)
3140 for (Results::iterator p
= this->results_
->begin();
3141 p
!= this->results_
->end();
3143 (*p
)->result_var_value()->set_function(this);
3146 // Return the closure variable, creating it if necessary.
3149 Function::closure_var()
3151 if (this->closure_var_
== NULL
)
3153 // We don't know the type of the variable yet. We add fields as
3155 Location loc
= this->type_
->location();
3156 Struct_field_list
* sfl
= new Struct_field_list
;
3157 Type
* struct_type
= Type::make_struct_type(sfl
, loc
);
3158 Variable
* var
= new Variable(Type::make_pointer_type(struct_type
),
3159 NULL
, false, true, false, loc
);
3161 this->closure_var_
= Named_object::make_variable("closure", NULL
, var
);
3162 // Note that the new variable is not in any binding contour.
3164 return this->closure_var_
;
3167 // Set the type of the closure variable.
3170 Function::set_closure_type()
3172 if (this->closure_var_
== NULL
)
3174 Named_object
* closure
= this->closure_var_
;
3175 Struct_type
* st
= closure
->var_value()->type()->deref()->struct_type();
3176 unsigned int index
= 0;
3177 for (Closure_fields::const_iterator p
= this->closure_fields_
.begin();
3178 p
!= this->closure_fields_
.end();
3181 Named_object
* no
= p
->first
;
3183 snprintf(buf
, sizeof buf
, "%u", index
);
3184 std::string n
= no
->name() + buf
;
3186 if (no
->is_variable())
3187 var_type
= no
->var_value()->type();
3189 var_type
= no
->result_var_value()->type();
3190 Type
* field_type
= Type::make_pointer_type(var_type
);
3191 st
->push_field(Struct_field(Typed_identifier(n
, field_type
, p
->second
)));
3195 // Return whether this function is a method.
3198 Function::is_method() const
3200 return this->type_
->is_method();
3203 // Add a label definition.
3206 Function::add_label_definition(Gogo
* gogo
, const std::string
& label_name
,
3209 Label
* lnull
= NULL
;
3210 std::pair
<Labels::iterator
, bool> ins
=
3211 this->labels_
.insert(std::make_pair(label_name
, lnull
));
3215 // This is a new label.
3216 label
= new Label(label_name
);
3217 ins
.first
->second
= label
;
3221 // The label was already in the hash table.
3222 label
= ins
.first
->second
;
3223 if (label
->is_defined())
3225 error_at(location
, "label %qs already defined",
3226 Gogo::message_name(label_name
).c_str());
3227 inform(label
->location(), "previous definition of %qs was here",
3228 Gogo::message_name(label_name
).c_str());
3229 return new Label(label_name
);
3233 label
->define(location
, gogo
->bindings_snapshot(location
));
3235 // Issue any errors appropriate for any previous goto's to this
3237 const std::vector
<Bindings_snapshot
*>& refs(label
->refs());
3238 for (std::vector
<Bindings_snapshot
*>::const_iterator p
= refs
.begin();
3241 (*p
)->check_goto_to(gogo
->current_block());
3242 label
->clear_refs();
3247 // Add a reference to a label.
3250 Function::add_label_reference(Gogo
* gogo
, const std::string
& label_name
,
3251 Location location
, bool issue_goto_errors
)
3253 Label
* lnull
= NULL
;
3254 std::pair
<Labels::iterator
, bool> ins
=
3255 this->labels_
.insert(std::make_pair(label_name
, lnull
));
3259 // The label was already in the hash table.
3260 label
= ins
.first
->second
;
3264 go_assert(ins
.first
->second
== NULL
);
3265 label
= new Label(label_name
);
3266 ins
.first
->second
= label
;
3269 label
->set_is_used();
3271 if (issue_goto_errors
)
3273 Bindings_snapshot
* snapshot
= label
->snapshot();
3274 if (snapshot
!= NULL
)
3275 snapshot
->check_goto_from(gogo
->current_block(), location
);
3277 label
->add_snapshot_ref(gogo
->bindings_snapshot(location
));
3283 // Warn about labels that are defined but not used.
3286 Function::check_labels() const
3288 for (Labels::const_iterator p
= this->labels_
.begin();
3289 p
!= this->labels_
.end();
3292 Label
* label
= p
->second
;
3293 if (!label
->is_used())
3294 error_at(label
->location(), "label %qs defined and not used",
3295 Gogo::message_name(label
->name()).c_str());
3299 // Swap one function with another. This is used when building the
3300 // thunk we use to call a function which calls recover. It may not
3301 // work for any other case.
3304 Function::swap_for_recover(Function
*x
)
3306 go_assert(this->enclosing_
== x
->enclosing_
);
3307 std::swap(this->results_
, x
->results_
);
3308 std::swap(this->closure_var_
, x
->closure_var_
);
3309 std::swap(this->block_
, x
->block_
);
3310 go_assert(this->location_
== x
->location_
);
3311 go_assert(this->fndecl_
== NULL
&& x
->fndecl_
== NULL
);
3312 go_assert(this->defer_stack_
== NULL
&& x
->defer_stack_
== NULL
);
3315 // Traverse the tree.
3318 Function::traverse(Traverse
* traverse
)
3320 unsigned int traverse_mask
= traverse
->traverse_mask();
3323 & (Traverse::traverse_types
| Traverse::traverse_expressions
))
3326 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3327 return TRAVERSE_EXIT
;
3330 // FIXME: We should check traverse_functions here if nested
3331 // functions are stored in block bindings.
3332 if (this->block_
!= NULL
3334 & (Traverse::traverse_variables
3335 | Traverse::traverse_constants
3336 | Traverse::traverse_blocks
3337 | Traverse::traverse_statements
3338 | Traverse::traverse_expressions
3339 | Traverse::traverse_types
)) != 0)
3341 if (this->block_
->traverse(traverse
) == TRAVERSE_EXIT
)
3342 return TRAVERSE_EXIT
;
3345 return TRAVERSE_CONTINUE
;
3348 // Work out types for unspecified variables and constants.
3351 Function::determine_types()
3353 if (this->block_
!= NULL
)
3354 this->block_
->determine_types();
3357 // Get a pointer to the variable representing the defer stack for this
3358 // function, making it if necessary. The value of the variable is set
3359 // by the runtime routines to true if the function is returning,
3360 // rather than panicing through. A pointer to this variable is used
3361 // as a marker for the functions on the defer stack associated with
3362 // this function. A function-specific variable permits inlining a
3363 // function which uses defer.
3366 Function::defer_stack(Location location
)
3368 if (this->defer_stack_
== NULL
)
3370 Type
* t
= Type::lookup_bool_type();
3371 Expression
* n
= Expression::make_boolean(false, location
);
3372 this->defer_stack_
= Statement::make_temporary(t
, n
, location
);
3373 this->defer_stack_
->set_is_address_taken();
3375 Expression
* ref
= Expression::make_temporary_reference(this->defer_stack_
,
3377 return Expression::make_unary(OPERATOR_AND
, ref
, location
);
3380 // Export the function.
3383 Function::export_func(Export
* exp
, const std::string
& name
) const
3385 Function::export_func_with_type(exp
, name
, this->type_
);
3388 // Export a function with a type.
3391 Function::export_func_with_type(Export
* exp
, const std::string
& name
,
3392 const Function_type
* fntype
)
3394 exp
->write_c_string("func ");
3396 if (fntype
->is_method())
3398 exp
->write_c_string("(");
3399 const Typed_identifier
* receiver
= fntype
->receiver();
3400 exp
->write_name(receiver
->name());
3401 exp
->write_c_string(" ");
3402 exp
->write_type(receiver
->type());
3403 exp
->write_c_string(") ");
3406 exp
->write_string(name
);
3408 exp
->write_c_string(" (");
3409 const Typed_identifier_list
* parameters
= fntype
->parameters();
3410 if (parameters
!= NULL
)
3412 bool is_varargs
= fntype
->is_varargs();
3414 for (Typed_identifier_list::const_iterator p
= parameters
->begin();
3415 p
!= parameters
->end();
3421 exp
->write_c_string(", ");
3422 exp
->write_name(p
->name());
3423 exp
->write_c_string(" ");
3424 if (!is_varargs
|| p
+ 1 != parameters
->end())
3425 exp
->write_type(p
->type());
3428 exp
->write_c_string("...");
3429 exp
->write_type(p
->type()->array_type()->element_type());
3433 exp
->write_c_string(")");
3435 const Typed_identifier_list
* results
= fntype
->results();
3436 if (results
!= NULL
)
3438 if (results
->size() == 1 && results
->begin()->name().empty())
3440 exp
->write_c_string(" ");
3441 exp
->write_type(results
->begin()->type());
3445 exp
->write_c_string(" (");
3447 for (Typed_identifier_list::const_iterator p
= results
->begin();
3448 p
!= results
->end();
3454 exp
->write_c_string(", ");
3455 exp
->write_name(p
->name());
3456 exp
->write_c_string(" ");
3457 exp
->write_type(p
->type());
3459 exp
->write_c_string(")");
3462 exp
->write_c_string(";\n");
3465 // Import a function.
3468 Function::import_func(Import
* imp
, std::string
* pname
,
3469 Typed_identifier
** preceiver
,
3470 Typed_identifier_list
** pparameters
,
3471 Typed_identifier_list
** presults
,
3474 imp
->require_c_string("func ");
3477 if (imp
->peek_char() == '(')
3479 imp
->require_c_string("(");
3480 std::string name
= imp
->read_name();
3481 imp
->require_c_string(" ");
3482 Type
* rtype
= imp
->read_type();
3483 *preceiver
= new Typed_identifier(name
, rtype
, imp
->location());
3484 imp
->require_c_string(") ");
3487 *pname
= imp
->read_identifier();
3489 Typed_identifier_list
* parameters
;
3490 *is_varargs
= false;
3491 imp
->require_c_string(" (");
3492 if (imp
->peek_char() == ')')
3496 parameters
= new Typed_identifier_list();
3499 std::string name
= imp
->read_name();
3500 imp
->require_c_string(" ");
3502 if (imp
->match_c_string("..."))
3508 Type
* ptype
= imp
->read_type();
3510 ptype
= Type::make_array_type(ptype
, NULL
);
3511 parameters
->push_back(Typed_identifier(name
, ptype
,
3513 if (imp
->peek_char() != ',')
3515 go_assert(!*is_varargs
);
3516 imp
->require_c_string(", ");
3519 imp
->require_c_string(")");
3520 *pparameters
= parameters
;
3522 Typed_identifier_list
* results
;
3523 if (imp
->peek_char() != ' ')
3527 results
= new Typed_identifier_list();
3528 imp
->require_c_string(" ");
3529 if (imp
->peek_char() != '(')
3531 Type
* rtype
= imp
->read_type();
3532 results
->push_back(Typed_identifier("", rtype
, imp
->location()));
3536 imp
->require_c_string("(");
3539 std::string name
= imp
->read_name();
3540 imp
->require_c_string(" ");
3541 Type
* rtype
= imp
->read_type();
3542 results
->push_back(Typed_identifier(name
, rtype
,
3544 if (imp
->peek_char() != ',')
3546 imp
->require_c_string(", ");
3548 imp
->require_c_string(")");
3551 imp
->require_c_string(";\n");
3552 *presults
= results
;
3557 Block::Block(Block
* enclosing
, Location location
)
3558 : enclosing_(enclosing
), statements_(),
3559 bindings_(new Bindings(enclosing
== NULL
3561 : enclosing
->bindings())),
3562 start_location_(location
),
3563 end_location_(UNKNOWN_LOCATION
)
3567 // Add a statement to a block.
3570 Block::add_statement(Statement
* statement
)
3572 this->statements_
.push_back(statement
);
3575 // Add a statement to the front of a block. This is slow but is only
3576 // used for reference counts of parameters.
3579 Block::add_statement_at_front(Statement
* statement
)
3581 this->statements_
.insert(this->statements_
.begin(), statement
);
3584 // Replace a statement in a block.
3587 Block::replace_statement(size_t index
, Statement
* s
)
3589 go_assert(index
< this->statements_
.size());
3590 this->statements_
[index
] = s
;
3593 // Add a statement before another statement.
3596 Block::insert_statement_before(size_t index
, Statement
* s
)
3598 go_assert(index
< this->statements_
.size());
3599 this->statements_
.insert(this->statements_
.begin() + index
, s
);
3602 // Add a statement after another statement.
3605 Block::insert_statement_after(size_t index
, Statement
* s
)
3607 go_assert(index
< this->statements_
.size());
3608 this->statements_
.insert(this->statements_
.begin() + index
+ 1, s
);
3611 // Traverse the tree.
3614 Block::traverse(Traverse
* traverse
)
3616 unsigned int traverse_mask
= traverse
->traverse_mask();
3618 if ((traverse_mask
& Traverse::traverse_blocks
) != 0)
3620 int t
= traverse
->block(this);
3621 if (t
== TRAVERSE_EXIT
)
3622 return TRAVERSE_EXIT
;
3623 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
3624 return TRAVERSE_CONTINUE
;
3628 & (Traverse::traverse_variables
3629 | Traverse::traverse_constants
3630 | Traverse::traverse_expressions
3631 | Traverse::traverse_types
)) != 0)
3633 const unsigned int e_or_t
= (Traverse::traverse_expressions
3634 | Traverse::traverse_types
);
3635 const unsigned int e_or_t_or_s
= (e_or_t
3636 | Traverse::traverse_statements
);
3637 for (Bindings::const_definitions_iterator pb
=
3638 this->bindings_
->begin_definitions();
3639 pb
!= this->bindings_
->end_definitions();
3642 int t
= TRAVERSE_CONTINUE
;
3643 switch ((*pb
)->classification())
3645 case Named_object::NAMED_OBJECT_CONST
:
3646 if ((traverse_mask
& Traverse::traverse_constants
) != 0)
3647 t
= traverse
->constant(*pb
, false);
3648 if (t
== TRAVERSE_CONTINUE
3649 && (traverse_mask
& e_or_t
) != 0)
3651 Type
* tc
= (*pb
)->const_value()->type();
3653 && Type::traverse(tc
, traverse
) == TRAVERSE_EXIT
)
3654 return TRAVERSE_EXIT
;
3655 t
= (*pb
)->const_value()->traverse_expression(traverse
);
3659 case Named_object::NAMED_OBJECT_VAR
:
3660 case Named_object::NAMED_OBJECT_RESULT_VAR
:
3661 if ((traverse_mask
& Traverse::traverse_variables
) != 0)
3662 t
= traverse
->variable(*pb
);
3663 if (t
== TRAVERSE_CONTINUE
3664 && (traverse_mask
& e_or_t
) != 0)
3666 if ((*pb
)->is_result_variable()
3667 || (*pb
)->var_value()->has_type())
3669 Type
* tv
= ((*pb
)->is_variable()
3670 ? (*pb
)->var_value()->type()
3671 : (*pb
)->result_var_value()->type());
3673 && Type::traverse(tv
, traverse
) == TRAVERSE_EXIT
)
3674 return TRAVERSE_EXIT
;
3677 if (t
== TRAVERSE_CONTINUE
3678 && (traverse_mask
& e_or_t_or_s
) != 0
3679 && (*pb
)->is_variable())
3680 t
= (*pb
)->var_value()->traverse_expression(traverse
,
3684 case Named_object::NAMED_OBJECT_FUNC
:
3685 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
3688 case Named_object::NAMED_OBJECT_TYPE
:
3689 if ((traverse_mask
& e_or_t
) != 0)
3690 t
= Type::traverse((*pb
)->type_value(), traverse
);
3693 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
3694 case Named_object::NAMED_OBJECT_UNKNOWN
:
3695 case Named_object::NAMED_OBJECT_ERRONEOUS
:
3698 case Named_object::NAMED_OBJECT_PACKAGE
:
3699 case Named_object::NAMED_OBJECT_SINK
:
3706 if (t
== TRAVERSE_EXIT
)
3707 return TRAVERSE_EXIT
;
3711 // No point in checking traverse_mask here--if we got here we always
3712 // want to walk the statements. The traversal can insert new
3713 // statements before or after the current statement. Inserting
3714 // statements before the current statement requires updating I via
3715 // the pointer; those statements will not be traversed. Any new
3716 // statements inserted after the current statement will be traversed
3718 for (size_t i
= 0; i
< this->statements_
.size(); ++i
)
3720 if (this->statements_
[i
]->traverse(this, &i
, traverse
) == TRAVERSE_EXIT
)
3721 return TRAVERSE_EXIT
;
3724 return TRAVERSE_CONTINUE
;
3727 // Work out types for unspecified variables and constants.
3730 Block::determine_types()
3732 for (Bindings::const_definitions_iterator pb
=
3733 this->bindings_
->begin_definitions();
3734 pb
!= this->bindings_
->end_definitions();
3737 if ((*pb
)->is_variable())
3738 (*pb
)->var_value()->determine_type();
3739 else if ((*pb
)->is_const())
3740 (*pb
)->const_value()->determine_type();
3743 for (std::vector
<Statement
*>::const_iterator ps
= this->statements_
.begin();
3744 ps
!= this->statements_
.end();
3746 (*ps
)->determine_types();
3749 // Return true if the statements in this block may fall through.
3752 Block::may_fall_through() const
3754 if (this->statements_
.empty())
3756 return this->statements_
.back()->may_fall_through();
3759 // Convert a block to the backend representation.
3762 Block::get_backend(Translate_context
* context
)
3764 Gogo
* gogo
= context
->gogo();
3765 Named_object
* function
= context
->function();
3766 std::vector
<Bvariable
*> vars
;
3767 vars
.reserve(this->bindings_
->size_definitions());
3768 for (Bindings::const_definitions_iterator pv
=
3769 this->bindings_
->begin_definitions();
3770 pv
!= this->bindings_
->end_definitions();
3773 if ((*pv
)->is_variable() && !(*pv
)->var_value()->is_parameter())
3774 vars
.push_back((*pv
)->get_backend_variable(gogo
, function
));
3777 // FIXME: Permitting FUNCTION to be NULL here is a temporary measure
3778 // until we have a proper representation of the init function.
3779 Bfunction
* bfunction
;
3780 if (function
== NULL
)
3783 bfunction
= tree_to_function(function
->func_value()->get_decl());
3784 Bblock
* ret
= context
->backend()->block(bfunction
, context
->bblock(),
3785 vars
, this->start_location_
,
3786 this->end_location_
);
3788 Translate_context
subcontext(gogo
, function
, this, ret
);
3789 std::vector
<Bstatement
*> bstatements
;
3790 bstatements
.reserve(this->statements_
.size());
3791 for (std::vector
<Statement
*>::const_iterator p
= this->statements_
.begin();
3792 p
!= this->statements_
.end();
3794 bstatements
.push_back((*p
)->get_backend(&subcontext
));
3796 context
->backend()->block_add_statements(ret
, bstatements
);
3801 // Class Bindings_snapshot.
3803 Bindings_snapshot::Bindings_snapshot(const Block
* b
, Location location
)
3804 : block_(b
), counts_(), location_(location
)
3808 this->counts_
.push_back(b
->bindings()->size_definitions());
3813 // Report errors appropriate for a goto from B to this.
3816 Bindings_snapshot::check_goto_from(const Block
* b
, Location loc
)
3819 if (!this->check_goto_block(loc
, b
, this->block_
, &dummy
))
3821 this->check_goto_defs(loc
, this->block_
,
3822 this->block_
->bindings()->size_definitions(),
3826 // Report errors appropriate for a goto from this to B.
3829 Bindings_snapshot::check_goto_to(const Block
* b
)
3832 if (!this->check_goto_block(this->location_
, this->block_
, b
, &index
))
3834 this->check_goto_defs(this->location_
, b
, this->counts_
[index
],
3835 b
->bindings()->size_definitions());
3838 // Report errors appropriate for a goto at LOC from BFROM to BTO.
3839 // Return true if all is well, false if we reported an error. If this
3840 // returns true, it sets *PINDEX to the number of blocks BTO is above
3844 Bindings_snapshot::check_goto_block(Location loc
, const Block
* bfrom
,
3845 const Block
* bto
, size_t* pindex
)
3847 // It is an error if BTO is not either BFROM or above BFROM.
3849 for (const Block
* pb
= bfrom
; pb
!= bto
; pb
= pb
->enclosing(), ++index
)
3853 error_at(loc
, "goto jumps into block");
3854 inform(bto
->start_location(), "goto target block starts here");
3862 // Report errors appropriate for a goto at LOC ending at BLOCK, where
3863 // CFROM is the number of names defined at the point of the goto and
3864 // CTO is the number of names defined at the point of the label.
3867 Bindings_snapshot::check_goto_defs(Location loc
, const Block
* block
,
3868 size_t cfrom
, size_t cto
)
3872 Bindings::const_definitions_iterator p
=
3873 block
->bindings()->begin_definitions();
3874 for (size_t i
= 0; i
< cfrom
; ++i
)
3876 go_assert(p
!= block
->bindings()->end_definitions());
3879 go_assert(p
!= block
->bindings()->end_definitions());
3881 std::string n
= (*p
)->message_name();
3882 error_at(loc
, "goto jumps over declaration of %qs", n
.c_str());
3883 inform((*p
)->location(), "%qs defined here", n
.c_str());
3889 Variable::Variable(Type
* type
, Expression
* init
, bool is_global
,
3890 bool is_parameter
, bool is_receiver
,
3892 : type_(type
), init_(init
), preinit_(NULL
), location_(location
),
3893 backend_(NULL
), is_global_(is_global
), is_parameter_(is_parameter
),
3894 is_receiver_(is_receiver
), is_varargs_parameter_(false), is_used_(false),
3895 is_address_taken_(false), is_non_escaping_address_taken_(false),
3896 seen_(false), init_is_lowered_(false), type_from_init_tuple_(false),
3897 type_from_range_index_(false), type_from_range_value_(false),
3898 type_from_chan_element_(false), is_type_switch_var_(false),
3899 determined_type_(false)
3901 go_assert(type
!= NULL
|| init
!= NULL
);
3902 go_assert(!is_parameter
|| init
== NULL
);
3905 // Traverse the initializer expression.
3908 Variable::traverse_expression(Traverse
* traverse
, unsigned int traverse_mask
)
3910 if (this->preinit_
!= NULL
)
3912 if (this->preinit_
->traverse(traverse
) == TRAVERSE_EXIT
)
3913 return TRAVERSE_EXIT
;
3915 if (this->init_
!= NULL
3917 & (Traverse::traverse_expressions
| Traverse::traverse_types
))
3920 if (Expression::traverse(&this->init_
, traverse
) == TRAVERSE_EXIT
)
3921 return TRAVERSE_EXIT
;
3923 return TRAVERSE_CONTINUE
;
3926 // Lower the initialization expression after parsing is complete.
3929 Variable::lower_init_expression(Gogo
* gogo
, Named_object
* function
,
3930 Statement_inserter
* inserter
)
3932 Named_object
* dep
= gogo
->var_depends_on(this);
3933 if (dep
!= NULL
&& dep
->is_variable())
3934 dep
->var_value()->lower_init_expression(gogo
, function
, inserter
);
3936 if (this->init_
!= NULL
&& !this->init_is_lowered_
)
3940 // We will give an error elsewhere, this is just to prevent
3941 // an infinite loop.
3946 Statement_inserter global_inserter
;
3947 if (this->is_global_
)
3949 global_inserter
= Statement_inserter(gogo
, this);
3950 inserter
= &global_inserter
;
3953 gogo
->lower_expression(function
, inserter
, &this->init_
);
3955 this->seen_
= false;
3957 this->init_is_lowered_
= true;
3961 // Get the preinit block.
3964 Variable::preinit_block(Gogo
* gogo
)
3966 go_assert(this->is_global_
);
3967 if (this->preinit_
== NULL
)
3968 this->preinit_
= new Block(NULL
, this->location());
3970 // If a global variable has a preinitialization statement, then we
3971 // need to have an initialization function.
3972 gogo
->set_need_init_fn();
3974 return this->preinit_
;
3977 // Add a statement to be run before the initialization expression.
3980 Variable::add_preinit_statement(Gogo
* gogo
, Statement
* s
)
3982 Block
* b
= this->preinit_block(gogo
);
3983 b
->add_statement(s
);
3984 b
->set_end_location(s
->location());
3987 // Whether this variable has a type.
3990 Variable::has_type() const
3992 if (this->type_
== NULL
)
3995 // A variable created in a type switch case nil does not actually
3996 // have a type yet. It will be changed to use the initializer's
3997 // type in determine_type.
3998 if (this->is_type_switch_var_
3999 && this->type_
->is_nil_constant_as_type())
4005 // In an assignment which sets a variable to a tuple of EXPR, return
4006 // the type of the first element of the tuple.
4009 Variable::type_from_tuple(Expression
* expr
, bool report_error
) const
4011 if (expr
->map_index_expression() != NULL
)
4013 Map_type
* mt
= expr
->map_index_expression()->get_map_type();
4015 return Type::make_error_type();
4016 return mt
->val_type();
4018 else if (expr
->receive_expression() != NULL
)
4020 Expression
* channel
= expr
->receive_expression()->channel();
4021 Type
* channel_type
= channel
->type();
4022 if (channel_type
->channel_type() == NULL
)
4023 return Type::make_error_type();
4024 return channel_type
->channel_type()->element_type();
4029 error_at(this->location(), "invalid tuple definition");
4030 return Type::make_error_type();
4034 // Given EXPR used in a range clause, return either the index type or
4035 // the value type of the range, depending upon GET_INDEX_TYPE.
4038 Variable::type_from_range(Expression
* expr
, bool get_index_type
,
4039 bool report_error
) const
4041 Type
* t
= expr
->type();
4042 if (t
->array_type() != NULL
4043 || (t
->points_to() != NULL
4044 && t
->points_to()->array_type() != NULL
4045 && !t
->points_to()->is_slice_type()))
4048 return Type::lookup_integer_type("int");
4050 return t
->deref()->array_type()->element_type();
4052 else if (t
->is_string_type())
4055 return Type::lookup_integer_type("int");
4057 return Type::lookup_integer_type("int32");
4059 else if (t
->map_type() != NULL
)
4062 return t
->map_type()->key_type();
4064 return t
->map_type()->val_type();
4066 else if (t
->channel_type() != NULL
)
4069 return t
->channel_type()->element_type();
4073 error_at(this->location(),
4074 "invalid definition of value variable for channel range");
4075 return Type::make_error_type();
4081 error_at(this->location(), "invalid type for range clause");
4082 return Type::make_error_type();
4086 // EXPR should be a channel. Return the channel's element type.
4089 Variable::type_from_chan_element(Expression
* expr
, bool report_error
) const
4091 Type
* t
= expr
->type();
4092 if (t
->channel_type() != NULL
)
4093 return t
->channel_type()->element_type();
4097 error_at(this->location(), "expected channel");
4098 return Type::make_error_type();
4102 // Return the type of the Variable. This may be called before
4103 // Variable::determine_type is called, which means that we may need to
4104 // get the type from the initializer. FIXME: If we combine lowering
4105 // with type determination, then this should be unnecessary.
4110 // A variable in a type switch with a nil case will have the wrong
4111 // type here. This gets fixed up in determine_type, below.
4112 Type
* type
= this->type_
;
4113 Expression
* init
= this->init_
;
4114 if (this->is_type_switch_var_
4115 && this->type_
->is_nil_constant_as_type())
4117 Type_guard_expression
* tge
= this->init_
->type_guard_expression();
4118 go_assert(tge
!= NULL
);
4125 if (this->type_
== NULL
|| !this->type_
->is_error_type())
4127 error_at(this->location_
, "variable initializer refers to itself");
4128 this->type_
= Type::make_error_type();
4137 else if (this->type_from_init_tuple_
)
4138 type
= this->type_from_tuple(init
, false);
4139 else if (this->type_from_range_index_
|| this->type_from_range_value_
)
4140 type
= this->type_from_range(init
, this->type_from_range_index_
, false);
4141 else if (this->type_from_chan_element_
)
4142 type
= this->type_from_chan_element(init
, false);
4145 go_assert(init
!= NULL
);
4146 type
= init
->type();
4147 go_assert(type
!= NULL
);
4149 // Variables should not have abstract types.
4150 if (type
->is_abstract())
4151 type
= type
->make_non_abstract_type();
4153 if (type
->is_void_type())
4154 type
= Type::make_error_type();
4157 this->seen_
= false;
4162 // Fetch the type from a const pointer, in which case it should have
4163 // been set already.
4166 Variable::type() const
4168 go_assert(this->type_
!= NULL
);
4172 // Set the type if necessary.
4175 Variable::determine_type()
4177 if (this->determined_type_
)
4179 this->determined_type_
= true;
4181 if (this->preinit_
!= NULL
)
4182 this->preinit_
->determine_types();
4184 // A variable in a type switch with a nil case will have the wrong
4185 // type here. It will have an initializer which is a type guard.
4186 // We want to initialize it to the value without the type guard, and
4187 // use the type of that value as well.
4188 if (this->is_type_switch_var_
&& this->type_
->is_nil_constant_as_type())
4190 Type_guard_expression
* tge
= this->init_
->type_guard_expression();
4191 go_assert(tge
!= NULL
);
4193 this->init_
= tge
->expr();
4196 if (this->init_
== NULL
)
4197 go_assert(this->type_
!= NULL
&& !this->type_
->is_abstract());
4198 else if (this->type_from_init_tuple_
)
4200 Expression
*init
= this->init_
;
4201 init
->determine_type_no_context();
4202 this->type_
= this->type_from_tuple(init
, true);
4205 else if (this->type_from_range_index_
|| this->type_from_range_value_
)
4207 Expression
* init
= this->init_
;
4208 init
->determine_type_no_context();
4209 this->type_
= this->type_from_range(init
, this->type_from_range_index_
,
4213 else if (this->type_from_chan_element_
)
4215 Expression
* init
= this->init_
;
4216 init
->determine_type_no_context();
4217 this->type_
= this->type_from_chan_element(init
, true);
4222 Type_context
context(this->type_
, false);
4223 this->init_
->determine_type(&context
);
4224 if (this->type_
== NULL
)
4226 Type
* type
= this->init_
->type();
4227 go_assert(type
!= NULL
);
4228 if (type
->is_abstract())
4229 type
= type
->make_non_abstract_type();
4231 if (type
->is_void_type())
4233 error_at(this->location_
, "variable has no type");
4234 type
= Type::make_error_type();
4236 else if (type
->is_nil_type())
4238 error_at(this->location_
, "variable defined to nil type");
4239 type
= Type::make_error_type();
4241 else if (type
->is_call_multiple_result_type())
4243 error_at(this->location_
,
4244 "single variable set to multiple-value function call");
4245 type
= Type::make_error_type();
4253 // Export the variable
4256 Variable::export_var(Export
* exp
, const std::string
& name
) const
4258 go_assert(this->is_global_
);
4259 exp
->write_c_string("var ");
4260 exp
->write_string(name
);
4261 exp
->write_c_string(" ");
4262 exp
->write_type(this->type());
4263 exp
->write_c_string(";\n");
4266 // Import a variable.
4269 Variable::import_var(Import
* imp
, std::string
* pname
, Type
** ptype
)
4271 imp
->require_c_string("var ");
4272 *pname
= imp
->read_identifier();
4273 imp
->require_c_string(" ");
4274 *ptype
= imp
->read_type();
4275 imp
->require_c_string(";\n");
4278 // Convert a variable to the backend representation.
4281 Variable::get_backend_variable(Gogo
* gogo
, Named_object
* function
,
4282 const Package
* package
, const std::string
& name
)
4284 if (this->backend_
== NULL
)
4286 Backend
* backend
= gogo
->backend();
4287 Type
* type
= this->type_
;
4288 if (type
->is_error_type()
4289 || (type
->is_undefined()
4290 && (!this->is_global_
|| package
== NULL
)))
4291 this->backend_
= backend
->error_variable();
4294 bool is_parameter
= this->is_parameter_
;
4295 if (this->is_receiver_
&& type
->points_to() == NULL
)
4296 is_parameter
= false;
4297 if (this->is_in_heap())
4299 is_parameter
= false;
4300 type
= Type::make_pointer_type(type
);
4303 std::string n
= Gogo::unpack_hidden_name(name
);
4304 Btype
* btype
= type
->get_backend(gogo
);
4307 if (this->is_global_
)
4308 bvar
= backend
->global_variable((package
== NULL
4309 ? gogo
->package_name()
4310 : package
->package_name()),
4312 ? gogo
->pkgpath_symbol()
4313 : package
->pkgpath_symbol()),
4317 Gogo::is_hidden_name(name
),
4319 else if (function
== NULL
)
4321 go_assert(saw_errors());
4322 bvar
= backend
->error_variable();
4326 tree fndecl
= function
->func_value()->get_decl();
4327 Bfunction
* bfunction
= tree_to_function(fndecl
);
4328 bool is_address_taken
= (this->is_non_escaping_address_taken_
4329 && !this->is_in_heap());
4331 bvar
= backend
->parameter_variable(bfunction
, n
, btype
,
4335 bvar
= backend
->local_variable(bfunction
, n
, btype
,
4339 this->backend_
= bvar
;
4342 return this->backend_
;
4345 // Class Result_variable.
4347 // Convert a result variable to the backend representation.
4350 Result_variable::get_backend_variable(Gogo
* gogo
, Named_object
* function
,
4351 const std::string
& name
)
4353 if (this->backend_
== NULL
)
4355 Backend
* backend
= gogo
->backend();
4356 Type
* type
= this->type_
;
4357 if (type
->is_error())
4358 this->backend_
= backend
->error_variable();
4361 if (this->is_in_heap())
4362 type
= Type::make_pointer_type(type
);
4363 Btype
* btype
= type
->get_backend(gogo
);
4364 tree fndecl
= function
->func_value()->get_decl();
4365 Bfunction
* bfunction
= tree_to_function(fndecl
);
4366 std::string n
= Gogo::unpack_hidden_name(name
);
4367 bool is_address_taken
= (this->is_non_escaping_address_taken_
4368 && !this->is_in_heap());
4369 this->backend_
= backend
->local_variable(bfunction
, n
, btype
,
4374 return this->backend_
;
4377 // Class Named_constant.
4379 // Traverse the initializer expression.
4382 Named_constant::traverse_expression(Traverse
* traverse
)
4384 return Expression::traverse(&this->expr_
, traverse
);
4387 // Determine the type of the constant.
4390 Named_constant::determine_type()
4392 if (this->type_
!= NULL
)
4394 Type_context
context(this->type_
, false);
4395 this->expr_
->determine_type(&context
);
4399 // A constant may have an abstract type.
4400 Type_context
context(NULL
, true);
4401 this->expr_
->determine_type(&context
);
4402 this->type_
= this->expr_
->type();
4403 go_assert(this->type_
!= NULL
);
4407 // Indicate that we found and reported an error for this constant.
4410 Named_constant::set_error()
4412 this->type_
= Type::make_error_type();
4413 this->expr_
= Expression::make_error(this->location_
);
4416 // Export a constant.
4419 Named_constant::export_const(Export
* exp
, const std::string
& name
) const
4421 exp
->write_c_string("const ");
4422 exp
->write_string(name
);
4423 exp
->write_c_string(" ");
4424 if (!this->type_
->is_abstract())
4426 exp
->write_type(this->type_
);
4427 exp
->write_c_string(" ");
4429 exp
->write_c_string("= ");
4430 this->expr()->export_expression(exp
);
4431 exp
->write_c_string(";\n");
4434 // Import a constant.
4437 Named_constant::import_const(Import
* imp
, std::string
* pname
, Type
** ptype
,
4440 imp
->require_c_string("const ");
4441 *pname
= imp
->read_identifier();
4442 imp
->require_c_string(" ");
4443 if (imp
->peek_char() == '=')
4447 *ptype
= imp
->read_type();
4448 imp
->require_c_string(" ");
4450 imp
->require_c_string("= ");
4451 *pexpr
= Expression::import_expression(imp
);
4452 imp
->require_c_string(";\n");
4458 Type_declaration::add_method(const std::string
& name
, Function
* function
)
4460 Named_object
* ret
= Named_object::make_function(name
, NULL
, function
);
4461 this->methods_
.push_back(ret
);
4465 // Add a method declaration.
4468 Type_declaration::add_method_declaration(const std::string
& name
,
4470 Function_type
* type
,
4473 Named_object
* ret
= Named_object::make_function_declaration(name
, package
,
4475 this->methods_
.push_back(ret
);
4479 // Return whether any methods ere defined.
4482 Type_declaration::has_methods() const
4484 return !this->methods_
.empty();
4487 // Define methods for the real type.
4490 Type_declaration::define_methods(Named_type
* nt
)
4492 for (Methods::const_iterator p
= this->methods_
.begin();
4493 p
!= this->methods_
.end();
4495 nt
->add_existing_method(*p
);
4498 // We are using the type. Return true if we should issue a warning.
4501 Type_declaration::using_type()
4503 bool ret
= !this->issued_warning_
;
4504 this->issued_warning_
= true;
4508 // Class Unknown_name.
4510 // Set the real named object.
4513 Unknown_name::set_real_named_object(Named_object
* no
)
4515 go_assert(this->real_named_object_
== NULL
);
4516 go_assert(!no
->is_unknown());
4517 this->real_named_object_
= no
;
4520 // Class Named_object.
4522 Named_object::Named_object(const std::string
& name
,
4523 const Package
* package
,
4524 Classification classification
)
4525 : name_(name
), package_(package
), classification_(classification
),
4528 if (Gogo::is_sink_name(name
))
4529 go_assert(classification
== NAMED_OBJECT_SINK
);
4532 // Make an unknown name. This is used by the parser. The name must
4533 // be resolved later. Unknown names are only added in the current
4537 Named_object::make_unknown_name(const std::string
& name
,
4540 Named_object
* named_object
= new Named_object(name
, NULL
,
4541 NAMED_OBJECT_UNKNOWN
);
4542 Unknown_name
* value
= new Unknown_name(location
);
4543 named_object
->u_
.unknown_value
= value
;
4544 return named_object
;
4550 Named_object::make_constant(const Typed_identifier
& tid
,
4551 const Package
* package
, Expression
* expr
,
4554 Named_object
* named_object
= new Named_object(tid
.name(), package
,
4555 NAMED_OBJECT_CONST
);
4556 Named_constant
* named_constant
= new Named_constant(tid
.type(), expr
,
4559 named_object
->u_
.const_value
= named_constant
;
4560 return named_object
;
4563 // Make a named type.
4566 Named_object::make_type(const std::string
& name
, const Package
* package
,
4567 Type
* type
, Location location
)
4569 Named_object
* named_object
= new Named_object(name
, package
,
4571 Named_type
* named_type
= Type::make_named_type(named_object
, type
, location
);
4572 named_object
->u_
.type_value
= named_type
;
4573 return named_object
;
4576 // Make a type declaration.
4579 Named_object::make_type_declaration(const std::string
& name
,
4580 const Package
* package
,
4583 Named_object
* named_object
= new Named_object(name
, package
,
4584 NAMED_OBJECT_TYPE_DECLARATION
);
4585 Type_declaration
* type_declaration
= new Type_declaration(location
);
4586 named_object
->u_
.type_declaration
= type_declaration
;
4587 return named_object
;
4593 Named_object::make_variable(const std::string
& name
, const Package
* package
,
4596 Named_object
* named_object
= new Named_object(name
, package
,
4598 named_object
->u_
.var_value
= variable
;
4599 return named_object
;
4602 // Make a result variable.
4605 Named_object::make_result_variable(const std::string
& name
,
4606 Result_variable
* result
)
4608 Named_object
* named_object
= new Named_object(name
, NULL
,
4609 NAMED_OBJECT_RESULT_VAR
);
4610 named_object
->u_
.result_var_value
= result
;
4611 return named_object
;
4614 // Make a sink. This is used for the special blank identifier _.
4617 Named_object::make_sink()
4619 return new Named_object("_", NULL
, NAMED_OBJECT_SINK
);
4622 // Make a named function.
4625 Named_object::make_function(const std::string
& name
, const Package
* package
,
4628 Named_object
* named_object
= new Named_object(name
, package
,
4630 named_object
->u_
.func_value
= function
;
4631 return named_object
;
4634 // Make a function declaration.
4637 Named_object::make_function_declaration(const std::string
& name
,
4638 const Package
* package
,
4639 Function_type
* fntype
,
4642 Named_object
* named_object
= new Named_object(name
, package
,
4643 NAMED_OBJECT_FUNC_DECLARATION
);
4644 Function_declaration
*func_decl
= new Function_declaration(fntype
, location
);
4645 named_object
->u_
.func_declaration_value
= func_decl
;
4646 return named_object
;
4652 Named_object::make_package(const std::string
& alias
, Package
* package
)
4654 Named_object
* named_object
= new Named_object(alias
, NULL
,
4655 NAMED_OBJECT_PACKAGE
);
4656 named_object
->u_
.package_value
= package
;
4657 return named_object
;
4660 // Return the name to use in an error message.
4663 Named_object::message_name() const
4665 if (this->package_
== NULL
)
4666 return Gogo::message_name(this->name_
);
4668 if (this->package_
->has_package_name())
4669 ret
= this->package_
->package_name();
4671 ret
= this->package_
->pkgpath();
4672 ret
= Gogo::message_name(ret
);
4674 ret
+= Gogo::message_name(this->name_
);
4678 // Set the type when a declaration is defined.
4681 Named_object::set_type_value(Named_type
* named_type
)
4683 go_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
4684 Type_declaration
* td
= this->u_
.type_declaration
;
4685 td
->define_methods(named_type
);
4687 Named_object
* in_function
= td
->in_function(&index
);
4688 if (in_function
!= NULL
)
4689 named_type
->set_in_function(in_function
, index
);
4691 this->classification_
= NAMED_OBJECT_TYPE
;
4692 this->u_
.type_value
= named_type
;
4695 // Define a function which was previously declared.
4698 Named_object::set_function_value(Function
* function
)
4700 go_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
4701 this->classification_
= NAMED_OBJECT_FUNC
;
4702 // FIXME: We should free the old value.
4703 this->u_
.func_value
= function
;
4706 // Declare an unknown object as a type declaration.
4709 Named_object::declare_as_type()
4711 go_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
4712 Unknown_name
* unk
= this->u_
.unknown_value
;
4713 this->classification_
= NAMED_OBJECT_TYPE_DECLARATION
;
4714 this->u_
.type_declaration
= new Type_declaration(unk
->location());
4718 // Return the location of a named object.
4721 Named_object::location() const
4723 switch (this->classification_
)
4726 case NAMED_OBJECT_UNINITIALIZED
:
4729 case NAMED_OBJECT_ERRONEOUS
:
4730 return Linemap::unknown_location();
4732 case NAMED_OBJECT_UNKNOWN
:
4733 return this->unknown_value()->location();
4735 case NAMED_OBJECT_CONST
:
4736 return this->const_value()->location();
4738 case NAMED_OBJECT_TYPE
:
4739 return this->type_value()->location();
4741 case NAMED_OBJECT_TYPE_DECLARATION
:
4742 return this->type_declaration_value()->location();
4744 case NAMED_OBJECT_VAR
:
4745 return this->var_value()->location();
4747 case NAMED_OBJECT_RESULT_VAR
:
4748 return this->result_var_value()->location();
4750 case NAMED_OBJECT_SINK
:
4753 case NAMED_OBJECT_FUNC
:
4754 return this->func_value()->location();
4756 case NAMED_OBJECT_FUNC_DECLARATION
:
4757 return this->func_declaration_value()->location();
4759 case NAMED_OBJECT_PACKAGE
:
4760 return this->package_value()->location();
4764 // Export a named object.
4767 Named_object::export_named_object(Export
* exp
) const
4769 switch (this->classification_
)
4772 case NAMED_OBJECT_UNINITIALIZED
:
4773 case NAMED_OBJECT_UNKNOWN
:
4776 case NAMED_OBJECT_ERRONEOUS
:
4779 case NAMED_OBJECT_CONST
:
4780 this->const_value()->export_const(exp
, this->name_
);
4783 case NAMED_OBJECT_TYPE
:
4784 this->type_value()->export_named_type(exp
, this->name_
);
4787 case NAMED_OBJECT_TYPE_DECLARATION
:
4788 error_at(this->type_declaration_value()->location(),
4789 "attempt to export %<%s%> which was declared but not defined",
4790 this->message_name().c_str());
4793 case NAMED_OBJECT_FUNC_DECLARATION
:
4794 this->func_declaration_value()->export_func(exp
, this->name_
);
4797 case NAMED_OBJECT_VAR
:
4798 this->var_value()->export_var(exp
, this->name_
);
4801 case NAMED_OBJECT_RESULT_VAR
:
4802 case NAMED_OBJECT_SINK
:
4805 case NAMED_OBJECT_FUNC
:
4806 this->func_value()->export_func(exp
, this->name_
);
4811 // Convert a variable to the backend representation.
4814 Named_object::get_backend_variable(Gogo
* gogo
, Named_object
* function
)
4816 if (this->classification_
== NAMED_OBJECT_VAR
)
4817 return this->var_value()->get_backend_variable(gogo
, function
,
4818 this->package_
, this->name_
);
4819 else if (this->classification_
== NAMED_OBJECT_RESULT_VAR
)
4820 return this->result_var_value()->get_backend_variable(gogo
, function
,
4828 Bindings::Bindings(Bindings
* enclosing
)
4829 : enclosing_(enclosing
), named_objects_(), bindings_()
4836 Bindings::clear_file_scope()
4838 Contour::iterator p
= this->bindings_
.begin();
4839 while (p
!= this->bindings_
.end())
4842 if (p
->second
->package() != NULL
)
4844 else if (p
->second
->is_package())
4846 else if (p
->second
->is_function()
4847 && !p
->second
->func_value()->type()->is_method()
4848 && Gogo::unpack_hidden_name(p
->second
->name()) == "init")
4856 p
= this->bindings_
.erase(p
);
4860 // Look up a symbol.
4863 Bindings::lookup(const std::string
& name
) const
4865 Contour::const_iterator p
= this->bindings_
.find(name
);
4866 if (p
!= this->bindings_
.end())
4867 return p
->second
->resolve();
4868 else if (this->enclosing_
!= NULL
)
4869 return this->enclosing_
->lookup(name
);
4874 // Look up a symbol locally.
4877 Bindings::lookup_local(const std::string
& name
) const
4879 Contour::const_iterator p
= this->bindings_
.find(name
);
4880 if (p
== this->bindings_
.end())
4885 // Remove an object from a set of bindings. This is used for a
4886 // special case in thunks for functions which call recover.
4889 Bindings::remove_binding(Named_object
* no
)
4891 Contour::iterator pb
= this->bindings_
.find(no
->name());
4892 go_assert(pb
!= this->bindings_
.end());
4893 this->bindings_
.erase(pb
);
4894 for (std::vector
<Named_object
*>::iterator pn
= this->named_objects_
.begin();
4895 pn
!= this->named_objects_
.end();
4900 this->named_objects_
.erase(pn
);
4907 // Add a method to the list of objects. This is not added to the
4908 // lookup table. This is so that we have a single list of objects
4909 // declared at the top level, which we walk through when it's time to
4910 // convert to trees.
4913 Bindings::add_method(Named_object
* method
)
4915 this->named_objects_
.push_back(method
);
4918 // Add a generic Named_object to a Contour.
4921 Bindings::add_named_object_to_contour(Contour
* contour
,
4922 Named_object
* named_object
)
4924 go_assert(named_object
== named_object
->resolve());
4925 const std::string
& name(named_object
->name());
4926 go_assert(!Gogo::is_sink_name(name
));
4928 std::pair
<Contour::iterator
, bool> ins
=
4929 contour
->insert(std::make_pair(name
, named_object
));
4932 // The name was already there.
4933 if (named_object
->package() != NULL
4934 && ins
.first
->second
->package() == named_object
->package()
4935 && (ins
.first
->second
->classification()
4936 == named_object
->classification()))
4938 // This is a second import of the same object.
4939 return ins
.first
->second
;
4941 ins
.first
->second
= this->new_definition(ins
.first
->second
,
4943 return ins
.first
->second
;
4947 // Don't push declarations on the list. We push them on when
4948 // and if we find the definitions. That way we genericize the
4949 // functions in order.
4950 if (!named_object
->is_type_declaration()
4951 && !named_object
->is_function_declaration()
4952 && !named_object
->is_unknown())
4953 this->named_objects_
.push_back(named_object
);
4954 return named_object
;
4958 // We had an existing named object OLD_OBJECT, and we've seen a new
4959 // one NEW_OBJECT with the same name. FIXME: This does not free the
4960 // new object when we don't need it.
4963 Bindings::new_definition(Named_object
* old_object
, Named_object
* new_object
)
4965 if (new_object
->is_erroneous() && !old_object
->is_erroneous())
4969 switch (old_object
->classification())
4972 case Named_object::NAMED_OBJECT_UNINITIALIZED
:
4975 case Named_object::NAMED_OBJECT_ERRONEOUS
:
4978 case Named_object::NAMED_OBJECT_UNKNOWN
:
4980 Named_object
* real
= old_object
->unknown_value()->real_named_object();
4982 return this->new_definition(real
, new_object
);
4983 go_assert(!new_object
->is_unknown());
4984 old_object
->unknown_value()->set_real_named_object(new_object
);
4985 if (!new_object
->is_type_declaration()
4986 && !new_object
->is_function_declaration())
4987 this->named_objects_
.push_back(new_object
);
4991 case Named_object::NAMED_OBJECT_CONST
:
4994 case Named_object::NAMED_OBJECT_TYPE
:
4995 if (new_object
->is_type_declaration())
4999 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
5000 if (new_object
->is_type_declaration())
5002 if (new_object
->is_type())
5004 old_object
->set_type_value(new_object
->type_value());
5005 new_object
->type_value()->set_named_object(old_object
);
5006 this->named_objects_
.push_back(old_object
);
5011 case Named_object::NAMED_OBJECT_VAR
:
5012 case Named_object::NAMED_OBJECT_RESULT_VAR
:
5013 // We have already given an error in the parser for cases where
5014 // one parameter or result variable redeclares another one.
5015 if ((new_object
->is_variable()
5016 && new_object
->var_value()->is_parameter())
5017 || new_object
->is_result_variable())
5021 case Named_object::NAMED_OBJECT_SINK
:
5024 case Named_object::NAMED_OBJECT_FUNC
:
5025 if (new_object
->is_function_declaration())
5027 if (!new_object
->func_declaration_value()->asm_name().empty())
5028 sorry("__asm__ for function definitions");
5029 Function_type
* old_type
= old_object
->func_value()->type();
5030 Function_type
* new_type
=
5031 new_object
->func_declaration_value()->type();
5032 if (old_type
->is_valid_redeclaration(new_type
, &reason
))
5037 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
5039 Function_type
* old_type
= old_object
->func_declaration_value()->type();
5040 if (new_object
->is_function_declaration())
5042 Function_type
* new_type
=
5043 new_object
->func_declaration_value()->type();
5044 if (old_type
->is_valid_redeclaration(new_type
, &reason
))
5047 if (new_object
->is_function())
5049 Function_type
* new_type
= new_object
->func_value()->type();
5050 if (old_type
->is_valid_redeclaration(new_type
, &reason
))
5052 if (!old_object
->func_declaration_value()->asm_name().empty())
5053 sorry("__asm__ for function definitions");
5054 old_object
->set_function_value(new_object
->func_value());
5055 this->named_objects_
.push_back(old_object
);
5062 case Named_object::NAMED_OBJECT_PACKAGE
:
5066 std::string n
= old_object
->message_name();
5068 error_at(new_object
->location(), "redefinition of %qs", n
.c_str());
5070 error_at(new_object
->location(), "redefinition of %qs: %s", n
.c_str(),
5073 inform(old_object
->location(), "previous definition of %qs was here",
5079 // Add a named type.
5082 Bindings::add_named_type(Named_type
* named_type
)
5084 return this->add_named_object(named_type
->named_object());
5090 Bindings::add_function(const std::string
& name
, const Package
* package
,
5093 return this->add_named_object(Named_object::make_function(name
, package
,
5097 // Add a function declaration.
5100 Bindings::add_function_declaration(const std::string
& name
,
5101 const Package
* package
,
5102 Function_type
* type
,
5105 Named_object
* no
= Named_object::make_function_declaration(name
, package
,
5107 return this->add_named_object(no
);
5110 // Define a type which was previously declared.
5113 Bindings::define_type(Named_object
* no
, Named_type
* type
)
5115 no
->set_type_value(type
);
5116 this->named_objects_
.push_back(no
);
5119 // Mark all local variables as used. This is used for some types of
5123 Bindings::mark_locals_used()
5125 for (std::vector
<Named_object
*>::iterator p
= this->named_objects_
.begin();
5126 p
!= this->named_objects_
.end();
5128 if ((*p
)->is_variable())
5129 (*p
)->var_value()->set_is_used();
5132 // Traverse bindings.
5135 Bindings::traverse(Traverse
* traverse
, bool is_global
)
5137 unsigned int traverse_mask
= traverse
->traverse_mask();
5139 // We don't use an iterator because we permit the traversal to add
5140 // new global objects.
5141 const unsigned int e_or_t
= (Traverse::traverse_expressions
5142 | Traverse::traverse_types
);
5143 const unsigned int e_or_t_or_s
= (e_or_t
5144 | Traverse::traverse_statements
);
5145 for (size_t i
= 0; i
< this->named_objects_
.size(); ++i
)
5147 Named_object
* p
= this->named_objects_
[i
];
5148 int t
= TRAVERSE_CONTINUE
;
5149 switch (p
->classification())
5151 case Named_object::NAMED_OBJECT_CONST
:
5152 if ((traverse_mask
& Traverse::traverse_constants
) != 0)
5153 t
= traverse
->constant(p
, is_global
);
5154 if (t
== TRAVERSE_CONTINUE
5155 && (traverse_mask
& e_or_t
) != 0)
5157 Type
* tc
= p
->const_value()->type();
5159 && Type::traverse(tc
, traverse
) == TRAVERSE_EXIT
)
5160 return TRAVERSE_EXIT
;
5161 t
= p
->const_value()->traverse_expression(traverse
);
5165 case Named_object::NAMED_OBJECT_VAR
:
5166 case Named_object::NAMED_OBJECT_RESULT_VAR
:
5167 if ((traverse_mask
& Traverse::traverse_variables
) != 0)
5168 t
= traverse
->variable(p
);
5169 if (t
== TRAVERSE_CONTINUE
5170 && (traverse_mask
& e_or_t
) != 0)
5172 if (p
->is_result_variable()
5173 || p
->var_value()->has_type())
5175 Type
* tv
= (p
->is_variable()
5176 ? p
->var_value()->type()
5177 : p
->result_var_value()->type());
5179 && Type::traverse(tv
, traverse
) == TRAVERSE_EXIT
)
5180 return TRAVERSE_EXIT
;
5183 if (t
== TRAVERSE_CONTINUE
5184 && (traverse_mask
& e_or_t_or_s
) != 0
5185 && p
->is_variable())
5186 t
= p
->var_value()->traverse_expression(traverse
, traverse_mask
);
5189 case Named_object::NAMED_OBJECT_FUNC
:
5190 if ((traverse_mask
& Traverse::traverse_functions
) != 0)
5191 t
= traverse
->function(p
);
5193 if (t
== TRAVERSE_CONTINUE
5195 & (Traverse::traverse_variables
5196 | Traverse::traverse_constants
5197 | Traverse::traverse_functions
5198 | Traverse::traverse_blocks
5199 | Traverse::traverse_statements
5200 | Traverse::traverse_expressions
5201 | Traverse::traverse_types
)) != 0)
5202 t
= p
->func_value()->traverse(traverse
);
5205 case Named_object::NAMED_OBJECT_PACKAGE
:
5206 // These are traversed in Gogo::traverse.
5207 go_assert(is_global
);
5210 case Named_object::NAMED_OBJECT_TYPE
:
5211 if ((traverse_mask
& e_or_t
) != 0)
5212 t
= Type::traverse(p
->type_value(), traverse
);
5215 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
5216 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
5217 case Named_object::NAMED_OBJECT_UNKNOWN
:
5218 case Named_object::NAMED_OBJECT_ERRONEOUS
:
5221 case Named_object::NAMED_OBJECT_SINK
:
5226 if (t
== TRAVERSE_EXIT
)
5227 return TRAVERSE_EXIT
;
5230 // If we need to traverse types, check the function declarations,
5231 // which have types. We don't need to check the type declarations,
5232 // as those are just names.
5233 if ((traverse_mask
& e_or_t
) != 0)
5235 for (Bindings::const_declarations_iterator p
=
5236 this->begin_declarations();
5237 p
!= this->end_declarations();
5240 if (p
->second
->is_function_declaration())
5242 if (Type::traverse(p
->second
->func_declaration_value()->type(),
5245 return TRAVERSE_EXIT
;
5250 return TRAVERSE_CONTINUE
;
5255 // Clear any references to this label.
5260 for (std::vector
<Bindings_snapshot
*>::iterator p
= this->refs_
.begin();
5261 p
!= this->refs_
.end();
5264 this->refs_
.clear();
5267 // Get the backend representation for a label.
5270 Label::get_backend_label(Translate_context
* context
)
5272 if (this->blabel_
== NULL
)
5274 Function
* function
= context
->function()->func_value();
5275 tree fndecl
= function
->get_decl();
5276 Bfunction
* bfunction
= tree_to_function(fndecl
);
5277 this->blabel_
= context
->backend()->label(bfunction
, this->name_
,
5280 return this->blabel_
;
5283 // Return an expression for the address of this label.
5286 Label::get_addr(Translate_context
* context
, Location location
)
5288 Blabel
* label
= this->get_backend_label(context
);
5289 return context
->backend()->label_address(label
, location
);
5292 // Class Unnamed_label.
5294 // Get the backend representation for an unnamed label.
5297 Unnamed_label::get_blabel(Translate_context
* context
)
5299 if (this->blabel_
== NULL
)
5301 Function
* function
= context
->function()->func_value();
5302 tree fndecl
= function
->get_decl();
5303 Bfunction
* bfunction
= tree_to_function(fndecl
);
5304 this->blabel_
= context
->backend()->label(bfunction
, "",
5307 return this->blabel_
;
5310 // Return a statement which defines this unnamed label.
5313 Unnamed_label::get_definition(Translate_context
* context
)
5315 Blabel
* blabel
= this->get_blabel(context
);
5316 return context
->backend()->label_definition_statement(blabel
);
5319 // Return a goto statement to this unnamed label.
5322 Unnamed_label::get_goto(Translate_context
* context
, Location location
)
5324 Blabel
* blabel
= this->get_blabel(context
);
5325 return context
->backend()->goto_statement(blabel
, location
);
5330 Package::Package(const std::string
& pkgpath
, Location location
)
5331 : pkgpath_(pkgpath
), pkgpath_symbol_(Gogo::pkgpath_for_symbol(pkgpath
)),
5332 package_name_(), bindings_(new Bindings(NULL
)), priority_(0),
5333 location_(location
), used_(false), is_imported_(false),
5334 uses_sink_alias_(false)
5336 go_assert(!pkgpath
.empty());
5340 // Set the package name.
5343 Package::set_package_name(const std::string
& package_name
, Location location
)
5345 go_assert(!package_name
.empty());
5346 if (this->package_name_
.empty())
5347 this->package_name_
= package_name
;
5348 else if (this->package_name_
!= package_name
)
5350 "saw two different packages with the same package path %s: %s, %s",
5351 this->pkgpath_
.c_str(), this->package_name_
.c_str(),
5352 package_name
.c_str());
5355 // Set the priority. We may see multiple priorities for an imported
5356 // package; we want to use the largest one.
5359 Package::set_priority(int priority
)
5361 if (priority
> this->priority_
)
5362 this->priority_
= priority
;
5365 // Determine types of constants. Everything else in a package
5366 // (variables, function declarations) should already have a fixed
5367 // type. Constants may have abstract types.
5370 Package::determine_types()
5372 Bindings
* bindings
= this->bindings_
;
5373 for (Bindings::const_definitions_iterator p
= bindings
->begin_definitions();
5374 p
!= bindings
->end_definitions();
5377 if ((*p
)->is_const())
5378 (*p
)->const_value()->determine_type();
5386 Traverse::~Traverse()
5388 if (this->types_seen_
!= NULL
)
5389 delete this->types_seen_
;
5390 if (this->expressions_seen_
!= NULL
)
5391 delete this->expressions_seen_
;
5394 // Record that we are looking at a type, and return true if we have
5398 Traverse::remember_type(const Type
* type
)
5400 if (type
->is_error_type())
5402 go_assert((this->traverse_mask() & traverse_types
) != 0
5403 || (this->traverse_mask() & traverse_expressions
) != 0);
5404 // We mostly only have to remember named types. But it turns out
5405 // that an interface type can refer to itself without using a name
5406 // by relying on interface inheritance, as in
5407 // type I interface { F() interface{I} }
5408 if (type
->classification() != Type::TYPE_NAMED
5409 && type
->classification() != Type::TYPE_INTERFACE
)
5411 if (this->types_seen_
== NULL
)
5412 this->types_seen_
= new Types_seen();
5413 std::pair
<Types_seen::iterator
, bool> ins
= this->types_seen_
->insert(type
);
5417 // Record that we are looking at an expression, and return true if we
5418 // have already seen it.
5421 Traverse::remember_expression(const Expression
* expression
)
5423 go_assert((this->traverse_mask() & traverse_types
) != 0
5424 || (this->traverse_mask() & traverse_expressions
) != 0);
5425 if (this->expressions_seen_
== NULL
)
5426 this->expressions_seen_
= new Expressions_seen();
5427 std::pair
<Expressions_seen::iterator
, bool> ins
=
5428 this->expressions_seen_
->insert(expression
);
5432 // The default versions of these functions should never be called: the
5433 // traversal mask indicates which functions may be called.
5436 Traverse::variable(Named_object
*)
5442 Traverse::constant(Named_object
*, bool)
5448 Traverse::function(Named_object
*)
5454 Traverse::block(Block
*)
5460 Traverse::statement(Block
*, size_t*, Statement
*)
5466 Traverse::expression(Expression
**)
5472 Traverse::type(Type
*)
5477 // Class Statement_inserter.
5480 Statement_inserter::insert(Statement
* s
)
5482 if (this->block_
!= NULL
)
5484 go_assert(this->pindex_
!= NULL
);
5485 this->block_
->insert_statement_before(*this->pindex_
, s
);
5488 else if (this->var_
!= NULL
)
5489 this->var_
->add_preinit_statement(this->gogo_
, s
);
5491 go_assert(saw_errors());