net/internal/socktest: build sys_unix.go on AIX
[official-gcc.git] / gcc / go / gofrontend / import.cc
blob20b077f7f9985e6436cdada24df66986f18ac727
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 Function::import_func(this, &name, &receiver,
611 &parameters, &results, &is_varargs);
612 Function_type *fntype = Type::make_function_type(receiver, parameters,
613 results, this->location_);
614 if (is_varargs)
615 fntype->set_is_varargs();
617 Location loc = this->location_;
618 Named_object* no;
619 if (fntype->is_method())
621 Type* rtype = receiver->type();
623 // We may still be reading the definition of RTYPE, so we have
624 // to be careful to avoid calling base or convert. If RTYPE is
625 // a named type or a forward declaration, then we know that it
626 // is not a pointer, because we are reading a method on RTYPE
627 // and named pointers can't have methods.
629 if (rtype->classification() == Type::TYPE_POINTER)
630 rtype = rtype->points_to();
632 if (rtype->is_error_type())
633 return NULL;
634 else if (rtype->named_type() != NULL)
635 no = rtype->named_type()->add_method_declaration(name, package, fntype,
636 loc);
637 else if (rtype->forward_declaration_type() != NULL)
638 no = rtype->forward_declaration_type()->add_method_declaration(name,
639 package,
640 fntype,
641 loc);
642 else
643 go_unreachable();
645 else
647 no = package->add_function_declaration(name, fntype, loc);
648 if (this->add_to_globals_)
649 this->gogo_->add_dot_import_object(no);
651 return no;
654 // Read a type in the import stream. This records the type by the
655 // type index. If the type is named, it registers the name, but marks
656 // it as invisible.
658 Type*
659 Import::read_type()
661 Stream* stream = this->stream_;
662 this->require_c_string("<type ");
664 std::string number;
665 int c;
666 while (true)
668 c = stream->get_char();
669 if (c != '-' && (c < '0' || c > '9'))
670 break;
671 number += c;
674 int index;
675 if (!this->string_to_int(number, true, &index))
676 return Type::make_error_type();
678 if (c == '>')
680 // This type was already defined.
681 if (index < 0
682 ? (static_cast<size_t>(- index) >= this->builtin_types_.size()
683 || this->builtin_types_[- index] == NULL)
684 : (static_cast<size_t>(index) >= this->types_.size()
685 || this->types_[index] == NULL))
687 go_error_at(this->location_,
688 "error in import data at %d: bad type index %d",
689 stream->pos(), index);
690 stream->set_saw_error();
691 return Type::make_error_type();
694 return index < 0 ? this->builtin_types_[- index] : this->types_[index];
697 if (c != ' ')
699 if (!stream->saw_error())
700 go_error_at(this->location_,
701 "error in import data at %d: expect %< %> or %<>%>'",
702 stream->pos());
703 stream->set_saw_error();
704 stream->advance(1);
705 return Type::make_error_type();
708 if (index <= 0
709 || (static_cast<size_t>(index) < this->types_.size()
710 && this->types_[index] != NULL))
712 go_error_at(this->location_,
713 "error in import data at %d: type index already defined",
714 stream->pos());
715 stream->set_saw_error();
716 return Type::make_error_type();
719 if (static_cast<size_t>(index) >= this->types_.size())
721 int newsize = std::max(static_cast<size_t>(index) + 1,
722 this->types_.size() * 2);
723 this->types_.resize(newsize, NULL);
726 if (stream->peek_char() != '"')
728 Type* type = Type::import_type(this);
729 this->require_c_string(">");
730 this->types_[index] = type;
731 return type;
734 // This type has a name.
736 stream->advance(1);
737 std::string type_name;
738 while ((c = stream->get_char()) != '"')
739 type_name += c;
741 // If this type is in the package we are currently importing, the
742 // name will be .PKGPATH.NAME or simply NAME with no dots.
743 // Otherwise, a non-hidden symbol will be PKGPATH.NAME and a hidden
744 // symbol will be .PKGPATH.NAME.
745 std::string pkgpath;
746 if (type_name.find('.') != std::string::npos)
748 size_t start = 0;
749 if (type_name[0] == '.')
750 start = 1;
751 size_t dot = type_name.rfind('.');
752 pkgpath = type_name.substr(start, dot - start);
753 if (type_name[0] != '.')
754 type_name.erase(0, dot + 1);
757 this->require_c_string(" ");
759 bool is_alias = false;
760 if (this->match_c_string("= "))
762 stream->advance(2);
763 is_alias = true;
766 // The package name may follow. This is the name of the package in
767 // the package clause of that package. The type name will include
768 // the pkgpath, which may be different.
769 std::string package_name;
770 if (stream->peek_char() == '"')
772 stream->advance(1);
773 while ((c = stream->get_char()) != '"')
774 package_name += c;
775 this->require_c_string(" ");
778 // Declare the type in the appropriate package. If we haven't seen
779 // it before, mark it as invisible. We declare it before we read
780 // the actual definition of the type, since the definition may refer
781 // to the type itself.
782 Package* package;
783 if (pkgpath.empty() || pkgpath == this->gogo_->pkgpath())
784 package = this->package_;
785 else
787 package = this->gogo_->register_package(pkgpath, "",
788 Linemap::unknown_location());
789 if (!package_name.empty())
790 package->set_package_name(package_name, this->location());
793 Named_object* no = package->bindings()->lookup(type_name);
794 if (no == NULL)
795 no = package->add_type_declaration(type_name, this->location_);
796 else if (!no->is_type_declaration() && !no->is_type())
798 go_error_at(this->location_, "imported %<%s.%s%> both type and non-type",
799 pkgpath.c_str(), Gogo::message_name(type_name).c_str());
800 stream->set_saw_error();
801 return Type::make_error_type();
803 else
804 go_assert(no->package() == package);
806 if (this->types_[index] == NULL)
808 if (no->is_type_declaration())
810 // FIXME: It's silly to make a forward declaration every time.
811 this->types_[index] = Type::make_forward_declaration(no);
813 else
815 go_assert(no->is_type());
816 this->types_[index] = no->type_value();
820 // If there is no type definition, then this is just a forward
821 // declaration of a type defined in some other file.
822 Type* type;
823 if (this->match_c_string(">"))
824 type = this->types_[index];
825 else
827 type = this->read_type();
829 if (no->is_type_declaration())
831 // We can define the type now.
833 no = package->add_type(type_name, type, this->location_);
834 Named_type* ntype = no->type_value();
836 // This type has not yet been imported.
837 ntype->clear_is_visible();
839 if (is_alias)
840 ntype->set_is_alias();
842 if (!type->is_undefined() && type->interface_type() != NULL)
843 this->gogo_->record_interface_type(type->interface_type());
845 type = ntype;
847 else if (no->is_type())
849 // We have seen this type before. FIXME: it would be a good
850 // idea to check that the two imported types are identical,
851 // but we have not finalized the methods yet, which means
852 // that we can not reliably compare interface types.
853 type = no->type_value();
855 // Don't change the visibility of the existing type.
858 this->types_[index] = type;
860 // Read the type methods.
861 if (this->match_c_string("\n"))
863 this->advance(1);
864 while (this->match_c_string(" func"))
866 this->advance(1);
867 this->import_func(package);
872 this->require_c_string(">");
874 return type;
877 // Read an escape note.
879 std::string
880 Import::read_escape()
882 if (this->match_c_string(" <esc:"))
884 Stream* stream = this->stream_;
885 this->require_c_string(" <esc:");
887 std::string escape = "esc:";
888 int c;
889 while (true)
891 c = stream->get_char();
892 if (c != 'x' && !ISXDIGIT(c))
893 break;
894 escape += c;
897 if (c != '>')
899 go_error_at(this->location(),
900 ("error in import data at %d: "
901 "expect %< %> or %<>%>, got %c"),
902 stream->pos(), c);
903 stream->set_saw_error();
904 stream->advance(1);
905 escape = Escape_note::make_tag(Node::ESCAPE_UNKNOWN);
907 return escape;
909 else
910 return Escape_note::make_tag(Node::ESCAPE_UNKNOWN);
914 // Register the builtin types.
916 void
917 Import::register_builtin_types(Gogo* gogo)
919 this->register_builtin_type(gogo, "int8", BUILTIN_INT8);
920 this->register_builtin_type(gogo, "int16", BUILTIN_INT16);
921 this->register_builtin_type(gogo, "int32", BUILTIN_INT32);
922 this->register_builtin_type(gogo, "int64", BUILTIN_INT64);
923 this->register_builtin_type(gogo, "uint8", BUILTIN_UINT8);
924 this->register_builtin_type(gogo, "uint16", BUILTIN_UINT16);
925 this->register_builtin_type(gogo, "uint32", BUILTIN_UINT32);
926 this->register_builtin_type(gogo, "uint64", BUILTIN_UINT64);
927 this->register_builtin_type(gogo, "float32", BUILTIN_FLOAT32);
928 this->register_builtin_type(gogo, "float64", BUILTIN_FLOAT64);
929 this->register_builtin_type(gogo, "complex64", BUILTIN_COMPLEX64);
930 this->register_builtin_type(gogo, "complex128", BUILTIN_COMPLEX128);
931 this->register_builtin_type(gogo, "int", BUILTIN_INT);
932 this->register_builtin_type(gogo, "uint", BUILTIN_UINT);
933 this->register_builtin_type(gogo, "uintptr", BUILTIN_UINTPTR);
934 this->register_builtin_type(gogo, "bool", BUILTIN_BOOL);
935 this->register_builtin_type(gogo, "string", BUILTIN_STRING);
936 this->register_builtin_type(gogo, "error", BUILTIN_ERROR);
937 this->register_builtin_type(gogo, "byte", BUILTIN_BYTE);
938 this->register_builtin_type(gogo, "rune", BUILTIN_RUNE);
941 // Register a single builtin type.
943 void
944 Import::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
946 Named_object* named_object = gogo->lookup_global(name);
947 go_assert(named_object != NULL && named_object->is_type());
948 int index = - static_cast<int>(code);
949 go_assert(index > 0
950 && static_cast<size_t>(index) < this->builtin_types_.size());
951 this->builtin_types_[index] = named_object->type_value();
954 // Read an identifier from the stream.
956 std::string
957 Import::read_identifier()
959 std::string ret;
960 Stream* stream = this->stream_;
961 int c;
962 while (true)
964 c = stream->peek_char();
965 if (c == -1 || c == ' ' || c == ';')
966 break;
967 ret += c;
968 stream->advance(1);
970 return ret;
973 // Read a name from the stream.
975 std::string
976 Import::read_name()
978 std::string ret = this->read_identifier();
979 if (ret == "?")
980 ret.clear();
981 else if (!Lex::is_exported_name(ret))
982 ret = '.' + this->package_->pkgpath() + '.' + ret;
983 return ret;
986 // Turn a string into a integer with appropriate error handling.
988 bool
989 Import::string_to_int(const std::string &s, bool is_neg_ok, int* ret)
991 char* end;
992 long prio = strtol(s.c_str(), &end, 10);
993 if (*end != '\0' || prio > 0x7fffffff || (prio < 0 && !is_neg_ok))
995 go_error_at(this->location_, "invalid integer in import data at %d",
996 this->stream_->pos());
997 this->stream_->set_saw_error();
998 return false;
1000 *ret = prio;
1001 return true;
1004 // Class Import::Stream.
1006 Import::Stream::Stream()
1007 : pos_(0), saw_error_(false)
1011 Import::Stream::~Stream()
1015 // Return the next character to come from the stream.
1018 Import::Stream::peek_char()
1020 const char* read;
1021 if (!this->do_peek(1, &read))
1022 return -1;
1023 // Make sure we return an unsigned char, so that we don't get
1024 // confused by \xff.
1025 unsigned char ret = *read;
1026 return ret;
1029 // Return true if the next LENGTH characters from the stream match
1030 // BYTES
1032 bool
1033 Import::Stream::match_bytes(const char* bytes, size_t length)
1035 const char* read;
1036 if (!this->do_peek(length, &read))
1037 return false;
1038 return memcmp(bytes, read, length) == 0;
1041 // Require that the next LENGTH bytes from the stream match BYTES.
1043 void
1044 Import::Stream::require_bytes(Location location, const char* bytes,
1045 size_t length)
1047 const char* read;
1048 if (!this->do_peek(length, &read)
1049 || memcmp(bytes, read, length) != 0)
1051 if (!this->saw_error_)
1052 go_error_at(location, "import error at %d: expected %<%.*s%>",
1053 this->pos(), static_cast<int>(length), bytes);
1054 this->saw_error_ = true;
1055 return;
1057 this->advance(length);
1060 // Class Stream_from_file.
1062 Stream_from_file::Stream_from_file(int fd)
1063 : fd_(fd), data_()
1065 if (lseek(fd, 0, SEEK_SET) != 0)
1067 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1068 this->set_saw_error();
1072 Stream_from_file::~Stream_from_file()
1074 close(this->fd_);
1077 // Read next bytes.
1079 bool
1080 Stream_from_file::do_peek(size_t length, const char** bytes)
1082 if (this->data_.length() <= length)
1084 *bytes = this->data_.data();
1085 return true;
1087 // Don't bother to handle the general case, since we don't need it.
1088 go_assert(length < 64);
1089 char buf[64];
1090 ssize_t got = read(this->fd_, buf, length);
1092 if (got < 0)
1094 if (!this->saw_error())
1095 go_fatal_error(Linemap::unknown_location(), "read failed: %m");
1096 this->set_saw_error();
1097 return false;
1100 if (lseek(this->fd_, - got, SEEK_CUR) != 0)
1102 if (!this->saw_error())
1103 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1104 this->set_saw_error();
1105 return false;
1108 if (static_cast<size_t>(got) < length)
1109 return false;
1111 this->data_.assign(buf, got);
1113 *bytes = this->data_.data();
1114 return true;
1117 // Advance.
1119 void
1120 Stream_from_file::do_advance(size_t skip)
1122 if (lseek(this->fd_, skip, SEEK_CUR) != 0)
1124 if (!this->saw_error())
1125 go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1126 this->set_saw_error();
1128 if (!this->data_.empty())
1130 if (this->data_.length() < skip)
1131 this->data_.erase(0, skip);
1132 else
1133 this->data_.clear();