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 #include <unordered_map>
314 #include <string>
315 #include <cstddef>
316 
323 class YELocale;
324 
325 class YLocale : public YCode
326 {
327  REP_BODY(YLocale);
328  const char *m_locale; // the string to be translated
329 
330  struct eqstr
331  {
332  bool operator()(const char* s1, const char* s2) const
333  {
334  return strcmp(s1, s2) == 0;
335  }
336  };
337 
338 public:
339  typedef unordered_map<const char*, bool, hash<const char*>, eqstr> t_uniquedomains;
340 
341  static t_uniquedomains domains; // keep every textdomain only once
342  static t_uniquedomains::const_iterator setDomainStatus (const string& domain, bool status);
343  static void ensureBindDomain (const string& domain);
344  static void bindDomainDir (const string& domain, const string& domain_path);
345  static bool findDomain(const string& domain);
346  YLocale (const char *locale, const char *textdomain);
348  ~YLocale ();
349  virtual ykind kind () const { return ycLocale; }
350  const char *value () const;
351  const char *domain () const;
352  string toString() const;
353  std::ostream & toStream (std::ostream & str) const;
354  std::ostream & toXml (std::ostream & str, int indent ) const;
355  YCPValue evaluate (bool cse = false);
356  constTypePtr type() const { return Type::Locale; }
357 
358 private:
359 
360  t_uniquedomains::const_iterator m_domain;
361 
362 };
363 
370 class YFunction : public YCode
371 {
373  // array of formal arguments of a function
374  // the formal parameters must be available in the local scope during parse
375  // of the function body (startDefinition()) and removed afterwards (endDefintion()).
376  // Keep track of the table entries in this block.
377  //
378  // When calling a function during execution, the actual
379  // arguments (values) are bound to the formal arguments
380  // (this array) so the function body can be evaluated.
381  // @see YEFunction::attachActualParameter()
382  //
383  // if NULL, it's a (void) function
384  YBlockPtr m_declaration;
385 
386  // the function definition ('body') is the block defining this function
387  YCodePtr m_definition;
388 
390 
391 public:
392  YFunction (YBlockPtr parameterblock, const SymbolEntryPtr entry = 0);
394  ~YFunction ();
395  virtual ykind kind () const { return ycFunction; }
396 
397  // access to formal parameters
398  unsigned int parameterCount () const;
399  YBlockPtr declaration () const;
400  SymbolEntryPtr parameter (unsigned int position) const;
401 
402  // access to definition block (= 0 if declaration only)
403  YCodePtr definition () const;
404  void setDefinition (YBlockPtr body);
405  void setDefinition (YBreakpointPtr body);
406  // read definition from stream
408 
409  string toStringDeclaration () const;
410  string toString () const;
411  std::ostream & toStreamDefinition (std::ostream & str ) const;
412  std::ostream & toXmlDefinition (std::ostream & str, int indent ) const;
413  std::ostream & toStream (std::ostream & str ) const;
414  std::ostream & toXml (std::ostream & str, int indent ) const;
415  virtual YCPValue evaluate (bool cse = false);
416  constTypePtr type() const;
417 };
418 
419 
420 #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:330
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:389
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:325
virtual ykind kind() const
Definition: YCode.h:395
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:360
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:341
Definition: YCode.h:58
constTypePtr type() const
Definition: YCode.h:356
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:332
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
unordered_map< const char *, bool, hash< const char * >, eqstr > t_uniquedomains
Definition: YCode.h:339
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:328
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:387
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:384
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:370
YCPValue evaluate(bool cse=false)
Definition: YCode.cc:431
Definition: YCode.h:93
Definition: YCode.h:132
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:349

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