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

Generated on Tue Nov 6 01:20:21 2007 for yast2-core by  doxygen 1.5.0