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