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 class Import_function_body
;
21 class Temporary_statement
;
23 class Finalize_methods
;
25 // Expressions can be imported either directly from import data (for
26 // simple constant expressions that can appear in a const declaration
27 // or as an array length in a type definition) or from an exported
28 // function body (for an inlinable function). These two cases happen
29 // at different points in the compilation and have different
30 // requirements, so it's not easy to unify them. Import_expression is
31 // an abstract interface that permits the expression import code to
32 // work at either point. When importing expressions that only occur
33 // for an inlinable function, the ifb method is available to get the
34 // full Import_function_body.
36 class Import_expression
39 // Return the import function body. This should only be called for
40 // expressions that can not appear outside of an inlinable function
42 virtual Import_function_body
*
45 // The location to report in an error message.
49 // Peek at the next character in the input, returning a value from 0
50 // to 0xff. Returns -1 at end of stream.
54 // Return the next character and advance.
58 // Return true if the next bytes match STR.
60 match_c_string(const char* str
) = 0;
62 // Require that the next bytes match STR.
64 require_c_string(const char* str
) = 0;
66 // Advance the stream SKIP bytes.
68 advance(size_t skip
) = 0;
70 // Read an identifier.
72 read_identifier() = 0;
78 // Return the maximum valid package index.
80 max_package_index() const = 0;
82 // Return the package for a package index.
84 package_at_index(int index
) = 0;
86 // Return the version number of the export data we're reading.
87 virtual Export_data_version
91 // This class manages importing Go declarations.
93 class Import
: public Import_expression
96 // The Stream class is an interface used to read the data. The
97 // caller should instantiate a child of this class.
104 // Set the position, for error messages.
107 { this->pos_
= pos
; }
109 // Return whether we have seen an error.
112 { return this->saw_error_
; }
114 // Record that we've seen an error.
117 { this->saw_error_
= true; }
119 // Return the next character (a value from 0 to 0xff) without
120 // advancing. Returns -1 at end of stream.
124 // Look for LENGTH characters, setting *BYTES to point to them.
125 // Returns false if the bytes are not available. Does not
128 peek(size_t length
, const char** bytes
)
129 { return this->do_peek(length
, bytes
); }
131 // Return the next character (a value from 0 to 0xff) and advance
132 // the read position by 1. Returns -1 at end of stream.
136 int c
= this->peek_char();
141 // Return true if at the end of the stream.
144 { return this->peek_char() == -1; }
146 // Return true if the next bytes match STR.
148 match_c_string(const char* str
)
149 { return this->match_bytes(str
, strlen(str
)); }
151 // Return true if the next LENGTH bytes match BYTES.
153 match_bytes(const char* bytes
, size_t length
);
155 // Give an error if the next bytes do not match STR. Advance the
156 // read position by the length of STR.
158 require_c_string(Location location
, const char* str
)
159 { this->require_bytes(location
, str
, strlen(str
)); }
161 // Given an error if the next LENGTH bytes do not match BYTES.
162 // Advance the read position by LENGTH.
164 require_bytes(Location
, const char* bytes
, size_t length
);
166 // Advance the read position by SKIP bytes.
170 this->do_advance(skip
);
174 // Return the current read position. This returns int because it
175 // is more convenient in error reporting. FIXME.
178 { return static_cast<int>(this->pos_
); }
181 // This function should set *BYTES to point to a buffer holding
182 // the LENGTH bytes at the current read position. It should
183 // return false if the bytes are not available. This should not
184 // change the current read position.
186 do_peek(size_t length
, const char** bytes
) = 0;
188 // This function should advance the current read position LENGTH
191 do_advance(size_t skip
) = 0;
194 // The current read position.
196 // True if we've seen an error reading from this stream.
200 // Find import data. This searches the file system for FILENAME and
201 // returns a pointer to a Stream object to read the data that it
202 // exports. LOCATION is the location of the import statement.
203 // RELATIVE_IMPORT_PATH is used as a prefix for a relative import.
205 open_package(const std::string
& filename
, Location location
,
206 const std::string
& relative_import_path
);
209 Import(Stream
*, Location
);
211 // Register the builtin types.
213 register_builtin_types(Gogo
*);
215 // Import everything defined in the stream. LOCAL_NAME is the local
216 // name to be used for bindings; if it is the string "." then
217 // bindings should be inserted in the global scope. If LOCAL_NAME
218 // is the empty string then the name of the package itself is the
219 // local name. This returns the imported package, or NULL on error.
221 import(Gogo
*, const std::string
& local_name
, bool is_local_name_exported
);
223 // The location of the import statement.
226 { return this->location_
; }
228 // Return the package we are importing.
231 { return this->package_
; }
233 // Return the next character.
236 { return this->stream_
->peek_char(); }
238 // Return the next character and advance.
241 { return this->stream_
->get_char(); }
243 // Read LENGTH characters into *OUT and advance past them. On
244 // EOF reports an error and sets *OUT to an empty string.
246 read(size_t length
, std::string
* out
);
248 // Return true at the end of the stream.
251 { return this->stream_
->at_eof(); }
253 // Return whether the next bytes match STR.
255 match_c_string(const char* str
)
256 { return this->stream_
->match_c_string(str
); }
258 // Require that the next bytes match STR.
260 require_c_string(const char* str
)
261 { this->stream_
->require_c_string(this->location_
, str
); }
263 // Advance the stream SKIP bytes.
266 { this->stream_
->advance(skip
); }
268 // Stream position, for error reporting.
271 { return this->stream_
->pos(); }
273 // Return the version number of the export data we're reading.
275 version() const { return this->version_
; }
277 // Skip a semicolon if using an older version.
279 require_semicolon_if_old_version()
281 if (this->version_
== EXPORT_FORMAT_V1
282 || this->version_
== EXPORT_FORMAT_V2
)
283 this->require_c_string(";");
286 // Read an identifier.
290 // Read a name. This is like read_identifier, except that a "?" is
291 // returned as an empty string. This matches Export::write_name.
295 // Return the maximum valid package index. This is the size of
296 // packages_ because we will subtract 1 in package_at_index.
298 max_package_index() const
299 { return this->packages_
.size(); }
301 // Return the package at an index. (We subtract 1 because package
302 // index 0 is not used.)
304 package_at_index(int index
)
305 { return this->packages_
.at(index
- 1); }
311 // Return the type for a type index. INPUT_NAME and INPUT_OFFSET
312 // are only for error reporting. PARSED is set to whether we parsed
313 // the type information for a new type.
315 type_for_index(int index
, const std::string
& input_name
,
316 size_t input_offset
, bool* parsed
);
318 // Read an escape note.
322 // Clear the stream when it is no longer accessible.
325 { this->stream_
= NULL
; }
327 // Just so that Import implements Import_expression.
328 Import_function_body
*
332 // Read a qualified identifier from an Import_expression. Sets
333 // *NAME, *PKG, and *IS_EXPORTED, and reports whether it succeeded.
335 read_qualified_identifier(Import_expression
*, std::string
* name
,
336 Package
** pkg
, bool* is_exported
);
340 try_package_in_directory(const std::string
&, Location
);
343 try_suffixes(std::string
*);
346 find_export_data(const std::string
& filename
, int fd
, Location
);
349 find_object_export_data(const std::string
& filename
, int fd
,
350 off_t offset
, Location
);
352 static const int archive_magic_len
= 8;
355 is_archive_magic(const char*);
358 find_archive_export_data(const std::string
& filename
, int fd
,
361 // Read a package line.
365 // Read an import line.
369 // Read an indirectimport line.
371 read_one_indirect_import();
373 // Read the import control functions and init graph.
375 read_import_init_fns(Gogo
*);
381 // Import a constant.
389 // Import a variable.
393 // Import a function.
395 import_func(Package
*);
397 // Parse a type definition.
399 parse_type(int index
);
401 // Read a named type and store it at this->type_[index].
403 read_named_type(int index
);
405 // Register a single builtin type.
407 register_builtin_type(Gogo
*, const char* name
, Builtin_code
);
409 // Get an integer from a string.
411 string_to_int(const std::string
&, bool is_neg_ok
, int* ret
);
413 // Get an unsigned integer from a string.
415 string_to_unsigned(const std::string
& s
, unsigned* ret
)
418 if (!this->string_to_int(s
, false, &ivalue
))
420 *ret
= static_cast<unsigned>(ivalue
);
424 // Finalize methods for newly imported types.
430 // The stream from which to read import data.
432 // The location of the import statement we are processing.
434 // The package we are importing.
436 // Whether to add new objects to the global scope, rather than to a
438 bool add_to_globals_
;
439 // Mapping from package index to package.
440 std::vector
<Package
*> packages_
;
442 std::string type_data_
;
443 // Position of type data in the stream.
445 // Mapping from type code to offset/length in type_data_.
446 std::vector
<std::pair
<size_t, size_t> > type_offsets_
;
447 // Mapping from negated builtin type codes to Type structures.
448 std::vector
<Named_type
*> builtin_types_
;
449 // Mapping from exported type codes to Type structures.
450 std::vector
<Type
*> types_
;
451 // Version of export data we're reading.
452 Export_data_version version_
;
455 // Read import data from a string.
457 class Stream_from_string
: public Import::Stream
460 Stream_from_string(const std::string
& str
)
466 do_peek(size_t length
, const char** bytes
)
468 if (this->pos_
+ length
> this->str_
.length())
470 *bytes
= this->str_
.data() + this->pos_
;
475 do_advance(size_t len
)
476 { this->pos_
+= len
; }
479 // The string of data we are reading.
481 // The current position within the string.
485 // Read import data from a buffer allocated using malloc.
487 class Stream_from_buffer
: public Import::Stream
490 Stream_from_buffer(char* buf
, size_t length
)
491 : buf_(buf
), length_(length
), pos_(0)
494 ~Stream_from_buffer()
495 { free(this->buf_
); }
499 do_peek(size_t length
, const char** bytes
)
501 if (this->pos_
+ length
> this->length_
)
503 *bytes
= this->buf_
+ this->pos_
;
508 do_advance(size_t len
)
509 { this->pos_
+= len
; }
512 // The data we are reading.
514 // The length of the buffer.
516 // The current position within the buffer.
520 // Read import data from an open file descriptor.
522 class Stream_from_file
: public Import::Stream
525 Stream_from_file(int fd
);
531 do_peek(size_t, const char**);
538 Stream_from_file(const Stream_from_file
&);
539 Stream_from_file
& operator=(const Stream_from_file
&);
541 // The file descriptor.
543 // Data read from the file.
547 // Read import data from an offset into a std::string. This uses a
548 // reference to the string, to avoid copying, so the string must be
549 // kept alive through some other mechanism.
551 class Stream_from_string_ref
: public Import::Stream
554 Stream_from_string_ref(const std::string
& str
, size_t offset
, size_t length
)
555 : str_(str
), pos_(offset
), end_(offset
+ length
)
558 ~Stream_from_string_ref()
563 do_peek(size_t length
, const char** bytes
)
565 if (this->pos_
+ length
> this->end_
)
567 *bytes
= &this->str_
[this->pos_
];
572 do_advance(size_t length
)
573 { this->pos_
+= length
; }
576 // A reference to the string we are reading from.
577 const std::string
& str_
;
578 // The current offset into the string.
580 // The index after the last byte we can read.
584 // Class to manage importing a function body. This is passed around
585 // to Statements and Expressions. It parses the function into the IR.
587 class Import_function_body
: public Import_expression
590 Import_function_body(Gogo
* gogo
, Import
* imp
, Named_object
* named_object
,
591 const std::string
& body
, size_t off
, Block
* block
,
593 ~Import_function_body();
598 { return this->gogo_
; }
600 // The location to report in an error message.
603 { return this->imp_
->location(); }
605 // The function we are importing.
608 { return this->named_object_
; }
610 // A reference to the body we are reading.
613 { return this->body_
; }
615 // The current offset into the body.
618 { return this->off_
; }
620 // Update the offset into the body.
623 { this->off_
= off
; }
625 // Advance the offset by SKIP bytes.
628 { this->off_
+= skip
; }
630 // The current block.
633 { return this->blocks_
.back(); }
635 // Begin importing a new block BLOCK nested within the current block.
637 begin_block(Block
*block
)
638 { this->blocks_
.push_back(block
); }
640 // Record the fact that we're done importing the current block.
643 { this->blocks_
.pop_back(); }
645 // The current indentation.
648 { return this->indent_
; }
650 // Increment the indentation level.
655 // Decrement the indentation level.
660 // The name of the function we are parsing.
664 // Return the next character in the input stream, or -1 at the end.
668 if (this->body_
.length() <= this->off_
)
670 return static_cast<unsigned char>(this->body_
[this->off_
]);
673 // Return the next character and advance.
677 if (this->body_
.length() <= this->off_
)
679 int c
= static_cast<unsigned char>(this->body_
[this->off_
]);
684 // Return whether the C string matches the current body position.
686 match_c_string(const char* str
)
688 size_t len
= strlen(str
);
689 return (this->body_
.length() >= this->off_
+ len
690 && this->body_
.compare(this->off_
, len
, str
) == 0);
693 // Give an error if the next bytes do not match STR. Advance the
694 // offset by the length of STR.
696 require_c_string(const char* str
);
698 // Read an identifier.
708 { return this->imp_
->version(); }
710 // Record the index of a temporary statement.
712 record_temporary(Temporary_statement
*, unsigned int);
714 // Return a temporary statement given an index.
716 temporary_statement(unsigned int);
718 // Return an unnamed label given an index, defining the label if we
719 // haven't seen it already.
721 unnamed_label(unsigned int, Location
);
723 // Implement Import_expression.
724 Import_function_body
*
728 // Return the maximum valid package index.
730 max_package_index() const
731 { return this->imp_
->max_package_index(); }
733 // Return the package at an index.
735 package_at_index(int index
)
736 { return this->imp_
->package_at_index(index
); }
738 // Return whether we have seen an error.
741 { return this->saw_error_
; }
743 // Record that we have seen an error.
746 { this->saw_error_
= true; }
756 // The function we are parsing.
757 Named_object
* named_object_
;
758 // The exported data we are parsing. Note that this is a reference;
759 // the body string must laster longer than this object.
760 const std::string
& body_
;
761 // The current offset into body_.
763 // Stack to record nesting of blocks being imported.
764 std::vector
<Block
*> blocks_
;
765 // Current expected indentation level.
767 // Temporary statements by index.
768 std::vector
<Temporary_statement
*> temporaries_
;
769 // Unnamed labels by index.
770 std::vector
<Unnamed_label
*> labels_
;
771 // Whether we've seen an error. Used to avoid reporting excess
776 #endif // !defined(GO_IMPORT_H)