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"
22 // The list of paths we search for import files.
24 static std::vector<std::string> search_path;
26 // Add a directory to the search path. This is called from the option
27 // handling language hook.
31 go_add_search_path(const char* path)
33 search_path.push_back(std::string(path));
36 // The name used for parameters, receivers, and results in imported
39 const char* const Import::import_marker = "*imported*";
41 // Find import data. This searches the file system for FILENAME and
42 // returns a pointer to a Stream object to read the data that it
43 // exports. If the file is not found, it returns NULL.
45 // When FILENAME is not an absolute path, we use the search path
46 // provided by -I and -L options.
48 // When FILENAME does not exist, we try modifying FILENAME to find the
49 // file. We use the first of these which exists:
50 // * We append ".gox".
51 // * We turn the base of FILENAME into libFILENAME.so.
52 // * We turn the base of FILENAME into libFILENAME.a.
55 // When using a search path, we apply each of these transformations at
56 // each entry on the search path before moving on to the next entry.
57 // If the file exists, but does not contain any Go export data, we
58 // stop; we do not keep looking for another file with the same name
59 // later in the search path.
62 Import::open_package(const std::string& filename, source_location location)
64 if (!IS_ABSOLUTE_PATH(filename))
66 for (std::vector<std::string>::const_iterator p = search_path.begin();
67 p != search_path.end();
70 std::string indir = *p;
71 if (!indir.empty() && indir[indir.size() - 1] != '/')
74 Stream* s = Import::try_package_in_directory(indir, location);
80 Stream* s = Import::try_package_in_directory(filename, location);
87 // Try to find the export data for FILENAME.
90 Import::try_package_in_directory(const std::string& filename,
91 source_location location)
93 std::string found_filename = filename;
94 int fd = open(found_filename.c_str(), O_RDONLY | O_BINARY);
99 if (fstat(fd, &s) >= 0 && S_ISDIR(s.st_mode))
109 if (errno != ENOENT && errno != EISDIR)
110 warning_at(location, 0, "%s: %m", filename.c_str());
112 fd = Import::try_suffixes(&found_filename);
117 // The export data may not be in this file.
118 Stream* s = Import::find_export_data(found_filename, fd, location);
124 error_at(location, "%s exists but does not contain any Go export data",
125 found_filename.c_str());
130 // Given import "*PFILENAME", where *PFILENAME does not exist, try
131 // various suffixes. If we find one, set *PFILENAME to the one we
132 // found. Return the open file descriptor.
135 Import::try_suffixes(std::string* pfilename)
137 std::string filename = *pfilename + ".gox";
138 int fd = open(filename.c_str(), O_RDONLY | O_BINARY);
141 *pfilename = filename;
145 const char* basename = lbasename(pfilename->c_str());
146 size_t basename_pos = basename - pfilename->c_str();
147 filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".so";
148 fd = open(filename.c_str(), O_RDONLY | O_BINARY);
151 *pfilename = filename;
155 filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".a";
156 fd = open(filename.c_str(), O_RDONLY | O_BINARY);
159 *pfilename = filename;
163 filename = *pfilename + ".o";
164 fd = open(filename.c_str(), O_RDONLY | O_BINARY);
167 *pfilename = filename;
174 // Look for export data in the file descriptor FD.
177 Import::find_export_data(const std::string& filename, int fd,
178 source_location location)
180 // See if we can read this as an object file.
181 Import::Stream* stream = Import::find_object_export_data(filename, fd, 0,
186 const int len = MAX(Export::v1_magic_len, Import::archive_magic_len);
188 if (lseek(fd, 0, SEEK_SET) < 0)
190 error_at(location, "lseek %s failed: %m", filename.c_str());
195 ssize_t c = read(fd, buf, len);
199 // Check for a file containing nothing but Go export data.
200 if (memcmp(buf, Export::v1_magic, Export::v1_magic_len) == 0)
201 return new Stream_from_file(fd);
203 // See if we can read this as an archive.
204 if (Import::is_archive_magic(buf))
205 return Import::find_archive_export_data(filename, fd, location);
210 // Look for export data in a simple_object.
213 Import::find_object_export_data(const std::string& filename,
216 source_location location)
220 simple_object_read* sobj = simple_object_start_read(fd, offset,
228 int found = simple_object_find_section(sobj, ".go_export", &sec_offset,
229 &sec_length, &errmsg, &err);
231 simple_object_release_read(sobj);
236 if (lseek(fd, offset + sec_offset, SEEK_SET) < 0)
238 error_at(location, "lseek %s failed: %m", filename.c_str());
242 char* buf = new char[sec_length];
243 ssize_t c = read(fd, buf, sec_length);
246 error_at(location, "read %s failed: %m", filename.c_str());
251 error_at(location, "%s: short read", filename.c_str());
255 return new Stream_from_buffer(buf, sec_length);
260 // Construct an Import object. We make the builtin_types_ vector
261 // large enough to hold all the builtin types.
263 Import::Import(Stream* stream, source_location location)
264 : gogo_(NULL), stream_(stream), location_(location), package_(NULL),
265 add_to_globals_(false),
266 builtin_types_((- SMALLEST_BUILTIN_CODE) + 1),
271 // Import the data in the associated stream.
274 Import::import(Gogo* gogo, const std::string& local_name,
275 bool is_local_name_exported)
277 // Hold on to the Gogo structure. Otherwise we need to pass it
278 // through all the import functions, because we need it when reading
282 // A stream of export data can include data from more than one input
283 // file. Here we loop over each input file.
284 Stream* stream = this->stream_;
285 while (!stream->at_eof() && !stream->saw_error())
287 // The vector of types is package specific.
288 this->types_.clear();
290 stream->require_bytes(this->location_, Export::v1_magic,
291 Export::v1_magic_len);
293 this->require_c_string("package ");
294 std::string package_name = this->read_identifier();
295 this->require_c_string(";\n");
297 this->require_c_string("prefix ");
298 std::string unique_prefix = this->read_identifier();
299 this->require_c_string(";\n");
301 this->package_ = gogo->add_imported_package(package_name, local_name,
302 is_local_name_exported,
305 &this->add_to_globals_);
306 if (this->package_ == NULL)
308 stream->set_saw_error();
312 this->require_c_string("priority ");
313 std::string priority_string = this->read_identifier();
315 if (!this->string_to_int(priority_string, false, &prio))
317 this->package_->set_priority(prio);
318 this->require_c_string(";\n");
320 if (stream->match_c_string("import "))
321 this->read_import_init_fns(gogo);
323 // Loop over all the input data for this package.
324 while (!stream->saw_error())
326 if (stream->match_c_string("const "))
327 this->import_const();
328 else if (stream->match_c_string("type "))
330 else if (stream->match_c_string("var "))
332 else if (stream->match_c_string("func "))
333 this->import_func(this->package_);
334 else if (stream->match_c_string("checksum "))
338 error_at(this->location_,
339 ("error in import data at %d: "
340 "expected %<const%>, %<type%>, %<var%>, "
341 "%<func%>, or %<checksum%>"),
343 stream->set_saw_error();
348 // We currently ignore the checksum. In the future we could
349 // store the checksum somewhere in the generated object and then
350 // verify that the checksum matches at link time or at dynamic
352 this->require_c_string("checksum ");
353 stream->advance(Export::v1_checksum_len * 2);
354 this->require_c_string(";\n");
357 return this->package_;
360 // Read the list of import control functions.
363 Import::read_import_init_fns(Gogo* gogo)
365 this->require_c_string("import");
366 while (!this->match_c_string(";"))
368 this->require_c_string(" ");
369 std::string package_name = this->read_identifier();
370 this->require_c_string(" ");
371 std::string init_name = this->read_identifier();
372 this->require_c_string(" ");
373 std::string prio_string = this->read_identifier();
375 if (!this->string_to_int(prio_string, false, &prio))
377 gogo->add_import_init_fn(package_name, init_name, prio);
379 this->require_c_string(";\n");
382 // Import a constant.
385 Import::import_const()
390 Named_constant::import_const(this, &name, &type, &expr);
391 Typed_identifier tid(name, type, this->location_);
392 Named_object* no = this->package_->add_constant(tid, expr);
393 if (this->add_to_globals_)
394 this->gogo_->add_named_object(no);
400 Import::import_type()
403 Named_type::import_named_type(this, &type);
405 // The named type has been added to the package by the type import
406 // process. Here we need to make it visible to the parser, and it
407 // to the global bindings if necessary.
408 type->set_is_visible();
410 if (this->add_to_globals_)
411 this->gogo_->add_named_type(type);
414 // Import a variable.
421 Variable::import_var(this, &name, &type);
422 Variable* var = new Variable(type, NULL, true, false, false,
425 no = this->package_->add_variable(name, var);
426 if (this->add_to_globals_)
427 this->gogo_->add_named_object(no);
430 // Import a function into PACKAGE. PACKAGE is normally
431 // THIS->PACKAGE_, but it will be different for a method associated
432 // with a type defined in a different package.
435 Import::import_func(Package* package)
438 Typed_identifier* receiver;
439 Typed_identifier_list* parameters;
440 Typed_identifier_list* results;
442 Function::import_func(this, &name, &receiver, ¶meters, &results,
444 Function_type *fntype = Type::make_function_type(receiver, parameters,
445 results, this->location_);
447 fntype->set_is_varargs();
449 source_location loc = this->location_;
451 if (fntype->is_method())
453 Type* rtype = receiver->type()->deref();
454 if (rtype->is_error_type())
456 Named_type* named_rtype = rtype->named_type();
457 gcc_assert(named_rtype != NULL);
458 no = named_rtype->add_method_declaration(name, package, fntype, loc);
462 no = package->add_function_declaration(name, fntype, loc);
463 if (this->add_to_globals_)
464 this->gogo_->add_named_object(no);
469 // Read a type in the import stream. This records the type by the
470 // type index. If the type is named, it registers the name, but marks
476 Stream* stream = this->stream_;
477 this->require_c_string("<type ");
483 c = stream->get_char();
484 if (c != '-' && (c < '0' || c > '9'))
490 if (!this->string_to_int(number, true, &index))
491 return Type::make_error_type();
495 // This type was already defined.
497 ? (static_cast<size_t>(- index) >= this->builtin_types_.size()
498 || this->builtin_types_[- index] == NULL)
499 : (static_cast<size_t>(index) >= this->types_.size()
500 || this->types_[index] == NULL))
502 error_at(this->location_,
503 "error in import data at %d: bad type index %d",
504 stream->pos(), index);
505 stream->set_saw_error();
506 return Type::make_error_type();
509 return index < 0 ? this->builtin_types_[- index] : this->types_[index];
514 if (!stream->saw_error())
515 error_at(this->location_,
516 "error in import data at %d: expect %< %> or %<>%>'",
518 stream->set_saw_error();
520 return Type::make_error_type();
524 || (static_cast<size_t>(index) < this->types_.size()
525 && this->types_[index] != NULL))
527 error_at(this->location_,
528 "error in import data at %d: type index already defined",
530 stream->set_saw_error();
531 return Type::make_error_type();
534 if (static_cast<size_t>(index) >= this->types_.size())
536 int newsize = std::max(static_cast<size_t>(index) + 1,
537 this->types_.size() * 2);
538 this->types_.resize(newsize, NULL);
541 if (stream->peek_char() != '"')
543 Type* type = Type::import_type(this);
544 this->require_c_string(">");
545 this->types_[index] = type;
549 // This type has a name.
552 std::string type_name;
553 while ((c = stream->get_char()) != '"')
556 // If this type is in the current package, the name will be
557 // .PREFIX.PACKAGE.NAME or simply NAME with no dots. Otherwise, a
558 // non-hidden symbol will be PREFIX.PACKAGE.NAME and a hidden symbol
559 // will be .PREFIX.PACKAGE.NAME.
560 std::string package_name;
561 std::string unique_prefix;
562 if (type_name.find('.') != std::string::npos)
564 bool is_hidden = false;
566 if (type_name[0] == '.')
571 size_t dot1 = type_name.find('.', start);
573 if (dot1 == std::string::npos)
574 dot2 = std::string::npos;
576 dot2 = type_name.find('.', dot1 + 1);
577 if (dot1 == std::string::npos || dot2 == std::string::npos)
579 error_at(this->location_,
580 ("error at import data at %d: missing dot in type name"),
582 stream->set_saw_error();
586 unique_prefix = type_name.substr(start, dot1 - start);
587 package_name = type_name.substr(dot1 + 1, dot2 - (dot1 + 1));
590 type_name.erase(0, dot2 + 1);
593 this->require_c_string(" ");
595 // Declare the type in the appropriate package. If we haven't seen
596 // it before, mark it as invisible. We declare it before we read
597 // the actual definition of the type, since the definition may refer
598 // to the type itself.
600 if (package_name.empty())
601 package = this->package_;
603 package = this->gogo_->register_package(package_name, unique_prefix,
606 Named_object* no = package->bindings()->lookup(type_name);
608 no = package->add_type_declaration(type_name, this->location_);
609 else if (!no->is_type_declaration() && !no->is_type())
611 error_at(this->location_, "imported %<%s.%s%> both type and non-type",
612 Gogo::message_name(package->name()).c_str(),
613 Gogo::message_name(type_name).c_str());
614 stream->set_saw_error();
615 return Type::make_error_type();
618 gcc_assert(no->package() == package);
620 if (this->types_[index] == NULL)
622 if (no->is_type_declaration())
624 // FIXME: It's silly to make a forward declaration every time.
625 this->types_[index] = Type::make_forward_declaration(no);
629 gcc_assert(no->is_type());
630 this->types_[index] = no->type_value();
634 // If there is no type definition, then this is just a forward
635 // declaration of a type defined in some other file.
637 if (this->match_c_string(">"))
638 type = this->types_[index];
641 type = this->read_type();
643 if (no->is_type_declaration())
645 // We can define the type now.
647 no = package->add_type(type_name, type, this->location_);
648 Named_type* ntype = no->type_value();
650 // This type has not yet been imported.
651 ntype->clear_is_visible();
655 else if (no->is_type())
657 // We have seen this type before. FIXME: it would be a good
658 // idea to check that the two imported types are identical,
659 // but we have not finalized the methds yet, which means
660 // that we can nt reliably compare interface types.
661 type = no->type_value();
663 // Don't change the visibility of the existing type.
666 this->types_[index] = type;
668 // Read the type methods.
669 if (this->match_c_string("\n"))
672 while (this->match_c_string(" func"))
675 this->import_func(package);
680 this->require_c_string(">");
685 // Register the builtin types.
688 Import::register_builtin_types(Gogo* gogo)
690 this->register_builtin_type(gogo, "int8", BUILTIN_INT8);
691 this->register_builtin_type(gogo, "int16", BUILTIN_INT16);
692 this->register_builtin_type(gogo, "int32", BUILTIN_INT32);
693 this->register_builtin_type(gogo, "int64", BUILTIN_INT64);
694 this->register_builtin_type(gogo, "uint8", BUILTIN_UINT8);
695 this->register_builtin_type(gogo, "uint16", BUILTIN_UINT16);
696 this->register_builtin_type(gogo, "uint32", BUILTIN_UINT32);
697 this->register_builtin_type(gogo, "uint64", BUILTIN_UINT64);
698 this->register_builtin_type(gogo, "float32", BUILTIN_FLOAT32);
699 this->register_builtin_type(gogo, "float64", BUILTIN_FLOAT64);
700 this->register_builtin_type(gogo, "complex64", BUILTIN_COMPLEX64);
701 this->register_builtin_type(gogo, "complex128", BUILTIN_COMPLEX128);
702 this->register_builtin_type(gogo, "int", BUILTIN_INT);
703 this->register_builtin_type(gogo, "uint", BUILTIN_UINT);
704 this->register_builtin_type(gogo, "uintptr", BUILTIN_UINTPTR);
705 this->register_builtin_type(gogo, "float", BUILTIN_FLOAT);
706 this->register_builtin_type(gogo, "complex", BUILTIN_COMPLEX);
707 this->register_builtin_type(gogo, "bool", BUILTIN_BOOL);
708 this->register_builtin_type(gogo, "string", BUILTIN_STRING);
711 // Register a single builtin type.
714 Import::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
716 Named_object* named_object = gogo->lookup_global(name);
717 gcc_assert(named_object != NULL && named_object->is_type());
718 int index = - static_cast<int>(code);
720 && static_cast<size_t>(index) < this->builtin_types_.size());
721 this->builtin_types_[index] = named_object->type_value();
724 // Read an identifier from the stream.
727 Import::read_identifier()
730 Stream* stream = this->stream_;
734 c = stream->peek_char();
735 if (c == -1 || c == ' ' || c == ';')
743 // Turn a string into a integer with appropriate error handling.
746 Import::string_to_int(const std::string &s, bool is_neg_ok, int* ret)
749 long prio = strtol(s.c_str(), &end, 10);
750 if (*end != '\0' || prio > 0x7fffffff || (prio < 0 && !is_neg_ok))
752 error_at(this->location_, "invalid integer in import data at %d",
753 this->stream_->pos());
754 this->stream_->set_saw_error();
761 // Class Import::Stream.
763 Import::Stream::Stream()
764 : pos_(0), saw_error_(false)
768 Import::Stream::~Stream()
772 // Return the next character to come from the stream.
775 Import::Stream::peek_char()
778 if (!this->do_peek(1, &read))
780 // Make sure we return an unsigned char, so that we don't get
782 unsigned char ret = *read;
786 // Return true if the next LENGTH characters from the stream match
790 Import::Stream::match_bytes(const char* bytes, size_t length)
793 if (!this->do_peek(length, &read))
795 return memcmp(bytes, read, length) == 0;
798 // Require that the next LENGTH bytes from the stream match BYTES.
801 Import::Stream::require_bytes(source_location location, const char* bytes,
805 if (!this->do_peek(length, &read)
806 || memcmp(bytes, read, length) != 0)
808 if (!this->saw_error_)
809 error_at(location, "import error at %d: expected %<%.*s%>",
810 this->pos(), static_cast<int>(length), bytes);
811 this->saw_error_ = true;
814 this->advance(length);
817 // Class Stream_from_file.
819 Stream_from_file::Stream_from_file(int fd)
822 if (lseek(fd, 0, SEEK_SET) != 0)
824 error("lseek failed: %m");
825 this->set_saw_error();
829 Stream_from_file::~Stream_from_file()
837 Stream_from_file::do_peek(size_t length, const char** bytes)
839 if (this->data_.length() <= length)
841 *bytes = this->data_.data();
844 // Don't bother to handle the general case, since we don't need it.
845 gcc_assert(length < 64);
847 ssize_t got = read(this->fd_, buf, length);
851 if (!this->saw_error())
852 error("read failed: %m");
853 this->set_saw_error();
857 if (lseek(this->fd_, - got, SEEK_CUR) != 0)
859 if (!this->saw_error())
860 error("lseek failed: %m");
861 this->set_saw_error();
865 if (static_cast<size_t>(got) < length)
868 this->data_.assign(buf, got);
870 *bytes = this->data_.data();
877 Stream_from_file::do_advance(size_t skip)
879 if (lseek(this->fd_, skip, SEEK_CUR) != 0)
881 if (!this->saw_error())
882 error("lseek failed: %m");
883 this->set_saw_error();
885 if (!this->data_.empty())
887 if (this->data_.length() < skip)
888 this->data_.erase(0, skip);