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 
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"
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
go_add_search_path(const char * path)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 start with ./ or ../, we use
45 // RELATIVE_IMPORT_PATH as a prefix.
46 
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".
53 
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.
59 
60 Import::Stream*
open_package(const std::string & filename,Location location,const std::string & relative_import_path)61 Import::open_package(const std::string& filename, Location location,
62 		     const std::string& relative_import_path)
63 {
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;
76 
77   std::string fn = filename;
78   if (is_local && !IS_ABSOLUTE_PATH(filename) && !relative_import_path.empty())
79     {
80       if (fn == ".")
81 	{
82 	  // A special case.
83 	  fn = relative_import_path;
84 	}
85       else if (fn[0] == '.' && fn[1] == '.'
86 	       && (fn[2] == '\0' || IS_DIR_SEPARATOR(fn[2])))
87 	{
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--)
98 	    ;
99 	  if (index > 0)
100 	    fn = relative_import_path.substr(0, index) + fn.substr(2);
101 	  else
102 	    fn = relative_import_path + '/' + fn;
103 	}
104       else
105 	fn = relative_import_path + '/' + fn;
106       is_local = false;
107     }
108 
109   if (!is_local)
110     {
111       for (std::vector<std::string>::const_iterator p = search_path.begin();
112 	   p != search_path.end();
113 	   ++p)
114 	{
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;
122 	}
123     }
124 
125   Stream* s = Import::try_package_in_directory(fn, location);
126   if (s != NULL)
127     return s;
128 
129   return NULL;
130 }
131 
132 // Try to find the export data for FILENAME.
133 
134 Import::Stream*
try_package_in_directory(const std::string & filename,Location location)135 Import::try_package_in_directory(const std::string& filename,
136 				 Location location)
137 {
138   std::string found_filename = filename;
139   int fd = open(found_filename.c_str(), O_RDONLY | O_BINARY);
140 
141   if (fd >= 0)
142     {
143       struct stat s;
144       if (fstat(fd, &s) >= 0 && S_ISDIR(s.st_mode))
145 	{
146 	  close(fd);
147 	  fd = -1;
148 	  errno = EISDIR;
149 	}
150     }
151 
152   if (fd < 0)
153     {
154       if (errno != ENOENT && errno != EISDIR)
155 	go_warning_at(location, 0, "%s: %m", filename.c_str());
156 
157       fd = Import::try_suffixes(&found_filename);
158       if (fd < 0)
159 	return NULL;
160     }
161 
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;
166 
167   close(fd);
168 
169   go_error_at(location, "%s exists but does not contain any Go export data",
170 	      found_filename.c_str());
171 
172   return NULL;
173 }
174 
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.
178 
179 int
try_suffixes(std::string * pfilename)180 Import::try_suffixes(std::string* pfilename)
181 {
182   std::string filename = *pfilename + ".gox";
183   int fd = open(filename.c_str(), O_RDONLY | O_BINARY);
184   if (fd >= 0)
185     {
186       *pfilename = filename;
187       return fd;
188     }
189 
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)
195     {
196       *pfilename = filename;
197       return fd;
198     }
199 
200   filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".a";
201   fd = open(filename.c_str(), O_RDONLY | O_BINARY);
202   if (fd >= 0)
203     {
204       *pfilename = filename;
205       return fd;
206     }
207 
208   filename = *pfilename + ".o";
209   fd = open(filename.c_str(), O_RDONLY | O_BINARY);
210   if (fd >= 0)
211     {
212       *pfilename = filename;
213       return fd;
214     }
215 
216   return -1;
217 }
218 
219 // Look for export data in the file descriptor FD.
220 
221 Import::Stream*
find_export_data(const std::string & filename,int fd,Location location)222 Import::find_export_data(const std::string& filename, int fd, Location location)
223 {
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;
229 
230   const int len = MAX(Export::magic_len, Import::archive_magic_len);
231 
232   if (lseek(fd, 0, SEEK_SET) < 0)
233     {
234       go_error_at(location, "lseek %s failed: %m", filename.c_str());
235       return NULL;
236     }
237 
238   char buf[len];
239   ssize_t c = read(fd, buf, len);
240   if (c < len)
241     return NULL;
242 
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);
247 
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);
251 
252   return NULL;
253 }
254 
255 // Look for export data in an object file.
256 
257 Import::Stream*
find_object_export_data(const std::string & filename,int fd,off_t offset,Location location)258 Import::find_object_export_data(const std::string& filename,
259 				int fd,
260 				off_t offset,
261 				Location location)
262 {
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)
268     {
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;
275     }
276 
277   if (buf == NULL)
278     return NULL;
279 
280   return new Stream_from_buffer(buf, len);
281 }
282 
283 // Class Import.
284 
285 // Construct an Import object.  We make the builtin_types_ vector
286 // large enough to hold all the builtin types.
287 
Import(Stream * stream,Location location)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)
293 {
294 }
295 
296 // Import the data in the associated stream.
297 
298 Package*
import(Gogo * gogo,const std::string & local_name,bool is_local_name_exported)299 Import::import(Gogo* gogo, const std::string& local_name,
300 	       bool is_local_name_exported)
301 {
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;
306 
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())
311     {
312       // The vector of types is package specific.
313       this->types_.clear();
314 
315       // Check magic string / version number.
316       if (stream->match_bytes(Export::cur_magic, Export::magic_len))
317 	{
318 	  stream->require_bytes(this->location_, Export::cur_magic,
319 	                        Export::magic_len);
320 	  this->version_ = EXPORT_FORMAT_CURRENT;
321 	}
322       else if (stream->match_bytes(Export::v1_magic, Export::magic_len))
323 	{
324 	  stream->require_bytes(this->location_, Export::v1_magic,
325 	                        Export::magic_len);
326 	  this->version_ = EXPORT_FORMAT_V1;
327 	}
328       else
329 	{
330 	  go_error_at(this->location_,
331 		      ("error in import data at %d: invalid magic string"),
332 		      stream->pos());
333 	  return NULL;
334 	}
335 
336       this->require_c_string("package ");
337       std::string package_name = this->read_identifier();
338       this->require_c_string(";\n");
339 
340       std::string pkgpath;
341       std::string pkgpath_symbol;
342       if (this->match_c_string("prefix "))
343 	{
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));
350 	}
351       else
352 	{
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);
357 	}
358 
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)
365 	{
366 	  stream->set_saw_error();
367 	  return NULL;
368 	}
369 
370       // Read and discard priority if older V1 export data format.
371       if (version() == EXPORT_FORMAT_V1)
372 	{
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");
379 	}
380 
381       while (stream->match_c_string("package"))
382 	this->read_one_package();
383 
384       while (stream->match_c_string("import"))
385 	this->read_one_import();
386 
387       if (stream->match_c_string("init"))
388 	this->read_import_init_fns(gogo);
389 
390       // Loop over all the input data for this package.
391       while (!stream->saw_error())
392 	{
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
404 	    {
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;
412 	    }
413 	}
414 
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");
422     }
423 
424   return this->package_;
425 }
426 
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.
429 
430 void
read_one_package()431 Import::read_one_package()
432 {
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");
440 
441   Package* p = this->gogo_->register_package(pkgpath, pkgpath_symbol,
442 					     Linemap::unknown_location());
443   p->set_package_name(package_name, this->location());
444 }
445 
446 // Read an import line.  We don't actually care about these.
447 
448 void
read_one_import()449 Import::read_one_import()
450 {
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");
460 
461   Package* p = this->gogo_->register_package(pkgpath, "",
462 					     Linemap::unknown_location());
463   p->set_package_name(package_name, this->location());
464 }
465 
466 // Read the list of import control functions and/or init graph.
467 
468 void
read_import_init_fns(Gogo * gogo)469 Import::read_import_init_fns(Gogo* gogo)
470 {
471   this->require_c_string("init");
472 
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;
476 
477   while (!this->match_c_string(";"))
478     {
479       int priority = -1;
480 
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)
486         {
487           // Older version 1 init fcn export data format is:
488           //
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;
494         }
495       gogo->add_import_init_fn(package_name, init_name, priority);
496 
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;
501     }
502   this->require_c_string(";\n");
503 
504   if (this->match_c_string("init_graph"))
505     {
506       this->require_c_string("init_graph");
507 
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)
515 	{
516 	  const std::string& init_name = it->first;
517 	  Import_init* ii = gogo->lookup_init(init_name);
518 	  import_initvec[it->second] = ii;
519 	}
520 
521       // Init graph format is:
522       //
523       //    init_graph <src1> <sink1> <src2> <sink2> ... ;
524       //
525       // where src + sink are init functions indices.
526 
527       while (!this->match_c_string(";"))
528 	{
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;
533 
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;
538 
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];
543 
544 	  ii_src->record_precursor_fcn(ii_sink->init_name());
545 	}
546       this->require_c_string(";\n");
547     }
548 }
549 
550 // Import a constant.
551 
552 void
import_const()553 Import::import_const()
554 {
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);
563 }
564 
565 // Import a type.
566 
567 void
import_type()568 Import::import_type()
569 {
570   Named_type* type;
571   Named_type::import_named_type(this, &type);
572 
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();
577 
578   if (this->add_to_globals_)
579     this->gogo_->add_named_type(type);
580 }
581 
582 // Import a variable.
583 
584 void
import_var()585 Import::import_var()
586 {
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);
596 }
597 
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.
601 
602 Named_object*
import_func(Package * package)603 Import::import_func(Package* package)
604 {
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();
617 
618   Location loc = this->location_;
619   Named_object* no;
620   if (fntype->is_method())
621     {
622       Type* rtype = receiver->type();
623 
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.
629 
630       if (rtype->classification() == Type::TYPE_POINTER)
631 	rtype = rtype->points_to();
632 
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();
645     }
646   else
647     {
648       no = package->add_function_declaration(name, fntype, loc);
649       if (this->add_to_globals_)
650 	this->gogo_->add_dot_import_object(no);
651     }
652 
653   if (nointerface)
654     no->func_declaration_value()->set_nointerface();
655 
656   return no;
657 }
658 
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.
662 
663 Type*
read_type()664 Import::read_type()
665 {
666   Stream* stream = this->stream_;
667   this->require_c_string("<type ");
668 
669   std::string number;
670   int c;
671   while (true)
672     {
673       c = stream->get_char();
674       if (c != '-' && (c < '0' || c > '9'))
675 	break;
676       number += c;
677     }
678 
679   int index;
680   if (!this->string_to_int(number, true, &index))
681     return Type::make_error_type();
682 
683   if (c == '>')
684     {
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))
691 	{
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();
697 	}
698 
699       return index < 0 ? this->builtin_types_[- index] : this->types_[index];
700     }
701 
702   if (c != ' ')
703     {
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();
711     }
712 
713   if (index <= 0
714       || (static_cast<size_t>(index) < this->types_.size()
715 	  && this->types_[index] != NULL))
716     {
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();
722     }
723 
724   if (static_cast<size_t>(index) >= this->types_.size())
725     {
726       int newsize = std::max(static_cast<size_t>(index) + 1,
727 			     this->types_.size() * 2);
728       this->types_.resize(newsize, NULL);
729     }
730 
731   if (stream->peek_char() != '"')
732     {
733       Type* type = Type::import_type(this);
734       this->require_c_string(">");
735       this->types_[index] = type;
736       return type;
737     }
738 
739   // This type has a name.
740 
741   stream->advance(1);
742   std::string type_name;
743   while ((c = stream->get_char()) != '"')
744     type_name += c;
745 
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)
752     {
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);
760     }
761 
762   this->require_c_string(" ");
763 
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() == '"')
769     {
770       stream->advance(1);
771       while ((c = stream->get_char()) != '"')
772 	package_name += c;
773       this->require_c_string(" ");
774     }
775 
776   bool is_alias = false;
777   if (this->match_c_string("= "))
778     {
779       stream->advance(2);
780       is_alias = true;
781     }
782 
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
791     {
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());
796     }
797 
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())
802     {
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();
807     }
808   else
809     go_assert(no->package() == package);
810 
811   if (this->types_[index] == NULL)
812     {
813       if (no->is_type_declaration())
814 	{
815 	  // FIXME: It's silly to make a forward declaration every time.
816 	  this->types_[index] = Type::make_forward_declaration(no);
817 	}
818       else
819 	{
820 	  go_assert(no->is_type());
821 	  this->types_[index] = no->type_value();
822 	}
823     }
824 
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
831     {
832       type = this->read_type();
833 
834       if (no->is_type_declaration())
835 	{
836 	  // We can define the type now.
837 
838 	  no = package->add_type(type_name, type, this->location_);
839 	  Named_type* ntype = no->type_value();
840 
841 	  // This type has not yet been imported.
842 	  ntype->clear_is_visible();
843 
844 	  if (is_alias)
845 	    ntype->set_is_alias();
846 
847 	  if (!type->is_undefined() && type->interface_type() != NULL)
848 	    this->gogo_->record_interface_type(type->interface_type());
849 
850 	  type = ntype;
851 	}
852       else if (no->is_type())
853 	{
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();
859 
860 	  // Don't change the visibility of the existing type.
861 	}
862 
863       this->types_[index] = type;
864 
865       // Read the type methods.
866       if (this->match_c_string("\n"))
867 	{
868 	  this->advance(1);
869 	  while (this->match_c_string(" func"))
870 	    {
871 	      this->advance(1);
872 	      this->import_func(package);
873 	    }
874 	}
875     }
876 
877   this->require_c_string(">");
878 
879   return type;
880 }
881 
882 // Read an escape note.
883 
884 std::string
read_escape()885 Import::read_escape()
886 {
887   if (this->match_c_string(" <esc:"))
888     {
889       Stream* stream = this->stream_;
890       this->require_c_string(" <esc:");
891 
892       std::string escape = "esc:";
893       int c;
894       while (true)
895 	{
896 	  c = stream->get_char();
897 	  if (c != 'x' && !ISXDIGIT(c))
898 	    break;
899 	  escape += c;
900 	}
901 
902       if (c != '>')
903 	{
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);
911 	}
912       return escape;
913     }
914   else
915     return Escape_note::make_tag(Node::ESCAPE_UNKNOWN);
916 }
917 
918 
919 // Register the builtin types.
920 
921 void
register_builtin_types(Gogo * gogo)922 Import::register_builtin_types(Gogo* gogo)
923 {
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);
944 }
945 
946 // Register a single builtin type.
947 
948 void
register_builtin_type(Gogo * gogo,const char * name,Builtin_code code)949 Import::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
950 {
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();
957 }
958 
959 // Read an identifier from the stream.
960 
961 std::string
read_identifier()962 Import::read_identifier()
963 {
964   std::string ret;
965   Stream* stream = this->stream_;
966   int c;
967   while (true)
968     {
969       c = stream->peek_char();
970       if (c == -1 || c == ' ' || c == ';')
971 	break;
972       ret += c;
973       stream->advance(1);
974     }
975   return ret;
976 }
977 
978 // Read a name from the stream.
979 
980 std::string
read_name()981 Import::read_name()
982 {
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;
989 }
990 
991 // Turn a string into a integer with appropriate error handling.
992 
993 bool
string_to_int(const std::string & s,bool is_neg_ok,int * ret)994 Import::string_to_int(const std::string &s, bool is_neg_ok, int* ret)
995 {
996   char* end;
997   long prio = strtol(s.c_str(), &end, 10);
998   if (*end != '\0' || prio > 0x7fffffff || (prio < 0 && !is_neg_ok))
999     {
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;
1004     }
1005   *ret = prio;
1006   return true;
1007 }
1008 
1009 // Class Import::Stream.
1010 
Stream()1011 Import::Stream::Stream()
1012   : pos_(0), saw_error_(false)
1013 {
1014 }
1015 
~Stream()1016 Import::Stream::~Stream()
1017 {
1018 }
1019 
1020 // Return the next character to come from the stream.
1021 
1022 int
peek_char()1023 Import::Stream::peek_char()
1024 {
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;
1032 }
1033 
1034 // Return true if the next LENGTH characters from the stream match
1035 // BYTES
1036 
1037 bool
match_bytes(const char * bytes,size_t length)1038 Import::Stream::match_bytes(const char* bytes, size_t length)
1039 {
1040   const char* read;
1041   if (!this->do_peek(length, &read))
1042     return false;
1043   return memcmp(bytes, read, length) == 0;
1044 }
1045 
1046 // Require that the next LENGTH bytes from the stream match BYTES.
1047 
1048 void
require_bytes(Location location,const char * bytes,size_t length)1049 Import::Stream::require_bytes(Location location, const char* bytes,
1050 			      size_t length)
1051 {
1052   const char* read;
1053   if (!this->do_peek(length, &read)
1054       || memcmp(bytes, read, length) != 0)
1055     {
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;
1061     }
1062   this->advance(length);
1063 }
1064 
1065 // Class Stream_from_file.
1066 
Stream_from_file(int fd)1067 Stream_from_file::Stream_from_file(int fd)
1068   : fd_(fd), data_()
1069 {
1070   if (lseek(fd, 0, SEEK_SET) != 0)
1071     {
1072       go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1073       this->set_saw_error();
1074     }
1075 }
1076 
~Stream_from_file()1077 Stream_from_file::~Stream_from_file()
1078 {
1079   close(this->fd_);
1080 }
1081 
1082 // Read next bytes.
1083 
1084 bool
do_peek(size_t length,const char ** bytes)1085 Stream_from_file::do_peek(size_t length, const char** bytes)
1086 {
1087   if (this->data_.length() <= length)
1088     {
1089       *bytes = this->data_.data();
1090       return true;
1091     }
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);
1096 
1097   if (got < 0)
1098     {
1099       if (!this->saw_error())
1100 	go_fatal_error(Linemap::unknown_location(), "read failed: %m");
1101       this->set_saw_error();
1102       return false;
1103     }
1104 
1105   if (lseek(this->fd_, - got, SEEK_CUR) != 0)
1106     {
1107       if (!this->saw_error())
1108 	go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1109       this->set_saw_error();
1110       return false;
1111     }
1112 
1113   if (static_cast<size_t>(got) < length)
1114     return false;
1115 
1116   this->data_.assign(buf, got);
1117 
1118   *bytes = this->data_.data();
1119   return true;
1120 }
1121 
1122 // Advance.
1123 
1124 void
do_advance(size_t skip)1125 Stream_from_file::do_advance(size_t skip)
1126 {
1127   if (lseek(this->fd_, skip, SEEK_CUR) != 0)
1128     {
1129       if (!this->saw_error())
1130 	go_fatal_error(Linemap::unknown_location(), "lseek failed: %m");
1131       this->set_saw_error();
1132     }
1133   if (!this->data_.empty())
1134     {
1135       if (this->data_.length() < skip)
1136 	this->data_.erase(0, skip);
1137       else
1138 	this->data_.clear();
1139     }
1140 }
1141