OSDN Git Service

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