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.
11 #include "go-linemap.h"
20 // This class manages importing Go declarations.
25 // The Stream class is an interface used to read the data. The
26 // caller should instantiate a child of this class.
33 // Return whether we have seen an error.
36 { return this->saw_error_; }
38 // Record that we've seen an error.
41 { this->saw_error_ = true; }
43 // Return the next character (a value from 0 to 0xff) without
44 // advancing. Returns -1 at end of stream.
48 // Look for LENGTH characters, setting *BYTES to point to them.
49 // Returns false if the bytes are not available. Does not
52 peek(size_t length, const char** bytes)
53 { return this->do_peek(length, bytes); }
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.
60 int c = this->peek_char();
65 // Return true if at the end of the stream.
68 { return this->peek_char() == -1; }
70 // Return true if the next bytes match STR.
72 match_c_string(const char* str)
73 { return this->match_bytes(str, strlen(str)); }
75 // Return true if the next LENGTH bytes match BYTES.
77 match_bytes(const char* bytes, size_t length);
79 // Give an error if the next bytes do not match STR. Advance the
80 // read position by the length of STR.
82 require_c_string(Location location, const char* str)
83 { this->require_bytes(location, str, strlen(str)); }
85 // Given an error if the next LENGTH bytes do not match BYTES.
86 // Advance the read position by LENGTH.
88 require_bytes(Location, const char* bytes, size_t length);
90 // Advance the read position by SKIP bytes.
94 this->do_advance(skip);
98 // Return the current read position. This returns int because it
99 // is more convenient in error reporting. FIXME.
102 { return static_cast<int>(this->pos_); }
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.
110 do_peek(size_t length, const char** bytes) = 0;
112 // This function should advance the current read position LENGTH
115 do_advance(size_t skip) = 0;
118 // The current read position.
120 // True if we've seen an error reading from this stream.
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.
128 open_package(const std::string& filename, Location location);
131 Import(Stream*, Location);
133 // Register the builtin types.
135 register_builtin_types(Gogo*);
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.
143 import(Gogo*, const std::string& local_name, bool is_local_name_exported);
145 // The location of the import statement.
148 { return this->location_; }
150 // Return the next character.
153 { return this->stream_->peek_char(); }
155 // Return the next character and advance.
158 { return this->stream_->get_char(); }
160 // Return true at the end of the stream.
163 { return this->stream_->at_eof(); }
165 // Return whether the next bytes match STR.
167 match_c_string(const char* str)
168 { return this->stream_->match_c_string(str); }
170 // Require that the next bytes match STR.
172 require_c_string(const char* str)
173 { this->stream_->require_c_string(this->location_, str); }
175 // Advance the stream SKIP bytes.
178 { this->stream_->advance(skip); }
180 // Read an identifier.
188 // The name used for parameters, receivers, and results in imported
190 static const char* const import_marker;
194 try_package_in_directory(const std::string&, Location);
197 try_suffixes(std::string*);
200 find_export_data(const std::string& filename, int fd, Location);
203 find_object_export_data(const std::string& filename, int fd,
204 off_t offset, Location);
206 static const int archive_magic_len = 8;
209 is_archive_magic(const char*);
212 find_archive_export_data(const std::string& filename, int fd,
215 // Read the import control functions.
217 read_import_init_fns(Gogo*);
219 // Import a constant.
227 // Import a variable.
231 // Import a function.
233 import_func(Package*);
235 // Register a single builtin type.
237 register_builtin_type(Gogo*, const char* name, Builtin_code);
239 // Get an integer from a string.
241 string_to_int(const std::string&, bool is_neg_ok, int* ret);
245 // The stream from which to read import data.
247 // The location of the import statement we are processing.
249 // The package we are importing.
251 // Whether to add new objects to the global scope, rather than to a
253 bool add_to_globals_;
254 // Mapping from negated builtin type codes to Type structures.
255 std::vector<Named_type*> builtin_types_;
256 // Mapping from exported type codes to Type structures.
257 std::vector<Type*> types_;
260 // Read import data from a string.
262 class Stream_from_string : public Import::Stream
265 Stream_from_string(const std::string& str)
271 do_peek(size_t length, const char** bytes)
273 if (this->pos_ + length > this->str_.length())
275 *bytes = this->str_.data() + this->pos_;
280 do_advance(size_t len)
281 { this->pos_ += len; }
284 // The string of data we are reading.
286 // The current position within the string.
290 // Read import data from a buffer allocated using malloc.
292 class Stream_from_buffer : public Import::Stream
295 Stream_from_buffer(char* buf, size_t length)
296 : buf_(buf), length_(length), pos_(0)
299 ~Stream_from_buffer()
300 { free(this->buf_); }
304 do_peek(size_t length, const char** bytes)
306 if (this->pos_ + length > this->length_)
308 *bytes = this->buf_ + this->pos_;
313 do_advance(size_t len)
314 { this->pos_ += len; }
317 // The data we are reading.
319 // The length of the buffer.
321 // The current position within the buffer.
325 // Read import data from an open file descriptor.
327 class Stream_from_file : public Import::Stream
330 Stream_from_file(int fd);
336 do_peek(size_t, const char**);
343 Stream_from_file(const Stream_from_file&);
344 Stream_from_file& operator=(const Stream_from_file&);
346 // The file descriptor.
348 // Data read from the file.
352 #endif // !defined(GO_IMPORT_H)