OSDN Git Service

compiler: Fix parse of (<- chan <- chan <- int)(x).
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / import.h
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 Package;
15 class Type;
16 class Named_object;
17 class Named_type;
18 class Expression;
19
20 // This class manages importing Go declarations.
21
22 class Import
23 {
24  public:
25   // The Stream class is an interface used to read the data.  The
26   // caller should instantiate a child of this class.
27   class Stream
28   {
29    public:
30     Stream();
31     virtual ~Stream();
32
33     // Return whether we have seen an error.
34     bool
35     saw_error() const
36     { return this->saw_error_; }
37
38     // Record that we've seen an error.
39     void
40     set_saw_error()
41     { this->saw_error_ = true; }
42
43     // Return the next character (a value from 0 to 0xff) without
44     // advancing.  Returns -1 at end of stream.
45     int
46     peek_char();
47
48     // Look for LENGTH characters, setting *BYTES to point to them.
49     // Returns false if the bytes are not available.  Does not
50     // advance.
51     bool
52     peek(size_t length, const char** bytes)
53     { return this->do_peek(length, bytes); }
54
55     // Return the next character (a value from 0 to 0xff) and advance
56     // the read position by 1.  Returns -1 at end of stream.
57     int
58     get_char()
59     {
60       int c = this->peek_char();
61       this->advance(1);
62       return c;
63     }
64
65     // Return true if at the end of the stream.
66     bool
67     at_eof()
68     { return this->peek_char() == -1; }
69
70     // Return true if the next bytes match STR.
71     bool
72     match_c_string(const char* str)
73     { return this->match_bytes(str, strlen(str)); }
74
75     // Return true if the next LENGTH bytes match BYTES.
76     bool
77     match_bytes(const char* bytes, size_t length);
78
79     // Give an error if the next bytes do not match STR.  Advance the
80     // read position by the length of STR.
81     void
82     require_c_string(Location location, const char* str)
83     { this->require_bytes(location, str, strlen(str)); }
84
85     // Given an error if the next LENGTH bytes do not match BYTES.
86     // Advance the read position by LENGTH.
87     void
88     require_bytes(Location, const char* bytes, size_t length);
89
90     // Advance the read position by SKIP bytes.
91     void
92     advance(size_t skip)
93     {
94       this->do_advance(skip);
95       this->pos_ += skip;
96     }
97
98     // Return the current read position.  This returns int because it
99     // is more convenient in error reporting.  FIXME.
100     int
101     pos()
102     { return static_cast<int>(this->pos_); }
103
104    protected:
105     // This function should set *BYTES to point to a buffer holding
106     // the LENGTH bytes at the current read position.  It should
107     // return false if the bytes are not available.  This should not
108     // change the current read position.
109     virtual bool
110     do_peek(size_t length, const char** bytes) = 0;
111
112     // This function should advance the current read position LENGTH
113     // bytes.
114     virtual void
115     do_advance(size_t skip) = 0;
116
117    private:
118     // The current read position.
119     size_t pos_;
120     // True if we've seen an error reading from this stream.
121     bool saw_error_;
122   };
123
124   // Find import data.  This searches the file system for FILENAME and
125   // returns a pointer to a Stream object to read the data that it
126   // exports.  LOCATION is the location of the import statement.
127   static Stream*
128   open_package(const std::string& filename, Location location);
129
130   // Constructor.
131   Import(Stream*, Location);
132
133   // Register the builtin types.
134   void
135   register_builtin_types(Gogo*);
136
137   // Import everything defined in the stream.  LOCAL_NAME is the local
138   // name to be used for bindings; if it is the string "." then
139   // bindings should be inserted in the global scope.  If LOCAL_NAME
140   // is the empty string then the name of the package itself is the
141   // local name.  This returns the imported package, or NULL on error.
142   Package*
143   import(Gogo*, const std::string& local_name, bool is_local_name_exported);
144
145   // The location of the import statement.
146   Location
147   location() const
148   { return this->location_; }
149
150   // Return the next character.
151   int
152   peek_char()
153   { return this->stream_->peek_char(); }
154
155   // Return the next character and advance.
156   int
157   get_char()
158   { return this->stream_->get_char(); }
159
160   // Return true at the end of the stream.
161   bool
162   at_eof()
163   { return this->stream_->at_eof(); }
164
165   // Return whether the next bytes match STR.
166   bool
167   match_c_string(const char* str)
168   { return this->stream_->match_c_string(str); }
169
170   // Require that the next bytes match STR.
171   void
172   require_c_string(const char* str)
173   { this->stream_->require_c_string(this->location_, str); }
174
175   // Advance the stream SKIP bytes.
176   void
177   advance(size_t skip)
178   { this->stream_->advance(skip); }
179
180   // Read an identifier.
181   std::string
182   read_identifier();
183
184   // Read a name.  This is like read_identifier, except that a "?" is
185   // returned as an empty string.  This matches Export::write_name.
186   std::string
187   read_name();
188
189   // Read a type.
190   Type*
191   read_type();
192
193  private:
194   static Stream*
195   try_package_in_directory(const std::string&, Location);
196
197   static int
198   try_suffixes(std::string*);
199
200   static Stream*
201   find_export_data(const std::string& filename, int fd, Location);
202
203   static Stream*
204   find_object_export_data(const std::string& filename, int fd,
205                           off_t offset, Location);
206
207   static const int archive_magic_len = 8;
208
209   static bool
210   is_archive_magic(const char*);
211
212   static Stream*
213   find_archive_export_data(const std::string& filename, int fd,
214                            Location);
215
216   // Read an import line.
217   void
218   read_one_import();
219
220   // Read the import control functions.
221   void
222   read_import_init_fns(Gogo*);
223
224   // Import a constant.
225   void
226   import_const();
227
228   // Import a type.
229   void
230   import_type();
231
232   // Import a variable.
233   void
234   import_var();
235
236   // Import a function.
237   Named_object*
238   import_func(Package*);
239
240   // Register a single builtin type.
241   void
242   register_builtin_type(Gogo*, const char* name, Builtin_code);
243
244   // Get an integer from a string.
245   bool
246   string_to_int(const std::string&, bool is_neg_ok, int* ret);
247
248   // The general IR.
249   Gogo* gogo_;
250   // The stream from which to read import data.
251   Stream* stream_;
252   // The location of the import statement we are processing.
253   Location location_;
254   // The package we are importing.
255   Package* package_;
256   // Whether to add new objects to the global scope, rather than to a
257   // package scope.
258   bool add_to_globals_;
259   // Mapping from negated builtin type codes to Type structures.
260   std::vector<Named_type*> builtin_types_;
261   // Mapping from exported type codes to Type structures.
262   std::vector<Type*> types_;
263 };
264
265 // Read import data from a string.
266
267 class Stream_from_string : public Import::Stream
268 {
269  public:
270   Stream_from_string(const std::string& str)
271     : str_(str), pos_(0)
272   { }
273
274  protected:
275   bool
276   do_peek(size_t length, const char** bytes)
277   {
278     if (this->pos_ + length > this->str_.length())
279       return false;
280     *bytes = this->str_.data() + this->pos_;
281     return true;
282   }
283
284   void
285   do_advance(size_t len)
286   { this->pos_ += len; }
287
288  private:
289   // The string of data we are reading.
290   std::string str_;
291   // The current position within the string.
292   size_t pos_;
293 };
294
295 // Read import data from a buffer allocated using malloc.
296
297 class Stream_from_buffer : public Import::Stream
298 {
299  public:
300   Stream_from_buffer(char* buf, size_t length)
301     : buf_(buf), length_(length), pos_(0)
302   { }
303
304   ~Stream_from_buffer()
305   { free(this->buf_); }
306
307  protected:
308   bool
309   do_peek(size_t length, const char** bytes)
310   {
311     if (this->pos_ + length > this->length_)
312       return false;
313     *bytes = this->buf_ + this->pos_;
314     return true;
315   }
316
317   void
318   do_advance(size_t len)
319   { this->pos_ += len; }
320
321  private:
322   // The data we are reading.
323   char* buf_;
324   // The length of the buffer.
325   size_t length_;
326   // The current position within the buffer.
327   size_t pos_;
328 };
329
330 // Read import data from an open file descriptor.
331
332 class Stream_from_file : public Import::Stream
333 {
334  public:
335   Stream_from_file(int fd);
336
337   ~Stream_from_file();
338
339  protected:
340   bool
341   do_peek(size_t, const char**);
342
343   void
344   do_advance(size_t);
345
346  private:
347   // No copying.
348   Stream_from_file(const Stream_from_file&);
349   Stream_from_file& operator=(const Stream_from_file&);
350
351   // The file descriptor.
352   int fd_;
353   // Data read from the file.
354   std::string data_;
355 };
356
357 #endif // !defined(GO_IMPORT_H)