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