1 // import.h -- Go frontend import declarations.     -*- C++ -*-
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 #ifndef GO_IMPORT_H
8 #define GO_IMPORT_H
9 
10 #include "export.h"
11 #include "go-linemap.h"
12 
13 class Gogo;
14 class Block;
15 class Package;
16 class Type;
17 class Named_object;
18 class Named_type;
19 class Expression;
20 class Import_function_body;
21 
22 // Expressions can be imported either directly from import data (for
23 // simple constant expressions that can appear in a const declaration
24 // or as an array length in a type definition) or from an exported
25 // function body (for an inlinable function).  These two cases happen
26 // at different points in the compilation and have different
27 // requirements, so it's not easy to unify them.  Import_expression is
28 // an abstract interface that permits the expression import code to
29 // work at either point.  When importing expressions that only occur
30 // for an inlinable function, the ifb method is available to get the
31 // full Import_function_body.
32 
33 class Import_expression
34 {
35  public:
36   // Return the import function body.  This should only be called for
37   // expressions that can not appear outside of an inlinable function
38   // body.
39   virtual Import_function_body*
40   ifb() = 0;
41 
42   // The location to report in an error message.
43   virtual Location
44   location() const = 0;
45 
46   // Peek at the next character in the input, returning a value from 0
47   // to 0xff.  Returns -1 at end of stream.
48   virtual int
49   peek_char() = 0;
50 
51   // Return the next character and advance.
52   virtual int
53   get_char() = 0;
54 
55   // Return true if the next bytes match STR.
56   virtual bool
57   match_c_string(const char* str) = 0;
58 
59   // Require that the next bytes match STR.
60   virtual void
61   require_c_string(const char* str) = 0;
62 
63   // Advance the stream SKIP bytes.
64   virtual void
65   advance(size_t skip) = 0;
66 
67   // Read an identifier.
68   virtual std::string
69   read_identifier() = 0;
70 
71   // Read a type.
72   virtual Type*
73   read_type() = 0;
74 
75   // Return the version number of the export data we're reading.
76   virtual Export_data_version
77   version() const = 0;
78 };
79 
80 // This class manages importing Go declarations.
81 
82 class Import : public Import_expression
83 {
84  public:
85   // The Stream class is an interface used to read the data.  The
86   // caller should instantiate a child of this class.
87   class Stream
88   {
89    public:
90     Stream();
91     virtual ~Stream();
92 
93     // Set the position, for error messages.
94     void
set_pos(int pos)95     set_pos(int pos)
96     { this->pos_ = pos; }
97 
98     // Return whether we have seen an error.
99     bool
saw_error()100     saw_error() const
101     { return this->saw_error_; }
102 
103     // Record that we've seen an error.
104     void
set_saw_error()105     set_saw_error()
106     { this->saw_error_ = true; }
107 
108     // Return the next character (a value from 0 to 0xff) without
109     // advancing.  Returns -1 at end of stream.
110     int
111     peek_char();
112 
113     // Look for LENGTH characters, setting *BYTES to point to them.
114     // Returns false if the bytes are not available.  Does not
115     // advance.
116     bool
peek(size_t length,const char ** bytes)117     peek(size_t length, const char** bytes)
118     { return this->do_peek(length, bytes); }
119 
120     // Return the next character (a value from 0 to 0xff) and advance
121     // the read position by 1.  Returns -1 at end of stream.
122     int
get_char()123     get_char()
124     {
125       int c = this->peek_char();
126       this->advance(1);
127       return c;
128     }
129 
130     // Return true if at the end of the stream.
131     bool
at_eof()132     at_eof()
133     { return this->peek_char() == -1; }
134 
135     // Return true if the next bytes match STR.
136     bool
match_c_string(const char * str)137     match_c_string(const char* str)
138     { return this->match_bytes(str, strlen(str)); }
139 
140     // Return true if the next LENGTH bytes match BYTES.
141     bool
142     match_bytes(const char* bytes, size_t length);
143 
144     // Give an error if the next bytes do not match STR.  Advance the
145     // read position by the length of STR.
146     void
require_c_string(Location location,const char * str)147     require_c_string(Location location, const char* str)
148     { this->require_bytes(location, str, strlen(str)); }
149 
150     // Given an error if the next LENGTH bytes do not match BYTES.
151     // Advance the read position by LENGTH.
152     void
153     require_bytes(Location, const char* bytes, size_t length);
154 
155     // Advance the read position by SKIP bytes.
156     void
advance(size_t skip)157     advance(size_t skip)
158     {
159       this->do_advance(skip);
160       this->pos_ += skip;
161     }
162 
163     // Return the current read position.  This returns int because it
164     // is more convenient in error reporting.  FIXME.
165     int
pos()166     pos()
167     { return static_cast<int>(this->pos_); }
168 
169    protected:
170     // This function should set *BYTES to point to a buffer holding
171     // the LENGTH bytes at the current read position.  It should
172     // return false if the bytes are not available.  This should not
173     // change the current read position.
174     virtual bool
175     do_peek(size_t length, const char** bytes) = 0;
176 
177     // This function should advance the current read position LENGTH
178     // bytes.
179     virtual void
180     do_advance(size_t skip) = 0;
181 
182    private:
183     // The current read position.
184     size_t pos_;
185     // True if we've seen an error reading from this stream.
186     bool saw_error_;
187   };
188 
189   // Find import data.  This searches the file system for FILENAME and
190   // returns a pointer to a Stream object to read the data that it
191   // exports.  LOCATION is the location of the import statement.
192   // RELATIVE_IMPORT_PATH is used as a prefix for a relative import.
193   static Stream*
194   open_package(const std::string& filename, Location location,
195 	       const std::string& relative_import_path);
196 
197   // Constructor.
198   Import(Stream*, Location);
199 
~Import()200   virtual ~Import()
201   {}
202 
203   // Register the builtin types.
204   void
205   register_builtin_types(Gogo*);
206 
207   // Import everything defined in the stream.  LOCAL_NAME is the local
208   // name to be used for bindings; if it is the string "." then
209   // bindings should be inserted in the global scope.  If LOCAL_NAME
210   // is the empty string then the name of the package itself is the
211   // local name.  This returns the imported package, or NULL on error.
212   Package*
213   import(Gogo*, const std::string& local_name, bool is_local_name_exported);
214 
215   // The location of the import statement.
216   Location
location()217   location() const
218   { return this->location_; }
219 
220   // Return the package we are importing.
221   Package*
package()222   package() const
223   { return this->package_; }
224 
225   // Return the next character.
226   int
peek_char()227   peek_char()
228   { return this->stream_->peek_char(); }
229 
230   // Return the next character and advance.
231   int
get_char()232   get_char()
233   { return this->stream_->get_char(); }
234 
235   // Read LENGTH characters into a string and advance past them.  On
236   // EOF reports an error and returns an empty string.
237   std::string
238   read(size_t length);
239 
240   // Return true at the end of the stream.
241   bool
at_eof()242   at_eof()
243   { return this->stream_->at_eof(); }
244 
245   // Return whether the next bytes match STR.
246   bool
match_c_string(const char * str)247   match_c_string(const char* str)
248   { return this->stream_->match_c_string(str); }
249 
250   // Require that the next bytes match STR.
251   void
require_c_string(const char * str)252   require_c_string(const char* str)
253   { this->stream_->require_c_string(this->location_, str); }
254 
255   // Advance the stream SKIP bytes.
256   void
advance(size_t skip)257   advance(size_t skip)
258   { this->stream_->advance(skip); }
259 
260   // Return the version number of the export data we're reading.
261   Export_data_version
version()262   version() const { return this->version_; }
263 
264   // Skip a semicolon if using an older version.
265   void
require_semicolon_if_old_version()266   require_semicolon_if_old_version()
267   {
268     if (this->version_ == EXPORT_FORMAT_V1
269 	|| this->version_ == EXPORT_FORMAT_V2)
270       this->require_c_string(";");
271   }
272 
273   // Read an identifier.
274   std::string
275   read_identifier();
276 
277   // Read a name.  This is like read_identifier, except that a "?" is
278   // returned as an empty string.  This matches Export::write_name.
279   std::string
280   read_name();
281 
282   // Read a type.
283   Type*
284   read_type();
285 
286   // Return the type for a type index.  INPUT_NAME and INPUT_OFFSET
287   // are only for error reporting.  PARSED is set to whether we parsed
288   // the type information for a new type.
289   Type*
290   type_for_index(int index, const std::string& input_name,
291 		 size_t input_offset, bool* parsed);
292 
293   // Read an escape note.
294   std::string
295   read_escape();
296 
297   // Clear the stream when it is no longer accessible.
298   void
clear_stream()299   clear_stream()
300   { this->stream_ = NULL; }
301 
302   // Just so that Import implements Import_expression.
303   Import_function_body*
ifb()304   ifb()
305   { return NULL; }
306 
307  private:
308   static Stream*
309   try_package_in_directory(const std::string&, Location);
310 
311   static int
312   try_suffixes(std::string*);
313 
314   static Stream*
315   find_export_data(const std::string& filename, int fd, Location);
316 
317   static Stream*
318   find_object_export_data(const std::string& filename, int fd,
319 			  off_t offset, Location);
320 
321   static const int archive_magic_len = 8;
322 
323   static bool
324   is_archive_magic(const char*);
325 
326   static Stream*
327   find_archive_export_data(const std::string& filename, int fd,
328 			   Location);
329 
330   // Read a package line.
331   void
332   read_one_package();
333 
334   // Read an import line.
335   void
336   read_one_import();
337 
338   // Read an indirectimport line.
339   void
340   read_one_indirect_import();
341 
342   // Read the import control functions and init graph.
343   void
344   read_import_init_fns(Gogo*);
345 
346   // Read the types.
347   bool
348   read_types();
349 
350   // Import a constant.
351   void
352   import_const();
353 
354   // Import a type.
355   void
356   import_type();
357 
358   // Import a variable.
359   void
360   import_var();
361 
362   // Import a function.
363   Named_object*
364   import_func(Package*);
365 
366   // Parse a type definition.
367   bool
368   parse_type(int index);
369 
370   // Read a named type and store it at this->type_[index].
371   Type*
372   read_named_type(int index);
373 
374   // Register a single builtin type.
375   void
376   register_builtin_type(Gogo*, const char* name, Builtin_code);
377 
378   // Get an integer from a string.
379   bool
380   string_to_int(const std::string&, bool is_neg_ok, int* ret);
381 
382   // Get an unsigned integer from a string.
383   bool
string_to_unsigned(const std::string & s,unsigned * ret)384   string_to_unsigned(const std::string& s, unsigned* ret)
385   {
386     int ivalue;
387     if (!this->string_to_int(s, false, &ivalue))
388       return false;
389     *ret = static_cast<unsigned>(ivalue);
390     return true;
391   }
392 
393   // The general IR.
394   Gogo* gogo_;
395   // The stream from which to read import data.
396   Stream* stream_;
397   // The location of the import statement we are processing.
398   Location location_;
399   // The package we are importing.
400   Package* package_;
401   // Whether to add new objects to the global scope, rather than to a
402   // package scope.
403   bool add_to_globals_;
404   // All type data.
405   std::string type_data_;
406   // Position of type data in the stream.
407   int type_pos_;
408   // Mapping from type code to offset/length in type_data_.
409   std::vector<std::pair<size_t, size_t> > type_offsets_;
410   // Mapping from negated builtin type codes to Type structures.
411   std::vector<Named_type*> builtin_types_;
412   // Mapping from exported type codes to Type structures.
413   std::vector<Type*> types_;
414   // Version of export data we're reading.
415   Export_data_version version_;
416 };
417 
418 // Read import data from a string.
419 
420 class Stream_from_string : public Import::Stream
421 {
422  public:
Stream_from_string(const std::string & str)423   Stream_from_string(const std::string& str)
424     : str_(str), pos_(0)
425   { }
426 
427  protected:
428   bool
do_peek(size_t length,const char ** bytes)429   do_peek(size_t length, const char** bytes)
430   {
431     if (this->pos_ + length > this->str_.length())
432       return false;
433     *bytes = this->str_.data() + this->pos_;
434     return true;
435   }
436 
437   void
do_advance(size_t len)438   do_advance(size_t len)
439   { this->pos_ += len; }
440 
441  private:
442   // The string of data we are reading.
443   std::string str_;
444   // The current position within the string.
445   size_t pos_;
446 };
447 
448 // Read import data from a buffer allocated using malloc.
449 
450 class Stream_from_buffer : public Import::Stream
451 {
452  public:
Stream_from_buffer(char * buf,size_t length)453   Stream_from_buffer(char* buf, size_t length)
454     : buf_(buf), length_(length), pos_(0)
455   { }
456 
~Stream_from_buffer()457   ~Stream_from_buffer()
458   { free(this->buf_); }
459 
460  protected:
461   bool
do_peek(size_t length,const char ** bytes)462   do_peek(size_t length, const char** bytes)
463   {
464     if (this->pos_ + length > this->length_)
465       return false;
466     *bytes = this->buf_ + this->pos_;
467     return true;
468   }
469 
470   void
do_advance(size_t len)471   do_advance(size_t len)
472   { this->pos_ += len; }
473 
474  private:
475   // The data we are reading.
476   char* buf_;
477   // The length of the buffer.
478   size_t length_;
479   // The current position within the buffer.
480   size_t pos_;
481 };
482 
483 // Read import data from an open file descriptor.
484 
485 class Stream_from_file : public Import::Stream
486 {
487  public:
488   Stream_from_file(int fd);
489 
490   ~Stream_from_file();
491 
492  protected:
493   bool
494   do_peek(size_t, const char**);
495 
496   void
497   do_advance(size_t);
498 
499  private:
500   // No copying.
501   Stream_from_file(const Stream_from_file&);
502   Stream_from_file& operator=(const Stream_from_file&);
503 
504   // The file descriptor.
505   int fd_;
506   // Data read from the file.
507   std::string data_;
508 };
509 
510 // Read import data from an offset into a std::string.  This uses a
511 // reference to the string, to avoid copying, so the string must be
512 // kept alive through some other mechanism.
513 
514 class Stream_from_string_ref : public Import::Stream
515 {
516  public:
Stream_from_string_ref(const std::string & str,size_t offset,size_t length)517   Stream_from_string_ref(const std::string& str, size_t offset, size_t length)
518     : str_(str), pos_(offset), end_(offset + length)
519   { }
520 
~Stream_from_string_ref()521   ~Stream_from_string_ref()
522   {}
523 
524  protected:
525   bool
do_peek(size_t length,const char ** bytes)526   do_peek(size_t length, const char** bytes)
527   {
528     if (this->pos_ + length > this->end_)
529       return false;
530     *bytes = &this->str_[this->pos_];
531     return true;
532   }
533 
534   void
do_advance(size_t length)535   do_advance(size_t length)
536   { this->pos_ += length; }
537 
538  private:
539   // A reference to the string we are reading from.
540   const std::string& str_;
541   // The current offset into the string.
542   size_t pos_;
543   // The index after the last byte we can read.
544   size_t end_;
545 };
546 
547 // Class to manage importing a function body.  This is passed around
548 // to Statements and Expressions.  It parses the function into the IR.
549 
550 class Import_function_body : public Import_expression
551 {
552  public:
Import_function_body(Gogo * gogo,Import * imp,Named_object * named_object,const std::string & body,size_t off,Block * block,int indent)553   Import_function_body(Gogo* gogo, Import* imp, Named_object* named_object,
554 		       const std::string& body, size_t off, Block* block,
555 		       int indent)
556     : gogo_(gogo), imp_(imp), named_object_(named_object), body_(body),
557       off_(off), block_(block), indent_(indent), saw_error_(false)
558   { }
559 
560   // The IR.
561   Gogo*
gogo()562   gogo()
563   { return this->gogo_; }
564 
565   // The location to report in an error message.
566   Location
location()567   location() const
568   { return this->imp_->location(); }
569 
570   // A reference to the body we are reading.
571   const std::string&
body()572   body() const
573   { return this->body_; }
574 
575   // The current offset into the body.
576   size_t
off()577   off()
578   { return this->off_; }
579 
580   // Update the offset into the body.
581   void
set_off(size_t off)582   set_off(size_t off)
583   { this->off_ = off; }
584 
585   // Advance the offset by SKIP bytes.
586   void
advance(size_t skip)587   advance(size_t skip)
588   { this->off_ += skip; }
589 
590   // The current block.
591   Block*
block()592   block()
593   { return this->block_; }
594 
595   // The current indentation.
596   int
indent()597   indent() const
598   { return this->indent_; }
599 
600   // Increment the indentation level.
601   void
increment_indent()602   increment_indent()
603   { ++this->indent_; }
604 
605   // Decrement the indentation level.
606   void
decrement_indent()607   decrement_indent()
608   { --this->indent_; }
609 
610   // The name of the function we are parsing.
611   const std::string&
612   name() const;
613 
614   // Return the next character in the input stream, or -1 at the end.
615   int
peek_char()616   peek_char()
617   {
618     if (this->body_.length() <= this->off_)
619       return -1;
620     return static_cast<unsigned char>(this->body_[this->off_]);
621   }
622 
623   // Return the next character and advance.
624   int
get_char()625   get_char()
626   {
627     if (this->body_.length() <= this->off_)
628       return -1;
629     int c = static_cast<unsigned char>(this->body_[this->off_]);
630     this->off_++;
631     return c;
632   }
633 
634   // Return whether the C string matches the current body position.
635   bool
match_c_string(const char * str)636   match_c_string(const char* str)
637   {
638     size_t len = strlen(str);
639     return (this->body_.length() >= this->off_ + len
640 	    && this->body_.compare(this->off_, len, str) == 0);
641   }
642 
643   // Give an error if the next bytes do not match STR.  Advance the
644   // offset by the length of STR.
645   void
646   require_c_string(const char* str);
647 
648   // Read an identifier.
649   std::string
650   read_identifier();
651 
652   // Read a type.
653   Type*
654   read_type();
655 
656   Export_data_version
version()657   version() const
658   { return this->imp_->version(); }
659 
660   // Implement Import_expression.
661   Import_function_body*
ifb()662   ifb()
663   { return this; }
664 
665   // Return whether we have seen an error.
666   bool
saw_error()667   saw_error() const
668   { return this->saw_error_; }
669 
670   // Record that we have seen an error.
671   void
set_saw_error()672   set_saw_error()
673   { this->saw_error_ = true; }
674 
675  private:
676   // The IR.
677   Gogo* gogo_;
678   // The importer.
679   Import* imp_;
680   // The function we are parsing.
681   Named_object* named_object_;
682   // The exported data we are parsing.  Note that this is a reference;
683   // the body string must laster longer than this object.
684   const std::string& body_;
685   // The current offset into body_.
686   size_t off_;
687   // Current block.
688   Block* block_;
689   // Current expected indentation level.
690   int indent_;
691   // Whether we've seen an error.  Used to avoid reporting excess
692   // errors.
693   bool saw_error_;
694 };
695 
696 #endif // !defined(GO_IMPORT_H)
697