export.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // export.h -- Export declarations in Go frontend. -*- C++ -*-
  2. // Copyright 2009 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. #ifndef GO_EXPORT_H
  6. #define GO_EXPORT_H
  7. #include "string-dump.h"
  8. struct sha1_ctx;
  9. class Gogo;
  10. class Import_init;
  11. class Bindings;
  12. class Type;
  13. class Package;
  14. // Codes used for the builtin types. These are all negative to make
  15. // them easily distinct from the codes assigned by Export::write_type.
  16. // Note that these codes may not be changed! Changing them would
  17. // break existing export data.
  18. enum Builtin_code
  19. {
  20. BUILTIN_INT8 = -1,
  21. BUILTIN_INT16 = -2,
  22. BUILTIN_INT32 = -3,
  23. BUILTIN_INT64 = -4,
  24. BUILTIN_UINT8 = -5,
  25. BUILTIN_UINT16 = -6,
  26. BUILTIN_UINT32 = -7,
  27. BUILTIN_UINT64 = -8,
  28. BUILTIN_FLOAT32 = -9,
  29. BUILTIN_FLOAT64 = -10,
  30. BUILTIN_INT = -11,
  31. BUILTIN_UINT = -12,
  32. BUILTIN_UINTPTR = -13,
  33. BUILTIN_BOOL = -15,
  34. BUILTIN_STRING = -16,
  35. BUILTIN_COMPLEX64 = -17,
  36. BUILTIN_COMPLEX128 = -18,
  37. BUILTIN_ERROR = -19,
  38. BUILTIN_BYTE = -20,
  39. BUILTIN_RUNE = -21,
  40. SMALLEST_BUILTIN_CODE = -21
  41. };
  42. // This class manages exporting Go declarations. It handles the main
  43. // loop of exporting. A pointer to this class is also passed to the
  44. // various specific export implementations.
  45. class Export : public String_dump
  46. {
  47. public:
  48. // The Stream class is an interface used to output the exported
  49. // information. The caller should instantiate a child of this
  50. // class.
  51. class Stream
  52. {
  53. public:
  54. Stream();
  55. virtual ~Stream();
  56. // Write a string. Implements the String_dump interface.
  57. void
  58. write_string(const std::string& s)
  59. { this->write_and_sum_bytes(s.data(), s.length()); }
  60. // Write a nul terminated string. Implements the String_dump interface.
  61. void
  62. write_c_string(const char* s)
  63. { this->write_and_sum_bytes(s, strlen(s)); }
  64. // Write some bytes.
  65. void
  66. write_bytes(const char* bytes, size_t length)
  67. { this->write_and_sum_bytes(bytes, length); }
  68. // Return the raw bytes of the checksum data.
  69. std::string
  70. checksum();
  71. // Write a checksum string to the stream. This will be called at
  72. // the end of the other output.
  73. void
  74. write_checksum(const std::string&);
  75. protected:
  76. // This function is called with data to export. This data must be
  77. // made available as a contiguous stream for the importer.
  78. virtual void
  79. do_write(const char* bytes, size_t length) = 0;
  80. private:
  81. void
  82. write_and_sum_bytes(const char*, size_t);
  83. // The checksum.
  84. sha1_ctx* checksum_;
  85. };
  86. Export(Stream*);
  87. // The magic code for version 1 export data.
  88. static const int v1_magic_len = 4;
  89. static const char v1_magic[v1_magic_len];
  90. // The length of the v1 checksum string.
  91. static const int v1_checksum_len = 20;
  92. // Register the builtin types.
  93. void
  94. register_builtin_types(Gogo*);
  95. // Export the identifiers in BINDINGS which are marked for export.
  96. // The exporting is done via a series of calls to THIS->STREAM_. If
  97. // is nothing to export, this->stream_->write will not be called.
  98. // PREFIX is the package prefix. PKGPATH is the package path.
  99. // Only one of PREFIX and PKGPATH will be non-empty.
  100. // PACKAGE_PRIORITY is the priority to use for this package.
  101. // PACKAGES is all the packages we have seen.
  102. // IMPORTS is the explicitly imported packages.
  103. // IMPORT_INIT_FN is the name of the import initialization function
  104. // for this package; it will be empty if none is needed.
  105. // IMPORTED_INIT_FNS is the list of initialization functions for
  106. // imported packages.
  107. void
  108. export_globals(const std::string& package_name,
  109. const std::string& prefix,
  110. const std::string& pkgpath,
  111. int package_priority,
  112. const std::map<std::string, Package*>& packages,
  113. const std::map<std::string, Package*>& imports,
  114. const std::string& import_init_fn,
  115. const std::set<Import_init>& imported_init_fns,
  116. const Bindings* bindings);
  117. // Write a string to the export stream.
  118. void
  119. write_string(const std::string& s)
  120. { this->stream_->write_string(s); }
  121. // Write a nul terminated string to the export stream.
  122. void
  123. write_c_string(const char* s)
  124. { this->stream_->write_c_string(s); }
  125. // Write some bytes to the export stream.
  126. void
  127. write_bytes(const char* bytes, size_t length)
  128. { this->stream_->write_bytes(bytes, length); }
  129. // Write a name to the export stream. If NAME is empty, write "?".
  130. void
  131. write_name(const std::string& name);
  132. // Write out a type. This handles references back to previous
  133. // definitions.
  134. void
  135. write_type(const Type*);
  136. private:
  137. Export(const Export&);
  138. Export& operator=(const Export&);
  139. // Write out all known packages.
  140. void
  141. write_packages(const std::map<std::string, Package*>& packages);
  142. // Write out the imported packages.
  143. void
  144. write_imports(const std::map<std::string, Package*>& imports);
  145. // Write out the imported initialization functions.
  146. void
  147. write_imported_init_fns(const std::string& package_name, int priority,
  148. const std::string&, const std::set<Import_init>&);
  149. // Register one builtin type.
  150. void
  151. register_builtin_type(Gogo*, const char* name, Builtin_code);
  152. // Mapping from Type objects to a constant index.
  153. typedef Unordered_map(const Type*, int) Type_refs;
  154. // The stream to which we are writing data.
  155. Stream* stream_;
  156. // Type mappings.
  157. Type_refs type_refs_;
  158. // Index number of next type.
  159. int type_index_;
  160. // Packages we have written out.
  161. Unordered_set(const Package*) packages_;
  162. };
  163. // An export streamer which puts the export stream in a named section.
  164. class Stream_to_section : public Export::Stream
  165. {
  166. public:
  167. Stream_to_section();
  168. protected:
  169. void
  170. do_write(const char*, size_t);
  171. };
  172. #endif // !defined(GO_EXPORT_H)