Titan Core Initial Contribution
[deliverable/titan.core.git] / core / Param_Types.cc
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 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 #include <string.h>
9
10 #include "../common/memory.h"
11 #include "Types.h"
12 #include "RInt.hh"
13 #include "Error.hh"
14
15 // Needed for logging
16 #include "openssl/bn.h"
17 #include "Integer.hh"
18 #include "Float.hh"
19 #include "Boolean.hh"
20 #include "Objid.hh"
21 #include "Verdicttype.hh"
22 #include "Bitstring.hh"
23 #include "Hexstring.hh"
24 #include "Octetstring.hh"
25 #include "Charstring.hh"
26 #include "Universal_charstring.hh"
27 #include "Logger.hh"
28
29
30 size_t Module_Param_Id::get_index() const {
31 TTCN_error("Internal error: Module_Param_Id::get_index()");
32 return 0;
33 }
34
35 char* Module_Param_Id::get_name() const {
36 TTCN_error("Internal error: Module_Param_Id::get_name()");
37 return NULL;
38 }
39
40 char* Module_Param_Id::get_current_name() const {
41 TTCN_error("Internal error: Module_Param_Id::get_current_name()");
42 return NULL;
43 }
44
45 bool Module_Param_Id::next_name(int offset) {
46 TTCN_error("Internal error: Module_Param_Id::next_name()");
47 return false;
48 }
49
50 char* Module_Param_Name::get_str() const {
51 char* result = NULL;
52 for (size_t i = 0; i < names.size(); ++i) {
53 bool is_index = names[i][0] >= '0' && names[i][0] <= '9';
54 if (i > 0 && !is_index) {
55 result = mputc(result, '.');
56 }
57 if (is_index) {
58 result = mputc(result, '[');
59 }
60 result = mputstr(result, names[i]);
61 if (is_index) {
62 result = mputc(result, ']');
63 }
64 }
65 return result;
66 }
67
68 char* Module_Param_FieldName::get_str() const {
69 return mcopystr(name);
70 }
71
72 char* Module_Param_Index::get_str() const {
73 return mprintf("[%lu]", (unsigned long)index);
74 }
75
76 void Module_Param_Length_Restriction::log() const {
77 TTCN_Logger::log_event(" length(%lu", (unsigned long)min);
78 if (min!=max) {
79 TTCN_Logger::log_event_str("..");
80 if (!has_max) TTCN_Logger::log_event_str("infinity");
81 else TTCN_Logger::log_event("%lu", (unsigned long)max);
82 }
83 TTCN_Logger::log_event_str(")");
84 }
85
86 Module_Param_Id* Module_Param::get_id() const {
87 return id;
88 }
89
90 const char* Module_Param::get_operation_type_str() const {
91 switch (operation_type) {
92 case OT_ASSIGN: return "assignment";
93 case OT_CONCAT: return "concatenation";
94 default: return "<unknown operation>";
95 }
96 }
97
98 const char* Module_Param::get_operation_type_sign_str() const {
99 switch (operation_type) {
100 case OT_ASSIGN: return ":=";
101 case OT_CONCAT: return "&=";
102 default: return "<unknown operation>";
103 }
104 }
105
106 void Module_Param::set_id(Module_Param_Id* p_id) {
107 if (id) TTCN_error("Internal error: Module_Param::set_id()");
108 id = p_id;
109 }
110
111 void Module_Param::set_length_restriction(Module_Param_Length_Restriction* p_length_restriction) {
112 if (length_restriction!=NULL) TTCN_error("Internal error: Module_Param::set_length_restriction()");
113 length_restriction = p_length_restriction;
114 }
115
116 void Module_Param::log(bool log_id) const {
117 if (log_id && id && id->is_explicit()) {
118 char* id_str = id->get_str();
119 TTCN_Logger::log_event_str(id_str);
120 Free(id_str);
121 TTCN_Logger::log_event_str(get_operation_type_sign_str());
122 }
123 log_value();
124 if (has_ifpresent) {
125 TTCN_Logger::log_event_str(" ifpresent");
126 }
127 if (length_restriction!=NULL) {
128 length_restriction->log();
129 }
130 }
131
132 void Module_Param::basic_check(int check_bits, const char* what) const {
133 bool is_template = check_bits & BC_TEMPLATE;
134 bool is_list = check_bits & BC_LIST;
135 if (is_template || !is_list) {
136 if (get_operation_type()!=OT_ASSIGN) error("The %s of %ss is not allowed.", get_operation_type_str(), what);
137 }
138 if (!is_template) {
139 if (has_ifpresent) error("%s cannot have an 'ifpresent' attribute", what);
140 }
141 if (!is_template || !is_list) {
142 if (length_restriction!=NULL) error("%s cannot have a length restriction", what);
143 }
144 }
145
146 void Module_Param::add_elem(Module_Param* /*value*/) {
147 TTCN_error("Internal error: Module_Param::add_elem()");
148 }
149
150 void Module_Param::add_list_with_implicit_ids(Vector<Module_Param*>* /*mp_list*/) {
151 TTCN_error("Internal error: Module_Param::add_list_with_implicit_ids()");
152 }
153
154 boolean Module_Param::get_boolean() const {
155 TTCN_error("Internal error: Module_Param::get_boolean()");
156 return FALSE;
157 }
158
159 size_t Module_Param::get_size() const {
160 TTCN_error("Internal error: Module_Param::get_size()");
161 return 0;
162 }
163
164 Module_Param* Module_Param::get_elem(size_t /*index*/) const {
165 TTCN_error("Internal error: Module_Param::get_elem()");
166 return NULL;
167 }
168
169 int Module_Param::get_string_size() const {
170 TTCN_error("Internal error: Module_Param::get_string_size()");
171 return 0;
172 }
173
174 void* Module_Param::get_string_data() const {
175 TTCN_error("Internal error: Module_Param::get_string_data()");
176 return NULL;
177 }
178
179 int_val_t* Module_Param::get_lower_int() const {
180 TTCN_error("Internal error: Module_Param::get_lower_int()");
181 return NULL;
182 }
183
184 int_val_t* Module_Param::get_upper_int() const {
185 TTCN_error("Internal error: Module_Param::get_upper_int()");
186 return NULL;
187 }
188
189 double Module_Param::get_lower_float() const {
190 TTCN_error("Internal error: Module_Param::get_lower_float()");
191 return 0.0;
192 }
193
194 double Module_Param::get_upper_float() const {
195 TTCN_error("Internal error: Module_Param::get_upper_float()");
196 return 0.0;
197 }
198
199 bool Module_Param::has_lower_float() const {
200 TTCN_error("Internal error: Module_Param::has_lower_float()");
201 return false;
202 }
203
204 bool Module_Param::has_upper_float() const {
205 TTCN_error("Internal error: Module_Param::has_upper_float()");
206 return false;
207 }
208
209 universal_char Module_Param::get_lower_uchar() const {
210 TTCN_error("Internal error: Module_Param::get_lower_uchar()");
211 return universal_char();
212 }
213
214 universal_char Module_Param::get_upper_uchar() const {
215 TTCN_error("Internal error: Module_Param::get_upper_uchar()");
216 return universal_char();
217 }
218
219 int_val_t* Module_Param::get_integer() const {
220 TTCN_error("Internal error: Module_Param::get_integer()");
221 return NULL;
222 }
223
224 double Module_Param::get_float() const {
225 TTCN_error("Internal error: Module_Param::get_float()");
226 return 0.0;
227 }
228
229 char* Module_Param::get_pattern() const {
230 TTCN_error("Internal error: Module_Param::get_pattern()");
231 return NULL;
232 }
233
234 verdicttype Module_Param::get_verdict() const {
235 TTCN_error("Internal error: Module_Param::get_verdict()");
236 return NONE;
237 }
238
239 char* Module_Param::get_enumerated() const {
240 TTCN_error("Internal error: Module_Param::get_enumerated()");
241 return NULL;
242 }
243
244 void Module_Param_NotUsed::log_value() const {
245 TTCN_Logger::log_event_str("-");
246 }
247
248 void Module_Param_Omit::log_value() const {
249 TTCN_Logger::log_event_str("omit");
250 }
251
252 Module_Param_Integer::Module_Param_Integer(int_val_t* p): integer_value(p) {
253 if (integer_value==NULL) TTCN_error("Internal error: Module_Param_Integer::Module_Param_Integer()");
254 }
255
256 void Module_Param_Integer::log_value() const {
257 if (integer_value->is_native()) {
258 INTEGER(integer_value->get_val()).log();
259 } else {
260 INTEGER integer;
261 integer.set_val(*integer_value);
262 integer.log();
263 }
264 }
265
266 void Module_Param_Float::log_value() const {
267 FLOAT(float_value).log();
268 }
269
270 void Module_Param_Boolean::log_value() const {
271 BOOLEAN(boolean_value).log();
272 }
273
274 void Module_Param_Verdict::log_value() const {
275 VERDICTTYPE(verdict_value).log();
276 }
277
278 void Module_Param_Objid::log_value() const {
279 OBJID(n_chars, chars_ptr).log();
280 }
281
282 void Module_Param_Bitstring::log_value() const {
283 BITSTRING(n_chars, chars_ptr).log();
284 }
285
286 void Module_Param_Hexstring::log_value() const {
287 HEXSTRING(n_chars, chars_ptr).log();
288 }
289
290 void Module_Param_Octetstring::log_value() const {
291 OCTETSTRING(n_chars, chars_ptr).log();
292 }
293
294 void Module_Param_Charstring::log_value() const {
295 CHARSTRING(n_chars, chars_ptr).log();
296 }
297
298 void Module_Param_Universal_Charstring::log_value() const {
299 UNIVERSAL_CHARSTRING(n_chars, chars_ptr).log();
300 }
301
302 void Module_Param_Enumerated::log_value() const {
303 TTCN_Logger::log_event_str(enum_value);
304 }
305
306 void Module_Param_Ttcn_Null::log_value() const {
307 TTCN_Logger::log_event_str("null");
308 }
309
310 void Module_Param_Ttcn_mtc::log_value() const {
311 TTCN_Logger::log_event_str("mtc");
312 }
313
314 void Module_Param_Ttcn_system::log_value() const {
315 TTCN_Logger::log_event_str("system");
316 }
317
318 void Module_Param_Asn_Null::log_value() const {
319 TTCN_Logger::log_event_str("NULL");
320 }
321
322 void Module_Param_Any::log_value() const {
323 TTCN_Logger::log_event_str("?");
324 }
325
326 void Module_Param_AnyOrNone::log_value() const {
327 TTCN_Logger::log_event_str("*");
328 }
329
330 void Module_Param_IntRange::log_bound(int_val_t* bound, bool is_lower) {
331 if (bound==NULL) {
332 if (is_lower) TTCN_Logger::log_event_str("-");
333 TTCN_Logger::log_event_str("infinity");
334 } else if (bound->is_native()) {
335 INTEGER(bound->get_val()).log();
336 } else {
337 INTEGER integer;
338 integer.set_val(*bound);
339 integer.log();
340 }
341 }
342
343 void Module_Param_IntRange::log_value() const {
344 TTCN_Logger::log_event_str("(");
345 log_bound(lower_bound, true);
346 TTCN_Logger::log_event_str("..");
347 log_bound(upper_bound, false);
348 TTCN_Logger::log_event_str(")");
349 }
350
351 void Module_Param_FloatRange::log_value() const {
352 TTCN_Logger::log_event_str("(");
353 if (has_lower) FLOAT(lower_bound).log();
354 else TTCN_Logger::log_event_str("-infinity");
355 TTCN_Logger::log_event_str("..");
356 if (has_upper) FLOAT(upper_bound).log();
357 else TTCN_Logger::log_event_str("infinity");
358 TTCN_Logger::log_event_str(")");
359 }
360
361 void Module_Param_StringRange::log_value() const {
362 TTCN_Logger::log_event_str("(");
363 UNIVERSAL_CHARSTRING(lower_bound).log();
364 TTCN_Logger::log_event_str("..");
365 UNIVERSAL_CHARSTRING(upper_bound).log();
366 TTCN_Logger::log_event_str(")");
367 }
368
369 void Module_Param_Pattern::log_value() const {
370 TTCN_Logger::log_event_str("pattern \"");
371 TTCN_Logger::log_event_str(pattern);
372 TTCN_Logger::log_event_str("\"");
373 }
374
375 void Module_Param_Bitstring_Template::log_value() const {
376 BITSTRING_template((unsigned int)n_chars, chars_ptr).log();
377 }
378
379 void Module_Param_Hexstring_Template::log_value() const {
380 HEXSTRING_template((unsigned int)n_chars, chars_ptr).log();
381 }
382
383 void Module_Param_Octetstring_Template::log_value() const {
384 OCTETSTRING_template((unsigned int)n_chars, chars_ptr).log();
385 }
386
387 Module_Param_Compound::~Module_Param_Compound() {
388 for (size_t i=0; i<values.size(); i++) {
389 delete values[i];
390 }
391 values.clear();
392 }
393
394 size_t Module_Param_Compound::get_size() const {
395 return values.size();
396 }
397
398 Module_Param* Module_Param_Compound::get_elem(size_t index) const {
399 if (index>=values.size()) TTCN_error("Internal error: Module_Param::get_elem(): index overflow");
400 return values[index];
401 }
402
403 void Module_Param_Compound::log_value_vec(const char* begin_str, const char* end_str) const {
404 TTCN_Logger::log_event_str(begin_str);
405 TTCN_Logger::log_event_str(" ");
406 for (size_t i=0; i<values.size(); ++i) {
407 if (i>0) TTCN_Logger::log_event_str(", ");
408 values[i]->log();
409 }
410 if (!values.empty()) TTCN_Logger::log_event_str(" ");
411 TTCN_Logger::log_event_str(end_str);
412 }
413
414 void Module_Param_Compound::add_elem(Module_Param* value) {
415 value->set_parent(this);
416 values.push_back(value);
417 }
418
419 void Module_Param_Compound::add_list_with_implicit_ids(Vector<Module_Param*>* mp_list) {
420 for (size_t i=0; i<mp_list->size(); i++) {
421 Module_Param* mp_current = mp_list->at(i);
422 mp_current->set_id(new Module_Param_Index(get_size(),false));
423 add_elem(mp_current);
424 }
425 }
426
427 char* Module_Param::get_param_context() const {
428 char* result = NULL;
429 if (parent != NULL) {
430 result = parent->get_param_context();
431 }
432 if (id) {
433 char* id_str = id->get_str();
434 if (parent!=NULL && !id->is_index()) {
435 result = mputc(result, '.');
436 }
437 result = mputstr(result, id_str);
438 Free(id_str);
439 }
440 return result;
441 }
442
443 void Module_Param::error(const char* err_msg, ...) const {
444 if (Ttcn_String_Parsing::happening()) {
445 char* exception_str = mcopystr("Error while setting parameter field '");
446 char* param_ctx = get_param_context();
447 exception_str = mputstr(exception_str, param_ctx);
448 Free(param_ctx);
449 exception_str = mputstr(exception_str, "': ");
450 va_list p_var;
451 va_start(p_var, err_msg);
452 char* error_msg_str = mprintf_va_list(err_msg, p_var);
453 va_end(p_var);
454 exception_str = mputstr(exception_str, error_msg_str);
455 Free(error_msg_str);
456 TTCN_error_begin("%s", exception_str);
457 Free(exception_str);
458 TTCN_error_end();
459 }
460 TTCN_Logger::begin_event(TTCN_Logger::ERROR_UNQUALIFIED);
461 TTCN_Logger::log_event_str("Error while ");
462 switch (operation_type) {
463 case OT_ASSIGN: TTCN_Logger::log_event_str("setting"); break;
464 case OT_CONCAT: TTCN_Logger::log_event_str("concatenating"); break;
465 default: TTCN_Logger::log_event_str("???");
466 }
467 TTCN_Logger::log_event_str(" parameter field '");
468 char* param_ctx = get_param_context();
469 TTCN_Logger::log_event_str(param_ctx);
470 Free(param_ctx);
471 switch (operation_type) {
472 case OT_ASSIGN: TTCN_Logger::log_event_str("' to '"); break;
473 case OT_CONCAT: TTCN_Logger::log_event_str("' and '"); break;
474 default: TTCN_Logger::log_event_str("' ??? '");
475 }
476 log(false);
477 TTCN_Logger::log_event_str("': ");
478 va_list p_var;
479 va_start(p_var, err_msg);
480 TTCN_Logger::log_event_va_list(err_msg, p_var);
481 va_end(p_var);
482 TTCN_Logger::send_event_as_error();
483 TTCN_Logger::end_event();
484 throw TC_Error();
485 }
486
487 bool Ttcn_String_Parsing::string_parsing = false;
This page took 0.041144 seconds and 5 git commands to generate.