yast2-core
YCode.h
Go to the documentation of this file.
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         // internal
00149         yiBreakpoint            // [55] -- debugger breakpoint
00150     };
00151 
00152 public:
00153 
00157     YCode ();
00158 
00162     virtual ~YCode();
00163 
00169     virtual ykind kind() const = 0;
00170    
00176     virtual string toString() const;
00177 
00184     static string toString(ykind kind);
00185 
00192     virtual std::ostream & toStream (std::ostream & str) const = 0;
00193 
00201     virtual std::ostream & toXml (std::ostream & str, int indent ) const = 0;
00202 
00208     virtual bool isConstant () const;
00209 
00215     bool isError () const;
00216 
00222     virtual bool isStatement () const;
00223 
00229     virtual bool isBlock () const;
00230 
00236     virtual bool isReferenceable () const;
00237 
00245     virtual YCPValue evaluate (bool cse = false);
00246 
00252   virtual constTypePtr type() const;
00253 };
00254 
00255 
00261 class YConst : public YCode
00262 {
00263     REP_BODY(YConst);
00264     ykind m_kind;
00265     YCPValue m_value;           // constant (not allowed in union :-( )
00266 public:
00267     YConst (ykind kind, YCPValue value);                // Constant
00268     YConst (ykind kind, bytecodeistream & str);
00269     ~YConst () {};
00270     YCPValue value() const;
00271     virtual ykind kind() const;
00272     string toString() const;
00273     std::ostream & toStream (std::ostream & str) const;
00274     std::ostream & toXml (std::ostream & str, int indent ) const;
00276     virtual bool isConstant () const { return true; }
00277     YCPValue evaluate (bool cse = false);
00278     constTypePtr type() const;
00279 };
00280 
00281 // bother, 4.3 requires -std=c++0x
00282 // so without a condition in configure.in you can't have code
00283 // that works with 4.2 and 4.3 without warnings
00284 #ifdef HAVE_CXX0X
00285 #include <unordered_map>
00286 #else
00287 #include <ext/hash_map>
00288 #endif
00289 
00290 #include <string>
00291 #include <cstddef>
00292 
00299 class YELocale;
00300 
00301 class YLocale : public YCode
00302 {
00303     REP_BODY(YLocale);
00304     const char *m_locale;               // the string to be translated
00305 
00306     struct eqstr
00307     {
00308         bool operator()(const char* s1, const char* s2) const
00309         {
00310             return strcmp(s1, s2) == 0;
00311         }
00312     };
00313 
00314 public:
00315 #ifdef HAVE_CXX0X
00316     typedef unordered_map<const char*, bool, hash<const char*>, eqstr> t_uniquedomains;
00317 #else
00318     typedef __gnu_cxx::hash_map<const char*, bool, __gnu_cxx::hash<const char*>, eqstr> t_uniquedomains;
00319 #endif
00320 
00321     static t_uniquedomains domains;     // keep every textdomain only once
00322     static t_uniquedomains::const_iterator setDomainStatus (const string& domain, bool status);
00323     static void ensureBindDomain (const string& domain);
00324     static void bindDomainDir (const string& domain, const string& domain_path);
00325     static bool findDomain(const string& domain);
00326     YLocale (const char *locale, const char *textdomain);
00327     YLocale (bytecodeistream & str);
00328     ~YLocale ();
00329     virtual ykind kind () const { return ycLocale; }
00330     const char *value () const;
00331     const char *domain () const;
00332     string toString() const;
00333     std::ostream & toStream (std::ostream & str) const;
00334     std::ostream & toXml (std::ostream & str, int indent ) const;
00335     YCPValue evaluate (bool cse = false);
00336     constTypePtr type() const { return Type::Locale; }
00337     
00338 private:
00339 
00340     t_uniquedomains::const_iterator m_domain;
00341 
00342 };
00343 
00350 class YFunction : public YCode
00351 {
00352     REP_BODY(YFunction);
00353     // array of formal arguments of a function
00354     // the formal parameters must be available in the local scope during parse
00355     // of the function body (startDefinition()) and removed afterwards (endDefintion()).
00356     // Keep track of the table entries in this block.
00357     //
00358     // When calling a function during execution, the actual
00359     // arguments (values) are bound to the formal arguments
00360     // (this array) so the function body can be evaluated.
00361     // @see YEFunction::attachActualParameter()
00362     //
00363     // if NULL, it's a (void) function
00364     YBlockPtr m_declaration;
00365 
00366     // the function definition ('body') is the block defining this function
00367     YCodePtr m_definition;
00368 
00369     bool m_is_global;
00370 
00371 public:
00372     YFunction (YBlockPtr parameterblock, const SymbolEntryPtr entry = 0);
00373     YFunction (bytecodeistream & str);
00374     ~YFunction ();
00375     virtual ykind kind () const { return ycFunction; }
00376 
00377     // access to formal parameters
00378     unsigned int parameterCount () const;
00379     YBlockPtr declaration () const;
00380     SymbolEntryPtr parameter (unsigned int position) const;
00381 
00382     // access to definition block (= 0 if declaration only)
00383     YCodePtr definition () const;
00384     void setDefinition (YBlockPtr body);
00385     void setDefinition (YBreakpointPtr body);
00386     // read definition from stream
00387     void setDefinition (bytecodeistream & str);
00388 
00389     string toStringDeclaration () const;
00390     string toString () const;
00391     std::ostream & toStreamDefinition (std::ostream & str ) const;
00392     std::ostream & toXmlDefinition (std::ostream & str, int indent ) const;
00393     std::ostream & toStream (std::ostream & str ) const;
00394     std::ostream & toXml (std::ostream & str, int indent ) const;
00395     virtual YCPValue evaluate (bool cse = false);
00396     constTypePtr type() const;
00397 };
00398 
00399 
00400 #endif   // YCode_h

Generated on a sunny day for yast2-core by doxygen 1.7.5.1