Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Examples

Type.h

Go to the documentation of this file.
00001 /*----------------------------------------------------------*- c++ -*--\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                    (C) SuSE Linux AG |
00011 \----------------------------------------------------------------------/
00012 
00013    File:       Type.h
00014 
00015    Author:     Klaus Kaempf <kkaempf@suse.de>
00016    Maintainer: Klaus Kaempf <kkaempf@suse.de>
00017 
00018 /-*/
00019 
00020 #ifndef Type_h
00021 #define Type_h
00022 
00023 #include <iosfwd>
00024 #include <vector>
00025 
00026 // MemUsage.h defines/undefines D_MEMUSAGE
00027 #include <y2util/MemUsage.h>
00028 #include "ycp/YCPValue.h"
00029 #include "ycp/TypePtr.h"
00030 
00031 class FunctionType;
00032 
00033 class Type : public Rep
00034 #ifdef D_MEMUSAGE
00035   , public MemUsage
00036 #endif
00037 {
00038     REP_BODY(Type);
00039 
00040 public:
00041     // type codes for basic types
00042     typedef enum type_kind {
00043         UnspecT = 0,            //  0 unspecified
00044         ErrorT,                 //  1 error
00045         AnyT,                   //  2 any
00046         BooleanT,               //  3 boolean
00047         ByteblockT,             //  4 byteblock
00048         FloatT,                 //  5 float
00049         IntegerT,               //  6 integer
00050         LocaleT,                //  7 locale
00051         PathT,                  //  8 path
00052         StringT,                //  9 string
00053         SymbolT,                // 10 symbol
00054         TermT,                  // 11 term
00055         VoidT,                  // 12 void
00056         WildcardT,              // 13 wildcard
00057 
00058         FlexT,                  // 14 flex
00059         VariableT,              // 15 variable <kind>
00060         ListT,                  // 16 list <kind>
00061         MapT,                   // 17 map <key_kind, value_kind>
00062         BlockT,                 // 18 block <kind>
00063         TupleT,                 // 19 tuple <kind, kind, kind, ...>
00064         FunctionT,              // 20 function <ret_kind, kind, kind, ...>
00065 
00066         NilT,                   // 21 only for "return;" (void) vs. "return nil;" (nil)
00067         NFlexT                  // 22 multiple Flex
00068     } tkind;
00069 
00070 protected:
00071     tkind m_kind;
00072     bool m_const;
00073     bool m_reference;
00074 
00075     Type (tkind kind, bool as_const = false, bool as_reference = false) : m_kind (kind), m_const (as_const), m_reference(as_reference) { };
00076 
00077 public:
00078     //-------------------------------------------------
00079     // static member functions
00080 
00084     static void setNocheck (bool nocheck);
00085 
00089     static constTypePtr vt2type (enum YCPValueType vt);
00090 
00094     static int nextToken (const char **signature);
00095 
00099     static constTypePtr fromSignature (const char **signature);
00100 
00105     static constTypePtr fromSignature (const string & signature) { const char *s = signature.c_str(); return Type::fromSignature (&s); }
00106 
00111     static constTypePtr determineFlexType (constFunctionTypePtr actual, constFunctionTypePtr declared);
00112 
00113 public:
00114 
00115     static const constTypePtr Unspec;   /* unspecified type  */
00116     static const constTypePtr Error;    /* error type  */
00117     static const constTypePtr Any;      /* any type  */
00118 
00119     static const constTypePtr Void;     /* void type  */
00120     static const constTypePtr Boolean;  /* boolean type  */
00121     static const constTypePtr Byteblock;/* byteblock type  */
00122     static const constTypePtr Float;    /* float type  */
00123     static const constTypePtr Integer;  /* integer type  */
00124     static const constTypePtr Locale;   /* locale type  */
00125     static const constTypePtr Path;     /* path type  */
00126     static const constTypePtr String;   /* string type  */
00127     static const constTypePtr Symbol;   /* symbol type  */
00128     static const constTypePtr Term;     /* term type  */
00129     static const constTypePtr Wildcard; /* wildcard (...) type  */
00130 
00131     static const constTypePtr ConstAny;         /* any type  */
00132     static const constTypePtr ConstVoid;        /* void type  */
00133     static const constTypePtr ConstBoolean;     /* boolean type  */
00134     static const constTypePtr ConstByteblock;   /* byteblock type  */
00135     static const constTypePtr ConstFloat;       /* float type  */
00136     static const constTypePtr ConstInteger;     /* integer type  */
00137     static const constTypePtr ConstLocale;      /* locale type  */
00138     static const constTypePtr ConstPath;        /* path type  */
00139     static const constTypePtr ConstString;      /* string type  */
00140     static const constTypePtr ConstSymbol;      /* symbol type  */
00141     static const constTypePtr ConstTerm;        /* term type  */
00142 
00143     static const constTypePtr ConstList;        /* list type  */
00144     static const constTypePtr ConstMap;         /* map type  */
00145 
00146     static const constTypePtr Flex;
00147     static const constTypePtr ConstFlex;
00148     static const constTypePtr NFlex1;
00149     static const constTypePtr ConstNFlex1;
00150     static const constTypePtr NFlex2;
00151     static const constTypePtr ConstNFlex2;
00152     static const constTypePtr NFlex3;
00153     static const constTypePtr ConstNFlex3;
00154     static const constTypePtr NFlex4;
00155     static const constTypePtr ConstNFlex4;
00156 
00157     static const constTypePtr ListUnspec;
00158     static const constTypePtr List;
00159     static const constTypePtr MapUnspec;
00160     static const constTypePtr Map;
00161     static const constTypePtr Variable;
00162     static const constTypePtr Block;
00163 
00164     static FunctionTypePtr Function(constTypePtr return_type);
00165 
00166     static const constTypePtr Nil;      /* "return nil;" type */
00167 
00168 private:
00169     /*
00170      * get kind
00171      */
00172     tkind kind () const { return m_kind; }
00173 
00174 public:
00175     Type ();
00176     Type (tkind kind, std::istream & str);
00177     virtual ~Type ();
00178 
00182     virtual string toString () const;
00183 
00187     virtual std::ostream & toStream (std::ostream & str) const;
00188 
00189     /*
00190      * is base or constructed type
00191      */
00192     virtual bool isBasetype () const { return true; }
00193 
00194     /*
00195      * match <flex<number>> to type, return type if <flex<number>> matches
00196      */
00197     virtual constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const { return 0; }
00198 
00203     virtual int match (constTypePtr expected) const;
00204 
00209     virtual bool canCast (constTypePtr to) const;
00210 
00214     virtual TypePtr clone () const;
00215 
00219     virtual constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00220 
00224     string preToString () const { return (m_const ? "const " : ""); }
00225 
00229     string postToString () const { return (m_reference ? " &" : ""); }
00230 
00234     bool isConst () const { return m_const; }
00235 
00239     void asConst () { m_const = true; }
00240 
00244     bool isReference () const { return m_reference; }
00245 
00249     void asReference () { m_reference = true; }
00250 
00255     int basematch (constTypePtr expected) const;
00256 
00260     virtual bool equals (constTypePtr expected) const;
00261 
00262     // ------------------------------------------------------------
00263     // checking types
00264 
00265     // kind
00266     bool isUnspec () const      { return m_kind == UnspecT; }
00267     bool isError () const       { return m_kind == ErrorT; }
00268     bool isAny () const         { return m_kind == AnyT; }
00269     bool isBoolean () const     { return m_kind == BooleanT; }
00270     bool isByteblock () const   { return m_kind == ByteblockT; }
00271     bool isFloat () const       { return m_kind == FloatT; }
00272     bool isInteger () const     { return m_kind == IntegerT; }
00273     bool isLocale () const      { return m_kind == LocaleT; }
00274     bool isPath () const        { return m_kind == PathT; }
00275     bool isString () const      { return m_kind == StringT; }
00276     bool isSymbol () const      { return m_kind == SymbolT; }
00277     bool isTerm () const        { return m_kind == TermT; }
00278     bool isVoid () const        { return m_kind == VoidT; }
00279     bool isWildcard () const    { return m_kind == WildcardT; }
00280     bool isFlex () const        { return ((m_kind == FlexT) || (m_kind == NFlexT)); }
00281     bool isNFlex () const       { return m_kind == NFlexT; }
00282 
00283     bool isVariable () const    { return m_kind == VariableT; }
00284     bool isList () const        { return m_kind == ListT; }
00285     bool isMap () const         { return m_kind == MapT; }
00286     bool isBlock () const       { return m_kind == BlockT; }
00287     bool isTuple () const       { return m_kind == TupleT; }
00288     bool isFunction () const    { return m_kind == FunctionT; }
00289 
00290     bool isNil () const         { return m_kind == NilT; }
00291     // ------------------------------------------------------------
00292     // misc methods
00293 
00294     YCPValueType valueType () const;
00295 
00296     virtual constTypePtr commontype (constTypePtr type) const;
00297 };
00298 
00299 // <flex>
00300 
00301 class FlexType : public Type
00302 {
00303     REP_BODY(FlexType);
00304 public:
00305     string toString () const;
00306     std::ostream & toStream (std::ostream & str) const;
00307     bool isBasetype () const { return false; }
00308     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00309     int match (constTypePtr expected) const;
00310     TypePtr clone () const;
00311     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00312     FlexType (bool as_const = false);
00313     FlexType (std::istream & str);
00314     ~FlexType ();
00315 };
00316 
00317 
00318 // <flexN>
00319 
00320 class NFlexType : public Type
00321 {
00322     REP_BODY(NFlexType);
00323     unsigned int m_number;              // there can be more than one flex
00324 public:
00325     string toString () const;
00326     std::ostream & toStream (std::ostream & str) const;
00327     bool isBasetype () const { return false; }
00328     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00329     int match (constTypePtr expected) const;
00330     TypePtr clone () const;
00331     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00332     unsigned int number () const;
00333     NFlexType (unsigned int number, bool as_const = false);
00334     NFlexType (std::istream & str);
00335     ~NFlexType ();
00336 };
00337 
00338 
00339 // Variable <type>
00340 
00341 class VariableType : public Type
00342 {
00343     REP_BODY(VariableType);
00344 private:
00345     const constTypePtr m_type;
00346 public:
00347     string toString () const;
00348     std::ostream & toStream (std::ostream & str) const;
00349     bool isBasetype () const { return false; }
00350     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00351     int match (constTypePtr expected) const;
00352     bool equals (constTypePtr expected) const;
00353     TypePtr clone () const;
00354     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00355     constTypePtr type () const { return m_type; }
00356     VariableType (constTypePtr type = Type::Unspec, bool as_const = false);
00357     VariableType (std::istream & str);
00358     ~VariableType ();
00359 };
00360 
00361 
00362 // List <type>
00363 
00364 class ListType : public Type
00365 {
00366     REP_BODY(ListType);
00367 private:
00368     const constTypePtr m_type;
00369 public:
00370     string toString () const;
00371     bool isBasetype () const { return false; }
00372     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00373     int match (constTypePtr expected) const;
00374     bool equals (constTypePtr expected) const;
00375     constTypePtr commontype (constTypePtr type) const;
00376     bool canCast (constTypePtr to) const;
00377     TypePtr clone () const;
00378     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00379     constTypePtr type () const { return m_type; }
00380     std::ostream & toStream (std::ostream & str) const;
00381     ListType (constTypePtr type = Type::Unspec, bool as_const = false);
00382     ListType (std::istream & str);
00383     ~ListType ();
00384 };
00385 
00386 
00387 // Map <keytype, valuetype>
00388 
00389 class MapType : public Type
00390 {
00391     REP_BODY(MapType);
00392 private:
00393     const constTypePtr m_keytype;
00394     const constTypePtr m_valuetype;
00395 public:
00396     string toString () const;
00397     bool isBasetype () const { return false; }
00398     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00399     int match (constTypePtr expected) const;
00400     bool equals (constTypePtr expected) const;
00401     constTypePtr commontype (constTypePtr type) const;
00402     bool canCast (constTypePtr to) const;
00403     TypePtr clone () const;
00404     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00405     constTypePtr keytype () const { return m_keytype; }
00406     constTypePtr valuetype () const { return m_valuetype; }
00407     std::ostream & toStream (std::ostream & str) const;
00408     MapType (constTypePtr key = Type::Unspec, constTypePtr value = Type::Unspec, bool as_const = false);
00409     MapType (std::istream & str);
00410     ~MapType ();
00411 };
00412 
00413 
00414 // Block <type>
00415 
00416 class BlockType : public Type
00417 {
00418     REP_BODY(BlockType);
00419 private:
00420     const constTypePtr m_type;
00421 public:
00422     string toString () const;
00423     bool isBasetype () const { return false; }
00424     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00425     int match (constTypePtr expected) const;
00426     bool equals (constTypePtr expected) const;
00427     bool canCast (constTypePtr to) const;
00428     TypePtr clone () const;
00429     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00430     constTypePtr returnType () const { return m_type; }
00431     std::ostream & toStream (std::ostream & str) const;
00432     BlockType (constTypePtr type, bool as_const = false);
00433     BlockType (std::istream & str);
00434     ~BlockType ();
00435 };
00436 
00437 
00438 // Tuple <type, type, ...>
00439 
00440 class TupleType : public Type
00441 {
00442     REP_BODY(TupleType);
00443 protected:
00444     std::vector <constTypePtr> m_types;
00445 public:
00446     string toString () const;
00447     bool isBasetype () const { return false; }
00448     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00449     int match (constTypePtr expected) const;
00450     bool equals (constTypePtr expected) const;
00451     bool canCast (constTypePtr to) const;
00452     TypePtr clone () const;
00453     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00454     std::ostream & toStream (std::ostream & str) const;
00455     TupleType (constTypePtr type, bool as_const = false);
00456     TupleType (std::istream & str);
00457     void concat (constTypePtr t);
00458     unsigned int parameterCount () const { return m_types.size(); }
00459     constTypePtr parameterType (unsigned int parameter_number) const;
00460     ~TupleType ();
00461 };
00462 
00463 
00464 // Function <returntype, arg1type, arg2type, ...>
00465 
00466 class FunctionType : public Type
00467 {
00468     REP_BODY(FunctionType);
00469 private:
00470     const constTypePtr m_returntype;
00471     TupleTypePtr m_arguments;
00472 public:
00473     FunctionType (constTypePtr return_type, constFunctionTypePtr arguments);
00474     string toString () const;
00475     bool isBasetype () const { return false; }
00476     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00477     int match (constTypePtr expected) const;
00478     bool equals (constTypePtr expected) const;
00479     bool canCast (constTypePtr to) const { return false; }
00480     TypePtr clone () const;
00481     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00482     std::ostream & toStream (std::ostream & str) const;
00483     FunctionType (constTypePtr returntype = Type::Unspec, bool as_const = false);
00484     FunctionType (std::istream & str);
00485     ~FunctionType ();
00486     constTypePtr returnType () const { return m_returntype; }
00487     void concat (constTypePtr t);
00488     int parameterCount () const;
00489     constTypePtr parameterType (unsigned int parameter_number) const;
00490     constTupleTypePtr parameters () const;
00491 };
00492 
00493 
00494 #endif   // Type_h

Generated on Fri Nov 9 18:15:22 2007 for yast2-core by doxygen 1.3.6