4aca1a3f9cb44dc07f988ee17e1b198ad4bcaa05
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());
252 error_at(location
, "%s: short read", filename
.c_str());
257 return new Stream_from_buffer(buf
, sec_length
);
262 // Construct an Import object. We make the builtin_types_ vector
263 // large enough to hold all the builtin types.
265 Import::Import(Stream
* stream
, source_location location
)
266 : gogo_(NULL
), stream_(stream
), location_(location
), package_(NULL
),
267 add_to_globals_(false),
268 builtin_types_((- SMALLEST_BUILTIN_CODE
) + 1),
273 // Import the data in the associated stream.
276 Import::import(Gogo
* gogo
, const std::string
& local_name
,
277 bool is_local_name_exported
)
279 // Hold on to the Gogo structure. Otherwise we need to pass it
280 // through all the import functions, because we need it when reading
284 // A stream of export data can include data from more than one input
285 // file. Here we loop over each input file.
286 Stream
* stream
= this->stream_
;
287 while (!stream
->at_eof() && !stream
->saw_error())
289 // The vector of types is package specific.
290 this->types_
.clear();
292 stream
->require_bytes(this->location_
, Export::v1_magic
,
293 Export::v1_magic_len
);
295 this->require_c_string("package ");
296 std::string package_name
= this->read_identifier();
297 this->require_c_string(";\n");
299 this->require_c_string("prefix ");
300 std::string unique_prefix
= this->read_identifier();
301 this->require_c_string(";\n");
303 this->package_
= gogo
->add_imported_package(package_name
, local_name
,
304 is_local_name_exported
,
307 &this->add_to_globals_
);
308 if (this->package_
== NULL
)
310 stream
->set_saw_error();
314 this->require_c_string("priority ");
315 std::string priority_string
= this->read_identifier();
317 if (!this->string_to_int(priority_string
, false, &prio
))
319 this->package_
->set_priority(prio
);
320 this->require_c_string(";\n");
322 if (stream
->match_c_string("import "))
323 this->read_import_init_fns(gogo
);
325 // Loop over all the input data for this package.
326 while (!stream
->saw_error())
328 if (stream
->match_c_string("const "))
329 this->import_const();
330 else if (stream
->match_c_string("type "))
332 else if (stream
->match_c_string("var "))
334 else if (stream
->match_c_string("func "))
335 this->import_func(this->package_
);
336 else if (stream
->match_c_string("checksum "))
340 error_at(this->location_
,
341 ("error in import data at %d: "
342 "expected %<const%>, %<type%>, %<var%>, "
343 "%<func%>, or %<checksum%>"),
345 stream
->set_saw_error();
350 // We currently ignore the checksum. In the future we could
351 // store the checksum somewhere in the generated object and then
352 // verify that the checksum matches at link time or at dynamic
354 this->require_c_string("checksum ");
355 stream
->advance(Export::v1_checksum_len
* 2);
356 this->require_c_string(";\n");
359 return this->package_
;
362 // Read the list of import control functions.
365 Import::read_import_init_fns(Gogo
* gogo
)
367 this->require_c_string("import");
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 source_location loc
= this->location_
;
453 if (fntype
->is_method())
455 Type
* rtype
= receiver
->type()->deref();
456 if (rtype
->is_error_type())
458 Named_type
* named_rtype
= rtype
->named_type();
459 go_assert(named_rtype
!= NULL
);
460 no
= named_rtype
->add_method_declaration(name
, package
, fntype
, loc
);
464 no
= package
->add_function_declaration(name
, fntype
, loc
);
465 if (this->add_to_globals_
)
466 this->gogo_
->add_named_object(no
);
471 // Read a type in the import stream. This records the type by the
472 // type index. If the type is named, it registers the name, but marks
478 Stream
* stream
= this->stream_
;
479 this->require_c_string("<type ");
485 c
= stream
->get_char();
486 if (c
!= '-' && (c
< '0' || c
> '9'))
492 if (!this->string_to_int(number
, true, &index
))
493 return Type::make_error_type();
497 // This type was already defined.
499 ? (static_cast<size_t>(- index
) >= this->builtin_types_
.size()
500 || this->builtin_types_
[- index
] == NULL
)
501 : (static_cast<size_t>(index
) >= this->types_
.size()
502 || this->types_
[index
] == NULL
))
504 error_at(this->location_
,
505 "error in import data at %d: bad type index %d",
506 stream
->pos(), index
);
507 stream
->set_saw_error();
508 return Type::make_error_type();
511 return index
< 0 ? this->builtin_types_
[- index
] : this->types_
[index
];
516 if (!stream
->saw_error())
517 error_at(this->location_
,
518 "error in import data at %d: expect %< %> or %<>%>'",
520 stream
->set_saw_error();
522 return Type::make_error_type();
526 || (static_cast<size_t>(index
) < this->types_
.size()
527 && this->types_
[index
] != NULL
))
529 error_at(this->location_
,
530 "error in import data at %d: type index already defined",
532 stream
->set_saw_error();
533 return Type::make_error_type();
536 if (static_cast<size_t>(index
) >= this->types_
.size())
538 int newsize
= std::max(static_cast<size_t>(index
) + 1,
539 this->types_
.size() * 2);
540 this->types_
.resize(newsize
, NULL
);
543 if (stream
->peek_char() != '"')
545 Type
* type
= Type::import_type(this);
546 this->require_c_string(">");
547 this->types_
[index
] = type
;
551 // This type has a name.
554 std::string type_name
;
555 while ((c
= stream
->get_char()) != '"')
558 // If this type is in the current package, the name will be
559 // .PREFIX.PACKAGE.NAME or simply NAME with no dots. Otherwise, a
560 // non-hidden symbol will be PREFIX.PACKAGE.NAME and a hidden symbol
561 // will be .PREFIX.PACKAGE.NAME.
562 std::string package_name
;
563 std::string unique_prefix
;
564 if (type_name
.find('.') != std::string::npos
)
566 bool is_hidden
= false;
568 if (type_name
[0] == '.')
573 size_t dot1
= type_name
.find('.', start
);
575 if (dot1
== std::string::npos
)
576 dot2
= std::string::npos
;
578 dot2
= type_name
.find('.', dot1
+ 1);
579 if (dot1
== std::string::npos
|| dot2
== std::string::npos
)
581 error_at(this->location_
,
582 ("error at import data at %d: missing dot in type name"),
584 stream
->set_saw_error();
588 unique_prefix
= type_name
.substr(start
, dot1
- start
);
589 package_name
= type_name
.substr(dot1
+ 1, dot2
- (dot1
+ 1));
592 type_name
.erase(0, dot2
+ 1);
595 this->require_c_string(" ");
597 // Declare the type in the appropriate package. If we haven't seen
598 // it before, mark it as invisible. We declare it before we read
599 // the actual definition of the type, since the definition may refer
600 // to the type itself.
602 if (package_name
.empty())
603 package
= this->package_
;
605 package
= this->gogo_
->register_package(package_name
, unique_prefix
,
608 Named_object
* no
= package
->bindings()->lookup(type_name
);
610 no
= package
->add_type_declaration(type_name
, this->location_
);
611 else if (!no
->is_type_declaration() && !no
->is_type())
613 error_at(this->location_
, "imported %<%s.%s%> both type and non-type",
614 Gogo::message_name(package
->name()).c_str(),
615 Gogo::message_name(type_name
).c_str());
616 stream
->set_saw_error();
617 return Type::make_error_type();
620 go_assert(no
->package() == package
);
622 if (this->types_
[index
] == NULL
)
624 if (no
->is_type_declaration())
626 // FIXME: It's silly to make a forward declaration every time.
627 this->types_
[index
] = Type::make_forward_declaration(no
);
631 go_assert(no
->is_type());
632 this->types_
[index
] = no
->type_value();
636 // If there is no type definition, then this is just a forward
637 // declaration of a type defined in some other file.
639 if (this->match_c_string(">"))
640 type
= this->types_
[index
];
643 type
= this->read_type();
645 if (no
->is_type_declaration())
647 // We can define the type now.
649 no
= package
->add_type(type_name
, type
, this->location_
);
650 Named_type
* ntype
= no
->type_value();
652 // This type has not yet been imported.
653 ntype
->clear_is_visible();
657 else if (no
->is_type())
659 // We have seen this type before. FIXME: it would be a good
660 // idea to check that the two imported types are identical,
661 // but we have not finalized the methds yet, which means
662 // that we can nt reliably compare interface types.
663 type
= no
->type_value();
665 // Don't change the visibility of the existing type.
668 this->types_
[index
] = type
;
670 // Read the type methods.
671 if (this->match_c_string("\n"))
674 while (this->match_c_string(" func"))
677 this->import_func(package
);
682 this->require_c_string(">");
687 // Register the builtin types.
690 Import::register_builtin_types(Gogo
* gogo
)
692 this->register_builtin_type(gogo
, "int8", BUILTIN_INT8
);
693 this->register_builtin_type(gogo
, "int16", BUILTIN_INT16
);
694 this->register_builtin_type(gogo
, "int32", BUILTIN_INT32
);
695 this->register_builtin_type(gogo
, "int64", BUILTIN_INT64
);
696 this->register_builtin_type(gogo
, "uint8", BUILTIN_UINT8
);
697 this->register_builtin_type(gogo
, "uint16", BUILTIN_UINT16
);
698 this->register_builtin_type(gogo
, "uint32", BUILTIN_UINT32
);
699 this->register_builtin_type(gogo
, "uint64", BUILTIN_UINT64
);
700 this->register_builtin_type(gogo
, "float32", BUILTIN_FLOAT32
);
701 this->register_builtin_type(gogo
, "float64", BUILTIN_FLOAT64
);
702 this->register_builtin_type(gogo
, "complex64", BUILTIN_COMPLEX64
);
703 this->register_builtin_type(gogo
, "complex128", BUILTIN_COMPLEX128
);
704 this->register_builtin_type(gogo
, "int", BUILTIN_INT
);
705 this->register_builtin_type(gogo
, "uint", BUILTIN_UINT
);
706 this->register_builtin_type(gogo
, "uintptr", BUILTIN_UINTPTR
);
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 go_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 go_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
);