Branch data Line data Source code
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 <fstream>
10 : :
11 : : #include "filenames.h"
12 : :
13 : : #include "go-c.h"
14 : : #include "go-diagnostics.h"
15 : : #include "go-encode-id.h"
16 : : #include "go-dump.h"
17 : : #include "go-optimize.h"
18 : : #include "lex.h"
19 : : #include "types.h"
20 : : #include "statements.h"
21 : : #include "expressions.h"
22 : : #include "runtime.h"
23 : : #include "import.h"
24 : : #include "export.h"
25 : : #include "backend.h"
26 : : #include "gogo.h"
27 : :
28 : : // Class Gogo.
29 : :
30 : 4645 : Gogo::Gogo(Backend* backend, Linemap* linemap, int, int pointer_size)
31 : 4645 : : backend_(backend),
32 : 4645 : linemap_(linemap),
33 : 4645 : package_(NULL),
34 : 4645 : functions_(),
35 : 4645 : globals_(new Bindings(NULL)),
36 : 4645 : file_block_names_(),
37 : 4645 : imports_(),
38 : 4645 : imported_unsafe_(false),
39 : 4645 : current_file_imported_unsafe_(false),
40 : 4645 : current_file_imported_embed_(false),
41 : 4645 : packages_(),
42 : 4645 : init_functions_(),
43 : 4645 : var_deps_(),
44 : 4645 : need_init_fn_(false),
45 : 4645 : init_fn_name_(),
46 : 4645 : imported_init_fns_(),
47 : 4645 : pkgpath_(),
48 : 4645 : pkgpath_symbol_(),
49 : 4645 : prefix_(),
50 : 4645 : pkgpath_set_(false),
51 : 4645 : pkgpath_from_option_(false),
52 : 4645 : prefix_from_option_(false),
53 : 4645 : relative_import_path_(),
54 : 4645 : c_header_(),
55 : 4645 : import_map_(),
56 : 4645 : package_file_(),
57 : 4645 : embed_patterns_(),
58 : 4645 : embed_files_(),
59 : 4645 : check_divide_by_zero_(true),
60 : 4645 : check_divide_overflow_(true),
61 : 4645 : compiling_runtime_(false),
62 : 4645 : debug_escape_level_(0),
63 : 4645 : debug_optimization_(false),
64 : 4645 : nil_check_size_threshold_(4096),
65 : 4645 : need_eqtype_(false),
66 : 4645 : verify_types_(),
67 : 4645 : interface_types_(),
68 : 4645 : specific_type_functions_(),
69 : 4645 : specific_type_functions_are_written_(false),
70 : 4645 : named_types_are_converted_(false),
71 : 4645 : analysis_sets_(),
72 : 4645 : gc_roots_(),
73 : 4645 : type_descriptors_(),
74 : 4645 : imported_inlinable_functions_(),
75 : 4645 : imported_inline_functions_()
76 : : {
77 : 4645 : const Location loc = Linemap::predeclared_location();
78 : :
79 : 4645 : Named_type* uint8_type = Type::make_integer_type("uint8", true, 8,
80 : : RUNTIME_TYPE_KIND_UINT8);
81 : 4645 : this->add_named_type(uint8_type);
82 : 4645 : this->add_named_type(Type::make_integer_type("uint16", true, 16,
83 : : RUNTIME_TYPE_KIND_UINT16));
84 : 4645 : this->add_named_type(Type::make_integer_type("uint32", true, 32,
85 : : RUNTIME_TYPE_KIND_UINT32));
86 : 4645 : this->add_named_type(Type::make_integer_type("uint64", true, 64,
87 : : RUNTIME_TYPE_KIND_UINT64));
88 : :
89 : 4645 : this->add_named_type(Type::make_integer_type("int8", false, 8,
90 : : RUNTIME_TYPE_KIND_INT8));
91 : 4645 : this->add_named_type(Type::make_integer_type("int16", false, 16,
92 : : RUNTIME_TYPE_KIND_INT16));
93 : 4645 : Named_type* int32_type = Type::make_integer_type("int32", false, 32,
94 : : RUNTIME_TYPE_KIND_INT32);
95 : 4645 : this->add_named_type(int32_type);
96 : 4645 : this->add_named_type(Type::make_integer_type("int64", false, 64,
97 : : RUNTIME_TYPE_KIND_INT64));
98 : :
99 : 4645 : this->add_named_type(Type::make_float_type("float32", 32,
100 : : RUNTIME_TYPE_KIND_FLOAT32));
101 : 4645 : this->add_named_type(Type::make_float_type("float64", 64,
102 : : RUNTIME_TYPE_KIND_FLOAT64));
103 : :
104 : 4645 : this->add_named_type(Type::make_complex_type("complex64", 64,
105 : : RUNTIME_TYPE_KIND_COMPLEX64));
106 : 4645 : this->add_named_type(Type::make_complex_type("complex128", 128,
107 : : RUNTIME_TYPE_KIND_COMPLEX128));
108 : :
109 : 4645 : int int_type_size = pointer_size;
110 : 4645 : if (int_type_size < 32)
111 : : int_type_size = 32;
112 : 4645 : this->add_named_type(Type::make_integer_type("uint", true,
113 : : int_type_size,
114 : : RUNTIME_TYPE_KIND_UINT));
115 : 4645 : Named_type* int_type = Type::make_integer_type("int", false, int_type_size,
116 : : RUNTIME_TYPE_KIND_INT);
117 : 4645 : this->add_named_type(int_type);
118 : :
119 : 4645 : this->add_named_type(Type::make_integer_type("uintptr", true,
120 : : pointer_size,
121 : : RUNTIME_TYPE_KIND_UINTPTR));
122 : :
123 : : // "byte" is an alias for "uint8".
124 : 9290 : uint8_type->integer_type()->set_is_byte();
125 : 4645 : this->add_named_type(Type::make_integer_type_alias("byte", uint8_type));
126 : :
127 : : // "rune" is an alias for "int32".
128 : 9290 : int32_type->integer_type()->set_is_rune();
129 : 4645 : this->add_named_type(Type::make_integer_type_alias("rune", int32_type));
130 : :
131 : 4645 : this->add_named_type(Type::make_named_bool_type());
132 : :
133 : 4645 : this->add_named_type(Type::make_named_string_type());
134 : :
135 : : // "error" is interface { Error() string }.
136 : 4645 : {
137 : 4645 : Typed_identifier_list *methods = new Typed_identifier_list;
138 : 4645 : Typed_identifier_list *results = new Typed_identifier_list;
139 : 4645 : results->push_back(Typed_identifier("", Type::lookup_string_type(), loc));
140 : 4645 : Type *method_type = Type::make_function_type(NULL, NULL, results, loc);
141 : 4645 : methods->push_back(Typed_identifier("Error", method_type, loc));
142 : 4645 : Interface_type *error_iface = Type::make_interface_type(methods, loc);
143 : 4645 : error_iface->finalize_methods();
144 : 4645 : Named_type *error_type = Named_object::make_type("error", NULL, error_iface, loc)->type_value();
145 : 4645 : this->add_named_type(error_type);
146 : : }
147 : :
148 : : // "any" is an alias for the empty interface type.
149 : 4645 : {
150 : 4645 : Type* empty = Type::make_empty_interface_type(loc);
151 : 4645 : Named_object* no = Named_object::make_type("any", NULL, empty, loc);
152 : 4645 : Named_type* nt = no->type_value();
153 : 4645 : nt->set_is_alias();
154 : 4645 : this->add_named_type(nt);
155 : : }
156 : :
157 : 9290 : this->globals_->add_constant(Typed_identifier("true",
158 : : Type::make_boolean_type(),
159 : 4645 : loc),
160 : : NULL,
161 : : Expression::make_boolean(true, loc),
162 : : 0);
163 : 9290 : this->globals_->add_constant(Typed_identifier("false",
164 : : Type::make_boolean_type(),
165 : 4645 : loc),
166 : : NULL,
167 : : Expression::make_boolean(false, loc),
168 : : 0);
169 : :
170 : 9290 : this->globals_->add_constant(Typed_identifier("nil", Type::make_nil_type(),
171 : 4645 : loc),
172 : : NULL,
173 : : Expression::make_nil(loc),
174 : : 0);
175 : :
176 : 4645 : Type* abstract_int_type = Type::make_abstract_integer_type();
177 : 9290 : this->globals_->add_constant(Typed_identifier("iota", abstract_int_type,
178 : 4645 : loc),
179 : : NULL,
180 : : Expression::make_iota(),
181 : : 0);
182 : :
183 : 4645 : Function_type* new_type = Type::make_function_type(NULL, NULL, NULL, loc);
184 : 4645 : new_type->set_is_varargs();
185 : 4645 : new_type->set_is_builtin();
186 : 4645 : this->globals_->add_function_declaration("new", NULL, new_type, loc);
187 : :
188 : 4645 : Function_type* make_type = Type::make_function_type(NULL, NULL, NULL, loc);
189 : 4645 : make_type->set_is_varargs();
190 : 4645 : make_type->set_is_builtin();
191 : 4645 : this->globals_->add_function_declaration("make", NULL, make_type, loc);
192 : :
193 : 4645 : Typed_identifier_list* len_result = new Typed_identifier_list();
194 : 4645 : len_result->push_back(Typed_identifier("", int_type, loc));
195 : 4645 : Function_type* len_type = Type::make_function_type(NULL, NULL, len_result,
196 : : loc);
197 : 4645 : len_type->set_is_builtin();
198 : 4645 : this->globals_->add_function_declaration("len", NULL, len_type, loc);
199 : :
200 : 4645 : Typed_identifier_list* cap_result = new Typed_identifier_list();
201 : 4645 : cap_result->push_back(Typed_identifier("", int_type, loc));
202 : 4645 : Function_type* cap_type = Type::make_function_type(NULL, NULL, len_result,
203 : : loc);
204 : 4645 : cap_type->set_is_builtin();
205 : 4645 : this->globals_->add_function_declaration("cap", NULL, cap_type, loc);
206 : :
207 : 4645 : Function_type* print_type = Type::make_function_type(NULL, NULL, NULL, loc);
208 : 4645 : print_type->set_is_varargs();
209 : 4645 : print_type->set_is_builtin();
210 : 4645 : this->globals_->add_function_declaration("print", NULL, print_type, loc);
211 : :
212 : 4645 : print_type = Type::make_function_type(NULL, NULL, NULL, loc);
213 : 4645 : print_type->set_is_varargs();
214 : 4645 : print_type->set_is_builtin();
215 : 4645 : this->globals_->add_function_declaration("println", NULL, print_type, loc);
216 : :
217 : 4645 : Type *empty = Type::make_empty_interface_type(loc);
218 : 4645 : Typed_identifier_list* panic_parms = new Typed_identifier_list();
219 : 4645 : panic_parms->push_back(Typed_identifier("e", empty, loc));
220 : 4645 : Function_type *panic_type = Type::make_function_type(NULL, panic_parms,
221 : : NULL, loc);
222 : 4645 : panic_type->set_is_builtin();
223 : 4645 : this->globals_->add_function_declaration("panic", NULL, panic_type, loc);
224 : :
225 : 4645 : Typed_identifier_list* recover_result = new Typed_identifier_list();
226 : 4645 : recover_result->push_back(Typed_identifier("", empty, loc));
227 : 4645 : Function_type* recover_type = Type::make_function_type(NULL, NULL,
228 : : recover_result,
229 : : loc);
230 : 4645 : recover_type->set_is_builtin();
231 : 4645 : this->globals_->add_function_declaration("recover", NULL, recover_type, loc);
232 : :
233 : 4645 : Function_type* close_type = Type::make_function_type(NULL, NULL, NULL, loc);
234 : 4645 : close_type->set_is_varargs();
235 : 4645 : close_type->set_is_builtin();
236 : 4645 : this->globals_->add_function_declaration("close", NULL, close_type, loc);
237 : :
238 : 4645 : Typed_identifier_list* copy_result = new Typed_identifier_list();
239 : 4645 : copy_result->push_back(Typed_identifier("", int_type, loc));
240 : 4645 : Function_type* copy_type = Type::make_function_type(NULL, NULL,
241 : : copy_result, loc);
242 : 4645 : copy_type->set_is_varargs();
243 : 4645 : copy_type->set_is_builtin();
244 : 4645 : this->globals_->add_function_declaration("copy", NULL, copy_type, loc);
245 : :
246 : 4645 : Function_type* append_type = Type::make_function_type(NULL, NULL, NULL, loc);
247 : 4645 : append_type->set_is_varargs();
248 : 4645 : append_type->set_is_builtin();
249 : 4645 : this->globals_->add_function_declaration("append", NULL, append_type, loc);
250 : :
251 : 4645 : Function_type* complex_type = Type::make_function_type(NULL, NULL, NULL, loc);
252 : 4645 : complex_type->set_is_varargs();
253 : 4645 : complex_type->set_is_builtin();
254 : 4645 : this->globals_->add_function_declaration("complex", NULL, complex_type, loc);
255 : :
256 : 4645 : Function_type* real_type = Type::make_function_type(NULL, NULL, NULL, loc);
257 : 4645 : real_type->set_is_varargs();
258 : 4645 : real_type->set_is_builtin();
259 : 4645 : this->globals_->add_function_declaration("real", NULL, real_type, loc);
260 : :
261 : 4645 : Function_type* imag_type = Type::make_function_type(NULL, NULL, NULL, loc);
262 : 4645 : imag_type->set_is_varargs();
263 : 4645 : imag_type->set_is_builtin();
264 : 4645 : this->globals_->add_function_declaration("imag", NULL, imag_type, loc);
265 : :
266 : 4645 : Function_type* delete_type = Type::make_function_type(NULL, NULL, NULL, loc);
267 : 4645 : delete_type->set_is_varargs();
268 : 4645 : delete_type->set_is_builtin();
269 : 4645 : this->globals_->add_function_declaration("delete", NULL, delete_type, loc);
270 : 4645 : }
271 : :
272 : : std::string
273 : 241605 : Gogo::pkgpath_for_symbol(const std::string& pkgpath)
274 : : {
275 : 241605 : go_assert(!pkgpath.empty());
276 : 241605 : return go_encode_id(pkgpath);
277 : : }
278 : :
279 : : // Return a hash code for a string, given a starting hash.
280 : :
281 : : unsigned int
282 : 13116454 : Gogo::hash_string(const std::string& s, unsigned int h)
283 : : {
284 : 13116454 : const char* p = s.data();
285 : 13116454 : size_t len = s.length();
286 : 116425550 : for (; len > 0; --len)
287 : : {
288 : 103309096 : h ^= *p++;
289 : 103309096 : h*= 16777619;
290 : : }
291 : 13116454 : return h;
292 : : }
293 : :
294 : : // Get the package path to use for type reflection data. This should
295 : : // ideally be unique across the entire link.
296 : :
297 : : const std::string&
298 : 7559694 : Gogo::pkgpath() const
299 : : {
300 : 7559694 : go_assert(this->pkgpath_set_);
301 : 7559694 : return this->pkgpath_;
302 : : }
303 : :
304 : : // Set the package path from the -fgo-pkgpath command line option.
305 : :
306 : : void
307 : 2013 : Gogo::set_pkgpath(const std::string& arg)
308 : : {
309 : 2013 : go_assert(!this->pkgpath_set_);
310 : 2013 : this->pkgpath_ = arg;
311 : 2013 : this->pkgpath_set_ = true;
312 : 2013 : this->pkgpath_from_option_ = true;
313 : 2013 : }
314 : :
315 : : // Get the package path to use for symbol names.
316 : :
317 : : const std::string&
318 : 199176 : Gogo::pkgpath_symbol() const
319 : : {
320 : 199176 : go_assert(this->pkgpath_set_);
321 : 199176 : return this->pkgpath_symbol_;
322 : : }
323 : :
324 : : // Set the unique prefix to use to determine the package path, from
325 : : // the -fgo-prefix command line option.
326 : :
327 : : void
328 : 0 : Gogo::set_prefix(const std::string& arg)
329 : : {
330 : 0 : go_assert(!this->prefix_from_option_);
331 : 0 : this->prefix_ = arg;
332 : 0 : this->prefix_from_option_ = true;
333 : 0 : }
334 : :
335 : : // Munge name for use in an error message.
336 : :
337 : : std::string
338 : 275142 : Gogo::message_name(const std::string& name)
339 : : {
340 : 275142 : return go_localize_identifier(Gogo::unpack_hidden_name(name).c_str());
341 : : }
342 : :
343 : : // Get the package name.
344 : :
345 : : const std::string&
346 : 349422 : Gogo::package_name() const
347 : : {
348 : 349422 : go_assert(this->package_ != NULL);
349 : 349422 : return this->package_->package_name();
350 : : }
351 : :
352 : : // Set the package name.
353 : :
354 : : void
355 : 12706 : Gogo::set_package_name(const std::string& package_name,
356 : : Location location)
357 : : {
358 : 12706 : if (this->package_ != NULL)
359 : : {
360 : 8061 : if (this->package_->package_name() != package_name)
361 : 0 : go_error_at(location, "expected package %qs",
362 : 0 : Gogo::message_name(this->package_->package_name()).c_str());
363 : 8061 : return;
364 : : }
365 : :
366 : : // Now that we know the name of the package we are compiling, set
367 : : // the package path to use for reflect.Type.PkgPath and global
368 : : // symbol names.
369 : 4645 : if (this->pkgpath_set_)
370 : 2013 : this->pkgpath_symbol_ = Gogo::pkgpath_for_symbol(this->pkgpath_);
371 : : else
372 : : {
373 : 2632 : if (!this->prefix_from_option_ && package_name == "main")
374 : : {
375 : 1889 : this->pkgpath_ = package_name;
376 : 1889 : this->pkgpath_symbol_ = Gogo::pkgpath_for_symbol(package_name);
377 : : }
378 : : else
379 : : {
380 : 743 : if (!this->prefix_from_option_)
381 : 743 : this->prefix_ = "go";
382 : 743 : this->pkgpath_ = this->prefix_ + '.' + package_name;
383 : 1486 : this->pkgpath_symbol_ = (Gogo::pkgpath_for_symbol(this->prefix_) + '.'
384 : 2229 : + Gogo::pkgpath_for_symbol(package_name));
385 : : }
386 : 2632 : this->pkgpath_set_ = true;
387 : : }
388 : :
389 : 9290 : this->package_ = this->register_package(this->pkgpath_,
390 : 4645 : this->pkgpath_symbol_, location);
391 : 4645 : this->package_->set_package_name(package_name, location);
392 : :
393 : 4645 : if (this->is_main_package())
394 : : {
395 : : // Declare "main" as a function which takes no parameters and
396 : : // returns no value.
397 : 1889 : Location uloc = Linemap::unknown_location();
398 : 1889 : this->declare_function(Gogo::pack_hidden_name("main", false),
399 : : Type::make_function_type (NULL, NULL, NULL, uloc),
400 : : uloc);
401 : : }
402 : : }
403 : :
404 : : // Return whether this is the "main" package. This is not true if
405 : : // -fgo-pkgpath or -fgo-prefix was used.
406 : :
407 : : bool
408 : 39033 : Gogo::is_main_package() const
409 : : {
410 : 39033 : return (this->package_name() == "main"
411 : 19480 : && !this->pkgpath_from_option_
412 : 58461 : && !this->prefix_from_option_);
413 : : }
414 : :
415 : : // Import a package.
416 : :
417 : : void
418 : 42242 : Gogo::import_package(const std::string& filename,
419 : : const std::string& local_name,
420 : : bool is_local_name_exported,
421 : : bool must_exist,
422 : : Location location)
423 : : {
424 : 42242 : if (filename.empty())
425 : : {
426 : 1 : go_error_at(location, "import path is empty");
427 : 20435 : return;
428 : : }
429 : :
430 : 42241 : const char *pf = filename.data();
431 : 42241 : const char *pend = pf + filename.length();
432 : 406477 : while (pf < pend)
433 : : {
434 : 364237 : unsigned int c;
435 : 364237 : int adv = Lex::fetch_char(pf, &c);
436 : 364237 : if (adv == 0)
437 : : {
438 : 0 : go_error_at(location, "import path contains invalid UTF-8 sequence");
439 : 1 : return;
440 : : }
441 : 364237 : if (c == '\0')
442 : : {
443 : 0 : go_error_at(location, "import path contains NUL");
444 : 0 : return;
445 : : }
446 : 364237 : if (c < 0x20 || c == 0x7f)
447 : : {
448 : 1 : go_error_at(location, "import path contains control character");
449 : 1 : return;
450 : : }
451 : 364236 : if (c == '\\')
452 : : {
453 : 0 : go_error_at(location, "import path contains backslash; use slash");
454 : 0 : return;
455 : : }
456 : 364236 : if (Lex::is_unicode_space(c))
457 : : {
458 : 0 : go_error_at(location, "import path contains space character");
459 : 0 : return;
460 : : }
461 : 364236 : if (c < 0x7f && strchr("!\"#$%&'()*,:;<=>?[]^`{|}", c) != NULL)
462 : : {
463 : 0 : go_error_at(location,
464 : : "import path contains invalid character '%c'", c);
465 : 0 : return;
466 : : }
467 : 364236 : pf += adv;
468 : : }
469 : :
470 : 42240 : if (IS_ABSOLUTE_PATH(filename.c_str()))
471 : : {
472 : 0 : go_error_at(location, "import path cannot be absolute path");
473 : 0 : return;
474 : : }
475 : :
476 : 42240 : if (local_name == "init")
477 : 1 : go_error_at(location, "cannot import package as init");
478 : :
479 : 42240 : if (filename == "unsafe")
480 : : {
481 : 1471 : this->import_unsafe(local_name, is_local_name_exported, location);
482 : 1471 : this->current_file_imported_unsafe_ = true;
483 : 1471 : return;
484 : : }
485 : :
486 : 40769 : if (filename == "embed")
487 : 8 : this->current_file_imported_embed_ = true;
488 : :
489 : 40769 : Imports::const_iterator p = this->imports_.find(filename);
490 : 40769 : if (p != this->imports_.end())
491 : : {
492 : 18951 : Package* package = p->second;
493 : 18951 : package->set_location(location);
494 : 18951 : std::string ln = local_name;
495 : 18951 : bool is_ln_exported = is_local_name_exported;
496 : 18951 : if (ln.empty())
497 : : {
498 : 18415 : ln = package->package_name();
499 : 18415 : go_assert(!ln.empty());
500 : 18415 : is_ln_exported = Lex::is_exported_name(ln);
501 : : }
502 : 18951 : if (ln == "_")
503 : : ;
504 : 18842 : else if (ln == ".")
505 : : {
506 : 329 : Bindings* bindings = package->bindings();
507 : 62651 : for (Bindings::const_declarations_iterator pd =
508 : 329 : bindings->begin_declarations();
509 : 62651 : pd != bindings->end_declarations();
510 : 62322 : ++pd)
511 : 62322 : this->add_dot_import_object(pd->second);
512 : 329 : std::string dot_alias = "." + package->package_name();
513 : 329 : package->add_alias(dot_alias, location);
514 : 329 : }
515 : : else
516 : : {
517 : 18513 : package->add_alias(ln, location);
518 : 18513 : ln = this->pack_hidden_name(ln, is_ln_exported);
519 : 18513 : this->package_->bindings()->add_package(ln, package);
520 : : }
521 : 18951 : return;
522 : 18951 : }
523 : :
524 : : // If we are using an importcfg file we have to check two mappings.
525 : : // IMPORT_MAP_ is a mapping from package path to real package path,
526 : : // for vendoring. PACKAGE_FILE_ is a mapping from package path to
527 : : // file name, to find the file in the build cache.
528 : 21818 : std::string path = filename;
529 : 21818 : Unordered_map(std::string, std::string)::const_iterator pi;
530 : 21818 : pi = this->import_map_.find(filename);
531 : 21818 : if (pi != this->import_map_.end())
532 : 19 : path = pi->second;
533 : 21818 : pi = this->package_file_.find(path);
534 : 21818 : if (pi != this->package_file_.end())
535 : 702 : path = pi->second;
536 : :
537 : 43636 : Import::Stream* stream = Import::open_package(path, location,
538 : 21818 : this->relative_import_path_);
539 : 21818 : if (stream == NULL)
540 : : {
541 : 10 : if (must_exist)
542 : 0 : go_error_at(location, "import file %qs not found", filename.c_str());
543 : 10 : return;
544 : : }
545 : :
546 : 21808 : Import* imp = new Import(stream, location);
547 : 21808 : imp->register_builtin_types(this);
548 : 21808 : Package* package = imp->import(this, local_name, is_local_name_exported);
549 : 21808 : if (package != NULL)
550 : : {
551 : 21808 : if (package->pkgpath() == this->pkgpath())
552 : 0 : go_error_at(location,
553 : : ("imported package uses same package path as package "
554 : : "being compiled (see %<-fgo-pkgpath%> option)"));
555 : :
556 : 21808 : this->imports_.insert(std::make_pair(filename, package));
557 : : }
558 : :
559 : 21808 : imp->clear_stream();
560 : 21808 : delete stream;
561 : :
562 : : // FIXME: we never delete imp; we may need it for inlinable functions.
563 : 21818 : }
564 : :
565 : : Import_init *
566 : 1549811 : Gogo::lookup_init(const std::string& init_name)
567 : : {
568 : 1549811 : Import_init tmp("", init_name, -1);
569 : 1549811 : Import_init_set::iterator it = this->imported_init_fns_.find(&tmp);
570 : 1549811 : return (it != this->imported_init_fns_.end()) ? *it : NULL;
571 : 1549811 : }
572 : :
573 : : // Add an import control function for an imported package to the list.
574 : :
575 : : void
576 : 660202 : Gogo::add_import_init_fn(const std::string& package_name,
577 : : const std::string& init_name, int prio)
578 : : {
579 : 30250330 : for (Import_init_set::iterator p =
580 : 660202 : this->imported_init_fns_.begin();
581 : 30910532 : p != this->imported_init_fns_.end();
582 : 30250330 : ++p)
583 : : {
584 : 30748345 : Import_init *ii = (*p);
585 : 30748345 : if (ii->init_name() == init_name)
586 : : {
587 : : // If a test of package P1, built as part of package P1,
588 : : // imports package P2, and P2 imports P1 (perhaps
589 : : // indirectly), then we will see the same import name with
590 : : // different import priorities. That is OK, so don't give
591 : : // an error about it.
592 : 498015 : if (ii->package_name() != package_name)
593 : : {
594 : 0 : go_error_at(Linemap::unknown_location(),
595 : : "duplicate package initialization name %qs",
596 : 0 : Gogo::message_name(init_name).c_str());
597 : 0 : go_inform(Linemap::unknown_location(), "used by package %qs",
598 : 0 : Gogo::message_name(ii->package_name()).c_str());
599 : 0 : go_inform(Linemap::unknown_location(), " and by package %qs",
600 : 0 : Gogo::message_name(package_name).c_str());
601 : : }
602 : 498015 : ii->set_priority(prio);
603 : 498015 : return;
604 : : }
605 : : }
606 : :
607 : 162187 : Import_init* nii = new Import_init(package_name, init_name, prio);
608 : 162187 : this->imported_init_fns_.insert(nii);
609 : : }
610 : :
611 : : // Return whether we are at the global binding level.
612 : :
613 : : bool
614 : 710784 : Gogo::in_global_scope() const
615 : : {
616 : 710784 : return this->functions_.empty();
617 : : }
618 : :
619 : : // Return the current binding contour.
620 : :
621 : : Bindings*
622 : 806359 : Gogo::current_bindings()
623 : : {
624 : 806359 : if (!this->functions_.empty())
625 : 388528 : return this->functions_.back().blocks.back()->bindings();
626 : 417831 : else if (this->package_ != NULL)
627 : 320286 : return this->package_->bindings();
628 : : else
629 : 97545 : return this->globals_;
630 : : }
631 : :
632 : : const Bindings*
633 : 0 : Gogo::current_bindings() const
634 : : {
635 : 0 : if (!this->functions_.empty())
636 : 0 : return this->functions_.back().blocks.back()->bindings();
637 : 0 : else if (this->package_ != NULL)
638 : 0 : return this->package_->bindings();
639 : : else
640 : 0 : return this->globals_;
641 : : }
642 : :
643 : : void
644 : 62428 : Gogo::update_init_priority(Import_init* ii,
645 : : std::set<const Import_init *>* visited)
646 : : {
647 : 62428 : visited->insert(ii);
648 : 62428 : int succ_prior = -1;
649 : :
650 : 445578 : for (std::set<std::string>::const_iterator pci =
651 : 62428 : ii->precursors().begin();
652 : 508006 : pci != ii->precursors().end();
653 : 445578 : ++pci)
654 : : {
655 : 445578 : Import_init* succ = this->lookup_init(*pci);
656 : 445578 : if (visited->find(succ) == visited->end())
657 : 32671 : update_init_priority(succ, visited);
658 : 445578 : succ_prior = std::max(succ_prior, succ->priority());
659 : : }
660 : 62428 : if (ii->priority() <= succ_prior)
661 : 62428 : ii->set_priority(succ_prior + 1);
662 : 62428 : }
663 : :
664 : : void
665 : 1879 : Gogo::recompute_init_priorities()
666 : : {
667 : 1879 : std::set<Import_init *> nonroots;
668 : :
669 : 62428 : for (Import_init_set::const_iterator p =
670 : 1879 : this->imported_init_fns_.begin();
671 : 64307 : p != this->imported_init_fns_.end();
672 : 62428 : ++p)
673 : : {
674 : 62428 : const Import_init *ii = *p;
675 : 508006 : for (std::set<std::string>::const_iterator pci =
676 : 62428 : ii->precursors().begin();
677 : 508006 : pci != ii->precursors().end();
678 : 445578 : ++pci)
679 : : {
680 : 445578 : Import_init* ii_init = this->lookup_init(*pci);
681 : 445578 : nonroots.insert(ii_init);
682 : : }
683 : : }
684 : :
685 : : // Recursively update priorities starting at roots.
686 : 1879 : std::set<const Import_init*> visited;
687 : 62428 : for (Import_init_set::iterator p =
688 : 1879 : this->imported_init_fns_.begin();
689 : 64307 : p != this->imported_init_fns_.end();
690 : 62428 : ++p)
691 : : {
692 : 62428 : Import_init* ii = *p;
693 : 62428 : if (nonroots.find(ii) != nonroots.end())
694 : 32671 : continue;
695 : 29757 : update_init_priority(ii, &visited);
696 : : }
697 : 1879 : }
698 : :
699 : : // Add statements to INIT_STMTS which run the initialization
700 : : // functions for imported packages. This is only used for the "main"
701 : : // package.
702 : :
703 : : void
704 : 1889 : Gogo::init_imports(std::vector<Bstatement*>& init_stmts, Bfunction *bfunction)
705 : : {
706 : 1889 : go_assert(this->is_main_package());
707 : :
708 : 1889 : if (this->imported_init_fns_.empty())
709 : 10 : return;
710 : :
711 : 1879 : Location unknown_loc = Linemap::unknown_location();
712 : 1879 : Function_type* func_type =
713 : 1879 : Type::make_function_type(NULL, NULL, NULL, unknown_loc);
714 : 1879 : Btype* fntype = func_type->get_backend_fntype(this);
715 : :
716 : : // Recompute init priorities based on a walk of the init graph.
717 : 1879 : recompute_init_priorities();
718 : :
719 : : // We must call them in increasing priority order.
720 : 1879 : std::vector<const Import_init*> v;
721 : 62428 : for (Import_init_set::const_iterator p =
722 : 1879 : this->imported_init_fns_.begin();
723 : 64307 : p != this->imported_init_fns_.end();
724 : 62428 : ++p)
725 : : {
726 : : // Don't include dummy inits. They are not real functions.
727 : 62428 : if ((*p)->is_dummy())
728 : 27184 : continue;
729 : 35244 : if ((*p)->priority() < 0)
730 : 0 : go_error_at(Linemap::unknown_location(),
731 : : "internal error: failed to set init priority for %s",
732 : 0 : (*p)->package_name().c_str());
733 : 35244 : v.push_back(*p);
734 : : }
735 : 1879 : std::sort(v.begin(), v.end(), priority_compare);
736 : :
737 : : // We build calls to the init functions, which take no arguments.
738 : 1879 : std::vector<Bexpression*> empty_args;
739 : 1879 : for (std::vector<const Import_init*>::const_iterator p = v.begin();
740 : 37123 : p != v.end();
741 : 35244 : ++p)
742 : : {
743 : 35244 : const Import_init* ii = *p;
744 : 35244 : std::string user_name = ii->package_name() + ".init";
745 : 35244 : const std::string& init_name(ii->init_name());
746 : 35244 : const unsigned int flags =
747 : : (Backend::function_is_visible
748 : : | Backend::function_is_declaration
749 : : | Backend::function_is_inlinable);
750 : 35244 : Bfunction* pfunc = this->backend()->function(fntype, user_name, init_name,
751 : : flags, unknown_loc);
752 : 35244 : Bexpression* pfunc_code =
753 : 35244 : this->backend()->function_code_expression(pfunc, unknown_loc);
754 : 35244 : Bexpression* pfunc_call =
755 : 35244 : this->backend()->call_expression(bfunction, pfunc_code, empty_args,
756 : : NULL, unknown_loc);
757 : 35244 : init_stmts.push_back(this->backend()->expression_statement(bfunction,
758 : : pfunc_call));
759 : 35244 : }
760 : 1879 : }
761 : :
762 : : // Register global variables with the garbage collector. We need to
763 : : // register all variables which can hold a pointer value. They become
764 : : // roots during the mark phase. We build a struct that is easy to
765 : : // hook into a list of roots.
766 : :
767 : : // type gcRoot struct {
768 : : // decl unsafe.Pointer // Pointer to variable.
769 : : // size uintptr // Total size of variable.
770 : : // ptrdata uintptr // Length of variable's gcdata.
771 : : // gcdata *byte // Pointer mask.
772 : : // }
773 : : //
774 : : // type gcRootList struct {
775 : : // next *gcRootList
776 : : // count int
777 : : // roots [...]gcRoot
778 : : // }
779 : :
780 : : // The last entry in the roots array has a NULL decl field.
781 : :
782 : : void
783 : 4645 : Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
784 : : std::vector<Bstatement*>& init_stmts,
785 : : Bfunction* init_bfn)
786 : : {
787 : 4645 : if (var_gc.empty() && this->gc_roots_.empty())
788 : 2534 : return;
789 : :
790 : 2111 : Type* pvt = Type::make_pointer_type(Type::make_void_type());
791 : 2111 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
792 : 2111 : Type* byte_type = Type::lookup_integer_type("byte");
793 : 2111 : Type* pointer_byte_type = Type::make_pointer_type(byte_type);
794 : 2111 : Struct_type* root_type =
795 : 2111 : Type::make_builtin_struct_type(4,
796 : : "decl", pvt,
797 : : "size", uintptr_type,
798 : : "ptrdata", uintptr_type,
799 : : "gcdata", pointer_byte_type);
800 : :
801 : 2111 : Location builtin_loc = Linemap::predeclared_location();
802 : 2111 : unsigned long roots_len = var_gc.size() + this->gc_roots_.size();
803 : 2111 : Expression* length = Expression::make_integer_ul(roots_len, NULL,
804 : : builtin_loc);
805 : 2111 : Array_type* root_array_type = Type::make_array_type(root_type, length);
806 : 2111 : root_array_type->set_is_array_incomparable();
807 : :
808 : 2111 : Type* int_type = Type::lookup_integer_type("int");
809 : 2111 : Struct_type* root_list_type =
810 : 2111 : Type::make_builtin_struct_type(3,
811 : : "next", pvt,
812 : : "count", int_type,
813 : : "roots", root_array_type);
814 : :
815 : : // Build an initializer for the roots array.
816 : :
817 : 2111 : Expression_list* roots_init = new Expression_list();
818 : :
819 : 2111 : for (std::vector<Named_object*>::const_iterator p = var_gc.begin();
820 : 21500 : p != var_gc.end();
821 : 19389 : ++p)
822 : : {
823 : 19389 : Expression_list* init = new Expression_list();
824 : :
825 : 19389 : Location no_loc = (*p)->location();
826 : 19389 : Expression* decl = Expression::make_var_reference(*p, no_loc);
827 : 19389 : Expression* decl_addr =
828 : 19389 : Expression::make_unary(OPERATOR_AND, decl, no_loc);
829 : 19389 : decl_addr->unary_expression()->set_does_not_escape();
830 : 19389 : decl_addr = Expression::make_cast(pvt, decl_addr, no_loc);
831 : 19389 : init->push_back(decl_addr);
832 : :
833 : 19389 : Expression* size =
834 : 19389 : Expression::make_type_info(decl->type(),
835 : : Expression::TYPE_INFO_SIZE);
836 : 19389 : init->push_back(size);
837 : :
838 : 19389 : Expression* ptrdata =
839 : 19389 : Expression::make_type_info(decl->type(),
840 : : Expression::TYPE_INFO_BACKEND_PTRDATA);
841 : 19389 : init->push_back(ptrdata);
842 : :
843 : 19389 : Expression* gcdata = Expression::make_ptrmask_symbol(decl->type());
844 : 19389 : init->push_back(gcdata);
845 : :
846 : 19389 : Expression* root_ctor =
847 : 19389 : Expression::make_struct_composite_literal(root_type, init, no_loc);
848 : 19389 : roots_init->push_back(root_ctor);
849 : : }
850 : :
851 : 2111 : for (std::vector<Expression*>::const_iterator p = this->gc_roots_.begin();
852 : 4771 : p != this->gc_roots_.end();
853 : 2660 : ++p)
854 : : {
855 : 2660 : Expression_list *init = new Expression_list();
856 : :
857 : 2660 : Expression* expr = *p;
858 : 2660 : Location eloc = expr->location();
859 : 2660 : init->push_back(Expression::make_cast(pvt, expr, eloc));
860 : :
861 : 2660 : Type* type = expr->type()->points_to();
862 : 2660 : go_assert(type != NULL);
863 : :
864 : 2660 : Expression* size =
865 : 2660 : Expression::make_type_info(type,
866 : : Expression::TYPE_INFO_SIZE);
867 : 2660 : init->push_back(size);
868 : :
869 : 2660 : Expression* ptrdata =
870 : 2660 : Expression::make_type_info(type,
871 : : Expression::TYPE_INFO_BACKEND_PTRDATA);
872 : 2660 : init->push_back(ptrdata);
873 : :
874 : 2660 : Expression* gcdata = Expression::make_ptrmask_symbol(type);
875 : 2660 : init->push_back(gcdata);
876 : :
877 : 2660 : Expression* root_ctor =
878 : 2660 : Expression::make_struct_composite_literal(root_type, init, eloc);
879 : 2660 : roots_init->push_back(root_ctor);
880 : : }
881 : :
882 : : // Build a constructor for the struct.
883 : :
884 : 2111 : Expression_list* root_list_init = new Expression_list();
885 : 2111 : root_list_init->push_back(Expression::make_nil(builtin_loc));
886 : 2111 : root_list_init->push_back(Expression::make_integer_ul(roots_len, int_type,
887 : : builtin_loc));
888 : :
889 : 2111 : Expression* roots_ctor =
890 : 2111 : Expression::make_array_composite_literal(root_array_type, roots_init,
891 : : builtin_loc);
892 : 2111 : root_list_init->push_back(roots_ctor);
893 : :
894 : 2111 : Expression* root_list_ctor =
895 : 2111 : Expression::make_struct_composite_literal(root_list_type, root_list_init,
896 : : builtin_loc);
897 : :
898 : 2111 : Expression* root_addr = Expression::make_unary(OPERATOR_AND, root_list_ctor,
899 : : builtin_loc);
900 : 2111 : root_addr->unary_expression()->set_is_gc_root();
901 : 2111 : Expression* register_roots = Runtime::make_call(this,
902 : : Runtime::REGISTER_GC_ROOTS,
903 : : builtin_loc, 1, root_addr);
904 : :
905 : 2111 : Translate_context context(this, NULL, NULL, NULL);
906 : 2111 : Bexpression* bcall = register_roots->get_backend(&context);
907 : 2111 : init_stmts.push_back(this->backend()->expression_statement(init_bfn, bcall));
908 : : }
909 : :
910 : : // Build the list of type descriptors defined in this package. This is to help
911 : : // the reflect package to find compiler-generated types.
912 : :
913 : : // type typeDescriptorList struct {
914 : : // count int
915 : : // types [...]unsafe.Pointer
916 : : // }
917 : :
918 : : static Struct_type*
919 : 6534 : type_descriptor_list_type(unsigned long len)
920 : : {
921 : 6534 : Location builtin_loc = Linemap::predeclared_location();
922 : 6534 : Type* int_type = Type::lookup_integer_type("int");
923 : 6534 : Type* ptr_type = Type::make_pointer_type(Type::make_void_type());
924 : : // Avoid creating zero-length type.
925 : 6534 : unsigned long nelems = (len != 0 ? len : 1);
926 : 6534 : Expression* len_expr = Expression::make_integer_ul(nelems, NULL,
927 : : builtin_loc);
928 : 6534 : Array_type* array_type = Type::make_array_type(ptr_type, len_expr);
929 : 6534 : array_type->set_is_array_incomparable();
930 : 6534 : Struct_type* list_type =
931 : 6534 : Type::make_builtin_struct_type(2, "count", int_type,
932 : : "types", array_type);
933 : 6534 : return list_type;
934 : : }
935 : :
936 : : void
937 : 4645 : Gogo::build_type_descriptor_list()
938 : : {
939 : : // Create the list type
940 : 4645 : Location builtin_loc = Linemap::predeclared_location();
941 : 4645 : unsigned long len = this->type_descriptors_.size();
942 : 4645 : Struct_type* list_type = type_descriptor_list_type(len);
943 : 4645 : Btype* bt = list_type->get_backend(this);
944 : 4645 : Btype* bat = list_type->field(1)->type()->get_backend(this);
945 : :
946 : : // Create the variable
947 : 4645 : std::string name = this->type_descriptor_list_symbol(this->pkgpath_symbol());
948 : 4645 : unsigned int flags = Backend::variable_is_constant;
949 : 4645 : Bvariable* bv = this->backend()->implicit_variable(name, name, bt, flags, 0);
950 : :
951 : : // Build the initializer
952 : 4645 : std::vector<unsigned long> indexes;
953 : 4645 : std::vector<Bexpression*> vals;
954 : 4645 : std::vector<Type*>::iterator p = this->type_descriptors_.begin();
955 : 180876 : for (unsigned long i = 0; i < len; ++i, ++p)
956 : : {
957 : 176231 : Bexpression* bexpr = (*p)->type_descriptor_pointer(this,
958 : 176231 : builtin_loc);
959 : 176231 : indexes.push_back(i);
960 : 176231 : vals.push_back(bexpr);
961 : : }
962 : 4645 : Bexpression* barray =
963 : 4645 : this->backend()->array_constructor_expression(bat, indexes, vals,
964 : 4645 : builtin_loc);
965 : :
966 : 4645 : Translate_context context(this, NULL, NULL, NULL);
967 : 4645 : std::vector<Bexpression*> fields;
968 : 4645 : Expression* len_expr = Expression::make_integer_ul(len, NULL,
969 : : builtin_loc);
970 : 4645 : fields.push_back(len_expr->get_backend(&context));
971 : 4645 : fields.push_back(barray);
972 : 4645 : Bexpression* binit =
973 : 4645 : this->backend()->constructor_expression(bt, fields, builtin_loc);
974 : :
975 : 4645 : this->backend()->implicit_variable_set_init(bv, name, bt, flags, binit);
976 : 4645 : }
977 : :
978 : : // Register the type descriptors with the runtime. This is to help
979 : : // the reflect package to find compiler-generated types.
980 : :
981 : : void
982 : 1889 : Gogo::register_type_descriptors(std::vector<Bstatement*>& init_stmts,
983 : : Bfunction* init_bfn)
984 : : {
985 : : // Create the list type
986 : 1889 : Location builtin_loc = Linemap::predeclared_location();
987 : 1889 : Struct_type* list_type = type_descriptor_list_type(1);
988 : 1889 : Btype* bt = list_type->get_backend(this);
989 : :
990 : : // Collect type lists from transitive imports.
991 : 1889 : std::vector<std::string> list_names;
992 : 1889 : for (Import_init_set::iterator it = this->imported_init_fns_.begin();
993 : 64317 : it != this->imported_init_fns_.end();
994 : 62428 : ++it)
995 : : {
996 : 62428 : std::string pkgpath_symbol =
997 : 62428 : this->pkgpath_symbol_from_init_fn_name((*it)->init_name());
998 : 62428 : list_names.push_back(this->type_descriptor_list_symbol(pkgpath_symbol));
999 : 62428 : }
1000 : : // Add the main package itself.
1001 : 1889 : list_names.push_back(this->type_descriptor_list_symbol("main"));
1002 : :
1003 : : // Build a list of lists.
1004 : 1889 : std::vector<unsigned long> indexes;
1005 : 1889 : std::vector<Bexpression*> vals;
1006 : 1889 : unsigned long i = 0;
1007 : 1889 : for (std::vector<std::string>::iterator p = list_names.begin();
1008 : 66206 : p != list_names.end();
1009 : 64317 : ++p)
1010 : : {
1011 : 64317 : Bvariable* bv =
1012 : 64317 : this->backend()->implicit_variable_reference(*p, *p, bt);
1013 : 64317 : Bexpression* bexpr = this->backend()->var_expression(bv, builtin_loc);
1014 : 64317 : bexpr = this->backend()->address_expression(bexpr, builtin_loc);
1015 : :
1016 : 64317 : indexes.push_back(i);
1017 : 64317 : vals.push_back(bexpr);
1018 : 64317 : i++;
1019 : : }
1020 : 1889 : Expression* len_expr = Expression::make_integer_ul(i, NULL, builtin_loc);
1021 : 1889 : Type* list_ptr_type = Type::make_pointer_type(list_type);
1022 : 1889 : Type* list_array_type = Type::make_array_type(list_ptr_type, len_expr);
1023 : 1889 : Btype* bat = list_array_type->get_backend(this);
1024 : 1889 : Bexpression* barray =
1025 : 1889 : this->backend()->array_constructor_expression(bat, indexes, vals,
1026 : : builtin_loc);
1027 : :
1028 : : // Create a variable holding the list.
1029 : 1889 : std::string name = this->typelists_symbol();
1030 : 1889 : unsigned int flags = (Backend::variable_is_hidden
1031 : : | Backend::variable_is_constant);
1032 : 1889 : Bvariable* bv = this->backend()->implicit_variable(name, name, bat, flags,
1033 : : 0);
1034 : 1889 : this->backend()->implicit_variable_set_init(bv, name, bat, flags, barray);
1035 : :
1036 : : // Build the call in main package's init function.
1037 : 1889 : Translate_context context(this, NULL, NULL, NULL);
1038 : 1889 : Bexpression* bexpr = this->backend()->var_expression(bv, builtin_loc);
1039 : 1889 : bexpr = this->backend()->address_expression(bexpr, builtin_loc);
1040 : 1889 : Type* array_ptr_type = Type::make_pointer_type(list_array_type);
1041 : 1889 : Expression* expr = Expression::make_backend(bexpr, array_ptr_type,
1042 : : builtin_loc);
1043 : 1889 : expr = Runtime::make_call(this, Runtime::REGISTER_TYPE_DESCRIPTORS,
1044 : : builtin_loc, 2, len_expr->copy(), expr);
1045 : 1889 : Bexpression* bcall = expr->get_backend(&context);
1046 : 1889 : init_stmts.push_back(this->backend()->expression_statement(init_bfn,
1047 : : bcall));
1048 : 1889 : }
1049 : :
1050 : : // Build the decl for the initialization function.
1051 : :
1052 : : Named_object*
1053 : 3453 : Gogo::initialization_function_decl()
1054 : : {
1055 : 3453 : std::string name = this->get_init_fn_name();
1056 : 3453 : Location loc = this->package_->location();
1057 : :
1058 : 3453 : Function_type* fntype = Type::make_function_type(NULL, NULL, NULL, loc);
1059 : 3453 : Function* initfn = new Function(fntype, NULL, NULL, loc);
1060 : 3453 : return Named_object::make_function(name, NULL, initfn);
1061 : 3453 : }
1062 : :
1063 : : // Create the magic initialization function. CODE_STMT is the
1064 : : // code that it needs to run.
1065 : :
1066 : : Named_object*
1067 : 3449 : Gogo::create_initialization_function(Named_object* initfn,
1068 : : Bstatement* code_stmt)
1069 : : {
1070 : : // Make sure that we thought we needed an initialization function,
1071 : : // as otherwise we will not have reported it in the export data.
1072 : 3449 : go_assert(this->is_main_package() || this->need_init_fn_);
1073 : :
1074 : 3449 : if (initfn == NULL)
1075 : 278 : initfn = this->initialization_function_decl();
1076 : :
1077 : : // Bind the initialization function code to a block.
1078 : 3449 : Bfunction* fndecl = initfn->func_value()->get_or_make_decl(this, initfn);
1079 : 3449 : Location pkg_loc = this->package_->location();
1080 : 3449 : std::vector<Bvariable*> vars;
1081 : 3449 : this->backend()->block(fndecl, NULL, vars, pkg_loc, pkg_loc);
1082 : :
1083 : 3449 : if (!this->backend()->function_set_body(fndecl, code_stmt))
1084 : : {
1085 : 41 : go_assert(saw_errors());
1086 : : return NULL;
1087 : : }
1088 : : return initfn;
1089 : 3449 : }
1090 : :
1091 : : // Given an expression, collect all the global variables defined in
1092 : : // this package that it references.
1093 : :
1094 : : class Find_vars : public Traverse
1095 : : {
1096 : : private:
1097 : : // The list of variables we accumulate.
1098 : : typedef Unordered_set(Named_object*) Vars;
1099 : :
1100 : : // A hash table we use to avoid looping. The index is a
1101 : : // Named_object* or a Temporary_statement*. We only look through
1102 : : // objects defined in this package.
1103 : : typedef Unordered_set(const void*) Seen_objects;
1104 : :
1105 : : public:
1106 : 55915 : Find_vars()
1107 : 55915 : : Traverse(traverse_expressions | traverse_statements),
1108 : 55915 : vars_(), seen_objects_(), lhs_is_ref_(false)
1109 : 55915 : { }
1110 : :
1111 : : // An iterator through the variables found, after the traversal.
1112 : : typedef Vars::const_iterator const_iterator;
1113 : :
1114 : : const_iterator
1115 : 55915 : begin() const
1116 : 55915 : { return this->vars_.begin(); }
1117 : :
1118 : : const_iterator
1119 : 77447 : end() const
1120 : 77447 : { return this->vars_.end(); }
1121 : :
1122 : : int
1123 : : expression(Expression**);
1124 : :
1125 : : int
1126 : : statement(Block*, size_t* index, Statement*);
1127 : :
1128 : : private:
1129 : : // Accumulated variables.
1130 : : Vars vars_;
1131 : : // Objects we have already seen.
1132 : : Seen_objects seen_objects_;
1133 : : // Whether an assignment to a variable counts as a reference.
1134 : : bool lhs_is_ref_;
1135 : : };
1136 : :
1137 : : // Collect global variables referenced by EXPR. Look through function
1138 : : // calls and variable initializations.
1139 : :
1140 : : int
1141 : 7219006 : Find_vars::expression(Expression** pexpr)
1142 : : {
1143 : 7219006 : Expression* e = *pexpr;
1144 : :
1145 : 7219006 : Var_expression* ve = e->var_expression();
1146 : 824867 : if (ve != NULL)
1147 : : {
1148 : 824867 : Named_object* v = ve->named_object();
1149 : 824867 : if (!v->is_variable() || v->package() != NULL)
1150 : : {
1151 : : // This is a result parameter or a variable defined in a
1152 : : // different package. Either way we don't care about it.
1153 : 636338 : return TRAVERSE_CONTINUE;
1154 : : }
1155 : :
1156 : 734392 : std::pair<Seen_objects::iterator, bool> ins =
1157 : 734392 : this->seen_objects_.insert(v);
1158 : 734392 : if (!ins.second)
1159 : : {
1160 : : // We've seen this variable before.
1161 : : return TRAVERSE_CONTINUE;
1162 : : }
1163 : :
1164 : 188529 : if (v->var_value()->is_global())
1165 : 21549 : this->vars_.insert(v);
1166 : :
1167 : 188529 : Expression* init = v->var_value()->init();
1168 : 188529 : if (init != NULL)
1169 : : {
1170 : 82447 : if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
1171 : : return TRAVERSE_EXIT;
1172 : : }
1173 : : }
1174 : :
1175 : : // We traverse the code of any function or bound method we see. Note that
1176 : : // this means that we will traverse the code of a function or bound method
1177 : : // whose address is taken even if it is not called.
1178 : 6582668 : Func_expression* fe = e->func_expression();
1179 : 6582668 : Bound_method_expression* bme = e->bound_method_expression();
1180 : 6582668 : if (fe != NULL || bme != NULL)
1181 : : {
1182 : 335257 : const Named_object* f = fe != NULL ? fe->named_object() : bme->function();
1183 : 335257 : if (f->is_function() && f->package() == NULL)
1184 : : {
1185 : 129265 : std::pair<Seen_objects::iterator, bool> ins =
1186 : 129265 : this->seen_objects_.insert(f);
1187 : 129265 : if (ins.second)
1188 : : {
1189 : : // This is the first time we have seen this name.
1190 : 41342 : bool hold = this->lhs_is_ref_;
1191 : 41342 : this->lhs_is_ref_ = true;
1192 : 41342 : int r = f->func_value()->block()->traverse(this);
1193 : 41342 : this->lhs_is_ref_ = hold;
1194 : 41342 : if (r == TRAVERSE_EXIT)
1195 : 0 : return TRAVERSE_EXIT;
1196 : : }
1197 : : }
1198 : : }
1199 : :
1200 : 6582668 : Temporary_reference_expression* tre = e->temporary_reference_expression();
1201 : 310565 : if (tre != NULL)
1202 : : {
1203 : 310565 : Temporary_statement* ts = tre->statement();
1204 : 310565 : Expression* init = ts->init();
1205 : 310565 : if (init != NULL)
1206 : : {
1207 : 260150 : std::pair<Seen_objects::iterator, bool> ins =
1208 : 260150 : this->seen_objects_.insert(ts);
1209 : 260150 : if (ins.second)
1210 : : {
1211 : : // This is the first time we have seen this temporary
1212 : : // statement.
1213 : 167876 : if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
1214 : 0 : return TRAVERSE_EXIT;
1215 : : }
1216 : : }
1217 : : }
1218 : :
1219 : : return TRAVERSE_CONTINUE;
1220 : : }
1221 : :
1222 : : // Check a statement while searching for variables. This is where we
1223 : : // skip variables on the left hand side of assigments if appropriate.
1224 : :
1225 : : int
1226 : 1047161 : Find_vars::statement(Block*, size_t*, Statement* s)
1227 : : {
1228 : 1047161 : if (this->lhs_is_ref_)
1229 : : return TRAVERSE_CONTINUE;
1230 : 9126 : Assignment_statement* as = s->assignment_statement();
1231 : 28 : if (as == NULL)
1232 : : return TRAVERSE_CONTINUE;
1233 : :
1234 : : // Only traverse subexpressions of the LHS.
1235 : 28 : if (as->lhs()->traverse_subexpressions(this) == TRAVERSE_EXIT)
1236 : : return TRAVERSE_EXIT;
1237 : :
1238 : 28 : Expression* rhs = as->rhs();
1239 : 28 : if (Expression::traverse(&rhs, this) == TRAVERSE_EXIT)
1240 : : return TRAVERSE_EXIT;
1241 : :
1242 : : return TRAVERSE_SKIP_COMPONENTS;
1243 : : }
1244 : :
1245 : : // Return true if EXPR, PREINIT, or DEP refers to VAR.
1246 : :
1247 : : static bool
1248 : 19214 : expression_requires(Expression* expr, Block* preinit, Named_object* dep,
1249 : : Named_object* var)
1250 : : {
1251 : 19214 : Find_vars find_vars;
1252 : 19214 : if (expr != NULL)
1253 : 19214 : Expression::traverse(&expr, &find_vars);
1254 : 19214 : if (preinit != NULL)
1255 : 909 : preinit->traverse(&find_vars);
1256 : 19214 : if (dep != NULL)
1257 : : {
1258 : 0 : Expression* init = dep->var_value()->init();
1259 : 0 : if (init != NULL)
1260 : 0 : Expression::traverse(&init, &find_vars);
1261 : 0 : if (dep->var_value()->has_pre_init())
1262 : 0 : dep->var_value()->preinit()->traverse(&find_vars);
1263 : : }
1264 : :
1265 : 29096 : for (Find_vars::const_iterator p = find_vars.begin();
1266 : 29096 : p != find_vars.end();
1267 : 9882 : ++p)
1268 : : {
1269 : 9899 : if (*p == var)
1270 : 19214 : return true;
1271 : : }
1272 : : return false;
1273 : 19214 : }
1274 : :
1275 : : // Sort variable initializations. If the initialization expression
1276 : : // for variable A refers directly or indirectly to the initialization
1277 : : // expression for variable B, then we must initialize B before A.
1278 : :
1279 : : class Var_init
1280 : : {
1281 : : public:
1282 : : Var_init()
1283 : : : var_(NULL), init_(NULL), dep_count_(0)
1284 : : { }
1285 : :
1286 : 13014 : Var_init(Named_object* var, Bstatement* init)
1287 : 13014 : : var_(var), init_(init), dep_count_(0)
1288 : : { }
1289 : :
1290 : : // Return the variable.
1291 : : Named_object*
1292 : 121944 : var() const
1293 : 121944 : { return this->var_; }
1294 : :
1295 : : // Return the initialization expression.
1296 : : Bstatement*
1297 : 13014 : init() const
1298 : 13014 : { return this->init_; }
1299 : :
1300 : : // Return the number of remaining dependencies.
1301 : : size_t
1302 : 17340 : dep_count() const
1303 : 17340 : { return this->dep_count_; }
1304 : :
1305 : : // Increment the number of dependencies.
1306 : : void
1307 : 7044 : add_dependency()
1308 : 7044 : { ++this->dep_count_; }
1309 : :
1310 : : // Decrement the number of dependencies.
1311 : : void
1312 : 7040 : remove_dependency()
1313 : 7040 : { --this->dep_count_; }
1314 : :
1315 : : private:
1316 : : // The variable being initialized.
1317 : : Named_object* var_;
1318 : : // The backend initialization statement.
1319 : : Bstatement* init_;
1320 : : // The number of initializations this is dependent on. A variable
1321 : : // initialization should not be emitted if any of its dependencies
1322 : : // have not yet been resolved.
1323 : : size_t dep_count_;
1324 : : };
1325 : :
1326 : : // For comparing Var_init keys in a map.
1327 : :
1328 : : inline bool
1329 : 88872 : operator<(const Var_init& v1, const Var_init& v2)
1330 : 88872 : { return v1.var()->name() < v2.var()->name(); }
1331 : :
1332 : : typedef std::list<Var_init> Var_inits;
1333 : :
1334 : : // Sort the variable initializations. The rule we follow is that we
1335 : : // emit them in the order they appear in the array, except that if the
1336 : : // initialization expression for a variable V1 depends upon another
1337 : : // variable V2 then we initialize V1 after V2.
1338 : :
1339 : : static void
1340 : 1857 : sort_var_inits(Var_inits* var_inits)
1341 : : {
1342 : 1857 : if (var_inits->empty())
1343 : 0 : return;
1344 : :
1345 : 1857 : std::map<Named_object*, Var_init*> var_to_init;
1346 : :
1347 : : // A mapping from a variable initialization to a set of
1348 : : // variable initializations that depend on it.
1349 : 1857 : typedef std::map<Var_init, std::set<Var_init*> > Init_deps;
1350 : 1857 : Init_deps init_deps;
1351 : 1857 : bool init_loop = false;
1352 : :
1353 : : // Map from variable to Var_init.
1354 : 1857 : for (Var_inits::iterator pvar = var_inits->begin();
1355 : 14871 : pvar != var_inits->end();
1356 : 13014 : ++pvar)
1357 : : {
1358 : 13014 : Named_object* var = pvar->var();
1359 : 13014 : var_to_init[var] = &*pvar;
1360 : : }
1361 : :
1362 : : // Add dependencies to init_deps, and check for cycles.
1363 : 14871 : for (Var_inits::iterator pvar = var_inits->begin();
1364 : 14871 : pvar != var_inits->end();
1365 : 13014 : ++pvar)
1366 : : {
1367 : 13014 : Named_object* var = pvar->var();
1368 : :
1369 : 13014 : const std::vector<Named_object*>* refs =
1370 : 13014 : pvar->var()->var_value()->init_refs();
1371 : 13014 : if (refs == NULL)
1372 : 9502 : continue;
1373 : 14985 : for (std::vector<Named_object*>::const_iterator pdep = refs->begin();
1374 : 14985 : pdep != refs->end();
1375 : 11473 : ++pdep)
1376 : : {
1377 : 11473 : Named_object* dep = *pdep;
1378 : 11473 : if (var == dep)
1379 : : {
1380 : : // This is a reference from a variable to itself.
1381 : 33 : go_error_at(var->location(),
1382 : : ("initialization expression for %qs "
1383 : : "depends upon itself"),
1384 : 33 : var->message_name().c_str());
1385 : 9584 : continue;
1386 : : }
1387 : :
1388 : 11440 : Var_init* dep_init = var_to_init[dep];
1389 : 11440 : if (dep_init == NULL)
1390 : : {
1391 : : // This is a dependency on some variable that doesn't
1392 : : // have an initializer, so for purposes of
1393 : : // initialization ordering this is irrelevant.
1394 : 4396 : continue;
1395 : : }
1396 : :
1397 : 7044 : init_deps[*dep_init].insert(&(*pvar));
1398 : 7044 : pvar->add_dependency();
1399 : :
1400 : : // Check for cycles.
1401 : 7044 : const std::vector<Named_object*>* deprefs =
1402 : 7044 : dep_init->var()->var_value()->init_refs();
1403 : 7044 : if (deprefs == NULL)
1404 : 5155 : continue;
1405 : 5108 : for (std::vector<Named_object*>::const_iterator pdepdep =
1406 : 1889 : deprefs->begin();
1407 : 5108 : pdepdep != deprefs->end();
1408 : 3219 : ++pdepdep)
1409 : : {
1410 : 3221 : if (*pdepdep == var)
1411 : : {
1412 : 2 : go_error_at(var->location(),
1413 : : ("initialization expressions for %qs and "
1414 : : "%qs depend upon each other"),
1415 : 4 : var->message_name().c_str(),
1416 : 2 : dep->message_name().c_str());
1417 : 2 : go_inform(dep->location(), "%qs defined here",
1418 : 2 : dep->message_name().c_str());
1419 : 2 : init_loop = true;
1420 : 2 : break;
1421 : : }
1422 : : }
1423 : : }
1424 : : }
1425 : :
1426 : 1857 : var_to_init.clear();
1427 : :
1428 : : // If there are no dependencies then the declaration order is sorted.
1429 : 1857 : if (!init_deps.empty() && !init_loop)
1430 : : {
1431 : : // Otherwise, sort variable initializations by emitting all variables with
1432 : : // no dependencies in declaration order. VAR_INITS is already in
1433 : : // declaration order.
1434 : 329 : Var_inits ready;
1435 : 8713 : while (!var_inits->empty())
1436 : : {
1437 : : Var_inits::iterator v1;;
1438 : 17340 : for (v1 = var_inits->begin(); v1 != var_inits->end(); ++v1)
1439 : : {
1440 : 17340 : if (v1->dep_count() == 0)
1441 : : break;
1442 : : }
1443 : 8055 : go_assert(v1 != var_inits->end());
1444 : :
1445 : : // V1 either has no dependencies or its dependencies have already
1446 : : // been emitted, add it to READY next. When V1 is emitted, remove
1447 : : // a dependency from each V that depends on V1.
1448 : 8055 : ready.splice(ready.end(), *var_inits, v1);
1449 : :
1450 : 8055 : Init_deps::iterator p1 = init_deps.find(*v1);
1451 : 8055 : if (p1 != init_deps.end())
1452 : : {
1453 : 3945 : std::set<Var_init*> resolved = p1->second;
1454 : 10985 : for (std::set<Var_init*>::iterator pv = resolved.begin();
1455 : 10985 : pv != resolved.end();
1456 : 7040 : ++pv)
1457 : 7040 : (*pv)->remove_dependency();
1458 : 3945 : init_deps.erase(p1);
1459 : 3945 : }
1460 : : }
1461 : 329 : var_inits->swap(ready);
1462 : 329 : go_assert(init_deps.empty());
1463 : 329 : }
1464 : 1857 : }
1465 : :
1466 : : // Give an error if the initialization expression for VAR depends on
1467 : : // itself. We only check if INIT is not NULL and there is no
1468 : : // dependency; when INIT is NULL, it means that PREINIT sets VAR,
1469 : : // which we will interpret as a loop.
1470 : :
1471 : : void
1472 : 11992 : Gogo::check_self_dep(Named_object* var)
1473 : : {
1474 : 11992 : Expression* init = var->var_value()->init();
1475 : 11992 : Block* preinit = var->var_value()->preinit();
1476 : 11992 : Named_object* dep = this->var_depends_on(var->var_value());
1477 : 11992 : if (init != NULL
1478 : 11992 : && dep == NULL
1479 : 11992 : && expression_requires(init, preinit, NULL, var))
1480 : 17 : go_error_at(var->location(),
1481 : : "initialization expression for %qs depends upon itself",
1482 : 34 : var->message_name().c_str());
1483 : 11992 : }
1484 : :
1485 : : // Write out the global definitions.
1486 : :
1487 : : void
1488 : 4645 : Gogo::write_globals()
1489 : : {
1490 : 4645 : this->build_interface_method_tables();
1491 : :
1492 : 4645 : Bindings* bindings = this->current_bindings();
1493 : :
1494 : 447243 : for (Bindings::const_declarations_iterator p = bindings->begin_declarations();
1495 : 447243 : p != bindings->end_declarations();
1496 : 442598 : ++p)
1497 : : {
1498 : : // If any function declarations needed a descriptor, make sure
1499 : : // we build it.
1500 : 442598 : Named_object* no = p->second;
1501 : 442598 : if (no->is_function_declaration())
1502 : 3904 : no->func_declaration_value()->build_backend_descriptor(this);
1503 : : }
1504 : :
1505 : : // Lists of globally declared types, variables, constants, and functions
1506 : : // that must be defined.
1507 : 4645 : std::vector<Btype*> type_decls;
1508 : 4645 : std::vector<Bvariable*> var_decls;
1509 : 4645 : std::vector<Bexpression*> const_decls;
1510 : 4645 : std::vector<Bfunction*> func_decls;
1511 : :
1512 : : // The init function declaration and associated Bfunction, if necessary.
1513 : 4645 : Named_object* init_fndecl = NULL;
1514 : 4645 : Bfunction* init_bfn = NULL;
1515 : :
1516 : 4645 : std::vector<Bstatement*> init_stmts;
1517 : 4645 : std::vector<Bstatement*> var_init_stmts;
1518 : :
1519 : 4645 : if (this->is_main_package())
1520 : : {
1521 : 1889 : init_fndecl = this->initialization_function_decl();
1522 : 1889 : init_bfn = init_fndecl->func_value()->get_or_make_decl(this, init_fndecl);
1523 : : }
1524 : :
1525 : : // A list of variable initializations.
1526 : 4645 : Var_inits var_inits;
1527 : :
1528 : : // A list of variables which need to be registered with the garbage
1529 : : // collector.
1530 : 4645 : size_t count_definitions = bindings->size_definitions();
1531 : 4645 : std::vector<Named_object*> var_gc;
1532 : 4645 : var_gc.reserve(count_definitions);
1533 : :
1534 : 563119 : for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
1535 : 563119 : p != bindings->end_definitions();
1536 : 558474 : ++p)
1537 : : {
1538 : 558474 : Named_object* no = *p;
1539 : 558474 : go_assert(!no->is_type_declaration() && !no->is_function_declaration());
1540 : :
1541 : : // There is nothing to do for a package.
1542 : 558474 : if (no->is_package())
1543 : 195920 : continue;
1544 : :
1545 : : // There is nothing to do for an object which was imported from
1546 : : // a different package into the global scope.
1547 : 519059 : if (no->package() != NULL)
1548 : 32785 : continue;
1549 : :
1550 : : // Skip blank named functions and constants.
1551 : 283387 : if ((no->is_function() && no->func_value()->is_sink())
1552 : 769361 : || (no->is_const() && no->const_value()->is_sink()))
1553 : 1477 : continue;
1554 : :
1555 : : // Skip global sink variables with static initializers. With
1556 : : // non-static initializers we have to evaluate for side effects,
1557 : : // and we wind up initializing a dummy variable. That is not
1558 : : // ideal but it works and it's a rare case.
1559 : 484869 : if (no->is_variable()
1560 : 25471 : && no->var_value()->is_global_sink()
1561 : 1114 : && !no->var_value()->has_pre_init()
1562 : 485226 : && (no->var_value()->init() == NULL
1563 : 417 : || no->var_value()->init()->is_static_initializer()))
1564 : 72 : continue;
1565 : :
1566 : : // There is nothing useful we can output for constants which
1567 : : // have ideal or non-integral type.
1568 : 484725 : if (no->is_const())
1569 : : {
1570 : 145570 : Type* type = no->const_value()->type();
1571 : 145570 : if (type == NULL)
1572 : 0 : type = no->const_value()->expr()->type();
1573 : 169178 : if (type->is_abstract() || !type->is_numeric_type())
1574 : 122171 : continue;
1575 : : }
1576 : :
1577 : 362554 : if (!no->is_variable())
1578 : 337155 : no->get_backend(this, const_decls, type_decls, func_decls);
1579 : : else
1580 : : {
1581 : 25399 : Variable* var = no->var_value();
1582 : 25399 : Bvariable* bvar = no->get_backend_variable(this, NULL);
1583 : 25399 : var_decls.push_back(bvar);
1584 : :
1585 : : // Check for a sink variable, which may be used to run an
1586 : : // initializer purely for its side effects.
1587 : 25399 : bool is_sink = no->name()[0] == '_' && no->name()[1] == '.';
1588 : :
1589 : 25399 : Bstatement* var_init_stmt = NULL;
1590 : 25399 : if (!var->has_pre_init())
1591 : : {
1592 : : // If the backend representation of the variable initializer is
1593 : : // constant, we can just set the initial value using
1594 : : // global_var_set_init instead of during the init() function.
1595 : : // The initializer is constant if it is the zero-value of the
1596 : : // variable's type or if the initial value is an immutable value
1597 : : // that is not copied to the heap.
1598 : 13191 : bool is_static_initializer = false;
1599 : 13191 : if (var->init() == NULL)
1600 : : is_static_initializer = true;
1601 : : else
1602 : : {
1603 : 8092 : Type* var_type = var->type();
1604 : 8092 : Expression* init = var->init();
1605 : 8092 : Expression* init_cast =
1606 : 8092 : Expression::make_cast(var_type, init, var->location());
1607 : 8092 : is_static_initializer = init_cast->is_static_initializer();
1608 : : }
1609 : :
1610 : : // Non-constant variable initializations might need to create
1611 : : // temporary variables, which will need the initialization
1612 : : // function as context.
1613 : 8092 : Named_object* var_init_fn;
1614 : 8092 : if (is_static_initializer)
1615 : : var_init_fn = NULL;
1616 : : else
1617 : : {
1618 : 802 : if (init_fndecl == NULL)
1619 : : {
1620 : 51 : init_fndecl = this->initialization_function_decl();
1621 : 51 : Function* func = init_fndecl->func_value();
1622 : 51 : init_bfn = func->get_or_make_decl(this, init_fndecl);
1623 : : }
1624 : : var_init_fn = init_fndecl;
1625 : : }
1626 : 13191 : Bexpression* var_binit = var->get_init(this, var_init_fn);
1627 : :
1628 : 13191 : if (var_binit == NULL)
1629 : : ;
1630 : 8092 : else if (is_static_initializer)
1631 : : {
1632 : 7290 : if (expression_requires(var->init(), NULL,
1633 : : this->var_depends_on(var), no))
1634 : 0 : go_error_at(no->location(),
1635 : : "initialization expression for %qs depends "
1636 : : "upon itself",
1637 : 0 : no->message_name().c_str());
1638 : 7290 : this->backend()->global_variable_set_init(bvar, var_binit);
1639 : : }
1640 : 802 : else if (is_sink)
1641 : 357 : var_init_stmt =
1642 : 357 : this->backend()->expression_statement(init_bfn, var_binit);
1643 : : else
1644 : : {
1645 : 445 : Location loc = var->location();
1646 : 445 : Bexpression* var_expr =
1647 : 445 : this->backend()->var_expression(bvar, loc);
1648 : 445 : var_init_stmt =
1649 : 445 : this->backend()->assignment_statement(init_bfn, var_expr,
1650 : : var_binit, loc);
1651 : : }
1652 : : }
1653 : : else
1654 : : {
1655 : : // We are going to create temporary variables which
1656 : : // means that we need an fndecl.
1657 : 12208 : if (init_fndecl == NULL)
1658 : 1235 : init_fndecl = this->initialization_function_decl();
1659 : :
1660 : 12208 : Bvariable* var_decl = is_sink ? NULL : bvar;
1661 : 12208 : var_init_stmt = var->get_init_block(this, init_fndecl, var_decl);
1662 : : }
1663 : :
1664 : 25399 : if (var_init_stmt != NULL)
1665 : : {
1666 : 13010 : if (var->init() == NULL && !var->has_pre_init())
1667 : 0 : var_init_stmts.push_back(var_init_stmt);
1668 : : else
1669 : 13010 : var_inits.push_back(Var_init(no, var_init_stmt));
1670 : : }
1671 : 12389 : else if (this->var_depends_on(var) != NULL)
1672 : : {
1673 : : // This variable is initialized from something that is
1674 : : // not in its init or preinit. This variable needs to
1675 : : // participate in dependency analysis sorting, in case
1676 : : // some other variable depends on this one.
1677 : 4 : Btype* btype = no->var_value()->type()->get_backend(this);
1678 : 4 : Bexpression* zero = this->backend()->zero_expression(btype);
1679 : 4 : Bstatement* zero_stmt =
1680 : 4 : this->backend()->expression_statement(init_bfn, zero);
1681 : 4 : var_inits.push_back(Var_init(no, zero_stmt));
1682 : : }
1683 : :
1684 : : // Collect a list of all global variables with pointers,
1685 : : // to register them for the garbage collector.
1686 : 25399 : if (!is_sink && var->type()->has_pointer())
1687 : : {
1688 : : // Avoid putting runtime.gcRoots itself on the list.
1689 : 38792 : if (this->compiling_runtime()
1690 : 832 : && this->package_name() == "runtime"
1691 : 39435 : && (Gogo::unpack_hidden_name(no->name()) == "gcRoots"
1692 : 19396 : || Gogo::unpack_hidden_name(no->name()) == "gcRootsIndex"))
1693 : : ;
1694 : : else
1695 : 19389 : var_gc.push_back(no);
1696 : : }
1697 : : }
1698 : : }
1699 : :
1700 : : // Output inline functions, which are in different packages.
1701 : 12247 : for (std::vector<Named_object*>::const_iterator p =
1702 : 4645 : this->imported_inline_functions_.begin();
1703 : 16892 : p != this->imported_inline_functions_.end();
1704 : 12247 : ++p)
1705 : 12247 : (*p)->get_backend(this, const_decls, type_decls, func_decls);
1706 : :
1707 : : // Build the list of type descriptors.
1708 : 4645 : this->build_type_descriptor_list();
1709 : :
1710 : 4645 : if (this->is_main_package())
1711 : : {
1712 : : // Register the type descriptor lists, so that at run time
1713 : : // the reflect package can find compiler-created types, and
1714 : : // deduplicate if the same type is created with reflection.
1715 : : // This needs to be done before calling any package's init
1716 : : // function, as it may create type through reflection.
1717 : 1889 : this->register_type_descriptors(init_stmts, init_bfn);
1718 : :
1719 : : // Initialize imported packages.
1720 : 1889 : this->init_imports(init_stmts, init_bfn);
1721 : : }
1722 : :
1723 : : // Register global variables with the garbage collector.
1724 : 4645 : this->register_gc_vars(var_gc, init_stmts, init_bfn);
1725 : :
1726 : : // Simple variable initializations, after all variables are
1727 : : // registered.
1728 : 4645 : init_stmts.push_back(this->backend()->statement_list(var_init_stmts));
1729 : :
1730 : : // Complete variable initializations, first sorting them into a
1731 : : // workable order.
1732 : 4645 : if (!var_inits.empty())
1733 : : {
1734 : 1857 : sort_var_inits(&var_inits);
1735 : 1857 : for (Var_inits::const_iterator p = var_inits.begin();
1736 : 14871 : p != var_inits.end();
1737 : 13014 : ++p)
1738 : 13014 : init_stmts.push_back(p->init());
1739 : : }
1740 : :
1741 : : // After all the variables are initialized, call the init
1742 : : // functions if there are any. Init functions take no arguments, so
1743 : : // we pass in EMPTY_ARGS to call them.
1744 : 4645 : std::vector<Bexpression*> empty_args;
1745 : 1884 : for (std::vector<Named_object*>::const_iterator p =
1746 : 4645 : this->init_functions_.begin();
1747 : 6529 : p != this->init_functions_.end();
1748 : 1884 : ++p)
1749 : : {
1750 : 1884 : Location func_loc = (*p)->location();
1751 : 1884 : Function* func = (*p)->func_value();
1752 : 1884 : Bfunction* initfn = func->get_or_make_decl(this, *p);
1753 : 1884 : Bexpression* func_code =
1754 : 1884 : this->backend()->function_code_expression(initfn, func_loc);
1755 : 1884 : Bexpression* call = this->backend()->call_expression(init_bfn, func_code,
1756 : : empty_args,
1757 : : NULL, func_loc);
1758 : 1884 : Bstatement* ist = this->backend()->expression_statement(init_bfn, call);
1759 : 1884 : init_stmts.push_back(ist);
1760 : : }
1761 : :
1762 : : // Set up a magic function to do all the initialization actions.
1763 : : // This will be called if this package is imported.
1764 : 4645 : Bstatement* init_fncode = this->backend()->statement_list(init_stmts);
1765 : 4645 : if (this->need_init_fn_ || this->is_main_package())
1766 : : {
1767 : 3449 : init_fndecl =
1768 : 3449 : this->create_initialization_function(init_fndecl, init_fncode);
1769 : 3449 : if (init_fndecl != NULL)
1770 : 3408 : func_decls.push_back(init_fndecl->func_value()->get_decl());
1771 : : }
1772 : :
1773 : : // We should not have seen any new bindings created during the conversion.
1774 : 4645 : go_assert(count_definitions == this->current_bindings()->size_definitions());
1775 : :
1776 : : // Define all globally declared values.
1777 : 4645 : if (!saw_errors())
1778 : 4216 : this->backend()->write_global_definitions(type_decls, const_decls,
1779 : : func_decls, var_decls);
1780 : 4645 : }
1781 : :
1782 : : // Return the current block.
1783 : :
1784 : : Block*
1785 : 927152 : Gogo::current_block()
1786 : : {
1787 : 927152 : if (this->functions_.empty())
1788 : : return NULL;
1789 : : else
1790 : 927151 : return this->functions_.back().blocks.back();
1791 : : }
1792 : :
1793 : : // Look up a name in the current binding contour. If PFUNCTION is not
1794 : : // NULL, set it to the function in which the name is defined, or NULL
1795 : : // if the name is defined in global scope.
1796 : :
1797 : : Named_object*
1798 : 4071240 : Gogo::lookup(const std::string& name, Named_object** pfunction) const
1799 : : {
1800 : 4071240 : if (pfunction != NULL)
1801 : 3213780 : *pfunction = NULL;
1802 : :
1803 : 4071240 : if (Gogo::is_sink_name(name))
1804 : 8421 : return Named_object::make_sink();
1805 : :
1806 : 5430772 : for (Open_functions::const_reverse_iterator p = this->functions_.rbegin();
1807 : 5430772 : p != this->functions_.rend();
1808 : 1367953 : ++p)
1809 : : {
1810 : 3485451 : Named_object* ret = p->blocks.back()->bindings()->lookup(name);
1811 : 3485451 : if (ret != NULL)
1812 : : {
1813 : 2117498 : if (pfunction != NULL)
1814 : 1860217 : *pfunction = p->function;
1815 : 2117498 : return ret;
1816 : : }
1817 : : }
1818 : :
1819 : 1945321 : if (this->package_ != NULL)
1820 : : {
1821 : 1945321 : Named_object* ret = this->package_->bindings()->lookup(name);
1822 : 1945321 : if (ret != NULL)
1823 : : {
1824 : 1744235 : if (ret->package() != NULL)
1825 : : {
1826 : 26027 : std::string dot_alias = "." + ret->package()->package_name();
1827 : 26027 : ret->package()->note_usage(dot_alias);
1828 : 26027 : }
1829 : 1744235 : return ret;
1830 : : }
1831 : : }
1832 : :
1833 : : // We do not look in the global namespace. If we did, the global
1834 : : // namespace would effectively hide names which were defined in
1835 : : // package scope which we have not yet seen. Instead,
1836 : : // define_global_names is called after parsing is over to connect
1837 : : // undefined names at package scope with names defined at global
1838 : : // scope.
1839 : :
1840 : : return NULL;
1841 : : }
1842 : :
1843 : : // Look up a name in the current block, without searching enclosing
1844 : : // blocks.
1845 : :
1846 : : Named_object*
1847 : 306705 : Gogo::lookup_in_block(const std::string& name) const
1848 : : {
1849 : 306705 : go_assert(!this->functions_.empty());
1850 : 306705 : go_assert(!this->functions_.back().blocks.empty());
1851 : 306705 : return this->functions_.back().blocks.back()->bindings()->lookup_local(name);
1852 : : }
1853 : :
1854 : : // Look up a name in the global namespace.
1855 : :
1856 : : Named_object*
1857 : 639601 : Gogo::lookup_global(const char* name) const
1858 : : {
1859 : 639601 : return this->globals_->lookup(name);
1860 : : }
1861 : :
1862 : : // Add an imported package.
1863 : :
1864 : : Package*
1865 : 25881 : Gogo::add_imported_package(const std::string& real_name,
1866 : : const std::string& alias_arg,
1867 : : bool is_alias_exported,
1868 : : const std::string& pkgpath,
1869 : : const std::string& pkgpath_symbol,
1870 : : Location location,
1871 : : bool* padd_to_globals)
1872 : : {
1873 : 25881 : Package* ret = this->register_package(pkgpath, pkgpath_symbol, location);
1874 : 25881 : ret->set_package_name(real_name, location);
1875 : :
1876 : 25881 : *padd_to_globals = false;
1877 : :
1878 : 25881 : if (alias_arg == "_")
1879 : : ;
1880 : 20914 : else if (alias_arg == ".")
1881 : : {
1882 : 10 : *padd_to_globals = true;
1883 : 10 : std::string dot_alias = "." + real_name;
1884 : 10 : ret->add_alias(dot_alias, location);
1885 : 10 : }
1886 : : else
1887 : : {
1888 : 20904 : std::string alias = alias_arg;
1889 : 20904 : if (alias.empty())
1890 : : {
1891 : 20308 : alias = real_name;
1892 : 20308 : is_alias_exported = Lex::is_exported_name(alias);
1893 : : }
1894 : 20904 : ret->add_alias(alias, location);
1895 : 20904 : alias = this->pack_hidden_name(alias, is_alias_exported);
1896 : 20904 : Named_object* no = this->package_->bindings()->add_package(alias, ret);
1897 : 20904 : if (!no->is_package())
1898 : 0 : return NULL;
1899 : 20904 : }
1900 : :
1901 : : return ret;
1902 : : }
1903 : :
1904 : : // Register a package. This package may or may not be imported. This
1905 : : // returns the Package structure for the package, creating if it
1906 : : // necessary. LOCATION is the location of the import statement that
1907 : : // led us to see this package. PKGPATH_SYMBOL is the symbol to use
1908 : : // for names in the package; it may be the empty string, in which case
1909 : : // we either get it later or make a guess when we need it.
1910 : :
1911 : : Package*
1912 : 734292 : Gogo::register_package(const std::string& pkgpath,
1913 : : const std::string& pkgpath_symbol, Location location)
1914 : : {
1915 : 734292 : Package* package = NULL;
1916 : 734292 : std::pair<Packages::iterator, bool> ins =
1917 : 734292 : this->packages_.insert(std::make_pair(pkgpath, package));
1918 : 734292 : if (!ins.second)
1919 : : {
1920 : : // We have seen this package name before.
1921 : 617651 : package = ins.first->second;
1922 : 617651 : go_assert(package != NULL && package->pkgpath() == pkgpath);
1923 : 617651 : if (!pkgpath_symbol.empty())
1924 : 14338 : package->set_pkgpath_symbol(pkgpath_symbol);
1925 : 617651 : if (Linemap::is_unknown_location(package->location()))
1926 : 318737 : package->set_location(location);
1927 : : }
1928 : : else
1929 : : {
1930 : : // First time we have seen this package name.
1931 : 116641 : package = new Package(pkgpath, pkgpath_symbol, location);
1932 : 116641 : go_assert(ins.first->second == NULL);
1933 : 116641 : ins.first->second = package;
1934 : : }
1935 : :
1936 : 734292 : return package;
1937 : : }
1938 : :
1939 : : // Return the pkgpath symbol for a package, given the pkgpath.
1940 : :
1941 : : std::string
1942 : 0 : Gogo::pkgpath_symbol_for_package(const std::string& pkgpath)
1943 : : {
1944 : 0 : Packages::iterator p = this->packages_.find(pkgpath);
1945 : 0 : go_assert(p != this->packages_.end());
1946 : 0 : return p->second->pkgpath_symbol();
1947 : : }
1948 : :
1949 : : // Start compiling a function.
1950 : :
1951 : : Named_object*
1952 : 283392 : Gogo::start_function(const std::string& name, Function_type* type,
1953 : : bool add_method_to_type, Location location)
1954 : : {
1955 : 283392 : bool at_top_level = this->functions_.empty();
1956 : :
1957 : 283392 : Block* block = new Block(NULL, location);
1958 : :
1959 : 283392 : Named_object* enclosing = (at_top_level
1960 : 283392 : ? NULL
1961 : 24308 : : this->functions_.back().function);
1962 : :
1963 : 283392 : Function* function = new Function(type, enclosing, block, location);
1964 : :
1965 : 283392 : if (type->is_method())
1966 : : {
1967 : 77457 : const Typed_identifier* receiver = type->receiver();
1968 : 77457 : Variable* this_param = new Variable(receiver->type(), NULL, false,
1969 : 77457 : true, true, location);
1970 : 77457 : std::string rname = receiver->name();
1971 : 77457 : unsigned rcounter = 0;
1972 : :
1973 : : // We need to give a nameless receiver parameter a synthesized name to
1974 : : // avoid having it clash with some other nameless param. FIXME.
1975 : 77457 : Gogo::rename_if_empty(&rname, "r", &rcounter);
1976 : :
1977 : 77457 : block->bindings()->add_variable(rname, NULL, this_param);
1978 : 77457 : }
1979 : :
1980 : 283392 : const Typed_identifier_list* parameters = type->parameters();
1981 : 283392 : bool is_varargs = type->is_varargs();
1982 : 283392 : unsigned pcounter = 0;
1983 : 283392 : if (parameters != NULL)
1984 : : {
1985 : 217991 : for (Typed_identifier_list::const_iterator p = parameters->begin();
1986 : 596801 : p != parameters->end();
1987 : 378810 : ++p)
1988 : : {
1989 : 378810 : Variable* param = new Variable(p->type(), NULL, false, true, false,
1990 : 378810 : p->location());
1991 : 378810 : if (is_varargs && p + 1 == parameters->end())
1992 : 2018 : param->set_is_varargs_parameter();
1993 : :
1994 : 378810 : std::string pname = p->name();
1995 : :
1996 : : // We need to give each nameless parameter a non-empty name to avoid
1997 : : // having it clash with some other nameless param. FIXME.
1998 : 378810 : Gogo::rename_if_empty(&pname, "p", &pcounter);
1999 : :
2000 : 378810 : block->bindings()->add_variable(pname, NULL, param);
2001 : 378810 : }
2002 : : }
2003 : :
2004 : 283392 : function->create_result_variables(this);
2005 : :
2006 : 283392 : const std::string* pname;
2007 : 283392 : std::string nested_name;
2008 : 283392 : bool is_init = false;
2009 : 566784 : if (Gogo::unpack_hidden_name(name) == "init" && !type->is_method())
2010 : : {
2011 : 1884 : if ((type->parameters() != NULL && !type->parameters()->empty())
2012 : 1884 : || (type->results() != NULL && !type->results()->empty()))
2013 : 0 : go_error_at(location,
2014 : : "func init must have no arguments and no return values");
2015 : : // There can be multiple "init" functions, so give them each a
2016 : : // different name.
2017 : 1884 : nested_name = this->init_function_name();
2018 : 1884 : pname = &nested_name;
2019 : 1884 : is_init = true;
2020 : : }
2021 : 281508 : else if (!name.empty())
2022 : : pname = &name;
2023 : : else
2024 : : {
2025 : : // Invent a name for a nested function.
2026 : 25609 : nested_name = this->nested_function_name(enclosing);
2027 : 25609 : pname = &nested_name;
2028 : : }
2029 : :
2030 : 283392 : Named_object* ret;
2031 : 283392 : if (Gogo::is_sink_name(*pname))
2032 : : {
2033 : 305 : std::string sname(this->sink_function_name());
2034 : 305 : ret = Named_object::make_function(sname, NULL, function);
2035 : 305 : ret->func_value()->set_is_sink();
2036 : :
2037 : 305 : if (!type->is_method())
2038 : 300 : ret = this->package_->bindings()->add_named_object(ret);
2039 : 5 : else if (add_method_to_type)
2040 : : {
2041 : : // We should report errors even for sink methods.
2042 : 5 : Type* rtype = type->receiver()->type();
2043 : : // Avoid points_to and deref to avoid getting an error if
2044 : : // the type is not yet defined.
2045 : 5 : if (rtype->classification() == Type::TYPE_POINTER)
2046 : 2 : rtype = rtype->points_to();
2047 : 5 : while (rtype->named_type() != NULL
2048 : 5 : && rtype->named_type()->is_alias())
2049 : 0 : rtype = rtype->named_type()->real_type()->forwarded();
2050 : 5 : if (rtype->is_error_type())
2051 : : ;
2052 : 5 : else if (rtype->named_type() != NULL)
2053 : : {
2054 : 4 : if (rtype->named_type()->named_object()->package() != NULL)
2055 : 0 : go_error_at(type->receiver()->location(),
2056 : : "may not define methods on non-local type");
2057 : : }
2058 : 1 : else if (rtype->forward_declaration_type() != NULL)
2059 : : {
2060 : : // Go ahead and add the method in case we need to report
2061 : : // an error when we see the definition.
2062 : 1 : rtype->forward_declaration_type()->add_existing_method(ret);
2063 : : }
2064 : : else
2065 : 0 : go_error_at(type->receiver()->location(),
2066 : : ("invalid receiver type "
2067 : : "(receiver must be a named type)"));
2068 : : }
2069 : 305 : }
2070 : 283087 : else if (!type->is_method())
2071 : : {
2072 : 205635 : ret = this->package_->bindings()->add_function(*pname, NULL, function);
2073 : 205635 : if (!ret->is_function() || ret->func_value() != function)
2074 : : {
2075 : : // Redefinition error. Invent a name to avoid knockon
2076 : : // errors.
2077 : 2 : std::string rname(this->redefined_function_name());
2078 : 2 : ret = this->package_->bindings()->add_function(rname, NULL, function);
2079 : 2 : }
2080 : : }
2081 : : else
2082 : : {
2083 : 77452 : if (!add_method_to_type)
2084 : 24262 : ret = Named_object::make_function(name, NULL, function);
2085 : : else
2086 : : {
2087 : 53190 : go_assert(at_top_level);
2088 : 53190 : Type* rtype = type->receiver()->type();
2089 : :
2090 : 53190 : while (rtype->named_type() != NULL
2091 : 53202 : && rtype->named_type()->is_alias())
2092 : 12 : rtype = rtype->named_type()->real_type()->forwarded();
2093 : :
2094 : : // We want to look through the pointer created by the
2095 : : // parser, without getting an error if the type is not yet
2096 : : // defined.
2097 : 53190 : if (rtype->classification() == Type::TYPE_POINTER)
2098 : 43456 : rtype = rtype->points_to();
2099 : :
2100 : 53192 : while (rtype->named_type() != NULL
2101 : 53192 : && rtype->named_type()->is_alias())
2102 : 2 : rtype = rtype->named_type()->real_type()->forwarded();
2103 : :
2104 : 53190 : if (rtype->is_error_type())
2105 : 0 : ret = Named_object::make_function(name, NULL, function);
2106 : 53190 : else if (rtype->named_type() != NULL)
2107 : : {
2108 : 50650 : if (rtype->named_type()->named_object()->package() != NULL)
2109 : : {
2110 : 3 : go_error_at(type->receiver()->location(),
2111 : : "may not define methods on non-local type");
2112 : 3 : ret = Named_object::make_function(name, NULL, function);
2113 : : }
2114 : : else
2115 : : {
2116 : 50647 : ret = rtype->named_type()->add_method(name, function);
2117 : 50647 : if (!ret->is_function())
2118 : : {
2119 : : // Redefinition error.
2120 : 2 : ret = Named_object::make_function(name, NULL, function);
2121 : : }
2122 : : }
2123 : : }
2124 : 2540 : else if (rtype->forward_declaration_type() != NULL)
2125 : : {
2126 : 2538 : Named_object* type_no =
2127 : 2538 : rtype->forward_declaration_type()->named_object();
2128 : 2538 : if (type_no->is_unknown())
2129 : : {
2130 : : // If we are seeing methods it really must be a
2131 : : // type. Declare it as such. An alternative would
2132 : : // be to support lists of methods for unknown
2133 : : // expressions. Either way the error messages if
2134 : : // this is not a type are going to get confusing.
2135 : 519 : Named_object* declared =
2136 : 519 : this->declare_package_type(type_no->name(),
2137 : : type_no->location());
2138 : 519 : go_assert(declared
2139 : : == type_no->unknown_value()->real_named_object());
2140 : : }
2141 : 2538 : ret = rtype->forward_declaration_type()->add_method(name,
2142 : : function);
2143 : : }
2144 : : else
2145 : : {
2146 : 2 : go_error_at(type->receiver()->location(),
2147 : : ("invalid receiver type (receiver must "
2148 : : "be a named type)"));
2149 : 2 : ret = Named_object::make_function(name, NULL, function);
2150 : : }
2151 : : }
2152 : 77452 : this->package_->bindings()->add_method(ret);
2153 : : }
2154 : :
2155 : 283392 : this->functions_.resize(this->functions_.size() + 1);
2156 : 283392 : Open_function& of(this->functions_.back());
2157 : 283392 : of.function = ret;
2158 : 283392 : of.blocks.push_back(block);
2159 : :
2160 : 283392 : if (is_init)
2161 : : {
2162 : 1884 : this->init_functions_.push_back(ret);
2163 : 1884 : this->need_init_fn_ = true;
2164 : : }
2165 : :
2166 : 283392 : return ret;
2167 : 283392 : }
2168 : :
2169 : : // Finish compiling a function.
2170 : :
2171 : : void
2172 : 283392 : Gogo::finish_function(Location location)
2173 : : {
2174 : 283392 : this->finish_block(location);
2175 : 283392 : go_assert(this->functions_.back().blocks.empty());
2176 : 283392 : this->functions_.pop_back();
2177 : 283392 : }
2178 : :
2179 : : // Return the current function.
2180 : :
2181 : : Named_object*
2182 : 2109334 : Gogo::current_function() const
2183 : : {
2184 : 2109334 : go_assert(!this->functions_.empty());
2185 : 2109334 : return this->functions_.back().function;
2186 : : }
2187 : :
2188 : : // Start a new block.
2189 : :
2190 : : void
2191 : 912962 : Gogo::start_block(Location location)
2192 : : {
2193 : 912962 : go_assert(!this->functions_.empty());
2194 : 912962 : Block* block = new Block(this->current_block(), location);
2195 : 912962 : this->functions_.back().blocks.push_back(block);
2196 : 912962 : }
2197 : :
2198 : : // Finish a block.
2199 : :
2200 : : Block*
2201 : 1196354 : Gogo::finish_block(Location location)
2202 : : {
2203 : 1196354 : go_assert(!this->functions_.empty());
2204 : 1196354 : go_assert(!this->functions_.back().blocks.empty());
2205 : 1196354 : Block* block = this->functions_.back().blocks.back();
2206 : 1196354 : this->functions_.back().blocks.pop_back();
2207 : 1196354 : block->set_end_location(location);
2208 : 1196354 : return block;
2209 : : }
2210 : :
2211 : : // Add an erroneous name.
2212 : :
2213 : : Named_object*
2214 : 14 : Gogo::add_erroneous_name(const std::string& name)
2215 : : {
2216 : 14 : return this->package_->bindings()->add_erroneous_name(name);
2217 : : }
2218 : :
2219 : : // Add an unknown name.
2220 : :
2221 : : Named_object*
2222 : 85535 : Gogo::add_unknown_name(const std::string& name, Location location)
2223 : : {
2224 : 85535 : return this->package_->bindings()->add_unknown_name(name, location);
2225 : : }
2226 : :
2227 : : // Declare a function.
2228 : :
2229 : : Named_object*
2230 : 5732 : Gogo::declare_function(const std::string& name, Function_type* type,
2231 : : Location location)
2232 : : {
2233 : 5732 : if (!type->is_method())
2234 : 5669 : return this->current_bindings()->add_function_declaration(name, NULL, type,
2235 : 5669 : location);
2236 : : else
2237 : : {
2238 : : // We don't bother to add this to the list of global
2239 : : // declarations.
2240 : 63 : Type* rtype = type->receiver()->type();
2241 : :
2242 : 63 : while (rtype->named_type() != NULL
2243 : 63 : && rtype->named_type()->is_alias())
2244 : 0 : rtype = rtype->named_type()->real_type()->forwarded();
2245 : :
2246 : : // We want to look through the pointer created by the
2247 : : // parser, without getting an error if the type is not yet
2248 : : // defined.
2249 : 63 : if (rtype->classification() == Type::TYPE_POINTER)
2250 : 5 : rtype = rtype->points_to();
2251 : :
2252 : 63 : while (rtype->named_type() != NULL
2253 : 63 : && rtype->named_type()->is_alias())
2254 : 0 : rtype = rtype->named_type()->real_type()->forwarded();
2255 : :
2256 : 63 : if (rtype->is_error_type())
2257 : : return NULL;
2258 : 63 : else if (rtype->named_type() != NULL)
2259 : 63 : return rtype->named_type()->add_method_declaration(name, NULL, type,
2260 : 63 : location);
2261 : 0 : else if (rtype->forward_declaration_type() != NULL)
2262 : : {
2263 : 0 : Forward_declaration_type* ftype = rtype->forward_declaration_type();
2264 : 0 : return ftype->add_method_declaration(name, NULL, type, location);
2265 : : }
2266 : : else
2267 : : {
2268 : 0 : go_error_at(type->receiver()->location(),
2269 : : "invalid receiver type (receiver must be a named type)");
2270 : 0 : return Named_object::make_erroneous_name(name);
2271 : : }
2272 : : }
2273 : : }
2274 : :
2275 : : // Add a label definition.
2276 : :
2277 : : Label*
2278 : 10525 : Gogo::add_label_definition(const std::string& label_name,
2279 : : Location location)
2280 : : {
2281 : 10525 : go_assert(!this->functions_.empty());
2282 : 10525 : Function* func = this->functions_.back().function->func_value();
2283 : 10525 : Label* label = func->add_label_definition(this, label_name, location);
2284 : 10525 : this->add_statement(Statement::make_label_statement(label, location));
2285 : 10525 : return label;
2286 : : }
2287 : :
2288 : : // Add a label reference.
2289 : :
2290 : : Label*
2291 : 10958 : Gogo::add_label_reference(const std::string& label_name,
2292 : : Location location, bool issue_goto_errors)
2293 : : {
2294 : 10958 : go_assert(!this->functions_.empty());
2295 : 10958 : Function* func = this->functions_.back().function->func_value();
2296 : 10958 : return func->add_label_reference(this, label_name, location,
2297 : 10958 : issue_goto_errors);
2298 : : }
2299 : :
2300 : : // Return the current binding state.
2301 : :
2302 : : Bindings_snapshot*
2303 : 12092 : Gogo::bindings_snapshot(Location location)
2304 : : {
2305 : 12092 : return new Bindings_snapshot(this->current_block(), location);
2306 : : }
2307 : :
2308 : : // Add a statement.
2309 : :
2310 : : void
2311 : 1798441 : Gogo::add_statement(Statement* statement)
2312 : : {
2313 : 1798441 : go_assert(!this->functions_.empty()
2314 : : && !this->functions_.back().blocks.empty());
2315 : 1798441 : this->functions_.back().blocks.back()->add_statement(statement);
2316 : 1798441 : }
2317 : :
2318 : : // Add a block.
2319 : :
2320 : : void
2321 : 444165 : Gogo::add_block(Block* block, Location location)
2322 : : {
2323 : 444165 : go_assert(!this->functions_.empty()
2324 : : && !this->functions_.back().blocks.empty());
2325 : 444165 : Statement* statement = Statement::make_block_statement(block, location);
2326 : 444165 : this->functions_.back().blocks.back()->add_statement(statement);
2327 : 444165 : }
2328 : :
2329 : : // Add a constant.
2330 : :
2331 : : Named_object*
2332 : 152210 : Gogo::add_constant(const Typed_identifier& tid, Expression* expr,
2333 : : int iota_value)
2334 : : {
2335 : 152210 : return this->current_bindings()->add_constant(tid, NULL, expr, iota_value);
2336 : : }
2337 : :
2338 : : // Add a type.
2339 : :
2340 : : void
2341 : 0 : Gogo::add_type(const std::string& name, Type* type, Location location)
2342 : : {
2343 : 0 : Named_object* no = this->current_bindings()->add_type(name, NULL, type,
2344 : : location);
2345 : 0 : if (!this->in_global_scope() && no->is_type())
2346 : : {
2347 : 0 : Named_object* f = this->functions_.back().function;
2348 : 0 : unsigned int index;
2349 : 0 : if (f->is_function())
2350 : 0 : index = f->func_value()->new_local_type_index();
2351 : : else
2352 : : index = 0;
2353 : 0 : no->type_value()->set_in_function(f, index);
2354 : : }
2355 : 0 : }
2356 : :
2357 : : // Add a named type.
2358 : :
2359 : : void
2360 : 97578 : Gogo::add_named_type(Named_type* type)
2361 : : {
2362 : 97578 : go_assert(this->in_global_scope());
2363 : 97578 : this->current_bindings()->add_named_type(type);
2364 : 97578 : }
2365 : :
2366 : : // Declare a type.
2367 : :
2368 : : Named_object*
2369 : 31951 : Gogo::declare_type(const std::string& name, Location location)
2370 : : {
2371 : 31951 : Bindings* bindings = this->current_bindings();
2372 : 31951 : Named_object* no = bindings->add_type_declaration(name, NULL, location);
2373 : 31951 : if (!this->in_global_scope() && no->is_type_declaration())
2374 : : {
2375 : 1295 : Named_object* f = this->functions_.back().function;
2376 : 1295 : unsigned int index;
2377 : 1295 : if (f->is_function())
2378 : 1295 : index = f->func_value()->new_local_type_index();
2379 : : else
2380 : : index = 0;
2381 : 1295 : no->type_declaration_value()->set_in_function(f, index);
2382 : : }
2383 : 31951 : return no;
2384 : : }
2385 : :
2386 : : // Declare a type at the package level.
2387 : :
2388 : : Named_object*
2389 : 519 : Gogo::declare_package_type(const std::string& name, Location location)
2390 : : {
2391 : 519 : return this->package_->bindings()->add_type_declaration(name, NULL, location);
2392 : : }
2393 : :
2394 : : // Declare a function at the package level.
2395 : :
2396 : : Named_object*
2397 : 92632 : Gogo::declare_package_function(const std::string& name, Function_type* type,
2398 : : Location location)
2399 : : {
2400 : 92632 : return this->package_->bindings()->add_function_declaration(name, NULL, type,
2401 : 92632 : location);
2402 : : }
2403 : :
2404 : : // Add a function declaration to the list of functions we may want to
2405 : : // inline.
2406 : :
2407 : : void
2408 : 82289 : Gogo::add_imported_inlinable_function(Named_object* no)
2409 : : {
2410 : 82289 : go_assert(no->is_function_declaration());
2411 : 82289 : Function_declaration* fd = no->func_declaration_value();
2412 : 82289 : if (fd->is_on_inlinable_list())
2413 : : return;
2414 : 12247 : this->imported_inlinable_functions_.push_back(no);
2415 : 12247 : fd->set_is_on_inlinable_list();
2416 : : }
2417 : :
2418 : : // Define a type which was already declared.
2419 : :
2420 : : void
2421 : 31951 : Gogo::define_type(Named_object* no, Named_type* type)
2422 : : {
2423 : 31951 : this->current_bindings()->define_type(no, type);
2424 : 31951 : }
2425 : :
2426 : : // Add a variable.
2427 : :
2428 : : Named_object*
2429 : 405952 : Gogo::add_variable(const std::string& name, Variable* variable)
2430 : : {
2431 : 405952 : Named_object* no = this->current_bindings()->add_variable(name, NULL,
2432 : : variable);
2433 : :
2434 : : // In a function the middle-end wants to see a DECL_EXPR node.
2435 : 405952 : if (no != NULL
2436 : 405952 : && no->is_variable()
2437 : 405950 : && !no->var_value()->is_parameter()
2438 : 811901 : && !this->functions_.empty())
2439 : 380475 : this->add_statement(Statement::make_variable_declaration(no));
2440 : :
2441 : 405952 : return no;
2442 : : }
2443 : :
2444 : : void
2445 : 473841 : Gogo::rename_if_empty(std::string* pname, const char* tag, unsigned* count)
2446 : : {
2447 : 473841 : if (pname->empty() || Gogo::is_sink_name(*pname))
2448 : : {
2449 : 3395 : char buf[50];
2450 : 3395 : go_assert(strlen(tag) < 10);
2451 : 3395 : snprintf(buf, sizeof buf, "%s.%u", tag, *count);
2452 : 3395 : ++(*count);
2453 : 3395 : *pname = buf;
2454 : : }
2455 : 473841 : }
2456 : :
2457 : :
2458 : : // Add a sink--a reference to the blank identifier _.
2459 : :
2460 : : Named_object*
2461 : 19303 : Gogo::add_sink()
2462 : : {
2463 : 19303 : return Named_object::make_sink();
2464 : : }
2465 : :
2466 : : // Add a named object for a dot import.
2467 : :
2468 : : void
2469 : 62469 : Gogo::add_dot_import_object(Named_object* no)
2470 : : {
2471 : : // If the name already exists, then it was defined in some file seen
2472 : : // earlier. If the earlier name is just a declaration, don't add
2473 : : // this name, because that will cause the previous declaration to
2474 : : // merge to this imported name, which should not happen. Just add
2475 : : // this name to the list of file block names to get appropriate
2476 : : // errors if we see a later definition.
2477 : 62469 : Named_object* e = this->package_->bindings()->lookup(no->name());
2478 : 62469 : if (e != NULL && e->package() == NULL)
2479 : : {
2480 : 1 : if (e->is_unknown())
2481 : 1 : e = e->resolve();
2482 : 1 : if (e->package() == NULL
2483 : 1 : && (e->is_type_declaration()
2484 : : || e->is_function_declaration()
2485 : : || e->is_unknown()))
2486 : : {
2487 : 1 : this->add_file_block_name(no->name(), no->location());
2488 : 1 : return;
2489 : : }
2490 : : }
2491 : :
2492 : 62468 : this->current_bindings()->add_named_object(no);
2493 : : }
2494 : :
2495 : : // Add a linkname. This implements the go:linkname compiler directive.
2496 : : // We only support this for functions and function declarations.
2497 : :
2498 : : void
2499 : 3913 : Gogo::add_linkname(const std::string& go_name, bool is_exported,
2500 : : const std::string& ext_name, Location loc)
2501 : : {
2502 : 3913 : Named_object* no =
2503 : 3913 : this->package_->bindings()->lookup(this->pack_hidden_name(go_name,
2504 : : is_exported));
2505 : 3913 : if (no == NULL)
2506 : 0 : go_error_at(loc, "%s is not defined", go_name.c_str());
2507 : 3913 : else if (no->is_function())
2508 : : {
2509 : 2641 : if (ext_name.empty())
2510 : 1673 : no->func_value()->set_is_exported_by_linkname();
2511 : : else
2512 : 968 : no->func_value()->set_asm_name(ext_name);
2513 : : }
2514 : 1272 : else if (no->is_function_declaration())
2515 : : {
2516 : 1272 : if (ext_name.empty())
2517 : 0 : go_error_at(loc,
2518 : : ("%<//go:linkname%> missing external name "
2519 : : "for declaration of %s"),
2520 : : go_name.c_str());
2521 : : else
2522 : 1272 : no->func_declaration_value()->set_asm_name(ext_name);
2523 : : }
2524 : : else
2525 : 0 : go_error_at(loc,
2526 : : ("%s is not a function; "
2527 : : "%<//go:linkname%> is only supported for functions"),
2528 : : go_name.c_str());
2529 : 3913 : }
2530 : :
2531 : : // Mark all local variables used. This is used when some types of
2532 : : // parse error occur.
2533 : :
2534 : : void
2535 : 79 : Gogo::mark_locals_used()
2536 : : {
2537 : 119 : for (Open_functions::iterator pf = this->functions_.begin();
2538 : 119 : pf != this->functions_.end();
2539 : 40 : ++pf)
2540 : : {
2541 : 96 : for (std::vector<Block*>::iterator pb = pf->blocks.begin();
2542 : 96 : pb != pf->blocks.end();
2543 : 56 : ++pb)
2544 : 56 : (*pb)->bindings()->mark_locals_used();
2545 : : }
2546 : 79 : }
2547 : :
2548 : : // Record that we've seen an interface type.
2549 : :
2550 : : void
2551 : 71624 : Gogo::record_interface_type(Interface_type* itype)
2552 : : {
2553 : 71624 : this->interface_types_.push_back(itype);
2554 : 71624 : }
2555 : :
2556 : : // Define the global names. We do this only after parsing all the
2557 : : // input files, because the program might define the global names
2558 : : // itself.
2559 : :
2560 : : void
2561 : 4645 : Gogo::define_global_names()
2562 : : {
2563 : 4645 : if (this->is_main_package())
2564 : : {
2565 : : // Every Go program has to import the runtime package, so that
2566 : : // it is properly initialized. We can't use
2567 : : // predeclared_location here as it will cause runtime functions
2568 : : // to appear to be builtin functions.
2569 : 1889 : this->import_package("runtime", "_", false, false,
2570 : 1889 : this->package_->location());
2571 : : }
2572 : :
2573 : 185800 : for (Bindings::const_declarations_iterator p =
2574 : 4645 : this->globals_->begin_declarations();
2575 : 190445 : p != this->globals_->end_declarations();
2576 : 185800 : ++p)
2577 : : {
2578 : 185800 : Named_object* global_no = p->second;
2579 : 185800 : std::string name(Gogo::pack_hidden_name(global_no->name(), false));
2580 : 185800 : Named_object* no = this->package_->bindings()->lookup(name);
2581 : 185800 : if (no == NULL)
2582 : 155886 : continue;
2583 : 29914 : no = no->resolve();
2584 : 29914 : if (no->is_type_declaration())
2585 : : {
2586 : 2 : if (global_no->is_type())
2587 : : {
2588 : 2 : if (no->type_declaration_value()->has_methods())
2589 : : {
2590 : 4 : for (std::vector<Named_object*>::const_iterator pm =
2591 : 2 : no->type_declaration_value()->methods()->begin();
2592 : 4 : pm != no->type_declaration_value()->methods()->end();
2593 : 2 : pm++)
2594 : 2 : go_error_at((*pm)->location(),
2595 : : "may not define methods on non-local type");
2596 : : }
2597 : 2 : no->set_type_value(global_no->type_value());
2598 : : }
2599 : : else
2600 : : {
2601 : 0 : go_error_at(no->location(), "expected type");
2602 : 0 : Type* errtype = Type::make_error_type();
2603 : 0 : Named_object* err =
2604 : 0 : Named_object::make_type("erroneous_type", NULL, errtype,
2605 : : Linemap::predeclared_location());
2606 : 0 : no->set_type_value(err->type_value());
2607 : : }
2608 : : }
2609 : 29912 : else if (no->is_unknown())
2610 : 29815 : no->unknown_value()->set_real_named_object(global_no);
2611 : 185800 : }
2612 : :
2613 : : // Give an error if any name is defined in both the package block
2614 : : // and the file block. For example, this can happen if one file
2615 : : // imports "fmt" and another file defines a global variable fmt.
2616 : 334188 : for (Bindings::const_declarations_iterator p =
2617 : 4645 : this->package_->bindings()->begin_declarations();
2618 : 338833 : p != this->package_->bindings()->end_declarations();
2619 : 334188 : ++p)
2620 : : {
2621 : 334188 : if (p->second->is_unknown()
2622 : 334188 : && p->second->unknown_value()->real_named_object() == NULL)
2623 : : {
2624 : : // No point in warning about an undefined name, as we will
2625 : : // get other errors later anyhow.
2626 : 53 : continue;
2627 : : }
2628 : 334135 : File_block_names::const_iterator pf =
2629 : 334135 : this->file_block_names_.find(p->second->name());
2630 : 334135 : if (pf != this->file_block_names_.end())
2631 : : {
2632 : 1 : std::string n = p->second->message_name();
2633 : 1 : go_error_at(p->second->location(),
2634 : : "%qs defined as both imported name and global name",
2635 : : n.c_str());
2636 : 1 : go_inform(pf->second, "%qs imported here", n.c_str());
2637 : 1 : }
2638 : :
2639 : : // No package scope identifier may be named "init".
2640 : 668270 : if (!p->second->is_function()
2641 : 334135 : && Gogo::unpack_hidden_name(p->second->name()) == "init")
2642 : : {
2643 : 4 : go_error_at(p->second->location(),
2644 : : "cannot declare init - must be func");
2645 : : }
2646 : : }
2647 : 4645 : }
2648 : :
2649 : : // Clear out names in file scope.
2650 : :
2651 : : void
2652 : 12706 : Gogo::clear_file_scope()
2653 : : {
2654 : 12706 : this->package_->bindings()->clear_file_scope(this);
2655 : :
2656 : : // Warn about packages which were imported but not used.
2657 : 12706 : bool quiet = saw_errors();
2658 : 375203 : for (Packages::iterator p = this->packages_.begin();
2659 : 375203 : p != this->packages_.end();
2660 : 362497 : ++p)
2661 : : {
2662 : 362497 : Package* package = p->second;
2663 : 362497 : if (package != this->package_ && !quiet)
2664 : : {
2665 : 389161 : for (Package::Aliases::const_iterator p1 = package->aliases().begin();
2666 : 389161 : p1 != package->aliases().end();
2667 : 39719 : ++p1)
2668 : : {
2669 : 39719 : if (!p1->second->used())
2670 : : {
2671 : : // Give a more refined error message if the alias name is known.
2672 : 13 : std::string pkg_name = package->package_name();
2673 : 13 : if (p1->first != pkg_name && p1->first[0] != '.')
2674 : : {
2675 : 8 : go_error_at(p1->second->location(),
2676 : : "imported and not used: %s as %s",
2677 : 8 : Gogo::message_name(pkg_name).c_str(),
2678 : 8 : Gogo::message_name(p1->first).c_str());
2679 : : }
2680 : : else
2681 : 9 : go_error_at(p1->second->location(),
2682 : : "imported and not used: %s",
2683 : 18 : Gogo::message_name(pkg_name).c_str());
2684 : 13 : }
2685 : : }
2686 : : }
2687 : 362497 : package->clear_used();
2688 : : }
2689 : :
2690 : 12706 : this->current_file_imported_unsafe_ = false;
2691 : 12706 : this->current_file_imported_embed_ = false;
2692 : 12706 : }
2693 : :
2694 : : // Queue up a type-specific hash function for later writing. These
2695 : : // are written out in write_specific_type_functions, called after the
2696 : : // parse tree is lowered.
2697 : :
2698 : : void
2699 : 1 : Gogo::queue_hash_function(Type* type, int64_t size, Backend_name* bname,
2700 : : Function_type* hash_fntype)
2701 : : {
2702 : 1 : go_assert(!this->specific_type_functions_are_written_);
2703 : 1 : go_assert(!this->in_global_scope());
2704 : 1 : Specific_type_function::Specific_type_function_kind kind =
2705 : : Specific_type_function::SPECIFIC_HASH;
2706 : 1 : Specific_type_function* tsf = new Specific_type_function(type, NULL, size,
2707 : : kind, bname,
2708 : 1 : hash_fntype);
2709 : 1 : this->specific_type_functions_.push_back(tsf);
2710 : 1 : }
2711 : :
2712 : : // Queue up a type-specific equal function for later writing. These
2713 : : // are written out in write_specific_type_functions, called after the
2714 : : // parse tree is lowered.
2715 : :
2716 : : void
2717 : 75 : Gogo::queue_equal_function(Type* type, Named_type* name, int64_t size,
2718 : : Backend_name* bname, Function_type* equal_fntype)
2719 : : {
2720 : 75 : go_assert(!this->specific_type_functions_are_written_);
2721 : 75 : go_assert(!this->in_global_scope());
2722 : 75 : Specific_type_function::Specific_type_function_kind kind =
2723 : : Specific_type_function::SPECIFIC_EQUAL;
2724 : 75 : Specific_type_function* tsf = new Specific_type_function(type, name, size,
2725 : : kind, bname,
2726 : 75 : equal_fntype);
2727 : 75 : this->specific_type_functions_.push_back(tsf);
2728 : 75 : }
2729 : :
2730 : : // Look for types which need specific hash or equality functions.
2731 : :
2732 : 9290 : class Specific_type_functions : public Traverse
2733 : : {
2734 : : public:
2735 : 4645 : Specific_type_functions(Gogo* gogo)
2736 : 4645 : : Traverse(traverse_types),
2737 : 4645 : gogo_(gogo)
2738 : : { }
2739 : :
2740 : : int
2741 : : type(Type*);
2742 : :
2743 : : private:
2744 : : Gogo* gogo_;
2745 : : };
2746 : :
2747 : : int
2748 : 11317765 : Specific_type_functions::type(Type* t)
2749 : : {
2750 : 11317765 : switch (t->classification())
2751 : : {
2752 : 688661 : case Type::TYPE_NAMED:
2753 : 688661 : {
2754 : 688661 : Named_type* nt = t->named_type();
2755 : 688661 : if (nt->is_alias())
2756 : : return TRAVERSE_CONTINUE;
2757 : 665472 : if (t->needs_specific_type_functions(this->gogo_))
2758 : 360315 : t->equal_function(this->gogo_, nt, NULL);
2759 : :
2760 : : // If this is a struct type, we don't want to make functions
2761 : : // for the unnamed struct.
2762 : 665472 : Type* rt = nt->real_type();
2763 : 665472 : if (rt->struct_type() == NULL)
2764 : : {
2765 : 256701 : if (Type::traverse(rt, this) == TRAVERSE_EXIT)
2766 : : return TRAVERSE_EXIT;
2767 : : }
2768 : : else
2769 : : {
2770 : : // If this type is defined in another package, then we don't
2771 : : // need to worry about the unexported fields.
2772 : 408771 : bool is_defined_elsewhere = nt->named_object()->package() != NULL;
2773 : 817542 : const Struct_field_list* fields = rt->struct_type()->fields();
2774 : 2687429 : for (Struct_field_list::const_iterator p = fields->begin();
2775 : 2687429 : p != fields->end();
2776 : 2278658 : ++p)
2777 : : {
2778 : 3849361 : if (is_defined_elsewhere
2779 : 2278658 : && Gogo::is_hidden_name(p->field_name()))
2780 : 1570703 : continue;
2781 : 707955 : if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
2782 : 0 : return TRAVERSE_EXIT;
2783 : : }
2784 : : }
2785 : :
2786 : : return TRAVERSE_SKIP_COMPONENTS;
2787 : : }
2788 : :
2789 : 680040 : case Type::TYPE_STRUCT:
2790 : 680040 : case Type::TYPE_ARRAY:
2791 : 680040 : if (t->needs_specific_type_functions(this->gogo_))
2792 : 165351 : t->equal_function(this->gogo_, NULL, NULL);
2793 : : break;
2794 : :
2795 : 77572 : case Type::TYPE_MAP:
2796 : 77572 : {
2797 : 155144 : Type* key_type = t->map_type()->key_type()->unalias();
2798 : 77572 : if (key_type->needs_specific_type_functions(this->gogo_))
2799 : 37920 : key_type->hash_function(this->gogo_, NULL);
2800 : : }
2801 : : break;
2802 : :
2803 : : default:
2804 : : break;
2805 : : }
2806 : :
2807 : : return TRAVERSE_CONTINUE;
2808 : : }
2809 : :
2810 : : // Write out type specific functions.
2811 : :
2812 : : void
2813 : 4645 : Gogo::write_specific_type_functions()
2814 : : {
2815 : 4645 : Specific_type_functions stf(this);
2816 : 4645 : this->traverse(&stf);
2817 : :
2818 : 9366 : while (!this->specific_type_functions_.empty())
2819 : : {
2820 : 76 : Specific_type_function* tsf = this->specific_type_functions_.back();
2821 : 76 : this->specific_type_functions_.pop_back();
2822 : 76 : if (tsf->kind == Specific_type_function::SPECIFIC_HASH)
2823 : 1 : tsf->type->write_hash_function(this, tsf->size, &tsf->bname,
2824 : : tsf->fntype);
2825 : : else
2826 : 75 : tsf->type->write_equal_function(this, tsf->name, tsf->size,
2827 : 75 : &tsf->bname, tsf->fntype);
2828 : 152 : delete tsf;
2829 : : }
2830 : 4645 : this->specific_type_functions_are_written_ = true;
2831 : 4645 : }
2832 : :
2833 : : // Traverse the tree.
2834 : :
2835 : : void
2836 : 93427 : Gogo::traverse(Traverse* traverse)
2837 : : {
2838 : : // Traverse the current package first for consistency. The other
2839 : : // packages will only contain imported types, constants, and
2840 : : // declarations.
2841 : 93427 : if (this->package_->bindings()->traverse(traverse, true) == TRAVERSE_EXIT)
2842 : : return;
2843 : 2497739 : for (Packages::const_iterator p = this->packages_.begin();
2844 : 2497739 : p != this->packages_.end();
2845 : 2404312 : ++p)
2846 : : {
2847 : 2404312 : if (p->second != this->package_)
2848 : : {
2849 : 2310885 : if (p->second->bindings()->traverse(traverse, true) == TRAVERSE_EXIT)
2850 : : break;
2851 : : }
2852 : : }
2853 : : }
2854 : :
2855 : : // Add a type to verify. This is used for types of sink variables, in
2856 : : // order to give appropriate error messages.
2857 : :
2858 : : void
2859 : 2352 : Gogo::add_type_to_verify(Type* type)
2860 : : {
2861 : 2352 : this->verify_types_.push_back(type);
2862 : 2352 : }
2863 : :
2864 : : // Traversal class used to verify types.
2865 : :
2866 : 9290 : class Verify_types : public Traverse
2867 : : {
2868 : : public:
2869 : 4645 : Verify_types(Gogo* gogo)
2870 : 4645 : : Traverse(traverse_types),
2871 : 4645 : gogo_(gogo)
2872 : : { }
2873 : :
2874 : : int
2875 : : type(Type*);
2876 : :
2877 : : private:
2878 : : Gogo* gogo_;
2879 : : };
2880 : :
2881 : : // Verify that a type is correct.
2882 : :
2883 : : int
2884 : 9957424 : Verify_types::type(Type* t)
2885 : : {
2886 : 9957424 : if (!t->verify(this->gogo_))
2887 : 46 : return TRAVERSE_SKIP_COMPONENTS;
2888 : : return TRAVERSE_CONTINUE;
2889 : : }
2890 : :
2891 : : // Verify that all types are correct.
2892 : :
2893 : : void
2894 : 4645 : Gogo::verify_types()
2895 : : {
2896 : 4645 : Verify_types traverse(this);
2897 : 4645 : this->traverse(&traverse);
2898 : :
2899 : 4645 : for (std::vector<Type*>::iterator p = this->verify_types_.begin();
2900 : 6997 : p != this->verify_types_.end();
2901 : 2352 : ++p)
2902 : 2352 : (*p)->verify(this);
2903 : 5045 : this->verify_types_.clear();
2904 : 4645 : }
2905 : :
2906 : : // Traversal class used to lower parse tree.
2907 : :
2908 : 4645 : class Lower_parse_tree : public Traverse
2909 : : {
2910 : : public:
2911 : 1844015 : Lower_parse_tree(Gogo* gogo, Named_object* function)
2912 : : : Traverse(traverse_variables
2913 : : | traverse_constants
2914 : : | traverse_functions
2915 : : | traverse_statements
2916 : : | traverse_expressions),
2917 : 1844015 : gogo_(gogo), function_(function), inserter_()
2918 : : { }
2919 : :
2920 : : void
2921 : 509729 : set_inserter(const Statement_inserter* inserter)
2922 : 509729 : { this->inserter_ = *inserter; }
2923 : :
2924 : : int
2925 : : variable(Named_object*);
2926 : :
2927 : : int
2928 : : constant(Named_object*, bool);
2929 : :
2930 : : int
2931 : : function(Named_object*);
2932 : :
2933 : : int
2934 : : statement(Block*, size_t* pindex, Statement*);
2935 : :
2936 : : int
2937 : : expression(Expression**);
2938 : :
2939 : : private:
2940 : : // General IR.
2941 : : Gogo* gogo_;
2942 : : // The function we are traversing.
2943 : : Named_object* function_;
2944 : : // Current statement inserter for use by expressions.
2945 : : Statement_inserter inserter_;
2946 : : };
2947 : :
2948 : : // Lower variables.
2949 : :
2950 : : int
2951 : 1303459 : Lower_parse_tree::variable(Named_object* no)
2952 : : {
2953 : 1303459 : if (!no->is_variable())
2954 : : return TRAVERSE_CONTINUE;
2955 : :
2956 : 1160523 : if (no->is_variable() && no->var_value()->is_global())
2957 : : {
2958 : : // Global variables can have loops in their initialization
2959 : : // expressions. This is handled in lower_init_expression.
2960 : 348764 : no->var_value()->lower_init_expression(this->gogo_, this->function_,
2961 : : &this->inserter_);
2962 : 348764 : return TRAVERSE_CONTINUE;
2963 : : }
2964 : :
2965 : : // This is a local variable. We are going to return
2966 : : // TRAVERSE_SKIP_COMPONENTS here because we want to traverse the
2967 : : // initialization expression when we reach the variable declaration
2968 : : // statement. However, that means that we need to traverse the type
2969 : : // ourselves.
2970 : 811759 : if (no->var_value()->has_type())
2971 : : {
2972 : 811759 : Type* type = no->var_value()->type();
2973 : 811759 : if (type != NULL)
2974 : : {
2975 : 811759 : if (Type::traverse(type, this) == TRAVERSE_EXIT)
2976 : : return TRAVERSE_EXIT;
2977 : : }
2978 : : }
2979 : 811759 : go_assert(!no->var_value()->has_pre_init());
2980 : :
2981 : : return TRAVERSE_SKIP_COMPONENTS;
2982 : : }
2983 : :
2984 : : // Lower constants. We handle constants specially so that we can set
2985 : : // the right value for the predeclared constant iota. This works in
2986 : : // conjunction with the way we lower Const_expression objects.
2987 : :
2988 : : int
2989 : 2189470 : Lower_parse_tree::constant(Named_object* no, bool)
2990 : : {
2991 : 2189470 : Named_constant* nc = no->const_value();
2992 : :
2993 : : // Don't get into trouble if the constant's initializer expression
2994 : : // refers to the constant itself.
2995 : 2189470 : if (nc->lowering())
2996 : : return TRAVERSE_CONTINUE;
2997 : 2189470 : nc->set_lowering();
2998 : :
2999 : 2189470 : nc->traverse_expression(this);
3000 : :
3001 : 2189470 : nc->clear_lowering();
3002 : :
3003 : : // We will traverse the expression a second time, but that will be
3004 : : // fast.
3005 : :
3006 : 2189470 : return TRAVERSE_CONTINUE;
3007 : : }
3008 : :
3009 : : // Lower the body of a function, and set the closure type. Record the
3010 : : // function while lowering it, so that we can pass it down when
3011 : : // lowering an expression.
3012 : :
3013 : : int
3014 : 175648 : Lower_parse_tree::function(Named_object* no)
3015 : : {
3016 : 175648 : go_assert(this->function_ == NULL);
3017 : 175648 : this->function_ = no;
3018 : 175648 : int t = no->func_value()->traverse(this);
3019 : 175648 : this->function_ = NULL;
3020 : :
3021 : 175648 : if (t == TRAVERSE_EXIT)
3022 : 0 : return t;
3023 : : return TRAVERSE_SKIP_COMPONENTS;
3024 : : }
3025 : :
3026 : : // Lower statement parse trees.
3027 : :
3028 : : int
3029 : 6344069 : Lower_parse_tree::statement(Block* block, size_t* pindex, Statement* sorig)
3030 : : {
3031 : : // Because we explicitly traverse the statement's contents
3032 : : // ourselves, we want to skip block statements here. There is
3033 : : // nothing to lower in a block statement.
3034 : 6344069 : if (sorig->is_block_statement())
3035 : : return TRAVERSE_CONTINUE;
3036 : :
3037 : 5282100 : Statement_inserter hold_inserter(this->inserter_);
3038 : 5282100 : this->inserter_ = Statement_inserter(block, pindex);
3039 : :
3040 : : // Lower the expressions first.
3041 : 5282100 : int t = sorig->traverse_contents(this);
3042 : 5282100 : if (t == TRAVERSE_EXIT)
3043 : : {
3044 : 0 : this->inserter_ = hold_inserter;
3045 : 0 : return t;
3046 : : }
3047 : :
3048 : : // Keep lowering until nothing changes.
3049 : : Statement* s = sorig;
3050 : 5998043 : while (true)
3051 : : {
3052 : 5998043 : Statement* snew = s->lower(this->gogo_, this->function_, block,
3053 : : &this->inserter_);
3054 : 5998043 : if (snew == s)
3055 : : break;
3056 : 715943 : s = snew;
3057 : 715943 : t = s->traverse_contents(this);
3058 : 715943 : if (t == TRAVERSE_EXIT)
3059 : : {
3060 : 0 : this->inserter_ = hold_inserter;
3061 : 0 : return t;
3062 : : }
3063 : : }
3064 : :
3065 : 5282100 : if (s != sorig)
3066 : 657124 : block->replace_statement(*pindex, s);
3067 : :
3068 : 5282100 : this->inserter_ = hold_inserter;
3069 : 5282100 : return TRAVERSE_SKIP_COMPONENTS;
3070 : : }
3071 : :
3072 : : // Lower expression parse trees.
3073 : :
3074 : : int
3075 : 29650051 : Lower_parse_tree::expression(Expression** pexpr)
3076 : : {
3077 : : // We have to lower all subexpressions first, so that we can get
3078 : : // their type if necessary. This is awkward, because we don't have
3079 : : // a postorder traversal pass.
3080 : 29650051 : if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
3081 : : return TRAVERSE_EXIT;
3082 : : // Keep lowering until nothing changes.
3083 : 33679945 : while (true)
3084 : : {
3085 : 31664998 : Expression* e = *pexpr;
3086 : 31664998 : Expression* enew = e->lower(this->gogo_, this->function_,
3087 : : &this->inserter_);
3088 : 31664998 : if (enew == e)
3089 : : break;
3090 : 2014947 : if (enew->traverse_subexpressions(this) == TRAVERSE_EXIT)
3091 : : return TRAVERSE_EXIT;
3092 : 2014947 : *pexpr = enew;
3093 : 2014947 : }
3094 : :
3095 : : // Lower the type of this expression before the parent looks at it,
3096 : : // in case the type contains an array that has expressions in its
3097 : : // length. Skip an Unknown_expression, as at this point that means
3098 : : // a composite literal key that does not have a type.
3099 : 29650051 : if ((*pexpr)->unknown_expression() == NULL)
3100 : 29650051 : Type::traverse((*pexpr)->type(), this);
3101 : :
3102 : : return TRAVERSE_SKIP_COMPONENTS;
3103 : : }
3104 : :
3105 : : // Lower the parse tree. This is called after the parse is complete,
3106 : : // when all names should be resolved.
3107 : :
3108 : : void
3109 : 4645 : Gogo::lower_parse_tree()
3110 : : {
3111 : 4645 : Lower_parse_tree lower_parse_tree(this, NULL);
3112 : 4645 : this->traverse(&lower_parse_tree);
3113 : :
3114 : : // If we found any functions defined in other packages that are
3115 : : // inlinables, import their bodies and turn them into functions.
3116 : : //
3117 : : // Note that as we import inlinable functions we may find more
3118 : : // inlinable functions, so don't use an iterator.
3119 : 16892 : for (size_t i = 0; i < this->imported_inlinable_functions_.size(); i++)
3120 : : {
3121 : 12247 : Named_object* no = this->imported_inlinable_functions_[i];
3122 : 12247 : no->func_declaration_value()->import_function_body(this, no);
3123 : : }
3124 : :
3125 : : // There might be type definitions that involve expressions such as the
3126 : : // array length. Make sure to lower these expressions as well. Otherwise,
3127 : : // errors hidden within a type can introduce unexpected errors into later
3128 : : // passes.
3129 : 4645 : for (std::vector<Type*>::iterator p = this->verify_types_.begin();
3130 : 4645 : p != this->verify_types_.end();
3131 : 0 : ++p)
3132 : 0 : Type::traverse(*p, &lower_parse_tree);
3133 : 4645 : }
3134 : :
3135 : : // Lower a block.
3136 : :
3137 : : void
3138 : 121474 : Gogo::lower_block(Named_object* function, Block* block)
3139 : : {
3140 : 121474 : Lower_parse_tree lower_parse_tree(this, function);
3141 : 121474 : block->traverse(&lower_parse_tree);
3142 : 121474 : }
3143 : :
3144 : : // Lower an expression. INSERTER may be NULL, in which case the
3145 : : // expression had better not need to create any temporaries.
3146 : :
3147 : : void
3148 : 526274 : Gogo::lower_expression(Named_object* function, Statement_inserter* inserter,
3149 : : Expression** pexpr)
3150 : : {
3151 : 526274 : Lower_parse_tree lower_parse_tree(this, function);
3152 : 526274 : if (inserter != NULL)
3153 : 509729 : lower_parse_tree.set_inserter(inserter);
3154 : 526274 : lower_parse_tree.expression(pexpr);
3155 : 526274 : }
3156 : :
3157 : : // Lower a constant. This is called when lowering a reference to a
3158 : : // constant. We have to make sure that the constant has already been
3159 : : // lowered.
3160 : :
3161 : : void
3162 : 1191622 : Gogo::lower_constant(Named_object* no)
3163 : : {
3164 : 1191622 : go_assert(no->is_const());
3165 : 1191622 : Lower_parse_tree lower(this, NULL);
3166 : 1191622 : lower.constant(no, false);
3167 : 1191622 : }
3168 : :
3169 : : // Make implicit type conversions explicit. Currently only does for
3170 : : // interface conversions, so the escape analysis can see them and
3171 : : // optimize.
3172 : :
3173 : 36572 : class Add_conversions : public Traverse
3174 : : {
3175 : : public:
3176 : 18286 : Add_conversions()
3177 : : : Traverse(traverse_statements
3178 : 18286 : | traverse_expressions)
3179 : : { }
3180 : :
3181 : : int
3182 : : statement(Block*, size_t* pindex, Statement*);
3183 : :
3184 : : int
3185 : : expression(Expression**);
3186 : : };
3187 : :
3188 : : // Add explicit conversions in a statement.
3189 : :
3190 : : int
3191 : 3845075 : Add_conversions::statement(Block*, size_t*, Statement* sorig)
3192 : : {
3193 : 3845075 : sorig->add_conversions();
3194 : 3845075 : return TRAVERSE_CONTINUE;
3195 : : }
3196 : :
3197 : : // Add explicit conversions in an expression.
3198 : :
3199 : : int
3200 : 11369227 : Add_conversions::expression(Expression** pexpr)
3201 : : {
3202 : 11369227 : (*pexpr)->add_conversions();
3203 : 11369227 : return TRAVERSE_CONTINUE;
3204 : : }
3205 : :
3206 : : void
3207 : 4645 : Gogo::add_conversions()
3208 : : {
3209 : 4645 : Add_conversions add_conversions;
3210 : 4645 : this->traverse(&add_conversions);
3211 : 4645 : }
3212 : :
3213 : : void
3214 : 13641 : Gogo::add_conversions_in_block(Block *b)
3215 : : {
3216 : 13641 : Add_conversions add_conversions;
3217 : 13641 : b->traverse(&add_conversions);
3218 : 13641 : }
3219 : :
3220 : : // Traversal class for simple deadcode elimination.
3221 : :
3222 : 9290 : class Remove_deadcode : public Traverse
3223 : : {
3224 : : public:
3225 : 4645 : Remove_deadcode(Gogo* gogo)
3226 : 4645 : : Traverse(traverse_statements
3227 : : | traverse_expressions),
3228 : 4645 : gogo_(gogo)
3229 : : { }
3230 : :
3231 : : int
3232 : : statement(Block*, size_t* pindex, Statement*);
3233 : :
3234 : : int
3235 : : expression(Expression**);
3236 : :
3237 : : private:
3238 : : Gogo* gogo_;
3239 : : };
3240 : :
3241 : : // Remove deadcode in a statement.
3242 : :
3243 : : int
3244 : 3792796 : Remove_deadcode::statement(Block* block, size_t* pindex, Statement* sorig)
3245 : : {
3246 : 3792796 : Location loc = sorig->location();
3247 : 3792796 : If_statement* ifs = sorig->if_statement();
3248 : 336470 : if (ifs != NULL)
3249 : : {
3250 : : // Remove the dead branch of an if statement.
3251 : 336470 : bool bval;
3252 : 336470 : if (ifs->condition()->boolean_constant_value(&bval))
3253 : : {
3254 : 4381 : Statement* s;
3255 : 4381 : if (bval)
3256 : 861 : s = Statement::make_block_statement(ifs->then_block(),
3257 : : loc);
3258 : : else
3259 : 3520 : if (ifs->else_block() != NULL)
3260 : 273 : s = Statement::make_block_statement(ifs->else_block(),
3261 : : loc);
3262 : : else
3263 : : // Make a dummy statement.
3264 : 3247 : s = Statement::make_statement(Expression::make_boolean(false, loc),
3265 : : true);
3266 : :
3267 : 4381 : block->replace_statement(*pindex, s);
3268 : : }
3269 : : }
3270 : 3792796 : return TRAVERSE_CONTINUE;
3271 : : }
3272 : :
3273 : : // Remove deadcode in an expression.
3274 : :
3275 : : int
3276 : 11067515 : Remove_deadcode::expression(Expression** pexpr)
3277 : : {
3278 : : // Discard the right arm of a shortcut expression of constant value.
3279 : 11067515 : Binary_expression* be = (*pexpr)->binary_expression();
3280 : 651226 : bool bval;
3281 : 651226 : if (be != NULL
3282 : 651226 : && be->boolean_constant_value(&bval)
3283 : 1310 : && (be->op() == OPERATOR_ANDAND
3284 : 385 : || be->op() == OPERATOR_OROR))
3285 : : {
3286 : 1308 : *pexpr = Expression::make_boolean(bval, be->location());
3287 : 1308 : Type_context context(NULL, false);
3288 : 1308 : (*pexpr)->determine_type(this->gogo_, &context);
3289 : : }
3290 : 11067515 : return TRAVERSE_CONTINUE;
3291 : : }
3292 : :
3293 : : // Remove deadcode.
3294 : :
3295 : : void
3296 : 4645 : Gogo::remove_deadcode()
3297 : : {
3298 : 4645 : Remove_deadcode remove_deadcode(this);
3299 : 4645 : this->traverse(&remove_deadcode);
3300 : 4645 : }
3301 : :
3302 : : // Traverse the tree to create function descriptors as needed.
3303 : :
3304 : 9290 : class Create_function_descriptors : public Traverse
3305 : : {
3306 : : public:
3307 : 4645 : Create_function_descriptors(Gogo* gogo)
3308 : 4645 : : Traverse(traverse_functions | traverse_expressions),
3309 : 4645 : gogo_(gogo)
3310 : : { }
3311 : :
3312 : : int
3313 : : function(Named_object*);
3314 : :
3315 : : int
3316 : : expression(Expression**);
3317 : :
3318 : : static bool
3319 : : skip_descriptor(Gogo* gogo, const Named_object*);
3320 : :
3321 : : private:
3322 : : Gogo* gogo_;
3323 : : };
3324 : :
3325 : : // Create a descriptor for every top-level exported function and every
3326 : : // function referenced by an inline function.
3327 : :
3328 : : int
3329 : 188603 : Create_function_descriptors::function(Named_object* no)
3330 : : {
3331 : 188603 : if (Create_function_descriptors::skip_descriptor(this->gogo_, no))
3332 : : return TRAVERSE_CONTINUE;
3333 : :
3334 : 188603 : if (no->is_function()
3335 : 188603 : && no->func_value()->enclosing() == NULL
3336 : 164295 : && !no->func_value()->is_method()
3337 : 274853 : && ((!Gogo::is_hidden_name(no->name())
3338 : 44009 : && !Gogo::is_thunk(no))
3339 : 43547 : || no->func_value()->is_referenced_by_inline()))
3340 : 42703 : no->func_value()->descriptor(this->gogo_, no);
3341 : :
3342 : : return TRAVERSE_CONTINUE;
3343 : : }
3344 : :
3345 : : // If we see a function referenced in any way other than calling it,
3346 : : // create a descriptor for it.
3347 : :
3348 : : int
3349 : 10270070 : Create_function_descriptors::expression(Expression** pexpr)
3350 : : {
3351 : 10270070 : Expression* expr = *pexpr;
3352 : :
3353 : 10270070 : Func_expression* fe = expr->func_expression();
3354 : 10270070 : if (fe != NULL)
3355 : : {
3356 : : // We would not get here for a call to this function, so this is
3357 : : // a reference to a function other than calling it. We need a
3358 : : // descriptor.
3359 : 46977 : if (fe->closure() != NULL)
3360 : : return TRAVERSE_CONTINUE;
3361 : 37237 : Named_object* no = fe->named_object();
3362 : 37237 : if (no->is_function() && !no->func_value()->is_method())
3363 : 21527 : no->func_value()->descriptor(this->gogo_, no);
3364 : 15710 : else if (no->is_function_declaration()
3365 : 15710 : && !no->func_declaration_value()->type()->is_method()
3366 : 31420 : && !Linemap::is_predeclared_location(no->location()))
3367 : 14737 : no->func_declaration_value()->descriptor(this->gogo_, no);
3368 : 37237 : return TRAVERSE_CONTINUE;
3369 : : }
3370 : :
3371 : 10223093 : Bound_method_expression* bme = expr->bound_method_expression();
3372 : 10223093 : if (bme != NULL)
3373 : : {
3374 : : // We would not get here for a call to this method, so this is a
3375 : : // method value. We need to create a thunk.
3376 : 934 : Bound_method_expression::create_thunk(this->gogo_, bme->method(),
3377 : : bme->function());
3378 : 934 : return TRAVERSE_CONTINUE;
3379 : : }
3380 : :
3381 : 10222159 : Interface_field_reference_expression* ifre =
3382 : 10222159 : expr->interface_field_reference_expression();
3383 : 10222159 : if (ifre != NULL)
3384 : : {
3385 : : // We would not get here for a call to this interface method, so
3386 : : // this is a method value. We need to create a thunk.
3387 : 105 : Interface_type* type = ifre->expr()->type()->interface_type();
3388 : 105 : if (type != NULL)
3389 : 105 : Interface_field_reference_expression::create_thunk(this->gogo_, type,
3390 : : ifre->name());
3391 : 105 : return TRAVERSE_CONTINUE;
3392 : : }
3393 : :
3394 : 10222054 : Call_expression* ce = expr->call_expression();
3395 : 816921 : if (ce != NULL)
3396 : : {
3397 : 816921 : Expression* fn = ce->fn();
3398 : 816921 : if (fn->func_expression() != NULL
3399 : : || fn->bound_method_expression() != NULL
3400 : : || fn->interface_field_reference_expression() != NULL)
3401 : : {
3402 : : // Traverse the arguments but not the function.
3403 : 797443 : Expression_list* args = ce->args();
3404 : 797443 : if (args != NULL)
3405 : : {
3406 : 740673 : if (args->traverse(this) == TRAVERSE_EXIT)
3407 : : return TRAVERSE_EXIT;
3408 : : }
3409 : :
3410 : : // Traverse the subexpressions of the function, if any.
3411 : 797443 : if (fn->traverse_subexpressions(this) == TRAVERSE_EXIT)
3412 : : return TRAVERSE_EXIT;
3413 : :
3414 : : return TRAVERSE_SKIP_COMPONENTS;
3415 : : }
3416 : : }
3417 : :
3418 : : return TRAVERSE_CONTINUE;
3419 : : }
3420 : :
3421 : : // The gc compiler has some special cases that it always compiles as
3422 : : // intrinsics. For those we don't want to generate a function
3423 : : // descriptor, as there will be no code for it to refer to.
3424 : :
3425 : : bool
3426 : 189200 : Create_function_descriptors::skip_descriptor(Gogo* gogo,
3427 : : const Named_object* no)
3428 : : {
3429 : 189200 : const std::string& pkgpath(no->package() == NULL
3430 : 189200 : ? gogo->pkgpath()
3431 : 12247 : : no->package()->pkgpath());
3432 : :
3433 : : // internal/abi is the standard library package,
3434 : : // bootstrap/internal/abi is the name used when bootstrapping the gc
3435 : : // compiler.
3436 : :
3437 : 189200 : return ((pkgpath == "internal/abi"
3438 : 189192 : || pkgpath == "bootstrap/internal/abi")
3439 : 189200 : && (no->name() == "FuncPCABI0"
3440 : 4 : || no->name() == "FuncPCABIInternal"));
3441 : : }
3442 : :
3443 : : // Create function descriptors as needed. We need a function
3444 : : // descriptor for all exported functions and for all functions that
3445 : : // are referenced without being called.
3446 : :
3447 : : void
3448 : 4645 : Gogo::create_function_descriptors()
3449 : : {
3450 : : // Create a function descriptor for any exported function that is
3451 : : // declared in this package. This is so that we have a descriptor
3452 : : // for functions written in assembly. Gather the descriptors first
3453 : : // so that we don't add declarations while looping over them.
3454 : 4645 : std::vector<Named_object*> fndecls;
3455 : 4645 : Bindings* b = this->package_->bindings();
3456 : 339504 : for (Bindings::const_declarations_iterator p = b->begin_declarations();
3457 : 339504 : p != b->end_declarations();
3458 : 334859 : ++p)
3459 : : {
3460 : 334859 : Named_object* no = p->second;
3461 : 334859 : if (no->is_function_declaration()
3462 : 3904 : && !no->func_declaration_value()->type()->is_method()
3463 : 3904 : && !Linemap::is_predeclared_location(no->location())
3464 : 3904 : && !Gogo::is_hidden_name(no->name())
3465 : 335456 : && !Create_function_descriptors::skip_descriptor(this, no))
3466 : 589 : fndecls.push_back(no);
3467 : : }
3468 : 5234 : for (std::vector<Named_object*>::const_iterator p = fndecls.begin();
3469 : 5234 : p != fndecls.end();
3470 : 589 : ++p)
3471 : 589 : (*p)->func_declaration_value()->descriptor(this, *p);
3472 : 4645 : fndecls.clear();
3473 : :
3474 : 4645 : Create_function_descriptors cfd(this);
3475 : 4645 : this->traverse(&cfd);
3476 : 4645 : }
3477 : :
3478 : : // Lower calls to builtin functions. We need to do this early because
3479 : : // some builtin calls are constant expressions. In particular we need
3480 : : // to do this before finalize_methods, because finalize_methods calls
3481 : : // is_direct_iface_type, which needs to know whether something like
3482 : : // [unsafe.Sizeof(byte(0))]*byte is a direct-iface type.
3483 : :
3484 : 9290 : class Lower_builtin_calls : public Traverse
3485 : : {
3486 : : public:
3487 : 4645 : Lower_builtin_calls(Gogo* gogo)
3488 : 4645 : : Traverse(traverse_expressions),
3489 : 4645 : gogo_(gogo)
3490 : : { }
3491 : :
3492 : : int
3493 : : expression(Expression**);
3494 : :
3495 : : private:
3496 : : Gogo* gogo_;
3497 : : };
3498 : :
3499 : : int
3500 : 8993148 : Lower_builtin_calls::expression(Expression** pexpr)
3501 : : {
3502 : 8993148 : Call_expression* ce = (*pexpr)->call_expression();
3503 : 836475 : if (ce != NULL)
3504 : 836475 : *pexpr = ce->lower_builtin(this->gogo_);
3505 : 8993148 : return TRAVERSE_CONTINUE;
3506 : : }
3507 : :
3508 : : void
3509 : 4645 : Gogo::lower_builtin_calls()
3510 : : {
3511 : 4645 : Lower_builtin_calls lbc(this);
3512 : 4645 : this->traverse(&lbc);
3513 : 4645 : }
3514 : :
3515 : : // Finalize the methods of an interface type.
3516 : :
3517 : : int
3518 : 25418038 : Finalize_methods::type(Type* t)
3519 : : {
3520 : : // Check the classification so that we don't finalize the methods
3521 : : // twice for a named interface type.
3522 : 25418038 : switch (t->classification())
3523 : : {
3524 : 272376 : case Type::TYPE_INTERFACE:
3525 : 272376 : t->interface_type()->finalize_methods();
3526 : 272376 : break;
3527 : :
3528 : 2286548 : case Type::TYPE_NAMED:
3529 : 2286548 : {
3530 : 2286548 : Named_type* nt = t->named_type();
3531 : :
3532 : 2286548 : if (nt->is_alias())
3533 : : return TRAVERSE_CONTINUE;
3534 : :
3535 : 2192659 : Type* rt = nt->real_type();
3536 : 2192659 : if (rt->classification() != Type::TYPE_STRUCT)
3537 : : {
3538 : : // Finalize the methods of the real type first.
3539 : 930764 : if (Type::traverse(rt, this) == TRAVERSE_EXIT)
3540 : : return TRAVERSE_EXIT;
3541 : :
3542 : : // Finalize the methods of this type.
3543 : 930764 : nt->finalize_methods(this->gogo_);
3544 : : }
3545 : : else
3546 : : {
3547 : : // We don't want to finalize the methods of a named struct
3548 : : // type, as the methods should be attached to the named
3549 : : // type, not the struct type. We just want to finalize
3550 : : // the field types.
3551 : : //
3552 : : // It is possible that a field type refers indirectly to
3553 : : // this type, such as via a field with function type with
3554 : : // an argument or result whose type is this type. To
3555 : : // avoid the cycle, first finalize the methods of any
3556 : : // embedded types, which are the only types we need to
3557 : : // know to finalize the methods of this type.
3558 : 2523790 : const Struct_field_list* fields = rt->struct_type()->fields();
3559 : 1261895 : if (fields != NULL)
3560 : : {
3561 : 8240142 : for (Struct_field_list::const_iterator pf = fields->begin();
3562 : 8240142 : pf != fields->end();
3563 : 6978247 : ++pf)
3564 : : {
3565 : 6978247 : if (pf->is_anonymous())
3566 : : {
3567 : 124573 : if (Type::traverse(pf->type(), this) == TRAVERSE_EXIT)
3568 : 0 : return TRAVERSE_EXIT;
3569 : : }
3570 : : }
3571 : : }
3572 : :
3573 : : // Finalize the methods of this type.
3574 : 1261895 : nt->finalize_methods(this->gogo_);
3575 : :
3576 : : // Finalize all the struct fields.
3577 : 2523790 : if (rt->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT)
3578 : : return TRAVERSE_EXIT;
3579 : : }
3580 : :
3581 : : // If this type is defined in a different package, then finalize the
3582 : : // types of all the methods, since we won't see them otherwise.
3583 : 2192659 : if (nt->named_object()->package() != NULL && nt->has_any_methods())
3584 : : {
3585 : 1041198 : const Methods* methods = nt->methods();
3586 : 8545555 : for (Methods::const_iterator p = methods->begin();
3587 : 8545555 : p != methods->end();
3588 : 7504357 : ++p)
3589 : : {
3590 : 7504357 : if (Type::traverse(p->second->type(), this) == TRAVERSE_EXIT)
3591 : 0 : return TRAVERSE_EXIT;
3592 : : }
3593 : : }
3594 : :
3595 : : return TRAVERSE_SKIP_COMPONENTS;
3596 : : }
3597 : :
3598 : 119380 : case Type::TYPE_STRUCT:
3599 : : // Traverse the field types first in case there is an embedded
3600 : : // field with methods that the struct should inherit.
3601 : 238760 : if (t->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT)
3602 : : return TRAVERSE_EXIT;
3603 : 119380 : t->struct_type()->finalize_methods(this->gogo_);
3604 : 119380 : return TRAVERSE_SKIP_COMPONENTS;
3605 : :
3606 : : default:
3607 : : break;
3608 : : }
3609 : :
3610 : : return TRAVERSE_CONTINUE;
3611 : : }
3612 : :
3613 : : // Finalize method lists and build stub methods for types.
3614 : :
3615 : : void
3616 : 4645 : Gogo::finalize_methods()
3617 : : {
3618 : 4645 : Finalize_methods finalize(this);
3619 : 4645 : this->traverse(&finalize);
3620 : 4645 : }
3621 : :
3622 : : // Finalize the method list for a type. This is called when a type is
3623 : : // parsed for an inlined function body, which happens after the
3624 : : // finalize_methods pass.
3625 : :
3626 : : void
3627 : 563 : Gogo::finalize_methods_for_type(Type* type)
3628 : : {
3629 : 563 : Finalize_methods finalize(this);
3630 : 563 : Type::traverse(type, &finalize);
3631 : 563 : }
3632 : :
3633 : : // Set types for unspecified variables and constants.
3634 : :
3635 : : void
3636 : 4645 : Gogo::determine_types()
3637 : : {
3638 : 4645 : this->current_bindings()->determine_types(this);
3639 : :
3640 : : // Determine the types of constants in packages.
3641 : 120889 : for (Packages::const_iterator p = this->packages_.begin();
3642 : 120889 : p != this->packages_.end();
3643 : 116244 : ++p)
3644 : 116244 : p->second->determine_types(this);
3645 : 4645 : }
3646 : :
3647 : : // Traversal class used for type checking.
3648 : :
3649 : 4645 : class Check_types_traverse : public Traverse
3650 : : {
3651 : : public:
3652 : 18286 : Check_types_traverse(Gogo* gogo)
3653 : 18286 : : Traverse(traverse_variables
3654 : : | traverse_constants
3655 : : | traverse_functions
3656 : : | traverse_statements
3657 : : | traverse_expressions),
3658 : 18286 : gogo_(gogo)
3659 : : { }
3660 : :
3661 : : int
3662 : : variable(Named_object*);
3663 : :
3664 : : int
3665 : : constant(Named_object*, bool);
3666 : :
3667 : : int
3668 : : function(Named_object*);
3669 : :
3670 : : int
3671 : : statement(Block*, size_t* pindex, Statement*);
3672 : :
3673 : : int
3674 : : expression(Expression**);
3675 : :
3676 : : private:
3677 : : // General IR.
3678 : : Gogo* gogo_;
3679 : : };
3680 : :
3681 : : // Check that a variable initializer has the right type.
3682 : :
3683 : : int
3684 : 1116170 : Check_types_traverse::variable(Named_object* named_object)
3685 : : {
3686 : 1116170 : if (named_object->is_variable())
3687 : : {
3688 : 984741 : Variable* var = named_object->var_value();
3689 : :
3690 : : // Give error if variable type is not defined.
3691 : 984741 : var->type()->base();
3692 : :
3693 : 984741 : Expression* init = var->init();
3694 : 984741 : std::string reason;
3695 : 984741 : if (init != NULL
3696 : 1283553 : && !Type::are_assignable(var->type(), init->type(), &reason))
3697 : : {
3698 : 45 : if (reason.empty())
3699 : 0 : go_error_at(var->location(), "incompatible type in initialization");
3700 : : else
3701 : 45 : go_error_at(var->location(),
3702 : : "incompatible type in initialization (%s)",
3703 : : reason.c_str());
3704 : 45 : init = Expression::make_error(named_object->location());
3705 : 45 : var->clear_init();
3706 : : }
3707 : 984696 : else if (init != NULL
3708 : 984696 : && init->func_expression() != NULL)
3709 : : {
3710 : 3219 : Named_object* no = init->func_expression()->named_object();
3711 : 3219 : Function_type* fntype;
3712 : 3219 : if (no->is_function())
3713 : 3047 : fntype = no->func_value()->type();
3714 : 172 : else if (no->is_function_declaration())
3715 : 172 : fntype = no->func_declaration_value()->type();
3716 : : else
3717 : 0 : go_unreachable();
3718 : :
3719 : : // Builtin functions cannot be used as function values for variable
3720 : : // initialization.
3721 : 3219 : if (fntype->is_builtin())
3722 : : {
3723 : 1 : go_error_at(init->location(),
3724 : : "invalid use of special built-in function %qs; "
3725 : : "must be called",
3726 : 2 : no->message_name().c_str());
3727 : : }
3728 : : }
3729 : :
3730 : 984741 : if (!var->is_used()
3731 : 368942 : && !var->is_global()
3732 : 51096 : && !var->is_parameter()
3733 : 35 : && !var->is_receiver()
3734 : 35 : && !var->type()->is_error()
3735 : 13 : && (init == NULL || !init->is_error_expression())
3736 : 984749 : && !Lex::is_invalid_identifier(named_object->name()))
3737 : : {
3738 : : // Avoid giving an error if the initializer is invalid.
3739 : 7 : if (init != NULL)
3740 : 6 : init->check_types(this->gogo_);
3741 : :
3742 : 6 : if (init == NULL || !init->is_error_expression())
3743 : 6 : go_error_at(var->location(), "%qs declared but not used",
3744 : 12 : named_object->message_name().c_str());
3745 : : }
3746 : 984741 : }
3747 : 1116170 : return TRAVERSE_CONTINUE;
3748 : : }
3749 : :
3750 : : // Check that a constant initializer has the right type.
3751 : :
3752 : : int
3753 : 997344 : Check_types_traverse::constant(Named_object* named_object, bool)
3754 : : {
3755 : 997344 : Named_constant* constant = named_object->const_value();
3756 : 997344 : Type* ctype = constant->type();
3757 : 1030967 : if (ctype->integer_type() == NULL
3758 : 1024540 : && ctype->float_type() == NULL
3759 : 1024496 : && ctype->complex_type() == NULL
3760 : 27177 : && !ctype->is_boolean_type()
3761 : 25650 : && !ctype->is_string_type())
3762 : : {
3763 : 25 : if (ctype->is_nil_type())
3764 : 1 : go_error_at(constant->location(), "const initializer cannot be nil");
3765 : 24 : else if (!ctype->is_error())
3766 : 15 : go_error_at(constant->location(), "invalid constant type");
3767 : 25 : constant->set_error();
3768 : : }
3769 : 997319 : else if (constant->expr()->is_error_expression())
3770 : : {
3771 : 3 : go_assert(saw_errors());
3772 : 3 : constant->set_error();
3773 : : }
3774 : 997316 : else if (!constant->expr()->is_constant())
3775 : : {
3776 : 12 : go_error_at(constant->expr()->location(), "expression is not constant");
3777 : 12 : constant->set_error();
3778 : : }
3779 : 997304 : else if (!Type::are_assignable(constant->type(), constant->expr()->type(),
3780 : : NULL))
3781 : : {
3782 : 1 : go_error_at(constant->location(),
3783 : : "initialization expression has wrong type");
3784 : 1 : constant->set_error();
3785 : : }
3786 : 997344 : return TRAVERSE_CONTINUE;
3787 : : }
3788 : :
3789 : : // There are no types to check in a function, but this is where we
3790 : : // issue warnings about labels which are defined but not referenced.
3791 : :
3792 : : int
3793 : 175575 : Check_types_traverse::function(Named_object* no)
3794 : : {
3795 : 175575 : no->func_value()->check_labels();
3796 : 175575 : return TRAVERSE_CONTINUE;
3797 : : }
3798 : :
3799 : : // Check that types are valid in a statement.
3800 : :
3801 : : int
3802 : 1852286 : Check_types_traverse::statement(Block*, size_t*, Statement* s)
3803 : : {
3804 : 1852286 : s->check_types(this->gogo_);
3805 : 1852286 : return TRAVERSE_CONTINUE;
3806 : : }
3807 : :
3808 : : // Check that types are valid in an expression.
3809 : :
3810 : : int
3811 : 9624503 : Check_types_traverse::expression(Expression** expr)
3812 : : {
3813 : 9624503 : (*expr)->check_types(this->gogo_);
3814 : 9624503 : return TRAVERSE_CONTINUE;
3815 : : }
3816 : :
3817 : : // Check that types are valid.
3818 : :
3819 : : void
3820 : 4645 : Gogo::check_types()
3821 : : {
3822 : 4645 : Check_types_traverse traverse(this);
3823 : 4645 : this->traverse(&traverse);
3824 : :
3825 : 4645 : Bindings* bindings = this->current_bindings();
3826 : 339431 : for (Bindings::const_declarations_iterator p = bindings->begin_declarations();
3827 : 339431 : p != bindings->end_declarations();
3828 : 334786 : ++p)
3829 : : {
3830 : : // Also check the types in a function declaration's signature.
3831 : 334786 : Named_object* no = p->second;
3832 : 334786 : if (no->is_function_declaration())
3833 : 3904 : no->func_declaration_value()->check_types();
3834 : : }
3835 : 4645 : }
3836 : :
3837 : : // Check the types in a single block.
3838 : :
3839 : : void
3840 : 13641 : Gogo::check_types_in_block(Block* block)
3841 : : {
3842 : 13641 : Check_types_traverse traverse(this);
3843 : 13641 : block->traverse(&traverse);
3844 : 13641 : }
3845 : :
3846 : : // For each global variable defined in the current package, record the
3847 : : // set of variables that its initializer depends on. We do this after
3848 : : // lowering so that all unknown names are resolved to their final
3849 : : // locations. We do this before write barrier insertion because that
3850 : : // makes it harder to distinguish references from assignments in
3851 : : // preinit blocks.
3852 : :
3853 : : void
3854 : 4645 : Gogo::record_global_init_refs()
3855 : : {
3856 : 4645 : Bindings* bindings = this->package_->bindings();
3857 : 456088 : for (Bindings::const_definitions_iterator pb = bindings->begin_definitions();
3858 : 456088 : pb != bindings->end_definitions();
3859 : 451443 : pb++)
3860 : : {
3861 : 451443 : Named_object* no = *pb;
3862 : 451443 : if (!no->is_variable())
3863 : 414742 : continue;
3864 : :
3865 : 36701 : Variable* var = no->var_value();
3866 : 36701 : go_assert(var->is_global());
3867 : :
3868 : 36701 : Find_vars find_vars;
3869 : 36701 : Expression* init = var->init();
3870 : 36701 : if (init != NULL)
3871 : 20356 : Expression::traverse(&init, &find_vars);
3872 : 36701 : if (var->has_pre_init())
3873 : 121 : var->preinit()->traverse(&find_vars);
3874 : 36701 : Named_object* dep = this->var_depends_on(var);
3875 : 36701 : if (dep != NULL)
3876 : : {
3877 : 99 : Expression* dinit = dep->var_value()->init();
3878 : 99 : if (dinit != NULL)
3879 : 95 : Expression::traverse(&dinit, &find_vars);
3880 : 99 : if (dep->var_value()->has_pre_init())
3881 : 99 : dep->var_value()->preinit()->traverse(&find_vars);
3882 : : }
3883 : :
3884 : 48351 : for (Find_vars::const_iterator pv = find_vars.begin();
3885 : 48351 : pv != find_vars.end();
3886 : 11650 : ++pv)
3887 : 11650 : var->add_init_ref(*pv);
3888 : 36701 : }
3889 : 4645 : }
3890 : :
3891 : : // A traversal class which finds all the expressions which must be
3892 : : // evaluated in order within a statement or larger expression. This
3893 : : // is used to implement the rules about order of evaluation.
3894 : :
3895 : : class Find_eval_ordering : public Traverse
3896 : : {
3897 : : private:
3898 : : typedef std::vector<Expression**> Expression_pointers;
3899 : :
3900 : : public:
3901 : 4733853 : Find_eval_ordering()
3902 : : : Traverse(traverse_blocks
3903 : : | traverse_statements
3904 : : | traverse_expressions),
3905 : 4733853 : exprs_()
3906 : : { }
3907 : :
3908 : : size_t
3909 : 4631969 : size() const
3910 : 4631969 : { return this->exprs_.size(); }
3911 : :
3912 : : typedef Expression_pointers::const_iterator const_iterator;
3913 : :
3914 : : const_iterator
3915 : 426928 : begin() const
3916 : 426928 : { return this->exprs_.begin(); }
3917 : :
3918 : : const_iterator
3919 : 1102350 : end() const
3920 : 711 : { return this->exprs_.end(); }
3921 : :
3922 : : protected:
3923 : : int
3924 : 1683193 : block(Block*)
3925 : 1683193 : { return TRAVERSE_SKIP_COMPONENTS; }
3926 : :
3927 : : int
3928 : 0 : statement(Block*, size_t*, Statement*)
3929 : 0 : { return TRAVERSE_SKIP_COMPONENTS; }
3930 : :
3931 : : int
3932 : : expression(Expression**);
3933 : :
3934 : : private:
3935 : : // A list of pointers to expressions with side-effects.
3936 : : Expression_pointers exprs_;
3937 : : };
3938 : :
3939 : : // If an expression must be evaluated in order, put it on the list.
3940 : :
3941 : : int
3942 : 12439142 : Find_eval_ordering::expression(Expression** expression_pointer)
3943 : : {
3944 : 12439142 : Binary_expression* binexp = (*expression_pointer)->binary_expression();
3945 : 740032 : if (binexp != NULL
3946 : 740032 : && (binexp->op() == OPERATOR_ANDAND || binexp->op() == OPERATOR_OROR))
3947 : : {
3948 : : // Shortcut expressions may potentially have side effects which need
3949 : : // to be ordered, so add them to the list.
3950 : : // We don't order its subexpressions here since they may be evaluated
3951 : : // conditionally. This is handled in remove_shortcuts.
3952 : 86467 : this->exprs_.push_back(expression_pointer);
3953 : 86467 : return TRAVERSE_SKIP_COMPONENTS;
3954 : : }
3955 : :
3956 : : // We have to look at subexpressions before this one.
3957 : 12352675 : if ((*expression_pointer)->traverse_subexpressions(this) == TRAVERSE_EXIT)
3958 : : return TRAVERSE_EXIT;
3959 : 12352675 : if ((*expression_pointer)->must_eval_in_order())
3960 : 898902 : this->exprs_.push_back(expression_pointer);
3961 : : return TRAVERSE_SKIP_COMPONENTS;
3962 : : }
3963 : :
3964 : : // A traversal class for ordering evaluations.
3965 : :
3966 : 367488 : class Order_eval : public Traverse
3967 : : {
3968 : : public:
3969 : 183744 : Order_eval(Gogo* gogo)
3970 : 183744 : : Traverse(traverse_variables
3971 : : | traverse_statements),
3972 : 183744 : gogo_(gogo)
3973 : : { }
3974 : :
3975 : : int
3976 : : variable(Named_object*);
3977 : :
3978 : : int
3979 : : statement(Block*, size_t*, Statement*);
3980 : :
3981 : : private:
3982 : : // The IR.
3983 : : Gogo* gogo_;
3984 : : };
3985 : :
3986 : : // Implement the order of evaluation rules for a statement.
3987 : :
3988 : : int
3989 : 4713497 : Order_eval::statement(Block* block, size_t* pindex, Statement* stmt)
3990 : : {
3991 : : // FIXME: This approach doesn't work for switch statements, because
3992 : : // we add the new statements before the whole switch when we need to
3993 : : // instead add them just before the switch expression. The right
3994 : : // fix is probably to lower switch statements with nonconstant cases
3995 : : // to a series of conditionals.
3996 : 4713497 : if (stmt->switch_statement() != NULL)
3997 : : return TRAVERSE_CONTINUE;
3998 : :
3999 : 4713497 : Find_eval_ordering find_eval_ordering;
4000 : :
4001 : : // If S is a variable declaration, then ordinary traversal won't do
4002 : : // anything. We want to explicitly traverse the initialization
4003 : : // expression if there is one.
4004 : 4713497 : Variable_declaration_statement* vds = stmt->variable_declaration_statement();
4005 : 4713497 : Expression* init = NULL;
4006 : 4713497 : Expression* orig_init = NULL;
4007 : 4713497 : if (vds == NULL)
4008 : 4330867 : stmt->traverse_contents(&find_eval_ordering);
4009 : : else
4010 : : {
4011 : 382630 : init = vds->var()->var_value()->init();
4012 : 382630 : if (init == NULL)
4013 : : return TRAVERSE_CONTINUE;
4014 : 280746 : orig_init = init;
4015 : :
4016 : : // It might seem that this could be
4017 : : // init->traverse_subexpressions. Unfortunately that can fail
4018 : : // in a case like
4019 : : // var err os.Error
4020 : : // newvar, err := call(arg())
4021 : : // Here newvar will have an init of call result 0 of
4022 : : // call(arg()). If we only traverse subexpressions, we will
4023 : : // only find arg(), and we won't bother to move anything out.
4024 : : // Then we get to the assignment to err, we will traverse the
4025 : : // whole statement, and this time we will find both call() and
4026 : : // arg(), and so we will move them out. This will cause them to
4027 : : // be put into temporary variables before the assignment to err
4028 : : // but after the declaration of newvar. To avoid that problem,
4029 : : // we traverse the entire expression here.
4030 : 280746 : Expression::traverse(&init, &find_eval_ordering);
4031 : : }
4032 : :
4033 : 4611613 : size_t c = find_eval_ordering.size();
4034 : 4611613 : if (c == 0)
4035 : : return TRAVERSE_CONTINUE;
4036 : :
4037 : : // If there is only one expression with a side-effect, we can
4038 : : // usually leave it in place.
4039 : 732287 : if (c == 1)
4040 : : {
4041 : 516499 : switch (stmt->classification())
4042 : : {
4043 : : case Statement::STATEMENT_ASSIGNMENT:
4044 : : // For an assignment statement, we need to evaluate an
4045 : : // expression on the right hand side before we evaluate any
4046 : : // index expression on the left hand side, so for that case
4047 : : // we always move the expression. Otherwise we mishandle
4048 : : // m[0] = len(m) where m is a map.
4049 : : break;
4050 : :
4051 : 49625 : case Statement::STATEMENT_EXPRESSION:
4052 : 49625 : {
4053 : : // If this is a call statement that doesn't return any
4054 : : // values, it will not have been counted as a value to
4055 : : // move. We need to move any subexpressions in case they
4056 : : // are themselves call statements that require passing a
4057 : : // closure.
4058 : 49625 : Expression* expr = stmt->expression_statement()->expr();
4059 : 49625 : if (expr->call_expression() != NULL
4060 : 48371 : && expr->call_expression()->result_count() == 0)
4061 : : break;
4062 : 37239 : return TRAVERSE_CONTINUE;
4063 : : }
4064 : :
4065 : : default:
4066 : : // We can leave the expression in place.
4067 : : return TRAVERSE_CONTINUE;
4068 : : }
4069 : : }
4070 : :
4071 : 425867 : bool is_thunk = stmt->thunk_statement() != NULL;
4072 : 425867 : Expression_statement* es = stmt->expression_statement();
4073 : 1091461 : for (Find_eval_ordering::const_iterator p = find_eval_ordering.begin();
4074 : 1091461 : p != find_eval_ordering.end();
4075 : 665594 : ++p)
4076 : : {
4077 : 665928 : Expression** pexpr = *p;
4078 : :
4079 : : // The last expression in a thunk will be the call passed to go
4080 : : // or defer, which we must not evaluate early.
4081 : 665928 : if (is_thunk && p + 1 == find_eval_ordering.end())
4082 : : break;
4083 : :
4084 : 665594 : Location loc = (*pexpr)->location();
4085 : 665594 : Statement* s;
4086 : 665594 : if ((*pexpr)->call_expression() == NULL
4087 : 470533 : || (*pexpr)->call_expression()->result_count() < 2)
4088 : : {
4089 : 495347 : Temporary_statement* ts = Statement::make_temporary(NULL, *pexpr,
4090 : : loc);
4091 : 495347 : s = ts;
4092 : 495347 : *pexpr = Expression::make_temporary_reference(ts, loc);
4093 : : }
4094 : : else
4095 : : {
4096 : : // A call expression which returns multiple results needs to
4097 : : // be handled specially. We can't create a temporary
4098 : : // because there is no type to give it. Any actual uses of
4099 : : // the values will be done via Call_result_expressions.
4100 : : //
4101 : : // Since a given call expression can be shared by multiple
4102 : : // Call_result_expressions, avoid hoisting the call the
4103 : : // second time we see it here. In addition, don't try to
4104 : : // hoist the top-level multi-return call in the statement,
4105 : : // since doing this would result a tree with more than one copy
4106 : : // of the call.
4107 : 170247 : if (this->remember_expression(*pexpr))
4108 : : s = NULL;
4109 : 81997 : else if (es != NULL && *pexpr == es->expr())
4110 : : s = NULL;
4111 : : else
4112 : 78880 : s = Statement::make_statement(*pexpr, true);
4113 : : }
4114 : :
4115 : 574227 : if (s != NULL)
4116 : : {
4117 : 574227 : block->insert_statement_before(*pindex, s);
4118 : 574227 : ++*pindex;
4119 : : }
4120 : : }
4121 : :
4122 : 425867 : if (init != orig_init)
4123 : 81272 : vds->var()->var_value()->set_init(init);
4124 : :
4125 : : return TRAVERSE_CONTINUE;
4126 : 4713497 : }
4127 : :
4128 : : // Implement the order of evaluation rules for the initializer of a
4129 : : // global variable.
4130 : :
4131 : : int
4132 : 1148735 : Order_eval::variable(Named_object* no)
4133 : : {
4134 : 1148735 : if (no->is_result_variable())
4135 : : return TRAVERSE_CONTINUE;
4136 : 1005419 : Variable* var = no->var_value();
4137 : 1005419 : Expression* init = var->init();
4138 : 1005419 : if (!var->is_global() || init == NULL)
4139 : : return TRAVERSE_CONTINUE;
4140 : :
4141 : 20356 : Find_eval_ordering find_eval_ordering;
4142 : 20356 : Expression::traverse(&init, &find_eval_ordering);
4143 : :
4144 : 20356 : if (find_eval_ordering.size() <= 1)
4145 : : {
4146 : : // If there is only one expression with a side-effect, we can
4147 : : // leave it in place.
4148 : : return TRAVERSE_SKIP_COMPONENTS;
4149 : : }
4150 : :
4151 : 1061 : Expression* orig_init = init;
4152 : :
4153 : 10178 : for (Find_eval_ordering::const_iterator p = find_eval_ordering.begin();
4154 : 10178 : p != find_eval_ordering.end();
4155 : 9117 : ++p)
4156 : : {
4157 : 9117 : Expression** pexpr = *p;
4158 : 9117 : Location loc = (*pexpr)->location();
4159 : 9117 : Statement* s;
4160 : 9117 : if ((*pexpr)->call_expression() == NULL
4161 : 8906 : || (*pexpr)->call_expression()->result_count() < 2)
4162 : : {
4163 : 8923 : Temporary_statement* ts = Statement::make_temporary(NULL, *pexpr,
4164 : : loc);
4165 : 8923 : s = ts;
4166 : 8923 : *pexpr = Expression::make_temporary_reference(ts, loc);
4167 : : }
4168 : : else
4169 : : {
4170 : : // A call expression which returns multiple results needs to
4171 : : // be handled specially.
4172 : 194 : s = Statement::make_statement(*pexpr, true);
4173 : : }
4174 : 9117 : var->add_preinit_statement(this->gogo_, s);
4175 : : }
4176 : :
4177 : 1061 : if (init != orig_init)
4178 : 448 : var->set_init(init);
4179 : :
4180 : : return TRAVERSE_SKIP_COMPONENTS;
4181 : 20356 : }
4182 : :
4183 : : // Use temporary variables to implement the order of evaluation rules.
4184 : :
4185 : : void
4186 : 4645 : Gogo::order_evaluations()
4187 : : {
4188 : 4645 : Order_eval order_eval(this);
4189 : 4645 : this->traverse(&order_eval);
4190 : 4645 : }
4191 : :
4192 : : // Order evaluations in a block.
4193 : :
4194 : : void
4195 : 92632 : Gogo::order_block(Block* block)
4196 : : {
4197 : 92632 : Order_eval order_eval(this);
4198 : 92632 : block->traverse(&order_eval);
4199 : 92632 : }
4200 : :
4201 : : // A traversal class used to find a single shortcut operator within an
4202 : : // expression.
4203 : :
4204 : 5383745 : class Find_shortcut : public Traverse
4205 : : {
4206 : : public:
4207 : 5404101 : Find_shortcut()
4208 : 5404101 : : Traverse(traverse_blocks
4209 : : | traverse_statements
4210 : : | traverse_expressions),
4211 : 5404101 : found_(NULL)
4212 : : { }
4213 : :
4214 : : // A pointer to the expression which was found, or NULL if none was
4215 : : // found.
4216 : : Expression**
4217 : 5302217 : found() const
4218 : 5302217 : { return this->found_; }
4219 : :
4220 : : protected:
4221 : : int
4222 : 1683339 : block(Block*)
4223 : 1683339 : { return TRAVERSE_SKIP_COMPONENTS; }
4224 : :
4225 : : int
4226 : 0 : statement(Block*, size_t*, Statement*)
4227 : 0 : { return TRAVERSE_SKIP_COMPONENTS; }
4228 : :
4229 : : int
4230 : : expression(Expression**);
4231 : :
4232 : : private:
4233 : : Expression** found_;
4234 : : };
4235 : :
4236 : : // Find a shortcut expression.
4237 : :
4238 : : int
4239 : 13600163 : Find_shortcut::expression(Expression** pexpr)
4240 : : {
4241 : 13600163 : Expression* expr = *pexpr;
4242 : 13600163 : Binary_expression* be = expr->binary_expression();
4243 : 744649 : if (be == NULL)
4244 : : return TRAVERSE_CONTINUE;
4245 : 744649 : Operator op = be->op();
4246 : 744649 : if (op != OPERATOR_OROR && op != OPERATOR_ANDAND)
4247 : : return TRAVERSE_CONTINUE;
4248 : 86467 : go_assert(this->found_ == NULL);
4249 : 86467 : this->found_ = pexpr;
4250 : 86467 : return TRAVERSE_EXIT;
4251 : : }
4252 : :
4253 : : // A traversal class used to turn shortcut operators into explicit if
4254 : : // statements.
4255 : :
4256 : 367488 : class Shortcuts : public Traverse
4257 : : {
4258 : : public:
4259 : 183744 : Shortcuts(Gogo* gogo)
4260 : 183744 : : Traverse(traverse_variables
4261 : : | traverse_statements),
4262 : 183744 : gogo_(gogo)
4263 : : { }
4264 : :
4265 : : protected:
4266 : : int
4267 : : variable(Named_object*);
4268 : :
4269 : : int
4270 : : statement(Block*, size_t*, Statement*);
4271 : :
4272 : : private:
4273 : : // Convert a shortcut operator.
4274 : : Statement*
4275 : : convert_shortcut(Block* enclosing, Expression** pshortcut);
4276 : :
4277 : : // The IR.
4278 : : Gogo* gogo_;
4279 : : };
4280 : :
4281 : : // Remove shortcut operators in a single statement.
4282 : :
4283 : : int
4284 : 5297278 : Shortcuts::statement(Block* block, size_t* pindex, Statement* s)
4285 : : {
4286 : : // FIXME: This approach doesn't work for switch statements, because
4287 : : // we add the new statements before the whole switch when we need to
4288 : : // instead add them just before the switch expression. The right
4289 : : // fix is probably to lower switch statements with nonconstant cases
4290 : : // to a series of conditionals.
4291 : 5297278 : if (s->switch_statement() != NULL)
4292 : : return TRAVERSE_CONTINUE;
4293 : :
4294 : 5470114 : while (true)
4295 : : {
4296 : 5383696 : Find_shortcut find_shortcut;
4297 : :
4298 : : // If S is a variable declaration, then ordinary traversal won't
4299 : : // do anything. We want to explicitly traverse the
4300 : : // initialization expression if there is one.
4301 : 5383696 : Variable_declaration_statement* vds = s->variable_declaration_statement();
4302 : 5383696 : Expression* init = NULL;
4303 : 5383696 : if (vds == NULL)
4304 : 5000433 : s->traverse_contents(&find_shortcut);
4305 : : else
4306 : : {
4307 : 383263 : init = vds->var()->var_value()->init();
4308 : 383263 : if (init == NULL)
4309 : : return TRAVERSE_CONTINUE;
4310 : 281379 : init->traverse(&init, &find_shortcut);
4311 : : }
4312 : 5281812 : Expression** pshortcut = find_shortcut.found();
4313 : 5281812 : if (pshortcut == NULL)
4314 : : return TRAVERSE_CONTINUE;
4315 : :
4316 : 86418 : Statement* snew = this->convert_shortcut(block, pshortcut);
4317 : 86418 : block->insert_statement_before(*pindex, snew);
4318 : 86418 : ++*pindex;
4319 : :
4320 : 86418 : if (pshortcut == &init)
4321 : 612 : vds->var()->var_value()->set_init(init);
4322 : 5383696 : }
4323 : : }
4324 : :
4325 : : // Remove shortcut operators in the initializer of a global variable.
4326 : :
4327 : : int
4328 : 1148735 : Shortcuts::variable(Named_object* no)
4329 : : {
4330 : 1148735 : if (no->is_result_variable())
4331 : : return TRAVERSE_CONTINUE;
4332 : 1005419 : Variable* var = no->var_value();
4333 : 1005419 : Expression* init = var->init();
4334 : 1005419 : if (!var->is_global() || init == NULL)
4335 : : return TRAVERSE_CONTINUE;
4336 : :
4337 : 20454 : while (true)
4338 : : {
4339 : 20405 : Find_shortcut find_shortcut;
4340 : 20405 : init->traverse(&init, &find_shortcut);
4341 : 20405 : Expression** pshortcut = find_shortcut.found();
4342 : 20405 : if (pshortcut == NULL)
4343 : 20356 : return TRAVERSE_CONTINUE;
4344 : :
4345 : 49 : Statement* snew = this->convert_shortcut(NULL, pshortcut);
4346 : 49 : var->add_preinit_statement(this->gogo_, snew);
4347 : 49 : if (pshortcut == &init)
4348 : 49 : var->set_init(init);
4349 : 20405 : }
4350 : : }
4351 : :
4352 : : // Given an expression which uses a shortcut operator, return a
4353 : : // statement which implements it, and update *PSHORTCUT accordingly.
4354 : :
4355 : : Statement*
4356 : 86467 : Shortcuts::convert_shortcut(Block* enclosing, Expression** pshortcut)
4357 : : {
4358 : 86467 : Binary_expression* shortcut = (*pshortcut)->binary_expression();
4359 : 86467 : Expression* left = shortcut->left();
4360 : 86467 : Expression* right = shortcut->right();
4361 : 86467 : Location loc = shortcut->location();
4362 : :
4363 : 86467 : Block* retblock = new Block(enclosing, loc);
4364 : 86467 : retblock->set_end_location(loc);
4365 : :
4366 : 86467 : Temporary_statement* ts = Statement::make_temporary(shortcut->type(),
4367 : : left, loc);
4368 : 86467 : retblock->add_statement(ts);
4369 : :
4370 : 86467 : Block* block = new Block(retblock, loc);
4371 : 86467 : block->set_end_location(loc);
4372 : 86467 : Expression* tmpref = Expression::make_temporary_reference(ts, loc);
4373 : 86467 : Statement* assign = Statement::make_assignment(tmpref, right, loc);
4374 : 86467 : block->add_statement(assign);
4375 : :
4376 : 86467 : Expression* cond = Expression::make_temporary_reference(ts, loc);
4377 : 86467 : if (shortcut->binary_expression()->op() == OPERATOR_OROR)
4378 : 30643 : cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
4379 : :
4380 : 86467 : Statement* if_statement = Statement::make_if_statement(cond, block, NULL,
4381 : : loc);
4382 : 86467 : if_statement->determine_types(this->gogo_);
4383 : 86467 : retblock->add_statement(if_statement);
4384 : :
4385 : 86467 : *pshortcut = Expression::make_temporary_reference(ts, loc);
4386 : :
4387 : 86467 : delete shortcut;
4388 : :
4389 : : // Now convert any shortcut operators in LEFT and RIGHT.
4390 : : // LEFT and RIGHT were skipped in the top level
4391 : : // Gogo::order_evaluations. We need to order their
4392 : : // components first.
4393 : 86467 : Order_eval order_eval(this->gogo_);
4394 : 86467 : retblock->traverse(&order_eval);
4395 : 86467 : Shortcuts shortcuts(this->gogo_);
4396 : 86467 : retblock->traverse(&shortcuts);
4397 : :
4398 : 86467 : return Statement::make_block_statement(retblock, loc);
4399 : 86467 : }
4400 : :
4401 : : // Turn shortcut operators into explicit if statements. Doing this
4402 : : // considerably simplifies the order of evaluation rules.
4403 : :
4404 : : void
4405 : 4645 : Gogo::remove_shortcuts()
4406 : : {
4407 : 4645 : Shortcuts shortcuts(this);
4408 : 4645 : this->traverse(&shortcuts);
4409 : 4645 : }
4410 : :
4411 : : // Turn shortcut operators into explicit if statements in a block.
4412 : :
4413 : : void
4414 : 92632 : Gogo::remove_shortcuts_in_block(Block* block)
4415 : : {
4416 : 92632 : Shortcuts shortcuts(this);
4417 : 92632 : block->traverse(&shortcuts);
4418 : 92632 : }
4419 : :
4420 : : // Traversal to flatten parse tree after order of evaluation rules are applied.
4421 : :
4422 : 927218 : class Flatten : public Traverse
4423 : : {
4424 : : public:
4425 : 463609 : Flatten(Gogo* gogo, Named_object* function)
4426 : : : Traverse(traverse_variables
4427 : : | traverse_functions
4428 : : | traverse_statements
4429 : : | traverse_expressions),
4430 : 463609 : gogo_(gogo), function_(function), inserter_()
4431 : : { }
4432 : :
4433 : : void
4434 : 445323 : set_inserter(const Statement_inserter* inserter)
4435 : 445323 : { this->inserter_ = *inserter; }
4436 : :
4437 : : int
4438 : : variable(Named_object*);
4439 : :
4440 : : int
4441 : : function(Named_object*);
4442 : :
4443 : : int
4444 : : statement(Block*, size_t* pindex, Statement*);
4445 : :
4446 : : int
4447 : : expression(Expression**);
4448 : :
4449 : : private:
4450 : : // General IR.
4451 : : Gogo* gogo_;
4452 : : // The function we are traversing.
4453 : : Named_object* function_;
4454 : : // Current statement inserter for use by expressions.
4455 : : Statement_inserter inserter_;
4456 : : };
4457 : :
4458 : : // Flatten variables.
4459 : :
4460 : : int
4461 : 1469305 : Flatten::variable(Named_object* no)
4462 : : {
4463 : 1469305 : if (!no->is_variable())
4464 : : return TRAVERSE_CONTINUE;
4465 : :
4466 : 1224587 : if (no->is_variable() && no->var_value()->is_global())
4467 : : {
4468 : : // Global variables can have loops in their initialization
4469 : : // expressions. This is handled in flatten_init_expression.
4470 : 348764 : no->var_value()->flatten_init_expression(this->gogo_, this->function_,
4471 : : &this->inserter_);
4472 : 348764 : return TRAVERSE_CONTINUE;
4473 : : }
4474 : :
4475 : 875823 : if (!no->var_value()->is_parameter()
4476 : 401990 : && !no->var_value()->is_receiver()
4477 : 401990 : && !no->var_value()->is_closure()
4478 : 401990 : && no->var_value()->is_non_escaping_address_taken()
4479 : 31914 : && !no->var_value()->is_in_heap()
4480 : 902526 : && no->var_value()->toplevel_decl() == NULL)
4481 : : {
4482 : : // Local variable that has address taken but not escape.
4483 : : // It needs to be live beyond its lexical scope. So we
4484 : : // create a top-level declaration for it.
4485 : : // No need to do it if it is already in the top level.
4486 : 21694 : Block* top_block = function_->func_value()->block();
4487 : 21694 : if (top_block->bindings()->lookup_local(no->name()) != no)
4488 : : {
4489 : 4935 : Variable* var = no->var_value();
4490 : 4935 : Temporary_statement* ts =
4491 : 4935 : Statement::make_temporary(var->type(), NULL, var->location());
4492 : 4935 : ts->set_is_address_taken();
4493 : 4935 : top_block->add_statement_at_front(ts);
4494 : 4935 : var->set_toplevel_decl(ts);
4495 : : }
4496 : : }
4497 : :
4498 : 875823 : go_assert(!no->var_value()->has_pre_init());
4499 : :
4500 : : return TRAVERSE_SKIP_COMPONENTS;
4501 : : }
4502 : :
4503 : : // Flatten the body of a function. Record the function while flattening it,
4504 : : // so that we can pass it down when flattening an expression.
4505 : :
4506 : : int
4507 : 295634 : Flatten::function(Named_object* no)
4508 : : {
4509 : 295634 : go_assert(this->function_ == NULL);
4510 : 295634 : this->function_ = no;
4511 : 295634 : int t = no->func_value()->traverse(this);
4512 : 295634 : this->function_ = NULL;
4513 : :
4514 : 295634 : if (t == TRAVERSE_EXIT)
4515 : 0 : return t;
4516 : : return TRAVERSE_SKIP_COMPONENTS;
4517 : : }
4518 : :
4519 : : // Flatten statement parse trees.
4520 : :
4521 : : int
4522 : 6381977 : Flatten::statement(Block* block, size_t* pindex, Statement* sorig)
4523 : : {
4524 : : // Because we explicitly traverse the statement's contents
4525 : : // ourselves, we want to skip block statements here. There is
4526 : : // nothing to flatten in a block statement.
4527 : 6381977 : if (sorig->is_block_statement())
4528 : : return TRAVERSE_CONTINUE;
4529 : :
4530 : 4957852 : Statement_inserter hold_inserter(this->inserter_);
4531 : 4957852 : this->inserter_ = Statement_inserter(block, pindex);
4532 : :
4533 : : // Flatten the expressions first.
4534 : 4957852 : int t = sorig->traverse_contents(this);
4535 : 4957852 : if (t == TRAVERSE_EXIT)
4536 : : {
4537 : 0 : this->inserter_ = hold_inserter;
4538 : 0 : return t;
4539 : : }
4540 : :
4541 : : // Keep flattening until nothing changes.
4542 : : Statement* s = sorig;
4543 : 4958213 : while (true)
4544 : : {
4545 : 4958213 : Statement* snew = s->flatten(this->gogo_, this->function_, block,
4546 : : &this->inserter_);
4547 : 4958213 : if (snew == s)
4548 : : break;
4549 : 361 : s = snew;
4550 : 361 : t = s->traverse_contents(this);
4551 : 361 : if (t == TRAVERSE_EXIT)
4552 : : {
4553 : 0 : this->inserter_ = hold_inserter;
4554 : 0 : return t;
4555 : : }
4556 : : }
4557 : :
4558 : 4957852 : if (s != sorig)
4559 : 361 : block->replace_statement(*pindex, s);
4560 : :
4561 : 4957852 : this->inserter_ = hold_inserter;
4562 : 4957852 : return TRAVERSE_SKIP_COMPONENTS;
4563 : : }
4564 : :
4565 : : // Flatten expression parse trees.
4566 : :
4567 : : int
4568 : 18299689 : Flatten::expression(Expression** pexpr)
4569 : : {
4570 : : // Keep flattening until nothing changes.
4571 : 18360189 : while (true)
4572 : : {
4573 : 18329939 : Expression* e = *pexpr;
4574 : 18329939 : if (e->traverse_subexpressions(this) == TRAVERSE_EXIT)
4575 : : return TRAVERSE_EXIT;
4576 : :
4577 : 18329939 : Expression* enew = e->flatten(this->gogo_, this->function_,
4578 : : &this->inserter_);
4579 : 18329939 : if (enew == e)
4580 : : break;
4581 : 30250 : *pexpr = enew;
4582 : 30250 : }
4583 : : return TRAVERSE_SKIP_COMPONENTS;
4584 : : }
4585 : :
4586 : : // Flatten a block.
4587 : :
4588 : : void
4589 : 13641 : Gogo::flatten_block(Named_object* function, Block* block)
4590 : : {
4591 : 13641 : Flatten flatten(this, function);
4592 : 13641 : block->traverse(&flatten);
4593 : 13641 : }
4594 : :
4595 : : // Flatten an expression. INSERTER may be NULL, in which case the
4596 : : // expression had better not need to create any temporaries.
4597 : :
4598 : : void
4599 : 445323 : Gogo::flatten_expression(Named_object* function, Statement_inserter* inserter,
4600 : : Expression** pexpr)
4601 : : {
4602 : 445323 : Flatten flatten(this, function);
4603 : 445323 : if (inserter != NULL)
4604 : 445323 : flatten.set_inserter(inserter);
4605 : 445323 : flatten.expression(pexpr);
4606 : 445323 : }
4607 : :
4608 : : void
4609 : 4645 : Gogo::flatten()
4610 : : {
4611 : 4645 : Flatten flatten(this, NULL);
4612 : 4645 : this->traverse(&flatten);
4613 : 4645 : }
4614 : :
4615 : : // Traversal to convert calls to the predeclared recover function to
4616 : : // pass in an argument indicating whether it can recover from a panic
4617 : : // or not.
4618 : :
4619 : 1662 : class Convert_recover : public Traverse
4620 : : {
4621 : : public:
4622 : 831 : Convert_recover(Named_object* arg)
4623 : 831 : : Traverse(traverse_expressions),
4624 : 831 : arg_(arg)
4625 : : { }
4626 : :
4627 : : protected:
4628 : : int
4629 : : expression(Expression**);
4630 : :
4631 : : private:
4632 : : // The argument to pass to the function.
4633 : : Named_object* arg_;
4634 : : };
4635 : :
4636 : : // Convert calls to recover.
4637 : :
4638 : : int
4639 : 22685 : Convert_recover::expression(Expression** pp)
4640 : : {
4641 : 22685 : Call_expression* ce = (*pp)->call_expression();
4642 : 2849 : if (ce != NULL && ce->is_recover_call())
4643 : 847 : ce->set_recover_arg(Expression::make_var_reference(this->arg_,
4644 : : ce->location()));
4645 : 22685 : return TRAVERSE_CONTINUE;
4646 : : }
4647 : :
4648 : : // Traversal for build_recover_thunks.
4649 : :
4650 : 9290 : class Build_recover_thunks : public Traverse
4651 : : {
4652 : : public:
4653 : 4645 : Build_recover_thunks(Gogo* gogo)
4654 : 4645 : : Traverse(traverse_functions),
4655 : 4645 : gogo_(gogo)
4656 : : { }
4657 : :
4658 : : int
4659 : : function(Named_object*);
4660 : :
4661 : : private:
4662 : : Expression*
4663 : : can_recover_arg(Location);
4664 : :
4665 : : // General IR.
4666 : : Gogo* gogo_;
4667 : : };
4668 : :
4669 : : // If this function calls recover, turn it into a thunk.
4670 : :
4671 : : int
4672 : 189434 : Build_recover_thunks::function(Named_object* orig_no)
4673 : : {
4674 : 189434 : Function* orig_func = orig_no->func_value();
4675 : 189434 : if (!orig_func->calls_recover()
4676 : 1662 : || orig_func->is_recover_thunk()
4677 : 191096 : || orig_func->has_recover_thunk())
4678 : : return TRAVERSE_CONTINUE;
4679 : :
4680 : 831 : Gogo* gogo = this->gogo_;
4681 : 831 : Location location = orig_func->location();
4682 : :
4683 : 831 : static int count;
4684 : 831 : char buf[50];
4685 : :
4686 : 831 : Function_type* orig_fntype = orig_func->type();
4687 : 831 : Typed_identifier_list* new_params = new Typed_identifier_list();
4688 : 831 : std::string receiver_name;
4689 : 831 : if (orig_fntype->is_method())
4690 : : {
4691 : 42 : const Typed_identifier* receiver = orig_fntype->receiver();
4692 : 42 : snprintf(buf, sizeof buf, "rt.%u", count);
4693 : 42 : ++count;
4694 : 42 : receiver_name = buf;
4695 : 84 : new_params->push_back(Typed_identifier(receiver_name, receiver->type(),
4696 : 42 : receiver->location()));
4697 : : }
4698 : 831 : const Typed_identifier_list* orig_params = orig_fntype->parameters();
4699 : 831 : if (orig_params != NULL && !orig_params->empty())
4700 : : {
4701 : 175 : for (Typed_identifier_list::const_iterator p = orig_params->begin();
4702 : 272 : p != orig_params->end();
4703 : 175 : ++p)
4704 : : {
4705 : 175 : snprintf(buf, sizeof buf, "pt.%u", count);
4706 : 175 : ++count;
4707 : 175 : new_params->push_back(Typed_identifier(buf, p->type(),
4708 : 350 : p->location()));
4709 : : }
4710 : : }
4711 : 831 : snprintf(buf, sizeof buf, "pr.%u", count);
4712 : 831 : ++count;
4713 : 831 : std::string can_recover_name = buf;
4714 : 1662 : new_params->push_back(Typed_identifier(can_recover_name,
4715 : 831 : Type::lookup_bool_type(),
4716 : 1662 : orig_fntype->location()));
4717 : :
4718 : 831 : const Typed_identifier_list* orig_results = orig_fntype->results();
4719 : 831 : Typed_identifier_list* new_results;
4720 : 831 : if (orig_results == NULL || orig_results->empty())
4721 : : new_results = NULL;
4722 : : else
4723 : : {
4724 : 3 : new_results = new Typed_identifier_list();
4725 : 3 : for (Typed_identifier_list::const_iterator p = orig_results->begin();
4726 : 6 : p != orig_results->end();
4727 : 3 : ++p)
4728 : 3 : new_results->push_back(Typed_identifier("", p->type(), p->location()));
4729 : : }
4730 : :
4731 : 831 : Function_type *new_fntype = Type::make_function_type(NULL, new_params,
4732 : : new_results,
4733 : : orig_fntype->location());
4734 : 831 : if (orig_fntype->is_varargs())
4735 : 1 : new_fntype->set_is_varargs();
4736 : :
4737 : 831 : Type* rtype = NULL;
4738 : 831 : if (orig_fntype->is_method())
4739 : 42 : rtype = orig_fntype->receiver()->type();
4740 : 831 : std::string name(gogo->recover_thunk_name(orig_no->name(), rtype));
4741 : 831 : Named_object *new_no = gogo->start_function(name, new_fntype, false,
4742 : : location);
4743 : 831 : Function *new_func = new_no->func_value();
4744 : 831 : if (orig_func->enclosing() != NULL)
4745 : 737 : new_func->set_enclosing(orig_func->enclosing());
4746 : :
4747 : : // We build the code for the original function attached to the new
4748 : : // function, and then swap the original and new function bodies.
4749 : : // This means that existing references to the original function will
4750 : : // then refer to the new function. That makes this code a little
4751 : : // confusing, in that the reference to NEW_NO really refers to the
4752 : : // other function, not the one we are building.
4753 : :
4754 : 831 : Expression* closure = NULL;
4755 : 831 : if (orig_func->needs_closure())
4756 : : {
4757 : : // For the new function we are creating, declare a new parameter
4758 : : // variable NEW_CLOSURE_NO and set it to be the closure variable
4759 : : // of the function. This will be set to the closure value
4760 : : // passed in by the caller. Then pass a reference to this
4761 : : // variable as the closure value when calling the original
4762 : : // function. In other words, simply pass the closure value
4763 : : // through the thunk we are creating.
4764 : 526 : Named_object* orig_closure_no = orig_func->closure_var();
4765 : 526 : Variable* orig_closure_var = orig_closure_no->var_value();
4766 : 526 : Variable* new_var = new Variable(orig_closure_var->type(), NULL, false,
4767 : 526 : false, false, location);
4768 : 526 : new_var->set_is_closure();
4769 : 526 : snprintf(buf, sizeof buf, "closure.%u", count);
4770 : 526 : ++count;
4771 : 526 : Named_object* new_closure_no = Named_object::make_variable(buf, NULL,
4772 : : new_var);
4773 : 526 : new_func->set_closure_var(new_closure_no);
4774 : 526 : closure = Expression::make_var_reference(new_closure_no, location);
4775 : : }
4776 : :
4777 : 831 : Expression* fn = Expression::make_func_reference(new_no, closure, location);
4778 : :
4779 : 831 : Expression_list* args = new Expression_list();
4780 : 831 : if (new_params != NULL)
4781 : : {
4782 : : // Note that we skip the last parameter, which is the boolean
4783 : : // indicating whether recover can succed.
4784 : 831 : for (Typed_identifier_list::const_iterator p = new_params->begin();
4785 : 1048 : p + 1 != new_params->end();
4786 : 217 : ++p)
4787 : : {
4788 : 217 : Named_object* p_no = gogo->lookup(p->name(), NULL);
4789 : 217 : go_assert(p_no != NULL
4790 : : && p_no->is_variable()
4791 : : && p_no->var_value()->is_parameter());
4792 : 217 : args->push_back(Expression::make_var_reference(p_no, location));
4793 : : }
4794 : : }
4795 : 831 : args->push_back(this->can_recover_arg(location));
4796 : :
4797 : 831 : gogo->start_block(location);
4798 : :
4799 : 831 : Call_expression* call = Expression::make_call(fn, args, false, location);
4800 : :
4801 : : // Any varargs call has already been lowered.
4802 : 831 : call->set_varargs_are_lowered();
4803 : :
4804 : 831 : Statement* s = Statement::make_return_from_call(new_no, call, location);
4805 : 831 : s->determine_types(this->gogo_);
4806 : 831 : gogo->add_statement(s);
4807 : :
4808 : 831 : Block* b = gogo->finish_block(location);
4809 : :
4810 : 831 : gogo->add_block(b, location);
4811 : :
4812 : : // Lower the call in case it returns multiple results.
4813 : 831 : gogo->lower_block(new_no, b);
4814 : :
4815 : 831 : gogo->finish_function(location);
4816 : :
4817 : : // Swap the function bodies and types.
4818 : 831 : new_func->swap_for_recover(orig_func);
4819 : 831 : orig_func->set_is_recover_thunk();
4820 : 831 : new_func->set_calls_recover();
4821 : 831 : new_func->set_has_recover_thunk();
4822 : :
4823 : 831 : Bindings* orig_bindings = orig_func->block()->bindings();
4824 : 831 : Bindings* new_bindings = new_func->block()->bindings();
4825 : 831 : if (orig_fntype->is_method())
4826 : : {
4827 : : // We changed the receiver to be a regular parameter. We have
4828 : : // to update the binding accordingly in both functions.
4829 : 42 : Named_object* orig_rec_no = orig_bindings->lookup_local(receiver_name);
4830 : 42 : go_assert(orig_rec_no != NULL
4831 : : && orig_rec_no->is_variable()
4832 : : && !orig_rec_no->var_value()->is_receiver());
4833 : 42 : orig_rec_no->var_value()->set_is_receiver();
4834 : :
4835 : 42 : std::string new_receiver_name(orig_fntype->receiver()->name());
4836 : 42 : if (new_receiver_name.empty())
4837 : : {
4838 : : // Find the receiver. It was named "r.NNN" in
4839 : : // Gogo::start_function.
4840 : 7 : for (Bindings::const_definitions_iterator p =
4841 : 7 : new_bindings->begin_definitions();
4842 : 7 : p != new_bindings->end_definitions();
4843 : 0 : ++p)
4844 : : {
4845 : 7 : const std::string& pname((*p)->name());
4846 : 7 : if (pname[0] == 'r' && pname[1] == '.')
4847 : : {
4848 : 7 : new_receiver_name = pname;
4849 : 7 : break;
4850 : : }
4851 : : }
4852 : 7 : go_assert(!new_receiver_name.empty());
4853 : : }
4854 : 42 : Named_object* new_rec_no = new_bindings->lookup_local(new_receiver_name);
4855 : 42 : if (new_rec_no == NULL)
4856 : 0 : go_assert(saw_errors());
4857 : : else
4858 : : {
4859 : 42 : go_assert(new_rec_no->is_variable()
4860 : : && new_rec_no->var_value()->is_receiver());
4861 : 42 : new_rec_no->var_value()->set_is_not_receiver();
4862 : : }
4863 : 42 : }
4864 : :
4865 : : // Because we flipped blocks but not types, the can_recover
4866 : : // parameter appears in the (now) old bindings as a parameter.
4867 : : // Change it to a local variable, whereupon it will be discarded.
4868 : 831 : Named_object* can_recover_no = orig_bindings->lookup_local(can_recover_name);
4869 : 831 : go_assert(can_recover_no != NULL
4870 : : && can_recover_no->is_variable()
4871 : : && can_recover_no->var_value()->is_parameter());
4872 : 831 : orig_bindings->remove_binding(can_recover_no);
4873 : :
4874 : : // Add the can_recover argument to the (now) new bindings, and
4875 : : // attach it to any recover statements.
4876 : 831 : Variable* can_recover_var = new Variable(Type::lookup_bool_type(), NULL,
4877 : 1662 : false, true, false, location);
4878 : 831 : can_recover_no = new_bindings->add_variable(can_recover_name, NULL,
4879 : : can_recover_var);
4880 : 831 : Convert_recover convert_recover(can_recover_no);
4881 : 831 : new_func->traverse(&convert_recover);
4882 : :
4883 : : // Update the function pointers in any named results.
4884 : 831 : new_func->update_result_variables();
4885 : 831 : orig_func->update_result_variables();
4886 : :
4887 : 831 : return TRAVERSE_CONTINUE;
4888 : 831 : }
4889 : :
4890 : : // Return the expression to pass for the .can_recover parameter to the
4891 : : // new function. This indicates whether a call to recover may return
4892 : : // non-nil. The expression is runtime.canrecover(__builtin_return_address()).
4893 : :
4894 : : Expression*
4895 : 831 : Build_recover_thunks::can_recover_arg(Location location)
4896 : : {
4897 : 831 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
4898 : 831 : static Named_object* can_recover;
4899 : 831 : if (can_recover == NULL)
4900 : : {
4901 : 319 : const Location bloc = Linemap::predeclared_location();
4902 : 319 : Typed_identifier_list* param_types = new Typed_identifier_list();
4903 : 319 : param_types->push_back(Typed_identifier("a", uintptr_type, bloc));
4904 : 319 : Type* boolean_type = Type::lookup_bool_type();
4905 : 319 : Typed_identifier_list* results = new Typed_identifier_list();
4906 : 319 : results->push_back(Typed_identifier("", boolean_type, bloc));
4907 : 319 : Function_type* fntype = Type::make_function_type(NULL, param_types,
4908 : : results, bloc);
4909 : 638 : can_recover =
4910 : 319 : Named_object::make_function_declaration("runtime_canrecover",
4911 : : NULL, fntype, bloc);
4912 : 319 : can_recover->func_declaration_value()->set_asm_name("runtime.canrecover");
4913 : : }
4914 : :
4915 : 831 : Expression* zexpr = Expression::make_integer_ul(0, NULL, location);
4916 : 831 : Expression* call = Runtime::make_call(this->gogo_,
4917 : : Runtime::BUILTIN_RETURN_ADDRESS,
4918 : : location, 1, zexpr);
4919 : 831 : call = Expression::make_unsafe_cast(uintptr_type, call, location);
4920 : :
4921 : 831 : Expression_list* args = new Expression_list();
4922 : 831 : args->push_back(call);
4923 : :
4924 : 831 : Expression* fn = Expression::make_func_reference(can_recover, NULL, location);
4925 : 831 : return Expression::make_call(fn, args, false, location);
4926 : : }
4927 : :
4928 : : // Build thunks for functions which call recover. We build a new
4929 : : // function with an extra parameter, which is whether a call to
4930 : : // recover can succeed. We then move the body of this function to
4931 : : // that one. We then turn this function into a thunk which calls the
4932 : : // new one, passing the value of runtime.canrecover(__builtin_return_address()).
4933 : : // The function will be marked as not splitting the stack. This will
4934 : : // cooperate with the implementation of defer to make recover do the
4935 : : // right thing.
4936 : :
4937 : : void
4938 : 4645 : Gogo::build_recover_thunks()
4939 : : {
4940 : 4645 : Build_recover_thunks build_recover_thunks(this);
4941 : 4645 : this->traverse(&build_recover_thunks);
4942 : 4645 : }
4943 : :
4944 : : // Look for named types to see whether we need to create an interface
4945 : : // method table.
4946 : :
4947 : 4178 : class Build_method_tables : public Traverse
4948 : : {
4949 : : public:
4950 : 2089 : Build_method_tables(Gogo* gogo,
4951 : : const std::vector<Interface_type*>& interfaces)
4952 : 2089 : : Traverse(traverse_types),
4953 : 2089 : gogo_(gogo), interfaces_(interfaces)
4954 : : { }
4955 : :
4956 : : int
4957 : : type(Type*);
4958 : :
4959 : : private:
4960 : : // The IR.
4961 : : Gogo* gogo_;
4962 : : // A list of locally defined interfaces which have hidden methods.
4963 : : const std::vector<Interface_type*>& interfaces_;
4964 : : };
4965 : :
4966 : : // Build all required interface method tables for types. We need to
4967 : : // ensure that we have an interface method table for every interface
4968 : : // which has a hidden method, for every named type which implements
4969 : : // that interface. Normally we can just build interface method tables
4970 : : // as we need them. However, in some cases we can require an
4971 : : // interface method table for an interface defined in a different
4972 : : // package for a type defined in that package. If that interface and
4973 : : // type both use a hidden method, that is OK. However, we will not be
4974 : : // able to build that interface method table when we need it, because
4975 : : // the type's hidden method will be static. So we have to build it
4976 : : // here, and just refer it from other packages as needed.
4977 : :
4978 : : void
4979 : 4645 : Gogo::build_interface_method_tables()
4980 : : {
4981 : 4645 : if (saw_errors())
4982 : 411 : return;
4983 : :
4984 : 4234 : std::vector<Interface_type*> hidden_interfaces;
4985 : 4234 : hidden_interfaces.reserve(this->interface_types_.size());
4986 : 70825 : for (std::vector<Interface_type*>::const_iterator pi =
4987 : 4234 : this->interface_types_.begin();
4988 : 75059 : pi != this->interface_types_.end();
4989 : 70825 : ++pi)
4990 : : {
4991 : 70825 : const Typed_identifier_list* methods = (*pi)->methods();
4992 : 70825 : if (methods == NULL)
4993 : 1486 : continue;
4994 : 293117 : for (Typed_identifier_list::const_iterator pm = methods->begin();
4995 : 293117 : pm != methods->end();
4996 : 223778 : ++pm)
4997 : : {
4998 : 232173 : if (Gogo::is_hidden_name(pm->name()))
4999 : : {
5000 : 8395 : hidden_interfaces.push_back(*pi);
5001 : 8395 : break;
5002 : : }
5003 : : }
5004 : : }
5005 : :
5006 : 4234 : if (!hidden_interfaces.empty())
5007 : : {
5008 : : // Now traverse the tree looking for all named types.
5009 : 2089 : Build_method_tables bmt(this, hidden_interfaces);
5010 : 2089 : this->traverse(&bmt);
5011 : 2089 : }
5012 : :
5013 : : // We no longer need the list of interfaces.
5014 : :
5015 : 7748 : this->interface_types_.clear();
5016 : 4234 : }
5017 : :
5018 : : // This is called for each type. For a named type, for each of the
5019 : : // interfaces with hidden methods that it implements, create the
5020 : : // method table.
5021 : :
5022 : : int
5023 : 11423750 : Build_method_tables::type(Type* type)
5024 : : {
5025 : 11423750 : Named_type* nt = type->named_type();
5026 : 11423750 : Struct_type* st = type->struct_type();
5027 : 11423750 : if (nt != NULL || st != NULL)
5028 : : {
5029 : 3642763 : Translate_context context(this->gogo_, NULL, NULL, NULL);
5030 : 19162958 : for (std::vector<Interface_type*>::const_iterator p =
5031 : 3642763 : this->interfaces_.begin();
5032 : 22805721 : p != this->interfaces_.end();
5033 : 19162958 : ++p)
5034 : : {
5035 : : // We ask whether a pointer to the named type implements the
5036 : : // interface, because a pointer can implement more methods
5037 : : // than a value.
5038 : 19162958 : if (nt != NULL)
5039 : : {
5040 : 15767331 : if ((*p)->implements_interface(Type::make_pointer_type(nt),
5041 : : NULL))
5042 : : {
5043 : 69546 : nt->interface_method_table(*p, false)->get_backend(&context);
5044 : 69546 : nt->interface_method_table(*p, true)->get_backend(&context);
5045 : : }
5046 : : }
5047 : : else
5048 : : {
5049 : 3395627 : if ((*p)->implements_interface(Type::make_pointer_type(st),
5050 : : NULL))
5051 : : {
5052 : 249 : st->interface_method_table(*p, false)->get_backend(&context);
5053 : 249 : st->interface_method_table(*p, true)->get_backend(&context);
5054 : : }
5055 : : }
5056 : : }
5057 : : }
5058 : 11423750 : return TRAVERSE_CONTINUE;
5059 : : }
5060 : :
5061 : : // Return an expression which allocates memory to hold values of type TYPE.
5062 : :
5063 : : Expression*
5064 : 204381 : Gogo::allocate_memory(Type* type, Location location)
5065 : : {
5066 : 204381 : Expression* td = Expression::make_type_descriptor(type, location);
5067 : 204381 : return Runtime::make_call(this, Runtime::NEW, location, 1, td);
5068 : : }
5069 : :
5070 : : // Traversal class used to check for return statements.
5071 : :
5072 : 9290 : class Check_return_statements_traverse : public Traverse
5073 : : {
5074 : : public:
5075 : 4645 : Check_return_statements_traverse()
5076 : 4645 : : Traverse(traverse_functions)
5077 : : { }
5078 : :
5079 : : int
5080 : : function(Named_object*);
5081 : : };
5082 : :
5083 : : // Check that a function has a return statement if it needs one.
5084 : :
5085 : : int
5086 : 174977 : Check_return_statements_traverse::function(Named_object* no)
5087 : : {
5088 : 174977 : Function* func = no->func_value();
5089 : 174977 : const Function_type* fntype = func->type();
5090 : 174977 : const Typed_identifier_list* results = fntype->results();
5091 : :
5092 : : // We only need a return statement if there is a return value.
5093 : 174977 : if (results == NULL || results->empty())
5094 : : return TRAVERSE_CONTINUE;
5095 : :
5096 : 104631 : if (func->block()->may_fall_through())
5097 : 226 : go_error_at(func->block()->end_location(),
5098 : : "missing return at end of function");
5099 : :
5100 : : return TRAVERSE_CONTINUE;
5101 : : }
5102 : :
5103 : : // Check return statements.
5104 : :
5105 : : void
5106 : 4645 : Gogo::check_return_statements()
5107 : : {
5108 : 4645 : Check_return_statements_traverse traverse;
5109 : 4645 : this->traverse(&traverse);
5110 : 4645 : }
5111 : :
5112 : : // Traversal class to decide whether a function body is less than the
5113 : : // inlining budget. This adjusts *available as it goes, and stops the
5114 : : // traversal if it goes negative.
5115 : :
5116 : 1636631 : class Inline_within_budget : public Traverse
5117 : : {
5118 : : public:
5119 : 1822972 : Inline_within_budget(int* available)
5120 : 1822972 : : Traverse(traverse_statements
5121 : : | traverse_expressions),
5122 : 1822972 : available_(available)
5123 : : { }
5124 : :
5125 : : int
5126 : : statement(Block*, size_t*, Statement*);
5127 : :
5128 : : int
5129 : : expression(Expression**);
5130 : :
5131 : : private:
5132 : : // Pointer to remaining budget.
5133 : : int* available_;
5134 : : };
5135 : :
5136 : : // Adjust the budget for the inlining cost of a statement.
5137 : :
5138 : : int
5139 : 4945654 : Inline_within_budget::statement(Block*, size_t*, Statement* s)
5140 : : {
5141 : 4945654 : if (*this->available_ < 0)
5142 : : return TRAVERSE_EXIT;
5143 : 4926467 : *this->available_ -= s->inlining_cost();
5144 : 4926467 : return TRAVERSE_CONTINUE;
5145 : : }
5146 : :
5147 : : // Adjust the budget for the inlining cost of an expression.
5148 : :
5149 : : int
5150 : 10891668 : Inline_within_budget::expression(Expression** pexpr)
5151 : : {
5152 : 10891668 : if (*this->available_ < 0)
5153 : : return TRAVERSE_EXIT;
5154 : 9245878 : *this->available_ -= (*pexpr)->inlining_cost();
5155 : 9245878 : return TRAVERSE_CONTINUE;
5156 : : }
5157 : :
5158 : : // Traversal class to find functions whose body should be exported for
5159 : : // inlining by other packages.
5160 : :
5161 : 8502 : class Mark_inline_candidates : public Traverse
5162 : : {
5163 : : public:
5164 : 4251 : Mark_inline_candidates(Unordered_set(Named_object*)* marked)
5165 : 4251 : : Traverse(traverse_functions
5166 : : | traverse_types),
5167 : 4251 : marked_functions_(marked)
5168 : : { }
5169 : :
5170 : : int
5171 : : function(Named_object*);
5172 : :
5173 : : int
5174 : : type(Type*);
5175 : :
5176 : : private:
5177 : : // We traverse the function body trying to determine how expensive
5178 : : // it is for inlining. We start with a budget, and decrease that
5179 : : // budget for each statement and expression. If the budget goes
5180 : : // negative, we do not export the function body. The value of this
5181 : : // budget is a heuristic. In the usual GCC spirit, we could
5182 : : // consider setting this via a command line option.
5183 : : const int budget_heuristic = 80;
5184 : :
5185 : : // Set of named objects that are marked as inline candidates.
5186 : : Unordered_set(Named_object*)* marked_functions_;
5187 : : };
5188 : :
5189 : : // Mark a function if it is an inline candidate.
5190 : :
5191 : : int
5192 : 186701 : Mark_inline_candidates::function(Named_object* no)
5193 : : {
5194 : 186701 : Function* func = no->func_value();
5195 : 186701 : if ((func->pragmas() & GOPRAGMA_NOINLINE) != 0)
5196 : : return TRAVERSE_CONTINUE;
5197 : 186341 : int budget = budget_heuristic;
5198 : 186341 : Inline_within_budget iwb(&budget);
5199 : 186341 : func->block()->traverse(&iwb);
5200 : 186341 : if (budget >= 0)
5201 : : {
5202 : 41215 : func->set_export_for_inlining();
5203 : 41215 : this->marked_functions_->insert(no);
5204 : : }
5205 : 186341 : return TRAVERSE_CONTINUE;
5206 : 186341 : }
5207 : :
5208 : : // Mark methods if they are inline candidates.
5209 : :
5210 : : int
5211 : 11035276 : Mark_inline_candidates::type(Type* t)
5212 : : {
5213 : 11035276 : Named_type* nt = t->named_type();
5214 : 11035276 : if (nt == NULL || nt->is_alias())
5215 : : return TRAVERSE_CONTINUE;
5216 : 3156634 : const Bindings* methods = nt->local_methods();
5217 : 3156634 : if (methods == NULL)
5218 : : return TRAVERSE_CONTINUE;
5219 : 2115020 : for (Bindings::const_definitions_iterator p = methods->begin_definitions();
5220 : 2115020 : p != methods->end_definitions();
5221 : 1637896 : ++p)
5222 : : {
5223 : 1637896 : Named_object* no = *p;
5224 : 1637896 : go_assert(no->is_function());
5225 : 1637896 : Function *func = no->func_value();
5226 : 1637896 : if ((func->pragmas() & GOPRAGMA_NOINLINE) != 0)
5227 : 1265 : continue;
5228 : 1636631 : int budget = budget_heuristic;
5229 : 1636631 : Inline_within_budget iwb(&budget);
5230 : 1636631 : func->block()->traverse(&iwb);
5231 : 1636631 : if (budget >= 0)
5232 : : {
5233 : 116157 : func->set_export_for_inlining();
5234 : 116157 : this->marked_functions_->insert(no);
5235 : : }
5236 : 1636631 : }
5237 : : return TRAVERSE_CONTINUE;
5238 : : }
5239 : :
5240 : : // Export identifiers as requested.
5241 : :
5242 : : void
5243 : 4645 : Gogo::do_exports()
5244 : : {
5245 : 4645 : if (saw_errors())
5246 : 394 : return;
5247 : :
5248 : : // Mark any functions whose body should be exported for inlining by
5249 : : // other packages.
5250 : 4251 : Unordered_set(Named_object*) marked_functions;
5251 : 4251 : Mark_inline_candidates mic(&marked_functions);
5252 : 4251 : this->traverse(&mic);
5253 : :
5254 : : // For now we always stream to a section. Later we may want to
5255 : : // support streaming to a separate file.
5256 : 4251 : Stream_to_section stream(this->backend());
5257 : :
5258 : : // Write out either the prefix or pkgpath depending on how we were
5259 : : // invoked.
5260 : 4251 : std::string prefix;
5261 : 4251 : std::string pkgpath;
5262 : 4251 : if (this->pkgpath_from_option_)
5263 : 2005 : pkgpath = this->pkgpath_;
5264 : 2246 : else if (this->prefix_from_option_)
5265 : 0 : prefix = this->prefix_;
5266 : 2246 : else if (this->is_main_package())
5267 : 1636 : pkgpath = "main";
5268 : : else
5269 : 610 : prefix = "go";
5270 : :
5271 : 4251 : std::string init_fn_name;
5272 : 4251 : if (this->is_main_package())
5273 : 1636 : init_fn_name = "";
5274 : 2615 : else if (this->need_init_fn_)
5275 : 1525 : init_fn_name = this->get_init_fn_name();
5276 : : else
5277 : 1090 : init_fn_name = this->dummy_init_fn_name();
5278 : :
5279 : 4251 : Export exp(&stream);
5280 : 4251 : exp.register_builtin_types(this);
5281 : 4251 : exp.export_globals(this->package_name(),
5282 : : prefix,
5283 : : pkgpath,
5284 : 4251 : this->packages_,
5285 : 4251 : this->imports_,
5286 : : init_fn_name,
5287 : 4251 : this->imported_init_fns_,
5288 : 4251 : this->package_->bindings(),
5289 : : &marked_functions);
5290 : :
5291 : 4251 : if (!this->c_header_.empty() && !saw_errors())
5292 : 4 : this->write_c_header();
5293 : 8502 : }
5294 : :
5295 : : // Write the top level named struct types in C format to a C header
5296 : : // file. This is used when building the runtime package, to share
5297 : : // struct definitions between C and Go.
5298 : :
5299 : : void
5300 : 4 : Gogo::write_c_header()
5301 : : {
5302 : 4 : std::ofstream out;
5303 : 4 : out.open(this->c_header_.c_str());
5304 : 4 : if (out.fail())
5305 : : {
5306 : 0 : go_error_at(Linemap::unknown_location(),
5307 : : "cannot open %s: %m", this->c_header_.c_str());
5308 : 0 : return;
5309 : : }
5310 : :
5311 : 4 : std::list<Named_object*> types;
5312 : 4 : Bindings* top = this->package_->bindings();
5313 : 40482 : for (Bindings::const_definitions_iterator p = top->begin_definitions();
5314 : 40482 : p != top->end_definitions();
5315 : 40478 : ++p)
5316 : : {
5317 : 40478 : Named_object* no = *p;
5318 : :
5319 : : // Skip names that start with underscore followed by something
5320 : : // other than an uppercase letter, as when compiling the runtime
5321 : : // package they are mostly types defined by mkrsysinfo.sh based
5322 : : // on the C system header files. We don't need to translate
5323 : : // types to C and back to Go. But do accept the special cases
5324 : : // _defer, _panic, and _type.
5325 : 40478 : std::string name = Gogo::unpack_hidden_name(no->name());
5326 : 40478 : if (name[0] == '_'
5327 : 28848 : && (name[1] < 'A' || name[1] > 'Z')
5328 : 50366 : && (name != "_defer" && name != "_panic" && name != "_type"))
5329 : 9872 : continue;
5330 : :
5331 : 30606 : if (no->is_type() && no->type_value()->struct_type() != NULL)
5332 : 656 : types.push_back(no);
5333 : 30606 : if (no->is_const()
5334 : 51416 : && no->const_value()->type()->integer_type() != NULL
5335 : 50880 : && !no->const_value()->is_sink())
5336 : : {
5337 : 20270 : Numeric_constant nc;
5338 : 20270 : unsigned long val;
5339 : 20270 : if (no->const_value()->expr()->numeric_constant_value(&nc)
5340 : 20270 : && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID)
5341 : : {
5342 : 60114 : out << "#define " << no->message_name() << ' ' << val
5343 : 40076 : << std::endl;
5344 : : }
5345 : 20270 : }
5346 : 40478 : }
5347 : :
5348 : 4 : std::vector<const Named_object*> written;
5349 : 4 : int loop = 0;
5350 : 772 : while (!types.empty())
5351 : : {
5352 : 768 : Named_object* no = types.front();
5353 : 768 : types.pop_front();
5354 : :
5355 : 768 : std::vector<const Named_object*> needs;
5356 : 768 : std::vector<const Named_object*> declare;
5357 : 1536 : if (!no->type_value()->struct_type()->can_write_to_c_header(&needs,
5358 : : &declare))
5359 : 60 : continue;
5360 : :
5361 : 708 : bool ok = true;
5362 : 1172 : for (std::vector<const Named_object*>::const_iterator pr
5363 : 708 : = needs.begin();
5364 : 1172 : pr != needs.end() && ok;
5365 : 464 : ++pr)
5366 : : {
5367 : 17612 : for (std::list<Named_object*>::const_iterator pt = types.begin();
5368 : 17612 : pt != types.end() && ok;
5369 : 17148 : ++pt)
5370 : 17148 : if (*pr == *pt)
5371 : 112 : ok = false;
5372 : : }
5373 : 708 : if (!ok)
5374 : : {
5375 : 112 : ++loop;
5376 : 112 : if (loop > 10000)
5377 : : {
5378 : : // This should be impossible since the code parsed and
5379 : : // type checked.
5380 : 0 : go_unreachable();
5381 : : }
5382 : :
5383 : 112 : types.push_back(no);
5384 : 112 : continue;
5385 : : }
5386 : :
5387 : 1136 : for (std::vector<const Named_object*>::const_iterator pd
5388 : 596 : = declare.begin();
5389 : 1136 : pd != declare.end();
5390 : 540 : ++pd)
5391 : : {
5392 : 540 : if (*pd == no)
5393 : 112 : continue;
5394 : :
5395 : 456 : std::vector<const Named_object*> dneeds;
5396 : 456 : std::vector<const Named_object*> ddeclare;
5397 : 912 : if (!(*pd)->type_value()->struct_type()->
5398 : 912 : can_write_to_c_header(&dneeds, &ddeclare))
5399 : 28 : continue;
5400 : :
5401 : 428 : bool done = false;
5402 : 18764 : for (std::vector<const Named_object*>::const_iterator pw
5403 : 428 : = written.begin();
5404 : 18764 : pw != written.end();
5405 : 18336 : ++pw)
5406 : : {
5407 : 18668 : if (*pw == *pd)
5408 : : {
5409 : : done = true;
5410 : : break;
5411 : : }
5412 : : }
5413 : 428 : if (!done)
5414 : : {
5415 : 96 : out << std::endl;
5416 : 96 : out << "struct " << (*pd)->message_name() << ";" << std::endl;
5417 : 96 : written.push_back(*pd);
5418 : : }
5419 : 456 : }
5420 : :
5421 : 596 : out << std::endl;
5422 : 596 : out << "struct " << no->message_name() << " {" << std::endl;
5423 : 1192 : no->type_value()->struct_type()->write_to_c_header(out);
5424 : 596 : out << "};" << std::endl;
5425 : 596 : written.push_back(no);
5426 : 768 : }
5427 : :
5428 : 4 : out.close();
5429 : 4 : if (out.fail())
5430 : 0 : go_error_at(Linemap::unknown_location(),
5431 : : "error writing to %s: %m", this->c_header_.c_str());
5432 : 4 : }
5433 : :
5434 : : // Find the blocks in order to convert named types defined in blocks.
5435 : :
5436 : 9290 : class Convert_named_types : public Traverse
5437 : : {
5438 : : public:
5439 : 4645 : Convert_named_types(Gogo* gogo)
5440 : 4645 : : Traverse(traverse_blocks),
5441 : 4645 : gogo_(gogo)
5442 : : { }
5443 : :
5444 : : protected:
5445 : : int
5446 : : block(Block* block);
5447 : :
5448 : : private:
5449 : : Gogo* gogo_;
5450 : : };
5451 : :
5452 : : int
5453 : 1676057 : Convert_named_types::block(Block* block)
5454 : : {
5455 : 1676057 : this->gogo_->convert_named_types_in_bindings(block->bindings());
5456 : 1676057 : return TRAVERSE_CONTINUE;
5457 : : }
5458 : :
5459 : : // Convert all named types to the backend representation. Since named
5460 : : // types can refer to other types, this needs to be done in the right
5461 : : // sequence, which is handled by Named_type::convert. Here we arrange
5462 : : // to call that for each named type.
5463 : :
5464 : : void
5465 : 4645 : Gogo::convert_named_types()
5466 : : {
5467 : 4645 : this->convert_named_types_in_bindings(this->globals_);
5468 : 120889 : for (Packages::iterator p = this->packages_.begin();
5469 : 120889 : p != this->packages_.end();
5470 : 116244 : ++p)
5471 : : {
5472 : 116244 : Package* package = p->second;
5473 : 116244 : this->convert_named_types_in_bindings(package->bindings());
5474 : : }
5475 : :
5476 : 4645 : Convert_named_types cnt(this);
5477 : 4645 : this->traverse(&cnt);
5478 : :
5479 : : // Make all the builtin named types used for type descriptors, and
5480 : : // then convert them. They will only be written out if they are
5481 : : // needed.
5482 : 4645 : Type::make_type_descriptor_type();
5483 : 4645 : Type::make_type_descriptor_ptr_type();
5484 : 4645 : Function_type::make_function_type_descriptor_type();
5485 : 4645 : Pointer_type::make_pointer_type_descriptor_type();
5486 : 4645 : Struct_type::make_struct_type_descriptor_type();
5487 : 4645 : Array_type::make_array_type_descriptor_type();
5488 : 4645 : Array_type::make_slice_type_descriptor_type();
5489 : 4645 : Map_type::make_map_type_descriptor_type();
5490 : 4645 : Channel_type::make_chan_type_descriptor_type();
5491 : 4645 : Interface_type::make_interface_type_descriptor_type();
5492 : 4645 : Expression::make_func_descriptor_type();
5493 : 4645 : Type::convert_builtin_named_types(this);
5494 : :
5495 : 4645 : Runtime::convert_types(this);
5496 : :
5497 : 4645 : this->named_types_are_converted_ = true;
5498 : :
5499 : 4645 : Type::finish_pointer_types(this);
5500 : 4645 : }
5501 : :
5502 : : // Convert all names types in a set of bindings.
5503 : :
5504 : : void
5505 : 1796946 : Gogo::convert_named_types_in_bindings(Bindings* bindings)
5506 : : {
5507 : 4922864 : for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
5508 : 4922864 : p != bindings->end_definitions();
5509 : 3125918 : ++p)
5510 : : {
5511 : 3125918 : if ((*p)->is_type())
5512 : 733266 : (*p)->type_value()->convert(this);
5513 : : }
5514 : 1796946 : }
5515 : :
5516 : : void
5517 : 0 : debug_go_gogo(Gogo* gogo)
5518 : : {
5519 : 0 : if (gogo != NULL)
5520 : 0 : gogo->debug_dump();
5521 : 0 : }
5522 : :
5523 : : void
5524 : 0 : Gogo::debug_dump()
5525 : : {
5526 : 0 : std::cerr << "Packages:\n";
5527 : 0 : for (Packages::const_iterator p = this->packages_.begin();
5528 : 0 : p != this->packages_.end();
5529 : 0 : ++p)
5530 : : {
5531 : 0 : const char *tag = " ";
5532 : 0 : if (p->second == this->package_)
5533 : 0 : tag = "* ";
5534 : 0 : std::cerr << tag << "'" << p->first << "' "
5535 : 0 : << p->second->pkgpath() << " " << ((void*)p->second) << "\n";
5536 : : }
5537 : 0 : }
5538 : :
5539 : : // Class Function.
5540 : :
5541 : 299092 : Function::Function(Function_type* type, Named_object* enclosing, Block* block,
5542 : 299092 : Location location)
5543 : 299092 : : type_(type), enclosing_(enclosing), results_(NULL),
5544 : 299092 : closure_var_(NULL), block_(block), location_(location), labels_(),
5545 : 299092 : local_type_count_(0), descriptor_(NULL), fndecl_(NULL), defer_stack_(NULL),
5546 : 299092 : pragmas_(0), nested_functions_(0), is_sink_(false),
5547 : 299092 : results_are_named_(false), is_unnamed_type_stub_method_(false),
5548 : 299092 : calls_recover_(false), is_recover_thunk_(false), has_recover_thunk_(false),
5549 : 299092 : calls_defer_retaddr_(false), is_type_specific_function_(false),
5550 : 299092 : in_unique_section_(false), export_for_inlining_(false),
5551 : 299092 : is_inline_only_(false), is_referenced_by_inline_(false),
5552 : 299092 : is_exported_by_linkname_(false)
5553 : : {
5554 : 299092 : }
5555 : :
5556 : : // Create the named result variables.
5557 : :
5558 : : void
5559 : 295639 : Function::create_result_variables(Gogo* gogo)
5560 : : {
5561 : 295639 : const Typed_identifier_list* results = this->type_->results();
5562 : 295639 : if (results == NULL || results->empty())
5563 : : return;
5564 : :
5565 : 217492 : if (!results->front().name().empty())
5566 : 21590 : this->results_are_named_ = true;
5567 : :
5568 : 217492 : this->results_ = new Results();
5569 : 217492 : this->results_->reserve(results->size());
5570 : :
5571 : 217492 : Block* block = this->block_;
5572 : 217492 : int index = 0;
5573 : 217492 : for (Typed_identifier_list::const_iterator p = results->begin();
5574 : 462211 : p != results->end();
5575 : 244719 : ++p, ++index)
5576 : : {
5577 : 244719 : std::string name = p->name();
5578 : 244719 : if (name.empty() || Gogo::is_sink_name(name))
5579 : : {
5580 : 211912 : static int result_counter;
5581 : 211912 : char buf[100];
5582 : 211912 : snprintf(buf, sizeof buf, "$ret%d", result_counter);
5583 : 211912 : ++result_counter;
5584 : 211912 : name = gogo->pack_hidden_name(buf, false);
5585 : : }
5586 : 244719 : Result_variable* result = new Result_variable(p->type(), this, index,
5587 : 244719 : p->location());
5588 : 244719 : Named_object* no = block->bindings()->add_result_variable(name, result);
5589 : 244719 : if (no->is_result_variable())
5590 : 244715 : this->results_->push_back(no);
5591 : : else
5592 : : {
5593 : 4 : static int dummy_result_count;
5594 : 4 : char buf[100];
5595 : 4 : snprintf(buf, sizeof buf, "$dret%d", dummy_result_count);
5596 : 4 : ++dummy_result_count;
5597 : 4 : name = gogo->pack_hidden_name(buf, false);
5598 : 4 : no = block->bindings()->add_result_variable(name, result);
5599 : 4 : go_assert(no->is_result_variable());
5600 : 4 : this->results_->push_back(no);
5601 : : }
5602 : 244719 : }
5603 : : }
5604 : :
5605 : : // Update the named result variables when cloning a function which
5606 : : // calls recover.
5607 : :
5608 : : void
5609 : 1662 : Function::update_result_variables()
5610 : : {
5611 : 1662 : if (this->results_ == NULL)
5612 : : return;
5613 : :
5614 : 12 : for (Results::iterator p = this->results_->begin();
5615 : 12 : p != this->results_->end();
5616 : 6 : ++p)
5617 : 6 : (*p)->result_var_value()->set_function(this);
5618 : : }
5619 : :
5620 : : // Whether this method should not be included in the type descriptor.
5621 : :
5622 : : bool
5623 : 572082 : Function::nointerface() const
5624 : : {
5625 : 572082 : go_assert(this->is_method());
5626 : 572082 : return (this->pragmas_ & GOPRAGMA_NOINTERFACE) != 0;
5627 : : }
5628 : :
5629 : : // Record that this method should not be included in the type
5630 : : // descriptor.
5631 : :
5632 : : void
5633 : 2 : Function::set_nointerface()
5634 : : {
5635 : 2 : this->pragmas_ |= GOPRAGMA_NOINTERFACE;
5636 : 2 : }
5637 : :
5638 : : // Return the closure variable, creating it if necessary.
5639 : :
5640 : : Named_object*
5641 : 56715 : Function::closure_var()
5642 : : {
5643 : 56715 : if (this->closure_var_ == NULL)
5644 : : {
5645 : 13057 : go_assert(this->descriptor_ == NULL);
5646 : : // We don't know the type of the variable yet. We add fields as
5647 : : // we find them.
5648 : 13057 : Location loc = this->type_->location();
5649 : 13057 : Struct_field_list* sfl = new Struct_field_list;
5650 : 13057 : Struct_type* struct_type = Type::make_struct_type(sfl, loc);
5651 : 13057 : struct_type->set_is_struct_incomparable();
5652 : 13057 : Variable* var = new Variable(Type::make_pointer_type(struct_type),
5653 : 26114 : NULL, false, false, false, loc);
5654 : 13057 : var->set_is_used();
5655 : 13057 : var->set_is_closure();
5656 : 13057 : this->closure_var_ = Named_object::make_variable("$closure", NULL, var);
5657 : : // Note that the new variable is not in any binding contour.
5658 : : }
5659 : 56715 : return this->closure_var_;
5660 : : }
5661 : :
5662 : : // Set the type of the closure variable.
5663 : :
5664 : : void
5665 : 175575 : Function::set_closure_type()
5666 : : {
5667 : 175575 : if (this->closure_var_ == NULL)
5668 : : return;
5669 : 13057 : Named_object* closure = this->closure_var_;
5670 : 26114 : Struct_type* st = closure->var_value()->type()->deref()->struct_type();
5671 : :
5672 : : // The first field of a closure is always a pointer to the function
5673 : : // code.
5674 : 13057 : Type* voidptr_type = Type::make_pointer_type(Type::make_void_type());
5675 : 26114 : st->push_field(Struct_field(Typed_identifier(".f", voidptr_type,
5676 : 13057 : this->location_)));
5677 : :
5678 : 13057 : unsigned int index = 1;
5679 : 13057 : for (Closure_fields::const_iterator p = this->closure_fields_.begin();
5680 : 38530 : p != this->closure_fields_.end();
5681 : 25473 : ++p, ++index)
5682 : : {
5683 : 25473 : Named_object* no = p->first;
5684 : 25473 : char buf[20];
5685 : 25473 : snprintf(buf, sizeof buf, "%u", index);
5686 : 25473 : std::string n = no->name() + buf;
5687 : 25473 : Type* var_type;
5688 : 25473 : if (no->is_variable())
5689 : 24646 : var_type = no->var_value()->type();
5690 : : else
5691 : 827 : var_type = no->result_var_value()->type();
5692 : 25473 : Type* field_type = Type::make_pointer_type(var_type);
5693 : 25473 : st->push_field(Struct_field(Typed_identifier(n, field_type, p->second)));
5694 : 25473 : }
5695 : : }
5696 : :
5697 : : // Return whether this function is a method.
5698 : :
5699 : : bool
5700 : 1802626 : Function::is_method() const
5701 : : {
5702 : 1802626 : return this->type_->is_method();
5703 : : }
5704 : :
5705 : : // Add a label definition.
5706 : :
5707 : : Label*
5708 : 10526 : Function::add_label_definition(Gogo* gogo, const std::string& label_name,
5709 : : Location location)
5710 : : {
5711 : 10526 : Label* lnull = NULL;
5712 : 10526 : std::pair<Labels::iterator, bool> ins =
5713 : 10526 : this->labels_.insert(std::make_pair(label_name, lnull));
5714 : 10526 : Label* label;
5715 : 10526 : if (label_name == "_")
5716 : : {
5717 : 3 : label = Label::create_dummy_label();
5718 : 3 : if (ins.second)
5719 : 2 : ins.first->second = label;
5720 : : }
5721 : 10523 : else if (ins.second)
5722 : : {
5723 : : // This is a new label.
5724 : 1126 : label = new Label(label_name);
5725 : 1126 : ins.first->second = label;
5726 : : }
5727 : : else
5728 : : {
5729 : : // The label was already in the hash table.
5730 : 9397 : label = ins.first->second;
5731 : 9397 : if (label->is_defined())
5732 : : {
5733 : 2 : go_error_at(location, "label %qs already defined",
5734 : 2 : Gogo::message_name(label_name).c_str());
5735 : 2 : go_inform(label->location(), "previous definition of %qs was here",
5736 : 2 : Gogo::message_name(label_name).c_str());
5737 : 2 : return new Label(label_name);
5738 : : }
5739 : : }
5740 : :
5741 : 10524 : label->define(location, gogo->bindings_snapshot(location));
5742 : :
5743 : : // Issue any errors appropriate for any previous goto's to this
5744 : : // label.
5745 : 10524 : const std::vector<Bindings_snapshot*>& refs(label->refs());
5746 : 12089 : for (std::vector<Bindings_snapshot*>::const_iterator p = refs.begin();
5747 : 12089 : p != refs.end();
5748 : 1565 : ++p)
5749 : 1565 : (*p)->check_goto_to(gogo->current_block());
5750 : 10524 : label->clear_refs();
5751 : :
5752 : 10524 : return label;
5753 : : }
5754 : :
5755 : : // Add a reference to a label.
5756 : :
5757 : : Label*
5758 : 10959 : Function::add_label_reference(Gogo* gogo, const std::string& label_name,
5759 : : Location location, bool issue_goto_errors)
5760 : : {
5761 : 10959 : Label* lnull = NULL;
5762 : 10959 : std::pair<Labels::iterator, bool> ins =
5763 : 10959 : this->labels_.insert(std::make_pair(label_name, lnull));
5764 : 10959 : Label* label;
5765 : 10959 : if (!ins.second)
5766 : : {
5767 : : // The label was already in the hash table.
5768 : 1559 : label = ins.first->second;
5769 : : }
5770 : : else
5771 : : {
5772 : 9400 : go_assert(ins.first->second == NULL);
5773 : 9400 : label = new Label(label_name);
5774 : 9400 : ins.first->second = label;
5775 : : }
5776 : :
5777 : 10959 : label->set_is_used();
5778 : :
5779 : 10959 : if (issue_goto_errors)
5780 : : {
5781 : 2101 : Bindings_snapshot* snapshot = label->snapshot();
5782 : 2101 : if (snapshot != NULL)
5783 : 533 : snapshot->check_goto_from(gogo->current_block(), location);
5784 : : else
5785 : 1568 : label->add_snapshot_ref(gogo->bindings_snapshot(location));
5786 : : }
5787 : :
5788 : 10959 : return label;
5789 : : }
5790 : :
5791 : : // Warn about labels that are defined but not used.
5792 : :
5793 : : void
5794 : 175575 : Function::check_labels() const
5795 : : {
5796 : 177262 : for (Labels::const_iterator p = this->labels_.begin();
5797 : 177262 : p != this->labels_.end();
5798 : 1687 : p++)
5799 : : {
5800 : 1687 : Label* label = p->second;
5801 : 1687 : if (!label->is_used())
5802 : 7 : go_error_at(label->location(), "label %qs defined and not used",
5803 : 14 : Gogo::message_name(label->name()).c_str());
5804 : : }
5805 : 175575 : }
5806 : :
5807 : : // Set the receiver type. This is used to remove aliases.
5808 : :
5809 : : void
5810 : 2 : Function::set_receiver_type(Type* rtype)
5811 : : {
5812 : 2 : Function_type* oft = this->type_;
5813 : 2 : Typed_identifier* rec = new Typed_identifier(oft->receiver()->name(),
5814 : : rtype,
5815 : 2 : oft->receiver()->location());
5816 : 2 : Typed_identifier_list* parameters = NULL;
5817 : 2 : if (oft->parameters() != NULL)
5818 : 0 : parameters = oft->parameters()->copy();
5819 : 2 : Typed_identifier_list* results = NULL;
5820 : 2 : if (oft->results() != NULL)
5821 : 0 : results = oft->results()->copy();
5822 : 2 : Function_type* nft = Type::make_function_type(rec, parameters, results,
5823 : : oft->location());
5824 : 2 : this->type_ = nft;
5825 : 2 : }
5826 : :
5827 : : // Swap one function with another. This is used when building the
5828 : : // thunk we use to call a function which calls recover. It may not
5829 : : // work for any other case.
5830 : :
5831 : : void
5832 : 831 : Function::swap_for_recover(Function *x)
5833 : : {
5834 : 831 : go_assert(this->enclosing_ == x->enclosing_);
5835 : 831 : std::swap(this->results_, x->results_);
5836 : 831 : std::swap(this->closure_var_, x->closure_var_);
5837 : 831 : std::swap(this->block_, x->block_);
5838 : 831 : go_assert(this->location_ == x->location_);
5839 : 831 : go_assert(this->fndecl_ == NULL && x->fndecl_ == NULL);
5840 : 831 : go_assert(this->defer_stack_ == NULL && x->defer_stack_ == NULL);
5841 : 831 : }
5842 : :
5843 : : // Traverse the tree.
5844 : :
5845 : : int
5846 : 4842947 : Function::traverse(Traverse* traverse)
5847 : : {
5848 : 4842947 : unsigned int traverse_mask = traverse->traverse_mask();
5849 : :
5850 : 4842947 : if ((traverse_mask
5851 : 4842947 : & (Traverse::traverse_types | Traverse::traverse_expressions))
5852 : : != 0)
5853 : : {
5854 : 3031646 : if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
5855 : : return TRAVERSE_EXIT;
5856 : : }
5857 : :
5858 : : // FIXME: We should check traverse_functions here if nested
5859 : : // functions are stored in block bindings.
5860 : 4842947 : if (this->block_ != NULL
5861 : 4842947 : && (traverse_mask
5862 : 4842947 : & (Traverse::traverse_variables
5863 : : | Traverse::traverse_constants
5864 : : | Traverse::traverse_blocks
5865 : : | Traverse::traverse_statements
5866 : : | Traverse::traverse_expressions
5867 : : | Traverse::traverse_types)) != 0)
5868 : : {
5869 : 4274614 : if (this->block_->traverse(traverse) == TRAVERSE_EXIT)
5870 : : return TRAVERSE_EXIT;
5871 : : }
5872 : :
5873 : : return TRAVERSE_CONTINUE;
5874 : : }
5875 : :
5876 : : // Work out types for unspecified variables and constants.
5877 : :
5878 : : void
5879 : 175575 : Function::determine_types(Gogo* gogo)
5880 : : {
5881 : 175575 : this->set_closure_type();
5882 : 175575 : if (this->block_ != NULL)
5883 : 175575 : this->block_->determine_types(gogo);
5884 : 175575 : }
5885 : :
5886 : : // Return the function descriptor, the value you get when you refer to
5887 : : // the function in Go code without calling it.
5888 : :
5889 : : Expression*
5890 : 194779 : Function::descriptor(Gogo*, Named_object* no)
5891 : : {
5892 : 194779 : go_assert(!this->is_method());
5893 : 194779 : go_assert(this->closure_var_ == NULL);
5894 : 194779 : if (this->descriptor_ == NULL)
5895 : 154462 : this->descriptor_ = Expression::make_func_descriptor(no);
5896 : 194779 : return this->descriptor_;
5897 : : }
5898 : :
5899 : : // Get a pointer to the variable representing the defer stack for this
5900 : : // function, making it if necessary. The value of the variable is set
5901 : : // by the runtime routines to true if the function is returning,
5902 : : // rather than panicing through. A pointer to this variable is used
5903 : : // as a marker for the functions on the defer stack associated with
5904 : : // this function. A function-specific variable permits inlining a
5905 : : // function which uses defer.
5906 : :
5907 : : Expression*
5908 : 47518 : Function::defer_stack(Location location)
5909 : : {
5910 : 47518 : if (this->defer_stack_ == NULL)
5911 : : {
5912 : 8354 : Type* t = Type::lookup_bool_type();
5913 : 8354 : Expression* n = Expression::make_boolean(false, location);
5914 : 8354 : this->defer_stack_ = Statement::make_temporary(t, n, location);
5915 : 8354 : this->defer_stack_->set_is_address_taken();
5916 : : }
5917 : 47518 : Expression* ref = Expression::make_temporary_reference(this->defer_stack_,
5918 : : location);
5919 : 47518 : return Expression::make_unary(OPERATOR_AND, ref, location);
5920 : : }
5921 : :
5922 : : // Export the function.
5923 : :
5924 : : void
5925 : 79191 : Function::export_func(Export* exp, const Named_object* no) const
5926 : : {
5927 : 79191 : Block* block = NULL;
5928 : 79191 : if (this->export_for_inlining())
5929 : 16707 : block = this->block_;
5930 : 158382 : Function::export_func_with_type(exp, no, this->type_, this->results_,
5931 : 79191 : this->is_method() && this->nointerface(),
5932 : : this->asm_name(), block, this->location_);
5933 : 79191 : }
5934 : :
5935 : : // Export a function with a type.
5936 : :
5937 : : void
5938 : 380525 : Function::export_func_with_type(Export* exp, const Named_object* no,
5939 : : const Function_type* fntype,
5940 : : Function::Results* result_vars,
5941 : : bool nointerface, const std::string& asm_name,
5942 : : Block* block, Location loc)
5943 : : {
5944 : 380525 : exp->write_c_string("func ");
5945 : :
5946 : 380525 : if (nointerface)
5947 : : {
5948 : 2 : go_assert(fntype->is_method());
5949 : 2 : exp->write_c_string("/*nointerface*/ ");
5950 : : }
5951 : :
5952 : 380525 : if (!asm_name.empty())
5953 : : {
5954 : 1943 : exp->write_c_string("/*asm ");
5955 : 1943 : exp->write_string(asm_name);
5956 : 1943 : exp->write_c_string(" */ ");
5957 : : }
5958 : :
5959 : 380525 : if (fntype->is_method())
5960 : : {
5961 : 332259 : exp->write_c_string("(");
5962 : 332259 : const Typed_identifier* receiver = fntype->receiver();
5963 : 332259 : exp->write_name(receiver->name());
5964 : 332259 : exp->write_escape(receiver->note());
5965 : 332259 : exp->write_c_string(" ");
5966 : 332259 : exp->write_type(receiver->type()->unalias());
5967 : 332259 : exp->write_c_string(") ");
5968 : : }
5969 : :
5970 : 380525 : if (no->package() != NULL && !fntype->is_method())
5971 : : {
5972 : 12519 : char buf[50];
5973 : 12519 : snprintf(buf, sizeof buf, "<p%d>", exp->package_index(no->package()));
5974 : 12519 : exp->write_c_string(buf);
5975 : : }
5976 : :
5977 : 380525 : const std::string& name(no->name());
5978 : 380525 : if (!Gogo::is_hidden_name(name))
5979 : 266401 : exp->write_string(name);
5980 : : else
5981 : : {
5982 : 114124 : exp->write_c_string(".");
5983 : 114124 : exp->write_string(Gogo::unpack_hidden_name(name));
5984 : : }
5985 : :
5986 : 380525 : exp->write_c_string(" (");
5987 : 380525 : const Typed_identifier_list* parameters = fntype->parameters();
5988 : 380525 : if (parameters != NULL)
5989 : : {
5990 : 196976 : size_t i = 0;
5991 : 196976 : bool is_varargs = fntype->is_varargs();
5992 : 196976 : bool first = true;
5993 : 196976 : for (Typed_identifier_list::const_iterator p = parameters->begin();
5994 : 502780 : p != parameters->end();
5995 : 305804 : ++p, ++i)
5996 : : {
5997 : 305804 : if (first)
5998 : : first = false;
5999 : : else
6000 : 108828 : exp->write_c_string(", ");
6001 : 305804 : exp->write_name(p->name());
6002 : 305804 : exp->write_escape(p->note());
6003 : 305804 : exp->write_c_string(" ");
6004 : 305804 : if (!is_varargs || p + 1 != parameters->end())
6005 : 297156 : exp->write_type(p->type());
6006 : : else
6007 : : {
6008 : 8648 : exp->write_c_string("...");
6009 : 17296 : exp->write_type(p->type()->array_type()->element_type());
6010 : : }
6011 : : }
6012 : : }
6013 : 380525 : exp->write_c_string(")");
6014 : :
6015 : 380525 : const Typed_identifier_list* result_decls = fntype->results();
6016 : 380525 : if (result_decls != NULL)
6017 : : {
6018 : 285501 : if (result_decls->size() == 1
6019 : 238548 : && result_decls->begin()->name().empty()
6020 : 495866 : && block == NULL)
6021 : : {
6022 : 202162 : exp->write_c_string(" ");
6023 : 202162 : exp->write_type(result_decls->begin()->type());
6024 : : }
6025 : : else
6026 : : {
6027 : 83339 : exp->write_c_string(" (");
6028 : 83339 : bool first = true;
6029 : 83339 : Results::const_iterator pr;
6030 : 83339 : if (result_vars != NULL)
6031 : 23063 : pr = result_vars->begin();
6032 : 228546 : for (Typed_identifier_list::const_iterator pd = result_decls->begin();
6033 : 228546 : pd != result_decls->end();
6034 : 145207 : ++pd)
6035 : : {
6036 : 145207 : if (first)
6037 : : first = false;
6038 : : else
6039 : 61868 : exp->write_c_string(", ");
6040 : : // We only use pr->name, which may be artificial, if
6041 : : // need it for inlining.
6042 : 145207 : if (block == NULL || result_vars == NULL)
6043 : 131610 : exp->write_name(pd->name());
6044 : : else
6045 : 13597 : exp->write_name((*pr)->name());
6046 : 145207 : exp->write_escape(pd->note());
6047 : 145207 : exp->write_c_string(" ");
6048 : 145207 : exp->write_type(pd->type());
6049 : 145207 : if (result_vars != NULL)
6050 : 34890 : ++pr;
6051 : : }
6052 : 83339 : if (result_vars != NULL)
6053 : 23063 : go_assert(pr == result_vars->end());
6054 : 83339 : exp->write_c_string(")");
6055 : : }
6056 : : }
6057 : :
6058 : 380525 : if (block == NULL)
6059 : 363818 : exp->write_c_string("\n");
6060 : : else
6061 : : {
6062 : 16707 : int indent = 1;
6063 : 16707 : if (fntype->is_method())
6064 : 7254 : indent++;
6065 : :
6066 : 16707 : Export_function_body efb(exp, indent);
6067 : :
6068 : 16707 : efb.indent();
6069 : 16707 : efb.write_c_string("// ");
6070 : 16707 : efb.write_string(Linemap::location_to_file(block->start_location()));
6071 : 16707 : efb.write_char(':');
6072 : 16707 : char buf[100];
6073 : 16707 : snprintf(buf, sizeof buf, "%d", Linemap::location_to_line(loc));
6074 : 16707 : efb.write_c_string(buf);
6075 : 16707 : efb.write_char('\n');
6076 : 16707 : block->export_block(&efb);
6077 : :
6078 : 16707 : const std::string& body(efb.body());
6079 : :
6080 : 16707 : snprintf(buf, sizeof buf, " <inl:%lu>\n",
6081 : 16707 : static_cast<unsigned long>(body.length()));
6082 : 16707 : exp->write_c_string(buf);
6083 : :
6084 : 16707 : exp->write_string(body);
6085 : 16707 : }
6086 : 380525 : }
6087 : :
6088 : : // Import a function.
6089 : :
6090 : : bool
6091 : 3055849 : Function::import_func(Import* imp, std::string* pname,
6092 : : Package** ppkg, bool* pis_exported,
6093 : : Typed_identifier** preceiver,
6094 : : Typed_identifier_list** pparameters,
6095 : : Typed_identifier_list** presults,
6096 : : bool* is_varargs,
6097 : : bool* nointerface,
6098 : : std::string* asm_name,
6099 : : std::string* body)
6100 : : {
6101 : 3055849 : imp->require_c_string("func ");
6102 : :
6103 : 3055849 : *nointerface = false;
6104 : 3168045 : while (imp->match_c_string("/*"))
6105 : : {
6106 : 112196 : imp->advance(2);
6107 : 112196 : if (imp->match_c_string("nointerface"))
6108 : : {
6109 : 2 : imp->require_c_string("nointerface*/ ");
6110 : 2 : *nointerface = true;
6111 : : }
6112 : 112194 : else if (imp->match_c_string("asm"))
6113 : : {
6114 : 112194 : imp->require_c_string("asm ");
6115 : 112194 : *asm_name = imp->read_identifier();
6116 : 112194 : imp->require_c_string(" */ ");
6117 : : }
6118 : : else
6119 : : {
6120 : 0 : go_error_at(imp->location(),
6121 : : "import error at %d: unrecognized function comment",
6122 : : imp->pos());
6123 : 0 : return false;
6124 : : }
6125 : : }
6126 : :
6127 : 3055849 : if (*nointerface)
6128 : : {
6129 : : // Only a method can be nointerface.
6130 : 2 : go_assert(imp->peek_char() == '(');
6131 : : }
6132 : :
6133 : 3055849 : *preceiver = NULL;
6134 : 3055849 : if (imp->peek_char() == '(')
6135 : : {
6136 : 1784464 : imp->require_c_string("(");
6137 : 1784464 : std::string name = imp->read_name();
6138 : 1784464 : std::string escape_note = imp->read_escape();
6139 : 1784464 : imp->require_c_string(" ");
6140 : 1784464 : Type* rtype = imp->read_type();
6141 : 1784464 : *preceiver = new Typed_identifier(name, rtype, imp->location());
6142 : 1784464 : (*preceiver)->set_note(escape_note);
6143 : 1784464 : imp->require_c_string(") ");
6144 : 1784464 : }
6145 : :
6146 : 3055849 : if (!Import::read_qualified_identifier(imp, pname, ppkg, pis_exported))
6147 : : {
6148 : 0 : go_error_at(imp->location(),
6149 : : "import error at %d: bad function name in export data",
6150 : : imp->pos());
6151 : 0 : return false;
6152 : : }
6153 : :
6154 : 3055849 : Typed_identifier_list* parameters;
6155 : 3055849 : *is_varargs = false;
6156 : 3055849 : imp->require_c_string(" (");
6157 : 3055849 : if (imp->peek_char() == ')')
6158 : : parameters = NULL;
6159 : : else
6160 : : {
6161 : 1898752 : parameters = new Typed_identifier_list();
6162 : 4524174 : while (true)
6163 : : {
6164 : 3211463 : std::string name = imp->read_name();
6165 : 3211463 : std::string escape_note = imp->read_escape();
6166 : 3211463 : imp->require_c_string(" ");
6167 : :
6168 : 3211463 : if (imp->match_c_string("..."))
6169 : : {
6170 : 52697 : imp->advance(3);
6171 : 52697 : *is_varargs = true;
6172 : : }
6173 : :
6174 : 3211463 : Type* ptype = imp->read_type();
6175 : 3211463 : if (*is_varargs)
6176 : 52697 : ptype = Type::make_array_type(ptype, NULL);
6177 : 3211463 : Typed_identifier t = Typed_identifier(name, ptype, imp->location());
6178 : 3211463 : t.set_note(escape_note);
6179 : 3211463 : parameters->push_back(t);
6180 : 3211463 : if (imp->peek_char() != ',')
6181 : : break;
6182 : 1312711 : go_assert(!*is_varargs);
6183 : 1312711 : imp->require_c_string(", ");
6184 : 3211463 : }
6185 : : }
6186 : 3055849 : imp->require_c_string(")");
6187 : 3055849 : *pparameters = parameters;
6188 : :
6189 : 3055849 : Typed_identifier_list* results;
6190 : 3055849 : if (imp->peek_char() != ' ' || imp->match_c_string(" <inl"))
6191 : : results = NULL;
6192 : : else
6193 : : {
6194 : 2247984 : results = new Typed_identifier_list();
6195 : 2247984 : imp->require_c_string(" ");
6196 : 2247984 : if (imp->peek_char() != '(')
6197 : : {
6198 : 1422320 : Type* rtype = imp->read_type();
6199 : 1422320 : results->push_back(Typed_identifier("", rtype, imp->location()));
6200 : : }
6201 : : else
6202 : : {
6203 : 825664 : imp->require_c_string("(");
6204 : 1762970 : while (true)
6205 : : {
6206 : 1294317 : std::string name = imp->read_name();
6207 : 1294317 : std::string note = imp->read_escape();
6208 : 1294317 : imp->require_c_string(" ");
6209 : 1294317 : Type* rtype = imp->read_type();
6210 : 1294317 : Typed_identifier t = Typed_identifier(name, rtype,
6211 : 1294317 : imp->location());
6212 : 1294317 : t.set_note(note);
6213 : 1294317 : results->push_back(t);
6214 : 1294317 : if (imp->peek_char() != ',')
6215 : : break;
6216 : 468653 : imp->require_c_string(", ");
6217 : 1294317 : }
6218 : 825664 : imp->require_c_string(")");
6219 : : }
6220 : : }
6221 : 3055849 : *presults = results;
6222 : :
6223 : 3055849 : if (!imp->match_c_string(" <inl:"))
6224 : : {
6225 : 2549164 : imp->require_semicolon_if_old_version();
6226 : 2549164 : imp->require_c_string("\n");
6227 : 2549164 : body->clear();
6228 : : }
6229 : : else
6230 : : {
6231 : 506685 : imp->require_c_string(" <inl:");
6232 : 506685 : std::string lenstr;
6233 : 1989446 : int c;
6234 : 3472207 : while (true)
6235 : : {
6236 : 1989446 : c = imp->peek_char();
6237 : 1989446 : if (c < '0' || c > '9')
6238 : : break;
6239 : 1482761 : lenstr += c;
6240 : 1482761 : imp->get_char();
6241 : : }
6242 : 506685 : imp->require_c_string(">\n");
6243 : :
6244 : 506685 : errno = 0;
6245 : 506685 : char* end;
6246 : 506685 : long llen = strtol(lenstr.c_str(), &end, 10);
6247 : 506685 : if (*end != '\0'
6248 : 506685 : || llen < 0
6249 : 506685 : || (llen == LONG_MAX && errno == ERANGE))
6250 : : {
6251 : 0 : go_error_at(imp->location(), "invalid inline function length %s",
6252 : : lenstr.c_str());
6253 : 0 : return false;
6254 : : }
6255 : :
6256 : 506685 : imp->read(static_cast<size_t>(llen), body);
6257 : 506685 : }
6258 : :
6259 : : return true;
6260 : : }
6261 : :
6262 : : // Get the backend name.
6263 : :
6264 : : void
6265 : 453227 : Function::backend_name(Gogo* gogo, Named_object* no, Backend_name *bname)
6266 : : {
6267 : 453227 : if (!this->asm_name_.empty())
6268 : 186274 : bname->set_asm_name(this->asm_name_);
6269 : 266953 : else if (no->package() == NULL && no->name() == gogo->get_init_fn_name())
6270 : : {
6271 : : // These names appear in the export data and are used
6272 : : // directly in the assembler code. If we change this here
6273 : : // we need to change Gogo::init_imports.
6274 : 3453 : bname->set_asm_name(no->name());
6275 : : }
6276 : 263500 : else if (this->enclosing_ != NULL)
6277 : : {
6278 : : // Rewrite the nested name to use the enclosing function name.
6279 : : // We don't do this earlier because we just store simple names
6280 : : // in a Named_object, not Backend_names.
6281 : :
6282 : : // The name was set by nested_function_name, which always
6283 : : // appends ..funcNNN. We want that to be our suffix.
6284 : 35810 : size_t pos = no->name().find("..func");
6285 : 35810 : go_assert(pos != std::string::npos);
6286 : :
6287 : 35810 : Named_object* enclosing = this->enclosing_;
6288 : 39375 : while (true)
6289 : : {
6290 : 39375 : Named_object* parent = enclosing->func_value()->enclosing();
6291 : 39375 : if (parent == NULL)
6292 : : break;
6293 : : enclosing = parent;
6294 : : }
6295 : :
6296 : 35810 : Type* rtype = NULL;
6297 : 35810 : if (enclosing->func_value()->type()->is_method())
6298 : 4021 : rtype = enclosing->func_value()->type()->receiver()->type();
6299 : 35810 : gogo->function_backend_name(enclosing->name(), enclosing->package(),
6300 : : rtype, bname);
6301 : 35810 : bname->append_suffix(no->name().substr(pos));
6302 : : }
6303 : : else
6304 : : {
6305 : 227690 : Type* rtype = NULL;
6306 : 227690 : if (this->type_->is_method())
6307 : 78028 : rtype = this->type_->receiver()->type();
6308 : 227690 : gogo->function_backend_name(no->name(), no->package(), rtype, bname);
6309 : : }
6310 : 453227 : }
6311 : :
6312 : : // Get the backend representation.
6313 : :
6314 : : Bfunction*
6315 : 3814539 : Function::get_or_make_decl(Gogo* gogo, Named_object* no)
6316 : : {
6317 : 3814539 : if (this->fndecl_ == NULL)
6318 : : {
6319 : 298769 : unsigned int flags = 0;
6320 : 298769 : if (no->package() != NULL)
6321 : : {
6322 : : // Functions defined in other packages must be visible.
6323 : : flags |= Backend::function_is_visible;
6324 : : }
6325 : 286522 : else if (this->enclosing_ != NULL || Gogo::is_thunk(no))
6326 : : ;
6327 : 246530 : else if (Gogo::unpack_hidden_name(no->name()) == "init"
6328 : 246530 : && !this->type_->is_method())
6329 : : ;
6330 : 246530 : else if (no->name() == gogo->get_init_fn_name())
6331 : : flags |= Backend::function_is_visible;
6332 : 243077 : else if (Gogo::unpack_hidden_name(no->name()) == "main"
6333 : 243077 : && gogo->is_main_package())
6334 : : flags |= Backend::function_is_visible;
6335 : : // Methods have to be public even if they are hidden because
6336 : : // they can be pulled into type descriptors when using
6337 : : // anonymous fields.
6338 : 241313 : else if (!Gogo::is_hidden_name(no->name())
6339 : 241313 : || this->type_->is_method())
6340 : : {
6341 : 201538 : if (!this->is_unnamed_type_stub_method_)
6342 : 208938 : flags |= Backend::function_is_visible;
6343 : : }
6344 : :
6345 : 298769 : if (!this->asm_name_.empty())
6346 : : {
6347 : : // If an assembler name is explicitly specified, there must
6348 : : // be some reason to refer to the symbol from a different
6349 : : // object file.
6350 : 93600 : flags |= Backend::function_is_visible;
6351 : : }
6352 : :
6353 : : // If an inline body refers to this function, then it
6354 : : // needs to be visible in the symbol table.
6355 : 298769 : if (this->is_referenced_by_inline_)
6356 : 8157 : flags |= Backend::function_is_visible;
6357 : :
6358 : : // A go:linkname directive can be used to force a function to be
6359 : : // visible.
6360 : 298769 : if (this->is_exported_by_linkname_)
6361 : 1673 : flags |= Backend::function_is_visible;
6362 : :
6363 : : // If a function calls the predeclared recover function, we
6364 : : // can't inline it, because recover behaves differently in a
6365 : : // function passed directly to defer. If this is a recover
6366 : : // thunk that we built to test whether a function can be
6367 : : // recovered, we can't inline it, because that will mess up
6368 : : // our return address comparison.
6369 : 298769 : bool is_inlinable = !(this->calls_recover_ || this->is_recover_thunk_);
6370 : :
6371 : : // If a function calls __go_set_defer_retaddr, then mark it as
6372 : : // uninlinable. This prevents the GCC backend from splitting
6373 : : // the function; splitting the function is a bad idea because we
6374 : : // want the return address label to be in the same function as
6375 : : // the call.
6376 : 298769 : if (this->calls_defer_retaddr_)
6377 : 8840 : is_inlinable = false;
6378 : :
6379 : : // Check the //go:noinline compiler directive.
6380 : 298769 : if ((this->pragmas_ & GOPRAGMA_NOINLINE) != 0)
6381 : : is_inlinable = false;
6382 : :
6383 : 298409 : if (is_inlinable)
6384 : 287906 : flags |= Backend::function_is_inlinable;
6385 : :
6386 : : // If this is a thunk created to call a function which calls
6387 : : // the predeclared recover function, we need to disable
6388 : : // stack splitting for the thunk.
6389 : 298769 : bool disable_split_stack = this->is_recover_thunk_;
6390 : :
6391 : : // Check the //go:nosplit compiler directive.
6392 : 298769 : if ((this->pragmas_ & GOPRAGMA_NOSPLIT) != 0)
6393 : : disable_split_stack = true;
6394 : :
6395 : 297335 : if (disable_split_stack)
6396 : 2264 : flags |= Backend::function_no_split_stack;
6397 : :
6398 : : // This should go into a unique section if that has been
6399 : : // requested elsewhere, or if this is a nointerface function.
6400 : : // We want to put a nointerface function into a unique section
6401 : : // because there is a good chance that the linker garbage
6402 : : // collection can discard it.
6403 : 298769 : if (this->in_unique_section_
6404 : 298769 : || (this->is_method() && this->nointerface()))
6405 : 3 : flags |= Backend::function_in_unique_section;
6406 : :
6407 : 298769 : if (this->is_inline_only_)
6408 : 12247 : flags |= Backend::function_only_inline;
6409 : :
6410 : 298769 : Btype* functype = this->type_->get_backend_fntype(gogo);
6411 : :
6412 : 298769 : Backend_name bname;
6413 : 298769 : this->backend_name(gogo, no, &bname);
6414 : :
6415 : 597538 : this->fndecl_ = gogo->backend()->function(functype,
6416 : 597538 : bname.name(),
6417 : 298769 : bname.optional_asm_name(),
6418 : : flags,
6419 : : this->location());
6420 : 298769 : }
6421 : 3814539 : return this->fndecl_;
6422 : : }
6423 : :
6424 : : // Get the backend name.
6425 : :
6426 : : void
6427 : 382131 : Function_declaration::backend_name(Gogo* gogo, Named_object* no,
6428 : : Backend_name* bname)
6429 : : {
6430 : 382131 : if (!this->asm_name_.empty())
6431 : 214278 : bname->set_asm_name(this->asm_name_);
6432 : : else
6433 : : {
6434 : 167853 : Type* rtype = NULL;
6435 : 167853 : if (this->fntype_->is_method())
6436 : 120044 : rtype = this->fntype_->receiver()->type();
6437 : 167853 : gogo->function_backend_name(no->name(), no->package(), rtype, bname);
6438 : : }
6439 : 382131 : }
6440 : :
6441 : : // Get the backend representation.
6442 : :
6443 : : Bfunction*
6444 : 1524849 : Function_declaration::get_or_make_decl(Gogo* gogo, Named_object* no)
6445 : : {
6446 : 1524849 : if (this->fndecl_ == NULL)
6447 : : {
6448 : 282892 : unsigned int flags =
6449 : : (Backend::function_is_visible
6450 : : | Backend::function_is_declaration
6451 : : | Backend::function_is_inlinable);
6452 : :
6453 : : // Let Go code use an asm declaration to pick up a builtin
6454 : : // function.
6455 : 282892 : if (!this->asm_name_.empty())
6456 : : {
6457 : 130067 : Bfunction* builtin_decl =
6458 : 130067 : gogo->backend()->lookup_builtin(this->asm_name_);
6459 : 130067 : if (builtin_decl != NULL)
6460 : : {
6461 : 5835 : this->fndecl_ = builtin_decl;
6462 : 5835 : return this->fndecl_;
6463 : : }
6464 : :
6465 : 124232 : if (this->asm_name_ == "runtime.gopanic"
6466 : 122082 : || this->asm_name_.compare(0, 13, "runtime.panic") == 0
6467 : 116978 : || this->asm_name_.compare(0, 15, "runtime.goPanic") == 0
6468 : 232393 : || this->asm_name_ == "runtime.block")
6469 : : flags |= Backend::function_does_not_return;
6470 : : }
6471 : :
6472 : 277057 : Btype* functype = this->fntype_->get_backend_fntype(gogo);
6473 : :
6474 : 277057 : Backend_name bname;
6475 : 277057 : this->backend_name(gogo, no, &bname);
6476 : :
6477 : 554114 : this->fndecl_ = gogo->backend()->function(functype,
6478 : 554114 : bname.name(),
6479 : 277057 : bname.optional_asm_name(),
6480 : : flags,
6481 : : this->location());
6482 : 277057 : }
6483 : :
6484 : 1519014 : return this->fndecl_;
6485 : : }
6486 : :
6487 : : // Build the descriptor for a function declaration. This won't
6488 : : // necessarily happen if the package has just a declaration for the
6489 : : // function and no other reference to it, but we may still need the
6490 : : // descriptor for references from other packages.
6491 : : void
6492 : 3904 : Function_declaration::build_backend_descriptor(Gogo* gogo)
6493 : : {
6494 : 3904 : if (this->descriptor_ != NULL)
6495 : : {
6496 : 650 : Translate_context context(gogo, NULL, NULL, NULL);
6497 : 650 : this->descriptor_->get_backend(&context);
6498 : : }
6499 : 3904 : }
6500 : :
6501 : : // Check that the types used in this declaration's signature are defined.
6502 : : // Reports errors for any undefined type.
6503 : :
6504 : : void
6505 : 3904 : Function_declaration::check_types() const
6506 : : {
6507 : : // Calling Type::base will give errors for any undefined types.
6508 : 3904 : Function_type* fntype = this->type();
6509 : 3904 : if (fntype->receiver() != NULL)
6510 : 0 : fntype->receiver()->type()->base();
6511 : 3904 : if (fntype->parameters() != NULL)
6512 : : {
6513 : 3035 : const Typed_identifier_list* params = fntype->parameters();
6514 : 9609 : for (Typed_identifier_list::const_iterator p = params->begin();
6515 : 9609 : p != params->end();
6516 : 6574 : ++p)
6517 : 6574 : p->type()->base();
6518 : : }
6519 : 3904 : }
6520 : :
6521 : : // Return the function's decl after it has been built.
6522 : :
6523 : : Bfunction*
6524 : 9317769 : Function::get_decl() const
6525 : : {
6526 : 9317769 : go_assert(this->fndecl_ != NULL);
6527 : 9317769 : return this->fndecl_;
6528 : : }
6529 : :
6530 : : // Build the backend representation for the function code.
6531 : :
6532 : : void
6533 : 295316 : Function::build(Gogo* gogo, Named_object* named_function)
6534 : : {
6535 : 295316 : Translate_context context(gogo, named_function, NULL, NULL);
6536 : :
6537 : : // A list of parameter variables for this function.
6538 : 295316 : std::vector<Bvariable*> param_vars;
6539 : :
6540 : : // Variables that need to be declared for this function and their
6541 : : // initial values.
6542 : 295316 : std::vector<Bvariable*> vars;
6543 : 295316 : std::vector<Expression*> var_inits;
6544 : 295316 : std::vector<Statement*> var_decls_stmts;
6545 : 1216035 : for (Bindings::const_definitions_iterator p =
6546 : 295316 : this->block_->bindings()->begin_definitions();
6547 : 1216035 : p != this->block_->bindings()->end_definitions();
6548 : 920719 : ++p)
6549 : : {
6550 : 920719 : Location loc = (*p)->location();
6551 : 920719 : if ((*p)->is_variable() && (*p)->var_value()->is_parameter())
6552 : : {
6553 : 473810 : Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
6554 : 473810 : Bvariable* parm_bvar = bvar;
6555 : :
6556 : : // We always pass the receiver to a method as a pointer. If
6557 : : // the receiver is declared as a non-pointer type, then we
6558 : : // copy the value into a local variable. For direct interface
6559 : : // type we pack the pointer into the type.
6560 : 473810 : if ((*p)->var_value()->is_receiver()
6561 : 473810 : && (*p)->var_value()->type()->points_to() == NULL)
6562 : : {
6563 : 15151 : std::string name = (*p)->name() + ".pointer";
6564 : 15151 : Type* var_type = (*p)->var_value()->type();
6565 : 15151 : Variable* parm_var =
6566 : 15151 : new Variable(Type::make_pointer_type(var_type), NULL, false,
6567 : 30302 : true, false, loc);
6568 : 15151 : Named_object* parm_no =
6569 : 15151 : Named_object::make_variable(name, NULL, parm_var);
6570 : 15151 : parm_bvar = parm_no->get_backend_variable(gogo, named_function);
6571 : :
6572 : 15151 : vars.push_back(bvar);
6573 : :
6574 : 15151 : Expression* parm_ref =
6575 : 15151 : Expression::make_var_reference(parm_no, loc);
6576 : 15151 : Type* recv_type = (*p)->var_value()->type();
6577 : 15151 : if (recv_type->is_direct_iface_type())
6578 : 2016 : parm_ref = Expression::pack_direct_iface(recv_type, parm_ref, loc);
6579 : : else
6580 : 13135 : parm_ref =
6581 : 13135 : Expression::make_dereference(parm_ref,
6582 : : Expression::NIL_CHECK_NEEDED,
6583 : : loc);
6584 : 15151 : if ((*p)->var_value()->is_in_heap())
6585 : 1222 : parm_ref = Expression::make_heap_expression(parm_ref, loc);
6586 : 15151 : var_inits.push_back(parm_ref);
6587 : 15151 : }
6588 : 458659 : else if ((*p)->var_value()->is_in_heap())
6589 : : {
6590 : : // If we take the address of a parameter, then we need
6591 : : // to copy it into the heap.
6592 : 3001 : std::string parm_name = (*p)->name() + ".param";
6593 : 6002 : Variable* parm_var = new Variable((*p)->var_value()->type(), NULL,
6594 : 3001 : false, true, false, loc);
6595 : 3001 : Named_object* parm_no =
6596 : 3001 : Named_object::make_variable(parm_name, NULL, parm_var);
6597 : 3001 : parm_bvar = parm_no->get_backend_variable(gogo, named_function);
6598 : :
6599 : 3001 : vars.push_back(bvar);
6600 : 3001 : Expression* var_ref =
6601 : 3001 : Expression::make_var_reference(parm_no, loc);
6602 : 3001 : var_ref = Expression::make_heap_expression(var_ref, loc);
6603 : 3001 : var_inits.push_back(var_ref);
6604 : 3001 : }
6605 : 473810 : param_vars.push_back(parm_bvar);
6606 : : }
6607 : 446909 : else if ((*p)->is_result_variable())
6608 : : {
6609 : 244562 : Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
6610 : :
6611 : 244562 : Type* type = (*p)->result_var_value()->type();
6612 : 244562 : Expression* init;
6613 : 244562 : if (!(*p)->result_var_value()->is_in_heap())
6614 : : {
6615 : 244440 : Btype* btype = type->get_backend(gogo);
6616 : 244440 : Bexpression* binit = gogo->backend()->zero_expression(btype);
6617 : 244440 : init = Expression::make_backend(binit, type, loc);
6618 : : }
6619 : : else
6620 : 122 : init = Expression::make_allocation(type, loc);
6621 : :
6622 : 244562 : vars.push_back(bvar);
6623 : 244562 : var_inits.push_back(init);
6624 : : }
6625 : 202347 : else if (this->defer_stack_ != NULL
6626 : 23528 : && (*p)->is_variable()
6627 : 22838 : && (*p)->var_value()->is_non_escaping_address_taken()
6628 : 946744 : && !(*p)->var_value()->is_in_heap())
6629 : : {
6630 : : // Local variable captured by deferred closure needs to be live
6631 : : // until the end of the function. We create a top-level
6632 : : // declaration for it.
6633 : : // TODO: we don't need to do this if the variable is not captured
6634 : : // by the defer closure. There is no easy way to check it here,
6635 : : // so we do this for all address-taken variables for now.
6636 : 1585 : Variable* var = (*p)->var_value();
6637 : 1585 : Temporary_statement* ts =
6638 : 1585 : Statement::make_temporary(var->type(), NULL, var->location());
6639 : 1585 : ts->set_is_address_taken();
6640 : 1585 : var->set_toplevel_decl(ts);
6641 : 1585 : var_decls_stmts.push_back(ts);
6642 : : }
6643 : : }
6644 : 295316 : if (!gogo->backend()->function_set_parameters(this->fndecl_, param_vars))
6645 : : {
6646 : 14 : go_assert(saw_errors());
6647 : : return;
6648 : : }
6649 : :
6650 : : // If we need a closure variable, make sure to create it.
6651 : : // It gets installed in the function as a side effect of creation.
6652 : 295302 : if (this->closure_var_ != NULL)
6653 : : {
6654 : 14291 : go_assert(this->closure_var_->var_value()->is_closure());
6655 : 14291 : this->closure_var_->get_backend_variable(gogo, named_function);
6656 : : }
6657 : :
6658 : 295302 : if (this->block_ != NULL)
6659 : : {
6660 : : // Declare variables if necessary.
6661 : 295302 : Bblock* var_decls = NULL;
6662 : 295302 : std::vector<Bstatement*> var_decls_bstmt_list;
6663 : 295302 : Bstatement* defer_init = NULL;
6664 : 295302 : if (!vars.empty() || this->defer_stack_ != NULL)
6665 : : {
6666 : 224719 : var_decls =
6667 : 449438 : gogo->backend()->block(this->fndecl_, NULL, vars,
6668 : 224719 : this->block_->start_location(),
6669 : 224719 : this->block_->end_location());
6670 : :
6671 : 224719 : if (this->defer_stack_ != NULL)
6672 : : {
6673 : 8353 : Translate_context dcontext(gogo, named_function, this->block_,
6674 : 8353 : var_decls);
6675 : 8353 : defer_init = this->defer_stack_->get_backend(&dcontext);
6676 : 8353 : var_decls_bstmt_list.push_back(defer_init);
6677 : 9938 : for (std::vector<Statement*>::iterator p = var_decls_stmts.begin();
6678 : 9938 : p != var_decls_stmts.end();
6679 : 1585 : ++p)
6680 : : {
6681 : 1585 : Bstatement* bstmt = (*p)->get_backend(&dcontext);
6682 : 1585 : var_decls_bstmt_list.push_back(bstmt);
6683 : : }
6684 : : }
6685 : : }
6686 : :
6687 : : // Build the backend representation for all the statements in the
6688 : : // function.
6689 : 295302 : Translate_context bcontext(gogo, named_function, NULL, NULL);
6690 : 295302 : Bblock* code_block = this->block_->get_backend(&bcontext);
6691 : :
6692 : : // Initialize variables if necessary.
6693 : 295302 : Translate_context icontext(gogo, named_function, this->block_,
6694 : 295302 : var_decls);
6695 : 295302 : std::vector<Bstatement*> init;
6696 : 295302 : go_assert(vars.size() == var_inits.size());
6697 : 558006 : for (size_t i = 0; i < vars.size(); ++i)
6698 : : {
6699 : 262704 : Bexpression* binit = var_inits[i]->get_backend(&icontext);
6700 : 262704 : Bstatement* init_stmt =
6701 : 262704 : gogo->backend()->init_statement(this->fndecl_, vars[i],
6702 : 262704 : binit);
6703 : 262704 : init.push_back(init_stmt);
6704 : : }
6705 : 295302 : Bstatement* var_init = gogo->backend()->statement_list(init);
6706 : :
6707 : : // Initialize all variables before executing this code block.
6708 : 295302 : Bstatement* code_stmt = gogo->backend()->block_statement(code_block);
6709 : 295302 : code_stmt = gogo->backend()->compound_statement(var_init, code_stmt);
6710 : :
6711 : : // If we have a defer stack, initialize it at the start of a
6712 : : // function.
6713 : 295302 : Bstatement* except = NULL;
6714 : 295302 : Bstatement* fini = NULL;
6715 : 295302 : if (defer_init != NULL)
6716 : : {
6717 : : // Clean up the defer stack when we leave the function.
6718 : 8353 : this->build_defer_wrapper(gogo, named_function, &except, &fini);
6719 : :
6720 : : // Wrap the code for this function in an exception handler to handle
6721 : : // defer calls.
6722 : 8353 : code_stmt =
6723 : 8353 : gogo->backend()->exception_handler_statement(code_stmt,
6724 : : except, fini,
6725 : : this->location_);
6726 : : }
6727 : :
6728 : : // Stick the code into the block we built for the receiver, if
6729 : : // we built one.
6730 : 295302 : if (var_decls != NULL)
6731 : : {
6732 : 224719 : var_decls_bstmt_list.push_back(code_stmt);
6733 : 224719 : gogo->backend()->block_add_statements(var_decls, var_decls_bstmt_list);
6734 : 224719 : code_stmt = gogo->backend()->block_statement(var_decls);
6735 : : }
6736 : :
6737 : 295302 : if (!gogo->backend()->function_set_body(this->fndecl_, code_stmt))
6738 : : {
6739 : 0 : go_assert(saw_errors());
6740 : 0 : return;
6741 : : }
6742 : 295302 : }
6743 : :
6744 : : // If we created a descriptor for the function, make sure we emit it.
6745 : 295302 : if (this->descriptor_ != NULL)
6746 : : {
6747 : 154458 : Translate_context dcontext(gogo, NULL, NULL, NULL);
6748 : 154458 : this->descriptor_->get_backend(&dcontext);
6749 : : }
6750 : 295316 : }
6751 : :
6752 : : // Build the wrappers around function code needed if the function has
6753 : : // any defer statements. This sets *EXCEPT to an exception handler
6754 : : // and *FINI to a finally handler.
6755 : :
6756 : : void
6757 : 8353 : Function::build_defer_wrapper(Gogo* gogo, Named_object* named_function,
6758 : : Bstatement** except, Bstatement** fini)
6759 : : {
6760 : 8353 : Location end_loc = this->block_->end_location();
6761 : :
6762 : : // Add an exception handler. This is used if a panic occurs. Its
6763 : : // purpose is to stop the stack unwinding if a deferred function
6764 : : // calls recover. There are more details in
6765 : : // libgo/runtime/go-unwind.c.
6766 : :
6767 : 8353 : std::vector<Bstatement*> stmts;
6768 : 8353 : Expression* call = Runtime::make_call(gogo, Runtime::CHECKDEFER, end_loc, 1,
6769 : : this->defer_stack(end_loc));
6770 : 8353 : Translate_context context(gogo, named_function, NULL, NULL);
6771 : 8353 : Bexpression* defer = call->get_backend(&context);
6772 : 8353 : stmts.push_back(gogo->backend()->expression_statement(this->fndecl_, defer));
6773 : :
6774 : 8353 : Bstatement* ret_bstmt = this->return_value(gogo, named_function, end_loc);
6775 : 8353 : if (ret_bstmt != NULL)
6776 : 3600 : stmts.push_back(ret_bstmt);
6777 : :
6778 : 8353 : go_assert(*except == NULL);
6779 : 8353 : *except = gogo->backend()->statement_list(stmts);
6780 : :
6781 : 8353 : call = Runtime::make_call(gogo, Runtime::CHECKDEFER, end_loc, 1,
6782 : : this->defer_stack(end_loc));
6783 : 8353 : defer = call->get_backend(&context);
6784 : :
6785 : 8353 : call = Runtime::make_call(gogo, Runtime::DEFERRETURN, end_loc, 1,
6786 : : this->defer_stack(end_loc));
6787 : 8353 : Bexpression* undefer = call->get_backend(&context);
6788 : 8353 : Bstatement* function_defer =
6789 : 8353 : gogo->backend()->function_defer_statement(this->fndecl_, undefer, defer,
6790 : 8353 : end_loc);
6791 : 8353 : stmts = std::vector<Bstatement*>(1, function_defer);
6792 : 8353 : if (this->type_->results() != NULL
6793 : 3600 : && !this->type_->results()->empty()
6794 : 11953 : && !this->type_->results()->front().name().empty())
6795 : : {
6796 : : // If the result variables are named, and we are returning from
6797 : : // this function rather than panicing through it, we need to
6798 : : // return them again, because they might have been changed by a
6799 : : // defer function. The runtime routines set the defer_stack
6800 : : // variable to true if we are returning from this function.
6801 : :
6802 : 1020 : ret_bstmt = this->return_value(gogo, named_function, end_loc);
6803 : 1020 : Bexpression* nil = Expression::make_nil(end_loc)->get_backend(&context);
6804 : 1020 : Bexpression* ret =
6805 : 1020 : gogo->backend()->compound_expression(ret_bstmt, nil, end_loc);
6806 : 1020 : Expression* ref =
6807 : 1020 : Expression::make_temporary_reference(this->defer_stack_, end_loc);
6808 : 1020 : Bexpression* bref = ref->get_backend(&context);
6809 : 1020 : ret = gogo->backend()->conditional_expression(this->fndecl_,
6810 : : NULL, bref, ret, NULL,
6811 : : end_loc);
6812 : 1020 : stmts.push_back(gogo->backend()->expression_statement(this->fndecl_, ret));
6813 : : }
6814 : :
6815 : 8353 : go_assert(*fini == NULL);
6816 : 8353 : *fini = gogo->backend()->statement_list(stmts);
6817 : 8353 : }
6818 : :
6819 : : // Return the statement that assigns values to this function's result struct.
6820 : :
6821 : : Bstatement*
6822 : 9373 : Function::return_value(Gogo* gogo, Named_object* named_function,
6823 : : Location location) const
6824 : : {
6825 : 9373 : const Typed_identifier_list* results = this->type_->results();
6826 : 9373 : if (results == NULL || results->empty())
6827 : : return NULL;
6828 : :
6829 : 4620 : go_assert(this->results_ != NULL);
6830 : 4620 : if (this->results_->size() != results->size())
6831 : : {
6832 : 0 : go_assert(saw_errors());
6833 : 0 : return gogo->backend()->error_statement();
6834 : : }
6835 : :
6836 : 4620 : std::vector<Bexpression*> vals(results->size());
6837 : 11463 : for (size_t i = 0; i < vals.size(); ++i)
6838 : : {
6839 : 6843 : Named_object* no = (*this->results_)[i];
6840 : 6843 : Bvariable* bvar = no->get_backend_variable(gogo, named_function);
6841 : 6843 : Bexpression* val = gogo->backend()->var_expression(bvar, location);
6842 : 6843 : if (no->result_var_value()->is_in_heap())
6843 : : {
6844 : 112 : Btype* bt = no->result_var_value()->type()->get_backend(gogo);
6845 : 112 : val = gogo->backend()->indirect_expression(bt, val, true, location);
6846 : : }
6847 : 6843 : vals[i] = val;
6848 : : }
6849 : 4620 : return gogo->backend()->return_statement(this->fndecl_, vals, location);
6850 : 4620 : }
6851 : :
6852 : : // Class Block.
6853 : :
6854 : 2715177 : Block::Block(Block* enclosing, Location location)
6855 : 2715177 : : enclosing_(enclosing), statements_(),
6856 : 2715177 : bindings_(new Bindings(enclosing == NULL
6857 : : ? NULL
6858 : 2715177 : : enclosing->bindings())),
6859 : 2715177 : start_location_(location),
6860 : 2715177 : end_location_(Linemap::unknown_location())
6861 : : {
6862 : 2715177 : }
6863 : :
6864 : : // Add a statement to a block.
6865 : :
6866 : : void
6867 : 5239555 : Block::add_statement(Statement* statement)
6868 : : {
6869 : 5239555 : this->statements_.push_back(statement);
6870 : 5239555 : }
6871 : :
6872 : : // Add a statement to the front of a block. This is slow but is only
6873 : : // used for reference counts of parameters.
6874 : :
6875 : : void
6876 : 4935 : Block::add_statement_at_front(Statement* statement)
6877 : : {
6878 : 4935 : this->statements_.insert(this->statements_.begin(), statement);
6879 : 4935 : }
6880 : :
6881 : : // Replace a statement in a block.
6882 : :
6883 : : void
6884 : 747673 : Block::replace_statement(size_t index, Statement* s)
6885 : : {
6886 : 747673 : go_assert(index < this->statements_.size());
6887 : 747673 : this->statements_[index] = s;
6888 : 747673 : }
6889 : :
6890 : : // Add a statement before another statement.
6891 : :
6892 : : void
6893 : 1792534 : Block::insert_statement_before(size_t index, Statement* s)
6894 : : {
6895 : 1792534 : go_assert(index < this->statements_.size());
6896 : 1792534 : this->statements_.insert(this->statements_.begin() + index, s);
6897 : 1792534 : }
6898 : :
6899 : : // Add a statement after another statement.
6900 : :
6901 : : void
6902 : 0 : Block::insert_statement_after(size_t index, Statement* s)
6903 : : {
6904 : 0 : go_assert(index < this->statements_.size());
6905 : 0 : this->statements_.insert(this->statements_.begin() + index + 1, s);
6906 : 0 : }
6907 : :
6908 : : // Traverse the tree.
6909 : :
6910 : : int
6911 : 43291566 : Block::traverse(Traverse* traverse)
6912 : : {
6913 : 43291566 : unsigned int traverse_mask = traverse->traverse_mask();
6914 : :
6915 : 43291566 : if ((traverse_mask & Traverse::traverse_blocks) != 0)
6916 : : {
6917 : 8957932 : int t = traverse->block(this);
6918 : 8957932 : if (t == TRAVERSE_EXIT)
6919 : : return TRAVERSE_EXIT;
6920 : 8957932 : else if (t == TRAVERSE_SKIP_COMPONENTS)
6921 : : return TRAVERSE_CONTINUE;
6922 : : }
6923 : :
6924 : 39911393 : if ((traverse_mask
6925 : 39911393 : & (Traverse::traverse_variables
6926 : : | Traverse::traverse_constants
6927 : : | Traverse::traverse_expressions
6928 : : | Traverse::traverse_types)) != 0)
6929 : : {
6930 : 22168126 : for (Bindings::const_definitions_iterator pb =
6931 : 34995885 : this->bindings_->begin_definitions();
6932 : 57164011 : pb != this->bindings_->end_definitions();
6933 : 22168126 : ++pb)
6934 : : {
6935 : 22767938 : if ((*pb)->traverse(traverse, false) == TRAVERSE_EXIT)
6936 : 3016289 : return TRAVERSE_EXIT;
6937 : : }
6938 : : }
6939 : :
6940 : : // No point in checking traverse_mask here--if we got here we always
6941 : : // want to walk the statements. The traversal can insert new
6942 : : // statements before or after the current statement. Inserting
6943 : : // statements before the current statement requires updating I via
6944 : : // the pointer; those statements will not be traversed. Any new
6945 : : // statements inserted after the current statement will be traversed
6946 : : // in their turn.
6947 : 130612114 : for (size_t i = 0; i < this->statements_.size(); ++i)
6948 : : {
6949 : 93717010 : if (this->statements_[i]->traverse(this, &i, traverse) == TRAVERSE_EXIT)
6950 : 2416477 : return TRAVERSE_EXIT;
6951 : : }
6952 : :
6953 : 36895104 : return TRAVERSE_CONTINUE;
6954 : : }
6955 : :
6956 : : // Work out types for unspecified variables and constants.
6957 : :
6958 : : void
6959 : 2102603 : Block::determine_types(Gogo* gogo)
6960 : : {
6961 : 842541 : for (Bindings::const_definitions_iterator pb =
6962 : 2102603 : this->bindings_->begin_definitions();
6963 : 2945144 : pb != this->bindings_->end_definitions();
6964 : 842541 : ++pb)
6965 : : {
6966 : 842541 : if ((*pb)->is_variable())
6967 : 692848 : (*pb)->var_value()->determine_type(gogo);
6968 : 149693 : else if ((*pb)->is_const())
6969 : 5531 : (*pb)->const_value()->determine_type(gogo);
6970 : : }
6971 : :
6972 : 5569533 : for (std::vector<Statement*>::const_iterator ps = this->statements_.begin();
6973 : 5569533 : ps != this->statements_.end();
6974 : 3466930 : ++ps)
6975 : 3466930 : (*ps)->determine_types(gogo);
6976 : 2102603 : }
6977 : :
6978 : : // Return true if the statements in this block may fall through.
6979 : :
6980 : : bool
6981 : 113775 : Block::may_fall_through() const
6982 : : {
6983 : 113775 : if (this->statements_.empty())
6984 : : return true;
6985 : 113732 : return this->statements_.back()->may_fall_through();
6986 : : }
6987 : :
6988 : : // Write export data for a block.
6989 : :
6990 : : void
6991 : 44123 : Block::export_block(Export_function_body* efb)
6992 : : {
6993 : 122751 : for (Block::iterator p = this->begin();
6994 : 122751 : p != this->end();
6995 : 78628 : ++p)
6996 : : {
6997 : 78628 : efb->indent();
6998 : :
6999 : 78628 : efb->increment_indent();
7000 : 78628 : (*p)->export_statement(efb);
7001 : 78628 : efb->decrement_indent();
7002 : :
7003 : 78628 : Location loc = (*p)->location();
7004 : 78628 : if ((*p)->is_block_statement())
7005 : : {
7006 : : // For a block we put the start location on the first brace
7007 : : // in Block_statement::do_export_statement. Here we put the
7008 : : // end location on the final brace.
7009 : 22817 : loc = (*p)->block_statement()->block()->end_location();
7010 : : }
7011 : 78628 : char buf[50];
7012 : 78628 : snprintf(buf, sizeof buf, " //%d\n", Linemap::location_to_line(loc));
7013 : 78628 : efb->write_c_string(buf);
7014 : : }
7015 : 44123 : }
7016 : :
7017 : : // Add exported block data to SET, reading from BODY starting at OFF.
7018 : : // Returns whether the import succeeded.
7019 : :
7020 : : bool
7021 : 38869 : Block::import_block(Block* set, Import_function_body *ifb, Location loc)
7022 : : {
7023 : 38869 : Location eloc = ifb->location();
7024 : 38869 : Location sloc = loc;
7025 : 38869 : const std::string& body(ifb->body());
7026 : 38869 : size_t off = ifb->off();
7027 : 108195 : while (off < body.length())
7028 : : {
7029 : 95948 : int indent = ifb->indent();
7030 : 95948 : if (off + indent >= body.length())
7031 : : {
7032 : 0 : go_error_at(eloc,
7033 : : "invalid export data for %qs: insufficient indentation",
7034 : 0 : ifb->name().c_str());
7035 : 0 : return false;
7036 : : }
7037 : 245244 : for (int i = 0; i < indent - 1; i++)
7038 : : {
7039 : 149296 : if (body[off + i] != ' ')
7040 : : {
7041 : 0 : go_error_at(eloc,
7042 : : "invalid export data for %qs: bad indentation",
7043 : 0 : ifb->name().c_str());
7044 : 0 : return false;
7045 : : }
7046 : : }
7047 : :
7048 : 95948 : bool at_end = false;
7049 : 95948 : if (body[off + indent - 1] == '}')
7050 : : at_end = true;
7051 : 69326 : else if (body[off + indent - 1] != ' ')
7052 : : {
7053 : 0 : go_error_at(eloc,
7054 : : "invalid export data for %qs: bad indentation",
7055 : 0 : ifb->name().c_str());
7056 : 0 : return false;
7057 : : }
7058 : :
7059 : 95948 : off += indent;
7060 : :
7061 : 95948 : size_t nl = body.find('\n', off);
7062 : 95948 : if (nl == std::string::npos)
7063 : : {
7064 : 0 : go_error_at(eloc, "invalid export data for %qs: missing newline",
7065 : 0 : ifb->name().c_str());
7066 : 0 : return false;
7067 : : }
7068 : :
7069 : 95948 : size_t lineno_pos = body.find(" //", off);
7070 : 95948 : if (lineno_pos == std::string::npos || lineno_pos >= nl)
7071 : : {
7072 : 0 : go_error_at(eloc, "invalid export data for %qs: missing line number",
7073 : 0 : ifb->name().c_str());
7074 : 0 : return false;
7075 : : }
7076 : :
7077 : 95948 : unsigned int lineno = 0;
7078 : 333045 : for (size_t i = lineno_pos + 3; i < nl; ++i)
7079 : : {
7080 : 237097 : char c = body[i];
7081 : 237097 : if (c < '0' || c > '9')
7082 : : {
7083 : 0 : go_error_at(loc,
7084 : : "invalid export data for %qs: invalid line number",
7085 : 0 : ifb->name().c_str());
7086 : 0 : return false;
7087 : : }
7088 : 237097 : lineno = lineno * 10 + c - '0';
7089 : : }
7090 : :
7091 : 95948 : ifb->gogo()->linemap()->start_line(lineno, 1);
7092 : 95948 : sloc = ifb->gogo()->linemap()->get_location(0);
7093 : :
7094 : 95948 : if (at_end)
7095 : : {
7096 : : // An if statement can have an "else" following the "}", in
7097 : : // which case we want to leave the offset where it is, just
7098 : : // after the "}". We don't get the block ending location
7099 : : // quite right for if statements.
7100 : 26622 : if (body.compare(off, 6, " else ") != 0)
7101 : 26486 : off = nl + 1;
7102 : : break;
7103 : : }
7104 : :
7105 : 69326 : ifb->set_off(off);
7106 : 69326 : Statement* s = Statement::import_statement(ifb, sloc);
7107 : 69326 : if (s == NULL)
7108 : : return false;
7109 : :
7110 : 69326 : set->add_statement(s);
7111 : :
7112 : 69326 : size_t at = ifb->off();
7113 : 69326 : if (at < nl + 1)
7114 : : off = nl + 1;
7115 : : else
7116 : : off = at;
7117 : : }
7118 : :
7119 : 38869 : ifb->set_off(off);
7120 : 38869 : set->set_end_location(sloc);
7121 : 38869 : return true;
7122 : : }
7123 : :
7124 : : // Convert a block to the backend representation.
7125 : :
7126 : : Bblock*
7127 : 2631206 : Block::get_backend(Translate_context* context)
7128 : : {
7129 : 2631206 : Gogo* gogo = context->gogo();
7130 : 2631206 : Named_object* function = context->function();
7131 : 2631206 : std::vector<Bvariable*> vars;
7132 : 2631206 : vars.reserve(this->bindings_->size_definitions());
7133 : 1107537 : for (Bindings::const_definitions_iterator pv =
7134 : 2631206 : this->bindings_->begin_definitions();
7135 : 3738743 : pv != this->bindings_->end_definitions();
7136 : 1107537 : ++pv)
7137 : : {
7138 : 1107537 : if ((*pv)->is_variable() && !(*pv)->var_value()->is_parameter())
7139 : 382504 : vars.push_back((*pv)->get_backend_variable(gogo, function));
7140 : : }
7141 : :
7142 : 2631206 : go_assert(function != NULL);
7143 : 2631206 : Bfunction* bfunction =
7144 : 2631206 : function->func_value()->get_or_make_decl(gogo, function);
7145 : 2631206 : Bblock* ret = context->backend()->block(bfunction, context->bblock(),
7146 : : vars, this->start_location_,
7147 : : this->end_location_);
7148 : :
7149 : 2631206 : Translate_context subcontext(gogo, function, this, ret);
7150 : 2631206 : std::vector<Bstatement*> bstatements;
7151 : 2631206 : bstatements.reserve(this->statements_.size());
7152 : 2631206 : for (std::vector<Statement*>::const_iterator p = this->statements_.begin();
7153 : 9647039 : p != this->statements_.end();
7154 : 7015833 : ++p)
7155 : 7015833 : bstatements.push_back((*p)->get_backend(&subcontext));
7156 : :
7157 : 2631206 : context->backend()->block_add_statements(ret, bstatements);
7158 : :
7159 : 2631206 : return ret;
7160 : 2631206 : }
7161 : :
7162 : : // Class Bindings_snapshot.
7163 : :
7164 : 12092 : Bindings_snapshot::Bindings_snapshot(const Block* b, Location location)
7165 : 12092 : : block_(b), counts_(), location_(location)
7166 : : {
7167 : 40730 : while (b != NULL)
7168 : : {
7169 : 28638 : this->counts_.push_back(b->bindings()->size_definitions());
7170 : 28638 : b = b->enclosing();
7171 : : }
7172 : 12092 : }
7173 : :
7174 : : // Report errors appropriate for a goto from B to this.
7175 : :
7176 : : void
7177 : 533 : Bindings_snapshot::check_goto_from(const Block* b, Location loc)
7178 : : {
7179 : 533 : size_t dummy;
7180 : 533 : if (!this->check_goto_block(loc, b, this->block_, &dummy))
7181 : 12 : return;
7182 : 521 : this->check_goto_defs(loc, this->block_,
7183 : 521 : this->block_->bindings()->size_definitions(),
7184 : 521 : this->counts_[0]);
7185 : : }
7186 : :
7187 : : // Report errors appropriate for a goto from this to B.
7188 : :
7189 : : void
7190 : 1565 : Bindings_snapshot::check_goto_to(const Block* b)
7191 : : {
7192 : 1565 : size_t index;
7193 : 1565 : if (!this->check_goto_block(this->location_, this->block_, b, &index))
7194 : 19 : return;
7195 : 1546 : this->check_goto_defs(this->location_, b, this->counts_[index],
7196 : : b->bindings()->size_definitions());
7197 : : }
7198 : :
7199 : : // Report errors appropriate for a goto at LOC from BFROM to BTO.
7200 : : // Return true if all is well, false if we reported an error. If this
7201 : : // returns true, it sets *PINDEX to the number of blocks BTO is above
7202 : : // BFROM.
7203 : :
7204 : : bool
7205 : 2098 : Bindings_snapshot::check_goto_block(Location loc, const Block* bfrom,
7206 : : const Block* bto, size_t* pindex)
7207 : : {
7208 : : // It is an error if BTO is not either BFROM or above BFROM.
7209 : 2098 : size_t index = 0;
7210 : 9845 : for (const Block* pb = bfrom; pb != bto; pb = pb->enclosing(), ++index)
7211 : : {
7212 : 7778 : if (pb == NULL)
7213 : : {
7214 : 31 : go_error_at(loc, "goto jumps into block");
7215 : 31 : go_inform(bto->start_location(), "goto target block starts here");
7216 : 31 : return false;
7217 : : }
7218 : : }
7219 : 2067 : *pindex = index;
7220 : 2067 : return true;
7221 : : }
7222 : :
7223 : : // Report errors appropriate for a goto at LOC ending at BLOCK, where
7224 : : // CFROM is the number of names defined at the point of the goto and
7225 : : // CTO is the number of names defined at the point of the label.
7226 : :
7227 : : void
7228 : 2067 : Bindings_snapshot::check_goto_defs(Location loc, const Block* block,
7229 : : size_t cfrom, size_t cto)
7230 : : {
7231 : 2067 : if (cfrom < cto)
7232 : : {
7233 : 14 : Bindings::const_definitions_iterator p =
7234 : 14 : block->bindings()->begin_definitions();
7235 : 31 : for (size_t i = 0; i < cfrom; ++i)
7236 : : {
7237 : 17 : go_assert(p != block->bindings()->end_definitions());
7238 : 17 : ++p;
7239 : : }
7240 : 14 : go_assert(p != block->bindings()->end_definitions());
7241 : :
7242 : 34 : for (; p != block->bindings()->end_definitions(); ++p)
7243 : : {
7244 : 20 : if ((*p)->is_variable())
7245 : : {
7246 : 6 : std::string n = (*p)->message_name();
7247 : 6 : go_error_at(loc, "goto jumps over declaration of %qs", n.c_str());
7248 : 6 : go_inform((*p)->location(), "%qs defined here", n.c_str());
7249 : 6 : }
7250 : : }
7251 : : }
7252 : 2067 : }
7253 : :
7254 : : // Class Function_declaration.
7255 : :
7256 : : // Whether this declares a method.
7257 : :
7258 : : bool
7259 : 2119211 : Function_declaration::is_method() const
7260 : : {
7261 : 2119211 : return this->fntype_->is_method();
7262 : : }
7263 : :
7264 : : // Whether this method should not be included in the type descriptor.
7265 : :
7266 : : bool
7267 : 1330920 : Function_declaration::nointerface() const
7268 : : {
7269 : 1330920 : go_assert(this->is_method());
7270 : 1330920 : return (this->pragmas_ & GOPRAGMA_NOINTERFACE) != 0;
7271 : : }
7272 : :
7273 : : // Record that this method should not be included in the type
7274 : : // descriptor.
7275 : :
7276 : : void
7277 : 2 : Function_declaration::set_nointerface()
7278 : : {
7279 : 2 : this->pragmas_ |= GOPRAGMA_NOINTERFACE;
7280 : 2 : }
7281 : :
7282 : : // Set the receiver type. This is used to remove aliases.
7283 : :
7284 : : void
7285 : 0 : Function_declaration::set_receiver_type(Type* rtype)
7286 : : {
7287 : 0 : Function_type* oft = this->fntype_;
7288 : 0 : Typed_identifier* rec = new Typed_identifier(oft->receiver()->name(),
7289 : : rtype,
7290 : 0 : oft->receiver()->location());
7291 : 0 : Typed_identifier_list* parameters = NULL;
7292 : 0 : if (oft->parameters() != NULL)
7293 : 0 : parameters = oft->parameters()->copy();
7294 : 0 : Typed_identifier_list* results = NULL;
7295 : 0 : if (oft->results() != NULL)
7296 : 0 : results = oft->results()->copy();
7297 : 0 : Function_type* nft = Type::make_function_type(rec, parameters, results,
7298 : : oft->location());
7299 : 0 : this->fntype_ = nft;
7300 : 0 : }
7301 : :
7302 : : // Import an inlinable function. This is used for an inlinable
7303 : : // function whose body is recorded in the export data. Parse the
7304 : : // export data into a Block and create a regular function using that
7305 : : // Block as its body. Redeclare this function declaration as the
7306 : : // function.
7307 : :
7308 : : void
7309 : 12247 : Function_declaration::import_function_body(Gogo* gogo, Named_object* no)
7310 : : {
7311 : 12247 : go_assert(no->func_declaration_value() == this);
7312 : 12247 : go_assert(no->package() != NULL);
7313 : 12247 : const std::string& body(this->imported_body_);
7314 : 12247 : go_assert(!body.empty());
7315 : :
7316 : : // Read the "//FILE:LINE" comment starts the export data.
7317 : :
7318 : 12247 : size_t indent = 1;
7319 : 12247 : if (this->is_method())
7320 : 593 : indent = 2;
7321 : 12247 : size_t i = 0;
7322 : 25087 : for (; i < indent; i++)
7323 : : {
7324 : 12840 : if (body.at(i) != ' ')
7325 : : {
7326 : 0 : go_error_at(this->location_,
7327 : : "invalid export body for %qs: bad initial indentation",
7328 : 0 : no->message_name().c_str());
7329 : 0 : return;
7330 : : }
7331 : : }
7332 : :
7333 : 12247 : if (body.substr(i, 2) != "//")
7334 : : {
7335 : 0 : go_error_at(this->location_,
7336 : : "invalid export body for %qs: missing file comment",
7337 : 0 : no->message_name().c_str());
7338 : 0 : return;
7339 : : }
7340 : :
7341 : 12247 : size_t colon = body.find(':', i + 2);
7342 : 12247 : size_t nl = body.find('\n', i + 2);
7343 : 12247 : if (nl == std::string::npos)
7344 : : {
7345 : 0 : go_error_at(this->location_,
7346 : : "invalid export body for %qs: missing file name",
7347 : 0 : no->message_name().c_str());
7348 : 0 : return;
7349 : : }
7350 : 12247 : if (colon == std::string::npos || nl < colon)
7351 : : {
7352 : 0 : go_error_at(this->location_,
7353 : : "invalid export body for %qs: missing initial line number",
7354 : 0 : no->message_name().c_str());
7355 : 0 : return;
7356 : : }
7357 : :
7358 : 12247 : std::string file = body.substr(i + 2, colon - (i + 2));
7359 : 12247 : std::string linestr = body.substr(colon + 1, nl - (colon + 1));
7360 : 12247 : char* end;
7361 : 12247 : long linenol = strtol(linestr.c_str(), &end, 10);
7362 : 12247 : if (*end != '\0')
7363 : : {
7364 : 0 : go_error_at(this->location_,
7365 : : "invalid export body for %qs: invalid initial line number",
7366 : 0 : no->message_name().c_str());
7367 : 0 : return;
7368 : : }
7369 : 12247 : unsigned int lineno = static_cast<unsigned int>(linenol);
7370 : :
7371 : : // Turn the file/line into a location.
7372 : :
7373 : 12247 : char* alc = new char[file.length() + 1];
7374 : 12247 : memcpy(alc, file.data(), file.length());
7375 : 12247 : alc[file.length()] = '\0';
7376 : 12247 : gogo->linemap()->start_file(alc, lineno);
7377 : 12247 : gogo->linemap()->start_line(lineno, 1);
7378 : 12247 : Location start_loc = gogo->linemap()->get_location(0);
7379 : :
7380 : : // Define the function with an outer block that declares the
7381 : : // parameters.
7382 : :
7383 : 12247 : Function_type* fntype = this->fntype_;
7384 : :
7385 : 12247 : Block* outer = new Block(NULL, start_loc);
7386 : :
7387 : 12247 : Function* fn = new Function(fntype, NULL, outer, start_loc);
7388 : 12247 : fn->set_is_inline_only();
7389 : :
7390 : 12247 : if (fntype->is_method())
7391 : : {
7392 : 593 : if (this->nointerface())
7393 : 1 : fn->set_nointerface();
7394 : 593 : const Typed_identifier* receiver = fntype->receiver();
7395 : 593 : Variable* recv_param = new Variable(receiver->type(), NULL, false,
7396 : 593 : true, true, start_loc);
7397 : :
7398 : 593 : std::string rname = receiver->name();
7399 : 593 : unsigned rcounter = 0;
7400 : :
7401 : : // We need to give a nameless receiver a name to avoid having it
7402 : : // clash with some other nameless param. FIXME.
7403 : 593 : Gogo::rename_if_empty(&rname, "r", &rcounter);
7404 : :
7405 : 593 : outer->bindings()->add_variable(rname, NULL, recv_param);
7406 : 593 : }
7407 : :
7408 : 12247 : const Typed_identifier_list* params = fntype->parameters();
7409 : 12247 : bool is_varargs = fntype->is_varargs();
7410 : 12247 : unsigned pcounter = 0;
7411 : 12247 : if (params != NULL)
7412 : : {
7413 : 10560 : for (Typed_identifier_list::const_iterator p = params->begin();
7414 : 27541 : p != params->end();
7415 : 16981 : ++p)
7416 : : {
7417 : 16981 : Variable* param = new Variable(p->type(), NULL, false, true, false,
7418 : 16981 : start_loc);
7419 : 16981 : if (is_varargs && p + 1 == params->end())
7420 : 581 : param->set_is_varargs_parameter();
7421 : :
7422 : 16981 : std::string pname = p->name();
7423 : :
7424 : : // We need to give each nameless parameter a non-empty name to avoid
7425 : : // having it clash with some other nameless param. FIXME.
7426 : 16981 : Gogo::rename_if_empty(&pname, "p", &pcounter);
7427 : :
7428 : 16981 : outer->bindings()->add_variable(pname, NULL, param);
7429 : 16981 : }
7430 : : }
7431 : :
7432 : 12247 : fn->create_result_variables(gogo);
7433 : :
7434 : 12247 : if (!fntype->is_method())
7435 : : {
7436 : 11654 : const Package* package = no->package();
7437 : 11654 : no = package->bindings()->add_function(no->name(), package, fn);
7438 : : }
7439 : : else
7440 : : {
7441 : 1186 : Named_type* rtype = fntype->receiver()->type()->deref()->named_type();
7442 : 593 : go_assert(rtype != NULL);
7443 : 593 : no = rtype->add_method(no->name(), fn);
7444 : 593 : const Package* package = rtype->named_object()->package();
7445 : 593 : package->bindings()->add_method(no);
7446 : : }
7447 : :
7448 : 12247 : Import_function_body ifb(gogo, this->imp_, no, body, nl + 1, outer, indent);
7449 : :
7450 : 12247 : if (!Block::import_block(outer, &ifb, start_loc))
7451 : 0 : return;
7452 : :
7453 : 12247 : outer->determine_types(gogo);
7454 : 12247 : gogo->lower_block(no, outer);
7455 : :
7456 : 12247 : gogo->add_imported_inline_function(no);
7457 : 12247 : }
7458 : :
7459 : : // Return the function descriptor.
7460 : :
7461 : : Expression*
7462 : 120055 : Function_declaration::descriptor(Gogo*, Named_object* no)
7463 : : {
7464 : 120055 : go_assert(!this->fntype_->is_method());
7465 : 120055 : if (this->descriptor_ == NULL)
7466 : 105079 : this->descriptor_ = Expression::make_func_descriptor(no);
7467 : 120055 : return this->descriptor_;
7468 : : }
7469 : :
7470 : : // Class Variable.
7471 : :
7472 : 1742687 : Variable::Variable(Type* type, Expression* init, bool is_global,
7473 : : bool is_parameter, bool is_receiver,
7474 : 1742687 : Location location)
7475 : 1742687 : : type_(type), init_(init), preinit_(NULL), location_(location),
7476 : 1742687 : toplevel_decl_(NULL), init_refs_(NULL), embeds_(NULL), backend_(NULL),
7477 : 1742687 : is_global_(is_global), is_parameter_(is_parameter), is_closure_(false),
7478 : 1742687 : is_receiver_(is_receiver), is_varargs_parameter_(false),
7479 : 1742687 : is_global_sink_(false), is_used_(false), is_address_taken_(false),
7480 : 1742687 : is_non_escaping_address_taken_(false), seen_(false),
7481 : 1742687 : init_is_lowered_(false), init_is_flattened_(false),
7482 : 1742687 : type_from_init_tuple_(false), type_from_range_index_(false),
7483 : 1742687 : type_from_range_value_(false), type_from_chan_element_(false),
7484 : 1742687 : is_type_switch_var_(false), determined_type_(false),
7485 : 1742687 : in_unique_section_(false), is_referenced_by_inline_(false)
7486 : : {
7487 : 1742687 : go_assert(type != NULL || init != NULL);
7488 : 1742687 : go_assert(!is_parameter || init == NULL);
7489 : 1742687 : }
7490 : :
7491 : : // Traverse the initializer expression.
7492 : :
7493 : : int
7494 : 21643942 : Variable::traverse_expression(Traverse* traverse, unsigned int traverse_mask)
7495 : : {
7496 : 21643942 : if (this->preinit_ != NULL)
7497 : : {
7498 : 36173 : if (this->preinit_->traverse(traverse) == TRAVERSE_EXIT)
7499 : : return TRAVERSE_EXIT;
7500 : : }
7501 : 21643942 : if (this->init_ != NULL
7502 : 5848926 : && ((traverse_mask
7503 : 5848926 : & (Traverse::traverse_expressions | Traverse::traverse_types))
7504 : : != 0))
7505 : : {
7506 : 4978871 : if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT)
7507 : : return TRAVERSE_EXIT;
7508 : : }
7509 : : return TRAVERSE_CONTINUE;
7510 : : }
7511 : :
7512 : : // Lower the initialization expression after parsing is complete.
7513 : :
7514 : : void
7515 : 6554903 : Variable::lower_init_expression(Gogo* gogo, Named_object* function,
7516 : : Statement_inserter* inserter)
7517 : : {
7518 : 6554903 : Named_object* dep = gogo->var_depends_on(this);
7519 : 6554903 : if (dep != NULL && dep->is_variable())
7520 : 194 : dep->var_value()->lower_init_expression(gogo, function, inserter);
7521 : :
7522 : 6554903 : if (this->embeds_ != NULL)
7523 : : {
7524 : : // Now that we have seen any possible type aliases, convert the
7525 : : // go:embed directives into an initializer.
7526 : 22 : go_assert(this->init_ == NULL && this->type_ != NULL);
7527 : 22 : this->init_ = gogo->initializer_for_embeds(this->type_, this->embeds_,
7528 : : this->location_);
7529 : 22 : delete this->embeds_;
7530 : 22 : this->embeds_ = NULL;
7531 : : }
7532 : :
7533 : 6554903 : if (this->init_ != NULL && !this->init_is_lowered_)
7534 : : {
7535 : 302033 : if (this->seen_)
7536 : : {
7537 : : // We will give an error elsewhere, this is just to prevent
7538 : : // an infinite loop.
7539 : 0 : return;
7540 : : }
7541 : 302033 : this->seen_ = true;
7542 : :
7543 : 302033 : Statement_inserter global_inserter;
7544 : 302033 : if (this->is_global_)
7545 : : {
7546 : 20356 : global_inserter = Statement_inserter(gogo, this);
7547 : 20356 : inserter = &global_inserter;
7548 : : }
7549 : :
7550 : 302033 : gogo->lower_expression(function, inserter, &this->init_);
7551 : :
7552 : 302033 : this->seen_ = false;
7553 : :
7554 : 302033 : this->init_is_lowered_ = true;
7555 : : }
7556 : : }
7557 : :
7558 : : // Flatten the initialization expression after ordering evaluations.
7559 : :
7560 : : void
7561 : 751215 : Variable::flatten_init_expression(Gogo* gogo, Named_object* function,
7562 : : Statement_inserter* inserter)
7563 : : {
7564 : 751215 : Named_object* dep = gogo->var_depends_on(this);
7565 : 751215 : if (dep != NULL && dep->is_variable())
7566 : 99 : dep->var_value()->flatten_init_expression(gogo, function, inserter);
7567 : :
7568 : 751215 : if (this->init_ != NULL && !this->init_is_flattened_)
7569 : : {
7570 : 282858 : if (this->seen_)
7571 : : {
7572 : : // We will give an error elsewhere, this is just to prevent
7573 : : // an infinite loop.
7574 : 0 : return;
7575 : : }
7576 : 282858 : this->seen_ = true;
7577 : :
7578 : 282858 : Statement_inserter global_inserter;
7579 : 282858 : if (this->is_global_)
7580 : : {
7581 : 8364 : global_inserter = Statement_inserter(gogo, this);
7582 : 8364 : inserter = &global_inserter;
7583 : : }
7584 : :
7585 : 282858 : gogo->flatten_expression(function, inserter, &this->init_);
7586 : :
7587 : : // If an interface conversion is needed, we need a temporary
7588 : : // variable.
7589 : 282858 : if (this->type_ != NULL
7590 : 282858 : && !Type::are_identical(this->type_, this->init_->type(),
7591 : : Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
7592 : : NULL)
7593 : 898 : && this->init_->type()->interface_type() != NULL
7594 : 282859 : && !this->init_->is_multi_eval_safe())
7595 : : {
7596 : 0 : Temporary_statement* temp =
7597 : 0 : Statement::make_temporary(NULL, this->init_, this->location_);
7598 : 0 : inserter->insert(temp);
7599 : 0 : this->init_ = Expression::make_temporary_reference(temp,
7600 : : this->location_);
7601 : : }
7602 : :
7603 : 282858 : this->seen_ = false;
7604 : 282858 : this->init_is_flattened_ = true;
7605 : : }
7606 : : }
7607 : :
7608 : : // Get the preinit block.
7609 : :
7610 : : Block*
7611 : 52980 : Variable::preinit_block(Gogo* gogo)
7612 : : {
7613 : 52980 : go_assert(this->is_global_);
7614 : 52980 : if (this->preinit_ == NULL)
7615 : 12208 : this->preinit_ = new Block(NULL, this->location());
7616 : :
7617 : : // If a global variable has a preinitialization statement, then we
7618 : : // need to have an initialization function.
7619 : 52980 : gogo->set_need_init_fn();
7620 : :
7621 : 52980 : return this->preinit_;
7622 : : }
7623 : :
7624 : : // Add a statement to be run before the initialization expression.
7625 : :
7626 : : void
7627 : 52980 : Variable::add_preinit_statement(Gogo* gogo, Statement* s)
7628 : : {
7629 : 52980 : Block* b = this->preinit_block(gogo);
7630 : 52980 : b->add_statement(s);
7631 : 52980 : b->set_end_location(s->location());
7632 : 52980 : }
7633 : :
7634 : : // Whether this variable has a type.
7635 : :
7636 : : bool
7637 : 19294261 : Variable::has_type() const
7638 : : {
7639 : 19294261 : if (this->type_ == NULL)
7640 : : return false;
7641 : :
7642 : : // A variable created in a type switch case nil does not actually
7643 : : // have a type yet. It will be changed to use the initializer's
7644 : : // type in determine_type.
7645 : 18652313 : if (this->is_type_switch_var_
7646 : 18652313 : && this->type_->is_nil_constant_as_type())
7647 : : return false;
7648 : :
7649 : : return true;
7650 : : }
7651 : :
7652 : : // In an assignment which sets a variable to a tuple of EXPR, return
7653 : : // the type of the first element of the tuple.
7654 : :
7655 : : Type*
7656 : 1594 : Variable::type_from_tuple(Expression* expr, bool report_error) const
7657 : : {
7658 : 1594 : if (Index_expression::is_map_index(expr))
7659 : : {
7660 : 1553 : Map_type* mt;
7661 : 1553 : if (expr->map_index_expression() != NULL)
7662 : 0 : mt = expr->map_index_expression()->get_map_type();
7663 : : else
7664 : 1553 : mt = expr->index_expression()->left()->type()->map_type();
7665 : 0 : if (mt == NULL)
7666 : 0 : return Type::make_error_type();
7667 : 1553 : return mt->val_type();
7668 : : }
7669 : 41 : else if (expr->receive_expression() != NULL)
7670 : : {
7671 : 41 : Expression* channel = expr->receive_expression()->channel();
7672 : 41 : Type* channel_type = channel->type();
7673 : 41 : if (channel_type->channel_type() == NULL)
7674 : 0 : return Type::make_error_type();
7675 : 82 : return channel_type->channel_type()->element_type();
7676 : : }
7677 : : else
7678 : : {
7679 : 0 : if (report_error)
7680 : 0 : go_error_at(this->location(), "invalid tuple definition");
7681 : 0 : return Type::make_error_type();
7682 : : }
7683 : : }
7684 : :
7685 : : // Given EXPR used in a range clause, return either the index type or
7686 : : // the value type of the range, depending upon GET_INDEX_TYPE.
7687 : :
7688 : : Type*
7689 : 31409 : Variable::type_from_range(Expression* expr, bool get_index_type,
7690 : : bool report_error) const
7691 : : {
7692 : 31409 : Type* t = expr->type();
7693 : 31409 : if (t->is_error_type())
7694 : : return t;
7695 : 31409 : else if (t->array_type() != NULL
7696 : 4113 : || (t->points_to() != NULL
7697 : 222 : && t->points_to()->array_type() != NULL
7698 : 222 : && !t->points_to()->is_slice_type()))
7699 : : {
7700 : 27518 : if (get_index_type)
7701 : 7286 : return Type::lookup_integer_type("int");
7702 : : else
7703 : 60696 : return t->deref()->array_type()->element_type();
7704 : : }
7705 : 3891 : else if (t->is_string_type())
7706 : : {
7707 : 732 : if (get_index_type)
7708 : 190 : return Type::lookup_integer_type("int");
7709 : : else
7710 : 542 : return Type::lookup_integer_type("int32");
7711 : : }
7712 : 3159 : else if (t->map_type() != NULL)
7713 : : {
7714 : 3084 : if (get_index_type)
7715 : 3378 : return t->map_type()->key_type();
7716 : : else
7717 : 2790 : return t->map_type()->val_type();
7718 : : }
7719 : 75 : else if (t->channel_type() != NULL)
7720 : : {
7721 : 75 : if (get_index_type)
7722 : 150 : return t->channel_type()->element_type();
7723 : : else
7724 : : {
7725 : 0 : if (report_error)
7726 : 0 : go_error_at(this->location(),
7727 : : ("invalid definition of value variable "
7728 : : "for channel range"));
7729 : 0 : return Type::make_error_type();
7730 : : }
7731 : : }
7732 : : else
7733 : : {
7734 : 0 : if (report_error)
7735 : 0 : go_error_at(this->location(), "invalid type for range clause");
7736 : 0 : return Type::make_error_type();
7737 : : }
7738 : : }
7739 : :
7740 : : // EXPR should be a channel. Return the channel's element type.
7741 : :
7742 : : Type*
7743 : 524 : Variable::type_from_chan_element(Expression* expr, bool report_error) const
7744 : : {
7745 : 524 : Type* t = expr->type();
7746 : 524 : if (t->channel_type() != NULL)
7747 : 1048 : return t->channel_type()->element_type();
7748 : : else
7749 : : {
7750 : 0 : if (report_error)
7751 : 0 : go_error_at(this->location(), "expected channel");
7752 : 0 : return Type::make_error_type();
7753 : : }
7754 : : }
7755 : :
7756 : : // Return the type of the Variable. This may be called before
7757 : : // Variable::determine_type is called, which means that we may need to
7758 : : // get the type from the initializer. FIXME: If we combine lowering
7759 : : // with type determination, then this should be unnecessary.
7760 : :
7761 : : Type*
7762 : 71414487 : Variable::type()
7763 : : {
7764 : : // A variable in a type switch with a nil case will have the wrong
7765 : : // type here. This gets fixed up in determine_type, below.
7766 : 71414487 : Type* type = this->type_;
7767 : 71414487 : Expression* init = this->init_;
7768 : 71414487 : if (this->is_type_switch_var_
7769 : 1009492 : && type != NULL
7770 : 72423979 : && this->type_->is_nil_constant_as_type())
7771 : : {
7772 : 0 : Type_guard_expression* tge = this->init_->type_guard_expression();
7773 : 0 : go_assert(tge != NULL);
7774 : 0 : init = tge->expr();
7775 : 0 : type = NULL;
7776 : : }
7777 : :
7778 : 71414487 : if (this->seen_)
7779 : : {
7780 : 0 : if (this->type_ == NULL || !this->type_->is_error_type())
7781 : : {
7782 : 0 : go_error_at(this->location_, "variable initializer refers to itself");
7783 : 0 : this->type_ = Type::make_error_type();
7784 : : }
7785 : 0 : return this->type_;
7786 : : }
7787 : :
7788 : 71414487 : this->seen_ = true;
7789 : :
7790 : 71414487 : if (type != NULL)
7791 : : ;
7792 : 0 : else if (this->type_from_init_tuple_)
7793 : 0 : type = this->type_from_tuple(init, false);
7794 : 0 : else if (this->type_from_range_index_ || this->type_from_range_value_)
7795 : 0 : type = this->type_from_range(init, this->type_from_range_index_, false);
7796 : 0 : else if (this->type_from_chan_element_)
7797 : 0 : type = this->type_from_chan_element(init, false);
7798 : : else
7799 : : {
7800 : 0 : go_assert(init != NULL);
7801 : 0 : type = init->type();
7802 : 0 : go_assert(type != NULL);
7803 : :
7804 : : // Variables should not have abstract types.
7805 : 0 : if (type->is_abstract())
7806 : 0 : type = type->make_non_abstract_type();
7807 : :
7808 : 0 : if (type->is_void_type())
7809 : 0 : type = Type::make_error_type();
7810 : : }
7811 : :
7812 : 71414487 : this->seen_ = false;
7813 : :
7814 : 71414487 : return type;
7815 : : }
7816 : :
7817 : : // Fetch the type from a const pointer, in which case it should have
7818 : : // been set already.
7819 : :
7820 : : Type*
7821 : 8440 : Variable::type() const
7822 : : {
7823 : 8440 : go_assert(this->type_ != NULL);
7824 : 8440 : return this->type_;
7825 : : }
7826 : :
7827 : : // Set the type if necessary.
7828 : :
7829 : : void
7830 : 6758897 : Variable::determine_type(Gogo* gogo)
7831 : : {
7832 : 6758897 : if (this->determined_type_)
7833 : : return;
7834 : 906438 : this->determined_type_ = true;
7835 : :
7836 : 906438 : if (this->preinit_ != NULL)
7837 : 4 : this->preinit_->determine_types(gogo);
7838 : :
7839 : : // A variable in a type switch with a nil case will have the wrong
7840 : : // type here. It will have an initializer which is a type guard.
7841 : : // We want to initialize it to the value without the type guard, and
7842 : : // use the type of that value as well.
7843 : 906438 : if (this->is_type_switch_var_
7844 : 9915 : && this->type_ != NULL
7845 : 915212 : && this->type_->is_nil_constant_as_type())
7846 : : {
7847 : 122 : Type_guard_expression* tge = this->init_->type_guard_expression();
7848 : 0 : go_assert(tge != NULL);
7849 : 122 : this->type_ = NULL;
7850 : 122 : this->init_ = tge->expr();
7851 : : }
7852 : :
7853 : 906438 : if (this->init_ == NULL)
7854 : 571417 : go_assert(this->type_ != NULL && !this->type_->is_abstract());
7855 : 335021 : else if (this->type_from_init_tuple_)
7856 : : {
7857 : 1594 : Expression *init = this->init_;
7858 : 1594 : init->determine_type_no_context(gogo);
7859 : 1594 : this->type_ = this->type_from_tuple(init, true);
7860 : 1594 : this->init_ = NULL;
7861 : : }
7862 : 333427 : else if (this->type_from_range_index_ || this->type_from_range_value_)
7863 : : {
7864 : 31409 : Expression* init = this->init_;
7865 : 31409 : init->determine_type_no_context(gogo);
7866 : 31409 : this->type_ = this->type_from_range(init, this->type_from_range_index_,
7867 : : true);
7868 : 31409 : this->init_ = NULL;
7869 : 31409 : }
7870 : 302018 : else if (this->type_from_chan_element_)
7871 : : {
7872 : 524 : Expression* init = this->init_;
7873 : 524 : init->determine_type_no_context(gogo);
7874 : 524 : this->type_ = this->type_from_chan_element(init, true);
7875 : 524 : this->init_ = NULL;
7876 : : }
7877 : : else
7878 : : {
7879 : 301494 : Type_context context(this->type_, false);
7880 : 301494 : this->init_->determine_type(gogo, &context);
7881 : 301494 : if (this->type_ == NULL)
7882 : : {
7883 : 287569 : Type* type = this->init_->type();
7884 : 287569 : go_assert(type != NULL);
7885 : 287569 : if (type->is_abstract())
7886 : 601 : type = type->make_non_abstract_type();
7887 : :
7888 : 287569 : if (type->is_void_type())
7889 : : {
7890 : 1 : go_error_at(this->location_, "variable has no type");
7891 : 1 : type = Type::make_error_type();
7892 : : }
7893 : 287568 : else if (type->is_nil_type())
7894 : : {
7895 : 2 : go_error_at(this->location_, "variable defined to nil type");
7896 : 2 : type = Type::make_error_type();
7897 : : }
7898 : 287566 : else if (type->is_call_multiple_result_type())
7899 : : {
7900 : 2 : go_error_at(this->location_,
7901 : : "single variable set to multiple-value function call");
7902 : 2 : type = Type::make_error_type();
7903 : : }
7904 : :
7905 : 287569 : this->type_ = type;
7906 : : }
7907 : : }
7908 : : }
7909 : :
7910 : : // Get the initial value of a variable. This does not
7911 : : // consider whether the variable is in the heap--it returns the
7912 : : // initial value as though it were always stored in the stack.
7913 : :
7914 : : Bexpression*
7915 : 395645 : Variable::get_init(Gogo* gogo, Named_object* function)
7916 : : {
7917 : 395645 : go_assert(this->preinit_ == NULL);
7918 : 395645 : Location loc = this->location();
7919 : 395645 : if (this->init_ == NULL)
7920 : : {
7921 : 113105 : go_assert(!this->is_parameter_);
7922 : 113105 : if (this->is_global_ || this->is_in_heap())
7923 : : return NULL;
7924 : 95247 : Btype* btype = this->type()->get_backend(gogo);
7925 : 95247 : return gogo->backend()->zero_expression(btype);
7926 : : }
7927 : : else
7928 : : {
7929 : 282540 : Translate_context context(gogo, function, NULL, NULL);
7930 : 282540 : Expression* init = Expression::make_cast(this->type(), this->init_, loc);
7931 : 282540 : return init->get_backend(&context);
7932 : : }
7933 : : }
7934 : :
7935 : : // Get the initial value of a variable when a block is required.
7936 : : // VAR_DECL is the decl to set; it may be NULL for a sink variable.
7937 : :
7938 : : Bstatement*
7939 : 12208 : Variable::get_init_block(Gogo* gogo, Named_object* function,
7940 : : Bvariable* var_decl)
7941 : : {
7942 : 12208 : go_assert(this->preinit_ != NULL);
7943 : :
7944 : : // We want to add the variable assignment to the end of the preinit
7945 : : // block.
7946 : :
7947 : 12208 : Translate_context context(gogo, function, NULL, NULL);
7948 : 12208 : Bblock* bblock = this->preinit_->get_backend(&context);
7949 : 12208 : Bfunction* bfunction =
7950 : 12208 : function->func_value()->get_or_make_decl(gogo, function);
7951 : :
7952 : : // It's possible to have pre-init statements without an initializer
7953 : : // if the pre-init statements set the variable.
7954 : 12208 : Bstatement* decl_init = NULL;
7955 : 12208 : if (this->init_ != NULL)
7956 : : {
7957 : 212 : if (var_decl == NULL)
7958 : : {
7959 : 29 : Bexpression* init_bexpr = this->init_->get_backend(&context);
7960 : 29 : decl_init = gogo->backend()->expression_statement(bfunction,
7961 : : init_bexpr);
7962 : : }
7963 : : else
7964 : : {
7965 : 183 : Location loc = this->location();
7966 : 183 : Expression* val_expr =
7967 : 183 : Expression::make_cast(this->type(), this->init_, loc);
7968 : 183 : Bexpression* val = val_expr->get_backend(&context);
7969 : 183 : Bexpression* var_ref =
7970 : 183 : gogo->backend()->var_expression(var_decl, loc);
7971 : 183 : decl_init = gogo->backend()->assignment_statement(bfunction, var_ref,
7972 : : val, loc);
7973 : : }
7974 : : }
7975 : 12208 : Bstatement* block_stmt = gogo->backend()->block_statement(bblock);
7976 : 12208 : if (decl_init != NULL)
7977 : 212 : block_stmt = gogo->backend()->compound_statement(block_stmt, decl_init);
7978 : 12208 : return block_stmt;
7979 : : }
7980 : :
7981 : : // Add an initializer reference.
7982 : :
7983 : : void
7984 : 11650 : Variable::add_init_ref(Named_object* var)
7985 : : {
7986 : 11650 : if (this->init_refs_ == NULL)
7987 : 3632 : this->init_refs_ = new std::vector<Named_object*>;
7988 : 11650 : this->init_refs_->push_back(var);
7989 : 11650 : }
7990 : :
7991 : : // Export the variable
7992 : :
7993 : : void
7994 : 8440 : Variable::export_var(Export* exp, const Named_object* no) const
7995 : : {
7996 : 8440 : go_assert(this->is_global_);
7997 : 8440 : exp->write_c_string("var ");
7998 : 8440 : if (no->package() != NULL)
7999 : : {
8000 : 2056 : char buf[50];
8001 : 2056 : snprintf(buf, sizeof buf, "<p%d>", exp->package_index(no->package()));
8002 : 2056 : exp->write_c_string(buf);
8003 : : }
8004 : :
8005 : 8440 : if (!Gogo::is_hidden_name(no->name()))
8006 : 5354 : exp->write_string(no->name());
8007 : : else
8008 : : {
8009 : 3086 : exp->write_c_string(".");
8010 : 3086 : exp->write_string(Gogo::unpack_hidden_name(no->name()));
8011 : : }
8012 : :
8013 : 8440 : exp->write_c_string(" ");
8014 : 8440 : exp->write_type(this->type());
8015 : 8440 : exp->write_c_string("\n");
8016 : 8440 : }
8017 : :
8018 : : // Import a variable.
8019 : :
8020 : : bool
8021 : 326311 : Variable::import_var(Import* imp, std::string* pname, Package** ppkg,
8022 : : bool* pis_exported, Type** ptype)
8023 : : {
8024 : 326311 : imp->require_c_string("var ");
8025 : 326311 : if (!Import::read_qualified_identifier(imp, pname, ppkg, pis_exported))
8026 : : {
8027 : 0 : go_error_at(imp->location(),
8028 : : "import error at %d: bad variable name in export data",
8029 : : imp->pos());
8030 : 0 : return false;
8031 : : }
8032 : 326311 : imp->require_c_string(" ");
8033 : 326311 : *ptype = imp->read_type();
8034 : 326311 : imp->require_semicolon_if_old_version();
8035 : 326311 : imp->require_c_string("\n");
8036 : 326311 : return true;
8037 : : }
8038 : :
8039 : : // Convert a variable to the backend representation.
8040 : :
8041 : : Bvariable*
8042 : 4440379 : Variable::get_backend_variable(Gogo* gogo, Named_object* function,
8043 : : const Package* package, const std::string& name)
8044 : : {
8045 : 4440379 : if (this->backend_ == NULL)
8046 : : {
8047 : 923204 : Backend* backend = gogo->backend();
8048 : 923204 : Type* type = this->type_;
8049 : 923204 : if (type->is_error_type()
8050 : 923204 : || (type->is_undefined()
8051 : 5 : && (!this->is_global_ || package == NULL)))
8052 : 149 : this->backend_ = backend->error_variable();
8053 : : else
8054 : : {
8055 : 923055 : bool is_parameter = this->is_parameter_;
8056 : 923055 : if (this->is_receiver_ && type->points_to() == NULL)
8057 : : is_parameter = false;
8058 : 923055 : if (this->is_in_heap())
8059 : : {
8060 : 18171 : is_parameter = false;
8061 : 18171 : type = Type::make_pointer_type(type);
8062 : : }
8063 : :
8064 : 923055 : Btype* btype = type->get_backend(gogo);
8065 : :
8066 : 923055 : Bvariable* bvar;
8067 : 923055 : if (Map_type::is_zero_value(this))
8068 : 3 : bvar = Map_type::backend_zero_value(gogo);
8069 : 923052 : else if (this->is_global_)
8070 : : {
8071 : 34342 : Backend_name bname;
8072 : 34342 : gogo->global_var_backend_name(name, package, &bname);
8073 : :
8074 : 34342 : bool is_hidden = Gogo::is_hidden_name(name);
8075 : : // Hack to export runtime.writeBarrier. FIXME.
8076 : : // This is because go:linkname doesn't work on variables.
8077 : 68684 : if (gogo->compiling_runtime()
8078 : 34342 : && bname.name() == "runtime.writeBarrier")
8079 : 20 : is_hidden = false;
8080 : :
8081 : : // If an inline body refers to this variable, then it
8082 : : // needs to be visible in the symbol table.
8083 : 34342 : if (this->is_referenced_by_inline_)
8084 : 4329 : is_hidden = false;
8085 : :
8086 : : // If this variable is in a different package, then it
8087 : : // can't be treated as a hidden symbol. This case can
8088 : : // arise when an inlined function refers to a
8089 : : // package-scope unexported variable.
8090 : 34342 : if (package != NULL)
8091 : 9045 : is_hidden = false;
8092 : :
8093 : 34342 : unsigned int flags = 0;
8094 : 34342 : if (this->is_address_taken_
8095 : 32083 : || this->is_non_escaping_address_taken_)
8096 : 3542 : flags |= Backend::variable_address_is_taken;
8097 : 34342 : if (package != NULL)
8098 : 9045 : flags |= Backend::variable_is_external;
8099 : 34342 : if (is_hidden)
8100 : 17925 : flags |= Backend::variable_is_hidden;
8101 : 34342 : if (this->in_unique_section_)
8102 : 0 : flags |= Backend::variable_in_unique_section;
8103 : :
8104 : : // For some reason asm_name can't be the empty string
8105 : : // for global_variable, so we call asm_name rather than
8106 : : // optional_asm_name here. FIXME.
8107 : :
8108 : 34342 : bvar = backend->global_variable(bname.name(),
8109 : 34342 : bname.asm_name(),
8110 : : btype, flags,
8111 : : this->location_);
8112 : 34342 : }
8113 : 888710 : else if (function == NULL)
8114 : : {
8115 : 0 : go_assert(saw_errors());
8116 : 0 : bvar = backend->error_variable();
8117 : : }
8118 : : else
8119 : : {
8120 : 888710 : const std::string n = Gogo::unpack_hidden_name(name);
8121 : 888710 : Bfunction* bfunction = function->func_value()->get_decl();
8122 : 888710 : unsigned int flags = 0;
8123 : 888710 : if (this->is_non_escaping_address_taken_
8124 : 888710 : && !this->is_in_heap())
8125 : : flags |= Backend::variable_address_is_taken;
8126 : 888710 : if (this->is_closure())
8127 : 14291 : bvar = backend->static_chain_variable(bfunction, n, btype,
8128 : : flags, this->location_);
8129 : 874419 : else if (is_parameter)
8130 : 473804 : bvar = backend->parameter_variable(bfunction, n, btype,
8131 : : flags, this->location_);
8132 : : else
8133 : : {
8134 : 400615 : Bvariable* bvar_decl = NULL;
8135 : 400615 : if (this->toplevel_decl_ != NULL)
8136 : : {
8137 : 6520 : Translate_context context(gogo, NULL, NULL, NULL);
8138 : 6520 : bvar_decl = this->toplevel_decl_->temporary_statement()
8139 : 6520 : ->get_backend_variable(&context);
8140 : : }
8141 : 400615 : bvar = backend->local_variable(bfunction, n, btype,
8142 : : bvar_decl, flags,
8143 : : this->location_);
8144 : : }
8145 : 888710 : }
8146 : 923055 : this->backend_ = bvar;
8147 : : }
8148 : : }
8149 : 4440379 : return this->backend_;
8150 : : }
8151 : :
8152 : : // Class Result_variable.
8153 : :
8154 : : // Convert a result variable to the backend representation.
8155 : :
8156 : : Bvariable*
8157 : 1161437 : Result_variable::get_backend_variable(Gogo* gogo, Named_object* function,
8158 : : const std::string& name)
8159 : : {
8160 : 1161437 : if (this->backend_ == NULL)
8161 : : {
8162 : 244562 : Backend* backend = gogo->backend();
8163 : 244562 : Type* type = this->type_;
8164 : 244562 : if (type->is_error())
8165 : 3 : this->backend_ = backend->error_variable();
8166 : : else
8167 : : {
8168 : 244559 : if (this->is_in_heap())
8169 : 122 : type = Type::make_pointer_type(type);
8170 : 244559 : Btype* btype = type->get_backend(gogo);
8171 : 244559 : Bfunction* bfunction = function->func_value()->get_decl();
8172 : 244559 : std::string n = Gogo::unpack_hidden_name(name);
8173 : 244559 : unsigned int flags = 0;
8174 : 244559 : if (this->is_non_escaping_address_taken_
8175 : 244559 : && !this->is_in_heap())
8176 : : flags |= Backend::variable_address_is_taken;
8177 : 244559 : this->backend_ = backend->local_variable(bfunction, n, btype,
8178 : : NULL, flags,
8179 : : this->location_);
8180 : 244559 : }
8181 : : }
8182 : 1161437 : return this->backend_;
8183 : : }
8184 : :
8185 : : // Class Named_constant.
8186 : :
8187 : : // Set the type of a named constant. This is only used to set the
8188 : : // type to an error type.
8189 : :
8190 : : void
8191 : 0 : Named_constant::set_type(Type* t)
8192 : : {
8193 : 0 : go_assert(this->type_ == NULL || t->is_error_type());
8194 : 0 : this->type_ = t;
8195 : 0 : }
8196 : :
8197 : : // Traverse the initializer expression.
8198 : :
8199 : : int
8200 : 16160131 : Named_constant::traverse_expression(Traverse* traverse)
8201 : : {
8202 : 16160131 : return Expression::traverse(&this->expr_, traverse);
8203 : : }
8204 : :
8205 : : // Set the iota value in a constant expression.
8206 : :
8207 : 994857 : class Set_iota_value : public Traverse
8208 : : {
8209 : : public:
8210 : 994857 : Set_iota_value(int iota_value)
8211 : 994857 : : Traverse(traverse_expressions),
8212 : 994857 : iota_value_(iota_value)
8213 : : { }
8214 : :
8215 : : int
8216 : : expression(Expression**);
8217 : :
8218 : : private:
8219 : : int iota_value_;
8220 : : };
8221 : :
8222 : : int
8223 : 1120705 : Set_iota_value::expression(Expression** pexpr)
8224 : : {
8225 : 1120705 : Expression* expr = *pexpr;
8226 : 1120705 : if (expr->const_expression() != NULL)
8227 : 46091 : expr->const_expression()->set_iota_value(this->iota_value_);
8228 : 1074614 : else if (expr->unknown_expression() != NULL)
8229 : : {
8230 : : // This case can happen for an array length that is not set in
8231 : : // the determine types pass.
8232 : 17160 : expr->unknown_expression()->set_iota_value(this->iota_value_);
8233 : : }
8234 : 1120705 : return TRAVERSE_CONTINUE;
8235 : : }
8236 : :
8237 : : // Determine the type of the constant.
8238 : :
8239 : : void
8240 : 1578631 : Named_constant::determine_type(Gogo* gogo)
8241 : : {
8242 : 1578631 : if (this->type_is_determined_)
8243 : 583774 : return;
8244 : 994857 : this->type_is_determined_ = true;
8245 : :
8246 : 994857 : if (this->type_ != NULL)
8247 : : {
8248 : 222604 : Type_context context(this->type_, this->type_->is_abstract());
8249 : 222604 : this->expr_->determine_type(gogo, &context);
8250 : : }
8251 : : else
8252 : : {
8253 : : // A constant may have an abstract type.
8254 : 772253 : Type_context context(NULL, true);
8255 : 772253 : this->expr_->determine_type(gogo, &context);
8256 : 772253 : this->type_ = this->expr_->type();
8257 : 772253 : go_assert(this->type_ != NULL);
8258 : : }
8259 : :
8260 : 994857 : Set_iota_value siv(this->iota_value_);
8261 : 994857 : this->traverse_expression(&siv);
8262 : 994857 : }
8263 : :
8264 : : // Indicate that we found and reported an error for this constant.
8265 : :
8266 : : void
8267 : 41 : Named_constant::set_error()
8268 : : {
8269 : 41 : this->type_ = Type::make_error_type();
8270 : 41 : this->expr_ = Expression::make_error(this->location_);
8271 : 41 : }
8272 : :
8273 : : // Export a constant.
8274 : :
8275 : : void
8276 : 39525 : Named_constant::export_const(Export* exp, const std::string& name) const
8277 : : {
8278 : 39525 : exp->write_c_string("const ");
8279 : 39525 : exp->write_string(name);
8280 : 39525 : exp->write_c_string(" ");
8281 : 39525 : if (!this->type_->is_abstract())
8282 : : {
8283 : 15584 : exp->write_type(this->type_);
8284 : 15584 : exp->write_c_string(" ");
8285 : : }
8286 : 39525 : exp->write_c_string("= ");
8287 : :
8288 : 39525 : Export_function_body efb(exp, 0);
8289 : 39525 : if (!this->type_->is_abstract())
8290 : 15584 : efb.set_type_context(this->type_);
8291 : 39525 : this->expr()->export_expression(&efb);
8292 : 39525 : exp->write_string(efb.body());
8293 : :
8294 : 39525 : exp->write_c_string("\n");
8295 : 39525 : }
8296 : :
8297 : : // Import a constant.
8298 : :
8299 : : void
8300 : 837398 : Named_constant::import_const(Import* imp, std::string* pname, Type** ptype,
8301 : : Expression** pexpr)
8302 : : {
8303 : 837398 : imp->require_c_string("const ");
8304 : 837398 : *pname = imp->read_identifier();
8305 : 837398 : imp->require_c_string(" ");
8306 : 837398 : if (imp->peek_char() == '=')
8307 : 642051 : *ptype = NULL;
8308 : : else
8309 : : {
8310 : 195347 : *ptype = imp->read_type();
8311 : 195347 : imp->require_c_string(" ");
8312 : : }
8313 : 837398 : imp->require_c_string("= ");
8314 : 837398 : *pexpr = Expression::import_expression(imp, imp->location());
8315 : 837398 : imp->require_semicolon_if_old_version();
8316 : 837398 : imp->require_c_string("\n");
8317 : 837398 : }
8318 : :
8319 : : // Get the backend representation.
8320 : :
8321 : : Bexpression*
8322 : 23399 : Named_constant::get_backend(Gogo* gogo, Named_object* const_no)
8323 : : {
8324 : 23399 : if (this->bconst_ == NULL)
8325 : : {
8326 : 23399 : Translate_context subcontext(gogo, NULL, NULL, NULL);
8327 : 23399 : Type* type = this->type();
8328 : 23399 : Location loc = this->location();
8329 : :
8330 : 23399 : Expression* const_ref = Expression::make_const_reference(const_no, loc);
8331 : 23399 : Bexpression* const_decl = const_ref->get_backend(&subcontext);
8332 : 46798 : if (type != NULL && type->is_numeric_type())
8333 : : {
8334 : 23399 : Btype* btype = type->get_backend(gogo);
8335 : 23399 : std::string name;
8336 : 23399 : if (const_no->package() == NULL)
8337 : 23399 : name = gogo->pkgpath();
8338 : : else
8339 : 0 : name = const_no->package()->pkgpath();
8340 : 23399 : name.push_back('.');
8341 : 23399 : name.append(Gogo::unpack_hidden_name(const_no->name()));
8342 : 23399 : const_decl =
8343 : 23399 : gogo->backend()->named_constant_expression(btype, name,
8344 : : const_decl, loc);
8345 : 23399 : }
8346 : 23399 : this->bconst_ = const_decl;
8347 : : }
8348 : 23399 : return this->bconst_;
8349 : : }
8350 : :
8351 : : // Add a method.
8352 : :
8353 : : Named_object*
8354 : 2538 : Type_declaration::add_method(const std::string& name, Function* function)
8355 : : {
8356 : 2538 : Named_object* ret = Named_object::make_function(name, NULL, function);
8357 : 2538 : this->methods_.push_back(ret);
8358 : 2538 : return ret;
8359 : : }
8360 : :
8361 : : // Add a method declaration.
8362 : :
8363 : : Named_object*
8364 : 0 : Type_declaration::add_method_declaration(const std::string& name,
8365 : : Package* package,
8366 : : Function_type* type,
8367 : : Location location)
8368 : : {
8369 : 0 : Named_object* ret = Named_object::make_function_declaration(name, package,
8370 : 0 : type, location);
8371 : 0 : this->methods_.push_back(ret);
8372 : 0 : return ret;
8373 : : }
8374 : :
8375 : : // Return whether any methods are defined.
8376 : :
8377 : : bool
8378 : 2 : Type_declaration::has_methods() const
8379 : : {
8380 : 2 : return !this->methods_.empty();
8381 : : }
8382 : :
8383 : : // Define methods for the real type.
8384 : :
8385 : : void
8386 : 622927 : Type_declaration::define_methods(Named_type* nt)
8387 : : {
8388 : 622927 : if (this->methods_.empty())
8389 : : return;
8390 : :
8391 : 518 : while (nt->is_alias())
8392 : : {
8393 : 0 : Type *t = nt->real_type()->forwarded();
8394 : 0 : if (t->named_type() != NULL)
8395 : 0 : nt = t->named_type();
8396 : 0 : else if (t->forward_declaration_type() != NULL)
8397 : : {
8398 : 0 : Named_object* no = t->forward_declaration_type()->named_object();
8399 : 0 : Type_declaration* td = no->type_declaration_value();
8400 : 0 : td->methods_.insert(td->methods_.end(), this->methods_.begin(),
8401 : : this->methods_.end());
8402 : 0 : this->methods_.clear();
8403 : 0 : return;
8404 : : }
8405 : : else
8406 : : {
8407 : 0 : for (std::vector<Named_object*>::const_iterator p =
8408 : 0 : this->methods_.begin();
8409 : 0 : p != this->methods_.end();
8410 : 0 : ++p)
8411 : 0 : go_error_at((*p)->location(),
8412 : : ("invalid receiver type "
8413 : : "(receiver must be a named type)"));
8414 : : return;
8415 : : }
8416 : : }
8417 : :
8418 : 3055 : for (std::vector<Named_object*>::const_iterator p = this->methods_.begin();
8419 : 3055 : p != this->methods_.end();
8420 : 2537 : ++p)
8421 : : {
8422 : 2537 : if ((*p)->is_function_declaration()
8423 : 2537 : || !(*p)->func_value()->is_sink())
8424 : 2536 : nt->add_existing_method(*p);
8425 : : }
8426 : : }
8427 : :
8428 : : // We are using the type. Return true if we should issue a warning.
8429 : :
8430 : : bool
8431 : 8 : Type_declaration::using_type()
8432 : : {
8433 : 8 : bool ret = !this->issued_warning_;
8434 : 8 : this->issued_warning_ = true;
8435 : 8 : return ret;
8436 : : }
8437 : :
8438 : : // Class Unknown_name.
8439 : :
8440 : : // Set the real named object.
8441 : :
8442 : : void
8443 : 85481 : Unknown_name::set_real_named_object(Named_object* no)
8444 : : {
8445 : 85481 : go_assert(this->real_named_object_ == NULL);
8446 : 85481 : go_assert(!no->is_unknown());
8447 : 85481 : this->real_named_object_ = no;
8448 : 85481 : }
8449 : :
8450 : : // Class Named_object.
8451 : :
8452 : 10334757 : Named_object::Named_object(const std::string& name,
8453 : : const Package* package,
8454 : 10334757 : Classification classification)
8455 : 10334757 : : name_(name), package_(package), classification_(classification),
8456 : 10334757 : is_redefinition_(false)
8457 : : {
8458 : 10334757 : if (Gogo::is_sink_name(name))
8459 : 1385474 : go_assert(classification == NAMED_OBJECT_SINK);
8460 : 10334757 : }
8461 : :
8462 : : // Make an unknown name. This is used by the parser. The name must
8463 : : // be resolved later. Unknown names are only added in the current
8464 : : // package.
8465 : :
8466 : : Named_object*
8467 : 85535 : Named_object::make_unknown_name(const std::string& name,
8468 : : Location location)
8469 : : {
8470 : 85535 : Named_object* named_object = new Named_object(name, NULL,
8471 : 85535 : NAMED_OBJECT_UNKNOWN);
8472 : 85535 : Unknown_name* value = new Unknown_name(location);
8473 : 85535 : named_object->u_.unknown_value = value;
8474 : 85535 : return named_object;
8475 : : }
8476 : :
8477 : : // Make a constant.
8478 : :
8479 : : Named_object*
8480 : 1008188 : Named_object::make_constant(const Typed_identifier& tid,
8481 : : const Package* package, Expression* expr,
8482 : : int iota_value)
8483 : : {
8484 : 1008188 : Named_object* named_object = new Named_object(tid.name(), package,
8485 : 1008188 : NAMED_OBJECT_CONST);
8486 : 1008188 : Named_constant* named_constant = new Named_constant(tid.type(), expr,
8487 : : iota_value,
8488 : 1008188 : tid.location());
8489 : 1008188 : named_object->u_.const_value = named_constant;
8490 : 1008188 : return named_object;
8491 : : }
8492 : :
8493 : : // Make a named type.
8494 : :
8495 : : Named_object*
8496 : 752527 : Named_object::make_type(const std::string& name, const Package* package,
8497 : : Type* type, Location location)
8498 : : {
8499 : 752527 : Named_object* named_object = new Named_object(name, package,
8500 : 752527 : NAMED_OBJECT_TYPE);
8501 : 752527 : Named_type* named_type = Type::make_named_type(named_object, type, location);
8502 : 752527 : named_object->u_.type_value = named_type;
8503 : 752527 : return named_object;
8504 : : }
8505 : :
8506 : : // Make a type declaration.
8507 : :
8508 : : Named_object*
8509 : 623444 : Named_object::make_type_declaration(const std::string& name,
8510 : : const Package* package,
8511 : : Location location)
8512 : : {
8513 : 623444 : Named_object* named_object = new Named_object(name, package,
8514 : 623444 : NAMED_OBJECT_TYPE_DECLARATION);
8515 : 623444 : Type_declaration* type_declaration = new Type_declaration(location);
8516 : 623444 : named_object->u_.type_declaration = type_declaration;
8517 : 623444 : return named_object;
8518 : : }
8519 : :
8520 : : // Make a variable.
8521 : :
8522 : : Named_object*
8523 : 1742687 : Named_object::make_variable(const std::string& name, const Package* package,
8524 : : Variable* variable)
8525 : : {
8526 : 1742687 : Named_object* named_object = new Named_object(name, package,
8527 : 1742687 : NAMED_OBJECT_VAR);
8528 : 1742687 : named_object->u_.var_value = variable;
8529 : 1742687 : return named_object;
8530 : : }
8531 : :
8532 : : // Make a result variable.
8533 : :
8534 : : Named_object*
8535 : 244723 : Named_object::make_result_variable(const std::string& name,
8536 : : Result_variable* result)
8537 : : {
8538 : 244723 : Named_object* named_object = new Named_object(name, NULL,
8539 : 244723 : NAMED_OBJECT_RESULT_VAR);
8540 : 244723 : named_object->u_.result_var_value = result;
8541 : 244723 : return named_object;
8542 : : }
8543 : :
8544 : : // Make a sink. This is used for the special blank identifier _.
8545 : :
8546 : : Named_object*
8547 : 1385474 : Named_object::make_sink()
8548 : : {
8549 : 1385474 : return new Named_object("_", NULL, NAMED_OBJECT_SINK);
8550 : : }
8551 : :
8552 : : // Make a named function.
8553 : :
8554 : : Named_object*
8555 : 299096 : Named_object::make_function(const std::string& name, const Package* package,
8556 : : Function* function)
8557 : : {
8558 : 299096 : Named_object* named_object = new Named_object(name, package,
8559 : 299096 : NAMED_OBJECT_FUNC);
8560 : 299096 : named_object->u_.func_value = function;
8561 : 299096 : return named_object;
8562 : : }
8563 : :
8564 : : // Make a function declaration.
8565 : :
8566 : : Named_object*
8567 : 4153652 : Named_object::make_function_declaration(const std::string& name,
8568 : : const Package* package,
8569 : : Function_type* fntype,
8570 : : Location location)
8571 : : {
8572 : 4153652 : Named_object* named_object = new Named_object(name, package,
8573 : 4153652 : NAMED_OBJECT_FUNC_DECLARATION);
8574 : 4153652 : Function_declaration *func_decl = new Function_declaration(fntype, location);
8575 : 4153652 : named_object->u_.func_declaration_value = func_decl;
8576 : 4153652 : return named_object;
8577 : : }
8578 : :
8579 : : // Make a package.
8580 : :
8581 : : Named_object*
8582 : 39417 : Named_object::make_package(const std::string& alias, Package* package)
8583 : : {
8584 : 39417 : Named_object* named_object = new Named_object(alias, NULL,
8585 : 39417 : NAMED_OBJECT_PACKAGE);
8586 : 39417 : named_object->u_.package_value = package;
8587 : 39417 : return named_object;
8588 : : }
8589 : :
8590 : : // Return the name to use in an error message.
8591 : :
8592 : : std::string
8593 : 233033 : Named_object::message_name() const
8594 : : {
8595 : 233033 : if (this->package_ == NULL)
8596 : 214929 : return Gogo::message_name(this->name_);
8597 : 18104 : std::string ret;
8598 : 18104 : if (this->package_->has_package_name())
8599 : 18104 : ret = this->package_->package_name();
8600 : : else
8601 : 0 : ret = this->package_->pkgpath();
8602 : 18104 : ret = Gogo::message_name(ret);
8603 : 18104 : ret += '.';
8604 : 18104 : ret += Gogo::message_name(this->name_);
8605 : 18104 : return ret;
8606 : 18104 : }
8607 : :
8608 : : // Set the type when a declaration is defined.
8609 : :
8610 : : void
8611 : 622927 : Named_object::set_type_value(Named_type* named_type)
8612 : : {
8613 : 622927 : go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
8614 : 622927 : Type_declaration* td = this->u_.type_declaration;
8615 : 622927 : td->define_methods(named_type);
8616 : 622927 : unsigned int index;
8617 : 622927 : Named_object* in_function = td->in_function(&index);
8618 : 622927 : if (in_function != NULL)
8619 : 1295 : named_type->set_in_function(in_function, index);
8620 : 624222 : delete td;
8621 : 622927 : this->classification_ = NAMED_OBJECT_TYPE;
8622 : 622927 : this->u_.type_value = named_type;
8623 : 622927 : }
8624 : :
8625 : : // Define a function which was previously declared.
8626 : :
8627 : : void
8628 : 106643 : Named_object::set_function_value(Function* function)
8629 : : {
8630 : 106643 : go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
8631 : 106643 : if (this->func_declaration_value()->has_descriptor())
8632 : : {
8633 : 0 : Expression* descriptor =
8634 : 0 : this->func_declaration_value()->descriptor(NULL, NULL);
8635 : 0 : function->set_descriptor(descriptor);
8636 : : }
8637 : 106643 : this->classification_ = NAMED_OBJECT_FUNC;
8638 : : // FIXME: We should free the old value.
8639 : 106643 : this->u_.func_value = function;
8640 : 106643 : }
8641 : :
8642 : : // Declare an unknown object as a type declaration.
8643 : :
8644 : : void
8645 : 1 : Named_object::declare_as_type()
8646 : : {
8647 : 1 : go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
8648 : 1 : Unknown_name* unk = this->u_.unknown_value;
8649 : 1 : this->classification_ = NAMED_OBJECT_TYPE_DECLARATION;
8650 : 1 : this->u_.type_declaration = new Type_declaration(unk->location());
8651 : 1 : delete unk;
8652 : 1 : }
8653 : :
8654 : : // Return the location of a named object.
8655 : :
8656 : : Location
8657 : 1913315 : Named_object::location() const
8658 : : {
8659 : 1913315 : switch (this->classification_)
8660 : : {
8661 : 0 : default:
8662 : 0 : case NAMED_OBJECT_UNINITIALIZED:
8663 : 0 : go_unreachable();
8664 : :
8665 : 0 : case NAMED_OBJECT_ERRONEOUS:
8666 : 0 : return Linemap::unknown_location();
8667 : :
8668 : 532 : case NAMED_OBJECT_UNKNOWN:
8669 : 532 : return this->unknown_value()->location();
8670 : :
8671 : 12553 : case NAMED_OBJECT_CONST:
8672 : 12553 : return this->const_value()->location();
8673 : :
8674 : 15018 : case NAMED_OBJECT_TYPE:
8675 : 15018 : return this->type_value()->location();
8676 : :
8677 : 0 : case NAMED_OBJECT_TYPE_DECLARATION:
8678 : 0 : return this->type_declaration_value()->location();
8679 : :
8680 : 700894 : case NAMED_OBJECT_VAR:
8681 : 700894 : return this->var_value()->location();
8682 : :
8683 : 244562 : case NAMED_OBJECT_RESULT_VAR:
8684 : 244562 : return this->result_var_value()->location();
8685 : :
8686 : 0 : case NAMED_OBJECT_SINK:
8687 : 0 : go_unreachable();
8688 : :
8689 : 441964 : case NAMED_OBJECT_FUNC:
8690 : 441964 : return this->func_value()->location();
8691 : :
8692 : 458366 : case NAMED_OBJECT_FUNC_DECLARATION:
8693 : 458366 : return this->func_declaration_value()->location();
8694 : :
8695 : 39426 : case NAMED_OBJECT_PACKAGE:
8696 : 39426 : return this->package_value()->location();
8697 : : }
8698 : : }
8699 : :
8700 : : // Traverse a Named_object.
8701 : :
8702 : : int
8703 : 68905509 : Named_object::traverse(Traverse* traverse, bool is_global)
8704 : : {
8705 : 68905509 : const unsigned int traverse_mask = traverse->traverse_mask();
8706 : 68905509 : const unsigned int e_or_t = (Traverse::traverse_expressions
8707 : : | Traverse::traverse_types);
8708 : 68905509 : const unsigned int e_or_t_or_s = (e_or_t
8709 : : | Traverse::traverse_statements);
8710 : :
8711 : 68905509 : int t = TRAVERSE_CONTINUE;
8712 : 68905509 : switch (this->classification_)
8713 : : {
8714 : 20964773 : case Named_object::NAMED_OBJECT_CONST:
8715 : 20964773 : if ((traverse_mask & Traverse::traverse_constants) != 0)
8716 : 1995192 : t = traverse->constant(this, is_global);
8717 : 1995192 : if (t == TRAVERSE_CONTINUE
8718 : 20964773 : && (traverse_mask & e_or_t) != 0)
8719 : : {
8720 : 12975804 : Type* tc = this->const_value()->type();
8721 : 12975804 : if (tc != NULL)
8722 : : {
8723 : 11425514 : if (Type::traverse(tc, traverse) == TRAVERSE_EXIT)
8724 : : return TRAVERSE_EXIT;
8725 : : }
8726 : 12975804 : t = this->const_value()->traverse_expression(traverse);
8727 : : }
8728 : : break;
8729 : :
8730 : 29745187 : case Named_object::NAMED_OBJECT_VAR:
8731 : 29745187 : case Named_object::NAMED_OBJECT_RESULT_VAR:
8732 : 29745187 : if ((traverse_mask & Traverse::traverse_variables) != 0)
8733 : 7659259 : t = traverse->variable(this);
8734 : 7659259 : if (t == TRAVERSE_CONTINUE
8735 : 28037249 : && (traverse_mask & e_or_t) != 0)
8736 : : {
8737 : 22625217 : if (this->is_result_variable() || this->var_value()->has_type())
8738 : : {
8739 : 21983025 : Type* tv = (this->is_variable()
8740 : 21983025 : ? this->var_value()->type()
8741 : 4142715 : : this->result_var_value()->type());
8742 : 21983025 : if (tv != NULL)
8743 : : {
8744 : 21983025 : if (Type::traverse(tv, traverse) == TRAVERSE_EXIT)
8745 : : return TRAVERSE_EXIT;
8746 : : }
8747 : : }
8748 : : }
8749 : 28037093 : if (t == TRAVERSE_CONTINUE
8750 : 28037093 : && (traverse_mask & e_or_t_or_s) != 0
8751 : 26310799 : && this->is_variable())
8752 : 21643942 : t = this->var_value()->traverse_expression(traverse,
8753 : : traverse_mask);
8754 : : break;
8755 : :
8756 : 4278751 : case Named_object::NAMED_OBJECT_FUNC:
8757 : 4278751 : if ((traverse_mask & Traverse::traverse_functions) != 0)
8758 : 2368513 : t = traverse->function(this);
8759 : 2368513 : if (t == TRAVERSE_CONTINUE
8760 : 3029450 : && (traverse_mask
8761 : 3029450 : & (Traverse::traverse_variables
8762 : : | Traverse::traverse_constants
8763 : : | Traverse::traverse_functions
8764 : : | Traverse::traverse_blocks
8765 : : | Traverse::traverse_statements
8766 : : | Traverse::traverse_expressions
8767 : : | Traverse::traverse_types)) != 0)
8768 : 3029450 : t = this->func_value()->traverse(traverse);
8769 : : break;
8770 : :
8771 : 13090477 : case Named_object::NAMED_OBJECT_TYPE:
8772 : 13090477 : if ((traverse_mask & e_or_t) != 0)
8773 : : {
8774 : 8061754 : t = Type::traverse(this->type_value(), traverse);
8775 : 8061754 : if (t == TRAVERSE_EXIT)
8776 : : return TRAVERSE_EXIT;
8777 : :
8778 : : // Traverse the types of any local methods that are declared
8779 : : // but not defined. We will see defined methods as
8780 : : // NAMED_OBJECT_FUNC, but we won't see methods that are only
8781 : : // declared.
8782 : 8061754 : if (this->package_ == NULL
8783 : 8061754 : && this->type_value()->named_type()->local_methods() != NULL)
8784 : : {
8785 : 151118 : const Bindings* methods =
8786 : 151118 : this->type_value()->named_type()->local_methods();
8787 : 844855 : for (Bindings::const_declarations_iterator p =
8788 : 151118 : methods->begin_declarations();
8789 : 844855 : p != methods->end_declarations();
8790 : 693737 : ++p)
8791 : : {
8792 : 693737 : if (p->second->is_function_declaration())
8793 : : {
8794 : 9224 : Type* mt = p->second->func_declaration_value()->type();
8795 : 9224 : if (Type::traverse(mt, traverse) == TRAVERSE_EXIT)
8796 : 156 : return TRAVERSE_EXIT;
8797 : : }
8798 : : }
8799 : : }
8800 : : }
8801 : :
8802 : : break;
8803 : :
8804 : : case Named_object::NAMED_OBJECT_PACKAGE:
8805 : : case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
8806 : : case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
8807 : : case Named_object::NAMED_OBJECT_UNKNOWN:
8808 : : case Named_object::NAMED_OBJECT_ERRONEOUS:
8809 : : break;
8810 : :
8811 : 0 : case Named_object::NAMED_OBJECT_SINK:
8812 : 0 : go_unreachable();
8813 : :
8814 : 0 : default:
8815 : 0 : go_unreachable();
8816 : : }
8817 : :
8818 : : return t;
8819 : : }
8820 : :
8821 : : // Export a named object.
8822 : :
8823 : : void
8824 : 428490 : Named_object::export_named_object(Export* exp) const
8825 : : {
8826 : 428490 : switch (this->classification_)
8827 : : {
8828 : 0 : default:
8829 : 0 : case NAMED_OBJECT_UNINITIALIZED:
8830 : 0 : case NAMED_OBJECT_UNKNOWN:
8831 : 0 : go_unreachable();
8832 : :
8833 : : case NAMED_OBJECT_ERRONEOUS:
8834 : : break;
8835 : :
8836 : 39525 : case NAMED_OBJECT_CONST:
8837 : 39525 : this->const_value()->export_const(exp, this->name_);
8838 : 39525 : break;
8839 : :
8840 : 0 : case NAMED_OBJECT_TYPE:
8841 : : // Types are handled by export::write_types.
8842 : 0 : go_unreachable();
8843 : :
8844 : 0 : case NAMED_OBJECT_TYPE_DECLARATION:
8845 : 0 : go_error_at(this->type_declaration_value()->location(),
8846 : : "attempt to export %qs which was declared but not defined",
8847 : 0 : this->message_name().c_str());
8848 : 0 : break;
8849 : :
8850 : 301334 : case NAMED_OBJECT_FUNC_DECLARATION:
8851 : 301334 : this->func_declaration_value()->export_func(exp, this);
8852 : 301334 : break;
8853 : :
8854 : 8440 : case NAMED_OBJECT_VAR:
8855 : 8440 : this->var_value()->export_var(exp, this);
8856 : 8440 : break;
8857 : :
8858 : 0 : case NAMED_OBJECT_RESULT_VAR:
8859 : 0 : case NAMED_OBJECT_SINK:
8860 : 0 : go_unreachable();
8861 : :
8862 : 79191 : case NAMED_OBJECT_FUNC:
8863 : 79191 : this->func_value()->export_func(exp, this);
8864 : 79191 : break;
8865 : : }
8866 : 428490 : }
8867 : :
8868 : : // Convert a variable to the backend representation.
8869 : :
8870 : : Bvariable*
8871 : 5601816 : Named_object::get_backend_variable(Gogo* gogo, Named_object* function)
8872 : : {
8873 : 5601816 : if (this->classification_ == NAMED_OBJECT_VAR)
8874 : 4440379 : return this->var_value()->get_backend_variable(gogo, function,
8875 : 4440379 : this->package_, this->name_);
8876 : 1161437 : else if (this->classification_ == NAMED_OBJECT_RESULT_VAR)
8877 : 1161437 : return this->result_var_value()->get_backend_variable(gogo, function,
8878 : 1161437 : this->name_);
8879 : : else
8880 : 0 : go_unreachable();
8881 : : }
8882 : :
8883 : : void
8884 : 0 : debug_go_named_object(Named_object* no)
8885 : : {
8886 : 0 : if (no == NULL)
8887 : : {
8888 : 0 : std::cerr << "<null>";
8889 : 0 : return;
8890 : : }
8891 : 0 : std::cerr << "'" << no->name() << "': ";
8892 : 0 : const char *tag;
8893 : 0 : switch (no->classification())
8894 : : {
8895 : : case Named_object::NAMED_OBJECT_UNINITIALIZED:
8896 : : tag = "uninitialized";
8897 : : break;
8898 : 0 : case Named_object::NAMED_OBJECT_ERRONEOUS:
8899 : 0 : tag = "<error>";
8900 : 0 : break;
8901 : 0 : case Named_object::NAMED_OBJECT_UNKNOWN:
8902 : 0 : tag = "<unknown>";
8903 : 0 : break;
8904 : 0 : case Named_object::NAMED_OBJECT_CONST:
8905 : 0 : tag = "constant";
8906 : 0 : break;
8907 : 0 : case Named_object::NAMED_OBJECT_TYPE:
8908 : 0 : tag = "type";
8909 : 0 : break;
8910 : 0 : case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
8911 : 0 : tag = "type_decl";
8912 : 0 : break;
8913 : 0 : case Named_object::NAMED_OBJECT_VAR:
8914 : 0 : tag = "var";
8915 : 0 : break;
8916 : 0 : case Named_object::NAMED_OBJECT_RESULT_VAR:
8917 : 0 : tag = "result_var";
8918 : 0 : break;
8919 : 0 : case Named_object::NAMED_OBJECT_SINK:
8920 : 0 : tag = "<sink>";
8921 : 0 : break;
8922 : 0 : case Named_object::NAMED_OBJECT_FUNC:
8923 : 0 : tag = "func";
8924 : 0 : break;
8925 : 0 : case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
8926 : 0 : tag = "func_decl";
8927 : 0 : break;
8928 : 0 : case Named_object::NAMED_OBJECT_PACKAGE:
8929 : 0 : tag = "package";
8930 : 0 : break;
8931 : 0 : default:
8932 : 0 : tag = "<unknown named object classification>";
8933 : 0 : break;
8934 : 0 : };
8935 : 0 : std::cerr << tag << "\n";
8936 : : }
8937 : :
8938 : : // Get the backend representation for this named object.
8939 : :
8940 : : void
8941 : 349402 : Named_object::get_backend(Gogo* gogo, std::vector<Bexpression*>& const_decls,
8942 : : std::vector<Btype*>& type_decls,
8943 : : std::vector<Bfunction*>& func_decls)
8944 : : {
8945 : : // If this is a definition, avoid trying to get the backend
8946 : : // representation, as that can crash.
8947 : 349402 : if (this->is_redefinition_)
8948 : : {
8949 : 18 : go_assert(saw_errors());
8950 : : return;
8951 : : }
8952 : :
8953 : 349384 : switch (this->classification_)
8954 : : {
8955 : 23399 : case NAMED_OBJECT_CONST:
8956 : 23399 : if (!Gogo::is_erroneous_name(this->name_))
8957 : 23399 : const_decls.push_back(this->u_.const_value->get_backend(gogo, this));
8958 : : break;
8959 : :
8960 : 30656 : case NAMED_OBJECT_TYPE:
8961 : 30656 : {
8962 : 30656 : Named_type* named_type = this->u_.type_value;
8963 : :
8964 : : // No need to do anything for aliases-- whatever has to be done
8965 : : // can be done for the alias target.
8966 : 30656 : if (named_type->is_alias())
8967 : : break;
8968 : :
8969 : 30362 : if (!Gogo::is_erroneous_name(this->name_))
8970 : 30362 : type_decls.push_back(named_type->get_backend(gogo));
8971 : :
8972 : : // We need to produce a type descriptor for every named
8973 : : // type, and for a pointer to every named type, since
8974 : : // other files or packages might refer to them. We need
8975 : : // to do this even for hidden types, because they might
8976 : : // still be returned by some function. Simply calling the
8977 : : // type_descriptor method is enough to create the type
8978 : : // descriptor, even though we don't do anything with it.
8979 : 30362 : if (this->package_ == NULL && !saw_errors())
8980 : : {
8981 : 29544 : named_type->
8982 : 29544 : type_descriptor_pointer(gogo, Linemap::predeclared_location());
8983 : 29544 : Type* pn = Type::make_pointer_type(named_type);
8984 : 29544 : pn->type_descriptor_pointer(gogo, Linemap::predeclared_location());
8985 : 29544 : if (named_type->in_heap())
8986 : : {
8987 : 29319 : named_type->gc_symbol_pointer(gogo);
8988 : 29319 : pn->gc_symbol_pointer(gogo);
8989 : : }
8990 : : }
8991 : : }
8992 : : break;
8993 : :
8994 : 0 : case NAMED_OBJECT_TYPE_DECLARATION:
8995 : 0 : go_error_at(Linemap::unknown_location(),
8996 : : "reference to undefined type %qs",
8997 : 0 : this->message_name().c_str());
8998 : 0 : return;
8999 : :
9000 : 0 : case NAMED_OBJECT_VAR:
9001 : 0 : case NAMED_OBJECT_RESULT_VAR:
9002 : 0 : case NAMED_OBJECT_SINK:
9003 : 0 : go_unreachable();
9004 : :
9005 : 295316 : case NAMED_OBJECT_FUNC:
9006 : 295316 : {
9007 : 295316 : Function* func = this->u_.func_value;
9008 : 295316 : if (!Gogo::is_erroneous_name(this->name_))
9009 : 295316 : func_decls.push_back(func->get_or_make_decl(gogo, this));
9010 : :
9011 : 295316 : if (func->block() != NULL)
9012 : 295316 : func->build(gogo, this);
9013 : : }
9014 : : break;
9015 : :
9016 : : case NAMED_OBJECT_ERRONEOUS:
9017 : : break;
9018 : :
9019 : 0 : default:
9020 : 0 : go_unreachable();
9021 : : }
9022 : : }
9023 : :
9024 : : // Class Bindings.
9025 : :
9026 : 3141787 : Bindings::Bindings(Bindings* enclosing)
9027 : 3141787 : : enclosing_(enclosing), named_objects_(), bindings_()
9028 : : {
9029 : 3141787 : }
9030 : :
9031 : : // Clear imports.
9032 : :
9033 : : void
9034 : 12706 : Bindings::clear_file_scope(Gogo* gogo)
9035 : : {
9036 : 12706 : Contour::iterator p = this->bindings_.begin();
9037 : 3418331 : while (p != this->bindings_.end())
9038 : : {
9039 : 3405625 : bool keep;
9040 : 3405625 : if (p->second->package() != NULL)
9041 : : keep = false;
9042 : 3343124 : else if (p->second->is_package())
9043 : : keep = false;
9044 : 6607418 : else if (p->second->is_function()
9045 : 1308250 : && !p->second->func_value()->type()->is_method()
9046 : 4611959 : && Gogo::unpack_hidden_name(p->second->name()) == "init")
9047 : : keep = false;
9048 : : else
9049 : 3303709 : keep = true;
9050 : :
9051 : 3303709 : if (keep)
9052 : 3303709 : ++p;
9053 : : else
9054 : : {
9055 : 101916 : gogo->add_file_block_name(p->second->name(), p->second->location());
9056 : 101916 : p = this->bindings_.erase(p);
9057 : : }
9058 : : }
9059 : 12706 : }
9060 : :
9061 : : // Look up a symbol.
9062 : :
9063 : : Named_object*
9064 : 8265427 : Bindings::lookup(const std::string& name) const
9065 : : {
9066 : 13215393 : Contour::const_iterator p = this->bindings_.find(name);
9067 : 13215393 : if (p != this->bindings_.end())
9068 : 5474525 : return p->second->resolve();
9069 : 7740868 : else if (this->enclosing_ != NULL)
9070 : : return this->enclosing_->lookup(name);
9071 : : else
9072 : : return NULL;
9073 : : }
9074 : :
9075 : : // Look up a symbol locally.
9076 : :
9077 : : Named_object*
9078 : 643809 : Bindings::lookup_local(const std::string& name) const
9079 : : {
9080 : 643809 : Contour::const_iterator p = this->bindings_.find(name);
9081 : 643809 : if (p == this->bindings_.end())
9082 : : return NULL;
9083 : 334424 : return p->second;
9084 : : }
9085 : :
9086 : : // Remove an object from a set of bindings. This is used for a
9087 : : // special case in thunks for functions which call recover.
9088 : :
9089 : : void
9090 : 831 : Bindings::remove_binding(Named_object* no)
9091 : : {
9092 : 831 : Contour::iterator pb = this->bindings_.find(no->name());
9093 : 831 : go_assert(pb != this->bindings_.end());
9094 : 831 : this->bindings_.erase(pb);
9095 : 1048 : for (std::vector<Named_object*>::iterator pn = this->named_objects_.begin();
9096 : 1048 : pn != this->named_objects_.end();
9097 : 217 : ++pn)
9098 : : {
9099 : 1048 : if (*pn == no)
9100 : : {
9101 : 831 : this->named_objects_.erase(pn);
9102 : 831 : return;
9103 : : }
9104 : : }
9105 : 0 : go_unreachable();
9106 : : }
9107 : :
9108 : : // Add a method to the list of objects. This is not added to the
9109 : : // lookup table. This is so that we have a single list of objects
9110 : : // declared at the top level, which we walk through when it's time to
9111 : : // convert to trees.
9112 : :
9113 : : void
9114 : 78045 : Bindings::add_method(Named_object* method)
9115 : : {
9116 : 78045 : this->named_objects_.push_back(method);
9117 : 78045 : }
9118 : :
9119 : : // Add a generic Named_object to a Contour.
9120 : :
9121 : : Named_object*
9122 : 7470259 : Bindings::add_named_object_to_contour(Contour* contour,
9123 : : Named_object* named_object)
9124 : : {
9125 : 7470259 : go_assert(named_object == named_object->resolve());
9126 : 7470259 : const std::string& name(named_object->name());
9127 : 7470259 : go_assert(!Gogo::is_sink_name(name));
9128 : :
9129 : 7470259 : std::pair<Contour::iterator, bool> ins =
9130 : 7470259 : contour->insert(std::make_pair(name, named_object));
9131 : 7470259 : if (!ins.second)
9132 : : {
9133 : : // The name was already there.
9134 : 825745 : if (named_object->package() != NULL
9135 : 675130 : && ins.first->second->package() == named_object->package()
9136 : 1500875 : && (ins.first->second->classification()
9137 : 675130 : == named_object->classification()))
9138 : : {
9139 : : // This is a second import of the same object.
9140 : : return ins.first->second;
9141 : : }
9142 : 749191 : ins.first->second = this->new_definition(ins.first->second,
9143 : : named_object);
9144 : 749191 : return ins.first->second;
9145 : : }
9146 : : else
9147 : : {
9148 : : // Don't push declarations on the list. We push them on when
9149 : : // and if we find the definitions. That way we genericize the
9150 : : // functions in order.
9151 : 6644514 : if (!named_object->is_type_declaration()
9152 : : && !named_object->is_function_declaration()
9153 : : && !named_object->is_unknown())
9154 : 2742618 : this->named_objects_.push_back(named_object);
9155 : 6644514 : return named_object;
9156 : : }
9157 : : }
9158 : :
9159 : : // We had an existing named object OLD_OBJECT, and we've seen a new
9160 : : // one NEW_OBJECT with the same name. FIXME: This does not free the
9161 : : // new object when we don't need it.
9162 : :
9163 : : Named_object*
9164 : 749191 : Bindings::new_definition(Named_object* old_object, Named_object* new_object)
9165 : : {
9166 : 749191 : if (new_object->is_erroneous() && !old_object->is_erroneous())
9167 : : return new_object;
9168 : :
9169 : 749191 : std::string reason;
9170 : 749191 : switch (old_object->classification())
9171 : : {
9172 : 0 : default:
9173 : 0 : case Named_object::NAMED_OBJECT_UNINITIALIZED:
9174 : 0 : go_unreachable();
9175 : :
9176 : : case Named_object::NAMED_OBJECT_ERRONEOUS:
9177 : : return old_object;
9178 : :
9179 : 55666 : case Named_object::NAMED_OBJECT_UNKNOWN:
9180 : 55666 : {
9181 : 55666 : Named_object* real = old_object->unknown_value()->real_named_object();
9182 : 55666 : if (real != NULL)
9183 : 0 : return this->new_definition(real, new_object);
9184 : 55666 : go_assert(!new_object->is_unknown());
9185 : 55666 : old_object->unknown_value()->set_real_named_object(new_object);
9186 : 55666 : if (!new_object->is_type_declaration()
9187 : 55666 : && !new_object->is_function_declaration())
9188 : 44332 : this->named_objects_.push_back(new_object);
9189 : : return new_object;
9190 : : }
9191 : :
9192 : : case Named_object::NAMED_OBJECT_CONST:
9193 : : break;
9194 : :
9195 : 0 : case Named_object::NAMED_OBJECT_TYPE:
9196 : 0 : if (new_object->is_type_declaration())
9197 : : return old_object;
9198 : : break;
9199 : :
9200 : 586845 : case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
9201 : 586845 : if (new_object->is_type_declaration())
9202 : : return old_object;
9203 : 586329 : if (new_object->is_type())
9204 : : {
9205 : 586329 : old_object->set_type_value(new_object->type_value());
9206 : 586329 : new_object->type_value()->set_named_object(old_object);
9207 : 586329 : this->named_objects_.push_back(old_object);
9208 : 586329 : return old_object;
9209 : : }
9210 : : break;
9211 : :
9212 : 13 : case Named_object::NAMED_OBJECT_VAR:
9213 : 13 : case Named_object::NAMED_OBJECT_RESULT_VAR:
9214 : : // We have already given an error in the parser for cases where
9215 : : // one parameter or result variable redeclares another one.
9216 : 13 : if ((new_object->is_variable()
9217 : 8 : && new_object->var_value()->is_parameter())
9218 : 18 : || new_object->is_result_variable())
9219 : : return old_object;
9220 : : break;
9221 : :
9222 : 0 : case Named_object::NAMED_OBJECT_SINK:
9223 : 0 : go_unreachable();
9224 : :
9225 : : case Named_object::NAMED_OBJECT_FUNC:
9226 : : break;
9227 : :
9228 : 106648 : case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
9229 : 106648 : {
9230 : : // We declare the hash and equality functions before defining
9231 : : // them, because we sometimes see that we need the declaration
9232 : : // while we are in the middle of a different function.
9233 : : //
9234 : : // We declare the main function before the user defines it, to
9235 : : // give better error messages.
9236 : : //
9237 : : // We declare inline functions before we define them, as we
9238 : : // only define them if we need them.
9239 : 213296 : if (new_object->is_function()
9240 : 106648 : && ((Linemap::is_predeclared_location(old_object->location())
9241 : 92632 : && Linemap::is_predeclared_location(new_object->location()))
9242 : 120662 : || (Gogo::unpack_hidden_name(old_object->name()) == "main"
9243 : 1764 : && Linemap::is_unknown_location(old_object->location()))
9244 : 12250 : || (new_object->package() != NULL
9245 : 12247 : && old_object->func_declaration_value()->has_imported_body()
9246 : 12247 : && new_object->func_value()->is_inline_only())))
9247 : : {
9248 : 106643 : Function_type* old_type =
9249 : 106643 : old_object->func_declaration_value()->type();
9250 : 106643 : Function_type* new_type = new_object->func_value()->type();
9251 : 106643 : if (old_type->is_valid_redeclaration(new_type, &reason))
9252 : : {
9253 : 106643 : Function_declaration* fd =
9254 : 106643 : old_object->func_declaration_value();
9255 : 106643 : go_assert(fd->asm_name().empty());
9256 : 106643 : old_object->set_function_value(new_object->func_value());
9257 : 106643 : this->named_objects_.push_back(old_object);
9258 : 106643 : return old_object;
9259 : : }
9260 : : }
9261 : : }
9262 : : break;
9263 : :
9264 : : case Named_object::NAMED_OBJECT_PACKAGE:
9265 : : break;
9266 : : }
9267 : :
9268 : 56 : std::string n = old_object->message_name();
9269 : 28 : if (reason.empty())
9270 : 28 : go_error_at(new_object->location(), "redefinition of %qs", n.c_str());
9271 : : else
9272 : 0 : go_error_at(new_object->location(), "redefinition of %qs: %s", n.c_str(),
9273 : : reason.c_str());
9274 : 28 : old_object->set_is_redefinition();
9275 : 28 : new_object->set_is_redefinition();
9276 : :
9277 : 28 : if (!Linemap::is_unknown_location(old_object->location())
9278 : 28 : && !Linemap::is_predeclared_location(old_object->location()))
9279 : 27 : go_inform(old_object->location(), "previous definition of %qs was here",
9280 : : n.c_str());
9281 : :
9282 : 28 : return old_object;
9283 : 749191 : }
9284 : :
9285 : : // Add a named type.
9286 : :
9287 : : Named_object*
9288 : 97578 : Bindings::add_named_type(Named_type* named_type)
9289 : : {
9290 : 97578 : return this->add_named_object(named_type->named_object());
9291 : : }
9292 : :
9293 : : // Add a function.
9294 : :
9295 : : Named_object*
9296 : 268531 : Bindings::add_function(const std::string& name, const Package* package,
9297 : : Function* function)
9298 : : {
9299 : 268531 : return this->add_named_object(Named_object::make_function(name, package,
9300 : 268531 : function));
9301 : : }
9302 : :
9303 : : // Add a function declaration.
9304 : :
9305 : : Named_object*
9306 : 3242003 : Bindings::add_function_declaration(const std::string& name,
9307 : : const Package* package,
9308 : : Function_type* type,
9309 : : Location location)
9310 : : {
9311 : 3242003 : Named_object* no = Named_object::make_function_declaration(name, package,
9312 : : type, location);
9313 : 3242003 : return this->add_named_object(no);
9314 : : }
9315 : :
9316 : : // Define a type which was previously declared.
9317 : :
9318 : : void
9319 : 31951 : Bindings::define_type(Named_object* no, Named_type* type)
9320 : : {
9321 : 31951 : no->set_type_value(type);
9322 : 31951 : this->named_objects_.push_back(no);
9323 : 31951 : }
9324 : :
9325 : : // Mark all local variables as used. This is used for some types of
9326 : : // parse error.
9327 : :
9328 : : void
9329 : 56 : Bindings::mark_locals_used()
9330 : : {
9331 : 72 : for (std::vector<Named_object*>::iterator p = this->named_objects_.begin();
9332 : 72 : p != this->named_objects_.end();
9333 : 16 : ++p)
9334 : 16 : if ((*p)->is_variable())
9335 : 12 : (*p)->var_value()->set_is_used();
9336 : 56 : }
9337 : :
9338 : : // Traverse bindings.
9339 : :
9340 : : int
9341 : 2404312 : Bindings::traverse(Traverse* traverse, bool is_global)
9342 : : {
9343 : 2404312 : unsigned int traverse_mask = traverse->traverse_mask();
9344 : :
9345 : : // We don't use an iterator because we permit the traversal to add
9346 : : // new global objects.
9347 : 2404312 : const unsigned int e_or_t = (Traverse::traverse_expressions
9348 : : | Traverse::traverse_types);
9349 : 48541883 : for (size_t i = 0; i < this->named_objects_.size(); ++i)
9350 : : {
9351 : 46137571 : Named_object* p = this->named_objects_[i];
9352 : 46137571 : if (p->traverse(traverse, is_global) == TRAVERSE_EXIT)
9353 : : return TRAVERSE_EXIT;
9354 : : }
9355 : :
9356 : : // If we need to traverse types, check the function declarations,
9357 : : // which have types. Also check any methods of a type declaration.
9358 : 2404312 : if ((traverse_mask & e_or_t) != 0)
9359 : : {
9360 : 43773416 : for (Bindings::const_declarations_iterator p =
9361 : 1481293 : this->begin_declarations();
9362 : 43773416 : p != this->end_declarations();
9363 : 42292123 : ++p)
9364 : : {
9365 : 42292123 : if (p->second->is_function_declaration())
9366 : : {
9367 : 15386027 : if (Type::traverse(p->second->func_declaration_value()->type(),
9368 : : traverse)
9369 : : == TRAVERSE_EXIT)
9370 : 2404312 : return TRAVERSE_EXIT;
9371 : : }
9372 : 26906096 : else if (p->second->is_type_declaration())
9373 : : {
9374 : 20 : const std::vector<Named_object*>* methods =
9375 : 20 : p->second->type_declaration_value()->methods();
9376 : 40 : for (std::vector<Named_object*>::const_iterator pm =
9377 : 20 : methods->begin();
9378 : 40 : pm != methods->end();
9379 : 20 : pm++)
9380 : : {
9381 : 20 : Named_object* no = *pm;
9382 : 20 : Type *t;
9383 : 20 : if (no->is_function())
9384 : 20 : t = no->func_value()->type();
9385 : 0 : else if (no->is_function_declaration())
9386 : 0 : t = no->func_declaration_value()->type();
9387 : : else
9388 : 0 : continue;
9389 : 20 : if (Type::traverse(t, traverse) == TRAVERSE_EXIT)
9390 : 0 : return TRAVERSE_EXIT;
9391 : : }
9392 : : }
9393 : : }
9394 : : }
9395 : :
9396 : : // Traverse function declarations when needed.
9397 : 2404312 : if ((traverse_mask & Traverse::traverse_func_declarations) != 0)
9398 : : {
9399 : 3289172 : for (Bindings::const_declarations_iterator p = this->begin_declarations();
9400 : 3289172 : p != this->end_declarations();
9401 : 3176610 : ++p)
9402 : : {
9403 : 3176610 : if (p->second->is_function_declaration())
9404 : : {
9405 : 1149080 : if (traverse->function_declaration(p->second) == TRAVERSE_EXIT)
9406 : 0 : return TRAVERSE_EXIT;
9407 : : }
9408 : : }
9409 : : }
9410 : :
9411 : : return TRAVERSE_CONTINUE;
9412 : : }
9413 : :
9414 : : // Determine types for the objects.
9415 : :
9416 : : void
9417 : 4645 : Bindings::determine_types(Gogo* gogo)
9418 : : {
9419 : : // We don't use an iterator because the traversal can add new
9420 : : // bindings.
9421 : 455307 : for (size_t i = 0; i < this->named_objects_.size(); ++i)
9422 : : {
9423 : 450662 : Named_object* no = this->named_objects_[i];
9424 : 450662 : if (no->is_function())
9425 : 175575 : no->func_value()->determine_types(gogo);
9426 : 275087 : else if (no->is_variable())
9427 : 36701 : no->var_value()->determine_type(gogo);
9428 : 238386 : else if (no->is_const())
9429 : 154484 : no->const_value()->determine_type(gogo);
9430 : :
9431 : : // See if a variable requires us to build an initialization
9432 : : // function. We know that we will see all global variables
9433 : : // here.
9434 : 450662 : if (!gogo->need_init_fn() && no->is_variable())
9435 : : {
9436 : 2937 : Variable* variable = no->var_value();
9437 : :
9438 : : // If this is a global variable which requires runtime
9439 : : // initialization, we need an initialization function.
9440 : :
9441 : 2937 : if (!variable->is_global())
9442 : 0 : continue;
9443 : :
9444 : 2937 : if (variable->init() == NULL)
9445 : : ;
9446 : 2258 : else if (variable->type()->interface_type() != NULL)
9447 : 253 : gogo->set_need_init_fn();
9448 : 2005 : else if (variable->init()->is_constant())
9449 : : ;
9450 : 1573 : else if (!variable->init()->is_composite_literal())
9451 : 494 : gogo->set_need_init_fn();
9452 : 1079 : else if (variable->init()->is_nonconstant_composite_literal())
9453 : 0 : gogo->set_need_init_fn();
9454 : :
9455 : : // If this global variable holds a pointer value, we need an
9456 : : // initialization function to register it as a GC root.
9457 : 2937 : if (variable->type()->has_pointer())
9458 : 1716 : gogo->set_need_init_fn();
9459 : : }
9460 : : }
9461 : 4645 : }
9462 : :
9463 : : void
9464 : 0 : Bindings::debug_dump()
9465 : : {
9466 : 0 : std::set<Named_object*> defs;
9467 : 0 : for (size_t i = 0; i < this->named_objects_.size(); ++i)
9468 : 0 : defs.insert(this->named_objects_[i]);
9469 : 0 : for (Contour::iterator p = this->bindings_.begin();
9470 : 0 : p != this->bindings_.end();
9471 : 0 : ++p)
9472 : : {
9473 : 0 : const char* tag = " ";
9474 : 0 : if (defs.find(p->second) != defs.end())
9475 : 0 : tag = "* ";
9476 : 0 : std::cerr << tag;
9477 : 0 : debug_go_named_object(p->second);
9478 : : }
9479 : 0 : }
9480 : :
9481 : : void
9482 : 0 : debug_go_bindings(Bindings* bindings)
9483 : : {
9484 : 0 : if (bindings != NULL)
9485 : 0 : bindings->debug_dump();
9486 : 0 : }
9487 : :
9488 : : // Class Label.
9489 : :
9490 : : // Clear any references to this label.
9491 : :
9492 : : void
9493 : 10524 : Label::clear_refs()
9494 : : {
9495 : 12089 : for (std::vector<Bindings_snapshot*>::iterator p = this->refs_.begin();
9496 : 12089 : p != this->refs_.end();
9497 : 1565 : ++p)
9498 : 3130 : delete *p;
9499 : 10524 : this->refs_.clear();
9500 : 10524 : }
9501 : :
9502 : : // Get the backend representation for a label.
9503 : :
9504 : : Blabel*
9505 : 30094 : Label::get_backend_label(Translate_context* context)
9506 : : {
9507 : 30094 : if (this->blabel_ == NULL)
9508 : : {
9509 : 10412 : Function* function = context->function()->func_value();
9510 : 10412 : Bfunction* bfunction = function->get_decl();
9511 : 10412 : this->blabel_ = context->backend()->label(bfunction, this->name_,
9512 : : this->location_);
9513 : : }
9514 : 30094 : return this->blabel_;
9515 : : }
9516 : :
9517 : : // Return an expression for the address of this label.
9518 : :
9519 : : Bexpression*
9520 : 8840 : Label::get_addr(Translate_context* context, Location location)
9521 : : {
9522 : 8840 : Blabel* label = this->get_backend_label(context);
9523 : 8840 : return context->backend()->label_address(label, location);
9524 : : }
9525 : :
9526 : : // Return the dummy label that represents any instance of the blank label.
9527 : :
9528 : : Label*
9529 : 3 : Label::create_dummy_label()
9530 : : {
9531 : 3 : static Label* dummy_label;
9532 : 3 : if (dummy_label == NULL)
9533 : : {
9534 : 2 : dummy_label = new Label("_");
9535 : 2 : dummy_label->set_is_used();
9536 : : }
9537 : 3 : return dummy_label;
9538 : : }
9539 : :
9540 : : // Class Unnamed_label.
9541 : :
9542 : : // Get the backend representation for an unnamed label.
9543 : :
9544 : : Blabel*
9545 : 398195 : Unnamed_label::get_blabel(Translate_context* context)
9546 : : {
9547 : 398195 : if (this->blabel_ == NULL)
9548 : : {
9549 : 174399 : Function* function = context->function()->func_value();
9550 : 174399 : Bfunction* bfunction = function->get_decl();
9551 : 174399 : this->blabel_ = context->backend()->label(bfunction, "",
9552 : : this->location_);
9553 : : }
9554 : 398195 : return this->blabel_;
9555 : : }
9556 : :
9557 : : // Return a statement which defines this unnamed label.
9558 : :
9559 : : Bstatement*
9560 : 174399 : Unnamed_label::get_definition(Translate_context* context)
9561 : : {
9562 : 174399 : Blabel* blabel = this->get_blabel(context);
9563 : 174399 : return context->backend()->label_definition_statement(blabel);
9564 : : }
9565 : :
9566 : : // Return a goto statement to this unnamed label.
9567 : :
9568 : : Bstatement*
9569 : 223796 : Unnamed_label::get_goto(Translate_context* context, Location location)
9570 : : {
9571 : 223796 : Blabel* blabel = this->get_blabel(context);
9572 : 223796 : return context->backend()->goto_statement(blabel, location);
9573 : : }
9574 : :
9575 : : // Class Package.
9576 : :
9577 : 116641 : Package::Package(const std::string& pkgpath,
9578 : 116641 : const std::string& pkgpath_symbol, Location location)
9579 : 116641 : : pkgpath_(pkgpath), pkgpath_symbol_(pkgpath_symbol),
9580 : 116641 : package_name_(), bindings_(new Bindings(NULL)),
9581 : 116641 : location_(location)
9582 : : {
9583 : 116641 : go_assert(!pkgpath.empty());
9584 : 116641 : }
9585 : :
9586 : : // Set the package name.
9587 : :
9588 : : void
9589 : 244824 : Package::set_package_name(const std::string& package_name, Location location)
9590 : : {
9591 : 244824 : go_assert(!package_name.empty());
9592 : 244824 : if (this->package_name_.empty())
9593 : 116641 : this->package_name_ = package_name;
9594 : 128183 : else if (this->package_name_ != package_name)
9595 : 0 : go_error_at(location,
9596 : : ("saw two different packages with "
9597 : : "the same package path %s: %s, %s"),
9598 : : this->pkgpath_.c_str(), this->package_name_.c_str(),
9599 : : package_name.c_str());
9600 : 244824 : }
9601 : :
9602 : : // Return the pkgpath symbol, which is a prefix for symbols defined in
9603 : : // this package.
9604 : :
9605 : : std::string
9606 : 194268 : Package::pkgpath_symbol() const
9607 : : {
9608 : 194268 : if (this->pkgpath_symbol_.empty())
9609 : 101672 : return Gogo::pkgpath_for_symbol(this->pkgpath_);
9610 : 92596 : return this->pkgpath_symbol_;
9611 : : }
9612 : :
9613 : : // Set the package path symbol.
9614 : :
9615 : : void
9616 : 14338 : Package::set_pkgpath_symbol(const std::string& pkgpath_symbol)
9617 : : {
9618 : 14338 : go_assert(!pkgpath_symbol.empty());
9619 : 14338 : if (this->pkgpath_symbol_.empty())
9620 : 11763 : this->pkgpath_symbol_ = pkgpath_symbol;
9621 : : else
9622 : 2575 : go_assert(this->pkgpath_symbol_ == pkgpath_symbol);
9623 : 14338 : }
9624 : :
9625 : : // Note that symbol from this package was and qualified by ALIAS.
9626 : :
9627 : : void
9628 : 322202 : Package::note_usage(const std::string& alias) const
9629 : : {
9630 : 322202 : Aliases::const_iterator p = this->aliases_.find(alias);
9631 : 322202 : go_assert(p != this->aliases_.end());
9632 : 322202 : p->second->note_usage();
9633 : 322202 : }
9634 : :
9635 : : // Forget a given usage. If forgetting this usage means this package becomes
9636 : : // unused, report that error.
9637 : :
9638 : : void
9639 : 39 : Package::forget_usage(Expression* usage) const
9640 : : {
9641 : 39 : if (this->fake_uses_.empty())
9642 : 39 : return;
9643 : :
9644 : 0 : std::set<Expression*>::iterator p = this->fake_uses_.find(usage);
9645 : 0 : go_assert(p != this->fake_uses_.end());
9646 : 0 : this->fake_uses_.erase(p);
9647 : :
9648 : 0 : if (this->fake_uses_.empty())
9649 : 0 : go_error_at(this->location(), "imported and not used: %s",
9650 : 0 : Gogo::message_name(this->package_name()).c_str());
9651 : : }
9652 : :
9653 : : // Clear the used field for the next file. If the only usages of this package
9654 : : // are possibly fake, keep the fake usages for lowering.
9655 : :
9656 : : void
9657 : 362497 : Package::clear_used()
9658 : : {
9659 : 362497 : std::string dot_alias = "." + this->package_name();
9660 : 362497 : Aliases::const_iterator p = this->aliases_.find(dot_alias);
9661 : 362497 : if (p != this->aliases_.end() && p->second->used() > this->fake_uses_.size())
9662 : 335 : this->fake_uses_.clear();
9663 : :
9664 : 362497 : this->aliases_.clear();
9665 : 362497 : }
9666 : :
9667 : : Package_alias*
9668 : 39756 : Package::add_alias(const std::string& alias, Location location)
9669 : : {
9670 : 39756 : Aliases::const_iterator p = this->aliases_.find(alias);
9671 : 39756 : if (p == this->aliases_.end())
9672 : : {
9673 : 39756 : std::pair<Aliases::iterator, bool> ret;
9674 : 39756 : ret = this->aliases_.insert(std::make_pair(alias,
9675 : 79512 : new Package_alias(location)));
9676 : 39756 : p = ret.first;
9677 : : }
9678 : 39756 : return p->second;
9679 : : }
9680 : :
9681 : : // Determine types of constants. Everything else in a package
9682 : : // (variables, function declarations) should already have a fixed
9683 : : // type. Constants may have abstract types.
9684 : :
9685 : : void
9686 : 116244 : Package::determine_types(Gogo* gogo)
9687 : : {
9688 : 116244 : Bindings* bindings = this->bindings_;
9689 : 2306201 : for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
9690 : 2306201 : p != bindings->end_definitions();
9691 : 2189957 : ++p)
9692 : : {
9693 : 2189957 : if ((*p)->is_const())
9694 : 991882 : (*p)->const_value()->determine_type(gogo);
9695 : : }
9696 : 116244 : }
9697 : :
9698 : : // Class Traverse.
9699 : :
9700 : : // Destructor.
9701 : :
9702 : 17867263 : Traverse::~Traverse()
9703 : : {
9704 : 17867263 : if (this->types_seen_ != NULL)
9705 : 16805350 : delete this->types_seen_;
9706 : 17867263 : if (this->expressions_seen_ != NULL)
9707 : 1342330 : delete this->expressions_seen_;
9708 : 17867263 : }
9709 : :
9710 : : // Record that we are looking at a type, and return true if we have
9711 : : // already seen it.
9712 : :
9713 : : bool
9714 : 1186019762 : Traverse::remember_type(const Type* type)
9715 : : {
9716 : 1186019762 : if (type->is_error_type())
9717 : : return true;
9718 : 1186009651 : go_assert((this->traverse_mask() & traverse_types) != 0
9719 : : || (this->traverse_mask() & traverse_expressions) != 0);
9720 : : // We mostly only have to remember named types. But it turns out
9721 : : // that an interface type can refer to itself without using a name
9722 : : // by relying on interface inheritance, as in
9723 : : //
9724 : : // type I interface { F() interface{I} }
9725 : : //
9726 : : // Similarly it is possible for array types to refer to themselves
9727 : : // without a name, e.g.
9728 : : //
9729 : : // var x [uintptr(unsafe.Sizeof(&x))]byte
9730 : : //
9731 : 1186009651 : if (type->classification() != Type::TYPE_NAMED
9732 : 601800499 : && type->classification() != Type::TYPE_ARRAY
9733 : 1708069184 : && type->classification() != Type::TYPE_INTERFACE)
9734 : : return false;
9735 : 677803016 : if (this->types_seen_ == NULL)
9736 : 8402675 : this->types_seen_ = new Types_seen();
9737 : 677803016 : std::pair<Types_seen::iterator, bool> ins = this->types_seen_->insert(type);
9738 : 677803016 : return !ins.second;
9739 : : }
9740 : :
9741 : : // Record that we are looking at an expression, and return true if we
9742 : : // have already seen it. NB: this routine used to assert if the traverse
9743 : : // mask did not include expressions/types -- this is no longer the case,
9744 : : // since it can be useful to remember specific expressions during
9745 : : // walks that only cover statements.
9746 : :
9747 : : bool
9748 : 3237533 : Traverse::remember_expression(const Expression* expression)
9749 : : {
9750 : 3237533 : if (this->expressions_seen_ == NULL)
9751 : 671165 : this->expressions_seen_ = new Expressions_seen();
9752 : 3237533 : std::pair<Expressions_seen::iterator, bool> ins =
9753 : 3237533 : this->expressions_seen_->insert(expression);
9754 : 3237533 : return !ins.second;
9755 : : }
9756 : :
9757 : : // The default versions of these functions should never be called: the
9758 : : // traversal mask indicates which functions may be called.
9759 : :
9760 : : int
9761 : 0 : Traverse::variable(Named_object*)
9762 : : {
9763 : 0 : go_unreachable();
9764 : : }
9765 : :
9766 : : int
9767 : 0 : Traverse::constant(Named_object*, bool)
9768 : : {
9769 : 0 : go_unreachable();
9770 : : }
9771 : :
9772 : : int
9773 : 0 : Traverse::function(Named_object*)
9774 : : {
9775 : 0 : go_unreachable();
9776 : : }
9777 : :
9778 : : int
9779 : 0 : Traverse::block(Block*)
9780 : : {
9781 : 0 : go_unreachable();
9782 : : }
9783 : :
9784 : : int
9785 : 0 : Traverse::statement(Block*, size_t*, Statement*)
9786 : : {
9787 : 0 : go_unreachable();
9788 : : }
9789 : :
9790 : : int
9791 : 0 : Traverse::expression(Expression**)
9792 : : {
9793 : 0 : go_unreachable();
9794 : : }
9795 : :
9796 : : int
9797 : 0 : Traverse::type(Type*)
9798 : : {
9799 : 0 : go_unreachable();
9800 : : }
9801 : :
9802 : : int
9803 : 0 : Traverse::function_declaration(Named_object*)
9804 : : {
9805 : 0 : go_unreachable();
9806 : : }
9807 : :
9808 : : // Class Statement_inserter.
9809 : :
9810 : : void
9811 : 1159926 : Statement_inserter::insert(Statement* s)
9812 : : {
9813 : 1159926 : if (this->statements_added_ != NULL)
9814 : 177892 : this->statements_added_->insert(s);
9815 : :
9816 : 1159926 : if (this->block_ != NULL)
9817 : : {
9818 : 1128108 : go_assert(this->pindex_ != NULL);
9819 : 1128108 : this->block_->insert_statement_before(*this->pindex_, s);
9820 : 1128108 : ++*this->pindex_;
9821 : : }
9822 : 31818 : else if (this->var_ != NULL)
9823 : 31818 : this->var_->add_preinit_statement(this->gogo_, s);
9824 : : else
9825 : 0 : go_assert(saw_errors());
9826 : 1159926 : }
|