]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/go/gofrontend/import.cc
c1982eb4300951dfbd9a3dcb10f8e5d5c2f76d5e
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.
12 #include "go-diagnostics.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
;
85 else if (fn
[0] == '.' && fn
[1] == '.'
86 && (fn
[2] == '\0' || IS_DIR_SEPARATOR(fn
[2])))
88 // We are going to join relative_import_path and fn, and it
89 // will look like DIR/../PATH. But DIR does not necessarily
90 // exist in this case, and if it doesn't the use of .. will
91 // fail although it shouldn't. The gc compiler uses
92 // path.Join here, which cleans up the .., so we need to do
95 for (index
= relative_import_path
.length() - 1;
96 index
> 0 && !IS_DIR_SEPARATOR(relative_import_path
[index
]);
100 fn
= relative_import_path
.substr(0, index
) + fn
.substr(2);
102 fn
= relative_import_path
+ '/' + fn
;
105 fn
= relative_import_path
+ '/' + fn
;
111 for (std::vector
<std::string
>::const_iterator p
= search_path
.begin();
112 p
!= search_path
.end();
115 std::string indir
= *p
;
116 if (!indir
.empty() && indir
[indir
.size() - 1] != '/')
119 Stream
* s
= Import::try_package_in_directory(indir
, location
);
125 Stream
* s
= Import::try_package_in_directory(fn
, location
);
132 // Try to find the export data for FILENAME.
135 Import::try_package_in_directory(const std::string
& filename
,
138 std::string found_filename
= filename
;
139 int fd
= open(found_filename
.c_str(), O_RDONLY
| O_BINARY
);
144 if (fstat(fd
, &s
) >= 0 && S_ISDIR(s
.st_mode
))
154 if (errno
!= ENOENT
&& errno
!= EISDIR
)
155 go_warning_at(location
, 0, "%s: %m", filename
.c_str());
157 fd
= Import::try_suffixes(&found_filename
);
162 // The export data may not be in this file.
163 Stream
* s
= Import::find_export_data(found_filename
, fd
, location
);
169 go_error_at(location
, "%s exists but does not contain any Go export data",
170 found_filename
.c_str());
175 // Given import "*PFILENAME", where *PFILENAME does not exist, try
176 // various suffixes. If we find one, set *PFILENAME to the one we
177 // found. Return the open file descriptor.
180 Import::try_suffixes(std::string
* pfilename
)
182 std::string filename
= *pfilename
+ ".gox";
183 int fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
186 *pfilename
= filename
;
190 const char* basename
= lbasename(pfilename
->c_str());
191 size_t basename_pos
= basename
- pfilename
->c_str();
192 filename
= pfilename
->substr(0, basename_pos
) + "lib" + basename
+ ".so";
193 fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
196 *pfilename
= filename
;
200 filename
= pfilename
->substr(0, basename_pos
) + "lib" + basename
+ ".a";
201 fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
204 *pfilename
= filename
;
208 filename
= *pfilename
+ ".o";
209 fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
212 *pfilename
= filename
;
219 // Look for export data in the file descriptor FD.
222 Import::find_export_data(const std::string
& filename
, int fd
, Location location
)
224 // See if we can read this as an object file.
225 Import::Stream
* stream
= Import::find_object_export_data(filename
, fd
, 0,
230 const int len
= MAX(Export::magic_len
, Import::archive_magic_len
);
232 if (lseek(fd
, 0, SEEK_SET
) < 0)
234 go_error_at(location
, "lseek %s failed: %m", filename
.c_str());
239 ssize_t c
= ::read(fd
, buf
, len
);
243 // Check for a file containing nothing but Go export data.
244 if (memcmp(buf
, Export::cur_magic
, Export::magic_len
) == 0
245 || memcmp(buf
, Export::v1_magic
, Export::magic_len
) == 0
246 || memcmp(buf
, Export::v2_magic
, Export::magic_len
) == 0)
247 return new Stream_from_file(fd
);
249 // See if we can read this as an archive.
250 if (Import::is_archive_magic(buf
))
251 return Import::find_archive_export_data(filename
, fd
, location
);
256 // Look for export data in an object file.
259 Import::find_object_export_data(const std::string
& filename
,
267 const char *errmsg
= go_read_export_data(fd
, offset
, &buf
, &len
, &err
);
271 go_error_at(location
, "%s: %s", filename
.c_str(), errmsg
);
273 go_error_at(location
, "%s: %s: %s", filename
.c_str(), errmsg
,
281 return new Stream_from_buffer(buf
, len
);
286 // Construct an Import object. We make the builtin_types_ vector
287 // large enough to hold all the builtin types.
289 Import::Import(Stream
* stream
, Location location
)
290 : gogo_(NULL
), stream_(stream
), location_(location
), package_(NULL
),
291 add_to_globals_(false), type_data_(), type_pos_(0), type_offsets_(),
292 builtin_types_((- SMALLEST_BUILTIN_CODE
) + 1),
293 types_(), version_(EXPORT_FORMAT_UNKNOWN
)
297 // Import the data in the associated stream.
300 Import::import(Gogo
* gogo
, const std::string
& local_name
,
301 bool is_local_name_exported
)
303 // Hold on to the Gogo structure. Otherwise we need to pass it
304 // through all the import functions, because we need it when reading
308 // A stream of export data can include data from more than one input
309 // file. Here we loop over each input file.
310 Stream
* stream
= this->stream_
;
311 while (!stream
->at_eof() && !stream
->saw_error())
313 // The vector of types is package specific.
314 this->types_
.clear();
316 // Check magic string / version number.
317 if (stream
->match_bytes(Export::cur_magic
, Export::magic_len
))
319 stream
->require_bytes(this->location_
, Export::cur_magic
,
321 this->version_
= EXPORT_FORMAT_CURRENT
;
323 else if (stream
->match_bytes(Export::v1_magic
, Export::magic_len
))
325 stream
->require_bytes(this->location_
, Export::v1_magic
,
327 this->version_
= EXPORT_FORMAT_V1
;
329 else if (stream
->match_bytes(Export::v2_magic
, Export::magic_len
))
331 stream
->require_bytes(this->location_
, Export::v2_magic
,
333 this->version_
= EXPORT_FORMAT_V2
;
337 go_error_at(this->location_
,
338 ("error in import data at %d: invalid magic string"),
343 this->require_c_string("package ");
344 std::string package_name
= this->read_identifier();
345 this->require_semicolon_if_old_version();
346 this->require_c_string("\n");
349 std::string pkgpath_symbol
;
350 if (this->match_c_string("prefix "))
353 std::string unique_prefix
= this->read_identifier();
354 this->require_semicolon_if_old_version();
355 this->require_c_string("\n");
356 pkgpath
= unique_prefix
+ '.' + package_name
;
357 pkgpath_symbol
= (Gogo::pkgpath_for_symbol(unique_prefix
) + '.'
358 + Gogo::pkgpath_for_symbol(package_name
));
362 this->require_c_string("pkgpath ");
363 pkgpath
= this->read_identifier();
364 this->require_semicolon_if_old_version();
365 this->require_c_string("\n");
366 pkgpath_symbol
= Gogo::pkgpath_for_symbol(pkgpath
);
369 if (stream
->saw_error())
372 this->package_
= gogo
->add_imported_package(package_name
, local_name
,
373 is_local_name_exported
,
374 pkgpath
, pkgpath_symbol
,
376 &this->add_to_globals_
);
377 if (this->package_
== NULL
)
379 stream
->set_saw_error();
383 // Read and discard priority if older V1 export data format.
384 if (version() == EXPORT_FORMAT_V1
)
386 this->require_c_string("priority ");
387 std::string priority_string
= this->read_identifier();
389 if (!this->string_to_int(priority_string
, false, &prio
))
391 this->require_c_string(";\n");
394 while (stream
->match_c_string("package"))
395 this->read_one_package();
397 while (stream
->match_c_string("import"))
398 this->read_one_import();
400 while (stream
->match_c_string("indirectimport"))
401 this->read_one_indirect_import();
403 if (stream
->match_c_string("init"))
404 this->read_import_init_fns(gogo
);
406 if (stream
->match_c_string("types "))
408 if (!this->read_types())
412 // Loop over all the input data for this package.
413 while (!stream
->saw_error())
415 if (stream
->match_c_string("const "))
416 this->import_const();
417 else if (stream
->match_c_string("type "))
419 else if (stream
->match_c_string("var "))
421 else if (stream
->match_c_string("func "))
422 this->import_func(this->package_
);
423 else if (stream
->match_c_string("checksum "))
427 go_error_at(this->location_
,
428 ("error in import data at %d: "
429 "expected %<const%>, %<type%>, %<var%>, "
430 "%<func%>, or %<checksum%>"),
432 stream
->set_saw_error();
437 // We currently ignore the checksum. In the future we could
438 // store the checksum somewhere in the generated object and then
439 // verify that the checksum matches at link time or at dynamic
441 this->require_c_string("checksum ");
442 stream
->advance(Export::checksum_len
* 2);
443 this->require_semicolon_if_old_version();
444 this->require_c_string("\n");
447 return this->package_
;
450 // Read a package line. This let us reliably determine the pkgpath
451 // symbol, even if the package was compiled with a -fgo-prefix option.
454 Import::read_one_package()
456 this->require_c_string("package ");
457 std::string package_name
= this->read_identifier();
458 this->require_c_string(" ");
459 std::string pkgpath
= this->read_identifier();
460 this->require_c_string(" ");
461 std::string pkgpath_symbol
= this->read_identifier();
462 this->require_semicolon_if_old_version();
463 this->require_c_string("\n");
465 Package
* p
= this->gogo_
->register_package(pkgpath
, pkgpath_symbol
,
466 Linemap::unknown_location());
467 p
->set_package_name(package_name
, this->location());
470 // Read an import line.
473 Import::read_one_import()
475 this->require_c_string("import ");
476 std::string package_name
= this->read_identifier();
477 this->require_c_string(" ");
478 std::string pkgpath
= this->read_identifier();
479 this->require_c_string(" \"");
480 Stream
* stream
= this->stream_
;
481 while (stream
->peek_char() != '"')
483 this->require_c_string("\"");
484 this->require_semicolon_if_old_version();
485 this->require_c_string("\n");
487 Package
* p
= this->gogo_
->register_package(pkgpath
, "",
488 Linemap::unknown_location());
489 p
->set_package_name(package_name
, this->location());
492 // Read an indirectimport line.
495 Import::read_one_indirect_import()
497 this->require_c_string("indirectimport ");
498 std::string package_name
= this->read_identifier();
499 this->require_c_string(" ");
500 std::string pkgpath
= this->read_identifier();
501 this->require_c_string("\n");
503 Package
* p
= this->gogo_
->register_package(pkgpath
, "",
504 Linemap::unknown_location());
505 p
->set_package_name(package_name
, this->location());
508 // Read the list of import control functions and/or init graph.
511 Import::read_import_init_fns(Gogo
* gogo
)
513 this->require_c_string("init");
515 // Maps init function to index in the "init" clause; needed
516 // to read the init_graph section.
517 std::map
<std::string
, unsigned> init_idx
;
519 while (!this->match_c_string("\n") && !this->match_c_string(";"))
523 this->require_c_string(" ");
524 std::string package_name
= this->read_identifier();
525 this->require_c_string(" ");
526 std::string init_name
= this->read_identifier();
527 if (this->version_
== EXPORT_FORMAT_V1
)
529 // Older version 1 init fcn export data format is:
531 // <packname> <fcn> <priority>
532 this->require_c_string(" ");
533 std::string prio_string
= this->read_identifier();
534 if (!this->string_to_int(prio_string
, false, &priority
))
537 gogo
->add_import_init_fn(package_name
, init_name
, priority
);
539 // Record the index of this init fcn so that we can look it
540 // up by index in the subsequent init_graph section.
541 unsigned idx
= init_idx
.size();
542 init_idx
[init_name
] = idx
;
544 this->require_semicolon_if_old_version();
545 this->require_c_string("\n");
547 if (this->match_c_string("init_graph"))
549 this->require_c_string("init_graph");
551 // Build a vector mapping init fcn slot to Import_init pointer.
552 go_assert(init_idx
.size() > 0);
553 std::vector
<Import_init
*> import_initvec
;
554 import_initvec
.resize(init_idx
.size());
555 for (std::map
<std::string
, unsigned>::const_iterator it
=
557 it
!= init_idx
.end(); ++it
)
559 const std::string
& init_name
= it
->first
;
560 Import_init
* ii
= gogo
->lookup_init(init_name
);
561 import_initvec
[it
->second
] = ii
;
564 // Init graph format is:
566 // init_graph <src1> <sink1> <src2> <sink2> ... ;
568 // where src + sink are init functions indices.
570 while (!this->match_c_string("\n") && !this->match_c_string(";"))
572 this->require_c_string(" ");
573 std::string src_string
= this->read_identifier();
575 if (!this->string_to_unsigned(src_string
, &src
)) return;
577 this->require_c_string(" ");
578 std::string sink_string
= this->read_identifier();
580 if (!this->string_to_unsigned(sink_string
, &sink
)) return;
582 go_assert(src
< import_initvec
.size());
583 Import_init
* ii_src
= import_initvec
[src
];
584 go_assert(sink
< import_initvec
.size());
585 Import_init
* ii_sink
= import_initvec
[sink
];
587 ii_src
->record_precursor_fcn(ii_sink
->init_name());
589 this->require_semicolon_if_old_version();
590 this->require_c_string("\n");
594 // Import the types. Starting in export format version 3 all the
595 // types are listed first.
600 this->require_c_string("types ");
601 std::string str
= this->read_identifier();
603 if (!this->string_to_int(str
, false, &maxp1
))
606 this->require_c_string(" ");
607 str
= this->read_identifier();
609 if (!this->string_to_int(str
, false, &exportedp1
))
612 this->type_offsets_
.resize(maxp1
, std::make_pair
<size_t, size_t>(0, 0));
613 size_t total_type_size
= 0;
614 // Start at 1 because type index 0 not used.
615 for (int i
= 1; i
< maxp1
; i
++)
617 this->require_c_string(" ");
618 str
= this->read_identifier();
620 if (!this->string_to_int(str
, false, &v
))
622 size_t vs
= static_cast<size_t>(v
);
623 this->type_offsets_
[i
] = std::make_pair(total_type_size
, vs
);
624 total_type_size
+= vs
;
627 this->require_c_string("\n");
629 // Types can refer to each other in an unpredictable order. Read
630 // all the type data into type_data_. The type_offsets_ vector we
631 // just initialized provides indexes into type_data_.
633 this->type_pos_
= this->stream_
->pos();
634 const char* type_data
;
635 if (!this->stream_
->peek(total_type_size
, &type_data
))
637 this->type_data_
= std::string(type_data
, total_type_size
);
638 this->advance(total_type_size
);
640 this->types_
.resize(maxp1
, NULL
);
642 // Parse all the exported types now, so that the names are properly
643 // bound and visible to the parser. Parse unexported types lazily.
645 // Start at 1 because there is no type 0.
646 for (int i
= 1; i
< exportedp1
; i
++)
648 // We may have already parsed this type when we parsed an
650 Type
* type
= this->types_
[i
];
653 if (!this->parse_type(i
))
655 type
= this->types_
[i
];
656 go_assert(type
!= NULL
);
658 Named_type
* nt
= type
->named_type();
661 go_error_at(this->location_
,
662 "error in import data: exported unnamed type %d",
666 nt
->set_is_visible();
667 if (this->add_to_globals_
)
668 this->gogo_
->add_named_type(nt
);
674 // Import a constant.
677 Import::import_const()
682 Named_constant::import_const(this, &name
, &type
, &expr
);
683 Typed_identifier
tid(name
, type
, this->location_
);
684 Named_object
* no
= this->package_
->add_constant(tid
, expr
);
685 if (this->add_to_globals_
)
686 this->gogo_
->add_dot_import_object(no
);
692 Import::import_type()
694 if (this->version_
>= EXPORT_FORMAT_V3
)
696 if (!this->stream_
->saw_error())
698 go_error_at(this->location_
,
699 "error in import data at %d: old type syntax",
700 this->stream_
->pos());
701 this->stream_
->set_saw_error();
707 Named_type::import_named_type(this, &type
);
709 // The named type has been added to the package by the type import
710 // process. Here we need to make it visible to the parser, and it
711 // to the global bindings if necessary.
712 type
->set_is_visible();
714 if (this->add_to_globals_
)
715 this->gogo_
->add_named_type(type
);
718 // Import a variable.
725 Variable::import_var(this, &name
, &type
);
726 Variable
* var
= new Variable(type
, NULL
, true, false, false,
729 no
= this->package_
->add_variable(name
, var
);
730 if (this->add_to_globals_
)
731 this->gogo_
->add_dot_import_object(no
);
734 // Import a function into PACKAGE. PACKAGE is normally
735 // THIS->PACKAGE_, but it will be different for a method associated
736 // with a type defined in a different package.
739 Import::import_func(Package
* package
)
742 Typed_identifier
* receiver
;
743 Typed_identifier_list
* parameters
;
744 Typed_identifier_list
* results
;
748 Function::import_func(this, &name
, &receiver
, ¶meters
, &results
,
749 &is_varargs
, &nointerface
, &body
);
750 Function_type
*fntype
= Type::make_function_type(receiver
, parameters
,
751 results
, this->location_
);
753 fntype
->set_is_varargs();
755 Location loc
= this->location_
;
757 if (fntype
->is_method())
759 Type
* rtype
= receiver
->type();
761 // We may still be reading the definition of RTYPE, so we have
762 // to be careful to avoid calling base or convert. If RTYPE is
763 // a named type or a forward declaration, then we know that it
764 // is not a pointer, because we are reading a method on RTYPE
765 // and named pointers can't have methods.
767 if (rtype
->classification() == Type::TYPE_POINTER
)
768 rtype
= rtype
->points_to();
770 if (rtype
->is_error_type())
772 else if (rtype
->named_type() != NULL
)
773 no
= rtype
->named_type()->add_method_declaration(name
, package
, fntype
,
775 else if (rtype
->forward_declaration_type() != NULL
)
776 no
= rtype
->forward_declaration_type()->add_method_declaration(name
,
785 no
= package
->add_function_declaration(name
, fntype
, loc
);
786 if (this->add_to_globals_
)
787 this->gogo_
->add_dot_import_object(no
);
791 no
->func_declaration_value()->set_nointerface();
792 if (!body
.empty() && !no
->func_declaration_value()->has_imported_body())
793 no
->func_declaration_value()->set_imported_body(this, body
);
798 // Read a type definition and initialize the entry in this->types_.
799 // This parses the type definition saved by read_types earlier. This
800 // returns true on success, false on failure.
803 Import::parse_type(int i
)
805 go_assert(i
>= 0 && static_cast<size_t>(i
) < this->types_
.size());
806 go_assert(this->types_
[i
] == NULL
);
807 size_t offset
= this->type_offsets_
[i
].first
;
808 size_t len
= this->type_offsets_
[i
].second
;
810 Stream
* orig_stream
= this->stream_
;
812 Stream_from_string_ref
stream(this->type_data_
, offset
, len
);
813 stream
.set_pos(this->type_pos_
+ offset
);
814 this->stream_
= &stream
;
816 this->require_c_string("type ");
817 std::string str
= this->read_identifier();
819 if (!this->string_to_int(str
, false, &id
))
821 this->stream_
= orig_stream
;
826 go_error_at(this->location_
,
827 ("error in import data at %d: "
828 "type ID mismatch: got %d, want %d"),
829 stream
.pos(), id
, i
);
830 this->stream_
= orig_stream
;
834 this->require_c_string(" ");
835 if (stream
.peek_char() == '"')
838 Type
* type
= this->read_named_type(i
);
839 if (type
->is_error_type())
841 this->stream_
= orig_stream
;
847 Type
* type
= Type::import_type(this);
848 if (type
->is_error_type())
850 this->stream_
= orig_stream
;
853 this->types_
[i
] = type
;
855 this->require_c_string("\n");
858 this->stream_
= orig_stream
;
862 // Read a type in the import stream. This records the type by the
863 // type index. If the type is named (which can only happen with older
864 // export formats), it registers the name, but marks it as invisible.
869 Stream
* stream
= this->stream_
;
870 this->require_c_string("<type ");
876 c
= stream
->get_char();
877 if (c
!= '-' && (c
< '0' || c
> '9'))
883 if (!this->string_to_int(number
, true, &index
))
884 return Type::make_error_type();
888 // A reference to a type defined earlier.
890 return this->type_for_index(index
, "import data", stream
->pos(),
894 if (this->version_
>= EXPORT_FORMAT_V3
)
896 if (!stream
->saw_error())
897 go_error_at(this->location_
,
898 "error in import data at %d: expected %<>%>",
900 stream
->set_saw_error();
901 return Type::make_error_type();
906 if (!stream
->saw_error())
907 go_error_at(this->location_
,
908 "error in import data at %d: expected %< %> or %<>%>'",
910 stream
->set_saw_error();
912 return Type::make_error_type();
916 || (static_cast<size_t>(index
) < this->types_
.size()
917 && this->types_
[index
] != NULL
))
919 go_error_at(this->location_
,
920 "error in import data at %d: type index already defined",
922 stream
->set_saw_error();
923 return Type::make_error_type();
926 if (static_cast<size_t>(index
) >= this->types_
.size())
928 int newsize
= std::max(static_cast<size_t>(index
) + 1,
929 this->types_
.size() * 2);
930 this->types_
.resize(newsize
, NULL
);
933 if (stream
->peek_char() != '"')
935 Type
* type
= Type::import_type(this);
936 this->require_c_string(">");
937 this->types_
[index
] = type
;
943 Type
* type
= this->read_named_type(index
);
945 this->require_c_string(">");
950 // Read a named type from the import stream and store it in
951 // this->types_[index]. The stream should be positioned immediately
952 // after the '"' that starts the name.
955 Import::read_named_type(int index
)
957 Stream
* stream
= this->stream_
;
958 std::string type_name
;
960 while ((c
= stream
->get_char()) != '"')
963 // If this type is in the package we are currently importing, the
964 // name will be .PKGPATH.NAME or simply NAME with no dots.
965 // Otherwise, a non-hidden symbol will be PKGPATH.NAME and a hidden
966 // symbol will be .PKGPATH.NAME.
968 if (type_name
.find('.') != std::string::npos
)
971 if (type_name
[0] == '.')
973 size_t dot
= type_name
.rfind('.');
974 pkgpath
= type_name
.substr(start
, dot
- start
);
975 if (type_name
[0] != '.')
976 type_name
.erase(0, dot
+ 1);
979 this->require_c_string(" ");
981 // The package name may follow. This is the name of the package in
982 // the package clause of that package. The type name will include
983 // the pkgpath, which may be different.
984 std::string package_name
;
985 if (stream
->peek_char() == '"')
988 while ((c
= stream
->get_char()) != '"')
990 this->require_c_string(" ");
993 bool is_alias
= false;
994 if (this->match_c_string("= "))
1000 // Declare the type in the appropriate package. If we haven't seen
1001 // it before, mark it as invisible. We declare it before we read
1002 // the actual definition of the type, since the definition may refer
1003 // to the type itself.
1005 if (pkgpath
.empty() || pkgpath
== this->gogo_
->pkgpath())
1006 package
= this->package_
;
1009 package
= this->gogo_
->register_package(pkgpath
, "",
1010 Linemap::unknown_location());
1011 if (!package_name
.empty())
1012 package
->set_package_name(package_name
, this->location());
1015 Named_object
* no
= package
->bindings()->lookup(type_name
);
1017 no
= package
->add_type_declaration(type_name
, this->location_
);
1018 else if (!no
->is_type_declaration() && !no
->is_type())
1020 go_error_at(this->location_
, "imported %<%s.%s%> both type and non-type",
1021 pkgpath
.c_str(), Gogo::message_name(type_name
).c_str());
1022 stream
->set_saw_error();
1023 return Type::make_error_type();
1026 go_assert(no
->package() == package
);
1028 if (this->types_
[index
] == NULL
)
1030 if (no
->is_type_declaration())
1032 // FIXME: It's silly to make a forward declaration every time.
1033 this->types_
[index
] = Type::make_forward_declaration(no
);
1037 go_assert(no
->is_type());
1038 this->types_
[index
] = no
->type_value();
1042 // If there is no type definition, then this is just a forward
1043 // declaration of a type defined in some other file.
1045 if (this->match_c_string(">") || this->match_c_string("\n"))
1046 type
= this->types_
[index
];
1049 type
= this->read_type();
1051 if (no
->is_type_declaration())
1053 // We can define the type now.
1055 no
= package
->add_type(type_name
, type
, this->location_
);
1056 Named_type
* ntype
= no
->type_value();
1058 // This type has not yet been imported.
1059 ntype
->clear_is_visible();
1062 ntype
->set_is_alias();
1064 if (!type
->is_undefined() && type
->interface_type() != NULL
)
1065 this->gogo_
->record_interface_type(type
->interface_type());
1069 else if (no
->is_type())
1071 // We have seen this type before. FIXME: it would be a good
1072 // idea to check that the two imported types are identical,
1073 // but we have not finalized the methods yet, which means
1074 // that we can not reliably compare interface types.
1075 type
= no
->type_value();
1077 // Don't change the visibility of the existing type.
1080 this->types_
[index
] = type
;
1082 // Read the type methods.
1083 if (this->match_c_string("\n"))
1086 while (this->match_c_string(" func"))
1089 this->import_func(package
);
1097 // Return the type given an index. Set *PARSED if we parsed it here.
1100 Import::type_for_index(int index
, const std::string
& input_name
,
1101 size_t input_offset
, bool* parsed
)
1104 if (index
>= 0 && !this->type_data_
.empty())
1106 if (static_cast<size_t>(index
) >= this->type_offsets_
.size())
1108 go_error_at(this->location_
,
1109 "error in %s at %lu: bad type index %d >= %d",
1111 static_cast<unsigned long>(input_offset
),
1112 index
, static_cast<int>(this->type_offsets_
.size()));
1113 return Type::make_error_type();
1116 if (this->types_
[index
] == NULL
)
1118 if (!this->parse_type(index
))
1119 return Type::make_error_type();
1125 ? (static_cast<size_t>(- index
) >= this->builtin_types_
.size()
1126 || this->builtin_types_
[- index
] == NULL
)
1127 : (static_cast<size_t>(index
) >= this->types_
.size()
1128 || this->types_
[index
] == NULL
))
1130 go_error_at(this->location_
,
1131 "error in %s at %lu: bad type index %d",
1133 static_cast<unsigned long>(input_offset
), index
);
1134 return Type::make_error_type();
1137 return index
< 0 ? this->builtin_types_
[- index
] : this->types_
[index
];
1140 // Read an escape note.
1143 Import::read_escape()
1145 if (this->match_c_string(" <esc:"))
1147 Stream
* stream
= this->stream_
;
1148 this->require_c_string(" <esc:");
1150 std::string escape
= "esc:";
1154 c
= stream
->get_char();
1155 if (c
!= 'x' && !ISXDIGIT(c
))
1162 go_error_at(this->location(),
1163 ("error in import data at %d: "
1164 "expect %< %> or %<>%>, got %c"),
1166 stream
->set_saw_error();
1168 escape
= Escape_note::make_tag(Node::ESCAPE_UNKNOWN
);
1173 return Escape_note::make_tag(Node::ESCAPE_UNKNOWN
);
1177 // Register the builtin types.
1180 Import::register_builtin_types(Gogo
* gogo
)
1182 this->register_builtin_type(gogo
, "int8", BUILTIN_INT8
);
1183 this->register_builtin_type(gogo
, "int16", BUILTIN_INT16
);
1184 this->register_builtin_type(gogo
, "int32", BUILTIN_INT32
);
1185 this->register_builtin_type(gogo
, "int64", BUILTIN_INT64
);
1186 this->register_builtin_type(gogo
, "uint8", BUILTIN_UINT8
);
1187 this->register_builtin_type(gogo
, "uint16", BUILTIN_UINT16
);
1188 this->register_builtin_type(gogo
, "uint32", BUILTIN_UINT32
);
1189 this->register_builtin_type(gogo
, "uint64", BUILTIN_UINT64
);
1190 this->register_builtin_type(gogo
, "float32", BUILTIN_FLOAT32
);
1191 this->register_builtin_type(gogo
, "float64", BUILTIN_FLOAT64
);
1192 this->register_builtin_type(gogo
, "complex64", BUILTIN_COMPLEX64
);
1193 this->register_builtin_type(gogo
, "complex128", BUILTIN_COMPLEX128
);
1194 this->register_builtin_type(gogo
, "int", BUILTIN_INT
);
1195 this->register_builtin_type(gogo
, "uint", BUILTIN_UINT
);
1196 this->register_builtin_type(gogo
, "uintptr", BUILTIN_UINTPTR
);
1197 this->register_builtin_type(gogo
, "bool", BUILTIN_BOOL
);
1198 this->register_builtin_type(gogo
, "string", BUILTIN_STRING
);
1199 this->register_builtin_type(gogo
, "error", BUILTIN_ERROR
);
1200 this->register_builtin_type(gogo
, "byte", BUILTIN_BYTE
);
1201 this->register_builtin_type(gogo
, "rune", BUILTIN_RUNE
);
1204 // Register a single builtin type.
1207 Import::register_builtin_type(Gogo
* gogo
, const char* name
, Builtin_code code
)
1209 Named_object
* named_object
= gogo
->lookup_global(name
);
1210 go_assert(named_object
!= NULL
&& named_object
->is_type());
1211 int index
= - static_cast<int>(code
);
1213 && static_cast<size_t>(index
) < this->builtin_types_
.size());
1214 this->builtin_types_
[index
] = named_object
->type_value();
1217 // Read an identifier from the stream.
1220 Import::read_identifier()
1223 Stream
* stream
= this->stream_
;
1227 c
= stream
->peek_char();
1228 if (c
== -1 || c
== ' ' || c
== '\n' || c
== ';' || c
== ')')
1236 // Read a name from the stream.
1241 std::string ret
= this->read_identifier();
1247 // Read LENGTH bytes from the stream.
1250 Import::read(size_t length
)
1253 if (!this->stream_
->peek(length
, &data
))
1255 if (!this->stream_
->saw_error())
1256 go_error_at(this->location_
, "import error at %d: expected %d bytes",
1257 this->stream_
->pos(), static_cast<int>(length
));
1258 this->stream_
->set_saw_error();
1261 this->advance(length
);
1262 return std::string(data
, length
);
1265 // Turn a string into a integer with appropriate error handling.
1268 Import::string_to_int(const std::string
&s
, bool is_neg_ok
, int* ret
)
1271 long prio
= strtol(s
.c_str(), &end
, 10);
1272 if (*end
!= '\0' || prio
> 0x7fffffff || (prio
< 0 && !is_neg_ok
))
1274 go_error_at(this->location_
, "invalid integer in import data at %d",
1275 this->stream_
->pos());
1276 this->stream_
->set_saw_error();
1283 // Class Import::Stream.
1285 Import::Stream::Stream()
1286 : pos_(0), saw_error_(false)
1290 Import::Stream::~Stream()
1294 // Return the next character to come from the stream.
1297 Import::Stream::peek_char()
1300 if (!this->do_peek(1, &read
))
1302 // Make sure we return an unsigned char, so that we don't get
1303 // confused by \xff.
1304 unsigned char ret
= *read
;
1308 // Return true if the next LENGTH characters from the stream match
1312 Import::Stream::match_bytes(const char* bytes
, size_t length
)
1315 if (!this->do_peek(length
, &read
))
1317 return memcmp(bytes
, read
, length
) == 0;
1320 // Require that the next LENGTH bytes from the stream match BYTES.
1323 Import::Stream::require_bytes(Location location
, const char* bytes
,
1327 if (!this->do_peek(length
, &read
)
1328 || memcmp(bytes
, read
, length
) != 0)
1330 if (!this->saw_error_
)
1331 go_error_at(location
, "import error at %d: expected %<%.*s%>",
1332 this->pos(), static_cast<int>(length
), bytes
);
1333 this->saw_error_
= true;
1336 this->advance(length
);
1339 // Class Stream_from_file.
1341 Stream_from_file::Stream_from_file(int fd
)
1344 if (lseek(fd
, 0, SEEK_SET
) != 0)
1346 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1347 this->set_saw_error();
1351 Stream_from_file::~Stream_from_file()
1359 Stream_from_file::do_peek(size_t length
, const char** bytes
)
1361 if (this->data_
.length() <= length
)
1363 *bytes
= this->data_
.data();
1367 this->data_
.resize(length
);
1368 ssize_t got
= ::read(this->fd_
, &this->data_
[0], length
);
1372 if (!this->saw_error())
1373 go_fatal_error(Linemap::unknown_location(), "read failed: %m");
1374 this->set_saw_error();
1378 if (lseek(this->fd_
, - got
, SEEK_CUR
) != 0)
1380 if (!this->saw_error())
1381 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1382 this->set_saw_error();
1386 if (static_cast<size_t>(got
) < length
)
1389 *bytes
= this->data_
.data();
1396 Stream_from_file::do_advance(size_t skip
)
1398 if (lseek(this->fd_
, skip
, SEEK_CUR
) != 0)
1400 if (!this->saw_error())
1401 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1402 this->set_saw_error();
1404 if (!this->data_
.empty())
1406 if (this->data_
.length() < skip
)
1407 this->data_
.erase(0, skip
);
1409 this->data_
.clear();
1413 // Class Import_function_body.
1415 // The name of the function we are parsing.
1418 Import_function_body::name() const
1420 return this->named_object_
->name();
1423 // Class Import_function_body.
1425 // Require that the next bytes match STR, issuing an error if not.
1426 // Advance past the string.
1429 Import_function_body::require_c_string(const char* str
)
1431 if (!this->match_c_string(str
))
1433 if (!this->saw_error_
)
1434 go_error_at(this->location(),
1435 "invalid export data for %qs: expected %qs at %lu",
1436 this->name().c_str(), str
,
1437 static_cast<unsigned long>(this->off_
));
1438 this->saw_error_
= true;
1441 this->advance(strlen(str
));
1444 // Read an identifier.
1447 Import_function_body::read_identifier()
1449 size_t start
= this->off_
;
1450 for (size_t i
= start
; i
< this->body_
.length(); i
++)
1452 int c
= static_cast<unsigned char>(this->body_
[i
]);
1453 if (c
== ' ' || c
== '\n' || c
== ';' || c
== ')')
1456 return this->body_
.substr(start
, i
- start
);
1459 this->off_
= this->body_
.length();
1460 return this->body_
.substr(start
);
1466 Import_function_body::read_type()
1468 this->require_c_string("<type ");
1469 size_t start
= this->off_
;
1472 for (i
= start
; i
< this->body_
.length(); ++i
)
1474 c
= static_cast<unsigned char>(this->body_
[i
]);
1475 if (c
!= '-' && (c
< '0' || c
> '9'))
1481 std::string num
= this->body_
.substr(start
, i
- start
);
1482 long val
= strtol(num
.c_str(), &end
, 10);
1483 if (*end
!= '\0' || val
> 0x7fffffff)
1485 if (!this->saw_error_
)
1486 go_error_at(this->location(),
1487 "invalid export data for %qs: expected integer at %lu",
1488 this->name().c_str(),
1489 static_cast<unsigned long>(start
));
1490 this->saw_error_
= true;
1491 return Type::make_error_type();
1496 if (!this->saw_error_
)
1497 go_error_at(this->location(),
1498 "invalid export data for %qs: expected %<>%> at %lu",
1499 this->name().c_str(),
1500 static_cast<unsigned long>(i
));
1501 this->saw_error_
= true;
1502 return Type::make_error_type();
1506 Type
* type
= this->imp_
->type_for_index(static_cast<int>(val
), this->name(),
1507 static_cast<unsigned long>(start
),
1510 // If we just read this type's information, its methods will not
1511 // have been finalized. Do that now.
1513 this->gogo_
->finalize_methods_for_type(type
);