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 // Read an importcfg file.
40 Gogo::read_importcfg(const char* filename
)
43 if (!Gogo::read_file(filename
, Linemap::unknown_location(), &data
))
45 const char* p
= data
.data();
46 const char* pend
= p
+ data
.length();
48 const char *pnext
= NULL
;
49 for (; p
< pend
; p
= pnext
)
51 // Line numbers start at 1.
55 const char* pnl
= static_cast<const char*>(memchr(p
, '\n', pend
- p
));
64 // Trim leading spaces.
68 int rune_len
= Lex::fetch_char(p
, &rune
);
71 go_error_at(Linemap::unknown_location(),
72 "%s:%d: invalid character in importcfg file",
76 if (!Lex::is_unicode_space(rune
))
81 // Trim trailing spaces.
84 size_t start
= pnl
- p
- 1;
85 unsigned int rune
= (unsigned char)p
[start
];
89 for (start
--; start
> 0; start
--)
91 unsigned char c
= p
[start
];
92 if ((c
& 0xc0) != 0x80)
95 rune_len
= Lex::fetch_char(p
+ start
, &rune
);
96 if (static_cast<size_t>(rune_len
) != (pnl
- p
) - start
)
98 go_error_at(Linemap::unknown_location(),
99 "%s:%d: invalid character in importcfg file",
104 if (!Lex::is_unicode_space(rune
))
109 // Skip empty lines and comment lines.
110 if (p
== pnl
|| *p
== '#')
114 const char* psp
= static_cast<const char*>(memchr(p
, ' ', pnl
- p
));
120 bool importmap
= false;
121 bool packagefile
= false;
122 if (strncmp(p
, "importmap", verb_len
) == 0)
124 else if (strncmp(p
, "packagefile", verb_len
) == 0)
128 go_error_at(Linemap::unknown_location(),
129 "%s:%d: unknown directive in importcfg file",
140 peq
= static_cast<const char*>(memchr(psp
, '=', pnl
- psp
));
142 if (peq
== NULL
|| peq
+ 1 == pnl
)
144 go_error_at(Linemap::unknown_location(),
145 "%s:%d: invalid syntax in importcfg file",
150 std::string
first(psp
, peq
- psp
);
151 std::string
second(peq
+ 1, pnl
- (peq
+ 1));
153 this->import_map_
[first
] = second
;
154 else if (packagefile
)
155 this->package_file_
[first
] = second
;
161 // Find import data. This searches the file system for FILENAME and
162 // returns a pointer to a Stream object to read the data that it
163 // exports. If the file is not found, it returns NULL.
165 // When FILENAME is not an absolute path and does not start with ./ or
166 // ../, we use the search path provided by -I and -L options.
168 // When FILENAME does start with ./ or ../, we use
169 // RELATIVE_IMPORT_PATH as a prefix.
171 // When FILENAME does not exist, we try modifying FILENAME to find the
172 // file. We use the first of these which exists:
173 // * We append ".gox".
174 // * We turn the base of FILENAME into libFILENAME.so.
175 // * We turn the base of FILENAME into libFILENAME.a.
178 // When using a search path, we apply each of these transformations at
179 // each entry on the search path before moving on to the next entry.
180 // If the file exists, but does not contain any Go export data, we
181 // stop; we do not keep looking for another file with the same name
182 // later in the search path.
185 Import::open_package(const std::string
& filename
, Location location
,
186 const std::string
& relative_import_path
)
189 if (IS_ABSOLUTE_PATH(filename
))
191 else if (filename
[0] == '.'
192 && (filename
[1] == '\0' || IS_DIR_SEPARATOR(filename
[1])))
194 else if (filename
[0] == '.'
195 && filename
[1] == '.'
196 && (filename
[2] == '\0' || IS_DIR_SEPARATOR(filename
[2])))
201 std::string fn
= filename
;
202 if (is_local
&& !IS_ABSOLUTE_PATH(filename
) && !relative_import_path
.empty())
207 fn
= relative_import_path
;
209 else if (fn
[0] == '.' && fn
[1] == '.'
210 && (fn
[2] == '\0' || IS_DIR_SEPARATOR(fn
[2])))
212 // We are going to join relative_import_path and fn, and it
213 // will look like DIR/../PATH. But DIR does not necessarily
214 // exist in this case, and if it doesn't the use of .. will
215 // fail although it shouldn't. The gc compiler uses
216 // path.Join here, which cleans up the .., so we need to do
219 for (index
= relative_import_path
.length() - 1;
220 index
> 0 && !IS_DIR_SEPARATOR(relative_import_path
[index
]);
224 fn
= relative_import_path
.substr(0, index
) + fn
.substr(2);
226 fn
= relative_import_path
+ '/' + fn
;
229 fn
= relative_import_path
+ '/' + fn
;
235 for (std::vector
<std::string
>::const_iterator p
= search_path
.begin();
236 p
!= search_path
.end();
239 std::string indir
= *p
;
240 if (!indir
.empty() && indir
[indir
.size() - 1] != '/')
243 Stream
* s
= Import::try_package_in_directory(indir
, location
);
249 Stream
* s
= Import::try_package_in_directory(fn
, location
);
256 // Try to find the export data for FILENAME.
259 Import::try_package_in_directory(const std::string
& filename
,
262 std::string found_filename
= filename
;
263 int fd
= open(found_filename
.c_str(), O_RDONLY
| O_BINARY
);
268 if (fstat(fd
, &s
) >= 0 && S_ISDIR(s
.st_mode
))
278 if (errno
!= ENOENT
&& errno
!= EISDIR
)
279 go_warning_at(location
, 0, "%s: %m", filename
.c_str());
281 fd
= Import::try_suffixes(&found_filename
);
286 // The export data may not be in this file.
287 Stream
* s
= Import::find_export_data(found_filename
, fd
, location
);
293 go_error_at(location
, "%s exists but does not contain any Go export data",
294 found_filename
.c_str());
299 // Given import "*PFILENAME", where *PFILENAME does not exist, try
300 // various suffixes. If we find one, set *PFILENAME to the one we
301 // found. Return the open file descriptor.
304 Import::try_suffixes(std::string
* pfilename
)
306 std::string filename
= *pfilename
+ ".gox";
307 int fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
310 *pfilename
= filename
;
314 const char* basename
= lbasename(pfilename
->c_str());
315 size_t basename_pos
= basename
- pfilename
->c_str();
316 filename
= pfilename
->substr(0, basename_pos
) + "lib" + basename
+ ".so";
317 fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
320 *pfilename
= filename
;
324 filename
= pfilename
->substr(0, basename_pos
) + "lib" + basename
+ ".a";
325 fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
328 *pfilename
= filename
;
332 filename
= *pfilename
+ ".o";
333 fd
= open(filename
.c_str(), O_RDONLY
| O_BINARY
);
336 *pfilename
= filename
;
343 // Look for export data in the file descriptor FD.
346 Import::find_export_data(const std::string
& filename
, int fd
, Location location
)
348 // See if we can read this as an object file.
349 Import::Stream
* stream
= Import::find_object_export_data(filename
, fd
, 0,
354 const int len
= MAX(Export::magic_len
, Import::archive_magic_len
);
356 if (lseek(fd
, 0, SEEK_SET
) < 0)
358 go_error_at(location
, "lseek %s failed: %m", filename
.c_str());
363 ssize_t c
= ::read(fd
, buf
, len
);
367 // Check for a file containing nothing but Go export data.
368 if (memcmp(buf
, Export::cur_magic
, Export::magic_len
) == 0
369 || memcmp(buf
, Export::v1_magic
, Export::magic_len
) == 0
370 || memcmp(buf
, Export::v2_magic
, Export::magic_len
) == 0)
371 return new Stream_from_file(fd
);
373 // See if we can read this as an archive.
374 if (Import::is_archive_magic(buf
))
375 return Import::find_archive_export_data(filename
, fd
, location
);
380 // Look for export data in an object file.
383 Import::find_object_export_data(const std::string
& filename
,
391 const char *errmsg
= go_read_export_data(fd
, offset
, &buf
, &len
, &err
);
395 go_error_at(location
, "%s: %s", filename
.c_str(), errmsg
);
397 go_error_at(location
, "%s: %s: %s", filename
.c_str(), errmsg
,
405 return new Stream_from_buffer(buf
, len
);
410 // Construct an Import object. We make the builtin_types_ vector
411 // large enough to hold all the builtin types.
413 Import::Import(Stream
* stream
, Location location
)
414 : gogo_(NULL
), stream_(stream
), location_(location
), package_(NULL
),
415 add_to_globals_(false), packages_(), type_data_(), type_pos_(0),
416 type_offsets_(), builtin_types_((- SMALLEST_BUILTIN_CODE
) + 1),
417 types_(), version_(EXPORT_FORMAT_UNKNOWN
)
421 // Import the data in the associated stream.
424 Import::import(Gogo
* gogo
, const std::string
& local_name
,
425 bool is_local_name_exported
)
427 // Hold on to the Gogo structure. Otherwise we need to pass it
428 // through all the import functions, because we need it when reading
432 // A stream of export data can include data from more than one input
433 // file. Here we loop over each input file.
434 Stream
* stream
= this->stream_
;
435 while (!stream
->at_eof() && !stream
->saw_error())
437 // The vector of types is package specific.
438 this->types_
.clear();
440 // Check magic string / version number.
441 if (stream
->match_bytes(Export::cur_magic
, Export::magic_len
))
443 stream
->require_bytes(this->location_
, Export::cur_magic
,
445 this->version_
= EXPORT_FORMAT_CURRENT
;
447 else if (stream
->match_bytes(Export::v1_magic
, Export::magic_len
))
449 stream
->require_bytes(this->location_
, Export::v1_magic
,
451 this->version_
= EXPORT_FORMAT_V1
;
453 else if (stream
->match_bytes(Export::v2_magic
, Export::magic_len
))
455 stream
->require_bytes(this->location_
, Export::v2_magic
,
457 this->version_
= EXPORT_FORMAT_V2
;
461 go_error_at(this->location_
,
462 ("error in import data at %d: invalid magic string"),
467 this->require_c_string("package ");
468 std::string package_name
= this->read_identifier();
469 this->require_semicolon_if_old_version();
470 this->require_c_string("\n");
473 std::string pkgpath_symbol
;
474 if (this->match_c_string("prefix "))
477 std::string unique_prefix
= this->read_identifier();
478 this->require_semicolon_if_old_version();
479 this->require_c_string("\n");
480 pkgpath
= unique_prefix
+ '.' + package_name
;
481 pkgpath_symbol
= (Gogo::pkgpath_for_symbol(unique_prefix
) + '.'
482 + Gogo::pkgpath_for_symbol(package_name
));
486 this->require_c_string("pkgpath ");
487 pkgpath
= this->read_identifier();
488 this->require_semicolon_if_old_version();
489 this->require_c_string("\n");
490 pkgpath_symbol
= Gogo::pkgpath_for_symbol(pkgpath
);
493 if (stream
->saw_error())
496 this->package_
= gogo
->add_imported_package(package_name
, local_name
,
497 is_local_name_exported
,
498 pkgpath
, pkgpath_symbol
,
500 &this->add_to_globals_
);
501 if (this->package_
== NULL
)
503 stream
->set_saw_error();
507 // Read and discard priority if older V1 export data format.
508 if (version() == EXPORT_FORMAT_V1
)
510 this->require_c_string("priority ");
511 std::string priority_string
= this->read_identifier();
513 if (!this->string_to_int(priority_string
, false, &prio
))
515 this->require_c_string(";\n");
518 while (stream
->match_c_string("package"))
519 this->read_one_package();
521 while (stream
->match_c_string("import"))
522 this->read_one_import();
524 while (stream
->match_c_string("indirectimport"))
525 this->read_one_indirect_import();
527 if (stream
->match_c_string("init"))
528 this->read_import_init_fns(gogo
);
530 if (stream
->match_c_string("types "))
532 if (!this->read_types())
536 // Loop over all the input data for this package.
537 while (!stream
->saw_error())
539 if (stream
->match_c_string("const "))
540 this->import_const();
541 else if (stream
->match_c_string("type "))
543 else if (stream
->match_c_string("var "))
545 else if (stream
->match_c_string("func "))
546 this->import_func(this->package_
);
547 else if (stream
->match_c_string("checksum "))
551 go_error_at(this->location_
,
552 ("error in import data at %d: "
553 "expected %<const%>, %<type%>, %<var%>, "
554 "%<func%>, or %<checksum%>"),
556 stream
->set_saw_error();
561 // We currently ignore the checksum. In the future we could
562 // store the checksum somewhere in the generated object and then
563 // verify that the checksum matches at link time or at dynamic
565 this->require_c_string("checksum ");
566 stream
->advance(Export::checksum_len
* 2);
567 this->require_semicolon_if_old_version();
568 this->require_c_string("\n");
571 // Finalize methods for any imported types. This call is made late in the
572 // import process so as to A) avoid finalization of a type whose methods
573 // refer to types that are only partially read in, and B) capture both the
574 // types imported by read_types() directly, and those imported indirectly
575 // because they are referenced by an imported function or variable.
576 // See issues #33013 and #33219 for more on why this is needed.
577 this->finalize_methods();
579 return this->package_
;
582 // Read a package line. This let us reliably determine the pkgpath
583 // symbol, even if the package was compiled with a -fgo-prefix option.
586 Import::read_one_package()
588 this->require_c_string("package ");
589 std::string package_name
= this->read_identifier();
590 this->require_c_string(" ");
591 std::string pkgpath
= this->read_identifier();
592 this->require_c_string(" ");
593 std::string pkgpath_symbol
= this->read_identifier();
594 this->require_semicolon_if_old_version();
595 this->require_c_string("\n");
597 Package
* p
= this->gogo_
->register_package(pkgpath
, pkgpath_symbol
,
598 Linemap::unknown_location());
599 p
->set_package_name(package_name
, this->location());
602 // Read an import line.
605 Import::read_one_import()
607 this->require_c_string("import ");
608 std::string package_name
= this->read_identifier();
609 this->require_c_string(" ");
610 std::string pkgpath
= this->read_identifier();
611 this->require_c_string(" \"");
612 Stream
* stream
= this->stream_
;
613 while (stream
->peek_char() != '"')
615 this->require_c_string("\"");
616 this->require_semicolon_if_old_version();
617 this->require_c_string("\n");
619 Package
* p
= this->gogo_
->register_package(pkgpath
, "",
620 Linemap::unknown_location());
621 p
->set_package_name(package_name
, this->location());
623 this->packages_
.push_back(p
);
625 if (pkgpath
== "unsafe")
626 this->gogo_
->add_unsafe_bindings(p
);
629 // Read an indirectimport line.
632 Import::read_one_indirect_import()
634 this->require_c_string("indirectimport ");
635 std::string package_name
= this->read_identifier();
636 this->require_c_string(" ");
637 std::string pkgpath
= this->read_identifier();
638 this->require_c_string("\n");
640 Package
* p
= this->gogo_
->register_package(pkgpath
, "",
641 Linemap::unknown_location());
642 p
->set_package_name(package_name
, this->location());
644 this->packages_
.push_back(p
);
646 if (pkgpath
== "unsafe")
647 this->gogo_
->add_unsafe_bindings(p
);
650 // Read the list of import control functions and/or init graph.
653 Import::read_import_init_fns(Gogo
* gogo
)
655 this->require_c_string("init");
657 // Maps init function to index in the "init" clause; needed
658 // to read the init_graph section.
659 std::map
<std::string
, unsigned> init_idx
;
661 while (!this->match_c_string("\n") && !this->match_c_string(";"))
665 this->require_c_string(" ");
666 std::string package_name
= this->read_identifier();
667 this->require_c_string(" ");
668 std::string init_name
= this->read_identifier();
669 if (this->version_
== EXPORT_FORMAT_V1
)
671 // Older version 1 init fcn export data format is:
673 // <packname> <fcn> <priority>
674 this->require_c_string(" ");
675 std::string prio_string
= this->read_identifier();
676 if (!this->string_to_int(prio_string
, false, &priority
))
679 gogo
->add_import_init_fn(package_name
, init_name
, priority
);
681 // Record the index of this init fcn so that we can look it
682 // up by index in the subsequent init_graph section.
683 unsigned idx
= init_idx
.size();
684 init_idx
[init_name
] = idx
;
686 this->require_semicolon_if_old_version();
687 this->require_c_string("\n");
689 if (this->match_c_string("init_graph"))
691 this->require_c_string("init_graph");
693 // Build a vector mapping init fcn slot to Import_init pointer.
694 go_assert(init_idx
.size() > 0);
695 std::vector
<Import_init
*> import_initvec
;
696 import_initvec
.resize(init_idx
.size());
697 for (std::map
<std::string
, unsigned>::const_iterator it
=
699 it
!= init_idx
.end(); ++it
)
701 const std::string
& init_name
= it
->first
;
702 Import_init
* ii
= gogo
->lookup_init(init_name
);
703 import_initvec
[it
->second
] = ii
;
706 // Init graph format is:
708 // init_graph <src1> <sink1> <src2> <sink2> ... ;
710 // where src + sink are init functions indices.
712 while (!this->match_c_string("\n") && !this->match_c_string(";"))
714 this->require_c_string(" ");
715 std::string src_string
= this->read_identifier();
717 if (!this->string_to_unsigned(src_string
, &src
)) return;
719 this->require_c_string(" ");
720 std::string sink_string
= this->read_identifier();
722 if (!this->string_to_unsigned(sink_string
, &sink
)) return;
724 go_assert(src
< import_initvec
.size());
725 Import_init
* ii_src
= import_initvec
[src
];
726 go_assert(sink
< import_initvec
.size());
727 Import_init
* ii_sink
= import_initvec
[sink
];
729 ii_src
->record_precursor_fcn(ii_sink
->init_name());
731 this->require_semicolon_if_old_version();
732 this->require_c_string("\n");
736 // Import the types. Starting in export format version 3 all the
737 // types are listed first.
742 this->require_c_string("types ");
743 std::string str
= this->read_identifier();
745 if (!this->string_to_int(str
, false, &maxp1
))
748 this->require_c_string(" ");
749 str
= this->read_identifier();
751 if (!this->string_to_int(str
, false, &exportedp1
))
754 this->type_offsets_
.resize(maxp1
, std::make_pair
<size_t, size_t>(0, 0));
755 size_t total_type_size
= 0;
756 // Start at 1 because type index 0 not used.
757 for (int i
= 1; i
< maxp1
; i
++)
759 this->require_c_string(" ");
760 str
= this->read_identifier();
762 if (!this->string_to_int(str
, false, &v
))
764 size_t vs
= static_cast<size_t>(v
);
765 this->type_offsets_
[i
] = std::make_pair(total_type_size
, vs
);
766 total_type_size
+= vs
;
769 this->require_c_string("\n");
771 // Types can refer to each other in an unpredictable order. Read
772 // all the type data into type_data_. The type_offsets_ vector we
773 // just initialized provides indexes into type_data_.
775 this->type_pos_
= this->stream_
->pos();
776 const char* type_data
;
777 if (!this->stream_
->peek(total_type_size
, &type_data
))
779 this->type_data_
= std::string(type_data
, total_type_size
);
780 this->advance(total_type_size
);
782 this->types_
.resize(maxp1
, NULL
);
784 // Parse all the exported types now, so that the names are properly
785 // bound and visible to the parser. Parse unexported types lazily.
787 // Start at 1 because there is no type 0.
788 for (int i
= 1; i
< exportedp1
; i
++)
790 // We may have already parsed this type when we parsed an
792 Type
* type
= this->types_
[i
];
795 if (!this->parse_type(i
))
797 type
= this->types_
[i
];
798 go_assert(type
!= NULL
);
800 Named_type
* nt
= type
->named_type();
803 go_error_at(this->location_
,
804 "error in import data: exported unnamed type %d",
808 nt
->set_is_visible();
809 if (this->add_to_globals_
)
810 this->gogo_
->add_named_type(nt
);
817 Import::finalize_methods()
819 Finalize_methods
finalizer(this->gogo_
);
820 Unordered_set(Type
*) real_for_named
;
821 for (size_t i
= 1; i
< this->types_
.size(); i
++)
823 Type
* type
= this->types_
[i
];
824 if (type
!= NULL
&& type
->named_type() != NULL
)
826 finalizer
.type(type
);
828 // If the real type is a struct type, we don't want to
829 // finalize its methods. For a named type defined as a
830 // struct type, we only want to finalize the methods of the
831 // named type. This is like Finalize_methods::type.
832 Type
* real_type
= type
->named_type()->real_type();
833 if (real_type
->struct_type() != NULL
)
834 real_for_named
.insert(real_type
);
837 for (size_t i
= 1; i
< this->types_
.size(); i
++)
839 Type
* type
= this->types_
[i
];
841 && type
->named_type() == NULL
842 && real_for_named
.find(type
) == real_for_named
.end())
843 finalizer
.type(type
);
847 // Import a constant.
850 Import::import_const()
855 Named_constant::import_const(this, &name
, &type
, &expr
);
856 Typed_identifier
tid(name
, type
, this->location_
);
857 Named_object
* no
= this->package_
->add_constant(tid
, expr
);
858 if (this->add_to_globals_
)
859 this->gogo_
->add_dot_import_object(no
);
865 Import::import_type()
867 if (this->version_
>= EXPORT_FORMAT_V3
)
869 if (!this->stream_
->saw_error())
871 go_error_at(this->location_
,
872 "error in import data at %d: old type syntax",
873 this->stream_
->pos());
874 this->stream_
->set_saw_error();
880 Named_type::import_named_type(this, &type
);
882 // The named type has been added to the package by the type import
883 // process. Here we need to make it visible to the parser, and it
884 // to the global bindings if necessary.
885 type
->set_is_visible();
887 if (this->add_to_globals_
)
888 this->gogo_
->add_named_type(type
);
891 // Import a variable.
900 if (!Variable::import_var(this, &name
, &vpkg
, &is_exported
, &type
))
903 vpkg
= this->package_
;
905 name
= '.' + vpkg
->pkgpath() + '.' + name
;
906 Variable
* var
= new Variable(type
, NULL
, true, false, false,
909 no
= vpkg
->add_variable(name
, var
);
910 if (this->add_to_globals_
&& vpkg
== this->package_
)
911 this->gogo_
->add_dot_import_object(no
);
914 // Import a function into PACKAGE. PACKAGE is normally
915 // THIS->PACKAGE_, but it will be different for a method associated
916 // with a type defined in a different package.
919 Import::import_func(Package
* package
)
924 Typed_identifier
* receiver
;
925 Typed_identifier_list
* parameters
;
926 Typed_identifier_list
* results
;
929 std::string asm_name
;
931 if (!Function::import_func(this, &name
, &fpkg
, &is_exported
, &receiver
,
932 ¶meters
, &results
, &is_varargs
, &nointerface
,
938 name
= '.' + fpkg
->pkgpath() + '.' + name
;
939 Function_type
*fntype
= Type::make_function_type(receiver
, parameters
,
940 results
, this->location_
);
942 fntype
->set_is_varargs();
944 Location loc
= this->location_
;
946 if (fntype
->is_method())
948 Type
* rtype
= receiver
->type();
950 // We may still be reading the definition of RTYPE, so we have
951 // to be careful to avoid calling base or convert. If RTYPE is
952 // a named type or a forward declaration, then we know that it
953 // is not a pointer, because we are reading a method on RTYPE
954 // and named pointers can't have methods.
956 if (rtype
->classification() == Type::TYPE_POINTER
)
957 rtype
= rtype
->points_to();
959 if (rtype
->is_error_type())
961 else if (rtype
->named_type() != NULL
)
962 no
= rtype
->named_type()->add_method_declaration(name
, fpkg
, fntype
,
964 else if (rtype
->forward_declaration_type() != NULL
)
965 no
= rtype
->forward_declaration_type()->add_method_declaration(name
,
974 no
= fpkg
->add_function_declaration(name
, fntype
, loc
);
975 if (this->add_to_globals_
&& fpkg
== package
)
976 this->gogo_
->add_dot_import_object(no
);
980 no
->func_declaration_value()->set_nointerface();
981 if (!asm_name
.empty())
982 no
->func_declaration_value()->set_asm_name(asm_name
);
983 if (!body
.empty() && !no
->func_declaration_value()->has_imported_body())
984 no
->func_declaration_value()->set_imported_body(this, body
);
987 // Read a type definition and initialize the entry in this->types_.
988 // This parses the type definition saved by read_types earlier. This
989 // returns true on success, false on failure.
992 Import::parse_type(int i
)
994 go_assert(i
>= 0 && static_cast<size_t>(i
) < this->types_
.size());
995 go_assert(this->types_
[i
] == NULL
);
996 size_t offset
= this->type_offsets_
[i
].first
;
997 size_t len
= this->type_offsets_
[i
].second
;
999 Stream
* orig_stream
= this->stream_
;
1001 Stream_from_string_ref
stream(this->type_data_
, offset
, len
);
1002 stream
.set_pos(this->type_pos_
+ offset
);
1003 this->stream_
= &stream
;
1005 this->require_c_string("type ");
1006 std::string str
= this->read_identifier();
1008 if (!this->string_to_int(str
, false, &id
))
1010 this->stream_
= orig_stream
;
1015 go_error_at(this->location_
,
1016 ("error in import data at %d: "
1017 "type ID mismatch: got %d, want %d"),
1018 stream
.pos(), id
, i
);
1019 this->stream_
= orig_stream
;
1023 this->require_c_string(" ");
1024 if (stream
.peek_char() == '"')
1027 Type
* type
= this->read_named_type(i
);
1028 if (type
->is_error_type())
1030 this->stream_
= orig_stream
;
1036 Type
* type
= Type::import_type(this);
1037 if (type
->is_error_type())
1039 this->stream_
= orig_stream
;
1042 this->types_
[i
] = type
;
1044 this->require_c_string("\n");
1047 this->stream_
= orig_stream
;
1051 // Read a type in the import stream. This records the type by the
1052 // type index. If the type is named (which can only happen with older
1053 // export formats), it registers the name, but marks it as invisible.
1058 Stream
* stream
= this->stream_
;
1059 this->require_c_string("<type ");
1065 c
= stream
->get_char();
1066 if (c
!= '-' && (c
< '0' || c
> '9'))
1072 if (!this->string_to_int(number
, true, &index
))
1073 return Type::make_error_type();
1077 // A reference to a type defined earlier.
1079 return this->type_for_index(index
, "import data", stream
->pos(),
1083 if (this->version_
>= EXPORT_FORMAT_V3
)
1085 if (!stream
->saw_error())
1086 go_error_at(this->location_
,
1087 "error in import data at %d: expected %<>%>",
1089 stream
->set_saw_error();
1090 return Type::make_error_type();
1095 if (!stream
->saw_error())
1096 go_error_at(this->location_
,
1097 "error in import data at %d: expected %< %> or %<>%>",
1099 stream
->set_saw_error();
1101 return Type::make_error_type();
1105 || (static_cast<size_t>(index
) < this->types_
.size()
1106 && this->types_
[index
] != NULL
))
1108 go_error_at(this->location_
,
1109 "error in import data at %d: type index already defined",
1111 stream
->set_saw_error();
1112 return Type::make_error_type();
1115 if (static_cast<size_t>(index
) >= this->types_
.size())
1117 int newsize
= std::max(static_cast<size_t>(index
) + 1,
1118 this->types_
.size() * 2);
1119 this->types_
.resize(newsize
, NULL
);
1122 if (stream
->peek_char() != '"')
1124 Type
* type
= Type::import_type(this);
1125 this->require_c_string(">");
1126 this->types_
[index
] = type
;
1132 Type
* type
= this->read_named_type(index
);
1134 this->require_c_string(">");
1139 // Read a named type from the import stream and store it in
1140 // this->types_[index]. The stream should be positioned immediately
1141 // after the '"' that starts the name.
1144 Import::read_named_type(int index
)
1146 Stream
* stream
= this->stream_
;
1147 std::string type_name
;
1149 while ((c
= stream
->get_char()) != '"')
1152 // If this type is in the package we are currently importing, the
1153 // name will be .PKGPATH.NAME or simply NAME with no dots.
1154 // Otherwise, a non-hidden symbol will be PKGPATH.NAME and a hidden
1155 // symbol will be .PKGPATH.NAME.
1156 std::string pkgpath
;
1157 if (type_name
.find('.') != std::string::npos
)
1160 if (type_name
[0] == '.')
1162 size_t dot
= type_name
.rfind('.');
1163 pkgpath
= type_name
.substr(start
, dot
- start
);
1164 if (type_name
[0] != '.')
1165 type_name
.erase(0, dot
+ 1);
1168 this->require_c_string(" ");
1170 // The package name may follow. This is the name of the package in
1171 // the package clause of that package. The type name will include
1172 // the pkgpath, which may be different.
1173 std::string package_name
;
1174 if (stream
->peek_char() == '"')
1177 while ((c
= stream
->get_char()) != '"')
1179 this->require_c_string(" ");
1182 bool in_heap
= true;
1183 if (this->match_c_string("notinheap"))
1185 this->require_c_string("notinheap ");
1189 bool is_alias
= false;
1190 if (this->match_c_string("= "))
1196 // Declare the type in the appropriate package. If we haven't seen
1197 // it before, mark it as invisible. We declare it before we read
1198 // the actual definition of the type, since the definition may refer
1199 // to the type itself.
1201 if (pkgpath
.empty() || pkgpath
== this->gogo_
->pkgpath())
1202 package
= this->package_
;
1205 package
= this->gogo_
->register_package(pkgpath
, "",
1206 Linemap::unknown_location());
1207 if (!package_name
.empty())
1208 package
->set_package_name(package_name
, this->location());
1211 Named_object
* no
= package
->bindings()->lookup(type_name
);
1213 no
= package
->add_type_declaration(type_name
, this->location_
);
1214 else if (!no
->is_type_declaration() && !no
->is_type())
1216 go_error_at(this->location_
, "imported %<%s.%s%> both type and non-type",
1217 pkgpath
.c_str(), Gogo::message_name(type_name
).c_str());
1218 stream
->set_saw_error();
1219 return Type::make_error_type();
1222 go_assert(no
->package() == package
);
1224 if (this->types_
[index
] == NULL
)
1226 if (no
->is_type_declaration())
1228 // FIXME: It's silly to make a forward declaration every time.
1229 this->types_
[index
] = Type::make_forward_declaration(no
);
1233 go_assert(no
->is_type());
1234 this->types_
[index
] = no
->type_value();
1238 // If there is no type definition, then this is just a forward
1239 // declaration of a type defined in some other file.
1241 if (this->match_c_string(">") || this->match_c_string("\n"))
1243 type
= this->types_
[index
];
1245 go_error_at(this->location_
,
1246 ("import error at %d for type index %d: "
1247 "forward declaration marked notinheap"),
1248 this->pos(), index
);
1252 if (no
->is_type_declaration())
1254 // We can define the type now.
1256 type
= this->read_type();
1258 no
= package
->add_type(type_name
, type
, this->location_
);
1259 Named_type
* ntype
= no
->type_value();
1261 // This type has not yet been imported.
1262 ntype
->clear_is_visible();
1265 ntype
->set_not_in_heap();
1267 ntype
->set_is_alias();
1269 if (!type
->is_undefined() && type
->interface_type() != NULL
)
1270 this->gogo_
->record_interface_type(type
->interface_type());
1274 else if (no
->is_type())
1276 // We have seen this type before.
1277 type
= no
->type_value();
1279 // Don't change the visibility of the existing type.
1281 // For older export versions, we need to skip the type
1282 // definition in the stream.
1283 if (this->version_
< EXPORT_FORMAT_V3
)
1289 this->types_
[index
] = type
;
1291 // Read the type methods.
1292 if (this->match_c_string("\n"))
1295 while (this->match_c_string(" func"))
1298 this->import_func(package
);
1306 // Return the type given an index. Set *PARSED if we parsed it here.
1309 Import::type_for_index(int index
, const std::string
& input_name
,
1310 size_t input_offset
, bool* parsed
)
1313 if (index
>= 0 && !this->type_data_
.empty())
1315 if (static_cast<size_t>(index
) >= this->type_offsets_
.size())
1317 go_error_at(this->location_
,
1318 "error in %s at %lu: bad type index %d, max %d",
1320 static_cast<unsigned long>(input_offset
),
1321 index
, static_cast<int>(this->type_offsets_
.size()));
1322 return Type::make_error_type();
1325 if (this->types_
[index
] == NULL
)
1327 if (!this->parse_type(index
))
1328 return Type::make_error_type();
1334 ? (static_cast<size_t>(- index
) >= this->builtin_types_
.size()
1335 || this->builtin_types_
[- index
] == NULL
)
1336 : (static_cast<size_t>(index
) >= this->types_
.size()
1337 || this->types_
[index
] == NULL
))
1339 go_error_at(this->location_
,
1340 "error in %s at %lu: bad type index %d",
1342 static_cast<unsigned long>(input_offset
), index
);
1343 return Type::make_error_type();
1346 return index
< 0 ? this->builtin_types_
[- index
] : this->types_
[index
];
1349 // Read an escape note.
1352 Import::read_escape()
1354 if (this->match_c_string(" <esc:"))
1356 Stream
* stream
= this->stream_
;
1357 this->require_c_string(" <esc:");
1359 std::string escape
= "esc:";
1363 c
= stream
->get_char();
1364 if (c
!= 'x' && !ISXDIGIT(c
))
1371 go_error_at(this->location(),
1372 ("error in import data at %d: "
1373 "expect %< %> or %<>%>, got %c"),
1375 stream
->set_saw_error();
1377 escape
= Escape_note::make_tag(Node::ESCAPE_UNKNOWN
);
1382 return Escape_note::make_tag(Node::ESCAPE_UNKNOWN
);
1386 // Register the builtin types.
1389 Import::register_builtin_types(Gogo
* gogo
)
1391 this->register_builtin_type(gogo
, "int8", BUILTIN_INT8
);
1392 this->register_builtin_type(gogo
, "int16", BUILTIN_INT16
);
1393 this->register_builtin_type(gogo
, "int32", BUILTIN_INT32
);
1394 this->register_builtin_type(gogo
, "int64", BUILTIN_INT64
);
1395 this->register_builtin_type(gogo
, "uint8", BUILTIN_UINT8
);
1396 this->register_builtin_type(gogo
, "uint16", BUILTIN_UINT16
);
1397 this->register_builtin_type(gogo
, "uint32", BUILTIN_UINT32
);
1398 this->register_builtin_type(gogo
, "uint64", BUILTIN_UINT64
);
1399 this->register_builtin_type(gogo
, "float32", BUILTIN_FLOAT32
);
1400 this->register_builtin_type(gogo
, "float64", BUILTIN_FLOAT64
);
1401 this->register_builtin_type(gogo
, "complex64", BUILTIN_COMPLEX64
);
1402 this->register_builtin_type(gogo
, "complex128", BUILTIN_COMPLEX128
);
1403 this->register_builtin_type(gogo
, "int", BUILTIN_INT
);
1404 this->register_builtin_type(gogo
, "uint", BUILTIN_UINT
);
1405 this->register_builtin_type(gogo
, "uintptr", BUILTIN_UINTPTR
);
1406 this->register_builtin_type(gogo
, "bool", BUILTIN_BOOL
);
1407 this->register_builtin_type(gogo
, "string", BUILTIN_STRING
);
1408 this->register_builtin_type(gogo
, "error", BUILTIN_ERROR
);
1409 this->register_builtin_type(gogo
, "byte", BUILTIN_BYTE
);
1410 this->register_builtin_type(gogo
, "rune", BUILTIN_RUNE
);
1411 this->register_builtin_type(gogo
, "any", BUILTIN_ANY
);
1414 // Register a single builtin type.
1417 Import::register_builtin_type(Gogo
* gogo
, const char* name
, Builtin_code code
)
1419 Named_object
* named_object
= gogo
->lookup_global(name
);
1420 go_assert(named_object
!= NULL
&& named_object
->is_type());
1421 int index
= - static_cast<int>(code
);
1423 && static_cast<size_t>(index
) < this->builtin_types_
.size());
1424 this->builtin_types_
[index
] = named_object
->type_value();
1427 // Characters that stop read_identifier. We base this on the
1428 // characters that stop an identifier, without worrying about
1429 // characters that are permitted in an identifier. That lets us skip
1431 static const char * const identifier_stop
= " \n;:,()[]";
1433 // Read an identifier from the stream.
1436 Import::read_identifier()
1439 Stream
* stream
= this->stream_
;
1443 c
= stream
->peek_char();
1444 if (c
== -1 || strchr(identifier_stop
, c
) != NULL
)
1447 // FIXME: Probably we shouldn't accept '.', but that might break
1448 // some existing imports.
1449 if (c
== '.' && stream
->match_c_string("..."))
1458 // Read a possibly qualified identifier from IMP. The qualification
1459 // is <pID>, where ID is a package number. If the name has a leading
1460 // '.', it is not exported; otherwise, it is. Set *NAME, *PKG and
1461 // *IS_EXPORTED. Reports whether the read succeeded.
1464 Import::read_qualified_identifier(Import_expression
* imp
, std::string
* name
,
1465 Package
** pkg
, bool* is_exported
)
1468 if (imp
->match_c_string("<p"))
1472 char *pbuf
= &buf
[0];
1475 int next
= imp
->peek_char();
1476 if (next
== -1 || static_cast<size_t>(pbuf
- buf
) >= sizeof buf
- 1)
1483 *pbuf
= static_cast<char>(next
);
1490 long index
= strtol(buf
, &end
, 10);
1493 || static_cast<size_t>(index
) > imp
->max_package_index())
1496 *pkg
= imp
->package_at_index(index
);
1497 go_assert(*pkg
!= NULL
);
1500 *is_exported
= true;
1501 if (imp
->match_c_string("."))
1504 *is_exported
= false;
1507 *name
= imp
->read_identifier();
1509 return !name
->empty();
1512 // Read a name from the stream.
1517 std::string ret
= this->read_identifier();
1523 // Read LENGTH bytes from the stream.
1526 Import::read(size_t length
, std::string
* out
)
1529 if (!this->stream_
->peek(length
, &data
))
1531 if (!this->stream_
->saw_error())
1532 go_error_at(this->location_
, "import error at %d: expected %d bytes",
1533 this->stream_
->pos(), static_cast<int>(length
));
1534 this->stream_
->set_saw_error();
1535 *out
= std::string("");
1538 *out
= std::string(data
, length
);
1539 this->advance(length
);
1542 // Turn a string into a integer with appropriate error handling.
1545 Import::string_to_int(const std::string
&s
, bool is_neg_ok
, int* ret
)
1548 long prio
= strtol(s
.c_str(), &end
, 10);
1549 if (*end
!= '\0' || prio
> 0x7fffffff || (prio
< 0 && !is_neg_ok
))
1551 go_error_at(this->location_
, "invalid integer in import data at %d",
1552 this->stream_
->pos());
1553 this->stream_
->set_saw_error();
1560 // Class Import::Stream.
1562 Import::Stream::Stream()
1563 : pos_(0), saw_error_(false)
1567 Import::Stream::~Stream()
1571 // Return the next character to come from the stream.
1574 Import::Stream::peek_char()
1577 if (!this->do_peek(1, &read
))
1579 // Make sure we return an unsigned char, so that we don't get
1580 // confused by \xff.
1581 unsigned char ret
= *read
;
1585 // Return true if the next LENGTH characters from the stream match
1589 Import::Stream::match_bytes(const char* bytes
, size_t length
)
1592 if (!this->do_peek(length
, &read
))
1594 return memcmp(bytes
, read
, length
) == 0;
1597 // Require that the next LENGTH bytes from the stream match BYTES.
1600 Import::Stream::require_bytes(Location location
, const char* bytes
,
1604 if (!this->do_peek(length
, &read
)
1605 || memcmp(bytes
, read
, length
) != 0)
1607 if (!this->saw_error_
)
1608 go_error_at(location
, "import error at %d: expected %<%.*s%>",
1609 this->pos(), static_cast<int>(length
), bytes
);
1610 this->saw_error_
= true;
1613 this->advance(length
);
1616 // Class Stream_from_file.
1618 Stream_from_file::Stream_from_file(int fd
)
1621 if (lseek(fd
, 0, SEEK_SET
) != 0)
1623 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1624 this->set_saw_error();
1628 Stream_from_file::~Stream_from_file()
1636 Stream_from_file::do_peek(size_t length
, const char** bytes
)
1638 if (this->data_
.length() >= length
)
1640 *bytes
= this->data_
.data();
1644 this->data_
.resize(length
);
1645 ssize_t got
= ::read(this->fd_
, &this->data_
[0], length
);
1649 if (!this->saw_error())
1650 go_fatal_error(Linemap::unknown_location(), "read failed: %m");
1651 this->set_saw_error();
1655 if (lseek(this->fd_
, - got
, SEEK_CUR
) < 0)
1657 if (!this->saw_error())
1658 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1659 this->set_saw_error();
1663 if (static_cast<size_t>(got
) < length
)
1666 *bytes
= this->data_
.data();
1673 Stream_from_file::do_advance(size_t skip
)
1675 if (lseek(this->fd_
, skip
, SEEK_CUR
) < 0)
1677 if (!this->saw_error())
1678 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1679 this->set_saw_error();
1681 if (!this->data_
.empty())
1683 if (this->data_
.length() > skip
)
1684 this->data_
.erase(0, skip
);
1686 this->data_
.clear();
1690 // Class Import_function_body.
1692 Import_function_body::Import_function_body(Gogo
* gogo
,
1694 Named_object
* named_object
,
1695 const std::string
& body
,
1699 : gogo_(gogo
), imp_(imp
), named_object_(named_object
), body_(body
),
1700 off_(off
), indent_(indent
), temporaries_(), labels_(),
1703 this->blocks_
.push_back(block
);
1706 Import_function_body::~Import_function_body()
1708 // At this point we should be left with the original outer block only.
1709 go_assert(saw_errors() || this->blocks_
.size() == 1);
1712 // The name of the function we are parsing.
1715 Import_function_body::name() const
1717 return this->named_object_
->name();
1720 // Class Import_function_body.
1722 // Require that the next bytes match STR, issuing an error if not.
1723 // Advance past the string.
1726 Import_function_body::require_c_string(const char* str
)
1728 if (!this->match_c_string(str
))
1730 if (!this->saw_error_
)
1731 go_error_at(this->location(),
1732 "invalid export data for %qs: expected %qs at %lu",
1733 this->name().c_str(), str
,
1734 static_cast<unsigned long>(this->off_
));
1735 this->saw_error_
= true;
1738 this->advance(strlen(str
));
1741 // Read an identifier.
1744 Import_function_body::read_identifier()
1746 size_t start
= this->off_
;
1747 for (size_t i
= start
; i
< this->body_
.length(); i
++)
1749 int c
= static_cast<unsigned char>(this->body_
[i
]);
1750 if (strchr(identifier_stop
, c
) != NULL
)
1753 return this->body_
.substr(start
, i
- start
);
1756 // FIXME: Probably we shouldn't accept '.', but that might break
1757 // some existing imports.
1759 && i
+ 2 < this->body_
.length()
1760 && this->body_
[i
+ 1] == '.'
1761 && this->body_
[i
+ 2] == '.')
1764 return this->body_
.substr(start
, i
- start
);
1767 this->off_
= this->body_
.length();
1768 return this->body_
.substr(start
);
1774 Import_function_body::read_type()
1776 this->require_c_string("<type ");
1777 size_t start
= this->off_
;
1780 for (i
= start
; i
< this->body_
.length(); ++i
)
1782 c
= static_cast<unsigned char>(this->body_
[i
]);
1783 if (c
!= '-' && (c
< '0' || c
> '9'))
1789 std::string num
= this->body_
.substr(start
, i
- start
);
1790 long val
= strtol(num
.c_str(), &end
, 10);
1791 if (*end
!= '\0' || val
> 0x7fffffff)
1793 if (!this->saw_error_
)
1794 go_error_at(this->location(),
1795 "invalid export data for %qs: expected integer at %lu",
1796 this->name().c_str(),
1797 static_cast<unsigned long>(start
));
1798 this->saw_error_
= true;
1799 return Type::make_error_type();
1804 if (!this->saw_error_
)
1805 go_error_at(this->location(),
1806 "invalid export data for %qs: expected %<>%> at %lu",
1807 this->name().c_str(),
1808 static_cast<unsigned long>(i
));
1809 this->saw_error_
= true;
1810 return Type::make_error_type();
1814 Type
* type
= this->imp_
->type_for_index(static_cast<int>(val
), this->name(),
1815 static_cast<unsigned long>(start
),
1818 // If we just read this type's information, its methods will not
1819 // have been finalized. Do that now.
1821 this->gogo_
->finalize_methods_for_type(type
);
1826 // Return the next size to use for a vector mapping indexes to values.
1829 Import_function_body::next_size(size_t have
)
1833 else if (have
< 256)
1839 // Record the index of a temporary statement.
1842 Import_function_body::record_temporary(Temporary_statement
* temp
,
1845 size_t have
= this->temporaries_
.size();
1846 while (static_cast<size_t>(idx
) >= have
)
1848 size_t want
= Import_function_body::next_size(have
);
1849 this->temporaries_
.resize(want
, NULL
);
1852 this->temporaries_
[idx
] = temp
;
1855 // Return a temporary statement given an index.
1857 Temporary_statement
*
1858 Import_function_body::temporary_statement(unsigned int idx
)
1860 if (static_cast<size_t>(idx
) >= this->temporaries_
.size())
1862 return this->temporaries_
[idx
];
1865 // Return an unnamed label given an index, defining the label if we
1866 // haven't seen it already.
1869 Import_function_body::unnamed_label(unsigned int idx
, Location loc
)
1871 size_t have
= this->labels_
.size();
1872 while (static_cast<size_t>(idx
) >= have
)
1874 size_t want
= Import_function_body::next_size(have
);
1875 this->labels_
.resize(want
, NULL
);
1878 Unnamed_label
* label
= this->labels_
[idx
];
1881 label
= new Unnamed_label(loc
);
1882 this->labels_
[idx
] = label
;