00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef Type_h
00021 #define Type_h
00022
00023 #include <iosfwd>
00024 #include <vector>
00025
00026
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
00045 typedef enum type_kind {
00046 UnspecT = 0,
00047 ErrorT,
00048 AnyT,
00049 BooleanT,
00050 ByteblockT,
00051 FloatT,
00052 IntegerT,
00053 LocaleT,
00054 PathT,
00055 StringT,
00056 SymbolT,
00057 TermT,
00058 VoidT,
00059 WildcardT,
00060
00061 FlexT,
00062 VariableT,
00063 ListT,
00064 MapT,
00065 BlockT,
00066 TupleT,
00067 FunctionT,
00068
00069 NilT,
00070 NFlexT
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
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;
00119 static const constTypePtr Error;
00120 static const constTypePtr Any;
00121
00122 static const constTypePtr Void;
00123 static const constTypePtr Boolean;
00124 static const constTypePtr Byteblock;
00125 static const constTypePtr Float;
00126 static const constTypePtr Integer;
00127 static const constTypePtr Locale;
00128 static const constTypePtr Path;
00129 static const constTypePtr String;
00130 static const constTypePtr Symbol;
00131 static const constTypePtr Term;
00132 static const constTypePtr Wildcard;
00133
00134 static const constTypePtr ConstAny;
00135 static const constTypePtr ConstVoid;
00136 static const constTypePtr ConstBoolean;
00137 static const constTypePtr ConstByteblock;
00138 static const constTypePtr ConstFloat;
00139 static const constTypePtr ConstInteger;
00140 static const constTypePtr ConstLocale;
00141 static const constTypePtr ConstPath;
00142 static const constTypePtr ConstString;
00143 static const constTypePtr ConstSymbol;
00144 static const constTypePtr ConstTerm;
00145
00146 static const constTypePtr ConstList;
00147 static const constTypePtr ConstMap;
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;
00170
00171 private:
00172
00173
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
00200
00201 virtual bool isBasetype () const { return true; }
00202
00203
00204
00205
00206 virtual constTypePtr matchFlex (constTypePtr , unsigned int = 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
00279
00280
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
00308
00309 YCPValueType valueType () const;
00310
00311
00312
00313
00314
00315
00316 virtual constTypePtr commontype (constTypePtr type) const;
00317
00318
00319
00320
00321
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;
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 ) 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