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 protected:
81  // Comments must be mutable, because it can be lazy moved from container
82  // element to its subelement like in YSExpression
83  mutable const char * comment_before;
84  mutable const char * comment_after;
85 
86 private:
87  REP_BODY(YCode);
88 
89 public:
90  enum ykind {
91  yxError = 0,
92  // [1] Constants (-> YCPValue, except(!) term -> yeLocale)
93  ycVoid, ycBoolean, ycInteger, ycFloat, // constants
95  ycList, // list
96  ycMap, // map
97  ycTerm, // term
98 
100 
101  ycConstant, // -- placeholder --
102  ycLocale, // locale constant (gettext)
103  ycFunction, // a function definition (parameters and body)
104 
105  // [16] Expressions (-> declaration_t + values)
106  yePropagate, // type propagation (value, type)
107  yeUnary, // unary (prefix) operator
108  yeBinary, // binary (infix) operator
109  yeTriple, // <exp> ? <exp> : <exp>
110  yeCompare, // compare
111 
112  // [21] Value expressions (-> values + internal)
113  yeLocale, // locale expression (ngettext)
114  yeList, // list expression
115  yeMap, // map expression
116  yeTerm, // <name> ( ...)
117  yeIs, // is()
118  yeBracket, // <name> [ <expr>, ... ] : <expr>
119 
120  // [27] Block (-> linked list of statements)
121  yeBlock, // block expression
122  yeReturn, // quoted expression, e.g. "``(<exp>)" which really is "{ return <exp>; }"
123 
124  // [29] Symbolref (-> SymbolEntry)
125  yeVariable, // variable ref
126  yeBuiltin, // builtin ref + args
127  yeFunction, // function ref + args
128  yeReference, // reference to a variable (identical to yeVariable but with different semantics)
129  // SuSE Linux v9.2
130  yeFunctionPointer, // function pointer
131 
132  yeExpression, // -- placeholder --
133 
134  // [35] Statements (-> YCode + next)
135  ysTypedef, // typedef
136  ysVariable, // variable defintion (-> YSAssign)
137  ysFunction, // function definition
138  ysAssign, // variable assignment (-> YSAssign)
139  ysBracket, // <name> [ <expr>, ... ] = <expr>
140  ysIf, // if, then, else
141  ysWhile, // while () do ...
142  ysDo, // do ... while ()
143  ysRepeat, // repeat ... until ()
144  ysExpression, // any expression (function call)
145  ysReturn, // return
146  ysBreak, // break
147  ysContinue, // continue
148  ysTextdomain, // textdomain
149  ysInclude, // include
150  ysFilename, // restore filename after include
151  ysImport, // import
152  ysBlock, // a block as statement
153  ysSwitch, // switch (since 10.0)
154  ysStatement, // [54] -- placeholder --
155 
156  // internal
157  yiBreakpoint // [55] -- debugger breakpoint
158  };
159 
160 public:
161 
165  YCode ();
166 
170  virtual ~YCode();
171 
177  virtual ykind kind() const = 0;
178 
184  virtual string toString() const;
185 
192  static string toString(ykind kind);
193 
200  virtual std::ostream & toStream (std::ostream & str) const = 0;
201 
209  virtual std::ostream & toXml (std::ostream & str, int indent ) const = 0;
210 
211 
216  std::string commentToXml () const;
220  std::ostream & commentToXml (std::ostream & str ) const;
221 
227  virtual bool isConstant () const;
228 
234  bool isError () const;
235 
241  virtual bool isStatement () const;
242 
248  virtual bool isBlock () const;
249 
255  virtual bool isReferenceable () const;
256 
264  virtual YCPValue evaluate (bool cse = false);
265 
271  virtual constTypePtr type() const;
272 
277  void setCommentBefore( const char * comment);
278 
283  void setCommentAfter( const char * comment);
284 
285 };
286 
287 
293 class YConst : public YCode
294 {
295  REP_BODY(YConst);
297  YCPValue m_value; // constant (not allowed in union :-( )
298 public:
299  YConst (ykind kind, YCPValue value); // Constant
301  ~YConst () {};
302  YCPValue value() const;
303  virtual ykind kind() const;
304  string toString() const;
305  std::ostream & toStream (std::ostream & str) const;
306  std::ostream & toXml (std::ostream & str, int indent ) const;
308  virtual bool isConstant () const { return true; }
309  YCPValue evaluate (bool cse = false);
310  constTypePtr type() const;
311 };
312 
313 // bother, 4.3 requires -std=c++0x
314 // so without a condition in configure.in you can't have code
315 // that works with 4.2 and 4.3 without warnings
316 #ifdef HAVE_CXX0X
317 #include <unordered_map>
318 #else
319 #include <ext/hash_map>
320 #endif
321 
322 #include <string>
323 #include <cstddef>
324 
331 class YELocale;
332 
333 class YLocale : public YCode
334 {
335  REP_BODY(YLocale);
336  const char *m_locale; // the string to be translated
337 
338  struct eqstr
339  {
340  bool operator()(const char* s1, const char* s2) const
341  {
342  return strcmp(s1, s2) == 0;
343  }
344  };
345 
346 public:
347 #ifdef HAVE_CXX0X
348  typedef unordered_map<const char*, bool, hash<const char*>, eqstr> t_uniquedomains;
349 #else
350  typedef __gnu_cxx::hash_map<const char*, bool, __gnu_cxx::hash<const char*>, eqstr> t_uniquedomains;
351 #endif
352 
353  static t_uniquedomains domains; // keep every textdomain only once
354  static t_uniquedomains::const_iterator setDomainStatus (const string& domain, bool status);
355  static void ensureBindDomain (const string& domain);
356  static void bindDomainDir (const string& domain, const string& domain_path);
357  static bool findDomain(const string& domain);
358  YLocale (const char *locale, const char *textdomain);
360  ~YLocale ();
361  virtual ykind kind () const { return ycLocale; }
362  const char *value () const;
363  const char *domain () const;
364  string toString() const;
365  std::ostream & toStream (std::ostream & str) const;
366  std::ostream & toXml (std::ostream & str, int indent ) const;
367  YCPValue evaluate (bool cse = false);
368  constTypePtr type() const { return Type::Locale; }
369 
370 private:
371 
372  t_uniquedomains::const_iterator m_domain;
373 
374 };
375 
382 class YFunction : public YCode
383 {
385  // array of formal arguments of a function
386  // the formal parameters must be available in the local scope during parse
387  // of the function body (startDefinition()) and removed afterwards (endDefintion()).
388  // Keep track of the table entries in this block.
389  //
390  // When calling a function during execution, the actual
391  // arguments (values) are bound to the formal arguments
392  // (this array) so the function body can be evaluated.
393  // @see YEFunction::attachActualParameter()
394  //
395  // if NULL, it's a (void) function
396  YBlockPtr m_declaration;
397 
398  // the function definition ('body') is the block defining this function
399  YCodePtr m_definition;
400 
402 
403 public:
404  YFunction (YBlockPtr parameterblock, const SymbolEntryPtr entry = 0);
406  ~YFunction ();
407  virtual ykind kind () const { return ycFunction; }
408 
409  // access to formal parameters
410  unsigned int parameterCount () const;
411  YBlockPtr declaration () const;
412  SymbolEntryPtr parameter (unsigned int position) const;
413 
414  // access to definition block (= 0 if declaration only)
415  YCodePtr definition () const;
416  void setDefinition (YBlockPtr body);
417  void setDefinition (YBreakpointPtr body);
418  // read definition from stream
420 
421  string toStringDeclaration () const;
422  string toString () const;
423  std::ostream & toStreamDefinition (std::ostream & str ) const;
424  std::ostream & toXmlDefinition (std::ostream & str, int indent ) const;
425  std::ostream & toStream (std::ostream & str ) const;
426  std::ostream & toXml (std::ostream & str, int indent ) const;
427  virtual YCPValue evaluate (bool cse = false);
428  constTypePtr type() const;
429 };
430 
431 
432 #endif // YCode_h
Definition: YCode.h:94
string toString() const
Definition: YCode.cc:764
std::ostream & toStream(std::ostream &str) const
Definition: YCode.cc:557
virtual YCPValue evaluate(bool cse=false)
Definition: YCode.cc:236
REP_BODY(YFunction)
Definition: YCode.h:106
YCode for precompiled ycp code.
Definition: YCode.h:75
Definition: YCode.h:110
Definition: YCode.h:338
Definition: YCode.h:135
Definition: YCode.h:151
~YFunction()
Definition: YCode.cc:652
void setCommentAfter(const char *comment)
Definition: YCode.cc:276
YCode()
Definition: YCode.cc:59
Definition: YCode.h:126
string toString() const
Definition: YCode.cc:401
#define str
Definition: scanner.cc:997
const char * comment_before
Definition: YCode.h:83
Definition: YExpression.h:158
~YConst()
Definition: YCode.h:301
bool m_is_global
Definition: YCode.h:401
std::ostream & toStreamDefinition(std::ostream &str) const
Definition: YCode.cc:819
static const constTypePtr Locale
Definition: Type.h:127
unsigned int parameterCount() const
Definition: YCode.cc:731
static bool findDomain(const string &domain)
Definition: YCode.cc:610
REP_BODY(YConst)
Definition: YCode.h:333
virtual ykind kind() const
Definition: YCode.h:407
Definition: YCode.h:142
Definition: YCode.h:94
std::ostream & toXml(std::ostream &str, int indent) const
Definition: YCode.cc:458
t_uniquedomains::const_iterator m_domain
Definition: YCode.h:372
Definition: YCode.h:94
ykind m_kind
Definition: YCode.h:296
Definition: YCode.h:97
ykind
Definition: YCode.h:90
YConst(ykind kind, YCPValue value)
Definition: YCode.cc:286
Definition: YCode.h:95
Definition: YCode.h:91
Definition: YCode.h:118
REP_BODY(YLocale)
const char * value() const
Definition: YCode.cc:543
Definition: YCode.h:127
SymbolEntryPtr parameter(unsigned int position) const
Definition: YCode.cc:738
Definition: YCode.h:136
Definition: YCode.h:99
Definition: YCode.h:140
#define comment
Definition: scanner.cc:998
REP_BODY(YCode)
static t_uniquedomains domains
Definition: YCode.h:353
Definition: YCode.h:58
constTypePtr type() const
Definition: YCode.h:368
struct ycodelist * next
Definition: YCode.h:59
Definition: YCode.h:139
YFunction(YBlockPtr parameterblock, const SymbolEntryPtr entry=0)
Definition: YCode.cc:643
bool operator()(const char *s1, const char *s2) const
Definition: YCode.h:340
virtual bool isReferenceable() const
Definition: YCode.cc:103
std::ostream & toXmlDefinition(std::ostream &str, int indent) const
Definition: YCode.cc:851
Definition: YCode.h:103
constTypePtr type() const
Definition: YCode.cc:914
Definition: YCode.h:93
Definition: YCode.h:154
virtual std::ostream & toStream(std::ostream &str) const =0
Definition: YCode.cc:196
const char * comment_after
Definition: YCode.h:84
YCPValue evaluate(bool cse=false)
Definition: YCode.cc:582
Definition: YCode.h:130
Definition: YCode.h:149
Definition: YCode.h:107
Definition: YCode.h:145
YCPValue value() const
Definition: YCode.cc:394
Definition: YCode.h:150
std::ostream & toStream(std::ostream &str) const
Definition: YCode.cc:442
std::ostream & toStream(std::ostream &str) const
Definition: YCode.cc:869
Definition: YCode.h:147
Definition: YCode.h:116
virtual ykind kind() const =0
YCodePtr code
Definition: YCode.h:60
Definition: YCode.h:153
Definition: YCode.h:113
Definition: YCode.h:125
Definition: YCode.h:117
Definition: YCode.h:122
virtual string toString() const
Definition: YCode.cc:188
Definition: YCode.h:157
string toStringDeclaration() const
Definition: YCode.cc:745
Definition: YCode.h:101
std::ostream & toXml(std::ostream &str, int indent) const
Definition: YCode.cc:566
Definition: YCode.h:143
const char * m_locale
Definition: YCode.h:336
virtual constTypePtr type() const
Definition: YCode.cc:250
Definition: YCode.h:138
YCPValue m_value
Definition: YCode.h:297
Definition: YCode.h:146
Definition: YCode.h:137
static void bindDomainDir(const string &domain, const string &domain_path)
Definition: YCode.cc:628
constTypePtr type() const
Definition: YCode.cc:473
Definition: YCode.h:93
Definition: YCode.h:96
YBlockPtr declaration() const
Definition: YCode.cc:665
virtual ~YCode()
Definition: YCode.cc:65
virtual ykind kind() const
Definition: YCode.cc:388
static t_uniquedomains::const_iterator setDomainStatus(const string &domain, bool status)
Definition: YCode.cc:596
Definition: YCode.h:108
YCodePtr m_definition
Definition: YCode.h:399
Definition: YCode.h:148
virtual bool isConstant() const
Definition: YCode.h:308
constTypePtr type
Definition: YCode.h:61
Definition: YCode.h:128
YBlockPtr m_declaration
Definition: YCode.h:396
string toString() const
Definition: YCode.cc:575
virtual bool isBlock() const
Definition: YCode.cc:96
Definition: YCode.h:115
Definition: YCode.h:144
virtual std::ostream & toXml(std::ostream &str, int indent) const =0
Definition: YCode.cc:225
Definition: YCode.h:121
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
YCodePtr definition() const
Definition: YCode.cc:658
Definition: YCode.h:382
YCPValue evaluate(bool cse=false)
Definition: YCode.cc:431
Definition: YCode.h:93
Definition: YCode.h:132
__gnu_cxx::hash_map< const char *, bool, __gnu_cxx::hash< const char * >, eqstr > t_uniquedomains
Definition: YCode.h:350
Base class for reference counted objects.
Definition: Rep.h:46
Definition: YCode.h:93
YCP Constant.
Definition: YCode.h:293
An istream that remembers some data about the bytecode.
Definition: Bytecode.h:42
Definition: YCode.h:152
const char * domain() const
Definition: YCode.cc:550
virtual bool isStatement() const
Definition: YCode.cc:89
std::ostream & toXml(std::ostream &str, int indent) const
Definition: YCode.cc:889
Definition: YCode.h:114
virtual bool isConstant() const
Definition: YCode.cc:75
Definition: YCode.h:102
void setCommentBefore(const char *comment)
Definition: YCode.cc:256
virtual YCPValue evaluate(bool cse=false)
Definition: YCode.cc:778
void setDefinition(YBlockPtr body)
Definition: YCode.cc:672
Definition: YCode.h:141
bool isError() const
Definition: YCode.cc:82
Definition: MemUsage.h:37
static void ensureBindDomain(const string &domain)
Definition: YCode.cc:617
Definition: YCode.h:94
std::string commentToXml() const
Definition: YCode.cc:207
YLocale(const char *locale, const char *textdomain)
Definition: YCode.cc:502
~YLocale()
Definition: YCode.cc:536
Definition: YCode.h:109
virtual ykind kind() const
Definition: YCode.h:361

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