compiler: Add -fgo-relative-import-path.
[gcc.git] / gcc / go / gofrontend / gogo.cc
1 // gogo.cc -- Go frontend parsed representation.
2
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.
6
7 #include "go-system.h"
8
9 #include "filenames.h"
10
11 #include "go-c.h"
12 #include "go-dump.h"
13 #include "lex.h"
14 #include "types.h"
15 #include "statements.h"
16 #include "expressions.h"
17 #include "dataflow.h"
18 #include "runtime.h"
19 #include "import.h"
20 #include "export.h"
21 #include "backend.h"
22 #include "gogo.h"
23
24 // Class Gogo.
25
26 Gogo::Gogo(Backend* backend, Linemap* linemap, int int_type_size,
27 int pointer_size)
28 : backend_(backend),
29 linemap_(linemap),
30 package_(NULL),
31 functions_(),
32 globals_(new Bindings(NULL)),
33 imports_(),
34 imported_unsafe_(false),
35 packages_(),
36 init_functions_(),
37 var_deps_(),
38 need_init_fn_(false),
39 init_fn_name_(),
40 imported_init_fns_(),
41 pkgpath_(),
42 pkgpath_symbol_(),
43 prefix_(),
44 pkgpath_set_(false),
45 pkgpath_from_option_(false),
46 prefix_from_option_(false),
47 relative_import_path_(),
48 verify_types_(),
49 interface_types_(),
50 specific_type_functions_(),
51 specific_type_functions_are_written_(false),
52 named_types_are_converted_(false)
53 {
54 const Location loc = Linemap::predeclared_location();
55
56 Named_type* uint8_type = Type::make_integer_type("uint8", true, 8,
57 RUNTIME_TYPE_KIND_UINT8);
58 this->add_named_type(uint8_type);
59 this->add_named_type(Type::make_integer_type("uint16", true, 16,
60 RUNTIME_TYPE_KIND_UINT16));
61 this->add_named_type(Type::make_integer_type("uint32", true, 32,
62 RUNTIME_TYPE_KIND_UINT32));
63 this->add_named_type(Type::make_integer_type("uint64", true, 64,
64 RUNTIME_TYPE_KIND_UINT64));
65
66 this->add_named_type(Type::make_integer_type("int8", false, 8,
67 RUNTIME_TYPE_KIND_INT8));
68 this->add_named_type(Type::make_integer_type("int16", false, 16,
69 RUNTIME_TYPE_KIND_INT16));
70 Named_type* int32_type = Type::make_integer_type("int32", false, 32,
71 RUNTIME_TYPE_KIND_INT32);
72 this->add_named_type(int32_type);
73 this->add_named_type(Type::make_integer_type("int64", false, 64,
74 RUNTIME_TYPE_KIND_INT64));
75
76 this->add_named_type(Type::make_float_type("float32", 32,
77 RUNTIME_TYPE_KIND_FLOAT32));
78 this->add_named_type(Type::make_float_type("float64", 64,
79 RUNTIME_TYPE_KIND_FLOAT64));
80
81 this->add_named_type(Type::make_complex_type("complex64", 64,
82 RUNTIME_TYPE_KIND_COMPLEX64));
83 this->add_named_type(Type::make_complex_type("complex128", 128,
84 RUNTIME_TYPE_KIND_COMPLEX128));
85
86 if (int_type_size < 32)
87 int_type_size = 32;
88 this->add_named_type(Type::make_integer_type("uint", true,
89 int_type_size,
90 RUNTIME_TYPE_KIND_UINT));
91 Named_type* int_type = Type::make_integer_type("int", false, int_type_size,
92 RUNTIME_TYPE_KIND_INT);
93 this->add_named_type(int_type);
94
95 this->add_named_type(Type::make_integer_type("uintptr", true,
96 pointer_size,
97 RUNTIME_TYPE_KIND_UINTPTR));
98
99 // "byte" is an alias for "uint8".
100 uint8_type->integer_type()->set_is_byte();
101 Named_object* byte_type = Named_object::make_type("byte", NULL, uint8_type,
102 loc);
103 this->add_named_type(byte_type->type_value());
104
105 // "rune" is an alias for "int32".
106 int32_type->integer_type()->set_is_rune();
107 Named_object* rune_type = Named_object::make_type("rune", NULL, int32_type,
108 loc);
109 this->add_named_type(rune_type->type_value());
110
111 this->add_named_type(Type::make_named_bool_type());
112
113 this->add_named_type(Type::make_named_string_type());
114
115 // "error" is interface { Error() string }.
116 {
117 Typed_identifier_list *methods = new Typed_identifier_list;
118 Typed_identifier_list *results = new Typed_identifier_list;
119 results->push_back(Typed_identifier("", Type::lookup_string_type(), loc));
120 Type *method_type = Type::make_function_type(NULL, NULL, results, loc);
121 methods->push_back(Typed_identifier("Error", method_type, loc));
122 Interface_type *error_iface = Type::make_interface_type(methods, loc);
123 error_iface->finalize_methods();
124 Named_type *error_type = Named_object::make_type("error", NULL, error_iface, loc)->type_value();
125 this->add_named_type(error_type);
126 }
127
128 this->globals_->add_constant(Typed_identifier("true",
129 Type::make_boolean_type(),
130 loc),
131 NULL,
132 Expression::make_boolean(true, loc),
133 0);
134 this->globals_->add_constant(Typed_identifier("false",
135 Type::make_boolean_type(),
136 loc),
137 NULL,
138 Expression::make_boolean(false, loc),
139 0);
140
141 this->globals_->add_constant(Typed_identifier("nil", Type::make_nil_type(),
142 loc),
143 NULL,
144 Expression::make_nil(loc),
145 0);
146
147 Type* abstract_int_type = Type::make_abstract_integer_type();
148 this->globals_->add_constant(Typed_identifier("iota", abstract_int_type,
149 loc),
150 NULL,
151 Expression::make_iota(),
152 0);
153
154 Function_type* new_type = Type::make_function_type(NULL, NULL, NULL, loc);
155 new_type->set_is_varargs();
156 new_type->set_is_builtin();
157 this->globals_->add_function_declaration("new", NULL, new_type, loc);
158
159 Function_type* make_type = Type::make_function_type(NULL, NULL, NULL, loc);
160 make_type->set_is_varargs();
161 make_type->set_is_builtin();
162 this->globals_->add_function_declaration("make", NULL, make_type, loc);
163
164 Typed_identifier_list* len_result = new Typed_identifier_list();
165 len_result->push_back(Typed_identifier("", int_type, loc));
166 Function_type* len_type = Type::make_function_type(NULL, NULL, len_result,
167 loc);
168 len_type->set_is_builtin();
169 this->globals_->add_function_declaration("len", NULL, len_type, loc);
170
171 Typed_identifier_list* cap_result = new Typed_identifier_list();
172 cap_result->push_back(Typed_identifier("", int_type, loc));
173 Function_type* cap_type = Type::make_function_type(NULL, NULL, len_result,
174 loc);
175 cap_type->set_is_builtin();
176 this->globals_->add_function_declaration("cap", NULL, cap_type, loc);
177
178 Function_type* print_type = Type::make_function_type(NULL, NULL, NULL, loc);
179 print_type->set_is_varargs();
180 print_type->set_is_builtin();
181 this->globals_->add_function_declaration("print", NULL, print_type, loc);
182
183 print_type = Type::make_function_type(NULL, NULL, NULL, loc);
184 print_type->set_is_varargs();
185 print_type->set_is_builtin();
186 this->globals_->add_function_declaration("println", NULL, print_type, loc);
187
188 Type *empty = Type::make_empty_interface_type(loc);
189 Typed_identifier_list* panic_parms = new Typed_identifier_list();
190 panic_parms->push_back(Typed_identifier("e", empty, loc));
191 Function_type *panic_type = Type::make_function_type(NULL, panic_parms,
192 NULL, loc);
193 panic_type->set_is_builtin();
194 this->globals_->add_function_declaration("panic", NULL, panic_type, loc);
195
196 Typed_identifier_list* recover_result = new Typed_identifier_list();
197 recover_result->push_back(Typed_identifier("", empty, loc));
198 Function_type* recover_type = Type::make_function_type(NULL, NULL,
199 recover_result,
200 loc);
201 recover_type->set_is_builtin();
202 this->globals_->add_function_declaration("recover", NULL, recover_type, loc);
203
204 Function_type* close_type = Type::make_function_type(NULL, NULL, NULL, loc);
205 close_type->set_is_varargs();
206 close_type->set_is_builtin();
207 this->globals_->add_function_declaration("close", NULL, close_type, loc);
208
209 Typed_identifier_list* copy_result = new Typed_identifier_list();
210 copy_result->push_back(Typed_identifier("", int_type, loc));
211 Function_type* copy_type = Type::make_function_type(NULL, NULL,
212 copy_result, loc);
213 copy_type->set_is_varargs();
214 copy_type->set_is_builtin();
215 this->globals_->add_function_declaration("copy", NULL, copy_type, loc);
216
217 Function_type* append_type = Type::make_function_type(NULL, NULL, NULL, loc);
218 append_type->set_is_varargs();
219 append_type->set_is_builtin();
220 this->globals_->add_function_declaration("append", NULL, append_type, loc);
221
222 Function_type* complex_type = Type::make_function_type(NULL, NULL, NULL, loc);
223 complex_type->set_is_varargs();
224 complex_type->set_is_builtin();
225 this->globals_->add_function_declaration("complex", NULL, complex_type, loc);
226
227 Function_type* real_type = Type::make_function_type(NULL, NULL, NULL, loc);
228 real_type->set_is_varargs();
229 real_type->set_is_builtin();
230 this->globals_->add_function_declaration("real", NULL, real_type, loc);
231
232 Function_type* imag_type = Type::make_function_type(NULL, NULL, NULL, loc);
233 imag_type->set_is_varargs();
234 imag_type->set_is_builtin();
235 this->globals_->add_function_declaration("imag", NULL, imag_type, loc);
236
237 Function_type* delete_type = Type::make_function_type(NULL, NULL, NULL, loc);
238 delete_type->set_is_varargs();
239 delete_type->set_is_builtin();
240 this->globals_->add_function_declaration("delete", NULL, delete_type, loc);
241 }
242
243 // Convert a pkgpath into a string suitable for a symbol. Note that
244 // this transformation is convenient but imperfect. A -fgo-pkgpath
245 // option of a/b_c will conflict with a -fgo-pkgpath option of a_b/c,
246 // possibly leading to link time errors.
247
248 std::string
249 Gogo::pkgpath_for_symbol(const std::string& pkgpath)
250 {
251 std::string s = pkgpath;
252 for (size_t i = 0; i < s.length(); ++i)
253 {
254 char c = s[i];
255 if ((c >= 'a' && c <= 'z')
256 || (c >= 'A' && c <= 'Z')
257 || (c >= '0' && c <= '9')
258 || c == '_'
259 || c == '.'
260 || c == '$')
261 ;
262 else
263 s[i] = '_';
264 }
265 return s;
266 }
267
268 // Get the package path to use for type reflection data. This should
269 // ideally be unique across the entire link.
270
271 const std::string&
272 Gogo::pkgpath() const
273 {
274 go_assert(this->pkgpath_set_);
275 return this->pkgpath_;
276 }
277
278 // Set the package path from the -fgo-pkgpath command line option.
279
280 void
281 Gogo::set_pkgpath(const std::string& arg)
282 {
283 go_assert(!this->pkgpath_set_);
284 this->pkgpath_ = arg;
285 this->pkgpath_set_ = true;
286 this->pkgpath_from_option_ = true;
287 }
288
289 // Get the package path to use for symbol names.
290
291 const std::string&
292 Gogo::pkgpath_symbol() const
293 {
294 go_assert(this->pkgpath_set_);
295 return this->pkgpath_symbol_;
296 }
297
298 // Set the unique prefix to use to determine the package path, from
299 // the -fgo-prefix command line option.
300
301 void
302 Gogo::set_prefix(const std::string& arg)
303 {
304 go_assert(!this->prefix_from_option_);
305 this->prefix_ = arg;
306 this->prefix_from_option_ = true;
307 }
308
309 // Munge name for use in an error message.
310
311 std::string
312 Gogo::message_name(const std::string& name)
313 {
314 return go_localize_identifier(Gogo::unpack_hidden_name(name).c_str());
315 }
316
317 // Get the package name.
318
319 const std::string&
320 Gogo::package_name() const
321 {
322 go_assert(this->package_ != NULL);
323 return this->package_->package_name();
324 }
325
326 // Set the package name.
327
328 void
329 Gogo::set_package_name(const std::string& package_name,
330 Location location)
331 {
332 if (this->package_ != NULL)
333 {
334 if (this->package_->package_name() != package_name)
335 error_at(location, "expected package %<%s%>",
336 Gogo::message_name(this->package_->package_name()).c_str());
337 return;
338 }
339
340 // Now that we know the name of the package we are compiling, set
341 // the package path to use for reflect.Type.PkgPath and global
342 // symbol names.
343 if (!this->pkgpath_set_)
344 {
345 if (!this->prefix_from_option_ && package_name == "main")
346 this->pkgpath_ = package_name;
347 else
348 {
349 if (!this->prefix_from_option_)
350 this->prefix_ = "go";
351 this->pkgpath_ = this->prefix_ + '.' + package_name;
352 }
353 this->pkgpath_set_ = true;
354 }
355
356 this->pkgpath_symbol_ = Gogo::pkgpath_for_symbol(this->pkgpath_);
357
358 this->package_ = this->register_package(this->pkgpath_, location);
359 this->package_->set_package_name(package_name, location);
360
361 if (this->is_main_package())
362 {
363 // Declare "main" as a function which takes no parameters and
364 // returns no value.
365 Location uloc = Linemap::unknown_location();
366 this->declare_function("main",
367 Type::make_function_type (NULL, NULL, NULL, uloc),
368 uloc);
369 }
370 }
371
372 // Return whether this is the "main" package. This is not true if
373 // -fgo-pkgpath or -fgo-prefix was used.
374
375 bool
376 Gogo::is_main_package() const
377 {
378 return (this->package_name() == "main"
379 && !this->pkgpath_from_option_
380 && !this->prefix_from_option_);
381 }
382
383 // Import a package.
384
385 void
386 Gogo::import_package(const std::string& filename,
387 const std::string& local_name,
388 bool is_local_name_exported,
389 Location location)
390 {
391 if (filename.empty())
392 {
393 error_at(location, "import path is empty");
394 return;
395 }
396
397 const char *pf = filename.data();
398 const char *pend = pf + filename.length();
399 while (pf < pend)
400 {
401 unsigned int c;
402 int adv = Lex::fetch_char(pf, &c);
403 if (adv == 0)
404 {
405 error_at(location, "import path contains invalid UTF-8 sequence");
406 return;
407 }
408 if (c == '\0')
409 {
410 error_at(location, "import path contains NUL");
411 return;
412 }
413 if (c < 0x20 || c == 0x7f)
414 {
415 error_at(location, "import path contains control character");
416 return;
417 }
418 if (c == '\\')
419 {
420 error_at(location, "import path contains backslash; use slash");
421 return;
422 }
423 if (Lex::is_unicode_space(c))
424 {
425 error_at(location, "import path contains space character");
426 return;
427 }
428 if (c < 0x7f && strchr("!\"#$%&'()*,:;<=>?[]^`{|}", c) != NULL)
429 {
430 error_at(location, "import path contains invalid character '%c'", c);
431 return;
432 }
433 pf += adv;
434 }
435
436 if (IS_ABSOLUTE_PATH(filename.c_str()))
437 {
438 error_at(location, "import path cannot be absolute path");
439 return;
440 }
441
442 if (filename == "unsafe")
443 {
444 this->import_unsafe(local_name, is_local_name_exported, location);
445 return;
446 }
447
448 Imports::const_iterator p = this->imports_.find(filename);
449 if (p != this->imports_.end())
450 {
451 Package* package = p->second;
452 package->set_location(location);
453 package->set_is_imported();
454 std::string ln = local_name;
455 bool is_ln_exported = is_local_name_exported;
456 if (ln.empty())
457 {
458 ln = package->package_name();
459 go_assert(!ln.empty());
460 is_ln_exported = Lex::is_exported_name(ln);
461 }
462 if (ln == ".")
463 {
464 Bindings* bindings = package->bindings();
465 for (Bindings::const_declarations_iterator p =
466 bindings->begin_declarations();
467 p != bindings->end_declarations();
468 ++p)
469 this->add_named_object(p->second);
470 }
471 else if (ln == "_")
472 package->set_uses_sink_alias();
473 else
474 {
475 ln = this->pack_hidden_name(ln, is_ln_exported);
476 this->package_->bindings()->add_package(ln, package);
477 }
478 return;
479 }
480
481 Import::Stream* stream = Import::open_package(filename, location,
482 this->relative_import_path_);
483 if (stream == NULL)
484 {
485 error_at(location, "import file %qs not found", filename.c_str());
486 return;
487 }
488
489 Import imp(stream, location);
490 imp.register_builtin_types(this);
491 Package* package = imp.import(this, local_name, is_local_name_exported);
492 if (package != NULL)
493 {
494 if (package->pkgpath() == this->pkgpath())
495 error_at(location,
496 ("imported package uses same package path as package "
497 "being compiled (see -fgo-pkgpath option)"));
498
499 this->imports_.insert(std::make_pair(filename, package));
500 package->set_is_imported();
501 }
502
503 delete stream;
504 }
505
506 // Add an import control function for an imported package to the list.
507
508 void
509 Gogo::add_import_init_fn(const std::string& package_name,
510 const std::string& init_name, int prio)
511 {
512 for (std::set<Import_init>::const_iterator p =
513 this->imported_init_fns_.begin();
514 p != this->imported_init_fns_.end();
515 ++p)
516 {
517 if (p->init_name() == init_name
518 && (p->package_name() != package_name || p->priority() != prio))
519 {
520 error("duplicate package initialization name %qs",
521 Gogo::message_name(init_name).c_str());
522 inform(UNKNOWN_LOCATION, "used by package %qs at priority %d",
523 Gogo::message_name(p->package_name()).c_str(),
524 p->priority());
525 inform(UNKNOWN_LOCATION, " and by package %qs at priority %d",
526 Gogo::message_name(package_name).c_str(), prio);
527 return;
528 }
529 }
530
531 this->imported_init_fns_.insert(Import_init(package_name, init_name,
532 prio));
533 }
534
535 // Return whether we are at the global binding level.
536
537 bool
538 Gogo::in_global_scope() const
539 {
540 return this->functions_.empty();
541 }
542
543 // Return the current binding contour.
544
545 Bindings*
546 Gogo::current_bindings()
547 {
548 if (!this->functions_.empty())
549 return this->functions_.back().blocks.back()->bindings();
550 else if (this->package_ != NULL)
551 return this->package_->bindings();
552 else
553 return this->globals_;
554 }
555
556 const Bindings*
557 Gogo::current_bindings() const
558 {
559 if (!this->functions_.empty())
560 return this->functions_.back().blocks.back()->bindings();
561 else if (this->package_ != NULL)
562 return this->package_->bindings();
563 else
564 return this->globals_;
565 }
566
567 // Return the current block.
568
569 Block*
570 Gogo::current_block()
571 {
572 if (this->functions_.empty())
573 return NULL;
574 else
575 return this->functions_.back().blocks.back();
576 }
577
578 // Look up a name in the current binding contour. If PFUNCTION is not
579 // NULL, set it to the function in which the name is defined, or NULL
580 // if the name is defined in global scope.
581
582 Named_object*
583 Gogo::lookup(const std::string& name, Named_object** pfunction) const
584 {
585 if (pfunction != NULL)
586 *pfunction = NULL;
587
588 if (Gogo::is_sink_name(name))
589 return Named_object::make_sink();
590
591 for (Open_functions::const_reverse_iterator p = this->functions_.rbegin();
592 p != this->functions_.rend();
593 ++p)
594 {
595 Named_object* ret = p->blocks.back()->bindings()->lookup(name);
596 if (ret != NULL)
597 {
598 if (pfunction != NULL)
599 *pfunction = p->function;
600 return ret;
601 }
602 }
603
604 if (this->package_ != NULL)
605 {
606 Named_object* ret = this->package_->bindings()->lookup(name);
607 if (ret != NULL)
608 {
609 if (ret->package() != NULL)
610 ret->package()->set_used();
611 return ret;
612 }
613 }
614
615 // We do not look in the global namespace. If we did, the global
616 // namespace would effectively hide names which were defined in
617 // package scope which we have not yet seen. Instead,
618 // define_global_names is called after parsing is over to connect
619 // undefined names at package scope with names defined at global
620 // scope.
621
622 return NULL;
623 }
624
625 // Look up a name in the current block, without searching enclosing
626 // blocks.
627
628 Named_object*
629 Gogo::lookup_in_block(const std::string& name) const
630 {
631 go_assert(!this->functions_.empty());
632 go_assert(!this->functions_.back().blocks.empty());
633 return this->functions_.back().blocks.back()->bindings()->lookup_local(name);
634 }
635
636 // Look up a name in the global namespace.
637
638 Named_object*
639 Gogo::lookup_global(const char* name) const
640 {
641 return this->globals_->lookup(name);
642 }
643
644 // Add an imported package.
645
646 Package*
647 Gogo::add_imported_package(const std::string& real_name,
648 const std::string& alias_arg,
649 bool is_alias_exported,
650 const std::string& pkgpath,
651 Location location,
652 bool* padd_to_globals)
653 {
654 Package* ret = this->register_package(pkgpath, location);
655 ret->set_package_name(real_name, location);
656
657 *padd_to_globals = false;
658
659 if (alias_arg == ".")
660 *padd_to_globals = true;
661 else if (alias_arg == "_")
662 ret->set_uses_sink_alias();
663 else
664 {
665 std::string alias = alias_arg;
666 if (alias.empty())
667 {
668 alias = real_name;
669 is_alias_exported = Lex::is_exported_name(alias);
670 }
671 alias = this->pack_hidden_name(alias, is_alias_exported);
672 Named_object* no = this->package_->bindings()->add_package(alias, ret);
673 if (!no->is_package())
674 return NULL;
675 }
676
677 return ret;
678 }
679
680 // Register a package. This package may or may not be imported. This
681 // returns the Package structure for the package, creating if it
682 // necessary. LOCATION is the location of the import statement that
683 // led us to see this package.
684
685 Package*
686 Gogo::register_package(const std::string& pkgpath, Location location)
687 {
688 Package* package = NULL;
689 std::pair<Packages::iterator, bool> ins =
690 this->packages_.insert(std::make_pair(pkgpath, package));
691 if (!ins.second)
692 {
693 // We have seen this package name before.
694 package = ins.first->second;
695 go_assert(package != NULL && package->pkgpath() == pkgpath);
696 if (Linemap::is_unknown_location(package->location()))
697 package->set_location(location);
698 }
699 else
700 {
701 // First time we have seen this package name.
702 package = new Package(pkgpath, location);
703 go_assert(ins.first->second == NULL);
704 ins.first->second = package;
705 }
706
707 return package;
708 }
709
710 // Start compiling a function.
711
712 Named_object*
713 Gogo::start_function(const std::string& name, Function_type* type,
714 bool add_method_to_type, Location location)
715 {
716 bool at_top_level = this->functions_.empty();
717
718 Block* block = new Block(NULL, location);
719
720 Function* enclosing = (at_top_level
721 ? NULL
722 : this->functions_.back().function->func_value());
723
724 Function* function = new Function(type, enclosing, block, location);
725
726 if (type->is_method())
727 {
728 const Typed_identifier* receiver = type->receiver();
729 Variable* this_param = new Variable(receiver->type(), NULL, false,
730 true, true, location);
731 std::string rname = receiver->name();
732 if (rname.empty() || Gogo::is_sink_name(rname))
733 {
734 // We need to give receivers a name since they wind up in
735 // DECL_ARGUMENTS. FIXME.
736 static unsigned int count;
737 char buf[50];
738 snprintf(buf, sizeof buf, "r.%u", count);
739 ++count;
740 rname = buf;
741 }
742 block->bindings()->add_variable(rname, NULL, this_param);
743 }
744
745 const Typed_identifier_list* parameters = type->parameters();
746 bool is_varargs = type->is_varargs();
747 if (parameters != NULL)
748 {
749 for (Typed_identifier_list::const_iterator p = parameters->begin();
750 p != parameters->end();
751 ++p)
752 {
753 Variable* param = new Variable(p->type(), NULL, false, true, false,
754 location);
755 if (is_varargs && p + 1 == parameters->end())
756 param->set_is_varargs_parameter();
757
758 std::string pname = p->name();
759 if (pname.empty() || Gogo::is_sink_name(pname))
760 {
761 // We need to give parameters a name since they wind up
762 // in DECL_ARGUMENTS. FIXME.
763 static unsigned int count;
764 char buf[50];
765 snprintf(buf, sizeof buf, "p.%u", count);
766 ++count;
767 pname = buf;
768 }
769 block->bindings()->add_variable(pname, NULL, param);
770 }
771 }
772
773 function->create_result_variables(this);
774
775 const std::string* pname;
776 std::string nested_name;
777 bool is_init = false;
778 if (Gogo::unpack_hidden_name(name) == "init" && !type->is_method())
779 {
780 if ((type->parameters() != NULL && !type->parameters()->empty())
781 || (type->results() != NULL && !type->results()->empty()))
782 error_at(location,
783 "func init must have no arguments and no return values");
784 // There can be multiple "init" functions, so give them each a
785 // different name.
786 static int init_count;
787 char buf[30];
788 snprintf(buf, sizeof buf, ".$init%d", init_count);
789 ++init_count;
790 nested_name = buf;
791 pname = &nested_name;
792 is_init = true;
793 }
794 else if (!name.empty())
795 pname = &name;
796 else
797 {
798 // Invent a name for a nested function.
799 static int nested_count;
800 char buf[30];
801 snprintf(buf, sizeof buf, ".$nested%d", nested_count);
802 ++nested_count;
803 nested_name = buf;
804 pname = &nested_name;
805 }
806
807 Named_object* ret;
808 if (Gogo::is_sink_name(*pname))
809 {
810 static int sink_count;
811 char buf[30];
812 snprintf(buf, sizeof buf, ".$sink%d", sink_count);
813 ++sink_count;
814 ret = Named_object::make_function(buf, NULL, function);
815 }
816 else if (!type->is_method())
817 {
818 ret = this->package_->bindings()->add_function(*pname, NULL, function);
819 if (!ret->is_function() || ret->func_value() != function)
820 {
821 // Redefinition error. Invent a name to avoid knockon
822 // errors.
823 static int redefinition_count;
824 char buf[30];
825 snprintf(buf, sizeof buf, ".$redefined%d", redefinition_count);
826 ++redefinition_count;
827 ret = this->package_->bindings()->add_function(buf, NULL, function);
828 }
829 }
830 else
831 {
832 if (!add_method_to_type)
833 ret = Named_object::make_function(name, NULL, function);
834 else
835 {
836 go_assert(at_top_level);
837 Type* rtype = type->receiver()->type();
838
839 // We want to look through the pointer created by the
840 // parser, without getting an error if the type is not yet
841 // defined.
842 if (rtype->classification() == Type::TYPE_POINTER)
843 rtype = rtype->points_to();
844
845 if (rtype->is_error_type())
846 ret = Named_object::make_function(name, NULL, function);
847 else if (rtype->named_type() != NULL)
848 {
849 ret = rtype->named_type()->add_method(name, function);
850 if (!ret->is_function())
851 {
852 // Redefinition error.
853 ret = Named_object::make_function(name, NULL, function);
854 }
855 }
856 else if (rtype->forward_declaration_type() != NULL)
857 {
858 Named_object* type_no =
859 rtype->forward_declaration_type()->named_object();
860 if (type_no->is_unknown())
861 {
862 // If we are seeing methods it really must be a
863 // type. Declare it as such. An alternative would
864 // be to support lists of methods for unknown
865 // expressions. Either way the error messages if
866 // this is not a type are going to get confusing.
867 Named_object* declared =
868 this->declare_package_type(type_no->name(),
869 type_no->location());
870 go_assert(declared
871 == type_no->unknown_value()->real_named_object());
872 }
873 ret = rtype->forward_declaration_type()->add_method(name,
874 function);
875 }
876 else
877 go_unreachable();
878 }
879 this->package_->bindings()->add_method(ret);
880 }
881
882 this->functions_.resize(this->functions_.size() + 1);
883 Open_function& of(this->functions_.back());
884 of.function = ret;
885 of.blocks.push_back(block);
886
887 if (is_init)
888 {
889 this->init_functions_.push_back(ret);
890 this->need_init_fn_ = true;
891 }
892
893 return ret;
894 }
895
896 // Finish compiling a function.
897
898 void
899 Gogo::finish_function(Location location)
900 {
901 this->finish_block(location);
902 go_assert(this->functions_.back().blocks.empty());
903 this->functions_.pop_back();
904 }
905
906 // Return the current function.
907
908 Named_object*
909 Gogo::current_function() const
910 {
911 go_assert(!this->functions_.empty());
912 return this->functions_.back().function;
913 }
914
915 // Start a new block.
916
917 void
918 Gogo::start_block(Location location)
919 {
920 go_assert(!this->functions_.empty());
921 Block* block = new Block(this->current_block(), location);
922 this->functions_.back().blocks.push_back(block);
923 }
924
925 // Finish a block.
926
927 Block*
928 Gogo::finish_block(Location location)
929 {
930 go_assert(!this->functions_.empty());
931 go_assert(!this->functions_.back().blocks.empty());
932 Block* block = this->functions_.back().blocks.back();
933 this->functions_.back().blocks.pop_back();
934 block->set_end_location(location);
935 return block;
936 }
937
938 // Add an erroneous name.
939
940 Named_object*
941 Gogo::add_erroneous_name(const std::string& name)
942 {
943 return this->package_->bindings()->add_erroneous_name(name);
944 }
945
946 // Add an unknown name.
947
948 Named_object*
949 Gogo::add_unknown_name(const std::string& name, Location location)
950 {
951 return this->package_->bindings()->add_unknown_name(name, location);
952 }
953
954 // Declare a function.
955
956 Named_object*
957 Gogo::declare_function(const std::string& name, Function_type* type,
958 Location location)
959 {
960 if (!type->is_method())
961 return this->current_bindings()->add_function_declaration(name, NULL, type,
962 location);
963 else
964 {
965 // We don't bother to add this to the list of global
966 // declarations.
967 Type* rtype = type->receiver()->type();
968
969 // We want to look through the pointer created by the
970 // parser, without getting an error if the type is not yet
971 // defined.
972 if (rtype->classification() == Type::TYPE_POINTER)
973 rtype = rtype->points_to();
974
975 if (rtype->is_error_type())
976 return NULL;
977 else if (rtype->named_type() != NULL)
978 return rtype->named_type()->add_method_declaration(name, NULL, type,
979 location);
980 else if (rtype->forward_declaration_type() != NULL)
981 {
982 Forward_declaration_type* ftype = rtype->forward_declaration_type();
983 return ftype->add_method_declaration(name, NULL, type, location);
984 }
985 else
986 go_unreachable();
987 }
988 }
989
990 // Add a label definition.
991
992 Label*
993 Gogo::add_label_definition(const std::string& label_name,
994 Location location)
995 {
996 go_assert(!this->functions_.empty());
997 Function* func = this->functions_.back().function->func_value();
998 Label* label = func->add_label_definition(this, label_name, location);
999 this->add_statement(Statement::make_label_statement(label, location));
1000 return label;
1001 }
1002
1003 // Add a label reference.
1004
1005 Label*
1006 Gogo::add_label_reference(const std::string& label_name,
1007 Location location, bool issue_goto_errors)
1008 {
1009 go_assert(!this->functions_.empty());
1010 Function* func = this->functions_.back().function->func_value();
1011 return func->add_label_reference(this, label_name, location,
1012 issue_goto_errors);
1013 }
1014
1015 // Return the current binding state.
1016
1017 Bindings_snapshot*
1018 Gogo::bindings_snapshot(Location location)
1019 {
1020 return new Bindings_snapshot(this->current_block(), location);
1021 }
1022
1023 // Add a statement.
1024
1025 void
1026 Gogo::add_statement(Statement* statement)
1027 {
1028 go_assert(!this->functions_.empty()
1029 && !this->functions_.back().blocks.empty());
1030 this->functions_.back().blocks.back()->add_statement(statement);
1031 }
1032
1033 // Add a block.
1034
1035 void
1036 Gogo::add_block(Block* block, Location location)
1037 {
1038 go_assert(!this->functions_.empty()
1039 && !this->functions_.back().blocks.empty());
1040 Statement* statement = Statement::make_block_statement(block, location);
1041 this->functions_.back().blocks.back()->add_statement(statement);
1042 }
1043
1044 // Add a constant.
1045
1046 Named_object*
1047 Gogo::add_constant(const Typed_identifier& tid, Expression* expr,
1048 int iota_value)
1049 {
1050 return this->current_bindings()->add_constant(tid, NULL, expr, iota_value);
1051 }
1052
1053 // Add a type.
1054
1055 void
1056 Gogo::add_type(const std::string& name, Type* type, Location location)
1057 {
1058 Named_object* no = this->current_bindings()->add_type(name, NULL, type,
1059 location);
1060 if (!this->in_global_scope() && no->is_type())
1061 {
1062 Named_object* f = this->functions_.back().function;
1063 unsigned int index;
1064 if (f->is_function())
1065 index = f->func_value()->new_local_type_index();
1066 else
1067 index = 0;
1068 no->type_value()->set_in_function(f, index);
1069 }
1070 }
1071
1072 // Add a named type.
1073
1074 void
1075 Gogo::add_named_type(Named_type* type)
1076 {
1077 go_assert(this->in_global_scope());
1078 this->current_bindings()->add_named_type(type);
1079 }
1080
1081 // Declare a type.
1082
1083 Named_object*
1084 Gogo::declare_type(const std::string& name, Location location)
1085 {
1086 Bindings* bindings = this->current_bindings();
1087 Named_object* no = bindings->add_type_declaration(name, NULL, location);
1088 if (!this->in_global_scope() && no->is_type_declaration())
1089 {
1090 Named_object* f = this->functions_.back().function;
1091 unsigned int index;
1092 if (f->is_function())
1093 index = f->func_value()->new_local_type_index();
1094 else
1095 index = 0;
1096 no->type_declaration_value()->set_in_function(f, index);
1097 }
1098 return no;
1099 }
1100
1101 // Declare a type at the package level.
1102
1103 Named_object*
1104 Gogo::declare_package_type(const std::string& name, Location location)
1105 {
1106 return this->package_->bindings()->add_type_declaration(name, NULL, location);
1107 }
1108
1109 // Declare a function at the package level.
1110
1111 Named_object*
1112 Gogo::declare_package_function(const std::string& name, Function_type* type,
1113 Location location)
1114 {
1115 return this->package_->bindings()->add_function_declaration(name, NULL, type,
1116 location);
1117 }
1118
1119 // Define a type which was already declared.
1120
1121 void
1122 Gogo::define_type(Named_object* no, Named_type* type)
1123 {
1124 this->current_bindings()->define_type(no, type);
1125 }
1126
1127 // Add a variable.
1128
1129 Named_object*
1130 Gogo::add_variable(const std::string& name, Variable* variable)
1131 {
1132 Named_object* no = this->current_bindings()->add_variable(name, NULL,
1133 variable);
1134
1135 // In a function the middle-end wants to see a DECL_EXPR node.
1136 if (no != NULL
1137 && no->is_variable()
1138 && !no->var_value()->is_parameter()
1139 && !this->functions_.empty())
1140 this->add_statement(Statement::make_variable_declaration(no));
1141
1142 return no;
1143 }
1144
1145 // Add a sink--a reference to the blank identifier _.
1146
1147 Named_object*
1148 Gogo::add_sink()
1149 {
1150 return Named_object::make_sink();
1151 }
1152
1153 // Add a named object.
1154
1155 void
1156 Gogo::add_named_object(Named_object* no)
1157 {
1158 this->current_bindings()->add_named_object(no);
1159 }
1160
1161 // Mark all local variables used. This is used when some types of
1162 // parse error occur.
1163
1164 void
1165 Gogo::mark_locals_used()
1166 {
1167 for (Open_functions::iterator pf = this->functions_.begin();
1168 pf != this->functions_.end();
1169 ++pf)
1170 {
1171 for (std::vector<Block*>::iterator pb = pf->blocks.begin();
1172 pb != pf->blocks.end();
1173 ++pb)
1174 (*pb)->bindings()->mark_locals_used();
1175 }
1176 }
1177
1178 // Record that we've seen an interface type.
1179
1180 void
1181 Gogo::record_interface_type(Interface_type* itype)
1182 {
1183 this->interface_types_.push_back(itype);
1184 }
1185
1186 // Return a name for a thunk object.
1187
1188 std::string
1189 Gogo::thunk_name()
1190 {
1191 static int thunk_count;
1192 char thunk_name[50];
1193 snprintf(thunk_name, sizeof thunk_name, "$thunk%d", thunk_count);
1194 ++thunk_count;
1195 return thunk_name;
1196 }
1197
1198 // Return whether a function is a thunk.
1199
1200 bool
1201 Gogo::is_thunk(const Named_object* no)
1202 {
1203 return no->name().compare(0, 6, "$thunk") == 0;
1204 }
1205
1206 // Define the global names. We do this only after parsing all the
1207 // input files, because the program might define the global names
1208 // itself.
1209
1210 void
1211 Gogo::define_global_names()
1212 {
1213 for (Bindings::const_declarations_iterator p =
1214 this->globals_->begin_declarations();
1215 p != this->globals_->end_declarations();
1216 ++p)
1217 {
1218 Named_object* global_no = p->second;
1219 std::string name(Gogo::pack_hidden_name(global_no->name(), false));
1220 Named_object* no = this->package_->bindings()->lookup(name);
1221 if (no == NULL)
1222 continue;
1223 no = no->resolve();
1224 if (no->is_type_declaration())
1225 {
1226 if (global_no->is_type())
1227 {
1228 if (no->type_declaration_value()->has_methods())
1229 error_at(no->location(),
1230 "may not define methods for global type");
1231 no->set_type_value(global_no->type_value());
1232 }
1233 else
1234 {
1235 error_at(no->location(), "expected type");
1236 Type* errtype = Type::make_error_type();
1237 Named_object* err =
1238 Named_object::make_type("erroneous_type", NULL, errtype,
1239 Linemap::predeclared_location());
1240 no->set_type_value(err->type_value());
1241 }
1242 }
1243 else if (no->is_unknown())
1244 no->unknown_value()->set_real_named_object(global_no);
1245 }
1246 }
1247
1248 // Clear out names in file scope.
1249
1250 void
1251 Gogo::clear_file_scope()
1252 {
1253 this->package_->bindings()->clear_file_scope();
1254
1255 // Warn about packages which were imported but not used.
1256 bool quiet = saw_errors();
1257 for (Packages::iterator p = this->packages_.begin();
1258 p != this->packages_.end();
1259 ++p)
1260 {
1261 Package* package = p->second;
1262 if (package != this->package_
1263 && package->is_imported()
1264 && !package->used()
1265 && !package->uses_sink_alias()
1266 && !quiet)
1267 error_at(package->location(), "imported and not used: %s",
1268 Gogo::message_name(package->package_name()).c_str());
1269 package->clear_is_imported();
1270 package->clear_uses_sink_alias();
1271 package->clear_used();
1272 }
1273 }
1274
1275 // Queue up a type specific function for later writing. These are
1276 // written out in write_specific_type_functions, called after the
1277 // parse tree is lowered.
1278
1279 void
1280 Gogo::queue_specific_type_function(Type* type, Named_type* name,
1281 const std::string& hash_name,
1282 Function_type* hash_fntype,
1283 const std::string& equal_name,
1284 Function_type* equal_fntype)
1285 {
1286 go_assert(!this->specific_type_functions_are_written_);
1287 go_assert(!this->in_global_scope());
1288 Specific_type_function* tsf = new Specific_type_function(type, name,
1289 hash_name,
1290 hash_fntype,
1291 equal_name,
1292 equal_fntype);
1293 this->specific_type_functions_.push_back(tsf);
1294 }
1295
1296 // Look for types which need specific hash or equality functions.
1297
1298 class Specific_type_functions : public Traverse
1299 {
1300 public:
1301 Specific_type_functions(Gogo* gogo)
1302 : Traverse(traverse_types),
1303 gogo_(gogo)
1304 { }
1305
1306 int
1307 type(Type*);
1308
1309 private:
1310 Gogo* gogo_;
1311 };
1312
1313 int
1314 Specific_type_functions::type(Type* t)
1315 {
1316 Named_object* hash_fn;
1317 Named_object* equal_fn;
1318 switch (t->classification())
1319 {
1320 case Type::TYPE_NAMED:
1321 {
1322 Named_type* nt = t->named_type();
1323 if (!t->compare_is_identity(this->gogo_) && t->is_comparable())
1324 t->type_functions(this->gogo_, nt, NULL, NULL, &hash_fn, &equal_fn);
1325
1326 // If this is a struct type, we don't want to make functions
1327 // for the unnamed struct.
1328 Type* rt = nt->real_type();
1329 if (rt->struct_type() == NULL)
1330 {
1331 if (Type::traverse(rt, this) == TRAVERSE_EXIT)
1332 return TRAVERSE_EXIT;
1333 }
1334 else
1335 {
1336 // If this type is defined in another package, then we don't
1337 // need to worry about the unexported fields.
1338 bool is_defined_elsewhere = nt->named_object()->package() != NULL;
1339 const Struct_field_list* fields = rt->struct_type()->fields();
1340 for (Struct_field_list::const_iterator p = fields->begin();
1341 p != fields->end();
1342 ++p)
1343 {
1344 if (is_defined_elsewhere
1345 && Gogo::is_hidden_name(p->field_name()))
1346 continue;
1347 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
1348 return TRAVERSE_EXIT;
1349 }
1350 }
1351
1352 return TRAVERSE_SKIP_COMPONENTS;
1353 }
1354
1355 case Type::TYPE_STRUCT:
1356 case Type::TYPE_ARRAY:
1357 if (!t->compare_is_identity(this->gogo_) && t->is_comparable())
1358 t->type_functions(this->gogo_, NULL, NULL, NULL, &hash_fn, &equal_fn);
1359 break;
1360
1361 default:
1362 break;
1363 }
1364
1365 return TRAVERSE_CONTINUE;
1366 }
1367
1368 // Write out type specific functions.
1369
1370 void
1371 Gogo::write_specific_type_functions()
1372 {
1373 Specific_type_functions stf(this);
1374 this->traverse(&stf);
1375
1376 while (!this->specific_type_functions_.empty())
1377 {
1378 Specific_type_function* tsf = this->specific_type_functions_.back();
1379 this->specific_type_functions_.pop_back();
1380 tsf->type->write_specific_type_functions(this, tsf->name,
1381 tsf->hash_name,
1382 tsf->hash_fntype,
1383 tsf->equal_name,
1384 tsf->equal_fntype);
1385 delete tsf;
1386 }
1387 this->specific_type_functions_are_written_ = true;
1388 }
1389
1390 // Traverse the tree.
1391
1392 void
1393 Gogo::traverse(Traverse* traverse)
1394 {
1395 // Traverse the current package first for consistency. The other
1396 // packages will only contain imported types, constants, and
1397 // declarations.
1398 if (this->package_->bindings()->traverse(traverse, true) == TRAVERSE_EXIT)
1399 return;
1400 for (Packages::const_iterator p = this->packages_.begin();
1401 p != this->packages_.end();
1402 ++p)
1403 {
1404 if (p->second != this->package_)
1405 {
1406 if (p->second->bindings()->traverse(traverse, true) == TRAVERSE_EXIT)
1407 break;
1408 }
1409 }
1410 }
1411
1412 // Add a type to verify. This is used for types of sink variables, in
1413 // order to give appropriate error messages.
1414
1415 void
1416 Gogo::add_type_to_verify(Type* type)
1417 {
1418 this->verify_types_.push_back(type);
1419 }
1420
1421 // Traversal class used to verify types.
1422
1423 class Verify_types : public Traverse
1424 {
1425 public:
1426 Verify_types()
1427 : Traverse(traverse_types)
1428 { }
1429
1430 int
1431 type(Type*);
1432 };
1433
1434 // Verify that a type is correct.
1435
1436 int
1437 Verify_types::type(Type* t)
1438 {
1439 if (!t->verify())
1440 return TRAVERSE_SKIP_COMPONENTS;
1441 return TRAVERSE_CONTINUE;
1442 }
1443
1444 // Verify that all types are correct.
1445
1446 void
1447 Gogo::verify_types()
1448 {
1449 Verify_types traverse;
1450 this->traverse(&traverse);
1451
1452 for (std::vector<Type*>::iterator p = this->verify_types_.begin();
1453 p != this->verify_types_.end();
1454 ++p)
1455 (*p)->verify();
1456 this->verify_types_.clear();
1457 }
1458
1459 // Traversal class used to lower parse tree.
1460
1461 class Lower_parse_tree : public Traverse
1462 {
1463 public:
1464 Lower_parse_tree(Gogo* gogo, Named_object* function)
1465 : Traverse(traverse_variables
1466 | traverse_constants
1467 | traverse_functions
1468 | traverse_statements
1469 | traverse_expressions),
1470 gogo_(gogo), function_(function), iota_value_(-1), inserter_()
1471 { }
1472
1473 void
1474 set_inserter(const Statement_inserter* inserter)
1475 { this->inserter_ = *inserter; }
1476
1477 int
1478 variable(Named_object*);
1479
1480 int
1481 constant(Named_object*, bool);
1482
1483 int
1484 function(Named_object*);
1485
1486 int
1487 statement(Block*, size_t* pindex, Statement*);
1488
1489 int
1490 expression(Expression**);
1491
1492 private:
1493 // General IR.
1494 Gogo* gogo_;
1495 // The function we are traversing.
1496 Named_object* function_;
1497 // Value to use for the predeclared constant iota.
1498 int iota_value_;
1499 // Current statement inserter for use by expressions.
1500 Statement_inserter inserter_;
1501 };
1502
1503 // Lower variables.
1504
1505 int
1506 Lower_parse_tree::variable(Named_object* no)
1507 {
1508 if (!no->is_variable())
1509 return TRAVERSE_CONTINUE;
1510
1511 if (no->is_variable() && no->var_value()->is_global())
1512 {
1513 // Global variables can have loops in their initialization
1514 // expressions. This is handled in lower_init_expression.
1515 no->var_value()->lower_init_expression(this->gogo_, this->function_,
1516 &this->inserter_);
1517 return TRAVERSE_CONTINUE;
1518 }
1519
1520 // This is a local variable. We are going to return
1521 // TRAVERSE_SKIP_COMPONENTS here because we want to traverse the
1522 // initialization expression when we reach the variable declaration
1523 // statement. However, that means that we need to traverse the type
1524 // ourselves.
1525 if (no->var_value()->has_type())
1526 {
1527 Type* type = no->var_value()->type();
1528 if (type != NULL)
1529 {
1530 if (Type::traverse(type, this) == TRAVERSE_EXIT)
1531 return TRAVERSE_EXIT;
1532 }
1533 }
1534 go_assert(!no->var_value()->has_pre_init());
1535
1536 return TRAVERSE_SKIP_COMPONENTS;
1537 }
1538
1539 // Lower constants. We handle constants specially so that we can set
1540 // the right value for the predeclared constant iota. This works in
1541 // conjunction with the way we lower Const_expression objects.
1542
1543 int
1544 Lower_parse_tree::constant(Named_object* no, bool)
1545 {
1546 Named_constant* nc = no->const_value();
1547
1548 // Don't get into trouble if the constant's initializer expression
1549 // refers to the constant itself.
1550 if (nc->lowering())
1551 return TRAVERSE_CONTINUE;
1552 nc->set_lowering();
1553
1554 go_assert(this->iota_value_ == -1);
1555 this->iota_value_ = nc->iota_value();
1556 nc->traverse_expression(this);
1557 this->iota_value_ = -1;
1558
1559 nc->clear_lowering();
1560
1561 // We will traverse the expression a second time, but that will be
1562 // fast.
1563
1564 return TRAVERSE_CONTINUE;
1565 }
1566
1567 // Lower function closure types. Record the function while lowering
1568 // it, so that we can pass it down when lowering an expression.
1569
1570 int
1571 Lower_parse_tree::function(Named_object* no)
1572 {
1573 no->func_value()->set_closure_type();
1574
1575 go_assert(this->function_ == NULL);
1576 this->function_ = no;
1577 int t = no->func_value()->traverse(this);
1578 this->function_ = NULL;
1579
1580 if (t == TRAVERSE_EXIT)
1581 return t;
1582 return TRAVERSE_SKIP_COMPONENTS;
1583 }
1584
1585 // Lower statement parse trees.
1586
1587 int
1588 Lower_parse_tree::statement(Block* block, size_t* pindex, Statement* sorig)
1589 {
1590 // Because we explicitly traverse the statement's contents
1591 // ourselves, we want to skip block statements here. There is
1592 // nothing to lower in a block statement.
1593 if (sorig->is_block_statement())
1594 return TRAVERSE_CONTINUE;
1595
1596 Statement_inserter hold_inserter(this->inserter_);
1597 this->inserter_ = Statement_inserter(block, pindex);
1598
1599 // Lower the expressions first.
1600 int t = sorig->traverse_contents(this);
1601 if (t == TRAVERSE_EXIT)
1602 {
1603 this->inserter_ = hold_inserter;
1604 return t;
1605 }
1606
1607 // Keep lowering until nothing changes.
1608 Statement* s = sorig;
1609 while (true)
1610 {
1611 Statement* snew = s->lower(this->gogo_, this->function_, block,
1612 &this->inserter_);
1613 if (snew == s)
1614 break;
1615 s = snew;
1616 t = s->traverse_contents(this);
1617 if (t == TRAVERSE_EXIT)
1618 {
1619 this->inserter_ = hold_inserter;
1620 return t;
1621 }
1622 }
1623
1624 if (s != sorig)
1625 block->replace_statement(*pindex, s);
1626
1627 this->inserter_ = hold_inserter;
1628 return TRAVERSE_SKIP_COMPONENTS;
1629 }
1630
1631 // Lower expression parse trees.
1632
1633 int
1634 Lower_parse_tree::expression(Expression** pexpr)
1635 {
1636 // We have to lower all subexpressions first, so that we can get
1637 // their type if necessary. This is awkward, because we don't have
1638 // a postorder traversal pass.
1639 if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
1640 return TRAVERSE_EXIT;
1641 // Keep lowering until nothing changes.
1642 while (true)
1643 {
1644 Expression* e = *pexpr;
1645 Expression* enew = e->lower(this->gogo_, this->function_,
1646 &this->inserter_, this->iota_value_);
1647 if (enew == e)
1648 break;
1649 if (enew->traverse_subexpressions(this) == TRAVERSE_EXIT)
1650 return TRAVERSE_EXIT;
1651 *pexpr = enew;
1652 }
1653 return TRAVERSE_SKIP_COMPONENTS;
1654 }
1655
1656 // Lower the parse tree. This is called after the parse is complete,
1657 // when all names should be resolved.
1658
1659 void
1660 Gogo::lower_parse_tree()
1661 {
1662 Lower_parse_tree lower_parse_tree(this, NULL);
1663 this->traverse(&lower_parse_tree);
1664 }
1665
1666 // Lower a block.
1667
1668 void
1669 Gogo::lower_block(Named_object* function, Block* block)
1670 {
1671 Lower_parse_tree lower_parse_tree(this, function);
1672 block->traverse(&lower_parse_tree);
1673 }
1674
1675 // Lower an expression. INSERTER may be NULL, in which case the
1676 // expression had better not need to create any temporaries.
1677
1678 void
1679 Gogo::lower_expression(Named_object* function, Statement_inserter* inserter,
1680 Expression** pexpr)
1681 {
1682 Lower_parse_tree lower_parse_tree(this, function);
1683 if (inserter != NULL)
1684 lower_parse_tree.set_inserter(inserter);
1685 lower_parse_tree.expression(pexpr);
1686 }
1687
1688 // Lower a constant. This is called when lowering a reference to a
1689 // constant. We have to make sure that the constant has already been
1690 // lowered.
1691
1692 void
1693 Gogo::lower_constant(Named_object* no)
1694 {
1695 go_assert(no->is_const());
1696 Lower_parse_tree lower(this, NULL);
1697 lower.constant(no, false);
1698 }
1699
1700 // Look for interface types to finalize methods of inherited
1701 // interfaces.
1702
1703 class Finalize_methods : public Traverse
1704 {
1705 public:
1706 Finalize_methods(Gogo* gogo)
1707 : Traverse(traverse_types),
1708 gogo_(gogo)
1709 { }
1710
1711 int
1712 type(Type*);
1713
1714 private:
1715 Gogo* gogo_;
1716 };
1717
1718 // Finalize the methods of an interface type.
1719
1720 int
1721 Finalize_methods::type(Type* t)
1722 {
1723 // Check the classification so that we don't finalize the methods
1724 // twice for a named interface type.
1725 switch (t->classification())
1726 {
1727 case Type::TYPE_INTERFACE:
1728 t->interface_type()->finalize_methods();
1729 break;
1730
1731 case Type::TYPE_NAMED:
1732 {
1733 // We have to finalize the methods of the real type first.
1734 // But if the real type is a struct type, then we only want to
1735 // finalize the methods of the field types, not of the struct
1736 // type itself. We don't want to add methods to the struct,
1737 // since it has a name.
1738 Named_type* nt = t->named_type();
1739 Type* rt = nt->real_type();
1740 if (rt->classification() != Type::TYPE_STRUCT)
1741 {
1742 if (Type::traverse(rt, this) == TRAVERSE_EXIT)
1743 return TRAVERSE_EXIT;
1744 }
1745 else
1746 {
1747 if (rt->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT)
1748 return TRAVERSE_EXIT;
1749 }
1750
1751 nt->finalize_methods(this->gogo_);
1752
1753 // If this type is defined in a different package, then finalize the
1754 // types of all the methods, since we won't see them otherwise.
1755 if (nt->named_object()->package() != NULL && nt->has_any_methods())
1756 {
1757 const Methods* methods = nt->methods();
1758 for (Methods::const_iterator p = methods->begin();
1759 p != methods->end();
1760 ++p)
1761 {
1762 if (Type::traverse(p->second->type(), this) == TRAVERSE_EXIT)
1763 return TRAVERSE_EXIT;
1764 }
1765 }
1766
1767 return TRAVERSE_SKIP_COMPONENTS;
1768 }
1769
1770 case Type::TYPE_STRUCT:
1771 // Traverse the field types first in case there is an embedded
1772 // field with methods that the struct should inherit.
1773 if (t->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT)
1774 return TRAVERSE_EXIT;
1775 t->struct_type()->finalize_methods(this->gogo_);
1776 return TRAVERSE_SKIP_COMPONENTS;
1777
1778 default:
1779 break;
1780 }
1781
1782 return TRAVERSE_CONTINUE;
1783 }
1784
1785 // Finalize method lists and build stub methods for types.
1786
1787 void
1788 Gogo::finalize_methods()
1789 {
1790 Finalize_methods finalize(this);
1791 this->traverse(&finalize);
1792 }
1793
1794 // Set types for unspecified variables and constants.
1795
1796 void
1797 Gogo::determine_types()
1798 {
1799 Bindings* bindings = this->current_bindings();
1800 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
1801 p != bindings->end_definitions();
1802 ++p)
1803 {
1804 if ((*p)->is_function())
1805 (*p)->func_value()->determine_types();
1806 else if ((*p)->is_variable())
1807 (*p)->var_value()->determine_type();
1808 else if ((*p)->is_const())
1809 (*p)->const_value()->determine_type();
1810
1811 // See if a variable requires us to build an initialization
1812 // function. We know that we will see all global variables
1813 // here.
1814 if (!this->need_init_fn_ && (*p)->is_variable())
1815 {
1816 Variable* variable = (*p)->var_value();
1817
1818 // If this is a global variable which requires runtime
1819 // initialization, we need an initialization function.
1820 if (!variable->is_global())
1821 ;
1822 else if (variable->init() == NULL)
1823 ;
1824 else if (variable->type()->interface_type() != NULL)
1825 this->need_init_fn_ = true;
1826 else if (variable->init()->is_constant())
1827 ;
1828 else if (!variable->init()->is_composite_literal())
1829 this->need_init_fn_ = true;
1830 else if (variable->init()->is_nonconstant_composite_literal())
1831 this->need_init_fn_ = true;
1832
1833 // If this is a global variable which holds a pointer value,
1834 // then we need an initialization function to register it as a
1835 // GC root.
1836 if (variable->is_global() && variable->type()->has_pointer())
1837 this->need_init_fn_ = true;
1838 }
1839 }
1840
1841 // Determine the types of constants in packages.
1842 for (Packages::const_iterator p = this->packages_.begin();
1843 p != this->packages_.end();
1844 ++p)
1845 p->second->determine_types();
1846 }
1847
1848 // Traversal class used for type checking.
1849
1850 class Check_types_traverse : public Traverse
1851 {
1852 public:
1853 Check_types_traverse(Gogo* gogo)
1854 : Traverse(traverse_variables
1855 | traverse_constants
1856 | traverse_functions
1857 | traverse_statements
1858 | traverse_expressions),
1859 gogo_(gogo)
1860 { }
1861
1862 int
1863 variable(Named_object*);
1864
1865 int
1866 constant(Named_object*, bool);
1867
1868 int
1869 function(Named_object*);
1870
1871 int
1872 statement(Block*, size_t* pindex, Statement*);
1873
1874 int
1875 expression(Expression**);
1876
1877 private:
1878 // General IR.
1879 Gogo* gogo_;
1880 };
1881
1882 // Check that a variable initializer has the right type.
1883
1884 int
1885 Check_types_traverse::variable(Named_object* named_object)
1886 {
1887 if (named_object->is_variable())
1888 {
1889 Variable* var = named_object->var_value();
1890
1891 // Give error if variable type is not defined.
1892 var->type()->base();
1893
1894 Expression* init = var->init();
1895 std::string reason;
1896 if (init != NULL
1897 && !Type::are_assignable(var->type(), init->type(), &reason))
1898 {
1899 if (reason.empty())
1900 error_at(var->location(), "incompatible type in initialization");
1901 else
1902 error_at(var->location(),
1903 "incompatible type in initialization (%s)",
1904 reason.c_str());
1905 var->clear_init();
1906 }
1907 else if (!var->is_used()
1908 && !var->is_global()
1909 && !var->is_parameter()
1910 && !var->is_receiver()
1911 && !var->type()->is_error()
1912 && (init == NULL || !init->is_error_expression())
1913 && !Lex::is_invalid_identifier(named_object->name()))
1914 error_at(var->location(), "%qs declared and not used",
1915 named_object->message_name().c_str());
1916 }
1917 return TRAVERSE_CONTINUE;
1918 }
1919
1920 // Check that a constant initializer has the right type.
1921
1922 int
1923 Check_types_traverse::constant(Named_object* named_object, bool)
1924 {
1925 Named_constant* constant = named_object->const_value();
1926 Type* ctype = constant->type();
1927 if (ctype->integer_type() == NULL
1928 && ctype->float_type() == NULL
1929 && ctype->complex_type() == NULL
1930 && !ctype->is_boolean_type()
1931 && !ctype->is_string_type())
1932 {
1933 if (ctype->is_nil_type())
1934 error_at(constant->location(), "const initializer cannot be nil");
1935 else if (!ctype->is_error())
1936 error_at(constant->location(), "invalid constant type");
1937 constant->set_error();
1938 }
1939 else if (!constant->expr()->is_constant())
1940 {
1941 error_at(constant->expr()->location(), "expression is not constant");
1942 constant->set_error();
1943 }
1944 else if (!Type::are_assignable(constant->type(), constant->expr()->type(),
1945 NULL))
1946 {
1947 error_at(constant->location(),
1948 "initialization expression has wrong type");
1949 constant->set_error();
1950 }
1951 return TRAVERSE_CONTINUE;
1952 }
1953
1954 // There are no types to check in a function, but this is where we
1955 // issue warnings about labels which are defined but not referenced.
1956
1957 int
1958 Check_types_traverse::function(Named_object* no)
1959 {
1960 no->func_value()->check_labels();
1961 return TRAVERSE_CONTINUE;
1962 }
1963
1964 // Check that types are valid in a statement.
1965
1966 int
1967 Check_types_traverse::statement(Block*, size_t*, Statement* s)
1968 {
1969 s->check_types(this->gogo_);
1970 return TRAVERSE_CONTINUE;
1971 }
1972
1973 // Check that types are valid in an expression.
1974
1975 int
1976 Check_types_traverse::expression(Expression** expr)
1977 {
1978 (*expr)->check_types(this->gogo_);
1979 return TRAVERSE_CONTINUE;
1980 }
1981
1982 // Check that types are valid.
1983
1984 void
1985 Gogo::check_types()
1986 {
1987 Check_types_traverse traverse(this);
1988 this->traverse(&traverse);
1989 }
1990
1991 // Check the types in a single block.
1992
1993 void
1994 Gogo::check_types_in_block(Block* block)
1995 {
1996 Check_types_traverse traverse(this);
1997 block->traverse(&traverse);
1998 }
1999
2000 // A traversal class used to find a single shortcut operator within an
2001 // expression.
2002
2003 class Find_shortcut : public Traverse
2004 {
2005 public:
2006 Find_shortcut()
2007 : Traverse(traverse_blocks
2008 | traverse_statements
2009 | traverse_expressions),
2010 found_(NULL)
2011 { }
2012
2013 // A pointer to the expression which was found, or NULL if none was
2014 // found.
2015 Expression**
2016 found() const
2017 { return this->found_; }
2018
2019 protected:
2020 int
2021 block(Block*)
2022 { return TRAVERSE_SKIP_COMPONENTS; }
2023
2024 int
2025 statement(Block*, size_t*, Statement*)
2026 { return TRAVERSE_SKIP_COMPONENTS; }
2027
2028 int
2029 expression(Expression**);
2030
2031 private:
2032 Expression** found_;
2033 };
2034
2035 // Find a shortcut expression.
2036
2037 int
2038 Find_shortcut::expression(Expression** pexpr)
2039 {
2040 Expression* expr = *pexpr;
2041 Binary_expression* be = expr->binary_expression();
2042 if (be == NULL)
2043 return TRAVERSE_CONTINUE;
2044 Operator op = be->op();
2045 if (op != OPERATOR_OROR && op != OPERATOR_ANDAND)
2046 return TRAVERSE_CONTINUE;
2047 go_assert(this->found_ == NULL);
2048 this->found_ = pexpr;
2049 return TRAVERSE_EXIT;
2050 }
2051
2052 // A traversal class used to turn shortcut operators into explicit if
2053 // statements.
2054
2055 class Shortcuts : public Traverse
2056 {
2057 public:
2058 Shortcuts(Gogo* gogo)
2059 : Traverse(traverse_variables
2060 | traverse_statements),
2061 gogo_(gogo)
2062 { }
2063
2064 protected:
2065 int
2066 variable(Named_object*);
2067
2068 int
2069 statement(Block*, size_t*, Statement*);
2070
2071 private:
2072 // Convert a shortcut operator.
2073 Statement*
2074 convert_shortcut(Block* enclosing, Expression** pshortcut);
2075
2076 // The IR.
2077 Gogo* gogo_;
2078 };
2079
2080 // Remove shortcut operators in a single statement.
2081
2082 int
2083 Shortcuts::statement(Block* block, size_t* pindex, Statement* s)
2084 {
2085 // FIXME: This approach doesn't work for switch statements, because
2086 // we add the new statements before the whole switch when we need to
2087 // instead add them just before the switch expression. The right
2088 // fix is probably to lower switch statements with nonconstant cases
2089 // to a series of conditionals.
2090 if (s->switch_statement() != NULL)
2091 return TRAVERSE_CONTINUE;
2092
2093 while (true)
2094 {
2095 Find_shortcut find_shortcut;
2096
2097 // If S is a variable declaration, then ordinary traversal won't
2098 // do anything. We want to explicitly traverse the
2099 // initialization expression if there is one.
2100 Variable_declaration_statement* vds = s->variable_declaration_statement();
2101 Expression* init = NULL;
2102 if (vds == NULL)
2103 s->traverse_contents(&find_shortcut);
2104 else
2105 {
2106 init = vds->var()->var_value()->init();
2107 if (init == NULL)
2108 return TRAVERSE_CONTINUE;
2109 init->traverse(&init, &find_shortcut);
2110 }
2111 Expression** pshortcut = find_shortcut.found();
2112 if (pshortcut == NULL)
2113 return TRAVERSE_CONTINUE;
2114
2115 Statement* snew = this->convert_shortcut(block, pshortcut);
2116 block->insert_statement_before(*pindex, snew);
2117 ++*pindex;
2118
2119 if (pshortcut == &init)
2120 vds->var()->var_value()->set_init(init);
2121 }
2122 }
2123
2124 // Remove shortcut operators in the initializer of a global variable.
2125
2126 int
2127 Shortcuts::variable(Named_object* no)
2128 {
2129 if (no->is_result_variable())
2130 return TRAVERSE_CONTINUE;
2131 Variable* var = no->var_value();
2132 Expression* init = var->init();
2133 if (!var->is_global() || init == NULL)
2134 return TRAVERSE_CONTINUE;
2135
2136 while (true)
2137 {
2138 Find_shortcut find_shortcut;
2139 init->traverse(&init, &find_shortcut);
2140 Expression** pshortcut = find_shortcut.found();
2141 if (pshortcut == NULL)
2142 return TRAVERSE_CONTINUE;
2143
2144 Statement* snew = this->convert_shortcut(NULL, pshortcut);
2145 var->add_preinit_statement(this->gogo_, snew);
2146 if (pshortcut == &init)
2147 var->set_init(init);
2148 }
2149 }
2150
2151 // Given an expression which uses a shortcut operator, return a
2152 // statement which implements it, and update *PSHORTCUT accordingly.
2153
2154 Statement*
2155 Shortcuts::convert_shortcut(Block* enclosing, Expression** pshortcut)
2156 {
2157 Binary_expression* shortcut = (*pshortcut)->binary_expression();
2158 Expression* left = shortcut->left();
2159 Expression* right = shortcut->right();
2160 Location loc = shortcut->location();
2161
2162 Block* retblock = new Block(enclosing, loc);
2163 retblock->set_end_location(loc);
2164
2165 Temporary_statement* ts = Statement::make_temporary(Type::lookup_bool_type(),
2166 left, loc);
2167 retblock->add_statement(ts);
2168
2169 Block* block = new Block(retblock, loc);
2170 block->set_end_location(loc);
2171 Expression* tmpref = Expression::make_temporary_reference(ts, loc);
2172 Statement* assign = Statement::make_assignment(tmpref, right, loc);
2173 block->add_statement(assign);
2174
2175 Expression* cond = Expression::make_temporary_reference(ts, loc);
2176 if (shortcut->binary_expression()->op() == OPERATOR_OROR)
2177 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
2178
2179 Statement* if_statement = Statement::make_if_statement(cond, block, NULL,
2180 loc);
2181 retblock->add_statement(if_statement);
2182
2183 *pshortcut = Expression::make_temporary_reference(ts, loc);
2184
2185 delete shortcut;
2186
2187 // Now convert any shortcut operators in LEFT and RIGHT.
2188 Shortcuts shortcuts(this->gogo_);
2189 retblock->traverse(&shortcuts);
2190
2191 return Statement::make_block_statement(retblock, loc);
2192 }
2193
2194 // Turn shortcut operators into explicit if statements. Doing this
2195 // considerably simplifies the order of evaluation rules.
2196
2197 void
2198 Gogo::remove_shortcuts()
2199 {
2200 Shortcuts shortcuts(this);
2201 this->traverse(&shortcuts);
2202 }
2203
2204 // A traversal class which finds all the expressions which must be
2205 // evaluated in order within a statement or larger expression. This
2206 // is used to implement the rules about order of evaluation.
2207
2208 class Find_eval_ordering : public Traverse
2209 {
2210 private:
2211 typedef std::vector<Expression**> Expression_pointers;
2212
2213 public:
2214 Find_eval_ordering()
2215 : Traverse(traverse_blocks
2216 | traverse_statements
2217 | traverse_expressions),
2218 exprs_()
2219 { }
2220
2221 size_t
2222 size() const
2223 { return this->exprs_.size(); }
2224
2225 typedef Expression_pointers::const_iterator const_iterator;
2226
2227 const_iterator
2228 begin() const
2229 { return this->exprs_.begin(); }
2230
2231 const_iterator
2232 end() const
2233 { return this->exprs_.end(); }
2234
2235 protected:
2236 int
2237 block(Block*)
2238 { return TRAVERSE_SKIP_COMPONENTS; }
2239
2240 int
2241 statement(Block*, size_t*, Statement*)
2242 { return TRAVERSE_SKIP_COMPONENTS; }
2243
2244 int
2245 expression(Expression**);
2246
2247 private:
2248 // A list of pointers to expressions with side-effects.
2249 Expression_pointers exprs_;
2250 };
2251
2252 // If an expression must be evaluated in order, put it on the list.
2253
2254 int
2255 Find_eval_ordering::expression(Expression** expression_pointer)
2256 {
2257 // We have to look at subexpressions before this one.
2258 if ((*expression_pointer)->traverse_subexpressions(this) == TRAVERSE_EXIT)
2259 return TRAVERSE_EXIT;
2260 if ((*expression_pointer)->must_eval_in_order())
2261 this->exprs_.push_back(expression_pointer);
2262 return TRAVERSE_SKIP_COMPONENTS;
2263 }
2264
2265 // A traversal class for ordering evaluations.
2266
2267 class Order_eval : public Traverse
2268 {
2269 public:
2270 Order_eval(Gogo* gogo)
2271 : Traverse(traverse_variables
2272 | traverse_statements),
2273 gogo_(gogo)
2274 { }
2275
2276 int
2277 variable(Named_object*);
2278
2279 int
2280 statement(Block*, size_t*, Statement*);
2281
2282 private:
2283 // The IR.
2284 Gogo* gogo_;
2285 };
2286
2287 // Implement the order of evaluation rules for a statement.
2288
2289 int
2290 Order_eval::statement(Block* block, size_t* pindex, Statement* s)
2291 {
2292 // FIXME: This approach doesn't work for switch statements, because
2293 // we add the new statements before the whole switch when we need to
2294 // instead add them just before the switch expression. The right
2295 // fix is probably to lower switch statements with nonconstant cases
2296 // to a series of conditionals.
2297 if (s->switch_statement() != NULL)
2298 return TRAVERSE_CONTINUE;
2299
2300 Find_eval_ordering find_eval_ordering;
2301
2302 // If S is a variable declaration, then ordinary traversal won't do
2303 // anything. We want to explicitly traverse the initialization
2304 // expression if there is one.
2305 Variable_declaration_statement* vds = s->variable_declaration_statement();
2306 Expression* init = NULL;
2307 Expression* orig_init = NULL;
2308 if (vds == NULL)
2309 s->traverse_contents(&find_eval_ordering);
2310 else
2311 {
2312 init = vds->var()->var_value()->init();
2313 if (init == NULL)
2314 return TRAVERSE_CONTINUE;
2315 orig_init = init;
2316
2317 // It might seem that this could be
2318 // init->traverse_subexpressions. Unfortunately that can fail
2319 // in a case like
2320 // var err os.Error
2321 // newvar, err := call(arg())
2322 // Here newvar will have an init of call result 0 of
2323 // call(arg()). If we only traverse subexpressions, we will
2324 // only find arg(), and we won't bother to move anything out.
2325 // Then we get to the assignment to err, we will traverse the
2326 // whole statement, and this time we will find both call() and
2327 // arg(), and so we will move them out. This will cause them to
2328 // be put into temporary variables before the assignment to err
2329 // but after the declaration of newvar. To avoid that problem,
2330 // we traverse the entire expression here.
2331 Expression::traverse(&init, &find_eval_ordering);
2332 }
2333
2334 size_t c = find_eval_ordering.size();
2335 if (c == 0)
2336 return TRAVERSE_CONTINUE;
2337
2338 // If there is only one expression with a side-effect, we can
2339 // usually leave it in place. However, for an assignment statement,
2340 // we need to evaluate an expression on the right hand side before
2341 // we evaluate any index expression on the left hand side, so for
2342 // that case we always move the expression. Otherwise we mishandle
2343 // m[0] = len(m) where m is a map.
2344 if (c == 1 && s->classification() != Statement::STATEMENT_ASSIGNMENT)
2345 return TRAVERSE_CONTINUE;
2346
2347 bool is_thunk = s->thunk_statement() != NULL;
2348 for (Find_eval_ordering::const_iterator p = find_eval_ordering.begin();
2349 p != find_eval_ordering.end();
2350 ++p)
2351 {
2352 Expression** pexpr = *p;
2353
2354 // The last expression in a thunk will be the call passed to go
2355 // or defer, which we must not evaluate early.
2356 if (is_thunk && p + 1 == find_eval_ordering.end())
2357 break;
2358
2359 Location loc = (*pexpr)->location();
2360 Statement* s;
2361 if ((*pexpr)->call_expression() == NULL
2362 || (*pexpr)->call_expression()->result_count() < 2)
2363 {
2364 Temporary_statement* ts = Statement::make_temporary(NULL, *pexpr,
2365 loc);
2366 s = ts;
2367 *pexpr = Expression::make_temporary_reference(ts, loc);
2368 }
2369 else
2370 {
2371 // A call expression which returns multiple results needs to
2372 // be handled specially. We can't create a temporary
2373 // because there is no type to give it. Any actual uses of
2374 // the values will be done via Call_result_expressions.
2375 s = Statement::make_statement(*pexpr, true);
2376 }
2377
2378 block->insert_statement_before(*pindex, s);
2379 ++*pindex;
2380 }
2381
2382 if (init != orig_init)
2383 vds->var()->var_value()->set_init(init);
2384
2385 return TRAVERSE_CONTINUE;
2386 }
2387
2388 // Implement the order of evaluation rules for the initializer of a
2389 // global variable.
2390
2391 int
2392 Order_eval::variable(Named_object* no)
2393 {
2394 if (no->is_result_variable())
2395 return TRAVERSE_CONTINUE;
2396 Variable* var = no->var_value();
2397 Expression* init = var->init();
2398 if (!var->is_global() || init == NULL)
2399 return TRAVERSE_CONTINUE;
2400
2401 Find_eval_ordering find_eval_ordering;
2402 Expression::traverse(&init, &find_eval_ordering);
2403
2404 if (find_eval_ordering.size() <= 1)
2405 {
2406 // If there is only one expression with a side-effect, we can
2407 // leave it in place.
2408 return TRAVERSE_SKIP_COMPONENTS;
2409 }
2410
2411 Expression* orig_init = init;
2412
2413 for (Find_eval_ordering::const_iterator p = find_eval_ordering.begin();
2414 p != find_eval_ordering.end();
2415 ++p)
2416 {
2417 Expression** pexpr = *p;
2418 Location loc = (*pexpr)->location();
2419 Statement* s;
2420 if ((*pexpr)->call_expression() == NULL
2421 || (*pexpr)->call_expression()->result_count() < 2)
2422 {
2423 Temporary_statement* ts = Statement::make_temporary(NULL, *pexpr,
2424 loc);
2425 s = ts;
2426 *pexpr = Expression::make_temporary_reference(ts, loc);
2427 }
2428 else
2429 {
2430 // A call expression which returns multiple results needs to
2431 // be handled specially.
2432 s = Statement::make_statement(*pexpr, true);
2433 }
2434 var->add_preinit_statement(this->gogo_, s);
2435 }
2436
2437 if (init != orig_init)
2438 var->set_init(init);
2439
2440 return TRAVERSE_SKIP_COMPONENTS;
2441 }
2442
2443 // Use temporary variables to implement the order of evaluation rules.
2444
2445 void
2446 Gogo::order_evaluations()
2447 {
2448 Order_eval order_eval(this);
2449 this->traverse(&order_eval);
2450 }
2451
2452 // Traversal to convert calls to the predeclared recover function to
2453 // pass in an argument indicating whether it can recover from a panic
2454 // or not.
2455
2456 class Convert_recover : public Traverse
2457 {
2458 public:
2459 Convert_recover(Named_object* arg)
2460 : Traverse(traverse_expressions),
2461 arg_(arg)
2462 { }
2463
2464 protected:
2465 int
2466 expression(Expression**);
2467
2468 private:
2469 // The argument to pass to the function.
2470 Named_object* arg_;
2471 };
2472
2473 // Convert calls to recover.
2474
2475 int
2476 Convert_recover::expression(Expression** pp)
2477 {
2478 Call_expression* ce = (*pp)->call_expression();
2479 if (ce != NULL && ce->is_recover_call())
2480 ce->set_recover_arg(Expression::make_var_reference(this->arg_,
2481 ce->location()));
2482 return TRAVERSE_CONTINUE;
2483 }
2484
2485 // Traversal for build_recover_thunks.
2486
2487 class Build_recover_thunks : public Traverse
2488 {
2489 public:
2490 Build_recover_thunks(Gogo* gogo)
2491 : Traverse(traverse_functions),
2492 gogo_(gogo)
2493 { }
2494
2495 int
2496 function(Named_object*);
2497
2498 private:
2499 Expression*
2500 can_recover_arg(Location);
2501
2502 // General IR.
2503 Gogo* gogo_;
2504 };
2505
2506 // If this function calls recover, turn it into a thunk.
2507
2508 int
2509 Build_recover_thunks::function(Named_object* orig_no)
2510 {
2511 Function* orig_func = orig_no->func_value();
2512 if (!orig_func->calls_recover()
2513 || orig_func->is_recover_thunk()
2514 || orig_func->has_recover_thunk())
2515 return TRAVERSE_CONTINUE;
2516
2517 Gogo* gogo = this->gogo_;
2518 Location location = orig_func->location();
2519
2520 static int count;
2521 char buf[50];
2522
2523 Function_type* orig_fntype = orig_func->type();
2524 Typed_identifier_list* new_params = new Typed_identifier_list();
2525 std::string receiver_name;
2526 if (orig_fntype->is_method())
2527 {
2528 const Typed_identifier* receiver = orig_fntype->receiver();
2529 snprintf(buf, sizeof buf, "rt.%u", count);
2530 ++count;
2531 receiver_name = buf;
2532 new_params->push_back(Typed_identifier(receiver_name, receiver->type(),
2533 receiver->location()));
2534 }
2535 const Typed_identifier_list* orig_params = orig_fntype->parameters();
2536 if (orig_params != NULL && !orig_params->empty())
2537 {
2538 for (Typed_identifier_list::const_iterator p = orig_params->begin();
2539 p != orig_params->end();
2540 ++p)
2541 {
2542 snprintf(buf, sizeof buf, "pt.%u", count);
2543 ++count;
2544 new_params->push_back(Typed_identifier(buf, p->type(),
2545 p->location()));
2546 }
2547 }
2548 snprintf(buf, sizeof buf, "pr.%u", count);
2549 ++count;
2550 std::string can_recover_name = buf;
2551 new_params->push_back(Typed_identifier(can_recover_name,
2552 Type::lookup_bool_type(),
2553 orig_fntype->location()));
2554
2555 const Typed_identifier_list* orig_results = orig_fntype->results();
2556 Typed_identifier_list* new_results;
2557 if (orig_results == NULL || orig_results->empty())
2558 new_results = NULL;
2559 else
2560 {
2561 new_results = new Typed_identifier_list();
2562 for (Typed_identifier_list::const_iterator p = orig_results->begin();
2563 p != orig_results->end();
2564 ++p)
2565 new_results->push_back(Typed_identifier("", p->type(), p->location()));
2566 }
2567
2568 Function_type *new_fntype = Type::make_function_type(NULL, new_params,
2569 new_results,
2570 orig_fntype->location());
2571 if (orig_fntype->is_varargs())
2572 new_fntype->set_is_varargs();
2573
2574 std::string name = orig_no->name() + "$recover";
2575 Named_object *new_no = gogo->start_function(name, new_fntype, false,
2576 location);
2577 Function *new_func = new_no->func_value();
2578 if (orig_func->enclosing() != NULL)
2579 new_func->set_enclosing(orig_func->enclosing());
2580
2581 // We build the code for the original function attached to the new
2582 // function, and then swap the original and new function bodies.
2583 // This means that existing references to the original function will
2584 // then refer to the new function. That makes this code a little
2585 // confusing, in that the reference to NEW_NO really refers to the
2586 // other function, not the one we are building.
2587
2588 Expression* closure = NULL;
2589 if (orig_func->needs_closure())
2590 {
2591 Named_object* orig_closure_no = orig_func->closure_var();
2592 Variable* orig_closure_var = orig_closure_no->var_value();
2593 Variable* new_var = new Variable(orig_closure_var->type(), NULL, false,
2594 true, false, location);
2595 snprintf(buf, sizeof buf, "closure.%u", count);
2596 ++count;
2597 Named_object* new_closure_no = Named_object::make_variable(buf, NULL,
2598 new_var);
2599 new_func->set_closure_var(new_closure_no);
2600 closure = Expression::make_var_reference(new_closure_no, location);
2601 }
2602
2603 Expression* fn = Expression::make_func_reference(new_no, closure, location);
2604
2605 Expression_list* args = new Expression_list();
2606 if (new_params != NULL)
2607 {
2608 // Note that we skip the last parameter, which is the boolean
2609 // indicating whether recover can succed.
2610 for (Typed_identifier_list::const_iterator p = new_params->begin();
2611 p + 1 != new_params->end();
2612 ++p)
2613 {
2614 Named_object* p_no = gogo->lookup(p->name(), NULL);
2615 go_assert(p_no != NULL
2616 && p_no->is_variable()
2617 && p_no->var_value()->is_parameter());
2618 args->push_back(Expression::make_var_reference(p_no, location));
2619 }
2620 }
2621 args->push_back(this->can_recover_arg(location));
2622
2623 gogo->start_block(location);
2624
2625 Call_expression* call = Expression::make_call(fn, args, false, location);
2626
2627 // Any varargs call has already been lowered.
2628 call->set_varargs_are_lowered();
2629
2630 Statement* s;
2631 if (orig_fntype->results() == NULL || orig_fntype->results()->empty())
2632 s = Statement::make_statement(call, true);
2633 else
2634 {
2635 Expression_list* vals = new Expression_list();
2636 size_t rc = orig_fntype->results()->size();
2637 if (rc == 1)
2638 vals->push_back(call);
2639 else
2640 {
2641 for (size_t i = 0; i < rc; ++i)
2642 vals->push_back(Expression::make_call_result(call, i));
2643 }
2644 s = Statement::make_return_statement(vals, location);
2645 }
2646 s->determine_types();
2647 gogo->add_statement(s);
2648
2649 Block* b = gogo->finish_block(location);
2650
2651 gogo->add_block(b, location);
2652
2653 // Lower the call in case it returns multiple results.
2654 gogo->lower_block(new_no, b);
2655
2656 gogo->finish_function(location);
2657
2658 // Swap the function bodies and types.
2659 new_func->swap_for_recover(orig_func);
2660 orig_func->set_is_recover_thunk();
2661 new_func->set_calls_recover();
2662 new_func->set_has_recover_thunk();
2663
2664 Bindings* orig_bindings = orig_func->block()->bindings();
2665 Bindings* new_bindings = new_func->block()->bindings();
2666 if (orig_fntype->is_method())
2667 {
2668 // We changed the receiver to be a regular parameter. We have
2669 // to update the binding accordingly in both functions.
2670 Named_object* orig_rec_no = orig_bindings->lookup_local(receiver_name);
2671 go_assert(orig_rec_no != NULL
2672 && orig_rec_no->is_variable()
2673 && !orig_rec_no->var_value()->is_receiver());
2674 orig_rec_no->var_value()->set_is_receiver();
2675
2676 const std::string& new_receiver_name(orig_fntype->receiver()->name());
2677 Named_object* new_rec_no = new_bindings->lookup_local(new_receiver_name);
2678 if (new_rec_no == NULL)
2679 go_assert(saw_errors());
2680 else
2681 {
2682 go_assert(new_rec_no->is_variable()
2683 && new_rec_no->var_value()->is_receiver());
2684 new_rec_no->var_value()->set_is_not_receiver();
2685 }
2686 }
2687
2688 // Because we flipped blocks but not types, the can_recover
2689 // parameter appears in the (now) old bindings as a parameter.
2690 // Change it to a local variable, whereupon it will be discarded.
2691 Named_object* can_recover_no = orig_bindings->lookup_local(can_recover_name);
2692 go_assert(can_recover_no != NULL
2693 && can_recover_no->is_variable()
2694 && can_recover_no->var_value()->is_parameter());
2695 orig_bindings->remove_binding(can_recover_no);
2696
2697 // Add the can_recover argument to the (now) new bindings, and
2698 // attach it to any recover statements.
2699 Variable* can_recover_var = new Variable(Type::lookup_bool_type(), NULL,
2700 false, true, false, location);
2701 can_recover_no = new_bindings->add_variable(can_recover_name, NULL,
2702 can_recover_var);
2703 Convert_recover convert_recover(can_recover_no);
2704 new_func->traverse(&convert_recover);
2705
2706 // Update the function pointers in any named results.
2707 new_func->update_result_variables();
2708 orig_func->update_result_variables();
2709
2710 return TRAVERSE_CONTINUE;
2711 }
2712
2713 // Return the expression to pass for the .can_recover parameter to the
2714 // new function. This indicates whether a call to recover may return
2715 // non-nil. The expression is
2716 // __go_can_recover(__builtin_return_address()).
2717
2718 Expression*
2719 Build_recover_thunks::can_recover_arg(Location location)
2720 {
2721 static Named_object* builtin_return_address;
2722 if (builtin_return_address == NULL)
2723 {
2724 const Location bloc = Linemap::predeclared_location();
2725
2726 Typed_identifier_list* param_types = new Typed_identifier_list();
2727 Type* uint_type = Type::lookup_integer_type("uint");
2728 param_types->push_back(Typed_identifier("l", uint_type, bloc));
2729
2730 Typed_identifier_list* return_types = new Typed_identifier_list();
2731 Type* voidptr_type = Type::make_pointer_type(Type::make_void_type());
2732 return_types->push_back(Typed_identifier("", voidptr_type, bloc));
2733
2734 Function_type* fntype = Type::make_function_type(NULL, param_types,
2735 return_types, bloc);
2736 builtin_return_address =
2737 Named_object::make_function_declaration("__builtin_return_address",
2738 NULL, fntype, bloc);
2739 const char* n = "__builtin_return_address";
2740 builtin_return_address->func_declaration_value()->set_asm_name(n);
2741 }
2742
2743 static Named_object* can_recover;
2744 if (can_recover == NULL)
2745 {
2746 const Location bloc = Linemap::predeclared_location();
2747 Typed_identifier_list* param_types = new Typed_identifier_list();
2748 Type* voidptr_type = Type::make_pointer_type(Type::make_void_type());
2749 param_types->push_back(Typed_identifier("a", voidptr_type, bloc));
2750 Type* boolean_type = Type::lookup_bool_type();
2751 Typed_identifier_list* results = new Typed_identifier_list();
2752 results->push_back(Typed_identifier("", boolean_type, bloc));
2753 Function_type* fntype = Type::make_function_type(NULL, param_types,
2754 results, bloc);
2755 can_recover = Named_object::make_function_declaration("__go_can_recover",
2756 NULL, fntype,
2757 bloc);
2758 can_recover->func_declaration_value()->set_asm_name("__go_can_recover");
2759 }
2760
2761 Expression* fn = Expression::make_func_reference(builtin_return_address,
2762 NULL, location);
2763
2764 mpz_t zval;
2765 mpz_init_set_ui(zval, 0UL);
2766 Expression* zexpr = Expression::make_integer(&zval, NULL, location);
2767 mpz_clear(zval);
2768 Expression_list *args = new Expression_list();
2769 args->push_back(zexpr);
2770
2771 Expression* call = Expression::make_call(fn, args, false, location);
2772
2773 args = new Expression_list();
2774 args->push_back(call);
2775
2776 fn = Expression::make_func_reference(can_recover, NULL, location);
2777 return Expression::make_call(fn, args, false, location);
2778 }
2779
2780 // Build thunks for functions which call recover. We build a new
2781 // function with an extra parameter, which is whether a call to
2782 // recover can succeed. We then move the body of this function to
2783 // that one. We then turn this function into a thunk which calls the
2784 // new one, passing the value of
2785 // __go_can_recover(__builtin_return_address()). The function will be
2786 // marked as not splitting the stack. This will cooperate with the
2787 // implementation of defer to make recover do the right thing.
2788
2789 void
2790 Gogo::build_recover_thunks()
2791 {
2792 Build_recover_thunks build_recover_thunks(this);
2793 this->traverse(&build_recover_thunks);
2794 }
2795
2796 // Look for named types to see whether we need to create an interface
2797 // method table.
2798
2799 class Build_method_tables : public Traverse
2800 {
2801 public:
2802 Build_method_tables(Gogo* gogo,
2803 const std::vector<Interface_type*>& interfaces)
2804 : Traverse(traverse_types),
2805 gogo_(gogo), interfaces_(interfaces)
2806 { }
2807
2808 int
2809 type(Type*);
2810
2811 private:
2812 // The IR.
2813 Gogo* gogo_;
2814 // A list of locally defined interfaces which have hidden methods.
2815 const std::vector<Interface_type*>& interfaces_;
2816 };
2817
2818 // Build all required interface method tables for types. We need to
2819 // ensure that we have an interface method table for every interface
2820 // which has a hidden method, for every named type which implements
2821 // that interface. Normally we can just build interface method tables
2822 // as we need them. However, in some cases we can require an
2823 // interface method table for an interface defined in a different
2824 // package for a type defined in that package. If that interface and
2825 // type both use a hidden method, that is OK. However, we will not be
2826 // able to build that interface method table when we need it, because
2827 // the type's hidden method will be static. So we have to build it
2828 // here, and just refer it from other packages as needed.
2829
2830 void
2831 Gogo::build_interface_method_tables()
2832 {
2833 if (saw_errors())
2834 return;
2835
2836 std::vector<Interface_type*> hidden_interfaces;
2837 hidden_interfaces.reserve(this->interface_types_.size());
2838 for (std::vector<Interface_type*>::const_iterator pi =
2839 this->interface_types_.begin();
2840 pi != this->interface_types_.end();
2841 ++pi)
2842 {
2843 const Typed_identifier_list* methods = (*pi)->methods();
2844 if (methods == NULL)
2845 continue;
2846 for (Typed_identifier_list::const_iterator pm = methods->begin();
2847 pm != methods->end();
2848 ++pm)
2849 {
2850 if (Gogo::is_hidden_name(pm->name()))
2851 {
2852 hidden_interfaces.push_back(*pi);
2853 break;
2854 }
2855 }
2856 }
2857
2858 if (!hidden_interfaces.empty())
2859 {
2860 // Now traverse the tree looking for all named types.
2861 Build_method_tables bmt(this, hidden_interfaces);
2862 this->traverse(&bmt);
2863 }
2864
2865 // We no longer need the list of interfaces.
2866
2867 this->interface_types_.clear();
2868 }
2869
2870 // This is called for each type. For a named type, for each of the
2871 // interfaces with hidden methods that it implements, create the
2872 // method table.
2873
2874 int
2875 Build_method_tables::type(Type* type)
2876 {
2877 Named_type* nt = type->named_type();
2878 Struct_type* st = type->struct_type();
2879 if (nt != NULL || st != NULL)
2880 {
2881 for (std::vector<Interface_type*>::const_iterator p =
2882 this->interfaces_.begin();
2883 p != this->interfaces_.end();
2884 ++p)
2885 {
2886 // We ask whether a pointer to the named type implements the
2887 // interface, because a pointer can implement more methods
2888 // than a value.
2889 if (nt != NULL)
2890 {
2891 if ((*p)->implements_interface(Type::make_pointer_type(nt),
2892 NULL))
2893 {
2894 nt->interface_method_table(this->gogo_, *p, false);
2895 nt->interface_method_table(this->gogo_, *p, true);
2896 }
2897 }
2898 else
2899 {
2900 if ((*p)->implements_interface(Type::make_pointer_type(st),
2901 NULL))
2902 {
2903 st->interface_method_table(this->gogo_, *p, false);
2904 st->interface_method_table(this->gogo_, *p, true);
2905 }
2906 }
2907 }
2908 }
2909 return TRAVERSE_CONTINUE;
2910 }
2911
2912 // Traversal class used to check for return statements.
2913
2914 class Check_return_statements_traverse : public Traverse
2915 {
2916 public:
2917 Check_return_statements_traverse()
2918 : Traverse(traverse_functions)
2919 { }
2920
2921 int
2922 function(Named_object*);
2923 };
2924
2925 // Check that a function has a return statement if it needs one.
2926
2927 int
2928 Check_return_statements_traverse::function(Named_object* no)
2929 {
2930 Function* func = no->func_value();
2931 const Function_type* fntype = func->type();
2932 const Typed_identifier_list* results = fntype->results();
2933
2934 // We only need a return statement if there is a return value.
2935 if (results == NULL || results->empty())
2936 return TRAVERSE_CONTINUE;
2937
2938 if (func->block()->may_fall_through())
2939 error_at(func->location(), "control reaches end of non-void function");
2940
2941 return TRAVERSE_CONTINUE;
2942 }
2943
2944 // Check return statements.
2945
2946 void
2947 Gogo::check_return_statements()
2948 {
2949 Check_return_statements_traverse traverse;
2950 this->traverse(&traverse);
2951 }
2952
2953 // Work out the package priority. It is one more than the maximum
2954 // priority of an imported package.
2955
2956 int
2957 Gogo::package_priority() const
2958 {
2959 int priority = 0;
2960 for (Packages::const_iterator p = this->packages_.begin();
2961 p != this->packages_.end();
2962 ++p)
2963 if (p->second->priority() > priority)
2964 priority = p->second->priority();
2965 return priority + 1;
2966 }
2967
2968 // Export identifiers as requested.
2969
2970 void
2971 Gogo::do_exports()
2972 {
2973 // For now we always stream to a section. Later we may want to
2974 // support streaming to a separate file.
2975 Stream_to_section stream;
2976
2977 Export exp(&stream);
2978 exp.register_builtin_types(this);
2979 exp.export_globals(this->package_name(),
2980 this->pkgpath(),
2981 this->package_priority(),
2982 this->imports_,
2983 (this->need_init_fn_ && !this->is_main_package()
2984 ? this->get_init_fn_name()
2985 : ""),
2986 this->imported_init_fns_,
2987 this->package_->bindings());
2988 }
2989
2990 // Find the blocks in order to convert named types defined in blocks.
2991
2992 class Convert_named_types : public Traverse
2993 {
2994 public:
2995 Convert_named_types(Gogo* gogo)
2996 : Traverse(traverse_blocks),
2997 gogo_(gogo)
2998 { }
2999
3000 protected:
3001 int
3002 block(Block* block);
3003
3004 private:
3005 Gogo* gogo_;
3006 };
3007
3008 int
3009 Convert_named_types::block(Block* block)
3010 {
3011 this->gogo_->convert_named_types_in_bindings(block->bindings());
3012 return TRAVERSE_CONTINUE;
3013 }
3014
3015 // Convert all named types to the backend representation. Since named
3016 // types can refer to other types, this needs to be done in the right
3017 // sequence, which is handled by Named_type::convert. Here we arrange
3018 // to call that for each named type.
3019
3020 void
3021 Gogo::convert_named_types()
3022 {
3023 this->convert_named_types_in_bindings(this->globals_);
3024 for (Packages::iterator p = this->packages_.begin();
3025 p != this->packages_.end();
3026 ++p)
3027 {
3028 Package* package = p->second;
3029 this->convert_named_types_in_bindings(package->bindings());
3030 }
3031
3032 Convert_named_types cnt(this);
3033 this->traverse(&cnt);
3034
3035 // Make all the builtin named types used for type descriptors, and
3036 // then convert them. They will only be written out if they are
3037 // needed.
3038 Type::make_type_descriptor_type();
3039 Type::make_type_descriptor_ptr_type();
3040 Function_type::make_function_type_descriptor_type();
3041 Pointer_type::make_pointer_type_descriptor_type();
3042 Struct_type::make_struct_type_descriptor_type();
3043 Array_type::make_array_type_descriptor_type();
3044 Array_type::make_slice_type_descriptor_type();
3045 Map_type::make_map_type_descriptor_type();
3046 Map_type::make_map_descriptor_type();
3047 Channel_type::make_chan_type_descriptor_type();
3048 Interface_type::make_interface_type_descriptor_type();
3049 Type::convert_builtin_named_types(this);
3050
3051 Runtime::convert_types(this);
3052
3053 this->named_types_are_converted_ = true;
3054 }
3055
3056 // Convert all names types in a set of bindings.
3057
3058 void
3059 Gogo::convert_named_types_in_bindings(Bindings* bindings)
3060 {
3061 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
3062 p != bindings->end_definitions();
3063 ++p)
3064 {
3065 if ((*p)->is_type())
3066 (*p)->type_value()->convert(this);
3067 }
3068 }
3069
3070 // Class Function.
3071
3072 Function::Function(Function_type* type, Function* enclosing, Block* block,
3073 Location location)
3074 : type_(type), enclosing_(enclosing), results_(NULL),
3075 closure_var_(NULL), block_(block), location_(location), labels_(),
3076 local_type_count_(0), fndecl_(NULL), defer_stack_(NULL),
3077 results_are_named_(false), calls_recover_(false), is_recover_thunk_(false),
3078 has_recover_thunk_(false)
3079 {
3080 }
3081
3082 // Create the named result variables.
3083
3084 void
3085 Function::create_result_variables(Gogo* gogo)
3086 {
3087 const Typed_identifier_list* results = this->type_->results();
3088 if (results == NULL || results->empty())
3089 return;
3090
3091 if (!results->front().name().empty())
3092 this->results_are_named_ = true;
3093
3094 this->results_ = new Results();
3095 this->results_->reserve(results->size());
3096
3097 Block* block = this->block_;
3098 int index = 0;
3099 for (Typed_identifier_list::const_iterator p = results->begin();
3100 p != results->end();
3101 ++p, ++index)
3102 {
3103 std::string name = p->name();
3104 if (name.empty() || Gogo::is_sink_name(name))
3105 {
3106 static int result_counter;
3107 char buf[100];
3108 snprintf(buf, sizeof buf, "$ret%d", result_counter);
3109 ++result_counter;
3110 name = gogo->pack_hidden_name(buf, false);
3111 }
3112 Result_variable* result = new Result_variable(p->type(), this, index,
3113 p->location());
3114 Named_object* no = block->bindings()->add_result_variable(name, result);
3115 if (no->is_result_variable())
3116 this->results_->push_back(no);
3117 else
3118 {
3119 static int dummy_result_count;
3120 char buf[100];
3121 snprintf(buf, sizeof buf, "$dret%d", dummy_result_count);
3122 ++dummy_result_count;
3123 name = gogo->pack_hidden_name(buf, false);
3124 no = block->bindings()->add_result_variable(name, result);
3125 go_assert(no->is_result_variable());
3126 this->results_->push_back(no);
3127 }
3128 }
3129 }
3130
3131 // Update the named result variables when cloning a function which
3132 // calls recover.
3133
3134 void
3135 Function::update_result_variables()
3136 {
3137 if (this->results_ == NULL)
3138 return;
3139
3140 for (Results::iterator p = this->results_->begin();
3141 p != this->results_->end();
3142 ++p)
3143 (*p)->result_var_value()->set_function(this);
3144 }
3145
3146 // Return the closure variable, creating it if necessary.
3147
3148 Named_object*
3149 Function::closure_var()
3150 {
3151 if (this->closure_var_ == NULL)
3152 {
3153 // We don't know the type of the variable yet. We add fields as
3154 // we find them.
3155 Location loc = this->type_->location();
3156 Struct_field_list* sfl = new Struct_field_list;
3157 Type* struct_type = Type::make_struct_type(sfl, loc);
3158 Variable* var = new Variable(Type::make_pointer_type(struct_type),
3159 NULL, false, true, false, loc);
3160 var->set_is_used();
3161 this->closure_var_ = Named_object::make_variable("closure", NULL, var);
3162 // Note that the new variable is not in any binding contour.
3163 }
3164 return this->closure_var_;
3165 }
3166
3167 // Set the type of the closure variable.
3168
3169 void
3170 Function::set_closure_type()
3171 {
3172 if (this->closure_var_ == NULL)
3173 return;
3174 Named_object* closure = this->closure_var_;
3175 Struct_type* st = closure->var_value()->type()->deref()->struct_type();
3176 unsigned int index = 0;
3177 for (Closure_fields::const_iterator p = this->closure_fields_.begin();
3178 p != this->closure_fields_.end();
3179 ++p, ++index)
3180 {
3181 Named_object* no = p->first;
3182 char buf[20];
3183 snprintf(buf, sizeof buf, "%u", index);
3184 std::string n = no->name() + buf;
3185 Type* var_type;
3186 if (no->is_variable())
3187 var_type = no->var_value()->type();
3188 else
3189 var_type = no->result_var_value()->type();
3190 Type* field_type = Type::make_pointer_type(var_type);
3191 st->push_field(Struct_field(Typed_identifier(n, field_type, p->second)));
3192 }
3193 }
3194
3195 // Return whether this function is a method.
3196
3197 bool
3198 Function::is_method() const
3199 {
3200 return this->type_->is_method();
3201 }
3202
3203 // Add a label definition.
3204
3205 Label*
3206 Function::add_label_definition(Gogo* gogo, const std::string& label_name,
3207 Location location)
3208 {
3209 Label* lnull = NULL;
3210 std::pair<Labels::iterator, bool> ins =
3211 this->labels_.insert(std::make_pair(label_name, lnull));
3212 Label* label;
3213 if (ins.second)
3214 {
3215 // This is a new label.
3216 label = new Label(label_name);
3217 ins.first->second = label;
3218 }
3219 else
3220 {
3221 // The label was already in the hash table.
3222 label = ins.first->second;
3223 if (label->is_defined())
3224 {
3225 error_at(location, "label %qs already defined",
3226 Gogo::message_name(label_name).c_str());
3227 inform(label->location(), "previous definition of %qs was here",
3228 Gogo::message_name(label_name).c_str());
3229 return new Label(label_name);
3230 }
3231 }
3232
3233 label->define(location, gogo->bindings_snapshot(location));
3234
3235 // Issue any errors appropriate for any previous goto's to this
3236 // label.
3237 const std::vector<Bindings_snapshot*>& refs(label->refs());
3238 for (std::vector<Bindings_snapshot*>::const_iterator p = refs.begin();
3239 p != refs.end();
3240 ++p)
3241 (*p)->check_goto_to(gogo->current_block());
3242 label->clear_refs();
3243
3244 return label;
3245 }
3246
3247 // Add a reference to a label.
3248
3249 Label*
3250 Function::add_label_reference(Gogo* gogo, const std::string& label_name,
3251 Location location, bool issue_goto_errors)
3252 {
3253 Label* lnull = NULL;
3254 std::pair<Labels::iterator, bool> ins =
3255 this->labels_.insert(std::make_pair(label_name, lnull));
3256 Label* label;
3257 if (!ins.second)
3258 {
3259 // The label was already in the hash table.
3260 label = ins.first->second;
3261 }
3262 else
3263 {
3264 go_assert(ins.first->second == NULL);
3265 label = new Label(label_name);
3266 ins.first->second = label;
3267 }
3268
3269 label->set_is_used();
3270
3271 if (issue_goto_errors)
3272 {
3273 Bindings_snapshot* snapshot = label->snapshot();
3274 if (snapshot != NULL)
3275 snapshot->check_goto_from(gogo->current_block(), location);
3276 else
3277 label->add_snapshot_ref(gogo->bindings_snapshot(location));
3278 }
3279
3280 return label;
3281 }
3282
3283 // Warn about labels that are defined but not used.
3284
3285 void
3286 Function::check_labels() const
3287 {
3288 for (Labels::const_iterator p = this->labels_.begin();
3289 p != this->labels_.end();
3290 p++)
3291 {
3292 Label* label = p->second;
3293 if (!label->is_used())
3294 error_at(label->location(), "label %qs defined and not used",
3295 Gogo::message_name(label->name()).c_str());
3296 }
3297 }
3298
3299 // Swap one function with another. This is used when building the
3300 // thunk we use to call a function which calls recover. It may not
3301 // work for any other case.
3302
3303 void
3304 Function::swap_for_recover(Function *x)
3305 {
3306 go_assert(this->enclosing_ == x->enclosing_);
3307 std::swap(this->results_, x->results_);
3308 std::swap(this->closure_var_, x->closure_var_);
3309 std::swap(this->block_, x->block_);
3310 go_assert(this->location_ == x->location_);
3311 go_assert(this->fndecl_ == NULL && x->fndecl_ == NULL);
3312 go_assert(this->defer_stack_ == NULL && x->defer_stack_ == NULL);
3313 }
3314
3315 // Traverse the tree.
3316
3317 int
3318 Function::traverse(Traverse* traverse)
3319 {
3320 unsigned int traverse_mask = traverse->traverse_mask();
3321
3322 if ((traverse_mask
3323 & (Traverse::traverse_types | Traverse::traverse_expressions))
3324 != 0)
3325 {
3326 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3327 return TRAVERSE_EXIT;
3328 }
3329
3330 // FIXME: We should check traverse_functions here if nested
3331 // functions are stored in block bindings.
3332 if (this->block_ != NULL
3333 && (traverse_mask
3334 & (Traverse::traverse_variables
3335 | Traverse::traverse_constants
3336 | Traverse::traverse_blocks
3337 | Traverse::traverse_statements
3338 | Traverse::traverse_expressions
3339 | Traverse::traverse_types)) != 0)
3340 {
3341 if (this->block_->traverse(traverse) == TRAVERSE_EXIT)
3342 return TRAVERSE_EXIT;
3343 }
3344
3345 return TRAVERSE_CONTINUE;
3346 }
3347
3348 // Work out types for unspecified variables and constants.
3349
3350 void
3351 Function::determine_types()
3352 {
3353 if (this->block_ != NULL)
3354 this->block_->determine_types();
3355 }
3356
3357 // Get a pointer to the variable representing the defer stack for this
3358 // function, making it if necessary. The value of the variable is set
3359 // by the runtime routines to true if the function is returning,
3360 // rather than panicing through. A pointer to this variable is used
3361 // as a marker for the functions on the defer stack associated with
3362 // this function. A function-specific variable permits inlining a
3363 // function which uses defer.
3364
3365 Expression*
3366 Function::defer_stack(Location location)
3367 {
3368 if (this->defer_stack_ == NULL)
3369 {
3370 Type* t = Type::lookup_bool_type();
3371 Expression* n = Expression::make_boolean(false, location);
3372 this->defer_stack_ = Statement::make_temporary(t, n, location);
3373 this->defer_stack_->set_is_address_taken();
3374 }
3375 Expression* ref = Expression::make_temporary_reference(this->defer_stack_,
3376 location);
3377 return Expression::make_unary(OPERATOR_AND, ref, location);
3378 }
3379
3380 // Export the function.
3381
3382 void
3383 Function::export_func(Export* exp, const std::string& name) const
3384 {
3385 Function::export_func_with_type(exp, name, this->type_);
3386 }
3387
3388 // Export a function with a type.
3389
3390 void
3391 Function::export_func_with_type(Export* exp, const std::string& name,
3392 const Function_type* fntype)
3393 {
3394 exp->write_c_string("func ");
3395
3396 if (fntype->is_method())
3397 {
3398 exp->write_c_string("(");
3399 const Typed_identifier* receiver = fntype->receiver();
3400 exp->write_name(receiver->name());
3401 exp->write_c_string(" ");
3402 exp->write_type(receiver->type());
3403 exp->write_c_string(") ");
3404 }
3405
3406 exp->write_string(name);
3407
3408 exp->write_c_string(" (");
3409 const Typed_identifier_list* parameters = fntype->parameters();
3410 if (parameters != NULL)
3411 {
3412 bool is_varargs = fntype->is_varargs();
3413 bool first = true;
3414 for (Typed_identifier_list::const_iterator p = parameters->begin();
3415 p != parameters->end();
3416 ++p)
3417 {
3418 if (first)
3419 first = false;
3420 else
3421 exp->write_c_string(", ");
3422 exp->write_name(p->name());
3423 exp->write_c_string(" ");
3424 if (!is_varargs || p + 1 != parameters->end())
3425 exp->write_type(p->type());
3426 else
3427 {
3428 exp->write_c_string("...");
3429 exp->write_type(p->type()->array_type()->element_type());
3430 }
3431 }
3432 }
3433 exp->write_c_string(")");
3434
3435 const Typed_identifier_list* results = fntype->results();
3436 if (results != NULL)
3437 {
3438 if (results->size() == 1 && results->begin()->name().empty())
3439 {
3440 exp->write_c_string(" ");
3441 exp->write_type(results->begin()->type());
3442 }
3443 else
3444 {
3445 exp->write_c_string(" (");
3446 bool first = true;
3447 for (Typed_identifier_list::const_iterator p = results->begin();
3448 p != results->end();
3449 ++p)
3450 {
3451 if (first)
3452 first = false;
3453 else
3454 exp->write_c_string(", ");
3455 exp->write_name(p->name());
3456 exp->write_c_string(" ");
3457 exp->write_type(p->type());
3458 }
3459 exp->write_c_string(")");
3460 }
3461 }
3462 exp->write_c_string(";\n");
3463 }
3464
3465 // Import a function.
3466
3467 void
3468 Function::import_func(Import* imp, std::string* pname,
3469 Typed_identifier** preceiver,
3470 Typed_identifier_list** pparameters,
3471 Typed_identifier_list** presults,
3472 bool* is_varargs)
3473 {
3474 imp->require_c_string("func ");
3475
3476 *preceiver = NULL;
3477 if (imp->peek_char() == '(')
3478 {
3479 imp->require_c_string("(");
3480 std::string name = imp->read_name();
3481 imp->require_c_string(" ");
3482 Type* rtype = imp->read_type();
3483 *preceiver = new Typed_identifier(name, rtype, imp->location());
3484 imp->require_c_string(") ");
3485 }
3486
3487 *pname = imp->read_identifier();
3488
3489 Typed_identifier_list* parameters;
3490 *is_varargs = false;
3491 imp->require_c_string(" (");
3492 if (imp->peek_char() == ')')
3493 parameters = NULL;
3494 else
3495 {
3496 parameters = new Typed_identifier_list();
3497 while (true)
3498 {
3499 std::string name = imp->read_name();
3500 imp->require_c_string(" ");
3501
3502 if (imp->match_c_string("..."))
3503 {
3504 imp->advance(3);
3505 *is_varargs = true;
3506 }
3507
3508 Type* ptype = imp->read_type();
3509 if (*is_varargs)
3510 ptype = Type::make_array_type(ptype, NULL);
3511 parameters->push_back(Typed_identifier(name, ptype,
3512 imp->location()));
3513 if (imp->peek_char() != ',')
3514 break;
3515 go_assert(!*is_varargs);
3516 imp->require_c_string(", ");
3517 }
3518 }
3519 imp->require_c_string(")");
3520 *pparameters = parameters;
3521
3522 Typed_identifier_list* results;
3523 if (imp->peek_char() != ' ')
3524 results = NULL;
3525 else
3526 {
3527 results = new Typed_identifier_list();
3528 imp->require_c_string(" ");
3529 if (imp->peek_char() != '(')
3530 {
3531 Type* rtype = imp->read_type();
3532 results->push_back(Typed_identifier("", rtype, imp->location()));
3533 }
3534 else
3535 {
3536 imp->require_c_string("(");
3537 while (true)
3538 {
3539 std::string name = imp->read_name();
3540 imp->require_c_string(" ");
3541 Type* rtype = imp->read_type();
3542 results->push_back(Typed_identifier(name, rtype,
3543 imp->location()));
3544 if (imp->peek_char() != ',')
3545 break;
3546 imp->require_c_string(", ");
3547 }
3548 imp->require_c_string(")");
3549 }
3550 }
3551 imp->require_c_string(";\n");
3552 *presults = results;
3553 }
3554
3555 // Class Block.
3556
3557 Block::Block(Block* enclosing, Location location)
3558 : enclosing_(enclosing), statements_(),
3559 bindings_(new Bindings(enclosing == NULL
3560 ? NULL
3561 : enclosing->bindings())),
3562 start_location_(location),
3563 end_location_(UNKNOWN_LOCATION)
3564 {
3565 }
3566
3567 // Add a statement to a block.
3568
3569 void
3570 Block::add_statement(Statement* statement)
3571 {
3572 this->statements_.push_back(statement);
3573 }
3574
3575 // Add a statement to the front of a block. This is slow but is only
3576 // used for reference counts of parameters.
3577
3578 void
3579 Block::add_statement_at_front(Statement* statement)
3580 {
3581 this->statements_.insert(this->statements_.begin(), statement);
3582 }
3583
3584 // Replace a statement in a block.
3585
3586 void
3587 Block::replace_statement(size_t index, Statement* s)
3588 {
3589 go_assert(index < this->statements_.size());
3590 this->statements_[index] = s;
3591 }
3592
3593 // Add a statement before another statement.
3594
3595 void
3596 Block::insert_statement_before(size_t index, Statement* s)
3597 {
3598 go_assert(index < this->statements_.size());
3599 this->statements_.insert(this->statements_.begin() + index, s);
3600 }
3601
3602 // Add a statement after another statement.
3603
3604 void
3605 Block::insert_statement_after(size_t index, Statement* s)
3606 {
3607 go_assert(index < this->statements_.size());
3608 this->statements_.insert(this->statements_.begin() + index + 1, s);
3609 }
3610
3611 // Traverse the tree.
3612
3613 int
3614 Block::traverse(Traverse* traverse)
3615 {
3616 unsigned int traverse_mask = traverse->traverse_mask();
3617
3618 if ((traverse_mask & Traverse::traverse_blocks) != 0)
3619 {
3620 int t = traverse->block(this);
3621 if (t == TRAVERSE_EXIT)
3622 return TRAVERSE_EXIT;
3623 else if (t == TRAVERSE_SKIP_COMPONENTS)
3624 return TRAVERSE_CONTINUE;
3625 }
3626
3627 if ((traverse_mask
3628 & (Traverse::traverse_variables
3629 | Traverse::traverse_constants
3630 | Traverse::traverse_expressions
3631 | Traverse::traverse_types)) != 0)
3632 {
3633 const unsigned int e_or_t = (Traverse::traverse_expressions
3634 | Traverse::traverse_types);
3635 const unsigned int e_or_t_or_s = (e_or_t
3636 | Traverse::traverse_statements);
3637 for (Bindings::const_definitions_iterator pb =
3638 this->bindings_->begin_definitions();
3639 pb != this->bindings_->end_definitions();
3640 ++pb)
3641 {
3642 int t = TRAVERSE_CONTINUE;
3643 switch ((*pb)->classification())
3644 {
3645 case Named_object::NAMED_OBJECT_CONST:
3646 if ((traverse_mask & Traverse::traverse_constants) != 0)
3647 t = traverse->constant(*pb, false);
3648 if (t == TRAVERSE_CONTINUE
3649 && (traverse_mask & e_or_t) != 0)
3650 {
3651 Type* tc = (*pb)->const_value()->type();
3652 if (tc != NULL
3653 && Type::traverse(tc, traverse) == TRAVERSE_EXIT)
3654 return TRAVERSE_EXIT;
3655 t = (*pb)->const_value()->traverse_expression(traverse);
3656 }
3657 break;
3658
3659 case Named_object::NAMED_OBJECT_VAR:
3660 case Named_object::NAMED_OBJECT_RESULT_VAR:
3661 if ((traverse_mask & Traverse::traverse_variables) != 0)
3662 t = traverse->variable(*pb);
3663 if (t == TRAVERSE_CONTINUE
3664 && (traverse_mask & e_or_t) != 0)
3665 {
3666 if ((*pb)->is_result_variable()
3667 || (*pb)->var_value()->has_type())
3668 {
3669 Type* tv = ((*pb)->is_variable()
3670 ? (*pb)->var_value()->type()
3671 : (*pb)->result_var_value()->type());
3672 if (tv != NULL
3673 && Type::traverse(tv, traverse) == TRAVERSE_EXIT)
3674 return TRAVERSE_EXIT;
3675 }
3676 }
3677 if (t == TRAVERSE_CONTINUE
3678 && (traverse_mask & e_or_t_or_s) != 0
3679 && (*pb)->is_variable())
3680 t = (*pb)->var_value()->traverse_expression(traverse,
3681 traverse_mask);
3682 break;
3683
3684 case Named_object::NAMED_OBJECT_FUNC:
3685 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3686 go_unreachable();
3687
3688 case Named_object::NAMED_OBJECT_TYPE:
3689 if ((traverse_mask & e_or_t) != 0)
3690 t = Type::traverse((*pb)->type_value(), traverse);
3691 break;
3692
3693 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3694 case Named_object::NAMED_OBJECT_UNKNOWN:
3695 case Named_object::NAMED_OBJECT_ERRONEOUS:
3696 break;
3697
3698 case Named_object::NAMED_OBJECT_PACKAGE:
3699 case Named_object::NAMED_OBJECT_SINK:
3700 go_unreachable();
3701
3702 default:
3703 go_unreachable();
3704 }
3705
3706 if (t == TRAVERSE_EXIT)
3707 return TRAVERSE_EXIT;
3708 }
3709 }
3710
3711 // No point in checking traverse_mask here--if we got here we always
3712 // want to walk the statements. The traversal can insert new
3713 // statements before or after the current statement. Inserting
3714 // statements before the current statement requires updating I via
3715 // the pointer; those statements will not be traversed. Any new
3716 // statements inserted after the current statement will be traversed
3717 // in their turn.
3718 for (size_t i = 0; i < this->statements_.size(); ++i)
3719 {
3720 if (this->statements_[i]->traverse(this, &i, traverse) == TRAVERSE_EXIT)
3721 return TRAVERSE_EXIT;
3722 }
3723
3724 return TRAVERSE_CONTINUE;
3725 }
3726
3727 // Work out types for unspecified variables and constants.
3728
3729 void
3730 Block::determine_types()
3731 {
3732 for (Bindings::const_definitions_iterator pb =
3733 this->bindings_->begin_definitions();
3734 pb != this->bindings_->end_definitions();
3735 ++pb)
3736 {
3737 if ((*pb)->is_variable())
3738 (*pb)->var_value()->determine_type();
3739 else if ((*pb)->is_const())
3740 (*pb)->const_value()->determine_type();
3741 }
3742
3743 for (std::vector<Statement*>::const_iterator ps = this->statements_.begin();
3744 ps != this->statements_.end();
3745 ++ps)
3746 (*ps)->determine_types();
3747 }
3748
3749 // Return true if the statements in this block may fall through.
3750
3751 bool
3752 Block::may_fall_through() const
3753 {
3754 if (this->statements_.empty())
3755 return true;
3756 return this->statements_.back()->may_fall_through();
3757 }
3758
3759 // Convert a block to the backend representation.
3760
3761 Bblock*
3762 Block::get_backend(Translate_context* context)
3763 {
3764 Gogo* gogo = context->gogo();
3765 Named_object* function = context->function();
3766 std::vector<Bvariable*> vars;
3767 vars.reserve(this->bindings_->size_definitions());
3768 for (Bindings::const_definitions_iterator pv =
3769 this->bindings_->begin_definitions();
3770 pv != this->bindings_->end_definitions();
3771 ++pv)
3772 {
3773 if ((*pv)->is_variable() && !(*pv)->var_value()->is_parameter())
3774 vars.push_back((*pv)->get_backend_variable(gogo, function));
3775 }
3776
3777 // FIXME: Permitting FUNCTION to be NULL here is a temporary measure
3778 // until we have a proper representation of the init function.
3779 Bfunction* bfunction;
3780 if (function == NULL)
3781 bfunction = NULL;
3782 else
3783 bfunction = tree_to_function(function->func_value()->get_decl());
3784 Bblock* ret = context->backend()->block(bfunction, context->bblock(),
3785 vars, this->start_location_,
3786 this->end_location_);
3787
3788 Translate_context subcontext(gogo, function, this, ret);
3789 std::vector<Bstatement*> bstatements;
3790 bstatements.reserve(this->statements_.size());
3791 for (std::vector<Statement*>::const_iterator p = this->statements_.begin();
3792 p != this->statements_.end();
3793 ++p)
3794 bstatements.push_back((*p)->get_backend(&subcontext));
3795
3796 context->backend()->block_add_statements(ret, bstatements);
3797
3798 return ret;
3799 }
3800
3801 // Class Bindings_snapshot.
3802
3803 Bindings_snapshot::Bindings_snapshot(const Block* b, Location location)
3804 : block_(b), counts_(), location_(location)
3805 {
3806 while (b != NULL)
3807 {
3808 this->counts_.push_back(b->bindings()->size_definitions());
3809 b = b->enclosing();
3810 }
3811 }
3812
3813 // Report errors appropriate for a goto from B to this.
3814
3815 void
3816 Bindings_snapshot::check_goto_from(const Block* b, Location loc)
3817 {
3818 size_t dummy;
3819 if (!this->check_goto_block(loc, b, this->block_, &dummy))
3820 return;
3821 this->check_goto_defs(loc, this->block_,
3822 this->block_->bindings()->size_definitions(),
3823 this->counts_[0]);
3824 }
3825
3826 // Report errors appropriate for a goto from this to B.
3827
3828 void
3829 Bindings_snapshot::check_goto_to(const Block* b)
3830 {
3831 size_t index;
3832 if (!this->check_goto_block(this->location_, this->block_, b, &index))
3833 return;
3834 this->check_goto_defs(this->location_, b, this->counts_[index],
3835 b->bindings()->size_definitions());
3836 }
3837
3838 // Report errors appropriate for a goto at LOC from BFROM to BTO.
3839 // Return true if all is well, false if we reported an error. If this
3840 // returns true, it sets *PINDEX to the number of blocks BTO is above
3841 // BFROM.
3842
3843 bool
3844 Bindings_snapshot::check_goto_block(Location loc, const Block* bfrom,
3845 const Block* bto, size_t* pindex)
3846 {
3847 // It is an error if BTO is not either BFROM or above BFROM.
3848 size_t index = 0;
3849 for (const Block* pb = bfrom; pb != bto; pb = pb->enclosing(), ++index)
3850 {
3851 if (pb == NULL)
3852 {
3853 error_at(loc, "goto jumps into block");
3854 inform(bto->start_location(), "goto target block starts here");
3855 return false;
3856 }
3857 }
3858 *pindex = index;
3859 return true;
3860 }
3861
3862 // Report errors appropriate for a goto at LOC ending at BLOCK, where
3863 // CFROM is the number of names defined at the point of the goto and
3864 // CTO is the number of names defined at the point of the label.
3865
3866 void
3867 Bindings_snapshot::check_goto_defs(Location loc, const Block* block,
3868 size_t cfrom, size_t cto)
3869 {
3870 if (cfrom < cto)
3871 {
3872 Bindings::const_definitions_iterator p =
3873 block->bindings()->begin_definitions();
3874 for (size_t i = 0; i < cfrom; ++i)
3875 {
3876 go_assert(p != block->bindings()->end_definitions());
3877 ++p;
3878 }
3879 go_assert(p != block->bindings()->end_definitions());
3880
3881 std::string n = (*p)->message_name();
3882 error_at(loc, "goto jumps over declaration of %qs", n.c_str());
3883 inform((*p)->location(), "%qs defined here", n.c_str());
3884 }
3885 }
3886
3887 // Class Variable.
3888
3889 Variable::Variable(Type* type, Expression* init, bool is_global,
3890 bool is_parameter, bool is_receiver,
3891 Location location)
3892 : type_(type), init_(init), preinit_(NULL), location_(location),
3893 backend_(NULL), is_global_(is_global), is_parameter_(is_parameter),
3894 is_receiver_(is_receiver), is_varargs_parameter_(false), is_used_(false),
3895 is_address_taken_(false), is_non_escaping_address_taken_(false),
3896 seen_(false), init_is_lowered_(false), type_from_init_tuple_(false),
3897 type_from_range_index_(false), type_from_range_value_(false),
3898 type_from_chan_element_(false), is_type_switch_var_(false),
3899 determined_type_(false)
3900 {
3901 go_assert(type != NULL || init != NULL);
3902 go_assert(!is_parameter || init == NULL);
3903 }
3904
3905 // Traverse the initializer expression.
3906
3907 int
3908 Variable::traverse_expression(Traverse* traverse, unsigned int traverse_mask)
3909 {
3910 if (this->preinit_ != NULL)
3911 {
3912 if (this->preinit_->traverse(traverse) == TRAVERSE_EXIT)
3913 return TRAVERSE_EXIT;
3914 }
3915 if (this->init_ != NULL
3916 && ((traverse_mask
3917 & (Traverse::traverse_expressions | Traverse::traverse_types))
3918 != 0))
3919 {
3920 if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT)
3921 return TRAVERSE_EXIT;
3922 }
3923 return TRAVERSE_CONTINUE;
3924 }
3925
3926 // Lower the initialization expression after parsing is complete.
3927
3928 void
3929 Variable::lower_init_expression(Gogo* gogo, Named_object* function,
3930 Statement_inserter* inserter)
3931 {
3932 Named_object* dep = gogo->var_depends_on(this);
3933 if (dep != NULL && dep->is_variable())
3934 dep->var_value()->lower_init_expression(gogo, function, inserter);
3935
3936 if (this->init_ != NULL && !this->init_is_lowered_)
3937 {
3938 if (this->seen_)
3939 {
3940 // We will give an error elsewhere, this is just to prevent
3941 // an infinite loop.
3942 return;
3943 }
3944 this->seen_ = true;
3945
3946 Statement_inserter global_inserter;
3947 if (this->is_global_)
3948 {
3949 global_inserter = Statement_inserter(gogo, this);
3950 inserter = &global_inserter;
3951 }
3952
3953 gogo->lower_expression(function, inserter, &this->init_);
3954
3955 this->seen_ = false;
3956
3957 this->init_is_lowered_ = true;
3958 }
3959 }
3960
3961 // Get the preinit block.
3962
3963 Block*
3964 Variable::preinit_block(Gogo* gogo)
3965 {
3966 go_assert(this->is_global_);
3967 if (this->preinit_ == NULL)
3968 this->preinit_ = new Block(NULL, this->location());
3969
3970 // If a global variable has a preinitialization statement, then we
3971 // need to have an initialization function.
3972 gogo->set_need_init_fn();
3973
3974 return this->preinit_;
3975 }
3976
3977 // Add a statement to be run before the initialization expression.
3978
3979 void
3980 Variable::add_preinit_statement(Gogo* gogo, Statement* s)
3981 {
3982 Block* b = this->preinit_block(gogo);
3983 b->add_statement(s);
3984 b->set_end_location(s->location());
3985 }
3986
3987 // Whether this variable has a type.
3988
3989 bool
3990 Variable::has_type() const
3991 {
3992 if (this->type_ == NULL)
3993 return false;
3994
3995 // A variable created in a type switch case nil does not actually
3996 // have a type yet. It will be changed to use the initializer's
3997 // type in determine_type.
3998 if (this->is_type_switch_var_
3999 && this->type_->is_nil_constant_as_type())
4000 return false;
4001
4002 return true;
4003 }
4004
4005 // In an assignment which sets a variable to a tuple of EXPR, return
4006 // the type of the first element of the tuple.
4007
4008 Type*
4009 Variable::type_from_tuple(Expression* expr, bool report_error) const
4010 {
4011 if (expr->map_index_expression() != NULL)
4012 {
4013 Map_type* mt = expr->map_index_expression()->get_map_type();
4014 if (mt == NULL)
4015 return Type::make_error_type();
4016 return mt->val_type();
4017 }
4018 else if (expr->receive_expression() != NULL)
4019 {
4020 Expression* channel = expr->receive_expression()->channel();
4021 Type* channel_type = channel->type();
4022 if (channel_type->channel_type() == NULL)
4023 return Type::make_error_type();
4024 return channel_type->channel_type()->element_type();
4025 }
4026 else
4027 {
4028 if (report_error)
4029 error_at(this->location(), "invalid tuple definition");
4030 return Type::make_error_type();
4031 }
4032 }
4033
4034 // Given EXPR used in a range clause, return either the index type or
4035 // the value type of the range, depending upon GET_INDEX_TYPE.
4036
4037 Type*
4038 Variable::type_from_range(Expression* expr, bool get_index_type,
4039 bool report_error) const
4040 {
4041 Type* t = expr->type();
4042 if (t->array_type() != NULL
4043 || (t->points_to() != NULL
4044 && t->points_to()->array_type() != NULL
4045 && !t->points_to()->is_slice_type()))
4046 {
4047 if (get_index_type)
4048 return Type::lookup_integer_type("int");
4049 else
4050 return t->deref()->array_type()->element_type();
4051 }
4052 else if (t->is_string_type())
4053 {
4054 if (get_index_type)
4055 return Type::lookup_integer_type("int");
4056 else
4057 return Type::lookup_integer_type("int32");
4058 }
4059 else if (t->map_type() != NULL)
4060 {
4061 if (get_index_type)
4062 return t->map_type()->key_type();
4063 else
4064 return t->map_type()->val_type();
4065 }
4066 else if (t->channel_type() != NULL)
4067 {
4068 if (get_index_type)
4069 return t->channel_type()->element_type();
4070 else
4071 {
4072 if (report_error)
4073 error_at(this->location(),
4074 "invalid definition of value variable for channel range");
4075 return Type::make_error_type();
4076 }
4077 }
4078 else
4079 {
4080 if (report_error)
4081 error_at(this->location(), "invalid type for range clause");
4082 return Type::make_error_type();
4083 }
4084 }
4085
4086 // EXPR should be a channel. Return the channel's element type.
4087
4088 Type*
4089 Variable::type_from_chan_element(Expression* expr, bool report_error) const
4090 {
4091 Type* t = expr->type();
4092 if (t->channel_type() != NULL)
4093 return t->channel_type()->element_type();
4094 else
4095 {
4096 if (report_error)
4097 error_at(this->location(), "expected channel");
4098 return Type::make_error_type();
4099 }
4100 }
4101
4102 // Return the type of the Variable. This may be called before
4103 // Variable::determine_type is called, which means that we may need to
4104 // get the type from the initializer. FIXME: If we combine lowering
4105 // with type determination, then this should be unnecessary.
4106
4107 Type*
4108 Variable::type()
4109 {
4110 // A variable in a type switch with a nil case will have the wrong
4111 // type here. This gets fixed up in determine_type, below.
4112 Type* type = this->type_;
4113 Expression* init = this->init_;
4114 if (this->is_type_switch_var_
4115 && this->type_->is_nil_constant_as_type())
4116 {
4117 Type_guard_expression* tge = this->init_->type_guard_expression();
4118 go_assert(tge != NULL);
4119 init = tge->expr();
4120 type = NULL;
4121 }
4122
4123 if (this->seen_)
4124 {
4125 if (this->type_ == NULL || !this->type_->is_error_type())
4126 {
4127 error_at(this->location_, "variable initializer refers to itself");
4128 this->type_ = Type::make_error_type();
4129 }
4130 return this->type_;
4131 }
4132
4133 this->seen_ = true;
4134
4135 if (type != NULL)
4136 ;
4137 else if (this->type_from_init_tuple_)
4138 type = this->type_from_tuple(init, false);
4139 else if (this->type_from_range_index_ || this->type_from_range_value_)
4140 type = this->type_from_range(init, this->type_from_range_index_, false);
4141 else if (this->type_from_chan_element_)
4142 type = this->type_from_chan_element(init, false);
4143 else
4144 {
4145 go_assert(init != NULL);
4146 type = init->type();
4147 go_assert(type != NULL);
4148
4149 // Variables should not have abstract types.
4150 if (type->is_abstract())
4151 type = type->make_non_abstract_type();
4152
4153 if (type->is_void_type())
4154 type = Type::make_error_type();
4155 }
4156
4157 this->seen_ = false;
4158
4159 return type;
4160 }
4161
4162 // Fetch the type from a const pointer, in which case it should have
4163 // been set already.
4164
4165 Type*
4166 Variable::type() const
4167 {
4168 go_assert(this->type_ != NULL);
4169 return this->type_;
4170 }
4171
4172 // Set the type if necessary.
4173
4174 void
4175 Variable::determine_type()
4176 {
4177 if (this->determined_type_)
4178 return;
4179 this->determined_type_ = true;
4180
4181 if (this->preinit_ != NULL)
4182 this->preinit_->determine_types();
4183
4184 // A variable in a type switch with a nil case will have the wrong
4185 // type here. It will have an initializer which is a type guard.
4186 // We want to initialize it to the value without the type guard, and
4187 // use the type of that value as well.
4188 if (this->is_type_switch_var_ && this->type_->is_nil_constant_as_type())
4189 {
4190 Type_guard_expression* tge = this->init_->type_guard_expression();
4191 go_assert(tge != NULL);
4192 this->type_ = NULL;
4193 this->init_ = tge->expr();
4194 }
4195
4196 if (this->init_ == NULL)
4197 go_assert(this->type_ != NULL && !this->type_->is_abstract());
4198 else if (this->type_from_init_tuple_)
4199 {
4200 Expression *init = this->init_;
4201 init->determine_type_no_context();
4202 this->type_ = this->type_from_tuple(init, true);
4203 this->init_ = NULL;
4204 }
4205 else if (this->type_from_range_index_ || this->type_from_range_value_)
4206 {
4207 Expression* init = this->init_;
4208 init->determine_type_no_context();
4209 this->type_ = this->type_from_range(init, this->type_from_range_index_,
4210 true);
4211 this->init_ = NULL;
4212 }
4213 else if (this->type_from_chan_element_)
4214 {
4215 Expression* init = this->init_;
4216 init->determine_type_no_context();
4217 this->type_ = this->type_from_chan_element(init, true);
4218 this->init_ = NULL;
4219 }
4220 else
4221 {
4222 Type_context context(this->type_, false);
4223 this->init_->determine_type(&context);
4224 if (this->type_ == NULL)
4225 {
4226 Type* type = this->init_->type();
4227 go_assert(type != NULL);
4228 if (type->is_abstract())
4229 type = type->make_non_abstract_type();
4230
4231 if (type->is_void_type())
4232 {
4233 error_at(this->location_, "variable has no type");
4234 type = Type::make_error_type();
4235 }
4236 else if (type->is_nil_type())
4237 {
4238 error_at(this->location_, "variable defined to nil type");
4239 type = Type::make_error_type();
4240 }
4241 else if (type->is_call_multiple_result_type())
4242 {
4243 error_at(this->location_,
4244 "single variable set to multiple-value function call");
4245 type = Type::make_error_type();
4246 }
4247
4248 this->type_ = type;
4249 }
4250 }
4251 }
4252
4253 // Export the variable
4254
4255 void
4256 Variable::export_var(Export* exp, const std::string& name) const
4257 {
4258 go_assert(this->is_global_);
4259 exp->write_c_string("var ");
4260 exp->write_string(name);
4261 exp->write_c_string(" ");
4262 exp->write_type(this->type());
4263 exp->write_c_string(";\n");
4264 }
4265
4266 // Import a variable.
4267
4268 void
4269 Variable::import_var(Import* imp, std::string* pname, Type** ptype)
4270 {
4271 imp->require_c_string("var ");
4272 *pname = imp->read_identifier();
4273 imp->require_c_string(" ");
4274 *ptype = imp->read_type();
4275 imp->require_c_string(";\n");
4276 }
4277
4278 // Convert a variable to the backend representation.
4279
4280 Bvariable*
4281 Variable::get_backend_variable(Gogo* gogo, Named_object* function,
4282 const Package* package, const std::string& name)
4283 {
4284 if (this->backend_ == NULL)
4285 {
4286 Backend* backend = gogo->backend();
4287 Type* type = this->type_;
4288 if (type->is_error_type()
4289 || (type->is_undefined()
4290 && (!this->is_global_ || package == NULL)))
4291 this->backend_ = backend->error_variable();
4292 else
4293 {
4294 bool is_parameter = this->is_parameter_;
4295 if (this->is_receiver_ && type->points_to() == NULL)
4296 is_parameter = false;
4297 if (this->is_in_heap())
4298 {
4299 is_parameter = false;
4300 type = Type::make_pointer_type(type);
4301 }
4302
4303 std::string n = Gogo::unpack_hidden_name(name);
4304 Btype* btype = type->get_backend(gogo);
4305
4306 Bvariable* bvar;
4307 if (this->is_global_)
4308 bvar = backend->global_variable((package == NULL
4309 ? gogo->package_name()
4310 : package->package_name()),
4311 (package == NULL
4312 ? gogo->pkgpath_symbol()
4313 : package->pkgpath_symbol()),
4314 n,
4315 btype,
4316 package != NULL,
4317 Gogo::is_hidden_name(name),
4318 this->location_);
4319 else if (function == NULL)
4320 {
4321 go_assert(saw_errors());
4322 bvar = backend->error_variable();
4323 }
4324 else
4325 {
4326 tree fndecl = function->func_value()->get_decl();
4327 Bfunction* bfunction = tree_to_function(fndecl);
4328 bool is_address_taken = (this->is_non_escaping_address_taken_
4329 && !this->is_in_heap());
4330 if (is_parameter)
4331 bvar = backend->parameter_variable(bfunction, n, btype,
4332 is_address_taken,
4333 this->location_);
4334 else
4335 bvar = backend->local_variable(bfunction, n, btype,
4336 is_address_taken,
4337 this->location_);
4338 }
4339 this->backend_ = bvar;
4340 }
4341 }
4342 return this->backend_;
4343 }
4344
4345 // Class Result_variable.
4346
4347 // Convert a result variable to the backend representation.
4348
4349 Bvariable*
4350 Result_variable::get_backend_variable(Gogo* gogo, Named_object* function,
4351 const std::string& name)
4352 {
4353 if (this->backend_ == NULL)
4354 {
4355 Backend* backend = gogo->backend();
4356 Type* type = this->type_;
4357 if (type->is_error())
4358 this->backend_ = backend->error_variable();
4359 else
4360 {
4361 if (this->is_in_heap())
4362 type = Type::make_pointer_type(type);
4363 Btype* btype = type->get_backend(gogo);
4364 tree fndecl = function->func_value()->get_decl();
4365 Bfunction* bfunction = tree_to_function(fndecl);
4366 std::string n = Gogo::unpack_hidden_name(name);
4367 bool is_address_taken = (this->is_non_escaping_address_taken_
4368 && !this->is_in_heap());
4369 this->backend_ = backend->local_variable(bfunction, n, btype,
4370 is_address_taken,
4371 this->location_);
4372 }
4373 }
4374 return this->backend_;
4375 }
4376
4377 // Class Named_constant.
4378
4379 // Traverse the initializer expression.
4380
4381 int
4382 Named_constant::traverse_expression(Traverse* traverse)
4383 {
4384 return Expression::traverse(&this->expr_, traverse);
4385 }
4386
4387 // Determine the type of the constant.
4388
4389 void
4390 Named_constant::determine_type()
4391 {
4392 if (this->type_ != NULL)
4393 {
4394 Type_context context(this->type_, false);
4395 this->expr_->determine_type(&context);
4396 }
4397 else
4398 {
4399 // A constant may have an abstract type.
4400 Type_context context(NULL, true);
4401 this->expr_->determine_type(&context);
4402 this->type_ = this->expr_->type();
4403 go_assert(this->type_ != NULL);
4404 }
4405 }
4406
4407 // Indicate that we found and reported an error for this constant.
4408
4409 void
4410 Named_constant::set_error()
4411 {
4412 this->type_ = Type::make_error_type();
4413 this->expr_ = Expression::make_error(this->location_);
4414 }
4415
4416 // Export a constant.
4417
4418 void
4419 Named_constant::export_const(Export* exp, const std::string& name) const
4420 {
4421 exp->write_c_string("const ");
4422 exp->write_string(name);
4423 exp->write_c_string(" ");
4424 if (!this->type_->is_abstract())
4425 {
4426 exp->write_type(this->type_);
4427 exp->write_c_string(" ");
4428 }
4429 exp->write_c_string("= ");
4430 this->expr()->export_expression(exp);
4431 exp->write_c_string(";\n");
4432 }
4433
4434 // Import a constant.
4435
4436 void
4437 Named_constant::import_const(Import* imp, std::string* pname, Type** ptype,
4438 Expression** pexpr)
4439 {
4440 imp->require_c_string("const ");
4441 *pname = imp->read_identifier();
4442 imp->require_c_string(" ");
4443 if (imp->peek_char() == '=')
4444 *ptype = NULL;
4445 else
4446 {
4447 *ptype = imp->read_type();
4448 imp->require_c_string(" ");
4449 }
4450 imp->require_c_string("= ");
4451 *pexpr = Expression::import_expression(imp);
4452 imp->require_c_string(";\n");
4453 }
4454
4455 // Add a method.
4456
4457 Named_object*
4458 Type_declaration::add_method(const std::string& name, Function* function)
4459 {
4460 Named_object* ret = Named_object::make_function(name, NULL, function);
4461 this->methods_.push_back(ret);
4462 return ret;
4463 }
4464
4465 // Add a method declaration.
4466
4467 Named_object*
4468 Type_declaration::add_method_declaration(const std::string& name,
4469 Package* package,
4470 Function_type* type,
4471 Location location)
4472 {
4473 Named_object* ret = Named_object::make_function_declaration(name, package,
4474 type, location);
4475 this->methods_.push_back(ret);
4476 return ret;
4477 }
4478
4479 // Return whether any methods ere defined.
4480
4481 bool
4482 Type_declaration::has_methods() const
4483 {
4484 return !this->methods_.empty();
4485 }
4486
4487 // Define methods for the real type.
4488
4489 void
4490 Type_declaration::define_methods(Named_type* nt)
4491 {
4492 for (Methods::const_iterator p = this->methods_.begin();
4493 p != this->methods_.end();
4494 ++p)
4495 nt->add_existing_method(*p);
4496 }
4497
4498 // We are using the type. Return true if we should issue a warning.
4499
4500 bool
4501 Type_declaration::using_type()
4502 {
4503 bool ret = !this->issued_warning_;
4504 this->issued_warning_ = true;
4505 return ret;
4506 }
4507
4508 // Class Unknown_name.
4509
4510 // Set the real named object.
4511
4512 void
4513 Unknown_name::set_real_named_object(Named_object* no)
4514 {
4515 go_assert(this->real_named_object_ == NULL);
4516 go_assert(!no->is_unknown());
4517 this->real_named_object_ = no;
4518 }
4519
4520 // Class Named_object.
4521
4522 Named_object::Named_object(const std::string& name,
4523 const Package* package,
4524 Classification classification)
4525 : name_(name), package_(package), classification_(classification),
4526 tree_(NULL)
4527 {
4528 if (Gogo::is_sink_name(name))
4529 go_assert(classification == NAMED_OBJECT_SINK);
4530 }
4531
4532 // Make an unknown name. This is used by the parser. The name must
4533 // be resolved later. Unknown names are only added in the current
4534 // package.
4535
4536 Named_object*
4537 Named_object::make_unknown_name(const std::string& name,
4538 Location location)
4539 {
4540 Named_object* named_object = new Named_object(name, NULL,
4541 NAMED_OBJECT_UNKNOWN);
4542 Unknown_name* value = new Unknown_name(location);
4543 named_object->u_.unknown_value = value;
4544 return named_object;
4545 }
4546
4547 // Make a constant.
4548
4549 Named_object*
4550 Named_object::make_constant(const Typed_identifier& tid,
4551 const Package* package, Expression* expr,
4552 int iota_value)
4553 {
4554 Named_object* named_object = new Named_object(tid.name(), package,
4555 NAMED_OBJECT_CONST);
4556 Named_constant* named_constant = new Named_constant(tid.type(), expr,
4557 iota_value,
4558 tid.location());
4559 named_object->u_.const_value = named_constant;
4560 return named_object;
4561 }
4562
4563 // Make a named type.
4564
4565 Named_object*
4566 Named_object::make_type(const std::string& name, const Package* package,
4567 Type* type, Location location)
4568 {
4569 Named_object* named_object = new Named_object(name, package,
4570 NAMED_OBJECT_TYPE);
4571 Named_type* named_type = Type::make_named_type(named_object, type, location);
4572 named_object->u_.type_value = named_type;
4573 return named_object;
4574 }
4575
4576 // Make a type declaration.
4577
4578 Named_object*
4579 Named_object::make_type_declaration(const std::string& name,
4580 const Package* package,
4581 Location location)
4582 {
4583 Named_object* named_object = new Named_object(name, package,
4584 NAMED_OBJECT_TYPE_DECLARATION);
4585 Type_declaration* type_declaration = new Type_declaration(location);
4586 named_object->u_.type_declaration = type_declaration;
4587 return named_object;
4588 }
4589
4590 // Make a variable.
4591
4592 Named_object*
4593 Named_object::make_variable(const std::string& name, const Package* package,
4594 Variable* variable)
4595 {
4596 Named_object* named_object = new Named_object(name, package,
4597 NAMED_OBJECT_VAR);
4598 named_object->u_.var_value = variable;
4599 return named_object;
4600 }
4601
4602 // Make a result variable.
4603
4604 Named_object*
4605 Named_object::make_result_variable(const std::string& name,
4606 Result_variable* result)
4607 {
4608 Named_object* named_object = new Named_object(name, NULL,
4609 NAMED_OBJECT_RESULT_VAR);
4610 named_object->u_.result_var_value = result;
4611 return named_object;
4612 }
4613
4614 // Make a sink. This is used for the special blank identifier _.
4615
4616 Named_object*
4617 Named_object::make_sink()
4618 {
4619 return new Named_object("_", NULL, NAMED_OBJECT_SINK);
4620 }
4621
4622 // Make a named function.
4623
4624 Named_object*
4625 Named_object::make_function(const std::string& name, const Package* package,
4626 Function* function)
4627 {
4628 Named_object* named_object = new Named_object(name, package,
4629 NAMED_OBJECT_FUNC);
4630 named_object->u_.func_value = function;
4631 return named_object;
4632 }
4633
4634 // Make a function declaration.
4635
4636 Named_object*
4637 Named_object::make_function_declaration(const std::string& name,
4638 const Package* package,
4639 Function_type* fntype,
4640 Location location)
4641 {
4642 Named_object* named_object = new Named_object(name, package,
4643 NAMED_OBJECT_FUNC_DECLARATION);
4644 Function_declaration *func_decl = new Function_declaration(fntype, location);
4645 named_object->u_.func_declaration_value = func_decl;
4646 return named_object;
4647 }
4648
4649 // Make a package.
4650
4651 Named_object*
4652 Named_object::make_package(const std::string& alias, Package* package)
4653 {
4654 Named_object* named_object = new Named_object(alias, NULL,
4655 NAMED_OBJECT_PACKAGE);
4656 named_object->u_.package_value = package;
4657 return named_object;
4658 }
4659
4660 // Return the name to use in an error message.
4661
4662 std::string
4663 Named_object::message_name() const
4664 {
4665 if (this->package_ == NULL)
4666 return Gogo::message_name(this->name_);
4667 std::string ret;
4668 if (this->package_->has_package_name())
4669 ret = this->package_->package_name();
4670 else
4671 ret = this->package_->pkgpath();
4672 ret = Gogo::message_name(ret);
4673 ret += '.';
4674 ret += Gogo::message_name(this->name_);
4675 return ret;
4676 }
4677
4678 // Set the type when a declaration is defined.
4679
4680 void
4681 Named_object::set_type_value(Named_type* named_type)
4682 {
4683 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
4684 Type_declaration* td = this->u_.type_declaration;
4685 td->define_methods(named_type);
4686 unsigned int index;
4687 Named_object* in_function = td->in_function(&index);
4688 if (in_function != NULL)
4689 named_type->set_in_function(in_function, index);
4690 delete td;
4691 this->classification_ = NAMED_OBJECT_TYPE;
4692 this->u_.type_value = named_type;
4693 }
4694
4695 // Define a function which was previously declared.
4696
4697 void
4698 Named_object::set_function_value(Function* function)
4699 {
4700 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
4701 this->classification_ = NAMED_OBJECT_FUNC;
4702 // FIXME: We should free the old value.
4703 this->u_.func_value = function;
4704 }
4705
4706 // Declare an unknown object as a type declaration.
4707
4708 void
4709 Named_object::declare_as_type()
4710 {
4711 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
4712 Unknown_name* unk = this->u_.unknown_value;
4713 this->classification_ = NAMED_OBJECT_TYPE_DECLARATION;
4714 this->u_.type_declaration = new Type_declaration(unk->location());
4715 delete unk;
4716 }
4717
4718 // Return the location of a named object.
4719
4720 Location
4721 Named_object::location() const
4722 {
4723 switch (this->classification_)
4724 {
4725 default:
4726 case NAMED_OBJECT_UNINITIALIZED:
4727 go_unreachable();
4728
4729 case NAMED_OBJECT_ERRONEOUS:
4730 return Linemap::unknown_location();
4731
4732 case NAMED_OBJECT_UNKNOWN:
4733 return this->unknown_value()->location();
4734
4735 case NAMED_OBJECT_CONST:
4736 return this->const_value()->location();
4737
4738 case NAMED_OBJECT_TYPE:
4739 return this->type_value()->location();
4740
4741 case NAMED_OBJECT_TYPE_DECLARATION:
4742 return this->type_declaration_value()->location();
4743
4744 case NAMED_OBJECT_VAR:
4745 return this->var_value()->location();
4746
4747 case NAMED_OBJECT_RESULT_VAR:
4748 return this->result_var_value()->location();
4749
4750 case NAMED_OBJECT_SINK:
4751 go_unreachable();
4752
4753 case NAMED_OBJECT_FUNC:
4754 return this->func_value()->location();
4755
4756 case NAMED_OBJECT_FUNC_DECLARATION:
4757 return this->func_declaration_value()->location();
4758
4759 case NAMED_OBJECT_PACKAGE:
4760 return this->package_value()->location();
4761 }
4762 }
4763
4764 // Export a named object.
4765
4766 void
4767 Named_object::export_named_object(Export* exp) const
4768 {
4769 switch (this->classification_)
4770 {
4771 default:
4772 case NAMED_OBJECT_UNINITIALIZED:
4773 case NAMED_OBJECT_UNKNOWN:
4774 go_unreachable();
4775
4776 case NAMED_OBJECT_ERRONEOUS:
4777 break;
4778
4779 case NAMED_OBJECT_CONST:
4780 this->const_value()->export_const(exp, this->name_);
4781 break;
4782
4783 case NAMED_OBJECT_TYPE:
4784 this->type_value()->export_named_type(exp, this->name_);
4785 break;
4786
4787 case NAMED_OBJECT_TYPE_DECLARATION:
4788 error_at(this->type_declaration_value()->location(),
4789 "attempt to export %<%s%> which was declared but not defined",
4790 this->message_name().c_str());
4791 break;
4792
4793 case NAMED_OBJECT_FUNC_DECLARATION:
4794 this->func_declaration_value()->export_func(exp, this->name_);
4795 break;
4796
4797 case NAMED_OBJECT_VAR:
4798 this->var_value()->export_var(exp, this->name_);
4799 break;
4800
4801 case NAMED_OBJECT_RESULT_VAR:
4802 case NAMED_OBJECT_SINK:
4803 go_unreachable();
4804
4805 case NAMED_OBJECT_FUNC:
4806 this->func_value()->export_func(exp, this->name_);
4807 break;
4808 }
4809 }
4810
4811 // Convert a variable to the backend representation.
4812
4813 Bvariable*
4814 Named_object::get_backend_variable(Gogo* gogo, Named_object* function)
4815 {
4816 if (this->classification_ == NAMED_OBJECT_VAR)
4817 return this->var_value()->get_backend_variable(gogo, function,
4818 this->package_, this->name_);
4819 else if (this->classification_ == NAMED_OBJECT_RESULT_VAR)
4820 return this->result_var_value()->get_backend_variable(gogo, function,
4821 this->name_);
4822 else
4823 go_unreachable();
4824 }
4825
4826 // Class Bindings.
4827
4828 Bindings::Bindings(Bindings* enclosing)
4829 : enclosing_(enclosing), named_objects_(), bindings_()
4830 {
4831 }
4832
4833 // Clear imports.
4834
4835 void
4836 Bindings::clear_file_scope()
4837 {
4838 Contour::iterator p = this->bindings_.begin();
4839 while (p != this->bindings_.end())
4840 {
4841 bool keep;
4842 if (p->second->package() != NULL)
4843 keep = false;
4844 else if (p->second->is_package())
4845 keep = false;
4846 else if (p->second->is_function()
4847 && !p->second->func_value()->type()->is_method()
4848 && Gogo::unpack_hidden_name(p->second->name()) == "init")
4849 keep = false;
4850 else
4851 keep = true;
4852
4853 if (keep)
4854 ++p;
4855 else
4856 p = this->bindings_.erase(p);
4857 }
4858 }
4859
4860 // Look up a symbol.
4861
4862 Named_object*
4863 Bindings::lookup(const std::string& name) const
4864 {
4865 Contour::const_iterator p = this->bindings_.find(name);
4866 if (p != this->bindings_.end())
4867 return p->second->resolve();
4868 else if (this->enclosing_ != NULL)
4869 return this->enclosing_->lookup(name);
4870 else
4871 return NULL;
4872 }
4873
4874 // Look up a symbol locally.
4875
4876 Named_object*
4877 Bindings::lookup_local(const std::string& name) const
4878 {
4879 Contour::const_iterator p = this->bindings_.find(name);
4880 if (p == this->bindings_.end())
4881 return NULL;
4882 return p->second;
4883 }
4884
4885 // Remove an object from a set of bindings. This is used for a
4886 // special case in thunks for functions which call recover.
4887
4888 void
4889 Bindings::remove_binding(Named_object* no)
4890 {
4891 Contour::iterator pb = this->bindings_.find(no->name());
4892 go_assert(pb != this->bindings_.end());
4893 this->bindings_.erase(pb);
4894 for (std::vector<Named_object*>::iterator pn = this->named_objects_.begin();
4895 pn != this->named_objects_.end();
4896 ++pn)
4897 {
4898 if (*pn == no)
4899 {
4900 this->named_objects_.erase(pn);
4901 return;
4902 }
4903 }
4904 go_unreachable();
4905 }
4906
4907 // Add a method to the list of objects. This is not added to the
4908 // lookup table. This is so that we have a single list of objects
4909 // declared at the top level, which we walk through when it's time to
4910 // convert to trees.
4911
4912 void
4913 Bindings::add_method(Named_object* method)
4914 {
4915 this->named_objects_.push_back(method);
4916 }
4917
4918 // Add a generic Named_object to a Contour.
4919
4920 Named_object*
4921 Bindings::add_named_object_to_contour(Contour* contour,
4922 Named_object* named_object)
4923 {
4924 go_assert(named_object == named_object->resolve());
4925 const std::string& name(named_object->name());
4926 go_assert(!Gogo::is_sink_name(name));
4927
4928 std::pair<Contour::iterator, bool> ins =
4929 contour->insert(std::make_pair(name, named_object));
4930 if (!ins.second)
4931 {
4932 // The name was already there.
4933 if (named_object->package() != NULL
4934 && ins.first->second->package() == named_object->package()
4935 && (ins.first->second->classification()
4936 == named_object->classification()))
4937 {
4938 // This is a second import of the same object.
4939 return ins.first->second;
4940 }
4941 ins.first->second = this->new_definition(ins.first->second,
4942 named_object);
4943 return ins.first->second;
4944 }
4945 else
4946 {
4947 // Don't push declarations on the list. We push them on when
4948 // and if we find the definitions. That way we genericize the
4949 // functions in order.
4950 if (!named_object->is_type_declaration()
4951 && !named_object->is_function_declaration()
4952 && !named_object->is_unknown())
4953 this->named_objects_.push_back(named_object);
4954 return named_object;
4955 }
4956 }
4957
4958 // We had an existing named object OLD_OBJECT, and we've seen a new
4959 // one NEW_OBJECT with the same name. FIXME: This does not free the
4960 // new object when we don't need it.
4961
4962 Named_object*
4963 Bindings::new_definition(Named_object* old_object, Named_object* new_object)
4964 {
4965 if (new_object->is_erroneous() && !old_object->is_erroneous())
4966 return new_object;
4967
4968 std::string reason;
4969 switch (old_object->classification())
4970 {
4971 default:
4972 case Named_object::NAMED_OBJECT_UNINITIALIZED:
4973 go_unreachable();
4974
4975 case Named_object::NAMED_OBJECT_ERRONEOUS:
4976 return old_object;
4977
4978 case Named_object::NAMED_OBJECT_UNKNOWN:
4979 {
4980 Named_object* real = old_object->unknown_value()->real_named_object();
4981 if (real != NULL)
4982 return this->new_definition(real, new_object);
4983 go_assert(!new_object->is_unknown());
4984 old_object->unknown_value()->set_real_named_object(new_object);
4985 if (!new_object->is_type_declaration()
4986 && !new_object->is_function_declaration())
4987 this->named_objects_.push_back(new_object);
4988 return new_object;
4989 }
4990
4991 case Named_object::NAMED_OBJECT_CONST:
4992 break;
4993
4994 case Named_object::NAMED_OBJECT_TYPE:
4995 if (new_object->is_type_declaration())
4996 return old_object;
4997 break;
4998
4999 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
5000 if (new_object->is_type_declaration())
5001 return old_object;
5002 if (new_object->is_type())
5003 {
5004 old_object->set_type_value(new_object->type_value());
5005 new_object->type_value()->set_named_object(old_object);
5006 this->named_objects_.push_back(old_object);
5007 return old_object;
5008 }
5009 break;
5010
5011 case Named_object::NAMED_OBJECT_VAR:
5012 case Named_object::NAMED_OBJECT_RESULT_VAR:
5013 // We have already given an error in the parser for cases where
5014 // one parameter or result variable redeclares another one.
5015 if ((new_object->is_variable()
5016 && new_object->var_value()->is_parameter())
5017 || new_object->is_result_variable())
5018 return old_object;
5019 break;
5020
5021 case Named_object::NAMED_OBJECT_SINK:
5022 go_unreachable();
5023
5024 case Named_object::NAMED_OBJECT_FUNC:
5025 if (new_object->is_function_declaration())
5026 {
5027 if (!new_object->func_declaration_value()->asm_name().empty())
5028 sorry("__asm__ for function definitions");
5029 Function_type* old_type = old_object->func_value()->type();
5030 Function_type* new_type =
5031 new_object->func_declaration_value()->type();
5032 if (old_type->is_valid_redeclaration(new_type, &reason))
5033 return old_object;
5034 }
5035 break;
5036
5037 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
5038 {
5039 Function_type* old_type = old_object->func_declaration_value()->type();
5040 if (new_object->is_function_declaration())
5041 {
5042 Function_type* new_type =
5043 new_object->func_declaration_value()->type();
5044 if (old_type->is_valid_redeclaration(new_type, &reason))
5045 return old_object;
5046 }
5047 if (new_object->is_function())
5048 {
5049 Function_type* new_type = new_object->func_value()->type();
5050 if (old_type->is_valid_redeclaration(new_type, &reason))
5051 {
5052 if (!old_object->func_declaration_value()->asm_name().empty())
5053 sorry("__asm__ for function definitions");
5054 old_object->set_function_value(new_object->func_value());
5055 this->named_objects_.push_back(old_object);
5056 return old_object;
5057 }
5058 }
5059 }
5060 break;
5061
5062 case Named_object::NAMED_OBJECT_PACKAGE:
5063 break;
5064 }
5065
5066 std::string n = old_object->message_name();
5067 if (reason.empty())
5068 error_at(new_object->location(), "redefinition of %qs", n.c_str());
5069 else
5070 error_at(new_object->location(), "redefinition of %qs: %s", n.c_str(),
5071 reason.c_str());
5072
5073 inform(old_object->location(), "previous definition of %qs was here",
5074 n.c_str());
5075
5076 return old_object;
5077 }
5078
5079 // Add a named type.
5080
5081 Named_object*
5082 Bindings::add_named_type(Named_type* named_type)
5083 {
5084 return this->add_named_object(named_type->named_object());
5085 }
5086
5087 // Add a function.
5088
5089 Named_object*
5090 Bindings::add_function(const std::string& name, const Package* package,
5091 Function* function)
5092 {
5093 return this->add_named_object(Named_object::make_function(name, package,
5094 function));
5095 }
5096
5097 // Add a function declaration.
5098
5099 Named_object*
5100 Bindings::add_function_declaration(const std::string& name,
5101 const Package* package,
5102 Function_type* type,
5103 Location location)
5104 {
5105 Named_object* no = Named_object::make_function_declaration(name, package,
5106 type, location);
5107 return this->add_named_object(no);
5108 }
5109
5110 // Define a type which was previously declared.
5111
5112 void
5113 Bindings::define_type(Named_object* no, Named_type* type)
5114 {
5115 no->set_type_value(type);
5116 this->named_objects_.push_back(no);
5117 }
5118
5119 // Mark all local variables as used. This is used for some types of
5120 // parse error.
5121
5122 void
5123 Bindings::mark_locals_used()
5124 {
5125 for (std::vector<Named_object*>::iterator p = this->named_objects_.begin();
5126 p != this->named_objects_.end();
5127 ++p)
5128 if ((*p)->is_variable())
5129 (*p)->var_value()->set_is_used();
5130 }
5131
5132 // Traverse bindings.
5133
5134 int
5135 Bindings::traverse(Traverse* traverse, bool is_global)
5136 {
5137 unsigned int traverse_mask = traverse->traverse_mask();
5138
5139 // We don't use an iterator because we permit the traversal to add
5140 // new global objects.
5141 const unsigned int e_or_t = (Traverse::traverse_expressions
5142 | Traverse::traverse_types);
5143 const unsigned int e_or_t_or_s = (e_or_t
5144 | Traverse::traverse_statements);
5145 for (size_t i = 0; i < this->named_objects_.size(); ++i)
5146 {
5147 Named_object* p = this->named_objects_[i];
5148 int t = TRAVERSE_CONTINUE;
5149 switch (p->classification())
5150 {
5151 case Named_object::NAMED_OBJECT_CONST:
5152 if ((traverse_mask & Traverse::traverse_constants) != 0)
5153 t = traverse->constant(p, is_global);
5154 if (t == TRAVERSE_CONTINUE
5155 && (traverse_mask & e_or_t) != 0)
5156 {
5157 Type* tc = p->const_value()->type();
5158 if (tc != NULL
5159 && Type::traverse(tc, traverse) == TRAVERSE_EXIT)
5160 return TRAVERSE_EXIT;
5161 t = p->const_value()->traverse_expression(traverse);
5162 }
5163 break;
5164
5165 case Named_object::NAMED_OBJECT_VAR:
5166 case Named_object::NAMED_OBJECT_RESULT_VAR:
5167 if ((traverse_mask & Traverse::traverse_variables) != 0)
5168 t = traverse->variable(p);
5169 if (t == TRAVERSE_CONTINUE
5170 && (traverse_mask & e_or_t) != 0)
5171 {
5172 if (p->is_result_variable()
5173 || p->var_value()->has_type())
5174 {
5175 Type* tv = (p->is_variable()
5176 ? p->var_value()->type()
5177 : p->result_var_value()->type());
5178 if (tv != NULL
5179 && Type::traverse(tv, traverse) == TRAVERSE_EXIT)
5180 return TRAVERSE_EXIT;
5181 }
5182 }
5183 if (t == TRAVERSE_CONTINUE
5184 && (traverse_mask & e_or_t_or_s) != 0
5185 && p->is_variable())
5186 t = p->var_value()->traverse_expression(traverse, traverse_mask);
5187 break;
5188
5189 case Named_object::NAMED_OBJECT_FUNC:
5190 if ((traverse_mask & Traverse::traverse_functions) != 0)
5191 t = traverse->function(p);
5192
5193 if (t == TRAVERSE_CONTINUE
5194 && (traverse_mask
5195 & (Traverse::traverse_variables
5196 | Traverse::traverse_constants
5197 | Traverse::traverse_functions
5198 | Traverse::traverse_blocks
5199 | Traverse::traverse_statements
5200 | Traverse::traverse_expressions
5201 | Traverse::traverse_types)) != 0)
5202 t = p->func_value()->traverse(traverse);
5203 break;
5204
5205 case Named_object::NAMED_OBJECT_PACKAGE:
5206 // These are traversed in Gogo::traverse.
5207 go_assert(is_global);
5208 break;
5209
5210 case Named_object::NAMED_OBJECT_TYPE:
5211 if ((traverse_mask & e_or_t) != 0)
5212 t = Type::traverse(p->type_value(), traverse);
5213 break;
5214
5215 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
5216 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
5217 case Named_object::NAMED_OBJECT_UNKNOWN:
5218 case Named_object::NAMED_OBJECT_ERRONEOUS:
5219 break;
5220
5221 case Named_object::NAMED_OBJECT_SINK:
5222 default:
5223 go_unreachable();
5224 }
5225
5226 if (t == TRAVERSE_EXIT)
5227 return TRAVERSE_EXIT;
5228 }
5229
5230 // If we need to traverse types, check the function declarations,
5231 // which have types. We don't need to check the type declarations,
5232 // as those are just names.
5233 if ((traverse_mask & e_or_t) != 0)
5234 {
5235 for (Bindings::const_declarations_iterator p =
5236 this->begin_declarations();
5237 p != this->end_declarations();
5238 ++p)
5239 {
5240 if (p->second->is_function_declaration())
5241 {
5242 if (Type::traverse(p->second->func_declaration_value()->type(),
5243 traverse)
5244 == TRAVERSE_EXIT)
5245 return TRAVERSE_EXIT;
5246 }
5247 }
5248 }
5249
5250 return TRAVERSE_CONTINUE;
5251 }
5252
5253 // Class Label.
5254
5255 // Clear any references to this label.
5256
5257 void
5258 Label::clear_refs()
5259 {
5260 for (std::vector<Bindings_snapshot*>::iterator p = this->refs_.begin();
5261 p != this->refs_.end();
5262 ++p)
5263 delete *p;
5264 this->refs_.clear();
5265 }
5266
5267 // Get the backend representation for a label.
5268
5269 Blabel*
5270 Label::get_backend_label(Translate_context* context)
5271 {
5272 if (this->blabel_ == NULL)
5273 {
5274 Function* function = context->function()->func_value();
5275 tree fndecl = function->get_decl();
5276 Bfunction* bfunction = tree_to_function(fndecl);
5277 this->blabel_ = context->backend()->label(bfunction, this->name_,
5278 this->location_);
5279 }
5280 return this->blabel_;
5281 }
5282
5283 // Return an expression for the address of this label.
5284
5285 Bexpression*
5286 Label::get_addr(Translate_context* context, Location location)
5287 {
5288 Blabel* label = this->get_backend_label(context);
5289 return context->backend()->label_address(label, location);
5290 }
5291
5292 // Class Unnamed_label.
5293
5294 // Get the backend representation for an unnamed label.
5295
5296 Blabel*
5297 Unnamed_label::get_blabel(Translate_context* context)
5298 {
5299 if (this->blabel_ == NULL)
5300 {
5301 Function* function = context->function()->func_value();
5302 tree fndecl = function->get_decl();
5303 Bfunction* bfunction = tree_to_function(fndecl);
5304 this->blabel_ = context->backend()->label(bfunction, "",
5305 this->location_);
5306 }
5307 return this->blabel_;
5308 }
5309
5310 // Return a statement which defines this unnamed label.
5311
5312 Bstatement*
5313 Unnamed_label::get_definition(Translate_context* context)
5314 {
5315 Blabel* blabel = this->get_blabel(context);
5316 return context->backend()->label_definition_statement(blabel);
5317 }
5318
5319 // Return a goto statement to this unnamed label.
5320
5321 Bstatement*
5322 Unnamed_label::get_goto(Translate_context* context, Location location)
5323 {
5324 Blabel* blabel = this->get_blabel(context);
5325 return context->backend()->goto_statement(blabel, location);
5326 }
5327
5328 // Class Package.
5329
5330 Package::Package(const std::string& pkgpath, Location location)
5331 : pkgpath_(pkgpath), pkgpath_symbol_(Gogo::pkgpath_for_symbol(pkgpath)),
5332 package_name_(), bindings_(new Bindings(NULL)), priority_(0),
5333 location_(location), used_(false), is_imported_(false),
5334 uses_sink_alias_(false)
5335 {
5336 go_assert(!pkgpath.empty());
5337
5338 }
5339
5340 // Set the package name.
5341
5342 void
5343 Package::set_package_name(const std::string& package_name, Location location)
5344 {
5345 go_assert(!package_name.empty());
5346 if (this->package_name_.empty())
5347 this->package_name_ = package_name;
5348 else if (this->package_name_ != package_name)
5349 error_at(location,
5350 "saw two different packages with the same package path %s: %s, %s",
5351 this->pkgpath_.c_str(), this->package_name_.c_str(),
5352 package_name.c_str());
5353 }
5354
5355 // Set the priority. We may see multiple priorities for an imported
5356 // package; we want to use the largest one.
5357
5358 void
5359 Package::set_priority(int priority)
5360 {
5361 if (priority > this->priority_)
5362 this->priority_ = priority;
5363 }
5364
5365 // Determine types of constants. Everything else in a package
5366 // (variables, function declarations) should already have a fixed
5367 // type. Constants may have abstract types.
5368
5369 void
5370 Package::determine_types()
5371 {
5372 Bindings* bindings = this->bindings_;
5373 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
5374 p != bindings->end_definitions();
5375 ++p)
5376 {
5377 if ((*p)->is_const())
5378 (*p)->const_value()->determine_type();
5379 }
5380 }
5381
5382 // Class Traverse.
5383
5384 // Destructor.
5385
5386 Traverse::~Traverse()
5387 {
5388 if (this->types_seen_ != NULL)
5389 delete this->types_seen_;
5390 if (this->expressions_seen_ != NULL)
5391 delete this->expressions_seen_;
5392 }
5393
5394 // Record that we are looking at a type, and return true if we have
5395 // already seen it.
5396
5397 bool
5398 Traverse::remember_type(const Type* type)
5399 {
5400 if (type->is_error_type())
5401 return true;
5402 go_assert((this->traverse_mask() & traverse_types) != 0
5403 || (this->traverse_mask() & traverse_expressions) != 0);
5404 // We mostly only have to remember named types. But it turns out
5405 // that an interface type can refer to itself without using a name
5406 // by relying on interface inheritance, as in
5407 // type I interface { F() interface{I} }
5408 if (type->classification() != Type::TYPE_NAMED
5409 && type->classification() != Type::TYPE_INTERFACE)
5410 return false;
5411 if (this->types_seen_ == NULL)
5412 this->types_seen_ = new Types_seen();
5413 std::pair<Types_seen::iterator, bool> ins = this->types_seen_->insert(type);
5414 return !ins.second;
5415 }
5416
5417 // Record that we are looking at an expression, and return true if we
5418 // have already seen it.
5419
5420 bool
5421 Traverse::remember_expression(const Expression* expression)
5422 {
5423 go_assert((this->traverse_mask() & traverse_types) != 0
5424 || (this->traverse_mask() & traverse_expressions) != 0);
5425 if (this->expressions_seen_ == NULL)
5426 this->expressions_seen_ = new Expressions_seen();
5427 std::pair<Expressions_seen::iterator, bool> ins =
5428 this->expressions_seen_->insert(expression);
5429 return !ins.second;
5430 }
5431
5432 // The default versions of these functions should never be called: the
5433 // traversal mask indicates which functions may be called.
5434
5435 int
5436 Traverse::variable(Named_object*)
5437 {
5438 go_unreachable();
5439 }
5440
5441 int
5442 Traverse::constant(Named_object*, bool)
5443 {
5444 go_unreachable();
5445 }
5446
5447 int
5448 Traverse::function(Named_object*)
5449 {
5450 go_unreachable();
5451 }
5452
5453 int
5454 Traverse::block(Block*)
5455 {
5456 go_unreachable();
5457 }
5458
5459 int
5460 Traverse::statement(Block*, size_t*, Statement*)
5461 {
5462 go_unreachable();
5463 }
5464
5465 int
5466 Traverse::expression(Expression**)
5467 {
5468 go_unreachable();
5469 }
5470
5471 int
5472 Traverse::type(Type*)
5473 {
5474 go_unreachable();
5475 }
5476
5477 // Class Statement_inserter.
5478
5479 void
5480 Statement_inserter::insert(Statement* s)
5481 {
5482 if (this->block_ != NULL)
5483 {
5484 go_assert(this->pindex_ != NULL);
5485 this->block_->insert_statement_before(*this->pindex_, s);
5486 ++*this->pindex_;
5487 }
5488 else if (this->var_ != NULL)
5489 this->var_->add_preinit_statement(this->gogo_, s);
5490 else
5491 go_assert(saw_errors());
5492 }