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 class bytecodeistream;
00033 
00034 class Type : public Rep
00035 #ifdef D_MEMUSAGE
00036   , public MemUsage
00037 #endif
00038 {
00039     REP_BODY(Type);
00040 
00041 public:
00042     // type codes for basic types
00043     typedef enum type_kind {
00044         UnspecT = 0,            //  0 unspecified
00045         ErrorT,                 //  1 error
00046         AnyT,                   //  2 any
00047         BooleanT,               //  3 boolean
00048         ByteblockT,             //  4 byteblock
00049         FloatT,                 //  5 float
00050         IntegerT,               //  6 integer
00051         LocaleT,                //  7 locale
00052         PathT,                  //  8 path
00053         StringT,                //  9 string
00054         SymbolT,                // 10 symbol
00055         TermT,                  // 11 term
00056         VoidT,                  // 12 void
00057         WildcardT,              // 13 wildcard
00058 
00059         FlexT,                  // 14 flex
00060         VariableT,              // 15 variable <kind>
00061         ListT,                  // 16 list <kind>
00062         MapT,                   // 17 map <key_kind, value_kind>
00063         BlockT,                 // 18 block <kind>
00064         TupleT,                 // 19 tuple <kind, kind, kind, ...>
00065         FunctionT,              // 20 function <ret_kind, kind, kind, ...>
00066 
00067         NilT,                   // 21 only for "return;" (void) vs. "return nil;" (nil)
00068         NFlexT                  // 22 multiple Flex
00069     } tkind;
00070 
00071 protected:
00072     tkind m_kind;
00073     bool m_const;
00074     bool m_reference;
00075 
00076     Type (tkind kind, bool as_const = false, bool as_reference = false) : m_kind (kind), m_const (as_const), m_reference(as_reference) { };
00077 
00078 public:
00079     //-------------------------------------------------
00080     // static member functions
00081 
00085     static void setNocheck (bool nocheck);
00086 
00090     static constTypePtr vt2type (enum YCPValueType vt);
00091 
00095     static int nextToken (const char **signature);
00096 
00100     static constTypePtr fromSignature (const char **signature);
00101 
00106     static constTypePtr fromSignature (const string & signature) { const char *s = signature.c_str(); return Type::fromSignature (&s); }
00107 
00112     static constTypePtr determineFlexType (constFunctionTypePtr actual, constFunctionTypePtr declared);
00113 
00114 public:
00115 
00116     static const constTypePtr Unspec;   /* unspecified type  */
00117     static const constTypePtr Error;    /* error type  */
00118     static const constTypePtr Any;      /* any type  */
00119 
00120     static const constTypePtr Void;     /* void type  */
00121     static const constTypePtr Boolean;  /* boolean type  */
00122     static const constTypePtr Byteblock;/* byteblock type  */
00123     static const constTypePtr Float;    /* float type  */
00124     static const constTypePtr Integer;  /* integer type  */
00125     static const constTypePtr Locale;   /* locale type  */
00126     static const constTypePtr Path;     /* path type  */
00127     static const constTypePtr String;   /* string type  */
00128     static const constTypePtr Symbol;   /* symbol type  */
00129     static const constTypePtr Term;     /* term type  */
00130     static const constTypePtr Wildcard; /* wildcard (...) type  */
00131 
00132     static const constTypePtr ConstAny;         /* any type  */
00133     static const constTypePtr ConstVoid;        /* void type  */
00134     static const constTypePtr ConstBoolean;     /* boolean type  */
00135     static const constTypePtr ConstByteblock;   /* byteblock type  */
00136     static const constTypePtr ConstFloat;       /* float type  */
00137     static const constTypePtr ConstInteger;     /* integer type  */
00138     static const constTypePtr ConstLocale;      /* locale type  */
00139     static const constTypePtr ConstPath;        /* path type  */
00140     static const constTypePtr ConstString;      /* string type  */
00141     static const constTypePtr ConstSymbol;      /* symbol type  */
00142     static const constTypePtr ConstTerm;        /* term type  */
00143 
00144     static const constTypePtr ConstList;        /* list type  */
00145     static const constTypePtr ConstMap;         /* map type  */
00146 
00147     static const constTypePtr Flex;
00148     static const constTypePtr ConstFlex;
00149     static const constTypePtr NFlex1;
00150     static const constTypePtr ConstNFlex1;
00151     static const constTypePtr NFlex2;
00152     static const constTypePtr ConstNFlex2;
00153     static const constTypePtr NFlex3;
00154     static const constTypePtr ConstNFlex3;
00155     static const constTypePtr NFlex4;
00156     static const constTypePtr ConstNFlex4;
00157 
00158     static const constTypePtr ListUnspec;
00159     static const constTypePtr List;
00160     static const constTypePtr MapUnspec;
00161     static const constTypePtr Map;
00162     static const constTypePtr Variable;
00163     static const constTypePtr Block;
00164 
00165     static FunctionTypePtr Function(constTypePtr return_type);
00166 
00167     static const constTypePtr Nil;      /* "return nil;" type */
00168 
00169 private:
00170     /*
00171      * get kind
00172      */
00173     tkind kind () const { return m_kind; }
00174 
00175 public:
00176     Type ();
00177     Type (tkind kind, bytecodeistream & str);
00178     virtual ~Type ();
00179 
00183     virtual string toString () const;
00184 
00188     virtual std::ostream & toStream (std::ostream & str) const;
00189 
00190     /*
00191      * is base or constructed type
00192      */
00193     virtual bool isBasetype () const { return true; }
00194 
00195     /*
00196      * match <flex<number>> to type, return type if <flex<number>> matches
00197      */
00198     virtual constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const { return 0; }
00199 
00204     virtual int match (constTypePtr expected) const;
00205 
00210     virtual int matchvalue (YCPValue value) const;
00211 
00216     virtual bool canCast (constTypePtr to) const;
00217 
00221     virtual TypePtr clone () const;
00222 
00226     virtual constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00227 
00231     string preToString () const { return (m_const ? "const " : ""); }
00232 
00236     string postToString () const { return (m_reference ? " &" : ""); }
00237 
00241     bool isConst () const { return m_const; }
00242 
00246     void asConst () { m_const = true; }
00247 
00251     bool isReference () const { return m_reference; }
00252 
00256     void asReference () { m_reference = true; }
00257 
00262     int basematch (constTypePtr expected) const;
00263 
00267     virtual bool equals (constTypePtr expected) const;
00268 
00269     // ------------------------------------------------------------
00270     // checking types
00271 
00272     // kind
00273     bool isUnspec () const      { return m_kind == UnspecT; }
00274     bool isError () const       { return m_kind == ErrorT; }
00275     bool isAny () const         { return m_kind == AnyT; }
00276     bool isBoolean () const     { return m_kind == BooleanT; }
00277     bool isByteblock () const   { return m_kind == ByteblockT; }
00278     bool isFloat () const       { return m_kind == FloatT; }
00279     bool isInteger () const     { return m_kind == IntegerT; }
00280     bool isLocale () const      { return m_kind == LocaleT; }
00281     bool isPath () const        { return m_kind == PathT; }
00282     bool isString () const      { return m_kind == StringT; }
00283     bool isSymbol () const      { return m_kind == SymbolT; }
00284     bool isTerm () const        { return m_kind == TermT; }
00285     bool isVoid () const        { return m_kind == VoidT; }
00286     bool isWildcard () const    { return m_kind == WildcardT; }
00287     bool isFlex () const        { return ((m_kind == FlexT) || (m_kind == NFlexT)); }
00288     bool isNFlex () const       { return m_kind == NFlexT; }
00289 
00290     bool isVariable () const    { return m_kind == VariableT; }
00291     bool isList () const        { return m_kind == ListT; }
00292     bool isMap () const         { return m_kind == MapT; }
00293     bool isBlock () const       { return m_kind == BlockT; }
00294     bool isTuple () const       { return m_kind == TupleT; }
00295     bool isFunction () const    { return m_kind == FunctionT; }
00296 
00297     bool isNil () const         { return m_kind == NilT; }
00298     // ------------------------------------------------------------
00299     // misc methods
00300 
00301     YCPValueType valueType () const;
00302 
00303     // determine the common type of two types, used to determine the type of lists
00304     // and maps with various elements.
00305     // -> returns the largets (containing least amount of information) matching
00306     //    type (Any if types do not match)
00307     //    the return type is 'least common denominator'
00308     virtual constTypePtr commontype (constTypePtr type) const;
00309 
00310     // determine the more detailed type of two types, used to determine the type of bracket
00311     // element vs. bracket default
00312     // -> returns the smallest (containing most amount of information) matching
00313     //    type (Error if types do not match)
00314     virtual constTypePtr detailedtype (constTypePtr type) const;
00315 };
00316 
00317 // <flex>
00318 
00319 class FlexType : public Type
00320 {
00321     REP_BODY(FlexType);
00322 public:
00323     string toString () const;
00324     std::ostream & toStream (std::ostream & str) const;
00325     bool isBasetype () const { return false; }
00326     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00327     int match (constTypePtr expected) const;
00328     TypePtr clone () const;
00329     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00330     FlexType (bool as_const = false);
00331     FlexType (bytecodeistream & str);
00332     ~FlexType ();
00333 };
00334 
00335 
00336 // <flexN>
00337 
00338 class NFlexType : public Type
00339 {
00340     REP_BODY(NFlexType);
00341     unsigned int m_number;              // there can be more than one flex
00342 public:
00343     string toString () const;
00344     std::ostream & toStream (std::ostream & str) const;
00345     bool isBasetype () const { return false; }
00346     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00347     int match (constTypePtr expected) const;
00348     TypePtr clone () const;
00349     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00350     unsigned int number () const;
00351     NFlexType (unsigned int number, bool as_const = false);
00352     NFlexType (bytecodeistream & str);
00353     ~NFlexType ();
00354 };
00355 
00356 
00357 // Variable <type>
00358 
00359 class VariableType : public Type
00360 {
00361     REP_BODY(VariableType);
00362 private:
00363     const constTypePtr m_type;
00364 public:
00365     string toString () const;
00366     std::ostream & toStream (std::ostream & str) const;
00367     bool isBasetype () const { return false; }
00368     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00369     int match (constTypePtr expected) const;
00370     bool equals (constTypePtr expected) const;
00371     TypePtr clone () const;
00372     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00373     constTypePtr type () const { return m_type; }
00374     VariableType (constTypePtr type = Type::Unspec, bool as_const = false);
00375     VariableType (bytecodeistream & str);
00376     ~VariableType ();
00377 };
00378 
00379 
00380 // List <type>
00381 
00382 class ListType : public Type
00383 {
00384     REP_BODY(ListType);
00385 private:
00386     const constTypePtr m_type;
00387 public:
00388     string toString () const;
00389     bool isBasetype () const { return false; }
00390     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00391     int match (constTypePtr expected) const;
00392     bool equals (constTypePtr expected) const;
00393     constTypePtr commontype (constTypePtr type) const;
00394     constTypePtr detailedtype (constTypePtr type) const;
00395     bool canCast (constTypePtr to) const;
00396     TypePtr clone () const;
00397     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00398     constTypePtr type () const { return m_type; }
00399     std::ostream & toStream (std::ostream & str) const;
00400     ListType (constTypePtr type = Type::Unspec, bool as_const = false);
00401     ListType (bytecodeistream & str);
00402     ~ListType ();
00403 };
00404 
00405 
00406 // Map <keytype, valuetype>
00407 
00408 class MapType : public Type
00409 {
00410     REP_BODY(MapType);
00411 private:
00412     const constTypePtr m_keytype;
00413     const constTypePtr m_valuetype;
00414 public:
00415     string toString () const;
00416     bool isBasetype () const { return false; }
00417     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00418     int match (constTypePtr expected) const;
00419     bool equals (constTypePtr expected) const;
00420     constTypePtr commontype (constTypePtr type) const;
00421     constTypePtr detailedtype (constTypePtr type) const;
00422     bool canCast (constTypePtr to) const;
00423     TypePtr clone () const;
00424     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00425     constTypePtr keytype () const { return m_keytype; }
00426     constTypePtr valuetype () const { return m_valuetype; }
00427     std::ostream & toStream (std::ostream & str) const;
00428     MapType (constTypePtr key = Type::Unspec, constTypePtr value = Type::Unspec, bool as_const = false);
00429     MapType (bytecodeistream & str);
00430     ~MapType ();
00431 };
00432 
00433 
00434 // Block <type>
00435 
00436 class BlockType : public Type
00437 {
00438     REP_BODY(BlockType);
00439 private:
00440     const constTypePtr m_type;
00441 public:
00442     string toString () const;
00443     bool isBasetype () const { return false; }
00444     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00445     int match (constTypePtr expected) const;
00446     bool equals (constTypePtr expected) const;
00447     bool canCast (constTypePtr to) const;
00448     TypePtr clone () const;
00449     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00450     constTypePtr returnType () const { return m_type; }
00451     std::ostream & toStream (std::ostream & str) const;
00452     BlockType (constTypePtr type, bool as_const = false);
00453     BlockType (bytecodeistream & str);
00454     ~BlockType ();
00455 };
00456 
00457 
00458 // Tuple <type, type, ...>
00459 
00460 class TupleType : public Type
00461 {
00462     REP_BODY(TupleType);
00463 protected:
00464     std::vector <constTypePtr> m_types;
00465 public:
00466     string toString () const;
00467     bool isBasetype () const { return false; }
00468     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00469     int match (constTypePtr expected) const;
00470     bool equals (constTypePtr expected) const;
00471     bool canCast (constTypePtr to) const;
00472     TypePtr clone () const;
00473     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00474     std::ostream & toStream (std::ostream & str) const;
00475     TupleType (constTypePtr type, bool as_const = false);
00476     TupleType (bytecodeistream & str);
00477     void concat (constTypePtr t);
00478     unsigned int parameterCount () const { return m_types.size(); }
00479     constTypePtr parameterType (unsigned int parameter_number) const;
00480     ~TupleType ();
00481 };
00482 
00483 
00484 // Function <returntype, arg1type, arg2type, ...>
00485 
00486 class FunctionType : public Type
00487 {
00488     REP_BODY(FunctionType);
00489 private:
00490     const constTypePtr m_returntype;
00491     TupleTypePtr m_arguments;
00492 public:
00493     FunctionType (constTypePtr return_type, constFunctionTypePtr arguments);
00494     string toString () const;
00495     bool isBasetype () const { return false; }
00496     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00497     int match (constTypePtr expected) const;
00498     bool equals (constTypePtr expected) const;
00499     bool canCast (constTypePtr to) const { return false; }
00500     TypePtr clone () const;
00501     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00502     std::ostream & toStream (std::ostream & str) const;
00503     FunctionType (constTypePtr returntype = Type::Unspec, bool as_const = false);
00504     FunctionType (bytecodeistream & str);
00505     ~FunctionType ();
00506     constTypePtr returnType () const { return m_returntype; }
00507     void concat (constTypePtr t);
00508     int parameterCount () const;
00509     constTypePtr parameterType (unsigned int parameter_number) const;
00510     constTupleTypePtr parameters () const;
00511 };
00512 
00513 
00514 #endif   // Type_h

Generated on Fri Jun 16 18:07:45 2006 for yast2-core by  doxygen 1.4.6