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 // Set the position, for error messages.
38 // Return whether we have seen an error.
41 { return this->saw_error_
; }
43 // Record that we've seen an 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.
53 // Look for LENGTH characters, setting *BYTES to point to them.
54 // Returns false if the bytes are not available. Does not
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.
65 int c
= this->peek_char();
70 // Return true if at the end of the stream.
73 { return this->peek_char() == -1; }
75 // Return true if the next bytes match STR.
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.
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.
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.
93 require_bytes(Location
, const char* bytes
, size_t length
);
95 // Advance the read position by SKIP bytes.
99 this->do_advance(skip
);
103 // Return the current read position. This returns int because it
104 // is more convenient in error reporting. FIXME.
107 { return static_cast<int>(this->pos_
); }
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.
115 do_peek(size_t length
, const char** bytes
) = 0;
117 // This function should advance the current read position LENGTH
120 do_advance(size_t skip
) = 0;
123 // The current read position.
125 // True if we've seen an error reading from this stream.
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.
134 open_package(const std::string
& filename
, Location location
,
135 const std::string
& relative_import_path
);
138 Import(Stream
*, Location
);
140 // Register the builtin types.
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.
150 import(Gogo
*, const std::string
& local_name
, bool is_local_name_exported
);
152 // The location of the import statement.
155 { return this->location_
; }
157 // Return the package we are importing.
160 { return this->package_
; }
162 // Return the next character.
165 { return this->stream_
->peek_char(); }
167 // Return the next character and advance.
170 { return this->stream_
->get_char(); }
172 // Return true at the end of the stream.
175 { return this->stream_
->at_eof(); }
177 // Return whether the next bytes match STR.
179 match_c_string(const char* str
)
180 { return this->stream_
->match_c_string(str
); }
182 // Require that the next bytes match STR.
184 require_c_string(const char* str
)
185 { this->stream_
->require_c_string(this->location_
, str
); }
187 // Advance the stream SKIP bytes.
190 { this->stream_
->advance(skip
); }
192 // Skip a semicolon if using an older version.
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.
205 // Read a name. This is like read_identifier, except that a "?" is
206 // returned as an empty string. This matches Export::write_name.
214 // Read an escape note.
220 try_package_in_directory(const std::string
&, Location
);
223 try_suffixes(std::string
*);
226 find_export_data(const std::string
& filename
, int fd
, Location
);
229 find_object_export_data(const std::string
& filename
, int fd
,
230 off_t offset
, Location
);
232 static const int archive_magic_len
= 8;
235 is_archive_magic(const char*);
238 find_archive_export_data(const std::string
& filename
, int fd
,
241 // Read a package line.
245 // Read an import line.
249 // Read an indirectimport line.
251 read_one_indirect_import();
253 // Read the import control functions and init graph.
255 read_import_init_fns(Gogo
*);
261 // Import a constant.
269 // Import a variable.
273 // Import a function.
275 import_func(Package
*);
277 // Parse a type definition.
279 parse_type(int index
);
281 // Read a named type and store it at this->type_[index].
283 read_named_type(int index
);
285 // Register a single builtin type.
287 register_builtin_type(Gogo
*, const char* name
, Builtin_code
);
289 // Get an integer from a string.
291 string_to_int(const std::string
&, bool is_neg_ok
, int* ret
);
293 // Get an unsigned integer from a string.
295 string_to_unsigned(const std::string
& s
, unsigned* ret
)
298 if (!this->string_to_int(s
, false, &ivalue
))
300 *ret
= static_cast<unsigned>(ivalue
);
304 // Return the version number of the export data we're reading.
306 version() const { return this->version_
; }
310 // The stream from which to read import data.
312 // The location of the import statement we are processing.
314 // The package we are importing.
316 // Whether to add new objects to the global scope, rather than to a
318 bool add_to_globals_
;
320 std::string type_data_
;
321 // Position of type data in the stream.
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
338 Stream_from_string(const std::string
& str
)
344 do_peek(size_t length
, const char** bytes
)
346 if (this->pos_
+ length
> this->str_
.length())
348 *bytes
= this->str_
.data() + this->pos_
;
353 do_advance(size_t len
)
354 { this->pos_
+= len
; }
357 // The string of data we are reading.
359 // The current position within the string.
363 // Read import data from a buffer allocated using malloc.
365 class Stream_from_buffer
: public Import::Stream
368 Stream_from_buffer(char* buf
, size_t length
)
369 : buf_(buf
), length_(length
), pos_(0)
372 ~Stream_from_buffer()
373 { free(this->buf_
); }
377 do_peek(size_t length
, const char** bytes
)
379 if (this->pos_
+ length
> this->length_
)
381 *bytes
= this->buf_
+ this->pos_
;
386 do_advance(size_t len
)
387 { this->pos_
+= len
; }
390 // The data we are reading.
392 // The length of the buffer.
394 // The current position within the buffer.
398 // Read import data from an open file descriptor.
400 class Stream_from_file
: public Import::Stream
403 Stream_from_file(int fd
);
409 do_peek(size_t, const char**);
416 Stream_from_file(const Stream_from_file
&);
417 Stream_from_file
& operator=(const Stream_from_file
&);
419 // The file descriptor.
421 // Data read from the file.
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
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()
441 do_peek(size_t length
, const char** bytes
)
443 if (this->pos_
+ length
> this->end_
)
445 *bytes
= &this->str_
[this->pos_
];
450 do_advance(size_t length
)
451 { this->pos_
+= length
; }
454 // A reference to the string we are reading from.
455 const std::string
& str_
;
456 // The current offset into the string.
458 // The index after the last byte we can read.
462 #endif // !defined(GO_IMPORT_H)