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 start with ./ or ../, we use
45 // RELATIVE_IMPORT_PATH as a prefix.
47 // When FILENAME does not exist, we try modifying FILENAME to find the
48 // file. We use the first of these which exists:
49 // * We append ".gox".
50 // * We turn the base of FILENAME into libFILENAME.so.
51 // * We turn the base of FILENAME into libFILENAME.a.
54 // When using a search path, we apply each of these transformations at
55 // each entry on the search path before moving on to the next entry.
56 // If the file exists, but does not contain any Go export data, we
57 // stop; we do not keep looking for another file with the same name
58 // later in the search path.
61 Import::open_package(const std::string
& filename
, Location location
,
62 const std::string
& relative_import_path
)
65 if (IS_ABSOLUTE_PATH(filename
))
67 else if (filename
[0] == '.'
68 && (filename
[1] == '\0' || IS_DIR_SEPARATOR(filename
[1])))
70 else if (filename
[0] == '.'
72 && (filename
[2] == '\0' || IS_DIR_SEPARATOR(filename
[2])))
77 std::string fn
= filename
;
78 if (is_local
&& !IS_ABSOLUTE_PATH(filename
) && !relative_import_path
.empty())
83 fn
= relative_import_path
;
86 fn
= relative_import_path
+ '/' + fn
;
92 for (std::vector
<std::string
>::const_iterator p
= search_path
.begin();
93 p
!= search_path
.end();
96 std::string indir
= *p
;
97 if (!indir
.empty() && indir
[indir
.size() - 1] != '/')
100 Stream
* s
= Import::try_package_in_directory(indir
, location
);
106 Stream
* s
= Import::try_package_in_directory(fn
, location
);
113 // Try to find the export data for FILENAME.
116 Import::try_package_in_directory(const std::string
& filename
,
119 std::string found_filename
= filename
;
120 int fd
= open(found_filename
.c_str(), O_RDONLY
| O_BINARY
);
125 if (fstat(fd
, &s
) >= 0 && S_ISDIR(s
.st_mode
))
135 if (errno
!= ENOENT
&& errno
!= EISDIR
)
136 warning_at(location
, 0, "%s: %m", filename
.c_str());
138 fd
= Import::try_suffixes(&found_filename
);
143 // The export data may not be in this file.
144 Stream
* s
= Import::find_export_data(found_filename
, fd
, location
);
150 error_at(location
, "%s exists but does not contain any Go export data",
151 found_filename
.c_str());
156 // Given import "*PFILENAME", where *PFILENAME does not exist, try
157 // various suffixes. If we find one, set *PFILENAME to the one we
158 // found. Return the open file descriptor.
161 Import::try_suffixes(std::string
* pfilename
)
163 std::string filename
= *pfilename
+ ".gox";
164 int fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
167 *pfilename
= filename
;
171 const char* basename
= lbasename(pfilename
->c_str());
172 size_t basename_pos
= basename
- pfilename
->c_str();
173 filename
= pfilename
->substr(0, basename_pos
) + "lib" + basename
+ ".so";
174 fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
177 *pfilename
= filename
;
181 filename
= pfilename
->substr(0, basename_pos
) + "lib" + basename
+ ".a";
182 fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
185 *pfilename
= filename
;
189 filename
= *pfilename
+ ".o";
190 fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
193 *pfilename
= filename
;
200 // Look for export data in the file descriptor FD.
203 Import::find_export_data(const std::string
& filename
, int fd
,
206 // See if we can read this as an object file.
207 Import::Stream
* stream
= Import::find_object_export_data(filename
, fd
, 0,
212 const int len
= MAX(Export::v1_magic_len
, Import::archive_magic_len
);
214 if (lseek(fd
, 0, SEEK_SET
) < 0)
216 error_at(location
, "lseek %s failed: %m", filename
.c_str());
221 ssize_t c
= read(fd
, buf
, len
);
225 // Check for a file containing nothing but Go export data.
226 if (memcmp(buf
, Export::v1_magic
, Export::v1_magic_len
) == 0)
227 return new Stream_from_file(fd
);
229 // See if we can read this as an archive.
230 if (Import::is_archive_magic(buf
))
231 return Import::find_archive_export_data(filename
, fd
, location
);
236 // Look for export data in a simple_object.
239 Import::find_object_export_data(const std::string
& filename
,
247 const char *errmsg
= go_read_export_data(fd
, offset
, &buf
, &len
, &err
);
251 error_at(location
, "%s: %s", filename
.c_str(), errmsg
);
253 error_at(location
, "%s: %s: %s", filename
.c_str(), errmsg
,
261 return new Stream_from_buffer(buf
, len
);
266 // Construct an Import object. We make the builtin_types_ vector
267 // large enough to hold all the builtin types.
269 Import::Import(Stream
* stream
, Location location
)
270 : gogo_(NULL
), stream_(stream
), location_(location
), package_(NULL
),
271 add_to_globals_(false),
272 builtin_types_((- SMALLEST_BUILTIN_CODE
) + 1),
277 // Import the data in the associated stream.
280 Import::import(Gogo
* gogo
, const std::string
& local_name
,
281 bool is_local_name_exported
)
283 // Hold on to the Gogo structure. Otherwise we need to pass it
284 // through all the import functions, because we need it when reading
288 // A stream of export data can include data from more than one input
289 // file. Here we loop over each input file.
290 Stream
* stream
= this->stream_
;
291 while (!stream
->at_eof() && !stream
->saw_error())
293 // The vector of types is package specific.
294 this->types_
.clear();
296 stream
->require_bytes(this->location_
, Export::v1_magic
,
297 Export::v1_magic_len
);
299 this->require_c_string("package ");
300 std::string package_name
= this->read_identifier();
301 this->require_c_string(";\n");
304 if (this->match_c_string("prefix "))
307 std::string unique_prefix
= this->read_identifier();
308 this->require_c_string(";\n");
309 pkgpath
= unique_prefix
+ '.' + package_name
;
313 this->require_c_string("pkgpath ");
314 pkgpath
= this->read_identifier();
315 this->require_c_string(";\n");
318 this->package_
= gogo
->add_imported_package(package_name
, local_name
,
319 is_local_name_exported
,
322 &this->add_to_globals_
);
323 if (this->package_
== NULL
)
325 stream
->set_saw_error();
329 this->require_c_string("priority ");
330 std::string priority_string
= this->read_identifier();
332 if (!this->string_to_int(priority_string
, false, &prio
))
334 this->package_
->set_priority(prio
);
335 this->require_c_string(";\n");
337 while (stream
->match_c_string("import"))
338 this->read_one_import();
340 if (stream
->match_c_string("init"))
341 this->read_import_init_fns(gogo
);
343 // Loop over all the input data for this package.
344 while (!stream
->saw_error())
346 if (stream
->match_c_string("const "))
347 this->import_const();
348 else if (stream
->match_c_string("type "))
350 else if (stream
->match_c_string("var "))
352 else if (stream
->match_c_string("func "))
353 this->import_func(this->package_
);
354 else if (stream
->match_c_string("checksum "))
358 error_at(this->location_
,
359 ("error in import data at %d: "
360 "expected %<const%>, %<type%>, %<var%>, "
361 "%<func%>, or %<checksum%>"),
363 stream
->set_saw_error();
368 // We currently ignore the checksum. In the future we could
369 // store the checksum somewhere in the generated object and then
370 // verify that the checksum matches at link time or at dynamic
372 this->require_c_string("checksum ");
373 stream
->advance(Export::v1_checksum_len
* 2);
374 this->require_c_string(";\n");
377 return this->package_
;
380 // Read an import line. We don't actually care about these.
383 Import::read_one_import()
385 this->require_c_string("import ");
386 std::string package_name
= this->read_identifier();
387 this->require_c_string(" ");
388 std::string pkgpath
= this->read_identifier();
389 this->require_c_string(" \"");
390 Stream
* stream
= this->stream_
;
391 while (stream
->peek_char() != '"')
393 this->require_c_string("\";\n");
395 Package
* p
= this->gogo_
->register_package(pkgpath
,
396 Linemap::unknown_location());
397 p
->set_package_name(package_name
, this->location());
400 // Read the list of import control functions.
403 Import::read_import_init_fns(Gogo
* gogo
)
405 this->require_c_string("init");
406 while (!this->match_c_string(";"))
408 this->require_c_string(" ");
409 std::string package_name
= this->read_identifier();
410 this->require_c_string(" ");
411 std::string init_name
= this->read_identifier();
412 this->require_c_string(" ");
413 std::string prio_string
= this->read_identifier();
415 if (!this->string_to_int(prio_string
, false, &prio
))
417 gogo
->add_import_init_fn(package_name
, init_name
, prio
);
419 this->require_c_string(";\n");
422 // Import a constant.
425 Import::import_const()
430 Named_constant::import_const(this, &name
, &type
, &expr
);
431 Typed_identifier
tid(name
, type
, this->location_
);
432 Named_object
* no
= this->package_
->add_constant(tid
, expr
);
433 if (this->add_to_globals_
)
434 this->gogo_
->add_named_object(no
);
440 Import::import_type()
443 Named_type::import_named_type(this, &type
);
445 // The named type has been added to the package by the type import
446 // process. Here we need to make it visible to the parser, and it
447 // to the global bindings if necessary.
448 type
->set_is_visible();
450 if (this->add_to_globals_
)
451 this->gogo_
->add_named_type(type
);
454 // Import a variable.
461 Variable::import_var(this, &name
, &type
);
462 Variable
* var
= new Variable(type
, NULL
, true, false, false,
465 no
= this->package_
->add_variable(name
, var
);
466 if (this->add_to_globals_
)
467 this->gogo_
->add_named_object(no
);
470 // Import a function into PACKAGE. PACKAGE is normally
471 // THIS->PACKAGE_, but it will be different for a method associated
472 // with a type defined in a different package.
475 Import::import_func(Package
* package
)
478 Typed_identifier
* receiver
;
479 Typed_identifier_list
* parameters
;
480 Typed_identifier_list
* results
;
482 Function::import_func(this, &name
, &receiver
, ¶meters
, &results
,
484 Function_type
*fntype
= Type::make_function_type(receiver
, parameters
,
485 results
, this->location_
);
487 fntype
->set_is_varargs();
489 Location loc
= this->location_
;
491 if (fntype
->is_method())
493 Type
* rtype
= receiver
->type();
495 // We may still be reading the definition of RTYPE, so we have
496 // to be careful to avoid calling base or convert. If RTYPE is
497 // a named type or a forward declaration, then we know that it
498 // is not a pointer, because we are reading a method on RTYPE
499 // and named pointers can't have methods.
501 if (rtype
->classification() == Type::TYPE_POINTER
)
502 rtype
= rtype
->points_to();
504 if (rtype
->is_error_type())
506 else if (rtype
->named_type() != NULL
)
507 no
= rtype
->named_type()->add_method_declaration(name
, package
, fntype
,
509 else if (rtype
->forward_declaration_type() != NULL
)
510 no
= rtype
->forward_declaration_type()->add_method_declaration(name
,
519 no
= package
->add_function_declaration(name
, fntype
, loc
);
520 if (this->add_to_globals_
)
521 this->gogo_
->add_named_object(no
);
526 // Read a type in the import stream. This records the type by the
527 // type index. If the type is named, it registers the name, but marks
533 Stream
* stream
= this->stream_
;
534 this->require_c_string("<type ");
540 c
= stream
->get_char();
541 if (c
!= '-' && (c
< '0' || c
> '9'))
547 if (!this->string_to_int(number
, true, &index
))
548 return Type::make_error_type();
552 // This type was already defined.
554 ? (static_cast<size_t>(- index
) >= this->builtin_types_
.size()
555 || this->builtin_types_
[- index
] == NULL
)
556 : (static_cast<size_t>(index
) >= this->types_
.size()
557 || this->types_
[index
] == NULL
))
559 error_at(this->location_
,
560 "error in import data at %d: bad type index %d",
561 stream
->pos(), index
);
562 stream
->set_saw_error();
563 return Type::make_error_type();
566 return index
< 0 ? this->builtin_types_
[- index
] : this->types_
[index
];
571 if (!stream
->saw_error())
572 error_at(this->location_
,
573 "error in import data at %d: expect %< %> or %<>%>'",
575 stream
->set_saw_error();
577 return Type::make_error_type();
581 || (static_cast<size_t>(index
) < this->types_
.size()
582 && this->types_
[index
] != NULL
))
584 error_at(this->location_
,
585 "error in import data at %d: type index already defined",
587 stream
->set_saw_error();
588 return Type::make_error_type();
591 if (static_cast<size_t>(index
) >= this->types_
.size())
593 int newsize
= std::max(static_cast<size_t>(index
) + 1,
594 this->types_
.size() * 2);
595 this->types_
.resize(newsize
, NULL
);
598 if (stream
->peek_char() != '"')
600 Type
* type
= Type::import_type(this);
601 this->require_c_string(">");
602 this->types_
[index
] = type
;
606 // This type has a name.
609 std::string type_name
;
610 while ((c
= stream
->get_char()) != '"')
613 // If this type is in the package we are currently importing, the
614 // name will be .PKGPATH.NAME or simply NAME with no dots.
615 // Otherwise, a non-hidden symbol will be PKGPATH.NAME and a hidden
616 // symbol will be .PKGPATH.NAME.
618 if (type_name
.find('.') != std::string::npos
)
621 if (type_name
[0] == '.')
623 size_t dot
= type_name
.rfind('.');
624 pkgpath
= type_name
.substr(start
, dot
- start
);
625 if (type_name
[0] != '.')
626 type_name
.erase(0, dot
+ 1);
629 this->require_c_string(" ");
631 // The package name may follow. This is the name of the package in
632 // the package clause of that package. The type name will include
633 // the pkgpath, which may be different.
634 std::string package_name
;
635 if (stream
->peek_char() == '"')
638 while ((c
= stream
->get_char()) != '"')
640 this->require_c_string(" ");
643 // Declare the type in the appropriate package. If we haven't seen
644 // it before, mark it as invisible. We declare it before we read
645 // the actual definition of the type, since the definition may refer
646 // to the type itself.
648 if (pkgpath
.empty() || pkgpath
== this->gogo_
->pkgpath())
649 package
= this->package_
;
652 package
= this->gogo_
->register_package(pkgpath
,
653 Linemap::unknown_location());
654 if (!package_name
.empty())
655 package
->set_package_name(package_name
, this->location());
658 Named_object
* no
= package
->bindings()->lookup(type_name
);
660 no
= package
->add_type_declaration(type_name
, this->location_
);
661 else if (!no
->is_type_declaration() && !no
->is_type())
663 error_at(this->location_
, "imported %<%s.%s%> both type and non-type",
664 pkgpath
.c_str(), Gogo::message_name(type_name
).c_str());
665 stream
->set_saw_error();
666 return Type::make_error_type();
669 go_assert(no
->package() == package
);
671 if (this->types_
[index
] == NULL
)
673 if (no
->is_type_declaration())
675 // FIXME: It's silly to make a forward declaration every time.
676 this->types_
[index
] = Type::make_forward_declaration(no
);
680 go_assert(no
->is_type());
681 this->types_
[index
] = no
->type_value();
685 // If there is no type definition, then this is just a forward
686 // declaration of a type defined in some other file.
688 if (this->match_c_string(">"))
689 type
= this->types_
[index
];
692 type
= this->read_type();
694 if (no
->is_type_declaration())
696 // We can define the type now.
698 no
= package
->add_type(type_name
, type
, this->location_
);
699 Named_type
* ntype
= no
->type_value();
701 // This type has not yet been imported.
702 ntype
->clear_is_visible();
704 if (!type
->is_undefined() && type
->interface_type() != NULL
)
705 this->gogo_
->record_interface_type(type
->interface_type());
709 else if (no
->is_type())
711 // We have seen this type before. FIXME: it would be a good
712 // idea to check that the two imported types are identical,
713 // but we have not finalized the methods yet, which means
714 // that we can not reliably compare interface types.
715 type
= no
->type_value();
717 // Don't change the visibility of the existing type.
720 this->types_
[index
] = type
;
722 // Read the type methods.
723 if (this->match_c_string("\n"))
726 while (this->match_c_string(" func"))
729 this->import_func(package
);
734 this->require_c_string(">");
739 // Register the builtin types.
742 Import::register_builtin_types(Gogo
* gogo
)
744 this->register_builtin_type(gogo
, "int8", BUILTIN_INT8
);
745 this->register_builtin_type(gogo
, "int16", BUILTIN_INT16
);
746 this->register_builtin_type(gogo
, "int32", BUILTIN_INT32
);
747 this->register_builtin_type(gogo
, "int64", BUILTIN_INT64
);
748 this->register_builtin_type(gogo
, "uint8", BUILTIN_UINT8
);
749 this->register_builtin_type(gogo
, "uint16", BUILTIN_UINT16
);
750 this->register_builtin_type(gogo
, "uint32", BUILTIN_UINT32
);
751 this->register_builtin_type(gogo
, "uint64", BUILTIN_UINT64
);
752 this->register_builtin_type(gogo
, "float32", BUILTIN_FLOAT32
);
753 this->register_builtin_type(gogo
, "float64", BUILTIN_FLOAT64
);
754 this->register_builtin_type(gogo
, "complex64", BUILTIN_COMPLEX64
);
755 this->register_builtin_type(gogo
, "complex128", BUILTIN_COMPLEX128
);
756 this->register_builtin_type(gogo
, "int", BUILTIN_INT
);
757 this->register_builtin_type(gogo
, "uint", BUILTIN_UINT
);
758 this->register_builtin_type(gogo
, "uintptr", BUILTIN_UINTPTR
);
759 this->register_builtin_type(gogo
, "bool", BUILTIN_BOOL
);
760 this->register_builtin_type(gogo
, "string", BUILTIN_STRING
);
761 this->register_builtin_type(gogo
, "error", BUILTIN_ERROR
);
762 this->register_builtin_type(gogo
, "byte", BUILTIN_BYTE
);
763 this->register_builtin_type(gogo
, "rune", BUILTIN_RUNE
);
766 // Register a single builtin type.
769 Import::register_builtin_type(Gogo
* gogo
, const char* name
, Builtin_code code
)
771 Named_object
* named_object
= gogo
->lookup_global(name
);
772 go_assert(named_object
!= NULL
&& named_object
->is_type());
773 int index
= - static_cast<int>(code
);
775 && static_cast<size_t>(index
) < this->builtin_types_
.size());
776 this->builtin_types_
[index
] = named_object
->type_value();
779 // Read an identifier from the stream.
782 Import::read_identifier()
785 Stream
* stream
= this->stream_
;
789 c
= stream
->peek_char();
790 if (c
== -1 || c
== ' ' || c
== ';')
798 // Read a name from the stream.
803 std::string ret
= this->read_identifier();
806 else if (!Lex::is_exported_name(ret
))
807 ret
= '.' + this->package_
->pkgpath() + '.' + ret
;
811 // Turn a string into a integer with appropriate error handling.
814 Import::string_to_int(const std::string
&s
, bool is_neg_ok
, int* ret
)
817 long prio
= strtol(s
.c_str(), &end
, 10);
818 if (*end
!= '\0' || prio
> 0x7fffffff || (prio
< 0 && !is_neg_ok
))
820 error_at(this->location_
, "invalid integer in import data at %d",
821 this->stream_
->pos());
822 this->stream_
->set_saw_error();
829 // Class Import::Stream.
831 Import::Stream::Stream()
832 : pos_(0), saw_error_(false)
836 Import::Stream::~Stream()
840 // Return the next character to come from the stream.
843 Import::Stream::peek_char()
846 if (!this->do_peek(1, &read
))
848 // Make sure we return an unsigned char, so that we don't get
850 unsigned char ret
= *read
;
854 // Return true if the next LENGTH characters from the stream match
858 Import::Stream::match_bytes(const char* bytes
, size_t length
)
861 if (!this->do_peek(length
, &read
))
863 return memcmp(bytes
, read
, length
) == 0;
866 // Require that the next LENGTH bytes from the stream match BYTES.
869 Import::Stream::require_bytes(Location location
, const char* bytes
,
873 if (!this->do_peek(length
, &read
)
874 || memcmp(bytes
, read
, length
) != 0)
876 if (!this->saw_error_
)
877 error_at(location
, "import error at %d: expected %<%.*s%>",
878 this->pos(), static_cast<int>(length
), bytes
);
879 this->saw_error_
= true;
882 this->advance(length
);
885 // Class Stream_from_file.
887 Stream_from_file::Stream_from_file(int fd
)
890 if (lseek(fd
, 0, SEEK_SET
) != 0)
892 error("lseek failed: %m");
893 this->set_saw_error();
897 Stream_from_file::~Stream_from_file()
905 Stream_from_file::do_peek(size_t length
, const char** bytes
)
907 if (this->data_
.length() <= length
)
909 *bytes
= this->data_
.data();
912 // Don't bother to handle the general case, since we don't need it.
913 go_assert(length
< 64);
915 ssize_t got
= read(this->fd_
, buf
, length
);
919 if (!this->saw_error())
920 error("read failed: %m");
921 this->set_saw_error();
925 if (lseek(this->fd_
, - got
, SEEK_CUR
) != 0)
927 if (!this->saw_error())
928 error("lseek failed: %m");
929 this->set_saw_error();
933 if (static_cast<size_t>(got
) < length
)
936 this->data_
.assign(buf
, got
);
938 *bytes
= this->data_
.data();
945 Stream_from_file::do_advance(size_t skip
)
947 if (lseek(this->fd_
, skip
, SEEK_CUR
) != 0)
949 if (!this->saw_error())
950 error("lseek failed: %m");
951 this->set_saw_error();
953 if (!this->data_
.empty())
955 if (this->data_
.length() < skip
)
956 this->data_
.erase(0, skip
);