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
00035 class Type : public Rep
00036 #ifdef D_MEMUSAGE
00037 , public MemUsage
00038 #endif
00039 {
00040 REP_BODY(Type);
00041
00042 public:
00043
00044 typedef enum type_kind {
00045 UnspecT = 0,
00046 ErrorT,
00047 AnyT,
00048 BooleanT,
00049 ByteblockT,
00050 FloatT,
00051 IntegerT,
00052 LocaleT,
00053 PathT,
00054 StringT,
00055 SymbolT,
00056 TermT,
00057 VoidT,
00058 WildcardT,
00059
00060 FlexT,
00061 VariableT,
00062 ListT,
00063 MapT,
00064 BlockT,
00065 TupleT,
00066 FunctionT,
00067
00068 NilT,
00069 NFlexT
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
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;
00118 static const constTypePtr Error;
00119 static const constTypePtr Any;
00120
00121 static const constTypePtr Void;
00122 static const constTypePtr Boolean;
00123 static const constTypePtr Byteblock;
00124 static const constTypePtr Float;
00125 static const constTypePtr Integer;
00126 static const constTypePtr Locale;
00127 static const constTypePtr Path;
00128 static const constTypePtr String;
00129 static const constTypePtr Symbol;
00130 static const constTypePtr Term;
00131 static const constTypePtr Wildcard;
00132
00133 static const constTypePtr ConstAny;
00134 static const constTypePtr ConstVoid;
00135 static const constTypePtr ConstBoolean;
00136 static const constTypePtr ConstByteblock;
00137 static const constTypePtr ConstFloat;
00138 static const constTypePtr ConstInteger;
00139 static const constTypePtr ConstLocale;
00140 static const constTypePtr ConstPath;
00141 static const constTypePtr ConstString;
00142 static const constTypePtr ConstSymbol;
00143 static const constTypePtr ConstTerm;
00144
00145 static const constTypePtr ConstList;
00146 static const constTypePtr ConstMap;
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;
00169
00170 private:
00171
00172
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
00193
00194 virtual bool isBasetype () const { return true; }
00195
00196
00197
00198
00199 virtual constTypePtr matchFlex (constTypePtr , unsigned int = 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
00272
00273
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
00301
00302 YCPValueType valueType () const;
00303
00304
00305
00306
00307
00308
00309 virtual constTypePtr commontype (constTypePtr type) const;
00310
00311
00312
00313
00314
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;
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 ) 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