github.com/alexanius/gollvm12@v0.0.0-20230419200121-b152358b84f3/gofrontend/go/import.cc (about) 1 // import.cc -- Go frontend import declarations. 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 #include "go-system.h" 8 9 #include "filenames.h" 10 11 #include "go-c.h" 12 #include "go-diagnostics.h" 13 #include "gogo.h" 14 #include "lex.h" 15 #include "types.h" 16 #include "export.h" 17 #include "import.h" 18 19 #ifndef O_BINARY 20 #define O_BINARY 0 21 #endif 22 23 // The list of paths we search for import files. 24 25 static std::vector<std::string> search_path; 26 27 // Add a directory to the search path. This is called from the option 28 // handling language hook. 29 30 GO_EXTERN_C 31 void 32 go_add_search_path(const char* path) 33 { 34 search_path.push_back(std::string(path)); 35 } 36 37 // Find import data. This searches the file system for FILENAME and 38 // returns a pointer to a Stream object to read the data that it 39 // exports. If the file is not found, it returns NULL. 40 41 // When FILENAME is not an absolute path and does not start with ./ or 42 // ../, we use the search path provided by -I and -L options. 43 44 // When FILENAME does start with ./ or ../, we use 45 // RELATIVE_IMPORT_PATH as a prefix. 46 47 // When FILENAME does not exist, we try modifying FILENAME to find the 48 // file. We use the first of these which exists: 49 // * We append ".gox". 50 // * We turn the base of FILENAME into libFILENAME.so. 51 // * We turn the base of FILENAME into libFILENAME.a. 52 // * We append ".o". 53 54 // When using a search path, we apply each of these transformations at 55 // each entry on the search path before moving on to the next entry. 56 // If the file exists, but does not contain any Go export data, we 57 // stop; we do not keep looking for another file with the same name 58 // later in the search path. 59 60 Import::Stream* 61 Import::open_package(const std::string& filename, Location location, 62 const std::string& relative_import_path) 63 { 64 bool is_local; 65 if (IS_ABSOLUTE_PATH(filename)) 66 is_local = true; 67 else if (filename[0] == '.' 68 && (filename[1] == '\0' || IS_DIR_SEPARATOR(filename[1]))) 69 is_local = true; 70 else if (filename[0] == '.' 71 && filename[1] == '.' 72 && (filename[2] == '\0' || IS_DIR_SEPARATOR(filename[2]))) 73 is_local = true; 74 else 75 is_local = false; 76 77 std::string fn = filename; 78 if (is_local && !IS_ABSOLUTE_PATH(filename) && !relative_import_path.empty()) 79 { 80 if (fn == ".") 81 { 82 // A special case. 83 fn = relative_import_path; 84 } 85 else if (fn[0] == '.' && fn[1] == '.' 86 && (fn[2] == '\0' || IS_DIR_SEPARATOR(fn[2]))) 87 { 88 // We are going to join relative_import_path and fn, and it 89 // will look like DIR/../PATH. But DIR does not necessarily 90 // exist in this case, and if it doesn't the use of .. will 91 // fail although it shouldn't. The gc compiler uses 92 // path.Join here, which cleans up the .., so we need to do 93 // the same. 94 size_t index; 95 for (index = relative_import_path.length() - 1; 96 index > 0 && !IS_DIR_SEPARATOR(relative_import_path[index]); 97 index--) 98 ; 99 if (index > 0) 100 fn = relative_import_path.substr(0, index) + fn.substr(2); 101 else 102 fn = relative_import_path + '/' + fn; 103 } 104 else 105 fn = relative_import_path + '/' + fn; 106 is_local = false; 107 } 108 109 if (!is_local) 110 { 111 for (std::vector<std::string>::const_iterator p = search_path.begin(); 112 p != search_path.end(); 113 ++p) 114 { 115 std::string indir = *p; 116 if (!indir.empty() && indir[indir.size() - 1] != '/') 117 indir += '/'; 118 indir += fn; 119 Stream* s = Import::try_package_in_directory(indir, location); 120 if (s != NULL) 121 return s; 122 } 123 } 124 125 Stream* s = Import::try_package_in_directory(fn, location); 126 if (s != NULL) 127 return s; 128 129 return NULL; 130 } 131 132 // Try to find the export data for FILENAME. 133 134 Import::Stream* 135 Import::try_package_in_directory(const std::string& filename, 136 Location location) 137 { 138 std::string found_filename = filename; 139 int fd = open(found_filename.c_str(), O_RDONLY | O_BINARY); 140 141 if (fd >= 0) 142 { 143 struct stat s; 144 if (fstat(fd, &s) >= 0 && S_ISDIR(s.st_mode)) 145 { 146 close(fd); 147 fd = -1; 148 errno = EISDIR; 149 } 150 } 151 152 if (fd < 0) 153 { 154 if (errno != ENOENT && errno != EISDIR) 155 go_warning_at(location, 0, "%s: %m", filename.c_str()); 156 157 fd = Import::try_suffixes(&found_filename); 158 if (fd < 0) 159 return NULL; 160 } 161 162 // The export data may not be in this file. 163 Stream* s = Import::find_export_data(found_filename, fd, location); 164 if (s != NULL) 165 return s; 166 167 close(fd); 168 169 go_error_at(location, "%s exists but does not contain any Go export data", 170 found_filename.c_str()); 171 172 return NULL; 173 } 174 175 // Given import "*PFILENAME", where *PFILENAME does not exist, try 176 // various suffixes. If we find one, set *PFILENAME to the one we 177 // found. Return the open file descriptor. 178 179 int 180 Import::try_suffixes(std::string* pfilename) 181 { 182 std::string filename = *pfilename + ".gox"; 183 int fd = open(filename.c_str(), O_RDONLY | O_BINARY); 184 if (fd >= 0) 185 { 186 *pfilename = filename; 187 return fd; 188 } 189 190 const char* basename = lbasename(pfilename->c_str()); 191 size_t basename_pos = basename - pfilename->c_str(); 192 filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".so"; 193 fd = open(filename.c_str(), O_RDONLY | O_BINARY); 194 if (fd >= 0) 195 { 196 *pfilename = filename; 197 return fd; 198 } 199 200 filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".a"; 201 fd = open(filename.c_str(), O_RDONLY | O_BINARY); 202 if (fd >= 0) 203 { 204 *pfilename = filename; 205 return fd; 206 } 207 208 filename = *pfilename + ".o"; 209 fd = open(filename.c_str(), O_RDONLY | O_BINARY); 210 if (fd >= 0) 211 { 212 *pfilename = filename; 213 return fd; 214 } 215 216 return -1; 217 } 218 219 // Look for export data in the file descriptor FD. 220 221 Import::Stream* 222 Import::find_export_data(const std::string& filename, int fd, Location location) 223 { 224 // See if we can read this as an object file. 225 Import::Stream* stream = Import::find_object_export_data(filename, fd, 0, 226 location); 227 if (stream != NULL) 228 return stream; 229 230 const int len = MAX(Export::magic_len, Import::archive_magic_len); 231 232 if (lseek(fd, 0, SEEK_SET) < 0) 233 { 234 go_error_at(location, "lseek %s failed: %m", filename.c_str()); 235 return NULL; 236 } 237 238 char buf[len]; 239 ssize_t c = ::read(fd, buf, len); 240 if (c < len) 241 return NULL; 242 243 // Check for a file containing nothing but Go export data. 244 if (memcmp(buf, Export::cur_magic, Export::magic_len) == 0 245 || memcmp(buf, Export::v1_magic, Export::magic_len) == 0 246 || memcmp(buf, Export::v2_magic, Export::magic_len) == 0) 247 return new Stream_from_file(fd); 248 249 // See if we can read this as an archive. 250 if (Import::is_archive_magic(buf)) 251 return Import::find_archive_export_data(filename, fd, location); 252 253 return NULL; 254 } 255 256 // Look for export data in an object file. 257 258 Import::Stream* 259 Import::find_object_export_data(const std::string& filename, 260 int fd, 261 off_t offset, 262 Location location) 263 { 264 char *buf; 265 size_t len; 266 int err; 267 const char *errmsg = go_read_export_data(fd, offset, &buf, &len, &err); 268 if (errmsg != NULL) 269 { 270 if (err == 0) 271 go_error_at(location, "%s: %s", filename.c_str(), errmsg); 272 else 273 go_error_at(location, "%s: %s: %s", filename.c_str(), errmsg, 274 xstrerror(err)); 275 return NULL; 276 } 277 278 if (buf == NULL) 279 return NULL; 280 281 return new Stream_from_buffer(buf, len); 282 } 283 284 // Class Import. 285 286 // Construct an Import object. We make the builtin_types_ vector 287 // large enough to hold all the builtin types. 288 289 Import::Import(Stream* stream, Location location) 290 : gogo_(NULL), stream_(stream), location_(location), package_(NULL), 291 add_to_globals_(false), packages_(), type_data_(), type_pos_(0), 292 type_offsets_(), builtin_types_((- SMALLEST_BUILTIN_CODE) + 1), 293 types_(), version_(EXPORT_FORMAT_UNKNOWN) 294 { 295 } 296 297 // Import the data in the associated stream. 298 299 Package* 300 Import::import(Gogo* gogo, const std::string& local_name, 301 bool is_local_name_exported) 302 { 303 // Hold on to the Gogo structure. Otherwise we need to pass it 304 // through all the import functions, because we need it when reading 305 // a type. 306 this->gogo_ = gogo; 307 308 // A stream of export data can include data from more than one input 309 // file. Here we loop over each input file. 310 Stream* stream = this->stream_; 311 while (!stream->at_eof() && !stream->saw_error()) 312 { 313 // The vector of types is package specific. 314 this->types_.clear(); 315 316 // Check magic string / version number. 317 if (stream->match_bytes(Export::cur_magic, Export::magic_len)) 318 { 319 stream->require_bytes(this->location_, Export::cur_magic, 320 Export::magic_len); 321 this->version_ = EXPORT_FORMAT_CURRENT; 322 } 323 else if (stream->match_bytes(Export::v1_magic, Export::magic_len)) 324 { 325 stream->require_bytes(this->location_, Export::v1_magic, 326 Export::magic_len); 327 this->version_ = EXPORT_FORMAT_V1; 328 } 329 else if (stream->match_bytes(Export::v2_magic, Export::magic_len)) 330 { 331 stream->require_bytes(this->location_, Export::v2_magic, 332 Export::magic_len); 333 this->version_ = EXPORT_FORMAT_V2; 334 } 335 else 336 { 337 go_error_at(this->location_, 338 ("error in import data at %d: invalid magic string"), 339 stream->pos()); 340 return NULL; 341 } 342 343 this->require_c_string("package "); 344 std::string package_name = this->read_identifier(); 345 this->require_semicolon_if_old_version(); 346 this->require_c_string("\n"); 347 348 std::string pkgpath; 349 std::string pkgpath_symbol; 350 if (this->match_c_string("prefix ")) 351 { 352 this->advance(7); 353 std::string unique_prefix = this->read_identifier(); 354 this->require_semicolon_if_old_version(); 355 this->require_c_string("\n"); 356 pkgpath = unique_prefix + '.' + package_name; 357 pkgpath_symbol = (Gogo::pkgpath_for_symbol(unique_prefix) + '.' 358 + Gogo::pkgpath_for_symbol(package_name)); 359 } 360 else 361 { 362 this->require_c_string("pkgpath "); 363 pkgpath = this->read_identifier(); 364 this->require_semicolon_if_old_version(); 365 this->require_c_string("\n"); 366 pkgpath_symbol = Gogo::pkgpath_for_symbol(pkgpath); 367 } 368 369 if (stream->saw_error()) 370 return NULL; 371 372 this->package_ = gogo->add_imported_package(package_name, local_name, 373 is_local_name_exported, 374 pkgpath, pkgpath_symbol, 375 this->location_, 376 &this->add_to_globals_); 377 if (this->package_ == NULL) 378 { 379 stream->set_saw_error(); 380 return NULL; 381 } 382 383 // Read and discard priority if older V1 export data format. 384 if (version() == EXPORT_FORMAT_V1) 385 { 386 this->require_c_string("priority "); 387 std::string priority_string = this->read_identifier(); 388 int prio; 389 if (!this->string_to_int(priority_string, false, &prio)) 390 return NULL; 391 this->require_c_string(";\n"); 392 } 393 394 while (stream->match_c_string("package")) 395 this->read_one_package(); 396 397 while (stream->match_c_string("import")) 398 this->read_one_import(); 399 400 while (stream->match_c_string("indirectimport")) 401 this->read_one_indirect_import(); 402 403 if (stream->match_c_string("init")) 404 this->read_import_init_fns(gogo); 405 406 if (stream->match_c_string("types ")) 407 { 408 if (!this->read_types()) 409 return NULL; 410 } 411 412 // Loop over all the input data for this package. 413 while (!stream->saw_error()) 414 { 415 if (stream->match_c_string("const ")) 416 this->import_const(); 417 else if (stream->match_c_string("type ")) 418 this->import_type(); 419 else if (stream->match_c_string("var ")) 420 this->import_var(); 421 else if (stream->match_c_string("func ")) 422 this->import_func(this->package_); 423 else if (stream->match_c_string("checksum ")) 424 break; 425 else 426 { 427 go_error_at(this->location_, 428 ("error in import data at %d: " 429 "expected %<const%>, %<type%>, %<var%>, " 430 "%<func%>, or %<checksum%>"), 431 stream->pos()); 432 stream->set_saw_error(); 433 return NULL; 434 } 435 } 436 437 // We currently ignore the checksum. In the future we could 438 // store the checksum somewhere in the generated object and then 439 // verify that the checksum matches at link time or at dynamic 440 // load time. 441 this->require_c_string("checksum "); 442 stream->advance(Export::checksum_len * 2); 443 this->require_semicolon_if_old_version(); 444 this->require_c_string("\n"); 445 } 446 447 // Finalize methods for any imported types. This call is made late in the 448 // import process so as to A) avoid finalization of a type whose methods 449 // refer to types that are only partially read in, and B) capture both the 450 // types imported by read_types() directly, and those imported indirectly 451 // because they are referenced by an imported function or variable. 452 // See issues #33013 and #33219 for more on why this is needed. 453 this->finalize_methods(); 454 455 return this->package_; 456 } 457 458 // Read a package line. This let us reliably determine the pkgpath 459 // symbol, even if the package was compiled with a -fgo-prefix option. 460 461 void 462 Import::read_one_package() 463 { 464 this->require_c_string("package "); 465 std::string package_name = this->read_identifier(); 466 this->require_c_string(" "); 467 std::string pkgpath = this->read_identifier(); 468 this->require_c_string(" "); 469 std::string pkgpath_symbol = this->read_identifier(); 470 this->require_semicolon_if_old_version(); 471 this->require_c_string("\n"); 472 473 Package* p = this->gogo_->register_package(pkgpath, pkgpath_symbol, 474 Linemap::unknown_location()); 475 p->set_package_name(package_name, this->location()); 476 } 477 478 // Read an import line. 479 480 void 481 Import::read_one_import() 482 { 483 this->require_c_string("import "); 484 std::string package_name = this->read_identifier(); 485 this->require_c_string(" "); 486 std::string pkgpath = this->read_identifier(); 487 this->require_c_string(" \""); 488 Stream* stream = this->stream_; 489 while (stream->peek_char() != '"') 490 stream->advance(1); 491 this->require_c_string("\""); 492 this->require_semicolon_if_old_version(); 493 this->require_c_string("\n"); 494 495 Package* p = this->gogo_->register_package(pkgpath, "", 496 Linemap::unknown_location()); 497 p->set_package_name(package_name, this->location()); 498 499 this->packages_.push_back(p); 500 501 if (pkgpath == "unsafe") 502 this->gogo_->add_unsafe_bindings(p); 503 } 504 505 // Read an indirectimport line. 506 507 void 508 Import::read_one_indirect_import() 509 { 510 this->require_c_string("indirectimport "); 511 std::string package_name = this->read_identifier(); 512 this->require_c_string(" "); 513 std::string pkgpath = this->read_identifier(); 514 this->require_c_string("\n"); 515 516 Package* p = this->gogo_->register_package(pkgpath, "", 517 Linemap::unknown_location()); 518 p->set_package_name(package_name, this->location()); 519 520 this->packages_.push_back(p); 521 522 if (pkgpath == "unsafe") 523 this->gogo_->add_unsafe_bindings(p); 524 } 525 526 // Read the list of import control functions and/or init graph. 527 528 void 529 Import::read_import_init_fns(Gogo* gogo) 530 { 531 this->require_c_string("init"); 532 533 // Maps init function to index in the "init" clause; needed 534 // to read the init_graph section. 535 std::map<std::string, unsigned> init_idx; 536 537 while (!this->match_c_string("\n") && !this->match_c_string(";")) 538 { 539 int priority = -1; 540 541 this->require_c_string(" "); 542 std::string package_name = this->read_identifier(); 543 this->require_c_string(" "); 544 std::string init_name = this->read_identifier(); 545 if (this->version_ == EXPORT_FORMAT_V1) 546 { 547 // Older version 1 init fcn export data format is: 548 // 549 // <packname> <fcn> <priority> 550 this->require_c_string(" "); 551 std::string prio_string = this->read_identifier(); 552 if (!this->string_to_int(prio_string, false, &priority)) 553 return; 554 } 555 gogo->add_import_init_fn(package_name, init_name, priority); 556 557 // Record the index of this init fcn so that we can look it 558 // up by index in the subsequent init_graph section. 559 unsigned idx = init_idx.size(); 560 init_idx[init_name] = idx; 561 } 562 this->require_semicolon_if_old_version(); 563 this->require_c_string("\n"); 564 565 if (this->match_c_string("init_graph")) 566 { 567 this->require_c_string("init_graph"); 568 569 // Build a vector mapping init fcn slot to Import_init pointer. 570 go_assert(init_idx.size() > 0); 571 std::vector<Import_init*> import_initvec; 572 import_initvec.resize(init_idx.size()); 573 for (std::map<std::string, unsigned>::const_iterator it = 574 init_idx.begin(); 575 it != init_idx.end(); ++it) 576 { 577 const std::string& init_name = it->first; 578 Import_init* ii = gogo->lookup_init(init_name); 579 import_initvec[it->second] = ii; 580 } 581 582 // Init graph format is: 583 // 584 // init_graph <src1> <sink1> <src2> <sink2> ... ; 585 // 586 // where src + sink are init functions indices. 587 588 while (!this->match_c_string("\n") && !this->match_c_string(";")) 589 { 590 this->require_c_string(" "); 591 std::string src_string = this->read_identifier(); 592 unsigned src; 593 if (!this->string_to_unsigned(src_string, &src)) return; 594 595 this->require_c_string(" "); 596 std::string sink_string = this->read_identifier(); 597 unsigned sink; 598 if (!this->string_to_unsigned(sink_string, &sink)) return; 599 600 go_assert(src < import_initvec.size()); 601 Import_init* ii_src = import_initvec[src]; 602 go_assert(sink < import_initvec.size()); 603 Import_init* ii_sink = import_initvec[sink]; 604 605 ii_src->record_precursor_fcn(ii_sink->init_name()); 606 } 607 this->require_semicolon_if_old_version(); 608 this->require_c_string("\n"); 609 } 610 } 611 612 // Import the types. Starting in export format version 3 all the 613 // types are listed first. 614 615 bool 616 Import::read_types() 617 { 618 this->require_c_string("types "); 619 std::string str = this->read_identifier(); 620 int maxp1; 621 if (!this->string_to_int(str, false, &maxp1)) 622 return false; 623 624 this->require_c_string(" "); 625 str = this->read_identifier(); 626 int exportedp1; 627 if (!this->string_to_int(str, false, &exportedp1)) 628 return false; 629 630 this->type_offsets_.resize(maxp1, std::make_pair<size_t, size_t>(0, 0)); 631 size_t total_type_size = 0; 632 // Start at 1 because type index 0 not used. 633 for (int i = 1; i < maxp1; i++) 634 { 635 this->require_c_string(" "); 636 str = this->read_identifier(); 637 int v; 638 if (!this->string_to_int(str, false, &v)) 639 return false; 640 size_t vs = static_cast<size_t>(v); 641 this->type_offsets_[i] = std::make_pair(total_type_size, vs); 642 total_type_size += vs; 643 } 644 645 this->require_c_string("\n"); 646 647 // Types can refer to each other in an unpredictable order. Read 648 // all the type data into type_data_. The type_offsets_ vector we 649 // just initialized provides indexes into type_data_. 650 651 this->type_pos_ = this->stream_->pos(); 652 const char* type_data; 653 if (!this->stream_->peek(total_type_size, &type_data)) 654 return false; 655 this->type_data_ = std::string(type_data, total_type_size); 656 this->advance(total_type_size); 657 658 this->types_.resize(maxp1, NULL); 659 660 // Parse all the exported types now, so that the names are properly 661 // bound and visible to the parser. Parse unexported types lazily. 662 663 // Start at 1 because there is no type 0. 664 for (int i = 1; i < exportedp1; i++) 665 { 666 // We may have already parsed this type when we parsed an 667 // earlier type. 668 Type* type = this->types_[i]; 669 if (type == NULL) 670 { 671 if (!this->parse_type(i)) 672 return false; 673 type = this->types_[i]; 674 go_assert(type != NULL); 675 } 676 Named_type* nt = type->named_type(); 677 if (nt == NULL) 678 { 679 go_error_at(this->location_, 680 "error in import data: exported unnamed type %d", 681 i); 682 return false; 683 } 684 nt->set_is_visible(); 685 if (this->add_to_globals_) 686 this->gogo_->add_named_type(nt); 687 } 688 689 return true; 690 } 691 692 void 693 Import::finalize_methods() 694 { 695 Finalize_methods finalizer(this->gogo_); 696 Unordered_set(Type*) real_for_named; 697 for (size_t i = 1; i < this->types_.size(); i++) 698 { 699 Type* type = this->types_[i]; 700 if (type != NULL && type->named_type() != NULL) 701 { 702 finalizer.type(type); 703 704 // If the real type is a struct type, we don't want to 705 // finalize its methods. For a named type defined as a 706 // struct type, we only want to finalize the methods of the 707 // named type. This is like Finalize_methods::type. 708 Type* real_type = type->named_type()->real_type(); 709 if (real_type->struct_type() != NULL) 710 real_for_named.insert(real_type); 711 } 712 } 713 for (size_t i = 1; i < this->types_.size(); i++) 714 { 715 Type* type = this->types_[i]; 716 if (type != NULL 717 && type->named_type() == NULL 718 && real_for_named.find(type) == real_for_named.end()) 719 finalizer.type(type); 720 } 721 } 722 723 // Import a constant. 724 725 void 726 Import::import_const() 727 { 728 std::string name; 729 Type* type; 730 Expression* expr; 731 Named_constant::import_const(this, &name, &type, &expr); 732 Typed_identifier tid(name, type, this->location_); 733 Named_object* no = this->package_->add_constant(tid, expr); 734 if (this->add_to_globals_) 735 this->gogo_->add_dot_import_object(no); 736 } 737 738 // Import a type. 739 740 void 741 Import::import_type() 742 { 743 if (this->version_ >= EXPORT_FORMAT_V3) 744 { 745 if (!this->stream_->saw_error()) 746 { 747 go_error_at(this->location_, 748 "error in import data at %d: old type syntax", 749 this->stream_->pos()); 750 this->stream_->set_saw_error(); 751 } 752 return; 753 } 754 755 Named_type* type; 756 Named_type::import_named_type(this, &type); 757 758 // The named type has been added to the package by the type import 759 // process. Here we need to make it visible to the parser, and it 760 // to the global bindings if necessary. 761 type->set_is_visible(); 762 763 if (this->add_to_globals_) 764 this->gogo_->add_named_type(type); 765 } 766 767 // Import a variable. 768 769 void 770 Import::import_var() 771 { 772 std::string name; 773 Package* vpkg; 774 bool is_exported; 775 Type* type; 776 if (!Variable::import_var(this, &name, &vpkg, &is_exported, &type)) 777 return; 778 if (vpkg == NULL) 779 vpkg = this->package_; 780 if (!is_exported) 781 name = '.' + vpkg->pkgpath() + '.' + name; 782 Variable* var = new Variable(type, NULL, true, false, false, 783 this->location_); 784 Named_object* no; 785 no = vpkg->add_variable(name, var); 786 if (this->add_to_globals_ && vpkg == this->package_) 787 this->gogo_->add_dot_import_object(no); 788 } 789 790 // Import a function into PACKAGE. PACKAGE is normally 791 // THIS->PACKAGE_, but it will be different for a method associated 792 // with a type defined in a different package. 793 794 void 795 Import::import_func(Package* package) 796 { 797 std::string name; 798 Package* fpkg; 799 bool is_exported; 800 Typed_identifier* receiver; 801 Typed_identifier_list* parameters; 802 Typed_identifier_list* results; 803 bool is_varargs; 804 bool nointerface; 805 std::string asm_name; 806 std::string body; 807 if (!Function::import_func(this, &name, &fpkg, &is_exported, &receiver, 808 ¶meters, &results, &is_varargs, &nointerface, 809 &asm_name, &body)) 810 return; 811 if (fpkg == NULL) 812 fpkg = package; 813 if (!is_exported) 814 name = '.' + fpkg->pkgpath() + '.' + name; 815 Function_type *fntype = Type::make_function_type(receiver, parameters, 816 results, this->location_); 817 if (is_varargs) 818 fntype->set_is_varargs(); 819 820 Location loc = this->location_; 821 Named_object* no; 822 if (fntype->is_method()) 823 { 824 Type* rtype = receiver->type(); 825 826 // We may still be reading the definition of RTYPE, so we have 827 // to be careful to avoid calling base or convert. If RTYPE is 828 // a named type or a forward declaration, then we know that it 829 // is not a pointer, because we are reading a method on RTYPE 830 // and named pointers can't have methods. 831 832 if (rtype->classification() == Type::TYPE_POINTER) 833 rtype = rtype->points_to(); 834 835 if (rtype->is_error_type()) 836 return; 837 else if (rtype->named_type() != NULL) 838 no = rtype->named_type()->add_method_declaration(name, fpkg, fntype, 839 loc); 840 else if (rtype->forward_declaration_type() != NULL) 841 no = rtype->forward_declaration_type()->add_method_declaration(name, 842 fpkg, 843 fntype, 844 loc); 845 else 846 go_unreachable(); 847 } 848 else 849 { 850 no = fpkg->add_function_declaration(name, fntype, loc); 851 if (this->add_to_globals_ && fpkg == package) 852 this->gogo_->add_dot_import_object(no); 853 } 854 855 if (nointerface) 856 no->func_declaration_value()->set_nointerface(); 857 if (!asm_name.empty()) 858 no->func_declaration_value()->set_asm_name(asm_name); 859 if (!body.empty() && !no->func_declaration_value()->has_imported_body()) 860 no->func_declaration_value()->set_imported_body(this, body); 861 } 862 863 // Read a type definition and initialize the entry in this->types_. 864 // This parses the type definition saved by read_types earlier. This 865 // returns true on success, false on failure. 866 867 bool 868 Import::parse_type(int i) 869 { 870 go_assert(i >= 0 && static_cast<size_t>(i) < this->types_.size()); 871 go_assert(this->types_[i] == NULL); 872 size_t offset = this->type_offsets_[i].first; 873 size_t len = this->type_offsets_[i].second; 874 875 Stream* orig_stream = this->stream_; 876 877 Stream_from_string_ref stream(this->type_data_, offset, len); 878 stream.set_pos(this->type_pos_ + offset); 879 this->stream_ = &stream; 880 881 this->require_c_string("type "); 882 std::string str = this->read_identifier(); 883 int id; 884 if (!this->string_to_int(str, false, &id)) 885 { 886 this->stream_ = orig_stream; 887 return false; 888 } 889 if (i != id) 890 { 891 go_error_at(this->location_, 892 ("error in import data at %d: " 893 "type ID mismatch: got %d, want %d"), 894 stream.pos(), id, i); 895 this->stream_ = orig_stream; 896 return false; 897 } 898 899 this->require_c_string(" "); 900 if (stream.peek_char() == '"') 901 { 902 stream.advance(1); 903 Type* type = this->read_named_type(i); 904 if (type->is_error_type()) 905 { 906 this->stream_ = orig_stream; 907 return false; 908 } 909 } 910 else 911 { 912 Type* type = Type::import_type(this); 913 if (type->is_error_type()) 914 { 915 this->stream_ = orig_stream; 916 return false; 917 } 918 this->types_[i] = type; 919 920 this->require_c_string("\n"); 921 } 922 923 this->stream_ = orig_stream; 924 return true; 925 } 926 927 // Read a type in the import stream. This records the type by the 928 // type index. If the type is named (which can only happen with older 929 // export formats), it registers the name, but marks it as invisible. 930 931 Type* 932 Import::read_type() 933 { 934 Stream* stream = this->stream_; 935 this->require_c_string("<type "); 936 937 std::string number; 938 int c; 939 while (true) 940 { 941 c = stream->get_char(); 942 if (c != '-' && (c < '0' || c > '9')) 943 break; 944 number += c; 945 } 946 947 int index; 948 if (!this->string_to_int(number, true, &index)) 949 return Type::make_error_type(); 950 951 if (c == '>') 952 { 953 // A reference to a type defined earlier. 954 bool parsed; 955 return this->type_for_index(index, "import data", stream->pos(), 956 &parsed); 957 } 958 959 if (this->version_ >= EXPORT_FORMAT_V3) 960 { 961 if (!stream->saw_error()) 962 go_error_at(this->location_, 963 "error in import data at %d: expected %<>%>", 964 stream->pos()); 965 stream->set_saw_error(); 966 return Type::make_error_type(); 967 } 968 969 if (c != ' ') 970 { 971 if (!stream->saw_error()) 972 go_error_at(this->location_, 973 "error in import data at %d: expected %< %> or %<>%>", 974 stream->pos()); 975 stream->set_saw_error(); 976 stream->advance(1); 977 return Type::make_error_type(); 978 } 979 980 if (index <= 0 981 || (static_cast<size_t>(index) < this->types_.size() 982 && this->types_[index] != NULL)) 983 { 984 go_error_at(this->location_, 985 "error in import data at %d: type index already defined", 986 stream->pos()); 987 stream->set_saw_error(); 988 return Type::make_error_type(); 989 } 990 991 if (static_cast<size_t>(index) >= this->types_.size()) 992 { 993 int newsize = std::max(static_cast<size_t>(index) + 1, 994 this->types_.size() * 2); 995 this->types_.resize(newsize, NULL); 996 } 997 998 if (stream->peek_char() != '"') 999 { 1000 Type* type = Type::import_type(this); 1001 this->require_c_string(">"); 1002 this->types_[index] = type; 1003 return type; 1004 } 1005 1006 stream->advance(1); 1007 1008 Type* type = this->read_named_type(index); 1009 1010 this->require_c_string(">"); 1011 1012 return type; 1013 } 1014 1015 // Read a named type from the import stream and store it in 1016 // this->types_[index]. The stream should be positioned immediately 1017 // after the '"' that starts the name. 1018 1019 Type* 1020 Import::read_named_type(int index) 1021 { 1022 Stream* stream = this->stream_; 1023 std::string type_name; 1024 int c; 1025 while ((c = stream->get_char()) != '"') 1026 type_name += c; 1027 1028 // If this type is in the package we are currently importing, the 1029 // name will be .PKGPATH.NAME or simply NAME with no dots. 1030 // Otherwise, a non-hidden symbol will be PKGPATH.NAME and a hidden 1031 // symbol will be .PKGPATH.NAME. 1032 std::string pkgpath; 1033 if (type_name.find('.') != std::string::npos) 1034 { 1035 size_t start = 0; 1036 if (type_name[0] == '.') 1037 start = 1; 1038 size_t dot = type_name.rfind('.'); 1039 pkgpath = type_name.substr(start, dot - start); 1040 if (type_name[0] != '.') 1041 type_name.erase(0, dot + 1); 1042 } 1043 1044 this->require_c_string(" "); 1045 1046 // The package name may follow. This is the name of the package in 1047 // the package clause of that package. The type name will include 1048 // the pkgpath, which may be different. 1049 std::string package_name; 1050 if (stream->peek_char() == '"') 1051 { 1052 stream->advance(1); 1053 while ((c = stream->get_char()) != '"') 1054 package_name += c; 1055 this->require_c_string(" "); 1056 } 1057 1058 bool in_heap = true; 1059 if (this->match_c_string("notinheap")) 1060 { 1061 this->require_c_string("notinheap "); 1062 in_heap = false; 1063 } 1064 1065 bool is_alias = false; 1066 if (this->match_c_string("= ")) 1067 { 1068 stream->advance(2); 1069 is_alias = true; 1070 } 1071 1072 // Declare the type in the appropriate package. If we haven't seen 1073 // it before, mark it as invisible. We declare it before we read 1074 // the actual definition of the type, since the definition may refer 1075 // to the type itself. 1076 Package* package; 1077 if (pkgpath.empty() || pkgpath == this->gogo_->pkgpath()) 1078 package = this->package_; 1079 else 1080 { 1081 package = this->gogo_->register_package(pkgpath, "", 1082 Linemap::unknown_location()); 1083 if (!package_name.empty()) 1084 package->set_package_name(package_name, this->location()); 1085 } 1086 1087 Named_object* no = package->bindings()->lookup(type_name); 1088 if (no == NULL) 1089 no = package->add_type_declaration(type_name, this->location_); 1090 else if (!no->is_type_declaration() && !no->is_type()) 1091 { 1092 go_error_at(this->location_, "imported %<%s.%s%> both type and non-type", 1093 pkgpath.c_str(), Gogo::message_name(type_name).c_str()); 1094 stream->set_saw_error(); 1095 return Type::make_error_type(); 1096 } 1097 else 1098 go_assert(no->package() == package); 1099 1100 if (this->types_[index] == NULL) 1101 { 1102 if (no->is_type_declaration()) 1103 { 1104 // FIXME: It's silly to make a forward declaration every time. 1105 this->types_[index] = Type::make_forward_declaration(no); 1106 } 1107 else 1108 { 1109 go_assert(no->is_type()); 1110 this->types_[index] = no->type_value(); 1111 } 1112 } 1113 1114 // If there is no type definition, then this is just a forward 1115 // declaration of a type defined in some other file. 1116 Type* type; 1117 if (this->match_c_string(">") || this->match_c_string("\n")) 1118 { 1119 type = this->types_[index]; 1120 if (!in_heap) 1121 go_error_at(this->location_, 1122 ("import error at %d for type index %d: " 1123 "forward declaration marked notinheap"), 1124 this->pos(), index); 1125 } 1126 else 1127 { 1128 if (no->is_type_declaration()) 1129 { 1130 // We can define the type now. 1131 1132 type = this->read_type(); 1133 1134 no = package->add_type(type_name, type, this->location_); 1135 Named_type* ntype = no->type_value(); 1136 1137 // This type has not yet been imported. 1138 ntype->clear_is_visible(); 1139 1140 if (!in_heap) 1141 ntype->set_not_in_heap(); 1142 if (is_alias) 1143 ntype->set_is_alias(); 1144 1145 if (!type->is_undefined() && type->interface_type() != NULL) 1146 this->gogo_->record_interface_type(type->interface_type()); 1147 1148 type = ntype; 1149 } 1150 else if (no->is_type()) 1151 { 1152 // We have seen this type before. 1153 type = no->type_value(); 1154 1155 // Don't change the visibility of the existing type. 1156 1157 // For older export versions, we need to skip the type 1158 // definition in the stream. 1159 if (this->version_ < EXPORT_FORMAT_V3) 1160 this->read_type(); 1161 } 1162 else 1163 go_unreachable(); 1164 1165 this->types_[index] = type; 1166 1167 // Read the type methods. 1168 if (this->match_c_string("\n")) 1169 { 1170 this->advance(1); 1171 while (this->match_c_string(" func")) 1172 { 1173 this->advance(1); 1174 this->import_func(package); 1175 } 1176 } 1177 } 1178 1179 return type; 1180 } 1181 1182 // Return the type given an index. Set *PARSED if we parsed it here. 1183 1184 Type* 1185 Import::type_for_index(int index, const std::string& input_name, 1186 size_t input_offset, bool* parsed) 1187 { 1188 *parsed = false; 1189 if (index >= 0 && !this->type_data_.empty()) 1190 { 1191 if (static_cast<size_t>(index) >= this->type_offsets_.size()) 1192 { 1193 go_error_at(this->location_, 1194 "error in %s at %lu: bad type index %d, max %d", 1195 input_name.c_str(), 1196 static_cast<unsigned long>(input_offset), 1197 index, static_cast<int>(this->type_offsets_.size())); 1198 return Type::make_error_type(); 1199 } 1200 1201 if (this->types_[index] == NULL) 1202 { 1203 if (!this->parse_type(index)) 1204 return Type::make_error_type(); 1205 *parsed = true; 1206 } 1207 } 1208 1209 if (index < 0 1210 ? (static_cast<size_t>(- index) >= this->builtin_types_.size() 1211 || this->builtin_types_[- index] == NULL) 1212 : (static_cast<size_t>(index) >= this->types_.size() 1213 || this->types_[index] == NULL)) 1214 { 1215 go_error_at(this->location_, 1216 "error in %s at %lu: bad type index %d", 1217 input_name.c_str(), 1218 static_cast<unsigned long>(input_offset), index); 1219 return Type::make_error_type(); 1220 } 1221 1222 return index < 0 ? this->builtin_types_[- index] : this->types_[index]; 1223 } 1224 1225 // Read an escape note. 1226 1227 std::string 1228 Import::read_escape() 1229 { 1230 if (this->match_c_string(" <esc:")) 1231 { 1232 Stream* stream = this->stream_; 1233 this->require_c_string(" <esc:"); 1234 1235 std::string escape = "esc:"; 1236 int c; 1237 while (true) 1238 { 1239 c = stream->get_char(); 1240 if (c != 'x' && !ISXDIGIT(c)) 1241 break; 1242 escape += c; 1243 } 1244 1245 if (c != '>') 1246 { 1247 go_error_at(this->location(), 1248 ("error in import data at %d: " 1249 "expect %< %> or %<>%>, got %c"), 1250 stream->pos(), c); 1251 stream->set_saw_error(); 1252 stream->advance(1); 1253 escape = Escape_note::make_tag(Node::ESCAPE_UNKNOWN); 1254 } 1255 return escape; 1256 } 1257 else 1258 return Escape_note::make_tag(Node::ESCAPE_UNKNOWN); 1259 } 1260 1261 1262 // Register the builtin types. 1263 1264 void 1265 Import::register_builtin_types(Gogo* gogo) 1266 { 1267 this->register_builtin_type(gogo, "int8", BUILTIN_INT8); 1268 this->register_builtin_type(gogo, "int16", BUILTIN_INT16); 1269 this->register_builtin_type(gogo, "int32", BUILTIN_INT32); 1270 this->register_builtin_type(gogo, "int64", BUILTIN_INT64); 1271 this->register_builtin_type(gogo, "uint8", BUILTIN_UINT8); 1272 this->register_builtin_type(gogo, "uint16", BUILTIN_UINT16); 1273 this->register_builtin_type(gogo, "uint32", BUILTIN_UINT32); 1274 this->register_builtin_type(gogo, "uint64", BUILTIN_UINT64); 1275 this->register_builtin_type(gogo, "float32", BUILTIN_FLOAT32); 1276 this->register_builtin_type(gogo, "float64", BUILTIN_FLOAT64); 1277 this->register_builtin_type(gogo, "complex64", BUILTIN_COMPLEX64); 1278 this->register_builtin_type(gogo, "complex128", BUILTIN_COMPLEX128); 1279 this->register_builtin_type(gogo, "int", BUILTIN_INT); 1280 this->register_builtin_type(gogo, "uint", BUILTIN_UINT); 1281 this->register_builtin_type(gogo, "uintptr", BUILTIN_UINTPTR); 1282 this->register_builtin_type(gogo, "bool", BUILTIN_BOOL); 1283 this->register_builtin_type(gogo, "string", BUILTIN_STRING); 1284 this->register_builtin_type(gogo, "error", BUILTIN_ERROR); 1285 this->register_builtin_type(gogo, "byte", BUILTIN_BYTE); 1286 this->register_builtin_type(gogo, "rune", BUILTIN_RUNE); 1287 } 1288 1289 // Register a single builtin type. 1290 1291 void 1292 Import::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code) 1293 { 1294 Named_object* named_object = gogo->lookup_global(name); 1295 go_assert(named_object != NULL && named_object->is_type()); 1296 int index = - static_cast<int>(code); 1297 go_assert(index > 0 1298 && static_cast<size_t>(index) < this->builtin_types_.size()); 1299 this->builtin_types_[index] = named_object->type_value(); 1300 } 1301 1302 // Characters that stop read_identifier. We base this on the 1303 // characters that stop an identifier, without worrying about 1304 // characters that are permitted in an identifier. That lets us skip 1305 // UTF-8 parsing. 1306 static const char * const identifier_stop = " \n;:,()[]"; 1307 1308 // Read an identifier from the stream. 1309 1310 std::string 1311 Import::read_identifier() 1312 { 1313 std::string ret; 1314 Stream* stream = this->stream_; 1315 int c; 1316 while (true) 1317 { 1318 c = stream->peek_char(); 1319 if (c == -1 || strchr(identifier_stop, c) != NULL) 1320 break; 1321 1322 // FIXME: Probably we shouldn't accept '.', but that might break 1323 // some existing imports. 1324 if (c == '.' && stream->match_c_string("...")) 1325 break; 1326 1327 ret += c; 1328 stream->advance(1); 1329 } 1330 return ret; 1331 } 1332 1333 // Read a possibly qualified identifier from IMP. The qualification 1334 // is <pID>, where ID is a package number. If the name has a leading 1335 // '.', it is not exported; otherwise, it is. Set *NAME, *PKG and 1336 // *IS_EXPORTED. Reports whether the read succeeded. 1337 1338 bool 1339 Import::read_qualified_identifier(Import_expression* imp, std::string* name, 1340 Package** pkg, bool* is_exported) 1341 { 1342 *pkg = NULL; 1343 if (imp->match_c_string("<p")) 1344 { 1345 imp->advance(2); 1346 char buf[50]; 1347 char *pbuf = &buf[0]; 1348 while (true) 1349 { 1350 int next = imp->peek_char(); 1351 if (next == -1 || static_cast<size_t>(pbuf - buf) >= sizeof buf - 1) 1352 return false; 1353 if (next == '>') 1354 { 1355 imp->advance(1); 1356 break; 1357 } 1358 *pbuf = static_cast<char>(next); 1359 ++pbuf; 1360 imp->advance(1); 1361 } 1362 1363 *pbuf = '\0'; 1364 char *end; 1365 long index = strtol(buf, &end, 10); 1366 if (*end != '\0' 1367 || index <= 0 1368 || static_cast<size_t>(index) > imp->max_package_index()) 1369 return false; 1370 1371 *pkg = imp->package_at_index(index); 1372 go_assert(*pkg != NULL); 1373 } 1374 1375 *is_exported = true; 1376 if (imp->match_c_string(".")) 1377 { 1378 imp->advance(1); 1379 *is_exported = false; 1380 } 1381 1382 *name = imp->read_identifier(); 1383 1384 return !name->empty(); 1385 } 1386 1387 // Read a name from the stream. 1388 1389 std::string 1390 Import::read_name() 1391 { 1392 std::string ret = this->read_identifier(); 1393 if (ret == "?") 1394 ret.clear(); 1395 return ret; 1396 } 1397 1398 // Read LENGTH bytes from the stream. 1399 1400 void 1401 Import::read(size_t length, std::string* out) 1402 { 1403 const char* data; 1404 if (!this->stream_->peek(length, &data)) 1405 { 1406 if (!this->stream_->saw_error()) 1407 go_error_at(this->location_, "import error at %d: expected %d bytes", 1408 this->stream_->pos(), static_cast<int>(length)); 1409 this->stream_->set_saw_error(); 1410 *out = std::string(""); 1411 return; 1412 } 1413 *out = std::string(data, length); 1414 this->advance(length); 1415 } 1416 1417 // Turn a string into a integer with appropriate error handling. 1418 1419 bool 1420 Import::string_to_int(const std::string &s, bool is_neg_ok, int* ret) 1421 { 1422 char* end; 1423 long prio = strtol(s.c_str(), &end, 10); 1424 if (*end != '\0' || prio > 0x7fffffff || (prio < 0 && !is_neg_ok)) 1425 { 1426 go_error_at(this->location_, "invalid integer in import data at %d", 1427 this->stream_->pos()); 1428 this->stream_->set_saw_error(); 1429 return false; 1430 } 1431 *ret = prio; 1432 return true; 1433 } 1434 1435 // Class Import::Stream. 1436 1437 Import::Stream::Stream() 1438 : pos_(0), saw_error_(false) 1439 { 1440 } 1441 1442 Import::Stream::~Stream() 1443 { 1444 } 1445 1446 // Return the next character to come from the stream. 1447 1448 int 1449 Import::Stream::peek_char() 1450 { 1451 const char* read; 1452 if (!this->do_peek(1, &read)) 1453 return -1; 1454 // Make sure we return an unsigned char, so that we don't get 1455 // confused by \xff. 1456 unsigned char ret = *read; 1457 return ret; 1458 } 1459 1460 // Return true if the next LENGTH characters from the stream match 1461 // BYTES 1462 1463 bool 1464 Import::Stream::match_bytes(const char* bytes, size_t length) 1465 { 1466 const char* read; 1467 if (!this->do_peek(length, &read)) 1468 return false; 1469 return memcmp(bytes, read, length) == 0; 1470 } 1471 1472 // Require that the next LENGTH bytes from the stream match BYTES. 1473 1474 void 1475 Import::Stream::require_bytes(Location location, const char* bytes, 1476 size_t length) 1477 { 1478 const char* read; 1479 if (!this->do_peek(length, &read) 1480 || memcmp(bytes, read, length) != 0) 1481 { 1482 if (!this->saw_error_) 1483 go_error_at(location, "import error at %d: expected %<%.*s%>", 1484 this->pos(), static_cast<int>(length), bytes); 1485 this->saw_error_ = true; 1486 return; 1487 } 1488 this->advance(length); 1489 } 1490 1491 // Class Stream_from_file. 1492 1493 Stream_from_file::Stream_from_file(int fd) 1494 : fd_(fd), data_() 1495 { 1496 if (lseek(fd, 0, SEEK_SET) != 0) 1497 { 1498 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m"); 1499 this->set_saw_error(); 1500 } 1501 } 1502 1503 Stream_from_file::~Stream_from_file() 1504 { 1505 close(this->fd_); 1506 } 1507 1508 // Read next bytes. 1509 1510 bool 1511 Stream_from_file::do_peek(size_t length, const char** bytes) 1512 { 1513 if (this->data_.length() >= length) 1514 { 1515 *bytes = this->data_.data(); 1516 return true; 1517 } 1518 1519 this->data_.resize(length); 1520 ssize_t got = ::read(this->fd_, &this->data_[0], length); 1521 1522 if (got < 0) 1523 { 1524 if (!this->saw_error()) 1525 go_fatal_error(Linemap::unknown_location(), "read failed: %m"); 1526 this->set_saw_error(); 1527 return false; 1528 } 1529 1530 if (lseek(this->fd_, - got, SEEK_CUR) < 0) 1531 { 1532 if (!this->saw_error()) 1533 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m"); 1534 this->set_saw_error(); 1535 return false; 1536 } 1537 1538 if (static_cast<size_t>(got) < length) 1539 return false; 1540 1541 *bytes = this->data_.data(); 1542 return true; 1543 } 1544 1545 // Advance. 1546 1547 void 1548 Stream_from_file::do_advance(size_t skip) 1549 { 1550 if (lseek(this->fd_, skip, SEEK_CUR) < 0) 1551 { 1552 if (!this->saw_error()) 1553 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m"); 1554 this->set_saw_error(); 1555 } 1556 if (!this->data_.empty()) 1557 { 1558 if (this->data_.length() > skip) 1559 this->data_.erase(0, skip); 1560 else 1561 this->data_.clear(); 1562 } 1563 } 1564 1565 // Class Import_function_body. 1566 1567 Import_function_body::Import_function_body(Gogo* gogo, 1568 Import* imp, 1569 Named_object* named_object, 1570 const std::string& body, 1571 size_t off, 1572 Block* block, 1573 int indent) 1574 : gogo_(gogo), imp_(imp), named_object_(named_object), body_(body), 1575 off_(off), indent_(indent), temporaries_(), labels_(), 1576 saw_error_(false) 1577 { 1578 this->blocks_.push_back(block); 1579 } 1580 1581 Import_function_body::~Import_function_body() 1582 { 1583 // At this point we should be left with the original outer block only. 1584 go_assert(saw_errors() || this->blocks_.size() == 1); 1585 } 1586 1587 // The name of the function we are parsing. 1588 1589 const std::string& 1590 Import_function_body::name() const 1591 { 1592 return this->named_object_->name(); 1593 } 1594 1595 // Class Import_function_body. 1596 1597 // Require that the next bytes match STR, issuing an error if not. 1598 // Advance past the string. 1599 1600 void 1601 Import_function_body::require_c_string(const char* str) 1602 { 1603 if (!this->match_c_string(str)) 1604 { 1605 if (!this->saw_error_) 1606 go_error_at(this->location(), 1607 "invalid export data for %qs: expected %qs at %lu", 1608 this->name().c_str(), str, 1609 static_cast<unsigned long>(this->off_)); 1610 this->saw_error_ = true; 1611 return; 1612 } 1613 this->advance(strlen(str)); 1614 } 1615 1616 // Read an identifier. 1617 1618 std::string 1619 Import_function_body::read_identifier() 1620 { 1621 size_t start = this->off_; 1622 for (size_t i = start; i < this->body_.length(); i++) 1623 { 1624 int c = static_cast<unsigned char>(this->body_[i]); 1625 if (strchr(identifier_stop, c) != NULL) 1626 { 1627 this->off_ = i; 1628 return this->body_.substr(start, i - start); 1629 } 1630 1631 // FIXME: Probably we shouldn't accept '.', but that might break 1632 // some existing imports. 1633 if (c == '.' 1634 && i + 2 < this->body_.length() 1635 && this->body_[i + 1] == '.' 1636 && this->body_[i + 2] == '.') 1637 { 1638 this->off_ = i; 1639 return this->body_.substr(start, i - start); 1640 } 1641 } 1642 this->off_ = this->body_.length(); 1643 return this->body_.substr(start); 1644 } 1645 1646 // Read a type. 1647 1648 Type* 1649 Import_function_body::read_type() 1650 { 1651 this->require_c_string("<type "); 1652 size_t start = this->off_; 1653 size_t i; 1654 int c = '\0'; 1655 for (i = start; i < this->body_.length(); ++i) 1656 { 1657 c = static_cast<unsigned char>(this->body_[i]); 1658 if (c != '-' && (c < '0' || c > '9')) 1659 break; 1660 } 1661 this->off_ = i + 1; 1662 1663 char *end; 1664 std::string num = this->body_.substr(start, i - start); 1665 long val = strtol(num.c_str(), &end, 10); 1666 if (*end != '\0' || val > 0x7fffffff) 1667 { 1668 if (!this->saw_error_) 1669 go_error_at(this->location(), 1670 "invalid export data for %qs: expected integer at %lu", 1671 this->name().c_str(), 1672 static_cast<unsigned long>(start)); 1673 this->saw_error_ = true; 1674 return Type::make_error_type(); 1675 } 1676 1677 if (c != '>') 1678 { 1679 if (!this->saw_error_) 1680 go_error_at(this->location(), 1681 "invalid export data for %qs: expected %<>%> at %lu", 1682 this->name().c_str(), 1683 static_cast<unsigned long>(i)); 1684 this->saw_error_ = true; 1685 return Type::make_error_type(); 1686 } 1687 1688 bool parsed; 1689 Type* type = this->imp_->type_for_index(static_cast<int>(val), this->name(), 1690 static_cast<unsigned long>(start), 1691 &parsed); 1692 1693 // If we just read this type's information, its methods will not 1694 // have been finalized. Do that now. 1695 if (parsed) 1696 this->gogo_->finalize_methods_for_type(type); 1697 1698 return type; 1699 } 1700 1701 // Return the next size to use for a vector mapping indexes to values. 1702 1703 size_t 1704 Import_function_body::next_size(size_t have) 1705 { 1706 if (have == 0) 1707 return 8; 1708 else if (have < 256) 1709 return have * 2; 1710 else 1711 return have + 64; 1712 } 1713 1714 // Record the index of a temporary statement. 1715 1716 void 1717 Import_function_body::record_temporary(Temporary_statement* temp, 1718 unsigned int idx) 1719 { 1720 size_t have = this->temporaries_.size(); 1721 while (static_cast<size_t>(idx) >= have) 1722 { 1723 size_t want = Import_function_body::next_size(have); 1724 this->temporaries_.resize(want, NULL); 1725 have = want; 1726 } 1727 this->temporaries_[idx] = temp; 1728 } 1729 1730 // Return a temporary statement given an index. 1731 1732 Temporary_statement* 1733 Import_function_body::temporary_statement(unsigned int idx) 1734 { 1735 if (static_cast<size_t>(idx) >= this->temporaries_.size()) 1736 return NULL; 1737 return this->temporaries_[idx]; 1738 } 1739 1740 // Return an unnamed label given an index, defining the label if we 1741 // haven't seen it already. 1742 1743 Unnamed_label* 1744 Import_function_body::unnamed_label(unsigned int idx, Location loc) 1745 { 1746 size_t have = this->labels_.size(); 1747 while (static_cast<size_t>(idx) >= have) 1748 { 1749 size_t want = Import_function_body::next_size(have); 1750 this->labels_.resize(want, NULL); 1751 have = want; 1752 } 1753 Unnamed_label* label = this->labels_[idx]; 1754 if (label == NULL) 1755 { 1756 label = new Unnamed_label(loc); 1757 this->labels_[idx] = label; 1758 } 1759 return label; 1760 }