Merge pull request #29 from BotondBaranyi/master
[deliverable/titan.core.git] / compiler2 / Value.hh
1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Baji, Laszlo
10 * Balasko, Jeno
11 * Baranyi, Botond
12 * Beres, Szabolcs
13 * Bibo, Zoltan
14 * Cserveni, Akos
15 * Delic, Adam
16 * Forstner, Matyas
17 * Gecse, Roland
18 * Kovacs, Ferenc
19 * Raduly, Csaba
20 * Szabados, Kristof
21 * Szabo, Janos Zoltan – initial implementation
22 * Tatarka, Gabor
23 * Zalanyi, Balazs Andor
24 *
25 ******************************************************************************/
26 #ifndef _Common_Value_HH
27 #define _Common_Value_HH
28
29 #include "Setting.hh"
30 #include "Code.hh"
31 #include "Type.hh"
32 #include "Int.hh"
33 #include "../common/ttcn3float.hh"
34
35 class ustring;
36 class JSON_Tokenizer;
37
38 namespace Asn {
39 class Block;
40 }
41
42 namespace Ttcn {
43 class Reference;
44 class Ref_base;
45 class TemplateInstance;
46 class TemplateInstances;
47 class ActualParList;
48 class ActualPar;
49 class ParsedActualParameters;
50 class LogArguments;
51 class JsonOmitCombination;
52 }
53
54 namespace Common {
55
56 // not defined here
57 class OID_comp;
58 class Values;
59 class NamedValue;
60 class NamedValues;
61 class CharSyms;
62 class Assignment;
63 class Scope;
64
65 using Asn::Block;
66 using Ttcn::TemplateInstance;
67 using Ttcn::LogArguments;
68
69 /**
70 * \ingroup AST
71 *
72 * \defgroup AST_Value Value
73 *
74 * These classes provide a unified interface for values.
75 *
76 * @{
77 */
78
79 /**
80 * Represents a value in the AST.
81 */
82 class Value : public GovernedSimple {
83 public:
84 /** value type */
85
86
87 enum valuetype_t {
88 V_ERROR, /**< erroneous */
89 V_NULL, /**< NULL (for ASN.1 NULL type, also in TTCN-3) */
90 V_BOOL, /**< boolean */
91 V_NAMEDINT, /**< integer / named number */
92 V_NAMEDBITS, /**< named bits (identifiers) */
93 V_INT, /**< integer */
94 V_REAL, /**< real/float */
95 V_ENUM, /**< enumerated */
96 V_BSTR, /**< bitstring */
97 V_HSTR, /**< hexstring */
98 V_OSTR, /**< octetstring */
99 V_CSTR, /**< charstring */
100 V_USTR, /**< universal charstring */
101 V_ISO2022STR, /**< ISO-2022 string (treat as octetstring) */
102 V_CHARSYMS, /**< parsed ASN.1 universal string notation */
103 V_OID, /**< object identifier */
104 V_ROID, /**< relative object identifier */
105 V_CHOICE, /**< choice; set directly by the ASN.1 parser */
106 V_SEQOF, /**< sequence (record) of */
107 V_SETOF, /**< set of */
108 V_ARRAY, /**< array */
109 V_SEQ, /**< sequence (record) */
110 V_SET, /**< set */
111 V_OPENTYPE, /**< open type */
112 V_REFD, /**< referenced */
113 V_UNDEF_LOWERID, /**< undefined loweridentifier */
114 V_UNDEF_BLOCK, /**< undefined {block} */
115 V_OMIT, /**< special value for optional values */
116 V_VERDICT, /**< verdict */
117 V_TTCN3_NULL, /**< TTCN-3 null (for component or default references) */
118 V_DEFAULT_NULL, /**< null default reference */
119 V_FAT_NULL, /**< null for function, altstep and testcase */
120 V_EXPR, /**< expressions */
121 V_MACRO, /**< macros (%%something) */
122 V_NOTUSED, /**< not used symbol ('-') */
123 V_FUNCTION, /**< function */
124 V_ALTSTEP, /**< altstep */
125 V_TESTCASE, /**< testcase */
126 V_INVOKE, /**< invoke operation */
127 V_REFER /**< refer(function) */
128 };
129
130 enum verdict_t {
131 Verdict_NONE,
132 Verdict_PASS,
133 Verdict_INCONC,
134 Verdict_FAIL,
135 Verdict_ERROR
136 };
137
138 /** Operation type, for V_EXPR */
139 enum operationtype_t {
140 OPTYPE_RND, // -
141 OPTYPE_TESTCASENAME, // -
142
143 OPTYPE_UNARYPLUS, // v1
144 OPTYPE_UNARYMINUS, // v1
145 OPTYPE_NOT, // v1
146 OPTYPE_NOT4B, // v1
147
148 OPTYPE_BIT2HEX, // v1 6
149 OPTYPE_BIT2INT, // v1
150 OPTYPE_BIT2OCT, // v1
151 OPTYPE_BIT2STR, // v1
152 OPTYPE_CHAR2INT, // v1 10
153 OPTYPE_CHAR2OCT, // v1
154 OPTYPE_FLOAT2INT, // v1
155 OPTYPE_FLOAT2STR, // v1
156 OPTYPE_HEX2BIT, // v1
157 OPTYPE_HEX2INT, // v1
158 OPTYPE_HEX2OCT, // v1
159 OPTYPE_HEX2STR, // v1
160 OPTYPE_INT2CHAR, // v1
161 OPTYPE_INT2FLOAT, // v1
162 OPTYPE_INT2STR, // v1 20
163 OPTYPE_INT2UNICHAR, // v1
164 OPTYPE_OCT2BIT, // v1
165 OPTYPE_OCT2CHAR, // v1
166 OPTYPE_OCT2HEX, // v1
167 OPTYPE_OCT2INT, // v1
168 OPTYPE_OCT2STR, // v1
169 OPTYPE_OCT2UNICHAR, // v1 [v2]
170 OPTYPE_STR2BIT, // v1
171 OPTYPE_STR2FLOAT, // v1
172 OPTYPE_STR2HEX, // v1
173 OPTYPE_STR2INT, // v1 30
174 OPTYPE_STR2OCT, // v1
175 OPTYPE_UNICHAR2INT, // v1
176 OPTYPE_UNICHAR2CHAR, // v1
177 OPTYPE_UNICHAR2OCT, // v1 [v2]
178 OPTYPE_ENUM2INT, // v1
179
180 OPTYPE_ENCODE, // ti1 35
181
182 OPTYPE_RNDWITHVAL, /** \todo -> SEED */ // v1
183
184 OPTYPE_ADD, // v1 v2
185 OPTYPE_SUBTRACT, // v1 v2
186 OPTYPE_MULTIPLY, // v1 v2
187 OPTYPE_DIVIDE, // v1 v2 40
188 OPTYPE_MOD, // v1 v2
189 OPTYPE_REM, // v1 v2
190 OPTYPE_CONCAT, // v1 v2
191 OPTYPE_EQ, // v1 v2
192 OPTYPE_LT, // v1 v2
193 OPTYPE_GT, // v1 v2
194 OPTYPE_NE, // v1 v2
195 OPTYPE_GE, // v1 v2
196 OPTYPE_LE, // v1 v2
197 OPTYPE_AND, // v1 v2 50
198 OPTYPE_OR, // v1 v2
199 OPTYPE_XOR, // v1 v2
200 OPTYPE_AND4B, // v1 v2
201 OPTYPE_OR4B, // v1 v2
202 OPTYPE_XOR4B, // v1 v2
203 OPTYPE_SHL, // v1 v2
204 OPTYPE_SHR, // v1 v2
205 OPTYPE_ROTL, // v1 v2
206 OPTYPE_ROTR, // v1 v2
207
208 OPTYPE_INT2BIT, // v1 v2 60
209 OPTYPE_INT2HEX, // v1 v2
210 OPTYPE_INT2OCT, // v1 v2
211
212 OPTYPE_DECODE, // r1 r2
213
214 OPTYPE_SUBSTR, // ti1 v2 v3
215 OPTYPE_REGEXP, // ti1 t2 v3
216 OPTYPE_DECOMP, // v1 v2 v3 66
217
218 OPTYPE_REPLACE, // ti1 v2 v3 ti4
219
220 OPTYPE_ISVALUE, // ti1 68
221 OPTYPE_ISBOUND, // ti1
222 OPTYPE_ISPRESENT, // ti1
223 OPTYPE_ISCHOSEN, // r1 i2
224 OPTYPE_ISCHOSEN_V, // v1 i2
225 OPTYPE_ISCHOSEN_T, // t1 i2
226
227 OPTYPE_LENGTHOF, // ti1
228 OPTYPE_SIZEOF, // ti1
229 OPTYPE_VALUEOF, // ti1
230 OPTYPE_MATCH, // v1 t2
231
232 OPTYPE_TTCN2STRING, // ti1
233 OPTYPE_REMOVE_BOM, //v1
234 OPTYPE_GET_STRINGENCODING, //v1
235 OPTYPE_ENCODE_BASE64, //v1 [v2]
236 OPTYPE_DECODE_BASE64, //v1
237
238 /** cannot distinguish during parsing; can be COMP or TMR */
239 OPTYPE_UNDEF_RUNNING, // r1 78
240 OPTYPE_COMP_NULL, // - (from V_TTCN3_NULL)
241 OPTYPE_COMP_MTC, // -
242 OPTYPE_COMP_SYSTEM, // -
243 OPTYPE_COMP_SELF, // -
244 OPTYPE_COMP_CREATE, // r1 [v2] [v3] b4
245 OPTYPE_COMP_RUNNING, // v1
246 OPTYPE_COMP_RUNNING_ANY, // -
247 OPTYPE_COMP_RUNNING_ALL, // -
248 OPTYPE_COMP_ALIVE, // v1
249 OPTYPE_COMP_ALIVE_ANY, // -
250 OPTYPE_COMP_ALIVE_ALL, // -
251 OPTYPE_TMR_READ, // r1 90
252 OPTYPE_TMR_RUNNING, // r1
253 OPTYPE_TMR_RUNNING_ANY, // -
254 OPTYPE_GETVERDICT, // -
255 OPTYPE_ACTIVATE, // r1
256 OPTYPE_ACTIVATE_REFD, //v1 t_list2
257 OPTYPE_EXECUTE, // r1 [v2]
258 OPTYPE_EXECUTE_REFD, // v1 t_list2 [v3]
259
260 OPTYPE_LOG2STR, // logagrs
261 OPTYPE_PROF_RUNNING, // - 99
262
263 NUMBER_OF_OPTYPES // must be last
264 };
265
266 enum macrotype_t {
267 MACRO_MODULEID, /**< module identifier (%moduleId) */
268 MACRO_FILENAME, /**< name of input file (%fileName) */
269 MACRO_BFILENAME, /**< name of input file (__BFILE__) */
270 MACRO_FILEPATH, /**< canonical file name (name with full path) */
271 MACRO_LINENUMBER, /**< line number (%lineNumber) */
272 MACRO_LINENUMBER_C, /**< line number in C style (__LINE__)
273 gives an integer value */
274 MACRO_DEFINITIONID, /**< name of (top level) definition (%definitionId) */
275 /* the following macro may be needed in future versions of titan,
276 * functionality will be explained by a quick study written by Kristof
277 MACRO_NOTSCOPE_NAME_BUT_THE_NAME_OF_SOMETHING_ELSE */
278 MACRO_SCOPE,
279 MACRO_TESTCASEID /**< name of current testcase (%testcaseId)
280 (evaluated at runtime) */
281 };
282
283 enum expr_state_t {EXPR_NOT_CHECKED, EXPR_CHECKING,
284 EXPR_CHECKING_ERR, EXPR_CHECKED};
285 private:
286
287 valuetype_t valuetype;
288 Type *my_governor;
289 union {
290 bool val_bool;
291 int_val_t *val_Int;
292 Identifier *val_id;
293 ttcn3float val_Real;
294 struct {
295 string *val_str;
296 map<size_t, Value> *str_elements;
297 } str;
298 struct {
299 ustring *val_ustr;
300 map<size_t, Value> *ustr_elements;
301 bool convert_str; /**< Indicates that universal charstring is */
302 /* initialized with charstring */
303 } ustr;
304 CharSyms *char_syms;
305 vector<OID_comp> *oid_comps;
306 struct {
307 Identifier *alt_name; /**< The name of the selected alternative */
308 Value *alt_value; /**< The value given */
309 } choice;
310 Values *val_vs;
311 NamedValues *val_nvs;
312 struct {
313 Reference *ref;
314 Value *refd_last; /**< cache */
315 } ref;
316 Reference *refered;
317 struct {
318 operationtype_t v_optype;
319 expr_state_t state;
320 union {
321 Value *v1;
322 Template *t1;
323 TemplateInstance *ti1;
324 Ttcn::Ref_base *r1; /**< timer or component */
325 LogArguments *logargs; /**< arguments of log2str() */
326 };
327 union {
328 Value *v2;
329 TemplateInstance *t2;
330 Identifier *i2;
331 Ttcn::ParsedActualParameters *t_list2;
332 Ttcn::ActualParList *ap_list2;
333 Ttcn::Ref_base *r2;
334 };
335 Value *v3;
336 union {
337 Value *v4;
338 TemplateInstance *ti4;
339 bool b4;
340 };
341 } expr;
342 macrotype_t macro;
343 struct {
344 Value *v;
345 Ttcn::ParsedActualParameters *t_list;
346 Ttcn::ActualParList *ap_list;
347 } invoke;
348 map<string, Identifier> *ids;
349 Block *block;
350 verdict_t verdict;
351 Common::Assignment *refd_fat; /** function, altstep, testcase */
352 } u;
353
354 /** Used to avoid infinite recursions of is_unfoldable() */
355 class UnfoldabilityCheck {
356 static map<Value*, void> running;
357 Value* value;
358 public:
359 static bool is_running(Value* p_value) { return running.has_key(p_value); }
360 UnfoldabilityCheck(Value* p_value) : value(p_value) { running.add(value, 0); }
361 ~UnfoldabilityCheck() { running.erase(value); }
362 };
363
364 /** Copy constructor for Value::clone() only. */
365 Value(const Value& p);
366 /** Assignment op not implemented. */
367 Value& operator=(const Value& p);
368 /** Release resources. */
369 void clean_up();
370 /** Release resources if Value::valuetype == V_EXPR.
371 * Called by Value::clean_up(). */
372 void clean_up_expr();
373 /** Copies (moves) the contents of \a src (valuetype & u) into \a this and
374 * after that destroys (deletes) src. Before doing the copy the contents of
375 * \a this are destroyed by calling \a clean_up(). */
376 void copy_and_destroy(Value *src);
377 public:
378 Value(valuetype_t p_vt);
379 Value(valuetype_t p_vt, bool p_val_bool);
380 Value(valuetype_t p_vt, const Int& p_val_Int);
381 Value(valuetype_t p_vt, int_val_t *p_val_Int);
382 Value(valuetype_t p_vt, string *p_val_str);
383 Value(valuetype_t p_vt, ustring *p_val_ustr);
384 Value(valuetype_t p_vt, CharSyms *p_char_syms);
385 Value(valuetype_t p_vt, Identifier *p_val_id);
386 Value(valuetype_t p_vt, Identifier *p_id, Value *p_val);
387 Value(valuetype_t p_vt, const Real& p_val_Real);
388 Value(valuetype_t p_vt, Values *p_vs);
389 Value(valuetype_t p_vt, Value *p_v, Ttcn::ParsedActualParameters *p_t_list);
390 /** Constructor used by V_EXPR "-": RND, TESTCASENAME, COMP_NULL, COMP_MTC,
391 * COMP_SYSTEM, COMP_SELF, COMP_RUNNING_ANY, COMP_RUNNING_ALL,
392 * COMP_ALIVE_ALL, COMP_ALIVE_ANY, TMR_RUNNING_ANY, GETVERDICT,
393 * PROF_RUNNING */
394 Value(operationtype_t p_optype);
395 /** Constructor used by V_EXPR "v1" */
396 Value(operationtype_t p_optype, Value *p_v1);
397 /** Constructor used by V_EXPR "ti1": LENGTHOF, SIZEOF, VALUEOF, TTCN2STRING */
398 Value(operationtype_t p_optype, TemplateInstance *p_ti1);
399 /** Constructor used by V_EXPR "r1": COMP_RUNNING, COMP_ALIVE,
400 * TMR_READ, TMR_RUNNING, ACTIVATE */
401 Value(operationtype_t p_optype, Ttcn::Ref_base *p_r1);
402 /** ACTIVATE_REFD, OPTYPE_INVOKE, OPTYPE_COMP_ALIVE_REFD */
403 Value(operationtype_t p_optype, Value *p_v1,
404 Ttcn::ParsedActualParameters *p_ap_list);
405 /** OPTYPE_EXECUTE_REFD */
406 Value(operationtype_t p_optype, Value *p_v1,
407 Ttcn::ParsedActualParameters *p_t_list2, Value *p_v3);
408 /** Constructor used by V_EXPR "r1 [v2]": EXECUTE */
409 Value(operationtype_t p_optype, Ttcn::Ref_base *p_r1, Value *v2);
410 /** Constructor used by V_EXPR "r1 [v2] [v3] b4": COMP_CREATE */
411 Value(operationtype_t p_optype, Ttcn::Ref_base *p_r1, Value *p_v2,
412 Value *p_v3, bool p_b4);
413 /** Constructor used by V_EXPR "v1 v2" */
414 Value(operationtype_t p_optype, Value *p_v1, Value *p_v2);
415 /** Constructor used by V_EXPR "v1 v2 v3" */
416 Value(operationtype_t p_optype, Value *p_v1, Value *p_v2, Value *p_v3);
417 /** Constructor used by V_EXPR "ti1 v2 v3" */
418 Value(operationtype_t p_optype, TemplateInstance *p_ti1, Value *p_v2, Value *p_v3);
419 /** Constructor used by V_EXPR "ti1 t2 v3" */
420 Value(operationtype_t p_optype, TemplateInstance *p_ti1, TemplateInstance *p_t2, Value *p_v3);
421 /** Constructor used by V_EXPR "ti1 v2 v3 ti4" */
422 Value(operationtype_t p_optype, TemplateInstance *p_ti1, Value *p_v2,
423 Value *p_v3, TemplateInstance *p_ti4);
424 /** Constructor used by V_EXPR "v1 t2": MATCH */
425 Value(operationtype_t p_optype, Value *p_v1, TemplateInstance *p_t2);
426 /** Constructor used by V_EXPR "r1 i2": ISPRESENT, ISCHOSEN */
427 Value(operationtype_t p_optype, Ttcn::Reference *p_r1, Identifier *p_i2);
428 Value(operationtype_t p_optype, LogArguments *p_logargs);
429 /** Constructor used by V_MACRO */
430 Value(valuetype_t p_vt, macrotype_t p_macrotype);
431 Value(valuetype_t p_vt, NamedValues *p_nvs);
432 /** V_REFD, V_REFER */
433 Value(valuetype_t p_vt, Reference *p_ref);
434 Value(valuetype_t p_vt, Block *p_block);
435 Value(valuetype_t p_vt, verdict_t p_verdict);
436 /** Constructor used by decode */
437 Value(operationtype_t p_optype, Ttcn::Ref_base *p_r1, Ttcn::Ref_base *p_r2);
438 virtual ~Value();
439 virtual Value* clone() const;
440 valuetype_t get_valuetype() const {return valuetype;}
441 /** it it is not V_EXPR then fatal error. */
442 operationtype_t get_optype() const;
443 /** Sets the governor type. */
444 virtual void set_my_governor(Type *p_gov);
445 /** Gets the governor type. */
446 virtual Type *get_my_governor() const;
447 /** Returns true if it is a value reference. */
448 bool is_ref() const { return valuetype == V_REFD; }
449 virtual void set_fullname(const string& p_fullname);
450 virtual void set_my_scope(Scope *p_scope);
451 private:
452 void set_fullname_expr(const string& p_fullname);
453 void set_my_scope_expr(Scope *p_scope);
454 public:
455 void set_genname_recursive(const string& p_genname);
456 void set_genname_prefix(const char *p_genname_prefix);
457 void set_code_section(code_section_t p_code_section);
458 void change_sign();
459 void add_oid_comp(OID_comp* p_comp);
460 void set_valuetype(valuetype_t p_valuetype);
461 void set_valuetype_COMP_NULL();
462 void set_valuetype(valuetype_t p_valuetype, const Int& p_val_int);
463 void set_valuetype(valuetype_t p_valuetype, string *p_str);
464 void set_valuetype(valuetype_t p_valuetype, Identifier *p_id);
465 void set_valuetype(valuetype_t p_valuetype, Assignment *p_ass);
466 void add_id(Identifier *p_id);
467 /** Follows the chain of references if any and returns the last
468 * element of the chain. In other words, the returned value is
469 * the first value which is not a reference. */
470 Value* get_value_refd_last
471 (ReferenceChain *refch=0,
472 Type::expected_value_t exp_val=Type::EXPECTED_DYNAMIC_VALUE);
473 Value *get_refd_sub_value(Ttcn::FieldOrArrayRefs *subrefs,
474 size_t start_i, bool usedInIsbound,
475 ReferenceChain *refch);
476 /** Returns true if the value is unknown at compile-time. */
477 bool is_unfoldable
478 (ReferenceChain *refch=0,
479 Type::expected_value_t exp_val=Type::EXPECTED_DYNAMIC_VALUE);
480
481 /** Returns whether the value is a single identifier, which can be either
482 * a reference or an enum value. */
483 bool is_undef_lowerid();
484 /** Returns the single identifier.
485 * It can be used only if \a is_undef_lowerid() returned true. */
486 const Identifier& get_undef_lowerid();
487 /** Converts the single identifier to a referenced value. */
488 void set_lowerid_to_ref();
489 /** Check if ref++; can be used instead of ref = ref + 1; */
490 bool can_use_increment(Reference *ref) const;
491
492 /** If this value is used in an expression, what is its return type? */
493 Type::typetype_t get_expr_returntype
494 (Type::expected_value_t exp_val=Type::EXPECTED_DYNAMIC_VALUE);
495 /** If this value is used in an expression, what is its governor?
496 * If has no governor, but is reference, then tries the
497 * referenced stuff... If not known, returns NULL. */
498 Type* get_expr_governor(Type::expected_value_t exp_val);
499 Type* get_expr_governor_v1v2(Type::expected_value_t exp_val);
500 Type* get_expr_governor_last();
501 /** get the type invoked */
502 Type *get_invoked_type(Type::expected_value_t exp_val);
503 private:
504 const char* get_opname() const;
505 /** Used to determine whether the reference points to value or
506 * template in ISCHOSEN, then convert to {ISCHOSEN}_{V,T} */
507 void chk_expr_ref_ischosen();
508 void chk_expr_operandtype_enum(const char *opname, Value *v,
509 Type::expected_value_t exp_val);
510 void chk_expr_operandtype_bool(Type::typetype_t tt, const char *opnum,
511 const char *opname, const Location *loc);
512 void chk_expr_operandtype_int(Type::typetype_t tt, const char *opnum,
513 const char *opname, const Location *loc);
514 void chk_expr_operandtype_float(Type::typetype_t tt, const char *opnum,
515 const char *opname, const Location *loc);
516 void chk_expr_operandtype_int_float(Type::typetype_t tt, const char *opnum,
517 const char *opname, const Location *loc);
518 void chk_expr_operandtype_int_float_enum(Type::typetype_t tt,
519 const char *opnum,
520 const char *opname,
521 const Location *loc);
522 /** Check that the operand is a list or string or something like it.
523 *
524 * This Value is usually an expression like
525 * @code lengthof(foo) @endcode
526 *
527 * @param t the type (governor) of the operand
528 * @param opnum describes the operand ("first", "left", ...)
529 * @param opname the name of the operation
530 * @param loc the operand itself as a Location object
531 * @param allow_array \a true if T_ARRAY is acceptable (rotate, lengthof)
532 * or \a false if it's not (concat, substr, replace).
533 */
534 void chk_expr_operandtype_list(Type* t, const char *opnum,
535 const char *opname, const Location *loc,
536 bool allow_array);
537 // Check that the operand of the operation is a string value
538 void chk_expr_operandtype_str(Type::typetype_t tt, const char *opnum,
539 const char *opname, const Location *loc);
540 void chk_expr_operandtype_charstr(Type::typetype_t tt, const char *opnum,
541 const char *opname, const Location *loc);
542 void chk_expr_operandtype_cstr(Type::typetype_t tt, const char *opnum,
543 const char *opname, const Location *loc);
544 void chk_expr_operandtype_binstr(Type::typetype_t tt, const char *opnum,
545 const char *opname, const Location *loc);
546 void chk_expr_operandtype_bstr(Type::typetype_t tt, const char *opnum,
547 const char *opname, const Location *loc);
548 void chk_expr_operandtype_hstr(Type::typetype_t tt, const char *opnum,
549 const char *opname, const Location *loc);
550 void chk_expr_operandtype_ostr(Type::typetype_t tt, const char *opnum,
551 const char *opname, const Location *loc);
552 void chk_expr_operandtypes_same(Type::typetype_t tt1, Type::typetype_t tt2,
553 const char *opname);
554 void chk_expr_operandtypes_same_with_opnum(Type::typetype_t tt1, Type::typetype_t tt2,
555 const char *opnum1, const char *opnum2,
556 const char *opname);
557 void chk_expr_operandtypes_compat(Type::expected_value_t exp_val,
558 Value *v1, Value *v2,
559 const char *opnum1 = "left",
560 const char *opnum2 = "right");
561 void chk_expr_operand_undef_running(Type::expected_value_t exp_val,
562 Ttcn::Ref_base *ref,
563 const char *opnum, const char *opname);
564 /** Returns the referred component type if it is correct. */
565 Type *chk_expr_operand_comptyperef_create();
566 /** Checks whether the special component references mtc, system and self
567 * have the correct component types (compatible with \a my_governor). */
568 void chk_expr_comptype_compat();
569 void chk_expr_operand_compref(Value *val, const char *opnum,
570 const char *opname);
571 void chk_expr_operand_tmrref(Ttcn::Ref_base *ref,
572 const char *opnum, const char *opname);
573 void chk_expr_operand_activate(Ttcn::Ref_base *ref,
574 const char *opnum, const char *opname);
575 void chk_expr_operand_activate_refd(Value *val,
576 Ttcn::TemplateInstances* t_list2,
577 Ttcn::ActualParList *&parlist,
578 const char *opnum, const char *opname);
579 void chk_expr_operand_execute(Ttcn::Ref_base *ref, Value *val,
580 const char *opnum, const char *opname);
581 void chk_expr_operand_execute_refd(Value *v1,
582 Ttcn::TemplateInstances* t_list2,
583 Ttcn::ActualParList *&parlist,
584 Value *v3, const char *opnum,
585 const char *opname);
586 void chk_invoke(Type::expected_value_t exp_val);
587 void chk_expr_eval_value(Value *val, Type &t,
588 ReferenceChain *refch,
589 Type::expected_value_t exp_val);
590 void chk_expr_eval_ti(TemplateInstance *ti, Type *t,
591 ReferenceChain *refch, Type::expected_value_t exp_val);
592 void chk_expr_val_int_pos0(Value *val, const char *opnum,
593 const char *opname);
594 void chk_expr_val_int_pos7bit(Value *val, const char *opnum,
595 const char *opname);
596 void chk_expr_val_int_pos31bit(Value *val, const char *opnum,
597 const char *opname);
598 void chk_expr_val_int_float_not0(Value *val, const char *opnum,
599 const char *opname);
600 void chk_expr_val_large_int(Value *val, const char *opnum,
601 const char *opname);
602 void chk_expr_val_len1(Value *val, const char *opnum,
603 const char *opname);
604 void chk_expr_val_str_len_even(Value *val, const char *opnum,
605 const char *opname);
606 void chk_expr_val_str_bindigits(Value *val, const char *opnum,
607 const char *opname);
608 void chk_expr_val_str_hexdigits(Value *val, const char *opnum,
609 const char *opname);
610 void chk_expr_val_str_7bitoctets(Value *val, const char *opnum,
611 const char *opname);
612 void chk_expr_val_str_int(Value *val, const char *opnum,
613 const char *opname);
614 void chk_expr_val_str_float(Value *val, const char *opnum,
615 const char *opname);
616 void chk_expr_val_ustr_7bitchars(Value *val, const char *opnum,
617 const char *opname);
618 void chk_expr_val_bitstr_intsize(Value *val, const char *opnum,
619 const char *opname);
620 void chk_expr_val_hexstr_intsize(Value *val, const char *opnum,
621 const char *opname);
622 void chk_expr_operands_int2binstr();
623 void chk_expr_operands_str_samelen();
624 void chk_expr_operands_replace();
625 void chk_expr_operands_substr();
626 void chk_expr_operands_regexp();
627 void chk_expr_operands_ischosen(ReferenceChain *refch,
628 Type::expected_value_t exp_val);
629 void chk_expr_operand_encode(ReferenceChain *refch,
630 Type::expected_value_t exp_val);
631 void chk_expr_operands_decode();
632 /** Checks whether \a this can be compared with omit value (i.e. \a this
633 * should be a referenced value pointing to a optional record/set field. */
634 void chk_expr_omit_comparison(Type::expected_value_t exp_val);
635 Int chk_eval_expr_sizeof(ReferenceChain *refch,
636 Type::expected_value_t exp_val);
637 /** The governor is returned. */
638 Type *chk_expr_operands_ti(TemplateInstance* ti, Type::expected_value_t exp_val);
639 void chk_expr_operands_match(Type::expected_value_t exp_val);
640 void chk_expr_dynamic_part(Type::expected_value_t exp_val,
641 bool allow_controlpart,
642 bool allow_runs_on = true,
643 bool require_runs_on = false);
644 /** Check the operands of an expression */
645 void chk_expr_operands(ReferenceChain *refch,
646 Type::expected_value_t exp_val);
647 void chk_expr_operand_valid_float(Value* v, const char *opnum, const char *opname);
648 /** Evaluate...
649 * Called by Value::get_value_refd_last() for V_EXPR */
650 void evaluate_value(ReferenceChain *refch, Type::expected_value_t exp_val);
651 /** Evaluate macro.
652 * Called by Value::get_value_refd_last() for V_MACRO */
653 void evaluate_macro(Type::expected_value_t exp_val);
654 /** Compile-time evaluation of isvalue()
655 *
656 * @param from_sequence \c true if called for a member of a sequence/set
657 * @return the compile-time result of calling isvalue()
658 */
659 bool evaluate_isvalue(bool from_sequence);
660
661 Value *get_refd_field_value(const Identifier& field_id, bool usedInIsbound,
662 const Location& loc);
663 Value *get_refd_array_value(Value *array_index, bool usedInIsbound,
664 ReferenceChain *refch);
665 Value *get_string_element(const Int& index, const Location& loc);
666
667 public:
668 /** Checks whether the value (expression) evaluates to built-in type
669 * \a p_tt. Argument \a type_name is the name of that type, it is used in
670 * error messages. */
671 void chk_expr_type(Type::typetype_t p_tt, const char *type_name,
672 Type::expected_value_t exp_val);
673 /** Checks that the value (expression) evals to a boolean value */
674 inline void chk_expr_bool(Type::expected_value_t exp_val)
675 { chk_expr_type(Type::T_BOOL, "boolean", exp_val); }
676 /** Checks that the value (expression) evals to an integer value */
677 inline void chk_expr_int(Type::expected_value_t exp_val)
678 { chk_expr_type(Type::T_INT, "integer", exp_val); }
679 /** Checks that the value (expression) evals to a float value */
680 inline void chk_expr_float(Type::expected_value_t exp_val)
681 { chk_expr_type(Type::T_REAL, "float", exp_val); }
682 /** Checks that the value (expression) evals to a verdict value */
683 inline void chk_expr_verdict(Type::expected_value_t exp_val)
684 { chk_expr_type(Type::T_VERDICT, "verdict", exp_val); }
685 /** Checks that the value (expression) evals to a default value */
686 inline void chk_expr_default(Type::expected_value_t exp_val)
687 { chk_expr_type(Type::T_DEFAULT, "default", exp_val); }
688
689 /* if "infinity" or "-infinity" was parsed then this is a real value or
690 a unary - expression containing a real value, where the real value is
691 infinity.
692 Returns -1 if -infinity was parsed, +1 if infinity, 0 in other cases */
693 int is_parsed_infinity();
694
695 bool get_val_bool();
696 int_val_t* get_val_Int();
697 const Identifier* get_val_id();
698 const ttcn3float& get_val_Real();
699 string get_val_str();
700 ustring get_val_ustr();
701 string get_val_iso2022str();
702 size_t get_val_strlen();
703 verdict_t get_val_verdict();
704 size_t get_nof_comps();
705 bool is_indexed() const;
706 const Identifier& get_alt_name();
707 Value *get_alt_value();
708 /** Sets the first letter in the name of the alternative to lowercase
709 * if it's an uppercase letter.
710 * Used on open types (the name of their alternatives can be given with both
711 * an uppercase or a lowercase first letter, and the generated code will need
712 * to use the lowercase version). */
713 void set_alt_name_to_lowercase();
714 /** Returns whether the embedded object identifier components
715 * contain any error. Applicable to OID/ROID values only. */
716 bool has_oid_error();
717 /** Collects all object identifier components of the value into \a
718 * comps. It follows and expands the embedded references.
719 * Applicable to OID/ROID values and referenced values pointing
720 * to them.
721 * @return true, if all components can be calculated in compile-time */
722 bool get_oid_comps(vector<string>& comps);
723 /** Get a component of SEQUENCE/SET, zero-based */
724 void add_se_comp(NamedValue* nv); // needed by implicit_omit
725 NamedValue* get_se_comp_byIndex(size_t n);
726 /** Get a component of an array/REC-OF/SET-OF */
727 Value *get_comp_byIndex(size_t n);
728 /** Get an index of an array/REC-OF/SET-OF */
729 Value *get_index_byIndex(size_t n);
730 /** Does a named component exist ? For SEQ/SET/CHOICE */
731 bool has_comp_withName(const Identifier& p_name);
732 NamedValue* get_se_comp_byName(const Identifier& p_name);
733 Value* get_comp_value_byName(const Identifier& p_name);
734 bool field_is_present(const Identifier& p_name);
735 bool field_is_chosen(const Identifier& p_name);
736 void chk_dupl_id();
737 size_t get_nof_ids() const;
738 Identifier* get_id_byIndex(size_t p_i);
739 bool has_id(const Identifier& p_id);
740 Reference *get_reference() const;
741 Reference *get_refered() const;
742 Common::Assignment *get_refd_fat() const;
743 /** Usable during AST building. If VariableRef is needed, then use
744 * the return value of this function, then delete this. */
745 Ttcn::Reference* steal_ttcn_ref();
746 Ttcn::Ref_base* steal_ttcn_ref_base();
747 void steal_invoke_data(Value*& p_v, Ttcn::ParsedActualParameters*& p_ti,
748 Ttcn::ActualParList*& p_ap);
749 Common::Assignment* get_refd_assignment();
750
751 void chk();
752 void chk_OID(ReferenceChain& refch);
753 void chk_ROID(ReferenceChain& refch);
754 /** Checks for circular references within embedded values */
755 void chk_recursions(ReferenceChain& refch);
756 /** Checks for circular references within embedded expressions */
757 void chk_recursions_expr(ReferenceChain& refch);
758 void chk_recursions_expr_decode(Ttcn::Ref_base* ref, ReferenceChain& refch);
759 /** Check that the value (a V_EXPR) - being used as the RHS - refers to
760 * the LHS of the assignment.
761 * @return true if self-assignment*/
762 bool chk_expr_self_ref(Common::Assignment *lhs);
763
764 virtual string create_stringRepr();
765 private:
766 static bool chk_expr_self_ref_templ(Ttcn::Template *t, Common::Assignment *lhs);
767 static bool chk_expr_self_ref_val (Common::Value *v, Common::Assignment *lhs);
768 string create_stringRepr_unary(const char *operator_str);
769 string create_stringRepr_infix(const char *operator_str);
770 string create_stringRepr_predef1(const char *function_name);
771 string create_stringRepr_predef2(const char *function_name);
772 public:
773 bool operator==(Value& val);
774 bool operator<(Value& val);
775 public:
776 /** Returns true if this value is of a string type */
777 bool is_string_type(Type::expected_value_t exp_val);
778 /** Public entry points for code generation. */
779 /** Generates the equivalent C++ code for the value. It is used
780 * when the value is part of a complex expression (e.g. as
781 * operand of a built-in operation, actual parameter, array
782 * index). The generated code fragments are appended to the
783 * fields of visitor \a expr. */
784 void generate_code_expr(expression_struct *expr);
785 /** Generates the C++ equivalent of \a this into \a expr and adds a "()"
786 * to expr->expr if \a this is referenced value that points to an optional
787 * field of a record/set value. */
788 void generate_code_expr_mandatory(expression_struct *expr);
789 /** Generates a C++ code sequence, which initializes the C++
790 * object named \a name with the contents of the value. The code
791 * sequence is appended to argument \a str and the resulting
792 * string is returned. */
793 char *generate_code_init(char *str, const char *name);
794 /** Appends the initialization sequence of all referred
795 * non-parameterized templates to \a str and returns the
796 * resulting string. Such templates may appear in the actual
797 * parameter list of parameterized value references
798 * (e.g. function calls) and in operands of valueof or match
799 * operations. */
800 char *rearrange_init_code(char *str, Common::Module* usage_mod);
801 /**
802 * Generates a value for temporary use. Example:
803 *
804 * str: // empty
805 * prefix: if(
806 * blockcount: 0
807 *
808 * if the value is simple, then returns:
809 *
810 * // empty
811 * if(simple
812 *
813 * if the value is complex, then returns:
814 *
815 * // empty
816 * {
817 * booelan tmp_2;
818 * {
819 * preamble... tmp_1...
820 * tmp_2=func5(tmp_1);
821 * postamble
822 * }
823 * if(tmp_2
824 *
825 * and also increments the blockcount because you have to close it...
826 */
827 char* generate_code_tmp(char *str, const char *prefix, size_t& blockcount);
828 char* generate_code_tmp(char *str, char*& init);
829 /** Generates the C++ statement that puts the value of \a this into the
830 * log. It is used when the value appears in the argument of a log()
831 * statement. */
832 void generate_code_log(expression_struct *expr);
833 /** Generates the C++ equivalent of a match operation if it
834 * appears within a log() statement. Applicable only if \a this is
835 * an expression containing a match operation. */
836 void generate_code_log_match(expression_struct *expr);
837
838 private:
839 /** Private helper functions for code generation. */
840 /** Helper function for \a generate_code_expr(). It is used when
841 * the value is an expression. */
842 void generate_code_expr_expr(expression_struct *expr);
843 /** Helper function for \a generate_code_expr_expr(). It handles
844 * unary operators. */
845 static void generate_code_expr_unary(expression_struct *expr,
846 const char *operator_str, Value *v1);
847 /** Helper function for \a generate_code_expr_expr(). It handles infix
848 * binary operators. Flag \a optional_allowed is true when the operation
849 * has different meaning on optional fields (like comparison). Otherwise
850 * the optional operands are explicitly converted to a regular data
851 * type. */
852 void generate_code_expr_infix(expression_struct *expr,
853 const char *operator_str, Value *v1,
854 Value *v2, bool optional_allowed);
855 /** Helper function for \a generate_code_expr_expr(). It handles the
856 * logical "and" and "or" operation with short-circuit evaluation
857 * semantics. */
858 void generate_code_expr_and_or(expression_struct *expr);
859 /** Helper function for \a generate_code_expr_expr(). It handles
860 * built-in functions with one argument. */
861 static void generate_code_expr_predef1(expression_struct *expr,
862 const char *function_name,
863 Value *v1);
864 /** Helper function for \a generate_code_expr_expr(). It handles
865 * built-in functions with two arguments. */
866 static void generate_code_expr_predef2(expression_struct *expr,
867 const char *function_name,
868 Value *v1, Value *v2);
869 /** Helper function for \a generate_code_expr_expr(). It handles
870 * built-in functions with three arguments. */
871 static void generate_code_expr_predef3(expression_struct *expr,
872 const char *function_name,
873 Value *v1, Value *v2, Value *v3);
874 /** Helper functions for \a generate_code_expr_expr(). */
875 void generate_code_expr_substr(expression_struct *expr);
876 void generate_code_expr_substr_replace_compat(expression_struct *expr);
877 void generate_code_expr_regexp(expression_struct *expr);
878 void generate_code_expr_replace(expression_struct *expr);
879 /** Helper function for \a generate_code_expr_expr(). It handles
880 * built-in function rnd(). */
881 static void generate_code_expr_rnd(expression_struct *expr,
882 Value *v1);
883 /** Helper function for \a generate_code_expr_expr(). It handles
884 * create(). */
885 static void generate_code_expr_create(expression_struct *expr,
886 Ttcn::Ref_base *type, Value *name, Value *location, bool alive);
887 /** Helper function for \a generate_code_expr_expr(). It handles
888 * activate(). */
889 void generate_code_expr_activate(expression_struct *expr);
890 /** Helper function for \a generate_code_expr_expr(). It handles
891 * activate() with derefers(). */
892 void generate_code_expr_activate_refd(expression_struct *expr);
893 /** Helper function for \a generate_code_expr_expr(). It handles
894 * execute(). */
895 void generate_code_expr_execute(expression_struct *expr);
896 /** Helper function for \a generate_code_expr_expr(). It handles
897 * execute() with derefers(). */
898 void generate_code_expr_execute_refd(expression_struct *expr);
899 /** Helper function for \a generate_code_expr(). It handles invoke */
900 void generate_code_expr_invoke(expression_struct *expr);
901
902 /** Adds the character sequence "()" to expr->expr if \a ref points to
903 * an optional field of a record/set value. */
904 static void generate_code_expr_optional_field_ref(expression_struct *expr,
905 Reference *ref);
906
907 void generate_code_expr_encode(expression_struct *expr);
908
909 void generate_code_expr_decode(expression_struct *expr);
910
911 /** Helper function for \a generate_code_init(). It handles the
912 * union (CHOICE) values. */
913 char *generate_code_init_choice(char *str, const char *name);
914 /** Helper function for \a generate_code_init(). It handles the
915 * 'record of'/'set of' ('SEQUENCE OF'/'SET OF') values. */
916 char *generate_code_init_seof(char *str, const char *name);
917 /** Helper function for \a generate_code_init(). It handles the
918 * indexed value notation for 'record of'/'set of' ('SEQUENCE OF'/
919 * 'SET OF') values. */
920 char *generate_code_init_indexed(char *str, const char *name);
921 /** Helper function for \a generate_code_init(). It handles the
922 * array values. */
923 char *generate_code_init_array(char *str, const char *name);
924 /** Helper function for \a generate_code_init(). It handles the
925 * record/set (SEQUENCE/SET) values. */
926 char *generate_code_init_se(char *str, const char *name);
927 /** Helper function for \a generate_code_init(). It handles the
928 * referenced values. */
929 char *generate_code_init_refd(char *str, const char *name);
930
931 public:
932 /** Generates JSON code from this value. Used in JSON schema generation.
933 * No code is generated for special float values NaN, INF and -INF if the
934 * 2nd parameter is false. */
935 void generate_json_value(JSON_Tokenizer& json,
936 bool allow_special_float = true, bool union_value_list = false,
937 Ttcn::JsonOmitCombination* omit_combo = NULL);
938
939 /** Returns whether C++ explicit cast (type conversion) is necessary when
940 * \a this is the argument of a send() or log() statement. True is returned
941 * when the type of the C++ equivalent is ambiguous or is a built-in type
942 * that has to be converted to the respective TTCN-3 value class
943 * (e.g. int -> class INTEGER). */
944 bool explicit_cast_needed(bool forIsValue = false);
945 /** Returns whether the value can be represented by an in-line C++
946 * expression. */
947 bool has_single_expr();
948 /** Returns the equivalent C++ expression. It can be used only if
949 * \a has_single_expr() returns true. */
950 string get_single_expr();
951 private:
952 /** Helper function for has_single_expr(). Used when the value contains
953 * an expression */
954 bool has_single_expr_expr();
955 /** Helper function for has_single_expr(). Used for invoke operations and
956 * for activate and execute combined with derefer */
957 static bool has_single_expr_invoke(Value *v, Ttcn::ActualParList *ap_list);
958 /** Helper function for \a get_single_expr(). Used for enumerated
959 * values only. It considers the enum-hack option. */
960 string get_single_expr_enum();
961 /** Helper function for \a get_single_expr(). Used for ISO2022
962 * string values only. The generated code refers to the
963 * user-defined conversion functions of the RTE. */
964 string get_single_expr_iso2022str();
965 /** Helper function for \a get_single_expr(). Used by literal function,
966 * altstep and testcase values denoted by refer(). */
967 string get_single_expr_fat();
968 /** Returns whether the value is compound (i.e. record, set,
969 * union, record of, set of). */
970 bool is_compound();
971 /** Returns whether the C++ initialization sequence requires a
972 * temporary variable reference to be introduced for efficiency
973 * reasons. */
974 bool needs_temp_ref();
975 /** Returns whether the evaluation of \a this has side-effects that shall
976 * be eliminated in case of short-circuit evaluation of logical "and" and
977 * "or" operations. This function is applied on the second (right) operand
978 * of the expression. */
979 bool needs_short_circuit();
980 public:
981 virtual void dump(unsigned level) const;
982 private:
983 inline void set_val_str(string *p_val_str)
984 { u.str.val_str = p_val_str; u.str.str_elements = 0; }
985 inline void set_val_ustr(ustring *p_val_ustr)
986 { u.ustr.val_ustr = p_val_ustr; u.ustr.ustr_elements = 0; }
987 void add_string_element(size_t index, Value *v_element,
988 map<size_t, Value>*& string_elements);
989 };
990
991 /** @} end of AST_Value group */
992
993 class LazyParamData {
994 static int depth; // recursive code generation: calling a func. with lazy param inside a lazy param
995 static bool used_as_lvalue;
996 static vector<string>* type_vec;
997 static vector<string>* refd_vec;
998 public:
999 static bool in_lazy();
1000 static void init(bool p_used_as_lvalue);
1001 static void clean();
1002 static string add_ref_genname(Assignment* ass, Scope* scope);
1003 static string get_member_name(size_t idx);
1004 static string get_constr_param_name(size_t idx);
1005 static void generate_code_for_value(expression_struct* expr, Value* val, Scope* my_scope);
1006 static void generate_code_for_template(expression_struct* expr, TemplateInstance* temp, template_restriction_t gen_restriction_check, Scope* my_scope);
1007 static void generate_code(expression_struct *expr, Value* value, Scope* scope);
1008 static void generate_code(expression_struct *expr, TemplateInstance* temp, template_restriction_t gen_restriction_check, Scope* scope);
1009 static void generate_code_lazyparam_class(expression_struct *expr, expression_struct& param_expr, const string& lazy_param_id, const string& type_name);
1010 static void generate_code_ap_default_ref(expression_struct *expr, Ttcn::Ref_base* ref, Scope* scope);
1011 static void generate_code_ap_default_value(expression_struct *expr, Value* value, Scope* scope);
1012 static void generate_code_ap_default_ti(expression_struct *expr, TemplateInstance* ti, Scope* scope);
1013 };
1014
1015 } // namespace Common
1016
1017 #endif // _Common_Value_HH
This page took 0.055708 seconds and 5 git commands to generate.