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