IDL compiler front-end library
Type.h
Go to the documentation of this file.
1 /*
3 * Nirvana IDL front-end library.
4 *
5 * This is a part of the Nirvana project.
6 *
7 * Author: Igor Popov
8 *
9 * Copyright (c) 2021 Igor Popov.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * Send comments and/or bug reports to:
25 * popov.nirvana@gmail.com
26 */
27 #ifndef IDLFE_AST_TYPE_H_
28 #define IDLFE_AST_TYPE_H_
29 #pragma once
30 
31 #include "BasicType.h"
32 #include "NamedItem.h"
33 #include <forward_list>
34 
35 namespace AST {
36 
37 typedef std::forward_list <unsigned> FixedArraySizes;
38 
40 typedef uint32_t Dim;
41 
42 class Sequence;
43 class Array;
44 
46 class Type
47 {
48 public:
50  enum class Kind
51  {
52  VOID,
53  BASIC_TYPE,
54  NAMED_TYPE,
55  STRING,
56  WSTRING,
57  FIXED,
58  SEQUENCE,
59  ARRAY
60  };
61 
63  Kind tkind () const noexcept
64  {
65  return kind_;
66  }
67 
70  BasicType basic_type () const noexcept
71  {
72  assert (tkind () == Kind::BASIC_TYPE);
73  return type_.basic_type;
74  }
75 
78  const NamedItem& named_type () const noexcept
79  {
80  assert (tkind () == Kind::NAMED_TYPE);
81  assert (type_.named_type);
82  return *type_.named_type;
83  }
84 
87  Dim string_bound () const noexcept
88  {
89  assert (tkind () == Kind::STRING || tkind () == Kind::WSTRING);
90  return type_.string_bound;
91  }
92 
94  Dim string_size () const noexcept
95  {
96  return string_bound ();
97  }
98 
101  const Sequence& sequence () const noexcept
102  {
103  assert (tkind () == Kind::SEQUENCE);
104  return *type_.sequence;
105  }
106 
109  const Array& array () const noexcept
110  {
111  assert (tkind () == Kind::ARRAY);
112  return *type_.array;
113  }
114 
115  // Fixed
116 
120  uint16_t fixed_digits () const noexcept
121  {
122  assert (tkind () == Kind::FIXED);
123  return type_.fixed.digits;
124  }
125 
129  uint16_t fixed_scale () const noexcept
130  {
131  assert (tkind () == Kind::FIXED);
132  return type_.fixed.scale;
133  }
134 
136  const Type& dereference_type () const noexcept;
137 
139  ~Type ()
140  {
141  clear ();
142  }
143 
145  Type () :
146  kind_ (Kind::VOID)
147  {}
148 
150  Type (const Type& src)
151  {
152  copy (src);
153  }
154 
156  Type (Type&& src) noexcept;
157 
159  Type& operator = (const Type& src);
160 
162  Type& operator = (Type&& src) noexcept;
163 
165  bool operator == (const Type& rhs) const noexcept;
166 
167  Type (BasicType bt);
168 
169  static Type make_string (Dim size = 0)
170  {
171  return Type (Kind::STRING, size);
172  }
173 
174  static Type make_wstring (Dim size = 0)
175  {
176  return Type (Kind::WSTRING, size);
177  }
178 
179  static Type make_sequence (const Type& type, Dim size = 0);
180 
181  Type (const Type& type, const FixedArraySizes& sizes);
182 
183  static Type make_fixed (unsigned digits, unsigned scale)
184  {
185  return Type (digits, scale);
186  }
187 
188  Type (const NamedItem* named);
189 
190 private:
191  friend class Builder;
192 
193  size_t key_max () const noexcept;
194 
195  bool is_complete_or_ref () const noexcept;
196  bool is_complete_or_seq () const noexcept;
197  bool is_complete () const noexcept;
198 
199 private:
200  void clear () noexcept;
201 
202  void reset ()
203  {
204  kind_ = Kind::VOID;
205  }
206 
207  void copy (const Type& src);
208 
209  Type (Kind string_kind, Dim size = 0) :
210  kind_ (string_kind),
211  type_ (size)
212  {}
213 
214  Type (unsigned digits, unsigned scale);
215 
216 private:
217  Type (Sequence* seq) :
218  kind_ (Kind::SEQUENCE),
219  type_ (seq)
220  {}
221 
222 private:
223  Kind kind_;
224  union U
225  {
226  BasicType basic_type; // `Kind::BASIC_TYPE`
227  const NamedItem* named_type; // `Kind::NAMED_TYPE`
228  Dim string_bound; // `Kind::STRING, Kind::WSTRING`
229  const Sequence* sequence; // `Kind::SEQUENCE`
230  const Array* array; // `Kind::ARRAY`
231  struct
232  {
233  uint16_t digits;
234  uint16_t scale;
235  }
236  fixed; // `Kind::FIXED`
237 
238  U ()
239  {}
240 
241  U (BasicType bt) :
242  basic_type (bt)
243  {}
244 
245  U (const NamedItem* nt) :
246  named_type (nt)
247  {}
248 
249  U (Dim bound) :
250  string_bound (bound)
251  {}
252 
253  U (const Sequence* pseq) :
254  sequence (pseq)
255  {}
256 
257  U (const Array* parr) :
258  array (parr)
259  {}
260 
261  ~U () {}
262  }
263  type_;
264 };
265 
266 }
267 
268 #endif
The array type descriptor.
Definition: Array.h:39
A named AST item.
Definition: NamedItem.h:45
The sequence type descriptor.
Definition: Sequence.h:38
An IDL type.
Definition: Type.h:47
const Sequence & sequence() const noexcept
Definition: Type.h:101
Kind tkind() const noexcept
Definition: Type.h:63
const Array & array() const noexcept
Definition: Type.h:109
Type()
Default constructor.
Definition: Type.h:145
uint16_t fixed_digits() const noexcept
Definition: Type.h:120
bool operator==(const Type &rhs) const noexcept
Check types for equality.
const NamedItem & named_type() const noexcept
Definition: Type.h:78
Type & operator=(const Type &src)
Copy assignment.
BasicType basic_type() const noexcept
Definition: Type.h:70
Type(const Type &src)
Copy constructor.
Definition: Type.h:150
Dim string_size() const noexcept
Obsolete. Use Type::string_bound () instead.
Definition: Type.h:94
uint16_t fixed_scale() const noexcept
Definition: Type.h:129
Type(Type &&src) noexcept
Move constructor.
Kind
The kind of type.
Definition: Type.h:51
@ SEQUENCE
sequence ()
@ STRING
string_bound ()
@ NAMED_TYPE
named_type ()
@ FIXED
fixed_digits (), fixed_scale ()
@ ARRAY
array ()
@ WSTRING
string_bound ()
@ BASIC_TYPE
Type::basic_type ();
const Type & dereference_type() const noexcept
Dim string_bound() const noexcept
Definition: Type.h:87
Abstract Syntax Tree namespace.
Definition: Array.h:34
uint32_t Dim
Array dimensions, string and sequence bounds.
Definition: Type.h:40
BasicType
CORBA basic type enumeration.
Definition: BasicType.h:35
std::forward_list< unsigned > FixedArraySizes
Array dimensions.
Definition: Declarators.h:75