yast2-core
YBlock.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | |
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
8 | |
9 | core system |
10 | (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12 
13  File: YBlock.h
14 
15  Author: Klaus Kaempf <kkaempf@suse.de>
16  Stanislav Visnovsky <visnov@suse.cz>
17  Maintainer: Stanislav Visnovsky <visnov@suse.cz>
18 
19 /-*/
20 // -*- c++ -*-
21 
22 #ifndef YBlock_h
23 #define YBlock_h
24 
25 #include <string>
26 #include <list>
27 using std::string;
28 #include <y2util/Ustring.h>
29 
30 #include <y2/Y2Namespace.h>
31 #include "ycp/YStatement.h"
32 
33 //-------------------------------------------------------------------
34 
43 class YSImport;
44 
49 class YBlock : public YCode, public Y2Namespace
50 {
51  REP_BODY (YBlock);
52 
53 public:
54  // block kinds
55  typedef enum {
56  b_unknown = 0, // 0: unspecified
57  b_module, // 1: toplevel module (-> m_table != 0)
58  b_file, // 2: toplevel file block
59  b_statement, // 3: used as statement (local block which might contain return)
60  b_definition, // 4: used as function definition
61  b_value, // 5: used as value (intermediate block)
62  b_namespace, // 6: block defines a namespace
63  b_using // 7: block is evaluated in different namespace
64  } blockkind_t;
65 
66 private:
67  // --------------------------------
68  // general block data
69 
70  // Block kind
71  // b_statement == implict return YCPNull() (block is statement, default)
72  // else YCPVoid () (treat block as expression)
74 
75  // Pointer to name of block.
76  // normaly empty, non-empty for b_module and b_namespace
77  string m_name;
78 
79  // --------------------------------
80  // Environment
81  // keep track of symbols entered into SymbolTable (declared
82  // in this block), we must remove then at finishBlock()
83  // so they go out of scope
84 
85  struct yTElist {
86  struct yTElist *next;
88  unsigned int position;
89  };
90  typedef struct yTElist yTElist_t;
91 
92  // block environment (linked list of local declarations)
93  // as TableEntry used during parse time
95 
96  // pointer to last declaration for easier append
97  // points to 0 after detachEnvironment()
99 
100  // --------------------------------
101  // source file, needs environment
102 
103  // Point (Filename) of source file (global SymbolEntry:c_filename)
104  // always points to the current file. During include, it points to
105  // a chain <include file> -> <toplevel file>. See Point.h
106  const Point *m_point;
107 
108  // --------------------------------
109  // Block content
110 
111  struct stmtlist {
112  YStatementPtr stmt;
113  struct stmtlist *next;
114  };
115  typedef struct stmtlist stmtlist_t;
116 
117  // linked list of statements
119 
120  // pointer to last statement for easier append
122 
126  typedef std::list<std::string> stringlist_t;
128 
129  constTypePtr m_type;
130 
131  bool m_running;
132 
133 public:
134  //---------------------------------------------------------------
135  // Constructor / Destructor
136 
137  // toplevel block
138  YBlock (const std::string & filename, blockkind_t kind = b_unknown);
139  // midlevel block
140  YBlock (const Point *point);
142  ~YBlock ();
143 
144  //---------------------------------------------------------------
145  // YCode
146 
148  virtual bool isBlock () const { return true; }
149  virtual ykind kind () const { return yeBlock; }
150 
151  // warning: it is return type in fact!
152  constTypePtr type () const { return m_type; }
153 
154  // set the return type of this block
155  void setType (constTypePtr type);
156 
157  // the whole block is parsed, do final changes
158  void finishBlock ();
159 
160  // evaluate the complete block
161  virtual YCPValue evaluate (bool cse = false);
162 
163  // evaluate the block from the given statement (switch)
164  YCPValue evaluateFrom (int statement_index);
165 
166  // evaluate a single statement
167  // this is a special purpose interface for macro player
168  // does not handle break, return
169  // and also skips initial 'import' statements (e.g. autogenerated
170  // import "UI") by default
171  YCPValue evaluate (int statement_index, bool skip_initial_imports = true);
172 
173  //---------------------------------------------------------------
174  // member access
175 
176  // return name of source file
177  virtual const std::string filename () const;
178 
179  // SymbolTable for global module environment (m_kind == b_module)
180  // non-const return since we must be able to find() which tracks references
181  virtual SymbolTable *table () const;
182 
183  virtual Y2Function* createFunctionCall (const string name, constFunctionTypePtr type);
184 
185  // returns the current parse file as Point
186  const Point *point () const;
187 
188  // returns the name of the block
189  const string name () const;
190  void setName (const string & name);
191 
192  const Y2Namespace *nameSpace () const { return (const Y2Namespace *)this; }
193  Y2Namespace *nameSpace () { return (Y2Namespace *)this; }
194 
195  //---------------------------------------------------------------
196  // block kind
197 
198  // set block kind
199  void setKind (blockkind_t kind);
200 
201  // get block kind
202  blockkind_t bkind () const;
203 
204  // block is toplevel block of a module
205  bool isModule () const { return (m_kind == b_module); } // toplevel module block
206  bool isFile () const { return (m_kind == b_file); } // toplevel file block
207  bool isStatement () const { return (m_kind == b_statement); } // used as statement (local block)
208  bool isDefinition () const { return (m_kind == b_definition); } // used as function definition
209  bool isValue () const { return (m_kind == b_value); } // used as value (intermediate block)
210  bool isNamespace () const { return (m_kind == b_namespace); } // block defines a namespace
211 
212  //---------------------------------------------------------------
213  // Value / Entry
214 
215  // add new value code to this block
216  // (used for functions which accept either symbolic variables or values, e.g. foreach())
217  // returns position
218  unsigned int newValue (constTypePtr type, YCodePtr code);
219 
220  // add a new table entry to this block
221  // and attach it to m_tenvironment
222  // return NULL if symbol of same name already declared in this block
223  TableEntry *newEntry (const char *name, SymbolEntry::category_t cat, constTypePtr type, unsigned int line);
224 
225  //---------------------------------------------------------------
226  // Namespace
227 
228  // add a new namespace entry to this block
229  // and attach it to m_tenvironment
230  // return NULL if symbol of same name already declared in this block
231  TableEntry *newNamespace (const string & name, Y2Namespace *name_space, int line);
232 
233  //---------------------------------------------------------------
234  // symbol handling
235 
236  // Attach entry (variable, typedef, ...) to local environment
237  void attachEntry (TableEntry *entry);
238 
239  // Detach local environment from symbol table
241 
242  //---------------------------------------------------------------
243  // statement handling
244 
245  // Attach statement to end of block
246  void attachStatement (YStatementPtr statement);
247 
248  // Pretach statement to beginning block
249  void pretachStatement (YStatementPtr statement);
250 
251  // count the statements in this block
252  int statementCount () const;
253 
254  //---------------------------------------------------------------
255  // return
256 
257  // returns the return statement if the block just consists of a single return
258  YSReturnPtr justReturn () const;
259 
260  //---------------------------------------------------------------
261  // include
262 
263  // end of include block, 'pop' head of m_point chain
264  void endInclude ();
265 
269  bool isIncluded (string includename) const;
270  void addIncluded (string includename);
271 
272  //---------------------------------------------------------------
273  // string output
274 
275  string toString () const;
276  string environmentToString () const;
277  string toStringSwitch (map<YCPValue, int, ycp_less> cases, int defaultcase) const;
278 
279  //---------------------------------------------------------------
280  // stream output
281 
282  // write block to stream
283  std::ostream & toStream (std::ostream & str) const;
284  std::ostream & toXml( std::ostream & str, int indent ) const;
285  std::ostream & toXmlSwitch( map<YCPValue, int, ycp_less> cases, int defaultcase, std::ostream & str, int indent ) const;
286 
287 };
288 
289 
290 #endif // YBlock_h
struct stmtlist * next
Definition: YBlock.h:113
blockkind_t bkind() const
Definition: YBlock.cc:360
YCode for precompiled ycp code.
Definition: YCode.h:75
stringlist_t * m_includes
Definition: YBlock.h:127
bool m_running
Definition: YBlock.h:131
Definition: YBlock.h:85
Definition: YBlock.h:58
#define str
Definition: scanner.cc:997
unsigned int position
Definition: YBlock.h:88
unsigned int newValue(constTypePtr type, YCodePtr code)
Definition: YBlock.cc:141
void pretachStatement(YStatementPtr statement)
Definition: YBlock.cc:262
bool isNamespace() const
Definition: YBlock.h:210
yTElist_t * m_last_tparm
Definition: YBlock.h:98
std::ostream & toStream(std::ostream &str) const
Definition: YBlock.cc:982
Definition: YBlock.h:49
Definition: YBlock.h:111
void addIncluded(string includename)
Definition: YBlock.cc:1159
blockkind_t
Definition: YBlock.h:55
ykind
Definition: YCode.h:90
Definition: SymbolTable.h:100
virtual SymbolTable * table() const
get our whole symbol table?
Definition: YBlock.cc:157
string toString() const
unparse. useful for debugging
Definition: YBlock.cc:445
YSReturnPtr justReturn() const
Definition: YBlock.cc:367
Definition: Point.h:59
category_t
Definition: SymbolEntry.h:54
constTypePtr m_type
Definition: YBlock.h:129
void endInclude()
Definition: YBlock.cc:404
Y2Namespace * nameSpace()
Definition: YBlock.h:193
string environmentToString() const
Definition: YBlock.cc:423
~YBlock()
Definition: YBlock.cc:98
Definition: YBlock.h:62
void detachEnvironment(SymbolTable *table)
Definition: YBlock.cc:300
stmtlist_t * m_statements
Definition: YBlock.h:118
void finishBlock()
void setKind(blockkind_t kind)
Definition: YBlock.cc:349
void attachStatement(YStatementPtr statement)
Definition: YBlock.cc:234
constTypePtr type() const
Definition: YBlock.h:152
struct yTElist * next
Definition: YBlock.h:86
std::list< std::string > stringlist_t
Definition: YBlock.h:126
bool isValue() const
Definition: YBlock.h:209
REP_BODY(YBlock)
Definition: YBlock.h:63
bool isModule() const
Definition: YBlock.h:205
Definition: YBlock.h:57
virtual ykind kind() const
Definition: YBlock.h:149
stmtlist_t * m_last_statement
Definition: YBlock.h:121
Definition: YBlock.h:56
virtual const std::string filename() const
used for error reporting
Definition: YBlock.cc:131
const Point * point() const
Definition: YBlock.cc:397
const string name() const
what namespace do we implement
Definition: YBlock.cc:382
bool isDefinition() const
Definition: YBlock.h:208
virtual Y2Function * createFunctionCall(const string name, constFunctionTypePtr type)
Definition: YBlock.cc:1174
const Y2Namespace * nameSpace() const
Definition: YBlock.h:192
Definition: YBlock.h:61
const Point * m_point
Definition: YBlock.h:106
Definition: YBlock.h:60
blockkind_t m_kind
Definition: YBlock.h:73
YCPValue evaluateFrom(int statement_index)
Definition: YBlock.cc:698
void setName(const string &name)
Definition: YBlock.cc:389
yTElist_t * m_tenvironment
Definition: YBlock.h:94
YBlock(const std::string &filename, blockkind_t kind=b_unknown)
Definition: YBlock.cc:57
Definition: YCode.h:121
YStatementPtr stmt
Definition: YBlock.h:112
Wrapper for YCPValueRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPValueRep with the arrow operator. See YCPValueRep.
Definition: YCPValue.h:275
TableEntry * newNamespace(const string &name, Y2Namespace *name_space, int line)
Definition: YBlock.cc:279
TableEntry * newEntry(const char *name, SymbolEntry::category_t cat, constTypePtr type, unsigned int line)
Definition: YBlock.cc:170
int statementCount() const
Definition: YBlock.cc:827
Definition: Y2Namespace.h:43
std::ostream & toXmlSwitch(map< YCPValue, int, ycp_less > cases, int defaultcase, std::ostream &str, int indent) const
Definition: YBlock.cc:524
string toStringSwitch(map< YCPValue, int, ycp_less > cases, int defaultcase) const
Definition: YBlock.cc:480
bool isFile() const
Definition: YBlock.h:206
bool isIncluded(string includename) const
Definition: YBlock.cc:1149
void attachEntry(TableEntry *entry)
Definition: YBlock.cc:205
An istream that remembers some data about the bytecode.
Definition: Bytecode.h:42
string m_name
Definition: YBlock.h:77
Definition: YBlock.h:59
virtual YCPValue evaluate(bool cse=false)
called when evaluating the import statement
Definition: YBlock.cc:575
TableEntry * tentry
Definition: YBlock.h:87
Definition: SymbolTable.h:42
std::ostream & toXml(std::ostream &str, int indent) const
Definition: YBlock.cc:1062
void setType(constTypePtr type)
Definition: YBlock.cc:1227
bool isStatement() const
Definition: YBlock.h:207
Definition: Y2Function.h:71
virtual bool isBlock() const
Definition: YBlock.h:148
Definition: YStatement.h:465

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