1 // import.cc -- Go frontend import declarations.
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.
10 #include "simple-object.h"
23 // The list of paths we search for import files.
25 static std::vector<std::string> search_path;
27 // Add a directory to the search path. This is called from the option
28 // handling language hook.
32 go_add_search_path(const char* path)
34 search_path.push_back(std::string(path));
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.
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.
44 // When FILENAME does not exist, we try modifying FILENAME to find the
45 // file. We use the first of these which exists:
46 // * We append ".gox".
47 // * We turn the base of FILENAME into libFILENAME.so.
48 // * We turn the base of FILENAME into libFILENAME.a.
51 // When using a search path, we apply each of these transformations at
52 // each entry on the search path before moving on to the next entry.
53 // If the file exists, but does not contain any Go export data, we
54 // stop; we do not keep looking for another file with the same name
55 // later in the search path.
58 Import::open_package(const std::string& filename, Location location)
61 if (IS_ABSOLUTE_PATH(filename))
63 else if (filename[0] == '.' && IS_DIR_SEPARATOR(filename[1]))
65 else if (filename[0] == '.'
67 && IS_DIR_SEPARATOR(filename[2]))
73 for (std::vector<std::string>::const_iterator p = search_path.begin();
74 p != search_path.end();
77 std::string indir = *p;
78 if (!indir.empty() && indir[indir.size() - 1] != '/')
81 Stream* s = Import::try_package_in_directory(indir, location);
87 Stream* s = Import::try_package_in_directory(filename, location);
94 // Try to find the export data for FILENAME.
97 Import::try_package_in_directory(const std::string& filename,
100 std::string found_filename = filename;
101 int fd = open(found_filename.c_str(), O_RDONLY | O_BINARY);
106 if (fstat(fd, &s) >= 0 && S_ISDIR(s.st_mode))
116 if (errno != ENOENT && errno != EISDIR)
117 warning_at(location, 0, "%s: %m", filename.c_str());
119 fd = Import::try_suffixes(&found_filename);
124 // The export data may not be in this file.
125 Stream* s = Import::find_export_data(found_filename, fd, location);
131 error_at(location, "%s exists but does not contain any Go export data",
132 found_filename.c_str());
137 // Given import "*PFILENAME", where *PFILENAME does not exist, try
138 // various suffixes. If we find one, set *PFILENAME to the one we
139 // found. Return the open file descriptor.
142 Import::try_suffixes(std::string* pfilename)
144 std::string filename = *pfilename + ".gox";
145 int fd = open(filename.c_str(), O_RDONLY | O_BINARY);
148 *pfilename = filename;
152 const char* basename = lbasename(pfilename->c_str());
153 size_t basename_pos = basename - pfilename->c_str();
154 filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".so";
155 fd = open(filename.c_str(), O_RDONLY | O_BINARY);
158 *pfilename = filename;
162 filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".a";
163 fd = open(filename.c_str(), O_RDONLY | O_BINARY);
166 *pfilename = filename;
170 filename = *pfilename + ".o";
171 fd = open(filename.c_str(), O_RDONLY | O_BINARY);
174 *pfilename = filename;
181 // Look for export data in the file descriptor FD.
184 Import::find_export_data(const std::string& filename, int fd,
187 // See if we can read this as an object file.
188 Import::Stream* stream = Import::find_object_export_data(filename, fd, 0,
193 const int len = MAX(Export::v1_magic_len, Import::archive_magic_len);
195 if (lseek(fd, 0, SEEK_SET) < 0)
197 error_at(location, "lseek %s failed: %m", filename.c_str());
202 ssize_t c = read(fd, buf, len);
206 // Check for a file containing nothing but Go export data.
207 if (memcmp(buf, Export::v1_magic, Export::v1_magic_len) == 0)
208 return new Stream_from_file(fd);
210 // See if we can read this as an archive.
211 if (Import::is_archive_magic(buf))
212 return Import::find_archive_export_data(filename, fd, location);
217 // Look for export data in a simple_object.
220 Import::find_object_export_data(const std::string& filename,
228 const char *errmsg = go_read_export_data(fd, offset, &buf, &len, &err);
232 error_at(location, "%s: %s", filename.c_str(), errmsg);
234 error_at(location, "%s: %s: %s", filename.c_str(), errmsg,
242 return new Stream_from_buffer(buf, len);
247 // Construct an Import object. We make the builtin_types_ vector
248 // large enough to hold all the builtin types.
250 Import::Import(Stream* stream, Location location)
251 : gogo_(NULL), stream_(stream), location_(location), package_(NULL),
252 add_to_globals_(false),
253 builtin_types_((- SMALLEST_BUILTIN_CODE) + 1),
258 // Import the data in the associated stream.
261 Import::import(Gogo* gogo, const std::string& local_name,
262 bool is_local_name_exported)
264 // Hold on to the Gogo structure. Otherwise we need to pass it
265 // through all the import functions, because we need it when reading
269 // A stream of export data can include data from more than one input
270 // file. Here we loop over each input file.
271 Stream* stream = this->stream_;
272 while (!stream->at_eof() && !stream->saw_error())
274 // The vector of types is package specific.
275 this->types_.clear();
277 stream->require_bytes(this->location_, Export::v1_magic,
278 Export::v1_magic_len);
280 this->require_c_string("package ");
281 std::string package_name = this->read_identifier();
282 this->require_c_string(";\n");
285 if (this->match_c_string("prefix "))
288 std::string unique_prefix = this->read_identifier();
289 this->require_c_string(";\n");
290 pkgpath = unique_prefix + '.' + package_name;
294 this->require_c_string("pkgpath ");
295 pkgpath = this->read_identifier();
296 this->require_c_string(";\n");
299 this->package_ = gogo->add_imported_package(package_name, local_name,
300 is_local_name_exported,
303 &this->add_to_globals_);
304 if (this->package_ == NULL)
306 stream->set_saw_error();
310 this->require_c_string("priority ");
311 std::string priority_string = this->read_identifier();
313 if (!this->string_to_int(priority_string, false, &prio))
315 this->package_->set_priority(prio);
316 this->require_c_string(";\n");
318 while (stream->match_c_string("import"))
319 this->read_one_import();
321 if (stream->match_c_string("init"))
322 this->read_import_init_fns(gogo);
324 // Loop over all the input data for this package.
325 while (!stream->saw_error())
327 if (stream->match_c_string("const "))
328 this->import_const();
329 else if (stream->match_c_string("type "))
331 else if (stream->match_c_string("var "))
333 else if (stream->match_c_string("func "))
334 this->import_func(this->package_);
335 else if (stream->match_c_string("checksum "))
339 error_at(this->location_,
340 ("error in import data at %d: "
341 "expected %<const%>, %<type%>, %<var%>, "
342 "%<func%>, or %<checksum%>"),
344 stream->set_saw_error();
349 // We currently ignore the checksum. In the future we could
350 // store the checksum somewhere in the generated object and then
351 // verify that the checksum matches at link time or at dynamic
353 this->require_c_string("checksum ");
354 stream->advance(Export::v1_checksum_len * 2);
355 this->require_c_string(";\n");
358 return this->package_;
361 // Read an import line. We don't actually care about these.
364 Import::read_one_import()
366 this->require_c_string("import ");
367 std::string package_name = this->read_identifier();
368 this->require_c_string(" ");
369 std::string pkgpath = this->read_identifier();
370 this->require_c_string(" \"");
371 Stream* stream = this->stream_;
372 while (stream->peek_char() != '"')
374 this->require_c_string("\";\n");
376 Package* p = this->gogo_->register_package(pkgpath,
377 Linemap::unknown_location());
378 p->set_package_name(package_name, this->location());
381 // Read the list of import control functions.
384 Import::read_import_init_fns(Gogo* gogo)
386 this->require_c_string("init");
387 while (!this->match_c_string(";"))
389 this->require_c_string(" ");
390 std::string package_name = this->read_identifier();
391 this->require_c_string(" ");
392 std::string init_name = this->read_identifier();
393 this->require_c_string(" ");
394 std::string prio_string = this->read_identifier();
396 if (!this->string_to_int(prio_string, false, &prio))
398 gogo->add_import_init_fn(package_name, init_name, prio);
400 this->require_c_string(";\n");
403 // Import a constant.
406 Import::import_const()
411 Named_constant::import_const(this, &name, &type, &expr);
412 Typed_identifier tid(name, type, this->location_);
413 Named_object* no = this->package_->add_constant(tid, expr);
414 if (this->add_to_globals_)
415 this->gogo_->add_named_object(no);
421 Import::import_type()
424 Named_type::import_named_type(this, &type);
426 // The named type has been added to the package by the type import
427 // process. Here we need to make it visible to the parser, and it
428 // to the global bindings if necessary.
429 type->set_is_visible();
431 if (this->add_to_globals_)
432 this->gogo_->add_named_type(type);
435 // Import a variable.
442 Variable::import_var(this, &name, &type);
443 Variable* var = new Variable(type, NULL, true, false, false,
446 no = this->package_->add_variable(name, var);
447 if (this->add_to_globals_)
448 this->gogo_->add_named_object(no);
451 // Import a function into PACKAGE. PACKAGE is normally
452 // THIS->PACKAGE_, but it will be different for a method associated
453 // with a type defined in a different package.
456 Import::import_func(Package* package)
459 Typed_identifier* receiver;
460 Typed_identifier_list* parameters;
461 Typed_identifier_list* results;
463 Function::import_func(this, &name, &receiver, ¶meters, &results,
465 Function_type *fntype = Type::make_function_type(receiver, parameters,
466 results, this->location_);
468 fntype->set_is_varargs();
470 Location loc = this->location_;
472 if (fntype->is_method())
474 Type* rtype = receiver->type();
476 // We may still be reading the definition of RTYPE, so we have
477 // to be careful to avoid calling base or convert. If RTYPE is
478 // a named type or a forward declaration, then we know that it
479 // is not a pointer, because we are reading a method on RTYPE
480 // and named pointers can't have methods.
482 if (rtype->classification() == Type::TYPE_POINTER)
483 rtype = rtype->points_to();
485 if (rtype->is_error_type())
487 else if (rtype->named_type() != NULL)
488 no = rtype->named_type()->add_method_declaration(name, package, fntype,
490 else if (rtype->forward_declaration_type() != NULL)
491 no = rtype->forward_declaration_type()->add_method_declaration(name,
500 no = package->add_function_declaration(name, fntype, loc);
501 if (this->add_to_globals_)
502 this->gogo_->add_named_object(no);
507 // Read a type in the import stream. This records the type by the
508 // type index. If the type is named, it registers the name, but marks
514 Stream* stream = this->stream_;
515 this->require_c_string("<type ");
521 c = stream->get_char();
522 if (c != '-' && (c < '0' || c > '9'))
528 if (!this->string_to_int(number, true, &index))
529 return Type::make_error_type();
533 // This type was already defined.
535 ? (static_cast<size_t>(- index) >= this->builtin_types_.size()
536 || this->builtin_types_[- index] == NULL)
537 : (static_cast<size_t>(index) >= this->types_.size()
538 || this->types_[index] == NULL))
540 error_at(this->location_,
541 "error in import data at %d: bad type index %d",
542 stream->pos(), index);
543 stream->set_saw_error();
544 return Type::make_error_type();
547 return index < 0 ? this->builtin_types_[- index] : this->types_[index];
552 if (!stream->saw_error())
553 error_at(this->location_,
554 "error in import data at %d: expect %< %> or %<>%>'",
556 stream->set_saw_error();
558 return Type::make_error_type();
562 || (static_cast<size_t>(index) < this->types_.size()
563 && this->types_[index] != NULL))
565 error_at(this->location_,
566 "error in import data at %d: type index already defined",
568 stream->set_saw_error();
569 return Type::make_error_type();
572 if (static_cast<size_t>(index) >= this->types_.size())
574 int newsize = std::max(static_cast<size_t>(index) + 1,
575 this->types_.size() * 2);
576 this->types_.resize(newsize, NULL);
579 if (stream->peek_char() != '"')
581 Type* type = Type::import_type(this);
582 this->require_c_string(">");
583 this->types_[index] = type;
587 // This type has a name.
590 std::string type_name;
591 while ((c = stream->get_char()) != '"')
594 // If this type is in the package we are currently importing, the
595 // name will be .PKGPATH.NAME or simply NAME with no dots.
596 // Otherwise, a non-hidden symbol will be PKGPATH.NAME and a hidden
597 // symbol will be .PKGPATH.NAME.
599 if (type_name.find('.') != std::string::npos)
602 if (type_name[0] == '.')
604 size_t dot = type_name.rfind('.');
605 pkgpath = type_name.substr(start, dot - start);
606 if (type_name[0] != '.')
607 type_name.erase(0, dot + 1);
610 this->require_c_string(" ");
612 // The package name may follow. This is the name of the package in
613 // the package clause of that package. The type name will include
614 // the pkgpath, which may be different.
615 std::string package_name;
616 if (stream->peek_char() == '"')
619 while ((c = stream->get_char()) != '"')
621 this->require_c_string(" ");
624 // Declare the type in the appropriate package. If we haven't seen
625 // it before, mark it as invisible. We declare it before we read
626 // the actual definition of the type, since the definition may refer
627 // to the type itself.
629 if (pkgpath.empty() || pkgpath == this->gogo_->pkgpath())
630 package = this->package_;
633 package = this->gogo_->register_package(pkgpath,
634 Linemap::unknown_location());
635 if (!package_name.empty())
636 package->set_package_name(package_name, this->location());
639 Named_object* no = package->bindings()->lookup(type_name);
641 no = package->add_type_declaration(type_name, this->location_);
642 else if (!no->is_type_declaration() && !no->is_type())
644 error_at(this->location_, "imported %<%s.%s%> both type and non-type",
645 pkgpath.c_str(), Gogo::message_name(type_name).c_str());
646 stream->set_saw_error();
647 return Type::make_error_type();
650 go_assert(no->package() == package);
652 if (this->types_[index] == NULL)
654 if (no->is_type_declaration())
656 // FIXME: It's silly to make a forward declaration every time.
657 this->types_[index] = Type::make_forward_declaration(no);
661 go_assert(no->is_type());
662 this->types_[index] = no->type_value();
666 // If there is no type definition, then this is just a forward
667 // declaration of a type defined in some other file.
669 if (this->match_c_string(">"))
670 type = this->types_[index];
673 type = this->read_type();
675 if (no->is_type_declaration())
677 // We can define the type now.
679 no = package->add_type(type_name, type, this->location_);
680 Named_type* ntype = no->type_value();
682 // This type has not yet been imported.
683 ntype->clear_is_visible();
685 if (!type->is_undefined() && type->interface_type() != NULL)
686 this->gogo_->record_interface_type(type->interface_type());
690 else if (no->is_type())
692 // We have seen this type before. FIXME: it would be a good
693 // idea to check that the two imported types are identical,
694 // but we have not finalized the methods yet, which means
695 // that we can not reliably compare interface types.
696 type = no->type_value();
698 // Don't change the visibility of the existing type.
701 this->types_[index] = type;
703 // Read the type methods.
704 if (this->match_c_string("\n"))
707 while (this->match_c_string(" func"))
710 this->import_func(package);
715 this->require_c_string(">");
720 // Register the builtin types.
723 Import::register_builtin_types(Gogo* gogo)
725 this->register_builtin_type(gogo, "int8", BUILTIN_INT8);
726 this->register_builtin_type(gogo, "int16", BUILTIN_INT16);
727 this->register_builtin_type(gogo, "int32", BUILTIN_INT32);
728 this->register_builtin_type(gogo, "int64", BUILTIN_INT64);
729 this->register_builtin_type(gogo, "uint8", BUILTIN_UINT8);
730 this->register_builtin_type(gogo, "uint16", BUILTIN_UINT16);
731 this->register_builtin_type(gogo, "uint32", BUILTIN_UINT32);
732 this->register_builtin_type(gogo, "uint64", BUILTIN_UINT64);
733 this->register_builtin_type(gogo, "float32", BUILTIN_FLOAT32);
734 this->register_builtin_type(gogo, "float64", BUILTIN_FLOAT64);
735 this->register_builtin_type(gogo, "complex64", BUILTIN_COMPLEX64);
736 this->register_builtin_type(gogo, "complex128", BUILTIN_COMPLEX128);
737 this->register_builtin_type(gogo, "int", BUILTIN_INT);
738 this->register_builtin_type(gogo, "uint", BUILTIN_UINT);
739 this->register_builtin_type(gogo, "uintptr", BUILTIN_UINTPTR);
740 this->register_builtin_type(gogo, "bool", BUILTIN_BOOL);
741 this->register_builtin_type(gogo, "string", BUILTIN_STRING);
742 this->register_builtin_type(gogo, "error", BUILTIN_ERROR);
743 this->register_builtin_type(gogo, "byte", BUILTIN_BYTE);
744 this->register_builtin_type(gogo, "rune", BUILTIN_RUNE);
747 // Register a single builtin type.
750 Import::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
752 Named_object* named_object = gogo->lookup_global(name);
753 go_assert(named_object != NULL && named_object->is_type());
754 int index = - static_cast<int>(code);
756 && static_cast<size_t>(index) < this->builtin_types_.size());
757 this->builtin_types_[index] = named_object->type_value();
760 // Read an identifier from the stream.
763 Import::read_identifier()
766 Stream* stream = this->stream_;
770 c = stream->peek_char();
771 if (c == -1 || c == ' ' || c == ';')
779 // Read a name from the stream.
784 std::string ret = this->read_identifier();
787 else if (!Lex::is_exported_name(ret))
788 ret = '.' + this->package_->pkgpath() + '.' + ret;
792 // Turn a string into a integer with appropriate error handling.
795 Import::string_to_int(const std::string &s, bool is_neg_ok, int* ret)
798 long prio = strtol(s.c_str(), &end, 10);
799 if (*end != '\0' || prio > 0x7fffffff || (prio < 0 && !is_neg_ok))
801 error_at(this->location_, "invalid integer in import data at %d",
802 this->stream_->pos());
803 this->stream_->set_saw_error();
810 // Class Import::Stream.
812 Import::Stream::Stream()
813 : pos_(0), saw_error_(false)
817 Import::Stream::~Stream()
821 // Return the next character to come from the stream.
824 Import::Stream::peek_char()
827 if (!this->do_peek(1, &read))
829 // Make sure we return an unsigned char, so that we don't get
831 unsigned char ret = *read;
835 // Return true if the next LENGTH characters from the stream match
839 Import::Stream::match_bytes(const char* bytes, size_t length)
842 if (!this->do_peek(length, &read))
844 return memcmp(bytes, read, length) == 0;
847 // Require that the next LENGTH bytes from the stream match BYTES.
850 Import::Stream::require_bytes(Location location, const char* bytes,
854 if (!this->do_peek(length, &read)
855 || memcmp(bytes, read, length) != 0)
857 if (!this->saw_error_)
858 error_at(location, "import error at %d: expected %<%.*s%>",
859 this->pos(), static_cast<int>(length), bytes);
860 this->saw_error_ = true;
863 this->advance(length);
866 // Class Stream_from_file.
868 Stream_from_file::Stream_from_file(int fd)
871 if (lseek(fd, 0, SEEK_SET) != 0)
873 error("lseek failed: %m");
874 this->set_saw_error();
878 Stream_from_file::~Stream_from_file()
886 Stream_from_file::do_peek(size_t length, const char** bytes)
888 if (this->data_.length() <= length)
890 *bytes = this->data_.data();
893 // Don't bother to handle the general case, since we don't need it.
894 go_assert(length < 64);
896 ssize_t got = read(this->fd_, buf, length);
900 if (!this->saw_error())
901 error("read failed: %m");
902 this->set_saw_error();
906 if (lseek(this->fd_, - got, SEEK_CUR) != 0)
908 if (!this->saw_error())
909 error("lseek failed: %m");
910 this->set_saw_error();
914 if (static_cast<size_t>(got) < length)
917 this->data_.assign(buf, got);
919 *bytes = this->data_.data();
926 Stream_from_file::do_advance(size_t skip)
928 if (lseek(this->fd_, skip, SEEK_CUR) != 0)
930 if (!this->saw_error())
931 error("lseek failed: %m");
932 this->set_saw_error();
934 if (!this->data_.empty())
936 if (this->data_.length() < skip)
937 this->data_.erase(0, skip);