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.
13 #include "statements.h"
14 #include "expressions.h"
22 Gogo::Gogo(int int_type_size
, int float_type_size
, int pointer_size
)
25 globals_(new Bindings(NULL
)),
27 imported_unsafe_(false),
29 map_descriptors_(NULL
),
30 type_descriptor_decls_(NULL
),
38 const source_location loc
= BUILTINS_LOCATION
;
40 Named_type
* uint8_type
= Type::make_integer_type("uint8", true, 8,
41 RUNTIME_TYPE_KIND_UINT8
);
42 this->add_named_type(uint8_type
);
43 this->add_named_type(Type::make_integer_type("uint16", true, 16,
44 RUNTIME_TYPE_KIND_UINT16
));
45 this->add_named_type(Type::make_integer_type("uint32", true, 32,
46 RUNTIME_TYPE_KIND_UINT32
));
47 this->add_named_type(Type::make_integer_type("uint64", true, 64,
48 RUNTIME_TYPE_KIND_UINT64
));
50 this->add_named_type(Type::make_integer_type("int8", false, 8,
51 RUNTIME_TYPE_KIND_INT8
));
52 this->add_named_type(Type::make_integer_type("int16", false, 16,
53 RUNTIME_TYPE_KIND_INT16
));
54 this->add_named_type(Type::make_integer_type("int32", false, 32,
55 RUNTIME_TYPE_KIND_INT32
));
56 this->add_named_type(Type::make_integer_type("int64", false, 64,
57 RUNTIME_TYPE_KIND_INT64
));
59 this->add_named_type(Type::make_float_type("float32", 32,
60 RUNTIME_TYPE_KIND_FLOAT32
));
61 this->add_named_type(Type::make_float_type("float64", 64,
62 RUNTIME_TYPE_KIND_FLOAT64
));
64 this->add_named_type(Type::make_complex_type("complex64", 64,
65 RUNTIME_TYPE_KIND_COMPLEX64
));
66 this->add_named_type(Type::make_complex_type("complex128", 128,
67 RUNTIME_TYPE_KIND_COMPLEX128
));
69 if (int_type_size
< 32)
71 this->add_named_type(Type::make_integer_type("uint", true,
73 RUNTIME_TYPE_KIND_UINT
));
74 Named_type
* int_type
= Type::make_integer_type("int", false, int_type_size
,
75 RUNTIME_TYPE_KIND_INT
);
76 this->add_named_type(int_type
);
78 // "byte" is an alias for "uint8". Construct a Named_object which
79 // points to UINT8_TYPE. Note that this breaks the normal pairing
80 // in which a Named_object points to a Named_type which points back
81 // to the same Named_object.
82 Named_object
* byte_type
= this->declare_type("byte", loc
);
83 byte_type
->set_type_value(uint8_type
);
85 this->add_named_type(Type::make_integer_type("uintptr", true,
87 RUNTIME_TYPE_KIND_UINTPTR
));
89 this->add_named_type(Type::make_float_type("float", float_type_size
,
90 RUNTIME_TYPE_KIND_FLOAT
));
92 this->add_named_type(Type::make_complex_type("complex", float_type_size
* 2,
93 RUNTIME_TYPE_KIND_COMPLEX
));
95 this->add_named_type(Type::make_named_bool_type());
97 this->add_named_type(Type::make_named_string_type());
99 this->globals_
->add_constant(Typed_identifier("true",
100 Type::make_boolean_type(),
103 Expression::make_boolean(true, loc
),
105 this->globals_
->add_constant(Typed_identifier("false",
106 Type::make_boolean_type(),
109 Expression::make_boolean(false, loc
),
112 this->globals_
->add_constant(Typed_identifier("nil", Type::make_nil_type(),
115 Expression::make_nil(loc
),
118 Type
* abstract_int_type
= Type::make_abstract_integer_type();
119 this->globals_
->add_constant(Typed_identifier("iota", abstract_int_type
,
122 Expression::make_iota(),
125 Function_type
* new_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
126 new_type
->set_is_varargs();
127 new_type
->set_is_builtin();
128 this->globals_
->add_function_declaration("new", NULL
, new_type
, loc
);
130 Function_type
* make_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
131 make_type
->set_is_varargs();
132 make_type
->set_is_builtin();
133 this->globals_
->add_function_declaration("make", NULL
, make_type
, loc
);
135 Typed_identifier_list
* len_result
= new Typed_identifier_list();
136 len_result
->push_back(Typed_identifier("", int_type
, loc
));
137 Function_type
* len_type
= Type::make_function_type(NULL
, NULL
, len_result
,
139 len_type
->set_is_builtin();
140 this->globals_
->add_function_declaration("len", NULL
, len_type
, loc
);
142 Typed_identifier_list
* cap_result
= new Typed_identifier_list();
143 cap_result
->push_back(Typed_identifier("", int_type
, loc
));
144 Function_type
* cap_type
= Type::make_function_type(NULL
, NULL
, len_result
,
146 cap_type
->set_is_builtin();
147 this->globals_
->add_function_declaration("cap", NULL
, cap_type
, loc
);
149 Function_type
* print_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
150 print_type
->set_is_varargs();
151 print_type
->set_is_builtin();
152 this->globals_
->add_function_declaration("print", NULL
, print_type
, loc
);
154 print_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
155 print_type
->set_is_varargs();
156 print_type
->set_is_builtin();
157 this->globals_
->add_function_declaration("println", NULL
, print_type
, loc
);
159 Type
*empty
= Type::make_interface_type(NULL
, loc
);
160 Typed_identifier_list
* panic_parms
= new Typed_identifier_list();
161 panic_parms
->push_back(Typed_identifier("e", empty
, loc
));
162 Function_type
*panic_type
= Type::make_function_type(NULL
, panic_parms
,
164 panic_type
->set_is_builtin();
165 this->globals_
->add_function_declaration("panic", NULL
, panic_type
, loc
);
167 Typed_identifier_list
* recover_result
= new Typed_identifier_list();
168 recover_result
->push_back(Typed_identifier("", empty
, loc
));
169 Function_type
* recover_type
= Type::make_function_type(NULL
, NULL
,
172 recover_type
->set_is_builtin();
173 this->globals_
->add_function_declaration("recover", NULL
, recover_type
, loc
);
175 Function_type
* close_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
176 close_type
->set_is_varargs();
177 close_type
->set_is_builtin();
178 this->globals_
->add_function_declaration("close", NULL
, close_type
, loc
);
180 Typed_identifier_list
* closed_result
= new Typed_identifier_list();
181 closed_result
->push_back(Typed_identifier("", Type::lookup_bool_type(),
183 Function_type
* closed_type
= Type::make_function_type(NULL
, NULL
,
185 closed_type
->set_is_varargs();
186 closed_type
->set_is_builtin();
187 this->globals_
->add_function_declaration("closed", NULL
, closed_type
, loc
);
189 Typed_identifier_list
* copy_result
= new Typed_identifier_list();
190 copy_result
->push_back(Typed_identifier("", int_type
, loc
));
191 Function_type
* copy_type
= Type::make_function_type(NULL
, NULL
,
193 copy_type
->set_is_varargs();
194 copy_type
->set_is_builtin();
195 this->globals_
->add_function_declaration("copy", NULL
, copy_type
, loc
);
197 Function_type
* append_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
198 append_type
->set_is_varargs();
199 append_type
->set_is_builtin();
200 this->globals_
->add_function_declaration("append", NULL
, append_type
, loc
);
202 Function_type
* cmplx_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
203 cmplx_type
->set_is_varargs();
204 cmplx_type
->set_is_builtin();
205 this->globals_
->add_function_declaration("cmplx", NULL
, cmplx_type
, loc
);
207 Function_type
* real_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
208 real_type
->set_is_varargs();
209 real_type
->set_is_builtin();
210 this->globals_
->add_function_declaration("real", NULL
, real_type
, loc
);
212 Function_type
* imag_type
= Type::make_function_type(NULL
, NULL
, NULL
, loc
);
213 imag_type
->set_is_varargs();
214 imag_type
->set_is_builtin();
215 this->globals_
->add_function_declaration("imag", NULL
, cmplx_type
, loc
);
217 this->define_builtin_function_trees();
219 // Declare "init", to ensure that it is not defined with parameters
221 this->declare_function("init",
222 Type::make_function_type(NULL
, NULL
, NULL
, loc
),
226 // Munge name for use in an error message.
229 Gogo::message_name(const std::string
& name
)
231 return go_localize_identifier(Gogo::unpack_hidden_name(name
).c_str());
234 // Get the package name.
237 Gogo::package_name() const
239 gcc_assert(this->package_
!= NULL
);
240 return this->package_
->name();
243 // Set the package name.
246 Gogo::set_package_name(const std::string
& package_name
,
247 source_location location
)
249 if (this->package_
!= NULL
&& this->package_
->name() != package_name
)
251 error_at(location
, "expected package %<%s%>",
252 Gogo::message_name(this->package_
->name()).c_str());
256 // If the user did not specify a unique prefix, we always use "go".
257 // This in effect requires that the package name be unique.
258 if (this->unique_prefix_
.empty())
259 this->unique_prefix_
= "go";
261 this->package_
= this->register_package(package_name
, this->unique_prefix_
,
264 // We used to permit people to qualify symbols with the current
265 // package name (e.g., P.x), but we no longer do.
266 // this->globals_->add_package(package_name, this->package_);
268 if (package_name
== "main")
270 // Declare "main" as a function which takes no parameters and
272 this->declare_function("main",
273 Type::make_function_type(NULL
, NULL
, NULL
,
282 Gogo::import_package(const std::string
& filename
,
283 const std::string
& local_name
,
284 bool is_local_name_exported
,
285 source_location location
)
287 if (filename
== "unsafe")
289 this->import_unsafe(local_name
, is_local_name_exported
, location
);
293 Imports::const_iterator p
= this->imports_
.find(filename
);
294 if (p
!= this->imports_
.end())
296 Package
* package
= p
->second
;
297 package
->set_location(location
);
298 package
->set_is_imported();
299 std::string ln
= local_name
;
300 bool is_ln_exported
= is_local_name_exported
;
303 ln
= package
->name();
304 is_ln_exported
= Lex::is_exported_name(ln
);
308 ln
= this->pack_hidden_name(ln
, is_ln_exported
);
309 this->package_
->bindings()->add_package(ln
, package
);
313 Bindings
* bindings
= package
->bindings();
314 for (Bindings::const_declarations_iterator p
=
315 bindings
->begin_declarations();
316 p
!= bindings
->end_declarations();
318 this->add_named_object(p
->second
);
323 Import::Stream
* stream
= Import::open_package(filename
, location
);
326 error_at(location
, "import file %qs not found", filename
.c_str());
330 Import
imp(stream
, location
);
331 imp
.register_builtin_types(this);
332 Package
* package
= imp
.import(this, local_name
, is_local_name_exported
);
333 this->imports_
.insert(std::make_pair(filename
, package
));
334 package
->set_is_imported();
339 // Add an import control function for an imported package to the list.
342 Gogo::add_import_init_fn(const std::string
& package_name
,
343 const std::string
& init_name
, int prio
)
345 for (std::set
<Import_init
>::const_iterator p
=
346 this->imported_init_fns_
.begin();
347 p
!= this->imported_init_fns_
.end();
350 if (p
->init_name() == init_name
351 && (p
->package_name() != package_name
|| p
->priority() != prio
))
353 error("duplicate package initialization name %qs",
354 Gogo::message_name(init_name
).c_str());
355 inform(UNKNOWN_LOCATION
, "used by package %qs at priority %d",
356 Gogo::message_name(p
->package_name()).c_str(),
358 inform(UNKNOWN_LOCATION
, " and by package %qs at priority %d",
359 Gogo::message_name(package_name
).c_str(), prio
);
364 this->imported_init_fns_
.insert(Import_init(package_name
, init_name
,
368 // Return whether we are at the global binding level.
371 Gogo::in_global_scope() const
373 return this->functions_
.empty();
376 // Return the current binding contour.
379 Gogo::current_bindings()
381 if (!this->functions_
.empty())
382 return this->functions_
.back().blocks
.back()->bindings();
383 else if (this->package_
!= NULL
)
384 return this->package_
->bindings();
386 return this->globals_
;
390 Gogo::current_bindings() const
392 if (!this->functions_
.empty())
393 return this->functions_
.back().blocks
.back()->bindings();
394 else if (this->package_
!= NULL
)
395 return this->package_
->bindings();
397 return this->globals_
;
400 // Return the current block.
403 Gogo::current_block()
405 if (this->functions_
.empty())
408 return this->functions_
.back().blocks
.back();
411 // Look up a name in the current binding contour. If PFUNCTION is not
412 // NULL, set it to the function in which the name is defined, or NULL
413 // if the name is defined in global scope.
416 Gogo::lookup(const std::string
& name
, Named_object
** pfunction
) const
418 if (pfunction
!= NULL
)
421 if (Gogo::is_sink_name(name
))
422 return Named_object::make_sink();
424 for (Open_functions::const_reverse_iterator p
= this->functions_
.rbegin();
425 p
!= this->functions_
.rend();
428 Named_object
* ret
= p
->blocks
.back()->bindings()->lookup(name
);
431 if (pfunction
!= NULL
)
432 *pfunction
= p
->function
;
437 if (this->package_
!= NULL
)
439 Named_object
* ret
= this->package_
->bindings()->lookup(name
);
442 if (ret
->package() != NULL
)
443 ret
->package()->set_used();
448 // We do not look in the global namespace. If we did, the global
449 // namespace would effectively hide names which were defined in
450 // package scope which we have not yet seen. Instead,
451 // define_global_names is called after parsing is over to connect
452 // undefined names at package scope with names defined at global
458 // Look up a name in the current block, without searching enclosing
462 Gogo::lookup_in_block(const std::string
& name
) const
464 gcc_assert(!this->functions_
.empty());
465 gcc_assert(!this->functions_
.back().blocks
.empty());
466 return this->functions_
.back().blocks
.back()->bindings()->lookup_local(name
);
469 // Look up a name in the global namespace.
472 Gogo::lookup_global(const char* name
) const
474 return this->globals_
->lookup(name
);
477 // Add an imported package.
480 Gogo::add_imported_package(const std::string
& real_name
,
481 const std::string
& alias_arg
,
482 bool is_alias_exported
,
483 const std::string
& unique_prefix
,
484 source_location location
,
485 bool* padd_to_globals
)
487 // FIXME: Now that we compile packages as a whole, should we permit
488 // importing the current package?
489 if (this->package_name() == real_name
490 && this->unique_prefix() == unique_prefix
)
492 *padd_to_globals
= false;
493 if (!alias_arg
.empty() && alias_arg
!= ".")
495 std::string alias
= this->pack_hidden_name(alias_arg
,
497 this->package_
->bindings()->add_package(alias
, this->package_
);
499 return this->package_
;
501 else if (alias_arg
== ".")
503 *padd_to_globals
= true;
504 return this->register_package(real_name
, unique_prefix
, location
);
506 else if (alias_arg
== "_")
508 Package
* ret
= this->register_package(real_name
, unique_prefix
, location
);
509 ret
->set_uses_sink_alias();
514 *padd_to_globals
= false;
515 std::string alias
= alias_arg
;
519 is_alias_exported
= Lex::is_exported_name(alias
);
521 alias
= this->pack_hidden_name(alias
, is_alias_exported
);
522 Named_object
* no
= this->add_package(real_name
, alias
, unique_prefix
,
524 if (!no
->is_package())
526 return no
->package_value();
533 Gogo::add_package(const std::string
& real_name
, const std::string
& alias
,
534 const std::string
& unique_prefix
, source_location location
)
536 gcc_assert(this->in_global_scope());
538 // Register the package. Note that we might have already seen it in
539 // an earlier import.
540 Package
* package
= this->register_package(real_name
, unique_prefix
, location
);
542 return this->package_
->bindings()->add_package(alias
, package
);
545 // Register a package. This package may or may not be imported. This
546 // returns the Package structure for the package, creating if it
550 Gogo::register_package(const std::string
& package_name
,
551 const std::string
& unique_prefix
,
552 source_location location
)
554 gcc_assert(!unique_prefix
.empty() && !package_name
.empty());
555 std::string name
= unique_prefix
+ '.' + package_name
;
556 Package
* package
= NULL
;
557 std::pair
<Packages::iterator
, bool> ins
=
558 this->packages_
.insert(std::make_pair(name
, package
));
561 // We have seen this package name before.
562 package
= ins
.first
->second
;
563 gcc_assert(package
!= NULL
);
564 gcc_assert(package
->name() == package_name
565 && package
->unique_prefix() == unique_prefix
);
566 if (package
->location() == UNKNOWN_LOCATION
)
567 package
->set_location(location
);
571 // First time we have seen this package name.
572 package
= new Package(package_name
, unique_prefix
, location
);
573 gcc_assert(ins
.first
->second
== NULL
);
574 ins
.first
->second
= package
;
580 // Start compiling a function.
583 Gogo::start_function(const std::string
& name
, Function_type
* type
,
584 bool add_method_to_type
, source_location location
)
586 bool at_top_level
= this->functions_
.empty();
588 Block
* block
= new Block(NULL
, location
);
590 Function
* enclosing
= (at_top_level
592 : this->functions_
.back().function
->func_value());
594 Function
* function
= new Function(type
, enclosing
, block
, location
);
596 if (type
->is_method())
598 const Typed_identifier
* receiver
= type
->receiver();
599 Variable
* this_param
= new Variable(receiver
->type(), NULL
, false,
600 true, true, location
);
601 std::string name
= receiver
->name();
604 // We need to give receivers a name since they wind up in
605 // DECL_ARGUMENTS. FIXME.
606 static unsigned int count
;
608 snprintf(buf
, sizeof buf
, "r.%u", count
);
612 block
->bindings()->add_variable(name
, NULL
, this_param
);
615 const Typed_identifier_list
* parameters
= type
->parameters();
616 bool is_varargs
= type
->is_varargs();
617 if (parameters
!= NULL
)
619 for (Typed_identifier_list::const_iterator p
= parameters
->begin();
620 p
!= parameters
->end();
623 Variable
* param
= new Variable(p
->type(), NULL
, false, true, false,
625 if (is_varargs
&& p
+ 1 == parameters
->end())
626 param
->set_is_varargs_parameter();
628 std::string name
= p
->name();
629 if (name
.empty() || Gogo::is_sink_name(name
))
631 // We need to give parameters a name since they wind up
632 // in DECL_ARGUMENTS. FIXME.
633 static unsigned int count
;
635 snprintf(buf
, sizeof buf
, "p.%u", count
);
639 block
->bindings()->add_variable(name
, NULL
, param
);
643 function
->create_named_result_variables(this);
645 const std::string
* pname
;
646 std::string nested_name
;
651 // Invent a name for a nested function.
652 static int nested_count
;
654 snprintf(buf
, sizeof buf
, ".$nested%d", nested_count
);
657 pname
= &nested_name
;
661 if (Gogo::is_sink_name(*pname
))
663 static int sink_count
;
665 snprintf(buf
, sizeof buf
, ".$sink%d", sink_count
);
667 ret
= Named_object::make_function(buf
, NULL
, function
);
669 else if (!type
->is_method())
671 ret
= this->package_
->bindings()->add_function(*pname
, NULL
, function
);
672 if (!ret
->is_function())
674 // Redefinition error.
675 ret
= Named_object::make_function(name
, NULL
, function
);
680 if (!add_method_to_type
)
681 ret
= Named_object::make_function(name
, NULL
, function
);
684 gcc_assert(at_top_level
);
685 Type
* rtype
= type
->receiver()->type();
687 // We want to look through the pointer created by the
688 // parser, without getting an error if the type is not yet
690 if (rtype
->classification() == Type::TYPE_POINTER
)
691 rtype
= rtype
->points_to();
693 if (rtype
->is_error_type())
694 ret
= Named_object::make_function(name
, NULL
, function
);
695 else if (rtype
->named_type() != NULL
)
697 ret
= rtype
->named_type()->add_method(name
, function
);
698 if (!ret
->is_function())
700 // Redefinition error.
701 ret
= Named_object::make_function(name
, NULL
, function
);
704 else if (rtype
->forward_declaration_type() != NULL
)
706 Named_object
* type_no
=
707 rtype
->forward_declaration_type()->named_object();
708 if (type_no
->is_unknown())
710 // If we are seeing methods it really must be a
711 // type. Declare it as such. An alternative would
712 // be to support lists of methods for unknown
713 // expressions. Either way the error messages if
714 // this is not a type are going to get confusing.
715 Named_object
* declared
=
716 this->declare_package_type(type_no
->name(),
717 type_no
->location());
719 == type_no
->unknown_value()->real_named_object());
721 ret
= rtype
->forward_declaration_type()->add_method(name
,
727 this->package_
->bindings()->add_method(ret
);
730 this->functions_
.resize(this->functions_
.size() + 1);
731 Open_function
& of(this->functions_
.back());
733 of
.blocks
.push_back(block
);
735 if (!type
->is_method() && Gogo::unpack_hidden_name(name
) == "init")
737 this->init_functions_
.push_back(ret
);
738 this->need_init_fn_
= true;
744 // Finish compiling a function.
747 Gogo::finish_function(source_location location
)
749 this->finish_block(location
);
750 gcc_assert(this->functions_
.back().blocks
.empty());
751 this->functions_
.pop_back();
754 // Return the current function.
757 Gogo::current_function() const
759 gcc_assert(!this->functions_
.empty());
760 return this->functions_
.back().function
;
763 // Start a new block.
766 Gogo::start_block(source_location location
)
768 gcc_assert(!this->functions_
.empty());
769 Block
* block
= new Block(this->current_block(), location
);
770 this->functions_
.back().blocks
.push_back(block
);
776 Gogo::finish_block(source_location location
)
778 gcc_assert(!this->functions_
.empty());
779 gcc_assert(!this->functions_
.back().blocks
.empty());
780 Block
* block
= this->functions_
.back().blocks
.back();
781 this->functions_
.back().blocks
.pop_back();
782 block
->set_end_location(location
);
786 // Add an unknown name.
789 Gogo::add_unknown_name(const std::string
& name
, source_location location
)
791 return this->package_
->bindings()->add_unknown_name(name
, location
);
794 // Declare a function.
797 Gogo::declare_function(const std::string
& name
, Function_type
* type
,
798 source_location location
)
800 if (!type
->is_method())
801 return this->current_bindings()->add_function_declaration(name
, NULL
, type
,
805 // We don't bother to add this to the list of global
807 Type
* rtype
= type
->receiver()->type();
809 // We want to look through the pointer created by the
810 // parser, without getting an error if the type is not yet
812 if (rtype
->classification() == Type::TYPE_POINTER
)
813 rtype
= rtype
->points_to();
815 if (rtype
->is_error_type())
817 else if (rtype
->named_type() != NULL
)
818 return rtype
->named_type()->add_method_declaration(name
, NULL
, type
,
820 else if (rtype
->forward_declaration_type() != NULL
)
822 Forward_declaration_type
* ftype
= rtype
->forward_declaration_type();
823 return ftype
->add_method_declaration(name
, type
, location
);
830 // Add a label definition.
833 Gogo::add_label_definition(const std::string
& label_name
,
834 source_location location
)
836 gcc_assert(!this->functions_
.empty());
837 Function
* func
= this->functions_
.back().function
->func_value();
838 Label
* label
= func
->add_label_definition(label_name
, location
);
839 this->add_statement(Statement::make_label_statement(label
, location
));
843 // Add a label reference.
846 Gogo::add_label_reference(const std::string
& label_name
)
848 gcc_assert(!this->functions_
.empty());
849 Function
* func
= this->functions_
.back().function
->func_value();
850 return func
->add_label_reference(label_name
);
856 Gogo::add_statement(Statement
* statement
)
858 gcc_assert(!this->functions_
.empty()
859 && !this->functions_
.back().blocks
.empty());
860 this->functions_
.back().blocks
.back()->add_statement(statement
);
866 Gogo::add_block(Block
* block
, source_location location
)
868 gcc_assert(!this->functions_
.empty()
869 && !this->functions_
.back().blocks
.empty());
870 Statement
* statement
= Statement::make_block_statement(block
, location
);
871 this->functions_
.back().blocks
.back()->add_statement(statement
);
877 Gogo::add_constant(const Typed_identifier
& tid
, Expression
* expr
,
880 return this->current_bindings()->add_constant(tid
, NULL
, expr
, iota_value
);
886 Gogo::add_type(const std::string
& name
, Type
* type
, source_location location
)
888 Named_object
* no
= this->current_bindings()->add_type(name
, NULL
, type
,
890 if (!this->in_global_scope() && no
->is_type())
891 no
->type_value()->set_in_function(this->functions_
.back().function
);
897 Gogo::add_named_type(Named_type
* type
)
899 gcc_assert(this->in_global_scope());
900 this->current_bindings()->add_named_type(type
);
906 Gogo::declare_type(const std::string
& name
, source_location location
)
908 Bindings
* bindings
= this->current_bindings();
909 Named_object
* no
= bindings
->add_type_declaration(name
, NULL
, location
);
910 if (!this->in_global_scope() && no
->is_type_declaration())
912 Named_object
* f
= this->functions_
.back().function
;
913 no
->type_declaration_value()->set_in_function(f
);
918 // Declare a type at the package level.
921 Gogo::declare_package_type(const std::string
& name
, source_location location
)
923 return this->package_
->bindings()->add_type_declaration(name
, NULL
, location
);
926 // Define a type which was already declared.
929 Gogo::define_type(Named_object
* no
, Named_type
* type
)
931 this->current_bindings()->define_type(no
, type
);
937 Gogo::add_variable(const std::string
& name
, Variable
* variable
)
939 Named_object
* no
= this->current_bindings()->add_variable(name
, NULL
,
942 // In a function the middle-end wants to see a DECL_EXPR node.
945 && !no
->var_value()->is_parameter()
946 && !this->functions_
.empty())
947 this->add_statement(Statement::make_variable_declaration(no
));
952 // Add a sink--a reference to the blank identifier _.
957 return Named_object::make_sink();
960 // Add a named object.
963 Gogo::add_named_object(Named_object
* no
)
965 this->current_bindings()->add_named_object(no
);
968 // Record that we've seen an interface type.
971 Gogo::record_interface_type(Interface_type
* itype
)
973 this->interface_types_
.push_back(itype
);
976 // Return a name for a thunk object.
981 static int thunk_count
;
983 snprintf(thunk_name
, sizeof thunk_name
, "$thunk%d", thunk_count
);
988 // Return whether a function is a thunk.
991 Gogo::is_thunk(const Named_object
* no
)
993 return no
->name().compare(0, 6, "$thunk") == 0;
996 // Define the global names. We do this only after parsing all the
997 // input files, because the program might define the global names
1001 Gogo::define_global_names()
1003 for (Bindings::const_declarations_iterator p
=
1004 this->globals_
->begin_declarations();
1005 p
!= this->globals_
->end_declarations();
1008 Named_object
* global_no
= p
->second
;
1009 std::string
name(Gogo::pack_hidden_name(global_no
->name(), false));
1010 Named_object
* no
= this->package_
->bindings()->lookup(name
);
1014 if (no
->is_type_declaration())
1016 if (global_no
->is_type())
1018 if (no
->type_declaration_value()->has_methods())
1019 error_at(no
->location(),
1020 "may not define methods for global type");
1021 no
->set_type_value(global_no
->type_value());
1025 error_at(no
->location(), "expected type");
1026 Type
* errtype
= Type::make_error_type();
1027 Named_object
* err
= Named_object::make_type("error", NULL
,
1030 no
->set_type_value(err
->type_value());
1033 else if (no
->is_unknown())
1034 no
->unknown_value()->set_real_named_object(global_no
);
1038 // Clear out names in file scope.
1041 Gogo::clear_file_scope()
1043 this->package_
->bindings()->clear_file_scope();
1045 // Warn about packages which were imported but not used.
1046 for (Packages::iterator p
= this->packages_
.begin();
1047 p
!= this->packages_
.end();
1050 Package
* package
= p
->second
;
1051 if (package
!= this->package_
1052 && package
->is_imported()
1054 && !package
->uses_sink_alias()
1056 error_at(package
->location(), "imported and not used: %s",
1057 Gogo::message_name(package
->name()).c_str());
1058 package
->clear_is_imported();
1059 package
->clear_uses_sink_alias();
1060 package
->clear_used();
1064 // Traverse the tree.
1067 Gogo::traverse(Traverse
* traverse
)
1069 // Traverse the current package first for consistency. The other
1070 // packages will only contain imported types, constants, and
1072 if (this->package_
->bindings()->traverse(traverse
, true) == TRAVERSE_EXIT
)
1074 for (Packages::const_iterator p
= this->packages_
.begin();
1075 p
!= this->packages_
.end();
1078 if (p
->second
!= this->package_
)
1080 if (p
->second
->bindings()->traverse(traverse
, true) == TRAVERSE_EXIT
)
1086 // Traversal class used to verify types.
1088 class Verify_types
: public Traverse
1092 : Traverse(traverse_types
)
1099 // Verify that a type is correct.
1102 Verify_types::type(Type
* t
)
1104 // Don't verify types defined in other packages.
1105 Named_type
* nt
= t
->named_type();
1106 if (nt
!= NULL
&& nt
->named_object()->package() != NULL
)
1107 return TRAVERSE_SKIP_COMPONENTS
;
1110 return TRAVERSE_SKIP_COMPONENTS
;
1111 return TRAVERSE_CONTINUE
;
1114 // Verify that all types are correct.
1117 Gogo::verify_types()
1119 Verify_types traverse
;
1120 this->traverse(&traverse
);
1123 // Traversal class used to lower parse tree.
1125 class Lower_parse_tree
: public Traverse
1128 Lower_parse_tree(Gogo
* gogo
, Named_object
* function
)
1129 : Traverse(traverse_constants
1130 | traverse_functions
1131 | traverse_statements
1132 | traverse_expressions
),
1133 gogo_(gogo
), function_(function
), iota_value_(-1)
1137 constant(Named_object
*, bool);
1140 function(Named_object
*);
1143 statement(Block
*, size_t* pindex
, Statement
*);
1146 expression(Expression
**);
1151 // The function we are traversing.
1152 Named_object
* function_
;
1153 // Value to use for the predeclared constant iota.
1157 // Lower constants. We handle constants specially so that we can set
1158 // the right value for the predeclared constant iota. This works in
1159 // conjunction with the way we lower Const_expression objects.
1162 Lower_parse_tree::constant(Named_object
* no
, bool)
1164 Named_constant
* nc
= no
->const_value();
1166 // Don't get into trouble if the constant's initializer expression
1167 // refers to the constant itself.
1169 return TRAVERSE_CONTINUE
;
1172 gcc_assert(this->iota_value_
== -1);
1173 this->iota_value_
= nc
->iota_value();
1174 nc
->traverse_expression(this);
1175 this->iota_value_
= -1;
1177 nc
->clear_lowering();
1179 // We will traverse the expression a second time, but that will be
1182 return TRAVERSE_CONTINUE
;
1185 // Lower function closure types. Record the function while lowering
1186 // it, so that we can pass it down when lowering an expression.
1189 Lower_parse_tree::function(Named_object
* no
)
1191 no
->func_value()->set_closure_type();
1193 gcc_assert(this->function_
== NULL
);
1194 this->function_
= no
;
1195 int t
= no
->func_value()->traverse(this);
1196 this->function_
= NULL
;
1198 if (t
== TRAVERSE_EXIT
)
1200 return TRAVERSE_SKIP_COMPONENTS
;
1203 // Lower statement parse trees.
1206 Lower_parse_tree::statement(Block
* block
, size_t* pindex
, Statement
* sorig
)
1208 // Lower the expressions first.
1209 int t
= sorig
->traverse_contents(this);
1210 if (t
== TRAVERSE_EXIT
)
1213 // Keep lowering until nothing changes.
1214 Statement
* s
= sorig
;
1217 Statement
* snew
= s
->lower(this->gogo_
, block
);
1221 t
= s
->traverse_contents(this);
1222 if (t
== TRAVERSE_EXIT
)
1227 block
->replace_statement(*pindex
, s
);
1229 return TRAVERSE_SKIP_COMPONENTS
;
1232 // Lower expression parse trees.
1235 Lower_parse_tree::expression(Expression
** pexpr
)
1237 // We have to lower all subexpressions first, so that we can get
1238 // their type if necessary. This is awkward, because we don't have
1239 // a postorder traversal pass.
1240 if ((*pexpr
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
1241 return TRAVERSE_EXIT
;
1242 // Keep lowering until nothing changes.
1245 Expression
* e
= *pexpr
;
1246 Expression
* enew
= e
->lower(this->gogo_
, this->function_
,
1252 return TRAVERSE_SKIP_COMPONENTS
;
1255 // Lower the parse tree. This is called after the parse is complete,
1256 // when all names should be resolved.
1259 Gogo::lower_parse_tree()
1261 Lower_parse_tree
lower_parse_tree(this, NULL
);
1262 this->traverse(&lower_parse_tree
);
1265 // Lower an expression.
1268 Gogo::lower_expression(Named_object
* function
, Expression
** pexpr
)
1270 Lower_parse_tree
lower_parse_tree(this, function
);
1271 lower_parse_tree
.expression(pexpr
);
1274 // Lower a constant. This is called when lowering a reference to a
1275 // constant. We have to make sure that the constant has already been
1279 Gogo::lower_constant(Named_object
* no
)
1281 gcc_assert(no
->is_const());
1282 Lower_parse_tree
lower(this, NULL
);
1283 lower
.constant(no
, false);
1286 // Look for interface types to finalize methods of inherited
1289 class Finalize_methods
: public Traverse
1292 Finalize_methods(Gogo
* gogo
)
1293 : Traverse(traverse_types
),
1304 // Finalize the methods of an interface type.
1307 Finalize_methods::type(Type
* t
)
1309 // Check the classification so that we don't finalize the methods
1310 // twice for a named interface type.
1311 switch (t
->classification())
1313 case Type::TYPE_INTERFACE
:
1314 t
->interface_type()->finalize_methods();
1317 case Type::TYPE_NAMED
:
1319 // We have to finalize the methods of the real type first.
1320 // But if the real type is a struct type, then we only want to
1321 // finalize the methods of the field types, not of the struct
1322 // type itself. We don't want to add methods to the struct,
1323 // since it has a name.
1324 Type
* rt
= t
->named_type()->real_type();
1325 if (rt
->classification() != Type::TYPE_STRUCT
)
1327 if (Type::traverse(rt
, this) == TRAVERSE_EXIT
)
1328 return TRAVERSE_EXIT
;
1332 if (rt
->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT
)
1333 return TRAVERSE_EXIT
;
1336 t
->named_type()->finalize_methods(this->gogo_
);
1338 return TRAVERSE_SKIP_COMPONENTS
;
1341 case Type::TYPE_STRUCT
:
1342 t
->struct_type()->finalize_methods(this->gogo_
);
1349 return TRAVERSE_CONTINUE
;
1352 // Finalize method lists and build stub methods for types.
1355 Gogo::finalize_methods()
1357 Finalize_methods
finalize(this);
1358 this->traverse(&finalize
);
1361 // Set types for unspecified variables and constants.
1364 Gogo::determine_types()
1366 Bindings
* bindings
= this->current_bindings();
1367 for (Bindings::const_definitions_iterator p
= bindings
->begin_definitions();
1368 p
!= bindings
->end_definitions();
1371 if ((*p
)->is_function())
1372 (*p
)->func_value()->determine_types();
1373 else if ((*p
)->is_variable())
1374 (*p
)->var_value()->determine_type();
1375 else if ((*p
)->is_const())
1376 (*p
)->const_value()->determine_type();
1378 // See if a variable requires us to build an initialization
1379 // function. We know that we will see all global variables
1381 if (!this->need_init_fn_
&& (*p
)->is_variable())
1383 Variable
* variable
= (*p
)->var_value();
1385 // If this is a global variable which requires runtime
1386 // initialization, we need an initialization function.
1387 if (!variable
->is_global())
1389 else if (variable
->has_pre_init())
1390 this->need_init_fn_
= true;
1391 else if (variable
->init() == NULL
)
1393 else if (variable
->type()->interface_type() != NULL
)
1394 this->need_init_fn_
= true;
1395 else if (variable
->init()->is_constant())
1397 else if (!variable
->init()->is_composite_literal())
1398 this->need_init_fn_
= true;
1399 else if (variable
->init()->is_nonconstant_composite_literal())
1400 this->need_init_fn_
= true;
1402 // If this is a global variable which holds a pointer value,
1403 // then we need an initialization function to register it as a
1405 if (variable
->is_global() && variable
->type()->has_pointer())
1406 this->need_init_fn_
= true;
1410 // Determine the types of constants in packages.
1411 for (Packages::const_iterator p
= this->packages_
.begin();
1412 p
!= this->packages_
.end();
1414 p
->second
->determine_types();
1417 // Traversal class used for type checking.
1419 class Check_types_traverse
: public Traverse
1422 Check_types_traverse(Gogo
* gogo
)
1423 : Traverse(traverse_variables
1424 | traverse_constants
1425 | traverse_statements
1426 | traverse_expressions
),
1431 variable(Named_object
*);
1434 constant(Named_object
*, bool);
1437 statement(Block
*, size_t* pindex
, Statement
*);
1440 expression(Expression
**);
1447 // Check that a variable initializer has the right type.
1450 Check_types_traverse::variable(Named_object
* named_object
)
1452 if (named_object
->is_variable())
1454 Variable
* var
= named_object
->var_value();
1455 Expression
* init
= var
->init();
1458 && !Type::are_assignable(var
->type(), init
->type(), &reason
))
1461 error_at(var
->location(), "incompatible type in initialization");
1463 error_at(var
->location(),
1464 "incompatible type in initialization (%s)",
1469 return TRAVERSE_CONTINUE
;
1472 // Check that a constant initializer has the right type.
1475 Check_types_traverse::constant(Named_object
* named_object
, bool)
1477 Named_constant
* constant
= named_object
->const_value();
1478 Type
* ctype
= constant
->type();
1479 if (ctype
->integer_type() == NULL
1480 && ctype
->float_type() == NULL
1481 && ctype
->complex_type() == NULL
1482 && !ctype
->is_boolean_type()
1483 && !ctype
->is_string_type())
1485 if (!ctype
->is_error_type())
1486 error_at(constant
->location(), "invalid constant type");
1487 constant
->set_error();
1489 else if (!constant
->expr()->is_constant())
1491 error_at(constant
->expr()->location(), "expression is not constant");
1492 constant
->set_error();
1494 else if (!Type::are_assignable(constant
->type(), constant
->expr()->type(),
1497 error_at(constant
->location(),
1498 "initialization expression has wrong type");
1499 constant
->set_error();
1501 return TRAVERSE_CONTINUE
;
1504 // Check that types are valid in a statement.
1507 Check_types_traverse::statement(Block
*, size_t*, Statement
* s
)
1509 s
->check_types(this->gogo_
);
1510 return TRAVERSE_CONTINUE
;
1513 // Check that types are valid in an expression.
1516 Check_types_traverse::expression(Expression
** expr
)
1518 (*expr
)->check_types(this->gogo_
);
1519 return TRAVERSE_CONTINUE
;
1522 // Check that types are valid.
1527 Check_types_traverse
traverse(this);
1528 this->traverse(&traverse
);
1531 // Check the types in a single block.
1534 Gogo::check_types_in_block(Block
* block
)
1536 Check_types_traverse
traverse(this);
1537 block
->traverse(&traverse
);
1540 // A traversal class used to find a single shortcut operator within an
1543 class Find_shortcut
: public Traverse
1547 : Traverse(traverse_blocks
1548 | traverse_statements
1549 | traverse_expressions
),
1553 // A pointer to the expression which was found, or NULL if none was
1557 { return this->found_
; }
1562 { return TRAVERSE_SKIP_COMPONENTS
; }
1565 statement(Block
*, size_t*, Statement
*)
1566 { return TRAVERSE_SKIP_COMPONENTS
; }
1569 expression(Expression
**);
1572 Expression
** found_
;
1575 // Find a shortcut expression.
1578 Find_shortcut::expression(Expression
** pexpr
)
1580 Expression
* expr
= *pexpr
;
1581 Binary_expression
* be
= expr
->binary_expression();
1583 return TRAVERSE_CONTINUE
;
1584 Operator op
= be
->op();
1585 if (op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
)
1586 return TRAVERSE_CONTINUE
;
1587 gcc_assert(this->found_
== NULL
);
1588 this->found_
= pexpr
;
1589 return TRAVERSE_EXIT
;
1592 // A traversal class used to turn shortcut operators into explicit if
1595 class Shortcuts
: public Traverse
1599 : Traverse(traverse_variables
1600 | traverse_statements
)
1605 variable(Named_object
*);
1608 statement(Block
*, size_t*, Statement
*);
1611 // Convert a shortcut operator.
1613 convert_shortcut(Block
* enclosing
, Expression
** pshortcut
);
1616 // Remove shortcut operators in a single statement.
1619 Shortcuts::statement(Block
* block
, size_t* pindex
, Statement
* s
)
1621 // FIXME: This approach doesn't work for switch statements, because
1622 // we add the new statements before the whole switch when we need to
1623 // instead add them just before the switch expression. The right
1624 // fix is probably to lower switch statements with nonconstant cases
1625 // to a series of conditionals.
1626 if (s
->switch_statement() != NULL
)
1627 return TRAVERSE_CONTINUE
;
1631 Find_shortcut find_shortcut
;
1633 // If S is a variable declaration, then ordinary traversal won't
1634 // do anything. We want to explicitly traverse the
1635 // initialization expression if there is one.
1636 Variable_declaration_statement
* vds
= s
->variable_declaration_statement();
1637 Expression
* init
= NULL
;
1639 s
->traverse_contents(&find_shortcut
);
1642 init
= vds
->var()->var_value()->init();
1644 return TRAVERSE_CONTINUE
;
1645 init
->traverse(&init
, &find_shortcut
);
1647 Expression
** pshortcut
= find_shortcut
.found();
1648 if (pshortcut
== NULL
)
1649 return TRAVERSE_CONTINUE
;
1651 Statement
* snew
= this->convert_shortcut(block
, pshortcut
);
1652 block
->insert_statement_before(*pindex
, snew
);
1655 if (pshortcut
== &init
)
1656 vds
->var()->var_value()->set_init(init
);
1660 // Remove shortcut operators in the initializer of a global variable.
1663 Shortcuts::variable(Named_object
* no
)
1665 if (no
->is_result_variable())
1666 return TRAVERSE_CONTINUE
;
1667 Variable
* var
= no
->var_value();
1668 Expression
* init
= var
->init();
1669 if (!var
->is_global() || init
== NULL
)
1670 return TRAVERSE_CONTINUE
;
1674 Find_shortcut find_shortcut
;
1675 init
->traverse(&init
, &find_shortcut
);
1676 Expression
** pshortcut
= find_shortcut
.found();
1677 if (pshortcut
== NULL
)
1678 return TRAVERSE_CONTINUE
;
1680 Statement
* snew
= this->convert_shortcut(NULL
, pshortcut
);
1681 var
->add_preinit_statement(snew
);
1682 if (pshortcut
== &init
)
1683 var
->set_init(init
);
1687 // Given an expression which uses a shortcut operator, return a
1688 // statement which implements it, and update *PSHORTCUT accordingly.
1691 Shortcuts::convert_shortcut(Block
* enclosing
, Expression
** pshortcut
)
1693 Binary_expression
* shortcut
= (*pshortcut
)->binary_expression();
1694 Expression
* left
= shortcut
->left();
1695 Expression
* right
= shortcut
->right();
1696 source_location loc
= shortcut
->location();
1698 Block
* retblock
= new Block(enclosing
, loc
);
1699 retblock
->set_end_location(loc
);
1701 Temporary_statement
* ts
= Statement::make_temporary(Type::make_boolean_type(),
1703 retblock
->add_statement(ts
);
1705 Block
* block
= new Block(retblock
, loc
);
1706 block
->set_end_location(loc
);
1707 Expression
* tmpref
= Expression::make_temporary_reference(ts
, loc
);
1708 Statement
* assign
= Statement::make_assignment(tmpref
, right
, loc
);
1709 block
->add_statement(assign
);
1711 Expression
* cond
= Expression::make_temporary_reference(ts
, loc
);
1712 if (shortcut
->binary_expression()->op() == OPERATOR_OROR
)
1713 cond
= Expression::make_unary(OPERATOR_NOT
, cond
, loc
);
1715 Statement
* if_statement
= Statement::make_if_statement(cond
, block
, NULL
,
1717 retblock
->add_statement(if_statement
);
1719 *pshortcut
= Expression::make_temporary_reference(ts
, loc
);
1723 // Now convert any shortcut operators in LEFT and RIGHT.
1724 Shortcuts shortcuts
;
1725 retblock
->traverse(&shortcuts
);
1727 return Statement::make_block_statement(retblock
, loc
);
1730 // Turn shortcut operators into explicit if statements. Doing this
1731 // considerably simplifies the order of evaluation rules.
1734 Gogo::remove_shortcuts()
1736 Shortcuts shortcuts
;
1737 this->traverse(&shortcuts
);
1740 // A traversal class which finds all the expressions which must be
1741 // evaluated in order within a statement or larger expression. This
1742 // is used to implement the rules about order of evaluation.
1744 class Find_eval_ordering
: public Traverse
1747 typedef std::vector
<Expression
**> Expression_pointers
;
1750 Find_eval_ordering()
1751 : Traverse(traverse_blocks
1752 | traverse_statements
1753 | traverse_expressions
),
1759 { return this->exprs_
.size(); }
1761 typedef Expression_pointers::const_iterator const_iterator
;
1765 { return this->exprs_
.begin(); }
1769 { return this->exprs_
.end(); }
1774 { return TRAVERSE_SKIP_COMPONENTS
; }
1777 statement(Block
*, size_t*, Statement
*)
1778 { return TRAVERSE_SKIP_COMPONENTS
; }
1781 expression(Expression
**);
1784 // A list of pointers to expressions with side-effects.
1785 Expression_pointers exprs_
;
1788 // If an expression must be evaluated in order, put it on the list.
1791 Find_eval_ordering::expression(Expression
** expression_pointer
)
1793 // We have to look at subexpressions before this one.
1794 if ((*expression_pointer
)->traverse_subexpressions(this) == TRAVERSE_EXIT
)
1795 return TRAVERSE_EXIT
;
1796 if ((*expression_pointer
)->must_eval_in_order())
1797 this->exprs_
.push_back(expression_pointer
);
1798 return TRAVERSE_SKIP_COMPONENTS
;
1801 // A traversal class for ordering evaluations.
1803 class Order_eval
: public Traverse
1807 : Traverse(traverse_variables
1808 | traverse_statements
)
1812 variable(Named_object
*);
1815 statement(Block
*, size_t*, Statement
*);
1818 // Implement the order of evaluation rules for a statement.
1821 Order_eval::statement(Block
* block
, size_t* pindex
, Statement
* s
)
1823 // FIXME: This approach doesn't work for switch statements, because
1824 // we add the new statements before the whole switch when we need to
1825 // instead add them just before the switch expression. The right
1826 // fix is probably to lower switch statements with nonconstant cases
1827 // to a series of conditionals.
1828 if (s
->switch_statement() != NULL
)
1829 return TRAVERSE_CONTINUE
;
1831 Find_eval_ordering find_eval_ordering
;
1833 // If S is a variable declaration, then ordinary traversal won't do
1834 // anything. We want to explicitly traverse the initialization
1835 // expression if there is one.
1836 Variable_declaration_statement
* vds
= s
->variable_declaration_statement();
1837 Expression
* init
= NULL
;
1838 Expression
* orig_init
= NULL
;
1840 s
->traverse_contents(&find_eval_ordering
);
1843 init
= vds
->var()->var_value()->init();
1845 return TRAVERSE_CONTINUE
;
1848 // It might seem that this could be
1849 // init->traverse_subexpressions. Unfortunately that can fail
1852 // newvar, err := call(arg())
1853 // Here newvar will have an init of call result 0 of
1854 // call(arg()). If we only traverse subexpressions, we will
1855 // only find arg(), and we won't bother to move anything out.
1856 // Then we get to the assignment to err, we will traverse the
1857 // whole statement, and this time we will find both call() and
1858 // arg(), and so we will move them out. This will cause them to
1859 // be put into temporary variables before the assignment to err
1860 // but after the declaration of newvar. To avoid that problem,
1861 // we traverse the entire expression here.
1862 Expression::traverse(&init
, &find_eval_ordering
);
1865 if (find_eval_ordering
.size() <= 1)
1867 // If there is only one expression with a side-effect, we can
1868 // leave it in place.
1869 return TRAVERSE_CONTINUE
;
1872 bool is_thunk
= s
->thunk_statement() != NULL
;
1873 for (Find_eval_ordering::const_iterator p
= find_eval_ordering
.begin();
1874 p
!= find_eval_ordering
.end();
1877 Expression
** pexpr
= *p
;
1879 // If the last expression is a send or receive expression, we
1880 // may be ignoring the value; we don't want to evaluate it
1882 if (p
+ 1 == find_eval_ordering
.end()
1883 && ((*pexpr
)->classification() == Expression::EXPRESSION_SEND
1884 || (*pexpr
)->classification() == Expression::EXPRESSION_RECEIVE
))
1887 // The last expression in a thunk will be the call passed to go
1888 // or defer, which we must not evaluate early.
1889 if (is_thunk
&& p
+ 1 == find_eval_ordering
.end())
1892 source_location loc
= (*pexpr
)->location();
1893 Temporary_statement
* ts
= Statement::make_temporary(NULL
, *pexpr
, loc
);
1894 block
->insert_statement_before(*pindex
, ts
);
1897 *pexpr
= Expression::make_temporary_reference(ts
, loc
);
1900 if (init
!= orig_init
)
1901 vds
->var()->var_value()->set_init(init
);
1903 return TRAVERSE_CONTINUE
;
1906 // Implement the order of evaluation rules for the initializer of a
1910 Order_eval::variable(Named_object
* no
)
1912 if (no
->is_result_variable())
1913 return TRAVERSE_CONTINUE
;
1914 Variable
* var
= no
->var_value();
1915 Expression
* init
= var
->init();
1916 if (!var
->is_global() || init
== NULL
)
1917 return TRAVERSE_CONTINUE
;
1919 Find_eval_ordering find_eval_ordering
;
1920 init
->traverse_subexpressions(&find_eval_ordering
);
1922 if (find_eval_ordering
.size() <= 1)
1924 // If there is only one expression with a side-effect, we can
1925 // leave it in place.
1926 return TRAVERSE_SKIP_COMPONENTS
;
1929 for (Find_eval_ordering::const_iterator p
= find_eval_ordering
.begin();
1930 p
!= find_eval_ordering
.end();
1933 Expression
** pexpr
= *p
;
1934 source_location loc
= (*pexpr
)->location();
1935 Temporary_statement
* ts
= Statement::make_temporary(NULL
, *pexpr
, loc
);
1936 var
->add_preinit_statement(ts
);
1937 *pexpr
= Expression::make_temporary_reference(ts
, loc
);
1940 return TRAVERSE_SKIP_COMPONENTS
;
1943 // Use temporary variables to implement the order of evaluation rules.
1946 Gogo::order_evaluations()
1948 Order_eval order_eval
;
1949 this->traverse(&order_eval
);
1952 // Traversal to convert calls to the predeclared recover function to
1953 // pass in an argument indicating whether it can recover from a panic
1956 class Convert_recover
: public Traverse
1959 Convert_recover(Named_object
* arg
)
1960 : Traverse(traverse_expressions
),
1966 expression(Expression
**);
1969 // The argument to pass to the function.
1973 // Convert calls to recover.
1976 Convert_recover::expression(Expression
** pp
)
1978 Call_expression
* ce
= (*pp
)->call_expression();
1979 if (ce
!= NULL
&& ce
->is_recover_call())
1980 ce
->set_recover_arg(Expression::make_var_reference(this->arg_
,
1982 return TRAVERSE_CONTINUE
;
1985 // Traversal for build_recover_thunks.
1987 class Build_recover_thunks
: public Traverse
1990 Build_recover_thunks(Gogo
* gogo
)
1991 : Traverse(traverse_functions
),
1996 function(Named_object
*);
2000 can_recover_arg(source_location
);
2006 // If this function calls recover, turn it into a thunk.
2009 Build_recover_thunks::function(Named_object
* orig_no
)
2011 Function
* orig_func
= orig_no
->func_value();
2012 if (!orig_func
->calls_recover()
2013 || orig_func
->is_recover_thunk()
2014 || orig_func
->has_recover_thunk())
2015 return TRAVERSE_CONTINUE
;
2017 Gogo
* gogo
= this->gogo_
;
2018 source_location location
= orig_func
->location();
2023 Function_type
* orig_fntype
= orig_func
->type();
2024 Typed_identifier_list
* new_params
= new Typed_identifier_list();
2025 std::string receiver_name
;
2026 if (orig_fntype
->is_method())
2028 const Typed_identifier
* receiver
= orig_fntype
->receiver();
2029 snprintf(buf
, sizeof buf
, "rt.%u", count
);
2031 receiver_name
= buf
;
2032 new_params
->push_back(Typed_identifier(receiver_name
, receiver
->type(),
2033 receiver
->location()));
2035 const Typed_identifier_list
* orig_params
= orig_fntype
->parameters();
2036 if (orig_params
!= NULL
&& !orig_params
->empty())
2038 for (Typed_identifier_list::const_iterator p
= orig_params
->begin();
2039 p
!= orig_params
->end();
2042 snprintf(buf
, sizeof buf
, "pt.%u", count
);
2044 new_params
->push_back(Typed_identifier(buf
, p
->type(),
2048 snprintf(buf
, sizeof buf
, "pr.%u", count
);
2050 std::string can_recover_name
= buf
;
2051 new_params
->push_back(Typed_identifier(can_recover_name
,
2052 Type::make_boolean_type(),
2053 orig_fntype
->location()));
2055 const Typed_identifier_list
* orig_results
= orig_fntype
->results();
2056 Typed_identifier_list
* new_results
;
2057 if (orig_results
== NULL
|| orig_results
->empty())
2061 new_results
= new Typed_identifier_list();
2062 for (Typed_identifier_list::const_iterator p
= orig_results
->begin();
2063 p
!= orig_results
->end();
2065 new_results
->push_back(*p
);
2068 Function_type
*new_fntype
= Type::make_function_type(NULL
, new_params
,
2070 orig_fntype
->location());
2071 if (orig_fntype
->is_varargs())
2072 new_fntype
->set_is_varargs();
2074 std::string name
= orig_no
->name() + "$recover";
2075 Named_object
*new_no
= gogo
->start_function(name
, new_fntype
, false,
2077 Function
*new_func
= new_no
->func_value();
2078 if (orig_func
->enclosing() != NULL
)
2079 new_func
->set_enclosing(orig_func
->enclosing());
2081 // We build the code for the original function attached to the new
2082 // function, and then swap the original and new function bodies.
2083 // This means that existing references to the original function will
2084 // then refer to the new function. That makes this code a little
2085 // confusing, in that the reference to NEW_NO really refers to the
2086 // other function, not the one we are building.
2088 Expression
* closure
= NULL
;
2089 if (orig_func
->needs_closure())
2091 Named_object
* orig_closure_no
= orig_func
->closure_var();
2092 Variable
* orig_closure_var
= orig_closure_no
->var_value();
2093 Variable
* new_var
= new Variable(orig_closure_var
->type(), NULL
, false,
2094 true, false, location
);
2095 snprintf(buf
, sizeof buf
, "closure.%u", count
);
2097 Named_object
* new_closure_no
= Named_object::make_variable(buf
, NULL
,
2099 new_func
->set_closure_var(new_closure_no
);
2100 closure
= Expression::make_var_reference(new_closure_no
, location
);
2103 Expression
* fn
= Expression::make_func_reference(new_no
, closure
, location
);
2105 Expression_list
* args
= new Expression_list();
2106 if (new_params
!= NULL
)
2108 // Note that we skip the last parameter, which is the boolean
2109 // indicating whether recover can succed.
2110 for (Typed_identifier_list::const_iterator p
= new_params
->begin();
2111 p
+ 1 != new_params
->end();
2114 Named_object
* p_no
= gogo
->lookup(p
->name(), NULL
);
2115 gcc_assert(p_no
!= NULL
2116 && p_no
->is_variable()
2117 && p_no
->var_value()->is_parameter());
2118 args
->push_back(Expression::make_var_reference(p_no
, location
));
2121 args
->push_back(this->can_recover_arg(location
));
2123 Expression
* call
= Expression::make_call(fn
, args
, false, location
);
2126 if (orig_fntype
->results() == NULL
|| orig_fntype
->results()->empty())
2127 s
= Statement::make_statement(call
);
2130 Expression_list
* vals
= new Expression_list();
2131 vals
->push_back(call
);
2132 s
= Statement::make_return_statement(new_func
->type()->results(),
2135 s
->determine_types();
2136 gogo
->add_statement(s
);
2138 gogo
->finish_function(location
);
2140 // Swap the function bodies and types.
2141 new_func
->swap_for_recover(orig_func
);
2142 orig_func
->set_is_recover_thunk();
2143 new_func
->set_calls_recover();
2144 new_func
->set_has_recover_thunk();
2146 Bindings
* orig_bindings
= orig_func
->block()->bindings();
2147 Bindings
* new_bindings
= new_func
->block()->bindings();
2148 if (orig_fntype
->is_method())
2150 // We changed the receiver to be a regular parameter. We have
2151 // to update the binding accordingly in both functions.
2152 Named_object
* orig_rec_no
= orig_bindings
->lookup_local(receiver_name
);
2153 gcc_assert(orig_rec_no
!= NULL
2154 && orig_rec_no
->is_variable()
2155 && !orig_rec_no
->var_value()->is_receiver());
2156 orig_rec_no
->var_value()->set_is_receiver();
2158 const std::string
& new_receiver_name(orig_fntype
->receiver()->name());
2159 Named_object
* new_rec_no
= new_bindings
->lookup_local(new_receiver_name
);
2160 gcc_assert(new_rec_no
!= NULL
2161 && new_rec_no
->is_variable()
2162 && new_rec_no
->var_value()->is_receiver());
2163 new_rec_no
->var_value()->set_is_not_receiver();
2166 // Because we flipped blocks but not types, the can_recover
2167 // parameter appears in the (now) old bindings as a parameter.
2168 // Change it to a local variable, whereupon it will be discarded.
2169 Named_object
* can_recover_no
= orig_bindings
->lookup_local(can_recover_name
);
2170 gcc_assert(can_recover_no
!= NULL
2171 && can_recover_no
->is_variable()
2172 && can_recover_no
->var_value()->is_parameter());
2173 orig_bindings
->remove_binding(can_recover_no
);
2175 // Add the can_recover argument to the (now) new bindings, and
2176 // attach it to any recover statements.
2177 Variable
* can_recover_var
= new Variable(Type::make_boolean_type(), NULL
,
2178 false, true, false, location
);
2179 can_recover_no
= new_bindings
->add_variable(can_recover_name
, NULL
,
2181 Convert_recover
convert_recover(can_recover_no
);
2182 new_func
->traverse(&convert_recover
);
2184 // Update the function pointers in any named results.
2185 new_func
->update_named_result_variables();
2186 orig_func
->update_named_result_variables();
2188 return TRAVERSE_CONTINUE
;
2191 // Return the expression to pass for the .can_recover parameter to the
2192 // new function. This indicates whether a call to recover may return
2193 // non-nil. The expression is
2194 // __go_can_recover(__builtin_return_address()).
2197 Build_recover_thunks::can_recover_arg(source_location location
)
2199 static Named_object
* builtin_return_address
;
2200 if (builtin_return_address
== NULL
)
2202 const source_location bloc
= BUILTINS_LOCATION
;
2204 Typed_identifier_list
* param_types
= new Typed_identifier_list();
2205 Type
* uint_type
= Type::lookup_integer_type("uint");
2206 param_types
->push_back(Typed_identifier("l", uint_type
, bloc
));
2208 Typed_identifier_list
* return_types
= new Typed_identifier_list();
2209 Type
* voidptr_type
= Type::make_pointer_type(Type::make_void_type());
2210 return_types
->push_back(Typed_identifier("", voidptr_type
, bloc
));
2212 Function_type
* fntype
= Type::make_function_type(NULL
, param_types
,
2213 return_types
, bloc
);
2214 builtin_return_address
=
2215 Named_object::make_function_declaration("__builtin_return_address",
2216 NULL
, fntype
, bloc
);
2217 const char* n
= "__builtin_return_address";
2218 builtin_return_address
->func_declaration_value()->set_asm_name(n
);
2221 static Named_object
* can_recover
;
2222 if (can_recover
== NULL
)
2224 const source_location bloc
= BUILTINS_LOCATION
;
2225 Typed_identifier_list
* param_types
= new Typed_identifier_list();
2226 Type
* voidptr_type
= Type::make_pointer_type(Type::make_void_type());
2227 param_types
->push_back(Typed_identifier("a", voidptr_type
, bloc
));
2228 Type
* boolean_type
= Type::make_boolean_type();
2229 Typed_identifier_list
* results
= new Typed_identifier_list();
2230 results
->push_back(Typed_identifier("", boolean_type
, bloc
));
2231 Function_type
* fntype
= Type::make_function_type(NULL
, param_types
,
2233 can_recover
= Named_object::make_function_declaration("__go_can_recover",
2236 can_recover
->func_declaration_value()->set_asm_name("__go_can_recover");
2239 Expression
* fn
= Expression::make_func_reference(builtin_return_address
,
2243 mpz_init_set_ui(zval
, 0UL);
2244 Expression
* zexpr
= Expression::make_integer(&zval
, NULL
, location
);
2246 Expression_list
*args
= new Expression_list();
2247 args
->push_back(zexpr
);
2249 Expression
* call
= Expression::make_call(fn
, args
, false, location
);
2251 args
= new Expression_list();
2252 args
->push_back(call
);
2254 fn
= Expression::make_func_reference(can_recover
, NULL
, location
);
2255 return Expression::make_call(fn
, args
, false, location
);
2258 // Build thunks for functions which call recover. We build a new
2259 // function with an extra parameter, which is whether a call to
2260 // recover can succeed. We then move the body of this function to
2261 // that one. We then turn this function into a thunk which calls the
2262 // new one, passing the value of
2263 // __go_can_recover(__builtin_return_address()). The function will be
2264 // marked as not splitting the stack. This will cooperate with the
2265 // implementation of defer to make recover do the right thing.
2268 Gogo::build_recover_thunks()
2270 Build_recover_thunks
build_recover_thunks(this);
2271 this->traverse(&build_recover_thunks
);
2274 // Look for named types to see whether we need to create an interface
2277 class Build_method_tables
: public Traverse
2280 Build_method_tables(Gogo
* gogo
,
2281 const std::vector
<Interface_type
*>& interfaces
)
2282 : Traverse(traverse_types
),
2283 gogo_(gogo
), interfaces_(interfaces
)
2292 // A list of locally defined interfaces which have hidden methods.
2293 const std::vector
<Interface_type
*>& interfaces_
;
2296 // Build all required interface method tables for types. We need to
2297 // ensure that we have an interface method table for every interface
2298 // which has a hidden method, for every named type which implements
2299 // that interface. Normally we can just build interface method tables
2300 // as we need them. However, in some cases we can require an
2301 // interface method table for an interface defined in a different
2302 // package for a type defined in that package. If that interface and
2303 // type both use a hidden method, that is OK. However, we will not be
2304 // able to build that interface method table when we need it, because
2305 // the type's hidden method will be static. So we have to build it
2306 // here, and just refer it from other packages as needed.
2309 Gogo::build_interface_method_tables()
2311 std::vector
<Interface_type
*> hidden_interfaces
;
2312 hidden_interfaces
.reserve(this->interface_types_
.size());
2313 for (std::vector
<Interface_type
*>::const_iterator pi
=
2314 this->interface_types_
.begin();
2315 pi
!= this->interface_types_
.end();
2318 const Typed_identifier_list
* methods
= (*pi
)->methods();
2319 if (methods
== NULL
)
2321 for (Typed_identifier_list::const_iterator pm
= methods
->begin();
2322 pm
!= methods
->end();
2325 if (Gogo::is_hidden_name(pm
->name()))
2327 hidden_interfaces
.push_back(*pi
);
2333 if (!hidden_interfaces
.empty())
2335 // Now traverse the tree looking for all named types.
2336 Build_method_tables
bmt(this, hidden_interfaces
);
2337 this->traverse(&bmt
);
2340 // We no longer need the list of interfaces.
2342 this->interface_types_
.clear();
2345 // This is called for each type. For a named type, for each of the
2346 // interfaces with hidden methods that it implements, create the
2350 Build_method_tables::type(Type
* type
)
2352 Named_type
* nt
= type
->named_type();
2355 for (std::vector
<Interface_type
*>::const_iterator p
=
2356 this->interfaces_
.begin();
2357 p
!= this->interfaces_
.end();
2360 // We ask whether a pointer to the named type implements the
2361 // interface, because a pointer can implement more methods
2363 if ((*p
)->implements_interface(Type::make_pointer_type(nt
), NULL
))
2365 nt
->interface_method_table(this->gogo_
, *p
, false);
2366 nt
->interface_method_table(this->gogo_
, *p
, true);
2370 return TRAVERSE_CONTINUE
;
2373 // Traversal class used to check for return statements.
2375 class Check_return_statements_traverse
: public Traverse
2378 Check_return_statements_traverse()
2379 : Traverse(traverse_functions
)
2383 function(Named_object
*);
2386 // Check that a function has a return statement if it needs one.
2389 Check_return_statements_traverse::function(Named_object
* no
)
2391 Function
* func
= no
->func_value();
2392 const Function_type
* fntype
= func
->type();
2393 const Typed_identifier_list
* results
= fntype
->results();
2395 // We only need a return statement if there is a return value.
2396 if (results
== NULL
|| results
->empty())
2397 return TRAVERSE_CONTINUE
;
2399 if (func
->block()->may_fall_through())
2400 error_at(func
->location(), "control reaches end of non-void function");
2402 return TRAVERSE_CONTINUE
;
2405 // Check return statements.
2408 Gogo::check_return_statements()
2410 Check_return_statements_traverse traverse
;
2411 this->traverse(&traverse
);
2414 // Get the unique prefix to use before all exported symbols. This
2415 // must be unique across the entire link.
2418 Gogo::unique_prefix() const
2420 gcc_assert(!this->unique_prefix_
.empty());
2421 return this->unique_prefix_
;
2424 // Set the unique prefix to use before all exported symbols. This
2425 // comes from the command line option -fgo-prefix=XXX.
2428 Gogo::set_unique_prefix(const std::string
& arg
)
2430 gcc_assert(this->unique_prefix_
.empty());
2431 this->unique_prefix_
= arg
;
2434 // Work out the package priority. It is one more than the maximum
2435 // priority of an imported package.
2438 Gogo::package_priority() const
2441 for (Packages::const_iterator p
= this->packages_
.begin();
2442 p
!= this->packages_
.end();
2444 if (p
->second
->priority() > priority
)
2445 priority
= p
->second
->priority();
2446 return priority
+ 1;
2449 // Export identifiers as requested.
2454 // For now we always stream to a section. Later we may want to
2455 // support streaming to a separate file.
2456 Stream_to_section stream
;
2458 Export
exp(&stream
);
2459 exp
.register_builtin_types(this);
2460 exp
.export_globals(this->package_name(),
2461 this->unique_prefix(),
2462 this->package_priority(),
2463 (this->need_init_fn_
&& this->package_name() != "main"
2464 ? this->get_init_fn_name()
2466 this->imported_init_fns_
,
2467 this->package_
->bindings());
2472 Function::Function(Function_type
* type
, Function
* enclosing
, Block
* block
,
2473 source_location location
)
2474 : type_(type
), enclosing_(enclosing
), named_results_(NULL
),
2475 closure_var_(NULL
), block_(block
), location_(location
), fndecl_(NULL
),
2476 defer_stack_(NULL
), calls_recover_(false), is_recover_thunk_(false),
2477 has_recover_thunk_(false)
2481 // Create the named result variables.
2484 Function::create_named_result_variables(Gogo
* gogo
)
2486 const Typed_identifier_list
* results
= this->type_
->results();
2489 || results
->front().name().empty())
2492 this->named_results_
= new Named_results();
2493 this->named_results_
->reserve(results
->size());
2495 Block
* block
= this->block_
;
2497 for (Typed_identifier_list::const_iterator p
= results
->begin();
2498 p
!= results
->end();
2501 std::string name
= p
->name();
2502 if (Gogo::is_sink_name(name
))
2504 static int unnamed_result_counter
;
2506 snprintf(buf
, sizeof buf
, "_$%d", unnamed_result_counter
);
2507 ++unnamed_result_counter
;
2508 name
= gogo
->pack_hidden_name(buf
, false);
2510 Result_variable
* result
= new Result_variable(p
->type(), this, index
);
2511 Named_object
* no
= block
->bindings()->add_result_variable(name
, result
);
2512 this->named_results_
->push_back(no
);
2516 // Update the named result variables when cloning a function which
2520 Function::update_named_result_variables()
2522 if (this->named_results_
== NULL
)
2525 for (Named_results::iterator p
= this->named_results_
->begin();
2526 p
!= this->named_results_
->end();
2528 (*p
)->result_var_value()->set_function(this);
2531 // Return the closure variable, creating it if necessary.
2534 Function::closure_var()
2536 if (this->closure_var_
== NULL
)
2538 // We don't know the type of the variable yet. We add fields as
2540 source_location loc
= this->type_
->location();
2541 Struct_field_list
* sfl
= new Struct_field_list
;
2542 Type
* struct_type
= Type::make_struct_type(sfl
, loc
);
2543 Variable
* var
= new Variable(Type::make_pointer_type(struct_type
),
2544 NULL
, false, true, false, loc
);
2545 this->closure_var_
= Named_object::make_variable("closure", NULL
, var
);
2546 // Note that the new variable is not in any binding contour.
2548 return this->closure_var_
;
2551 // Set the type of the closure variable.
2554 Function::set_closure_type()
2556 if (this->closure_var_
== NULL
)
2558 Named_object
* closure
= this->closure_var_
;
2559 Struct_type
* st
= closure
->var_value()->type()->deref()->struct_type();
2560 unsigned int index
= 0;
2561 for (Closure_fields::const_iterator p
= this->closure_fields_
.begin();
2562 p
!= this->closure_fields_
.end();
2565 Named_object
* no
= p
->first
;
2567 snprintf(buf
, sizeof buf
, "%u", index
);
2568 std::string n
= no
->name() + buf
;
2570 if (no
->is_variable())
2571 var_type
= no
->var_value()->type();
2573 var_type
= no
->result_var_value()->type();
2574 Type
* field_type
= Type::make_pointer_type(var_type
);
2575 st
->push_field(Struct_field(Typed_identifier(n
, field_type
, p
->second
)));
2579 // Return whether this function is a method.
2582 Function::is_method() const
2584 return this->type_
->is_method();
2587 // Add a label definition.
2590 Function::add_label_definition(const std::string
& label_name
,
2591 source_location location
)
2593 Label
* lnull
= NULL
;
2594 std::pair
<Labels::iterator
, bool> ins
=
2595 this->labels_
.insert(std::make_pair(label_name
, lnull
));
2598 // This is a new label.
2599 Label
* label
= new Label(label_name
);
2600 label
->define(location
);
2601 ins
.first
->second
= label
;
2606 // The label was already in the hash table.
2607 Label
* label
= ins
.first
->second
;
2608 if (!label
->is_defined())
2610 label
->define(location
);
2615 error_at(location
, "redefinition of label %qs",
2616 Gogo::message_name(label_name
).c_str());
2617 inform(label
->location(), "previous definition of %qs was here",
2618 Gogo::message_name(label_name
).c_str());
2619 return new Label(label_name
);
2624 // Add a reference to a label.
2627 Function::add_label_reference(const std::string
& label_name
)
2629 Label
* lnull
= NULL
;
2630 std::pair
<Labels::iterator
, bool> ins
=
2631 this->labels_
.insert(std::make_pair(label_name
, lnull
));
2634 // The label was already in the hash table.
2635 return ins
.first
->second
;
2639 gcc_assert(ins
.first
->second
== NULL
);
2640 Label
* label
= new Label(label_name
);
2641 ins
.first
->second
= label
;
2646 // Swap one function with another. This is used when building the
2647 // thunk we use to call a function which calls recover. It may not
2648 // work for any other case.
2651 Function::swap_for_recover(Function
*x
)
2653 gcc_assert(this->enclosing_
== x
->enclosing_
);
2654 std::swap(this->named_results_
, x
->named_results_
);
2655 std::swap(this->closure_var_
, x
->closure_var_
);
2656 std::swap(this->block_
, x
->block_
);
2657 gcc_assert(this->location_
== x
->location_
);
2658 gcc_assert(this->fndecl_
== NULL
&& x
->fndecl_
== NULL
);
2659 gcc_assert(this->defer_stack_
== NULL
&& x
->defer_stack_
== NULL
);
2662 // Traverse the tree.
2665 Function::traverse(Traverse
* traverse
)
2667 unsigned int traverse_mask
= traverse
->traverse_mask();
2670 & (Traverse::traverse_types
| Traverse::traverse_expressions
))
2673 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
2674 return TRAVERSE_EXIT
;
2677 // FIXME: We should check traverse_functions here if nested
2678 // functions are stored in block bindings.
2679 if (this->block_
!= NULL
2681 & (Traverse::traverse_variables
2682 | Traverse::traverse_constants
2683 | Traverse::traverse_blocks
2684 | Traverse::traverse_statements
2685 | Traverse::traverse_expressions
2686 | Traverse::traverse_types
)) != 0)
2688 if (this->block_
->traverse(traverse
) == TRAVERSE_EXIT
)
2689 return TRAVERSE_EXIT
;
2692 return TRAVERSE_CONTINUE
;
2695 // Work out types for unspecified variables and constants.
2698 Function::determine_types()
2700 if (this->block_
!= NULL
)
2701 this->block_
->determine_types();
2704 // Export the function.
2707 Function::export_func(Export
* exp
, const std::string
& name
) const
2709 Function::export_func_with_type(exp
, name
, this->type_
);
2712 // Export a function with a type.
2715 Function::export_func_with_type(Export
* exp
, const std::string
& name
,
2716 const Function_type
* fntype
)
2718 exp
->write_c_string("func ");
2720 if (fntype
->is_method())
2722 exp
->write_c_string("(");
2723 exp
->write_type(fntype
->receiver()->type());
2724 exp
->write_c_string(") ");
2727 exp
->write_string(name
);
2729 exp
->write_c_string(" (");
2730 const Typed_identifier_list
* parameters
= fntype
->parameters();
2731 if (parameters
!= NULL
)
2733 bool is_varargs
= fntype
->is_varargs();
2735 for (Typed_identifier_list::const_iterator p
= parameters
->begin();
2736 p
!= parameters
->end();
2742 exp
->write_c_string(", ");
2743 if (!is_varargs
|| p
+ 1 != parameters
->end())
2744 exp
->write_type(p
->type());
2747 exp
->write_c_string("...");
2748 exp
->write_type(p
->type()->array_type()->element_type());
2752 exp
->write_c_string(")");
2754 const Typed_identifier_list
* results
= fntype
->results();
2755 if (results
!= NULL
)
2757 if (results
->size() == 1)
2759 exp
->write_c_string(" ");
2760 exp
->write_type(results
->begin()->type());
2764 exp
->write_c_string(" (");
2766 for (Typed_identifier_list::const_iterator p
= results
->begin();
2767 p
!= results
->end();
2773 exp
->write_c_string(", ");
2774 exp
->write_type(p
->type());
2776 exp
->write_c_string(")");
2779 exp
->write_c_string(";\n");
2782 // Import a function.
2785 Function::import_func(Import
* imp
, std::string
* pname
,
2786 Typed_identifier
** preceiver
,
2787 Typed_identifier_list
** pparameters
,
2788 Typed_identifier_list
** presults
,
2791 imp
->require_c_string("func ");
2794 if (imp
->peek_char() == '(')
2796 imp
->require_c_string("(");
2797 Type
* rtype
= imp
->read_type();
2798 *preceiver
= new Typed_identifier(Import::import_marker
, rtype
,
2800 imp
->require_c_string(") ");
2803 *pname
= imp
->read_identifier();
2805 Typed_identifier_list
* parameters
;
2806 *is_varargs
= false;
2807 imp
->require_c_string(" (");
2808 if (imp
->peek_char() == ')')
2812 parameters
= new Typed_identifier_list();
2815 if (imp
->match_c_string("..."))
2821 Type
* ptype
= imp
->read_type();
2823 ptype
= Type::make_array_type(ptype
, NULL
);
2824 parameters
->push_back(Typed_identifier(Import::import_marker
,
2825 ptype
, imp
->location()));
2826 if (imp
->peek_char() != ',')
2828 gcc_assert(!*is_varargs
);
2829 imp
->require_c_string(", ");
2832 imp
->require_c_string(")");
2833 *pparameters
= parameters
;
2835 Typed_identifier_list
* results
;
2836 if (imp
->peek_char() != ' ')
2840 results
= new Typed_identifier_list();
2841 imp
->require_c_string(" ");
2842 if (imp
->peek_char() != '(')
2844 Type
* rtype
= imp
->read_type();
2845 results
->push_back(Typed_identifier(Import::import_marker
, rtype
,
2850 imp
->require_c_string("(");
2853 Type
* rtype
= imp
->read_type();
2854 results
->push_back(Typed_identifier(Import::import_marker
,
2855 rtype
, imp
->location()));
2856 if (imp
->peek_char() != ',')
2858 imp
->require_c_string(", ");
2860 imp
->require_c_string(")");
2863 imp
->require_c_string(";\n");
2864 *presults
= results
;
2869 Block::Block(Block
* enclosing
, source_location location
)
2870 : enclosing_(enclosing
), statements_(),
2871 bindings_(new Bindings(enclosing
== NULL
2873 : enclosing
->bindings())),
2874 start_location_(location
),
2875 end_location_(UNKNOWN_LOCATION
)
2879 // Add a statement to a block.
2882 Block::add_statement(Statement
* statement
)
2884 this->statements_
.push_back(statement
);
2887 // Add a statement to the front of a block. This is slow but is only
2888 // used for reference counts of parameters.
2891 Block::add_statement_at_front(Statement
* statement
)
2893 this->statements_
.insert(this->statements_
.begin(), statement
);
2896 // Replace a statement in a block.
2899 Block::replace_statement(size_t index
, Statement
* s
)
2901 gcc_assert(index
< this->statements_
.size());
2902 this->statements_
[index
] = s
;
2905 // Add a statement before another statement.
2908 Block::insert_statement_before(size_t index
, Statement
* s
)
2910 gcc_assert(index
< this->statements_
.size());
2911 this->statements_
.insert(this->statements_
.begin() + index
, s
);
2914 // Add a statement after another statement.
2917 Block::insert_statement_after(size_t index
, Statement
* s
)
2919 gcc_assert(index
< this->statements_
.size());
2920 this->statements_
.insert(this->statements_
.begin() + index
+ 1, s
);
2923 // Traverse the tree.
2926 Block::traverse(Traverse
* traverse
)
2928 unsigned int traverse_mask
= traverse
->traverse_mask();
2930 if ((traverse_mask
& Traverse::traverse_blocks
) != 0)
2932 int t
= traverse
->block(this);
2933 if (t
== TRAVERSE_EXIT
)
2934 return TRAVERSE_EXIT
;
2935 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
2936 return TRAVERSE_CONTINUE
;
2940 & (Traverse::traverse_variables
2941 | Traverse::traverse_constants
2942 | Traverse::traverse_expressions
2943 | Traverse::traverse_types
)) != 0)
2945 for (Bindings::const_definitions_iterator pb
=
2946 this->bindings_
->begin_definitions();
2947 pb
!= this->bindings_
->end_definitions();
2950 switch ((*pb
)->classification())
2952 case Named_object::NAMED_OBJECT_CONST
:
2953 if ((traverse_mask
& Traverse::traverse_constants
) != 0)
2955 if (traverse
->constant(*pb
, false) == TRAVERSE_EXIT
)
2956 return TRAVERSE_EXIT
;
2958 if ((traverse_mask
& Traverse::traverse_types
) != 0
2959 || (traverse_mask
& Traverse::traverse_expressions
) != 0)
2961 Type
* t
= (*pb
)->const_value()->type();
2963 && Type::traverse(t
, traverse
) == TRAVERSE_EXIT
)
2964 return TRAVERSE_EXIT
;
2966 if ((traverse_mask
& Traverse::traverse_expressions
) != 0
2967 || (traverse_mask
& Traverse::traverse_types
) != 0)
2969 if ((*pb
)->const_value()->traverse_expression(traverse
)
2971 return TRAVERSE_EXIT
;
2975 case Named_object::NAMED_OBJECT_VAR
:
2976 case Named_object::NAMED_OBJECT_RESULT_VAR
:
2977 if ((traverse_mask
& Traverse::traverse_variables
) != 0)
2979 if (traverse
->variable(*pb
) == TRAVERSE_EXIT
)
2980 return TRAVERSE_EXIT
;
2982 if (((traverse_mask
& Traverse::traverse_types
) != 0
2983 || (traverse_mask
& Traverse::traverse_expressions
) != 0)
2984 && ((*pb
)->is_result_variable()
2985 || (*pb
)->var_value()->has_type()))
2987 Type
* t
= ((*pb
)->is_variable()
2988 ? (*pb
)->var_value()->type()
2989 : (*pb
)->result_var_value()->type());
2991 && Type::traverse(t
, traverse
) == TRAVERSE_EXIT
)
2992 return TRAVERSE_EXIT
;
2994 if ((*pb
)->is_variable()
2995 && ((traverse_mask
& Traverse::traverse_expressions
) != 0
2996 || (traverse_mask
& Traverse::traverse_types
) != 0))
2998 if ((*pb
)->var_value()->traverse_expression(traverse
)
3000 return TRAVERSE_EXIT
;
3004 case Named_object::NAMED_OBJECT_FUNC
:
3005 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
3006 // FIXME: Where will nested functions be found?
3009 case Named_object::NAMED_OBJECT_TYPE
:
3010 if ((traverse_mask
& Traverse::traverse_types
) != 0
3011 || (traverse_mask
& Traverse::traverse_expressions
) != 0)
3013 if (Type::traverse((*pb
)->type_value(), traverse
)
3015 return TRAVERSE_EXIT
;
3019 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
3020 case Named_object::NAMED_OBJECT_UNKNOWN
:
3023 case Named_object::NAMED_OBJECT_PACKAGE
:
3024 case Named_object::NAMED_OBJECT_SINK
:
3033 // No point in checking traverse_mask here--if we got here we always
3034 // want to walk the statements. The traversal can insert new
3035 // statements before or after the current statement. Inserting
3036 // statements before the current statement requires updating I via
3037 // the pointer; those statements will not be traversed. Any new
3038 // statements inserted after the current statement will be traversed
3040 for (size_t i
= 0; i
< this->statements_
.size(); ++i
)
3042 if (this->statements_
[i
]->traverse(this, &i
, traverse
) == TRAVERSE_EXIT
)
3043 return TRAVERSE_EXIT
;
3046 return TRAVERSE_CONTINUE
;
3049 // Work out types for unspecified variables and constants.
3052 Block::determine_types()
3054 for (Bindings::const_definitions_iterator pb
=
3055 this->bindings_
->begin_definitions();
3056 pb
!= this->bindings_
->end_definitions();
3059 if ((*pb
)->is_variable())
3060 (*pb
)->var_value()->determine_type();
3061 else if ((*pb
)->is_const())
3062 (*pb
)->const_value()->determine_type();
3065 for (std::vector
<Statement
*>::const_iterator ps
= this->statements_
.begin();
3066 ps
!= this->statements_
.end();
3068 (*ps
)->determine_types();
3071 // Return true if the statements in this block may fall through.
3074 Block::may_fall_through() const
3076 if (this->statements_
.empty())
3078 return this->statements_
.back()->may_fall_through();
3083 Variable::Variable(Type
* type
, Expression
* init
, bool is_global
,
3084 bool is_parameter
, bool is_receiver
,
3085 source_location location
)
3086 : type_(type
), init_(init
), preinit_(NULL
), location_(location
),
3087 is_global_(is_global
), is_parameter_(is_parameter
),
3088 is_receiver_(is_receiver
), is_varargs_parameter_(false),
3089 is_address_taken_(false), seen_(false), init_is_lowered_(false),
3090 type_from_init_tuple_(false), type_from_range_index_(false),
3091 type_from_range_value_(false), type_from_chan_element_(false),
3092 is_type_switch_var_(false)
3094 gcc_assert(type
!= NULL
|| init
!= NULL
);
3095 gcc_assert(!is_parameter
|| init
== NULL
);
3098 // Traverse the initializer expression.
3101 Variable::traverse_expression(Traverse
* traverse
)
3103 if (this->preinit_
!= NULL
)
3105 if (this->preinit_
->traverse(traverse
) == TRAVERSE_EXIT
)
3106 return TRAVERSE_EXIT
;
3108 if (this->init_
!= NULL
)
3110 if (Expression::traverse(&this->init_
, traverse
) == TRAVERSE_EXIT
)
3111 return TRAVERSE_EXIT
;
3113 return TRAVERSE_CONTINUE
;
3116 // Lower the initialization expression after parsing is complete.
3119 Variable::lower_init_expression(Gogo
* gogo
, Named_object
* function
)
3121 if (this->init_
!= NULL
&& !this->init_is_lowered_
)
3125 // We will give an error elsewhere, this is just to prevent
3126 // an infinite loop.
3131 gogo
->lower_expression(function
, &this->init_
);
3133 this->seen_
= false;
3135 this->init_is_lowered_
= true;
3139 // Get the preinit block.
3142 Variable::preinit_block()
3144 gcc_assert(this->is_global_
);
3145 if (this->preinit_
== NULL
)
3146 this->preinit_
= new Block(NULL
, this->location());
3147 return this->preinit_
;
3150 // Add a statement to be run before the initialization expression.
3153 Variable::add_preinit_statement(Statement
* s
)
3155 Block
* b
= this->preinit_block();
3156 b
->add_statement(s
);
3157 b
->set_end_location(s
->location());
3160 // In an assignment which sets a variable to a tuple of EXPR, return
3161 // the type of the first element of the tuple.
3164 Variable::type_from_tuple(Expression
* expr
, bool report_error
) const
3166 if (expr
->map_index_expression() != NULL
)
3168 Map_type
* mt
= expr
->map_index_expression()->get_map_type();
3170 return Type::make_error_type();
3171 return mt
->val_type();
3173 else if (expr
->receive_expression() != NULL
)
3175 Expression
* channel
= expr
->receive_expression()->channel();
3176 Type
* channel_type
= channel
->type();
3177 if (channel_type
->channel_type() == NULL
)
3178 return Type::make_error_type();
3179 return channel_type
->channel_type()->element_type();
3184 error_at(this->location(), "invalid tuple definition");
3185 return Type::make_error_type();
3189 // Given EXPR used in a range clause, return either the index type or
3190 // the value type of the range, depending upon GET_INDEX_TYPE.
3193 Variable::type_from_range(Expression
* expr
, bool get_index_type
,
3194 bool report_error
) const
3196 Type
* t
= expr
->type();
3197 if (t
->array_type() != NULL
3198 || (t
->points_to() != NULL
3199 && t
->points_to()->array_type() != NULL
3200 && !t
->points_to()->is_open_array_type()))
3203 return Type::lookup_integer_type("int");
3205 return t
->deref()->array_type()->element_type();
3207 else if (t
->is_string_type())
3208 return Type::lookup_integer_type("int");
3209 else if (t
->map_type() != NULL
)
3212 return t
->map_type()->key_type();
3214 return t
->map_type()->val_type();
3216 else if (t
->channel_type() != NULL
)
3219 return t
->channel_type()->element_type();
3223 error_at(this->location(),
3224 "invalid definition of value variable for channel range");
3225 return Type::make_error_type();
3231 error_at(this->location(), "invalid type for range clause");
3232 return Type::make_error_type();
3236 // EXPR should be a channel. Return the channel's element type.
3239 Variable::type_from_chan_element(Expression
* expr
, bool report_error
) const
3241 Type
* t
= expr
->type();
3242 if (t
->channel_type() != NULL
)
3243 return t
->channel_type()->element_type();
3247 error_at(this->location(), "expected channel");
3248 return Type::make_error_type();
3252 // Return the type of the Variable. This may be called before
3253 // Variable::determine_type is called, which means that we may need to
3254 // get the type from the initializer. FIXME: If we combine lowering
3255 // with type determination, then this should be unnecessary.
3260 // A variable in a type switch with a nil case will have the wrong
3261 // type here. This gets fixed up in determine_type, below.
3262 Type
* type
= this->type_
;
3263 Expression
* init
= this->init_
;
3264 if (this->is_type_switch_var_
3265 && this->type_
->is_nil_constant_as_type())
3267 Type_guard_expression
* tge
= this->init_
->type_guard_expression();
3268 gcc_assert(tge
!= NULL
);
3275 if (this->type_
== NULL
|| !this->type_
->is_error_type())
3277 error_at(this->location_
, "variable initializer refers to itself");
3278 this->type_
= Type::make_error_type();
3287 else if (this->type_from_init_tuple_
)
3288 type
= this->type_from_tuple(init
, false);
3289 else if (this->type_from_range_index_
|| this->type_from_range_value_
)
3290 type
= this->type_from_range(init
, this->type_from_range_index_
, false);
3291 else if (this->type_from_chan_element_
)
3292 type
= this->type_from_chan_element(init
, false);
3295 gcc_assert(init
!= NULL
);
3296 type
= init
->type();
3297 gcc_assert(type
!= NULL
);
3299 // Variables should not have abstract types.
3300 if (type
->is_abstract())
3301 type
= type
->make_non_abstract_type();
3303 if (type
->is_void_type())
3304 type
= Type::make_error_type();
3307 this->seen_
= false;
3312 // Fetch the type from a const pointer, in which case it should have
3313 // been set already.
3316 Variable::type() const
3318 gcc_assert(this->type_
!= NULL
);
3322 // Set the type if necessary.
3325 Variable::determine_type()
3327 // A variable in a type switch with a nil case will have the wrong
3328 // type here. It will have an initializer which is a type guard.
3329 // We want to initialize it to the value without the type guard, and
3330 // use the type of that value as well.
3331 if (this->is_type_switch_var_
&& this->type_
->is_nil_constant_as_type())
3333 Type_guard_expression
* tge
= this->init_
->type_guard_expression();
3334 gcc_assert(tge
!= NULL
);
3336 this->init_
= tge
->expr();
3339 if (this->init_
== NULL
)
3340 gcc_assert(this->type_
!= NULL
&& !this->type_
->is_abstract());
3341 else if (this->type_from_init_tuple_
)
3343 Expression
*init
= this->init_
;
3344 init
->determine_type_no_context();
3345 this->type_
= this->type_from_tuple(init
, true);
3348 else if (this->type_from_range_index_
|| this->type_from_range_value_
)
3350 Expression
* init
= this->init_
;
3351 init
->determine_type_no_context();
3352 this->type_
= this->type_from_range(init
, this->type_from_range_index_
,
3358 // type_from_chan_element_ should have been cleared during
3360 gcc_assert(!this->type_from_chan_element_
);
3362 Type_context
context(this->type_
, false);
3363 this->init_
->determine_type(&context
);
3364 if (this->type_
== NULL
)
3366 Type
* type
= this->init_
->type();
3367 gcc_assert(type
!= NULL
);
3368 if (type
->is_abstract())
3369 type
= type
->make_non_abstract_type();
3371 if (type
->is_void_type())
3373 error_at(this->location_
, "variable has no type");
3374 type
= Type::make_error_type();
3376 else if (type
->is_nil_type())
3378 error_at(this->location_
, "variable defined to nil type");
3379 type
= Type::make_error_type();
3381 else if (type
->is_call_multiple_result_type())
3383 error_at(this->location_
,
3384 "single variable set to multiple value function call");
3385 type
= Type::make_error_type();
3393 // Export the variable
3396 Variable::export_var(Export
* exp
, const std::string
& name
) const
3398 gcc_assert(this->is_global_
);
3399 exp
->write_c_string("var ");
3400 exp
->write_string(name
);
3401 exp
->write_c_string(" ");
3402 exp
->write_type(this->type());
3403 exp
->write_c_string(";\n");
3406 // Import a variable.
3409 Variable::import_var(Import
* imp
, std::string
* pname
, Type
** ptype
)
3411 imp
->require_c_string("var ");
3412 *pname
= imp
->read_identifier();
3413 imp
->require_c_string(" ");
3414 *ptype
= imp
->read_type();
3415 imp
->require_c_string(";\n");
3418 // Class Named_constant.
3420 // Traverse the initializer expression.
3423 Named_constant::traverse_expression(Traverse
* traverse
)
3425 return Expression::traverse(&this->expr_
, traverse
);
3428 // Determine the type of the constant.
3431 Named_constant::determine_type()
3433 if (this->type_
!= NULL
)
3435 Type_context
context(this->type_
, false);
3436 this->expr_
->determine_type(&context
);
3440 // A constant may have an abstract type.
3441 Type_context
context(NULL
, true);
3442 this->expr_
->determine_type(&context
);
3443 this->type_
= this->expr_
->type();
3444 gcc_assert(this->type_
!= NULL
);
3448 // Indicate that we found and reported an error for this constant.
3451 Named_constant::set_error()
3453 this->type_
= Type::make_error_type();
3454 this->expr_
= Expression::make_error(this->location_
);
3457 // Export a constant.
3460 Named_constant::export_const(Export
* exp
, const std::string
& name
) const
3462 exp
->write_c_string("const ");
3463 exp
->write_string(name
);
3464 exp
->write_c_string(" ");
3465 if (!this->type_
->is_abstract())
3467 exp
->write_type(this->type_
);
3468 exp
->write_c_string(" ");
3470 exp
->write_c_string("= ");
3471 this->expr()->export_expression(exp
);
3472 exp
->write_c_string(";\n");
3475 // Import a constant.
3478 Named_constant::import_const(Import
* imp
, std::string
* pname
, Type
** ptype
,
3481 imp
->require_c_string("const ");
3482 *pname
= imp
->read_identifier();
3483 imp
->require_c_string(" ");
3484 if (imp
->peek_char() == '=')
3488 *ptype
= imp
->read_type();
3489 imp
->require_c_string(" ");
3491 imp
->require_c_string("= ");
3492 *pexpr
= Expression::import_expression(imp
);
3493 imp
->require_c_string(";\n");
3499 Type_declaration::add_method(const std::string
& name
, Function
* function
)
3501 Named_object
* ret
= Named_object::make_function(name
, NULL
, function
);
3502 this->methods_
.push_back(ret
);
3506 // Add a method declaration.
3509 Type_declaration::add_method_declaration(const std::string
& name
,
3510 Function_type
* type
,
3511 source_location location
)
3513 Named_object
* ret
= Named_object::make_function_declaration(name
, NULL
, type
,
3515 this->methods_
.push_back(ret
);
3519 // Return whether any methods ere defined.
3522 Type_declaration::has_methods() const
3524 return !this->methods_
.empty();
3527 // Define methods for the real type.
3530 Type_declaration::define_methods(Named_type
* nt
)
3532 for (Methods::const_iterator p
= this->methods_
.begin();
3533 p
!= this->methods_
.end();
3535 nt
->add_existing_method(*p
);
3538 // We are using the type. Return true if we should issue a warning.
3541 Type_declaration::using_type()
3543 bool ret
= !this->issued_warning_
;
3544 this->issued_warning_
= true;
3548 // Class Unknown_name.
3550 // Set the real named object.
3553 Unknown_name::set_real_named_object(Named_object
* no
)
3555 gcc_assert(this->real_named_object_
== NULL
);
3556 gcc_assert(!no
->is_unknown());
3557 this->real_named_object_
= no
;
3560 // Class Named_object.
3562 Named_object::Named_object(const std::string
& name
,
3563 const Package
* package
,
3564 Classification classification
)
3565 : name_(name
), package_(package
), classification_(classification
),
3568 if (Gogo::is_sink_name(name
))
3569 gcc_assert(classification
== NAMED_OBJECT_SINK
);
3572 // Make an unknown name. This is used by the parser. The name must
3573 // be resolved later. Unknown names are only added in the current
3577 Named_object::make_unknown_name(const std::string
& name
,
3578 source_location location
)
3580 Named_object
* named_object
= new Named_object(name
, NULL
,
3581 NAMED_OBJECT_UNKNOWN
);
3582 Unknown_name
* value
= new Unknown_name(location
);
3583 named_object
->u_
.unknown_value
= value
;
3584 return named_object
;
3590 Named_object::make_constant(const Typed_identifier
& tid
,
3591 const Package
* package
, Expression
* expr
,
3594 Named_object
* named_object
= new Named_object(tid
.name(), package
,
3595 NAMED_OBJECT_CONST
);
3596 Named_constant
* named_constant
= new Named_constant(tid
.type(), expr
,
3599 named_object
->u_
.const_value
= named_constant
;
3600 return named_object
;
3603 // Make a named type.
3606 Named_object::make_type(const std::string
& name
, const Package
* package
,
3607 Type
* type
, source_location location
)
3609 Named_object
* named_object
= new Named_object(name
, package
,
3611 Named_type
* named_type
= Type::make_named_type(named_object
, type
, location
);
3612 named_object
->u_
.type_value
= named_type
;
3613 return named_object
;
3616 // Make a type declaration.
3619 Named_object::make_type_declaration(const std::string
& name
,
3620 const Package
* package
,
3621 source_location location
)
3623 Named_object
* named_object
= new Named_object(name
, package
,
3624 NAMED_OBJECT_TYPE_DECLARATION
);
3625 Type_declaration
* type_declaration
= new Type_declaration(location
);
3626 named_object
->u_
.type_declaration
= type_declaration
;
3627 return named_object
;
3633 Named_object::make_variable(const std::string
& name
, const Package
* package
,
3636 Named_object
* named_object
= new Named_object(name
, package
,
3638 named_object
->u_
.var_value
= variable
;
3639 return named_object
;
3642 // Make a result variable.
3645 Named_object::make_result_variable(const std::string
& name
,
3646 Result_variable
* result
)
3648 Named_object
* named_object
= new Named_object(name
, NULL
,
3649 NAMED_OBJECT_RESULT_VAR
);
3650 named_object
->u_
.result_var_value
= result
;
3651 return named_object
;
3654 // Make a sink. This is used for the special blank identifier _.
3657 Named_object::make_sink()
3659 return new Named_object("_", NULL
, NAMED_OBJECT_SINK
);
3662 // Make a named function.
3665 Named_object::make_function(const std::string
& name
, const Package
* package
,
3668 Named_object
* named_object
= new Named_object(name
, package
,
3670 named_object
->u_
.func_value
= function
;
3671 return named_object
;
3674 // Make a function declaration.
3677 Named_object::make_function_declaration(const std::string
& name
,
3678 const Package
* package
,
3679 Function_type
* fntype
,
3680 source_location location
)
3682 Named_object
* named_object
= new Named_object(name
, package
,
3683 NAMED_OBJECT_FUNC_DECLARATION
);
3684 Function_declaration
*func_decl
= new Function_declaration(fntype
, location
);
3685 named_object
->u_
.func_declaration_value
= func_decl
;
3686 return named_object
;
3692 Named_object::make_package(const std::string
& alias
, Package
* package
)
3694 Named_object
* named_object
= new Named_object(alias
, NULL
,
3695 NAMED_OBJECT_PACKAGE
);
3696 named_object
->u_
.package_value
= package
;
3697 return named_object
;
3700 // Return the name to use in an error message.
3703 Named_object::message_name() const
3705 if (this->package_
== NULL
)
3706 return Gogo::message_name(this->name_
);
3707 std::string ret
= Gogo::message_name(this->package_
->name());
3709 ret
+= Gogo::message_name(this->name_
);
3713 // Set the type when a declaration is defined.
3716 Named_object::set_type_value(Named_type
* named_type
)
3718 gcc_assert(this->classification_
== NAMED_OBJECT_TYPE_DECLARATION
);
3719 Type_declaration
* td
= this->u_
.type_declaration
;
3720 td
->define_methods(named_type
);
3721 Named_object
* in_function
= td
->in_function();
3722 if (in_function
!= NULL
)
3723 named_type
->set_in_function(in_function
);
3725 this->classification_
= NAMED_OBJECT_TYPE
;
3726 this->u_
.type_value
= named_type
;
3729 // Define a function which was previously declared.
3732 Named_object::set_function_value(Function
* function
)
3734 gcc_assert(this->classification_
== NAMED_OBJECT_FUNC_DECLARATION
);
3735 this->classification_
= NAMED_OBJECT_FUNC
;
3736 // FIXME: We should free the old value.
3737 this->u_
.func_value
= function
;
3740 // Declare an unknown object as a type declaration.
3743 Named_object::declare_as_type()
3745 gcc_assert(this->classification_
== NAMED_OBJECT_UNKNOWN
);
3746 Unknown_name
* unk
= this->u_
.unknown_value
;
3747 this->classification_
= NAMED_OBJECT_TYPE_DECLARATION
;
3748 this->u_
.type_declaration
= new Type_declaration(unk
->location());
3752 // Return the location of a named object.
3755 Named_object::location() const
3757 switch (this->classification_
)
3760 case NAMED_OBJECT_UNINITIALIZED
:
3763 case NAMED_OBJECT_UNKNOWN
:
3764 return this->unknown_value()->location();
3766 case NAMED_OBJECT_CONST
:
3767 return this->const_value()->location();
3769 case NAMED_OBJECT_TYPE
:
3770 return this->type_value()->location();
3772 case NAMED_OBJECT_TYPE_DECLARATION
:
3773 return this->type_declaration_value()->location();
3775 case NAMED_OBJECT_VAR
:
3776 return this->var_value()->location();
3778 case NAMED_OBJECT_RESULT_VAR
:
3779 return this->result_var_value()->function()->location();
3781 case NAMED_OBJECT_SINK
:
3784 case NAMED_OBJECT_FUNC
:
3785 return this->func_value()->location();
3787 case NAMED_OBJECT_FUNC_DECLARATION
:
3788 return this->func_declaration_value()->location();
3790 case NAMED_OBJECT_PACKAGE
:
3791 return this->package_value()->location();
3795 // Export a named object.
3798 Named_object::export_named_object(Export
* exp
) const
3800 switch (this->classification_
)
3803 case NAMED_OBJECT_UNINITIALIZED
:
3804 case NAMED_OBJECT_UNKNOWN
:
3807 case NAMED_OBJECT_CONST
:
3808 this->const_value()->export_const(exp
, this->name_
);
3811 case NAMED_OBJECT_TYPE
:
3812 this->type_value()->export_named_type(exp
, this->name_
);
3815 case NAMED_OBJECT_TYPE_DECLARATION
:
3816 error_at(this->type_declaration_value()->location(),
3817 "attempt to export %<%s%> which was declared but not defined",
3818 this->message_name().c_str());
3821 case NAMED_OBJECT_FUNC_DECLARATION
:
3822 this->func_declaration_value()->export_func(exp
, this->name_
);
3825 case NAMED_OBJECT_VAR
:
3826 this->var_value()->export_var(exp
, this->name_
);
3829 case NAMED_OBJECT_RESULT_VAR
:
3830 case NAMED_OBJECT_SINK
:
3833 case NAMED_OBJECT_FUNC
:
3834 this->func_value()->export_func(exp
, this->name_
);
3841 Bindings::Bindings(Bindings
* enclosing
)
3842 : enclosing_(enclosing
), named_objects_(), bindings_()
3849 Bindings::clear_file_scope()
3851 Contour::iterator p
= this->bindings_
.begin();
3852 while (p
!= this->bindings_
.end())
3855 if (p
->second
->package() != NULL
)
3857 else if (p
->second
->is_package())
3859 else if (p
->second
->is_function()
3860 && !p
->second
->func_value()->type()->is_method()
3861 && Gogo::unpack_hidden_name(p
->second
->name()) == "init")
3869 p
= this->bindings_
.erase(p
);
3873 // Look up a symbol.
3876 Bindings::lookup(const std::string
& name
) const
3878 Contour::const_iterator p
= this->bindings_
.find(name
);
3879 if (p
!= this->bindings_
.end())
3880 return p
->second
->resolve();
3881 else if (this->enclosing_
!= NULL
)
3882 return this->enclosing_
->lookup(name
);
3887 // Look up a symbol locally.
3890 Bindings::lookup_local(const std::string
& name
) const
3892 Contour::const_iterator p
= this->bindings_
.find(name
);
3893 if (p
== this->bindings_
.end())
3898 // Remove an object from a set of bindings. This is used for a
3899 // special case in thunks for functions which call recover.
3902 Bindings::remove_binding(Named_object
* no
)
3904 Contour::iterator pb
= this->bindings_
.find(no
->name());
3905 gcc_assert(pb
!= this->bindings_
.end());
3906 this->bindings_
.erase(pb
);
3907 for (std::vector
<Named_object
*>::iterator pn
= this->named_objects_
.begin();
3908 pn
!= this->named_objects_
.end();
3913 this->named_objects_
.erase(pn
);
3920 // Add a method to the list of objects. This is not added to the
3921 // lookup table. This is so that we have a single list of objects
3922 // declared at the top level, which we walk through when it's time to
3923 // convert to trees.
3926 Bindings::add_method(Named_object
* method
)
3928 this->named_objects_
.push_back(method
);
3931 // Add a generic Named_object to a Contour.
3934 Bindings::add_named_object_to_contour(Contour
* contour
,
3935 Named_object
* named_object
)
3937 gcc_assert(named_object
== named_object
->resolve());
3938 const std::string
& name(named_object
->name());
3939 gcc_assert(!Gogo::is_sink_name(name
));
3941 std::pair
<Contour::iterator
, bool> ins
=
3942 contour
->insert(std::make_pair(name
, named_object
));
3945 // The name was already there.
3946 if (named_object
->package() != NULL
3947 && ins
.first
->second
->package() == named_object
->package()
3948 && (ins
.first
->second
->classification()
3949 == named_object
->classification()))
3951 // This is a second import of the same object.
3952 return ins
.first
->second
;
3954 ins
.first
->second
= this->new_definition(ins
.first
->second
,
3956 return ins
.first
->second
;
3960 // Don't push declarations on the list. We push them on when
3961 // and if we find the definitions. That way we genericize the
3962 // functions in order.
3963 if (!named_object
->is_type_declaration()
3964 && !named_object
->is_function_declaration()
3965 && !named_object
->is_unknown())
3966 this->named_objects_
.push_back(named_object
);
3967 return named_object
;
3971 // We had an existing named object OLD_OBJECT, and we've seen a new
3972 // one NEW_OBJECT with the same name. FIXME: This does not free the
3973 // new object when we don't need it.
3976 Bindings::new_definition(Named_object
* old_object
, Named_object
* new_object
)
3979 switch (old_object
->classification())
3982 case Named_object::NAMED_OBJECT_UNINITIALIZED
:
3985 case Named_object::NAMED_OBJECT_UNKNOWN
:
3987 Named_object
* real
= old_object
->unknown_value()->real_named_object();
3989 return this->new_definition(real
, new_object
);
3990 gcc_assert(!new_object
->is_unknown());
3991 old_object
->unknown_value()->set_real_named_object(new_object
);
3992 if (!new_object
->is_type_declaration()
3993 && !new_object
->is_function_declaration())
3994 this->named_objects_
.push_back(new_object
);
3998 case Named_object::NAMED_OBJECT_CONST
:
4001 case Named_object::NAMED_OBJECT_TYPE
:
4002 if (new_object
->is_type_declaration())
4006 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
4007 if (new_object
->is_type_declaration())
4009 if (new_object
->is_type())
4011 old_object
->set_type_value(new_object
->type_value());
4012 new_object
->type_value()->set_named_object(old_object
);
4013 this->named_objects_
.push_back(old_object
);
4018 case Named_object::NAMED_OBJECT_VAR
:
4019 case Named_object::NAMED_OBJECT_RESULT_VAR
:
4022 case Named_object::NAMED_OBJECT_SINK
:
4025 case Named_object::NAMED_OBJECT_FUNC
:
4026 if (new_object
->is_function_declaration())
4028 if (!new_object
->func_declaration_value()->asm_name().empty())
4029 sorry("__asm__ for function definitions");
4030 Function_type
* old_type
= old_object
->func_value()->type();
4031 Function_type
* new_type
=
4032 new_object
->func_declaration_value()->type();
4033 if (old_type
->is_valid_redeclaration(new_type
, &reason
))
4038 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
4040 Function_type
* old_type
= old_object
->func_declaration_value()->type();
4041 if (new_object
->is_function_declaration())
4043 Function_type
* new_type
=
4044 new_object
->func_declaration_value()->type();
4045 if (old_type
->is_valid_redeclaration(new_type
, &reason
))
4048 if (new_object
->is_function())
4050 Function_type
* new_type
= new_object
->func_value()->type();
4051 if (old_type
->is_valid_redeclaration(new_type
, &reason
))
4053 if (!old_object
->func_declaration_value()->asm_name().empty())
4054 sorry("__asm__ for function definitions");
4055 old_object
->set_function_value(new_object
->func_value());
4056 this->named_objects_
.push_back(old_object
);
4063 case Named_object::NAMED_OBJECT_PACKAGE
:
4064 if (new_object
->is_package()
4065 && (old_object
->package_value()->name()
4066 == new_object
->package_value()->name()))
4072 std::string n
= old_object
->message_name();
4074 error_at(new_object
->location(), "redefinition of %qs", n
.c_str());
4076 error_at(new_object
->location(), "redefinition of %qs: %s", n
.c_str(),
4079 inform(old_object
->location(), "previous definition of %qs was here",
4085 // Add a named type.
4088 Bindings::add_named_type(Named_type
* named_type
)
4090 return this->add_named_object(named_type
->named_object());
4096 Bindings::add_function(const std::string
& name
, const Package
* package
,
4099 return this->add_named_object(Named_object::make_function(name
, package
,
4103 // Add a function declaration.
4106 Bindings::add_function_declaration(const std::string
& name
,
4107 const Package
* package
,
4108 Function_type
* type
,
4109 source_location location
)
4111 Named_object
* no
= Named_object::make_function_declaration(name
, package
,
4113 return this->add_named_object(no
);
4116 // Define a type which was previously declared.
4119 Bindings::define_type(Named_object
* no
, Named_type
* type
)
4121 no
->set_type_value(type
);
4122 this->named_objects_
.push_back(no
);
4125 // Traverse bindings.
4128 Bindings::traverse(Traverse
* traverse
, bool is_global
)
4130 unsigned int traverse_mask
= traverse
->traverse_mask();
4132 // We don't use an iterator because we permit the traversal to add
4133 // new global objects.
4134 for (size_t i
= 0; i
< this->named_objects_
.size(); ++i
)
4136 Named_object
* p
= this->named_objects_
[i
];
4137 switch (p
->classification())
4139 case Named_object::NAMED_OBJECT_CONST
:
4140 if ((traverse_mask
& Traverse::traverse_constants
) != 0)
4142 if (traverse
->constant(p
, is_global
) == TRAVERSE_EXIT
)
4143 return TRAVERSE_EXIT
;
4145 if ((traverse_mask
& Traverse::traverse_types
) != 0
4146 || (traverse_mask
& Traverse::traverse_expressions
) != 0)
4148 Type
* t
= p
->const_value()->type();
4150 && Type::traverse(t
, traverse
) == TRAVERSE_EXIT
)
4151 return TRAVERSE_EXIT
;
4153 if ((traverse_mask
& Traverse::traverse_expressions
) != 0)
4155 if (p
->const_value()->traverse_expression(traverse
)
4157 return TRAVERSE_EXIT
;
4161 case Named_object::NAMED_OBJECT_VAR
:
4162 case Named_object::NAMED_OBJECT_RESULT_VAR
:
4163 if ((traverse_mask
& Traverse::traverse_variables
) != 0)
4165 if (traverse
->variable(p
) == TRAVERSE_EXIT
)
4166 return TRAVERSE_EXIT
;
4168 if (((traverse_mask
& Traverse::traverse_types
) != 0
4169 || (traverse_mask
& Traverse::traverse_expressions
) != 0)
4170 && (p
->is_result_variable()
4171 || p
->var_value()->has_type()))
4173 Type
* t
= (p
->is_variable()
4174 ? p
->var_value()->type()
4175 : p
->result_var_value()->type());
4177 && Type::traverse(t
, traverse
) == TRAVERSE_EXIT
)
4178 return TRAVERSE_EXIT
;
4180 if (p
->is_variable()
4181 && (traverse_mask
& Traverse::traverse_expressions
) != 0)
4183 if (p
->var_value()->traverse_expression(traverse
)
4185 return TRAVERSE_EXIT
;
4189 case Named_object::NAMED_OBJECT_FUNC
:
4190 if ((traverse_mask
& Traverse::traverse_functions
) != 0)
4192 int t
= traverse
->function(p
);
4193 if (t
== TRAVERSE_EXIT
)
4194 return TRAVERSE_EXIT
;
4195 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
4200 & (Traverse::traverse_variables
4201 | Traverse::traverse_constants
4202 | Traverse::traverse_functions
4203 | Traverse::traverse_blocks
4204 | Traverse::traverse_statements
4205 | Traverse::traverse_expressions
4206 | Traverse::traverse_types
)) != 0)
4208 if (p
->func_value()->traverse(traverse
) == TRAVERSE_EXIT
)
4209 return TRAVERSE_EXIT
;
4213 case Named_object::NAMED_OBJECT_PACKAGE
:
4214 // These are traversed in Gogo::traverse.
4215 gcc_assert(is_global
);
4218 case Named_object::NAMED_OBJECT_TYPE
:
4219 if ((traverse_mask
& Traverse::traverse_types
) != 0
4220 || (traverse_mask
& Traverse::traverse_expressions
) != 0)
4222 if (Type::traverse(p
->type_value(), traverse
) == TRAVERSE_EXIT
)
4223 return TRAVERSE_EXIT
;
4227 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
4228 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
4229 case Named_object::NAMED_OBJECT_UNKNOWN
:
4232 case Named_object::NAMED_OBJECT_SINK
:
4238 return TRAVERSE_CONTINUE
;
4243 Package::Package(const std::string
& name
, const std::string
& unique_prefix
,
4244 source_location location
)
4245 : name_(name
), unique_prefix_(unique_prefix
), bindings_(new Bindings(NULL
)),
4246 priority_(0), location_(location
), used_(false), is_imported_(false),
4247 uses_sink_alias_(false)
4249 gcc_assert(!name
.empty() && !unique_prefix
.empty());
4252 // Set the priority. We may see multiple priorities for an imported
4253 // package; we want to use the largest one.
4256 Package::set_priority(int priority
)
4258 if (priority
> this->priority_
)
4259 this->priority_
= priority
;
4262 // Determine types of constants. Everything else in a package
4263 // (variables, function declarations) should already have a fixed
4264 // type. Constants may have abstract types.
4267 Package::determine_types()
4269 Bindings
* bindings
= this->bindings_
;
4270 for (Bindings::const_definitions_iterator p
= bindings
->begin_definitions();
4271 p
!= bindings
->end_definitions();
4274 if ((*p
)->is_const())
4275 (*p
)->const_value()->determine_type();
4283 Traverse::~Traverse()
4285 if (this->types_seen_
!= NULL
)
4286 delete this->types_seen_
;
4287 if (this->expressions_seen_
!= NULL
)
4288 delete this->expressions_seen_
;
4291 // Record that we are looking at a type, and return true if we have
4295 Traverse::remember_type(const Type
* type
)
4297 if (type
->is_error_type())
4299 gcc_assert((this->traverse_mask() & traverse_types
) != 0
4300 || (this->traverse_mask() & traverse_expressions
) != 0);
4301 // We only have to remember named types, as they are the only ones
4302 // we can see multiple times in a traversal.
4303 if (type
->classification() != Type::TYPE_NAMED
)
4305 if (this->types_seen_
== NULL
)
4306 this->types_seen_
= new Types_seen();
4307 std::pair
<Types_seen::iterator
, bool> ins
= this->types_seen_
->insert(type
);
4311 // Record that we are looking at an expression, and return true if we
4312 // have already seen it.
4315 Traverse::remember_expression(const Expression
* expression
)
4317 gcc_assert((this->traverse_mask() & traverse_types
) != 0
4318 || (this->traverse_mask() & traverse_expressions
) != 0);
4319 if (this->expressions_seen_
== NULL
)
4320 this->expressions_seen_
= new Expressions_seen();
4321 std::pair
<Expressions_seen::iterator
, bool> ins
=
4322 this->expressions_seen_
->insert(expression
);
4326 // The default versions of these functions should never be called: the
4327 // traversal mask indicates which functions may be called.
4330 Traverse::variable(Named_object
*)
4336 Traverse::constant(Named_object
*, bool)
4342 Traverse::function(Named_object
*)
4348 Traverse::block(Block
*)
4354 Traverse::statement(Block
*, size_t*, Statement
*)
4360 Traverse::expression(Expression
**)
4366 Traverse::type(Type
*)