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");
284 this->require_c_string("prefix ");
285 std::string unique_prefix
= this->read_identifier();
286 this->require_c_string(";\n");
288 this->package_
= gogo
->add_imported_package(package_name
, local_name
,
289 is_local_name_exported
,
292 &this->add_to_globals_
);
293 if (this->package_
== NULL
)
295 stream
->set_saw_error();
299 this->require_c_string("priority ");
300 std::string priority_string
= this->read_identifier();
302 if (!this->string_to_int(priority_string
, false, &prio
))
304 this->package_
->set_priority(prio
);
305 this->require_c_string(";\n");
307 while (stream
->match_c_string("import"))
308 this->read_one_import();
310 if (stream
->match_c_string("init"))
311 this->read_import_init_fns(gogo
);
313 // Loop over all the input data for this package.
314 while (!stream
->saw_error())
316 if (stream
->match_c_string("const "))
317 this->import_const();
318 else if (stream
->match_c_string("type "))
320 else if (stream
->match_c_string("var "))
322 else if (stream
->match_c_string("func "))
323 this->import_func(this->package_
);
324 else if (stream
->match_c_string("checksum "))
328 error_at(this->location_
,
329 ("error in import data at %d: "
330 "expected %<const%>, %<type%>, %<var%>, "
331 "%<func%>, or %<checksum%>"),
333 stream
->set_saw_error();
338 // We currently ignore the checksum. In the future we could
339 // store the checksum somewhere in the generated object and then
340 // verify that the checksum matches at link time or at dynamic
342 this->require_c_string("checksum ");
343 stream
->advance(Export::v1_checksum_len
* 2);
344 this->require_c_string(";\n");
347 return this->package_
;
350 // Read an import line. We don't actually care about these.
353 Import::read_one_import()
355 this->require_c_string("import ");
356 Stream
* stream
= this->stream_
;
357 while (stream
->peek_char() != ';')
359 this->require_c_string(";\n");
362 // Read the list of import control functions.
365 Import::read_import_init_fns(Gogo
* gogo
)
367 this->require_c_string("init");
368 while (!this->match_c_string(";"))
370 this->require_c_string(" ");
371 std::string package_name
= this->read_identifier();
372 this->require_c_string(" ");
373 std::string init_name
= this->read_identifier();
374 this->require_c_string(" ");
375 std::string prio_string
= this->read_identifier();
377 if (!this->string_to_int(prio_string
, false, &prio
))
379 gogo
->add_import_init_fn(package_name
, init_name
, prio
);
381 this->require_c_string(";\n");
384 // Import a constant.
387 Import::import_const()
392 Named_constant::import_const(this, &name
, &type
, &expr
);
393 Typed_identifier
tid(name
, type
, this->location_
);
394 Named_object
* no
= this->package_
->add_constant(tid
, expr
);
395 if (this->add_to_globals_
)
396 this->gogo_
->add_named_object(no
);
402 Import::import_type()
405 Named_type::import_named_type(this, &type
);
407 // The named type has been added to the package by the type import
408 // process. Here we need to make it visible to the parser, and it
409 // to the global bindings if necessary.
410 type
->set_is_visible();
412 if (this->add_to_globals_
)
413 this->gogo_
->add_named_type(type
);
416 // Import a variable.
423 Variable::import_var(this, &name
, &type
);
424 Variable
* var
= new Variable(type
, NULL
, true, false, false,
427 no
= this->package_
->add_variable(name
, var
);
428 if (this->add_to_globals_
)
429 this->gogo_
->add_named_object(no
);
432 // Import a function into PACKAGE. PACKAGE is normally
433 // THIS->PACKAGE_, but it will be different for a method associated
434 // with a type defined in a different package.
437 Import::import_func(Package
* package
)
440 Typed_identifier
* receiver
;
441 Typed_identifier_list
* parameters
;
442 Typed_identifier_list
* results
;
444 Function::import_func(this, &name
, &receiver
, ¶meters
, &results
,
446 Function_type
*fntype
= Type::make_function_type(receiver
, parameters
,
447 results
, this->location_
);
449 fntype
->set_is_varargs();
451 Location loc
= this->location_
;
453 if (fntype
->is_method())
455 Type
* rtype
= receiver
->type();
457 // We may still be reading the definition of RTYPE, so we have
458 // to be careful to avoid calling base or convert. If RTYPE is
459 // a named type or a forward declaration, then we know that it
460 // is not a pointer, because we are reading a method on RTYPE
461 // and named pointers can't have methods.
463 if (rtype
->classification() == Type::TYPE_POINTER
)
464 rtype
= rtype
->points_to();
466 if (rtype
->is_error_type())
468 else if (rtype
->named_type() != NULL
)
469 no
= rtype
->named_type()->add_method_declaration(name
, package
, fntype
,
471 else if (rtype
->forward_declaration_type() != NULL
)
472 no
= rtype
->forward_declaration_type()->add_method_declaration(name
,
481 no
= package
->add_function_declaration(name
, fntype
, loc
);
482 if (this->add_to_globals_
)
483 this->gogo_
->add_named_object(no
);
488 // Read a type in the import stream. This records the type by the
489 // type index. If the type is named, it registers the name, but marks
495 Stream
* stream
= this->stream_
;
496 this->require_c_string("<type ");
502 c
= stream
->get_char();
503 if (c
!= '-' && (c
< '0' || c
> '9'))
509 if (!this->string_to_int(number
, true, &index
))
510 return Type::make_error_type();
514 // This type was already defined.
516 ? (static_cast<size_t>(- index
) >= this->builtin_types_
.size()
517 || this->builtin_types_
[- index
] == NULL
)
518 : (static_cast<size_t>(index
) >= this->types_
.size()
519 || this->types_
[index
] == NULL
))
521 error_at(this->location_
,
522 "error in import data at %d: bad type index %d",
523 stream
->pos(), index
);
524 stream
->set_saw_error();
525 return Type::make_error_type();
528 return index
< 0 ? this->builtin_types_
[- index
] : this->types_
[index
];
533 if (!stream
->saw_error())
534 error_at(this->location_
,
535 "error in import data at %d: expect %< %> or %<>%>'",
537 stream
->set_saw_error();
539 return Type::make_error_type();
543 || (static_cast<size_t>(index
) < this->types_
.size()
544 && this->types_
[index
] != NULL
))
546 error_at(this->location_
,
547 "error in import data at %d: type index already defined",
549 stream
->set_saw_error();
550 return Type::make_error_type();
553 if (static_cast<size_t>(index
) >= this->types_
.size())
555 int newsize
= std::max(static_cast<size_t>(index
) + 1,
556 this->types_
.size() * 2);
557 this->types_
.resize(newsize
, NULL
);
560 if (stream
->peek_char() != '"')
562 Type
* type
= Type::import_type(this);
563 this->require_c_string(">");
564 this->types_
[index
] = type
;
568 // This type has a name.
571 std::string type_name
;
572 while ((c
= stream
->get_char()) != '"')
575 // If this type is in the current package, the name will be
576 // .PREFIX.PACKAGE.NAME or simply NAME with no dots. Otherwise, a
577 // non-hidden symbol will be PREFIX.PACKAGE.NAME and a hidden symbol
578 // will be .PREFIX.PACKAGE.NAME.
579 std::string package_name
;
580 std::string unique_prefix
;
581 if (type_name
.find('.') != std::string::npos
)
583 bool is_hidden
= false;
585 if (type_name
[0] == '.')
590 size_t dot1
= type_name
.find('.', start
);
592 if (dot1
== std::string::npos
)
593 dot2
= std::string::npos
;
595 dot2
= type_name
.find('.', dot1
+ 1);
596 if (dot1
== std::string::npos
|| dot2
== std::string::npos
)
598 error_at(this->location_
,
599 ("error at import data at %d: missing dot in type name"),
601 stream
->set_saw_error();
605 unique_prefix
= type_name
.substr(start
, dot1
- start
);
606 package_name
= type_name
.substr(dot1
+ 1, dot2
- (dot1
+ 1));
609 type_name
.erase(0, dot2
+ 1);
612 this->require_c_string(" ");
614 // Declare the type in the appropriate package. If we haven't seen
615 // it before, mark it as invisible. We declare it before we read
616 // the actual definition of the type, since the definition may refer
617 // to the type itself.
619 if (package_name
.empty())
620 package
= this->package_
;
622 package
= this->gogo_
->register_package(package_name
, unique_prefix
,
623 Linemap::unknown_location());
625 Named_object
* no
= package
->bindings()->lookup(type_name
);
627 no
= package
->add_type_declaration(type_name
, this->location_
);
628 else if (!no
->is_type_declaration() && !no
->is_type())
630 error_at(this->location_
, "imported %<%s.%s%> both type and non-type",
631 Gogo::message_name(package
->name()).c_str(),
632 Gogo::message_name(type_name
).c_str());
633 stream
->set_saw_error();
634 return Type::make_error_type();
637 go_assert(no
->package() == package
);
639 if (this->types_
[index
] == NULL
)
641 if (no
->is_type_declaration())
643 // FIXME: It's silly to make a forward declaration every time.
644 this->types_
[index
] = Type::make_forward_declaration(no
);
648 go_assert(no
->is_type());
649 this->types_
[index
] = no
->type_value();
653 // If there is no type definition, then this is just a forward
654 // declaration of a type defined in some other file.
656 if (this->match_c_string(">"))
657 type
= this->types_
[index
];
660 type
= this->read_type();
662 if (no
->is_type_declaration())
664 // We can define the type now.
666 no
= package
->add_type(type_name
, type
, this->location_
);
667 Named_type
* ntype
= no
->type_value();
669 // This type has not yet been imported.
670 ntype
->clear_is_visible();
672 if (!type
->is_undefined() && type
->interface_type() != NULL
)
673 this->gogo_
->record_interface_type(type
->interface_type());
677 else if (no
->is_type())
679 // We have seen this type before. FIXME: it would be a good
680 // idea to check that the two imported types are identical,
681 // but we have not finalized the methods yet, which means
682 // that we can not reliably compare interface types.
683 type
= no
->type_value();
685 // Don't change the visibility of the existing type.
688 this->types_
[index
] = type
;
690 // Read the type methods.
691 if (this->match_c_string("\n"))
694 while (this->match_c_string(" func"))
697 this->import_func(package
);
702 this->require_c_string(">");
707 // Register the builtin types.
710 Import::register_builtin_types(Gogo
* gogo
)
712 this->register_builtin_type(gogo
, "int8", BUILTIN_INT8
);
713 this->register_builtin_type(gogo
, "int16", BUILTIN_INT16
);
714 this->register_builtin_type(gogo
, "int32", BUILTIN_INT32
);
715 this->register_builtin_type(gogo
, "int64", BUILTIN_INT64
);
716 this->register_builtin_type(gogo
, "uint8", BUILTIN_UINT8
);
717 this->register_builtin_type(gogo
, "uint16", BUILTIN_UINT16
);
718 this->register_builtin_type(gogo
, "uint32", BUILTIN_UINT32
);
719 this->register_builtin_type(gogo
, "uint64", BUILTIN_UINT64
);
720 this->register_builtin_type(gogo
, "float32", BUILTIN_FLOAT32
);
721 this->register_builtin_type(gogo
, "float64", BUILTIN_FLOAT64
);
722 this->register_builtin_type(gogo
, "complex64", BUILTIN_COMPLEX64
);
723 this->register_builtin_type(gogo
, "complex128", BUILTIN_COMPLEX128
);
724 this->register_builtin_type(gogo
, "int", BUILTIN_INT
);
725 this->register_builtin_type(gogo
, "uint", BUILTIN_UINT
);
726 this->register_builtin_type(gogo
, "uintptr", BUILTIN_UINTPTR
);
727 this->register_builtin_type(gogo
, "bool", BUILTIN_BOOL
);
728 this->register_builtin_type(gogo
, "string", BUILTIN_STRING
);
729 this->register_builtin_type(gogo
, "error", BUILTIN_ERROR
);
730 this->register_builtin_type(gogo
, "byte", BUILTIN_BYTE
);
731 this->register_builtin_type(gogo
, "rune", BUILTIN_RUNE
);
734 // Register a single builtin type.
737 Import::register_builtin_type(Gogo
* gogo
, const char* name
, Builtin_code code
)
739 Named_object
* named_object
= gogo
->lookup_global(name
);
740 go_assert(named_object
!= NULL
&& named_object
->is_type());
741 int index
= - static_cast<int>(code
);
743 && static_cast<size_t>(index
) < this->builtin_types_
.size());
744 this->builtin_types_
[index
] = named_object
->type_value();
747 // Read an identifier from the stream.
750 Import::read_identifier()
753 Stream
* stream
= this->stream_
;
757 c
= stream
->peek_char();
758 if (c
== -1 || c
== ' ' || c
== ';')
766 // Read a name from the stream.
771 std::string ret
= this->read_identifier();
774 else if (!Lex::is_exported_name(ret
))
775 ret
= ('.' + this->package_
->unique_prefix()
776 + '.' + this->package_
->name()
781 // Turn a string into a integer with appropriate error handling.
784 Import::string_to_int(const std::string
&s
, bool is_neg_ok
, int* ret
)
787 long prio
= strtol(s
.c_str(), &end
, 10);
788 if (*end
!= '\0' || prio
> 0x7fffffff || (prio
< 0 && !is_neg_ok
))
790 error_at(this->location_
, "invalid integer in import data at %d",
791 this->stream_
->pos());
792 this->stream_
->set_saw_error();
799 // Class Import::Stream.
801 Import::Stream::Stream()
802 : pos_(0), saw_error_(false)
806 Import::Stream::~Stream()
810 // Return the next character to come from the stream.
813 Import::Stream::peek_char()
816 if (!this->do_peek(1, &read
))
818 // Make sure we return an unsigned char, so that we don't get
820 unsigned char ret
= *read
;
824 // Return true if the next LENGTH characters from the stream match
828 Import::Stream::match_bytes(const char* bytes
, size_t length
)
831 if (!this->do_peek(length
, &read
))
833 return memcmp(bytes
, read
, length
) == 0;
836 // Require that the next LENGTH bytes from the stream match BYTES.
839 Import::Stream::require_bytes(Location location
, const char* bytes
,
843 if (!this->do_peek(length
, &read
)
844 || memcmp(bytes
, read
, length
) != 0)
846 if (!this->saw_error_
)
847 error_at(location
, "import error at %d: expected %<%.*s%>",
848 this->pos(), static_cast<int>(length
), bytes
);
849 this->saw_error_
= true;
852 this->advance(length
);
855 // Class Stream_from_file.
857 Stream_from_file::Stream_from_file(int fd
)
860 if (lseek(fd
, 0, SEEK_SET
) != 0)
862 error("lseek failed: %m");
863 this->set_saw_error();
867 Stream_from_file::~Stream_from_file()
875 Stream_from_file::do_peek(size_t length
, const char** bytes
)
877 if (this->data_
.length() <= length
)
879 *bytes
= this->data_
.data();
882 // Don't bother to handle the general case, since we don't need it.
883 go_assert(length
< 64);
885 ssize_t got
= read(this->fd_
, buf
, length
);
889 if (!this->saw_error())
890 error("read failed: %m");
891 this->set_saw_error();
895 if (lseek(this->fd_
, - got
, SEEK_CUR
) != 0)
897 if (!this->saw_error())
898 error("lseek failed: %m");
899 this->set_saw_error();
903 if (static_cast<size_t>(got
) < length
)
906 this->data_
.assign(buf
, got
);
908 *bytes
= this->data_
.data();
915 Stream_from_file::do_advance(size_t skip
)
917 if (lseek(this->fd_
, skip
, SEEK_CUR
) != 0)
919 if (!this->saw_error())
920 error("lseek failed: %m");
921 this->set_saw_error();
923 if (!this->data_
.empty())
925 if (this->data_
.length() < skip
)
926 this->data_
.erase(0, skip
);