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 : 4642 : Gogo::Gogo(Backend* backend, Linemap* linemap, int, int pointer_size)
31 : 4642 : : backend_(backend),
32 : 4642 : linemap_(linemap),
33 : 4642 : package_(NULL),
34 : 4642 : functions_(),
35 : 4642 : globals_(new Bindings(NULL)),
36 : 4642 : file_block_names_(),
37 : 4642 : imports_(),
38 : 4642 : imported_unsafe_(false),
39 : 4642 : current_file_imported_unsafe_(false),
40 : 4642 : current_file_imported_embed_(false),
41 : 4642 : packages_(),
42 : 4642 : init_functions_(),
43 : 4642 : var_deps_(),
44 : 4642 : need_init_fn_(false),
45 : 4642 : init_fn_name_(),
46 : 4642 : imported_init_fns_(),
47 : 4642 : pkgpath_(),
48 : 4642 : pkgpath_symbol_(),
49 : 4642 : prefix_(),
50 : 4642 : pkgpath_set_(false),
51 : 4642 : pkgpath_from_option_(false),
52 : 4642 : prefix_from_option_(false),
53 : 4642 : relative_import_path_(),
54 : 4642 : c_header_(),
55 : 4642 : import_map_(),
56 : 4642 : package_file_(),
57 : 4642 : embed_patterns_(),
58 : 4642 : embed_files_(),
59 : 4642 : check_divide_by_zero_(true),
60 : 4642 : check_divide_overflow_(true),
61 : 4642 : compiling_runtime_(false),
62 : 4642 : debug_escape_level_(0),
63 : 4642 : debug_optimization_(false),
64 : 4642 : nil_check_size_threshold_(4096),
65 : 4642 : need_eqtype_(false),
66 : 4642 : verify_types_(),
67 : 4642 : interface_types_(),
68 : 4642 : specific_type_functions_(),
69 : 4642 : specific_type_functions_are_written_(false),
70 : 4642 : named_types_are_converted_(false),
71 : 4642 : analysis_sets_(),
72 : 4642 : gc_roots_(),
73 : 4642 : type_descriptors_(),
74 : 4642 : imported_inlinable_functions_(),
75 : 4642 : imported_inline_functions_()
76 : : {
77 : 4642 : const Location loc = Linemap::predeclared_location();
78 : :
79 : 4642 : Named_type* uint8_type = Type::make_integer_type("uint8", true, 8,
80 : : RUNTIME_TYPE_KIND_UINT8);
81 : 4642 : this->add_named_type(uint8_type);
82 : 4642 : this->add_named_type(Type::make_integer_type("uint16", true, 16,
83 : : RUNTIME_TYPE_KIND_UINT16));
84 : 4642 : this->add_named_type(Type::make_integer_type("uint32", true, 32,
85 : : RUNTIME_TYPE_KIND_UINT32));
86 : 4642 : this->add_named_type(Type::make_integer_type("uint64", true, 64,
87 : : RUNTIME_TYPE_KIND_UINT64));
88 : :
89 : 4642 : this->add_named_type(Type::make_integer_type("int8", false, 8,
90 : : RUNTIME_TYPE_KIND_INT8));
91 : 4642 : this->add_named_type(Type::make_integer_type("int16", false, 16,
92 : : RUNTIME_TYPE_KIND_INT16));
93 : 4642 : Named_type* int32_type = Type::make_integer_type("int32", false, 32,
94 : : RUNTIME_TYPE_KIND_INT32);
95 : 4642 : this->add_named_type(int32_type);
96 : 4642 : this->add_named_type(Type::make_integer_type("int64", false, 64,
97 : : RUNTIME_TYPE_KIND_INT64));
98 : :
99 : 4642 : this->add_named_type(Type::make_float_type("float32", 32,
100 : : RUNTIME_TYPE_KIND_FLOAT32));
101 : 4642 : this->add_named_type(Type::make_float_type("float64", 64,
102 : : RUNTIME_TYPE_KIND_FLOAT64));
103 : :
104 : 4642 : this->add_named_type(Type::make_complex_type("complex64", 64,
105 : : RUNTIME_TYPE_KIND_COMPLEX64));
106 : 4642 : this->add_named_type(Type::make_complex_type("complex128", 128,
107 : : RUNTIME_TYPE_KIND_COMPLEX128));
108 : :
109 : 4642 : int int_type_size = pointer_size;
110 : 4642 : if (int_type_size < 32)
111 : : int_type_size = 32;
112 : 4642 : this->add_named_type(Type::make_integer_type("uint", true,
113 : : int_type_size,
114 : : RUNTIME_TYPE_KIND_UINT));
115 : 4642 : Named_type* int_type = Type::make_integer_type("int", false, int_type_size,
116 : : RUNTIME_TYPE_KIND_INT);
117 : 4642 : this->add_named_type(int_type);
118 : :
119 : 4642 : 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 : 9284 : uint8_type->integer_type()->set_is_byte();
125 : 4642 : this->add_named_type(Type::make_integer_type_alias("byte", uint8_type));
126 : :
127 : : // "rune" is an alias for "int32".
128 : 9284 : int32_type->integer_type()->set_is_rune();
129 : 4642 : this->add_named_type(Type::make_integer_type_alias("rune", int32_type));
130 : :
131 : 4642 : this->add_named_type(Type::make_named_bool_type());
132 : :
133 : 4642 : this->add_named_type(Type::make_named_string_type());
134 : :
135 : : // "error" is interface { Error() string }.
136 : 4642 : {
137 : 4642 : Typed_identifier_list *methods = new Typed_identifier_list;
138 : 4642 : Typed_identifier_list *results = new Typed_identifier_list;
139 : 4642 : results->push_back(Typed_identifier("", Type::lookup_string_type(), loc));
140 : 4642 : Type *method_type = Type::make_function_type(NULL, NULL, results, loc);
141 : 4642 : methods->push_back(Typed_identifier("Error", method_type, loc));
142 : 4642 : Interface_type *error_iface = Type::make_interface_type(methods, loc);
143 : 4642 : error_iface->finalize_methods();
144 : 4642 : Named_type *error_type = Named_object::make_type("error", NULL, error_iface, loc)->type_value();
145 : 4642 : this->add_named_type(error_type);
146 : : }
147 : :
148 : : // "any" is an alias for the empty interface type.
149 : 4642 : {
150 : 4642 : Type* empty = Type::make_empty_interface_type(loc);
151 : 4642 : Named_object* no = Named_object::make_type("any", NULL, empty, loc);
152 : 4642 : Named_type* nt = no->type_value();
153 : 4642 : nt->set_is_alias();
154 : 4642 : this->add_named_type(nt);
155 : : }
156 : :
157 : 4642 : this->globals_->add_constant(Typed_identifier("true",
158 : : Type::make_boolean_type(),
159 : : loc),
160 : : NULL,
161 : : Expression::make_boolean(true, loc),
162 : : 0);
163 : 4642 : this->globals_->add_constant(Typed_identifier("false",
164 : : Type::make_boolean_type(),
165 : : loc),
166 : : NULL,
167 : : Expression::make_boolean(false, loc),
168 : : 0);
169 : :
170 : 4642 : this->globals_->add_constant(Typed_identifier("nil", Type::make_nil_type(),
171 : : loc),
172 : : NULL,
173 : : Expression::make_nil(loc),
174 : : 0);
175 : :
176 : 4642 : Type* abstract_int_type = Type::make_abstract_integer_type();
177 : 4642 : this->globals_->add_constant(Typed_identifier("iota", abstract_int_type,
178 : : loc),
179 : : NULL,
180 : : Expression::make_iota(),
181 : : 0);
182 : :
183 : 4642 : Function_type* new_type = Type::make_function_type(NULL, NULL, NULL, loc);
184 : 4642 : new_type->set_is_varargs();
185 : 4642 : new_type->set_is_builtin();
186 : 4642 : this->globals_->add_function_declaration("new", NULL, new_type, loc);
187 : :
188 : 4642 : Function_type* make_type = Type::make_function_type(NULL, NULL, NULL, loc);
189 : 4642 : make_type->set_is_varargs();
190 : 4642 : make_type->set_is_builtin();
191 : 4642 : this->globals_->add_function_declaration("make", NULL, make_type, loc);
192 : :
193 : 4642 : Typed_identifier_list* len_result = new Typed_identifier_list();
194 : 4642 : len_result->push_back(Typed_identifier("", int_type, loc));
195 : 4642 : Function_type* len_type = Type::make_function_type(NULL, NULL, len_result,
196 : : loc);
197 : 4642 : len_type->set_is_builtin();
198 : 4642 : this->globals_->add_function_declaration("len", NULL, len_type, loc);
199 : :
200 : 4642 : Typed_identifier_list* cap_result = new Typed_identifier_list();
201 : 4642 : cap_result->push_back(Typed_identifier("", int_type, loc));
202 : 4642 : Function_type* cap_type = Type::make_function_type(NULL, NULL, len_result,
203 : : loc);
204 : 4642 : cap_type->set_is_builtin();
205 : 4642 : this->globals_->add_function_declaration("cap", NULL, cap_type, loc);
206 : :
207 : 4642 : Function_type* print_type = Type::make_function_type(NULL, NULL, NULL, loc);
208 : 4642 : print_type->set_is_varargs();
209 : 4642 : print_type->set_is_builtin();
210 : 4642 : this->globals_->add_function_declaration("print", NULL, print_type, loc);
211 : :
212 : 4642 : print_type = Type::make_function_type(NULL, NULL, NULL, loc);
213 : 4642 : print_type->set_is_varargs();
214 : 4642 : print_type->set_is_builtin();
215 : 4642 : this->globals_->add_function_declaration("println", NULL, print_type, loc);
216 : :
217 : 4642 : Type *empty = Type::make_empty_interface_type(loc);
218 : 4642 : Typed_identifier_list* panic_parms = new Typed_identifier_list();
219 : 4642 : panic_parms->push_back(Typed_identifier("e", empty, loc));
220 : 4642 : Function_type *panic_type = Type::make_function_type(NULL, panic_parms,
221 : : NULL, loc);
222 : 4642 : panic_type->set_is_builtin();
223 : 4642 : this->globals_->add_function_declaration("panic", NULL, panic_type, loc);
224 : :
225 : 4642 : Typed_identifier_list* recover_result = new Typed_identifier_list();
226 : 4642 : recover_result->push_back(Typed_identifier("", empty, loc));
227 : 4642 : Function_type* recover_type = Type::make_function_type(NULL, NULL,
228 : : recover_result,
229 : : loc);
230 : 4642 : recover_type->set_is_builtin();
231 : 4642 : this->globals_->add_function_declaration("recover", NULL, recover_type, loc);
232 : :
233 : 4642 : Function_type* close_type = Type::make_function_type(NULL, NULL, NULL, loc);
234 : 4642 : close_type->set_is_varargs();
235 : 4642 : close_type->set_is_builtin();
236 : 4642 : this->globals_->add_function_declaration("close", NULL, close_type, loc);
237 : :
238 : 4642 : Typed_identifier_list* copy_result = new Typed_identifier_list();
239 : 4642 : copy_result->push_back(Typed_identifier("", int_type, loc));
240 : 4642 : Function_type* copy_type = Type::make_function_type(NULL, NULL,
241 : : copy_result, loc);
242 : 4642 : copy_type->set_is_varargs();
243 : 4642 : copy_type->set_is_builtin();
244 : 4642 : this->globals_->add_function_declaration("copy", NULL, copy_type, loc);
245 : :
246 : 4642 : Function_type* append_type = Type::make_function_type(NULL, NULL, NULL, loc);
247 : 4642 : append_type->set_is_varargs();
248 : 4642 : append_type->set_is_builtin();
249 : 4642 : this->globals_->add_function_declaration("append", NULL, append_type, loc);
250 : :
251 : 4642 : Function_type* complex_type = Type::make_function_type(NULL, NULL, NULL, loc);
252 : 4642 : complex_type->set_is_varargs();
253 : 4642 : complex_type->set_is_builtin();
254 : 4642 : this->globals_->add_function_declaration("complex", NULL, complex_type, loc);
255 : :
256 : 4642 : Function_type* real_type = Type::make_function_type(NULL, NULL, NULL, loc);
257 : 4642 : real_type->set_is_varargs();
258 : 4642 : real_type->set_is_builtin();
259 : 4642 : this->globals_->add_function_declaration("real", NULL, real_type, loc);
260 : :
261 : 4642 : Function_type* imag_type = Type::make_function_type(NULL, NULL, NULL, loc);
262 : 4642 : imag_type->set_is_varargs();
263 : 4642 : imag_type->set_is_builtin();
264 : 4642 : this->globals_->add_function_declaration("imag", NULL, imag_type, loc);
265 : :
266 : 4642 : Function_type* delete_type = Type::make_function_type(NULL, NULL, NULL, loc);
267 : 4642 : delete_type->set_is_varargs();
268 : 4642 : delete_type->set_is_builtin();
269 : 4642 : this->globals_->add_function_declaration("delete", NULL, delete_type, loc);
270 : 4642 : }
271 : :
272 : : std::string
273 : 241574 : Gogo::pkgpath_for_symbol(const std::string& pkgpath)
274 : : {
275 : 241574 : go_assert(!pkgpath.empty());
276 : 241574 : return go_encode_id(pkgpath);
277 : : }
278 : :
279 : : // Return a hash code for a string, given a starting hash.
280 : :
281 : : unsigned int
282 : 13115269 : Gogo::hash_string(const std::string& s, unsigned int h)
283 : : {
284 : 13115269 : const char* p = s.data();
285 : 13115269 : size_t len = s.length();
286 : 116411242 : for (; len > 0; --len)
287 : : {
288 : 103295973 : h ^= *p++;
289 : 103295973 : h*= 16777619;
290 : : }
291 : 13115269 : 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 : 7557465 : Gogo::pkgpath() const
299 : : {
300 : 7557465 : go_assert(this->pkgpath_set_);
301 : 7557465 : return this->pkgpath_;
302 : : }
303 : :
304 : : // Set the package path from the -fgo-pkgpath command line option.
305 : :
306 : : void
307 : 2012 : Gogo::set_pkgpath(const std::string& arg)
308 : : {
309 : 2012 : go_assert(!this->pkgpath_set_);
310 : 2012 : this->pkgpath_ = arg;
311 : 2012 : this->pkgpath_set_ = true;
312 : 2012 : this->pkgpath_from_option_ = true;
313 : 2012 : }
314 : :
315 : : // Get the package path to use for symbol names.
316 : :
317 : : const std::string&
318 : 199130 : Gogo::pkgpath_symbol() const
319 : : {
320 : 199130 : go_assert(this->pkgpath_set_);
321 : 199130 : 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 : 274722 : Gogo::message_name(const std::string& name)
339 : : {
340 : 274722 : return go_localize_identifier(Gogo::unpack_hidden_name(name).c_str());
341 : : }
342 : :
343 : : // Get the package name.
344 : :
345 : : const std::string&
346 : 349337 : Gogo::package_name() const
347 : : {
348 : 349337 : go_assert(this->package_ != NULL);
349 : 349337 : return this->package_->package_name();
350 : : }
351 : :
352 : : // Set the package name.
353 : :
354 : : void
355 : 12703 : Gogo::set_package_name(const std::string& package_name,
356 : : Location location)
357 : : {
358 : 12703 : if (this->package_ != NULL)
359 : : {
360 : 8061 : if (this->package_->package_name() != package_name)
361 : 0 : go_error_at(location, "expected package %<%s%>",
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 : 4642 : if (this->pkgpath_set_)
370 : 2012 : this->pkgpath_symbol_ = Gogo::pkgpath_for_symbol(this->pkgpath_);
371 : : else
372 : : {
373 : 2630 : if (!this->prefix_from_option_ && package_name == "main")
374 : : {
375 : 1888 : this->pkgpath_ = package_name;
376 : 1888 : this->pkgpath_symbol_ = Gogo::pkgpath_for_symbol(package_name);
377 : : }
378 : : else
379 : : {
380 : 742 : if (!this->prefix_from_option_)
381 : 742 : this->prefix_ = "go";
382 : 742 : this->pkgpath_ = this->prefix_ + '.' + package_name;
383 : 1484 : this->pkgpath_symbol_ = (Gogo::pkgpath_for_symbol(this->prefix_) + '.'
384 : 2226 : + Gogo::pkgpath_for_symbol(package_name));
385 : : }
386 : 2630 : this->pkgpath_set_ = true;
387 : : }
388 : :
389 : 9284 : this->package_ = this->register_package(this->pkgpath_,
390 : 4642 : this->pkgpath_symbol_, location);
391 : 4642 : this->package_->set_package_name(package_name, location);
392 : :
393 : 4642 : if (this->is_main_package())
394 : : {
395 : : // Declare "main" as a function which takes no parameters and
396 : : // returns no value.
397 : 1888 : Location uloc = Linemap::unknown_location();
398 : 1888 : 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 : 39008 : Gogo::is_main_package() const
409 : : {
410 : 39008 : return (this->package_name() == "main"
411 : 19469 : && !this->pkgpath_from_option_
412 : 58425 : && !this->prefix_from_option_);
413 : : }
414 : :
415 : : // Import a package.
416 : :
417 : : void
418 : 42240 : 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 : 42240 : if (filename.empty())
425 : : {
426 : 1 : go_error_at(location, "import path is empty");
427 : 20435 : return;
428 : : }
429 : :
430 : 42239 : const char *pf = filename.data();
431 : 42239 : const char *pend = pf + filename.length();
432 : 406464 : while (pf < pend)
433 : : {
434 : 364226 : unsigned int c;
435 : 364226 : int adv = Lex::fetch_char(pf, &c);
436 : 364226 : if (adv == 0)
437 : : {
438 : 0 : go_error_at(location, "import path contains invalid UTF-8 sequence");
439 : 1 : return;
440 : : }
441 : 364226 : if (c == '\0')
442 : : {
443 : 0 : go_error_at(location, "import path contains NUL");
444 : 0 : return;
445 : : }
446 : 364226 : if (c < 0x20 || c == 0x7f)
447 : : {
448 : 1 : go_error_at(location, "import path contains control character");
449 : 1 : return;
450 : : }
451 : 364225 : if (c == '\\')
452 : : {
453 : 0 : go_error_at(location, "import path contains backslash; use slash");
454 : 0 : return;
455 : : }
456 : 364225 : if (Lex::is_unicode_space(c))
457 : : {
458 : 0 : go_error_at(location, "import path contains space character");
459 : 0 : return;
460 : : }
461 : 364225 : 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 : 364225 : pf += adv;
468 : : }
469 : :
470 : 42238 : 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 : 42238 : if (local_name == "init")
477 : 1 : go_error_at(location, "cannot import package as init");
478 : :
479 : 42238 : 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 : 40767 : if (filename == "embed")
487 : 8 : this->current_file_imported_embed_ = true;
488 : :
489 : 40767 : Imports::const_iterator p = this->imports_.find(filename);
490 : 40767 : 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 : 21816 : std::string path = filename;
529 : 21816 : Unordered_map(std::string, std::string)::const_iterator pi;
530 : 21816 : pi = this->import_map_.find(filename);
531 : 21816 : if (pi != this->import_map_.end())
532 : 18 : path = pi->second;
533 : 21816 : pi = this->package_file_.find(path);
534 : 21816 : if (pi != this->package_file_.end())
535 : 701 : path = pi->second;
536 : :
537 : 43632 : Import::Stream* stream = Import::open_package(path, location,
538 : 21816 : this->relative_import_path_);
539 : 21816 : 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 : 21806 : Import* imp = new Import(stream, location);
547 : 21806 : imp->register_builtin_types(this);
548 : 21806 : Package* package = imp->import(this, local_name, is_local_name_exported);
549 : 21806 : if (package != NULL)
550 : : {
551 : 21806 : 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 : 21806 : this->imports_.insert(std::make_pair(filename, package));
557 : : }
558 : :
559 : 21806 : imp->clear_stream();
560 : 21806 : delete stream;
561 : :
562 : : // FIXME: we never delete imp; we may need it for inlinable functions.
563 : 21816 : }
564 : :
565 : : Import_init *
566 : 1549799 : Gogo::lookup_init(const std::string& init_name)
567 : : {
568 : 1549799 : Import_init tmp("", init_name, -1);
569 : 1549799 : Import_init_set::iterator it = this->imported_init_fns_.find(&tmp);
570 : 1549799 : return (it != this->imported_init_fns_.end()) ? *it : NULL;
571 : 1549799 : }
572 : :
573 : : // Add an import control function for an imported package to the list.
574 : :
575 : : void
576 : 660191 : Gogo::add_import_init_fn(const std::string& package_name,
577 : : const std::string& init_name, int prio)
578 : : {
579 : 30250275 : for (Import_init_set::iterator p =
580 : 660191 : this->imported_init_fns_.begin();
581 : 30910466 : p != this->imported_init_fns_.end();
582 : 30250275 : ++p)
583 : : {
584 : 30748290 : Import_init *ii = (*p);
585 : 30748290 : 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 : 162176 : Import_init* nii = new Import_init(package_name, init_name, prio);
608 : 162176 : this->imported_init_fns_.insert(nii);
609 : : }
610 : :
611 : : // Return whether we are at the global binding level.
612 : :
613 : : bool
614 : 710685 : Gogo::in_global_scope() const
615 : : {
616 : 710685 : return this->functions_.empty();
617 : : }
618 : :
619 : : // Return the current binding contour.
620 : :
621 : : Bindings*
622 : 804722 : Gogo::current_bindings()
623 : : {
624 : 804722 : if (!this->functions_.empty())
625 : 388532 : return this->functions_.back().blocks.back()->bindings();
626 : 416190 : else if (this->package_ != NULL)
627 : 318708 : return this->package_->bindings();
628 : : else
629 : 97482 : 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 : 62417 : Gogo::update_init_priority(Import_init* ii,
645 : : std::set<const Import_init *>* visited)
646 : : {
647 : 62417 : visited->insert(ii);
648 : 62417 : int succ_prior = -1;
649 : :
650 : 507994 : for (std::set<std::string>::const_iterator pci =
651 : 62417 : ii->precursors().begin();
652 : 507994 : pci != ii->precursors().end();
653 : 445577 : ++pci)
654 : : {
655 : 445577 : Import_init* succ = this->lookup_init(*pci);
656 : 445577 : if (visited->find(succ) == visited->end())
657 : 32670 : update_init_priority(succ, visited);
658 : 526162 : succ_prior = std::max(succ_prior, succ->priority());
659 : : }
660 : 62417 : if (ii->priority() <= succ_prior)
661 : 62417 : ii->set_priority(succ_prior + 1);
662 : 62417 : }
663 : :
664 : : void
665 : 1878 : Gogo::recompute_init_priorities()
666 : : {
667 : 1878 : std::set<Import_init *> nonroots;
668 : :
669 : 62417 : for (Import_init_set::const_iterator p =
670 : 1878 : this->imported_init_fns_.begin();
671 : 64295 : p != this->imported_init_fns_.end();
672 : 62417 : ++p)
673 : : {
674 : 62417 : const Import_init *ii = *p;
675 : 507994 : for (std::set<std::string>::const_iterator pci =
676 : 62417 : ii->precursors().begin();
677 : 507994 : pci != ii->precursors().end();
678 : 445577 : ++pci)
679 : : {
680 : 445577 : Import_init* ii_init = this->lookup_init(*pci);
681 : 445577 : nonroots.insert(ii_init);
682 : : }
683 : : }
684 : :
685 : : // Recursively update priorities starting at roots.
686 : 1878 : std::set<const Import_init*> visited;
687 : 62417 : for (Import_init_set::iterator p =
688 : 1878 : this->imported_init_fns_.begin();
689 : 64295 : p != this->imported_init_fns_.end();
690 : 62417 : ++p)
691 : : {
692 : 62417 : Import_init* ii = *p;
693 : 62417 : if (nonroots.find(ii) != nonroots.end())
694 : 32670 : continue;
695 : 29747 : update_init_priority(ii, &visited);
696 : : }
697 : 1878 : }
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 : 1888 : Gogo::init_imports(std::vector<Bstatement*>& init_stmts, Bfunction *bfunction)
705 : : {
706 : 1888 : go_assert(this->is_main_package());
707 : :
708 : 1888 : if (this->imported_init_fns_.empty())
709 : 10 : return;
710 : :
711 : 1878 : Location unknown_loc = Linemap::unknown_location();
712 : 1878 : Function_type* func_type =
713 : 1878 : Type::make_function_type(NULL, NULL, NULL, unknown_loc);
714 : 1878 : Btype* fntype = func_type->get_backend_fntype(this);
715 : :
716 : : // Recompute init priorities based on a walk of the init graph.
717 : 1878 : recompute_init_priorities();
718 : :
719 : : // We must call them in increasing priority order.
720 : 1878 : std::vector<const Import_init*> v;
721 : 62417 : for (Import_init_set::const_iterator p =
722 : 1878 : this->imported_init_fns_.begin();
723 : 64295 : p != this->imported_init_fns_.end();
724 : 62417 : ++p)
725 : : {
726 : : // Don't include dummy inits. They are not real functions.
727 : 62417 : if ((*p)->is_dummy())
728 : 27175 : continue;
729 : 35242 : 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 : 35242 : v.push_back(*p);
734 : : }
735 : 1878 : std::sort(v.begin(), v.end(), priority_compare);
736 : :
737 : : // We build calls to the init functions, which take no arguments.
738 : 1878 : std::vector<Bexpression*> empty_args;
739 : 1878 : for (std::vector<const Import_init*>::const_iterator p = v.begin();
740 : 37120 : p != v.end();
741 : 35242 : ++p)
742 : : {
743 : 35242 : const Import_init* ii = *p;
744 : 35242 : std::string user_name = ii->package_name() + ".init";
745 : 35242 : const std::string& init_name(ii->init_name());
746 : 35242 : const unsigned int flags =
747 : : (Backend::function_is_visible
748 : : | Backend::function_is_declaration
749 : : | Backend::function_is_inlinable);
750 : 35242 : Bfunction* pfunc = this->backend()->function(fntype, user_name, init_name,
751 : : flags, unknown_loc);
752 : 35242 : Bexpression* pfunc_code =
753 : 35242 : this->backend()->function_code_expression(pfunc, unknown_loc);
754 : 35242 : Bexpression* pfunc_call =
755 : 35242 : this->backend()->call_expression(bfunction, pfunc_code, empty_args,
756 : : NULL, unknown_loc);
757 : 35242 : init_stmts.push_back(this->backend()->expression_statement(bfunction,
758 : : pfunc_call));
759 : 35242 : }
760 : 1878 : }
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 : 4642 : Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
784 : : std::vector<Bstatement*>& init_stmts,
785 : : Bfunction* init_bfn)
786 : : {
787 : 4642 : if (var_gc.empty() && this->gc_roots_.empty())
788 : 2531 : 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 : 21498 : p != var_gc.end();
821 : 19387 : ++p)
822 : : {
823 : 19387 : Expression_list* init = new Expression_list();
824 : :
825 : 19387 : Location no_loc = (*p)->location();
826 : 19387 : Expression* decl = Expression::make_var_reference(*p, no_loc);
827 : 19387 : Expression* decl_addr =
828 : 19387 : Expression::make_unary(OPERATOR_AND, decl, no_loc);
829 : 19387 : decl_addr->unary_expression()->set_does_not_escape();
830 : 19387 : decl_addr = Expression::make_cast(pvt, decl_addr, no_loc);
831 : 19387 : init->push_back(decl_addr);
832 : :
833 : 19387 : Expression* size =
834 : 19387 : Expression::make_type_info(decl->type(),
835 : : Expression::TYPE_INFO_SIZE);
836 : 19387 : init->push_back(size);
837 : :
838 : 19387 : Expression* ptrdata =
839 : 19387 : Expression::make_type_info(decl->type(),
840 : : Expression::TYPE_INFO_BACKEND_PTRDATA);
841 : 19387 : init->push_back(ptrdata);
842 : :
843 : 19387 : Expression* gcdata = Expression::make_ptrmask_symbol(decl->type());
844 : 19387 : init->push_back(gcdata);
845 : :
846 : 19387 : Expression* root_ctor =
847 : 19387 : Expression::make_struct_composite_literal(root_type, init, no_loc);
848 : 19387 : 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 : 6530 : type_descriptor_list_type(unsigned long len)
920 : : {
921 : 6530 : Location builtin_loc = Linemap::predeclared_location();
922 : 6530 : Type* int_type = Type::lookup_integer_type("int");
923 : 6530 : Type* ptr_type = Type::make_pointer_type(Type::make_void_type());
924 : : // Avoid creating zero-length type.
925 : 6530 : unsigned long nelems = (len != 0 ? len : 1);
926 : 6530 : Expression* len_expr = Expression::make_integer_ul(nelems, NULL,
927 : : builtin_loc);
928 : 6530 : Array_type* array_type = Type::make_array_type(ptr_type, len_expr);
929 : 6530 : array_type->set_is_array_incomparable();
930 : 6530 : Struct_type* list_type =
931 : 6530 : Type::make_builtin_struct_type(2, "count", int_type,
932 : : "types", array_type);
933 : 6530 : return list_type;
934 : : }
935 : :
936 : : void
937 : 4642 : Gogo::build_type_descriptor_list()
938 : : {
939 : : // Create the list type
940 : 4642 : Location builtin_loc = Linemap::predeclared_location();
941 : 4642 : unsigned long len = this->type_descriptors_.size();
942 : 4642 : Struct_type* list_type = type_descriptor_list_type(len);
943 : 4642 : Btype* bt = list_type->get_backend(this);
944 : 4642 : Btype* bat = list_type->field(1)->type()->get_backend(this);
945 : :
946 : : // Create the variable
947 : 4642 : std::string name = this->type_descriptor_list_symbol(this->pkgpath_symbol());
948 : 4642 : unsigned int flags = Backend::variable_is_constant;
949 : 4642 : Bvariable* bv = this->backend()->implicit_variable(name, name, bt, flags, 0);
950 : :
951 : : // Build the initializer
952 : 4642 : std::vector<unsigned long> indexes;
953 : 4642 : std::vector<Bexpression*> vals;
954 : 4642 : std::vector<Type*>::iterator p = this->type_descriptors_.begin();
955 : 180848 : for (unsigned long i = 0; i < len; ++i, ++p)
956 : : {
957 : 176206 : Bexpression* bexpr = (*p)->type_descriptor_pointer(this,
958 : 176206 : builtin_loc);
959 : 176206 : indexes.push_back(i);
960 : 176206 : vals.push_back(bexpr);
961 : : }
962 : 4642 : Bexpression* barray =
963 : 4642 : this->backend()->array_constructor_expression(bat, indexes, vals,
964 : 4642 : builtin_loc);
965 : :
966 : 4642 : Translate_context context(this, NULL, NULL, NULL);
967 : 4642 : std::vector<Bexpression*> fields;
968 : 4642 : Expression* len_expr = Expression::make_integer_ul(len, NULL,
969 : : builtin_loc);
970 : 4642 : fields.push_back(len_expr->get_backend(&context));
971 : 4642 : fields.push_back(barray);
972 : 4642 : Bexpression* binit =
973 : 4642 : this->backend()->constructor_expression(bt, fields, builtin_loc);
974 : :
975 : 4642 : this->backend()->implicit_variable_set_init(bv, name, bt, flags, binit);
976 : 4642 : }
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 : 1888 : Gogo::register_type_descriptors(std::vector<Bstatement*>& init_stmts,
983 : : Bfunction* init_bfn)
984 : : {
985 : : // Create the list type
986 : 1888 : Location builtin_loc = Linemap::predeclared_location();
987 : 1888 : Struct_type* list_type = type_descriptor_list_type(1);
988 : 1888 : Btype* bt = list_type->get_backend(this);
989 : :
990 : : // Collect type lists from transitive imports.
991 : 1888 : std::vector<std::string> list_names;
992 : 1888 : for (Import_init_set::iterator it = this->imported_init_fns_.begin();
993 : 64305 : it != this->imported_init_fns_.end();
994 : 62417 : ++it)
995 : : {
996 : 62417 : std::string pkgpath_symbol =
997 : 62417 : this->pkgpath_symbol_from_init_fn_name((*it)->init_name());
998 : 62417 : list_names.push_back(this->type_descriptor_list_symbol(pkgpath_symbol));
999 : 62417 : }
1000 : : // Add the main package itself.
1001 : 1888 : list_names.push_back(this->type_descriptor_list_symbol("main"));
1002 : :
1003 : : // Build a list of lists.
1004 : 1888 : std::vector<unsigned long> indexes;
1005 : 1888 : std::vector<Bexpression*> vals;
1006 : 1888 : unsigned long i = 0;
1007 : 1888 : for (std::vector<std::string>::iterator p = list_names.begin();
1008 : 66193 : p != list_names.end();
1009 : 64305 : ++p)
1010 : : {
1011 : 64305 : Bvariable* bv =
1012 : 64305 : this->backend()->implicit_variable_reference(*p, *p, bt);
1013 : 64305 : Bexpression* bexpr = this->backend()->var_expression(bv, builtin_loc);
1014 : 64305 : bexpr = this->backend()->address_expression(bexpr, builtin_loc);
1015 : :
1016 : 64305 : indexes.push_back(i);
1017 : 64305 : vals.push_back(bexpr);
1018 : 64305 : i++;
1019 : : }
1020 : 1888 : Expression* len_expr = Expression::make_integer_ul(i, NULL, builtin_loc);
1021 : 1888 : Type* list_ptr_type = Type::make_pointer_type(list_type);
1022 : 1888 : Type* list_array_type = Type::make_array_type(list_ptr_type, len_expr);
1023 : 1888 : Btype* bat = list_array_type->get_backend(this);
1024 : 1888 : Bexpression* barray =
1025 : 1888 : this->backend()->array_constructor_expression(bat, indexes, vals,
1026 : : builtin_loc);
1027 : :
1028 : : // Create a variable holding the list.
1029 : 1888 : std::string name = this->typelists_symbol();
1030 : 1888 : unsigned int flags = (Backend::variable_is_hidden
1031 : : | Backend::variable_is_constant);
1032 : 1888 : Bvariable* bv = this->backend()->implicit_variable(name, name, bat, flags,
1033 : : 0);
1034 : 1888 : this->backend()->implicit_variable_set_init(bv, name, bat, flags, barray);
1035 : :
1036 : : // Build the call in main package's init function.
1037 : 1888 : Translate_context context(this, NULL, NULL, NULL);
1038 : 1888 : Bexpression* bexpr = this->backend()->var_expression(bv, builtin_loc);
1039 : 1888 : bexpr = this->backend()->address_expression(bexpr, builtin_loc);
1040 : 1888 : Type* array_ptr_type = Type::make_pointer_type(list_array_type);
1041 : 1888 : Expression* expr = Expression::make_backend(bexpr, array_ptr_type,
1042 : : builtin_loc);
1043 : 1888 : expr = Runtime::make_call(this, Runtime::REGISTER_TYPE_DESCRIPTORS,
1044 : : builtin_loc, 2, len_expr->copy(), expr);
1045 : 1888 : Bexpression* bcall = expr->get_backend(&context);
1046 : 1888 : init_stmts.push_back(this->backend()->expression_statement(init_bfn,
1047 : : bcall));
1048 : 1888 : }
1049 : :
1050 : : // Build the decl for the initialization function.
1051 : :
1052 : : Named_object*
1053 : 3452 : Gogo::initialization_function_decl()
1054 : : {
1055 : 3452 : std::string name = this->get_init_fn_name();
1056 : 3452 : Location loc = this->package_->location();
1057 : :
1058 : 3452 : Function_type* fntype = Type::make_function_type(NULL, NULL, NULL, loc);
1059 : 3452 : Function* initfn = new Function(fntype, NULL, NULL, loc);
1060 : 3452 : return Named_object::make_function(name, NULL, initfn);
1061 : 3452 : }
1062 : :
1063 : : // Create the magic initialization function. CODE_STMT is the
1064 : : // code that it needs to run.
1065 : :
1066 : : Named_object*
1067 : 3448 : 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 : 3448 : go_assert(this->is_main_package() || this->need_init_fn_);
1073 : :
1074 : 3448 : if (initfn == NULL)
1075 : 278 : initfn = this->initialization_function_decl();
1076 : :
1077 : : // Bind the initialization function code to a block.
1078 : 3448 : Bfunction* fndecl = initfn->func_value()->get_or_make_decl(this, initfn);
1079 : 3448 : Location pkg_loc = this->package_->location();
1080 : 3448 : std::vector<Bvariable*> vars;
1081 : 3448 : this->backend()->block(fndecl, NULL, vars, pkg_loc, pkg_loc);
1082 : :
1083 : 3448 : if (!this->backend()->function_set_body(fndecl, code_stmt))
1084 : : {
1085 : 41 : go_assert(saw_errors());
1086 : : return NULL;
1087 : : }
1088 : : return initfn;
1089 : 3448 : }
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 : 55911 : Find_vars()
1107 : 55911 : : Traverse(traverse_expressions | traverse_statements),
1108 : 55911 : vars_(), seen_objects_(), lhs_is_ref_(false)
1109 : 55911 : { }
1110 : :
1111 : : // An iterator through the variables found, after the traversal.
1112 : : typedef Vars::const_iterator const_iterator;
1113 : :
1114 : : const_iterator
1115 : 55911 : begin() const
1116 : 55911 : { return this->vars_.begin(); }
1117 : :
1118 : : const_iterator
1119 : 77443 : end() const
1120 : 77443 : { 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 : 7218972 : Find_vars::expression(Expression** pexpr)
1142 : : {
1143 : 7218972 : Expression* e = *pexpr;
1144 : :
1145 : 7218972 : Var_expression* ve = e->var_expression();
1146 : 824863 : if (ve != NULL)
1147 : : {
1148 : 824863 : Named_object* v = ve->named_object();
1149 : 824863 : 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 : 636334 : 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 : 6582638 : Func_expression* fe = e->func_expression();
1179 : 6582638 : Bound_method_expression* bme = e->bound_method_expression();
1180 : 6582638 : if (fe != NULL || bme != NULL)
1181 : : {
1182 : 335247 : const Named_object* f = fe != NULL ? fe->named_object() : bme->function();
1183 : 335247 : if (f->is_function() && f->package() == NULL)
1184 : : {
1185 : 129261 : std::pair<Seen_objects::iterator, bool> ins =
1186 : 129261 : this->seen_objects_.insert(f);
1187 : 129261 : if (ins.second)
1188 : : {
1189 : : // This is the first time we have seen this name.
1190 : 41338 : bool hold = this->lhs_is_ref_;
1191 : 41338 : this->lhs_is_ref_ = true;
1192 : 41338 : int r = f->func_value()->block()->traverse(this);
1193 : 41338 : this->lhs_is_ref_ = hold;
1194 : 41338 : if (r == TRAVERSE_EXIT)
1195 : 0 : return TRAVERSE_EXIT;
1196 : : }
1197 : : }
1198 : : }
1199 : :
1200 : 6582638 : Temporary_reference_expression* tre = e->temporary_reference_expression();
1201 : 310563 : if (tre != NULL)
1202 : : {
1203 : 310563 : Temporary_statement* ts = tre->statement();
1204 : 310563 : Expression* init = ts->init();
1205 : 310563 : if (init != NULL)
1206 : : {
1207 : 260148 : std::pair<Seen_objects::iterator, bool> ins =
1208 : 260148 : this->seen_objects_.insert(ts);
1209 : 260148 : if (ins.second)
1210 : : {
1211 : : // This is the first time we have seen this temporary
1212 : : // statement.
1213 : 167874 : 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 : 1047147 : Find_vars::statement(Block*, size_t*, Statement* s)
1227 : : {
1228 : 1047147 : 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 : 19212 : expression_requires(Expression* expr, Block* preinit, Named_object* dep,
1249 : : Named_object* var)
1250 : : {
1251 : 19212 : Find_vars find_vars;
1252 : 19212 : if (expr != NULL)
1253 : 19212 : Expression::traverse(&expr, &find_vars);
1254 : 19212 : if (preinit != NULL)
1255 : 909 : preinit->traverse(&find_vars);
1256 : 19212 : 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 : 29094 : for (Find_vars::const_iterator p = find_vars.begin();
1266 : 29094 : p != find_vars.end();
1267 : 9882 : ++p)
1268 : : {
1269 : 9899 : if (*p == var)
1270 : 19212 : return true;
1271 : : }
1272 : : return false;
1273 : 19212 : }
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 : 13012 : Var_init(Named_object* var, Bstatement* init)
1287 : 13012 : : var_(var), init_(init), dep_count_(0)
1288 : : { }
1289 : :
1290 : : // Return the variable.
1291 : : Named_object*
1292 : 121737 : var() const
1293 : 121737 : { return this->var_; }
1294 : :
1295 : : // Return the initialization expression.
1296 : : Bstatement*
1297 : 13012 : init() const
1298 : 13012 : { return this->init_; }
1299 : :
1300 : : // Return the number of remaining dependencies.
1301 : : size_t
1302 : 17336 : dep_count() const
1303 : 17336 : { 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 : 88669 : operator<(const Var_init& v1, const Var_init& v2)
1330 : 88669 : { 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 : 14869 : pvar != var_inits->end();
1356 : 13012 : ++pvar)
1357 : : {
1358 : 13012 : Named_object* var = pvar->var();
1359 : 13012 : var_to_init[var] = &*pvar;
1360 : : }
1361 : :
1362 : : // Add dependencies to init_deps, and check for cycles.
1363 : 14869 : for (Var_inits::iterator pvar = var_inits->begin();
1364 : 14869 : pvar != var_inits->end();
1365 : 13012 : ++pvar)
1366 : : {
1367 : 13012 : Named_object* var = pvar->var();
1368 : :
1369 : 13012 : const std::vector<Named_object*>* refs =
1370 : 13012 : pvar->var()->var_value()->init_refs();
1371 : 13012 : if (refs == NULL)
1372 : 9500 : 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 : 8711 : while (!var_inits->empty())
1436 : : {
1437 : : Var_inits::iterator v1;;
1438 : 17336 : for (v1 = var_inits->begin(); v1 != var_inits->end(); ++v1)
1439 : : {
1440 : 17336 : if (v1->dep_count() == 0)
1441 : : break;
1442 : : }
1443 : 8053 : 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 : 8053 : ready.splice(ready.end(), *var_inits, v1);
1449 : :
1450 : 8053 : Init_deps::iterator p1 = init_deps.find(*v1);
1451 : 8053 : 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 : 11990 : Gogo::check_self_dep(Named_object* var)
1473 : : {
1474 : 11990 : Expression* init = var->var_value()->init();
1475 : 11990 : Block* preinit = var->var_value()->preinit();
1476 : 11990 : Named_object* dep = this->var_depends_on(var->var_value());
1477 : 11990 : if (init != NULL
1478 : 11990 : && dep == NULL
1479 : 11990 : && 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 : 11990 : }
1484 : :
1485 : : // Write out the global definitions.
1486 : :
1487 : : void
1488 : 4642 : Gogo::write_globals()
1489 : : {
1490 : 4642 : this->build_interface_method_tables();
1491 : :
1492 : 4642 : Bindings* bindings = this->current_bindings();
1493 : :
1494 : 445664 : for (Bindings::const_declarations_iterator p = bindings->begin_declarations();
1495 : 445664 : p != bindings->end_declarations();
1496 : 441022 : ++p)
1497 : : {
1498 : : // If any function declarations needed a descriptor, make sure
1499 : : // we build it.
1500 : 441022 : Named_object* no = p->second;
1501 : 441022 : 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 : 4642 : std::vector<Btype*> type_decls;
1508 : 4642 : std::vector<Bvariable*> var_decls;
1509 : 4642 : std::vector<Bexpression*> const_decls;
1510 : 4642 : std::vector<Bfunction*> func_decls;
1511 : :
1512 : : // The init function declaration and associated Bfunction, if necessary.
1513 : 4642 : Named_object* init_fndecl = NULL;
1514 : 4642 : Bfunction* init_bfn = NULL;
1515 : :
1516 : 4642 : std::vector<Bstatement*> init_stmts;
1517 : 4642 : std::vector<Bstatement*> var_init_stmts;
1518 : :
1519 : 4642 : if (this->is_main_package())
1520 : : {
1521 : 1888 : init_fndecl = this->initialization_function_decl();
1522 : 1888 : init_bfn = init_fndecl->func_value()->get_or_make_decl(this, init_fndecl);
1523 : : }
1524 : :
1525 : : // A list of variable initializations.
1526 : 4642 : Var_inits var_inits;
1527 : :
1528 : : // A list of variables which need to be registered with the garbage
1529 : : // collector.
1530 : 4642 : size_t count_definitions = bindings->size_definitions();
1531 : 4642 : std::vector<Named_object*> var_gc;
1532 : 4642 : var_gc.reserve(count_definitions);
1533 : :
1534 : 561537 : for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
1535 : 561537 : p != bindings->end_definitions();
1536 : 556895 : ++p)
1537 : : {
1538 : 556895 : Named_object* no = *p;
1539 : 556895 : go_assert(!no->is_type_declaration() && !no->is_function_declaration());
1540 : :
1541 : : // There is nothing to do for a package.
1542 : 556895 : if (no->is_package())
1543 : 194383 : continue;
1544 : :
1545 : : // There is nothing to do for an object which was imported from
1546 : : // a different package into the global scope.
1547 : 517480 : if (no->package() != NULL)
1548 : 32785 : continue;
1549 : :
1550 : : // Skip blank named functions and constants.
1551 : 283360 : if ((no->is_function() && no->func_value()->is_sink())
1552 : 767755 : || (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 : 483290 : if (no->is_variable()
1560 : 25469 : && no->var_value()->is_global_sink()
1561 : 1114 : && !no->var_value()->has_pre_init()
1562 : 483647 : && (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 : 483146 : if (no->is_const())
1569 : : {
1570 : 144033 : Type* type = no->const_value()->type();
1571 : 144033 : if (type == NULL)
1572 : 0 : type = no->const_value()->expr()->type();
1573 : 167641 : if (type->is_abstract() || !type->is_numeric_type())
1574 : 120634 : continue;
1575 : : }
1576 : :
1577 : 362512 : if (!no->is_variable())
1578 : 337115 : no->get_backend(this, const_decls, type_decls, func_decls);
1579 : : else
1580 : : {
1581 : 25397 : Variable* var = no->var_value();
1582 : 25397 : Bvariable* bvar = no->get_backend_variable(this, NULL);
1583 : 25397 : 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 : 25397 : bool is_sink = no->name()[0] == '_' && no->name()[1] == '.';
1588 : :
1589 : 25397 : Bstatement* var_init_stmt = NULL;
1590 : 25397 : 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 : 12206 : if (init_fndecl == NULL)
1658 : 1235 : init_fndecl = this->initialization_function_decl();
1659 : :
1660 : 12206 : Bvariable* var_decl = is_sink ? NULL : bvar;
1661 : 12206 : var_init_stmt = var->get_init_block(this, init_fndecl, var_decl);
1662 : : }
1663 : :
1664 : 25397 : if (var_init_stmt != NULL)
1665 : : {
1666 : 13008 : if (var->init() == NULL && !var->has_pre_init())
1667 : 0 : var_init_stmts.push_back(var_init_stmt);
1668 : : else
1669 : 13008 : 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 : 25397 : if (!is_sink && var->type()->has_pointer())
1687 : : {
1688 : : // Avoid putting runtime.gcRoots itself on the list.
1689 : 38788 : if (this->compiling_runtime()
1690 : 832 : && this->package_name() == "runtime"
1691 : 39431 : && (Gogo::unpack_hidden_name(no->name()) == "gcRoots"
1692 : 20037 : || Gogo::unpack_hidden_name(no->name()) == "gcRootsIndex"))
1693 : : ;
1694 : : else
1695 : 19387 : 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 : 4642 : this->imported_inline_functions_.begin();
1703 : 16889 : 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 : 4642 : this->build_type_descriptor_list();
1709 : :
1710 : 4642 : 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 : 1888 : this->register_type_descriptors(init_stmts, init_bfn);
1718 : :
1719 : : // Initialize imported packages.
1720 : 1888 : this->init_imports(init_stmts, init_bfn);
1721 : : }
1722 : :
1723 : : // Register global variables with the garbage collector.
1724 : 4642 : this->register_gc_vars(var_gc, init_stmts, init_bfn);
1725 : :
1726 : : // Simple variable initializations, after all variables are
1727 : : // registered.
1728 : 4642 : 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 : 4642 : if (!var_inits.empty())
1733 : : {
1734 : 1857 : sort_var_inits(&var_inits);
1735 : 1857 : for (Var_inits::const_iterator p = var_inits.begin();
1736 : 14869 : p != var_inits.end();
1737 : 13012 : ++p)
1738 : 13012 : 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 : 4642 : std::vector<Bexpression*> empty_args;
1745 : 1884 : for (std::vector<Named_object*>::const_iterator p =
1746 : 4642 : this->init_functions_.begin();
1747 : 6526 : 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 : 4642 : Bstatement* init_fncode = this->backend()->statement_list(init_stmts);
1765 : 4642 : if (this->need_init_fn_ || this->is_main_package())
1766 : : {
1767 : 3448 : init_fndecl =
1768 : 3448 : this->create_initialization_function(init_fndecl, init_fncode);
1769 : 3448 : if (init_fndecl != NULL)
1770 : 3407 : 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 : 4642 : go_assert(count_definitions == this->current_bindings()->size_definitions());
1775 : :
1776 : : // Define all globally declared values.
1777 : 4642 : if (!saw_errors())
1778 : 4213 : this->backend()->write_global_definitions(type_decls, const_decls,
1779 : : func_decls, var_decls);
1780 : 4642 : }
1781 : :
1782 : : // Return the current block.
1783 : :
1784 : : Block*
1785 : 927023 : Gogo::current_block()
1786 : : {
1787 : 927023 : if (this->functions_.empty())
1788 : : return NULL;
1789 : : else
1790 : 927022 : 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 : 4070335 : Gogo::lookup(const std::string& name, Named_object** pfunction) const
1799 : : {
1800 : 4070335 : if (pfunction != NULL)
1801 : 3213111 : *pfunction = NULL;
1802 : :
1803 : 4070335 : if (Gogo::is_sink_name(name))
1804 : 8421 : return Named_object::make_sink();
1805 : :
1806 : 5429843 : for (Open_functions::const_reverse_iterator p = this->functions_.rbegin();
1807 : 5429843 : p != this->functions_.rend();
1808 : 1367929 : ++p)
1809 : : {
1810 : 3485377 : Named_object* ret = p->blocks.back()->bindings()->lookup(name);
1811 : 3485377 : if (ret != NULL)
1812 : : {
1813 : 2117448 : if (pfunction != NULL)
1814 : 1860223 : *pfunction = p->function;
1815 : 2117448 : return ret;
1816 : : }
1817 : : }
1818 : :
1819 : 1944466 : if (this->package_ != NULL)
1820 : : {
1821 : 1944466 : Named_object* ret = this->package_->bindings()->lookup(name);
1822 : 1944466 : if (ret != NULL)
1823 : : {
1824 : 1743266 : 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 : 1743266 : 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 : 306709 : Gogo::lookup_in_block(const std::string& name) const
1848 : : {
1849 : 306709 : go_assert(!this->functions_.empty());
1850 : 306709 : go_assert(!this->functions_.back().blocks.empty());
1851 : 306709 : 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 : 639497 : Gogo::lookup_global(const char* name) const
1858 : : {
1859 : 639497 : return this->globals_->lookup(name);
1860 : : }
1861 : :
1862 : : // Add an imported package.
1863 : :
1864 : : Package*
1865 : 25879 : 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 : 25879 : Package* ret = this->register_package(pkgpath, pkgpath_symbol, location);
1874 : 25879 : ret->set_package_name(real_name, location);
1875 : :
1876 : 25879 : *padd_to_globals = false;
1877 : :
1878 : 25879 : 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 : 734185 : Gogo::register_package(const std::string& pkgpath,
1913 : : const std::string& pkgpath_symbol, Location location)
1914 : : {
1915 : 734185 : Package* package = NULL;
1916 : 734185 : std::pair<Packages::iterator, bool> ins =
1917 : 734185 : this->packages_.insert(std::make_pair(pkgpath, package));
1918 : 734185 : if (!ins.second)
1919 : : {
1920 : : // We have seen this package name before.
1921 : 617559 : package = ins.first->second;
1922 : 617559 : go_assert(package != NULL && package->pkgpath() == pkgpath);
1923 : 617559 : if (!pkgpath_symbol.empty())
1924 : 14338 : package->set_pkgpath_symbol(pkgpath_symbol);
1925 : 617559 : if (Linemap::is_unknown_location(package->location()))
1926 : 318733 : package->set_location(location);
1927 : : }
1928 : : else
1929 : : {
1930 : : // First time we have seen this package name.
1931 : 116626 : package = new Package(pkgpath, pkgpath_symbol, location);
1932 : 116626 : go_assert(ins.first->second == NULL);
1933 : 116626 : ins.first->second = package;
1934 : : }
1935 : :
1936 : 734185 : 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 : 283365 : Gogo::start_function(const std::string& name, Function_type* type,
1953 : : bool add_method_to_type, Location location)
1954 : : {
1955 : 283365 : bool at_top_level = this->functions_.empty();
1956 : :
1957 : 283365 : Block* block = new Block(NULL, location);
1958 : :
1959 : 283365 : Named_object* enclosing = (at_top_level
1960 : 283365 : ? NULL
1961 : 24314 : : this->functions_.back().function);
1962 : :
1963 : 283365 : Function* function = new Function(type, enclosing, block, location);
1964 : :
1965 : 283365 : if (type->is_method())
1966 : : {
1967 : 77453 : const Typed_identifier* receiver = type->receiver();
1968 : 77453 : Variable* this_param = new Variable(receiver->type(), NULL, false,
1969 : 77453 : true, true, location);
1970 : 77453 : std::string rname = receiver->name();
1971 : 77453 : 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 : 77453 : Gogo::rename_if_empty(&rname, "r", &rcounter);
1976 : :
1977 : 77453 : block->bindings()->add_variable(rname, NULL, this_param);
1978 : 77453 : }
1979 : :
1980 : 283365 : const Typed_identifier_list* parameters = type->parameters();
1981 : 283365 : bool is_varargs = type->is_varargs();
1982 : 283365 : unsigned pcounter = 0;
1983 : 283365 : if (parameters != NULL)
1984 : : {
1985 : 217963 : for (Typed_identifier_list::const_iterator p = parameters->begin();
1986 : 596720 : p != parameters->end();
1987 : 378757 : ++p)
1988 : : {
1989 : 378757 : Variable* param = new Variable(p->type(), NULL, false, true, false,
1990 : 378757 : p->location());
1991 : 378757 : if (is_varargs && p + 1 == parameters->end())
1992 : 2018 : param->set_is_varargs_parameter();
1993 : :
1994 : 378757 : 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 : 378757 : Gogo::rename_if_empty(&pname, "p", &pcounter);
1999 : :
2000 : 378757 : block->bindings()->add_variable(pname, NULL, param);
2001 : 378757 : }
2002 : : }
2003 : :
2004 : 283365 : function->create_result_variables(this);
2005 : :
2006 : 283365 : const std::string* pname;
2007 : 283365 : std::string nested_name;
2008 : 283365 : bool is_init = false;
2009 : 566730 : 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 : 281481 : else if (!name.empty())
2022 : : pname = &name;
2023 : : else
2024 : : {
2025 : : // Invent a name for a nested function.
2026 : 25613 : nested_name = this->nested_function_name(enclosing);
2027 : 25613 : pname = &nested_name;
2028 : : }
2029 : :
2030 : 283365 : Named_object* ret;
2031 : 283365 : 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 : 283060 : else if (!type->is_method())
2071 : : {
2072 : 205612 : ret = this->package_->bindings()->add_function(*pname, NULL, function);
2073 : 205612 : 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 : 77448 : if (!add_method_to_type)
2084 : 24258 : 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 : 77448 : this->package_->bindings()->add_method(ret);
2153 : : }
2154 : :
2155 : 283365 : this->functions_.resize(this->functions_.size() + 1);
2156 : 283365 : Open_function& of(this->functions_.back());
2157 : 283365 : of.function = ret;
2158 : 283365 : of.blocks.push_back(block);
2159 : :
2160 : 283365 : if (is_init)
2161 : : {
2162 : 1884 : this->init_functions_.push_back(ret);
2163 : 1884 : this->need_init_fn_ = true;
2164 : : }
2165 : :
2166 : 283365 : return ret;
2167 : 283365 : }
2168 : :
2169 : : // Finish compiling a function.
2170 : :
2171 : : void
2172 : 283365 : Gogo::finish_function(Location location)
2173 : : {
2174 : 283365 : this->finish_block(location);
2175 : 283365 : go_assert(this->functions_.back().blocks.empty());
2176 : 283365 : this->functions_.pop_back();
2177 : 283365 : }
2178 : :
2179 : : // Return the current function.
2180 : :
2181 : : Named_object*
2182 : 2109347 : Gogo::current_function() const
2183 : : {
2184 : 2109347 : go_assert(!this->functions_.empty());
2185 : 2109347 : return this->functions_.back().function;
2186 : : }
2187 : :
2188 : : // Start a new block.
2189 : :
2190 : : void
2191 : 912833 : Gogo::start_block(Location location)
2192 : : {
2193 : 912833 : go_assert(!this->functions_.empty());
2194 : 912833 : Block* block = new Block(this->current_block(), location);
2195 : 912833 : this->functions_.back().blocks.push_back(block);
2196 : 912833 : }
2197 : :
2198 : : // Finish a block.
2199 : :
2200 : : Block*
2201 : 1196198 : Gogo::finish_block(Location location)
2202 : : {
2203 : 1196198 : go_assert(!this->functions_.empty());
2204 : 1196198 : go_assert(!this->functions_.back().blocks.empty());
2205 : 1196198 : Block* block = this->functions_.back().blocks.back();
2206 : 1196198 : this->functions_.back().blocks.pop_back();
2207 : 1196198 : block->set_end_location(location);
2208 : 1196198 : 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 : 85665 : Gogo::add_unknown_name(const std::string& name, Location location)
2223 : : {
2224 : 85665 : return this->package_->bindings()->add_unknown_name(name, location);
2225 : : }
2226 : :
2227 : : // Declare a function.
2228 : :
2229 : : Named_object*
2230 : 5731 : Gogo::declare_function(const std::string& name, Function_type* type,
2231 : : Location location)
2232 : : {
2233 : 5731 : if (!type->is_method())
2234 : 5668 : return this->current_bindings()->add_function_declaration(name, NULL, type,
2235 : 5668 : 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 : 1798199 : Gogo::add_statement(Statement* statement)
2312 : : {
2313 : 1798199 : go_assert(!this->functions_.empty()
2314 : : && !this->functions_.back().blocks.empty());
2315 : 1798199 : this->functions_.back().blocks.back()->add_statement(statement);
2316 : 1798199 : }
2317 : :
2318 : : // Add a block.
2319 : :
2320 : : void
2321 : 444140 : Gogo::add_block(Block* block, Location location)
2322 : : {
2323 : 444140 : go_assert(!this->functions_.empty()
2324 : : && !this->functions_.back().blocks.empty());
2325 : 444140 : Statement* statement = Statement::make_block_statement(block, location);
2326 : 444140 : this->functions_.back().blocks.back()->add_statement(statement);
2327 : 444140 : }
2328 : :
2329 : : // Add a constant.
2330 : :
2331 : : Named_object*
2332 : 150673 : Gogo::add_constant(const Typed_identifier& tid, Expression* expr,
2333 : : int iota_value)
2334 : : {
2335 : 150673 : 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 : 97515 : Gogo::add_named_type(Named_type* type)
2361 : : {
2362 : 97515 : go_assert(this->in_global_scope());
2363 : 97515 : this->current_bindings()->add_named_type(type);
2364 : 97515 : }
2365 : :
2366 : : // Declare a type.
2367 : :
2368 : : Named_object*
2369 : 31938 : Gogo::declare_type(const std::string& name, Location location)
2370 : : {
2371 : 31938 : Bindings* bindings = this->current_bindings();
2372 : 31938 : Named_object* no = bindings->add_type_declaration(name, NULL, location);
2373 : 31938 : 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 : 31938 : 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 : 92607 : Gogo::declare_package_function(const std::string& name, Function_type* type,
2398 : : Location location)
2399 : : {
2400 : 92607 : return this->package_->bindings()->add_function_declaration(name, NULL, type,
2401 : 92607 : location);
2402 : : }
2403 : :
2404 : : // Add a function declaration to the list of functions we may want to
2405 : : // inline.
2406 : :
2407 : : void
2408 : 82297 : Gogo::add_imported_inlinable_function(Named_object* no)
2409 : : {
2410 : 82297 : go_assert(no->is_function_declaration());
2411 : 82297 : Function_declaration* fd = no->func_declaration_value();
2412 : 82297 : 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 : 31938 : Gogo::define_type(Named_object* no, Named_type* type)
2422 : : {
2423 : 31938 : this->current_bindings()->define_type(no, type);
2424 : 31938 : }
2425 : :
2426 : : // Add a variable.
2427 : :
2428 : : Named_object*
2429 : 405954 : Gogo::add_variable(const std::string& name, Variable* variable)
2430 : : {
2431 : 405954 : 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 : 405954 : if (no != NULL
2436 : 405954 : && no->is_variable()
2437 : 405952 : && !no->var_value()->is_parameter()
2438 : 811905 : && !this->functions_.empty())
2439 : 380479 : this->add_statement(Statement::make_variable_declaration(no));
2440 : :
2441 : 405954 : return no;
2442 : : }
2443 : :
2444 : : void
2445 : 473784 : Gogo::rename_if_empty(std::string* pname, const char* tag, unsigned* count)
2446 : : {
2447 : 473784 : 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 : 473784 : }
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 : 71623 : Gogo::record_interface_type(Interface_type* itype)
2552 : : {
2553 : 71623 : this->interface_types_.push_back(itype);
2554 : 71623 : }
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 : 4642 : Gogo::define_global_names()
2562 : : {
2563 : 4642 : 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 : 1888 : this->import_package("runtime", "_", false, false,
2570 : 1888 : this->package_->location());
2571 : : }
2572 : :
2573 : 185680 : for (Bindings::const_declarations_iterator p =
2574 : 4642 : this->globals_->begin_declarations();
2575 : 190322 : p != this->globals_->end_declarations();
2576 : 185680 : ++p)
2577 : : {
2578 : 185680 : Named_object* global_no = p->second;
2579 : 185680 : std::string name(Gogo::pack_hidden_name(global_no->name(), false));
2580 : 185680 : Named_object* no = this->package_->bindings()->lookup(name);
2581 : 185680 : if (no == NULL)
2582 : 155767 : continue;
2583 : 29913 : no = no->resolve();
2584 : 29913 : 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 : 29911 : else if (no->is_unknown())
2610 : 29814 : no->unknown_value()->set_real_named_object(global_no);
2611 : 185680 : }
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 : 332637 : for (Bindings::const_declarations_iterator p =
2617 : 4642 : this->package_->bindings()->begin_declarations();
2618 : 337279 : p != this->package_->bindings()->end_declarations();
2619 : 332637 : ++p)
2620 : : {
2621 : 332637 : if (p->second->is_unknown()
2622 : 362504 : && 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 : 332584 : File_block_names::const_iterator pf =
2629 : 332584 : this->file_block_names_.find(p->second->name());
2630 : 332584 : 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 : 332584 : if (!p->second->is_function()
2641 : 567641 : && 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 : 4642 : }
2648 : :
2649 : : // Clear out names in file scope.
2650 : :
2651 : : void
2652 : 12703 : Gogo::clear_file_scope()
2653 : : {
2654 : 12703 : this->package_->bindings()->clear_file_scope(this);
2655 : :
2656 : : // Warn about packages which were imported but not used.
2657 : 12703 : bool quiet = saw_errors();
2658 : 375196 : for (Packages::iterator p = this->packages_.begin();
2659 : 375196 : p != this->packages_.end();
2660 : 362493 : ++p)
2661 : : {
2662 : 362493 : Package* package = p->second;
2663 : 362493 : if (package != this->package_ && !quiet)
2664 : : {
2665 : 389160 : for (Package::Aliases::const_iterator p1 = package->aliases().begin();
2666 : 389160 : 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 : 362493 : package->clear_used();
2688 : : }
2689 : :
2690 : 12703 : this->current_file_imported_unsafe_ = false;
2691 : 12703 : this->current_file_imported_embed_ = false;
2692 : 12703 : }
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 : 9284 : class Specific_type_functions : public Traverse
2733 : : {
2734 : : public:
2735 : 4642 : Specific_type_functions(Gogo* gogo)
2736 : 4642 : : Traverse(traverse_types),
2737 : 4642 : gogo_(gogo)
2738 : : { }
2739 : :
2740 : : int
2741 : : type(Type*);
2742 : :
2743 : : private:
2744 : : Gogo* gogo_;
2745 : : };
2746 : :
2747 : : int
2748 : 11290045 : Specific_type_functions::type(Type* t)
2749 : : {
2750 : 11290045 : switch (t->classification())
2751 : : {
2752 : 688524 : case Type::TYPE_NAMED:
2753 : 688524 : {
2754 : 688524 : Named_type* nt = t->named_type();
2755 : 688524 : if (nt->is_alias())
2756 : : return TRAVERSE_CONTINUE;
2757 : 665338 : if (t->needs_specific_type_functions(this->gogo_))
2758 : 360253 : 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 : 665338 : Type* rt = nt->real_type();
2763 : 665338 : if (rt->struct_type() == NULL)
2764 : : {
2765 : 256642 : 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 : 408696 : bool is_defined_elsewhere = nt->named_object()->package() != NULL;
2773 : 817392 : const Struct_field_list* fields = rt->struct_type()->fields();
2774 : 2686662 : for (Struct_field_list::const_iterator p = fields->begin();
2775 : 2686662 : p != fields->end();
2776 : 2277966 : ++p)
2777 : : {
2778 : 3848195 : if (is_defined_elsewhere
2779 : 2277966 : && Gogo::is_hidden_name(p->field_name()))
2780 : 1570229 : continue;
2781 : 707737 : if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
2782 : 0 : return TRAVERSE_EXIT;
2783 : : }
2784 : : }
2785 : :
2786 : : return TRAVERSE_SKIP_COMPONENTS;
2787 : : }
2788 : :
2789 : 679966 : case Type::TYPE_STRUCT:
2790 : 679966 : case Type::TYPE_ARRAY:
2791 : 679966 : if (t->needs_specific_type_functions(this->gogo_))
2792 : 165311 : t->equal_function(this->gogo_, NULL, NULL);
2793 : : break;
2794 : :
2795 : 77559 : case Type::TYPE_MAP:
2796 : 77559 : {
2797 : 155118 : Type* key_type = t->map_type()->key_type()->unalias();
2798 : 77559 : if (key_type->needs_specific_type_functions(this->gogo_))
2799 : 37907 : 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 : 4642 : Gogo::write_specific_type_functions()
2814 : : {
2815 : 4642 : Specific_type_functions stf(this);
2816 : 4642 : this->traverse(&stf);
2817 : :
2818 : 9360 : 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 : 4642 : this->specific_type_functions_are_written_ = true;
2831 : 4642 : }
2832 : :
2833 : : // Traverse the tree.
2834 : :
2835 : : void
2836 : 93367 : 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 : 93367 : if (this->package_->bindings()->traverse(traverse, true) == TRAVERSE_EXIT)
2842 : : return;
2843 : 2497379 : for (Packages::const_iterator p = this->packages_.begin();
2844 : 2497379 : p != this->packages_.end();
2845 : 2404012 : ++p)
2846 : : {
2847 : 2404012 : if (p->second != this->package_)
2848 : : {
2849 : 2310645 : 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 : 9284 : class Verify_types : public Traverse
2867 : : {
2868 : : public:
2869 : 4642 : Verify_types(Gogo* gogo)
2870 : 4642 : : Traverse(traverse_types),
2871 : 4642 : 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 : 9929545 : Verify_types::type(Type* t)
2885 : : {
2886 : 9929545 : 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 : 4642 : Gogo::verify_types()
2895 : : {
2896 : 4642 : Verify_types traverse(this);
2897 : 4642 : this->traverse(&traverse);
2898 : :
2899 : 4642 : for (std::vector<Type*>::iterator p = this->verify_types_.begin();
2900 : 6994 : p != this->verify_types_.end();
2901 : 2352 : ++p)
2902 : 2352 : (*p)->verify(this);
2903 : 4642 : this->verify_types_.clear();
2904 : 4642 : }
2905 : :
2906 : : // Traversal class used to lower parse tree.
2907 : :
2908 : 4642 : class Lower_parse_tree : public Traverse
2909 : : {
2910 : : public:
2911 : 1842937 : 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 : 1842937 : gogo_(gogo), function_(function), inserter_()
2918 : : { }
2919 : :
2920 : : void
2921 : 509737 : set_inserter(const Statement_inserter* inserter)
2922 : 509737 : { 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 : 1303379 : Lower_parse_tree::variable(Named_object* no)
2952 : : {
2953 : 1303379 : if (!no->is_variable())
2954 : : return TRAVERSE_CONTINUE;
2955 : :
2956 : 1160442 : 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 : 348686 : no->var_value()->lower_init_expression(this->gogo_, this->function_,
2961 : : &this->inserter_);
2962 : 348686 : 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 : 811756 : if (no->var_value()->has_type())
2971 : : {
2972 : 811756 : Type* type = no->var_value()->type();
2973 : 811756 : if (type != NULL)
2974 : : {
2975 : 811756 : if (Type::traverse(type, this) == TRAVERSE_EXIT)
2976 : : return TRAVERSE_EXIT;
2977 : : }
2978 : : }
2979 : 811756 : 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 : 2175643 : Lower_parse_tree::constant(Named_object* no, bool)
2990 : : {
2991 : 2175643 : 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 : 2175643 : if (nc->lowering())
2996 : : return TRAVERSE_CONTINUE;
2997 : 2175643 : nc->set_lowering();
2998 : :
2999 : 2175643 : nc->traverse_expression(this);
3000 : :
3001 : 2175643 : nc->clear_lowering();
3002 : :
3003 : : // We will traverse the expression a second time, but that will be
3004 : : // fast.
3005 : :
3006 : 2175643 : 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 : 175646 : Lower_parse_tree::function(Named_object* no)
3015 : : {
3016 : 175646 : go_assert(this->function_ == NULL);
3017 : 175646 : this->function_ = no;
3018 : 175646 : int t = no->func_value()->traverse(this);
3019 : 175646 : this->function_ = NULL;
3020 : :
3021 : 175646 : if (t == TRAVERSE_EXIT)
3022 : 0 : return t;
3023 : : return TRAVERSE_SKIP_COMPONENTS;
3024 : : }
3025 : :
3026 : : // Lower statement parse trees.
3027 : :
3028 : : int
3029 : 6343489 : 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 : 6343489 : if (sorig->is_block_statement())
3035 : : return TRAVERSE_CONTINUE;
3036 : :
3037 : 5281541 : Statement_inserter hold_inserter(this->inserter_);
3038 : 5281541 : this->inserter_ = Statement_inserter(block, pindex);
3039 : :
3040 : : // Lower the expressions first.
3041 : 5281541 : int t = sorig->traverse_contents(this);
3042 : 5281541 : 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 : 5997347 : while (true)
3051 : : {
3052 : 5997347 : Statement* snew = s->lower(this->gogo_, this->function_, block,
3053 : : &this->inserter_);
3054 : 5997347 : if (snew == s)
3055 : : break;
3056 : 715806 : s = snew;
3057 : 715806 : t = s->traverse_contents(this);
3058 : 715806 : if (t == TRAVERSE_EXIT)
3059 : : {
3060 : 0 : this->inserter_ = hold_inserter;
3061 : 0 : return t;
3062 : : }
3063 : : }
3064 : :
3065 : 5281541 : if (s != sorig)
3066 : 656990 : block->replace_statement(*pindex, s);
3067 : :
3068 : 5281541 : this->inserter_ = hold_inserter;
3069 : 5281541 : return TRAVERSE_SKIP_COMPONENTS;
3070 : : }
3071 : :
3072 : : // Lower expression parse trees.
3073 : :
3074 : : int
3075 : 29621465 : 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 : 29621465 : if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
3081 : : return TRAVERSE_EXIT;
3082 : : // Keep lowering until nothing changes.
3083 : 33650925 : while (true)
3084 : : {
3085 : 31636195 : Expression* e = *pexpr;
3086 : 31636195 : Expression* enew = e->lower(this->gogo_, this->function_,
3087 : : &this->inserter_);
3088 : 31636195 : if (enew == e)
3089 : : break;
3090 : 2014730 : if (enew->traverse_subexpressions(this) == TRAVERSE_EXIT)
3091 : : return TRAVERSE_EXIT;
3092 : 2014730 : *pexpr = enew;
3093 : 2014730 : }
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 : 29621465 : if ((*pexpr)->unknown_expression() == NULL)
3100 : 29621465 : 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 : 4642 : Gogo::lower_parse_tree()
3110 : : {
3111 : 4642 : Lower_parse_tree lower_parse_tree(this, NULL);
3112 : 4642 : 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 : 16889 : 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 : 4642 : for (std::vector<Type*>::iterator p = this->verify_types_.begin();
3130 : 4642 : p != this->verify_types_.end();
3131 : 0 : ++p)
3132 : 0 : Type::traverse(*p, &lower_parse_tree);
3133 : 4642 : }
3134 : :
3135 : : // Lower a block.
3136 : :
3137 : : void
3138 : 121449 : Gogo::lower_block(Named_object* function, Block* block)
3139 : : {
3140 : 121449 : Lower_parse_tree lower_parse_tree(this, function);
3141 : 121449 : block->traverse(&lower_parse_tree);
3142 : 121449 : }
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 : 526286 : Gogo::lower_expression(Named_object* function, Statement_inserter* inserter,
3149 : : Expression** pexpr)
3150 : : {
3151 : 526286 : Lower_parse_tree lower_parse_tree(this, function);
3152 : 526286 : if (inserter != NULL)
3153 : 509737 : lower_parse_tree.set_inserter(inserter);
3154 : 526286 : lower_parse_tree.expression(pexpr);
3155 : 526286 : }
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 : 1190560 : Gogo::lower_constant(Named_object* no)
3163 : : {
3164 : 1190560 : go_assert(no->is_const());
3165 : 1190560 : Lower_parse_tree lower(this, NULL);
3166 : 1190560 : lower.constant(no, false);
3167 : 1190560 : }
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 : 36566 : class Add_conversions : public Traverse
3174 : : {
3175 : : public:
3176 : 18283 : Add_conversions()
3177 : : : Traverse(traverse_statements
3178 : 18283 : | 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 : 3845080 : Add_conversions::statement(Block*, size_t*, Statement* sorig)
3192 : : {
3193 : 3845080 : sorig->add_conversions();
3194 : 3845080 : return TRAVERSE_CONTINUE;
3195 : : }
3196 : :
3197 : : // Add explicit conversions in an expression.
3198 : :
3199 : : int
3200 : 11356385 : Add_conversions::expression(Expression** pexpr)
3201 : : {
3202 : 11356385 : (*pexpr)->add_conversions();
3203 : 11356385 : return TRAVERSE_CONTINUE;
3204 : : }
3205 : :
3206 : : void
3207 : 4642 : Gogo::add_conversions()
3208 : : {
3209 : 4642 : Add_conversions add_conversions;
3210 : 4642 : this->traverse(&add_conversions);
3211 : 4642 : }
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 : 9284 : class Remove_deadcode : public Traverse
3223 : : {
3224 : : public:
3225 : 4642 : Remove_deadcode(Gogo* gogo)
3226 : 4642 : : Traverse(traverse_statements
3227 : : | traverse_expressions),
3228 : 4642 : 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 : 3792801 : Remove_deadcode::statement(Block* block, size_t* pindex, Statement* sorig)
3245 : : {
3246 : 3792801 : Location loc = sorig->location();
3247 : 3792801 : 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 : 3792801 : return TRAVERSE_CONTINUE;
3271 : : }
3272 : :
3273 : : // Remove deadcode in an expression.
3274 : :
3275 : : int
3276 : 11054673 : Remove_deadcode::expression(Expression** pexpr)
3277 : : {
3278 : : // Discard the right arm of a shortcut expression of constant value.
3279 : 11054673 : 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 : 11054673 : return TRAVERSE_CONTINUE;
3291 : : }
3292 : :
3293 : : // Remove deadcode.
3294 : :
3295 : : void
3296 : 4642 : Gogo::remove_deadcode()
3297 : : {
3298 : 4642 : Remove_deadcode remove_deadcode(this);
3299 : 4642 : this->traverse(&remove_deadcode);
3300 : 4642 : }
3301 : :
3302 : : // Traverse the tree to create function descriptors as needed.
3303 : :
3304 : 9284 : class Create_function_descriptors : public Traverse
3305 : : {
3306 : : public:
3307 : 4642 : Create_function_descriptors(Gogo* gogo)
3308 : 4642 : : Traverse(traverse_functions | traverse_expressions),
3309 : 4642 : 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 : 188601 : Create_function_descriptors::function(Named_object* no)
3330 : : {
3331 : 188601 : if (Create_function_descriptors::skip_descriptor(this->gogo_, no))
3332 : : return TRAVERSE_CONTINUE;
3333 : :
3334 : 188601 : if (no->is_function()
3335 : 188601 : && no->func_value()->enclosing() == NULL
3336 : 164287 : && !no->func_value()->is_method()
3337 : 274847 : && ((!Gogo::is_hidden_name(no->name())
3338 : 44008 : && !Gogo::is_thunk(no))
3339 : 43544 : || no->func_value()->is_referenced_by_inline()))
3340 : 42702 : 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 : 10257214 : Create_function_descriptors::expression(Expression** pexpr)
3350 : : {
3351 : 10257214 : Expression* expr = *pexpr;
3352 : :
3353 : 10257214 : Func_expression* fe = expr->func_expression();
3354 : 10257214 : 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 : 46981 : if (fe->closure() != NULL)
3360 : : return TRAVERSE_CONTINUE;
3361 : 37241 : Named_object* no = fe->named_object();
3362 : 37241 : if (no->is_function() && !no->func_value()->is_method())
3363 : 21531 : 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 : 37241 : return TRAVERSE_CONTINUE;
3369 : : }
3370 : :
3371 : 10210233 : Bound_method_expression* bme = expr->bound_method_expression();
3372 : 10210233 : 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 : 10209299 : Interface_field_reference_expression* ifre =
3382 : 10209299 : expr->interface_field_reference_expression();
3383 : 10209299 : 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 : 10209194 : 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 : 797457 : Expression_list* args = ce->args();
3404 : 797457 : 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 : 797457 : 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 : 189198 : Create_function_descriptors::skip_descriptor(Gogo* gogo,
3427 : : const Named_object* no)
3428 : : {
3429 : 189198 : const std::string& pkgpath(no->package() == NULL
3430 : 189198 : ? 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 : 189198 : return ((pkgpath == "internal/abi"
3438 : 189190 : || pkgpath == "bootstrap/internal/abi")
3439 : 189198 : && (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 : 4642 : 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 : 4642 : std::vector<Named_object*> fndecls;
3455 : 4642 : Bindings* b = this->package_->bindings();
3456 : 337950 : for (Bindings::const_declarations_iterator p = b->begin_declarations();
3457 : 337950 : p != b->end_declarations();
3458 : 333308 : ++p)
3459 : : {
3460 : 333308 : Named_object* no = p->second;
3461 : 333308 : 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 : 333905 : && !Create_function_descriptors::skip_descriptor(this, no))
3466 : 589 : fndecls.push_back(no);
3467 : : }
3468 : 5231 : for (std::vector<Named_object*>::const_iterator p = fndecls.begin();
3469 : 5231 : p != fndecls.end();
3470 : 589 : ++p)
3471 : 589 : (*p)->func_declaration_value()->descriptor(this, *p);
3472 : 4642 : fndecls.clear();
3473 : :
3474 : 4642 : Create_function_descriptors cfd(this);
3475 : 4642 : this->traverse(&cfd);
3476 : 4642 : }
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 : 9284 : class Lower_builtin_calls : public Traverse
3485 : : {
3486 : : public:
3487 : 4642 : Lower_builtin_calls(Gogo* gogo)
3488 : 4642 : : Traverse(traverse_expressions),
3489 : 4642 : gogo_(gogo)
3490 : : { }
3491 : :
3492 : : int
3493 : : expression(Expression**);
3494 : :
3495 : : private:
3496 : : Gogo* gogo_;
3497 : : };
3498 : :
3499 : : int
3500 : 8979794 : Lower_builtin_calls::expression(Expression** pexpr)
3501 : : {
3502 : 8979794 : Call_expression* ce = (*pexpr)->call_expression();
3503 : 836475 : if (ce != NULL)
3504 : 836475 : *pexpr = ce->lower_builtin(this->gogo_);
3505 : 8979794 : return TRAVERSE_CONTINUE;
3506 : : }
3507 : :
3508 : : void
3509 : 4642 : Gogo::lower_builtin_calls()
3510 : : {
3511 : 4642 : Lower_builtin_calls lbc(this);
3512 : 4642 : this->traverse(&lbc);
3513 : 4642 : }
3514 : :
3515 : : // Finalize the methods of an interface type.
3516 : :
3517 : : int
3518 : 25413745 : 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 : 25413745 : switch (t->classification())
3523 : : {
3524 : 272370 : case Type::TYPE_INTERFACE:
3525 : 272370 : t->interface_type()->finalize_methods();
3526 : 272370 : break;
3527 : :
3528 : 2286214 : case Type::TYPE_NAMED:
3529 : 2286214 : {
3530 : 2286214 : Named_type* nt = t->named_type();
3531 : :
3532 : 2286214 : if (nt->is_alias())
3533 : : return TRAVERSE_CONTINUE;
3534 : :
3535 : 2192329 : Type* rt = nt->real_type();
3536 : 2192329 : if (rt->classification() != Type::TYPE_STRUCT)
3537 : : {
3538 : : // Finalize the methods of the real type first.
3539 : 930644 : if (Type::traverse(rt, this) == TRAVERSE_EXIT)
3540 : : return TRAVERSE_EXIT;
3541 : :
3542 : : // Finalize the methods of this type.
3543 : 930644 : 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 : 2523370 : const Struct_field_list* fields = rt->struct_type()->fields();
3559 : 1261685 : if (fields != NULL)
3560 : : {
3561 : 8238299 : for (Struct_field_list::const_iterator pf = fields->begin();
3562 : 8238299 : pf != fields->end();
3563 : 6976614 : ++pf)
3564 : : {
3565 : 6976614 : if (pf->is_anonymous())
3566 : : {
3567 : 124550 : 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 : 1261685 : nt->finalize_methods(this->gogo_);
3575 : :
3576 : : // Finalize all the struct fields.
3577 : 2523370 : 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 : 2192329 : if (nt->named_object()->package() != NULL && nt->has_any_methods())
3584 : : {
3585 : 1041037 : const Methods* methods = nt->methods();
3586 : 8544690 : for (Methods::const_iterator p = methods->begin();
3587 : 8544690 : p != methods->end();
3588 : 7503653 : ++p)
3589 : : {
3590 : 7503653 : if (Type::traverse(p->second->type(), this) == TRAVERSE_EXIT)
3591 : 0 : return TRAVERSE_EXIT;
3592 : : }
3593 : : }
3594 : :
3595 : : // Finalize the types of all methods that are declared but not
3596 : : // defined, since we won't see the declarations otherwise.
3597 : 2192329 : if (nt->named_object()->package() == NULL
3598 : 2192329 : && nt->local_methods() != NULL)
3599 : : {
3600 : 11671 : const Bindings* methods = nt->local_methods();
3601 : 64897 : for (Bindings::const_declarations_iterator p =
3602 : 11671 : methods->begin_declarations();
3603 : 64897 : p != methods->end_declarations();
3604 : 53226 : p++)
3605 : : {
3606 : 53226 : if (p->second->is_function_declaration())
3607 : : {
3608 : 63 : Type* mt = p->second->func_declaration_value()->type();
3609 : 63 : if (Type::traverse(mt, this) == TRAVERSE_EXIT)
3610 : 0 : return TRAVERSE_EXIT;
3611 : : }
3612 : : }
3613 : : }
3614 : :
3615 : : return TRAVERSE_SKIP_COMPONENTS;
3616 : : }
3617 : :
3618 : 119350 : case Type::TYPE_STRUCT:
3619 : : // Traverse the field types first in case there is an embedded
3620 : : // field with methods that the struct should inherit.
3621 : 238700 : if (t->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT)
3622 : : return TRAVERSE_EXIT;
3623 : 119350 : t->struct_type()->finalize_methods(this->gogo_);
3624 : 119350 : return TRAVERSE_SKIP_COMPONENTS;
3625 : :
3626 : : default:
3627 : : break;
3628 : : }
3629 : :
3630 : : return TRAVERSE_CONTINUE;
3631 : : }
3632 : :
3633 : : // Finalize method lists and build stub methods for types.
3634 : :
3635 : : void
3636 : 4642 : Gogo::finalize_methods()
3637 : : {
3638 : 4642 : Finalize_methods finalize(this);
3639 : 4642 : this->traverse(&finalize);
3640 : 4642 : }
3641 : :
3642 : : // Finalize the method list for a type. This is called when a type is
3643 : : // parsed for an inlined function body, which happens after the
3644 : : // finalize_methods pass.
3645 : :
3646 : : void
3647 : 563 : Gogo::finalize_methods_for_type(Type* type)
3648 : : {
3649 : 563 : Finalize_methods finalize(this);
3650 : 563 : Type::traverse(type, &finalize);
3651 : 563 : }
3652 : :
3653 : : // Set types for unspecified variables and constants.
3654 : :
3655 : : void
3656 : 4642 : Gogo::determine_types()
3657 : : {
3658 : 4642 : this->current_bindings()->determine_types(this);
3659 : :
3660 : : // Determine the types of constants in packages.
3661 : 120871 : for (Packages::const_iterator p = this->packages_.begin();
3662 : 120871 : p != this->packages_.end();
3663 : 116229 : ++p)
3664 : 116229 : p->second->determine_types(this);
3665 : 4642 : }
3666 : :
3667 : : // Traversal class used for type checking.
3668 : :
3669 : 4642 : class Check_types_traverse : public Traverse
3670 : : {
3671 : : public:
3672 : 18283 : Check_types_traverse(Gogo* gogo)
3673 : 18283 : : Traverse(traverse_variables
3674 : : | traverse_constants
3675 : : | traverse_functions
3676 : : | traverse_statements
3677 : : | traverse_expressions),
3678 : 18283 : gogo_(gogo)
3679 : : { }
3680 : :
3681 : : int
3682 : : variable(Named_object*);
3683 : :
3684 : : int
3685 : : constant(Named_object*, bool);
3686 : :
3687 : : int
3688 : : function(Named_object*);
3689 : :
3690 : : int
3691 : : statement(Block*, size_t* pindex, Statement*);
3692 : :
3693 : : int
3694 : : expression(Expression**);
3695 : :
3696 : : private:
3697 : : // General IR.
3698 : : Gogo* gogo_;
3699 : : };
3700 : :
3701 : : // Check that a variable initializer has the right type.
3702 : :
3703 : : int
3704 : 1116090 : Check_types_traverse::variable(Named_object* named_object)
3705 : : {
3706 : 1116090 : if (named_object->is_variable())
3707 : : {
3708 : 984660 : Variable* var = named_object->var_value();
3709 : :
3710 : : // Give error if variable type is not defined.
3711 : 984660 : var->type()->base();
3712 : :
3713 : 984660 : Expression* init = var->init();
3714 : 984660 : std::string reason;
3715 : 984660 : if (init != NULL
3716 : 1283474 : && !Type::are_assignable(var->type(), init->type(), &reason))
3717 : : {
3718 : 45 : if (reason.empty())
3719 : 0 : go_error_at(var->location(), "incompatible type in initialization");
3720 : : else
3721 : 45 : go_error_at(var->location(),
3722 : : "incompatible type in initialization (%s)",
3723 : : reason.c_str());
3724 : 45 : init = Expression::make_error(named_object->location());
3725 : 45 : var->clear_init();
3726 : : }
3727 : 984615 : else if (init != NULL
3728 : 984615 : && init->func_expression() != NULL)
3729 : : {
3730 : 3221 : Named_object* no = init->func_expression()->named_object();
3731 : 3221 : Function_type* fntype;
3732 : 3221 : if (no->is_function())
3733 : 3049 : fntype = no->func_value()->type();
3734 : 172 : else if (no->is_function_declaration())
3735 : 172 : fntype = no->func_declaration_value()->type();
3736 : : else
3737 : 0 : go_unreachable();
3738 : :
3739 : : // Builtin functions cannot be used as function values for variable
3740 : : // initialization.
3741 : 3221 : if (fntype->is_builtin())
3742 : : {
3743 : 1 : go_error_at(init->location(),
3744 : : "invalid use of special built-in function %qs; "
3745 : : "must be called",
3746 : 2 : no->message_name().c_str());
3747 : : }
3748 : : }
3749 : :
3750 : 984660 : if (!var->is_used()
3751 : 368859 : && !var->is_global()
3752 : 51089 : && !var->is_parameter()
3753 : 35 : && !var->is_receiver()
3754 : 35 : && !var->type()->is_error()
3755 : 13 : && (init == NULL || !init->is_error_expression())
3756 : 984668 : && !Lex::is_invalid_identifier(named_object->name()))
3757 : : {
3758 : : // Avoid giving an error if the initializer is invalid.
3759 : 7 : if (init != NULL)
3760 : 6 : init->check_types(this->gogo_);
3761 : :
3762 : 6 : if (init == NULL || !init->is_error_expression())
3763 : 6 : go_error_at(var->location(), "%qs declared but not used",
3764 : 12 : named_object->message_name().c_str());
3765 : : }
3766 : 984660 : }
3767 : 1116090 : return TRAVERSE_CONTINUE;
3768 : : }
3769 : :
3770 : : // Check that a constant initializer has the right type.
3771 : :
3772 : : int
3773 : 984579 : Check_types_traverse::constant(Named_object* named_object, bool)
3774 : : {
3775 : 984579 : Named_constant* constant = named_object->const_value();
3776 : 984579 : Type* ctype = constant->type();
3777 : 1018198 : if (ctype->integer_type() == NULL
3778 : 1011771 : && ctype->float_type() == NULL
3779 : 1011727 : && ctype->complex_type() == NULL
3780 : 27173 : && !ctype->is_boolean_type()
3781 : 25646 : && !ctype->is_string_type())
3782 : : {
3783 : 25 : if (ctype->is_nil_type())
3784 : 1 : go_error_at(constant->location(), "const initializer cannot be nil");
3785 : 24 : else if (!ctype->is_error())
3786 : 15 : go_error_at(constant->location(), "invalid constant type");
3787 : 25 : constant->set_error();
3788 : : }
3789 : 984554 : else if (constant->expr()->is_error_expression())
3790 : : {
3791 : 3 : go_assert(saw_errors());
3792 : 3 : constant->set_error();
3793 : : }
3794 : 984551 : else if (!constant->expr()->is_constant())
3795 : : {
3796 : 12 : go_error_at(constant->expr()->location(), "expression is not constant");
3797 : 12 : constant->set_error();
3798 : : }
3799 : 984539 : else if (!Type::are_assignable(constant->type(), constant->expr()->type(),
3800 : : NULL))
3801 : : {
3802 : 1 : go_error_at(constant->location(),
3803 : : "initialization expression has wrong type");
3804 : 1 : constant->set_error();
3805 : : }
3806 : 984579 : return TRAVERSE_CONTINUE;
3807 : : }
3808 : :
3809 : : // There are no types to check in a function, but this is where we
3810 : : // issue warnings about labels which are defined but not referenced.
3811 : :
3812 : : int
3813 : 175573 : Check_types_traverse::function(Named_object* no)
3814 : : {
3815 : 175573 : no->func_value()->check_labels();
3816 : 175573 : return TRAVERSE_CONTINUE;
3817 : : }
3818 : :
3819 : : // Check that types are valid in a statement.
3820 : :
3821 : : int
3822 : 1852289 : Check_types_traverse::statement(Block*, size_t*, Statement* s)
3823 : : {
3824 : 1852289 : s->check_types(this->gogo_);
3825 : 1852289 : return TRAVERSE_CONTINUE;
3826 : : }
3827 : :
3828 : : // Check that types are valid in an expression.
3829 : :
3830 : : int
3831 : 9611198 : Check_types_traverse::expression(Expression** expr)
3832 : : {
3833 : 9611198 : (*expr)->check_types(this->gogo_);
3834 : 9611198 : return TRAVERSE_CONTINUE;
3835 : : }
3836 : :
3837 : : // Check that types are valid.
3838 : :
3839 : : void
3840 : 4642 : Gogo::check_types()
3841 : : {
3842 : 4642 : Check_types_traverse traverse(this);
3843 : 4642 : this->traverse(&traverse);
3844 : :
3845 : 4642 : Bindings* bindings = this->current_bindings();
3846 : 337877 : for (Bindings::const_declarations_iterator p = bindings->begin_declarations();
3847 : 337877 : p != bindings->end_declarations();
3848 : 333235 : ++p)
3849 : : {
3850 : : // Also check the types in a function declaration's signature.
3851 : 333235 : Named_object* no = p->second;
3852 : 333235 : if (no->is_function_declaration())
3853 : 3904 : no->func_declaration_value()->check_types();
3854 : : }
3855 : 4642 : }
3856 : :
3857 : : // Check the types in a single block.
3858 : :
3859 : : void
3860 : 13641 : Gogo::check_types_in_block(Block* block)
3861 : : {
3862 : 13641 : Check_types_traverse traverse(this);
3863 : 13641 : block->traverse(&traverse);
3864 : 13641 : }
3865 : :
3866 : : // For each global variable defined in the current package, record the
3867 : : // set of variables that its initializer depends on. We do this after
3868 : : // lowering so that all unknown names are resolved to their final
3869 : : // locations. We do this before write barrier insertion because that
3870 : : // makes it harder to distinguish references from assignments in
3871 : : // preinit blocks.
3872 : :
3873 : : void
3874 : 4642 : Gogo::record_global_init_refs()
3875 : : {
3876 : 4642 : Bindings* bindings = this->package_->bindings();
3877 : 454531 : for (Bindings::const_definitions_iterator pb = bindings->begin_definitions();
3878 : 454531 : pb != bindings->end_definitions();
3879 : 449889 : pb++)
3880 : : {
3881 : 449889 : Named_object* no = *pb;
3882 : 449889 : if (!no->is_variable())
3883 : 413190 : continue;
3884 : :
3885 : 36699 : Variable* var = no->var_value();
3886 : 36699 : go_assert(var->is_global());
3887 : :
3888 : 36699 : Find_vars find_vars;
3889 : 36699 : Expression* init = var->init();
3890 : 36699 : if (init != NULL)
3891 : 20354 : Expression::traverse(&init, &find_vars);
3892 : 36699 : if (var->has_pre_init())
3893 : 121 : var->preinit()->traverse(&find_vars);
3894 : 36699 : Named_object* dep = this->var_depends_on(var);
3895 : 36699 : if (dep != NULL)
3896 : : {
3897 : 99 : Expression* dinit = dep->var_value()->init();
3898 : 99 : if (dinit != NULL)
3899 : 95 : Expression::traverse(&dinit, &find_vars);
3900 : 99 : if (dep->var_value()->has_pre_init())
3901 : 99 : dep->var_value()->preinit()->traverse(&find_vars);
3902 : : }
3903 : :
3904 : 48349 : for (Find_vars::const_iterator pv = find_vars.begin();
3905 : 48349 : pv != find_vars.end();
3906 : 11650 : ++pv)
3907 : 11650 : var->add_init_ref(*pv);
3908 : 36699 : }
3909 : 4642 : }
3910 : :
3911 : : // A traversal class which finds all the expressions which must be
3912 : : // evaluated in order within a statement or larger expression. This
3913 : : // is used to implement the rules about order of evaluation.
3914 : :
3915 : : class Find_eval_ordering : public Traverse
3916 : : {
3917 : : private:
3918 : : typedef std::vector<Expression**> Expression_pointers;
3919 : :
3920 : : public:
3921 : 4733298 : Find_eval_ordering()
3922 : : : Traverse(traverse_blocks
3923 : : | traverse_statements
3924 : : | traverse_expressions),
3925 : 4733298 : exprs_()
3926 : : { }
3927 : :
3928 : : size_t
3929 : 4631414 : size() const
3930 : 4631414 : { return this->exprs_.size(); }
3931 : :
3932 : : typedef Expression_pointers::const_iterator const_iterator;
3933 : :
3934 : : const_iterator
3935 : 426909 : begin() const
3936 : 426909 : { return this->exprs_.begin(); }
3937 : :
3938 : : const_iterator
3939 : 1102310 : end() const
3940 : 711 : { return this->exprs_.end(); }
3941 : :
3942 : : protected:
3943 : : int
3944 : 1682942 : block(Block*)
3945 : 1682942 : { return TRAVERSE_SKIP_COMPONENTS; }
3946 : :
3947 : : int
3948 : 0 : statement(Block*, size_t*, Statement*)
3949 : 0 : { return TRAVERSE_SKIP_COMPONENTS; }
3950 : :
3951 : : int
3952 : : expression(Expression**);
3953 : :
3954 : : private:
3955 : : // A list of pointers to expressions with side-effects.
3956 : : Expression_pointers exprs_;
3957 : : };
3958 : :
3959 : : // If an expression must be evaluated in order, put it on the list.
3960 : :
3961 : : int
3962 : 12437975 : Find_eval_ordering::expression(Expression** expression_pointer)
3963 : : {
3964 : 12437975 : Binary_expression* binexp = (*expression_pointer)->binary_expression();
3965 : 739919 : if (binexp != NULL
3966 : 739919 : && (binexp->op() == OPERATOR_ANDAND || binexp->op() == OPERATOR_OROR))
3967 : : {
3968 : : // Shortcut expressions may potentially have side effects which need
3969 : : // to be ordered, so add them to the list.
3970 : : // We don't order its subexpressions here since they may be evaluated
3971 : : // conditionally. This is handled in remove_shortcuts.
3972 : 86463 : this->exprs_.push_back(expression_pointer);
3973 : 86463 : return TRAVERSE_SKIP_COMPONENTS;
3974 : : }
3975 : :
3976 : : // We have to look at subexpressions before this one.
3977 : 12351512 : if ((*expression_pointer)->traverse_subexpressions(this) == TRAVERSE_EXIT)
3978 : : return TRAVERSE_EXIT;
3979 : 12351512 : if ((*expression_pointer)->must_eval_in_order())
3980 : 898881 : this->exprs_.push_back(expression_pointer);
3981 : : return TRAVERSE_SKIP_COMPONENTS;
3982 : : }
3983 : :
3984 : : // A traversal class for ordering evaluations.
3985 : :
3986 : 367424 : class Order_eval : public Traverse
3987 : : {
3988 : : public:
3989 : 183712 : Order_eval(Gogo* gogo)
3990 : 183712 : : Traverse(traverse_variables
3991 : : | traverse_statements),
3992 : 183712 : gogo_(gogo)
3993 : : { }
3994 : :
3995 : : int
3996 : : variable(Named_object*);
3997 : :
3998 : : int
3999 : : statement(Block*, size_t*, Statement*);
4000 : :
4001 : : private:
4002 : : // The IR.
4003 : : Gogo* gogo_;
4004 : : };
4005 : :
4006 : : // Implement the order of evaluation rules for a statement.
4007 : :
4008 : : int
4009 : 4712944 : Order_eval::statement(Block* block, size_t* pindex, Statement* stmt)
4010 : : {
4011 : : // FIXME: This approach doesn't work for switch statements, because
4012 : : // we add the new statements before the whole switch when we need to
4013 : : // instead add them just before the switch expression. The right
4014 : : // fix is probably to lower switch statements with nonconstant cases
4015 : : // to a series of conditionals.
4016 : 4712944 : if (stmt->switch_statement() != NULL)
4017 : : return TRAVERSE_CONTINUE;
4018 : :
4019 : 4712944 : Find_eval_ordering find_eval_ordering;
4020 : :
4021 : : // If S is a variable declaration, then ordinary traversal won't do
4022 : : // anything. We want to explicitly traverse the initialization
4023 : : // expression if there is one.
4024 : 4712944 : Variable_declaration_statement* vds = stmt->variable_declaration_statement();
4025 : 4712944 : Expression* init = NULL;
4026 : 4712944 : Expression* orig_init = NULL;
4027 : 4712944 : if (vds == NULL)
4028 : 4330310 : stmt->traverse_contents(&find_eval_ordering);
4029 : : else
4030 : : {
4031 : 382634 : init = vds->var()->var_value()->init();
4032 : 382634 : if (init == NULL)
4033 : : return TRAVERSE_CONTINUE;
4034 : 280750 : orig_init = init;
4035 : :
4036 : : // It might seem that this could be
4037 : : // init->traverse_subexpressions. Unfortunately that can fail
4038 : : // in a case like
4039 : : // var err os.Error
4040 : : // newvar, err := call(arg())
4041 : : // Here newvar will have an init of call result 0 of
4042 : : // call(arg()). If we only traverse subexpressions, we will
4043 : : // only find arg(), and we won't bother to move anything out.
4044 : : // Then we get to the assignment to err, we will traverse the
4045 : : // whole statement, and this time we will find both call() and
4046 : : // arg(), and so we will move them out. This will cause them to
4047 : : // be put into temporary variables before the assignment to err
4048 : : // but after the declaration of newvar. To avoid that problem,
4049 : : // we traverse the entire expression here.
4050 : 280750 : Expression::traverse(&init, &find_eval_ordering);
4051 : : }
4052 : :
4053 : 4611060 : size_t c = find_eval_ordering.size();
4054 : 4611060 : if (c == 0)
4055 : : return TRAVERSE_CONTINUE;
4056 : :
4057 : : // If there is only one expression with a side-effect, we can
4058 : : // usually leave it in place.
4059 : 732264 : if (c == 1)
4060 : : {
4061 : 516478 : switch (stmt->classification())
4062 : : {
4063 : : case Statement::STATEMENT_ASSIGNMENT:
4064 : : // For an assignment statement, we need to evaluate an
4065 : : // expression on the right hand side before we evaluate any
4066 : : // index expression on the left hand side, so for that case
4067 : : // we always move the expression. Otherwise we mishandle
4068 : : // m[0] = len(m) where m is a map.
4069 : : break;
4070 : :
4071 : 49625 : case Statement::STATEMENT_EXPRESSION:
4072 : 49625 : {
4073 : : // If this is a call statement that doesn't return any
4074 : : // values, it will not have been counted as a value to
4075 : : // move. We need to move any subexpressions in case they
4076 : : // are themselves call statements that require passing a
4077 : : // closure.
4078 : 49625 : Expression* expr = stmt->expression_statement()->expr();
4079 : 49625 : if (expr->call_expression() != NULL
4080 : 48371 : && expr->call_expression()->result_count() == 0)
4081 : : break;
4082 : 37239 : return TRAVERSE_CONTINUE;
4083 : : }
4084 : :
4085 : : default:
4086 : : // We can leave the expression in place.
4087 : : return TRAVERSE_CONTINUE;
4088 : : }
4089 : : }
4090 : :
4091 : 425848 : bool is_thunk = stmt->thunk_statement() != NULL;
4092 : 425848 : Expression_statement* es = stmt->expression_statement();
4093 : 1091421 : for (Find_eval_ordering::const_iterator p = find_eval_ordering.begin();
4094 : 1091421 : p != find_eval_ordering.end();
4095 : 665573 : ++p)
4096 : : {
4097 : 665907 : Expression** pexpr = *p;
4098 : :
4099 : : // The last expression in a thunk will be the call passed to go
4100 : : // or defer, which we must not evaluate early.
4101 : 665907 : if (is_thunk && p + 1 == find_eval_ordering.end())
4102 : : break;
4103 : :
4104 : 665573 : Location loc = (*pexpr)->location();
4105 : 665573 : Statement* s;
4106 : 665573 : if ((*pexpr)->call_expression() == NULL
4107 : 470516 : || (*pexpr)->call_expression()->result_count() < 2)
4108 : : {
4109 : 495326 : Temporary_statement* ts = Statement::make_temporary(NULL, *pexpr,
4110 : : loc);
4111 : 495326 : s = ts;
4112 : 495326 : *pexpr = Expression::make_temporary_reference(ts, loc);
4113 : : }
4114 : : else
4115 : : {
4116 : : // A call expression which returns multiple results needs to
4117 : : // be handled specially. We can't create a temporary
4118 : : // because there is no type to give it. Any actual uses of
4119 : : // the values will be done via Call_result_expressions.
4120 : : //
4121 : : // Since a given call expression can be shared by multiple
4122 : : // Call_result_expressions, avoid hoisting the call the
4123 : : // second time we see it here. In addition, don't try to
4124 : : // hoist the top-level multi-return call in the statement,
4125 : : // since doing this would result a tree with more than one copy
4126 : : // of the call.
4127 : 170247 : if (this->remember_expression(*pexpr))
4128 : : s = NULL;
4129 : 81997 : else if (es != NULL && *pexpr == es->expr())
4130 : : s = NULL;
4131 : : else
4132 : 78880 : s = Statement::make_statement(*pexpr, true);
4133 : : }
4134 : :
4135 : 574206 : if (s != NULL)
4136 : : {
4137 : 574206 : block->insert_statement_before(*pindex, s);
4138 : 574206 : ++*pindex;
4139 : : }
4140 : : }
4141 : :
4142 : 425848 : if (init != orig_init)
4143 : 81272 : vds->var()->var_value()->set_init(init);
4144 : :
4145 : : return TRAVERSE_CONTINUE;
4146 : 4712944 : }
4147 : :
4148 : : // Implement the order of evaluation rules for the initializer of a
4149 : : // global variable.
4150 : :
4151 : : int
4152 : 1148655 : Order_eval::variable(Named_object* no)
4153 : : {
4154 : 1148655 : if (no->is_result_variable())
4155 : : return TRAVERSE_CONTINUE;
4156 : 1005338 : Variable* var = no->var_value();
4157 : 1005338 : Expression* init = var->init();
4158 : 1005338 : if (!var->is_global() || init == NULL)
4159 : : return TRAVERSE_CONTINUE;
4160 : :
4161 : 20354 : Find_eval_ordering find_eval_ordering;
4162 : 20354 : Expression::traverse(&init, &find_eval_ordering);
4163 : :
4164 : 20354 : if (find_eval_ordering.size() <= 1)
4165 : : {
4166 : : // If there is only one expression with a side-effect, we can
4167 : : // leave it in place.
4168 : : return TRAVERSE_SKIP_COMPONENTS;
4169 : : }
4170 : :
4171 : 1061 : Expression* orig_init = init;
4172 : :
4173 : 10178 : for (Find_eval_ordering::const_iterator p = find_eval_ordering.begin();
4174 : 10178 : p != find_eval_ordering.end();
4175 : 9117 : ++p)
4176 : : {
4177 : 9117 : Expression** pexpr = *p;
4178 : 9117 : Location loc = (*pexpr)->location();
4179 : 9117 : Statement* s;
4180 : 9117 : if ((*pexpr)->call_expression() == NULL
4181 : 8906 : || (*pexpr)->call_expression()->result_count() < 2)
4182 : : {
4183 : 8923 : Temporary_statement* ts = Statement::make_temporary(NULL, *pexpr,
4184 : : loc);
4185 : 8923 : s = ts;
4186 : 8923 : *pexpr = Expression::make_temporary_reference(ts, loc);
4187 : : }
4188 : : else
4189 : : {
4190 : : // A call expression which returns multiple results needs to
4191 : : // be handled specially.
4192 : 194 : s = Statement::make_statement(*pexpr, true);
4193 : : }
4194 : 9117 : var->add_preinit_statement(this->gogo_, s);
4195 : : }
4196 : :
4197 : 1061 : if (init != orig_init)
4198 : 448 : var->set_init(init);
4199 : :
4200 : : return TRAVERSE_SKIP_COMPONENTS;
4201 : 20354 : }
4202 : :
4203 : : // Use temporary variables to implement the order of evaluation rules.
4204 : :
4205 : : void
4206 : 4642 : Gogo::order_evaluations()
4207 : : {
4208 : 4642 : Order_eval order_eval(this);
4209 : 4642 : this->traverse(&order_eval);
4210 : 4642 : }
4211 : :
4212 : : // Order evaluations in a block.
4213 : :
4214 : : void
4215 : 92607 : Gogo::order_block(Block* block)
4216 : : {
4217 : 92607 : Order_eval order_eval(this);
4218 : 92607 : block->traverse(&order_eval);
4219 : 92607 : }
4220 : :
4221 : : // A traversal class used to find a single shortcut operator within an
4222 : : // expression.
4223 : :
4224 : 5383167 : class Find_shortcut : public Traverse
4225 : : {
4226 : : public:
4227 : 5403521 : Find_shortcut()
4228 : 5403521 : : Traverse(traverse_blocks
4229 : : | traverse_statements
4230 : : | traverse_expressions),
4231 : 5403521 : found_(NULL)
4232 : : { }
4233 : :
4234 : : // A pointer to the expression which was found, or NULL if none was
4235 : : // found.
4236 : : Expression**
4237 : 5301637 : found() const
4238 : 5301637 : { return this->found_; }
4239 : :
4240 : : protected:
4241 : : int
4242 : 1683088 : block(Block*)
4243 : 1683088 : { return TRAVERSE_SKIP_COMPONENTS; }
4244 : :
4245 : : int
4246 : 0 : statement(Block*, size_t*, Statement*)
4247 : 0 : { return TRAVERSE_SKIP_COMPONENTS; }
4248 : :
4249 : : int
4250 : : expression(Expression**);
4251 : :
4252 : : private:
4253 : : Expression** found_;
4254 : : };
4255 : :
4256 : : // Find a shortcut expression.
4257 : :
4258 : : int
4259 : 13598969 : Find_shortcut::expression(Expression** pexpr)
4260 : : {
4261 : 13598969 : Expression* expr = *pexpr;
4262 : 13598969 : Binary_expression* be = expr->binary_expression();
4263 : 744536 : if (be == NULL)
4264 : : return TRAVERSE_CONTINUE;
4265 : 744536 : Operator op = be->op();
4266 : 744536 : if (op != OPERATOR_OROR && op != OPERATOR_ANDAND)
4267 : : return TRAVERSE_CONTINUE;
4268 : 86463 : go_assert(this->found_ == NULL);
4269 : 86463 : this->found_ = pexpr;
4270 : 86463 : return TRAVERSE_EXIT;
4271 : : }
4272 : :
4273 : : // A traversal class used to turn shortcut operators into explicit if
4274 : : // statements.
4275 : :
4276 : 367424 : class Shortcuts : public Traverse
4277 : : {
4278 : : public:
4279 : 183712 : Shortcuts(Gogo* gogo)
4280 : 183712 : : Traverse(traverse_variables
4281 : : | traverse_statements),
4282 : 183712 : gogo_(gogo)
4283 : : { }
4284 : :
4285 : : protected:
4286 : : int
4287 : : variable(Named_object*);
4288 : :
4289 : : int
4290 : : statement(Block*, size_t*, Statement*);
4291 : :
4292 : : private:
4293 : : // Convert a shortcut operator.
4294 : : Statement*
4295 : : convert_shortcut(Block* enclosing, Expression** pshortcut);
4296 : :
4297 : : // The IR.
4298 : : Gogo* gogo_;
4299 : : };
4300 : :
4301 : : // Remove shortcut operators in a single statement.
4302 : :
4303 : : int
4304 : 5296704 : Shortcuts::statement(Block* block, size_t* pindex, Statement* s)
4305 : : {
4306 : : // FIXME: This approach doesn't work for switch statements, because
4307 : : // we add the new statements before the whole switch when we need to
4308 : : // instead add them just before the switch expression. The right
4309 : : // fix is probably to lower switch statements with nonconstant cases
4310 : : // to a series of conditionals.
4311 : 5296704 : if (s->switch_statement() != NULL)
4312 : : return TRAVERSE_CONTINUE;
4313 : :
4314 : 5469532 : while (true)
4315 : : {
4316 : 5383118 : Find_shortcut find_shortcut;
4317 : :
4318 : : // If S is a variable declaration, then ordinary traversal won't
4319 : : // do anything. We want to explicitly traverse the
4320 : : // initialization expression if there is one.
4321 : 5383118 : Variable_declaration_statement* vds = s->variable_declaration_statement();
4322 : 5383118 : Expression* init = NULL;
4323 : 5383118 : if (vds == NULL)
4324 : 4999851 : s->traverse_contents(&find_shortcut);
4325 : : else
4326 : : {
4327 : 383267 : init = vds->var()->var_value()->init();
4328 : 383267 : if (init == NULL)
4329 : : return TRAVERSE_CONTINUE;
4330 : 281383 : init->traverse(&init, &find_shortcut);
4331 : : }
4332 : 5281234 : Expression** pshortcut = find_shortcut.found();
4333 : 5281234 : if (pshortcut == NULL)
4334 : : return TRAVERSE_CONTINUE;
4335 : :
4336 : 86414 : Statement* snew = this->convert_shortcut(block, pshortcut);
4337 : 86414 : block->insert_statement_before(*pindex, snew);
4338 : 86414 : ++*pindex;
4339 : :
4340 : 86414 : if (pshortcut == &init)
4341 : 612 : vds->var()->var_value()->set_init(init);
4342 : 5383118 : }
4343 : : }
4344 : :
4345 : : // Remove shortcut operators in the initializer of a global variable.
4346 : :
4347 : : int
4348 : 1148655 : Shortcuts::variable(Named_object* no)
4349 : : {
4350 : 1148655 : if (no->is_result_variable())
4351 : : return TRAVERSE_CONTINUE;
4352 : 1005338 : Variable* var = no->var_value();
4353 : 1005338 : Expression* init = var->init();
4354 : 1005338 : if (!var->is_global() || init == NULL)
4355 : : return TRAVERSE_CONTINUE;
4356 : :
4357 : 20452 : while (true)
4358 : : {
4359 : 20403 : Find_shortcut find_shortcut;
4360 : 20403 : init->traverse(&init, &find_shortcut);
4361 : 20403 : Expression** pshortcut = find_shortcut.found();
4362 : 20403 : if (pshortcut == NULL)
4363 : 20354 : return TRAVERSE_CONTINUE;
4364 : :
4365 : 49 : Statement* snew = this->convert_shortcut(NULL, pshortcut);
4366 : 49 : var->add_preinit_statement(this->gogo_, snew);
4367 : 49 : if (pshortcut == &init)
4368 : 49 : var->set_init(init);
4369 : 20403 : }
4370 : : }
4371 : :
4372 : : // Given an expression which uses a shortcut operator, return a
4373 : : // statement which implements it, and update *PSHORTCUT accordingly.
4374 : :
4375 : : Statement*
4376 : 86463 : Shortcuts::convert_shortcut(Block* enclosing, Expression** pshortcut)
4377 : : {
4378 : 86463 : Binary_expression* shortcut = (*pshortcut)->binary_expression();
4379 : 86463 : Expression* left = shortcut->left();
4380 : 86463 : Expression* right = shortcut->right();
4381 : 86463 : Location loc = shortcut->location();
4382 : :
4383 : 86463 : Block* retblock = new Block(enclosing, loc);
4384 : 86463 : retblock->set_end_location(loc);
4385 : :
4386 : 86463 : Temporary_statement* ts = Statement::make_temporary(shortcut->type(),
4387 : : left, loc);
4388 : 86463 : retblock->add_statement(ts);
4389 : :
4390 : 86463 : Block* block = new Block(retblock, loc);
4391 : 86463 : block->set_end_location(loc);
4392 : 86463 : Expression* tmpref = Expression::make_temporary_reference(ts, loc);
4393 : 86463 : Statement* assign = Statement::make_assignment(tmpref, right, loc);
4394 : 86463 : block->add_statement(assign);
4395 : :
4396 : 86463 : Expression* cond = Expression::make_temporary_reference(ts, loc);
4397 : 86463 : if (shortcut->binary_expression()->op() == OPERATOR_OROR)
4398 : 30643 : cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
4399 : :
4400 : 86463 : Statement* if_statement = Statement::make_if_statement(cond, block, NULL,
4401 : : loc);
4402 : 86463 : if_statement->determine_types(this->gogo_);
4403 : 86463 : retblock->add_statement(if_statement);
4404 : :
4405 : 86463 : *pshortcut = Expression::make_temporary_reference(ts, loc);
4406 : :
4407 : 86463 : delete shortcut;
4408 : :
4409 : : // Now convert any shortcut operators in LEFT and RIGHT.
4410 : : // LEFT and RIGHT were skipped in the top level
4411 : : // Gogo::order_evaluations. We need to order their
4412 : : // components first.
4413 : 86463 : Order_eval order_eval(this->gogo_);
4414 : 86463 : retblock->traverse(&order_eval);
4415 : 86463 : Shortcuts shortcuts(this->gogo_);
4416 : 86463 : retblock->traverse(&shortcuts);
4417 : :
4418 : 86463 : return Statement::make_block_statement(retblock, loc);
4419 : 86463 : }
4420 : :
4421 : : // Turn shortcut operators into explicit if statements. Doing this
4422 : : // considerably simplifies the order of evaluation rules.
4423 : :
4424 : : void
4425 : 4642 : Gogo::remove_shortcuts()
4426 : : {
4427 : 4642 : Shortcuts shortcuts(this);
4428 : 4642 : this->traverse(&shortcuts);
4429 : 4642 : }
4430 : :
4431 : : // Turn shortcut operators into explicit if statements in a block.
4432 : :
4433 : : void
4434 : 92607 : Gogo::remove_shortcuts_in_block(Block* block)
4435 : : {
4436 : 92607 : Shortcuts shortcuts(this);
4437 : 92607 : block->traverse(&shortcuts);
4438 : 92607 : }
4439 : :
4440 : : // Traversal to flatten parse tree after order of evaluation rules are applied.
4441 : :
4442 : 927220 : class Flatten : public Traverse
4443 : : {
4444 : : public:
4445 : 463610 : Flatten(Gogo* gogo, Named_object* function)
4446 : : : Traverse(traverse_variables
4447 : : | traverse_functions
4448 : : | traverse_statements
4449 : : | traverse_expressions),
4450 : 463610 : gogo_(gogo), function_(function), inserter_()
4451 : : { }
4452 : :
4453 : : void
4454 : 445327 : set_inserter(const Statement_inserter* inserter)
4455 : 445327 : { this->inserter_ = *inserter; }
4456 : :
4457 : : int
4458 : : variable(Named_object*);
4459 : :
4460 : : int
4461 : : function(Named_object*);
4462 : :
4463 : : int
4464 : : statement(Block*, size_t* pindex, Statement*);
4465 : :
4466 : : int
4467 : : expression(Expression**);
4468 : :
4469 : : private:
4470 : : // General IR.
4471 : : Gogo* gogo_;
4472 : : // The function we are traversing.
4473 : : Named_object* function_;
4474 : : // Current statement inserter for use by expressions.
4475 : : Statement_inserter inserter_;
4476 : : };
4477 : :
4478 : : // Flatten variables.
4479 : :
4480 : : int
4481 : 1469150 : Flatten::variable(Named_object* no)
4482 : : {
4483 : 1469150 : if (!no->is_variable())
4484 : : return TRAVERSE_CONTINUE;
4485 : :
4486 : 1224456 : if (no->is_variable() && no->var_value()->is_global())
4487 : : {
4488 : : // Global variables can have loops in their initialization
4489 : : // expressions. This is handled in flatten_init_expression.
4490 : 348686 : no->var_value()->flatten_init_expression(this->gogo_, this->function_,
4491 : : &this->inserter_);
4492 : 348686 : return TRAVERSE_CONTINUE;
4493 : : }
4494 : :
4495 : 875770 : if (!no->var_value()->is_parameter()
4496 : 401994 : && !no->var_value()->is_receiver()
4497 : 401994 : && !no->var_value()->is_closure()
4498 : 401994 : && no->var_value()->is_non_escaping_address_taken()
4499 : 31900 : && !no->var_value()->is_in_heap()
4500 : 902473 : && no->var_value()->toplevel_decl() == NULL)
4501 : : {
4502 : : // Local variable that has address taken but not escape.
4503 : : // It needs to be live beyond its lexical scope. So we
4504 : : // create a top-level declaration for it.
4505 : : // No need to do it if it is already in the top level.
4506 : 21694 : Block* top_block = function_->func_value()->block();
4507 : 21694 : if (top_block->bindings()->lookup_local(no->name()) != no)
4508 : : {
4509 : 4935 : Variable* var = no->var_value();
4510 : 4935 : Temporary_statement* ts =
4511 : 4935 : Statement::make_temporary(var->type(), NULL, var->location());
4512 : 4935 : ts->set_is_address_taken();
4513 : 4935 : top_block->add_statement_at_front(ts);
4514 : 4935 : var->set_toplevel_decl(ts);
4515 : : }
4516 : : }
4517 : :
4518 : 875770 : go_assert(!no->var_value()->has_pre_init());
4519 : :
4520 : : return TRAVERSE_SKIP_COMPONENTS;
4521 : : }
4522 : :
4523 : : // Flatten the body of a function. Record the function while flattening it,
4524 : : // so that we can pass it down when flattening an expression.
4525 : :
4526 : : int
4527 : 295607 : Flatten::function(Named_object* no)
4528 : : {
4529 : 295607 : go_assert(this->function_ == NULL);
4530 : 295607 : this->function_ = no;
4531 : 295607 : int t = no->func_value()->traverse(this);
4532 : 295607 : this->function_ = NULL;
4533 : :
4534 : 295607 : if (t == TRAVERSE_EXIT)
4535 : 0 : return t;
4536 : : return TRAVERSE_SKIP_COMPONENTS;
4537 : : }
4538 : :
4539 : : // Flatten statement parse trees.
4540 : :
4541 : : int
4542 : 6381384 : Flatten::statement(Block* block, size_t* pindex, Statement* sorig)
4543 : : {
4544 : : // Because we explicitly traverse the statement's contents
4545 : : // ourselves, we want to skip block statements here. There is
4546 : : // nothing to flatten in a block statement.
4547 : 6381384 : if (sorig->is_block_statement())
4548 : : return TRAVERSE_CONTINUE;
4549 : :
4550 : 4957431 : Statement_inserter hold_inserter(this->inserter_);
4551 : 4957431 : this->inserter_ = Statement_inserter(block, pindex);
4552 : :
4553 : : // Flatten the expressions first.
4554 : 4957431 : int t = sorig->traverse_contents(this);
4555 : 4957431 : if (t == TRAVERSE_EXIT)
4556 : : {
4557 : 0 : this->inserter_ = hold_inserter;
4558 : 0 : return t;
4559 : : }
4560 : :
4561 : : // Keep flattening until nothing changes.
4562 : : Statement* s = sorig;
4563 : 4957792 : while (true)
4564 : : {
4565 : 4957792 : Statement* snew = s->flatten(this->gogo_, this->function_, block,
4566 : : &this->inserter_);
4567 : 4957792 : if (snew == s)
4568 : : break;
4569 : 361 : s = snew;
4570 : 361 : t = s->traverse_contents(this);
4571 : 361 : if (t == TRAVERSE_EXIT)
4572 : : {
4573 : 0 : this->inserter_ = hold_inserter;
4574 : 0 : return t;
4575 : : }
4576 : : }
4577 : :
4578 : 4957431 : if (s != sorig)
4579 : 361 : block->replace_statement(*pindex, s);
4580 : :
4581 : 4957431 : this->inserter_ = hold_inserter;
4582 : 4957431 : return TRAVERSE_SKIP_COMPONENTS;
4583 : : }
4584 : :
4585 : : // Flatten expression parse trees.
4586 : :
4587 : : int
4588 : 18285741 : Flatten::expression(Expression** pexpr)
4589 : : {
4590 : : // Keep flattening until nothing changes.
4591 : 18346241 : while (true)
4592 : : {
4593 : 18315991 : Expression* e = *pexpr;
4594 : 18315991 : if (e->traverse_subexpressions(this) == TRAVERSE_EXIT)
4595 : : return TRAVERSE_EXIT;
4596 : :
4597 : 18315991 : Expression* enew = e->flatten(this->gogo_, this->function_,
4598 : : &this->inserter_);
4599 : 18315991 : if (enew == e)
4600 : : break;
4601 : 30250 : *pexpr = enew;
4602 : 30250 : }
4603 : : return TRAVERSE_SKIP_COMPONENTS;
4604 : : }
4605 : :
4606 : : // Flatten a block.
4607 : :
4608 : : void
4609 : 13641 : Gogo::flatten_block(Named_object* function, Block* block)
4610 : : {
4611 : 13641 : Flatten flatten(this, function);
4612 : 13641 : block->traverse(&flatten);
4613 : 13641 : }
4614 : :
4615 : : // Flatten an expression. INSERTER may be NULL, in which case the
4616 : : // expression had better not need to create any temporaries.
4617 : :
4618 : : void
4619 : 445327 : Gogo::flatten_expression(Named_object* function, Statement_inserter* inserter,
4620 : : Expression** pexpr)
4621 : : {
4622 : 445327 : Flatten flatten(this, function);
4623 : 445327 : if (inserter != NULL)
4624 : 445327 : flatten.set_inserter(inserter);
4625 : 445327 : flatten.expression(pexpr);
4626 : 445327 : }
4627 : :
4628 : : void
4629 : 4642 : Gogo::flatten()
4630 : : {
4631 : 4642 : Flatten flatten(this, NULL);
4632 : 4642 : this->traverse(&flatten);
4633 : 4642 : }
4634 : :
4635 : : // Traversal to convert calls to the predeclared recover function to
4636 : : // pass in an argument indicating whether it can recover from a panic
4637 : : // or not.
4638 : :
4639 : 1662 : class Convert_recover : public Traverse
4640 : : {
4641 : : public:
4642 : 831 : Convert_recover(Named_object* arg)
4643 : 831 : : Traverse(traverse_expressions),
4644 : 831 : arg_(arg)
4645 : : { }
4646 : :
4647 : : protected:
4648 : : int
4649 : : expression(Expression**);
4650 : :
4651 : : private:
4652 : : // The argument to pass to the function.
4653 : : Named_object* arg_;
4654 : : };
4655 : :
4656 : : // Convert calls to recover.
4657 : :
4658 : : int
4659 : 22685 : Convert_recover::expression(Expression** pp)
4660 : : {
4661 : 22685 : Call_expression* ce = (*pp)->call_expression();
4662 : 2849 : if (ce != NULL && ce->is_recover_call())
4663 : 847 : ce->set_recover_arg(Expression::make_var_reference(this->arg_,
4664 : : ce->location()));
4665 : 22685 : return TRAVERSE_CONTINUE;
4666 : : }
4667 : :
4668 : : // Traversal for build_recover_thunks.
4669 : :
4670 : 9284 : class Build_recover_thunks : public Traverse
4671 : : {
4672 : : public:
4673 : 4642 : Build_recover_thunks(Gogo* gogo)
4674 : 4642 : : Traverse(traverse_functions),
4675 : 4642 : gogo_(gogo)
4676 : : { }
4677 : :
4678 : : int
4679 : : function(Named_object*);
4680 : :
4681 : : private:
4682 : : Expression*
4683 : : can_recover_arg(Location);
4684 : :
4685 : : // General IR.
4686 : : Gogo* gogo_;
4687 : : };
4688 : :
4689 : : // If this function calls recover, turn it into a thunk.
4690 : :
4691 : : int
4692 : 189432 : Build_recover_thunks::function(Named_object* orig_no)
4693 : : {
4694 : 189432 : Function* orig_func = orig_no->func_value();
4695 : 189432 : if (!orig_func->calls_recover()
4696 : 1662 : || orig_func->is_recover_thunk()
4697 : 191094 : || orig_func->has_recover_thunk())
4698 : : return TRAVERSE_CONTINUE;
4699 : :
4700 : 831 : Gogo* gogo = this->gogo_;
4701 : 831 : Location location = orig_func->location();
4702 : :
4703 : 831 : static int count;
4704 : 831 : char buf[50];
4705 : :
4706 : 831 : Function_type* orig_fntype = orig_func->type();
4707 : 831 : Typed_identifier_list* new_params = new Typed_identifier_list();
4708 : 831 : std::string receiver_name;
4709 : 831 : if (orig_fntype->is_method())
4710 : : {
4711 : 42 : const Typed_identifier* receiver = orig_fntype->receiver();
4712 : 42 : snprintf(buf, sizeof buf, "rt.%u", count);
4713 : 42 : ++count;
4714 : 42 : receiver_name = buf;
4715 : 42 : new_params->push_back(Typed_identifier(receiver_name, receiver->type(),
4716 : : receiver->location()));
4717 : : }
4718 : 831 : const Typed_identifier_list* orig_params = orig_fntype->parameters();
4719 : 831 : if (orig_params != NULL && !orig_params->empty())
4720 : : {
4721 : 175 : for (Typed_identifier_list::const_iterator p = orig_params->begin();
4722 : 272 : p != orig_params->end();
4723 : 175 : ++p)
4724 : : {
4725 : 175 : snprintf(buf, sizeof buf, "pt.%u", count);
4726 : 175 : ++count;
4727 : 175 : new_params->push_back(Typed_identifier(buf, p->type(),
4728 : : p->location()));
4729 : : }
4730 : : }
4731 : 831 : snprintf(buf, sizeof buf, "pr.%u", count);
4732 : 831 : ++count;
4733 : 831 : std::string can_recover_name = buf;
4734 : 1662 : new_params->push_back(Typed_identifier(can_recover_name,
4735 : 831 : Type::lookup_bool_type(),
4736 : : orig_fntype->location()));
4737 : :
4738 : 831 : const Typed_identifier_list* orig_results = orig_fntype->results();
4739 : 831 : Typed_identifier_list* new_results;
4740 : 831 : if (orig_results == NULL || orig_results->empty())
4741 : : new_results = NULL;
4742 : : else
4743 : : {
4744 : 3 : new_results = new Typed_identifier_list();
4745 : 6 : for (Typed_identifier_list::const_iterator p = orig_results->begin();
4746 : 6 : p != orig_results->end();
4747 : 3 : ++p)
4748 : 3 : new_results->push_back(Typed_identifier("", p->type(), p->location()));
4749 : : }
4750 : :
4751 : 831 : Function_type *new_fntype = Type::make_function_type(NULL, new_params,
4752 : : new_results,
4753 : : orig_fntype->location());
4754 : 831 : if (orig_fntype->is_varargs())
4755 : 1 : new_fntype->set_is_varargs();
4756 : :
4757 : 831 : Type* rtype = NULL;
4758 : 831 : if (orig_fntype->is_method())
4759 : 42 : rtype = orig_fntype->receiver()->type();
4760 : 831 : std::string name(gogo->recover_thunk_name(orig_no->name(), rtype));
4761 : 831 : Named_object *new_no = gogo->start_function(name, new_fntype, false,
4762 : : location);
4763 : 831 : Function *new_func = new_no->func_value();
4764 : 831 : if (orig_func->enclosing() != NULL)
4765 : 737 : new_func->set_enclosing(orig_func->enclosing());
4766 : :
4767 : : // We build the code for the original function attached to the new
4768 : : // function, and then swap the original and new function bodies.
4769 : : // This means that existing references to the original function will
4770 : : // then refer to the new function. That makes this code a little
4771 : : // confusing, in that the reference to NEW_NO really refers to the
4772 : : // other function, not the one we are building.
4773 : :
4774 : 831 : Expression* closure = NULL;
4775 : 831 : if (orig_func->needs_closure())
4776 : : {
4777 : : // For the new function we are creating, declare a new parameter
4778 : : // variable NEW_CLOSURE_NO and set it to be the closure variable
4779 : : // of the function. This will be set to the closure value
4780 : : // passed in by the caller. Then pass a reference to this
4781 : : // variable as the closure value when calling the original
4782 : : // function. In other words, simply pass the closure value
4783 : : // through the thunk we are creating.
4784 : 526 : Named_object* orig_closure_no = orig_func->closure_var();
4785 : 526 : Variable* orig_closure_var = orig_closure_no->var_value();
4786 : 526 : Variable* new_var = new Variable(orig_closure_var->type(), NULL, false,
4787 : 526 : false, false, location);
4788 : 526 : new_var->set_is_closure();
4789 : 526 : snprintf(buf, sizeof buf, "closure.%u", count);
4790 : 526 : ++count;
4791 : 526 : Named_object* new_closure_no = Named_object::make_variable(buf, NULL,
4792 : : new_var);
4793 : 526 : new_func->set_closure_var(new_closure_no);
4794 : 526 : closure = Expression::make_var_reference(new_closure_no, location);
4795 : : }
4796 : :
4797 : 831 : Expression* fn = Expression::make_func_reference(new_no, closure, location);
4798 : :
4799 : 831 : Expression_list* args = new Expression_list();
4800 : 831 : if (new_params != NULL)
4801 : : {
4802 : : // Note that we skip the last parameter, which is the boolean
4803 : : // indicating whether recover can succed.
4804 : 831 : for (Typed_identifier_list::const_iterator p = new_params->begin();
4805 : 1048 : p + 1 != new_params->end();
4806 : 217 : ++p)
4807 : : {
4808 : 217 : Named_object* p_no = gogo->lookup(p->name(), NULL);
4809 : 217 : go_assert(p_no != NULL
4810 : : && p_no->is_variable()
4811 : : && p_no->var_value()->is_parameter());
4812 : 217 : args->push_back(Expression::make_var_reference(p_no, location));
4813 : : }
4814 : : }
4815 : 831 : args->push_back(this->can_recover_arg(location));
4816 : :
4817 : 831 : gogo->start_block(location);
4818 : :
4819 : 831 : Call_expression* call = Expression::make_call(fn, args, false, location);
4820 : :
4821 : : // Any varargs call has already been lowered.
4822 : 831 : call->set_varargs_are_lowered();
4823 : :
4824 : 831 : Statement* s = Statement::make_return_from_call(new_no, call, location);
4825 : 831 : s->determine_types(this->gogo_);
4826 : 831 : gogo->add_statement(s);
4827 : :
4828 : 831 : Block* b = gogo->finish_block(location);
4829 : :
4830 : 831 : gogo->add_block(b, location);
4831 : :
4832 : : // Lower the call in case it returns multiple results.
4833 : 831 : gogo->lower_block(new_no, b);
4834 : :
4835 : 831 : gogo->finish_function(location);
4836 : :
4837 : : // Swap the function bodies and types.
4838 : 831 : new_func->swap_for_recover(orig_func);
4839 : 831 : orig_func->set_is_recover_thunk();
4840 : 831 : new_func->set_calls_recover();
4841 : 831 : new_func->set_has_recover_thunk();
4842 : :
4843 : 831 : Bindings* orig_bindings = orig_func->block()->bindings();
4844 : 831 : Bindings* new_bindings = new_func->block()->bindings();
4845 : 831 : if (orig_fntype->is_method())
4846 : : {
4847 : : // We changed the receiver to be a regular parameter. We have
4848 : : // to update the binding accordingly in both functions.
4849 : 42 : Named_object* orig_rec_no = orig_bindings->lookup_local(receiver_name);
4850 : 42 : go_assert(orig_rec_no != NULL
4851 : : && orig_rec_no->is_variable()
4852 : : && !orig_rec_no->var_value()->is_receiver());
4853 : 42 : orig_rec_no->var_value()->set_is_receiver();
4854 : :
4855 : 42 : std::string new_receiver_name(orig_fntype->receiver()->name());
4856 : 42 : if (new_receiver_name.empty())
4857 : : {
4858 : : // Find the receiver. It was named "r.NNN" in
4859 : : // Gogo::start_function.
4860 : 7 : for (Bindings::const_definitions_iterator p =
4861 : 7 : new_bindings->begin_definitions();
4862 : 7 : p != new_bindings->end_definitions();
4863 : 0 : ++p)
4864 : : {
4865 : 7 : const std::string& pname((*p)->name());
4866 : 7 : if (pname[0] == 'r' && pname[1] == '.')
4867 : : {
4868 : 7 : new_receiver_name = pname;
4869 : 7 : break;
4870 : : }
4871 : : }
4872 : 7 : go_assert(!new_receiver_name.empty());
4873 : : }
4874 : 42 : Named_object* new_rec_no = new_bindings->lookup_local(new_receiver_name);
4875 : 42 : if (new_rec_no == NULL)
4876 : 0 : go_assert(saw_errors());
4877 : : else
4878 : : {
4879 : 42 : go_assert(new_rec_no->is_variable()
4880 : : && new_rec_no->var_value()->is_receiver());
4881 : 42 : new_rec_no->var_value()->set_is_not_receiver();
4882 : : }
4883 : 42 : }
4884 : :
4885 : : // Because we flipped blocks but not types, the can_recover
4886 : : // parameter appears in the (now) old bindings as a parameter.
4887 : : // Change it to a local variable, whereupon it will be discarded.
4888 : 831 : Named_object* can_recover_no = orig_bindings->lookup_local(can_recover_name);
4889 : 831 : go_assert(can_recover_no != NULL
4890 : : && can_recover_no->is_variable()
4891 : : && can_recover_no->var_value()->is_parameter());
4892 : 831 : orig_bindings->remove_binding(can_recover_no);
4893 : :
4894 : : // Add the can_recover argument to the (now) new bindings, and
4895 : : // attach it to any recover statements.
4896 : 831 : Variable* can_recover_var = new Variable(Type::lookup_bool_type(), NULL,
4897 : 1662 : false, true, false, location);
4898 : 831 : can_recover_no = new_bindings->add_variable(can_recover_name, NULL,
4899 : : can_recover_var);
4900 : 831 : Convert_recover convert_recover(can_recover_no);
4901 : 831 : new_func->traverse(&convert_recover);
4902 : :
4903 : : // Update the function pointers in any named results.
4904 : 831 : new_func->update_result_variables();
4905 : 831 : orig_func->update_result_variables();
4906 : :
4907 : 831 : return TRAVERSE_CONTINUE;
4908 : 831 : }
4909 : :
4910 : : // Return the expression to pass for the .can_recover parameter to the
4911 : : // new function. This indicates whether a call to recover may return
4912 : : // non-nil. The expression is runtime.canrecover(__builtin_return_address()).
4913 : :
4914 : : Expression*
4915 : 831 : Build_recover_thunks::can_recover_arg(Location location)
4916 : : {
4917 : 831 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
4918 : 831 : static Named_object* can_recover;
4919 : 831 : if (can_recover == NULL)
4920 : : {
4921 : 319 : const Location bloc = Linemap::predeclared_location();
4922 : 319 : Typed_identifier_list* param_types = new Typed_identifier_list();
4923 : 319 : param_types->push_back(Typed_identifier("a", uintptr_type, bloc));
4924 : 319 : Type* boolean_type = Type::lookup_bool_type();
4925 : 319 : Typed_identifier_list* results = new Typed_identifier_list();
4926 : 319 : results->push_back(Typed_identifier("", boolean_type, bloc));
4927 : 319 : Function_type* fntype = Type::make_function_type(NULL, param_types,
4928 : : results, bloc);
4929 : 638 : can_recover =
4930 : 638 : Named_object::make_function_declaration("runtime_canrecover",
4931 : : NULL, fntype, bloc);
4932 : 319 : can_recover->func_declaration_value()->set_asm_name("runtime.canrecover");
4933 : : }
4934 : :
4935 : 831 : Expression* zexpr = Expression::make_integer_ul(0, NULL, location);
4936 : 831 : Expression* call = Runtime::make_call(this->gogo_,
4937 : : Runtime::BUILTIN_RETURN_ADDRESS,
4938 : : location, 1, zexpr);
4939 : 831 : call = Expression::make_unsafe_cast(uintptr_type, call, location);
4940 : :
4941 : 831 : Expression_list* args = new Expression_list();
4942 : 831 : args->push_back(call);
4943 : :
4944 : 831 : Expression* fn = Expression::make_func_reference(can_recover, NULL, location);
4945 : 831 : return Expression::make_call(fn, args, false, location);
4946 : : }
4947 : :
4948 : : // Build thunks for functions which call recover. We build a new
4949 : : // function with an extra parameter, which is whether a call to
4950 : : // recover can succeed. We then move the body of this function to
4951 : : // that one. We then turn this function into a thunk which calls the
4952 : : // new one, passing the value of runtime.canrecover(__builtin_return_address()).
4953 : : // The function will be marked as not splitting the stack. This will
4954 : : // cooperate with the implementation of defer to make recover do the
4955 : : // right thing.
4956 : :
4957 : : void
4958 : 4642 : Gogo::build_recover_thunks()
4959 : : {
4960 : 4642 : Build_recover_thunks build_recover_thunks(this);
4961 : 4642 : this->traverse(&build_recover_thunks);
4962 : 4642 : }
4963 : :
4964 : : // Look for named types to see whether we need to create an interface
4965 : : // method table.
4966 : :
4967 : 4178 : class Build_method_tables : public Traverse
4968 : : {
4969 : : public:
4970 : 2089 : Build_method_tables(Gogo* gogo,
4971 : : const std::vector<Interface_type*>& interfaces)
4972 : 2089 : : Traverse(traverse_types),
4973 : 2089 : gogo_(gogo), interfaces_(interfaces)
4974 : : { }
4975 : :
4976 : : int
4977 : : type(Type*);
4978 : :
4979 : : private:
4980 : : // The IR.
4981 : : Gogo* gogo_;
4982 : : // A list of locally defined interfaces which have hidden methods.
4983 : : const std::vector<Interface_type*>& interfaces_;
4984 : : };
4985 : :
4986 : : // Build all required interface method tables for types. We need to
4987 : : // ensure that we have an interface method table for every interface
4988 : : // which has a hidden method, for every named type which implements
4989 : : // that interface. Normally we can just build interface method tables
4990 : : // as we need them. However, in some cases we can require an
4991 : : // interface method table for an interface defined in a different
4992 : : // package for a type defined in that package. If that interface and
4993 : : // type both use a hidden method, that is OK. However, we will not be
4994 : : // able to build that interface method table when we need it, because
4995 : : // the type's hidden method will be static. So we have to build it
4996 : : // here, and just refer it from other packages as needed.
4997 : :
4998 : : void
4999 : 4642 : Gogo::build_interface_method_tables()
5000 : : {
5001 : 4642 : if (saw_errors())
5002 : 411 : return;
5003 : :
5004 : 4231 : std::vector<Interface_type*> hidden_interfaces;
5005 : 4231 : hidden_interfaces.reserve(this->interface_types_.size());
5006 : 70824 : for (std::vector<Interface_type*>::const_iterator pi =
5007 : 4231 : this->interface_types_.begin();
5008 : 75055 : pi != this->interface_types_.end();
5009 : 70824 : ++pi)
5010 : : {
5011 : 70824 : const Typed_identifier_list* methods = (*pi)->methods();
5012 : 70824 : if (methods == NULL)
5013 : 1486 : continue;
5014 : 293114 : for (Typed_identifier_list::const_iterator pm = methods->begin();
5015 : 293114 : pm != methods->end();
5016 : 223776 : ++pm)
5017 : : {
5018 : 232171 : if (Gogo::is_hidden_name(pm->name()))
5019 : : {
5020 : 8395 : hidden_interfaces.push_back(*pi);
5021 : 8395 : break;
5022 : : }
5023 : : }
5024 : : }
5025 : :
5026 : 4231 : if (!hidden_interfaces.empty())
5027 : : {
5028 : : // Now traverse the tree looking for all named types.
5029 : 2089 : Build_method_tables bmt(this, hidden_interfaces);
5030 : 2089 : this->traverse(&bmt);
5031 : 2089 : }
5032 : :
5033 : : // We no longer need the list of interfaces.
5034 : :
5035 : 4231 : this->interface_types_.clear();
5036 : 4231 : }
5037 : :
5038 : : // This is called for each type. For a named type, for each of the
5039 : : // interfaces with hidden methods that it implements, create the
5040 : : // method table.
5041 : :
5042 : : int
5043 : 11398732 : Build_method_tables::type(Type* type)
5044 : : {
5045 : 11398732 : Named_type* nt = type->named_type();
5046 : 11398732 : Struct_type* st = type->struct_type();
5047 : 11398732 : if (nt != NULL || st != NULL)
5048 : : {
5049 : 3642618 : Translate_context context(this->gogo_, NULL, NULL, NULL);
5050 : 19162808 : for (std::vector<Interface_type*>::const_iterator p =
5051 : 3642618 : this->interfaces_.begin();
5052 : 22805426 : p != this->interfaces_.end();
5053 : 19162808 : ++p)
5054 : : {
5055 : : // We ask whether a pointer to the named type implements the
5056 : : // interface, because a pointer can implement more methods
5057 : : // than a value.
5058 : 19162808 : if (nt != NULL)
5059 : : {
5060 : 15767181 : if ((*p)->implements_interface(Type::make_pointer_type(nt),
5061 : : NULL))
5062 : : {
5063 : 69546 : nt->interface_method_table(*p, false)->get_backend(&context);
5064 : 69546 : nt->interface_method_table(*p, true)->get_backend(&context);
5065 : : }
5066 : : }
5067 : : else
5068 : : {
5069 : 3395627 : if ((*p)->implements_interface(Type::make_pointer_type(st),
5070 : : NULL))
5071 : : {
5072 : 249 : st->interface_method_table(*p, false)->get_backend(&context);
5073 : 249 : st->interface_method_table(*p, true)->get_backend(&context);
5074 : : }
5075 : : }
5076 : : }
5077 : : }
5078 : 11398732 : return TRAVERSE_CONTINUE;
5079 : : }
5080 : :
5081 : : // Return an expression which allocates memory to hold values of type TYPE.
5082 : :
5083 : : Expression*
5084 : 204385 : Gogo::allocate_memory(Type* type, Location location)
5085 : : {
5086 : 204385 : Expression* td = Expression::make_type_descriptor(type, location);
5087 : 204385 : return Runtime::make_call(this, Runtime::NEW, location, 1, td);
5088 : : }
5089 : :
5090 : : // Traversal class used to check for return statements.
5091 : :
5092 : 9284 : class Check_return_statements_traverse : public Traverse
5093 : : {
5094 : : public:
5095 : 4642 : Check_return_statements_traverse()
5096 : 4642 : : Traverse(traverse_functions)
5097 : : { }
5098 : :
5099 : : int
5100 : : function(Named_object*);
5101 : : };
5102 : :
5103 : : // Check that a function has a return statement if it needs one.
5104 : :
5105 : : int
5106 : 174975 : Check_return_statements_traverse::function(Named_object* no)
5107 : : {
5108 : 174975 : Function* func = no->func_value();
5109 : 174975 : const Function_type* fntype = func->type();
5110 : 174975 : const Typed_identifier_list* results = fntype->results();
5111 : :
5112 : : // We only need a return statement if there is a return value.
5113 : 174975 : if (results == NULL || results->empty())
5114 : : return TRAVERSE_CONTINUE;
5115 : :
5116 : 104632 : if (func->block()->may_fall_through())
5117 : 226 : go_error_at(func->block()->end_location(),
5118 : : "missing return at end of function");
5119 : :
5120 : : return TRAVERSE_CONTINUE;
5121 : : }
5122 : :
5123 : : // Check return statements.
5124 : :
5125 : : void
5126 : 4642 : Gogo::check_return_statements()
5127 : : {
5128 : 4642 : Check_return_statements_traverse traverse;
5129 : 4642 : this->traverse(&traverse);
5130 : 4642 : }
5131 : :
5132 : : // Traversal class to decide whether a function body is less than the
5133 : : // inlining budget. This adjusts *available as it goes, and stops the
5134 : : // traversal if it goes negative.
5135 : :
5136 : 1636607 : class Inline_within_budget : public Traverse
5137 : : {
5138 : : public:
5139 : 1822946 : Inline_within_budget(int* available)
5140 : 1822946 : : Traverse(traverse_statements
5141 : : | traverse_expressions),
5142 : 1822946 : available_(available)
5143 : : { }
5144 : :
5145 : : int
5146 : : statement(Block*, size_t*, Statement*);
5147 : :
5148 : : int
5149 : : expression(Expression**);
5150 : :
5151 : : private:
5152 : : // Pointer to remaining budget.
5153 : : int* available_;
5154 : : };
5155 : :
5156 : : // Adjust the budget for the inlining cost of a statement.
5157 : :
5158 : : int
5159 : 4945621 : Inline_within_budget::statement(Block*, size_t*, Statement* s)
5160 : : {
5161 : 4945621 : if (*this->available_ < 0)
5162 : : return TRAVERSE_EXIT;
5163 : 4926434 : *this->available_ -= s->inlining_cost();
5164 : 4926434 : return TRAVERSE_CONTINUE;
5165 : : }
5166 : :
5167 : : // Adjust the budget for the inlining cost of an expression.
5168 : :
5169 : : int
5170 : 10891618 : Inline_within_budget::expression(Expression** pexpr)
5171 : : {
5172 : 10891618 : if (*this->available_ < 0)
5173 : : return TRAVERSE_EXIT;
5174 : 9245832 : *this->available_ -= (*pexpr)->inlining_cost();
5175 : 9245832 : return TRAVERSE_CONTINUE;
5176 : : }
5177 : :
5178 : : // Traversal class to find functions whose body should be exported for
5179 : : // inlining by other packages.
5180 : :
5181 : 8496 : class Mark_inline_candidates : public Traverse
5182 : : {
5183 : : public:
5184 : 4248 : Mark_inline_candidates(Unordered_set(Named_object*)* marked)
5185 : 4248 : : Traverse(traverse_functions
5186 : : | traverse_types),
5187 : 4248 : marked_functions_(marked)
5188 : : { }
5189 : :
5190 : : int
5191 : : function(Named_object*);
5192 : :
5193 : : int
5194 : : type(Type*);
5195 : :
5196 : : private:
5197 : : // We traverse the function body trying to determine how expensive
5198 : : // it is for inlining. We start with a budget, and decrease that
5199 : : // budget for each statement and expression. If the budget goes
5200 : : // negative, we do not export the function body. The value of this
5201 : : // budget is a heuristic. In the usual GCC spirit, we could
5202 : : // consider setting this via a command line option.
5203 : : const int budget_heuristic = 80;
5204 : :
5205 : : // Set of named objects that are marked as inline candidates.
5206 : : Unordered_set(Named_object*)* marked_functions_;
5207 : : };
5208 : :
5209 : : // Mark a function if it is an inline candidate.
5210 : :
5211 : : int
5212 : 186699 : Mark_inline_candidates::function(Named_object* no)
5213 : : {
5214 : 186699 : Function* func = no->func_value();
5215 : 186699 : if ((func->pragmas() & GOPRAGMA_NOINLINE) != 0)
5216 : : return TRAVERSE_CONTINUE;
5217 : 186339 : int budget = budget_heuristic;
5218 : 186339 : Inline_within_budget iwb(&budget);
5219 : 186339 : func->block()->traverse(&iwb);
5220 : 186339 : if (budget >= 0)
5221 : : {
5222 : 41217 : func->set_export_for_inlining();
5223 : 41217 : this->marked_functions_->insert(no);
5224 : : }
5225 : 186339 : return TRAVERSE_CONTINUE;
5226 : 186339 : }
5227 : :
5228 : : // Mark methods if they are inline candidates.
5229 : :
5230 : : int
5231 : 11008007 : Mark_inline_candidates::type(Type* t)
5232 : : {
5233 : 11008007 : Named_type* nt = t->named_type();
5234 : 11008007 : if (nt == NULL || nt->is_alias())
5235 : : return TRAVERSE_CONTINUE;
5236 : 3156237 : const Bindings* methods = nt->local_methods();
5237 : 3156237 : if (methods == NULL)
5238 : : return TRAVERSE_CONTINUE;
5239 : 2114922 : for (Bindings::const_definitions_iterator p = methods->begin_definitions();
5240 : 2114922 : p != methods->end_definitions();
5241 : 1637872 : ++p)
5242 : : {
5243 : 1637872 : Named_object* no = *p;
5244 : 1637872 : go_assert(no->is_function());
5245 : 1637872 : Function *func = no->func_value();
5246 : 1637872 : if ((func->pragmas() & GOPRAGMA_NOINLINE) != 0)
5247 : 1265 : continue;
5248 : 1636607 : int budget = budget_heuristic;
5249 : 1636607 : Inline_within_budget iwb(&budget);
5250 : 1636607 : func->block()->traverse(&iwb);
5251 : 1636607 : if (budget >= 0)
5252 : : {
5253 : 116133 : func->set_export_for_inlining();
5254 : 116133 : this->marked_functions_->insert(no);
5255 : : }
5256 : 1636607 : }
5257 : : return TRAVERSE_CONTINUE;
5258 : : }
5259 : :
5260 : : // Export identifiers as requested.
5261 : :
5262 : : void
5263 : 4642 : Gogo::do_exports()
5264 : : {
5265 : 4642 : if (saw_errors())
5266 : 394 : return;
5267 : :
5268 : : // Mark any functions whose body should be exported for inlining by
5269 : : // other packages.
5270 : 4248 : Unordered_set(Named_object*) marked_functions;
5271 : 4248 : Mark_inline_candidates mic(&marked_functions);
5272 : 4248 : this->traverse(&mic);
5273 : :
5274 : : // For now we always stream to a section. Later we may want to
5275 : : // support streaming to a separate file.
5276 : 4248 : Stream_to_section stream(this->backend());
5277 : :
5278 : : // Write out either the prefix or pkgpath depending on how we were
5279 : : // invoked.
5280 : 4248 : std::string prefix;
5281 : 4248 : std::string pkgpath;
5282 : 4248 : if (this->pkgpath_from_option_)
5283 : 2004 : pkgpath = this->pkgpath_;
5284 : 2244 : else if (this->prefix_from_option_)
5285 : 0 : prefix = this->prefix_;
5286 : 2244 : else if (this->is_main_package())
5287 : 1635 : pkgpath = "main";
5288 : : else
5289 : 609 : prefix = "go";
5290 : :
5291 : 4248 : std::string init_fn_name;
5292 : 4248 : if (this->is_main_package())
5293 : 1635 : init_fn_name = "";
5294 : 2613 : else if (this->need_init_fn_)
5295 : 1525 : init_fn_name = this->get_init_fn_name();
5296 : : else
5297 : 1088 : init_fn_name = this->dummy_init_fn_name();
5298 : :
5299 : 4248 : Export exp(&stream);
5300 : 4248 : exp.register_builtin_types(this);
5301 : 4248 : exp.export_globals(this->package_name(),
5302 : : prefix,
5303 : : pkgpath,
5304 : 4248 : this->packages_,
5305 : 4248 : this->imports_,
5306 : : init_fn_name,
5307 : 4248 : this->imported_init_fns_,
5308 : 4248 : this->package_->bindings(),
5309 : : &marked_functions);
5310 : :
5311 : 4248 : if (!this->c_header_.empty() && !saw_errors())
5312 : 4 : this->write_c_header();
5313 : 8496 : }
5314 : :
5315 : : // Write the top level named struct types in C format to a C header
5316 : : // file. This is used when building the runtime package, to share
5317 : : // struct definitions between C and Go.
5318 : :
5319 : : void
5320 : 4 : Gogo::write_c_header()
5321 : : {
5322 : 4 : std::ofstream out;
5323 : 4 : out.open(this->c_header_.c_str());
5324 : 4 : if (out.fail())
5325 : : {
5326 : 0 : go_error_at(Linemap::unknown_location(),
5327 : : "cannot open %s: %m", this->c_header_.c_str());
5328 : 0 : return;
5329 : : }
5330 : :
5331 : 4 : std::list<Named_object*> types;
5332 : 4 : Bindings* top = this->package_->bindings();
5333 : 40118 : for (Bindings::const_definitions_iterator p = top->begin_definitions();
5334 : 40118 : p != top->end_definitions();
5335 : 40114 : ++p)
5336 : : {
5337 : 40114 : Named_object* no = *p;
5338 : :
5339 : : // Skip names that start with underscore followed by something
5340 : : // other than an uppercase letter, as when compiling the runtime
5341 : : // package they are mostly types defined by mkrsysinfo.sh based
5342 : : // on the C system header files. We don't need to translate
5343 : : // types to C and back to Go. But do accept the special cases
5344 : : // _defer, _panic, and _type.
5345 : 40114 : std::string name = Gogo::unpack_hidden_name(no->name());
5346 : 40114 : if (name[0] == '_'
5347 : 28484 : && (name[1] < 'A' || name[1] > 'Z')
5348 : 49938 : && (name != "_defer" && name != "_panic" && name != "_type"))
5349 : 9808 : continue;
5350 : :
5351 : 30306 : if (no->is_type() && no->type_value()->struct_type() != NULL)
5352 : 656 : types.push_back(no);
5353 : 30306 : if (no->is_const()
5354 : 50816 : && no->const_value()->type()->integer_type() != NULL
5355 : 50280 : && !no->const_value()->is_sink())
5356 : : {
5357 : 19970 : Numeric_constant nc;
5358 : 19970 : unsigned long val;
5359 : 19970 : if (no->const_value()->expr()->numeric_constant_value(&nc)
5360 : 19970 : && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID)
5361 : : {
5362 : 59214 : out << "#define " << no->message_name() << ' ' << val
5363 : 39476 : << std::endl;
5364 : : }
5365 : 19970 : }
5366 : 40114 : }
5367 : :
5368 : 4 : std::vector<const Named_object*> written;
5369 : 4 : int loop = 0;
5370 : 772 : while (!types.empty())
5371 : : {
5372 : 768 : Named_object* no = types.front();
5373 : 768 : types.pop_front();
5374 : :
5375 : 768 : std::vector<const Named_object*> needs;
5376 : 768 : std::vector<const Named_object*> declare;
5377 : 1536 : if (!no->type_value()->struct_type()->can_write_to_c_header(&needs,
5378 : : &declare))
5379 : 60 : continue;
5380 : :
5381 : 708 : bool ok = true;
5382 : 1172 : for (std::vector<const Named_object*>::const_iterator pr
5383 : 708 : = needs.begin();
5384 : 1172 : pr != needs.end() && ok;
5385 : 464 : ++pr)
5386 : : {
5387 : 17612 : for (std::list<Named_object*>::const_iterator pt = types.begin();
5388 : 17612 : pt != types.end() && ok;
5389 : 17148 : ++pt)
5390 : 17148 : if (*pr == *pt)
5391 : 112 : ok = false;
5392 : : }
5393 : 708 : if (!ok)
5394 : : {
5395 : 112 : ++loop;
5396 : 112 : if (loop > 10000)
5397 : : {
5398 : : // This should be impossible since the code parsed and
5399 : : // type checked.
5400 : 0 : go_unreachable();
5401 : : }
5402 : :
5403 : 112 : types.push_back(no);
5404 : 112 : continue;
5405 : : }
5406 : :
5407 : 1136 : for (std::vector<const Named_object*>::const_iterator pd
5408 : 596 : = declare.begin();
5409 : 1136 : pd != declare.end();
5410 : 540 : ++pd)
5411 : : {
5412 : 540 : if (*pd == no)
5413 : 112 : continue;
5414 : :
5415 : 456 : std::vector<const Named_object*> dneeds;
5416 : 456 : std::vector<const Named_object*> ddeclare;
5417 : 912 : if (!(*pd)->type_value()->struct_type()->
5418 : 912 : can_write_to_c_header(&dneeds, &ddeclare))
5419 : 28 : continue;
5420 : :
5421 : 428 : bool done = false;
5422 : 18764 : for (std::vector<const Named_object*>::const_iterator pw
5423 : 428 : = written.begin();
5424 : 18764 : pw != written.end();
5425 : 18336 : ++pw)
5426 : : {
5427 : 18668 : if (*pw == *pd)
5428 : : {
5429 : : done = true;
5430 : : break;
5431 : : }
5432 : : }
5433 : 428 : if (!done)
5434 : : {
5435 : 96 : out << std::endl;
5436 : 96 : out << "struct " << (*pd)->message_name() << ";" << std::endl;
5437 : 96 : written.push_back(*pd);
5438 : : }
5439 : 456 : }
5440 : :
5441 : 596 : out << std::endl;
5442 : 596 : out << "struct " << no->message_name() << " {" << std::endl;
5443 : 1192 : no->type_value()->struct_type()->write_to_c_header(out);
5444 : 596 : out << "};" << std::endl;
5445 : 596 : written.push_back(no);
5446 : 768 : }
5447 : :
5448 : 4 : out.close();
5449 : 4 : if (out.fail())
5450 : 0 : go_error_at(Linemap::unknown_location(),
5451 : : "error writing to %s: %m", this->c_header_.c_str());
5452 : 4 : }
5453 : :
5454 : : // Find the blocks in order to convert named types defined in blocks.
5455 : :
5456 : 9284 : class Convert_named_types : public Traverse
5457 : : {
5458 : : public:
5459 : 4642 : Convert_named_types(Gogo* gogo)
5460 : 4642 : : Traverse(traverse_blocks),
5461 : 4642 : gogo_(gogo)
5462 : : { }
5463 : :
5464 : : protected:
5465 : : int
5466 : : block(Block* block);
5467 : :
5468 : : private:
5469 : : Gogo* gogo_;
5470 : : };
5471 : :
5472 : : int
5473 : 1676056 : Convert_named_types::block(Block* block)
5474 : : {
5475 : 1676056 : this->gogo_->convert_named_types_in_bindings(block->bindings());
5476 : 1676056 : return TRAVERSE_CONTINUE;
5477 : : }
5478 : :
5479 : : // Convert all named types to the backend representation. Since named
5480 : : // types can refer to other types, this needs to be done in the right
5481 : : // sequence, which is handled by Named_type::convert. Here we arrange
5482 : : // to call that for each named type.
5483 : :
5484 : : void
5485 : 4642 : Gogo::convert_named_types()
5486 : : {
5487 : 4642 : this->convert_named_types_in_bindings(this->globals_);
5488 : 120871 : for (Packages::iterator p = this->packages_.begin();
5489 : 120871 : p != this->packages_.end();
5490 : 116229 : ++p)
5491 : : {
5492 : 116229 : Package* package = p->second;
5493 : 116229 : this->convert_named_types_in_bindings(package->bindings());
5494 : : }
5495 : :
5496 : 4642 : Convert_named_types cnt(this);
5497 : 4642 : this->traverse(&cnt);
5498 : :
5499 : : // Make all the builtin named types used for type descriptors, and
5500 : : // then convert them. They will only be written out if they are
5501 : : // needed.
5502 : 4642 : Type::make_type_descriptor_type();
5503 : 4642 : Type::make_type_descriptor_ptr_type();
5504 : 4642 : Function_type::make_function_type_descriptor_type();
5505 : 4642 : Pointer_type::make_pointer_type_descriptor_type();
5506 : 4642 : Struct_type::make_struct_type_descriptor_type();
5507 : 4642 : Array_type::make_array_type_descriptor_type();
5508 : 4642 : Array_type::make_slice_type_descriptor_type();
5509 : 4642 : Map_type::make_map_type_descriptor_type();
5510 : 4642 : Channel_type::make_chan_type_descriptor_type();
5511 : 4642 : Interface_type::make_interface_type_descriptor_type();
5512 : 4642 : Expression::make_func_descriptor_type();
5513 : 4642 : Type::convert_builtin_named_types(this);
5514 : :
5515 : 4642 : Runtime::convert_types(this);
5516 : :
5517 : 4642 : this->named_types_are_converted_ = true;
5518 : :
5519 : 4642 : Type::finish_pointer_types(this);
5520 : 4642 : }
5521 : :
5522 : : // Convert all names types in a set of bindings.
5523 : :
5524 : : void
5525 : 1796927 : Gogo::convert_named_types_in_bindings(Bindings* bindings)
5526 : : {
5527 : 4909809 : for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
5528 : 4909809 : p != bindings->end_definitions();
5529 : 3112882 : ++p)
5530 : : {
5531 : 3112882 : if ((*p)->is_type())
5532 : 733089 : (*p)->type_value()->convert(this);
5533 : : }
5534 : 1796927 : }
5535 : :
5536 : : void
5537 : 0 : debug_go_gogo(Gogo* gogo)
5538 : : {
5539 : 0 : if (gogo != NULL)
5540 : 0 : gogo->debug_dump();
5541 : 0 : }
5542 : :
5543 : : void
5544 : 0 : Gogo::debug_dump()
5545 : : {
5546 : 0 : std::cerr << "Packages:\n";
5547 : 0 : for (Packages::const_iterator p = this->packages_.begin();
5548 : 0 : p != this->packages_.end();
5549 : 0 : ++p)
5550 : : {
5551 : 0 : const char *tag = " ";
5552 : 0 : if (p->second == this->package_)
5553 : 0 : tag = "* ";
5554 : 0 : std::cerr << tag << "'" << p->first << "' "
5555 : 0 : << p->second->pkgpath() << " " << ((void*)p->second) << "\n";
5556 : : }
5557 : 0 : }
5558 : :
5559 : : // Class Function.
5560 : :
5561 : 299064 : Function::Function(Function_type* type, Named_object* enclosing, Block* block,
5562 : 299064 : Location location)
5563 : 299064 : : type_(type), enclosing_(enclosing), results_(NULL),
5564 : 299064 : closure_var_(NULL), block_(block), location_(location), labels_(),
5565 : 299064 : local_type_count_(0), descriptor_(NULL), fndecl_(NULL), defer_stack_(NULL),
5566 : 299064 : pragmas_(0), nested_functions_(0), is_sink_(false),
5567 : 299064 : results_are_named_(false), is_unnamed_type_stub_method_(false),
5568 : 299064 : calls_recover_(false), is_recover_thunk_(false), has_recover_thunk_(false),
5569 : 299064 : calls_defer_retaddr_(false), is_type_specific_function_(false),
5570 : 299064 : in_unique_section_(false), export_for_inlining_(false),
5571 : 299064 : is_inline_only_(false), is_referenced_by_inline_(false),
5572 : 299064 : is_exported_by_linkname_(false)
5573 : : {
5574 : 299064 : }
5575 : :
5576 : : // Create the named result variables.
5577 : :
5578 : : void
5579 : 295612 : Function::create_result_variables(Gogo* gogo)
5580 : : {
5581 : 295612 : const Typed_identifier_list* results = this->type_->results();
5582 : 295612 : if (results == NULL || results->empty())
5583 : : return;
5584 : :
5585 : 217468 : if (!results->front().name().empty())
5586 : 21590 : this->results_are_named_ = true;
5587 : :
5588 : 217468 : this->results_ = new Results();
5589 : 217468 : this->results_->reserve(results->size());
5590 : :
5591 : 217468 : Block* block = this->block_;
5592 : 217468 : int index = 0;
5593 : 217468 : for (Typed_identifier_list::const_iterator p = results->begin();
5594 : 462163 : p != results->end();
5595 : 244695 : ++p, ++index)
5596 : : {
5597 : 244695 : std::string name = p->name();
5598 : 244695 : if (name.empty() || Gogo::is_sink_name(name))
5599 : : {
5600 : 211888 : static int result_counter;
5601 : 211888 : char buf[100];
5602 : 211888 : snprintf(buf, sizeof buf, "$ret%d", result_counter);
5603 : 211888 : ++result_counter;
5604 : 211888 : name = gogo->pack_hidden_name(buf, false);
5605 : : }
5606 : 244695 : Result_variable* result = new Result_variable(p->type(), this, index,
5607 : 244695 : p->location());
5608 : 244695 : Named_object* no = block->bindings()->add_result_variable(name, result);
5609 : 244695 : if (no->is_result_variable())
5610 : 244691 : this->results_->push_back(no);
5611 : : else
5612 : : {
5613 : 4 : static int dummy_result_count;
5614 : 4 : char buf[100];
5615 : 4 : snprintf(buf, sizeof buf, "$dret%d", dummy_result_count);
5616 : 4 : ++dummy_result_count;
5617 : 4 : name = gogo->pack_hidden_name(buf, false);
5618 : 4 : no = block->bindings()->add_result_variable(name, result);
5619 : 4 : go_assert(no->is_result_variable());
5620 : 4 : this->results_->push_back(no);
5621 : : }
5622 : 244695 : }
5623 : : }
5624 : :
5625 : : // Update the named result variables when cloning a function which
5626 : : // calls recover.
5627 : :
5628 : : void
5629 : 1662 : Function::update_result_variables()
5630 : : {
5631 : 1662 : if (this->results_ == NULL)
5632 : : return;
5633 : :
5634 : 12 : for (Results::iterator p = this->results_->begin();
5635 : 12 : p != this->results_->end();
5636 : 6 : ++p)
5637 : 6 : (*p)->result_var_value()->set_function(this);
5638 : : }
5639 : :
5640 : : // Whether this method should not be included in the type descriptor.
5641 : :
5642 : : bool
5643 : 572078 : Function::nointerface() const
5644 : : {
5645 : 572078 : go_assert(this->is_method());
5646 : 572078 : return (this->pragmas_ & GOPRAGMA_NOINTERFACE) != 0;
5647 : : }
5648 : :
5649 : : // Record that this method should not be included in the type
5650 : : // descriptor.
5651 : :
5652 : : void
5653 : 2 : Function::set_nointerface()
5654 : : {
5655 : 2 : this->pragmas_ |= GOPRAGMA_NOINTERFACE;
5656 : 2 : }
5657 : :
5658 : : // Return the closure variable, creating it if necessary.
5659 : :
5660 : : Named_object*
5661 : 56719 : Function::closure_var()
5662 : : {
5663 : 56719 : if (this->closure_var_ == NULL)
5664 : : {
5665 : 13057 : go_assert(this->descriptor_ == NULL);
5666 : : // We don't know the type of the variable yet. We add fields as
5667 : : // we find them.
5668 : 13057 : Location loc = this->type_->location();
5669 : 13057 : Struct_field_list* sfl = new Struct_field_list;
5670 : 13057 : Struct_type* struct_type = Type::make_struct_type(sfl, loc);
5671 : 13057 : struct_type->set_is_struct_incomparable();
5672 : 13057 : Variable* var = new Variable(Type::make_pointer_type(struct_type),
5673 : 26114 : NULL, false, false, false, loc);
5674 : 13057 : var->set_is_used();
5675 : 13057 : var->set_is_closure();
5676 : 13057 : this->closure_var_ = Named_object::make_variable("$closure", NULL, var);
5677 : : // Note that the new variable is not in any binding contour.
5678 : : }
5679 : 56719 : return this->closure_var_;
5680 : : }
5681 : :
5682 : : // Set the type of the closure variable.
5683 : :
5684 : : void
5685 : 175573 : Function::set_closure_type()
5686 : : {
5687 : 175573 : if (this->closure_var_ == NULL)
5688 : : return;
5689 : 13057 : Named_object* closure = this->closure_var_;
5690 : 26114 : Struct_type* st = closure->var_value()->type()->deref()->struct_type();
5691 : :
5692 : : // The first field of a closure is always a pointer to the function
5693 : : // code.
5694 : 13057 : Type* voidptr_type = Type::make_pointer_type(Type::make_void_type());
5695 : 13057 : st->push_field(Struct_field(Typed_identifier(".f", voidptr_type,
5696 : : this->location_)));
5697 : :
5698 : 13057 : unsigned int index = 1;
5699 : 13057 : for (Closure_fields::const_iterator p = this->closure_fields_.begin();
5700 : 38534 : p != this->closure_fields_.end();
5701 : 25477 : ++p, ++index)
5702 : : {
5703 : 25477 : Named_object* no = p->first;
5704 : 25477 : char buf[20];
5705 : 25477 : snprintf(buf, sizeof buf, "%u", index);
5706 : 25477 : std::string n = no->name() + buf;
5707 : 25477 : Type* var_type;
5708 : 25477 : if (no->is_variable())
5709 : 24650 : var_type = no->var_value()->type();
5710 : : else
5711 : 827 : var_type = no->result_var_value()->type();
5712 : 25477 : Type* field_type = Type::make_pointer_type(var_type);
5713 : 25477 : st->push_field(Struct_field(Typed_identifier(n, field_type, p->second)));
5714 : 25477 : }
5715 : : }
5716 : :
5717 : : // Return whether this function is a method.
5718 : :
5719 : : bool
5720 : 1802575 : Function::is_method() const
5721 : : {
5722 : 1802575 : return this->type_->is_method();
5723 : : }
5724 : :
5725 : : // Add a label definition.
5726 : :
5727 : : Label*
5728 : 10526 : Function::add_label_definition(Gogo* gogo, const std::string& label_name,
5729 : : Location location)
5730 : : {
5731 : 10526 : Label* lnull = NULL;
5732 : 10526 : std::pair<Labels::iterator, bool> ins =
5733 : 10526 : this->labels_.insert(std::make_pair(label_name, lnull));
5734 : 10526 : Label* label;
5735 : 10526 : if (label_name == "_")
5736 : : {
5737 : 3 : label = Label::create_dummy_label();
5738 : 3 : if (ins.second)
5739 : 2 : ins.first->second = label;
5740 : : }
5741 : 10523 : else if (ins.second)
5742 : : {
5743 : : // This is a new label.
5744 : 1126 : label = new Label(label_name);
5745 : 1126 : ins.first->second = label;
5746 : : }
5747 : : else
5748 : : {
5749 : : // The label was already in the hash table.
5750 : 9397 : label = ins.first->second;
5751 : 9397 : if (label->is_defined())
5752 : : {
5753 : 2 : go_error_at(location, "label %qs already defined",
5754 : 2 : Gogo::message_name(label_name).c_str());
5755 : 2 : go_inform(label->location(), "previous definition of %qs was here",
5756 : 2 : Gogo::message_name(label_name).c_str());
5757 : 2 : return new Label(label_name);
5758 : : }
5759 : : }
5760 : :
5761 : 10524 : label->define(location, gogo->bindings_snapshot(location));
5762 : :
5763 : : // Issue any errors appropriate for any previous goto's to this
5764 : : // label.
5765 : 10524 : const std::vector<Bindings_snapshot*>& refs(label->refs());
5766 : 12089 : for (std::vector<Bindings_snapshot*>::const_iterator p = refs.begin();
5767 : 12089 : p != refs.end();
5768 : 1565 : ++p)
5769 : 1565 : (*p)->check_goto_to(gogo->current_block());
5770 : 10524 : label->clear_refs();
5771 : :
5772 : 10524 : return label;
5773 : : }
5774 : :
5775 : : // Add a reference to a label.
5776 : :
5777 : : Label*
5778 : 10959 : Function::add_label_reference(Gogo* gogo, const std::string& label_name,
5779 : : Location location, bool issue_goto_errors)
5780 : : {
5781 : 10959 : Label* lnull = NULL;
5782 : 10959 : std::pair<Labels::iterator, bool> ins =
5783 : 10959 : this->labels_.insert(std::make_pair(label_name, lnull));
5784 : 10959 : Label* label;
5785 : 10959 : if (!ins.second)
5786 : : {
5787 : : // The label was already in the hash table.
5788 : 1559 : label = ins.first->second;
5789 : : }
5790 : : else
5791 : : {
5792 : 9400 : go_assert(ins.first->second == NULL);
5793 : 9400 : label = new Label(label_name);
5794 : 9400 : ins.first->second = label;
5795 : : }
5796 : :
5797 : 10959 : label->set_is_used();
5798 : :
5799 : 10959 : if (issue_goto_errors)
5800 : : {
5801 : 2101 : Bindings_snapshot* snapshot = label->snapshot();
5802 : 2101 : if (snapshot != NULL)
5803 : 533 : snapshot->check_goto_from(gogo->current_block(), location);
5804 : : else
5805 : 1568 : label->add_snapshot_ref(gogo->bindings_snapshot(location));
5806 : : }
5807 : :
5808 : 10959 : return label;
5809 : : }
5810 : :
5811 : : // Warn about labels that are defined but not used.
5812 : :
5813 : : void
5814 : 175573 : Function::check_labels() const
5815 : : {
5816 : 177260 : for (Labels::const_iterator p = this->labels_.begin();
5817 : 177260 : p != this->labels_.end();
5818 : 1687 : p++)
5819 : : {
5820 : 1687 : Label* label = p->second;
5821 : 1687 : if (!label->is_used())
5822 : 7 : go_error_at(label->location(), "label %qs defined and not used",
5823 : 14 : Gogo::message_name(label->name()).c_str());
5824 : : }
5825 : 175573 : }
5826 : :
5827 : : // Set the receiver type. This is used to remove aliases.
5828 : :
5829 : : void
5830 : 2 : Function::set_receiver_type(Type* rtype)
5831 : : {
5832 : 2 : Function_type* oft = this->type_;
5833 : 2 : Typed_identifier* rec = new Typed_identifier(oft->receiver()->name(),
5834 : : rtype,
5835 : 2 : oft->receiver()->location());
5836 : 2 : Typed_identifier_list* parameters = NULL;
5837 : 2 : if (oft->parameters() != NULL)
5838 : 0 : parameters = oft->parameters()->copy();
5839 : 2 : Typed_identifier_list* results = NULL;
5840 : 2 : if (oft->results() != NULL)
5841 : 0 : results = oft->results()->copy();
5842 : 2 : Function_type* nft = Type::make_function_type(rec, parameters, results,
5843 : : oft->location());
5844 : 2 : this->type_ = nft;
5845 : 2 : }
5846 : :
5847 : : // Swap one function with another. This is used when building the
5848 : : // thunk we use to call a function which calls recover. It may not
5849 : : // work for any other case.
5850 : :
5851 : : void
5852 : 831 : Function::swap_for_recover(Function *x)
5853 : : {
5854 : 831 : go_assert(this->enclosing_ == x->enclosing_);
5855 : 831 : std::swap(this->results_, x->results_);
5856 : 831 : std::swap(this->closure_var_, x->closure_var_);
5857 : 831 : std::swap(this->block_, x->block_);
5858 : 831 : go_assert(this->location_ == x->location_);
5859 : 831 : go_assert(this->fndecl_ == NULL && x->fndecl_ == NULL);
5860 : 831 : go_assert(this->defer_stack_ == NULL && x->defer_stack_ == NULL);
5861 : 831 : }
5862 : :
5863 : : // Traverse the tree.
5864 : :
5865 : : int
5866 : 4842826 : Function::traverse(Traverse* traverse)
5867 : : {
5868 : 4842826 : unsigned int traverse_mask = traverse->traverse_mask();
5869 : :
5870 : 4842826 : if ((traverse_mask
5871 : 4842826 : & (Traverse::traverse_types | Traverse::traverse_expressions))
5872 : : != 0)
5873 : : {
5874 : 3031568 : if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
5875 : : return TRAVERSE_EXIT;
5876 : : }
5877 : :
5878 : : // FIXME: We should check traverse_functions here if nested
5879 : : // functions are stored in block bindings.
5880 : 4842826 : if (this->block_ != NULL
5881 : 4842826 : && (traverse_mask
5882 : 4842826 : & (Traverse::traverse_variables
5883 : : | Traverse::traverse_constants
5884 : : | Traverse::traverse_blocks
5885 : : | Traverse::traverse_statements
5886 : : | Traverse::traverse_expressions
5887 : : | Traverse::traverse_types)) != 0)
5888 : : {
5889 : 4274499 : if (this->block_->traverse(traverse) == TRAVERSE_EXIT)
5890 : : return TRAVERSE_EXIT;
5891 : : }
5892 : :
5893 : : return TRAVERSE_CONTINUE;
5894 : : }
5895 : :
5896 : : // Work out types for unspecified variables and constants.
5897 : :
5898 : : void
5899 : 175573 : Function::determine_types(Gogo* gogo)
5900 : : {
5901 : 175573 : this->set_closure_type();
5902 : 175573 : if (this->block_ != NULL)
5903 : 175573 : this->block_->determine_types(gogo);
5904 : 175573 : }
5905 : :
5906 : : // Return the function descriptor, the value you get when you refer to
5907 : : // the function in Go code without calling it.
5908 : :
5909 : : Expression*
5910 : 194761 : Function::descriptor(Gogo*, Named_object* no)
5911 : : {
5912 : 194761 : go_assert(!this->is_method());
5913 : 194761 : go_assert(this->closure_var_ == NULL);
5914 : 194761 : if (this->descriptor_ == NULL)
5915 : 154440 : this->descriptor_ = Expression::make_func_descriptor(no);
5916 : 194761 : return this->descriptor_;
5917 : : }
5918 : :
5919 : : // Get a pointer to the variable representing the defer stack for this
5920 : : // function, making it if necessary. The value of the variable is set
5921 : : // by the runtime routines to true if the function is returning,
5922 : : // rather than panicing through. A pointer to this variable is used
5923 : : // as a marker for the functions on the defer stack associated with
5924 : : // this function. A function-specific variable permits inlining a
5925 : : // function which uses defer.
5926 : :
5927 : : Expression*
5928 : 47518 : Function::defer_stack(Location location)
5929 : : {
5930 : 47518 : if (this->defer_stack_ == NULL)
5931 : : {
5932 : 8354 : Type* t = Type::lookup_bool_type();
5933 : 8354 : Expression* n = Expression::make_boolean(false, location);
5934 : 8354 : this->defer_stack_ = Statement::make_temporary(t, n, location);
5935 : 8354 : this->defer_stack_->set_is_address_taken();
5936 : : }
5937 : 47518 : Expression* ref = Expression::make_temporary_reference(this->defer_stack_,
5938 : : location);
5939 : 47518 : return Expression::make_unary(OPERATOR_AND, ref, location);
5940 : : }
5941 : :
5942 : : // Export the function.
5943 : :
5944 : : void
5945 : 79190 : Function::export_func(Export* exp, const Named_object* no) const
5946 : : {
5947 : 79190 : Block* block = NULL;
5948 : 79190 : if (this->export_for_inlining())
5949 : 16706 : block = this->block_;
5950 : 158380 : Function::export_func_with_type(exp, no, this->type_, this->results_,
5951 : 79190 : this->is_method() && this->nointerface(),
5952 : : this->asm_name(), block, this->location_);
5953 : 79190 : }
5954 : :
5955 : : // Export a function with a type.
5956 : :
5957 : : void
5958 : 380526 : Function::export_func_with_type(Export* exp, const Named_object* no,
5959 : : const Function_type* fntype,
5960 : : Function::Results* result_vars,
5961 : : bool nointerface, const std::string& asm_name,
5962 : : Block* block, Location loc)
5963 : : {
5964 : 380526 : exp->write_c_string("func ");
5965 : :
5966 : 380526 : if (nointerface)
5967 : : {
5968 : 2 : go_assert(fntype->is_method());
5969 : 2 : exp->write_c_string("/*nointerface*/ ");
5970 : : }
5971 : :
5972 : 380526 : if (!asm_name.empty())
5973 : : {
5974 : 1943 : exp->write_c_string("/*asm ");
5975 : 1943 : exp->write_string(asm_name);
5976 : 1943 : exp->write_c_string(" */ ");
5977 : : }
5978 : :
5979 : 380526 : if (fntype->is_method())
5980 : : {
5981 : 332259 : exp->write_c_string("(");
5982 : 332259 : const Typed_identifier* receiver = fntype->receiver();
5983 : 332259 : exp->write_name(receiver->name());
5984 : 332259 : exp->write_escape(receiver->note());
5985 : 332259 : exp->write_c_string(" ");
5986 : 332259 : exp->write_type(receiver->type()->unalias());
5987 : 332259 : exp->write_c_string(") ");
5988 : : }
5989 : :
5990 : 380526 : if (no->package() != NULL && !fntype->is_method())
5991 : : {
5992 : 12521 : char buf[50];
5993 : 12521 : snprintf(buf, sizeof buf, "<p%d>", exp->package_index(no->package()));
5994 : 12521 : exp->write_c_string(buf);
5995 : : }
5996 : :
5997 : 380526 : const std::string& name(no->name());
5998 : 380526 : if (!Gogo::is_hidden_name(name))
5999 : 266402 : exp->write_string(name);
6000 : : else
6001 : : {
6002 : 114124 : exp->write_c_string(".");
6003 : 114124 : exp->write_string(Gogo::unpack_hidden_name(name));
6004 : : }
6005 : :
6006 : 380526 : exp->write_c_string(" (");
6007 : 380526 : const Typed_identifier_list* parameters = fntype->parameters();
6008 : 380526 : if (parameters != NULL)
6009 : : {
6010 : 196975 : size_t i = 0;
6011 : 196975 : bool is_varargs = fntype->is_varargs();
6012 : 196975 : bool first = true;
6013 : 196975 : for (Typed_identifier_list::const_iterator p = parameters->begin();
6014 : 502778 : p != parameters->end();
6015 : 305803 : ++p, ++i)
6016 : : {
6017 : 305803 : if (first)
6018 : : first = false;
6019 : : else
6020 : 108828 : exp->write_c_string(", ");
6021 : 305803 : exp->write_name(p->name());
6022 : 305803 : exp->write_escape(p->note());
6023 : 305803 : exp->write_c_string(" ");
6024 : 305803 : if (!is_varargs || p + 1 != parameters->end())
6025 : 297155 : exp->write_type(p->type());
6026 : : else
6027 : : {
6028 : 8648 : exp->write_c_string("...");
6029 : 17296 : exp->write_type(p->type()->array_type()->element_type());
6030 : : }
6031 : : }
6032 : : }
6033 : 380526 : exp->write_c_string(")");
6034 : :
6035 : 380526 : const Typed_identifier_list* result_decls = fntype->results();
6036 : 380526 : if (result_decls != NULL)
6037 : : {
6038 : 285502 : if (result_decls->size() == 1
6039 : 238549 : && result_decls->begin()->name().empty()
6040 : 495868 : && block == NULL)
6041 : : {
6042 : 202164 : exp->write_c_string(" ");
6043 : 202164 : exp->write_type(result_decls->begin()->type());
6044 : : }
6045 : : else
6046 : : {
6047 : 83338 : exp->write_c_string(" (");
6048 : 83338 : bool first = true;
6049 : 83338 : Results::const_iterator pr;
6050 : 83338 : if (result_vars != NULL)
6051 : 23062 : pr = result_vars->begin();
6052 : 228544 : for (Typed_identifier_list::const_iterator pd = result_decls->begin();
6053 : 228544 : pd != result_decls->end();
6054 : 145206 : ++pd)
6055 : : {
6056 : 145206 : if (first)
6057 : : first = false;
6058 : : else
6059 : 61868 : exp->write_c_string(", ");
6060 : : // We only use pr->name, which may be artificial, if
6061 : : // need it for inlining.
6062 : 145206 : if (block == NULL || result_vars == NULL)
6063 : 131610 : exp->write_name(pd->name());
6064 : : else
6065 : 13596 : exp->write_name((*pr)->name());
6066 : 145206 : exp->write_escape(pd->note());
6067 : 145206 : exp->write_c_string(" ");
6068 : 145206 : exp->write_type(pd->type());
6069 : 145206 : if (result_vars != NULL)
6070 : 34889 : ++pr;
6071 : : }
6072 : 83338 : if (result_vars != NULL)
6073 : 23062 : go_assert(pr == result_vars->end());
6074 : 83338 : exp->write_c_string(")");
6075 : : }
6076 : : }
6077 : :
6078 : 380526 : if (block == NULL)
6079 : 363820 : exp->write_c_string("\n");
6080 : : else
6081 : : {
6082 : 16706 : int indent = 1;
6083 : 16706 : if (fntype->is_method())
6084 : 7254 : indent++;
6085 : :
6086 : 16706 : Export_function_body efb(exp, indent);
6087 : :
6088 : 16706 : efb.indent();
6089 : 16706 : efb.write_c_string("// ");
6090 : 16706 : efb.write_string(Linemap::location_to_file(block->start_location()));
6091 : 16706 : efb.write_char(':');
6092 : 16706 : char buf[100];
6093 : 16706 : snprintf(buf, sizeof buf, "%d", Linemap::location_to_line(loc));
6094 : 16706 : efb.write_c_string(buf);
6095 : 16706 : efb.write_char('\n');
6096 : 16706 : block->export_block(&efb);
6097 : :
6098 : 16706 : const std::string& body(efb.body());
6099 : :
6100 : 16706 : snprintf(buf, sizeof buf, " <inl:%lu>\n",
6101 : 16706 : static_cast<unsigned long>(body.length()));
6102 : 16706 : exp->write_c_string(buf);
6103 : :
6104 : 16706 : exp->write_string(body);
6105 : 16706 : }
6106 : 380526 : }
6107 : :
6108 : : // Import a function.
6109 : :
6110 : : bool
6111 : 3055368 : Function::import_func(Import* imp, std::string* pname,
6112 : : Package** ppkg, bool* pis_exported,
6113 : : Typed_identifier** preceiver,
6114 : : Typed_identifier_list** pparameters,
6115 : : Typed_identifier_list** presults,
6116 : : bool* is_varargs,
6117 : : bool* nointerface,
6118 : : std::string* asm_name,
6119 : : std::string* body)
6120 : : {
6121 : 3055368 : imp->require_c_string("func ");
6122 : :
6123 : 3055368 : *nointerface = false;
6124 : 3167537 : while (imp->match_c_string("/*"))
6125 : : {
6126 : 112169 : imp->advance(2);
6127 : 112169 : if (imp->match_c_string("nointerface"))
6128 : : {
6129 : 2 : imp->require_c_string("nointerface*/ ");
6130 : 2 : *nointerface = true;
6131 : : }
6132 : 112167 : else if (imp->match_c_string("asm"))
6133 : : {
6134 : 112167 : imp->require_c_string("asm ");
6135 : 112167 : *asm_name = imp->read_identifier();
6136 : 112167 : imp->require_c_string(" */ ");
6137 : : }
6138 : : else
6139 : : {
6140 : 0 : go_error_at(imp->location(),
6141 : : "import error at %d: unrecognized function comment",
6142 : : imp->pos());
6143 : 0 : return false;
6144 : : }
6145 : : }
6146 : :
6147 : 3055368 : if (*nointerface)
6148 : : {
6149 : : // Only a method can be nointerface.
6150 : 2 : go_assert(imp->peek_char() == '(');
6151 : : }
6152 : :
6153 : 3055368 : *preceiver = NULL;
6154 : 3055368 : if (imp->peek_char() == '(')
6155 : : {
6156 : 1784240 : imp->require_c_string("(");
6157 : 1784240 : std::string name = imp->read_name();
6158 : 1784240 : std::string escape_note = imp->read_escape();
6159 : 1784240 : imp->require_c_string(" ");
6160 : 1784240 : Type* rtype = imp->read_type();
6161 : 1784240 : *preceiver = new Typed_identifier(name, rtype, imp->location());
6162 : 1784240 : (*preceiver)->set_note(escape_note);
6163 : 1784240 : imp->require_c_string(") ");
6164 : 1784240 : }
6165 : :
6166 : 3055368 : if (!Import::read_qualified_identifier(imp, pname, ppkg, pis_exported))
6167 : : {
6168 : 0 : go_error_at(imp->location(),
6169 : : "import error at %d: bad function name in export data",
6170 : : imp->pos());
6171 : 0 : return false;
6172 : : }
6173 : :
6174 : 3055368 : Typed_identifier_list* parameters;
6175 : 3055368 : *is_varargs = false;
6176 : 3055368 : imp->require_c_string(" (");
6177 : 3055368 : if (imp->peek_char() == ')')
6178 : : parameters = NULL;
6179 : : else
6180 : : {
6181 : 1898440 : parameters = new Typed_identifier_list();
6182 : 4523444 : while (true)
6183 : : {
6184 : 3210942 : std::string name = imp->read_name();
6185 : 3210942 : std::string escape_note = imp->read_escape();
6186 : 3210942 : imp->require_c_string(" ");
6187 : :
6188 : 3210942 : if (imp->match_c_string("..."))
6189 : : {
6190 : 52696 : imp->advance(3);
6191 : 52696 : *is_varargs = true;
6192 : : }
6193 : :
6194 : 3210942 : Type* ptype = imp->read_type();
6195 : 3210942 : if (*is_varargs)
6196 : 52696 : ptype = Type::make_array_type(ptype, NULL);
6197 : 3210942 : Typed_identifier t = Typed_identifier(name, ptype, imp->location());
6198 : 3210942 : t.set_note(escape_note);
6199 : 3210942 : parameters->push_back(t);
6200 : 3210942 : if (imp->peek_char() != ',')
6201 : : break;
6202 : 1312502 : go_assert(!*is_varargs);
6203 : 1312502 : imp->require_c_string(", ");
6204 : 3210942 : }
6205 : : }
6206 : 3055368 : imp->require_c_string(")");
6207 : 3055368 : *pparameters = parameters;
6208 : :
6209 : 3055368 : Typed_identifier_list* results;
6210 : 3055368 : if (imp->peek_char() != ' ' || imp->match_c_string(" <inl"))
6211 : : results = NULL;
6212 : : else
6213 : : {
6214 : 2247714 : results = new Typed_identifier_list();
6215 : 2247714 : imp->require_c_string(" ");
6216 : 2247714 : if (imp->peek_char() != '(')
6217 : : {
6218 : 1422145 : Type* rtype = imp->read_type();
6219 : 1422145 : results->push_back(Typed_identifier("", rtype, imp->location()));
6220 : : }
6221 : : else
6222 : : {
6223 : 825569 : imp->require_c_string("(");
6224 : 1762811 : while (true)
6225 : : {
6226 : 1294190 : std::string name = imp->read_name();
6227 : 1294190 : std::string note = imp->read_escape();
6228 : 1294190 : imp->require_c_string(" ");
6229 : 1294190 : Type* rtype = imp->read_type();
6230 : 1294190 : Typed_identifier t = Typed_identifier(name, rtype,
6231 : 1294190 : imp->location());
6232 : 1294190 : t.set_note(note);
6233 : 1294190 : results->push_back(t);
6234 : 1294190 : if (imp->peek_char() != ',')
6235 : : break;
6236 : 468621 : imp->require_c_string(", ");
6237 : 1294190 : }
6238 : 825569 : imp->require_c_string(")");
6239 : : }
6240 : : }
6241 : 3055368 : *presults = results;
6242 : :
6243 : 3055368 : if (!imp->match_c_string(" <inl:"))
6244 : : {
6245 : 2548797 : imp->require_semicolon_if_old_version();
6246 : 2548797 : imp->require_c_string("\n");
6247 : 2548797 : body->clear();
6248 : : }
6249 : : else
6250 : : {
6251 : 506571 : imp->require_c_string(" <inl:");
6252 : 506571 : std::string lenstr;
6253 : 1989000 : int c;
6254 : 3471429 : while (true)
6255 : : {
6256 : 1989000 : c = imp->peek_char();
6257 : 1989000 : if (c < '0' || c > '9')
6258 : : break;
6259 : 1482429 : lenstr += c;
6260 : 1482429 : imp->get_char();
6261 : : }
6262 : 506571 : imp->require_c_string(">\n");
6263 : :
6264 : 506571 : errno = 0;
6265 : 506571 : char* end;
6266 : 506571 : long llen = strtol(lenstr.c_str(), &end, 10);
6267 : 506571 : if (*end != '\0'
6268 : 506571 : || llen < 0
6269 : 506571 : || (llen == LONG_MAX && errno == ERANGE))
6270 : : {
6271 : 0 : go_error_at(imp->location(), "invalid inline function length %s",
6272 : : lenstr.c_str());
6273 : 0 : return false;
6274 : : }
6275 : :
6276 : 506571 : imp->read(static_cast<size_t>(llen), body);
6277 : 506571 : }
6278 : :
6279 : : return true;
6280 : : }
6281 : :
6282 : : // Get the backend name.
6283 : :
6284 : : void
6285 : 453177 : Function::backend_name(Gogo* gogo, Named_object* no, Backend_name *bname)
6286 : : {
6287 : 453177 : if (!this->asm_name_.empty())
6288 : 186224 : bname->set_asm_name(this->asm_name_);
6289 : 266953 : else if (no->package() == NULL && no->name() == gogo->get_init_fn_name())
6290 : : {
6291 : : // These names appear in the export data and are used
6292 : : // directly in the assembler code. If we change this here
6293 : : // we need to change Gogo::init_imports.
6294 : 3452 : bname->set_asm_name(no->name());
6295 : : }
6296 : 263501 : else if (this->enclosing_ != NULL)
6297 : : {
6298 : : // Rewrite the nested name to use the enclosing function name.
6299 : : // We don't do this earlier because we just store simple names
6300 : : // in a Named_object, not Backend_names.
6301 : :
6302 : : // The name was set by nested_function_name, which always
6303 : : // appends ..funcNNN. We want that to be our suffix.
6304 : 35822 : size_t pos = no->name().find("..func");
6305 : 35822 : go_assert(pos != std::string::npos);
6306 : :
6307 : 35822 : Named_object* enclosing = this->enclosing_;
6308 : 39387 : while (true)
6309 : : {
6310 : 39387 : Named_object* parent = enclosing->func_value()->enclosing();
6311 : 39387 : if (parent == NULL)
6312 : : break;
6313 : : enclosing = parent;
6314 : : }
6315 : :
6316 : 35822 : Type* rtype = NULL;
6317 : 35822 : if (enclosing->func_value()->type()->is_method())
6318 : 4021 : rtype = enclosing->func_value()->type()->receiver()->type();
6319 : 35822 : gogo->function_backend_name(enclosing->name(), enclosing->package(),
6320 : : rtype, bname);
6321 : 35822 : bname->append_suffix(no->name().substr(pos));
6322 : : }
6323 : : else
6324 : : {
6325 : 227679 : Type* rtype = NULL;
6326 : 227679 : if (this->type_->is_method())
6327 : 78024 : rtype = this->type_->receiver()->type();
6328 : 227679 : gogo->function_backend_name(no->name(), no->package(), rtype, bname);
6329 : : }
6330 : 453177 : }
6331 : :
6332 : : // Get the backend representation.
6333 : :
6334 : : Bfunction*
6335 : 3814181 : Function::get_or_make_decl(Gogo* gogo, Named_object* no)
6336 : : {
6337 : 3814181 : if (this->fndecl_ == NULL)
6338 : : {
6339 : 298741 : unsigned int flags = 0;
6340 : 298741 : if (no->package() != NULL)
6341 : : {
6342 : : // Functions defined in other packages must be visible.
6343 : : flags |= Backend::function_is_visible;
6344 : : }
6345 : 286494 : else if (this->enclosing_ != NULL || Gogo::is_thunk(no))
6346 : : ;
6347 : 246496 : else if (Gogo::unpack_hidden_name(no->name()) == "init"
6348 : 246496 : && !this->type_->is_method())
6349 : : ;
6350 : 246496 : else if (no->name() == gogo->get_init_fn_name())
6351 : : flags |= Backend::function_is_visible;
6352 : 243044 : else if (Gogo::unpack_hidden_name(no->name()) == "main"
6353 : 243044 : && gogo->is_main_package())
6354 : : flags |= Backend::function_is_visible;
6355 : : // Methods have to be public even if they are hidden because
6356 : : // they can be pulled into type descriptors when using
6357 : : // anonymous fields.
6358 : 241281 : else if (!Gogo::is_hidden_name(no->name())
6359 : 241281 : || this->type_->is_method())
6360 : : {
6361 : 201508 : if (!this->is_unnamed_type_stub_method_)
6362 : 208910 : flags |= Backend::function_is_visible;
6363 : : }
6364 : :
6365 : 298741 : if (!this->asm_name_.empty())
6366 : : {
6367 : : // If an assembler name is explicitly specified, there must
6368 : : // be some reason to refer to the symbol from a different
6369 : : // object file.
6370 : 93575 : flags |= Backend::function_is_visible;
6371 : : }
6372 : :
6373 : : // If an inline body refers to this function, then it
6374 : : // needs to be visible in the symbol table.
6375 : 298741 : if (this->is_referenced_by_inline_)
6376 : 8157 : flags |= Backend::function_is_visible;
6377 : :
6378 : : // A go:linkname directive can be used to force a function to be
6379 : : // visible.
6380 : 298741 : if (this->is_exported_by_linkname_)
6381 : 1673 : flags |= Backend::function_is_visible;
6382 : :
6383 : : // If a function calls the predeclared recover function, we
6384 : : // can't inline it, because recover behaves differently in a
6385 : : // function passed directly to defer. If this is a recover
6386 : : // thunk that we built to test whether a function can be
6387 : : // recovered, we can't inline it, because that will mess up
6388 : : // our return address comparison.
6389 : 298741 : bool is_inlinable = !(this->calls_recover_ || this->is_recover_thunk_);
6390 : :
6391 : : // If a function calls __go_set_defer_retaddr, then mark it as
6392 : : // uninlinable. This prevents the GCC backend from splitting
6393 : : // the function; splitting the function is a bad idea because we
6394 : : // want the return address label to be in the same function as
6395 : : // the call.
6396 : 298741 : if (this->calls_defer_retaddr_)
6397 : 8840 : is_inlinable = false;
6398 : :
6399 : : // Check the //go:noinline compiler directive.
6400 : 298741 : if ((this->pragmas_ & GOPRAGMA_NOINLINE) != 0)
6401 : : is_inlinable = false;
6402 : :
6403 : 298381 : if (is_inlinable)
6404 : 287878 : flags |= Backend::function_is_inlinable;
6405 : :
6406 : : // If this is a thunk created to call a function which calls
6407 : : // the predeclared recover function, we need to disable
6408 : : // stack splitting for the thunk.
6409 : 298741 : bool disable_split_stack = this->is_recover_thunk_;
6410 : :
6411 : : // Check the //go:nosplit compiler directive.
6412 : 298741 : if ((this->pragmas_ & GOPRAGMA_NOSPLIT) != 0)
6413 : : disable_split_stack = true;
6414 : :
6415 : 297307 : if (disable_split_stack)
6416 : 2264 : flags |= Backend::function_no_split_stack;
6417 : :
6418 : : // This should go into a unique section if that has been
6419 : : // requested elsewhere, or if this is a nointerface function.
6420 : : // We want to put a nointerface function into a unique section
6421 : : // because there is a good chance that the linker garbage
6422 : : // collection can discard it.
6423 : 298741 : if (this->in_unique_section_
6424 : 298741 : || (this->is_method() && this->nointerface()))
6425 : 3 : flags |= Backend::function_in_unique_section;
6426 : :
6427 : 298741 : if (this->is_inline_only_)
6428 : 12247 : flags |= Backend::function_only_inline;
6429 : :
6430 : 298741 : Btype* functype = this->type_->get_backend_fntype(gogo);
6431 : :
6432 : 298741 : Backend_name bname;
6433 : 298741 : this->backend_name(gogo, no, &bname);
6434 : :
6435 : 597482 : this->fndecl_ = gogo->backend()->function(functype,
6436 : 597482 : bname.name(),
6437 : 298741 : bname.optional_asm_name(),
6438 : : flags,
6439 : : this->location());
6440 : 298741 : }
6441 : 3814181 : return this->fndecl_;
6442 : : }
6443 : :
6444 : : // Get the backend name.
6445 : :
6446 : : void
6447 : 382066 : Function_declaration::backend_name(Gogo* gogo, Named_object* no,
6448 : : Backend_name* bname)
6449 : : {
6450 : 382066 : if (!this->asm_name_.empty())
6451 : 214217 : bname->set_asm_name(this->asm_name_);
6452 : : else
6453 : : {
6454 : 167849 : Type* rtype = NULL;
6455 : 167849 : if (this->fntype_->is_method())
6456 : 120040 : rtype = this->fntype_->receiver()->type();
6457 : 167849 : gogo->function_backend_name(no->name(), no->package(), rtype, bname);
6458 : : }
6459 : 382066 : }
6460 : :
6461 : : // Get the backend representation.
6462 : :
6463 : : Bfunction*
6464 : 1524832 : Function_declaration::get_or_make_decl(Gogo* gogo, Named_object* no)
6465 : : {
6466 : 1524832 : if (this->fndecl_ == NULL)
6467 : : {
6468 : 282863 : unsigned int flags =
6469 : : (Backend::function_is_visible
6470 : : | Backend::function_is_declaration
6471 : : | Backend::function_is_inlinable);
6472 : :
6473 : : // Let Go code use an asm declaration to pick up a builtin
6474 : : // function.
6475 : 282863 : if (!this->asm_name_.empty())
6476 : : {
6477 : 130042 : Bfunction* builtin_decl =
6478 : 130042 : gogo->backend()->lookup_builtin(this->asm_name_);
6479 : 130042 : if (builtin_decl != NULL)
6480 : : {
6481 : 5833 : this->fndecl_ = builtin_decl;
6482 : 5833 : return this->fndecl_;
6483 : : }
6484 : :
6485 : 124209 : if (this->asm_name_ == "runtime.gopanic"
6486 : 122059 : || this->asm_name_.compare(0, 13, "runtime.panic") == 0
6487 : 116956 : || this->asm_name_.compare(0, 15, "runtime.goPanic") == 0
6488 : 232349 : || this->asm_name_ == "runtime.block")
6489 : : flags |= Backend::function_does_not_return;
6490 : : }
6491 : :
6492 : 277030 : Btype* functype = this->fntype_->get_backend_fntype(gogo);
6493 : :
6494 : 277030 : Backend_name bname;
6495 : 277030 : this->backend_name(gogo, no, &bname);
6496 : :
6497 : 554060 : this->fndecl_ = gogo->backend()->function(functype,
6498 : 554060 : bname.name(),
6499 : 277030 : bname.optional_asm_name(),
6500 : : flags,
6501 : : this->location());
6502 : 277030 : }
6503 : :
6504 : 1518999 : return this->fndecl_;
6505 : : }
6506 : :
6507 : : // Build the descriptor for a function declaration. This won't
6508 : : // necessarily happen if the package has just a declaration for the
6509 : : // function and no other reference to it, but we may still need the
6510 : : // descriptor for references from other packages.
6511 : : void
6512 : 3904 : Function_declaration::build_backend_descriptor(Gogo* gogo)
6513 : : {
6514 : 3904 : if (this->descriptor_ != NULL)
6515 : : {
6516 : 650 : Translate_context context(gogo, NULL, NULL, NULL);
6517 : 650 : this->descriptor_->get_backend(&context);
6518 : : }
6519 : 3904 : }
6520 : :
6521 : : // Check that the types used in this declaration's signature are defined.
6522 : : // Reports errors for any undefined type.
6523 : :
6524 : : void
6525 : 3904 : Function_declaration::check_types() const
6526 : : {
6527 : : // Calling Type::base will give errors for any undefined types.
6528 : 3904 : Function_type* fntype = this->type();
6529 : 3904 : if (fntype->receiver() != NULL)
6530 : 0 : fntype->receiver()->type()->base();
6531 : 3904 : if (fntype->parameters() != NULL)
6532 : : {
6533 : 3035 : const Typed_identifier_list* params = fntype->parameters();
6534 : 9609 : for (Typed_identifier_list::const_iterator p = params->begin();
6535 : 9609 : p != params->end();
6536 : 6574 : ++p)
6537 : 6574 : p->type()->base();
6538 : : }
6539 : 3904 : }
6540 : :
6541 : : // Return the function's decl after it has been built.
6542 : :
6543 : : Bfunction*
6544 : 9317224 : Function::get_decl() const
6545 : : {
6546 : 9317224 : go_assert(this->fndecl_ != NULL);
6547 : 9317224 : return this->fndecl_;
6548 : : }
6549 : :
6550 : : // Build the backend representation for the function code.
6551 : :
6552 : : void
6553 : 295289 : Function::build(Gogo* gogo, Named_object* named_function)
6554 : : {
6555 : 295289 : Translate_context context(gogo, named_function, NULL, NULL);
6556 : :
6557 : : // A list of parameter variables for this function.
6558 : 295289 : std::vector<Bvariable*> param_vars;
6559 : :
6560 : : // Variables that need to be declared for this function and their
6561 : : // initial values.
6562 : 295289 : std::vector<Bvariable*> vars;
6563 : 295289 : std::vector<Expression*> var_inits;
6564 : 295289 : std::vector<Statement*> var_decls_stmts;
6565 : 1215931 : for (Bindings::const_definitions_iterator p =
6566 : 295289 : this->block_->bindings()->begin_definitions();
6567 : 1215931 : p != this->block_->bindings()->end_definitions();
6568 : 920642 : ++p)
6569 : : {
6570 : 920642 : Location loc = (*p)->location();
6571 : 920642 : if ((*p)->is_variable() && (*p)->var_value()->is_parameter())
6572 : : {
6573 : 473753 : Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
6574 : 473753 : Bvariable* parm_bvar = bvar;
6575 : :
6576 : : // We always pass the receiver to a method as a pointer. If
6577 : : // the receiver is declared as a non-pointer type, then we
6578 : : // copy the value into a local variable. For direct interface
6579 : : // type we pack the pointer into the type.
6580 : 473753 : if ((*p)->var_value()->is_receiver()
6581 : 473753 : && (*p)->var_value()->type()->points_to() == NULL)
6582 : : {
6583 : 15151 : std::string name = (*p)->name() + ".pointer";
6584 : 15151 : Type* var_type = (*p)->var_value()->type();
6585 : 15151 : Variable* parm_var =
6586 : 15151 : new Variable(Type::make_pointer_type(var_type), NULL, false,
6587 : 30302 : true, false, loc);
6588 : 15151 : Named_object* parm_no =
6589 : 15151 : Named_object::make_variable(name, NULL, parm_var);
6590 : 15151 : parm_bvar = parm_no->get_backend_variable(gogo, named_function);
6591 : :
6592 : 15151 : vars.push_back(bvar);
6593 : :
6594 : 15151 : Expression* parm_ref =
6595 : 15151 : Expression::make_var_reference(parm_no, loc);
6596 : 15151 : Type* recv_type = (*p)->var_value()->type();
6597 : 15151 : if (recv_type->is_direct_iface_type())
6598 : 2016 : parm_ref = Expression::pack_direct_iface(recv_type, parm_ref, loc);
6599 : : else
6600 : 13135 : parm_ref =
6601 : 13135 : Expression::make_dereference(parm_ref,
6602 : : Expression::NIL_CHECK_NEEDED,
6603 : : loc);
6604 : 15151 : if ((*p)->var_value()->is_in_heap())
6605 : 1222 : parm_ref = Expression::make_heap_expression(parm_ref, loc);
6606 : 15151 : var_inits.push_back(parm_ref);
6607 : 15151 : }
6608 : 458602 : else if ((*p)->var_value()->is_in_heap())
6609 : : {
6610 : : // If we take the address of a parameter, then we need
6611 : : // to copy it into the heap.
6612 : 3001 : std::string parm_name = (*p)->name() + ".param";
6613 : 6002 : Variable* parm_var = new Variable((*p)->var_value()->type(), NULL,
6614 : 3001 : false, true, false, loc);
6615 : 3001 : Named_object* parm_no =
6616 : 3001 : Named_object::make_variable(parm_name, NULL, parm_var);
6617 : 3001 : parm_bvar = parm_no->get_backend_variable(gogo, named_function);
6618 : :
6619 : 3001 : vars.push_back(bvar);
6620 : 3001 : Expression* var_ref =
6621 : 3001 : Expression::make_var_reference(parm_no, loc);
6622 : 3001 : var_ref = Expression::make_heap_expression(var_ref, loc);
6623 : 3001 : var_inits.push_back(var_ref);
6624 : 3001 : }
6625 : 473753 : param_vars.push_back(parm_bvar);
6626 : : }
6627 : 446889 : else if ((*p)->is_result_variable())
6628 : : {
6629 : 244538 : Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
6630 : :
6631 : 244538 : Type* type = (*p)->result_var_value()->type();
6632 : 244538 : Expression* init;
6633 : 244538 : if (!(*p)->result_var_value()->is_in_heap())
6634 : : {
6635 : 244416 : Btype* btype = type->get_backend(gogo);
6636 : 244416 : Bexpression* binit = gogo->backend()->zero_expression(btype);
6637 : 244416 : init = Expression::make_backend(binit, type, loc);
6638 : : }
6639 : : else
6640 : 122 : init = Expression::make_allocation(type, loc);
6641 : :
6642 : 244538 : vars.push_back(bvar);
6643 : 244538 : var_inits.push_back(init);
6644 : : }
6645 : 202351 : else if (this->defer_stack_ != NULL
6646 : 23528 : && (*p)->is_variable()
6647 : 22838 : && (*p)->var_value()->is_non_escaping_address_taken()
6648 : 946670 : && !(*p)->var_value()->is_in_heap())
6649 : : {
6650 : : // Local variable captured by deferred closure needs to be live
6651 : : // until the end of the function. We create a top-level
6652 : : // declaration for it.
6653 : : // TODO: we don't need to do this if the variable is not captured
6654 : : // by the defer closure. There is no easy way to check it here,
6655 : : // so we do this for all address-taken variables for now.
6656 : 1585 : Variable* var = (*p)->var_value();
6657 : 1585 : Temporary_statement* ts =
6658 : 1585 : Statement::make_temporary(var->type(), NULL, var->location());
6659 : 1585 : ts->set_is_address_taken();
6660 : 1585 : var->set_toplevel_decl(ts);
6661 : 1585 : var_decls_stmts.push_back(ts);
6662 : : }
6663 : : }
6664 : 295289 : if (!gogo->backend()->function_set_parameters(this->fndecl_, param_vars))
6665 : : {
6666 : 14 : go_assert(saw_errors());
6667 : : return;
6668 : : }
6669 : :
6670 : : // If we need a closure variable, make sure to create it.
6671 : : // It gets installed in the function as a side effect of creation.
6672 : 295275 : if (this->closure_var_ != NULL)
6673 : : {
6674 : 14291 : go_assert(this->closure_var_->var_value()->is_closure());
6675 : 14291 : this->closure_var_->get_backend_variable(gogo, named_function);
6676 : : }
6677 : :
6678 : 295275 : if (this->block_ != NULL)
6679 : : {
6680 : : // Declare variables if necessary.
6681 : 295275 : Bblock* var_decls = NULL;
6682 : 295275 : std::vector<Bstatement*> var_decls_bstmt_list;
6683 : 295275 : Bstatement* defer_init = NULL;
6684 : 295275 : if (!vars.empty() || this->defer_stack_ != NULL)
6685 : : {
6686 : 224695 : var_decls =
6687 : 449390 : gogo->backend()->block(this->fndecl_, NULL, vars,
6688 : 224695 : this->block_->start_location(),
6689 : 224695 : this->block_->end_location());
6690 : :
6691 : 224695 : if (this->defer_stack_ != NULL)
6692 : : {
6693 : 8353 : Translate_context dcontext(gogo, named_function, this->block_,
6694 : 8353 : var_decls);
6695 : 8353 : defer_init = this->defer_stack_->get_backend(&dcontext);
6696 : 8353 : var_decls_bstmt_list.push_back(defer_init);
6697 : 9938 : for (std::vector<Statement*>::iterator p = var_decls_stmts.begin();
6698 : 9938 : p != var_decls_stmts.end();
6699 : 1585 : ++p)
6700 : : {
6701 : 1585 : Bstatement* bstmt = (*p)->get_backend(&dcontext);
6702 : 1585 : var_decls_bstmt_list.push_back(bstmt);
6703 : : }
6704 : : }
6705 : : }
6706 : :
6707 : : // Build the backend representation for all the statements in the
6708 : : // function.
6709 : 295275 : Translate_context bcontext(gogo, named_function, NULL, NULL);
6710 : 295275 : Bblock* code_block = this->block_->get_backend(&bcontext);
6711 : :
6712 : : // Initialize variables if necessary.
6713 : 295275 : Translate_context icontext(gogo, named_function, this->block_,
6714 : 295275 : var_decls);
6715 : 295275 : std::vector<Bstatement*> init;
6716 : 295275 : go_assert(vars.size() == var_inits.size());
6717 : 557955 : for (size_t i = 0; i < vars.size(); ++i)
6718 : : {
6719 : 262680 : Bexpression* binit = var_inits[i]->get_backend(&icontext);
6720 : 262680 : Bstatement* init_stmt =
6721 : 262680 : gogo->backend()->init_statement(this->fndecl_, vars[i],
6722 : 262680 : binit);
6723 : 262680 : init.push_back(init_stmt);
6724 : : }
6725 : 295275 : Bstatement* var_init = gogo->backend()->statement_list(init);
6726 : :
6727 : : // Initialize all variables before executing this code block.
6728 : 295275 : Bstatement* code_stmt = gogo->backend()->block_statement(code_block);
6729 : 295275 : code_stmt = gogo->backend()->compound_statement(var_init, code_stmt);
6730 : :
6731 : : // If we have a defer stack, initialize it at the start of a
6732 : : // function.
6733 : 295275 : Bstatement* except = NULL;
6734 : 295275 : Bstatement* fini = NULL;
6735 : 295275 : if (defer_init != NULL)
6736 : : {
6737 : : // Clean up the defer stack when we leave the function.
6738 : 8353 : this->build_defer_wrapper(gogo, named_function, &except, &fini);
6739 : :
6740 : : // Wrap the code for this function in an exception handler to handle
6741 : : // defer calls.
6742 : 8353 : code_stmt =
6743 : 8353 : gogo->backend()->exception_handler_statement(code_stmt,
6744 : : except, fini,
6745 : : this->location_);
6746 : : }
6747 : :
6748 : : // Stick the code into the block we built for the receiver, if
6749 : : // we built one.
6750 : 295275 : if (var_decls != NULL)
6751 : : {
6752 : 224695 : var_decls_bstmt_list.push_back(code_stmt);
6753 : 224695 : gogo->backend()->block_add_statements(var_decls, var_decls_bstmt_list);
6754 : 224695 : code_stmt = gogo->backend()->block_statement(var_decls);
6755 : : }
6756 : :
6757 : 295275 : if (!gogo->backend()->function_set_body(this->fndecl_, code_stmt))
6758 : : {
6759 : 0 : go_assert(saw_errors());
6760 : 0 : return;
6761 : : }
6762 : 295275 : }
6763 : :
6764 : : // If we created a descriptor for the function, make sure we emit it.
6765 : 295275 : if (this->descriptor_ != NULL)
6766 : : {
6767 : 154436 : Translate_context dcontext(gogo, NULL, NULL, NULL);
6768 : 154436 : this->descriptor_->get_backend(&dcontext);
6769 : : }
6770 : 295289 : }
6771 : :
6772 : : // Build the wrappers around function code needed if the function has
6773 : : // any defer statements. This sets *EXCEPT to an exception handler
6774 : : // and *FINI to a finally handler.
6775 : :
6776 : : void
6777 : 8353 : Function::build_defer_wrapper(Gogo* gogo, Named_object* named_function,
6778 : : Bstatement** except, Bstatement** fini)
6779 : : {
6780 : 8353 : Location end_loc = this->block_->end_location();
6781 : :
6782 : : // Add an exception handler. This is used if a panic occurs. Its
6783 : : // purpose is to stop the stack unwinding if a deferred function
6784 : : // calls recover. There are more details in
6785 : : // libgo/runtime/go-unwind.c.
6786 : :
6787 : 8353 : std::vector<Bstatement*> stmts;
6788 : 8353 : Expression* call = Runtime::make_call(gogo, Runtime::CHECKDEFER, end_loc, 1,
6789 : : this->defer_stack(end_loc));
6790 : 8353 : Translate_context context(gogo, named_function, NULL, NULL);
6791 : 8353 : Bexpression* defer = call->get_backend(&context);
6792 : 8353 : stmts.push_back(gogo->backend()->expression_statement(this->fndecl_, defer));
6793 : :
6794 : 8353 : Bstatement* ret_bstmt = this->return_value(gogo, named_function, end_loc);
6795 : 8353 : if (ret_bstmt != NULL)
6796 : 3600 : stmts.push_back(ret_bstmt);
6797 : :
6798 : 8353 : go_assert(*except == NULL);
6799 : 8353 : *except = gogo->backend()->statement_list(stmts);
6800 : :
6801 : 8353 : call = Runtime::make_call(gogo, Runtime::CHECKDEFER, end_loc, 1,
6802 : : this->defer_stack(end_loc));
6803 : 8353 : defer = call->get_backend(&context);
6804 : :
6805 : 8353 : call = Runtime::make_call(gogo, Runtime::DEFERRETURN, end_loc, 1,
6806 : : this->defer_stack(end_loc));
6807 : 8353 : Bexpression* undefer = call->get_backend(&context);
6808 : 8353 : Bstatement* function_defer =
6809 : 8353 : gogo->backend()->function_defer_statement(this->fndecl_, undefer, defer,
6810 : 8353 : end_loc);
6811 : 8353 : stmts = std::vector<Bstatement*>(1, function_defer);
6812 : 8353 : if (this->type_->results() != NULL
6813 : 3600 : && !this->type_->results()->empty()
6814 : 11953 : && !this->type_->results()->front().name().empty())
6815 : : {
6816 : : // If the result variables are named, and we are returning from
6817 : : // this function rather than panicing through it, we need to
6818 : : // return them again, because they might have been changed by a
6819 : : // defer function. The runtime routines set the defer_stack
6820 : : // variable to true if we are returning from this function.
6821 : :
6822 : 1020 : ret_bstmt = this->return_value(gogo, named_function, end_loc);
6823 : 1020 : Bexpression* nil = Expression::make_nil(end_loc)->get_backend(&context);
6824 : 1020 : Bexpression* ret =
6825 : 1020 : gogo->backend()->compound_expression(ret_bstmt, nil, end_loc);
6826 : 1020 : Expression* ref =
6827 : 1020 : Expression::make_temporary_reference(this->defer_stack_, end_loc);
6828 : 1020 : Bexpression* bref = ref->get_backend(&context);
6829 : 1020 : ret = gogo->backend()->conditional_expression(this->fndecl_,
6830 : : NULL, bref, ret, NULL,
6831 : : end_loc);
6832 : 1020 : stmts.push_back(gogo->backend()->expression_statement(this->fndecl_, ret));
6833 : : }
6834 : :
6835 : 8353 : go_assert(*fini == NULL);
6836 : 8353 : *fini = gogo->backend()->statement_list(stmts);
6837 : 8353 : }
6838 : :
6839 : : // Return the statement that assigns values to this function's result struct.
6840 : :
6841 : : Bstatement*
6842 : 9373 : Function::return_value(Gogo* gogo, Named_object* named_function,
6843 : : Location location) const
6844 : : {
6845 : 9373 : const Typed_identifier_list* results = this->type_->results();
6846 : 9373 : if (results == NULL || results->empty())
6847 : : return NULL;
6848 : :
6849 : 4620 : go_assert(this->results_ != NULL);
6850 : 4620 : if (this->results_->size() != results->size())
6851 : : {
6852 : 0 : go_assert(saw_errors());
6853 : 0 : return gogo->backend()->error_statement();
6854 : : }
6855 : :
6856 : 4620 : std::vector<Bexpression*> vals(results->size());
6857 : 11463 : for (size_t i = 0; i < vals.size(); ++i)
6858 : : {
6859 : 6843 : Named_object* no = (*this->results_)[i];
6860 : 6843 : Bvariable* bvar = no->get_backend_variable(gogo, named_function);
6861 : 6843 : Bexpression* val = gogo->backend()->var_expression(bvar, location);
6862 : 6843 : if (no->result_var_value()->is_in_heap())
6863 : : {
6864 : 112 : Btype* bt = no->result_var_value()->type()->get_backend(gogo);
6865 : 112 : val = gogo->backend()->indirect_expression(bt, val, true, location);
6866 : : }
6867 : 6843 : vals[i] = val;
6868 : : }
6869 : 4620 : return gogo->backend()->return_statement(this->fndecl_, vals, location);
6870 : 4620 : }
6871 : :
6872 : : // Class Block.
6873 : :
6874 : 2714869 : Block::Block(Block* enclosing, Location location)
6875 : 2714869 : : enclosing_(enclosing), statements_(),
6876 : 2714869 : bindings_(new Bindings(enclosing == NULL
6877 : : ? NULL
6878 : 2714869 : : enclosing->bindings())),
6879 : 2714869 : start_location_(location),
6880 : 2714869 : end_location_(Linemap::unknown_location())
6881 : : {
6882 : 2714869 : }
6883 : :
6884 : : // Add a statement to a block.
6885 : :
6886 : : void
6887 : 5238979 : Block::add_statement(Statement* statement)
6888 : : {
6889 : 5238979 : this->statements_.push_back(statement);
6890 : 5238979 : }
6891 : :
6892 : : // Add a statement to the front of a block. This is slow but is only
6893 : : // used for reference counts of parameters.
6894 : :
6895 : : void
6896 : 4935 : Block::add_statement_at_front(Statement* statement)
6897 : : {
6898 : 4935 : this->statements_.insert(this->statements_.begin(), statement);
6899 : 4935 : }
6900 : :
6901 : : // Replace a statement in a block.
6902 : :
6903 : : void
6904 : 747543 : Block::replace_statement(size_t index, Statement* s)
6905 : : {
6906 : 747543 : go_assert(index < this->statements_.size());
6907 : 747543 : this->statements_[index] = s;
6908 : 747543 : }
6909 : :
6910 : : // Add a statement before another statement.
6911 : :
6912 : : void
6913 : 1792507 : Block::insert_statement_before(size_t index, Statement* s)
6914 : : {
6915 : 1792507 : go_assert(index < this->statements_.size());
6916 : 1792507 : this->statements_.insert(this->statements_.begin() + index, s);
6917 : 1792507 : }
6918 : :
6919 : : // Add a statement after another statement.
6920 : :
6921 : : void
6922 : 0 : Block::insert_statement_after(size_t index, Statement* s)
6923 : : {
6924 : 0 : go_assert(index < this->statements_.size());
6925 : 0 : this->statements_.insert(this->statements_.begin() + index + 1, s);
6926 : 0 : }
6927 : :
6928 : : // Traverse the tree.
6929 : :
6930 : : int
6931 : 43288842 : Block::traverse(Traverse* traverse)
6932 : : {
6933 : 43288842 : unsigned int traverse_mask = traverse->traverse_mask();
6934 : :
6935 : 43288842 : if ((traverse_mask & Traverse::traverse_blocks) != 0)
6936 : : {
6937 : 8957119 : int t = traverse->block(this);
6938 : 8957119 : if (t == TRAVERSE_EXIT)
6939 : : return TRAVERSE_EXIT;
6940 : 8957119 : else if (t == TRAVERSE_SKIP_COMPONENTS)
6941 : : return TRAVERSE_CONTINUE;
6942 : : }
6943 : :
6944 : 39909171 : if ((traverse_mask
6945 : 39909171 : & (Traverse::traverse_variables
6946 : : | Traverse::traverse_constants
6947 : : | Traverse::traverse_expressions
6948 : : | Traverse::traverse_types)) != 0)
6949 : : {
6950 : 22167817 : for (Bindings::const_definitions_iterator pb =
6951 : 34993666 : this->bindings_->begin_definitions();
6952 : 57161483 : pb != this->bindings_->end_definitions();
6953 : 22167817 : ++pb)
6954 : : {
6955 : 22767629 : if ((*pb)->traverse(traverse, false) == TRAVERSE_EXIT)
6956 : 3016283 : return TRAVERSE_EXIT;
6957 : : }
6958 : : }
6959 : :
6960 : : // No point in checking traverse_mask here--if we got here we always
6961 : : // want to walk the statements. The traversal can insert new
6962 : : // statements before or after the current statement. Inserting
6963 : : // statements before the current statement requires updating I via
6964 : : // the pointer; those statements will not be traversed. Any new
6965 : : // statements inserted after the current statement will be traversed
6966 : : // in their turn.
6967 : 130605669 : for (size_t i = 0; i < this->statements_.size(); ++i)
6968 : : {
6969 : 93712781 : if (this->statements_[i]->traverse(this, &i, traverse) == TRAVERSE_EXIT)
6970 : 2416471 : return TRAVERSE_EXIT;
6971 : : }
6972 : :
6973 : 36892888 : return TRAVERSE_CONTINUE;
6974 : : }
6975 : :
6976 : : // Work out types for unspecified variables and constants.
6977 : :
6978 : : void
6979 : 2102344 : Block::determine_types(Gogo* gogo)
6980 : : {
6981 : 842539 : for (Bindings::const_definitions_iterator pb =
6982 : 2102344 : this->bindings_->begin_definitions();
6983 : 2944883 : pb != this->bindings_->end_definitions();
6984 : 842539 : ++pb)
6985 : : {
6986 : 842539 : if ((*pb)->is_variable())
6987 : 692845 : (*pb)->var_value()->determine_type(gogo);
6988 : 149694 : else if ((*pb)->is_const())
6989 : 5531 : (*pb)->const_value()->determine_type(gogo);
6990 : : }
6991 : :
6992 : 5568893 : for (std::vector<Statement*>::const_iterator ps = this->statements_.begin();
6993 : 5568893 : ps != this->statements_.end();
6994 : 3466549 : ++ps)
6995 : 3466549 : (*ps)->determine_types(gogo);
6996 : 2102344 : }
6997 : :
6998 : : // Return true if the statements in this block may fall through.
6999 : :
7000 : : bool
7001 : 113776 : Block::may_fall_through() const
7002 : : {
7003 : 113776 : if (this->statements_.empty())
7004 : : return true;
7005 : 113733 : return this->statements_.back()->may_fall_through();
7006 : : }
7007 : :
7008 : : // Write export data for a block.
7009 : :
7010 : : void
7011 : 44121 : Block::export_block(Export_function_body* efb)
7012 : : {
7013 : 122746 : for (Block::iterator p = this->begin();
7014 : 122746 : p != this->end();
7015 : 78625 : ++p)
7016 : : {
7017 : 78625 : efb->indent();
7018 : :
7019 : 78625 : efb->increment_indent();
7020 : 78625 : (*p)->export_statement(efb);
7021 : 78625 : efb->decrement_indent();
7022 : :
7023 : 78625 : Location loc = (*p)->location();
7024 : 78625 : if ((*p)->is_block_statement())
7025 : : {
7026 : : // For a block we put the start location on the first brace
7027 : : // in Block_statement::do_export_statement. Here we put the
7028 : : // end location on the final brace.
7029 : 22816 : loc = (*p)->block_statement()->block()->end_location();
7030 : : }
7031 : 78625 : char buf[50];
7032 : 78625 : snprintf(buf, sizeof buf, " //%d\n", Linemap::location_to_line(loc));
7033 : 78625 : efb->write_c_string(buf);
7034 : : }
7035 : 44121 : }
7036 : :
7037 : : // Add exported block data to SET, reading from BODY starting at OFF.
7038 : : // Returns whether the import succeeded.
7039 : :
7040 : : bool
7041 : 38869 : Block::import_block(Block* set, Import_function_body *ifb, Location loc)
7042 : : {
7043 : 38869 : Location eloc = ifb->location();
7044 : 38869 : Location sloc = loc;
7045 : 38869 : const std::string& body(ifb->body());
7046 : 38869 : size_t off = ifb->off();
7047 : 108195 : while (off < body.length())
7048 : : {
7049 : 95948 : int indent = ifb->indent();
7050 : 95948 : if (off + indent >= body.length())
7051 : : {
7052 : 0 : go_error_at(eloc,
7053 : : "invalid export data for %qs: insufficient indentation",
7054 : 0 : ifb->name().c_str());
7055 : 0 : return false;
7056 : : }
7057 : 245244 : for (int i = 0; i < indent - 1; i++)
7058 : : {
7059 : 149296 : if (body[off + i] != ' ')
7060 : : {
7061 : 0 : go_error_at(eloc,
7062 : : "invalid export data for %qs: bad indentation",
7063 : 0 : ifb->name().c_str());
7064 : 0 : return false;
7065 : : }
7066 : : }
7067 : :
7068 : 95948 : bool at_end = false;
7069 : 95948 : if (body[off + indent - 1] == '}')
7070 : : at_end = true;
7071 : 69326 : else if (body[off + indent - 1] != ' ')
7072 : : {
7073 : 0 : go_error_at(eloc,
7074 : : "invalid export data for %qs: bad indentation",
7075 : 0 : ifb->name().c_str());
7076 : 0 : return false;
7077 : : }
7078 : :
7079 : 95948 : off += indent;
7080 : :
7081 : 95948 : size_t nl = body.find('\n', off);
7082 : 95948 : if (nl == std::string::npos)
7083 : : {
7084 : 0 : go_error_at(eloc, "invalid export data for %qs: missing newline",
7085 : 0 : ifb->name().c_str());
7086 : 0 : return false;
7087 : : }
7088 : :
7089 : 95948 : size_t lineno_pos = body.find(" //", off);
7090 : 95948 : if (lineno_pos == std::string::npos || lineno_pos >= nl)
7091 : : {
7092 : 0 : go_error_at(eloc, "invalid export data for %qs: missing line number",
7093 : 0 : ifb->name().c_str());
7094 : 0 : return false;
7095 : : }
7096 : :
7097 : 95948 : unsigned int lineno = 0;
7098 : 333045 : for (size_t i = lineno_pos + 3; i < nl; ++i)
7099 : : {
7100 : 237097 : char c = body[i];
7101 : 237097 : if (c < '0' || c > '9')
7102 : : {
7103 : 0 : go_error_at(loc,
7104 : : "invalid export data for %qs: invalid line number",
7105 : 0 : ifb->name().c_str());
7106 : 0 : return false;
7107 : : }
7108 : 237097 : lineno = lineno * 10 + c - '0';
7109 : : }
7110 : :
7111 : 95948 : ifb->gogo()->linemap()->start_line(lineno, 1);
7112 : 95948 : sloc = ifb->gogo()->linemap()->get_location(0);
7113 : :
7114 : 95948 : if (at_end)
7115 : : {
7116 : : // An if statement can have an "else" following the "}", in
7117 : : // which case we want to leave the offset where it is, just
7118 : : // after the "}". We don't get the block ending location
7119 : : // quite right for if statements.
7120 : 26622 : if (body.compare(off, 6, " else ") != 0)
7121 : 26486 : off = nl + 1;
7122 : : break;
7123 : : }
7124 : :
7125 : 69326 : ifb->set_off(off);
7126 : 69326 : Statement* s = Statement::import_statement(ifb, sloc);
7127 : 69326 : if (s == NULL)
7128 : : return false;
7129 : :
7130 : 69326 : set->add_statement(s);
7131 : :
7132 : 69326 : size_t at = ifb->off();
7133 : 69326 : if (at < nl + 1)
7134 : : off = nl + 1;
7135 : : else
7136 : : off = at;
7137 : : }
7138 : :
7139 : 38869 : ifb->set_off(off);
7140 : 38869 : set->set_end_location(sloc);
7141 : 38869 : return true;
7142 : : }
7143 : :
7144 : : // Convert a block to the backend representation.
7145 : :
7146 : : Bblock*
7147 : 2630901 : Block::get_backend(Translate_context* context)
7148 : : {
7149 : 2630901 : Gogo* gogo = context->gogo();
7150 : 2630901 : Named_object* function = context->function();
7151 : 2630901 : std::vector<Bvariable*> vars;
7152 : 2630901 : vars.reserve(this->bindings_->size_definitions());
7153 : 1107460 : for (Bindings::const_definitions_iterator pv =
7154 : 2630901 : this->bindings_->begin_definitions();
7155 : 3738361 : pv != this->bindings_->end_definitions();
7156 : 1107460 : ++pv)
7157 : : {
7158 : 1107460 : if ((*pv)->is_variable() && !(*pv)->var_value()->is_parameter())
7159 : 382508 : vars.push_back((*pv)->get_backend_variable(gogo, function));
7160 : : }
7161 : :
7162 : 2630901 : go_assert(function != NULL);
7163 : 2630901 : Bfunction* bfunction =
7164 : 2630901 : function->func_value()->get_or_make_decl(gogo, function);
7165 : 2630901 : Bblock* ret = context->backend()->block(bfunction, context->bblock(),
7166 : : vars, this->start_location_,
7167 : : this->end_location_);
7168 : :
7169 : 2630901 : Translate_context subcontext(gogo, function, this, ret);
7170 : 2630901 : std::vector<Bstatement*> bstatements;
7171 : 2630901 : bstatements.reserve(this->statements_.size());
7172 : 2630901 : for (std::vector<Statement*>::const_iterator p = this->statements_.begin();
7173 : 9646131 : p != this->statements_.end();
7174 : 7015230 : ++p)
7175 : 7015230 : bstatements.push_back((*p)->get_backend(&subcontext));
7176 : :
7177 : 2630901 : context->backend()->block_add_statements(ret, bstatements);
7178 : :
7179 : 5261802 : return ret;
7180 : 2630901 : }
7181 : :
7182 : : // Class Bindings_snapshot.
7183 : :
7184 : 12092 : Bindings_snapshot::Bindings_snapshot(const Block* b, Location location)
7185 : 12092 : : block_(b), counts_(), location_(location)
7186 : : {
7187 : 40730 : while (b != NULL)
7188 : : {
7189 : 28638 : this->counts_.push_back(b->bindings()->size_definitions());
7190 : 28638 : b = b->enclosing();
7191 : : }
7192 : 12092 : }
7193 : :
7194 : : // Report errors appropriate for a goto from B to this.
7195 : :
7196 : : void
7197 : 533 : Bindings_snapshot::check_goto_from(const Block* b, Location loc)
7198 : : {
7199 : 533 : size_t dummy;
7200 : 533 : if (!this->check_goto_block(loc, b, this->block_, &dummy))
7201 : 12 : return;
7202 : 521 : this->check_goto_defs(loc, this->block_,
7203 : 521 : this->block_->bindings()->size_definitions(),
7204 : 521 : this->counts_[0]);
7205 : : }
7206 : :
7207 : : // Report errors appropriate for a goto from this to B.
7208 : :
7209 : : void
7210 : 1565 : Bindings_snapshot::check_goto_to(const Block* b)
7211 : : {
7212 : 1565 : size_t index;
7213 : 1565 : if (!this->check_goto_block(this->location_, this->block_, b, &index))
7214 : 19 : return;
7215 : 1546 : this->check_goto_defs(this->location_, b, this->counts_[index],
7216 : : b->bindings()->size_definitions());
7217 : : }
7218 : :
7219 : : // Report errors appropriate for a goto at LOC from BFROM to BTO.
7220 : : // Return true if all is well, false if we reported an error. If this
7221 : : // returns true, it sets *PINDEX to the number of blocks BTO is above
7222 : : // BFROM.
7223 : :
7224 : : bool
7225 : 2098 : Bindings_snapshot::check_goto_block(Location loc, const Block* bfrom,
7226 : : const Block* bto, size_t* pindex)
7227 : : {
7228 : : // It is an error if BTO is not either BFROM or above BFROM.
7229 : 2098 : size_t index = 0;
7230 : 9845 : for (const Block* pb = bfrom; pb != bto; pb = pb->enclosing(), ++index)
7231 : : {
7232 : 7778 : if (pb == NULL)
7233 : : {
7234 : 31 : go_error_at(loc, "goto jumps into block");
7235 : 31 : go_inform(bto->start_location(), "goto target block starts here");
7236 : 31 : return false;
7237 : : }
7238 : : }
7239 : 2067 : *pindex = index;
7240 : 2067 : return true;
7241 : : }
7242 : :
7243 : : // Report errors appropriate for a goto at LOC ending at BLOCK, where
7244 : : // CFROM is the number of names defined at the point of the goto and
7245 : : // CTO is the number of names defined at the point of the label.
7246 : :
7247 : : void
7248 : 2067 : Bindings_snapshot::check_goto_defs(Location loc, const Block* block,
7249 : : size_t cfrom, size_t cto)
7250 : : {
7251 : 2067 : if (cfrom < cto)
7252 : : {
7253 : 14 : Bindings::const_definitions_iterator p =
7254 : 14 : block->bindings()->begin_definitions();
7255 : 31 : for (size_t i = 0; i < cfrom; ++i)
7256 : : {
7257 : 17 : go_assert(p != block->bindings()->end_definitions());
7258 : 17 : ++p;
7259 : : }
7260 : 14 : go_assert(p != block->bindings()->end_definitions());
7261 : :
7262 : 34 : for (; p != block->bindings()->end_definitions(); ++p)
7263 : : {
7264 : 20 : if ((*p)->is_variable())
7265 : : {
7266 : 6 : std::string n = (*p)->message_name();
7267 : 6 : go_error_at(loc, "goto jumps over declaration of %qs", n.c_str());
7268 : 6 : go_inform((*p)->location(), "%qs defined here", n.c_str());
7269 : 6 : }
7270 : : }
7271 : : }
7272 : 2067 : }
7273 : :
7274 : : // Class Function_declaration.
7275 : :
7276 : : // Whether this declares a method.
7277 : :
7278 : : bool
7279 : 2119202 : Function_declaration::is_method() const
7280 : : {
7281 : 2119202 : return this->fntype_->is_method();
7282 : : }
7283 : :
7284 : : // Whether this method should not be included in the type descriptor.
7285 : :
7286 : : bool
7287 : 1330916 : Function_declaration::nointerface() const
7288 : : {
7289 : 1330916 : go_assert(this->is_method());
7290 : 1330916 : return (this->pragmas_ & GOPRAGMA_NOINTERFACE) != 0;
7291 : : }
7292 : :
7293 : : // Record that this method should not be included in the type
7294 : : // descriptor.
7295 : :
7296 : : void
7297 : 2 : Function_declaration::set_nointerface()
7298 : : {
7299 : 2 : this->pragmas_ |= GOPRAGMA_NOINTERFACE;
7300 : 2 : }
7301 : :
7302 : : // Set the receiver type. This is used to remove aliases.
7303 : :
7304 : : void
7305 : 0 : Function_declaration::set_receiver_type(Type* rtype)
7306 : : {
7307 : 0 : Function_type* oft = this->fntype_;
7308 : 0 : Typed_identifier* rec = new Typed_identifier(oft->receiver()->name(),
7309 : : rtype,
7310 : 0 : oft->receiver()->location());
7311 : 0 : Typed_identifier_list* parameters = NULL;
7312 : 0 : if (oft->parameters() != NULL)
7313 : 0 : parameters = oft->parameters()->copy();
7314 : 0 : Typed_identifier_list* results = NULL;
7315 : 0 : if (oft->results() != NULL)
7316 : 0 : results = oft->results()->copy();
7317 : 0 : Function_type* nft = Type::make_function_type(rec, parameters, results,
7318 : : oft->location());
7319 : 0 : this->fntype_ = nft;
7320 : 0 : }
7321 : :
7322 : : // Import an inlinable function. This is used for an inlinable
7323 : : // function whose body is recorded in the export data. Parse the
7324 : : // export data into a Block and create a regular function using that
7325 : : // Block as its body. Redeclare this function declaration as the
7326 : : // function.
7327 : :
7328 : : void
7329 : 12247 : Function_declaration::import_function_body(Gogo* gogo, Named_object* no)
7330 : : {
7331 : 12247 : go_assert(no->func_declaration_value() == this);
7332 : 12247 : go_assert(no->package() != NULL);
7333 : 12247 : const std::string& body(this->imported_body_);
7334 : 12247 : go_assert(!body.empty());
7335 : :
7336 : : // Read the "//FILE:LINE" comment starts the export data.
7337 : :
7338 : 12247 : size_t indent = 1;
7339 : 12247 : if (this->is_method())
7340 : 593 : indent = 2;
7341 : 12247 : size_t i = 0;
7342 : 25087 : for (; i < indent; i++)
7343 : : {
7344 : 12840 : if (body.at(i) != ' ')
7345 : : {
7346 : 0 : go_error_at(this->location_,
7347 : : "invalid export body for %qs: bad initial indentation",
7348 : 0 : no->message_name().c_str());
7349 : 0 : return;
7350 : : }
7351 : : }
7352 : :
7353 : 12247 : if (body.substr(i, 2) != "//")
7354 : : {
7355 : 0 : go_error_at(this->location_,
7356 : : "invalid export body for %qs: missing file comment",
7357 : 0 : no->message_name().c_str());
7358 : 0 : return;
7359 : : }
7360 : :
7361 : 12247 : size_t colon = body.find(':', i + 2);
7362 : 12247 : size_t nl = body.find('\n', i + 2);
7363 : 12247 : if (nl == std::string::npos)
7364 : : {
7365 : 0 : go_error_at(this->location_,
7366 : : "invalid export body for %qs: missing file name",
7367 : 0 : no->message_name().c_str());
7368 : 0 : return;
7369 : : }
7370 : 12247 : if (colon == std::string::npos || nl < colon)
7371 : : {
7372 : 0 : go_error_at(this->location_,
7373 : : "invalid export body for %qs: missing initial line number",
7374 : 0 : no->message_name().c_str());
7375 : 0 : return;
7376 : : }
7377 : :
7378 : 12247 : std::string file = body.substr(i + 2, colon - (i + 2));
7379 : 12247 : std::string linestr = body.substr(colon + 1, nl - (colon + 1));
7380 : 12247 : char* end;
7381 : 12247 : long linenol = strtol(linestr.c_str(), &end, 10);
7382 : 12247 : if (*end != '\0')
7383 : : {
7384 : 0 : go_error_at(this->location_,
7385 : : "invalid export body for %qs: invalid initial line number",
7386 : 0 : no->message_name().c_str());
7387 : 0 : return;
7388 : : }
7389 : 12247 : unsigned int lineno = static_cast<unsigned int>(linenol);
7390 : :
7391 : : // Turn the file/line into a location.
7392 : :
7393 : 12247 : char* alc = new char[file.length() + 1];
7394 : 12247 : memcpy(alc, file.data(), file.length());
7395 : 12247 : alc[file.length()] = '\0';
7396 : 12247 : gogo->linemap()->start_file(alc, lineno);
7397 : 12247 : gogo->linemap()->start_line(lineno, 1);
7398 : 12247 : Location start_loc = gogo->linemap()->get_location(0);
7399 : :
7400 : : // Define the function with an outer block that declares the
7401 : : // parameters.
7402 : :
7403 : 12247 : Function_type* fntype = this->fntype_;
7404 : :
7405 : 12247 : Block* outer = new Block(NULL, start_loc);
7406 : :
7407 : 12247 : Function* fn = new Function(fntype, NULL, outer, start_loc);
7408 : 12247 : fn->set_is_inline_only();
7409 : :
7410 : 12247 : if (fntype->is_method())
7411 : : {
7412 : 593 : if (this->nointerface())
7413 : 1 : fn->set_nointerface();
7414 : 593 : const Typed_identifier* receiver = fntype->receiver();
7415 : 593 : Variable* recv_param = new Variable(receiver->type(), NULL, false,
7416 : 593 : true, true, start_loc);
7417 : :
7418 : 593 : std::string rname = receiver->name();
7419 : 593 : unsigned rcounter = 0;
7420 : :
7421 : : // We need to give a nameless receiver a name to avoid having it
7422 : : // clash with some other nameless param. FIXME.
7423 : 593 : Gogo::rename_if_empty(&rname, "r", &rcounter);
7424 : :
7425 : 593 : outer->bindings()->add_variable(rname, NULL, recv_param);
7426 : 593 : }
7427 : :
7428 : 12247 : const Typed_identifier_list* params = fntype->parameters();
7429 : 12247 : bool is_varargs = fntype->is_varargs();
7430 : 12247 : unsigned pcounter = 0;
7431 : 12247 : if (params != NULL)
7432 : : {
7433 : 10560 : for (Typed_identifier_list::const_iterator p = params->begin();
7434 : 27541 : p != params->end();
7435 : 16981 : ++p)
7436 : : {
7437 : 16981 : Variable* param = new Variable(p->type(), NULL, false, true, false,
7438 : 16981 : start_loc);
7439 : 16981 : if (is_varargs && p + 1 == params->end())
7440 : 581 : param->set_is_varargs_parameter();
7441 : :
7442 : 16981 : std::string pname = p->name();
7443 : :
7444 : : // We need to give each nameless parameter a non-empty name to avoid
7445 : : // having it clash with some other nameless param. FIXME.
7446 : 16981 : Gogo::rename_if_empty(&pname, "p", &pcounter);
7447 : :
7448 : 16981 : outer->bindings()->add_variable(pname, NULL, param);
7449 : 16981 : }
7450 : : }
7451 : :
7452 : 12247 : fn->create_result_variables(gogo);
7453 : :
7454 : 12247 : if (!fntype->is_method())
7455 : : {
7456 : 11654 : const Package* package = no->package();
7457 : 11654 : no = package->bindings()->add_function(no->name(), package, fn);
7458 : : }
7459 : : else
7460 : : {
7461 : 1186 : Named_type* rtype = fntype->receiver()->type()->deref()->named_type();
7462 : 593 : go_assert(rtype != NULL);
7463 : 593 : no = rtype->add_method(no->name(), fn);
7464 : 593 : const Package* package = rtype->named_object()->package();
7465 : 593 : package->bindings()->add_method(no);
7466 : : }
7467 : :
7468 : 12247 : Import_function_body ifb(gogo, this->imp_, no, body, nl + 1, outer, indent);
7469 : :
7470 : 12247 : if (!Block::import_block(outer, &ifb, start_loc))
7471 : 0 : return;
7472 : :
7473 : 12247 : outer->determine_types(gogo);
7474 : 12247 : gogo->lower_block(no, outer);
7475 : :
7476 : 12247 : gogo->add_imported_inline_function(no);
7477 : 12247 : }
7478 : :
7479 : : // Return the function descriptor.
7480 : :
7481 : : Expression*
7482 : 120017 : Function_declaration::descriptor(Gogo*, Named_object* no)
7483 : : {
7484 : 120017 : go_assert(!this->fntype_->is_method());
7485 : 120017 : if (this->descriptor_ == NULL)
7486 : 105041 : this->descriptor_ = Expression::make_func_descriptor(no);
7487 : 120017 : return this->descriptor_;
7488 : : }
7489 : :
7490 : : // Class Variable.
7491 : :
7492 : 1742558 : Variable::Variable(Type* type, Expression* init, bool is_global,
7493 : : bool is_parameter, bool is_receiver,
7494 : 1742558 : Location location)
7495 : 1742558 : : type_(type), init_(init), preinit_(NULL), location_(location),
7496 : 1742558 : toplevel_decl_(NULL), init_refs_(NULL), embeds_(NULL), backend_(NULL),
7497 : 1742558 : is_global_(is_global), is_parameter_(is_parameter), is_closure_(false),
7498 : 1742558 : is_receiver_(is_receiver), is_varargs_parameter_(false),
7499 : 1742558 : is_global_sink_(false), is_used_(false), is_address_taken_(false),
7500 : 1742558 : is_non_escaping_address_taken_(false), seen_(false),
7501 : 1742558 : init_is_lowered_(false), init_is_flattened_(false),
7502 : 1742558 : type_from_init_tuple_(false), type_from_range_index_(false),
7503 : 1742558 : type_from_range_value_(false), type_from_chan_element_(false),
7504 : 1742558 : is_type_switch_var_(false), determined_type_(false),
7505 : 1742558 : in_unique_section_(false), is_referenced_by_inline_(false)
7506 : : {
7507 : 1742558 : go_assert(type != NULL || init != NULL);
7508 : 1742558 : go_assert(!is_parameter || init == NULL);
7509 : 1742558 : }
7510 : :
7511 : : // Traverse the initializer expression.
7512 : :
7513 : : int
7514 : 21642586 : Variable::traverse_expression(Traverse* traverse, unsigned int traverse_mask)
7515 : : {
7516 : 21642586 : if (this->preinit_ != NULL)
7517 : : {
7518 : 36167 : if (this->preinit_->traverse(traverse) == TRAVERSE_EXIT)
7519 : : return TRAVERSE_EXIT;
7520 : : }
7521 : 21642586 : if (this->init_ != NULL
7522 : 5848962 : && ((traverse_mask
7523 : 5848962 : & (Traverse::traverse_expressions | Traverse::traverse_types))
7524 : : != 0))
7525 : : {
7526 : 4978897 : if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT)
7527 : : return TRAVERSE_EXIT;
7528 : : }
7529 : : return TRAVERSE_CONTINUE;
7530 : : }
7531 : :
7532 : : // Lower the initialization expression after parsing is complete.
7533 : :
7534 : : void
7535 : 6554631 : Variable::lower_init_expression(Gogo* gogo, Named_object* function,
7536 : : Statement_inserter* inserter)
7537 : : {
7538 : 6554631 : Named_object* dep = gogo->var_depends_on(this);
7539 : 6554631 : if (dep != NULL && dep->is_variable())
7540 : 194 : dep->var_value()->lower_init_expression(gogo, function, inserter);
7541 : :
7542 : 6554631 : if (this->embeds_ != NULL)
7543 : : {
7544 : : // Now that we have seen any possible type aliases, convert the
7545 : : // go:embed directives into an initializer.
7546 : 22 : go_assert(this->init_ == NULL && this->type_ != NULL);
7547 : 22 : this->init_ = gogo->initializer_for_embeds(this->type_, this->embeds_,
7548 : : this->location_);
7549 : 22 : delete this->embeds_;
7550 : 22 : this->embeds_ = NULL;
7551 : : }
7552 : :
7553 : 6554631 : if (this->init_ != NULL && !this->init_is_lowered_)
7554 : : {
7555 : 302035 : if (this->seen_)
7556 : : {
7557 : : // We will give an error elsewhere, this is just to prevent
7558 : : // an infinite loop.
7559 : 0 : return;
7560 : : }
7561 : 302035 : this->seen_ = true;
7562 : :
7563 : 302035 : Statement_inserter global_inserter;
7564 : 302035 : if (this->is_global_)
7565 : : {
7566 : 20354 : global_inserter = Statement_inserter(gogo, this);
7567 : 20354 : inserter = &global_inserter;
7568 : : }
7569 : :
7570 : 302035 : gogo->lower_expression(function, inserter, &this->init_);
7571 : :
7572 : 302035 : this->seen_ = false;
7573 : :
7574 : 302035 : this->init_is_lowered_ = true;
7575 : : }
7576 : : }
7577 : :
7578 : : // Flatten the initialization expression after ordering evaluations.
7579 : :
7580 : : void
7581 : 751141 : Variable::flatten_init_expression(Gogo* gogo, Named_object* function,
7582 : : Statement_inserter* inserter)
7583 : : {
7584 : 751141 : Named_object* dep = gogo->var_depends_on(this);
7585 : 751141 : if (dep != NULL && dep->is_variable())
7586 : 99 : dep->var_value()->flatten_init_expression(gogo, function, inserter);
7587 : :
7588 : 751141 : if (this->init_ != NULL && !this->init_is_flattened_)
7589 : : {
7590 : 282858 : if (this->seen_)
7591 : : {
7592 : : // We will give an error elsewhere, this is just to prevent
7593 : : // an infinite loop.
7594 : 0 : return;
7595 : : }
7596 : 282858 : this->seen_ = true;
7597 : :
7598 : 282858 : Statement_inserter global_inserter;
7599 : 282858 : if (this->is_global_)
7600 : : {
7601 : 8364 : global_inserter = Statement_inserter(gogo, this);
7602 : 8364 : inserter = &global_inserter;
7603 : : }
7604 : :
7605 : 282858 : gogo->flatten_expression(function, inserter, &this->init_);
7606 : :
7607 : : // If an interface conversion is needed, we need a temporary
7608 : : // variable.
7609 : 282858 : if (this->type_ != NULL
7610 : 282858 : && !Type::are_identical(this->type_, this->init_->type(),
7611 : : Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
7612 : : NULL)
7613 : 898 : && this->init_->type()->interface_type() != NULL
7614 : 282859 : && !this->init_->is_multi_eval_safe())
7615 : : {
7616 : 0 : Temporary_statement* temp =
7617 : 0 : Statement::make_temporary(NULL, this->init_, this->location_);
7618 : 0 : inserter->insert(temp);
7619 : 0 : this->init_ = Expression::make_temporary_reference(temp,
7620 : : this->location_);
7621 : : }
7622 : :
7623 : 282858 : this->seen_ = false;
7624 : 282858 : this->init_is_flattened_ = true;
7625 : : }
7626 : : }
7627 : :
7628 : : // Get the preinit block.
7629 : :
7630 : : Block*
7631 : 52974 : Variable::preinit_block(Gogo* gogo)
7632 : : {
7633 : 52974 : go_assert(this->is_global_);
7634 : 52974 : if (this->preinit_ == NULL)
7635 : 12206 : this->preinit_ = new Block(NULL, this->location());
7636 : :
7637 : : // If a global variable has a preinitialization statement, then we
7638 : : // need to have an initialization function.
7639 : 52974 : gogo->set_need_init_fn();
7640 : :
7641 : 52974 : return this->preinit_;
7642 : : }
7643 : :
7644 : : // Add a statement to be run before the initialization expression.
7645 : :
7646 : : void
7647 : 52974 : Variable::add_preinit_statement(Gogo* gogo, Statement* s)
7648 : : {
7649 : 52974 : Block* b = this->preinit_block(gogo);
7650 : 52974 : b->add_statement(s);
7651 : 52974 : b->set_end_location(s->location());
7652 : 52974 : }
7653 : :
7654 : : // Whether this variable has a type.
7655 : :
7656 : : bool
7657 : 19293193 : Variable::has_type() const
7658 : : {
7659 : 19293193 : if (this->type_ == NULL)
7660 : : return false;
7661 : :
7662 : : // A variable created in a type switch case nil does not actually
7663 : : // have a type yet. It will be changed to use the initializer's
7664 : : // type in determine_type.
7665 : 18651241 : if (this->is_type_switch_var_
7666 : 18651241 : && this->type_->is_nil_constant_as_type())
7667 : : return false;
7668 : :
7669 : : return true;
7670 : : }
7671 : :
7672 : : // In an assignment which sets a variable to a tuple of EXPR, return
7673 : : // the type of the first element of the tuple.
7674 : :
7675 : : Type*
7676 : 1594 : Variable::type_from_tuple(Expression* expr, bool report_error) const
7677 : : {
7678 : 1594 : if (Index_expression::is_map_index(expr))
7679 : : {
7680 : 1553 : Map_type* mt;
7681 : 1553 : if (expr->map_index_expression() != NULL)
7682 : 0 : mt = expr->map_index_expression()->get_map_type();
7683 : : else
7684 : 1553 : mt = expr->index_expression()->left()->type()->map_type();
7685 : 0 : if (mt == NULL)
7686 : 0 : return Type::make_error_type();
7687 : 1553 : return mt->val_type();
7688 : : }
7689 : 41 : else if (expr->receive_expression() != NULL)
7690 : : {
7691 : 41 : Expression* channel = expr->receive_expression()->channel();
7692 : 41 : Type* channel_type = channel->type();
7693 : 41 : if (channel_type->channel_type() == NULL)
7694 : 0 : return Type::make_error_type();
7695 : 82 : return channel_type->channel_type()->element_type();
7696 : : }
7697 : : else
7698 : : {
7699 : 0 : if (report_error)
7700 : 0 : go_error_at(this->location(), "invalid tuple definition");
7701 : 0 : return Type::make_error_type();
7702 : : }
7703 : : }
7704 : :
7705 : : // Given EXPR used in a range clause, return either the index type or
7706 : : // the value type of the range, depending upon GET_INDEX_TYPE.
7707 : :
7708 : : Type*
7709 : 31409 : Variable::type_from_range(Expression* expr, bool get_index_type,
7710 : : bool report_error) const
7711 : : {
7712 : 31409 : Type* t = expr->type();
7713 : 31409 : if (t->is_error_type())
7714 : : return t;
7715 : 31409 : else if (t->array_type() != NULL
7716 : 4113 : || (t->points_to() != NULL
7717 : 222 : && t->points_to()->array_type() != NULL
7718 : 222 : && !t->points_to()->is_slice_type()))
7719 : : {
7720 : 27518 : if (get_index_type)
7721 : 7286 : return Type::lookup_integer_type("int");
7722 : : else
7723 : 60696 : return t->deref()->array_type()->element_type();
7724 : : }
7725 : 3891 : else if (t->is_string_type())
7726 : : {
7727 : 732 : if (get_index_type)
7728 : 190 : return Type::lookup_integer_type("int");
7729 : : else
7730 : 542 : return Type::lookup_integer_type("int32");
7731 : : }
7732 : 3159 : else if (t->map_type() != NULL)
7733 : : {
7734 : 3084 : if (get_index_type)
7735 : 3378 : return t->map_type()->key_type();
7736 : : else
7737 : 2790 : return t->map_type()->val_type();
7738 : : }
7739 : 75 : else if (t->channel_type() != NULL)
7740 : : {
7741 : 75 : if (get_index_type)
7742 : 150 : return t->channel_type()->element_type();
7743 : : else
7744 : : {
7745 : 0 : if (report_error)
7746 : 0 : go_error_at(this->location(),
7747 : : ("invalid definition of value variable "
7748 : : "for channel range"));
7749 : 0 : return Type::make_error_type();
7750 : : }
7751 : : }
7752 : : else
7753 : : {
7754 : 0 : if (report_error)
7755 : 0 : go_error_at(this->location(), "invalid type for range clause");
7756 : 0 : return Type::make_error_type();
7757 : : }
7758 : : }
7759 : :
7760 : : // EXPR should be a channel. Return the channel's element type.
7761 : :
7762 : : Type*
7763 : 524 : Variable::type_from_chan_element(Expression* expr, bool report_error) const
7764 : : {
7765 : 524 : Type* t = expr->type();
7766 : 524 : if (t->channel_type() != NULL)
7767 : 1048 : return t->channel_type()->element_type();
7768 : : else
7769 : : {
7770 : 0 : if (report_error)
7771 : 0 : go_error_at(this->location(), "expected channel");
7772 : 0 : return Type::make_error_type();
7773 : : }
7774 : : }
7775 : :
7776 : : // Return the type of the Variable. This may be called before
7777 : : // Variable::determine_type is called, which means that we may need to
7778 : : // get the type from the initializer. FIXME: If we combine lowering
7779 : : // with type determination, then this should be unnecessary.
7780 : :
7781 : : Type*
7782 : 71412277 : Variable::type()
7783 : : {
7784 : : // A variable in a type switch with a nil case will have the wrong
7785 : : // type here. This gets fixed up in determine_type, below.
7786 : 71412277 : Type* type = this->type_;
7787 : 71412277 : Expression* init = this->init_;
7788 : 71412277 : if (this->is_type_switch_var_
7789 : 1009491 : && type != NULL
7790 : 72421768 : && this->type_->is_nil_constant_as_type())
7791 : : {
7792 : 0 : Type_guard_expression* tge = this->init_->type_guard_expression();
7793 : 0 : go_assert(tge != NULL);
7794 : 0 : init = tge->expr();
7795 : 0 : type = NULL;
7796 : : }
7797 : :
7798 : 71412277 : if (this->seen_)
7799 : : {
7800 : 0 : if (this->type_ == NULL || !this->type_->is_error_type())
7801 : : {
7802 : 0 : go_error_at(this->location_, "variable initializer refers to itself");
7803 : 0 : this->type_ = Type::make_error_type();
7804 : : }
7805 : 0 : return this->type_;
7806 : : }
7807 : :
7808 : 71412277 : this->seen_ = true;
7809 : :
7810 : 71412277 : if (type != NULL)
7811 : : ;
7812 : 0 : else if (this->type_from_init_tuple_)
7813 : 0 : type = this->type_from_tuple(init, false);
7814 : 0 : else if (this->type_from_range_index_ || this->type_from_range_value_)
7815 : 0 : type = this->type_from_range(init, this->type_from_range_index_, false);
7816 : 0 : else if (this->type_from_chan_element_)
7817 : 0 : type = this->type_from_chan_element(init, false);
7818 : : else
7819 : : {
7820 : 0 : go_assert(init != NULL);
7821 : 0 : type = init->type();
7822 : 0 : go_assert(type != NULL);
7823 : :
7824 : : // Variables should not have abstract types.
7825 : 0 : if (type->is_abstract())
7826 : 0 : type = type->make_non_abstract_type();
7827 : :
7828 : 0 : if (type->is_void_type())
7829 : 0 : type = Type::make_error_type();
7830 : : }
7831 : :
7832 : 71412277 : this->seen_ = false;
7833 : :
7834 : 71412277 : return type;
7835 : : }
7836 : :
7837 : : // Fetch the type from a const pointer, in which case it should have
7838 : : // been set already.
7839 : :
7840 : : Type*
7841 : 8438 : Variable::type() const
7842 : : {
7843 : 8438 : go_assert(this->type_ != NULL);
7844 : 8438 : return this->type_;
7845 : : }
7846 : :
7847 : : // Set the type if necessary.
7848 : :
7849 : : void
7850 : 6758796 : Variable::determine_type(Gogo* gogo)
7851 : : {
7852 : 6758796 : if (this->determined_type_)
7853 : : return;
7854 : 906383 : this->determined_type_ = true;
7855 : :
7856 : 906383 : if (this->preinit_ != NULL)
7857 : 4 : this->preinit_->determine_types(gogo);
7858 : :
7859 : : // A variable in a type switch with a nil case will have the wrong
7860 : : // type here. It will have an initializer which is a type guard.
7861 : : // We want to initialize it to the value without the type guard, and
7862 : : // use the type of that value as well.
7863 : 906383 : if (this->is_type_switch_var_
7864 : 9915 : && this->type_ != NULL
7865 : 915157 : && this->type_->is_nil_constant_as_type())
7866 : : {
7867 : 122 : Type_guard_expression* tge = this->init_->type_guard_expression();
7868 : 0 : go_assert(tge != NULL);
7869 : 122 : this->type_ = NULL;
7870 : 122 : this->init_ = tge->expr();
7871 : : }
7872 : :
7873 : 906383 : if (this->init_ == NULL)
7874 : 571360 : go_assert(this->type_ != NULL && !this->type_->is_abstract());
7875 : 335023 : else if (this->type_from_init_tuple_)
7876 : : {
7877 : 1594 : Expression *init = this->init_;
7878 : 1594 : init->determine_type_no_context(gogo);
7879 : 1594 : this->type_ = this->type_from_tuple(init, true);
7880 : 1594 : this->init_ = NULL;
7881 : : }
7882 : 333429 : else if (this->type_from_range_index_ || this->type_from_range_value_)
7883 : : {
7884 : 31409 : Expression* init = this->init_;
7885 : 31409 : init->determine_type_no_context(gogo);
7886 : 31409 : this->type_ = this->type_from_range(init, this->type_from_range_index_,
7887 : : true);
7888 : 31409 : this->init_ = NULL;
7889 : 31409 : }
7890 : 302020 : else if (this->type_from_chan_element_)
7891 : : {
7892 : 524 : Expression* init = this->init_;
7893 : 524 : init->determine_type_no_context(gogo);
7894 : 524 : this->type_ = this->type_from_chan_element(init, true);
7895 : 524 : this->init_ = NULL;
7896 : : }
7897 : : else
7898 : : {
7899 : 301496 : Type_context context(this->type_, false);
7900 : 301496 : this->init_->determine_type(gogo, &context);
7901 : 301496 : if (this->type_ == NULL)
7902 : : {
7903 : 287571 : Type* type = this->init_->type();
7904 : 287571 : go_assert(type != NULL);
7905 : 287571 : if (type->is_abstract())
7906 : 601 : type = type->make_non_abstract_type();
7907 : :
7908 : 287571 : if (type->is_void_type())
7909 : : {
7910 : 1 : go_error_at(this->location_, "variable has no type");
7911 : 1 : type = Type::make_error_type();
7912 : : }
7913 : 287570 : else if (type->is_nil_type())
7914 : : {
7915 : 2 : go_error_at(this->location_, "variable defined to nil type");
7916 : 2 : type = Type::make_error_type();
7917 : : }
7918 : 287568 : else if (type->is_call_multiple_result_type())
7919 : : {
7920 : 2 : go_error_at(this->location_,
7921 : : "single variable set to multiple-value function call");
7922 : 2 : type = Type::make_error_type();
7923 : : }
7924 : :
7925 : 287571 : this->type_ = type;
7926 : : }
7927 : : }
7928 : : }
7929 : :
7930 : : // Get the initial value of a variable. This does not
7931 : : // consider whether the variable is in the heap--it returns the
7932 : : // initial value as though it were always stored in the stack.
7933 : :
7934 : : Bexpression*
7935 : 395649 : Variable::get_init(Gogo* gogo, Named_object* function)
7936 : : {
7937 : 395649 : go_assert(this->preinit_ == NULL);
7938 : 395649 : Location loc = this->location();
7939 : 395649 : if (this->init_ == NULL)
7940 : : {
7941 : 113109 : go_assert(!this->is_parameter_);
7942 : 113109 : if (this->is_global_ || this->is_in_heap())
7943 : : return NULL;
7944 : 95247 : Btype* btype = this->type()->get_backend(gogo);
7945 : 95247 : return gogo->backend()->zero_expression(btype);
7946 : : }
7947 : : else
7948 : : {
7949 : 282540 : Translate_context context(gogo, function, NULL, NULL);
7950 : 282540 : Expression* init = Expression::make_cast(this->type(), this->init_, loc);
7951 : 282540 : return init->get_backend(&context);
7952 : : }
7953 : : }
7954 : :
7955 : : // Get the initial value of a variable when a block is required.
7956 : : // VAR_DECL is the decl to set; it may be NULL for a sink variable.
7957 : :
7958 : : Bstatement*
7959 : 12206 : Variable::get_init_block(Gogo* gogo, Named_object* function,
7960 : : Bvariable* var_decl)
7961 : : {
7962 : 12206 : go_assert(this->preinit_ != NULL);
7963 : :
7964 : : // We want to add the variable assignment to the end of the preinit
7965 : : // block.
7966 : :
7967 : 12206 : Translate_context context(gogo, function, NULL, NULL);
7968 : 12206 : Bblock* bblock = this->preinit_->get_backend(&context);
7969 : 12206 : Bfunction* bfunction =
7970 : 12206 : function->func_value()->get_or_make_decl(gogo, function);
7971 : :
7972 : : // It's possible to have pre-init statements without an initializer
7973 : : // if the pre-init statements set the variable.
7974 : 12206 : Bstatement* decl_init = NULL;
7975 : 12206 : if (this->init_ != NULL)
7976 : : {
7977 : 212 : if (var_decl == NULL)
7978 : : {
7979 : 29 : Bexpression* init_bexpr = this->init_->get_backend(&context);
7980 : 29 : decl_init = gogo->backend()->expression_statement(bfunction,
7981 : : init_bexpr);
7982 : : }
7983 : : else
7984 : : {
7985 : 183 : Location loc = this->location();
7986 : 183 : Expression* val_expr =
7987 : 183 : Expression::make_cast(this->type(), this->init_, loc);
7988 : 183 : Bexpression* val = val_expr->get_backend(&context);
7989 : 183 : Bexpression* var_ref =
7990 : 183 : gogo->backend()->var_expression(var_decl, loc);
7991 : 183 : decl_init = gogo->backend()->assignment_statement(bfunction, var_ref,
7992 : : val, loc);
7993 : : }
7994 : : }
7995 : 12206 : Bstatement* block_stmt = gogo->backend()->block_statement(bblock);
7996 : 12206 : if (decl_init != NULL)
7997 : 212 : block_stmt = gogo->backend()->compound_statement(block_stmt, decl_init);
7998 : 12206 : return block_stmt;
7999 : : }
8000 : :
8001 : : // Add an initializer reference.
8002 : :
8003 : : void
8004 : 11650 : Variable::add_init_ref(Named_object* var)
8005 : : {
8006 : 11650 : if (this->init_refs_ == NULL)
8007 : 3632 : this->init_refs_ = new std::vector<Named_object*>;
8008 : 11650 : this->init_refs_->push_back(var);
8009 : 11650 : }
8010 : :
8011 : : // Export the variable
8012 : :
8013 : : void
8014 : 8438 : Variable::export_var(Export* exp, const Named_object* no) const
8015 : : {
8016 : 8438 : go_assert(this->is_global_);
8017 : 8438 : exp->write_c_string("var ");
8018 : 8438 : if (no->package() != NULL)
8019 : : {
8020 : 2056 : char buf[50];
8021 : 2056 : snprintf(buf, sizeof buf, "<p%d>", exp->package_index(no->package()));
8022 : 2056 : exp->write_c_string(buf);
8023 : : }
8024 : :
8025 : 8438 : if (!Gogo::is_hidden_name(no->name()))
8026 : 5354 : exp->write_string(no->name());
8027 : : else
8028 : : {
8029 : 3084 : exp->write_c_string(".");
8030 : 3084 : exp->write_string(Gogo::unpack_hidden_name(no->name()));
8031 : : }
8032 : :
8033 : 8438 : exp->write_c_string(" ");
8034 : 8438 : exp->write_type(this->type());
8035 : 8438 : exp->write_c_string("\n");
8036 : 8438 : }
8037 : :
8038 : : // Import a variable.
8039 : :
8040 : : bool
8041 : 326235 : Variable::import_var(Import* imp, std::string* pname, Package** ppkg,
8042 : : bool* pis_exported, Type** ptype)
8043 : : {
8044 : 326235 : imp->require_c_string("var ");
8045 : 326235 : if (!Import::read_qualified_identifier(imp, pname, ppkg, pis_exported))
8046 : : {
8047 : 0 : go_error_at(imp->location(),
8048 : : "import error at %d: bad variable name in export data",
8049 : : imp->pos());
8050 : 0 : return false;
8051 : : }
8052 : 326235 : imp->require_c_string(" ");
8053 : 326235 : *ptype = imp->read_type();
8054 : 326235 : imp->require_semicolon_if_old_version();
8055 : 326235 : imp->require_c_string("\n");
8056 : 326235 : return true;
8057 : : }
8058 : :
8059 : : // Convert a variable to the backend representation.
8060 : :
8061 : : Bvariable*
8062 : 4440232 : Variable::get_backend_variable(Gogo* gogo, Named_object* function,
8063 : : const Package* package, const std::string& name)
8064 : : {
8065 : 4440232 : if (this->backend_ == NULL)
8066 : : {
8067 : 923149 : Backend* backend = gogo->backend();
8068 : 923149 : Type* type = this->type_;
8069 : 923149 : if (type->is_error_type()
8070 : 923149 : || (type->is_undefined()
8071 : 5 : && (!this->is_global_ || package == NULL)))
8072 : 149 : this->backend_ = backend->error_variable();
8073 : : else
8074 : : {
8075 : 923000 : bool is_parameter = this->is_parameter_;
8076 : 923000 : if (this->is_receiver_ && type->points_to() == NULL)
8077 : : is_parameter = false;
8078 : 923000 : if (this->is_in_heap())
8079 : : {
8080 : 18175 : is_parameter = false;
8081 : 18175 : type = Type::make_pointer_type(type);
8082 : : }
8083 : :
8084 : 923000 : Btype* btype = type->get_backend(gogo);
8085 : :
8086 : 923000 : Bvariable* bvar;
8087 : 923000 : if (Map_type::is_zero_value(this))
8088 : 3 : bvar = Map_type::backend_zero_value(gogo);
8089 : 922997 : else if (this->is_global_)
8090 : : {
8091 : 34340 : Backend_name bname;
8092 : 34340 : gogo->global_var_backend_name(name, package, &bname);
8093 : :
8094 : 34340 : bool is_hidden = Gogo::is_hidden_name(name);
8095 : : // Hack to export runtime.writeBarrier. FIXME.
8096 : : // This is because go:linkname doesn't work on variables.
8097 : 68680 : if (gogo->compiling_runtime()
8098 : 34340 : && bname.name() == "runtime.writeBarrier")
8099 : 20 : is_hidden = false;
8100 : :
8101 : : // If an inline body refers to this variable, then it
8102 : : // needs to be visible in the symbol table.
8103 : 34340 : if (this->is_referenced_by_inline_)
8104 : 4327 : is_hidden = false;
8105 : :
8106 : : // If this variable is in a different package, then it
8107 : : // can't be treated as a hidden symbol. This case can
8108 : : // arise when an inlined function refers to a
8109 : : // package-scope unexported variable.
8110 : 34340 : if (package != NULL)
8111 : 9045 : is_hidden = false;
8112 : :
8113 : 34340 : unsigned int flags = 0;
8114 : 34340 : if (this->is_address_taken_
8115 : 32081 : || this->is_non_escaping_address_taken_)
8116 : 3542 : flags |= Backend::variable_address_is_taken;
8117 : 34340 : if (package != NULL)
8118 : 9045 : flags |= Backend::variable_is_external;
8119 : 34340 : if (is_hidden)
8120 : 17925 : flags |= Backend::variable_is_hidden;
8121 : 34340 : if (this->in_unique_section_)
8122 : 0 : flags |= Backend::variable_in_unique_section;
8123 : :
8124 : : // For some reason asm_name can't be the empty string
8125 : : // for global_variable, so we call asm_name rather than
8126 : : // optional_asm_name here. FIXME.
8127 : :
8128 : 34340 : bvar = backend->global_variable(bname.name(),
8129 : 34340 : bname.asm_name(),
8130 : : btype, flags,
8131 : : this->location_);
8132 : 34340 : }
8133 : 888657 : else if (function == NULL)
8134 : : {
8135 : 0 : go_assert(saw_errors());
8136 : 0 : bvar = backend->error_variable();
8137 : : }
8138 : : else
8139 : : {
8140 : 888657 : const std::string n = Gogo::unpack_hidden_name(name);
8141 : 888657 : Bfunction* bfunction = function->func_value()->get_decl();
8142 : 888657 : unsigned int flags = 0;
8143 : 888657 : if (this->is_non_escaping_address_taken_
8144 : 888657 : && !this->is_in_heap())
8145 : : flags |= Backend::variable_address_is_taken;
8146 : 888657 : if (this->is_closure())
8147 : 14291 : bvar = backend->static_chain_variable(bfunction, n, btype,
8148 : : flags, this->location_);
8149 : 874366 : else if (is_parameter)
8150 : 473747 : bvar = backend->parameter_variable(bfunction, n, btype,
8151 : : flags, this->location_);
8152 : : else
8153 : : {
8154 : 400619 : Bvariable* bvar_decl = NULL;
8155 : 400619 : if (this->toplevel_decl_ != NULL)
8156 : : {
8157 : 6520 : Translate_context context(gogo, NULL, NULL, NULL);
8158 : 6520 : bvar_decl = this->toplevel_decl_->temporary_statement()
8159 : 6520 : ->get_backend_variable(&context);
8160 : : }
8161 : 400619 : bvar = backend->local_variable(bfunction, n, btype,
8162 : : bvar_decl, flags,
8163 : : this->location_);
8164 : : }
8165 : 888657 : }
8166 : 923000 : this->backend_ = bvar;
8167 : : }
8168 : : }
8169 : 4440232 : return this->backend_;
8170 : : }
8171 : :
8172 : : // Class Result_variable.
8173 : :
8174 : : // Convert a result variable to the backend representation.
8175 : :
8176 : : Bvariable*
8177 : 1161163 : Result_variable::get_backend_variable(Gogo* gogo, Named_object* function,
8178 : : const std::string& name)
8179 : : {
8180 : 1161163 : if (this->backend_ == NULL)
8181 : : {
8182 : 244538 : Backend* backend = gogo->backend();
8183 : 244538 : Type* type = this->type_;
8184 : 244538 : if (type->is_error())
8185 : 3 : this->backend_ = backend->error_variable();
8186 : : else
8187 : : {
8188 : 244535 : if (this->is_in_heap())
8189 : 122 : type = Type::make_pointer_type(type);
8190 : 244535 : Btype* btype = type->get_backend(gogo);
8191 : 244535 : Bfunction* bfunction = function->func_value()->get_decl();
8192 : 244535 : std::string n = Gogo::unpack_hidden_name(name);
8193 : 244535 : unsigned int flags = 0;
8194 : 244535 : if (this->is_non_escaping_address_taken_
8195 : 244535 : && !this->is_in_heap())
8196 : : flags |= Backend::variable_address_is_taken;
8197 : 244535 : this->backend_ = backend->local_variable(bfunction, n, btype,
8198 : : NULL, flags,
8199 : : this->location_);
8200 : 244535 : }
8201 : : }
8202 : 1161163 : return this->backend_;
8203 : : }
8204 : :
8205 : : // Class Named_constant.
8206 : :
8207 : : // Set the type of a named constant. This is only used to set the
8208 : : // type to an error type.
8209 : :
8210 : : void
8211 : 0 : Named_constant::set_type(Type* t)
8212 : : {
8213 : 0 : go_assert(this->type_ == NULL || t->is_error_type());
8214 : 0 : this->type_ = t;
8215 : 0 : }
8216 : :
8217 : : // Traverse the initializer expression.
8218 : :
8219 : : int
8220 : 15967781 : Named_constant::traverse_expression(Traverse* traverse)
8221 : : {
8222 : 15967781 : return Expression::traverse(&this->expr_, traverse);
8223 : : }
8224 : :
8225 : : // Set the iota value in a constant expression.
8226 : :
8227 : 982092 : class Set_iota_value : public Traverse
8228 : : {
8229 : : public:
8230 : 982092 : Set_iota_value(int iota_value)
8231 : 982092 : : Traverse(traverse_expressions),
8232 : 982092 : iota_value_(iota_value)
8233 : : { }
8234 : :
8235 : : int
8236 : : expression(Expression**);
8237 : :
8238 : : private:
8239 : : int iota_value_;
8240 : : };
8241 : :
8242 : : int
8243 : 1107540 : Set_iota_value::expression(Expression** pexpr)
8244 : : {
8245 : 1107540 : Expression* expr = *pexpr;
8246 : 1107540 : if (expr->const_expression() != NULL)
8247 : 45452 : expr->const_expression()->set_iota_value(this->iota_value_);
8248 : 1062088 : else if (expr->unknown_expression() != NULL)
8249 : : {
8250 : : // This case can happen for an array length that is not set in
8251 : : // the determine types pass.
8252 : 17241 : expr->unknown_expression()->set_iota_value(this->iota_value_);
8253 : : }
8254 : 1107540 : return TRAVERSE_CONTINUE;
8255 : : }
8256 : :
8257 : : // Determine the type of the constant.
8258 : :
8259 : : void
8260 : 1563690 : Named_constant::determine_type(Gogo* gogo)
8261 : : {
8262 : 1563690 : if (this->type_is_determined_)
8263 : 581598 : return;
8264 : 982092 : this->type_is_determined_ = true;
8265 : :
8266 : 982092 : if (this->type_ != NULL)
8267 : : {
8268 : 222601 : Type_context context(this->type_, this->type_->is_abstract());
8269 : 222601 : this->expr_->determine_type(gogo, &context);
8270 : : }
8271 : : else
8272 : : {
8273 : : // A constant may have an abstract type.
8274 : 759491 : Type_context context(NULL, true);
8275 : 759491 : this->expr_->determine_type(gogo, &context);
8276 : 759491 : this->type_ = this->expr_->type();
8277 : 759491 : go_assert(this->type_ != NULL);
8278 : : }
8279 : :
8280 : 982092 : Set_iota_value siv(this->iota_value_);
8281 : 982092 : this->traverse_expression(&siv);
8282 : 982092 : }
8283 : :
8284 : : // Indicate that we found and reported an error for this constant.
8285 : :
8286 : : void
8287 : 41 : Named_constant::set_error()
8288 : : {
8289 : 41 : this->type_ = Type::make_error_type();
8290 : 41 : this->expr_ = Expression::make_error(this->location_);
8291 : 41 : }
8292 : :
8293 : : // Export a constant.
8294 : :
8295 : : void
8296 : 39159 : Named_constant::export_const(Export* exp, const std::string& name) const
8297 : : {
8298 : 39159 : exp->write_c_string("const ");
8299 : 39159 : exp->write_string(name);
8300 : 39159 : exp->write_c_string(" ");
8301 : 39159 : if (!this->type_->is_abstract())
8302 : : {
8303 : 15584 : exp->write_type(this->type_);
8304 : 15584 : exp->write_c_string(" ");
8305 : : }
8306 : 39159 : exp->write_c_string("= ");
8307 : :
8308 : 39159 : Export_function_body efb(exp, 0);
8309 : 39159 : if (!this->type_->is_abstract())
8310 : 15584 : efb.set_type_context(this->type_);
8311 : 39159 : this->expr()->export_expression(&efb);
8312 : 39159 : exp->write_string(efb.body());
8313 : :
8314 : 39159 : exp->write_c_string("\n");
8315 : 39159 : }
8316 : :
8317 : : // Import a constant.
8318 : :
8319 : : void
8320 : 826170 : Named_constant::import_const(Import* imp, std::string* pname, Type** ptype,
8321 : : Expression** pexpr)
8322 : : {
8323 : 826170 : imp->require_c_string("const ");
8324 : 826170 : *pname = imp->read_identifier();
8325 : 826170 : imp->require_c_string(" ");
8326 : 826170 : if (imp->peek_char() == '=')
8327 : 630826 : *ptype = NULL;
8328 : : else
8329 : : {
8330 : 195344 : *ptype = imp->read_type();
8331 : 195344 : imp->require_c_string(" ");
8332 : : }
8333 : 826170 : imp->require_c_string("= ");
8334 : 826170 : *pexpr = Expression::import_expression(imp, imp->location());
8335 : 826170 : imp->require_semicolon_if_old_version();
8336 : 826170 : imp->require_c_string("\n");
8337 : 826170 : }
8338 : :
8339 : : // Get the backend representation.
8340 : :
8341 : : Bexpression*
8342 : 23399 : Named_constant::get_backend(Gogo* gogo, Named_object* const_no)
8343 : : {
8344 : 23399 : if (this->bconst_ == NULL)
8345 : : {
8346 : 23399 : Translate_context subcontext(gogo, NULL, NULL, NULL);
8347 : 23399 : Type* type = this->type();
8348 : 23399 : Location loc = this->location();
8349 : :
8350 : 23399 : Expression* const_ref = Expression::make_const_reference(const_no, loc);
8351 : 23399 : Bexpression* const_decl = const_ref->get_backend(&subcontext);
8352 : 46798 : if (type != NULL && type->is_numeric_type())
8353 : : {
8354 : 23399 : Btype* btype = type->get_backend(gogo);
8355 : 23399 : std::string name;
8356 : 23399 : if (const_no->package() == NULL)
8357 : 23399 : name = gogo->pkgpath();
8358 : : else
8359 : 0 : name = const_no->package()->pkgpath();
8360 : 23399 : name.push_back('.');
8361 : 23399 : name.append(Gogo::unpack_hidden_name(const_no->name()));
8362 : 23399 : const_decl =
8363 : 23399 : gogo->backend()->named_constant_expression(btype, name,
8364 : : const_decl, loc);
8365 : 23399 : }
8366 : 23399 : this->bconst_ = const_decl;
8367 : : }
8368 : 23399 : return this->bconst_;
8369 : : }
8370 : :
8371 : : // Add a method.
8372 : :
8373 : : Named_object*
8374 : 2538 : Type_declaration::add_method(const std::string& name, Function* function)
8375 : : {
8376 : 2538 : Named_object* ret = Named_object::make_function(name, NULL, function);
8377 : 2538 : this->methods_.push_back(ret);
8378 : 2538 : return ret;
8379 : : }
8380 : :
8381 : : // Add a method declaration.
8382 : :
8383 : : Named_object*
8384 : 0 : Type_declaration::add_method_declaration(const std::string& name,
8385 : : Package* package,
8386 : : Function_type* type,
8387 : : Location location)
8388 : : {
8389 : 0 : Named_object* ret = Named_object::make_function_declaration(name, package,
8390 : 0 : type, location);
8391 : 0 : this->methods_.push_back(ret);
8392 : 0 : return ret;
8393 : : }
8394 : :
8395 : : // Return whether any methods are defined.
8396 : :
8397 : : bool
8398 : 2 : Type_declaration::has_methods() const
8399 : : {
8400 : 2 : return !this->methods_.empty();
8401 : : }
8402 : :
8403 : : // Define methods for the real type.
8404 : :
8405 : : void
8406 : 622811 : Type_declaration::define_methods(Named_type* nt)
8407 : : {
8408 : 622811 : if (this->methods_.empty())
8409 : : return;
8410 : :
8411 : 518 : while (nt->is_alias())
8412 : : {
8413 : 0 : Type *t = nt->real_type()->forwarded();
8414 : 0 : if (t->named_type() != NULL)
8415 : 0 : nt = t->named_type();
8416 : 0 : else if (t->forward_declaration_type() != NULL)
8417 : : {
8418 : 0 : Named_object* no = t->forward_declaration_type()->named_object();
8419 : 0 : Type_declaration* td = no->type_declaration_value();
8420 : 0 : td->methods_.insert(td->methods_.end(), this->methods_.begin(),
8421 : : this->methods_.end());
8422 : 0 : this->methods_.clear();
8423 : 0 : return;
8424 : : }
8425 : : else
8426 : : {
8427 : 0 : for (std::vector<Named_object*>::const_iterator p =
8428 : 0 : this->methods_.begin();
8429 : 0 : p != this->methods_.end();
8430 : 0 : ++p)
8431 : 0 : go_error_at((*p)->location(),
8432 : : ("invalid receiver type "
8433 : : "(receiver must be a named type)"));
8434 : : return;
8435 : : }
8436 : : }
8437 : :
8438 : 3055 : for (std::vector<Named_object*>::const_iterator p = this->methods_.begin();
8439 : 3055 : p != this->methods_.end();
8440 : 2537 : ++p)
8441 : : {
8442 : 2537 : if ((*p)->is_function_declaration()
8443 : 2537 : || !(*p)->func_value()->is_sink())
8444 : 2536 : nt->add_existing_method(*p);
8445 : : }
8446 : : }
8447 : :
8448 : : // We are using the type. Return true if we should issue a warning.
8449 : :
8450 : : bool
8451 : 8 : Type_declaration::using_type()
8452 : : {
8453 : 8 : bool ret = !this->issued_warning_;
8454 : 8 : this->issued_warning_ = true;
8455 : 8 : return ret;
8456 : : }
8457 : :
8458 : : // Class Unknown_name.
8459 : :
8460 : : // Set the real named object.
8461 : :
8462 : : void
8463 : 85611 : Unknown_name::set_real_named_object(Named_object* no)
8464 : : {
8465 : 85611 : go_assert(this->real_named_object_ == NULL);
8466 : 85611 : go_assert(!no->is_unknown());
8467 : 85611 : this->real_named_object_ = no;
8468 : 85611 : }
8469 : :
8470 : : // Class Named_object.
8471 : :
8472 : 10320588 : Named_object::Named_object(const std::string& name,
8473 : : const Package* package,
8474 : 10320588 : Classification classification)
8475 : 10320588 : : name_(name), package_(package), classification_(classification),
8476 : 10320588 : is_redefinition_(false)
8477 : : {
8478 : 10320588 : if (Gogo::is_sink_name(name))
8479 : 1385198 : go_assert(classification == NAMED_OBJECT_SINK);
8480 : 10320588 : }
8481 : :
8482 : : // Make an unknown name. This is used by the parser. The name must
8483 : : // be resolved later. Unknown names are only added in the current
8484 : : // package.
8485 : :
8486 : : Named_object*
8487 : 85665 : Named_object::make_unknown_name(const std::string& name,
8488 : : Location location)
8489 : : {
8490 : 85665 : Named_object* named_object = new Named_object(name, NULL,
8491 : 85665 : NAMED_OBJECT_UNKNOWN);
8492 : 85665 : Unknown_name* value = new Unknown_name(location);
8493 : 85665 : named_object->u_.unknown_value = value;
8494 : 85665 : return named_object;
8495 : : }
8496 : :
8497 : : // Make a constant.
8498 : :
8499 : : Named_object*
8500 : 995411 : Named_object::make_constant(const Typed_identifier& tid,
8501 : : const Package* package, Expression* expr,
8502 : : int iota_value)
8503 : : {
8504 : 995411 : Named_object* named_object = new Named_object(tid.name(), package,
8505 : 995411 : NAMED_OBJECT_CONST);
8506 : 995411 : Named_constant* named_constant = new Named_constant(tid.type(), expr,
8507 : : iota_value,
8508 : 995411 : tid.location());
8509 : 995411 : named_object->u_.const_value = named_constant;
8510 : 995411 : return named_object;
8511 : : }
8512 : :
8513 : : // Make a named type.
8514 : :
8515 : : Named_object*
8516 : 752321 : Named_object::make_type(const std::string& name, const Package* package,
8517 : : Type* type, Location location)
8518 : : {
8519 : 752321 : Named_object* named_object = new Named_object(name, package,
8520 : 752321 : NAMED_OBJECT_TYPE);
8521 : 752321 : Named_type* named_type = Type::make_named_type(named_object, type, location);
8522 : 752321 : named_object->u_.type_value = named_type;
8523 : 752321 : return named_object;
8524 : : }
8525 : :
8526 : : // Make a type declaration.
8527 : :
8528 : : Named_object*
8529 : 623328 : Named_object::make_type_declaration(const std::string& name,
8530 : : const Package* package,
8531 : : Location location)
8532 : : {
8533 : 623328 : Named_object* named_object = new Named_object(name, package,
8534 : 623328 : NAMED_OBJECT_TYPE_DECLARATION);
8535 : 623328 : Type_declaration* type_declaration = new Type_declaration(location);
8536 : 623328 : named_object->u_.type_declaration = type_declaration;
8537 : 623328 : return named_object;
8538 : : }
8539 : :
8540 : : // Make a variable.
8541 : :
8542 : : Named_object*
8543 : 1742558 : Named_object::make_variable(const std::string& name, const Package* package,
8544 : : Variable* variable)
8545 : : {
8546 : 1742558 : Named_object* named_object = new Named_object(name, package,
8547 : 1742558 : NAMED_OBJECT_VAR);
8548 : 1742558 : named_object->u_.var_value = variable;
8549 : 1742558 : return named_object;
8550 : : }
8551 : :
8552 : : // Make a result variable.
8553 : :
8554 : : Named_object*
8555 : 244699 : Named_object::make_result_variable(const std::string& name,
8556 : : Result_variable* result)
8557 : : {
8558 : 244699 : Named_object* named_object = new Named_object(name, NULL,
8559 : 244699 : NAMED_OBJECT_RESULT_VAR);
8560 : 244699 : named_object->u_.result_var_value = result;
8561 : 244699 : return named_object;
8562 : : }
8563 : :
8564 : : // Make a sink. This is used for the special blank identifier _.
8565 : :
8566 : : Named_object*
8567 : 1385198 : Named_object::make_sink()
8568 : : {
8569 : 1385198 : return new Named_object("_", NULL, NAMED_OBJECT_SINK);
8570 : : }
8571 : :
8572 : : // Make a named function.
8573 : :
8574 : : Named_object*
8575 : 299068 : Named_object::make_function(const std::string& name, const Package* package,
8576 : : Function* function)
8577 : : {
8578 : 299068 : Named_object* named_object = new Named_object(name, package,
8579 : 299068 : NAMED_OBJECT_FUNC);
8580 : 299068 : named_object->u_.func_value = function;
8581 : 299068 : return named_object;
8582 : : }
8583 : :
8584 : : // Make a function declaration.
8585 : :
8586 : : Named_object*
8587 : 4152909 : Named_object::make_function_declaration(const std::string& name,
8588 : : const Package* package,
8589 : : Function_type* fntype,
8590 : : Location location)
8591 : : {
8592 : 4152909 : Named_object* named_object = new Named_object(name, package,
8593 : 4152909 : NAMED_OBJECT_FUNC_DECLARATION);
8594 : 4152909 : Function_declaration *func_decl = new Function_declaration(fntype, location);
8595 : 4152909 : named_object->u_.func_declaration_value = func_decl;
8596 : 4152909 : return named_object;
8597 : : }
8598 : :
8599 : : // Make a package.
8600 : :
8601 : : Named_object*
8602 : 39417 : Named_object::make_package(const std::string& alias, Package* package)
8603 : : {
8604 : 39417 : Named_object* named_object = new Named_object(alias, NULL,
8605 : 39417 : NAMED_OBJECT_PACKAGE);
8606 : 39417 : named_object->u_.package_value = package;
8607 : 39417 : return named_object;
8608 : : }
8609 : :
8610 : : // Return the name to use in an error message.
8611 : :
8612 : : std::string
8613 : 232729 : Named_object::message_name() const
8614 : : {
8615 : 232729 : if (this->package_ == NULL)
8616 : 214625 : return Gogo::message_name(this->name_);
8617 : 18104 : std::string ret;
8618 : 18104 : if (this->package_->has_package_name())
8619 : 18104 : ret = this->package_->package_name();
8620 : : else
8621 : 0 : ret = this->package_->pkgpath();
8622 : 18104 : ret = Gogo::message_name(ret);
8623 : 18104 : ret += '.';
8624 : 18104 : ret += Gogo::message_name(this->name_);
8625 : 18104 : return ret;
8626 : 18104 : }
8627 : :
8628 : : // Set the type when a declaration is defined.
8629 : :
8630 : : void
8631 : 622811 : Named_object::set_type_value(Named_type* named_type)
8632 : : {
8633 : 622811 : go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
8634 : 622811 : Type_declaration* td = this->u_.type_declaration;
8635 : 622811 : td->define_methods(named_type);
8636 : 622811 : unsigned int index;
8637 : 622811 : Named_object* in_function = td->in_function(&index);
8638 : 622811 : if (in_function != NULL)
8639 : 1295 : named_type->set_in_function(in_function, index);
8640 : 624106 : delete td;
8641 : 622811 : this->classification_ = NAMED_OBJECT_TYPE;
8642 : 622811 : this->u_.type_value = named_type;
8643 : 622811 : }
8644 : :
8645 : : // Define a function which was previously declared.
8646 : :
8647 : : void
8648 : 106617 : Named_object::set_function_value(Function* function)
8649 : : {
8650 : 106617 : go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
8651 : 106617 : if (this->func_declaration_value()->has_descriptor())
8652 : : {
8653 : 0 : Expression* descriptor =
8654 : 0 : this->func_declaration_value()->descriptor(NULL, NULL);
8655 : 0 : function->set_descriptor(descriptor);
8656 : : }
8657 : 106617 : this->classification_ = NAMED_OBJECT_FUNC;
8658 : : // FIXME: We should free the old value.
8659 : 106617 : this->u_.func_value = function;
8660 : 106617 : }
8661 : :
8662 : : // Declare an unknown object as a type declaration.
8663 : :
8664 : : void
8665 : 1 : Named_object::declare_as_type()
8666 : : {
8667 : 1 : go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
8668 : 1 : Unknown_name* unk = this->u_.unknown_value;
8669 : 1 : this->classification_ = NAMED_OBJECT_TYPE_DECLARATION;
8670 : 1 : this->u_.type_declaration = new Type_declaration(unk->location());
8671 : 1 : delete unk;
8672 : 1 : }
8673 : :
8674 : : // Return the location of a named object.
8675 : :
8676 : : Location
8677 : 1913030 : Named_object::location() const
8678 : : {
8679 : 1913030 : switch (this->classification_)
8680 : : {
8681 : 0 : default:
8682 : 0 : case NAMED_OBJECT_UNINITIALIZED:
8683 : 0 : go_unreachable();
8684 : :
8685 : 0 : case NAMED_OBJECT_ERRONEOUS:
8686 : 0 : return Linemap::unknown_location();
8687 : :
8688 : 532 : case NAMED_OBJECT_UNKNOWN:
8689 : 532 : return this->unknown_value()->location();
8690 : :
8691 : 12553 : case NAMED_OBJECT_CONST:
8692 : 12553 : return this->const_value()->location();
8693 : :
8694 : 15018 : case NAMED_OBJECT_TYPE:
8695 : 15018 : return this->type_value()->location();
8696 : :
8697 : 0 : case NAMED_OBJECT_TYPE_DECLARATION:
8698 : 0 : return this->type_declaration_value()->location();
8699 : :
8700 : 700839 : case NAMED_OBJECT_VAR:
8701 : 700839 : return this->var_value()->location();
8702 : :
8703 : 244538 : case NAMED_OBJECT_RESULT_VAR:
8704 : 244538 : return this->result_var_value()->location();
8705 : :
8706 : 0 : case NAMED_OBJECT_SINK:
8707 : 0 : go_unreachable();
8708 : :
8709 : 441899 : case NAMED_OBJECT_FUNC:
8710 : 441899 : return this->func_value()->location();
8711 : :
8712 : 458225 : case NAMED_OBJECT_FUNC_DECLARATION:
8713 : 458225 : return this->func_declaration_value()->location();
8714 : :
8715 : 39426 : case NAMED_OBJECT_PACKAGE:
8716 : 39426 : return this->package_value()->location();
8717 : : }
8718 : : }
8719 : :
8720 : : // Traverse a Named_object.
8721 : :
8722 : : int
8723 : 68632832 : Named_object::traverse(Traverse* traverse, bool is_global)
8724 : : {
8725 : 68632832 : const unsigned int traverse_mask = traverse->traverse_mask();
8726 : 68632832 : const unsigned int e_or_t = (Traverse::traverse_expressions
8727 : : | Traverse::traverse_types);
8728 : 68632832 : const unsigned int e_or_t_or_s = (e_or_t
8729 : : | Traverse::traverse_statements);
8730 : :
8731 : 68632832 : int t = TRAVERSE_CONTINUE;
8732 : 68632832 : switch (this->classification_)
8733 : : {
8734 : 20696386 : case Named_object::NAMED_OBJECT_CONST:
8735 : 20696386 : if ((traverse_mask & Traverse::traverse_constants) != 0)
8736 : 1969662 : t = traverse->constant(this, is_global);
8737 : 1969662 : if (t == TRAVERSE_CONTINUE
8738 : 20696386 : && (traverse_mask & e_or_t) != 0)
8739 : : {
8740 : 12810046 : Type* tc = this->const_value()->type();
8741 : 12810046 : if (tc != NULL)
8742 : : {
8743 : 11285280 : if (Type::traverse(tc, traverse) == TRAVERSE_EXIT)
8744 : : return TRAVERSE_EXIT;
8745 : : }
8746 : 12810046 : t = this->const_value()->traverse_expression(traverse);
8747 : : }
8748 : : break;
8749 : :
8750 : 29743312 : case Named_object::NAMED_OBJECT_VAR:
8751 : 29743312 : case Named_object::NAMED_OBJECT_RESULT_VAR:
8752 : 29743312 : if ((traverse_mask & Traverse::traverse_variables) != 0)
8753 : 7658629 : t = traverse->variable(this);
8754 : 7658629 : if (t == TRAVERSE_CONTINUE
8755 : 28035432 : && (traverse_mask & e_or_t) != 0)
8756 : : {
8757 : 22624103 : if (this->is_result_variable() || this->var_value()->has_type())
8758 : : {
8759 : 21981907 : Type* tv = (this->is_variable()
8760 : 21981907 : ? this->var_value()->type()
8761 : 4142666 : : this->result_var_value()->type());
8762 : 21981907 : if (tv != NULL)
8763 : : {
8764 : 21981907 : if (Type::traverse(tv, traverse) == TRAVERSE_EXIT)
8765 : : return TRAVERSE_EXIT;
8766 : : }
8767 : : }
8768 : : }
8769 : 28035276 : if (t == TRAVERSE_CONTINUE
8770 : 28035276 : && (traverse_mask & e_or_t_or_s) != 0
8771 : 26309372 : && this->is_variable())
8772 : 21642586 : t = this->var_value()->traverse_expression(traverse,
8773 : : traverse_mask);
8774 : : break;
8775 : :
8776 : 4278636 : case Named_object::NAMED_OBJECT_FUNC:
8777 : 4278636 : if ((traverse_mask & Traverse::traverse_functions) != 0)
8778 : 2368416 : t = traverse->function(this);
8779 : 2368416 : if (t == TRAVERSE_CONTINUE
8780 : 3029420 : && (traverse_mask
8781 : 3029420 : & (Traverse::traverse_variables
8782 : : | Traverse::traverse_constants
8783 : : | Traverse::traverse_functions
8784 : : | Traverse::traverse_blocks
8785 : : | Traverse::traverse_statements
8786 : : | Traverse::traverse_expressions
8787 : : | Traverse::traverse_types)) != 0)
8788 : 3029420 : t = this->func_value()->traverse(traverse);
8789 : : break;
8790 : :
8791 : 13088177 : case Named_object::NAMED_OBJECT_TYPE:
8792 : 13088177 : if ((traverse_mask & e_or_t) != 0)
8793 : 8060373 : t = Type::traverse(this->type_value(), traverse);
8794 : : break;
8795 : :
8796 : : case Named_object::NAMED_OBJECT_PACKAGE:
8797 : : case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
8798 : : case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
8799 : : case Named_object::NAMED_OBJECT_UNKNOWN:
8800 : : case Named_object::NAMED_OBJECT_ERRONEOUS:
8801 : : break;
8802 : :
8803 : 0 : case Named_object::NAMED_OBJECT_SINK:
8804 : 0 : go_unreachable();
8805 : :
8806 : 0 : default:
8807 : 0 : go_unreachable();
8808 : : }
8809 : :
8810 : : return t;
8811 : : }
8812 : :
8813 : : // Export a named object.
8814 : :
8815 : : void
8816 : 428123 : Named_object::export_named_object(Export* exp) const
8817 : : {
8818 : 428123 : switch (this->classification_)
8819 : : {
8820 : 0 : default:
8821 : 0 : case NAMED_OBJECT_UNINITIALIZED:
8822 : 0 : case NAMED_OBJECT_UNKNOWN:
8823 : 0 : go_unreachable();
8824 : :
8825 : : case NAMED_OBJECT_ERRONEOUS:
8826 : : break;
8827 : :
8828 : 39159 : case NAMED_OBJECT_CONST:
8829 : 39159 : this->const_value()->export_const(exp, this->name_);
8830 : 39159 : break;
8831 : :
8832 : 0 : case NAMED_OBJECT_TYPE:
8833 : : // Types are handled by export::write_types.
8834 : 0 : go_unreachable();
8835 : :
8836 : 0 : case NAMED_OBJECT_TYPE_DECLARATION:
8837 : 0 : go_error_at(this->type_declaration_value()->location(),
8838 : : "attempt to export %<%s%> which was declared but not defined",
8839 : 0 : this->message_name().c_str());
8840 : 0 : break;
8841 : :
8842 : 301336 : case NAMED_OBJECT_FUNC_DECLARATION:
8843 : 301336 : this->func_declaration_value()->export_func(exp, this);
8844 : 301336 : break;
8845 : :
8846 : 8438 : case NAMED_OBJECT_VAR:
8847 : 8438 : this->var_value()->export_var(exp, this);
8848 : 8438 : break;
8849 : :
8850 : 0 : case NAMED_OBJECT_RESULT_VAR:
8851 : 0 : case NAMED_OBJECT_SINK:
8852 : 0 : go_unreachable();
8853 : :
8854 : 79190 : case NAMED_OBJECT_FUNC:
8855 : 79190 : this->func_value()->export_func(exp, this);
8856 : 79190 : break;
8857 : : }
8858 : 428123 : }
8859 : :
8860 : : // Convert a variable to the backend representation.
8861 : :
8862 : : Bvariable*
8863 : 5601395 : Named_object::get_backend_variable(Gogo* gogo, Named_object* function)
8864 : : {
8865 : 5601395 : if (this->classification_ == NAMED_OBJECT_VAR)
8866 : 4440232 : return this->var_value()->get_backend_variable(gogo, function,
8867 : 4440232 : this->package_, this->name_);
8868 : 1161163 : else if (this->classification_ == NAMED_OBJECT_RESULT_VAR)
8869 : 1161163 : return this->result_var_value()->get_backend_variable(gogo, function,
8870 : 1161163 : this->name_);
8871 : : else
8872 : 0 : go_unreachable();
8873 : : }
8874 : :
8875 : : void
8876 : 0 : debug_go_named_object(Named_object* no)
8877 : : {
8878 : 0 : if (no == NULL)
8879 : : {
8880 : 0 : std::cerr << "<null>";
8881 : 0 : return;
8882 : : }
8883 : 0 : std::cerr << "'" << no->name() << "': ";
8884 : 0 : const char *tag;
8885 : 0 : switch (no->classification())
8886 : : {
8887 : : case Named_object::NAMED_OBJECT_UNINITIALIZED:
8888 : : tag = "uninitialized";
8889 : : break;
8890 : 0 : case Named_object::NAMED_OBJECT_ERRONEOUS:
8891 : 0 : tag = "<error>";
8892 : 0 : break;
8893 : 0 : case Named_object::NAMED_OBJECT_UNKNOWN:
8894 : 0 : tag = "<unknown>";
8895 : 0 : break;
8896 : 0 : case Named_object::NAMED_OBJECT_CONST:
8897 : 0 : tag = "constant";
8898 : 0 : break;
8899 : 0 : case Named_object::NAMED_OBJECT_TYPE:
8900 : 0 : tag = "type";
8901 : 0 : break;
8902 : 0 : case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
8903 : 0 : tag = "type_decl";
8904 : 0 : break;
8905 : 0 : case Named_object::NAMED_OBJECT_VAR:
8906 : 0 : tag = "var";
8907 : 0 : break;
8908 : 0 : case Named_object::NAMED_OBJECT_RESULT_VAR:
8909 : 0 : tag = "result_var";
8910 : 0 : break;
8911 : 0 : case Named_object::NAMED_OBJECT_SINK:
8912 : 0 : tag = "<sink>";
8913 : 0 : break;
8914 : 0 : case Named_object::NAMED_OBJECT_FUNC:
8915 : 0 : tag = "func";
8916 : 0 : break;
8917 : 0 : case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
8918 : 0 : tag = "func_decl";
8919 : 0 : break;
8920 : 0 : case Named_object::NAMED_OBJECT_PACKAGE:
8921 : 0 : tag = "package";
8922 : 0 : break;
8923 : 0 : default:
8924 : 0 : tag = "<unknown named object classification>";
8925 : 0 : break;
8926 : 0 : };
8927 : 0 : std::cerr << tag << "\n";
8928 : : }
8929 : :
8930 : : // Get the backend representation for this named object.
8931 : :
8932 : : void
8933 : 349362 : Named_object::get_backend(Gogo* gogo, std::vector<Bexpression*>& const_decls,
8934 : : std::vector<Btype*>& type_decls,
8935 : : std::vector<Bfunction*>& func_decls)
8936 : : {
8937 : : // If this is a definition, avoid trying to get the backend
8938 : : // representation, as that can crash.
8939 : 349362 : if (this->is_redefinition_)
8940 : : {
8941 : 18 : go_assert(saw_errors());
8942 : : return;
8943 : : }
8944 : :
8945 : 349344 : switch (this->classification_)
8946 : : {
8947 : 23399 : case NAMED_OBJECT_CONST:
8948 : 23399 : if (!Gogo::is_erroneous_name(this->name_))
8949 : 23399 : const_decls.push_back(this->u_.const_value->get_backend(gogo, this));
8950 : : break;
8951 : :
8952 : 30643 : case NAMED_OBJECT_TYPE:
8953 : 30643 : {
8954 : 30643 : Named_type* named_type = this->u_.type_value;
8955 : :
8956 : : // No need to do anything for aliases-- whatever has to be done
8957 : : // can be done for the alias target.
8958 : 30643 : if (named_type->is_alias())
8959 : : break;
8960 : :
8961 : 30349 : if (!Gogo::is_erroneous_name(this->name_))
8962 : 30349 : type_decls.push_back(named_type->get_backend(gogo));
8963 : :
8964 : : // We need to produce a type descriptor for every named
8965 : : // type, and for a pointer to every named type, since
8966 : : // other files or packages might refer to them. We need
8967 : : // to do this even for hidden types, because they might
8968 : : // still be returned by some function. Simply calling the
8969 : : // type_descriptor method is enough to create the type
8970 : : // descriptor, even though we don't do anything with it.
8971 : 30349 : if (this->package_ == NULL && !saw_errors())
8972 : : {
8973 : 29531 : named_type->
8974 : 29531 : type_descriptor_pointer(gogo, Linemap::predeclared_location());
8975 : 29531 : Type* pn = Type::make_pointer_type(named_type);
8976 : 29531 : pn->type_descriptor_pointer(gogo, Linemap::predeclared_location());
8977 : 29531 : if (named_type->in_heap())
8978 : : {
8979 : 29306 : named_type->gc_symbol_pointer(gogo);
8980 : 29306 : pn->gc_symbol_pointer(gogo);
8981 : : }
8982 : : }
8983 : : }
8984 : : break;
8985 : :
8986 : 0 : case NAMED_OBJECT_TYPE_DECLARATION:
8987 : 0 : go_error_at(Linemap::unknown_location(),
8988 : : "reference to undefined type %qs",
8989 : 0 : this->message_name().c_str());
8990 : 0 : return;
8991 : :
8992 : 0 : case NAMED_OBJECT_VAR:
8993 : 0 : case NAMED_OBJECT_RESULT_VAR:
8994 : 0 : case NAMED_OBJECT_SINK:
8995 : 0 : go_unreachable();
8996 : :
8997 : 295289 : case NAMED_OBJECT_FUNC:
8998 : 295289 : {
8999 : 295289 : Function* func = this->u_.func_value;
9000 : 295289 : if (!Gogo::is_erroneous_name(this->name_))
9001 : 295289 : func_decls.push_back(func->get_or_make_decl(gogo, this));
9002 : :
9003 : 295289 : if (func->block() != NULL)
9004 : 295289 : func->build(gogo, this);
9005 : : }
9006 : : break;
9007 : :
9008 : : case NAMED_OBJECT_ERRONEOUS:
9009 : : break;
9010 : :
9011 : 0 : default:
9012 : 0 : go_unreachable();
9013 : : }
9014 : : }
9015 : :
9016 : : // Class Bindings.
9017 : :
9018 : 3141408 : Bindings::Bindings(Bindings* enclosing)
9019 : 3141408 : : enclosing_(enclosing), named_objects_(), bindings_()
9020 : : {
9021 : 3141408 : }
9022 : :
9023 : : // Clear imports.
9024 : :
9025 : : void
9026 : 12703 : Bindings::clear_file_scope(Gogo* gogo)
9027 : : {
9028 : 12703 : Contour::iterator p = this->bindings_.begin();
9029 : 3412803 : while (p != this->bindings_.end())
9030 : : {
9031 : 3400100 : bool keep;
9032 : 3400100 : if (p->second->package() != NULL)
9033 : : keep = false;
9034 : 3337599 : else if (p->second->is_package())
9035 : : keep = false;
9036 : 3298184 : else if (p->second->is_function()
9037 : 1308338 : && !p->second->func_value()->type()->is_method()
9038 : 6596368 : && Gogo::unpack_hidden_name(p->second->name()) == "init")
9039 : : keep = false;
9040 : : else
9041 : 3298184 : keep = true;
9042 : :
9043 : 3298184 : if (keep)
9044 : 3298184 : ++p;
9045 : : else
9046 : : {
9047 : 101916 : gogo->add_file_block_name(p->second->name(), p->second->location());
9048 : 101916 : p = this->bindings_.erase(p);
9049 : : }
9050 : : }
9051 : 12703 : }
9052 : :
9053 : : // Look up a symbol.
9054 : :
9055 : : Named_object*
9056 : 8264187 : Bindings::lookup(const std::string& name) const
9057 : : {
9058 : 13214091 : Contour::const_iterator p = this->bindings_.find(name);
9059 : 13214091 : if (p != this->bindings_.end())
9060 : 5473415 : return p->second->resolve();
9061 : 7740676 : else if (this->enclosing_ != NULL)
9062 : : return this->enclosing_->lookup(name);
9063 : : else
9064 : : return NULL;
9065 : : }
9066 : :
9067 : : // Look up a symbol locally.
9068 : :
9069 : : Named_object*
9070 : 643805 : Bindings::lookup_local(const std::string& name) const
9071 : : {
9072 : 643805 : Contour::const_iterator p = this->bindings_.find(name);
9073 : 643805 : if (p == this->bindings_.end())
9074 : : return NULL;
9075 : 334417 : return p->second;
9076 : : }
9077 : :
9078 : : // Remove an object from a set of bindings. This is used for a
9079 : : // special case in thunks for functions which call recover.
9080 : :
9081 : : void
9082 : 831 : Bindings::remove_binding(Named_object* no)
9083 : : {
9084 : 831 : Contour::iterator pb = this->bindings_.find(no->name());
9085 : 831 : go_assert(pb != this->bindings_.end());
9086 : 831 : this->bindings_.erase(pb);
9087 : 1048 : for (std::vector<Named_object*>::iterator pn = this->named_objects_.begin();
9088 : 1048 : pn != this->named_objects_.end();
9089 : 217 : ++pn)
9090 : : {
9091 : 1048 : if (*pn == no)
9092 : : {
9093 : 831 : this->named_objects_.erase(pn);
9094 : 831 : return;
9095 : : }
9096 : : }
9097 : 0 : go_unreachable();
9098 : : }
9099 : :
9100 : : // Add a method to the list of objects. This is not added to the
9101 : : // lookup table. This is so that we have a single list of objects
9102 : : // declared at the top level, which we walk through when it's time to
9103 : : // convert to trees.
9104 : :
9105 : : void
9106 : 78041 : Bindings::add_method(Named_object* method)
9107 : : {
9108 : 78041 : this->named_objects_.push_back(method);
9109 : 78041 : }
9110 : :
9111 : : // Add a generic Named_object to a Contour.
9112 : :
9113 : : Named_object*
9114 : 7456600 : Bindings::add_named_object_to_contour(Contour* contour,
9115 : : Named_object* named_object)
9116 : : {
9117 : 7456600 : go_assert(named_object == named_object->resolve());
9118 : 7456600 : const std::string& name(named_object->name());
9119 : 7456600 : go_assert(!Gogo::is_sink_name(name));
9120 : :
9121 : 7456600 : std::pair<Contour::iterator, bool> ins =
9122 : 7456600 : contour->insert(std::make_pair(name, named_object));
9123 : 7456600 : if (!ins.second)
9124 : : {
9125 : : // The name was already there.
9126 : 825752 : if (named_object->package() != NULL
9127 : 675032 : && ins.first->second->package() == named_object->package()
9128 : 1500784 : && (ins.first->second->classification()
9129 : 675032 : == named_object->classification()))
9130 : : {
9131 : : // This is a second import of the same object.
9132 : 76556 : return ins.first->second;
9133 : : }
9134 : 749196 : ins.first->second = this->new_definition(ins.first->second,
9135 : : named_object);
9136 : 749196 : return ins.first->second;
9137 : : }
9138 : : else
9139 : : {
9140 : : // Don't push declarations on the list. We push them on when
9141 : : // and if we find the definitions. That way we genericize the
9142 : : // functions in order.
9143 : 6630848 : if (!named_object->is_type_declaration()
9144 : : && !named_object->is_function_declaration()
9145 : : && !named_object->is_unknown())
9146 : 2729494 : this->named_objects_.push_back(named_object);
9147 : 6630848 : return named_object;
9148 : : }
9149 : : }
9150 : :
9151 : : // We had an existing named object OLD_OBJECT, and we've seen a new
9152 : : // one NEW_OBJECT with the same name. FIXME: This does not free the
9153 : : // new object when we don't need it.
9154 : :
9155 : : Named_object*
9156 : 749196 : Bindings::new_definition(Named_object* old_object, Named_object* new_object)
9157 : : {
9158 : 749196 : if (new_object->is_erroneous() && !old_object->is_erroneous())
9159 : : return new_object;
9160 : :
9161 : 749196 : std::string reason;
9162 : 749196 : switch (old_object->classification())
9163 : : {
9164 : 0 : default:
9165 : 0 : case Named_object::NAMED_OBJECT_UNINITIALIZED:
9166 : 0 : go_unreachable();
9167 : :
9168 : : case Named_object::NAMED_OBJECT_ERRONEOUS:
9169 : : return old_object;
9170 : :
9171 : 55797 : case Named_object::NAMED_OBJECT_UNKNOWN:
9172 : 55797 : {
9173 : 55797 : Named_object* real = old_object->unknown_value()->real_named_object();
9174 : 55797 : if (real != NULL)
9175 : 0 : return this->new_definition(real, new_object);
9176 : 55797 : go_assert(!new_object->is_unknown());
9177 : 55797 : old_object->unknown_value()->set_real_named_object(new_object);
9178 : 55797 : if (!new_object->is_type_declaration()
9179 : 55797 : && !new_object->is_function_declaration())
9180 : 44463 : this->named_objects_.push_back(new_object);
9181 : : return new_object;
9182 : : }
9183 : :
9184 : : case Named_object::NAMED_OBJECT_CONST:
9185 : : break;
9186 : :
9187 : 0 : case Named_object::NAMED_OBJECT_TYPE:
9188 : 0 : if (new_object->is_type_declaration())
9189 : : return old_object;
9190 : : break;
9191 : :
9192 : 586745 : case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
9193 : 586745 : if (new_object->is_type_declaration())
9194 : : return old_object;
9195 : 586229 : if (new_object->is_type())
9196 : : {
9197 : 586229 : old_object->set_type_value(new_object->type_value());
9198 : 586229 : new_object->type_value()->set_named_object(old_object);
9199 : 586229 : this->named_objects_.push_back(old_object);
9200 : 586229 : return old_object;
9201 : : }
9202 : : break;
9203 : :
9204 : 13 : case Named_object::NAMED_OBJECT_VAR:
9205 : 13 : case Named_object::NAMED_OBJECT_RESULT_VAR:
9206 : : // We have already given an error in the parser for cases where
9207 : : // one parameter or result variable redeclares another one.
9208 : 13 : if ((new_object->is_variable()
9209 : 8 : && new_object->var_value()->is_parameter())
9210 : 18 : || new_object->is_result_variable())
9211 : : return old_object;
9212 : : break;
9213 : :
9214 : 0 : case Named_object::NAMED_OBJECT_SINK:
9215 : 0 : go_unreachable();
9216 : :
9217 : : case Named_object::NAMED_OBJECT_FUNC:
9218 : : break;
9219 : :
9220 : 106622 : case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
9221 : 106622 : {
9222 : : // We declare the hash and equality functions before defining
9223 : : // them, because we sometimes see that we need the declaration
9224 : : // while we are in the middle of a different function.
9225 : : //
9226 : : // We declare the main function before the user defines it, to
9227 : : // give better error messages.
9228 : : //
9229 : : // We declare inline functions before we define them, as we
9230 : : // only define them if we need them.
9231 : 213244 : if (new_object->is_function()
9232 : 106622 : && ((Linemap::is_predeclared_location(old_object->location())
9233 : 92607 : && Linemap::is_predeclared_location(new_object->location()))
9234 : 120635 : || (Gogo::unpack_hidden_name(old_object->name()) == "main"
9235 : 1763 : && Linemap::is_unknown_location(old_object->location()))
9236 : 12250 : || (new_object->package() != NULL
9237 : 12247 : && old_object->func_declaration_value()->has_imported_body()
9238 : 12247 : && new_object->func_value()->is_inline_only())))
9239 : : {
9240 : 106617 : Function_type* old_type =
9241 : 106617 : old_object->func_declaration_value()->type();
9242 : 106617 : Function_type* new_type = new_object->func_value()->type();
9243 : 106617 : if (old_type->is_valid_redeclaration(new_type, &reason))
9244 : : {
9245 : 106617 : Function_declaration* fd =
9246 : 106617 : old_object->func_declaration_value();
9247 : 106617 : go_assert(fd->asm_name().empty());
9248 : 106617 : old_object->set_function_value(new_object->func_value());
9249 : 106617 : this->named_objects_.push_back(old_object);
9250 : 106617 : return old_object;
9251 : : }
9252 : : }
9253 : : }
9254 : : break;
9255 : :
9256 : : case Named_object::NAMED_OBJECT_PACKAGE:
9257 : : break;
9258 : : }
9259 : :
9260 : 56 : std::string n = old_object->message_name();
9261 : 28 : if (reason.empty())
9262 : 28 : go_error_at(new_object->location(), "redefinition of %qs", n.c_str());
9263 : : else
9264 : 0 : go_error_at(new_object->location(), "redefinition of %qs: %s", n.c_str(),
9265 : : reason.c_str());
9266 : 28 : old_object->set_is_redefinition();
9267 : 28 : new_object->set_is_redefinition();
9268 : :
9269 : 28 : if (!Linemap::is_unknown_location(old_object->location())
9270 : 28 : && !Linemap::is_predeclared_location(old_object->location()))
9271 : 27 : go_inform(old_object->location(), "previous definition of %qs was here",
9272 : : n.c_str());
9273 : :
9274 : 28 : return old_object;
9275 : 749196 : }
9276 : :
9277 : : // Add a named type.
9278 : :
9279 : : Named_object*
9280 : 97515 : Bindings::add_named_type(Named_type* named_type)
9281 : : {
9282 : 97515 : return this->add_named_object(named_type->named_object());
9283 : : }
9284 : :
9285 : : // Add a function.
9286 : :
9287 : : Named_object*
9288 : 268508 : Bindings::add_function(const std::string& name, const Package* package,
9289 : : Function* function)
9290 : : {
9291 : 268508 : return this->add_named_object(Named_object::make_function(name, package,
9292 : 268508 : function));
9293 : : }
9294 : :
9295 : : // Add a function declaration.
9296 : :
9297 : : Named_object*
9298 : 3241446 : Bindings::add_function_declaration(const std::string& name,
9299 : : const Package* package,
9300 : : Function_type* type,
9301 : : Location location)
9302 : : {
9303 : 3241446 : Named_object* no = Named_object::make_function_declaration(name, package,
9304 : : type, location);
9305 : 3241446 : return this->add_named_object(no);
9306 : : }
9307 : :
9308 : : // Define a type which was previously declared.
9309 : :
9310 : : void
9311 : 31938 : Bindings::define_type(Named_object* no, Named_type* type)
9312 : : {
9313 : 31938 : no->set_type_value(type);
9314 : 31938 : this->named_objects_.push_back(no);
9315 : 31938 : }
9316 : :
9317 : : // Mark all local variables as used. This is used for some types of
9318 : : // parse error.
9319 : :
9320 : : void
9321 : 56 : Bindings::mark_locals_used()
9322 : : {
9323 : 72 : for (std::vector<Named_object*>::iterator p = this->named_objects_.begin();
9324 : 72 : p != this->named_objects_.end();
9325 : 16 : ++p)
9326 : 16 : if ((*p)->is_variable())
9327 : 12 : (*p)->var_value()->set_is_used();
9328 : 56 : }
9329 : :
9330 : : // Traverse bindings.
9331 : :
9332 : : int
9333 : 2404012 : Bindings::traverse(Traverse* traverse, bool is_global)
9334 : : {
9335 : 2404012 : unsigned int traverse_mask = traverse->traverse_mask();
9336 : :
9337 : : // We don't use an iterator because we permit the traversal to add
9338 : : // new global objects.
9339 : 2404012 : const unsigned int e_or_t = (Traverse::traverse_expressions
9340 : : | Traverse::traverse_types);
9341 : 48269215 : for (size_t i = 0; i < this->named_objects_.size(); ++i)
9342 : : {
9343 : 45865203 : Named_object* p = this->named_objects_[i];
9344 : 45865203 : if (p->traverse(traverse, is_global) == TRAVERSE_EXIT)
9345 : : return TRAVERSE_EXIT;
9346 : : }
9347 : :
9348 : : // If we need to traverse types, check the function declarations,
9349 : : // which have types. Also check any methods of a type declaration.
9350 : 2404012 : if ((traverse_mask & e_or_t) != 0)
9351 : : {
9352 : 43601951 : for (Bindings::const_declarations_iterator p =
9353 : 1481113 : this->begin_declarations();
9354 : 43601951 : p != this->end_declarations();
9355 : 42120838 : ++p)
9356 : : {
9357 : 42120838 : if (p->second->is_function_declaration())
9358 : : {
9359 : 15382861 : if (Type::traverse(p->second->func_declaration_value()->type(),
9360 : : traverse)
9361 : : == TRAVERSE_EXIT)
9362 : 2404012 : return TRAVERSE_EXIT;
9363 : : }
9364 : 26737977 : else if (p->second->is_type_declaration())
9365 : : {
9366 : 20 : const std::vector<Named_object*>* methods =
9367 : 20 : p->second->type_declaration_value()->methods();
9368 : 40 : for (std::vector<Named_object*>::const_iterator pm =
9369 : 20 : methods->begin();
9370 : 40 : pm != methods->end();
9371 : 20 : pm++)
9372 : : {
9373 : 20 : Named_object* no = *pm;
9374 : 20 : Type *t;
9375 : 20 : if (no->is_function())
9376 : 20 : t = no->func_value()->type();
9377 : 0 : else if (no->is_function_declaration())
9378 : 0 : t = no->func_declaration_value()->type();
9379 : : else
9380 : 0 : continue;
9381 : 20 : if (Type::traverse(t, traverse) == TRAVERSE_EXIT)
9382 : 0 : return TRAVERSE_EXIT;
9383 : : }
9384 : : }
9385 : : }
9386 : : }
9387 : :
9388 : : // Traverse function declarations when needed.
9389 : 2404012 : if ((traverse_mask & Traverse::traverse_func_declarations) != 0)
9390 : : {
9391 : 3275998 : for (Bindings::const_declarations_iterator p = this->begin_declarations();
9392 : 3275998 : p != this->end_declarations();
9393 : 3163451 : ++p)
9394 : : {
9395 : 3163451 : if (p->second->is_function_declaration())
9396 : : {
9397 : 1148816 : if (traverse->function_declaration(p->second) == TRAVERSE_EXIT)
9398 : 0 : return TRAVERSE_EXIT;
9399 : : }
9400 : : }
9401 : : }
9402 : :
9403 : : return TRAVERSE_CONTINUE;
9404 : : }
9405 : :
9406 : : // Determine types for the objects.
9407 : :
9408 : : void
9409 : 4642 : Bindings::determine_types(Gogo* gogo)
9410 : : {
9411 : : // We don't use an iterator because the traversal can add new
9412 : : // bindings.
9413 : 453750 : for (size_t i = 0; i < this->named_objects_.size(); ++i)
9414 : : {
9415 : 449108 : Named_object* no = this->named_objects_[i];
9416 : 449108 : if (no->is_function())
9417 : 175573 : no->func_value()->determine_types(gogo);
9418 : 273535 : else if (no->is_variable())
9419 : 36699 : no->var_value()->determine_type(gogo);
9420 : 236836 : else if (no->is_const())
9421 : 152947 : no->const_value()->determine_type(gogo);
9422 : :
9423 : : // See if a variable requires us to build an initialization
9424 : : // function. We know that we will see all global variables
9425 : : // here.
9426 : 449108 : if (!gogo->need_init_fn() && no->is_variable())
9427 : : {
9428 : 2937 : Variable* variable = no->var_value();
9429 : :
9430 : : // If this is a global variable which requires runtime
9431 : : // initialization, we need an initialization function.
9432 : :
9433 : 2937 : if (!variable->is_global())
9434 : 0 : continue;
9435 : :
9436 : 2937 : if (variable->init() == NULL)
9437 : : ;
9438 : 2258 : else if (variable->type()->interface_type() != NULL)
9439 : 253 : gogo->set_need_init_fn();
9440 : 2005 : else if (variable->init()->is_constant())
9441 : : ;
9442 : 1573 : else if (!variable->init()->is_composite_literal())
9443 : 494 : gogo->set_need_init_fn();
9444 : 1079 : else if (variable->init()->is_nonconstant_composite_literal())
9445 : 0 : gogo->set_need_init_fn();
9446 : :
9447 : : // If this global variable holds a pointer value, we need an
9448 : : // initialization function to register it as a GC root.
9449 : 2937 : if (variable->type()->has_pointer())
9450 : 1716 : gogo->set_need_init_fn();
9451 : : }
9452 : : }
9453 : 4642 : }
9454 : :
9455 : : void
9456 : 0 : Bindings::debug_dump()
9457 : : {
9458 : 0 : std::set<Named_object*> defs;
9459 : 0 : for (size_t i = 0; i < this->named_objects_.size(); ++i)
9460 : 0 : defs.insert(this->named_objects_[i]);
9461 : 0 : for (Contour::iterator p = this->bindings_.begin();
9462 : 0 : p != this->bindings_.end();
9463 : 0 : ++p)
9464 : : {
9465 : 0 : const char* tag = " ";
9466 : 0 : if (defs.find(p->second) != defs.end())
9467 : 0 : tag = "* ";
9468 : 0 : std::cerr << tag;
9469 : 0 : debug_go_named_object(p->second);
9470 : : }
9471 : 0 : }
9472 : :
9473 : : void
9474 : 0 : debug_go_bindings(Bindings* bindings)
9475 : : {
9476 : 0 : if (bindings != NULL)
9477 : 0 : bindings->debug_dump();
9478 : 0 : }
9479 : :
9480 : : // Class Label.
9481 : :
9482 : : // Clear any references to this label.
9483 : :
9484 : : void
9485 : 10524 : Label::clear_refs()
9486 : : {
9487 : 12089 : for (std::vector<Bindings_snapshot*>::iterator p = this->refs_.begin();
9488 : 12089 : p != this->refs_.end();
9489 : 1565 : ++p)
9490 : 3130 : delete *p;
9491 : 10524 : this->refs_.clear();
9492 : 10524 : }
9493 : :
9494 : : // Get the backend representation for a label.
9495 : :
9496 : : Blabel*
9497 : 30094 : Label::get_backend_label(Translate_context* context)
9498 : : {
9499 : 30094 : if (this->blabel_ == NULL)
9500 : : {
9501 : 10412 : Function* function = context->function()->func_value();
9502 : 10412 : Bfunction* bfunction = function->get_decl();
9503 : 10412 : this->blabel_ = context->backend()->label(bfunction, this->name_,
9504 : : this->location_);
9505 : : }
9506 : 30094 : return this->blabel_;
9507 : : }
9508 : :
9509 : : // Return an expression for the address of this label.
9510 : :
9511 : : Bexpression*
9512 : 8840 : Label::get_addr(Translate_context* context, Location location)
9513 : : {
9514 : 8840 : Blabel* label = this->get_backend_label(context);
9515 : 8840 : return context->backend()->label_address(label, location);
9516 : : }
9517 : :
9518 : : // Return the dummy label that represents any instance of the blank label.
9519 : :
9520 : : Label*
9521 : 3 : Label::create_dummy_label()
9522 : : {
9523 : 3 : static Label* dummy_label;
9524 : 3 : if (dummy_label == NULL)
9525 : : {
9526 : 2 : dummy_label = new Label("_");
9527 : 2 : dummy_label->set_is_used();
9528 : : }
9529 : 3 : return dummy_label;
9530 : : }
9531 : :
9532 : : // Class Unnamed_label.
9533 : :
9534 : : // Get the backend representation for an unnamed label.
9535 : :
9536 : : Blabel*
9537 : 398183 : Unnamed_label::get_blabel(Translate_context* context)
9538 : : {
9539 : 398183 : if (this->blabel_ == NULL)
9540 : : {
9541 : 174393 : Function* function = context->function()->func_value();
9542 : 174393 : Bfunction* bfunction = function->get_decl();
9543 : 174393 : this->blabel_ = context->backend()->label(bfunction, "",
9544 : : this->location_);
9545 : : }
9546 : 398183 : return this->blabel_;
9547 : : }
9548 : :
9549 : : // Return a statement which defines this unnamed label.
9550 : :
9551 : : Bstatement*
9552 : 174393 : Unnamed_label::get_definition(Translate_context* context)
9553 : : {
9554 : 174393 : Blabel* blabel = this->get_blabel(context);
9555 : 174393 : return context->backend()->label_definition_statement(blabel);
9556 : : }
9557 : :
9558 : : // Return a goto statement to this unnamed label.
9559 : :
9560 : : Bstatement*
9561 : 223790 : Unnamed_label::get_goto(Translate_context* context, Location location)
9562 : : {
9563 : 223790 : Blabel* blabel = this->get_blabel(context);
9564 : 223790 : return context->backend()->goto_statement(blabel, location);
9565 : : }
9566 : :
9567 : : // Class Package.
9568 : :
9569 : 116626 : Package::Package(const std::string& pkgpath,
9570 : 116626 : const std::string& pkgpath_symbol, Location location)
9571 : 116626 : : pkgpath_(pkgpath), pkgpath_symbol_(pkgpath_symbol),
9572 : 116626 : package_name_(), bindings_(new Bindings(NULL)),
9573 : 116626 : location_(location)
9574 : : {
9575 : 116626 : go_assert(!pkgpath.empty());
9576 : 116626 : }
9577 : :
9578 : : // Set the package name.
9579 : :
9580 : : void
9581 : 244809 : Package::set_package_name(const std::string& package_name, Location location)
9582 : : {
9583 : 244809 : go_assert(!package_name.empty());
9584 : 244809 : if (this->package_name_.empty())
9585 : 116626 : this->package_name_ = package_name;
9586 : 128183 : else if (this->package_name_ != package_name)
9587 : 0 : go_error_at(location,
9588 : : ("saw two different packages with "
9589 : : "the same package path %s: %s, %s"),
9590 : : this->pkgpath_.c_str(), this->package_name_.c_str(),
9591 : : package_name.c_str());
9592 : 244809 : }
9593 : :
9594 : : // Return the pkgpath symbol, which is a prefix for symbols defined in
9595 : : // this package.
9596 : :
9597 : : std::string
9598 : 194258 : Package::pkgpath_symbol() const
9599 : : {
9600 : 194258 : if (this->pkgpath_symbol_.empty())
9601 : 101662 : return Gogo::pkgpath_for_symbol(this->pkgpath_);
9602 : 92596 : return this->pkgpath_symbol_;
9603 : : }
9604 : :
9605 : : // Set the package path symbol.
9606 : :
9607 : : void
9608 : 14338 : Package::set_pkgpath_symbol(const std::string& pkgpath_symbol)
9609 : : {
9610 : 14338 : go_assert(!pkgpath_symbol.empty());
9611 : 14338 : if (this->pkgpath_symbol_.empty())
9612 : 11763 : this->pkgpath_symbol_ = pkgpath_symbol;
9613 : : else
9614 : 2575 : go_assert(this->pkgpath_symbol_ == pkgpath_symbol);
9615 : 14338 : }
9616 : :
9617 : : // Note that symbol from this package was and qualified by ALIAS.
9618 : :
9619 : : void
9620 : 322224 : Package::note_usage(const std::string& alias) const
9621 : : {
9622 : 322224 : Aliases::const_iterator p = this->aliases_.find(alias);
9623 : 322224 : go_assert(p != this->aliases_.end());
9624 : 322224 : p->second->note_usage();
9625 : 322224 : }
9626 : :
9627 : : // Forget a given usage. If forgetting this usage means this package becomes
9628 : : // unused, report that error.
9629 : :
9630 : : void
9631 : 39 : Package::forget_usage(Expression* usage) const
9632 : : {
9633 : 39 : if (this->fake_uses_.empty())
9634 : 39 : return;
9635 : :
9636 : 0 : std::set<Expression*>::iterator p = this->fake_uses_.find(usage);
9637 : 0 : go_assert(p != this->fake_uses_.end());
9638 : 0 : this->fake_uses_.erase(p);
9639 : :
9640 : 0 : if (this->fake_uses_.empty())
9641 : 0 : go_error_at(this->location(), "imported and not used: %s",
9642 : 0 : Gogo::message_name(this->package_name()).c_str());
9643 : : }
9644 : :
9645 : : // Clear the used field for the next file. If the only usages of this package
9646 : : // are possibly fake, keep the fake usages for lowering.
9647 : :
9648 : : void
9649 : 362493 : Package::clear_used()
9650 : : {
9651 : 362493 : std::string dot_alias = "." + this->package_name();
9652 : 362493 : Aliases::const_iterator p = this->aliases_.find(dot_alias);
9653 : 362493 : if (p != this->aliases_.end() && p->second->used() > this->fake_uses_.size())
9654 : 335 : this->fake_uses_.clear();
9655 : :
9656 : 362493 : this->aliases_.clear();
9657 : 362493 : }
9658 : :
9659 : : Package_alias*
9660 : 39756 : Package::add_alias(const std::string& alias, Location location)
9661 : : {
9662 : 39756 : Aliases::const_iterator p = this->aliases_.find(alias);
9663 : 39756 : if (p == this->aliases_.end())
9664 : : {
9665 : 39756 : std::pair<Aliases::iterator, bool> ret;
9666 : 39756 : ret = this->aliases_.insert(std::make_pair(alias,
9667 : 79512 : new Package_alias(location)));
9668 : 39756 : p = ret.first;
9669 : : }
9670 : 39756 : return p->second;
9671 : : }
9672 : :
9673 : : // Determine types of constants. Everything else in a package
9674 : : // (variables, function declarations) should already have a fixed
9675 : : // type. Constants may have abstract types.
9676 : :
9677 : : void
9678 : 116229 : Package::determine_types(Gogo* gogo)
9679 : : {
9680 : 116229 : Bindings* bindings = this->bindings_;
9681 : 2293227 : for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
9682 : 2293227 : p != bindings->end_definitions();
9683 : 2176998 : ++p)
9684 : : {
9685 : 2176998 : if ((*p)->is_const())
9686 : 979117 : (*p)->const_value()->determine_type(gogo);
9687 : : }
9688 : 116229 : }
9689 : :
9690 : : // Class Traverse.
9691 : :
9692 : : // Destructor.
9693 : :
9694 : 17851257 : Traverse::~Traverse()
9695 : : {
9696 : 17851257 : if (this->types_seen_ != NULL)
9697 : 16803418 : delete this->types_seen_;
9698 : 17851257 : if (this->expressions_seen_ != NULL)
9699 : 1342330 : delete this->expressions_seen_;
9700 : 17851257 : }
9701 : :
9702 : : // Record that we are looking at a type, and return true if we have
9703 : : // already seen it.
9704 : :
9705 : : bool
9706 : 1185601201 : Traverse::remember_type(const Type* type)
9707 : : {
9708 : 1185601201 : if (type->is_error_type())
9709 : : return true;
9710 : 1185591090 : go_assert((this->traverse_mask() & traverse_types) != 0
9711 : : || (this->traverse_mask() & traverse_expressions) != 0);
9712 : : // We mostly only have to remember named types. But it turns out
9713 : : // that an interface type can refer to itself without using a name
9714 : : // by relying on interface inheritance, as in
9715 : : //
9716 : : // type I interface { F() interface{I} }
9717 : : //
9718 : : // Similarly it is possible for array types to refer to themselves
9719 : : // without a name, e.g.
9720 : : //
9721 : : // var x [uintptr(unsafe.Sizeof(&x))]byte
9722 : : //
9723 : 1185591090 : if (type->classification() != Type::TYPE_NAMED
9724 : 601431237 : && type->classification() != Type::TYPE_ARRAY
9725 : 1707284122 : && type->classification() != Type::TYPE_INTERFACE)
9726 : : return false;
9727 : 677750899 : if (this->types_seen_ == NULL)
9728 : 8401709 : this->types_seen_ = new Types_seen();
9729 : 677750899 : std::pair<Types_seen::iterator, bool> ins = this->types_seen_->insert(type);
9730 : 677750899 : return !ins.second;
9731 : : }
9732 : :
9733 : : // Record that we are looking at an expression, and return true if we
9734 : : // have already seen it. NB: this routine used to assert if the traverse
9735 : : // mask did not include expressions/types -- this is no longer the case,
9736 : : // since it can be useful to remember specific expressions during
9737 : : // walks that only cover statements.
9738 : :
9739 : : bool
9740 : 3237533 : Traverse::remember_expression(const Expression* expression)
9741 : : {
9742 : 3237533 : if (this->expressions_seen_ == NULL)
9743 : 671165 : this->expressions_seen_ = new Expressions_seen();
9744 : 3237533 : std::pair<Expressions_seen::iterator, bool> ins =
9745 : 3237533 : this->expressions_seen_->insert(expression);
9746 : 3237533 : return !ins.second;
9747 : : }
9748 : :
9749 : : // The default versions of these functions should never be called: the
9750 : : // traversal mask indicates which functions may be called.
9751 : :
9752 : : int
9753 : 0 : Traverse::variable(Named_object*)
9754 : : {
9755 : 0 : go_unreachable();
9756 : : }
9757 : :
9758 : : int
9759 : 0 : Traverse::constant(Named_object*, bool)
9760 : : {
9761 : 0 : go_unreachable();
9762 : : }
9763 : :
9764 : : int
9765 : 0 : Traverse::function(Named_object*)
9766 : : {
9767 : 0 : go_unreachable();
9768 : : }
9769 : :
9770 : : int
9771 : 0 : Traverse::block(Block*)
9772 : : {
9773 : 0 : go_unreachable();
9774 : : }
9775 : :
9776 : : int
9777 : 0 : Traverse::statement(Block*, size_t*, Statement*)
9778 : : {
9779 : 0 : go_unreachable();
9780 : : }
9781 : :
9782 : : int
9783 : 0 : Traverse::expression(Expression**)
9784 : : {
9785 : 0 : go_unreachable();
9786 : : }
9787 : :
9788 : : int
9789 : 0 : Traverse::type(Type*)
9790 : : {
9791 : 0 : go_unreachable();
9792 : : }
9793 : :
9794 : : int
9795 : 0 : Traverse::function_declaration(Named_object*)
9796 : : {
9797 : 0 : go_unreachable();
9798 : : }
9799 : :
9800 : : // Class Statement_inserter.
9801 : :
9802 : : void
9803 : 1159920 : Statement_inserter::insert(Statement* s)
9804 : : {
9805 : 1159920 : if (this->statements_added_ != NULL)
9806 : 177900 : this->statements_added_->insert(s);
9807 : :
9808 : 1159920 : if (this->block_ != NULL)
9809 : : {
9810 : 1128106 : go_assert(this->pindex_ != NULL);
9811 : 1128106 : this->block_->insert_statement_before(*this->pindex_, s);
9812 : 1128106 : ++*this->pindex_;
9813 : : }
9814 : 31814 : else if (this->var_ != NULL)
9815 : 31814 : this->var_->add_preinit_statement(this->gogo_, s);
9816 : : else
9817 : 0 : go_assert(saw_errors());
9818 : 1159920 : }
|