yast2-core
Type.h
Go to the documentation of this file.
1 /*----------------------------------------------------------*- c++ -*--\
2 | |
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
8 | |
9 | core system |
10 | (C) SuSE Linux AG |
11 \----------------------------------------------------------------------/
12 
13  File: Type.h
14 
15  Author: Klaus Kaempf <kkaempf@suse.de>
16  Maintainer: Klaus Kaempf <kkaempf@suse.de>
17 
18 /-*/
19 
20 #ifndef Type_h
21 #define Type_h
22 
23 #include <iosfwd>
24 #include <vector>
25 
26 // MemUsage.h defines/undefines D_MEMUSAGE
27 #include <y2util/MemUsage.h>
28 #include "ycp/YCPValue.h"
29 #include "ycp/TypePtr.h"
30 
31 class FunctionType;
32 class bytecodeistream;
33 class xmlcodeistream;
34 
36 class Type : public Rep
37 #ifdef D_MEMUSAGE
38  , public MemUsage
39 #endif
40 {
41  REP_BODY(Type);
42 
43 public:
44  // type codes for basic types
45  typedef enum type_kind {
46  UnspecT = 0, // 0 unspecified
47  ErrorT, // 1 error
48  AnyT, // 2 any
49  BooleanT, // 3 boolean
50  ByteblockT, // 4 byteblock
51  FloatT, // 5 float
52  IntegerT, // 6 integer
53  LocaleT, // 7 locale
54  PathT, // 8 path
55  StringT, // 9 string
56  SymbolT, // 10 symbol
57  TermT, // 11 term
58  VoidT, // 12 void
59  WildcardT, // 13 wildcard
60 
61  FlexT, // 14 flex
62  VariableT, // 15 variable <kind>
63  ListT, // 16 list <kind>
64  MapT, // 17 map <key_kind, value_kind>
65  BlockT, // 18 block <kind>
66  TupleT, // 19 tuple <kind, kind, kind, ...>
67  FunctionT, // 20 function <ret_kind, kind, kind, ...>
68 
69  NilT, // 21 only for "return;" (void) vs. "return nil;" (nil)
70  NFlexT // 22 multiple Flex
71  } tkind;
72 
73 protected:
75  bool m_const;
77 
78  Type (tkind kind, bool as_const = false, bool as_reference = false) : m_kind (kind), m_const (as_const), m_reference(as_reference) { };
79 
80 public:
81  //-------------------------------------------------
82  // static member functions
83 
87  static void setNocheck (bool nocheck);
88 
92  static constTypePtr vt2type (enum YCPValueType vt);
93 
97  static int nextToken (const char **signature);
98 
102  static constTypePtr fromSignature (const char **signature);
103 
108  static constTypePtr fromSignature (const string & signature) { const char *s = signature.c_str(); return Type::fromSignature (&s); }
109 
114  static constTypePtr determineFlexType (constFunctionTypePtr actual, constFunctionTypePtr declared);
115 
116 public:
117 
118  static const constTypePtr Unspec; /* unspecified type */
119  static const constTypePtr Error; /* error type */
120  static const constTypePtr Any; /* any type */
121 
122  static const constTypePtr Void; /* void type */
123  static const constTypePtr Boolean; /* boolean type */
124  static const constTypePtr Byteblock;/* byteblock type */
125  static const constTypePtr Float; /* float type */
126  static const constTypePtr Integer; /* integer type */
127  static const constTypePtr Locale; /* locale type */
128  static const constTypePtr Path; /* path type */
129  static const constTypePtr String; /* string type */
130  static const constTypePtr Symbol; /* symbol type */
131  static const constTypePtr Term; /* term type */
132  static const constTypePtr Wildcard; /* wildcard (...) type */
133 
134  static const constTypePtr ConstAny; /* any type */
135  static const constTypePtr ConstVoid; /* void type */
136  static const constTypePtr ConstBoolean; /* boolean type */
137  static const constTypePtr ConstByteblock; /* byteblock type */
138  static const constTypePtr ConstFloat; /* float type */
139  static const constTypePtr ConstInteger; /* integer type */
140  static const constTypePtr ConstLocale; /* locale type */
141  static const constTypePtr ConstPath; /* path type */
142  static const constTypePtr ConstString; /* string type */
143  static const constTypePtr ConstSymbol; /* symbol type */
144  static const constTypePtr ConstTerm; /* term type */
145 
146  static const constTypePtr ConstList; /* list type */
147  static const constTypePtr ConstMap; /* map type */
148 
149  static const constTypePtr Flex;
150  static const constTypePtr ConstFlex;
151  static const constTypePtr NFlex1;
152  static const constTypePtr ConstNFlex1;
153  static const constTypePtr NFlex2;
154  static const constTypePtr ConstNFlex2;
155  static const constTypePtr NFlex3;
156  static const constTypePtr ConstNFlex3;
157  static const constTypePtr NFlex4;
158  static const constTypePtr ConstNFlex4;
159 
160  static const constTypePtr ListUnspec;
161  static const constTypePtr List;
162  static const constTypePtr MapUnspec;
163  static const constTypePtr Map;
164  static const constTypePtr Variable;
165  static const constTypePtr Block;
166 
167  static FunctionTypePtr Function(constTypePtr return_type);
168 
169  static const constTypePtr Nil; /* "return nil;" type */
170 
171 private:
172  /*
173  * get kind
174  */
175  tkind kind () const { return m_kind; }
176 
177 public:
178  Type ();
180  virtual ~Type ();
181 
185  virtual string toString () const;
186  virtual string toXmlString () const;
187 
191  virtual std::ostream & toStream (std::ostream & str) const;
192 
196  virtual std::ostream & toXml (std::ostream & str, int indent ) const;
197 
198  /*
199  * is base or constructed type
200  */
201  virtual bool isBasetype () const { return true; }
202 
203  /*
204  * match <flex<number>> to type, return type if <flex<number>> matches
205  */
206  virtual constTypePtr matchFlex (constTypePtr /*type*/, unsigned int /*number*/ = 0) const { return 0; }
207 
212  virtual int match (constTypePtr expected) const;
213 
218  virtual int matchvalue (YCPValue value) const;
219 
224  virtual bool canCast (constTypePtr to) const;
225 
229  virtual TypePtr clone () const;
230 
234  virtual constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
235 
239  string preToString () const { return (m_const ? "const " : ""); }
240 
244  string postToString () const { return (m_reference ? " &" : ""); }
245 
249  bool isConst () const { return m_const; }
250 
254  void asConst () { m_const = true; }
255 
259  bool isReference () const { return m_reference; }
260 
264  void asReference () { m_reference = true; }
265 
270  int basematch (constTypePtr expected) const;
271 
275  virtual bool equals (constTypePtr expected) const;
276 
277  // ------------------------------------------------------------
278  // checking types
279 
280  // kind
281  bool isUnspec () const { return m_kind == UnspecT; }
282  bool isError () const { return m_kind == ErrorT; }
283  bool isAny () const { return m_kind == AnyT; }
284  bool isBoolean () const { return m_kind == BooleanT; }
285  bool isByteblock () const { return m_kind == ByteblockT; }
286  bool isFloat () const { return m_kind == FloatT; }
287  bool isInteger () const { return m_kind == IntegerT; }
288  bool isLocale () const { return m_kind == LocaleT; }
289  bool isPath () const { return m_kind == PathT; }
290  bool isString () const { return m_kind == StringT; }
291  bool isSymbol () const { return m_kind == SymbolT; }
292  bool isTerm () const { return m_kind == TermT; }
293  bool isVoid () const { return m_kind == VoidT; }
294  bool isWildcard () const { return m_kind == WildcardT; }
295  bool isFlex () const { return ((m_kind == FlexT) || (m_kind == NFlexT)); }
296  bool isNFlex () const { return m_kind == NFlexT; }
297 
298  bool isVariable () const { return m_kind == VariableT; }
299  bool isList () const { return m_kind == ListT; }
300  bool isMap () const { return m_kind == MapT; }
301  bool isBlock () const { return m_kind == BlockT; }
302  bool isTuple () const { return m_kind == TupleT; }
303  bool isFunction () const { return m_kind == FunctionT; }
304 
305  bool isNil () const { return m_kind == NilT; }
306  // ------------------------------------------------------------
307  // misc methods
308 
309  YCPValueType valueType () const;
310 
311  // determine the common type of two types, used to determine the type of lists
312  // and maps with various elements.
313  // -> returns the largets (containing least amount of information) matching
314  // type (Any if types do not match)
315  // the return type is 'least common denominator'
316  virtual constTypePtr commontype (constTypePtr type) const;
317 
318  // determine the more detailed type of two types, used to determine the type of bracket
319  // element vs. bracket default
320  // -> returns the smallest (containing most amount of information) matching
321  // type (Error if types do not match)
322  virtual constTypePtr detailedtype (constTypePtr type) const;
323 };
324 
326 
327 class FlexType : public Type
328 {
330 public:
331  string toString () const;
332  std::ostream & toStream (std::ostream & str) const;
333  bool isBasetype () const { return false; }
334  constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
335  int match (constTypePtr expected) const;
336  TypePtr clone () const;
337  constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
338  FlexType (bool as_const = false);
339  FlexType (bytecodeistream & str);
340  ~FlexType ();
341 };
342 
343 
345 
346 class NFlexType : public Type
347 {
349  unsigned int m_number; // there can be more than one flex
350 public:
351  string toString () const;
352  std::ostream & toStream (std::ostream & str) const;
353  bool isBasetype () const { return false; }
354  constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
355  int match (constTypePtr expected) const;
356  TypePtr clone () const;
357  constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
358  unsigned int number () const;
359  NFlexType (unsigned int number, bool as_const = false);
361  ~NFlexType ();
362 };
363 
364 
366 
367 class VariableType : public Type
368 {
370 private:
371  const constTypePtr m_type;
372 public:
373  string toString () const;
374  std::ostream & toStream (std::ostream & str) const;
375  bool isBasetype () const { return false; }
376  constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
377  int match (constTypePtr expected) const;
378  bool equals (constTypePtr expected) const;
379  TypePtr clone () const;
380  constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
381  constTypePtr type () const { return m_type; }
382  VariableType (constTypePtr type = Type::Unspec, bool as_const = false);
384  ~VariableType ();
385 };
386 
387 
389 
390 class ListType : public Type
391 {
393 private:
394  const constTypePtr m_type;
395 public:
396  string toString () const;
397  bool isBasetype () const { return false; }
398  constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
399  int match (constTypePtr expected) const;
400  bool equals (constTypePtr expected) const;
401  constTypePtr commontype (constTypePtr type) const;
402  constTypePtr detailedtype (constTypePtr type) const;
403  bool canCast (constTypePtr to) const;
404  TypePtr clone () const;
405  constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
406  constTypePtr type () const { return m_type; }
407  std::ostream & toStream (std::ostream & str) const;
408  ListType (constTypePtr type = Type::Unspec, bool as_const = false);
410  ~ListType ();
411 };
412 
413 
415 
416 class MapType : public Type
417 {
418  REP_BODY(MapType);
419 private:
420  const constTypePtr m_keytype;
421  const constTypePtr m_valuetype;
422 public:
423  string toString () const;
424  bool isBasetype () const { return false; }
425  constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
426  int match (constTypePtr expected) const;
427  bool equals (constTypePtr expected) const;
428  constTypePtr commontype (constTypePtr type) const;
429  constTypePtr detailedtype (constTypePtr type) const;
430  bool canCast (constTypePtr to) const;
431  TypePtr clone () const;
432  constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
433  constTypePtr keytype () const { return m_keytype; }
434  constTypePtr valuetype () const { return m_valuetype; }
435  std::ostream & toStream (std::ostream & str) const;
436  MapType (constTypePtr key = Type::Unspec, constTypePtr value = Type::Unspec, bool as_const = false);
438  ~MapType ();
439 };
440 
441 
443 class BlockType : public Type
444 {
446 private:
447  const constTypePtr m_type;
448 public:
449  string toString () const;
450  bool isBasetype () const { return false; }
451  constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
452  int match (constTypePtr expected) const;
453  bool equals (constTypePtr expected) const;
454  bool canCast (constTypePtr to) const;
455  TypePtr clone () const;
456  constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
457  constTypePtr returnType () const { return m_type; }
458  std::ostream & toStream (std::ostream & str) const;
459  BlockType (constTypePtr type, bool as_const = false);
461  ~BlockType ();
462 };
463 
464 
466 
467 class TupleType : public Type
468 {
470 protected:
471  std::vector <constTypePtr> m_types;
472 public:
473  string toString () const;
474  bool isBasetype () const { return false; }
475  constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
476  int match (constTypePtr expected) const;
477  bool equals (constTypePtr expected) const;
478  bool canCast (constTypePtr to) const;
479  TypePtr clone () const;
480  constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
481  std::ostream & toStream (std::ostream & str) const;
482  TupleType (constTypePtr type, bool as_const = false);
484  void concat (constTypePtr t);
485  unsigned int parameterCount () const { return m_types.size(); }
486  constTypePtr parameterType (unsigned int parameter_number) const;
487  ~TupleType ();
488 };
489 
490 
492 
493 class FunctionType : public Type
494 {
496 private:
497  const constTypePtr m_returntype;
498  TupleTypePtr m_arguments;
499 public:
500  FunctionType (constTypePtr return_type, constFunctionTypePtr arguments);
501  string toString () const;
502  bool isBasetype () const { return false; }
503  constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
504  int match (constTypePtr expected) const;
505  bool equals (constTypePtr expected) const;
506  bool canCast (constTypePtr /*to*/) const { return false; }
507  TypePtr clone () const;
508  constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
509  std::ostream & toStream (std::ostream & str) const;
510  FunctionType (constTypePtr returntype = Type::Unspec, bool as_const = false);
512  ~FunctionType ();
513  constTypePtr returnType () const { return m_returntype; }
514  void concat (constTypePtr t);
515  int parameterCount () const;
516  constTypePtr parameterType (unsigned int parameter_number) const;
517  constTupleTypePtr parameters () const;
518 };
519 
520 
521 #endif // Type_h

Generated on a sunny day for yast2-core by doxygen 1.8.2