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 return new Stream_from_file(fd
);
248 // See if we can read this as an archive.
249 if (Import::is_archive_magic(buf
))
250 return Import::find_archive_export_data(filename
, fd
, location
);
255 // Look for export data in an object file.
258 Import::find_object_export_data(const std::string
& filename
,
266 const char *errmsg
= go_read_export_data(fd
, offset
, &buf
, &len
, &err
);
270 go_error_at(location
, "%s: %s", filename
.c_str(), errmsg
);
272 go_error_at(location
, "%s: %s: %s", filename
.c_str(), errmsg
,
280 return new Stream_from_buffer(buf
, len
);
285 // Construct an Import object. We make the builtin_types_ vector
286 // large enough to hold all the builtin types.
288 Import::Import(Stream
* stream
, Location location
)
289 : gogo_(NULL
), stream_(stream
), location_(location
), package_(NULL
),
290 add_to_globals_(false),
291 builtin_types_((- SMALLEST_BUILTIN_CODE
) + 1),
292 types_(), version_(EXPORT_FORMAT_UNKNOWN
)
296 // Import the data in the associated stream.
299 Import::import(Gogo
* gogo
, const std::string
& local_name
,
300 bool is_local_name_exported
)
302 // Hold on to the Gogo structure. Otherwise we need to pass it
303 // through all the import functions, because we need it when reading
307 // A stream of export data can include data from more than one input
308 // file. Here we loop over each input file.
309 Stream
* stream
= this->stream_
;
310 while (!stream
->at_eof() && !stream
->saw_error())
312 // The vector of types is package specific.
313 this->types_
.clear();
315 // Check magic string / version number.
316 if (stream
->match_bytes(Export::cur_magic
, Export::magic_len
))
318 stream
->require_bytes(this->location_
, Export::cur_magic
,
320 this->version_
= EXPORT_FORMAT_CURRENT
;
322 else if (stream
->match_bytes(Export::v1_magic
, Export::magic_len
))
324 stream
->require_bytes(this->location_
, Export::v1_magic
,
326 this->version_
= EXPORT_FORMAT_V1
;
330 go_error_at(this->location_
,
331 ("error in import data at %d: invalid magic string"),
336 this->require_c_string("package ");
337 std::string package_name
= this->read_identifier();
338 this->require_c_string(";\n");
341 std::string pkgpath_symbol
;
342 if (this->match_c_string("prefix "))
345 std::string unique_prefix
= this->read_identifier();
346 this->require_c_string(";\n");
347 pkgpath
= unique_prefix
+ '.' + package_name
;
348 pkgpath_symbol
= (Gogo::pkgpath_for_symbol(unique_prefix
) + '.'
349 + Gogo::pkgpath_for_symbol(package_name
));
353 this->require_c_string("pkgpath ");
354 pkgpath
= this->read_identifier();
355 this->require_c_string(";\n");
356 pkgpath_symbol
= Gogo::pkgpath_for_symbol(pkgpath
);
359 this->package_
= gogo
->add_imported_package(package_name
, local_name
,
360 is_local_name_exported
,
361 pkgpath
, pkgpath_symbol
,
363 &this->add_to_globals_
);
364 if (this->package_
== NULL
)
366 stream
->set_saw_error();
370 // Read and discard priority if older V1 export data format.
371 if (version() == EXPORT_FORMAT_V1
)
373 this->require_c_string("priority ");
374 std::string priority_string
= this->read_identifier();
376 if (!this->string_to_int(priority_string
, false, &prio
))
378 this->require_c_string(";\n");
381 while (stream
->match_c_string("package"))
382 this->read_one_package();
384 while (stream
->match_c_string("import"))
385 this->read_one_import();
387 if (stream
->match_c_string("init"))
388 this->read_import_init_fns(gogo
);
390 // Loop over all the input data for this package.
391 while (!stream
->saw_error())
393 if (stream
->match_c_string("const "))
394 this->import_const();
395 else if (stream
->match_c_string("type "))
397 else if (stream
->match_c_string("var "))
399 else if (stream
->match_c_string("func "))
400 this->import_func(this->package_
);
401 else if (stream
->match_c_string("checksum "))
405 go_error_at(this->location_
,
406 ("error in import data at %d: "
407 "expected %<const%>, %<type%>, %<var%>, "
408 "%<func%>, or %<checksum%>"),
410 stream
->set_saw_error();
415 // We currently ignore the checksum. In the future we could
416 // store the checksum somewhere in the generated object and then
417 // verify that the checksum matches at link time or at dynamic
419 this->require_c_string("checksum ");
420 stream
->advance(Export::checksum_len
* 2);
421 this->require_c_string(";\n");
424 return this->package_
;
427 // Read a package line. This let us reliably determine the pkgpath
428 // symbol, even if the package was compiled with a -fgo-prefix option.
431 Import::read_one_package()
433 this->require_c_string("package ");
434 std::string package_name
= this->read_identifier();
435 this->require_c_string(" ");
436 std::string pkgpath
= this->read_identifier();
437 this->require_c_string(" ");
438 std::string pkgpath_symbol
= this->read_identifier();
439 this->require_c_string(";\n");
441 Package
* p
= this->gogo_
->register_package(pkgpath
, pkgpath_symbol
,
442 Linemap::unknown_location());
443 p
->set_package_name(package_name
, this->location());
446 // Read an import line. We don't actually care about these.
449 Import::read_one_import()
451 this->require_c_string("import ");
452 std::string package_name
= this->read_identifier();
453 this->require_c_string(" ");
454 std::string pkgpath
= this->read_identifier();
455 this->require_c_string(" \"");
456 Stream
* stream
= this->stream_
;
457 while (stream
->peek_char() != '"')
459 this->require_c_string("\";\n");
461 Package
* p
= this->gogo_
->register_package(pkgpath
, "",
462 Linemap::unknown_location());
463 p
->set_package_name(package_name
, this->location());
466 // Read the list of import control functions and/or init graph.
469 Import::read_import_init_fns(Gogo
* gogo
)
471 this->require_c_string("init");
473 // Maps init function to index in the "init" clause; needed
474 // to read the init_graph section.
475 std::map
<std::string
, unsigned> init_idx
;
477 while (!this->match_c_string(";"))
481 this->require_c_string(" ");
482 std::string package_name
= this->read_identifier();
483 this->require_c_string(" ");
484 std::string init_name
= this->read_identifier();
485 if (this->version_
== EXPORT_FORMAT_V1
)
487 // Older version 1 init fcn export data format is:
489 // <packname> <fcn> <priority>
490 this->require_c_string(" ");
491 std::string prio_string
= this->read_identifier();
492 if (!this->string_to_int(prio_string
, false, &priority
))
495 gogo
->add_import_init_fn(package_name
, init_name
, priority
);
497 // Record the index of this init fcn so that we can look it
498 // up by index in the subsequent init_graph section.
499 unsigned idx
= init_idx
.size();
500 init_idx
[init_name
] = idx
;
502 this->require_c_string(";\n");
504 if (this->match_c_string("init_graph"))
506 this->require_c_string("init_graph");
508 // Build a vector mapping init fcn slot to Import_init pointer.
509 go_assert(init_idx
.size() > 0);
510 std::vector
<Import_init
*> import_initvec
;
511 import_initvec
.resize(init_idx
.size());
512 for (std::map
<std::string
, unsigned>::const_iterator it
=
514 it
!= init_idx
.end(); ++it
)
516 const std::string
& init_name
= it
->first
;
517 Import_init
* ii
= gogo
->lookup_init(init_name
);
518 import_initvec
[it
->second
] = ii
;
521 // Init graph format is:
523 // init_graph <src1> <sink1> <src2> <sink2> ... ;
525 // where src + sink are init functions indices.
527 while (!this->match_c_string(";"))
529 this->require_c_string(" ");
530 std::string src_string
= this->read_identifier();
532 if (!this->string_to_unsigned(src_string
, &src
)) return;
534 this->require_c_string(" ");
535 std::string sink_string
= this->read_identifier();
537 if (!this->string_to_unsigned(sink_string
, &sink
)) return;
539 go_assert(src
< import_initvec
.size());
540 Import_init
* ii_src
= import_initvec
[src
];
541 go_assert(sink
< import_initvec
.size());
542 Import_init
* ii_sink
= import_initvec
[sink
];
544 ii_src
->record_precursor_fcn(ii_sink
->init_name());
546 this->require_c_string(";\n");
550 // Import a constant.
553 Import::import_const()
558 Named_constant::import_const(this, &name
, &type
, &expr
);
559 Typed_identifier
tid(name
, type
, this->location_
);
560 Named_object
* no
= this->package_
->add_constant(tid
, expr
);
561 if (this->add_to_globals_
)
562 this->gogo_
->add_dot_import_object(no
);
568 Import::import_type()
571 Named_type::import_named_type(this, &type
);
573 // The named type has been added to the package by the type import
574 // process. Here we need to make it visible to the parser, and it
575 // to the global bindings if necessary.
576 type
->set_is_visible();
578 if (this->add_to_globals_
)
579 this->gogo_
->add_named_type(type
);
582 // Import a variable.
589 Variable::import_var(this, &name
, &type
);
590 Variable
* var
= new Variable(type
, NULL
, true, false, false,
593 no
= this->package_
->add_variable(name
, var
);
594 if (this->add_to_globals_
)
595 this->gogo_
->add_dot_import_object(no
);
598 // Import a function into PACKAGE. PACKAGE is normally
599 // THIS->PACKAGE_, but it will be different for a method associated
600 // with a type defined in a different package.
603 Import::import_func(Package
* package
)
606 Typed_identifier
* receiver
;
607 Typed_identifier_list
* parameters
;
608 Typed_identifier_list
* results
;
611 Function::import_func(this, &name
, &receiver
,
612 ¶meters
, &results
, &is_varargs
, &nointerface
);
613 Function_type
*fntype
= Type::make_function_type(receiver
, parameters
,
614 results
, this->location_
);
616 fntype
->set_is_varargs();
618 Location loc
= this->location_
;
620 if (fntype
->is_method())
622 Type
* rtype
= receiver
->type();
624 // We may still be reading the definition of RTYPE, so we have
625 // to be careful to avoid calling base or convert. If RTYPE is
626 // a named type or a forward declaration, then we know that it
627 // is not a pointer, because we are reading a method on RTYPE
628 // and named pointers can't have methods.
630 if (rtype
->classification() == Type::TYPE_POINTER
)
631 rtype
= rtype
->points_to();
633 if (rtype
->is_error_type())
635 else if (rtype
->named_type() != NULL
)
636 no
= rtype
->named_type()->add_method_declaration(name
, package
, fntype
,
638 else if (rtype
->forward_declaration_type() != NULL
)
639 no
= rtype
->forward_declaration_type()->add_method_declaration(name
,
648 no
= package
->add_function_declaration(name
, fntype
, loc
);
649 if (this->add_to_globals_
)
650 this->gogo_
->add_dot_import_object(no
);
654 no
->func_declaration_value()->set_nointerface();
659 // Read a type in the import stream. This records the type by the
660 // type index. If the type is named, it registers the name, but marks
666 Stream
* stream
= this->stream_
;
667 this->require_c_string("<type ");
673 c
= stream
->get_char();
674 if (c
!= '-' && (c
< '0' || c
> '9'))
680 if (!this->string_to_int(number
, true, &index
))
681 return Type::make_error_type();
685 // This type was already defined.
687 ? (static_cast<size_t>(- index
) >= this->builtin_types_
.size()
688 || this->builtin_types_
[- index
] == NULL
)
689 : (static_cast<size_t>(index
) >= this->types_
.size()
690 || this->types_
[index
] == NULL
))
692 go_error_at(this->location_
,
693 "error in import data at %d: bad type index %d",
694 stream
->pos(), index
);
695 stream
->set_saw_error();
696 return Type::make_error_type();
699 return index
< 0 ? this->builtin_types_
[- index
] : this->types_
[index
];
704 if (!stream
->saw_error())
705 go_error_at(this->location_
,
706 "error in import data at %d: expect %< %> or %<>%>'",
708 stream
->set_saw_error();
710 return Type::make_error_type();
714 || (static_cast<size_t>(index
) < this->types_
.size()
715 && this->types_
[index
] != NULL
))
717 go_error_at(this->location_
,
718 "error in import data at %d: type index already defined",
720 stream
->set_saw_error();
721 return Type::make_error_type();
724 if (static_cast<size_t>(index
) >= this->types_
.size())
726 int newsize
= std::max(static_cast<size_t>(index
) + 1,
727 this->types_
.size() * 2);
728 this->types_
.resize(newsize
, NULL
);
731 if (stream
->peek_char() != '"')
733 Type
* type
= Type::import_type(this);
734 this->require_c_string(">");
735 this->types_
[index
] = type
;
739 // This type has a name.
742 std::string type_name
;
743 while ((c
= stream
->get_char()) != '"')
746 // If this type is in the package we are currently importing, the
747 // name will be .PKGPATH.NAME or simply NAME with no dots.
748 // Otherwise, a non-hidden symbol will be PKGPATH.NAME and a hidden
749 // symbol will be .PKGPATH.NAME.
751 if (type_name
.find('.') != std::string::npos
)
754 if (type_name
[0] == '.')
756 size_t dot
= type_name
.rfind('.');
757 pkgpath
= type_name
.substr(start
, dot
- start
);
758 if (type_name
[0] != '.')
759 type_name
.erase(0, dot
+ 1);
762 this->require_c_string(" ");
764 // The package name may follow. This is the name of the package in
765 // the package clause of that package. The type name will include
766 // the pkgpath, which may be different.
767 std::string package_name
;
768 if (stream
->peek_char() == '"')
771 while ((c
= stream
->get_char()) != '"')
773 this->require_c_string(" ");
776 bool is_alias
= false;
777 if (this->match_c_string("= "))
783 // Declare the type in the appropriate package. If we haven't seen
784 // it before, mark it as invisible. We declare it before we read
785 // the actual definition of the type, since the definition may refer
786 // to the type itself.
788 if (pkgpath
.empty() || pkgpath
== this->gogo_
->pkgpath())
789 package
= this->package_
;
792 package
= this->gogo_
->register_package(pkgpath
, "",
793 Linemap::unknown_location());
794 if (!package_name
.empty())
795 package
->set_package_name(package_name
, this->location());
798 Named_object
* no
= package
->bindings()->lookup(type_name
);
800 no
= package
->add_type_declaration(type_name
, this->location_
);
801 else if (!no
->is_type_declaration() && !no
->is_type())
803 go_error_at(this->location_
, "imported %<%s.%s%> both type and non-type",
804 pkgpath
.c_str(), Gogo::message_name(type_name
).c_str());
805 stream
->set_saw_error();
806 return Type::make_error_type();
809 go_assert(no
->package() == package
);
811 if (this->types_
[index
] == NULL
)
813 if (no
->is_type_declaration())
815 // FIXME: It's silly to make a forward declaration every time.
816 this->types_
[index
] = Type::make_forward_declaration(no
);
820 go_assert(no
->is_type());
821 this->types_
[index
] = no
->type_value();
825 // If there is no type definition, then this is just a forward
826 // declaration of a type defined in some other file.
828 if (this->match_c_string(">"))
829 type
= this->types_
[index
];
832 type
= this->read_type();
834 if (no
->is_type_declaration())
836 // We can define the type now.
838 no
= package
->add_type(type_name
, type
, this->location_
);
839 Named_type
* ntype
= no
->type_value();
841 // This type has not yet been imported.
842 ntype
->clear_is_visible();
845 ntype
->set_is_alias();
847 if (!type
->is_undefined() && type
->interface_type() != NULL
)
848 this->gogo_
->record_interface_type(type
->interface_type());
852 else if (no
->is_type())
854 // We have seen this type before. FIXME: it would be a good
855 // idea to check that the two imported types are identical,
856 // but we have not finalized the methods yet, which means
857 // that we can not reliably compare interface types.
858 type
= no
->type_value();
860 // Don't change the visibility of the existing type.
863 this->types_
[index
] = type
;
865 // Read the type methods.
866 if (this->match_c_string("\n"))
869 while (this->match_c_string(" func"))
872 this->import_func(package
);
877 this->require_c_string(">");
882 // Read an escape note.
885 Import::read_escape()
887 if (this->match_c_string(" <esc:"))
889 Stream
* stream
= this->stream_
;
890 this->require_c_string(" <esc:");
892 std::string escape
= "esc:";
896 c
= stream
->get_char();
897 if (c
!= 'x' && !ISXDIGIT(c
))
904 go_error_at(this->location(),
905 ("error in import data at %d: "
906 "expect %< %> or %<>%>, got %c"),
908 stream
->set_saw_error();
910 escape
= Escape_note::make_tag(Node::ESCAPE_UNKNOWN
);
915 return Escape_note::make_tag(Node::ESCAPE_UNKNOWN
);
919 // Register the builtin types.
922 Import::register_builtin_types(Gogo
* gogo
)
924 this->register_builtin_type(gogo
, "int8", BUILTIN_INT8
);
925 this->register_builtin_type(gogo
, "int16", BUILTIN_INT16
);
926 this->register_builtin_type(gogo
, "int32", BUILTIN_INT32
);
927 this->register_builtin_type(gogo
, "int64", BUILTIN_INT64
);
928 this->register_builtin_type(gogo
, "uint8", BUILTIN_UINT8
);
929 this->register_builtin_type(gogo
, "uint16", BUILTIN_UINT16
);
930 this->register_builtin_type(gogo
, "uint32", BUILTIN_UINT32
);
931 this->register_builtin_type(gogo
, "uint64", BUILTIN_UINT64
);
932 this->register_builtin_type(gogo
, "float32", BUILTIN_FLOAT32
);
933 this->register_builtin_type(gogo
, "float64", BUILTIN_FLOAT64
);
934 this->register_builtin_type(gogo
, "complex64", BUILTIN_COMPLEX64
);
935 this->register_builtin_type(gogo
, "complex128", BUILTIN_COMPLEX128
);
936 this->register_builtin_type(gogo
, "int", BUILTIN_INT
);
937 this->register_builtin_type(gogo
, "uint", BUILTIN_UINT
);
938 this->register_builtin_type(gogo
, "uintptr", BUILTIN_UINTPTR
);
939 this->register_builtin_type(gogo
, "bool", BUILTIN_BOOL
);
940 this->register_builtin_type(gogo
, "string", BUILTIN_STRING
);
941 this->register_builtin_type(gogo
, "error", BUILTIN_ERROR
);
942 this->register_builtin_type(gogo
, "byte", BUILTIN_BYTE
);
943 this->register_builtin_type(gogo
, "rune", BUILTIN_RUNE
);
946 // Register a single builtin type.
949 Import::register_builtin_type(Gogo
* gogo
, const char* name
, Builtin_code code
)
951 Named_object
* named_object
= gogo
->lookup_global(name
);
952 go_assert(named_object
!= NULL
&& named_object
->is_type());
953 int index
= - static_cast<int>(code
);
955 && static_cast<size_t>(index
) < this->builtin_types_
.size());
956 this->builtin_types_
[index
] = named_object
->type_value();
959 // Read an identifier from the stream.
962 Import::read_identifier()
965 Stream
* stream
= this->stream_
;
969 c
= stream
->peek_char();
970 if (c
== -1 || c
== ' ' || c
== ';')
978 // Read a name from the stream.
983 std::string ret
= this->read_identifier();
986 else if (!Lex::is_exported_name(ret
))
987 ret
= '.' + this->package_
->pkgpath() + '.' + ret
;
991 // Turn a string into a integer with appropriate error handling.
994 Import::string_to_int(const std::string
&s
, bool is_neg_ok
, int* ret
)
997 long prio
= strtol(s
.c_str(), &end
, 10);
998 if (*end
!= '\0' || prio
> 0x7fffffff || (prio
< 0 && !is_neg_ok
))
1000 go_error_at(this->location_
, "invalid integer in import data at %d",
1001 this->stream_
->pos());
1002 this->stream_
->set_saw_error();
1009 // Class Import::Stream.
1011 Import::Stream::Stream()
1012 : pos_(0), saw_error_(false)
1016 Import::Stream::~Stream()
1020 // Return the next character to come from the stream.
1023 Import::Stream::peek_char()
1026 if (!this->do_peek(1, &read
))
1028 // Make sure we return an unsigned char, so that we don't get
1029 // confused by \xff.
1030 unsigned char ret
= *read
;
1034 // Return true if the next LENGTH characters from the stream match
1038 Import::Stream::match_bytes(const char* bytes
, size_t length
)
1041 if (!this->do_peek(length
, &read
))
1043 return memcmp(bytes
, read
, length
) == 0;
1046 // Require that the next LENGTH bytes from the stream match BYTES.
1049 Import::Stream::require_bytes(Location location
, const char* bytes
,
1053 if (!this->do_peek(length
, &read
)
1054 || memcmp(bytes
, read
, length
) != 0)
1056 if (!this->saw_error_
)
1057 go_error_at(location
, "import error at %d: expected %<%.*s%>",
1058 this->pos(), static_cast<int>(length
), bytes
);
1059 this->saw_error_
= true;
1062 this->advance(length
);
1065 // Class Stream_from_file.
1067 Stream_from_file::Stream_from_file(int fd
)
1070 if (lseek(fd
, 0, SEEK_SET
) != 0)
1072 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1073 this->set_saw_error();
1077 Stream_from_file::~Stream_from_file()
1085 Stream_from_file::do_peek(size_t length
, const char** bytes
)
1087 if (this->data_
.length() <= length
)
1089 *bytes
= this->data_
.data();
1092 // Don't bother to handle the general case, since we don't need it.
1093 go_assert(length
< 64);
1095 ssize_t got
= read(this->fd_
, buf
, length
);
1099 if (!this->saw_error())
1100 go_fatal_error(Linemap::unknown_location(), "read failed: %m");
1101 this->set_saw_error();
1105 if (lseek(this->fd_
, - got
, SEEK_CUR
) != 0)
1107 if (!this->saw_error())
1108 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1109 this->set_saw_error();
1113 if (static_cast<size_t>(got
) < length
)
1116 this->data_
.assign(buf
, got
);
1118 *bytes
= this->data_
.data();
1125 Stream_from_file::do_advance(size_t skip
)
1127 if (lseek(this->fd_
, skip
, SEEK_CUR
) != 0)
1129 if (!this->saw_error())
1130 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1131 this->set_saw_error();
1133 if (!this->data_
.empty())
1135 if (this->data_
.length() < skip
)
1136 this->data_
.erase(0, skip
);
1138 this->data_
.clear();