OSDN Git Service

compiler: Fix parse of (<- chan <- chan <- int)(x).
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / import.cc
1 // import.cc -- Go frontend import declarations.
2
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.
6
7 #include "go-system.h"
8
9 #include "filenames.h"
10 #include "simple-object.h"
11
12 #include "go-c.h"
13 #include "gogo.h"
14 #include "lex.h"
15 #include "types.h"
16 #include "export.h"
17 #include "import.h"
18
19 #ifndef O_BINARY
20 #define O_BINARY 0
21 #endif
22
23 // The list of paths we search for import files.
24
25 static std::vector<std::string> search_path;
26
27 // Add a directory to the search path.  This is called from the option
28 // handling language hook.
29
30 GO_EXTERN_C
31 void
32 go_add_search_path(const char* path)
33 {
34   search_path.push_back(std::string(path));
35 }
36
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.
40
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.
43
44 // When FILENAME does not exist, we try modifying FILENAME to find the
45 // file.  We use the first of these which exists:
46 //   * We append ".gox".
47 //   * We turn the base of FILENAME into libFILENAME.so.
48 //   * We turn the base of FILENAME into libFILENAME.a.
49 //   * We append ".o".
50
51 // When using a search path, we apply each of these transformations at
52 // each entry on the search path before moving on to the next entry.
53 // If the file exists, but does not contain any Go export data, we
54 // stop; we do not keep looking for another file with the same name
55 // later in the search path.
56
57 Import::Stream*
58 Import::open_package(const std::string& filename, Location location)
59 {
60   bool is_local;
61   if (IS_ABSOLUTE_PATH(filename))
62     is_local = true;
63   else if (filename[0] == '.' && IS_DIR_SEPARATOR(filename[1]))
64     is_local = true;
65   else if (filename[0] == '.'
66            && filename[1] == '.'
67            && IS_DIR_SEPARATOR(filename[2]))
68     is_local = true;
69   else
70     is_local = false;
71   if (!is_local)
72     {
73       for (std::vector<std::string>::const_iterator p = search_path.begin();
74            p != search_path.end();
75            ++p)
76         {
77           std::string indir = *p;
78           if (!indir.empty() && indir[indir.size() - 1] != '/')
79             indir += '/';
80           indir += filename;
81           Stream* s = Import::try_package_in_directory(indir, location);
82           if (s != NULL)
83             return s;
84         }
85     }
86
87   Stream* s = Import::try_package_in_directory(filename, location);
88   if (s != NULL)
89     return s;
90
91   return NULL;
92 }
93
94 // Try to find the export data for FILENAME.
95
96 Import::Stream*
97 Import::try_package_in_directory(const std::string& filename,
98                                  Location location)
99 {
100   std::string found_filename = filename;
101   int fd = open(found_filename.c_str(), O_RDONLY | O_BINARY);
102
103   if (fd >= 0)
104     {
105       struct stat s;
106       if (fstat(fd, &s) >= 0 && S_ISDIR(s.st_mode))
107         {
108           close(fd);
109           fd = -1;
110           errno = EISDIR;
111         }
112     }
113
114   if (fd < 0)
115     {
116       if (errno != ENOENT && errno != EISDIR)
117         warning_at(location, 0, "%s: %m", filename.c_str());
118
119       fd = Import::try_suffixes(&found_filename);
120       if (fd < 0)
121         return NULL;
122     }
123
124   // The export data may not be in this file.
125   Stream* s = Import::find_export_data(found_filename, fd, location);
126   if (s != NULL)
127     return s;
128
129   close(fd);
130
131   error_at(location, "%s exists but does not contain any Go export data",
132            found_filename.c_str());
133
134   return NULL;
135 }
136
137 // Given import "*PFILENAME", where *PFILENAME does not exist, try
138 // various suffixes.  If we find one, set *PFILENAME to the one we
139 // found.  Return the open file descriptor.
140
141 int
142 Import::try_suffixes(std::string* pfilename)
143 {
144   std::string filename = *pfilename + ".gox";
145   int fd = open(filename.c_str(), O_RDONLY | O_BINARY);
146   if (fd >= 0)
147     {
148       *pfilename = filename;
149       return fd;
150     }
151
152   const char* basename = lbasename(pfilename->c_str());
153   size_t basename_pos = basename - pfilename->c_str();
154   filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".so";
155   fd = open(filename.c_str(), O_RDONLY | O_BINARY);
156   if (fd >= 0)
157     {
158       *pfilename = filename;
159       return fd;
160     }
161
162   filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".a";
163   fd = open(filename.c_str(), O_RDONLY | O_BINARY);
164   if (fd >= 0)
165     {
166       *pfilename = filename;
167       return fd;
168     }
169
170   filename = *pfilename + ".o";
171   fd = open(filename.c_str(), O_RDONLY | O_BINARY);
172   if (fd >= 0)
173     {
174       *pfilename = filename;
175       return fd;
176     }
177
178   return -1;
179 }
180
181 // Look for export data in the file descriptor FD.
182
183 Import::Stream*
184 Import::find_export_data(const std::string& filename, int fd,
185                          Location location)
186 {
187   // See if we can read this as an object file.
188   Import::Stream* stream = Import::find_object_export_data(filename, fd, 0,
189                                                            location);
190   if (stream != NULL)
191     return stream;
192
193   const int len = MAX(Export::v1_magic_len, Import::archive_magic_len);
194
195   if (lseek(fd, 0, SEEK_SET) < 0)
196     {
197       error_at(location, "lseek %s failed: %m", filename.c_str());
198       return NULL;
199     }
200
201   char buf[len];
202   ssize_t c = read(fd, buf, len);
203   if (c < len)
204     return NULL;
205
206   // Check for a file containing nothing but Go export data.
207   if (memcmp(buf, Export::v1_magic, Export::v1_magic_len) == 0)
208     return new Stream_from_file(fd);
209
210   // See if we can read this as an archive.
211   if (Import::is_archive_magic(buf))
212     return Import::find_archive_export_data(filename, fd, location);
213
214   return NULL;
215 }
216
217 // Look for export data in a simple_object.
218
219 Import::Stream*
220 Import::find_object_export_data(const std::string& filename,
221                                 int fd,
222                                 off_t offset,
223                                 Location location)
224 {
225   char *buf;
226   size_t len;
227   int err;
228   const char *errmsg = go_read_export_data(fd, offset, &buf, &len, &err);
229   if (errmsg != NULL)
230     {
231       if (err == 0)
232         error_at(location, "%s: %s", filename.c_str(), errmsg);
233       else
234         error_at(location, "%s: %s: %s", filename.c_str(), errmsg,
235                  xstrerror(err));
236       return NULL;
237     }
238
239   if (buf == NULL)
240     return NULL;
241
242   return new Stream_from_buffer(buf, len);
243 }
244
245 // Class Import.
246
247 // Construct an Import object.  We make the builtin_types_ vector
248 // large enough to hold all the builtin types.
249
250 Import::Import(Stream* stream, Location location)
251   : gogo_(NULL), stream_(stream), location_(location), package_(NULL),
252     add_to_globals_(false),
253     builtin_types_((- SMALLEST_BUILTIN_CODE) + 1),
254     types_()
255 {
256 }
257
258 // Import the data in the associated stream.
259
260 Package*
261 Import::import(Gogo* gogo, const std::string& local_name,
262                bool is_local_name_exported)
263 {
264   // Hold on to the Gogo structure.  Otherwise we need to pass it
265   // through all the import functions, because we need it when reading
266   // a type.
267   this->gogo_ = gogo;
268
269   // A stream of export data can include data from more than one input
270   // file.  Here we loop over each input file.
271   Stream* stream = this->stream_;
272   while (!stream->at_eof() && !stream->saw_error())
273     {
274       // The vector of types is package specific.
275       this->types_.clear();
276
277       stream->require_bytes(this->location_, Export::v1_magic,
278                             Export::v1_magic_len);
279
280       this->require_c_string("package ");
281       std::string package_name = this->read_identifier();
282       this->require_c_string(";\n");
283
284       std::string pkgpath;
285       if (this->match_c_string("prefix "))
286         {
287           this->advance(7);
288           std::string unique_prefix = this->read_identifier();
289           this->require_c_string(";\n");
290           pkgpath = unique_prefix + '.' + package_name;
291         }
292       else
293         {
294           this->require_c_string("pkgpath ");
295           pkgpath = this->read_identifier();
296           this->require_c_string(";\n");
297         }
298
299       this->package_ = gogo->add_imported_package(package_name, local_name,
300                                                   is_local_name_exported,
301                                                   pkgpath,
302                                                   this->location_,
303                                                   &this->add_to_globals_);
304       if (this->package_ == NULL)
305         {
306           stream->set_saw_error();
307           return NULL;
308         }
309
310       this->require_c_string("priority ");
311       std::string priority_string = this->read_identifier();
312       int prio;
313       if (!this->string_to_int(priority_string, false, &prio))
314         return NULL;
315       this->package_->set_priority(prio);
316       this->require_c_string(";\n");
317
318       while (stream->match_c_string("import"))
319         this->read_one_import();
320
321       if (stream->match_c_string("init"))
322         this->read_import_init_fns(gogo);
323
324       // Loop over all the input data for this package.
325       while (!stream->saw_error())
326         {
327           if (stream->match_c_string("const "))
328             this->import_const();
329           else if (stream->match_c_string("type "))
330             this->import_type();
331           else if (stream->match_c_string("var "))
332             this->import_var();
333           else if (stream->match_c_string("func "))
334             this->import_func(this->package_);
335           else if (stream->match_c_string("checksum "))
336             break;
337           else
338             {
339               error_at(this->location_,
340                        ("error in import data at %d: "
341                         "expected %<const%>, %<type%>, %<var%>, "
342                         "%<func%>, or %<checksum%>"),
343                        stream->pos());
344               stream->set_saw_error();
345               return NULL;
346             }
347         }
348
349       // We currently ignore the checksum.  In the future we could
350       // store the checksum somewhere in the generated object and then
351       // verify that the checksum matches at link time or at dynamic
352       // load time.
353       this->require_c_string("checksum ");
354       stream->advance(Export::v1_checksum_len * 2);
355       this->require_c_string(";\n");
356     }
357
358   return this->package_;
359 }
360
361 // Read an import line.  We don't actually care about these.
362
363 void
364 Import::read_one_import()
365 {
366   this->require_c_string("import ");
367   std::string package_name = this->read_identifier();
368   this->require_c_string(" ");
369   std::string pkgpath = this->read_identifier();
370   this->require_c_string(" \"");
371   Stream* stream = this->stream_;
372   while (stream->peek_char() != '"')
373     stream->advance(1);
374   this->require_c_string("\";\n");
375
376   Package* p = this->gogo_->register_package(pkgpath,
377                                              Linemap::unknown_location());
378   p->set_package_name(package_name, this->location());
379 }
380
381 // Read the list of import control functions.
382
383 void
384 Import::read_import_init_fns(Gogo* gogo)
385 {
386   this->require_c_string("init");
387   while (!this->match_c_string(";"))
388     {
389       this->require_c_string(" ");
390       std::string package_name = this->read_identifier();
391       this->require_c_string(" ");
392       std::string init_name = this->read_identifier();
393       this->require_c_string(" ");
394       std::string prio_string = this->read_identifier();
395       int prio;
396       if (!this->string_to_int(prio_string, false, &prio))
397         return;
398       gogo->add_import_init_fn(package_name, init_name, prio);
399     }
400   this->require_c_string(";\n");
401 }
402
403 // Import a constant.
404
405 void
406 Import::import_const()
407 {
408   std::string name;
409   Type* type;
410   Expression* expr;
411   Named_constant::import_const(this, &name, &type, &expr);
412   Typed_identifier tid(name, type, this->location_);
413   Named_object* no = this->package_->add_constant(tid, expr);
414   if (this->add_to_globals_)
415     this->gogo_->add_named_object(no);
416 }
417
418 // Import a type.
419
420 void
421 Import::import_type()
422 {
423   Named_type* type;
424   Named_type::import_named_type(this, &type);
425
426   // The named type has been added to the package by the type import
427   // process.  Here we need to make it visible to the parser, and it
428   // to the global bindings if necessary.
429   type->set_is_visible();
430
431   if (this->add_to_globals_)
432     this->gogo_->add_named_type(type);
433 }
434
435 // Import a variable.
436
437 void
438 Import::import_var()
439 {
440   std::string name;
441   Type* type;
442   Variable::import_var(this, &name, &type);
443   Variable* var = new Variable(type, NULL, true, false, false,
444                                this->location_);
445   Named_object* no;
446   no = this->package_->add_variable(name, var);
447   if (this->add_to_globals_)
448     this->gogo_->add_named_object(no);
449 }
450
451 // Import a function into PACKAGE.  PACKAGE is normally
452 // THIS->PACKAGE_, but it will be different for a method associated
453 // with a type defined in a different package.
454
455 Named_object*
456 Import::import_func(Package* package)
457 {
458   std::string name;
459   Typed_identifier* receiver;
460   Typed_identifier_list* parameters;
461   Typed_identifier_list* results;
462   bool is_varargs;
463   Function::import_func(this, &name, &receiver, &parameters, &results,
464                         &is_varargs);
465   Function_type *fntype = Type::make_function_type(receiver, parameters,
466                                                    results, this->location_);
467   if (is_varargs)
468     fntype->set_is_varargs();
469
470   Location loc = this->location_;
471   Named_object* no;
472   if (fntype->is_method())
473     {
474       Type* rtype = receiver->type();
475
476       // We may still be reading the definition of RTYPE, so we have
477       // to be careful to avoid calling base or convert.  If RTYPE is
478       // a named type or a forward declaration, then we know that it
479       // is not a pointer, because we are reading a method on RTYPE
480       // and named pointers can't have methods.
481
482       if (rtype->classification() == Type::TYPE_POINTER)
483         rtype = rtype->points_to();
484
485       if (rtype->is_error_type())
486         return NULL;
487       else if (rtype->named_type() != NULL)
488         no = rtype->named_type()->add_method_declaration(name, package, fntype,
489                                                          loc);
490       else if (rtype->forward_declaration_type() != NULL)
491         no = rtype->forward_declaration_type()->add_method_declaration(name,
492                                                                        package,
493                                                                        fntype,
494                                                                        loc);
495       else
496         go_unreachable();
497     }
498   else
499     {
500       no = package->add_function_declaration(name, fntype, loc);
501       if (this->add_to_globals_)
502         this->gogo_->add_named_object(no);
503     }
504   return no;
505 }
506
507 // Read a type in the import stream.  This records the type by the
508 // type index.  If the type is named, it registers the name, but marks
509 // it as invisible.
510
511 Type*
512 Import::read_type()
513 {
514   Stream* stream = this->stream_;
515   this->require_c_string("<type ");
516
517   std::string number;
518   int c;
519   while (true)
520     {
521       c = stream->get_char();
522       if (c != '-' && (c < '0' || c > '9'))
523         break;
524       number += c;
525     }
526
527   int index;
528   if (!this->string_to_int(number, true, &index))
529     return Type::make_error_type();
530
531   if (c == '>')
532     {
533       // This type was already defined.
534       if (index < 0
535           ? (static_cast<size_t>(- index) >= this->builtin_types_.size()
536              || this->builtin_types_[- index] == NULL)
537           : (static_cast<size_t>(index) >= this->types_.size()
538              || this->types_[index] == NULL))
539         {
540           error_at(this->location_,
541                    "error in import data at %d: bad type index %d",
542                    stream->pos(), index);
543           stream->set_saw_error();
544           return Type::make_error_type();
545         }
546
547       return index < 0 ? this->builtin_types_[- index] : this->types_[index];
548     }
549
550   if (c != ' ')
551     {
552       if (!stream->saw_error())
553         error_at(this->location_,
554                  "error in import data at %d: expect %< %> or %<>%>'",
555                  stream->pos());
556       stream->set_saw_error();
557       stream->advance(1);
558       return Type::make_error_type();
559     }
560
561   if (index <= 0
562       || (static_cast<size_t>(index) < this->types_.size()
563           && this->types_[index] != NULL))
564     {
565       error_at(this->location_,
566                "error in import data at %d: type index already defined",
567                stream->pos());
568       stream->set_saw_error();
569       return Type::make_error_type();
570     }
571
572   if (static_cast<size_t>(index) >= this->types_.size())
573     {
574       int newsize = std::max(static_cast<size_t>(index) + 1,
575                              this->types_.size() * 2);
576       this->types_.resize(newsize, NULL);
577     }
578
579   if (stream->peek_char() != '"')
580     {
581       Type* type = Type::import_type(this);
582       this->require_c_string(">");
583       this->types_[index] = type;
584       return type;
585     }
586
587   // This type has a name.
588
589   stream->advance(1);
590   std::string type_name;
591   while ((c = stream->get_char()) != '"')
592     type_name += c;
593
594   // If this type is in the package we are currently importing, the
595   // name will be .PKGPATH.NAME or simply NAME with no dots.
596   // Otherwise, a non-hidden symbol will be PKGPATH.NAME and a hidden
597   // symbol will be .PKGPATH.NAME.
598   std::string pkgpath;
599   if (type_name.find('.') != std::string::npos)
600     {
601       size_t start = 0;
602       if (type_name[0] == '.')
603         start = 1;
604       size_t dot = type_name.rfind('.');
605       pkgpath = type_name.substr(start, dot - start);
606       if (type_name[0] != '.')
607         type_name.erase(0, dot + 1);
608     }
609
610   this->require_c_string(" ");
611
612   // The package name may follow.  This is the name of the package in
613   // the package clause of that package.  The type name will include
614   // the pkgpath, which may be different.
615   std::string package_name;
616   if (stream->peek_char() == '"')
617     {
618       stream->advance(1);
619       while ((c = stream->get_char()) != '"')
620         package_name += c;
621       this->require_c_string(" ");
622     }
623
624   // Declare the type in the appropriate package.  If we haven't seen
625   // it before, mark it as invisible.  We declare it before we read
626   // the actual definition of the type, since the definition may refer
627   // to the type itself.
628   Package* package;
629   if (pkgpath.empty() || pkgpath == this->gogo_->pkgpath())
630     package = this->package_;
631   else
632     {
633       package = this->gogo_->register_package(pkgpath,
634                                               Linemap::unknown_location());
635       if (!package_name.empty())
636         package->set_package_name(package_name, this->location());
637     }
638
639   Named_object* no = package->bindings()->lookup(type_name);
640   if (no == NULL)
641     no = package->add_type_declaration(type_name, this->location_);
642   else if (!no->is_type_declaration() && !no->is_type())
643     {
644       error_at(this->location_, "imported %<%s.%s%> both type and non-type",
645                pkgpath.c_str(), Gogo::message_name(type_name).c_str());
646       stream->set_saw_error();
647       return Type::make_error_type();
648     }
649   else
650     go_assert(no->package() == package);
651
652   if (this->types_[index] == NULL)
653     {
654       if (no->is_type_declaration())
655         {
656           // FIXME: It's silly to make a forward declaration every time.
657           this->types_[index] = Type::make_forward_declaration(no);
658         }
659       else
660         {
661           go_assert(no->is_type());
662           this->types_[index] = no->type_value();
663         }
664     }
665
666   // If there is no type definition, then this is just a forward
667   // declaration of a type defined in some other file.
668   Type* type;
669   if (this->match_c_string(">"))
670     type = this->types_[index];
671   else
672     {
673       type = this->read_type();
674
675       if (no->is_type_declaration())
676         {
677           // We can define the type now.
678
679           no = package->add_type(type_name, type, this->location_);
680           Named_type* ntype = no->type_value();
681
682           // This type has not yet been imported.
683           ntype->clear_is_visible();
684
685           if (!type->is_undefined() && type->interface_type() != NULL)
686             this->gogo_->record_interface_type(type->interface_type());
687
688           type = ntype;
689         }
690       else if (no->is_type())
691         {
692           // We have seen this type before.  FIXME: it would be a good
693           // idea to check that the two imported types are identical,
694           // but we have not finalized the methods yet, which means
695           // that we can not reliably compare interface types.
696           type = no->type_value();
697
698           // Don't change the visibility of the existing type.
699         }
700
701       this->types_[index] = type;
702
703       // Read the type methods.
704       if (this->match_c_string("\n"))
705         {
706           this->advance(1);
707           while (this->match_c_string(" func"))
708             {
709               this->advance(1);
710               this->import_func(package);
711             }
712         }
713     }
714
715   this->require_c_string(">");
716
717   return type;
718 }
719
720 // Register the builtin types.
721
722 void
723 Import::register_builtin_types(Gogo* gogo)
724 {
725   this->register_builtin_type(gogo, "int8", BUILTIN_INT8);
726   this->register_builtin_type(gogo, "int16", BUILTIN_INT16);
727   this->register_builtin_type(gogo, "int32", BUILTIN_INT32);
728   this->register_builtin_type(gogo, "int64", BUILTIN_INT64);
729   this->register_builtin_type(gogo, "uint8", BUILTIN_UINT8);
730   this->register_builtin_type(gogo, "uint16", BUILTIN_UINT16);
731   this->register_builtin_type(gogo, "uint32", BUILTIN_UINT32);
732   this->register_builtin_type(gogo, "uint64", BUILTIN_UINT64);
733   this->register_builtin_type(gogo, "float32", BUILTIN_FLOAT32);
734   this->register_builtin_type(gogo, "float64", BUILTIN_FLOAT64);
735   this->register_builtin_type(gogo, "complex64", BUILTIN_COMPLEX64);
736   this->register_builtin_type(gogo, "complex128", BUILTIN_COMPLEX128);
737   this->register_builtin_type(gogo, "int", BUILTIN_INT);
738   this->register_builtin_type(gogo, "uint", BUILTIN_UINT);
739   this->register_builtin_type(gogo, "uintptr", BUILTIN_UINTPTR);
740   this->register_builtin_type(gogo, "bool", BUILTIN_BOOL);
741   this->register_builtin_type(gogo, "string", BUILTIN_STRING);
742   this->register_builtin_type(gogo, "error", BUILTIN_ERROR);
743   this->register_builtin_type(gogo, "byte", BUILTIN_BYTE);
744   this->register_builtin_type(gogo, "rune", BUILTIN_RUNE);
745 }
746
747 // Register a single builtin type.
748
749 void
750 Import::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
751 {
752   Named_object* named_object = gogo->lookup_global(name);
753   go_assert(named_object != NULL && named_object->is_type());
754   int index = - static_cast<int>(code);
755   go_assert(index > 0
756              && static_cast<size_t>(index) < this->builtin_types_.size());
757   this->builtin_types_[index] = named_object->type_value();
758 }
759
760 // Read an identifier from the stream.
761
762 std::string
763 Import::read_identifier()
764 {
765   std::string ret;
766   Stream* stream = this->stream_;
767   int c;
768   while (true)
769     {
770       c = stream->peek_char();
771       if (c == -1 || c == ' ' || c == ';')
772         break;
773       ret += c;
774       stream->advance(1);
775     }
776   return ret;
777 }
778
779 // Read a name from the stream.
780
781 std::string
782 Import::read_name()
783 {
784   std::string ret = this->read_identifier();
785   if (ret == "?")
786     ret.clear();
787   else if (!Lex::is_exported_name(ret))
788     ret = '.' + this->package_->pkgpath() + '.' + ret;
789   return ret;
790 }
791
792 // Turn a string into a integer with appropriate error handling.
793
794 bool
795 Import::string_to_int(const std::string &s, bool is_neg_ok, int* ret)
796 {
797   char* end;
798   long prio = strtol(s.c_str(), &end, 10);
799   if (*end != '\0' || prio > 0x7fffffff || (prio < 0 && !is_neg_ok))
800     {
801       error_at(this->location_, "invalid integer in import data at %d",
802                this->stream_->pos());
803       this->stream_->set_saw_error();
804       return false;
805     }
806   *ret = prio;
807   return true;
808 }
809
810 // Class Import::Stream.
811
812 Import::Stream::Stream()
813   : pos_(0), saw_error_(false)
814 {
815 }
816
817 Import::Stream::~Stream()
818 {
819 }
820
821 // Return the next character to come from the stream.
822
823 int
824 Import::Stream::peek_char()
825 {
826   const char* read;
827   if (!this->do_peek(1, &read))
828     return -1;
829   // Make sure we return an unsigned char, so that we don't get
830   // confused by \xff.
831   unsigned char ret = *read;
832   return ret;
833 }
834
835 // Return true if the next LENGTH characters from the stream match
836 // BYTES
837
838 bool
839 Import::Stream::match_bytes(const char* bytes, size_t length)
840 {
841   const char* read;
842   if (!this->do_peek(length, &read))
843     return false;
844   return memcmp(bytes, read, length) == 0;
845 }
846
847 // Require that the next LENGTH bytes from the stream match BYTES.
848
849 void
850 Import::Stream::require_bytes(Location location, const char* bytes,
851                               size_t length)
852 {
853   const char* read;
854   if (!this->do_peek(length, &read)
855       || memcmp(bytes, read, length) != 0)
856     {
857       if (!this->saw_error_)
858         error_at(location, "import error at %d: expected %<%.*s%>",
859                  this->pos(), static_cast<int>(length), bytes);
860       this->saw_error_ = true;
861       return;
862     }
863   this->advance(length);
864 }
865
866 // Class Stream_from_file.
867
868 Stream_from_file::Stream_from_file(int fd)
869   : fd_(fd), data_()
870 {
871   if (lseek(fd, 0, SEEK_SET) != 0)
872     {
873       error("lseek failed: %m");
874       this->set_saw_error();
875     }
876 }
877
878 Stream_from_file::~Stream_from_file()
879 {
880   close(this->fd_);
881 }
882
883 // Read next bytes.
884
885 bool
886 Stream_from_file::do_peek(size_t length, const char** bytes)
887 {
888   if (this->data_.length() <= length)
889     {
890       *bytes = this->data_.data();
891       return true;
892     }
893   // Don't bother to handle the general case, since we don't need it.
894   go_assert(length < 64);
895   char buf[64];
896   ssize_t got = read(this->fd_, buf, length);
897
898   if (got < 0)
899     {
900       if (!this->saw_error())
901         error("read failed: %m");
902       this->set_saw_error();
903       return false;
904     }
905
906   if (lseek(this->fd_, - got, SEEK_CUR) != 0)
907     {
908       if (!this->saw_error())
909         error("lseek failed: %m");
910       this->set_saw_error();
911       return false;
912     }
913
914   if (static_cast<size_t>(got) < length)
915     return false;
916
917   this->data_.assign(buf, got);
918
919   *bytes = this->data_.data();
920   return true;
921 }
922
923 // Advance.
924
925 void
926 Stream_from_file::do_advance(size_t skip)
927 {
928   if (lseek(this->fd_, skip, SEEK_CUR) != 0)
929     {
930       if (!this->saw_error())
931         error("lseek failed: %m");
932       this->set_saw_error();
933     }
934   if (!this->data_.empty())
935     {
936       if (this->data_.length() < skip)
937         this->data_.erase(0, skip);
938       else
939         this->data_.clear();
940     }
941 }