Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Examples

YBlock.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:        YBlock.h
00014 
00015    Author:      Klaus Kaempf <kkaempf@suse.de>
00016    Maintainer:  Klaus Kaempf <kkaempf@suse.de>
00017 
00018 /-*/
00019 // -*- c++ -*-
00020 
00021 #ifndef YBlock_h
00022 #define YBlock_h
00023 
00024 #include <string>
00025 #include <list>
00026 using std::string;
00027 #include <y2util/Ustring.h>
00028 
00029 #include <y2/Y2Namespace.h>
00030 #include "ycp/YStatement.h"
00031 
00032 //-------------------------------------------------------------------
00033 
00042 class YSImport;
00043 
00048 class YBlock : public YCode, public Y2Namespace
00049 {
00050     REP_BODY (YBlock);
00051 
00052 public:
00053     // block kinds
00054     typedef enum {
00055         b_unknown = 0,          // 0: unspecified
00056         b_module,               // 1: toplevel module (-> m_table != 0)
00057         b_file,                 // 2: toplevel file block
00058         b_statement,            // 3: used as statement (local block which might contain return)
00059         b_definition,           // 4: used as function definition
00060         b_value,                // 5: used as value (intermediate block)
00061         b_namespace,            // 6: block defines a namespace
00062         b_using                 // 7: block is evaluated in different namespace
00063     } blockkind_t;
00064 
00065 private:
00066     // --------------------------------
00067     // general block data
00068 
00069     // Block kind
00070     // b_statement == implict return YCPNull() (block is statement, default)
00071     // else YCPVoid () (treat block as expression)
00072     blockkind_t m_kind;
00073 
00074     // Pointer to name of block.
00075     //   normaly empty, non-empty for b_module and b_namespace
00076     string m_name;
00077 
00078     // --------------------------------
00079     // Environment
00080     // keep track of symbols entered into SymbolTable (declared
00081     // in this block), we must remove then at finishBlock()
00082     // so they go out of scope
00083 
00084     struct yTElist {
00085         struct yTElist *next;
00086         TableEntry *tentry;
00087         unsigned int position;
00088     };
00089     typedef struct yTElist yTElist_t;
00090 
00091     // block environment (linked list of local declarations)
00092     //  as TableEntry used during parse time
00093     yTElist_t *m_tenvironment;
00094 
00095     // pointer to last declaration for easier append
00096     // points to 0 after detachEnvironment()
00097     yTElist_t *m_last_tparm;
00098 
00099     // --------------------------------
00100     // source file, needs environment
00101 
00102     // Point (Filename) of source file (global SymbolEntry:c_filename)
00103     // always points to the current file. During include, it points to
00104     // a chain <include file> -> <toplevel file>. See Point.h
00105     const Point *m_point;
00106 
00107     // --------------------------------
00108     // Block content
00109 
00110     struct stmtlist {
00111         YStatementPtr stmt;
00112         struct stmtlist *next;
00113     };
00114     typedef struct stmtlist stmtlist_t;
00115 
00116     // linked list of statements
00117     stmtlist_t *m_statements;
00118 
00119     // pointer to last statement for easier append
00120     stmtlist_t *m_last_statement;
00121 
00125     typedef std::list<std::string> stringlist_t;
00126     stringlist_t* m_includes;
00127     
00128     constTypePtr m_type;
00129     
00130     bool m_running;
00131 
00132 public:
00133     //---------------------------------------------------------------
00134     // Constructor / Destructor
00135    
00136     // toplevel block
00137     YBlock (const std::string & filename, blockkind_t kind = b_unknown);
00138     // midlevel block
00139     YBlock (const Point *point);
00140     YBlock (bytecodeistream & str);
00141     ~YBlock ();
00142 
00143     //---------------------------------------------------------------
00144     // YCode
00145 
00146     // warning: it is return type in fact!
00147     constTypePtr type () const { return m_type; }
00148     
00149     // set the return type of this block
00150     void setType (constTypePtr type);
00151 
00152     // the whole block is parsed, do final changes
00153     void finishBlock ();
00154 
00155     // evaluate the complete block
00156     virtual YCPValue evaluate (bool cse = false);
00157 
00158     // evaluate a single statement
00159     // this is a special purpose interface for macro player
00160     // does not handle break, return
00161     //   and also skips initial 'import' statements (e.g. autogenerated
00162     //    import "UI")  by default
00163     YCPValue evaluate (int statement_index, bool skip_initial_imports = true);
00164     
00165     //---------------------------------------------------------------
00166     // member access
00167 
00168     // return name of source file
00169     virtual const std::string filename () const;
00170 
00171     // SymbolTable for global module environment (m_kind == b_module)
00172     //   non-const return since we must be able to find() which tracks references
00173     virtual SymbolTable *table () const;
00174 
00175     virtual Y2Function* createFunctionCall (const string name, constFunctionTypePtr type);
00176 
00177     // returns the current parse file as Point
00178     const Point *point () const;
00179 
00180     // returns the name of the block
00181     const string name () const;
00182     void setName (const string & name);
00183 
00184     const Y2Namespace *nameSpace () const { return (const Y2Namespace *)this; }
00185     Y2Namespace *nameSpace () { return (Y2Namespace *)this; }
00186 
00187     //---------------------------------------------------------------
00188     // block kind
00189 
00190     // set block kind
00191     void setKind (blockkind_t kind);
00192 
00193     // get block kind
00194     blockkind_t kind () const;
00195 
00196     // block is toplevel block of a module
00197     bool isModule () const      { return (m_kind == b_module); }                // toplevel module block
00198     bool isFile () const        { return (m_kind == b_file); }                  // toplevel file block
00199     bool isStatement () const   { return (m_kind == b_statement); }             // used as statement (local block)
00200     bool isDefinition () const  { return (m_kind == b_definition); }            // used as function definition
00201     bool isValue () const       { return (m_kind == b_value); }                 // used as value (intermediate block)
00202     bool isNamespace () const   { return (m_kind == b_namespace); }             // block defines a namespace
00203 
00204     //---------------------------------------------------------------
00205     // Value / Entry
00206 
00207     // add new value code to this block
00208     //   (used for functions which accept either symbolic variables or values, e.g. foreach())
00209     // returns position
00210     unsigned int newValue (constTypePtr type, YCodePtr code);
00211 
00212     // add a new table entry to this block
00213     //  and attach it to m_tenvironment
00214     //   return NULL if symbol of same name already declared in this block
00215     TableEntry *newEntry (const char *name, SymbolEntry::category_t cat, constTypePtr type, unsigned int line);
00216 
00217     //---------------------------------------------------------------
00218     // Namespace
00219 
00220     // add a new namespace entry to this block
00221     //  and attach it to m_tenvironment
00222     //   return NULL if symbol of same name already declared in this block
00223     TableEntry *newNamespace (const string & name, Y2Namespace *name_space, int line);
00224 
00225     //---------------------------------------------------------------
00226     // symbol handling
00227 
00228     // Attach entry (variable, typedef, ...) to local environment
00229     void attachEntry (TableEntry *entry);
00230 
00231     // Detach local environment from symbol table
00232     void detachEnvironment (SymbolTable *table);
00233 
00234     //---------------------------------------------------------------
00235     // statement handling
00236 
00237     // Attach statement to end of block
00238     void attachStatement (YStatementPtr statement);
00239 
00240     // Pretach statement to beginning block
00241     void pretachStatement (YStatementPtr statement);
00242 
00243     // count the statements in this block
00244     int statementCount () const;
00245 
00246     //---------------------------------------------------------------
00247     // return
00248 
00249     // returns the return statement if the block just consists of a single return
00250     YSReturnPtr justReturn () const;
00251 
00252     //---------------------------------------------------------------
00253     // include
00254 
00255     // end of include block, 'pop' head of m_point chain
00256     void endInclude ();
00257 
00261     bool isIncluded (string includename) const;
00262     void addIncluded (string includename);
00263 
00264     //---------------------------------------------------------------
00265     // string output
00266 
00267     string toString () const;
00268     string environmentToString () const;
00269 
00270     //---------------------------------------------------------------
00271     // stream output
00272 
00273     // write block to stream
00274     std::ostream & toStream (std::ostream & str) const;
00275 
00276 };
00277 
00278 
00279 #endif // YBlock_h

Generated on Tue Mar 22 11:35:48 2005 for yast2-core by  doxygen 1.4.1