OSDN Git Service

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