|
yast2-core
|
00001 /*---------------------------------------------------------------------\ 00002 | | 00003 | __ __ ____ _____ ____ | 00004 | \ \ / /_ _/ ___|_ _|___ \ | 00005 | \ V / _` \___ \ | | __) | | 00006 | | | (_| |___) || | / __/ | 00007 | |_|\__,_|____/ |_| |_____| | 00008 | | 00009 | core system | 00010 | (C) SuSE GmbH | 00011 \----------------------------------------------------------------------/ 00012 00013 File: YExpression.h 00014 00015 Author: Klaus Kaempf <kkaempf@suse.de> 00016 Maintainer: Klaus Kaempf <kkaempf@suse.de> 00017 00018 This file defines the various 'expressions' in YCode 00019 00020 /-*/ 00021 // -*- c++ -*- 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 // variable ref (-> Block + position) 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 std::ostream & toXml (std::ostream & str, int indent ) const; 00076 constTypePtr type() const { return m_entry->type(); } 00077 }; 00078 00079 //--------------------------------------------------------- 00080 // reference (-> Block + position) 00081 00082 class YEReference : public YCode 00083 { 00084 REP_BODY(YEReference); 00085 SymbolEntryPtr m_entry; 00086 public: 00087 YEReference (SymbolEntryPtr entry); 00088 YEReference (bytecodeistream & str); 00089 ~YEReference () {}; 00090 virtual ykind kind () const { return yeReference; } 00091 const char *name () const; 00092 SymbolEntryPtr entry () const; 00093 string toString () const; 00094 YCPValue evaluate (bool cse = false); 00095 std::ostream & toStream (std::ostream & str) const; 00096 std::ostream & toXml (std::ostream & str, int indent ) const; 00097 constTypePtr type() const; 00098 }; 00099 00100 //--------------------------------------------------------- 00101 // Term (-> name, args) 00102 00103 class YETerm : public YCode 00104 { 00105 REP_BODY(YETerm); 00106 const char *m_name; 00107 ycodelist_t *m_parameters; 00108 public: 00109 YETerm (const char *name); 00110 YETerm (bytecodeistream & str); 00111 ~YETerm (); 00112 virtual ykind kind () const { return yeTerm; } 00113 // dummy is here just to make it similar to YEBuiltin and YEFunction 00114 constTypePtr attachParameter (YCodePtr code, constTypePtr dummy = Type::Unspec); 00115 string toString () const; 00116 const char *name () const; 00117 YCPValue evaluate (bool cse = false); 00118 std::ostream & toStream (std::ostream & str) const; 00119 std::ostream & toXml (std::ostream & str, int indent ) const; 00120 constTypePtr type() const { return Type::Term; } 00121 }; 00122 00123 00124 //--------------------------------------------------------- 00125 // Compare (-> arg1, arg2, type) 00126 00127 class YECompare : public YCode 00128 { 00129 REP_BODY(YECompare); 00130 public: 00131 enum cmp_op { C_NOT = 1, C_EQ = 2, C_LT = 4, // base operations 00132 C_NEQ = C_NOT|C_EQ, 00133 C_LE = C_EQ|C_LT, 00134 C_GE = C_NOT|C_LT, 00135 C_GT = C_NOT|C_EQ|C_LT 00136 }; 00137 typedef cmp_op c_op; 00138 private: 00139 YCodePtr m_left; 00140 c_op m_op; 00141 YCodePtr m_right; 00142 public: 00143 YECompare (YCodePtr left, c_op op, YCodePtr right); 00144 YECompare (bytecodeistream & str); 00145 ~YECompare (); 00146 virtual ykind kind () const { return yeCompare; } 00147 string toString () const; 00148 YCPValue evaluate (bool cse = false); 00149 std::ostream & toStream (std::ostream & str) const; 00150 std::ostream & toXml (std::ostream & str, int indent ) const; 00151 constTypePtr type() const { return Type::Boolean; } 00152 }; 00153 00154 00155 //--------------------------------------------------------- 00156 // locale expression (-> singular, plural, count) 00157 00158 class YELocale : public YCode 00159 { 00160 REP_BODY(YELocale); 00161 const char *m_singular; 00162 const char *m_plural; 00163 YCodePtr m_count; 00164 YLocale::t_uniquedomains::const_iterator m_domain; 00165 public: 00166 YELocale (const char *singular, const char *plural, YCodePtr count, const char *textdomain); 00167 YELocale (bytecodeistream & str); 00168 ~YELocale (); 00169 virtual ykind kind () const { return yeLocale; } 00170 string toString () const; 00171 YCPValue evaluate (bool cse = false); 00172 std::ostream & toStream (std::ostream & str) const; 00173 std::ostream & toXml (std::ostream & str, int indent ) const; 00174 constTypePtr type() const { return Type::Locale; } 00175 }; 00176 00177 00178 //--------------------------------------------------------- 00179 // list expression (-> value, next list value) 00180 00181 class YEList : public YCode 00182 { 00183 REP_BODY(YEList); 00184 ycodelist_t *m_first; 00185 public: 00186 YEList (YCodePtr code); 00187 YEList (bytecodeistream & str); 00188 ~YEList (); 00189 virtual ykind kind () const { return yeList; } 00190 void attach (YCodePtr element); 00191 // YCodePtr code () const; 00192 string toString () const; 00193 YCPValue evaluate (bool cse = false); 00194 std::ostream & toStream (std::ostream & str) const; 00195 std::ostream & toXml (std::ostream & str, int indent ) const; 00196 constTypePtr type() const; 00197 int count () const; 00198 YCodePtr value (int index) const; 00199 }; 00200 00201 00202 //--------------------------------------------------------- 00203 // map expression (-> key, value, next key/value pair) 00204 00205 class YEMap : public YCode 00206 { 00207 REP_BODY(YEMap); 00208 typedef struct mapval { YCodePtr key; YCodePtr value; struct mapval *next; } mapval_t; 00209 mapval_t *m_first; 00210 public: 00211 YEMap (YCodePtr key, YCodePtr value); 00212 YEMap (bytecodeistream & str); 00213 ~YEMap (); 00214 virtual ykind kind () const { return yeMap; } 00215 void attach (YCodePtr key, YCodePtr value); 00216 // YCodePtr key () const; 00217 // YCodePtr value () const; 00218 string toString () const; 00219 YCPValue evaluate (bool cse = false); 00220 std::ostream & toStream (std::ostream & str) const; 00221 std::ostream & toXml (std::ostream & str, int indent ) const; 00222 constTypePtr type() const; 00223 }; 00224 00225 00226 //--------------------------------------------------------- 00227 // propagation expression (-> value, from type, to type) 00228 00229 class YEPropagate : public YCode 00230 { 00231 REP_BODY(YEPropagate); 00232 constTypePtr m_from; 00233 constTypePtr m_to; 00234 YCodePtr m_value; 00235 public: 00236 YEPropagate (YCodePtr value, constTypePtr from, constTypePtr to); 00237 YEPropagate (bytecodeistream & str); 00238 ~YEPropagate (); 00239 virtual ykind kind () const { return yePropagate; } 00240 string toString () const; 00241 bool canPropagate(const YCPValue& value, constTypePtr to_type) const; 00242 YCPValue evaluate (bool cse = false); 00243 std::ostream & toStream (std::ostream & str) const; 00244 std::ostream & toXml (std::ostream & str, int indent ) const; 00245 constTypePtr type() const { return m_to; } 00246 }; 00247 00248 00249 //--------------------------------------------------------- 00250 // unary expression (-> declaration_t, arg) 00251 00252 class YEUnary : public YCode 00253 { 00254 REP_BODY(YEUnary); 00255 declaration_t *m_decl; 00256 YCodePtr m_arg; // argument 00257 public: 00258 YEUnary (declaration_t *decl, YCodePtr arg); // expression 00259 YEUnary (bytecodeistream & str); 00260 ~YEUnary (); 00261 virtual ykind kind () const { return yeUnary; } 00262 declaration_t *decl () const; 00263 // YCodePtr arg () const; 00264 string toString () const; 00265 YCPValue evaluate (bool cse = false); 00266 std::ostream & toStream (std::ostream & str) const; 00267 std::ostream & toXml (std::ostream & str, int indent ) const; 00268 constTypePtr type() const { return ((constFunctionTypePtr)m_decl->type)->returnType (); } 00269 }; 00270 00271 00272 //--------------------------------------------------------- 00273 // binary expression (-> declaration_t, arg1, arg2) 00274 00275 class YEBinary : public YCode 00276 { 00277 REP_BODY(YEBinary); 00278 declaration_t *m_decl; 00279 YCodePtr m_arg1; // argument1 00280 YCodePtr m_arg2; // argument2 00281 public: 00282 YEBinary (declaration_t *decl, YCodePtr arg1, YCodePtr arg2); 00283 YEBinary (bytecodeistream & str); 00284 ~YEBinary (); 00285 virtual ykind kind () const { return yeBinary; } 00286 declaration_t *decl (); 00287 // YCodePtr arg1 () const; 00288 // YCodePtr arg2 () const; 00289 string toString () const; 00290 YCPValue evaluate (bool cse = false); 00291 std::ostream & toStream (std::ostream & str) const; 00292 std::ostream & toXml (std::ostream & str, int indent ) const; 00293 constTypePtr type() const; 00294 }; 00295 00296 00297 //--------------------------------------------------------- 00298 // Triple (? :) expression (-> bool expr, true value, false value) 00299 00300 class YETriple : public YCode 00301 { 00302 REP_BODY(YETriple); 00303 YCodePtr m_expr; // bool expr 00304 YCodePtr m_true; // true value 00305 YCodePtr m_false; // false value 00306 public: 00307 YETriple (YCodePtr a_expr, YCodePtr a_true, YCodePtr a_false); 00308 YETriple (bytecodeistream & str); 00309 ~YETriple (); 00310 virtual ykind kind () const { return yeTriple; } 00311 // YCodePtr expr () const; 00312 // YCodePtr iftrue () const; 00313 // YCodePtr iffalse () const; 00314 string toString () const; 00315 YCPValue evaluate (bool cse = false); 00316 std::ostream & toStream (std::ostream & str) const; 00317 std::ostream & toXml (std::ostream & str, int indent ) const; 00318 constTypePtr type() const { return m_true->type ()->commontype (m_false->type ()); } 00319 }; 00320 00321 00322 //--------------------------------------------------------- 00323 // is (-> expression, type) 00324 00325 class YEIs : public YCode 00326 { 00327 REP_BODY(YEIs); 00328 YCodePtr m_expr; 00329 constTypePtr m_type; 00330 public: 00331 YEIs (YCodePtr expr, constTypePtr type); 00332 YEIs (bytecodeistream & str); 00333 ~YEIs (); 00334 virtual ykind kind () const { return yeIs; } 00335 string toString () const; 00336 YCPValue evaluate (bool cse = false); 00337 std::ostream & toStream (std::ostream & str) const; 00338 std::ostream & toXml (std::ostream & str, int indent ) const; 00339 constTypePtr type() const { return Type::Boolean; } 00340 }; 00341 00342 00343 //--------------------------------------------------------- 00344 // return (-> expression) 00345 00346 class YEReturn : public YCode 00347 { 00348 REP_BODY(YEReturn); 00349 YCodePtr m_expr; 00350 public: 00351 YEReturn (YCodePtr expr); 00352 YEReturn (bytecodeistream & str); 00353 ~YEReturn (); 00354 virtual ykind kind () const { return yeReturn; } 00355 string toString () const; 00356 YCPValue evaluate (bool cse = false); 00357 std::ostream & toStream (std::ostream & str) const; 00358 std::ostream & toXml (std::ostream & str, int indent ) const; 00359 constTypePtr type() const { return m_expr->type(); } 00360 }; 00361 00362 00363 //--------------------------------------------------------- 00364 // bracket (-> expression) 00365 00366 class YEBracket : public YCode 00367 { 00368 REP_BODY(YEBracket); 00369 YCodePtr m_var; // (list, map) variable 00370 YCodePtr m_arg; // bracket arguments 00371 YCodePtr m_def; // default expression 00372 constTypePtr m_resultType; // result type according to the parser 00373 public: 00374 YEBracket (YCodePtr var, YCodePtr arg, YCodePtr def, constTypePtr resultType); 00375 YEBracket (bytecodeistream & str); 00376 ~YEBracket (); 00377 virtual ykind kind () const { return yeBracket; } 00378 string toString () const; 00379 YCPValue evaluate (bool cse = false); 00380 std::ostream & toStream (std::ostream & str) const; 00381 std::ostream & toXml (std::ostream & str, int indent ) const; 00382 constTypePtr type() const { return m_resultType; } 00383 YCodePtr def () const { return m_def; } 00384 }; 00385 00386 00387 //--------------------------------------------------------- 00388 // Builtin ref (-> declaration_t, type, Args) 00389 00390 class YEBuiltin : public YCode 00391 { 00392 REP_BODY(YEBuiltin); 00393 declaration_t *m_decl; 00394 constFunctionTypePtr m_type; 00395 00396 // symbol parameters (NULL, if no symbolic parameters) 00397 YBlockPtr m_parameterblock; 00398 00399 ycodelist_t *m_parameters; 00400 public: 00401 YEBuiltin (declaration_t *decl, YBlockPtr parameterblock = 0, constTypePtr type = 0); 00402 YEBuiltin (bytecodeistream & str); 00403 ~YEBuiltin (); 00404 virtual ykind kind () const { return yeBuiltin; } 00405 declaration_t *decl () const; 00414 constTypePtr finalize (Logger* problem_logger); 00415 // check if m_parameterblock is really needed, drop if not 00416 void closeParameters (); 00417 // see YEFunction::attachParameter 00418 constTypePtr attachParameter (YCodePtr code, constTypePtr type = Type::Unspec); 00419 // attach symbolic variable parameter to function, return created TableEntry 00420 constTypePtr attachSymVariable (const char *name, constTypePtr type, unsigned int line, TableEntry *&tentry); 00421 string toString () const; 00422 YCPValue evaluate (bool cse = false); 00423 std::ostream & toStream (std::ostream & str) const; 00424 std::ostream & toXml (std::ostream & str, int indent ) const; 00425 constTypePtr type () const; 00426 constTypePtr completeType () const; 00427 YBlockPtr parameterBlock () const; 00428 }; 00429 00430 //--------------------------------------------------------- 00431 // Function call - parameter handling common base 00432 00433 class YECall : public YCode 00434 { 00435 REP_BODY(YECall); 00436 protected: 00437 TableEntry* m_entry; 00438 SymbolEntryPtr m_sentry; 00439 YCodePtr *m_parameters; 00440 constTypePtr *m_parameter_types; 00441 Y2Function* m_functioncall; 00442 00443 uint m_next_param_id; 00444 public: 00445 YECall (TableEntry* entry); 00446 YECall (bytecodeistream & str); 00447 ~YECall (); 00448 const SymbolEntryPtr entry () const; 00458 constTypePtr attachParameter (YCodePtr code, constTypePtr type); 00466 virtual constTypePtr finalize (); 00467 string toString () const; 00468 std::ostream & toStream (std::ostream & str) const; 00469 std::ostream & toXml (std::ostream & str, int indent ) const; 00470 constTypePtr type() const; 00471 string qualifiedName () const; 00472 00473 static YECallPtr readCall (bytecodeistream & str); 00474 }; 00475 00476 //--------------------------------------------------------- 00477 // Function ref (-> SymbolEntry ( param, param, ...) ) 00478 00479 class YEFunction : public YECall 00480 { 00481 REP_BODY(YEFunction); 00482 public: 00483 YEFunction (TableEntry* entry); 00484 YEFunction (bytecodeistream & str); 00485 virtual ykind kind () const { return yeFunction; } 00486 virtual YCPValue evaluate (bool cse = false); 00487 virtual constTypePtr finalize (); 00488 }; 00489 00490 //--------------------------------------------------------- 00491 // Function ref (-> SymbolEntry ( param, param, ...) ) 00492 00493 class YEFunctionPointer : public YECall 00494 { 00495 REP_BODY(YEFunctionPointer); 00496 public: 00497 YEFunctionPointer (TableEntry* entry); 00498 YEFunctionPointer (bytecodeistream & str); 00499 virtual ykind kind () const { return yeFunctionPointer; } 00500 virtual YCPValue evaluate (bool cse = false); 00501 }; 00502 00503 //--------------------------------------------------------- 00504 // Function call for outer space (similar to YEFunction) ref (-> SymbolEntry ( param, param, ...) ) 00505 00506 class Y2YCPFunction : public Y2Function 00507 { 00508 YSymbolEntryPtr m_sentry; 00509 YCPValue* m_parameters; 00510 public: 00511 Y2YCPFunction (YSymbolEntryPtr entry); 00512 ~Y2YCPFunction (); 00513 00514 string qualifiedName () const; 00515 string name () const; 00516 00517 // implementation of the Y2Function interface: 00518 00519 virtual bool attachParameter (const YCPValue& arg, const int pos); 00520 virtual constTypePtr wantedParameterType () const; 00521 virtual bool appendParameter (const YCPValue& arg); 00522 virtual bool finishParameters (); 00523 virtual YCPValue evaluateCall (); 00524 virtual bool reset (); 00525 }; 00526 00527 #endif // YExpression_h
1.7.3