00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef YExpression_h
00024 #define YExpression_h
00025
00026 #include <iosfwd>
00027 #include <string>
00028 using std::string;
00029
00030 #include "ycp/YCode.h"
00031 #include "y2/Y2Function.h"
00032
00033 class Logger;
00034
00035
00036
00037 DEFINE_DERIVED_POINTER(YEVariable, YCode);
00038 DEFINE_DERIVED_POINTER(YEReference, YCode);
00039 DEFINE_DERIVED_POINTER(YETerm, YCode);
00040 DEFINE_DERIVED_POINTER(YECompare, YCode);
00041 DEFINE_DERIVED_POINTER(YELocale, YCode);
00042 DEFINE_DERIVED_POINTER(YEList, YCode);
00043 DEFINE_DERIVED_POINTER(YEMap, YCode);
00044 DEFINE_DERIVED_POINTER(YEPropagate, YCode);
00045 DEFINE_DERIVED_POINTER(YEUnary, YCode);
00046 DEFINE_DERIVED_POINTER(YEBinary, YCode);
00047 DEFINE_DERIVED_POINTER(YETriple, YCode);
00048 DEFINE_DERIVED_POINTER(YEIs, YCode);
00049 DEFINE_DERIVED_POINTER(YEReturn, YCode);
00050 DEFINE_DERIVED_POINTER(YEBracket, YCode);
00051 DEFINE_DERIVED_POINTER(YEBuiltin, YCode);
00052 DEFINE_DERIVED_POINTER(YECall, YCode);
00053 DEFINE_DERIVED_POINTER(YEFunction, YECall);
00054 DEFINE_DERIVED_POINTER(YEFunctionPointer, YECall);
00055
00056
00057
00058
00059 class YEVariable : public YCode
00060 {
00061 REP_BODY(YEVariable);
00062 SymbolEntryPtr m_entry;
00063 public:
00064 YEVariable (SymbolEntryPtr entry);
00065 YEVariable (bytecodeistream & str);
00066 ~YEVariable () {};
00067 virtual ykind kind () const { return yeVariable; }
00068 const char *name () const;
00069 SymbolEntryPtr entry () const;
00070 string toString () const;
00072 virtual bool isReferenceable () const { return true; }
00073 YCPValue evaluate (bool cse = false);
00074 std::ostream & toStream (std::ostream & str) const;
00075 constTypePtr type() const { return m_entry->type(); }
00076 };
00077
00078
00079
00080
00081 class YEReference : public YCode
00082 {
00083 REP_BODY(YEReference);
00084 SymbolEntryPtr m_entry;
00085 public:
00086 YEReference (SymbolEntryPtr entry);
00087 YEReference (bytecodeistream & str);
00088 ~YEReference () {};
00089 virtual ykind kind () const { return yeReference; }
00090 const char *name () const;
00091 SymbolEntryPtr entry () const;
00092 string toString () const;
00093 YCPValue evaluate (bool cse = false);
00094 std::ostream & toStream (std::ostream & str) const;
00095 constTypePtr type() const;
00096 };
00097
00098
00099
00100
00101 class YETerm : public YCode
00102 {
00103 REP_BODY(YETerm);
00104 const char *m_name;
00105 ycodelist_t *m_parameters;
00106 public:
00107 YETerm (const char *name);
00108 YETerm (bytecodeistream & str);
00109 ~YETerm ();
00110 virtual ykind kind () const { return yeTerm; }
00111
00112 constTypePtr attachParameter (YCodePtr code, constTypePtr dummy = Type::Unspec);
00113 string toString () const;
00114 const char *name () const;
00115 YCPValue evaluate (bool cse = false);
00116 std::ostream & toStream (std::ostream & str) const;
00117 constTypePtr type() const { return Type::Term; }
00118 };
00119
00120
00121
00122
00123
00124 class YECompare : public YCode
00125 {
00126 REP_BODY(YECompare);
00127 public:
00128 enum cmp_op { C_NOT = 1, C_EQ = 2, C_LT = 4,
00129 C_NEQ = C_NOT|C_EQ,
00130 C_LE = C_EQ|C_LT,
00131 C_GE = C_NOT|C_LT,
00132 C_GT = C_NOT|C_EQ|C_LT
00133 };
00134 typedef cmp_op c_op;
00135 private:
00136 YCodePtr m_left;
00137 c_op m_op;
00138 YCodePtr m_right;
00139 public:
00140 YECompare (YCodePtr left, c_op op, YCodePtr right);
00141 YECompare (bytecodeistream & str);
00142 ~YECompare ();
00143 virtual ykind kind () const { return yeCompare; }
00144 string toString () const;
00145 YCPValue evaluate (bool cse = false);
00146 std::ostream & toStream (std::ostream & str) const;
00147 constTypePtr type() const { return Type::Boolean; }
00148 };
00149
00150
00151
00152
00153
00154 class YELocale : public YCode
00155 {
00156 REP_BODY(YELocale);
00157 const char *m_singular;
00158 const char *m_plural;
00159 YCodePtr m_count;
00160 YLocale::t_uniquedomains::const_iterator m_domain;
00161 public:
00162 YELocale (const char *singular, const char *plural, YCodePtr count, const char *textdomain);
00163 YELocale (bytecodeistream & str);
00164 ~YELocale ();
00165 virtual ykind kind () const { return yeLocale; }
00166 string toString () const;
00167 YCPValue evaluate (bool cse = false);
00168 std::ostream & toStream (std::ostream & str) const;
00169 constTypePtr type() const { return Type::Locale; }
00170 };
00171
00172
00173
00174
00175
00176 class YEList : public YCode
00177 {
00178 REP_BODY(YEList);
00179 ycodelist_t *m_first;
00180 public:
00181 YEList (YCodePtr code);
00182 YEList (bytecodeistream & str);
00183 ~YEList ();
00184 virtual ykind kind () const { return yeList; }
00185 void attach (YCodePtr element);
00186
00187 string toString () const;
00188 YCPValue evaluate (bool cse = false);
00189 std::ostream & toStream (std::ostream & str) const;
00190 constTypePtr type() const;
00191 int count () const;
00192 YCodePtr value (int index) const;
00193 };
00194
00195
00196
00197
00198
00199 class YEMap : public YCode
00200 {
00201 REP_BODY(YEMap);
00202 typedef struct mapval { YCodePtr key; YCodePtr value; struct mapval *next; } mapval_t;
00203 mapval_t *m_first;
00204 public:
00205 YEMap (YCodePtr key, YCodePtr value);
00206 YEMap (bytecodeistream & str);
00207 ~YEMap ();
00208 virtual ykind kind () const { return yeMap; }
00209 void attach (YCodePtr key, YCodePtr value);
00210
00211
00212 string toString () const;
00213 YCPValue evaluate (bool cse = false);
00214 std::ostream & toStream (std::ostream & str) const;
00215 constTypePtr type() const;
00216 };
00217
00218
00219
00220
00221
00222 class YEPropagate : public YCode
00223 {
00224 REP_BODY(YEPropagate);
00225 constTypePtr m_from;
00226 constTypePtr m_to;
00227 YCodePtr m_value;
00228 public:
00229 YEPropagate (YCodePtr value, constTypePtr from, constTypePtr to);
00230 YEPropagate (bytecodeistream & str);
00231 ~YEPropagate ();
00232 virtual ykind kind () const { return yePropagate; }
00233 string toString () const;
00234 bool canPropagate(const YCPValue& value, constTypePtr to_type) const;
00235 YCPValue evaluate (bool cse = false);
00236 std::ostream & toStream (std::ostream & str) const;
00237 constTypePtr type() const { return m_to; }
00238 };
00239
00240
00241
00242
00243
00244 class YEUnary : public YCode
00245 {
00246 REP_BODY(YEUnary);
00247 declaration_t *m_decl;
00248 YCodePtr m_arg;
00249 public:
00250 YEUnary (declaration_t *decl, YCodePtr arg);
00251 YEUnary (bytecodeistream & str);
00252 ~YEUnary ();
00253 virtual ykind kind () const { return yeUnary; }
00254 declaration_t *decl () const;
00255
00256 string toString () const;
00257 YCPValue evaluate (bool cse = false);
00258 std::ostream & toStream (std::ostream & str) const;
00259 constTypePtr type() const { return ((constFunctionTypePtr)m_decl->type)->returnType (); }
00260 };
00261
00262
00263
00264
00265
00266 class YEBinary : public YCode
00267 {
00268 REP_BODY(YEBinary);
00269 declaration_t *m_decl;
00270 YCodePtr m_arg1;
00271 YCodePtr m_arg2;
00272 public:
00273 YEBinary (declaration_t *decl, YCodePtr arg1, YCodePtr arg2);
00274 YEBinary (bytecodeistream & str);
00275 ~YEBinary ();
00276 virtual ykind kind () const { return yeBinary; }
00277 declaration_t *decl ();
00278
00279
00280 string toString () const;
00281 YCPValue evaluate (bool cse = false);
00282 std::ostream & toStream (std::ostream & str) const;
00283 constTypePtr type() const;
00284 };
00285
00286
00287
00288
00289
00290 class YETriple : public YCode
00291 {
00292 REP_BODY(YETriple);
00293 YCodePtr m_expr;
00294 YCodePtr m_true;
00295 YCodePtr m_false;
00296 public:
00297 YETriple (YCodePtr a_expr, YCodePtr a_true, YCodePtr a_false);
00298 YETriple (bytecodeistream & str);
00299 ~YETriple ();
00300 virtual ykind kind () const { return yeTriple; }
00301
00302
00303
00304 string toString () const;
00305 YCPValue evaluate (bool cse = false);
00306 std::ostream & toStream (std::ostream & str) const;
00307 constTypePtr type() const { return m_true->type ()->commontype (m_false->type ()); }
00308 };
00309
00310
00311
00312
00313
00314 class YEIs : public YCode
00315 {
00316 REP_BODY(YEIs);
00317 YCodePtr m_expr;
00318 constTypePtr m_type;
00319 public:
00320 YEIs (YCodePtr expr, constTypePtr type);
00321 YEIs (bytecodeistream & str);
00322 ~YEIs ();
00323 virtual ykind kind () const { return yeIs; }
00324 string toString () const;
00325 YCPValue evaluate (bool cse = false);
00326 std::ostream & toStream (std::ostream & str) const;
00327 constTypePtr type() const { return Type::Boolean; }
00328 };
00329
00330
00331
00332
00333
00334 class YEReturn : public YCode
00335 {
00336 REP_BODY(YEReturn);
00337 YCodePtr m_expr;
00338 public:
00339 YEReturn (YCodePtr expr);
00340 YEReturn (bytecodeistream & str);
00341 ~YEReturn ();
00342 virtual ykind kind () const { return yeReturn; }
00343 string toString () const;
00344 YCPValue evaluate (bool cse = false);
00345 std::ostream & toStream (std::ostream & str) const;
00346 constTypePtr type() const { return m_expr->type(); }
00347 };
00348
00349
00350
00351
00352
00353 class YEBracket : public YCode
00354 {
00355 REP_BODY(YEBracket);
00356 YCodePtr m_var;
00357 YCodePtr m_arg;
00358 YCodePtr m_def;
00359 constTypePtr m_resultType;
00360 public:
00361 YEBracket (YCodePtr var, YCodePtr arg, YCodePtr def, constTypePtr resultType);
00362 YEBracket (bytecodeistream & str);
00363 ~YEBracket ();
00364 virtual ykind kind () const { return yeBracket; }
00365 string toString () const;
00366 YCPValue evaluate (bool cse = false);
00367 std::ostream & toStream (std::ostream & str) const;
00368 constTypePtr type() const { return m_resultType; }
00369 YCodePtr def () const { return m_def; }
00370 };
00371
00372
00373
00374
00375
00376 class YEBuiltin : public YCode
00377 {
00378 REP_BODY(YEBuiltin);
00379 declaration_t *m_decl;
00380 constFunctionTypePtr m_type;
00381
00382
00383 YBlockPtr m_parameterblock;
00384
00385 ycodelist_t *m_parameters;
00386 public:
00387 YEBuiltin (declaration_t *decl, YBlockPtr parameterblock = 0, constTypePtr type = 0);
00388 YEBuiltin (bytecodeistream & str);
00389 ~YEBuiltin ();
00390 virtual ykind kind () const { return yeBuiltin; }
00391 declaration_t *decl () const;
00400 constTypePtr finalize (Logger* problem_logger);
00401
00402 void closeParameters ();
00403
00404 constTypePtr attachParameter (YCodePtr code, constTypePtr type = Type::Unspec);
00405
00406 constTypePtr attachSymVariable (const char *name, constTypePtr type, unsigned int line, TableEntry *&tentry);
00407 string toString () const;
00408 YCPValue evaluate (bool cse = false);
00409 std::ostream & toStream (std::ostream & str) const;
00410 constTypePtr type () const;
00411 constTypePtr completeType () const;
00412 YBlockPtr parameterBlock () const;
00413 };
00414
00415
00416
00417
00418 class YECall : public YCode
00419 {
00420 REP_BODY(YECall);
00421 protected:
00422 TableEntry* m_entry;
00423 SymbolEntryPtr m_sentry;
00424 YCodePtr *m_parameters;
00425 constTypePtr *m_parameter_types;
00426 Y2Function* m_functioncall;
00427
00428 uint m_next_param_id;
00429 public:
00430 YECall (TableEntry* entry);
00431 YECall (bytecodeistream & str);
00432 ~YECall ();
00433 const SymbolEntryPtr entry () const;
00443 constTypePtr attachParameter (YCodePtr code, constTypePtr type);
00451 virtual constTypePtr finalize ();
00452 string toString () const;
00453 std::ostream & toStream (std::ostream & str) const;
00454 constTypePtr type() const;
00455 string qualifiedName () const;
00456
00457 static YECallPtr readCall (bytecodeistream & str);
00458 };
00459
00460
00461
00462
00463 class YEFunction : public YECall
00464 {
00465 REP_BODY(YEFunction);
00466 public:
00467 YEFunction (TableEntry* entry);
00468 YEFunction (bytecodeistream & str);
00469 virtual ykind kind () const { return yeFunction; }
00470 virtual YCPValue evaluate (bool cse = false);
00471 virtual constTypePtr finalize ();
00472 };
00473
00474
00475
00476
00477 class YEFunctionPointer : public YECall
00478 {
00479 REP_BODY(YEFunctionPointer);
00480 public:
00481 YEFunctionPointer (TableEntry* entry);
00482 YEFunctionPointer (bytecodeistream & str);
00483 virtual ykind kind () const { return yeFunctionPointer; }
00484 virtual YCPValue evaluate (bool cse = false);
00485 };
00486
00487
00488
00489
00490 class Y2YCPFunction : public Y2Function
00491 {
00492 YSymbolEntryPtr m_sentry;
00493 YCPValue* m_parameters;
00494 public:
00495 Y2YCPFunction (YSymbolEntryPtr entry);
00496 ~Y2YCPFunction ();
00497
00498 string qualifiedName () const;
00499 string name () const;
00500
00501
00502
00503 virtual bool attachParameter (const YCPValue& arg, const int pos);
00504 virtual constTypePtr wantedParameterType () const;
00505 virtual bool appendParameter (const YCPValue& arg);
00506 virtual bool finishParameters ();
00507 virtual YCPValue evaluateCall ();
00508 virtual bool reset ();
00509 };
00510
00511 #endif // YExpression_h