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