YCode.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
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 
00024 #include <string>
00025 using std::string;
00026 
00027 // MemUsage.h defines/undefines D_MEMUSAGE
00028 #include <y2util/MemUsage.h>
00029 #include "ycp/YCodePtr.h"
00030 
00031 #include "ycp/YCPValue.h"
00032 #include "ycp/YCPString.h"
00033 #include "ycp/Type.h"
00034 #include "ycp/YSymbolEntry.h"
00035 
00043 struct ycodelist {
00044     struct ycodelist *next;
00045     YCodePtr code;
00046     constTypePtr type;
00047 };
00048 typedef struct ycodelist ycodelist_t;
00049 
00053 class YCode : public Rep
00054 #ifdef D_MEMUSAGE
00055   , public MemUsage
00056 #endif
00057 {
00058     REP_BODY(YCode);
00059 public:
00060     enum ykind {
00061         yxError = 0,
00062         // [1] Constants        (-> YCPValue, except(!) term -> yeLocale)
00063         ycVoid, ycBoolean, ycInteger, ycFloat,  // constants
00064         ycString, ycByteblock, ycPath, ycSymbol,
00065         ycList,                                 // list
00066         ycMap,                                  // map
00067         ycTerm,                                 // term
00068 
00069         ycEntry,
00070 
00071         ycConstant,             // -- placeholder --
00072         ycLocale,                               // locale constant (gettext)
00073         ycFunction,                             // a function definition (parameters and body)
00074 
00075         // [16] Expressions     (-> declaration_t + values)
00076         yePropagate,            // type propagation (value, type)
00077         yeUnary,                // unary (prefix) operator
00078         yeBinary,               // binary (infix) operator
00079         yeTriple,               // <exp> ? <exp> : <exp>
00080         yeCompare,              // compare
00081 
00082         // [21] Value expressions (-> values + internal)
00083         yeLocale,               // locale expression (ngettext)
00084         yeList,                 // list expression
00085         yeMap,                  // map expression
00086         yeTerm,                 // <name> ( ...)
00087         yeIs,                   // is()
00088         yeBracket,              // <name> [ <expr>, ... ] : <expr>
00089 
00090         // [27] Block (-> linked list of statements)
00091         yeBlock,                // block expression
00092         yeReturn,               // quoted expression, e.g. "``(<exp>)" which really is "{ return <exp>; }"
00093 
00094         // [29] Symbolref (-> SymbolEntry)
00095         yeVariable,             // variable ref
00096         yeBuiltin,              // builtin ref + args
00097         yeFunction,             // function ref + args
00098         yeReference,            // reference to a variable (identical to yeVariable but with different semantics)
00099         // SuSE Linux v9.2
00100         yeFunctionPointer,      // function pointer
00101 
00102         yeExpression,           // -- placeholder --
00103 
00104         // [35] Statements      (-> YCode + next)
00105         ysTypedef,              // typedef
00106         ysVariable,             // variable defintion (-> YSAssign)
00107         ysFunction,             // function definition
00108         ysAssign,               // variable assignment (-> YSAssign)
00109         ysBracket,              // <name> [ <expr>, ... ] = <expr>
00110         ysIf,                   // if, then, else
00111         ysWhile,                // while () do ...
00112         ysDo,                   // do ... while ()
00113         ysRepeat,               // repeat ... until ()
00114         ysExpression,           //  any expression (function call)
00115         ysReturn,               // return
00116         ysBreak,                // break
00117         ysContinue,             // continue
00118         ysTextdomain,           // textdomain
00119         ysInclude,              // include
00120         ysFilename,             //  restore filename after include
00121         ysImport,               // import
00122         ysBlock,                // a block as statement
00123         ysSwitch,               // switch (since 10.0)
00124         ysStatement             // [54] -- placeholder --
00125     };
00126 
00127 protected:
00128     ykind m_kind;
00129     bool m_valid;
00130 
00131 public:
00132 
00136     YCode (ykind kind);
00137 
00141     virtual ~YCode();
00142 
00146     ykind kind() const;
00147    
00151     bool valid() const;
00152 
00156     virtual string toString() const;
00157     static string toString(ykind kind);
00158 
00163     virtual std::ostream & toStream (std::ostream & str) const = 0;
00164 
00168     bool isConstant () const;
00169 
00173     bool isError () const;
00174 
00178     bool isStatement () const;
00179 
00183     bool isBlock () const;
00184 
00188     bool isReferenceable () const;
00189 
00197     virtual YCPValue evaluate (bool cse = false);
00198 
00202   virtual constTypePtr type() const;
00203 };
00204 
00205 
00210 class YConst : public YCode
00211 {
00212     REP_BODY(YConst);
00213     YCPValue m_value;           // constant (not allowed in union :-( )
00214 public:
00215     YConst (ykind kind, YCPValue value);                // Constant
00216     YConst (ykind kind, bytecodeistream & str);
00217     ~YConst () {};
00218     YCPValue value() const;
00219     string toString() const;
00220     std::ostream & toStream (std::ostream & str) const;
00221     YCPValue evaluate (bool cse = false);
00222     constTypePtr type() const;
00223 };
00224 
00225 #include <ext/hash_map>
00226 #include <string>
00227 #include <cstddef>
00228 
00235 class YELocale;
00236 
00237 class YLocale : public YCode
00238 {
00239     REP_BODY(YLocale);
00240     const char *m_locale;               // the string to be translated
00241 
00242     struct eqstr
00243     {
00244         bool operator()(const char* s1, const char* s2) const
00245         {
00246             return strcmp(s1, s2) == 0;
00247         }
00248     };
00249 
00250 public:
00251     typedef __gnu_cxx::hash_map<const char*, bool, __gnu_cxx::hash<const char*>, eqstr> t_uniquedomains;
00252 
00253     static t_uniquedomains domains;     // keep every textdomain only once
00254     static t_uniquedomains::const_iterator setDomainStatus (const string& domain, bool status);
00255     static void ensureBindDomain (const string& domain);
00256 
00257     YLocale (const char *locale, const char *textdomain);
00258     YLocale (bytecodeistream & str);
00259     ~YLocale ();
00260     const char *value () const;
00261     const char *domain () const;
00262     string toString() const;
00263     std::ostream & toStream (std::ostream & str) const;
00264     YCPValue evaluate (bool cse = false);
00265     constTypePtr type() const { return Type::Locale; }
00266     
00267 private:
00268 
00269     t_uniquedomains::const_iterator m_domain;
00270 
00271 };
00272 
00277 class YDeclaration : public YCode
00278 {
00279     REP_BODY(YDeclaration);
00280     declaration_t *m_value;     // builtin declaration
00281 public:
00282     YDeclaration (ykind kind, declaration_t *value);    // Builtin decl
00283     YDeclaration (bytecodeistream & str);
00284     ~YDeclaration () {};
00285     declaration_t *value() const;
00286     string toString() const;
00287     std::ostream & toStream (std::ostream & str) const;
00288     YCPValue evaluate (bool cse = false);
00289     constTypePtr type() const { return m_value->type; }
00290 };
00291 
00298 class YFunction : public YCode
00299 {
00300     REP_BODY(YFunction);
00301     // array of formal arguments of a function
00302     // the formal parameters must be available in the local scope during parse
00303     // of the function body (startDefinition()) and removed afterwards (endDefintion()).
00304     // Keep track of the table entries in this block.
00305     //
00306     // When calling a function during execution, the actual
00307     // arguments (values) are bound to the formal arguments
00308     // (this array) so the function body can be evaluated.
00309     // @see YEFunction::attachActualParameter()
00310     //
00311     // if NULL, it's a (void) function
00312     YBlockPtr m_declaration;
00313 
00314     // the function definition ('body') is the block defining this function
00315     YBlockPtr m_definition;
00316 
00317     bool m_is_global;
00318 
00319 public:
00320     YFunction (YBlockPtr parameterblock, const SymbolEntryPtr entry = 0);
00321     YFunction (bytecodeistream & str);
00322     ~YFunction ();
00323 
00324     // access to formal parameters
00325     unsigned int parameterCount () const;
00326     YBlockPtr declaration () const;
00327     SymbolEntryPtr parameter (unsigned int position) const;
00328 
00329     // access to definition block (= 0 if declaration only)
00330     YBlockPtr definition () const;
00331     void setDefinition (YBlockPtr body);
00332     // read definition from stream
00333     void setDefinition (bytecodeistream & str);
00334 
00335     string toStringDeclaration () const;
00336     string toString () const;
00337     std::ostream & toStreamDefinition (std::ostream & str) const;
00338     std::ostream & toStream (std::ostream & str) const;
00339     virtual YCPValue evaluate (bool cse = false);
00340     constTypePtr type() const;
00341 };
00342 
00343 
00344 #endif   // YCode_h

Generated on Fri Jun 16 18:07:45 2006 for yast2-core by  doxygen 1.4.6