compiler: export indexed type data, read unexported types lazily
[official-gcc.git] / gcc / go / gofrontend / import.h
blobcc7703bcc943871f7ec98586f999dc73309042f1
1 // import.h -- Go frontend import declarations. -*- C++ -*-
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 #ifndef GO_IMPORT_H
8 #define GO_IMPORT_H
10 #include "export.h"
11 #include "go-linemap.h"
13 class Gogo;
14 class Package;
15 class Type;
16 class Named_object;
17 class Named_type;
18 class Expression;
20 // This class manages importing Go declarations.
22 class Import
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
29 public:
30 Stream();
31 virtual ~Stream();
33 // Set the position, for error messages.
34 void
35 set_pos(int pos)
36 { this->pos_ = pos; }
38 // Return whether we have seen an error.
39 bool
40 saw_error() const
41 { return this->saw_error_; }
43 // Record that we've seen an error.
44 void
45 set_saw_error()
46 { this->saw_error_ = true; }
48 // Return the next character (a value from 0 to 0xff) without
49 // advancing. Returns -1 at end of stream.
50 int
51 peek_char();
53 // Look for LENGTH characters, setting *BYTES to point to them.
54 // Returns false if the bytes are not available. Does not
55 // advance.
56 bool
57 peek(size_t length, const char** bytes)
58 { return this->do_peek(length, bytes); }
60 // Return the next character (a value from 0 to 0xff) and advance
61 // the read position by 1. Returns -1 at end of stream.
62 int
63 get_char()
65 int c = this->peek_char();
66 this->advance(1);
67 return c;
70 // Return true if at the end of the stream.
71 bool
72 at_eof()
73 { return this->peek_char() == -1; }
75 // Return true if the next bytes match STR.
76 bool
77 match_c_string(const char* str)
78 { return this->match_bytes(str, strlen(str)); }
80 // Return true if the next LENGTH bytes match BYTES.
81 bool
82 match_bytes(const char* bytes, size_t length);
84 // Give an error if the next bytes do not match STR. Advance the
85 // read position by the length of STR.
86 void
87 require_c_string(Location location, const char* str)
88 { this->require_bytes(location, str, strlen(str)); }
90 // Given an error if the next LENGTH bytes do not match BYTES.
91 // Advance the read position by LENGTH.
92 void
93 require_bytes(Location, const char* bytes, size_t length);
95 // Advance the read position by SKIP bytes.
96 void
97 advance(size_t skip)
99 this->do_advance(skip);
100 this->pos_ += skip;
103 // Return the current read position. This returns int because it
104 // is more convenient in error reporting. FIXME.
106 pos()
107 { return static_cast<int>(this->pos_); }
109 protected:
110 // This function should set *BYTES to point to a buffer holding
111 // the LENGTH bytes at the current read position. It should
112 // return false if the bytes are not available. This should not
113 // change the current read position.
114 virtual bool
115 do_peek(size_t length, const char** bytes) = 0;
117 // This function should advance the current read position LENGTH
118 // bytes.
119 virtual void
120 do_advance(size_t skip) = 0;
122 private:
123 // The current read position.
124 size_t pos_;
125 // True if we've seen an error reading from this stream.
126 bool saw_error_;
129 // Find import data. This searches the file system for FILENAME and
130 // returns a pointer to a Stream object to read the data that it
131 // exports. LOCATION is the location of the import statement.
132 // RELATIVE_IMPORT_PATH is used as a prefix for a relative import.
133 static Stream*
134 open_package(const std::string& filename, Location location,
135 const std::string& relative_import_path);
137 // Constructor.
138 Import(Stream*, Location);
140 // Register the builtin types.
141 void
142 register_builtin_types(Gogo*);
144 // Import everything defined in the stream. LOCAL_NAME is the local
145 // name to be used for bindings; if it is the string "." then
146 // bindings should be inserted in the global scope. If LOCAL_NAME
147 // is the empty string then the name of the package itself is the
148 // local name. This returns the imported package, or NULL on error.
149 Package*
150 import(Gogo*, const std::string& local_name, bool is_local_name_exported);
152 // The location of the import statement.
153 Location
154 location() const
155 { return this->location_; }
157 // Return the package we are importing.
158 Package*
159 package() const
160 { return this->package_; }
162 // Return the next character.
164 peek_char()
165 { return this->stream_->peek_char(); }
167 // Return the next character and advance.
169 get_char()
170 { return this->stream_->get_char(); }
172 // Return true at the end of the stream.
173 bool
174 at_eof()
175 { return this->stream_->at_eof(); }
177 // Return whether the next bytes match STR.
178 bool
179 match_c_string(const char* str)
180 { return this->stream_->match_c_string(str); }
182 // Require that the next bytes match STR.
183 void
184 require_c_string(const char* str)
185 { this->stream_->require_c_string(this->location_, str); }
187 // Advance the stream SKIP bytes.
188 void
189 advance(size_t skip)
190 { this->stream_->advance(skip); }
192 // Skip a semicolon if using an older version.
193 void
194 require_semicolon_if_old_version()
196 if (this->version_ == EXPORT_FORMAT_V1
197 || this->version_ == EXPORT_FORMAT_V2)
198 this->require_c_string(";");
201 // Read an identifier.
202 std::string
203 read_identifier();
205 // Read a name. This is like read_identifier, except that a "?" is
206 // returned as an empty string. This matches Export::write_name.
207 std::string
208 read_name();
210 // Read a type.
211 Type*
212 read_type();
214 // Read an escape note.
215 std::string
216 read_escape();
218 private:
219 static Stream*
220 try_package_in_directory(const std::string&, Location);
222 static int
223 try_suffixes(std::string*);
225 static Stream*
226 find_export_data(const std::string& filename, int fd, Location);
228 static Stream*
229 find_object_export_data(const std::string& filename, int fd,
230 off_t offset, Location);
232 static const int archive_magic_len = 8;
234 static bool
235 is_archive_magic(const char*);
237 static Stream*
238 find_archive_export_data(const std::string& filename, int fd,
239 Location);
241 // Read a package line.
242 void
243 read_one_package();
245 // Read an import line.
246 void
247 read_one_import();
249 // Read an indirectimport line.
250 void
251 read_one_indirect_import();
253 // Read the import control functions and init graph.
254 void
255 read_import_init_fns(Gogo*);
257 // Read the types.
258 bool
259 read_types();
261 // Import a constant.
262 void
263 import_const();
265 // Import a type.
266 void
267 import_type();
269 // Import a variable.
270 void
271 import_var();
273 // Import a function.
274 Named_object*
275 import_func(Package*);
277 // Parse a type definition.
278 bool
279 parse_type(int index);
281 // Read a named type and store it at this->type_[index].
282 Type*
283 read_named_type(int index);
285 // Register a single builtin type.
286 void
287 register_builtin_type(Gogo*, const char* name, Builtin_code);
289 // Get an integer from a string.
290 bool
291 string_to_int(const std::string&, bool is_neg_ok, int* ret);
293 // Get an unsigned integer from a string.
294 bool
295 string_to_unsigned(const std::string& s, unsigned* ret)
297 int ivalue;
298 if (!this->string_to_int(s, false, &ivalue))
299 return false;
300 *ret = static_cast<unsigned>(ivalue);
301 return true;
304 // Return the version number of the export data we're reading.
305 Export_data_version
306 version() const { return this->version_; }
308 // The general IR.
309 Gogo* gogo_;
310 // The stream from which to read import data.
311 Stream* stream_;
312 // The location of the import statement we are processing.
313 Location location_;
314 // The package we are importing.
315 Package* package_;
316 // Whether to add new objects to the global scope, rather than to a
317 // package scope.
318 bool add_to_globals_;
319 // All type data.
320 std::string type_data_;
321 // Position of type data in the stream.
322 int type_pos_;
323 // Mapping from type code to offset/length in type_data_.
324 std::vector<std::pair<size_t, size_t> > type_offsets_;
325 // Mapping from negated builtin type codes to Type structures.
326 std::vector<Named_type*> builtin_types_;
327 // Mapping from exported type codes to Type structures.
328 std::vector<Type*> types_;
329 // Version of export data we're reading.
330 Export_data_version version_;
333 // Read import data from a string.
335 class Stream_from_string : public Import::Stream
337 public:
338 Stream_from_string(const std::string& str)
339 : str_(str), pos_(0)
342 protected:
343 bool
344 do_peek(size_t length, const char** bytes)
346 if (this->pos_ + length > this->str_.length())
347 return false;
348 *bytes = this->str_.data() + this->pos_;
349 return true;
352 void
353 do_advance(size_t len)
354 { this->pos_ += len; }
356 private:
357 // The string of data we are reading.
358 std::string str_;
359 // The current position within the string.
360 size_t pos_;
363 // Read import data from a buffer allocated using malloc.
365 class Stream_from_buffer : public Import::Stream
367 public:
368 Stream_from_buffer(char* buf, size_t length)
369 : buf_(buf), length_(length), pos_(0)
372 ~Stream_from_buffer()
373 { free(this->buf_); }
375 protected:
376 bool
377 do_peek(size_t length, const char** bytes)
379 if (this->pos_ + length > this->length_)
380 return false;
381 *bytes = this->buf_ + this->pos_;
382 return true;
385 void
386 do_advance(size_t len)
387 { this->pos_ += len; }
389 private:
390 // The data we are reading.
391 char* buf_;
392 // The length of the buffer.
393 size_t length_;
394 // The current position within the buffer.
395 size_t pos_;
398 // Read import data from an open file descriptor.
400 class Stream_from_file : public Import::Stream
402 public:
403 Stream_from_file(int fd);
405 ~Stream_from_file();
407 protected:
408 bool
409 do_peek(size_t, const char**);
411 void
412 do_advance(size_t);
414 private:
415 // No copying.
416 Stream_from_file(const Stream_from_file&);
417 Stream_from_file& operator=(const Stream_from_file&);
419 // The file descriptor.
420 int fd_;
421 // Data read from the file.
422 std::string data_;
425 // Read import data from an offset into a std::string. This uses a
426 // reference to the string, to avoid copying, so the string must be
427 // kept alive through some other mechanism.
429 class Stream_from_string_ref : public Import::Stream
431 public:
432 Stream_from_string_ref(const std::string& str, size_t offset, size_t length)
433 : str_(str), pos_(offset), end_(offset + length)
436 ~Stream_from_string_ref()
439 protected:
440 bool
441 do_peek(size_t length, const char** bytes)
443 if (this->pos_ + length > this->end_)
444 return false;
445 *bytes = &this->str_[this->pos_];
446 return true;
449 void
450 do_advance(size_t length)
451 { this->pos_ += length; }
453 private:
454 // A reference to the string we are reading from.
455 const std::string& str_;
456 // The current offset into the string.
457 size_t pos_;
458 // The index after the last byte we can read.
459 size_t end_;
462 #endif // !defined(GO_IMPORT_H)