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