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