|
yast2-core
|
00001 /*-----------------------------------------------------------*- c++ -*-\ 00002 | | 00003 | __ __ ____ _____ ____ | 00004 | \ \ / /_ _/ ___|_ _|___ \ | 00005 | \ V / _` \___ \ | | __) | | 00006 | | | (_| |___) || | / __/ | 00007 | |_|\__,_|____/ |_| |_____| | 00008 | | 00009 | core system | 00010 | (C) SuSE GmbH | 00011 \----------------------------------------------------------------------/ 00012 00013 File: YCode.h 00014 00015 Author: Klaus Kaempf <kkaempf@suse.de> 00016 Maintainer: Klaus Kaempf <kkaempf@suse.de> 00017 00018 /-*/ 00019 // -*- c++ -*- 00020 00021 #ifndef YCode_h 00022 #define YCode_h 00023 00039 #include <string> 00040 using std::string; 00041 00042 // MemUsage.h defines/undefines D_MEMUSAGE 00043 #include <y2util/MemUsage.h> 00044 #include "ycp/YCodePtr.h" 00045 00046 #include "ycp/YCPValue.h" 00047 #include "ycp/YCPString.h" 00048 #include "ycp/Type.h" 00049 #include "ycp/YSymbolEntry.h" 00050 00058 struct ycodelist { 00059 struct ycodelist *next; 00060 YCodePtr code; 00061 constTypePtr type; 00062 }; 00063 typedef struct ycodelist ycodelist_t; 00064 00075 class YCode : public Rep 00076 #ifdef D_MEMUSAGE 00077 , public MemUsage 00078 #endif 00079 { 00080 REP_BODY(YCode); 00081 public: 00082 enum ykind { 00083 yxError = 0, 00084 // [1] Constants (-> YCPValue, except(!) term -> yeLocale) 00085 ycVoid, ycBoolean, ycInteger, ycFloat, // constants 00086 ycString, ycByteblock, ycPath, ycSymbol, 00087 ycList, // list 00088 ycMap, // map 00089 ycTerm, // term 00090 00091 ycEntry, 00092 00093 ycConstant, // -- placeholder -- 00094 ycLocale, // locale constant (gettext) 00095 ycFunction, // a function definition (parameters and body) 00096 00097 // [16] Expressions (-> declaration_t + values) 00098 yePropagate, // type propagation (value, type) 00099 yeUnary, // unary (prefix) operator 00100 yeBinary, // binary (infix) operator 00101 yeTriple, // <exp> ? <exp> : <exp> 00102 yeCompare, // compare 00103 00104 // [21] Value expressions (-> values + internal) 00105 yeLocale, // locale expression (ngettext) 00106 yeList, // list expression 00107 yeMap, // map expression 00108 yeTerm, // <name> ( ...) 00109 yeIs, // is() 00110 yeBracket, // <name> [ <expr>, ... ] : <expr> 00111 00112 // [27] Block (-> linked list of statements) 00113 yeBlock, // block expression 00114 yeReturn, // quoted expression, e.g. "``(<exp>)" which really is "{ return <exp>; }" 00115 00116 // [29] Symbolref (-> SymbolEntry) 00117 yeVariable, // variable ref 00118 yeBuiltin, // builtin ref + args 00119 yeFunction, // function ref + args 00120 yeReference, // reference to a variable (identical to yeVariable but with different semantics) 00121 // SuSE Linux v9.2 00122 yeFunctionPointer, // function pointer 00123 00124 yeExpression, // -- placeholder -- 00125 00126 // [35] Statements (-> YCode + next) 00127 ysTypedef, // typedef 00128 ysVariable, // variable defintion (-> YSAssign) 00129 ysFunction, // function definition 00130 ysAssign, // variable assignment (-> YSAssign) 00131 ysBracket, // <name> [ <expr>, ... ] = <expr> 00132 ysIf, // if, then, else 00133 ysWhile, // while () do ... 00134 ysDo, // do ... while () 00135 ysRepeat, // repeat ... until () 00136 ysExpression, // any expression (function call) 00137 ysReturn, // return 00138 ysBreak, // break 00139 ysContinue, // continue 00140 ysTextdomain, // textdomain 00141 ysInclude, // include 00142 ysFilename, // restore filename after include 00143 ysImport, // import 00144 ysBlock, // a block as statement 00145 ysSwitch, // switch (since 10.0) 00146 ysStatement // [54] -- placeholder -- 00147 }; 00148 00149 public: 00150 00154 YCode (); 00155 00159 virtual ~YCode(); 00160 00166 virtual ykind kind() const = 0; 00167 00173 virtual string toString() const; 00174 00181 static string toString(ykind kind); 00182 00189 virtual std::ostream & toStream (std::ostream & str) const = 0; 00190 00198 virtual std::ostream & toXml (std::ostream & str, int indent ) const = 0; 00199 00205 virtual bool isConstant () const; 00206 00212 bool isError () const; 00213 00219 virtual bool isStatement () const; 00220 00226 virtual bool isBlock () const; 00227 00233 virtual bool isReferenceable () const; 00234 00242 virtual YCPValue evaluate (bool cse = false); 00243 00249 virtual constTypePtr type() const; 00250 }; 00251 00252 00258 class YConst : public YCode 00259 { 00260 REP_BODY(YConst); 00261 ykind m_kind; 00262 YCPValue m_value; // constant (not allowed in union :-( ) 00263 public: 00264 YConst (ykind kind, YCPValue value); // Constant 00265 YConst (ykind kind, bytecodeistream & str); 00266 ~YConst () {}; 00267 YCPValue value() const; 00268 virtual ykind kind() const; 00269 string toString() const; 00270 std::ostream & toStream (std::ostream & str) const; 00271 std::ostream & toXml (std::ostream & str, int indent ) const; 00273 virtual bool isConstant () const { return true; } 00274 YCPValue evaluate (bool cse = false); 00275 constTypePtr type() const; 00276 }; 00277 00278 // bother, 4.3 requires -std=c++0x 00279 // so without a condition in configure.in you can't have code 00280 // that works with 4.2 and 4.3 without warnings 00281 #ifdef HAVE_CXX0X 00282 #include <unordered_map> 00283 #else 00284 #include <ext/hash_map> 00285 #endif 00286 00287 #include <string> 00288 #include <cstddef> 00289 00296 class YELocale; 00297 00298 class YLocale : public YCode 00299 { 00300 REP_BODY(YLocale); 00301 const char *m_locale; // the string to be translated 00302 00303 struct eqstr 00304 { 00305 bool operator()(const char* s1, const char* s2) const 00306 { 00307 return strcmp(s1, s2) == 0; 00308 } 00309 }; 00310 00311 public: 00312 #ifdef HAVE_CXX0X 00313 typedef unordered_map<const char*, bool, hash<const char*>, eqstr> t_uniquedomains; 00314 #else 00315 typedef __gnu_cxx::hash_map<const char*, bool, __gnu_cxx::hash<const char*>, eqstr> t_uniquedomains; 00316 #endif 00317 00318 static t_uniquedomains domains; // keep every textdomain only once 00319 static t_uniquedomains::const_iterator setDomainStatus (const string& domain, bool status); 00320 static void ensureBindDomain (const string& domain); 00321 static void bindDomainDir (const string& domain, const string& domain_path); 00322 static bool findDomain(const string& domain); 00323 YLocale (const char *locale, const char *textdomain); 00324 YLocale (bytecodeistream & str); 00325 ~YLocale (); 00326 virtual ykind kind () const { return ycLocale; } 00327 const char *value () const; 00328 const char *domain () const; 00329 string toString() const; 00330 std::ostream & toStream (std::ostream & str) const; 00331 std::ostream & toXml (std::ostream & str, int indent ) const; 00332 YCPValue evaluate (bool cse = false); 00333 constTypePtr type() const { return Type::Locale; } 00334 00335 private: 00336 00337 t_uniquedomains::const_iterator m_domain; 00338 00339 }; 00340 00347 class YFunction : public YCode 00348 { 00349 REP_BODY(YFunction); 00350 // array of formal arguments of a function 00351 // the formal parameters must be available in the local scope during parse 00352 // of the function body (startDefinition()) and removed afterwards (endDefintion()). 00353 // Keep track of the table entries in this block. 00354 // 00355 // When calling a function during execution, the actual 00356 // arguments (values) are bound to the formal arguments 00357 // (this array) so the function body can be evaluated. 00358 // @see YEFunction::attachActualParameter() 00359 // 00360 // if NULL, it's a (void) function 00361 YBlockPtr m_declaration; 00362 00363 // the function definition ('body') is the block defining this function 00364 YBlockPtr m_definition; 00365 00366 bool m_is_global; 00367 00368 public: 00369 YFunction (YBlockPtr parameterblock, const SymbolEntryPtr entry = 0); 00370 YFunction (bytecodeistream & str); 00371 ~YFunction (); 00372 virtual ykind kind () const { return ycFunction; } 00373 00374 // access to formal parameters 00375 unsigned int parameterCount () const; 00376 YBlockPtr declaration () const; 00377 SymbolEntryPtr parameter (unsigned int position) const; 00378 00379 // access to definition block (= 0 if declaration only) 00380 YBlockPtr definition () const; 00381 void setDefinition (YBlockPtr body); 00382 // read definition from stream 00383 void setDefinition (bytecodeistream & str); 00384 00385 string toStringDeclaration () const; 00386 string toString () const; 00387 std::ostream & toStreamDefinition (std::ostream & str ) const; 00388 std::ostream & toXmlDefinition (std::ostream & str, int indent ) const; 00389 std::ostream & toStream (std::ostream & str ) const; 00390 std::ostream & toXml (std::ostream & str, int indent ) const; 00391 virtual YCPValue evaluate (bool cse = false); 00392 constTypePtr type() const; 00393 }; 00394 00395 00396 #endif // YCode_h
1.7.3