OSDN Git Service

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