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 
00035 class Type : public Rep
00036 #ifdef D_MEMUSAGE
00037   , public MemUsage
00038 #endif
00039 {
00040     REP_BODY(Type);
00041 
00042 public:
00043     // type codes for basic types
00044     typedef enum type_kind {
00045         UnspecT = 0,            //  0 unspecified
00046         ErrorT,                 //  1 error
00047         AnyT,                   //  2 any
00048         BooleanT,               //  3 boolean
00049         ByteblockT,             //  4 byteblock
00050         FloatT,                 //  5 float
00051         IntegerT,               //  6 integer
00052         LocaleT,                //  7 locale
00053         PathT,                  //  8 path
00054         StringT,                //  9 string
00055         SymbolT,                // 10 symbol
00056         TermT,                  // 11 term
00057         VoidT,                  // 12 void
00058         WildcardT,              // 13 wildcard
00059 
00060         FlexT,                  // 14 flex
00061         VariableT,              // 15 variable <kind>
00062         ListT,                  // 16 list <kind>
00063         MapT,                   // 17 map <key_kind, value_kind>
00064         BlockT,                 // 18 block <kind>
00065         TupleT,                 // 19 tuple <kind, kind, kind, ...>
00066         FunctionT,              // 20 function <ret_kind, kind, kind, ...>
00067 
00068         NilT,                   // 21 only for "return;" (void) vs. "return nil;" (nil)
00069         NFlexT                  // 22 multiple Flex
00070     } tkind;
00071 
00072 protected:
00073     tkind m_kind;
00074     bool m_const;
00075     bool m_reference;
00076 
00077     Type (tkind kind, bool as_const = false, bool as_reference = false) : m_kind (kind), m_const (as_const), m_reference(as_reference) { };
00078 
00079 public:
00080     //-------------------------------------------------
00081     // static member functions
00082 
00086     static void setNocheck (bool nocheck);
00087 
00091     static constTypePtr vt2type (enum YCPValueType vt);
00092 
00096     static int nextToken (const char **signature);
00097 
00101     static constTypePtr fromSignature (const char **signature);
00102 
00107     static constTypePtr fromSignature (const string & signature) { const char *s = signature.c_str(); return Type::fromSignature (&s); }
00108 
00113     static constTypePtr determineFlexType (constFunctionTypePtr actual, constFunctionTypePtr declared);
00114 
00115 public:
00116 
00117     static const constTypePtr Unspec;   /* unspecified type  */
00118     static const constTypePtr Error;    /* error type  */
00119     static const constTypePtr Any;      /* any type  */
00120 
00121     static const constTypePtr Void;     /* void type  */
00122     static const constTypePtr Boolean;  /* boolean type  */
00123     static const constTypePtr Byteblock;/* byteblock type  */
00124     static const constTypePtr Float;    /* float type  */
00125     static const constTypePtr Integer;  /* integer type  */
00126     static const constTypePtr Locale;   /* locale type  */
00127     static const constTypePtr Path;     /* path type  */
00128     static const constTypePtr String;   /* string type  */
00129     static const constTypePtr Symbol;   /* symbol type  */
00130     static const constTypePtr Term;     /* term type  */
00131     static const constTypePtr Wildcard; /* wildcard (...) type  */
00132 
00133     static const constTypePtr ConstAny;         /* any type  */
00134     static const constTypePtr ConstVoid;        /* void type  */
00135     static const constTypePtr ConstBoolean;     /* boolean type  */
00136     static const constTypePtr ConstByteblock;   /* byteblock type  */
00137     static const constTypePtr ConstFloat;       /* float type  */
00138     static const constTypePtr ConstInteger;     /* integer type  */
00139     static const constTypePtr ConstLocale;      /* locale type  */
00140     static const constTypePtr ConstPath;        /* path type  */
00141     static const constTypePtr ConstString;      /* string type  */
00142     static const constTypePtr ConstSymbol;      /* symbol type  */
00143     static const constTypePtr ConstTerm;        /* term type  */
00144 
00145     static const constTypePtr ConstList;        /* list type  */
00146     static const constTypePtr ConstMap;         /* map type  */
00147 
00148     static const constTypePtr Flex;
00149     static const constTypePtr ConstFlex;
00150     static const constTypePtr NFlex1;
00151     static const constTypePtr ConstNFlex1;
00152     static const constTypePtr NFlex2;
00153     static const constTypePtr ConstNFlex2;
00154     static const constTypePtr NFlex3;
00155     static const constTypePtr ConstNFlex3;
00156     static const constTypePtr NFlex4;
00157     static const constTypePtr ConstNFlex4;
00158 
00159     static const constTypePtr ListUnspec;
00160     static const constTypePtr List;
00161     static const constTypePtr MapUnspec;
00162     static const constTypePtr Map;
00163     static const constTypePtr Variable;
00164     static const constTypePtr Block;
00165 
00166     static FunctionTypePtr Function(constTypePtr return_type);
00167 
00168     static const constTypePtr Nil;      /* "return nil;" type */
00169 
00170 private:
00171     /*
00172      * get kind
00173      */
00174     tkind kind () const { return m_kind; }
00175 
00176 public:
00177     Type ();
00178     Type (tkind kind, bytecodeistream & str);
00179     virtual ~Type ();
00180 
00184     virtual string toString () const;
00185 
00189     virtual std::ostream & toStream (std::ostream & str) const;
00190 
00191     /*
00192      * is base or constructed type
00193      */
00194     virtual bool isBasetype () const { return true; }
00195 
00196     /*
00197      * match <flex<number>> to type, return type if <flex<number>> matches
00198      */
00199     virtual constTypePtr matchFlex (constTypePtr /*type*/, unsigned int /*number*/ = 0) const { return 0; }
00200 
00205     virtual int match (constTypePtr expected) const;
00206 
00211     virtual int matchvalue (YCPValue value) const;
00212 
00217     virtual bool canCast (constTypePtr to) const;
00218 
00222     virtual TypePtr clone () const;
00223 
00227     virtual constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00228 
00232     string preToString () const { return (m_const ? "const " : ""); }
00233 
00237     string postToString () const { return (m_reference ? " &" : ""); }
00238 
00242     bool isConst () const { return m_const; }
00243 
00247     void asConst () { m_const = true; }
00248 
00252     bool isReference () const { return m_reference; }
00253 
00257     void asReference () { m_reference = true; }
00258 
00263     int basematch (constTypePtr expected) const;
00264 
00268     virtual bool equals (constTypePtr expected) const;
00269 
00270     // ------------------------------------------------------------
00271     // checking types
00272 
00273     // kind
00274     bool isUnspec () const      { return m_kind == UnspecT; }
00275     bool isError () const       { return m_kind == ErrorT; }
00276     bool isAny () const         { return m_kind == AnyT; }
00277     bool isBoolean () const     { return m_kind == BooleanT; }
00278     bool isByteblock () const   { return m_kind == ByteblockT; }
00279     bool isFloat () const       { return m_kind == FloatT; }
00280     bool isInteger () const     { return m_kind == IntegerT; }
00281     bool isLocale () const      { return m_kind == LocaleT; }
00282     bool isPath () const        { return m_kind == PathT; }
00283     bool isString () const      { return m_kind == StringT; }
00284     bool isSymbol () const      { return m_kind == SymbolT; }
00285     bool isTerm () const        { return m_kind == TermT; }
00286     bool isVoid () const        { return m_kind == VoidT; }
00287     bool isWildcard () const    { return m_kind == WildcardT; }
00288     bool isFlex () const        { return ((m_kind == FlexT) || (m_kind == NFlexT)); }
00289     bool isNFlex () const       { return m_kind == NFlexT; }
00290 
00291     bool isVariable () const    { return m_kind == VariableT; }
00292     bool isList () const        { return m_kind == ListT; }
00293     bool isMap () const         { return m_kind == MapT; }
00294     bool isBlock () const       { return m_kind == BlockT; }
00295     bool isTuple () const       { return m_kind == TupleT; }
00296     bool isFunction () const    { return m_kind == FunctionT; }
00297 
00298     bool isNil () const         { return m_kind == NilT; }
00299     // ------------------------------------------------------------
00300     // misc methods
00301 
00302     YCPValueType valueType () const;
00303 
00304     // determine the common type of two types, used to determine the type of lists
00305     // and maps with various elements.
00306     // -> returns the largets (containing least amount of information) matching
00307     //    type (Any if types do not match)
00308     //    the return type is 'least common denominator'
00309     virtual constTypePtr commontype (constTypePtr type) const;
00310 
00311     // determine the more detailed type of two types, used to determine the type of bracket
00312     // element vs. bracket default
00313     // -> returns the smallest (containing most amount of information) matching
00314     //    type (Error if types do not match)
00315     virtual constTypePtr detailedtype (constTypePtr type) const;
00316 };
00317 
00319 
00320 class FlexType : public Type
00321 {
00322     REP_BODY(FlexType);
00323 public:
00324     string toString () const;
00325     std::ostream & toStream (std::ostream & str) const;
00326     bool isBasetype () const { return false; }
00327     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00328     int match (constTypePtr expected) const;
00329     TypePtr clone () const;
00330     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00331     FlexType (bool as_const = false);
00332     FlexType (bytecodeistream & str);
00333     ~FlexType ();
00334 };
00335 
00336 
00338 
00339 class NFlexType : public Type
00340 {
00341     REP_BODY(NFlexType);
00342     unsigned int m_number;              // there can be more than one flex
00343 public:
00344     string toString () const;
00345     std::ostream & toStream (std::ostream & str) const;
00346     bool isBasetype () const { return false; }
00347     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00348     int match (constTypePtr expected) const;
00349     TypePtr clone () const;
00350     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00351     unsigned int number () const;
00352     NFlexType (unsigned int number, bool as_const = false);
00353     NFlexType (bytecodeistream & str);
00354     ~NFlexType ();
00355 };
00356 
00357 
00359 
00360 class VariableType : public Type
00361 {
00362     REP_BODY(VariableType);
00363 private:
00364     const constTypePtr m_type;
00365 public:
00366     string toString () const;
00367     std::ostream & toStream (std::ostream & str) const;
00368     bool isBasetype () const { return false; }
00369     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00370     int match (constTypePtr expected) const;
00371     bool equals (constTypePtr expected) const;
00372     TypePtr clone () const;
00373     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00374     constTypePtr type () const { return m_type; }
00375     VariableType (constTypePtr type = Type::Unspec, bool as_const = false);
00376     VariableType (bytecodeistream & str);
00377     ~VariableType ();
00378 };
00379 
00380 
00382 
00383 class ListType : public Type
00384 {
00385     REP_BODY(ListType);
00386 private:
00387     const constTypePtr m_type;
00388 public:
00389     string toString () const;
00390     bool isBasetype () const { return false; }
00391     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00392     int match (constTypePtr expected) const;
00393     bool equals (constTypePtr expected) const;
00394     constTypePtr commontype (constTypePtr type) const;
00395     constTypePtr detailedtype (constTypePtr type) const;
00396     bool canCast (constTypePtr to) const;
00397     TypePtr clone () const;
00398     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00399     constTypePtr type () const { return m_type; }
00400     std::ostream & toStream (std::ostream & str) const;
00401     ListType (constTypePtr type = Type::Unspec, bool as_const = false);
00402     ListType (bytecodeistream & str);
00403     ~ListType ();
00404 };
00405 
00406 
00408 
00409 class MapType : public Type
00410 {
00411     REP_BODY(MapType);
00412 private:
00413     const constTypePtr m_keytype;
00414     const constTypePtr m_valuetype;
00415 public:
00416     string toString () const;
00417     bool isBasetype () const { return false; }
00418     constTypePtr matchFlex (constTypePtr type, unsigned int number = 0) const;
00419     int match (constTypePtr expected) const;
00420     bool equals (constTypePtr expected) const;
00421     constTypePtr commontype (constTypePtr type) const;
00422     constTypePtr detailedtype (constTypePtr type) const;
00423     bool canCast (constTypePtr to) const;
00424     TypePtr clone () const;
00425     constTypePtr unflex (constTypePtr type, unsigned int number = 0) const;
00426     constTypePtr keytype () const { return m_keytype; }
00427     constTypePtr valuetype () const { return m_valuetype; }
00428     std::ostream & toStream (std::ostream & str) const;
00429     MapType (constTypePtr key = Type::Unspec, constTypePtr value = Type::Unspec, bool as_const = false);
00430     MapType (bytecodeistream & str);
00431     ~MapType ();
00432 };
00433 
00434 
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 
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 
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 Tue Nov 6 01:20:21 2007 for yast2-core by  doxygen 1.5.0