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