compiler: track //go:nointerface in export data
[official-gcc.git] / gcc / go / gofrontend / import.cc
blob7e06a3c085047d9531434756e805302ba307d7ee
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.
7 #include "go-system.h"
9 #include "filenames.h"
11 #include "go-c.h"
12 #include "go-diagnostics.h"
13 #include "gogo.h"
14 #include "lex.h"
15 #include "types.h"
16 #include "export.h"
17 #include "import.h"
19 #ifndef O_BINARY
20 #define O_BINARY 0
21 #endif
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.
30 GO_EXTERN_C
31 void
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.
52 // * We append ".o".
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.
60 Import::Stream*
61 Import::open_package(const std::string& filename, Location location,
62 const std::string& relative_import_path)
64 bool is_local;
65 if (IS_ABSOLUTE_PATH(filename))
66 is_local = true;
67 else if (filename[0] == '.'
68 && (filename[1] == '\0' || IS_DIR_SEPARATOR(filename[1])))
69 is_local = true;
70 else if (filename[0] == '.'
71 && filename[1] == '.'
72 && (filename[2] == '\0' || IS_DIR_SEPARATOR(filename[2])))
73 is_local = true;
74 else
75 is_local = false;
77 std::string fn = filename;
78 if (is_local && !IS_ABSOLUTE_PATH(filename) && !relative_import_path.empty())
80 if (fn == ".")
82 // A special case.
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
93 // the same.
94 size_t index;
95 for (index = relative_import_path.length() - 1;
96 index > 0 && !IS_DIR_SEPARATOR(relative_import_path[index]);
97 index--)
99 if (index > 0)
100 fn = relative_import_path.substr(0, index) + fn.substr(2);
101 else
102 fn = relative_import_path + '/' + fn;
104 else
105 fn = relative_import_path + '/' + fn;
106 is_local = false;
109 if (!is_local)
111 for (std::vector<std::string>::const_iterator p = search_path.begin();
112 p != search_path.end();
113 ++p)
115 std::string indir = *p;
116 if (!indir.empty() && indir[indir.size() - 1] != '/')
117 indir += '/';
118 indir += fn;
119 Stream* s = Import::try_package_in_directory(indir, location);
120 if (s != NULL)
121 return s;
125 Stream* s = Import::try_package_in_directory(fn, location);
126 if (s != NULL)
127 return s;
129 return NULL;
132 // Try to find the export data for FILENAME.
134 Import::Stream*
135 Import::try_package_in_directory(const std::string& filename,
136 Location location)
138 std::string found_filename = filename;
139 int fd = open(found_filename.c_str(), O_RDONLY | O_BINARY);
141 if (fd >= 0)
143 struct stat s;
144 if (fstat(fd, &s) >= 0 && S_ISDIR(s.st_mode))
146 close(fd);
147 fd = -1;
148 errno = EISDIR;
152 if (fd < 0)
154 if (errno != ENOENT && errno != EISDIR)
155 go_warning_at(location, 0, "%s: %m", filename.c_str());
157 fd = Import::try_suffixes(&found_filename);
158 if (fd < 0)
159 return NULL;
162 // The export data may not be in this file.
163 Stream* s = Import::find_export_data(found_filename, fd, location);
164 if (s != NULL)
165 return s;
167 close(fd);
169 go_error_at(location, "%s exists but does not contain any Go export data",
170 found_filename.c_str());
172 return NULL;
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);
184 if (fd >= 0)
186 *pfilename = filename;
187 return fd;
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);
194 if (fd >= 0)
196 *pfilename = filename;
197 return fd;
200 filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".a";
201 fd = open(filename.c_str(), O_RDONLY | O_BINARY);
202 if (fd >= 0)
204 *pfilename = filename;
205 return fd;
208 filename = *pfilename + ".o";
209 fd = open(filename.c_str(), O_RDONLY | O_BINARY);
210 if (fd >= 0)
212 *pfilename = filename;
213 return fd;
216 return -1;
219 // Look for export data in the file descriptor FD.
221 Import::Stream*
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,
226 location);
227 if (stream != NULL)
228 return stream;
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());
235 return NULL;
238 char buf[len];
239 ssize_t c = read(fd, buf, len);
240 if (c < len)
241 return NULL;
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);
252 return NULL;
255 // Look for export data in an object file.
257 Import::Stream*
258 Import::find_object_export_data(const std::string& filename,
259 int fd,
260 off_t offset,
261 Location location)
263 char *buf;
264 size_t len;
265 int err;
266 const char *errmsg = go_read_export_data(fd, offset, &buf, &len, &err);
267 if (errmsg != NULL)
269 if (err == 0)
270 go_error_at(location, "%s: %s", filename.c_str(), errmsg);
271 else
272 go_error_at(location, "%s: %s: %s", filename.c_str(), errmsg,
273 xstrerror(err));
274 return NULL;
277 if (buf == NULL)
278 return NULL;
280 return new Stream_from_buffer(buf, len);
283 // Class Import.
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.
298 Package*
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
304 // a type.
305 this->gogo_ = gogo;
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,
319 Export::magic_len);
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,
325 Export::magic_len);
326 this->version_ = EXPORT_FORMAT_V1;
328 else
330 go_error_at(this->location_,
331 ("error in import data at %d: invalid magic string"),
332 stream->pos());
333 return NULL;
336 this->require_c_string("package ");
337 std::string package_name = this->read_identifier();
338 this->require_c_string(";\n");
340 std::string pkgpath;
341 std::string pkgpath_symbol;
342 if (this->match_c_string("prefix "))
344 this->advance(7);
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));
351 else
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,
362 this->location_,
363 &this->add_to_globals_);
364 if (this->package_ == NULL)
366 stream->set_saw_error();
367 return NULL;
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();
375 int prio;
376 if (!this->string_to_int(priority_string, false, &prio))
377 return NULL;
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 "))
396 this->import_type();
397 else if (stream->match_c_string("var "))
398 this->import_var();
399 else if (stream->match_c_string("func "))
400 this->import_func(this->package_);
401 else if (stream->match_c_string("checksum "))
402 break;
403 else
405 go_error_at(this->location_,
406 ("error in import data at %d: "
407 "expected %<const%>, %<type%>, %<var%>, "
408 "%<func%>, or %<checksum%>"),
409 stream->pos());
410 stream->set_saw_error();
411 return NULL;
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
418 // load time.
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.
430 void
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.
448 void
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() != '"')
458 stream->advance(1);
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.
468 void
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(";"))
479 int priority = -1;
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))
493 return;
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 =
513 init_idx.begin();
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();
531 unsigned src;
532 if (!this->string_to_unsigned(src_string, &src)) return;
534 this->require_c_string(" ");
535 std::string sink_string = this->read_identifier();
536 unsigned sink;
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.
552 void
553 Import::import_const()
555 std::string name;
556 Type* type;
557 Expression* expr;
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);
565 // Import a type.
567 void
568 Import::import_type()
570 Named_type* 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.
584 void
585 Import::import_var()
587 std::string name;
588 Type* type;
589 Variable::import_var(this, &name, &type);
590 Variable* var = new Variable(type, NULL, true, false, false,
591 this->location_);
592 Named_object* no;
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.
602 Named_object*
603 Import::import_func(Package* package)
605 std::string name;
606 Typed_identifier* receiver;
607 Typed_identifier_list* parameters;
608 Typed_identifier_list* results;
609 bool is_varargs;
610 bool nointerface;
611 Function::import_func(this, &name, &receiver,
612 &parameters, &results, &is_varargs, &nointerface);
613 Function_type *fntype = Type::make_function_type(receiver, parameters,
614 results, this->location_);
615 if (is_varargs)
616 fntype->set_is_varargs();
618 Location loc = this->location_;
619 Named_object* no;
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())
634 return NULL;
635 else if (rtype->named_type() != NULL)
636 no = rtype->named_type()->add_method_declaration(name, package, fntype,
637 loc);
638 else if (rtype->forward_declaration_type() != NULL)
639 no = rtype->forward_declaration_type()->add_method_declaration(name,
640 package,
641 fntype,
642 loc);
643 else
644 go_unreachable();
646 else
648 no = package->add_function_declaration(name, fntype, loc);
649 if (this->add_to_globals_)
650 this->gogo_->add_dot_import_object(no);
653 if (nointerface)
654 no->func_declaration_value()->set_nointerface();
656 return no;
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
661 // it as invisible.
663 Type*
664 Import::read_type()
666 Stream* stream = this->stream_;
667 this->require_c_string("<type ");
669 std::string number;
670 int c;
671 while (true)
673 c = stream->get_char();
674 if (c != '-' && (c < '0' || c > '9'))
675 break;
676 number += c;
679 int index;
680 if (!this->string_to_int(number, true, &index))
681 return Type::make_error_type();
683 if (c == '>')
685 // This type was already defined.
686 if (index < 0
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];
702 if (c != ' ')
704 if (!stream->saw_error())
705 go_error_at(this->location_,
706 "error in import data at %d: expect %< %> or %<>%>'",
707 stream->pos());
708 stream->set_saw_error();
709 stream->advance(1);
710 return Type::make_error_type();
713 if (index <= 0
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",
719 stream->pos());
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;
736 return type;
739 // This type has a name.
741 stream->advance(1);
742 std::string type_name;
743 while ((c = stream->get_char()) != '"')
744 type_name += c;
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.
750 std::string pkgpath;
751 if (type_name.find('.') != std::string::npos)
753 size_t start = 0;
754 if (type_name[0] == '.')
755 start = 1;
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() == '"')
770 stream->advance(1);
771 while ((c = stream->get_char()) != '"')
772 package_name += c;
773 this->require_c_string(" ");
776 bool is_alias = false;
777 if (this->match_c_string("= "))
779 stream->advance(2);
780 is_alias = true;
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.
787 Package* package;
788 if (pkgpath.empty() || pkgpath == this->gogo_->pkgpath())
789 package = this->package_;
790 else
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);
799 if (no == NULL)
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();
808 else
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);
818 else
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.
827 Type* type;
828 if (this->match_c_string(">"))
829 type = this->types_[index];
830 else
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();
844 if (is_alias)
845 ntype->set_is_alias();
847 if (!type->is_undefined() && type->interface_type() != NULL)
848 this->gogo_->record_interface_type(type->interface_type());
850 type = ntype;
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"))
868 this->advance(1);
869 while (this->match_c_string(" func"))
871 this->advance(1);
872 this->import_func(package);
877 this->require_c_string(">");
879 return type;
882 // Read an escape note.
884 std::string
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:";
893 int c;
894 while (true)
896 c = stream->get_char();
897 if (c != 'x' && !ISXDIGIT(c))
898 break;
899 escape += c;
902 if (c != '>')
904 go_error_at(this->location(),
905 ("error in import data at %d: "
906 "expect %< %> or %<>%>, got %c"),
907 stream->pos(), c);
908 stream->set_saw_error();
909 stream->advance(1);
910 escape = Escape_note::make_tag(Node::ESCAPE_UNKNOWN);
912 return escape;
914 else
915 return Escape_note::make_tag(Node::ESCAPE_UNKNOWN);
919 // Register the builtin types.
921 void
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.
948 void
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);
954 go_assert(index > 0
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.
961 std::string
962 Import::read_identifier()
964 std::string ret;
965 Stream* stream = this->stream_;
966 int c;
967 while (true)
969 c = stream->peek_char();
970 if (c == -1 || c == ' ' || c == ';')
971 break;
972 ret += c;
973 stream->advance(1);
975 return ret;
978 // Read a name from the stream.
980 std::string
981 Import::read_name()
983 std::string ret = this->read_identifier();
984 if (ret == "?")
985 ret.clear();
986 else if (!Lex::is_exported_name(ret))
987 ret = '.' + this->package_->pkgpath() + '.' + ret;
988 return ret;
991 // Turn a string into a integer with appropriate error handling.
993 bool
994 Import::string_to_int(const std::string &s, bool is_neg_ok, int* ret)
996 char* end;
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();
1003 return false;
1005 *ret = prio;
1006 return true;
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()
1025 const char* read;
1026 if (!this->do_peek(1, &read))
1027 return -1;
1028 // Make sure we return an unsigned char, so that we don't get
1029 // confused by \xff.
1030 unsigned char ret = *read;
1031 return ret;
1034 // Return true if the next LENGTH characters from the stream match
1035 // BYTES
1037 bool
1038 Import::Stream::match_bytes(const char* bytes, size_t length)
1040 const char* read;
1041 if (!this->do_peek(length, &read))
1042 return false;
1043 return memcmp(bytes, read, length) == 0;
1046 // Require that the next LENGTH bytes from the stream match BYTES.
1048 void
1049 Import::Stream::require_bytes(Location location, const char* bytes,
1050 size_t length)
1052 const char* read;
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;
1060 return;
1062 this->advance(length);
1065 // Class Stream_from_file.
1067 Stream_from_file::Stream_from_file(int fd)
1068 : fd_(fd), data_()
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()
1079 close(this->fd_);
1082 // Read next bytes.
1084 bool
1085 Stream_from_file::do_peek(size_t length, const char** bytes)
1087 if (this->data_.length() <= length)
1089 *bytes = this->data_.data();
1090 return true;
1092 // Don't bother to handle the general case, since we don't need it.
1093 go_assert(length < 64);
1094 char buf[64];
1095 ssize_t got = read(this->fd_, buf, length);
1097 if (got < 0)
1099 if (!this->saw_error())
1100 go_fatal_error(Linemap::unknown_location(), "read failed: %m");
1101 this->set_saw_error();
1102 return false;
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();
1110 return false;
1113 if (static_cast<size_t>(got) < length)
1114 return false;
1116 this->data_.assign(buf, got);
1118 *bytes = this->data_.data();
1119 return true;
1122 // Advance.
1124 void
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);
1137 else
1138 this->data_.clear();