yast2-core
YCode.h
Go to the documentation of this file.
1 /*-----------------------------------------------------------*- c++ -*-\
2 | |
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
8 | |
9 | core system |
10 | (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12 
13  File: YCode.h
14 
15  Author: Klaus Kaempf <kkaempf@suse.de>
16  Maintainer: Klaus Kaempf <kkaempf@suse.de>
17 
18 /-*/
19 // -*- c++ -*-
20 
21 #ifndef YCode_h
22 #define YCode_h
23 
39 #include <string>
40 using std::string;
41 
42 // MemUsage.h defines/undefines D_MEMUSAGE
43 #include <y2util/MemUsage.h>
44 #include "ycp/YCodePtr.h"
45 
46 #include "ycp/YCPValue.h"
47 #include "ycp/YCPString.h"
48 #include "ycp/Type.h"
49 #include "ycp/YSymbolEntry.h"
50 
58 struct ycodelist {
59  struct ycodelist *next;
60  YCodePtr code;
61  constTypePtr type;
62 };
63 typedef struct ycodelist ycodelist_t;
64 
75 class YCode : public Rep
76 #ifdef D_MEMUSAGE
77  , public MemUsage
78 #endif
79 {
80  REP_BODY(YCode);
81 public:
82  enum ykind {
83  yxError = 0,
84  // [1] Constants (-> YCPValue, except(!) term -> yeLocale)
85  ycVoid, ycBoolean, ycInteger, ycFloat, // constants
87  ycList, // list
88  ycMap, // map
89  ycTerm, // term
90 
92 
93  ycConstant, // -- placeholder --
94  ycLocale, // locale constant (gettext)
95  ycFunction, // a function definition (parameters and body)
96 
97  // [16] Expressions (-> declaration_t + values)
98  yePropagate, // type propagation (value, type)
99  yeUnary, // unary (prefix) operator
100  yeBinary, // binary (infix) operator
101  yeTriple, // <exp> ? <exp> : <exp>
102  yeCompare, // compare
103 
104  // [21] Value expressions (-> values + internal)
105  yeLocale, // locale expression (ngettext)
106  yeList, // list expression
107  yeMap, // map expression
108  yeTerm, // <name> ( ...)
109  yeIs, // is()
110  yeBracket, // <name> [ <expr>, ... ] : <expr>
111 
112  // [27] Block (-> linked list of statements)
113  yeBlock, // block expression
114  yeReturn, // quoted expression, e.g. "``(<exp>)" which really is "{ return <exp>; }"
115 
116  // [29] Symbolref (-> SymbolEntry)
117  yeVariable, // variable ref
118  yeBuiltin, // builtin ref + args
119  yeFunction, // function ref + args
120  yeReference, // reference to a variable (identical to yeVariable but with different semantics)
121  // SuSE Linux v9.2
122  yeFunctionPointer, // function pointer
123 
124  yeExpression, // -- placeholder --
125 
126  // [35] Statements (-> YCode + next)
127  ysTypedef, // typedef
128  ysVariable, // variable defintion (-> YSAssign)
129  ysFunction, // function definition
130  ysAssign, // variable assignment (-> YSAssign)
131  ysBracket, // <name> [ <expr>, ... ] = <expr>
132  ysIf, // if, then, else
133  ysWhile, // while () do ...
134  ysDo, // do ... while ()
135  ysRepeat, // repeat ... until ()
136  ysExpression, // any expression (function call)
137  ysReturn, // return
138  ysBreak, // break
139  ysContinue, // continue
140  ysTextdomain, // textdomain
141  ysInclude, // include
142  ysFilename, // restore filename after include
143  ysImport, // import
144  ysBlock, // a block as statement
145  ysSwitch, // switch (since 10.0)
146  ysStatement, // [54] -- placeholder --
147 
148  // internal
149  yiBreakpoint // [55] -- debugger breakpoint
150  };
151 
152 public:
153 
157  YCode ();
158 
162  virtual ~YCode();
163 
169  virtual ykind kind() const = 0;
170 
176  virtual string toString() const;
177 
184  static string toString(ykind kind);
185 
192  virtual std::ostream & toStream (std::ostream & str) const = 0;
193 
201  virtual std::ostream & toXml (std::ostream & str, int indent ) const = 0;
202 
208  virtual bool isConstant () const;
209 
215  bool isError () const;
216 
222  virtual bool isStatement () const;
223 
229  virtual bool isBlock () const;
230 
236  virtual bool isReferenceable () const;
237 
245  virtual YCPValue evaluate (bool cse = false);
246 
252  virtual constTypePtr type() const;
253 };
254 
255 
261 class YConst : public YCode
262 {
263  REP_BODY(YConst);
265  YCPValue m_value; // constant (not allowed in union :-( )
266 public:
267  YConst (ykind kind, YCPValue value); // Constant
269  ~YConst () {};
270  YCPValue value() const;
271  virtual ykind kind() const;
272  string toString() const;
273  std::ostream & toStream (std::ostream & str) const;
274  std::ostream & toXml (std::ostream & str, int indent ) const;
276  virtual bool isConstant () const { return true; }
277  YCPValue evaluate (bool cse = false);
278  constTypePtr type() const;
279 };
280 
281 // bother, 4.3 requires -std=c++0x
282 // so without a condition in configure.in you can't have code
283 // that works with 4.2 and 4.3 without warnings
284 #ifdef HAVE_CXX0X
285 #include <unordered_map>
286 #else
287 #include <ext/hash_map>
288 #endif
289 
290 #include <string>
291 #include <cstddef>
292 
299 class YELocale;
300 
301 class YLocale : public YCode
302 {
303  REP_BODY(YLocale);
304  const char *m_locale; // the string to be translated
305 
306  struct eqstr
307  {
308  bool operator()(const char* s1, const char* s2) const
309  {
310  return strcmp(s1, s2) == 0;
311  }
312  };
313 
314 public:
315 #ifdef HAVE_CXX0X
316  typedef unordered_map<const char*, bool, hash<const char*>, eqstr> t_uniquedomains;
317 #else
318  typedef __gnu_cxx::hash_map<const char*, bool, __gnu_cxx::hash<const char*>, eqstr> t_uniquedomains;
319 #endif
320 
321  static t_uniquedomains domains; // keep every textdomain only once
322  static t_uniquedomains::const_iterator setDomainStatus (const string& domain, bool status);
323  static void ensureBindDomain (const string& domain);
324  static void bindDomainDir (const string& domain, const string& domain_path);
325  static bool findDomain(const string& domain);
326  YLocale (const char *locale, const char *textdomain);
328  ~YLocale ();
329  virtual ykind kind () const { return ycLocale; }
330  const char *value () const;
331  const char *domain () const;
332  string toString() const;
333  std::ostream & toStream (std::ostream & str) const;
334  std::ostream & toXml (std::ostream & str, int indent ) const;
335  YCPValue evaluate (bool cse = false);
336  constTypePtr type() const { return Type::Locale; }
337 
338 private:
339 
340  t_uniquedomains::const_iterator m_domain;
341 
342 };
343 
350 class YFunction : public YCode
351 {
353  // array of formal arguments of a function
354  // the formal parameters must be available in the local scope during parse
355  // of the function body (startDefinition()) and removed afterwards (endDefintion()).
356  // Keep track of the table entries in this block.
357  //
358  // When calling a function during execution, the actual
359  // arguments (values) are bound to the formal arguments
360  // (this array) so the function body can be evaluated.
361  // @see YEFunction::attachActualParameter()
362  //
363  // if NULL, it's a (void) function
364  YBlockPtr m_declaration;
365 
366  // the function definition ('body') is the block defining this function
367  YCodePtr m_definition;
368 
370 
371 public:
372  YFunction (YBlockPtr parameterblock, const SymbolEntryPtr entry = 0);
374  ~YFunction ();
375  virtual ykind kind () const { return ycFunction; }
376 
377  // access to formal parameters
378  unsigned int parameterCount () const;
379  YBlockPtr declaration () const;
380  SymbolEntryPtr parameter (unsigned int position) const;
381 
382  // access to definition block (= 0 if declaration only)
383  YCodePtr definition () const;
384  void setDefinition (YBlockPtr body);
385  void setDefinition (YBreakpointPtr body);
386  // read definition from stream
388 
389  string toStringDeclaration () const;
390  string toString () const;
391  std::ostream & toStreamDefinition (std::ostream & str ) const;
392  std::ostream & toXmlDefinition (std::ostream & str, int indent ) const;
393  std::ostream & toStream (std::ostream & str ) const;
394  std::ostream & toXml (std::ostream & str, int indent ) const;
395  virtual YCPValue evaluate (bool cse = false);
396  constTypePtr type() const;
397 };
398 
399 
400 #endif // YCode_h

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