1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
|
// import.h -- Go frontend import declarations. -*- C++ -*-
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#ifndef GO_IMPORT_H
#define GO_IMPORT_H
#include "export.h"
#include "go-linemap.h"
class Gogo;
class Block;
class Package;
class Type;
class Named_object;
class Named_type;
class Expression;
class Import_function_body;
class Temporary_statement;
class Unnamed_label;
class Finalize_methods;
// Expressions can be imported either directly from import data (for
// simple constant expressions that can appear in a const declaration
// or as an array length in a type definition) or from an exported
// function body (for an inlinable function). These two cases happen
// at different points in the compilation and have different
// requirements, so it's not easy to unify them. Import_expression is
// an abstract interface that permits the expression import code to
// work at either point. When importing expressions that only occur
// for an inlinable function, the ifb method is available to get the
// full Import_function_body.
class Import_expression
{
public:
// Return the import function body. This should only be called for
// expressions that can not appear outside of an inlinable function
// body.
virtual Import_function_body*
ifb() = 0;
// The location to report in an error message.
virtual Location
location() const = 0;
// Peek at the next character in the input, returning a value from 0
// to 0xff. Returns -1 at end of stream.
virtual int
peek_char() = 0;
// Return the next character and advance.
virtual int
get_char() = 0;
// Return true if the next bytes match STR.
virtual bool
match_c_string(const char* str) = 0;
// Require that the next bytes match STR.
virtual void
require_c_string(const char* str) = 0;
// Advance the stream SKIP bytes.
virtual void
advance(size_t skip) = 0;
// Read an identifier.
virtual std::string
read_identifier() = 0;
// Read a type.
virtual Type*
read_type() = 0;
// Return the maximum valid package index.
virtual size_t
max_package_index() const = 0;
// Return the package for a package index.
virtual Package*
package_at_index(int index) = 0;
// Return the version number of the export data we're reading.
virtual Export_data_version
version() const = 0;
};
// This class manages importing Go declarations.
class Import : public Import_expression
{
public:
// The Stream class is an interface used to read the data. The
// caller should instantiate a child of this class.
class Stream
{
public:
Stream();
virtual ~Stream();
// Set the position, for error messages.
void
set_pos(int pos)
{ this->pos_ = pos; }
// Return whether we have seen an error.
bool
saw_error() const
{ return this->saw_error_; }
// Record that we've seen an error.
void
set_saw_error()
{ this->saw_error_ = true; }
// Return the next character (a value from 0 to 0xff) without
// advancing. Returns -1 at end of stream.
int
peek_char();
// Look for LENGTH characters, setting *BYTES to point to them.
// Returns false if the bytes are not available. Does not
// advance.
bool
peek(size_t length, const char** bytes)
{ return this->do_peek(length, bytes); }
// Return the next character (a value from 0 to 0xff) and advance
// the read position by 1. Returns -1 at end of stream.
int
get_char()
{
int c = this->peek_char();
this->advance(1);
return c;
}
// Return true if at the end of the stream.
bool
at_eof()
{ return this->peek_char() == -1; }
// Return true if the next bytes match STR.
bool
match_c_string(const char* str)
{ return this->match_bytes(str, strlen(str)); }
// Return true if the next LENGTH bytes match BYTES.
bool
match_bytes(const char* bytes, size_t length);
// Give an error if the next bytes do not match STR. Advance the
// read position by the length of STR.
void
require_c_string(Location location, const char* str)
{ this->require_bytes(location, str, strlen(str)); }
// Given an error if the next LENGTH bytes do not match BYTES.
// Advance the read position by LENGTH.
void
require_bytes(Location, const char* bytes, size_t length);
// Advance the read position by SKIP bytes.
void
advance(size_t skip)
{
this->do_advance(skip);
this->pos_ += skip;
}
// Return the current read position. This returns int because it
// is more convenient in error reporting. FIXME.
int
pos()
{ return static_cast<int>(this->pos_); }
protected:
// This function should set *BYTES to point to a buffer holding
// the LENGTH bytes at the current read position. It should
// return false if the bytes are not available. This should not
// change the current read position.
virtual bool
do_peek(size_t length, const char** bytes) = 0;
// This function should advance the current read position LENGTH
// bytes.
virtual void
do_advance(size_t skip) = 0;
private:
// The current read position.
size_t pos_;
// True if we've seen an error reading from this stream.
bool saw_error_;
};
// Find import data. This searches the file system for FILENAME and
// returns a pointer to a Stream object to read the data that it
// exports. LOCATION is the location of the import statement.
// RELATIVE_IMPORT_PATH is used as a prefix for a relative import.
static Stream*
open_package(const std::string& filename, Location location,
const std::string& relative_import_path);
// Constructor.
Import(Stream*, Location);
// Register the builtin types.
void
register_builtin_types(Gogo*);
// Import everything defined in the stream. LOCAL_NAME is the local
// name to be used for bindings; if it is the string "." then
// bindings should be inserted in the global scope. If LOCAL_NAME
// is the empty string then the name of the package itself is the
// local name. This returns the imported package, or NULL on error.
Package*
import(Gogo*, const std::string& local_name, bool is_local_name_exported);
// The location of the import statement.
Location
location() const
{ return this->location_; }
// Return the package we are importing.
Package*
package() const
{ return this->package_; }
// Return the next character.
int
peek_char()
{ return this->stream_->peek_char(); }
// Return the next character and advance.
int
get_char()
{ return this->stream_->get_char(); }
// Read LENGTH characters into *OUT and advance past them. On
// EOF reports an error and sets *OUT to an empty string.
void
read(size_t length, std::string* out);
// Return true at the end of the stream.
bool
at_eof()
{ return this->stream_->at_eof(); }
// Return whether the next bytes match STR.
bool
match_c_string(const char* str)
{ return this->stream_->match_c_string(str); }
// Require that the next bytes match STR.
void
require_c_string(const char* str)
{ this->stream_->require_c_string(this->location_, str); }
// Advance the stream SKIP bytes.
void
advance(size_t skip)
{ this->stream_->advance(skip); }
// Stream position, for error reporting.
int
pos()
{ return this->stream_->pos(); }
// Return the version number of the export data we're reading.
Export_data_version
version() const { return this->version_; }
// Skip a semicolon if using an older version.
void
require_semicolon_if_old_version()
{
if (this->version_ == EXPORT_FORMAT_V1
|| this->version_ == EXPORT_FORMAT_V2)
this->require_c_string(";");
}
// Read an identifier.
std::string
read_identifier();
// Read a name. This is like read_identifier, except that a "?" is
// returned as an empty string. This matches Export::write_name.
std::string
read_name();
// Return the maximum valid package index. This is the size of
// packages_ because we will subtract 1 in package_at_index.
size_t
max_package_index() const
{ return this->packages_.size(); }
// Return the package at an index. (We subtract 1 because package
// index 0 is not used.)
Package*
package_at_index(int index)
{ return this->packages_.at(index - 1); }
// Read a type.
Type*
read_type();
// Return the type for a type index. INPUT_NAME and INPUT_OFFSET
// are only for error reporting. PARSED is set to whether we parsed
// the type information for a new type.
Type*
type_for_index(int index, const std::string& input_name,
size_t input_offset, bool* parsed);
// Read an escape note.
std::string
read_escape();
// Clear the stream when it is no longer accessible.
void
clear_stream()
{ this->stream_ = NULL; }
// Just so that Import implements Import_expression.
Import_function_body*
ifb()
{ return NULL; }
// Read a qualified identifier from an Import_expression. Sets
// *NAME, *PKG, and *IS_EXPORTED, and reports whether it succeeded.
static bool
read_qualified_identifier(Import_expression*, std::string* name,
Package** pkg, bool* is_exported);
private:
static Stream*
try_package_in_directory(const std::string&, Location);
static int
try_suffixes(std::string*);
static Stream*
find_export_data(const std::string& filename, int fd, Location);
static Stream*
find_object_export_data(const std::string& filename, int fd,
off_t offset, Location);
static const int archive_magic_len = 8;
static bool
is_archive_magic(const char*);
static Stream*
find_archive_export_data(const std::string& filename, int fd,
Location);
// Read a package line.
void
read_one_package();
// Read an import line.
void
read_one_import();
// Read an indirectimport line.
void
read_one_indirect_import();
// Read the import control functions and init graph.
void
read_import_init_fns(Gogo*);
// Read the types.
bool
read_types();
// Import a constant.
void
import_const();
// Import a type.
void
import_type();
// Import a variable.
void
import_var();
// Import a function.
void
import_func(Package*);
// Parse a type definition.
bool
parse_type(int index);
// Read a named type and store it at this->type_[index].
Type*
read_named_type(int index);
// Register a single builtin type.
void
register_builtin_type(Gogo*, const char* name, Builtin_code);
// Get an integer from a string.
bool
string_to_int(const std::string&, bool is_neg_ok, int* ret);
// Get an unsigned integer from a string.
bool
string_to_unsigned(const std::string& s, unsigned* ret)
{
int ivalue;
if (!this->string_to_int(s, false, &ivalue))
return false;
*ret = static_cast<unsigned>(ivalue);
return true;
}
// Finalize methods for newly imported types.
void
finalize_methods();
// The general IR.
Gogo* gogo_;
// The stream from which to read import data.
Stream* stream_;
// The location of the import statement we are processing.
Location location_;
// The package we are importing.
Package* package_;
// Whether to add new objects to the global scope, rather than to a
// package scope.
bool add_to_globals_;
// Mapping from package index to package.
std::vector<Package*> packages_;
// All type data.
std::string type_data_;
// Position of type data in the stream.
int type_pos_;
// Mapping from type code to offset/length in type_data_.
std::vector<std::pair<size_t, size_t> > type_offsets_;
// Mapping from negated builtin type codes to Type structures.
std::vector<Named_type*> builtin_types_;
// Mapping from exported type codes to Type structures.
std::vector<Type*> types_;
// Version of export data we're reading.
Export_data_version version_;
};
// Read import data from a string.
class Stream_from_string : public Import::Stream
{
public:
Stream_from_string(const std::string& str)
: str_(str), pos_(0)
{ }
protected:
bool
do_peek(size_t length, const char** bytes)
{
if (this->pos_ + length > this->str_.length())
return false;
*bytes = this->str_.data() + this->pos_;
return true;
}
void
do_advance(size_t len)
{ this->pos_ += len; }
private:
// The string of data we are reading.
std::string str_;
// The current position within the string.
size_t pos_;
};
// Read import data from a buffer allocated using malloc.
class Stream_from_buffer : public Import::Stream
{
public:
Stream_from_buffer(char* buf, size_t length)
: buf_(buf), length_(length), pos_(0)
{ }
~Stream_from_buffer()
{ free(this->buf_); }
protected:
bool
do_peek(size_t length, const char** bytes)
{
if (this->pos_ + length > this->length_)
return false;
*bytes = this->buf_ + this->pos_;
return true;
}
void
do_advance(size_t len)
{ this->pos_ += len; }
private:
// The data we are reading.
char* buf_;
// The length of the buffer.
size_t length_;
// The current position within the buffer.
size_t pos_;
};
// Read import data from an open file descriptor.
class Stream_from_file : public Import::Stream
{
public:
Stream_from_file(int fd);
~Stream_from_file();
protected:
bool
do_peek(size_t, const char**);
void
do_advance(size_t);
private:
// No copying.
Stream_from_file(const Stream_from_file&);
Stream_from_file& operator=(const Stream_from_file&);
// The file descriptor.
int fd_;
// Data read from the file.
std::string data_;
};
// Read import data from an offset into a std::string. This uses a
// reference to the string, to avoid copying, so the string must be
// kept alive through some other mechanism.
class Stream_from_string_ref : public Import::Stream
{
public:
Stream_from_string_ref(const std::string& str, size_t offset, size_t length)
: str_(str), pos_(offset), end_(offset + length)
{ }
~Stream_from_string_ref()
{}
protected:
bool
do_peek(size_t length, const char** bytes)
{
if (this->pos_ + length > this->end_)
return false;
*bytes = &this->str_[this->pos_];
return true;
}
void
do_advance(size_t length)
{ this->pos_ += length; }
private:
// A reference to the string we are reading from.
const std::string& str_;
// The current offset into the string.
size_t pos_;
// The index after the last byte we can read.
size_t end_;
};
// Class to manage importing a function body. This is passed around
// to Statements and Expressions. It parses the function into the IR.
class Import_function_body : public Import_expression
{
public:
Import_function_body(Gogo* gogo, Import* imp, Named_object* named_object,
const std::string& body, size_t off, Block* block,
int indent);
~Import_function_body();
// The IR.
Gogo*
gogo()
{ return this->gogo_; }
// The location to report in an error message.
Location
location() const
{ return this->imp_->location(); }
// The function we are importing.
Named_object*
function() const
{ return this->named_object_; }
// A reference to the body we are reading.
const std::string&
body() const
{ return this->body_; }
// The current offset into the body.
size_t
off()
{ return this->off_; }
// Update the offset into the body.
void
set_off(size_t off)
{ this->off_ = off; }
// Advance the offset by SKIP bytes.
void
advance(size_t skip)
{ this->off_ += skip; }
// The current block.
Block*
block()
{ return this->blocks_.back(); }
// Begin importing a new block BLOCK nested within the current block.
void
begin_block(Block *block)
{ this->blocks_.push_back(block); }
// Record the fact that we're done importing the current block.
void
finish_block()
{ this->blocks_.pop_back(); }
// The current indentation.
int
indent() const
{ return this->indent_; }
// Increment the indentation level.
void
increment_indent()
{ ++this->indent_; }
// Decrement the indentation level.
void
decrement_indent()
{ --this->indent_; }
// The name of the function we are parsing.
const std::string&
name() const;
// Return the next character in the input stream, or -1 at the end.
int
peek_char()
{
if (this->body_.length() <= this->off_)
return -1;
return static_cast<unsigned char>(this->body_[this->off_]);
}
// Return the next character and advance.
int
get_char()
{
if (this->body_.length() <= this->off_)
return -1;
int c = static_cast<unsigned char>(this->body_[this->off_]);
this->off_++;
return c;
}
// Return whether the C string matches the current body position.
bool
match_c_string(const char* str)
{
size_t len = strlen(str);
return (this->body_.length() >= this->off_ + len
&& this->body_.compare(this->off_, len, str) == 0);
}
// Give an error if the next bytes do not match STR. Advance the
// offset by the length of STR.
void
require_c_string(const char* str);
// Read an identifier.
std::string
read_identifier();
// Read a type.
Type*
read_type();
Export_data_version
version() const
{ return this->imp_->version(); }
// Record the index of a temporary statement.
void
record_temporary(Temporary_statement*, unsigned int);
// Return a temporary statement given an index.
Temporary_statement*
temporary_statement(unsigned int);
// Return an unnamed label given an index, defining the label if we
// haven't seen it already.
Unnamed_label*
unnamed_label(unsigned int, Location);
// Implement Import_expression.
Import_function_body*
ifb()
{ return this; }
// Return the maximum valid package index.
size_t
max_package_index() const
{ return this->imp_->max_package_index(); }
// Return the package at an index.
Package*
package_at_index(int index)
{ return this->imp_->package_at_index(index); }
// Return whether we have seen an error.
bool
saw_error() const
{ return this->saw_error_; }
// Record that we have seen an error.
void
set_saw_error()
{ this->saw_error_ = true; }
private:
static size_t
next_size(size_t);
// The IR.
Gogo* gogo_;
// The importer.
Import* imp_;
// The function we are parsing.
Named_object* named_object_;
// The exported data we are parsing. Note that this is a reference;
// the body string must laster longer than this object.
const std::string& body_;
// The current offset into body_.
size_t off_;
// Stack to record nesting of blocks being imported.
std::vector<Block *> blocks_;
// Current expected indentation level.
int indent_;
// Temporary statements by index.
std::vector<Temporary_statement*> temporaries_;
// Unnamed labels by index.
std::vector<Unnamed_label*> labels_;
// Whether we've seen an error. Used to avoid reporting excess
// errors.
bool saw_error_;
};
#endif // !defined(GO_IMPORT_H)
|